2017-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / cp / pt.c
blob416f1323f4fa5ff3e89549cc27322c00a9875ce3
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"
44 /* The type of functions taking a tree, and some additional data, and
45 returning an int. */
46 typedef int (*tree_fn_t) (tree, void*);
48 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51 struct GTY ((chain_next ("%h.next"))) pending_template {
52 struct pending_template *next;
53 struct tinst_level *tinst;
56 static GTY(()) struct pending_template *pending_templates;
57 static GTY(()) struct pending_template *last_pending_template;
59 int processing_template_parmlist;
60 static int template_header_count;
62 static GTY(()) tree saved_trees;
63 static vec<int> inline_parm_levels;
65 static GTY(()) struct tinst_level *current_tinst_level;
67 static GTY(()) tree saved_access_scope;
69 /* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72 static tree cur_stmt_expr;
74 // -------------------------------------------------------------------------- //
75 // Local Specialization Stack
77 // Implementation of the RAII helper for creating new local
78 // specializations.
79 local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations)
82 local_specializations = new hash_map<tree, tree>;
85 local_specialization_stack::~local_specialization_stack ()
87 delete local_specializations;
88 local_specializations = saved;
91 /* True if we've recursed into fn_type_unification too many times. */
92 static bool excessive_deduction_depth;
94 struct GTY((for_user)) spec_entry
96 tree tmpl;
97 tree args;
98 tree spec;
101 struct spec_hasher : ggc_ptr_hash<spec_entry>
103 static hashval_t hash (spec_entry *);
104 static bool equal (spec_entry *, spec_entry *);
107 static GTY (()) hash_table<spec_hasher> *decl_specializations;
109 static GTY (()) hash_table<spec_hasher> *type_specializations;
111 /* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
117 #define UNIFY_ALLOW_NONE 0
118 #define UNIFY_ALLOW_MORE_CV_QUAL 1
119 #define UNIFY_ALLOW_LESS_CV_QUAL 2
120 #define UNIFY_ALLOW_DERIVED 4
121 #define UNIFY_ALLOW_INTEGER 8
122 #define UNIFY_ALLOW_OUTER_LEVEL 16
123 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
126 enum template_base_result {
127 tbr_incomplete_type,
128 tbr_ambiguous_baseclass,
129 tbr_success
132 static void push_access_scope (tree);
133 static void pop_access_scope (tree);
134 static bool resolve_overloaded_unification (tree, tree, tree, tree,
135 unification_kind_t, int,
136 bool);
137 static int try_one_overload (tree, tree, tree, tree, tree,
138 unification_kind_t, int, bool, bool);
139 static int unify (tree, tree, tree, tree, int, bool);
140 static void add_pending_template (tree);
141 static tree reopen_tinst_level (struct tinst_level *);
142 static tree tsubst_initializer_list (tree, tree);
143 static tree get_partial_spec_bindings (tree, tree, tree);
144 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
145 bool, bool);
146 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
147 bool, bool);
148 static void tsubst_enum (tree, tree, tree);
149 static tree add_to_template_args (tree, tree);
150 static tree add_outermost_template_args (tree, tree);
151 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
152 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
153 tree);
154 static int type_unification_real (tree, tree, tree, const tree *,
155 unsigned int, int, unification_kind_t, int,
156 vec<deferred_access_check, va_gc> **,
157 bool);
158 static void note_template_header (int);
159 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
160 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
161 static tree convert_template_argument (tree, tree, tree,
162 tsubst_flags_t, int, tree);
163 static tree for_each_template_parm (tree, tree_fn_t, void*,
164 hash_set<tree> *, bool, tree_fn_t = NULL);
165 static tree expand_template_argument_pack (tree);
166 static tree build_template_parm_index (int, int, int, tree, tree);
167 static bool inline_needs_template_parms (tree, bool);
168 static void push_inline_template_parms_recursive (tree, int);
169 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
170 static int mark_template_parm (tree, void *);
171 static int template_parm_this_level_p (tree, void *);
172 static tree tsubst_friend_function (tree, tree);
173 static tree tsubst_friend_class (tree, tree);
174 static int can_complete_type_without_circularity (tree);
175 static tree get_bindings (tree, tree, tree, bool);
176 static int template_decl_level (tree);
177 static int check_cv_quals_for_unify (int, tree, tree);
178 static void template_parm_level_and_index (tree, int*, int*);
179 static int unify_pack_expansion (tree, tree, tree,
180 tree, unification_kind_t, bool, bool);
181 static tree copy_template_args (tree);
182 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
184 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
185 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
186 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
187 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
189 static bool check_specialization_scope (void);
190 static tree process_partial_specialization (tree);
191 static void set_current_access_from_decl (tree);
192 static enum template_base_result get_template_base (tree, tree, tree, tree,
193 bool , tree *);
194 static tree try_class_unification (tree, tree, tree, tree, bool);
195 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
196 tree, tree);
197 static bool template_template_parm_bindings_ok_p (tree, tree);
198 static void tsubst_default_arguments (tree, tsubst_flags_t);
199 static tree for_each_template_parm_r (tree *, int *, void *);
200 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
201 static void copy_default_args_to_explicit_spec (tree);
202 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
203 static bool dependent_template_arg_p (tree);
204 static bool any_template_arguments_need_structural_equality_p (tree);
205 static bool dependent_type_p_r (tree);
206 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
207 static tree tsubst_decl (tree, tree, tsubst_flags_t);
208 static void perform_typedefs_access_check (tree tmpl, tree targs);
209 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
210 location_t);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215 static bool complex_alias_template_p (const_tree tmpl);
216 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
217 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
219 /* Make the current scope suitable for access checking when we are
220 processing T. T can be FUNCTION_DECL for instantiated function
221 template, VAR_DECL for static member variable, or TYPE_DECL for
222 alias template (needed by instantiate_decl). */
224 static void
225 push_access_scope (tree t)
227 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
228 || TREE_CODE (t) == TYPE_DECL);
230 if (DECL_FRIEND_CONTEXT (t))
231 push_nested_class (DECL_FRIEND_CONTEXT (t));
232 else if (DECL_CLASS_SCOPE_P (t))
233 push_nested_class (DECL_CONTEXT (t));
234 else
235 push_to_top_level ();
237 if (TREE_CODE (t) == FUNCTION_DECL)
239 saved_access_scope = tree_cons
240 (NULL_TREE, current_function_decl, saved_access_scope);
241 current_function_decl = t;
245 /* Restore the scope set up by push_access_scope. T is the node we
246 are processing. */
248 static void
249 pop_access_scope (tree t)
251 if (TREE_CODE (t) == FUNCTION_DECL)
253 current_function_decl = TREE_VALUE (saved_access_scope);
254 saved_access_scope = TREE_CHAIN (saved_access_scope);
257 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
258 pop_nested_class ();
259 else
260 pop_from_top_level ();
263 /* Do any processing required when DECL (a member template
264 declaration) is finished. Returns the TEMPLATE_DECL corresponding
265 to DECL, unless it is a specialization, in which case the DECL
266 itself is returned. */
268 tree
269 finish_member_template_decl (tree decl)
271 if (decl == error_mark_node)
272 return error_mark_node;
274 gcc_assert (DECL_P (decl));
276 if (TREE_CODE (decl) == TYPE_DECL)
278 tree type;
280 type = TREE_TYPE (decl);
281 if (type == error_mark_node)
282 return error_mark_node;
283 if (MAYBE_CLASS_TYPE_P (type)
284 && CLASSTYPE_TEMPLATE_INFO (type)
285 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
287 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
288 check_member_template (tmpl);
289 return tmpl;
291 return NULL_TREE;
293 else if (TREE_CODE (decl) == FIELD_DECL)
294 error ("data member %qD cannot be a member template", decl);
295 else if (DECL_TEMPLATE_INFO (decl))
297 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
299 check_member_template (DECL_TI_TEMPLATE (decl));
300 return DECL_TI_TEMPLATE (decl);
302 else
303 return decl;
305 else
306 error ("invalid member template declaration %qD", decl);
308 return error_mark_node;
311 /* Create a template info node. */
313 tree
314 build_template_info (tree template_decl, tree template_args)
316 tree result = make_node (TEMPLATE_INFO);
317 TI_TEMPLATE (result) = template_decl;
318 TI_ARGS (result) = template_args;
319 return result;
322 /* Return the template info node corresponding to T, whatever T is. */
324 tree
325 get_template_info (const_tree t)
327 tree tinfo = NULL_TREE;
329 if (!t || t == error_mark_node)
330 return NULL;
332 if (TREE_CODE (t) == NAMESPACE_DECL
333 || TREE_CODE (t) == PARM_DECL)
334 return NULL;
336 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
337 tinfo = DECL_TEMPLATE_INFO (t);
339 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
340 t = TREE_TYPE (t);
342 if (OVERLOAD_TYPE_P (t))
343 tinfo = TYPE_TEMPLATE_INFO (t);
344 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
345 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
347 return tinfo;
350 /* Returns the template nesting level of the indicated class TYPE.
352 For example, in:
353 template <class T>
354 struct A
356 template <class U>
357 struct B {};
360 A<T>::B<U> has depth two, while A<T> has depth one.
361 Both A<T>::B<int> and A<int>::B<U> have depth one, if
362 they are instantiations, not specializations.
364 This function is guaranteed to return 0 if passed NULL_TREE so
365 that, for example, `template_class_depth (current_class_type)' is
366 always safe. */
369 template_class_depth (tree type)
371 int depth;
373 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
375 tree tinfo = get_template_info (type);
377 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
378 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
379 ++depth;
381 if (DECL_P (type))
382 type = CP_DECL_CONTEXT (type);
383 else if (LAMBDA_TYPE_P (type))
384 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
385 else
386 type = CP_TYPE_CONTEXT (type);
389 return depth;
392 /* Subroutine of maybe_begin_member_template_processing.
393 Returns true if processing DECL needs us to push template parms. */
395 static bool
396 inline_needs_template_parms (tree decl, bool nsdmi)
398 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
399 return false;
401 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
402 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
405 /* Subroutine of maybe_begin_member_template_processing.
406 Push the template parms in PARMS, starting from LEVELS steps into the
407 chain, and ending at the beginning, since template parms are listed
408 innermost first. */
410 static void
411 push_inline_template_parms_recursive (tree parmlist, int levels)
413 tree parms = TREE_VALUE (parmlist);
414 int i;
416 if (levels > 1)
417 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
419 ++processing_template_decl;
420 current_template_parms
421 = tree_cons (size_int (processing_template_decl),
422 parms, current_template_parms);
423 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
425 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
426 NULL);
427 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
429 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
431 if (error_operand_p (parm))
432 continue;
434 gcc_assert (DECL_P (parm));
436 switch (TREE_CODE (parm))
438 case TYPE_DECL:
439 case TEMPLATE_DECL:
440 pushdecl (parm);
441 break;
443 case PARM_DECL:
444 /* Push the CONST_DECL. */
445 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
446 break;
448 default:
449 gcc_unreachable ();
454 /* Restore the template parameter context for a member template, a
455 friend template defined in a class definition, or a non-template
456 member of template class. */
458 void
459 maybe_begin_member_template_processing (tree decl)
461 tree parms;
462 int levels = 0;
463 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
465 if (nsdmi)
467 tree ctx = DECL_CONTEXT (decl);
468 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
469 /* Disregard full specializations (c++/60999). */
470 && uses_template_parms (ctx)
471 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
474 if (inline_needs_template_parms (decl, nsdmi))
476 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
477 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
479 if (DECL_TEMPLATE_SPECIALIZATION (decl))
481 --levels;
482 parms = TREE_CHAIN (parms);
485 push_inline_template_parms_recursive (parms, levels);
488 /* Remember how many levels of template parameters we pushed so that
489 we can pop them later. */
490 inline_parm_levels.safe_push (levels);
493 /* Undo the effects of maybe_begin_member_template_processing. */
495 void
496 maybe_end_member_template_processing (void)
498 int i;
499 int last;
501 if (inline_parm_levels.length () == 0)
502 return;
504 last = inline_parm_levels.pop ();
505 for (i = 0; i < last; ++i)
507 --processing_template_decl;
508 current_template_parms = TREE_CHAIN (current_template_parms);
509 poplevel (0, 0, 0);
513 /* Return a new template argument vector which contains all of ARGS,
514 but has as its innermost set of arguments the EXTRA_ARGS. */
516 static tree
517 add_to_template_args (tree args, tree extra_args)
519 tree new_args;
520 int extra_depth;
521 int i;
522 int j;
524 if (args == NULL_TREE || extra_args == error_mark_node)
525 return extra_args;
527 extra_depth = TMPL_ARGS_DEPTH (extra_args);
528 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
530 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
531 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
533 for (j = 1; j <= extra_depth; ++j, ++i)
534 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
536 return new_args;
539 /* Like add_to_template_args, but only the outermost ARGS are added to
540 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
541 (EXTRA_ARGS) levels are added. This function is used to combine
542 the template arguments from a partial instantiation with the
543 template arguments used to attain the full instantiation from the
544 partial instantiation. */
546 static tree
547 add_outermost_template_args (tree args, tree extra_args)
549 tree new_args;
551 /* If there are more levels of EXTRA_ARGS than there are ARGS,
552 something very fishy is going on. */
553 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
555 /* If *all* the new arguments will be the EXTRA_ARGS, just return
556 them. */
557 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
558 return extra_args;
560 /* For the moment, we make ARGS look like it contains fewer levels. */
561 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
563 new_args = add_to_template_args (args, extra_args);
565 /* Now, we restore ARGS to its full dimensions. */
566 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
568 return new_args;
571 /* Return the N levels of innermost template arguments from the ARGS. */
573 tree
574 get_innermost_template_args (tree args, int n)
576 tree new_args;
577 int extra_levels;
578 int i;
580 gcc_assert (n >= 0);
582 /* If N is 1, just return the innermost set of template arguments. */
583 if (n == 1)
584 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
586 /* If we're not removing anything, just return the arguments we were
587 given. */
588 extra_levels = TMPL_ARGS_DEPTH (args) - n;
589 gcc_assert (extra_levels >= 0);
590 if (extra_levels == 0)
591 return args;
593 /* Make a new set of arguments, not containing the outer arguments. */
594 new_args = make_tree_vec (n);
595 for (i = 1; i <= n; ++i)
596 SET_TMPL_ARGS_LEVEL (new_args, i,
597 TMPL_ARGS_LEVEL (args, i + extra_levels));
599 return new_args;
602 /* The inverse of get_innermost_template_args: Return all but the innermost
603 EXTRA_LEVELS levels of template arguments from the ARGS. */
605 static tree
606 strip_innermost_template_args (tree args, int extra_levels)
608 tree new_args;
609 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
610 int i;
612 gcc_assert (n >= 0);
614 /* If N is 1, just return the outermost set of template arguments. */
615 if (n == 1)
616 return TMPL_ARGS_LEVEL (args, 1);
618 /* If we're not removing anything, just return the arguments we were
619 given. */
620 gcc_assert (extra_levels >= 0);
621 if (extra_levels == 0)
622 return args;
624 /* Make a new set of arguments, not containing the inner arguments. */
625 new_args = make_tree_vec (n);
626 for (i = 1; i <= n; ++i)
627 SET_TMPL_ARGS_LEVEL (new_args, i,
628 TMPL_ARGS_LEVEL (args, i));
630 return new_args;
633 /* We've got a template header coming up; push to a new level for storing
634 the parms. */
636 void
637 begin_template_parm_list (void)
639 /* We use a non-tag-transparent scope here, which causes pushtag to
640 put tags in this scope, rather than in the enclosing class or
641 namespace scope. This is the right thing, since we want
642 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
643 global template class, push_template_decl handles putting the
644 TEMPLATE_DECL into top-level scope. For a nested template class,
645 e.g.:
647 template <class T> struct S1 {
648 template <class T> struct S2 {};
651 pushtag contains special code to call pushdecl_with_scope on the
652 TEMPLATE_DECL for S2. */
653 begin_scope (sk_template_parms, NULL);
654 ++processing_template_decl;
655 ++processing_template_parmlist;
656 note_template_header (0);
658 /* Add a dummy parameter level while we process the parameter list. */
659 current_template_parms
660 = tree_cons (size_int (processing_template_decl),
661 make_tree_vec (0),
662 current_template_parms);
665 /* This routine is called when a specialization is declared. If it is
666 invalid to declare a specialization here, an error is reported and
667 false is returned, otherwise this routine will return true. */
669 static bool
670 check_specialization_scope (void)
672 tree scope = current_scope ();
674 /* [temp.expl.spec]
676 An explicit specialization shall be declared in the namespace of
677 which the template is a member, or, for member templates, in the
678 namespace of which the enclosing class or enclosing class
679 template is a member. An explicit specialization of a member
680 function, member class or static data member of a class template
681 shall be declared in the namespace of which the class template
682 is a member. */
683 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
685 error ("explicit specialization in non-namespace scope %qD", scope);
686 return false;
689 /* [temp.expl.spec]
691 In an explicit specialization declaration for a member of a class
692 template or a member template that appears in namespace scope,
693 the member template and some of its enclosing class templates may
694 remain unspecialized, except that the declaration shall not
695 explicitly specialize a class member template if its enclosing
696 class templates are not explicitly specialized as well. */
697 if (current_template_parms)
699 error ("enclosing class templates are not explicitly specialized");
700 return false;
703 return true;
706 /* We've just seen template <>. */
708 bool
709 begin_specialization (void)
711 begin_scope (sk_template_spec, NULL);
712 note_template_header (1);
713 return check_specialization_scope ();
716 /* Called at then end of processing a declaration preceded by
717 template<>. */
719 void
720 end_specialization (void)
722 finish_scope ();
723 reset_specialization ();
726 /* Any template <>'s that we have seen thus far are not referring to a
727 function specialization. */
729 void
730 reset_specialization (void)
732 processing_specialization = 0;
733 template_header_count = 0;
736 /* We've just seen a template header. If SPECIALIZATION is nonzero,
737 it was of the form template <>. */
739 static void
740 note_template_header (int specialization)
742 processing_specialization = specialization;
743 template_header_count++;
746 /* We're beginning an explicit instantiation. */
748 void
749 begin_explicit_instantiation (void)
751 gcc_assert (!processing_explicit_instantiation);
752 processing_explicit_instantiation = true;
756 void
757 end_explicit_instantiation (void)
759 gcc_assert (processing_explicit_instantiation);
760 processing_explicit_instantiation = false;
763 /* An explicit specialization or partial specialization of TMPL is being
764 declared. Check that the namespace in which the specialization is
765 occurring is permissible. Returns false iff it is invalid to
766 specialize TMPL in the current namespace. */
768 static bool
769 check_specialization_namespace (tree tmpl)
771 tree tpl_ns = decl_namespace_context (tmpl);
773 /* [tmpl.expl.spec]
775 An explicit specialization shall be declared in a namespace enclosing the
776 specialized template. An explicit specialization whose declarator-id is
777 not qualified shall be declared in the nearest enclosing namespace of the
778 template, or, if the namespace is inline (7.3.1), any namespace from its
779 enclosing namespace set. */
780 if (current_scope() != DECL_CONTEXT (tmpl)
781 && !at_namespace_scope_p ())
783 error ("specialization of %qD must appear at namespace scope", tmpl);
784 return false;
787 if (cxx_dialect < cxx11
788 ? is_associated_namespace (current_namespace, tpl_ns)
789 : is_ancestor (current_namespace, tpl_ns))
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_ancestor (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 tree class_template;
1193 tree class_specialization;
1194 vec<tree, va_gc> *methods;
1195 tree fns;
1196 int idx;
1198 /* The template arguments actually apply to the containing
1199 class. Find the class specialization with those
1200 arguments. */
1201 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1202 class_specialization
1203 = retrieve_specialization (class_template, args, 0);
1204 if (!class_specialization)
1205 return NULL_TREE;
1206 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1207 for the specialization. */
1208 idx = class_method_index_for_fn (class_specialization, tmpl);
1209 if (idx == -1)
1210 return NULL_TREE;
1211 /* Iterate through the methods with the indicated name, looking
1212 for the one that has an instance of TMPL. */
1213 methods = CLASSTYPE_METHOD_VEC (class_specialization);
1214 for (fns = (*methods)[idx]; fns; fns = OVL_NEXT (fns))
1216 tree fn = OVL_CURRENT (fns);
1217 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn) == class_specialization)
1221 return fn;
1223 return NULL_TREE;
1225 else
1227 spec_entry *found;
1228 spec_entry elt;
1229 hash_table<spec_hasher> *specializations;
1231 elt.tmpl = tmpl;
1232 elt.args = args;
1233 elt.spec = NULL_TREE;
1235 if (DECL_CLASS_TEMPLATE_P (tmpl))
1236 specializations = type_specializations;
1237 else
1238 specializations = decl_specializations;
1240 if (hash == 0)
1241 hash = spec_hasher::hash (&elt);
1242 found = specializations->find_with_hash (&elt, hash);
1243 if (found)
1244 return found->spec;
1247 return NULL_TREE;
1250 /* Like retrieve_specialization, but for local declarations. */
1252 tree
1253 retrieve_local_specialization (tree tmpl)
1255 if (local_specializations == NULL)
1256 return NULL_TREE;
1258 tree *slot = local_specializations->get (tmpl);
1259 return slot ? *slot : NULL_TREE;
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1265 is_specialization_of (tree decl, tree tmpl)
1267 tree t;
1269 if (TREE_CODE (decl) == FUNCTION_DECL)
1271 for (t = decl;
1272 t != NULL_TREE;
1273 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1274 if (t == tmpl)
1275 return 1;
1277 else
1279 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1281 for (t = TREE_TYPE (decl);
1282 t != NULL_TREE;
1283 t = CLASSTYPE_USE_TEMPLATE (t)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1285 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1286 return 1;
1289 return 0;
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1295 bool
1296 is_specialization_of_friend (tree decl, tree friend_decl)
1298 bool need_template = true;
1299 int template_depth;
1301 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1302 || TREE_CODE (decl) == TYPE_DECL);
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1306 if this. */
1307 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl)
1309 && !DECL_USE_TEMPLATE (friend_decl))
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1313 need_template = false;
1315 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl))
1317 need_template = false;
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1321 return false;
1323 if (is_specialization_of (decl, friend_decl))
1324 return true;
1326 /* [temp.friend/6]
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1332 For example, given a template friend declaration
1334 template <class T> friend void A<T>::f();
1336 the member function below is considered a friend
1338 template <> struct A<int> {
1339 void f();
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1346 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1347 if (template_depth
1348 && DECL_CLASS_SCOPE_P (decl)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1358 and DECL's are
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368 tree context = DECL_CONTEXT (decl);
1369 tree args = NULL_TREE;
1370 int current_depth = 0;
1372 while (current_depth < template_depth)
1374 if (CLASSTYPE_TEMPLATE_INFO (context))
1376 if (current_depth == 0)
1377 args = TYPE_TI_ARGS (context);
1378 else
1379 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1380 current_depth++;
1382 context = TYPE_CONTEXT (context);
1385 if (TREE_CODE (decl) == FUNCTION_DECL)
1387 bool is_template;
1388 tree friend_type;
1389 tree decl_type;
1390 tree friend_args_type;
1391 tree decl_args_type;
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 non-templates. */
1395 is_template = DECL_TEMPLATE_INFO (decl)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1397 if (need_template ^ is_template)
1398 return false;
1399 else if (is_template)
1401 /* If both are templates, check template parameter list. */
1402 tree friend_parms
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1404 args, tf_none);
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1407 friend_parms))
1408 return false;
1410 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1412 else
1413 decl_type = TREE_TYPE (decl);
1415 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1416 tf_none, NULL_TREE);
1417 if (friend_type == error_mark_node)
1418 return false;
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1422 return false;
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type = TYPE_ARG_TYPES (friend_type);
1427 decl_args_type = TYPE_ARG_TYPES (decl_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1429 friend_args_type = TREE_CHAIN (friend_args_type);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1431 decl_args_type = TREE_CHAIN (decl_args_type);
1433 return compparms (decl_args_type, friend_args_type);
1435 else
1437 /* DECL is a TYPE_DECL */
1438 bool is_template;
1439 tree decl_type = TREE_TYPE (decl);
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 non-templates. */
1443 is_template
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1447 if (need_template ^ is_template)
1448 return false;
1449 else if (is_template)
1451 tree friend_parms;
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1455 != DECL_NAME (friend_decl))
1456 return false;
1458 /* Now check template parameter list. */
1459 friend_parms
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1461 args, tf_none);
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1464 friend_parms);
1466 else
1467 return (DECL_NAME (decl)
1468 == DECL_NAME (friend_decl));
1471 return false;
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. Returns SPEC, or an
1477 equivalent prior declaration, if available.
1479 We also store instantiations of field packs in the hash table, even
1480 though they are not themselves templates, to make lookup easier. */
1482 static tree
1483 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1484 hashval_t hash)
1486 tree fn;
1487 spec_entry **slot = NULL;
1488 spec_entry elt;
1490 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1491 || (TREE_CODE (tmpl) == FIELD_DECL
1492 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1494 if (TREE_CODE (spec) == FUNCTION_DECL
1495 && uses_template_parms (DECL_TI_ARGS (spec)))
1496 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1497 register it; we want the corresponding TEMPLATE_DECL instead.
1498 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1499 the more obvious `uses_template_parms (spec)' to avoid problems
1500 with default function arguments. In particular, given
1501 something like this:
1503 template <class T> void f(T t1, T t = T())
1505 the default argument expression is not substituted for in an
1506 instantiation unless and until it is actually needed. */
1507 return spec;
1509 if (optimize_specialization_lookup_p (tmpl))
1510 /* We don't put these specializations in the hash table, but we might
1511 want to give an error about a mismatch. */
1512 fn = retrieve_specialization (tmpl, args, 0);
1513 else
1515 elt.tmpl = tmpl;
1516 elt.args = args;
1517 elt.spec = spec;
1519 if (hash == 0)
1520 hash = spec_hasher::hash (&elt);
1522 slot =
1523 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1524 if (*slot)
1525 fn = ((spec_entry *) *slot)->spec;
1526 else
1527 fn = NULL_TREE;
1530 /* We can sometimes try to re-register a specialization that we've
1531 already got. In particular, regenerate_decl_from_template calls
1532 duplicate_decls which will update the specialization list. But,
1533 we'll still get called again here anyhow. It's more convenient
1534 to simply allow this than to try to prevent it. */
1535 if (fn == spec)
1536 return spec;
1537 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1539 if (DECL_TEMPLATE_INSTANTIATION (fn))
1541 if (DECL_ODR_USED (fn)
1542 || DECL_EXPLICIT_INSTANTIATION (fn))
1544 error ("specialization of %qD after instantiation",
1545 fn);
1546 return error_mark_node;
1548 else
1550 tree clone;
1551 /* This situation should occur only if the first
1552 specialization is an implicit instantiation, the
1553 second is an explicit specialization, and the
1554 implicit instantiation has not yet been used. That
1555 situation can occur if we have implicitly
1556 instantiated a member function and then specialized
1557 it later.
1559 We can also wind up here if a friend declaration that
1560 looked like an instantiation turns out to be a
1561 specialization:
1563 template <class T> void foo(T);
1564 class S { friend void foo<>(int) };
1565 template <> void foo(int);
1567 We transform the existing DECL in place so that any
1568 pointers to it become pointers to the updated
1569 declaration.
1571 If there was a definition for the template, but not
1572 for the specialization, we want this to look as if
1573 there were no definition, and vice versa. */
1574 DECL_INITIAL (fn) = NULL_TREE;
1575 duplicate_decls (spec, fn, is_friend);
1576 /* The call to duplicate_decls will have applied
1577 [temp.expl.spec]:
1579 An explicit specialization of a function template
1580 is inline only if it is explicitly declared to be,
1581 and independently of whether its function template
1584 to the primary function; now copy the inline bits to
1585 the various clones. */
1586 FOR_EACH_CLONE (clone, fn)
1588 DECL_DECLARED_INLINE_P (clone)
1589 = DECL_DECLARED_INLINE_P (fn);
1590 DECL_SOURCE_LOCATION (clone)
1591 = DECL_SOURCE_LOCATION (fn);
1592 DECL_DELETED_FN (clone)
1593 = DECL_DELETED_FN (fn);
1595 check_specialization_namespace (tmpl);
1597 return fn;
1600 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1602 tree dd = duplicate_decls (spec, fn, is_friend);
1603 if (dd == error_mark_node)
1604 /* We've already complained in duplicate_decls. */
1605 return error_mark_node;
1607 if (dd == NULL_TREE && DECL_INITIAL (spec))
1608 /* Dup decl failed, but this is a new definition. Set the
1609 line number so any errors match this new
1610 definition. */
1611 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1613 return fn;
1616 else if (fn)
1617 return duplicate_decls (spec, fn, is_friend);
1619 /* A specialization must be declared in the same namespace as the
1620 template it is specializing. */
1621 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1622 && !check_specialization_namespace (tmpl))
1623 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1625 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1627 spec_entry *entry = ggc_alloc<spec_entry> ();
1628 gcc_assert (tmpl && args && spec);
1629 *entry = elt;
1630 *slot = entry;
1631 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1632 && PRIMARY_TEMPLATE_P (tmpl)
1633 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1634 || variable_template_p (tmpl))
1635 /* If TMPL is a forward declaration of a template function, keep a list
1636 of all specializations in case we need to reassign them to a friend
1637 template later in tsubst_friend_function.
1639 Also keep a list of all variable template instantiations so that
1640 process_partial_specialization can check whether a later partial
1641 specialization would have used it. */
1642 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1643 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1646 return spec;
1649 /* Returns true iff two spec_entry nodes are equivalent. */
1651 int comparing_specializations;
1653 bool
1654 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1656 int equal;
1658 ++comparing_specializations;
1659 equal = (e1->tmpl == e2->tmpl
1660 && comp_template_args (e1->args, e2->args));
1661 if (equal && flag_concepts
1662 /* tmpl could be a FIELD_DECL for a capture pack. */
1663 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1664 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1665 && uses_template_parms (e1->args))
1667 /* Partial specializations of a variable template can be distinguished by
1668 constraints. */
1669 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1670 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1671 equal = equivalent_constraints (c1, c2);
1673 --comparing_specializations;
1675 return equal;
1678 /* Returns a hash for a template TMPL and template arguments ARGS. */
1680 static hashval_t
1681 hash_tmpl_and_args (tree tmpl, tree args)
1683 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1684 return iterative_hash_template_arg (args, val);
1687 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1688 ignoring SPEC. */
1690 hashval_t
1691 spec_hasher::hash (spec_entry *e)
1693 return hash_tmpl_and_args (e->tmpl, e->args);
1696 /* Recursively calculate a hash value for a template argument ARG, for use
1697 in the hash tables of template specializations. */
1699 hashval_t
1700 iterative_hash_template_arg (tree arg, hashval_t val)
1702 unsigned HOST_WIDE_INT i;
1703 enum tree_code code;
1704 char tclass;
1706 if (arg == NULL_TREE)
1707 return iterative_hash_object (arg, val);
1709 if (!TYPE_P (arg))
1710 STRIP_NOPS (arg);
1712 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1713 gcc_unreachable ();
1715 code = TREE_CODE (arg);
1716 tclass = TREE_CODE_CLASS (code);
1718 val = iterative_hash_object (code, val);
1720 switch (code)
1722 case ERROR_MARK:
1723 return val;
1725 case IDENTIFIER_NODE:
1726 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1728 case TREE_VEC:
1730 int i, len = TREE_VEC_LENGTH (arg);
1731 for (i = 0; i < len; ++i)
1732 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1733 return val;
1736 case TYPE_PACK_EXPANSION:
1737 case EXPR_PACK_EXPANSION:
1738 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1739 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1741 case TYPE_ARGUMENT_PACK:
1742 case NONTYPE_ARGUMENT_PACK:
1743 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1745 case TREE_LIST:
1746 for (; arg; arg = TREE_CHAIN (arg))
1747 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1748 return val;
1750 case OVERLOAD:
1751 for (; arg; arg = OVL_NEXT (arg))
1752 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1753 return val;
1755 case CONSTRUCTOR:
1757 tree field, value;
1758 iterative_hash_template_arg (TREE_TYPE (arg), val);
1759 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1761 val = iterative_hash_template_arg (field, val);
1762 val = iterative_hash_template_arg (value, val);
1764 return val;
1767 case PARM_DECL:
1768 if (!DECL_ARTIFICIAL (arg))
1770 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1771 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1773 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1775 case TARGET_EXPR:
1776 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1778 case PTRMEM_CST:
1779 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1780 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1782 case TEMPLATE_PARM_INDEX:
1783 val = iterative_hash_template_arg
1784 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1785 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1786 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1788 case TRAIT_EXPR:
1789 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1790 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1791 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1793 case BASELINK:
1794 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1795 val);
1796 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1797 val);
1799 case MODOP_EXPR:
1800 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1801 code = TREE_CODE (TREE_OPERAND (arg, 1));
1802 val = iterative_hash_object (code, val);
1803 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1805 case LAMBDA_EXPR:
1806 /* A lambda can't appear in a template arg, but don't crash on
1807 erroneous input. */
1808 gcc_assert (seen_error ());
1809 return val;
1811 case CAST_EXPR:
1812 case IMPLICIT_CONV_EXPR:
1813 case STATIC_CAST_EXPR:
1814 case REINTERPRET_CAST_EXPR:
1815 case CONST_CAST_EXPR:
1816 case DYNAMIC_CAST_EXPR:
1817 case NEW_EXPR:
1818 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1819 /* Now hash operands as usual. */
1820 break;
1822 default:
1823 break;
1826 switch (tclass)
1828 case tcc_type:
1829 if (alias_template_specialization_p (arg))
1831 // We want an alias specialization that survived strip_typedefs
1832 // to hash differently from its TYPE_CANONICAL, to avoid hash
1833 // collisions that compare as different in template_args_equal.
1834 // These could be dependent specializations that strip_typedefs
1835 // left alone, or untouched specializations because
1836 // coerce_template_parms returns the unconverted template
1837 // arguments if it sees incomplete argument packs.
1838 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1839 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1841 if (TYPE_CANONICAL (arg))
1842 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1843 val);
1844 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1845 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1846 /* Otherwise just compare the types during lookup. */
1847 return val;
1849 case tcc_declaration:
1850 case tcc_constant:
1851 return iterative_hash_expr (arg, val);
1853 default:
1854 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1856 unsigned n = cp_tree_operand_length (arg);
1857 for (i = 0; i < n; ++i)
1858 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1859 return val;
1862 gcc_unreachable ();
1863 return 0;
1866 /* Unregister the specialization SPEC as a specialization of TMPL.
1867 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1868 if the SPEC was listed as a specialization of TMPL.
1870 Note that SPEC has been ggc_freed, so we can't look inside it. */
1872 bool
1873 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1875 spec_entry *entry;
1876 spec_entry elt;
1878 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1879 elt.args = TI_ARGS (tinfo);
1880 elt.spec = NULL_TREE;
1882 entry = decl_specializations->find (&elt);
1883 if (entry != NULL)
1885 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1886 gcc_assert (new_spec != NULL_TREE);
1887 entry->spec = new_spec;
1888 return 1;
1891 return 0;
1894 /* Like register_specialization, but for local declarations. We are
1895 registering SPEC, an instantiation of TMPL. */
1897 void
1898 register_local_specialization (tree spec, tree tmpl)
1900 local_specializations->put (tmpl, spec);
1903 /* TYPE is a class type. Returns true if TYPE is an explicitly
1904 specialized class. */
1906 bool
1907 explicit_class_specialization_p (tree type)
1909 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1910 return false;
1911 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1914 /* Print the list of functions at FNS, going through all the overloads
1915 for each element of the list. Alternatively, FNS can not be a
1916 TREE_LIST, in which case it will be printed together with all the
1917 overloads.
1919 MORE and *STR should respectively be FALSE and NULL when the function
1920 is called from the outside. They are used internally on recursive
1921 calls. print_candidates manages the two parameters and leaves NULL
1922 in *STR when it ends. */
1924 static void
1925 print_candidates_1 (tree fns, bool more, const char **str)
1927 tree fn, fn2;
1928 char *spaces = NULL;
1930 for (fn = fns; fn; fn = OVL_NEXT (fn))
1931 if (TREE_CODE (fn) == TREE_LIST)
1933 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1934 print_candidates_1 (TREE_VALUE (fn2),
1935 TREE_CHAIN (fn2) || more, str);
1937 else
1939 tree cand = OVL_CURRENT (fn);
1940 if (!*str)
1942 /* Pick the prefix string. */
1943 if (!more && !OVL_NEXT (fns))
1945 inform (DECL_SOURCE_LOCATION (cand),
1946 "candidate is: %#D", cand);
1947 continue;
1950 *str = _("candidates are:");
1951 spaces = get_spaces (*str);
1953 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1954 *str = spaces ? spaces : *str;
1957 if (!more)
1959 free (spaces);
1960 *str = NULL;
1964 /* Print the list of candidate FNS in an error message. FNS can also
1965 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1967 void
1968 print_candidates (tree fns)
1970 const char *str = NULL;
1971 print_candidates_1 (fns, false, &str);
1972 gcc_assert (str == NULL);
1975 /* Get a (possibly) constrained template declaration for the
1976 purpose of ordering candidates. */
1977 static tree
1978 get_template_for_ordering (tree list)
1980 gcc_assert (TREE_CODE (list) == TREE_LIST);
1981 tree f = TREE_VALUE (list);
1982 if (tree ti = DECL_TEMPLATE_INFO (f))
1983 return TI_TEMPLATE (ti);
1984 return f;
1987 /* Among candidates having the same signature, return the
1988 most constrained or NULL_TREE if there is no best candidate.
1989 If the signatures of candidates vary (e.g., template
1990 specialization vs. member function), then there can be no
1991 most constrained.
1993 Note that we don't compare constraints on the functions
1994 themselves, but rather those of their templates. */
1995 static tree
1996 most_constrained_function (tree candidates)
1998 // Try to find the best candidate in a first pass.
1999 tree champ = candidates;
2000 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2002 int winner = more_constrained (get_template_for_ordering (champ),
2003 get_template_for_ordering (c));
2004 if (winner == -1)
2005 champ = c; // The candidate is more constrained
2006 else if (winner == 0)
2007 return NULL_TREE; // Neither is more constrained
2010 // Verify that the champ is better than previous candidates.
2011 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2012 if (!more_constrained (get_template_for_ordering (champ),
2013 get_template_for_ordering (c)))
2014 return NULL_TREE;
2017 return champ;
2021 /* Returns the template (one of the functions given by TEMPLATE_ID)
2022 which can be specialized to match the indicated DECL with the
2023 explicit template args given in TEMPLATE_ID. The DECL may be
2024 NULL_TREE if none is available. In that case, the functions in
2025 TEMPLATE_ID are non-members.
2027 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2028 specialization of a member template.
2030 The TEMPLATE_COUNT is the number of references to qualifying
2031 template classes that appeared in the name of the function. See
2032 check_explicit_specialization for a more accurate description.
2034 TSK indicates what kind of template declaration (if any) is being
2035 declared. TSK_TEMPLATE indicates that the declaration given by
2036 DECL, though a FUNCTION_DECL, has template parameters, and is
2037 therefore a template function.
2039 The template args (those explicitly specified and those deduced)
2040 are output in a newly created vector *TARGS_OUT.
2042 If it is impossible to determine the result, an error message is
2043 issued. The error_mark_node is returned to indicate failure. */
2045 static tree
2046 determine_specialization (tree template_id,
2047 tree decl,
2048 tree* targs_out,
2049 int need_member_template,
2050 int template_count,
2051 tmpl_spec_kind tsk)
2053 tree fns;
2054 tree targs;
2055 tree explicit_targs;
2056 tree candidates = NULL_TREE;
2058 /* A TREE_LIST of templates of which DECL may be a specialization.
2059 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2060 corresponding TREE_PURPOSE is the set of template arguments that,
2061 when used to instantiate the template, would produce a function
2062 with the signature of DECL. */
2063 tree templates = NULL_TREE;
2064 int header_count;
2065 cp_binding_level *b;
2067 *targs_out = NULL_TREE;
2069 if (template_id == error_mark_node || decl == error_mark_node)
2070 return error_mark_node;
2072 /* We shouldn't be specializing a member template of an
2073 unspecialized class template; we already gave an error in
2074 check_specialization_scope, now avoid crashing. */
2075 if (template_count && DECL_CLASS_SCOPE_P (decl)
2076 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2078 gcc_assert (errorcount);
2079 return error_mark_node;
2082 fns = TREE_OPERAND (template_id, 0);
2083 explicit_targs = TREE_OPERAND (template_id, 1);
2085 if (fns == error_mark_node)
2086 return error_mark_node;
2088 /* Check for baselinks. */
2089 if (BASELINK_P (fns))
2090 fns = BASELINK_FUNCTIONS (fns);
2092 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2094 error ("%qD is not a function template", fns);
2095 return error_mark_node;
2097 else if (VAR_P (decl) && !variable_template_p (fns))
2099 error ("%qD is not a variable template", fns);
2100 return error_mark_node;
2103 /* Count the number of template headers specified for this
2104 specialization. */
2105 header_count = 0;
2106 for (b = current_binding_level;
2107 b->kind == sk_template_parms;
2108 b = b->level_chain)
2109 ++header_count;
2111 tree orig_fns = fns;
2113 if (variable_template_p (fns))
2115 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2116 targs = coerce_template_parms (parms, explicit_targs, fns,
2117 tf_warning_or_error,
2118 /*req_all*/true, /*use_defarg*/true);
2119 if (targs != error_mark_node)
2120 templates = tree_cons (targs, fns, templates);
2122 else for (; fns; fns = OVL_NEXT (fns))
2124 tree fn = OVL_CURRENT (fns);
2126 if (TREE_CODE (fn) == TEMPLATE_DECL)
2128 tree decl_arg_types;
2129 tree fn_arg_types;
2130 tree insttype;
2132 /* In case of explicit specialization, we need to check if
2133 the number of template headers appearing in the specialization
2134 is correct. This is usually done in check_explicit_specialization,
2135 but the check done there cannot be exhaustive when specializing
2136 member functions. Consider the following code:
2138 template <> void A<int>::f(int);
2139 template <> template <> void A<int>::f(int);
2141 Assuming that A<int> is not itself an explicit specialization
2142 already, the first line specializes "f" which is a non-template
2143 member function, whilst the second line specializes "f" which
2144 is a template member function. So both lines are syntactically
2145 correct, and check_explicit_specialization does not reject
2146 them.
2148 Here, we can do better, as we are matching the specialization
2149 against the declarations. We count the number of template
2150 headers, and we check if they match TEMPLATE_COUNT + 1
2151 (TEMPLATE_COUNT is the number of qualifying template classes,
2152 plus there must be another header for the member template
2153 itself).
2155 Notice that if header_count is zero, this is not a
2156 specialization but rather a template instantiation, so there
2157 is no check we can perform here. */
2158 if (header_count && header_count != template_count + 1)
2159 continue;
2161 /* Check that the number of template arguments at the
2162 innermost level for DECL is the same as for FN. */
2163 if (current_binding_level->kind == sk_template_parms
2164 && !current_binding_level->explicit_spec_p
2165 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2166 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2167 (current_template_parms))))
2168 continue;
2170 /* DECL might be a specialization of FN. */
2171 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2172 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2174 /* For a non-static member function, we need to make sure
2175 that the const qualification is the same. Since
2176 get_bindings does not try to merge the "this" parameter,
2177 we must do the comparison explicitly. */
2178 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2179 && !same_type_p (TREE_VALUE (fn_arg_types),
2180 TREE_VALUE (decl_arg_types)))
2181 continue;
2183 /* Skip the "this" parameter and, for constructors of
2184 classes with virtual bases, the VTT parameter. A
2185 full specialization of a constructor will have a VTT
2186 parameter, but a template never will. */
2187 decl_arg_types
2188 = skip_artificial_parms_for (decl, decl_arg_types);
2189 fn_arg_types
2190 = skip_artificial_parms_for (fn, fn_arg_types);
2192 /* Function templates cannot be specializations; there are
2193 no partial specializations of functions. Therefore, if
2194 the type of DECL does not match FN, there is no
2195 match.
2197 Note that it should never be the case that we have both
2198 candidates added here, and for regular member functions
2199 below. */
2200 if (tsk == tsk_template)
2202 if (compparms (fn_arg_types, decl_arg_types))
2203 candidates = tree_cons (NULL_TREE, fn, candidates);
2204 continue;
2207 /* See whether this function might be a specialization of this
2208 template. Suppress access control because we might be trying
2209 to make this specialization a friend, and we have already done
2210 access control for the declaration of the specialization. */
2211 push_deferring_access_checks (dk_no_check);
2212 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2213 pop_deferring_access_checks ();
2215 if (!targs)
2216 /* We cannot deduce template arguments that when used to
2217 specialize TMPL will produce DECL. */
2218 continue;
2220 /* Remove, from the set of candidates, all those functions
2221 whose constraints are not satisfied. */
2222 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2223 continue;
2225 // Then, try to form the new function type.
2226 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2227 if (insttype == error_mark_node)
2228 continue;
2229 fn_arg_types
2230 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2231 if (!compparms (fn_arg_types, decl_arg_types))
2232 continue;
2234 /* Save this template, and the arguments deduced. */
2235 templates = tree_cons (targs, fn, templates);
2237 else if (need_member_template)
2238 /* FN is an ordinary member function, and we need a
2239 specialization of a member template. */
2241 else if (TREE_CODE (fn) != FUNCTION_DECL)
2242 /* We can get IDENTIFIER_NODEs here in certain erroneous
2243 cases. */
2245 else if (!DECL_FUNCTION_MEMBER_P (fn))
2246 /* This is just an ordinary non-member function. Nothing can
2247 be a specialization of that. */
2249 else if (DECL_ARTIFICIAL (fn))
2250 /* Cannot specialize functions that are created implicitly. */
2252 else
2254 tree decl_arg_types;
2256 /* This is an ordinary member function. However, since
2257 we're here, we can assume its enclosing class is a
2258 template class. For example,
2260 template <typename T> struct S { void f(); };
2261 template <> void S<int>::f() {}
2263 Here, S<int>::f is a non-template, but S<int> is a
2264 template class. If FN has the same type as DECL, we
2265 might be in business. */
2267 if (!DECL_TEMPLATE_INFO (fn))
2268 /* Its enclosing class is an explicit specialization
2269 of a template class. This is not a candidate. */
2270 continue;
2272 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2273 TREE_TYPE (TREE_TYPE (fn))))
2274 /* The return types differ. */
2275 continue;
2277 /* Adjust the type of DECL in case FN is a static member. */
2278 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2279 if (DECL_STATIC_FUNCTION_P (fn)
2280 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2281 decl_arg_types = TREE_CHAIN (decl_arg_types);
2283 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2284 decl_arg_types))
2285 continue;
2287 // If the deduced arguments do not satisfy the constraints,
2288 // this is not a candidate.
2289 if (flag_concepts && !constraints_satisfied_p (fn))
2290 continue;
2292 // Add the candidate.
2293 candidates = tree_cons (NULL_TREE, fn, candidates);
2297 if (templates && TREE_CHAIN (templates))
2299 /* We have:
2301 [temp.expl.spec]
2303 It is possible for a specialization with a given function
2304 signature to be instantiated from more than one function
2305 template. In such cases, explicit specification of the
2306 template arguments must be used to uniquely identify the
2307 function template specialization being specialized.
2309 Note that here, there's no suggestion that we're supposed to
2310 determine which of the candidate templates is most
2311 specialized. However, we, also have:
2313 [temp.func.order]
2315 Partial ordering of overloaded function template
2316 declarations is used in the following contexts to select
2317 the function template to which a function template
2318 specialization refers:
2320 -- when an explicit specialization refers to a function
2321 template.
2323 So, we do use the partial ordering rules, at least for now.
2324 This extension can only serve to make invalid programs valid,
2325 so it's safe. And, there is strong anecdotal evidence that
2326 the committee intended the partial ordering rules to apply;
2327 the EDG front end has that behavior, and John Spicer claims
2328 that the committee simply forgot to delete the wording in
2329 [temp.expl.spec]. */
2330 tree tmpl = most_specialized_instantiation (templates);
2331 if (tmpl != error_mark_node)
2333 templates = tmpl;
2334 TREE_CHAIN (templates) = NULL_TREE;
2338 // Concepts allows multiple declarations of member functions
2339 // with the same signature. Like above, we need to rely on
2340 // on the partial ordering of those candidates to determine which
2341 // is the best.
2342 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2344 if (tree cand = most_constrained_function (candidates))
2346 candidates = cand;
2347 TREE_CHAIN (cand) = NULL_TREE;
2351 if (templates == NULL_TREE && candidates == NULL_TREE)
2353 error ("template-id %qD for %q+D does not match any template "
2354 "declaration", template_id, decl);
2355 if (header_count && header_count != template_count + 1)
2356 inform (input_location, "saw %d %<template<>%>, need %d for "
2357 "specializing a member function template",
2358 header_count, template_count + 1);
2359 else
2360 print_candidates (orig_fns);
2361 return error_mark_node;
2363 else if ((templates && TREE_CHAIN (templates))
2364 || (candidates && TREE_CHAIN (candidates))
2365 || (templates && candidates))
2367 error ("ambiguous template specialization %qD for %q+D",
2368 template_id, decl);
2369 candidates = chainon (candidates, templates);
2370 print_candidates (candidates);
2371 return error_mark_node;
2374 /* We have one, and exactly one, match. */
2375 if (candidates)
2377 tree fn = TREE_VALUE (candidates);
2378 *targs_out = copy_node (DECL_TI_ARGS (fn));
2380 // Propagate the candidate's constraints to the declaration.
2381 set_constraints (decl, get_constraints (fn));
2383 /* DECL is a re-declaration or partial instantiation of a template
2384 function. */
2385 if (TREE_CODE (fn) == TEMPLATE_DECL)
2386 return fn;
2387 /* It was a specialization of an ordinary member function in a
2388 template class. */
2389 return DECL_TI_TEMPLATE (fn);
2392 /* It was a specialization of a template. */
2393 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2394 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2396 *targs_out = copy_node (targs);
2397 SET_TMPL_ARGS_LEVEL (*targs_out,
2398 TMPL_ARGS_DEPTH (*targs_out),
2399 TREE_PURPOSE (templates));
2401 else
2402 *targs_out = TREE_PURPOSE (templates);
2403 return TREE_VALUE (templates);
2406 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2407 but with the default argument values filled in from those in the
2408 TMPL_TYPES. */
2410 static tree
2411 copy_default_args_to_explicit_spec_1 (tree spec_types,
2412 tree tmpl_types)
2414 tree new_spec_types;
2416 if (!spec_types)
2417 return NULL_TREE;
2419 if (spec_types == void_list_node)
2420 return void_list_node;
2422 /* Substitute into the rest of the list. */
2423 new_spec_types =
2424 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2425 TREE_CHAIN (tmpl_types));
2427 /* Add the default argument for this parameter. */
2428 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2429 TREE_VALUE (spec_types),
2430 new_spec_types);
2433 /* DECL is an explicit specialization. Replicate default arguments
2434 from the template it specializes. (That way, code like:
2436 template <class T> void f(T = 3);
2437 template <> void f(double);
2438 void g () { f (); }
2440 works, as required.) An alternative approach would be to look up
2441 the correct default arguments at the call-site, but this approach
2442 is consistent with how implicit instantiations are handled. */
2444 static void
2445 copy_default_args_to_explicit_spec (tree decl)
2447 tree tmpl;
2448 tree spec_types;
2449 tree tmpl_types;
2450 tree new_spec_types;
2451 tree old_type;
2452 tree new_type;
2453 tree t;
2454 tree object_type = NULL_TREE;
2455 tree in_charge = NULL_TREE;
2456 tree vtt = NULL_TREE;
2458 /* See if there's anything we need to do. */
2459 tmpl = DECL_TI_TEMPLATE (decl);
2460 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2461 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2462 if (TREE_PURPOSE (t))
2463 break;
2464 if (!t)
2465 return;
2467 old_type = TREE_TYPE (decl);
2468 spec_types = TYPE_ARG_TYPES (old_type);
2470 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2472 /* Remove the this pointer, but remember the object's type for
2473 CV quals. */
2474 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2475 spec_types = TREE_CHAIN (spec_types);
2476 tmpl_types = TREE_CHAIN (tmpl_types);
2478 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2480 /* DECL may contain more parameters than TMPL due to the extra
2481 in-charge parameter in constructors and destructors. */
2482 in_charge = spec_types;
2483 spec_types = TREE_CHAIN (spec_types);
2485 if (DECL_HAS_VTT_PARM_P (decl))
2487 vtt = spec_types;
2488 spec_types = TREE_CHAIN (spec_types);
2492 /* Compute the merged default arguments. */
2493 new_spec_types =
2494 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2496 /* Compute the new FUNCTION_TYPE. */
2497 if (object_type)
2499 if (vtt)
2500 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2501 TREE_VALUE (vtt),
2502 new_spec_types);
2504 if (in_charge)
2505 /* Put the in-charge parameter back. */
2506 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2507 TREE_VALUE (in_charge),
2508 new_spec_types);
2510 new_type = build_method_type_directly (object_type,
2511 TREE_TYPE (old_type),
2512 new_spec_types);
2514 else
2515 new_type = build_function_type (TREE_TYPE (old_type),
2516 new_spec_types);
2517 new_type = cp_build_type_attribute_variant (new_type,
2518 TYPE_ATTRIBUTES (old_type));
2519 new_type = build_exception_variant (new_type,
2520 TYPE_RAISES_EXCEPTIONS (old_type));
2522 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2523 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2525 TREE_TYPE (decl) = new_type;
2528 /* Return the number of template headers we expect to see for a definition
2529 or specialization of CTYPE or one of its non-template members. */
2532 num_template_headers_for_class (tree ctype)
2534 int num_templates = 0;
2536 while (ctype && CLASS_TYPE_P (ctype))
2538 /* You're supposed to have one `template <...>' for every
2539 template class, but you don't need one for a full
2540 specialization. For example:
2542 template <class T> struct S{};
2543 template <> struct S<int> { void f(); };
2544 void S<int>::f () {}
2546 is correct; there shouldn't be a `template <>' for the
2547 definition of `S<int>::f'. */
2548 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2549 /* If CTYPE does not have template information of any
2550 kind, then it is not a template, nor is it nested
2551 within a template. */
2552 break;
2553 if (explicit_class_specialization_p (ctype))
2554 break;
2555 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2556 ++num_templates;
2558 ctype = TYPE_CONTEXT (ctype);
2561 return num_templates;
2564 /* Do a simple sanity check on the template headers that precede the
2565 variable declaration DECL. */
2567 void
2568 check_template_variable (tree decl)
2570 tree ctx = CP_DECL_CONTEXT (decl);
2571 int wanted = num_template_headers_for_class (ctx);
2572 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2573 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2575 if (cxx_dialect < cxx14)
2576 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2577 "variable templates only available with "
2578 "-std=c++14 or -std=gnu++14");
2580 // Namespace-scope variable templates should have a template header.
2581 ++wanted;
2583 if (template_header_count > wanted)
2585 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2586 "too many template headers for %D (should be %d)",
2587 decl, wanted);
2588 if (warned && CLASS_TYPE_P (ctx)
2589 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2590 inform (DECL_SOURCE_LOCATION (decl),
2591 "members of an explicitly specialized class are defined "
2592 "without a template header");
2596 /* An explicit specialization whose declarator-id or class-head-name is not
2597 qualified shall be declared in the nearest enclosing namespace of the
2598 template, or, if the namespace is inline (7.3.1), any namespace from its
2599 enclosing namespace set.
2601 If the name declared in the explicit instantiation is an unqualified name,
2602 the explicit instantiation shall appear in the namespace where its template
2603 is declared or, if that namespace is inline (7.3.1), any namespace from its
2604 enclosing namespace set. */
2606 void
2607 check_unqualified_spec_or_inst (tree t, location_t loc)
2609 tree tmpl = most_general_template (t);
2610 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2611 && !is_associated_namespace (current_namespace,
2612 CP_DECL_CONTEXT (tmpl)))
2614 if (processing_specialization)
2615 permerror (loc, "explicit specialization of %qD outside its "
2616 "namespace must use a nested-name-specifier", tmpl);
2617 else if (processing_explicit_instantiation
2618 && cxx_dialect >= cxx11)
2619 /* This was allowed in C++98, so only pedwarn. */
2620 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2621 "outside its namespace must use a nested-name-"
2622 "specifier", tmpl);
2626 /* Check to see if the function just declared, as indicated in
2627 DECLARATOR, and in DECL, is a specialization of a function
2628 template. We may also discover that the declaration is an explicit
2629 instantiation at this point.
2631 Returns DECL, or an equivalent declaration that should be used
2632 instead if all goes well. Issues an error message if something is
2633 amiss. Returns error_mark_node if the error is not easily
2634 recoverable.
2636 FLAGS is a bitmask consisting of the following flags:
2638 2: The function has a definition.
2639 4: The function is a friend.
2641 The TEMPLATE_COUNT is the number of references to qualifying
2642 template classes that appeared in the name of the function. For
2643 example, in
2645 template <class T> struct S { void f(); };
2646 void S<int>::f();
2648 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2649 classes are not counted in the TEMPLATE_COUNT, so that in
2651 template <class T> struct S {};
2652 template <> struct S<int> { void f(); }
2653 template <> void S<int>::f();
2655 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2656 invalid; there should be no template <>.)
2658 If the function is a specialization, it is marked as such via
2659 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2660 is set up correctly, and it is added to the list of specializations
2661 for that template. */
2663 tree
2664 check_explicit_specialization (tree declarator,
2665 tree decl,
2666 int template_count,
2667 int flags)
2669 int have_def = flags & 2;
2670 int is_friend = flags & 4;
2671 bool is_concept = flags & 8;
2672 int specialization = 0;
2673 int explicit_instantiation = 0;
2674 int member_specialization = 0;
2675 tree ctype = DECL_CLASS_CONTEXT (decl);
2676 tree dname = DECL_NAME (decl);
2677 tmpl_spec_kind tsk;
2679 if (is_friend)
2681 if (!processing_specialization)
2682 tsk = tsk_none;
2683 else
2684 tsk = tsk_excessive_parms;
2686 else
2687 tsk = current_tmpl_spec_kind (template_count);
2689 switch (tsk)
2691 case tsk_none:
2692 if (processing_specialization && !VAR_P (decl))
2694 specialization = 1;
2695 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2697 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2699 if (is_friend)
2700 /* This could be something like:
2702 template <class T> void f(T);
2703 class S { friend void f<>(int); } */
2704 specialization = 1;
2705 else
2707 /* This case handles bogus declarations like template <>
2708 template <class T> void f<int>(); */
2710 error ("template-id %qD in declaration of primary template",
2711 declarator);
2712 return decl;
2715 break;
2717 case tsk_invalid_member_spec:
2718 /* The error has already been reported in
2719 check_specialization_scope. */
2720 return error_mark_node;
2722 case tsk_invalid_expl_inst:
2723 error ("template parameter list used in explicit instantiation");
2725 /* Fall through. */
2727 case tsk_expl_inst:
2728 if (have_def)
2729 error ("definition provided for explicit instantiation");
2731 explicit_instantiation = 1;
2732 break;
2734 case tsk_excessive_parms:
2735 case tsk_insufficient_parms:
2736 if (tsk == tsk_excessive_parms)
2737 error ("too many template parameter lists in declaration of %qD",
2738 decl);
2739 else if (template_header_count)
2740 error("too few template parameter lists in declaration of %qD", decl);
2741 else
2742 error("explicit specialization of %qD must be introduced by "
2743 "%<template <>%>", decl);
2745 /* Fall through. */
2746 case tsk_expl_spec:
2747 if (is_concept)
2748 error ("explicit specialization declared %<concept%>");
2750 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2751 /* In cases like template<> constexpr bool v = true;
2752 We'll give an error in check_template_variable. */
2753 break;
2755 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2756 if (ctype)
2757 member_specialization = 1;
2758 else
2759 specialization = 1;
2760 break;
2762 case tsk_template:
2763 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2765 /* This case handles bogus declarations like template <>
2766 template <class T> void f<int>(); */
2768 if (!uses_template_parms (declarator))
2769 error ("template-id %qD in declaration of primary template",
2770 declarator);
2771 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2773 /* Partial specialization of variable template. */
2774 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2775 specialization = 1;
2776 goto ok;
2778 else if (cxx_dialect < cxx14)
2779 error ("non-type partial specialization %qD "
2780 "is not allowed", declarator);
2781 else
2782 error ("non-class, non-variable partial specialization %qD "
2783 "is not allowed", declarator);
2784 return decl;
2785 ok:;
2788 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2789 /* This is a specialization of a member template, without
2790 specialization the containing class. Something like:
2792 template <class T> struct S {
2793 template <class U> void f (U);
2795 template <> template <class U> void S<int>::f(U) {}
2797 That's a specialization -- but of the entire template. */
2798 specialization = 1;
2799 break;
2801 default:
2802 gcc_unreachable ();
2805 if ((specialization || member_specialization)
2806 /* This doesn't apply to variable templates. */
2807 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2808 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2810 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2811 for (; t; t = TREE_CHAIN (t))
2812 if (TREE_PURPOSE (t))
2814 permerror (input_location,
2815 "default argument specified in explicit specialization");
2816 break;
2820 if (specialization || member_specialization || explicit_instantiation)
2822 tree tmpl = NULL_TREE;
2823 tree targs = NULL_TREE;
2824 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2826 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2827 if (!was_template_id)
2829 tree fns;
2831 gcc_assert (identifier_p (declarator));
2832 if (ctype)
2833 fns = dname;
2834 else
2836 /* If there is no class context, the explicit instantiation
2837 must be at namespace scope. */
2838 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2840 /* Find the namespace binding, using the declaration
2841 context. */
2842 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2843 false, true);
2844 if (fns == error_mark_node)
2845 /* If lookup fails, look for a friend declaration so we can
2846 give a better diagnostic. */
2847 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2848 /*type*/false, /*complain*/true,
2849 /*hidden*/true);
2851 if (fns == error_mark_node || !is_overloaded_fn (fns))
2853 error ("%qD is not a template function", dname);
2854 fns = error_mark_node;
2858 declarator = lookup_template_function (fns, NULL_TREE);
2861 if (declarator == error_mark_node)
2862 return error_mark_node;
2864 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2866 if (!explicit_instantiation)
2867 /* A specialization in class scope. This is invalid,
2868 but the error will already have been flagged by
2869 check_specialization_scope. */
2870 return error_mark_node;
2871 else
2873 /* It's not valid to write an explicit instantiation in
2874 class scope, e.g.:
2876 class C { template void f(); }
2878 This case is caught by the parser. However, on
2879 something like:
2881 template class C { void f(); };
2883 (which is invalid) we can get here. The error will be
2884 issued later. */
2888 return decl;
2890 else if (ctype != NULL_TREE
2891 && (identifier_p (TREE_OPERAND (declarator, 0))))
2893 // We'll match variable templates in start_decl.
2894 if (VAR_P (decl))
2895 return decl;
2897 /* Find the list of functions in ctype that have the same
2898 name as the declared function. */
2899 tree name = TREE_OPERAND (declarator, 0);
2900 tree fns = NULL_TREE;
2901 int idx;
2903 if (constructor_name_p (name, ctype))
2905 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2907 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2908 : !CLASSTYPE_DESTRUCTORS (ctype))
2910 /* From [temp.expl.spec]:
2912 If such an explicit specialization for the member
2913 of a class template names an implicitly-declared
2914 special member function (clause _special_), the
2915 program is ill-formed.
2917 Similar language is found in [temp.explicit]. */
2918 error ("specialization of implicitly-declared special member function");
2919 return error_mark_node;
2922 name = is_constructor ? ctor_identifier : dtor_identifier;
2925 if (!DECL_CONV_FN_P (decl))
2927 idx = lookup_fnfields_1 (ctype, name);
2928 if (idx >= 0)
2929 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2931 else
2933 vec<tree, va_gc> *methods;
2934 tree ovl;
2936 /* For a type-conversion operator, we cannot do a
2937 name-based lookup. We might be looking for `operator
2938 int' which will be a specialization of `operator T'.
2939 So, we find *all* the conversion operators, and then
2940 select from them. */
2941 fns = NULL_TREE;
2943 methods = CLASSTYPE_METHOD_VEC (ctype);
2944 if (methods)
2945 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2946 methods->iterate (idx, &ovl);
2947 ++idx)
2949 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2950 /* There are no more conversion functions. */
2951 break;
2953 /* Glue all these conversion functions together
2954 with those we already have. */
2955 for (; ovl; ovl = OVL_NEXT (ovl))
2956 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2960 if (fns == NULL_TREE)
2962 error ("no member function %qD declared in %qT", name, ctype);
2963 return error_mark_node;
2965 else
2966 TREE_OPERAND (declarator, 0) = fns;
2969 /* Figure out what exactly is being specialized at this point.
2970 Note that for an explicit instantiation, even one for a
2971 member function, we cannot tell apriori whether the
2972 instantiation is for a member template, or just a member
2973 function of a template class. Even if a member template is
2974 being instantiated, the member template arguments may be
2975 elided if they can be deduced from the rest of the
2976 declaration. */
2977 tmpl = determine_specialization (declarator, decl,
2978 &targs,
2979 member_specialization,
2980 template_count,
2981 tsk);
2983 if (!tmpl || tmpl == error_mark_node)
2984 /* We couldn't figure out what this declaration was
2985 specializing. */
2986 return error_mark_node;
2987 else
2989 if (TREE_CODE (decl) == FUNCTION_DECL
2990 && DECL_HIDDEN_FRIEND_P (tmpl))
2992 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2993 "friend declaration %qD is not visible to "
2994 "explicit specialization", tmpl))
2995 inform (DECL_SOURCE_LOCATION (tmpl),
2996 "friend declaration here");
2998 else if (!ctype && !is_friend
2999 && CP_DECL_CONTEXT (decl) == current_namespace)
3000 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3002 tree gen_tmpl = most_general_template (tmpl);
3004 if (explicit_instantiation)
3006 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3007 is done by do_decl_instantiation later. */
3009 int arg_depth = TMPL_ARGS_DEPTH (targs);
3010 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3012 if (arg_depth > parm_depth)
3014 /* If TMPL is not the most general template (for
3015 example, if TMPL is a friend template that is
3016 injected into namespace scope), then there will
3017 be too many levels of TARGS. Remove some of them
3018 here. */
3019 int i;
3020 tree new_targs;
3022 new_targs = make_tree_vec (parm_depth);
3023 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3024 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3025 = TREE_VEC_ELT (targs, i);
3026 targs = new_targs;
3029 return instantiate_template (tmpl, targs, tf_error);
3032 /* If we thought that the DECL was a member function, but it
3033 turns out to be specializing a static member function,
3034 make DECL a static member function as well. */
3035 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3036 && DECL_STATIC_FUNCTION_P (tmpl)
3037 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3038 revert_static_member_fn (decl);
3040 /* If this is a specialization of a member template of a
3041 template class, we want to return the TEMPLATE_DECL, not
3042 the specialization of it. */
3043 if (tsk == tsk_template && !was_template_id)
3045 tree result = DECL_TEMPLATE_RESULT (tmpl);
3046 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3047 DECL_INITIAL (result) = NULL_TREE;
3048 if (have_def)
3050 tree parm;
3051 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3052 DECL_SOURCE_LOCATION (result)
3053 = DECL_SOURCE_LOCATION (decl);
3054 /* We want to use the argument list specified in the
3055 definition, not in the original declaration. */
3056 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3057 for (parm = DECL_ARGUMENTS (result); parm;
3058 parm = DECL_CHAIN (parm))
3059 DECL_CONTEXT (parm) = result;
3061 return register_specialization (tmpl, gen_tmpl, targs,
3062 is_friend, 0);
3065 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3066 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3068 if (was_template_id)
3069 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3071 /* Inherit default function arguments from the template
3072 DECL is specializing. */
3073 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3074 copy_default_args_to_explicit_spec (decl);
3076 /* This specialization has the same protection as the
3077 template it specializes. */
3078 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3079 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3081 /* 7.1.1-1 [dcl.stc]
3083 A storage-class-specifier shall not be specified in an
3084 explicit specialization...
3086 The parser rejects these, so unless action is taken here,
3087 explicit function specializations will always appear with
3088 global linkage.
3090 The action recommended by the C++ CWG in response to C++
3091 defect report 605 is to make the storage class and linkage
3092 of the explicit specialization match the templated function:
3094 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3096 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3098 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3099 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3101 /* A concept cannot be specialized. */
3102 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3104 error ("explicit specialization of function concept %qD",
3105 gen_tmpl);
3106 return error_mark_node;
3109 /* This specialization has the same linkage and visibility as
3110 the function template it specializes. */
3111 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3112 if (! TREE_PUBLIC (decl))
3114 DECL_INTERFACE_KNOWN (decl) = 1;
3115 DECL_NOT_REALLY_EXTERN (decl) = 1;
3117 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3118 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3120 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3121 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3125 /* If DECL is a friend declaration, declared using an
3126 unqualified name, the namespace associated with DECL may
3127 have been set incorrectly. For example, in:
3129 template <typename T> void f(T);
3130 namespace N {
3131 struct S { friend void f<int>(int); }
3134 we will have set the DECL_CONTEXT for the friend
3135 declaration to N, rather than to the global namespace. */
3136 if (DECL_NAMESPACE_SCOPE_P (decl))
3137 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3139 if (is_friend && !have_def)
3140 /* This is not really a declaration of a specialization.
3141 It's just the name of an instantiation. But, it's not
3142 a request for an instantiation, either. */
3143 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3144 else if (TREE_CODE (decl) == FUNCTION_DECL)
3145 /* A specialization is not necessarily COMDAT. */
3146 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3147 && DECL_DECLARED_INLINE_P (decl));
3148 else if (VAR_P (decl))
3149 DECL_COMDAT (decl) = false;
3151 /* If this is a full specialization, register it so that we can find
3152 it again. Partial specializations will be registered in
3153 process_partial_specialization. */
3154 if (!processing_template_decl)
3155 decl = register_specialization (decl, gen_tmpl, targs,
3156 is_friend, 0);
3158 /* A 'structor should already have clones. */
3159 gcc_assert (decl == error_mark_node
3160 || variable_template_p (tmpl)
3161 || !(DECL_CONSTRUCTOR_P (decl)
3162 || DECL_DESTRUCTOR_P (decl))
3163 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3167 return decl;
3170 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3171 parameters. These are represented in the same format used for
3172 DECL_TEMPLATE_PARMS. */
3175 comp_template_parms (const_tree parms1, const_tree parms2)
3177 const_tree p1;
3178 const_tree p2;
3180 if (parms1 == parms2)
3181 return 1;
3183 for (p1 = parms1, p2 = parms2;
3184 p1 != NULL_TREE && p2 != NULL_TREE;
3185 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3187 tree t1 = TREE_VALUE (p1);
3188 tree t2 = TREE_VALUE (p2);
3189 int i;
3191 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3192 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3194 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3195 return 0;
3197 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3199 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3200 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3202 /* If either of the template parameters are invalid, assume
3203 they match for the sake of error recovery. */
3204 if (error_operand_p (parm1) || error_operand_p (parm2))
3205 return 1;
3207 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3208 return 0;
3210 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3211 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3212 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3213 continue;
3214 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3215 return 0;
3219 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3220 /* One set of parameters has more parameters lists than the
3221 other. */
3222 return 0;
3224 return 1;
3227 /* Determine whether PARM is a parameter pack. */
3229 bool
3230 template_parameter_pack_p (const_tree parm)
3232 /* Determine if we have a non-type template parameter pack. */
3233 if (TREE_CODE (parm) == PARM_DECL)
3234 return (DECL_TEMPLATE_PARM_P (parm)
3235 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3236 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3237 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3239 /* If this is a list of template parameters, we could get a
3240 TYPE_DECL or a TEMPLATE_DECL. */
3241 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3242 parm = TREE_TYPE (parm);
3244 /* Otherwise it must be a type template parameter. */
3245 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3246 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3247 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3250 /* Determine if T is a function parameter pack. */
3252 bool
3253 function_parameter_pack_p (const_tree t)
3255 if (t && TREE_CODE (t) == PARM_DECL)
3256 return DECL_PACK_P (t);
3257 return false;
3260 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3261 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3263 tree
3264 get_function_template_decl (const_tree primary_func_tmpl_inst)
3266 if (! primary_func_tmpl_inst
3267 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3268 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3269 return NULL;
3271 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3274 /* Return true iff the function parameter PARAM_DECL was expanded
3275 from the function parameter pack PACK. */
3277 bool
3278 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3280 if (DECL_ARTIFICIAL (param_decl)
3281 || !function_parameter_pack_p (pack))
3282 return false;
3284 /* The parameter pack and its pack arguments have the same
3285 DECL_PARM_INDEX. */
3286 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3289 /* Determine whether ARGS describes a variadic template args list,
3290 i.e., one that is terminated by a template argument pack. */
3292 static bool
3293 template_args_variadic_p (tree args)
3295 int nargs;
3296 tree last_parm;
3298 if (args == NULL_TREE)
3299 return false;
3301 args = INNERMOST_TEMPLATE_ARGS (args);
3302 nargs = TREE_VEC_LENGTH (args);
3304 if (nargs == 0)
3305 return false;
3307 last_parm = TREE_VEC_ELT (args, nargs - 1);
3309 return ARGUMENT_PACK_P (last_parm);
3312 /* Generate a new name for the parameter pack name NAME (an
3313 IDENTIFIER_NODE) that incorporates its */
3315 static tree
3316 make_ith_pack_parameter_name (tree name, int i)
3318 /* Munge the name to include the parameter index. */
3319 #define NUMBUF_LEN 128
3320 char numbuf[NUMBUF_LEN];
3321 char* newname;
3322 int newname_len;
3324 if (name == NULL_TREE)
3325 return name;
3326 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3327 newname_len = IDENTIFIER_LENGTH (name)
3328 + strlen (numbuf) + 2;
3329 newname = (char*)alloca (newname_len);
3330 snprintf (newname, newname_len,
3331 "%s#%i", IDENTIFIER_POINTER (name), i);
3332 return get_identifier (newname);
3335 /* Return true if T is a primary function, class or alias template
3336 instantiation. */
3338 bool
3339 primary_template_instantiation_p (const_tree t)
3341 if (!t)
3342 return false;
3344 if (TREE_CODE (t) == FUNCTION_DECL)
3345 return DECL_LANG_SPECIFIC (t)
3346 && DECL_TEMPLATE_INSTANTIATION (t)
3347 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3348 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3349 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3350 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3351 else if (alias_template_specialization_p (t))
3352 return true;
3353 return false;
3356 /* Return true if PARM is a template template parameter. */
3358 bool
3359 template_template_parameter_p (const_tree parm)
3361 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3364 /* Return true iff PARM is a DECL representing a type template
3365 parameter. */
3367 bool
3368 template_type_parameter_p (const_tree parm)
3370 return (parm
3371 && (TREE_CODE (parm) == TYPE_DECL
3372 || TREE_CODE (parm) == TEMPLATE_DECL)
3373 && DECL_TEMPLATE_PARM_P (parm));
3376 /* Return the template parameters of T if T is a
3377 primary template instantiation, NULL otherwise. */
3379 tree
3380 get_primary_template_innermost_parameters (const_tree t)
3382 tree parms = NULL, template_info = NULL;
3384 if ((template_info = get_template_info (t))
3385 && primary_template_instantiation_p (t))
3386 parms = INNERMOST_TEMPLATE_PARMS
3387 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3389 return parms;
3392 /* Return the template parameters of the LEVELth level from the full list
3393 of template parameters PARMS. */
3395 tree
3396 get_template_parms_at_level (tree parms, int level)
3398 tree p;
3399 if (!parms
3400 || TREE_CODE (parms) != TREE_LIST
3401 || level > TMPL_PARMS_DEPTH (parms))
3402 return NULL_TREE;
3404 for (p = parms; p; p = TREE_CHAIN (p))
3405 if (TMPL_PARMS_DEPTH (p) == level)
3406 return p;
3408 return NULL_TREE;
3411 /* Returns the template arguments of T if T is a template instantiation,
3412 NULL otherwise. */
3414 tree
3415 get_template_innermost_arguments (const_tree t)
3417 tree args = NULL, template_info = NULL;
3419 if ((template_info = get_template_info (t))
3420 && TI_ARGS (template_info))
3421 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3423 return args;
3426 /* Return the argument pack elements of T if T is a template argument pack,
3427 NULL otherwise. */
3429 tree
3430 get_template_argument_pack_elems (const_tree t)
3432 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3433 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3434 return NULL;
3436 return ARGUMENT_PACK_ARGS (t);
3439 /* Structure used to track the progress of find_parameter_packs_r. */
3440 struct find_parameter_pack_data
3442 /* TREE_LIST that will contain all of the parameter packs found by
3443 the traversal. */
3444 tree* parameter_packs;
3446 /* Set of AST nodes that have been visited by the traversal. */
3447 hash_set<tree> *visited;
3449 /* True iff we're making a type pack expansion. */
3450 bool type_pack_expansion_p;
3453 /* Identifies all of the argument packs that occur in a template
3454 argument and appends them to the TREE_LIST inside DATA, which is a
3455 find_parameter_pack_data structure. This is a subroutine of
3456 make_pack_expansion and uses_parameter_packs. */
3457 static tree
3458 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3460 tree t = *tp;
3461 struct find_parameter_pack_data* ppd =
3462 (struct find_parameter_pack_data*)data;
3463 bool parameter_pack_p = false;
3465 /* Handle type aliases/typedefs. */
3466 if (TYPE_ALIAS_P (t))
3468 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3469 cp_walk_tree (&TI_ARGS (tinfo),
3470 &find_parameter_packs_r,
3471 ppd, ppd->visited);
3472 *walk_subtrees = 0;
3473 return NULL_TREE;
3476 /* Identify whether this is a parameter pack or not. */
3477 switch (TREE_CODE (t))
3479 case TEMPLATE_PARM_INDEX:
3480 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3481 parameter_pack_p = true;
3482 break;
3484 case TEMPLATE_TYPE_PARM:
3485 t = TYPE_MAIN_VARIANT (t);
3486 /* FALLTHRU */
3487 case TEMPLATE_TEMPLATE_PARM:
3488 /* If the placeholder appears in the decl-specifier-seq of a function
3489 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3490 is a pack expansion, the invented template parameter is a template
3491 parameter pack. */
3492 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3493 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3494 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3495 parameter_pack_p = true;
3496 break;
3498 case FIELD_DECL:
3499 case PARM_DECL:
3500 if (DECL_PACK_P (t))
3502 /* We don't want to walk into the type of a PARM_DECL,
3503 because we don't want to see the type parameter pack. */
3504 *walk_subtrees = 0;
3505 parameter_pack_p = true;
3507 break;
3509 /* Look through a lambda capture proxy to the field pack. */
3510 case VAR_DECL:
3511 if (DECL_HAS_VALUE_EXPR_P (t))
3513 tree v = DECL_VALUE_EXPR (t);
3514 cp_walk_tree (&v,
3515 &find_parameter_packs_r,
3516 ppd, ppd->visited);
3517 *walk_subtrees = 0;
3519 else if (variable_template_specialization_p (t))
3521 cp_walk_tree (&DECL_TI_ARGS (t),
3522 find_parameter_packs_r,
3523 ppd, ppd->visited);
3524 *walk_subtrees = 0;
3526 break;
3528 case BASES:
3529 parameter_pack_p = true;
3530 break;
3531 default:
3532 /* Not a parameter pack. */
3533 break;
3536 if (parameter_pack_p)
3538 /* Add this parameter pack to the list. */
3539 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3542 if (TYPE_P (t))
3543 cp_walk_tree (&TYPE_CONTEXT (t),
3544 &find_parameter_packs_r, ppd, ppd->visited);
3546 /* This switch statement will return immediately if we don't find a
3547 parameter pack. */
3548 switch (TREE_CODE (t))
3550 case TEMPLATE_PARM_INDEX:
3551 return NULL_TREE;
3553 case BOUND_TEMPLATE_TEMPLATE_PARM:
3554 /* Check the template itself. */
3555 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3556 &find_parameter_packs_r, ppd, ppd->visited);
3557 /* Check the template arguments. */
3558 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3559 ppd->visited);
3560 *walk_subtrees = 0;
3561 return NULL_TREE;
3563 case TEMPLATE_TYPE_PARM:
3564 case TEMPLATE_TEMPLATE_PARM:
3565 return NULL_TREE;
3567 case PARM_DECL:
3568 return NULL_TREE;
3570 case RECORD_TYPE:
3571 if (TYPE_PTRMEMFUNC_P (t))
3572 return NULL_TREE;
3573 /* Fall through. */
3575 case UNION_TYPE:
3576 case ENUMERAL_TYPE:
3577 if (TYPE_TEMPLATE_INFO (t))
3578 cp_walk_tree (&TYPE_TI_ARGS (t),
3579 &find_parameter_packs_r, ppd, ppd->visited);
3581 *walk_subtrees = 0;
3582 return NULL_TREE;
3584 case TEMPLATE_DECL:
3585 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3586 return NULL_TREE;
3587 gcc_fallthrough();
3589 case CONSTRUCTOR:
3590 cp_walk_tree (&TREE_TYPE (t),
3591 &find_parameter_packs_r, ppd, ppd->visited);
3592 return NULL_TREE;
3594 case TYPENAME_TYPE:
3595 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3596 ppd, ppd->visited);
3597 *walk_subtrees = 0;
3598 return NULL_TREE;
3600 case TYPE_PACK_EXPANSION:
3601 case EXPR_PACK_EXPANSION:
3602 *walk_subtrees = 0;
3603 return NULL_TREE;
3605 case INTEGER_TYPE:
3606 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3607 ppd, ppd->visited);
3608 *walk_subtrees = 0;
3609 return NULL_TREE;
3611 case IDENTIFIER_NODE:
3612 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3613 ppd->visited);
3614 *walk_subtrees = 0;
3615 return NULL_TREE;
3617 case DECLTYPE_TYPE:
3619 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3620 type_pack_expansion_p to false so that any placeholders
3621 within the expression don't get marked as parameter packs. */
3622 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3623 ppd->type_pack_expansion_p = false;
3624 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3625 ppd, ppd->visited);
3626 ppd->type_pack_expansion_p = type_pack_expansion_p;
3627 *walk_subtrees = 0;
3628 return NULL_TREE;
3631 default:
3632 return NULL_TREE;
3635 return NULL_TREE;
3638 /* Determines if the expression or type T uses any parameter packs. */
3639 bool
3640 uses_parameter_packs (tree t)
3642 tree parameter_packs = NULL_TREE;
3643 struct find_parameter_pack_data ppd;
3644 ppd.parameter_packs = &parameter_packs;
3645 ppd.visited = new hash_set<tree>;
3646 ppd.type_pack_expansion_p = false;
3647 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3648 delete ppd.visited;
3649 return parameter_packs != NULL_TREE;
3652 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3653 representation a base-class initializer into a parameter pack
3654 expansion. If all goes well, the resulting node will be an
3655 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3656 respectively. */
3657 tree
3658 make_pack_expansion (tree arg)
3660 tree result;
3661 tree parameter_packs = NULL_TREE;
3662 bool for_types = false;
3663 struct find_parameter_pack_data ppd;
3665 if (!arg || arg == error_mark_node)
3666 return arg;
3668 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3670 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3671 class initializer. In this case, the TREE_PURPOSE will be a
3672 _TYPE node (representing the base class expansion we're
3673 initializing) and the TREE_VALUE will be a TREE_LIST
3674 containing the initialization arguments.
3676 The resulting expansion looks somewhat different from most
3677 expansions. Rather than returning just one _EXPANSION, we
3678 return a TREE_LIST whose TREE_PURPOSE is a
3679 TYPE_PACK_EXPANSION containing the bases that will be
3680 initialized. The TREE_VALUE will be identical to the
3681 original TREE_VALUE, which is a list of arguments that will
3682 be passed to each base. We do not introduce any new pack
3683 expansion nodes into the TREE_VALUE (although it is possible
3684 that some already exist), because the TREE_PURPOSE and
3685 TREE_VALUE all need to be expanded together with the same
3686 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3687 resulting TREE_PURPOSE will mention the parameter packs in
3688 both the bases and the arguments to the bases. */
3689 tree purpose;
3690 tree value;
3691 tree parameter_packs = NULL_TREE;
3693 /* Determine which parameter packs will be used by the base
3694 class expansion. */
3695 ppd.visited = new hash_set<tree>;
3696 ppd.parameter_packs = &parameter_packs;
3697 ppd.type_pack_expansion_p = true;
3698 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3699 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3700 &ppd, ppd.visited);
3702 if (parameter_packs == NULL_TREE)
3704 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3705 delete ppd.visited;
3706 return error_mark_node;
3709 if (TREE_VALUE (arg) != void_type_node)
3711 /* Collect the sets of parameter packs used in each of the
3712 initialization arguments. */
3713 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3715 /* Determine which parameter packs will be expanded in this
3716 argument. */
3717 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3718 &ppd, ppd.visited);
3722 delete ppd.visited;
3724 /* Create the pack expansion type for the base type. */
3725 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3726 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3727 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3729 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3730 they will rarely be compared to anything. */
3731 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3733 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3736 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3737 for_types = true;
3739 /* Build the PACK_EXPANSION_* node. */
3740 result = for_types
3741 ? cxx_make_type (TYPE_PACK_EXPANSION)
3742 : make_node (EXPR_PACK_EXPANSION);
3743 SET_PACK_EXPANSION_PATTERN (result, arg);
3744 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3746 /* Propagate type and const-expression information. */
3747 TREE_TYPE (result) = TREE_TYPE (arg);
3748 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3749 /* Mark this read now, since the expansion might be length 0. */
3750 mark_exp_read (arg);
3752 else
3753 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3754 they will rarely be compared to anything. */
3755 SET_TYPE_STRUCTURAL_EQUALITY (result);
3757 /* Determine which parameter packs will be expanded. */
3758 ppd.parameter_packs = &parameter_packs;
3759 ppd.visited = new hash_set<tree>;
3760 ppd.type_pack_expansion_p = TYPE_P (arg);
3761 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3762 delete ppd.visited;
3764 /* Make sure we found some parameter packs. */
3765 if (parameter_packs == NULL_TREE)
3767 if (TYPE_P (arg))
3768 error ("expansion pattern %<%T%> contains no argument packs", arg);
3769 else
3770 error ("expansion pattern %<%E%> contains no argument packs", arg);
3771 return error_mark_node;
3773 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3775 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3777 return result;
3780 /* Checks T for any "bare" parameter packs, which have not yet been
3781 expanded, and issues an error if any are found. This operation can
3782 only be done on full expressions or types (e.g., an expression
3783 statement, "if" condition, etc.), because we could have expressions like:
3785 foo(f(g(h(args)))...)
3787 where "args" is a parameter pack. check_for_bare_parameter_packs
3788 should not be called for the subexpressions args, h(args),
3789 g(h(args)), or f(g(h(args))), because we would produce erroneous
3790 error messages.
3792 Returns TRUE and emits an error if there were bare parameter packs,
3793 returns FALSE otherwise. */
3794 bool
3795 check_for_bare_parameter_packs (tree t)
3797 tree parameter_packs = NULL_TREE;
3798 struct find_parameter_pack_data ppd;
3800 if (!processing_template_decl || !t || t == error_mark_node)
3801 return false;
3803 if (TREE_CODE (t) == TYPE_DECL)
3804 t = TREE_TYPE (t);
3806 ppd.parameter_packs = &parameter_packs;
3807 ppd.visited = new hash_set<tree>;
3808 ppd.type_pack_expansion_p = false;
3809 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3810 delete ppd.visited;
3812 if (parameter_packs)
3814 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3815 error_at (loc, "parameter packs not expanded with %<...%>:");
3816 while (parameter_packs)
3818 tree pack = TREE_VALUE (parameter_packs);
3819 tree name = NULL_TREE;
3821 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3822 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3823 name = TYPE_NAME (pack);
3824 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3825 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3826 else
3827 name = DECL_NAME (pack);
3829 if (name)
3830 inform (loc, " %qD", name);
3831 else
3832 inform (loc, " <anonymous>");
3834 parameter_packs = TREE_CHAIN (parameter_packs);
3837 return true;
3840 return false;
3843 /* Expand any parameter packs that occur in the template arguments in
3844 ARGS. */
3845 tree
3846 expand_template_argument_pack (tree args)
3848 if (args == error_mark_node)
3849 return error_mark_node;
3851 tree result_args = NULL_TREE;
3852 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3853 int num_result_args = -1;
3854 int non_default_args_count = -1;
3856 /* First, determine if we need to expand anything, and the number of
3857 slots we'll need. */
3858 for (in_arg = 0; in_arg < nargs; ++in_arg)
3860 tree arg = TREE_VEC_ELT (args, in_arg);
3861 if (arg == NULL_TREE)
3862 return args;
3863 if (ARGUMENT_PACK_P (arg))
3865 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3866 if (num_result_args < 0)
3867 num_result_args = in_arg + num_packed;
3868 else
3869 num_result_args += num_packed;
3871 else
3873 if (num_result_args >= 0)
3874 num_result_args++;
3878 /* If no expansion is necessary, we're done. */
3879 if (num_result_args < 0)
3880 return args;
3882 /* Expand arguments. */
3883 result_args = make_tree_vec (num_result_args);
3884 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3885 non_default_args_count =
3886 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3887 for (in_arg = 0; in_arg < nargs; ++in_arg)
3889 tree arg = TREE_VEC_ELT (args, in_arg);
3890 if (ARGUMENT_PACK_P (arg))
3892 tree packed = ARGUMENT_PACK_ARGS (arg);
3893 int i, num_packed = TREE_VEC_LENGTH (packed);
3894 for (i = 0; i < num_packed; ++i, ++out_arg)
3895 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3896 if (non_default_args_count > 0)
3897 non_default_args_count += num_packed - 1;
3899 else
3901 TREE_VEC_ELT (result_args, out_arg) = arg;
3902 ++out_arg;
3905 if (non_default_args_count >= 0)
3906 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3907 return result_args;
3910 /* Checks if DECL shadows a template parameter.
3912 [temp.local]: A template-parameter shall not be redeclared within its
3913 scope (including nested scopes).
3915 Emits an error and returns TRUE if the DECL shadows a parameter,
3916 returns FALSE otherwise. */
3918 bool
3919 check_template_shadow (tree decl)
3921 tree olddecl;
3923 /* If we're not in a template, we can't possibly shadow a template
3924 parameter. */
3925 if (!current_template_parms)
3926 return true;
3928 /* Figure out what we're shadowing. */
3929 if (TREE_CODE (decl) == OVERLOAD)
3930 decl = OVL_CURRENT (decl);
3931 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3933 /* If there's no previous binding for this name, we're not shadowing
3934 anything, let alone a template parameter. */
3935 if (!olddecl)
3936 return true;
3938 /* If we're not shadowing a template parameter, we're done. Note
3939 that OLDDECL might be an OVERLOAD (or perhaps even an
3940 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3941 node. */
3942 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3943 return true;
3945 /* We check for decl != olddecl to avoid bogus errors for using a
3946 name inside a class. We check TPFI to avoid duplicate errors for
3947 inline member templates. */
3948 if (decl == olddecl
3949 || (DECL_TEMPLATE_PARM_P (decl)
3950 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3951 return true;
3953 /* Don't complain about the injected class name, as we've already
3954 complained about the class itself. */
3955 if (DECL_SELF_REFERENCE_P (decl))
3956 return false;
3958 if (DECL_TEMPLATE_PARM_P (decl))
3959 error ("declaration of template parameter %q+D shadows "
3960 "template parameter", decl);
3961 else
3962 error ("declaration of %q+#D shadows template parameter", decl);
3963 inform (DECL_SOURCE_LOCATION (olddecl),
3964 "template parameter %qD declared here", olddecl);
3965 return false;
3968 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3969 ORIG_LEVEL, DECL, and TYPE. */
3971 static tree
3972 build_template_parm_index (int index,
3973 int level,
3974 int orig_level,
3975 tree decl,
3976 tree type)
3978 tree t = make_node (TEMPLATE_PARM_INDEX);
3979 TEMPLATE_PARM_IDX (t) = index;
3980 TEMPLATE_PARM_LEVEL (t) = level;
3981 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3982 TEMPLATE_PARM_DECL (t) = decl;
3983 TREE_TYPE (t) = type;
3984 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3985 TREE_READONLY (t) = TREE_READONLY (decl);
3987 return t;
3990 /* Find the canonical type parameter for the given template type
3991 parameter. Returns the canonical type parameter, which may be TYPE
3992 if no such parameter existed. */
3994 static tree
3995 canonical_type_parameter (tree type)
3997 tree list;
3998 int idx = TEMPLATE_TYPE_IDX (type);
3999 if (!canonical_template_parms)
4000 vec_alloc (canonical_template_parms, idx+1);
4002 while (canonical_template_parms->length () <= (unsigned)idx)
4003 vec_safe_push (canonical_template_parms, NULL_TREE);
4005 list = (*canonical_template_parms)[idx];
4006 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4007 list = TREE_CHAIN (list);
4009 if (list)
4010 return TREE_VALUE (list);
4011 else
4013 (*canonical_template_parms)[idx]
4014 = tree_cons (NULL_TREE, type,
4015 (*canonical_template_parms)[idx]);
4016 return type;
4020 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4021 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4022 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4023 new one is created. */
4025 static tree
4026 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4027 tsubst_flags_t complain)
4029 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4030 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4031 != TEMPLATE_PARM_LEVEL (index) - levels)
4032 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4034 tree orig_decl = TEMPLATE_PARM_DECL (index);
4035 tree decl, t;
4037 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4038 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4039 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4040 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4041 DECL_ARTIFICIAL (decl) = 1;
4042 SET_DECL_TEMPLATE_PARM_P (decl);
4044 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4045 TEMPLATE_PARM_LEVEL (index) - levels,
4046 TEMPLATE_PARM_ORIG_LEVEL (index),
4047 decl, type);
4048 TEMPLATE_PARM_DESCENDANTS (index) = t;
4049 TEMPLATE_PARM_PARAMETER_PACK (t)
4050 = TEMPLATE_PARM_PARAMETER_PACK (index);
4052 /* Template template parameters need this. */
4053 if (TREE_CODE (decl) == TEMPLATE_DECL)
4055 DECL_TEMPLATE_RESULT (decl)
4056 = build_decl (DECL_SOURCE_LOCATION (decl),
4057 TYPE_DECL, DECL_NAME (decl), type);
4058 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4059 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4060 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4064 return TEMPLATE_PARM_DESCENDANTS (index);
4067 /* Process information from new template parameter PARM and append it
4068 to the LIST being built. This new parameter is a non-type
4069 parameter iff IS_NON_TYPE is true. This new parameter is a
4070 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4071 is in PARM_LOC. */
4073 tree
4074 process_template_parm (tree list, location_t parm_loc, tree parm,
4075 bool is_non_type, bool is_parameter_pack)
4077 tree decl = 0;
4078 int idx = 0;
4080 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4081 tree defval = TREE_PURPOSE (parm);
4082 tree constr = TREE_TYPE (parm);
4084 if (list)
4086 tree p = tree_last (list);
4088 if (p && TREE_VALUE (p) != error_mark_node)
4090 p = TREE_VALUE (p);
4091 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4092 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4093 else
4094 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4097 ++idx;
4100 if (is_non_type)
4102 parm = TREE_VALUE (parm);
4104 SET_DECL_TEMPLATE_PARM_P (parm);
4106 if (TREE_TYPE (parm) != error_mark_node)
4108 /* [temp.param]
4110 The top-level cv-qualifiers on the template-parameter are
4111 ignored when determining its type. */
4112 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4113 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4114 TREE_TYPE (parm) = error_mark_node;
4115 else if (uses_parameter_packs (TREE_TYPE (parm))
4116 && !is_parameter_pack
4117 /* If we're in a nested template parameter list, the template
4118 template parameter could be a parameter pack. */
4119 && processing_template_parmlist == 1)
4121 /* This template parameter is not a parameter pack, but it
4122 should be. Complain about "bare" parameter packs. */
4123 check_for_bare_parameter_packs (TREE_TYPE (parm));
4125 /* Recover by calling this a parameter pack. */
4126 is_parameter_pack = true;
4130 /* A template parameter is not modifiable. */
4131 TREE_CONSTANT (parm) = 1;
4132 TREE_READONLY (parm) = 1;
4133 decl = build_decl (parm_loc,
4134 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4135 TREE_CONSTANT (decl) = 1;
4136 TREE_READONLY (decl) = 1;
4137 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4138 = build_template_parm_index (idx, processing_template_decl,
4139 processing_template_decl,
4140 decl, TREE_TYPE (parm));
4142 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4143 = is_parameter_pack;
4145 else
4147 tree t;
4148 parm = TREE_VALUE (TREE_VALUE (parm));
4150 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4152 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4153 /* This is for distinguishing between real templates and template
4154 template parameters */
4155 TREE_TYPE (parm) = t;
4156 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4157 decl = parm;
4159 else
4161 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4162 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4163 decl = build_decl (parm_loc,
4164 TYPE_DECL, parm, t);
4167 TYPE_NAME (t) = decl;
4168 TYPE_STUB_DECL (t) = decl;
4169 parm = decl;
4170 TEMPLATE_TYPE_PARM_INDEX (t)
4171 = build_template_parm_index (idx, processing_template_decl,
4172 processing_template_decl,
4173 decl, TREE_TYPE (parm));
4174 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4175 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4177 DECL_ARTIFICIAL (decl) = 1;
4178 SET_DECL_TEMPLATE_PARM_P (decl);
4180 /* Build requirements for the type/template parameter.
4181 This must be done after SET_DECL_TEMPLATE_PARM_P or
4182 process_template_parm could fail. */
4183 tree reqs = finish_shorthand_constraint (parm, constr);
4185 pushdecl (decl);
4187 /* Build the parameter node linking the parameter declaration,
4188 its default argument (if any), and its constraints (if any). */
4189 parm = build_tree_list (defval, parm);
4190 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4192 return chainon (list, parm);
4195 /* The end of a template parameter list has been reached. Process the
4196 tree list into a parameter vector, converting each parameter into a more
4197 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4198 as PARM_DECLs. */
4200 tree
4201 end_template_parm_list (tree parms)
4203 int nparms;
4204 tree parm, next;
4205 tree saved_parmlist = make_tree_vec (list_length (parms));
4207 /* Pop the dummy parameter level and add the real one. */
4208 current_template_parms = TREE_CHAIN (current_template_parms);
4210 current_template_parms
4211 = tree_cons (size_int (processing_template_decl),
4212 saved_parmlist, current_template_parms);
4214 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4216 next = TREE_CHAIN (parm);
4217 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4218 TREE_CHAIN (parm) = NULL_TREE;
4221 --processing_template_parmlist;
4223 return saved_parmlist;
4226 // Explicitly indicate the end of the template parameter list. We assume
4227 // that the current template parameters have been constructed and/or
4228 // managed explicitly, as when creating new template template parameters
4229 // from a shorthand constraint.
4230 void
4231 end_template_parm_list ()
4233 --processing_template_parmlist;
4236 /* end_template_decl is called after a template declaration is seen. */
4238 void
4239 end_template_decl (void)
4241 reset_specialization ();
4243 if (! processing_template_decl)
4244 return;
4246 /* This matches the pushlevel in begin_template_parm_list. */
4247 finish_scope ();
4249 --processing_template_decl;
4250 current_template_parms = TREE_CHAIN (current_template_parms);
4253 /* Takes a TREE_LIST representing a template parameter and convert it
4254 into an argument suitable to be passed to the type substitution
4255 functions. Note that If the TREE_LIST contains an error_mark
4256 node, the returned argument is error_mark_node. */
4258 tree
4259 template_parm_to_arg (tree t)
4262 if (t == NULL_TREE
4263 || TREE_CODE (t) != TREE_LIST)
4264 return t;
4266 if (error_operand_p (TREE_VALUE (t)))
4267 return error_mark_node;
4269 t = TREE_VALUE (t);
4271 if (TREE_CODE (t) == TYPE_DECL
4272 || TREE_CODE (t) == TEMPLATE_DECL)
4274 t = TREE_TYPE (t);
4276 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4278 /* Turn this argument into a TYPE_ARGUMENT_PACK
4279 with a single element, which expands T. */
4280 tree vec = make_tree_vec (1);
4281 if (CHECKING_P)
4282 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4284 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4286 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4287 SET_ARGUMENT_PACK_ARGS (t, vec);
4290 else
4292 t = DECL_INITIAL (t);
4294 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4296 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4297 with a single element, which expands T. */
4298 tree vec = make_tree_vec (1);
4299 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4300 if (CHECKING_P)
4301 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4303 t = convert_from_reference (t);
4304 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4306 t = make_node (NONTYPE_ARGUMENT_PACK);
4307 SET_ARGUMENT_PACK_ARGS (t, vec);
4308 TREE_TYPE (t) = type;
4310 else
4311 t = convert_from_reference (t);
4313 return t;
4316 /* Given a single level of template parameters (a TREE_VEC), return it
4317 as a set of template arguments. */
4319 static tree
4320 template_parms_level_to_args (tree parms)
4322 tree a = copy_node (parms);
4323 TREE_TYPE (a) = NULL_TREE;
4324 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4325 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4327 if (CHECKING_P)
4328 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4330 return a;
4333 /* Given a set of template parameters, return them as a set of template
4334 arguments. The template parameters are represented as a TREE_VEC, in
4335 the form documented in cp-tree.h for template arguments. */
4337 static tree
4338 template_parms_to_args (tree parms)
4340 tree header;
4341 tree args = NULL_TREE;
4342 int length = TMPL_PARMS_DEPTH (parms);
4343 int l = length;
4345 /* If there is only one level of template parameters, we do not
4346 create a TREE_VEC of TREE_VECs. Instead, we return a single
4347 TREE_VEC containing the arguments. */
4348 if (length > 1)
4349 args = make_tree_vec (length);
4351 for (header = parms; header; header = TREE_CHAIN (header))
4353 tree a = template_parms_level_to_args (TREE_VALUE (header));
4355 if (length > 1)
4356 TREE_VEC_ELT (args, --l) = a;
4357 else
4358 args = a;
4361 return args;
4364 /* Within the declaration of a template, return the currently active
4365 template parameters as an argument TREE_VEC. */
4367 static tree
4368 current_template_args (void)
4370 return template_parms_to_args (current_template_parms);
4373 /* Update the declared TYPE by doing any lookups which were thought to be
4374 dependent, but are not now that we know the SCOPE of the declarator. */
4376 tree
4377 maybe_update_decl_type (tree orig_type, tree scope)
4379 tree type = orig_type;
4381 if (type == NULL_TREE)
4382 return type;
4384 if (TREE_CODE (orig_type) == TYPE_DECL)
4385 type = TREE_TYPE (type);
4387 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4388 && dependent_type_p (type)
4389 /* Don't bother building up the args in this case. */
4390 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4392 /* tsubst in the args corresponding to the template parameters,
4393 including auto if present. Most things will be unchanged, but
4394 make_typename_type and tsubst_qualified_id will resolve
4395 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4396 tree args = current_template_args ();
4397 tree auto_node = type_uses_auto (type);
4398 tree pushed;
4399 if (auto_node)
4401 tree auto_vec = make_tree_vec (1);
4402 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4403 args = add_to_template_args (args, auto_vec);
4405 pushed = push_scope (scope);
4406 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4407 if (pushed)
4408 pop_scope (scope);
4411 if (type == error_mark_node)
4412 return orig_type;
4414 if (TREE_CODE (orig_type) == TYPE_DECL)
4416 if (same_type_p (type, TREE_TYPE (orig_type)))
4417 type = orig_type;
4418 else
4419 type = TYPE_NAME (type);
4421 return type;
4424 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4425 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4426 the new template is a member template. */
4428 tree
4429 build_template_decl (tree decl, tree parms, bool member_template_p)
4431 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4432 DECL_TEMPLATE_PARMS (tmpl) = parms;
4433 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4434 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4435 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4437 return tmpl;
4440 struct template_parm_data
4442 /* The level of the template parameters we are currently
4443 processing. */
4444 int level;
4446 /* The index of the specialization argument we are currently
4447 processing. */
4448 int current_arg;
4450 /* An array whose size is the number of template parameters. The
4451 elements are nonzero if the parameter has been used in any one
4452 of the arguments processed so far. */
4453 int* parms;
4455 /* An array whose size is the number of template arguments. The
4456 elements are nonzero if the argument makes use of template
4457 parameters of this level. */
4458 int* arg_uses_template_parms;
4461 /* Subroutine of push_template_decl used to see if each template
4462 parameter in a partial specialization is used in the explicit
4463 argument list. If T is of the LEVEL given in DATA (which is
4464 treated as a template_parm_data*), then DATA->PARMS is marked
4465 appropriately. */
4467 static int
4468 mark_template_parm (tree t, void* data)
4470 int level;
4471 int idx;
4472 struct template_parm_data* tpd = (struct template_parm_data*) data;
4474 template_parm_level_and_index (t, &level, &idx);
4476 if (level == tpd->level)
4478 tpd->parms[idx] = 1;
4479 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4482 /* In C++17 the type of a non-type argument is a deduced context. */
4483 if (cxx_dialect >= cxx1z
4484 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4485 for_each_template_parm (TREE_TYPE (t),
4486 &mark_template_parm,
4487 data,
4488 NULL,
4489 /*include_nondeduced_p=*/false);
4491 /* Return zero so that for_each_template_parm will continue the
4492 traversal of the tree; we want to mark *every* template parm. */
4493 return 0;
4496 /* Process the partial specialization DECL. */
4498 static tree
4499 process_partial_specialization (tree decl)
4501 tree type = TREE_TYPE (decl);
4502 tree tinfo = get_template_info (decl);
4503 tree maintmpl = TI_TEMPLATE (tinfo);
4504 tree specargs = TI_ARGS (tinfo);
4505 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4506 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4507 tree inner_parms;
4508 tree inst;
4509 int nargs = TREE_VEC_LENGTH (inner_args);
4510 int ntparms;
4511 int i;
4512 bool did_error_intro = false;
4513 struct template_parm_data tpd;
4514 struct template_parm_data tpd2;
4516 gcc_assert (current_template_parms);
4518 /* A concept cannot be specialized. */
4519 if (flag_concepts && variable_concept_p (maintmpl))
4521 error ("specialization of variable concept %q#D", maintmpl);
4522 return error_mark_node;
4525 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4526 ntparms = TREE_VEC_LENGTH (inner_parms);
4528 /* We check that each of the template parameters given in the
4529 partial specialization is used in the argument list to the
4530 specialization. For example:
4532 template <class T> struct S;
4533 template <class T> struct S<T*>;
4535 The second declaration is OK because `T*' uses the template
4536 parameter T, whereas
4538 template <class T> struct S<int>;
4540 is no good. Even trickier is:
4542 template <class T>
4543 struct S1
4545 template <class U>
4546 struct S2;
4547 template <class U>
4548 struct S2<T>;
4551 The S2<T> declaration is actually invalid; it is a
4552 full-specialization. Of course,
4554 template <class U>
4555 struct S2<T (*)(U)>;
4557 or some such would have been OK. */
4558 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4559 tpd.parms = XALLOCAVEC (int, ntparms);
4560 memset (tpd.parms, 0, sizeof (int) * ntparms);
4562 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4563 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4564 for (i = 0; i < nargs; ++i)
4566 tpd.current_arg = i;
4567 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4568 &mark_template_parm,
4569 &tpd,
4570 NULL,
4571 /*include_nondeduced_p=*/false);
4573 for (i = 0; i < ntparms; ++i)
4574 if (tpd.parms[i] == 0)
4576 /* One of the template parms was not used in a deduced context in the
4577 specialization. */
4578 if (!did_error_intro)
4580 error ("template parameters not deducible in "
4581 "partial specialization:");
4582 did_error_intro = true;
4585 inform (input_location, " %qD",
4586 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4589 if (did_error_intro)
4590 return error_mark_node;
4592 /* [temp.class.spec]
4594 The argument list of the specialization shall not be identical to
4595 the implicit argument list of the primary template. */
4596 tree main_args
4597 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4598 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4599 && (!flag_concepts
4600 || !strictly_subsumes (current_template_constraints (),
4601 get_constraints (maintmpl))))
4603 if (!flag_concepts)
4604 error ("partial specialization %q+D does not specialize "
4605 "any template arguments", decl);
4606 else
4607 error ("partial specialization %q+D does not specialize any "
4608 "template arguments and is not more constrained than", decl);
4609 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4612 /* A partial specialization that replaces multiple parameters of the
4613 primary template with a pack expansion is less specialized for those
4614 parameters. */
4615 if (nargs < DECL_NTPARMS (maintmpl))
4617 error ("partial specialization is not more specialized than the "
4618 "primary template because it replaces multiple parameters "
4619 "with a pack expansion");
4620 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4621 /* Avoid crash in process_partial_specialization. */
4622 return decl;
4625 /* If we aren't in a dependent class, we can actually try deduction. */
4626 else if (tpd.level == 1
4627 /* FIXME we should be able to handle a partial specialization of a
4628 partial instantiation, but currently we can't (c++/41727). */
4629 && TMPL_ARGS_DEPTH (specargs) == 1
4630 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4632 if (permerror (input_location, "partial specialization %qD is not "
4633 "more specialized than", decl))
4634 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4635 maintmpl);
4638 /* [temp.class.spec]
4640 A partially specialized non-type argument expression shall not
4641 involve template parameters of the partial specialization except
4642 when the argument expression is a simple identifier.
4644 The type of a template parameter corresponding to a specialized
4645 non-type argument shall not be dependent on a parameter of the
4646 specialization.
4648 Also, we verify that pack expansions only occur at the
4649 end of the argument list. */
4650 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4651 tpd2.parms = 0;
4652 for (i = 0; i < nargs; ++i)
4654 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4655 tree arg = TREE_VEC_ELT (inner_args, i);
4656 tree packed_args = NULL_TREE;
4657 int j, len = 1;
4659 if (ARGUMENT_PACK_P (arg))
4661 /* Extract the arguments from the argument pack. We'll be
4662 iterating over these in the following loop. */
4663 packed_args = ARGUMENT_PACK_ARGS (arg);
4664 len = TREE_VEC_LENGTH (packed_args);
4667 for (j = 0; j < len; j++)
4669 if (packed_args)
4670 /* Get the Jth argument in the parameter pack. */
4671 arg = TREE_VEC_ELT (packed_args, j);
4673 if (PACK_EXPANSION_P (arg))
4675 /* Pack expansions must come at the end of the
4676 argument list. */
4677 if ((packed_args && j < len - 1)
4678 || (!packed_args && i < nargs - 1))
4680 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4681 error ("parameter pack argument %qE must be at the "
4682 "end of the template argument list", arg);
4683 else
4684 error ("parameter pack argument %qT must be at the "
4685 "end of the template argument list", arg);
4689 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4690 /* We only care about the pattern. */
4691 arg = PACK_EXPANSION_PATTERN (arg);
4693 if (/* These first two lines are the `non-type' bit. */
4694 !TYPE_P (arg)
4695 && TREE_CODE (arg) != TEMPLATE_DECL
4696 /* This next two lines are the `argument expression is not just a
4697 simple identifier' condition and also the `specialized
4698 non-type argument' bit. */
4699 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4700 && !(REFERENCE_REF_P (arg)
4701 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4703 if ((!packed_args && tpd.arg_uses_template_parms[i])
4704 || (packed_args && uses_template_parms (arg)))
4705 error ("template argument %qE involves template parameter(s)",
4706 arg);
4707 else
4709 /* Look at the corresponding template parameter,
4710 marking which template parameters its type depends
4711 upon. */
4712 tree type = TREE_TYPE (parm);
4714 if (!tpd2.parms)
4716 /* We haven't yet initialized TPD2. Do so now. */
4717 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4718 /* The number of parameters here is the number in the
4719 main template, which, as checked in the assertion
4720 above, is NARGS. */
4721 tpd2.parms = XALLOCAVEC (int, nargs);
4722 tpd2.level =
4723 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4726 /* Mark the template parameters. But this time, we're
4727 looking for the template parameters of the main
4728 template, not in the specialization. */
4729 tpd2.current_arg = i;
4730 tpd2.arg_uses_template_parms[i] = 0;
4731 memset (tpd2.parms, 0, sizeof (int) * nargs);
4732 for_each_template_parm (type,
4733 &mark_template_parm,
4734 &tpd2,
4735 NULL,
4736 /*include_nondeduced_p=*/false);
4738 if (tpd2.arg_uses_template_parms [i])
4740 /* The type depended on some template parameters.
4741 If they are fully specialized in the
4742 specialization, that's OK. */
4743 int j;
4744 int count = 0;
4745 for (j = 0; j < nargs; ++j)
4746 if (tpd2.parms[j] != 0
4747 && tpd.arg_uses_template_parms [j])
4748 ++count;
4749 if (count != 0)
4750 error_n (input_location, count,
4751 "type %qT of template argument %qE depends "
4752 "on a template parameter",
4753 "type %qT of template argument %qE depends "
4754 "on template parameters",
4755 type,
4756 arg);
4763 /* We should only get here once. */
4764 if (TREE_CODE (decl) == TYPE_DECL)
4765 gcc_assert (!COMPLETE_TYPE_P (type));
4767 // Build the template decl.
4768 tree tmpl = build_template_decl (decl, current_template_parms,
4769 DECL_MEMBER_TEMPLATE_P (maintmpl));
4770 TREE_TYPE (tmpl) = type;
4771 DECL_TEMPLATE_RESULT (tmpl) = decl;
4772 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4773 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4774 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4776 /* Give template template parms a DECL_CONTEXT of the template
4777 for which they are a parameter. */
4778 for (i = 0; i < ntparms; ++i)
4780 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4781 if (TREE_CODE (parm) == TEMPLATE_DECL)
4782 DECL_CONTEXT (parm) = tmpl;
4785 if (VAR_P (decl))
4786 /* We didn't register this in check_explicit_specialization so we could
4787 wait until the constraints were set. */
4788 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4789 else
4790 associate_classtype_constraints (type);
4792 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4793 = tree_cons (specargs, tmpl,
4794 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4795 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4797 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4798 inst = TREE_CHAIN (inst))
4800 tree instance = TREE_VALUE (inst);
4801 if (TYPE_P (instance)
4802 ? (COMPLETE_TYPE_P (instance)
4803 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4804 : DECL_TEMPLATE_INSTANTIATION (instance))
4806 tree spec = most_specialized_partial_spec (instance, tf_none);
4807 tree inst_decl = (DECL_P (instance)
4808 ? instance : TYPE_NAME (instance));
4809 if (!spec)
4810 /* OK */;
4811 else if (spec == error_mark_node)
4812 permerror (input_location,
4813 "declaration of %qD ambiguates earlier template "
4814 "instantiation for %qD", decl, inst_decl);
4815 else if (TREE_VALUE (spec) == tmpl)
4816 permerror (input_location,
4817 "partial specialization of %qD after instantiation "
4818 "of %qD", decl, inst_decl);
4822 return decl;
4825 /* PARM is a template parameter of some form; return the corresponding
4826 TEMPLATE_PARM_INDEX. */
4828 static tree
4829 get_template_parm_index (tree parm)
4831 if (TREE_CODE (parm) == PARM_DECL
4832 || TREE_CODE (parm) == CONST_DECL)
4833 parm = DECL_INITIAL (parm);
4834 else if (TREE_CODE (parm) == TYPE_DECL
4835 || TREE_CODE (parm) == TEMPLATE_DECL)
4836 parm = TREE_TYPE (parm);
4837 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4838 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4839 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4840 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4841 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4842 return parm;
4845 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4846 parameter packs used by the template parameter PARM. */
4848 static void
4849 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4851 /* A type parm can't refer to another parm. */
4852 if (TREE_CODE (parm) == TYPE_DECL)
4853 return;
4854 else if (TREE_CODE (parm) == PARM_DECL)
4856 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4857 ppd, ppd->visited);
4858 return;
4861 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4863 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4864 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4865 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4868 /* PARM is a template parameter pack. Return any parameter packs used in
4869 its type or the type of any of its template parameters. If there are
4870 any such packs, it will be instantiated into a fixed template parameter
4871 list by partial instantiation rather than be fully deduced. */
4873 tree
4874 fixed_parameter_pack_p (tree parm)
4876 /* This can only be true in a member template. */
4877 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4878 return NULL_TREE;
4879 /* This can only be true for a parameter pack. */
4880 if (!template_parameter_pack_p (parm))
4881 return NULL_TREE;
4882 /* A type parm can't refer to another parm. */
4883 if (TREE_CODE (parm) == TYPE_DECL)
4884 return NULL_TREE;
4886 tree parameter_packs = NULL_TREE;
4887 struct find_parameter_pack_data ppd;
4888 ppd.parameter_packs = &parameter_packs;
4889 ppd.visited = new hash_set<tree>;
4890 ppd.type_pack_expansion_p = false;
4892 fixed_parameter_pack_p_1 (parm, &ppd);
4894 delete ppd.visited;
4895 return parameter_packs;
4898 /* Check that a template declaration's use of default arguments and
4899 parameter packs is not invalid. Here, PARMS are the template
4900 parameters. IS_PRIMARY is true if DECL is the thing declared by
4901 a primary template. IS_PARTIAL is true if DECL is a partial
4902 specialization.
4904 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4905 declaration (but not a definition); 1 indicates a declaration, 2
4906 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4907 emitted for extraneous default arguments.
4909 Returns TRUE if there were no errors found, FALSE otherwise. */
4911 bool
4912 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4913 bool is_partial, int is_friend_decl)
4915 const char *msg;
4916 int last_level_to_check;
4917 tree parm_level;
4918 bool no_errors = true;
4920 /* [temp.param]
4922 A default template-argument shall not be specified in a
4923 function template declaration or a function template definition, nor
4924 in the template-parameter-list of the definition of a member of a
4925 class template. */
4927 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4928 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4929 /* You can't have a function template declaration in a local
4930 scope, nor you can you define a member of a class template in a
4931 local scope. */
4932 return true;
4934 if ((TREE_CODE (decl) == TYPE_DECL
4935 && TREE_TYPE (decl)
4936 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4937 || (TREE_CODE (decl) == FUNCTION_DECL
4938 && LAMBDA_FUNCTION_P (decl)))
4939 /* A lambda doesn't have an explicit declaration; don't complain
4940 about the parms of the enclosing class. */
4941 return true;
4943 if (current_class_type
4944 && !TYPE_BEING_DEFINED (current_class_type)
4945 && DECL_LANG_SPECIFIC (decl)
4946 && DECL_DECLARES_FUNCTION_P (decl)
4947 /* If this is either a friend defined in the scope of the class
4948 or a member function. */
4949 && (DECL_FUNCTION_MEMBER_P (decl)
4950 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4951 : DECL_FRIEND_CONTEXT (decl)
4952 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4953 : false)
4954 /* And, if it was a member function, it really was defined in
4955 the scope of the class. */
4956 && (!DECL_FUNCTION_MEMBER_P (decl)
4957 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4958 /* We already checked these parameters when the template was
4959 declared, so there's no need to do it again now. This function
4960 was defined in class scope, but we're processing its body now
4961 that the class is complete. */
4962 return true;
4964 /* Core issue 226 (C++0x only): the following only applies to class
4965 templates. */
4966 if (is_primary
4967 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4969 /* [temp.param]
4971 If a template-parameter has a default template-argument, all
4972 subsequent template-parameters shall have a default
4973 template-argument supplied. */
4974 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4976 tree inner_parms = TREE_VALUE (parm_level);
4977 int ntparms = TREE_VEC_LENGTH (inner_parms);
4978 int seen_def_arg_p = 0;
4979 int i;
4981 for (i = 0; i < ntparms; ++i)
4983 tree parm = TREE_VEC_ELT (inner_parms, i);
4985 if (parm == error_mark_node)
4986 continue;
4988 if (TREE_PURPOSE (parm))
4989 seen_def_arg_p = 1;
4990 else if (seen_def_arg_p
4991 && !template_parameter_pack_p (TREE_VALUE (parm)))
4993 error ("no default argument for %qD", TREE_VALUE (parm));
4994 /* For better subsequent error-recovery, we indicate that
4995 there should have been a default argument. */
4996 TREE_PURPOSE (parm) = error_mark_node;
4997 no_errors = false;
4999 else if (!is_partial
5000 && !is_friend_decl
5001 /* Don't complain about an enclosing partial
5002 specialization. */
5003 && parm_level == parms
5004 && TREE_CODE (decl) == TYPE_DECL
5005 && i < ntparms - 1
5006 && template_parameter_pack_p (TREE_VALUE (parm))
5007 /* A fixed parameter pack will be partially
5008 instantiated into a fixed length list. */
5009 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5011 /* A primary class template can only have one
5012 parameter pack, at the end of the template
5013 parameter list. */
5015 error ("parameter pack %q+D must be at the end of the"
5016 " template parameter list", TREE_VALUE (parm));
5018 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5019 = error_mark_node;
5020 no_errors = false;
5026 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5027 || is_partial
5028 || !is_primary
5029 || is_friend_decl)
5030 /* For an ordinary class template, default template arguments are
5031 allowed at the innermost level, e.g.:
5032 template <class T = int>
5033 struct S {};
5034 but, in a partial specialization, they're not allowed even
5035 there, as we have in [temp.class.spec]:
5037 The template parameter list of a specialization shall not
5038 contain default template argument values.
5040 So, for a partial specialization, or for a function template
5041 (in C++98/C++03), we look at all of them. */
5043 else
5044 /* But, for a primary class template that is not a partial
5045 specialization we look at all template parameters except the
5046 innermost ones. */
5047 parms = TREE_CHAIN (parms);
5049 /* Figure out what error message to issue. */
5050 if (is_friend_decl == 2)
5051 msg = G_("default template arguments may not be used in function template "
5052 "friend re-declaration");
5053 else if (is_friend_decl)
5054 msg = G_("default template arguments may not be used in function template "
5055 "friend declarations");
5056 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5057 msg = G_("default template arguments may not be used in function templates "
5058 "without -std=c++11 or -std=gnu++11");
5059 else if (is_partial)
5060 msg = G_("default template arguments may not be used in "
5061 "partial specializations");
5062 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5063 msg = G_("default argument for template parameter for class enclosing %qD");
5064 else
5065 /* Per [temp.param]/9, "A default template-argument shall not be
5066 specified in the template-parameter-lists of the definition of
5067 a member of a class template that appears outside of the member's
5068 class.", thus if we aren't handling a member of a class template
5069 there is no need to examine the parameters. */
5070 return true;
5072 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5073 /* If we're inside a class definition, there's no need to
5074 examine the parameters to the class itself. On the one
5075 hand, they will be checked when the class is defined, and,
5076 on the other, default arguments are valid in things like:
5077 template <class T = double>
5078 struct S { template <class U> void f(U); };
5079 Here the default argument for `S' has no bearing on the
5080 declaration of `f'. */
5081 last_level_to_check = template_class_depth (current_class_type) + 1;
5082 else
5083 /* Check everything. */
5084 last_level_to_check = 0;
5086 for (parm_level = parms;
5087 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5088 parm_level = TREE_CHAIN (parm_level))
5090 tree inner_parms = TREE_VALUE (parm_level);
5091 int i;
5092 int ntparms;
5094 ntparms = TREE_VEC_LENGTH (inner_parms);
5095 for (i = 0; i < ntparms; ++i)
5097 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5098 continue;
5100 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5102 if (msg)
5104 no_errors = false;
5105 if (is_friend_decl == 2)
5106 return no_errors;
5108 error (msg, decl);
5109 msg = 0;
5112 /* Clear out the default argument so that we are not
5113 confused later. */
5114 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5118 /* At this point, if we're still interested in issuing messages,
5119 they must apply to classes surrounding the object declared. */
5120 if (msg)
5121 msg = G_("default argument for template parameter for class "
5122 "enclosing %qD");
5125 return no_errors;
5128 /* Worker for push_template_decl_real, called via
5129 for_each_template_parm. DATA is really an int, indicating the
5130 level of the parameters we are interested in. If T is a template
5131 parameter of that level, return nonzero. */
5133 static int
5134 template_parm_this_level_p (tree t, void* data)
5136 int this_level = *(int *)data;
5137 int level;
5139 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5140 level = TEMPLATE_PARM_LEVEL (t);
5141 else
5142 level = TEMPLATE_TYPE_LEVEL (t);
5143 return level == this_level;
5146 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5147 DATA is really an int, indicating the innermost outer level of parameters.
5148 If T is a template parameter of that level or further out, return
5149 nonzero. */
5151 static int
5152 template_parm_outer_level (tree t, void *data)
5154 int this_level = *(int *)data;
5155 int level;
5157 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5158 level = TEMPLATE_PARM_LEVEL (t);
5159 else
5160 level = TEMPLATE_TYPE_LEVEL (t);
5161 return level <= this_level;
5164 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5165 parameters given by current_template_args, or reuses a
5166 previously existing one, if appropriate. Returns the DECL, or an
5167 equivalent one, if it is replaced via a call to duplicate_decls.
5169 If IS_FRIEND is true, DECL is a friend declaration. */
5171 tree
5172 push_template_decl_real (tree decl, bool is_friend)
5174 tree tmpl;
5175 tree args;
5176 tree info;
5177 tree ctx;
5178 bool is_primary;
5179 bool is_partial;
5180 int new_template_p = 0;
5181 /* True if the template is a member template, in the sense of
5182 [temp.mem]. */
5183 bool member_template_p = false;
5185 if (decl == error_mark_node || !current_template_parms)
5186 return error_mark_node;
5188 /* See if this is a partial specialization. */
5189 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5190 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5191 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5192 || (VAR_P (decl)
5193 && DECL_LANG_SPECIFIC (decl)
5194 && DECL_TEMPLATE_SPECIALIZATION (decl)
5195 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5197 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5198 is_friend = true;
5200 if (is_friend)
5201 /* For a friend, we want the context of the friend function, not
5202 the type of which it is a friend. */
5203 ctx = CP_DECL_CONTEXT (decl);
5204 else if (CP_DECL_CONTEXT (decl)
5205 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5206 /* In the case of a virtual function, we want the class in which
5207 it is defined. */
5208 ctx = CP_DECL_CONTEXT (decl);
5209 else
5210 /* Otherwise, if we're currently defining some class, the DECL
5211 is assumed to be a member of the class. */
5212 ctx = current_scope ();
5214 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5215 ctx = NULL_TREE;
5217 if (!DECL_CONTEXT (decl))
5218 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5220 /* See if this is a primary template. */
5221 if (is_friend && ctx
5222 && uses_template_parms_level (ctx, processing_template_decl))
5223 /* A friend template that specifies a class context, i.e.
5224 template <typename T> friend void A<T>::f();
5225 is not primary. */
5226 is_primary = false;
5227 else if (TREE_CODE (decl) == TYPE_DECL
5228 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5229 is_primary = false;
5230 else
5231 is_primary = template_parm_scope_p ();
5233 if (is_primary)
5235 warning (OPT_Wtemplates, "template %qD declared", decl);
5237 if (DECL_CLASS_SCOPE_P (decl))
5238 member_template_p = true;
5239 if (TREE_CODE (decl) == TYPE_DECL
5240 && anon_aggrname_p (DECL_NAME (decl)))
5242 error ("template class without a name");
5243 return error_mark_node;
5245 else if (TREE_CODE (decl) == FUNCTION_DECL)
5247 if (member_template_p)
5249 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5250 error ("member template %qD may not have virt-specifiers", decl);
5252 if (DECL_DESTRUCTOR_P (decl))
5254 /* [temp.mem]
5256 A destructor shall not be a member template. */
5257 error ("destructor %qD declared as member template", decl);
5258 return error_mark_node;
5260 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5261 && (!prototype_p (TREE_TYPE (decl))
5262 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5263 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5264 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5265 == void_list_node)))
5267 /* [basic.stc.dynamic.allocation]
5269 An allocation function can be a function
5270 template. ... Template allocation functions shall
5271 have two or more parameters. */
5272 error ("invalid template declaration of %qD", decl);
5273 return error_mark_node;
5276 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5277 && CLASS_TYPE_P (TREE_TYPE (decl)))
5279 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5280 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5281 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5283 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5284 if (TREE_CODE (t) == TYPE_DECL)
5285 t = TREE_TYPE (t);
5286 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5287 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5290 else if (TREE_CODE (decl) == TYPE_DECL
5291 && TYPE_DECL_ALIAS_P (decl))
5292 /* alias-declaration */
5293 gcc_assert (!DECL_ARTIFICIAL (decl));
5294 else if (VAR_P (decl))
5295 /* C++14 variable template. */;
5296 else
5298 error ("template declaration of %q#D", decl);
5299 return error_mark_node;
5303 /* Check to see that the rules regarding the use of default
5304 arguments are not being violated. */
5305 check_default_tmpl_args (decl, current_template_parms,
5306 is_primary, is_partial, /*is_friend_decl=*/0);
5308 /* Ensure that there are no parameter packs in the type of this
5309 declaration that have not been expanded. */
5310 if (TREE_CODE (decl) == FUNCTION_DECL)
5312 /* Check each of the arguments individually to see if there are
5313 any bare parameter packs. */
5314 tree type = TREE_TYPE (decl);
5315 tree arg = DECL_ARGUMENTS (decl);
5316 tree argtype = TYPE_ARG_TYPES (type);
5318 while (arg && argtype)
5320 if (!DECL_PACK_P (arg)
5321 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5323 /* This is a PARM_DECL that contains unexpanded parameter
5324 packs. We have already complained about this in the
5325 check_for_bare_parameter_packs call, so just replace
5326 these types with ERROR_MARK_NODE. */
5327 TREE_TYPE (arg) = error_mark_node;
5328 TREE_VALUE (argtype) = error_mark_node;
5331 arg = DECL_CHAIN (arg);
5332 argtype = TREE_CHAIN (argtype);
5335 /* Check for bare parameter packs in the return type and the
5336 exception specifiers. */
5337 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5338 /* Errors were already issued, set return type to int
5339 as the frontend doesn't expect error_mark_node as
5340 the return type. */
5341 TREE_TYPE (type) = integer_type_node;
5342 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5343 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5345 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5346 && TYPE_DECL_ALIAS_P (decl))
5347 ? DECL_ORIGINAL_TYPE (decl)
5348 : TREE_TYPE (decl)))
5350 TREE_TYPE (decl) = error_mark_node;
5351 return error_mark_node;
5354 if (is_partial)
5355 return process_partial_specialization (decl);
5357 args = current_template_args ();
5359 if (!ctx
5360 || TREE_CODE (ctx) == FUNCTION_DECL
5361 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5362 || (TREE_CODE (decl) == TYPE_DECL
5363 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5364 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5366 if (DECL_LANG_SPECIFIC (decl)
5367 && DECL_TEMPLATE_INFO (decl)
5368 && DECL_TI_TEMPLATE (decl))
5369 tmpl = DECL_TI_TEMPLATE (decl);
5370 /* If DECL is a TYPE_DECL for a class-template, then there won't
5371 be DECL_LANG_SPECIFIC. The information equivalent to
5372 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5373 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5374 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5375 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5377 /* Since a template declaration already existed for this
5378 class-type, we must be redeclaring it here. Make sure
5379 that the redeclaration is valid. */
5380 redeclare_class_template (TREE_TYPE (decl),
5381 current_template_parms,
5382 current_template_constraints ());
5383 /* We don't need to create a new TEMPLATE_DECL; just use the
5384 one we already had. */
5385 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5387 else
5389 tmpl = build_template_decl (decl, current_template_parms,
5390 member_template_p);
5391 new_template_p = 1;
5393 if (DECL_LANG_SPECIFIC (decl)
5394 && DECL_TEMPLATE_SPECIALIZATION (decl))
5396 /* A specialization of a member template of a template
5397 class. */
5398 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5399 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5400 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5404 else
5406 tree a, t, current, parms;
5407 int i;
5408 tree tinfo = get_template_info (decl);
5410 if (!tinfo)
5412 error ("template definition of non-template %q#D", decl);
5413 return error_mark_node;
5416 tmpl = TI_TEMPLATE (tinfo);
5418 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5419 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5420 && DECL_TEMPLATE_SPECIALIZATION (decl)
5421 && DECL_MEMBER_TEMPLATE_P (tmpl))
5423 tree new_tmpl;
5425 /* The declaration is a specialization of a member
5426 template, declared outside the class. Therefore, the
5427 innermost template arguments will be NULL, so we
5428 replace them with the arguments determined by the
5429 earlier call to check_explicit_specialization. */
5430 args = DECL_TI_ARGS (decl);
5432 new_tmpl
5433 = build_template_decl (decl, current_template_parms,
5434 member_template_p);
5435 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5436 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5437 DECL_TI_TEMPLATE (decl) = new_tmpl;
5438 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5439 DECL_TEMPLATE_INFO (new_tmpl)
5440 = build_template_info (tmpl, args);
5442 register_specialization (new_tmpl,
5443 most_general_template (tmpl),
5444 args,
5445 is_friend, 0);
5446 return decl;
5449 /* Make sure the template headers we got make sense. */
5451 parms = DECL_TEMPLATE_PARMS (tmpl);
5452 i = TMPL_PARMS_DEPTH (parms);
5453 if (TMPL_ARGS_DEPTH (args) != i)
5455 error ("expected %d levels of template parms for %q#D, got %d",
5456 i, decl, TMPL_ARGS_DEPTH (args));
5457 DECL_INTERFACE_KNOWN (decl) = 1;
5458 return error_mark_node;
5460 else
5461 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5463 a = TMPL_ARGS_LEVEL (args, i);
5464 t = INNERMOST_TEMPLATE_PARMS (parms);
5466 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5468 if (current == decl)
5469 error ("got %d template parameters for %q#D",
5470 TREE_VEC_LENGTH (a), decl);
5471 else
5472 error ("got %d template parameters for %q#T",
5473 TREE_VEC_LENGTH (a), current);
5474 error (" but %d required", TREE_VEC_LENGTH (t));
5475 /* Avoid crash in import_export_decl. */
5476 DECL_INTERFACE_KNOWN (decl) = 1;
5477 return error_mark_node;
5480 if (current == decl)
5481 current = ctx;
5482 else if (current == NULL_TREE)
5483 /* Can happen in erroneous input. */
5484 break;
5485 else
5486 current = get_containing_scope (current);
5489 /* Check that the parms are used in the appropriate qualifying scopes
5490 in the declarator. */
5491 if (!comp_template_args
5492 (TI_ARGS (tinfo),
5493 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5495 error ("\
5496 template arguments to %qD do not match original template %qD",
5497 decl, DECL_TEMPLATE_RESULT (tmpl));
5498 if (!uses_template_parms (TI_ARGS (tinfo)))
5499 inform (input_location, "use template<> for an explicit specialization");
5500 /* Avoid crash in import_export_decl. */
5501 DECL_INTERFACE_KNOWN (decl) = 1;
5502 return error_mark_node;
5506 DECL_TEMPLATE_RESULT (tmpl) = decl;
5507 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5509 /* Push template declarations for global functions and types. Note
5510 that we do not try to push a global template friend declared in a
5511 template class; such a thing may well depend on the template
5512 parameters of the class. */
5513 if (new_template_p && !ctx
5514 && !(is_friend && template_class_depth (current_class_type) > 0))
5516 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5517 if (tmpl == error_mark_node)
5518 return error_mark_node;
5520 /* Hide template friend classes that haven't been declared yet. */
5521 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5523 DECL_ANTICIPATED (tmpl) = 1;
5524 DECL_FRIEND_P (tmpl) = 1;
5528 if (is_primary)
5530 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5531 int i;
5533 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5534 if (DECL_CONV_FN_P (tmpl))
5536 int depth = TMPL_PARMS_DEPTH (parms);
5538 /* It is a conversion operator. See if the type converted to
5539 depends on innermost template operands. */
5541 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5542 depth))
5543 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5546 /* Give template template parms a DECL_CONTEXT of the template
5547 for which they are a parameter. */
5548 parms = INNERMOST_TEMPLATE_PARMS (parms);
5549 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5551 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5552 if (TREE_CODE (parm) == TEMPLATE_DECL)
5553 DECL_CONTEXT (parm) = tmpl;
5556 if (TREE_CODE (decl) == TYPE_DECL
5557 && TYPE_DECL_ALIAS_P (decl)
5558 && complex_alias_template_p (tmpl))
5559 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5562 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5563 back to its most general template. If TMPL is a specialization,
5564 ARGS may only have the innermost set of arguments. Add the missing
5565 argument levels if necessary. */
5566 if (DECL_TEMPLATE_INFO (tmpl))
5567 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5569 info = build_template_info (tmpl, args);
5571 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5572 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5573 else
5575 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5576 retrofit_lang_decl (decl);
5577 if (DECL_LANG_SPECIFIC (decl))
5578 DECL_TEMPLATE_INFO (decl) = info;
5581 if (flag_implicit_templates
5582 && !is_friend
5583 && TREE_PUBLIC (decl)
5584 && VAR_OR_FUNCTION_DECL_P (decl))
5585 /* Set DECL_COMDAT on template instantiations; if we force
5586 them to be emitted by explicit instantiation or -frepo,
5587 mark_needed will tell cgraph to do the right thing. */
5588 DECL_COMDAT (decl) = true;
5590 return DECL_TEMPLATE_RESULT (tmpl);
5593 tree
5594 push_template_decl (tree decl)
5596 return push_template_decl_real (decl, false);
5599 /* FN is an inheriting constructor that inherits from the constructor
5600 template INHERITED; turn FN into a constructor template with a matching
5601 template header. */
5603 tree
5604 add_inherited_template_parms (tree fn, tree inherited)
5606 tree inner_parms
5607 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5608 inner_parms = copy_node (inner_parms);
5609 tree parms
5610 = tree_cons (size_int (processing_template_decl + 1),
5611 inner_parms, current_template_parms);
5612 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5613 tree args = template_parms_to_args (parms);
5614 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5615 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5616 DECL_TEMPLATE_RESULT (tmpl) = fn;
5617 DECL_ARTIFICIAL (tmpl) = true;
5618 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5619 return tmpl;
5622 /* Called when a class template TYPE is redeclared with the indicated
5623 template PARMS, e.g.:
5625 template <class T> struct S;
5626 template <class T> struct S {}; */
5628 bool
5629 redeclare_class_template (tree type, tree parms, tree cons)
5631 tree tmpl;
5632 tree tmpl_parms;
5633 int i;
5635 if (!TYPE_TEMPLATE_INFO (type))
5637 error ("%qT is not a template type", type);
5638 return false;
5641 tmpl = TYPE_TI_TEMPLATE (type);
5642 if (!PRIMARY_TEMPLATE_P (tmpl))
5643 /* The type is nested in some template class. Nothing to worry
5644 about here; there are no new template parameters for the nested
5645 type. */
5646 return true;
5648 if (!parms)
5650 error ("template specifiers not specified in declaration of %qD",
5651 tmpl);
5652 return false;
5655 parms = INNERMOST_TEMPLATE_PARMS (parms);
5656 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5658 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5660 error_n (input_location, TREE_VEC_LENGTH (parms),
5661 "redeclared with %d template parameter",
5662 "redeclared with %d template parameters",
5663 TREE_VEC_LENGTH (parms));
5664 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5665 "previous declaration %qD used %d template parameter",
5666 "previous declaration %qD used %d template parameters",
5667 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5668 return false;
5671 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5673 tree tmpl_parm;
5674 tree parm;
5675 tree tmpl_default;
5676 tree parm_default;
5678 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5679 || TREE_VEC_ELT (parms, i) == error_mark_node)
5680 continue;
5682 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5683 if (error_operand_p (tmpl_parm))
5684 return false;
5686 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5687 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5688 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5690 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5691 TEMPLATE_DECL. */
5692 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5693 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5694 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5695 || (TREE_CODE (tmpl_parm) != PARM_DECL
5696 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5697 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5698 || (TREE_CODE (tmpl_parm) == PARM_DECL
5699 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5700 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5702 error ("template parameter %q+#D", tmpl_parm);
5703 error ("redeclared here as %q#D", parm);
5704 return false;
5707 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5709 /* We have in [temp.param]:
5711 A template-parameter may not be given default arguments
5712 by two different declarations in the same scope. */
5713 error_at (input_location, "redefinition of default argument for %q#D", parm);
5714 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5715 "original definition appeared here");
5716 return false;
5719 if (parm_default != NULL_TREE)
5720 /* Update the previous template parameters (which are the ones
5721 that will really count) with the new default value. */
5722 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5723 else if (tmpl_default != NULL_TREE)
5724 /* Update the new parameters, too; they'll be used as the
5725 parameters for any members. */
5726 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5728 /* Give each template template parm in this redeclaration a
5729 DECL_CONTEXT of the template for which they are a parameter. */
5730 if (TREE_CODE (parm) == TEMPLATE_DECL)
5732 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5733 DECL_CONTEXT (parm) = tmpl;
5736 if (TREE_CODE (parm) == TYPE_DECL)
5737 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5740 // Cannot redeclare a class template with a different set of constraints.
5741 if (!equivalent_constraints (get_constraints (tmpl), cons))
5743 error_at (input_location, "redeclaration %q#D with different "
5744 "constraints", tmpl);
5745 inform (DECL_SOURCE_LOCATION (tmpl),
5746 "original declaration appeared here");
5749 return true;
5752 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5753 to be used when the caller has already checked
5754 (processing_template_decl
5755 && !instantiation_dependent_expression_p (expr)
5756 && potential_constant_expression (expr))
5757 and cleared processing_template_decl. */
5759 tree
5760 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5762 return tsubst_copy_and_build (expr,
5763 /*args=*/NULL_TREE,
5764 complain,
5765 /*in_decl=*/NULL_TREE,
5766 /*function_p=*/false,
5767 /*integral_constant_expression_p=*/true);
5770 /* Simplify EXPR if it is a non-dependent expression. Returns the
5771 (possibly simplified) expression. */
5773 tree
5774 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5776 if (expr == NULL_TREE)
5777 return NULL_TREE;
5779 /* If we're in a template, but EXPR isn't value dependent, simplify
5780 it. We're supposed to treat:
5782 template <typename T> void f(T[1 + 1]);
5783 template <typename T> void f(T[2]);
5785 as two declarations of the same function, for example. */
5786 if (processing_template_decl
5787 && potential_nondependent_constant_expression (expr))
5789 processing_template_decl_sentinel s;
5790 expr = instantiate_non_dependent_expr_internal (expr, complain);
5792 return expr;
5795 tree
5796 instantiate_non_dependent_expr (tree expr)
5798 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5801 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5802 an uninstantiated expression. */
5804 tree
5805 instantiate_non_dependent_or_null (tree expr)
5807 if (expr == NULL_TREE)
5808 return NULL_TREE;
5809 if (processing_template_decl)
5811 if (!potential_nondependent_constant_expression (expr))
5812 expr = NULL_TREE;
5813 else
5815 processing_template_decl_sentinel s;
5816 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5819 return expr;
5822 /* True iff T is a specialization of a variable template. */
5824 bool
5825 variable_template_specialization_p (tree t)
5827 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5828 return false;
5829 tree tmpl = DECL_TI_TEMPLATE (t);
5830 return variable_template_p (tmpl);
5833 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5834 template declaration, or a TYPE_DECL for an alias declaration. */
5836 bool
5837 alias_type_or_template_p (tree t)
5839 if (t == NULL_TREE)
5840 return false;
5841 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5842 || (TYPE_P (t)
5843 && TYPE_NAME (t)
5844 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5845 || DECL_ALIAS_TEMPLATE_P (t));
5848 /* Return TRUE iff T is a specialization of an alias template. */
5850 bool
5851 alias_template_specialization_p (const_tree t)
5853 /* It's an alias template specialization if it's an alias and its
5854 TYPE_NAME is a specialization of a primary template. */
5855 if (TYPE_ALIAS_P (t))
5856 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5857 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5859 return false;
5862 /* An alias template is complex from a SFINAE perspective if a template-id
5863 using that alias can be ill-formed when the expansion is not, as with
5864 the void_t template. We determine this by checking whether the
5865 expansion for the alias template uses all its template parameters. */
5867 struct uses_all_template_parms_data
5869 int level;
5870 bool *seen;
5873 static int
5874 uses_all_template_parms_r (tree t, void *data_)
5876 struct uses_all_template_parms_data &data
5877 = *(struct uses_all_template_parms_data*)data_;
5878 tree idx = get_template_parm_index (t);
5880 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5881 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5882 return 0;
5885 static bool
5886 complex_alias_template_p (const_tree tmpl)
5888 struct uses_all_template_parms_data data;
5889 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5890 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5891 data.level = TMPL_PARMS_DEPTH (parms);
5892 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5893 data.seen = XALLOCAVEC (bool, len);
5894 for (int i = 0; i < len; ++i)
5895 data.seen[i] = false;
5897 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5898 for (int i = 0; i < len; ++i)
5899 if (!data.seen[i])
5900 return true;
5901 return false;
5904 /* Return TRUE iff T is a specialization of a complex alias template with
5905 dependent template-arguments. */
5907 bool
5908 dependent_alias_template_spec_p (const_tree t)
5910 if (!alias_template_specialization_p (t))
5911 return false;
5913 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5914 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5915 return false;
5917 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5918 if (!any_dependent_template_arguments_p (args))
5919 return false;
5921 return true;
5924 /* Return the number of innermost template parameters in TMPL. */
5926 static int
5927 num_innermost_template_parms (tree tmpl)
5929 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5930 return TREE_VEC_LENGTH (parms);
5933 /* Return either TMPL or another template that it is equivalent to under DR
5934 1286: An alias that just changes the name of a template is equivalent to
5935 the other template. */
5937 static tree
5938 get_underlying_template (tree tmpl)
5940 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5941 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5943 /* Determine if the alias is equivalent to an underlying template. */
5944 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5945 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
5946 if (!tinfo)
5947 break;
5949 tree underlying = TI_TEMPLATE (tinfo);
5950 if (!PRIMARY_TEMPLATE_P (underlying)
5951 || (num_innermost_template_parms (tmpl)
5952 != num_innermost_template_parms (underlying)))
5953 break;
5955 tree alias_args = INNERMOST_TEMPLATE_ARGS
5956 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5957 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
5958 break;
5960 /* Alias is equivalent. Strip it and repeat. */
5961 tmpl = underlying;
5964 return tmpl;
5967 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5968 must be a reference-to-function or a pointer-to-function type, as specified
5969 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5970 and check that the resulting function has external linkage. */
5972 static tree
5973 convert_nontype_argument_function (tree type, tree expr,
5974 tsubst_flags_t complain)
5976 tree fns = expr;
5977 tree fn, fn_no_ptr;
5978 linkage_kind linkage;
5980 fn = instantiate_type (type, fns, tf_none);
5981 if (fn == error_mark_node)
5982 return error_mark_node;
5984 if (value_dependent_expression_p (fn))
5985 return fn;
5987 fn_no_ptr = strip_fnptr_conv (fn);
5988 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5989 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5990 if (BASELINK_P (fn_no_ptr))
5991 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5993 /* [temp.arg.nontype]/1
5995 A template-argument for a non-type, non-template template-parameter
5996 shall be one of:
5997 [...]
5998 -- the address of an object or function with external [C++11: or
5999 internal] linkage. */
6001 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6003 if (complain & tf_error)
6005 error ("%qE is not a valid template argument for type %qT",
6006 expr, type);
6007 if (TYPE_PTR_P (type))
6008 inform (input_location, "it must be the address of a function "
6009 "with external linkage");
6010 else
6011 inform (input_location, "it must be the name of a function with "
6012 "external linkage");
6014 return NULL_TREE;
6017 linkage = decl_linkage (fn_no_ptr);
6018 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6020 if (complain & tf_error)
6022 if (cxx_dialect >= cxx11)
6023 error ("%qE is not a valid template argument for type %qT "
6024 "because %qD has no linkage",
6025 expr, type, fn_no_ptr);
6026 else
6027 error ("%qE is not a valid template argument for type %qT "
6028 "because %qD does not have external linkage",
6029 expr, type, fn_no_ptr);
6031 return NULL_TREE;
6034 if (TREE_CODE (type) == REFERENCE_TYPE)
6035 fn = build_address (fn);
6036 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6037 fn = build_nop (type, fn);
6039 return fn;
6042 /* Subroutine of convert_nontype_argument.
6043 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6044 Emit an error otherwise. */
6046 static bool
6047 check_valid_ptrmem_cst_expr (tree type, tree expr,
6048 tsubst_flags_t complain)
6050 STRIP_NOPS (expr);
6051 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
6052 return true;
6053 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6054 return true;
6055 if (processing_template_decl
6056 && TREE_CODE (expr) == ADDR_EXPR
6057 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6058 return true;
6059 if (complain & tf_error)
6061 error ("%qE is not a valid template argument for type %qT",
6062 expr, type);
6063 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6065 return false;
6068 /* Returns TRUE iff the address of OP is value-dependent.
6070 14.6.2.4 [temp.dep.temp]:
6071 A non-integral non-type template-argument is dependent if its type is
6072 dependent or it has either of the following forms
6073 qualified-id
6074 & qualified-id
6075 and contains a nested-name-specifier which specifies a class-name that
6076 names a dependent type.
6078 We generalize this to just say that the address of a member of a
6079 dependent class is value-dependent; the above doesn't cover the
6080 address of a static data member named with an unqualified-id. */
6082 static bool
6083 has_value_dependent_address (tree op)
6085 /* We could use get_inner_reference here, but there's no need;
6086 this is only relevant for template non-type arguments, which
6087 can only be expressed as &id-expression. */
6088 if (DECL_P (op))
6090 tree ctx = CP_DECL_CONTEXT (op);
6091 if (TYPE_P (ctx) && dependent_type_p (ctx))
6092 return true;
6095 return false;
6098 /* The next set of functions are used for providing helpful explanatory
6099 diagnostics for failed overload resolution. Their messages should be
6100 indented by two spaces for consistency with the messages in
6101 call.c */
6103 static int
6104 unify_success (bool /*explain_p*/)
6106 return 0;
6109 static int
6110 unify_parameter_deduction_failure (bool explain_p, tree parm)
6112 if (explain_p)
6113 inform (input_location,
6114 " couldn't deduce template parameter %qD", parm);
6115 return 1;
6118 static int
6119 unify_invalid (bool /*explain_p*/)
6121 return 1;
6124 static int
6125 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6127 if (explain_p)
6128 inform (input_location,
6129 " types %qT and %qT have incompatible cv-qualifiers",
6130 parm, arg);
6131 return 1;
6134 static int
6135 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6137 if (explain_p)
6138 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6139 return 1;
6142 static int
6143 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6145 if (explain_p)
6146 inform (input_location,
6147 " template parameter %qD is not a parameter pack, but "
6148 "argument %qD is",
6149 parm, arg);
6150 return 1;
6153 static int
6154 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6156 if (explain_p)
6157 inform (input_location,
6158 " template argument %qE does not match "
6159 "pointer-to-member constant %qE",
6160 arg, parm);
6161 return 1;
6164 static int
6165 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6167 if (explain_p)
6168 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6169 return 1;
6172 static int
6173 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6175 if (explain_p)
6176 inform (input_location,
6177 " inconsistent parameter pack deduction with %qT and %qT",
6178 old_arg, new_arg);
6179 return 1;
6182 static int
6183 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6185 if (explain_p)
6187 if (TYPE_P (parm))
6188 inform (input_location,
6189 " deduced conflicting types for parameter %qT (%qT and %qT)",
6190 parm, first, second);
6191 else
6192 inform (input_location,
6193 " deduced conflicting values for non-type parameter "
6194 "%qE (%qE and %qE)", parm, first, second);
6196 return 1;
6199 static int
6200 unify_vla_arg (bool explain_p, tree arg)
6202 if (explain_p)
6203 inform (input_location,
6204 " variable-sized array type %qT is not "
6205 "a valid template argument",
6206 arg);
6207 return 1;
6210 static int
6211 unify_method_type_error (bool explain_p, tree arg)
6213 if (explain_p)
6214 inform (input_location,
6215 " member function type %qT is not a valid template argument",
6216 arg);
6217 return 1;
6220 static int
6221 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6223 if (explain_p)
6225 if (least_p)
6226 inform_n (input_location, wanted,
6227 " candidate expects at least %d argument, %d provided",
6228 " candidate expects at least %d arguments, %d provided",
6229 wanted, have);
6230 else
6231 inform_n (input_location, wanted,
6232 " candidate expects %d argument, %d provided",
6233 " candidate expects %d arguments, %d provided",
6234 wanted, have);
6236 return 1;
6239 static int
6240 unify_too_many_arguments (bool explain_p, int have, int wanted)
6242 return unify_arity (explain_p, have, wanted);
6245 static int
6246 unify_too_few_arguments (bool explain_p, int have, int wanted,
6247 bool least_p = false)
6249 return unify_arity (explain_p, have, wanted, least_p);
6252 static int
6253 unify_arg_conversion (bool explain_p, tree to_type,
6254 tree from_type, tree arg)
6256 if (explain_p)
6257 inform (EXPR_LOC_OR_LOC (arg, input_location),
6258 " cannot convert %qE (type %qT) to type %qT",
6259 arg, from_type, to_type);
6260 return 1;
6263 static int
6264 unify_no_common_base (bool explain_p, enum template_base_result r,
6265 tree parm, tree arg)
6267 if (explain_p)
6268 switch (r)
6270 case tbr_ambiguous_baseclass:
6271 inform (input_location, " %qT is an ambiguous base class of %qT",
6272 parm, arg);
6273 break;
6274 default:
6275 inform (input_location, " %qT is not derived from %qT", arg, parm);
6276 break;
6278 return 1;
6281 static int
6282 unify_inconsistent_template_template_parameters (bool explain_p)
6284 if (explain_p)
6285 inform (input_location,
6286 " template parameters of a template template argument are "
6287 "inconsistent with other deduced template arguments");
6288 return 1;
6291 static int
6292 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6294 if (explain_p)
6295 inform (input_location,
6296 " can't deduce a template for %qT from non-template type %qT",
6297 parm, arg);
6298 return 1;
6301 static int
6302 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6304 if (explain_p)
6305 inform (input_location,
6306 " template argument %qE does not match %qE", arg, parm);
6307 return 1;
6310 static int
6311 unify_overload_resolution_failure (bool explain_p, tree arg)
6313 if (explain_p)
6314 inform (input_location,
6315 " could not resolve address from overloaded function %qE",
6316 arg);
6317 return 1;
6320 /* Attempt to convert the non-type template parameter EXPR to the
6321 indicated TYPE. If the conversion is successful, return the
6322 converted value. If the conversion is unsuccessful, return
6323 NULL_TREE if we issued an error message, or error_mark_node if we
6324 did not. We issue error messages for out-and-out bad template
6325 parameters, but not simply because the conversion failed, since we
6326 might be just trying to do argument deduction. Both TYPE and EXPR
6327 must be non-dependent.
6329 The conversion follows the special rules described in
6330 [temp.arg.nontype], and it is much more strict than an implicit
6331 conversion.
6333 This function is called twice for each template argument (see
6334 lookup_template_class for a more accurate description of this
6335 problem). This means that we need to handle expressions which
6336 are not valid in a C++ source, but can be created from the
6337 first call (for instance, casts to perform conversions). These
6338 hacks can go away after we fix the double coercion problem. */
6340 static tree
6341 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6343 tree expr_type;
6345 /* Detect immediately string literals as invalid non-type argument.
6346 This special-case is not needed for correctness (we would easily
6347 catch this later), but only to provide better diagnostic for this
6348 common user mistake. As suggested by DR 100, we do not mention
6349 linkage issues in the diagnostic as this is not the point. */
6350 /* FIXME we're making this OK. */
6351 if (TREE_CODE (expr) == STRING_CST)
6353 if (complain & tf_error)
6354 error ("%qE is not a valid template argument for type %qT "
6355 "because string literals can never be used in this context",
6356 expr, type);
6357 return NULL_TREE;
6360 /* Add the ADDR_EXPR now for the benefit of
6361 value_dependent_expression_p. */
6362 if (TYPE_PTROBV_P (type)
6363 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6365 expr = decay_conversion (expr, complain);
6366 if (expr == error_mark_node)
6367 return error_mark_node;
6370 /* If we are in a template, EXPR may be non-dependent, but still
6371 have a syntactic, rather than semantic, form. For example, EXPR
6372 might be a SCOPE_REF, rather than the VAR_DECL to which the
6373 SCOPE_REF refers. Preserving the qualifying scope is necessary
6374 so that access checking can be performed when the template is
6375 instantiated -- but here we need the resolved form so that we can
6376 convert the argument. */
6377 bool non_dep = false;
6378 if (TYPE_REF_OBJ_P (type)
6379 && has_value_dependent_address (expr))
6380 /* If we want the address and it's value-dependent, don't fold. */;
6381 else if (processing_template_decl
6382 && potential_nondependent_constant_expression (expr))
6383 non_dep = true;
6384 if (error_operand_p (expr))
6385 return error_mark_node;
6386 expr_type = TREE_TYPE (expr);
6387 if (TREE_CODE (type) == REFERENCE_TYPE)
6388 expr = mark_lvalue_use (expr);
6389 else
6390 expr = mark_rvalue_use (expr);
6392 /* If the argument is non-dependent, perform any conversions in
6393 non-dependent context as well. */
6394 processing_template_decl_sentinel s (non_dep);
6395 if (non_dep)
6396 expr = instantiate_non_dependent_expr_internal (expr, complain);
6398 if (value_dependent_expression_p (expr))
6399 expr = canonicalize_expr_argument (expr, complain);
6401 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6402 to a non-type argument of "nullptr". */
6403 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6404 expr = fold_simple (convert (type, expr));
6406 /* In C++11, integral or enumeration non-type template arguments can be
6407 arbitrary constant expressions. Pointer and pointer to
6408 member arguments can be general constant expressions that evaluate
6409 to a null value, but otherwise still need to be of a specific form. */
6410 if (cxx_dialect >= cxx11)
6412 if (TREE_CODE (expr) == PTRMEM_CST)
6413 /* A PTRMEM_CST is already constant, and a valid template
6414 argument for a parameter of pointer to member type, we just want
6415 to leave it in that form rather than lower it to a
6416 CONSTRUCTOR. */;
6417 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6418 /* Constant value checking is done later with type conversion. */;
6419 else if (cxx_dialect >= cxx1z)
6421 if (TREE_CODE (type) != REFERENCE_TYPE)
6422 expr = maybe_constant_value (expr);
6423 else if (REFERENCE_REF_P (expr))
6425 expr = TREE_OPERAND (expr, 0);
6426 expr = maybe_constant_value (expr);
6427 expr = convert_from_reference (expr);
6430 else if (TYPE_PTR_OR_PTRMEM_P (type))
6432 tree folded = maybe_constant_value (expr);
6433 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6434 : null_member_pointer_value_p (folded))
6435 expr = folded;
6439 /* HACK: Due to double coercion, we can get a
6440 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6441 which is the tree that we built on the first call (see
6442 below when coercing to reference to object or to reference to
6443 function). We just strip everything and get to the arg.
6444 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6445 for examples. */
6446 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6448 tree probe_type, probe = expr;
6449 if (REFERENCE_REF_P (probe))
6450 probe = TREE_OPERAND (probe, 0);
6451 probe_type = TREE_TYPE (probe);
6452 if (TREE_CODE (probe) == NOP_EXPR)
6454 /* ??? Maybe we could use convert_from_reference here, but we
6455 would need to relax its constraints because the NOP_EXPR
6456 could actually change the type to something more cv-qualified,
6457 and this is not folded by convert_from_reference. */
6458 tree addr = TREE_OPERAND (probe, 0);
6459 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6460 && TREE_CODE (addr) == ADDR_EXPR
6461 && TYPE_PTR_P (TREE_TYPE (addr))
6462 && (same_type_ignoring_top_level_qualifiers_p
6463 (TREE_TYPE (probe_type),
6464 TREE_TYPE (TREE_TYPE (addr)))))
6466 expr = TREE_OPERAND (addr, 0);
6467 expr_type = TREE_TYPE (probe_type);
6472 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6473 parameter is a pointer to object, through decay and
6474 qualification conversion. Let's strip everything. */
6475 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6477 tree probe = expr;
6478 STRIP_NOPS (probe);
6479 if (TREE_CODE (probe) == ADDR_EXPR
6480 && TYPE_PTR_P (TREE_TYPE (probe)))
6482 /* Skip the ADDR_EXPR only if it is part of the decay for
6483 an array. Otherwise, it is part of the original argument
6484 in the source code. */
6485 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6486 probe = TREE_OPERAND (probe, 0);
6487 expr = probe;
6488 expr_type = TREE_TYPE (expr);
6492 /* [temp.arg.nontype]/5, bullet 1
6494 For a non-type template-parameter of integral or enumeration type,
6495 integral promotions (_conv.prom_) and integral conversions
6496 (_conv.integral_) are applied. */
6497 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6499 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6500 t = maybe_constant_value (t);
6501 if (t != error_mark_node)
6502 expr = t;
6504 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6505 return error_mark_node;
6507 /* Notice that there are constant expressions like '4 % 0' which
6508 do not fold into integer constants. */
6509 if (TREE_CODE (expr) != INTEGER_CST
6510 && !value_dependent_expression_p (expr))
6512 if (complain & tf_error)
6514 int errs = errorcount, warns = warningcount + werrorcount;
6515 if (processing_template_decl
6516 && !require_potential_constant_expression (expr))
6517 return NULL_TREE;
6518 expr = cxx_constant_value (expr);
6519 if (errorcount > errs || warningcount + werrorcount > warns)
6520 inform (EXPR_LOC_OR_LOC (expr, input_location),
6521 "in template argument for type %qT ", type);
6522 if (expr == error_mark_node)
6523 return NULL_TREE;
6524 /* else cxx_constant_value complained but gave us
6525 a real constant, so go ahead. */
6526 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6528 else
6529 return NULL_TREE;
6532 /* Avoid typedef problems. */
6533 if (TREE_TYPE (expr) != type)
6534 expr = fold_convert (type, expr);
6536 /* [temp.arg.nontype]/5, bullet 2
6538 For a non-type template-parameter of type pointer to object,
6539 qualification conversions (_conv.qual_) and the array-to-pointer
6540 conversion (_conv.array_) are applied. */
6541 else if (TYPE_PTROBV_P (type))
6543 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6545 A template-argument for a non-type, non-template template-parameter
6546 shall be one of: [...]
6548 -- the name of a non-type template-parameter;
6549 -- the address of an object or function with external linkage, [...]
6550 expressed as "& id-expression" where the & is optional if the name
6551 refers to a function or array, or if the corresponding
6552 template-parameter is a reference.
6554 Here, we do not care about functions, as they are invalid anyway
6555 for a parameter of type pointer-to-object. */
6557 if (value_dependent_expression_p (expr))
6558 /* Non-type template parameters are OK. */
6560 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6561 /* Null pointer values are OK in C++11. */;
6562 else if (TREE_CODE (expr) != ADDR_EXPR
6563 && TREE_CODE (expr_type) != ARRAY_TYPE)
6565 if (VAR_P (expr))
6567 if (complain & tf_error)
6568 error ("%qD is not a valid template argument "
6569 "because %qD is a variable, not the address of "
6570 "a variable", expr, expr);
6571 return NULL_TREE;
6573 if (POINTER_TYPE_P (expr_type))
6575 if (complain & tf_error)
6576 error ("%qE is not a valid template argument for %qT "
6577 "because it is not the address of a variable",
6578 expr, type);
6579 return NULL_TREE;
6581 /* Other values, like integer constants, might be valid
6582 non-type arguments of some other type. */
6583 return error_mark_node;
6585 else
6587 tree decl;
6589 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6590 ? TREE_OPERAND (expr, 0) : expr);
6591 if (!VAR_P (decl))
6593 if (complain & tf_error)
6594 error ("%qE is not a valid template argument of type %qT "
6595 "because %qE is not a variable", expr, type, decl);
6596 return NULL_TREE;
6598 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6600 if (complain & tf_error)
6601 error ("%qE is not a valid template argument of type %qT "
6602 "because %qD does not have external linkage",
6603 expr, type, decl);
6604 return NULL_TREE;
6606 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6608 if (complain & tf_error)
6609 error ("%qE is not a valid template argument of type %qT "
6610 "because %qD has no linkage", expr, type, decl);
6611 return NULL_TREE;
6615 expr = decay_conversion (expr, complain);
6616 if (expr == error_mark_node)
6617 return error_mark_node;
6619 expr = perform_qualification_conversions (type, expr);
6620 if (expr == error_mark_node)
6621 return error_mark_node;
6623 /* [temp.arg.nontype]/5, bullet 3
6625 For a non-type template-parameter of type reference to object, no
6626 conversions apply. The type referred to by the reference may be more
6627 cv-qualified than the (otherwise identical) type of the
6628 template-argument. The template-parameter is bound directly to the
6629 template-argument, which must be an lvalue. */
6630 else if (TYPE_REF_OBJ_P (type))
6632 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6633 expr_type))
6634 return error_mark_node;
6636 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6638 if (complain & tf_error)
6639 error ("%qE is not a valid template argument for type %qT "
6640 "because of conflicts in cv-qualification", expr, type);
6641 return NULL_TREE;
6644 if (!lvalue_p (expr))
6646 if (complain & tf_error)
6647 error ("%qE is not a valid template argument for type %qT "
6648 "because it is not an lvalue", expr, type);
6649 return NULL_TREE;
6652 /* [temp.arg.nontype]/1
6654 A template-argument for a non-type, non-template template-parameter
6655 shall be one of: [...]
6657 -- the address of an object or function with external linkage. */
6658 if (INDIRECT_REF_P (expr)
6659 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6661 expr = TREE_OPERAND (expr, 0);
6662 if (DECL_P (expr))
6664 if (complain & tf_error)
6665 error ("%q#D is not a valid template argument for type %qT "
6666 "because a reference variable does not have a constant "
6667 "address", expr, type);
6668 return NULL_TREE;
6672 if (!value_dependent_expression_p (expr))
6674 if (!DECL_P (expr))
6676 if (complain & tf_error)
6677 error ("%qE is not a valid template argument for type %qT "
6678 "because it is not an object with linkage",
6679 expr, type);
6680 return NULL_TREE;
6683 /* DR 1155 allows internal linkage in C++11 and up. */
6684 linkage_kind linkage = decl_linkage (expr);
6685 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6687 if (complain & tf_error)
6688 error ("%qE is not a valid template argument for type %qT "
6689 "because object %qD does not have linkage",
6690 expr, type, expr);
6691 return NULL_TREE;
6694 expr = build_nop (type, build_address (expr));
6697 /* [temp.arg.nontype]/5, bullet 4
6699 For a non-type template-parameter of type pointer to function, only
6700 the function-to-pointer conversion (_conv.func_) is applied. If the
6701 template-argument represents a set of overloaded functions (or a
6702 pointer to such), the matching function is selected from the set
6703 (_over.over_). */
6704 else if (TYPE_PTRFN_P (type))
6706 /* If the argument is a template-id, we might not have enough
6707 context information to decay the pointer. */
6708 if (!type_unknown_p (expr_type))
6710 expr = decay_conversion (expr, complain);
6711 if (expr == error_mark_node)
6712 return error_mark_node;
6715 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6716 /* Null pointer values are OK in C++11. */
6717 return perform_qualification_conversions (type, expr);
6719 expr = convert_nontype_argument_function (type, expr, complain);
6720 if (!expr || expr == error_mark_node)
6721 return expr;
6723 /* [temp.arg.nontype]/5, bullet 5
6725 For a non-type template-parameter of type reference to function, no
6726 conversions apply. If the template-argument represents a set of
6727 overloaded functions, the matching function is selected from the set
6728 (_over.over_). */
6729 else if (TYPE_REFFN_P (type))
6731 if (TREE_CODE (expr) == ADDR_EXPR)
6733 if (complain & tf_error)
6735 error ("%qE is not a valid template argument for type %qT "
6736 "because it is a pointer", expr, type);
6737 inform (input_location, "try using %qE instead",
6738 TREE_OPERAND (expr, 0));
6740 return NULL_TREE;
6743 expr = convert_nontype_argument_function (type, expr, complain);
6744 if (!expr || expr == error_mark_node)
6745 return expr;
6747 /* [temp.arg.nontype]/5, bullet 6
6749 For a non-type template-parameter of type pointer to member function,
6750 no conversions apply. If the template-argument represents a set of
6751 overloaded member functions, the matching member function is selected
6752 from the set (_over.over_). */
6753 else if (TYPE_PTRMEMFUNC_P (type))
6755 expr = instantiate_type (type, expr, tf_none);
6756 if (expr == error_mark_node)
6757 return error_mark_node;
6759 /* [temp.arg.nontype] bullet 1 says the pointer to member
6760 expression must be a pointer-to-member constant. */
6761 if (!value_dependent_expression_p (expr)
6762 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6763 return error_mark_node;
6765 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6766 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6767 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6768 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6770 /* There is no way to disable standard conversions in
6771 resolve_address_of_overloaded_function (called by
6772 instantiate_type). It is possible that the call succeeded by
6773 converting &B::I to &D::I (where B is a base of D), so we need
6774 to reject this conversion here.
6776 Actually, even if there was a way to disable standard conversions,
6777 it would still be better to reject them here so that we can
6778 provide a superior diagnostic. */
6779 if (!same_type_p (TREE_TYPE (expr), type))
6781 if (complain & tf_error)
6783 error ("%qE is not a valid template argument for type %qT "
6784 "because it is of type %qT", expr, type,
6785 TREE_TYPE (expr));
6786 /* If we are just one standard conversion off, explain. */
6787 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6788 inform (input_location,
6789 "standard conversions are not allowed in this context");
6791 return NULL_TREE;
6794 /* [temp.arg.nontype]/5, bullet 7
6796 For a non-type template-parameter of type pointer to data member,
6797 qualification conversions (_conv.qual_) are applied. */
6798 else if (TYPE_PTRDATAMEM_P (type))
6800 /* [temp.arg.nontype] bullet 1 says the pointer to member
6801 expression must be a pointer-to-member constant. */
6802 if (!value_dependent_expression_p (expr)
6803 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6804 return error_mark_node;
6806 expr = perform_qualification_conversions (type, expr);
6807 if (expr == error_mark_node)
6808 return expr;
6810 else if (NULLPTR_TYPE_P (type))
6812 if (expr != nullptr_node)
6814 if (complain & tf_error)
6815 error ("%qE is not a valid template argument for type %qT "
6816 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6817 return NULL_TREE;
6819 return expr;
6821 /* A template non-type parameter must be one of the above. */
6822 else
6823 gcc_unreachable ();
6825 /* Sanity check: did we actually convert the argument to the
6826 right type? */
6827 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6828 (type, TREE_TYPE (expr)));
6829 return convert_from_reference (expr);
6832 /* Subroutine of coerce_template_template_parms, which returns 1 if
6833 PARM_PARM and ARG_PARM match using the rule for the template
6834 parameters of template template parameters. Both PARM and ARG are
6835 template parameters; the rest of the arguments are the same as for
6836 coerce_template_template_parms.
6838 static int
6839 coerce_template_template_parm (tree parm,
6840 tree arg,
6841 tsubst_flags_t complain,
6842 tree in_decl,
6843 tree outer_args)
6845 if (arg == NULL_TREE || error_operand_p (arg)
6846 || parm == NULL_TREE || error_operand_p (parm))
6847 return 0;
6849 if (TREE_CODE (arg) != TREE_CODE (parm))
6850 return 0;
6852 switch (TREE_CODE (parm))
6854 case TEMPLATE_DECL:
6855 /* We encounter instantiations of templates like
6856 template <template <template <class> class> class TT>
6857 class C; */
6859 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6860 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6862 if (!coerce_template_template_parms
6863 (parmparm, argparm, complain, in_decl, outer_args))
6864 return 0;
6866 /* Fall through. */
6868 case TYPE_DECL:
6869 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6870 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6871 /* Argument is a parameter pack but parameter is not. */
6872 return 0;
6873 break;
6875 case PARM_DECL:
6876 /* The tsubst call is used to handle cases such as
6878 template <int> class C {};
6879 template <class T, template <T> class TT> class D {};
6880 D<int, C> d;
6882 i.e. the parameter list of TT depends on earlier parameters. */
6883 if (!uses_template_parms (TREE_TYPE (arg)))
6885 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6886 if (!uses_template_parms (t)
6887 && !same_type_p (t, TREE_TYPE (arg)))
6888 return 0;
6891 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6892 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6893 /* Argument is a parameter pack but parameter is not. */
6894 return 0;
6896 break;
6898 default:
6899 gcc_unreachable ();
6902 return 1;
6905 /* Coerce template argument list ARGLIST for use with template
6906 template-parameter TEMPL. */
6908 static tree
6909 coerce_template_args_for_ttp (tree templ, tree arglist,
6910 tsubst_flags_t complain)
6912 /* Consider an example where a template template parameter declared as
6914 template <class T, class U = std::allocator<T> > class TT
6916 The template parameter level of T and U are one level larger than
6917 of TT. To proper process the default argument of U, say when an
6918 instantiation `TT<int>' is seen, we need to build the full
6919 arguments containing {int} as the innermost level. Outer levels,
6920 available when not appearing as default template argument, can be
6921 obtained from the arguments of the enclosing template.
6923 Suppose that TT is later substituted with std::vector. The above
6924 instantiation is `TT<int, std::allocator<T> >' with TT at
6925 level 1, and T at level 2, while the template arguments at level 1
6926 becomes {std::vector} and the inner level 2 is {int}. */
6928 tree outer = DECL_CONTEXT (templ);
6929 if (outer)
6931 if (DECL_TEMPLATE_SPECIALIZATION (outer))
6932 /* We want arguments for the partial specialization, not arguments for
6933 the primary template. */
6934 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
6935 else
6936 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6938 else if (current_template_parms)
6940 /* This is an argument of the current template, so we haven't set
6941 DECL_CONTEXT yet. */
6942 tree relevant_template_parms;
6944 /* Parameter levels that are greater than the level of the given
6945 template template parm are irrelevant. */
6946 relevant_template_parms = current_template_parms;
6947 while (TMPL_PARMS_DEPTH (relevant_template_parms)
6948 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
6949 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
6951 outer = template_parms_to_args (relevant_template_parms);
6954 if (outer)
6955 arglist = add_to_template_args (outer, arglist);
6957 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6958 return coerce_template_parms (parmlist, arglist, templ,
6959 complain,
6960 /*require_all_args=*/true,
6961 /*use_default_args=*/true);
6964 /* A cache of template template parameters with match-all default
6965 arguments. */
6966 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
6967 static void
6968 store_defaulted_ttp (tree v, tree t)
6970 if (!defaulted_ttp_cache)
6971 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
6972 defaulted_ttp_cache->put (v, t);
6974 static tree
6975 lookup_defaulted_ttp (tree v)
6977 if (defaulted_ttp_cache)
6978 if (tree *p = defaulted_ttp_cache->get (v))
6979 return *p;
6980 return NULL_TREE;
6983 /* T is a bound template template-parameter. Copy its arguments into default
6984 arguments of the template template-parameter's template parameters. */
6986 static tree
6987 add_defaults_to_ttp (tree otmpl)
6989 if (tree c = lookup_defaulted_ttp (otmpl))
6990 return c;
6992 tree ntmpl = copy_node (otmpl);
6994 tree ntype = copy_node (TREE_TYPE (otmpl));
6995 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
6996 TYPE_MAIN_VARIANT (ntype) = ntype;
6997 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
6998 TYPE_NAME (ntype) = ntmpl;
6999 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7001 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7002 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7003 TEMPLATE_PARM_DECL (idx) = ntmpl;
7004 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7006 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7007 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7008 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7009 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7010 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7012 tree o = TREE_VEC_ELT (vec, i);
7013 if (!template_parameter_pack_p (TREE_VALUE (o)))
7015 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7016 TREE_PURPOSE (n) = any_targ_node;
7020 store_defaulted_ttp (otmpl, ntmpl);
7021 return ntmpl;
7024 /* ARG is a bound potential template template-argument, and PARGS is a list
7025 of arguments for the corresponding template template-parameter. Adjust
7026 PARGS as appropriate for application to ARG's template, and if ARG is a
7027 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7028 arguments to the template template parameter. */
7030 static tree
7031 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7033 ++processing_template_decl;
7034 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7035 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7037 /* When comparing two template template-parameters in partial ordering,
7038 rewrite the one currently being used as an argument to have default
7039 arguments for all parameters. */
7040 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7041 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7042 if (pargs != error_mark_node)
7043 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7044 TYPE_TI_ARGS (arg));
7046 else
7048 tree aparms
7049 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7050 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7051 /*require_all*/true,
7052 /*use_default*/true);
7054 --processing_template_decl;
7055 return pargs;
7058 /* Subroutine of unify for the case when PARM is a
7059 BOUND_TEMPLATE_TEMPLATE_PARM. */
7061 static int
7062 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7063 bool explain_p)
7065 tree parmvec = TYPE_TI_ARGS (parm);
7066 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7068 /* The template template parm might be variadic and the argument
7069 not, so flatten both argument lists. */
7070 parmvec = expand_template_argument_pack (parmvec);
7071 argvec = expand_template_argument_pack (argvec);
7073 tree nparmvec = parmvec;
7074 if (flag_new_ttp)
7076 /* In keeping with P0522R0, adjust P's template arguments
7077 to apply to A's template; then flatten it again. */
7078 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7079 nparmvec = expand_template_argument_pack (nparmvec);
7082 if (unify (tparms, targs, nparmvec, argvec,
7083 UNIFY_ALLOW_NONE, explain_p))
7084 return 1;
7086 /* If the P0522 adjustment eliminated a pack expansion, deduce
7087 empty packs. */
7088 if (flag_new_ttp
7089 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7090 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7091 DEDUCE_EXACT, /*sub*/true, explain_p))
7092 return 1;
7094 return 0;
7097 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7098 template template parameters. Both PARM_PARMS and ARG_PARMS are
7099 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7100 or PARM_DECL.
7102 Consider the example:
7103 template <class T> class A;
7104 template<template <class U> class TT> class B;
7106 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7107 the parameters to A, and OUTER_ARGS contains A. */
7109 static int
7110 coerce_template_template_parms (tree parm_parms,
7111 tree arg_parms,
7112 tsubst_flags_t complain,
7113 tree in_decl,
7114 tree outer_args)
7116 int nparms, nargs, i;
7117 tree parm, arg;
7118 int variadic_p = 0;
7120 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7121 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7123 nparms = TREE_VEC_LENGTH (parm_parms);
7124 nargs = TREE_VEC_LENGTH (arg_parms);
7126 if (flag_new_ttp)
7128 /* P0522R0: A template template-parameter P is at least as specialized as
7129 a template template-argument A if, given the following rewrite to two
7130 function templates, the function template corresponding to P is at
7131 least as specialized as the function template corresponding to A
7132 according to the partial ordering rules for function templates
7133 ([temp.func.order]). Given an invented class template X with the
7134 template parameter list of A (including default arguments):
7136 * Each of the two function templates has the same template parameters,
7137 respectively, as P or A.
7139 * Each function template has a single function parameter whose type is
7140 a specialization of X with template arguments corresponding to the
7141 template parameters from the respective function template where, for
7142 each template parameter PP in the template parameter list of the
7143 function template, a corresponding template argument AA is formed. If
7144 PP declares a parameter pack, then AA is the pack expansion
7145 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7147 If the rewrite produces an invalid type, then P is not at least as
7148 specialized as A. */
7150 /* So coerce P's args to apply to A's parms, and then deduce between A's
7151 args and the converted args. If that succeeds, A is at least as
7152 specialized as P, so they match.*/
7153 tree pargs = template_parms_level_to_args (parm_parms);
7154 ++processing_template_decl;
7155 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7156 /*require_all*/true, /*use_default*/true);
7157 --processing_template_decl;
7158 if (pargs != error_mark_node)
7160 tree targs = make_tree_vec (nargs);
7161 tree aargs = template_parms_level_to_args (arg_parms);
7162 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7163 /*explain*/false))
7164 return 1;
7168 /* Determine whether we have a parameter pack at the end of the
7169 template template parameter's template parameter list. */
7170 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7172 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7174 if (error_operand_p (parm))
7175 return 0;
7177 switch (TREE_CODE (parm))
7179 case TEMPLATE_DECL:
7180 case TYPE_DECL:
7181 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7182 variadic_p = 1;
7183 break;
7185 case PARM_DECL:
7186 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7187 variadic_p = 1;
7188 break;
7190 default:
7191 gcc_unreachable ();
7195 if (nargs != nparms
7196 && !(variadic_p && nargs >= nparms - 1))
7197 return 0;
7199 /* Check all of the template parameters except the parameter pack at
7200 the end (if any). */
7201 for (i = 0; i < nparms - variadic_p; ++i)
7203 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7204 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7205 continue;
7207 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7208 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7210 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7211 outer_args))
7212 return 0;
7216 if (variadic_p)
7218 /* Check each of the template parameters in the template
7219 argument against the template parameter pack at the end of
7220 the template template parameter. */
7221 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7222 return 0;
7224 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7226 for (; i < nargs; ++i)
7228 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7229 continue;
7231 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7233 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7234 outer_args))
7235 return 0;
7239 return 1;
7242 /* Verifies that the deduced template arguments (in TARGS) for the
7243 template template parameters (in TPARMS) represent valid bindings,
7244 by comparing the template parameter list of each template argument
7245 to the template parameter list of its corresponding template
7246 template parameter, in accordance with DR150. This
7247 routine can only be called after all template arguments have been
7248 deduced. It will return TRUE if all of the template template
7249 parameter bindings are okay, FALSE otherwise. */
7250 bool
7251 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7253 int i, ntparms = TREE_VEC_LENGTH (tparms);
7254 bool ret = true;
7256 /* We're dealing with template parms in this process. */
7257 ++processing_template_decl;
7259 targs = INNERMOST_TEMPLATE_ARGS (targs);
7261 for (i = 0; i < ntparms; ++i)
7263 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7264 tree targ = TREE_VEC_ELT (targs, i);
7266 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7268 tree packed_args = NULL_TREE;
7269 int idx, len = 1;
7271 if (ARGUMENT_PACK_P (targ))
7273 /* Look inside the argument pack. */
7274 packed_args = ARGUMENT_PACK_ARGS (targ);
7275 len = TREE_VEC_LENGTH (packed_args);
7278 for (idx = 0; idx < len; ++idx)
7280 tree targ_parms = NULL_TREE;
7282 if (packed_args)
7283 /* Extract the next argument from the argument
7284 pack. */
7285 targ = TREE_VEC_ELT (packed_args, idx);
7287 if (PACK_EXPANSION_P (targ))
7288 /* Look at the pattern of the pack expansion. */
7289 targ = PACK_EXPANSION_PATTERN (targ);
7291 /* Extract the template parameters from the template
7292 argument. */
7293 if (TREE_CODE (targ) == TEMPLATE_DECL)
7294 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7295 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7296 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7298 /* Verify that we can coerce the template template
7299 parameters from the template argument to the template
7300 parameter. This requires an exact match. */
7301 if (targ_parms
7302 && !coerce_template_template_parms
7303 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7304 targ_parms,
7305 tf_none,
7306 tparm,
7307 targs))
7309 ret = false;
7310 goto out;
7316 out:
7318 --processing_template_decl;
7319 return ret;
7322 /* Since type attributes aren't mangled, we need to strip them from
7323 template type arguments. */
7325 static tree
7326 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7328 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7329 return arg;
7330 bool removed_attributes = false;
7331 tree canon = strip_typedefs (arg, &removed_attributes);
7332 if (removed_attributes
7333 && (complain & tf_warning))
7334 warning (OPT_Wignored_attributes,
7335 "ignoring attributes on template argument %qT", arg);
7336 return canon;
7339 /* And from inside dependent non-type arguments like sizeof(Type). */
7341 static tree
7342 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7344 if (!arg || arg == error_mark_node)
7345 return arg;
7346 bool removed_attributes = false;
7347 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7348 if (removed_attributes
7349 && (complain & tf_warning))
7350 warning (OPT_Wignored_attributes,
7351 "ignoring attributes in template argument %qE", arg);
7352 return canon;
7355 // A template declaration can be substituted for a constrained
7356 // template template parameter only when the argument is more
7357 // constrained than the parameter.
7358 static bool
7359 is_compatible_template_arg (tree parm, tree arg)
7361 tree parm_cons = get_constraints (parm);
7363 /* For now, allow constrained template template arguments
7364 and unconstrained template template parameters. */
7365 if (parm_cons == NULL_TREE)
7366 return true;
7368 tree arg_cons = get_constraints (arg);
7370 // If the template parameter is constrained, we need to rewrite its
7371 // constraints in terms of the ARG's template parameters. This ensures
7372 // that all of the template parameter types will have the same depth.
7374 // Note that this is only valid when coerce_template_template_parm is
7375 // true for the innermost template parameters of PARM and ARG. In other
7376 // words, because coercion is successful, this conversion will be valid.
7377 if (parm_cons)
7379 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7380 parm_cons = tsubst_constraint_info (parm_cons,
7381 INNERMOST_TEMPLATE_ARGS (args),
7382 tf_none, NULL_TREE);
7383 if (parm_cons == error_mark_node)
7384 return false;
7387 return subsumes (parm_cons, arg_cons);
7390 // Convert a placeholder argument into a binding to the original
7391 // parameter. The original parameter is saved as the TREE_TYPE of
7392 // ARG.
7393 static inline tree
7394 convert_wildcard_argument (tree parm, tree arg)
7396 TREE_TYPE (arg) = parm;
7397 return arg;
7400 /* Convert the indicated template ARG as necessary to match the
7401 indicated template PARM. Returns the converted ARG, or
7402 error_mark_node if the conversion was unsuccessful. Error and
7403 warning messages are issued under control of COMPLAIN. This
7404 conversion is for the Ith parameter in the parameter list. ARGS is
7405 the full set of template arguments deduced so far. */
7407 static tree
7408 convert_template_argument (tree parm,
7409 tree arg,
7410 tree args,
7411 tsubst_flags_t complain,
7412 int i,
7413 tree in_decl)
7415 tree orig_arg;
7416 tree val;
7417 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7419 if (parm == error_mark_node)
7420 return error_mark_node;
7422 /* Trivially convert placeholders. */
7423 if (TREE_CODE (arg) == WILDCARD_DECL)
7424 return convert_wildcard_argument (parm, arg);
7426 if (arg == any_targ_node)
7427 return arg;
7429 if (TREE_CODE (arg) == TREE_LIST
7430 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7432 /* The template argument was the name of some
7433 member function. That's usually
7434 invalid, but static members are OK. In any
7435 case, grab the underlying fields/functions
7436 and issue an error later if required. */
7437 orig_arg = TREE_VALUE (arg);
7438 TREE_TYPE (arg) = unknown_type_node;
7441 orig_arg = arg;
7443 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7444 requires_type = (TREE_CODE (parm) == TYPE_DECL
7445 || requires_tmpl_type);
7447 /* When determining whether an argument pack expansion is a template,
7448 look at the pattern. */
7449 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7450 arg = PACK_EXPANSION_PATTERN (arg);
7452 /* Deal with an injected-class-name used as a template template arg. */
7453 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7455 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7456 if (TREE_CODE (t) == TEMPLATE_DECL)
7458 if (cxx_dialect >= cxx11)
7459 /* OK under DR 1004. */;
7460 else if (complain & tf_warning_or_error)
7461 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7462 " used as template template argument", TYPE_NAME (arg));
7463 else if (flag_pedantic_errors)
7464 t = arg;
7466 arg = t;
7470 is_tmpl_type =
7471 ((TREE_CODE (arg) == TEMPLATE_DECL
7472 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7473 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7474 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7475 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7477 if (is_tmpl_type
7478 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7479 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7480 arg = TYPE_STUB_DECL (arg);
7482 is_type = TYPE_P (arg) || is_tmpl_type;
7484 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7485 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7487 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7489 if (complain & tf_error)
7490 error ("invalid use of destructor %qE as a type", orig_arg);
7491 return error_mark_node;
7494 permerror (input_location,
7495 "to refer to a type member of a template parameter, "
7496 "use %<typename %E%>", orig_arg);
7498 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7499 TREE_OPERAND (arg, 1),
7500 typename_type,
7501 complain);
7502 arg = orig_arg;
7503 is_type = 1;
7505 if (is_type != requires_type)
7507 if (in_decl)
7509 if (complain & tf_error)
7511 error ("type/value mismatch at argument %d in template "
7512 "parameter list for %qD",
7513 i + 1, in_decl);
7514 if (is_type)
7515 inform (input_location,
7516 " expected a constant of type %qT, got %qT",
7517 TREE_TYPE (parm),
7518 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7519 else if (requires_tmpl_type)
7520 inform (input_location,
7521 " expected a class template, got %qE", orig_arg);
7522 else
7523 inform (input_location,
7524 " expected a type, got %qE", orig_arg);
7527 return error_mark_node;
7529 if (is_tmpl_type ^ requires_tmpl_type)
7531 if (in_decl && (complain & tf_error))
7533 error ("type/value mismatch at argument %d in template "
7534 "parameter list for %qD",
7535 i + 1, in_decl);
7536 if (is_tmpl_type)
7537 inform (input_location,
7538 " expected a type, got %qT", DECL_NAME (arg));
7539 else
7540 inform (input_location,
7541 " expected a class template, got %qT", orig_arg);
7543 return error_mark_node;
7546 if (is_type)
7548 if (requires_tmpl_type)
7550 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7551 val = orig_arg;
7552 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7553 /* The number of argument required is not known yet.
7554 Just accept it for now. */
7555 val = TREE_TYPE (arg);
7556 else
7558 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7559 tree argparm;
7561 /* Strip alias templates that are equivalent to another
7562 template. */
7563 arg = get_underlying_template (arg);
7564 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7566 if (coerce_template_template_parms (parmparm, argparm,
7567 complain, in_decl,
7568 args))
7570 val = arg;
7572 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7573 TEMPLATE_DECL. */
7574 if (val != error_mark_node)
7576 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7577 val = TREE_TYPE (val);
7578 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7579 val = make_pack_expansion (val);
7582 else
7584 if (in_decl && (complain & tf_error))
7586 error ("type/value mismatch at argument %d in "
7587 "template parameter list for %qD",
7588 i + 1, in_decl);
7589 inform (input_location,
7590 " expected a template of type %qD, got %qT",
7591 parm, orig_arg);
7594 val = error_mark_node;
7597 // Check that the constraints are compatible before allowing the
7598 // substitution.
7599 if (val != error_mark_node)
7600 if (!is_compatible_template_arg (parm, arg))
7602 if (in_decl && (complain & tf_error))
7604 error ("constraint mismatch at argument %d in "
7605 "template parameter list for %qD",
7606 i + 1, in_decl);
7607 inform (input_location, " expected %qD but got %qD",
7608 parm, arg);
7610 val = error_mark_node;
7614 else
7615 val = orig_arg;
7616 /* We only form one instance of each template specialization.
7617 Therefore, if we use a non-canonical variant (i.e., a
7618 typedef), any future messages referring to the type will use
7619 the typedef, which is confusing if those future uses do not
7620 themselves also use the typedef. */
7621 if (TYPE_P (val))
7622 val = canonicalize_type_argument (val, complain);
7624 else
7626 tree t = TREE_TYPE (parm);
7628 if (tree a = type_uses_auto (t))
7630 if (ARGUMENT_PACK_P (orig_arg))
7631 /* There's nothing to check for an auto argument pack. */
7632 return orig_arg;
7634 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7635 if (t == error_mark_node)
7636 return error_mark_node;
7638 else
7639 t = tsubst (t, args, complain, in_decl);
7641 if (invalid_nontype_parm_type_p (t, complain))
7642 return error_mark_node;
7644 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7646 if (same_type_p (t, TREE_TYPE (orig_arg)))
7647 val = orig_arg;
7648 else
7650 /* Not sure if this is reachable, but it doesn't hurt
7651 to be robust. */
7652 error ("type mismatch in nontype parameter pack");
7653 val = error_mark_node;
7656 else if (!type_dependent_expression_p (orig_arg)
7657 && !uses_template_parms (t))
7658 /* We used to call digest_init here. However, digest_init
7659 will report errors, which we don't want when complain
7660 is zero. More importantly, digest_init will try too
7661 hard to convert things: for example, `0' should not be
7662 converted to pointer type at this point according to
7663 the standard. Accepting this is not merely an
7664 extension, since deciding whether or not these
7665 conversions can occur is part of determining which
7666 function template to call, or whether a given explicit
7667 argument specification is valid. */
7668 val = convert_nontype_argument (t, orig_arg, complain);
7669 else
7670 val = canonicalize_expr_argument (orig_arg, complain);
7672 if (val == NULL_TREE)
7673 val = error_mark_node;
7674 else if (val == error_mark_node && (complain & tf_error))
7675 error ("could not convert template argument %qE from %qT to %qT",
7676 orig_arg, TREE_TYPE (orig_arg), t);
7678 if (INDIRECT_REF_P (val))
7680 /* Reject template arguments that are references to built-in
7681 functions with no library fallbacks. */
7682 const_tree inner = TREE_OPERAND (val, 0);
7683 const_tree innertype = TREE_TYPE (inner);
7684 if (innertype
7685 && TREE_CODE (innertype) == REFERENCE_TYPE
7686 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7687 && 0 < TREE_OPERAND_LENGTH (inner)
7688 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7689 return error_mark_node;
7692 if (TREE_CODE (val) == SCOPE_REF)
7694 /* Strip typedefs from the SCOPE_REF. */
7695 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7696 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7697 complain);
7698 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7699 QUALIFIED_NAME_IS_TEMPLATE (val));
7703 return val;
7706 /* Coerces the remaining template arguments in INNER_ARGS (from
7707 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7708 Returns the coerced argument pack. PARM_IDX is the position of this
7709 parameter in the template parameter list. ARGS is the original
7710 template argument list. */
7711 static tree
7712 coerce_template_parameter_pack (tree parms,
7713 int parm_idx,
7714 tree args,
7715 tree inner_args,
7716 int arg_idx,
7717 tree new_args,
7718 int* lost,
7719 tree in_decl,
7720 tsubst_flags_t complain)
7722 tree parm = TREE_VEC_ELT (parms, parm_idx);
7723 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7724 tree packed_args;
7725 tree argument_pack;
7726 tree packed_parms = NULL_TREE;
7728 if (arg_idx > nargs)
7729 arg_idx = nargs;
7731 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7733 /* When the template parameter is a non-type template parameter pack
7734 or template template parameter pack whose type or template
7735 parameters use parameter packs, we know exactly how many arguments
7736 we are looking for. Build a vector of the instantiated decls for
7737 these template parameters in PACKED_PARMS. */
7738 /* We can't use make_pack_expansion here because it would interpret a
7739 _DECL as a use rather than a declaration. */
7740 tree decl = TREE_VALUE (parm);
7741 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7742 SET_PACK_EXPANSION_PATTERN (exp, decl);
7743 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7744 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7746 TREE_VEC_LENGTH (args)--;
7747 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7748 TREE_VEC_LENGTH (args)++;
7750 if (packed_parms == error_mark_node)
7751 return error_mark_node;
7753 /* If we're doing a partial instantiation of a member template,
7754 verify that all of the types used for the non-type
7755 template parameter pack are, in fact, valid for non-type
7756 template parameters. */
7757 if (arg_idx < nargs
7758 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7760 int j, len = TREE_VEC_LENGTH (packed_parms);
7761 for (j = 0; j < len; ++j)
7763 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7764 if (invalid_nontype_parm_type_p (t, complain))
7765 return error_mark_node;
7767 /* We don't know how many args we have yet, just
7768 use the unconverted ones for now. */
7769 return NULL_TREE;
7772 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7774 /* Check if we have a placeholder pack, which indicates we're
7775 in the context of a introduction list. In that case we want
7776 to match this pack to the single placeholder. */
7777 else if (arg_idx < nargs
7778 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7779 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7781 nargs = arg_idx + 1;
7782 packed_args = make_tree_vec (1);
7784 else
7785 packed_args = make_tree_vec (nargs - arg_idx);
7787 /* Convert the remaining arguments, which will be a part of the
7788 parameter pack "parm". */
7789 int first_pack_arg = arg_idx;
7790 for (; arg_idx < nargs; ++arg_idx)
7792 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7793 tree actual_parm = TREE_VALUE (parm);
7794 int pack_idx = arg_idx - first_pack_arg;
7796 if (packed_parms)
7798 /* Once we've packed as many args as we have types, stop. */
7799 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7800 break;
7801 else if (PACK_EXPANSION_P (arg))
7802 /* We don't know how many args we have yet, just
7803 use the unconverted ones for now. */
7804 return NULL_TREE;
7805 else
7806 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7809 if (arg == error_mark_node)
7811 if (complain & tf_error)
7812 error ("template argument %d is invalid", arg_idx + 1);
7814 else
7815 arg = convert_template_argument (actual_parm,
7816 arg, new_args, complain, parm_idx,
7817 in_decl);
7818 if (arg == error_mark_node)
7819 (*lost)++;
7820 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7823 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7824 && TREE_VEC_LENGTH (packed_args) > 0)
7826 if (complain & tf_error)
7827 error ("wrong number of template arguments (%d, should be %d)",
7828 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7829 return error_mark_node;
7832 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7833 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7834 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7835 else
7837 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7838 TREE_TYPE (argument_pack)
7839 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7840 TREE_CONSTANT (argument_pack) = 1;
7843 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7844 if (CHECKING_P)
7845 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7846 TREE_VEC_LENGTH (packed_args));
7847 return argument_pack;
7850 /* Returns the number of pack expansions in the template argument vector
7851 ARGS. */
7853 static int
7854 pack_expansion_args_count (tree args)
7856 int i;
7857 int count = 0;
7858 if (args)
7859 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7861 tree elt = TREE_VEC_ELT (args, i);
7862 if (elt && PACK_EXPANSION_P (elt))
7863 ++count;
7865 return count;
7868 /* Convert all template arguments to their appropriate types, and
7869 return a vector containing the innermost resulting template
7870 arguments. If any error occurs, return error_mark_node. Error and
7871 warning messages are issued under control of COMPLAIN.
7873 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7874 for arguments not specified in ARGS. Otherwise, if
7875 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7876 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7877 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7878 ARGS. */
7880 static tree
7881 coerce_template_parms (tree parms,
7882 tree args,
7883 tree in_decl,
7884 tsubst_flags_t complain,
7885 bool require_all_args,
7886 bool use_default_args)
7888 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7889 tree orig_inner_args;
7890 tree inner_args;
7891 tree new_args;
7892 tree new_inner_args;
7893 int saved_unevaluated_operand;
7894 int saved_inhibit_evaluation_warnings;
7896 /* When used as a boolean value, indicates whether this is a
7897 variadic template parameter list. Since it's an int, we can also
7898 subtract it from nparms to get the number of non-variadic
7899 parameters. */
7900 int variadic_p = 0;
7901 int variadic_args_p = 0;
7902 int post_variadic_parms = 0;
7904 /* Likewise for parameters with default arguments. */
7905 int default_p = 0;
7907 if (args == error_mark_node)
7908 return error_mark_node;
7910 nparms = TREE_VEC_LENGTH (parms);
7912 /* Determine if there are any parameter packs or default arguments. */
7913 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7915 tree parm = TREE_VEC_ELT (parms, parm_idx);
7916 if (variadic_p)
7917 ++post_variadic_parms;
7918 if (template_parameter_pack_p (TREE_VALUE (parm)))
7919 ++variadic_p;
7920 if (TREE_PURPOSE (parm))
7921 ++default_p;
7924 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7925 /* If there are no parameters that follow a parameter pack, we need to
7926 expand any argument packs so that we can deduce a parameter pack from
7927 some non-packed args followed by an argument pack, as in variadic85.C.
7928 If there are such parameters, we need to leave argument packs intact
7929 so the arguments are assigned properly. This can happen when dealing
7930 with a nested class inside a partial specialization of a class
7931 template, as in variadic92.C, or when deducing a template parameter pack
7932 from a sub-declarator, as in variadic114.C. */
7933 if (!post_variadic_parms)
7934 inner_args = expand_template_argument_pack (inner_args);
7936 /* Count any pack expansion args. */
7937 variadic_args_p = pack_expansion_args_count (inner_args);
7939 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7940 if ((nargs - variadic_args_p > nparms && !variadic_p)
7941 || (nargs < nparms - variadic_p
7942 && require_all_args
7943 && !variadic_args_p
7944 && (!use_default_args
7945 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7946 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7948 if (complain & tf_error)
7950 if (variadic_p || default_p)
7952 nparms -= variadic_p + default_p;
7953 error ("wrong number of template arguments "
7954 "(%d, should be at least %d)", nargs, nparms);
7956 else
7957 error ("wrong number of template arguments "
7958 "(%d, should be %d)", nargs, nparms);
7960 if (in_decl)
7961 inform (DECL_SOURCE_LOCATION (in_decl),
7962 "provided for %qD", in_decl);
7965 return error_mark_node;
7967 /* We can't pass a pack expansion to a non-pack parameter of an alias
7968 template (DR 1430). */
7969 else if (in_decl
7970 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7971 || concept_template_p (in_decl))
7972 && variadic_args_p
7973 && nargs - variadic_args_p < nparms - variadic_p)
7975 if (complain & tf_error)
7977 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7979 tree arg = TREE_VEC_ELT (inner_args, i);
7980 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7982 if (PACK_EXPANSION_P (arg)
7983 && !template_parameter_pack_p (parm))
7985 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7986 error_at (location_of (arg),
7987 "pack expansion argument for non-pack parameter "
7988 "%qD of alias template %qD", parm, in_decl);
7989 else
7990 error_at (location_of (arg),
7991 "pack expansion argument for non-pack parameter "
7992 "%qD of concept %qD", parm, in_decl);
7993 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7994 goto found;
7997 gcc_unreachable ();
7998 found:;
8000 return error_mark_node;
8003 /* We need to evaluate the template arguments, even though this
8004 template-id may be nested within a "sizeof". */
8005 saved_unevaluated_operand = cp_unevaluated_operand;
8006 cp_unevaluated_operand = 0;
8007 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8008 c_inhibit_evaluation_warnings = 0;
8009 new_inner_args = make_tree_vec (nparms);
8010 new_args = add_outermost_template_args (args, new_inner_args);
8011 int pack_adjust = 0;
8012 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8014 tree arg;
8015 tree parm;
8017 /* Get the Ith template parameter. */
8018 parm = TREE_VEC_ELT (parms, parm_idx);
8020 if (parm == error_mark_node)
8022 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8023 continue;
8026 /* Calculate the next argument. */
8027 if (arg_idx < nargs)
8028 arg = TREE_VEC_ELT (inner_args, arg_idx);
8029 else
8030 arg = NULL_TREE;
8032 if (template_parameter_pack_p (TREE_VALUE (parm))
8033 && !(arg && ARGUMENT_PACK_P (arg)))
8035 /* Some arguments will be placed in the
8036 template parameter pack PARM. */
8037 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8038 inner_args, arg_idx,
8039 new_args, &lost,
8040 in_decl, complain);
8042 if (arg == NULL_TREE)
8044 /* We don't know how many args we have yet, just use the
8045 unconverted (and still packed) ones for now. */
8046 new_inner_args = orig_inner_args;
8047 arg_idx = nargs;
8048 break;
8051 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8053 /* Store this argument. */
8054 if (arg == error_mark_node)
8056 lost++;
8057 /* We are done with all of the arguments. */
8058 arg_idx = nargs;
8060 else
8062 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8063 arg_idx += pack_adjust;
8066 continue;
8068 else if (arg)
8070 if (PACK_EXPANSION_P (arg))
8072 /* "If every valid specialization of a variadic template
8073 requires an empty template parameter pack, the template is
8074 ill-formed, no diagnostic required." So check that the
8075 pattern works with this parameter. */
8076 tree pattern = PACK_EXPANSION_PATTERN (arg);
8077 tree conv = convert_template_argument (TREE_VALUE (parm),
8078 pattern, new_args,
8079 complain, parm_idx,
8080 in_decl);
8081 if (conv == error_mark_node)
8083 if (complain & tf_error)
8084 inform (input_location, "so any instantiation with a "
8085 "non-empty parameter pack would be ill-formed");
8086 ++lost;
8088 else if (TYPE_P (conv) && !TYPE_P (pattern))
8089 /* Recover from missing typename. */
8090 TREE_VEC_ELT (inner_args, arg_idx)
8091 = make_pack_expansion (conv);
8093 /* We don't know how many args we have yet, just
8094 use the unconverted ones for now. */
8095 new_inner_args = inner_args;
8096 arg_idx = nargs;
8097 break;
8100 else if (require_all_args)
8102 /* There must be a default arg in this case. */
8103 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8104 complain, in_decl);
8105 /* The position of the first default template argument,
8106 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8107 Record that. */
8108 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8109 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8110 arg_idx - pack_adjust);
8112 else
8113 break;
8115 if (arg == error_mark_node)
8117 if (complain & tf_error)
8118 error ("template argument %d is invalid", arg_idx + 1);
8120 else if (!arg)
8121 /* This only occurs if there was an error in the template
8122 parameter list itself (which we would already have
8123 reported) that we are trying to recover from, e.g., a class
8124 template with a parameter list such as
8125 template<typename..., typename>. */
8126 ++lost;
8127 else
8128 arg = convert_template_argument (TREE_VALUE (parm),
8129 arg, new_args, complain,
8130 parm_idx, in_decl);
8132 if (arg == error_mark_node)
8133 lost++;
8134 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8136 cp_unevaluated_operand = saved_unevaluated_operand;
8137 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8139 if (variadic_p && arg_idx < nargs)
8141 if (complain & tf_error)
8143 error ("wrong number of template arguments "
8144 "(%d, should be %d)", nargs, arg_idx);
8145 if (in_decl)
8146 error ("provided for %q+D", in_decl);
8148 return error_mark_node;
8151 if (lost)
8152 return error_mark_node;
8154 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8155 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8156 TREE_VEC_LENGTH (new_inner_args));
8158 return new_inner_args;
8161 /* Convert all template arguments to their appropriate types, and
8162 return a vector containing the innermost resulting template
8163 arguments. If any error occurs, return error_mark_node. Error and
8164 warning messages are not issued.
8166 Note that no function argument deduction is performed, and default
8167 arguments are used to fill in unspecified arguments. */
8168 tree
8169 coerce_template_parms (tree parms, tree args, tree in_decl)
8171 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8174 /* Convert all template arguments to their appropriate type, and
8175 instantiate default arguments as needed. This returns a vector
8176 containing the innermost resulting template arguments, or
8177 error_mark_node if unsuccessful. */
8178 tree
8179 coerce_template_parms (tree parms, tree args, tree in_decl,
8180 tsubst_flags_t complain)
8182 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8185 /* Like coerce_template_parms. If PARMS represents all template
8186 parameters levels, this function returns a vector of vectors
8187 representing all the resulting argument levels. Note that in this
8188 case, only the innermost arguments are coerced because the
8189 outermost ones are supposed to have been coerced already.
8191 Otherwise, if PARMS represents only (the innermost) vector of
8192 parameters, this function returns a vector containing just the
8193 innermost resulting arguments. */
8195 static tree
8196 coerce_innermost_template_parms (tree parms,
8197 tree args,
8198 tree in_decl,
8199 tsubst_flags_t complain,
8200 bool require_all_args,
8201 bool use_default_args)
8203 int parms_depth = TMPL_PARMS_DEPTH (parms);
8204 int args_depth = TMPL_ARGS_DEPTH (args);
8205 tree coerced_args;
8207 if (parms_depth > 1)
8209 coerced_args = make_tree_vec (parms_depth);
8210 tree level;
8211 int cur_depth;
8213 for (level = parms, cur_depth = parms_depth;
8214 parms_depth > 0 && level != NULL_TREE;
8215 level = TREE_CHAIN (level), --cur_depth)
8217 tree l;
8218 if (cur_depth == args_depth)
8219 l = coerce_template_parms (TREE_VALUE (level),
8220 args, in_decl, complain,
8221 require_all_args,
8222 use_default_args);
8223 else
8224 l = TMPL_ARGS_LEVEL (args, cur_depth);
8226 if (l == error_mark_node)
8227 return error_mark_node;
8229 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8232 else
8233 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8234 args, in_decl, complain,
8235 require_all_args,
8236 use_default_args);
8237 return coerced_args;
8240 /* Returns 1 if template args OT and NT are equivalent. */
8243 template_args_equal (tree ot, tree nt)
8245 if (nt == ot)
8246 return 1;
8247 if (nt == NULL_TREE || ot == NULL_TREE)
8248 return false;
8249 if (nt == any_targ_node || ot == any_targ_node)
8250 return true;
8252 if (TREE_CODE (nt) == TREE_VEC)
8253 /* For member templates */
8254 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8255 else if (PACK_EXPANSION_P (ot))
8256 return (PACK_EXPANSION_P (nt)
8257 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8258 PACK_EXPANSION_PATTERN (nt))
8259 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8260 PACK_EXPANSION_EXTRA_ARGS (nt)));
8261 else if (ARGUMENT_PACK_P (ot))
8263 int i, len;
8264 tree opack, npack;
8266 if (!ARGUMENT_PACK_P (nt))
8267 return 0;
8269 opack = ARGUMENT_PACK_ARGS (ot);
8270 npack = ARGUMENT_PACK_ARGS (nt);
8271 len = TREE_VEC_LENGTH (opack);
8272 if (TREE_VEC_LENGTH (npack) != len)
8273 return 0;
8274 for (i = 0; i < len; ++i)
8275 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8276 TREE_VEC_ELT (npack, i)))
8277 return 0;
8278 return 1;
8280 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8281 gcc_unreachable ();
8282 else if (TYPE_P (nt))
8284 if (!TYPE_P (ot))
8285 return false;
8286 /* Don't treat an alias template specialization with dependent
8287 arguments as equivalent to its underlying type when used as a
8288 template argument; we need them to be distinct so that we
8289 substitute into the specialization arguments at instantiation
8290 time. And aliases can't be equivalent without being ==, so
8291 we don't need to look any deeper. */
8292 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
8293 return false;
8294 else
8295 return same_type_p (ot, nt);
8297 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8298 return 0;
8299 else
8301 /* Try to treat a template non-type argument that has been converted
8302 to the parameter type as equivalent to one that hasn't yet. */
8303 for (enum tree_code code1 = TREE_CODE (ot);
8304 CONVERT_EXPR_CODE_P (code1)
8305 || code1 == NON_LVALUE_EXPR;
8306 code1 = TREE_CODE (ot))
8307 ot = TREE_OPERAND (ot, 0);
8308 for (enum tree_code code2 = TREE_CODE (nt);
8309 CONVERT_EXPR_CODE_P (code2)
8310 || code2 == NON_LVALUE_EXPR;
8311 code2 = TREE_CODE (nt))
8312 nt = TREE_OPERAND (nt, 0);
8314 return cp_tree_equal (ot, nt);
8318 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8319 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8320 NEWARG_PTR with the offending arguments if they are non-NULL. */
8323 comp_template_args (tree oldargs, tree newargs,
8324 tree *oldarg_ptr, tree *newarg_ptr)
8326 int i;
8328 if (oldargs == newargs)
8329 return 1;
8331 if (!oldargs || !newargs)
8332 return 0;
8334 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8335 return 0;
8337 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8339 tree nt = TREE_VEC_ELT (newargs, i);
8340 tree ot = TREE_VEC_ELT (oldargs, i);
8342 if (! template_args_equal (ot, nt))
8344 if (oldarg_ptr != NULL)
8345 *oldarg_ptr = ot;
8346 if (newarg_ptr != NULL)
8347 *newarg_ptr = nt;
8348 return 0;
8351 return 1;
8354 static void
8355 add_pending_template (tree d)
8357 tree ti = (TYPE_P (d)
8358 ? CLASSTYPE_TEMPLATE_INFO (d)
8359 : DECL_TEMPLATE_INFO (d));
8360 struct pending_template *pt;
8361 int level;
8363 if (TI_PENDING_TEMPLATE_FLAG (ti))
8364 return;
8366 /* We are called both from instantiate_decl, where we've already had a
8367 tinst_level pushed, and instantiate_template, where we haven't.
8368 Compensate. */
8369 level = !current_tinst_level || current_tinst_level->decl != d;
8371 if (level)
8372 push_tinst_level (d);
8374 pt = ggc_alloc<pending_template> ();
8375 pt->next = NULL;
8376 pt->tinst = current_tinst_level;
8377 if (last_pending_template)
8378 last_pending_template->next = pt;
8379 else
8380 pending_templates = pt;
8382 last_pending_template = pt;
8384 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8386 if (level)
8387 pop_tinst_level ();
8391 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8392 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8393 documentation for TEMPLATE_ID_EXPR. */
8395 tree
8396 lookup_template_function (tree fns, tree arglist)
8398 tree type;
8400 if (fns == error_mark_node || arglist == error_mark_node)
8401 return error_mark_node;
8403 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8405 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8407 error ("%q#D is not a function template", fns);
8408 return error_mark_node;
8411 if (BASELINK_P (fns))
8413 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8414 unknown_type_node,
8415 BASELINK_FUNCTIONS (fns),
8416 arglist);
8417 return fns;
8420 type = TREE_TYPE (fns);
8421 if (TREE_CODE (fns) == OVERLOAD || !type)
8422 type = unknown_type_node;
8424 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8427 /* Within the scope of a template class S<T>, the name S gets bound
8428 (in build_self_reference) to a TYPE_DECL for the class, not a
8429 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8430 or one of its enclosing classes, and that type is a template,
8431 return the associated TEMPLATE_DECL. Otherwise, the original
8432 DECL is returned.
8434 Also handle the case when DECL is a TREE_LIST of ambiguous
8435 injected-class-names from different bases. */
8437 tree
8438 maybe_get_template_decl_from_type_decl (tree decl)
8440 if (decl == NULL_TREE)
8441 return decl;
8443 /* DR 176: A lookup that finds an injected-class-name (10.2
8444 [class.member.lookup]) can result in an ambiguity in certain cases
8445 (for example, if it is found in more than one base class). If all of
8446 the injected-class-names that are found refer to specializations of
8447 the same class template, and if the name is followed by a
8448 template-argument-list, the reference refers to the class template
8449 itself and not a specialization thereof, and is not ambiguous. */
8450 if (TREE_CODE (decl) == TREE_LIST)
8452 tree t, tmpl = NULL_TREE;
8453 for (t = decl; t; t = TREE_CHAIN (t))
8455 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8456 if (!tmpl)
8457 tmpl = elt;
8458 else if (tmpl != elt)
8459 break;
8461 if (tmpl && t == NULL_TREE)
8462 return tmpl;
8463 else
8464 return decl;
8467 return (decl != NULL_TREE
8468 && DECL_SELF_REFERENCE_P (decl)
8469 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8470 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8473 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8474 parameters, find the desired type.
8476 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8478 IN_DECL, if non-NULL, is the template declaration we are trying to
8479 instantiate.
8481 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8482 the class we are looking up.
8484 Issue error and warning messages under control of COMPLAIN.
8486 If the template class is really a local class in a template
8487 function, then the FUNCTION_CONTEXT is the function in which it is
8488 being instantiated.
8490 ??? Note that this function is currently called *twice* for each
8491 template-id: the first time from the parser, while creating the
8492 incomplete type (finish_template_type), and the second type during the
8493 real instantiation (instantiate_template_class). This is surely something
8494 that we want to avoid. It also causes some problems with argument
8495 coercion (see convert_nontype_argument for more information on this). */
8497 static tree
8498 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8499 int entering_scope, tsubst_flags_t complain)
8501 tree templ = NULL_TREE, parmlist;
8502 tree t;
8503 spec_entry **slot;
8504 spec_entry *entry;
8505 spec_entry elt;
8506 hashval_t hash;
8508 if (identifier_p (d1))
8510 tree value = innermost_non_namespace_value (d1);
8511 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8512 templ = value;
8513 else
8515 if (context)
8516 push_decl_namespace (context);
8517 templ = lookup_name (d1);
8518 templ = maybe_get_template_decl_from_type_decl (templ);
8519 if (context)
8520 pop_decl_namespace ();
8522 if (templ)
8523 context = DECL_CONTEXT (templ);
8525 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8527 tree type = TREE_TYPE (d1);
8529 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8530 an implicit typename for the second A. Deal with it. */
8531 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8532 type = TREE_TYPE (type);
8534 if (CLASSTYPE_TEMPLATE_INFO (type))
8536 templ = CLASSTYPE_TI_TEMPLATE (type);
8537 d1 = DECL_NAME (templ);
8540 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8541 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8543 templ = TYPE_TI_TEMPLATE (d1);
8544 d1 = DECL_NAME (templ);
8546 else if (DECL_TYPE_TEMPLATE_P (d1))
8548 templ = d1;
8549 d1 = DECL_NAME (templ);
8550 context = DECL_CONTEXT (templ);
8552 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8554 templ = d1;
8555 d1 = DECL_NAME (templ);
8558 /* Issue an error message if we didn't find a template. */
8559 if (! templ)
8561 if (complain & tf_error)
8562 error ("%qT is not a template", d1);
8563 return error_mark_node;
8566 if (TREE_CODE (templ) != TEMPLATE_DECL
8567 /* Make sure it's a user visible template, if it was named by
8568 the user. */
8569 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8570 && !PRIMARY_TEMPLATE_P (templ)))
8572 if (complain & tf_error)
8574 error ("non-template type %qT used as a template", d1);
8575 if (in_decl)
8576 error ("for template declaration %q+D", in_decl);
8578 return error_mark_node;
8581 complain &= ~tf_user;
8583 /* An alias that just changes the name of a template is equivalent to the
8584 other template, so if any of the arguments are pack expansions, strip
8585 the alias to avoid problems with a pack expansion passed to a non-pack
8586 alias template parameter (DR 1430). */
8587 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8588 templ = get_underlying_template (templ);
8590 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8592 tree parm;
8593 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8594 if (arglist2 == error_mark_node
8595 || (!uses_template_parms (arglist2)
8596 && check_instantiated_args (templ, arglist2, complain)))
8597 return error_mark_node;
8599 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8600 return parm;
8602 else
8604 tree template_type = TREE_TYPE (templ);
8605 tree gen_tmpl;
8606 tree type_decl;
8607 tree found = NULL_TREE;
8608 int arg_depth;
8609 int parm_depth;
8610 int is_dependent_type;
8611 int use_partial_inst_tmpl = false;
8613 if (template_type == error_mark_node)
8614 /* An error occurred while building the template TEMPL, and a
8615 diagnostic has most certainly been emitted for that
8616 already. Let's propagate that error. */
8617 return error_mark_node;
8619 gen_tmpl = most_general_template (templ);
8620 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8621 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8622 arg_depth = TMPL_ARGS_DEPTH (arglist);
8624 if (arg_depth == 1 && parm_depth > 1)
8626 /* We've been given an incomplete set of template arguments.
8627 For example, given:
8629 template <class T> struct S1 {
8630 template <class U> struct S2 {};
8631 template <class U> struct S2<U*> {};
8634 we will be called with an ARGLIST of `U*', but the
8635 TEMPLATE will be `template <class T> template
8636 <class U> struct S1<T>::S2'. We must fill in the missing
8637 arguments. */
8638 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8639 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8640 arg_depth = TMPL_ARGS_DEPTH (arglist);
8643 /* Now we should have enough arguments. */
8644 gcc_assert (parm_depth == arg_depth);
8646 /* From here on, we're only interested in the most general
8647 template. */
8649 /* Calculate the BOUND_ARGS. These will be the args that are
8650 actually tsubst'd into the definition to create the
8651 instantiation. */
8652 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8653 complain,
8654 /*require_all_args=*/true,
8655 /*use_default_args=*/true);
8657 if (arglist == error_mark_node)
8658 /* We were unable to bind the arguments. */
8659 return error_mark_node;
8661 /* In the scope of a template class, explicit references to the
8662 template class refer to the type of the template, not any
8663 instantiation of it. For example, in:
8665 template <class T> class C { void f(C<T>); }
8667 the `C<T>' is just the same as `C'. Outside of the
8668 class, however, such a reference is an instantiation. */
8669 if (entering_scope
8670 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8671 || currently_open_class (template_type))
8673 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (template_type);
8675 if (comp_template_args (TI_ARGS (tinfo), arglist))
8676 return template_type;
8679 /* If we already have this specialization, return it. */
8680 elt.tmpl = gen_tmpl;
8681 elt.args = arglist;
8682 elt.spec = NULL_TREE;
8683 hash = spec_hasher::hash (&elt);
8684 entry = type_specializations->find_with_hash (&elt, hash);
8686 if (entry)
8687 return entry->spec;
8689 /* If the the template's constraints are not satisfied,
8690 then we cannot form a valid type.
8692 Note that the check is deferred until after the hash
8693 lookup. This prevents redundant checks on previously
8694 instantiated specializations. */
8695 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8697 if (complain & tf_error)
8699 error ("template constraint failure");
8700 diagnose_constraints (input_location, gen_tmpl, arglist);
8702 return error_mark_node;
8705 is_dependent_type = uses_template_parms (arglist);
8707 /* If the deduced arguments are invalid, then the binding
8708 failed. */
8709 if (!is_dependent_type
8710 && check_instantiated_args (gen_tmpl,
8711 INNERMOST_TEMPLATE_ARGS (arglist),
8712 complain))
8713 return error_mark_node;
8715 if (!is_dependent_type
8716 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8717 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8718 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8720 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8721 DECL_NAME (gen_tmpl),
8722 /*tag_scope=*/ts_global);
8723 return found;
8726 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8727 complain, in_decl);
8728 if (context == error_mark_node)
8729 return error_mark_node;
8731 if (!context)
8732 context = global_namespace;
8734 /* Create the type. */
8735 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8737 /* The user referred to a specialization of an alias
8738 template represented by GEN_TMPL.
8740 [temp.alias]/2 says:
8742 When a template-id refers to the specialization of an
8743 alias template, it is equivalent to the associated
8744 type obtained by substitution of its
8745 template-arguments for the template-parameters in the
8746 type-id of the alias template. */
8748 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8749 /* Note that the call above (by indirectly calling
8750 register_specialization in tsubst_decl) registers the
8751 TYPE_DECL representing the specialization of the alias
8752 template. So next time someone substitutes ARGLIST for
8753 the template parms into the alias template (GEN_TMPL),
8754 she'll get that TYPE_DECL back. */
8756 if (t == error_mark_node)
8757 return t;
8759 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8761 if (!is_dependent_type)
8763 set_current_access_from_decl (TYPE_NAME (template_type));
8764 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8765 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8766 arglist, complain, in_decl),
8767 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8768 arglist, complain, in_decl),
8769 SCOPED_ENUM_P (template_type), NULL);
8771 if (t == error_mark_node)
8772 return t;
8774 else
8776 /* We don't want to call start_enum for this type, since
8777 the values for the enumeration constants may involve
8778 template parameters. And, no one should be interested
8779 in the enumeration constants for such a type. */
8780 t = cxx_make_type (ENUMERAL_TYPE);
8781 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8783 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8784 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8785 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8787 else if (CLASS_TYPE_P (template_type))
8789 t = make_class_type (TREE_CODE (template_type));
8790 CLASSTYPE_DECLARED_CLASS (t)
8791 = CLASSTYPE_DECLARED_CLASS (template_type);
8792 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8794 /* A local class. Make sure the decl gets registered properly. */
8795 if (context == current_function_decl)
8796 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8798 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8799 /* This instantiation is another name for the primary
8800 template type. Set the TYPE_CANONICAL field
8801 appropriately. */
8802 TYPE_CANONICAL (t) = template_type;
8803 else if (any_template_arguments_need_structural_equality_p (arglist))
8804 /* Some of the template arguments require structural
8805 equality testing, so this template class requires
8806 structural equality testing. */
8807 SET_TYPE_STRUCTURAL_EQUALITY (t);
8809 else
8810 gcc_unreachable ();
8812 /* If we called start_enum or pushtag above, this information
8813 will already be set up. */
8814 if (!TYPE_NAME (t))
8816 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8818 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8819 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8820 DECL_SOURCE_LOCATION (type_decl)
8821 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8823 else
8824 type_decl = TYPE_NAME (t);
8826 if (CLASS_TYPE_P (template_type))
8828 TREE_PRIVATE (type_decl)
8829 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8830 TREE_PROTECTED (type_decl)
8831 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8832 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8834 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8835 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8839 if (OVERLOAD_TYPE_P (t)
8840 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8842 static const char *tags[] = {"abi_tag", "may_alias"};
8844 for (unsigned ix = 0; ix != 2; ix++)
8846 tree attributes
8847 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8849 if (attributes)
8850 TYPE_ATTRIBUTES (t)
8851 = tree_cons (TREE_PURPOSE (attributes),
8852 TREE_VALUE (attributes),
8853 TYPE_ATTRIBUTES (t));
8857 /* Let's consider the explicit specialization of a member
8858 of a class template specialization that is implicitly instantiated,
8859 e.g.:
8860 template<class T>
8861 struct S
8863 template<class U> struct M {}; //#0
8866 template<>
8867 template<>
8868 struct S<int>::M<char> //#1
8870 int i;
8872 [temp.expl.spec]/4 says this is valid.
8874 In this case, when we write:
8875 S<int>::M<char> m;
8877 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8878 the one of #0.
8880 When we encounter #1, we want to store the partial instantiation
8881 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8883 For all cases other than this "explicit specialization of member of a
8884 class template", we just want to store the most general template into
8885 the CLASSTYPE_TI_TEMPLATE of M.
8887 This case of "explicit specialization of member of a class template"
8888 only happens when:
8889 1/ the enclosing class is an instantiation of, and therefore not
8890 the same as, the context of the most general template, and
8891 2/ we aren't looking at the partial instantiation itself, i.e.
8892 the innermost arguments are not the same as the innermost parms of
8893 the most general template.
8895 So it's only when 1/ and 2/ happens that we want to use the partial
8896 instantiation of the member template in lieu of its most general
8897 template. */
8899 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8900 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8901 /* the enclosing class must be an instantiation... */
8902 && CLASS_TYPE_P (context)
8903 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8905 TREE_VEC_LENGTH (arglist)--;
8906 ++processing_template_decl;
8907 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
8908 tree partial_inst_args =
8909 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
8910 arglist, complain, NULL_TREE);
8911 --processing_template_decl;
8912 TREE_VEC_LENGTH (arglist)++;
8913 if (partial_inst_args == error_mark_node)
8914 return error_mark_node;
8915 use_partial_inst_tmpl =
8916 /*...and we must not be looking at the partial instantiation
8917 itself. */
8918 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8919 partial_inst_args);
8922 if (!use_partial_inst_tmpl)
8923 /* This case is easy; there are no member templates involved. */
8924 found = gen_tmpl;
8925 else
8927 /* This is a full instantiation of a member template. Find
8928 the partial instantiation of which this is an instance. */
8930 /* Temporarily reduce by one the number of levels in the ARGLIST
8931 so as to avoid comparing the last set of arguments. */
8932 TREE_VEC_LENGTH (arglist)--;
8933 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8934 TREE_VEC_LENGTH (arglist)++;
8935 /* FOUND is either a proper class type, or an alias
8936 template specialization. In the later case, it's a
8937 TYPE_DECL, resulting from the substituting of arguments
8938 for parameters in the TYPE_DECL of the alias template
8939 done earlier. So be careful while getting the template
8940 of FOUND. */
8941 found = (TREE_CODE (found) == TEMPLATE_DECL
8942 ? found
8943 : (TREE_CODE (found) == TYPE_DECL
8944 ? DECL_TI_TEMPLATE (found)
8945 : CLASSTYPE_TI_TEMPLATE (found)));
8948 // Build template info for the new specialization.
8949 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8951 elt.spec = t;
8952 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8953 entry = ggc_alloc<spec_entry> ();
8954 *entry = elt;
8955 *slot = entry;
8957 /* Note this use of the partial instantiation so we can check it
8958 later in maybe_process_partial_specialization. */
8959 DECL_TEMPLATE_INSTANTIATIONS (found)
8960 = tree_cons (arglist, t,
8961 DECL_TEMPLATE_INSTANTIATIONS (found));
8963 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8964 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8965 /* Now that the type has been registered on the instantiations
8966 list, we set up the enumerators. Because the enumeration
8967 constants may involve the enumeration type itself, we make
8968 sure to register the type first, and then create the
8969 constants. That way, doing tsubst_expr for the enumeration
8970 constants won't result in recursive calls here; we'll find
8971 the instantiation and exit above. */
8972 tsubst_enum (template_type, t, arglist);
8974 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8975 /* If the type makes use of template parameters, the
8976 code that generates debugging information will crash. */
8977 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8979 /* Possibly limit visibility based on template args. */
8980 TREE_PUBLIC (type_decl) = 1;
8981 determine_visibility (type_decl);
8983 inherit_targ_abi_tags (t);
8985 return t;
8989 /* Wrapper for lookup_template_class_1. */
8991 tree
8992 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8993 int entering_scope, tsubst_flags_t complain)
8995 tree ret;
8996 timevar_push (TV_TEMPLATE_INST);
8997 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8998 entering_scope, complain);
8999 timevar_pop (TV_TEMPLATE_INST);
9000 return ret;
9003 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9005 tree
9006 lookup_template_variable (tree templ, tree arglist)
9008 /* The type of the expression is NULL_TREE since the template-id could refer
9009 to an explicit or partial specialization. */
9010 tree type = NULL_TREE;
9011 if (flag_concepts && variable_concept_p (templ))
9012 /* Except that concepts are always bool. */
9013 type = boolean_type_node;
9014 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9017 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9019 tree
9020 finish_template_variable (tree var, tsubst_flags_t complain)
9022 tree templ = TREE_OPERAND (var, 0);
9023 tree arglist = TREE_OPERAND (var, 1);
9025 /* We never want to return a VAR_DECL for a variable concept, since they
9026 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9027 bool concept_p = flag_concepts && variable_concept_p (templ);
9028 if (concept_p && processing_template_decl)
9029 return var;
9031 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9032 arglist = add_outermost_template_args (tmpl_args, arglist);
9034 templ = most_general_template (templ);
9035 tree parms = DECL_TEMPLATE_PARMS (templ);
9036 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9037 /*req_all*/true,
9038 /*use_default*/true);
9040 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9042 if (complain & tf_error)
9044 error ("use of invalid variable template %qE", var);
9045 diagnose_constraints (location_of (var), templ, arglist);
9047 return error_mark_node;
9050 /* If a template-id refers to a specialization of a variable
9051 concept, then the expression is true if and only if the
9052 concept's constraints are satisfied by the given template
9053 arguments.
9055 NOTE: This is an extension of Concepts Lite TS that
9056 allows constraints to be used in expressions. */
9057 if (concept_p)
9059 tree decl = DECL_TEMPLATE_RESULT (templ);
9060 return evaluate_variable_concept (decl, arglist);
9063 return instantiate_template (templ, arglist, complain);
9066 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9067 TARGS template args, and instantiate it if it's not dependent. */
9069 tree
9070 lookup_and_finish_template_variable (tree templ, tree targs,
9071 tsubst_flags_t complain)
9073 templ = lookup_template_variable (templ, targs);
9074 if (!any_dependent_template_arguments_p (targs))
9076 templ = finish_template_variable (templ, complain);
9077 mark_used (templ);
9080 return convert_from_reference (templ);
9084 struct pair_fn_data
9086 tree_fn_t fn;
9087 tree_fn_t any_fn;
9088 void *data;
9089 /* True when we should also visit template parameters that occur in
9090 non-deduced contexts. */
9091 bool include_nondeduced_p;
9092 hash_set<tree> *visited;
9095 /* Called from for_each_template_parm via walk_tree. */
9097 static tree
9098 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9100 tree t = *tp;
9101 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9102 tree_fn_t fn = pfd->fn;
9103 void *data = pfd->data;
9104 tree result = NULL_TREE;
9106 #define WALK_SUBTREE(NODE) \
9107 do \
9109 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9110 pfd->include_nondeduced_p, \
9111 pfd->any_fn); \
9112 if (result) goto out; \
9114 while (0)
9116 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9117 return t;
9119 if (TYPE_P (t)
9120 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9121 WALK_SUBTREE (TYPE_CONTEXT (t));
9123 switch (TREE_CODE (t))
9125 case RECORD_TYPE:
9126 if (TYPE_PTRMEMFUNC_P (t))
9127 break;
9128 /* Fall through. */
9130 case UNION_TYPE:
9131 case ENUMERAL_TYPE:
9132 if (!TYPE_TEMPLATE_INFO (t))
9133 *walk_subtrees = 0;
9134 else
9135 WALK_SUBTREE (TYPE_TI_ARGS (t));
9136 break;
9138 case INTEGER_TYPE:
9139 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9140 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9141 break;
9143 case METHOD_TYPE:
9144 /* Since we're not going to walk subtrees, we have to do this
9145 explicitly here. */
9146 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9147 /* Fall through. */
9149 case FUNCTION_TYPE:
9150 /* Check the return type. */
9151 WALK_SUBTREE (TREE_TYPE (t));
9153 /* Check the parameter types. Since default arguments are not
9154 instantiated until they are needed, the TYPE_ARG_TYPES may
9155 contain expressions that involve template parameters. But,
9156 no-one should be looking at them yet. And, once they're
9157 instantiated, they don't contain template parameters, so
9158 there's no point in looking at them then, either. */
9160 tree parm;
9162 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9163 WALK_SUBTREE (TREE_VALUE (parm));
9165 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9166 want walk_tree walking into them itself. */
9167 *walk_subtrees = 0;
9170 if (flag_noexcept_type)
9172 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9173 if (spec)
9174 WALK_SUBTREE (TREE_PURPOSE (spec));
9176 break;
9178 case TYPEOF_TYPE:
9179 case UNDERLYING_TYPE:
9180 if (pfd->include_nondeduced_p
9181 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9182 pfd->visited,
9183 pfd->include_nondeduced_p,
9184 pfd->any_fn))
9185 return error_mark_node;
9186 break;
9188 case FUNCTION_DECL:
9189 case VAR_DECL:
9190 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9191 WALK_SUBTREE (DECL_TI_ARGS (t));
9192 /* Fall through. */
9194 case PARM_DECL:
9195 case CONST_DECL:
9196 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9197 WALK_SUBTREE (DECL_INITIAL (t));
9198 if (DECL_CONTEXT (t)
9199 && pfd->include_nondeduced_p)
9200 WALK_SUBTREE (DECL_CONTEXT (t));
9201 break;
9203 case BOUND_TEMPLATE_TEMPLATE_PARM:
9204 /* Record template parameters such as `T' inside `TT<T>'. */
9205 WALK_SUBTREE (TYPE_TI_ARGS (t));
9206 /* Fall through. */
9208 case TEMPLATE_TEMPLATE_PARM:
9209 case TEMPLATE_TYPE_PARM:
9210 case TEMPLATE_PARM_INDEX:
9211 if (fn && (*fn)(t, data))
9212 return t;
9213 else if (!fn)
9214 return t;
9215 break;
9217 case TEMPLATE_DECL:
9218 /* A template template parameter is encountered. */
9219 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9220 WALK_SUBTREE (TREE_TYPE (t));
9222 /* Already substituted template template parameter */
9223 *walk_subtrees = 0;
9224 break;
9226 case TYPENAME_TYPE:
9227 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9228 partial instantiation. */
9229 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9230 break;
9232 case CONSTRUCTOR:
9233 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9234 && pfd->include_nondeduced_p)
9235 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9236 break;
9238 case INDIRECT_REF:
9239 case COMPONENT_REF:
9240 /* If there's no type, then this thing must be some expression
9241 involving template parameters. */
9242 if (!fn && !TREE_TYPE (t))
9243 return error_mark_node;
9244 break;
9246 case MODOP_EXPR:
9247 case CAST_EXPR:
9248 case IMPLICIT_CONV_EXPR:
9249 case REINTERPRET_CAST_EXPR:
9250 case CONST_CAST_EXPR:
9251 case STATIC_CAST_EXPR:
9252 case DYNAMIC_CAST_EXPR:
9253 case ARROW_EXPR:
9254 case DOTSTAR_EXPR:
9255 case TYPEID_EXPR:
9256 case PSEUDO_DTOR_EXPR:
9257 if (!fn)
9258 return error_mark_node;
9259 break;
9261 default:
9262 break;
9265 #undef WALK_SUBTREE
9267 /* We didn't find any template parameters we liked. */
9268 out:
9269 return result;
9272 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9273 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9274 call FN with the parameter and the DATA.
9275 If FN returns nonzero, the iteration is terminated, and
9276 for_each_template_parm returns 1. Otherwise, the iteration
9277 continues. If FN never returns a nonzero value, the value
9278 returned by for_each_template_parm is 0. If FN is NULL, it is
9279 considered to be the function which always returns 1.
9281 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9282 parameters that occur in non-deduced contexts. When false, only
9283 visits those template parameters that can be deduced. */
9285 static tree
9286 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9287 hash_set<tree> *visited,
9288 bool include_nondeduced_p,
9289 tree_fn_t any_fn)
9291 struct pair_fn_data pfd;
9292 tree result;
9294 /* Set up. */
9295 pfd.fn = fn;
9296 pfd.any_fn = any_fn;
9297 pfd.data = data;
9298 pfd.include_nondeduced_p = include_nondeduced_p;
9300 /* Walk the tree. (Conceptually, we would like to walk without
9301 duplicates, but for_each_template_parm_r recursively calls
9302 for_each_template_parm, so we would need to reorganize a fair
9303 bit to use walk_tree_without_duplicates, so we keep our own
9304 visited list.) */
9305 if (visited)
9306 pfd.visited = visited;
9307 else
9308 pfd.visited = new hash_set<tree>;
9309 result = cp_walk_tree (&t,
9310 for_each_template_parm_r,
9311 &pfd,
9312 pfd.visited);
9314 /* Clean up. */
9315 if (!visited)
9317 delete pfd.visited;
9318 pfd.visited = 0;
9321 return result;
9324 /* Returns true if T depends on any template parameter. */
9327 uses_template_parms (tree t)
9329 if (t == NULL_TREE)
9330 return false;
9332 bool dependent_p;
9333 int saved_processing_template_decl;
9335 saved_processing_template_decl = processing_template_decl;
9336 if (!saved_processing_template_decl)
9337 processing_template_decl = 1;
9338 if (TYPE_P (t))
9339 dependent_p = dependent_type_p (t);
9340 else if (TREE_CODE (t) == TREE_VEC)
9341 dependent_p = any_dependent_template_arguments_p (t);
9342 else if (TREE_CODE (t) == TREE_LIST)
9343 dependent_p = (uses_template_parms (TREE_VALUE (t))
9344 || uses_template_parms (TREE_CHAIN (t)));
9345 else if (TREE_CODE (t) == TYPE_DECL)
9346 dependent_p = dependent_type_p (TREE_TYPE (t));
9347 else if (DECL_P (t)
9348 || EXPR_P (t)
9349 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9350 || TREE_CODE (t) == OVERLOAD
9351 || BASELINK_P (t)
9352 || identifier_p (t)
9353 || TREE_CODE (t) == TRAIT_EXPR
9354 || TREE_CODE (t) == CONSTRUCTOR
9355 || CONSTANT_CLASS_P (t))
9356 dependent_p = (type_dependent_expression_p (t)
9357 || value_dependent_expression_p (t));
9358 else
9360 gcc_assert (t == error_mark_node);
9361 dependent_p = false;
9364 processing_template_decl = saved_processing_template_decl;
9366 return dependent_p;
9369 /* Returns true iff current_function_decl is an incompletely instantiated
9370 template. Useful instead of processing_template_decl because the latter
9371 is set to 0 during instantiate_non_dependent_expr. */
9373 bool
9374 in_template_function (void)
9376 tree fn = current_function_decl;
9377 bool ret;
9378 ++processing_template_decl;
9379 ret = (fn && DECL_LANG_SPECIFIC (fn)
9380 && DECL_TEMPLATE_INFO (fn)
9381 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9382 --processing_template_decl;
9383 return ret;
9386 /* Returns true if T depends on any template parameter with level LEVEL. */
9388 bool
9389 uses_template_parms_level (tree t, int level)
9391 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9392 /*include_nondeduced_p=*/true);
9395 /* Returns true if the signature of DECL depends on any template parameter from
9396 its enclosing class. */
9398 bool
9399 uses_outer_template_parms (tree decl)
9401 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9402 if (depth == 0)
9403 return false;
9404 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9405 &depth, NULL, /*include_nondeduced_p=*/true))
9406 return true;
9407 if (PRIMARY_TEMPLATE_P (decl)
9408 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9409 (DECL_TEMPLATE_PARMS (decl)),
9410 template_parm_outer_level,
9411 &depth, NULL, /*include_nondeduced_p=*/true))
9412 return true;
9413 tree ci = get_constraints (decl);
9414 if (ci)
9415 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9416 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9417 &depth, NULL, /*nondeduced*/true))
9418 return true;
9419 return false;
9422 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9423 ill-formed translation unit, i.e. a variable or function that isn't
9424 usable in a constant expression. */
9426 static inline bool
9427 neglectable_inst_p (tree d)
9429 return (DECL_P (d)
9430 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9431 : decl_maybe_constant_var_p (d)));
9434 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9435 neglectable and instantiated from within an erroneous instantiation. */
9437 static bool
9438 limit_bad_template_recursion (tree decl)
9440 struct tinst_level *lev = current_tinst_level;
9441 int errs = errorcount + sorrycount;
9442 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9443 return false;
9445 for (; lev; lev = lev->next)
9446 if (neglectable_inst_p (lev->decl))
9447 break;
9449 return (lev && errs > lev->errors);
9452 static int tinst_depth;
9453 extern int max_tinst_depth;
9454 int depth_reached;
9456 static GTY(()) struct tinst_level *last_error_tinst_level;
9458 /* We're starting to instantiate D; record the template instantiation context
9459 for diagnostics and to restore it later. */
9461 bool
9462 push_tinst_level (tree d)
9464 return push_tinst_level_loc (d, input_location);
9467 /* We're starting to instantiate D; record the template instantiation context
9468 at LOC for diagnostics and to restore it later. */
9470 bool
9471 push_tinst_level_loc (tree d, location_t loc)
9473 struct tinst_level *new_level;
9475 if (tinst_depth >= max_tinst_depth)
9477 /* Tell error.c not to try to instantiate any templates. */
9478 at_eof = 2;
9479 fatal_error (input_location,
9480 "template instantiation depth exceeds maximum of %d"
9481 " (use -ftemplate-depth= to increase the maximum)",
9482 max_tinst_depth);
9483 return false;
9486 /* If the current instantiation caused problems, don't let it instantiate
9487 anything else. Do allow deduction substitution and decls usable in
9488 constant expressions. */
9489 if (limit_bad_template_recursion (d))
9490 return false;
9492 /* When not -quiet, dump template instantiations other than functions, since
9493 announce_function will take care of those. */
9494 if (!quiet_flag
9495 && TREE_CODE (d) != TREE_LIST
9496 && TREE_CODE (d) != FUNCTION_DECL)
9497 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9499 new_level = ggc_alloc<tinst_level> ();
9500 new_level->decl = d;
9501 new_level->locus = loc;
9502 new_level->errors = errorcount+sorrycount;
9503 new_level->in_system_header_p = in_system_header_at (input_location);
9504 new_level->next = current_tinst_level;
9505 current_tinst_level = new_level;
9507 ++tinst_depth;
9508 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9509 depth_reached = tinst_depth;
9511 return true;
9514 /* We're done instantiating this template; return to the instantiation
9515 context. */
9517 void
9518 pop_tinst_level (void)
9520 /* Restore the filename and line number stashed away when we started
9521 this instantiation. */
9522 input_location = current_tinst_level->locus;
9523 current_tinst_level = current_tinst_level->next;
9524 --tinst_depth;
9527 /* We're instantiating a deferred template; restore the template
9528 instantiation context in which the instantiation was requested, which
9529 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9531 static tree
9532 reopen_tinst_level (struct tinst_level *level)
9534 struct tinst_level *t;
9536 tinst_depth = 0;
9537 for (t = level; t; t = t->next)
9538 ++tinst_depth;
9540 current_tinst_level = level;
9541 pop_tinst_level ();
9542 if (current_tinst_level)
9543 current_tinst_level->errors = errorcount+sorrycount;
9544 return level->decl;
9547 /* Returns the TINST_LEVEL which gives the original instantiation
9548 context. */
9550 struct tinst_level *
9551 outermost_tinst_level (void)
9553 struct tinst_level *level = current_tinst_level;
9554 if (level)
9555 while (level->next)
9556 level = level->next;
9557 return level;
9560 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9561 vector of template arguments, as for tsubst.
9563 Returns an appropriate tsubst'd friend declaration. */
9565 static tree
9566 tsubst_friend_function (tree decl, tree args)
9568 tree new_friend;
9570 if (TREE_CODE (decl) == FUNCTION_DECL
9571 && DECL_TEMPLATE_INSTANTIATION (decl)
9572 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9573 /* This was a friend declared with an explicit template
9574 argument list, e.g.:
9576 friend void f<>(T);
9578 to indicate that f was a template instantiation, not a new
9579 function declaration. Now, we have to figure out what
9580 instantiation of what template. */
9582 tree template_id, arglist, fns;
9583 tree new_args;
9584 tree tmpl;
9585 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9587 /* Friend functions are looked up in the containing namespace scope.
9588 We must enter that scope, to avoid finding member functions of the
9589 current class with same name. */
9590 push_nested_namespace (ns);
9591 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9592 tf_warning_or_error, NULL_TREE,
9593 /*integral_constant_expression_p=*/false);
9594 pop_nested_namespace (ns);
9595 arglist = tsubst (DECL_TI_ARGS (decl), args,
9596 tf_warning_or_error, NULL_TREE);
9597 template_id = lookup_template_function (fns, arglist);
9599 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9600 tmpl = determine_specialization (template_id, new_friend,
9601 &new_args,
9602 /*need_member_template=*/0,
9603 TREE_VEC_LENGTH (args),
9604 tsk_none);
9605 return instantiate_template (tmpl, new_args, tf_error);
9608 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9610 /* The NEW_FRIEND will look like an instantiation, to the
9611 compiler, but is not an instantiation from the point of view of
9612 the language. For example, we might have had:
9614 template <class T> struct S {
9615 template <class U> friend void f(T, U);
9618 Then, in S<int>, template <class U> void f(int, U) is not an
9619 instantiation of anything. */
9620 if (new_friend == error_mark_node)
9621 return error_mark_node;
9623 DECL_USE_TEMPLATE (new_friend) = 0;
9624 if (TREE_CODE (decl) == TEMPLATE_DECL)
9626 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9627 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9628 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9631 /* The mangled name for the NEW_FRIEND is incorrect. The function
9632 is not a template instantiation and should not be mangled like
9633 one. Therefore, we forget the mangling here; we'll recompute it
9634 later if we need it. */
9635 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9637 SET_DECL_RTL (new_friend, NULL);
9638 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9641 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9643 tree old_decl;
9644 tree new_friend_template_info;
9645 tree new_friend_result_template_info;
9646 tree ns;
9647 int new_friend_is_defn;
9649 /* We must save some information from NEW_FRIEND before calling
9650 duplicate decls since that function will free NEW_FRIEND if
9651 possible. */
9652 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9653 new_friend_is_defn =
9654 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9655 (template_for_substitution (new_friend)))
9656 != NULL_TREE);
9657 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9659 /* This declaration is a `primary' template. */
9660 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9662 new_friend_result_template_info
9663 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9665 else
9666 new_friend_result_template_info = NULL_TREE;
9668 /* Inside pushdecl_namespace_level, we will push into the
9669 current namespace. However, the friend function should go
9670 into the namespace of the template. */
9671 ns = decl_namespace_context (new_friend);
9672 push_nested_namespace (ns);
9673 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9674 pop_nested_namespace (ns);
9676 if (old_decl == error_mark_node)
9677 return error_mark_node;
9679 if (old_decl != new_friend)
9681 /* This new friend declaration matched an existing
9682 declaration. For example, given:
9684 template <class T> void f(T);
9685 template <class U> class C {
9686 template <class T> friend void f(T) {}
9689 the friend declaration actually provides the definition
9690 of `f', once C has been instantiated for some type. So,
9691 old_decl will be the out-of-class template declaration,
9692 while new_friend is the in-class definition.
9694 But, if `f' was called before this point, the
9695 instantiation of `f' will have DECL_TI_ARGS corresponding
9696 to `T' but not to `U', references to which might appear
9697 in the definition of `f'. Previously, the most general
9698 template for an instantiation of `f' was the out-of-class
9699 version; now it is the in-class version. Therefore, we
9700 run through all specialization of `f', adding to their
9701 DECL_TI_ARGS appropriately. In particular, they need a
9702 new set of outer arguments, corresponding to the
9703 arguments for this class instantiation.
9705 The same situation can arise with something like this:
9707 friend void f(int);
9708 template <class T> class C {
9709 friend void f(T) {}
9712 when `C<int>' is instantiated. Now, `f(int)' is defined
9713 in the class. */
9715 if (!new_friend_is_defn)
9716 /* On the other hand, if the in-class declaration does
9717 *not* provide a definition, then we don't want to alter
9718 existing definitions. We can just leave everything
9719 alone. */
9721 else
9723 tree new_template = TI_TEMPLATE (new_friend_template_info);
9724 tree new_args = TI_ARGS (new_friend_template_info);
9726 /* Overwrite whatever template info was there before, if
9727 any, with the new template information pertaining to
9728 the declaration. */
9729 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9731 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9733 /* We should have called reregister_specialization in
9734 duplicate_decls. */
9735 gcc_assert (retrieve_specialization (new_template,
9736 new_args, 0)
9737 == old_decl);
9739 /* Instantiate it if the global has already been used. */
9740 if (DECL_ODR_USED (old_decl))
9741 instantiate_decl (old_decl, /*defer_ok=*/true,
9742 /*expl_inst_class_mem_p=*/false);
9744 else
9746 tree t;
9748 /* Indicate that the old function template is a partial
9749 instantiation. */
9750 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9751 = new_friend_result_template_info;
9753 gcc_assert (new_template
9754 == most_general_template (new_template));
9755 gcc_assert (new_template != old_decl);
9757 /* Reassign any specializations already in the hash table
9758 to the new more general template, and add the
9759 additional template args. */
9760 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9761 t != NULL_TREE;
9762 t = TREE_CHAIN (t))
9764 tree spec = TREE_VALUE (t);
9765 spec_entry elt;
9767 elt.tmpl = old_decl;
9768 elt.args = DECL_TI_ARGS (spec);
9769 elt.spec = NULL_TREE;
9771 decl_specializations->remove_elt (&elt);
9773 DECL_TI_ARGS (spec)
9774 = add_outermost_template_args (new_args,
9775 DECL_TI_ARGS (spec));
9777 register_specialization
9778 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9781 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9785 /* The information from NEW_FRIEND has been merged into OLD_DECL
9786 by duplicate_decls. */
9787 new_friend = old_decl;
9790 else
9792 tree context = DECL_CONTEXT (new_friend);
9793 bool dependent_p;
9795 /* In the code
9796 template <class T> class C {
9797 template <class U> friend void C1<U>::f (); // case 1
9798 friend void C2<T>::f (); // case 2
9800 we only need to make sure CONTEXT is a complete type for
9801 case 2. To distinguish between the two cases, we note that
9802 CONTEXT of case 1 remains dependent type after tsubst while
9803 this isn't true for case 2. */
9804 ++processing_template_decl;
9805 dependent_p = dependent_type_p (context);
9806 --processing_template_decl;
9808 if (!dependent_p
9809 && !complete_type_or_else (context, NULL_TREE))
9810 return error_mark_node;
9812 if (COMPLETE_TYPE_P (context))
9814 tree fn = new_friend;
9815 /* do_friend adds the TEMPLATE_DECL for any member friend
9816 template even if it isn't a member template, i.e.
9817 template <class T> friend A<T>::f();
9818 Look through it in that case. */
9819 if (TREE_CODE (fn) == TEMPLATE_DECL
9820 && !PRIMARY_TEMPLATE_P (fn))
9821 fn = DECL_TEMPLATE_RESULT (fn);
9822 /* Check to see that the declaration is really present, and,
9823 possibly obtain an improved declaration. */
9824 fn = check_classfn (context, fn, NULL_TREE);
9826 if (fn)
9827 new_friend = fn;
9831 return new_friend;
9834 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9835 template arguments, as for tsubst.
9837 Returns an appropriate tsubst'd friend type or error_mark_node on
9838 failure. */
9840 static tree
9841 tsubst_friend_class (tree friend_tmpl, tree args)
9843 tree friend_type;
9844 tree tmpl;
9845 tree context;
9847 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9849 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9850 return TREE_TYPE (t);
9853 context = CP_DECL_CONTEXT (friend_tmpl);
9855 if (context != global_namespace)
9857 if (TREE_CODE (context) == NAMESPACE_DECL)
9858 push_nested_namespace (context);
9859 else
9860 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9863 /* Look for a class template declaration. We look for hidden names
9864 because two friend declarations of the same template are the
9865 same. For example, in:
9867 struct A {
9868 template <typename> friend class F;
9870 template <typename> struct B {
9871 template <typename> friend class F;
9874 both F templates are the same. */
9875 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9876 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9878 /* But, if we don't find one, it might be because we're in a
9879 situation like this:
9881 template <class T>
9882 struct S {
9883 template <class U>
9884 friend struct S;
9887 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9888 for `S<int>', not the TEMPLATE_DECL. */
9889 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9891 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9892 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9895 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9897 /* The friend template has already been declared. Just
9898 check to see that the declarations match, and install any new
9899 default parameters. We must tsubst the default parameters,
9900 of course. We only need the innermost template parameters
9901 because that is all that redeclare_class_template will look
9902 at. */
9903 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9904 > TMPL_ARGS_DEPTH (args))
9906 tree parms;
9907 location_t saved_input_location;
9908 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9909 args, tf_warning_or_error);
9911 saved_input_location = input_location;
9912 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9913 tree cons = get_constraints (tmpl);
9914 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9915 input_location = saved_input_location;
9919 friend_type = TREE_TYPE (tmpl);
9921 else
9923 /* The friend template has not already been declared. In this
9924 case, the instantiation of the template class will cause the
9925 injection of this template into the global scope. */
9926 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9927 if (tmpl == error_mark_node)
9928 return error_mark_node;
9930 /* The new TMPL is not an instantiation of anything, so we
9931 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9932 the new type because that is supposed to be the corresponding
9933 template decl, i.e., TMPL. */
9934 DECL_USE_TEMPLATE (tmpl) = 0;
9935 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9936 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9937 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9938 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9940 /* Inject this template into the global scope. */
9941 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9944 if (context != global_namespace)
9946 if (TREE_CODE (context) == NAMESPACE_DECL)
9947 pop_nested_namespace (context);
9948 else
9949 pop_nested_class ();
9952 return friend_type;
9955 /* Returns zero if TYPE cannot be completed later due to circularity.
9956 Otherwise returns one. */
9958 static int
9959 can_complete_type_without_circularity (tree type)
9961 if (type == NULL_TREE || type == error_mark_node)
9962 return 0;
9963 else if (COMPLETE_TYPE_P (type))
9964 return 1;
9965 else if (TREE_CODE (type) == ARRAY_TYPE)
9966 return can_complete_type_without_circularity (TREE_TYPE (type));
9967 else if (CLASS_TYPE_P (type)
9968 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9969 return 0;
9970 else
9971 return 1;
9974 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9975 tsubst_flags_t, tree);
9977 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9978 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9980 static tree
9981 tsubst_attribute (tree t, tree *decl_p, tree args,
9982 tsubst_flags_t complain, tree in_decl)
9984 gcc_assert (ATTR_IS_DEPENDENT (t));
9986 tree val = TREE_VALUE (t);
9987 if (val == NULL_TREE)
9988 /* Nothing to do. */;
9989 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9990 && is_attribute_p ("omp declare simd",
9991 get_attribute_name (t)))
9993 tree clauses = TREE_VALUE (val);
9994 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
9995 complain, in_decl);
9996 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9997 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
9998 tree parms = DECL_ARGUMENTS (*decl_p);
9999 clauses
10000 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10001 if (clauses)
10002 val = build_tree_list (NULL_TREE, clauses);
10003 else
10004 val = NULL_TREE;
10006 /* If the first attribute argument is an identifier, don't
10007 pass it through tsubst. Attributes like mode, format,
10008 cleanup and several target specific attributes expect it
10009 unmodified. */
10010 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10012 tree chain
10013 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10014 /*integral_constant_expression_p=*/false);
10015 if (chain != TREE_CHAIN (val))
10016 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10018 else if (PACK_EXPANSION_P (val))
10020 /* An attribute pack expansion. */
10021 tree purp = TREE_PURPOSE (t);
10022 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10023 if (pack == error_mark_node)
10024 return error_mark_node;
10025 int len = TREE_VEC_LENGTH (pack);
10026 tree list = NULL_TREE;
10027 tree *q = &list;
10028 for (int i = 0; i < len; ++i)
10030 tree elt = TREE_VEC_ELT (pack, i);
10031 *q = build_tree_list (purp, elt);
10032 q = &TREE_CHAIN (*q);
10034 return list;
10036 else
10037 val = tsubst_expr (val, args, complain, in_decl,
10038 /*integral_constant_expression_p=*/false);
10040 if (val != TREE_VALUE (t))
10041 return build_tree_list (TREE_PURPOSE (t), val);
10042 return t;
10045 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10046 unchanged or a new TREE_LIST chain. */
10048 static tree
10049 tsubst_attributes (tree attributes, tree args,
10050 tsubst_flags_t complain, tree in_decl)
10052 tree last_dep = NULL_TREE;
10054 for (tree t = attributes; t; t = TREE_CHAIN (t))
10055 if (ATTR_IS_DEPENDENT (t))
10057 last_dep = t;
10058 attributes = copy_list (attributes);
10059 break;
10062 if (last_dep)
10063 for (tree *p = &attributes; *p; )
10065 tree t = *p;
10066 if (ATTR_IS_DEPENDENT (t))
10068 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10069 if (subst != t)
10071 *p = subst;
10073 p = &TREE_CHAIN (*p);
10074 while (*p);
10075 *p = TREE_CHAIN (t);
10076 continue;
10079 p = &TREE_CHAIN (*p);
10082 return attributes;
10085 /* Apply any attributes which had to be deferred until instantiation
10086 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10087 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10089 static void
10090 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10091 tree args, tsubst_flags_t complain, tree in_decl)
10093 tree last_dep = NULL_TREE;
10094 tree t;
10095 tree *p;
10097 if (attributes == NULL_TREE)
10098 return;
10100 if (DECL_P (*decl_p))
10102 if (TREE_TYPE (*decl_p) == error_mark_node)
10103 return;
10104 p = &DECL_ATTRIBUTES (*decl_p);
10105 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10106 to our attributes parameter. */
10107 gcc_assert (*p == attributes);
10109 else
10111 p = &TYPE_ATTRIBUTES (*decl_p);
10112 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10113 lookup_template_class_1, and should be preserved. */
10114 gcc_assert (*p != attributes);
10115 while (*p)
10116 p = &TREE_CHAIN (*p);
10119 for (t = attributes; t; t = TREE_CHAIN (t))
10120 if (ATTR_IS_DEPENDENT (t))
10122 last_dep = t;
10123 attributes = copy_list (attributes);
10124 break;
10127 *p = attributes;
10128 if (last_dep)
10130 tree late_attrs = NULL_TREE;
10131 tree *q = &late_attrs;
10133 for (; *p; )
10135 t = *p;
10136 if (ATTR_IS_DEPENDENT (t))
10138 *p = TREE_CHAIN (t);
10139 TREE_CHAIN (t) = NULL_TREE;
10140 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10142 q = &TREE_CHAIN (*q);
10143 while (*q);
10145 else
10146 p = &TREE_CHAIN (t);
10149 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10153 /* Perform (or defer) access check for typedefs that were referenced
10154 from within the template TMPL code.
10155 This is a subroutine of instantiate_decl and instantiate_class_template.
10156 TMPL is the template to consider and TARGS is the list of arguments of
10157 that template. */
10159 static void
10160 perform_typedefs_access_check (tree tmpl, tree targs)
10162 location_t saved_location;
10163 unsigned i;
10164 qualified_typedef_usage_t *iter;
10166 if (!tmpl
10167 || (!CLASS_TYPE_P (tmpl)
10168 && TREE_CODE (tmpl) != FUNCTION_DECL))
10169 return;
10171 saved_location = input_location;
10172 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10174 tree type_decl = iter->typedef_decl;
10175 tree type_scope = iter->context;
10177 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10178 continue;
10180 if (uses_template_parms (type_decl))
10181 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10182 if (uses_template_parms (type_scope))
10183 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10185 /* Make access check error messages point to the location
10186 of the use of the typedef. */
10187 input_location = iter->locus;
10188 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10189 type_decl, type_decl,
10190 tf_warning_or_error);
10192 input_location = saved_location;
10195 static tree
10196 instantiate_class_template_1 (tree type)
10198 tree templ, args, pattern, t, member;
10199 tree typedecl;
10200 tree pbinfo;
10201 tree base_list;
10202 unsigned int saved_maximum_field_alignment;
10203 tree fn_context;
10205 if (type == error_mark_node)
10206 return error_mark_node;
10208 if (COMPLETE_OR_OPEN_TYPE_P (type)
10209 || uses_template_parms (type))
10210 return type;
10212 /* Figure out which template is being instantiated. */
10213 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10214 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10216 /* Determine what specialization of the original template to
10217 instantiate. */
10218 t = most_specialized_partial_spec (type, tf_warning_or_error);
10219 if (t == error_mark_node)
10221 TYPE_BEING_DEFINED (type) = 1;
10222 return error_mark_node;
10224 else if (t)
10226 /* This TYPE is actually an instantiation of a partial
10227 specialization. We replace the innermost set of ARGS with
10228 the arguments appropriate for substitution. For example,
10229 given:
10231 template <class T> struct S {};
10232 template <class T> struct S<T*> {};
10234 and supposing that we are instantiating S<int*>, ARGS will
10235 presently be {int*} -- but we need {int}. */
10236 pattern = TREE_TYPE (t);
10237 args = TREE_PURPOSE (t);
10239 else
10241 pattern = TREE_TYPE (templ);
10242 args = CLASSTYPE_TI_ARGS (type);
10245 /* If the template we're instantiating is incomplete, then clearly
10246 there's nothing we can do. */
10247 if (!COMPLETE_TYPE_P (pattern))
10248 return type;
10250 /* If we've recursively instantiated too many templates, stop. */
10251 if (! push_tinst_level (type))
10252 return type;
10254 /* Now we're really doing the instantiation. Mark the type as in
10255 the process of being defined. */
10256 TYPE_BEING_DEFINED (type) = 1;
10258 /* We may be in the middle of deferred access check. Disable
10259 it now. */
10260 push_deferring_access_checks (dk_no_deferred);
10262 int saved_unevaluated_operand = cp_unevaluated_operand;
10263 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10265 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10266 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10267 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10268 fn_context = error_mark_node;
10269 if (!fn_context)
10270 push_to_top_level ();
10271 else
10273 cp_unevaluated_operand = 0;
10274 c_inhibit_evaluation_warnings = 0;
10276 /* Use #pragma pack from the template context. */
10277 saved_maximum_field_alignment = maximum_field_alignment;
10278 maximum_field_alignment = TYPE_PRECISION (pattern);
10280 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10282 /* Set the input location to the most specialized template definition.
10283 This is needed if tsubsting causes an error. */
10284 typedecl = TYPE_MAIN_DECL (pattern);
10285 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10286 DECL_SOURCE_LOCATION (typedecl);
10288 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10289 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10290 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10291 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10292 if (ANON_AGGR_TYPE_P (pattern))
10293 SET_ANON_AGGR_TYPE_P (type);
10294 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10296 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10297 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10298 /* Adjust visibility for template arguments. */
10299 determine_visibility (TYPE_MAIN_DECL (type));
10301 if (CLASS_TYPE_P (type))
10302 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10304 pbinfo = TYPE_BINFO (pattern);
10306 /* We should never instantiate a nested class before its enclosing
10307 class; we need to look up the nested class by name before we can
10308 instantiate it, and that lookup should instantiate the enclosing
10309 class. */
10310 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10311 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10313 base_list = NULL_TREE;
10314 if (BINFO_N_BASE_BINFOS (pbinfo))
10316 tree pbase_binfo;
10317 tree pushed_scope;
10318 int i;
10320 /* We must enter the scope containing the type, as that is where
10321 the accessibility of types named in dependent bases are
10322 looked up from. */
10323 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10325 /* Substitute into each of the bases to determine the actual
10326 basetypes. */
10327 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10329 tree base;
10330 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10331 tree expanded_bases = NULL_TREE;
10332 int idx, len = 1;
10334 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10336 expanded_bases =
10337 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10338 args, tf_error, NULL_TREE);
10339 if (expanded_bases == error_mark_node)
10340 continue;
10342 len = TREE_VEC_LENGTH (expanded_bases);
10345 for (idx = 0; idx < len; idx++)
10347 if (expanded_bases)
10348 /* Extract the already-expanded base class. */
10349 base = TREE_VEC_ELT (expanded_bases, idx);
10350 else
10351 /* Substitute to figure out the base class. */
10352 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10353 NULL_TREE);
10355 if (base == error_mark_node)
10356 continue;
10358 base_list = tree_cons (access, base, base_list);
10359 if (BINFO_VIRTUAL_P (pbase_binfo))
10360 TREE_TYPE (base_list) = integer_type_node;
10364 /* The list is now in reverse order; correct that. */
10365 base_list = nreverse (base_list);
10367 if (pushed_scope)
10368 pop_scope (pushed_scope);
10370 /* Now call xref_basetypes to set up all the base-class
10371 information. */
10372 xref_basetypes (type, base_list);
10374 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10375 (int) ATTR_FLAG_TYPE_IN_PLACE,
10376 args, tf_error, NULL_TREE);
10377 fixup_attribute_variants (type);
10379 /* Now that our base classes are set up, enter the scope of the
10380 class, so that name lookups into base classes, etc. will work
10381 correctly. This is precisely analogous to what we do in
10382 begin_class_definition when defining an ordinary non-template
10383 class, except we also need to push the enclosing classes. */
10384 push_nested_class (type);
10386 /* Now members are processed in the order of declaration. */
10387 for (member = CLASSTYPE_DECL_LIST (pattern);
10388 member; member = TREE_CHAIN (member))
10390 tree t = TREE_VALUE (member);
10392 if (TREE_PURPOSE (member))
10394 if (TYPE_P (t))
10396 /* Build new CLASSTYPE_NESTED_UTDS. */
10398 tree newtag;
10399 bool class_template_p;
10401 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10402 && TYPE_LANG_SPECIFIC (t)
10403 && CLASSTYPE_IS_TEMPLATE (t));
10404 /* If the member is a class template, then -- even after
10405 substitution -- there may be dependent types in the
10406 template argument list for the class. We increment
10407 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10408 that function will assume that no types are dependent
10409 when outside of a template. */
10410 if (class_template_p)
10411 ++processing_template_decl;
10412 newtag = tsubst (t, args, tf_error, NULL_TREE);
10413 if (class_template_p)
10414 --processing_template_decl;
10415 if (newtag == error_mark_node)
10416 continue;
10418 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10420 tree name = TYPE_IDENTIFIER (t);
10422 if (class_template_p)
10423 /* Unfortunately, lookup_template_class sets
10424 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10425 instantiation (i.e., for the type of a member
10426 template class nested within a template class.)
10427 This behavior is required for
10428 maybe_process_partial_specialization to work
10429 correctly, but is not accurate in this case;
10430 the TAG is not an instantiation of anything.
10431 (The corresponding TEMPLATE_DECL is an
10432 instantiation, but the TYPE is not.) */
10433 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10435 /* Now, we call pushtag to put this NEWTAG into the scope of
10436 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10437 pushtag calling push_template_decl. We don't have to do
10438 this for enums because it will already have been done in
10439 tsubst_enum. */
10440 if (name)
10441 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10442 pushtag (name, newtag, /*tag_scope=*/ts_current);
10445 else if (DECL_DECLARES_FUNCTION_P (t))
10447 /* Build new TYPE_METHODS. */
10448 tree r;
10450 if (TREE_CODE (t) == TEMPLATE_DECL)
10451 ++processing_template_decl;
10452 r = tsubst (t, args, tf_error, NULL_TREE);
10453 if (TREE_CODE (t) == TEMPLATE_DECL)
10454 --processing_template_decl;
10455 set_current_access_from_decl (r);
10456 finish_member_declaration (r);
10457 /* Instantiate members marked with attribute used. */
10458 if (r != error_mark_node && DECL_PRESERVE_P (r))
10459 mark_used (r);
10460 if (TREE_CODE (r) == FUNCTION_DECL
10461 && DECL_OMP_DECLARE_REDUCTION_P (r))
10462 cp_check_omp_declare_reduction (r);
10464 else if (DECL_CLASS_TEMPLATE_P (t)
10465 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10466 /* A closure type for a lambda in a default argument for a
10467 member template. Ignore it; it will be instantiated with
10468 the default argument. */;
10469 else
10471 /* Build new TYPE_FIELDS. */
10472 if (TREE_CODE (t) == STATIC_ASSERT)
10474 tree condition;
10476 ++c_inhibit_evaluation_warnings;
10477 condition =
10478 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10479 tf_warning_or_error, NULL_TREE,
10480 /*integral_constant_expression_p=*/true);
10481 --c_inhibit_evaluation_warnings;
10483 finish_static_assert (condition,
10484 STATIC_ASSERT_MESSAGE (t),
10485 STATIC_ASSERT_SOURCE_LOCATION (t),
10486 /*member_p=*/true);
10488 else if (TREE_CODE (t) != CONST_DECL)
10490 tree r;
10491 tree vec = NULL_TREE;
10492 int len = 1;
10494 /* The file and line for this declaration, to
10495 assist in error message reporting. Since we
10496 called push_tinst_level above, we don't need to
10497 restore these. */
10498 input_location = DECL_SOURCE_LOCATION (t);
10500 if (TREE_CODE (t) == TEMPLATE_DECL)
10501 ++processing_template_decl;
10502 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10503 if (TREE_CODE (t) == TEMPLATE_DECL)
10504 --processing_template_decl;
10506 if (TREE_CODE (r) == TREE_VEC)
10508 /* A capture pack became multiple fields. */
10509 vec = r;
10510 len = TREE_VEC_LENGTH (vec);
10513 for (int i = 0; i < len; ++i)
10515 if (vec)
10516 r = TREE_VEC_ELT (vec, i);
10517 if (VAR_P (r))
10519 /* In [temp.inst]:
10521 [t]he initialization (and any associated
10522 side-effects) of a static data member does
10523 not occur unless the static data member is
10524 itself used in a way that requires the
10525 definition of the static data member to
10526 exist.
10528 Therefore, we do not substitute into the
10529 initialized for the static data member here. */
10530 finish_static_data_member_decl
10532 /*init=*/NULL_TREE,
10533 /*init_const_expr_p=*/false,
10534 /*asmspec_tree=*/NULL_TREE,
10535 /*flags=*/0);
10536 /* Instantiate members marked with attribute used. */
10537 if (r != error_mark_node && DECL_PRESERVE_P (r))
10538 mark_used (r);
10540 else if (TREE_CODE (r) == FIELD_DECL)
10542 /* Determine whether R has a valid type and can be
10543 completed later. If R is invalid, then its type
10544 is replaced by error_mark_node. */
10545 tree rtype = TREE_TYPE (r);
10546 if (can_complete_type_without_circularity (rtype))
10547 complete_type (rtype);
10549 if (!complete_or_array_type_p (rtype))
10551 /* If R's type couldn't be completed and
10552 it isn't a flexible array member (whose
10553 type is incomplete by definition) give
10554 an error. */
10555 cxx_incomplete_type_error (r, rtype);
10556 TREE_TYPE (r) = error_mark_node;
10560 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10561 such a thing will already have been added to the field
10562 list by tsubst_enum in finish_member_declaration in the
10563 CLASSTYPE_NESTED_UTDS case above. */
10564 if (!(TREE_CODE (r) == TYPE_DECL
10565 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10566 && DECL_ARTIFICIAL (r)))
10568 set_current_access_from_decl (r);
10569 finish_member_declaration (r);
10575 else
10577 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10578 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10580 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10582 tree friend_type = t;
10583 bool adjust_processing_template_decl = false;
10585 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10587 /* template <class T> friend class C; */
10588 friend_type = tsubst_friend_class (friend_type, args);
10589 adjust_processing_template_decl = true;
10591 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10593 /* template <class T> friend class C::D; */
10594 friend_type = tsubst (friend_type, args,
10595 tf_warning_or_error, NULL_TREE);
10596 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10597 friend_type = TREE_TYPE (friend_type);
10598 adjust_processing_template_decl = true;
10600 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10601 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10603 /* This could be either
10605 friend class T::C;
10607 when dependent_type_p is false or
10609 template <class U> friend class T::C;
10611 otherwise. */
10612 /* Bump processing_template_decl in case this is something like
10613 template <class T> friend struct A<T>::B. */
10614 ++processing_template_decl;
10615 friend_type = tsubst (friend_type, args,
10616 tf_warning_or_error, NULL_TREE);
10617 if (dependent_type_p (friend_type))
10618 adjust_processing_template_decl = true;
10619 --processing_template_decl;
10621 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10622 && hidden_name_p (TYPE_NAME (friend_type)))
10624 /* friend class C;
10626 where C hasn't been declared yet. Let's lookup name
10627 from namespace scope directly, bypassing any name that
10628 come from dependent base class. */
10629 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10631 /* The call to xref_tag_from_type does injection for friend
10632 classes. */
10633 push_nested_namespace (ns);
10634 friend_type =
10635 xref_tag_from_type (friend_type, NULL_TREE,
10636 /*tag_scope=*/ts_current);
10637 pop_nested_namespace (ns);
10639 else if (uses_template_parms (friend_type))
10640 /* friend class C<T>; */
10641 friend_type = tsubst (friend_type, args,
10642 tf_warning_or_error, NULL_TREE);
10643 /* Otherwise it's
10645 friend class C;
10647 where C is already declared or
10649 friend class C<int>;
10651 We don't have to do anything in these cases. */
10653 if (adjust_processing_template_decl)
10654 /* Trick make_friend_class into realizing that the friend
10655 we're adding is a template, not an ordinary class. It's
10656 important that we use make_friend_class since it will
10657 perform some error-checking and output cross-reference
10658 information. */
10659 ++processing_template_decl;
10661 if (friend_type != error_mark_node)
10662 make_friend_class (type, friend_type, /*complain=*/false);
10664 if (adjust_processing_template_decl)
10665 --processing_template_decl;
10667 else
10669 /* Build new DECL_FRIENDLIST. */
10670 tree r;
10672 /* The file and line for this declaration, to
10673 assist in error message reporting. Since we
10674 called push_tinst_level above, we don't need to
10675 restore these. */
10676 input_location = DECL_SOURCE_LOCATION (t);
10678 if (TREE_CODE (t) == TEMPLATE_DECL)
10680 ++processing_template_decl;
10681 push_deferring_access_checks (dk_no_check);
10684 r = tsubst_friend_function (t, args);
10685 add_friend (type, r, /*complain=*/false);
10686 if (TREE_CODE (t) == TEMPLATE_DECL)
10688 pop_deferring_access_checks ();
10689 --processing_template_decl;
10695 if (fn_context)
10697 /* Restore these before substituting into the lambda capture
10698 initializers. */
10699 cp_unevaluated_operand = saved_unevaluated_operand;
10700 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10703 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10705 tree decl = lambda_function (type);
10706 if (decl)
10708 if (cxx_dialect >= cxx1z)
10709 CLASSTYPE_LITERAL_P (type) = true;
10711 if (!DECL_TEMPLATE_INFO (decl)
10712 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10714 /* Set function_depth to avoid garbage collection. */
10715 ++function_depth;
10716 instantiate_decl (decl, /*defer_ok=*/false, false);
10717 --function_depth;
10720 /* We need to instantiate the capture list from the template
10721 after we've instantiated the closure members, but before we
10722 consider adding the conversion op. Also keep any captures
10723 that may have been added during instantiation of the op(). */
10724 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10725 tree tmpl_cap
10726 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10727 args, tf_warning_or_error, NULL_TREE,
10728 false, false);
10730 LAMBDA_EXPR_CAPTURE_LIST (expr)
10731 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10733 maybe_add_lambda_conv_op (type);
10735 else
10736 gcc_assert (errorcount);
10739 /* Set the file and line number information to whatever is given for
10740 the class itself. This puts error messages involving generated
10741 implicit functions at a predictable point, and the same point
10742 that would be used for non-template classes. */
10743 input_location = DECL_SOURCE_LOCATION (typedecl);
10745 unreverse_member_declarations (type);
10746 finish_struct_1 (type);
10747 TYPE_BEING_DEFINED (type) = 0;
10749 /* We don't instantiate default arguments for member functions. 14.7.1:
10751 The implicit instantiation of a class template specialization causes
10752 the implicit instantiation of the declarations, but not of the
10753 definitions or default arguments, of the class member functions,
10754 member classes, static data members and member templates.... */
10756 /* Some typedefs referenced from within the template code need to be access
10757 checked at template instantiation time, i.e now. These types were
10758 added to the template at parsing time. Let's get those and perform
10759 the access checks then. */
10760 perform_typedefs_access_check (pattern, args);
10761 perform_deferred_access_checks (tf_warning_or_error);
10762 pop_nested_class ();
10763 maximum_field_alignment = saved_maximum_field_alignment;
10764 if (!fn_context)
10765 pop_from_top_level ();
10766 pop_deferring_access_checks ();
10767 pop_tinst_level ();
10769 /* The vtable for a template class can be emitted in any translation
10770 unit in which the class is instantiated. When there is no key
10771 method, however, finish_struct_1 will already have added TYPE to
10772 the keyed_classes list. */
10773 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10774 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10776 return type;
10779 /* Wrapper for instantiate_class_template_1. */
10781 tree
10782 instantiate_class_template (tree type)
10784 tree ret;
10785 timevar_push (TV_TEMPLATE_INST);
10786 ret = instantiate_class_template_1 (type);
10787 timevar_pop (TV_TEMPLATE_INST);
10788 return ret;
10791 static tree
10792 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10794 tree r;
10796 if (!t)
10797 r = t;
10798 else if (TYPE_P (t))
10799 r = tsubst (t, args, complain, in_decl);
10800 else
10802 if (!(complain & tf_warning))
10803 ++c_inhibit_evaluation_warnings;
10804 r = tsubst_expr (t, args, complain, in_decl,
10805 /*integral_constant_expression_p=*/true);
10806 if (!(complain & tf_warning))
10807 --c_inhibit_evaluation_warnings;
10809 return r;
10812 /* Given a function parameter pack TMPL_PARM and some function parameters
10813 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10814 and set *SPEC_P to point at the next point in the list. */
10816 tree
10817 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10819 /* Collect all of the extra "packed" parameters into an
10820 argument pack. */
10821 tree parmvec;
10822 tree parmtypevec;
10823 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10824 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10825 tree spec_parm = *spec_p;
10826 int i, len;
10828 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10829 if (tmpl_parm
10830 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10831 break;
10833 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10834 parmvec = make_tree_vec (len);
10835 parmtypevec = make_tree_vec (len);
10836 spec_parm = *spec_p;
10837 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10839 TREE_VEC_ELT (parmvec, i) = spec_parm;
10840 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10843 /* Build the argument packs. */
10844 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10845 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10846 TREE_TYPE (argpack) = argtypepack;
10847 *spec_p = spec_parm;
10849 return argpack;
10852 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10853 NONTYPE_ARGUMENT_PACK. */
10855 static tree
10856 make_fnparm_pack (tree spec_parm)
10858 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10861 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10862 pack expansion with no extra args, 2 if it has extra args, or 0
10863 if it is not a pack expansion. */
10865 static int
10866 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10868 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10869 if (i >= TREE_VEC_LENGTH (vec))
10870 return 0;
10871 tree elt = TREE_VEC_ELT (vec, i);
10872 if (DECL_P (elt))
10873 /* A decl pack is itself an expansion. */
10874 elt = TREE_TYPE (elt);
10875 if (!PACK_EXPANSION_P (elt))
10876 return 0;
10877 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10878 return 2;
10879 return 1;
10883 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10885 static tree
10886 make_argument_pack_select (tree arg_pack, unsigned index)
10888 tree aps = make_node (ARGUMENT_PACK_SELECT);
10890 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10891 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10893 return aps;
10896 /* This is a subroutine of tsubst_pack_expansion.
10898 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10899 mechanism to store the (non complete list of) arguments of the
10900 substitution and return a non substituted pack expansion, in order
10901 to wait for when we have enough arguments to really perform the
10902 substitution. */
10904 static bool
10905 use_pack_expansion_extra_args_p (tree parm_packs,
10906 int arg_pack_len,
10907 bool has_empty_arg)
10909 /* If one pack has an expansion and another pack has a normal
10910 argument or if one pack has an empty argument and an another
10911 one hasn't then tsubst_pack_expansion cannot perform the
10912 substitution and need to fall back on the
10913 PACK_EXPANSION_EXTRA mechanism. */
10914 if (parm_packs == NULL_TREE)
10915 return false;
10916 else if (has_empty_arg)
10917 return true;
10919 bool has_expansion_arg = false;
10920 for (int i = 0 ; i < arg_pack_len; ++i)
10922 bool has_non_expansion_arg = false;
10923 for (tree parm_pack = parm_packs;
10924 parm_pack;
10925 parm_pack = TREE_CHAIN (parm_pack))
10927 tree arg = TREE_VALUE (parm_pack);
10929 int exp = argument_pack_element_is_expansion_p (arg, i);
10930 if (exp == 2)
10931 /* We can't substitute a pack expansion with extra args into
10932 our pattern. */
10933 return true;
10934 else if (exp)
10935 has_expansion_arg = true;
10936 else
10937 has_non_expansion_arg = true;
10940 if (has_expansion_arg && has_non_expansion_arg)
10941 return true;
10943 return false;
10946 /* [temp.variadic]/6 says that:
10948 The instantiation of a pack expansion [...]
10949 produces a list E1,E2, ..., En, where N is the number of elements
10950 in the pack expansion parameters.
10952 This subroutine of tsubst_pack_expansion produces one of these Ei.
10954 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10955 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10956 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10957 INDEX is the index 'i' of the element Ei to produce. ARGS,
10958 COMPLAIN, and IN_DECL are the same parameters as for the
10959 tsubst_pack_expansion function.
10961 The function returns the resulting Ei upon successful completion,
10962 or error_mark_node.
10964 Note that this function possibly modifies the ARGS parameter, so
10965 it's the responsibility of the caller to restore it. */
10967 static tree
10968 gen_elem_of_pack_expansion_instantiation (tree pattern,
10969 tree parm_packs,
10970 unsigned index,
10971 tree args /* This parm gets
10972 modified. */,
10973 tsubst_flags_t complain,
10974 tree in_decl)
10976 tree t;
10977 bool ith_elem_is_expansion = false;
10979 /* For each parameter pack, change the substitution of the parameter
10980 pack to the ith argument in its argument pack, then expand the
10981 pattern. */
10982 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10984 tree parm = TREE_PURPOSE (pack);
10985 tree arg_pack = TREE_VALUE (pack);
10986 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10988 ith_elem_is_expansion |=
10989 argument_pack_element_is_expansion_p (arg_pack, index);
10991 /* Select the Ith argument from the pack. */
10992 if (TREE_CODE (parm) == PARM_DECL
10993 || TREE_CODE (parm) == FIELD_DECL)
10995 if (index == 0)
10997 aps = make_argument_pack_select (arg_pack, index);
10998 if (!mark_used (parm, complain) && !(complain & tf_error))
10999 return error_mark_node;
11000 register_local_specialization (aps, parm);
11002 else
11003 aps = retrieve_local_specialization (parm);
11005 else
11007 int idx, level;
11008 template_parm_level_and_index (parm, &level, &idx);
11010 if (index == 0)
11012 aps = make_argument_pack_select (arg_pack, index);
11013 /* Update the corresponding argument. */
11014 TMPL_ARG (args, level, idx) = aps;
11016 else
11017 /* Re-use the ARGUMENT_PACK_SELECT. */
11018 aps = TMPL_ARG (args, level, idx);
11020 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11023 /* Substitute into the PATTERN with the (possibly altered)
11024 arguments. */
11025 if (pattern == in_decl)
11026 /* Expanding a fixed parameter pack from
11027 coerce_template_parameter_pack. */
11028 t = tsubst_decl (pattern, args, complain);
11029 else if (pattern == error_mark_node)
11030 t = error_mark_node;
11031 else if (constraint_p (pattern))
11033 if (processing_template_decl)
11034 t = tsubst_constraint (pattern, args, complain, in_decl);
11035 else
11036 t = (constraints_satisfied_p (pattern, args)
11037 ? boolean_true_node : boolean_false_node);
11039 else if (!TYPE_P (pattern))
11040 t = tsubst_expr (pattern, args, complain, in_decl,
11041 /*integral_constant_expression_p=*/false);
11042 else
11043 t = tsubst (pattern, args, complain, in_decl);
11045 /* If the Ith argument pack element is a pack expansion, then
11046 the Ith element resulting from the substituting is going to
11047 be a pack expansion as well. */
11048 if (ith_elem_is_expansion)
11049 t = make_pack_expansion (t);
11051 return t;
11054 /* When the unexpanded parameter pack in a fold expression expands to an empty
11055 sequence, the value of the expression is as follows; the program is
11056 ill-formed if the operator is not listed in this table.
11058 && true
11059 || false
11060 , void() */
11062 tree
11063 expand_empty_fold (tree t, tsubst_flags_t complain)
11065 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11066 if (!FOLD_EXPR_MODIFY_P (t))
11067 switch (code)
11069 case TRUTH_ANDIF_EXPR:
11070 return boolean_true_node;
11071 case TRUTH_ORIF_EXPR:
11072 return boolean_false_node;
11073 case COMPOUND_EXPR:
11074 return void_node;
11075 default:
11076 break;
11079 if (complain & tf_error)
11080 error_at (location_of (t),
11081 "fold of empty expansion over %O", code);
11082 return error_mark_node;
11085 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11086 form an expression that combines the two terms using the
11087 operator of T. */
11089 static tree
11090 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11092 tree op = FOLD_EXPR_OP (t);
11093 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11095 // Handle compound assignment operators.
11096 if (FOLD_EXPR_MODIFY_P (t))
11097 return build_x_modify_expr (input_location, left, code, right, complain);
11099 switch (code)
11101 case COMPOUND_EXPR:
11102 return build_x_compound_expr (input_location, left, right, complain);
11103 case DOTSTAR_EXPR:
11104 return build_m_component_ref (left, right, complain);
11105 default:
11106 return build_x_binary_op (input_location, code,
11107 left, TREE_CODE (left),
11108 right, TREE_CODE (right),
11109 /*overload=*/NULL,
11110 complain);
11114 /* Substitute ARGS into the pack of a fold expression T. */
11116 static inline tree
11117 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11119 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11122 /* Substitute ARGS into the pack of a fold expression T. */
11124 static inline tree
11125 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11127 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11130 /* Expand a PACK of arguments into a grouped as left fold.
11131 Given a pack containing elements A0, A1, ..., An and an
11132 operator @, this builds the expression:
11134 ((A0 @ A1) @ A2) ... @ An
11136 Note that PACK must not be empty.
11138 The operator is defined by the original fold expression T. */
11140 static tree
11141 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11143 tree left = TREE_VEC_ELT (pack, 0);
11144 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11146 tree right = TREE_VEC_ELT (pack, i);
11147 left = fold_expression (t, left, right, complain);
11149 return left;
11152 /* Substitute into a unary left fold expression. */
11154 static tree
11155 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11156 tree in_decl)
11158 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11159 if (pack == error_mark_node)
11160 return error_mark_node;
11161 if (PACK_EXPANSION_P (pack))
11163 tree r = copy_node (t);
11164 FOLD_EXPR_PACK (r) = pack;
11165 return r;
11167 if (TREE_VEC_LENGTH (pack) == 0)
11168 return expand_empty_fold (t, complain);
11169 else
11170 return expand_left_fold (t, pack, complain);
11173 /* Substitute into a binary left fold expression.
11175 Do ths by building a single (non-empty) vector of argumnts and
11176 building the expression from those elements. */
11178 static tree
11179 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11180 tree in_decl)
11182 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11183 if (pack == error_mark_node)
11184 return error_mark_node;
11185 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11186 if (init == error_mark_node)
11187 return error_mark_node;
11189 if (PACK_EXPANSION_P (pack))
11191 tree r = copy_node (t);
11192 FOLD_EXPR_PACK (r) = pack;
11193 FOLD_EXPR_INIT (r) = init;
11194 return r;
11197 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11198 TREE_VEC_ELT (vec, 0) = init;
11199 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11200 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11202 return expand_left_fold (t, vec, complain);
11205 /* Expand a PACK of arguments into a grouped as right fold.
11206 Given a pack containing elementns A0, A1, ..., and an
11207 operator @, this builds the expression:
11209 A0@ ... (An-2 @ (An-1 @ An))
11211 Note that PACK must not be empty.
11213 The operator is defined by the original fold expression T. */
11215 tree
11216 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11218 // Build the expression.
11219 int n = TREE_VEC_LENGTH (pack);
11220 tree right = TREE_VEC_ELT (pack, n - 1);
11221 for (--n; n != 0; --n)
11223 tree left = TREE_VEC_ELT (pack, n - 1);
11224 right = fold_expression (t, left, right, complain);
11226 return right;
11229 /* Substitute into a unary right fold expression. */
11231 static tree
11232 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11233 tree in_decl)
11235 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11236 if (pack == error_mark_node)
11237 return error_mark_node;
11238 if (PACK_EXPANSION_P (pack))
11240 tree r = copy_node (t);
11241 FOLD_EXPR_PACK (r) = pack;
11242 return r;
11244 if (TREE_VEC_LENGTH (pack) == 0)
11245 return expand_empty_fold (t, complain);
11246 else
11247 return expand_right_fold (t, pack, complain);
11250 /* Substitute into a binary right fold expression.
11252 Do ths by building a single (non-empty) vector of arguments and
11253 building the expression from those elements. */
11255 static tree
11256 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11257 tree in_decl)
11259 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11260 if (pack == error_mark_node)
11261 return error_mark_node;
11262 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11263 if (init == error_mark_node)
11264 return error_mark_node;
11266 if (PACK_EXPANSION_P (pack))
11268 tree r = copy_node (t);
11269 FOLD_EXPR_PACK (r) = pack;
11270 FOLD_EXPR_INIT (r) = init;
11271 return r;
11274 int n = TREE_VEC_LENGTH (pack);
11275 tree vec = make_tree_vec (n + 1);
11276 for (int i = 0; i < n; ++i)
11277 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11278 TREE_VEC_ELT (vec, n) = init;
11280 return expand_right_fold (t, vec, complain);
11284 /* Substitute ARGS into T, which is an pack expansion
11285 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11286 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11287 (if only a partial substitution could be performed) or
11288 ERROR_MARK_NODE if there was an error. */
11289 tree
11290 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11291 tree in_decl)
11293 tree pattern;
11294 tree pack, packs = NULL_TREE;
11295 bool unsubstituted_packs = false;
11296 int i, len = -1;
11297 tree result;
11298 hash_map<tree, tree> *saved_local_specializations = NULL;
11299 bool need_local_specializations = false;
11300 int levels;
11302 gcc_assert (PACK_EXPANSION_P (t));
11303 pattern = PACK_EXPANSION_PATTERN (t);
11305 /* Add in any args remembered from an earlier partial instantiation. */
11306 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11308 levels = TMPL_ARGS_DEPTH (args);
11310 /* Determine the argument packs that will instantiate the parameter
11311 packs used in the expansion expression. While we're at it,
11312 compute the number of arguments to be expanded and make sure it
11313 is consistent. */
11314 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11315 pack = TREE_CHAIN (pack))
11317 tree parm_pack = TREE_VALUE (pack);
11318 tree arg_pack = NULL_TREE;
11319 tree orig_arg = NULL_TREE;
11320 int level = 0;
11322 if (TREE_CODE (parm_pack) == BASES)
11324 if (BASES_DIRECT (parm_pack))
11325 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11326 args, complain, in_decl, false));
11327 else
11328 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11329 args, complain, in_decl, false));
11331 if (TREE_CODE (parm_pack) == PARM_DECL)
11333 /* We know we have correct local_specializations if this
11334 expansion is at function scope, or if we're dealing with a
11335 local parameter in a requires expression; for the latter,
11336 tsubst_requires_expr set it up appropriately. */
11337 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11338 arg_pack = retrieve_local_specialization (parm_pack);
11339 else
11340 /* We can't rely on local_specializations for a parameter
11341 name used later in a function declaration (such as in a
11342 late-specified return type). Even if it exists, it might
11343 have the wrong value for a recursive call. */
11344 need_local_specializations = true;
11346 if (!arg_pack)
11348 /* This parameter pack was used in an unevaluated context. Just
11349 make a dummy decl, since it's only used for its type. */
11350 arg_pack = tsubst_decl (parm_pack, args, complain);
11351 if (arg_pack && DECL_PACK_P (arg_pack))
11352 /* Partial instantiation of the parm_pack, we can't build
11353 up an argument pack yet. */
11354 arg_pack = NULL_TREE;
11355 else
11356 arg_pack = make_fnparm_pack (arg_pack);
11359 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11360 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11361 else
11363 int idx;
11364 template_parm_level_and_index (parm_pack, &level, &idx);
11366 if (level <= levels)
11367 arg_pack = TMPL_ARG (args, level, idx);
11370 orig_arg = arg_pack;
11371 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11372 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11374 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11375 /* This can only happen if we forget to expand an argument
11376 pack somewhere else. Just return an error, silently. */
11378 result = make_tree_vec (1);
11379 TREE_VEC_ELT (result, 0) = error_mark_node;
11380 return result;
11383 if (arg_pack)
11385 int my_len =
11386 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11388 /* Don't bother trying to do a partial substitution with
11389 incomplete packs; we'll try again after deduction. */
11390 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11391 return t;
11393 if (len < 0)
11394 len = my_len;
11395 else if (len != my_len)
11397 if (!(complain & tf_error))
11398 /* Fail quietly. */;
11399 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11400 error ("mismatched argument pack lengths while expanding "
11401 "%<%T%>",
11402 pattern);
11403 else
11404 error ("mismatched argument pack lengths while expanding "
11405 "%<%E%>",
11406 pattern);
11407 return error_mark_node;
11410 /* Keep track of the parameter packs and their corresponding
11411 argument packs. */
11412 packs = tree_cons (parm_pack, arg_pack, packs);
11413 TREE_TYPE (packs) = orig_arg;
11415 else
11417 /* We can't substitute for this parameter pack. We use a flag as
11418 well as the missing_level counter because function parameter
11419 packs don't have a level. */
11420 gcc_assert (processing_template_decl);
11421 unsubstituted_packs = true;
11425 /* If the expansion is just T..., return the matching argument pack, unless
11426 we need to call convert_from_reference on all the elements. This is an
11427 important optimization; see c++/68422. */
11428 if (!unsubstituted_packs
11429 && TREE_PURPOSE (packs) == pattern)
11431 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11432 /* Types need no adjustment, nor does sizeof..., and if we still have
11433 some pack expansion args we won't do anything yet. */
11434 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11435 || PACK_EXPANSION_SIZEOF_P (t)
11436 || pack_expansion_args_count (args))
11437 return args;
11438 /* Also optimize expression pack expansions if we can tell that the
11439 elements won't have reference type. */
11440 tree type = TREE_TYPE (pattern);
11441 if (type && TREE_CODE (type) != REFERENCE_TYPE
11442 && !PACK_EXPANSION_P (type)
11443 && !WILDCARD_TYPE_P (type))
11444 return args;
11445 /* Otherwise use the normal path so we get convert_from_reference. */
11448 /* We cannot expand this expansion expression, because we don't have
11449 all of the argument packs we need. */
11450 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11452 /* We got some full packs, but we can't substitute them in until we
11453 have values for all the packs. So remember these until then. */
11455 t = make_pack_expansion (pattern);
11456 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11457 return t;
11459 else if (unsubstituted_packs)
11461 /* There were no real arguments, we're just replacing a parameter
11462 pack with another version of itself. Substitute into the
11463 pattern and return a PACK_EXPANSION_*. The caller will need to
11464 deal with that. */
11465 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11466 t = tsubst_expr (pattern, args, complain, in_decl,
11467 /*integral_constant_expression_p=*/false);
11468 else
11469 t = tsubst (pattern, args, complain, in_decl);
11470 t = make_pack_expansion (t);
11471 return t;
11474 gcc_assert (len >= 0);
11476 if (need_local_specializations)
11478 /* We're in a late-specified return type, so create our own local
11479 specializations map; the current map is either NULL or (in the
11480 case of recursive unification) might have bindings that we don't
11481 want to use or alter. */
11482 saved_local_specializations = local_specializations;
11483 local_specializations = new hash_map<tree, tree>;
11486 /* For each argument in each argument pack, substitute into the
11487 pattern. */
11488 result = make_tree_vec (len);
11489 tree elem_args = copy_template_args (args);
11490 for (i = 0; i < len; ++i)
11492 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11494 elem_args, complain,
11495 in_decl);
11496 TREE_VEC_ELT (result, i) = t;
11497 if (t == error_mark_node)
11499 result = error_mark_node;
11500 break;
11504 /* Update ARGS to restore the substitution from parameter packs to
11505 their argument packs. */
11506 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11508 tree parm = TREE_PURPOSE (pack);
11510 if (TREE_CODE (parm) == PARM_DECL
11511 || TREE_CODE (parm) == FIELD_DECL)
11512 register_local_specialization (TREE_TYPE (pack), parm);
11513 else
11515 int idx, level;
11517 if (TREE_VALUE (pack) == NULL_TREE)
11518 continue;
11520 template_parm_level_and_index (parm, &level, &idx);
11522 /* Update the corresponding argument. */
11523 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11524 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11525 TREE_TYPE (pack);
11526 else
11527 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11531 if (need_local_specializations)
11533 delete local_specializations;
11534 local_specializations = saved_local_specializations;
11537 /* If the dependent pack arguments were such that we end up with only a
11538 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11539 if (len == 1 && TREE_CODE (result) == TREE_VEC
11540 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11541 return TREE_VEC_ELT (result, 0);
11543 return result;
11546 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11547 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11548 parameter packs; all parms generated from a function parameter pack will
11549 have the same DECL_PARM_INDEX. */
11551 tree
11552 get_pattern_parm (tree parm, tree tmpl)
11554 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11555 tree patparm;
11557 if (DECL_ARTIFICIAL (parm))
11559 for (patparm = DECL_ARGUMENTS (pattern);
11560 patparm; patparm = DECL_CHAIN (patparm))
11561 if (DECL_ARTIFICIAL (patparm)
11562 && DECL_NAME (parm) == DECL_NAME (patparm))
11563 break;
11565 else
11567 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11568 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11569 gcc_assert (DECL_PARM_INDEX (patparm)
11570 == DECL_PARM_INDEX (parm));
11573 return patparm;
11576 /* Make an argument pack out of the TREE_VEC VEC. */
11578 static tree
11579 make_argument_pack (tree vec)
11581 tree pack;
11582 tree elt = TREE_VEC_ELT (vec, 0);
11583 if (TYPE_P (elt))
11584 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11585 else
11587 pack = make_node (NONTYPE_ARGUMENT_PACK);
11588 TREE_TYPE (pack) = TREE_TYPE (elt);
11589 TREE_CONSTANT (pack) = 1;
11591 SET_ARGUMENT_PACK_ARGS (pack, vec);
11592 return pack;
11595 /* Return an exact copy of template args T that can be modified
11596 independently. */
11598 static tree
11599 copy_template_args (tree t)
11601 if (t == error_mark_node)
11602 return t;
11604 int len = TREE_VEC_LENGTH (t);
11605 tree new_vec = make_tree_vec (len);
11607 for (int i = 0; i < len; ++i)
11609 tree elt = TREE_VEC_ELT (t, i);
11610 if (elt && TREE_CODE (elt) == TREE_VEC)
11611 elt = copy_template_args (elt);
11612 TREE_VEC_ELT (new_vec, i) = elt;
11615 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11616 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11618 return new_vec;
11621 /* Substitute ARGS into the vector or list of template arguments T. */
11623 static tree
11624 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11626 tree orig_t = t;
11627 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11628 tree *elts;
11630 if (t == error_mark_node)
11631 return error_mark_node;
11633 len = TREE_VEC_LENGTH (t);
11634 elts = XALLOCAVEC (tree, len);
11636 for (i = 0; i < len; i++)
11638 tree orig_arg = TREE_VEC_ELT (t, i);
11639 tree new_arg;
11641 if (TREE_CODE (orig_arg) == TREE_VEC)
11642 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11643 else if (PACK_EXPANSION_P (orig_arg))
11645 /* Substitute into an expansion expression. */
11646 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11648 if (TREE_CODE (new_arg) == TREE_VEC)
11649 /* Add to the expanded length adjustment the number of
11650 expanded arguments. We subtract one from this
11651 measurement, because the argument pack expression
11652 itself is already counted as 1 in
11653 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11654 the argument pack is empty. */
11655 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11657 else if (ARGUMENT_PACK_P (orig_arg))
11659 /* Substitute into each of the arguments. */
11660 new_arg = TYPE_P (orig_arg)
11661 ? cxx_make_type (TREE_CODE (orig_arg))
11662 : make_node (TREE_CODE (orig_arg));
11664 SET_ARGUMENT_PACK_ARGS (
11665 new_arg,
11666 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11667 args, complain, in_decl));
11669 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11670 new_arg = error_mark_node;
11672 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11673 if (type_uses_auto (TREE_TYPE (orig_arg)))
11674 TREE_TYPE (new_arg) = TREE_TYPE (orig_arg);
11675 else
11676 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11677 complain, in_decl);
11678 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11680 if (TREE_TYPE (new_arg) == error_mark_node)
11681 new_arg = error_mark_node;
11684 else
11685 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11687 if (new_arg == error_mark_node)
11688 return error_mark_node;
11690 elts[i] = new_arg;
11691 if (new_arg != orig_arg)
11692 need_new = 1;
11695 if (!need_new)
11696 return t;
11698 /* Make space for the expanded arguments coming from template
11699 argument packs. */
11700 t = make_tree_vec (len + expanded_len_adjust);
11701 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11702 arguments for a member template.
11703 In that case each TREE_VEC in ORIG_T represents a level of template
11704 arguments, and ORIG_T won't carry any non defaulted argument count.
11705 It will rather be the nested TREE_VECs that will carry one.
11706 In other words, ORIG_T carries a non defaulted argument count only
11707 if it doesn't contain any nested TREE_VEC. */
11708 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11710 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11711 count += expanded_len_adjust;
11712 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11714 for (i = 0, out = 0; i < len; i++)
11716 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11717 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11718 && TREE_CODE (elts[i]) == TREE_VEC)
11720 int idx;
11722 /* Now expand the template argument pack "in place". */
11723 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11724 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11726 else
11728 TREE_VEC_ELT (t, out) = elts[i];
11729 out++;
11733 return t;
11736 /* Substitute ARGS into one level PARMS of template parameters. */
11738 static tree
11739 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11741 if (parms == error_mark_node)
11742 return error_mark_node;
11744 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11746 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11748 tree tuple = TREE_VEC_ELT (parms, i);
11750 if (tuple == error_mark_node)
11751 continue;
11753 TREE_VEC_ELT (new_vec, i) =
11754 tsubst_template_parm (tuple, args, complain);
11757 return new_vec;
11760 /* Return the result of substituting ARGS into the template parameters
11761 given by PARMS. If there are m levels of ARGS and m + n levels of
11762 PARMS, then the result will contain n levels of PARMS. For
11763 example, if PARMS is `template <class T> template <class U>
11764 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11765 result will be `template <int*, double, class V>'. */
11767 static tree
11768 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11770 tree r = NULL_TREE;
11771 tree* new_parms;
11773 /* When substituting into a template, we must set
11774 PROCESSING_TEMPLATE_DECL as the template parameters may be
11775 dependent if they are based on one-another, and the dependency
11776 predicates are short-circuit outside of templates. */
11777 ++processing_template_decl;
11779 for (new_parms = &r;
11780 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11781 new_parms = &(TREE_CHAIN (*new_parms)),
11782 parms = TREE_CHAIN (parms))
11784 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11785 args, complain);
11786 *new_parms =
11787 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11788 - TMPL_ARGS_DEPTH (args)),
11789 new_vec, NULL_TREE);
11792 --processing_template_decl;
11794 return r;
11797 /* Return the result of substituting ARGS into one template parameter
11798 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11799 parameter and which TREE_PURPOSE is the default argument of the
11800 template parameter. */
11802 static tree
11803 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11805 tree default_value, parm_decl;
11807 if (args == NULL_TREE
11808 || t == NULL_TREE
11809 || t == error_mark_node)
11810 return t;
11812 gcc_assert (TREE_CODE (t) == TREE_LIST);
11814 default_value = TREE_PURPOSE (t);
11815 parm_decl = TREE_VALUE (t);
11817 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11818 if (TREE_CODE (parm_decl) == PARM_DECL
11819 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11820 parm_decl = error_mark_node;
11821 default_value = tsubst_template_arg (default_value, args,
11822 complain, NULL_TREE);
11824 return build_tree_list (default_value, parm_decl);
11827 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11828 type T. If T is not an aggregate or enumeration type, it is
11829 handled as if by tsubst. IN_DECL is as for tsubst. If
11830 ENTERING_SCOPE is nonzero, T is the context for a template which
11831 we are presently tsubst'ing. Return the substituted value. */
11833 static tree
11834 tsubst_aggr_type (tree t,
11835 tree args,
11836 tsubst_flags_t complain,
11837 tree in_decl,
11838 int entering_scope)
11840 if (t == NULL_TREE)
11841 return NULL_TREE;
11843 switch (TREE_CODE (t))
11845 case RECORD_TYPE:
11846 if (TYPE_PTRMEMFUNC_P (t))
11847 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11849 /* Fall through. */
11850 case ENUMERAL_TYPE:
11851 case UNION_TYPE:
11852 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11854 tree argvec;
11855 tree context;
11856 tree r;
11857 int saved_unevaluated_operand;
11858 int saved_inhibit_evaluation_warnings;
11860 /* In "sizeof(X<I>)" we need to evaluate "I". */
11861 saved_unevaluated_operand = cp_unevaluated_operand;
11862 cp_unevaluated_operand = 0;
11863 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11864 c_inhibit_evaluation_warnings = 0;
11866 /* First, determine the context for the type we are looking
11867 up. */
11868 context = TYPE_CONTEXT (t);
11869 if (context && TYPE_P (context))
11871 context = tsubst_aggr_type (context, args, complain,
11872 in_decl, /*entering_scope=*/1);
11873 /* If context is a nested class inside a class template,
11874 it may still need to be instantiated (c++/33959). */
11875 context = complete_type (context);
11878 /* Then, figure out what arguments are appropriate for the
11879 type we are trying to find. For example, given:
11881 template <class T> struct S;
11882 template <class T, class U> void f(T, U) { S<U> su; }
11884 and supposing that we are instantiating f<int, double>,
11885 then our ARGS will be {int, double}, but, when looking up
11886 S we only want {double}. */
11887 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11888 complain, in_decl);
11889 if (argvec == error_mark_node)
11890 r = error_mark_node;
11891 else
11893 r = lookup_template_class (t, argvec, in_decl, context,
11894 entering_scope, complain);
11895 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11898 cp_unevaluated_operand = saved_unevaluated_operand;
11899 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11901 return r;
11903 else
11904 /* This is not a template type, so there's nothing to do. */
11905 return t;
11907 default:
11908 return tsubst (t, args, complain, in_decl);
11912 /* Substitute into the default argument ARG (a default argument for
11913 FN), which has the indicated TYPE. */
11915 tree
11916 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11918 tree saved_class_ptr = NULL_TREE;
11919 tree saved_class_ref = NULL_TREE;
11920 int errs = errorcount + sorrycount;
11922 /* This can happen in invalid code. */
11923 if (TREE_CODE (arg) == DEFAULT_ARG)
11924 return arg;
11926 /* This default argument came from a template. Instantiate the
11927 default argument here, not in tsubst. In the case of
11928 something like:
11930 template <class T>
11931 struct S {
11932 static T t();
11933 void f(T = t());
11936 we must be careful to do name lookup in the scope of S<T>,
11937 rather than in the current class. */
11938 push_access_scope (fn);
11939 /* The "this" pointer is not valid in a default argument. */
11940 if (cfun)
11942 saved_class_ptr = current_class_ptr;
11943 cp_function_chain->x_current_class_ptr = NULL_TREE;
11944 saved_class_ref = current_class_ref;
11945 cp_function_chain->x_current_class_ref = NULL_TREE;
11948 push_deferring_access_checks(dk_no_deferred);
11949 /* The default argument expression may cause implicitly defined
11950 member functions to be synthesized, which will result in garbage
11951 collection. We must treat this situation as if we were within
11952 the body of function so as to avoid collecting live data on the
11953 stack. */
11954 ++function_depth;
11955 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11956 complain, NULL_TREE,
11957 /*integral_constant_expression_p=*/false);
11958 --function_depth;
11959 pop_deferring_access_checks();
11961 /* Restore the "this" pointer. */
11962 if (cfun)
11964 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11965 cp_function_chain->x_current_class_ref = saved_class_ref;
11968 if (errorcount+sorrycount > errs
11969 && (complain & tf_warning_or_error))
11970 inform (input_location,
11971 " when instantiating default argument for call to %D", fn);
11973 /* Make sure the default argument is reasonable. */
11974 arg = check_default_argument (type, arg, complain);
11976 pop_access_scope (fn);
11978 return arg;
11981 /* Substitute into all the default arguments for FN. */
11983 static void
11984 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11986 tree arg;
11987 tree tmpl_args;
11989 tmpl_args = DECL_TI_ARGS (fn);
11991 /* If this function is not yet instantiated, we certainly don't need
11992 its default arguments. */
11993 if (uses_template_parms (tmpl_args))
11994 return;
11995 /* Don't do this again for clones. */
11996 if (DECL_CLONED_FUNCTION_P (fn))
11997 return;
11999 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12000 arg;
12001 arg = TREE_CHAIN (arg))
12002 if (TREE_PURPOSE (arg))
12003 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
12004 TREE_VALUE (arg),
12005 TREE_PURPOSE (arg),
12006 complain);
12009 /* Substitute the ARGS into the T, which is a _DECL. Return the
12010 result of the substitution. Issue error and warning messages under
12011 control of COMPLAIN. */
12013 static tree
12014 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12016 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12017 location_t saved_loc;
12018 tree r = NULL_TREE;
12019 tree in_decl = t;
12020 hashval_t hash = 0;
12022 /* Set the filename and linenumber to improve error-reporting. */
12023 saved_loc = input_location;
12024 input_location = DECL_SOURCE_LOCATION (t);
12026 switch (TREE_CODE (t))
12028 case TEMPLATE_DECL:
12030 /* We can get here when processing a member function template,
12031 member class template, or template template parameter. */
12032 tree decl = DECL_TEMPLATE_RESULT (t);
12033 tree spec;
12034 tree tmpl_args;
12035 tree full_args;
12037 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12039 /* Template template parameter is treated here. */
12040 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12041 if (new_type == error_mark_node)
12042 r = error_mark_node;
12043 /* If we get a real template back, return it. This can happen in
12044 the context of most_specialized_partial_spec. */
12045 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12046 r = new_type;
12047 else
12048 /* The new TEMPLATE_DECL was built in
12049 reduce_template_parm_level. */
12050 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12051 break;
12054 /* We might already have an instance of this template.
12055 The ARGS are for the surrounding class type, so the
12056 full args contain the tsubst'd args for the context,
12057 plus the innermost args from the template decl. */
12058 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12059 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12060 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12061 /* Because this is a template, the arguments will still be
12062 dependent, even after substitution. If
12063 PROCESSING_TEMPLATE_DECL is not set, the dependency
12064 predicates will short-circuit. */
12065 ++processing_template_decl;
12066 full_args = tsubst_template_args (tmpl_args, args,
12067 complain, in_decl);
12068 --processing_template_decl;
12069 if (full_args == error_mark_node)
12070 RETURN (error_mark_node);
12072 /* If this is a default template template argument,
12073 tsubst might not have changed anything. */
12074 if (full_args == tmpl_args)
12075 RETURN (t);
12077 hash = hash_tmpl_and_args (t, full_args);
12078 spec = retrieve_specialization (t, full_args, hash);
12079 if (spec != NULL_TREE)
12081 r = spec;
12082 break;
12085 /* Make a new template decl. It will be similar to the
12086 original, but will record the current template arguments.
12087 We also create a new function declaration, which is just
12088 like the old one, but points to this new template, rather
12089 than the old one. */
12090 r = copy_decl (t);
12091 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12092 DECL_CHAIN (r) = NULL_TREE;
12094 // Build new template info linking to the original template decl.
12095 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12097 if (TREE_CODE (decl) == TYPE_DECL
12098 && !TYPE_DECL_ALIAS_P (decl))
12100 tree new_type;
12101 ++processing_template_decl;
12102 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12103 --processing_template_decl;
12104 if (new_type == error_mark_node)
12105 RETURN (error_mark_node);
12107 TREE_TYPE (r) = new_type;
12108 /* For a partial specialization, we need to keep pointing to
12109 the primary template. */
12110 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12111 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12112 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12113 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12114 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12116 else
12118 tree new_decl;
12119 ++processing_template_decl;
12120 new_decl = tsubst (decl, args, complain, in_decl);
12121 --processing_template_decl;
12122 if (new_decl == error_mark_node)
12123 RETURN (error_mark_node);
12125 DECL_TEMPLATE_RESULT (r) = new_decl;
12126 DECL_TI_TEMPLATE (new_decl) = r;
12127 TREE_TYPE (r) = TREE_TYPE (new_decl);
12128 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12129 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12132 SET_DECL_IMPLICIT_INSTANTIATION (r);
12133 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12134 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12136 /* The template parameters for this new template are all the
12137 template parameters for the old template, except the
12138 outermost level of parameters. */
12139 DECL_TEMPLATE_PARMS (r)
12140 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12141 complain);
12143 if (PRIMARY_TEMPLATE_P (t))
12144 DECL_PRIMARY_TEMPLATE (r) = r;
12146 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12147 /* Record this non-type partial instantiation. */
12148 register_specialization (r, t,
12149 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12150 false, hash);
12152 break;
12154 case FUNCTION_DECL:
12156 tree ctx;
12157 tree argvec = NULL_TREE;
12158 tree *friends;
12159 tree gen_tmpl;
12160 tree type;
12161 int member;
12162 int args_depth;
12163 int parms_depth;
12165 /* Nobody should be tsubst'ing into non-template functions. */
12166 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12168 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12170 tree spec;
12172 /* If T is not dependent, just return it. */
12173 if (!uses_template_parms (DECL_TI_ARGS (t)))
12174 RETURN (t);
12176 /* Calculate the most general template of which R is a
12177 specialization, and the complete set of arguments used to
12178 specialize R. */
12179 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12180 argvec = tsubst_template_args (DECL_TI_ARGS
12181 (DECL_TEMPLATE_RESULT
12182 (DECL_TI_TEMPLATE (t))),
12183 args, complain, in_decl);
12184 if (argvec == error_mark_node)
12185 RETURN (error_mark_node);
12187 /* Check to see if we already have this specialization. */
12188 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12189 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12191 if (spec)
12193 r = spec;
12194 break;
12197 /* We can see more levels of arguments than parameters if
12198 there was a specialization of a member template, like
12199 this:
12201 template <class T> struct S { template <class U> void f(); }
12202 template <> template <class U> void S<int>::f(U);
12204 Here, we'll be substituting into the specialization,
12205 because that's where we can find the code we actually
12206 want to generate, but we'll have enough arguments for
12207 the most general template.
12209 We also deal with the peculiar case:
12211 template <class T> struct S {
12212 template <class U> friend void f();
12214 template <class U> void f() {}
12215 template S<int>;
12216 template void f<double>();
12218 Here, the ARGS for the instantiation of will be {int,
12219 double}. But, we only need as many ARGS as there are
12220 levels of template parameters in CODE_PATTERN. We are
12221 careful not to get fooled into reducing the ARGS in
12222 situations like:
12224 template <class T> struct S { template <class U> void f(U); }
12225 template <class T> template <> void S<T>::f(int) {}
12227 which we can spot because the pattern will be a
12228 specialization in this case. */
12229 args_depth = TMPL_ARGS_DEPTH (args);
12230 parms_depth =
12231 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12232 if (args_depth > parms_depth
12233 && !DECL_TEMPLATE_SPECIALIZATION (t))
12234 args = get_innermost_template_args (args, parms_depth);
12236 else
12238 /* This special case arises when we have something like this:
12240 template <class T> struct S {
12241 friend void f<int>(int, double);
12244 Here, the DECL_TI_TEMPLATE for the friend declaration
12245 will be an IDENTIFIER_NODE. We are being called from
12246 tsubst_friend_function, and we want only to create a
12247 new decl (R) with appropriate types so that we can call
12248 determine_specialization. */
12249 gen_tmpl = NULL_TREE;
12252 if (DECL_CLASS_SCOPE_P (t))
12254 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12255 member = 2;
12256 else
12257 member = 1;
12258 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12259 complain, t, /*entering_scope=*/1);
12261 else
12263 member = 0;
12264 ctx = DECL_CONTEXT (t);
12266 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12267 if (type == error_mark_node)
12268 RETURN (error_mark_node);
12270 /* If we hit excessive deduction depth, the type is bogus even if
12271 it isn't error_mark_node, so don't build a decl. */
12272 if (excessive_deduction_depth)
12273 RETURN (error_mark_node);
12275 /* We do NOT check for matching decls pushed separately at this
12276 point, as they may not represent instantiations of this
12277 template, and in any case are considered separate under the
12278 discrete model. */
12279 r = copy_decl (t);
12280 DECL_USE_TEMPLATE (r) = 0;
12281 TREE_TYPE (r) = type;
12282 /* Clear out the mangled name and RTL for the instantiation. */
12283 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12284 SET_DECL_RTL (r, NULL);
12285 /* Leave DECL_INITIAL set on deleted instantiations. */
12286 if (!DECL_DELETED_FN (r))
12287 DECL_INITIAL (r) = NULL_TREE;
12288 DECL_CONTEXT (r) = ctx;
12290 /* OpenMP UDRs have the only argument a reference to the declared
12291 type. We want to diagnose if the declared type is a reference,
12292 which is invalid, but as references to references are usually
12293 quietly merged, diagnose it here. */
12294 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12296 tree argtype
12297 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12298 argtype = tsubst (argtype, args, complain, in_decl);
12299 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12300 error_at (DECL_SOURCE_LOCATION (t),
12301 "reference type %qT in "
12302 "%<#pragma omp declare reduction%>", argtype);
12303 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12304 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12305 argtype);
12308 if (member && DECL_CONV_FN_P (r))
12309 /* Type-conversion operator. Reconstruct the name, in
12310 case it's the name of one of the template's parameters. */
12311 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12313 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12314 complain, t);
12315 DECL_RESULT (r) = NULL_TREE;
12317 TREE_STATIC (r) = 0;
12318 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12319 DECL_EXTERNAL (r) = 1;
12320 /* If this is an instantiation of a function with internal
12321 linkage, we already know what object file linkage will be
12322 assigned to the instantiation. */
12323 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12324 DECL_DEFER_OUTPUT (r) = 0;
12325 DECL_CHAIN (r) = NULL_TREE;
12326 DECL_PENDING_INLINE_INFO (r) = 0;
12327 DECL_PENDING_INLINE_P (r) = 0;
12328 DECL_SAVED_TREE (r) = NULL_TREE;
12329 DECL_STRUCT_FUNCTION (r) = NULL;
12330 TREE_USED (r) = 0;
12331 /* We'll re-clone as appropriate in instantiate_template. */
12332 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12334 /* If we aren't complaining now, return on error before we register
12335 the specialization so that we'll complain eventually. */
12336 if ((complain & tf_error) == 0
12337 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12338 && !grok_op_properties (r, /*complain=*/false))
12339 RETURN (error_mark_node);
12341 /* When instantiating a constrained member, substitute
12342 into the constraints to create a new constraint. */
12343 if (tree ci = get_constraints (t))
12344 if (member)
12346 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12347 set_constraints (r, ci);
12350 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12351 this in the special friend case mentioned above where
12352 GEN_TMPL is NULL. */
12353 if (gen_tmpl)
12355 DECL_TEMPLATE_INFO (r)
12356 = build_template_info (gen_tmpl, argvec);
12357 SET_DECL_IMPLICIT_INSTANTIATION (r);
12359 tree new_r
12360 = register_specialization (r, gen_tmpl, argvec, false, hash);
12361 if (new_r != r)
12362 /* We instantiated this while substituting into
12363 the type earlier (template/friend54.C). */
12364 RETURN (new_r);
12366 /* We're not supposed to instantiate default arguments
12367 until they are called, for a template. But, for a
12368 declaration like:
12370 template <class T> void f ()
12371 { extern void g(int i = T()); }
12373 we should do the substitution when the template is
12374 instantiated. We handle the member function case in
12375 instantiate_class_template since the default arguments
12376 might refer to other members of the class. */
12377 if (!member
12378 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12379 && !uses_template_parms (argvec))
12380 tsubst_default_arguments (r, complain);
12382 else
12383 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12385 /* Copy the list of befriending classes. */
12386 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12387 *friends;
12388 friends = &TREE_CHAIN (*friends))
12390 *friends = copy_node (*friends);
12391 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12392 args, complain,
12393 in_decl);
12396 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12398 maybe_retrofit_in_chrg (r);
12399 if (DECL_CONSTRUCTOR_P (r))
12400 grok_ctor_properties (ctx, r);
12401 /* If this is an instantiation of a member template, clone it.
12402 If it isn't, that'll be handled by
12403 clone_constructors_and_destructors. */
12404 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12405 clone_function_decl (r, /*update_method_vec_p=*/0);
12407 else if ((complain & tf_error) != 0
12408 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12409 && !grok_op_properties (r, /*complain=*/true))
12410 RETURN (error_mark_node);
12412 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12413 SET_DECL_FRIEND_CONTEXT (r,
12414 tsubst (DECL_FRIEND_CONTEXT (t),
12415 args, complain, in_decl));
12417 /* Possibly limit visibility based on template args. */
12418 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12419 if (DECL_VISIBILITY_SPECIFIED (t))
12421 DECL_VISIBILITY_SPECIFIED (r) = 0;
12422 DECL_ATTRIBUTES (r)
12423 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12425 determine_visibility (r);
12426 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12427 && !processing_template_decl)
12428 defaulted_late_check (r);
12430 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12431 args, complain, in_decl);
12433 break;
12435 case PARM_DECL:
12437 tree type = NULL_TREE;
12438 int i, len = 1;
12439 tree expanded_types = NULL_TREE;
12440 tree prev_r = NULL_TREE;
12441 tree first_r = NULL_TREE;
12443 if (DECL_PACK_P (t))
12445 /* If there is a local specialization that isn't a
12446 parameter pack, it means that we're doing a "simple"
12447 substitution from inside tsubst_pack_expansion. Just
12448 return the local specialization (which will be a single
12449 parm). */
12450 tree spec = retrieve_local_specialization (t);
12451 if (spec
12452 && TREE_CODE (spec) == PARM_DECL
12453 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12454 RETURN (spec);
12456 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12457 the parameters in this function parameter pack. */
12458 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12459 complain, in_decl);
12460 if (TREE_CODE (expanded_types) == TREE_VEC)
12462 len = TREE_VEC_LENGTH (expanded_types);
12464 /* Zero-length parameter packs are boring. Just substitute
12465 into the chain. */
12466 if (len == 0)
12467 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12468 TREE_CHAIN (t)));
12470 else
12472 /* All we did was update the type. Make a note of that. */
12473 type = expanded_types;
12474 expanded_types = NULL_TREE;
12478 /* Loop through all of the parameters we'll build. When T is
12479 a function parameter pack, LEN is the number of expanded
12480 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12481 r = NULL_TREE;
12482 for (i = 0; i < len; ++i)
12484 prev_r = r;
12485 r = copy_node (t);
12486 if (DECL_TEMPLATE_PARM_P (t))
12487 SET_DECL_TEMPLATE_PARM_P (r);
12489 if (expanded_types)
12490 /* We're on the Ith parameter of the function parameter
12491 pack. */
12493 /* Get the Ith type. */
12494 type = TREE_VEC_ELT (expanded_types, i);
12496 /* Rename the parameter to include the index. */
12497 DECL_NAME (r)
12498 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12500 else if (!type)
12501 /* We're dealing with a normal parameter. */
12502 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12504 type = type_decays_to (type);
12505 TREE_TYPE (r) = type;
12506 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12508 if (DECL_INITIAL (r))
12510 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12511 DECL_INITIAL (r) = TREE_TYPE (r);
12512 else
12513 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12514 complain, in_decl);
12517 DECL_CONTEXT (r) = NULL_TREE;
12519 if (!DECL_TEMPLATE_PARM_P (r))
12520 DECL_ARG_TYPE (r) = type_passed_as (type);
12522 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12523 args, complain, in_decl);
12525 /* Keep track of the first new parameter we
12526 generate. That's what will be returned to the
12527 caller. */
12528 if (!first_r)
12529 first_r = r;
12531 /* Build a proper chain of parameters when substituting
12532 into a function parameter pack. */
12533 if (prev_r)
12534 DECL_CHAIN (prev_r) = r;
12537 /* If cp_unevaluated_operand is set, we're just looking for a
12538 single dummy parameter, so don't keep going. */
12539 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12540 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12541 complain, DECL_CHAIN (t));
12543 /* FIRST_R contains the start of the chain we've built. */
12544 r = first_r;
12546 break;
12548 case FIELD_DECL:
12550 tree type = NULL_TREE;
12551 tree vec = NULL_TREE;
12552 tree expanded_types = NULL_TREE;
12553 int len = 1;
12555 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12557 /* This field is a lambda capture pack. Return a TREE_VEC of
12558 the expanded fields to instantiate_class_template_1 and
12559 store them in the specializations hash table as a
12560 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12561 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12562 complain, in_decl);
12563 if (TREE_CODE (expanded_types) == TREE_VEC)
12565 len = TREE_VEC_LENGTH (expanded_types);
12566 vec = make_tree_vec (len);
12568 else
12570 /* All we did was update the type. Make a note of that. */
12571 type = expanded_types;
12572 expanded_types = NULL_TREE;
12576 for (int i = 0; i < len; ++i)
12578 r = copy_decl (t);
12579 if (expanded_types)
12581 type = TREE_VEC_ELT (expanded_types, i);
12582 DECL_NAME (r)
12583 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12585 else if (!type)
12586 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12588 if (type == error_mark_node)
12589 RETURN (error_mark_node);
12590 TREE_TYPE (r) = type;
12591 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12593 if (DECL_C_BIT_FIELD (r))
12594 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12595 non-bit-fields DECL_INITIAL is a non-static data member
12596 initializer, which gets deferred instantiation. */
12597 DECL_INITIAL (r)
12598 = tsubst_expr (DECL_INITIAL (t), args,
12599 complain, in_decl,
12600 /*integral_constant_expression_p=*/true);
12601 else if (DECL_INITIAL (t))
12603 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12604 NSDMI in perform_member_init. Still set DECL_INITIAL
12605 so that we know there is one. */
12606 DECL_INITIAL (r) = void_node;
12607 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12608 retrofit_lang_decl (r);
12609 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12611 /* We don't have to set DECL_CONTEXT here; it is set by
12612 finish_member_declaration. */
12613 DECL_CHAIN (r) = NULL_TREE;
12615 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12616 args, complain, in_decl);
12618 if (vec)
12619 TREE_VEC_ELT (vec, i) = r;
12622 if (vec)
12624 r = vec;
12625 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12626 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12627 SET_ARGUMENT_PACK_ARGS (pack, vec);
12628 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12629 TREE_TYPE (pack) = tpack;
12630 register_specialization (pack, t, args, false, 0);
12633 break;
12635 case USING_DECL:
12636 /* We reach here only for member using decls. We also need to check
12637 uses_template_parms because DECL_DEPENDENT_P is not set for a
12638 using-declaration that designates a member of the current
12639 instantiation (c++/53549). */
12640 if (DECL_DEPENDENT_P (t)
12641 || uses_template_parms (USING_DECL_SCOPE (t)))
12643 tree scope = USING_DECL_SCOPE (t);
12644 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12645 if (PACK_EXPANSION_P (scope))
12647 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12648 int len = TREE_VEC_LENGTH (vec);
12649 r = make_tree_vec (len);
12650 for (int i = 0; i < len; ++i)
12652 tree escope = TREE_VEC_ELT (vec, i);
12653 tree elt = do_class_using_decl (escope, name);
12654 if (!elt)
12656 r = error_mark_node;
12657 break;
12659 else
12661 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12662 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12664 TREE_VEC_ELT (r, i) = elt;
12667 else
12669 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12670 complain, in_decl);
12671 r = do_class_using_decl (inst_scope, name);
12672 if (!r)
12673 r = error_mark_node;
12674 else
12676 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12677 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12681 else
12683 r = copy_node (t);
12684 DECL_CHAIN (r) = NULL_TREE;
12686 break;
12688 case TYPE_DECL:
12689 case VAR_DECL:
12691 tree argvec = NULL_TREE;
12692 tree gen_tmpl = NULL_TREE;
12693 tree spec;
12694 tree tmpl = NULL_TREE;
12695 tree ctx;
12696 tree type = NULL_TREE;
12697 bool local_p;
12699 if (TREE_TYPE (t) == error_mark_node)
12700 RETURN (error_mark_node);
12702 if (TREE_CODE (t) == TYPE_DECL
12703 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12705 /* If this is the canonical decl, we don't have to
12706 mess with instantiations, and often we can't (for
12707 typename, template type parms and such). Note that
12708 TYPE_NAME is not correct for the above test if
12709 we've copied the type for a typedef. */
12710 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12711 if (type == error_mark_node)
12712 RETURN (error_mark_node);
12713 r = TYPE_NAME (type);
12714 break;
12717 /* Check to see if we already have the specialization we
12718 need. */
12719 spec = NULL_TREE;
12720 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12722 /* T is a static data member or namespace-scope entity.
12723 We have to substitute into namespace-scope variables
12724 (not just variable templates) because of cases like:
12726 template <class T> void f() { extern T t; }
12728 where the entity referenced is not known until
12729 instantiation time. */
12730 local_p = false;
12731 ctx = DECL_CONTEXT (t);
12732 if (DECL_CLASS_SCOPE_P (t))
12734 ctx = tsubst_aggr_type (ctx, args,
12735 complain,
12736 in_decl, /*entering_scope=*/1);
12737 /* If CTX is unchanged, then T is in fact the
12738 specialization we want. That situation occurs when
12739 referencing a static data member within in its own
12740 class. We can use pointer equality, rather than
12741 same_type_p, because DECL_CONTEXT is always
12742 canonical... */
12743 if (ctx == DECL_CONTEXT (t)
12744 /* ... unless T is a member template; in which
12745 case our caller can be willing to create a
12746 specialization of that template represented
12747 by T. */
12748 && !(DECL_TI_TEMPLATE (t)
12749 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12750 spec = t;
12753 if (!spec)
12755 tmpl = DECL_TI_TEMPLATE (t);
12756 gen_tmpl = most_general_template (tmpl);
12757 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12758 if (argvec != error_mark_node)
12759 argvec = (coerce_innermost_template_parms
12760 (DECL_TEMPLATE_PARMS (gen_tmpl),
12761 argvec, t, complain,
12762 /*all*/true, /*defarg*/true));
12763 if (argvec == error_mark_node)
12764 RETURN (error_mark_node);
12765 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12766 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12769 else
12771 /* A local variable. */
12772 local_p = true;
12773 /* Subsequent calls to pushdecl will fill this in. */
12774 ctx = NULL_TREE;
12775 /* Unless this is a reference to a static variable from an
12776 enclosing function, in which case we need to fill it in now. */
12777 if (TREE_STATIC (t))
12779 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12780 if (fn != current_function_decl)
12781 ctx = fn;
12783 spec = retrieve_local_specialization (t);
12785 /* If we already have the specialization we need, there is
12786 nothing more to do. */
12787 if (spec)
12789 r = spec;
12790 break;
12793 /* Create a new node for the specialization we need. */
12794 r = copy_decl (t);
12795 if (type == NULL_TREE)
12797 if (is_typedef_decl (t))
12798 type = DECL_ORIGINAL_TYPE (t);
12799 else
12800 type = TREE_TYPE (t);
12801 if (VAR_P (t)
12802 && VAR_HAD_UNKNOWN_BOUND (t)
12803 && type != error_mark_node)
12804 type = strip_array_domain (type);
12805 type = tsubst (type, args, complain, in_decl);
12807 if (VAR_P (r))
12809 /* Even if the original location is out of scope, the
12810 newly substituted one is not. */
12811 DECL_DEAD_FOR_LOCAL (r) = 0;
12812 DECL_INITIALIZED_P (r) = 0;
12813 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12814 if (type == error_mark_node)
12815 RETURN (error_mark_node);
12816 if (TREE_CODE (type) == FUNCTION_TYPE)
12818 /* It may seem that this case cannot occur, since:
12820 typedef void f();
12821 void g() { f x; }
12823 declares a function, not a variable. However:
12825 typedef void f();
12826 template <typename T> void g() { T t; }
12827 template void g<f>();
12829 is an attempt to declare a variable with function
12830 type. */
12831 error ("variable %qD has function type",
12832 /* R is not yet sufficiently initialized, so we
12833 just use its name. */
12834 DECL_NAME (r));
12835 RETURN (error_mark_node);
12837 type = complete_type (type);
12838 /* Wait until cp_finish_decl to set this again, to handle
12839 circular dependency (template/instantiate6.C). */
12840 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12841 type = check_var_type (DECL_NAME (r), type);
12843 if (DECL_HAS_VALUE_EXPR_P (t))
12845 tree ve = DECL_VALUE_EXPR (t);
12846 ve = tsubst_expr (ve, args, complain, in_decl,
12847 /*constant_expression_p=*/false);
12848 if (REFERENCE_REF_P (ve))
12850 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12851 ve = TREE_OPERAND (ve, 0);
12853 SET_DECL_VALUE_EXPR (r, ve);
12855 if (CP_DECL_THREAD_LOCAL_P (r)
12856 && !processing_template_decl)
12857 set_decl_tls_model (r, decl_default_tls_model (r));
12859 else if (DECL_SELF_REFERENCE_P (t))
12860 SET_DECL_SELF_REFERENCE_P (r);
12861 TREE_TYPE (r) = type;
12862 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12863 DECL_CONTEXT (r) = ctx;
12864 /* Clear out the mangled name and RTL for the instantiation. */
12865 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12866 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12867 SET_DECL_RTL (r, NULL);
12868 /* The initializer must not be expanded until it is required;
12869 see [temp.inst]. */
12870 DECL_INITIAL (r) = NULL_TREE;
12871 if (VAR_P (r))
12872 SET_DECL_MODE (r, VOIDmode);
12873 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12874 SET_DECL_RTL (r, NULL);
12875 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12876 if (VAR_P (r))
12878 /* Possibly limit visibility based on template args. */
12879 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12880 if (DECL_VISIBILITY_SPECIFIED (t))
12882 DECL_VISIBILITY_SPECIFIED (r) = 0;
12883 DECL_ATTRIBUTES (r)
12884 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12886 determine_visibility (r);
12889 if (!local_p)
12891 /* A static data member declaration is always marked
12892 external when it is declared in-class, even if an
12893 initializer is present. We mimic the non-template
12894 processing here. */
12895 DECL_EXTERNAL (r) = 1;
12896 if (DECL_NAMESPACE_SCOPE_P (t))
12897 DECL_NOT_REALLY_EXTERN (r) = 1;
12899 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12900 SET_DECL_IMPLICIT_INSTANTIATION (r);
12901 register_specialization (r, gen_tmpl, argvec, false, hash);
12903 else
12905 if (DECL_LANG_SPECIFIC (r))
12906 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12907 if (!cp_unevaluated_operand)
12908 register_local_specialization (r, t);
12911 DECL_CHAIN (r) = NULL_TREE;
12913 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12914 /*flags=*/0,
12915 args, complain, in_decl);
12917 /* Preserve a typedef that names a type. */
12918 if (is_typedef_decl (r) && type != error_mark_node)
12920 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12921 set_underlying_type (r);
12922 if (TYPE_DECL_ALIAS_P (r))
12923 /* An alias template specialization can be dependent
12924 even if its underlying type is not. */
12925 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12928 layout_decl (r, 0);
12930 break;
12932 default:
12933 gcc_unreachable ();
12935 #undef RETURN
12937 out:
12938 /* Restore the file and line information. */
12939 input_location = saved_loc;
12941 return r;
12944 /* Substitute into the ARG_TYPES of a function type.
12945 If END is a TREE_CHAIN, leave it and any following types
12946 un-substituted. */
12948 static tree
12949 tsubst_arg_types (tree arg_types,
12950 tree args,
12951 tree end,
12952 tsubst_flags_t complain,
12953 tree in_decl)
12955 tree remaining_arg_types;
12956 tree type = NULL_TREE;
12957 int i = 1;
12958 tree expanded_args = NULL_TREE;
12959 tree default_arg;
12961 if (!arg_types || arg_types == void_list_node || arg_types == end)
12962 return arg_types;
12964 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12965 args, end, complain, in_decl);
12966 if (remaining_arg_types == error_mark_node)
12967 return error_mark_node;
12969 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12971 /* For a pack expansion, perform substitution on the
12972 entire expression. Later on, we'll handle the arguments
12973 one-by-one. */
12974 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12975 args, complain, in_decl);
12977 if (TREE_CODE (expanded_args) == TREE_VEC)
12978 /* So that we'll spin through the parameters, one by one. */
12979 i = TREE_VEC_LENGTH (expanded_args);
12980 else
12982 /* We only partially substituted into the parameter
12983 pack. Our type is TYPE_PACK_EXPANSION. */
12984 type = expanded_args;
12985 expanded_args = NULL_TREE;
12989 while (i > 0) {
12990 --i;
12992 if (expanded_args)
12993 type = TREE_VEC_ELT (expanded_args, i);
12994 else if (!type)
12995 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12997 if (type == error_mark_node)
12998 return error_mark_node;
12999 if (VOID_TYPE_P (type))
13001 if (complain & tf_error)
13003 error ("invalid parameter type %qT", type);
13004 if (in_decl)
13005 error ("in declaration %q+D", in_decl);
13007 return error_mark_node;
13009 /* DR 657. */
13010 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13011 return error_mark_node;
13013 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13014 top-level qualifiers as required. */
13015 type = cv_unqualified (type_decays_to (type));
13017 /* We do not substitute into default arguments here. The standard
13018 mandates that they be instantiated only when needed, which is
13019 done in build_over_call. */
13020 default_arg = TREE_PURPOSE (arg_types);
13022 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13024 /* We've instantiated a template before its default arguments
13025 have been parsed. This can happen for a nested template
13026 class, and is not an error unless we require the default
13027 argument in a call of this function. */
13028 remaining_arg_types =
13029 tree_cons (default_arg, type, remaining_arg_types);
13030 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13032 else
13033 remaining_arg_types =
13034 hash_tree_cons (default_arg, type, remaining_arg_types);
13037 return remaining_arg_types;
13040 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13041 *not* handle the exception-specification for FNTYPE, because the
13042 initial substitution of explicitly provided template parameters
13043 during argument deduction forbids substitution into the
13044 exception-specification:
13046 [temp.deduct]
13048 All references in the function type of the function template to the
13049 corresponding template parameters are replaced by the specified tem-
13050 plate argument values. If a substitution in a template parameter or
13051 in the function type of the function template results in an invalid
13052 type, type deduction fails. [Note: The equivalent substitution in
13053 exception specifications is done only when the function is instanti-
13054 ated, at which point a program is ill-formed if the substitution
13055 results in an invalid type.] */
13057 static tree
13058 tsubst_function_type (tree t,
13059 tree args,
13060 tsubst_flags_t complain,
13061 tree in_decl)
13063 tree return_type;
13064 tree arg_types = NULL_TREE;
13065 tree fntype;
13067 /* The TYPE_CONTEXT is not used for function/method types. */
13068 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13070 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13071 failure. */
13072 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13074 if (late_return_type_p)
13076 /* Substitute the argument types. */
13077 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13078 complain, in_decl);
13079 if (arg_types == error_mark_node)
13080 return error_mark_node;
13082 tree save_ccp = current_class_ptr;
13083 tree save_ccr = current_class_ref;
13084 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13085 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13086 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13087 if (do_inject)
13089 /* DR 1207: 'this' is in scope in the trailing return type. */
13090 inject_this_parameter (this_type, cp_type_quals (this_type));
13093 /* Substitute the return type. */
13094 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13096 if (do_inject)
13098 current_class_ptr = save_ccp;
13099 current_class_ref = save_ccr;
13102 else
13103 /* Substitute the return type. */
13104 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13106 if (return_type == error_mark_node)
13107 return error_mark_node;
13108 /* DR 486 clarifies that creation of a function type with an
13109 invalid return type is a deduction failure. */
13110 if (TREE_CODE (return_type) == ARRAY_TYPE
13111 || TREE_CODE (return_type) == FUNCTION_TYPE)
13113 if (complain & tf_error)
13115 if (TREE_CODE (return_type) == ARRAY_TYPE)
13116 error ("function returning an array");
13117 else
13118 error ("function returning a function");
13120 return error_mark_node;
13122 /* And DR 657. */
13123 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13124 return error_mark_node;
13126 if (!late_return_type_p)
13128 /* Substitute the argument types. */
13129 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13130 complain, in_decl);
13131 if (arg_types == error_mark_node)
13132 return error_mark_node;
13135 /* Construct a new type node and return it. */
13136 if (TREE_CODE (t) == FUNCTION_TYPE)
13138 fntype = build_function_type (return_type, arg_types);
13139 fntype = apply_memfn_quals (fntype,
13140 type_memfn_quals (t),
13141 type_memfn_rqual (t));
13143 else
13145 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13146 /* Don't pick up extra function qualifiers from the basetype. */
13147 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13148 if (! MAYBE_CLASS_TYPE_P (r))
13150 /* [temp.deduct]
13152 Type deduction may fail for any of the following
13153 reasons:
13155 -- Attempting to create "pointer to member of T" when T
13156 is not a class type. */
13157 if (complain & tf_error)
13158 error ("creating pointer to member function of non-class type %qT",
13160 return error_mark_node;
13163 fntype = build_method_type_directly (r, return_type,
13164 TREE_CHAIN (arg_types));
13165 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13167 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13169 if (late_return_type_p)
13170 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13172 return fntype;
13175 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13176 ARGS into that specification, and return the substituted
13177 specification. If there is no specification, return NULL_TREE. */
13179 static tree
13180 tsubst_exception_specification (tree fntype,
13181 tree args,
13182 tsubst_flags_t complain,
13183 tree in_decl,
13184 bool defer_ok)
13186 tree specs;
13187 tree new_specs;
13189 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13190 new_specs = NULL_TREE;
13191 if (specs && TREE_PURPOSE (specs))
13193 /* A noexcept-specifier. */
13194 tree expr = TREE_PURPOSE (specs);
13195 if (TREE_CODE (expr) == INTEGER_CST)
13196 new_specs = expr;
13197 else if (defer_ok)
13199 /* Defer instantiation of noexcept-specifiers to avoid
13200 excessive instantiations (c++/49107). */
13201 new_specs = make_node (DEFERRED_NOEXCEPT);
13202 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13204 /* We already partially instantiated this member template,
13205 so combine the new args with the old. */
13206 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13207 = DEFERRED_NOEXCEPT_PATTERN (expr);
13208 DEFERRED_NOEXCEPT_ARGS (new_specs)
13209 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13211 else
13213 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13214 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13217 else
13218 new_specs = tsubst_copy_and_build
13219 (expr, args, complain, in_decl, /*function_p=*/false,
13220 /*integral_constant_expression_p=*/true);
13221 new_specs = build_noexcept_spec (new_specs, complain);
13223 else if (specs)
13225 if (! TREE_VALUE (specs))
13226 new_specs = specs;
13227 else
13228 while (specs)
13230 tree spec;
13231 int i, len = 1;
13232 tree expanded_specs = NULL_TREE;
13234 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13236 /* Expand the pack expansion type. */
13237 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13238 args, complain,
13239 in_decl);
13241 if (expanded_specs == error_mark_node)
13242 return error_mark_node;
13243 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13244 len = TREE_VEC_LENGTH (expanded_specs);
13245 else
13247 /* We're substituting into a member template, so
13248 we got a TYPE_PACK_EXPANSION back. Add that
13249 expansion and move on. */
13250 gcc_assert (TREE_CODE (expanded_specs)
13251 == TYPE_PACK_EXPANSION);
13252 new_specs = add_exception_specifier (new_specs,
13253 expanded_specs,
13254 complain);
13255 specs = TREE_CHAIN (specs);
13256 continue;
13260 for (i = 0; i < len; ++i)
13262 if (expanded_specs)
13263 spec = TREE_VEC_ELT (expanded_specs, i);
13264 else
13265 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13266 if (spec == error_mark_node)
13267 return spec;
13268 new_specs = add_exception_specifier (new_specs, spec,
13269 complain);
13272 specs = TREE_CHAIN (specs);
13275 return new_specs;
13278 /* Take the tree structure T and replace template parameters used
13279 therein with the argument vector ARGS. IN_DECL is an associated
13280 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13281 Issue error and warning messages under control of COMPLAIN. Note
13282 that we must be relatively non-tolerant of extensions here, in
13283 order to preserve conformance; if we allow substitutions that
13284 should not be allowed, we may allow argument deductions that should
13285 not succeed, and therefore report ambiguous overload situations
13286 where there are none. In theory, we could allow the substitution,
13287 but indicate that it should have failed, and allow our caller to
13288 make sure that the right thing happens, but we don't try to do this
13289 yet.
13291 This function is used for dealing with types, decls and the like;
13292 for expressions, use tsubst_expr or tsubst_copy. */
13294 tree
13295 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13297 enum tree_code code;
13298 tree type, r = NULL_TREE;
13300 if (t == NULL_TREE || t == error_mark_node
13301 || t == integer_type_node
13302 || t == void_type_node
13303 || t == char_type_node
13304 || t == unknown_type_node
13305 || TREE_CODE (t) == NAMESPACE_DECL
13306 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13307 return t;
13309 if (DECL_P (t))
13310 return tsubst_decl (t, args, complain);
13312 if (args == NULL_TREE)
13313 return t;
13315 code = TREE_CODE (t);
13317 if (code == IDENTIFIER_NODE)
13318 type = IDENTIFIER_TYPE_VALUE (t);
13319 else
13320 type = TREE_TYPE (t);
13322 gcc_assert (type != unknown_type_node);
13324 /* Reuse typedefs. We need to do this to handle dependent attributes,
13325 such as attribute aligned. */
13326 if (TYPE_P (t)
13327 && typedef_variant_p (t))
13329 tree decl = TYPE_NAME (t);
13331 if (alias_template_specialization_p (t))
13333 /* DECL represents an alias template and we want to
13334 instantiate it. */
13335 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13336 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13337 r = instantiate_alias_template (tmpl, gen_args, complain);
13339 else if (DECL_CLASS_SCOPE_P (decl)
13340 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13341 && uses_template_parms (DECL_CONTEXT (decl)))
13343 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13344 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13345 r = retrieve_specialization (tmpl, gen_args, 0);
13347 else if (DECL_FUNCTION_SCOPE_P (decl)
13348 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13349 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13350 r = retrieve_local_specialization (decl);
13351 else
13352 /* The typedef is from a non-template context. */
13353 return t;
13355 if (r)
13357 r = TREE_TYPE (r);
13358 r = cp_build_qualified_type_real
13359 (r, cp_type_quals (t) | cp_type_quals (r),
13360 complain | tf_ignore_bad_quals);
13361 return r;
13363 else
13365 /* We don't have an instantiation yet, so drop the typedef. */
13366 int quals = cp_type_quals (t);
13367 t = DECL_ORIGINAL_TYPE (decl);
13368 t = cp_build_qualified_type_real (t, quals,
13369 complain | tf_ignore_bad_quals);
13373 bool fndecl_type = (complain & tf_fndecl_type);
13374 complain &= ~tf_fndecl_type;
13376 if (type
13377 && code != TYPENAME_TYPE
13378 && code != TEMPLATE_TYPE_PARM
13379 && code != IDENTIFIER_NODE
13380 && code != FUNCTION_TYPE
13381 && code != METHOD_TYPE)
13382 type = tsubst (type, args, complain, in_decl);
13383 if (type == error_mark_node)
13384 return error_mark_node;
13386 switch (code)
13388 case RECORD_TYPE:
13389 case UNION_TYPE:
13390 case ENUMERAL_TYPE:
13391 return tsubst_aggr_type (t, args, complain, in_decl,
13392 /*entering_scope=*/0);
13394 case ERROR_MARK:
13395 case IDENTIFIER_NODE:
13396 case VOID_TYPE:
13397 case REAL_TYPE:
13398 case COMPLEX_TYPE:
13399 case VECTOR_TYPE:
13400 case BOOLEAN_TYPE:
13401 case NULLPTR_TYPE:
13402 case LANG_TYPE:
13403 return t;
13405 case INTEGER_TYPE:
13406 if (t == integer_type_node)
13407 return t;
13409 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13410 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13411 return t;
13414 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13416 max = tsubst_expr (omax, args, complain, in_decl,
13417 /*integral_constant_expression_p=*/false);
13419 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13420 needed. */
13421 if (TREE_CODE (max) == NOP_EXPR
13422 && TREE_SIDE_EFFECTS (omax)
13423 && !TREE_TYPE (max))
13424 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13426 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13427 with TREE_SIDE_EFFECTS that indicates this is not an integral
13428 constant expression. */
13429 if (processing_template_decl
13430 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13432 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13433 TREE_SIDE_EFFECTS (max) = 1;
13436 return compute_array_index_type (NULL_TREE, max, complain);
13439 case TEMPLATE_TYPE_PARM:
13440 case TEMPLATE_TEMPLATE_PARM:
13441 case BOUND_TEMPLATE_TEMPLATE_PARM:
13442 case TEMPLATE_PARM_INDEX:
13444 int idx;
13445 int level;
13446 int levels;
13447 tree arg = NULL_TREE;
13449 /* Early in template argument deduction substitution, we don't
13450 want to reduce the level of 'auto', or it will be confused
13451 with a normal template parm in subsequent deduction. */
13452 if (is_auto (t) && (complain & tf_partial))
13453 return t;
13455 r = NULL_TREE;
13457 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13458 template_parm_level_and_index (t, &level, &idx);
13460 levels = TMPL_ARGS_DEPTH (args);
13461 if (level <= levels
13462 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13464 arg = TMPL_ARG (args, level, idx);
13466 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13468 /* See through ARGUMENT_PACK_SELECT arguments. */
13469 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13470 /* If the selected argument is an expansion E, that most
13471 likely means we were called from
13472 gen_elem_of_pack_expansion_instantiation during the
13473 substituting of pack an argument pack (which Ith
13474 element is a pack expansion, where I is
13475 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13476 In this case, the Ith element resulting from this
13477 substituting is going to be a pack expansion, which
13478 pattern is the pattern of E. Let's return the
13479 pattern of E, and
13480 gen_elem_of_pack_expansion_instantiation will
13481 build the resulting pack expansion from it. */
13482 if (PACK_EXPANSION_P (arg))
13484 /* Make sure we aren't throwing away arg info. */
13485 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13486 arg = PACK_EXPANSION_PATTERN (arg);
13491 if (arg == error_mark_node)
13492 return error_mark_node;
13493 else if (arg != NULL_TREE)
13495 if (ARGUMENT_PACK_P (arg))
13496 /* If ARG is an argument pack, we don't actually want to
13497 perform a substitution here, because substitutions
13498 for argument packs are only done
13499 element-by-element. We can get to this point when
13500 substituting the type of a non-type template
13501 parameter pack, when that type actually contains
13502 template parameter packs from an outer template, e.g.,
13504 template<typename... Types> struct A {
13505 template<Types... Values> struct B { };
13506 }; */
13507 return t;
13509 if (code == TEMPLATE_TYPE_PARM)
13511 int quals;
13512 gcc_assert (TYPE_P (arg));
13514 quals = cp_type_quals (arg) | cp_type_quals (t);
13516 return cp_build_qualified_type_real
13517 (arg, quals, complain | tf_ignore_bad_quals);
13519 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13521 /* We are processing a type constructed from a
13522 template template parameter. */
13523 tree argvec = tsubst (TYPE_TI_ARGS (t),
13524 args, complain, in_decl);
13525 if (argvec == error_mark_node)
13526 return error_mark_node;
13528 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13529 || TREE_CODE (arg) == TEMPLATE_DECL
13530 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13532 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13533 /* Consider this code:
13535 template <template <class> class Template>
13536 struct Internal {
13537 template <class Arg> using Bind = Template<Arg>;
13540 template <template <class> class Template, class Arg>
13541 using Instantiate = Template<Arg>; //#0
13543 template <template <class> class Template,
13544 class Argument>
13545 using Bind =
13546 Instantiate<Internal<Template>::template Bind,
13547 Argument>; //#1
13549 When #1 is parsed, the
13550 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13551 parameter `Template' in #0 matches the
13552 UNBOUND_CLASS_TEMPLATE representing the argument
13553 `Internal<Template>::template Bind'; We then want
13554 to assemble the type `Bind<Argument>' that can't
13555 be fully created right now, because
13556 `Internal<Template>' not being complete, the Bind
13557 template cannot be looked up in that context. So
13558 we need to "store" `Bind<Argument>' for later
13559 when the context of Bind becomes complete. Let's
13560 store that in a TYPENAME_TYPE. */
13561 return make_typename_type (TYPE_CONTEXT (arg),
13562 build_nt (TEMPLATE_ID_EXPR,
13563 TYPE_IDENTIFIER (arg),
13564 argvec),
13565 typename_type,
13566 complain);
13568 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13569 are resolving nested-types in the signature of a
13570 member function templates. Otherwise ARG is a
13571 TEMPLATE_DECL and is the real template to be
13572 instantiated. */
13573 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13574 arg = TYPE_NAME (arg);
13576 r = lookup_template_class (arg,
13577 argvec, in_decl,
13578 DECL_CONTEXT (arg),
13579 /*entering_scope=*/0,
13580 complain);
13581 return cp_build_qualified_type_real
13582 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13584 else if (code == TEMPLATE_TEMPLATE_PARM)
13585 return arg;
13586 else
13587 /* TEMPLATE_PARM_INDEX. */
13588 return convert_from_reference (unshare_expr (arg));
13591 if (level == 1)
13592 /* This can happen during the attempted tsubst'ing in
13593 unify. This means that we don't yet have any information
13594 about the template parameter in question. */
13595 return t;
13597 /* If we get here, we must have been looking at a parm for a
13598 more deeply nested template. Make a new version of this
13599 template parameter, but with a lower level. */
13600 switch (code)
13602 case TEMPLATE_TYPE_PARM:
13603 case TEMPLATE_TEMPLATE_PARM:
13604 case BOUND_TEMPLATE_TEMPLATE_PARM:
13605 if (cp_type_quals (t))
13607 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13608 r = cp_build_qualified_type_real
13609 (r, cp_type_quals (t),
13610 complain | (code == TEMPLATE_TYPE_PARM
13611 ? tf_ignore_bad_quals : 0));
13613 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13614 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13615 && (r = (TEMPLATE_PARM_DESCENDANTS
13616 (TEMPLATE_TYPE_PARM_INDEX (t))))
13617 && (r = TREE_TYPE (r))
13618 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13619 /* Break infinite recursion when substituting the constraints
13620 of a constrained placeholder. */;
13621 else
13623 r = copy_type (t);
13624 TEMPLATE_TYPE_PARM_INDEX (r)
13625 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13626 r, levels, args, complain);
13627 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13628 TYPE_MAIN_VARIANT (r) = r;
13629 TYPE_POINTER_TO (r) = NULL_TREE;
13630 TYPE_REFERENCE_TO (r) = NULL_TREE;
13632 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13634 /* Propagate constraints on placeholders. */
13635 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13636 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13637 = tsubst_constraint (constr, args, complain, in_decl);
13638 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13640 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13641 pl = tsubst (pl, args, complain, in_decl);
13642 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13646 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13647 /* We have reduced the level of the template
13648 template parameter, but not the levels of its
13649 template parameters, so canonical_type_parameter
13650 will not be able to find the canonical template
13651 template parameter for this level. Thus, we
13652 require structural equality checking to compare
13653 TEMPLATE_TEMPLATE_PARMs. */
13654 SET_TYPE_STRUCTURAL_EQUALITY (r);
13655 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13656 SET_TYPE_STRUCTURAL_EQUALITY (r);
13657 else
13658 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13660 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13662 tree tinfo = TYPE_TEMPLATE_INFO (t);
13663 /* We might need to substitute into the types of non-type
13664 template parameters. */
13665 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13666 complain, in_decl);
13667 if (tmpl == error_mark_node)
13668 return error_mark_node;
13669 tree argvec = tsubst (TI_ARGS (tinfo), args,
13670 complain, in_decl);
13671 if (argvec == error_mark_node)
13672 return error_mark_node;
13674 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13675 = build_template_info (tmpl, argvec);
13678 break;
13680 case TEMPLATE_PARM_INDEX:
13681 r = reduce_template_parm_level (t, type, levels, args, complain);
13682 break;
13684 default:
13685 gcc_unreachable ();
13688 return r;
13691 case TREE_LIST:
13693 tree purpose, value, chain;
13695 if (t == void_list_node)
13696 return t;
13698 purpose = TREE_PURPOSE (t);
13699 if (purpose)
13701 purpose = tsubst (purpose, args, complain, in_decl);
13702 if (purpose == error_mark_node)
13703 return error_mark_node;
13705 value = TREE_VALUE (t);
13706 if (value)
13708 value = tsubst (value, args, complain, in_decl);
13709 if (value == error_mark_node)
13710 return error_mark_node;
13712 chain = TREE_CHAIN (t);
13713 if (chain && chain != void_type_node)
13715 chain = tsubst (chain, args, complain, in_decl);
13716 if (chain == error_mark_node)
13717 return error_mark_node;
13719 if (purpose == TREE_PURPOSE (t)
13720 && value == TREE_VALUE (t)
13721 && chain == TREE_CHAIN (t))
13722 return t;
13723 return hash_tree_cons (purpose, value, chain);
13726 case TREE_BINFO:
13727 /* We should never be tsubsting a binfo. */
13728 gcc_unreachable ();
13730 case TREE_VEC:
13731 /* A vector of template arguments. */
13732 gcc_assert (!type);
13733 return tsubst_template_args (t, args, complain, in_decl);
13735 case POINTER_TYPE:
13736 case REFERENCE_TYPE:
13738 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13739 return t;
13741 /* [temp.deduct]
13743 Type deduction may fail for any of the following
13744 reasons:
13746 -- Attempting to create a pointer to reference type.
13747 -- Attempting to create a reference to a reference type or
13748 a reference to void.
13750 Core issue 106 says that creating a reference to a reference
13751 during instantiation is no longer a cause for failure. We
13752 only enforce this check in strict C++98 mode. */
13753 if ((TREE_CODE (type) == REFERENCE_TYPE
13754 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13755 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13757 static location_t last_loc;
13759 /* We keep track of the last time we issued this error
13760 message to avoid spewing a ton of messages during a
13761 single bad template instantiation. */
13762 if (complain & tf_error
13763 && last_loc != input_location)
13765 if (VOID_TYPE_P (type))
13766 error ("forming reference to void");
13767 else if (code == POINTER_TYPE)
13768 error ("forming pointer to reference type %qT", type);
13769 else
13770 error ("forming reference to reference type %qT", type);
13771 last_loc = input_location;
13774 return error_mark_node;
13776 else if (TREE_CODE (type) == FUNCTION_TYPE
13777 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13778 || type_memfn_rqual (type) != REF_QUAL_NONE))
13780 if (complain & tf_error)
13782 if (code == POINTER_TYPE)
13783 error ("forming pointer to qualified function type %qT",
13784 type);
13785 else
13786 error ("forming reference to qualified function type %qT",
13787 type);
13789 return error_mark_node;
13791 else if (code == POINTER_TYPE)
13793 r = build_pointer_type (type);
13794 if (TREE_CODE (type) == METHOD_TYPE)
13795 r = build_ptrmemfunc_type (r);
13797 else if (TREE_CODE (type) == REFERENCE_TYPE)
13798 /* In C++0x, during template argument substitution, when there is an
13799 attempt to create a reference to a reference type, reference
13800 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13802 "If a template-argument for a template-parameter T names a type
13803 that is a reference to a type A, an attempt to create the type
13804 'lvalue reference to cv T' creates the type 'lvalue reference to
13805 A,' while an attempt to create the type type rvalue reference to
13806 cv T' creates the type T"
13808 r = cp_build_reference_type
13809 (TREE_TYPE (type),
13810 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13811 else
13812 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13813 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13815 if (r != error_mark_node)
13816 /* Will this ever be needed for TYPE_..._TO values? */
13817 layout_type (r);
13819 return r;
13821 case OFFSET_TYPE:
13823 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13824 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13826 /* [temp.deduct]
13828 Type deduction may fail for any of the following
13829 reasons:
13831 -- Attempting to create "pointer to member of T" when T
13832 is not a class type. */
13833 if (complain & tf_error)
13834 error ("creating pointer to member of non-class type %qT", r);
13835 return error_mark_node;
13837 if (TREE_CODE (type) == REFERENCE_TYPE)
13839 if (complain & tf_error)
13840 error ("creating pointer to member reference type %qT", type);
13841 return error_mark_node;
13843 if (VOID_TYPE_P (type))
13845 if (complain & tf_error)
13846 error ("creating pointer to member of type void");
13847 return error_mark_node;
13849 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13850 if (TREE_CODE (type) == FUNCTION_TYPE)
13852 /* The type of the implicit object parameter gets its
13853 cv-qualifiers from the FUNCTION_TYPE. */
13854 tree memptr;
13855 tree method_type
13856 = build_memfn_type (type, r, type_memfn_quals (type),
13857 type_memfn_rqual (type));
13858 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13859 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13860 complain);
13862 else
13863 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13864 cp_type_quals (t),
13865 complain);
13867 case FUNCTION_TYPE:
13868 case METHOD_TYPE:
13870 tree fntype;
13871 tree specs;
13872 fntype = tsubst_function_type (t, args, complain, in_decl);
13873 if (fntype == error_mark_node)
13874 return error_mark_node;
13876 /* Substitute the exception specification. */
13877 specs = tsubst_exception_specification (t, args, complain, in_decl,
13878 /*defer_ok*/fndecl_type);
13879 if (specs == error_mark_node)
13880 return error_mark_node;
13881 if (specs)
13882 fntype = build_exception_variant (fntype, specs);
13883 return fntype;
13885 case ARRAY_TYPE:
13887 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13888 if (domain == error_mark_node)
13889 return error_mark_node;
13891 /* As an optimization, we avoid regenerating the array type if
13892 it will obviously be the same as T. */
13893 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13894 return t;
13896 /* These checks should match the ones in create_array_type_for_decl.
13898 [temp.deduct]
13900 The deduction may fail for any of the following reasons:
13902 -- Attempting to create an array with an element type that
13903 is void, a function type, or a reference type, or [DR337]
13904 an abstract class type. */
13905 if (VOID_TYPE_P (type)
13906 || TREE_CODE (type) == FUNCTION_TYPE
13907 || (TREE_CODE (type) == ARRAY_TYPE
13908 && TYPE_DOMAIN (type) == NULL_TREE)
13909 || TREE_CODE (type) == REFERENCE_TYPE)
13911 if (complain & tf_error)
13912 error ("creating array of %qT", type);
13913 return error_mark_node;
13916 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13917 return error_mark_node;
13919 r = build_cplus_array_type (type, domain);
13921 if (TYPE_USER_ALIGN (t))
13923 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13924 TYPE_USER_ALIGN (r) = 1;
13927 return r;
13930 case TYPENAME_TYPE:
13932 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13933 in_decl, /*entering_scope=*/1);
13934 if (ctx == error_mark_node)
13935 return error_mark_node;
13937 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13938 complain, in_decl);
13939 if (f == error_mark_node)
13940 return error_mark_node;
13942 if (!MAYBE_CLASS_TYPE_P (ctx))
13944 if (complain & tf_error)
13945 error ("%qT is not a class, struct, or union type", ctx);
13946 return error_mark_node;
13948 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13950 /* Normally, make_typename_type does not require that the CTX
13951 have complete type in order to allow things like:
13953 template <class T> struct S { typename S<T>::X Y; };
13955 But, such constructs have already been resolved by this
13956 point, so here CTX really should have complete type, unless
13957 it's a partial instantiation. */
13958 ctx = complete_type (ctx);
13959 if (!COMPLETE_TYPE_P (ctx))
13961 if (complain & tf_error)
13962 cxx_incomplete_type_error (NULL_TREE, ctx);
13963 return error_mark_node;
13967 f = make_typename_type (ctx, f, typename_type,
13968 complain | tf_keep_type_decl);
13969 if (f == error_mark_node)
13970 return f;
13971 if (TREE_CODE (f) == TYPE_DECL)
13973 complain |= tf_ignore_bad_quals;
13974 f = TREE_TYPE (f);
13977 if (TREE_CODE (f) != TYPENAME_TYPE)
13979 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13981 if (complain & tf_error)
13982 error ("%qT resolves to %qT, which is not an enumeration type",
13983 t, f);
13984 else
13985 return error_mark_node;
13987 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13989 if (complain & tf_error)
13990 error ("%qT resolves to %qT, which is is not a class type",
13991 t, f);
13992 else
13993 return error_mark_node;
13997 return cp_build_qualified_type_real
13998 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14001 case UNBOUND_CLASS_TEMPLATE:
14003 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14004 in_decl, /*entering_scope=*/1);
14005 tree name = TYPE_IDENTIFIER (t);
14006 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14008 if (ctx == error_mark_node || name == error_mark_node)
14009 return error_mark_node;
14011 if (parm_list)
14012 parm_list = tsubst_template_parms (parm_list, args, complain);
14013 return make_unbound_class_template (ctx, name, parm_list, complain);
14016 case TYPEOF_TYPE:
14018 tree type;
14020 ++cp_unevaluated_operand;
14021 ++c_inhibit_evaluation_warnings;
14023 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14024 complain, in_decl,
14025 /*integral_constant_expression_p=*/false);
14027 --cp_unevaluated_operand;
14028 --c_inhibit_evaluation_warnings;
14030 type = finish_typeof (type);
14031 return cp_build_qualified_type_real (type,
14032 cp_type_quals (t)
14033 | cp_type_quals (type),
14034 complain);
14037 case DECLTYPE_TYPE:
14039 tree type;
14041 ++cp_unevaluated_operand;
14042 ++c_inhibit_evaluation_warnings;
14044 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14045 complain|tf_decltype, in_decl,
14046 /*function_p*/false,
14047 /*integral_constant_expression*/false);
14049 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14051 if (type == NULL_TREE)
14053 if (complain & tf_error)
14054 error ("empty initializer in lambda init-capture");
14055 type = error_mark_node;
14057 else if (TREE_CODE (type) == TREE_LIST)
14058 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14061 --cp_unevaluated_operand;
14062 --c_inhibit_evaluation_warnings;
14064 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14065 type = lambda_capture_field_type (type,
14066 DECLTYPE_FOR_INIT_CAPTURE (t),
14067 DECLTYPE_FOR_REF_CAPTURE (t));
14068 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14069 type = lambda_proxy_type (type);
14070 else
14072 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14073 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14074 && EXPR_P (type))
14075 /* In a template ~id could be either a complement expression
14076 or an unqualified-id naming a destructor; if instantiating
14077 it produces an expression, it's not an id-expression or
14078 member access. */
14079 id = false;
14080 type = finish_decltype_type (type, id, complain);
14082 return cp_build_qualified_type_real (type,
14083 cp_type_quals (t)
14084 | cp_type_quals (type),
14085 complain | tf_ignore_bad_quals);
14088 case UNDERLYING_TYPE:
14090 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14091 complain, in_decl);
14092 return finish_underlying_type (type);
14095 case TYPE_ARGUMENT_PACK:
14096 case NONTYPE_ARGUMENT_PACK:
14098 tree r;
14100 if (code == NONTYPE_ARGUMENT_PACK)
14102 r = make_node (code);
14103 /* Set the already-substituted type. */
14104 TREE_TYPE (r) = type;
14106 else
14107 r = cxx_make_type (code);
14109 tree pack_args = ARGUMENT_PACK_ARGS (t);
14110 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14111 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14113 return r;
14116 case VOID_CST:
14117 case INTEGER_CST:
14118 case REAL_CST:
14119 case STRING_CST:
14120 case PLUS_EXPR:
14121 case MINUS_EXPR:
14122 case NEGATE_EXPR:
14123 case NOP_EXPR:
14124 case INDIRECT_REF:
14125 case ADDR_EXPR:
14126 case CALL_EXPR:
14127 case ARRAY_REF:
14128 case SCOPE_REF:
14129 /* We should use one of the expression tsubsts for these codes. */
14130 gcc_unreachable ();
14132 default:
14133 sorry ("use of %qs in template", get_tree_code_name (code));
14134 return error_mark_node;
14138 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14139 type of the expression on the left-hand side of the "." or "->"
14140 operator. */
14142 static tree
14143 tsubst_baselink (tree baselink, tree object_type,
14144 tree args, tsubst_flags_t complain, tree in_decl)
14146 tree name;
14147 tree qualifying_scope;
14148 tree fns;
14149 tree optype;
14150 tree template_args = 0;
14151 bool template_id_p = false;
14152 bool qualified = BASELINK_QUALIFIED_P (baselink);
14154 /* A baselink indicates a function from a base class. Both the
14155 BASELINK_ACCESS_BINFO and the base class referenced may
14156 indicate bases of the template class, rather than the
14157 instantiated class. In addition, lookups that were not
14158 ambiguous before may be ambiguous now. Therefore, we perform
14159 the lookup again. */
14160 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14161 qualifying_scope = tsubst (qualifying_scope, args,
14162 complain, in_decl);
14163 fns = BASELINK_FUNCTIONS (baselink);
14164 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
14165 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14167 template_id_p = true;
14168 template_args = TREE_OPERAND (fns, 1);
14169 fns = TREE_OPERAND (fns, 0);
14170 if (template_args)
14171 template_args = tsubst_template_args (template_args, args,
14172 complain, in_decl);
14174 name = DECL_NAME (get_first_fn (fns));
14175 if (IDENTIFIER_TYPENAME_P (name))
14176 name = mangle_conv_op_name_for_type (optype);
14177 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14178 if (!baselink)
14180 if (constructor_name_p (name, qualifying_scope))
14182 if (complain & tf_error)
14183 error ("cannot call constructor %<%T::%D%> directly",
14184 qualifying_scope, name);
14186 return error_mark_node;
14189 /* If lookup found a single function, mark it as used at this
14190 point. (If it lookup found multiple functions the one selected
14191 later by overload resolution will be marked as used at that
14192 point.) */
14193 if (BASELINK_P (baselink))
14194 fns = BASELINK_FUNCTIONS (baselink);
14195 if (!template_id_p && !really_overloaded_fn (fns)
14196 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
14197 return error_mark_node;
14199 /* Add back the template arguments, if present. */
14200 if (BASELINK_P (baselink) && template_id_p)
14201 BASELINK_FUNCTIONS (baselink)
14202 = build2 (TEMPLATE_ID_EXPR,
14203 unknown_type_node,
14204 BASELINK_FUNCTIONS (baselink),
14205 template_args);
14206 /* Update the conversion operator type. */
14207 if (BASELINK_P (baselink))
14208 BASELINK_OPTYPE (baselink) = optype;
14210 if (!object_type)
14211 object_type = current_class_type;
14213 if (qualified || name == complete_dtor_identifier)
14215 baselink = adjust_result_of_qualified_name_lookup (baselink,
14216 qualifying_scope,
14217 object_type);
14218 if (!qualified)
14219 /* We need to call adjust_result_of_qualified_name_lookup in case the
14220 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14221 so that we still get virtual function binding. */
14222 BASELINK_QUALIFIED_P (baselink) = false;
14224 return baselink;
14227 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14228 true if the qualified-id will be a postfix-expression in-and-of
14229 itself; false if more of the postfix-expression follows the
14230 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14231 of "&". */
14233 static tree
14234 tsubst_qualified_id (tree qualified_id, tree args,
14235 tsubst_flags_t complain, tree in_decl,
14236 bool done, bool address_p)
14238 tree expr;
14239 tree scope;
14240 tree name;
14241 bool is_template;
14242 tree template_args;
14243 location_t loc = UNKNOWN_LOCATION;
14245 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14247 /* Figure out what name to look up. */
14248 name = TREE_OPERAND (qualified_id, 1);
14249 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14251 is_template = true;
14252 loc = EXPR_LOCATION (name);
14253 template_args = TREE_OPERAND (name, 1);
14254 if (template_args)
14255 template_args = tsubst_template_args (template_args, args,
14256 complain, in_decl);
14257 if (template_args == error_mark_node)
14258 return error_mark_node;
14259 name = TREE_OPERAND (name, 0);
14261 else
14263 is_template = false;
14264 template_args = NULL_TREE;
14267 /* Substitute into the qualifying scope. When there are no ARGS, we
14268 are just trying to simplify a non-dependent expression. In that
14269 case the qualifying scope may be dependent, and, in any case,
14270 substituting will not help. */
14271 scope = TREE_OPERAND (qualified_id, 0);
14272 if (args)
14274 scope = tsubst (scope, args, complain, in_decl);
14275 expr = tsubst_copy (name, args, complain, in_decl);
14277 else
14278 expr = name;
14280 if (dependent_scope_p (scope))
14282 if (is_template)
14283 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14284 tree r = build_qualified_name (NULL_TREE, scope, expr,
14285 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14286 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14287 return r;
14290 if (!BASELINK_P (name) && !DECL_P (expr))
14292 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14294 /* A BIT_NOT_EXPR is used to represent a destructor. */
14295 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14297 error ("qualifying type %qT does not match destructor name ~%qT",
14298 scope, TREE_OPERAND (expr, 0));
14299 expr = error_mark_node;
14301 else
14302 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14303 /*is_type_p=*/0, false);
14305 else
14306 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14307 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14308 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14310 if (complain & tf_error)
14312 error ("dependent-name %qE is parsed as a non-type, but "
14313 "instantiation yields a type", qualified_id);
14314 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14316 return error_mark_node;
14320 if (DECL_P (expr))
14322 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14323 scope);
14324 /* Remember that there was a reference to this entity. */
14325 if (!mark_used (expr, complain) && !(complain & tf_error))
14326 return error_mark_node;
14329 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14331 if (complain & tf_error)
14332 qualified_name_lookup_error (scope,
14333 TREE_OPERAND (qualified_id, 1),
14334 expr, input_location);
14335 return error_mark_node;
14338 if (is_template)
14340 if (variable_template_p (expr))
14341 expr = lookup_and_finish_template_variable (expr, template_args,
14342 complain);
14343 else
14344 expr = lookup_template_function (expr, template_args);
14347 if (expr == error_mark_node && complain & tf_error)
14348 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14349 expr, input_location);
14350 else if (TYPE_P (scope))
14352 expr = (adjust_result_of_qualified_name_lookup
14353 (expr, scope, current_nonlambda_class_type ()));
14354 expr = (finish_qualified_id_expr
14355 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14356 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14357 /*template_arg_p=*/false, complain));
14360 /* Expressions do not generally have reference type. */
14361 if (TREE_CODE (expr) != SCOPE_REF
14362 /* However, if we're about to form a pointer-to-member, we just
14363 want the referenced member referenced. */
14364 && TREE_CODE (expr) != OFFSET_REF)
14365 expr = convert_from_reference (expr);
14367 if (REF_PARENTHESIZED_P (qualified_id))
14368 expr = force_paren_expr (expr);
14370 return expr;
14373 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14374 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14375 for tsubst. */
14377 static tree
14378 tsubst_init (tree init, tree decl, tree args,
14379 tsubst_flags_t complain, tree in_decl)
14381 if (!init)
14382 return NULL_TREE;
14384 init = tsubst_expr (init, args, complain, in_decl, false);
14386 if (!init && TREE_TYPE (decl) != error_mark_node)
14388 /* If we had an initializer but it
14389 instantiated to nothing,
14390 value-initialize the object. This will
14391 only occur when the initializer was a
14392 pack expansion where the parameter packs
14393 used in that expansion were of length
14394 zero. */
14395 init = build_value_init (TREE_TYPE (decl),
14396 complain);
14397 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14398 init = get_target_expr_sfinae (init, complain);
14401 return init;
14404 /* Like tsubst, but deals with expressions. This function just replaces
14405 template parms; to finish processing the resultant expression, use
14406 tsubst_copy_and_build or tsubst_expr. */
14408 static tree
14409 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14411 enum tree_code code;
14412 tree r;
14414 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14415 return t;
14417 code = TREE_CODE (t);
14419 switch (code)
14421 case PARM_DECL:
14422 r = retrieve_local_specialization (t);
14424 if (r == NULL_TREE)
14426 /* We get here for a use of 'this' in an NSDMI as part of a
14427 constructor call or as part of an aggregate initialization. */
14428 if (DECL_NAME (t) == this_identifier
14429 && ((current_function_decl
14430 && DECL_CONSTRUCTOR_P (current_function_decl))
14431 || (current_class_ref
14432 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14433 return current_class_ptr;
14435 /* This can happen for a parameter name used later in a function
14436 declaration (such as in a late-specified return type). Just
14437 make a dummy decl, since it's only used for its type. */
14438 gcc_assert (cp_unevaluated_operand != 0);
14439 r = tsubst_decl (t, args, complain);
14440 /* Give it the template pattern as its context; its true context
14441 hasn't been instantiated yet and this is good enough for
14442 mangling. */
14443 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14446 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14447 r = ARGUMENT_PACK_SELECT_ARG (r);
14448 if (!mark_used (r, complain) && !(complain & tf_error))
14449 return error_mark_node;
14450 return r;
14452 case CONST_DECL:
14454 tree enum_type;
14455 tree v;
14457 if (DECL_TEMPLATE_PARM_P (t))
14458 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14459 /* There is no need to substitute into namespace-scope
14460 enumerators. */
14461 if (DECL_NAMESPACE_SCOPE_P (t))
14462 return t;
14463 /* If ARGS is NULL, then T is known to be non-dependent. */
14464 if (args == NULL_TREE)
14465 return scalar_constant_value (t);
14467 /* Unfortunately, we cannot just call lookup_name here.
14468 Consider:
14470 template <int I> int f() {
14471 enum E { a = I };
14472 struct S { void g() { E e = a; } };
14475 When we instantiate f<7>::S::g(), say, lookup_name is not
14476 clever enough to find f<7>::a. */
14477 enum_type
14478 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14479 /*entering_scope=*/0);
14481 for (v = TYPE_VALUES (enum_type);
14482 v != NULL_TREE;
14483 v = TREE_CHAIN (v))
14484 if (TREE_PURPOSE (v) == DECL_NAME (t))
14485 return TREE_VALUE (v);
14487 /* We didn't find the name. That should never happen; if
14488 name-lookup found it during preliminary parsing, we
14489 should find it again here during instantiation. */
14490 gcc_unreachable ();
14492 return t;
14494 case FIELD_DECL:
14495 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14497 /* Check for a local specialization set up by
14498 tsubst_pack_expansion. */
14499 if (tree r = retrieve_local_specialization (t))
14501 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14502 r = ARGUMENT_PACK_SELECT_ARG (r);
14503 return r;
14506 /* When retrieving a capture pack from a generic lambda, remove the
14507 lambda call op's own template argument list from ARGS. Only the
14508 template arguments active for the closure type should be used to
14509 retrieve the pack specialization. */
14510 if (LAMBDA_FUNCTION_P (current_function_decl)
14511 && (template_class_depth (DECL_CONTEXT (t))
14512 != TMPL_ARGS_DEPTH (args)))
14513 args = strip_innermost_template_args (args, 1);
14515 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14516 tsubst_decl put in the hash table. */
14517 return retrieve_specialization (t, args, 0);
14520 if (DECL_CONTEXT (t))
14522 tree ctx;
14524 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14525 /*entering_scope=*/1);
14526 if (ctx != DECL_CONTEXT (t))
14528 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14529 if (!r)
14531 if (complain & tf_error)
14532 error ("using invalid field %qD", t);
14533 return error_mark_node;
14535 return r;
14539 return t;
14541 case VAR_DECL:
14542 case FUNCTION_DECL:
14543 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14544 r = tsubst (t, args, complain, in_decl);
14545 else if (local_variable_p (t)
14546 && uses_template_parms (DECL_CONTEXT (t)))
14548 r = retrieve_local_specialization (t);
14549 if (r == NULL_TREE)
14551 /* First try name lookup to find the instantiation. */
14552 r = lookup_name (DECL_NAME (t));
14553 if (r)
14555 /* Make sure that the one we found is the one we want. */
14556 tree ctx = DECL_CONTEXT (t);
14557 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14558 ctx = tsubst (ctx, args, complain, in_decl);
14559 if (ctx != DECL_CONTEXT (r))
14560 r = NULL_TREE;
14563 if (r)
14564 /* OK */;
14565 else
14567 /* This can happen for a variable used in a
14568 late-specified return type of a local lambda, or for a
14569 local static or constant. Building a new VAR_DECL
14570 should be OK in all those cases. */
14571 r = tsubst_decl (t, args, complain);
14572 if (decl_maybe_constant_var_p (r))
14574 /* We can't call cp_finish_decl, so handle the
14575 initializer by hand. */
14576 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14577 complain, in_decl);
14578 if (!processing_template_decl)
14579 init = maybe_constant_init (init);
14580 if (processing_template_decl
14581 ? potential_constant_expression (init)
14582 : reduced_constant_expression_p (init))
14583 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14584 = TREE_CONSTANT (r) = true;
14585 DECL_INITIAL (r) = init;
14587 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14588 || decl_constant_var_p (r)
14589 || errorcount || sorrycount);
14590 if (!processing_template_decl
14591 && !TREE_STATIC (r))
14592 r = process_outer_var_ref (r, complain);
14594 /* Remember this for subsequent uses. */
14595 if (local_specializations)
14596 register_local_specialization (r, t);
14599 else
14600 r = t;
14601 if (!mark_used (r, complain))
14602 return error_mark_node;
14603 return r;
14605 case NAMESPACE_DECL:
14606 return t;
14608 case OVERLOAD:
14609 /* An OVERLOAD will always be a non-dependent overload set; an
14610 overload set from function scope will just be represented with an
14611 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14612 gcc_assert (!uses_template_parms (t));
14613 return t;
14615 case BASELINK:
14616 return tsubst_baselink (t, current_nonlambda_class_type (),
14617 args, complain, in_decl);
14619 case TEMPLATE_DECL:
14620 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14621 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14622 args, complain, in_decl);
14623 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14624 return tsubst (t, args, complain, in_decl);
14625 else if (DECL_CLASS_SCOPE_P (t)
14626 && uses_template_parms (DECL_CONTEXT (t)))
14628 /* Template template argument like the following example need
14629 special treatment:
14631 template <template <class> class TT> struct C {};
14632 template <class T> struct D {
14633 template <class U> struct E {};
14634 C<E> c; // #1
14636 D<int> d; // #2
14638 We are processing the template argument `E' in #1 for
14639 the template instantiation #2. Originally, `E' is a
14640 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14641 have to substitute this with one having context `D<int>'. */
14643 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14644 if (dependent_scope_p (context))
14646 /* When rewriting a constructor into a deduction guide, a
14647 non-dependent name can become dependent, so memtmpl<args>
14648 becomes context::template memtmpl<args>. */
14649 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14650 return build_qualified_name (type, context, DECL_NAME (t),
14651 /*template*/true);
14653 return lookup_field (context, DECL_NAME(t), 0, false);
14655 else
14656 /* Ordinary template template argument. */
14657 return t;
14659 case CAST_EXPR:
14660 case REINTERPRET_CAST_EXPR:
14661 case CONST_CAST_EXPR:
14662 case STATIC_CAST_EXPR:
14663 case DYNAMIC_CAST_EXPR:
14664 case IMPLICIT_CONV_EXPR:
14665 case CONVERT_EXPR:
14666 case NOP_EXPR:
14668 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14669 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14670 return build1 (code, type, op0);
14673 case SIZEOF_EXPR:
14674 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14675 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14677 tree expanded, op = TREE_OPERAND (t, 0);
14678 int len = 0;
14680 if (SIZEOF_EXPR_TYPE_P (t))
14681 op = TREE_TYPE (op);
14683 ++cp_unevaluated_operand;
14684 ++c_inhibit_evaluation_warnings;
14685 /* We only want to compute the number of arguments. */
14686 if (PACK_EXPANSION_P (op))
14687 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14688 else
14689 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14690 args, complain, in_decl);
14691 --cp_unevaluated_operand;
14692 --c_inhibit_evaluation_warnings;
14694 if (TREE_CODE (expanded) == TREE_VEC)
14696 len = TREE_VEC_LENGTH (expanded);
14697 /* Set TREE_USED for the benefit of -Wunused. */
14698 for (int i = 0; i < len; i++)
14699 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14700 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14703 if (expanded == error_mark_node)
14704 return error_mark_node;
14705 else if (PACK_EXPANSION_P (expanded)
14706 || (TREE_CODE (expanded) == TREE_VEC
14707 && pack_expansion_args_count (expanded)))
14710 if (PACK_EXPANSION_P (expanded))
14711 /* OK. */;
14712 else if (TREE_VEC_LENGTH (expanded) == 1)
14713 expanded = TREE_VEC_ELT (expanded, 0);
14714 else
14715 expanded = make_argument_pack (expanded);
14717 if (TYPE_P (expanded))
14718 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14719 complain & tf_error);
14720 else
14721 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14722 complain & tf_error);
14724 else
14725 return build_int_cst (size_type_node, len);
14727 if (SIZEOF_EXPR_TYPE_P (t))
14729 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14730 args, complain, in_decl);
14731 r = build1 (NOP_EXPR, r, error_mark_node);
14732 r = build1 (SIZEOF_EXPR,
14733 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14734 SIZEOF_EXPR_TYPE_P (r) = 1;
14735 return r;
14737 /* Fall through */
14739 case INDIRECT_REF:
14740 case NEGATE_EXPR:
14741 case TRUTH_NOT_EXPR:
14742 case BIT_NOT_EXPR:
14743 case ADDR_EXPR:
14744 case UNARY_PLUS_EXPR: /* Unary + */
14745 case ALIGNOF_EXPR:
14746 case AT_ENCODE_EXPR:
14747 case ARROW_EXPR:
14748 case THROW_EXPR:
14749 case TYPEID_EXPR:
14750 case REALPART_EXPR:
14751 case IMAGPART_EXPR:
14752 case PAREN_EXPR:
14754 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14755 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14756 return build1 (code, type, op0);
14759 case COMPONENT_REF:
14761 tree object;
14762 tree name;
14764 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14765 name = TREE_OPERAND (t, 1);
14766 if (TREE_CODE (name) == BIT_NOT_EXPR)
14768 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14769 complain, in_decl);
14770 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14772 else if (TREE_CODE (name) == SCOPE_REF
14773 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14775 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14776 complain, in_decl);
14777 name = TREE_OPERAND (name, 1);
14778 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14779 complain, in_decl);
14780 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14781 name = build_qualified_name (/*type=*/NULL_TREE,
14782 base, name,
14783 /*template_p=*/false);
14785 else if (BASELINK_P (name))
14786 name = tsubst_baselink (name,
14787 non_reference (TREE_TYPE (object)),
14788 args, complain,
14789 in_decl);
14790 else
14791 name = tsubst_copy (name, args, complain, in_decl);
14792 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14795 case PLUS_EXPR:
14796 case MINUS_EXPR:
14797 case MULT_EXPR:
14798 case TRUNC_DIV_EXPR:
14799 case CEIL_DIV_EXPR:
14800 case FLOOR_DIV_EXPR:
14801 case ROUND_DIV_EXPR:
14802 case EXACT_DIV_EXPR:
14803 case BIT_AND_EXPR:
14804 case BIT_IOR_EXPR:
14805 case BIT_XOR_EXPR:
14806 case TRUNC_MOD_EXPR:
14807 case FLOOR_MOD_EXPR:
14808 case TRUTH_ANDIF_EXPR:
14809 case TRUTH_ORIF_EXPR:
14810 case TRUTH_AND_EXPR:
14811 case TRUTH_OR_EXPR:
14812 case RSHIFT_EXPR:
14813 case LSHIFT_EXPR:
14814 case RROTATE_EXPR:
14815 case LROTATE_EXPR:
14816 case EQ_EXPR:
14817 case NE_EXPR:
14818 case MAX_EXPR:
14819 case MIN_EXPR:
14820 case LE_EXPR:
14821 case GE_EXPR:
14822 case LT_EXPR:
14823 case GT_EXPR:
14824 case COMPOUND_EXPR:
14825 case DOTSTAR_EXPR:
14826 case MEMBER_REF:
14827 case PREDECREMENT_EXPR:
14828 case PREINCREMENT_EXPR:
14829 case POSTDECREMENT_EXPR:
14830 case POSTINCREMENT_EXPR:
14832 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14833 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14834 return build_nt (code, op0, op1);
14837 case SCOPE_REF:
14839 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14840 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14841 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14842 QUALIFIED_NAME_IS_TEMPLATE (t));
14845 case ARRAY_REF:
14847 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14848 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14849 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14852 case CALL_EXPR:
14854 int n = VL_EXP_OPERAND_LENGTH (t);
14855 tree result = build_vl_exp (CALL_EXPR, n);
14856 int i;
14857 for (i = 0; i < n; i++)
14858 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14859 complain, in_decl);
14860 return result;
14863 case COND_EXPR:
14864 case MODOP_EXPR:
14865 case PSEUDO_DTOR_EXPR:
14866 case VEC_PERM_EXPR:
14868 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14869 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14870 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14871 r = build_nt (code, op0, op1, op2);
14872 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14873 return r;
14876 case NEW_EXPR:
14878 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14879 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14880 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14881 r = build_nt (code, op0, op1, op2);
14882 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14883 return r;
14886 case DELETE_EXPR:
14888 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14889 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14890 r = build_nt (code, op0, op1);
14891 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14892 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14893 return r;
14896 case TEMPLATE_ID_EXPR:
14898 /* Substituted template arguments */
14899 tree fn = TREE_OPERAND (t, 0);
14900 tree targs = TREE_OPERAND (t, 1);
14902 fn = tsubst_copy (fn, args, complain, in_decl);
14903 if (targs)
14904 targs = tsubst_template_args (targs, args, complain, in_decl);
14906 return lookup_template_function (fn, targs);
14909 case TREE_LIST:
14911 tree purpose, value, chain;
14913 if (t == void_list_node)
14914 return t;
14916 purpose = TREE_PURPOSE (t);
14917 if (purpose)
14918 purpose = tsubst_copy (purpose, args, complain, in_decl);
14919 value = TREE_VALUE (t);
14920 if (value)
14921 value = tsubst_copy (value, args, complain, in_decl);
14922 chain = TREE_CHAIN (t);
14923 if (chain && chain != void_type_node)
14924 chain = tsubst_copy (chain, args, complain, in_decl);
14925 if (purpose == TREE_PURPOSE (t)
14926 && value == TREE_VALUE (t)
14927 && chain == TREE_CHAIN (t))
14928 return t;
14929 return tree_cons (purpose, value, chain);
14932 case RECORD_TYPE:
14933 case UNION_TYPE:
14934 case ENUMERAL_TYPE:
14935 case INTEGER_TYPE:
14936 case TEMPLATE_TYPE_PARM:
14937 case TEMPLATE_TEMPLATE_PARM:
14938 case BOUND_TEMPLATE_TEMPLATE_PARM:
14939 case TEMPLATE_PARM_INDEX:
14940 case POINTER_TYPE:
14941 case REFERENCE_TYPE:
14942 case OFFSET_TYPE:
14943 case FUNCTION_TYPE:
14944 case METHOD_TYPE:
14945 case ARRAY_TYPE:
14946 case TYPENAME_TYPE:
14947 case UNBOUND_CLASS_TEMPLATE:
14948 case TYPEOF_TYPE:
14949 case DECLTYPE_TYPE:
14950 case TYPE_DECL:
14951 return tsubst (t, args, complain, in_decl);
14953 case USING_DECL:
14954 t = DECL_NAME (t);
14955 /* Fall through. */
14956 case IDENTIFIER_NODE:
14957 if (IDENTIFIER_TYPENAME_P (t))
14959 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14960 return mangle_conv_op_name_for_type (new_type);
14962 else
14963 return t;
14965 case CONSTRUCTOR:
14966 /* This is handled by tsubst_copy_and_build. */
14967 gcc_unreachable ();
14969 case VA_ARG_EXPR:
14971 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14972 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14973 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14976 case CLEANUP_POINT_EXPR:
14977 /* We shouldn't have built any of these during initial template
14978 generation. Instead, they should be built during instantiation
14979 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14980 gcc_unreachable ();
14982 case OFFSET_REF:
14984 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14985 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14986 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14987 r = build2 (code, type, op0, op1);
14988 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14989 if (!mark_used (TREE_OPERAND (r, 1), complain)
14990 && !(complain & tf_error))
14991 return error_mark_node;
14992 return r;
14995 case EXPR_PACK_EXPANSION:
14996 error ("invalid use of pack expansion expression");
14997 return error_mark_node;
14999 case NONTYPE_ARGUMENT_PACK:
15000 error ("use %<...%> to expand argument pack");
15001 return error_mark_node;
15003 case VOID_CST:
15004 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15005 return t;
15007 case INTEGER_CST:
15008 case REAL_CST:
15009 case STRING_CST:
15010 case COMPLEX_CST:
15012 /* Instantiate any typedefs in the type. */
15013 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15014 r = fold_convert (type, t);
15015 gcc_assert (TREE_CODE (r) == code);
15016 return r;
15019 case PTRMEM_CST:
15020 /* These can sometimes show up in a partial instantiation, but never
15021 involve template parms. */
15022 gcc_assert (!uses_template_parms (t));
15023 return t;
15025 case UNARY_LEFT_FOLD_EXPR:
15026 return tsubst_unary_left_fold (t, args, complain, in_decl);
15027 case UNARY_RIGHT_FOLD_EXPR:
15028 return tsubst_unary_right_fold (t, args, complain, in_decl);
15029 case BINARY_LEFT_FOLD_EXPR:
15030 return tsubst_binary_left_fold (t, args, complain, in_decl);
15031 case BINARY_RIGHT_FOLD_EXPR:
15032 return tsubst_binary_right_fold (t, args, complain, in_decl);
15034 default:
15035 /* We shouldn't get here, but keep going if !flag_checking. */
15036 if (flag_checking)
15037 gcc_unreachable ();
15038 return t;
15042 /* Helper function for tsubst_omp_clauses, used for instantiation of
15043 OMP_CLAUSE_DECL of clauses. */
15045 static tree
15046 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15047 tree in_decl)
15049 if (decl == NULL_TREE)
15050 return NULL_TREE;
15052 /* Handle an OpenMP array section represented as a TREE_LIST (or
15053 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15054 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15055 TREE_LIST. We can handle it exactly the same as an array section
15056 (purpose, value, and a chain), even though the nomenclature
15057 (low_bound, length, etc) is different. */
15058 if (TREE_CODE (decl) == TREE_LIST)
15060 tree low_bound
15061 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15062 /*integral_constant_expression_p=*/false);
15063 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15064 /*integral_constant_expression_p=*/false);
15065 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15066 in_decl);
15067 if (TREE_PURPOSE (decl) == low_bound
15068 && TREE_VALUE (decl) == length
15069 && TREE_CHAIN (decl) == chain)
15070 return decl;
15071 tree ret = tree_cons (low_bound, length, chain);
15072 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15073 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15074 return ret;
15076 tree ret = tsubst_expr (decl, args, complain, in_decl,
15077 /*integral_constant_expression_p=*/false);
15078 /* Undo convert_from_reference tsubst_expr could have called. */
15079 if (decl
15080 && REFERENCE_REF_P (ret)
15081 && !REFERENCE_REF_P (decl))
15082 ret = TREE_OPERAND (ret, 0);
15083 return ret;
15086 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15088 static tree
15089 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15090 tree args, tsubst_flags_t complain, tree in_decl)
15092 tree new_clauses = NULL_TREE, nc, oc;
15093 tree linear_no_step = NULL_TREE;
15095 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15097 nc = copy_node (oc);
15098 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15099 new_clauses = nc;
15101 switch (OMP_CLAUSE_CODE (nc))
15103 case OMP_CLAUSE_LASTPRIVATE:
15104 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15106 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15107 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15108 in_decl, /*integral_constant_expression_p=*/false);
15109 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15110 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15112 /* FALLTHRU */
15113 case OMP_CLAUSE_PRIVATE:
15114 case OMP_CLAUSE_SHARED:
15115 case OMP_CLAUSE_FIRSTPRIVATE:
15116 case OMP_CLAUSE_COPYIN:
15117 case OMP_CLAUSE_COPYPRIVATE:
15118 case OMP_CLAUSE_UNIFORM:
15119 case OMP_CLAUSE_DEPEND:
15120 case OMP_CLAUSE_FROM:
15121 case OMP_CLAUSE_TO:
15122 case OMP_CLAUSE_MAP:
15123 case OMP_CLAUSE_USE_DEVICE_PTR:
15124 case OMP_CLAUSE_IS_DEVICE_PTR:
15125 OMP_CLAUSE_DECL (nc)
15126 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15127 in_decl);
15128 break;
15129 case OMP_CLAUSE_TILE:
15130 case OMP_CLAUSE_IF:
15131 case OMP_CLAUSE_NUM_THREADS:
15132 case OMP_CLAUSE_SCHEDULE:
15133 case OMP_CLAUSE_COLLAPSE:
15134 case OMP_CLAUSE_FINAL:
15135 case OMP_CLAUSE_DEVICE:
15136 case OMP_CLAUSE_DIST_SCHEDULE:
15137 case OMP_CLAUSE_NUM_TEAMS:
15138 case OMP_CLAUSE_THREAD_LIMIT:
15139 case OMP_CLAUSE_SAFELEN:
15140 case OMP_CLAUSE_SIMDLEN:
15141 case OMP_CLAUSE_NUM_TASKS:
15142 case OMP_CLAUSE_GRAINSIZE:
15143 case OMP_CLAUSE_PRIORITY:
15144 case OMP_CLAUSE_ORDERED:
15145 case OMP_CLAUSE_HINT:
15146 case OMP_CLAUSE_NUM_GANGS:
15147 case OMP_CLAUSE_NUM_WORKERS:
15148 case OMP_CLAUSE_VECTOR_LENGTH:
15149 case OMP_CLAUSE_WORKER:
15150 case OMP_CLAUSE_VECTOR:
15151 case OMP_CLAUSE_ASYNC:
15152 case OMP_CLAUSE_WAIT:
15153 OMP_CLAUSE_OPERAND (nc, 0)
15154 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15155 in_decl, /*integral_constant_expression_p=*/false);
15156 break;
15157 case OMP_CLAUSE_REDUCTION:
15158 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15160 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15161 if (TREE_CODE (placeholder) == SCOPE_REF)
15163 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15164 complain, in_decl);
15165 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15166 = build_qualified_name (NULL_TREE, scope,
15167 TREE_OPERAND (placeholder, 1),
15168 false);
15170 else
15171 gcc_assert (identifier_p (placeholder));
15173 OMP_CLAUSE_DECL (nc)
15174 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15175 in_decl);
15176 break;
15177 case OMP_CLAUSE_GANG:
15178 case OMP_CLAUSE_ALIGNED:
15179 OMP_CLAUSE_DECL (nc)
15180 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15181 in_decl);
15182 OMP_CLAUSE_OPERAND (nc, 1)
15183 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15184 in_decl, /*integral_constant_expression_p=*/false);
15185 break;
15186 case OMP_CLAUSE_LINEAR:
15187 OMP_CLAUSE_DECL (nc)
15188 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15189 in_decl);
15190 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15192 gcc_assert (!linear_no_step);
15193 linear_no_step = nc;
15195 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15196 OMP_CLAUSE_LINEAR_STEP (nc)
15197 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15198 complain, in_decl);
15199 else
15200 OMP_CLAUSE_LINEAR_STEP (nc)
15201 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15202 in_decl,
15203 /*integral_constant_expression_p=*/false);
15204 break;
15205 case OMP_CLAUSE_NOWAIT:
15206 case OMP_CLAUSE_DEFAULT:
15207 case OMP_CLAUSE_UNTIED:
15208 case OMP_CLAUSE_MERGEABLE:
15209 case OMP_CLAUSE_INBRANCH:
15210 case OMP_CLAUSE_NOTINBRANCH:
15211 case OMP_CLAUSE_PROC_BIND:
15212 case OMP_CLAUSE_FOR:
15213 case OMP_CLAUSE_PARALLEL:
15214 case OMP_CLAUSE_SECTIONS:
15215 case OMP_CLAUSE_TASKGROUP:
15216 case OMP_CLAUSE_NOGROUP:
15217 case OMP_CLAUSE_THREADS:
15218 case OMP_CLAUSE_SIMD:
15219 case OMP_CLAUSE_DEFAULTMAP:
15220 case OMP_CLAUSE_INDEPENDENT:
15221 case OMP_CLAUSE_AUTO:
15222 case OMP_CLAUSE_SEQ:
15223 break;
15224 default:
15225 gcc_unreachable ();
15227 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15228 switch (OMP_CLAUSE_CODE (nc))
15230 case OMP_CLAUSE_SHARED:
15231 case OMP_CLAUSE_PRIVATE:
15232 case OMP_CLAUSE_FIRSTPRIVATE:
15233 case OMP_CLAUSE_LASTPRIVATE:
15234 case OMP_CLAUSE_COPYPRIVATE:
15235 case OMP_CLAUSE_LINEAR:
15236 case OMP_CLAUSE_REDUCTION:
15237 case OMP_CLAUSE_USE_DEVICE_PTR:
15238 case OMP_CLAUSE_IS_DEVICE_PTR:
15239 /* tsubst_expr on SCOPE_REF results in returning
15240 finish_non_static_data_member result. Undo that here. */
15241 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15242 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15243 == IDENTIFIER_NODE))
15245 tree t = OMP_CLAUSE_DECL (nc);
15246 tree v = t;
15247 while (v)
15248 switch (TREE_CODE (v))
15250 case COMPONENT_REF:
15251 case MEM_REF:
15252 case INDIRECT_REF:
15253 CASE_CONVERT:
15254 case POINTER_PLUS_EXPR:
15255 v = TREE_OPERAND (v, 0);
15256 continue;
15257 case PARM_DECL:
15258 if (DECL_CONTEXT (v) == current_function_decl
15259 && DECL_ARTIFICIAL (v)
15260 && DECL_NAME (v) == this_identifier)
15261 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15262 /* FALLTHRU */
15263 default:
15264 v = NULL_TREE;
15265 break;
15268 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15269 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15270 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15271 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15272 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15274 tree decl = OMP_CLAUSE_DECL (nc);
15275 if (VAR_P (decl))
15277 if (!DECL_LANG_SPECIFIC (decl))
15278 retrofit_lang_decl (decl);
15279 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15282 break;
15283 default:
15284 break;
15288 new_clauses = nreverse (new_clauses);
15289 if (ort != C_ORT_OMP_DECLARE_SIMD)
15291 new_clauses = finish_omp_clauses (new_clauses, ort);
15292 if (linear_no_step)
15293 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15294 if (nc == linear_no_step)
15296 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15297 break;
15300 return new_clauses;
15303 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15305 static tree
15306 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15307 tree in_decl)
15309 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15311 tree purpose, value, chain;
15313 if (t == NULL)
15314 return t;
15316 if (TREE_CODE (t) != TREE_LIST)
15317 return tsubst_copy_and_build (t, args, complain, in_decl,
15318 /*function_p=*/false,
15319 /*integral_constant_expression_p=*/false);
15321 if (t == void_list_node)
15322 return t;
15324 purpose = TREE_PURPOSE (t);
15325 if (purpose)
15326 purpose = RECUR (purpose);
15327 value = TREE_VALUE (t);
15328 if (value)
15330 if (TREE_CODE (value) != LABEL_DECL)
15331 value = RECUR (value);
15332 else
15334 value = lookup_label (DECL_NAME (value));
15335 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15336 TREE_USED (value) = 1;
15339 chain = TREE_CHAIN (t);
15340 if (chain && chain != void_type_node)
15341 chain = RECUR (chain);
15342 return tree_cons (purpose, value, chain);
15343 #undef RECUR
15346 /* Used to temporarily communicate the list of #pragma omp parallel
15347 clauses to #pragma omp for instantiation if they are combined
15348 together. */
15350 static tree *omp_parallel_combined_clauses;
15352 /* Substitute one OMP_FOR iterator. */
15354 static void
15355 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15356 tree initv, tree condv, tree incrv, tree *clauses,
15357 tree args, tsubst_flags_t complain, tree in_decl,
15358 bool integral_constant_expression_p)
15360 #define RECUR(NODE) \
15361 tsubst_expr ((NODE), args, complain, in_decl, \
15362 integral_constant_expression_p)
15363 tree decl, init, cond, incr;
15365 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15366 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15368 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15370 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15371 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15374 decl = TREE_OPERAND (init, 0);
15375 init = TREE_OPERAND (init, 1);
15376 tree decl_expr = NULL_TREE;
15377 if (init && TREE_CODE (init) == DECL_EXPR)
15379 /* We need to jump through some hoops to handle declarations in the
15380 init-statement, since we might need to handle auto deduction,
15381 but we need to keep control of initialization. */
15382 decl_expr = init;
15383 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15384 decl = tsubst_decl (decl, args, complain);
15386 else
15388 if (TREE_CODE (decl) == SCOPE_REF)
15390 decl = RECUR (decl);
15391 if (TREE_CODE (decl) == COMPONENT_REF)
15393 tree v = decl;
15394 while (v)
15395 switch (TREE_CODE (v))
15397 case COMPONENT_REF:
15398 case MEM_REF:
15399 case INDIRECT_REF:
15400 CASE_CONVERT:
15401 case POINTER_PLUS_EXPR:
15402 v = TREE_OPERAND (v, 0);
15403 continue;
15404 case PARM_DECL:
15405 if (DECL_CONTEXT (v) == current_function_decl
15406 && DECL_ARTIFICIAL (v)
15407 && DECL_NAME (v) == this_identifier)
15409 decl = TREE_OPERAND (decl, 1);
15410 decl = omp_privatize_field (decl, false);
15412 /* FALLTHRU */
15413 default:
15414 v = NULL_TREE;
15415 break;
15419 else
15420 decl = RECUR (decl);
15422 init = RECUR (init);
15424 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15425 if (auto_node && init)
15426 TREE_TYPE (decl)
15427 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15429 gcc_assert (!type_dependent_expression_p (decl));
15431 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15433 if (decl_expr)
15435 /* Declare the variable, but don't let that initialize it. */
15436 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15437 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15438 RECUR (decl_expr);
15439 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15442 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15443 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15444 if (TREE_CODE (incr) == MODIFY_EXPR)
15446 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15447 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15448 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15449 NOP_EXPR, rhs, complain);
15451 else
15452 incr = RECUR (incr);
15453 TREE_VEC_ELT (declv, i) = decl;
15454 TREE_VEC_ELT (initv, i) = init;
15455 TREE_VEC_ELT (condv, i) = cond;
15456 TREE_VEC_ELT (incrv, i) = incr;
15457 return;
15460 if (decl_expr)
15462 /* Declare and initialize the variable. */
15463 RECUR (decl_expr);
15464 init = NULL_TREE;
15466 else if (init)
15468 tree *pc;
15469 int j;
15470 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15472 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15474 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15475 && OMP_CLAUSE_DECL (*pc) == decl)
15476 break;
15477 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15478 && OMP_CLAUSE_DECL (*pc) == decl)
15480 if (j)
15481 break;
15482 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15483 tree c = *pc;
15484 *pc = OMP_CLAUSE_CHAIN (c);
15485 OMP_CLAUSE_CHAIN (c) = *clauses;
15486 *clauses = c;
15488 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15489 && OMP_CLAUSE_DECL (*pc) == decl)
15491 error ("iteration variable %qD should not be firstprivate",
15492 decl);
15493 *pc = OMP_CLAUSE_CHAIN (*pc);
15495 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15496 && OMP_CLAUSE_DECL (*pc) == decl)
15498 error ("iteration variable %qD should not be reduction",
15499 decl);
15500 *pc = OMP_CLAUSE_CHAIN (*pc);
15502 else
15503 pc = &OMP_CLAUSE_CHAIN (*pc);
15505 if (*pc)
15506 break;
15508 if (*pc == NULL_TREE)
15510 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15511 OMP_CLAUSE_DECL (c) = decl;
15512 c = finish_omp_clauses (c, C_ORT_OMP);
15513 if (c)
15515 OMP_CLAUSE_CHAIN (c) = *clauses;
15516 *clauses = c;
15520 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15521 if (COMPARISON_CLASS_P (cond))
15523 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15524 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15525 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15527 else
15528 cond = RECUR (cond);
15529 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15530 switch (TREE_CODE (incr))
15532 case PREINCREMENT_EXPR:
15533 case PREDECREMENT_EXPR:
15534 case POSTINCREMENT_EXPR:
15535 case POSTDECREMENT_EXPR:
15536 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15537 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15538 break;
15539 case MODIFY_EXPR:
15540 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15541 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15543 tree rhs = TREE_OPERAND (incr, 1);
15544 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15545 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15546 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15547 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15548 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15549 rhs0, rhs1));
15551 else
15552 incr = RECUR (incr);
15553 break;
15554 case MODOP_EXPR:
15555 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15556 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15558 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15559 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15560 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15561 TREE_TYPE (decl), lhs,
15562 RECUR (TREE_OPERAND (incr, 2))));
15564 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15565 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15566 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15568 tree rhs = TREE_OPERAND (incr, 2);
15569 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15570 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15571 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15572 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15573 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15574 rhs0, rhs1));
15576 else
15577 incr = RECUR (incr);
15578 break;
15579 default:
15580 incr = RECUR (incr);
15581 break;
15584 TREE_VEC_ELT (declv, i) = decl;
15585 TREE_VEC_ELT (initv, i) = init;
15586 TREE_VEC_ELT (condv, i) = cond;
15587 TREE_VEC_ELT (incrv, i) = incr;
15588 #undef RECUR
15591 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15592 of OMP_TARGET's body. */
15594 static tree
15595 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15597 *walk_subtrees = 0;
15598 switch (TREE_CODE (*tp))
15600 case OMP_TEAMS:
15601 return *tp;
15602 case BIND_EXPR:
15603 case STATEMENT_LIST:
15604 *walk_subtrees = 1;
15605 break;
15606 default:
15607 break;
15609 return NULL_TREE;
15612 /* Helper function for tsubst_expr. For decomposition declaration
15613 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15614 also the corresponding decls representing the identifiers
15615 of the decomposition declaration. Return DECL if successful
15616 or error_mark_node otherwise, set *FIRST to the first decl
15617 in the list chained through DECL_CHAIN and *CNT to the number
15618 of such decls. */
15620 static tree
15621 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15622 tsubst_flags_t complain, tree in_decl, tree *first,
15623 unsigned int *cnt)
15625 tree decl2, decl3, prev = decl;
15626 *cnt = 0;
15627 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15628 for (decl2 = DECL_CHAIN (pattern_decl);
15629 decl2
15630 && VAR_P (decl2)
15631 && DECL_DECOMPOSITION_P (decl2)
15632 && DECL_NAME (decl2);
15633 decl2 = DECL_CHAIN (decl2))
15635 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15637 gcc_assert (errorcount);
15638 return error_mark_node;
15640 (*cnt)++;
15641 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15642 tree v = DECL_VALUE_EXPR (decl2);
15643 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15644 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15645 decl3 = tsubst (decl2, args, complain, in_decl);
15646 SET_DECL_VALUE_EXPR (decl2, v);
15647 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15648 if (VAR_P (decl3))
15649 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15650 maybe_push_decl (decl3);
15651 if (error_operand_p (decl3))
15652 decl = error_mark_node;
15653 else if (decl != error_mark_node
15654 && DECL_CHAIN (decl3) != prev)
15656 gcc_assert (errorcount);
15657 decl = error_mark_node;
15659 else
15660 prev = decl3;
15662 *first = prev;
15663 return decl;
15666 /* Like tsubst_copy for expressions, etc. but also does semantic
15667 processing. */
15669 tree
15670 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15671 bool integral_constant_expression_p)
15673 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15674 #define RECUR(NODE) \
15675 tsubst_expr ((NODE), args, complain, in_decl, \
15676 integral_constant_expression_p)
15678 tree stmt, tmp;
15679 tree r;
15680 location_t loc;
15682 if (t == NULL_TREE || t == error_mark_node)
15683 return t;
15685 loc = input_location;
15686 if (EXPR_HAS_LOCATION (t))
15687 input_location = EXPR_LOCATION (t);
15688 if (STATEMENT_CODE_P (TREE_CODE (t)))
15689 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15691 switch (TREE_CODE (t))
15693 case STATEMENT_LIST:
15695 tree_stmt_iterator i;
15696 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15697 RECUR (tsi_stmt (i));
15698 break;
15701 case CTOR_INITIALIZER:
15702 finish_mem_initializers (tsubst_initializer_list
15703 (TREE_OPERAND (t, 0), args));
15704 break;
15706 case RETURN_EXPR:
15707 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15708 break;
15710 case EXPR_STMT:
15711 tmp = RECUR (EXPR_STMT_EXPR (t));
15712 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15713 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15714 else
15715 finish_expr_stmt (tmp);
15716 break;
15718 case USING_STMT:
15719 do_using_directive (USING_STMT_NAMESPACE (t));
15720 break;
15722 case DECL_EXPR:
15724 tree decl, pattern_decl;
15725 tree init;
15727 pattern_decl = decl = DECL_EXPR_DECL (t);
15728 if (TREE_CODE (decl) == LABEL_DECL)
15729 finish_label_decl (DECL_NAME (decl));
15730 else if (TREE_CODE (decl) == USING_DECL)
15732 tree scope = USING_DECL_SCOPE (decl);
15733 tree name = DECL_NAME (decl);
15735 scope = tsubst (scope, args, complain, in_decl);
15736 decl = lookup_qualified_name (scope, name,
15737 /*is_type_p=*/false,
15738 /*complain=*/false);
15739 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15740 qualified_name_lookup_error (scope, name, decl, input_location);
15741 else
15742 do_local_using_decl (decl, scope, name);
15744 else if (DECL_PACK_P (decl))
15746 /* Don't build up decls for a variadic capture proxy, we'll
15747 instantiate the elements directly as needed. */
15748 break;
15750 else
15752 init = DECL_INITIAL (decl);
15753 decl = tsubst (decl, args, complain, in_decl);
15754 if (decl != error_mark_node)
15756 /* By marking the declaration as instantiated, we avoid
15757 trying to instantiate it. Since instantiate_decl can't
15758 handle local variables, and since we've already done
15759 all that needs to be done, that's the right thing to
15760 do. */
15761 if (VAR_P (decl))
15762 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15763 if (VAR_P (decl)
15764 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15765 /* Anonymous aggregates are a special case. */
15766 finish_anon_union (decl);
15767 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15769 DECL_CONTEXT (decl) = current_function_decl;
15770 if (DECL_NAME (decl) == this_identifier)
15772 tree lam = DECL_CONTEXT (current_function_decl);
15773 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15774 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15776 insert_capture_proxy (decl);
15778 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15779 /* We already did a pushtag. */;
15780 else if (TREE_CODE (decl) == FUNCTION_DECL
15781 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15782 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15784 DECL_CONTEXT (decl) = NULL_TREE;
15785 pushdecl (decl);
15786 DECL_CONTEXT (decl) = current_function_decl;
15787 cp_check_omp_declare_reduction (decl);
15789 else
15791 int const_init = false;
15792 maybe_push_decl (decl);
15793 if (VAR_P (decl)
15794 && DECL_PRETTY_FUNCTION_P (decl))
15796 /* For __PRETTY_FUNCTION__ we have to adjust the
15797 initializer. */
15798 const char *const name
15799 = cxx_printable_name (current_function_decl, 2);
15800 init = cp_fname_init (name, &TREE_TYPE (decl));
15802 else
15803 init = tsubst_init (init, decl, args, complain, in_decl);
15805 if (VAR_P (decl))
15806 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15807 (pattern_decl));
15808 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15809 if (VAR_P (decl)
15810 && DECL_DECOMPOSITION_P (decl)
15811 && TREE_TYPE (pattern_decl) != error_mark_node)
15813 unsigned int cnt;
15814 tree first;
15815 decl = tsubst_decomp_names (decl, pattern_decl, args,
15816 complain, in_decl, &first,
15817 &cnt);
15818 if (decl != error_mark_node)
15819 cp_finish_decomp (decl, first, cnt);
15825 break;
15828 case FOR_STMT:
15829 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15830 RECUR (FOR_INIT_STMT (t));
15831 finish_init_stmt (stmt);
15832 tmp = RECUR (FOR_COND (t));
15833 finish_for_cond (tmp, stmt, false);
15834 tmp = RECUR (FOR_EXPR (t));
15835 finish_for_expr (tmp, stmt);
15836 RECUR (FOR_BODY (t));
15837 finish_for_stmt (stmt);
15838 break;
15840 case RANGE_FOR_STMT:
15842 tree decl, expr;
15843 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15844 decl = RANGE_FOR_DECL (t);
15845 decl = tsubst (decl, args, complain, in_decl);
15846 maybe_push_decl (decl);
15847 expr = RECUR (RANGE_FOR_EXPR (t));
15848 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15850 unsigned int cnt;
15851 tree first;
15852 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15853 complain, in_decl, &first, &cnt);
15854 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15855 RANGE_FOR_IVDEP (t));
15857 else
15858 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15859 RANGE_FOR_IVDEP (t));
15860 RECUR (RANGE_FOR_BODY (t));
15861 finish_for_stmt (stmt);
15863 break;
15865 case WHILE_STMT:
15866 stmt = begin_while_stmt ();
15867 tmp = RECUR (WHILE_COND (t));
15868 finish_while_stmt_cond (tmp, stmt, false);
15869 RECUR (WHILE_BODY (t));
15870 finish_while_stmt (stmt);
15871 break;
15873 case DO_STMT:
15874 stmt = begin_do_stmt ();
15875 RECUR (DO_BODY (t));
15876 finish_do_body (stmt);
15877 tmp = RECUR (DO_COND (t));
15878 finish_do_stmt (tmp, stmt, false);
15879 break;
15881 case IF_STMT:
15882 stmt = begin_if_stmt ();
15883 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15884 tmp = RECUR (IF_COND (t));
15885 tmp = finish_if_stmt_cond (tmp, stmt);
15886 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15887 /* Don't instantiate the THEN_CLAUSE. */;
15888 else
15890 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15891 if (inhibit)
15892 ++c_inhibit_evaluation_warnings;
15893 RECUR (THEN_CLAUSE (t));
15894 if (inhibit)
15895 --c_inhibit_evaluation_warnings;
15897 finish_then_clause (stmt);
15899 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15900 /* Don't instantiate the ELSE_CLAUSE. */;
15901 else if (ELSE_CLAUSE (t))
15903 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15904 begin_else_clause (stmt);
15905 if (inhibit)
15906 ++c_inhibit_evaluation_warnings;
15907 RECUR (ELSE_CLAUSE (t));
15908 if (inhibit)
15909 --c_inhibit_evaluation_warnings;
15910 finish_else_clause (stmt);
15913 finish_if_stmt (stmt);
15914 break;
15916 case BIND_EXPR:
15917 if (BIND_EXPR_BODY_BLOCK (t))
15918 stmt = begin_function_body ();
15919 else
15920 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15921 ? BCS_TRY_BLOCK : 0);
15923 RECUR (BIND_EXPR_BODY (t));
15925 if (BIND_EXPR_BODY_BLOCK (t))
15926 finish_function_body (stmt);
15927 else
15928 finish_compound_stmt (stmt);
15929 break;
15931 case BREAK_STMT:
15932 finish_break_stmt ();
15933 break;
15935 case CONTINUE_STMT:
15936 finish_continue_stmt ();
15937 break;
15939 case SWITCH_STMT:
15940 stmt = begin_switch_stmt ();
15941 tmp = RECUR (SWITCH_STMT_COND (t));
15942 finish_switch_cond (tmp, stmt);
15943 RECUR (SWITCH_STMT_BODY (t));
15944 finish_switch_stmt (stmt);
15945 break;
15947 case CASE_LABEL_EXPR:
15949 tree low = RECUR (CASE_LOW (t));
15950 tree high = RECUR (CASE_HIGH (t));
15951 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
15952 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
15953 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
15954 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
15956 break;
15958 case LABEL_EXPR:
15960 tree decl = LABEL_EXPR_LABEL (t);
15961 tree label;
15963 label = finish_label_stmt (DECL_NAME (decl));
15964 if (TREE_CODE (label) == LABEL_DECL)
15965 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
15966 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15967 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15969 break;
15971 case GOTO_EXPR:
15972 tmp = GOTO_DESTINATION (t);
15973 if (TREE_CODE (tmp) != LABEL_DECL)
15974 /* Computed goto's must be tsubst'd into. On the other hand,
15975 non-computed gotos must not be; the identifier in question
15976 will have no binding. */
15977 tmp = RECUR (tmp);
15978 else
15979 tmp = DECL_NAME (tmp);
15980 finish_goto_stmt (tmp);
15981 break;
15983 case ASM_EXPR:
15985 tree string = RECUR (ASM_STRING (t));
15986 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15987 complain, in_decl);
15988 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15989 complain, in_decl);
15990 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15991 complain, in_decl);
15992 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15993 complain, in_decl);
15994 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15995 clobbers, labels);
15996 tree asm_expr = tmp;
15997 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15998 asm_expr = TREE_OPERAND (asm_expr, 0);
15999 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16001 break;
16003 case TRY_BLOCK:
16004 if (CLEANUP_P (t))
16006 stmt = begin_try_block ();
16007 RECUR (TRY_STMTS (t));
16008 finish_cleanup_try_block (stmt);
16009 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16011 else
16013 tree compound_stmt = NULL_TREE;
16015 if (FN_TRY_BLOCK_P (t))
16016 stmt = begin_function_try_block (&compound_stmt);
16017 else
16018 stmt = begin_try_block ();
16020 RECUR (TRY_STMTS (t));
16022 if (FN_TRY_BLOCK_P (t))
16023 finish_function_try_block (stmt);
16024 else
16025 finish_try_block (stmt);
16027 RECUR (TRY_HANDLERS (t));
16028 if (FN_TRY_BLOCK_P (t))
16029 finish_function_handler_sequence (stmt, compound_stmt);
16030 else
16031 finish_handler_sequence (stmt);
16033 break;
16035 case HANDLER:
16037 tree decl = HANDLER_PARMS (t);
16039 if (decl)
16041 decl = tsubst (decl, args, complain, in_decl);
16042 /* Prevent instantiate_decl from trying to instantiate
16043 this variable. We've already done all that needs to be
16044 done. */
16045 if (decl != error_mark_node)
16046 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16048 stmt = begin_handler ();
16049 finish_handler_parms (decl, stmt);
16050 RECUR (HANDLER_BODY (t));
16051 finish_handler (stmt);
16053 break;
16055 case TAG_DEFN:
16056 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16057 if (CLASS_TYPE_P (tmp))
16059 /* Local classes are not independent templates; they are
16060 instantiated along with their containing function. And this
16061 way we don't have to deal with pushing out of one local class
16062 to instantiate a member of another local class. */
16063 tree fn;
16064 /* Closures are handled by the LAMBDA_EXPR. */
16065 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16066 complete_type (tmp);
16067 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16068 if (!DECL_ARTIFICIAL (fn))
16069 instantiate_decl (fn, /*defer_ok=*/false,
16070 /*expl_inst_class=*/false);
16072 break;
16074 case STATIC_ASSERT:
16076 tree condition;
16078 ++c_inhibit_evaluation_warnings;
16079 condition =
16080 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16081 args,
16082 complain, in_decl,
16083 /*integral_constant_expression_p=*/true);
16084 --c_inhibit_evaluation_warnings;
16086 finish_static_assert (condition,
16087 STATIC_ASSERT_MESSAGE (t),
16088 STATIC_ASSERT_SOURCE_LOCATION (t),
16089 /*member_p=*/false);
16091 break;
16093 case OACC_KERNELS:
16094 case OACC_PARALLEL:
16095 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16096 in_decl);
16097 stmt = begin_omp_parallel ();
16098 RECUR (OMP_BODY (t));
16099 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16100 break;
16102 case OMP_PARALLEL:
16103 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16104 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16105 complain, in_decl);
16106 if (OMP_PARALLEL_COMBINED (t))
16107 omp_parallel_combined_clauses = &tmp;
16108 stmt = begin_omp_parallel ();
16109 RECUR (OMP_PARALLEL_BODY (t));
16110 gcc_assert (omp_parallel_combined_clauses == NULL);
16111 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16112 = OMP_PARALLEL_COMBINED (t);
16113 pop_omp_privatization_clauses (r);
16114 break;
16116 case OMP_TASK:
16117 r = push_omp_privatization_clauses (false);
16118 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16119 complain, in_decl);
16120 stmt = begin_omp_task ();
16121 RECUR (OMP_TASK_BODY (t));
16122 finish_omp_task (tmp, stmt);
16123 pop_omp_privatization_clauses (r);
16124 break;
16126 case OMP_FOR:
16127 case OMP_SIMD:
16128 case CILK_SIMD:
16129 case CILK_FOR:
16130 case OMP_DISTRIBUTE:
16131 case OMP_TASKLOOP:
16132 case OACC_LOOP:
16134 tree clauses, body, pre_body;
16135 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16136 tree orig_declv = NULL_TREE;
16137 tree incrv = NULL_TREE;
16138 enum c_omp_region_type ort = C_ORT_OMP;
16139 int i;
16141 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16142 ort = C_ORT_CILK;
16143 else if (TREE_CODE (t) == OACC_LOOP)
16144 ort = C_ORT_ACC;
16146 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16147 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16148 in_decl);
16149 if (OMP_FOR_INIT (t) != NULL_TREE)
16151 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16152 if (OMP_FOR_ORIG_DECLS (t))
16153 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16154 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16155 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16156 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16159 stmt = begin_omp_structured_block ();
16161 pre_body = push_stmt_list ();
16162 RECUR (OMP_FOR_PRE_BODY (t));
16163 pre_body = pop_stmt_list (pre_body);
16165 if (OMP_FOR_INIT (t) != NULL_TREE)
16166 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16167 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16168 incrv, &clauses, args, complain, in_decl,
16169 integral_constant_expression_p);
16170 omp_parallel_combined_clauses = NULL;
16172 body = push_stmt_list ();
16173 RECUR (OMP_FOR_BODY (t));
16174 body = pop_stmt_list (body);
16176 if (OMP_FOR_INIT (t) != NULL_TREE)
16177 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16178 orig_declv, initv, condv, incrv, body, pre_body,
16179 NULL, clauses);
16180 else
16182 t = make_node (TREE_CODE (t));
16183 TREE_TYPE (t) = void_type_node;
16184 OMP_FOR_BODY (t) = body;
16185 OMP_FOR_PRE_BODY (t) = pre_body;
16186 OMP_FOR_CLAUSES (t) = clauses;
16187 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16188 add_stmt (t);
16191 add_stmt (finish_omp_structured_block (stmt));
16192 pop_omp_privatization_clauses (r);
16194 break;
16196 case OMP_SECTIONS:
16197 omp_parallel_combined_clauses = NULL;
16198 /* FALLTHRU */
16199 case OMP_SINGLE:
16200 case OMP_TEAMS:
16201 case OMP_CRITICAL:
16202 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16203 && OMP_TEAMS_COMBINED (t));
16204 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16205 in_decl);
16206 stmt = push_stmt_list ();
16207 RECUR (OMP_BODY (t));
16208 stmt = pop_stmt_list (stmt);
16210 t = copy_node (t);
16211 OMP_BODY (t) = stmt;
16212 OMP_CLAUSES (t) = tmp;
16213 add_stmt (t);
16214 pop_omp_privatization_clauses (r);
16215 break;
16217 case OACC_DATA:
16218 case OMP_TARGET_DATA:
16219 case OMP_TARGET:
16220 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16221 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16222 in_decl);
16223 keep_next_level (true);
16224 stmt = begin_omp_structured_block ();
16226 RECUR (OMP_BODY (t));
16227 stmt = finish_omp_structured_block (stmt);
16229 t = copy_node (t);
16230 OMP_BODY (t) = stmt;
16231 OMP_CLAUSES (t) = tmp;
16232 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16234 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16235 if (teams)
16237 /* For combined target teams, ensure the num_teams and
16238 thread_limit clause expressions are evaluated on the host,
16239 before entering the target construct. */
16240 tree c;
16241 for (c = OMP_TEAMS_CLAUSES (teams);
16242 c; c = OMP_CLAUSE_CHAIN (c))
16243 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16244 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16245 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16247 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16248 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16249 if (expr == error_mark_node)
16250 continue;
16251 tmp = TARGET_EXPR_SLOT (expr);
16252 add_stmt (expr);
16253 OMP_CLAUSE_OPERAND (c, 0) = expr;
16254 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16255 OMP_CLAUSE_FIRSTPRIVATE);
16256 OMP_CLAUSE_DECL (tc) = tmp;
16257 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16258 OMP_TARGET_CLAUSES (t) = tc;
16262 add_stmt (t);
16263 break;
16265 case OACC_DECLARE:
16266 t = copy_node (t);
16267 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16268 complain, in_decl);
16269 OACC_DECLARE_CLAUSES (t) = tmp;
16270 add_stmt (t);
16271 break;
16273 case OMP_TARGET_UPDATE:
16274 case OMP_TARGET_ENTER_DATA:
16275 case OMP_TARGET_EXIT_DATA:
16276 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16277 complain, in_decl);
16278 t = copy_node (t);
16279 OMP_STANDALONE_CLAUSES (t) = tmp;
16280 add_stmt (t);
16281 break;
16283 case OACC_ENTER_DATA:
16284 case OACC_EXIT_DATA:
16285 case OACC_UPDATE:
16286 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16287 complain, in_decl);
16288 t = copy_node (t);
16289 OMP_STANDALONE_CLAUSES (t) = tmp;
16290 add_stmt (t);
16291 break;
16293 case OMP_ORDERED:
16294 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16295 complain, in_decl);
16296 stmt = push_stmt_list ();
16297 RECUR (OMP_BODY (t));
16298 stmt = pop_stmt_list (stmt);
16300 t = copy_node (t);
16301 OMP_BODY (t) = stmt;
16302 OMP_ORDERED_CLAUSES (t) = tmp;
16303 add_stmt (t);
16304 break;
16306 case OMP_SECTION:
16307 case OMP_MASTER:
16308 case OMP_TASKGROUP:
16309 stmt = push_stmt_list ();
16310 RECUR (OMP_BODY (t));
16311 stmt = pop_stmt_list (stmt);
16313 t = copy_node (t);
16314 OMP_BODY (t) = stmt;
16315 add_stmt (t);
16316 break;
16318 case OMP_ATOMIC:
16319 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16320 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16322 tree op1 = TREE_OPERAND (t, 1);
16323 tree rhs1 = NULL_TREE;
16324 tree lhs, rhs;
16325 if (TREE_CODE (op1) == COMPOUND_EXPR)
16327 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16328 op1 = TREE_OPERAND (op1, 1);
16330 lhs = RECUR (TREE_OPERAND (op1, 0));
16331 rhs = RECUR (TREE_OPERAND (op1, 1));
16332 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16333 NULL_TREE, NULL_TREE, rhs1,
16334 OMP_ATOMIC_SEQ_CST (t));
16336 else
16338 tree op1 = TREE_OPERAND (t, 1);
16339 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16340 tree rhs1 = NULL_TREE;
16341 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16342 enum tree_code opcode = NOP_EXPR;
16343 if (code == OMP_ATOMIC_READ)
16345 v = RECUR (TREE_OPERAND (op1, 0));
16346 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16348 else if (code == OMP_ATOMIC_CAPTURE_OLD
16349 || code == OMP_ATOMIC_CAPTURE_NEW)
16351 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16352 v = RECUR (TREE_OPERAND (op1, 0));
16353 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16354 if (TREE_CODE (op11) == COMPOUND_EXPR)
16356 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16357 op11 = TREE_OPERAND (op11, 1);
16359 lhs = RECUR (TREE_OPERAND (op11, 0));
16360 rhs = RECUR (TREE_OPERAND (op11, 1));
16361 opcode = TREE_CODE (op11);
16362 if (opcode == MODIFY_EXPR)
16363 opcode = NOP_EXPR;
16365 else
16367 code = OMP_ATOMIC;
16368 lhs = RECUR (TREE_OPERAND (op1, 0));
16369 rhs = RECUR (TREE_OPERAND (op1, 1));
16371 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16372 OMP_ATOMIC_SEQ_CST (t));
16374 break;
16376 case TRANSACTION_EXPR:
16378 int flags = 0;
16379 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16380 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16382 if (TRANSACTION_EXPR_IS_STMT (t))
16384 tree body = TRANSACTION_EXPR_BODY (t);
16385 tree noex = NULL_TREE;
16386 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16388 noex = MUST_NOT_THROW_COND (body);
16389 if (noex == NULL_TREE)
16390 noex = boolean_true_node;
16391 body = TREE_OPERAND (body, 0);
16393 stmt = begin_transaction_stmt (input_location, NULL, flags);
16394 RECUR (body);
16395 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16397 else
16399 stmt = build_transaction_expr (EXPR_LOCATION (t),
16400 RECUR (TRANSACTION_EXPR_BODY (t)),
16401 flags, NULL_TREE);
16402 RETURN (stmt);
16405 break;
16407 case MUST_NOT_THROW_EXPR:
16409 tree op0 = RECUR (TREE_OPERAND (t, 0));
16410 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16411 RETURN (build_must_not_throw_expr (op0, cond));
16414 case EXPR_PACK_EXPANSION:
16415 error ("invalid use of pack expansion expression");
16416 RETURN (error_mark_node);
16418 case NONTYPE_ARGUMENT_PACK:
16419 error ("use %<...%> to expand argument pack");
16420 RETURN (error_mark_node);
16422 case CILK_SPAWN_STMT:
16423 cfun->calls_cilk_spawn = 1;
16424 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16426 case CILK_SYNC_STMT:
16427 RETURN (build_cilk_sync ());
16429 case COMPOUND_EXPR:
16430 tmp = RECUR (TREE_OPERAND (t, 0));
16431 if (tmp == NULL_TREE)
16432 /* If the first operand was a statement, we're done with it. */
16433 RETURN (RECUR (TREE_OPERAND (t, 1)));
16434 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16435 RECUR (TREE_OPERAND (t, 1)),
16436 complain));
16438 case ANNOTATE_EXPR:
16439 tmp = RECUR (TREE_OPERAND (t, 0));
16440 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16441 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16443 default:
16444 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16446 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16447 /*function_p=*/false,
16448 integral_constant_expression_p));
16451 RETURN (NULL_TREE);
16452 out:
16453 input_location = loc;
16454 return r;
16455 #undef RECUR
16456 #undef RETURN
16459 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16460 function. For description of the body see comment above
16461 cp_parser_omp_declare_reduction_exprs. */
16463 static void
16464 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16466 if (t == NULL_TREE || t == error_mark_node)
16467 return;
16469 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16471 tree_stmt_iterator tsi;
16472 int i;
16473 tree stmts[7];
16474 memset (stmts, 0, sizeof stmts);
16475 for (i = 0, tsi = tsi_start (t);
16476 i < 7 && !tsi_end_p (tsi);
16477 i++, tsi_next (&tsi))
16478 stmts[i] = tsi_stmt (tsi);
16479 gcc_assert (tsi_end_p (tsi));
16481 if (i >= 3)
16483 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16484 && TREE_CODE (stmts[1]) == DECL_EXPR);
16485 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16486 args, complain, in_decl);
16487 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16488 args, complain, in_decl);
16489 DECL_CONTEXT (omp_out) = current_function_decl;
16490 DECL_CONTEXT (omp_in) = current_function_decl;
16491 keep_next_level (true);
16492 tree block = begin_omp_structured_block ();
16493 tsubst_expr (stmts[2], args, complain, in_decl, false);
16494 block = finish_omp_structured_block (block);
16495 block = maybe_cleanup_point_expr_void (block);
16496 add_decl_expr (omp_out);
16497 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16498 TREE_NO_WARNING (omp_out) = 1;
16499 add_decl_expr (omp_in);
16500 finish_expr_stmt (block);
16502 if (i >= 6)
16504 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16505 && TREE_CODE (stmts[4]) == DECL_EXPR);
16506 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16507 args, complain, in_decl);
16508 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16509 args, complain, in_decl);
16510 DECL_CONTEXT (omp_priv) = current_function_decl;
16511 DECL_CONTEXT (omp_orig) = current_function_decl;
16512 keep_next_level (true);
16513 tree block = begin_omp_structured_block ();
16514 tsubst_expr (stmts[5], args, complain, in_decl, false);
16515 block = finish_omp_structured_block (block);
16516 block = maybe_cleanup_point_expr_void (block);
16517 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16518 add_decl_expr (omp_priv);
16519 add_decl_expr (omp_orig);
16520 finish_expr_stmt (block);
16521 if (i == 7)
16522 add_decl_expr (omp_orig);
16526 /* T is a postfix-expression that is not being used in a function
16527 call. Return the substituted version of T. */
16529 static tree
16530 tsubst_non_call_postfix_expression (tree t, tree args,
16531 tsubst_flags_t complain,
16532 tree in_decl)
16534 if (TREE_CODE (t) == SCOPE_REF)
16535 t = tsubst_qualified_id (t, args, complain, in_decl,
16536 /*done=*/false, /*address_p=*/false);
16537 else
16538 t = tsubst_copy_and_build (t, args, complain, in_decl,
16539 /*function_p=*/false,
16540 /*integral_constant_expression_p=*/false);
16542 return t;
16545 /* Like tsubst but deals with expressions and performs semantic
16546 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16548 tree
16549 tsubst_copy_and_build (tree t,
16550 tree args,
16551 tsubst_flags_t complain,
16552 tree in_decl,
16553 bool function_p,
16554 bool integral_constant_expression_p)
16556 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16557 #define RECUR(NODE) \
16558 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16559 /*function_p=*/false, \
16560 integral_constant_expression_p)
16562 tree retval, op1;
16563 location_t loc;
16565 if (t == NULL_TREE || t == error_mark_node)
16566 return t;
16568 loc = input_location;
16569 if (EXPR_HAS_LOCATION (t))
16570 input_location = EXPR_LOCATION (t);
16572 /* N3276 decltype magic only applies to calls at the top level or on the
16573 right side of a comma. */
16574 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16575 complain &= ~tf_decltype;
16577 switch (TREE_CODE (t))
16579 case USING_DECL:
16580 t = DECL_NAME (t);
16581 /* Fall through. */
16582 case IDENTIFIER_NODE:
16584 tree decl;
16585 cp_id_kind idk;
16586 bool non_integral_constant_expression_p;
16587 const char *error_msg;
16589 if (IDENTIFIER_TYPENAME_P (t))
16591 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16592 t = mangle_conv_op_name_for_type (new_type);
16595 /* Look up the name. */
16596 decl = lookup_name (t);
16598 /* By convention, expressions use ERROR_MARK_NODE to indicate
16599 failure, not NULL_TREE. */
16600 if (decl == NULL_TREE)
16601 decl = error_mark_node;
16603 decl = finish_id_expression (t, decl, NULL_TREE,
16604 &idk,
16605 integral_constant_expression_p,
16606 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16607 &non_integral_constant_expression_p,
16608 /*template_p=*/false,
16609 /*done=*/true,
16610 /*address_p=*/false,
16611 /*template_arg_p=*/false,
16612 &error_msg,
16613 input_location);
16614 if (error_msg)
16615 error (error_msg);
16616 if (!function_p && identifier_p (decl))
16618 if (complain & tf_error)
16619 unqualified_name_lookup_error (decl);
16620 decl = error_mark_node;
16622 RETURN (decl);
16625 case TEMPLATE_ID_EXPR:
16627 tree object;
16628 tree templ = RECUR (TREE_OPERAND (t, 0));
16629 tree targs = TREE_OPERAND (t, 1);
16631 if (targs)
16632 targs = tsubst_template_args (targs, args, complain, in_decl);
16633 if (targs == error_mark_node)
16634 return error_mark_node;
16636 if (TREE_CODE (templ) == SCOPE_REF)
16638 tree name = TREE_OPERAND (templ, 1);
16639 tree tid = lookup_template_function (name, targs);
16640 TREE_OPERAND (templ, 1) = tid;
16641 return templ;
16644 if (variable_template_p (templ))
16645 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16647 if (TREE_CODE (templ) == COMPONENT_REF)
16649 object = TREE_OPERAND (templ, 0);
16650 templ = TREE_OPERAND (templ, 1);
16652 else
16653 object = NULL_TREE;
16654 templ = lookup_template_function (templ, targs);
16656 if (object)
16657 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16658 object, templ, NULL_TREE));
16659 else
16660 RETURN (baselink_for_fns (templ));
16663 case INDIRECT_REF:
16665 tree r = RECUR (TREE_OPERAND (t, 0));
16667 if (REFERENCE_REF_P (t))
16669 /* A type conversion to reference type will be enclosed in
16670 such an indirect ref, but the substitution of the cast
16671 will have also added such an indirect ref. */
16672 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16673 r = convert_from_reference (r);
16675 else
16676 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16677 complain|decltype_flag);
16679 if (TREE_CODE (r) == INDIRECT_REF)
16680 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16682 RETURN (r);
16685 case NOP_EXPR:
16687 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16688 tree op0 = RECUR (TREE_OPERAND (t, 0));
16689 RETURN (build_nop (type, op0));
16692 case IMPLICIT_CONV_EXPR:
16694 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16695 tree expr = RECUR (TREE_OPERAND (t, 0));
16696 int flags = LOOKUP_IMPLICIT;
16697 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16698 flags = LOOKUP_NORMAL;
16699 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16700 flags));
16703 case CONVERT_EXPR:
16705 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16706 tree op0 = RECUR (TREE_OPERAND (t, 0));
16707 RETURN (build1 (CONVERT_EXPR, type, op0));
16710 case CAST_EXPR:
16711 case REINTERPRET_CAST_EXPR:
16712 case CONST_CAST_EXPR:
16713 case DYNAMIC_CAST_EXPR:
16714 case STATIC_CAST_EXPR:
16716 tree type;
16717 tree op, r = NULL_TREE;
16719 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16720 if (integral_constant_expression_p
16721 && !cast_valid_in_integral_constant_expression_p (type))
16723 if (complain & tf_error)
16724 error ("a cast to a type other than an integral or "
16725 "enumeration type cannot appear in a constant-expression");
16726 RETURN (error_mark_node);
16729 op = RECUR (TREE_OPERAND (t, 0));
16731 warning_sentinel s(warn_useless_cast);
16732 switch (TREE_CODE (t))
16734 case CAST_EXPR:
16735 r = build_functional_cast (type, op, complain);
16736 break;
16737 case REINTERPRET_CAST_EXPR:
16738 r = build_reinterpret_cast (type, op, complain);
16739 break;
16740 case CONST_CAST_EXPR:
16741 r = build_const_cast (type, op, complain);
16742 break;
16743 case DYNAMIC_CAST_EXPR:
16744 r = build_dynamic_cast (type, op, complain);
16745 break;
16746 case STATIC_CAST_EXPR:
16747 r = build_static_cast (type, op, complain);
16748 break;
16749 default:
16750 gcc_unreachable ();
16753 RETURN (r);
16756 case POSTDECREMENT_EXPR:
16757 case POSTINCREMENT_EXPR:
16758 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16759 args, complain, in_decl);
16760 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16761 complain|decltype_flag));
16763 case PREDECREMENT_EXPR:
16764 case PREINCREMENT_EXPR:
16765 case NEGATE_EXPR:
16766 case BIT_NOT_EXPR:
16767 case ABS_EXPR:
16768 case TRUTH_NOT_EXPR:
16769 case UNARY_PLUS_EXPR: /* Unary + */
16770 case REALPART_EXPR:
16771 case IMAGPART_EXPR:
16772 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16773 RECUR (TREE_OPERAND (t, 0)),
16774 complain|decltype_flag));
16776 case FIX_TRUNC_EXPR:
16777 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16778 false, complain));
16780 case ADDR_EXPR:
16781 op1 = TREE_OPERAND (t, 0);
16782 if (TREE_CODE (op1) == LABEL_DECL)
16783 RETURN (finish_label_address_expr (DECL_NAME (op1),
16784 EXPR_LOCATION (op1)));
16785 if (TREE_CODE (op1) == SCOPE_REF)
16786 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16787 /*done=*/true, /*address_p=*/true);
16788 else
16789 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16790 in_decl);
16791 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16792 complain|decltype_flag));
16794 case PLUS_EXPR:
16795 case MINUS_EXPR:
16796 case MULT_EXPR:
16797 case TRUNC_DIV_EXPR:
16798 case CEIL_DIV_EXPR:
16799 case FLOOR_DIV_EXPR:
16800 case ROUND_DIV_EXPR:
16801 case EXACT_DIV_EXPR:
16802 case BIT_AND_EXPR:
16803 case BIT_IOR_EXPR:
16804 case BIT_XOR_EXPR:
16805 case TRUNC_MOD_EXPR:
16806 case FLOOR_MOD_EXPR:
16807 case TRUTH_ANDIF_EXPR:
16808 case TRUTH_ORIF_EXPR:
16809 case TRUTH_AND_EXPR:
16810 case TRUTH_OR_EXPR:
16811 case RSHIFT_EXPR:
16812 case LSHIFT_EXPR:
16813 case RROTATE_EXPR:
16814 case LROTATE_EXPR:
16815 case EQ_EXPR:
16816 case NE_EXPR:
16817 case MAX_EXPR:
16818 case MIN_EXPR:
16819 case LE_EXPR:
16820 case GE_EXPR:
16821 case LT_EXPR:
16822 case GT_EXPR:
16823 case MEMBER_REF:
16824 case DOTSTAR_EXPR:
16826 warning_sentinel s1(warn_type_limits);
16827 warning_sentinel s2(warn_div_by_zero);
16828 warning_sentinel s3(warn_logical_op);
16829 warning_sentinel s4(warn_tautological_compare);
16830 tree op0 = RECUR (TREE_OPERAND (t, 0));
16831 tree op1 = RECUR (TREE_OPERAND (t, 1));
16832 tree r = build_x_binary_op
16833 (input_location, TREE_CODE (t),
16834 op0,
16835 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16836 ? ERROR_MARK
16837 : TREE_CODE (TREE_OPERAND (t, 0))),
16838 op1,
16839 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16840 ? ERROR_MARK
16841 : TREE_CODE (TREE_OPERAND (t, 1))),
16842 /*overload=*/NULL,
16843 complain|decltype_flag);
16844 if (EXPR_P (r) && TREE_NO_WARNING (t))
16845 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16847 RETURN (r);
16850 case POINTER_PLUS_EXPR:
16852 tree op0 = RECUR (TREE_OPERAND (t, 0));
16853 tree op1 = RECUR (TREE_OPERAND (t, 1));
16854 return fold_build_pointer_plus (op0, op1);
16857 case SCOPE_REF:
16858 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16859 /*address_p=*/false));
16860 case ARRAY_REF:
16861 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16862 args, complain, in_decl);
16863 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16864 RECUR (TREE_OPERAND (t, 1)),
16865 complain|decltype_flag));
16867 case ARRAY_NOTATION_REF:
16869 tree start_index, length, stride;
16870 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16871 args, complain, in_decl);
16872 start_index = RECUR (ARRAY_NOTATION_START (t));
16873 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16874 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16875 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16876 length, stride, TREE_TYPE (op1)));
16878 case SIZEOF_EXPR:
16879 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16880 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16881 RETURN (tsubst_copy (t, args, complain, in_decl));
16882 /* Fall through */
16884 case ALIGNOF_EXPR:
16886 tree r;
16888 op1 = TREE_OPERAND (t, 0);
16889 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16890 op1 = TREE_TYPE (op1);
16891 if (!args)
16893 /* When there are no ARGS, we are trying to evaluate a
16894 non-dependent expression from the parser. Trying to do
16895 the substitutions may not work. */
16896 if (!TYPE_P (op1))
16897 op1 = TREE_TYPE (op1);
16899 else
16901 ++cp_unevaluated_operand;
16902 ++c_inhibit_evaluation_warnings;
16903 if (TYPE_P (op1))
16904 op1 = tsubst (op1, args, complain, in_decl);
16905 else
16906 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16907 /*function_p=*/false,
16908 /*integral_constant_expression_p=*/
16909 false);
16910 --cp_unevaluated_operand;
16911 --c_inhibit_evaluation_warnings;
16913 if (TYPE_P (op1))
16914 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16915 complain & tf_error);
16916 else
16917 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16918 complain & tf_error);
16919 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16921 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16923 if (!processing_template_decl && TYPE_P (op1))
16925 r = build_min (SIZEOF_EXPR, size_type_node,
16926 build1 (NOP_EXPR, op1, error_mark_node));
16927 SIZEOF_EXPR_TYPE_P (r) = 1;
16929 else
16930 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16931 TREE_SIDE_EFFECTS (r) = 0;
16932 TREE_READONLY (r) = 1;
16934 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16936 RETURN (r);
16939 case AT_ENCODE_EXPR:
16941 op1 = TREE_OPERAND (t, 0);
16942 ++cp_unevaluated_operand;
16943 ++c_inhibit_evaluation_warnings;
16944 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16945 /*function_p=*/false,
16946 /*integral_constant_expression_p=*/false);
16947 --cp_unevaluated_operand;
16948 --c_inhibit_evaluation_warnings;
16949 RETURN (objc_build_encode_expr (op1));
16952 case NOEXCEPT_EXPR:
16953 op1 = TREE_OPERAND (t, 0);
16954 ++cp_unevaluated_operand;
16955 ++c_inhibit_evaluation_warnings;
16956 ++cp_noexcept_operand;
16957 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16958 /*function_p=*/false,
16959 /*integral_constant_expression_p=*/false);
16960 --cp_unevaluated_operand;
16961 --c_inhibit_evaluation_warnings;
16962 --cp_noexcept_operand;
16963 RETURN (finish_noexcept_expr (op1, complain));
16965 case MODOP_EXPR:
16967 warning_sentinel s(warn_div_by_zero);
16968 tree lhs = RECUR (TREE_OPERAND (t, 0));
16969 tree rhs = RECUR (TREE_OPERAND (t, 2));
16970 tree r = build_x_modify_expr
16971 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16972 complain|decltype_flag);
16973 /* TREE_NO_WARNING must be set if either the expression was
16974 parenthesized or it uses an operator such as >>= rather
16975 than plain assignment. In the former case, it was already
16976 set and must be copied. In the latter case,
16977 build_x_modify_expr sets it and it must not be reset
16978 here. */
16979 if (TREE_NO_WARNING (t))
16980 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16982 RETURN (r);
16985 case ARROW_EXPR:
16986 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16987 args, complain, in_decl);
16988 /* Remember that there was a reference to this entity. */
16989 if (DECL_P (op1)
16990 && !mark_used (op1, complain) && !(complain & tf_error))
16991 RETURN (error_mark_node);
16992 RETURN (build_x_arrow (input_location, op1, complain));
16994 case NEW_EXPR:
16996 tree placement = RECUR (TREE_OPERAND (t, 0));
16997 tree init = RECUR (TREE_OPERAND (t, 3));
16998 vec<tree, va_gc> *placement_vec;
16999 vec<tree, va_gc> *init_vec;
17000 tree ret;
17002 if (placement == NULL_TREE)
17003 placement_vec = NULL;
17004 else
17006 placement_vec = make_tree_vector ();
17007 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17008 vec_safe_push (placement_vec, TREE_VALUE (placement));
17011 /* If there was an initializer in the original tree, but it
17012 instantiated to an empty list, then we should pass a
17013 non-NULL empty vector to tell build_new that it was an
17014 empty initializer() rather than no initializer. This can
17015 only happen when the initializer is a pack expansion whose
17016 parameter packs are of length zero. */
17017 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17018 init_vec = NULL;
17019 else
17021 init_vec = make_tree_vector ();
17022 if (init == void_node)
17023 gcc_assert (init_vec != NULL);
17024 else
17026 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17027 vec_safe_push (init_vec, TREE_VALUE (init));
17031 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17032 tree op2 = RECUR (TREE_OPERAND (t, 2));
17033 ret = build_new (&placement_vec, op1, op2, &init_vec,
17034 NEW_EXPR_USE_GLOBAL (t),
17035 complain);
17037 if (placement_vec != NULL)
17038 release_tree_vector (placement_vec);
17039 if (init_vec != NULL)
17040 release_tree_vector (init_vec);
17042 RETURN (ret);
17045 case DELETE_EXPR:
17047 tree op0 = RECUR (TREE_OPERAND (t, 0));
17048 tree op1 = RECUR (TREE_OPERAND (t, 1));
17049 RETURN (delete_sanity (op0, op1,
17050 DELETE_EXPR_USE_VEC (t),
17051 DELETE_EXPR_USE_GLOBAL (t),
17052 complain));
17055 case COMPOUND_EXPR:
17057 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17058 complain & ~tf_decltype, in_decl,
17059 /*function_p=*/false,
17060 integral_constant_expression_p);
17061 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17062 op0,
17063 RECUR (TREE_OPERAND (t, 1)),
17064 complain|decltype_flag));
17067 case CALL_EXPR:
17069 tree function;
17070 vec<tree, va_gc> *call_args;
17071 unsigned int nargs, i;
17072 bool qualified_p;
17073 bool koenig_p;
17074 tree ret;
17076 function = CALL_EXPR_FN (t);
17077 /* Internal function with no arguments. */
17078 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17079 RETURN (t);
17081 /* When we parsed the expression, we determined whether or
17082 not Koenig lookup should be performed. */
17083 koenig_p = KOENIG_LOOKUP_P (t);
17084 if (function == NULL_TREE)
17086 koenig_p = false;
17087 qualified_p = false;
17089 else if (TREE_CODE (function) == SCOPE_REF)
17091 qualified_p = true;
17092 function = tsubst_qualified_id (function, args, complain, in_decl,
17093 /*done=*/false,
17094 /*address_p=*/false);
17096 else if (koenig_p && identifier_p (function))
17098 /* Do nothing; calling tsubst_copy_and_build on an identifier
17099 would incorrectly perform unqualified lookup again.
17101 Note that we can also have an IDENTIFIER_NODE if the earlier
17102 unqualified lookup found a member function; in that case
17103 koenig_p will be false and we do want to do the lookup
17104 again to find the instantiated member function.
17106 FIXME but doing that causes c++/15272, so we need to stop
17107 using IDENTIFIER_NODE in that situation. */
17108 qualified_p = false;
17110 else
17112 if (TREE_CODE (function) == COMPONENT_REF)
17114 tree op = TREE_OPERAND (function, 1);
17116 qualified_p = (TREE_CODE (op) == SCOPE_REF
17117 || (BASELINK_P (op)
17118 && BASELINK_QUALIFIED_P (op)));
17120 else
17121 qualified_p = false;
17123 if (TREE_CODE (function) == ADDR_EXPR
17124 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17125 /* Avoid error about taking the address of a constructor. */
17126 function = TREE_OPERAND (function, 0);
17128 function = tsubst_copy_and_build (function, args, complain,
17129 in_decl,
17130 !qualified_p,
17131 integral_constant_expression_p);
17133 if (BASELINK_P (function))
17134 qualified_p = true;
17137 nargs = call_expr_nargs (t);
17138 call_args = make_tree_vector ();
17139 for (i = 0; i < nargs; ++i)
17141 tree arg = CALL_EXPR_ARG (t, i);
17143 if (!PACK_EXPANSION_P (arg))
17144 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17145 else
17147 /* Expand the pack expansion and push each entry onto
17148 CALL_ARGS. */
17149 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17150 if (TREE_CODE (arg) == TREE_VEC)
17152 unsigned int len, j;
17154 len = TREE_VEC_LENGTH (arg);
17155 for (j = 0; j < len; ++j)
17157 tree value = TREE_VEC_ELT (arg, j);
17158 if (value != NULL_TREE)
17159 value = convert_from_reference (value);
17160 vec_safe_push (call_args, value);
17163 else
17165 /* A partial substitution. Add one entry. */
17166 vec_safe_push (call_args, arg);
17171 /* We do not perform argument-dependent lookup if normal
17172 lookup finds a non-function, in accordance with the
17173 expected resolution of DR 218. */
17174 if (koenig_p
17175 && ((is_overloaded_fn (function)
17176 /* If lookup found a member function, the Koenig lookup is
17177 not appropriate, even if an unqualified-name was used
17178 to denote the function. */
17179 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17180 || identifier_p (function))
17181 /* Only do this when substitution turns a dependent call
17182 into a non-dependent call. */
17183 && type_dependent_expression_p_push (t)
17184 && !any_type_dependent_arguments_p (call_args))
17185 function = perform_koenig_lookup (function, call_args, tf_none);
17187 if (function != NULL_TREE
17188 && identifier_p (function)
17189 && !any_type_dependent_arguments_p (call_args))
17191 if (koenig_p && (complain & tf_warning_or_error))
17193 /* For backwards compatibility and good diagnostics, try
17194 the unqualified lookup again if we aren't in SFINAE
17195 context. */
17196 tree unq = (tsubst_copy_and_build
17197 (function, args, complain, in_decl, true,
17198 integral_constant_expression_p));
17199 if (unq == error_mark_node)
17201 release_tree_vector (call_args);
17202 RETURN (error_mark_node);
17205 if (unq != function)
17207 /* In a lambda fn, we have to be careful to not
17208 introduce new this captures. Legacy code can't
17209 be using lambdas anyway, so it's ok to be
17210 stricter. */
17211 bool in_lambda = (current_class_type
17212 && LAMBDA_TYPE_P (current_class_type));
17213 char const *const msg
17214 = G_("%qD was not declared in this scope, "
17215 "and no declarations were found by "
17216 "argument-dependent lookup at the point "
17217 "of instantiation");
17219 bool diag = true;
17220 if (in_lambda)
17221 error_at (EXPR_LOC_OR_LOC (t, input_location),
17222 msg, function);
17223 else
17224 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17225 msg, function);
17226 if (diag)
17228 tree fn = unq;
17229 if (INDIRECT_REF_P (fn))
17230 fn = TREE_OPERAND (fn, 0);
17231 if (TREE_CODE (fn) == COMPONENT_REF)
17232 fn = TREE_OPERAND (fn, 1);
17233 if (is_overloaded_fn (fn))
17234 fn = get_first_fn (fn);
17236 if (!DECL_P (fn))
17237 /* Can't say anything more. */;
17238 else if (DECL_CLASS_SCOPE_P (fn))
17240 location_t loc = EXPR_LOC_OR_LOC (t,
17241 input_location);
17242 inform (loc,
17243 "declarations in dependent base %qT are "
17244 "not found by unqualified lookup",
17245 DECL_CLASS_CONTEXT (fn));
17246 if (current_class_ptr)
17247 inform (loc,
17248 "use %<this->%D%> instead", function);
17249 else
17250 inform (loc,
17251 "use %<%T::%D%> instead",
17252 current_class_name, function);
17254 else
17255 inform (DECL_SOURCE_LOCATION (fn),
17256 "%qD declared here, later in the "
17257 "translation unit", fn);
17258 if (in_lambda)
17260 release_tree_vector (call_args);
17261 RETURN (error_mark_node);
17265 function = unq;
17268 if (identifier_p (function))
17270 if (complain & tf_error)
17271 unqualified_name_lookup_error (function);
17272 release_tree_vector (call_args);
17273 RETURN (error_mark_node);
17277 /* Remember that there was a reference to this entity. */
17278 if (function != NULL_TREE
17279 && DECL_P (function)
17280 && !mark_used (function, complain) && !(complain & tf_error))
17282 release_tree_vector (call_args);
17283 RETURN (error_mark_node);
17286 /* Put back tf_decltype for the actual call. */
17287 complain |= decltype_flag;
17289 if (function == NULL_TREE)
17290 switch (CALL_EXPR_IFN (t))
17292 case IFN_LAUNDER:
17293 gcc_assert (nargs == 1);
17294 if (vec_safe_length (call_args) != 1)
17296 error_at (EXPR_LOC_OR_LOC (t, input_location),
17297 "wrong number of arguments to "
17298 "%<__builtin_launder%>");
17299 ret = error_mark_node;
17301 else
17302 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17303 input_location),
17304 (*call_args)[0], complain);
17305 break;
17307 default:
17308 /* Unsupported internal function with arguments. */
17309 gcc_unreachable ();
17311 else if (TREE_CODE (function) == OFFSET_REF)
17312 ret = build_offset_ref_call_from_tree (function, &call_args,
17313 complain);
17314 else if (TREE_CODE (function) == COMPONENT_REF)
17316 tree instance = TREE_OPERAND (function, 0);
17317 tree fn = TREE_OPERAND (function, 1);
17319 if (processing_template_decl
17320 && (type_dependent_expression_p (instance)
17321 || (!BASELINK_P (fn)
17322 && TREE_CODE (fn) != FIELD_DECL)
17323 || type_dependent_expression_p (fn)
17324 || any_type_dependent_arguments_p (call_args)))
17325 ret = build_nt_call_vec (function, call_args);
17326 else if (!BASELINK_P (fn))
17327 ret = finish_call_expr (function, &call_args,
17328 /*disallow_virtual=*/false,
17329 /*koenig_p=*/false,
17330 complain);
17331 else
17332 ret = (build_new_method_call
17333 (instance, fn,
17334 &call_args, NULL_TREE,
17335 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17336 /*fn_p=*/NULL,
17337 complain));
17339 else
17340 ret = finish_call_expr (function, &call_args,
17341 /*disallow_virtual=*/qualified_p,
17342 koenig_p,
17343 complain);
17345 release_tree_vector (call_args);
17347 if (ret != error_mark_node)
17349 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17350 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17351 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17352 bool thk = CALL_FROM_THUNK_P (t);
17353 if (op || ord || rev || thk)
17355 function = extract_call_expr (ret);
17356 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17357 CALL_EXPR_ORDERED_ARGS (function) = ord;
17358 CALL_EXPR_REVERSE_ARGS (function) = rev;
17359 if (thk)
17361 CALL_FROM_THUNK_P (function) = true;
17362 /* The thunk location is not interesting. */
17363 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17368 RETURN (ret);
17371 case COND_EXPR:
17373 tree cond = RECUR (TREE_OPERAND (t, 0));
17374 tree folded_cond = fold_non_dependent_expr (cond);
17375 tree exp1, exp2;
17377 if (TREE_CODE (folded_cond) == INTEGER_CST)
17379 if (integer_zerop (folded_cond))
17381 ++c_inhibit_evaluation_warnings;
17382 exp1 = RECUR (TREE_OPERAND (t, 1));
17383 --c_inhibit_evaluation_warnings;
17384 exp2 = RECUR (TREE_OPERAND (t, 2));
17386 else
17388 exp1 = RECUR (TREE_OPERAND (t, 1));
17389 ++c_inhibit_evaluation_warnings;
17390 exp2 = RECUR (TREE_OPERAND (t, 2));
17391 --c_inhibit_evaluation_warnings;
17393 cond = folded_cond;
17395 else
17397 exp1 = RECUR (TREE_OPERAND (t, 1));
17398 exp2 = RECUR (TREE_OPERAND (t, 2));
17401 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17402 cond, exp1, exp2, complain));
17405 case PSEUDO_DTOR_EXPR:
17407 tree op0 = RECUR (TREE_OPERAND (t, 0));
17408 tree op1 = RECUR (TREE_OPERAND (t, 1));
17409 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17410 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17411 input_location));
17414 case TREE_LIST:
17416 tree purpose, value, chain;
17418 if (t == void_list_node)
17419 RETURN (t);
17421 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17422 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17424 /* We have pack expansions, so expand those and
17425 create a new list out of it. */
17426 tree purposevec = NULL_TREE;
17427 tree valuevec = NULL_TREE;
17428 tree chain;
17429 int i, len = -1;
17431 /* Expand the argument expressions. */
17432 if (TREE_PURPOSE (t))
17433 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17434 complain, in_decl);
17435 if (TREE_VALUE (t))
17436 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17437 complain, in_decl);
17439 /* Build the rest of the list. */
17440 chain = TREE_CHAIN (t);
17441 if (chain && chain != void_type_node)
17442 chain = RECUR (chain);
17444 /* Determine the number of arguments. */
17445 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17447 len = TREE_VEC_LENGTH (purposevec);
17448 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17450 else if (TREE_CODE (valuevec) == TREE_VEC)
17451 len = TREE_VEC_LENGTH (valuevec);
17452 else
17454 /* Since we only performed a partial substitution into
17455 the argument pack, we only RETURN (a single list
17456 node. */
17457 if (purposevec == TREE_PURPOSE (t)
17458 && valuevec == TREE_VALUE (t)
17459 && chain == TREE_CHAIN (t))
17460 RETURN (t);
17462 RETURN (tree_cons (purposevec, valuevec, chain));
17465 /* Convert the argument vectors into a TREE_LIST */
17466 i = len;
17467 while (i > 0)
17469 /* Grab the Ith values. */
17470 i--;
17471 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17472 : NULL_TREE;
17473 value
17474 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17475 : NULL_TREE;
17477 /* Build the list (backwards). */
17478 chain = tree_cons (purpose, value, chain);
17481 RETURN (chain);
17484 purpose = TREE_PURPOSE (t);
17485 if (purpose)
17486 purpose = RECUR (purpose);
17487 value = TREE_VALUE (t);
17488 if (value)
17489 value = RECUR (value);
17490 chain = TREE_CHAIN (t);
17491 if (chain && chain != void_type_node)
17492 chain = RECUR (chain);
17493 if (purpose == TREE_PURPOSE (t)
17494 && value == TREE_VALUE (t)
17495 && chain == TREE_CHAIN (t))
17496 RETURN (t);
17497 RETURN (tree_cons (purpose, value, chain));
17500 case COMPONENT_REF:
17502 tree object;
17503 tree object_type;
17504 tree member;
17505 tree r;
17507 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17508 args, complain, in_decl);
17509 /* Remember that there was a reference to this entity. */
17510 if (DECL_P (object)
17511 && !mark_used (object, complain) && !(complain & tf_error))
17512 RETURN (error_mark_node);
17513 object_type = TREE_TYPE (object);
17515 member = TREE_OPERAND (t, 1);
17516 if (BASELINK_P (member))
17517 member = tsubst_baselink (member,
17518 non_reference (TREE_TYPE (object)),
17519 args, complain, in_decl);
17520 else
17521 member = tsubst_copy (member, args, complain, in_decl);
17522 if (member == error_mark_node)
17523 RETURN (error_mark_node);
17525 if (TREE_CODE (member) == FIELD_DECL)
17527 r = finish_non_static_data_member (member, object, NULL_TREE);
17528 if (TREE_CODE (r) == COMPONENT_REF)
17529 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17530 RETURN (r);
17532 else if (type_dependent_expression_p (object))
17533 /* We can't do much here. */;
17534 else if (!CLASS_TYPE_P (object_type))
17536 if (scalarish_type_p (object_type))
17538 tree s = NULL_TREE;
17539 tree dtor = member;
17541 if (TREE_CODE (dtor) == SCOPE_REF)
17543 s = TREE_OPERAND (dtor, 0);
17544 dtor = TREE_OPERAND (dtor, 1);
17546 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17548 dtor = TREE_OPERAND (dtor, 0);
17549 if (TYPE_P (dtor))
17550 RETURN (finish_pseudo_destructor_expr
17551 (object, s, dtor, input_location));
17555 else if (TREE_CODE (member) == SCOPE_REF
17556 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17558 /* Lookup the template functions now that we know what the
17559 scope is. */
17560 tree scope = TREE_OPERAND (member, 0);
17561 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17562 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17563 member = lookup_qualified_name (scope, tmpl,
17564 /*is_type_p=*/false,
17565 /*complain=*/false);
17566 if (BASELINK_P (member))
17568 BASELINK_FUNCTIONS (member)
17569 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17570 args);
17571 member = (adjust_result_of_qualified_name_lookup
17572 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17573 object_type));
17575 else
17577 qualified_name_lookup_error (scope, tmpl, member,
17578 input_location);
17579 RETURN (error_mark_node);
17582 else if (TREE_CODE (member) == SCOPE_REF
17583 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17584 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17586 if (complain & tf_error)
17588 if (TYPE_P (TREE_OPERAND (member, 0)))
17589 error ("%qT is not a class or namespace",
17590 TREE_OPERAND (member, 0));
17591 else
17592 error ("%qD is not a class or namespace",
17593 TREE_OPERAND (member, 0));
17595 RETURN (error_mark_node);
17598 r = finish_class_member_access_expr (object, member,
17599 /*template_p=*/false,
17600 complain);
17601 if (TREE_CODE (r) == COMPONENT_REF)
17602 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17603 RETURN (r);
17606 case THROW_EXPR:
17607 RETURN (build_throw
17608 (RECUR (TREE_OPERAND (t, 0))));
17610 case CONSTRUCTOR:
17612 vec<constructor_elt, va_gc> *n;
17613 constructor_elt *ce;
17614 unsigned HOST_WIDE_INT idx;
17615 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17616 bool process_index_p;
17617 int newlen;
17618 bool need_copy_p = false;
17619 tree r;
17621 if (type == error_mark_node)
17622 RETURN (error_mark_node);
17624 /* digest_init will do the wrong thing if we let it. */
17625 if (type && TYPE_PTRMEMFUNC_P (type))
17626 RETURN (t);
17628 /* We do not want to process the index of aggregate
17629 initializers as they are identifier nodes which will be
17630 looked up by digest_init. */
17631 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17633 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17634 newlen = vec_safe_length (n);
17635 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17637 if (ce->index && process_index_p
17638 /* An identifier index is looked up in the type
17639 being initialized, not the current scope. */
17640 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17641 ce->index = RECUR (ce->index);
17643 if (PACK_EXPANSION_P (ce->value))
17645 /* Substitute into the pack expansion. */
17646 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17647 in_decl);
17649 if (ce->value == error_mark_node
17650 || PACK_EXPANSION_P (ce->value))
17652 else if (TREE_VEC_LENGTH (ce->value) == 1)
17653 /* Just move the argument into place. */
17654 ce->value = TREE_VEC_ELT (ce->value, 0);
17655 else
17657 /* Update the length of the final CONSTRUCTOR
17658 arguments vector, and note that we will need to
17659 copy.*/
17660 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17661 need_copy_p = true;
17664 else
17665 ce->value = RECUR (ce->value);
17668 if (need_copy_p)
17670 vec<constructor_elt, va_gc> *old_n = n;
17672 vec_alloc (n, newlen);
17673 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17675 if (TREE_CODE (ce->value) == TREE_VEC)
17677 int i, len = TREE_VEC_LENGTH (ce->value);
17678 for (i = 0; i < len; ++i)
17679 CONSTRUCTOR_APPEND_ELT (n, 0,
17680 TREE_VEC_ELT (ce->value, i));
17682 else
17683 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17687 r = build_constructor (init_list_type_node, n);
17688 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17690 if (TREE_HAS_CONSTRUCTOR (t))
17691 RETURN (finish_compound_literal (type, r, complain));
17693 TREE_TYPE (r) = type;
17694 RETURN (r);
17697 case TYPEID_EXPR:
17699 tree operand_0 = TREE_OPERAND (t, 0);
17700 if (TYPE_P (operand_0))
17702 operand_0 = tsubst (operand_0, args, complain, in_decl);
17703 RETURN (get_typeid (operand_0, complain));
17705 else
17707 operand_0 = RECUR (operand_0);
17708 RETURN (build_typeid (operand_0, complain));
17712 case VAR_DECL:
17713 if (!args)
17714 RETURN (t);
17715 else if (DECL_PACK_P (t))
17717 /* We don't build decls for an instantiation of a
17718 variadic capture proxy, we instantiate the elements
17719 when needed. */
17720 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17721 return RECUR (DECL_VALUE_EXPR (t));
17723 /* Fall through */
17725 case PARM_DECL:
17727 tree r = tsubst_copy (t, args, complain, in_decl);
17728 /* ??? We're doing a subset of finish_id_expression here. */
17729 if (VAR_P (r)
17730 && !processing_template_decl
17731 && !cp_unevaluated_operand
17732 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17733 && CP_DECL_THREAD_LOCAL_P (r))
17735 if (tree wrap = get_tls_wrapper_fn (r))
17736 /* Replace an evaluated use of the thread_local variable with
17737 a call to its wrapper. */
17738 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17740 else if (outer_automatic_var_p (r))
17742 r = process_outer_var_ref (r, complain);
17743 if (is_capture_proxy (r))
17744 register_local_specialization (r, t);
17747 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17748 /* If the original type was a reference, we'll be wrapped in
17749 the appropriate INDIRECT_REF. */
17750 r = convert_from_reference (r);
17751 RETURN (r);
17754 case VA_ARG_EXPR:
17756 tree op0 = RECUR (TREE_OPERAND (t, 0));
17757 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17758 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17761 case OFFSETOF_EXPR:
17763 tree object_ptr
17764 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17765 in_decl, /*function_p=*/false,
17766 /*integral_constant_expression_p=*/false);
17767 RETURN (finish_offsetof (object_ptr,
17768 RECUR (TREE_OPERAND (t, 0)),
17769 EXPR_LOCATION (t)));
17772 case ADDRESSOF_EXPR:
17773 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17774 RECUR (TREE_OPERAND (t, 0)), complain));
17776 case TRAIT_EXPR:
17778 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17779 complain, in_decl);
17781 tree type2 = TRAIT_EXPR_TYPE2 (t);
17782 if (type2 && TREE_CODE (type2) == TREE_LIST)
17783 type2 = RECUR (type2);
17784 else if (type2)
17785 type2 = tsubst (type2, args, complain, in_decl);
17787 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17790 case STMT_EXPR:
17792 tree old_stmt_expr = cur_stmt_expr;
17793 tree stmt_expr = begin_stmt_expr ();
17795 cur_stmt_expr = stmt_expr;
17796 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17797 integral_constant_expression_p);
17798 stmt_expr = finish_stmt_expr (stmt_expr, false);
17799 cur_stmt_expr = old_stmt_expr;
17801 /* If the resulting list of expression statement is empty,
17802 fold it further into void_node. */
17803 if (empty_expr_stmt_p (stmt_expr))
17804 stmt_expr = void_node;
17806 RETURN (stmt_expr);
17809 case LAMBDA_EXPR:
17811 tree r = build_lambda_expr ();
17813 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17814 LAMBDA_EXPR_CLOSURE (r) = type;
17815 CLASSTYPE_LAMBDA_EXPR (type) = r;
17817 LAMBDA_EXPR_LOCATION (r)
17818 = LAMBDA_EXPR_LOCATION (t);
17819 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17820 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17821 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17822 LAMBDA_EXPR_DISCRIMINATOR (r)
17823 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17824 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17825 if (!scope)
17826 /* No substitution needed. */;
17827 else if (VAR_OR_FUNCTION_DECL_P (scope))
17828 /* For a function or variable scope, we want to use tsubst so that we
17829 don't complain about referring to an auto before deduction. */
17830 scope = tsubst (scope, args, complain, in_decl);
17831 else if (TREE_CODE (scope) == PARM_DECL)
17833 /* Look up the parameter we want directly, as tsubst_copy
17834 doesn't do what we need. */
17835 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17836 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17837 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17838 parm = DECL_CHAIN (parm);
17839 scope = parm;
17840 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17841 if (DECL_CONTEXT (scope) == NULL_TREE)
17842 DECL_CONTEXT (scope) = fn;
17844 else if (TREE_CODE (scope) == FIELD_DECL)
17845 /* For a field, use tsubst_copy so that we look up the existing field
17846 rather than build a new one. */
17847 scope = RECUR (scope);
17848 else
17849 gcc_unreachable ();
17850 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17852 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17853 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17855 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17856 determine_visibility (TYPE_NAME (type));
17857 /* Now that we know visibility, instantiate the type so we have a
17858 declaration of the op() for later calls to lambda_function. */
17859 complete_type (type);
17861 if (tree fn = lambda_function (type))
17862 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17864 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17866 insert_pending_capture_proxies ();
17868 RETURN (build_lambda_object (r));
17871 case TARGET_EXPR:
17872 /* We can get here for a constant initializer of non-dependent type.
17873 FIXME stop folding in cp_parser_initializer_clause. */
17875 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17876 complain);
17877 RETURN (r);
17880 case TRANSACTION_EXPR:
17881 RETURN (tsubst_expr(t, args, complain, in_decl,
17882 integral_constant_expression_p));
17884 case PAREN_EXPR:
17885 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17887 case VEC_PERM_EXPR:
17889 tree op0 = RECUR (TREE_OPERAND (t, 0));
17890 tree op1 = RECUR (TREE_OPERAND (t, 1));
17891 tree op2 = RECUR (TREE_OPERAND (t, 2));
17892 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17893 complain));
17896 case REQUIRES_EXPR:
17897 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17899 default:
17900 /* Handle Objective-C++ constructs, if appropriate. */
17902 tree subst
17903 = objcp_tsubst_copy_and_build (t, args, complain,
17904 in_decl, /*function_p=*/false);
17905 if (subst)
17906 RETURN (subst);
17908 RETURN (tsubst_copy (t, args, complain, in_decl));
17911 #undef RECUR
17912 #undef RETURN
17913 out:
17914 input_location = loc;
17915 return retval;
17918 /* Verify that the instantiated ARGS are valid. For type arguments,
17919 make sure that the type's linkage is ok. For non-type arguments,
17920 make sure they are constants if they are integral or enumerations.
17921 Emit an error under control of COMPLAIN, and return TRUE on error. */
17923 static bool
17924 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17926 if (dependent_template_arg_p (t))
17927 return false;
17928 if (ARGUMENT_PACK_P (t))
17930 tree vec = ARGUMENT_PACK_ARGS (t);
17931 int len = TREE_VEC_LENGTH (vec);
17932 bool result = false;
17933 int i;
17935 for (i = 0; i < len; ++i)
17936 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17937 result = true;
17938 return result;
17940 else if (TYPE_P (t))
17942 /* [basic.link]: A name with no linkage (notably, the name
17943 of a class or enumeration declared in a local scope)
17944 shall not be used to declare an entity with linkage.
17945 This implies that names with no linkage cannot be used as
17946 template arguments
17948 DR 757 relaxes this restriction for C++0x. */
17949 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17950 : no_linkage_check (t, /*relaxed_p=*/false));
17952 if (nt)
17954 /* DR 488 makes use of a type with no linkage cause
17955 type deduction to fail. */
17956 if (complain & tf_error)
17958 if (TYPE_UNNAMED_P (nt))
17959 error ("%qT is/uses unnamed type", t);
17960 else
17961 error ("template argument for %qD uses local type %qT",
17962 tmpl, t);
17964 return true;
17966 /* In order to avoid all sorts of complications, we do not
17967 allow variably-modified types as template arguments. */
17968 else if (variably_modified_type_p (t, NULL_TREE))
17970 if (complain & tf_error)
17971 error ("%qT is a variably modified type", t);
17972 return true;
17975 /* Class template and alias template arguments should be OK. */
17976 else if (DECL_TYPE_TEMPLATE_P (t))
17978 /* A non-type argument of integral or enumerated type must be a
17979 constant. */
17980 else if (TREE_TYPE (t)
17981 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17982 && !REFERENCE_REF_P (t)
17983 && !TREE_CONSTANT (t))
17985 if (complain & tf_error)
17986 error ("integral expression %qE is not constant", t);
17987 return true;
17989 return false;
17992 static bool
17993 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17995 int ix, len = DECL_NTPARMS (tmpl);
17996 bool result = false;
17998 for (ix = 0; ix != len; ix++)
18000 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18001 result = true;
18003 if (result && (complain & tf_error))
18004 error (" trying to instantiate %qD", tmpl);
18005 return result;
18008 /* We're out of SFINAE context now, so generate diagnostics for the access
18009 errors we saw earlier when instantiating D from TMPL and ARGS. */
18011 static void
18012 recheck_decl_substitution (tree d, tree tmpl, tree args)
18014 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18015 tree type = TREE_TYPE (pattern);
18016 location_t loc = input_location;
18018 push_access_scope (d);
18019 push_deferring_access_checks (dk_no_deferred);
18020 input_location = DECL_SOURCE_LOCATION (pattern);
18021 tsubst (type, args, tf_warning_or_error, d);
18022 input_location = loc;
18023 pop_deferring_access_checks ();
18024 pop_access_scope (d);
18027 /* Instantiate the indicated variable, function, or alias template TMPL with
18028 the template arguments in TARG_PTR. */
18030 static tree
18031 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18033 tree targ_ptr = orig_args;
18034 tree fndecl;
18035 tree gen_tmpl;
18036 tree spec;
18037 bool access_ok = true;
18039 if (tmpl == error_mark_node)
18040 return error_mark_node;
18042 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18044 /* If this function is a clone, handle it specially. */
18045 if (DECL_CLONED_FUNCTION_P (tmpl))
18047 tree spec;
18048 tree clone;
18050 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18051 DECL_CLONED_FUNCTION. */
18052 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18053 targ_ptr, complain);
18054 if (spec == error_mark_node)
18055 return error_mark_node;
18057 /* Look for the clone. */
18058 FOR_EACH_CLONE (clone, spec)
18059 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18060 return clone;
18061 /* We should always have found the clone by now. */
18062 gcc_unreachable ();
18063 return NULL_TREE;
18066 if (targ_ptr == error_mark_node)
18067 return error_mark_node;
18069 /* Check to see if we already have this specialization. */
18070 gen_tmpl = most_general_template (tmpl);
18071 if (TMPL_ARGS_DEPTH (targ_ptr)
18072 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18073 /* targ_ptr only has the innermost template args, so add the outer ones
18074 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18075 the case of a non-dependent call within a template definition). */
18076 targ_ptr = (add_outermost_template_args
18077 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18078 targ_ptr));
18080 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18081 but it doesn't seem to be on the hot path. */
18082 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18084 gcc_assert (tmpl == gen_tmpl
18085 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18086 == spec)
18087 || fndecl == NULL_TREE);
18089 if (spec != NULL_TREE)
18091 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18093 if (complain & tf_error)
18094 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18095 return error_mark_node;
18097 return spec;
18100 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18101 complain))
18102 return error_mark_node;
18104 /* We are building a FUNCTION_DECL, during which the access of its
18105 parameters and return types have to be checked. However this
18106 FUNCTION_DECL which is the desired context for access checking
18107 is not built yet. We solve this chicken-and-egg problem by
18108 deferring all checks until we have the FUNCTION_DECL. */
18109 push_deferring_access_checks (dk_deferred);
18111 /* Instantiation of the function happens in the context of the function
18112 template, not the context of the overload resolution we're doing. */
18113 push_to_top_level ();
18114 /* If there are dependent arguments, e.g. because we're doing partial
18115 ordering, make sure processing_template_decl stays set. */
18116 if (uses_template_parms (targ_ptr))
18117 ++processing_template_decl;
18118 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18120 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18121 complain, gen_tmpl, true);
18122 push_nested_class (ctx);
18125 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18127 fndecl = NULL_TREE;
18128 if (VAR_P (pattern))
18130 /* We need to determine if we're using a partial or explicit
18131 specialization now, because the type of the variable could be
18132 different. */
18133 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18134 tree elt = most_specialized_partial_spec (tid, complain);
18135 if (elt == error_mark_node)
18136 pattern = error_mark_node;
18137 else if (elt)
18139 tree partial_tmpl = TREE_VALUE (elt);
18140 tree partial_args = TREE_PURPOSE (elt);
18141 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18142 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18146 /* Substitute template parameters to obtain the specialization. */
18147 if (fndecl == NULL_TREE)
18148 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18149 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18150 pop_nested_class ();
18151 pop_from_top_level ();
18153 if (fndecl == error_mark_node)
18155 pop_deferring_access_checks ();
18156 return error_mark_node;
18159 /* The DECL_TI_TEMPLATE should always be the immediate parent
18160 template, not the most general template. */
18161 DECL_TI_TEMPLATE (fndecl) = tmpl;
18162 DECL_TI_ARGS (fndecl) = targ_ptr;
18164 /* Now we know the specialization, compute access previously
18165 deferred. Do no access control for inheriting constructors,
18166 as we already checked access for the inherited constructor. */
18167 if (!(flag_new_inheriting_ctors
18168 && DECL_INHERITED_CTOR (fndecl)))
18170 push_access_scope (fndecl);
18171 if (!perform_deferred_access_checks (complain))
18172 access_ok = false;
18173 pop_access_scope (fndecl);
18175 pop_deferring_access_checks ();
18177 /* If we've just instantiated the main entry point for a function,
18178 instantiate all the alternate entry points as well. We do this
18179 by cloning the instantiation of the main entry point, not by
18180 instantiating the template clones. */
18181 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18182 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
18184 if (!access_ok)
18186 if (!(complain & tf_error))
18188 /* Remember to reinstantiate when we're out of SFINAE so the user
18189 can see the errors. */
18190 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18192 return error_mark_node;
18194 return fndecl;
18197 /* Wrapper for instantiate_template_1. */
18199 tree
18200 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18202 tree ret;
18203 timevar_push (TV_TEMPLATE_INST);
18204 ret = instantiate_template_1 (tmpl, orig_args, complain);
18205 timevar_pop (TV_TEMPLATE_INST);
18206 return ret;
18209 /* Instantiate the alias template TMPL with ARGS. Also push a template
18210 instantiation level, which instantiate_template doesn't do because
18211 functions and variables have sufficient context established by the
18212 callers. */
18214 static tree
18215 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18217 struct pending_template *old_last_pend = last_pending_template;
18218 struct tinst_level *old_error_tinst = last_error_tinst_level;
18219 if (tmpl == error_mark_node || args == error_mark_node)
18220 return error_mark_node;
18221 tree tinst = build_tree_list (tmpl, args);
18222 if (!push_tinst_level (tinst))
18224 ggc_free (tinst);
18225 return error_mark_node;
18228 args =
18229 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18230 args, tmpl, complain,
18231 /*require_all_args=*/true,
18232 /*use_default_args=*/true);
18234 tree r = instantiate_template (tmpl, args, complain);
18235 pop_tinst_level ();
18236 /* We can't free this if a pending_template entry or last_error_tinst_level
18237 is pointing at it. */
18238 if (last_pending_template == old_last_pend
18239 && last_error_tinst_level == old_error_tinst)
18240 ggc_free (tinst);
18242 return r;
18245 /* PARM is a template parameter pack for FN. Returns true iff
18246 PARM is used in a deducible way in the argument list of FN. */
18248 static bool
18249 pack_deducible_p (tree parm, tree fn)
18251 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18252 for (; t; t = TREE_CHAIN (t))
18254 tree type = TREE_VALUE (t);
18255 tree packs;
18256 if (!PACK_EXPANSION_P (type))
18257 continue;
18258 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18259 packs; packs = TREE_CHAIN (packs))
18260 if (template_args_equal (TREE_VALUE (packs), parm))
18262 /* The template parameter pack is used in a function parameter
18263 pack. If this is the end of the parameter list, the
18264 template parameter pack is deducible. */
18265 if (TREE_CHAIN (t) == void_list_node)
18266 return true;
18267 else
18268 /* Otherwise, not. Well, it could be deduced from
18269 a non-pack parameter, but doing so would end up with
18270 a deduction mismatch, so don't bother. */
18271 return false;
18274 /* The template parameter pack isn't used in any function parameter
18275 packs, but it might be used deeper, e.g. tuple<Args...>. */
18276 return true;
18279 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18280 NARGS elements of the arguments that are being used when calling
18281 it. TARGS is a vector into which the deduced template arguments
18282 are placed.
18284 Returns either a FUNCTION_DECL for the matching specialization of FN or
18285 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18286 true, diagnostics will be printed to explain why it failed.
18288 If FN is a conversion operator, or we are trying to produce a specific
18289 specialization, RETURN_TYPE is the return type desired.
18291 The EXPLICIT_TARGS are explicit template arguments provided via a
18292 template-id.
18294 The parameter STRICT is one of:
18296 DEDUCE_CALL:
18297 We are deducing arguments for a function call, as in
18298 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18299 deducing arguments for a call to the result of a conversion
18300 function template, as in [over.call.object].
18302 DEDUCE_CONV:
18303 We are deducing arguments for a conversion function, as in
18304 [temp.deduct.conv].
18306 DEDUCE_EXACT:
18307 We are deducing arguments when doing an explicit instantiation
18308 as in [temp.explicit], when determining an explicit specialization
18309 as in [temp.expl.spec], or when taking the address of a function
18310 template, as in [temp.deduct.funcaddr]. */
18312 tree
18313 fn_type_unification (tree fn,
18314 tree explicit_targs,
18315 tree targs,
18316 const tree *args,
18317 unsigned int nargs,
18318 tree return_type,
18319 unification_kind_t strict,
18320 int flags,
18321 bool explain_p,
18322 bool decltype_p)
18324 tree parms;
18325 tree fntype;
18326 tree decl = NULL_TREE;
18327 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18328 bool ok;
18329 static int deduction_depth;
18330 struct pending_template *old_last_pend = last_pending_template;
18331 struct tinst_level *old_error_tinst = last_error_tinst_level;
18333 tree orig_fn = fn;
18334 if (flag_new_inheriting_ctors)
18335 fn = strip_inheriting_ctors (fn);
18337 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18338 tree tinst;
18339 tree r = error_mark_node;
18341 tree full_targs = targs;
18342 if (TMPL_ARGS_DEPTH (targs)
18343 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18344 full_targs = (add_outermost_template_args
18345 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18346 targs));
18348 if (decltype_p)
18349 complain |= tf_decltype;
18351 /* In C++0x, it's possible to have a function template whose type depends
18352 on itself recursively. This is most obvious with decltype, but can also
18353 occur with enumeration scope (c++/48969). So we need to catch infinite
18354 recursion and reject the substitution at deduction time; this function
18355 will return error_mark_node for any repeated substitution.
18357 This also catches excessive recursion such as when f<N> depends on
18358 f<N-1> across all integers, and returns error_mark_node for all the
18359 substitutions back up to the initial one.
18361 This is, of course, not reentrant. */
18362 if (excessive_deduction_depth)
18363 return error_mark_node;
18364 tinst = build_tree_list (fn, NULL_TREE);
18365 ++deduction_depth;
18367 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18369 fntype = TREE_TYPE (fn);
18370 if (explicit_targs)
18372 /* [temp.deduct]
18374 The specified template arguments must match the template
18375 parameters in kind (i.e., type, nontype, template), and there
18376 must not be more arguments than there are parameters;
18377 otherwise type deduction fails.
18379 Nontype arguments must match the types of the corresponding
18380 nontype template parameters, or must be convertible to the
18381 types of the corresponding nontype parameters as specified in
18382 _temp.arg.nontype_, otherwise type deduction fails.
18384 All references in the function type of the function template
18385 to the corresponding template parameters are replaced by the
18386 specified template argument values. If a substitution in a
18387 template parameter or in the function type of the function
18388 template results in an invalid type, type deduction fails. */
18389 int i, len = TREE_VEC_LENGTH (tparms);
18390 location_t loc = input_location;
18391 bool incomplete = false;
18393 if (explicit_targs == error_mark_node)
18394 goto fail;
18396 if (TMPL_ARGS_DEPTH (explicit_targs)
18397 < TMPL_ARGS_DEPTH (full_targs))
18398 explicit_targs = add_outermost_template_args (full_targs,
18399 explicit_targs);
18401 /* Adjust any explicit template arguments before entering the
18402 substitution context. */
18403 explicit_targs
18404 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18405 complain,
18406 /*require_all_args=*/false,
18407 /*use_default_args=*/false));
18408 if (explicit_targs == error_mark_node)
18409 goto fail;
18411 /* Substitute the explicit args into the function type. This is
18412 necessary so that, for instance, explicitly declared function
18413 arguments can match null pointed constants. If we were given
18414 an incomplete set of explicit args, we must not do semantic
18415 processing during substitution as we could create partial
18416 instantiations. */
18417 for (i = 0; i < len; i++)
18419 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18420 bool parameter_pack = false;
18421 tree targ = TREE_VEC_ELT (explicit_targs, i);
18423 /* Dig out the actual parm. */
18424 if (TREE_CODE (parm) == TYPE_DECL
18425 || TREE_CODE (parm) == TEMPLATE_DECL)
18427 parm = TREE_TYPE (parm);
18428 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18430 else if (TREE_CODE (parm) == PARM_DECL)
18432 parm = DECL_INITIAL (parm);
18433 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18436 if (!parameter_pack && targ == NULL_TREE)
18437 /* No explicit argument for this template parameter. */
18438 incomplete = true;
18440 if (parameter_pack && pack_deducible_p (parm, fn))
18442 /* Mark the argument pack as "incomplete". We could
18443 still deduce more arguments during unification.
18444 We remove this mark in type_unification_real. */
18445 if (targ)
18447 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18448 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18449 = ARGUMENT_PACK_ARGS (targ);
18452 /* We have some incomplete argument packs. */
18453 incomplete = true;
18457 TREE_VALUE (tinst) = explicit_targs;
18458 if (!push_tinst_level (tinst))
18460 excessive_deduction_depth = true;
18461 goto fail;
18463 processing_template_decl += incomplete;
18464 input_location = DECL_SOURCE_LOCATION (fn);
18465 /* Ignore any access checks; we'll see them again in
18466 instantiate_template and they might have the wrong
18467 access path at this point. */
18468 push_deferring_access_checks (dk_deferred);
18469 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18470 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18471 pop_deferring_access_checks ();
18472 input_location = loc;
18473 processing_template_decl -= incomplete;
18474 pop_tinst_level ();
18476 if (fntype == error_mark_node)
18477 goto fail;
18479 /* Place the explicitly specified arguments in TARGS. */
18480 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18481 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18482 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18485 /* Never do unification on the 'this' parameter. */
18486 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18488 if (return_type && strict == DEDUCE_CALL)
18490 /* We're deducing for a call to the result of a template conversion
18491 function. The parms we really want are in return_type. */
18492 if (POINTER_TYPE_P (return_type))
18493 return_type = TREE_TYPE (return_type);
18494 parms = TYPE_ARG_TYPES (return_type);
18496 else if (return_type)
18498 tree *new_args;
18500 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18501 new_args = XALLOCAVEC (tree, nargs + 1);
18502 new_args[0] = return_type;
18503 memcpy (new_args + 1, args, nargs * sizeof (tree));
18504 args = new_args;
18505 ++nargs;
18508 /* We allow incomplete unification without an error message here
18509 because the standard doesn't seem to explicitly prohibit it. Our
18510 callers must be ready to deal with unification failures in any
18511 event. */
18513 TREE_VALUE (tinst) = targs;
18514 /* If we aren't explaining yet, push tinst context so we can see where
18515 any errors (e.g. from class instantiations triggered by instantiation
18516 of default template arguments) come from. If we are explaining, this
18517 context is redundant. */
18518 if (!explain_p && !push_tinst_level (tinst))
18520 excessive_deduction_depth = true;
18521 goto fail;
18524 /* type_unification_real will pass back any access checks from default
18525 template argument substitution. */
18526 vec<deferred_access_check, va_gc> *checks;
18527 checks = NULL;
18529 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18530 full_targs, parms, args, nargs, /*subr=*/0,
18531 strict, flags, &checks, explain_p);
18532 if (!explain_p)
18533 pop_tinst_level ();
18534 if (!ok)
18535 goto fail;
18537 /* Now that we have bindings for all of the template arguments,
18538 ensure that the arguments deduced for the template template
18539 parameters have compatible template parameter lists. We cannot
18540 check this property before we have deduced all template
18541 arguments, because the template parameter types of a template
18542 template parameter might depend on prior template parameters
18543 deduced after the template template parameter. The following
18544 ill-formed example illustrates this issue:
18546 template<typename T, template<T> class C> void f(C<5>, T);
18548 template<int N> struct X {};
18550 void g() {
18551 f(X<5>(), 5l); // error: template argument deduction fails
18554 The template parameter list of 'C' depends on the template type
18555 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18556 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18557 time that we deduce 'C'. */
18558 if (!template_template_parm_bindings_ok_p
18559 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18561 unify_inconsistent_template_template_parameters (explain_p);
18562 goto fail;
18565 /* All is well so far. Now, check:
18567 [temp.deduct]
18569 When all template arguments have been deduced, all uses of
18570 template parameters in nondeduced contexts are replaced with
18571 the corresponding deduced argument values. If the
18572 substitution results in an invalid type, as described above,
18573 type deduction fails. */
18574 TREE_VALUE (tinst) = targs;
18575 if (!push_tinst_level (tinst))
18577 excessive_deduction_depth = true;
18578 goto fail;
18581 /* Also collect access checks from the instantiation. */
18582 reopen_deferring_access_checks (checks);
18584 decl = instantiate_template (fn, targs, complain);
18586 checks = get_deferred_access_checks ();
18587 pop_deferring_access_checks ();
18589 pop_tinst_level ();
18591 if (decl == error_mark_node)
18592 goto fail;
18594 /* Now perform any access checks encountered during substitution. */
18595 push_access_scope (decl);
18596 ok = perform_access_checks (checks, complain);
18597 pop_access_scope (decl);
18598 if (!ok)
18599 goto fail;
18601 /* If we're looking for an exact match, check that what we got
18602 is indeed an exact match. It might not be if some template
18603 parameters are used in non-deduced contexts. But don't check
18604 for an exact match if we have dependent template arguments;
18605 in that case we're doing partial ordering, and we already know
18606 that we have two candidates that will provide the actual type. */
18607 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18609 tree substed = TREE_TYPE (decl);
18610 unsigned int i;
18612 tree sarg
18613 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18614 if (return_type)
18615 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18616 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18617 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18619 unify_type_mismatch (explain_p, args[i],
18620 TREE_VALUE (sarg));
18621 goto fail;
18625 /* After doing deduction with the inherited constructor, actually return an
18626 instantiation of the inheriting constructor. */
18627 if (orig_fn != fn)
18628 decl = instantiate_template (orig_fn, targs, complain);
18630 r = decl;
18632 fail:
18633 --deduction_depth;
18634 if (excessive_deduction_depth)
18636 if (deduction_depth == 0)
18637 /* Reset once we're all the way out. */
18638 excessive_deduction_depth = false;
18641 /* We can't free this if a pending_template entry or last_error_tinst_level
18642 is pointing at it. */
18643 if (last_pending_template == old_last_pend
18644 && last_error_tinst_level == old_error_tinst)
18645 ggc_free (tinst);
18647 return r;
18650 /* Adjust types before performing type deduction, as described in
18651 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18652 sections are symmetric. PARM is the type of a function parameter
18653 or the return type of the conversion function. ARG is the type of
18654 the argument passed to the call, or the type of the value
18655 initialized with the result of the conversion function.
18656 ARG_EXPR is the original argument expression, which may be null. */
18658 static int
18659 maybe_adjust_types_for_deduction (unification_kind_t strict,
18660 tree* parm,
18661 tree* arg,
18662 tree arg_expr)
18664 int result = 0;
18666 switch (strict)
18668 case DEDUCE_CALL:
18669 break;
18671 case DEDUCE_CONV:
18672 /* Swap PARM and ARG throughout the remainder of this
18673 function; the handling is precisely symmetric since PARM
18674 will initialize ARG rather than vice versa. */
18675 std::swap (parm, arg);
18676 break;
18678 case DEDUCE_EXACT:
18679 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18680 too, but here handle it by stripping the reference from PARM
18681 rather than by adding it to ARG. */
18682 if (TREE_CODE (*parm) == REFERENCE_TYPE
18683 && TYPE_REF_IS_RVALUE (*parm)
18684 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18685 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18686 && TREE_CODE (*arg) == REFERENCE_TYPE
18687 && !TYPE_REF_IS_RVALUE (*arg))
18688 *parm = TREE_TYPE (*parm);
18689 /* Nothing else to do in this case. */
18690 return 0;
18692 default:
18693 gcc_unreachable ();
18696 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18698 /* [temp.deduct.call]
18700 If P is not a reference type:
18702 --If A is an array type, the pointer type produced by the
18703 array-to-pointer standard conversion (_conv.array_) is
18704 used in place of A for type deduction; otherwise,
18706 --If A is a function type, the pointer type produced by
18707 the function-to-pointer standard conversion
18708 (_conv.func_) is used in place of A for type deduction;
18709 otherwise,
18711 --If A is a cv-qualified type, the top level
18712 cv-qualifiers of A's type are ignored for type
18713 deduction. */
18714 if (TREE_CODE (*arg) == ARRAY_TYPE)
18715 *arg = build_pointer_type (TREE_TYPE (*arg));
18716 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18717 *arg = build_pointer_type (*arg);
18718 else
18719 *arg = TYPE_MAIN_VARIANT (*arg);
18722 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18723 reference to a cv-unqualified template parameter that does not represent a
18724 template parameter of a class template (during class template argument
18725 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18726 an lvalue, the type "lvalue reference to A" is used in place of A for type
18727 deduction. */
18728 if (TREE_CODE (*parm) == REFERENCE_TYPE
18729 && TYPE_REF_IS_RVALUE (*parm)
18730 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18731 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18732 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18733 && (arg_expr ? lvalue_p (arg_expr)
18734 /* try_one_overload doesn't provide an arg_expr, but
18735 functions are always lvalues. */
18736 : TREE_CODE (*arg) == FUNCTION_TYPE))
18737 *arg = build_reference_type (*arg);
18739 /* [temp.deduct.call]
18741 If P is a cv-qualified type, the top level cv-qualifiers
18742 of P's type are ignored for type deduction. If P is a
18743 reference type, the type referred to by P is used for
18744 type deduction. */
18745 *parm = TYPE_MAIN_VARIANT (*parm);
18746 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18748 *parm = TREE_TYPE (*parm);
18749 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18752 /* DR 322. For conversion deduction, remove a reference type on parm
18753 too (which has been swapped into ARG). */
18754 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18755 *arg = TREE_TYPE (*arg);
18757 return result;
18760 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18761 template which does contain any deducible template parameters; check if
18762 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18763 unify_one_argument. */
18765 static int
18766 check_non_deducible_conversion (tree parm, tree arg, int strict,
18767 int flags, bool explain_p)
18769 tree type;
18771 if (!TYPE_P (arg))
18772 type = TREE_TYPE (arg);
18773 else
18774 type = arg;
18776 if (same_type_p (parm, type))
18777 return unify_success (explain_p);
18779 if (strict == DEDUCE_CONV)
18781 if (can_convert_arg (type, parm, NULL_TREE, flags,
18782 explain_p ? tf_warning_or_error : tf_none))
18783 return unify_success (explain_p);
18785 else if (strict != DEDUCE_EXACT)
18787 if (can_convert_arg (parm, type,
18788 TYPE_P (arg) ? NULL_TREE : arg,
18789 flags, explain_p ? tf_warning_or_error : tf_none))
18790 return unify_success (explain_p);
18793 if (strict == DEDUCE_EXACT)
18794 return unify_type_mismatch (explain_p, parm, arg);
18795 else
18796 return unify_arg_conversion (explain_p, parm, type, arg);
18799 static bool uses_deducible_template_parms (tree type);
18801 /* Returns true iff the expression EXPR is one from which a template
18802 argument can be deduced. In other words, if it's an undecorated
18803 use of a template non-type parameter. */
18805 static bool
18806 deducible_expression (tree expr)
18808 /* Strip implicit conversions. */
18809 while (CONVERT_EXPR_P (expr))
18810 expr = TREE_OPERAND (expr, 0);
18811 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18814 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18815 deducible way; that is, if it has a max value of <PARM> - 1. */
18817 static bool
18818 deducible_array_bound (tree domain)
18820 if (domain == NULL_TREE)
18821 return false;
18823 tree max = TYPE_MAX_VALUE (domain);
18824 if (TREE_CODE (max) != MINUS_EXPR)
18825 return false;
18827 return deducible_expression (TREE_OPERAND (max, 0));
18830 /* Returns true iff the template arguments ARGS use a template parameter
18831 in a deducible way. */
18833 static bool
18834 deducible_template_args (tree args)
18836 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18838 bool deducible;
18839 tree elt = TREE_VEC_ELT (args, i);
18840 if (ARGUMENT_PACK_P (elt))
18841 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18842 else
18844 if (PACK_EXPANSION_P (elt))
18845 elt = PACK_EXPANSION_PATTERN (elt);
18846 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18847 deducible = true;
18848 else if (TYPE_P (elt))
18849 deducible = uses_deducible_template_parms (elt);
18850 else
18851 deducible = deducible_expression (elt);
18853 if (deducible)
18854 return true;
18856 return false;
18859 /* Returns true iff TYPE contains any deducible references to template
18860 parameters, as per 14.8.2.5. */
18862 static bool
18863 uses_deducible_template_parms (tree type)
18865 if (PACK_EXPANSION_P (type))
18866 type = PACK_EXPANSION_PATTERN (type);
18868 /* T
18869 cv-list T
18870 TT<T>
18871 TT<i>
18872 TT<> */
18873 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18874 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18875 return true;
18877 /* T*
18879 T&& */
18880 if (POINTER_TYPE_P (type))
18881 return uses_deducible_template_parms (TREE_TYPE (type));
18883 /* T[integer-constant ]
18884 type [i] */
18885 if (TREE_CODE (type) == ARRAY_TYPE)
18886 return (uses_deducible_template_parms (TREE_TYPE (type))
18887 || deducible_array_bound (TYPE_DOMAIN (type)));
18889 /* T type ::*
18890 type T::*
18891 T T::*
18892 T (type ::*)()
18893 type (T::*)()
18894 type (type ::*)(T)
18895 type (T::*)(T)
18896 T (type ::*)(T)
18897 T (T::*)()
18898 T (T::*)(T) */
18899 if (TYPE_PTRMEM_P (type))
18900 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18901 || (uses_deducible_template_parms
18902 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18904 /* template-name <T> (where template-name refers to a class template)
18905 template-name <i> (where template-name refers to a class template) */
18906 if (CLASS_TYPE_P (type)
18907 && CLASSTYPE_TEMPLATE_INFO (type)
18908 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18909 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18910 (CLASSTYPE_TI_ARGS (type)));
18912 /* type (T)
18914 T(T) */
18915 if (TREE_CODE (type) == FUNCTION_TYPE
18916 || TREE_CODE (type) == METHOD_TYPE)
18918 if (uses_deducible_template_parms (TREE_TYPE (type)))
18919 return true;
18920 tree parm = TYPE_ARG_TYPES (type);
18921 if (TREE_CODE (type) == METHOD_TYPE)
18922 parm = TREE_CHAIN (parm);
18923 for (; parm; parm = TREE_CHAIN (parm))
18924 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18925 return true;
18928 return false;
18931 /* Subroutine of type_unification_real and unify_pack_expansion to
18932 handle unification of a single P/A pair. Parameters are as
18933 for those functions. */
18935 static int
18936 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18937 int subr, unification_kind_t strict,
18938 bool explain_p)
18940 tree arg_expr = NULL_TREE;
18941 int arg_strict;
18943 if (arg == error_mark_node || parm == error_mark_node)
18944 return unify_invalid (explain_p);
18945 if (arg == unknown_type_node)
18946 /* We can't deduce anything from this, but we might get all the
18947 template args from other function args. */
18948 return unify_success (explain_p);
18950 /* Implicit conversions (Clause 4) will be performed on a function
18951 argument to convert it to the type of the corresponding function
18952 parameter if the parameter type contains no template-parameters that
18953 participate in template argument deduction. */
18954 if (strict != DEDUCE_EXACT
18955 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18956 /* For function parameters with no deducible template parameters,
18957 just return. We'll check non-dependent conversions later. */
18958 return unify_success (explain_p);
18960 switch (strict)
18962 case DEDUCE_CALL:
18963 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18964 | UNIFY_ALLOW_MORE_CV_QUAL
18965 | UNIFY_ALLOW_DERIVED);
18966 break;
18968 case DEDUCE_CONV:
18969 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18970 break;
18972 case DEDUCE_EXACT:
18973 arg_strict = UNIFY_ALLOW_NONE;
18974 break;
18976 default:
18977 gcc_unreachable ();
18980 /* We only do these transformations if this is the top-level
18981 parameter_type_list in a call or declaration matching; in other
18982 situations (nested function declarators, template argument lists) we
18983 won't be comparing a type to an expression, and we don't do any type
18984 adjustments. */
18985 if (!subr)
18987 if (!TYPE_P (arg))
18989 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18990 if (type_unknown_p (arg))
18992 /* [temp.deduct.type] A template-argument can be
18993 deduced from a pointer to function or pointer
18994 to member function argument if the set of
18995 overloaded functions does not contain function
18996 templates and at most one of a set of
18997 overloaded functions provides a unique
18998 match. */
19000 if (resolve_overloaded_unification
19001 (tparms, targs, parm, arg, strict,
19002 arg_strict, explain_p))
19003 return unify_success (explain_p);
19004 return unify_overload_resolution_failure (explain_p, arg);
19007 arg_expr = arg;
19008 arg = unlowered_expr_type (arg);
19009 if (arg == error_mark_node)
19010 return unify_invalid (explain_p);
19013 arg_strict |=
19014 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19016 else
19017 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19018 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19019 return unify_template_argument_mismatch (explain_p, parm, arg);
19021 /* For deduction from an init-list we need the actual list. */
19022 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19023 arg = arg_expr;
19024 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19027 /* for_each_template_parm callback that always returns 0. */
19029 static int
19030 zero_r (tree, void *)
19032 return 0;
19035 /* for_each_template_parm any_fn callback to handle deduction of a template
19036 type argument from the type of an array bound. */
19038 static int
19039 array_deduction_r (tree t, void *data)
19041 tree_pair_p d = (tree_pair_p)data;
19042 tree &tparms = d->purpose;
19043 tree &targs = d->value;
19045 if (TREE_CODE (t) == ARRAY_TYPE)
19046 if (tree dom = TYPE_DOMAIN (t))
19047 if (tree max = TYPE_MAX_VALUE (dom))
19049 if (TREE_CODE (max) == MINUS_EXPR)
19050 max = TREE_OPERAND (max, 0);
19051 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19052 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19053 UNIFY_ALLOW_NONE, /*explain*/false);
19056 /* Keep walking. */
19057 return 0;
19060 /* Try to deduce any not-yet-deduced template type arguments from the type of
19061 an array bound. This is handled separately from unify because 14.8.2.5 says
19062 "The type of a type parameter is only deduced from an array bound if it is
19063 not otherwise deduced." */
19065 static void
19066 try_array_deduction (tree tparms, tree targs, tree parm)
19068 tree_pair_s data = { tparms, targs };
19069 hash_set<tree> visited;
19070 for_each_template_parm (parm, zero_r, &data, &visited,
19071 /*nondeduced*/false, array_deduction_r);
19074 /* Most parms like fn_type_unification.
19076 If SUBR is 1, we're being called recursively (to unify the
19077 arguments of a function or method parameter of a function
19078 template).
19080 CHECKS is a pointer to a vector of access checks encountered while
19081 substituting default template arguments. */
19083 static int
19084 type_unification_real (tree tparms,
19085 tree full_targs,
19086 tree xparms,
19087 const tree *xargs,
19088 unsigned int xnargs,
19089 int subr,
19090 unification_kind_t strict,
19091 int flags,
19092 vec<deferred_access_check, va_gc> **checks,
19093 bool explain_p)
19095 tree parm, arg;
19096 int i;
19097 int ntparms = TREE_VEC_LENGTH (tparms);
19098 int saw_undeduced = 0;
19099 tree parms;
19100 const tree *args;
19101 unsigned int nargs;
19102 unsigned int ia;
19104 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19105 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19106 gcc_assert (ntparms > 0);
19108 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19110 /* Reset the number of non-defaulted template arguments contained
19111 in TARGS. */
19112 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19114 again:
19115 parms = xparms;
19116 args = xargs;
19117 nargs = xnargs;
19119 ia = 0;
19120 while (parms && parms != void_list_node
19121 && ia < nargs)
19123 parm = TREE_VALUE (parms);
19125 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19126 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19127 /* For a function parameter pack that occurs at the end of the
19128 parameter-declaration-list, the type A of each remaining
19129 argument of the call is compared with the type P of the
19130 declarator-id of the function parameter pack. */
19131 break;
19133 parms = TREE_CHAIN (parms);
19135 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19136 /* For a function parameter pack that does not occur at the
19137 end of the parameter-declaration-list, the type of the
19138 parameter pack is a non-deduced context. */
19139 continue;
19141 arg = args[ia];
19142 ++ia;
19144 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19145 explain_p))
19146 return 1;
19149 if (parms
19150 && parms != void_list_node
19151 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19153 /* Unify the remaining arguments with the pack expansion type. */
19154 tree argvec;
19155 tree parmvec = make_tree_vec (1);
19157 /* Allocate a TREE_VEC and copy in all of the arguments */
19158 argvec = make_tree_vec (nargs - ia);
19159 for (i = 0; ia < nargs; ++ia, ++i)
19160 TREE_VEC_ELT (argvec, i) = args[ia];
19162 /* Copy the parameter into parmvec. */
19163 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19164 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19165 /*subr=*/subr, explain_p))
19166 return 1;
19168 /* Advance to the end of the list of parameters. */
19169 parms = TREE_CHAIN (parms);
19172 /* Fail if we've reached the end of the parm list, and more args
19173 are present, and the parm list isn't variadic. */
19174 if (ia < nargs && parms == void_list_node)
19175 return unify_too_many_arguments (explain_p, nargs, ia);
19176 /* Fail if parms are left and they don't have default values and
19177 they aren't all deduced as empty packs (c++/57397). This is
19178 consistent with sufficient_parms_p. */
19179 if (parms && parms != void_list_node
19180 && TREE_PURPOSE (parms) == NULL_TREE)
19182 unsigned int count = nargs;
19183 tree p = parms;
19184 bool type_pack_p;
19187 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19188 if (!type_pack_p)
19189 count++;
19190 p = TREE_CHAIN (p);
19192 while (p && p != void_list_node);
19193 if (count != nargs)
19194 return unify_too_few_arguments (explain_p, ia, count,
19195 type_pack_p);
19198 if (!subr)
19200 tsubst_flags_t complain = (explain_p
19201 ? tf_warning_or_error
19202 : tf_none);
19203 bool tried_array_deduction = (cxx_dialect < cxx1z);
19205 for (i = 0; i < ntparms; i++)
19207 tree targ = TREE_VEC_ELT (targs, i);
19208 tree tparm = TREE_VEC_ELT (tparms, i);
19210 /* Clear the "incomplete" flags on all argument packs now so that
19211 substituting them into later default arguments works. */
19212 if (targ && ARGUMENT_PACK_P (targ))
19214 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19215 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19218 if (targ || tparm == error_mark_node)
19219 continue;
19220 tparm = TREE_VALUE (tparm);
19222 if (TREE_CODE (tparm) == TYPE_DECL
19223 && !tried_array_deduction)
19225 try_array_deduction (tparms, targs, xparms);
19226 tried_array_deduction = true;
19227 if (TREE_VEC_ELT (targs, i))
19228 continue;
19231 /* If this is an undeduced nontype parameter that depends on
19232 a type parameter, try another pass; its type may have been
19233 deduced from a later argument than the one from which
19234 this parameter can be deduced. */
19235 if (TREE_CODE (tparm) == PARM_DECL
19236 && uses_template_parms (TREE_TYPE (tparm))
19237 && saw_undeduced < 2)
19239 saw_undeduced = 1;
19240 continue;
19243 /* Core issue #226 (C++0x) [temp.deduct]:
19245 If a template argument has not been deduced, its
19246 default template argument, if any, is used.
19248 When we are in C++98 mode, TREE_PURPOSE will either
19249 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19250 to explicitly check cxx_dialect here. */
19251 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19252 /* OK, there is a default argument. Wait until after the
19253 conversion check to do substitution. */
19254 continue;
19256 /* If the type parameter is a parameter pack, then it will
19257 be deduced to an empty parameter pack. */
19258 if (template_parameter_pack_p (tparm))
19260 tree arg;
19262 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19264 arg = make_node (NONTYPE_ARGUMENT_PACK);
19265 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
19266 TREE_CONSTANT (arg) = 1;
19268 else
19269 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19271 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19273 TREE_VEC_ELT (targs, i) = arg;
19274 continue;
19277 return unify_parameter_deduction_failure (explain_p, tparm);
19280 /* DR 1391: All parameters have args, now check non-dependent parms for
19281 convertibility. */
19282 if (saw_undeduced < 2)
19283 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19284 parms && parms != void_list_node && ia < nargs; )
19286 parm = TREE_VALUE (parms);
19288 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19289 && (!TREE_CHAIN (parms)
19290 || TREE_CHAIN (parms) == void_list_node))
19291 /* For a function parameter pack that occurs at the end of the
19292 parameter-declaration-list, the type A of each remaining
19293 argument of the call is compared with the type P of the
19294 declarator-id of the function parameter pack. */
19295 break;
19297 parms = TREE_CHAIN (parms);
19299 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19300 /* For a function parameter pack that does not occur at the
19301 end of the parameter-declaration-list, the type of the
19302 parameter pack is a non-deduced context. */
19303 continue;
19305 arg = args[ia];
19306 ++ia;
19308 if (uses_template_parms (parm))
19309 continue;
19310 if (check_non_deducible_conversion (parm, arg, strict, flags,
19311 explain_p))
19312 return 1;
19315 /* Now substitute into the default template arguments. */
19316 for (i = 0; i < ntparms; i++)
19318 tree targ = TREE_VEC_ELT (targs, i);
19319 tree tparm = TREE_VEC_ELT (tparms, i);
19321 if (targ || tparm == error_mark_node)
19322 continue;
19323 tree parm = TREE_VALUE (tparm);
19325 if (TREE_CODE (parm) == PARM_DECL
19326 && uses_template_parms (TREE_TYPE (parm))
19327 && saw_undeduced < 2)
19328 continue;
19330 tree arg = TREE_PURPOSE (tparm);
19331 reopen_deferring_access_checks (*checks);
19332 location_t save_loc = input_location;
19333 if (DECL_P (parm))
19334 input_location = DECL_SOURCE_LOCATION (parm);
19335 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19336 if (!uses_template_parms (arg))
19337 arg = convert_template_argument (parm, arg, full_targs, complain,
19338 i, NULL_TREE);
19339 else if (saw_undeduced < 2)
19340 arg = NULL_TREE;
19341 else
19342 arg = error_mark_node;
19343 input_location = save_loc;
19344 *checks = get_deferred_access_checks ();
19345 pop_deferring_access_checks ();
19346 if (arg == error_mark_node)
19347 return 1;
19348 else if (arg)
19350 TREE_VEC_ELT (targs, i) = arg;
19351 /* The position of the first default template argument,
19352 is also the number of non-defaulted arguments in TARGS.
19353 Record that. */
19354 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19355 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19359 if (saw_undeduced++ == 1)
19360 goto again;
19363 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19364 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19366 return unify_success (explain_p);
19369 /* Subroutine of type_unification_real. Args are like the variables
19370 at the call site. ARG is an overloaded function (or template-id);
19371 we try deducing template args from each of the overloads, and if
19372 only one succeeds, we go with that. Modifies TARGS and returns
19373 true on success. */
19375 static bool
19376 resolve_overloaded_unification (tree tparms,
19377 tree targs,
19378 tree parm,
19379 tree arg,
19380 unification_kind_t strict,
19381 int sub_strict,
19382 bool explain_p)
19384 tree tempargs = copy_node (targs);
19385 int good = 0;
19386 tree goodfn = NULL_TREE;
19387 bool addr_p;
19389 if (TREE_CODE (arg) == ADDR_EXPR)
19391 arg = TREE_OPERAND (arg, 0);
19392 addr_p = true;
19394 else
19395 addr_p = false;
19397 if (TREE_CODE (arg) == COMPONENT_REF)
19398 /* Handle `&x' where `x' is some static or non-static member
19399 function name. */
19400 arg = TREE_OPERAND (arg, 1);
19402 if (TREE_CODE (arg) == OFFSET_REF)
19403 arg = TREE_OPERAND (arg, 1);
19405 /* Strip baselink information. */
19406 if (BASELINK_P (arg))
19407 arg = BASELINK_FUNCTIONS (arg);
19409 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19411 /* If we got some explicit template args, we need to plug them into
19412 the affected templates before we try to unify, in case the
19413 explicit args will completely resolve the templates in question. */
19415 int ok = 0;
19416 tree expl_subargs = TREE_OPERAND (arg, 1);
19417 arg = TREE_OPERAND (arg, 0);
19419 for (; arg; arg = OVL_NEXT (arg))
19421 tree fn = OVL_CURRENT (arg);
19422 tree subargs, elem;
19424 if (TREE_CODE (fn) != TEMPLATE_DECL)
19425 continue;
19427 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19428 expl_subargs, NULL_TREE, tf_none,
19429 /*require_all_args=*/true,
19430 /*use_default_args=*/true);
19431 if (subargs != error_mark_node
19432 && !any_dependent_template_arguments_p (subargs))
19434 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19435 if (try_one_overload (tparms, targs, tempargs, parm,
19436 elem, strict, sub_strict, addr_p, explain_p)
19437 && (!goodfn || !same_type_p (goodfn, elem)))
19439 goodfn = elem;
19440 ++good;
19443 else if (subargs)
19444 ++ok;
19446 /* If no templates (or more than one) are fully resolved by the
19447 explicit arguments, this template-id is a non-deduced context; it
19448 could still be OK if we deduce all template arguments for the
19449 enclosing call through other arguments. */
19450 if (good != 1)
19451 good = ok;
19453 else if (TREE_CODE (arg) != OVERLOAD
19454 && TREE_CODE (arg) != FUNCTION_DECL)
19455 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19456 -- but the deduction does not succeed because the expression is
19457 not just the function on its own. */
19458 return false;
19459 else
19460 for (; arg; arg = OVL_NEXT (arg))
19461 if (try_one_overload (tparms, targs, tempargs, parm,
19462 TREE_TYPE (OVL_CURRENT (arg)),
19463 strict, sub_strict, addr_p, explain_p)
19464 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
19466 goodfn = OVL_CURRENT (arg);
19467 ++good;
19470 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19471 to function or pointer to member function argument if the set of
19472 overloaded functions does not contain function templates and at most
19473 one of a set of overloaded functions provides a unique match.
19475 So if we found multiple possibilities, we return success but don't
19476 deduce anything. */
19478 if (good == 1)
19480 int i = TREE_VEC_LENGTH (targs);
19481 for (; i--; )
19482 if (TREE_VEC_ELT (tempargs, i))
19484 tree old = TREE_VEC_ELT (targs, i);
19485 tree new_ = TREE_VEC_ELT (tempargs, i);
19486 if (new_ && old && ARGUMENT_PACK_P (old)
19487 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19488 /* Don't forget explicit template arguments in a pack. */
19489 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19490 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19491 TREE_VEC_ELT (targs, i) = new_;
19494 if (good)
19495 return true;
19497 return false;
19500 /* Core DR 115: In contexts where deduction is done and fails, or in
19501 contexts where deduction is not done, if a template argument list is
19502 specified and it, along with any default template arguments, identifies
19503 a single function template specialization, then the template-id is an
19504 lvalue for the function template specialization. */
19506 tree
19507 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19509 tree expr, offset, baselink;
19510 bool addr;
19512 if (!type_unknown_p (orig_expr))
19513 return orig_expr;
19515 expr = orig_expr;
19516 addr = false;
19517 offset = NULL_TREE;
19518 baselink = NULL_TREE;
19520 if (TREE_CODE (expr) == ADDR_EXPR)
19522 expr = TREE_OPERAND (expr, 0);
19523 addr = true;
19525 if (TREE_CODE (expr) == OFFSET_REF)
19527 offset = expr;
19528 expr = TREE_OPERAND (expr, 1);
19530 if (BASELINK_P (expr))
19532 baselink = expr;
19533 expr = BASELINK_FUNCTIONS (expr);
19536 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19538 int good = 0;
19539 tree goodfn = NULL_TREE;
19541 /* If we got some explicit template args, we need to plug them into
19542 the affected templates before we try to unify, in case the
19543 explicit args will completely resolve the templates in question. */
19545 tree expl_subargs = TREE_OPERAND (expr, 1);
19546 tree arg = TREE_OPERAND (expr, 0);
19547 tree badfn = NULL_TREE;
19548 tree badargs = NULL_TREE;
19550 for (; arg; arg = OVL_NEXT (arg))
19552 tree fn = OVL_CURRENT (arg);
19553 tree subargs, elem;
19555 if (TREE_CODE (fn) != TEMPLATE_DECL)
19556 continue;
19558 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19559 expl_subargs, NULL_TREE, tf_none,
19560 /*require_all_args=*/true,
19561 /*use_default_args=*/true);
19562 if (subargs != error_mark_node
19563 && !any_dependent_template_arguments_p (subargs))
19565 elem = instantiate_template (fn, subargs, tf_none);
19566 if (elem == error_mark_node)
19568 badfn = fn;
19569 badargs = subargs;
19571 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19573 goodfn = elem;
19574 ++good;
19578 if (good == 1)
19580 mark_used (goodfn);
19581 expr = goodfn;
19582 if (baselink)
19583 expr = build_baselink (BASELINK_BINFO (baselink),
19584 BASELINK_ACCESS_BINFO (baselink),
19585 expr, BASELINK_OPTYPE (baselink));
19586 if (offset)
19588 tree base
19589 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19590 expr = build_offset_ref (base, expr, addr, complain);
19592 if (addr)
19593 expr = cp_build_addr_expr (expr, complain);
19594 return expr;
19596 else if (good == 0 && badargs && (complain & tf_error))
19597 /* There were no good options and at least one bad one, so let the
19598 user know what the problem is. */
19599 instantiate_template (badfn, badargs, complain);
19601 return orig_expr;
19604 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19605 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19606 different overloads deduce different arguments for a given parm.
19607 ADDR_P is true if the expression for which deduction is being
19608 performed was of the form "& fn" rather than simply "fn".
19610 Returns 1 on success. */
19612 static int
19613 try_one_overload (tree tparms,
19614 tree orig_targs,
19615 tree targs,
19616 tree parm,
19617 tree arg,
19618 unification_kind_t strict,
19619 int sub_strict,
19620 bool addr_p,
19621 bool explain_p)
19623 int nargs;
19624 tree tempargs;
19625 int i;
19627 if (arg == error_mark_node)
19628 return 0;
19630 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19631 to function or pointer to member function argument if the set of
19632 overloaded functions does not contain function templates and at most
19633 one of a set of overloaded functions provides a unique match.
19635 So if this is a template, just return success. */
19637 if (uses_template_parms (arg))
19638 return 1;
19640 if (TREE_CODE (arg) == METHOD_TYPE)
19641 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19642 else if (addr_p)
19643 arg = build_pointer_type (arg);
19645 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19647 /* We don't copy orig_targs for this because if we have already deduced
19648 some template args from previous args, unify would complain when we
19649 try to deduce a template parameter for the same argument, even though
19650 there isn't really a conflict. */
19651 nargs = TREE_VEC_LENGTH (targs);
19652 tempargs = make_tree_vec (nargs);
19654 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19655 return 0;
19657 /* First make sure we didn't deduce anything that conflicts with
19658 explicitly specified args. */
19659 for (i = nargs; i--; )
19661 tree elt = TREE_VEC_ELT (tempargs, i);
19662 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19664 if (!elt)
19665 /*NOP*/;
19666 else if (uses_template_parms (elt))
19667 /* Since we're unifying against ourselves, we will fill in
19668 template args used in the function parm list with our own
19669 template parms. Discard them. */
19670 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19671 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19673 /* Check that the argument at each index of the deduced argument pack
19674 is equivalent to the corresponding explicitly specified argument.
19675 We may have deduced more arguments than were explicitly specified,
19676 and that's OK. */
19677 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt));
19678 gcc_assert (ARGUMENT_PACK_ARGS (oldelt)
19679 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt));
19681 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19682 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19684 if (TREE_VEC_LENGTH (deduced_pack)
19685 < TREE_VEC_LENGTH (explicit_pack))
19686 return 0;
19688 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19689 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19690 TREE_VEC_ELT (deduced_pack, j)))
19691 return 0;
19693 else if (oldelt && !template_args_equal (oldelt, elt))
19694 return 0;
19697 for (i = nargs; i--; )
19699 tree elt = TREE_VEC_ELT (tempargs, i);
19701 if (elt)
19702 TREE_VEC_ELT (targs, i) = elt;
19705 return 1;
19708 /* PARM is a template class (perhaps with unbound template
19709 parameters). ARG is a fully instantiated type. If ARG can be
19710 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19711 TARGS are as for unify. */
19713 static tree
19714 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19715 bool explain_p)
19717 tree copy_of_targs;
19719 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19720 return NULL_TREE;
19721 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19722 /* Matches anything. */;
19723 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19724 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19725 return NULL_TREE;
19727 /* We need to make a new template argument vector for the call to
19728 unify. If we used TARGS, we'd clutter it up with the result of
19729 the attempted unification, even if this class didn't work out.
19730 We also don't want to commit ourselves to all the unifications
19731 we've already done, since unification is supposed to be done on
19732 an argument-by-argument basis. In other words, consider the
19733 following pathological case:
19735 template <int I, int J, int K>
19736 struct S {};
19738 template <int I, int J>
19739 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19741 template <int I, int J, int K>
19742 void f(S<I, J, K>, S<I, I, I>);
19744 void g() {
19745 S<0, 0, 0> s0;
19746 S<0, 1, 2> s2;
19748 f(s0, s2);
19751 Now, by the time we consider the unification involving `s2', we
19752 already know that we must have `f<0, 0, 0>'. But, even though
19753 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19754 because there are two ways to unify base classes of S<0, 1, 2>
19755 with S<I, I, I>. If we kept the already deduced knowledge, we
19756 would reject the possibility I=1. */
19757 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19759 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19761 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19762 return NULL_TREE;
19763 return arg;
19766 /* If unification failed, we're done. */
19767 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19768 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19769 return NULL_TREE;
19771 return arg;
19774 /* Given a template type PARM and a class type ARG, find the unique
19775 base type in ARG that is an instance of PARM. We do not examine
19776 ARG itself; only its base-classes. If there is not exactly one
19777 appropriate base class, return NULL_TREE. PARM may be the type of
19778 a partial specialization, as well as a plain template type. Used
19779 by unify. */
19781 static enum template_base_result
19782 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19783 bool explain_p, tree *result)
19785 tree rval = NULL_TREE;
19786 tree binfo;
19788 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19790 binfo = TYPE_BINFO (complete_type (arg));
19791 if (!binfo)
19793 /* The type could not be completed. */
19794 *result = NULL_TREE;
19795 return tbr_incomplete_type;
19798 /* Walk in inheritance graph order. The search order is not
19799 important, and this avoids multiple walks of virtual bases. */
19800 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19802 tree r = try_class_unification (tparms, targs, parm,
19803 BINFO_TYPE (binfo), explain_p);
19805 if (r)
19807 /* If there is more than one satisfactory baseclass, then:
19809 [temp.deduct.call]
19811 If they yield more than one possible deduced A, the type
19812 deduction fails.
19814 applies. */
19815 if (rval && !same_type_p (r, rval))
19817 *result = NULL_TREE;
19818 return tbr_ambiguous_baseclass;
19821 rval = r;
19825 *result = rval;
19826 return tbr_success;
19829 /* Returns the level of DECL, which declares a template parameter. */
19831 static int
19832 template_decl_level (tree decl)
19834 switch (TREE_CODE (decl))
19836 case TYPE_DECL:
19837 case TEMPLATE_DECL:
19838 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19840 case PARM_DECL:
19841 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19843 default:
19844 gcc_unreachable ();
19846 return 0;
19849 /* Decide whether ARG can be unified with PARM, considering only the
19850 cv-qualifiers of each type, given STRICT as documented for unify.
19851 Returns nonzero iff the unification is OK on that basis. */
19853 static int
19854 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19856 int arg_quals = cp_type_quals (arg);
19857 int parm_quals = cp_type_quals (parm);
19859 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19860 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19862 /* Although a CVR qualifier is ignored when being applied to a
19863 substituted template parameter ([8.3.2]/1 for example), that
19864 does not allow us to unify "const T" with "int&" because both
19865 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19866 It is ok when we're allowing additional CV qualifiers
19867 at the outer level [14.8.2.1]/3,1st bullet. */
19868 if ((TREE_CODE (arg) == REFERENCE_TYPE
19869 || TREE_CODE (arg) == FUNCTION_TYPE
19870 || TREE_CODE (arg) == METHOD_TYPE)
19871 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19872 return 0;
19874 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19875 && (parm_quals & TYPE_QUAL_RESTRICT))
19876 return 0;
19879 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19880 && (arg_quals & parm_quals) != parm_quals)
19881 return 0;
19883 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19884 && (parm_quals & arg_quals) != arg_quals)
19885 return 0;
19887 return 1;
19890 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19891 void
19892 template_parm_level_and_index (tree parm, int* level, int* index)
19894 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19895 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19896 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19898 *index = TEMPLATE_TYPE_IDX (parm);
19899 *level = TEMPLATE_TYPE_LEVEL (parm);
19901 else
19903 *index = TEMPLATE_PARM_IDX (parm);
19904 *level = TEMPLATE_PARM_LEVEL (parm);
19908 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19909 do { \
19910 if (unify (TP, TA, P, A, S, EP)) \
19911 return 1; \
19912 } while (0)
19914 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19915 expansion at the end of PACKED_PARMS. Returns 0 if the type
19916 deduction succeeds, 1 otherwise. STRICT is the same as in
19917 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19918 function call argument list. We'll need to adjust the arguments to make them
19919 types. SUBR tells us if this is from a recursive call to
19920 type_unification_real, or for comparing two template argument
19921 lists. */
19923 static int
19924 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19925 tree packed_args, unification_kind_t strict,
19926 bool subr, bool explain_p)
19928 tree parm
19929 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19930 tree pattern = PACK_EXPANSION_PATTERN (parm);
19931 tree pack, packs = NULL_TREE;
19932 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19934 packed_args = expand_template_argument_pack (packed_args);
19936 int len = TREE_VEC_LENGTH (packed_args);
19938 /* Determine the parameter packs we will be deducing from the
19939 pattern, and record their current deductions. */
19940 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19941 pack; pack = TREE_CHAIN (pack))
19943 tree parm_pack = TREE_VALUE (pack);
19944 int idx, level;
19946 /* Determine the index and level of this parameter pack. */
19947 template_parm_level_and_index (parm_pack, &level, &idx);
19949 /* Keep track of the parameter packs and their corresponding
19950 argument packs. */
19951 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19952 TREE_TYPE (packs) = make_tree_vec (len - start);
19955 /* Loop through all of the arguments that have not yet been
19956 unified and unify each with the pattern. */
19957 for (i = start; i < len; i++)
19959 tree parm;
19960 bool any_explicit = false;
19961 tree arg = TREE_VEC_ELT (packed_args, i);
19963 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19964 or the element of its argument pack at the current index if
19965 this argument was explicitly specified. */
19966 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19968 int idx, level;
19969 tree arg, pargs;
19970 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19972 arg = NULL_TREE;
19973 if (TREE_VALUE (pack)
19974 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19975 && (i - start < TREE_VEC_LENGTH (pargs)))
19977 any_explicit = true;
19978 arg = TREE_VEC_ELT (pargs, i - start);
19980 TMPL_ARG (targs, level, idx) = arg;
19983 /* If we had explicit template arguments, substitute them into the
19984 pattern before deduction. */
19985 if (any_explicit)
19987 /* Some arguments might still be unspecified or dependent. */
19988 bool dependent;
19989 ++processing_template_decl;
19990 dependent = any_dependent_template_arguments_p (targs);
19991 if (!dependent)
19992 --processing_template_decl;
19993 parm = tsubst (pattern, targs,
19994 explain_p ? tf_warning_or_error : tf_none,
19995 NULL_TREE);
19996 if (dependent)
19997 --processing_template_decl;
19998 if (parm == error_mark_node)
19999 return 1;
20001 else
20002 parm = pattern;
20004 /* Unify the pattern with the current argument. */
20005 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20006 explain_p))
20007 return 1;
20009 /* For each parameter pack, collect the deduced value. */
20010 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20012 int idx, level;
20013 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20015 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20016 TMPL_ARG (targs, level, idx);
20020 /* Verify that the results of unification with the parameter packs
20021 produce results consistent with what we've seen before, and make
20022 the deduced argument packs available. */
20023 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20025 tree old_pack = TREE_VALUE (pack);
20026 tree new_args = TREE_TYPE (pack);
20027 int i, len = TREE_VEC_LENGTH (new_args);
20028 int idx, level;
20029 bool nondeduced_p = false;
20031 /* By default keep the original deduced argument pack.
20032 If necessary, more specific code is going to update the
20033 resulting deduced argument later down in this function. */
20034 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20035 TMPL_ARG (targs, level, idx) = old_pack;
20037 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20038 actually deduce anything. */
20039 for (i = 0; i < len && !nondeduced_p; ++i)
20040 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20041 nondeduced_p = true;
20042 if (nondeduced_p)
20043 continue;
20045 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20047 /* If we had fewer function args than explicit template args,
20048 just use the explicits. */
20049 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20050 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20051 if (len < explicit_len)
20052 new_args = explicit_args;
20055 if (!old_pack)
20057 tree result;
20058 /* Build the deduced *_ARGUMENT_PACK. */
20059 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20061 result = make_node (NONTYPE_ARGUMENT_PACK);
20062 TREE_TYPE (result) =
20063 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
20064 TREE_CONSTANT (result) = 1;
20066 else
20067 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20069 SET_ARGUMENT_PACK_ARGS (result, new_args);
20071 /* Note the deduced argument packs for this parameter
20072 pack. */
20073 TMPL_ARG (targs, level, idx) = result;
20075 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20076 && (ARGUMENT_PACK_ARGS (old_pack)
20077 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20079 /* We only had the explicitly-provided arguments before, but
20080 now we have a complete set of arguments. */
20081 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20083 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20084 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20085 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20087 else
20089 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20090 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20092 if (!comp_template_args (old_args, new_args,
20093 &bad_old_arg, &bad_new_arg))
20094 /* Inconsistent unification of this parameter pack. */
20095 return unify_parameter_pack_inconsistent (explain_p,
20096 bad_old_arg,
20097 bad_new_arg);
20101 return unify_success (explain_p);
20104 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20105 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20106 parameters and return value are as for unify. */
20108 static int
20109 unify_array_domain (tree tparms, tree targs,
20110 tree parm_dom, tree arg_dom,
20111 bool explain_p)
20113 tree parm_max;
20114 tree arg_max;
20115 bool parm_cst;
20116 bool arg_cst;
20118 /* Our representation of array types uses "N - 1" as the
20119 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20120 not an integer constant. We cannot unify arbitrarily
20121 complex expressions, so we eliminate the MINUS_EXPRs
20122 here. */
20123 parm_max = TYPE_MAX_VALUE (parm_dom);
20124 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20125 if (!parm_cst)
20127 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20128 parm_max = TREE_OPERAND (parm_max, 0);
20130 arg_max = TYPE_MAX_VALUE (arg_dom);
20131 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20132 if (!arg_cst)
20134 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20135 trying to unify the type of a variable with the type
20136 of a template parameter. For example:
20138 template <unsigned int N>
20139 void f (char (&) [N]);
20140 int g();
20141 void h(int i) {
20142 char a[g(i)];
20143 f(a);
20146 Here, the type of the ARG will be "int [g(i)]", and
20147 may be a SAVE_EXPR, etc. */
20148 if (TREE_CODE (arg_max) != MINUS_EXPR)
20149 return unify_vla_arg (explain_p, arg_dom);
20150 arg_max = TREE_OPERAND (arg_max, 0);
20153 /* If only one of the bounds used a MINUS_EXPR, compensate
20154 by adding one to the other bound. */
20155 if (parm_cst && !arg_cst)
20156 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20157 integer_type_node,
20158 parm_max,
20159 integer_one_node);
20160 else if (arg_cst && !parm_cst)
20161 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20162 integer_type_node,
20163 arg_max,
20164 integer_one_node);
20166 return unify (tparms, targs, parm_max, arg_max,
20167 UNIFY_ALLOW_INTEGER, explain_p);
20170 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20172 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20174 static pa_kind_t
20175 pa_kind (tree t)
20177 if (PACK_EXPANSION_P (t))
20178 t = PACK_EXPANSION_PATTERN (t);
20179 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20180 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20181 || DECL_TYPE_TEMPLATE_P (t))
20182 return pa_tmpl;
20183 else if (TYPE_P (t))
20184 return pa_type;
20185 else
20186 return pa_expr;
20189 /* Deduce the value of template parameters. TPARMS is the (innermost)
20190 set of template parameters to a template. TARGS is the bindings
20191 for those template parameters, as determined thus far; TARGS may
20192 include template arguments for outer levels of template parameters
20193 as well. PARM is a parameter to a template function, or a
20194 subcomponent of that parameter; ARG is the corresponding argument.
20195 This function attempts to match PARM with ARG in a manner
20196 consistent with the existing assignments in TARGS. If more values
20197 are deduced, then TARGS is updated.
20199 Returns 0 if the type deduction succeeds, 1 otherwise. The
20200 parameter STRICT is a bitwise or of the following flags:
20202 UNIFY_ALLOW_NONE:
20203 Require an exact match between PARM and ARG.
20204 UNIFY_ALLOW_MORE_CV_QUAL:
20205 Allow the deduced ARG to be more cv-qualified (by qualification
20206 conversion) than ARG.
20207 UNIFY_ALLOW_LESS_CV_QUAL:
20208 Allow the deduced ARG to be less cv-qualified than ARG.
20209 UNIFY_ALLOW_DERIVED:
20210 Allow the deduced ARG to be a template base class of ARG,
20211 or a pointer to a template base class of the type pointed to by
20212 ARG.
20213 UNIFY_ALLOW_INTEGER:
20214 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20215 case for more information.
20216 UNIFY_ALLOW_OUTER_LEVEL:
20217 This is the outermost level of a deduction. Used to determine validity
20218 of qualification conversions. A valid qualification conversion must
20219 have const qualified pointers leading up to the inner type which
20220 requires additional CV quals, except at the outer level, where const
20221 is not required [conv.qual]. It would be normal to set this flag in
20222 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20223 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20224 This is the outermost level of a deduction, and PARM can be more CV
20225 qualified at this point.
20226 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20227 This is the outermost level of a deduction, and PARM can be less CV
20228 qualified at this point. */
20230 static int
20231 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20232 bool explain_p)
20234 int idx;
20235 tree targ;
20236 tree tparm;
20237 int strict_in = strict;
20238 tsubst_flags_t complain = (explain_p
20239 ? tf_warning_or_error
20240 : tf_none);
20242 /* I don't think this will do the right thing with respect to types.
20243 But the only case I've seen it in so far has been array bounds, where
20244 signedness is the only information lost, and I think that will be
20245 okay. */
20246 while (CONVERT_EXPR_P (parm))
20247 parm = TREE_OPERAND (parm, 0);
20249 if (arg == error_mark_node)
20250 return unify_invalid (explain_p);
20251 if (arg == unknown_type_node
20252 || arg == init_list_type_node)
20253 /* We can't deduce anything from this, but we might get all the
20254 template args from other function args. */
20255 return unify_success (explain_p);
20257 if (parm == any_targ_node || arg == any_targ_node)
20258 return unify_success (explain_p);
20260 /* If PARM uses template parameters, then we can't bail out here,
20261 even if ARG == PARM, since we won't record unifications for the
20262 template parameters. We might need them if we're trying to
20263 figure out which of two things is more specialized. */
20264 if (arg == parm && !uses_template_parms (parm))
20265 return unify_success (explain_p);
20267 /* Handle init lists early, so the rest of the function can assume
20268 we're dealing with a type. */
20269 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20271 tree elt, elttype;
20272 unsigned i;
20273 tree orig_parm = parm;
20275 /* Replace T with std::initializer_list<T> for deduction. */
20276 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20277 && flag_deduce_init_list)
20278 parm = listify (parm);
20280 if (!is_std_init_list (parm)
20281 && TREE_CODE (parm) != ARRAY_TYPE)
20282 /* We can only deduce from an initializer list argument if the
20283 parameter is std::initializer_list or an array; otherwise this
20284 is a non-deduced context. */
20285 return unify_success (explain_p);
20287 if (TREE_CODE (parm) == ARRAY_TYPE)
20288 elttype = TREE_TYPE (parm);
20289 else
20291 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20292 /* Deduction is defined in terms of a single type, so just punt
20293 on the (bizarre) std::initializer_list<T...>. */
20294 if (PACK_EXPANSION_P (elttype))
20295 return unify_success (explain_p);
20298 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20300 int elt_strict = strict;
20302 if (elt == error_mark_node)
20303 return unify_invalid (explain_p);
20305 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20307 tree type = TREE_TYPE (elt);
20308 if (type == error_mark_node)
20309 return unify_invalid (explain_p);
20310 /* It should only be possible to get here for a call. */
20311 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20312 elt_strict |= maybe_adjust_types_for_deduction
20313 (DEDUCE_CALL, &elttype, &type, elt);
20314 elt = type;
20317 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20318 explain_p);
20321 if (TREE_CODE (parm) == ARRAY_TYPE
20322 && deducible_array_bound (TYPE_DOMAIN (parm)))
20324 /* Also deduce from the length of the initializer list. */
20325 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20326 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20327 if (idx == error_mark_node)
20328 return unify_invalid (explain_p);
20329 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20330 idx, explain_p);
20333 /* If the std::initializer_list<T> deduction worked, replace the
20334 deduced A with std::initializer_list<A>. */
20335 if (orig_parm != parm)
20337 idx = TEMPLATE_TYPE_IDX (orig_parm);
20338 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20339 targ = listify (targ);
20340 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20342 return unify_success (explain_p);
20345 /* If parm and arg aren't the same kind of thing (template, type, or
20346 expression), fail early. */
20347 if (pa_kind (parm) != pa_kind (arg))
20348 return unify_invalid (explain_p);
20350 /* Immediately reject some pairs that won't unify because of
20351 cv-qualification mismatches. */
20352 if (TREE_CODE (arg) == TREE_CODE (parm)
20353 && TYPE_P (arg)
20354 /* It is the elements of the array which hold the cv quals of an array
20355 type, and the elements might be template type parms. We'll check
20356 when we recurse. */
20357 && TREE_CODE (arg) != ARRAY_TYPE
20358 /* We check the cv-qualifiers when unifying with template type
20359 parameters below. We want to allow ARG `const T' to unify with
20360 PARM `T' for example, when computing which of two templates
20361 is more specialized, for example. */
20362 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20363 && !check_cv_quals_for_unify (strict_in, arg, parm))
20364 return unify_cv_qual_mismatch (explain_p, parm, arg);
20366 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20367 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20368 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20369 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20370 strict &= ~UNIFY_ALLOW_DERIVED;
20371 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20372 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20374 switch (TREE_CODE (parm))
20376 case TYPENAME_TYPE:
20377 case SCOPE_REF:
20378 case UNBOUND_CLASS_TEMPLATE:
20379 /* In a type which contains a nested-name-specifier, template
20380 argument values cannot be deduced for template parameters used
20381 within the nested-name-specifier. */
20382 return unify_success (explain_p);
20384 case TEMPLATE_TYPE_PARM:
20385 case TEMPLATE_TEMPLATE_PARM:
20386 case BOUND_TEMPLATE_TEMPLATE_PARM:
20387 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20388 if (error_operand_p (tparm))
20389 return unify_invalid (explain_p);
20391 if (TEMPLATE_TYPE_LEVEL (parm)
20392 != template_decl_level (tparm))
20393 /* The PARM is not one we're trying to unify. Just check
20394 to see if it matches ARG. */
20396 if (TREE_CODE (arg) == TREE_CODE (parm)
20397 && (is_auto (parm) ? is_auto (arg)
20398 : same_type_p (parm, arg)))
20399 return unify_success (explain_p);
20400 else
20401 return unify_type_mismatch (explain_p, parm, arg);
20403 idx = TEMPLATE_TYPE_IDX (parm);
20404 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20405 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20406 if (error_operand_p (tparm))
20407 return unify_invalid (explain_p);
20409 /* Check for mixed types and values. */
20410 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20411 && TREE_CODE (tparm) != TYPE_DECL)
20412 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20413 && TREE_CODE (tparm) != TEMPLATE_DECL))
20414 gcc_unreachable ();
20416 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20418 if ((strict_in & UNIFY_ALLOW_DERIVED)
20419 && CLASS_TYPE_P (arg))
20421 /* First try to match ARG directly. */
20422 tree t = try_class_unification (tparms, targs, parm, arg,
20423 explain_p);
20424 if (!t)
20426 /* Otherwise, look for a suitable base of ARG, as below. */
20427 enum template_base_result r;
20428 r = get_template_base (tparms, targs, parm, arg,
20429 explain_p, &t);
20430 if (!t)
20431 return unify_no_common_base (explain_p, r, parm, arg);
20432 arg = t;
20435 /* ARG must be constructed from a template class or a template
20436 template parameter. */
20437 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20438 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20439 return unify_template_deduction_failure (explain_p, parm, arg);
20441 /* Deduce arguments T, i from TT<T> or TT<i>. */
20442 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20443 return 1;
20445 arg = TYPE_TI_TEMPLATE (arg);
20447 /* Fall through to deduce template name. */
20450 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20451 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20453 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20455 /* Simple cases: Value already set, does match or doesn't. */
20456 if (targ != NULL_TREE && template_args_equal (targ, arg))
20457 return unify_success (explain_p);
20458 else if (targ)
20459 return unify_inconsistency (explain_p, parm, targ, arg);
20461 else
20463 /* If PARM is `const T' and ARG is only `int', we don't have
20464 a match unless we are allowing additional qualification.
20465 If ARG is `const int' and PARM is just `T' that's OK;
20466 that binds `const int' to `T'. */
20467 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20468 arg, parm))
20469 return unify_cv_qual_mismatch (explain_p, parm, arg);
20471 /* Consider the case where ARG is `const volatile int' and
20472 PARM is `const T'. Then, T should be `volatile int'. */
20473 arg = cp_build_qualified_type_real
20474 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20475 if (arg == error_mark_node)
20476 return unify_invalid (explain_p);
20478 /* Simple cases: Value already set, does match or doesn't. */
20479 if (targ != NULL_TREE && same_type_p (targ, arg))
20480 return unify_success (explain_p);
20481 else if (targ)
20482 return unify_inconsistency (explain_p, parm, targ, arg);
20484 /* Make sure that ARG is not a variable-sized array. (Note
20485 that were talking about variable-sized arrays (like
20486 `int[n]'), rather than arrays of unknown size (like
20487 `int[]').) We'll get very confused by such a type since
20488 the bound of the array is not constant, and therefore
20489 not mangleable. Besides, such types are not allowed in
20490 ISO C++, so we can do as we please here. We do allow
20491 them for 'auto' deduction, since that isn't ABI-exposed. */
20492 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20493 return unify_vla_arg (explain_p, arg);
20495 /* Strip typedefs as in convert_template_argument. */
20496 arg = canonicalize_type_argument (arg, tf_none);
20499 /* If ARG is a parameter pack or an expansion, we cannot unify
20500 against it unless PARM is also a parameter pack. */
20501 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20502 && !template_parameter_pack_p (parm))
20503 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20505 /* If the argument deduction results is a METHOD_TYPE,
20506 then there is a problem.
20507 METHOD_TYPE doesn't map to any real C++ type the result of
20508 the deduction can not be of that type. */
20509 if (TREE_CODE (arg) == METHOD_TYPE)
20510 return unify_method_type_error (explain_p, arg);
20512 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20513 return unify_success (explain_p);
20515 case TEMPLATE_PARM_INDEX:
20516 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20517 if (error_operand_p (tparm))
20518 return unify_invalid (explain_p);
20520 if (TEMPLATE_PARM_LEVEL (parm)
20521 != template_decl_level (tparm))
20523 /* The PARM is not one we're trying to unify. Just check
20524 to see if it matches ARG. */
20525 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20526 && cp_tree_equal (parm, arg));
20527 if (result)
20528 unify_expression_unequal (explain_p, parm, arg);
20529 return result;
20532 idx = TEMPLATE_PARM_IDX (parm);
20533 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20535 if (targ)
20537 int x = !cp_tree_equal (targ, arg);
20538 if (x)
20539 unify_inconsistency (explain_p, parm, targ, arg);
20540 return x;
20543 if (cxx_dialect >= cxx1z
20544 /* We deduce from array bounds in try_array_deduction. */
20545 && !(strict & UNIFY_ALLOW_INTEGER)
20546 && uses_template_parms (TREE_TYPE (parm))
20547 && !type_uses_auto (TREE_TYPE (parm)))
20549 tree atype = TREE_TYPE (arg);
20550 RECUR_AND_CHECK_FAILURE (tparms, targs,
20551 TREE_TYPE (parm), atype,
20552 UNIFY_ALLOW_NONE, explain_p);
20555 /* [temp.deduct.type] If, in the declaration of a function template
20556 with a non-type template-parameter, the non-type
20557 template-parameter is used in an expression in the function
20558 parameter-list and, if the corresponding template-argument is
20559 deduced, the template-argument type shall match the type of the
20560 template-parameter exactly, except that a template-argument
20561 deduced from an array bound may be of any integral type.
20562 The non-type parameter might use already deduced type parameters. */
20563 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20564 if (tree a = type_uses_auto (tparm))
20566 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20567 if (tparm == error_mark_node)
20568 return 1;
20571 if (!TREE_TYPE (arg))
20572 /* Template-parameter dependent expression. Just accept it for now.
20573 It will later be processed in convert_template_argument. */
20575 else if (same_type_p (TREE_TYPE (arg), tparm))
20576 /* OK */;
20577 else if ((strict & UNIFY_ALLOW_INTEGER)
20578 && CP_INTEGRAL_TYPE_P (tparm))
20579 /* Convert the ARG to the type of PARM; the deduced non-type
20580 template argument must exactly match the types of the
20581 corresponding parameter. */
20582 arg = fold (build_nop (tparm, arg));
20583 else if (uses_template_parms (tparm))
20584 /* We haven't deduced the type of this parameter yet. Try again
20585 later. */
20586 return unify_success (explain_p);
20587 else
20588 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20590 /* If ARG is a parameter pack or an expansion, we cannot unify
20591 against it unless PARM is also a parameter pack. */
20592 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20593 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20594 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20597 bool removed_attr = false;
20598 arg = strip_typedefs_expr (arg, &removed_attr);
20600 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20601 return unify_success (explain_p);
20603 case PTRMEM_CST:
20605 /* A pointer-to-member constant can be unified only with
20606 another constant. */
20607 if (TREE_CODE (arg) != PTRMEM_CST)
20608 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20610 /* Just unify the class member. It would be useless (and possibly
20611 wrong, depending on the strict flags) to unify also
20612 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20613 arg refer to the same variable, even if through different
20614 classes. For instance:
20616 struct A { int x; };
20617 struct B : A { };
20619 Unification of &A::x and &B::x must succeed. */
20620 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20621 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20624 case POINTER_TYPE:
20626 if (!TYPE_PTR_P (arg))
20627 return unify_type_mismatch (explain_p, parm, arg);
20629 /* [temp.deduct.call]
20631 A can be another pointer or pointer to member type that can
20632 be converted to the deduced A via a qualification
20633 conversion (_conv.qual_).
20635 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20636 This will allow for additional cv-qualification of the
20637 pointed-to types if appropriate. */
20639 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20640 /* The derived-to-base conversion only persists through one
20641 level of pointers. */
20642 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20644 return unify (tparms, targs, TREE_TYPE (parm),
20645 TREE_TYPE (arg), strict, explain_p);
20648 case REFERENCE_TYPE:
20649 if (TREE_CODE (arg) != REFERENCE_TYPE)
20650 return unify_type_mismatch (explain_p, parm, arg);
20651 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20652 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20654 case ARRAY_TYPE:
20655 if (TREE_CODE (arg) != ARRAY_TYPE)
20656 return unify_type_mismatch (explain_p, parm, arg);
20657 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20658 != (TYPE_DOMAIN (arg) == NULL_TREE))
20659 return unify_type_mismatch (explain_p, parm, arg);
20660 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20661 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20662 if (TYPE_DOMAIN (parm) != NULL_TREE)
20663 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20664 TYPE_DOMAIN (arg), explain_p);
20665 return unify_success (explain_p);
20667 case REAL_TYPE:
20668 case COMPLEX_TYPE:
20669 case VECTOR_TYPE:
20670 case INTEGER_TYPE:
20671 case BOOLEAN_TYPE:
20672 case ENUMERAL_TYPE:
20673 case VOID_TYPE:
20674 case NULLPTR_TYPE:
20675 if (TREE_CODE (arg) != TREE_CODE (parm))
20676 return unify_type_mismatch (explain_p, parm, arg);
20678 /* We have already checked cv-qualification at the top of the
20679 function. */
20680 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20681 return unify_type_mismatch (explain_p, parm, arg);
20683 /* As far as unification is concerned, this wins. Later checks
20684 will invalidate it if necessary. */
20685 return unify_success (explain_p);
20687 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20688 /* Type INTEGER_CST can come from ordinary constant template args. */
20689 case INTEGER_CST:
20690 while (CONVERT_EXPR_P (arg))
20691 arg = TREE_OPERAND (arg, 0);
20693 if (TREE_CODE (arg) != INTEGER_CST)
20694 return unify_template_argument_mismatch (explain_p, parm, arg);
20695 return (tree_int_cst_equal (parm, arg)
20696 ? unify_success (explain_p)
20697 : unify_template_argument_mismatch (explain_p, parm, arg));
20699 case TREE_VEC:
20701 int i, len, argslen;
20702 int parm_variadic_p = 0;
20704 if (TREE_CODE (arg) != TREE_VEC)
20705 return unify_template_argument_mismatch (explain_p, parm, arg);
20707 len = TREE_VEC_LENGTH (parm);
20708 argslen = TREE_VEC_LENGTH (arg);
20710 /* Check for pack expansions in the parameters. */
20711 for (i = 0; i < len; ++i)
20713 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20715 if (i == len - 1)
20716 /* We can unify against something with a trailing
20717 parameter pack. */
20718 parm_variadic_p = 1;
20719 else
20720 /* [temp.deduct.type]/9: If the template argument list of
20721 P contains a pack expansion that is not the last
20722 template argument, the entire template argument list
20723 is a non-deduced context. */
20724 return unify_success (explain_p);
20728 /* If we don't have enough arguments to satisfy the parameters
20729 (not counting the pack expression at the end), or we have
20730 too many arguments for a parameter list that doesn't end in
20731 a pack expression, we can't unify. */
20732 if (parm_variadic_p
20733 ? argslen < len - parm_variadic_p
20734 : argslen != len)
20735 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20737 /* Unify all of the parameters that precede the (optional)
20738 pack expression. */
20739 for (i = 0; i < len - parm_variadic_p; ++i)
20741 RECUR_AND_CHECK_FAILURE (tparms, targs,
20742 TREE_VEC_ELT (parm, i),
20743 TREE_VEC_ELT (arg, i),
20744 UNIFY_ALLOW_NONE, explain_p);
20746 if (parm_variadic_p)
20747 return unify_pack_expansion (tparms, targs, parm, arg,
20748 DEDUCE_EXACT,
20749 /*subr=*/true, explain_p);
20750 return unify_success (explain_p);
20753 case RECORD_TYPE:
20754 case UNION_TYPE:
20755 if (TREE_CODE (arg) != TREE_CODE (parm))
20756 return unify_type_mismatch (explain_p, parm, arg);
20758 if (TYPE_PTRMEMFUNC_P (parm))
20760 if (!TYPE_PTRMEMFUNC_P (arg))
20761 return unify_type_mismatch (explain_p, parm, arg);
20763 return unify (tparms, targs,
20764 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20765 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20766 strict, explain_p);
20768 else if (TYPE_PTRMEMFUNC_P (arg))
20769 return unify_type_mismatch (explain_p, parm, arg);
20771 if (CLASSTYPE_TEMPLATE_INFO (parm))
20773 tree t = NULL_TREE;
20775 if (strict_in & UNIFY_ALLOW_DERIVED)
20777 /* First, we try to unify the PARM and ARG directly. */
20778 t = try_class_unification (tparms, targs,
20779 parm, arg, explain_p);
20781 if (!t)
20783 /* Fallback to the special case allowed in
20784 [temp.deduct.call]:
20786 If P is a class, and P has the form
20787 template-id, then A can be a derived class of
20788 the deduced A. Likewise, if P is a pointer to
20789 a class of the form template-id, A can be a
20790 pointer to a derived class pointed to by the
20791 deduced A. */
20792 enum template_base_result r;
20793 r = get_template_base (tparms, targs, parm, arg,
20794 explain_p, &t);
20796 if (!t)
20798 /* Don't give the derived diagnostic if we're
20799 already dealing with the same template. */
20800 bool same_template
20801 = (CLASSTYPE_TEMPLATE_INFO (arg)
20802 && (CLASSTYPE_TI_TEMPLATE (parm)
20803 == CLASSTYPE_TI_TEMPLATE (arg)));
20804 return unify_no_common_base (explain_p && !same_template,
20805 r, parm, arg);
20809 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20810 && (CLASSTYPE_TI_TEMPLATE (parm)
20811 == CLASSTYPE_TI_TEMPLATE (arg)))
20812 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20813 Then, we should unify `int' and `U'. */
20814 t = arg;
20815 else
20816 /* There's no chance of unification succeeding. */
20817 return unify_type_mismatch (explain_p, parm, arg);
20819 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20820 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20822 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20823 return unify_type_mismatch (explain_p, parm, arg);
20824 return unify_success (explain_p);
20826 case METHOD_TYPE:
20827 case FUNCTION_TYPE:
20829 unsigned int nargs;
20830 tree *args;
20831 tree a;
20832 unsigned int i;
20834 if (TREE_CODE (arg) != TREE_CODE (parm))
20835 return unify_type_mismatch (explain_p, parm, arg);
20837 /* CV qualifications for methods can never be deduced, they must
20838 match exactly. We need to check them explicitly here,
20839 because type_unification_real treats them as any other
20840 cv-qualified parameter. */
20841 if (TREE_CODE (parm) == METHOD_TYPE
20842 && (!check_cv_quals_for_unify
20843 (UNIFY_ALLOW_NONE,
20844 class_of_this_parm (arg),
20845 class_of_this_parm (parm))))
20846 return unify_cv_qual_mismatch (explain_p, parm, arg);
20847 if (TREE_CODE (arg) == FUNCTION_TYPE
20848 && type_memfn_quals (parm) != type_memfn_quals (arg))
20849 return unify_cv_qual_mismatch (explain_p, parm, arg);
20850 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20851 return unify_type_mismatch (explain_p, parm, arg);
20853 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20854 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20856 nargs = list_length (TYPE_ARG_TYPES (arg));
20857 args = XALLOCAVEC (tree, nargs);
20858 for (a = TYPE_ARG_TYPES (arg), i = 0;
20859 a != NULL_TREE && a != void_list_node;
20860 a = TREE_CHAIN (a), ++i)
20861 args[i] = TREE_VALUE (a);
20862 nargs = i;
20864 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20865 args, nargs, 1, DEDUCE_EXACT,
20866 LOOKUP_NORMAL, NULL, explain_p))
20867 return 1;
20869 if (flag_noexcept_type)
20871 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20872 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20873 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20874 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20875 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20876 && uses_template_parms (TREE_PURPOSE (pspec)))
20877 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20878 TREE_PURPOSE (aspec),
20879 UNIFY_ALLOW_NONE, explain_p);
20880 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20881 return unify_type_mismatch (explain_p, parm, arg);
20884 return 0;
20887 case OFFSET_TYPE:
20888 /* Unify a pointer to member with a pointer to member function, which
20889 deduces the type of the member as a function type. */
20890 if (TYPE_PTRMEMFUNC_P (arg))
20892 /* Check top-level cv qualifiers */
20893 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20894 return unify_cv_qual_mismatch (explain_p, parm, arg);
20896 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20897 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20898 UNIFY_ALLOW_NONE, explain_p);
20900 /* Determine the type of the function we are unifying against. */
20901 tree fntype = static_fn_type (arg);
20903 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20906 if (TREE_CODE (arg) != OFFSET_TYPE)
20907 return unify_type_mismatch (explain_p, parm, arg);
20908 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20909 TYPE_OFFSET_BASETYPE (arg),
20910 UNIFY_ALLOW_NONE, explain_p);
20911 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20912 strict, explain_p);
20914 case CONST_DECL:
20915 if (DECL_TEMPLATE_PARM_P (parm))
20916 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20917 if (arg != scalar_constant_value (parm))
20918 return unify_template_argument_mismatch (explain_p, parm, arg);
20919 return unify_success (explain_p);
20921 case FIELD_DECL:
20922 case TEMPLATE_DECL:
20923 /* Matched cases are handled by the ARG == PARM test above. */
20924 return unify_template_argument_mismatch (explain_p, parm, arg);
20926 case VAR_DECL:
20927 /* We might get a variable as a non-type template argument in parm if the
20928 corresponding parameter is type-dependent. Make any necessary
20929 adjustments based on whether arg is a reference. */
20930 if (CONSTANT_CLASS_P (arg))
20931 parm = fold_non_dependent_expr (parm);
20932 else if (REFERENCE_REF_P (arg))
20934 tree sub = TREE_OPERAND (arg, 0);
20935 STRIP_NOPS (sub);
20936 if (TREE_CODE (sub) == ADDR_EXPR)
20937 arg = TREE_OPERAND (sub, 0);
20939 /* Now use the normal expression code to check whether they match. */
20940 goto expr;
20942 case TYPE_ARGUMENT_PACK:
20943 case NONTYPE_ARGUMENT_PACK:
20944 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20945 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20947 case TYPEOF_TYPE:
20948 case DECLTYPE_TYPE:
20949 case UNDERLYING_TYPE:
20950 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20951 or UNDERLYING_TYPE nodes. */
20952 return unify_success (explain_p);
20954 case ERROR_MARK:
20955 /* Unification fails if we hit an error node. */
20956 return unify_invalid (explain_p);
20958 case INDIRECT_REF:
20959 if (REFERENCE_REF_P (parm))
20961 bool pexp = PACK_EXPANSION_P (arg);
20962 if (pexp)
20963 arg = PACK_EXPANSION_PATTERN (arg);
20964 if (REFERENCE_REF_P (arg))
20965 arg = TREE_OPERAND (arg, 0);
20966 if (pexp)
20967 arg = make_pack_expansion (arg);
20968 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20969 strict, explain_p);
20971 /* FALLTHRU */
20973 default:
20974 /* An unresolved overload is a nondeduced context. */
20975 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20976 return unify_success (explain_p);
20977 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
20978 expr:
20979 /* We must be looking at an expression. This can happen with
20980 something like:
20982 template <int I>
20983 void foo(S<I>, S<I + 2>);
20985 This is a "nondeduced context":
20987 [deduct.type]
20989 The nondeduced contexts are:
20991 --A type that is a template-id in which one or more of
20992 the template-arguments is an expression that references
20993 a template-parameter.
20995 In these cases, we assume deduction succeeded, but don't
20996 actually infer any unifications. */
20998 if (!uses_template_parms (parm)
20999 && !template_args_equal (parm, arg))
21000 return unify_expression_unequal (explain_p, parm, arg);
21001 else
21002 return unify_success (explain_p);
21005 #undef RECUR_AND_CHECK_FAILURE
21007 /* Note that DECL can be defined in this translation unit, if
21008 required. */
21010 static void
21011 mark_definable (tree decl)
21013 tree clone;
21014 DECL_NOT_REALLY_EXTERN (decl) = 1;
21015 FOR_EACH_CLONE (clone, decl)
21016 DECL_NOT_REALLY_EXTERN (clone) = 1;
21019 /* Called if RESULT is explicitly instantiated, or is a member of an
21020 explicitly instantiated class. */
21022 void
21023 mark_decl_instantiated (tree result, int extern_p)
21025 SET_DECL_EXPLICIT_INSTANTIATION (result);
21027 /* If this entity has already been written out, it's too late to
21028 make any modifications. */
21029 if (TREE_ASM_WRITTEN (result))
21030 return;
21032 /* For anonymous namespace we don't need to do anything. */
21033 if (decl_anon_ns_mem_p (result))
21035 gcc_assert (!TREE_PUBLIC (result));
21036 return;
21039 if (TREE_CODE (result) != FUNCTION_DECL)
21040 /* The TREE_PUBLIC flag for function declarations will have been
21041 set correctly by tsubst. */
21042 TREE_PUBLIC (result) = 1;
21044 /* This might have been set by an earlier implicit instantiation. */
21045 DECL_COMDAT (result) = 0;
21047 if (extern_p)
21048 DECL_NOT_REALLY_EXTERN (result) = 0;
21049 else
21051 mark_definable (result);
21052 mark_needed (result);
21053 /* Always make artificials weak. */
21054 if (DECL_ARTIFICIAL (result) && flag_weak)
21055 comdat_linkage (result);
21056 /* For WIN32 we also want to put explicit instantiations in
21057 linkonce sections. */
21058 else if (TREE_PUBLIC (result))
21059 maybe_make_one_only (result);
21062 /* If EXTERN_P, then this function will not be emitted -- unless
21063 followed by an explicit instantiation, at which point its linkage
21064 will be adjusted. If !EXTERN_P, then this function will be
21065 emitted here. In neither circumstance do we want
21066 import_export_decl to adjust the linkage. */
21067 DECL_INTERFACE_KNOWN (result) = 1;
21070 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21071 important template arguments. If any are missing, we check whether
21072 they're important by using error_mark_node for substituting into any
21073 args that were used for partial ordering (the ones between ARGS and END)
21074 and seeing if it bubbles up. */
21076 static bool
21077 check_undeduced_parms (tree targs, tree args, tree end)
21079 bool found = false;
21080 int i;
21081 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21082 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21084 found = true;
21085 TREE_VEC_ELT (targs, i) = error_mark_node;
21087 if (found)
21089 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21090 if (substed == error_mark_node)
21091 return true;
21093 return false;
21096 /* Given two function templates PAT1 and PAT2, return:
21098 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21099 -1 if PAT2 is more specialized than PAT1.
21100 0 if neither is more specialized.
21102 LEN indicates the number of parameters we should consider
21103 (defaulted parameters should not be considered).
21105 The 1998 std underspecified function template partial ordering, and
21106 DR214 addresses the issue. We take pairs of arguments, one from
21107 each of the templates, and deduce them against each other. One of
21108 the templates will be more specialized if all the *other*
21109 template's arguments deduce against its arguments and at least one
21110 of its arguments *does* *not* deduce against the other template's
21111 corresponding argument. Deduction is done as for class templates.
21112 The arguments used in deduction have reference and top level cv
21113 qualifiers removed. Iff both arguments were originally reference
21114 types *and* deduction succeeds in both directions, an lvalue reference
21115 wins against an rvalue reference and otherwise the template
21116 with the more cv-qualified argument wins for that pairing (if
21117 neither is more cv-qualified, they both are equal). Unlike regular
21118 deduction, after all the arguments have been deduced in this way,
21119 we do *not* verify the deduced template argument values can be
21120 substituted into non-deduced contexts.
21122 The logic can be a bit confusing here, because we look at deduce1 and
21123 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21124 can find template arguments for pat1 to make arg1 look like arg2, that
21125 means that arg2 is at least as specialized as arg1. */
21128 more_specialized_fn (tree pat1, tree pat2, int len)
21130 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21131 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21132 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21133 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21134 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21135 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21136 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21137 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21138 tree origs1, origs2;
21139 bool lose1 = false;
21140 bool lose2 = false;
21142 /* Remove the this parameter from non-static member functions. If
21143 one is a non-static member function and the other is not a static
21144 member function, remove the first parameter from that function
21145 also. This situation occurs for operator functions where we
21146 locate both a member function (with this pointer) and non-member
21147 operator (with explicit first operand). */
21148 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21150 len--; /* LEN is the number of significant arguments for DECL1 */
21151 args1 = TREE_CHAIN (args1);
21152 if (!DECL_STATIC_FUNCTION_P (decl2))
21153 args2 = TREE_CHAIN (args2);
21155 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21157 args2 = TREE_CHAIN (args2);
21158 if (!DECL_STATIC_FUNCTION_P (decl1))
21160 len--;
21161 args1 = TREE_CHAIN (args1);
21165 /* If only one is a conversion operator, they are unordered. */
21166 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21167 return 0;
21169 /* Consider the return type for a conversion function */
21170 if (DECL_CONV_FN_P (decl1))
21172 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21173 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21174 len++;
21177 processing_template_decl++;
21179 origs1 = args1;
21180 origs2 = args2;
21182 while (len--
21183 /* Stop when an ellipsis is seen. */
21184 && args1 != NULL_TREE && args2 != NULL_TREE)
21186 tree arg1 = TREE_VALUE (args1);
21187 tree arg2 = TREE_VALUE (args2);
21188 int deduce1, deduce2;
21189 int quals1 = -1;
21190 int quals2 = -1;
21191 int ref1 = 0;
21192 int ref2 = 0;
21194 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21195 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21197 /* When both arguments are pack expansions, we need only
21198 unify the patterns themselves. */
21199 arg1 = PACK_EXPANSION_PATTERN (arg1);
21200 arg2 = PACK_EXPANSION_PATTERN (arg2);
21202 /* This is the last comparison we need to do. */
21203 len = 0;
21206 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21208 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21209 arg1 = TREE_TYPE (arg1);
21210 quals1 = cp_type_quals (arg1);
21213 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21215 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21216 arg2 = TREE_TYPE (arg2);
21217 quals2 = cp_type_quals (arg2);
21220 arg1 = TYPE_MAIN_VARIANT (arg1);
21221 arg2 = TYPE_MAIN_VARIANT (arg2);
21223 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21225 int i, len2 = remaining_arguments (args2);
21226 tree parmvec = make_tree_vec (1);
21227 tree argvec = make_tree_vec (len2);
21228 tree ta = args2;
21230 /* Setup the parameter vector, which contains only ARG1. */
21231 TREE_VEC_ELT (parmvec, 0) = arg1;
21233 /* Setup the argument vector, which contains the remaining
21234 arguments. */
21235 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21236 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21238 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21239 argvec, DEDUCE_EXACT,
21240 /*subr=*/true, /*explain_p=*/false)
21241 == 0);
21243 /* We cannot deduce in the other direction, because ARG1 is
21244 a pack expansion but ARG2 is not. */
21245 deduce2 = 0;
21247 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21249 int i, len1 = remaining_arguments (args1);
21250 tree parmvec = make_tree_vec (1);
21251 tree argvec = make_tree_vec (len1);
21252 tree ta = args1;
21254 /* Setup the parameter vector, which contains only ARG1. */
21255 TREE_VEC_ELT (parmvec, 0) = arg2;
21257 /* Setup the argument vector, which contains the remaining
21258 arguments. */
21259 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21260 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21262 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21263 argvec, DEDUCE_EXACT,
21264 /*subr=*/true, /*explain_p=*/false)
21265 == 0);
21267 /* We cannot deduce in the other direction, because ARG2 is
21268 a pack expansion but ARG1 is not.*/
21269 deduce1 = 0;
21272 else
21274 /* The normal case, where neither argument is a pack
21275 expansion. */
21276 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21277 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21278 == 0);
21279 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21280 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21281 == 0);
21284 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21285 arg2, then arg2 is not as specialized as arg1. */
21286 if (!deduce1)
21287 lose2 = true;
21288 if (!deduce2)
21289 lose1 = true;
21291 /* "If, for a given type, deduction succeeds in both directions
21292 (i.e., the types are identical after the transformations above)
21293 and both P and A were reference types (before being replaced with
21294 the type referred to above):
21295 - if the type from the argument template was an lvalue reference and
21296 the type from the parameter template was not, the argument type is
21297 considered to be more specialized than the other; otherwise,
21298 - if the type from the argument template is more cv-qualified
21299 than the type from the parameter template (as described above),
21300 the argument type is considered to be more specialized than the other;
21301 otherwise,
21302 - neither type is more specialized than the other." */
21304 if (deduce1 && deduce2)
21306 if (ref1 && ref2 && ref1 != ref2)
21308 if (ref1 > ref2)
21309 lose1 = true;
21310 else
21311 lose2 = true;
21313 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21315 if ((quals1 & quals2) == quals2)
21316 lose2 = true;
21317 if ((quals1 & quals2) == quals1)
21318 lose1 = true;
21322 if (lose1 && lose2)
21323 /* We've failed to deduce something in either direction.
21324 These must be unordered. */
21325 break;
21327 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21328 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21329 /* We have already processed all of the arguments in our
21330 handing of the pack expansion type. */
21331 len = 0;
21333 args1 = TREE_CHAIN (args1);
21334 args2 = TREE_CHAIN (args2);
21337 /* "In most cases, all template parameters must have values in order for
21338 deduction to succeed, but for partial ordering purposes a template
21339 parameter may remain without a value provided it is not used in the
21340 types being used for partial ordering."
21342 Thus, if we are missing any of the targs1 we need to substitute into
21343 origs1, then pat2 is not as specialized as pat1. This can happen when
21344 there is a nondeduced context. */
21345 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21346 lose2 = true;
21347 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21348 lose1 = true;
21350 processing_template_decl--;
21352 /* If both deductions succeed, the partial ordering selects the more
21353 constrained template. */
21354 if (!lose1 && !lose2)
21356 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21357 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21358 lose1 = !subsumes_constraints (c1, c2);
21359 lose2 = !subsumes_constraints (c2, c1);
21362 /* All things being equal, if the next argument is a pack expansion
21363 for one function but not for the other, prefer the
21364 non-variadic function. FIXME this is bogus; see c++/41958. */
21365 if (lose1 == lose2
21366 && args1 && TREE_VALUE (args1)
21367 && args2 && TREE_VALUE (args2))
21369 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21370 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21373 if (lose1 == lose2)
21374 return 0;
21375 else if (!lose1)
21376 return 1;
21377 else
21378 return -1;
21381 /* Determine which of two partial specializations of TMPL is more
21382 specialized.
21384 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21385 to the first partial specialization. The TREE_PURPOSE is the
21386 innermost set of template parameters for the partial
21387 specialization. PAT2 is similar, but for the second template.
21389 Return 1 if the first partial specialization is more specialized;
21390 -1 if the second is more specialized; 0 if neither is more
21391 specialized.
21393 See [temp.class.order] for information about determining which of
21394 two templates is more specialized. */
21396 static int
21397 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21399 tree targs;
21400 int winner = 0;
21401 bool any_deductions = false;
21403 tree tmpl1 = TREE_VALUE (pat1);
21404 tree tmpl2 = TREE_VALUE (pat2);
21405 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21406 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21408 /* Just like what happens for functions, if we are ordering between
21409 different template specializations, we may encounter dependent
21410 types in the arguments, and we need our dependency check functions
21411 to behave correctly. */
21412 ++processing_template_decl;
21413 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21414 if (targs)
21416 --winner;
21417 any_deductions = true;
21420 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21421 if (targs)
21423 ++winner;
21424 any_deductions = true;
21426 --processing_template_decl;
21428 /* If both deductions succeed, the partial ordering selects the more
21429 constrained template. */
21430 if (!winner && any_deductions)
21431 return more_constrained (tmpl1, tmpl2);
21433 /* In the case of a tie where at least one of the templates
21434 has a parameter pack at the end, the template with the most
21435 non-packed parameters wins. */
21436 if (winner == 0
21437 && any_deductions
21438 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21439 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21441 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21442 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21443 int len1 = TREE_VEC_LENGTH (args1);
21444 int len2 = TREE_VEC_LENGTH (args2);
21446 /* We don't count the pack expansion at the end. */
21447 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21448 --len1;
21449 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21450 --len2;
21452 if (len1 > len2)
21453 return 1;
21454 else if (len1 < len2)
21455 return -1;
21458 return winner;
21461 /* Return the template arguments that will produce the function signature
21462 DECL from the function template FN, with the explicit template
21463 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21464 also match. Return NULL_TREE if no satisfactory arguments could be
21465 found. */
21467 static tree
21468 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21470 int ntparms = DECL_NTPARMS (fn);
21471 tree targs = make_tree_vec (ntparms);
21472 tree decl_type = TREE_TYPE (decl);
21473 tree decl_arg_types;
21474 tree *args;
21475 unsigned int nargs, ix;
21476 tree arg;
21478 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21480 /* Never do unification on the 'this' parameter. */
21481 decl_arg_types = skip_artificial_parms_for (decl,
21482 TYPE_ARG_TYPES (decl_type));
21484 nargs = list_length (decl_arg_types);
21485 args = XALLOCAVEC (tree, nargs);
21486 for (arg = decl_arg_types, ix = 0;
21487 arg != NULL_TREE && arg != void_list_node;
21488 arg = TREE_CHAIN (arg), ++ix)
21489 args[ix] = TREE_VALUE (arg);
21491 if (fn_type_unification (fn, explicit_args, targs,
21492 args, ix,
21493 (check_rettype || DECL_CONV_FN_P (fn)
21494 ? TREE_TYPE (decl_type) : NULL_TREE),
21495 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21496 /*decltype*/false)
21497 == error_mark_node)
21498 return NULL_TREE;
21500 return targs;
21503 /* Return the innermost template arguments that, when applied to a partial
21504 specialization SPEC_TMPL of TMPL, yield the ARGS.
21506 For example, suppose we have:
21508 template <class T, class U> struct S {};
21509 template <class T> struct S<T*, int> {};
21511 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21512 partial specialization and the ARGS will be {double*, int}. The resulting
21513 vector will be {double}, indicating that `T' is bound to `double'. */
21515 static tree
21516 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21518 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21519 tree spec_args
21520 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21521 int i, ntparms = TREE_VEC_LENGTH (tparms);
21522 tree deduced_args;
21523 tree innermost_deduced_args;
21525 innermost_deduced_args = make_tree_vec (ntparms);
21526 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21528 deduced_args = copy_node (args);
21529 SET_TMPL_ARGS_LEVEL (deduced_args,
21530 TMPL_ARGS_DEPTH (deduced_args),
21531 innermost_deduced_args);
21533 else
21534 deduced_args = innermost_deduced_args;
21536 bool tried_array_deduction = (cxx_dialect < cxx1z);
21537 again:
21538 if (unify (tparms, deduced_args,
21539 INNERMOST_TEMPLATE_ARGS (spec_args),
21540 INNERMOST_TEMPLATE_ARGS (args),
21541 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21542 return NULL_TREE;
21544 for (i = 0; i < ntparms; ++i)
21545 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21547 if (!tried_array_deduction)
21549 try_array_deduction (tparms, innermost_deduced_args,
21550 INNERMOST_TEMPLATE_ARGS (spec_args));
21551 tried_array_deduction = true;
21552 if (TREE_VEC_ELT (innermost_deduced_args, i))
21553 goto again;
21555 return NULL_TREE;
21558 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21559 if (!push_tinst_level (tinst))
21561 excessive_deduction_depth = true;
21562 return NULL_TREE;
21565 /* Verify that nondeduced template arguments agree with the type
21566 obtained from argument deduction.
21568 For example:
21570 struct A { typedef int X; };
21571 template <class T, class U> struct C {};
21572 template <class T> struct C<T, typename T::X> {};
21574 Then with the instantiation `C<A, int>', we can deduce that
21575 `T' is `A' but unify () does not check whether `typename T::X'
21576 is `int'. */
21577 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21578 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21579 spec_args, tmpl,
21580 tf_none, false, false);
21582 pop_tinst_level ();
21584 if (spec_args == error_mark_node
21585 /* We only need to check the innermost arguments; the other
21586 arguments will always agree. */
21587 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
21588 INNERMOST_TEMPLATE_ARGS (args)))
21589 return NULL_TREE;
21591 /* Now that we have bindings for all of the template arguments,
21592 ensure that the arguments deduced for the template template
21593 parameters have compatible template parameter lists. See the use
21594 of template_template_parm_bindings_ok_p in fn_type_unification
21595 for more information. */
21596 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21597 return NULL_TREE;
21599 return deduced_args;
21602 // Compare two function templates T1 and T2 by deducing bindings
21603 // from one against the other. If both deductions succeed, compare
21604 // constraints to see which is more constrained.
21605 static int
21606 more_specialized_inst (tree t1, tree t2)
21608 int fate = 0;
21609 int count = 0;
21611 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21613 --fate;
21614 ++count;
21617 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21619 ++fate;
21620 ++count;
21623 // If both deductions succeed, then one may be more constrained.
21624 if (count == 2 && fate == 0)
21625 fate = more_constrained (t1, t2);
21627 return fate;
21630 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21631 Return the TREE_LIST node with the most specialized template, if
21632 any. If there is no most specialized template, the error_mark_node
21633 is returned.
21635 Note that this function does not look at, or modify, the
21636 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21637 returned is one of the elements of INSTANTIATIONS, callers may
21638 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21639 and retrieve it from the value returned. */
21641 tree
21642 most_specialized_instantiation (tree templates)
21644 tree fn, champ;
21646 ++processing_template_decl;
21648 champ = templates;
21649 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21651 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21652 if (fate == -1)
21653 champ = fn;
21654 else if (!fate)
21656 /* Equally specialized, move to next function. If there
21657 is no next function, nothing's most specialized. */
21658 fn = TREE_CHAIN (fn);
21659 champ = fn;
21660 if (!fn)
21661 break;
21665 if (champ)
21666 /* Now verify that champ is better than everything earlier in the
21667 instantiation list. */
21668 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21669 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21671 champ = NULL_TREE;
21672 break;
21676 processing_template_decl--;
21678 if (!champ)
21679 return error_mark_node;
21681 return champ;
21684 /* If DECL is a specialization of some template, return the most
21685 general such template. Otherwise, returns NULL_TREE.
21687 For example, given:
21689 template <class T> struct S { template <class U> void f(U); };
21691 if TMPL is `template <class U> void S<int>::f(U)' this will return
21692 the full template. This function will not trace past partial
21693 specializations, however. For example, given in addition:
21695 template <class T> struct S<T*> { template <class U> void f(U); };
21697 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21698 `template <class T> template <class U> S<T*>::f(U)'. */
21700 tree
21701 most_general_template (tree decl)
21703 if (TREE_CODE (decl) != TEMPLATE_DECL)
21705 if (tree tinfo = get_template_info (decl))
21706 decl = TI_TEMPLATE (tinfo);
21707 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21708 template friend, or a FIELD_DECL for a capture pack. */
21709 if (TREE_CODE (decl) != TEMPLATE_DECL)
21710 return NULL_TREE;
21713 /* Look for more and more general templates. */
21714 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21716 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21717 (See cp-tree.h for details.) */
21718 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21719 break;
21721 if (CLASS_TYPE_P (TREE_TYPE (decl))
21722 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21723 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21724 break;
21726 /* Stop if we run into an explicitly specialized class template. */
21727 if (!DECL_NAMESPACE_SCOPE_P (decl)
21728 && DECL_CONTEXT (decl)
21729 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21730 break;
21732 decl = DECL_TI_TEMPLATE (decl);
21735 return decl;
21738 /* Return the most specialized of the template partial specializations
21739 which can produce TARGET, a specialization of some class or variable
21740 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21741 a TEMPLATE_DECL node corresponding to the partial specialization, while
21742 the TREE_PURPOSE is the set of template arguments that must be
21743 substituted into the template pattern in order to generate TARGET.
21745 If the choice of partial specialization is ambiguous, a diagnostic
21746 is issued, and the error_mark_node is returned. If there are no
21747 partial specializations matching TARGET, then NULL_TREE is
21748 returned, indicating that the primary template should be used. */
21750 static tree
21751 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21753 tree list = NULL_TREE;
21754 tree t;
21755 tree champ;
21756 int fate;
21757 bool ambiguous_p;
21758 tree outer_args = NULL_TREE;
21759 tree tmpl, args;
21761 if (TYPE_P (target))
21763 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21764 tmpl = TI_TEMPLATE (tinfo);
21765 args = TI_ARGS (tinfo);
21767 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21769 tmpl = TREE_OPERAND (target, 0);
21770 args = TREE_OPERAND (target, 1);
21772 else if (VAR_P (target))
21774 tree tinfo = DECL_TEMPLATE_INFO (target);
21775 tmpl = TI_TEMPLATE (tinfo);
21776 args = TI_ARGS (tinfo);
21778 else
21779 gcc_unreachable ();
21781 tree main_tmpl = most_general_template (tmpl);
21783 /* For determining which partial specialization to use, only the
21784 innermost args are interesting. */
21785 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21787 outer_args = strip_innermost_template_args (args, 1);
21788 args = INNERMOST_TEMPLATE_ARGS (args);
21791 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21793 tree spec_args;
21794 tree spec_tmpl = TREE_VALUE (t);
21796 if (outer_args)
21798 /* Substitute in the template args from the enclosing class. */
21799 ++processing_template_decl;
21800 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21801 --processing_template_decl;
21804 if (spec_tmpl == error_mark_node)
21805 return error_mark_node;
21807 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21808 if (spec_args)
21810 if (outer_args)
21811 spec_args = add_to_template_args (outer_args, spec_args);
21813 /* Keep the candidate only if the constraints are satisfied,
21814 or if we're not compiling with concepts. */
21815 if (!flag_concepts
21816 || constraints_satisfied_p (spec_tmpl, spec_args))
21818 list = tree_cons (spec_args, TREE_VALUE (t), list);
21819 TREE_TYPE (list) = TREE_TYPE (t);
21824 if (! list)
21825 return NULL_TREE;
21827 ambiguous_p = false;
21828 t = list;
21829 champ = t;
21830 t = TREE_CHAIN (t);
21831 for (; t; t = TREE_CHAIN (t))
21833 fate = more_specialized_partial_spec (tmpl, champ, t);
21834 if (fate == 1)
21836 else
21838 if (fate == 0)
21840 t = TREE_CHAIN (t);
21841 if (! t)
21843 ambiguous_p = true;
21844 break;
21847 champ = t;
21851 if (!ambiguous_p)
21852 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21854 fate = more_specialized_partial_spec (tmpl, champ, t);
21855 if (fate != 1)
21857 ambiguous_p = true;
21858 break;
21862 if (ambiguous_p)
21864 const char *str;
21865 char *spaces = NULL;
21866 if (!(complain & tf_error))
21867 return error_mark_node;
21868 if (TYPE_P (target))
21869 error ("ambiguous template instantiation for %q#T", target);
21870 else
21871 error ("ambiguous template instantiation for %q#D", target);
21872 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21873 for (t = list; t; t = TREE_CHAIN (t))
21875 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21876 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21877 "%s %#S", spaces ? spaces : str, subst);
21878 spaces = spaces ? spaces : get_spaces (str);
21880 free (spaces);
21881 return error_mark_node;
21884 return champ;
21887 /* Explicitly instantiate DECL. */
21889 void
21890 do_decl_instantiation (tree decl, tree storage)
21892 tree result = NULL_TREE;
21893 int extern_p = 0;
21895 if (!decl || decl == error_mark_node)
21896 /* An error occurred, for which grokdeclarator has already issued
21897 an appropriate message. */
21898 return;
21899 else if (! DECL_LANG_SPECIFIC (decl))
21901 error ("explicit instantiation of non-template %q#D", decl);
21902 return;
21905 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21906 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21908 if (VAR_P (decl) && !var_templ)
21910 /* There is an asymmetry here in the way VAR_DECLs and
21911 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21912 the latter, the DECL we get back will be marked as a
21913 template instantiation, and the appropriate
21914 DECL_TEMPLATE_INFO will be set up. This does not happen for
21915 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21916 should handle VAR_DECLs as it currently handles
21917 FUNCTION_DECLs. */
21918 if (!DECL_CLASS_SCOPE_P (decl))
21920 error ("%qD is not a static data member of a class template", decl);
21921 return;
21923 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21924 if (!result || !VAR_P (result))
21926 error ("no matching template for %qD found", decl);
21927 return;
21929 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21931 error ("type %qT for explicit instantiation %qD does not match "
21932 "declared type %qT", TREE_TYPE (result), decl,
21933 TREE_TYPE (decl));
21934 return;
21937 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21939 error ("explicit instantiation of %q#D", decl);
21940 return;
21942 else
21943 result = decl;
21945 /* Check for various error cases. Note that if the explicit
21946 instantiation is valid the RESULT will currently be marked as an
21947 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21948 until we get here. */
21950 if (DECL_TEMPLATE_SPECIALIZATION (result))
21952 /* DR 259 [temp.spec].
21954 Both an explicit instantiation and a declaration of an explicit
21955 specialization shall not appear in a program unless the explicit
21956 instantiation follows a declaration of the explicit specialization.
21958 For a given set of template parameters, if an explicit
21959 instantiation of a template appears after a declaration of an
21960 explicit specialization for that template, the explicit
21961 instantiation has no effect. */
21962 return;
21964 else if (DECL_EXPLICIT_INSTANTIATION (result))
21966 /* [temp.spec]
21968 No program shall explicitly instantiate any template more
21969 than once.
21971 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21972 the first instantiation was `extern' and the second is not,
21973 and EXTERN_P for the opposite case. */
21974 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21975 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21976 /* If an "extern" explicit instantiation follows an ordinary
21977 explicit instantiation, the template is instantiated. */
21978 if (extern_p)
21979 return;
21981 else if (!DECL_IMPLICIT_INSTANTIATION (result))
21983 error ("no matching template for %qD found", result);
21984 return;
21986 else if (!DECL_TEMPLATE_INFO (result))
21988 permerror (input_location, "explicit instantiation of non-template %q#D", result);
21989 return;
21992 if (storage == NULL_TREE)
21994 else if (storage == ridpointers[(int) RID_EXTERN])
21996 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
21997 pedwarn (input_location, OPT_Wpedantic,
21998 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21999 "instantiations");
22000 extern_p = 1;
22002 else
22003 error ("storage class %qD applied to template instantiation", storage);
22005 check_explicit_instantiation_namespace (result);
22006 mark_decl_instantiated (result, extern_p);
22007 if (! extern_p)
22008 instantiate_decl (result, /*defer_ok=*/true,
22009 /*expl_inst_class_mem_p=*/false);
22012 static void
22013 mark_class_instantiated (tree t, int extern_p)
22015 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22016 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22017 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22018 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22019 if (! extern_p)
22021 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22022 rest_of_type_compilation (t, 1);
22026 /* Called from do_type_instantiation through binding_table_foreach to
22027 do recursive instantiation for the type bound in ENTRY. */
22028 static void
22029 bt_instantiate_type_proc (binding_entry entry, void *data)
22031 tree storage = *(tree *) data;
22033 if (MAYBE_CLASS_TYPE_P (entry->type)
22034 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22035 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22038 /* Called from do_type_instantiation to instantiate a member
22039 (a member function or a static member variable) of an
22040 explicitly instantiated class template. */
22041 static void
22042 instantiate_class_member (tree decl, int extern_p)
22044 mark_decl_instantiated (decl, extern_p);
22045 if (! extern_p)
22046 instantiate_decl (decl, /*defer_ok=*/true,
22047 /*expl_inst_class_mem_p=*/true);
22050 /* Perform an explicit instantiation of template class T. STORAGE, if
22051 non-null, is the RID for extern, inline or static. COMPLAIN is
22052 nonzero if this is called from the parser, zero if called recursively,
22053 since the standard is unclear (as detailed below). */
22055 void
22056 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22058 int extern_p = 0;
22059 int nomem_p = 0;
22060 int static_p = 0;
22061 int previous_instantiation_extern_p = 0;
22063 if (TREE_CODE (t) == TYPE_DECL)
22064 t = TREE_TYPE (t);
22066 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22068 tree tmpl =
22069 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22070 if (tmpl)
22071 error ("explicit instantiation of non-class template %qD", tmpl);
22072 else
22073 error ("explicit instantiation of non-template type %qT", t);
22074 return;
22077 complete_type (t);
22079 if (!COMPLETE_TYPE_P (t))
22081 if (complain & tf_error)
22082 error ("explicit instantiation of %q#T before definition of template",
22084 return;
22087 if (storage != NULL_TREE)
22089 if (!in_system_header_at (input_location))
22091 if (storage == ridpointers[(int) RID_EXTERN])
22093 if (cxx_dialect == cxx98)
22094 pedwarn (input_location, OPT_Wpedantic,
22095 "ISO C++ 1998 forbids the use of %<extern%> on "
22096 "explicit instantiations");
22098 else
22099 pedwarn (input_location, OPT_Wpedantic,
22100 "ISO C++ forbids the use of %qE"
22101 " on explicit instantiations", storage);
22104 if (storage == ridpointers[(int) RID_INLINE])
22105 nomem_p = 1;
22106 else if (storage == ridpointers[(int) RID_EXTERN])
22107 extern_p = 1;
22108 else if (storage == ridpointers[(int) RID_STATIC])
22109 static_p = 1;
22110 else
22112 error ("storage class %qD applied to template instantiation",
22113 storage);
22114 extern_p = 0;
22118 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22120 /* DR 259 [temp.spec].
22122 Both an explicit instantiation and a declaration of an explicit
22123 specialization shall not appear in a program unless the explicit
22124 instantiation follows a declaration of the explicit specialization.
22126 For a given set of template parameters, if an explicit
22127 instantiation of a template appears after a declaration of an
22128 explicit specialization for that template, the explicit
22129 instantiation has no effect. */
22130 return;
22132 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22134 /* [temp.spec]
22136 No program shall explicitly instantiate any template more
22137 than once.
22139 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22140 instantiation was `extern'. If EXTERN_P then the second is.
22141 These cases are OK. */
22142 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22144 if (!previous_instantiation_extern_p && !extern_p
22145 && (complain & tf_error))
22146 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22148 /* If we've already instantiated the template, just return now. */
22149 if (!CLASSTYPE_INTERFACE_ONLY (t))
22150 return;
22153 check_explicit_instantiation_namespace (TYPE_NAME (t));
22154 mark_class_instantiated (t, extern_p);
22156 if (nomem_p)
22157 return;
22160 tree tmp;
22162 /* In contrast to implicit instantiation, where only the
22163 declarations, and not the definitions, of members are
22164 instantiated, we have here:
22166 [temp.explicit]
22168 The explicit instantiation of a class template specialization
22169 implies the instantiation of all of its members not
22170 previously explicitly specialized in the translation unit
22171 containing the explicit instantiation.
22173 Of course, we can't instantiate member template classes, since
22174 we don't have any arguments for them. Note that the standard
22175 is unclear on whether the instantiation of the members are
22176 *explicit* instantiations or not. However, the most natural
22177 interpretation is that it should be an explicit instantiation. */
22179 if (! static_p)
22180 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22181 if (TREE_CODE (tmp) == FUNCTION_DECL
22182 && DECL_TEMPLATE_INSTANTIATION (tmp)
22183 && user_provided_p (tmp))
22184 instantiate_class_member (tmp, extern_p);
22186 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22187 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22188 instantiate_class_member (tmp, extern_p);
22190 if (CLASSTYPE_NESTED_UTDS (t))
22191 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22192 bt_instantiate_type_proc, &storage);
22196 /* Given a function DECL, which is a specialization of TMPL, modify
22197 DECL to be a re-instantiation of TMPL with the same template
22198 arguments. TMPL should be the template into which tsubst'ing
22199 should occur for DECL, not the most general template.
22201 One reason for doing this is a scenario like this:
22203 template <class T>
22204 void f(const T&, int i);
22206 void g() { f(3, 7); }
22208 template <class T>
22209 void f(const T& t, const int i) { }
22211 Note that when the template is first instantiated, with
22212 instantiate_template, the resulting DECL will have no name for the
22213 first parameter, and the wrong type for the second. So, when we go
22214 to instantiate the DECL, we regenerate it. */
22216 static void
22217 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22219 /* The arguments used to instantiate DECL, from the most general
22220 template. */
22221 tree code_pattern;
22223 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22225 /* Make sure that we can see identifiers, and compute access
22226 correctly. */
22227 push_access_scope (decl);
22229 if (TREE_CODE (decl) == FUNCTION_DECL)
22231 tree decl_parm;
22232 tree pattern_parm;
22233 tree specs;
22234 int args_depth;
22235 int parms_depth;
22237 args_depth = TMPL_ARGS_DEPTH (args);
22238 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22239 if (args_depth > parms_depth)
22240 args = get_innermost_template_args (args, parms_depth);
22242 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22243 args, tf_error, NULL_TREE,
22244 /*defer_ok*/false);
22245 if (specs && specs != error_mark_node)
22246 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22247 specs);
22249 /* Merge parameter declarations. */
22250 decl_parm = skip_artificial_parms_for (decl,
22251 DECL_ARGUMENTS (decl));
22252 pattern_parm
22253 = skip_artificial_parms_for (code_pattern,
22254 DECL_ARGUMENTS (code_pattern));
22255 while (decl_parm && !DECL_PACK_P (pattern_parm))
22257 tree parm_type;
22258 tree attributes;
22260 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22261 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22262 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22263 NULL_TREE);
22264 parm_type = type_decays_to (parm_type);
22265 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22266 TREE_TYPE (decl_parm) = parm_type;
22267 attributes = DECL_ATTRIBUTES (pattern_parm);
22268 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22270 DECL_ATTRIBUTES (decl_parm) = attributes;
22271 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22273 decl_parm = DECL_CHAIN (decl_parm);
22274 pattern_parm = DECL_CHAIN (pattern_parm);
22276 /* Merge any parameters that match with the function parameter
22277 pack. */
22278 if (pattern_parm && DECL_PACK_P (pattern_parm))
22280 int i, len;
22281 tree expanded_types;
22282 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22283 the parameters in this function parameter pack. */
22284 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22285 args, tf_error, NULL_TREE);
22286 len = TREE_VEC_LENGTH (expanded_types);
22287 for (i = 0; i < len; i++)
22289 tree parm_type;
22290 tree attributes;
22292 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22293 /* Rename the parameter to include the index. */
22294 DECL_NAME (decl_parm) =
22295 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22296 parm_type = TREE_VEC_ELT (expanded_types, i);
22297 parm_type = type_decays_to (parm_type);
22298 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22299 TREE_TYPE (decl_parm) = parm_type;
22300 attributes = DECL_ATTRIBUTES (pattern_parm);
22301 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22303 DECL_ATTRIBUTES (decl_parm) = attributes;
22304 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22306 decl_parm = DECL_CHAIN (decl_parm);
22309 /* Merge additional specifiers from the CODE_PATTERN. */
22310 if (DECL_DECLARED_INLINE_P (code_pattern)
22311 && !DECL_DECLARED_INLINE_P (decl))
22312 DECL_DECLARED_INLINE_P (decl) = 1;
22314 else if (VAR_P (decl))
22316 DECL_INITIAL (decl) =
22317 tsubst_expr (DECL_INITIAL (code_pattern), args,
22318 tf_error, DECL_TI_TEMPLATE (decl),
22319 /*integral_constant_expression_p=*/false);
22320 if (VAR_HAD_UNKNOWN_BOUND (decl))
22321 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22322 tf_error, DECL_TI_TEMPLATE (decl));
22324 else
22325 gcc_unreachable ();
22327 pop_access_scope (decl);
22330 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22331 substituted to get DECL. */
22333 tree
22334 template_for_substitution (tree decl)
22336 tree tmpl = DECL_TI_TEMPLATE (decl);
22338 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22339 for the instantiation. This is not always the most general
22340 template. Consider, for example:
22342 template <class T>
22343 struct S { template <class U> void f();
22344 template <> void f<int>(); };
22346 and an instantiation of S<double>::f<int>. We want TD to be the
22347 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22348 while (/* An instantiation cannot have a definition, so we need a
22349 more general template. */
22350 DECL_TEMPLATE_INSTANTIATION (tmpl)
22351 /* We must also deal with friend templates. Given:
22353 template <class T> struct S {
22354 template <class U> friend void f() {};
22357 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22358 so far as the language is concerned, but that's still
22359 where we get the pattern for the instantiation from. On
22360 other hand, if the definition comes outside the class, say:
22362 template <class T> struct S {
22363 template <class U> friend void f();
22365 template <class U> friend void f() {}
22367 we don't need to look any further. That's what the check for
22368 DECL_INITIAL is for. */
22369 || (TREE_CODE (decl) == FUNCTION_DECL
22370 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22371 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22373 /* The present template, TD, should not be a definition. If it
22374 were a definition, we should be using it! Note that we
22375 cannot restructure the loop to just keep going until we find
22376 a template with a definition, since that might go too far if
22377 a specialization was declared, but not defined. */
22379 /* Fetch the more general template. */
22380 tmpl = DECL_TI_TEMPLATE (tmpl);
22383 return tmpl;
22386 /* Returns true if we need to instantiate this template instance even if we
22387 know we aren't going to emit it. */
22389 bool
22390 always_instantiate_p (tree decl)
22392 /* We always instantiate inline functions so that we can inline them. An
22393 explicit instantiation declaration prohibits implicit instantiation of
22394 non-inline functions. With high levels of optimization, we would
22395 normally inline non-inline functions -- but we're not allowed to do
22396 that for "extern template" functions. Therefore, we check
22397 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22398 return ((TREE_CODE (decl) == FUNCTION_DECL
22399 && (DECL_DECLARED_INLINE_P (decl)
22400 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22401 /* And we need to instantiate static data members so that
22402 their initializers are available in integral constant
22403 expressions. */
22404 || (VAR_P (decl)
22405 && decl_maybe_constant_var_p (decl)));
22408 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22409 instantiate it now, modifying TREE_TYPE (fn). */
22411 void
22412 maybe_instantiate_noexcept (tree fn)
22414 tree fntype, spec, noex, clone;
22416 /* Don't instantiate a noexcept-specification from template context. */
22417 if (processing_template_decl)
22418 return;
22420 if (DECL_CLONED_FUNCTION_P (fn))
22421 fn = DECL_CLONED_FUNCTION (fn);
22422 fntype = TREE_TYPE (fn);
22423 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22425 if (!spec || !TREE_PURPOSE (spec))
22426 return;
22428 noex = TREE_PURPOSE (spec);
22430 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22432 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22433 spec = get_defaulted_eh_spec (fn);
22434 else if (push_tinst_level (fn))
22436 push_access_scope (fn);
22437 push_deferring_access_checks (dk_no_deferred);
22438 input_location = DECL_SOURCE_LOCATION (fn);
22439 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22440 DEFERRED_NOEXCEPT_ARGS (noex),
22441 tf_warning_or_error, fn,
22442 /*function_p=*/false,
22443 /*integral_constant_expression_p=*/true);
22444 pop_deferring_access_checks ();
22445 pop_access_scope (fn);
22446 pop_tinst_level ();
22447 spec = build_noexcept_spec (noex, tf_warning_or_error);
22448 if (spec == error_mark_node)
22449 spec = noexcept_false_spec;
22451 else
22452 spec = noexcept_false_spec;
22454 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22457 FOR_EACH_CLONE (clone, fn)
22459 if (TREE_TYPE (clone) == fntype)
22460 TREE_TYPE (clone) = TREE_TYPE (fn);
22461 else
22462 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22466 /* Produce the definition of D, a _DECL generated from a template. If
22467 DEFER_OK is true, then we don't have to actually do the
22468 instantiation now; we just have to do it sometime. Normally it is
22469 an error if this is an explicit instantiation but D is undefined.
22470 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22471 instantiated class template. */
22473 tree
22474 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22476 tree tmpl = DECL_TI_TEMPLATE (d);
22477 tree gen_args;
22478 tree args;
22479 tree td;
22480 tree code_pattern;
22481 tree spec;
22482 tree gen_tmpl;
22483 bool pattern_defined;
22484 location_t saved_loc = input_location;
22485 int saved_unevaluated_operand = cp_unevaluated_operand;
22486 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22487 bool external_p;
22488 bool deleted_p;
22490 /* This function should only be used to instantiate templates for
22491 functions and static member variables. */
22492 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22494 /* A concept is never instantiated. */
22495 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22497 /* Variables are never deferred; if instantiation is required, they
22498 are instantiated right away. That allows for better code in the
22499 case that an expression refers to the value of the variable --
22500 if the variable has a constant value the referring expression can
22501 take advantage of that fact. */
22502 if (VAR_P (d))
22503 defer_ok = false;
22505 /* Don't instantiate cloned functions. Instead, instantiate the
22506 functions they cloned. */
22507 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22508 d = DECL_CLONED_FUNCTION (d);
22510 if (DECL_TEMPLATE_INSTANTIATED (d)
22511 || (TREE_CODE (d) == FUNCTION_DECL
22512 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22513 || DECL_TEMPLATE_SPECIALIZATION (d))
22514 /* D has already been instantiated or explicitly specialized, so
22515 there's nothing for us to do here.
22517 It might seem reasonable to check whether or not D is an explicit
22518 instantiation, and, if so, stop here. But when an explicit
22519 instantiation is deferred until the end of the compilation,
22520 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22521 the instantiation. */
22522 return d;
22524 /* Check to see whether we know that this template will be
22525 instantiated in some other file, as with "extern template"
22526 extension. */
22527 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22529 /* In general, we do not instantiate such templates. */
22530 if (external_p && !always_instantiate_p (d))
22531 return d;
22533 gen_tmpl = most_general_template (tmpl);
22534 gen_args = DECL_TI_ARGS (d);
22536 if (tmpl != gen_tmpl)
22537 /* We should already have the extra args. */
22538 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22539 == TMPL_ARGS_DEPTH (gen_args));
22540 /* And what's in the hash table should match D. */
22541 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22542 || spec == NULL_TREE);
22544 /* This needs to happen before any tsubsting. */
22545 if (! push_tinst_level (d))
22546 return d;
22548 timevar_push (TV_TEMPLATE_INST);
22550 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22551 for the instantiation. */
22552 td = template_for_substitution (d);
22553 args = gen_args;
22555 if (VAR_P (d))
22557 /* Look up an explicit specialization, if any. */
22558 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22559 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22560 if (elt && elt != error_mark_node)
22562 td = TREE_VALUE (elt);
22563 args = TREE_PURPOSE (elt);
22567 code_pattern = DECL_TEMPLATE_RESULT (td);
22569 /* We should never be trying to instantiate a member of a class
22570 template or partial specialization. */
22571 gcc_assert (d != code_pattern);
22573 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22574 || DECL_TEMPLATE_SPECIALIZATION (td))
22575 /* In the case of a friend template whose definition is provided
22576 outside the class, we may have too many arguments. Drop the
22577 ones we don't need. The same is true for specializations. */
22578 args = get_innermost_template_args
22579 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22581 if (TREE_CODE (d) == FUNCTION_DECL)
22583 deleted_p = DECL_DELETED_FN (code_pattern);
22584 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22585 && DECL_INITIAL (code_pattern) != error_mark_node)
22586 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22587 || deleted_p);
22589 else
22591 deleted_p = false;
22592 if (DECL_CLASS_SCOPE_P (code_pattern))
22593 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22594 || DECL_INLINE_VAR_P (code_pattern));
22595 else
22596 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22599 /* We may be in the middle of deferred access check. Disable it now. */
22600 push_deferring_access_checks (dk_no_deferred);
22602 /* Unless an explicit instantiation directive has already determined
22603 the linkage of D, remember that a definition is available for
22604 this entity. */
22605 if (pattern_defined
22606 && !DECL_INTERFACE_KNOWN (d)
22607 && !DECL_NOT_REALLY_EXTERN (d))
22608 mark_definable (d);
22610 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22611 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22612 input_location = DECL_SOURCE_LOCATION (d);
22614 /* If D is a member of an explicitly instantiated class template,
22615 and no definition is available, treat it like an implicit
22616 instantiation. */
22617 if (!pattern_defined && expl_inst_class_mem_p
22618 && DECL_EXPLICIT_INSTANTIATION (d))
22620 /* Leave linkage flags alone on instantiations with anonymous
22621 visibility. */
22622 if (TREE_PUBLIC (d))
22624 DECL_NOT_REALLY_EXTERN (d) = 0;
22625 DECL_INTERFACE_KNOWN (d) = 0;
22627 SET_DECL_IMPLICIT_INSTANTIATION (d);
22630 /* Defer all other templates, unless we have been explicitly
22631 forbidden from doing so. */
22632 if (/* If there is no definition, we cannot instantiate the
22633 template. */
22634 ! pattern_defined
22635 /* If it's OK to postpone instantiation, do so. */
22636 || defer_ok
22637 /* If this is a static data member that will be defined
22638 elsewhere, we don't want to instantiate the entire data
22639 member, but we do want to instantiate the initializer so that
22640 we can substitute that elsewhere. */
22641 || (external_p && VAR_P (d))
22642 /* Handle here a deleted function too, avoid generating
22643 its body (c++/61080). */
22644 || deleted_p)
22646 /* The definition of the static data member is now required so
22647 we must substitute the initializer. */
22648 if (VAR_P (d)
22649 && !DECL_INITIAL (d)
22650 && DECL_INITIAL (code_pattern))
22652 tree ns;
22653 tree init;
22654 bool const_init = false;
22655 bool enter_context = DECL_CLASS_SCOPE_P (d);
22657 ns = decl_namespace_context (d);
22658 push_nested_namespace (ns);
22659 if (enter_context)
22660 push_nested_class (DECL_CONTEXT (d));
22661 init = tsubst_expr (DECL_INITIAL (code_pattern),
22662 args,
22663 tf_warning_or_error, NULL_TREE,
22664 /*integral_constant_expression_p=*/false);
22665 /* If instantiating the initializer involved instantiating this
22666 again, don't call cp_finish_decl twice. */
22667 if (!DECL_INITIAL (d))
22669 /* Make sure the initializer is still constant, in case of
22670 circular dependency (template/instantiate6.C). */
22671 const_init
22672 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22673 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22674 /*asmspec_tree=*/NULL_TREE,
22675 LOOKUP_ONLYCONVERTING);
22677 if (enter_context)
22678 pop_nested_class ();
22679 pop_nested_namespace (ns);
22682 /* We restore the source position here because it's used by
22683 add_pending_template. */
22684 input_location = saved_loc;
22686 if (at_eof && !pattern_defined
22687 && DECL_EXPLICIT_INSTANTIATION (d)
22688 && DECL_NOT_REALLY_EXTERN (d))
22689 /* [temp.explicit]
22691 The definition of a non-exported function template, a
22692 non-exported member function template, or a non-exported
22693 member function or static data member of a class template
22694 shall be present in every translation unit in which it is
22695 explicitly instantiated. */
22696 permerror (input_location, "explicit instantiation of %qD "
22697 "but no definition available", d);
22699 /* If we're in unevaluated context, we just wanted to get the
22700 constant value; this isn't an odr use, so don't queue
22701 a full instantiation. */
22702 if (cp_unevaluated_operand != 0)
22703 goto out;
22704 /* ??? Historically, we have instantiated inline functions, even
22705 when marked as "extern template". */
22706 if (!(external_p && VAR_P (d)))
22707 add_pending_template (d);
22708 goto out;
22710 /* Tell the repository that D is available in this translation unit
22711 -- and see if it is supposed to be instantiated here. */
22712 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22714 /* In a PCH file, despite the fact that the repository hasn't
22715 requested instantiation in the PCH it is still possible that
22716 an instantiation will be required in a file that includes the
22717 PCH. */
22718 if (pch_file)
22719 add_pending_template (d);
22720 /* Instantiate inline functions so that the inliner can do its
22721 job, even though we'll not be emitting a copy of this
22722 function. */
22723 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22724 goto out;
22727 bool push_to_top, nested;
22728 tree fn_context;
22729 fn_context = decl_function_context (d);
22730 nested = current_function_decl != NULL_TREE;
22731 push_to_top = !(nested && fn_context == current_function_decl);
22733 vec<tree> omp_privatization_save;
22734 if (nested)
22735 save_omp_privatization_clauses (omp_privatization_save);
22737 if (push_to_top)
22738 push_to_top_level ();
22739 else
22741 push_function_context ();
22742 cp_unevaluated_operand = 0;
22743 c_inhibit_evaluation_warnings = 0;
22746 /* Mark D as instantiated so that recursive calls to
22747 instantiate_decl do not try to instantiate it again. */
22748 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22750 /* Regenerate the declaration in case the template has been modified
22751 by a subsequent redeclaration. */
22752 regenerate_decl_from_template (d, td, args);
22754 /* We already set the file and line above. Reset them now in case
22755 they changed as a result of calling regenerate_decl_from_template. */
22756 input_location = DECL_SOURCE_LOCATION (d);
22758 if (VAR_P (d))
22760 tree init;
22761 bool const_init = false;
22763 /* Clear out DECL_RTL; whatever was there before may not be right
22764 since we've reset the type of the declaration. */
22765 SET_DECL_RTL (d, NULL);
22766 DECL_IN_AGGR_P (d) = 0;
22768 /* The initializer is placed in DECL_INITIAL by
22769 regenerate_decl_from_template so we don't need to
22770 push/pop_access_scope again here. Pull it out so that
22771 cp_finish_decl can process it. */
22772 init = DECL_INITIAL (d);
22773 DECL_INITIAL (d) = NULL_TREE;
22774 DECL_INITIALIZED_P (d) = 0;
22776 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22777 initializer. That function will defer actual emission until
22778 we have a chance to determine linkage. */
22779 DECL_EXTERNAL (d) = 0;
22781 /* Enter the scope of D so that access-checking works correctly. */
22782 bool enter_context = DECL_CLASS_SCOPE_P (d);
22783 if (enter_context)
22784 push_nested_class (DECL_CONTEXT (d));
22786 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22787 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22789 if (enter_context)
22790 pop_nested_class ();
22792 if (variable_template_p (gen_tmpl))
22793 note_variable_template_instantiation (d);
22795 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22796 synthesize_method (d);
22797 else if (TREE_CODE (d) == FUNCTION_DECL)
22799 hash_map<tree, tree> *saved_local_specializations;
22800 tree tmpl_parm;
22801 tree spec_parm;
22802 tree block = NULL_TREE;
22803 tree lambda_ctx = NULL_TREE;
22805 /* Save away the current list, in case we are instantiating one
22806 template from within the body of another. */
22807 saved_local_specializations = local_specializations;
22809 /* Set up the list of local specializations. */
22810 local_specializations = new hash_map<tree, tree>;
22812 /* Set up context. */
22813 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22814 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22815 block = push_stmt_list ();
22816 else
22818 if (push_to_top && LAMBDA_FUNCTION_P (d))
22820 /* When instantiating a lambda's templated function
22821 operator, we need to push the non-lambda class scope
22822 of the lambda itself so that the nested function
22823 stack is sufficiently correct to deal with this
22824 capture. */
22825 lambda_ctx = DECL_CONTEXT (d);
22827 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22828 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22829 if (lambda_ctx)
22830 push_nested_class (lambda_ctx);
22832 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22835 /* Some typedefs referenced from within the template code need to be
22836 access checked at template instantiation time, i.e now. These
22837 types were added to the template at parsing time. Let's get those
22838 and perform the access checks then. */
22839 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22840 args);
22842 /* Create substitution entries for the parameters. */
22843 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22844 spec_parm = DECL_ARGUMENTS (d);
22845 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22847 register_local_specialization (spec_parm, tmpl_parm);
22848 spec_parm = skip_artificial_parms_for (d, spec_parm);
22849 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22851 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22853 if (!DECL_PACK_P (tmpl_parm))
22855 register_local_specialization (spec_parm, tmpl_parm);
22856 spec_parm = DECL_CHAIN (spec_parm);
22858 else
22860 /* Register the (value) argument pack as a specialization of
22861 TMPL_PARM, then move on. */
22862 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22863 register_local_specialization (argpack, tmpl_parm);
22866 gcc_assert (!spec_parm);
22868 /* Substitute into the body of the function. */
22869 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22870 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22871 tf_warning_or_error, tmpl);
22872 else
22874 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22875 tf_warning_or_error, tmpl,
22876 /*integral_constant_expression_p=*/false);
22878 /* Set the current input_location to the end of the function
22879 so that finish_function knows where we are. */
22880 input_location
22881 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22883 /* Remember if we saw an infinite loop in the template. */
22884 current_function_infinite_loop
22885 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22888 /* We don't need the local specializations any more. */
22889 delete local_specializations;
22890 local_specializations = saved_local_specializations;
22892 /* Finish the function. */
22893 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22894 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22895 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22896 else
22898 d = finish_function (0);
22899 expand_or_defer_fn (d);
22901 if (lambda_ctx)
22902 pop_nested_class ();
22904 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22905 cp_check_omp_declare_reduction (d);
22908 /* We're not deferring instantiation any more. */
22909 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22911 if (push_to_top)
22912 pop_from_top_level ();
22913 else
22914 pop_function_context ();
22916 if (nested)
22917 restore_omp_privatization_clauses (omp_privatization_save);
22919 out:
22920 pop_deferring_access_checks ();
22921 timevar_pop (TV_TEMPLATE_INST);
22922 pop_tinst_level ();
22923 input_location = saved_loc;
22924 cp_unevaluated_operand = saved_unevaluated_operand;
22925 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22927 return d;
22930 /* Run through the list of templates that we wish we could
22931 instantiate, and instantiate any we can. RETRIES is the
22932 number of times we retry pending template instantiation. */
22934 void
22935 instantiate_pending_templates (int retries)
22937 int reconsider;
22938 location_t saved_loc = input_location;
22940 /* Instantiating templates may trigger vtable generation. This in turn
22941 may require further template instantiations. We place a limit here
22942 to avoid infinite loop. */
22943 if (pending_templates && retries >= max_tinst_depth)
22945 tree decl = pending_templates->tinst->decl;
22947 fatal_error (input_location,
22948 "template instantiation depth exceeds maximum of %d"
22949 " instantiating %q+D, possibly from virtual table generation"
22950 " (use -ftemplate-depth= to increase the maximum)",
22951 max_tinst_depth, decl);
22952 if (TREE_CODE (decl) == FUNCTION_DECL)
22953 /* Pretend that we defined it. */
22954 DECL_INITIAL (decl) = error_mark_node;
22955 return;
22960 struct pending_template **t = &pending_templates;
22961 struct pending_template *last = NULL;
22962 reconsider = 0;
22963 while (*t)
22965 tree instantiation = reopen_tinst_level ((*t)->tinst);
22966 bool complete = false;
22968 if (TYPE_P (instantiation))
22970 tree fn;
22972 if (!COMPLETE_TYPE_P (instantiation))
22974 instantiate_class_template (instantiation);
22975 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22976 for (fn = TYPE_METHODS (instantiation);
22978 fn = TREE_CHAIN (fn))
22979 if (! DECL_ARTIFICIAL (fn))
22980 instantiate_decl (fn,
22981 /*defer_ok=*/false,
22982 /*expl_inst_class_mem_p=*/false);
22983 if (COMPLETE_TYPE_P (instantiation))
22984 reconsider = 1;
22987 complete = COMPLETE_TYPE_P (instantiation);
22989 else
22991 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
22992 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
22994 instantiation
22995 = instantiate_decl (instantiation,
22996 /*defer_ok=*/false,
22997 /*expl_inst_class_mem_p=*/false);
22998 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
22999 reconsider = 1;
23002 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23003 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23006 if (complete)
23007 /* If INSTANTIATION has been instantiated, then we don't
23008 need to consider it again in the future. */
23009 *t = (*t)->next;
23010 else
23012 last = *t;
23013 t = &(*t)->next;
23015 tinst_depth = 0;
23016 current_tinst_level = NULL;
23018 last_pending_template = last;
23020 while (reconsider);
23022 input_location = saved_loc;
23025 /* Substitute ARGVEC into T, which is a list of initializers for
23026 either base class or a non-static data member. The TREE_PURPOSEs
23027 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23028 instantiate_decl. */
23030 static tree
23031 tsubst_initializer_list (tree t, tree argvec)
23033 tree inits = NULL_TREE;
23035 for (; t; t = TREE_CHAIN (t))
23037 tree decl;
23038 tree init;
23039 tree expanded_bases = NULL_TREE;
23040 tree expanded_arguments = NULL_TREE;
23041 int i, len = 1;
23043 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23045 tree expr;
23046 tree arg;
23048 /* Expand the base class expansion type into separate base
23049 classes. */
23050 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23051 tf_warning_or_error,
23052 NULL_TREE);
23053 if (expanded_bases == error_mark_node)
23054 continue;
23056 /* We'll be building separate TREE_LISTs of arguments for
23057 each base. */
23058 len = TREE_VEC_LENGTH (expanded_bases);
23059 expanded_arguments = make_tree_vec (len);
23060 for (i = 0; i < len; i++)
23061 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23063 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23064 expand each argument in the TREE_VALUE of t. */
23065 expr = make_node (EXPR_PACK_EXPANSION);
23066 PACK_EXPANSION_LOCAL_P (expr) = true;
23067 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23068 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23070 if (TREE_VALUE (t) == void_type_node)
23071 /* VOID_TYPE_NODE is used to indicate
23072 value-initialization. */
23074 for (i = 0; i < len; i++)
23075 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23077 else
23079 /* Substitute parameter packs into each argument in the
23080 TREE_LIST. */
23081 in_base_initializer = 1;
23082 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23084 tree expanded_exprs;
23086 /* Expand the argument. */
23087 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23088 expanded_exprs
23089 = tsubst_pack_expansion (expr, argvec,
23090 tf_warning_or_error,
23091 NULL_TREE);
23092 if (expanded_exprs == error_mark_node)
23093 continue;
23095 /* Prepend each of the expanded expressions to the
23096 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23097 for (i = 0; i < len; i++)
23099 TREE_VEC_ELT (expanded_arguments, i) =
23100 tree_cons (NULL_TREE,
23101 TREE_VEC_ELT (expanded_exprs, i),
23102 TREE_VEC_ELT (expanded_arguments, i));
23105 in_base_initializer = 0;
23107 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23108 since we built them backwards. */
23109 for (i = 0; i < len; i++)
23111 TREE_VEC_ELT (expanded_arguments, i) =
23112 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23117 for (i = 0; i < len; ++i)
23119 if (expanded_bases)
23121 decl = TREE_VEC_ELT (expanded_bases, i);
23122 decl = expand_member_init (decl);
23123 init = TREE_VEC_ELT (expanded_arguments, i);
23125 else
23127 tree tmp;
23128 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23129 tf_warning_or_error, NULL_TREE);
23131 decl = expand_member_init (decl);
23132 if (decl && !DECL_P (decl))
23133 in_base_initializer = 1;
23135 init = TREE_VALUE (t);
23136 tmp = init;
23137 if (init != void_type_node)
23138 init = tsubst_expr (init, argvec,
23139 tf_warning_or_error, NULL_TREE,
23140 /*integral_constant_expression_p=*/false);
23141 if (init == NULL_TREE && tmp != NULL_TREE)
23142 /* If we had an initializer but it instantiated to nothing,
23143 value-initialize the object. This will only occur when
23144 the initializer was a pack expansion where the parameter
23145 packs used in that expansion were of length zero. */
23146 init = void_type_node;
23147 in_base_initializer = 0;
23150 if (decl)
23152 init = build_tree_list (decl, init);
23153 TREE_CHAIN (init) = inits;
23154 inits = init;
23158 return inits;
23161 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23163 static void
23164 set_current_access_from_decl (tree decl)
23166 if (TREE_PRIVATE (decl))
23167 current_access_specifier = access_private_node;
23168 else if (TREE_PROTECTED (decl))
23169 current_access_specifier = access_protected_node;
23170 else
23171 current_access_specifier = access_public_node;
23174 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23175 is the instantiation (which should have been created with
23176 start_enum) and ARGS are the template arguments to use. */
23178 static void
23179 tsubst_enum (tree tag, tree newtag, tree args)
23181 tree e;
23183 if (SCOPED_ENUM_P (newtag))
23184 begin_scope (sk_scoped_enum, newtag);
23186 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23188 tree value;
23189 tree decl;
23191 decl = TREE_VALUE (e);
23192 /* Note that in a template enum, the TREE_VALUE is the
23193 CONST_DECL, not the corresponding INTEGER_CST. */
23194 value = tsubst_expr (DECL_INITIAL (decl),
23195 args, tf_warning_or_error, NULL_TREE,
23196 /*integral_constant_expression_p=*/true);
23198 /* Give this enumeration constant the correct access. */
23199 set_current_access_from_decl (decl);
23201 /* Actually build the enumerator itself. Here we're assuming that
23202 enumerators can't have dependent attributes. */
23203 build_enumerator (DECL_NAME (decl), value, newtag,
23204 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23207 if (SCOPED_ENUM_P (newtag))
23208 finish_scope ();
23210 finish_enum_value_list (newtag);
23211 finish_enum (newtag);
23213 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23214 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23217 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23218 its type -- but without substituting the innermost set of template
23219 arguments. So, innermost set of template parameters will appear in
23220 the type. */
23222 tree
23223 get_mostly_instantiated_function_type (tree decl)
23225 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23226 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23229 /* Return truthvalue if we're processing a template different from
23230 the last one involved in diagnostics. */
23231 bool
23232 problematic_instantiation_changed (void)
23234 return current_tinst_level != last_error_tinst_level;
23237 /* Remember current template involved in diagnostics. */
23238 void
23239 record_last_problematic_instantiation (void)
23241 last_error_tinst_level = current_tinst_level;
23244 struct tinst_level *
23245 current_instantiation (void)
23247 return current_tinst_level;
23250 /* Return TRUE if current_function_decl is being instantiated, false
23251 otherwise. */
23253 bool
23254 instantiating_current_function_p (void)
23256 return (current_instantiation ()
23257 && current_instantiation ()->decl == current_function_decl);
23260 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23261 type. Return zero for ok, nonzero for disallowed. Issue error and
23262 warning messages under control of COMPLAIN. */
23264 static int
23265 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23267 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23268 return 0;
23269 else if (POINTER_TYPE_P (type))
23270 return 0;
23271 else if (TYPE_PTRMEM_P (type))
23272 return 0;
23273 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23274 return 0;
23275 else if (TREE_CODE (type) == TYPENAME_TYPE)
23276 return 0;
23277 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23278 return 0;
23279 else if (TREE_CODE (type) == NULLPTR_TYPE)
23280 return 0;
23281 /* A bound template template parm could later be instantiated to have a valid
23282 nontype parm type via an alias template. */
23283 else if (cxx_dialect >= cxx11
23284 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23285 return 0;
23287 if (complain & tf_error)
23289 if (type == error_mark_node)
23290 inform (input_location, "invalid template non-type parameter");
23291 else
23292 error ("%q#T is not a valid type for a template non-type parameter",
23293 type);
23295 return 1;
23298 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23299 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23301 static bool
23302 dependent_type_p_r (tree type)
23304 tree scope;
23306 /* [temp.dep.type]
23308 A type is dependent if it is:
23310 -- a template parameter. Template template parameters are types
23311 for us (since TYPE_P holds true for them) so we handle
23312 them here. */
23313 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23314 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23315 return true;
23316 /* -- a qualified-id with a nested-name-specifier which contains a
23317 class-name that names a dependent type or whose unqualified-id
23318 names a dependent type. */
23319 if (TREE_CODE (type) == TYPENAME_TYPE)
23320 return true;
23322 /* An alias template specialization can be dependent even if the
23323 resulting type is not. */
23324 if (dependent_alias_template_spec_p (type))
23325 return true;
23327 /* -- a cv-qualified type where the cv-unqualified type is
23328 dependent.
23329 No code is necessary for this bullet; the code below handles
23330 cv-qualified types, and we don't want to strip aliases with
23331 TYPE_MAIN_VARIANT because of DR 1558. */
23332 /* -- a compound type constructed from any dependent type. */
23333 if (TYPE_PTRMEM_P (type))
23334 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23335 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23336 (type)));
23337 else if (TYPE_PTR_P (type)
23338 || TREE_CODE (type) == REFERENCE_TYPE)
23339 return dependent_type_p (TREE_TYPE (type));
23340 else if (TREE_CODE (type) == FUNCTION_TYPE
23341 || TREE_CODE (type) == METHOD_TYPE)
23343 tree arg_type;
23345 if (dependent_type_p (TREE_TYPE (type)))
23346 return true;
23347 for (arg_type = TYPE_ARG_TYPES (type);
23348 arg_type;
23349 arg_type = TREE_CHAIN (arg_type))
23350 if (dependent_type_p (TREE_VALUE (arg_type)))
23351 return true;
23352 return false;
23354 /* -- an array type constructed from any dependent type or whose
23355 size is specified by a constant expression that is
23356 value-dependent.
23358 We checked for type- and value-dependence of the bounds in
23359 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23360 if (TREE_CODE (type) == ARRAY_TYPE)
23362 if (TYPE_DOMAIN (type)
23363 && dependent_type_p (TYPE_DOMAIN (type)))
23364 return true;
23365 return dependent_type_p (TREE_TYPE (type));
23368 /* -- a template-id in which either the template name is a template
23369 parameter ... */
23370 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23371 return true;
23372 /* ... or any of the template arguments is a dependent type or
23373 an expression that is type-dependent or value-dependent. */
23374 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23375 && (any_dependent_template_arguments_p
23376 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23377 return true;
23379 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23380 dependent; if the argument of the `typeof' expression is not
23381 type-dependent, then it should already been have resolved. */
23382 if (TREE_CODE (type) == TYPEOF_TYPE
23383 || TREE_CODE (type) == DECLTYPE_TYPE
23384 || TREE_CODE (type) == UNDERLYING_TYPE)
23385 return true;
23387 /* A template argument pack is dependent if any of its packed
23388 arguments are. */
23389 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23391 tree args = ARGUMENT_PACK_ARGS (type);
23392 int i, len = TREE_VEC_LENGTH (args);
23393 for (i = 0; i < len; ++i)
23394 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23395 return true;
23398 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23399 be template parameters. */
23400 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23401 return true;
23403 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23404 return true;
23406 /* The standard does not specifically mention types that are local
23407 to template functions or local classes, but they should be
23408 considered dependent too. For example:
23410 template <int I> void f() {
23411 enum E { a = I };
23412 S<sizeof (E)> s;
23415 The size of `E' cannot be known until the value of `I' has been
23416 determined. Therefore, `E' must be considered dependent. */
23417 scope = TYPE_CONTEXT (type);
23418 if (scope && TYPE_P (scope))
23419 return dependent_type_p (scope);
23420 /* Don't use type_dependent_expression_p here, as it can lead
23421 to infinite recursion trying to determine whether a lambda
23422 nested in a lambda is dependent (c++/47687). */
23423 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23424 && DECL_LANG_SPECIFIC (scope)
23425 && DECL_TEMPLATE_INFO (scope)
23426 && (any_dependent_template_arguments_p
23427 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23428 return true;
23430 /* Other types are non-dependent. */
23431 return false;
23434 /* Returns TRUE if TYPE is dependent, in the sense of
23435 [temp.dep.type]. Note that a NULL type is considered dependent. */
23437 bool
23438 dependent_type_p (tree type)
23440 /* If there are no template parameters in scope, then there can't be
23441 any dependent types. */
23442 if (!processing_template_decl)
23444 /* If we are not processing a template, then nobody should be
23445 providing us with a dependent type. */
23446 gcc_assert (type);
23447 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23448 return false;
23451 /* If the type is NULL, we have not computed a type for the entity
23452 in question; in that case, the type is dependent. */
23453 if (!type)
23454 return true;
23456 /* Erroneous types can be considered non-dependent. */
23457 if (type == error_mark_node)
23458 return false;
23460 /* If we have not already computed the appropriate value for TYPE,
23461 do so now. */
23462 if (!TYPE_DEPENDENT_P_VALID (type))
23464 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23465 TYPE_DEPENDENT_P_VALID (type) = 1;
23468 return TYPE_DEPENDENT_P (type);
23471 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23472 lookup. In other words, a dependent type that is not the current
23473 instantiation. */
23475 bool
23476 dependent_scope_p (tree scope)
23478 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23479 && !currently_open_class (scope));
23482 /* T is a SCOPE_REF; return whether we need to consider it
23483 instantiation-dependent so that we can check access at instantiation
23484 time even though we know which member it resolves to. */
23486 static bool
23487 instantiation_dependent_scope_ref_p (tree t)
23489 if (DECL_P (TREE_OPERAND (t, 1))
23490 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23491 && accessible_in_template_p (TREE_OPERAND (t, 0),
23492 TREE_OPERAND (t, 1)))
23493 return false;
23494 else
23495 return true;
23498 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23499 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23500 expression. */
23502 /* Note that this predicate is not appropriate for general expressions;
23503 only constant expressions (that satisfy potential_constant_expression)
23504 can be tested for value dependence. */
23506 bool
23507 value_dependent_expression_p (tree expression)
23509 if (!processing_template_decl || expression == NULL_TREE)
23510 return false;
23512 /* A name declared with a dependent type. */
23513 if (DECL_P (expression) && type_dependent_expression_p (expression))
23514 return true;
23516 switch (TREE_CODE (expression))
23518 case BASELINK:
23519 /* A dependent member function of the current instantiation. */
23520 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23522 case FUNCTION_DECL:
23523 /* A dependent member function of the current instantiation. */
23524 if (DECL_CLASS_SCOPE_P (expression)
23525 && dependent_type_p (DECL_CONTEXT (expression)))
23526 return true;
23527 break;
23529 case IDENTIFIER_NODE:
23530 /* A name that has not been looked up -- must be dependent. */
23531 return true;
23533 case TEMPLATE_PARM_INDEX:
23534 /* A non-type template parm. */
23535 return true;
23537 case CONST_DECL:
23538 /* A non-type template parm. */
23539 if (DECL_TEMPLATE_PARM_P (expression))
23540 return true;
23541 return value_dependent_expression_p (DECL_INITIAL (expression));
23543 case VAR_DECL:
23544 /* A constant with literal type and is initialized
23545 with an expression that is value-dependent.
23547 Note that a non-dependent parenthesized initializer will have
23548 already been replaced with its constant value, so if we see
23549 a TREE_LIST it must be dependent. */
23550 if (DECL_INITIAL (expression)
23551 && decl_constant_var_p (expression)
23552 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23553 /* cp_finish_decl doesn't fold reference initializers. */
23554 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23555 || type_dependent_expression_p (DECL_INITIAL (expression))
23556 || value_dependent_expression_p (DECL_INITIAL (expression))))
23557 return true;
23558 return false;
23560 case DYNAMIC_CAST_EXPR:
23561 case STATIC_CAST_EXPR:
23562 case CONST_CAST_EXPR:
23563 case REINTERPRET_CAST_EXPR:
23564 case CAST_EXPR:
23565 /* These expressions are value-dependent if the type to which
23566 the cast occurs is dependent or the expression being casted
23567 is value-dependent. */
23569 tree type = TREE_TYPE (expression);
23571 if (dependent_type_p (type))
23572 return true;
23574 /* A functional cast has a list of operands. */
23575 expression = TREE_OPERAND (expression, 0);
23576 if (!expression)
23578 /* If there are no operands, it must be an expression such
23579 as "int()". This should not happen for aggregate types
23580 because it would form non-constant expressions. */
23581 gcc_assert (cxx_dialect >= cxx11
23582 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23584 return false;
23587 if (TREE_CODE (expression) == TREE_LIST)
23588 return any_value_dependent_elements_p (expression);
23590 return value_dependent_expression_p (expression);
23593 case SIZEOF_EXPR:
23594 if (SIZEOF_EXPR_TYPE_P (expression))
23595 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23596 /* FALLTHRU */
23597 case ALIGNOF_EXPR:
23598 case TYPEID_EXPR:
23599 /* A `sizeof' expression is value-dependent if the operand is
23600 type-dependent or is a pack expansion. */
23601 expression = TREE_OPERAND (expression, 0);
23602 if (PACK_EXPANSION_P (expression))
23603 return true;
23604 else if (TYPE_P (expression))
23605 return dependent_type_p (expression);
23606 return instantiation_dependent_uneval_expression_p (expression);
23608 case AT_ENCODE_EXPR:
23609 /* An 'encode' expression is value-dependent if the operand is
23610 type-dependent. */
23611 expression = TREE_OPERAND (expression, 0);
23612 return dependent_type_p (expression);
23614 case NOEXCEPT_EXPR:
23615 expression = TREE_OPERAND (expression, 0);
23616 return instantiation_dependent_uneval_expression_p (expression);
23618 case SCOPE_REF:
23619 /* All instantiation-dependent expressions should also be considered
23620 value-dependent. */
23621 return instantiation_dependent_scope_ref_p (expression);
23623 case COMPONENT_REF:
23624 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23625 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23627 case NONTYPE_ARGUMENT_PACK:
23628 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23629 is value-dependent. */
23631 tree values = ARGUMENT_PACK_ARGS (expression);
23632 int i, len = TREE_VEC_LENGTH (values);
23634 for (i = 0; i < len; ++i)
23635 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23636 return true;
23638 return false;
23641 case TRAIT_EXPR:
23643 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23644 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23645 || (type2 ? dependent_type_p (type2) : false));
23648 case MODOP_EXPR:
23649 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23650 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23652 case ARRAY_REF:
23653 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23654 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23656 case ADDR_EXPR:
23658 tree op = TREE_OPERAND (expression, 0);
23659 return (value_dependent_expression_p (op)
23660 || has_value_dependent_address (op));
23663 case REQUIRES_EXPR:
23664 /* Treat all requires-expressions as value-dependent so
23665 we don't try to fold them. */
23666 return true;
23668 case TYPE_REQ:
23669 return dependent_type_p (TREE_OPERAND (expression, 0));
23671 case CALL_EXPR:
23673 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23674 return true;
23675 tree fn = get_callee_fndecl (expression);
23676 int i, nargs;
23677 nargs = call_expr_nargs (expression);
23678 for (i = 0; i < nargs; ++i)
23680 tree op = CALL_EXPR_ARG (expression, i);
23681 /* In a call to a constexpr member function, look through the
23682 implicit ADDR_EXPR on the object argument so that it doesn't
23683 cause the call to be considered value-dependent. We also
23684 look through it in potential_constant_expression. */
23685 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23686 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23687 && TREE_CODE (op) == ADDR_EXPR)
23688 op = TREE_OPERAND (op, 0);
23689 if (value_dependent_expression_p (op))
23690 return true;
23692 return false;
23695 case TEMPLATE_ID_EXPR:
23696 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23697 type-dependent. */
23698 return type_dependent_expression_p (expression)
23699 || variable_concept_p (TREE_OPERAND (expression, 0));
23701 case CONSTRUCTOR:
23703 unsigned ix;
23704 tree val;
23705 if (dependent_type_p (TREE_TYPE (expression)))
23706 return true;
23707 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23708 if (value_dependent_expression_p (val))
23709 return true;
23710 return false;
23713 case STMT_EXPR:
23714 /* Treat a GNU statement expression as dependent to avoid crashing
23715 under instantiate_non_dependent_expr; it can't be constant. */
23716 return true;
23718 default:
23719 /* A constant expression is value-dependent if any subexpression is
23720 value-dependent. */
23721 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23723 case tcc_reference:
23724 case tcc_unary:
23725 case tcc_comparison:
23726 case tcc_binary:
23727 case tcc_expression:
23728 case tcc_vl_exp:
23730 int i, len = cp_tree_operand_length (expression);
23732 for (i = 0; i < len; i++)
23734 tree t = TREE_OPERAND (expression, i);
23736 /* In some cases, some of the operands may be missing.l
23737 (For example, in the case of PREDECREMENT_EXPR, the
23738 amount to increment by may be missing.) That doesn't
23739 make the expression dependent. */
23740 if (t && value_dependent_expression_p (t))
23741 return true;
23744 break;
23745 default:
23746 break;
23748 break;
23751 /* The expression is not value-dependent. */
23752 return false;
23755 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23756 [temp.dep.expr]. Note that an expression with no type is
23757 considered dependent. Other parts of the compiler arrange for an
23758 expression with type-dependent subexpressions to have no type, so
23759 this function doesn't have to be fully recursive. */
23761 bool
23762 type_dependent_expression_p (tree expression)
23764 if (!processing_template_decl)
23765 return false;
23767 if (expression == NULL_TREE || expression == error_mark_node)
23768 return false;
23770 /* An unresolved name is always dependent. */
23771 if (identifier_p (expression)
23772 || TREE_CODE (expression) == USING_DECL
23773 || TREE_CODE (expression) == WILDCARD_DECL)
23774 return true;
23776 /* A fold expression is type-dependent. */
23777 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23778 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23779 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23780 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23781 return true;
23783 /* Some expression forms are never type-dependent. */
23784 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23785 || TREE_CODE (expression) == SIZEOF_EXPR
23786 || TREE_CODE (expression) == ALIGNOF_EXPR
23787 || TREE_CODE (expression) == AT_ENCODE_EXPR
23788 || TREE_CODE (expression) == NOEXCEPT_EXPR
23789 || TREE_CODE (expression) == TRAIT_EXPR
23790 || TREE_CODE (expression) == TYPEID_EXPR
23791 || TREE_CODE (expression) == DELETE_EXPR
23792 || TREE_CODE (expression) == VEC_DELETE_EXPR
23793 || TREE_CODE (expression) == THROW_EXPR
23794 || TREE_CODE (expression) == REQUIRES_EXPR)
23795 return false;
23797 /* The types of these expressions depends only on the type to which
23798 the cast occurs. */
23799 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23800 || TREE_CODE (expression) == STATIC_CAST_EXPR
23801 || TREE_CODE (expression) == CONST_CAST_EXPR
23802 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23803 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23804 || TREE_CODE (expression) == CAST_EXPR)
23805 return dependent_type_p (TREE_TYPE (expression));
23807 /* The types of these expressions depends only on the type created
23808 by the expression. */
23809 if (TREE_CODE (expression) == NEW_EXPR
23810 || TREE_CODE (expression) == VEC_NEW_EXPR)
23812 /* For NEW_EXPR tree nodes created inside a template, either
23813 the object type itself or a TREE_LIST may appear as the
23814 operand 1. */
23815 tree type = TREE_OPERAND (expression, 1);
23816 if (TREE_CODE (type) == TREE_LIST)
23817 /* This is an array type. We need to check array dimensions
23818 as well. */
23819 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23820 || value_dependent_expression_p
23821 (TREE_OPERAND (TREE_VALUE (type), 1));
23822 else
23823 return dependent_type_p (type);
23826 if (TREE_CODE (expression) == SCOPE_REF)
23828 tree scope = TREE_OPERAND (expression, 0);
23829 tree name = TREE_OPERAND (expression, 1);
23831 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23832 contains an identifier associated by name lookup with one or more
23833 declarations declared with a dependent type, or...a
23834 nested-name-specifier or qualified-id that names a member of an
23835 unknown specialization. */
23836 return (type_dependent_expression_p (name)
23837 || dependent_scope_p (scope));
23840 if (TREE_CODE (expression) == TEMPLATE_DECL
23841 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23842 return uses_outer_template_parms (expression);
23844 if (TREE_CODE (expression) == STMT_EXPR)
23845 expression = stmt_expr_value_expr (expression);
23847 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23849 tree elt;
23850 unsigned i;
23852 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23854 if (type_dependent_expression_p (elt))
23855 return true;
23857 return false;
23860 /* A static data member of the current instantiation with incomplete
23861 array type is type-dependent, as the definition and specializations
23862 can have different bounds. */
23863 if (VAR_P (expression)
23864 && DECL_CLASS_SCOPE_P (expression)
23865 && dependent_type_p (DECL_CONTEXT (expression))
23866 && VAR_HAD_UNKNOWN_BOUND (expression))
23867 return true;
23869 /* An array of unknown bound depending on a variadic parameter, eg:
23871 template<typename... Args>
23872 void foo (Args... args)
23874 int arr[] = { args... };
23877 template<int... vals>
23878 void bar ()
23880 int arr[] = { vals... };
23883 If the array has no length and has an initializer, it must be that
23884 we couldn't determine its length in cp_complete_array_type because
23885 it is dependent. */
23886 if (VAR_P (expression)
23887 && TREE_TYPE (expression) != NULL_TREE
23888 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23889 && !TYPE_DOMAIN (TREE_TYPE (expression))
23890 && DECL_INITIAL (expression))
23891 return true;
23893 /* A function or variable template-id is type-dependent if it has any
23894 dependent template arguments. Note that we only consider the innermost
23895 template arguments here, since those are the ones that come from the
23896 template-id; the template arguments for the enclosing class do not make it
23897 type-dependent, they only make a member function value-dependent. */
23898 if (VAR_OR_FUNCTION_DECL_P (expression)
23899 && DECL_LANG_SPECIFIC (expression)
23900 && DECL_TEMPLATE_INFO (expression)
23901 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23902 && (any_dependent_template_arguments_p
23903 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23904 return true;
23906 /* Always dependent, on the number of arguments if nothing else. */
23907 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23908 return true;
23910 if (TREE_TYPE (expression) == unknown_type_node)
23912 if (TREE_CODE (expression) == ADDR_EXPR)
23913 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23914 if (TREE_CODE (expression) == COMPONENT_REF
23915 || TREE_CODE (expression) == OFFSET_REF)
23917 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23918 return true;
23919 expression = TREE_OPERAND (expression, 1);
23920 if (identifier_p (expression))
23921 return false;
23923 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23924 if (TREE_CODE (expression) == SCOPE_REF)
23925 return false;
23927 if (BASELINK_P (expression))
23929 if (BASELINK_OPTYPE (expression)
23930 && dependent_type_p (BASELINK_OPTYPE (expression)))
23931 return true;
23932 expression = BASELINK_FUNCTIONS (expression);
23935 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23937 if (any_dependent_template_arguments_p
23938 (TREE_OPERAND (expression, 1)))
23939 return true;
23940 expression = TREE_OPERAND (expression, 0);
23941 if (identifier_p (expression))
23942 return true;
23945 gcc_assert (TREE_CODE (expression) == OVERLOAD
23946 || TREE_CODE (expression) == FUNCTION_DECL);
23948 while (expression)
23950 if (type_dependent_expression_p (OVL_CURRENT (expression)))
23951 return true;
23952 expression = OVL_NEXT (expression);
23954 return false;
23957 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23959 /* Dependent type attributes might not have made it from the decl to
23960 the type yet. */
23961 if (DECL_P (expression)
23962 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
23963 return true;
23965 return (dependent_type_p (TREE_TYPE (expression)));
23968 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23969 type-dependent if the expression refers to a member of the current
23970 instantiation and the type of the referenced member is dependent, or the
23971 class member access expression refers to a member of an unknown
23972 specialization.
23974 This function returns true if the OBJECT in such a class member access
23975 expression is of an unknown specialization. */
23977 bool
23978 type_dependent_object_expression_p (tree object)
23980 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
23981 dependent. */
23982 if (TREE_CODE (object) == IDENTIFIER_NODE)
23983 return true;
23984 tree scope = TREE_TYPE (object);
23985 return (!scope || dependent_scope_p (scope));
23988 /* walk_tree callback function for instantiation_dependent_expression_p,
23989 below. Returns non-zero if a dependent subexpression is found. */
23991 static tree
23992 instantiation_dependent_r (tree *tp, int *walk_subtrees,
23993 void * /*data*/)
23995 if (TYPE_P (*tp))
23997 /* We don't have to worry about decltype currently because decltype
23998 of an instantiation-dependent expr is a dependent type. This
23999 might change depending on the resolution of DR 1172. */
24000 *walk_subtrees = false;
24001 return NULL_TREE;
24003 enum tree_code code = TREE_CODE (*tp);
24004 switch (code)
24006 /* Don't treat an argument list as dependent just because it has no
24007 TREE_TYPE. */
24008 case TREE_LIST:
24009 case TREE_VEC:
24010 return NULL_TREE;
24012 case TEMPLATE_PARM_INDEX:
24013 return *tp;
24015 /* Handle expressions with type operands. */
24016 case SIZEOF_EXPR:
24017 case ALIGNOF_EXPR:
24018 case TYPEID_EXPR:
24019 case AT_ENCODE_EXPR:
24021 tree op = TREE_OPERAND (*tp, 0);
24022 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24023 op = TREE_TYPE (op);
24024 if (TYPE_P (op))
24026 if (dependent_type_p (op))
24027 return *tp;
24028 else
24030 *walk_subtrees = false;
24031 return NULL_TREE;
24034 break;
24037 case COMPONENT_REF:
24038 if (identifier_p (TREE_OPERAND (*tp, 1)))
24039 /* In a template, finish_class_member_access_expr creates a
24040 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24041 type-dependent, so that we can check access control at
24042 instantiation time (PR 42277). See also Core issue 1273. */
24043 return *tp;
24044 break;
24046 case SCOPE_REF:
24047 if (instantiation_dependent_scope_ref_p (*tp))
24048 return *tp;
24049 else
24050 break;
24052 /* Treat statement-expressions as dependent. */
24053 case BIND_EXPR:
24054 return *tp;
24056 /* Treat requires-expressions as dependent. */
24057 case REQUIRES_EXPR:
24058 return *tp;
24060 case CALL_EXPR:
24061 /* Treat calls to function concepts as dependent. */
24062 if (function_concept_check_p (*tp))
24063 return *tp;
24064 break;
24066 case TEMPLATE_ID_EXPR:
24067 /* And variable concepts. */
24068 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24069 return *tp;
24070 break;
24072 default:
24073 break;
24076 if (type_dependent_expression_p (*tp))
24077 return *tp;
24078 else
24079 return NULL_TREE;
24082 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24083 sense defined by the ABI:
24085 "An expression is instantiation-dependent if it is type-dependent
24086 or value-dependent, or it has a subexpression that is type-dependent
24087 or value-dependent."
24089 Except don't actually check value-dependence for unevaluated expressions,
24090 because in sizeof(i) we don't care about the value of i. Checking
24091 type-dependence will in turn check value-dependence of array bounds/template
24092 arguments as needed. */
24094 bool
24095 instantiation_dependent_uneval_expression_p (tree expression)
24097 tree result;
24099 if (!processing_template_decl)
24100 return false;
24102 if (expression == error_mark_node)
24103 return false;
24105 result = cp_walk_tree_without_duplicates (&expression,
24106 instantiation_dependent_r, NULL);
24107 return result != NULL_TREE;
24110 /* As above, but also check value-dependence of the expression as a whole. */
24112 bool
24113 instantiation_dependent_expression_p (tree expression)
24115 return (instantiation_dependent_uneval_expression_p (expression)
24116 || value_dependent_expression_p (expression));
24119 /* Like type_dependent_expression_p, but it also works while not processing
24120 a template definition, i.e. during substitution or mangling. */
24122 bool
24123 type_dependent_expression_p_push (tree expr)
24125 bool b;
24126 ++processing_template_decl;
24127 b = type_dependent_expression_p (expr);
24128 --processing_template_decl;
24129 return b;
24132 /* Returns TRUE if ARGS contains a type-dependent expression. */
24134 bool
24135 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24137 unsigned int i;
24138 tree arg;
24140 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24142 if (type_dependent_expression_p (arg))
24143 return true;
24145 return false;
24148 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24149 expressions) contains any type-dependent expressions. */
24151 bool
24152 any_type_dependent_elements_p (const_tree list)
24154 for (; list; list = TREE_CHAIN (list))
24155 if (type_dependent_expression_p (TREE_VALUE (list)))
24156 return true;
24158 return false;
24161 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24162 expressions) contains any value-dependent expressions. */
24164 bool
24165 any_value_dependent_elements_p (const_tree list)
24167 for (; list; list = TREE_CHAIN (list))
24168 if (value_dependent_expression_p (TREE_VALUE (list)))
24169 return true;
24171 return false;
24174 /* Returns TRUE if the ARG (a template argument) is dependent. */
24176 bool
24177 dependent_template_arg_p (tree arg)
24179 if (!processing_template_decl)
24180 return false;
24182 /* Assume a template argument that was wrongly written by the user
24183 is dependent. This is consistent with what
24184 any_dependent_template_arguments_p [that calls this function]
24185 does. */
24186 if (!arg || arg == error_mark_node)
24187 return true;
24189 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24190 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24192 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24193 return true;
24194 if (TREE_CODE (arg) == TEMPLATE_DECL)
24196 if (DECL_TEMPLATE_PARM_P (arg))
24197 return true;
24198 /* A member template of a dependent class is not necessarily
24199 type-dependent, but it is a dependent template argument because it
24200 will be a member of an unknown specialization to that template. */
24201 tree scope = CP_DECL_CONTEXT (arg);
24202 return TYPE_P (scope) && dependent_type_p (scope);
24204 else if (ARGUMENT_PACK_P (arg))
24206 tree args = ARGUMENT_PACK_ARGS (arg);
24207 int i, len = TREE_VEC_LENGTH (args);
24208 for (i = 0; i < len; ++i)
24210 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24211 return true;
24214 return false;
24216 else if (TYPE_P (arg))
24217 return dependent_type_p (arg);
24218 else
24219 return (type_dependent_expression_p (arg)
24220 || value_dependent_expression_p (arg));
24223 /* Returns true if ARGS (a collection of template arguments) contains
24224 any types that require structural equality testing. */
24226 bool
24227 any_template_arguments_need_structural_equality_p (tree args)
24229 int i;
24230 int j;
24232 if (!args)
24233 return false;
24234 if (args == error_mark_node)
24235 return true;
24237 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24239 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24240 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24242 tree arg = TREE_VEC_ELT (level, j);
24243 tree packed_args = NULL_TREE;
24244 int k, len = 1;
24246 if (ARGUMENT_PACK_P (arg))
24248 /* Look inside the argument pack. */
24249 packed_args = ARGUMENT_PACK_ARGS (arg);
24250 len = TREE_VEC_LENGTH (packed_args);
24253 for (k = 0; k < len; ++k)
24255 if (packed_args)
24256 arg = TREE_VEC_ELT (packed_args, k);
24258 if (error_operand_p (arg))
24259 return true;
24260 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24261 continue;
24262 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24263 return true;
24264 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24265 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24266 return true;
24271 return false;
24274 /* Returns true if ARGS (a collection of template arguments) contains
24275 any dependent arguments. */
24277 bool
24278 any_dependent_template_arguments_p (const_tree args)
24280 int i;
24281 int j;
24283 if (!args)
24284 return false;
24285 if (args == error_mark_node)
24286 return true;
24288 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24290 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24291 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24292 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24293 return true;
24296 return false;
24299 /* Returns TRUE if the template TMPL is type-dependent. */
24301 bool
24302 dependent_template_p (tree tmpl)
24304 if (TREE_CODE (tmpl) == OVERLOAD)
24306 while (tmpl)
24308 if (dependent_template_p (OVL_CURRENT (tmpl)))
24309 return true;
24310 tmpl = OVL_NEXT (tmpl);
24312 return false;
24315 /* Template template parameters are dependent. */
24316 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24317 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24318 return true;
24319 /* So are names that have not been looked up. */
24320 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24321 return true;
24322 return false;
24325 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24327 bool
24328 dependent_template_id_p (tree tmpl, tree args)
24330 return (dependent_template_p (tmpl)
24331 || any_dependent_template_arguments_p (args));
24334 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24335 are dependent. */
24337 bool
24338 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24340 int i;
24342 if (!processing_template_decl)
24343 return false;
24345 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24347 tree decl = TREE_VEC_ELT (declv, i);
24348 tree init = TREE_VEC_ELT (initv, i);
24349 tree cond = TREE_VEC_ELT (condv, i);
24350 tree incr = TREE_VEC_ELT (incrv, i);
24352 if (type_dependent_expression_p (decl)
24353 || TREE_CODE (decl) == SCOPE_REF)
24354 return true;
24356 if (init && type_dependent_expression_p (init))
24357 return true;
24359 if (type_dependent_expression_p (cond))
24360 return true;
24362 if (COMPARISON_CLASS_P (cond)
24363 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24364 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24365 return true;
24367 if (TREE_CODE (incr) == MODOP_EXPR)
24369 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24370 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24371 return true;
24373 else if (type_dependent_expression_p (incr))
24374 return true;
24375 else if (TREE_CODE (incr) == MODIFY_EXPR)
24377 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24378 return true;
24379 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24381 tree t = TREE_OPERAND (incr, 1);
24382 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24383 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24384 return true;
24389 return false;
24392 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24393 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24394 no such TYPE can be found. Note that this function peers inside
24395 uninstantiated templates and therefore should be used only in
24396 extremely limited situations. ONLY_CURRENT_P restricts this
24397 peering to the currently open classes hierarchy (which is required
24398 when comparing types). */
24400 tree
24401 resolve_typename_type (tree type, bool only_current_p)
24403 tree scope;
24404 tree name;
24405 tree decl;
24406 int quals;
24407 tree pushed_scope;
24408 tree result;
24410 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24412 scope = TYPE_CONTEXT (type);
24413 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24414 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24415 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24416 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24417 identifier of the TYPENAME_TYPE anymore.
24418 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24419 TYPENAME_TYPE instead, we avoid messing up with a possible
24420 typedef variant case. */
24421 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24423 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24424 it first before we can figure out what NAME refers to. */
24425 if (TREE_CODE (scope) == TYPENAME_TYPE)
24427 if (TYPENAME_IS_RESOLVING_P (scope))
24428 /* Given a class template A with a dependent base with nested type C,
24429 typedef typename A::C::C C will land us here, as trying to resolve
24430 the initial A::C leads to the local C typedef, which leads back to
24431 A::C::C. So we break the recursion now. */
24432 return type;
24433 else
24434 scope = resolve_typename_type (scope, only_current_p);
24436 /* If we don't know what SCOPE refers to, then we cannot resolve the
24437 TYPENAME_TYPE. */
24438 if (!CLASS_TYPE_P (scope))
24439 return type;
24440 /* If this is a typedef, we don't want to look inside (c++/11987). */
24441 if (typedef_variant_p (type))
24442 return type;
24443 /* If SCOPE isn't the template itself, it will not have a valid
24444 TYPE_FIELDS list. */
24445 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24446 /* scope is either the template itself or a compatible instantiation
24447 like X<T>, so look up the name in the original template. */
24448 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24449 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24450 gcc_checking_assert (uses_template_parms (scope));
24451 /* If scope has no fields, it can't be a current instantiation. Check this
24452 before currently_open_class to avoid infinite recursion (71515). */
24453 if (!TYPE_FIELDS (scope))
24454 return type;
24455 /* If the SCOPE is not the current instantiation, there's no reason
24456 to look inside it. */
24457 if (only_current_p && !currently_open_class (scope))
24458 return type;
24459 /* Enter the SCOPE so that name lookup will be resolved as if we
24460 were in the class definition. In particular, SCOPE will no
24461 longer be considered a dependent type. */
24462 pushed_scope = push_scope (scope);
24463 /* Look up the declaration. */
24464 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24465 tf_warning_or_error);
24467 result = NULL_TREE;
24469 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24470 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24471 if (!decl)
24472 /*nop*/;
24473 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24474 && TREE_CODE (decl) == TYPE_DECL)
24476 result = TREE_TYPE (decl);
24477 if (result == error_mark_node)
24478 result = NULL_TREE;
24480 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24481 && DECL_CLASS_TEMPLATE_P (decl))
24483 tree tmpl;
24484 tree args;
24485 /* Obtain the template and the arguments. */
24486 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24487 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24488 /* Instantiate the template. */
24489 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24490 /*entering_scope=*/0,
24491 tf_error | tf_user);
24492 if (result == error_mark_node)
24493 result = NULL_TREE;
24496 /* Leave the SCOPE. */
24497 if (pushed_scope)
24498 pop_scope (pushed_scope);
24500 /* If we failed to resolve it, return the original typename. */
24501 if (!result)
24502 return type;
24504 /* If lookup found a typename type, resolve that too. */
24505 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24507 /* Ill-formed programs can cause infinite recursion here, so we
24508 must catch that. */
24509 TYPENAME_IS_RESOLVING_P (result) = 1;
24510 result = resolve_typename_type (result, only_current_p);
24511 TYPENAME_IS_RESOLVING_P (result) = 0;
24514 /* Qualify the resulting type. */
24515 quals = cp_type_quals (type);
24516 if (quals)
24517 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24519 return result;
24522 /* EXPR is an expression which is not type-dependent. Return a proxy
24523 for EXPR that can be used to compute the types of larger
24524 expressions containing EXPR. */
24526 tree
24527 build_non_dependent_expr (tree expr)
24529 tree inner_expr;
24531 /* When checking, try to get a constant value for all non-dependent
24532 expressions in order to expose bugs in *_dependent_expression_p
24533 and constexpr. This can affect code generation, see PR70704, so
24534 only do this for -fchecking=2. */
24535 if (flag_checking > 1
24536 && cxx_dialect >= cxx11
24537 /* Don't do this during nsdmi parsing as it can lead to
24538 unexpected recursive instantiations. */
24539 && !parsing_nsdmi ()
24540 /* Don't do this during concept expansion either and for
24541 the same reason. */
24542 && !expanding_concept ())
24543 fold_non_dependent_expr (expr);
24545 /* Preserve OVERLOADs; the functions must be available to resolve
24546 types. */
24547 inner_expr = expr;
24548 if (TREE_CODE (inner_expr) == STMT_EXPR)
24549 inner_expr = stmt_expr_value_expr (inner_expr);
24550 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24551 inner_expr = TREE_OPERAND (inner_expr, 0);
24552 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24553 inner_expr = TREE_OPERAND (inner_expr, 1);
24554 if (is_overloaded_fn (inner_expr)
24555 || TREE_CODE (inner_expr) == OFFSET_REF)
24556 return expr;
24557 /* There is no need to return a proxy for a variable. */
24558 if (VAR_P (expr))
24559 return expr;
24560 /* Preserve string constants; conversions from string constants to
24561 "char *" are allowed, even though normally a "const char *"
24562 cannot be used to initialize a "char *". */
24563 if (TREE_CODE (expr) == STRING_CST)
24564 return expr;
24565 /* Preserve void and arithmetic constants, as an optimization -- there is no
24566 reason to create a new node. */
24567 if (TREE_CODE (expr) == VOID_CST
24568 || TREE_CODE (expr) == INTEGER_CST
24569 || TREE_CODE (expr) == REAL_CST)
24570 return expr;
24571 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24572 There is at least one place where we want to know that a
24573 particular expression is a throw-expression: when checking a ?:
24574 expression, there are special rules if the second or third
24575 argument is a throw-expression. */
24576 if (TREE_CODE (expr) == THROW_EXPR)
24577 return expr;
24579 /* Don't wrap an initializer list, we need to be able to look inside. */
24580 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24581 return expr;
24583 /* Don't wrap a dummy object, we need to be able to test for it. */
24584 if (is_dummy_object (expr))
24585 return expr;
24587 if (TREE_CODE (expr) == COND_EXPR)
24588 return build3 (COND_EXPR,
24589 TREE_TYPE (expr),
24590 TREE_OPERAND (expr, 0),
24591 (TREE_OPERAND (expr, 1)
24592 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24593 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24594 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24595 if (TREE_CODE (expr) == COMPOUND_EXPR
24596 && !COMPOUND_EXPR_OVERLOADED (expr))
24597 return build2 (COMPOUND_EXPR,
24598 TREE_TYPE (expr),
24599 TREE_OPERAND (expr, 0),
24600 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24602 /* If the type is unknown, it can't really be non-dependent */
24603 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24605 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24606 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24609 /* ARGS is a vector of expressions as arguments to a function call.
24610 Replace the arguments with equivalent non-dependent expressions.
24611 This modifies ARGS in place. */
24613 void
24614 make_args_non_dependent (vec<tree, va_gc> *args)
24616 unsigned int ix;
24617 tree arg;
24619 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24621 tree newarg = build_non_dependent_expr (arg);
24622 if (newarg != arg)
24623 (*args)[ix] = newarg;
24627 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24628 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24629 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24631 static tree
24632 make_auto_1 (tree name, bool set_canonical)
24634 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24635 TYPE_NAME (au) = build_decl (input_location,
24636 TYPE_DECL, name, au);
24637 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24638 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24639 (0, processing_template_decl + 1, processing_template_decl + 1,
24640 TYPE_NAME (au), NULL_TREE);
24641 if (set_canonical)
24642 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24643 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24644 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24646 return au;
24649 tree
24650 make_decltype_auto (void)
24652 return make_auto_1 (decltype_auto_identifier, true);
24655 tree
24656 make_auto (void)
24658 return make_auto_1 (auto_identifier, true);
24661 /* Return a C++17 deduction placeholder for class template TMPL. */
24663 tree
24664 make_template_placeholder (tree tmpl)
24666 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24667 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24668 return t;
24671 /* Make a "constrained auto" type-specifier. This is an
24672 auto type with constraints that must be associated after
24673 deduction. The constraint is formed from the given
24674 CONC and its optional sequence of arguments, which are
24675 non-null if written as partial-concept-id. */
24677 tree
24678 make_constrained_auto (tree con, tree args)
24680 tree type = make_auto_1 (auto_identifier, false);
24682 /* Build the constraint. */
24683 tree tmpl = DECL_TI_TEMPLATE (con);
24684 tree expr;
24685 if (VAR_P (con))
24686 expr = build_concept_check (tmpl, type, args);
24687 else
24688 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
24690 tree constr = normalize_expression (expr);
24691 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24693 /* Our canonical type depends on the constraint. */
24694 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24696 /* Attach the constraint to the type declaration. */
24697 tree decl = TYPE_NAME (type);
24698 return decl;
24701 /* Given type ARG, return std::initializer_list<ARG>. */
24703 static tree
24704 listify (tree arg)
24706 tree std_init_list = namespace_binding
24707 (get_identifier ("initializer_list"), std_node);
24708 tree argvec;
24709 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24711 error ("deducing from brace-enclosed initializer list requires "
24712 "#include <initializer_list>");
24713 return error_mark_node;
24715 argvec = make_tree_vec (1);
24716 TREE_VEC_ELT (argvec, 0) = arg;
24717 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24718 NULL_TREE, 0, tf_warning_or_error);
24721 /* Replace auto in TYPE with std::initializer_list<auto>. */
24723 static tree
24724 listify_autos (tree type, tree auto_node)
24726 tree init_auto = listify (auto_node);
24727 tree argvec = make_tree_vec (1);
24728 TREE_VEC_ELT (argvec, 0) = init_auto;
24729 if (processing_template_decl)
24730 argvec = add_to_template_args (current_template_args (), argvec);
24731 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24734 /* Hash traits for hashing possibly constrained 'auto'
24735 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24737 struct auto_hash : default_hash_traits<tree>
24739 static inline hashval_t hash (tree);
24740 static inline bool equal (tree, tree);
24743 /* Hash the 'auto' T. */
24745 inline hashval_t
24746 auto_hash::hash (tree t)
24748 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24749 /* Matching constrained-type-specifiers denote the same template
24750 parameter, so hash the constraint. */
24751 return hash_placeholder_constraint (c);
24752 else
24753 /* But unconstrained autos are all separate, so just hash the pointer. */
24754 return iterative_hash_object (t, 0);
24757 /* Compare two 'auto's. */
24759 inline bool
24760 auto_hash::equal (tree t1, tree t2)
24762 if (t1 == t2)
24763 return true;
24765 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24766 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24768 /* Two unconstrained autos are distinct. */
24769 if (!c1 || !c2)
24770 return false;
24772 return equivalent_placeholder_constraints (c1, c2);
24775 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24776 constrained) auto, add it to the vector. */
24778 static int
24779 extract_autos_r (tree t, void *data)
24781 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24782 if (is_auto_or_concept (t))
24784 /* All the autos were built with index 0; fix that up now. */
24785 tree *p = hash.find_slot (t, INSERT);
24786 unsigned idx;
24787 if (*p)
24788 /* If this is a repeated constrained-type-specifier, use the index we
24789 chose before. */
24790 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24791 else
24793 /* Otherwise this is new, so use the current count. */
24794 *p = t;
24795 idx = hash.elements () - 1;
24797 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24800 /* Always keep walking. */
24801 return 0;
24804 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24805 says they can appear anywhere in the type. */
24807 static tree
24808 extract_autos (tree type)
24810 hash_set<tree> visited;
24811 hash_table<auto_hash> hash (2);
24813 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24815 tree tree_vec = make_tree_vec (hash.elements());
24816 for (hash_table<auto_hash>::iterator iter = hash.begin();
24817 iter != hash.end(); ++iter)
24819 tree elt = *iter;
24820 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24821 TREE_VEC_ELT (tree_vec, i)
24822 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24825 return tree_vec;
24828 /* The stem for deduction guide names. */
24829 const char *const dguide_base = "__dguide_";
24831 /* Return the name for a deduction guide for class template TMPL. */
24833 tree
24834 dguide_name (tree tmpl)
24836 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24837 tree tname = TYPE_IDENTIFIER (type);
24838 char *buf = (char *) alloca (1 + strlen (dguide_base)
24839 + IDENTIFIER_LENGTH (tname));
24840 memcpy (buf, dguide_base, strlen (dguide_base));
24841 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24842 IDENTIFIER_LENGTH (tname) + 1);
24843 tree dname = get_identifier (buf);
24844 TREE_TYPE (dname) = type;
24845 return dname;
24848 /* True if NAME is the name of a deduction guide. */
24850 bool
24851 dguide_name_p (tree name)
24853 return (TREE_TYPE (name)
24854 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24855 strlen (dguide_base)));
24858 /* True if FN is a deduction guide. */
24860 bool
24861 deduction_guide_p (const_tree fn)
24863 if (DECL_P (fn))
24864 if (tree name = DECL_NAME (fn))
24865 return dguide_name_p (name);
24866 return false;
24869 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
24871 bool
24872 copy_guide_p (const_tree fn)
24874 gcc_assert (deduction_guide_p (fn));
24875 if (!DECL_ARTIFICIAL (fn))
24876 return false;
24877 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
24878 return (TREE_CHAIN (parms) == void_list_node
24879 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
24882 /* True if FN is a guide generated from a constructor template. */
24884 bool
24885 template_guide_p (const_tree fn)
24887 gcc_assert (deduction_guide_p (fn));
24888 if (!DECL_ARTIFICIAL (fn))
24889 return false;
24890 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
24892 tree tmpl = DECL_TI_TEMPLATE (ctor);
24893 return PRIMARY_TEMPLATE_P (tmpl);
24895 return false;
24898 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24899 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24900 template parameter types. Note that the handling of template template
24901 parameters relies on current_template_parms being set appropriately for the
24902 new template. */
24904 static tree
24905 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
24906 tree tsubst_args, tsubst_flags_t complain)
24908 tree oldidx = get_template_parm_index (olddecl);
24910 tree newtype;
24911 if (TREE_CODE (olddecl) == TYPE_DECL
24912 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24914 tree oldtype = TREE_TYPE (olddecl);
24915 newtype = cxx_make_type (TREE_CODE (oldtype));
24916 TYPE_MAIN_VARIANT (newtype) = newtype;
24917 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
24918 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
24919 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
24921 else
24922 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
24923 complain, NULL_TREE);
24925 tree newdecl
24926 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
24927 DECL_NAME (olddecl), newtype);
24928 SET_DECL_TEMPLATE_PARM_P (newdecl);
24930 tree newidx;
24931 if (TREE_CODE (olddecl) == TYPE_DECL
24932 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24934 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
24935 = build_template_parm_index (index, level, level,
24936 newdecl, newtype);
24937 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
24938 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
24940 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
24942 DECL_TEMPLATE_RESULT (newdecl)
24943 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
24944 DECL_NAME (olddecl), newtype);
24945 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
24946 // First create a copy (ttargs) of tsubst_args with an
24947 // additional level for the template template parameter's own
24948 // template parameters (ttparms).
24949 tree ttparms = (INNERMOST_TEMPLATE_PARMS
24950 (DECL_TEMPLATE_PARMS (olddecl)));
24951 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
24952 tree ttargs = make_tree_vec (depth + 1);
24953 for (int i = 0; i < depth; ++i)
24954 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
24955 TREE_VEC_ELT (ttargs, depth)
24956 = template_parms_level_to_args (ttparms);
24957 // Substitute ttargs into ttparms to fix references to
24958 // other template parameters.
24959 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24960 complain);
24961 // Now substitute again with args based on tparms, to reduce
24962 // the level of the ttparms.
24963 ttargs = current_template_args ();
24964 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24965 complain);
24966 // Finally, tack the adjusted parms onto tparms.
24967 ttparms = tree_cons (size_int (depth), ttparms,
24968 current_template_parms);
24969 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
24972 else
24974 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
24975 tree newconst
24976 = build_decl (DECL_SOURCE_LOCATION (oldconst),
24977 TREE_CODE (oldconst),
24978 DECL_NAME (oldconst), newtype);
24979 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
24980 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
24981 SET_DECL_TEMPLATE_PARM_P (newconst);
24982 newidx = build_template_parm_index (index, level, level,
24983 newconst, newtype);
24984 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
24987 TEMPLATE_PARM_PARAMETER_PACK (newidx)
24988 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
24989 return newdecl;
24992 /* Returns a C++17 class deduction guide template based on the constructor
24993 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
24994 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
24996 static tree
24997 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
24999 tree type, tparms, targs, fparms, fargs, ci;
25000 bool memtmpl = false;
25001 bool explicit_p;
25002 location_t loc;
25004 if (TYPE_P (ctor))
25006 type = ctor;
25007 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25008 if (copy_p)
25010 type = TREE_TYPE (type);
25011 fparms = tree_cons (NULL_TREE, type, void_list_node);
25013 else
25014 fparms = void_list_node;
25016 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25017 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25018 targs = CLASSTYPE_TI_ARGS (type);
25019 ci = NULL_TREE;
25020 fargs = NULL_TREE;
25021 loc = DECL_SOURCE_LOCATION (ctmpl);
25022 explicit_p = false;
25024 else
25026 if (outer_args)
25027 ctor = tsubst (ctor, outer_args, complain, ctor);
25028 type = DECL_CONTEXT (ctor);
25029 tree fn_tmpl;
25030 if (TREE_CODE (ctor) == TEMPLATE_DECL)
25032 fn_tmpl = ctor;
25033 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25035 else
25036 fn_tmpl = DECL_TI_TEMPLATE (ctor);
25038 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25039 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25040 fully specialized args for the enclosing class. Strip those off, as
25041 the deduction guide won't have those template parameters. */
25042 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25043 TMPL_PARMS_DEPTH (tparms));
25044 /* Discard the 'this' parameter. */
25045 fparms = FUNCTION_ARG_CHAIN (ctor);
25046 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25047 ci = get_constraints (ctor);
25048 loc = DECL_SOURCE_LOCATION (ctor);
25049 explicit_p = DECL_NONCONVERTING_P (ctor);
25051 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25053 memtmpl = true;
25055 /* For a member template constructor, we need to flatten the two
25056 template parameter lists into one, and then adjust the function
25057 signature accordingly. This gets...complicated. */
25058 ++processing_template_decl;
25059 tree save_parms = current_template_parms;
25061 /* For a member template we should have two levels of parms/args, one
25062 for the class and one for the constructor. We stripped
25063 specialized args for further enclosing classes above. */
25064 const int depth = 2;
25065 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25067 /* Template args for translating references to the two-level template
25068 parameters into references to the one-level template parameters we
25069 are creating. */
25070 tree tsubst_args = copy_node (targs);
25071 TMPL_ARGS_LEVEL (tsubst_args, depth)
25072 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25074 /* Template parms for the constructor template. */
25075 tree ftparms = TREE_VALUE (tparms);
25076 unsigned flen = TREE_VEC_LENGTH (ftparms);
25077 /* Template parms for the class template. */
25078 tparms = TREE_CHAIN (tparms);
25079 tree ctparms = TREE_VALUE (tparms);
25080 unsigned clen = TREE_VEC_LENGTH (ctparms);
25081 /* Template parms for the deduction guide start as a copy of the
25082 template parms for the class. We set current_template_parms for
25083 lookup_template_class_1. */
25084 current_template_parms = tparms = copy_node (tparms);
25085 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25086 for (unsigned i = 0; i < clen; ++i)
25087 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25089 /* Now we need to rewrite the constructor parms to append them to the
25090 class parms. */
25091 for (unsigned i = 0; i < flen; ++i)
25093 unsigned index = i + clen;
25094 unsigned level = 1;
25095 tree oldelt = TREE_VEC_ELT (ftparms, i);
25096 tree olddecl = TREE_VALUE (oldelt);
25097 tree newdecl = rewrite_template_parm (olddecl, index, level,
25098 tsubst_args, complain);
25099 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25100 tsubst_args, complain, ctor);
25101 tree list = build_tree_list (newdef, newdecl);
25102 TEMPLATE_PARM_CONSTRAINTS (list)
25103 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25104 tsubst_args, complain, ctor);
25105 TREE_VEC_ELT (new_vec, index) = list;
25106 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25109 /* Now we have a final set of template parms to substitute into the
25110 function signature. */
25111 targs = template_parms_to_args (tparms);
25112 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25113 complain, ctor);
25114 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25115 if (ci)
25116 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25118 current_template_parms = save_parms;
25119 --processing_template_decl;
25123 if (!memtmpl)
25125 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25126 tparms = copy_node (tparms);
25127 INNERMOST_TEMPLATE_PARMS (tparms)
25128 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25131 tree fntype = build_function_type (type, fparms);
25132 tree ded_fn = build_lang_decl_loc (loc,
25133 FUNCTION_DECL,
25134 dguide_name (type), fntype);
25135 DECL_ARGUMENTS (ded_fn) = fargs;
25136 DECL_ARTIFICIAL (ded_fn) = true;
25137 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25138 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25139 DECL_ARTIFICIAL (ded_tmpl) = true;
25140 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25141 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25142 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25143 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25144 if (DECL_P (ctor))
25145 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25146 if (ci)
25147 set_constraints (ded_tmpl, ci);
25149 return ded_tmpl;
25152 /* Deduce template arguments for the class template placeholder PTYPE for
25153 template TMPL based on the initializer INIT, and return the resulting
25154 type. */
25156 static tree
25157 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25158 tsubst_flags_t complain)
25160 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25162 /* We should have handled this in the caller. */
25163 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25164 return ptype;
25165 if (complain & tf_error)
25166 error ("non-class template %qT used without template arguments", tmpl);
25167 return error_mark_node;
25170 tree type = TREE_TYPE (tmpl);
25172 vec<tree,va_gc> *args;
25173 if (init == NULL_TREE
25174 || TREE_CODE (init) == TREE_LIST)
25175 args = make_tree_vector_from_list (init);
25176 else if (BRACE_ENCLOSED_INITIALIZER_P (init)
25177 && !TYPE_HAS_LIST_CTOR (type)
25178 && !is_std_init_list (type))
25179 args = make_tree_vector_from_ctor (init);
25180 else
25181 args = make_tree_vector_single (init);
25183 tree dname = dguide_name (tmpl);
25184 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25185 /*type*/false, /*complain*/false,
25186 /*hidden*/false);
25187 if (cands == error_mark_node)
25188 cands = NULL_TREE;
25190 tree outer_args = NULL_TREE;
25191 if (DECL_CLASS_SCOPE_P (tmpl)
25192 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25194 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25195 type = TREE_TYPE (most_general_template (tmpl));
25198 bool saw_ctor = false;
25199 if (CLASSTYPE_METHOD_VEC (type))
25200 // FIXME cache artificial deduction guides
25201 for (tree fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
25203 tree fn = OVL_CURRENT (fns);
25204 tree guide = build_deduction_guide (fn, outer_args, complain);
25205 cands = ovl_cons (guide, cands);
25207 saw_ctor = true;
25210 if (!saw_ctor && args->length() == 0)
25212 tree guide = build_deduction_guide (type, outer_args, complain);
25213 cands = ovl_cons (guide, cands);
25215 if (args->length() == 1)
25217 tree guide = build_deduction_guide (build_reference_type (type),
25218 outer_args, complain);
25219 cands = ovl_cons (guide, cands);
25222 /* Prune explicit deduction guides in copy-initialization context. */
25223 tree old_cands = cands;
25224 if (flags & LOOKUP_ONLYCONVERTING)
25226 tree t = cands;
25227 for (; t; t = OVL_NEXT (t))
25228 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (OVL_CURRENT (t))))
25229 break;
25230 if (t)
25232 tree pruned = NULL_TREE;
25233 for (t = cands; t; t = OVL_NEXT (t))
25235 tree f = OVL_CURRENT (t);
25236 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (f)))
25237 pruned = build_overload (f, pruned);
25239 cands = pruned;
25240 if (cands == NULL_TREE)
25242 error ("cannot deduce template arguments for copy-initialization"
25243 " of %qT, as it has no non-explicit deduction guides or "
25244 "user-declared constructors", type);
25245 return error_mark_node;
25250 ++cp_unevaluated_operand;
25251 tree t = build_new_function_call (cands, &args, /*koenig*/false,
25252 tf_decltype);
25254 if (t == error_mark_node && (complain & tf_warning_or_error))
25256 error ("class template argument deduction failed:");
25257 t = build_new_function_call (cands, &args, /*koenig*/false,
25258 complain | tf_decltype);
25259 if (old_cands != cands)
25260 inform (input_location, "explicit deduction guides not considered "
25261 "for copy-initialization");
25264 --cp_unevaluated_operand;
25265 release_tree_vector (args);
25267 return TREE_TYPE (t);
25270 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25271 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25273 tree
25274 do_auto_deduction (tree type, tree init, tree auto_node)
25276 return do_auto_deduction (type, init, auto_node,
25277 tf_warning_or_error,
25278 adc_unspecified);
25281 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25282 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25283 The CONTEXT determines the context in which auto deduction is performed
25284 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25285 OUTER_TARGS are used during template argument deduction
25286 (context == adc_unify) to properly substitute the result, and is ignored
25287 in other contexts.
25289 For partial-concept-ids, extra args may be appended to the list of deduced
25290 template arguments prior to determining constraint satisfaction. */
25292 tree
25293 do_auto_deduction (tree type, tree init, tree auto_node,
25294 tsubst_flags_t complain, auto_deduction_context context,
25295 tree outer_targs, int flags)
25297 tree targs;
25299 if (init == error_mark_node)
25300 return error_mark_node;
25302 if (init && type_dependent_expression_p (init)
25303 && context != adc_unify)
25304 /* Defining a subset of type-dependent expressions that we can deduce
25305 from ahead of time isn't worth the trouble. */
25306 return type;
25308 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25309 /* C++17 class template argument deduction. */
25310 return do_class_deduction (type, tmpl, init, flags, complain);
25312 if (TREE_TYPE (init) == NULL_TREE)
25313 /* Nothing we can do with this, even in deduction context. */
25314 return type;
25316 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25317 with either a new invented type template parameter U or, if the
25318 initializer is a braced-init-list (8.5.4), with
25319 std::initializer_list<U>. */
25320 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25322 if (!DIRECT_LIST_INIT_P (init))
25323 type = listify_autos (type, auto_node);
25324 else if (CONSTRUCTOR_NELTS (init) == 1)
25325 init = CONSTRUCTOR_ELT (init, 0)->value;
25326 else
25328 if (complain & tf_warning_or_error)
25330 if (permerror (input_location, "direct-list-initialization of "
25331 "%<auto%> requires exactly one element"))
25332 inform (input_location,
25333 "for deduction to %<std::initializer_list%>, use copy-"
25334 "list-initialization (i.e. add %<=%> before the %<{%>)");
25336 type = listify_autos (type, auto_node);
25340 if (type == error_mark_node)
25341 return error_mark_node;
25343 init = resolve_nondeduced_context (init, complain);
25345 if (context == adc_decomp_type
25346 && auto_node == type
25347 && init != error_mark_node
25348 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25349 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25350 and initializer has array type, deduce cv-qualified array type. */
25351 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25352 complain);
25353 else if (AUTO_IS_DECLTYPE (auto_node))
25355 bool id = (DECL_P (init)
25356 || ((TREE_CODE (init) == COMPONENT_REF
25357 || TREE_CODE (init) == SCOPE_REF)
25358 && !REF_PARENTHESIZED_P (init)));
25359 targs = make_tree_vec (1);
25360 TREE_VEC_ELT (targs, 0)
25361 = finish_decltype_type (init, id, tf_warning_or_error);
25362 if (type != auto_node)
25364 if (complain & tf_error)
25365 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25366 return error_mark_node;
25369 else
25371 tree parms = build_tree_list (NULL_TREE, type);
25372 tree tparms;
25374 if (flag_concepts)
25375 tparms = extract_autos (type);
25376 else
25378 tparms = make_tree_vec (1);
25379 TREE_VEC_ELT (tparms, 0)
25380 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25383 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25384 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25385 DEDUCE_CALL, LOOKUP_NORMAL,
25386 NULL, /*explain_p=*/false);
25387 if (val > 0)
25389 if (processing_template_decl)
25390 /* Try again at instantiation time. */
25391 return type;
25392 if (type && type != error_mark_node
25393 && (complain & tf_error))
25394 /* If type is error_mark_node a diagnostic must have been
25395 emitted by now. Also, having a mention to '<type error>'
25396 in the diagnostic is not really useful to the user. */
25398 if (cfun && auto_node == current_function_auto_return_pattern
25399 && LAMBDA_FUNCTION_P (current_function_decl))
25400 error ("unable to deduce lambda return type from %qE", init);
25401 else
25402 error ("unable to deduce %qT from %qE", type, init);
25403 type_unification_real (tparms, targs, parms, &init, 1, 0,
25404 DEDUCE_CALL, LOOKUP_NORMAL,
25405 NULL, /*explain_p=*/true);
25407 return error_mark_node;
25411 /* Check any placeholder constraints against the deduced type. */
25412 if (flag_concepts && !processing_template_decl)
25413 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25415 /* Use the deduced type to check the associated constraints. If we
25416 have a partial-concept-id, rebuild the argument list so that
25417 we check using the extra arguments. */
25418 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25419 tree cargs = CHECK_CONSTR_ARGS (constr);
25420 if (TREE_VEC_LENGTH (cargs) > 1)
25422 cargs = copy_node (cargs);
25423 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25425 else
25426 cargs = targs;
25427 if (!constraints_satisfied_p (constr, cargs))
25429 if (complain & tf_warning_or_error)
25431 switch (context)
25433 case adc_unspecified:
25434 case adc_unify:
25435 error("placeholder constraints not satisfied");
25436 break;
25437 case adc_variable_type:
25438 case adc_decomp_type:
25439 error ("deduced initializer does not satisfy "
25440 "placeholder constraints");
25441 break;
25442 case adc_return_type:
25443 error ("deduced return type does not satisfy "
25444 "placeholder constraints");
25445 break;
25446 case adc_requirement:
25447 error ("deduced expression type does not satisfy "
25448 "placeholder constraints");
25449 break;
25451 diagnose_constraints (input_location, constr, targs);
25453 return error_mark_node;
25457 if (processing_template_decl && context != adc_unify)
25458 outer_targs = current_template_args ();
25459 targs = add_to_template_args (outer_targs, targs);
25460 return tsubst (type, targs, complain, NULL_TREE);
25463 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25464 result. */
25466 tree
25467 splice_late_return_type (tree type, tree late_return_type)
25469 if (is_auto (type))
25471 if (late_return_type)
25472 return late_return_type;
25474 tree idx = get_template_parm_index (type);
25475 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25476 /* In an abbreviated function template we didn't know we were dealing
25477 with a function template when we saw the auto return type, so update
25478 it to have the correct level. */
25479 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25481 return type;
25484 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25485 'decltype(auto)' or a deduced class template. */
25487 bool
25488 is_auto (const_tree type)
25490 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25491 && (TYPE_IDENTIFIER (type) == auto_identifier
25492 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25493 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25494 return true;
25495 else
25496 return false;
25499 /* for_each_template_parm callback for type_uses_auto. */
25502 is_auto_r (tree tp, void */*data*/)
25504 return is_auto_or_concept (tp);
25507 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25508 a use of `auto'. Returns NULL_TREE otherwise. */
25510 tree
25511 type_uses_auto (tree type)
25513 if (type == NULL_TREE)
25514 return NULL_TREE;
25515 else if (flag_concepts)
25517 /* The Concepts TS allows multiple autos in one type-specifier; just
25518 return the first one we find, do_auto_deduction will collect all of
25519 them. */
25520 if (uses_template_parms (type))
25521 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25522 /*visited*/NULL, /*nondeduced*/true);
25523 else
25524 return NULL_TREE;
25526 else
25527 return find_type_usage (type, is_auto);
25530 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
25531 'decltype(auto)' or a concept. */
25533 bool
25534 is_auto_or_concept (const_tree type)
25536 return is_auto (type); // or concept
25539 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
25540 a concept identifier) iff TYPE contains a use of a generic type. Returns
25541 NULL_TREE otherwise. */
25543 tree
25544 type_uses_auto_or_concept (tree type)
25546 return find_type_usage (type, is_auto_or_concept);
25550 /* For a given template T, return the vector of typedefs referenced
25551 in T for which access check is needed at T instantiation time.
25552 T is either a FUNCTION_DECL or a RECORD_TYPE.
25553 Those typedefs were added to T by the function
25554 append_type_to_template_for_access_check. */
25556 vec<qualified_typedef_usage_t, va_gc> *
25557 get_types_needing_access_check (tree t)
25559 tree ti;
25560 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25562 if (!t || t == error_mark_node)
25563 return NULL;
25565 if (!(ti = get_template_info (t)))
25566 return NULL;
25568 if (CLASS_TYPE_P (t)
25569 || TREE_CODE (t) == FUNCTION_DECL)
25571 if (!TI_TEMPLATE (ti))
25572 return NULL;
25574 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25577 return result;
25580 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25581 tied to T. That list of typedefs will be access checked at
25582 T instantiation time.
25583 T is either a FUNCTION_DECL or a RECORD_TYPE.
25584 TYPE_DECL is a TYPE_DECL node representing a typedef.
25585 SCOPE is the scope through which TYPE_DECL is accessed.
25586 LOCATION is the location of the usage point of TYPE_DECL.
25588 This function is a subroutine of
25589 append_type_to_template_for_access_check. */
25591 static void
25592 append_type_to_template_for_access_check_1 (tree t,
25593 tree type_decl,
25594 tree scope,
25595 location_t location)
25597 qualified_typedef_usage_t typedef_usage;
25598 tree ti;
25600 if (!t || t == error_mark_node)
25601 return;
25603 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25604 || CLASS_TYPE_P (t))
25605 && type_decl
25606 && TREE_CODE (type_decl) == TYPE_DECL
25607 && scope);
25609 if (!(ti = get_template_info (t)))
25610 return;
25612 gcc_assert (TI_TEMPLATE (ti));
25614 typedef_usage.typedef_decl = type_decl;
25615 typedef_usage.context = scope;
25616 typedef_usage.locus = location;
25618 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25621 /* Append TYPE_DECL to the template TEMPL.
25622 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25623 At TEMPL instanciation time, TYPE_DECL will be checked to see
25624 if it can be accessed through SCOPE.
25625 LOCATION is the location of the usage point of TYPE_DECL.
25627 e.g. consider the following code snippet:
25629 class C
25631 typedef int myint;
25634 template<class U> struct S
25636 C::myint mi; // <-- usage point of the typedef C::myint
25639 S<char> s;
25641 At S<char> instantiation time, we need to check the access of C::myint
25642 In other words, we need to check the access of the myint typedef through
25643 the C scope. For that purpose, this function will add the myint typedef
25644 and the scope C through which its being accessed to a list of typedefs
25645 tied to the template S. That list will be walked at template instantiation
25646 time and access check performed on each typedefs it contains.
25647 Note that this particular code snippet should yield an error because
25648 myint is private to C. */
25650 void
25651 append_type_to_template_for_access_check (tree templ,
25652 tree type_decl,
25653 tree scope,
25654 location_t location)
25656 qualified_typedef_usage_t *iter;
25657 unsigned i;
25659 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25661 /* Make sure we don't append the type to the template twice. */
25662 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25663 if (iter->typedef_decl == type_decl && scope == iter->context)
25664 return;
25666 append_type_to_template_for_access_check_1 (templ, type_decl,
25667 scope, location);
25670 /* Convert the generic type parameters in PARM that match the types given in the
25671 range [START_IDX, END_IDX) from the current_template_parms into generic type
25672 packs. */
25674 tree
25675 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25677 tree current = current_template_parms;
25678 int depth = TMPL_PARMS_DEPTH (current);
25679 current = INNERMOST_TEMPLATE_PARMS (current);
25680 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25682 for (int i = 0; i < start_idx; ++i)
25683 TREE_VEC_ELT (replacement, i)
25684 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25686 for (int i = start_idx; i < end_idx; ++i)
25688 /* Create a distinct parameter pack type from the current parm and add it
25689 to the replacement args to tsubst below into the generic function
25690 parameter. */
25692 tree o = TREE_TYPE (TREE_VALUE
25693 (TREE_VEC_ELT (current, i)));
25694 tree t = copy_type (o);
25695 TEMPLATE_TYPE_PARM_INDEX (t)
25696 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25697 o, 0, 0, tf_none);
25698 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25699 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25700 TYPE_MAIN_VARIANT (t) = t;
25701 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25702 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25703 TREE_VEC_ELT (replacement, i) = t;
25704 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25707 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25708 TREE_VEC_ELT (replacement, i)
25709 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25711 /* If there are more levels then build up the replacement with the outer
25712 template parms. */
25713 if (depth > 1)
25714 replacement = add_to_template_args (template_parms_to_args
25715 (TREE_CHAIN (current_template_parms)),
25716 replacement);
25718 return tsubst (parm, replacement, tf_none, NULL_TREE);
25721 /* Entries in the decl_constraint hash table. */
25722 struct GTY((for_user)) constr_entry
25724 tree decl;
25725 tree ci;
25728 /* Hashing function and equality for constraint entries. */
25729 struct constr_hasher : ggc_ptr_hash<constr_entry>
25731 static hashval_t hash (constr_entry *e)
25733 return (hashval_t)DECL_UID (e->decl);
25736 static bool equal (constr_entry *e1, constr_entry *e2)
25738 return e1->decl == e2->decl;
25742 /* A mapping from declarations to constraint information. Note that
25743 both templates and their underlying declarations are mapped to the
25744 same constraint information.
25746 FIXME: This is defined in pt.c because garbage collection
25747 code is not being generated for constraint.cc. */
25749 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25751 /* Returns the template constraints of declaration T. If T is not
25752 constrained, return NULL_TREE. Note that T must be non-null. */
25754 tree
25755 get_constraints (tree t)
25757 if (!flag_concepts)
25758 return NULL_TREE;
25760 gcc_assert (DECL_P (t));
25761 if (TREE_CODE (t) == TEMPLATE_DECL)
25762 t = DECL_TEMPLATE_RESULT (t);
25763 constr_entry elt = { t, NULL_TREE };
25764 constr_entry* found = decl_constraints->find (&elt);
25765 if (found)
25766 return found->ci;
25767 else
25768 return NULL_TREE;
25771 /* Associate the given constraint information CI with the declaration
25772 T. If T is a template, then the constraints are associated with
25773 its underlying declaration. Don't build associations if CI is
25774 NULL_TREE. */
25776 void
25777 set_constraints (tree t, tree ci)
25779 if (!ci)
25780 return;
25781 gcc_assert (t && flag_concepts);
25782 if (TREE_CODE (t) == TEMPLATE_DECL)
25783 t = DECL_TEMPLATE_RESULT (t);
25784 gcc_assert (!get_constraints (t));
25785 constr_entry elt = {t, ci};
25786 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25787 constr_entry* entry = ggc_alloc<constr_entry> ();
25788 *entry = elt;
25789 *slot = entry;
25792 /* Remove the associated constraints of the declaration T. */
25794 void
25795 remove_constraints (tree t)
25797 gcc_assert (DECL_P (t));
25798 if (TREE_CODE (t) == TEMPLATE_DECL)
25799 t = DECL_TEMPLATE_RESULT (t);
25801 constr_entry elt = {t, NULL_TREE};
25802 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25803 if (slot)
25804 decl_constraints->clear_slot (slot);
25807 /* Memoized satisfaction results for declarations. This
25808 maps the pair (constraint_info, arguments) to the result computed
25809 by constraints_satisfied_p. */
25811 struct GTY((for_user)) constraint_sat_entry
25813 tree ci;
25814 tree args;
25815 tree result;
25818 /* Hashing function and equality for constraint entries. */
25820 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25822 static hashval_t hash (constraint_sat_entry *e)
25824 hashval_t val = iterative_hash_object(e->ci, 0);
25825 return iterative_hash_template_arg (e->args, val);
25828 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25830 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25834 /* Memoized satisfaction results for concept checks. */
25836 struct GTY((for_user)) concept_spec_entry
25838 tree tmpl;
25839 tree args;
25840 tree result;
25843 /* Hashing function and equality for constraint entries. */
25845 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25847 static hashval_t hash (concept_spec_entry *e)
25849 return hash_tmpl_and_args (e->tmpl, e->args);
25852 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25854 ++comparing_specializations;
25855 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25856 --comparing_specializations;
25857 return eq;
25861 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25862 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25864 /* Search for a memoized satisfaction result. Returns one of the
25865 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25867 tree
25868 lookup_constraint_satisfaction (tree ci, tree args)
25870 constraint_sat_entry elt = { ci, args, NULL_TREE };
25871 constraint_sat_entry* found = constraint_memos->find (&elt);
25872 if (found)
25873 return found->result;
25874 else
25875 return NULL_TREE;
25878 /* Memoize the result of a satisfication test. Returns the saved result. */
25880 tree
25881 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25883 constraint_sat_entry elt = {ci, args, result};
25884 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25885 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25886 *entry = elt;
25887 *slot = entry;
25888 return result;
25891 /* Search for a memoized satisfaction result for a concept. */
25893 tree
25894 lookup_concept_satisfaction (tree tmpl, tree args)
25896 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25897 concept_spec_entry* found = concept_memos->find (&elt);
25898 if (found)
25899 return found->result;
25900 else
25901 return NULL_TREE;
25904 /* Memoize the result of a concept check. Returns the saved result. */
25906 tree
25907 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
25909 concept_spec_entry elt = {tmpl, args, result};
25910 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
25911 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25912 *entry = elt;
25913 *slot = entry;
25914 return result;
25917 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
25919 /* Returns a prior concept specialization. This returns the substituted
25920 and normalized constraints defined by the concept. */
25922 tree
25923 get_concept_expansion (tree tmpl, tree args)
25925 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25926 concept_spec_entry* found = concept_expansions->find (&elt);
25927 if (found)
25928 return found->result;
25929 else
25930 return NULL_TREE;
25933 /* Save a concept expansion for later. */
25935 tree
25936 save_concept_expansion (tree tmpl, tree args, tree def)
25938 concept_spec_entry elt = {tmpl, args, def};
25939 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
25940 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25941 *entry = elt;
25942 *slot = entry;
25943 return def;
25946 static hashval_t
25947 hash_subsumption_args (tree t1, tree t2)
25949 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
25950 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
25951 int val = 0;
25952 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
25953 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
25954 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
25955 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
25956 return val;
25959 /* Compare the constraints of two subsumption entries. The LEFT1 and
25960 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
25961 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
25963 static bool
25964 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
25966 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
25967 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
25968 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
25969 CHECK_CONSTR_ARGS (right1)))
25970 return comp_template_args (CHECK_CONSTR_ARGS (left2),
25971 CHECK_CONSTR_ARGS (right2));
25972 return false;
25975 /* Key/value pair for learning and memoizing subsumption results. This
25976 associates a pair of check constraints (including arguments) with
25977 a boolean value indicating the result. */
25979 struct GTY((for_user)) subsumption_entry
25981 tree t1;
25982 tree t2;
25983 bool result;
25986 /* Hashing function and equality for constraint entries. */
25988 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
25990 static hashval_t hash (subsumption_entry *e)
25992 return hash_subsumption_args (e->t1, e->t2);
25995 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
25997 ++comparing_specializations;
25998 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
25999 --comparing_specializations;
26000 return eq;
26004 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26006 /* Search for a previously cached subsumption result. */
26008 bool*
26009 lookup_subsumption_result (tree t1, tree t2)
26011 subsumption_entry elt = { t1, t2, false };
26012 subsumption_entry* found = subsumption_table->find (&elt);
26013 if (found)
26014 return &found->result;
26015 else
26016 return 0;
26019 /* Save a subsumption result. */
26021 bool
26022 save_subsumption_result (tree t1, tree t2, bool result)
26024 subsumption_entry elt = {t1, t2, result};
26025 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26026 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26027 *entry = elt;
26028 *slot = entry;
26029 return result;
26032 /* Set up the hash table for constraint association. */
26034 void
26035 init_constraint_processing (void)
26037 if (!flag_concepts)
26038 return;
26040 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26041 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26042 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26043 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26044 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26047 /* Set up the hash tables for template instantiations. */
26049 void
26050 init_template_processing (void)
26052 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26053 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26056 /* Print stats about the template hash tables for -fstats. */
26058 void
26059 print_template_statistics (void)
26061 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26062 "%f collisions\n", (long) decl_specializations->size (),
26063 (long) decl_specializations->elements (),
26064 decl_specializations->collisions ());
26065 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26066 "%f collisions\n", (long) type_specializations->size (),
26067 (long) type_specializations->elements (),
26068 type_specializations->collisions ());
26071 #include "gt-cp-pt.h"