PR c++/41727 - ICE with partial spec of partial instantiation
[official-gcc.git] / gcc / cp / pt.c
blob38a01e168a7876ce9b7c0ccc51786aa4f44fe8bf
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 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
1603 /* Dup decl failed, but this is a new definition. Set the
1604 line number so any errors match this new
1605 definition. */
1606 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1608 return fn;
1611 else if (fn)
1612 return duplicate_decls (spec, fn, is_friend);
1614 /* A specialization must be declared in the same namespace as the
1615 template it is specializing. */
1616 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1617 && !check_specialization_namespace (tmpl))
1618 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1620 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1622 spec_entry *entry = ggc_alloc<spec_entry> ();
1623 gcc_assert (tmpl && args && spec);
1624 *entry = elt;
1625 *slot = entry;
1626 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1627 && PRIMARY_TEMPLATE_P (tmpl)
1628 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1629 || variable_template_p (tmpl))
1630 /* If TMPL is a forward declaration of a template function, keep a list
1631 of all specializations in case we need to reassign them to a friend
1632 template later in tsubst_friend_function.
1634 Also keep a list of all variable template instantiations so that
1635 process_partial_specialization can check whether a later partial
1636 specialization would have used it. */
1637 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1638 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1641 return spec;
1644 /* Returns true iff two spec_entry nodes are equivalent. */
1646 int comparing_specializations;
1648 bool
1649 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1651 int equal;
1653 ++comparing_specializations;
1654 equal = (e1->tmpl == e2->tmpl
1655 && comp_template_args (e1->args, e2->args));
1656 if (equal && flag_concepts
1657 /* tmpl could be a FIELD_DECL for a capture pack. */
1658 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1659 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1660 && uses_template_parms (e1->args))
1662 /* Partial specializations of a variable template can be distinguished by
1663 constraints. */
1664 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1665 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1666 equal = equivalent_constraints (c1, c2);
1668 --comparing_specializations;
1670 return equal;
1673 /* Returns a hash for a template TMPL and template arguments ARGS. */
1675 static hashval_t
1676 hash_tmpl_and_args (tree tmpl, tree args)
1678 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1679 return iterative_hash_template_arg (args, val);
1682 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1683 ignoring SPEC. */
1685 hashval_t
1686 spec_hasher::hash (spec_entry *e)
1688 return hash_tmpl_and_args (e->tmpl, e->args);
1691 /* Recursively calculate a hash value for a template argument ARG, for use
1692 in the hash tables of template specializations. */
1694 hashval_t
1695 iterative_hash_template_arg (tree arg, hashval_t val)
1697 unsigned HOST_WIDE_INT i;
1698 enum tree_code code;
1699 char tclass;
1701 if (arg == NULL_TREE)
1702 return iterative_hash_object (arg, val);
1704 if (!TYPE_P (arg))
1705 STRIP_NOPS (arg);
1707 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1708 gcc_unreachable ();
1710 code = TREE_CODE (arg);
1711 tclass = TREE_CODE_CLASS (code);
1713 val = iterative_hash_object (code, val);
1715 switch (code)
1717 case ERROR_MARK:
1718 return val;
1720 case IDENTIFIER_NODE:
1721 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1723 case TREE_VEC:
1725 int i, len = TREE_VEC_LENGTH (arg);
1726 for (i = 0; i < len; ++i)
1727 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1728 return val;
1731 case TYPE_PACK_EXPANSION:
1732 case EXPR_PACK_EXPANSION:
1733 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1734 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1736 case TYPE_ARGUMENT_PACK:
1737 case NONTYPE_ARGUMENT_PACK:
1738 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1740 case TREE_LIST:
1741 for (; arg; arg = TREE_CHAIN (arg))
1742 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1743 return val;
1745 case OVERLOAD:
1746 for (; arg; arg = OVL_NEXT (arg))
1747 val = iterative_hash_template_arg (OVL_CURRENT (arg), val);
1748 return val;
1750 case CONSTRUCTOR:
1752 tree field, value;
1753 iterative_hash_template_arg (TREE_TYPE (arg), val);
1754 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1756 val = iterative_hash_template_arg (field, val);
1757 val = iterative_hash_template_arg (value, val);
1759 return val;
1762 case PARM_DECL:
1763 if (!DECL_ARTIFICIAL (arg))
1765 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1766 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1768 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1770 case TARGET_EXPR:
1771 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1773 case PTRMEM_CST:
1774 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1775 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1777 case TEMPLATE_PARM_INDEX:
1778 val = iterative_hash_template_arg
1779 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1780 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1781 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1783 case TRAIT_EXPR:
1784 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1785 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1786 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1788 case BASELINK:
1789 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1790 val);
1791 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1792 val);
1794 case MODOP_EXPR:
1795 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1796 code = TREE_CODE (TREE_OPERAND (arg, 1));
1797 val = iterative_hash_object (code, val);
1798 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1800 case LAMBDA_EXPR:
1801 /* A lambda can't appear in a template arg, but don't crash on
1802 erroneous input. */
1803 gcc_assert (seen_error ());
1804 return val;
1806 case CAST_EXPR:
1807 case IMPLICIT_CONV_EXPR:
1808 case STATIC_CAST_EXPR:
1809 case REINTERPRET_CAST_EXPR:
1810 case CONST_CAST_EXPR:
1811 case DYNAMIC_CAST_EXPR:
1812 case NEW_EXPR:
1813 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1814 /* Now hash operands as usual. */
1815 break;
1817 default:
1818 break;
1821 switch (tclass)
1823 case tcc_type:
1824 if (alias_template_specialization_p (arg))
1826 // We want an alias specialization that survived strip_typedefs
1827 // to hash differently from its TYPE_CANONICAL, to avoid hash
1828 // collisions that compare as different in template_args_equal.
1829 // These could be dependent specializations that strip_typedefs
1830 // left alone, or untouched specializations because
1831 // coerce_template_parms returns the unconverted template
1832 // arguments if it sees incomplete argument packs.
1833 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1834 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1836 if (TYPE_CANONICAL (arg))
1837 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1838 val);
1839 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1840 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1841 /* Otherwise just compare the types during lookup. */
1842 return val;
1844 case tcc_declaration:
1845 case tcc_constant:
1846 return iterative_hash_expr (arg, val);
1848 default:
1849 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1851 unsigned n = cp_tree_operand_length (arg);
1852 for (i = 0; i < n; ++i)
1853 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1854 return val;
1857 gcc_unreachable ();
1858 return 0;
1861 /* Unregister the specialization SPEC as a specialization of TMPL.
1862 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1863 if the SPEC was listed as a specialization of TMPL.
1865 Note that SPEC has been ggc_freed, so we can't look inside it. */
1867 bool
1868 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1870 spec_entry *entry;
1871 spec_entry elt;
1873 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1874 elt.args = TI_ARGS (tinfo);
1875 elt.spec = NULL_TREE;
1877 entry = decl_specializations->find (&elt);
1878 if (entry != NULL)
1880 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1881 gcc_assert (new_spec != NULL_TREE);
1882 entry->spec = new_spec;
1883 return 1;
1886 return 0;
1889 /* Like register_specialization, but for local declarations. We are
1890 registering SPEC, an instantiation of TMPL. */
1892 void
1893 register_local_specialization (tree spec, tree tmpl)
1895 local_specializations->put (tmpl, spec);
1898 /* TYPE is a class type. Returns true if TYPE is an explicitly
1899 specialized class. */
1901 bool
1902 explicit_class_specialization_p (tree type)
1904 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1905 return false;
1906 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1909 /* Print the list of functions at FNS, going through all the overloads
1910 for each element of the list. Alternatively, FNS can not be a
1911 TREE_LIST, in which case it will be printed together with all the
1912 overloads.
1914 MORE and *STR should respectively be FALSE and NULL when the function
1915 is called from the outside. They are used internally on recursive
1916 calls. print_candidates manages the two parameters and leaves NULL
1917 in *STR when it ends. */
1919 static void
1920 print_candidates_1 (tree fns, bool more, const char **str)
1922 tree fn, fn2;
1923 char *spaces = NULL;
1925 for (fn = fns; fn; fn = OVL_NEXT (fn))
1926 if (TREE_CODE (fn) == TREE_LIST)
1928 for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
1929 print_candidates_1 (TREE_VALUE (fn2),
1930 TREE_CHAIN (fn2) || more, str);
1932 else
1934 tree cand = OVL_CURRENT (fn);
1935 if (!*str)
1937 /* Pick the prefix string. */
1938 if (!more && !OVL_NEXT (fns))
1940 inform (DECL_SOURCE_LOCATION (cand),
1941 "candidate is: %#D", cand);
1942 continue;
1945 *str = _("candidates are:");
1946 spaces = get_spaces (*str);
1948 inform (DECL_SOURCE_LOCATION (cand), "%s %#D", *str, cand);
1949 *str = spaces ? spaces : *str;
1952 if (!more)
1954 free (spaces);
1955 *str = NULL;
1959 /* Print the list of candidate FNS in an error message. FNS can also
1960 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1962 void
1963 print_candidates (tree fns)
1965 const char *str = NULL;
1966 print_candidates_1 (fns, false, &str);
1967 gcc_assert (str == NULL);
1970 /* Get a (possibly) constrained template declaration for the
1971 purpose of ordering candidates. */
1972 static tree
1973 get_template_for_ordering (tree list)
1975 gcc_assert (TREE_CODE (list) == TREE_LIST);
1976 tree f = TREE_VALUE (list);
1977 if (tree ti = DECL_TEMPLATE_INFO (f))
1978 return TI_TEMPLATE (ti);
1979 return f;
1982 /* Among candidates having the same signature, return the
1983 most constrained or NULL_TREE if there is no best candidate.
1984 If the signatures of candidates vary (e.g., template
1985 specialization vs. member function), then there can be no
1986 most constrained.
1988 Note that we don't compare constraints on the functions
1989 themselves, but rather those of their templates. */
1990 static tree
1991 most_constrained_function (tree candidates)
1993 // Try to find the best candidate in a first pass.
1994 tree champ = candidates;
1995 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1997 int winner = more_constrained (get_template_for_ordering (champ),
1998 get_template_for_ordering (c));
1999 if (winner == -1)
2000 champ = c; // The candidate is more constrained
2001 else if (winner == 0)
2002 return NULL_TREE; // Neither is more constrained
2005 // Verify that the champ is better than previous candidates.
2006 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2007 if (!more_constrained (get_template_for_ordering (champ),
2008 get_template_for_ordering (c)))
2009 return NULL_TREE;
2012 return champ;
2016 /* Returns the template (one of the functions given by TEMPLATE_ID)
2017 which can be specialized to match the indicated DECL with the
2018 explicit template args given in TEMPLATE_ID. The DECL may be
2019 NULL_TREE if none is available. In that case, the functions in
2020 TEMPLATE_ID are non-members.
2022 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2023 specialization of a member template.
2025 The TEMPLATE_COUNT is the number of references to qualifying
2026 template classes that appeared in the name of the function. See
2027 check_explicit_specialization for a more accurate description.
2029 TSK indicates what kind of template declaration (if any) is being
2030 declared. TSK_TEMPLATE indicates that the declaration given by
2031 DECL, though a FUNCTION_DECL, has template parameters, and is
2032 therefore a template function.
2034 The template args (those explicitly specified and those deduced)
2035 are output in a newly created vector *TARGS_OUT.
2037 If it is impossible to determine the result, an error message is
2038 issued. The error_mark_node is returned to indicate failure. */
2040 static tree
2041 determine_specialization (tree template_id,
2042 tree decl,
2043 tree* targs_out,
2044 int need_member_template,
2045 int template_count,
2046 tmpl_spec_kind tsk)
2048 tree fns;
2049 tree targs;
2050 tree explicit_targs;
2051 tree candidates = NULL_TREE;
2053 /* A TREE_LIST of templates of which DECL may be a specialization.
2054 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2055 corresponding TREE_PURPOSE is the set of template arguments that,
2056 when used to instantiate the template, would produce a function
2057 with the signature of DECL. */
2058 tree templates = NULL_TREE;
2059 int header_count;
2060 cp_binding_level *b;
2062 *targs_out = NULL_TREE;
2064 if (template_id == error_mark_node || decl == error_mark_node)
2065 return error_mark_node;
2067 /* We shouldn't be specializing a member template of an
2068 unspecialized class template; we already gave an error in
2069 check_specialization_scope, now avoid crashing. */
2070 if (template_count && DECL_CLASS_SCOPE_P (decl)
2071 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2073 gcc_assert (errorcount);
2074 return error_mark_node;
2077 fns = TREE_OPERAND (template_id, 0);
2078 explicit_targs = TREE_OPERAND (template_id, 1);
2080 if (fns == error_mark_node)
2081 return error_mark_node;
2083 /* Check for baselinks. */
2084 if (BASELINK_P (fns))
2085 fns = BASELINK_FUNCTIONS (fns);
2087 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2089 error ("%qD is not a function template", fns);
2090 return error_mark_node;
2092 else if (VAR_P (decl) && !variable_template_p (fns))
2094 error ("%qD is not a variable template", fns);
2095 return error_mark_node;
2098 /* Count the number of template headers specified for this
2099 specialization. */
2100 header_count = 0;
2101 for (b = current_binding_level;
2102 b->kind == sk_template_parms;
2103 b = b->level_chain)
2104 ++header_count;
2106 tree orig_fns = fns;
2108 if (variable_template_p (fns))
2110 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2111 targs = coerce_template_parms (parms, explicit_targs, fns,
2112 tf_warning_or_error,
2113 /*req_all*/true, /*use_defarg*/true);
2114 if (targs != error_mark_node)
2115 templates = tree_cons (targs, fns, templates);
2117 else for (; fns; fns = OVL_NEXT (fns))
2119 tree fn = OVL_CURRENT (fns);
2121 if (TREE_CODE (fn) == TEMPLATE_DECL)
2123 tree decl_arg_types;
2124 tree fn_arg_types;
2125 tree insttype;
2127 /* In case of explicit specialization, we need to check if
2128 the number of template headers appearing in the specialization
2129 is correct. This is usually done in check_explicit_specialization,
2130 but the check done there cannot be exhaustive when specializing
2131 member functions. Consider the following code:
2133 template <> void A<int>::f(int);
2134 template <> template <> void A<int>::f(int);
2136 Assuming that A<int> is not itself an explicit specialization
2137 already, the first line specializes "f" which is a non-template
2138 member function, whilst the second line specializes "f" which
2139 is a template member function. So both lines are syntactically
2140 correct, and check_explicit_specialization does not reject
2141 them.
2143 Here, we can do better, as we are matching the specialization
2144 against the declarations. We count the number of template
2145 headers, and we check if they match TEMPLATE_COUNT + 1
2146 (TEMPLATE_COUNT is the number of qualifying template classes,
2147 plus there must be another header for the member template
2148 itself).
2150 Notice that if header_count is zero, this is not a
2151 specialization but rather a template instantiation, so there
2152 is no check we can perform here. */
2153 if (header_count && header_count != template_count + 1)
2154 continue;
2156 /* Check that the number of template arguments at the
2157 innermost level for DECL is the same as for FN. */
2158 if (current_binding_level->kind == sk_template_parms
2159 && !current_binding_level->explicit_spec_p
2160 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2161 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2162 (current_template_parms))))
2163 continue;
2165 /* DECL might be a specialization of FN. */
2166 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2167 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2169 /* For a non-static member function, we need to make sure
2170 that the const qualification is the same. Since
2171 get_bindings does not try to merge the "this" parameter,
2172 we must do the comparison explicitly. */
2173 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2174 && !same_type_p (TREE_VALUE (fn_arg_types),
2175 TREE_VALUE (decl_arg_types)))
2176 continue;
2178 /* Skip the "this" parameter and, for constructors of
2179 classes with virtual bases, the VTT parameter. A
2180 full specialization of a constructor will have a VTT
2181 parameter, but a template never will. */
2182 decl_arg_types
2183 = skip_artificial_parms_for (decl, decl_arg_types);
2184 fn_arg_types
2185 = skip_artificial_parms_for (fn, fn_arg_types);
2187 /* Function templates cannot be specializations; there are
2188 no partial specializations of functions. Therefore, if
2189 the type of DECL does not match FN, there is no
2190 match.
2192 Note that it should never be the case that we have both
2193 candidates added here, and for regular member functions
2194 below. */
2195 if (tsk == tsk_template)
2197 if (compparms (fn_arg_types, decl_arg_types))
2198 candidates = tree_cons (NULL_TREE, fn, candidates);
2199 continue;
2202 /* See whether this function might be a specialization of this
2203 template. Suppress access control because we might be trying
2204 to make this specialization a friend, and we have already done
2205 access control for the declaration of the specialization. */
2206 push_deferring_access_checks (dk_no_check);
2207 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2208 pop_deferring_access_checks ();
2210 if (!targs)
2211 /* We cannot deduce template arguments that when used to
2212 specialize TMPL will produce DECL. */
2213 continue;
2215 /* Remove, from the set of candidates, all those functions
2216 whose constraints are not satisfied. */
2217 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2218 continue;
2220 // Then, try to form the new function type.
2221 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2222 if (insttype == error_mark_node)
2223 continue;
2224 fn_arg_types
2225 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2226 if (!compparms (fn_arg_types, decl_arg_types))
2227 continue;
2229 /* Save this template, and the arguments deduced. */
2230 templates = tree_cons (targs, fn, templates);
2232 else if (need_member_template)
2233 /* FN is an ordinary member function, and we need a
2234 specialization of a member template. */
2236 else if (TREE_CODE (fn) != FUNCTION_DECL)
2237 /* We can get IDENTIFIER_NODEs here in certain erroneous
2238 cases. */
2240 else if (!DECL_FUNCTION_MEMBER_P (fn))
2241 /* This is just an ordinary non-member function. Nothing can
2242 be a specialization of that. */
2244 else if (DECL_ARTIFICIAL (fn))
2245 /* Cannot specialize functions that are created implicitly. */
2247 else
2249 tree decl_arg_types;
2251 /* This is an ordinary member function. However, since
2252 we're here, we can assume its enclosing class is a
2253 template class. For example,
2255 template <typename T> struct S { void f(); };
2256 template <> void S<int>::f() {}
2258 Here, S<int>::f is a non-template, but S<int> is a
2259 template class. If FN has the same type as DECL, we
2260 might be in business. */
2262 if (!DECL_TEMPLATE_INFO (fn))
2263 /* Its enclosing class is an explicit specialization
2264 of a template class. This is not a candidate. */
2265 continue;
2267 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2268 TREE_TYPE (TREE_TYPE (fn))))
2269 /* The return types differ. */
2270 continue;
2272 /* Adjust the type of DECL in case FN is a static member. */
2273 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2274 if (DECL_STATIC_FUNCTION_P (fn)
2275 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2276 decl_arg_types = TREE_CHAIN (decl_arg_types);
2278 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2279 decl_arg_types))
2280 continue;
2282 // If the deduced arguments do not satisfy the constraints,
2283 // this is not a candidate.
2284 if (flag_concepts && !constraints_satisfied_p (fn))
2285 continue;
2287 // Add the candidate.
2288 candidates = tree_cons (NULL_TREE, fn, candidates);
2292 if (templates && TREE_CHAIN (templates))
2294 /* We have:
2296 [temp.expl.spec]
2298 It is possible for a specialization with a given function
2299 signature to be instantiated from more than one function
2300 template. In such cases, explicit specification of the
2301 template arguments must be used to uniquely identify the
2302 function template specialization being specialized.
2304 Note that here, there's no suggestion that we're supposed to
2305 determine which of the candidate templates is most
2306 specialized. However, we, also have:
2308 [temp.func.order]
2310 Partial ordering of overloaded function template
2311 declarations is used in the following contexts to select
2312 the function template to which a function template
2313 specialization refers:
2315 -- when an explicit specialization refers to a function
2316 template.
2318 So, we do use the partial ordering rules, at least for now.
2319 This extension can only serve to make invalid programs valid,
2320 so it's safe. And, there is strong anecdotal evidence that
2321 the committee intended the partial ordering rules to apply;
2322 the EDG front end has that behavior, and John Spicer claims
2323 that the committee simply forgot to delete the wording in
2324 [temp.expl.spec]. */
2325 tree tmpl = most_specialized_instantiation (templates);
2326 if (tmpl != error_mark_node)
2328 templates = tmpl;
2329 TREE_CHAIN (templates) = NULL_TREE;
2333 // Concepts allows multiple declarations of member functions
2334 // with the same signature. Like above, we need to rely on
2335 // on the partial ordering of those candidates to determine which
2336 // is the best.
2337 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2339 if (tree cand = most_constrained_function (candidates))
2341 candidates = cand;
2342 TREE_CHAIN (cand) = NULL_TREE;
2346 if (templates == NULL_TREE && candidates == NULL_TREE)
2348 error ("template-id %qD for %q+D does not match any template "
2349 "declaration", template_id, decl);
2350 if (header_count && header_count != template_count + 1)
2351 inform (input_location, "saw %d %<template<>%>, need %d for "
2352 "specializing a member function template",
2353 header_count, template_count + 1);
2354 else
2355 print_candidates (orig_fns);
2356 return error_mark_node;
2358 else if ((templates && TREE_CHAIN (templates))
2359 || (candidates && TREE_CHAIN (candidates))
2360 || (templates && candidates))
2362 error ("ambiguous template specialization %qD for %q+D",
2363 template_id, decl);
2364 candidates = chainon (candidates, templates);
2365 print_candidates (candidates);
2366 return error_mark_node;
2369 /* We have one, and exactly one, match. */
2370 if (candidates)
2372 tree fn = TREE_VALUE (candidates);
2373 *targs_out = copy_node (DECL_TI_ARGS (fn));
2375 // Propagate the candidate's constraints to the declaration.
2376 set_constraints (decl, get_constraints (fn));
2378 /* DECL is a re-declaration or partial instantiation of a template
2379 function. */
2380 if (TREE_CODE (fn) == TEMPLATE_DECL)
2381 return fn;
2382 /* It was a specialization of an ordinary member function in a
2383 template class. */
2384 return DECL_TI_TEMPLATE (fn);
2387 /* It was a specialization of a template. */
2388 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2389 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2391 *targs_out = copy_node (targs);
2392 SET_TMPL_ARGS_LEVEL (*targs_out,
2393 TMPL_ARGS_DEPTH (*targs_out),
2394 TREE_PURPOSE (templates));
2396 else
2397 *targs_out = TREE_PURPOSE (templates);
2398 return TREE_VALUE (templates);
2401 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2402 but with the default argument values filled in from those in the
2403 TMPL_TYPES. */
2405 static tree
2406 copy_default_args_to_explicit_spec_1 (tree spec_types,
2407 tree tmpl_types)
2409 tree new_spec_types;
2411 if (!spec_types)
2412 return NULL_TREE;
2414 if (spec_types == void_list_node)
2415 return void_list_node;
2417 /* Substitute into the rest of the list. */
2418 new_spec_types =
2419 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2420 TREE_CHAIN (tmpl_types));
2422 /* Add the default argument for this parameter. */
2423 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2424 TREE_VALUE (spec_types),
2425 new_spec_types);
2428 /* DECL is an explicit specialization. Replicate default arguments
2429 from the template it specializes. (That way, code like:
2431 template <class T> void f(T = 3);
2432 template <> void f(double);
2433 void g () { f (); }
2435 works, as required.) An alternative approach would be to look up
2436 the correct default arguments at the call-site, but this approach
2437 is consistent with how implicit instantiations are handled. */
2439 static void
2440 copy_default_args_to_explicit_spec (tree decl)
2442 tree tmpl;
2443 tree spec_types;
2444 tree tmpl_types;
2445 tree new_spec_types;
2446 tree old_type;
2447 tree new_type;
2448 tree t;
2449 tree object_type = NULL_TREE;
2450 tree in_charge = NULL_TREE;
2451 tree vtt = NULL_TREE;
2453 /* See if there's anything we need to do. */
2454 tmpl = DECL_TI_TEMPLATE (decl);
2455 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2456 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2457 if (TREE_PURPOSE (t))
2458 break;
2459 if (!t)
2460 return;
2462 old_type = TREE_TYPE (decl);
2463 spec_types = TYPE_ARG_TYPES (old_type);
2465 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2467 /* Remove the this pointer, but remember the object's type for
2468 CV quals. */
2469 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2470 spec_types = TREE_CHAIN (spec_types);
2471 tmpl_types = TREE_CHAIN (tmpl_types);
2473 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2475 /* DECL may contain more parameters than TMPL due to the extra
2476 in-charge parameter in constructors and destructors. */
2477 in_charge = spec_types;
2478 spec_types = TREE_CHAIN (spec_types);
2480 if (DECL_HAS_VTT_PARM_P (decl))
2482 vtt = spec_types;
2483 spec_types = TREE_CHAIN (spec_types);
2487 /* Compute the merged default arguments. */
2488 new_spec_types =
2489 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2491 /* Compute the new FUNCTION_TYPE. */
2492 if (object_type)
2494 if (vtt)
2495 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2496 TREE_VALUE (vtt),
2497 new_spec_types);
2499 if (in_charge)
2500 /* Put the in-charge parameter back. */
2501 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2502 TREE_VALUE (in_charge),
2503 new_spec_types);
2505 new_type = build_method_type_directly (object_type,
2506 TREE_TYPE (old_type),
2507 new_spec_types);
2509 else
2510 new_type = build_function_type (TREE_TYPE (old_type),
2511 new_spec_types);
2512 new_type = cp_build_type_attribute_variant (new_type,
2513 TYPE_ATTRIBUTES (old_type));
2514 new_type = build_exception_variant (new_type,
2515 TYPE_RAISES_EXCEPTIONS (old_type));
2517 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2518 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2520 TREE_TYPE (decl) = new_type;
2523 /* Return the number of template headers we expect to see for a definition
2524 or specialization of CTYPE or one of its non-template members. */
2527 num_template_headers_for_class (tree ctype)
2529 int num_templates = 0;
2531 while (ctype && CLASS_TYPE_P (ctype))
2533 /* You're supposed to have one `template <...>' for every
2534 template class, but you don't need one for a full
2535 specialization. For example:
2537 template <class T> struct S{};
2538 template <> struct S<int> { void f(); };
2539 void S<int>::f () {}
2541 is correct; there shouldn't be a `template <>' for the
2542 definition of `S<int>::f'. */
2543 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2544 /* If CTYPE does not have template information of any
2545 kind, then it is not a template, nor is it nested
2546 within a template. */
2547 break;
2548 if (explicit_class_specialization_p (ctype))
2549 break;
2550 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2551 ++num_templates;
2553 ctype = TYPE_CONTEXT (ctype);
2556 return num_templates;
2559 /* Do a simple sanity check on the template headers that precede the
2560 variable declaration DECL. */
2562 void
2563 check_template_variable (tree decl)
2565 tree ctx = CP_DECL_CONTEXT (decl);
2566 int wanted = num_template_headers_for_class (ctx);
2567 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2568 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2570 if (cxx_dialect < cxx14)
2571 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2572 "variable templates only available with "
2573 "-std=c++14 or -std=gnu++14");
2575 // Namespace-scope variable templates should have a template header.
2576 ++wanted;
2578 if (template_header_count > wanted)
2580 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2581 "too many template headers for %D (should be %d)",
2582 decl, wanted);
2583 if (warned && CLASS_TYPE_P (ctx)
2584 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2585 inform (DECL_SOURCE_LOCATION (decl),
2586 "members of an explicitly specialized class are defined "
2587 "without a template header");
2591 /* An explicit specialization whose declarator-id or class-head-name is not
2592 qualified shall be declared in the nearest enclosing namespace of the
2593 template, or, if the namespace is inline (7.3.1), any namespace from its
2594 enclosing namespace set.
2596 If the name declared in the explicit instantiation is an unqualified name,
2597 the explicit instantiation shall appear in the namespace where its template
2598 is declared or, if that namespace is inline (7.3.1), any namespace from its
2599 enclosing namespace set. */
2601 void
2602 check_unqualified_spec_or_inst (tree t, location_t loc)
2604 tree tmpl = most_general_template (t);
2605 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2606 && !is_associated_namespace (current_namespace,
2607 CP_DECL_CONTEXT (tmpl)))
2609 if (processing_specialization)
2610 permerror (loc, "explicit specialization of %qD outside its "
2611 "namespace must use a nested-name-specifier", tmpl);
2612 else if (processing_explicit_instantiation
2613 && cxx_dialect >= cxx11)
2614 /* This was allowed in C++98, so only pedwarn. */
2615 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2616 "outside its namespace must use a nested-name-"
2617 "specifier", tmpl);
2621 /* Check to see if the function just declared, as indicated in
2622 DECLARATOR, and in DECL, is a specialization of a function
2623 template. We may also discover that the declaration is an explicit
2624 instantiation at this point.
2626 Returns DECL, or an equivalent declaration that should be used
2627 instead if all goes well. Issues an error message if something is
2628 amiss. Returns error_mark_node if the error is not easily
2629 recoverable.
2631 FLAGS is a bitmask consisting of the following flags:
2633 2: The function has a definition.
2634 4: The function is a friend.
2636 The TEMPLATE_COUNT is the number of references to qualifying
2637 template classes that appeared in the name of the function. For
2638 example, in
2640 template <class T> struct S { void f(); };
2641 void S<int>::f();
2643 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2644 classes are not counted in the TEMPLATE_COUNT, so that in
2646 template <class T> struct S {};
2647 template <> struct S<int> { void f(); }
2648 template <> void S<int>::f();
2650 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2651 invalid; there should be no template <>.)
2653 If the function is a specialization, it is marked as such via
2654 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2655 is set up correctly, and it is added to the list of specializations
2656 for that template. */
2658 tree
2659 check_explicit_specialization (tree declarator,
2660 tree decl,
2661 int template_count,
2662 int flags)
2664 int have_def = flags & 2;
2665 int is_friend = flags & 4;
2666 bool is_concept = flags & 8;
2667 int specialization = 0;
2668 int explicit_instantiation = 0;
2669 int member_specialization = 0;
2670 tree ctype = DECL_CLASS_CONTEXT (decl);
2671 tree dname = DECL_NAME (decl);
2672 tmpl_spec_kind tsk;
2674 if (is_friend)
2676 if (!processing_specialization)
2677 tsk = tsk_none;
2678 else
2679 tsk = tsk_excessive_parms;
2681 else
2682 tsk = current_tmpl_spec_kind (template_count);
2684 switch (tsk)
2686 case tsk_none:
2687 if (processing_specialization && !VAR_P (decl))
2689 specialization = 1;
2690 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2692 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2694 if (is_friend)
2695 /* This could be something like:
2697 template <class T> void f(T);
2698 class S { friend void f<>(int); } */
2699 specialization = 1;
2700 else
2702 /* This case handles bogus declarations like template <>
2703 template <class T> void f<int>(); */
2705 error ("template-id %qD in declaration of primary template",
2706 declarator);
2707 return decl;
2710 break;
2712 case tsk_invalid_member_spec:
2713 /* The error has already been reported in
2714 check_specialization_scope. */
2715 return error_mark_node;
2717 case tsk_invalid_expl_inst:
2718 error ("template parameter list used in explicit instantiation");
2720 /* Fall through. */
2722 case tsk_expl_inst:
2723 if (have_def)
2724 error ("definition provided for explicit instantiation");
2726 explicit_instantiation = 1;
2727 break;
2729 case tsk_excessive_parms:
2730 case tsk_insufficient_parms:
2731 if (tsk == tsk_excessive_parms)
2732 error ("too many template parameter lists in declaration of %qD",
2733 decl);
2734 else if (template_header_count)
2735 error("too few template parameter lists in declaration of %qD", decl);
2736 else
2737 error("explicit specialization of %qD must be introduced by "
2738 "%<template <>%>", decl);
2740 /* Fall through. */
2741 case tsk_expl_spec:
2742 if (is_concept)
2743 error ("explicit specialization declared %<concept%>");
2745 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2746 /* In cases like template<> constexpr bool v = true;
2747 We'll give an error in check_template_variable. */
2748 break;
2750 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2751 if (ctype)
2752 member_specialization = 1;
2753 else
2754 specialization = 1;
2755 break;
2757 case tsk_template:
2758 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2760 /* This case handles bogus declarations like template <>
2761 template <class T> void f<int>(); */
2763 if (!uses_template_parms (declarator))
2764 error ("template-id %qD in declaration of primary template",
2765 declarator);
2766 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2768 /* Partial specialization of variable template. */
2769 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2770 specialization = 1;
2771 goto ok;
2773 else if (cxx_dialect < cxx14)
2774 error ("non-type partial specialization %qD "
2775 "is not allowed", declarator);
2776 else
2777 error ("non-class, non-variable partial specialization %qD "
2778 "is not allowed", declarator);
2779 return decl;
2780 ok:;
2783 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2784 /* This is a specialization of a member template, without
2785 specialization the containing class. Something like:
2787 template <class T> struct S {
2788 template <class U> void f (U);
2790 template <> template <class U> void S<int>::f(U) {}
2792 That's a specialization -- but of the entire template. */
2793 specialization = 1;
2794 break;
2796 default:
2797 gcc_unreachable ();
2800 if ((specialization || member_specialization)
2801 /* This doesn't apply to variable templates. */
2802 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2803 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2805 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2806 for (; t; t = TREE_CHAIN (t))
2807 if (TREE_PURPOSE (t))
2809 permerror (input_location,
2810 "default argument specified in explicit specialization");
2811 break;
2815 if (specialization || member_specialization || explicit_instantiation)
2817 tree tmpl = NULL_TREE;
2818 tree targs = NULL_TREE;
2819 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2821 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2822 if (!was_template_id)
2824 tree fns;
2826 gcc_assert (identifier_p (declarator));
2827 if (ctype)
2828 fns = dname;
2829 else
2831 /* If there is no class context, the explicit instantiation
2832 must be at namespace scope. */
2833 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2835 /* Find the namespace binding, using the declaration
2836 context. */
2837 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2838 false, true);
2839 if (fns == error_mark_node)
2840 /* If lookup fails, look for a friend declaration so we can
2841 give a better diagnostic. */
2842 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2843 /*type*/false, /*complain*/true,
2844 /*hidden*/true);
2846 if (fns == error_mark_node || !is_overloaded_fn (fns))
2848 error ("%qD is not a template function", dname);
2849 fns = error_mark_node;
2853 declarator = lookup_template_function (fns, NULL_TREE);
2856 if (declarator == error_mark_node)
2857 return error_mark_node;
2859 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2861 if (!explicit_instantiation)
2862 /* A specialization in class scope. This is invalid,
2863 but the error will already have been flagged by
2864 check_specialization_scope. */
2865 return error_mark_node;
2866 else
2868 /* It's not valid to write an explicit instantiation in
2869 class scope, e.g.:
2871 class C { template void f(); }
2873 This case is caught by the parser. However, on
2874 something like:
2876 template class C { void f(); };
2878 (which is invalid) we can get here. The error will be
2879 issued later. */
2883 return decl;
2885 else if (ctype != NULL_TREE
2886 && (identifier_p (TREE_OPERAND (declarator, 0))))
2888 // We'll match variable templates in start_decl.
2889 if (VAR_P (decl))
2890 return decl;
2892 /* Find the list of functions in ctype that have the same
2893 name as the declared function. */
2894 tree name = TREE_OPERAND (declarator, 0);
2895 tree fns = NULL_TREE;
2896 int idx;
2898 if (constructor_name_p (name, ctype))
2900 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2902 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2903 : !CLASSTYPE_DESTRUCTORS (ctype))
2905 /* From [temp.expl.spec]:
2907 If such an explicit specialization for the member
2908 of a class template names an implicitly-declared
2909 special member function (clause _special_), the
2910 program is ill-formed.
2912 Similar language is found in [temp.explicit]. */
2913 error ("specialization of implicitly-declared special member function");
2914 return error_mark_node;
2917 name = is_constructor ? ctor_identifier : dtor_identifier;
2920 if (!DECL_CONV_FN_P (decl))
2922 idx = lookup_fnfields_1 (ctype, name);
2923 if (idx >= 0)
2924 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2926 else
2928 vec<tree, va_gc> *methods;
2929 tree ovl;
2931 /* For a type-conversion operator, we cannot do a
2932 name-based lookup. We might be looking for `operator
2933 int' which will be a specialization of `operator T'.
2934 So, we find *all* the conversion operators, and then
2935 select from them. */
2936 fns = NULL_TREE;
2938 methods = CLASSTYPE_METHOD_VEC (ctype);
2939 if (methods)
2940 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2941 methods->iterate (idx, &ovl);
2942 ++idx)
2944 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
2945 /* There are no more conversion functions. */
2946 break;
2948 /* Glue all these conversion functions together
2949 with those we already have. */
2950 for (; ovl; ovl = OVL_NEXT (ovl))
2951 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2955 if (fns == NULL_TREE)
2957 error ("no member function %qD declared in %qT", name, ctype);
2958 return error_mark_node;
2960 else
2961 TREE_OPERAND (declarator, 0) = fns;
2964 /* Figure out what exactly is being specialized at this point.
2965 Note that for an explicit instantiation, even one for a
2966 member function, we cannot tell apriori whether the
2967 instantiation is for a member template, or just a member
2968 function of a template class. Even if a member template is
2969 being instantiated, the member template arguments may be
2970 elided if they can be deduced from the rest of the
2971 declaration. */
2972 tmpl = determine_specialization (declarator, decl,
2973 &targs,
2974 member_specialization,
2975 template_count,
2976 tsk);
2978 if (!tmpl || tmpl == error_mark_node)
2979 /* We couldn't figure out what this declaration was
2980 specializing. */
2981 return error_mark_node;
2982 else
2984 if (TREE_CODE (decl) == FUNCTION_DECL
2985 && DECL_HIDDEN_FRIEND_P (tmpl))
2987 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2988 "friend declaration %qD is not visible to "
2989 "explicit specialization", tmpl))
2990 inform (DECL_SOURCE_LOCATION (tmpl),
2991 "friend declaration here");
2993 else if (!ctype && !is_friend
2994 && CP_DECL_CONTEXT (decl) == current_namespace)
2995 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2997 tree gen_tmpl = most_general_template (tmpl);
2999 if (explicit_instantiation)
3001 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3002 is done by do_decl_instantiation later. */
3004 int arg_depth = TMPL_ARGS_DEPTH (targs);
3005 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3007 if (arg_depth > parm_depth)
3009 /* If TMPL is not the most general template (for
3010 example, if TMPL is a friend template that is
3011 injected into namespace scope), then there will
3012 be too many levels of TARGS. Remove some of them
3013 here. */
3014 int i;
3015 tree new_targs;
3017 new_targs = make_tree_vec (parm_depth);
3018 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3019 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3020 = TREE_VEC_ELT (targs, i);
3021 targs = new_targs;
3024 return instantiate_template (tmpl, targs, tf_error);
3027 /* If we thought that the DECL was a member function, but it
3028 turns out to be specializing a static member function,
3029 make DECL a static member function as well. */
3030 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3031 && DECL_STATIC_FUNCTION_P (tmpl)
3032 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3033 revert_static_member_fn (decl);
3035 /* If this is a specialization of a member template of a
3036 template class, we want to return the TEMPLATE_DECL, not
3037 the specialization of it. */
3038 if (tsk == tsk_template && !was_template_id)
3040 tree result = DECL_TEMPLATE_RESULT (tmpl);
3041 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3042 DECL_INITIAL (result) = NULL_TREE;
3043 if (have_def)
3045 tree parm;
3046 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3047 DECL_SOURCE_LOCATION (result)
3048 = DECL_SOURCE_LOCATION (decl);
3049 /* We want to use the argument list specified in the
3050 definition, not in the original declaration. */
3051 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3052 for (parm = DECL_ARGUMENTS (result); parm;
3053 parm = DECL_CHAIN (parm))
3054 DECL_CONTEXT (parm) = result;
3056 return register_specialization (tmpl, gen_tmpl, targs,
3057 is_friend, 0);
3060 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3061 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3063 if (was_template_id)
3064 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3066 /* Inherit default function arguments from the template
3067 DECL is specializing. */
3068 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3069 copy_default_args_to_explicit_spec (decl);
3071 /* This specialization has the same protection as the
3072 template it specializes. */
3073 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3074 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3076 /* 7.1.1-1 [dcl.stc]
3078 A storage-class-specifier shall not be specified in an
3079 explicit specialization...
3081 The parser rejects these, so unless action is taken here,
3082 explicit function specializations will always appear with
3083 global linkage.
3085 The action recommended by the C++ CWG in response to C++
3086 defect report 605 is to make the storage class and linkage
3087 of the explicit specialization match the templated function:
3089 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3091 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3093 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3094 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3096 /* A concept cannot be specialized. */
3097 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3099 error ("explicit specialization of function concept %qD",
3100 gen_tmpl);
3101 return error_mark_node;
3104 /* This specialization has the same linkage and visibility as
3105 the function template it specializes. */
3106 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3107 if (! TREE_PUBLIC (decl))
3109 DECL_INTERFACE_KNOWN (decl) = 1;
3110 DECL_NOT_REALLY_EXTERN (decl) = 1;
3112 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3113 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3115 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3116 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3120 /* If DECL is a friend declaration, declared using an
3121 unqualified name, the namespace associated with DECL may
3122 have been set incorrectly. For example, in:
3124 template <typename T> void f(T);
3125 namespace N {
3126 struct S { friend void f<int>(int); }
3129 we will have set the DECL_CONTEXT for the friend
3130 declaration to N, rather than to the global namespace. */
3131 if (DECL_NAMESPACE_SCOPE_P (decl))
3132 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3134 if (is_friend && !have_def)
3135 /* This is not really a declaration of a specialization.
3136 It's just the name of an instantiation. But, it's not
3137 a request for an instantiation, either. */
3138 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3139 else if (TREE_CODE (decl) == FUNCTION_DECL)
3140 /* A specialization is not necessarily COMDAT. */
3141 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3142 && DECL_DECLARED_INLINE_P (decl));
3143 else if (VAR_P (decl))
3144 DECL_COMDAT (decl) = false;
3146 /* If this is a full specialization, register it so that we can find
3147 it again. Partial specializations will be registered in
3148 process_partial_specialization. */
3149 if (!processing_template_decl)
3150 decl = register_specialization (decl, gen_tmpl, targs,
3151 is_friend, 0);
3153 /* A 'structor should already have clones. */
3154 gcc_assert (decl == error_mark_node
3155 || variable_template_p (tmpl)
3156 || !(DECL_CONSTRUCTOR_P (decl)
3157 || DECL_DESTRUCTOR_P (decl))
3158 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3162 return decl;
3165 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3166 parameters. These are represented in the same format used for
3167 DECL_TEMPLATE_PARMS. */
3170 comp_template_parms (const_tree parms1, const_tree parms2)
3172 const_tree p1;
3173 const_tree p2;
3175 if (parms1 == parms2)
3176 return 1;
3178 for (p1 = parms1, p2 = parms2;
3179 p1 != NULL_TREE && p2 != NULL_TREE;
3180 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3182 tree t1 = TREE_VALUE (p1);
3183 tree t2 = TREE_VALUE (p2);
3184 int i;
3186 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3187 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3189 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3190 return 0;
3192 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3194 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3195 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3197 /* If either of the template parameters are invalid, assume
3198 they match for the sake of error recovery. */
3199 if (error_operand_p (parm1) || error_operand_p (parm2))
3200 return 1;
3202 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3203 return 0;
3205 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3206 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3207 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3208 continue;
3209 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3210 return 0;
3214 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3215 /* One set of parameters has more parameters lists than the
3216 other. */
3217 return 0;
3219 return 1;
3222 /* Determine whether PARM is a parameter pack. */
3224 bool
3225 template_parameter_pack_p (const_tree parm)
3227 /* Determine if we have a non-type template parameter pack. */
3228 if (TREE_CODE (parm) == PARM_DECL)
3229 return (DECL_TEMPLATE_PARM_P (parm)
3230 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3231 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3232 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3234 /* If this is a list of template parameters, we could get a
3235 TYPE_DECL or a TEMPLATE_DECL. */
3236 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3237 parm = TREE_TYPE (parm);
3239 /* Otherwise it must be a type template parameter. */
3240 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3241 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3242 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3245 /* Determine if T is a function parameter pack. */
3247 bool
3248 function_parameter_pack_p (const_tree t)
3250 if (t && TREE_CODE (t) == PARM_DECL)
3251 return DECL_PACK_P (t);
3252 return false;
3255 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3256 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3258 tree
3259 get_function_template_decl (const_tree primary_func_tmpl_inst)
3261 if (! primary_func_tmpl_inst
3262 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3263 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3264 return NULL;
3266 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3269 /* Return true iff the function parameter PARAM_DECL was expanded
3270 from the function parameter pack PACK. */
3272 bool
3273 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3275 if (DECL_ARTIFICIAL (param_decl)
3276 || !function_parameter_pack_p (pack))
3277 return false;
3279 /* The parameter pack and its pack arguments have the same
3280 DECL_PARM_INDEX. */
3281 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3284 /* Determine whether ARGS describes a variadic template args list,
3285 i.e., one that is terminated by a template argument pack. */
3287 static bool
3288 template_args_variadic_p (tree args)
3290 int nargs;
3291 tree last_parm;
3293 if (args == NULL_TREE)
3294 return false;
3296 args = INNERMOST_TEMPLATE_ARGS (args);
3297 nargs = TREE_VEC_LENGTH (args);
3299 if (nargs == 0)
3300 return false;
3302 last_parm = TREE_VEC_ELT (args, nargs - 1);
3304 return ARGUMENT_PACK_P (last_parm);
3307 /* Generate a new name for the parameter pack name NAME (an
3308 IDENTIFIER_NODE) that incorporates its */
3310 static tree
3311 make_ith_pack_parameter_name (tree name, int i)
3313 /* Munge the name to include the parameter index. */
3314 #define NUMBUF_LEN 128
3315 char numbuf[NUMBUF_LEN];
3316 char* newname;
3317 int newname_len;
3319 if (name == NULL_TREE)
3320 return name;
3321 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3322 newname_len = IDENTIFIER_LENGTH (name)
3323 + strlen (numbuf) + 2;
3324 newname = (char*)alloca (newname_len);
3325 snprintf (newname, newname_len,
3326 "%s#%i", IDENTIFIER_POINTER (name), i);
3327 return get_identifier (newname);
3330 /* Return true if T is a primary function, class or alias template
3331 instantiation. */
3333 bool
3334 primary_template_instantiation_p (const_tree t)
3336 if (!t)
3337 return false;
3339 if (TREE_CODE (t) == FUNCTION_DECL)
3340 return DECL_LANG_SPECIFIC (t)
3341 && DECL_TEMPLATE_INSTANTIATION (t)
3342 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3343 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3344 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3345 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3346 else if (alias_template_specialization_p (t))
3347 return true;
3348 return false;
3351 /* Return true if PARM is a template template parameter. */
3353 bool
3354 template_template_parameter_p (const_tree parm)
3356 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3359 /* Return true iff PARM is a DECL representing a type template
3360 parameter. */
3362 bool
3363 template_type_parameter_p (const_tree parm)
3365 return (parm
3366 && (TREE_CODE (parm) == TYPE_DECL
3367 || TREE_CODE (parm) == TEMPLATE_DECL)
3368 && DECL_TEMPLATE_PARM_P (parm));
3371 /* Return the template parameters of T if T is a
3372 primary template instantiation, NULL otherwise. */
3374 tree
3375 get_primary_template_innermost_parameters (const_tree t)
3377 tree parms = NULL, template_info = NULL;
3379 if ((template_info = get_template_info (t))
3380 && primary_template_instantiation_p (t))
3381 parms = INNERMOST_TEMPLATE_PARMS
3382 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3384 return parms;
3387 /* Return the template parameters of the LEVELth level from the full list
3388 of template parameters PARMS. */
3390 tree
3391 get_template_parms_at_level (tree parms, int level)
3393 tree p;
3394 if (!parms
3395 || TREE_CODE (parms) != TREE_LIST
3396 || level > TMPL_PARMS_DEPTH (parms))
3397 return NULL_TREE;
3399 for (p = parms; p; p = TREE_CHAIN (p))
3400 if (TMPL_PARMS_DEPTH (p) == level)
3401 return p;
3403 return NULL_TREE;
3406 /* Returns the template arguments of T if T is a template instantiation,
3407 NULL otherwise. */
3409 tree
3410 get_template_innermost_arguments (const_tree t)
3412 tree args = NULL, template_info = NULL;
3414 if ((template_info = get_template_info (t))
3415 && TI_ARGS (template_info))
3416 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3418 return args;
3421 /* Return the argument pack elements of T if T is a template argument pack,
3422 NULL otherwise. */
3424 tree
3425 get_template_argument_pack_elems (const_tree t)
3427 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3428 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3429 return NULL;
3431 return ARGUMENT_PACK_ARGS (t);
3434 /* Structure used to track the progress of find_parameter_packs_r. */
3435 struct find_parameter_pack_data
3437 /* TREE_LIST that will contain all of the parameter packs found by
3438 the traversal. */
3439 tree* parameter_packs;
3441 /* Set of AST nodes that have been visited by the traversal. */
3442 hash_set<tree> *visited;
3444 /* True iff we're making a type pack expansion. */
3445 bool type_pack_expansion_p;
3448 /* Identifies all of the argument packs that occur in a template
3449 argument and appends them to the TREE_LIST inside DATA, which is a
3450 find_parameter_pack_data structure. This is a subroutine of
3451 make_pack_expansion and uses_parameter_packs. */
3452 static tree
3453 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3455 tree t = *tp;
3456 struct find_parameter_pack_data* ppd =
3457 (struct find_parameter_pack_data*)data;
3458 bool parameter_pack_p = false;
3460 /* Handle type aliases/typedefs. */
3461 if (TYPE_ALIAS_P (t))
3463 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3464 cp_walk_tree (&TI_ARGS (tinfo),
3465 &find_parameter_packs_r,
3466 ppd, ppd->visited);
3467 *walk_subtrees = 0;
3468 return NULL_TREE;
3471 /* Identify whether this is a parameter pack or not. */
3472 switch (TREE_CODE (t))
3474 case TEMPLATE_PARM_INDEX:
3475 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3476 parameter_pack_p = true;
3477 break;
3479 case TEMPLATE_TYPE_PARM:
3480 t = TYPE_MAIN_VARIANT (t);
3481 /* FALLTHRU */
3482 case TEMPLATE_TEMPLATE_PARM:
3483 /* If the placeholder appears in the decl-specifier-seq of a function
3484 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3485 is a pack expansion, the invented template parameter is a template
3486 parameter pack. */
3487 if (ppd->type_pack_expansion_p && is_auto_or_concept (t))
3488 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3489 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3490 parameter_pack_p = true;
3491 break;
3493 case FIELD_DECL:
3494 case PARM_DECL:
3495 if (DECL_PACK_P (t))
3497 /* We don't want to walk into the type of a PARM_DECL,
3498 because we don't want to see the type parameter pack. */
3499 *walk_subtrees = 0;
3500 parameter_pack_p = true;
3502 break;
3504 /* Look through a lambda capture proxy to the field pack. */
3505 case VAR_DECL:
3506 if (DECL_HAS_VALUE_EXPR_P (t))
3508 tree v = DECL_VALUE_EXPR (t);
3509 cp_walk_tree (&v,
3510 &find_parameter_packs_r,
3511 ppd, ppd->visited);
3512 *walk_subtrees = 0;
3514 else if (variable_template_specialization_p (t))
3516 cp_walk_tree (&DECL_TI_ARGS (t),
3517 find_parameter_packs_r,
3518 ppd, ppd->visited);
3519 *walk_subtrees = 0;
3521 break;
3523 case BASES:
3524 parameter_pack_p = true;
3525 break;
3526 default:
3527 /* Not a parameter pack. */
3528 break;
3531 if (parameter_pack_p)
3533 /* Add this parameter pack to the list. */
3534 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3537 if (TYPE_P (t))
3538 cp_walk_tree (&TYPE_CONTEXT (t),
3539 &find_parameter_packs_r, ppd, ppd->visited);
3541 /* This switch statement will return immediately if we don't find a
3542 parameter pack. */
3543 switch (TREE_CODE (t))
3545 case TEMPLATE_PARM_INDEX:
3546 return NULL_TREE;
3548 case BOUND_TEMPLATE_TEMPLATE_PARM:
3549 /* Check the template itself. */
3550 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3551 &find_parameter_packs_r, ppd, ppd->visited);
3552 /* Check the template arguments. */
3553 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3554 ppd->visited);
3555 *walk_subtrees = 0;
3556 return NULL_TREE;
3558 case TEMPLATE_TYPE_PARM:
3559 case TEMPLATE_TEMPLATE_PARM:
3560 return NULL_TREE;
3562 case PARM_DECL:
3563 return NULL_TREE;
3565 case RECORD_TYPE:
3566 if (TYPE_PTRMEMFUNC_P (t))
3567 return NULL_TREE;
3568 /* Fall through. */
3570 case UNION_TYPE:
3571 case ENUMERAL_TYPE:
3572 if (TYPE_TEMPLATE_INFO (t))
3573 cp_walk_tree (&TYPE_TI_ARGS (t),
3574 &find_parameter_packs_r, ppd, ppd->visited);
3576 *walk_subtrees = 0;
3577 return NULL_TREE;
3579 case TEMPLATE_DECL:
3580 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3581 return NULL_TREE;
3582 gcc_fallthrough();
3584 case CONSTRUCTOR:
3585 cp_walk_tree (&TREE_TYPE (t),
3586 &find_parameter_packs_r, ppd, ppd->visited);
3587 return NULL_TREE;
3589 case TYPENAME_TYPE:
3590 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3591 ppd, ppd->visited);
3592 *walk_subtrees = 0;
3593 return NULL_TREE;
3595 case TYPE_PACK_EXPANSION:
3596 case EXPR_PACK_EXPANSION:
3597 *walk_subtrees = 0;
3598 return NULL_TREE;
3600 case INTEGER_TYPE:
3601 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3602 ppd, ppd->visited);
3603 *walk_subtrees = 0;
3604 return NULL_TREE;
3606 case IDENTIFIER_NODE:
3607 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3608 ppd->visited);
3609 *walk_subtrees = 0;
3610 return NULL_TREE;
3612 case DECLTYPE_TYPE:
3614 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3615 type_pack_expansion_p to false so that any placeholders
3616 within the expression don't get marked as parameter packs. */
3617 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3618 ppd->type_pack_expansion_p = false;
3619 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3620 ppd, ppd->visited);
3621 ppd->type_pack_expansion_p = type_pack_expansion_p;
3622 *walk_subtrees = 0;
3623 return NULL_TREE;
3626 default:
3627 return NULL_TREE;
3630 return NULL_TREE;
3633 /* Determines if the expression or type T uses any parameter packs. */
3634 bool
3635 uses_parameter_packs (tree t)
3637 tree parameter_packs = NULL_TREE;
3638 struct find_parameter_pack_data ppd;
3639 ppd.parameter_packs = &parameter_packs;
3640 ppd.visited = new hash_set<tree>;
3641 ppd.type_pack_expansion_p = false;
3642 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3643 delete ppd.visited;
3644 return parameter_packs != NULL_TREE;
3647 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3648 representation a base-class initializer into a parameter pack
3649 expansion. If all goes well, the resulting node will be an
3650 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3651 respectively. */
3652 tree
3653 make_pack_expansion (tree arg)
3655 tree result;
3656 tree parameter_packs = NULL_TREE;
3657 bool for_types = false;
3658 struct find_parameter_pack_data ppd;
3660 if (!arg || arg == error_mark_node)
3661 return arg;
3663 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3665 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3666 class initializer. In this case, the TREE_PURPOSE will be a
3667 _TYPE node (representing the base class expansion we're
3668 initializing) and the TREE_VALUE will be a TREE_LIST
3669 containing the initialization arguments.
3671 The resulting expansion looks somewhat different from most
3672 expansions. Rather than returning just one _EXPANSION, we
3673 return a TREE_LIST whose TREE_PURPOSE is a
3674 TYPE_PACK_EXPANSION containing the bases that will be
3675 initialized. The TREE_VALUE will be identical to the
3676 original TREE_VALUE, which is a list of arguments that will
3677 be passed to each base. We do not introduce any new pack
3678 expansion nodes into the TREE_VALUE (although it is possible
3679 that some already exist), because the TREE_PURPOSE and
3680 TREE_VALUE all need to be expanded together with the same
3681 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3682 resulting TREE_PURPOSE will mention the parameter packs in
3683 both the bases and the arguments to the bases. */
3684 tree purpose;
3685 tree value;
3686 tree parameter_packs = NULL_TREE;
3688 /* Determine which parameter packs will be used by the base
3689 class expansion. */
3690 ppd.visited = new hash_set<tree>;
3691 ppd.parameter_packs = &parameter_packs;
3692 ppd.type_pack_expansion_p = true;
3693 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3694 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3695 &ppd, ppd.visited);
3697 if (parameter_packs == NULL_TREE)
3699 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
3700 delete ppd.visited;
3701 return error_mark_node;
3704 if (TREE_VALUE (arg) != void_type_node)
3706 /* Collect the sets of parameter packs used in each of the
3707 initialization arguments. */
3708 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3710 /* Determine which parameter packs will be expanded in this
3711 argument. */
3712 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3713 &ppd, ppd.visited);
3717 delete ppd.visited;
3719 /* Create the pack expansion type for the base type. */
3720 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3721 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3722 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3724 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3725 they will rarely be compared to anything. */
3726 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3728 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3731 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3732 for_types = true;
3734 /* Build the PACK_EXPANSION_* node. */
3735 result = for_types
3736 ? cxx_make_type (TYPE_PACK_EXPANSION)
3737 : make_node (EXPR_PACK_EXPANSION);
3738 SET_PACK_EXPANSION_PATTERN (result, arg);
3739 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3741 /* Propagate type and const-expression information. */
3742 TREE_TYPE (result) = TREE_TYPE (arg);
3743 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3744 /* Mark this read now, since the expansion might be length 0. */
3745 mark_exp_read (arg);
3747 else
3748 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3749 they will rarely be compared to anything. */
3750 SET_TYPE_STRUCTURAL_EQUALITY (result);
3752 /* Determine which parameter packs will be expanded. */
3753 ppd.parameter_packs = &parameter_packs;
3754 ppd.visited = new hash_set<tree>;
3755 ppd.type_pack_expansion_p = TYPE_P (arg);
3756 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3757 delete ppd.visited;
3759 /* Make sure we found some parameter packs. */
3760 if (parameter_packs == NULL_TREE)
3762 if (TYPE_P (arg))
3763 error ("expansion pattern %<%T%> contains no argument packs", arg);
3764 else
3765 error ("expansion pattern %<%E%> contains no argument packs", arg);
3766 return error_mark_node;
3768 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3770 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3772 return result;
3775 /* Checks T for any "bare" parameter packs, which have not yet been
3776 expanded, and issues an error if any are found. This operation can
3777 only be done on full expressions or types (e.g., an expression
3778 statement, "if" condition, etc.), because we could have expressions like:
3780 foo(f(g(h(args)))...)
3782 where "args" is a parameter pack. check_for_bare_parameter_packs
3783 should not be called for the subexpressions args, h(args),
3784 g(h(args)), or f(g(h(args))), because we would produce erroneous
3785 error messages.
3787 Returns TRUE and emits an error if there were bare parameter packs,
3788 returns FALSE otherwise. */
3789 bool
3790 check_for_bare_parameter_packs (tree t)
3792 tree parameter_packs = NULL_TREE;
3793 struct find_parameter_pack_data ppd;
3795 if (!processing_template_decl || !t || t == error_mark_node)
3796 return false;
3798 if (TREE_CODE (t) == TYPE_DECL)
3799 t = TREE_TYPE (t);
3801 ppd.parameter_packs = &parameter_packs;
3802 ppd.visited = new hash_set<tree>;
3803 ppd.type_pack_expansion_p = false;
3804 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3805 delete ppd.visited;
3807 if (parameter_packs)
3809 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3810 error_at (loc, "parameter packs not expanded with %<...%>:");
3811 while (parameter_packs)
3813 tree pack = TREE_VALUE (parameter_packs);
3814 tree name = NULL_TREE;
3816 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3817 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3818 name = TYPE_NAME (pack);
3819 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3820 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3821 else
3822 name = DECL_NAME (pack);
3824 if (name)
3825 inform (loc, " %qD", name);
3826 else
3827 inform (loc, " <anonymous>");
3829 parameter_packs = TREE_CHAIN (parameter_packs);
3832 return true;
3835 return false;
3838 /* Expand any parameter packs that occur in the template arguments in
3839 ARGS. */
3840 tree
3841 expand_template_argument_pack (tree args)
3843 if (args == error_mark_node)
3844 return error_mark_node;
3846 tree result_args = NULL_TREE;
3847 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3848 int num_result_args = -1;
3849 int non_default_args_count = -1;
3851 /* First, determine if we need to expand anything, and the number of
3852 slots we'll need. */
3853 for (in_arg = 0; in_arg < nargs; ++in_arg)
3855 tree arg = TREE_VEC_ELT (args, in_arg);
3856 if (arg == NULL_TREE)
3857 return args;
3858 if (ARGUMENT_PACK_P (arg))
3860 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3861 if (num_result_args < 0)
3862 num_result_args = in_arg + num_packed;
3863 else
3864 num_result_args += num_packed;
3866 else
3868 if (num_result_args >= 0)
3869 num_result_args++;
3873 /* If no expansion is necessary, we're done. */
3874 if (num_result_args < 0)
3875 return args;
3877 /* Expand arguments. */
3878 result_args = make_tree_vec (num_result_args);
3879 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3880 non_default_args_count =
3881 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3882 for (in_arg = 0; in_arg < nargs; ++in_arg)
3884 tree arg = TREE_VEC_ELT (args, in_arg);
3885 if (ARGUMENT_PACK_P (arg))
3887 tree packed = ARGUMENT_PACK_ARGS (arg);
3888 int i, num_packed = TREE_VEC_LENGTH (packed);
3889 for (i = 0; i < num_packed; ++i, ++out_arg)
3890 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3891 if (non_default_args_count > 0)
3892 non_default_args_count += num_packed - 1;
3894 else
3896 TREE_VEC_ELT (result_args, out_arg) = arg;
3897 ++out_arg;
3900 if (non_default_args_count >= 0)
3901 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3902 return result_args;
3905 /* Checks if DECL shadows a template parameter.
3907 [temp.local]: A template-parameter shall not be redeclared within its
3908 scope (including nested scopes).
3910 Emits an error and returns TRUE if the DECL shadows a parameter,
3911 returns FALSE otherwise. */
3913 bool
3914 check_template_shadow (tree decl)
3916 tree olddecl;
3918 /* If we're not in a template, we can't possibly shadow a template
3919 parameter. */
3920 if (!current_template_parms)
3921 return true;
3923 /* Figure out what we're shadowing. */
3924 if (TREE_CODE (decl) == OVERLOAD)
3925 decl = OVL_CURRENT (decl);
3926 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3928 /* If there's no previous binding for this name, we're not shadowing
3929 anything, let alone a template parameter. */
3930 if (!olddecl)
3931 return true;
3933 /* If we're not shadowing a template parameter, we're done. Note
3934 that OLDDECL might be an OVERLOAD (or perhaps even an
3935 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3936 node. */
3937 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3938 return true;
3940 /* We check for decl != olddecl to avoid bogus errors for using a
3941 name inside a class. We check TPFI to avoid duplicate errors for
3942 inline member templates. */
3943 if (decl == olddecl
3944 || (DECL_TEMPLATE_PARM_P (decl)
3945 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3946 return true;
3948 /* Don't complain about the injected class name, as we've already
3949 complained about the class itself. */
3950 if (DECL_SELF_REFERENCE_P (decl))
3951 return false;
3953 if (DECL_TEMPLATE_PARM_P (decl))
3954 error ("declaration of template parameter %q+D shadows "
3955 "template parameter", decl);
3956 else
3957 error ("declaration of %q+#D shadows template parameter", decl);
3958 inform (DECL_SOURCE_LOCATION (olddecl),
3959 "template parameter %qD declared here", olddecl);
3960 return false;
3963 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3964 ORIG_LEVEL, DECL, and TYPE. */
3966 static tree
3967 build_template_parm_index (int index,
3968 int level,
3969 int orig_level,
3970 tree decl,
3971 tree type)
3973 tree t = make_node (TEMPLATE_PARM_INDEX);
3974 TEMPLATE_PARM_IDX (t) = index;
3975 TEMPLATE_PARM_LEVEL (t) = level;
3976 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3977 TEMPLATE_PARM_DECL (t) = decl;
3978 TREE_TYPE (t) = type;
3979 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3980 TREE_READONLY (t) = TREE_READONLY (decl);
3982 return t;
3985 /* Find the canonical type parameter for the given template type
3986 parameter. Returns the canonical type parameter, which may be TYPE
3987 if no such parameter existed. */
3989 static tree
3990 canonical_type_parameter (tree type)
3992 tree list;
3993 int idx = TEMPLATE_TYPE_IDX (type);
3994 if (!canonical_template_parms)
3995 vec_alloc (canonical_template_parms, idx+1);
3997 while (canonical_template_parms->length () <= (unsigned)idx)
3998 vec_safe_push (canonical_template_parms, NULL_TREE);
4000 list = (*canonical_template_parms)[idx];
4001 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4002 list = TREE_CHAIN (list);
4004 if (list)
4005 return TREE_VALUE (list);
4006 else
4008 (*canonical_template_parms)[idx]
4009 = tree_cons (NULL_TREE, type,
4010 (*canonical_template_parms)[idx]);
4011 return type;
4015 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4016 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4017 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4018 new one is created. */
4020 static tree
4021 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4022 tsubst_flags_t complain)
4024 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4025 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4026 != TEMPLATE_PARM_LEVEL (index) - levels)
4027 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4029 tree orig_decl = TEMPLATE_PARM_DECL (index);
4030 tree decl, t;
4032 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4033 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4034 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4035 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4036 DECL_ARTIFICIAL (decl) = 1;
4037 SET_DECL_TEMPLATE_PARM_P (decl);
4039 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4040 TEMPLATE_PARM_LEVEL (index) - levels,
4041 TEMPLATE_PARM_ORIG_LEVEL (index),
4042 decl, type);
4043 TEMPLATE_PARM_DESCENDANTS (index) = t;
4044 TEMPLATE_PARM_PARAMETER_PACK (t)
4045 = TEMPLATE_PARM_PARAMETER_PACK (index);
4047 /* Template template parameters need this. */
4048 if (TREE_CODE (decl) == TEMPLATE_DECL)
4050 DECL_TEMPLATE_RESULT (decl)
4051 = build_decl (DECL_SOURCE_LOCATION (decl),
4052 TYPE_DECL, DECL_NAME (decl), type);
4053 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4054 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4055 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4059 return TEMPLATE_PARM_DESCENDANTS (index);
4062 /* Process information from new template parameter PARM and append it
4063 to the LIST being built. This new parameter is a non-type
4064 parameter iff IS_NON_TYPE is true. This new parameter is a
4065 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4066 is in PARM_LOC. */
4068 tree
4069 process_template_parm (tree list, location_t parm_loc, tree parm,
4070 bool is_non_type, bool is_parameter_pack)
4072 tree decl = 0;
4073 int idx = 0;
4075 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4076 tree defval = TREE_PURPOSE (parm);
4077 tree constr = TREE_TYPE (parm);
4079 if (list)
4081 tree p = tree_last (list);
4083 if (p && TREE_VALUE (p) != error_mark_node)
4085 p = TREE_VALUE (p);
4086 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4087 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4088 else
4089 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4092 ++idx;
4095 if (is_non_type)
4097 parm = TREE_VALUE (parm);
4099 SET_DECL_TEMPLATE_PARM_P (parm);
4101 if (TREE_TYPE (parm) != error_mark_node)
4103 /* [temp.param]
4105 The top-level cv-qualifiers on the template-parameter are
4106 ignored when determining its type. */
4107 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4108 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4109 TREE_TYPE (parm) = error_mark_node;
4110 else if (uses_parameter_packs (TREE_TYPE (parm))
4111 && !is_parameter_pack
4112 /* If we're in a nested template parameter list, the template
4113 template parameter could be a parameter pack. */
4114 && processing_template_parmlist == 1)
4116 /* This template parameter is not a parameter pack, but it
4117 should be. Complain about "bare" parameter packs. */
4118 check_for_bare_parameter_packs (TREE_TYPE (parm));
4120 /* Recover by calling this a parameter pack. */
4121 is_parameter_pack = true;
4125 /* A template parameter is not modifiable. */
4126 TREE_CONSTANT (parm) = 1;
4127 TREE_READONLY (parm) = 1;
4128 decl = build_decl (parm_loc,
4129 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4130 TREE_CONSTANT (decl) = 1;
4131 TREE_READONLY (decl) = 1;
4132 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4133 = build_template_parm_index (idx, processing_template_decl,
4134 processing_template_decl,
4135 decl, TREE_TYPE (parm));
4137 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4138 = is_parameter_pack;
4140 else
4142 tree t;
4143 parm = TREE_VALUE (TREE_VALUE (parm));
4145 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4147 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4148 /* This is for distinguishing between real templates and template
4149 template parameters */
4150 TREE_TYPE (parm) = t;
4151 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4152 decl = parm;
4154 else
4156 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4157 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4158 decl = build_decl (parm_loc,
4159 TYPE_DECL, parm, t);
4162 TYPE_NAME (t) = decl;
4163 TYPE_STUB_DECL (t) = decl;
4164 parm = decl;
4165 TEMPLATE_TYPE_PARM_INDEX (t)
4166 = build_template_parm_index (idx, processing_template_decl,
4167 processing_template_decl,
4168 decl, TREE_TYPE (parm));
4169 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4170 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4172 DECL_ARTIFICIAL (decl) = 1;
4173 SET_DECL_TEMPLATE_PARM_P (decl);
4175 /* Build requirements for the type/template parameter.
4176 This must be done after SET_DECL_TEMPLATE_PARM_P or
4177 process_template_parm could fail. */
4178 tree reqs = finish_shorthand_constraint (parm, constr);
4180 pushdecl (decl);
4182 /* Build the parameter node linking the parameter declaration,
4183 its default argument (if any), and its constraints (if any). */
4184 parm = build_tree_list (defval, parm);
4185 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4187 return chainon (list, parm);
4190 /* The end of a template parameter list has been reached. Process the
4191 tree list into a parameter vector, converting each parameter into a more
4192 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4193 as PARM_DECLs. */
4195 tree
4196 end_template_parm_list (tree parms)
4198 int nparms;
4199 tree parm, next;
4200 tree saved_parmlist = make_tree_vec (list_length (parms));
4202 /* Pop the dummy parameter level and add the real one. */
4203 current_template_parms = TREE_CHAIN (current_template_parms);
4205 current_template_parms
4206 = tree_cons (size_int (processing_template_decl),
4207 saved_parmlist, current_template_parms);
4209 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4211 next = TREE_CHAIN (parm);
4212 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4213 TREE_CHAIN (parm) = NULL_TREE;
4216 --processing_template_parmlist;
4218 return saved_parmlist;
4221 // Explicitly indicate the end of the template parameter list. We assume
4222 // that the current template parameters have been constructed and/or
4223 // managed explicitly, as when creating new template template parameters
4224 // from a shorthand constraint.
4225 void
4226 end_template_parm_list ()
4228 --processing_template_parmlist;
4231 /* end_template_decl is called after a template declaration is seen. */
4233 void
4234 end_template_decl (void)
4236 reset_specialization ();
4238 if (! processing_template_decl)
4239 return;
4241 /* This matches the pushlevel in begin_template_parm_list. */
4242 finish_scope ();
4244 --processing_template_decl;
4245 current_template_parms = TREE_CHAIN (current_template_parms);
4248 /* Takes a TREE_LIST representing a template parameter and convert it
4249 into an argument suitable to be passed to the type substitution
4250 functions. Note that If the TREE_LIST contains an error_mark
4251 node, the returned argument is error_mark_node. */
4253 tree
4254 template_parm_to_arg (tree t)
4257 if (t == NULL_TREE
4258 || TREE_CODE (t) != TREE_LIST)
4259 return t;
4261 if (error_operand_p (TREE_VALUE (t)))
4262 return error_mark_node;
4264 t = TREE_VALUE (t);
4266 if (TREE_CODE (t) == TYPE_DECL
4267 || TREE_CODE (t) == TEMPLATE_DECL)
4269 t = TREE_TYPE (t);
4271 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4273 /* Turn this argument into a TYPE_ARGUMENT_PACK
4274 with a single element, which expands T. */
4275 tree vec = make_tree_vec (1);
4276 if (CHECKING_P)
4277 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4279 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4281 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4282 SET_ARGUMENT_PACK_ARGS (t, vec);
4285 else
4287 t = DECL_INITIAL (t);
4289 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4291 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4292 with a single element, which expands T. */
4293 tree vec = make_tree_vec (1);
4294 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
4295 if (CHECKING_P)
4296 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4298 t = convert_from_reference (t);
4299 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4301 t = make_node (NONTYPE_ARGUMENT_PACK);
4302 SET_ARGUMENT_PACK_ARGS (t, vec);
4303 TREE_TYPE (t) = type;
4305 else
4306 t = convert_from_reference (t);
4308 return t;
4311 /* Given a single level of template parameters (a TREE_VEC), return it
4312 as a set of template arguments. */
4314 static tree
4315 template_parms_level_to_args (tree parms)
4317 tree a = copy_node (parms);
4318 TREE_TYPE (a) = NULL_TREE;
4319 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4320 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4322 if (CHECKING_P)
4323 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4325 return a;
4328 /* Given a set of template parameters, return them as a set of template
4329 arguments. The template parameters are represented as a TREE_VEC, in
4330 the form documented in cp-tree.h for template arguments. */
4332 static tree
4333 template_parms_to_args (tree parms)
4335 tree header;
4336 tree args = NULL_TREE;
4337 int length = TMPL_PARMS_DEPTH (parms);
4338 int l = length;
4340 /* If there is only one level of template parameters, we do not
4341 create a TREE_VEC of TREE_VECs. Instead, we return a single
4342 TREE_VEC containing the arguments. */
4343 if (length > 1)
4344 args = make_tree_vec (length);
4346 for (header = parms; header; header = TREE_CHAIN (header))
4348 tree a = template_parms_level_to_args (TREE_VALUE (header));
4350 if (length > 1)
4351 TREE_VEC_ELT (args, --l) = a;
4352 else
4353 args = a;
4356 return args;
4359 /* Within the declaration of a template, return the currently active
4360 template parameters as an argument TREE_VEC. */
4362 static tree
4363 current_template_args (void)
4365 return template_parms_to_args (current_template_parms);
4368 /* Update the declared TYPE by doing any lookups which were thought to be
4369 dependent, but are not now that we know the SCOPE of the declarator. */
4371 tree
4372 maybe_update_decl_type (tree orig_type, tree scope)
4374 tree type = orig_type;
4376 if (type == NULL_TREE)
4377 return type;
4379 if (TREE_CODE (orig_type) == TYPE_DECL)
4380 type = TREE_TYPE (type);
4382 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4383 && dependent_type_p (type)
4384 /* Don't bother building up the args in this case. */
4385 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4387 /* tsubst in the args corresponding to the template parameters,
4388 including auto if present. Most things will be unchanged, but
4389 make_typename_type and tsubst_qualified_id will resolve
4390 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4391 tree args = current_template_args ();
4392 tree auto_node = type_uses_auto (type);
4393 tree pushed;
4394 if (auto_node)
4396 tree auto_vec = make_tree_vec (1);
4397 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4398 args = add_to_template_args (args, auto_vec);
4400 pushed = push_scope (scope);
4401 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4402 if (pushed)
4403 pop_scope (scope);
4406 if (type == error_mark_node)
4407 return orig_type;
4409 if (TREE_CODE (orig_type) == TYPE_DECL)
4411 if (same_type_p (type, TREE_TYPE (orig_type)))
4412 type = orig_type;
4413 else
4414 type = TYPE_NAME (type);
4416 return type;
4419 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4420 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4421 the new template is a member template. */
4423 tree
4424 build_template_decl (tree decl, tree parms, bool member_template_p)
4426 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4427 DECL_TEMPLATE_PARMS (tmpl) = parms;
4428 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4429 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4430 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4432 return tmpl;
4435 struct template_parm_data
4437 /* The level of the template parameters we are currently
4438 processing. */
4439 int level;
4441 /* The index of the specialization argument we are currently
4442 processing. */
4443 int current_arg;
4445 /* An array whose size is the number of template parameters. The
4446 elements are nonzero if the parameter has been used in any one
4447 of the arguments processed so far. */
4448 int* parms;
4450 /* An array whose size is the number of template arguments. The
4451 elements are nonzero if the argument makes use of template
4452 parameters of this level. */
4453 int* arg_uses_template_parms;
4456 /* Subroutine of push_template_decl used to see if each template
4457 parameter in a partial specialization is used in the explicit
4458 argument list. If T is of the LEVEL given in DATA (which is
4459 treated as a template_parm_data*), then DATA->PARMS is marked
4460 appropriately. */
4462 static int
4463 mark_template_parm (tree t, void* data)
4465 int level;
4466 int idx;
4467 struct template_parm_data* tpd = (struct template_parm_data*) data;
4469 template_parm_level_and_index (t, &level, &idx);
4471 if (level == tpd->level)
4473 tpd->parms[idx] = 1;
4474 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4477 /* In C++17 the type of a non-type argument is a deduced context. */
4478 if (cxx_dialect >= cxx1z
4479 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4480 for_each_template_parm (TREE_TYPE (t),
4481 &mark_template_parm,
4482 data,
4483 NULL,
4484 /*include_nondeduced_p=*/false);
4486 /* Return zero so that for_each_template_parm will continue the
4487 traversal of the tree; we want to mark *every* template parm. */
4488 return 0;
4491 /* Process the partial specialization DECL. */
4493 static tree
4494 process_partial_specialization (tree decl)
4496 tree type = TREE_TYPE (decl);
4497 tree tinfo = get_template_info (decl);
4498 tree maintmpl = TI_TEMPLATE (tinfo);
4499 tree specargs = TI_ARGS (tinfo);
4500 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4501 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4502 tree inner_parms;
4503 tree inst;
4504 int nargs = TREE_VEC_LENGTH (inner_args);
4505 int ntparms;
4506 int i;
4507 bool did_error_intro = false;
4508 struct template_parm_data tpd;
4509 struct template_parm_data tpd2;
4511 gcc_assert (current_template_parms);
4513 /* A concept cannot be specialized. */
4514 if (flag_concepts && variable_concept_p (maintmpl))
4516 error ("specialization of variable concept %q#D", maintmpl);
4517 return error_mark_node;
4520 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4521 ntparms = TREE_VEC_LENGTH (inner_parms);
4523 /* We check that each of the template parameters given in the
4524 partial specialization is used in the argument list to the
4525 specialization. For example:
4527 template <class T> struct S;
4528 template <class T> struct S<T*>;
4530 The second declaration is OK because `T*' uses the template
4531 parameter T, whereas
4533 template <class T> struct S<int>;
4535 is no good. Even trickier is:
4537 template <class T>
4538 struct S1
4540 template <class U>
4541 struct S2;
4542 template <class U>
4543 struct S2<T>;
4546 The S2<T> declaration is actually invalid; it is a
4547 full-specialization. Of course,
4549 template <class U>
4550 struct S2<T (*)(U)>;
4552 or some such would have been OK. */
4553 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4554 tpd.parms = XALLOCAVEC (int, ntparms);
4555 memset (tpd.parms, 0, sizeof (int) * ntparms);
4557 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4558 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4559 for (i = 0; i < nargs; ++i)
4561 tpd.current_arg = i;
4562 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4563 &mark_template_parm,
4564 &tpd,
4565 NULL,
4566 /*include_nondeduced_p=*/false);
4568 for (i = 0; i < ntparms; ++i)
4569 if (tpd.parms[i] == 0)
4571 /* One of the template parms was not used in a deduced context in the
4572 specialization. */
4573 if (!did_error_intro)
4575 error ("template parameters not deducible in "
4576 "partial specialization:");
4577 did_error_intro = true;
4580 inform (input_location, " %qD",
4581 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4584 if (did_error_intro)
4585 return error_mark_node;
4587 /* [temp.class.spec]
4589 The argument list of the specialization shall not be identical to
4590 the implicit argument list of the primary template. */
4591 tree main_args
4592 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4593 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4594 && (!flag_concepts
4595 || !strictly_subsumes (current_template_constraints (),
4596 get_constraints (maintmpl))))
4598 if (!flag_concepts)
4599 error ("partial specialization %q+D does not specialize "
4600 "any template arguments", decl);
4601 else
4602 error ("partial specialization %q+D does not specialize any "
4603 "template arguments and is not more constrained than", decl);
4604 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4607 /* A partial specialization that replaces multiple parameters of the
4608 primary template with a pack expansion is less specialized for those
4609 parameters. */
4610 if (nargs < DECL_NTPARMS (maintmpl))
4612 error ("partial specialization is not more specialized than the "
4613 "primary template because it replaces multiple parameters "
4614 "with a pack expansion");
4615 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4616 /* Avoid crash in process_partial_specialization. */
4617 return decl;
4620 /* If we aren't in a dependent class, we can actually try deduction. */
4621 else if (tpd.level == 1
4622 /* FIXME we should be able to handle a partial specialization of a
4623 partial instantiation, but currently we can't (c++/41727). */
4624 && TMPL_ARGS_DEPTH (specargs) == 1
4625 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4627 if (permerror (input_location, "partial specialization %qD is not "
4628 "more specialized than", decl))
4629 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4630 maintmpl);
4633 /* [temp.class.spec]
4635 A partially specialized non-type argument expression shall not
4636 involve template parameters of the partial specialization except
4637 when the argument expression is a simple identifier.
4639 The type of a template parameter corresponding to a specialized
4640 non-type argument shall not be dependent on a parameter of the
4641 specialization.
4643 Also, we verify that pack expansions only occur at the
4644 end of the argument list. */
4645 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4646 tpd2.parms = 0;
4647 for (i = 0; i < nargs; ++i)
4649 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4650 tree arg = TREE_VEC_ELT (inner_args, i);
4651 tree packed_args = NULL_TREE;
4652 int j, len = 1;
4654 if (ARGUMENT_PACK_P (arg))
4656 /* Extract the arguments from the argument pack. We'll be
4657 iterating over these in the following loop. */
4658 packed_args = ARGUMENT_PACK_ARGS (arg);
4659 len = TREE_VEC_LENGTH (packed_args);
4662 for (j = 0; j < len; j++)
4664 if (packed_args)
4665 /* Get the Jth argument in the parameter pack. */
4666 arg = TREE_VEC_ELT (packed_args, j);
4668 if (PACK_EXPANSION_P (arg))
4670 /* Pack expansions must come at the end of the
4671 argument list. */
4672 if ((packed_args && j < len - 1)
4673 || (!packed_args && i < nargs - 1))
4675 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4676 error ("parameter pack argument %qE must be at the "
4677 "end of the template argument list", arg);
4678 else
4679 error ("parameter pack argument %qT must be at the "
4680 "end of the template argument list", arg);
4684 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4685 /* We only care about the pattern. */
4686 arg = PACK_EXPANSION_PATTERN (arg);
4688 if (/* These first two lines are the `non-type' bit. */
4689 !TYPE_P (arg)
4690 && TREE_CODE (arg) != TEMPLATE_DECL
4691 /* This next two lines are the `argument expression is not just a
4692 simple identifier' condition and also the `specialized
4693 non-type argument' bit. */
4694 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4695 && !(REFERENCE_REF_P (arg)
4696 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4698 if ((!packed_args && tpd.arg_uses_template_parms[i])
4699 || (packed_args && uses_template_parms (arg)))
4700 error ("template argument %qE involves template parameter(s)",
4701 arg);
4702 else
4704 /* Look at the corresponding template parameter,
4705 marking which template parameters its type depends
4706 upon. */
4707 tree type = TREE_TYPE (parm);
4709 if (!tpd2.parms)
4711 /* We haven't yet initialized TPD2. Do so now. */
4712 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4713 /* The number of parameters here is the number in the
4714 main template, which, as checked in the assertion
4715 above, is NARGS. */
4716 tpd2.parms = XALLOCAVEC (int, nargs);
4717 tpd2.level =
4718 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4721 /* Mark the template parameters. But this time, we're
4722 looking for the template parameters of the main
4723 template, not in the specialization. */
4724 tpd2.current_arg = i;
4725 tpd2.arg_uses_template_parms[i] = 0;
4726 memset (tpd2.parms, 0, sizeof (int) * nargs);
4727 for_each_template_parm (type,
4728 &mark_template_parm,
4729 &tpd2,
4730 NULL,
4731 /*include_nondeduced_p=*/false);
4733 if (tpd2.arg_uses_template_parms [i])
4735 /* The type depended on some template parameters.
4736 If they are fully specialized in the
4737 specialization, that's OK. */
4738 int j;
4739 int count = 0;
4740 for (j = 0; j < nargs; ++j)
4741 if (tpd2.parms[j] != 0
4742 && tpd.arg_uses_template_parms [j])
4743 ++count;
4744 if (count != 0)
4745 error_n (input_location, count,
4746 "type %qT of template argument %qE depends "
4747 "on a template parameter",
4748 "type %qT of template argument %qE depends "
4749 "on template parameters",
4750 type,
4751 arg);
4758 /* We should only get here once. */
4759 if (TREE_CODE (decl) == TYPE_DECL)
4760 gcc_assert (!COMPLETE_TYPE_P (type));
4762 // Build the template decl.
4763 tree tmpl = build_template_decl (decl, current_template_parms,
4764 DECL_MEMBER_TEMPLATE_P (maintmpl));
4765 TREE_TYPE (tmpl) = type;
4766 DECL_TEMPLATE_RESULT (tmpl) = decl;
4767 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4768 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4769 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4771 /* Give template template parms a DECL_CONTEXT of the template
4772 for which they are a parameter. */
4773 for (i = 0; i < ntparms; ++i)
4775 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4776 if (TREE_CODE (parm) == TEMPLATE_DECL)
4777 DECL_CONTEXT (parm) = tmpl;
4780 if (VAR_P (decl))
4781 /* We didn't register this in check_explicit_specialization so we could
4782 wait until the constraints were set. */
4783 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4784 else
4785 associate_classtype_constraints (type);
4787 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4788 = tree_cons (specargs, tmpl,
4789 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4790 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4792 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4793 inst = TREE_CHAIN (inst))
4795 tree instance = TREE_VALUE (inst);
4796 if (TYPE_P (instance)
4797 ? (COMPLETE_TYPE_P (instance)
4798 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4799 : DECL_TEMPLATE_INSTANTIATION (instance))
4801 tree spec = most_specialized_partial_spec (instance, tf_none);
4802 tree inst_decl = (DECL_P (instance)
4803 ? instance : TYPE_NAME (instance));
4804 if (!spec)
4805 /* OK */;
4806 else if (spec == error_mark_node)
4807 permerror (input_location,
4808 "declaration of %qD ambiguates earlier template "
4809 "instantiation for %qD", decl, inst_decl);
4810 else if (TREE_VALUE (spec) == tmpl)
4811 permerror (input_location,
4812 "partial specialization of %qD after instantiation "
4813 "of %qD", decl, inst_decl);
4817 return decl;
4820 /* PARM is a template parameter of some form; return the corresponding
4821 TEMPLATE_PARM_INDEX. */
4823 static tree
4824 get_template_parm_index (tree parm)
4826 if (TREE_CODE (parm) == PARM_DECL
4827 || TREE_CODE (parm) == CONST_DECL)
4828 parm = DECL_INITIAL (parm);
4829 else if (TREE_CODE (parm) == TYPE_DECL
4830 || TREE_CODE (parm) == TEMPLATE_DECL)
4831 parm = TREE_TYPE (parm);
4832 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4833 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4834 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4835 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4836 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4837 return parm;
4840 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4841 parameter packs used by the template parameter PARM. */
4843 static void
4844 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4846 /* A type parm can't refer to another parm. */
4847 if (TREE_CODE (parm) == TYPE_DECL)
4848 return;
4849 else if (TREE_CODE (parm) == PARM_DECL)
4851 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4852 ppd, ppd->visited);
4853 return;
4856 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4858 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4859 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4860 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4863 /* PARM is a template parameter pack. Return any parameter packs used in
4864 its type or the type of any of its template parameters. If there are
4865 any such packs, it will be instantiated into a fixed template parameter
4866 list by partial instantiation rather than be fully deduced. */
4868 tree
4869 fixed_parameter_pack_p (tree parm)
4871 /* This can only be true in a member template. */
4872 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4873 return NULL_TREE;
4874 /* This can only be true for a parameter pack. */
4875 if (!template_parameter_pack_p (parm))
4876 return NULL_TREE;
4877 /* A type parm can't refer to another parm. */
4878 if (TREE_CODE (parm) == TYPE_DECL)
4879 return NULL_TREE;
4881 tree parameter_packs = NULL_TREE;
4882 struct find_parameter_pack_data ppd;
4883 ppd.parameter_packs = &parameter_packs;
4884 ppd.visited = new hash_set<tree>;
4885 ppd.type_pack_expansion_p = false;
4887 fixed_parameter_pack_p_1 (parm, &ppd);
4889 delete ppd.visited;
4890 return parameter_packs;
4893 /* Check that a template declaration's use of default arguments and
4894 parameter packs is not invalid. Here, PARMS are the template
4895 parameters. IS_PRIMARY is true if DECL is the thing declared by
4896 a primary template. IS_PARTIAL is true if DECL is a partial
4897 specialization.
4899 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4900 declaration (but not a definition); 1 indicates a declaration, 2
4901 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4902 emitted for extraneous default arguments.
4904 Returns TRUE if there were no errors found, FALSE otherwise. */
4906 bool
4907 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4908 bool is_partial, int is_friend_decl)
4910 const char *msg;
4911 int last_level_to_check;
4912 tree parm_level;
4913 bool no_errors = true;
4915 /* [temp.param]
4917 A default template-argument shall not be specified in a
4918 function template declaration or a function template definition, nor
4919 in the template-parameter-list of the definition of a member of a
4920 class template. */
4922 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4923 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4924 /* You can't have a function template declaration in a local
4925 scope, nor you can you define a member of a class template in a
4926 local scope. */
4927 return true;
4929 if ((TREE_CODE (decl) == TYPE_DECL
4930 && TREE_TYPE (decl)
4931 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4932 || (TREE_CODE (decl) == FUNCTION_DECL
4933 && LAMBDA_FUNCTION_P (decl)))
4934 /* A lambda doesn't have an explicit declaration; don't complain
4935 about the parms of the enclosing class. */
4936 return true;
4938 if (current_class_type
4939 && !TYPE_BEING_DEFINED (current_class_type)
4940 && DECL_LANG_SPECIFIC (decl)
4941 && DECL_DECLARES_FUNCTION_P (decl)
4942 /* If this is either a friend defined in the scope of the class
4943 or a member function. */
4944 && (DECL_FUNCTION_MEMBER_P (decl)
4945 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4946 : DECL_FRIEND_CONTEXT (decl)
4947 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4948 : false)
4949 /* And, if it was a member function, it really was defined in
4950 the scope of the class. */
4951 && (!DECL_FUNCTION_MEMBER_P (decl)
4952 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4953 /* We already checked these parameters when the template was
4954 declared, so there's no need to do it again now. This function
4955 was defined in class scope, but we're processing its body now
4956 that the class is complete. */
4957 return true;
4959 /* Core issue 226 (C++0x only): the following only applies to class
4960 templates. */
4961 if (is_primary
4962 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4964 /* [temp.param]
4966 If a template-parameter has a default template-argument, all
4967 subsequent template-parameters shall have a default
4968 template-argument supplied. */
4969 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4971 tree inner_parms = TREE_VALUE (parm_level);
4972 int ntparms = TREE_VEC_LENGTH (inner_parms);
4973 int seen_def_arg_p = 0;
4974 int i;
4976 for (i = 0; i < ntparms; ++i)
4978 tree parm = TREE_VEC_ELT (inner_parms, i);
4980 if (parm == error_mark_node)
4981 continue;
4983 if (TREE_PURPOSE (parm))
4984 seen_def_arg_p = 1;
4985 else if (seen_def_arg_p
4986 && !template_parameter_pack_p (TREE_VALUE (parm)))
4988 error ("no default argument for %qD", TREE_VALUE (parm));
4989 /* For better subsequent error-recovery, we indicate that
4990 there should have been a default argument. */
4991 TREE_PURPOSE (parm) = error_mark_node;
4992 no_errors = false;
4994 else if (!is_partial
4995 && !is_friend_decl
4996 /* Don't complain about an enclosing partial
4997 specialization. */
4998 && parm_level == parms
4999 && TREE_CODE (decl) == TYPE_DECL
5000 && i < ntparms - 1
5001 && template_parameter_pack_p (TREE_VALUE (parm))
5002 /* A fixed parameter pack will be partially
5003 instantiated into a fixed length list. */
5004 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5006 /* A primary class template can only have one
5007 parameter pack, at the end of the template
5008 parameter list. */
5010 error ("parameter pack %q+D must be at the end of the"
5011 " template parameter list", TREE_VALUE (parm));
5013 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5014 = error_mark_node;
5015 no_errors = false;
5021 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5022 || is_partial
5023 || !is_primary
5024 || is_friend_decl)
5025 /* For an ordinary class template, default template arguments are
5026 allowed at the innermost level, e.g.:
5027 template <class T = int>
5028 struct S {};
5029 but, in a partial specialization, they're not allowed even
5030 there, as we have in [temp.class.spec]:
5032 The template parameter list of a specialization shall not
5033 contain default template argument values.
5035 So, for a partial specialization, or for a function template
5036 (in C++98/C++03), we look at all of them. */
5038 else
5039 /* But, for a primary class template that is not a partial
5040 specialization we look at all template parameters except the
5041 innermost ones. */
5042 parms = TREE_CHAIN (parms);
5044 /* Figure out what error message to issue. */
5045 if (is_friend_decl == 2)
5046 msg = G_("default template arguments may not be used in function template "
5047 "friend re-declaration");
5048 else if (is_friend_decl)
5049 msg = G_("default template arguments may not be used in function template "
5050 "friend declarations");
5051 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5052 msg = G_("default template arguments may not be used in function templates "
5053 "without -std=c++11 or -std=gnu++11");
5054 else if (is_partial)
5055 msg = G_("default template arguments may not be used in "
5056 "partial specializations");
5057 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5058 msg = G_("default argument for template parameter for class enclosing %qD");
5059 else
5060 /* Per [temp.param]/9, "A default template-argument shall not be
5061 specified in the template-parameter-lists of the definition of
5062 a member of a class template that appears outside of the member's
5063 class.", thus if we aren't handling a member of a class template
5064 there is no need to examine the parameters. */
5065 return true;
5067 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5068 /* If we're inside a class definition, there's no need to
5069 examine the parameters to the class itself. On the one
5070 hand, they will be checked when the class is defined, and,
5071 on the other, default arguments are valid in things like:
5072 template <class T = double>
5073 struct S { template <class U> void f(U); };
5074 Here the default argument for `S' has no bearing on the
5075 declaration of `f'. */
5076 last_level_to_check = template_class_depth (current_class_type) + 1;
5077 else
5078 /* Check everything. */
5079 last_level_to_check = 0;
5081 for (parm_level = parms;
5082 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5083 parm_level = TREE_CHAIN (parm_level))
5085 tree inner_parms = TREE_VALUE (parm_level);
5086 int i;
5087 int ntparms;
5089 ntparms = TREE_VEC_LENGTH (inner_parms);
5090 for (i = 0; i < ntparms; ++i)
5092 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5093 continue;
5095 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5097 if (msg)
5099 no_errors = false;
5100 if (is_friend_decl == 2)
5101 return no_errors;
5103 error (msg, decl);
5104 msg = 0;
5107 /* Clear out the default argument so that we are not
5108 confused later. */
5109 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5113 /* At this point, if we're still interested in issuing messages,
5114 they must apply to classes surrounding the object declared. */
5115 if (msg)
5116 msg = G_("default argument for template parameter for class "
5117 "enclosing %qD");
5120 return no_errors;
5123 /* Worker for push_template_decl_real, called via
5124 for_each_template_parm. DATA is really an int, indicating the
5125 level of the parameters we are interested in. If T is a template
5126 parameter of that level, return nonzero. */
5128 static int
5129 template_parm_this_level_p (tree t, void* data)
5131 int this_level = *(int *)data;
5132 int level;
5134 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5135 level = TEMPLATE_PARM_LEVEL (t);
5136 else
5137 level = TEMPLATE_TYPE_LEVEL (t);
5138 return level == this_level;
5141 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5142 DATA is really an int, indicating the innermost outer level of parameters.
5143 If T is a template parameter of that level or further out, return
5144 nonzero. */
5146 static int
5147 template_parm_outer_level (tree t, void *data)
5149 int this_level = *(int *)data;
5150 int level;
5152 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5153 level = TEMPLATE_PARM_LEVEL (t);
5154 else
5155 level = TEMPLATE_TYPE_LEVEL (t);
5156 return level <= this_level;
5159 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5160 parameters given by current_template_args, or reuses a
5161 previously existing one, if appropriate. Returns the DECL, or an
5162 equivalent one, if it is replaced via a call to duplicate_decls.
5164 If IS_FRIEND is true, DECL is a friend declaration. */
5166 tree
5167 push_template_decl_real (tree decl, bool is_friend)
5169 tree tmpl;
5170 tree args;
5171 tree info;
5172 tree ctx;
5173 bool is_primary;
5174 bool is_partial;
5175 int new_template_p = 0;
5176 /* True if the template is a member template, in the sense of
5177 [temp.mem]. */
5178 bool member_template_p = false;
5180 if (decl == error_mark_node || !current_template_parms)
5181 return error_mark_node;
5183 /* See if this is a partial specialization. */
5184 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5185 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5186 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5187 || (VAR_P (decl)
5188 && DECL_LANG_SPECIFIC (decl)
5189 && DECL_TEMPLATE_SPECIALIZATION (decl)
5190 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5192 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5193 is_friend = true;
5195 if (is_friend)
5196 /* For a friend, we want the context of the friend function, not
5197 the type of which it is a friend. */
5198 ctx = CP_DECL_CONTEXT (decl);
5199 else if (CP_DECL_CONTEXT (decl)
5200 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5201 /* In the case of a virtual function, we want the class in which
5202 it is defined. */
5203 ctx = CP_DECL_CONTEXT (decl);
5204 else
5205 /* Otherwise, if we're currently defining some class, the DECL
5206 is assumed to be a member of the class. */
5207 ctx = current_scope ();
5209 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5210 ctx = NULL_TREE;
5212 if (!DECL_CONTEXT (decl))
5213 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5215 /* See if this is a primary template. */
5216 if (is_friend && ctx
5217 && uses_template_parms_level (ctx, processing_template_decl))
5218 /* A friend template that specifies a class context, i.e.
5219 template <typename T> friend void A<T>::f();
5220 is not primary. */
5221 is_primary = false;
5222 else if (TREE_CODE (decl) == TYPE_DECL
5223 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5224 is_primary = false;
5225 else
5226 is_primary = template_parm_scope_p ();
5228 if (is_primary)
5230 warning (OPT_Wtemplates, "template %qD declared", decl);
5232 if (DECL_CLASS_SCOPE_P (decl))
5233 member_template_p = true;
5234 if (TREE_CODE (decl) == TYPE_DECL
5235 && anon_aggrname_p (DECL_NAME (decl)))
5237 error ("template class without a name");
5238 return error_mark_node;
5240 else if (TREE_CODE (decl) == FUNCTION_DECL)
5242 if (member_template_p)
5244 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5245 error ("member template %qD may not have virt-specifiers", decl);
5247 if (DECL_DESTRUCTOR_P (decl))
5249 /* [temp.mem]
5251 A destructor shall not be a member template. */
5252 error ("destructor %qD declared as member template", decl);
5253 return error_mark_node;
5255 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5256 && (!prototype_p (TREE_TYPE (decl))
5257 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5258 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5259 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5260 == void_list_node)))
5262 /* [basic.stc.dynamic.allocation]
5264 An allocation function can be a function
5265 template. ... Template allocation functions shall
5266 have two or more parameters. */
5267 error ("invalid template declaration of %qD", decl);
5268 return error_mark_node;
5271 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5272 && CLASS_TYPE_P (TREE_TYPE (decl)))
5274 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5275 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5276 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5278 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5279 if (TREE_CODE (t) == TYPE_DECL)
5280 t = TREE_TYPE (t);
5281 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5282 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5285 else if (TREE_CODE (decl) == TYPE_DECL
5286 && TYPE_DECL_ALIAS_P (decl))
5287 /* alias-declaration */
5288 gcc_assert (!DECL_ARTIFICIAL (decl));
5289 else if (VAR_P (decl))
5290 /* C++14 variable template. */;
5291 else
5293 error ("template declaration of %q#D", decl);
5294 return error_mark_node;
5298 /* Check to see that the rules regarding the use of default
5299 arguments are not being violated. */
5300 check_default_tmpl_args (decl, current_template_parms,
5301 is_primary, is_partial, /*is_friend_decl=*/0);
5303 /* Ensure that there are no parameter packs in the type of this
5304 declaration that have not been expanded. */
5305 if (TREE_CODE (decl) == FUNCTION_DECL)
5307 /* Check each of the arguments individually to see if there are
5308 any bare parameter packs. */
5309 tree type = TREE_TYPE (decl);
5310 tree arg = DECL_ARGUMENTS (decl);
5311 tree argtype = TYPE_ARG_TYPES (type);
5313 while (arg && argtype)
5315 if (!DECL_PACK_P (arg)
5316 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5318 /* This is a PARM_DECL that contains unexpanded parameter
5319 packs. We have already complained about this in the
5320 check_for_bare_parameter_packs call, so just replace
5321 these types with ERROR_MARK_NODE. */
5322 TREE_TYPE (arg) = error_mark_node;
5323 TREE_VALUE (argtype) = error_mark_node;
5326 arg = DECL_CHAIN (arg);
5327 argtype = TREE_CHAIN (argtype);
5330 /* Check for bare parameter packs in the return type and the
5331 exception specifiers. */
5332 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5333 /* Errors were already issued, set return type to int
5334 as the frontend doesn't expect error_mark_node as
5335 the return type. */
5336 TREE_TYPE (type) = integer_type_node;
5337 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5338 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5340 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5341 && TYPE_DECL_ALIAS_P (decl))
5342 ? DECL_ORIGINAL_TYPE (decl)
5343 : TREE_TYPE (decl)))
5345 TREE_TYPE (decl) = error_mark_node;
5346 return error_mark_node;
5349 if (is_partial)
5350 return process_partial_specialization (decl);
5352 args = current_template_args ();
5354 if (!ctx
5355 || TREE_CODE (ctx) == FUNCTION_DECL
5356 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5357 || (TREE_CODE (decl) == TYPE_DECL
5358 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5359 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5361 if (DECL_LANG_SPECIFIC (decl)
5362 && DECL_TEMPLATE_INFO (decl)
5363 && DECL_TI_TEMPLATE (decl))
5364 tmpl = DECL_TI_TEMPLATE (decl);
5365 /* If DECL is a TYPE_DECL for a class-template, then there won't
5366 be DECL_LANG_SPECIFIC. The information equivalent to
5367 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5368 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5369 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5370 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5372 /* Since a template declaration already existed for this
5373 class-type, we must be redeclaring it here. Make sure
5374 that the redeclaration is valid. */
5375 redeclare_class_template (TREE_TYPE (decl),
5376 current_template_parms,
5377 current_template_constraints ());
5378 /* We don't need to create a new TEMPLATE_DECL; just use the
5379 one we already had. */
5380 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5382 else
5384 tmpl = build_template_decl (decl, current_template_parms,
5385 member_template_p);
5386 new_template_p = 1;
5388 if (DECL_LANG_SPECIFIC (decl)
5389 && DECL_TEMPLATE_SPECIALIZATION (decl))
5391 /* A specialization of a member template of a template
5392 class. */
5393 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5394 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5395 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5399 else
5401 tree a, t, current, parms;
5402 int i;
5403 tree tinfo = get_template_info (decl);
5405 if (!tinfo)
5407 error ("template definition of non-template %q#D", decl);
5408 return error_mark_node;
5411 tmpl = TI_TEMPLATE (tinfo);
5413 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5414 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5415 && DECL_TEMPLATE_SPECIALIZATION (decl)
5416 && DECL_MEMBER_TEMPLATE_P (tmpl))
5418 tree new_tmpl;
5420 /* The declaration is a specialization of a member
5421 template, declared outside the class. Therefore, the
5422 innermost template arguments will be NULL, so we
5423 replace them with the arguments determined by the
5424 earlier call to check_explicit_specialization. */
5425 args = DECL_TI_ARGS (decl);
5427 new_tmpl
5428 = build_template_decl (decl, current_template_parms,
5429 member_template_p);
5430 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5431 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5432 DECL_TI_TEMPLATE (decl) = new_tmpl;
5433 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5434 DECL_TEMPLATE_INFO (new_tmpl)
5435 = build_template_info (tmpl, args);
5437 register_specialization (new_tmpl,
5438 most_general_template (tmpl),
5439 args,
5440 is_friend, 0);
5441 return decl;
5444 /* Make sure the template headers we got make sense. */
5446 parms = DECL_TEMPLATE_PARMS (tmpl);
5447 i = TMPL_PARMS_DEPTH (parms);
5448 if (TMPL_ARGS_DEPTH (args) != i)
5450 error ("expected %d levels of template parms for %q#D, got %d",
5451 i, decl, TMPL_ARGS_DEPTH (args));
5452 DECL_INTERFACE_KNOWN (decl) = 1;
5453 return error_mark_node;
5455 else
5456 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5458 a = TMPL_ARGS_LEVEL (args, i);
5459 t = INNERMOST_TEMPLATE_PARMS (parms);
5461 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5463 if (current == decl)
5464 error ("got %d template parameters for %q#D",
5465 TREE_VEC_LENGTH (a), decl);
5466 else
5467 error ("got %d template parameters for %q#T",
5468 TREE_VEC_LENGTH (a), current);
5469 error (" but %d required", TREE_VEC_LENGTH (t));
5470 /* Avoid crash in import_export_decl. */
5471 DECL_INTERFACE_KNOWN (decl) = 1;
5472 return error_mark_node;
5475 if (current == decl)
5476 current = ctx;
5477 else if (current == NULL_TREE)
5478 /* Can happen in erroneous input. */
5479 break;
5480 else
5481 current = get_containing_scope (current);
5484 /* Check that the parms are used in the appropriate qualifying scopes
5485 in the declarator. */
5486 if (!comp_template_args
5487 (TI_ARGS (tinfo),
5488 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5490 error ("\
5491 template arguments to %qD do not match original template %qD",
5492 decl, DECL_TEMPLATE_RESULT (tmpl));
5493 if (!uses_template_parms (TI_ARGS (tinfo)))
5494 inform (input_location, "use template<> for an explicit specialization");
5495 /* Avoid crash in import_export_decl. */
5496 DECL_INTERFACE_KNOWN (decl) = 1;
5497 return error_mark_node;
5501 DECL_TEMPLATE_RESULT (tmpl) = decl;
5502 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5504 /* Push template declarations for global functions and types. Note
5505 that we do not try to push a global template friend declared in a
5506 template class; such a thing may well depend on the template
5507 parameters of the class. */
5508 if (new_template_p && !ctx
5509 && !(is_friend && template_class_depth (current_class_type) > 0))
5511 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5512 if (tmpl == error_mark_node)
5513 return error_mark_node;
5515 /* Hide template friend classes that haven't been declared yet. */
5516 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5518 DECL_ANTICIPATED (tmpl) = 1;
5519 DECL_FRIEND_P (tmpl) = 1;
5523 if (is_primary)
5525 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5526 int i;
5528 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5529 if (DECL_CONV_FN_P (tmpl))
5531 int depth = TMPL_PARMS_DEPTH (parms);
5533 /* It is a conversion operator. See if the type converted to
5534 depends on innermost template operands. */
5536 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5537 depth))
5538 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5541 /* Give template template parms a DECL_CONTEXT of the template
5542 for which they are a parameter. */
5543 parms = INNERMOST_TEMPLATE_PARMS (parms);
5544 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5546 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5547 if (TREE_CODE (parm) == TEMPLATE_DECL)
5548 DECL_CONTEXT (parm) = tmpl;
5551 if (TREE_CODE (decl) == TYPE_DECL
5552 && TYPE_DECL_ALIAS_P (decl)
5553 && complex_alias_template_p (tmpl))
5554 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5557 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5558 back to its most general template. If TMPL is a specialization,
5559 ARGS may only have the innermost set of arguments. Add the missing
5560 argument levels if necessary. */
5561 if (DECL_TEMPLATE_INFO (tmpl))
5562 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5564 info = build_template_info (tmpl, args);
5566 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5567 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5568 else
5570 if (is_primary && !DECL_LANG_SPECIFIC (decl))
5571 retrofit_lang_decl (decl);
5572 if (DECL_LANG_SPECIFIC (decl))
5573 DECL_TEMPLATE_INFO (decl) = info;
5576 if (flag_implicit_templates
5577 && !is_friend
5578 && TREE_PUBLIC (decl)
5579 && VAR_OR_FUNCTION_DECL_P (decl))
5580 /* Set DECL_COMDAT on template instantiations; if we force
5581 them to be emitted by explicit instantiation or -frepo,
5582 mark_needed will tell cgraph to do the right thing. */
5583 DECL_COMDAT (decl) = true;
5585 return DECL_TEMPLATE_RESULT (tmpl);
5588 tree
5589 push_template_decl (tree decl)
5591 return push_template_decl_real (decl, false);
5594 /* FN is an inheriting constructor that inherits from the constructor
5595 template INHERITED; turn FN into a constructor template with a matching
5596 template header. */
5598 tree
5599 add_inherited_template_parms (tree fn, tree inherited)
5601 tree inner_parms
5602 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5603 inner_parms = copy_node (inner_parms);
5604 tree parms
5605 = tree_cons (size_int (processing_template_decl + 1),
5606 inner_parms, current_template_parms);
5607 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5608 tree args = template_parms_to_args (parms);
5609 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5610 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5611 DECL_TEMPLATE_RESULT (tmpl) = fn;
5612 DECL_ARTIFICIAL (tmpl) = true;
5613 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5614 return tmpl;
5617 /* Called when a class template TYPE is redeclared with the indicated
5618 template PARMS, e.g.:
5620 template <class T> struct S;
5621 template <class T> struct S {}; */
5623 bool
5624 redeclare_class_template (tree type, tree parms, tree cons)
5626 tree tmpl;
5627 tree tmpl_parms;
5628 int i;
5630 if (!TYPE_TEMPLATE_INFO (type))
5632 error ("%qT is not a template type", type);
5633 return false;
5636 tmpl = TYPE_TI_TEMPLATE (type);
5637 if (!PRIMARY_TEMPLATE_P (tmpl))
5638 /* The type is nested in some template class. Nothing to worry
5639 about here; there are no new template parameters for the nested
5640 type. */
5641 return true;
5643 if (!parms)
5645 error ("template specifiers not specified in declaration of %qD",
5646 tmpl);
5647 return false;
5650 parms = INNERMOST_TEMPLATE_PARMS (parms);
5651 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5653 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5655 error_n (input_location, TREE_VEC_LENGTH (parms),
5656 "redeclared with %d template parameter",
5657 "redeclared with %d template parameters",
5658 TREE_VEC_LENGTH (parms));
5659 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5660 "previous declaration %qD used %d template parameter",
5661 "previous declaration %qD used %d template parameters",
5662 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5663 return false;
5666 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5668 tree tmpl_parm;
5669 tree parm;
5670 tree tmpl_default;
5671 tree parm_default;
5673 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5674 || TREE_VEC_ELT (parms, i) == error_mark_node)
5675 continue;
5677 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5678 if (error_operand_p (tmpl_parm))
5679 return false;
5681 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5682 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5683 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5685 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5686 TEMPLATE_DECL. */
5687 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5688 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5689 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5690 || (TREE_CODE (tmpl_parm) != PARM_DECL
5691 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5692 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5693 || (TREE_CODE (tmpl_parm) == PARM_DECL
5694 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5695 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5697 error ("template parameter %q+#D", tmpl_parm);
5698 error ("redeclared here as %q#D", parm);
5699 return false;
5702 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5704 /* We have in [temp.param]:
5706 A template-parameter may not be given default arguments
5707 by two different declarations in the same scope. */
5708 error_at (input_location, "redefinition of default argument for %q#D", parm);
5709 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5710 "original definition appeared here");
5711 return false;
5714 if (parm_default != NULL_TREE)
5715 /* Update the previous template parameters (which are the ones
5716 that will really count) with the new default value. */
5717 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5718 else if (tmpl_default != NULL_TREE)
5719 /* Update the new parameters, too; they'll be used as the
5720 parameters for any members. */
5721 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5723 /* Give each template template parm in this redeclaration a
5724 DECL_CONTEXT of the template for which they are a parameter. */
5725 if (TREE_CODE (parm) == TEMPLATE_DECL)
5727 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5728 DECL_CONTEXT (parm) = tmpl;
5732 // Cannot redeclare a class template with a different set of constraints.
5733 if (!equivalent_constraints (get_constraints (tmpl), cons))
5735 error_at (input_location, "redeclaration %q#D with different "
5736 "constraints", tmpl);
5737 inform (DECL_SOURCE_LOCATION (tmpl),
5738 "original declaration appeared here");
5741 return true;
5744 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5745 to be used when the caller has already checked
5746 (processing_template_decl
5747 && !instantiation_dependent_expression_p (expr)
5748 && potential_constant_expression (expr))
5749 and cleared processing_template_decl. */
5751 tree
5752 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5754 return tsubst_copy_and_build (expr,
5755 /*args=*/NULL_TREE,
5756 complain,
5757 /*in_decl=*/NULL_TREE,
5758 /*function_p=*/false,
5759 /*integral_constant_expression_p=*/true);
5762 /* Simplify EXPR if it is a non-dependent expression. Returns the
5763 (possibly simplified) expression. */
5765 tree
5766 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5768 if (expr == NULL_TREE)
5769 return NULL_TREE;
5771 /* If we're in a template, but EXPR isn't value dependent, simplify
5772 it. We're supposed to treat:
5774 template <typename T> void f(T[1 + 1]);
5775 template <typename T> void f(T[2]);
5777 as two declarations of the same function, for example. */
5778 if (processing_template_decl
5779 && potential_nondependent_constant_expression (expr))
5781 processing_template_decl_sentinel s;
5782 expr = instantiate_non_dependent_expr_internal (expr, complain);
5784 return expr;
5787 tree
5788 instantiate_non_dependent_expr (tree expr)
5790 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5793 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5794 an uninstantiated expression. */
5796 tree
5797 instantiate_non_dependent_or_null (tree expr)
5799 if (expr == NULL_TREE)
5800 return NULL_TREE;
5801 if (processing_template_decl)
5803 if (!potential_nondependent_constant_expression (expr))
5804 expr = NULL_TREE;
5805 else
5807 processing_template_decl_sentinel s;
5808 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5811 return expr;
5814 /* True iff T is a specialization of a variable template. */
5816 bool
5817 variable_template_specialization_p (tree t)
5819 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5820 return false;
5821 tree tmpl = DECL_TI_TEMPLATE (t);
5822 return variable_template_p (tmpl);
5825 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5826 template declaration, or a TYPE_DECL for an alias declaration. */
5828 bool
5829 alias_type_or_template_p (tree t)
5831 if (t == NULL_TREE)
5832 return false;
5833 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5834 || (TYPE_P (t)
5835 && TYPE_NAME (t)
5836 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5837 || DECL_ALIAS_TEMPLATE_P (t));
5840 /* Return TRUE iff T is a specialization of an alias template. */
5842 bool
5843 alias_template_specialization_p (const_tree t)
5845 /* It's an alias template specialization if it's an alias and its
5846 TYPE_NAME is a specialization of a primary template. */
5847 if (TYPE_ALIAS_P (t))
5848 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5849 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5851 return false;
5854 /* An alias template is complex from a SFINAE perspective if a template-id
5855 using that alias can be ill-formed when the expansion is not, as with
5856 the void_t template. We determine this by checking whether the
5857 expansion for the alias template uses all its template parameters. */
5859 struct uses_all_template_parms_data
5861 int level;
5862 bool *seen;
5865 static int
5866 uses_all_template_parms_r (tree t, void *data_)
5868 struct uses_all_template_parms_data &data
5869 = *(struct uses_all_template_parms_data*)data_;
5870 tree idx = get_template_parm_index (t);
5872 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5873 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5874 return 0;
5877 static bool
5878 complex_alias_template_p (const_tree tmpl)
5880 struct uses_all_template_parms_data data;
5881 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5882 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5883 data.level = TMPL_PARMS_DEPTH (parms);
5884 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5885 data.seen = XALLOCAVEC (bool, len);
5886 for (int i = 0; i < len; ++i)
5887 data.seen[i] = false;
5889 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5890 for (int i = 0; i < len; ++i)
5891 if (!data.seen[i])
5892 return true;
5893 return false;
5896 /* Return TRUE iff T is a specialization of a complex alias template with
5897 dependent template-arguments. */
5899 bool
5900 dependent_alias_template_spec_p (const_tree t)
5902 if (!alias_template_specialization_p (t))
5903 return false;
5905 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5906 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5907 return false;
5909 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5910 if (!any_dependent_template_arguments_p (args))
5911 return false;
5913 return true;
5916 /* Return the number of innermost template parameters in TMPL. */
5918 static int
5919 num_innermost_template_parms (tree tmpl)
5921 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5922 return TREE_VEC_LENGTH (parms);
5925 /* Return either TMPL or another template that it is equivalent to under DR
5926 1286: An alias that just changes the name of a template is equivalent to
5927 the other template. */
5929 static tree
5930 get_underlying_template (tree tmpl)
5932 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5933 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5935 /* Determine if the alias is equivalent to an underlying template. */
5936 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5937 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
5938 if (!tinfo)
5939 break;
5941 tree underlying = TI_TEMPLATE (tinfo);
5942 if (!PRIMARY_TEMPLATE_P (underlying)
5943 || (num_innermost_template_parms (tmpl)
5944 != num_innermost_template_parms (underlying)))
5945 break;
5947 tree alias_args = INNERMOST_TEMPLATE_ARGS
5948 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5949 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
5950 break;
5952 /* Alias is equivalent. Strip it and repeat. */
5953 tmpl = underlying;
5956 return tmpl;
5959 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5960 must be a reference-to-function or a pointer-to-function type, as specified
5961 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5962 and check that the resulting function has external linkage. */
5964 static tree
5965 convert_nontype_argument_function (tree type, tree expr,
5966 tsubst_flags_t complain)
5968 tree fns = expr;
5969 tree fn, fn_no_ptr;
5970 linkage_kind linkage;
5972 fn = instantiate_type (type, fns, tf_none);
5973 if (fn == error_mark_node)
5974 return error_mark_node;
5976 if (value_dependent_expression_p (fn))
5977 return fn;
5979 fn_no_ptr = strip_fnptr_conv (fn);
5980 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5981 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5982 if (BASELINK_P (fn_no_ptr))
5983 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5985 /* [temp.arg.nontype]/1
5987 A template-argument for a non-type, non-template template-parameter
5988 shall be one of:
5989 [...]
5990 -- the address of an object or function with external [C++11: or
5991 internal] linkage. */
5993 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5995 if (complain & tf_error)
5997 error ("%qE is not a valid template argument for type %qT",
5998 expr, type);
5999 if (TYPE_PTR_P (type))
6000 inform (input_location, "it must be the address of a function "
6001 "with external linkage");
6002 else
6003 inform (input_location, "it must be the name of a function with "
6004 "external linkage");
6006 return NULL_TREE;
6009 linkage = decl_linkage (fn_no_ptr);
6010 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6012 if (complain & tf_error)
6014 if (cxx_dialect >= cxx11)
6015 error ("%qE is not a valid template argument for type %qT "
6016 "because %qD has no linkage",
6017 expr, type, fn_no_ptr);
6018 else
6019 error ("%qE is not a valid template argument for type %qT "
6020 "because %qD does not have external linkage",
6021 expr, type, fn_no_ptr);
6023 return NULL_TREE;
6026 if (TREE_CODE (type) == REFERENCE_TYPE)
6027 fn = build_address (fn);
6028 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6029 fn = build_nop (type, fn);
6031 return fn;
6034 /* Subroutine of convert_nontype_argument.
6035 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6036 Emit an error otherwise. */
6038 static bool
6039 check_valid_ptrmem_cst_expr (tree type, tree expr,
6040 tsubst_flags_t complain)
6042 STRIP_NOPS (expr);
6043 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
6044 return true;
6045 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6046 return true;
6047 if (processing_template_decl
6048 && TREE_CODE (expr) == ADDR_EXPR
6049 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6050 return true;
6051 if (complain & tf_error)
6053 error ("%qE is not a valid template argument for type %qT",
6054 expr, type);
6055 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6057 return false;
6060 /* Returns TRUE iff the address of OP is value-dependent.
6062 14.6.2.4 [temp.dep.temp]:
6063 A non-integral non-type template-argument is dependent if its type is
6064 dependent or it has either of the following forms
6065 qualified-id
6066 & qualified-id
6067 and contains a nested-name-specifier which specifies a class-name that
6068 names a dependent type.
6070 We generalize this to just say that the address of a member of a
6071 dependent class is value-dependent; the above doesn't cover the
6072 address of a static data member named with an unqualified-id. */
6074 static bool
6075 has_value_dependent_address (tree op)
6077 /* We could use get_inner_reference here, but there's no need;
6078 this is only relevant for template non-type arguments, which
6079 can only be expressed as &id-expression. */
6080 if (DECL_P (op))
6082 tree ctx = CP_DECL_CONTEXT (op);
6083 if (TYPE_P (ctx) && dependent_type_p (ctx))
6084 return true;
6087 return false;
6090 /* The next set of functions are used for providing helpful explanatory
6091 diagnostics for failed overload resolution. Their messages should be
6092 indented by two spaces for consistency with the messages in
6093 call.c */
6095 static int
6096 unify_success (bool /*explain_p*/)
6098 return 0;
6101 static int
6102 unify_parameter_deduction_failure (bool explain_p, tree parm)
6104 if (explain_p)
6105 inform (input_location,
6106 " couldn't deduce template parameter %qD", parm);
6107 return 1;
6110 static int
6111 unify_invalid (bool /*explain_p*/)
6113 return 1;
6116 static int
6117 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6119 if (explain_p)
6120 inform (input_location,
6121 " types %qT and %qT have incompatible cv-qualifiers",
6122 parm, arg);
6123 return 1;
6126 static int
6127 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6129 if (explain_p)
6130 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6131 return 1;
6134 static int
6135 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6137 if (explain_p)
6138 inform (input_location,
6139 " template parameter %qD is not a parameter pack, but "
6140 "argument %qD is",
6141 parm, arg);
6142 return 1;
6145 static int
6146 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6148 if (explain_p)
6149 inform (input_location,
6150 " template argument %qE does not match "
6151 "pointer-to-member constant %qE",
6152 arg, parm);
6153 return 1;
6156 static int
6157 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6159 if (explain_p)
6160 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6161 return 1;
6164 static int
6165 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6167 if (explain_p)
6168 inform (input_location,
6169 " inconsistent parameter pack deduction with %qT and %qT",
6170 old_arg, new_arg);
6171 return 1;
6174 static int
6175 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6177 if (explain_p)
6179 if (TYPE_P (parm))
6180 inform (input_location,
6181 " deduced conflicting types for parameter %qT (%qT and %qT)",
6182 parm, first, second);
6183 else
6184 inform (input_location,
6185 " deduced conflicting values for non-type parameter "
6186 "%qE (%qE and %qE)", parm, first, second);
6188 return 1;
6191 static int
6192 unify_vla_arg (bool explain_p, tree arg)
6194 if (explain_p)
6195 inform (input_location,
6196 " variable-sized array type %qT is not "
6197 "a valid template argument",
6198 arg);
6199 return 1;
6202 static int
6203 unify_method_type_error (bool explain_p, tree arg)
6205 if (explain_p)
6206 inform (input_location,
6207 " member function type %qT is not a valid template argument",
6208 arg);
6209 return 1;
6212 static int
6213 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6215 if (explain_p)
6217 if (least_p)
6218 inform_n (input_location, wanted,
6219 " candidate expects at least %d argument, %d provided",
6220 " candidate expects at least %d arguments, %d provided",
6221 wanted, have);
6222 else
6223 inform_n (input_location, wanted,
6224 " candidate expects %d argument, %d provided",
6225 " candidate expects %d arguments, %d provided",
6226 wanted, have);
6228 return 1;
6231 static int
6232 unify_too_many_arguments (bool explain_p, int have, int wanted)
6234 return unify_arity (explain_p, have, wanted);
6237 static int
6238 unify_too_few_arguments (bool explain_p, int have, int wanted,
6239 bool least_p = false)
6241 return unify_arity (explain_p, have, wanted, least_p);
6244 static int
6245 unify_arg_conversion (bool explain_p, tree to_type,
6246 tree from_type, tree arg)
6248 if (explain_p)
6249 inform (EXPR_LOC_OR_LOC (arg, input_location),
6250 " cannot convert %qE (type %qT) to type %qT",
6251 arg, from_type, to_type);
6252 return 1;
6255 static int
6256 unify_no_common_base (bool explain_p, enum template_base_result r,
6257 tree parm, tree arg)
6259 if (explain_p)
6260 switch (r)
6262 case tbr_ambiguous_baseclass:
6263 inform (input_location, " %qT is an ambiguous base class of %qT",
6264 parm, arg);
6265 break;
6266 default:
6267 inform (input_location, " %qT is not derived from %qT", arg, parm);
6268 break;
6270 return 1;
6273 static int
6274 unify_inconsistent_template_template_parameters (bool explain_p)
6276 if (explain_p)
6277 inform (input_location,
6278 " template parameters of a template template argument are "
6279 "inconsistent with other deduced template arguments");
6280 return 1;
6283 static int
6284 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6286 if (explain_p)
6287 inform (input_location,
6288 " can't deduce a template for %qT from non-template type %qT",
6289 parm, arg);
6290 return 1;
6293 static int
6294 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6296 if (explain_p)
6297 inform (input_location,
6298 " template argument %qE does not match %qE", arg, parm);
6299 return 1;
6302 static int
6303 unify_overload_resolution_failure (bool explain_p, tree arg)
6305 if (explain_p)
6306 inform (input_location,
6307 " could not resolve address from overloaded function %qE",
6308 arg);
6309 return 1;
6312 /* Attempt to convert the non-type template parameter EXPR to the
6313 indicated TYPE. If the conversion is successful, return the
6314 converted value. If the conversion is unsuccessful, return
6315 NULL_TREE if we issued an error message, or error_mark_node if we
6316 did not. We issue error messages for out-and-out bad template
6317 parameters, but not simply because the conversion failed, since we
6318 might be just trying to do argument deduction. Both TYPE and EXPR
6319 must be non-dependent.
6321 The conversion follows the special rules described in
6322 [temp.arg.nontype], and it is much more strict than an implicit
6323 conversion.
6325 This function is called twice for each template argument (see
6326 lookup_template_class for a more accurate description of this
6327 problem). This means that we need to handle expressions which
6328 are not valid in a C++ source, but can be created from the
6329 first call (for instance, casts to perform conversions). These
6330 hacks can go away after we fix the double coercion problem. */
6332 static tree
6333 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6335 tree expr_type;
6337 /* Detect immediately string literals as invalid non-type argument.
6338 This special-case is not needed for correctness (we would easily
6339 catch this later), but only to provide better diagnostic for this
6340 common user mistake. As suggested by DR 100, we do not mention
6341 linkage issues in the diagnostic as this is not the point. */
6342 /* FIXME we're making this OK. */
6343 if (TREE_CODE (expr) == STRING_CST)
6345 if (complain & tf_error)
6346 error ("%qE is not a valid template argument for type %qT "
6347 "because string literals can never be used in this context",
6348 expr, type);
6349 return NULL_TREE;
6352 /* Add the ADDR_EXPR now for the benefit of
6353 value_dependent_expression_p. */
6354 if (TYPE_PTROBV_P (type)
6355 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6357 expr = decay_conversion (expr, complain);
6358 if (expr == error_mark_node)
6359 return error_mark_node;
6362 /* If we are in a template, EXPR may be non-dependent, but still
6363 have a syntactic, rather than semantic, form. For example, EXPR
6364 might be a SCOPE_REF, rather than the VAR_DECL to which the
6365 SCOPE_REF refers. Preserving the qualifying scope is necessary
6366 so that access checking can be performed when the template is
6367 instantiated -- but here we need the resolved form so that we can
6368 convert the argument. */
6369 bool non_dep = false;
6370 if (TYPE_REF_OBJ_P (type)
6371 && has_value_dependent_address (expr))
6372 /* If we want the address and it's value-dependent, don't fold. */;
6373 else if (processing_template_decl
6374 && potential_nondependent_constant_expression (expr))
6375 non_dep = true;
6376 if (error_operand_p (expr))
6377 return error_mark_node;
6378 expr_type = TREE_TYPE (expr);
6379 if (TREE_CODE (type) == REFERENCE_TYPE)
6380 expr = mark_lvalue_use (expr);
6381 else
6382 expr = mark_rvalue_use (expr);
6384 /* If the argument is non-dependent, perform any conversions in
6385 non-dependent context as well. */
6386 processing_template_decl_sentinel s (non_dep);
6387 if (non_dep)
6388 expr = instantiate_non_dependent_expr_internal (expr, complain);
6390 if (value_dependent_expression_p (expr))
6391 expr = canonicalize_expr_argument (expr, complain);
6393 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6394 to a non-type argument of "nullptr". */
6395 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6396 expr = fold_simple (convert (type, expr));
6398 /* In C++11, integral or enumeration non-type template arguments can be
6399 arbitrary constant expressions. Pointer and pointer to
6400 member arguments can be general constant expressions that evaluate
6401 to a null value, but otherwise still need to be of a specific form. */
6402 if (cxx_dialect >= cxx11)
6404 if (TREE_CODE (expr) == PTRMEM_CST)
6405 /* A PTRMEM_CST is already constant, and a valid template
6406 argument for a parameter of pointer to member type, we just want
6407 to leave it in that form rather than lower it to a
6408 CONSTRUCTOR. */;
6409 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6410 /* Constant value checking is done later with type conversion. */;
6411 else if (cxx_dialect >= cxx1z)
6413 if (TREE_CODE (type) != REFERENCE_TYPE)
6414 expr = maybe_constant_value (expr);
6415 else if (REFERENCE_REF_P (expr))
6417 expr = TREE_OPERAND (expr, 0);
6418 expr = maybe_constant_value (expr);
6419 expr = convert_from_reference (expr);
6422 else if (TYPE_PTR_OR_PTRMEM_P (type))
6424 tree folded = maybe_constant_value (expr);
6425 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6426 : null_member_pointer_value_p (folded))
6427 expr = folded;
6431 /* HACK: Due to double coercion, we can get a
6432 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6433 which is the tree that we built on the first call (see
6434 below when coercing to reference to object or to reference to
6435 function). We just strip everything and get to the arg.
6436 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6437 for examples. */
6438 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6440 tree probe_type, probe = expr;
6441 if (REFERENCE_REF_P (probe))
6442 probe = TREE_OPERAND (probe, 0);
6443 probe_type = TREE_TYPE (probe);
6444 if (TREE_CODE (probe) == NOP_EXPR)
6446 /* ??? Maybe we could use convert_from_reference here, but we
6447 would need to relax its constraints because the NOP_EXPR
6448 could actually change the type to something more cv-qualified,
6449 and this is not folded by convert_from_reference. */
6450 tree addr = TREE_OPERAND (probe, 0);
6451 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6452 && TREE_CODE (addr) == ADDR_EXPR
6453 && TYPE_PTR_P (TREE_TYPE (addr))
6454 && (same_type_ignoring_top_level_qualifiers_p
6455 (TREE_TYPE (probe_type),
6456 TREE_TYPE (TREE_TYPE (addr)))))
6458 expr = TREE_OPERAND (addr, 0);
6459 expr_type = TREE_TYPE (probe_type);
6464 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6465 parameter is a pointer to object, through decay and
6466 qualification conversion. Let's strip everything. */
6467 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6469 tree probe = expr;
6470 STRIP_NOPS (probe);
6471 if (TREE_CODE (probe) == ADDR_EXPR
6472 && TYPE_PTR_P (TREE_TYPE (probe)))
6474 /* Skip the ADDR_EXPR only if it is part of the decay for
6475 an array. Otherwise, it is part of the original argument
6476 in the source code. */
6477 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6478 probe = TREE_OPERAND (probe, 0);
6479 expr = probe;
6480 expr_type = TREE_TYPE (expr);
6484 /* [temp.arg.nontype]/5, bullet 1
6486 For a non-type template-parameter of integral or enumeration type,
6487 integral promotions (_conv.prom_) and integral conversions
6488 (_conv.integral_) are applied. */
6489 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6491 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6492 t = maybe_constant_value (t);
6493 if (t != error_mark_node)
6494 expr = t;
6496 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6497 return error_mark_node;
6499 /* Notice that there are constant expressions like '4 % 0' which
6500 do not fold into integer constants. */
6501 if (TREE_CODE (expr) != INTEGER_CST
6502 && !value_dependent_expression_p (expr))
6504 if (complain & tf_error)
6506 int errs = errorcount, warns = warningcount + werrorcount;
6507 if (processing_template_decl
6508 && !require_potential_constant_expression (expr))
6509 return NULL_TREE;
6510 expr = cxx_constant_value (expr);
6511 if (errorcount > errs || warningcount + werrorcount > warns)
6512 inform (EXPR_LOC_OR_LOC (expr, input_location),
6513 "in template argument for type %qT ", type);
6514 if (expr == error_mark_node)
6515 return NULL_TREE;
6516 /* else cxx_constant_value complained but gave us
6517 a real constant, so go ahead. */
6518 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6520 else
6521 return NULL_TREE;
6524 /* Avoid typedef problems. */
6525 if (TREE_TYPE (expr) != type)
6526 expr = fold_convert (type, expr);
6528 /* [temp.arg.nontype]/5, bullet 2
6530 For a non-type template-parameter of type pointer to object,
6531 qualification conversions (_conv.qual_) and the array-to-pointer
6532 conversion (_conv.array_) are applied. */
6533 else if (TYPE_PTROBV_P (type))
6535 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6537 A template-argument for a non-type, non-template template-parameter
6538 shall be one of: [...]
6540 -- the name of a non-type template-parameter;
6541 -- the address of an object or function with external linkage, [...]
6542 expressed as "& id-expression" where the & is optional if the name
6543 refers to a function or array, or if the corresponding
6544 template-parameter is a reference.
6546 Here, we do not care about functions, as they are invalid anyway
6547 for a parameter of type pointer-to-object. */
6549 if (value_dependent_expression_p (expr))
6550 /* Non-type template parameters are OK. */
6552 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6553 /* Null pointer values are OK in C++11. */;
6554 else if (TREE_CODE (expr) != ADDR_EXPR
6555 && TREE_CODE (expr_type) != ARRAY_TYPE)
6557 if (VAR_P (expr))
6559 if (complain & tf_error)
6560 error ("%qD is not a valid template argument "
6561 "because %qD is a variable, not the address of "
6562 "a variable", expr, expr);
6563 return NULL_TREE;
6565 if (POINTER_TYPE_P (expr_type))
6567 if (complain & tf_error)
6568 error ("%qE is not a valid template argument for %qT "
6569 "because it is not the address of a variable",
6570 expr, type);
6571 return NULL_TREE;
6573 /* Other values, like integer constants, might be valid
6574 non-type arguments of some other type. */
6575 return error_mark_node;
6577 else
6579 tree decl;
6581 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6582 ? TREE_OPERAND (expr, 0) : expr);
6583 if (!VAR_P (decl))
6585 if (complain & tf_error)
6586 error ("%qE is not a valid template argument of type %qT "
6587 "because %qE is not a variable", expr, type, decl);
6588 return NULL_TREE;
6590 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6592 if (complain & tf_error)
6593 error ("%qE is not a valid template argument of type %qT "
6594 "because %qD does not have external linkage",
6595 expr, type, decl);
6596 return NULL_TREE;
6598 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6600 if (complain & tf_error)
6601 error ("%qE is not a valid template argument of type %qT "
6602 "because %qD has no linkage", expr, type, decl);
6603 return NULL_TREE;
6607 expr = decay_conversion (expr, complain);
6608 if (expr == error_mark_node)
6609 return error_mark_node;
6611 expr = perform_qualification_conversions (type, expr);
6612 if (expr == error_mark_node)
6613 return error_mark_node;
6615 /* [temp.arg.nontype]/5, bullet 3
6617 For a non-type template-parameter of type reference to object, no
6618 conversions apply. The type referred to by the reference may be more
6619 cv-qualified than the (otherwise identical) type of the
6620 template-argument. The template-parameter is bound directly to the
6621 template-argument, which must be an lvalue. */
6622 else if (TYPE_REF_OBJ_P (type))
6624 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6625 expr_type))
6626 return error_mark_node;
6628 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6630 if (complain & tf_error)
6631 error ("%qE is not a valid template argument for type %qT "
6632 "because of conflicts in cv-qualification", expr, type);
6633 return NULL_TREE;
6636 if (!lvalue_p (expr))
6638 if (complain & tf_error)
6639 error ("%qE is not a valid template argument for type %qT "
6640 "because it is not an lvalue", expr, type);
6641 return NULL_TREE;
6644 /* [temp.arg.nontype]/1
6646 A template-argument for a non-type, non-template template-parameter
6647 shall be one of: [...]
6649 -- the address of an object or function with external linkage. */
6650 if (INDIRECT_REF_P (expr)
6651 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6653 expr = TREE_OPERAND (expr, 0);
6654 if (DECL_P (expr))
6656 if (complain & tf_error)
6657 error ("%q#D is not a valid template argument for type %qT "
6658 "because a reference variable does not have a constant "
6659 "address", expr, type);
6660 return NULL_TREE;
6664 if (!value_dependent_expression_p (expr))
6666 if (!DECL_P (expr))
6668 if (complain & tf_error)
6669 error ("%qE is not a valid template argument for type %qT "
6670 "because it is not an object with linkage",
6671 expr, type);
6672 return NULL_TREE;
6675 /* DR 1155 allows internal linkage in C++11 and up. */
6676 linkage_kind linkage = decl_linkage (expr);
6677 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6679 if (complain & tf_error)
6680 error ("%qE is not a valid template argument for type %qT "
6681 "because object %qD does not have linkage",
6682 expr, type, expr);
6683 return NULL_TREE;
6686 expr = build_nop (type, build_address (expr));
6689 /* [temp.arg.nontype]/5, bullet 4
6691 For a non-type template-parameter of type pointer to function, only
6692 the function-to-pointer conversion (_conv.func_) is applied. If the
6693 template-argument represents a set of overloaded functions (or a
6694 pointer to such), the matching function is selected from the set
6695 (_over.over_). */
6696 else if (TYPE_PTRFN_P (type))
6698 /* If the argument is a template-id, we might not have enough
6699 context information to decay the pointer. */
6700 if (!type_unknown_p (expr_type))
6702 expr = decay_conversion (expr, complain);
6703 if (expr == error_mark_node)
6704 return error_mark_node;
6707 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6708 /* Null pointer values are OK in C++11. */
6709 return perform_qualification_conversions (type, expr);
6711 expr = convert_nontype_argument_function (type, expr, complain);
6712 if (!expr || expr == error_mark_node)
6713 return expr;
6715 /* [temp.arg.nontype]/5, bullet 5
6717 For a non-type template-parameter of type reference to function, no
6718 conversions apply. If the template-argument represents a set of
6719 overloaded functions, the matching function is selected from the set
6720 (_over.over_). */
6721 else if (TYPE_REFFN_P (type))
6723 if (TREE_CODE (expr) == ADDR_EXPR)
6725 if (complain & tf_error)
6727 error ("%qE is not a valid template argument for type %qT "
6728 "because it is a pointer", expr, type);
6729 inform (input_location, "try using %qE instead",
6730 TREE_OPERAND (expr, 0));
6732 return NULL_TREE;
6735 expr = convert_nontype_argument_function (type, expr, complain);
6736 if (!expr || expr == error_mark_node)
6737 return expr;
6739 /* [temp.arg.nontype]/5, bullet 6
6741 For a non-type template-parameter of type pointer to member function,
6742 no conversions apply. If the template-argument represents a set of
6743 overloaded member functions, the matching member function is selected
6744 from the set (_over.over_). */
6745 else if (TYPE_PTRMEMFUNC_P (type))
6747 expr = instantiate_type (type, expr, tf_none);
6748 if (expr == error_mark_node)
6749 return error_mark_node;
6751 /* [temp.arg.nontype] bullet 1 says the pointer to member
6752 expression must be a pointer-to-member constant. */
6753 if (!value_dependent_expression_p (expr)
6754 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6755 return error_mark_node;
6757 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6758 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6759 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6760 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6762 /* There is no way to disable standard conversions in
6763 resolve_address_of_overloaded_function (called by
6764 instantiate_type). It is possible that the call succeeded by
6765 converting &B::I to &D::I (where B is a base of D), so we need
6766 to reject this conversion here.
6768 Actually, even if there was a way to disable standard conversions,
6769 it would still be better to reject them here so that we can
6770 provide a superior diagnostic. */
6771 if (!same_type_p (TREE_TYPE (expr), type))
6773 if (complain & tf_error)
6775 error ("%qE is not a valid template argument for type %qT "
6776 "because it is of type %qT", expr, type,
6777 TREE_TYPE (expr));
6778 /* If we are just one standard conversion off, explain. */
6779 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6780 inform (input_location,
6781 "standard conversions are not allowed in this context");
6783 return NULL_TREE;
6786 /* [temp.arg.nontype]/5, bullet 7
6788 For a non-type template-parameter of type pointer to data member,
6789 qualification conversions (_conv.qual_) are applied. */
6790 else if (TYPE_PTRDATAMEM_P (type))
6792 /* [temp.arg.nontype] bullet 1 says the pointer to member
6793 expression must be a pointer-to-member constant. */
6794 if (!value_dependent_expression_p (expr)
6795 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6796 return error_mark_node;
6798 expr = perform_qualification_conversions (type, expr);
6799 if (expr == error_mark_node)
6800 return expr;
6802 else if (NULLPTR_TYPE_P (type))
6804 if (expr != nullptr_node)
6806 if (complain & tf_error)
6807 error ("%qE is not a valid template argument for type %qT "
6808 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6809 return NULL_TREE;
6811 return expr;
6813 /* A template non-type parameter must be one of the above. */
6814 else
6815 gcc_unreachable ();
6817 /* Sanity check: did we actually convert the argument to the
6818 right type? */
6819 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6820 (type, TREE_TYPE (expr)));
6821 return convert_from_reference (expr);
6824 /* Subroutine of coerce_template_template_parms, which returns 1 if
6825 PARM_PARM and ARG_PARM match using the rule for the template
6826 parameters of template template parameters. Both PARM and ARG are
6827 template parameters; the rest of the arguments are the same as for
6828 coerce_template_template_parms.
6830 static int
6831 coerce_template_template_parm (tree parm,
6832 tree arg,
6833 tsubst_flags_t complain,
6834 tree in_decl,
6835 tree outer_args)
6837 if (arg == NULL_TREE || error_operand_p (arg)
6838 || parm == NULL_TREE || error_operand_p (parm))
6839 return 0;
6841 if (TREE_CODE (arg) != TREE_CODE (parm))
6842 return 0;
6844 switch (TREE_CODE (parm))
6846 case TEMPLATE_DECL:
6847 /* We encounter instantiations of templates like
6848 template <template <template <class> class> class TT>
6849 class C; */
6851 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6852 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6854 if (!coerce_template_template_parms
6855 (parmparm, argparm, complain, in_decl, outer_args))
6856 return 0;
6858 /* Fall through. */
6860 case TYPE_DECL:
6861 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6862 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6863 /* Argument is a parameter pack but parameter is not. */
6864 return 0;
6865 break;
6867 case PARM_DECL:
6868 /* The tsubst call is used to handle cases such as
6870 template <int> class C {};
6871 template <class T, template <T> class TT> class D {};
6872 D<int, C> d;
6874 i.e. the parameter list of TT depends on earlier parameters. */
6875 if (!uses_template_parms (TREE_TYPE (arg)))
6877 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6878 if (!uses_template_parms (t)
6879 && !same_type_p (t, TREE_TYPE (arg)))
6880 return 0;
6883 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6884 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6885 /* Argument is a parameter pack but parameter is not. */
6886 return 0;
6888 break;
6890 default:
6891 gcc_unreachable ();
6894 return 1;
6897 /* Coerce template argument list ARGLIST for use with template
6898 template-parameter TEMPL. */
6900 static tree
6901 coerce_template_args_for_ttp (tree templ, tree arglist,
6902 tsubst_flags_t complain)
6904 /* Consider an example where a template template parameter declared as
6906 template <class T, class U = std::allocator<T> > class TT
6908 The template parameter level of T and U are one level larger than
6909 of TT. To proper process the default argument of U, say when an
6910 instantiation `TT<int>' is seen, we need to build the full
6911 arguments containing {int} as the innermost level. Outer levels,
6912 available when not appearing as default template argument, can be
6913 obtained from the arguments of the enclosing template.
6915 Suppose that TT is later substituted with std::vector. The above
6916 instantiation is `TT<int, std::allocator<T> >' with TT at
6917 level 1, and T at level 2, while the template arguments at level 1
6918 becomes {std::vector} and the inner level 2 is {int}. */
6920 tree outer = DECL_CONTEXT (templ);
6921 if (outer)
6923 if (DECL_TEMPLATE_SPECIALIZATION (outer))
6924 /* We want arguments for the partial specialization, not arguments for
6925 the primary template. */
6926 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
6927 else
6928 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6930 else if (current_template_parms)
6932 /* This is an argument of the current template, so we haven't set
6933 DECL_CONTEXT yet. */
6934 tree relevant_template_parms;
6936 /* Parameter levels that are greater than the level of the given
6937 template template parm are irrelevant. */
6938 relevant_template_parms = current_template_parms;
6939 while (TMPL_PARMS_DEPTH (relevant_template_parms)
6940 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
6941 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
6943 outer = template_parms_to_args (relevant_template_parms);
6946 if (outer)
6947 arglist = add_to_template_args (outer, arglist);
6949 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6950 return coerce_template_parms (parmlist, arglist, templ,
6951 complain,
6952 /*require_all_args=*/true,
6953 /*use_default_args=*/true);
6956 /* A cache of template template parameters with match-all default
6957 arguments. */
6958 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
6959 static void
6960 store_defaulted_ttp (tree v, tree t)
6962 if (!defaulted_ttp_cache)
6963 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
6964 defaulted_ttp_cache->put (v, t);
6966 static tree
6967 lookup_defaulted_ttp (tree v)
6969 if (defaulted_ttp_cache)
6970 if (tree *p = defaulted_ttp_cache->get (v))
6971 return *p;
6972 return NULL_TREE;
6975 /* T is a bound template template-parameter. Copy its arguments into default
6976 arguments of the template template-parameter's template parameters. */
6978 static tree
6979 add_defaults_to_ttp (tree otmpl)
6981 if (tree c = lookup_defaulted_ttp (otmpl))
6982 return c;
6984 tree ntmpl = copy_node (otmpl);
6986 tree ntype = copy_node (TREE_TYPE (otmpl));
6987 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
6988 TYPE_MAIN_VARIANT (ntype) = ntype;
6989 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
6990 TYPE_NAME (ntype) = ntmpl;
6991 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
6993 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
6994 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
6995 TEMPLATE_PARM_DECL (idx) = ntmpl;
6996 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
6998 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
6999 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7000 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7001 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7002 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7004 tree o = TREE_VEC_ELT (vec, i);
7005 if (!template_parameter_pack_p (TREE_VALUE (o)))
7007 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7008 TREE_PURPOSE (n) = any_targ_node;
7012 store_defaulted_ttp (otmpl, ntmpl);
7013 return ntmpl;
7016 /* ARG is a bound potential template template-argument, and PARGS is a list
7017 of arguments for the corresponding template template-parameter. Adjust
7018 PARGS as appropriate for application to ARG's template, and if ARG is a
7019 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7020 arguments to the template template parameter. */
7022 static tree
7023 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7025 ++processing_template_decl;
7026 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7027 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7029 /* When comparing two template template-parameters in partial ordering,
7030 rewrite the one currently being used as an argument to have default
7031 arguments for all parameters. */
7032 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7033 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7034 if (pargs != error_mark_node)
7035 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7036 TYPE_TI_ARGS (arg));
7038 else
7040 tree aparms
7041 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7042 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7043 /*require_all*/true,
7044 /*use_default*/true);
7046 --processing_template_decl;
7047 return pargs;
7050 /* Subroutine of unify for the case when PARM is a
7051 BOUND_TEMPLATE_TEMPLATE_PARM. */
7053 static int
7054 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7055 bool explain_p)
7057 tree parmvec = TYPE_TI_ARGS (parm);
7058 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7060 /* The template template parm might be variadic and the argument
7061 not, so flatten both argument lists. */
7062 parmvec = expand_template_argument_pack (parmvec);
7063 argvec = expand_template_argument_pack (argvec);
7065 tree nparmvec = parmvec;
7066 if (flag_new_ttp)
7068 /* In keeping with P0522R0, adjust P's template arguments
7069 to apply to A's template; then flatten it again. */
7070 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7071 nparmvec = expand_template_argument_pack (nparmvec);
7074 if (unify (tparms, targs, nparmvec, argvec,
7075 UNIFY_ALLOW_NONE, explain_p))
7076 return 1;
7078 /* If the P0522 adjustment eliminated a pack expansion, deduce
7079 empty packs. */
7080 if (flag_new_ttp
7081 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7082 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7083 DEDUCE_EXACT, /*sub*/true, explain_p))
7084 return 1;
7086 return 0;
7089 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7090 template template parameters. Both PARM_PARMS and ARG_PARMS are
7091 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7092 or PARM_DECL.
7094 Consider the example:
7095 template <class T> class A;
7096 template<template <class U> class TT> class B;
7098 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7099 the parameters to A, and OUTER_ARGS contains A. */
7101 static int
7102 coerce_template_template_parms (tree parm_parms,
7103 tree arg_parms,
7104 tsubst_flags_t complain,
7105 tree in_decl,
7106 tree outer_args)
7108 int nparms, nargs, i;
7109 tree parm, arg;
7110 int variadic_p = 0;
7112 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7113 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7115 nparms = TREE_VEC_LENGTH (parm_parms);
7116 nargs = TREE_VEC_LENGTH (arg_parms);
7118 if (flag_new_ttp)
7120 /* P0522R0: A template template-parameter P is at least as specialized as
7121 a template template-argument A if, given the following rewrite to two
7122 function templates, the function template corresponding to P is at
7123 least as specialized as the function template corresponding to A
7124 according to the partial ordering rules for function templates
7125 ([temp.func.order]). Given an invented class template X with the
7126 template parameter list of A (including default arguments):
7128 * Each of the two function templates has the same template parameters,
7129 respectively, as P or A.
7131 * Each function template has a single function parameter whose type is
7132 a specialization of X with template arguments corresponding to the
7133 template parameters from the respective function template where, for
7134 each template parameter PP in the template parameter list of the
7135 function template, a corresponding template argument AA is formed. If
7136 PP declares a parameter pack, then AA is the pack expansion
7137 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7139 If the rewrite produces an invalid type, then P is not at least as
7140 specialized as A. */
7142 /* So coerce P's args to apply to A's parms, and then deduce between A's
7143 args and the converted args. If that succeeds, A is at least as
7144 specialized as P, so they match.*/
7145 tree pargs = template_parms_level_to_args (parm_parms);
7146 ++processing_template_decl;
7147 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7148 /*require_all*/true, /*use_default*/true);
7149 --processing_template_decl;
7150 if (pargs != error_mark_node)
7152 tree targs = make_tree_vec (nargs);
7153 tree aargs = template_parms_level_to_args (arg_parms);
7154 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7155 /*explain*/false))
7156 return 1;
7160 /* Determine whether we have a parameter pack at the end of the
7161 template template parameter's template parameter list. */
7162 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7164 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7166 if (error_operand_p (parm))
7167 return 0;
7169 switch (TREE_CODE (parm))
7171 case TEMPLATE_DECL:
7172 case TYPE_DECL:
7173 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7174 variadic_p = 1;
7175 break;
7177 case PARM_DECL:
7178 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7179 variadic_p = 1;
7180 break;
7182 default:
7183 gcc_unreachable ();
7187 if (nargs != nparms
7188 && !(variadic_p && nargs >= nparms - 1))
7189 return 0;
7191 /* Check all of the template parameters except the parameter pack at
7192 the end (if any). */
7193 for (i = 0; i < nparms - variadic_p; ++i)
7195 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7196 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7197 continue;
7199 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7200 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7202 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7203 outer_args))
7204 return 0;
7208 if (variadic_p)
7210 /* Check each of the template parameters in the template
7211 argument against the template parameter pack at the end of
7212 the template template parameter. */
7213 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7214 return 0;
7216 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7218 for (; i < nargs; ++i)
7220 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7221 continue;
7223 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7225 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7226 outer_args))
7227 return 0;
7231 return 1;
7234 /* Verifies that the deduced template arguments (in TARGS) for the
7235 template template parameters (in TPARMS) represent valid bindings,
7236 by comparing the template parameter list of each template argument
7237 to the template parameter list of its corresponding template
7238 template parameter, in accordance with DR150. This
7239 routine can only be called after all template arguments have been
7240 deduced. It will return TRUE if all of the template template
7241 parameter bindings are okay, FALSE otherwise. */
7242 bool
7243 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7245 int i, ntparms = TREE_VEC_LENGTH (tparms);
7246 bool ret = true;
7248 /* We're dealing with template parms in this process. */
7249 ++processing_template_decl;
7251 targs = INNERMOST_TEMPLATE_ARGS (targs);
7253 for (i = 0; i < ntparms; ++i)
7255 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7256 tree targ = TREE_VEC_ELT (targs, i);
7258 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7260 tree packed_args = NULL_TREE;
7261 int idx, len = 1;
7263 if (ARGUMENT_PACK_P (targ))
7265 /* Look inside the argument pack. */
7266 packed_args = ARGUMENT_PACK_ARGS (targ);
7267 len = TREE_VEC_LENGTH (packed_args);
7270 for (idx = 0; idx < len; ++idx)
7272 tree targ_parms = NULL_TREE;
7274 if (packed_args)
7275 /* Extract the next argument from the argument
7276 pack. */
7277 targ = TREE_VEC_ELT (packed_args, idx);
7279 if (PACK_EXPANSION_P (targ))
7280 /* Look at the pattern of the pack expansion. */
7281 targ = PACK_EXPANSION_PATTERN (targ);
7283 /* Extract the template parameters from the template
7284 argument. */
7285 if (TREE_CODE (targ) == TEMPLATE_DECL)
7286 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7287 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7288 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7290 /* Verify that we can coerce the template template
7291 parameters from the template argument to the template
7292 parameter. This requires an exact match. */
7293 if (targ_parms
7294 && !coerce_template_template_parms
7295 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7296 targ_parms,
7297 tf_none,
7298 tparm,
7299 targs))
7301 ret = false;
7302 goto out;
7308 out:
7310 --processing_template_decl;
7311 return ret;
7314 /* Since type attributes aren't mangled, we need to strip them from
7315 template type arguments. */
7317 static tree
7318 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7320 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7321 return arg;
7322 bool removed_attributes = false;
7323 tree canon = strip_typedefs (arg, &removed_attributes);
7324 if (removed_attributes
7325 && (complain & tf_warning))
7326 warning (OPT_Wignored_attributes,
7327 "ignoring attributes on template argument %qT", arg);
7328 return canon;
7331 /* And from inside dependent non-type arguments like sizeof(Type). */
7333 static tree
7334 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7336 if (!arg || arg == error_mark_node)
7337 return arg;
7338 bool removed_attributes = false;
7339 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7340 if (removed_attributes
7341 && (complain & tf_warning))
7342 warning (OPT_Wignored_attributes,
7343 "ignoring attributes in template argument %qE", arg);
7344 return canon;
7347 // A template declaration can be substituted for a constrained
7348 // template template parameter only when the argument is more
7349 // constrained than the parameter.
7350 static bool
7351 is_compatible_template_arg (tree parm, tree arg)
7353 tree parm_cons = get_constraints (parm);
7355 /* For now, allow constrained template template arguments
7356 and unconstrained template template parameters. */
7357 if (parm_cons == NULL_TREE)
7358 return true;
7360 tree arg_cons = get_constraints (arg);
7362 // If the template parameter is constrained, we need to rewrite its
7363 // constraints in terms of the ARG's template parameters. This ensures
7364 // that all of the template parameter types will have the same depth.
7366 // Note that this is only valid when coerce_template_template_parm is
7367 // true for the innermost template parameters of PARM and ARG. In other
7368 // words, because coercion is successful, this conversion will be valid.
7369 if (parm_cons)
7371 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7372 parm_cons = tsubst_constraint_info (parm_cons,
7373 INNERMOST_TEMPLATE_ARGS (args),
7374 tf_none, NULL_TREE);
7375 if (parm_cons == error_mark_node)
7376 return false;
7379 return subsumes (parm_cons, arg_cons);
7382 // Convert a placeholder argument into a binding to the original
7383 // parameter. The original parameter is saved as the TREE_TYPE of
7384 // ARG.
7385 static inline tree
7386 convert_wildcard_argument (tree parm, tree arg)
7388 TREE_TYPE (arg) = parm;
7389 return arg;
7392 /* Convert the indicated template ARG as necessary to match the
7393 indicated template PARM. Returns the converted ARG, or
7394 error_mark_node if the conversion was unsuccessful. Error and
7395 warning messages are issued under control of COMPLAIN. This
7396 conversion is for the Ith parameter in the parameter list. ARGS is
7397 the full set of template arguments deduced so far. */
7399 static tree
7400 convert_template_argument (tree parm,
7401 tree arg,
7402 tree args,
7403 tsubst_flags_t complain,
7404 int i,
7405 tree in_decl)
7407 tree orig_arg;
7408 tree val;
7409 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7411 if (parm == error_mark_node)
7412 return error_mark_node;
7414 /* Trivially convert placeholders. */
7415 if (TREE_CODE (arg) == WILDCARD_DECL)
7416 return convert_wildcard_argument (parm, arg);
7418 if (arg == any_targ_node)
7419 return arg;
7421 if (TREE_CODE (arg) == TREE_LIST
7422 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7424 /* The template argument was the name of some
7425 member function. That's usually
7426 invalid, but static members are OK. In any
7427 case, grab the underlying fields/functions
7428 and issue an error later if required. */
7429 orig_arg = TREE_VALUE (arg);
7430 TREE_TYPE (arg) = unknown_type_node;
7433 orig_arg = arg;
7435 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7436 requires_type = (TREE_CODE (parm) == TYPE_DECL
7437 || requires_tmpl_type);
7439 /* When determining whether an argument pack expansion is a template,
7440 look at the pattern. */
7441 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7442 arg = PACK_EXPANSION_PATTERN (arg);
7444 /* Deal with an injected-class-name used as a template template arg. */
7445 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7447 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7448 if (TREE_CODE (t) == TEMPLATE_DECL)
7450 if (cxx_dialect >= cxx11)
7451 /* OK under DR 1004. */;
7452 else if (complain & tf_warning_or_error)
7453 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7454 " used as template template argument", TYPE_NAME (arg));
7455 else if (flag_pedantic_errors)
7456 t = arg;
7458 arg = t;
7462 is_tmpl_type =
7463 ((TREE_CODE (arg) == TEMPLATE_DECL
7464 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7465 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7466 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7467 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7469 if (is_tmpl_type
7470 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7471 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7472 arg = TYPE_STUB_DECL (arg);
7474 is_type = TYPE_P (arg) || is_tmpl_type;
7476 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7477 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7479 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7481 if (complain & tf_error)
7482 error ("invalid use of destructor %qE as a type", orig_arg);
7483 return error_mark_node;
7486 permerror (input_location,
7487 "to refer to a type member of a template parameter, "
7488 "use %<typename %E%>", orig_arg);
7490 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7491 TREE_OPERAND (arg, 1),
7492 typename_type,
7493 complain);
7494 arg = orig_arg;
7495 is_type = 1;
7497 if (is_type != requires_type)
7499 if (in_decl)
7501 if (complain & tf_error)
7503 error ("type/value mismatch at argument %d in template "
7504 "parameter list for %qD",
7505 i + 1, in_decl);
7506 if (is_type)
7507 inform (input_location,
7508 " expected a constant of type %qT, got %qT",
7509 TREE_TYPE (parm),
7510 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7511 else if (requires_tmpl_type)
7512 inform (input_location,
7513 " expected a class template, got %qE", orig_arg);
7514 else
7515 inform (input_location,
7516 " expected a type, got %qE", orig_arg);
7519 return error_mark_node;
7521 if (is_tmpl_type ^ requires_tmpl_type)
7523 if (in_decl && (complain & tf_error))
7525 error ("type/value mismatch at argument %d in template "
7526 "parameter list for %qD",
7527 i + 1, in_decl);
7528 if (is_tmpl_type)
7529 inform (input_location,
7530 " expected a type, got %qT", DECL_NAME (arg));
7531 else
7532 inform (input_location,
7533 " expected a class template, got %qT", orig_arg);
7535 return error_mark_node;
7538 if (is_type)
7540 if (requires_tmpl_type)
7542 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7543 val = orig_arg;
7544 else if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7545 /* The number of argument required is not known yet.
7546 Just accept it for now. */
7547 val = TREE_TYPE (arg);
7548 else
7550 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7551 tree argparm;
7553 /* Strip alias templates that are equivalent to another
7554 template. */
7555 arg = get_underlying_template (arg);
7556 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7558 if (coerce_template_template_parms (parmparm, argparm,
7559 complain, in_decl,
7560 args))
7562 val = arg;
7564 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7565 TEMPLATE_DECL. */
7566 if (val != error_mark_node)
7568 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7569 val = TREE_TYPE (val);
7570 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7571 val = make_pack_expansion (val);
7574 else
7576 if (in_decl && (complain & tf_error))
7578 error ("type/value mismatch at argument %d in "
7579 "template parameter list for %qD",
7580 i + 1, in_decl);
7581 inform (input_location,
7582 " expected a template of type %qD, got %qT",
7583 parm, orig_arg);
7586 val = error_mark_node;
7589 // Check that the constraints are compatible before allowing the
7590 // substitution.
7591 if (val != error_mark_node)
7592 if (!is_compatible_template_arg (parm, arg))
7594 if (in_decl && (complain & tf_error))
7596 error ("constraint mismatch at argument %d in "
7597 "template parameter list for %qD",
7598 i + 1, in_decl);
7599 inform (input_location, " expected %qD but got %qD",
7600 parm, arg);
7602 val = error_mark_node;
7606 else
7607 val = orig_arg;
7608 /* We only form one instance of each template specialization.
7609 Therefore, if we use a non-canonical variant (i.e., a
7610 typedef), any future messages referring to the type will use
7611 the typedef, which is confusing if those future uses do not
7612 themselves also use the typedef. */
7613 if (TYPE_P (val))
7614 val = canonicalize_type_argument (val, complain);
7616 else
7618 tree t = TREE_TYPE (parm);
7620 if (tree a = type_uses_auto (t))
7622 if (ARGUMENT_PACK_P (orig_arg))
7623 /* There's nothing to check for an auto argument pack. */
7624 return orig_arg;
7626 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7627 if (t == error_mark_node)
7628 return error_mark_node;
7630 else
7631 t = tsubst (t, args, complain, in_decl);
7633 if (invalid_nontype_parm_type_p (t, complain))
7634 return error_mark_node;
7636 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7638 if (same_type_p (t, TREE_TYPE (orig_arg)))
7639 val = orig_arg;
7640 else
7642 /* Not sure if this is reachable, but it doesn't hurt
7643 to be robust. */
7644 error ("type mismatch in nontype parameter pack");
7645 val = error_mark_node;
7648 else if (!type_dependent_expression_p (orig_arg)
7649 && !uses_template_parms (t))
7650 /* We used to call digest_init here. However, digest_init
7651 will report errors, which we don't want when complain
7652 is zero. More importantly, digest_init will try too
7653 hard to convert things: for example, `0' should not be
7654 converted to pointer type at this point according to
7655 the standard. Accepting this is not merely an
7656 extension, since deciding whether or not these
7657 conversions can occur is part of determining which
7658 function template to call, or whether a given explicit
7659 argument specification is valid. */
7660 val = convert_nontype_argument (t, orig_arg, complain);
7661 else
7662 val = canonicalize_expr_argument (orig_arg, complain);
7664 if (val == NULL_TREE)
7665 val = error_mark_node;
7666 else if (val == error_mark_node && (complain & tf_error))
7667 error ("could not convert template argument %qE from %qT to %qT",
7668 orig_arg, TREE_TYPE (orig_arg), t);
7670 if (INDIRECT_REF_P (val))
7672 /* Reject template arguments that are references to built-in
7673 functions with no library fallbacks. */
7674 const_tree inner = TREE_OPERAND (val, 0);
7675 const_tree innertype = TREE_TYPE (inner);
7676 if (innertype
7677 && TREE_CODE (innertype) == REFERENCE_TYPE
7678 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7679 && 0 < TREE_OPERAND_LENGTH (inner)
7680 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7681 return error_mark_node;
7684 if (TREE_CODE (val) == SCOPE_REF)
7686 /* Strip typedefs from the SCOPE_REF. */
7687 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7688 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7689 complain);
7690 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7691 QUALIFIED_NAME_IS_TEMPLATE (val));
7695 return val;
7698 /* Coerces the remaining template arguments in INNER_ARGS (from
7699 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7700 Returns the coerced argument pack. PARM_IDX is the position of this
7701 parameter in the template parameter list. ARGS is the original
7702 template argument list. */
7703 static tree
7704 coerce_template_parameter_pack (tree parms,
7705 int parm_idx,
7706 tree args,
7707 tree inner_args,
7708 int arg_idx,
7709 tree new_args,
7710 int* lost,
7711 tree in_decl,
7712 tsubst_flags_t complain)
7714 tree parm = TREE_VEC_ELT (parms, parm_idx);
7715 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7716 tree packed_args;
7717 tree argument_pack;
7718 tree packed_parms = NULL_TREE;
7720 if (arg_idx > nargs)
7721 arg_idx = nargs;
7723 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7725 /* When the template parameter is a non-type template parameter pack
7726 or template template parameter pack whose type or template
7727 parameters use parameter packs, we know exactly how many arguments
7728 we are looking for. Build a vector of the instantiated decls for
7729 these template parameters in PACKED_PARMS. */
7730 /* We can't use make_pack_expansion here because it would interpret a
7731 _DECL as a use rather than a declaration. */
7732 tree decl = TREE_VALUE (parm);
7733 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7734 SET_PACK_EXPANSION_PATTERN (exp, decl);
7735 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7736 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7738 TREE_VEC_LENGTH (args)--;
7739 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7740 TREE_VEC_LENGTH (args)++;
7742 if (packed_parms == error_mark_node)
7743 return error_mark_node;
7745 /* If we're doing a partial instantiation of a member template,
7746 verify that all of the types used for the non-type
7747 template parameter pack are, in fact, valid for non-type
7748 template parameters. */
7749 if (arg_idx < nargs
7750 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7752 int j, len = TREE_VEC_LENGTH (packed_parms);
7753 for (j = 0; j < len; ++j)
7755 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7756 if (invalid_nontype_parm_type_p (t, complain))
7757 return error_mark_node;
7759 /* We don't know how many args we have yet, just
7760 use the unconverted ones for now. */
7761 return NULL_TREE;
7764 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7766 /* Check if we have a placeholder pack, which indicates we're
7767 in the context of a introduction list. In that case we want
7768 to match this pack to the single placeholder. */
7769 else if (arg_idx < nargs
7770 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7771 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7773 nargs = arg_idx + 1;
7774 packed_args = make_tree_vec (1);
7776 else
7777 packed_args = make_tree_vec (nargs - arg_idx);
7779 /* Convert the remaining arguments, which will be a part of the
7780 parameter pack "parm". */
7781 int first_pack_arg = arg_idx;
7782 for (; arg_idx < nargs; ++arg_idx)
7784 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7785 tree actual_parm = TREE_VALUE (parm);
7786 int pack_idx = arg_idx - first_pack_arg;
7788 if (packed_parms)
7790 /* Once we've packed as many args as we have types, stop. */
7791 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7792 break;
7793 else if (PACK_EXPANSION_P (arg))
7794 /* We don't know how many args we have yet, just
7795 use the unconverted ones for now. */
7796 return NULL_TREE;
7797 else
7798 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7801 if (arg == error_mark_node)
7803 if (complain & tf_error)
7804 error ("template argument %d is invalid", arg_idx + 1);
7806 else
7807 arg = convert_template_argument (actual_parm,
7808 arg, new_args, complain, parm_idx,
7809 in_decl);
7810 if (arg == error_mark_node)
7811 (*lost)++;
7812 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7815 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7816 && TREE_VEC_LENGTH (packed_args) > 0)
7818 if (complain & tf_error)
7819 error ("wrong number of template arguments (%d, should be %d)",
7820 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7821 return error_mark_node;
7824 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7825 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7826 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7827 else
7829 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7830 TREE_TYPE (argument_pack)
7831 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
7832 TREE_CONSTANT (argument_pack) = 1;
7835 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7836 if (CHECKING_P)
7837 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7838 TREE_VEC_LENGTH (packed_args));
7839 return argument_pack;
7842 /* Returns the number of pack expansions in the template argument vector
7843 ARGS. */
7845 static int
7846 pack_expansion_args_count (tree args)
7848 int i;
7849 int count = 0;
7850 if (args)
7851 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7853 tree elt = TREE_VEC_ELT (args, i);
7854 if (elt && PACK_EXPANSION_P (elt))
7855 ++count;
7857 return count;
7860 /* Convert all template arguments to their appropriate types, and
7861 return a vector containing the innermost resulting template
7862 arguments. If any error occurs, return error_mark_node. Error and
7863 warning messages are issued under control of COMPLAIN.
7865 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7866 for arguments not specified in ARGS. Otherwise, if
7867 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7868 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7869 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7870 ARGS. */
7872 static tree
7873 coerce_template_parms (tree parms,
7874 tree args,
7875 tree in_decl,
7876 tsubst_flags_t complain,
7877 bool require_all_args,
7878 bool use_default_args)
7880 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7881 tree orig_inner_args;
7882 tree inner_args;
7883 tree new_args;
7884 tree new_inner_args;
7885 int saved_unevaluated_operand;
7886 int saved_inhibit_evaluation_warnings;
7888 /* When used as a boolean value, indicates whether this is a
7889 variadic template parameter list. Since it's an int, we can also
7890 subtract it from nparms to get the number of non-variadic
7891 parameters. */
7892 int variadic_p = 0;
7893 int variadic_args_p = 0;
7894 int post_variadic_parms = 0;
7896 /* Likewise for parameters with default arguments. */
7897 int default_p = 0;
7899 if (args == error_mark_node)
7900 return error_mark_node;
7902 nparms = TREE_VEC_LENGTH (parms);
7904 /* Determine if there are any parameter packs or default arguments. */
7905 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7907 tree parm = TREE_VEC_ELT (parms, parm_idx);
7908 if (variadic_p)
7909 ++post_variadic_parms;
7910 if (template_parameter_pack_p (TREE_VALUE (parm)))
7911 ++variadic_p;
7912 if (TREE_PURPOSE (parm))
7913 ++default_p;
7916 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7917 /* If there are no parameters that follow a parameter pack, we need to
7918 expand any argument packs so that we can deduce a parameter pack from
7919 some non-packed args followed by an argument pack, as in variadic85.C.
7920 If there are such parameters, we need to leave argument packs intact
7921 so the arguments are assigned properly. This can happen when dealing
7922 with a nested class inside a partial specialization of a class
7923 template, as in variadic92.C, or when deducing a template parameter pack
7924 from a sub-declarator, as in variadic114.C. */
7925 if (!post_variadic_parms)
7926 inner_args = expand_template_argument_pack (inner_args);
7928 /* Count any pack expansion args. */
7929 variadic_args_p = pack_expansion_args_count (inner_args);
7931 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7932 if ((nargs - variadic_args_p > nparms && !variadic_p)
7933 || (nargs < nparms - variadic_p
7934 && require_all_args
7935 && !variadic_args_p
7936 && (!use_default_args
7937 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7938 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7940 if (complain & tf_error)
7942 if (variadic_p || default_p)
7944 nparms -= variadic_p + default_p;
7945 error ("wrong number of template arguments "
7946 "(%d, should be at least %d)", nargs, nparms);
7948 else
7949 error ("wrong number of template arguments "
7950 "(%d, should be %d)", nargs, nparms);
7952 if (in_decl)
7953 inform (DECL_SOURCE_LOCATION (in_decl),
7954 "provided for %qD", in_decl);
7957 return error_mark_node;
7959 /* We can't pass a pack expansion to a non-pack parameter of an alias
7960 template (DR 1430). */
7961 else if (in_decl
7962 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7963 || concept_template_p (in_decl))
7964 && variadic_args_p
7965 && nargs - variadic_args_p < nparms - variadic_p)
7967 if (complain & tf_error)
7969 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7971 tree arg = TREE_VEC_ELT (inner_args, i);
7972 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7974 if (PACK_EXPANSION_P (arg)
7975 && !template_parameter_pack_p (parm))
7977 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7978 error_at (location_of (arg),
7979 "pack expansion argument for non-pack parameter "
7980 "%qD of alias template %qD", parm, in_decl);
7981 else
7982 error_at (location_of (arg),
7983 "pack expansion argument for non-pack parameter "
7984 "%qD of concept %qD", parm, in_decl);
7985 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7986 goto found;
7989 gcc_unreachable ();
7990 found:;
7992 return error_mark_node;
7995 /* We need to evaluate the template arguments, even though this
7996 template-id may be nested within a "sizeof". */
7997 saved_unevaluated_operand = cp_unevaluated_operand;
7998 cp_unevaluated_operand = 0;
7999 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8000 c_inhibit_evaluation_warnings = 0;
8001 new_inner_args = make_tree_vec (nparms);
8002 new_args = add_outermost_template_args (args, new_inner_args);
8003 int pack_adjust = 0;
8004 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8006 tree arg;
8007 tree parm;
8009 /* Get the Ith template parameter. */
8010 parm = TREE_VEC_ELT (parms, parm_idx);
8012 if (parm == error_mark_node)
8014 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8015 continue;
8018 /* Calculate the next argument. */
8019 if (arg_idx < nargs)
8020 arg = TREE_VEC_ELT (inner_args, arg_idx);
8021 else
8022 arg = NULL_TREE;
8024 if (template_parameter_pack_p (TREE_VALUE (parm))
8025 && !(arg && ARGUMENT_PACK_P (arg)))
8027 /* Some arguments will be placed in the
8028 template parameter pack PARM. */
8029 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8030 inner_args, arg_idx,
8031 new_args, &lost,
8032 in_decl, complain);
8034 if (arg == NULL_TREE)
8036 /* We don't know how many args we have yet, just use the
8037 unconverted (and still packed) ones for now. */
8038 new_inner_args = orig_inner_args;
8039 arg_idx = nargs;
8040 break;
8043 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8045 /* Store this argument. */
8046 if (arg == error_mark_node)
8048 lost++;
8049 /* We are done with all of the arguments. */
8050 arg_idx = nargs;
8052 else
8054 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8055 arg_idx += pack_adjust;
8058 continue;
8060 else if (arg)
8062 if (PACK_EXPANSION_P (arg))
8064 /* "If every valid specialization of a variadic template
8065 requires an empty template parameter pack, the template is
8066 ill-formed, no diagnostic required." So check that the
8067 pattern works with this parameter. */
8068 tree pattern = PACK_EXPANSION_PATTERN (arg);
8069 tree conv = convert_template_argument (TREE_VALUE (parm),
8070 pattern, new_args,
8071 complain, parm_idx,
8072 in_decl);
8073 if (conv == error_mark_node)
8075 if (complain & tf_error)
8076 inform (input_location, "so any instantiation with a "
8077 "non-empty parameter pack would be ill-formed");
8078 ++lost;
8080 else if (TYPE_P (conv) && !TYPE_P (pattern))
8081 /* Recover from missing typename. */
8082 TREE_VEC_ELT (inner_args, arg_idx)
8083 = make_pack_expansion (conv);
8085 /* We don't know how many args we have yet, just
8086 use the unconverted ones for now. */
8087 new_inner_args = inner_args;
8088 arg_idx = nargs;
8089 break;
8092 else if (require_all_args)
8094 /* There must be a default arg in this case. */
8095 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8096 complain, in_decl);
8097 /* The position of the first default template argument,
8098 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8099 Record that. */
8100 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8101 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8102 arg_idx - pack_adjust);
8104 else
8105 break;
8107 if (arg == error_mark_node)
8109 if (complain & tf_error)
8110 error ("template argument %d is invalid", arg_idx + 1);
8112 else if (!arg)
8113 /* This only occurs if there was an error in the template
8114 parameter list itself (which we would already have
8115 reported) that we are trying to recover from, e.g., a class
8116 template with a parameter list such as
8117 template<typename..., typename>. */
8118 ++lost;
8119 else
8120 arg = convert_template_argument (TREE_VALUE (parm),
8121 arg, new_args, complain,
8122 parm_idx, in_decl);
8124 if (arg == error_mark_node)
8125 lost++;
8126 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8128 cp_unevaluated_operand = saved_unevaluated_operand;
8129 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8131 if (variadic_p && arg_idx < nargs)
8133 if (complain & tf_error)
8135 error ("wrong number of template arguments "
8136 "(%d, should be %d)", nargs, arg_idx);
8137 if (in_decl)
8138 error ("provided for %q+D", in_decl);
8140 return error_mark_node;
8143 if (lost)
8144 return error_mark_node;
8146 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8147 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8148 TREE_VEC_LENGTH (new_inner_args));
8150 return new_inner_args;
8153 /* Convert all template arguments to their appropriate types, and
8154 return a vector containing the innermost resulting template
8155 arguments. If any error occurs, return error_mark_node. Error and
8156 warning messages are not issued.
8158 Note that no function argument deduction is performed, and default
8159 arguments are used to fill in unspecified arguments. */
8160 tree
8161 coerce_template_parms (tree parms, tree args, tree in_decl)
8163 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8166 /* Convert all template arguments to their appropriate type, and
8167 instantiate default arguments as needed. This returns a vector
8168 containing the innermost resulting template arguments, or
8169 error_mark_node if unsuccessful. */
8170 tree
8171 coerce_template_parms (tree parms, tree args, tree in_decl,
8172 tsubst_flags_t complain)
8174 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8177 /* Like coerce_template_parms. If PARMS represents all template
8178 parameters levels, this function returns a vector of vectors
8179 representing all the resulting argument levels. Note that in this
8180 case, only the innermost arguments are coerced because the
8181 outermost ones are supposed to have been coerced already.
8183 Otherwise, if PARMS represents only (the innermost) vector of
8184 parameters, this function returns a vector containing just the
8185 innermost resulting arguments. */
8187 static tree
8188 coerce_innermost_template_parms (tree parms,
8189 tree args,
8190 tree in_decl,
8191 tsubst_flags_t complain,
8192 bool require_all_args,
8193 bool use_default_args)
8195 int parms_depth = TMPL_PARMS_DEPTH (parms);
8196 int args_depth = TMPL_ARGS_DEPTH (args);
8197 tree coerced_args;
8199 if (parms_depth > 1)
8201 coerced_args = make_tree_vec (parms_depth);
8202 tree level;
8203 int cur_depth;
8205 for (level = parms, cur_depth = parms_depth;
8206 parms_depth > 0 && level != NULL_TREE;
8207 level = TREE_CHAIN (level), --cur_depth)
8209 tree l;
8210 if (cur_depth == args_depth)
8211 l = coerce_template_parms (TREE_VALUE (level),
8212 args, in_decl, complain,
8213 require_all_args,
8214 use_default_args);
8215 else
8216 l = TMPL_ARGS_LEVEL (args, cur_depth);
8218 if (l == error_mark_node)
8219 return error_mark_node;
8221 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8224 else
8225 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8226 args, in_decl, complain,
8227 require_all_args,
8228 use_default_args);
8229 return coerced_args;
8232 /* Returns 1 if template args OT and NT are equivalent. */
8235 template_args_equal (tree ot, tree nt)
8237 if (nt == ot)
8238 return 1;
8239 if (nt == NULL_TREE || ot == NULL_TREE)
8240 return false;
8241 if (nt == any_targ_node || ot == any_targ_node)
8242 return true;
8244 if (TREE_CODE (nt) == TREE_VEC)
8245 /* For member templates */
8246 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8247 else if (PACK_EXPANSION_P (ot))
8248 return (PACK_EXPANSION_P (nt)
8249 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8250 PACK_EXPANSION_PATTERN (nt))
8251 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8252 PACK_EXPANSION_EXTRA_ARGS (nt)));
8253 else if (ARGUMENT_PACK_P (ot))
8255 int i, len;
8256 tree opack, npack;
8258 if (!ARGUMENT_PACK_P (nt))
8259 return 0;
8261 opack = ARGUMENT_PACK_ARGS (ot);
8262 npack = ARGUMENT_PACK_ARGS (nt);
8263 len = TREE_VEC_LENGTH (opack);
8264 if (TREE_VEC_LENGTH (npack) != len)
8265 return 0;
8266 for (i = 0; i < len; ++i)
8267 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8268 TREE_VEC_ELT (npack, i)))
8269 return 0;
8270 return 1;
8272 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8273 gcc_unreachable ();
8274 else if (TYPE_P (nt))
8276 if (!TYPE_P (ot))
8277 return false;
8278 /* Don't treat an alias template specialization with dependent
8279 arguments as equivalent to its underlying type when used as a
8280 template argument; we need them to be distinct so that we
8281 substitute into the specialization arguments at instantiation
8282 time. And aliases can't be equivalent without being ==, so
8283 we don't need to look any deeper. */
8284 if (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot))
8285 return false;
8286 else
8287 return same_type_p (ot, nt);
8289 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8290 return 0;
8291 else
8293 /* Try to treat a template non-type argument that has been converted
8294 to the parameter type as equivalent to one that hasn't yet. */
8295 for (enum tree_code code1 = TREE_CODE (ot);
8296 CONVERT_EXPR_CODE_P (code1)
8297 || code1 == NON_LVALUE_EXPR;
8298 code1 = TREE_CODE (ot))
8299 ot = TREE_OPERAND (ot, 0);
8300 for (enum tree_code code2 = TREE_CODE (nt);
8301 CONVERT_EXPR_CODE_P (code2)
8302 || code2 == NON_LVALUE_EXPR;
8303 code2 = TREE_CODE (nt))
8304 nt = TREE_OPERAND (nt, 0);
8306 return cp_tree_equal (ot, nt);
8310 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8311 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8312 NEWARG_PTR with the offending arguments if they are non-NULL. */
8315 comp_template_args (tree oldargs, tree newargs,
8316 tree *oldarg_ptr, tree *newarg_ptr)
8318 int i;
8320 if (oldargs == newargs)
8321 return 1;
8323 if (!oldargs || !newargs)
8324 return 0;
8326 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8327 return 0;
8329 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8331 tree nt = TREE_VEC_ELT (newargs, i);
8332 tree ot = TREE_VEC_ELT (oldargs, i);
8334 if (! template_args_equal (ot, nt))
8336 if (oldarg_ptr != NULL)
8337 *oldarg_ptr = ot;
8338 if (newarg_ptr != NULL)
8339 *newarg_ptr = nt;
8340 return 0;
8343 return 1;
8346 static void
8347 add_pending_template (tree d)
8349 tree ti = (TYPE_P (d)
8350 ? CLASSTYPE_TEMPLATE_INFO (d)
8351 : DECL_TEMPLATE_INFO (d));
8352 struct pending_template *pt;
8353 int level;
8355 if (TI_PENDING_TEMPLATE_FLAG (ti))
8356 return;
8358 /* We are called both from instantiate_decl, where we've already had a
8359 tinst_level pushed, and instantiate_template, where we haven't.
8360 Compensate. */
8361 level = !current_tinst_level || current_tinst_level->decl != d;
8363 if (level)
8364 push_tinst_level (d);
8366 pt = ggc_alloc<pending_template> ();
8367 pt->next = NULL;
8368 pt->tinst = current_tinst_level;
8369 if (last_pending_template)
8370 last_pending_template->next = pt;
8371 else
8372 pending_templates = pt;
8374 last_pending_template = pt;
8376 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8378 if (level)
8379 pop_tinst_level ();
8383 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8384 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8385 documentation for TEMPLATE_ID_EXPR. */
8387 tree
8388 lookup_template_function (tree fns, tree arglist)
8390 tree type;
8392 if (fns == error_mark_node || arglist == error_mark_node)
8393 return error_mark_node;
8395 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8397 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8399 error ("%q#D is not a function template", fns);
8400 return error_mark_node;
8403 if (BASELINK_P (fns))
8405 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8406 unknown_type_node,
8407 BASELINK_FUNCTIONS (fns),
8408 arglist);
8409 return fns;
8412 type = TREE_TYPE (fns);
8413 if (TREE_CODE (fns) == OVERLOAD || !type)
8414 type = unknown_type_node;
8416 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8419 /* Within the scope of a template class S<T>, the name S gets bound
8420 (in build_self_reference) to a TYPE_DECL for the class, not a
8421 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8422 or one of its enclosing classes, and that type is a template,
8423 return the associated TEMPLATE_DECL. Otherwise, the original
8424 DECL is returned.
8426 Also handle the case when DECL is a TREE_LIST of ambiguous
8427 injected-class-names from different bases. */
8429 tree
8430 maybe_get_template_decl_from_type_decl (tree decl)
8432 if (decl == NULL_TREE)
8433 return decl;
8435 /* DR 176: A lookup that finds an injected-class-name (10.2
8436 [class.member.lookup]) can result in an ambiguity in certain cases
8437 (for example, if it is found in more than one base class). If all of
8438 the injected-class-names that are found refer to specializations of
8439 the same class template, and if the name is followed by a
8440 template-argument-list, the reference refers to the class template
8441 itself and not a specialization thereof, and is not ambiguous. */
8442 if (TREE_CODE (decl) == TREE_LIST)
8444 tree t, tmpl = NULL_TREE;
8445 for (t = decl; t; t = TREE_CHAIN (t))
8447 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8448 if (!tmpl)
8449 tmpl = elt;
8450 else if (tmpl != elt)
8451 break;
8453 if (tmpl && t == NULL_TREE)
8454 return tmpl;
8455 else
8456 return decl;
8459 return (decl != NULL_TREE
8460 && DECL_SELF_REFERENCE_P (decl)
8461 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8462 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8465 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8466 parameters, find the desired type.
8468 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8470 IN_DECL, if non-NULL, is the template declaration we are trying to
8471 instantiate.
8473 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8474 the class we are looking up.
8476 Issue error and warning messages under control of COMPLAIN.
8478 If the template class is really a local class in a template
8479 function, then the FUNCTION_CONTEXT is the function in which it is
8480 being instantiated.
8482 ??? Note that this function is currently called *twice* for each
8483 template-id: the first time from the parser, while creating the
8484 incomplete type (finish_template_type), and the second type during the
8485 real instantiation (instantiate_template_class). This is surely something
8486 that we want to avoid. It also causes some problems with argument
8487 coercion (see convert_nontype_argument for more information on this). */
8489 static tree
8490 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8491 int entering_scope, tsubst_flags_t complain)
8493 tree templ = NULL_TREE, parmlist;
8494 tree t;
8495 spec_entry **slot;
8496 spec_entry *entry;
8497 spec_entry elt;
8498 hashval_t hash;
8500 if (identifier_p (d1))
8502 tree value = innermost_non_namespace_value (d1);
8503 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8504 templ = value;
8505 else
8507 if (context)
8508 push_decl_namespace (context);
8509 templ = lookup_name (d1);
8510 templ = maybe_get_template_decl_from_type_decl (templ);
8511 if (context)
8512 pop_decl_namespace ();
8514 if (templ)
8515 context = DECL_CONTEXT (templ);
8517 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8519 tree type = TREE_TYPE (d1);
8521 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8522 an implicit typename for the second A. Deal with it. */
8523 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8524 type = TREE_TYPE (type);
8526 if (CLASSTYPE_TEMPLATE_INFO (type))
8528 templ = CLASSTYPE_TI_TEMPLATE (type);
8529 d1 = DECL_NAME (templ);
8532 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8533 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8535 templ = TYPE_TI_TEMPLATE (d1);
8536 d1 = DECL_NAME (templ);
8538 else if (DECL_TYPE_TEMPLATE_P (d1))
8540 templ = d1;
8541 d1 = DECL_NAME (templ);
8542 context = DECL_CONTEXT (templ);
8544 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8546 templ = d1;
8547 d1 = DECL_NAME (templ);
8550 /* Issue an error message if we didn't find a template. */
8551 if (! templ)
8553 if (complain & tf_error)
8554 error ("%qT is not a template", d1);
8555 return error_mark_node;
8558 if (TREE_CODE (templ) != TEMPLATE_DECL
8559 /* Make sure it's a user visible template, if it was named by
8560 the user. */
8561 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8562 && !PRIMARY_TEMPLATE_P (templ)))
8564 if (complain & tf_error)
8566 error ("non-template type %qT used as a template", d1);
8567 if (in_decl)
8568 error ("for template declaration %q+D", in_decl);
8570 return error_mark_node;
8573 complain &= ~tf_user;
8575 /* An alias that just changes the name of a template is equivalent to the
8576 other template, so if any of the arguments are pack expansions, strip
8577 the alias to avoid problems with a pack expansion passed to a non-pack
8578 alias template parameter (DR 1430). */
8579 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8580 templ = get_underlying_template (templ);
8582 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8584 tree parm;
8585 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8586 if (arglist2 == error_mark_node
8587 || (!uses_template_parms (arglist2)
8588 && check_instantiated_args (templ, arglist2, complain)))
8589 return error_mark_node;
8591 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8592 return parm;
8594 else
8596 tree template_type = TREE_TYPE (templ);
8597 tree gen_tmpl;
8598 tree type_decl;
8599 tree found = NULL_TREE;
8600 int arg_depth;
8601 int parm_depth;
8602 int is_dependent_type;
8603 int use_partial_inst_tmpl = false;
8605 if (template_type == error_mark_node)
8606 /* An error occurred while building the template TEMPL, and a
8607 diagnostic has most certainly been emitted for that
8608 already. Let's propagate that error. */
8609 return error_mark_node;
8611 gen_tmpl = most_general_template (templ);
8612 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8613 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8614 arg_depth = TMPL_ARGS_DEPTH (arglist);
8616 if (arg_depth == 1 && parm_depth > 1)
8618 /* We've been given an incomplete set of template arguments.
8619 For example, given:
8621 template <class T> struct S1 {
8622 template <class U> struct S2 {};
8623 template <class U> struct S2<U*> {};
8626 we will be called with an ARGLIST of `U*', but the
8627 TEMPLATE will be `template <class T> template
8628 <class U> struct S1<T>::S2'. We must fill in the missing
8629 arguments. */
8630 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8631 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8632 arg_depth = TMPL_ARGS_DEPTH (arglist);
8635 /* Now we should have enough arguments. */
8636 gcc_assert (parm_depth == arg_depth);
8638 /* From here on, we're only interested in the most general
8639 template. */
8641 /* Calculate the BOUND_ARGS. These will be the args that are
8642 actually tsubst'd into the definition to create the
8643 instantiation. */
8644 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8645 complain,
8646 /*require_all_args=*/true,
8647 /*use_default_args=*/true);
8649 if (arglist == error_mark_node)
8650 /* We were unable to bind the arguments. */
8651 return error_mark_node;
8653 /* In the scope of a template class, explicit references to the
8654 template class refer to the type of the template, not any
8655 instantiation of it. For example, in:
8657 template <class T> class C { void f(C<T>); }
8659 the `C<T>' is just the same as `C'. Outside of the
8660 class, however, such a reference is an instantiation. */
8661 if (entering_scope
8662 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8663 || currently_open_class (template_type))
8665 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (template_type);
8667 if (comp_template_args (TI_ARGS (tinfo), arglist))
8668 return template_type;
8671 /* If we already have this specialization, return it. */
8672 elt.tmpl = gen_tmpl;
8673 elt.args = arglist;
8674 elt.spec = NULL_TREE;
8675 hash = spec_hasher::hash (&elt);
8676 entry = type_specializations->find_with_hash (&elt, hash);
8678 if (entry)
8679 return entry->spec;
8681 /* If the the template's constraints are not satisfied,
8682 then we cannot form a valid type.
8684 Note that the check is deferred until after the hash
8685 lookup. This prevents redundant checks on previously
8686 instantiated specializations. */
8687 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8689 if (complain & tf_error)
8691 error ("template constraint failure");
8692 diagnose_constraints (input_location, gen_tmpl, arglist);
8694 return error_mark_node;
8697 is_dependent_type = uses_template_parms (arglist);
8699 /* If the deduced arguments are invalid, then the binding
8700 failed. */
8701 if (!is_dependent_type
8702 && check_instantiated_args (gen_tmpl,
8703 INNERMOST_TEMPLATE_ARGS (arglist),
8704 complain))
8705 return error_mark_node;
8707 if (!is_dependent_type
8708 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8709 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8710 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8712 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8713 DECL_NAME (gen_tmpl),
8714 /*tag_scope=*/ts_global);
8715 return found;
8718 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8719 complain, in_decl);
8720 if (context == error_mark_node)
8721 return error_mark_node;
8723 if (!context)
8724 context = global_namespace;
8726 /* Create the type. */
8727 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8729 /* The user referred to a specialization of an alias
8730 template represented by GEN_TMPL.
8732 [temp.alias]/2 says:
8734 When a template-id refers to the specialization of an
8735 alias template, it is equivalent to the associated
8736 type obtained by substitution of its
8737 template-arguments for the template-parameters in the
8738 type-id of the alias template. */
8740 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8741 /* Note that the call above (by indirectly calling
8742 register_specialization in tsubst_decl) registers the
8743 TYPE_DECL representing the specialization of the alias
8744 template. So next time someone substitutes ARGLIST for
8745 the template parms into the alias template (GEN_TMPL),
8746 she'll get that TYPE_DECL back. */
8748 if (t == error_mark_node)
8749 return t;
8751 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8753 if (!is_dependent_type)
8755 set_current_access_from_decl (TYPE_NAME (template_type));
8756 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8757 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8758 arglist, complain, in_decl),
8759 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8760 arglist, complain, in_decl),
8761 SCOPED_ENUM_P (template_type), NULL);
8763 if (t == error_mark_node)
8764 return t;
8766 else
8768 /* We don't want to call start_enum for this type, since
8769 the values for the enumeration constants may involve
8770 template parameters. And, no one should be interested
8771 in the enumeration constants for such a type. */
8772 t = cxx_make_type (ENUMERAL_TYPE);
8773 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8775 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8776 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8777 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8779 else if (CLASS_TYPE_P (template_type))
8781 t = make_class_type (TREE_CODE (template_type));
8782 CLASSTYPE_DECLARED_CLASS (t)
8783 = CLASSTYPE_DECLARED_CLASS (template_type);
8784 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8786 /* A local class. Make sure the decl gets registered properly. */
8787 if (context == current_function_decl)
8788 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8790 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8791 /* This instantiation is another name for the primary
8792 template type. Set the TYPE_CANONICAL field
8793 appropriately. */
8794 TYPE_CANONICAL (t) = template_type;
8795 else if (any_template_arguments_need_structural_equality_p (arglist))
8796 /* Some of the template arguments require structural
8797 equality testing, so this template class requires
8798 structural equality testing. */
8799 SET_TYPE_STRUCTURAL_EQUALITY (t);
8801 else
8802 gcc_unreachable ();
8804 /* If we called start_enum or pushtag above, this information
8805 will already be set up. */
8806 if (!TYPE_NAME (t))
8808 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8810 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8811 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8812 DECL_SOURCE_LOCATION (type_decl)
8813 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8815 else
8816 type_decl = TYPE_NAME (t);
8818 if (CLASS_TYPE_P (template_type))
8820 TREE_PRIVATE (type_decl)
8821 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8822 TREE_PROTECTED (type_decl)
8823 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8824 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8826 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8827 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8831 if (OVERLOAD_TYPE_P (t)
8832 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8834 static const char *tags[] = {"abi_tag", "may_alias"};
8836 for (unsigned ix = 0; ix != 2; ix++)
8838 tree attributes
8839 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8841 if (attributes)
8842 TYPE_ATTRIBUTES (t)
8843 = tree_cons (TREE_PURPOSE (attributes),
8844 TREE_VALUE (attributes),
8845 TYPE_ATTRIBUTES (t));
8849 /* Let's consider the explicit specialization of a member
8850 of a class template specialization that is implicitly instantiated,
8851 e.g.:
8852 template<class T>
8853 struct S
8855 template<class U> struct M {}; //#0
8858 template<>
8859 template<>
8860 struct S<int>::M<char> //#1
8862 int i;
8864 [temp.expl.spec]/4 says this is valid.
8866 In this case, when we write:
8867 S<int>::M<char> m;
8869 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8870 the one of #0.
8872 When we encounter #1, we want to store the partial instantiation
8873 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8875 For all cases other than this "explicit specialization of member of a
8876 class template", we just want to store the most general template into
8877 the CLASSTYPE_TI_TEMPLATE of M.
8879 This case of "explicit specialization of member of a class template"
8880 only happens when:
8881 1/ the enclosing class is an instantiation of, and therefore not
8882 the same as, the context of the most general template, and
8883 2/ we aren't looking at the partial instantiation itself, i.e.
8884 the innermost arguments are not the same as the innermost parms of
8885 the most general template.
8887 So it's only when 1/ and 2/ happens that we want to use the partial
8888 instantiation of the member template in lieu of its most general
8889 template. */
8891 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8892 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8893 /* the enclosing class must be an instantiation... */
8894 && CLASS_TYPE_P (context)
8895 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8897 TREE_VEC_LENGTH (arglist)--;
8898 ++processing_template_decl;
8899 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
8900 tree partial_inst_args =
8901 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
8902 arglist, complain, NULL_TREE);
8903 --processing_template_decl;
8904 TREE_VEC_LENGTH (arglist)++;
8905 if (partial_inst_args == error_mark_node)
8906 return error_mark_node;
8907 use_partial_inst_tmpl =
8908 /*...and we must not be looking at the partial instantiation
8909 itself. */
8910 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8911 partial_inst_args);
8914 if (!use_partial_inst_tmpl)
8915 /* This case is easy; there are no member templates involved. */
8916 found = gen_tmpl;
8917 else
8919 /* This is a full instantiation of a member template. Find
8920 the partial instantiation of which this is an instance. */
8922 /* Temporarily reduce by one the number of levels in the ARGLIST
8923 so as to avoid comparing the last set of arguments. */
8924 TREE_VEC_LENGTH (arglist)--;
8925 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8926 TREE_VEC_LENGTH (arglist)++;
8927 /* FOUND is either a proper class type, or an alias
8928 template specialization. In the later case, it's a
8929 TYPE_DECL, resulting from the substituting of arguments
8930 for parameters in the TYPE_DECL of the alias template
8931 done earlier. So be careful while getting the template
8932 of FOUND. */
8933 found = (TREE_CODE (found) == TEMPLATE_DECL
8934 ? found
8935 : (TREE_CODE (found) == TYPE_DECL
8936 ? DECL_TI_TEMPLATE (found)
8937 : CLASSTYPE_TI_TEMPLATE (found)));
8940 // Build template info for the new specialization.
8941 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8943 elt.spec = t;
8944 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8945 entry = ggc_alloc<spec_entry> ();
8946 *entry = elt;
8947 *slot = entry;
8949 /* Note this use of the partial instantiation so we can check it
8950 later in maybe_process_partial_specialization. */
8951 DECL_TEMPLATE_INSTANTIATIONS (found)
8952 = tree_cons (arglist, t,
8953 DECL_TEMPLATE_INSTANTIATIONS (found));
8955 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8956 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8957 /* Now that the type has been registered on the instantiations
8958 list, we set up the enumerators. Because the enumeration
8959 constants may involve the enumeration type itself, we make
8960 sure to register the type first, and then create the
8961 constants. That way, doing tsubst_expr for the enumeration
8962 constants won't result in recursive calls here; we'll find
8963 the instantiation and exit above. */
8964 tsubst_enum (template_type, t, arglist);
8966 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8967 /* If the type makes use of template parameters, the
8968 code that generates debugging information will crash. */
8969 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8971 /* Possibly limit visibility based on template args. */
8972 TREE_PUBLIC (type_decl) = 1;
8973 determine_visibility (type_decl);
8975 inherit_targ_abi_tags (t);
8977 return t;
8981 /* Wrapper for lookup_template_class_1. */
8983 tree
8984 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8985 int entering_scope, tsubst_flags_t complain)
8987 tree ret;
8988 timevar_push (TV_TEMPLATE_INST);
8989 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8990 entering_scope, complain);
8991 timevar_pop (TV_TEMPLATE_INST);
8992 return ret;
8995 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8997 tree
8998 lookup_template_variable (tree templ, tree arglist)
9000 /* The type of the expression is NULL_TREE since the template-id could refer
9001 to an explicit or partial specialization. */
9002 tree type = NULL_TREE;
9003 if (flag_concepts && variable_concept_p (templ))
9004 /* Except that concepts are always bool. */
9005 type = boolean_type_node;
9006 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9009 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9011 tree
9012 finish_template_variable (tree var, tsubst_flags_t complain)
9014 tree templ = TREE_OPERAND (var, 0);
9015 tree arglist = TREE_OPERAND (var, 1);
9017 /* We never want to return a VAR_DECL for a variable concept, since they
9018 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9019 bool concept_p = flag_concepts && variable_concept_p (templ);
9020 if (concept_p && processing_template_decl)
9021 return var;
9023 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9024 arglist = add_outermost_template_args (tmpl_args, arglist);
9026 templ = most_general_template (templ);
9027 tree parms = DECL_TEMPLATE_PARMS (templ);
9028 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9029 /*req_all*/true,
9030 /*use_default*/true);
9032 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9034 if (complain & tf_error)
9036 error ("use of invalid variable template %qE", var);
9037 diagnose_constraints (location_of (var), templ, arglist);
9039 return error_mark_node;
9042 /* If a template-id refers to a specialization of a variable
9043 concept, then the expression is true if and only if the
9044 concept's constraints are satisfied by the given template
9045 arguments.
9047 NOTE: This is an extension of Concepts Lite TS that
9048 allows constraints to be used in expressions. */
9049 if (concept_p)
9051 tree decl = DECL_TEMPLATE_RESULT (templ);
9052 return evaluate_variable_concept (decl, arglist);
9055 return instantiate_template (templ, arglist, complain);
9058 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9059 TARGS template args, and instantiate it if it's not dependent. */
9061 tree
9062 lookup_and_finish_template_variable (tree templ, tree targs,
9063 tsubst_flags_t complain)
9065 templ = lookup_template_variable (templ, targs);
9066 if (!any_dependent_template_arguments_p (targs))
9068 templ = finish_template_variable (templ, complain);
9069 mark_used (templ);
9072 return convert_from_reference (templ);
9076 struct pair_fn_data
9078 tree_fn_t fn;
9079 tree_fn_t any_fn;
9080 void *data;
9081 /* True when we should also visit template parameters that occur in
9082 non-deduced contexts. */
9083 bool include_nondeduced_p;
9084 hash_set<tree> *visited;
9087 /* Called from for_each_template_parm via walk_tree. */
9089 static tree
9090 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9092 tree t = *tp;
9093 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9094 tree_fn_t fn = pfd->fn;
9095 void *data = pfd->data;
9096 tree result = NULL_TREE;
9098 #define WALK_SUBTREE(NODE) \
9099 do \
9101 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9102 pfd->include_nondeduced_p, \
9103 pfd->any_fn); \
9104 if (result) goto out; \
9106 while (0)
9108 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9109 return t;
9111 if (TYPE_P (t)
9112 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9113 WALK_SUBTREE (TYPE_CONTEXT (t));
9115 switch (TREE_CODE (t))
9117 case RECORD_TYPE:
9118 if (TYPE_PTRMEMFUNC_P (t))
9119 break;
9120 /* Fall through. */
9122 case UNION_TYPE:
9123 case ENUMERAL_TYPE:
9124 if (!TYPE_TEMPLATE_INFO (t))
9125 *walk_subtrees = 0;
9126 else
9127 WALK_SUBTREE (TYPE_TI_ARGS (t));
9128 break;
9130 case INTEGER_TYPE:
9131 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9132 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9133 break;
9135 case METHOD_TYPE:
9136 /* Since we're not going to walk subtrees, we have to do this
9137 explicitly here. */
9138 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9139 /* Fall through. */
9141 case FUNCTION_TYPE:
9142 /* Check the return type. */
9143 WALK_SUBTREE (TREE_TYPE (t));
9145 /* Check the parameter types. Since default arguments are not
9146 instantiated until they are needed, the TYPE_ARG_TYPES may
9147 contain expressions that involve template parameters. But,
9148 no-one should be looking at them yet. And, once they're
9149 instantiated, they don't contain template parameters, so
9150 there's no point in looking at them then, either. */
9152 tree parm;
9154 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9155 WALK_SUBTREE (TREE_VALUE (parm));
9157 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9158 want walk_tree walking into them itself. */
9159 *walk_subtrees = 0;
9162 if (flag_noexcept_type)
9164 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9165 if (spec)
9166 WALK_SUBTREE (TREE_PURPOSE (spec));
9168 break;
9170 case TYPEOF_TYPE:
9171 case UNDERLYING_TYPE:
9172 if (pfd->include_nondeduced_p
9173 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9174 pfd->visited,
9175 pfd->include_nondeduced_p,
9176 pfd->any_fn))
9177 return error_mark_node;
9178 break;
9180 case FUNCTION_DECL:
9181 case VAR_DECL:
9182 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9183 WALK_SUBTREE (DECL_TI_ARGS (t));
9184 /* Fall through. */
9186 case PARM_DECL:
9187 case CONST_DECL:
9188 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9189 WALK_SUBTREE (DECL_INITIAL (t));
9190 if (DECL_CONTEXT (t)
9191 && pfd->include_nondeduced_p)
9192 WALK_SUBTREE (DECL_CONTEXT (t));
9193 break;
9195 case BOUND_TEMPLATE_TEMPLATE_PARM:
9196 /* Record template parameters such as `T' inside `TT<T>'. */
9197 WALK_SUBTREE (TYPE_TI_ARGS (t));
9198 /* Fall through. */
9200 case TEMPLATE_TEMPLATE_PARM:
9201 case TEMPLATE_TYPE_PARM:
9202 case TEMPLATE_PARM_INDEX:
9203 if (fn && (*fn)(t, data))
9204 return t;
9205 else if (!fn)
9206 return t;
9207 break;
9209 case TEMPLATE_DECL:
9210 /* A template template parameter is encountered. */
9211 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9212 WALK_SUBTREE (TREE_TYPE (t));
9214 /* Already substituted template template parameter */
9215 *walk_subtrees = 0;
9216 break;
9218 case TYPENAME_TYPE:
9219 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9220 partial instantiation. */
9221 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9222 break;
9224 case CONSTRUCTOR:
9225 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9226 && pfd->include_nondeduced_p)
9227 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9228 break;
9230 case INDIRECT_REF:
9231 case COMPONENT_REF:
9232 /* If there's no type, then this thing must be some expression
9233 involving template parameters. */
9234 if (!fn && !TREE_TYPE (t))
9235 return error_mark_node;
9236 break;
9238 case MODOP_EXPR:
9239 case CAST_EXPR:
9240 case IMPLICIT_CONV_EXPR:
9241 case REINTERPRET_CAST_EXPR:
9242 case CONST_CAST_EXPR:
9243 case STATIC_CAST_EXPR:
9244 case DYNAMIC_CAST_EXPR:
9245 case ARROW_EXPR:
9246 case DOTSTAR_EXPR:
9247 case TYPEID_EXPR:
9248 case PSEUDO_DTOR_EXPR:
9249 if (!fn)
9250 return error_mark_node;
9251 break;
9253 default:
9254 break;
9257 #undef WALK_SUBTREE
9259 /* We didn't find any template parameters we liked. */
9260 out:
9261 return result;
9264 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9265 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9266 call FN with the parameter and the DATA.
9267 If FN returns nonzero, the iteration is terminated, and
9268 for_each_template_parm returns 1. Otherwise, the iteration
9269 continues. If FN never returns a nonzero value, the value
9270 returned by for_each_template_parm is 0. If FN is NULL, it is
9271 considered to be the function which always returns 1.
9273 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9274 parameters that occur in non-deduced contexts. When false, only
9275 visits those template parameters that can be deduced. */
9277 static tree
9278 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9279 hash_set<tree> *visited,
9280 bool include_nondeduced_p,
9281 tree_fn_t any_fn)
9283 struct pair_fn_data pfd;
9284 tree result;
9286 /* Set up. */
9287 pfd.fn = fn;
9288 pfd.any_fn = any_fn;
9289 pfd.data = data;
9290 pfd.include_nondeduced_p = include_nondeduced_p;
9292 /* Walk the tree. (Conceptually, we would like to walk without
9293 duplicates, but for_each_template_parm_r recursively calls
9294 for_each_template_parm, so we would need to reorganize a fair
9295 bit to use walk_tree_without_duplicates, so we keep our own
9296 visited list.) */
9297 if (visited)
9298 pfd.visited = visited;
9299 else
9300 pfd.visited = new hash_set<tree>;
9301 result = cp_walk_tree (&t,
9302 for_each_template_parm_r,
9303 &pfd,
9304 pfd.visited);
9306 /* Clean up. */
9307 if (!visited)
9309 delete pfd.visited;
9310 pfd.visited = 0;
9313 return result;
9316 /* Returns true if T depends on any template parameter. */
9319 uses_template_parms (tree t)
9321 if (t == NULL_TREE)
9322 return false;
9324 bool dependent_p;
9325 int saved_processing_template_decl;
9327 saved_processing_template_decl = processing_template_decl;
9328 if (!saved_processing_template_decl)
9329 processing_template_decl = 1;
9330 if (TYPE_P (t))
9331 dependent_p = dependent_type_p (t);
9332 else if (TREE_CODE (t) == TREE_VEC)
9333 dependent_p = any_dependent_template_arguments_p (t);
9334 else if (TREE_CODE (t) == TREE_LIST)
9335 dependent_p = (uses_template_parms (TREE_VALUE (t))
9336 || uses_template_parms (TREE_CHAIN (t)));
9337 else if (TREE_CODE (t) == TYPE_DECL)
9338 dependent_p = dependent_type_p (TREE_TYPE (t));
9339 else if (DECL_P (t)
9340 || EXPR_P (t)
9341 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9342 || TREE_CODE (t) == OVERLOAD
9343 || BASELINK_P (t)
9344 || identifier_p (t)
9345 || TREE_CODE (t) == TRAIT_EXPR
9346 || TREE_CODE (t) == CONSTRUCTOR
9347 || CONSTANT_CLASS_P (t))
9348 dependent_p = (type_dependent_expression_p (t)
9349 || value_dependent_expression_p (t));
9350 else
9352 gcc_assert (t == error_mark_node);
9353 dependent_p = false;
9356 processing_template_decl = saved_processing_template_decl;
9358 return dependent_p;
9361 /* Returns true iff current_function_decl is an incompletely instantiated
9362 template. Useful instead of processing_template_decl because the latter
9363 is set to 0 during instantiate_non_dependent_expr. */
9365 bool
9366 in_template_function (void)
9368 tree fn = current_function_decl;
9369 bool ret;
9370 ++processing_template_decl;
9371 ret = (fn && DECL_LANG_SPECIFIC (fn)
9372 && DECL_TEMPLATE_INFO (fn)
9373 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9374 --processing_template_decl;
9375 return ret;
9378 /* Returns true if T depends on any template parameter with level LEVEL. */
9380 bool
9381 uses_template_parms_level (tree t, int level)
9383 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9384 /*include_nondeduced_p=*/true);
9387 /* Returns true if the signature of DECL depends on any template parameter from
9388 its enclosing class. */
9390 bool
9391 uses_outer_template_parms (tree decl)
9393 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9394 if (depth == 0)
9395 return false;
9396 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9397 &depth, NULL, /*include_nondeduced_p=*/true))
9398 return true;
9399 if (PRIMARY_TEMPLATE_P (decl)
9400 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9401 (DECL_TEMPLATE_PARMS (decl)),
9402 template_parm_outer_level,
9403 &depth, NULL, /*include_nondeduced_p=*/true))
9404 return true;
9405 tree ci = get_constraints (decl);
9406 if (ci)
9407 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9408 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9409 &depth, NULL, /*nondeduced*/true))
9410 return true;
9411 return false;
9414 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9415 ill-formed translation unit, i.e. a variable or function that isn't
9416 usable in a constant expression. */
9418 static inline bool
9419 neglectable_inst_p (tree d)
9421 return (DECL_P (d)
9422 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9423 : decl_maybe_constant_var_p (d)));
9426 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9427 neglectable and instantiated from within an erroneous instantiation. */
9429 static bool
9430 limit_bad_template_recursion (tree decl)
9432 struct tinst_level *lev = current_tinst_level;
9433 int errs = errorcount + sorrycount;
9434 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9435 return false;
9437 for (; lev; lev = lev->next)
9438 if (neglectable_inst_p (lev->decl))
9439 break;
9441 return (lev && errs > lev->errors);
9444 static int tinst_depth;
9445 extern int max_tinst_depth;
9446 int depth_reached;
9448 static GTY(()) struct tinst_level *last_error_tinst_level;
9450 /* We're starting to instantiate D; record the template instantiation context
9451 for diagnostics and to restore it later. */
9453 bool
9454 push_tinst_level (tree d)
9456 return push_tinst_level_loc (d, input_location);
9459 /* We're starting to instantiate D; record the template instantiation context
9460 at LOC for diagnostics and to restore it later. */
9462 bool
9463 push_tinst_level_loc (tree d, location_t loc)
9465 struct tinst_level *new_level;
9467 if (tinst_depth >= max_tinst_depth)
9469 /* Tell error.c not to try to instantiate any templates. */
9470 at_eof = 2;
9471 fatal_error (input_location,
9472 "template instantiation depth exceeds maximum of %d"
9473 " (use -ftemplate-depth= to increase the maximum)",
9474 max_tinst_depth);
9475 return false;
9478 /* If the current instantiation caused problems, don't let it instantiate
9479 anything else. Do allow deduction substitution and decls usable in
9480 constant expressions. */
9481 if (limit_bad_template_recursion (d))
9482 return false;
9484 /* When not -quiet, dump template instantiations other than functions, since
9485 announce_function will take care of those. */
9486 if (!quiet_flag
9487 && TREE_CODE (d) != TREE_LIST
9488 && TREE_CODE (d) != FUNCTION_DECL)
9489 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9491 new_level = ggc_alloc<tinst_level> ();
9492 new_level->decl = d;
9493 new_level->locus = loc;
9494 new_level->errors = errorcount+sorrycount;
9495 new_level->in_system_header_p = in_system_header_at (input_location);
9496 new_level->next = current_tinst_level;
9497 current_tinst_level = new_level;
9499 ++tinst_depth;
9500 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9501 depth_reached = tinst_depth;
9503 return true;
9506 /* We're done instantiating this template; return to the instantiation
9507 context. */
9509 void
9510 pop_tinst_level (void)
9512 /* Restore the filename and line number stashed away when we started
9513 this instantiation. */
9514 input_location = current_tinst_level->locus;
9515 current_tinst_level = current_tinst_level->next;
9516 --tinst_depth;
9519 /* We're instantiating a deferred template; restore the template
9520 instantiation context in which the instantiation was requested, which
9521 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9523 static tree
9524 reopen_tinst_level (struct tinst_level *level)
9526 struct tinst_level *t;
9528 tinst_depth = 0;
9529 for (t = level; t; t = t->next)
9530 ++tinst_depth;
9532 current_tinst_level = level;
9533 pop_tinst_level ();
9534 if (current_tinst_level)
9535 current_tinst_level->errors = errorcount+sorrycount;
9536 return level->decl;
9539 /* Returns the TINST_LEVEL which gives the original instantiation
9540 context. */
9542 struct tinst_level *
9543 outermost_tinst_level (void)
9545 struct tinst_level *level = current_tinst_level;
9546 if (level)
9547 while (level->next)
9548 level = level->next;
9549 return level;
9552 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9553 vector of template arguments, as for tsubst.
9555 Returns an appropriate tsubst'd friend declaration. */
9557 static tree
9558 tsubst_friend_function (tree decl, tree args)
9560 tree new_friend;
9562 if (TREE_CODE (decl) == FUNCTION_DECL
9563 && DECL_TEMPLATE_INSTANTIATION (decl)
9564 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9565 /* This was a friend declared with an explicit template
9566 argument list, e.g.:
9568 friend void f<>(T);
9570 to indicate that f was a template instantiation, not a new
9571 function declaration. Now, we have to figure out what
9572 instantiation of what template. */
9574 tree template_id, arglist, fns;
9575 tree new_args;
9576 tree tmpl;
9577 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9579 /* Friend functions are looked up in the containing namespace scope.
9580 We must enter that scope, to avoid finding member functions of the
9581 current class with same name. */
9582 push_nested_namespace (ns);
9583 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9584 tf_warning_or_error, NULL_TREE,
9585 /*integral_constant_expression_p=*/false);
9586 pop_nested_namespace (ns);
9587 arglist = tsubst (DECL_TI_ARGS (decl), args,
9588 tf_warning_or_error, NULL_TREE);
9589 template_id = lookup_template_function (fns, arglist);
9591 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9592 tmpl = determine_specialization (template_id, new_friend,
9593 &new_args,
9594 /*need_member_template=*/0,
9595 TREE_VEC_LENGTH (args),
9596 tsk_none);
9597 return instantiate_template (tmpl, new_args, tf_error);
9600 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9602 /* The NEW_FRIEND will look like an instantiation, to the
9603 compiler, but is not an instantiation from the point of view of
9604 the language. For example, we might have had:
9606 template <class T> struct S {
9607 template <class U> friend void f(T, U);
9610 Then, in S<int>, template <class U> void f(int, U) is not an
9611 instantiation of anything. */
9612 if (new_friend == error_mark_node)
9613 return error_mark_node;
9615 DECL_USE_TEMPLATE (new_friend) = 0;
9616 if (TREE_CODE (decl) == TEMPLATE_DECL)
9618 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9619 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9620 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9623 /* The mangled name for the NEW_FRIEND is incorrect. The function
9624 is not a template instantiation and should not be mangled like
9625 one. Therefore, we forget the mangling here; we'll recompute it
9626 later if we need it. */
9627 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9629 SET_DECL_RTL (new_friend, NULL);
9630 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9633 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9635 tree old_decl;
9636 tree new_friend_template_info;
9637 tree new_friend_result_template_info;
9638 tree ns;
9639 int new_friend_is_defn;
9641 /* We must save some information from NEW_FRIEND before calling
9642 duplicate decls since that function will free NEW_FRIEND if
9643 possible. */
9644 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9645 new_friend_is_defn =
9646 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9647 (template_for_substitution (new_friend)))
9648 != NULL_TREE);
9649 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9651 /* This declaration is a `primary' template. */
9652 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9654 new_friend_result_template_info
9655 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9657 else
9658 new_friend_result_template_info = NULL_TREE;
9660 /* Inside pushdecl_namespace_level, we will push into the
9661 current namespace. However, the friend function should go
9662 into the namespace of the template. */
9663 ns = decl_namespace_context (new_friend);
9664 push_nested_namespace (ns);
9665 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9666 pop_nested_namespace (ns);
9668 if (old_decl == error_mark_node)
9669 return error_mark_node;
9671 if (old_decl != new_friend)
9673 /* This new friend declaration matched an existing
9674 declaration. For example, given:
9676 template <class T> void f(T);
9677 template <class U> class C {
9678 template <class T> friend void f(T) {}
9681 the friend declaration actually provides the definition
9682 of `f', once C has been instantiated for some type. So,
9683 old_decl will be the out-of-class template declaration,
9684 while new_friend is the in-class definition.
9686 But, if `f' was called before this point, the
9687 instantiation of `f' will have DECL_TI_ARGS corresponding
9688 to `T' but not to `U', references to which might appear
9689 in the definition of `f'. Previously, the most general
9690 template for an instantiation of `f' was the out-of-class
9691 version; now it is the in-class version. Therefore, we
9692 run through all specialization of `f', adding to their
9693 DECL_TI_ARGS appropriately. In particular, they need a
9694 new set of outer arguments, corresponding to the
9695 arguments for this class instantiation.
9697 The same situation can arise with something like this:
9699 friend void f(int);
9700 template <class T> class C {
9701 friend void f(T) {}
9704 when `C<int>' is instantiated. Now, `f(int)' is defined
9705 in the class. */
9707 if (!new_friend_is_defn)
9708 /* On the other hand, if the in-class declaration does
9709 *not* provide a definition, then we don't want to alter
9710 existing definitions. We can just leave everything
9711 alone. */
9713 else
9715 tree new_template = TI_TEMPLATE (new_friend_template_info);
9716 tree new_args = TI_ARGS (new_friend_template_info);
9718 /* Overwrite whatever template info was there before, if
9719 any, with the new template information pertaining to
9720 the declaration. */
9721 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9723 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9725 /* We should have called reregister_specialization in
9726 duplicate_decls. */
9727 gcc_assert (retrieve_specialization (new_template,
9728 new_args, 0)
9729 == old_decl);
9731 /* Instantiate it if the global has already been used. */
9732 if (DECL_ODR_USED (old_decl))
9733 instantiate_decl (old_decl, /*defer_ok=*/true,
9734 /*expl_inst_class_mem_p=*/false);
9736 else
9738 tree t;
9740 /* Indicate that the old function template is a partial
9741 instantiation. */
9742 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9743 = new_friend_result_template_info;
9745 gcc_assert (new_template
9746 == most_general_template (new_template));
9747 gcc_assert (new_template != old_decl);
9749 /* Reassign any specializations already in the hash table
9750 to the new more general template, and add the
9751 additional template args. */
9752 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9753 t != NULL_TREE;
9754 t = TREE_CHAIN (t))
9756 tree spec = TREE_VALUE (t);
9757 spec_entry elt;
9759 elt.tmpl = old_decl;
9760 elt.args = DECL_TI_ARGS (spec);
9761 elt.spec = NULL_TREE;
9763 decl_specializations->remove_elt (&elt);
9765 DECL_TI_ARGS (spec)
9766 = add_outermost_template_args (new_args,
9767 DECL_TI_ARGS (spec));
9769 register_specialization
9770 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9773 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9777 /* The information from NEW_FRIEND has been merged into OLD_DECL
9778 by duplicate_decls. */
9779 new_friend = old_decl;
9782 else
9784 tree context = DECL_CONTEXT (new_friend);
9785 bool dependent_p;
9787 /* In the code
9788 template <class T> class C {
9789 template <class U> friend void C1<U>::f (); // case 1
9790 friend void C2<T>::f (); // case 2
9792 we only need to make sure CONTEXT is a complete type for
9793 case 2. To distinguish between the two cases, we note that
9794 CONTEXT of case 1 remains dependent type after tsubst while
9795 this isn't true for case 2. */
9796 ++processing_template_decl;
9797 dependent_p = dependent_type_p (context);
9798 --processing_template_decl;
9800 if (!dependent_p
9801 && !complete_type_or_else (context, NULL_TREE))
9802 return error_mark_node;
9804 if (COMPLETE_TYPE_P (context))
9806 tree fn = new_friend;
9807 /* do_friend adds the TEMPLATE_DECL for any member friend
9808 template even if it isn't a member template, i.e.
9809 template <class T> friend A<T>::f();
9810 Look through it in that case. */
9811 if (TREE_CODE (fn) == TEMPLATE_DECL
9812 && !PRIMARY_TEMPLATE_P (fn))
9813 fn = DECL_TEMPLATE_RESULT (fn);
9814 /* Check to see that the declaration is really present, and,
9815 possibly obtain an improved declaration. */
9816 fn = check_classfn (context, fn, NULL_TREE);
9818 if (fn)
9819 new_friend = fn;
9823 return new_friend;
9826 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9827 template arguments, as for tsubst.
9829 Returns an appropriate tsubst'd friend type or error_mark_node on
9830 failure. */
9832 static tree
9833 tsubst_friend_class (tree friend_tmpl, tree args)
9835 tree friend_type;
9836 tree tmpl;
9837 tree context;
9839 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9841 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9842 return TREE_TYPE (t);
9845 context = CP_DECL_CONTEXT (friend_tmpl);
9847 if (context != global_namespace)
9849 if (TREE_CODE (context) == NAMESPACE_DECL)
9850 push_nested_namespace (context);
9851 else
9852 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9855 /* Look for a class template declaration. We look for hidden names
9856 because two friend declarations of the same template are the
9857 same. For example, in:
9859 struct A {
9860 template <typename> friend class F;
9862 template <typename> struct B {
9863 template <typename> friend class F;
9866 both F templates are the same. */
9867 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9868 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9870 /* But, if we don't find one, it might be because we're in a
9871 situation like this:
9873 template <class T>
9874 struct S {
9875 template <class U>
9876 friend struct S;
9879 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9880 for `S<int>', not the TEMPLATE_DECL. */
9881 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9883 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9884 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9887 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9889 /* The friend template has already been declared. Just
9890 check to see that the declarations match, and install any new
9891 default parameters. We must tsubst the default parameters,
9892 of course. We only need the innermost template parameters
9893 because that is all that redeclare_class_template will look
9894 at. */
9895 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9896 > TMPL_ARGS_DEPTH (args))
9898 tree parms;
9899 location_t saved_input_location;
9900 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9901 args, tf_warning_or_error);
9903 saved_input_location = input_location;
9904 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9905 tree cons = get_constraints (tmpl);
9906 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9907 input_location = saved_input_location;
9911 friend_type = TREE_TYPE (tmpl);
9913 else
9915 /* The friend template has not already been declared. In this
9916 case, the instantiation of the template class will cause the
9917 injection of this template into the global scope. */
9918 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9919 if (tmpl == error_mark_node)
9920 return error_mark_node;
9922 /* The new TMPL is not an instantiation of anything, so we
9923 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9924 the new type because that is supposed to be the corresponding
9925 template decl, i.e., TMPL. */
9926 DECL_USE_TEMPLATE (tmpl) = 0;
9927 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9928 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9929 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9930 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9932 /* Inject this template into the global scope. */
9933 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
9936 if (context != global_namespace)
9938 if (TREE_CODE (context) == NAMESPACE_DECL)
9939 pop_nested_namespace (context);
9940 else
9941 pop_nested_class ();
9944 return friend_type;
9947 /* Returns zero if TYPE cannot be completed later due to circularity.
9948 Otherwise returns one. */
9950 static int
9951 can_complete_type_without_circularity (tree type)
9953 if (type == NULL_TREE || type == error_mark_node)
9954 return 0;
9955 else if (COMPLETE_TYPE_P (type))
9956 return 1;
9957 else if (TREE_CODE (type) == ARRAY_TYPE)
9958 return can_complete_type_without_circularity (TREE_TYPE (type));
9959 else if (CLASS_TYPE_P (type)
9960 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9961 return 0;
9962 else
9963 return 1;
9966 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9967 tsubst_flags_t, tree);
9969 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9970 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9972 static tree
9973 tsubst_attribute (tree t, tree *decl_p, tree args,
9974 tsubst_flags_t complain, tree in_decl)
9976 gcc_assert (ATTR_IS_DEPENDENT (t));
9978 tree val = TREE_VALUE (t);
9979 if (val == NULL_TREE)
9980 /* Nothing to do. */;
9981 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9982 && is_attribute_p ("omp declare simd",
9983 get_attribute_name (t)))
9985 tree clauses = TREE_VALUE (val);
9986 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
9987 complain, in_decl);
9988 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9989 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
9990 tree parms = DECL_ARGUMENTS (*decl_p);
9991 clauses
9992 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9993 if (clauses)
9994 val = build_tree_list (NULL_TREE, clauses);
9995 else
9996 val = NULL_TREE;
9998 /* If the first attribute argument is an identifier, don't
9999 pass it through tsubst. Attributes like mode, format,
10000 cleanup and several target specific attributes expect it
10001 unmodified. */
10002 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10004 tree chain
10005 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10006 /*integral_constant_expression_p=*/false);
10007 if (chain != TREE_CHAIN (val))
10008 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10010 else if (PACK_EXPANSION_P (val))
10012 /* An attribute pack expansion. */
10013 tree purp = TREE_PURPOSE (t);
10014 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10015 int len = TREE_VEC_LENGTH (pack);
10016 tree list = NULL_TREE;
10017 tree *q = &list;
10018 for (int i = 0; i < len; ++i)
10020 tree elt = TREE_VEC_ELT (pack, i);
10021 *q = build_tree_list (purp, elt);
10022 q = &TREE_CHAIN (*q);
10024 return list;
10026 else
10027 val = tsubst_expr (val, args, complain, in_decl,
10028 /*integral_constant_expression_p=*/false);
10030 if (val != TREE_VALUE (t))
10031 return build_tree_list (TREE_PURPOSE (t), val);
10032 return t;
10035 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10036 unchanged or a new TREE_LIST chain. */
10038 static tree
10039 tsubst_attributes (tree attributes, tree args,
10040 tsubst_flags_t complain, tree in_decl)
10042 tree last_dep = NULL_TREE;
10044 for (tree t = attributes; t; t = TREE_CHAIN (t))
10045 if (ATTR_IS_DEPENDENT (t))
10047 last_dep = t;
10048 attributes = copy_list (attributes);
10049 break;
10052 if (last_dep)
10053 for (tree *p = &attributes; *p; )
10055 tree t = *p;
10056 if (ATTR_IS_DEPENDENT (t))
10058 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10059 if (subst != t)
10061 *p = subst;
10063 p = &TREE_CHAIN (*p);
10064 while (*p);
10065 *p = TREE_CHAIN (t);
10066 continue;
10069 p = &TREE_CHAIN (*p);
10072 return attributes;
10075 /* Apply any attributes which had to be deferred until instantiation
10076 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10077 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10079 static void
10080 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10081 tree args, tsubst_flags_t complain, tree in_decl)
10083 tree last_dep = NULL_TREE;
10084 tree t;
10085 tree *p;
10087 if (attributes == NULL_TREE)
10088 return;
10090 if (DECL_P (*decl_p))
10092 if (TREE_TYPE (*decl_p) == error_mark_node)
10093 return;
10094 p = &DECL_ATTRIBUTES (*decl_p);
10095 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10096 to our attributes parameter. */
10097 gcc_assert (*p == attributes);
10099 else
10101 p = &TYPE_ATTRIBUTES (*decl_p);
10102 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10103 lookup_template_class_1, and should be preserved. */
10104 gcc_assert (*p != attributes);
10105 while (*p)
10106 p = &TREE_CHAIN (*p);
10109 for (t = attributes; t; t = TREE_CHAIN (t))
10110 if (ATTR_IS_DEPENDENT (t))
10112 last_dep = t;
10113 attributes = copy_list (attributes);
10114 break;
10117 *p = attributes;
10118 if (last_dep)
10120 tree late_attrs = NULL_TREE;
10121 tree *q = &late_attrs;
10123 for (; *p; )
10125 t = *p;
10126 if (ATTR_IS_DEPENDENT (t))
10128 *p = TREE_CHAIN (t);
10129 TREE_CHAIN (t) = NULL_TREE;
10130 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10132 q = &TREE_CHAIN (*q);
10133 while (*q);
10135 else
10136 p = &TREE_CHAIN (t);
10139 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10143 /* Perform (or defer) access check for typedefs that were referenced
10144 from within the template TMPL code.
10145 This is a subroutine of instantiate_decl and instantiate_class_template.
10146 TMPL is the template to consider and TARGS is the list of arguments of
10147 that template. */
10149 static void
10150 perform_typedefs_access_check (tree tmpl, tree targs)
10152 location_t saved_location;
10153 unsigned i;
10154 qualified_typedef_usage_t *iter;
10156 if (!tmpl
10157 || (!CLASS_TYPE_P (tmpl)
10158 && TREE_CODE (tmpl) != FUNCTION_DECL))
10159 return;
10161 saved_location = input_location;
10162 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10164 tree type_decl = iter->typedef_decl;
10165 tree type_scope = iter->context;
10167 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10168 continue;
10170 if (uses_template_parms (type_decl))
10171 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10172 if (uses_template_parms (type_scope))
10173 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10175 /* Make access check error messages point to the location
10176 of the use of the typedef. */
10177 input_location = iter->locus;
10178 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10179 type_decl, type_decl,
10180 tf_warning_or_error);
10182 input_location = saved_location;
10185 static tree
10186 instantiate_class_template_1 (tree type)
10188 tree templ, args, pattern, t, member;
10189 tree typedecl;
10190 tree pbinfo;
10191 tree base_list;
10192 unsigned int saved_maximum_field_alignment;
10193 tree fn_context;
10195 if (type == error_mark_node)
10196 return error_mark_node;
10198 if (COMPLETE_OR_OPEN_TYPE_P (type)
10199 || uses_template_parms (type))
10200 return type;
10202 /* Figure out which template is being instantiated. */
10203 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10204 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10206 /* Determine what specialization of the original template to
10207 instantiate. */
10208 t = most_specialized_partial_spec (type, tf_warning_or_error);
10209 if (t == error_mark_node)
10211 TYPE_BEING_DEFINED (type) = 1;
10212 return error_mark_node;
10214 else if (t)
10216 /* This TYPE is actually an instantiation of a partial
10217 specialization. We replace the innermost set of ARGS with
10218 the arguments appropriate for substitution. For example,
10219 given:
10221 template <class T> struct S {};
10222 template <class T> struct S<T*> {};
10224 and supposing that we are instantiating S<int*>, ARGS will
10225 presently be {int*} -- but we need {int}. */
10226 pattern = TREE_TYPE (t);
10227 args = TREE_PURPOSE (t);
10229 else
10231 pattern = TREE_TYPE (templ);
10232 args = CLASSTYPE_TI_ARGS (type);
10235 /* If the template we're instantiating is incomplete, then clearly
10236 there's nothing we can do. */
10237 if (!COMPLETE_TYPE_P (pattern))
10238 return type;
10240 /* If we've recursively instantiated too many templates, stop. */
10241 if (! push_tinst_level (type))
10242 return type;
10244 /* Now we're really doing the instantiation. Mark the type as in
10245 the process of being defined. */
10246 TYPE_BEING_DEFINED (type) = 1;
10248 /* We may be in the middle of deferred access check. Disable
10249 it now. */
10250 push_deferring_access_checks (dk_no_deferred);
10252 int saved_unevaluated_operand = cp_unevaluated_operand;
10253 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10255 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10256 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10257 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10258 fn_context = error_mark_node;
10259 if (!fn_context)
10260 push_to_top_level ();
10261 else
10263 cp_unevaluated_operand = 0;
10264 c_inhibit_evaluation_warnings = 0;
10266 /* Use #pragma pack from the template context. */
10267 saved_maximum_field_alignment = maximum_field_alignment;
10268 maximum_field_alignment = TYPE_PRECISION (pattern);
10270 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10272 /* Set the input location to the most specialized template definition.
10273 This is needed if tsubsting causes an error. */
10274 typedecl = TYPE_MAIN_DECL (pattern);
10275 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10276 DECL_SOURCE_LOCATION (typedecl);
10278 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10279 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10280 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10281 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10282 if (ANON_AGGR_TYPE_P (pattern))
10283 SET_ANON_AGGR_TYPE_P (type);
10284 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10286 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10287 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10288 /* Adjust visibility for template arguments. */
10289 determine_visibility (TYPE_MAIN_DECL (type));
10291 if (CLASS_TYPE_P (type))
10292 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10294 pbinfo = TYPE_BINFO (pattern);
10296 /* We should never instantiate a nested class before its enclosing
10297 class; we need to look up the nested class by name before we can
10298 instantiate it, and that lookup should instantiate the enclosing
10299 class. */
10300 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10301 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10303 base_list = NULL_TREE;
10304 if (BINFO_N_BASE_BINFOS (pbinfo))
10306 tree pbase_binfo;
10307 tree pushed_scope;
10308 int i;
10310 /* We must enter the scope containing the type, as that is where
10311 the accessibility of types named in dependent bases are
10312 looked up from. */
10313 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10315 /* Substitute into each of the bases to determine the actual
10316 basetypes. */
10317 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10319 tree base;
10320 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10321 tree expanded_bases = NULL_TREE;
10322 int idx, len = 1;
10324 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10326 expanded_bases =
10327 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10328 args, tf_error, NULL_TREE);
10329 if (expanded_bases == error_mark_node)
10330 continue;
10332 len = TREE_VEC_LENGTH (expanded_bases);
10335 for (idx = 0; idx < len; idx++)
10337 if (expanded_bases)
10338 /* Extract the already-expanded base class. */
10339 base = TREE_VEC_ELT (expanded_bases, idx);
10340 else
10341 /* Substitute to figure out the base class. */
10342 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10343 NULL_TREE);
10345 if (base == error_mark_node)
10346 continue;
10348 base_list = tree_cons (access, base, base_list);
10349 if (BINFO_VIRTUAL_P (pbase_binfo))
10350 TREE_TYPE (base_list) = integer_type_node;
10354 /* The list is now in reverse order; correct that. */
10355 base_list = nreverse (base_list);
10357 if (pushed_scope)
10358 pop_scope (pushed_scope);
10360 /* Now call xref_basetypes to set up all the base-class
10361 information. */
10362 xref_basetypes (type, base_list);
10364 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10365 (int) ATTR_FLAG_TYPE_IN_PLACE,
10366 args, tf_error, NULL_TREE);
10367 fixup_attribute_variants (type);
10369 /* Now that our base classes are set up, enter the scope of the
10370 class, so that name lookups into base classes, etc. will work
10371 correctly. This is precisely analogous to what we do in
10372 begin_class_definition when defining an ordinary non-template
10373 class, except we also need to push the enclosing classes. */
10374 push_nested_class (type);
10376 /* Now members are processed in the order of declaration. */
10377 for (member = CLASSTYPE_DECL_LIST (pattern);
10378 member; member = TREE_CHAIN (member))
10380 tree t = TREE_VALUE (member);
10382 if (TREE_PURPOSE (member))
10384 if (TYPE_P (t))
10386 /* Build new CLASSTYPE_NESTED_UTDS. */
10388 tree newtag;
10389 bool class_template_p;
10391 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10392 && TYPE_LANG_SPECIFIC (t)
10393 && CLASSTYPE_IS_TEMPLATE (t));
10394 /* If the member is a class template, then -- even after
10395 substitution -- there may be dependent types in the
10396 template argument list for the class. We increment
10397 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10398 that function will assume that no types are dependent
10399 when outside of a template. */
10400 if (class_template_p)
10401 ++processing_template_decl;
10402 newtag = tsubst (t, args, tf_error, NULL_TREE);
10403 if (class_template_p)
10404 --processing_template_decl;
10405 if (newtag == error_mark_node)
10406 continue;
10408 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10410 tree name = TYPE_IDENTIFIER (t);
10412 if (class_template_p)
10413 /* Unfortunately, lookup_template_class sets
10414 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10415 instantiation (i.e., for the type of a member
10416 template class nested within a template class.)
10417 This behavior is required for
10418 maybe_process_partial_specialization to work
10419 correctly, but is not accurate in this case;
10420 the TAG is not an instantiation of anything.
10421 (The corresponding TEMPLATE_DECL is an
10422 instantiation, but the TYPE is not.) */
10423 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10425 /* Now, we call pushtag to put this NEWTAG into the scope of
10426 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10427 pushtag calling push_template_decl. We don't have to do
10428 this for enums because it will already have been done in
10429 tsubst_enum. */
10430 if (name)
10431 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10432 pushtag (name, newtag, /*tag_scope=*/ts_current);
10435 else if (DECL_DECLARES_FUNCTION_P (t))
10437 /* Build new TYPE_METHODS. */
10438 tree r;
10440 if (TREE_CODE (t) == TEMPLATE_DECL)
10441 ++processing_template_decl;
10442 r = tsubst (t, args, tf_error, NULL_TREE);
10443 if (TREE_CODE (t) == TEMPLATE_DECL)
10444 --processing_template_decl;
10445 set_current_access_from_decl (r);
10446 finish_member_declaration (r);
10447 /* Instantiate members marked with attribute used. */
10448 if (r != error_mark_node && DECL_PRESERVE_P (r))
10449 mark_used (r);
10450 if (TREE_CODE (r) == FUNCTION_DECL
10451 && DECL_OMP_DECLARE_REDUCTION_P (r))
10452 cp_check_omp_declare_reduction (r);
10454 else if (DECL_CLASS_TEMPLATE_P (t)
10455 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10456 /* A closure type for a lambda in a default argument for a
10457 member template. Ignore it; it will be instantiated with
10458 the default argument. */;
10459 else
10461 /* Build new TYPE_FIELDS. */
10462 if (TREE_CODE (t) == STATIC_ASSERT)
10464 tree condition;
10466 ++c_inhibit_evaluation_warnings;
10467 condition =
10468 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10469 tf_warning_or_error, NULL_TREE,
10470 /*integral_constant_expression_p=*/true);
10471 --c_inhibit_evaluation_warnings;
10473 finish_static_assert (condition,
10474 STATIC_ASSERT_MESSAGE (t),
10475 STATIC_ASSERT_SOURCE_LOCATION (t),
10476 /*member_p=*/true);
10478 else if (TREE_CODE (t) != CONST_DECL)
10480 tree r;
10481 tree vec = NULL_TREE;
10482 int len = 1;
10484 /* The file and line for this declaration, to
10485 assist in error message reporting. Since we
10486 called push_tinst_level above, we don't need to
10487 restore these. */
10488 input_location = DECL_SOURCE_LOCATION (t);
10490 if (TREE_CODE (t) == TEMPLATE_DECL)
10491 ++processing_template_decl;
10492 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10493 if (TREE_CODE (t) == TEMPLATE_DECL)
10494 --processing_template_decl;
10496 if (TREE_CODE (r) == TREE_VEC)
10498 /* A capture pack became multiple fields. */
10499 vec = r;
10500 len = TREE_VEC_LENGTH (vec);
10503 for (int i = 0; i < len; ++i)
10505 if (vec)
10506 r = TREE_VEC_ELT (vec, i);
10507 if (VAR_P (r))
10509 /* In [temp.inst]:
10511 [t]he initialization (and any associated
10512 side-effects) of a static data member does
10513 not occur unless the static data member is
10514 itself used in a way that requires the
10515 definition of the static data member to
10516 exist.
10518 Therefore, we do not substitute into the
10519 initialized for the static data member here. */
10520 finish_static_data_member_decl
10522 /*init=*/NULL_TREE,
10523 /*init_const_expr_p=*/false,
10524 /*asmspec_tree=*/NULL_TREE,
10525 /*flags=*/0);
10526 /* Instantiate members marked with attribute used. */
10527 if (r != error_mark_node && DECL_PRESERVE_P (r))
10528 mark_used (r);
10530 else if (TREE_CODE (r) == FIELD_DECL)
10532 /* Determine whether R has a valid type and can be
10533 completed later. If R is invalid, then its type
10534 is replaced by error_mark_node. */
10535 tree rtype = TREE_TYPE (r);
10536 if (can_complete_type_without_circularity (rtype))
10537 complete_type (rtype);
10539 if (!complete_or_array_type_p (rtype))
10541 /* If R's type couldn't be completed and
10542 it isn't a flexible array member (whose
10543 type is incomplete by definition) give
10544 an error. */
10545 cxx_incomplete_type_error (r, rtype);
10546 TREE_TYPE (r) = error_mark_node;
10550 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10551 such a thing will already have been added to the field
10552 list by tsubst_enum in finish_member_declaration in the
10553 CLASSTYPE_NESTED_UTDS case above. */
10554 if (!(TREE_CODE (r) == TYPE_DECL
10555 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10556 && DECL_ARTIFICIAL (r)))
10558 set_current_access_from_decl (r);
10559 finish_member_declaration (r);
10565 else
10567 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10568 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10570 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10572 tree friend_type = t;
10573 bool adjust_processing_template_decl = false;
10575 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10577 /* template <class T> friend class C; */
10578 friend_type = tsubst_friend_class (friend_type, args);
10579 adjust_processing_template_decl = true;
10581 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10583 /* template <class T> friend class C::D; */
10584 friend_type = tsubst (friend_type, args,
10585 tf_warning_or_error, NULL_TREE);
10586 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10587 friend_type = TREE_TYPE (friend_type);
10588 adjust_processing_template_decl = true;
10590 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10591 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10593 /* This could be either
10595 friend class T::C;
10597 when dependent_type_p is false or
10599 template <class U> friend class T::C;
10601 otherwise. */
10602 /* Bump processing_template_decl in case this is something like
10603 template <class T> friend struct A<T>::B. */
10604 ++processing_template_decl;
10605 friend_type = tsubst (friend_type, args,
10606 tf_warning_or_error, NULL_TREE);
10607 if (dependent_type_p (friend_type))
10608 adjust_processing_template_decl = true;
10609 --processing_template_decl;
10611 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10612 && hidden_name_p (TYPE_NAME (friend_type)))
10614 /* friend class C;
10616 where C hasn't been declared yet. Let's lookup name
10617 from namespace scope directly, bypassing any name that
10618 come from dependent base class. */
10619 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10621 /* The call to xref_tag_from_type does injection for friend
10622 classes. */
10623 push_nested_namespace (ns);
10624 friend_type =
10625 xref_tag_from_type (friend_type, NULL_TREE,
10626 /*tag_scope=*/ts_current);
10627 pop_nested_namespace (ns);
10629 else if (uses_template_parms (friend_type))
10630 /* friend class C<T>; */
10631 friend_type = tsubst (friend_type, args,
10632 tf_warning_or_error, NULL_TREE);
10633 /* Otherwise it's
10635 friend class C;
10637 where C is already declared or
10639 friend class C<int>;
10641 We don't have to do anything in these cases. */
10643 if (adjust_processing_template_decl)
10644 /* Trick make_friend_class into realizing that the friend
10645 we're adding is a template, not an ordinary class. It's
10646 important that we use make_friend_class since it will
10647 perform some error-checking and output cross-reference
10648 information. */
10649 ++processing_template_decl;
10651 if (friend_type != error_mark_node)
10652 make_friend_class (type, friend_type, /*complain=*/false);
10654 if (adjust_processing_template_decl)
10655 --processing_template_decl;
10657 else
10659 /* Build new DECL_FRIENDLIST. */
10660 tree r;
10662 /* The file and line for this declaration, to
10663 assist in error message reporting. Since we
10664 called push_tinst_level above, we don't need to
10665 restore these. */
10666 input_location = DECL_SOURCE_LOCATION (t);
10668 if (TREE_CODE (t) == TEMPLATE_DECL)
10670 ++processing_template_decl;
10671 push_deferring_access_checks (dk_no_check);
10674 r = tsubst_friend_function (t, args);
10675 add_friend (type, r, /*complain=*/false);
10676 if (TREE_CODE (t) == TEMPLATE_DECL)
10678 pop_deferring_access_checks ();
10679 --processing_template_decl;
10685 if (fn_context)
10687 /* Restore these before substituting into the lambda capture
10688 initializers. */
10689 cp_unevaluated_operand = saved_unevaluated_operand;
10690 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10693 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10695 tree decl = lambda_function (type);
10696 if (decl)
10698 if (cxx_dialect >= cxx1z)
10699 CLASSTYPE_LITERAL_P (type) = true;
10701 if (!DECL_TEMPLATE_INFO (decl)
10702 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10704 /* Set function_depth to avoid garbage collection. */
10705 ++function_depth;
10706 instantiate_decl (decl, /*defer_ok=*/false, false);
10707 --function_depth;
10710 /* We need to instantiate the capture list from the template
10711 after we've instantiated the closure members, but before we
10712 consider adding the conversion op. Also keep any captures
10713 that may have been added during instantiation of the op(). */
10714 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10715 tree tmpl_cap
10716 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10717 args, tf_warning_or_error, NULL_TREE,
10718 false, false);
10720 LAMBDA_EXPR_CAPTURE_LIST (expr)
10721 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10723 maybe_add_lambda_conv_op (type);
10725 else
10726 gcc_assert (errorcount);
10729 /* Set the file and line number information to whatever is given for
10730 the class itself. This puts error messages involving generated
10731 implicit functions at a predictable point, and the same point
10732 that would be used for non-template classes. */
10733 input_location = DECL_SOURCE_LOCATION (typedecl);
10735 unreverse_member_declarations (type);
10736 finish_struct_1 (type);
10737 TYPE_BEING_DEFINED (type) = 0;
10739 /* We don't instantiate default arguments for member functions. 14.7.1:
10741 The implicit instantiation of a class template specialization causes
10742 the implicit instantiation of the declarations, but not of the
10743 definitions or default arguments, of the class member functions,
10744 member classes, static data members and member templates.... */
10746 /* Some typedefs referenced from within the template code need to be access
10747 checked at template instantiation time, i.e now. These types were
10748 added to the template at parsing time. Let's get those and perform
10749 the access checks then. */
10750 perform_typedefs_access_check (pattern, args);
10751 perform_deferred_access_checks (tf_warning_or_error);
10752 pop_nested_class ();
10753 maximum_field_alignment = saved_maximum_field_alignment;
10754 if (!fn_context)
10755 pop_from_top_level ();
10756 pop_deferring_access_checks ();
10757 pop_tinst_level ();
10759 /* The vtable for a template class can be emitted in any translation
10760 unit in which the class is instantiated. When there is no key
10761 method, however, finish_struct_1 will already have added TYPE to
10762 the keyed_classes list. */
10763 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10764 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10766 return type;
10769 /* Wrapper for instantiate_class_template_1. */
10771 tree
10772 instantiate_class_template (tree type)
10774 tree ret;
10775 timevar_push (TV_TEMPLATE_INST);
10776 ret = instantiate_class_template_1 (type);
10777 timevar_pop (TV_TEMPLATE_INST);
10778 return ret;
10781 static tree
10782 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10784 tree r;
10786 if (!t)
10787 r = t;
10788 else if (TYPE_P (t))
10789 r = tsubst (t, args, complain, in_decl);
10790 else
10792 if (!(complain & tf_warning))
10793 ++c_inhibit_evaluation_warnings;
10794 r = tsubst_expr (t, args, complain, in_decl,
10795 /*integral_constant_expression_p=*/true);
10796 if (!(complain & tf_warning))
10797 --c_inhibit_evaluation_warnings;
10799 return r;
10802 /* Given a function parameter pack TMPL_PARM and some function parameters
10803 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10804 and set *SPEC_P to point at the next point in the list. */
10806 tree
10807 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10809 /* Collect all of the extra "packed" parameters into an
10810 argument pack. */
10811 tree parmvec;
10812 tree parmtypevec;
10813 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10814 tree argtypepack = cxx_make_type (TYPE_ARGUMENT_PACK);
10815 tree spec_parm = *spec_p;
10816 int i, len;
10818 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10819 if (tmpl_parm
10820 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10821 break;
10823 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10824 parmvec = make_tree_vec (len);
10825 parmtypevec = make_tree_vec (len);
10826 spec_parm = *spec_p;
10827 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10829 TREE_VEC_ELT (parmvec, i) = spec_parm;
10830 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
10833 /* Build the argument packs. */
10834 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10835 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
10836 TREE_TYPE (argpack) = argtypepack;
10837 *spec_p = spec_parm;
10839 return argpack;
10842 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10843 NONTYPE_ARGUMENT_PACK. */
10845 static tree
10846 make_fnparm_pack (tree spec_parm)
10848 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10851 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10852 pack expansion with no extra args, 2 if it has extra args, or 0
10853 if it is not a pack expansion. */
10855 static int
10856 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10858 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10859 if (i >= TREE_VEC_LENGTH (vec))
10860 return 0;
10861 tree elt = TREE_VEC_ELT (vec, i);
10862 if (DECL_P (elt))
10863 /* A decl pack is itself an expansion. */
10864 elt = TREE_TYPE (elt);
10865 if (!PACK_EXPANSION_P (elt))
10866 return 0;
10867 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10868 return 2;
10869 return 1;
10873 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10875 static tree
10876 make_argument_pack_select (tree arg_pack, unsigned index)
10878 tree aps = make_node (ARGUMENT_PACK_SELECT);
10880 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10881 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10883 return aps;
10886 /* This is a subroutine of tsubst_pack_expansion.
10888 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10889 mechanism to store the (non complete list of) arguments of the
10890 substitution and return a non substituted pack expansion, in order
10891 to wait for when we have enough arguments to really perform the
10892 substitution. */
10894 static bool
10895 use_pack_expansion_extra_args_p (tree parm_packs,
10896 int arg_pack_len,
10897 bool has_empty_arg)
10899 /* If one pack has an expansion and another pack has a normal
10900 argument or if one pack has an empty argument and an another
10901 one hasn't then tsubst_pack_expansion cannot perform the
10902 substitution and need to fall back on the
10903 PACK_EXPANSION_EXTRA mechanism. */
10904 if (parm_packs == NULL_TREE)
10905 return false;
10906 else if (has_empty_arg)
10907 return true;
10909 bool has_expansion_arg = false;
10910 for (int i = 0 ; i < arg_pack_len; ++i)
10912 bool has_non_expansion_arg = false;
10913 for (tree parm_pack = parm_packs;
10914 parm_pack;
10915 parm_pack = TREE_CHAIN (parm_pack))
10917 tree arg = TREE_VALUE (parm_pack);
10919 int exp = argument_pack_element_is_expansion_p (arg, i);
10920 if (exp == 2)
10921 /* We can't substitute a pack expansion with extra args into
10922 our pattern. */
10923 return true;
10924 else if (exp)
10925 has_expansion_arg = true;
10926 else
10927 has_non_expansion_arg = true;
10930 if (has_expansion_arg && has_non_expansion_arg)
10931 return true;
10933 return false;
10936 /* [temp.variadic]/6 says that:
10938 The instantiation of a pack expansion [...]
10939 produces a list E1,E2, ..., En, where N is the number of elements
10940 in the pack expansion parameters.
10942 This subroutine of tsubst_pack_expansion produces one of these Ei.
10944 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10945 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10946 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10947 INDEX is the index 'i' of the element Ei to produce. ARGS,
10948 COMPLAIN, and IN_DECL are the same parameters as for the
10949 tsubst_pack_expansion function.
10951 The function returns the resulting Ei upon successful completion,
10952 or error_mark_node.
10954 Note that this function possibly modifies the ARGS parameter, so
10955 it's the responsibility of the caller to restore it. */
10957 static tree
10958 gen_elem_of_pack_expansion_instantiation (tree pattern,
10959 tree parm_packs,
10960 unsigned index,
10961 tree args /* This parm gets
10962 modified. */,
10963 tsubst_flags_t complain,
10964 tree in_decl)
10966 tree t;
10967 bool ith_elem_is_expansion = false;
10969 /* For each parameter pack, change the substitution of the parameter
10970 pack to the ith argument in its argument pack, then expand the
10971 pattern. */
10972 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10974 tree parm = TREE_PURPOSE (pack);
10975 tree arg_pack = TREE_VALUE (pack);
10976 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10978 ith_elem_is_expansion |=
10979 argument_pack_element_is_expansion_p (arg_pack, index);
10981 /* Select the Ith argument from the pack. */
10982 if (TREE_CODE (parm) == PARM_DECL
10983 || TREE_CODE (parm) == FIELD_DECL)
10985 if (index == 0)
10987 aps = make_argument_pack_select (arg_pack, index);
10988 if (!mark_used (parm, complain) && !(complain & tf_error))
10989 return error_mark_node;
10990 register_local_specialization (aps, parm);
10992 else
10993 aps = retrieve_local_specialization (parm);
10995 else
10997 int idx, level;
10998 template_parm_level_and_index (parm, &level, &idx);
11000 if (index == 0)
11002 aps = make_argument_pack_select (arg_pack, index);
11003 /* Update the corresponding argument. */
11004 TMPL_ARG (args, level, idx) = aps;
11006 else
11007 /* Re-use the ARGUMENT_PACK_SELECT. */
11008 aps = TMPL_ARG (args, level, idx);
11010 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11013 /* Substitute into the PATTERN with the (possibly altered)
11014 arguments. */
11015 if (pattern == in_decl)
11016 /* Expanding a fixed parameter pack from
11017 coerce_template_parameter_pack. */
11018 t = tsubst_decl (pattern, args, complain);
11019 else if (pattern == error_mark_node)
11020 t = error_mark_node;
11021 else if (constraint_p (pattern))
11023 if (processing_template_decl)
11024 t = tsubst_constraint (pattern, args, complain, in_decl);
11025 else
11026 t = (constraints_satisfied_p (pattern, args)
11027 ? boolean_true_node : boolean_false_node);
11029 else if (!TYPE_P (pattern))
11030 t = tsubst_expr (pattern, args, complain, in_decl,
11031 /*integral_constant_expression_p=*/false);
11032 else
11033 t = tsubst (pattern, args, complain, in_decl);
11035 /* If the Ith argument pack element is a pack expansion, then
11036 the Ith element resulting from the substituting is going to
11037 be a pack expansion as well. */
11038 if (ith_elem_is_expansion)
11039 t = make_pack_expansion (t);
11041 return t;
11044 /* When the unexpanded parameter pack in a fold expression expands to an empty
11045 sequence, the value of the expression is as follows; the program is
11046 ill-formed if the operator is not listed in this table.
11048 && true
11049 || false
11050 , void() */
11052 tree
11053 expand_empty_fold (tree t, tsubst_flags_t complain)
11055 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11056 if (!FOLD_EXPR_MODIFY_P (t))
11057 switch (code)
11059 case TRUTH_ANDIF_EXPR:
11060 return boolean_true_node;
11061 case TRUTH_ORIF_EXPR:
11062 return boolean_false_node;
11063 case COMPOUND_EXPR:
11064 return void_node;
11065 default:
11066 break;
11069 if (complain & tf_error)
11070 error_at (location_of (t),
11071 "fold of empty expansion over %O", code);
11072 return error_mark_node;
11075 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11076 form an expression that combines the two terms using the
11077 operator of T. */
11079 static tree
11080 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11082 tree op = FOLD_EXPR_OP (t);
11083 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11085 // Handle compound assignment operators.
11086 if (FOLD_EXPR_MODIFY_P (t))
11087 return build_x_modify_expr (input_location, left, code, right, complain);
11089 switch (code)
11091 case COMPOUND_EXPR:
11092 return build_x_compound_expr (input_location, left, right, complain);
11093 case DOTSTAR_EXPR:
11094 return build_m_component_ref (left, right, complain);
11095 default:
11096 return build_x_binary_op (input_location, code,
11097 left, TREE_CODE (left),
11098 right, TREE_CODE (right),
11099 /*overload=*/NULL,
11100 complain);
11104 /* Substitute ARGS into the pack of a fold expression T. */
11106 static inline tree
11107 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11109 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11112 /* Substitute ARGS into the pack of a fold expression T. */
11114 static inline tree
11115 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11117 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11120 /* Expand a PACK of arguments into a grouped as left fold.
11121 Given a pack containing elements A0, A1, ..., An and an
11122 operator @, this builds the expression:
11124 ((A0 @ A1) @ A2) ... @ An
11126 Note that PACK must not be empty.
11128 The operator is defined by the original fold expression T. */
11130 static tree
11131 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11133 tree left = TREE_VEC_ELT (pack, 0);
11134 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11136 tree right = TREE_VEC_ELT (pack, i);
11137 left = fold_expression (t, left, right, complain);
11139 return left;
11142 /* Substitute into a unary left fold expression. */
11144 static tree
11145 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11146 tree in_decl)
11148 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11149 if (pack == error_mark_node)
11150 return error_mark_node;
11151 if (PACK_EXPANSION_P (pack))
11153 tree r = copy_node (t);
11154 FOLD_EXPR_PACK (r) = pack;
11155 return r;
11157 if (TREE_VEC_LENGTH (pack) == 0)
11158 return expand_empty_fold (t, complain);
11159 else
11160 return expand_left_fold (t, pack, complain);
11163 /* Substitute into a binary left fold expression.
11165 Do ths by building a single (non-empty) vector of argumnts and
11166 building the expression from those elements. */
11168 static tree
11169 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11170 tree in_decl)
11172 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11173 if (pack == error_mark_node)
11174 return error_mark_node;
11175 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11176 if (init == error_mark_node)
11177 return error_mark_node;
11179 if (PACK_EXPANSION_P (pack))
11181 tree r = copy_node (t);
11182 FOLD_EXPR_PACK (r) = pack;
11183 FOLD_EXPR_INIT (r) = init;
11184 return r;
11187 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11188 TREE_VEC_ELT (vec, 0) = init;
11189 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11190 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11192 return expand_left_fold (t, vec, complain);
11195 /* Expand a PACK of arguments into a grouped as right fold.
11196 Given a pack containing elementns A0, A1, ..., and an
11197 operator @, this builds the expression:
11199 A0@ ... (An-2 @ (An-1 @ An))
11201 Note that PACK must not be empty.
11203 The operator is defined by the original fold expression T. */
11205 tree
11206 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11208 // Build the expression.
11209 int n = TREE_VEC_LENGTH (pack);
11210 tree right = TREE_VEC_ELT (pack, n - 1);
11211 for (--n; n != 0; --n)
11213 tree left = TREE_VEC_ELT (pack, n - 1);
11214 right = fold_expression (t, left, right, complain);
11216 return right;
11219 /* Substitute into a unary right fold expression. */
11221 static tree
11222 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11223 tree in_decl)
11225 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11226 if (pack == error_mark_node)
11227 return error_mark_node;
11228 if (PACK_EXPANSION_P (pack))
11230 tree r = copy_node (t);
11231 FOLD_EXPR_PACK (r) = pack;
11232 return r;
11234 if (TREE_VEC_LENGTH (pack) == 0)
11235 return expand_empty_fold (t, complain);
11236 else
11237 return expand_right_fold (t, pack, complain);
11240 /* Substitute into a binary right fold expression.
11242 Do ths by building a single (non-empty) vector of arguments and
11243 building the expression from those elements. */
11245 static tree
11246 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11247 tree in_decl)
11249 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11250 if (pack == error_mark_node)
11251 return error_mark_node;
11252 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11253 if (init == error_mark_node)
11254 return error_mark_node;
11256 if (PACK_EXPANSION_P (pack))
11258 tree r = copy_node (t);
11259 FOLD_EXPR_PACK (r) = pack;
11260 FOLD_EXPR_INIT (r) = init;
11261 return r;
11264 int n = TREE_VEC_LENGTH (pack);
11265 tree vec = make_tree_vec (n + 1);
11266 for (int i = 0; i < n; ++i)
11267 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11268 TREE_VEC_ELT (vec, n) = init;
11270 return expand_right_fold (t, vec, complain);
11274 /* Substitute ARGS into T, which is an pack expansion
11275 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11276 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11277 (if only a partial substitution could be performed) or
11278 ERROR_MARK_NODE if there was an error. */
11279 tree
11280 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11281 tree in_decl)
11283 tree pattern;
11284 tree pack, packs = NULL_TREE;
11285 bool unsubstituted_packs = false;
11286 int i, len = -1;
11287 tree result;
11288 hash_map<tree, tree> *saved_local_specializations = NULL;
11289 bool need_local_specializations = false;
11290 int levels;
11292 gcc_assert (PACK_EXPANSION_P (t));
11293 pattern = PACK_EXPANSION_PATTERN (t);
11295 /* Add in any args remembered from an earlier partial instantiation. */
11296 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11298 levels = TMPL_ARGS_DEPTH (args);
11300 /* Determine the argument packs that will instantiate the parameter
11301 packs used in the expansion expression. While we're at it,
11302 compute the number of arguments to be expanded and make sure it
11303 is consistent. */
11304 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11305 pack = TREE_CHAIN (pack))
11307 tree parm_pack = TREE_VALUE (pack);
11308 tree arg_pack = NULL_TREE;
11309 tree orig_arg = NULL_TREE;
11310 int level = 0;
11312 if (TREE_CODE (parm_pack) == BASES)
11314 if (BASES_DIRECT (parm_pack))
11315 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11316 args, complain, in_decl, false));
11317 else
11318 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11319 args, complain, in_decl, false));
11321 if (TREE_CODE (parm_pack) == PARM_DECL)
11323 /* We know we have correct local_specializations if this
11324 expansion is at function scope, or if we're dealing with a
11325 local parameter in a requires expression; for the latter,
11326 tsubst_requires_expr set it up appropriately. */
11327 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11328 arg_pack = retrieve_local_specialization (parm_pack);
11329 else
11330 /* We can't rely on local_specializations for a parameter
11331 name used later in a function declaration (such as in a
11332 late-specified return type). Even if it exists, it might
11333 have the wrong value for a recursive call. */
11334 need_local_specializations = true;
11336 if (!arg_pack)
11338 /* This parameter pack was used in an unevaluated context. Just
11339 make a dummy decl, since it's only used for its type. */
11340 arg_pack = tsubst_decl (parm_pack, args, complain);
11341 if (arg_pack && DECL_PACK_P (arg_pack))
11342 /* Partial instantiation of the parm_pack, we can't build
11343 up an argument pack yet. */
11344 arg_pack = NULL_TREE;
11345 else
11346 arg_pack = make_fnparm_pack (arg_pack);
11349 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11350 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11351 else
11353 int idx;
11354 template_parm_level_and_index (parm_pack, &level, &idx);
11356 if (level <= levels)
11357 arg_pack = TMPL_ARG (args, level, idx);
11360 orig_arg = arg_pack;
11361 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11362 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11364 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11365 /* This can only happen if we forget to expand an argument
11366 pack somewhere else. Just return an error, silently. */
11368 result = make_tree_vec (1);
11369 TREE_VEC_ELT (result, 0) = error_mark_node;
11370 return result;
11373 if (arg_pack)
11375 int my_len =
11376 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11378 /* Don't bother trying to do a partial substitution with
11379 incomplete packs; we'll try again after deduction. */
11380 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11381 return t;
11383 if (len < 0)
11384 len = my_len;
11385 else if (len != my_len)
11387 if (!(complain & tf_error))
11388 /* Fail quietly. */;
11389 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11390 error ("mismatched argument pack lengths while expanding "
11391 "%<%T%>",
11392 pattern);
11393 else
11394 error ("mismatched argument pack lengths while expanding "
11395 "%<%E%>",
11396 pattern);
11397 return error_mark_node;
11400 /* Keep track of the parameter packs and their corresponding
11401 argument packs. */
11402 packs = tree_cons (parm_pack, arg_pack, packs);
11403 TREE_TYPE (packs) = orig_arg;
11405 else
11407 /* We can't substitute for this parameter pack. We use a flag as
11408 well as the missing_level counter because function parameter
11409 packs don't have a level. */
11410 gcc_assert (processing_template_decl);
11411 unsubstituted_packs = true;
11415 /* If the expansion is just T..., return the matching argument pack, unless
11416 we need to call convert_from_reference on all the elements. This is an
11417 important optimization; see c++/68422. */
11418 if (!unsubstituted_packs
11419 && TREE_PURPOSE (packs) == pattern)
11421 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11422 /* Types need no adjustment, nor does sizeof..., and if we still have
11423 some pack expansion args we won't do anything yet. */
11424 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11425 || PACK_EXPANSION_SIZEOF_P (t)
11426 || pack_expansion_args_count (args))
11427 return args;
11428 /* Also optimize expression pack expansions if we can tell that the
11429 elements won't have reference type. */
11430 tree type = TREE_TYPE (pattern);
11431 if (type && TREE_CODE (type) != REFERENCE_TYPE
11432 && !PACK_EXPANSION_P (type)
11433 && !WILDCARD_TYPE_P (type))
11434 return args;
11435 /* Otherwise use the normal path so we get convert_from_reference. */
11438 /* We cannot expand this expansion expression, because we don't have
11439 all of the argument packs we need. */
11440 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11442 /* We got some full packs, but we can't substitute them in until we
11443 have values for all the packs. So remember these until then. */
11445 t = make_pack_expansion (pattern);
11446 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11447 return t;
11449 else if (unsubstituted_packs)
11451 /* There were no real arguments, we're just replacing a parameter
11452 pack with another version of itself. Substitute into the
11453 pattern and return a PACK_EXPANSION_*. The caller will need to
11454 deal with that. */
11455 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11456 t = tsubst_expr (pattern, args, complain, in_decl,
11457 /*integral_constant_expression_p=*/false);
11458 else
11459 t = tsubst (pattern, args, complain, in_decl);
11460 t = make_pack_expansion (t);
11461 return t;
11464 gcc_assert (len >= 0);
11466 if (need_local_specializations)
11468 /* We're in a late-specified return type, so create our own local
11469 specializations map; the current map is either NULL or (in the
11470 case of recursive unification) might have bindings that we don't
11471 want to use or alter. */
11472 saved_local_specializations = local_specializations;
11473 local_specializations = new hash_map<tree, tree>;
11476 /* For each argument in each argument pack, substitute into the
11477 pattern. */
11478 result = make_tree_vec (len);
11479 tree elem_args = copy_template_args (args);
11480 for (i = 0; i < len; ++i)
11482 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11484 elem_args, complain,
11485 in_decl);
11486 TREE_VEC_ELT (result, i) = t;
11487 if (t == error_mark_node)
11489 result = error_mark_node;
11490 break;
11494 /* Update ARGS to restore the substitution from parameter packs to
11495 their argument packs. */
11496 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11498 tree parm = TREE_PURPOSE (pack);
11500 if (TREE_CODE (parm) == PARM_DECL
11501 || TREE_CODE (parm) == FIELD_DECL)
11502 register_local_specialization (TREE_TYPE (pack), parm);
11503 else
11505 int idx, level;
11507 if (TREE_VALUE (pack) == NULL_TREE)
11508 continue;
11510 template_parm_level_and_index (parm, &level, &idx);
11512 /* Update the corresponding argument. */
11513 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11514 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11515 TREE_TYPE (pack);
11516 else
11517 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11521 if (need_local_specializations)
11523 delete local_specializations;
11524 local_specializations = saved_local_specializations;
11527 /* If the dependent pack arguments were such that we end up with only a
11528 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11529 if (len == 1 && TREE_CODE (result) == TREE_VEC
11530 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11531 return TREE_VEC_ELT (result, 0);
11533 return result;
11536 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11537 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11538 parameter packs; all parms generated from a function parameter pack will
11539 have the same DECL_PARM_INDEX. */
11541 tree
11542 get_pattern_parm (tree parm, tree tmpl)
11544 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11545 tree patparm;
11547 if (DECL_ARTIFICIAL (parm))
11549 for (patparm = DECL_ARGUMENTS (pattern);
11550 patparm; patparm = DECL_CHAIN (patparm))
11551 if (DECL_ARTIFICIAL (patparm)
11552 && DECL_NAME (parm) == DECL_NAME (patparm))
11553 break;
11555 else
11557 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11558 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11559 gcc_assert (DECL_PARM_INDEX (patparm)
11560 == DECL_PARM_INDEX (parm));
11563 return patparm;
11566 /* Make an argument pack out of the TREE_VEC VEC. */
11568 static tree
11569 make_argument_pack (tree vec)
11571 tree pack;
11572 tree elt = TREE_VEC_ELT (vec, 0);
11573 if (TYPE_P (elt))
11574 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11575 else
11577 pack = make_node (NONTYPE_ARGUMENT_PACK);
11578 TREE_TYPE (pack) = TREE_TYPE (elt);
11579 TREE_CONSTANT (pack) = 1;
11581 SET_ARGUMENT_PACK_ARGS (pack, vec);
11582 return pack;
11585 /* Return an exact copy of template args T that can be modified
11586 independently. */
11588 static tree
11589 copy_template_args (tree t)
11591 if (t == error_mark_node)
11592 return t;
11594 int len = TREE_VEC_LENGTH (t);
11595 tree new_vec = make_tree_vec (len);
11597 for (int i = 0; i < len; ++i)
11599 tree elt = TREE_VEC_ELT (t, i);
11600 if (elt && TREE_CODE (elt) == TREE_VEC)
11601 elt = copy_template_args (elt);
11602 TREE_VEC_ELT (new_vec, i) = elt;
11605 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11606 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11608 return new_vec;
11611 /* Substitute ARGS into the vector or list of template arguments T. */
11613 static tree
11614 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11616 tree orig_t = t;
11617 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11618 tree *elts;
11620 if (t == error_mark_node)
11621 return error_mark_node;
11623 len = TREE_VEC_LENGTH (t);
11624 elts = XALLOCAVEC (tree, len);
11626 for (i = 0; i < len; i++)
11628 tree orig_arg = TREE_VEC_ELT (t, i);
11629 tree new_arg;
11631 if (TREE_CODE (orig_arg) == TREE_VEC)
11632 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11633 else if (PACK_EXPANSION_P (orig_arg))
11635 /* Substitute into an expansion expression. */
11636 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11638 if (TREE_CODE (new_arg) == TREE_VEC)
11639 /* Add to the expanded length adjustment the number of
11640 expanded arguments. We subtract one from this
11641 measurement, because the argument pack expression
11642 itself is already counted as 1 in
11643 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11644 the argument pack is empty. */
11645 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11647 else if (ARGUMENT_PACK_P (orig_arg))
11649 /* Substitute into each of the arguments. */
11650 new_arg = TYPE_P (orig_arg)
11651 ? cxx_make_type (TREE_CODE (orig_arg))
11652 : make_node (TREE_CODE (orig_arg));
11654 SET_ARGUMENT_PACK_ARGS (
11655 new_arg,
11656 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11657 args, complain, in_decl));
11659 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
11660 new_arg = error_mark_node;
11662 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
11663 if (type_uses_auto (TREE_TYPE (orig_arg)))
11664 TREE_TYPE (new_arg) = TREE_TYPE (orig_arg);
11665 else
11666 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
11667 complain, in_decl);
11668 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11670 if (TREE_TYPE (new_arg) == error_mark_node)
11671 new_arg = error_mark_node;
11674 else
11675 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11677 if (new_arg == error_mark_node)
11678 return error_mark_node;
11680 elts[i] = new_arg;
11681 if (new_arg != orig_arg)
11682 need_new = 1;
11685 if (!need_new)
11686 return t;
11688 /* Make space for the expanded arguments coming from template
11689 argument packs. */
11690 t = make_tree_vec (len + expanded_len_adjust);
11691 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11692 arguments for a member template.
11693 In that case each TREE_VEC in ORIG_T represents a level of template
11694 arguments, and ORIG_T won't carry any non defaulted argument count.
11695 It will rather be the nested TREE_VECs that will carry one.
11696 In other words, ORIG_T carries a non defaulted argument count only
11697 if it doesn't contain any nested TREE_VEC. */
11698 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11700 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11701 count += expanded_len_adjust;
11702 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11704 for (i = 0, out = 0; i < len; i++)
11706 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11707 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11708 && TREE_CODE (elts[i]) == TREE_VEC)
11710 int idx;
11712 /* Now expand the template argument pack "in place". */
11713 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11714 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11716 else
11718 TREE_VEC_ELT (t, out) = elts[i];
11719 out++;
11723 return t;
11726 /* Substitute ARGS into one level PARMS of template parameters. */
11728 static tree
11729 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11731 if (parms == error_mark_node)
11732 return error_mark_node;
11734 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11736 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11738 tree tuple = TREE_VEC_ELT (parms, i);
11740 if (tuple == error_mark_node)
11741 continue;
11743 TREE_VEC_ELT (new_vec, i) =
11744 tsubst_template_parm (tuple, args, complain);
11747 return new_vec;
11750 /* Return the result of substituting ARGS into the template parameters
11751 given by PARMS. If there are m levels of ARGS and m + n levels of
11752 PARMS, then the result will contain n levels of PARMS. For
11753 example, if PARMS is `template <class T> template <class U>
11754 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11755 result will be `template <int*, double, class V>'. */
11757 static tree
11758 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11760 tree r = NULL_TREE;
11761 tree* new_parms;
11763 /* When substituting into a template, we must set
11764 PROCESSING_TEMPLATE_DECL as the template parameters may be
11765 dependent if they are based on one-another, and the dependency
11766 predicates are short-circuit outside of templates. */
11767 ++processing_template_decl;
11769 for (new_parms = &r;
11770 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11771 new_parms = &(TREE_CHAIN (*new_parms)),
11772 parms = TREE_CHAIN (parms))
11774 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11775 args, complain);
11776 *new_parms =
11777 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11778 - TMPL_ARGS_DEPTH (args)),
11779 new_vec, NULL_TREE);
11782 --processing_template_decl;
11784 return r;
11787 /* Return the result of substituting ARGS into one template parameter
11788 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11789 parameter and which TREE_PURPOSE is the default argument of the
11790 template parameter. */
11792 static tree
11793 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11795 tree default_value, parm_decl;
11797 if (args == NULL_TREE
11798 || t == NULL_TREE
11799 || t == error_mark_node)
11800 return t;
11802 gcc_assert (TREE_CODE (t) == TREE_LIST);
11804 default_value = TREE_PURPOSE (t);
11805 parm_decl = TREE_VALUE (t);
11807 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11808 if (TREE_CODE (parm_decl) == PARM_DECL
11809 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11810 parm_decl = error_mark_node;
11811 default_value = tsubst_template_arg (default_value, args,
11812 complain, NULL_TREE);
11814 return build_tree_list (default_value, parm_decl);
11817 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11818 type T. If T is not an aggregate or enumeration type, it is
11819 handled as if by tsubst. IN_DECL is as for tsubst. If
11820 ENTERING_SCOPE is nonzero, T is the context for a template which
11821 we are presently tsubst'ing. Return the substituted value. */
11823 static tree
11824 tsubst_aggr_type (tree t,
11825 tree args,
11826 tsubst_flags_t complain,
11827 tree in_decl,
11828 int entering_scope)
11830 if (t == NULL_TREE)
11831 return NULL_TREE;
11833 switch (TREE_CODE (t))
11835 case RECORD_TYPE:
11836 if (TYPE_PTRMEMFUNC_P (t))
11837 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11839 /* Fall through. */
11840 case ENUMERAL_TYPE:
11841 case UNION_TYPE:
11842 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11844 tree argvec;
11845 tree context;
11846 tree r;
11847 int saved_unevaluated_operand;
11848 int saved_inhibit_evaluation_warnings;
11850 /* In "sizeof(X<I>)" we need to evaluate "I". */
11851 saved_unevaluated_operand = cp_unevaluated_operand;
11852 cp_unevaluated_operand = 0;
11853 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11854 c_inhibit_evaluation_warnings = 0;
11856 /* First, determine the context for the type we are looking
11857 up. */
11858 context = TYPE_CONTEXT (t);
11859 if (context && TYPE_P (context))
11861 context = tsubst_aggr_type (context, args, complain,
11862 in_decl, /*entering_scope=*/1);
11863 /* If context is a nested class inside a class template,
11864 it may still need to be instantiated (c++/33959). */
11865 context = complete_type (context);
11868 /* Then, figure out what arguments are appropriate for the
11869 type we are trying to find. For example, given:
11871 template <class T> struct S;
11872 template <class T, class U> void f(T, U) { S<U> su; }
11874 and supposing that we are instantiating f<int, double>,
11875 then our ARGS will be {int, double}, but, when looking up
11876 S we only want {double}. */
11877 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11878 complain, in_decl);
11879 if (argvec == error_mark_node)
11880 r = error_mark_node;
11881 else
11883 r = lookup_template_class (t, argvec, in_decl, context,
11884 entering_scope, complain);
11885 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11888 cp_unevaluated_operand = saved_unevaluated_operand;
11889 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11891 return r;
11893 else
11894 /* This is not a template type, so there's nothing to do. */
11895 return t;
11897 default:
11898 return tsubst (t, args, complain, in_decl);
11902 /* Substitute into the default argument ARG (a default argument for
11903 FN), which has the indicated TYPE. */
11905 tree
11906 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11908 tree saved_class_ptr = NULL_TREE;
11909 tree saved_class_ref = NULL_TREE;
11910 int errs = errorcount + sorrycount;
11912 /* This can happen in invalid code. */
11913 if (TREE_CODE (arg) == DEFAULT_ARG)
11914 return arg;
11916 /* This default argument came from a template. Instantiate the
11917 default argument here, not in tsubst. In the case of
11918 something like:
11920 template <class T>
11921 struct S {
11922 static T t();
11923 void f(T = t());
11926 we must be careful to do name lookup in the scope of S<T>,
11927 rather than in the current class. */
11928 push_access_scope (fn);
11929 /* The "this" pointer is not valid in a default argument. */
11930 if (cfun)
11932 saved_class_ptr = current_class_ptr;
11933 cp_function_chain->x_current_class_ptr = NULL_TREE;
11934 saved_class_ref = current_class_ref;
11935 cp_function_chain->x_current_class_ref = NULL_TREE;
11938 push_deferring_access_checks(dk_no_deferred);
11939 /* The default argument expression may cause implicitly defined
11940 member functions to be synthesized, which will result in garbage
11941 collection. We must treat this situation as if we were within
11942 the body of function so as to avoid collecting live data on the
11943 stack. */
11944 ++function_depth;
11945 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11946 complain, NULL_TREE,
11947 /*integral_constant_expression_p=*/false);
11948 --function_depth;
11949 pop_deferring_access_checks();
11951 /* Restore the "this" pointer. */
11952 if (cfun)
11954 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11955 cp_function_chain->x_current_class_ref = saved_class_ref;
11958 if (errorcount+sorrycount > errs
11959 && (complain & tf_warning_or_error))
11960 inform (input_location,
11961 " when instantiating default argument for call to %D", fn);
11963 /* Make sure the default argument is reasonable. */
11964 arg = check_default_argument (type, arg, complain);
11966 pop_access_scope (fn);
11968 return arg;
11971 /* Substitute into all the default arguments for FN. */
11973 static void
11974 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11976 tree arg;
11977 tree tmpl_args;
11979 tmpl_args = DECL_TI_ARGS (fn);
11981 /* If this function is not yet instantiated, we certainly don't need
11982 its default arguments. */
11983 if (uses_template_parms (tmpl_args))
11984 return;
11985 /* Don't do this again for clones. */
11986 if (DECL_CLONED_FUNCTION_P (fn))
11987 return;
11989 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11990 arg;
11991 arg = TREE_CHAIN (arg))
11992 if (TREE_PURPOSE (arg))
11993 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11994 TREE_VALUE (arg),
11995 TREE_PURPOSE (arg),
11996 complain);
11999 /* Substitute the ARGS into the T, which is a _DECL. Return the
12000 result of the substitution. Issue error and warning messages under
12001 control of COMPLAIN. */
12003 static tree
12004 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12006 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12007 location_t saved_loc;
12008 tree r = NULL_TREE;
12009 tree in_decl = t;
12010 hashval_t hash = 0;
12012 /* Set the filename and linenumber to improve error-reporting. */
12013 saved_loc = input_location;
12014 input_location = DECL_SOURCE_LOCATION (t);
12016 switch (TREE_CODE (t))
12018 case TEMPLATE_DECL:
12020 /* We can get here when processing a member function template,
12021 member class template, or template template parameter. */
12022 tree decl = DECL_TEMPLATE_RESULT (t);
12023 tree spec;
12024 tree tmpl_args;
12025 tree full_args;
12027 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12029 /* Template template parameter is treated here. */
12030 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12031 if (new_type == error_mark_node)
12032 r = error_mark_node;
12033 /* If we get a real template back, return it. This can happen in
12034 the context of most_specialized_partial_spec. */
12035 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12036 r = new_type;
12037 else
12038 /* The new TEMPLATE_DECL was built in
12039 reduce_template_parm_level. */
12040 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12041 break;
12044 /* We might already have an instance of this template.
12045 The ARGS are for the surrounding class type, so the
12046 full args contain the tsubst'd args for the context,
12047 plus the innermost args from the template decl. */
12048 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12049 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12050 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12051 /* Because this is a template, the arguments will still be
12052 dependent, even after substitution. If
12053 PROCESSING_TEMPLATE_DECL is not set, the dependency
12054 predicates will short-circuit. */
12055 ++processing_template_decl;
12056 full_args = tsubst_template_args (tmpl_args, args,
12057 complain, in_decl);
12058 --processing_template_decl;
12059 if (full_args == error_mark_node)
12060 RETURN (error_mark_node);
12062 /* If this is a default template template argument,
12063 tsubst might not have changed anything. */
12064 if (full_args == tmpl_args)
12065 RETURN (t);
12067 hash = hash_tmpl_and_args (t, full_args);
12068 spec = retrieve_specialization (t, full_args, hash);
12069 if (spec != NULL_TREE)
12071 r = spec;
12072 break;
12075 /* Make a new template decl. It will be similar to the
12076 original, but will record the current template arguments.
12077 We also create a new function declaration, which is just
12078 like the old one, but points to this new template, rather
12079 than the old one. */
12080 r = copy_decl (t);
12081 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12082 DECL_CHAIN (r) = NULL_TREE;
12084 // Build new template info linking to the original template decl.
12085 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12087 if (TREE_CODE (decl) == TYPE_DECL
12088 && !TYPE_DECL_ALIAS_P (decl))
12090 tree new_type;
12091 ++processing_template_decl;
12092 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12093 --processing_template_decl;
12094 if (new_type == error_mark_node)
12095 RETURN (error_mark_node);
12097 TREE_TYPE (r) = new_type;
12098 /* For a partial specialization, we need to keep pointing to
12099 the primary template. */
12100 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12101 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12102 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12103 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12104 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12106 else
12108 tree new_decl;
12109 ++processing_template_decl;
12110 new_decl = tsubst (decl, args, complain, in_decl);
12111 --processing_template_decl;
12112 if (new_decl == error_mark_node)
12113 RETURN (error_mark_node);
12115 DECL_TEMPLATE_RESULT (r) = new_decl;
12116 DECL_TI_TEMPLATE (new_decl) = r;
12117 TREE_TYPE (r) = TREE_TYPE (new_decl);
12118 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12119 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12122 SET_DECL_IMPLICIT_INSTANTIATION (r);
12123 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12124 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12126 /* The template parameters for this new template are all the
12127 template parameters for the old template, except the
12128 outermost level of parameters. */
12129 DECL_TEMPLATE_PARMS (r)
12130 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12131 complain);
12133 if (PRIMARY_TEMPLATE_P (t))
12134 DECL_PRIMARY_TEMPLATE (r) = r;
12136 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12137 /* Record this non-type partial instantiation. */
12138 register_specialization (r, t,
12139 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12140 false, hash);
12142 break;
12144 case FUNCTION_DECL:
12146 tree ctx;
12147 tree argvec = NULL_TREE;
12148 tree *friends;
12149 tree gen_tmpl;
12150 tree type;
12151 int member;
12152 int args_depth;
12153 int parms_depth;
12155 /* Nobody should be tsubst'ing into non-template functions. */
12156 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12158 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12160 tree spec;
12162 /* If T is not dependent, just return it. */
12163 if (!uses_template_parms (DECL_TI_ARGS (t)))
12164 RETURN (t);
12166 /* Calculate the most general template of which R is a
12167 specialization, and the complete set of arguments used to
12168 specialize R. */
12169 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12170 argvec = tsubst_template_args (DECL_TI_ARGS
12171 (DECL_TEMPLATE_RESULT
12172 (DECL_TI_TEMPLATE (t))),
12173 args, complain, in_decl);
12174 if (argvec == error_mark_node)
12175 RETURN (error_mark_node);
12177 /* Check to see if we already have this specialization. */
12178 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12179 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12181 if (spec)
12183 r = spec;
12184 break;
12187 /* We can see more levels of arguments than parameters if
12188 there was a specialization of a member template, like
12189 this:
12191 template <class T> struct S { template <class U> void f(); }
12192 template <> template <class U> void S<int>::f(U);
12194 Here, we'll be substituting into the specialization,
12195 because that's where we can find the code we actually
12196 want to generate, but we'll have enough arguments for
12197 the most general template.
12199 We also deal with the peculiar case:
12201 template <class T> struct S {
12202 template <class U> friend void f();
12204 template <class U> void f() {}
12205 template S<int>;
12206 template void f<double>();
12208 Here, the ARGS for the instantiation of will be {int,
12209 double}. But, we only need as many ARGS as there are
12210 levels of template parameters in CODE_PATTERN. We are
12211 careful not to get fooled into reducing the ARGS in
12212 situations like:
12214 template <class T> struct S { template <class U> void f(U); }
12215 template <class T> template <> void S<T>::f(int) {}
12217 which we can spot because the pattern will be a
12218 specialization in this case. */
12219 args_depth = TMPL_ARGS_DEPTH (args);
12220 parms_depth =
12221 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12222 if (args_depth > parms_depth
12223 && !DECL_TEMPLATE_SPECIALIZATION (t))
12224 args = get_innermost_template_args (args, parms_depth);
12226 else
12228 /* This special case arises when we have something like this:
12230 template <class T> struct S {
12231 friend void f<int>(int, double);
12234 Here, the DECL_TI_TEMPLATE for the friend declaration
12235 will be an IDENTIFIER_NODE. We are being called from
12236 tsubst_friend_function, and we want only to create a
12237 new decl (R) with appropriate types so that we can call
12238 determine_specialization. */
12239 gen_tmpl = NULL_TREE;
12242 if (DECL_CLASS_SCOPE_P (t))
12244 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12245 member = 2;
12246 else
12247 member = 1;
12248 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12249 complain, t, /*entering_scope=*/1);
12251 else
12253 member = 0;
12254 ctx = DECL_CONTEXT (t);
12256 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12257 if (type == error_mark_node)
12258 RETURN (error_mark_node);
12260 /* If we hit excessive deduction depth, the type is bogus even if
12261 it isn't error_mark_node, so don't build a decl. */
12262 if (excessive_deduction_depth)
12263 RETURN (error_mark_node);
12265 /* We do NOT check for matching decls pushed separately at this
12266 point, as they may not represent instantiations of this
12267 template, and in any case are considered separate under the
12268 discrete model. */
12269 r = copy_decl (t);
12270 DECL_USE_TEMPLATE (r) = 0;
12271 TREE_TYPE (r) = type;
12272 /* Clear out the mangled name and RTL for the instantiation. */
12273 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12274 SET_DECL_RTL (r, NULL);
12275 /* Leave DECL_INITIAL set on deleted instantiations. */
12276 if (!DECL_DELETED_FN (r))
12277 DECL_INITIAL (r) = NULL_TREE;
12278 DECL_CONTEXT (r) = ctx;
12280 /* OpenMP UDRs have the only argument a reference to the declared
12281 type. We want to diagnose if the declared type is a reference,
12282 which is invalid, but as references to references are usually
12283 quietly merged, diagnose it here. */
12284 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12286 tree argtype
12287 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12288 argtype = tsubst (argtype, args, complain, in_decl);
12289 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12290 error_at (DECL_SOURCE_LOCATION (t),
12291 "reference type %qT in "
12292 "%<#pragma omp declare reduction%>", argtype);
12293 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12294 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12295 argtype);
12298 if (member && DECL_CONV_FN_P (r))
12299 /* Type-conversion operator. Reconstruct the name, in
12300 case it's the name of one of the template's parameters. */
12301 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12303 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12304 complain, t);
12305 DECL_RESULT (r) = NULL_TREE;
12307 TREE_STATIC (r) = 0;
12308 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12309 DECL_EXTERNAL (r) = 1;
12310 /* If this is an instantiation of a function with internal
12311 linkage, we already know what object file linkage will be
12312 assigned to the instantiation. */
12313 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12314 DECL_DEFER_OUTPUT (r) = 0;
12315 DECL_CHAIN (r) = NULL_TREE;
12316 DECL_PENDING_INLINE_INFO (r) = 0;
12317 DECL_PENDING_INLINE_P (r) = 0;
12318 DECL_SAVED_TREE (r) = NULL_TREE;
12319 DECL_STRUCT_FUNCTION (r) = NULL;
12320 TREE_USED (r) = 0;
12321 /* We'll re-clone as appropriate in instantiate_template. */
12322 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12324 /* If we aren't complaining now, return on error before we register
12325 the specialization so that we'll complain eventually. */
12326 if ((complain & tf_error) == 0
12327 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12328 && !grok_op_properties (r, /*complain=*/false))
12329 RETURN (error_mark_node);
12331 /* When instantiating a constrained member, substitute
12332 into the constraints to create a new constraint. */
12333 if (tree ci = get_constraints (t))
12334 if (member)
12336 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12337 set_constraints (r, ci);
12340 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12341 this in the special friend case mentioned above where
12342 GEN_TMPL is NULL. */
12343 if (gen_tmpl)
12345 DECL_TEMPLATE_INFO (r)
12346 = build_template_info (gen_tmpl, argvec);
12347 SET_DECL_IMPLICIT_INSTANTIATION (r);
12349 tree new_r
12350 = register_specialization (r, gen_tmpl, argvec, false, hash);
12351 if (new_r != r)
12352 /* We instantiated this while substituting into
12353 the type earlier (template/friend54.C). */
12354 RETURN (new_r);
12356 /* We're not supposed to instantiate default arguments
12357 until they are called, for a template. But, for a
12358 declaration like:
12360 template <class T> void f ()
12361 { extern void g(int i = T()); }
12363 we should do the substitution when the template is
12364 instantiated. We handle the member function case in
12365 instantiate_class_template since the default arguments
12366 might refer to other members of the class. */
12367 if (!member
12368 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12369 && !uses_template_parms (argvec))
12370 tsubst_default_arguments (r, complain);
12372 else
12373 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12375 /* Copy the list of befriending classes. */
12376 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12377 *friends;
12378 friends = &TREE_CHAIN (*friends))
12380 *friends = copy_node (*friends);
12381 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12382 args, complain,
12383 in_decl);
12386 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12388 maybe_retrofit_in_chrg (r);
12389 if (DECL_CONSTRUCTOR_P (r))
12390 grok_ctor_properties (ctx, r);
12391 /* If this is an instantiation of a member template, clone it.
12392 If it isn't, that'll be handled by
12393 clone_constructors_and_destructors. */
12394 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12395 clone_function_decl (r, /*update_method_vec_p=*/0);
12397 else if ((complain & tf_error) != 0
12398 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12399 && !grok_op_properties (r, /*complain=*/true))
12400 RETURN (error_mark_node);
12402 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12403 SET_DECL_FRIEND_CONTEXT (r,
12404 tsubst (DECL_FRIEND_CONTEXT (t),
12405 args, complain, in_decl));
12407 /* Possibly limit visibility based on template args. */
12408 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12409 if (DECL_VISIBILITY_SPECIFIED (t))
12411 DECL_VISIBILITY_SPECIFIED (r) = 0;
12412 DECL_ATTRIBUTES (r)
12413 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12415 determine_visibility (r);
12416 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12417 && !processing_template_decl)
12418 defaulted_late_check (r);
12420 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12421 args, complain, in_decl);
12423 break;
12425 case PARM_DECL:
12427 tree type = NULL_TREE;
12428 int i, len = 1;
12429 tree expanded_types = NULL_TREE;
12430 tree prev_r = NULL_TREE;
12431 tree first_r = NULL_TREE;
12433 if (DECL_PACK_P (t))
12435 /* If there is a local specialization that isn't a
12436 parameter pack, it means that we're doing a "simple"
12437 substitution from inside tsubst_pack_expansion. Just
12438 return the local specialization (which will be a single
12439 parm). */
12440 tree spec = retrieve_local_specialization (t);
12441 if (spec
12442 && TREE_CODE (spec) == PARM_DECL
12443 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12444 RETURN (spec);
12446 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12447 the parameters in this function parameter pack. */
12448 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12449 complain, in_decl);
12450 if (TREE_CODE (expanded_types) == TREE_VEC)
12452 len = TREE_VEC_LENGTH (expanded_types);
12454 /* Zero-length parameter packs are boring. Just substitute
12455 into the chain. */
12456 if (len == 0)
12457 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12458 TREE_CHAIN (t)));
12460 else
12462 /* All we did was update the type. Make a note of that. */
12463 type = expanded_types;
12464 expanded_types = NULL_TREE;
12468 /* Loop through all of the parameters we'll build. When T is
12469 a function parameter pack, LEN is the number of expanded
12470 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12471 r = NULL_TREE;
12472 for (i = 0; i < len; ++i)
12474 prev_r = r;
12475 r = copy_node (t);
12476 if (DECL_TEMPLATE_PARM_P (t))
12477 SET_DECL_TEMPLATE_PARM_P (r);
12479 if (expanded_types)
12480 /* We're on the Ith parameter of the function parameter
12481 pack. */
12483 /* Get the Ith type. */
12484 type = TREE_VEC_ELT (expanded_types, i);
12486 /* Rename the parameter to include the index. */
12487 DECL_NAME (r)
12488 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12490 else if (!type)
12491 /* We're dealing with a normal parameter. */
12492 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12494 type = type_decays_to (type);
12495 TREE_TYPE (r) = type;
12496 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12498 if (DECL_INITIAL (r))
12500 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12501 DECL_INITIAL (r) = TREE_TYPE (r);
12502 else
12503 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12504 complain, in_decl);
12507 DECL_CONTEXT (r) = NULL_TREE;
12509 if (!DECL_TEMPLATE_PARM_P (r))
12510 DECL_ARG_TYPE (r) = type_passed_as (type);
12512 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12513 args, complain, in_decl);
12515 /* Keep track of the first new parameter we
12516 generate. That's what will be returned to the
12517 caller. */
12518 if (!first_r)
12519 first_r = r;
12521 /* Build a proper chain of parameters when substituting
12522 into a function parameter pack. */
12523 if (prev_r)
12524 DECL_CHAIN (prev_r) = r;
12527 /* If cp_unevaluated_operand is set, we're just looking for a
12528 single dummy parameter, so don't keep going. */
12529 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12530 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12531 complain, DECL_CHAIN (t));
12533 /* FIRST_R contains the start of the chain we've built. */
12534 r = first_r;
12536 break;
12538 case FIELD_DECL:
12540 tree type = NULL_TREE;
12541 tree vec = NULL_TREE;
12542 tree expanded_types = NULL_TREE;
12543 int len = 1;
12545 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12547 /* This field is a lambda capture pack. Return a TREE_VEC of
12548 the expanded fields to instantiate_class_template_1 and
12549 store them in the specializations hash table as a
12550 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12551 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12552 complain, in_decl);
12553 if (TREE_CODE (expanded_types) == TREE_VEC)
12555 len = TREE_VEC_LENGTH (expanded_types);
12556 vec = make_tree_vec (len);
12558 else
12560 /* All we did was update the type. Make a note of that. */
12561 type = expanded_types;
12562 expanded_types = NULL_TREE;
12566 for (int i = 0; i < len; ++i)
12568 r = copy_decl (t);
12569 if (expanded_types)
12571 type = TREE_VEC_ELT (expanded_types, i);
12572 DECL_NAME (r)
12573 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12575 else if (!type)
12576 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12578 if (type == error_mark_node)
12579 RETURN (error_mark_node);
12580 TREE_TYPE (r) = type;
12581 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12583 if (DECL_C_BIT_FIELD (r))
12584 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12585 non-bit-fields DECL_INITIAL is a non-static data member
12586 initializer, which gets deferred instantiation. */
12587 DECL_INITIAL (r)
12588 = tsubst_expr (DECL_INITIAL (t), args,
12589 complain, in_decl,
12590 /*integral_constant_expression_p=*/true);
12591 else if (DECL_INITIAL (t))
12593 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12594 NSDMI in perform_member_init. Still set DECL_INITIAL
12595 so that we know there is one. */
12596 DECL_INITIAL (r) = void_node;
12597 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12598 retrofit_lang_decl (r);
12599 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12601 /* We don't have to set DECL_CONTEXT here; it is set by
12602 finish_member_declaration. */
12603 DECL_CHAIN (r) = NULL_TREE;
12605 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12606 args, complain, in_decl);
12608 if (vec)
12609 TREE_VEC_ELT (vec, i) = r;
12612 if (vec)
12614 r = vec;
12615 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12616 tree tpack = cxx_make_type (TYPE_ARGUMENT_PACK);
12617 SET_ARGUMENT_PACK_ARGS (pack, vec);
12618 SET_ARGUMENT_PACK_ARGS (tpack, expanded_types);
12619 TREE_TYPE (pack) = tpack;
12620 register_specialization (pack, t, args, false, 0);
12623 break;
12625 case USING_DECL:
12626 /* We reach here only for member using decls. We also need to check
12627 uses_template_parms because DECL_DEPENDENT_P is not set for a
12628 using-declaration that designates a member of the current
12629 instantiation (c++/53549). */
12630 if (DECL_DEPENDENT_P (t)
12631 || uses_template_parms (USING_DECL_SCOPE (t)))
12633 tree scope = USING_DECL_SCOPE (t);
12634 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12635 if (PACK_EXPANSION_P (scope))
12637 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12638 int len = TREE_VEC_LENGTH (vec);
12639 r = make_tree_vec (len);
12640 for (int i = 0; i < len; ++i)
12642 tree escope = TREE_VEC_ELT (vec, i);
12643 tree elt = do_class_using_decl (escope, name);
12644 if (!elt)
12646 r = error_mark_node;
12647 break;
12649 else
12651 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12652 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12654 TREE_VEC_ELT (r, i) = elt;
12657 else
12659 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12660 complain, in_decl);
12661 r = do_class_using_decl (inst_scope, name);
12662 if (!r)
12663 r = error_mark_node;
12664 else
12666 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12667 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12671 else
12673 r = copy_node (t);
12674 DECL_CHAIN (r) = NULL_TREE;
12676 break;
12678 case TYPE_DECL:
12679 case VAR_DECL:
12681 tree argvec = NULL_TREE;
12682 tree gen_tmpl = NULL_TREE;
12683 tree spec;
12684 tree tmpl = NULL_TREE;
12685 tree ctx;
12686 tree type = NULL_TREE;
12687 bool local_p;
12689 if (TREE_TYPE (t) == error_mark_node)
12690 RETURN (error_mark_node);
12692 if (TREE_CODE (t) == TYPE_DECL
12693 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12695 /* If this is the canonical decl, we don't have to
12696 mess with instantiations, and often we can't (for
12697 typename, template type parms and such). Note that
12698 TYPE_NAME is not correct for the above test if
12699 we've copied the type for a typedef. */
12700 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12701 if (type == error_mark_node)
12702 RETURN (error_mark_node);
12703 r = TYPE_NAME (type);
12704 break;
12707 /* Check to see if we already have the specialization we
12708 need. */
12709 spec = NULL_TREE;
12710 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12712 /* T is a static data member or namespace-scope entity.
12713 We have to substitute into namespace-scope variables
12714 (not just variable templates) because of cases like:
12716 template <class T> void f() { extern T t; }
12718 where the entity referenced is not known until
12719 instantiation time. */
12720 local_p = false;
12721 ctx = DECL_CONTEXT (t);
12722 if (DECL_CLASS_SCOPE_P (t))
12724 ctx = tsubst_aggr_type (ctx, args,
12725 complain,
12726 in_decl, /*entering_scope=*/1);
12727 /* If CTX is unchanged, then T is in fact the
12728 specialization we want. That situation occurs when
12729 referencing a static data member within in its own
12730 class. We can use pointer equality, rather than
12731 same_type_p, because DECL_CONTEXT is always
12732 canonical... */
12733 if (ctx == DECL_CONTEXT (t)
12734 /* ... unless T is a member template; in which
12735 case our caller can be willing to create a
12736 specialization of that template represented
12737 by T. */
12738 && !(DECL_TI_TEMPLATE (t)
12739 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12740 spec = t;
12743 if (!spec)
12745 tmpl = DECL_TI_TEMPLATE (t);
12746 gen_tmpl = most_general_template (tmpl);
12747 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12748 if (argvec != error_mark_node)
12749 argvec = (coerce_innermost_template_parms
12750 (DECL_TEMPLATE_PARMS (gen_tmpl),
12751 argvec, t, complain,
12752 /*all*/true, /*defarg*/true));
12753 if (argvec == error_mark_node)
12754 RETURN (error_mark_node);
12755 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12756 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12759 else
12761 /* A local variable. */
12762 local_p = true;
12763 /* Subsequent calls to pushdecl will fill this in. */
12764 ctx = NULL_TREE;
12765 /* Unless this is a reference to a static variable from an
12766 enclosing function, in which case we need to fill it in now. */
12767 if (TREE_STATIC (t))
12769 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12770 if (fn != current_function_decl)
12771 ctx = fn;
12773 spec = retrieve_local_specialization (t);
12775 /* If we already have the specialization we need, there is
12776 nothing more to do. */
12777 if (spec)
12779 r = spec;
12780 break;
12783 /* Create a new node for the specialization we need. */
12784 r = copy_decl (t);
12785 if (type == NULL_TREE)
12787 if (is_typedef_decl (t))
12788 type = DECL_ORIGINAL_TYPE (t);
12789 else
12790 type = TREE_TYPE (t);
12791 if (VAR_P (t)
12792 && VAR_HAD_UNKNOWN_BOUND (t)
12793 && type != error_mark_node)
12794 type = strip_array_domain (type);
12795 type = tsubst (type, args, complain, in_decl);
12797 if (VAR_P (r))
12799 /* Even if the original location is out of scope, the
12800 newly substituted one is not. */
12801 DECL_DEAD_FOR_LOCAL (r) = 0;
12802 DECL_INITIALIZED_P (r) = 0;
12803 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12804 if (type == error_mark_node)
12805 RETURN (error_mark_node);
12806 if (TREE_CODE (type) == FUNCTION_TYPE)
12808 /* It may seem that this case cannot occur, since:
12810 typedef void f();
12811 void g() { f x; }
12813 declares a function, not a variable. However:
12815 typedef void f();
12816 template <typename T> void g() { T t; }
12817 template void g<f>();
12819 is an attempt to declare a variable with function
12820 type. */
12821 error ("variable %qD has function type",
12822 /* R is not yet sufficiently initialized, so we
12823 just use its name. */
12824 DECL_NAME (r));
12825 RETURN (error_mark_node);
12827 type = complete_type (type);
12828 /* Wait until cp_finish_decl to set this again, to handle
12829 circular dependency (template/instantiate6.C). */
12830 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12831 type = check_var_type (DECL_NAME (r), type);
12833 if (DECL_HAS_VALUE_EXPR_P (t))
12835 tree ve = DECL_VALUE_EXPR (t);
12836 ve = tsubst_expr (ve, args, complain, in_decl,
12837 /*constant_expression_p=*/false);
12838 if (REFERENCE_REF_P (ve))
12840 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12841 ve = TREE_OPERAND (ve, 0);
12843 SET_DECL_VALUE_EXPR (r, ve);
12845 if (CP_DECL_THREAD_LOCAL_P (r)
12846 && !processing_template_decl)
12847 set_decl_tls_model (r, decl_default_tls_model (r));
12849 else if (DECL_SELF_REFERENCE_P (t))
12850 SET_DECL_SELF_REFERENCE_P (r);
12851 TREE_TYPE (r) = type;
12852 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12853 DECL_CONTEXT (r) = ctx;
12854 /* Clear out the mangled name and RTL for the instantiation. */
12855 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12856 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12857 SET_DECL_RTL (r, NULL);
12858 /* The initializer must not be expanded until it is required;
12859 see [temp.inst]. */
12860 DECL_INITIAL (r) = NULL_TREE;
12861 if (VAR_P (r))
12862 SET_DECL_MODE (r, VOIDmode);
12863 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12864 SET_DECL_RTL (r, NULL);
12865 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12866 if (VAR_P (r))
12868 /* Possibly limit visibility based on template args. */
12869 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12870 if (DECL_VISIBILITY_SPECIFIED (t))
12872 DECL_VISIBILITY_SPECIFIED (r) = 0;
12873 DECL_ATTRIBUTES (r)
12874 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12876 determine_visibility (r);
12879 if (!local_p)
12881 /* A static data member declaration is always marked
12882 external when it is declared in-class, even if an
12883 initializer is present. We mimic the non-template
12884 processing here. */
12885 DECL_EXTERNAL (r) = 1;
12886 if (DECL_NAMESPACE_SCOPE_P (t))
12887 DECL_NOT_REALLY_EXTERN (r) = 1;
12889 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12890 SET_DECL_IMPLICIT_INSTANTIATION (r);
12891 register_specialization (r, gen_tmpl, argvec, false, hash);
12893 else
12895 if (DECL_LANG_SPECIFIC (r))
12896 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12897 if (!cp_unevaluated_operand)
12898 register_local_specialization (r, t);
12901 DECL_CHAIN (r) = NULL_TREE;
12903 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12904 /*flags=*/0,
12905 args, complain, in_decl);
12907 /* Preserve a typedef that names a type. */
12908 if (is_typedef_decl (r) && type != error_mark_node)
12910 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12911 set_underlying_type (r);
12912 if (TYPE_DECL_ALIAS_P (r))
12913 /* An alias template specialization can be dependent
12914 even if its underlying type is not. */
12915 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12918 layout_decl (r, 0);
12920 break;
12922 default:
12923 gcc_unreachable ();
12925 #undef RETURN
12927 out:
12928 /* Restore the file and line information. */
12929 input_location = saved_loc;
12931 return r;
12934 /* Substitute into the ARG_TYPES of a function type.
12935 If END is a TREE_CHAIN, leave it and any following types
12936 un-substituted. */
12938 static tree
12939 tsubst_arg_types (tree arg_types,
12940 tree args,
12941 tree end,
12942 tsubst_flags_t complain,
12943 tree in_decl)
12945 tree remaining_arg_types;
12946 tree type = NULL_TREE;
12947 int i = 1;
12948 tree expanded_args = NULL_TREE;
12949 tree default_arg;
12951 if (!arg_types || arg_types == void_list_node || arg_types == end)
12952 return arg_types;
12954 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12955 args, end, complain, in_decl);
12956 if (remaining_arg_types == error_mark_node)
12957 return error_mark_node;
12959 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12961 /* For a pack expansion, perform substitution on the
12962 entire expression. Later on, we'll handle the arguments
12963 one-by-one. */
12964 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12965 args, complain, in_decl);
12967 if (TREE_CODE (expanded_args) == TREE_VEC)
12968 /* So that we'll spin through the parameters, one by one. */
12969 i = TREE_VEC_LENGTH (expanded_args);
12970 else
12972 /* We only partially substituted into the parameter
12973 pack. Our type is TYPE_PACK_EXPANSION. */
12974 type = expanded_args;
12975 expanded_args = NULL_TREE;
12979 while (i > 0) {
12980 --i;
12982 if (expanded_args)
12983 type = TREE_VEC_ELT (expanded_args, i);
12984 else if (!type)
12985 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12987 if (type == error_mark_node)
12988 return error_mark_node;
12989 if (VOID_TYPE_P (type))
12991 if (complain & tf_error)
12993 error ("invalid parameter type %qT", type);
12994 if (in_decl)
12995 error ("in declaration %q+D", in_decl);
12997 return error_mark_node;
12999 /* DR 657. */
13000 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13001 return error_mark_node;
13003 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13004 top-level qualifiers as required. */
13005 type = cv_unqualified (type_decays_to (type));
13007 /* We do not substitute into default arguments here. The standard
13008 mandates that they be instantiated only when needed, which is
13009 done in build_over_call. */
13010 default_arg = TREE_PURPOSE (arg_types);
13012 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13014 /* We've instantiated a template before its default arguments
13015 have been parsed. This can happen for a nested template
13016 class, and is not an error unless we require the default
13017 argument in a call of this function. */
13018 remaining_arg_types =
13019 tree_cons (default_arg, type, remaining_arg_types);
13020 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13022 else
13023 remaining_arg_types =
13024 hash_tree_cons (default_arg, type, remaining_arg_types);
13027 return remaining_arg_types;
13030 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13031 *not* handle the exception-specification for FNTYPE, because the
13032 initial substitution of explicitly provided template parameters
13033 during argument deduction forbids substitution into the
13034 exception-specification:
13036 [temp.deduct]
13038 All references in the function type of the function template to the
13039 corresponding template parameters are replaced by the specified tem-
13040 plate argument values. If a substitution in a template parameter or
13041 in the function type of the function template results in an invalid
13042 type, type deduction fails. [Note: The equivalent substitution in
13043 exception specifications is done only when the function is instanti-
13044 ated, at which point a program is ill-formed if the substitution
13045 results in an invalid type.] */
13047 static tree
13048 tsubst_function_type (tree t,
13049 tree args,
13050 tsubst_flags_t complain,
13051 tree in_decl)
13053 tree return_type;
13054 tree arg_types = NULL_TREE;
13055 tree fntype;
13057 /* The TYPE_CONTEXT is not used for function/method types. */
13058 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13060 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13061 failure. */
13062 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13064 if (late_return_type_p)
13066 /* Substitute the argument types. */
13067 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13068 complain, in_decl);
13069 if (arg_types == error_mark_node)
13070 return error_mark_node;
13072 tree save_ccp = current_class_ptr;
13073 tree save_ccr = current_class_ref;
13074 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13075 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13076 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13077 if (do_inject)
13079 /* DR 1207: 'this' is in scope in the trailing return type. */
13080 inject_this_parameter (this_type, cp_type_quals (this_type));
13083 /* Substitute the return type. */
13084 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13086 if (do_inject)
13088 current_class_ptr = save_ccp;
13089 current_class_ref = save_ccr;
13092 else
13093 /* Substitute the return type. */
13094 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13096 if (return_type == error_mark_node)
13097 return error_mark_node;
13098 /* DR 486 clarifies that creation of a function type with an
13099 invalid return type is a deduction failure. */
13100 if (TREE_CODE (return_type) == ARRAY_TYPE
13101 || TREE_CODE (return_type) == FUNCTION_TYPE)
13103 if (complain & tf_error)
13105 if (TREE_CODE (return_type) == ARRAY_TYPE)
13106 error ("function returning an array");
13107 else
13108 error ("function returning a function");
13110 return error_mark_node;
13112 /* And DR 657. */
13113 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13114 return error_mark_node;
13116 if (!late_return_type_p)
13118 /* Substitute the argument types. */
13119 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13120 complain, in_decl);
13121 if (arg_types == error_mark_node)
13122 return error_mark_node;
13125 /* Construct a new type node and return it. */
13126 if (TREE_CODE (t) == FUNCTION_TYPE)
13128 fntype = build_function_type (return_type, arg_types);
13129 fntype = apply_memfn_quals (fntype,
13130 type_memfn_quals (t),
13131 type_memfn_rqual (t));
13133 else
13135 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13136 /* Don't pick up extra function qualifiers from the basetype. */
13137 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13138 if (! MAYBE_CLASS_TYPE_P (r))
13140 /* [temp.deduct]
13142 Type deduction may fail for any of the following
13143 reasons:
13145 -- Attempting to create "pointer to member of T" when T
13146 is not a class type. */
13147 if (complain & tf_error)
13148 error ("creating pointer to member function of non-class type %qT",
13150 return error_mark_node;
13153 fntype = build_method_type_directly (r, return_type,
13154 TREE_CHAIN (arg_types));
13155 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13157 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13159 if (late_return_type_p)
13160 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13162 return fntype;
13165 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13166 ARGS into that specification, and return the substituted
13167 specification. If there is no specification, return NULL_TREE. */
13169 static tree
13170 tsubst_exception_specification (tree fntype,
13171 tree args,
13172 tsubst_flags_t complain,
13173 tree in_decl,
13174 bool defer_ok)
13176 tree specs;
13177 tree new_specs;
13179 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13180 new_specs = NULL_TREE;
13181 if (specs && TREE_PURPOSE (specs))
13183 /* A noexcept-specifier. */
13184 tree expr = TREE_PURPOSE (specs);
13185 if (TREE_CODE (expr) == INTEGER_CST)
13186 new_specs = expr;
13187 else if (defer_ok)
13189 /* Defer instantiation of noexcept-specifiers to avoid
13190 excessive instantiations (c++/49107). */
13191 new_specs = make_node (DEFERRED_NOEXCEPT);
13192 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13194 /* We already partially instantiated this member template,
13195 so combine the new args with the old. */
13196 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13197 = DEFERRED_NOEXCEPT_PATTERN (expr);
13198 DEFERRED_NOEXCEPT_ARGS (new_specs)
13199 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13201 else
13203 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13204 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13207 else
13208 new_specs = tsubst_copy_and_build
13209 (expr, args, complain, in_decl, /*function_p=*/false,
13210 /*integral_constant_expression_p=*/true);
13211 new_specs = build_noexcept_spec (new_specs, complain);
13213 else if (specs)
13215 if (! TREE_VALUE (specs))
13216 new_specs = specs;
13217 else
13218 while (specs)
13220 tree spec;
13221 int i, len = 1;
13222 tree expanded_specs = NULL_TREE;
13224 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13226 /* Expand the pack expansion type. */
13227 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13228 args, complain,
13229 in_decl);
13231 if (expanded_specs == error_mark_node)
13232 return error_mark_node;
13233 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13234 len = TREE_VEC_LENGTH (expanded_specs);
13235 else
13237 /* We're substituting into a member template, so
13238 we got a TYPE_PACK_EXPANSION back. Add that
13239 expansion and move on. */
13240 gcc_assert (TREE_CODE (expanded_specs)
13241 == TYPE_PACK_EXPANSION);
13242 new_specs = add_exception_specifier (new_specs,
13243 expanded_specs,
13244 complain);
13245 specs = TREE_CHAIN (specs);
13246 continue;
13250 for (i = 0; i < len; ++i)
13252 if (expanded_specs)
13253 spec = TREE_VEC_ELT (expanded_specs, i);
13254 else
13255 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13256 if (spec == error_mark_node)
13257 return spec;
13258 new_specs = add_exception_specifier (new_specs, spec,
13259 complain);
13262 specs = TREE_CHAIN (specs);
13265 return new_specs;
13268 /* Take the tree structure T and replace template parameters used
13269 therein with the argument vector ARGS. IN_DECL is an associated
13270 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13271 Issue error and warning messages under control of COMPLAIN. Note
13272 that we must be relatively non-tolerant of extensions here, in
13273 order to preserve conformance; if we allow substitutions that
13274 should not be allowed, we may allow argument deductions that should
13275 not succeed, and therefore report ambiguous overload situations
13276 where there are none. In theory, we could allow the substitution,
13277 but indicate that it should have failed, and allow our caller to
13278 make sure that the right thing happens, but we don't try to do this
13279 yet.
13281 This function is used for dealing with types, decls and the like;
13282 for expressions, use tsubst_expr or tsubst_copy. */
13284 tree
13285 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13287 enum tree_code code;
13288 tree type, r = NULL_TREE;
13290 if (t == NULL_TREE || t == error_mark_node
13291 || t == integer_type_node
13292 || t == void_type_node
13293 || t == char_type_node
13294 || t == unknown_type_node
13295 || TREE_CODE (t) == NAMESPACE_DECL
13296 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13297 return t;
13299 if (DECL_P (t))
13300 return tsubst_decl (t, args, complain);
13302 if (args == NULL_TREE)
13303 return t;
13305 code = TREE_CODE (t);
13307 if (code == IDENTIFIER_NODE)
13308 type = IDENTIFIER_TYPE_VALUE (t);
13309 else
13310 type = TREE_TYPE (t);
13312 gcc_assert (type != unknown_type_node);
13314 /* Reuse typedefs. We need to do this to handle dependent attributes,
13315 such as attribute aligned. */
13316 if (TYPE_P (t)
13317 && typedef_variant_p (t))
13319 tree decl = TYPE_NAME (t);
13321 if (alias_template_specialization_p (t))
13323 /* DECL represents an alias template and we want to
13324 instantiate it. */
13325 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13326 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13327 r = instantiate_alias_template (tmpl, gen_args, complain);
13329 else if (DECL_CLASS_SCOPE_P (decl)
13330 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13331 && uses_template_parms (DECL_CONTEXT (decl)))
13333 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13334 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13335 r = retrieve_specialization (tmpl, gen_args, 0);
13337 else if (DECL_FUNCTION_SCOPE_P (decl)
13338 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13339 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13340 r = retrieve_local_specialization (decl);
13341 else
13342 /* The typedef is from a non-template context. */
13343 return t;
13345 if (r)
13347 r = TREE_TYPE (r);
13348 r = cp_build_qualified_type_real
13349 (r, cp_type_quals (t) | cp_type_quals (r),
13350 complain | tf_ignore_bad_quals);
13351 return r;
13353 else
13355 /* We don't have an instantiation yet, so drop the typedef. */
13356 int quals = cp_type_quals (t);
13357 t = DECL_ORIGINAL_TYPE (decl);
13358 t = cp_build_qualified_type_real (t, quals,
13359 complain | tf_ignore_bad_quals);
13363 bool fndecl_type = (complain & tf_fndecl_type);
13364 complain &= ~tf_fndecl_type;
13366 if (type
13367 && code != TYPENAME_TYPE
13368 && code != TEMPLATE_TYPE_PARM
13369 && code != IDENTIFIER_NODE
13370 && code != FUNCTION_TYPE
13371 && code != METHOD_TYPE)
13372 type = tsubst (type, args, complain, in_decl);
13373 if (type == error_mark_node)
13374 return error_mark_node;
13376 switch (code)
13378 case RECORD_TYPE:
13379 case UNION_TYPE:
13380 case ENUMERAL_TYPE:
13381 return tsubst_aggr_type (t, args, complain, in_decl,
13382 /*entering_scope=*/0);
13384 case ERROR_MARK:
13385 case IDENTIFIER_NODE:
13386 case VOID_TYPE:
13387 case REAL_TYPE:
13388 case COMPLEX_TYPE:
13389 case VECTOR_TYPE:
13390 case BOOLEAN_TYPE:
13391 case NULLPTR_TYPE:
13392 case LANG_TYPE:
13393 return t;
13395 case INTEGER_TYPE:
13396 if (t == integer_type_node)
13397 return t;
13399 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13400 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13401 return t;
13404 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13406 max = tsubst_expr (omax, args, complain, in_decl,
13407 /*integral_constant_expression_p=*/false);
13409 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13410 needed. */
13411 if (TREE_CODE (max) == NOP_EXPR
13412 && TREE_SIDE_EFFECTS (omax)
13413 && !TREE_TYPE (max))
13414 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13416 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13417 with TREE_SIDE_EFFECTS that indicates this is not an integral
13418 constant expression. */
13419 if (processing_template_decl
13420 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13422 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13423 TREE_SIDE_EFFECTS (max) = 1;
13426 return compute_array_index_type (NULL_TREE, max, complain);
13429 case TEMPLATE_TYPE_PARM:
13430 case TEMPLATE_TEMPLATE_PARM:
13431 case BOUND_TEMPLATE_TEMPLATE_PARM:
13432 case TEMPLATE_PARM_INDEX:
13434 int idx;
13435 int level;
13436 int levels;
13437 tree arg = NULL_TREE;
13439 /* Early in template argument deduction substitution, we don't
13440 want to reduce the level of 'auto', or it will be confused
13441 with a normal template parm in subsequent deduction. */
13442 if (is_auto (t) && (complain & tf_partial))
13443 return t;
13445 r = NULL_TREE;
13447 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13448 template_parm_level_and_index (t, &level, &idx);
13450 levels = TMPL_ARGS_DEPTH (args);
13451 if (level <= levels
13452 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13454 arg = TMPL_ARG (args, level, idx);
13456 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13458 /* See through ARGUMENT_PACK_SELECT arguments. */
13459 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13460 /* If the selected argument is an expansion E, that most
13461 likely means we were called from
13462 gen_elem_of_pack_expansion_instantiation during the
13463 substituting of pack an argument pack (which Ith
13464 element is a pack expansion, where I is
13465 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13466 In this case, the Ith element resulting from this
13467 substituting is going to be a pack expansion, which
13468 pattern is the pattern of E. Let's return the
13469 pattern of E, and
13470 gen_elem_of_pack_expansion_instantiation will
13471 build the resulting pack expansion from it. */
13472 if (PACK_EXPANSION_P (arg))
13474 /* Make sure we aren't throwing away arg info. */
13475 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13476 arg = PACK_EXPANSION_PATTERN (arg);
13481 if (arg == error_mark_node)
13482 return error_mark_node;
13483 else if (arg != NULL_TREE)
13485 if (ARGUMENT_PACK_P (arg))
13486 /* If ARG is an argument pack, we don't actually want to
13487 perform a substitution here, because substitutions
13488 for argument packs are only done
13489 element-by-element. We can get to this point when
13490 substituting the type of a non-type template
13491 parameter pack, when that type actually contains
13492 template parameter packs from an outer template, e.g.,
13494 template<typename... Types> struct A {
13495 template<Types... Values> struct B { };
13496 }; */
13497 return t;
13499 if (code == TEMPLATE_TYPE_PARM)
13501 int quals;
13502 gcc_assert (TYPE_P (arg));
13504 quals = cp_type_quals (arg) | cp_type_quals (t);
13506 return cp_build_qualified_type_real
13507 (arg, quals, complain | tf_ignore_bad_quals);
13509 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13511 /* We are processing a type constructed from a
13512 template template parameter. */
13513 tree argvec = tsubst (TYPE_TI_ARGS (t),
13514 args, complain, in_decl);
13515 if (argvec == error_mark_node)
13516 return error_mark_node;
13518 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13519 || TREE_CODE (arg) == TEMPLATE_DECL
13520 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13522 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13523 /* Consider this code:
13525 template <template <class> class Template>
13526 struct Internal {
13527 template <class Arg> using Bind = Template<Arg>;
13530 template <template <class> class Template, class Arg>
13531 using Instantiate = Template<Arg>; //#0
13533 template <template <class> class Template,
13534 class Argument>
13535 using Bind =
13536 Instantiate<Internal<Template>::template Bind,
13537 Argument>; //#1
13539 When #1 is parsed, the
13540 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13541 parameter `Template' in #0 matches the
13542 UNBOUND_CLASS_TEMPLATE representing the argument
13543 `Internal<Template>::template Bind'; We then want
13544 to assemble the type `Bind<Argument>' that can't
13545 be fully created right now, because
13546 `Internal<Template>' not being complete, the Bind
13547 template cannot be looked up in that context. So
13548 we need to "store" `Bind<Argument>' for later
13549 when the context of Bind becomes complete. Let's
13550 store that in a TYPENAME_TYPE. */
13551 return make_typename_type (TYPE_CONTEXT (arg),
13552 build_nt (TEMPLATE_ID_EXPR,
13553 TYPE_IDENTIFIER (arg),
13554 argvec),
13555 typename_type,
13556 complain);
13558 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13559 are resolving nested-types in the signature of a
13560 member function templates. Otherwise ARG is a
13561 TEMPLATE_DECL and is the real template to be
13562 instantiated. */
13563 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13564 arg = TYPE_NAME (arg);
13566 r = lookup_template_class (arg,
13567 argvec, in_decl,
13568 DECL_CONTEXT (arg),
13569 /*entering_scope=*/0,
13570 complain);
13571 return cp_build_qualified_type_real
13572 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13574 else if (code == TEMPLATE_TEMPLATE_PARM)
13575 return arg;
13576 else
13577 /* TEMPLATE_PARM_INDEX. */
13578 return convert_from_reference (unshare_expr (arg));
13581 if (level == 1)
13582 /* This can happen during the attempted tsubst'ing in
13583 unify. This means that we don't yet have any information
13584 about the template parameter in question. */
13585 return t;
13587 /* If we get here, we must have been looking at a parm for a
13588 more deeply nested template. Make a new version of this
13589 template parameter, but with a lower level. */
13590 switch (code)
13592 case TEMPLATE_TYPE_PARM:
13593 case TEMPLATE_TEMPLATE_PARM:
13594 case BOUND_TEMPLATE_TEMPLATE_PARM:
13595 if (cp_type_quals (t))
13597 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13598 r = cp_build_qualified_type_real
13599 (r, cp_type_quals (t),
13600 complain | (code == TEMPLATE_TYPE_PARM
13601 ? tf_ignore_bad_quals : 0));
13603 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13604 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13605 && (r = (TEMPLATE_PARM_DESCENDANTS
13606 (TEMPLATE_TYPE_PARM_INDEX (t))))
13607 && (r = TREE_TYPE (r))
13608 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13609 /* Break infinite recursion when substituting the constraints
13610 of a constrained placeholder. */;
13611 else
13613 r = copy_type (t);
13614 TEMPLATE_TYPE_PARM_INDEX (r)
13615 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13616 r, levels, args, complain);
13617 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13618 TYPE_MAIN_VARIANT (r) = r;
13619 TYPE_POINTER_TO (r) = NULL_TREE;
13620 TYPE_REFERENCE_TO (r) = NULL_TREE;
13622 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13624 /* Propagate constraints on placeholders. */
13625 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13626 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13627 = tsubst_constraint (constr, args, complain, in_decl);
13628 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13630 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13631 pl = tsubst (pl, args, complain, in_decl);
13632 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13636 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13637 /* We have reduced the level of the template
13638 template parameter, but not the levels of its
13639 template parameters, so canonical_type_parameter
13640 will not be able to find the canonical template
13641 template parameter for this level. Thus, we
13642 require structural equality checking to compare
13643 TEMPLATE_TEMPLATE_PARMs. */
13644 SET_TYPE_STRUCTURAL_EQUALITY (r);
13645 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13646 SET_TYPE_STRUCTURAL_EQUALITY (r);
13647 else
13648 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13650 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13652 tree tinfo = TYPE_TEMPLATE_INFO (t);
13653 /* We might need to substitute into the types of non-type
13654 template parameters. */
13655 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13656 complain, in_decl);
13657 if (tmpl == error_mark_node)
13658 return error_mark_node;
13659 tree argvec = tsubst (TI_ARGS (tinfo), args,
13660 complain, in_decl);
13661 if (argvec == error_mark_node)
13662 return error_mark_node;
13664 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13665 = build_template_info (tmpl, argvec);
13668 break;
13670 case TEMPLATE_PARM_INDEX:
13671 r = reduce_template_parm_level (t, type, levels, args, complain);
13672 break;
13674 default:
13675 gcc_unreachable ();
13678 return r;
13681 case TREE_LIST:
13683 tree purpose, value, chain;
13685 if (t == void_list_node)
13686 return t;
13688 purpose = TREE_PURPOSE (t);
13689 if (purpose)
13691 purpose = tsubst (purpose, args, complain, in_decl);
13692 if (purpose == error_mark_node)
13693 return error_mark_node;
13695 value = TREE_VALUE (t);
13696 if (value)
13698 value = tsubst (value, args, complain, in_decl);
13699 if (value == error_mark_node)
13700 return error_mark_node;
13702 chain = TREE_CHAIN (t);
13703 if (chain && chain != void_type_node)
13705 chain = tsubst (chain, args, complain, in_decl);
13706 if (chain == error_mark_node)
13707 return error_mark_node;
13709 if (purpose == TREE_PURPOSE (t)
13710 && value == TREE_VALUE (t)
13711 && chain == TREE_CHAIN (t))
13712 return t;
13713 return hash_tree_cons (purpose, value, chain);
13716 case TREE_BINFO:
13717 /* We should never be tsubsting a binfo. */
13718 gcc_unreachable ();
13720 case TREE_VEC:
13721 /* A vector of template arguments. */
13722 gcc_assert (!type);
13723 return tsubst_template_args (t, args, complain, in_decl);
13725 case POINTER_TYPE:
13726 case REFERENCE_TYPE:
13728 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13729 return t;
13731 /* [temp.deduct]
13733 Type deduction may fail for any of the following
13734 reasons:
13736 -- Attempting to create a pointer to reference type.
13737 -- Attempting to create a reference to a reference type or
13738 a reference to void.
13740 Core issue 106 says that creating a reference to a reference
13741 during instantiation is no longer a cause for failure. We
13742 only enforce this check in strict C++98 mode. */
13743 if ((TREE_CODE (type) == REFERENCE_TYPE
13744 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13745 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13747 static location_t last_loc;
13749 /* We keep track of the last time we issued this error
13750 message to avoid spewing a ton of messages during a
13751 single bad template instantiation. */
13752 if (complain & tf_error
13753 && last_loc != input_location)
13755 if (VOID_TYPE_P (type))
13756 error ("forming reference to void");
13757 else if (code == POINTER_TYPE)
13758 error ("forming pointer to reference type %qT", type);
13759 else
13760 error ("forming reference to reference type %qT", type);
13761 last_loc = input_location;
13764 return error_mark_node;
13766 else if (TREE_CODE (type) == FUNCTION_TYPE
13767 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13768 || type_memfn_rqual (type) != REF_QUAL_NONE))
13770 if (complain & tf_error)
13772 if (code == POINTER_TYPE)
13773 error ("forming pointer to qualified function type %qT",
13774 type);
13775 else
13776 error ("forming reference to qualified function type %qT",
13777 type);
13779 return error_mark_node;
13781 else if (code == POINTER_TYPE)
13783 r = build_pointer_type (type);
13784 if (TREE_CODE (type) == METHOD_TYPE)
13785 r = build_ptrmemfunc_type (r);
13787 else if (TREE_CODE (type) == REFERENCE_TYPE)
13788 /* In C++0x, during template argument substitution, when there is an
13789 attempt to create a reference to a reference type, reference
13790 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13792 "If a template-argument for a template-parameter T names a type
13793 that is a reference to a type A, an attempt to create the type
13794 'lvalue reference to cv T' creates the type 'lvalue reference to
13795 A,' while an attempt to create the type type rvalue reference to
13796 cv T' creates the type T"
13798 r = cp_build_reference_type
13799 (TREE_TYPE (type),
13800 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13801 else
13802 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13803 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13805 if (r != error_mark_node)
13806 /* Will this ever be needed for TYPE_..._TO values? */
13807 layout_type (r);
13809 return r;
13811 case OFFSET_TYPE:
13813 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13814 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13816 /* [temp.deduct]
13818 Type deduction may fail for any of the following
13819 reasons:
13821 -- Attempting to create "pointer to member of T" when T
13822 is not a class type. */
13823 if (complain & tf_error)
13824 error ("creating pointer to member of non-class type %qT", r);
13825 return error_mark_node;
13827 if (TREE_CODE (type) == REFERENCE_TYPE)
13829 if (complain & tf_error)
13830 error ("creating pointer to member reference type %qT", type);
13831 return error_mark_node;
13833 if (VOID_TYPE_P (type))
13835 if (complain & tf_error)
13836 error ("creating pointer to member of type void");
13837 return error_mark_node;
13839 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13840 if (TREE_CODE (type) == FUNCTION_TYPE)
13842 /* The type of the implicit object parameter gets its
13843 cv-qualifiers from the FUNCTION_TYPE. */
13844 tree memptr;
13845 tree method_type
13846 = build_memfn_type (type, r, type_memfn_quals (type),
13847 type_memfn_rqual (type));
13848 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13849 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13850 complain);
13852 else
13853 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13854 cp_type_quals (t),
13855 complain);
13857 case FUNCTION_TYPE:
13858 case METHOD_TYPE:
13860 tree fntype;
13861 tree specs;
13862 fntype = tsubst_function_type (t, args, complain, in_decl);
13863 if (fntype == error_mark_node)
13864 return error_mark_node;
13866 /* Substitute the exception specification. */
13867 specs = tsubst_exception_specification (t, args, complain, in_decl,
13868 /*defer_ok*/fndecl_type);
13869 if (specs == error_mark_node)
13870 return error_mark_node;
13871 if (specs)
13872 fntype = build_exception_variant (fntype, specs);
13873 return fntype;
13875 case ARRAY_TYPE:
13877 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13878 if (domain == error_mark_node)
13879 return error_mark_node;
13881 /* As an optimization, we avoid regenerating the array type if
13882 it will obviously be the same as T. */
13883 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13884 return t;
13886 /* These checks should match the ones in create_array_type_for_decl.
13888 [temp.deduct]
13890 The deduction may fail for any of the following reasons:
13892 -- Attempting to create an array with an element type that
13893 is void, a function type, or a reference type, or [DR337]
13894 an abstract class type. */
13895 if (VOID_TYPE_P (type)
13896 || TREE_CODE (type) == FUNCTION_TYPE
13897 || (TREE_CODE (type) == ARRAY_TYPE
13898 && TYPE_DOMAIN (type) == NULL_TREE)
13899 || TREE_CODE (type) == REFERENCE_TYPE)
13901 if (complain & tf_error)
13902 error ("creating array of %qT", type);
13903 return error_mark_node;
13906 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13907 return error_mark_node;
13909 r = build_cplus_array_type (type, domain);
13911 if (TYPE_USER_ALIGN (t))
13913 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13914 TYPE_USER_ALIGN (r) = 1;
13917 return r;
13920 case TYPENAME_TYPE:
13922 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13923 in_decl, /*entering_scope=*/1);
13924 if (ctx == error_mark_node)
13925 return error_mark_node;
13927 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13928 complain, in_decl);
13929 if (f == error_mark_node)
13930 return error_mark_node;
13932 if (!MAYBE_CLASS_TYPE_P (ctx))
13934 if (complain & tf_error)
13935 error ("%qT is not a class, struct, or union type", ctx);
13936 return error_mark_node;
13938 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13940 /* Normally, make_typename_type does not require that the CTX
13941 have complete type in order to allow things like:
13943 template <class T> struct S { typename S<T>::X Y; };
13945 But, such constructs have already been resolved by this
13946 point, so here CTX really should have complete type, unless
13947 it's a partial instantiation. */
13948 ctx = complete_type (ctx);
13949 if (!COMPLETE_TYPE_P (ctx))
13951 if (complain & tf_error)
13952 cxx_incomplete_type_error (NULL_TREE, ctx);
13953 return error_mark_node;
13957 f = make_typename_type (ctx, f, typename_type,
13958 complain | tf_keep_type_decl);
13959 if (f == error_mark_node)
13960 return f;
13961 if (TREE_CODE (f) == TYPE_DECL)
13963 complain |= tf_ignore_bad_quals;
13964 f = TREE_TYPE (f);
13967 if (TREE_CODE (f) != TYPENAME_TYPE)
13969 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13971 if (complain & tf_error)
13972 error ("%qT resolves to %qT, which is not an enumeration type",
13973 t, f);
13974 else
13975 return error_mark_node;
13977 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13979 if (complain & tf_error)
13980 error ("%qT resolves to %qT, which is is not a class type",
13981 t, f);
13982 else
13983 return error_mark_node;
13987 return cp_build_qualified_type_real
13988 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13991 case UNBOUND_CLASS_TEMPLATE:
13993 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13994 in_decl, /*entering_scope=*/1);
13995 tree name = TYPE_IDENTIFIER (t);
13996 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13998 if (ctx == error_mark_node || name == error_mark_node)
13999 return error_mark_node;
14001 if (parm_list)
14002 parm_list = tsubst_template_parms (parm_list, args, complain);
14003 return make_unbound_class_template (ctx, name, parm_list, complain);
14006 case TYPEOF_TYPE:
14008 tree type;
14010 ++cp_unevaluated_operand;
14011 ++c_inhibit_evaluation_warnings;
14013 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14014 complain, in_decl,
14015 /*integral_constant_expression_p=*/false);
14017 --cp_unevaluated_operand;
14018 --c_inhibit_evaluation_warnings;
14020 type = finish_typeof (type);
14021 return cp_build_qualified_type_real (type,
14022 cp_type_quals (t)
14023 | cp_type_quals (type),
14024 complain);
14027 case DECLTYPE_TYPE:
14029 tree type;
14031 ++cp_unevaluated_operand;
14032 ++c_inhibit_evaluation_warnings;
14034 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14035 complain|tf_decltype, in_decl,
14036 /*function_p*/false,
14037 /*integral_constant_expression*/false);
14039 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14041 if (type == NULL_TREE)
14043 if (complain & tf_error)
14044 error ("empty initializer in lambda init-capture");
14045 type = error_mark_node;
14047 else if (TREE_CODE (type) == TREE_LIST)
14048 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14051 --cp_unevaluated_operand;
14052 --c_inhibit_evaluation_warnings;
14054 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14055 type = lambda_capture_field_type (type,
14056 DECLTYPE_FOR_INIT_CAPTURE (t),
14057 DECLTYPE_FOR_REF_CAPTURE (t));
14058 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14059 type = lambda_proxy_type (type);
14060 else
14062 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14063 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14064 && EXPR_P (type))
14065 /* In a template ~id could be either a complement expression
14066 or an unqualified-id naming a destructor; if instantiating
14067 it produces an expression, it's not an id-expression or
14068 member access. */
14069 id = false;
14070 type = finish_decltype_type (type, id, complain);
14072 return cp_build_qualified_type_real (type,
14073 cp_type_quals (t)
14074 | cp_type_quals (type),
14075 complain | tf_ignore_bad_quals);
14078 case UNDERLYING_TYPE:
14080 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14081 complain, in_decl);
14082 return finish_underlying_type (type);
14085 case TYPE_ARGUMENT_PACK:
14086 case NONTYPE_ARGUMENT_PACK:
14088 tree r;
14090 if (code == NONTYPE_ARGUMENT_PACK)
14092 r = make_node (code);
14093 /* Set the already-substituted type. */
14094 TREE_TYPE (r) = type;
14096 else
14097 r = cxx_make_type (code);
14099 tree pack_args = ARGUMENT_PACK_ARGS (t);
14100 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14101 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14103 return r;
14106 case VOID_CST:
14107 case INTEGER_CST:
14108 case REAL_CST:
14109 case STRING_CST:
14110 case PLUS_EXPR:
14111 case MINUS_EXPR:
14112 case NEGATE_EXPR:
14113 case NOP_EXPR:
14114 case INDIRECT_REF:
14115 case ADDR_EXPR:
14116 case CALL_EXPR:
14117 case ARRAY_REF:
14118 case SCOPE_REF:
14119 /* We should use one of the expression tsubsts for these codes. */
14120 gcc_unreachable ();
14122 default:
14123 sorry ("use of %qs in template", get_tree_code_name (code));
14124 return error_mark_node;
14128 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14129 type of the expression on the left-hand side of the "." or "->"
14130 operator. */
14132 static tree
14133 tsubst_baselink (tree baselink, tree object_type,
14134 tree args, tsubst_flags_t complain, tree in_decl)
14136 tree name;
14137 tree qualifying_scope;
14138 tree fns;
14139 tree optype;
14140 tree template_args = 0;
14141 bool template_id_p = false;
14142 bool qualified = BASELINK_QUALIFIED_P (baselink);
14144 /* A baselink indicates a function from a base class. Both the
14145 BASELINK_ACCESS_BINFO and the base class referenced may
14146 indicate bases of the template class, rather than the
14147 instantiated class. In addition, lookups that were not
14148 ambiguous before may be ambiguous now. Therefore, we perform
14149 the lookup again. */
14150 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14151 qualifying_scope = tsubst (qualifying_scope, args,
14152 complain, in_decl);
14153 fns = BASELINK_FUNCTIONS (baselink);
14154 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
14155 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14157 template_id_p = true;
14158 template_args = TREE_OPERAND (fns, 1);
14159 fns = TREE_OPERAND (fns, 0);
14160 if (template_args)
14161 template_args = tsubst_template_args (template_args, args,
14162 complain, in_decl);
14164 name = DECL_NAME (get_first_fn (fns));
14165 if (IDENTIFIER_TYPENAME_P (name))
14166 name = mangle_conv_op_name_for_type (optype);
14167 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14168 if (!baselink)
14170 if (constructor_name_p (name, qualifying_scope))
14172 if (complain & tf_error)
14173 error ("cannot call constructor %<%T::%D%> directly",
14174 qualifying_scope, name);
14176 return error_mark_node;
14179 /* If lookup found a single function, mark it as used at this
14180 point. (If it lookup found multiple functions the one selected
14181 later by overload resolution will be marked as used at that
14182 point.) */
14183 if (BASELINK_P (baselink))
14184 fns = BASELINK_FUNCTIONS (baselink);
14185 if (!template_id_p && !really_overloaded_fn (fns)
14186 && !mark_used (OVL_CURRENT (fns), complain) && !(complain & tf_error))
14187 return error_mark_node;
14189 /* Add back the template arguments, if present. */
14190 if (BASELINK_P (baselink) && template_id_p)
14191 BASELINK_FUNCTIONS (baselink)
14192 = build2 (TEMPLATE_ID_EXPR,
14193 unknown_type_node,
14194 BASELINK_FUNCTIONS (baselink),
14195 template_args);
14196 /* Update the conversion operator type. */
14197 if (BASELINK_P (baselink))
14198 BASELINK_OPTYPE (baselink) = optype;
14200 if (!object_type)
14201 object_type = current_class_type;
14203 if (qualified || name == complete_dtor_identifier)
14205 baselink = adjust_result_of_qualified_name_lookup (baselink,
14206 qualifying_scope,
14207 object_type);
14208 if (!qualified)
14209 /* We need to call adjust_result_of_qualified_name_lookup in case the
14210 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14211 so that we still get virtual function binding. */
14212 BASELINK_QUALIFIED_P (baselink) = false;
14214 return baselink;
14217 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14218 true if the qualified-id will be a postfix-expression in-and-of
14219 itself; false if more of the postfix-expression follows the
14220 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14221 of "&". */
14223 static tree
14224 tsubst_qualified_id (tree qualified_id, tree args,
14225 tsubst_flags_t complain, tree in_decl,
14226 bool done, bool address_p)
14228 tree expr;
14229 tree scope;
14230 tree name;
14231 bool is_template;
14232 tree template_args;
14233 location_t loc = UNKNOWN_LOCATION;
14235 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14237 /* Figure out what name to look up. */
14238 name = TREE_OPERAND (qualified_id, 1);
14239 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14241 is_template = true;
14242 loc = EXPR_LOCATION (name);
14243 template_args = TREE_OPERAND (name, 1);
14244 if (template_args)
14245 template_args = tsubst_template_args (template_args, args,
14246 complain, in_decl);
14247 if (template_args == error_mark_node)
14248 return error_mark_node;
14249 name = TREE_OPERAND (name, 0);
14251 else
14253 is_template = false;
14254 template_args = NULL_TREE;
14257 /* Substitute into the qualifying scope. When there are no ARGS, we
14258 are just trying to simplify a non-dependent expression. In that
14259 case the qualifying scope may be dependent, and, in any case,
14260 substituting will not help. */
14261 scope = TREE_OPERAND (qualified_id, 0);
14262 if (args)
14264 scope = tsubst (scope, args, complain, in_decl);
14265 expr = tsubst_copy (name, args, complain, in_decl);
14267 else
14268 expr = name;
14270 if (dependent_scope_p (scope))
14272 if (is_template)
14273 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14274 tree r = build_qualified_name (NULL_TREE, scope, expr,
14275 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14276 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14277 return r;
14280 if (!BASELINK_P (name) && !DECL_P (expr))
14282 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14284 /* A BIT_NOT_EXPR is used to represent a destructor. */
14285 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14287 error ("qualifying type %qT does not match destructor name ~%qT",
14288 scope, TREE_OPERAND (expr, 0));
14289 expr = error_mark_node;
14291 else
14292 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14293 /*is_type_p=*/0, false);
14295 else
14296 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14297 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14298 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14300 if (complain & tf_error)
14302 error ("dependent-name %qE is parsed as a non-type, but "
14303 "instantiation yields a type", qualified_id);
14304 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14306 return error_mark_node;
14310 if (DECL_P (expr))
14312 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14313 scope);
14314 /* Remember that there was a reference to this entity. */
14315 if (!mark_used (expr, complain) && !(complain & tf_error))
14316 return error_mark_node;
14319 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14321 if (complain & tf_error)
14322 qualified_name_lookup_error (scope,
14323 TREE_OPERAND (qualified_id, 1),
14324 expr, input_location);
14325 return error_mark_node;
14328 if (is_template)
14330 if (variable_template_p (expr))
14331 expr = lookup_and_finish_template_variable (expr, template_args,
14332 complain);
14333 else
14334 expr = lookup_template_function (expr, template_args);
14337 if (expr == error_mark_node && complain & tf_error)
14338 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14339 expr, input_location);
14340 else if (TYPE_P (scope))
14342 expr = (adjust_result_of_qualified_name_lookup
14343 (expr, scope, current_nonlambda_class_type ()));
14344 expr = (finish_qualified_id_expr
14345 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14346 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14347 /*template_arg_p=*/false, complain));
14350 /* Expressions do not generally have reference type. */
14351 if (TREE_CODE (expr) != SCOPE_REF
14352 /* However, if we're about to form a pointer-to-member, we just
14353 want the referenced member referenced. */
14354 && TREE_CODE (expr) != OFFSET_REF)
14355 expr = convert_from_reference (expr);
14357 if (REF_PARENTHESIZED_P (qualified_id))
14358 expr = force_paren_expr (expr);
14360 return expr;
14363 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14364 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14365 for tsubst. */
14367 static tree
14368 tsubst_init (tree init, tree decl, tree args,
14369 tsubst_flags_t complain, tree in_decl)
14371 if (!init)
14372 return NULL_TREE;
14374 init = tsubst_expr (init, args, complain, in_decl, false);
14376 if (!init && TREE_TYPE (decl) != error_mark_node)
14378 /* If we had an initializer but it
14379 instantiated to nothing,
14380 value-initialize the object. This will
14381 only occur when the initializer was a
14382 pack expansion where the parameter packs
14383 used in that expansion were of length
14384 zero. */
14385 init = build_value_init (TREE_TYPE (decl),
14386 complain);
14387 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14388 init = get_target_expr_sfinae (init, complain);
14391 return init;
14394 /* Like tsubst, but deals with expressions. This function just replaces
14395 template parms; to finish processing the resultant expression, use
14396 tsubst_copy_and_build or tsubst_expr. */
14398 static tree
14399 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14401 enum tree_code code;
14402 tree r;
14404 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14405 return t;
14407 code = TREE_CODE (t);
14409 switch (code)
14411 case PARM_DECL:
14412 r = retrieve_local_specialization (t);
14414 if (r == NULL_TREE)
14416 /* We get here for a use of 'this' in an NSDMI as part of a
14417 constructor call or as part of an aggregate initialization. */
14418 if (DECL_NAME (t) == this_identifier
14419 && ((current_function_decl
14420 && DECL_CONSTRUCTOR_P (current_function_decl))
14421 || (current_class_ref
14422 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14423 return current_class_ptr;
14425 /* This can happen for a parameter name used later in a function
14426 declaration (such as in a late-specified return type). Just
14427 make a dummy decl, since it's only used for its type. */
14428 gcc_assert (cp_unevaluated_operand != 0);
14429 r = tsubst_decl (t, args, complain);
14430 /* Give it the template pattern as its context; its true context
14431 hasn't been instantiated yet and this is good enough for
14432 mangling. */
14433 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14436 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14437 r = ARGUMENT_PACK_SELECT_ARG (r);
14438 if (!mark_used (r, complain) && !(complain & tf_error))
14439 return error_mark_node;
14440 return r;
14442 case CONST_DECL:
14444 tree enum_type;
14445 tree v;
14447 if (DECL_TEMPLATE_PARM_P (t))
14448 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14449 /* There is no need to substitute into namespace-scope
14450 enumerators. */
14451 if (DECL_NAMESPACE_SCOPE_P (t))
14452 return t;
14453 /* If ARGS is NULL, then T is known to be non-dependent. */
14454 if (args == NULL_TREE)
14455 return scalar_constant_value (t);
14457 /* Unfortunately, we cannot just call lookup_name here.
14458 Consider:
14460 template <int I> int f() {
14461 enum E { a = I };
14462 struct S { void g() { E e = a; } };
14465 When we instantiate f<7>::S::g(), say, lookup_name is not
14466 clever enough to find f<7>::a. */
14467 enum_type
14468 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14469 /*entering_scope=*/0);
14471 for (v = TYPE_VALUES (enum_type);
14472 v != NULL_TREE;
14473 v = TREE_CHAIN (v))
14474 if (TREE_PURPOSE (v) == DECL_NAME (t))
14475 return TREE_VALUE (v);
14477 /* We didn't find the name. That should never happen; if
14478 name-lookup found it during preliminary parsing, we
14479 should find it again here during instantiation. */
14480 gcc_unreachable ();
14482 return t;
14484 case FIELD_DECL:
14485 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14487 /* Check for a local specialization set up by
14488 tsubst_pack_expansion. */
14489 if (tree r = retrieve_local_specialization (t))
14491 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14492 r = ARGUMENT_PACK_SELECT_ARG (r);
14493 return r;
14496 /* When retrieving a capture pack from a generic lambda, remove the
14497 lambda call op's own template argument list from ARGS. Only the
14498 template arguments active for the closure type should be used to
14499 retrieve the pack specialization. */
14500 if (LAMBDA_FUNCTION_P (current_function_decl)
14501 && (template_class_depth (DECL_CONTEXT (t))
14502 != TMPL_ARGS_DEPTH (args)))
14503 args = strip_innermost_template_args (args, 1);
14505 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14506 tsubst_decl put in the hash table. */
14507 return retrieve_specialization (t, args, 0);
14510 if (DECL_CONTEXT (t))
14512 tree ctx;
14514 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14515 /*entering_scope=*/1);
14516 if (ctx != DECL_CONTEXT (t))
14518 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14519 if (!r)
14521 if (complain & tf_error)
14522 error ("using invalid field %qD", t);
14523 return error_mark_node;
14525 return r;
14529 return t;
14531 case VAR_DECL:
14532 case FUNCTION_DECL:
14533 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14534 r = tsubst (t, args, complain, in_decl);
14535 else if (local_variable_p (t)
14536 && uses_template_parms (DECL_CONTEXT (t)))
14538 r = retrieve_local_specialization (t);
14539 if (r == NULL_TREE)
14541 /* First try name lookup to find the instantiation. */
14542 r = lookup_name (DECL_NAME (t));
14543 if (r)
14545 /* Make sure that the one we found is the one we want. */
14546 tree ctx = DECL_CONTEXT (t);
14547 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14548 ctx = tsubst (ctx, args, complain, in_decl);
14549 if (ctx != DECL_CONTEXT (r))
14550 r = NULL_TREE;
14553 if (r)
14554 /* OK */;
14555 else
14557 /* This can happen for a variable used in a
14558 late-specified return type of a local lambda, or for a
14559 local static or constant. Building a new VAR_DECL
14560 should be OK in all those cases. */
14561 r = tsubst_decl (t, args, complain);
14562 if (decl_maybe_constant_var_p (r))
14564 /* We can't call cp_finish_decl, so handle the
14565 initializer by hand. */
14566 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14567 complain, in_decl);
14568 if (!processing_template_decl)
14569 init = maybe_constant_init (init);
14570 if (processing_template_decl
14571 ? potential_constant_expression (init)
14572 : reduced_constant_expression_p (init))
14573 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14574 = TREE_CONSTANT (r) = true;
14575 DECL_INITIAL (r) = init;
14577 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14578 || decl_constant_var_p (r)
14579 || errorcount || sorrycount);
14580 if (!processing_template_decl
14581 && !TREE_STATIC (r))
14582 r = process_outer_var_ref (r, complain);
14584 /* Remember this for subsequent uses. */
14585 if (local_specializations)
14586 register_local_specialization (r, t);
14589 else
14590 r = t;
14591 if (!mark_used (r, complain))
14592 return error_mark_node;
14593 return r;
14595 case NAMESPACE_DECL:
14596 return t;
14598 case OVERLOAD:
14599 /* An OVERLOAD will always be a non-dependent overload set; an
14600 overload set from function scope will just be represented with an
14601 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14602 gcc_assert (!uses_template_parms (t));
14603 return t;
14605 case BASELINK:
14606 return tsubst_baselink (t, current_nonlambda_class_type (),
14607 args, complain, in_decl);
14609 case TEMPLATE_DECL:
14610 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14611 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14612 args, complain, in_decl);
14613 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14614 return tsubst (t, args, complain, in_decl);
14615 else if (DECL_CLASS_SCOPE_P (t)
14616 && uses_template_parms (DECL_CONTEXT (t)))
14618 /* Template template argument like the following example need
14619 special treatment:
14621 template <template <class> class TT> struct C {};
14622 template <class T> struct D {
14623 template <class U> struct E {};
14624 C<E> c; // #1
14626 D<int> d; // #2
14628 We are processing the template argument `E' in #1 for
14629 the template instantiation #2. Originally, `E' is a
14630 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14631 have to substitute this with one having context `D<int>'. */
14633 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14634 return lookup_field (context, DECL_NAME(t), 0, false);
14636 else
14637 /* Ordinary template template argument. */
14638 return t;
14640 case CAST_EXPR:
14641 case REINTERPRET_CAST_EXPR:
14642 case CONST_CAST_EXPR:
14643 case STATIC_CAST_EXPR:
14644 case DYNAMIC_CAST_EXPR:
14645 case IMPLICIT_CONV_EXPR:
14646 case CONVERT_EXPR:
14647 case NOP_EXPR:
14649 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14650 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14651 return build1 (code, type, op0);
14654 case SIZEOF_EXPR:
14655 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14656 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14658 tree expanded, op = TREE_OPERAND (t, 0);
14659 int len = 0;
14661 if (SIZEOF_EXPR_TYPE_P (t))
14662 op = TREE_TYPE (op);
14664 ++cp_unevaluated_operand;
14665 ++c_inhibit_evaluation_warnings;
14666 /* We only want to compute the number of arguments. */
14667 if (PACK_EXPANSION_P (op))
14668 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14669 else
14670 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14671 args, complain, in_decl);
14672 --cp_unevaluated_operand;
14673 --c_inhibit_evaluation_warnings;
14675 if (TREE_CODE (expanded) == TREE_VEC)
14677 len = TREE_VEC_LENGTH (expanded);
14678 /* Set TREE_USED for the benefit of -Wunused. */
14679 for (int i = 0; i < len; i++)
14680 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14681 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14684 if (expanded == error_mark_node)
14685 return error_mark_node;
14686 else if (PACK_EXPANSION_P (expanded)
14687 || (TREE_CODE (expanded) == TREE_VEC
14688 && pack_expansion_args_count (expanded)))
14691 if (PACK_EXPANSION_P (expanded))
14692 /* OK. */;
14693 else if (TREE_VEC_LENGTH (expanded) == 1)
14694 expanded = TREE_VEC_ELT (expanded, 0);
14695 else
14696 expanded = make_argument_pack (expanded);
14698 if (TYPE_P (expanded))
14699 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14700 complain & tf_error);
14701 else
14702 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14703 complain & tf_error);
14705 else
14706 return build_int_cst (size_type_node, len);
14708 if (SIZEOF_EXPR_TYPE_P (t))
14710 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14711 args, complain, in_decl);
14712 r = build1 (NOP_EXPR, r, error_mark_node);
14713 r = build1 (SIZEOF_EXPR,
14714 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14715 SIZEOF_EXPR_TYPE_P (r) = 1;
14716 return r;
14718 /* Fall through */
14720 case INDIRECT_REF:
14721 case NEGATE_EXPR:
14722 case TRUTH_NOT_EXPR:
14723 case BIT_NOT_EXPR:
14724 case ADDR_EXPR:
14725 case UNARY_PLUS_EXPR: /* Unary + */
14726 case ALIGNOF_EXPR:
14727 case AT_ENCODE_EXPR:
14728 case ARROW_EXPR:
14729 case THROW_EXPR:
14730 case TYPEID_EXPR:
14731 case REALPART_EXPR:
14732 case IMAGPART_EXPR:
14733 case PAREN_EXPR:
14735 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14736 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14737 return build1 (code, type, op0);
14740 case COMPONENT_REF:
14742 tree object;
14743 tree name;
14745 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14746 name = TREE_OPERAND (t, 1);
14747 if (TREE_CODE (name) == BIT_NOT_EXPR)
14749 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14750 complain, in_decl);
14751 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14753 else if (TREE_CODE (name) == SCOPE_REF
14754 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14756 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14757 complain, in_decl);
14758 name = TREE_OPERAND (name, 1);
14759 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14760 complain, in_decl);
14761 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14762 name = build_qualified_name (/*type=*/NULL_TREE,
14763 base, name,
14764 /*template_p=*/false);
14766 else if (BASELINK_P (name))
14767 name = tsubst_baselink (name,
14768 non_reference (TREE_TYPE (object)),
14769 args, complain,
14770 in_decl);
14771 else
14772 name = tsubst_copy (name, args, complain, in_decl);
14773 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14776 case PLUS_EXPR:
14777 case MINUS_EXPR:
14778 case MULT_EXPR:
14779 case TRUNC_DIV_EXPR:
14780 case CEIL_DIV_EXPR:
14781 case FLOOR_DIV_EXPR:
14782 case ROUND_DIV_EXPR:
14783 case EXACT_DIV_EXPR:
14784 case BIT_AND_EXPR:
14785 case BIT_IOR_EXPR:
14786 case BIT_XOR_EXPR:
14787 case TRUNC_MOD_EXPR:
14788 case FLOOR_MOD_EXPR:
14789 case TRUTH_ANDIF_EXPR:
14790 case TRUTH_ORIF_EXPR:
14791 case TRUTH_AND_EXPR:
14792 case TRUTH_OR_EXPR:
14793 case RSHIFT_EXPR:
14794 case LSHIFT_EXPR:
14795 case RROTATE_EXPR:
14796 case LROTATE_EXPR:
14797 case EQ_EXPR:
14798 case NE_EXPR:
14799 case MAX_EXPR:
14800 case MIN_EXPR:
14801 case LE_EXPR:
14802 case GE_EXPR:
14803 case LT_EXPR:
14804 case GT_EXPR:
14805 case COMPOUND_EXPR:
14806 case DOTSTAR_EXPR:
14807 case MEMBER_REF:
14808 case PREDECREMENT_EXPR:
14809 case PREINCREMENT_EXPR:
14810 case POSTDECREMENT_EXPR:
14811 case POSTINCREMENT_EXPR:
14813 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14814 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14815 return build_nt (code, op0, op1);
14818 case SCOPE_REF:
14820 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14821 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14822 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14823 QUALIFIED_NAME_IS_TEMPLATE (t));
14826 case ARRAY_REF:
14828 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14829 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14830 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14833 case CALL_EXPR:
14835 int n = VL_EXP_OPERAND_LENGTH (t);
14836 tree result = build_vl_exp (CALL_EXPR, n);
14837 int i;
14838 for (i = 0; i < n; i++)
14839 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14840 complain, in_decl);
14841 return result;
14844 case COND_EXPR:
14845 case MODOP_EXPR:
14846 case PSEUDO_DTOR_EXPR:
14847 case VEC_PERM_EXPR:
14849 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14850 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14851 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14852 r = build_nt (code, op0, op1, op2);
14853 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14854 return r;
14857 case NEW_EXPR:
14859 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14860 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14861 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14862 r = build_nt (code, op0, op1, op2);
14863 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14864 return r;
14867 case DELETE_EXPR:
14869 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14870 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14871 r = build_nt (code, op0, op1);
14872 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14873 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14874 return r;
14877 case TEMPLATE_ID_EXPR:
14879 /* Substituted template arguments */
14880 tree fn = TREE_OPERAND (t, 0);
14881 tree targs = TREE_OPERAND (t, 1);
14883 fn = tsubst_copy (fn, args, complain, in_decl);
14884 if (targs)
14885 targs = tsubst_template_args (targs, args, complain, in_decl);
14887 return lookup_template_function (fn, targs);
14890 case TREE_LIST:
14892 tree purpose, value, chain;
14894 if (t == void_list_node)
14895 return t;
14897 purpose = TREE_PURPOSE (t);
14898 if (purpose)
14899 purpose = tsubst_copy (purpose, args, complain, in_decl);
14900 value = TREE_VALUE (t);
14901 if (value)
14902 value = tsubst_copy (value, args, complain, in_decl);
14903 chain = TREE_CHAIN (t);
14904 if (chain && chain != void_type_node)
14905 chain = tsubst_copy (chain, args, complain, in_decl);
14906 if (purpose == TREE_PURPOSE (t)
14907 && value == TREE_VALUE (t)
14908 && chain == TREE_CHAIN (t))
14909 return t;
14910 return tree_cons (purpose, value, chain);
14913 case RECORD_TYPE:
14914 case UNION_TYPE:
14915 case ENUMERAL_TYPE:
14916 case INTEGER_TYPE:
14917 case TEMPLATE_TYPE_PARM:
14918 case TEMPLATE_TEMPLATE_PARM:
14919 case BOUND_TEMPLATE_TEMPLATE_PARM:
14920 case TEMPLATE_PARM_INDEX:
14921 case POINTER_TYPE:
14922 case REFERENCE_TYPE:
14923 case OFFSET_TYPE:
14924 case FUNCTION_TYPE:
14925 case METHOD_TYPE:
14926 case ARRAY_TYPE:
14927 case TYPENAME_TYPE:
14928 case UNBOUND_CLASS_TEMPLATE:
14929 case TYPEOF_TYPE:
14930 case DECLTYPE_TYPE:
14931 case TYPE_DECL:
14932 return tsubst (t, args, complain, in_decl);
14934 case USING_DECL:
14935 t = DECL_NAME (t);
14936 /* Fall through. */
14937 case IDENTIFIER_NODE:
14938 if (IDENTIFIER_TYPENAME_P (t))
14940 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14941 return mangle_conv_op_name_for_type (new_type);
14943 else
14944 return t;
14946 case CONSTRUCTOR:
14947 /* This is handled by tsubst_copy_and_build. */
14948 gcc_unreachable ();
14950 case VA_ARG_EXPR:
14952 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14953 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14954 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14957 case CLEANUP_POINT_EXPR:
14958 /* We shouldn't have built any of these during initial template
14959 generation. Instead, they should be built during instantiation
14960 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14961 gcc_unreachable ();
14963 case OFFSET_REF:
14965 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14966 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14967 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14968 r = build2 (code, type, op0, op1);
14969 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14970 if (!mark_used (TREE_OPERAND (r, 1), complain)
14971 && !(complain & tf_error))
14972 return error_mark_node;
14973 return r;
14976 case EXPR_PACK_EXPANSION:
14977 error ("invalid use of pack expansion expression");
14978 return error_mark_node;
14980 case NONTYPE_ARGUMENT_PACK:
14981 error ("use %<...%> to expand argument pack");
14982 return error_mark_node;
14984 case VOID_CST:
14985 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14986 return t;
14988 case INTEGER_CST:
14989 case REAL_CST:
14990 case STRING_CST:
14991 case COMPLEX_CST:
14993 /* Instantiate any typedefs in the type. */
14994 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14995 r = fold_convert (type, t);
14996 gcc_assert (TREE_CODE (r) == code);
14997 return r;
15000 case PTRMEM_CST:
15001 /* These can sometimes show up in a partial instantiation, but never
15002 involve template parms. */
15003 gcc_assert (!uses_template_parms (t));
15004 return t;
15006 case UNARY_LEFT_FOLD_EXPR:
15007 return tsubst_unary_left_fold (t, args, complain, in_decl);
15008 case UNARY_RIGHT_FOLD_EXPR:
15009 return tsubst_unary_right_fold (t, args, complain, in_decl);
15010 case BINARY_LEFT_FOLD_EXPR:
15011 return tsubst_binary_left_fold (t, args, complain, in_decl);
15012 case BINARY_RIGHT_FOLD_EXPR:
15013 return tsubst_binary_right_fold (t, args, complain, in_decl);
15015 default:
15016 /* We shouldn't get here, but keep going if !flag_checking. */
15017 if (flag_checking)
15018 gcc_unreachable ();
15019 return t;
15023 /* Helper function for tsubst_omp_clauses, used for instantiation of
15024 OMP_CLAUSE_DECL of clauses. */
15026 static tree
15027 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15028 tree in_decl)
15030 if (decl == NULL_TREE)
15031 return NULL_TREE;
15033 /* Handle an OpenMP array section represented as a TREE_LIST (or
15034 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15035 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15036 TREE_LIST. We can handle it exactly the same as an array section
15037 (purpose, value, and a chain), even though the nomenclature
15038 (low_bound, length, etc) is different. */
15039 if (TREE_CODE (decl) == TREE_LIST)
15041 tree low_bound
15042 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15043 /*integral_constant_expression_p=*/false);
15044 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15045 /*integral_constant_expression_p=*/false);
15046 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15047 in_decl);
15048 if (TREE_PURPOSE (decl) == low_bound
15049 && TREE_VALUE (decl) == length
15050 && TREE_CHAIN (decl) == chain)
15051 return decl;
15052 tree ret = tree_cons (low_bound, length, chain);
15053 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15054 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15055 return ret;
15057 tree ret = tsubst_expr (decl, args, complain, in_decl,
15058 /*integral_constant_expression_p=*/false);
15059 /* Undo convert_from_reference tsubst_expr could have called. */
15060 if (decl
15061 && REFERENCE_REF_P (ret)
15062 && !REFERENCE_REF_P (decl))
15063 ret = TREE_OPERAND (ret, 0);
15064 return ret;
15067 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15069 static tree
15070 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15071 tree args, tsubst_flags_t complain, tree in_decl)
15073 tree new_clauses = NULL_TREE, nc, oc;
15074 tree linear_no_step = NULL_TREE;
15076 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15078 nc = copy_node (oc);
15079 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15080 new_clauses = nc;
15082 switch (OMP_CLAUSE_CODE (nc))
15084 case OMP_CLAUSE_LASTPRIVATE:
15085 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15087 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15088 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15089 in_decl, /*integral_constant_expression_p=*/false);
15090 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15091 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15093 /* FALLTHRU */
15094 case OMP_CLAUSE_PRIVATE:
15095 case OMP_CLAUSE_SHARED:
15096 case OMP_CLAUSE_FIRSTPRIVATE:
15097 case OMP_CLAUSE_COPYIN:
15098 case OMP_CLAUSE_COPYPRIVATE:
15099 case OMP_CLAUSE_UNIFORM:
15100 case OMP_CLAUSE_DEPEND:
15101 case OMP_CLAUSE_FROM:
15102 case OMP_CLAUSE_TO:
15103 case OMP_CLAUSE_MAP:
15104 case OMP_CLAUSE_USE_DEVICE_PTR:
15105 case OMP_CLAUSE_IS_DEVICE_PTR:
15106 OMP_CLAUSE_DECL (nc)
15107 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15108 in_decl);
15109 break;
15110 case OMP_CLAUSE_TILE:
15111 case OMP_CLAUSE_IF:
15112 case OMP_CLAUSE_NUM_THREADS:
15113 case OMP_CLAUSE_SCHEDULE:
15114 case OMP_CLAUSE_COLLAPSE:
15115 case OMP_CLAUSE_FINAL:
15116 case OMP_CLAUSE_DEVICE:
15117 case OMP_CLAUSE_DIST_SCHEDULE:
15118 case OMP_CLAUSE_NUM_TEAMS:
15119 case OMP_CLAUSE_THREAD_LIMIT:
15120 case OMP_CLAUSE_SAFELEN:
15121 case OMP_CLAUSE_SIMDLEN:
15122 case OMP_CLAUSE_NUM_TASKS:
15123 case OMP_CLAUSE_GRAINSIZE:
15124 case OMP_CLAUSE_PRIORITY:
15125 case OMP_CLAUSE_ORDERED:
15126 case OMP_CLAUSE_HINT:
15127 case OMP_CLAUSE_NUM_GANGS:
15128 case OMP_CLAUSE_NUM_WORKERS:
15129 case OMP_CLAUSE_VECTOR_LENGTH:
15130 case OMP_CLAUSE_WORKER:
15131 case OMP_CLAUSE_VECTOR:
15132 case OMP_CLAUSE_ASYNC:
15133 case OMP_CLAUSE_WAIT:
15134 OMP_CLAUSE_OPERAND (nc, 0)
15135 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15136 in_decl, /*integral_constant_expression_p=*/false);
15137 break;
15138 case OMP_CLAUSE_REDUCTION:
15139 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15141 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15142 if (TREE_CODE (placeholder) == SCOPE_REF)
15144 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15145 complain, in_decl);
15146 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15147 = build_qualified_name (NULL_TREE, scope,
15148 TREE_OPERAND (placeholder, 1),
15149 false);
15151 else
15152 gcc_assert (identifier_p (placeholder));
15154 OMP_CLAUSE_DECL (nc)
15155 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15156 in_decl);
15157 break;
15158 case OMP_CLAUSE_GANG:
15159 case OMP_CLAUSE_ALIGNED:
15160 OMP_CLAUSE_DECL (nc)
15161 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15162 in_decl);
15163 OMP_CLAUSE_OPERAND (nc, 1)
15164 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15165 in_decl, /*integral_constant_expression_p=*/false);
15166 break;
15167 case OMP_CLAUSE_LINEAR:
15168 OMP_CLAUSE_DECL (nc)
15169 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15170 in_decl);
15171 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15173 gcc_assert (!linear_no_step);
15174 linear_no_step = nc;
15176 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15177 OMP_CLAUSE_LINEAR_STEP (nc)
15178 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15179 complain, in_decl);
15180 else
15181 OMP_CLAUSE_LINEAR_STEP (nc)
15182 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15183 in_decl,
15184 /*integral_constant_expression_p=*/false);
15185 break;
15186 case OMP_CLAUSE_NOWAIT:
15187 case OMP_CLAUSE_DEFAULT:
15188 case OMP_CLAUSE_UNTIED:
15189 case OMP_CLAUSE_MERGEABLE:
15190 case OMP_CLAUSE_INBRANCH:
15191 case OMP_CLAUSE_NOTINBRANCH:
15192 case OMP_CLAUSE_PROC_BIND:
15193 case OMP_CLAUSE_FOR:
15194 case OMP_CLAUSE_PARALLEL:
15195 case OMP_CLAUSE_SECTIONS:
15196 case OMP_CLAUSE_TASKGROUP:
15197 case OMP_CLAUSE_NOGROUP:
15198 case OMP_CLAUSE_THREADS:
15199 case OMP_CLAUSE_SIMD:
15200 case OMP_CLAUSE_DEFAULTMAP:
15201 case OMP_CLAUSE_INDEPENDENT:
15202 case OMP_CLAUSE_AUTO:
15203 case OMP_CLAUSE_SEQ:
15204 break;
15205 default:
15206 gcc_unreachable ();
15208 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15209 switch (OMP_CLAUSE_CODE (nc))
15211 case OMP_CLAUSE_SHARED:
15212 case OMP_CLAUSE_PRIVATE:
15213 case OMP_CLAUSE_FIRSTPRIVATE:
15214 case OMP_CLAUSE_LASTPRIVATE:
15215 case OMP_CLAUSE_COPYPRIVATE:
15216 case OMP_CLAUSE_LINEAR:
15217 case OMP_CLAUSE_REDUCTION:
15218 case OMP_CLAUSE_USE_DEVICE_PTR:
15219 case OMP_CLAUSE_IS_DEVICE_PTR:
15220 /* tsubst_expr on SCOPE_REF results in returning
15221 finish_non_static_data_member result. Undo that here. */
15222 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15223 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15224 == IDENTIFIER_NODE))
15226 tree t = OMP_CLAUSE_DECL (nc);
15227 tree v = t;
15228 while (v)
15229 switch (TREE_CODE (v))
15231 case COMPONENT_REF:
15232 case MEM_REF:
15233 case INDIRECT_REF:
15234 CASE_CONVERT:
15235 case POINTER_PLUS_EXPR:
15236 v = TREE_OPERAND (v, 0);
15237 continue;
15238 case PARM_DECL:
15239 if (DECL_CONTEXT (v) == current_function_decl
15240 && DECL_ARTIFICIAL (v)
15241 && DECL_NAME (v) == this_identifier)
15242 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15243 /* FALLTHRU */
15244 default:
15245 v = NULL_TREE;
15246 break;
15249 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15250 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15251 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15252 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15253 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15255 tree decl = OMP_CLAUSE_DECL (nc);
15256 if (VAR_P (decl))
15258 if (!DECL_LANG_SPECIFIC (decl))
15259 retrofit_lang_decl (decl);
15260 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15263 break;
15264 default:
15265 break;
15269 new_clauses = nreverse (new_clauses);
15270 if (ort != C_ORT_OMP_DECLARE_SIMD)
15272 new_clauses = finish_omp_clauses (new_clauses, ort);
15273 if (linear_no_step)
15274 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15275 if (nc == linear_no_step)
15277 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15278 break;
15281 return new_clauses;
15284 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15286 static tree
15287 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15288 tree in_decl)
15290 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15292 tree purpose, value, chain;
15294 if (t == NULL)
15295 return t;
15297 if (TREE_CODE (t) != TREE_LIST)
15298 return tsubst_copy_and_build (t, args, complain, in_decl,
15299 /*function_p=*/false,
15300 /*integral_constant_expression_p=*/false);
15302 if (t == void_list_node)
15303 return t;
15305 purpose = TREE_PURPOSE (t);
15306 if (purpose)
15307 purpose = RECUR (purpose);
15308 value = TREE_VALUE (t);
15309 if (value)
15311 if (TREE_CODE (value) != LABEL_DECL)
15312 value = RECUR (value);
15313 else
15315 value = lookup_label (DECL_NAME (value));
15316 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15317 TREE_USED (value) = 1;
15320 chain = TREE_CHAIN (t);
15321 if (chain && chain != void_type_node)
15322 chain = RECUR (chain);
15323 return tree_cons (purpose, value, chain);
15324 #undef RECUR
15327 /* Used to temporarily communicate the list of #pragma omp parallel
15328 clauses to #pragma omp for instantiation if they are combined
15329 together. */
15331 static tree *omp_parallel_combined_clauses;
15333 /* Substitute one OMP_FOR iterator. */
15335 static void
15336 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15337 tree initv, tree condv, tree incrv, tree *clauses,
15338 tree args, tsubst_flags_t complain, tree in_decl,
15339 bool integral_constant_expression_p)
15341 #define RECUR(NODE) \
15342 tsubst_expr ((NODE), args, complain, in_decl, \
15343 integral_constant_expression_p)
15344 tree decl, init, cond, incr;
15346 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15347 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15349 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15351 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15352 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15355 decl = TREE_OPERAND (init, 0);
15356 init = TREE_OPERAND (init, 1);
15357 tree decl_expr = NULL_TREE;
15358 if (init && TREE_CODE (init) == DECL_EXPR)
15360 /* We need to jump through some hoops to handle declarations in the
15361 init-statement, since we might need to handle auto deduction,
15362 but we need to keep control of initialization. */
15363 decl_expr = init;
15364 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15365 decl = tsubst_decl (decl, args, complain);
15367 else
15369 if (TREE_CODE (decl) == SCOPE_REF)
15371 decl = RECUR (decl);
15372 if (TREE_CODE (decl) == COMPONENT_REF)
15374 tree v = decl;
15375 while (v)
15376 switch (TREE_CODE (v))
15378 case COMPONENT_REF:
15379 case MEM_REF:
15380 case INDIRECT_REF:
15381 CASE_CONVERT:
15382 case POINTER_PLUS_EXPR:
15383 v = TREE_OPERAND (v, 0);
15384 continue;
15385 case PARM_DECL:
15386 if (DECL_CONTEXT (v) == current_function_decl
15387 && DECL_ARTIFICIAL (v)
15388 && DECL_NAME (v) == this_identifier)
15390 decl = TREE_OPERAND (decl, 1);
15391 decl = omp_privatize_field (decl, false);
15393 /* FALLTHRU */
15394 default:
15395 v = NULL_TREE;
15396 break;
15400 else
15401 decl = RECUR (decl);
15403 init = RECUR (init);
15405 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15406 if (auto_node && init)
15407 TREE_TYPE (decl)
15408 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15410 gcc_assert (!type_dependent_expression_p (decl));
15412 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15414 if (decl_expr)
15416 /* Declare the variable, but don't let that initialize it. */
15417 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15418 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15419 RECUR (decl_expr);
15420 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15423 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15424 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15425 if (TREE_CODE (incr) == MODIFY_EXPR)
15427 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15428 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15429 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15430 NOP_EXPR, rhs, complain);
15432 else
15433 incr = RECUR (incr);
15434 TREE_VEC_ELT (declv, i) = decl;
15435 TREE_VEC_ELT (initv, i) = init;
15436 TREE_VEC_ELT (condv, i) = cond;
15437 TREE_VEC_ELT (incrv, i) = incr;
15438 return;
15441 if (decl_expr)
15443 /* Declare and initialize the variable. */
15444 RECUR (decl_expr);
15445 init = NULL_TREE;
15447 else if (init)
15449 tree *pc;
15450 int j;
15451 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15453 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15455 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15456 && OMP_CLAUSE_DECL (*pc) == decl)
15457 break;
15458 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15459 && OMP_CLAUSE_DECL (*pc) == decl)
15461 if (j)
15462 break;
15463 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15464 tree c = *pc;
15465 *pc = OMP_CLAUSE_CHAIN (c);
15466 OMP_CLAUSE_CHAIN (c) = *clauses;
15467 *clauses = c;
15469 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15470 && OMP_CLAUSE_DECL (*pc) == decl)
15472 error ("iteration variable %qD should not be firstprivate",
15473 decl);
15474 *pc = OMP_CLAUSE_CHAIN (*pc);
15476 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15477 && OMP_CLAUSE_DECL (*pc) == decl)
15479 error ("iteration variable %qD should not be reduction",
15480 decl);
15481 *pc = OMP_CLAUSE_CHAIN (*pc);
15483 else
15484 pc = &OMP_CLAUSE_CHAIN (*pc);
15486 if (*pc)
15487 break;
15489 if (*pc == NULL_TREE)
15491 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15492 OMP_CLAUSE_DECL (c) = decl;
15493 c = finish_omp_clauses (c, C_ORT_OMP);
15494 if (c)
15496 OMP_CLAUSE_CHAIN (c) = *clauses;
15497 *clauses = c;
15501 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15502 if (COMPARISON_CLASS_P (cond))
15504 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15505 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15506 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15508 else
15509 cond = RECUR (cond);
15510 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15511 switch (TREE_CODE (incr))
15513 case PREINCREMENT_EXPR:
15514 case PREDECREMENT_EXPR:
15515 case POSTINCREMENT_EXPR:
15516 case POSTDECREMENT_EXPR:
15517 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15518 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15519 break;
15520 case MODIFY_EXPR:
15521 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15522 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15524 tree rhs = TREE_OPERAND (incr, 1);
15525 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15526 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15527 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15528 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15529 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15530 rhs0, rhs1));
15532 else
15533 incr = RECUR (incr);
15534 break;
15535 case MODOP_EXPR:
15536 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15537 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15539 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15540 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15541 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15542 TREE_TYPE (decl), lhs,
15543 RECUR (TREE_OPERAND (incr, 2))));
15545 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15546 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15547 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15549 tree rhs = TREE_OPERAND (incr, 2);
15550 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15551 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15552 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15553 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15554 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15555 rhs0, rhs1));
15557 else
15558 incr = RECUR (incr);
15559 break;
15560 default:
15561 incr = RECUR (incr);
15562 break;
15565 TREE_VEC_ELT (declv, i) = decl;
15566 TREE_VEC_ELT (initv, i) = init;
15567 TREE_VEC_ELT (condv, i) = cond;
15568 TREE_VEC_ELT (incrv, i) = incr;
15569 #undef RECUR
15572 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15573 of OMP_TARGET's body. */
15575 static tree
15576 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15578 *walk_subtrees = 0;
15579 switch (TREE_CODE (*tp))
15581 case OMP_TEAMS:
15582 return *tp;
15583 case BIND_EXPR:
15584 case STATEMENT_LIST:
15585 *walk_subtrees = 1;
15586 break;
15587 default:
15588 break;
15590 return NULL_TREE;
15593 /* Helper function for tsubst_expr. For decomposition declaration
15594 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15595 also the corresponding decls representing the identifiers
15596 of the decomposition declaration. Return DECL if successful
15597 or error_mark_node otherwise, set *FIRST to the first decl
15598 in the list chained through DECL_CHAIN and *CNT to the number
15599 of such decls. */
15601 static tree
15602 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15603 tsubst_flags_t complain, tree in_decl, tree *first,
15604 unsigned int *cnt)
15606 tree decl2, decl3, prev = decl;
15607 *cnt = 0;
15608 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15609 for (decl2 = DECL_CHAIN (pattern_decl);
15610 decl2
15611 && VAR_P (decl2)
15612 && DECL_DECOMPOSITION_P (decl2)
15613 && DECL_NAME (decl2);
15614 decl2 = DECL_CHAIN (decl2))
15616 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15618 gcc_assert (errorcount);
15619 return error_mark_node;
15621 (*cnt)++;
15622 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15623 tree v = DECL_VALUE_EXPR (decl2);
15624 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15625 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15626 decl3 = tsubst (decl2, args, complain, in_decl);
15627 SET_DECL_VALUE_EXPR (decl2, v);
15628 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15629 if (VAR_P (decl3))
15630 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15631 maybe_push_decl (decl3);
15632 if (error_operand_p (decl3))
15633 decl = error_mark_node;
15634 else if (decl != error_mark_node
15635 && DECL_CHAIN (decl3) != prev)
15637 gcc_assert (errorcount);
15638 decl = error_mark_node;
15640 else
15641 prev = decl3;
15643 *first = prev;
15644 return decl;
15647 /* Like tsubst_copy for expressions, etc. but also does semantic
15648 processing. */
15650 tree
15651 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15652 bool integral_constant_expression_p)
15654 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15655 #define RECUR(NODE) \
15656 tsubst_expr ((NODE), args, complain, in_decl, \
15657 integral_constant_expression_p)
15659 tree stmt, tmp;
15660 tree r;
15661 location_t loc;
15663 if (t == NULL_TREE || t == error_mark_node)
15664 return t;
15666 loc = input_location;
15667 if (EXPR_HAS_LOCATION (t))
15668 input_location = EXPR_LOCATION (t);
15669 if (STATEMENT_CODE_P (TREE_CODE (t)))
15670 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15672 switch (TREE_CODE (t))
15674 case STATEMENT_LIST:
15676 tree_stmt_iterator i;
15677 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15678 RECUR (tsi_stmt (i));
15679 break;
15682 case CTOR_INITIALIZER:
15683 finish_mem_initializers (tsubst_initializer_list
15684 (TREE_OPERAND (t, 0), args));
15685 break;
15687 case RETURN_EXPR:
15688 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15689 break;
15691 case EXPR_STMT:
15692 tmp = RECUR (EXPR_STMT_EXPR (t));
15693 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15694 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15695 else
15696 finish_expr_stmt (tmp);
15697 break;
15699 case USING_STMT:
15700 do_using_directive (USING_STMT_NAMESPACE (t));
15701 break;
15703 case DECL_EXPR:
15705 tree decl, pattern_decl;
15706 tree init;
15708 pattern_decl = decl = DECL_EXPR_DECL (t);
15709 if (TREE_CODE (decl) == LABEL_DECL)
15710 finish_label_decl (DECL_NAME (decl));
15711 else if (TREE_CODE (decl) == USING_DECL)
15713 tree scope = USING_DECL_SCOPE (decl);
15714 tree name = DECL_NAME (decl);
15716 scope = tsubst (scope, args, complain, in_decl);
15717 decl = lookup_qualified_name (scope, name,
15718 /*is_type_p=*/false,
15719 /*complain=*/false);
15720 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15721 qualified_name_lookup_error (scope, name, decl, input_location);
15722 else
15723 do_local_using_decl (decl, scope, name);
15725 else if (DECL_PACK_P (decl))
15727 /* Don't build up decls for a variadic capture proxy, we'll
15728 instantiate the elements directly as needed. */
15729 break;
15731 else
15733 init = DECL_INITIAL (decl);
15734 decl = tsubst (decl, args, complain, in_decl);
15735 if (decl != error_mark_node)
15737 /* By marking the declaration as instantiated, we avoid
15738 trying to instantiate it. Since instantiate_decl can't
15739 handle local variables, and since we've already done
15740 all that needs to be done, that's the right thing to
15741 do. */
15742 if (VAR_P (decl))
15743 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15744 if (VAR_P (decl)
15745 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15746 /* Anonymous aggregates are a special case. */
15747 finish_anon_union (decl);
15748 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15750 DECL_CONTEXT (decl) = current_function_decl;
15751 if (DECL_NAME (decl) == this_identifier)
15753 tree lam = DECL_CONTEXT (current_function_decl);
15754 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15755 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15757 insert_capture_proxy (decl);
15759 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15760 /* We already did a pushtag. */;
15761 else if (TREE_CODE (decl) == FUNCTION_DECL
15762 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15763 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15765 DECL_CONTEXT (decl) = NULL_TREE;
15766 pushdecl (decl);
15767 DECL_CONTEXT (decl) = current_function_decl;
15768 cp_check_omp_declare_reduction (decl);
15770 else
15772 int const_init = false;
15773 maybe_push_decl (decl);
15774 if (VAR_P (decl)
15775 && DECL_PRETTY_FUNCTION_P (decl))
15777 /* For __PRETTY_FUNCTION__ we have to adjust the
15778 initializer. */
15779 const char *const name
15780 = cxx_printable_name (current_function_decl, 2);
15781 init = cp_fname_init (name, &TREE_TYPE (decl));
15783 else
15784 init = tsubst_init (init, decl, args, complain, in_decl);
15786 if (VAR_P (decl))
15787 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15788 (pattern_decl));
15789 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15790 if (VAR_P (decl)
15791 && DECL_DECOMPOSITION_P (decl)
15792 && TREE_TYPE (pattern_decl) != error_mark_node)
15794 unsigned int cnt;
15795 tree first;
15796 decl = tsubst_decomp_names (decl, pattern_decl, args,
15797 complain, in_decl, &first,
15798 &cnt);
15799 if (decl != error_mark_node)
15800 cp_finish_decomp (decl, first, cnt);
15806 break;
15809 case FOR_STMT:
15810 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15811 RECUR (FOR_INIT_STMT (t));
15812 finish_init_stmt (stmt);
15813 tmp = RECUR (FOR_COND (t));
15814 finish_for_cond (tmp, stmt, false);
15815 tmp = RECUR (FOR_EXPR (t));
15816 finish_for_expr (tmp, stmt);
15817 RECUR (FOR_BODY (t));
15818 finish_for_stmt (stmt);
15819 break;
15821 case RANGE_FOR_STMT:
15823 tree decl, expr;
15824 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15825 decl = RANGE_FOR_DECL (t);
15826 decl = tsubst (decl, args, complain, in_decl);
15827 maybe_push_decl (decl);
15828 expr = RECUR (RANGE_FOR_EXPR (t));
15829 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15831 unsigned int cnt;
15832 tree first;
15833 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15834 complain, in_decl, &first, &cnt);
15835 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15836 RANGE_FOR_IVDEP (t));
15838 else
15839 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15840 RANGE_FOR_IVDEP (t));
15841 RECUR (RANGE_FOR_BODY (t));
15842 finish_for_stmt (stmt);
15844 break;
15846 case WHILE_STMT:
15847 stmt = begin_while_stmt ();
15848 tmp = RECUR (WHILE_COND (t));
15849 finish_while_stmt_cond (tmp, stmt, false);
15850 RECUR (WHILE_BODY (t));
15851 finish_while_stmt (stmt);
15852 break;
15854 case DO_STMT:
15855 stmt = begin_do_stmt ();
15856 RECUR (DO_BODY (t));
15857 finish_do_body (stmt);
15858 tmp = RECUR (DO_COND (t));
15859 finish_do_stmt (tmp, stmt, false);
15860 break;
15862 case IF_STMT:
15863 stmt = begin_if_stmt ();
15864 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15865 tmp = RECUR (IF_COND (t));
15866 tmp = finish_if_stmt_cond (tmp, stmt);
15867 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15868 /* Don't instantiate the THEN_CLAUSE. */;
15869 else
15871 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15872 if (inhibit)
15873 ++c_inhibit_evaluation_warnings;
15874 RECUR (THEN_CLAUSE (t));
15875 if (inhibit)
15876 --c_inhibit_evaluation_warnings;
15878 finish_then_clause (stmt);
15880 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15881 /* Don't instantiate the ELSE_CLAUSE. */;
15882 else if (ELSE_CLAUSE (t))
15884 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15885 begin_else_clause (stmt);
15886 if (inhibit)
15887 ++c_inhibit_evaluation_warnings;
15888 RECUR (ELSE_CLAUSE (t));
15889 if (inhibit)
15890 --c_inhibit_evaluation_warnings;
15891 finish_else_clause (stmt);
15894 finish_if_stmt (stmt);
15895 break;
15897 case BIND_EXPR:
15898 if (BIND_EXPR_BODY_BLOCK (t))
15899 stmt = begin_function_body ();
15900 else
15901 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15902 ? BCS_TRY_BLOCK : 0);
15904 RECUR (BIND_EXPR_BODY (t));
15906 if (BIND_EXPR_BODY_BLOCK (t))
15907 finish_function_body (stmt);
15908 else
15909 finish_compound_stmt (stmt);
15910 break;
15912 case BREAK_STMT:
15913 finish_break_stmt ();
15914 break;
15916 case CONTINUE_STMT:
15917 finish_continue_stmt ();
15918 break;
15920 case SWITCH_STMT:
15921 stmt = begin_switch_stmt ();
15922 tmp = RECUR (SWITCH_STMT_COND (t));
15923 finish_switch_cond (tmp, stmt);
15924 RECUR (SWITCH_STMT_BODY (t));
15925 finish_switch_stmt (stmt);
15926 break;
15928 case CASE_LABEL_EXPR:
15930 tree low = RECUR (CASE_LOW (t));
15931 tree high = RECUR (CASE_HIGH (t));
15932 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
15933 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
15934 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
15935 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
15937 break;
15939 case LABEL_EXPR:
15941 tree decl = LABEL_EXPR_LABEL (t);
15942 tree label;
15944 label = finish_label_stmt (DECL_NAME (decl));
15945 if (TREE_CODE (label) == LABEL_DECL)
15946 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
15947 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15948 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15950 break;
15952 case GOTO_EXPR:
15953 tmp = GOTO_DESTINATION (t);
15954 if (TREE_CODE (tmp) != LABEL_DECL)
15955 /* Computed goto's must be tsubst'd into. On the other hand,
15956 non-computed gotos must not be; the identifier in question
15957 will have no binding. */
15958 tmp = RECUR (tmp);
15959 else
15960 tmp = DECL_NAME (tmp);
15961 finish_goto_stmt (tmp);
15962 break;
15964 case ASM_EXPR:
15966 tree string = RECUR (ASM_STRING (t));
15967 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15968 complain, in_decl);
15969 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15970 complain, in_decl);
15971 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15972 complain, in_decl);
15973 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15974 complain, in_decl);
15975 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15976 clobbers, labels);
15977 tree asm_expr = tmp;
15978 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15979 asm_expr = TREE_OPERAND (asm_expr, 0);
15980 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15982 break;
15984 case TRY_BLOCK:
15985 if (CLEANUP_P (t))
15987 stmt = begin_try_block ();
15988 RECUR (TRY_STMTS (t));
15989 finish_cleanup_try_block (stmt);
15990 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15992 else
15994 tree compound_stmt = NULL_TREE;
15996 if (FN_TRY_BLOCK_P (t))
15997 stmt = begin_function_try_block (&compound_stmt);
15998 else
15999 stmt = begin_try_block ();
16001 RECUR (TRY_STMTS (t));
16003 if (FN_TRY_BLOCK_P (t))
16004 finish_function_try_block (stmt);
16005 else
16006 finish_try_block (stmt);
16008 RECUR (TRY_HANDLERS (t));
16009 if (FN_TRY_BLOCK_P (t))
16010 finish_function_handler_sequence (stmt, compound_stmt);
16011 else
16012 finish_handler_sequence (stmt);
16014 break;
16016 case HANDLER:
16018 tree decl = HANDLER_PARMS (t);
16020 if (decl)
16022 decl = tsubst (decl, args, complain, in_decl);
16023 /* Prevent instantiate_decl from trying to instantiate
16024 this variable. We've already done all that needs to be
16025 done. */
16026 if (decl != error_mark_node)
16027 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16029 stmt = begin_handler ();
16030 finish_handler_parms (decl, stmt);
16031 RECUR (HANDLER_BODY (t));
16032 finish_handler (stmt);
16034 break;
16036 case TAG_DEFN:
16037 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16038 if (CLASS_TYPE_P (tmp))
16040 /* Local classes are not independent templates; they are
16041 instantiated along with their containing function. And this
16042 way we don't have to deal with pushing out of one local class
16043 to instantiate a member of another local class. */
16044 tree fn;
16045 /* Closures are handled by the LAMBDA_EXPR. */
16046 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16047 complete_type (tmp);
16048 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16049 if (!DECL_ARTIFICIAL (fn))
16050 instantiate_decl (fn, /*defer_ok=*/false,
16051 /*expl_inst_class=*/false);
16053 break;
16055 case STATIC_ASSERT:
16057 tree condition;
16059 ++c_inhibit_evaluation_warnings;
16060 condition =
16061 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16062 args,
16063 complain, in_decl,
16064 /*integral_constant_expression_p=*/true);
16065 --c_inhibit_evaluation_warnings;
16067 finish_static_assert (condition,
16068 STATIC_ASSERT_MESSAGE (t),
16069 STATIC_ASSERT_SOURCE_LOCATION (t),
16070 /*member_p=*/false);
16072 break;
16074 case OACC_KERNELS:
16075 case OACC_PARALLEL:
16076 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16077 in_decl);
16078 stmt = begin_omp_parallel ();
16079 RECUR (OMP_BODY (t));
16080 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16081 break;
16083 case OMP_PARALLEL:
16084 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16085 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16086 complain, in_decl);
16087 if (OMP_PARALLEL_COMBINED (t))
16088 omp_parallel_combined_clauses = &tmp;
16089 stmt = begin_omp_parallel ();
16090 RECUR (OMP_PARALLEL_BODY (t));
16091 gcc_assert (omp_parallel_combined_clauses == NULL);
16092 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16093 = OMP_PARALLEL_COMBINED (t);
16094 pop_omp_privatization_clauses (r);
16095 break;
16097 case OMP_TASK:
16098 r = push_omp_privatization_clauses (false);
16099 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16100 complain, in_decl);
16101 stmt = begin_omp_task ();
16102 RECUR (OMP_TASK_BODY (t));
16103 finish_omp_task (tmp, stmt);
16104 pop_omp_privatization_clauses (r);
16105 break;
16107 case OMP_FOR:
16108 case OMP_SIMD:
16109 case CILK_SIMD:
16110 case CILK_FOR:
16111 case OMP_DISTRIBUTE:
16112 case OMP_TASKLOOP:
16113 case OACC_LOOP:
16115 tree clauses, body, pre_body;
16116 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16117 tree orig_declv = NULL_TREE;
16118 tree incrv = NULL_TREE;
16119 enum c_omp_region_type ort = C_ORT_OMP;
16120 int i;
16122 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16123 ort = C_ORT_CILK;
16124 else if (TREE_CODE (t) == OACC_LOOP)
16125 ort = C_ORT_ACC;
16127 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16128 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16129 in_decl);
16130 if (OMP_FOR_INIT (t) != NULL_TREE)
16132 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16133 if (OMP_FOR_ORIG_DECLS (t))
16134 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16135 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16136 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16137 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16140 stmt = begin_omp_structured_block ();
16142 pre_body = push_stmt_list ();
16143 RECUR (OMP_FOR_PRE_BODY (t));
16144 pre_body = pop_stmt_list (pre_body);
16146 if (OMP_FOR_INIT (t) != NULL_TREE)
16147 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16148 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16149 incrv, &clauses, args, complain, in_decl,
16150 integral_constant_expression_p);
16151 omp_parallel_combined_clauses = NULL;
16153 body = push_stmt_list ();
16154 RECUR (OMP_FOR_BODY (t));
16155 body = pop_stmt_list (body);
16157 if (OMP_FOR_INIT (t) != NULL_TREE)
16158 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16159 orig_declv, initv, condv, incrv, body, pre_body,
16160 NULL, clauses);
16161 else
16163 t = make_node (TREE_CODE (t));
16164 TREE_TYPE (t) = void_type_node;
16165 OMP_FOR_BODY (t) = body;
16166 OMP_FOR_PRE_BODY (t) = pre_body;
16167 OMP_FOR_CLAUSES (t) = clauses;
16168 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16169 add_stmt (t);
16172 add_stmt (finish_omp_structured_block (stmt));
16173 pop_omp_privatization_clauses (r);
16175 break;
16177 case OMP_SECTIONS:
16178 omp_parallel_combined_clauses = NULL;
16179 /* FALLTHRU */
16180 case OMP_SINGLE:
16181 case OMP_TEAMS:
16182 case OMP_CRITICAL:
16183 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16184 && OMP_TEAMS_COMBINED (t));
16185 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16186 in_decl);
16187 stmt = push_stmt_list ();
16188 RECUR (OMP_BODY (t));
16189 stmt = pop_stmt_list (stmt);
16191 t = copy_node (t);
16192 OMP_BODY (t) = stmt;
16193 OMP_CLAUSES (t) = tmp;
16194 add_stmt (t);
16195 pop_omp_privatization_clauses (r);
16196 break;
16198 case OACC_DATA:
16199 case OMP_TARGET_DATA:
16200 case OMP_TARGET:
16201 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16202 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16203 in_decl);
16204 keep_next_level (true);
16205 stmt = begin_omp_structured_block ();
16207 RECUR (OMP_BODY (t));
16208 stmt = finish_omp_structured_block (stmt);
16210 t = copy_node (t);
16211 OMP_BODY (t) = stmt;
16212 OMP_CLAUSES (t) = tmp;
16213 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16215 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16216 if (teams)
16218 /* For combined target teams, ensure the num_teams and
16219 thread_limit clause expressions are evaluated on the host,
16220 before entering the target construct. */
16221 tree c;
16222 for (c = OMP_TEAMS_CLAUSES (teams);
16223 c; c = OMP_CLAUSE_CHAIN (c))
16224 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16225 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16226 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16228 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16229 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16230 if (expr == error_mark_node)
16231 continue;
16232 tmp = TARGET_EXPR_SLOT (expr);
16233 add_stmt (expr);
16234 OMP_CLAUSE_OPERAND (c, 0) = expr;
16235 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16236 OMP_CLAUSE_FIRSTPRIVATE);
16237 OMP_CLAUSE_DECL (tc) = tmp;
16238 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16239 OMP_TARGET_CLAUSES (t) = tc;
16243 add_stmt (t);
16244 break;
16246 case OACC_DECLARE:
16247 t = copy_node (t);
16248 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16249 complain, in_decl);
16250 OACC_DECLARE_CLAUSES (t) = tmp;
16251 add_stmt (t);
16252 break;
16254 case OMP_TARGET_UPDATE:
16255 case OMP_TARGET_ENTER_DATA:
16256 case OMP_TARGET_EXIT_DATA:
16257 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16258 complain, in_decl);
16259 t = copy_node (t);
16260 OMP_STANDALONE_CLAUSES (t) = tmp;
16261 add_stmt (t);
16262 break;
16264 case OACC_ENTER_DATA:
16265 case OACC_EXIT_DATA:
16266 case OACC_UPDATE:
16267 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16268 complain, in_decl);
16269 t = copy_node (t);
16270 OMP_STANDALONE_CLAUSES (t) = tmp;
16271 add_stmt (t);
16272 break;
16274 case OMP_ORDERED:
16275 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16276 complain, in_decl);
16277 stmt = push_stmt_list ();
16278 RECUR (OMP_BODY (t));
16279 stmt = pop_stmt_list (stmt);
16281 t = copy_node (t);
16282 OMP_BODY (t) = stmt;
16283 OMP_ORDERED_CLAUSES (t) = tmp;
16284 add_stmt (t);
16285 break;
16287 case OMP_SECTION:
16288 case OMP_MASTER:
16289 case OMP_TASKGROUP:
16290 stmt = push_stmt_list ();
16291 RECUR (OMP_BODY (t));
16292 stmt = pop_stmt_list (stmt);
16294 t = copy_node (t);
16295 OMP_BODY (t) = stmt;
16296 add_stmt (t);
16297 break;
16299 case OMP_ATOMIC:
16300 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16301 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16303 tree op1 = TREE_OPERAND (t, 1);
16304 tree rhs1 = NULL_TREE;
16305 tree lhs, rhs;
16306 if (TREE_CODE (op1) == COMPOUND_EXPR)
16308 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16309 op1 = TREE_OPERAND (op1, 1);
16311 lhs = RECUR (TREE_OPERAND (op1, 0));
16312 rhs = RECUR (TREE_OPERAND (op1, 1));
16313 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16314 NULL_TREE, NULL_TREE, rhs1,
16315 OMP_ATOMIC_SEQ_CST (t));
16317 else
16319 tree op1 = TREE_OPERAND (t, 1);
16320 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16321 tree rhs1 = NULL_TREE;
16322 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16323 enum tree_code opcode = NOP_EXPR;
16324 if (code == OMP_ATOMIC_READ)
16326 v = RECUR (TREE_OPERAND (op1, 0));
16327 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16329 else if (code == OMP_ATOMIC_CAPTURE_OLD
16330 || code == OMP_ATOMIC_CAPTURE_NEW)
16332 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16333 v = RECUR (TREE_OPERAND (op1, 0));
16334 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16335 if (TREE_CODE (op11) == COMPOUND_EXPR)
16337 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16338 op11 = TREE_OPERAND (op11, 1);
16340 lhs = RECUR (TREE_OPERAND (op11, 0));
16341 rhs = RECUR (TREE_OPERAND (op11, 1));
16342 opcode = TREE_CODE (op11);
16343 if (opcode == MODIFY_EXPR)
16344 opcode = NOP_EXPR;
16346 else
16348 code = OMP_ATOMIC;
16349 lhs = RECUR (TREE_OPERAND (op1, 0));
16350 rhs = RECUR (TREE_OPERAND (op1, 1));
16352 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16353 OMP_ATOMIC_SEQ_CST (t));
16355 break;
16357 case TRANSACTION_EXPR:
16359 int flags = 0;
16360 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16361 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16363 if (TRANSACTION_EXPR_IS_STMT (t))
16365 tree body = TRANSACTION_EXPR_BODY (t);
16366 tree noex = NULL_TREE;
16367 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16369 noex = MUST_NOT_THROW_COND (body);
16370 if (noex == NULL_TREE)
16371 noex = boolean_true_node;
16372 body = TREE_OPERAND (body, 0);
16374 stmt = begin_transaction_stmt (input_location, NULL, flags);
16375 RECUR (body);
16376 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16378 else
16380 stmt = build_transaction_expr (EXPR_LOCATION (t),
16381 RECUR (TRANSACTION_EXPR_BODY (t)),
16382 flags, NULL_TREE);
16383 RETURN (stmt);
16386 break;
16388 case MUST_NOT_THROW_EXPR:
16390 tree op0 = RECUR (TREE_OPERAND (t, 0));
16391 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16392 RETURN (build_must_not_throw_expr (op0, cond));
16395 case EXPR_PACK_EXPANSION:
16396 error ("invalid use of pack expansion expression");
16397 RETURN (error_mark_node);
16399 case NONTYPE_ARGUMENT_PACK:
16400 error ("use %<...%> to expand argument pack");
16401 RETURN (error_mark_node);
16403 case CILK_SPAWN_STMT:
16404 cfun->calls_cilk_spawn = 1;
16405 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16407 case CILK_SYNC_STMT:
16408 RETURN (build_cilk_sync ());
16410 case COMPOUND_EXPR:
16411 tmp = RECUR (TREE_OPERAND (t, 0));
16412 if (tmp == NULL_TREE)
16413 /* If the first operand was a statement, we're done with it. */
16414 RETURN (RECUR (TREE_OPERAND (t, 1)));
16415 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16416 RECUR (TREE_OPERAND (t, 1)),
16417 complain));
16419 case ANNOTATE_EXPR:
16420 tmp = RECUR (TREE_OPERAND (t, 0));
16421 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16422 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16424 default:
16425 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16427 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16428 /*function_p=*/false,
16429 integral_constant_expression_p));
16432 RETURN (NULL_TREE);
16433 out:
16434 input_location = loc;
16435 return r;
16436 #undef RECUR
16437 #undef RETURN
16440 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16441 function. For description of the body see comment above
16442 cp_parser_omp_declare_reduction_exprs. */
16444 static void
16445 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16447 if (t == NULL_TREE || t == error_mark_node)
16448 return;
16450 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16452 tree_stmt_iterator tsi;
16453 int i;
16454 tree stmts[7];
16455 memset (stmts, 0, sizeof stmts);
16456 for (i = 0, tsi = tsi_start (t);
16457 i < 7 && !tsi_end_p (tsi);
16458 i++, tsi_next (&tsi))
16459 stmts[i] = tsi_stmt (tsi);
16460 gcc_assert (tsi_end_p (tsi));
16462 if (i >= 3)
16464 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16465 && TREE_CODE (stmts[1]) == DECL_EXPR);
16466 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16467 args, complain, in_decl);
16468 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16469 args, complain, in_decl);
16470 DECL_CONTEXT (omp_out) = current_function_decl;
16471 DECL_CONTEXT (omp_in) = current_function_decl;
16472 keep_next_level (true);
16473 tree block = begin_omp_structured_block ();
16474 tsubst_expr (stmts[2], args, complain, in_decl, false);
16475 block = finish_omp_structured_block (block);
16476 block = maybe_cleanup_point_expr_void (block);
16477 add_decl_expr (omp_out);
16478 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16479 TREE_NO_WARNING (omp_out) = 1;
16480 add_decl_expr (omp_in);
16481 finish_expr_stmt (block);
16483 if (i >= 6)
16485 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16486 && TREE_CODE (stmts[4]) == DECL_EXPR);
16487 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16488 args, complain, in_decl);
16489 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16490 args, complain, in_decl);
16491 DECL_CONTEXT (omp_priv) = current_function_decl;
16492 DECL_CONTEXT (omp_orig) = current_function_decl;
16493 keep_next_level (true);
16494 tree block = begin_omp_structured_block ();
16495 tsubst_expr (stmts[5], args, complain, in_decl, false);
16496 block = finish_omp_structured_block (block);
16497 block = maybe_cleanup_point_expr_void (block);
16498 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16499 add_decl_expr (omp_priv);
16500 add_decl_expr (omp_orig);
16501 finish_expr_stmt (block);
16502 if (i == 7)
16503 add_decl_expr (omp_orig);
16507 /* T is a postfix-expression that is not being used in a function
16508 call. Return the substituted version of T. */
16510 static tree
16511 tsubst_non_call_postfix_expression (tree t, tree args,
16512 tsubst_flags_t complain,
16513 tree in_decl)
16515 if (TREE_CODE (t) == SCOPE_REF)
16516 t = tsubst_qualified_id (t, args, complain, in_decl,
16517 /*done=*/false, /*address_p=*/false);
16518 else
16519 t = tsubst_copy_and_build (t, args, complain, in_decl,
16520 /*function_p=*/false,
16521 /*integral_constant_expression_p=*/false);
16523 return t;
16526 /* Like tsubst but deals with expressions and performs semantic
16527 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16529 tree
16530 tsubst_copy_and_build (tree t,
16531 tree args,
16532 tsubst_flags_t complain,
16533 tree in_decl,
16534 bool function_p,
16535 bool integral_constant_expression_p)
16537 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16538 #define RECUR(NODE) \
16539 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16540 /*function_p=*/false, \
16541 integral_constant_expression_p)
16543 tree retval, op1;
16544 location_t loc;
16546 if (t == NULL_TREE || t == error_mark_node)
16547 return t;
16549 loc = input_location;
16550 if (EXPR_HAS_LOCATION (t))
16551 input_location = EXPR_LOCATION (t);
16553 /* N3276 decltype magic only applies to calls at the top level or on the
16554 right side of a comma. */
16555 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16556 complain &= ~tf_decltype;
16558 switch (TREE_CODE (t))
16560 case USING_DECL:
16561 t = DECL_NAME (t);
16562 /* Fall through. */
16563 case IDENTIFIER_NODE:
16565 tree decl;
16566 cp_id_kind idk;
16567 bool non_integral_constant_expression_p;
16568 const char *error_msg;
16570 if (IDENTIFIER_TYPENAME_P (t))
16572 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16573 t = mangle_conv_op_name_for_type (new_type);
16576 /* Look up the name. */
16577 decl = lookup_name (t);
16579 /* By convention, expressions use ERROR_MARK_NODE to indicate
16580 failure, not NULL_TREE. */
16581 if (decl == NULL_TREE)
16582 decl = error_mark_node;
16584 decl = finish_id_expression (t, decl, NULL_TREE,
16585 &idk,
16586 integral_constant_expression_p,
16587 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16588 &non_integral_constant_expression_p,
16589 /*template_p=*/false,
16590 /*done=*/true,
16591 /*address_p=*/false,
16592 /*template_arg_p=*/false,
16593 &error_msg,
16594 input_location);
16595 if (error_msg)
16596 error (error_msg);
16597 if (!function_p && identifier_p (decl))
16599 if (complain & tf_error)
16600 unqualified_name_lookup_error (decl);
16601 decl = error_mark_node;
16603 RETURN (decl);
16606 case TEMPLATE_ID_EXPR:
16608 tree object;
16609 tree templ = RECUR (TREE_OPERAND (t, 0));
16610 tree targs = TREE_OPERAND (t, 1);
16612 if (targs)
16613 targs = tsubst_template_args (targs, args, complain, in_decl);
16614 if (targs == error_mark_node)
16615 return error_mark_node;
16617 if (variable_template_p (templ))
16618 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16620 if (TREE_CODE (templ) == COMPONENT_REF)
16622 object = TREE_OPERAND (templ, 0);
16623 templ = TREE_OPERAND (templ, 1);
16625 else
16626 object = NULL_TREE;
16627 templ = lookup_template_function (templ, targs);
16629 if (object)
16630 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16631 object, templ, NULL_TREE));
16632 else
16633 RETURN (baselink_for_fns (templ));
16636 case INDIRECT_REF:
16638 tree r = RECUR (TREE_OPERAND (t, 0));
16640 if (REFERENCE_REF_P (t))
16642 /* A type conversion to reference type will be enclosed in
16643 such an indirect ref, but the substitution of the cast
16644 will have also added such an indirect ref. */
16645 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16646 r = convert_from_reference (r);
16648 else
16649 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16650 complain|decltype_flag);
16652 if (TREE_CODE (r) == INDIRECT_REF)
16653 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16655 RETURN (r);
16658 case NOP_EXPR:
16660 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16661 tree op0 = RECUR (TREE_OPERAND (t, 0));
16662 RETURN (build_nop (type, op0));
16665 case IMPLICIT_CONV_EXPR:
16667 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16668 tree expr = RECUR (TREE_OPERAND (t, 0));
16669 int flags = LOOKUP_IMPLICIT;
16670 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16671 flags = LOOKUP_NORMAL;
16672 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16673 flags));
16676 case CONVERT_EXPR:
16678 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16679 tree op0 = RECUR (TREE_OPERAND (t, 0));
16680 RETURN (build1 (CONVERT_EXPR, type, op0));
16683 case CAST_EXPR:
16684 case REINTERPRET_CAST_EXPR:
16685 case CONST_CAST_EXPR:
16686 case DYNAMIC_CAST_EXPR:
16687 case STATIC_CAST_EXPR:
16689 tree type;
16690 tree op, r = NULL_TREE;
16692 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16693 if (integral_constant_expression_p
16694 && !cast_valid_in_integral_constant_expression_p (type))
16696 if (complain & tf_error)
16697 error ("a cast to a type other than an integral or "
16698 "enumeration type cannot appear in a constant-expression");
16699 RETURN (error_mark_node);
16702 op = RECUR (TREE_OPERAND (t, 0));
16704 warning_sentinel s(warn_useless_cast);
16705 switch (TREE_CODE (t))
16707 case CAST_EXPR:
16708 r = build_functional_cast (type, op, complain);
16709 break;
16710 case REINTERPRET_CAST_EXPR:
16711 r = build_reinterpret_cast (type, op, complain);
16712 break;
16713 case CONST_CAST_EXPR:
16714 r = build_const_cast (type, op, complain);
16715 break;
16716 case DYNAMIC_CAST_EXPR:
16717 r = build_dynamic_cast (type, op, complain);
16718 break;
16719 case STATIC_CAST_EXPR:
16720 r = build_static_cast (type, op, complain);
16721 break;
16722 default:
16723 gcc_unreachable ();
16726 RETURN (r);
16729 case POSTDECREMENT_EXPR:
16730 case POSTINCREMENT_EXPR:
16731 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16732 args, complain, in_decl);
16733 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16734 complain|decltype_flag));
16736 case PREDECREMENT_EXPR:
16737 case PREINCREMENT_EXPR:
16738 case NEGATE_EXPR:
16739 case BIT_NOT_EXPR:
16740 case ABS_EXPR:
16741 case TRUTH_NOT_EXPR:
16742 case UNARY_PLUS_EXPR: /* Unary + */
16743 case REALPART_EXPR:
16744 case IMAGPART_EXPR:
16745 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16746 RECUR (TREE_OPERAND (t, 0)),
16747 complain|decltype_flag));
16749 case FIX_TRUNC_EXPR:
16750 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16751 false, complain));
16753 case ADDR_EXPR:
16754 op1 = TREE_OPERAND (t, 0);
16755 if (TREE_CODE (op1) == LABEL_DECL)
16756 RETURN (finish_label_address_expr (DECL_NAME (op1),
16757 EXPR_LOCATION (op1)));
16758 if (TREE_CODE (op1) == SCOPE_REF)
16759 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16760 /*done=*/true, /*address_p=*/true);
16761 else
16762 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16763 in_decl);
16764 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16765 complain|decltype_flag));
16767 case PLUS_EXPR:
16768 case MINUS_EXPR:
16769 case MULT_EXPR:
16770 case TRUNC_DIV_EXPR:
16771 case CEIL_DIV_EXPR:
16772 case FLOOR_DIV_EXPR:
16773 case ROUND_DIV_EXPR:
16774 case EXACT_DIV_EXPR:
16775 case BIT_AND_EXPR:
16776 case BIT_IOR_EXPR:
16777 case BIT_XOR_EXPR:
16778 case TRUNC_MOD_EXPR:
16779 case FLOOR_MOD_EXPR:
16780 case TRUTH_ANDIF_EXPR:
16781 case TRUTH_ORIF_EXPR:
16782 case TRUTH_AND_EXPR:
16783 case TRUTH_OR_EXPR:
16784 case RSHIFT_EXPR:
16785 case LSHIFT_EXPR:
16786 case RROTATE_EXPR:
16787 case LROTATE_EXPR:
16788 case EQ_EXPR:
16789 case NE_EXPR:
16790 case MAX_EXPR:
16791 case MIN_EXPR:
16792 case LE_EXPR:
16793 case GE_EXPR:
16794 case LT_EXPR:
16795 case GT_EXPR:
16796 case MEMBER_REF:
16797 case DOTSTAR_EXPR:
16799 warning_sentinel s1(warn_type_limits);
16800 warning_sentinel s2(warn_div_by_zero);
16801 warning_sentinel s3(warn_logical_op);
16802 warning_sentinel s4(warn_tautological_compare);
16803 tree op0 = RECUR (TREE_OPERAND (t, 0));
16804 tree op1 = RECUR (TREE_OPERAND (t, 1));
16805 tree r = build_x_binary_op
16806 (input_location, TREE_CODE (t),
16807 op0,
16808 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16809 ? ERROR_MARK
16810 : TREE_CODE (TREE_OPERAND (t, 0))),
16811 op1,
16812 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16813 ? ERROR_MARK
16814 : TREE_CODE (TREE_OPERAND (t, 1))),
16815 /*overload=*/NULL,
16816 complain|decltype_flag);
16817 if (EXPR_P (r) && TREE_NO_WARNING (t))
16818 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16820 RETURN (r);
16823 case POINTER_PLUS_EXPR:
16825 tree op0 = RECUR (TREE_OPERAND (t, 0));
16826 tree op1 = RECUR (TREE_OPERAND (t, 1));
16827 return fold_build_pointer_plus (op0, op1);
16830 case SCOPE_REF:
16831 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16832 /*address_p=*/false));
16833 case ARRAY_REF:
16834 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16835 args, complain, in_decl);
16836 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16837 RECUR (TREE_OPERAND (t, 1)),
16838 complain|decltype_flag));
16840 case ARRAY_NOTATION_REF:
16842 tree start_index, length, stride;
16843 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16844 args, complain, in_decl);
16845 start_index = RECUR (ARRAY_NOTATION_START (t));
16846 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16847 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16848 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16849 length, stride, TREE_TYPE (op1)));
16851 case SIZEOF_EXPR:
16852 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16853 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16854 RETURN (tsubst_copy (t, args, complain, in_decl));
16855 /* Fall through */
16857 case ALIGNOF_EXPR:
16859 tree r;
16861 op1 = TREE_OPERAND (t, 0);
16862 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16863 op1 = TREE_TYPE (op1);
16864 if (!args)
16866 /* When there are no ARGS, we are trying to evaluate a
16867 non-dependent expression from the parser. Trying to do
16868 the substitutions may not work. */
16869 if (!TYPE_P (op1))
16870 op1 = TREE_TYPE (op1);
16872 else
16874 ++cp_unevaluated_operand;
16875 ++c_inhibit_evaluation_warnings;
16876 if (TYPE_P (op1))
16877 op1 = tsubst (op1, args, complain, in_decl);
16878 else
16879 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16880 /*function_p=*/false,
16881 /*integral_constant_expression_p=*/
16882 false);
16883 --cp_unevaluated_operand;
16884 --c_inhibit_evaluation_warnings;
16886 if (TYPE_P (op1))
16887 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16888 complain & tf_error);
16889 else
16890 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16891 complain & tf_error);
16892 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16894 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16896 if (!processing_template_decl && TYPE_P (op1))
16898 r = build_min (SIZEOF_EXPR, size_type_node,
16899 build1 (NOP_EXPR, op1, error_mark_node));
16900 SIZEOF_EXPR_TYPE_P (r) = 1;
16902 else
16903 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16904 TREE_SIDE_EFFECTS (r) = 0;
16905 TREE_READONLY (r) = 1;
16907 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16909 RETURN (r);
16912 case AT_ENCODE_EXPR:
16914 op1 = TREE_OPERAND (t, 0);
16915 ++cp_unevaluated_operand;
16916 ++c_inhibit_evaluation_warnings;
16917 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16918 /*function_p=*/false,
16919 /*integral_constant_expression_p=*/false);
16920 --cp_unevaluated_operand;
16921 --c_inhibit_evaluation_warnings;
16922 RETURN (objc_build_encode_expr (op1));
16925 case NOEXCEPT_EXPR:
16926 op1 = TREE_OPERAND (t, 0);
16927 ++cp_unevaluated_operand;
16928 ++c_inhibit_evaluation_warnings;
16929 ++cp_noexcept_operand;
16930 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16931 /*function_p=*/false,
16932 /*integral_constant_expression_p=*/false);
16933 --cp_unevaluated_operand;
16934 --c_inhibit_evaluation_warnings;
16935 --cp_noexcept_operand;
16936 RETURN (finish_noexcept_expr (op1, complain));
16938 case MODOP_EXPR:
16940 warning_sentinel s(warn_div_by_zero);
16941 tree lhs = RECUR (TREE_OPERAND (t, 0));
16942 tree rhs = RECUR (TREE_OPERAND (t, 2));
16943 tree r = build_x_modify_expr
16944 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16945 complain|decltype_flag);
16946 /* TREE_NO_WARNING must be set if either the expression was
16947 parenthesized or it uses an operator such as >>= rather
16948 than plain assignment. In the former case, it was already
16949 set and must be copied. In the latter case,
16950 build_x_modify_expr sets it and it must not be reset
16951 here. */
16952 if (TREE_NO_WARNING (t))
16953 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16955 RETURN (r);
16958 case ARROW_EXPR:
16959 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16960 args, complain, in_decl);
16961 /* Remember that there was a reference to this entity. */
16962 if (DECL_P (op1)
16963 && !mark_used (op1, complain) && !(complain & tf_error))
16964 RETURN (error_mark_node);
16965 RETURN (build_x_arrow (input_location, op1, complain));
16967 case NEW_EXPR:
16969 tree placement = RECUR (TREE_OPERAND (t, 0));
16970 tree init = RECUR (TREE_OPERAND (t, 3));
16971 vec<tree, va_gc> *placement_vec;
16972 vec<tree, va_gc> *init_vec;
16973 tree ret;
16975 if (placement == NULL_TREE)
16976 placement_vec = NULL;
16977 else
16979 placement_vec = make_tree_vector ();
16980 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16981 vec_safe_push (placement_vec, TREE_VALUE (placement));
16984 /* If there was an initializer in the original tree, but it
16985 instantiated to an empty list, then we should pass a
16986 non-NULL empty vector to tell build_new that it was an
16987 empty initializer() rather than no initializer. This can
16988 only happen when the initializer is a pack expansion whose
16989 parameter packs are of length zero. */
16990 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16991 init_vec = NULL;
16992 else
16994 init_vec = make_tree_vector ();
16995 if (init == void_node)
16996 gcc_assert (init_vec != NULL);
16997 else
16999 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17000 vec_safe_push (init_vec, TREE_VALUE (init));
17004 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17005 tree op2 = RECUR (TREE_OPERAND (t, 2));
17006 ret = build_new (&placement_vec, op1, op2, &init_vec,
17007 NEW_EXPR_USE_GLOBAL (t),
17008 complain);
17010 if (placement_vec != NULL)
17011 release_tree_vector (placement_vec);
17012 if (init_vec != NULL)
17013 release_tree_vector (init_vec);
17015 RETURN (ret);
17018 case DELETE_EXPR:
17020 tree op0 = RECUR (TREE_OPERAND (t, 0));
17021 tree op1 = RECUR (TREE_OPERAND (t, 1));
17022 RETURN (delete_sanity (op0, op1,
17023 DELETE_EXPR_USE_VEC (t),
17024 DELETE_EXPR_USE_GLOBAL (t),
17025 complain));
17028 case COMPOUND_EXPR:
17030 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17031 complain & ~tf_decltype, in_decl,
17032 /*function_p=*/false,
17033 integral_constant_expression_p);
17034 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17035 op0,
17036 RECUR (TREE_OPERAND (t, 1)),
17037 complain|decltype_flag));
17040 case CALL_EXPR:
17042 tree function;
17043 vec<tree, va_gc> *call_args;
17044 unsigned int nargs, i;
17045 bool qualified_p;
17046 bool koenig_p;
17047 tree ret;
17049 function = CALL_EXPR_FN (t);
17050 /* Internal function with no arguments. */
17051 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17052 RETURN (t);
17054 /* When we parsed the expression, we determined whether or
17055 not Koenig lookup should be performed. */
17056 koenig_p = KOENIG_LOOKUP_P (t);
17057 if (function == NULL_TREE)
17059 koenig_p = false;
17060 qualified_p = false;
17062 else if (TREE_CODE (function) == SCOPE_REF)
17064 qualified_p = true;
17065 function = tsubst_qualified_id (function, args, complain, in_decl,
17066 /*done=*/false,
17067 /*address_p=*/false);
17069 else if (koenig_p && identifier_p (function))
17071 /* Do nothing; calling tsubst_copy_and_build on an identifier
17072 would incorrectly perform unqualified lookup again.
17074 Note that we can also have an IDENTIFIER_NODE if the earlier
17075 unqualified lookup found a member function; in that case
17076 koenig_p will be false and we do want to do the lookup
17077 again to find the instantiated member function.
17079 FIXME but doing that causes c++/15272, so we need to stop
17080 using IDENTIFIER_NODE in that situation. */
17081 qualified_p = false;
17083 else
17085 if (TREE_CODE (function) == COMPONENT_REF)
17087 tree op = TREE_OPERAND (function, 1);
17089 qualified_p = (TREE_CODE (op) == SCOPE_REF
17090 || (BASELINK_P (op)
17091 && BASELINK_QUALIFIED_P (op)));
17093 else
17094 qualified_p = false;
17096 if (TREE_CODE (function) == ADDR_EXPR
17097 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17098 /* Avoid error about taking the address of a constructor. */
17099 function = TREE_OPERAND (function, 0);
17101 function = tsubst_copy_and_build (function, args, complain,
17102 in_decl,
17103 !qualified_p,
17104 integral_constant_expression_p);
17106 if (BASELINK_P (function))
17107 qualified_p = true;
17110 nargs = call_expr_nargs (t);
17111 call_args = make_tree_vector ();
17112 for (i = 0; i < nargs; ++i)
17114 tree arg = CALL_EXPR_ARG (t, i);
17116 if (!PACK_EXPANSION_P (arg))
17117 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17118 else
17120 /* Expand the pack expansion and push each entry onto
17121 CALL_ARGS. */
17122 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17123 if (TREE_CODE (arg) == TREE_VEC)
17125 unsigned int len, j;
17127 len = TREE_VEC_LENGTH (arg);
17128 for (j = 0; j < len; ++j)
17130 tree value = TREE_VEC_ELT (arg, j);
17131 if (value != NULL_TREE)
17132 value = convert_from_reference (value);
17133 vec_safe_push (call_args, value);
17136 else
17138 /* A partial substitution. Add one entry. */
17139 vec_safe_push (call_args, arg);
17144 /* We do not perform argument-dependent lookup if normal
17145 lookup finds a non-function, in accordance with the
17146 expected resolution of DR 218. */
17147 if (koenig_p
17148 && ((is_overloaded_fn (function)
17149 /* If lookup found a member function, the Koenig lookup is
17150 not appropriate, even if an unqualified-name was used
17151 to denote the function. */
17152 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17153 || identifier_p (function))
17154 /* Only do this when substitution turns a dependent call
17155 into a non-dependent call. */
17156 && type_dependent_expression_p_push (t)
17157 && !any_type_dependent_arguments_p (call_args))
17158 function = perform_koenig_lookup (function, call_args, tf_none);
17160 if (function != NULL_TREE
17161 && identifier_p (function)
17162 && !any_type_dependent_arguments_p (call_args))
17164 if (koenig_p && (complain & tf_warning_or_error))
17166 /* For backwards compatibility and good diagnostics, try
17167 the unqualified lookup again if we aren't in SFINAE
17168 context. */
17169 tree unq = (tsubst_copy_and_build
17170 (function, args, complain, in_decl, true,
17171 integral_constant_expression_p));
17172 if (unq == error_mark_node)
17174 release_tree_vector (call_args);
17175 RETURN (error_mark_node);
17178 if (unq != function)
17180 /* In a lambda fn, we have to be careful to not
17181 introduce new this captures. Legacy code can't
17182 be using lambdas anyway, so it's ok to be
17183 stricter. */
17184 bool in_lambda = (current_class_type
17185 && LAMBDA_TYPE_P (current_class_type));
17186 char const *msg = "%qD was not declared in this scope, "
17187 "and no declarations were found by "
17188 "argument-dependent lookup at the point "
17189 "of instantiation";
17191 bool diag = true;
17192 if (in_lambda)
17193 error_at (EXPR_LOC_OR_LOC (t, input_location),
17194 msg, function);
17195 else
17196 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17197 msg, function);
17198 if (diag)
17200 tree fn = unq;
17201 if (INDIRECT_REF_P (fn))
17202 fn = TREE_OPERAND (fn, 0);
17203 if (TREE_CODE (fn) == COMPONENT_REF)
17204 fn = TREE_OPERAND (fn, 1);
17205 if (is_overloaded_fn (fn))
17206 fn = get_first_fn (fn);
17208 if (!DECL_P (fn))
17209 /* Can't say anything more. */;
17210 else if (DECL_CLASS_SCOPE_P (fn))
17212 location_t loc = EXPR_LOC_OR_LOC (t,
17213 input_location);
17214 inform (loc,
17215 "declarations in dependent base %qT are "
17216 "not found by unqualified lookup",
17217 DECL_CLASS_CONTEXT (fn));
17218 if (current_class_ptr)
17219 inform (loc,
17220 "use %<this->%D%> instead", function);
17221 else
17222 inform (loc,
17223 "use %<%T::%D%> instead",
17224 current_class_name, function);
17226 else
17227 inform (DECL_SOURCE_LOCATION (fn),
17228 "%qD declared here, later in the "
17229 "translation unit", fn);
17230 if (in_lambda)
17232 release_tree_vector (call_args);
17233 RETURN (error_mark_node);
17237 function = unq;
17240 if (identifier_p (function))
17242 if (complain & tf_error)
17243 unqualified_name_lookup_error (function);
17244 release_tree_vector (call_args);
17245 RETURN (error_mark_node);
17249 /* Remember that there was a reference to this entity. */
17250 if (function != NULL_TREE
17251 && DECL_P (function)
17252 && !mark_used (function, complain) && !(complain & tf_error))
17254 release_tree_vector (call_args);
17255 RETURN (error_mark_node);
17258 /* Put back tf_decltype for the actual call. */
17259 complain |= decltype_flag;
17261 if (function == NULL_TREE)
17262 switch (CALL_EXPR_IFN (t))
17264 case IFN_LAUNDER:
17265 gcc_assert (nargs == 1);
17266 if (vec_safe_length (call_args) != 1)
17268 error_at (EXPR_LOC_OR_LOC (t, input_location),
17269 "wrong number of arguments to "
17270 "%<__builtin_launder%>");
17271 ret = error_mark_node;
17273 else
17274 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17275 input_location),
17276 (*call_args)[0], complain);
17277 break;
17279 default:
17280 /* Unsupported internal function with arguments. */
17281 gcc_unreachable ();
17283 else if (TREE_CODE (function) == OFFSET_REF)
17284 ret = build_offset_ref_call_from_tree (function, &call_args,
17285 complain);
17286 else if (TREE_CODE (function) == COMPONENT_REF)
17288 tree instance = TREE_OPERAND (function, 0);
17289 tree fn = TREE_OPERAND (function, 1);
17291 if (processing_template_decl
17292 && (type_dependent_expression_p (instance)
17293 || (!BASELINK_P (fn)
17294 && TREE_CODE (fn) != FIELD_DECL)
17295 || type_dependent_expression_p (fn)
17296 || any_type_dependent_arguments_p (call_args)))
17297 ret = build_nt_call_vec (function, call_args);
17298 else if (!BASELINK_P (fn))
17299 ret = finish_call_expr (function, &call_args,
17300 /*disallow_virtual=*/false,
17301 /*koenig_p=*/false,
17302 complain);
17303 else
17304 ret = (build_new_method_call
17305 (instance, fn,
17306 &call_args, NULL_TREE,
17307 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17308 /*fn_p=*/NULL,
17309 complain));
17311 else
17312 ret = finish_call_expr (function, &call_args,
17313 /*disallow_virtual=*/qualified_p,
17314 koenig_p,
17315 complain);
17317 release_tree_vector (call_args);
17319 if (ret != error_mark_node)
17321 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17322 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17323 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17324 bool thk = CALL_FROM_THUNK_P (t);
17325 if (op || ord || rev || thk)
17327 function = extract_call_expr (ret);
17328 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17329 CALL_EXPR_ORDERED_ARGS (function) = ord;
17330 CALL_EXPR_REVERSE_ARGS (function) = rev;
17331 if (thk)
17333 CALL_FROM_THUNK_P (function) = true;
17334 /* The thunk location is not interesting. */
17335 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17340 RETURN (ret);
17343 case COND_EXPR:
17345 tree cond = RECUR (TREE_OPERAND (t, 0));
17346 tree folded_cond = fold_non_dependent_expr (cond);
17347 tree exp1, exp2;
17349 if (TREE_CODE (folded_cond) == INTEGER_CST)
17351 if (integer_zerop (folded_cond))
17353 ++c_inhibit_evaluation_warnings;
17354 exp1 = RECUR (TREE_OPERAND (t, 1));
17355 --c_inhibit_evaluation_warnings;
17356 exp2 = RECUR (TREE_OPERAND (t, 2));
17358 else
17360 exp1 = RECUR (TREE_OPERAND (t, 1));
17361 ++c_inhibit_evaluation_warnings;
17362 exp2 = RECUR (TREE_OPERAND (t, 2));
17363 --c_inhibit_evaluation_warnings;
17365 cond = folded_cond;
17367 else
17369 exp1 = RECUR (TREE_OPERAND (t, 1));
17370 exp2 = RECUR (TREE_OPERAND (t, 2));
17373 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17374 cond, exp1, exp2, complain));
17377 case PSEUDO_DTOR_EXPR:
17379 tree op0 = RECUR (TREE_OPERAND (t, 0));
17380 tree op1 = RECUR (TREE_OPERAND (t, 1));
17381 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17382 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17383 input_location));
17386 case TREE_LIST:
17388 tree purpose, value, chain;
17390 if (t == void_list_node)
17391 RETURN (t);
17393 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17394 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17396 /* We have pack expansions, so expand those and
17397 create a new list out of it. */
17398 tree purposevec = NULL_TREE;
17399 tree valuevec = NULL_TREE;
17400 tree chain;
17401 int i, len = -1;
17403 /* Expand the argument expressions. */
17404 if (TREE_PURPOSE (t))
17405 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17406 complain, in_decl);
17407 if (TREE_VALUE (t))
17408 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17409 complain, in_decl);
17411 /* Build the rest of the list. */
17412 chain = TREE_CHAIN (t);
17413 if (chain && chain != void_type_node)
17414 chain = RECUR (chain);
17416 /* Determine the number of arguments. */
17417 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17419 len = TREE_VEC_LENGTH (purposevec);
17420 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17422 else if (TREE_CODE (valuevec) == TREE_VEC)
17423 len = TREE_VEC_LENGTH (valuevec);
17424 else
17426 /* Since we only performed a partial substitution into
17427 the argument pack, we only RETURN (a single list
17428 node. */
17429 if (purposevec == TREE_PURPOSE (t)
17430 && valuevec == TREE_VALUE (t)
17431 && chain == TREE_CHAIN (t))
17432 RETURN (t);
17434 RETURN (tree_cons (purposevec, valuevec, chain));
17437 /* Convert the argument vectors into a TREE_LIST */
17438 i = len;
17439 while (i > 0)
17441 /* Grab the Ith values. */
17442 i--;
17443 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17444 : NULL_TREE;
17445 value
17446 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17447 : NULL_TREE;
17449 /* Build the list (backwards). */
17450 chain = tree_cons (purpose, value, chain);
17453 RETURN (chain);
17456 purpose = TREE_PURPOSE (t);
17457 if (purpose)
17458 purpose = RECUR (purpose);
17459 value = TREE_VALUE (t);
17460 if (value)
17461 value = RECUR (value);
17462 chain = TREE_CHAIN (t);
17463 if (chain && chain != void_type_node)
17464 chain = RECUR (chain);
17465 if (purpose == TREE_PURPOSE (t)
17466 && value == TREE_VALUE (t)
17467 && chain == TREE_CHAIN (t))
17468 RETURN (t);
17469 RETURN (tree_cons (purpose, value, chain));
17472 case COMPONENT_REF:
17474 tree object;
17475 tree object_type;
17476 tree member;
17477 tree r;
17479 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17480 args, complain, in_decl);
17481 /* Remember that there was a reference to this entity. */
17482 if (DECL_P (object)
17483 && !mark_used (object, complain) && !(complain & tf_error))
17484 RETURN (error_mark_node);
17485 object_type = TREE_TYPE (object);
17487 member = TREE_OPERAND (t, 1);
17488 if (BASELINK_P (member))
17489 member = tsubst_baselink (member,
17490 non_reference (TREE_TYPE (object)),
17491 args, complain, in_decl);
17492 else
17493 member = tsubst_copy (member, args, complain, in_decl);
17494 if (member == error_mark_node)
17495 RETURN (error_mark_node);
17497 if (TREE_CODE (member) == FIELD_DECL)
17499 r = finish_non_static_data_member (member, object, NULL_TREE);
17500 if (TREE_CODE (r) == COMPONENT_REF)
17501 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17502 RETURN (r);
17504 else if (type_dependent_expression_p (object))
17505 /* We can't do much here. */;
17506 else if (!CLASS_TYPE_P (object_type))
17508 if (scalarish_type_p (object_type))
17510 tree s = NULL_TREE;
17511 tree dtor = member;
17513 if (TREE_CODE (dtor) == SCOPE_REF)
17515 s = TREE_OPERAND (dtor, 0);
17516 dtor = TREE_OPERAND (dtor, 1);
17518 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17520 dtor = TREE_OPERAND (dtor, 0);
17521 if (TYPE_P (dtor))
17522 RETURN (finish_pseudo_destructor_expr
17523 (object, s, dtor, input_location));
17527 else if (TREE_CODE (member) == SCOPE_REF
17528 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17530 /* Lookup the template functions now that we know what the
17531 scope is. */
17532 tree scope = TREE_OPERAND (member, 0);
17533 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17534 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17535 member = lookup_qualified_name (scope, tmpl,
17536 /*is_type_p=*/false,
17537 /*complain=*/false);
17538 if (BASELINK_P (member))
17540 BASELINK_FUNCTIONS (member)
17541 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17542 args);
17543 member = (adjust_result_of_qualified_name_lookup
17544 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17545 object_type));
17547 else
17549 qualified_name_lookup_error (scope, tmpl, member,
17550 input_location);
17551 RETURN (error_mark_node);
17554 else if (TREE_CODE (member) == SCOPE_REF
17555 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17556 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17558 if (complain & tf_error)
17560 if (TYPE_P (TREE_OPERAND (member, 0)))
17561 error ("%qT is not a class or namespace",
17562 TREE_OPERAND (member, 0));
17563 else
17564 error ("%qD is not a class or namespace",
17565 TREE_OPERAND (member, 0));
17567 RETURN (error_mark_node);
17570 r = finish_class_member_access_expr (object, member,
17571 /*template_p=*/false,
17572 complain);
17573 if (TREE_CODE (r) == COMPONENT_REF)
17574 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17575 RETURN (r);
17578 case THROW_EXPR:
17579 RETURN (build_throw
17580 (RECUR (TREE_OPERAND (t, 0))));
17582 case CONSTRUCTOR:
17584 vec<constructor_elt, va_gc> *n;
17585 constructor_elt *ce;
17586 unsigned HOST_WIDE_INT idx;
17587 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17588 bool process_index_p;
17589 int newlen;
17590 bool need_copy_p = false;
17591 tree r;
17593 if (type == error_mark_node)
17594 RETURN (error_mark_node);
17596 /* digest_init will do the wrong thing if we let it. */
17597 if (type && TYPE_PTRMEMFUNC_P (type))
17598 RETURN (t);
17600 /* We do not want to process the index of aggregate
17601 initializers as they are identifier nodes which will be
17602 looked up by digest_init. */
17603 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17605 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17606 newlen = vec_safe_length (n);
17607 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17609 if (ce->index && process_index_p
17610 /* An identifier index is looked up in the type
17611 being initialized, not the current scope. */
17612 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17613 ce->index = RECUR (ce->index);
17615 if (PACK_EXPANSION_P (ce->value))
17617 /* Substitute into the pack expansion. */
17618 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17619 in_decl);
17621 if (ce->value == error_mark_node
17622 || PACK_EXPANSION_P (ce->value))
17624 else if (TREE_VEC_LENGTH (ce->value) == 1)
17625 /* Just move the argument into place. */
17626 ce->value = TREE_VEC_ELT (ce->value, 0);
17627 else
17629 /* Update the length of the final CONSTRUCTOR
17630 arguments vector, and note that we will need to
17631 copy.*/
17632 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17633 need_copy_p = true;
17636 else
17637 ce->value = RECUR (ce->value);
17640 if (need_copy_p)
17642 vec<constructor_elt, va_gc> *old_n = n;
17644 vec_alloc (n, newlen);
17645 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17647 if (TREE_CODE (ce->value) == TREE_VEC)
17649 int i, len = TREE_VEC_LENGTH (ce->value);
17650 for (i = 0; i < len; ++i)
17651 CONSTRUCTOR_APPEND_ELT (n, 0,
17652 TREE_VEC_ELT (ce->value, i));
17654 else
17655 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17659 r = build_constructor (init_list_type_node, n);
17660 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17662 if (TREE_HAS_CONSTRUCTOR (t))
17663 RETURN (finish_compound_literal (type, r, complain));
17665 TREE_TYPE (r) = type;
17666 RETURN (r);
17669 case TYPEID_EXPR:
17671 tree operand_0 = TREE_OPERAND (t, 0);
17672 if (TYPE_P (operand_0))
17674 operand_0 = tsubst (operand_0, args, complain, in_decl);
17675 RETURN (get_typeid (operand_0, complain));
17677 else
17679 operand_0 = RECUR (operand_0);
17680 RETURN (build_typeid (operand_0, complain));
17684 case VAR_DECL:
17685 if (!args)
17686 RETURN (t);
17687 else if (DECL_PACK_P (t))
17689 /* We don't build decls for an instantiation of a
17690 variadic capture proxy, we instantiate the elements
17691 when needed. */
17692 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17693 return RECUR (DECL_VALUE_EXPR (t));
17695 /* Fall through */
17697 case PARM_DECL:
17699 tree r = tsubst_copy (t, args, complain, in_decl);
17700 /* ??? We're doing a subset of finish_id_expression here. */
17701 if (VAR_P (r)
17702 && !processing_template_decl
17703 && !cp_unevaluated_operand
17704 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17705 && CP_DECL_THREAD_LOCAL_P (r))
17707 if (tree wrap = get_tls_wrapper_fn (r))
17708 /* Replace an evaluated use of the thread_local variable with
17709 a call to its wrapper. */
17710 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17712 else if (outer_automatic_var_p (r))
17714 r = process_outer_var_ref (r, complain);
17715 if (is_capture_proxy (r))
17716 register_local_specialization (r, t);
17719 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17720 /* If the original type was a reference, we'll be wrapped in
17721 the appropriate INDIRECT_REF. */
17722 r = convert_from_reference (r);
17723 RETURN (r);
17726 case VA_ARG_EXPR:
17728 tree op0 = RECUR (TREE_OPERAND (t, 0));
17729 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17730 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17733 case OFFSETOF_EXPR:
17735 tree object_ptr
17736 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17737 in_decl, /*function_p=*/false,
17738 /*integral_constant_expression_p=*/false);
17739 RETURN (finish_offsetof (object_ptr,
17740 RECUR (TREE_OPERAND (t, 0)),
17741 EXPR_LOCATION (t)));
17744 case ADDRESSOF_EXPR:
17745 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17746 RECUR (TREE_OPERAND (t, 0)), complain));
17748 case TRAIT_EXPR:
17750 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17751 complain, in_decl);
17753 tree type2 = TRAIT_EXPR_TYPE2 (t);
17754 if (type2 && TREE_CODE (type2) == TREE_LIST)
17755 type2 = RECUR (type2);
17756 else if (type2)
17757 type2 = tsubst (type2, args, complain, in_decl);
17759 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17762 case STMT_EXPR:
17764 tree old_stmt_expr = cur_stmt_expr;
17765 tree stmt_expr = begin_stmt_expr ();
17767 cur_stmt_expr = stmt_expr;
17768 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17769 integral_constant_expression_p);
17770 stmt_expr = finish_stmt_expr (stmt_expr, false);
17771 cur_stmt_expr = old_stmt_expr;
17773 /* If the resulting list of expression statement is empty,
17774 fold it further into void_node. */
17775 if (empty_expr_stmt_p (stmt_expr))
17776 stmt_expr = void_node;
17778 RETURN (stmt_expr);
17781 case LAMBDA_EXPR:
17783 tree r = build_lambda_expr ();
17785 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17786 LAMBDA_EXPR_CLOSURE (r) = type;
17787 CLASSTYPE_LAMBDA_EXPR (type) = r;
17789 LAMBDA_EXPR_LOCATION (r)
17790 = LAMBDA_EXPR_LOCATION (t);
17791 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17792 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17793 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17794 LAMBDA_EXPR_DISCRIMINATOR (r)
17795 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17796 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17797 if (!scope)
17798 /* No substitution needed. */;
17799 else if (VAR_OR_FUNCTION_DECL_P (scope))
17800 /* For a function or variable scope, we want to use tsubst so that we
17801 don't complain about referring to an auto before deduction. */
17802 scope = tsubst (scope, args, complain, in_decl);
17803 else if (TREE_CODE (scope) == PARM_DECL)
17805 /* Look up the parameter we want directly, as tsubst_copy
17806 doesn't do what we need. */
17807 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17808 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17809 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17810 parm = DECL_CHAIN (parm);
17811 scope = parm;
17812 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17813 if (DECL_CONTEXT (scope) == NULL_TREE)
17814 DECL_CONTEXT (scope) = fn;
17816 else if (TREE_CODE (scope) == FIELD_DECL)
17817 /* For a field, use tsubst_copy so that we look up the existing field
17818 rather than build a new one. */
17819 scope = RECUR (scope);
17820 else
17821 gcc_unreachable ();
17822 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17824 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17825 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17827 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17828 determine_visibility (TYPE_NAME (type));
17829 /* Now that we know visibility, instantiate the type so we have a
17830 declaration of the op() for later calls to lambda_function. */
17831 complete_type (type);
17833 if (tree fn = lambda_function (type))
17834 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17836 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17838 insert_pending_capture_proxies ();
17840 RETURN (build_lambda_object (r));
17843 case TARGET_EXPR:
17844 /* We can get here for a constant initializer of non-dependent type.
17845 FIXME stop folding in cp_parser_initializer_clause. */
17847 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17848 complain);
17849 RETURN (r);
17852 case TRANSACTION_EXPR:
17853 RETURN (tsubst_expr(t, args, complain, in_decl,
17854 integral_constant_expression_p));
17856 case PAREN_EXPR:
17857 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17859 case VEC_PERM_EXPR:
17861 tree op0 = RECUR (TREE_OPERAND (t, 0));
17862 tree op1 = RECUR (TREE_OPERAND (t, 1));
17863 tree op2 = RECUR (TREE_OPERAND (t, 2));
17864 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17865 complain));
17868 case REQUIRES_EXPR:
17869 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17871 default:
17872 /* Handle Objective-C++ constructs, if appropriate. */
17874 tree subst
17875 = objcp_tsubst_copy_and_build (t, args, complain,
17876 in_decl, /*function_p=*/false);
17877 if (subst)
17878 RETURN (subst);
17880 RETURN (tsubst_copy (t, args, complain, in_decl));
17883 #undef RECUR
17884 #undef RETURN
17885 out:
17886 input_location = loc;
17887 return retval;
17890 /* Verify that the instantiated ARGS are valid. For type arguments,
17891 make sure that the type's linkage is ok. For non-type arguments,
17892 make sure they are constants if they are integral or enumerations.
17893 Emit an error under control of COMPLAIN, and return TRUE on error. */
17895 static bool
17896 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17898 if (dependent_template_arg_p (t))
17899 return false;
17900 if (ARGUMENT_PACK_P (t))
17902 tree vec = ARGUMENT_PACK_ARGS (t);
17903 int len = TREE_VEC_LENGTH (vec);
17904 bool result = false;
17905 int i;
17907 for (i = 0; i < len; ++i)
17908 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17909 result = true;
17910 return result;
17912 else if (TYPE_P (t))
17914 /* [basic.link]: A name with no linkage (notably, the name
17915 of a class or enumeration declared in a local scope)
17916 shall not be used to declare an entity with linkage.
17917 This implies that names with no linkage cannot be used as
17918 template arguments
17920 DR 757 relaxes this restriction for C++0x. */
17921 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17922 : no_linkage_check (t, /*relaxed_p=*/false));
17924 if (nt)
17926 /* DR 488 makes use of a type with no linkage cause
17927 type deduction to fail. */
17928 if (complain & tf_error)
17930 if (TYPE_UNNAMED_P (nt))
17931 error ("%qT is/uses unnamed type", t);
17932 else
17933 error ("template argument for %qD uses local type %qT",
17934 tmpl, t);
17936 return true;
17938 /* In order to avoid all sorts of complications, we do not
17939 allow variably-modified types as template arguments. */
17940 else if (variably_modified_type_p (t, NULL_TREE))
17942 if (complain & tf_error)
17943 error ("%qT is a variably modified type", t);
17944 return true;
17947 /* Class template and alias template arguments should be OK. */
17948 else if (DECL_TYPE_TEMPLATE_P (t))
17950 /* A non-type argument of integral or enumerated type must be a
17951 constant. */
17952 else if (TREE_TYPE (t)
17953 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17954 && !REFERENCE_REF_P (t)
17955 && !TREE_CONSTANT (t))
17957 if (complain & tf_error)
17958 error ("integral expression %qE is not constant", t);
17959 return true;
17961 return false;
17964 static bool
17965 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17967 int ix, len = DECL_NTPARMS (tmpl);
17968 bool result = false;
17970 for (ix = 0; ix != len; ix++)
17972 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17973 result = true;
17975 if (result && (complain & tf_error))
17976 error (" trying to instantiate %qD", tmpl);
17977 return result;
17980 /* We're out of SFINAE context now, so generate diagnostics for the access
17981 errors we saw earlier when instantiating D from TMPL and ARGS. */
17983 static void
17984 recheck_decl_substitution (tree d, tree tmpl, tree args)
17986 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17987 tree type = TREE_TYPE (pattern);
17988 location_t loc = input_location;
17990 push_access_scope (d);
17991 push_deferring_access_checks (dk_no_deferred);
17992 input_location = DECL_SOURCE_LOCATION (pattern);
17993 tsubst (type, args, tf_warning_or_error, d);
17994 input_location = loc;
17995 pop_deferring_access_checks ();
17996 pop_access_scope (d);
17999 /* Instantiate the indicated variable, function, or alias template TMPL with
18000 the template arguments in TARG_PTR. */
18002 static tree
18003 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18005 tree targ_ptr = orig_args;
18006 tree fndecl;
18007 tree gen_tmpl;
18008 tree spec;
18009 bool access_ok = true;
18011 if (tmpl == error_mark_node)
18012 return error_mark_node;
18014 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18016 /* If this function is a clone, handle it specially. */
18017 if (DECL_CLONED_FUNCTION_P (tmpl))
18019 tree spec;
18020 tree clone;
18022 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18023 DECL_CLONED_FUNCTION. */
18024 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18025 targ_ptr, complain);
18026 if (spec == error_mark_node)
18027 return error_mark_node;
18029 /* Look for the clone. */
18030 FOR_EACH_CLONE (clone, spec)
18031 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18032 return clone;
18033 /* We should always have found the clone by now. */
18034 gcc_unreachable ();
18035 return NULL_TREE;
18038 if (targ_ptr == error_mark_node)
18039 return error_mark_node;
18041 /* Check to see if we already have this specialization. */
18042 gen_tmpl = most_general_template (tmpl);
18043 if (TMPL_ARGS_DEPTH (targ_ptr)
18044 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18045 /* targ_ptr only has the innermost template args, so add the outer ones
18046 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18047 the case of a non-dependent call within a template definition). */
18048 targ_ptr = (add_outermost_template_args
18049 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18050 targ_ptr));
18052 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18053 but it doesn't seem to be on the hot path. */
18054 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18056 gcc_assert (tmpl == gen_tmpl
18057 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18058 == spec)
18059 || fndecl == NULL_TREE);
18061 if (spec != NULL_TREE)
18063 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18065 if (complain & tf_error)
18066 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18067 return error_mark_node;
18069 return spec;
18072 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18073 complain))
18074 return error_mark_node;
18076 /* We are building a FUNCTION_DECL, during which the access of its
18077 parameters and return types have to be checked. However this
18078 FUNCTION_DECL which is the desired context for access checking
18079 is not built yet. We solve this chicken-and-egg problem by
18080 deferring all checks until we have the FUNCTION_DECL. */
18081 push_deferring_access_checks (dk_deferred);
18083 /* Instantiation of the function happens in the context of the function
18084 template, not the context of the overload resolution we're doing. */
18085 push_to_top_level ();
18086 /* If there are dependent arguments, e.g. because we're doing partial
18087 ordering, make sure processing_template_decl stays set. */
18088 if (uses_template_parms (targ_ptr))
18089 ++processing_template_decl;
18090 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18092 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18093 complain, gen_tmpl, true);
18094 push_nested_class (ctx);
18097 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18099 fndecl = NULL_TREE;
18100 if (VAR_P (pattern))
18102 /* We need to determine if we're using a partial or explicit
18103 specialization now, because the type of the variable could be
18104 different. */
18105 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18106 tree elt = most_specialized_partial_spec (tid, complain);
18107 if (elt == error_mark_node)
18108 pattern = error_mark_node;
18109 else if (elt)
18111 tree partial_tmpl = TREE_VALUE (elt);
18112 tree partial_args = TREE_PURPOSE (elt);
18113 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18114 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18118 /* Substitute template parameters to obtain the specialization. */
18119 if (fndecl == NULL_TREE)
18120 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18121 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18122 pop_nested_class ();
18123 pop_from_top_level ();
18125 if (fndecl == error_mark_node)
18127 pop_deferring_access_checks ();
18128 return error_mark_node;
18131 /* The DECL_TI_TEMPLATE should always be the immediate parent
18132 template, not the most general template. */
18133 DECL_TI_TEMPLATE (fndecl) = tmpl;
18134 DECL_TI_ARGS (fndecl) = targ_ptr;
18136 /* Now we know the specialization, compute access previously
18137 deferred. Do no access control for inheriting constructors,
18138 as we already checked access for the inherited constructor. */
18139 if (!(flag_new_inheriting_ctors
18140 && DECL_INHERITED_CTOR (fndecl)))
18142 push_access_scope (fndecl);
18143 if (!perform_deferred_access_checks (complain))
18144 access_ok = false;
18145 pop_access_scope (fndecl);
18147 pop_deferring_access_checks ();
18149 /* If we've just instantiated the main entry point for a function,
18150 instantiate all the alternate entry points as well. We do this
18151 by cloning the instantiation of the main entry point, not by
18152 instantiating the template clones. */
18153 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18154 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
18156 if (!access_ok)
18158 if (!(complain & tf_error))
18160 /* Remember to reinstantiate when we're out of SFINAE so the user
18161 can see the errors. */
18162 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18164 return error_mark_node;
18166 return fndecl;
18169 /* Wrapper for instantiate_template_1. */
18171 tree
18172 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18174 tree ret;
18175 timevar_push (TV_TEMPLATE_INST);
18176 ret = instantiate_template_1 (tmpl, orig_args, complain);
18177 timevar_pop (TV_TEMPLATE_INST);
18178 return ret;
18181 /* Instantiate the alias template TMPL with ARGS. Also push a template
18182 instantiation level, which instantiate_template doesn't do because
18183 functions and variables have sufficient context established by the
18184 callers. */
18186 static tree
18187 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18189 struct pending_template *old_last_pend = last_pending_template;
18190 struct tinst_level *old_error_tinst = last_error_tinst_level;
18191 if (tmpl == error_mark_node || args == error_mark_node)
18192 return error_mark_node;
18193 tree tinst = build_tree_list (tmpl, args);
18194 if (!push_tinst_level (tinst))
18196 ggc_free (tinst);
18197 return error_mark_node;
18200 args =
18201 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18202 args, tmpl, complain,
18203 /*require_all_args=*/true,
18204 /*use_default_args=*/true);
18206 tree r = instantiate_template (tmpl, args, complain);
18207 pop_tinst_level ();
18208 /* We can't free this if a pending_template entry or last_error_tinst_level
18209 is pointing at it. */
18210 if (last_pending_template == old_last_pend
18211 && last_error_tinst_level == old_error_tinst)
18212 ggc_free (tinst);
18214 return r;
18217 /* PARM is a template parameter pack for FN. Returns true iff
18218 PARM is used in a deducible way in the argument list of FN. */
18220 static bool
18221 pack_deducible_p (tree parm, tree fn)
18223 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18224 for (; t; t = TREE_CHAIN (t))
18226 tree type = TREE_VALUE (t);
18227 tree packs;
18228 if (!PACK_EXPANSION_P (type))
18229 continue;
18230 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18231 packs; packs = TREE_CHAIN (packs))
18232 if (template_args_equal (TREE_VALUE (packs), parm))
18234 /* The template parameter pack is used in a function parameter
18235 pack. If this is the end of the parameter list, the
18236 template parameter pack is deducible. */
18237 if (TREE_CHAIN (t) == void_list_node)
18238 return true;
18239 else
18240 /* Otherwise, not. Well, it could be deduced from
18241 a non-pack parameter, but doing so would end up with
18242 a deduction mismatch, so don't bother. */
18243 return false;
18246 /* The template parameter pack isn't used in any function parameter
18247 packs, but it might be used deeper, e.g. tuple<Args...>. */
18248 return true;
18251 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18252 NARGS elements of the arguments that are being used when calling
18253 it. TARGS is a vector into which the deduced template arguments
18254 are placed.
18256 Returns either a FUNCTION_DECL for the matching specialization of FN or
18257 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18258 true, diagnostics will be printed to explain why it failed.
18260 If FN is a conversion operator, or we are trying to produce a specific
18261 specialization, RETURN_TYPE is the return type desired.
18263 The EXPLICIT_TARGS are explicit template arguments provided via a
18264 template-id.
18266 The parameter STRICT is one of:
18268 DEDUCE_CALL:
18269 We are deducing arguments for a function call, as in
18270 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18271 deducing arguments for a call to the result of a conversion
18272 function template, as in [over.call.object].
18274 DEDUCE_CONV:
18275 We are deducing arguments for a conversion function, as in
18276 [temp.deduct.conv].
18278 DEDUCE_EXACT:
18279 We are deducing arguments when doing an explicit instantiation
18280 as in [temp.explicit], when determining an explicit specialization
18281 as in [temp.expl.spec], or when taking the address of a function
18282 template, as in [temp.deduct.funcaddr]. */
18284 tree
18285 fn_type_unification (tree fn,
18286 tree explicit_targs,
18287 tree targs,
18288 const tree *args,
18289 unsigned int nargs,
18290 tree return_type,
18291 unification_kind_t strict,
18292 int flags,
18293 bool explain_p,
18294 bool decltype_p)
18296 tree parms;
18297 tree fntype;
18298 tree decl = NULL_TREE;
18299 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18300 bool ok;
18301 static int deduction_depth;
18302 struct pending_template *old_last_pend = last_pending_template;
18303 struct tinst_level *old_error_tinst = last_error_tinst_level;
18305 tree orig_fn = fn;
18306 if (flag_new_inheriting_ctors)
18307 fn = strip_inheriting_ctors (fn);
18309 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18310 tree tinst;
18311 tree r = error_mark_node;
18313 tree full_targs = targs;
18314 if (TMPL_ARGS_DEPTH (targs)
18315 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18316 full_targs = (add_outermost_template_args
18317 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18318 targs));
18320 if (decltype_p)
18321 complain |= tf_decltype;
18323 /* In C++0x, it's possible to have a function template whose type depends
18324 on itself recursively. This is most obvious with decltype, but can also
18325 occur with enumeration scope (c++/48969). So we need to catch infinite
18326 recursion and reject the substitution at deduction time; this function
18327 will return error_mark_node for any repeated substitution.
18329 This also catches excessive recursion such as when f<N> depends on
18330 f<N-1> across all integers, and returns error_mark_node for all the
18331 substitutions back up to the initial one.
18333 This is, of course, not reentrant. */
18334 if (excessive_deduction_depth)
18335 return error_mark_node;
18336 tinst = build_tree_list (fn, NULL_TREE);
18337 ++deduction_depth;
18339 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18341 fntype = TREE_TYPE (fn);
18342 if (explicit_targs)
18344 /* [temp.deduct]
18346 The specified template arguments must match the template
18347 parameters in kind (i.e., type, nontype, template), and there
18348 must not be more arguments than there are parameters;
18349 otherwise type deduction fails.
18351 Nontype arguments must match the types of the corresponding
18352 nontype template parameters, or must be convertible to the
18353 types of the corresponding nontype parameters as specified in
18354 _temp.arg.nontype_, otherwise type deduction fails.
18356 All references in the function type of the function template
18357 to the corresponding template parameters are replaced by the
18358 specified template argument values. If a substitution in a
18359 template parameter or in the function type of the function
18360 template results in an invalid type, type deduction fails. */
18361 int i, len = TREE_VEC_LENGTH (tparms);
18362 location_t loc = input_location;
18363 bool incomplete = false;
18365 if (explicit_targs == error_mark_node)
18366 goto fail;
18368 if (TMPL_ARGS_DEPTH (explicit_targs)
18369 < TMPL_ARGS_DEPTH (full_targs))
18370 explicit_targs = add_outermost_template_args (full_targs,
18371 explicit_targs);
18373 /* Adjust any explicit template arguments before entering the
18374 substitution context. */
18375 explicit_targs
18376 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18377 complain,
18378 /*require_all_args=*/false,
18379 /*use_default_args=*/false));
18380 if (explicit_targs == error_mark_node)
18381 goto fail;
18383 /* Substitute the explicit args into the function type. This is
18384 necessary so that, for instance, explicitly declared function
18385 arguments can match null pointed constants. If we were given
18386 an incomplete set of explicit args, we must not do semantic
18387 processing during substitution as we could create partial
18388 instantiations. */
18389 for (i = 0; i < len; i++)
18391 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18392 bool parameter_pack = false;
18393 tree targ = TREE_VEC_ELT (explicit_targs, i);
18395 /* Dig out the actual parm. */
18396 if (TREE_CODE (parm) == TYPE_DECL
18397 || TREE_CODE (parm) == TEMPLATE_DECL)
18399 parm = TREE_TYPE (parm);
18400 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18402 else if (TREE_CODE (parm) == PARM_DECL)
18404 parm = DECL_INITIAL (parm);
18405 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18408 if (!parameter_pack && targ == NULL_TREE)
18409 /* No explicit argument for this template parameter. */
18410 incomplete = true;
18412 if (parameter_pack && pack_deducible_p (parm, fn))
18414 /* Mark the argument pack as "incomplete". We could
18415 still deduce more arguments during unification.
18416 We remove this mark in type_unification_real. */
18417 if (targ)
18419 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18420 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18421 = ARGUMENT_PACK_ARGS (targ);
18424 /* We have some incomplete argument packs. */
18425 incomplete = true;
18429 TREE_VALUE (tinst) = explicit_targs;
18430 if (!push_tinst_level (tinst))
18432 excessive_deduction_depth = true;
18433 goto fail;
18435 processing_template_decl += incomplete;
18436 input_location = DECL_SOURCE_LOCATION (fn);
18437 /* Ignore any access checks; we'll see them again in
18438 instantiate_template and they might have the wrong
18439 access path at this point. */
18440 push_deferring_access_checks (dk_deferred);
18441 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18442 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18443 pop_deferring_access_checks ();
18444 input_location = loc;
18445 processing_template_decl -= incomplete;
18446 pop_tinst_level ();
18448 if (fntype == error_mark_node)
18449 goto fail;
18451 /* Place the explicitly specified arguments in TARGS. */
18452 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18453 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18454 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18457 /* Never do unification on the 'this' parameter. */
18458 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18460 if (return_type && strict == DEDUCE_CALL)
18462 /* We're deducing for a call to the result of a template conversion
18463 function. The parms we really want are in return_type. */
18464 if (POINTER_TYPE_P (return_type))
18465 return_type = TREE_TYPE (return_type);
18466 parms = TYPE_ARG_TYPES (return_type);
18468 else if (return_type)
18470 tree *new_args;
18472 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18473 new_args = XALLOCAVEC (tree, nargs + 1);
18474 new_args[0] = return_type;
18475 memcpy (new_args + 1, args, nargs * sizeof (tree));
18476 args = new_args;
18477 ++nargs;
18480 /* We allow incomplete unification without an error message here
18481 because the standard doesn't seem to explicitly prohibit it. Our
18482 callers must be ready to deal with unification failures in any
18483 event. */
18485 TREE_VALUE (tinst) = targs;
18486 /* If we aren't explaining yet, push tinst context so we can see where
18487 any errors (e.g. from class instantiations triggered by instantiation
18488 of default template arguments) come from. If we are explaining, this
18489 context is redundant. */
18490 if (!explain_p && !push_tinst_level (tinst))
18492 excessive_deduction_depth = true;
18493 goto fail;
18496 /* type_unification_real will pass back any access checks from default
18497 template argument substitution. */
18498 vec<deferred_access_check, va_gc> *checks;
18499 checks = NULL;
18501 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18502 full_targs, parms, args, nargs, /*subr=*/0,
18503 strict, flags, &checks, explain_p);
18504 if (!explain_p)
18505 pop_tinst_level ();
18506 if (!ok)
18507 goto fail;
18509 /* Now that we have bindings for all of the template arguments,
18510 ensure that the arguments deduced for the template template
18511 parameters have compatible template parameter lists. We cannot
18512 check this property before we have deduced all template
18513 arguments, because the template parameter types of a template
18514 template parameter might depend on prior template parameters
18515 deduced after the template template parameter. The following
18516 ill-formed example illustrates this issue:
18518 template<typename T, template<T> class C> void f(C<5>, T);
18520 template<int N> struct X {};
18522 void g() {
18523 f(X<5>(), 5l); // error: template argument deduction fails
18526 The template parameter list of 'C' depends on the template type
18527 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18528 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18529 time that we deduce 'C'. */
18530 if (!template_template_parm_bindings_ok_p
18531 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18533 unify_inconsistent_template_template_parameters (explain_p);
18534 goto fail;
18537 /* All is well so far. Now, check:
18539 [temp.deduct]
18541 When all template arguments have been deduced, all uses of
18542 template parameters in nondeduced contexts are replaced with
18543 the corresponding deduced argument values. If the
18544 substitution results in an invalid type, as described above,
18545 type deduction fails. */
18546 TREE_VALUE (tinst) = targs;
18547 if (!push_tinst_level (tinst))
18549 excessive_deduction_depth = true;
18550 goto fail;
18553 /* Also collect access checks from the instantiation. */
18554 reopen_deferring_access_checks (checks);
18556 decl = instantiate_template (fn, targs, complain);
18558 checks = get_deferred_access_checks ();
18559 pop_deferring_access_checks ();
18561 pop_tinst_level ();
18563 if (decl == error_mark_node)
18564 goto fail;
18566 /* Now perform any access checks encountered during substitution. */
18567 push_access_scope (decl);
18568 ok = perform_access_checks (checks, complain);
18569 pop_access_scope (decl);
18570 if (!ok)
18571 goto fail;
18573 /* If we're looking for an exact match, check that what we got
18574 is indeed an exact match. It might not be if some template
18575 parameters are used in non-deduced contexts. But don't check
18576 for an exact match if we have dependent template arguments;
18577 in that case we're doing partial ordering, and we already know
18578 that we have two candidates that will provide the actual type. */
18579 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18581 tree substed = TREE_TYPE (decl);
18582 unsigned int i;
18584 tree sarg
18585 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18586 if (return_type)
18587 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18588 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18589 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18591 unify_type_mismatch (explain_p, args[i],
18592 TREE_VALUE (sarg));
18593 goto fail;
18597 /* After doing deduction with the inherited constructor, actually return an
18598 instantiation of the inheriting constructor. */
18599 if (orig_fn != fn)
18600 decl = instantiate_template (orig_fn, targs, complain);
18602 r = decl;
18604 fail:
18605 --deduction_depth;
18606 if (excessive_deduction_depth)
18608 if (deduction_depth == 0)
18609 /* Reset once we're all the way out. */
18610 excessive_deduction_depth = false;
18613 /* We can't free this if a pending_template entry or last_error_tinst_level
18614 is pointing at it. */
18615 if (last_pending_template == old_last_pend
18616 && last_error_tinst_level == old_error_tinst)
18617 ggc_free (tinst);
18619 return r;
18622 /* Adjust types before performing type deduction, as described in
18623 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18624 sections are symmetric. PARM is the type of a function parameter
18625 or the return type of the conversion function. ARG is the type of
18626 the argument passed to the call, or the type of the value
18627 initialized with the result of the conversion function.
18628 ARG_EXPR is the original argument expression, which may be null. */
18630 static int
18631 maybe_adjust_types_for_deduction (unification_kind_t strict,
18632 tree* parm,
18633 tree* arg,
18634 tree arg_expr)
18636 int result = 0;
18638 switch (strict)
18640 case DEDUCE_CALL:
18641 break;
18643 case DEDUCE_CONV:
18644 /* Swap PARM and ARG throughout the remainder of this
18645 function; the handling is precisely symmetric since PARM
18646 will initialize ARG rather than vice versa. */
18647 std::swap (parm, arg);
18648 break;
18650 case DEDUCE_EXACT:
18651 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18652 too, but here handle it by stripping the reference from PARM
18653 rather than by adding it to ARG. */
18654 if (TREE_CODE (*parm) == REFERENCE_TYPE
18655 && TYPE_REF_IS_RVALUE (*parm)
18656 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18657 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18658 && TREE_CODE (*arg) == REFERENCE_TYPE
18659 && !TYPE_REF_IS_RVALUE (*arg))
18660 *parm = TREE_TYPE (*parm);
18661 /* Nothing else to do in this case. */
18662 return 0;
18664 default:
18665 gcc_unreachable ();
18668 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18670 /* [temp.deduct.call]
18672 If P is not a reference type:
18674 --If A is an array type, the pointer type produced by the
18675 array-to-pointer standard conversion (_conv.array_) is
18676 used in place of A for type deduction; otherwise,
18678 --If A is a function type, the pointer type produced by
18679 the function-to-pointer standard conversion
18680 (_conv.func_) is used in place of A for type deduction;
18681 otherwise,
18683 --If A is a cv-qualified type, the top level
18684 cv-qualifiers of A's type are ignored for type
18685 deduction. */
18686 if (TREE_CODE (*arg) == ARRAY_TYPE)
18687 *arg = build_pointer_type (TREE_TYPE (*arg));
18688 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18689 *arg = build_pointer_type (*arg);
18690 else
18691 *arg = TYPE_MAIN_VARIANT (*arg);
18694 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18695 reference to a cv-unqualified template parameter that does not represent a
18696 template parameter of a class template (during class template argument
18697 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18698 an lvalue, the type "lvalue reference to A" is used in place of A for type
18699 deduction. */
18700 if (TREE_CODE (*parm) == REFERENCE_TYPE
18701 && TYPE_REF_IS_RVALUE (*parm)
18702 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18703 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18704 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18705 && (arg_expr ? lvalue_p (arg_expr)
18706 /* try_one_overload doesn't provide an arg_expr, but
18707 functions are always lvalues. */
18708 : TREE_CODE (*arg) == FUNCTION_TYPE))
18709 *arg = build_reference_type (*arg);
18711 /* [temp.deduct.call]
18713 If P is a cv-qualified type, the top level cv-qualifiers
18714 of P's type are ignored for type deduction. If P is a
18715 reference type, the type referred to by P is used for
18716 type deduction. */
18717 *parm = TYPE_MAIN_VARIANT (*parm);
18718 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18720 *parm = TREE_TYPE (*parm);
18721 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18724 /* DR 322. For conversion deduction, remove a reference type on parm
18725 too (which has been swapped into ARG). */
18726 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18727 *arg = TREE_TYPE (*arg);
18729 return result;
18732 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18733 template which does contain any deducible template parameters; check if
18734 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18735 unify_one_argument. */
18737 static int
18738 check_non_deducible_conversion (tree parm, tree arg, int strict,
18739 int flags, bool explain_p)
18741 tree type;
18743 if (!TYPE_P (arg))
18744 type = TREE_TYPE (arg);
18745 else
18746 type = arg;
18748 if (same_type_p (parm, type))
18749 return unify_success (explain_p);
18751 if (strict == DEDUCE_CONV)
18753 if (can_convert_arg (type, parm, NULL_TREE, flags,
18754 explain_p ? tf_warning_or_error : tf_none))
18755 return unify_success (explain_p);
18757 else if (strict != DEDUCE_EXACT)
18759 if (can_convert_arg (parm, type,
18760 TYPE_P (arg) ? NULL_TREE : arg,
18761 flags, explain_p ? tf_warning_or_error : tf_none))
18762 return unify_success (explain_p);
18765 if (strict == DEDUCE_EXACT)
18766 return unify_type_mismatch (explain_p, parm, arg);
18767 else
18768 return unify_arg_conversion (explain_p, parm, type, arg);
18771 static bool uses_deducible_template_parms (tree type);
18773 /* Returns true iff the expression EXPR is one from which a template
18774 argument can be deduced. In other words, if it's an undecorated
18775 use of a template non-type parameter. */
18777 static bool
18778 deducible_expression (tree expr)
18780 /* Strip implicit conversions. */
18781 while (CONVERT_EXPR_P (expr))
18782 expr = TREE_OPERAND (expr, 0);
18783 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18786 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18787 deducible way; that is, if it has a max value of <PARM> - 1. */
18789 static bool
18790 deducible_array_bound (tree domain)
18792 if (domain == NULL_TREE)
18793 return false;
18795 tree max = TYPE_MAX_VALUE (domain);
18796 if (TREE_CODE (max) != MINUS_EXPR)
18797 return false;
18799 return deducible_expression (TREE_OPERAND (max, 0));
18802 /* Returns true iff the template arguments ARGS use a template parameter
18803 in a deducible way. */
18805 static bool
18806 deducible_template_args (tree args)
18808 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18810 bool deducible;
18811 tree elt = TREE_VEC_ELT (args, i);
18812 if (ARGUMENT_PACK_P (elt))
18813 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18814 else
18816 if (PACK_EXPANSION_P (elt))
18817 elt = PACK_EXPANSION_PATTERN (elt);
18818 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18819 deducible = true;
18820 else if (TYPE_P (elt))
18821 deducible = uses_deducible_template_parms (elt);
18822 else
18823 deducible = deducible_expression (elt);
18825 if (deducible)
18826 return true;
18828 return false;
18831 /* Returns true iff TYPE contains any deducible references to template
18832 parameters, as per 14.8.2.5. */
18834 static bool
18835 uses_deducible_template_parms (tree type)
18837 if (PACK_EXPANSION_P (type))
18838 type = PACK_EXPANSION_PATTERN (type);
18840 /* T
18841 cv-list T
18842 TT<T>
18843 TT<i>
18844 TT<> */
18845 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18846 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18847 return true;
18849 /* T*
18851 T&& */
18852 if (POINTER_TYPE_P (type))
18853 return uses_deducible_template_parms (TREE_TYPE (type));
18855 /* T[integer-constant ]
18856 type [i] */
18857 if (TREE_CODE (type) == ARRAY_TYPE)
18858 return (uses_deducible_template_parms (TREE_TYPE (type))
18859 || deducible_array_bound (TYPE_DOMAIN (type)));
18861 /* T type ::*
18862 type T::*
18863 T T::*
18864 T (type ::*)()
18865 type (T::*)()
18866 type (type ::*)(T)
18867 type (T::*)(T)
18868 T (type ::*)(T)
18869 T (T::*)()
18870 T (T::*)(T) */
18871 if (TYPE_PTRMEM_P (type))
18872 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18873 || (uses_deducible_template_parms
18874 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18876 /* template-name <T> (where template-name refers to a class template)
18877 template-name <i> (where template-name refers to a class template) */
18878 if (CLASS_TYPE_P (type)
18879 && CLASSTYPE_TEMPLATE_INFO (type)
18880 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18881 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18882 (CLASSTYPE_TI_ARGS (type)));
18884 /* type (T)
18886 T(T) */
18887 if (TREE_CODE (type) == FUNCTION_TYPE
18888 || TREE_CODE (type) == METHOD_TYPE)
18890 if (uses_deducible_template_parms (TREE_TYPE (type)))
18891 return true;
18892 tree parm = TYPE_ARG_TYPES (type);
18893 if (TREE_CODE (type) == METHOD_TYPE)
18894 parm = TREE_CHAIN (parm);
18895 for (; parm; parm = TREE_CHAIN (parm))
18896 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18897 return true;
18900 return false;
18903 /* Subroutine of type_unification_real and unify_pack_expansion to
18904 handle unification of a single P/A pair. Parameters are as
18905 for those functions. */
18907 static int
18908 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18909 int subr, unification_kind_t strict,
18910 bool explain_p)
18912 tree arg_expr = NULL_TREE;
18913 int arg_strict;
18915 if (arg == error_mark_node || parm == error_mark_node)
18916 return unify_invalid (explain_p);
18917 if (arg == unknown_type_node)
18918 /* We can't deduce anything from this, but we might get all the
18919 template args from other function args. */
18920 return unify_success (explain_p);
18922 /* Implicit conversions (Clause 4) will be performed on a function
18923 argument to convert it to the type of the corresponding function
18924 parameter if the parameter type contains no template-parameters that
18925 participate in template argument deduction. */
18926 if (strict != DEDUCE_EXACT
18927 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18928 /* For function parameters with no deducible template parameters,
18929 just return. We'll check non-dependent conversions later. */
18930 return unify_success (explain_p);
18932 switch (strict)
18934 case DEDUCE_CALL:
18935 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18936 | UNIFY_ALLOW_MORE_CV_QUAL
18937 | UNIFY_ALLOW_DERIVED);
18938 break;
18940 case DEDUCE_CONV:
18941 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18942 break;
18944 case DEDUCE_EXACT:
18945 arg_strict = UNIFY_ALLOW_NONE;
18946 break;
18948 default:
18949 gcc_unreachable ();
18952 /* We only do these transformations if this is the top-level
18953 parameter_type_list in a call or declaration matching; in other
18954 situations (nested function declarators, template argument lists) we
18955 won't be comparing a type to an expression, and we don't do any type
18956 adjustments. */
18957 if (!subr)
18959 if (!TYPE_P (arg))
18961 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18962 if (type_unknown_p (arg))
18964 /* [temp.deduct.type] A template-argument can be
18965 deduced from a pointer to function or pointer
18966 to member function argument if the set of
18967 overloaded functions does not contain function
18968 templates and at most one of a set of
18969 overloaded functions provides a unique
18970 match. */
18972 if (resolve_overloaded_unification
18973 (tparms, targs, parm, arg, strict,
18974 arg_strict, explain_p))
18975 return unify_success (explain_p);
18976 return unify_overload_resolution_failure (explain_p, arg);
18979 arg_expr = arg;
18980 arg = unlowered_expr_type (arg);
18981 if (arg == error_mark_node)
18982 return unify_invalid (explain_p);
18985 arg_strict |=
18986 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18988 else
18989 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18990 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18991 return unify_template_argument_mismatch (explain_p, parm, arg);
18993 /* For deduction from an init-list we need the actual list. */
18994 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18995 arg = arg_expr;
18996 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18999 /* for_each_template_parm callback that always returns 0. */
19001 static int
19002 zero_r (tree, void *)
19004 return 0;
19007 /* for_each_template_parm any_fn callback to handle deduction of a template
19008 type argument from the type of an array bound. */
19010 static int
19011 array_deduction_r (tree t, void *data)
19013 tree_pair_p d = (tree_pair_p)data;
19014 tree &tparms = d->purpose;
19015 tree &targs = d->value;
19017 if (TREE_CODE (t) == ARRAY_TYPE)
19018 if (tree dom = TYPE_DOMAIN (t))
19019 if (tree max = TYPE_MAX_VALUE (dom))
19021 if (TREE_CODE (max) == MINUS_EXPR)
19022 max = TREE_OPERAND (max, 0);
19023 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19024 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19025 UNIFY_ALLOW_NONE, /*explain*/false);
19028 /* Keep walking. */
19029 return 0;
19032 /* Try to deduce any not-yet-deduced template type arguments from the type of
19033 an array bound. This is handled separately from unify because 14.8.2.5 says
19034 "The type of a type parameter is only deduced from an array bound if it is
19035 not otherwise deduced." */
19037 static void
19038 try_array_deduction (tree tparms, tree targs, tree parm)
19040 tree_pair_s data = { tparms, targs };
19041 hash_set<tree> visited;
19042 for_each_template_parm (parm, zero_r, &data, &visited,
19043 /*nondeduced*/false, array_deduction_r);
19046 /* Most parms like fn_type_unification.
19048 If SUBR is 1, we're being called recursively (to unify the
19049 arguments of a function or method parameter of a function
19050 template).
19052 CHECKS is a pointer to a vector of access checks encountered while
19053 substituting default template arguments. */
19055 static int
19056 type_unification_real (tree tparms,
19057 tree full_targs,
19058 tree xparms,
19059 const tree *xargs,
19060 unsigned int xnargs,
19061 int subr,
19062 unification_kind_t strict,
19063 int flags,
19064 vec<deferred_access_check, va_gc> **checks,
19065 bool explain_p)
19067 tree parm, arg;
19068 int i;
19069 int ntparms = TREE_VEC_LENGTH (tparms);
19070 int saw_undeduced = 0;
19071 tree parms;
19072 const tree *args;
19073 unsigned int nargs;
19074 unsigned int ia;
19076 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19077 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19078 gcc_assert (ntparms > 0);
19080 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19082 /* Reset the number of non-defaulted template arguments contained
19083 in TARGS. */
19084 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19086 again:
19087 parms = xparms;
19088 args = xargs;
19089 nargs = xnargs;
19091 ia = 0;
19092 while (parms && parms != void_list_node
19093 && ia < nargs)
19095 parm = TREE_VALUE (parms);
19097 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19098 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19099 /* For a function parameter pack that occurs at the end of the
19100 parameter-declaration-list, the type A of each remaining
19101 argument of the call is compared with the type P of the
19102 declarator-id of the function parameter pack. */
19103 break;
19105 parms = TREE_CHAIN (parms);
19107 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19108 /* For a function parameter pack that does not occur at the
19109 end of the parameter-declaration-list, the type of the
19110 parameter pack is a non-deduced context. */
19111 continue;
19113 arg = args[ia];
19114 ++ia;
19116 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19117 explain_p))
19118 return 1;
19121 if (parms
19122 && parms != void_list_node
19123 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19125 /* Unify the remaining arguments with the pack expansion type. */
19126 tree argvec;
19127 tree parmvec = make_tree_vec (1);
19129 /* Allocate a TREE_VEC and copy in all of the arguments */
19130 argvec = make_tree_vec (nargs - ia);
19131 for (i = 0; ia < nargs; ++ia, ++i)
19132 TREE_VEC_ELT (argvec, i) = args[ia];
19134 /* Copy the parameter into parmvec. */
19135 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19136 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19137 /*subr=*/subr, explain_p))
19138 return 1;
19140 /* Advance to the end of the list of parameters. */
19141 parms = TREE_CHAIN (parms);
19144 /* Fail if we've reached the end of the parm list, and more args
19145 are present, and the parm list isn't variadic. */
19146 if (ia < nargs && parms == void_list_node)
19147 return unify_too_many_arguments (explain_p, nargs, ia);
19148 /* Fail if parms are left and they don't have default values and
19149 they aren't all deduced as empty packs (c++/57397). This is
19150 consistent with sufficient_parms_p. */
19151 if (parms && parms != void_list_node
19152 && TREE_PURPOSE (parms) == NULL_TREE)
19154 unsigned int count = nargs;
19155 tree p = parms;
19156 bool type_pack_p;
19159 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19160 if (!type_pack_p)
19161 count++;
19162 p = TREE_CHAIN (p);
19164 while (p && p != void_list_node);
19165 if (count != nargs)
19166 return unify_too_few_arguments (explain_p, ia, count,
19167 type_pack_p);
19170 if (!subr)
19172 tsubst_flags_t complain = (explain_p
19173 ? tf_warning_or_error
19174 : tf_none);
19175 bool tried_array_deduction = (cxx_dialect < cxx1z);
19177 for (i = 0; i < ntparms; i++)
19179 tree targ = TREE_VEC_ELT (targs, i);
19180 tree tparm = TREE_VEC_ELT (tparms, i);
19182 /* Clear the "incomplete" flags on all argument packs now so that
19183 substituting them into later default arguments works. */
19184 if (targ && ARGUMENT_PACK_P (targ))
19186 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19187 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19190 if (targ || tparm == error_mark_node)
19191 continue;
19192 tparm = TREE_VALUE (tparm);
19194 if (TREE_CODE (tparm) == TYPE_DECL
19195 && !tried_array_deduction)
19197 try_array_deduction (tparms, targs, xparms);
19198 tried_array_deduction = true;
19199 if (TREE_VEC_ELT (targs, i))
19200 continue;
19203 /* If this is an undeduced nontype parameter that depends on
19204 a type parameter, try another pass; its type may have been
19205 deduced from a later argument than the one from which
19206 this parameter can be deduced. */
19207 if (TREE_CODE (tparm) == PARM_DECL
19208 && uses_template_parms (TREE_TYPE (tparm))
19209 && saw_undeduced < 2)
19211 saw_undeduced = 1;
19212 continue;
19215 /* Core issue #226 (C++0x) [temp.deduct]:
19217 If a template argument has not been deduced, its
19218 default template argument, if any, is used.
19220 When we are in C++98 mode, TREE_PURPOSE will either
19221 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19222 to explicitly check cxx_dialect here. */
19223 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19224 /* OK, there is a default argument. Wait until after the
19225 conversion check to do substitution. */
19226 continue;
19228 /* If the type parameter is a parameter pack, then it will
19229 be deduced to an empty parameter pack. */
19230 if (template_parameter_pack_p (tparm))
19232 tree arg;
19234 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19236 arg = make_node (NONTYPE_ARGUMENT_PACK);
19237 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
19238 TREE_CONSTANT (arg) = 1;
19240 else
19241 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19243 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19245 TREE_VEC_ELT (targs, i) = arg;
19246 continue;
19249 return unify_parameter_deduction_failure (explain_p, tparm);
19252 /* DR 1391: All parameters have args, now check non-dependent parms for
19253 convertibility. */
19254 if (saw_undeduced < 2)
19255 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19256 parms && parms != void_list_node && ia < nargs; )
19258 parm = TREE_VALUE (parms);
19260 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19261 && (!TREE_CHAIN (parms)
19262 || TREE_CHAIN (parms) == void_list_node))
19263 /* For a function parameter pack that occurs at the end of the
19264 parameter-declaration-list, the type A of each remaining
19265 argument of the call is compared with the type P of the
19266 declarator-id of the function parameter pack. */
19267 break;
19269 parms = TREE_CHAIN (parms);
19271 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19272 /* For a function parameter pack that does not occur at the
19273 end of the parameter-declaration-list, the type of the
19274 parameter pack is a non-deduced context. */
19275 continue;
19277 arg = args[ia];
19278 ++ia;
19280 if (uses_template_parms (parm))
19281 continue;
19282 if (check_non_deducible_conversion (parm, arg, strict, flags,
19283 explain_p))
19284 return 1;
19287 /* Now substitute into the default template arguments. */
19288 for (i = 0; i < ntparms; i++)
19290 tree targ = TREE_VEC_ELT (targs, i);
19291 tree tparm = TREE_VEC_ELT (tparms, i);
19293 if (targ || tparm == error_mark_node)
19294 continue;
19295 tree parm = TREE_VALUE (tparm);
19297 if (TREE_CODE (parm) == PARM_DECL
19298 && uses_template_parms (TREE_TYPE (parm))
19299 && saw_undeduced < 2)
19300 continue;
19302 tree arg = TREE_PURPOSE (tparm);
19303 reopen_deferring_access_checks (*checks);
19304 location_t save_loc = input_location;
19305 if (DECL_P (parm))
19306 input_location = DECL_SOURCE_LOCATION (parm);
19307 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19308 if (!uses_template_parms (arg))
19309 arg = convert_template_argument (parm, arg, full_targs, complain,
19310 i, NULL_TREE);
19311 else if (saw_undeduced < 2)
19312 arg = NULL_TREE;
19313 else
19314 arg = error_mark_node;
19315 input_location = save_loc;
19316 *checks = get_deferred_access_checks ();
19317 pop_deferring_access_checks ();
19318 if (arg == error_mark_node)
19319 return 1;
19320 else if (arg)
19322 TREE_VEC_ELT (targs, i) = arg;
19323 /* The position of the first default template argument,
19324 is also the number of non-defaulted arguments in TARGS.
19325 Record that. */
19326 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19327 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19331 if (saw_undeduced++ == 1)
19332 goto again;
19335 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19336 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19338 return unify_success (explain_p);
19341 /* Subroutine of type_unification_real. Args are like the variables
19342 at the call site. ARG is an overloaded function (or template-id);
19343 we try deducing template args from each of the overloads, and if
19344 only one succeeds, we go with that. Modifies TARGS and returns
19345 true on success. */
19347 static bool
19348 resolve_overloaded_unification (tree tparms,
19349 tree targs,
19350 tree parm,
19351 tree arg,
19352 unification_kind_t strict,
19353 int sub_strict,
19354 bool explain_p)
19356 tree tempargs = copy_node (targs);
19357 int good = 0;
19358 tree goodfn = NULL_TREE;
19359 bool addr_p;
19361 if (TREE_CODE (arg) == ADDR_EXPR)
19363 arg = TREE_OPERAND (arg, 0);
19364 addr_p = true;
19366 else
19367 addr_p = false;
19369 if (TREE_CODE (arg) == COMPONENT_REF)
19370 /* Handle `&x' where `x' is some static or non-static member
19371 function name. */
19372 arg = TREE_OPERAND (arg, 1);
19374 if (TREE_CODE (arg) == OFFSET_REF)
19375 arg = TREE_OPERAND (arg, 1);
19377 /* Strip baselink information. */
19378 if (BASELINK_P (arg))
19379 arg = BASELINK_FUNCTIONS (arg);
19381 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19383 /* If we got some explicit template args, we need to plug them into
19384 the affected templates before we try to unify, in case the
19385 explicit args will completely resolve the templates in question. */
19387 int ok = 0;
19388 tree expl_subargs = TREE_OPERAND (arg, 1);
19389 arg = TREE_OPERAND (arg, 0);
19391 for (; arg; arg = OVL_NEXT (arg))
19393 tree fn = OVL_CURRENT (arg);
19394 tree subargs, elem;
19396 if (TREE_CODE (fn) != TEMPLATE_DECL)
19397 continue;
19399 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19400 expl_subargs, NULL_TREE, tf_none,
19401 /*require_all_args=*/true,
19402 /*use_default_args=*/true);
19403 if (subargs != error_mark_node
19404 && !any_dependent_template_arguments_p (subargs))
19406 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19407 if (try_one_overload (tparms, targs, tempargs, parm,
19408 elem, strict, sub_strict, addr_p, explain_p)
19409 && (!goodfn || !same_type_p (goodfn, elem)))
19411 goodfn = elem;
19412 ++good;
19415 else if (subargs)
19416 ++ok;
19418 /* If no templates (or more than one) are fully resolved by the
19419 explicit arguments, this template-id is a non-deduced context; it
19420 could still be OK if we deduce all template arguments for the
19421 enclosing call through other arguments. */
19422 if (good != 1)
19423 good = ok;
19425 else if (TREE_CODE (arg) != OVERLOAD
19426 && TREE_CODE (arg) != FUNCTION_DECL)
19427 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19428 -- but the deduction does not succeed because the expression is
19429 not just the function on its own. */
19430 return false;
19431 else
19432 for (; arg; arg = OVL_NEXT (arg))
19433 if (try_one_overload (tparms, targs, tempargs, parm,
19434 TREE_TYPE (OVL_CURRENT (arg)),
19435 strict, sub_strict, addr_p, explain_p)
19436 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
19438 goodfn = OVL_CURRENT (arg);
19439 ++good;
19442 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19443 to function or pointer to member function argument if the set of
19444 overloaded functions does not contain function templates and at most
19445 one of a set of overloaded functions provides a unique match.
19447 So if we found multiple possibilities, we return success but don't
19448 deduce anything. */
19450 if (good == 1)
19452 int i = TREE_VEC_LENGTH (targs);
19453 for (; i--; )
19454 if (TREE_VEC_ELT (tempargs, i))
19456 tree old = TREE_VEC_ELT (targs, i);
19457 tree new_ = TREE_VEC_ELT (tempargs, i);
19458 if (new_ && old && ARGUMENT_PACK_P (old)
19459 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19460 /* Don't forget explicit template arguments in a pack. */
19461 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19462 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19463 TREE_VEC_ELT (targs, i) = new_;
19466 if (good)
19467 return true;
19469 return false;
19472 /* Core DR 115: In contexts where deduction is done and fails, or in
19473 contexts where deduction is not done, if a template argument list is
19474 specified and it, along with any default template arguments, identifies
19475 a single function template specialization, then the template-id is an
19476 lvalue for the function template specialization. */
19478 tree
19479 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19481 tree expr, offset, baselink;
19482 bool addr;
19484 if (!type_unknown_p (orig_expr))
19485 return orig_expr;
19487 expr = orig_expr;
19488 addr = false;
19489 offset = NULL_TREE;
19490 baselink = NULL_TREE;
19492 if (TREE_CODE (expr) == ADDR_EXPR)
19494 expr = TREE_OPERAND (expr, 0);
19495 addr = true;
19497 if (TREE_CODE (expr) == OFFSET_REF)
19499 offset = expr;
19500 expr = TREE_OPERAND (expr, 1);
19502 if (BASELINK_P (expr))
19504 baselink = expr;
19505 expr = BASELINK_FUNCTIONS (expr);
19508 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19510 int good = 0;
19511 tree goodfn = NULL_TREE;
19513 /* If we got some explicit template args, we need to plug them into
19514 the affected templates before we try to unify, in case the
19515 explicit args will completely resolve the templates in question. */
19517 tree expl_subargs = TREE_OPERAND (expr, 1);
19518 tree arg = TREE_OPERAND (expr, 0);
19519 tree badfn = NULL_TREE;
19520 tree badargs = NULL_TREE;
19522 for (; arg; arg = OVL_NEXT (arg))
19524 tree fn = OVL_CURRENT (arg);
19525 tree subargs, elem;
19527 if (TREE_CODE (fn) != TEMPLATE_DECL)
19528 continue;
19530 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19531 expl_subargs, NULL_TREE, tf_none,
19532 /*require_all_args=*/true,
19533 /*use_default_args=*/true);
19534 if (subargs != error_mark_node
19535 && !any_dependent_template_arguments_p (subargs))
19537 elem = instantiate_template (fn, subargs, tf_none);
19538 if (elem == error_mark_node)
19540 badfn = fn;
19541 badargs = subargs;
19543 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19545 goodfn = elem;
19546 ++good;
19550 if (good == 1)
19552 mark_used (goodfn);
19553 expr = goodfn;
19554 if (baselink)
19555 expr = build_baselink (BASELINK_BINFO (baselink),
19556 BASELINK_ACCESS_BINFO (baselink),
19557 expr, BASELINK_OPTYPE (baselink));
19558 if (offset)
19560 tree base
19561 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19562 expr = build_offset_ref (base, expr, addr, complain);
19564 if (addr)
19565 expr = cp_build_addr_expr (expr, complain);
19566 return expr;
19568 else if (good == 0 && badargs && (complain & tf_error))
19569 /* There were no good options and at least one bad one, so let the
19570 user know what the problem is. */
19571 instantiate_template (badfn, badargs, complain);
19573 return orig_expr;
19576 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19577 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19578 different overloads deduce different arguments for a given parm.
19579 ADDR_P is true if the expression for which deduction is being
19580 performed was of the form "& fn" rather than simply "fn".
19582 Returns 1 on success. */
19584 static int
19585 try_one_overload (tree tparms,
19586 tree orig_targs,
19587 tree targs,
19588 tree parm,
19589 tree arg,
19590 unification_kind_t strict,
19591 int sub_strict,
19592 bool addr_p,
19593 bool explain_p)
19595 int nargs;
19596 tree tempargs;
19597 int i;
19599 if (arg == error_mark_node)
19600 return 0;
19602 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19603 to function or pointer to member function argument if the set of
19604 overloaded functions does not contain function templates and at most
19605 one of a set of overloaded functions provides a unique match.
19607 So if this is a template, just return success. */
19609 if (uses_template_parms (arg))
19610 return 1;
19612 if (TREE_CODE (arg) == METHOD_TYPE)
19613 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19614 else if (addr_p)
19615 arg = build_pointer_type (arg);
19617 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19619 /* We don't copy orig_targs for this because if we have already deduced
19620 some template args from previous args, unify would complain when we
19621 try to deduce a template parameter for the same argument, even though
19622 there isn't really a conflict. */
19623 nargs = TREE_VEC_LENGTH (targs);
19624 tempargs = make_tree_vec (nargs);
19626 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19627 return 0;
19629 /* First make sure we didn't deduce anything that conflicts with
19630 explicitly specified args. */
19631 for (i = nargs; i--; )
19633 tree elt = TREE_VEC_ELT (tempargs, i);
19634 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19636 if (!elt)
19637 /*NOP*/;
19638 else if (uses_template_parms (elt))
19639 /* Since we're unifying against ourselves, we will fill in
19640 template args used in the function parm list with our own
19641 template parms. Discard them. */
19642 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19643 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19645 /* Check that the argument at each index of the deduced argument pack
19646 is equivalent to the corresponding explicitly specified argument.
19647 We may have deduced more arguments than were explicitly specified,
19648 and that's OK. */
19649 gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt));
19650 gcc_assert (ARGUMENT_PACK_ARGS (oldelt)
19651 == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt));
19653 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19654 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19656 if (TREE_VEC_LENGTH (deduced_pack)
19657 < TREE_VEC_LENGTH (explicit_pack))
19658 return 0;
19660 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19661 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19662 TREE_VEC_ELT (deduced_pack, j)))
19663 return 0;
19665 else if (oldelt && !template_args_equal (oldelt, elt))
19666 return 0;
19669 for (i = nargs; i--; )
19671 tree elt = TREE_VEC_ELT (tempargs, i);
19673 if (elt)
19674 TREE_VEC_ELT (targs, i) = elt;
19677 return 1;
19680 /* PARM is a template class (perhaps with unbound template
19681 parameters). ARG is a fully instantiated type. If ARG can be
19682 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19683 TARGS are as for unify. */
19685 static tree
19686 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19687 bool explain_p)
19689 tree copy_of_targs;
19691 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19692 return NULL_TREE;
19693 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19694 /* Matches anything. */;
19695 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19696 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19697 return NULL_TREE;
19699 /* We need to make a new template argument vector for the call to
19700 unify. If we used TARGS, we'd clutter it up with the result of
19701 the attempted unification, even if this class didn't work out.
19702 We also don't want to commit ourselves to all the unifications
19703 we've already done, since unification is supposed to be done on
19704 an argument-by-argument basis. In other words, consider the
19705 following pathological case:
19707 template <int I, int J, int K>
19708 struct S {};
19710 template <int I, int J>
19711 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19713 template <int I, int J, int K>
19714 void f(S<I, J, K>, S<I, I, I>);
19716 void g() {
19717 S<0, 0, 0> s0;
19718 S<0, 1, 2> s2;
19720 f(s0, s2);
19723 Now, by the time we consider the unification involving `s2', we
19724 already know that we must have `f<0, 0, 0>'. But, even though
19725 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19726 because there are two ways to unify base classes of S<0, 1, 2>
19727 with S<I, I, I>. If we kept the already deduced knowledge, we
19728 would reject the possibility I=1. */
19729 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19731 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19733 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19734 return NULL_TREE;
19735 return arg;
19738 /* If unification failed, we're done. */
19739 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19740 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19741 return NULL_TREE;
19743 return arg;
19746 /* Given a template type PARM and a class type ARG, find the unique
19747 base type in ARG that is an instance of PARM. We do not examine
19748 ARG itself; only its base-classes. If there is not exactly one
19749 appropriate base class, return NULL_TREE. PARM may be the type of
19750 a partial specialization, as well as a plain template type. Used
19751 by unify. */
19753 static enum template_base_result
19754 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19755 bool explain_p, tree *result)
19757 tree rval = NULL_TREE;
19758 tree binfo;
19760 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19762 binfo = TYPE_BINFO (complete_type (arg));
19763 if (!binfo)
19765 /* The type could not be completed. */
19766 *result = NULL_TREE;
19767 return tbr_incomplete_type;
19770 /* Walk in inheritance graph order. The search order is not
19771 important, and this avoids multiple walks of virtual bases. */
19772 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19774 tree r = try_class_unification (tparms, targs, parm,
19775 BINFO_TYPE (binfo), explain_p);
19777 if (r)
19779 /* If there is more than one satisfactory baseclass, then:
19781 [temp.deduct.call]
19783 If they yield more than one possible deduced A, the type
19784 deduction fails.
19786 applies. */
19787 if (rval && !same_type_p (r, rval))
19789 *result = NULL_TREE;
19790 return tbr_ambiguous_baseclass;
19793 rval = r;
19797 *result = rval;
19798 return tbr_success;
19801 /* Returns the level of DECL, which declares a template parameter. */
19803 static int
19804 template_decl_level (tree decl)
19806 switch (TREE_CODE (decl))
19808 case TYPE_DECL:
19809 case TEMPLATE_DECL:
19810 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19812 case PARM_DECL:
19813 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19815 default:
19816 gcc_unreachable ();
19818 return 0;
19821 /* Decide whether ARG can be unified with PARM, considering only the
19822 cv-qualifiers of each type, given STRICT as documented for unify.
19823 Returns nonzero iff the unification is OK on that basis. */
19825 static int
19826 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19828 int arg_quals = cp_type_quals (arg);
19829 int parm_quals = cp_type_quals (parm);
19831 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19832 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19834 /* Although a CVR qualifier is ignored when being applied to a
19835 substituted template parameter ([8.3.2]/1 for example), that
19836 does not allow us to unify "const T" with "int&" because both
19837 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19838 It is ok when we're allowing additional CV qualifiers
19839 at the outer level [14.8.2.1]/3,1st bullet. */
19840 if ((TREE_CODE (arg) == REFERENCE_TYPE
19841 || TREE_CODE (arg) == FUNCTION_TYPE
19842 || TREE_CODE (arg) == METHOD_TYPE)
19843 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19844 return 0;
19846 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19847 && (parm_quals & TYPE_QUAL_RESTRICT))
19848 return 0;
19851 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19852 && (arg_quals & parm_quals) != parm_quals)
19853 return 0;
19855 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19856 && (parm_quals & arg_quals) != arg_quals)
19857 return 0;
19859 return 1;
19862 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19863 void
19864 template_parm_level_and_index (tree parm, int* level, int* index)
19866 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19867 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19868 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19870 *index = TEMPLATE_TYPE_IDX (parm);
19871 *level = TEMPLATE_TYPE_LEVEL (parm);
19873 else
19875 *index = TEMPLATE_PARM_IDX (parm);
19876 *level = TEMPLATE_PARM_LEVEL (parm);
19880 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19881 do { \
19882 if (unify (TP, TA, P, A, S, EP)) \
19883 return 1; \
19884 } while (0)
19886 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19887 expansion at the end of PACKED_PARMS. Returns 0 if the type
19888 deduction succeeds, 1 otherwise. STRICT is the same as in
19889 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19890 function call argument list. We'll need to adjust the arguments to make them
19891 types. SUBR tells us if this is from a recursive call to
19892 type_unification_real, or for comparing two template argument
19893 lists. */
19895 static int
19896 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19897 tree packed_args, unification_kind_t strict,
19898 bool subr, bool explain_p)
19900 tree parm
19901 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19902 tree pattern = PACK_EXPANSION_PATTERN (parm);
19903 tree pack, packs = NULL_TREE;
19904 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19906 packed_args = expand_template_argument_pack (packed_args);
19908 int len = TREE_VEC_LENGTH (packed_args);
19910 /* Determine the parameter packs we will be deducing from the
19911 pattern, and record their current deductions. */
19912 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19913 pack; pack = TREE_CHAIN (pack))
19915 tree parm_pack = TREE_VALUE (pack);
19916 int idx, level;
19918 /* Determine the index and level of this parameter pack. */
19919 template_parm_level_and_index (parm_pack, &level, &idx);
19921 /* Keep track of the parameter packs and their corresponding
19922 argument packs. */
19923 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19924 TREE_TYPE (packs) = make_tree_vec (len - start);
19927 /* Loop through all of the arguments that have not yet been
19928 unified and unify each with the pattern. */
19929 for (i = start; i < len; i++)
19931 tree parm;
19932 bool any_explicit = false;
19933 tree arg = TREE_VEC_ELT (packed_args, i);
19935 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19936 or the element of its argument pack at the current index if
19937 this argument was explicitly specified. */
19938 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19940 int idx, level;
19941 tree arg, pargs;
19942 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19944 arg = NULL_TREE;
19945 if (TREE_VALUE (pack)
19946 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19947 && (i - start < TREE_VEC_LENGTH (pargs)))
19949 any_explicit = true;
19950 arg = TREE_VEC_ELT (pargs, i - start);
19952 TMPL_ARG (targs, level, idx) = arg;
19955 /* If we had explicit template arguments, substitute them into the
19956 pattern before deduction. */
19957 if (any_explicit)
19959 /* Some arguments might still be unspecified or dependent. */
19960 bool dependent;
19961 ++processing_template_decl;
19962 dependent = any_dependent_template_arguments_p (targs);
19963 if (!dependent)
19964 --processing_template_decl;
19965 parm = tsubst (pattern, targs,
19966 explain_p ? tf_warning_or_error : tf_none,
19967 NULL_TREE);
19968 if (dependent)
19969 --processing_template_decl;
19970 if (parm == error_mark_node)
19971 return 1;
19973 else
19974 parm = pattern;
19976 /* Unify the pattern with the current argument. */
19977 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
19978 explain_p))
19979 return 1;
19981 /* For each parameter pack, collect the deduced value. */
19982 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19984 int idx, level;
19985 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19987 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
19988 TMPL_ARG (targs, level, idx);
19992 /* Verify that the results of unification with the parameter packs
19993 produce results consistent with what we've seen before, and make
19994 the deduced argument packs available. */
19995 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19997 tree old_pack = TREE_VALUE (pack);
19998 tree new_args = TREE_TYPE (pack);
19999 int i, len = TREE_VEC_LENGTH (new_args);
20000 int idx, level;
20001 bool nondeduced_p = false;
20003 /* By default keep the original deduced argument pack.
20004 If necessary, more specific code is going to update the
20005 resulting deduced argument later down in this function. */
20006 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20007 TMPL_ARG (targs, level, idx) = old_pack;
20009 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20010 actually deduce anything. */
20011 for (i = 0; i < len && !nondeduced_p; ++i)
20012 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20013 nondeduced_p = true;
20014 if (nondeduced_p)
20015 continue;
20017 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20019 /* If we had fewer function args than explicit template args,
20020 just use the explicits. */
20021 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20022 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20023 if (len < explicit_len)
20024 new_args = explicit_args;
20027 if (!old_pack)
20029 tree result;
20030 /* Build the deduced *_ARGUMENT_PACK. */
20031 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20033 result = make_node (NONTYPE_ARGUMENT_PACK);
20034 TREE_TYPE (result) =
20035 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
20036 TREE_CONSTANT (result) = 1;
20038 else
20039 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20041 SET_ARGUMENT_PACK_ARGS (result, new_args);
20043 /* Note the deduced argument packs for this parameter
20044 pack. */
20045 TMPL_ARG (targs, level, idx) = result;
20047 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20048 && (ARGUMENT_PACK_ARGS (old_pack)
20049 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20051 /* We only had the explicitly-provided arguments before, but
20052 now we have a complete set of arguments. */
20053 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20055 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20056 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20057 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20059 else
20061 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20062 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20064 if (!comp_template_args (old_args, new_args,
20065 &bad_old_arg, &bad_new_arg))
20066 /* Inconsistent unification of this parameter pack. */
20067 return unify_parameter_pack_inconsistent (explain_p,
20068 bad_old_arg,
20069 bad_new_arg);
20073 return unify_success (explain_p);
20076 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20077 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20078 parameters and return value are as for unify. */
20080 static int
20081 unify_array_domain (tree tparms, tree targs,
20082 tree parm_dom, tree arg_dom,
20083 bool explain_p)
20085 tree parm_max;
20086 tree arg_max;
20087 bool parm_cst;
20088 bool arg_cst;
20090 /* Our representation of array types uses "N - 1" as the
20091 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20092 not an integer constant. We cannot unify arbitrarily
20093 complex expressions, so we eliminate the MINUS_EXPRs
20094 here. */
20095 parm_max = TYPE_MAX_VALUE (parm_dom);
20096 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20097 if (!parm_cst)
20099 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20100 parm_max = TREE_OPERAND (parm_max, 0);
20102 arg_max = TYPE_MAX_VALUE (arg_dom);
20103 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20104 if (!arg_cst)
20106 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20107 trying to unify the type of a variable with the type
20108 of a template parameter. For example:
20110 template <unsigned int N>
20111 void f (char (&) [N]);
20112 int g();
20113 void h(int i) {
20114 char a[g(i)];
20115 f(a);
20118 Here, the type of the ARG will be "int [g(i)]", and
20119 may be a SAVE_EXPR, etc. */
20120 if (TREE_CODE (arg_max) != MINUS_EXPR)
20121 return unify_vla_arg (explain_p, arg_dom);
20122 arg_max = TREE_OPERAND (arg_max, 0);
20125 /* If only one of the bounds used a MINUS_EXPR, compensate
20126 by adding one to the other bound. */
20127 if (parm_cst && !arg_cst)
20128 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20129 integer_type_node,
20130 parm_max,
20131 integer_one_node);
20132 else if (arg_cst && !parm_cst)
20133 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20134 integer_type_node,
20135 arg_max,
20136 integer_one_node);
20138 return unify (tparms, targs, parm_max, arg_max,
20139 UNIFY_ALLOW_INTEGER, explain_p);
20142 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20144 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20146 static pa_kind_t
20147 pa_kind (tree t)
20149 if (PACK_EXPANSION_P (t))
20150 t = PACK_EXPANSION_PATTERN (t);
20151 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20152 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20153 || DECL_TYPE_TEMPLATE_P (t))
20154 return pa_tmpl;
20155 else if (TYPE_P (t))
20156 return pa_type;
20157 else
20158 return pa_expr;
20161 /* Deduce the value of template parameters. TPARMS is the (innermost)
20162 set of template parameters to a template. TARGS is the bindings
20163 for those template parameters, as determined thus far; TARGS may
20164 include template arguments for outer levels of template parameters
20165 as well. PARM is a parameter to a template function, or a
20166 subcomponent of that parameter; ARG is the corresponding argument.
20167 This function attempts to match PARM with ARG in a manner
20168 consistent with the existing assignments in TARGS. If more values
20169 are deduced, then TARGS is updated.
20171 Returns 0 if the type deduction succeeds, 1 otherwise. The
20172 parameter STRICT is a bitwise or of the following flags:
20174 UNIFY_ALLOW_NONE:
20175 Require an exact match between PARM and ARG.
20176 UNIFY_ALLOW_MORE_CV_QUAL:
20177 Allow the deduced ARG to be more cv-qualified (by qualification
20178 conversion) than ARG.
20179 UNIFY_ALLOW_LESS_CV_QUAL:
20180 Allow the deduced ARG to be less cv-qualified than ARG.
20181 UNIFY_ALLOW_DERIVED:
20182 Allow the deduced ARG to be a template base class of ARG,
20183 or a pointer to a template base class of the type pointed to by
20184 ARG.
20185 UNIFY_ALLOW_INTEGER:
20186 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20187 case for more information.
20188 UNIFY_ALLOW_OUTER_LEVEL:
20189 This is the outermost level of a deduction. Used to determine validity
20190 of qualification conversions. A valid qualification conversion must
20191 have const qualified pointers leading up to the inner type which
20192 requires additional CV quals, except at the outer level, where const
20193 is not required [conv.qual]. It would be normal to set this flag in
20194 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20195 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20196 This is the outermost level of a deduction, and PARM can be more CV
20197 qualified at this point.
20198 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20199 This is the outermost level of a deduction, and PARM can be less CV
20200 qualified at this point. */
20202 static int
20203 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20204 bool explain_p)
20206 int idx;
20207 tree targ;
20208 tree tparm;
20209 int strict_in = strict;
20210 tsubst_flags_t complain = (explain_p
20211 ? tf_warning_or_error
20212 : tf_none);
20214 /* I don't think this will do the right thing with respect to types.
20215 But the only case I've seen it in so far has been array bounds, where
20216 signedness is the only information lost, and I think that will be
20217 okay. */
20218 while (CONVERT_EXPR_P (parm))
20219 parm = TREE_OPERAND (parm, 0);
20221 if (arg == error_mark_node)
20222 return unify_invalid (explain_p);
20223 if (arg == unknown_type_node
20224 || arg == init_list_type_node)
20225 /* We can't deduce anything from this, but we might get all the
20226 template args from other function args. */
20227 return unify_success (explain_p);
20229 if (parm == any_targ_node || arg == any_targ_node)
20230 return unify_success (explain_p);
20232 /* If PARM uses template parameters, then we can't bail out here,
20233 even if ARG == PARM, since we won't record unifications for the
20234 template parameters. We might need them if we're trying to
20235 figure out which of two things is more specialized. */
20236 if (arg == parm && !uses_template_parms (parm))
20237 return unify_success (explain_p);
20239 /* Handle init lists early, so the rest of the function can assume
20240 we're dealing with a type. */
20241 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20243 tree elt, elttype;
20244 unsigned i;
20245 tree orig_parm = parm;
20247 /* Replace T with std::initializer_list<T> for deduction. */
20248 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20249 && flag_deduce_init_list)
20250 parm = listify (parm);
20252 if (!is_std_init_list (parm)
20253 && TREE_CODE (parm) != ARRAY_TYPE)
20254 /* We can only deduce from an initializer list argument if the
20255 parameter is std::initializer_list or an array; otherwise this
20256 is a non-deduced context. */
20257 return unify_success (explain_p);
20259 if (TREE_CODE (parm) == ARRAY_TYPE)
20260 elttype = TREE_TYPE (parm);
20261 else
20263 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20264 /* Deduction is defined in terms of a single type, so just punt
20265 on the (bizarre) std::initializer_list<T...>. */
20266 if (PACK_EXPANSION_P (elttype))
20267 return unify_success (explain_p);
20270 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20272 int elt_strict = strict;
20274 if (elt == error_mark_node)
20275 return unify_invalid (explain_p);
20277 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20279 tree type = TREE_TYPE (elt);
20280 if (type == error_mark_node)
20281 return unify_invalid (explain_p);
20282 /* It should only be possible to get here for a call. */
20283 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20284 elt_strict |= maybe_adjust_types_for_deduction
20285 (DEDUCE_CALL, &elttype, &type, elt);
20286 elt = type;
20289 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20290 explain_p);
20293 if (TREE_CODE (parm) == ARRAY_TYPE
20294 && deducible_array_bound (TYPE_DOMAIN (parm)))
20296 /* Also deduce from the length of the initializer list. */
20297 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20298 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20299 if (idx == error_mark_node)
20300 return unify_invalid (explain_p);
20301 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20302 idx, explain_p);
20305 /* If the std::initializer_list<T> deduction worked, replace the
20306 deduced A with std::initializer_list<A>. */
20307 if (orig_parm != parm)
20309 idx = TEMPLATE_TYPE_IDX (orig_parm);
20310 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20311 targ = listify (targ);
20312 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20314 return unify_success (explain_p);
20317 /* If parm and arg aren't the same kind of thing (template, type, or
20318 expression), fail early. */
20319 if (pa_kind (parm) != pa_kind (arg))
20320 return unify_invalid (explain_p);
20322 /* Immediately reject some pairs that won't unify because of
20323 cv-qualification mismatches. */
20324 if (TREE_CODE (arg) == TREE_CODE (parm)
20325 && TYPE_P (arg)
20326 /* It is the elements of the array which hold the cv quals of an array
20327 type, and the elements might be template type parms. We'll check
20328 when we recurse. */
20329 && TREE_CODE (arg) != ARRAY_TYPE
20330 /* We check the cv-qualifiers when unifying with template type
20331 parameters below. We want to allow ARG `const T' to unify with
20332 PARM `T' for example, when computing which of two templates
20333 is more specialized, for example. */
20334 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20335 && !check_cv_quals_for_unify (strict_in, arg, parm))
20336 return unify_cv_qual_mismatch (explain_p, parm, arg);
20338 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20339 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20340 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20341 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20342 strict &= ~UNIFY_ALLOW_DERIVED;
20343 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20344 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20346 switch (TREE_CODE (parm))
20348 case TYPENAME_TYPE:
20349 case SCOPE_REF:
20350 case UNBOUND_CLASS_TEMPLATE:
20351 /* In a type which contains a nested-name-specifier, template
20352 argument values cannot be deduced for template parameters used
20353 within the nested-name-specifier. */
20354 return unify_success (explain_p);
20356 case TEMPLATE_TYPE_PARM:
20357 case TEMPLATE_TEMPLATE_PARM:
20358 case BOUND_TEMPLATE_TEMPLATE_PARM:
20359 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20360 if (error_operand_p (tparm))
20361 return unify_invalid (explain_p);
20363 if (TEMPLATE_TYPE_LEVEL (parm)
20364 != template_decl_level (tparm))
20365 /* The PARM is not one we're trying to unify. Just check
20366 to see if it matches ARG. */
20368 if (TREE_CODE (arg) == TREE_CODE (parm)
20369 && (is_auto (parm) ? is_auto (arg)
20370 : same_type_p (parm, arg)))
20371 return unify_success (explain_p);
20372 else
20373 return unify_type_mismatch (explain_p, parm, arg);
20375 idx = TEMPLATE_TYPE_IDX (parm);
20376 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20377 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20378 if (error_operand_p (tparm))
20379 return unify_invalid (explain_p);
20381 /* Check for mixed types and values. */
20382 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20383 && TREE_CODE (tparm) != TYPE_DECL)
20384 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20385 && TREE_CODE (tparm) != TEMPLATE_DECL))
20386 gcc_unreachable ();
20388 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20390 if ((strict_in & UNIFY_ALLOW_DERIVED)
20391 && CLASS_TYPE_P (arg))
20393 /* First try to match ARG directly. */
20394 tree t = try_class_unification (tparms, targs, parm, arg,
20395 explain_p);
20396 if (!t)
20398 /* Otherwise, look for a suitable base of ARG, as below. */
20399 enum template_base_result r;
20400 r = get_template_base (tparms, targs, parm, arg,
20401 explain_p, &t);
20402 if (!t)
20403 return unify_no_common_base (explain_p, r, parm, arg);
20404 arg = t;
20407 /* ARG must be constructed from a template class or a template
20408 template parameter. */
20409 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20410 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20411 return unify_template_deduction_failure (explain_p, parm, arg);
20413 /* Deduce arguments T, i from TT<T> or TT<i>. */
20414 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20415 return 1;
20417 arg = TYPE_TI_TEMPLATE (arg);
20419 /* Fall through to deduce template name. */
20422 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20423 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20425 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20427 /* Simple cases: Value already set, does match or doesn't. */
20428 if (targ != NULL_TREE && template_args_equal (targ, arg))
20429 return unify_success (explain_p);
20430 else if (targ)
20431 return unify_inconsistency (explain_p, parm, targ, arg);
20433 else
20435 /* If PARM is `const T' and ARG is only `int', we don't have
20436 a match unless we are allowing additional qualification.
20437 If ARG is `const int' and PARM is just `T' that's OK;
20438 that binds `const int' to `T'. */
20439 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20440 arg, parm))
20441 return unify_cv_qual_mismatch (explain_p, parm, arg);
20443 /* Consider the case where ARG is `const volatile int' and
20444 PARM is `const T'. Then, T should be `volatile int'. */
20445 arg = cp_build_qualified_type_real
20446 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20447 if (arg == error_mark_node)
20448 return unify_invalid (explain_p);
20450 /* Simple cases: Value already set, does match or doesn't. */
20451 if (targ != NULL_TREE && same_type_p (targ, arg))
20452 return unify_success (explain_p);
20453 else if (targ)
20454 return unify_inconsistency (explain_p, parm, targ, arg);
20456 /* Make sure that ARG is not a variable-sized array. (Note
20457 that were talking about variable-sized arrays (like
20458 `int[n]'), rather than arrays of unknown size (like
20459 `int[]').) We'll get very confused by such a type since
20460 the bound of the array is not constant, and therefore
20461 not mangleable. Besides, such types are not allowed in
20462 ISO C++, so we can do as we please here. We do allow
20463 them for 'auto' deduction, since that isn't ABI-exposed. */
20464 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20465 return unify_vla_arg (explain_p, arg);
20467 /* Strip typedefs as in convert_template_argument. */
20468 arg = canonicalize_type_argument (arg, tf_none);
20471 /* If ARG is a parameter pack or an expansion, we cannot unify
20472 against it unless PARM is also a parameter pack. */
20473 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20474 && !template_parameter_pack_p (parm))
20475 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20477 /* If the argument deduction results is a METHOD_TYPE,
20478 then there is a problem.
20479 METHOD_TYPE doesn't map to any real C++ type the result of
20480 the deduction can not be of that type. */
20481 if (TREE_CODE (arg) == METHOD_TYPE)
20482 return unify_method_type_error (explain_p, arg);
20484 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20485 return unify_success (explain_p);
20487 case TEMPLATE_PARM_INDEX:
20488 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20489 if (error_operand_p (tparm))
20490 return unify_invalid (explain_p);
20492 if (TEMPLATE_PARM_LEVEL (parm)
20493 != template_decl_level (tparm))
20495 /* The PARM is not one we're trying to unify. Just check
20496 to see if it matches ARG. */
20497 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20498 && cp_tree_equal (parm, arg));
20499 if (result)
20500 unify_expression_unequal (explain_p, parm, arg);
20501 return result;
20504 idx = TEMPLATE_PARM_IDX (parm);
20505 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20507 if (targ)
20509 int x = !cp_tree_equal (targ, arg);
20510 if (x)
20511 unify_inconsistency (explain_p, parm, targ, arg);
20512 return x;
20515 if (cxx_dialect >= cxx1z
20516 /* We deduce from array bounds in try_array_deduction. */
20517 && !(strict & UNIFY_ALLOW_INTEGER)
20518 && uses_template_parms (TREE_TYPE (parm))
20519 && !type_uses_auto (TREE_TYPE (parm)))
20521 tree atype = TREE_TYPE (arg);
20522 RECUR_AND_CHECK_FAILURE (tparms, targs,
20523 TREE_TYPE (parm), atype,
20524 UNIFY_ALLOW_NONE, explain_p);
20527 /* [temp.deduct.type] If, in the declaration of a function template
20528 with a non-type template-parameter, the non-type
20529 template-parameter is used in an expression in the function
20530 parameter-list and, if the corresponding template-argument is
20531 deduced, the template-argument type shall match the type of the
20532 template-parameter exactly, except that a template-argument
20533 deduced from an array bound may be of any integral type.
20534 The non-type parameter might use already deduced type parameters. */
20535 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20536 if (tree a = type_uses_auto (tparm))
20538 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20539 if (tparm == error_mark_node)
20540 return 1;
20543 if (!TREE_TYPE (arg))
20544 /* Template-parameter dependent expression. Just accept it for now.
20545 It will later be processed in convert_template_argument. */
20547 else if (same_type_p (TREE_TYPE (arg), tparm))
20548 /* OK */;
20549 else if ((strict & UNIFY_ALLOW_INTEGER)
20550 && CP_INTEGRAL_TYPE_P (tparm))
20551 /* Convert the ARG to the type of PARM; the deduced non-type
20552 template argument must exactly match the types of the
20553 corresponding parameter. */
20554 arg = fold (build_nop (tparm, arg));
20555 else if (uses_template_parms (tparm))
20556 /* We haven't deduced the type of this parameter yet. Try again
20557 later. */
20558 return unify_success (explain_p);
20559 else
20560 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20562 /* If ARG is a parameter pack or an expansion, we cannot unify
20563 against it unless PARM is also a parameter pack. */
20564 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20565 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20566 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20569 bool removed_attr = false;
20570 arg = strip_typedefs_expr (arg, &removed_attr);
20572 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20573 return unify_success (explain_p);
20575 case PTRMEM_CST:
20577 /* A pointer-to-member constant can be unified only with
20578 another constant. */
20579 if (TREE_CODE (arg) != PTRMEM_CST)
20580 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20582 /* Just unify the class member. It would be useless (and possibly
20583 wrong, depending on the strict flags) to unify also
20584 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20585 arg refer to the same variable, even if through different
20586 classes. For instance:
20588 struct A { int x; };
20589 struct B : A { };
20591 Unification of &A::x and &B::x must succeed. */
20592 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20593 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20596 case POINTER_TYPE:
20598 if (!TYPE_PTR_P (arg))
20599 return unify_type_mismatch (explain_p, parm, arg);
20601 /* [temp.deduct.call]
20603 A can be another pointer or pointer to member type that can
20604 be converted to the deduced A via a qualification
20605 conversion (_conv.qual_).
20607 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20608 This will allow for additional cv-qualification of the
20609 pointed-to types if appropriate. */
20611 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20612 /* The derived-to-base conversion only persists through one
20613 level of pointers. */
20614 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20616 return unify (tparms, targs, TREE_TYPE (parm),
20617 TREE_TYPE (arg), strict, explain_p);
20620 case REFERENCE_TYPE:
20621 if (TREE_CODE (arg) != REFERENCE_TYPE)
20622 return unify_type_mismatch (explain_p, parm, arg);
20623 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20624 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20626 case ARRAY_TYPE:
20627 if (TREE_CODE (arg) != ARRAY_TYPE)
20628 return unify_type_mismatch (explain_p, parm, arg);
20629 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20630 != (TYPE_DOMAIN (arg) == NULL_TREE))
20631 return unify_type_mismatch (explain_p, parm, arg);
20632 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20633 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20634 if (TYPE_DOMAIN (parm) != NULL_TREE)
20635 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20636 TYPE_DOMAIN (arg), explain_p);
20637 return unify_success (explain_p);
20639 case REAL_TYPE:
20640 case COMPLEX_TYPE:
20641 case VECTOR_TYPE:
20642 case INTEGER_TYPE:
20643 case BOOLEAN_TYPE:
20644 case ENUMERAL_TYPE:
20645 case VOID_TYPE:
20646 case NULLPTR_TYPE:
20647 if (TREE_CODE (arg) != TREE_CODE (parm))
20648 return unify_type_mismatch (explain_p, parm, arg);
20650 /* We have already checked cv-qualification at the top of the
20651 function. */
20652 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20653 return unify_type_mismatch (explain_p, parm, arg);
20655 /* As far as unification is concerned, this wins. Later checks
20656 will invalidate it if necessary. */
20657 return unify_success (explain_p);
20659 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20660 /* Type INTEGER_CST can come from ordinary constant template args. */
20661 case INTEGER_CST:
20662 while (CONVERT_EXPR_P (arg))
20663 arg = TREE_OPERAND (arg, 0);
20665 if (TREE_CODE (arg) != INTEGER_CST)
20666 return unify_template_argument_mismatch (explain_p, parm, arg);
20667 return (tree_int_cst_equal (parm, arg)
20668 ? unify_success (explain_p)
20669 : unify_template_argument_mismatch (explain_p, parm, arg));
20671 case TREE_VEC:
20673 int i, len, argslen;
20674 int parm_variadic_p = 0;
20676 if (TREE_CODE (arg) != TREE_VEC)
20677 return unify_template_argument_mismatch (explain_p, parm, arg);
20679 len = TREE_VEC_LENGTH (parm);
20680 argslen = TREE_VEC_LENGTH (arg);
20682 /* Check for pack expansions in the parameters. */
20683 for (i = 0; i < len; ++i)
20685 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20687 if (i == len - 1)
20688 /* We can unify against something with a trailing
20689 parameter pack. */
20690 parm_variadic_p = 1;
20691 else
20692 /* [temp.deduct.type]/9: If the template argument list of
20693 P contains a pack expansion that is not the last
20694 template argument, the entire template argument list
20695 is a non-deduced context. */
20696 return unify_success (explain_p);
20700 /* If we don't have enough arguments to satisfy the parameters
20701 (not counting the pack expression at the end), or we have
20702 too many arguments for a parameter list that doesn't end in
20703 a pack expression, we can't unify. */
20704 if (parm_variadic_p
20705 ? argslen < len - parm_variadic_p
20706 : argslen != len)
20707 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20709 /* Unify all of the parameters that precede the (optional)
20710 pack expression. */
20711 for (i = 0; i < len - parm_variadic_p; ++i)
20713 RECUR_AND_CHECK_FAILURE (tparms, targs,
20714 TREE_VEC_ELT (parm, i),
20715 TREE_VEC_ELT (arg, i),
20716 UNIFY_ALLOW_NONE, explain_p);
20718 if (parm_variadic_p)
20719 return unify_pack_expansion (tparms, targs, parm, arg,
20720 DEDUCE_EXACT,
20721 /*subr=*/true, explain_p);
20722 return unify_success (explain_p);
20725 case RECORD_TYPE:
20726 case UNION_TYPE:
20727 if (TREE_CODE (arg) != TREE_CODE (parm))
20728 return unify_type_mismatch (explain_p, parm, arg);
20730 if (TYPE_PTRMEMFUNC_P (parm))
20732 if (!TYPE_PTRMEMFUNC_P (arg))
20733 return unify_type_mismatch (explain_p, parm, arg);
20735 return unify (tparms, targs,
20736 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20737 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20738 strict, explain_p);
20740 else if (TYPE_PTRMEMFUNC_P (arg))
20741 return unify_type_mismatch (explain_p, parm, arg);
20743 if (CLASSTYPE_TEMPLATE_INFO (parm))
20745 tree t = NULL_TREE;
20747 if (strict_in & UNIFY_ALLOW_DERIVED)
20749 /* First, we try to unify the PARM and ARG directly. */
20750 t = try_class_unification (tparms, targs,
20751 parm, arg, explain_p);
20753 if (!t)
20755 /* Fallback to the special case allowed in
20756 [temp.deduct.call]:
20758 If P is a class, and P has the form
20759 template-id, then A can be a derived class of
20760 the deduced A. Likewise, if P is a pointer to
20761 a class of the form template-id, A can be a
20762 pointer to a derived class pointed to by the
20763 deduced A. */
20764 enum template_base_result r;
20765 r = get_template_base (tparms, targs, parm, arg,
20766 explain_p, &t);
20768 if (!t)
20770 /* Don't give the derived diagnostic if we're
20771 already dealing with the same template. */
20772 bool same_template
20773 = (CLASSTYPE_TEMPLATE_INFO (arg)
20774 && (CLASSTYPE_TI_TEMPLATE (parm)
20775 == CLASSTYPE_TI_TEMPLATE (arg)));
20776 return unify_no_common_base (explain_p && !same_template,
20777 r, parm, arg);
20781 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20782 && (CLASSTYPE_TI_TEMPLATE (parm)
20783 == CLASSTYPE_TI_TEMPLATE (arg)))
20784 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20785 Then, we should unify `int' and `U'. */
20786 t = arg;
20787 else
20788 /* There's no chance of unification succeeding. */
20789 return unify_type_mismatch (explain_p, parm, arg);
20791 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20792 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20794 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20795 return unify_type_mismatch (explain_p, parm, arg);
20796 return unify_success (explain_p);
20798 case METHOD_TYPE:
20799 case FUNCTION_TYPE:
20801 unsigned int nargs;
20802 tree *args;
20803 tree a;
20804 unsigned int i;
20806 if (TREE_CODE (arg) != TREE_CODE (parm))
20807 return unify_type_mismatch (explain_p, parm, arg);
20809 /* CV qualifications for methods can never be deduced, they must
20810 match exactly. We need to check them explicitly here,
20811 because type_unification_real treats them as any other
20812 cv-qualified parameter. */
20813 if (TREE_CODE (parm) == METHOD_TYPE
20814 && (!check_cv_quals_for_unify
20815 (UNIFY_ALLOW_NONE,
20816 class_of_this_parm (arg),
20817 class_of_this_parm (parm))))
20818 return unify_cv_qual_mismatch (explain_p, parm, arg);
20819 if (TREE_CODE (arg) == FUNCTION_TYPE
20820 && type_memfn_quals (parm) != type_memfn_quals (arg))
20821 return unify_cv_qual_mismatch (explain_p, parm, arg);
20822 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20823 return unify_type_mismatch (explain_p, parm, arg);
20825 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20826 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20828 nargs = list_length (TYPE_ARG_TYPES (arg));
20829 args = XALLOCAVEC (tree, nargs);
20830 for (a = TYPE_ARG_TYPES (arg), i = 0;
20831 a != NULL_TREE && a != void_list_node;
20832 a = TREE_CHAIN (a), ++i)
20833 args[i] = TREE_VALUE (a);
20834 nargs = i;
20836 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20837 args, nargs, 1, DEDUCE_EXACT,
20838 LOOKUP_NORMAL, NULL, explain_p))
20839 return 1;
20841 if (flag_noexcept_type)
20843 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20844 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20845 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20846 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20847 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20848 && uses_template_parms (TREE_PURPOSE (pspec)))
20849 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20850 TREE_PURPOSE (aspec),
20851 UNIFY_ALLOW_NONE, explain_p);
20852 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20853 return unify_type_mismatch (explain_p, parm, arg);
20856 return 0;
20859 case OFFSET_TYPE:
20860 /* Unify a pointer to member with a pointer to member function, which
20861 deduces the type of the member as a function type. */
20862 if (TYPE_PTRMEMFUNC_P (arg))
20864 /* Check top-level cv qualifiers */
20865 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20866 return unify_cv_qual_mismatch (explain_p, parm, arg);
20868 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20869 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20870 UNIFY_ALLOW_NONE, explain_p);
20872 /* Determine the type of the function we are unifying against. */
20873 tree fntype = static_fn_type (arg);
20875 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20878 if (TREE_CODE (arg) != OFFSET_TYPE)
20879 return unify_type_mismatch (explain_p, parm, arg);
20880 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20881 TYPE_OFFSET_BASETYPE (arg),
20882 UNIFY_ALLOW_NONE, explain_p);
20883 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20884 strict, explain_p);
20886 case CONST_DECL:
20887 if (DECL_TEMPLATE_PARM_P (parm))
20888 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20889 if (arg != scalar_constant_value (parm))
20890 return unify_template_argument_mismatch (explain_p, parm, arg);
20891 return unify_success (explain_p);
20893 case FIELD_DECL:
20894 case TEMPLATE_DECL:
20895 /* Matched cases are handled by the ARG == PARM test above. */
20896 return unify_template_argument_mismatch (explain_p, parm, arg);
20898 case VAR_DECL:
20899 /* We might get a variable as a non-type template argument in parm if the
20900 corresponding parameter is type-dependent. Make any necessary
20901 adjustments based on whether arg is a reference. */
20902 if (CONSTANT_CLASS_P (arg))
20903 parm = fold_non_dependent_expr (parm);
20904 else if (REFERENCE_REF_P (arg))
20906 tree sub = TREE_OPERAND (arg, 0);
20907 STRIP_NOPS (sub);
20908 if (TREE_CODE (sub) == ADDR_EXPR)
20909 arg = TREE_OPERAND (sub, 0);
20911 /* Now use the normal expression code to check whether they match. */
20912 goto expr;
20914 case TYPE_ARGUMENT_PACK:
20915 case NONTYPE_ARGUMENT_PACK:
20916 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20917 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20919 case TYPEOF_TYPE:
20920 case DECLTYPE_TYPE:
20921 case UNDERLYING_TYPE:
20922 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20923 or UNDERLYING_TYPE nodes. */
20924 return unify_success (explain_p);
20926 case ERROR_MARK:
20927 /* Unification fails if we hit an error node. */
20928 return unify_invalid (explain_p);
20930 case INDIRECT_REF:
20931 if (REFERENCE_REF_P (parm))
20933 bool pexp = PACK_EXPANSION_P (arg);
20934 if (pexp)
20935 arg = PACK_EXPANSION_PATTERN (arg);
20936 if (REFERENCE_REF_P (arg))
20937 arg = TREE_OPERAND (arg, 0);
20938 if (pexp)
20939 arg = make_pack_expansion (arg);
20940 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20941 strict, explain_p);
20943 /* FALLTHRU */
20945 default:
20946 /* An unresolved overload is a nondeduced context. */
20947 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20948 return unify_success (explain_p);
20949 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
20950 expr:
20951 /* We must be looking at an expression. This can happen with
20952 something like:
20954 template <int I>
20955 void foo(S<I>, S<I + 2>);
20957 This is a "nondeduced context":
20959 [deduct.type]
20961 The nondeduced contexts are:
20963 --A type that is a template-id in which one or more of
20964 the template-arguments is an expression that references
20965 a template-parameter.
20967 In these cases, we assume deduction succeeded, but don't
20968 actually infer any unifications. */
20970 if (!uses_template_parms (parm)
20971 && !template_args_equal (parm, arg))
20972 return unify_expression_unequal (explain_p, parm, arg);
20973 else
20974 return unify_success (explain_p);
20977 #undef RECUR_AND_CHECK_FAILURE
20979 /* Note that DECL can be defined in this translation unit, if
20980 required. */
20982 static void
20983 mark_definable (tree decl)
20985 tree clone;
20986 DECL_NOT_REALLY_EXTERN (decl) = 1;
20987 FOR_EACH_CLONE (clone, decl)
20988 DECL_NOT_REALLY_EXTERN (clone) = 1;
20991 /* Called if RESULT is explicitly instantiated, or is a member of an
20992 explicitly instantiated class. */
20994 void
20995 mark_decl_instantiated (tree result, int extern_p)
20997 SET_DECL_EXPLICIT_INSTANTIATION (result);
20999 /* If this entity has already been written out, it's too late to
21000 make any modifications. */
21001 if (TREE_ASM_WRITTEN (result))
21002 return;
21004 /* For anonymous namespace we don't need to do anything. */
21005 if (decl_anon_ns_mem_p (result))
21007 gcc_assert (!TREE_PUBLIC (result));
21008 return;
21011 if (TREE_CODE (result) != FUNCTION_DECL)
21012 /* The TREE_PUBLIC flag for function declarations will have been
21013 set correctly by tsubst. */
21014 TREE_PUBLIC (result) = 1;
21016 /* This might have been set by an earlier implicit instantiation. */
21017 DECL_COMDAT (result) = 0;
21019 if (extern_p)
21020 DECL_NOT_REALLY_EXTERN (result) = 0;
21021 else
21023 mark_definable (result);
21024 mark_needed (result);
21025 /* Always make artificials weak. */
21026 if (DECL_ARTIFICIAL (result) && flag_weak)
21027 comdat_linkage (result);
21028 /* For WIN32 we also want to put explicit instantiations in
21029 linkonce sections. */
21030 else if (TREE_PUBLIC (result))
21031 maybe_make_one_only (result);
21034 /* If EXTERN_P, then this function will not be emitted -- unless
21035 followed by an explicit instantiation, at which point its linkage
21036 will be adjusted. If !EXTERN_P, then this function will be
21037 emitted here. In neither circumstance do we want
21038 import_export_decl to adjust the linkage. */
21039 DECL_INTERFACE_KNOWN (result) = 1;
21042 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21043 important template arguments. If any are missing, we check whether
21044 they're important by using error_mark_node for substituting into any
21045 args that were used for partial ordering (the ones between ARGS and END)
21046 and seeing if it bubbles up. */
21048 static bool
21049 check_undeduced_parms (tree targs, tree args, tree end)
21051 bool found = false;
21052 int i;
21053 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21054 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21056 found = true;
21057 TREE_VEC_ELT (targs, i) = error_mark_node;
21059 if (found)
21061 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21062 if (substed == error_mark_node)
21063 return true;
21065 return false;
21068 /* Given two function templates PAT1 and PAT2, return:
21070 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21071 -1 if PAT2 is more specialized than PAT1.
21072 0 if neither is more specialized.
21074 LEN indicates the number of parameters we should consider
21075 (defaulted parameters should not be considered).
21077 The 1998 std underspecified function template partial ordering, and
21078 DR214 addresses the issue. We take pairs of arguments, one from
21079 each of the templates, and deduce them against each other. One of
21080 the templates will be more specialized if all the *other*
21081 template's arguments deduce against its arguments and at least one
21082 of its arguments *does* *not* deduce against the other template's
21083 corresponding argument. Deduction is done as for class templates.
21084 The arguments used in deduction have reference and top level cv
21085 qualifiers removed. Iff both arguments were originally reference
21086 types *and* deduction succeeds in both directions, an lvalue reference
21087 wins against an rvalue reference and otherwise the template
21088 with the more cv-qualified argument wins for that pairing (if
21089 neither is more cv-qualified, they both are equal). Unlike regular
21090 deduction, after all the arguments have been deduced in this way,
21091 we do *not* verify the deduced template argument values can be
21092 substituted into non-deduced contexts.
21094 The logic can be a bit confusing here, because we look at deduce1 and
21095 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21096 can find template arguments for pat1 to make arg1 look like arg2, that
21097 means that arg2 is at least as specialized as arg1. */
21100 more_specialized_fn (tree pat1, tree pat2, int len)
21102 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21103 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21104 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21105 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21106 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21107 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21108 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21109 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21110 tree origs1, origs2;
21111 bool lose1 = false;
21112 bool lose2 = false;
21114 /* Remove the this parameter from non-static member functions. If
21115 one is a non-static member function and the other is not a static
21116 member function, remove the first parameter from that function
21117 also. This situation occurs for operator functions where we
21118 locate both a member function (with this pointer) and non-member
21119 operator (with explicit first operand). */
21120 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21122 len--; /* LEN is the number of significant arguments for DECL1 */
21123 args1 = TREE_CHAIN (args1);
21124 if (!DECL_STATIC_FUNCTION_P (decl2))
21125 args2 = TREE_CHAIN (args2);
21127 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21129 args2 = TREE_CHAIN (args2);
21130 if (!DECL_STATIC_FUNCTION_P (decl1))
21132 len--;
21133 args1 = TREE_CHAIN (args1);
21137 /* If only one is a conversion operator, they are unordered. */
21138 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21139 return 0;
21141 /* Consider the return type for a conversion function */
21142 if (DECL_CONV_FN_P (decl1))
21144 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21145 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21146 len++;
21149 processing_template_decl++;
21151 origs1 = args1;
21152 origs2 = args2;
21154 while (len--
21155 /* Stop when an ellipsis is seen. */
21156 && args1 != NULL_TREE && args2 != NULL_TREE)
21158 tree arg1 = TREE_VALUE (args1);
21159 tree arg2 = TREE_VALUE (args2);
21160 int deduce1, deduce2;
21161 int quals1 = -1;
21162 int quals2 = -1;
21163 int ref1 = 0;
21164 int ref2 = 0;
21166 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21167 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21169 /* When both arguments are pack expansions, we need only
21170 unify the patterns themselves. */
21171 arg1 = PACK_EXPANSION_PATTERN (arg1);
21172 arg2 = PACK_EXPANSION_PATTERN (arg2);
21174 /* This is the last comparison we need to do. */
21175 len = 0;
21178 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21180 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21181 arg1 = TREE_TYPE (arg1);
21182 quals1 = cp_type_quals (arg1);
21185 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21187 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21188 arg2 = TREE_TYPE (arg2);
21189 quals2 = cp_type_quals (arg2);
21192 arg1 = TYPE_MAIN_VARIANT (arg1);
21193 arg2 = TYPE_MAIN_VARIANT (arg2);
21195 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21197 int i, len2 = remaining_arguments (args2);
21198 tree parmvec = make_tree_vec (1);
21199 tree argvec = make_tree_vec (len2);
21200 tree ta = args2;
21202 /* Setup the parameter vector, which contains only ARG1. */
21203 TREE_VEC_ELT (parmvec, 0) = arg1;
21205 /* Setup the argument vector, which contains the remaining
21206 arguments. */
21207 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21208 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21210 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21211 argvec, DEDUCE_EXACT,
21212 /*subr=*/true, /*explain_p=*/false)
21213 == 0);
21215 /* We cannot deduce in the other direction, because ARG1 is
21216 a pack expansion but ARG2 is not. */
21217 deduce2 = 0;
21219 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21221 int i, len1 = remaining_arguments (args1);
21222 tree parmvec = make_tree_vec (1);
21223 tree argvec = make_tree_vec (len1);
21224 tree ta = args1;
21226 /* Setup the parameter vector, which contains only ARG1. */
21227 TREE_VEC_ELT (parmvec, 0) = arg2;
21229 /* Setup the argument vector, which contains the remaining
21230 arguments. */
21231 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21232 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21234 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21235 argvec, DEDUCE_EXACT,
21236 /*subr=*/true, /*explain_p=*/false)
21237 == 0);
21239 /* We cannot deduce in the other direction, because ARG2 is
21240 a pack expansion but ARG1 is not.*/
21241 deduce1 = 0;
21244 else
21246 /* The normal case, where neither argument is a pack
21247 expansion. */
21248 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21249 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21250 == 0);
21251 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21252 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21253 == 0);
21256 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21257 arg2, then arg2 is not as specialized as arg1. */
21258 if (!deduce1)
21259 lose2 = true;
21260 if (!deduce2)
21261 lose1 = true;
21263 /* "If, for a given type, deduction succeeds in both directions
21264 (i.e., the types are identical after the transformations above)
21265 and both P and A were reference types (before being replaced with
21266 the type referred to above):
21267 - if the type from the argument template was an lvalue reference and
21268 the type from the parameter template was not, the argument type is
21269 considered to be more specialized than the other; otherwise,
21270 - if the type from the argument template is more cv-qualified
21271 than the type from the parameter template (as described above),
21272 the argument type is considered to be more specialized than the other;
21273 otherwise,
21274 - neither type is more specialized than the other." */
21276 if (deduce1 && deduce2)
21278 if (ref1 && ref2 && ref1 != ref2)
21280 if (ref1 > ref2)
21281 lose1 = true;
21282 else
21283 lose2 = true;
21285 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21287 if ((quals1 & quals2) == quals2)
21288 lose2 = true;
21289 if ((quals1 & quals2) == quals1)
21290 lose1 = true;
21294 if (lose1 && lose2)
21295 /* We've failed to deduce something in either direction.
21296 These must be unordered. */
21297 break;
21299 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21300 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21301 /* We have already processed all of the arguments in our
21302 handing of the pack expansion type. */
21303 len = 0;
21305 args1 = TREE_CHAIN (args1);
21306 args2 = TREE_CHAIN (args2);
21309 /* "In most cases, all template parameters must have values in order for
21310 deduction to succeed, but for partial ordering purposes a template
21311 parameter may remain without a value provided it is not used in the
21312 types being used for partial ordering."
21314 Thus, if we are missing any of the targs1 we need to substitute into
21315 origs1, then pat2 is not as specialized as pat1. This can happen when
21316 there is a nondeduced context. */
21317 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21318 lose2 = true;
21319 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21320 lose1 = true;
21322 processing_template_decl--;
21324 /* If both deductions succeed, the partial ordering selects the more
21325 constrained template. */
21326 if (!lose1 && !lose2)
21328 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21329 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21330 lose1 = !subsumes_constraints (c1, c2);
21331 lose2 = !subsumes_constraints (c2, c1);
21334 /* All things being equal, if the next argument is a pack expansion
21335 for one function but not for the other, prefer the
21336 non-variadic function. FIXME this is bogus; see c++/41958. */
21337 if (lose1 == lose2
21338 && args1 && TREE_VALUE (args1)
21339 && args2 && TREE_VALUE (args2))
21341 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21342 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21345 if (lose1 == lose2)
21346 return 0;
21347 else if (!lose1)
21348 return 1;
21349 else
21350 return -1;
21353 /* Determine which of two partial specializations of TMPL is more
21354 specialized.
21356 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21357 to the first partial specialization. The TREE_PURPOSE is the
21358 innermost set of template parameters for the partial
21359 specialization. PAT2 is similar, but for the second template.
21361 Return 1 if the first partial specialization is more specialized;
21362 -1 if the second is more specialized; 0 if neither is more
21363 specialized.
21365 See [temp.class.order] for information about determining which of
21366 two templates is more specialized. */
21368 static int
21369 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21371 tree targs;
21372 int winner = 0;
21373 bool any_deductions = false;
21375 tree tmpl1 = TREE_VALUE (pat1);
21376 tree tmpl2 = TREE_VALUE (pat2);
21377 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21378 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21380 /* Just like what happens for functions, if we are ordering between
21381 different template specializations, we may encounter dependent
21382 types in the arguments, and we need our dependency check functions
21383 to behave correctly. */
21384 ++processing_template_decl;
21385 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21386 if (targs)
21388 --winner;
21389 any_deductions = true;
21392 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21393 if (targs)
21395 ++winner;
21396 any_deductions = true;
21398 --processing_template_decl;
21400 /* If both deductions succeed, the partial ordering selects the more
21401 constrained template. */
21402 if (!winner && any_deductions)
21403 return more_constrained (tmpl1, tmpl2);
21405 /* In the case of a tie where at least one of the templates
21406 has a parameter pack at the end, the template with the most
21407 non-packed parameters wins. */
21408 if (winner == 0
21409 && any_deductions
21410 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21411 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21413 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21414 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21415 int len1 = TREE_VEC_LENGTH (args1);
21416 int len2 = TREE_VEC_LENGTH (args2);
21418 /* We don't count the pack expansion at the end. */
21419 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21420 --len1;
21421 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21422 --len2;
21424 if (len1 > len2)
21425 return 1;
21426 else if (len1 < len2)
21427 return -1;
21430 return winner;
21433 /* Return the template arguments that will produce the function signature
21434 DECL from the function template FN, with the explicit template
21435 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21436 also match. Return NULL_TREE if no satisfactory arguments could be
21437 found. */
21439 static tree
21440 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21442 int ntparms = DECL_NTPARMS (fn);
21443 tree targs = make_tree_vec (ntparms);
21444 tree decl_type = TREE_TYPE (decl);
21445 tree decl_arg_types;
21446 tree *args;
21447 unsigned int nargs, ix;
21448 tree arg;
21450 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21452 /* Never do unification on the 'this' parameter. */
21453 decl_arg_types = skip_artificial_parms_for (decl,
21454 TYPE_ARG_TYPES (decl_type));
21456 nargs = list_length (decl_arg_types);
21457 args = XALLOCAVEC (tree, nargs);
21458 for (arg = decl_arg_types, ix = 0;
21459 arg != NULL_TREE && arg != void_list_node;
21460 arg = TREE_CHAIN (arg), ++ix)
21461 args[ix] = TREE_VALUE (arg);
21463 if (fn_type_unification (fn, explicit_args, targs,
21464 args, ix,
21465 (check_rettype || DECL_CONV_FN_P (fn)
21466 ? TREE_TYPE (decl_type) : NULL_TREE),
21467 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21468 /*decltype*/false)
21469 == error_mark_node)
21470 return NULL_TREE;
21472 return targs;
21475 /* Return the innermost template arguments that, when applied to a partial
21476 specialization SPEC_TMPL of TMPL, yield the ARGS.
21478 For example, suppose we have:
21480 template <class T, class U> struct S {};
21481 template <class T> struct S<T*, int> {};
21483 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21484 partial specialization and the ARGS will be {double*, int}. The resulting
21485 vector will be {double}, indicating that `T' is bound to `double'. */
21487 static tree
21488 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21490 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21491 tree spec_args
21492 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21493 int i, ntparms = TREE_VEC_LENGTH (tparms);
21494 tree deduced_args;
21495 tree innermost_deduced_args;
21497 innermost_deduced_args = make_tree_vec (ntparms);
21498 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21500 deduced_args = copy_node (args);
21501 SET_TMPL_ARGS_LEVEL (deduced_args,
21502 TMPL_ARGS_DEPTH (deduced_args),
21503 innermost_deduced_args);
21505 else
21506 deduced_args = innermost_deduced_args;
21508 bool tried_array_deduction = (cxx_dialect < cxx1z);
21509 again:
21510 if (unify (tparms, deduced_args,
21511 INNERMOST_TEMPLATE_ARGS (spec_args),
21512 INNERMOST_TEMPLATE_ARGS (args),
21513 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21514 return NULL_TREE;
21516 for (i = 0; i < ntparms; ++i)
21517 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21519 if (!tried_array_deduction)
21521 try_array_deduction (tparms, innermost_deduced_args,
21522 INNERMOST_TEMPLATE_ARGS (spec_args));
21523 tried_array_deduction = true;
21524 if (TREE_VEC_ELT (innermost_deduced_args, i))
21525 goto again;
21527 return NULL_TREE;
21530 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21531 if (!push_tinst_level (tinst))
21533 excessive_deduction_depth = true;
21534 return NULL_TREE;
21537 /* Verify that nondeduced template arguments agree with the type
21538 obtained from argument deduction.
21540 For example:
21542 struct A { typedef int X; };
21543 template <class T, class U> struct C {};
21544 template <class T> struct C<T, typename T::X> {};
21546 Then with the instantiation `C<A, int>', we can deduce that
21547 `T' is `A' but unify () does not check whether `typename T::X'
21548 is `int'. */
21549 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21550 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21551 spec_args, tmpl,
21552 tf_none, false, false);
21554 pop_tinst_level ();
21556 if (spec_args == error_mark_node
21557 /* We only need to check the innermost arguments; the other
21558 arguments will always agree. */
21559 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
21560 INNERMOST_TEMPLATE_ARGS (args)))
21561 return NULL_TREE;
21563 /* Now that we have bindings for all of the template arguments,
21564 ensure that the arguments deduced for the template template
21565 parameters have compatible template parameter lists. See the use
21566 of template_template_parm_bindings_ok_p in fn_type_unification
21567 for more information. */
21568 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21569 return NULL_TREE;
21571 return deduced_args;
21574 // Compare two function templates T1 and T2 by deducing bindings
21575 // from one against the other. If both deductions succeed, compare
21576 // constraints to see which is more constrained.
21577 static int
21578 more_specialized_inst (tree t1, tree t2)
21580 int fate = 0;
21581 int count = 0;
21583 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21585 --fate;
21586 ++count;
21589 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21591 ++fate;
21592 ++count;
21595 // If both deductions succeed, then one may be more constrained.
21596 if (count == 2 && fate == 0)
21597 fate = more_constrained (t1, t2);
21599 return fate;
21602 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21603 Return the TREE_LIST node with the most specialized template, if
21604 any. If there is no most specialized template, the error_mark_node
21605 is returned.
21607 Note that this function does not look at, or modify, the
21608 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21609 returned is one of the elements of INSTANTIATIONS, callers may
21610 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21611 and retrieve it from the value returned. */
21613 tree
21614 most_specialized_instantiation (tree templates)
21616 tree fn, champ;
21618 ++processing_template_decl;
21620 champ = templates;
21621 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21623 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21624 if (fate == -1)
21625 champ = fn;
21626 else if (!fate)
21628 /* Equally specialized, move to next function. If there
21629 is no next function, nothing's most specialized. */
21630 fn = TREE_CHAIN (fn);
21631 champ = fn;
21632 if (!fn)
21633 break;
21637 if (champ)
21638 /* Now verify that champ is better than everything earlier in the
21639 instantiation list. */
21640 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21641 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21643 champ = NULL_TREE;
21644 break;
21648 processing_template_decl--;
21650 if (!champ)
21651 return error_mark_node;
21653 return champ;
21656 /* If DECL is a specialization of some template, return the most
21657 general such template. Otherwise, returns NULL_TREE.
21659 For example, given:
21661 template <class T> struct S { template <class U> void f(U); };
21663 if TMPL is `template <class U> void S<int>::f(U)' this will return
21664 the full template. This function will not trace past partial
21665 specializations, however. For example, given in addition:
21667 template <class T> struct S<T*> { template <class U> void f(U); };
21669 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21670 `template <class T> template <class U> S<T*>::f(U)'. */
21672 tree
21673 most_general_template (tree decl)
21675 if (TREE_CODE (decl) != TEMPLATE_DECL)
21677 if (tree tinfo = get_template_info (decl))
21678 decl = TI_TEMPLATE (tinfo);
21679 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21680 template friend, or a FIELD_DECL for a capture pack. */
21681 if (TREE_CODE (decl) != TEMPLATE_DECL)
21682 return NULL_TREE;
21685 /* Look for more and more general templates. */
21686 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21688 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21689 (See cp-tree.h for details.) */
21690 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21691 break;
21693 if (CLASS_TYPE_P (TREE_TYPE (decl))
21694 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21695 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21696 break;
21698 /* Stop if we run into an explicitly specialized class template. */
21699 if (!DECL_NAMESPACE_SCOPE_P (decl)
21700 && DECL_CONTEXT (decl)
21701 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21702 break;
21704 decl = DECL_TI_TEMPLATE (decl);
21707 return decl;
21710 /* Return the most specialized of the template partial specializations
21711 which can produce TARGET, a specialization of some class or variable
21712 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21713 a TEMPLATE_DECL node corresponding to the partial specialization, while
21714 the TREE_PURPOSE is the set of template arguments that must be
21715 substituted into the template pattern in order to generate TARGET.
21717 If the choice of partial specialization is ambiguous, a diagnostic
21718 is issued, and the error_mark_node is returned. If there are no
21719 partial specializations matching TARGET, then NULL_TREE is
21720 returned, indicating that the primary template should be used. */
21722 static tree
21723 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21725 tree list = NULL_TREE;
21726 tree t;
21727 tree champ;
21728 int fate;
21729 bool ambiguous_p;
21730 tree outer_args = NULL_TREE;
21731 tree tmpl, args;
21733 if (TYPE_P (target))
21735 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21736 tmpl = TI_TEMPLATE (tinfo);
21737 args = TI_ARGS (tinfo);
21739 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21741 tmpl = TREE_OPERAND (target, 0);
21742 args = TREE_OPERAND (target, 1);
21744 else if (VAR_P (target))
21746 tree tinfo = DECL_TEMPLATE_INFO (target);
21747 tmpl = TI_TEMPLATE (tinfo);
21748 args = TI_ARGS (tinfo);
21750 else
21751 gcc_unreachable ();
21753 tree main_tmpl = most_general_template (tmpl);
21755 /* For determining which partial specialization to use, only the
21756 innermost args are interesting. */
21757 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21759 outer_args = strip_innermost_template_args (args, 1);
21760 args = INNERMOST_TEMPLATE_ARGS (args);
21763 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21765 tree spec_args;
21766 tree spec_tmpl = TREE_VALUE (t);
21768 if (outer_args)
21770 /* Substitute in the template args from the enclosing class. */
21771 ++processing_template_decl;
21772 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21773 --processing_template_decl;
21776 if (spec_tmpl == error_mark_node)
21777 return error_mark_node;
21779 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21780 if (spec_args)
21782 if (outer_args)
21783 spec_args = add_to_template_args (outer_args, spec_args);
21785 /* Keep the candidate only if the constraints are satisfied,
21786 or if we're not compiling with concepts. */
21787 if (!flag_concepts
21788 || constraints_satisfied_p (spec_tmpl, spec_args))
21790 list = tree_cons (spec_args, TREE_VALUE (t), list);
21791 TREE_TYPE (list) = TREE_TYPE (t);
21796 if (! list)
21797 return NULL_TREE;
21799 ambiguous_p = false;
21800 t = list;
21801 champ = t;
21802 t = TREE_CHAIN (t);
21803 for (; t; t = TREE_CHAIN (t))
21805 fate = more_specialized_partial_spec (tmpl, champ, t);
21806 if (fate == 1)
21808 else
21810 if (fate == 0)
21812 t = TREE_CHAIN (t);
21813 if (! t)
21815 ambiguous_p = true;
21816 break;
21819 champ = t;
21823 if (!ambiguous_p)
21824 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21826 fate = more_specialized_partial_spec (tmpl, champ, t);
21827 if (fate != 1)
21829 ambiguous_p = true;
21830 break;
21834 if (ambiguous_p)
21836 const char *str;
21837 char *spaces = NULL;
21838 if (!(complain & tf_error))
21839 return error_mark_node;
21840 if (TYPE_P (target))
21841 error ("ambiguous template instantiation for %q#T", target);
21842 else
21843 error ("ambiguous template instantiation for %q#D", target);
21844 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21845 for (t = list; t; t = TREE_CHAIN (t))
21847 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21848 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21849 "%s %#S", spaces ? spaces : str, subst);
21850 spaces = spaces ? spaces : get_spaces (str);
21852 free (spaces);
21853 return error_mark_node;
21856 return champ;
21859 /* Explicitly instantiate DECL. */
21861 void
21862 do_decl_instantiation (tree decl, tree storage)
21864 tree result = NULL_TREE;
21865 int extern_p = 0;
21867 if (!decl || decl == error_mark_node)
21868 /* An error occurred, for which grokdeclarator has already issued
21869 an appropriate message. */
21870 return;
21871 else if (! DECL_LANG_SPECIFIC (decl))
21873 error ("explicit instantiation of non-template %q#D", decl);
21874 return;
21877 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21878 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21880 if (VAR_P (decl) && !var_templ)
21882 /* There is an asymmetry here in the way VAR_DECLs and
21883 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21884 the latter, the DECL we get back will be marked as a
21885 template instantiation, and the appropriate
21886 DECL_TEMPLATE_INFO will be set up. This does not happen for
21887 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21888 should handle VAR_DECLs as it currently handles
21889 FUNCTION_DECLs. */
21890 if (!DECL_CLASS_SCOPE_P (decl))
21892 error ("%qD is not a static data member of a class template", decl);
21893 return;
21895 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21896 if (!result || !VAR_P (result))
21898 error ("no matching template for %qD found", decl);
21899 return;
21901 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21903 error ("type %qT for explicit instantiation %qD does not match "
21904 "declared type %qT", TREE_TYPE (result), decl,
21905 TREE_TYPE (decl));
21906 return;
21909 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21911 error ("explicit instantiation of %q#D", decl);
21912 return;
21914 else
21915 result = decl;
21917 /* Check for various error cases. Note that if the explicit
21918 instantiation is valid the RESULT will currently be marked as an
21919 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21920 until we get here. */
21922 if (DECL_TEMPLATE_SPECIALIZATION (result))
21924 /* DR 259 [temp.spec].
21926 Both an explicit instantiation and a declaration of an explicit
21927 specialization shall not appear in a program unless the explicit
21928 instantiation follows a declaration of the explicit specialization.
21930 For a given set of template parameters, if an explicit
21931 instantiation of a template appears after a declaration of an
21932 explicit specialization for that template, the explicit
21933 instantiation has no effect. */
21934 return;
21936 else if (DECL_EXPLICIT_INSTANTIATION (result))
21938 /* [temp.spec]
21940 No program shall explicitly instantiate any template more
21941 than once.
21943 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21944 the first instantiation was `extern' and the second is not,
21945 and EXTERN_P for the opposite case. */
21946 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21947 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21948 /* If an "extern" explicit instantiation follows an ordinary
21949 explicit instantiation, the template is instantiated. */
21950 if (extern_p)
21951 return;
21953 else if (!DECL_IMPLICIT_INSTANTIATION (result))
21955 error ("no matching template for %qD found", result);
21956 return;
21958 else if (!DECL_TEMPLATE_INFO (result))
21960 permerror (input_location, "explicit instantiation of non-template %q#D", result);
21961 return;
21964 if (storage == NULL_TREE)
21966 else if (storage == ridpointers[(int) RID_EXTERN])
21968 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
21969 pedwarn (input_location, OPT_Wpedantic,
21970 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21971 "instantiations");
21972 extern_p = 1;
21974 else
21975 error ("storage class %qD applied to template instantiation", storage);
21977 check_explicit_instantiation_namespace (result);
21978 mark_decl_instantiated (result, extern_p);
21979 if (! extern_p)
21980 instantiate_decl (result, /*defer_ok=*/true,
21981 /*expl_inst_class_mem_p=*/false);
21984 static void
21985 mark_class_instantiated (tree t, int extern_p)
21987 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
21988 SET_CLASSTYPE_INTERFACE_KNOWN (t);
21989 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
21990 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
21991 if (! extern_p)
21993 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
21994 rest_of_type_compilation (t, 1);
21998 /* Called from do_type_instantiation through binding_table_foreach to
21999 do recursive instantiation for the type bound in ENTRY. */
22000 static void
22001 bt_instantiate_type_proc (binding_entry entry, void *data)
22003 tree storage = *(tree *) data;
22005 if (MAYBE_CLASS_TYPE_P (entry->type)
22006 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22007 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22010 /* Called from do_type_instantiation to instantiate a member
22011 (a member function or a static member variable) of an
22012 explicitly instantiated class template. */
22013 static void
22014 instantiate_class_member (tree decl, int extern_p)
22016 mark_decl_instantiated (decl, extern_p);
22017 if (! extern_p)
22018 instantiate_decl (decl, /*defer_ok=*/true,
22019 /*expl_inst_class_mem_p=*/true);
22022 /* Perform an explicit instantiation of template class T. STORAGE, if
22023 non-null, is the RID for extern, inline or static. COMPLAIN is
22024 nonzero if this is called from the parser, zero if called recursively,
22025 since the standard is unclear (as detailed below). */
22027 void
22028 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22030 int extern_p = 0;
22031 int nomem_p = 0;
22032 int static_p = 0;
22033 int previous_instantiation_extern_p = 0;
22035 if (TREE_CODE (t) == TYPE_DECL)
22036 t = TREE_TYPE (t);
22038 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22040 tree tmpl =
22041 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22042 if (tmpl)
22043 error ("explicit instantiation of non-class template %qD", tmpl);
22044 else
22045 error ("explicit instantiation of non-template type %qT", t);
22046 return;
22049 complete_type (t);
22051 if (!COMPLETE_TYPE_P (t))
22053 if (complain & tf_error)
22054 error ("explicit instantiation of %q#T before definition of template",
22056 return;
22059 if (storage != NULL_TREE)
22061 if (!in_system_header_at (input_location))
22063 if (storage == ridpointers[(int) RID_EXTERN])
22065 if (cxx_dialect == cxx98)
22066 pedwarn (input_location, OPT_Wpedantic,
22067 "ISO C++ 1998 forbids the use of %<extern%> on "
22068 "explicit instantiations");
22070 else
22071 pedwarn (input_location, OPT_Wpedantic,
22072 "ISO C++ forbids the use of %qE"
22073 " on explicit instantiations", storage);
22076 if (storage == ridpointers[(int) RID_INLINE])
22077 nomem_p = 1;
22078 else if (storage == ridpointers[(int) RID_EXTERN])
22079 extern_p = 1;
22080 else if (storage == ridpointers[(int) RID_STATIC])
22081 static_p = 1;
22082 else
22084 error ("storage class %qD applied to template instantiation",
22085 storage);
22086 extern_p = 0;
22090 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22092 /* DR 259 [temp.spec].
22094 Both an explicit instantiation and a declaration of an explicit
22095 specialization shall not appear in a program unless the explicit
22096 instantiation follows a declaration of the explicit specialization.
22098 For a given set of template parameters, if an explicit
22099 instantiation of a template appears after a declaration of an
22100 explicit specialization for that template, the explicit
22101 instantiation has no effect. */
22102 return;
22104 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22106 /* [temp.spec]
22108 No program shall explicitly instantiate any template more
22109 than once.
22111 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22112 instantiation was `extern'. If EXTERN_P then the second is.
22113 These cases are OK. */
22114 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22116 if (!previous_instantiation_extern_p && !extern_p
22117 && (complain & tf_error))
22118 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22120 /* If we've already instantiated the template, just return now. */
22121 if (!CLASSTYPE_INTERFACE_ONLY (t))
22122 return;
22125 check_explicit_instantiation_namespace (TYPE_NAME (t));
22126 mark_class_instantiated (t, extern_p);
22128 if (nomem_p)
22129 return;
22132 tree tmp;
22134 /* In contrast to implicit instantiation, where only the
22135 declarations, and not the definitions, of members are
22136 instantiated, we have here:
22138 [temp.explicit]
22140 The explicit instantiation of a class template specialization
22141 implies the instantiation of all of its members not
22142 previously explicitly specialized in the translation unit
22143 containing the explicit instantiation.
22145 Of course, we can't instantiate member template classes, since
22146 we don't have any arguments for them. Note that the standard
22147 is unclear on whether the instantiation of the members are
22148 *explicit* instantiations or not. However, the most natural
22149 interpretation is that it should be an explicit instantiation. */
22151 if (! static_p)
22152 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22153 if (TREE_CODE (tmp) == FUNCTION_DECL
22154 && DECL_TEMPLATE_INSTANTIATION (tmp)
22155 && user_provided_p (tmp))
22156 instantiate_class_member (tmp, extern_p);
22158 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22159 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22160 instantiate_class_member (tmp, extern_p);
22162 if (CLASSTYPE_NESTED_UTDS (t))
22163 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22164 bt_instantiate_type_proc, &storage);
22168 /* Given a function DECL, which is a specialization of TMPL, modify
22169 DECL to be a re-instantiation of TMPL with the same template
22170 arguments. TMPL should be the template into which tsubst'ing
22171 should occur for DECL, not the most general template.
22173 One reason for doing this is a scenario like this:
22175 template <class T>
22176 void f(const T&, int i);
22178 void g() { f(3, 7); }
22180 template <class T>
22181 void f(const T& t, const int i) { }
22183 Note that when the template is first instantiated, with
22184 instantiate_template, the resulting DECL will have no name for the
22185 first parameter, and the wrong type for the second. So, when we go
22186 to instantiate the DECL, we regenerate it. */
22188 static void
22189 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22191 /* The arguments used to instantiate DECL, from the most general
22192 template. */
22193 tree code_pattern;
22195 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22197 /* Make sure that we can see identifiers, and compute access
22198 correctly. */
22199 push_access_scope (decl);
22201 if (TREE_CODE (decl) == FUNCTION_DECL)
22203 tree decl_parm;
22204 tree pattern_parm;
22205 tree specs;
22206 int args_depth;
22207 int parms_depth;
22209 args_depth = TMPL_ARGS_DEPTH (args);
22210 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22211 if (args_depth > parms_depth)
22212 args = get_innermost_template_args (args, parms_depth);
22214 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22215 args, tf_error, NULL_TREE,
22216 /*defer_ok*/false);
22217 if (specs && specs != error_mark_node)
22218 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22219 specs);
22221 /* Merge parameter declarations. */
22222 decl_parm = skip_artificial_parms_for (decl,
22223 DECL_ARGUMENTS (decl));
22224 pattern_parm
22225 = skip_artificial_parms_for (code_pattern,
22226 DECL_ARGUMENTS (code_pattern));
22227 while (decl_parm && !DECL_PACK_P (pattern_parm))
22229 tree parm_type;
22230 tree attributes;
22232 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22233 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22234 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22235 NULL_TREE);
22236 parm_type = type_decays_to (parm_type);
22237 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22238 TREE_TYPE (decl_parm) = parm_type;
22239 attributes = DECL_ATTRIBUTES (pattern_parm);
22240 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22242 DECL_ATTRIBUTES (decl_parm) = attributes;
22243 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22245 decl_parm = DECL_CHAIN (decl_parm);
22246 pattern_parm = DECL_CHAIN (pattern_parm);
22248 /* Merge any parameters that match with the function parameter
22249 pack. */
22250 if (pattern_parm && DECL_PACK_P (pattern_parm))
22252 int i, len;
22253 tree expanded_types;
22254 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22255 the parameters in this function parameter pack. */
22256 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22257 args, tf_error, NULL_TREE);
22258 len = TREE_VEC_LENGTH (expanded_types);
22259 for (i = 0; i < len; i++)
22261 tree parm_type;
22262 tree attributes;
22264 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22265 /* Rename the parameter to include the index. */
22266 DECL_NAME (decl_parm) =
22267 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22268 parm_type = TREE_VEC_ELT (expanded_types, i);
22269 parm_type = type_decays_to (parm_type);
22270 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22271 TREE_TYPE (decl_parm) = parm_type;
22272 attributes = DECL_ATTRIBUTES (pattern_parm);
22273 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22275 DECL_ATTRIBUTES (decl_parm) = attributes;
22276 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22278 decl_parm = DECL_CHAIN (decl_parm);
22281 /* Merge additional specifiers from the CODE_PATTERN. */
22282 if (DECL_DECLARED_INLINE_P (code_pattern)
22283 && !DECL_DECLARED_INLINE_P (decl))
22284 DECL_DECLARED_INLINE_P (decl) = 1;
22286 else if (VAR_P (decl))
22288 DECL_INITIAL (decl) =
22289 tsubst_expr (DECL_INITIAL (code_pattern), args,
22290 tf_error, DECL_TI_TEMPLATE (decl),
22291 /*integral_constant_expression_p=*/false);
22292 if (VAR_HAD_UNKNOWN_BOUND (decl))
22293 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22294 tf_error, DECL_TI_TEMPLATE (decl));
22296 else
22297 gcc_unreachable ();
22299 pop_access_scope (decl);
22302 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22303 substituted to get DECL. */
22305 tree
22306 template_for_substitution (tree decl)
22308 tree tmpl = DECL_TI_TEMPLATE (decl);
22310 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22311 for the instantiation. This is not always the most general
22312 template. Consider, for example:
22314 template <class T>
22315 struct S { template <class U> void f();
22316 template <> void f<int>(); };
22318 and an instantiation of S<double>::f<int>. We want TD to be the
22319 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22320 while (/* An instantiation cannot have a definition, so we need a
22321 more general template. */
22322 DECL_TEMPLATE_INSTANTIATION (tmpl)
22323 /* We must also deal with friend templates. Given:
22325 template <class T> struct S {
22326 template <class U> friend void f() {};
22329 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22330 so far as the language is concerned, but that's still
22331 where we get the pattern for the instantiation from. On
22332 other hand, if the definition comes outside the class, say:
22334 template <class T> struct S {
22335 template <class U> friend void f();
22337 template <class U> friend void f() {}
22339 we don't need to look any further. That's what the check for
22340 DECL_INITIAL is for. */
22341 || (TREE_CODE (decl) == FUNCTION_DECL
22342 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22343 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22345 /* The present template, TD, should not be a definition. If it
22346 were a definition, we should be using it! Note that we
22347 cannot restructure the loop to just keep going until we find
22348 a template with a definition, since that might go too far if
22349 a specialization was declared, but not defined. */
22351 /* Fetch the more general template. */
22352 tmpl = DECL_TI_TEMPLATE (tmpl);
22355 return tmpl;
22358 /* Returns true if we need to instantiate this template instance even if we
22359 know we aren't going to emit it. */
22361 bool
22362 always_instantiate_p (tree decl)
22364 /* We always instantiate inline functions so that we can inline them. An
22365 explicit instantiation declaration prohibits implicit instantiation of
22366 non-inline functions. With high levels of optimization, we would
22367 normally inline non-inline functions -- but we're not allowed to do
22368 that for "extern template" functions. Therefore, we check
22369 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22370 return ((TREE_CODE (decl) == FUNCTION_DECL
22371 && (DECL_DECLARED_INLINE_P (decl)
22372 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22373 /* And we need to instantiate static data members so that
22374 their initializers are available in integral constant
22375 expressions. */
22376 || (VAR_P (decl)
22377 && decl_maybe_constant_var_p (decl)));
22380 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22381 instantiate it now, modifying TREE_TYPE (fn). */
22383 void
22384 maybe_instantiate_noexcept (tree fn)
22386 tree fntype, spec, noex, clone;
22388 /* Don't instantiate a noexcept-specification from template context. */
22389 if (processing_template_decl)
22390 return;
22392 if (DECL_CLONED_FUNCTION_P (fn))
22393 fn = DECL_CLONED_FUNCTION (fn);
22394 fntype = TREE_TYPE (fn);
22395 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22397 if (!spec || !TREE_PURPOSE (spec))
22398 return;
22400 noex = TREE_PURPOSE (spec);
22402 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22404 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22405 spec = get_defaulted_eh_spec (fn);
22406 else if (push_tinst_level (fn))
22408 push_access_scope (fn);
22409 push_deferring_access_checks (dk_no_deferred);
22410 input_location = DECL_SOURCE_LOCATION (fn);
22411 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22412 DEFERRED_NOEXCEPT_ARGS (noex),
22413 tf_warning_or_error, fn,
22414 /*function_p=*/false,
22415 /*integral_constant_expression_p=*/true);
22416 pop_deferring_access_checks ();
22417 pop_access_scope (fn);
22418 pop_tinst_level ();
22419 spec = build_noexcept_spec (noex, tf_warning_or_error);
22420 if (spec == error_mark_node)
22421 spec = noexcept_false_spec;
22423 else
22424 spec = noexcept_false_spec;
22426 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22429 FOR_EACH_CLONE (clone, fn)
22431 if (TREE_TYPE (clone) == fntype)
22432 TREE_TYPE (clone) = TREE_TYPE (fn);
22433 else
22434 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22438 /* Produce the definition of D, a _DECL generated from a template. If
22439 DEFER_OK is true, then we don't have to actually do the
22440 instantiation now; we just have to do it sometime. Normally it is
22441 an error if this is an explicit instantiation but D is undefined.
22442 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22443 instantiated class template. */
22445 tree
22446 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22448 tree tmpl = DECL_TI_TEMPLATE (d);
22449 tree gen_args;
22450 tree args;
22451 tree td;
22452 tree code_pattern;
22453 tree spec;
22454 tree gen_tmpl;
22455 bool pattern_defined;
22456 location_t saved_loc = input_location;
22457 int saved_unevaluated_operand = cp_unevaluated_operand;
22458 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22459 bool external_p;
22460 bool deleted_p;
22462 /* This function should only be used to instantiate templates for
22463 functions and static member variables. */
22464 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22466 /* A concept is never instantiated. */
22467 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22469 /* Variables are never deferred; if instantiation is required, they
22470 are instantiated right away. That allows for better code in the
22471 case that an expression refers to the value of the variable --
22472 if the variable has a constant value the referring expression can
22473 take advantage of that fact. */
22474 if (VAR_P (d))
22475 defer_ok = false;
22477 /* Don't instantiate cloned functions. Instead, instantiate the
22478 functions they cloned. */
22479 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22480 d = DECL_CLONED_FUNCTION (d);
22482 if (DECL_TEMPLATE_INSTANTIATED (d)
22483 || (TREE_CODE (d) == FUNCTION_DECL
22484 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22485 || DECL_TEMPLATE_SPECIALIZATION (d))
22486 /* D has already been instantiated or explicitly specialized, so
22487 there's nothing for us to do here.
22489 It might seem reasonable to check whether or not D is an explicit
22490 instantiation, and, if so, stop here. But when an explicit
22491 instantiation is deferred until the end of the compilation,
22492 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22493 the instantiation. */
22494 return d;
22496 /* Check to see whether we know that this template will be
22497 instantiated in some other file, as with "extern template"
22498 extension. */
22499 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22501 /* In general, we do not instantiate such templates. */
22502 if (external_p && !always_instantiate_p (d))
22503 return d;
22505 gen_tmpl = most_general_template (tmpl);
22506 gen_args = DECL_TI_ARGS (d);
22508 if (tmpl != gen_tmpl)
22509 /* We should already have the extra args. */
22510 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22511 == TMPL_ARGS_DEPTH (gen_args));
22512 /* And what's in the hash table should match D. */
22513 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22514 || spec == NULL_TREE);
22516 /* This needs to happen before any tsubsting. */
22517 if (! push_tinst_level (d))
22518 return d;
22520 timevar_push (TV_TEMPLATE_INST);
22522 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22523 for the instantiation. */
22524 td = template_for_substitution (d);
22525 args = gen_args;
22527 if (VAR_P (d))
22529 /* Look up an explicit specialization, if any. */
22530 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22531 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22532 if (elt && elt != error_mark_node)
22534 td = TREE_VALUE (elt);
22535 args = TREE_PURPOSE (elt);
22539 code_pattern = DECL_TEMPLATE_RESULT (td);
22541 /* We should never be trying to instantiate a member of a class
22542 template or partial specialization. */
22543 gcc_assert (d != code_pattern);
22545 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22546 || DECL_TEMPLATE_SPECIALIZATION (td))
22547 /* In the case of a friend template whose definition is provided
22548 outside the class, we may have too many arguments. Drop the
22549 ones we don't need. The same is true for specializations. */
22550 args = get_innermost_template_args
22551 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22553 if (TREE_CODE (d) == FUNCTION_DECL)
22555 deleted_p = DECL_DELETED_FN (code_pattern);
22556 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22557 && DECL_INITIAL (code_pattern) != error_mark_node)
22558 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22559 || deleted_p);
22561 else
22563 deleted_p = false;
22564 if (DECL_CLASS_SCOPE_P (code_pattern))
22565 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22566 || DECL_INLINE_VAR_P (code_pattern));
22567 else
22568 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22571 /* We may be in the middle of deferred access check. Disable it now. */
22572 push_deferring_access_checks (dk_no_deferred);
22574 /* Unless an explicit instantiation directive has already determined
22575 the linkage of D, remember that a definition is available for
22576 this entity. */
22577 if (pattern_defined
22578 && !DECL_INTERFACE_KNOWN (d)
22579 && !DECL_NOT_REALLY_EXTERN (d))
22580 mark_definable (d);
22582 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22583 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22584 input_location = DECL_SOURCE_LOCATION (d);
22586 /* If D is a member of an explicitly instantiated class template,
22587 and no definition is available, treat it like an implicit
22588 instantiation. */
22589 if (!pattern_defined && expl_inst_class_mem_p
22590 && DECL_EXPLICIT_INSTANTIATION (d))
22592 /* Leave linkage flags alone on instantiations with anonymous
22593 visibility. */
22594 if (TREE_PUBLIC (d))
22596 DECL_NOT_REALLY_EXTERN (d) = 0;
22597 DECL_INTERFACE_KNOWN (d) = 0;
22599 SET_DECL_IMPLICIT_INSTANTIATION (d);
22602 /* Defer all other templates, unless we have been explicitly
22603 forbidden from doing so. */
22604 if (/* If there is no definition, we cannot instantiate the
22605 template. */
22606 ! pattern_defined
22607 /* If it's OK to postpone instantiation, do so. */
22608 || defer_ok
22609 /* If this is a static data member that will be defined
22610 elsewhere, we don't want to instantiate the entire data
22611 member, but we do want to instantiate the initializer so that
22612 we can substitute that elsewhere. */
22613 || (external_p && VAR_P (d))
22614 /* Handle here a deleted function too, avoid generating
22615 its body (c++/61080). */
22616 || deleted_p)
22618 /* The definition of the static data member is now required so
22619 we must substitute the initializer. */
22620 if (VAR_P (d)
22621 && !DECL_INITIAL (d)
22622 && DECL_INITIAL (code_pattern))
22624 tree ns;
22625 tree init;
22626 bool const_init = false;
22627 bool enter_context = DECL_CLASS_SCOPE_P (d);
22629 ns = decl_namespace_context (d);
22630 push_nested_namespace (ns);
22631 if (enter_context)
22632 push_nested_class (DECL_CONTEXT (d));
22633 init = tsubst_expr (DECL_INITIAL (code_pattern),
22634 args,
22635 tf_warning_or_error, NULL_TREE,
22636 /*integral_constant_expression_p=*/false);
22637 /* If instantiating the initializer involved instantiating this
22638 again, don't call cp_finish_decl twice. */
22639 if (!DECL_INITIAL (d))
22641 /* Make sure the initializer is still constant, in case of
22642 circular dependency (template/instantiate6.C). */
22643 const_init
22644 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22645 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22646 /*asmspec_tree=*/NULL_TREE,
22647 LOOKUP_ONLYCONVERTING);
22649 if (enter_context)
22650 pop_nested_class ();
22651 pop_nested_namespace (ns);
22654 /* We restore the source position here because it's used by
22655 add_pending_template. */
22656 input_location = saved_loc;
22658 if (at_eof && !pattern_defined
22659 && DECL_EXPLICIT_INSTANTIATION (d)
22660 && DECL_NOT_REALLY_EXTERN (d))
22661 /* [temp.explicit]
22663 The definition of a non-exported function template, a
22664 non-exported member function template, or a non-exported
22665 member function or static data member of a class template
22666 shall be present in every translation unit in which it is
22667 explicitly instantiated. */
22668 permerror (input_location, "explicit instantiation of %qD "
22669 "but no definition available", d);
22671 /* If we're in unevaluated context, we just wanted to get the
22672 constant value; this isn't an odr use, so don't queue
22673 a full instantiation. */
22674 if (cp_unevaluated_operand != 0)
22675 goto out;
22676 /* ??? Historically, we have instantiated inline functions, even
22677 when marked as "extern template". */
22678 if (!(external_p && VAR_P (d)))
22679 add_pending_template (d);
22680 goto out;
22682 /* Tell the repository that D is available in this translation unit
22683 -- and see if it is supposed to be instantiated here. */
22684 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22686 /* In a PCH file, despite the fact that the repository hasn't
22687 requested instantiation in the PCH it is still possible that
22688 an instantiation will be required in a file that includes the
22689 PCH. */
22690 if (pch_file)
22691 add_pending_template (d);
22692 /* Instantiate inline functions so that the inliner can do its
22693 job, even though we'll not be emitting a copy of this
22694 function. */
22695 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22696 goto out;
22699 bool push_to_top, nested;
22700 tree fn_context;
22701 fn_context = decl_function_context (d);
22702 nested = current_function_decl != NULL_TREE;
22703 push_to_top = !(nested && fn_context == current_function_decl);
22705 vec<tree> omp_privatization_save;
22706 if (nested)
22707 save_omp_privatization_clauses (omp_privatization_save);
22709 if (push_to_top)
22710 push_to_top_level ();
22711 else
22713 push_function_context ();
22714 cp_unevaluated_operand = 0;
22715 c_inhibit_evaluation_warnings = 0;
22718 /* Mark D as instantiated so that recursive calls to
22719 instantiate_decl do not try to instantiate it again. */
22720 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22722 /* Regenerate the declaration in case the template has been modified
22723 by a subsequent redeclaration. */
22724 regenerate_decl_from_template (d, td, args);
22726 /* We already set the file and line above. Reset them now in case
22727 they changed as a result of calling regenerate_decl_from_template. */
22728 input_location = DECL_SOURCE_LOCATION (d);
22730 if (VAR_P (d))
22732 tree init;
22733 bool const_init = false;
22735 /* Clear out DECL_RTL; whatever was there before may not be right
22736 since we've reset the type of the declaration. */
22737 SET_DECL_RTL (d, NULL);
22738 DECL_IN_AGGR_P (d) = 0;
22740 /* The initializer is placed in DECL_INITIAL by
22741 regenerate_decl_from_template so we don't need to
22742 push/pop_access_scope again here. Pull it out so that
22743 cp_finish_decl can process it. */
22744 init = DECL_INITIAL (d);
22745 DECL_INITIAL (d) = NULL_TREE;
22746 DECL_INITIALIZED_P (d) = 0;
22748 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22749 initializer. That function will defer actual emission until
22750 we have a chance to determine linkage. */
22751 DECL_EXTERNAL (d) = 0;
22753 /* Enter the scope of D so that access-checking works correctly. */
22754 bool enter_context = DECL_CLASS_SCOPE_P (d);
22755 if (enter_context)
22756 push_nested_class (DECL_CONTEXT (d));
22758 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22759 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22761 if (enter_context)
22762 pop_nested_class ();
22764 if (variable_template_p (gen_tmpl))
22765 note_variable_template_instantiation (d);
22767 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22768 synthesize_method (d);
22769 else if (TREE_CODE (d) == FUNCTION_DECL)
22771 hash_map<tree, tree> *saved_local_specializations;
22772 tree tmpl_parm;
22773 tree spec_parm;
22774 tree block = NULL_TREE;
22775 tree lambda_ctx = NULL_TREE;
22777 /* Save away the current list, in case we are instantiating one
22778 template from within the body of another. */
22779 saved_local_specializations = local_specializations;
22781 /* Set up the list of local specializations. */
22782 local_specializations = new hash_map<tree, tree>;
22784 /* Set up context. */
22785 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22786 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22787 block = push_stmt_list ();
22788 else
22790 if (push_to_top && LAMBDA_FUNCTION_P (d))
22792 /* When instantiating a lambda's templated function
22793 operator, we need to push the non-lambda class scope
22794 of the lambda itself so that the nested function
22795 stack is sufficiently correct to deal with this
22796 capture. */
22797 lambda_ctx = DECL_CONTEXT (d);
22799 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22800 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22801 if (lambda_ctx)
22802 push_nested_class (lambda_ctx);
22804 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22807 /* Some typedefs referenced from within the template code need to be
22808 access checked at template instantiation time, i.e now. These
22809 types were added to the template at parsing time. Let's get those
22810 and perform the access checks then. */
22811 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22812 args);
22814 /* Create substitution entries for the parameters. */
22815 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22816 spec_parm = DECL_ARGUMENTS (d);
22817 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22819 register_local_specialization (spec_parm, tmpl_parm);
22820 spec_parm = skip_artificial_parms_for (d, spec_parm);
22821 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22823 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22825 if (!DECL_PACK_P (tmpl_parm))
22827 register_local_specialization (spec_parm, tmpl_parm);
22828 spec_parm = DECL_CHAIN (spec_parm);
22830 else
22832 /* Register the (value) argument pack as a specialization of
22833 TMPL_PARM, then move on. */
22834 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22835 register_local_specialization (argpack, tmpl_parm);
22838 gcc_assert (!spec_parm);
22840 /* Substitute into the body of the function. */
22841 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22842 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22843 tf_warning_or_error, tmpl);
22844 else
22846 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22847 tf_warning_or_error, tmpl,
22848 /*integral_constant_expression_p=*/false);
22850 /* Set the current input_location to the end of the function
22851 so that finish_function knows where we are. */
22852 input_location
22853 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22855 /* Remember if we saw an infinite loop in the template. */
22856 current_function_infinite_loop
22857 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22860 /* We don't need the local specializations any more. */
22861 delete local_specializations;
22862 local_specializations = saved_local_specializations;
22864 /* Finish the function. */
22865 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22866 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22867 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22868 else
22870 d = finish_function (0);
22871 expand_or_defer_fn (d);
22873 if (lambda_ctx)
22874 pop_nested_class ();
22876 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22877 cp_check_omp_declare_reduction (d);
22880 /* We're not deferring instantiation any more. */
22881 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22883 if (push_to_top)
22884 pop_from_top_level ();
22885 else
22886 pop_function_context ();
22888 if (nested)
22889 restore_omp_privatization_clauses (omp_privatization_save);
22891 out:
22892 pop_deferring_access_checks ();
22893 timevar_pop (TV_TEMPLATE_INST);
22894 pop_tinst_level ();
22895 input_location = saved_loc;
22896 cp_unevaluated_operand = saved_unevaluated_operand;
22897 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22899 return d;
22902 /* Run through the list of templates that we wish we could
22903 instantiate, and instantiate any we can. RETRIES is the
22904 number of times we retry pending template instantiation. */
22906 void
22907 instantiate_pending_templates (int retries)
22909 int reconsider;
22910 location_t saved_loc = input_location;
22912 /* Instantiating templates may trigger vtable generation. This in turn
22913 may require further template instantiations. We place a limit here
22914 to avoid infinite loop. */
22915 if (pending_templates && retries >= max_tinst_depth)
22917 tree decl = pending_templates->tinst->decl;
22919 fatal_error (input_location,
22920 "template instantiation depth exceeds maximum of %d"
22921 " instantiating %q+D, possibly from virtual table generation"
22922 " (use -ftemplate-depth= to increase the maximum)",
22923 max_tinst_depth, decl);
22924 if (TREE_CODE (decl) == FUNCTION_DECL)
22925 /* Pretend that we defined it. */
22926 DECL_INITIAL (decl) = error_mark_node;
22927 return;
22932 struct pending_template **t = &pending_templates;
22933 struct pending_template *last = NULL;
22934 reconsider = 0;
22935 while (*t)
22937 tree instantiation = reopen_tinst_level ((*t)->tinst);
22938 bool complete = false;
22940 if (TYPE_P (instantiation))
22942 tree fn;
22944 if (!COMPLETE_TYPE_P (instantiation))
22946 instantiate_class_template (instantiation);
22947 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22948 for (fn = TYPE_METHODS (instantiation);
22950 fn = TREE_CHAIN (fn))
22951 if (! DECL_ARTIFICIAL (fn))
22952 instantiate_decl (fn,
22953 /*defer_ok=*/false,
22954 /*expl_inst_class_mem_p=*/false);
22955 if (COMPLETE_TYPE_P (instantiation))
22956 reconsider = 1;
22959 complete = COMPLETE_TYPE_P (instantiation);
22961 else
22963 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
22964 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
22966 instantiation
22967 = instantiate_decl (instantiation,
22968 /*defer_ok=*/false,
22969 /*expl_inst_class_mem_p=*/false);
22970 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
22971 reconsider = 1;
22974 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
22975 || DECL_TEMPLATE_INSTANTIATED (instantiation));
22978 if (complete)
22979 /* If INSTANTIATION has been instantiated, then we don't
22980 need to consider it again in the future. */
22981 *t = (*t)->next;
22982 else
22984 last = *t;
22985 t = &(*t)->next;
22987 tinst_depth = 0;
22988 current_tinst_level = NULL;
22990 last_pending_template = last;
22992 while (reconsider);
22994 input_location = saved_loc;
22997 /* Substitute ARGVEC into T, which is a list of initializers for
22998 either base class or a non-static data member. The TREE_PURPOSEs
22999 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23000 instantiate_decl. */
23002 static tree
23003 tsubst_initializer_list (tree t, tree argvec)
23005 tree inits = NULL_TREE;
23007 for (; t; t = TREE_CHAIN (t))
23009 tree decl;
23010 tree init;
23011 tree expanded_bases = NULL_TREE;
23012 tree expanded_arguments = NULL_TREE;
23013 int i, len = 1;
23015 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23017 tree expr;
23018 tree arg;
23020 /* Expand the base class expansion type into separate base
23021 classes. */
23022 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23023 tf_warning_or_error,
23024 NULL_TREE);
23025 if (expanded_bases == error_mark_node)
23026 continue;
23028 /* We'll be building separate TREE_LISTs of arguments for
23029 each base. */
23030 len = TREE_VEC_LENGTH (expanded_bases);
23031 expanded_arguments = make_tree_vec (len);
23032 for (i = 0; i < len; i++)
23033 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23035 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23036 expand each argument in the TREE_VALUE of t. */
23037 expr = make_node (EXPR_PACK_EXPANSION);
23038 PACK_EXPANSION_LOCAL_P (expr) = true;
23039 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23040 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23042 if (TREE_VALUE (t) == void_type_node)
23043 /* VOID_TYPE_NODE is used to indicate
23044 value-initialization. */
23046 for (i = 0; i < len; i++)
23047 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23049 else
23051 /* Substitute parameter packs into each argument in the
23052 TREE_LIST. */
23053 in_base_initializer = 1;
23054 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23056 tree expanded_exprs;
23058 /* Expand the argument. */
23059 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23060 expanded_exprs
23061 = tsubst_pack_expansion (expr, argvec,
23062 tf_warning_or_error,
23063 NULL_TREE);
23064 if (expanded_exprs == error_mark_node)
23065 continue;
23067 /* Prepend each of the expanded expressions to the
23068 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23069 for (i = 0; i < len; i++)
23071 TREE_VEC_ELT (expanded_arguments, i) =
23072 tree_cons (NULL_TREE,
23073 TREE_VEC_ELT (expanded_exprs, i),
23074 TREE_VEC_ELT (expanded_arguments, i));
23077 in_base_initializer = 0;
23079 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23080 since we built them backwards. */
23081 for (i = 0; i < len; i++)
23083 TREE_VEC_ELT (expanded_arguments, i) =
23084 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23089 for (i = 0; i < len; ++i)
23091 if (expanded_bases)
23093 decl = TREE_VEC_ELT (expanded_bases, i);
23094 decl = expand_member_init (decl);
23095 init = TREE_VEC_ELT (expanded_arguments, i);
23097 else
23099 tree tmp;
23100 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23101 tf_warning_or_error, NULL_TREE);
23103 decl = expand_member_init (decl);
23104 if (decl && !DECL_P (decl))
23105 in_base_initializer = 1;
23107 init = TREE_VALUE (t);
23108 tmp = init;
23109 if (init != void_type_node)
23110 init = tsubst_expr (init, argvec,
23111 tf_warning_or_error, NULL_TREE,
23112 /*integral_constant_expression_p=*/false);
23113 if (init == NULL_TREE && tmp != NULL_TREE)
23114 /* If we had an initializer but it instantiated to nothing,
23115 value-initialize the object. This will only occur when
23116 the initializer was a pack expansion where the parameter
23117 packs used in that expansion were of length zero. */
23118 init = void_type_node;
23119 in_base_initializer = 0;
23122 if (decl)
23124 init = build_tree_list (decl, init);
23125 TREE_CHAIN (init) = inits;
23126 inits = init;
23130 return inits;
23133 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23135 static void
23136 set_current_access_from_decl (tree decl)
23138 if (TREE_PRIVATE (decl))
23139 current_access_specifier = access_private_node;
23140 else if (TREE_PROTECTED (decl))
23141 current_access_specifier = access_protected_node;
23142 else
23143 current_access_specifier = access_public_node;
23146 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23147 is the instantiation (which should have been created with
23148 start_enum) and ARGS are the template arguments to use. */
23150 static void
23151 tsubst_enum (tree tag, tree newtag, tree args)
23153 tree e;
23155 if (SCOPED_ENUM_P (newtag))
23156 begin_scope (sk_scoped_enum, newtag);
23158 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23160 tree value;
23161 tree decl;
23163 decl = TREE_VALUE (e);
23164 /* Note that in a template enum, the TREE_VALUE is the
23165 CONST_DECL, not the corresponding INTEGER_CST. */
23166 value = tsubst_expr (DECL_INITIAL (decl),
23167 args, tf_warning_or_error, NULL_TREE,
23168 /*integral_constant_expression_p=*/true);
23170 /* Give this enumeration constant the correct access. */
23171 set_current_access_from_decl (decl);
23173 /* Actually build the enumerator itself. Here we're assuming that
23174 enumerators can't have dependent attributes. */
23175 build_enumerator (DECL_NAME (decl), value, newtag,
23176 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23179 if (SCOPED_ENUM_P (newtag))
23180 finish_scope ();
23182 finish_enum_value_list (newtag);
23183 finish_enum (newtag);
23185 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23186 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23189 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23190 its type -- but without substituting the innermost set of template
23191 arguments. So, innermost set of template parameters will appear in
23192 the type. */
23194 tree
23195 get_mostly_instantiated_function_type (tree decl)
23197 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23198 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23201 /* Return truthvalue if we're processing a template different from
23202 the last one involved in diagnostics. */
23203 bool
23204 problematic_instantiation_changed (void)
23206 return current_tinst_level != last_error_tinst_level;
23209 /* Remember current template involved in diagnostics. */
23210 void
23211 record_last_problematic_instantiation (void)
23213 last_error_tinst_level = current_tinst_level;
23216 struct tinst_level *
23217 current_instantiation (void)
23219 return current_tinst_level;
23222 /* Return TRUE if current_function_decl is being instantiated, false
23223 otherwise. */
23225 bool
23226 instantiating_current_function_p (void)
23228 return (current_instantiation ()
23229 && current_instantiation ()->decl == current_function_decl);
23232 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23233 type. Return zero for ok, nonzero for disallowed. Issue error and
23234 warning messages under control of COMPLAIN. */
23236 static int
23237 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23239 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23240 return 0;
23241 else if (POINTER_TYPE_P (type))
23242 return 0;
23243 else if (TYPE_PTRMEM_P (type))
23244 return 0;
23245 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23246 return 0;
23247 else if (TREE_CODE (type) == TYPENAME_TYPE)
23248 return 0;
23249 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23250 return 0;
23251 else if (TREE_CODE (type) == NULLPTR_TYPE)
23252 return 0;
23253 /* A bound template template parm could later be instantiated to have a valid
23254 nontype parm type via an alias template. */
23255 else if (cxx_dialect >= cxx11
23256 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23257 return 0;
23259 if (complain & tf_error)
23261 if (type == error_mark_node)
23262 inform (input_location, "invalid template non-type parameter");
23263 else
23264 error ("%q#T is not a valid type for a template non-type parameter",
23265 type);
23267 return 1;
23270 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23271 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23273 static bool
23274 dependent_type_p_r (tree type)
23276 tree scope;
23278 /* [temp.dep.type]
23280 A type is dependent if it is:
23282 -- a template parameter. Template template parameters are types
23283 for us (since TYPE_P holds true for them) so we handle
23284 them here. */
23285 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23286 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23287 return true;
23288 /* -- a qualified-id with a nested-name-specifier which contains a
23289 class-name that names a dependent type or whose unqualified-id
23290 names a dependent type. */
23291 if (TREE_CODE (type) == TYPENAME_TYPE)
23292 return true;
23294 /* An alias template specialization can be dependent even if the
23295 resulting type is not. */
23296 if (dependent_alias_template_spec_p (type))
23297 return true;
23299 /* -- a cv-qualified type where the cv-unqualified type is
23300 dependent.
23301 No code is necessary for this bullet; the code below handles
23302 cv-qualified types, and we don't want to strip aliases with
23303 TYPE_MAIN_VARIANT because of DR 1558. */
23304 /* -- a compound type constructed from any dependent type. */
23305 if (TYPE_PTRMEM_P (type))
23306 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23307 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23308 (type)));
23309 else if (TYPE_PTR_P (type)
23310 || TREE_CODE (type) == REFERENCE_TYPE)
23311 return dependent_type_p (TREE_TYPE (type));
23312 else if (TREE_CODE (type) == FUNCTION_TYPE
23313 || TREE_CODE (type) == METHOD_TYPE)
23315 tree arg_type;
23317 if (dependent_type_p (TREE_TYPE (type)))
23318 return true;
23319 for (arg_type = TYPE_ARG_TYPES (type);
23320 arg_type;
23321 arg_type = TREE_CHAIN (arg_type))
23322 if (dependent_type_p (TREE_VALUE (arg_type)))
23323 return true;
23324 return false;
23326 /* -- an array type constructed from any dependent type or whose
23327 size is specified by a constant expression that is
23328 value-dependent.
23330 We checked for type- and value-dependence of the bounds in
23331 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23332 if (TREE_CODE (type) == ARRAY_TYPE)
23334 if (TYPE_DOMAIN (type)
23335 && dependent_type_p (TYPE_DOMAIN (type)))
23336 return true;
23337 return dependent_type_p (TREE_TYPE (type));
23340 /* -- a template-id in which either the template name is a template
23341 parameter ... */
23342 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23343 return true;
23344 /* ... or any of the template arguments is a dependent type or
23345 an expression that is type-dependent or value-dependent. */
23346 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23347 && (any_dependent_template_arguments_p
23348 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23349 return true;
23351 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23352 dependent; if the argument of the `typeof' expression is not
23353 type-dependent, then it should already been have resolved. */
23354 if (TREE_CODE (type) == TYPEOF_TYPE
23355 || TREE_CODE (type) == DECLTYPE_TYPE
23356 || TREE_CODE (type) == UNDERLYING_TYPE)
23357 return true;
23359 /* A template argument pack is dependent if any of its packed
23360 arguments are. */
23361 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23363 tree args = ARGUMENT_PACK_ARGS (type);
23364 int i, len = TREE_VEC_LENGTH (args);
23365 for (i = 0; i < len; ++i)
23366 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23367 return true;
23370 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23371 be template parameters. */
23372 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23373 return true;
23375 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23376 return true;
23378 /* The standard does not specifically mention types that are local
23379 to template functions or local classes, but they should be
23380 considered dependent too. For example:
23382 template <int I> void f() {
23383 enum E { a = I };
23384 S<sizeof (E)> s;
23387 The size of `E' cannot be known until the value of `I' has been
23388 determined. Therefore, `E' must be considered dependent. */
23389 scope = TYPE_CONTEXT (type);
23390 if (scope && TYPE_P (scope))
23391 return dependent_type_p (scope);
23392 /* Don't use type_dependent_expression_p here, as it can lead
23393 to infinite recursion trying to determine whether a lambda
23394 nested in a lambda is dependent (c++/47687). */
23395 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23396 && DECL_LANG_SPECIFIC (scope)
23397 && DECL_TEMPLATE_INFO (scope)
23398 && (any_dependent_template_arguments_p
23399 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23400 return true;
23402 /* Other types are non-dependent. */
23403 return false;
23406 /* Returns TRUE if TYPE is dependent, in the sense of
23407 [temp.dep.type]. Note that a NULL type is considered dependent. */
23409 bool
23410 dependent_type_p (tree type)
23412 /* If there are no template parameters in scope, then there can't be
23413 any dependent types. */
23414 if (!processing_template_decl)
23416 /* If we are not processing a template, then nobody should be
23417 providing us with a dependent type. */
23418 gcc_assert (type);
23419 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23420 return false;
23423 /* If the type is NULL, we have not computed a type for the entity
23424 in question; in that case, the type is dependent. */
23425 if (!type)
23426 return true;
23428 /* Erroneous types can be considered non-dependent. */
23429 if (type == error_mark_node)
23430 return false;
23432 /* If we have not already computed the appropriate value for TYPE,
23433 do so now. */
23434 if (!TYPE_DEPENDENT_P_VALID (type))
23436 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23437 TYPE_DEPENDENT_P_VALID (type) = 1;
23440 return TYPE_DEPENDENT_P (type);
23443 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23444 lookup. In other words, a dependent type that is not the current
23445 instantiation. */
23447 bool
23448 dependent_scope_p (tree scope)
23450 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23451 && !currently_open_class (scope));
23454 /* T is a SCOPE_REF; return whether we need to consider it
23455 instantiation-dependent so that we can check access at instantiation
23456 time even though we know which member it resolves to. */
23458 static bool
23459 instantiation_dependent_scope_ref_p (tree t)
23461 if (DECL_P (TREE_OPERAND (t, 1))
23462 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23463 && accessible_in_template_p (TREE_OPERAND (t, 0),
23464 TREE_OPERAND (t, 1)))
23465 return false;
23466 else
23467 return true;
23470 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23471 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23472 expression. */
23474 /* Note that this predicate is not appropriate for general expressions;
23475 only constant expressions (that satisfy potential_constant_expression)
23476 can be tested for value dependence. */
23478 bool
23479 value_dependent_expression_p (tree expression)
23481 if (!processing_template_decl || expression == NULL_TREE)
23482 return false;
23484 /* A name declared with a dependent type. */
23485 if (DECL_P (expression) && type_dependent_expression_p (expression))
23486 return true;
23488 switch (TREE_CODE (expression))
23490 case BASELINK:
23491 /* A dependent member function of the current instantiation. */
23492 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23494 case FUNCTION_DECL:
23495 /* A dependent member function of the current instantiation. */
23496 if (DECL_CLASS_SCOPE_P (expression)
23497 && dependent_type_p (DECL_CONTEXT (expression)))
23498 return true;
23499 break;
23501 case IDENTIFIER_NODE:
23502 /* A name that has not been looked up -- must be dependent. */
23503 return true;
23505 case TEMPLATE_PARM_INDEX:
23506 /* A non-type template parm. */
23507 return true;
23509 case CONST_DECL:
23510 /* A non-type template parm. */
23511 if (DECL_TEMPLATE_PARM_P (expression))
23512 return true;
23513 return value_dependent_expression_p (DECL_INITIAL (expression));
23515 case VAR_DECL:
23516 /* A constant with literal type and is initialized
23517 with an expression that is value-dependent.
23519 Note that a non-dependent parenthesized initializer will have
23520 already been replaced with its constant value, so if we see
23521 a TREE_LIST it must be dependent. */
23522 if (DECL_INITIAL (expression)
23523 && decl_constant_var_p (expression)
23524 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23525 /* cp_finish_decl doesn't fold reference initializers. */
23526 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23527 || type_dependent_expression_p (DECL_INITIAL (expression))
23528 || value_dependent_expression_p (DECL_INITIAL (expression))))
23529 return true;
23530 return false;
23532 case DYNAMIC_CAST_EXPR:
23533 case STATIC_CAST_EXPR:
23534 case CONST_CAST_EXPR:
23535 case REINTERPRET_CAST_EXPR:
23536 case CAST_EXPR:
23537 /* These expressions are value-dependent if the type to which
23538 the cast occurs is dependent or the expression being casted
23539 is value-dependent. */
23541 tree type = TREE_TYPE (expression);
23543 if (dependent_type_p (type))
23544 return true;
23546 /* A functional cast has a list of operands. */
23547 expression = TREE_OPERAND (expression, 0);
23548 if (!expression)
23550 /* If there are no operands, it must be an expression such
23551 as "int()". This should not happen for aggregate types
23552 because it would form non-constant expressions. */
23553 gcc_assert (cxx_dialect >= cxx11
23554 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23556 return false;
23559 if (TREE_CODE (expression) == TREE_LIST)
23560 return any_value_dependent_elements_p (expression);
23562 return value_dependent_expression_p (expression);
23565 case SIZEOF_EXPR:
23566 if (SIZEOF_EXPR_TYPE_P (expression))
23567 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23568 /* FALLTHRU */
23569 case ALIGNOF_EXPR:
23570 case TYPEID_EXPR:
23571 /* A `sizeof' expression is value-dependent if the operand is
23572 type-dependent or is a pack expansion. */
23573 expression = TREE_OPERAND (expression, 0);
23574 if (PACK_EXPANSION_P (expression))
23575 return true;
23576 else if (TYPE_P (expression))
23577 return dependent_type_p (expression);
23578 return instantiation_dependent_uneval_expression_p (expression);
23580 case AT_ENCODE_EXPR:
23581 /* An 'encode' expression is value-dependent if the operand is
23582 type-dependent. */
23583 expression = TREE_OPERAND (expression, 0);
23584 return dependent_type_p (expression);
23586 case NOEXCEPT_EXPR:
23587 expression = TREE_OPERAND (expression, 0);
23588 return instantiation_dependent_uneval_expression_p (expression);
23590 case SCOPE_REF:
23591 /* All instantiation-dependent expressions should also be considered
23592 value-dependent. */
23593 return instantiation_dependent_scope_ref_p (expression);
23595 case COMPONENT_REF:
23596 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23597 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23599 case NONTYPE_ARGUMENT_PACK:
23600 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23601 is value-dependent. */
23603 tree values = ARGUMENT_PACK_ARGS (expression);
23604 int i, len = TREE_VEC_LENGTH (values);
23606 for (i = 0; i < len; ++i)
23607 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23608 return true;
23610 return false;
23613 case TRAIT_EXPR:
23615 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23616 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23617 || (type2 ? dependent_type_p (type2) : false));
23620 case MODOP_EXPR:
23621 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23622 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23624 case ARRAY_REF:
23625 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23626 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23628 case ADDR_EXPR:
23630 tree op = TREE_OPERAND (expression, 0);
23631 return (value_dependent_expression_p (op)
23632 || has_value_dependent_address (op));
23635 case REQUIRES_EXPR:
23636 /* Treat all requires-expressions as value-dependent so
23637 we don't try to fold them. */
23638 return true;
23640 case TYPE_REQ:
23641 return dependent_type_p (TREE_OPERAND (expression, 0));
23643 case CALL_EXPR:
23645 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23646 return true;
23647 tree fn = get_callee_fndecl (expression);
23648 int i, nargs;
23649 nargs = call_expr_nargs (expression);
23650 for (i = 0; i < nargs; ++i)
23652 tree op = CALL_EXPR_ARG (expression, i);
23653 /* In a call to a constexpr member function, look through the
23654 implicit ADDR_EXPR on the object argument so that it doesn't
23655 cause the call to be considered value-dependent. We also
23656 look through it in potential_constant_expression. */
23657 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23658 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23659 && TREE_CODE (op) == ADDR_EXPR)
23660 op = TREE_OPERAND (op, 0);
23661 if (value_dependent_expression_p (op))
23662 return true;
23664 return false;
23667 case TEMPLATE_ID_EXPR:
23668 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23669 type-dependent. */
23670 return type_dependent_expression_p (expression)
23671 || variable_concept_p (TREE_OPERAND (expression, 0));
23673 case CONSTRUCTOR:
23675 unsigned ix;
23676 tree val;
23677 if (dependent_type_p (TREE_TYPE (expression)))
23678 return true;
23679 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23680 if (value_dependent_expression_p (val))
23681 return true;
23682 return false;
23685 case STMT_EXPR:
23686 /* Treat a GNU statement expression as dependent to avoid crashing
23687 under instantiate_non_dependent_expr; it can't be constant. */
23688 return true;
23690 default:
23691 /* A constant expression is value-dependent if any subexpression is
23692 value-dependent. */
23693 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23695 case tcc_reference:
23696 case tcc_unary:
23697 case tcc_comparison:
23698 case tcc_binary:
23699 case tcc_expression:
23700 case tcc_vl_exp:
23702 int i, len = cp_tree_operand_length (expression);
23704 for (i = 0; i < len; i++)
23706 tree t = TREE_OPERAND (expression, i);
23708 /* In some cases, some of the operands may be missing.l
23709 (For example, in the case of PREDECREMENT_EXPR, the
23710 amount to increment by may be missing.) That doesn't
23711 make the expression dependent. */
23712 if (t && value_dependent_expression_p (t))
23713 return true;
23716 break;
23717 default:
23718 break;
23720 break;
23723 /* The expression is not value-dependent. */
23724 return false;
23727 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23728 [temp.dep.expr]. Note that an expression with no type is
23729 considered dependent. Other parts of the compiler arrange for an
23730 expression with type-dependent subexpressions to have no type, so
23731 this function doesn't have to be fully recursive. */
23733 bool
23734 type_dependent_expression_p (tree expression)
23736 if (!processing_template_decl)
23737 return false;
23739 if (expression == NULL_TREE || expression == error_mark_node)
23740 return false;
23742 /* An unresolved name is always dependent. */
23743 if (identifier_p (expression)
23744 || TREE_CODE (expression) == USING_DECL
23745 || TREE_CODE (expression) == WILDCARD_DECL)
23746 return true;
23748 /* A fold expression is type-dependent. */
23749 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23750 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23751 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23752 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23753 return true;
23755 /* Some expression forms are never type-dependent. */
23756 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23757 || TREE_CODE (expression) == SIZEOF_EXPR
23758 || TREE_CODE (expression) == ALIGNOF_EXPR
23759 || TREE_CODE (expression) == AT_ENCODE_EXPR
23760 || TREE_CODE (expression) == NOEXCEPT_EXPR
23761 || TREE_CODE (expression) == TRAIT_EXPR
23762 || TREE_CODE (expression) == TYPEID_EXPR
23763 || TREE_CODE (expression) == DELETE_EXPR
23764 || TREE_CODE (expression) == VEC_DELETE_EXPR
23765 || TREE_CODE (expression) == THROW_EXPR
23766 || TREE_CODE (expression) == REQUIRES_EXPR)
23767 return false;
23769 /* The types of these expressions depends only on the type to which
23770 the cast occurs. */
23771 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23772 || TREE_CODE (expression) == STATIC_CAST_EXPR
23773 || TREE_CODE (expression) == CONST_CAST_EXPR
23774 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23775 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23776 || TREE_CODE (expression) == CAST_EXPR)
23777 return dependent_type_p (TREE_TYPE (expression));
23779 /* The types of these expressions depends only on the type created
23780 by the expression. */
23781 if (TREE_CODE (expression) == NEW_EXPR
23782 || TREE_CODE (expression) == VEC_NEW_EXPR)
23784 /* For NEW_EXPR tree nodes created inside a template, either
23785 the object type itself or a TREE_LIST may appear as the
23786 operand 1. */
23787 tree type = TREE_OPERAND (expression, 1);
23788 if (TREE_CODE (type) == TREE_LIST)
23789 /* This is an array type. We need to check array dimensions
23790 as well. */
23791 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23792 || value_dependent_expression_p
23793 (TREE_OPERAND (TREE_VALUE (type), 1));
23794 else
23795 return dependent_type_p (type);
23798 if (TREE_CODE (expression) == SCOPE_REF)
23800 tree scope = TREE_OPERAND (expression, 0);
23801 tree name = TREE_OPERAND (expression, 1);
23803 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23804 contains an identifier associated by name lookup with one or more
23805 declarations declared with a dependent type, or...a
23806 nested-name-specifier or qualified-id that names a member of an
23807 unknown specialization. */
23808 return (type_dependent_expression_p (name)
23809 || dependent_scope_p (scope));
23812 if (TREE_CODE (expression) == TEMPLATE_DECL
23813 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23814 return uses_outer_template_parms (expression);
23816 if (TREE_CODE (expression) == STMT_EXPR)
23817 expression = stmt_expr_value_expr (expression);
23819 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23821 tree elt;
23822 unsigned i;
23824 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23826 if (type_dependent_expression_p (elt))
23827 return true;
23829 return false;
23832 /* A static data member of the current instantiation with incomplete
23833 array type is type-dependent, as the definition and specializations
23834 can have different bounds. */
23835 if (VAR_P (expression)
23836 && DECL_CLASS_SCOPE_P (expression)
23837 && dependent_type_p (DECL_CONTEXT (expression))
23838 && VAR_HAD_UNKNOWN_BOUND (expression))
23839 return true;
23841 /* An array of unknown bound depending on a variadic parameter, eg:
23843 template<typename... Args>
23844 void foo (Args... args)
23846 int arr[] = { args... };
23849 template<int... vals>
23850 void bar ()
23852 int arr[] = { vals... };
23855 If the array has no length and has an initializer, it must be that
23856 we couldn't determine its length in cp_complete_array_type because
23857 it is dependent. */
23858 if (VAR_P (expression)
23859 && TREE_TYPE (expression) != NULL_TREE
23860 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23861 && !TYPE_DOMAIN (TREE_TYPE (expression))
23862 && DECL_INITIAL (expression))
23863 return true;
23865 /* A function or variable template-id is type-dependent if it has any
23866 dependent template arguments. Note that we only consider the innermost
23867 template arguments here, since those are the ones that come from the
23868 template-id; the template arguments for the enclosing class do not make it
23869 type-dependent, they only make a member function value-dependent. */
23870 if (VAR_OR_FUNCTION_DECL_P (expression)
23871 && DECL_LANG_SPECIFIC (expression)
23872 && DECL_TEMPLATE_INFO (expression)
23873 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23874 && (any_dependent_template_arguments_p
23875 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23876 return true;
23878 /* Always dependent, on the number of arguments if nothing else. */
23879 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23880 return true;
23882 if (TREE_TYPE (expression) == unknown_type_node)
23884 if (TREE_CODE (expression) == ADDR_EXPR)
23885 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23886 if (TREE_CODE (expression) == COMPONENT_REF
23887 || TREE_CODE (expression) == OFFSET_REF)
23889 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23890 return true;
23891 expression = TREE_OPERAND (expression, 1);
23892 if (identifier_p (expression))
23893 return false;
23895 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23896 if (TREE_CODE (expression) == SCOPE_REF)
23897 return false;
23899 if (BASELINK_P (expression))
23901 if (BASELINK_OPTYPE (expression)
23902 && dependent_type_p (BASELINK_OPTYPE (expression)))
23903 return true;
23904 expression = BASELINK_FUNCTIONS (expression);
23907 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23909 if (any_dependent_template_arguments_p
23910 (TREE_OPERAND (expression, 1)))
23911 return true;
23912 expression = TREE_OPERAND (expression, 0);
23913 if (identifier_p (expression))
23914 return true;
23917 gcc_assert (TREE_CODE (expression) == OVERLOAD
23918 || TREE_CODE (expression) == FUNCTION_DECL);
23920 while (expression)
23922 if (type_dependent_expression_p (OVL_CURRENT (expression)))
23923 return true;
23924 expression = OVL_NEXT (expression);
23926 return false;
23929 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23931 /* Dependent type attributes might not have made it from the decl to
23932 the type yet. */
23933 if (DECL_P (expression)
23934 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
23935 return true;
23937 return (dependent_type_p (TREE_TYPE (expression)));
23940 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23941 type-dependent if the expression refers to a member of the current
23942 instantiation and the type of the referenced member is dependent, or the
23943 class member access expression refers to a member of an unknown
23944 specialization.
23946 This function returns true if the OBJECT in such a class member access
23947 expression is of an unknown specialization. */
23949 bool
23950 type_dependent_object_expression_p (tree object)
23952 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
23953 dependent. */
23954 if (TREE_CODE (object) == IDENTIFIER_NODE)
23955 return true;
23956 tree scope = TREE_TYPE (object);
23957 return (!scope || dependent_scope_p (scope));
23960 /* walk_tree callback function for instantiation_dependent_expression_p,
23961 below. Returns non-zero if a dependent subexpression is found. */
23963 static tree
23964 instantiation_dependent_r (tree *tp, int *walk_subtrees,
23965 void * /*data*/)
23967 if (TYPE_P (*tp))
23969 /* We don't have to worry about decltype currently because decltype
23970 of an instantiation-dependent expr is a dependent type. This
23971 might change depending on the resolution of DR 1172. */
23972 *walk_subtrees = false;
23973 return NULL_TREE;
23975 enum tree_code code = TREE_CODE (*tp);
23976 switch (code)
23978 /* Don't treat an argument list as dependent just because it has no
23979 TREE_TYPE. */
23980 case TREE_LIST:
23981 case TREE_VEC:
23982 return NULL_TREE;
23984 case TEMPLATE_PARM_INDEX:
23985 return *tp;
23987 /* Handle expressions with type operands. */
23988 case SIZEOF_EXPR:
23989 case ALIGNOF_EXPR:
23990 case TYPEID_EXPR:
23991 case AT_ENCODE_EXPR:
23993 tree op = TREE_OPERAND (*tp, 0);
23994 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
23995 op = TREE_TYPE (op);
23996 if (TYPE_P (op))
23998 if (dependent_type_p (op))
23999 return *tp;
24000 else
24002 *walk_subtrees = false;
24003 return NULL_TREE;
24006 break;
24009 case COMPONENT_REF:
24010 if (identifier_p (TREE_OPERAND (*tp, 1)))
24011 /* In a template, finish_class_member_access_expr creates a
24012 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24013 type-dependent, so that we can check access control at
24014 instantiation time (PR 42277). See also Core issue 1273. */
24015 return *tp;
24016 break;
24018 case SCOPE_REF:
24019 if (instantiation_dependent_scope_ref_p (*tp))
24020 return *tp;
24021 else
24022 break;
24024 /* Treat statement-expressions as dependent. */
24025 case BIND_EXPR:
24026 return *tp;
24028 /* Treat requires-expressions as dependent. */
24029 case REQUIRES_EXPR:
24030 return *tp;
24032 case CALL_EXPR:
24033 /* Treat calls to function concepts as dependent. */
24034 if (function_concept_check_p (*tp))
24035 return *tp;
24036 break;
24038 case TEMPLATE_ID_EXPR:
24039 /* And variable concepts. */
24040 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24041 return *tp;
24042 break;
24044 default:
24045 break;
24048 if (type_dependent_expression_p (*tp))
24049 return *tp;
24050 else
24051 return NULL_TREE;
24054 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24055 sense defined by the ABI:
24057 "An expression is instantiation-dependent if it is type-dependent
24058 or value-dependent, or it has a subexpression that is type-dependent
24059 or value-dependent."
24061 Except don't actually check value-dependence for unevaluated expressions,
24062 because in sizeof(i) we don't care about the value of i. Checking
24063 type-dependence will in turn check value-dependence of array bounds/template
24064 arguments as needed. */
24066 bool
24067 instantiation_dependent_uneval_expression_p (tree expression)
24069 tree result;
24071 if (!processing_template_decl)
24072 return false;
24074 if (expression == error_mark_node)
24075 return false;
24077 result = cp_walk_tree_without_duplicates (&expression,
24078 instantiation_dependent_r, NULL);
24079 return result != NULL_TREE;
24082 /* As above, but also check value-dependence of the expression as a whole. */
24084 bool
24085 instantiation_dependent_expression_p (tree expression)
24087 return (instantiation_dependent_uneval_expression_p (expression)
24088 || value_dependent_expression_p (expression));
24091 /* Like type_dependent_expression_p, but it also works while not processing
24092 a template definition, i.e. during substitution or mangling. */
24094 bool
24095 type_dependent_expression_p_push (tree expr)
24097 bool b;
24098 ++processing_template_decl;
24099 b = type_dependent_expression_p (expr);
24100 --processing_template_decl;
24101 return b;
24104 /* Returns TRUE if ARGS contains a type-dependent expression. */
24106 bool
24107 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24109 unsigned int i;
24110 tree arg;
24112 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24114 if (type_dependent_expression_p (arg))
24115 return true;
24117 return false;
24120 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24121 expressions) contains any type-dependent expressions. */
24123 bool
24124 any_type_dependent_elements_p (const_tree list)
24126 for (; list; list = TREE_CHAIN (list))
24127 if (type_dependent_expression_p (TREE_VALUE (list)))
24128 return true;
24130 return false;
24133 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24134 expressions) contains any value-dependent expressions. */
24136 bool
24137 any_value_dependent_elements_p (const_tree list)
24139 for (; list; list = TREE_CHAIN (list))
24140 if (value_dependent_expression_p (TREE_VALUE (list)))
24141 return true;
24143 return false;
24146 /* Returns TRUE if the ARG (a template argument) is dependent. */
24148 bool
24149 dependent_template_arg_p (tree arg)
24151 if (!processing_template_decl)
24152 return false;
24154 /* Assume a template argument that was wrongly written by the user
24155 is dependent. This is consistent with what
24156 any_dependent_template_arguments_p [that calls this function]
24157 does. */
24158 if (!arg || arg == error_mark_node)
24159 return true;
24161 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24162 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24164 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24165 return true;
24166 if (TREE_CODE (arg) == TEMPLATE_DECL)
24168 if (DECL_TEMPLATE_PARM_P (arg))
24169 return true;
24170 /* A member template of a dependent class is not necessarily
24171 type-dependent, but it is a dependent template argument because it
24172 will be a member of an unknown specialization to that template. */
24173 tree scope = CP_DECL_CONTEXT (arg);
24174 return TYPE_P (scope) && dependent_type_p (scope);
24176 else if (ARGUMENT_PACK_P (arg))
24178 tree args = ARGUMENT_PACK_ARGS (arg);
24179 int i, len = TREE_VEC_LENGTH (args);
24180 for (i = 0; i < len; ++i)
24182 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24183 return true;
24186 return false;
24188 else if (TYPE_P (arg))
24189 return dependent_type_p (arg);
24190 else
24191 return (type_dependent_expression_p (arg)
24192 || value_dependent_expression_p (arg));
24195 /* Returns true if ARGS (a collection of template arguments) contains
24196 any types that require structural equality testing. */
24198 bool
24199 any_template_arguments_need_structural_equality_p (tree args)
24201 int i;
24202 int j;
24204 if (!args)
24205 return false;
24206 if (args == error_mark_node)
24207 return true;
24209 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24211 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24212 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24214 tree arg = TREE_VEC_ELT (level, j);
24215 tree packed_args = NULL_TREE;
24216 int k, len = 1;
24218 if (ARGUMENT_PACK_P (arg))
24220 /* Look inside the argument pack. */
24221 packed_args = ARGUMENT_PACK_ARGS (arg);
24222 len = TREE_VEC_LENGTH (packed_args);
24225 for (k = 0; k < len; ++k)
24227 if (packed_args)
24228 arg = TREE_VEC_ELT (packed_args, k);
24230 if (error_operand_p (arg))
24231 return true;
24232 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24233 continue;
24234 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24235 return true;
24236 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24237 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24238 return true;
24243 return false;
24246 /* Returns true if ARGS (a collection of template arguments) contains
24247 any dependent arguments. */
24249 bool
24250 any_dependent_template_arguments_p (const_tree args)
24252 int i;
24253 int j;
24255 if (!args)
24256 return false;
24257 if (args == error_mark_node)
24258 return true;
24260 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24262 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24263 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24264 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24265 return true;
24268 return false;
24271 /* Returns TRUE if the template TMPL is type-dependent. */
24273 bool
24274 dependent_template_p (tree tmpl)
24276 if (TREE_CODE (tmpl) == OVERLOAD)
24278 while (tmpl)
24280 if (dependent_template_p (OVL_CURRENT (tmpl)))
24281 return true;
24282 tmpl = OVL_NEXT (tmpl);
24284 return false;
24287 /* Template template parameters are dependent. */
24288 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24289 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24290 return true;
24291 /* So are names that have not been looked up. */
24292 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24293 return true;
24294 return false;
24297 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24299 bool
24300 dependent_template_id_p (tree tmpl, tree args)
24302 return (dependent_template_p (tmpl)
24303 || any_dependent_template_arguments_p (args));
24306 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24307 are dependent. */
24309 bool
24310 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24312 int i;
24314 if (!processing_template_decl)
24315 return false;
24317 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24319 tree decl = TREE_VEC_ELT (declv, i);
24320 tree init = TREE_VEC_ELT (initv, i);
24321 tree cond = TREE_VEC_ELT (condv, i);
24322 tree incr = TREE_VEC_ELT (incrv, i);
24324 if (type_dependent_expression_p (decl)
24325 || TREE_CODE (decl) == SCOPE_REF)
24326 return true;
24328 if (init && type_dependent_expression_p (init))
24329 return true;
24331 if (type_dependent_expression_p (cond))
24332 return true;
24334 if (COMPARISON_CLASS_P (cond)
24335 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24336 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24337 return true;
24339 if (TREE_CODE (incr) == MODOP_EXPR)
24341 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24342 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24343 return true;
24345 else if (type_dependent_expression_p (incr))
24346 return true;
24347 else if (TREE_CODE (incr) == MODIFY_EXPR)
24349 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24350 return true;
24351 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24353 tree t = TREE_OPERAND (incr, 1);
24354 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24355 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24356 return true;
24361 return false;
24364 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24365 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24366 no such TYPE can be found. Note that this function peers inside
24367 uninstantiated templates and therefore should be used only in
24368 extremely limited situations. ONLY_CURRENT_P restricts this
24369 peering to the currently open classes hierarchy (which is required
24370 when comparing types). */
24372 tree
24373 resolve_typename_type (tree type, bool only_current_p)
24375 tree scope;
24376 tree name;
24377 tree decl;
24378 int quals;
24379 tree pushed_scope;
24380 tree result;
24382 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24384 scope = TYPE_CONTEXT (type);
24385 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24386 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24387 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24388 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24389 identifier of the TYPENAME_TYPE anymore.
24390 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24391 TYPENAME_TYPE instead, we avoid messing up with a possible
24392 typedef variant case. */
24393 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24395 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24396 it first before we can figure out what NAME refers to. */
24397 if (TREE_CODE (scope) == TYPENAME_TYPE)
24399 if (TYPENAME_IS_RESOLVING_P (scope))
24400 /* Given a class template A with a dependent base with nested type C,
24401 typedef typename A::C::C C will land us here, as trying to resolve
24402 the initial A::C leads to the local C typedef, which leads back to
24403 A::C::C. So we break the recursion now. */
24404 return type;
24405 else
24406 scope = resolve_typename_type (scope, only_current_p);
24408 /* If we don't know what SCOPE refers to, then we cannot resolve the
24409 TYPENAME_TYPE. */
24410 if (!CLASS_TYPE_P (scope))
24411 return type;
24412 /* If this is a typedef, we don't want to look inside (c++/11987). */
24413 if (typedef_variant_p (type))
24414 return type;
24415 /* If SCOPE isn't the template itself, it will not have a valid
24416 TYPE_FIELDS list. */
24417 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24418 /* scope is either the template itself or a compatible instantiation
24419 like X<T>, so look up the name in the original template. */
24420 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24421 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24422 gcc_checking_assert (uses_template_parms (scope));
24423 /* If scope has no fields, it can't be a current instantiation. Check this
24424 before currently_open_class to avoid infinite recursion (71515). */
24425 if (!TYPE_FIELDS (scope))
24426 return type;
24427 /* If the SCOPE is not the current instantiation, there's no reason
24428 to look inside it. */
24429 if (only_current_p && !currently_open_class (scope))
24430 return type;
24431 /* Enter the SCOPE so that name lookup will be resolved as if we
24432 were in the class definition. In particular, SCOPE will no
24433 longer be considered a dependent type. */
24434 pushed_scope = push_scope (scope);
24435 /* Look up the declaration. */
24436 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24437 tf_warning_or_error);
24439 result = NULL_TREE;
24441 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24442 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24443 if (!decl)
24444 /*nop*/;
24445 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24446 && TREE_CODE (decl) == TYPE_DECL)
24448 result = TREE_TYPE (decl);
24449 if (result == error_mark_node)
24450 result = NULL_TREE;
24452 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24453 && DECL_CLASS_TEMPLATE_P (decl))
24455 tree tmpl;
24456 tree args;
24457 /* Obtain the template and the arguments. */
24458 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24459 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24460 /* Instantiate the template. */
24461 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24462 /*entering_scope=*/0,
24463 tf_error | tf_user);
24464 if (result == error_mark_node)
24465 result = NULL_TREE;
24468 /* Leave the SCOPE. */
24469 if (pushed_scope)
24470 pop_scope (pushed_scope);
24472 /* If we failed to resolve it, return the original typename. */
24473 if (!result)
24474 return type;
24476 /* If lookup found a typename type, resolve that too. */
24477 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24479 /* Ill-formed programs can cause infinite recursion here, so we
24480 must catch that. */
24481 TYPENAME_IS_RESOLVING_P (result) = 1;
24482 result = resolve_typename_type (result, only_current_p);
24483 TYPENAME_IS_RESOLVING_P (result) = 0;
24486 /* Qualify the resulting type. */
24487 quals = cp_type_quals (type);
24488 if (quals)
24489 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24491 return result;
24494 /* EXPR is an expression which is not type-dependent. Return a proxy
24495 for EXPR that can be used to compute the types of larger
24496 expressions containing EXPR. */
24498 tree
24499 build_non_dependent_expr (tree expr)
24501 tree inner_expr;
24503 /* When checking, try to get a constant value for all non-dependent
24504 expressions in order to expose bugs in *_dependent_expression_p
24505 and constexpr. This can affect code generation, see PR70704, so
24506 only do this for -fchecking=2. */
24507 if (flag_checking > 1
24508 && cxx_dialect >= cxx11
24509 /* Don't do this during nsdmi parsing as it can lead to
24510 unexpected recursive instantiations. */
24511 && !parsing_nsdmi ()
24512 /* Don't do this during concept expansion either and for
24513 the same reason. */
24514 && !expanding_concept ())
24515 fold_non_dependent_expr (expr);
24517 /* Preserve OVERLOADs; the functions must be available to resolve
24518 types. */
24519 inner_expr = expr;
24520 if (TREE_CODE (inner_expr) == STMT_EXPR)
24521 inner_expr = stmt_expr_value_expr (inner_expr);
24522 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24523 inner_expr = TREE_OPERAND (inner_expr, 0);
24524 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24525 inner_expr = TREE_OPERAND (inner_expr, 1);
24526 if (is_overloaded_fn (inner_expr)
24527 || TREE_CODE (inner_expr) == OFFSET_REF)
24528 return expr;
24529 /* There is no need to return a proxy for a variable. */
24530 if (VAR_P (expr))
24531 return expr;
24532 /* Preserve string constants; conversions from string constants to
24533 "char *" are allowed, even though normally a "const char *"
24534 cannot be used to initialize a "char *". */
24535 if (TREE_CODE (expr) == STRING_CST)
24536 return expr;
24537 /* Preserve void and arithmetic constants, as an optimization -- there is no
24538 reason to create a new node. */
24539 if (TREE_CODE (expr) == VOID_CST
24540 || TREE_CODE (expr) == INTEGER_CST
24541 || TREE_CODE (expr) == REAL_CST)
24542 return expr;
24543 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24544 There is at least one place where we want to know that a
24545 particular expression is a throw-expression: when checking a ?:
24546 expression, there are special rules if the second or third
24547 argument is a throw-expression. */
24548 if (TREE_CODE (expr) == THROW_EXPR)
24549 return expr;
24551 /* Don't wrap an initializer list, we need to be able to look inside. */
24552 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24553 return expr;
24555 /* Don't wrap a dummy object, we need to be able to test for it. */
24556 if (is_dummy_object (expr))
24557 return expr;
24559 if (TREE_CODE (expr) == COND_EXPR)
24560 return build3 (COND_EXPR,
24561 TREE_TYPE (expr),
24562 TREE_OPERAND (expr, 0),
24563 (TREE_OPERAND (expr, 1)
24564 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24565 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24566 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24567 if (TREE_CODE (expr) == COMPOUND_EXPR
24568 && !COMPOUND_EXPR_OVERLOADED (expr))
24569 return build2 (COMPOUND_EXPR,
24570 TREE_TYPE (expr),
24571 TREE_OPERAND (expr, 0),
24572 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24574 /* If the type is unknown, it can't really be non-dependent */
24575 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24577 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24578 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24581 /* ARGS is a vector of expressions as arguments to a function call.
24582 Replace the arguments with equivalent non-dependent expressions.
24583 This modifies ARGS in place. */
24585 void
24586 make_args_non_dependent (vec<tree, va_gc> *args)
24588 unsigned int ix;
24589 tree arg;
24591 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24593 tree newarg = build_non_dependent_expr (arg);
24594 if (newarg != arg)
24595 (*args)[ix] = newarg;
24599 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24600 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24601 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24603 static tree
24604 make_auto_1 (tree name, bool set_canonical)
24606 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24607 TYPE_NAME (au) = build_decl (input_location,
24608 TYPE_DECL, name, au);
24609 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24610 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24611 (0, processing_template_decl + 1, processing_template_decl + 1,
24612 TYPE_NAME (au), NULL_TREE);
24613 if (set_canonical)
24614 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24615 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24616 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24618 return au;
24621 tree
24622 make_decltype_auto (void)
24624 return make_auto_1 (decltype_auto_identifier, true);
24627 tree
24628 make_auto (void)
24630 return make_auto_1 (auto_identifier, true);
24633 /* Return a C++17 deduction placeholder for class template TMPL. */
24635 tree
24636 make_template_placeholder (tree tmpl)
24638 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24639 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24640 return t;
24643 /* Make a "constrained auto" type-specifier. This is an
24644 auto type with constraints that must be associated after
24645 deduction. The constraint is formed from the given
24646 CONC and its optional sequence of arguments, which are
24647 non-null if written as partial-concept-id. */
24649 tree
24650 make_constrained_auto (tree con, tree args)
24652 tree type = make_auto_1 (auto_identifier, false);
24654 /* Build the constraint. */
24655 tree tmpl = DECL_TI_TEMPLATE (con);
24656 tree expr;
24657 if (VAR_P (con))
24658 expr = build_concept_check (tmpl, type, args);
24659 else
24660 expr = build_concept_check (build_overload (tmpl, NULL_TREE), type, args);
24662 tree constr = normalize_expression (expr);
24663 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24665 /* Our canonical type depends on the constraint. */
24666 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24668 /* Attach the constraint to the type declaration. */
24669 tree decl = TYPE_NAME (type);
24670 return decl;
24673 /* Given type ARG, return std::initializer_list<ARG>. */
24675 static tree
24676 listify (tree arg)
24678 tree std_init_list = namespace_binding
24679 (get_identifier ("initializer_list"), std_node);
24680 tree argvec;
24681 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24683 error ("deducing from brace-enclosed initializer list requires "
24684 "#include <initializer_list>");
24685 return error_mark_node;
24687 argvec = make_tree_vec (1);
24688 TREE_VEC_ELT (argvec, 0) = arg;
24689 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24690 NULL_TREE, 0, tf_warning_or_error);
24693 /* Replace auto in TYPE with std::initializer_list<auto>. */
24695 static tree
24696 listify_autos (tree type, tree auto_node)
24698 tree init_auto = listify (auto_node);
24699 tree argvec = make_tree_vec (1);
24700 TREE_VEC_ELT (argvec, 0) = init_auto;
24701 if (processing_template_decl)
24702 argvec = add_to_template_args (current_template_args (), argvec);
24703 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24706 /* Hash traits for hashing possibly constrained 'auto'
24707 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24709 struct auto_hash : default_hash_traits<tree>
24711 static inline hashval_t hash (tree);
24712 static inline bool equal (tree, tree);
24715 /* Hash the 'auto' T. */
24717 inline hashval_t
24718 auto_hash::hash (tree t)
24720 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24721 /* Matching constrained-type-specifiers denote the same template
24722 parameter, so hash the constraint. */
24723 return hash_placeholder_constraint (c);
24724 else
24725 /* But unconstrained autos are all separate, so just hash the pointer. */
24726 return iterative_hash_object (t, 0);
24729 /* Compare two 'auto's. */
24731 inline bool
24732 auto_hash::equal (tree t1, tree t2)
24734 if (t1 == t2)
24735 return true;
24737 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24738 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24740 /* Two unconstrained autos are distinct. */
24741 if (!c1 || !c2)
24742 return false;
24744 return equivalent_placeholder_constraints (c1, c2);
24747 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24748 constrained) auto, add it to the vector. */
24750 static int
24751 extract_autos_r (tree t, void *data)
24753 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24754 if (is_auto_or_concept (t))
24756 /* All the autos were built with index 0; fix that up now. */
24757 tree *p = hash.find_slot (t, INSERT);
24758 unsigned idx;
24759 if (*p)
24760 /* If this is a repeated constrained-type-specifier, use the index we
24761 chose before. */
24762 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24763 else
24765 /* Otherwise this is new, so use the current count. */
24766 *p = t;
24767 idx = hash.elements () - 1;
24769 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24772 /* Always keep walking. */
24773 return 0;
24776 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24777 says they can appear anywhere in the type. */
24779 static tree
24780 extract_autos (tree type)
24782 hash_set<tree> visited;
24783 hash_table<auto_hash> hash (2);
24785 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24787 tree tree_vec = make_tree_vec (hash.elements());
24788 for (hash_table<auto_hash>::iterator iter = hash.begin();
24789 iter != hash.end(); ++iter)
24791 tree elt = *iter;
24792 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24793 TREE_VEC_ELT (tree_vec, i)
24794 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24797 return tree_vec;
24800 /* The stem for deduction guide names. */
24801 const char *const dguide_base = "__dguide_";
24803 /* Return the name for a deduction guide for class template TMPL. */
24805 tree
24806 dguide_name (tree tmpl)
24808 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24809 tree tname = TYPE_IDENTIFIER (type);
24810 char *buf = (char *) alloca (1 + strlen (dguide_base)
24811 + IDENTIFIER_LENGTH (tname));
24812 memcpy (buf, dguide_base, strlen (dguide_base));
24813 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24814 IDENTIFIER_LENGTH (tname) + 1);
24815 tree dname = get_identifier (buf);
24816 TREE_TYPE (dname) = type;
24817 return dname;
24820 /* True if NAME is the name of a deduction guide. */
24822 bool
24823 dguide_name_p (tree name)
24825 return (TREE_TYPE (name)
24826 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24827 strlen (dguide_base)));
24830 /* True if FN is a deduction guide. */
24832 bool
24833 deduction_guide_p (const_tree fn)
24835 if (DECL_P (fn))
24836 if (tree name = DECL_NAME (fn))
24837 return dguide_name_p (name);
24838 return false;
24841 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24842 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24843 template parameter types. Note that the handling of template template
24844 parameters relies on current_template_parms being set appropriately for the
24845 new template. */
24847 static tree
24848 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
24849 tree tsubst_args, tsubst_flags_t complain)
24851 tree oldidx = get_template_parm_index (olddecl);
24853 tree newtype;
24854 if (TREE_CODE (olddecl) == TYPE_DECL
24855 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24857 tree oldtype = TREE_TYPE (olddecl);
24858 newtype = cxx_make_type (TREE_CODE (oldtype));
24859 TYPE_MAIN_VARIANT (newtype) = newtype;
24860 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
24861 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
24862 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
24864 else
24865 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
24866 complain, NULL_TREE);
24868 tree newdecl
24869 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
24870 DECL_NAME (olddecl), newtype);
24871 SET_DECL_TEMPLATE_PARM_P (newdecl);
24873 tree newidx;
24874 if (TREE_CODE (olddecl) == TYPE_DECL
24875 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24877 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
24878 = build_template_parm_index (index, level, level,
24879 newdecl, newtype);
24880 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
24881 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
24883 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
24885 DECL_TEMPLATE_RESULT (newdecl)
24886 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
24887 DECL_NAME (olddecl), newtype);
24888 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
24889 // First create a copy (ttargs) of tsubst_args with an
24890 // additional level for the template template parameter's own
24891 // template parameters (ttparms).
24892 tree ttparms = (INNERMOST_TEMPLATE_PARMS
24893 (DECL_TEMPLATE_PARMS (olddecl)));
24894 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
24895 tree ttargs = make_tree_vec (depth + 1);
24896 for (int i = 0; i < depth; ++i)
24897 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
24898 TREE_VEC_ELT (ttargs, depth)
24899 = template_parms_level_to_args (ttparms);
24900 // Substitute ttargs into ttparms to fix references to
24901 // other template parameters.
24902 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24903 complain);
24904 // Now substitute again with args based on tparms, to reduce
24905 // the level of the ttparms.
24906 ttargs = current_template_args ();
24907 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24908 complain);
24909 // Finally, tack the adjusted parms onto tparms.
24910 ttparms = tree_cons (size_int (depth), ttparms,
24911 current_template_parms);
24912 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
24915 else
24917 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
24918 tree newconst
24919 = build_decl (DECL_SOURCE_LOCATION (oldconst),
24920 TREE_CODE (oldconst),
24921 DECL_NAME (oldconst), newtype);
24922 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
24923 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
24924 SET_DECL_TEMPLATE_PARM_P (newconst);
24925 newidx = build_template_parm_index (index, level, level,
24926 newconst, newtype);
24927 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
24930 TEMPLATE_PARM_PARAMETER_PACK (newidx)
24931 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
24932 return newdecl;
24935 /* Returns a C++17 class deduction guide template based on the constructor
24936 CTOR. */
24938 static tree
24939 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
24941 if (outer_args)
24942 ctor = tsubst (ctor, outer_args, complain, ctor);
24943 tree type = DECL_CONTEXT (ctor);
24944 tree fn_tmpl;
24945 if (TREE_CODE (ctor) == TEMPLATE_DECL)
24947 fn_tmpl = ctor;
24948 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
24950 else
24951 fn_tmpl = DECL_TI_TEMPLATE (ctor);
24953 tree tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
24954 /* If type is a member class template, DECL_TI_ARGS (ctor) will have fully
24955 specialized args for the enclosing class. Strip those off, as the
24956 deduction guide won't have those template parameters. */
24957 tree targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
24958 TMPL_PARMS_DEPTH (tparms));
24959 /* Discard the 'this' parameter. */
24960 tree fparms = FUNCTION_ARG_CHAIN (ctor);
24961 tree fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
24962 tree ci = get_constraints (ctor);
24964 if (PRIMARY_TEMPLATE_P (fn_tmpl))
24966 /* For a member template constructor, we need to flatten the two template
24967 parameter lists into one, and then adjust the function signature
24968 accordingly. This gets...complicated. */
24969 ++processing_template_decl;
24970 tree save_parms = current_template_parms;
24972 /* For a member template we should have two levels of parms/args, one for
24973 the class and one for the constructor. We stripped specialized args
24974 for further enclosing classes above. */
24975 const int depth = 2;
24976 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
24978 /* Template args for translating references to the two-level template
24979 parameters into references to the one-level template parameters we are
24980 creating. */
24981 tree tsubst_args = copy_node (targs);
24982 TMPL_ARGS_LEVEL (tsubst_args, depth)
24983 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
24985 /* Template parms for the constructor template. */
24986 tree ftparms = TREE_VALUE (tparms);
24987 unsigned flen = TREE_VEC_LENGTH (ftparms);
24988 /* Template parms for the class template. */
24989 tparms = TREE_CHAIN (tparms);
24990 tree ctparms = TREE_VALUE (tparms);
24991 unsigned clen = TREE_VEC_LENGTH (ctparms);
24992 /* Template parms for the deduction guide start as a copy of the template
24993 parms for the class. We set current_template_parms for
24994 lookup_template_class_1. */
24995 current_template_parms = tparms = copy_node (tparms);
24996 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
24997 for (unsigned i = 0; i < clen; ++i)
24998 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25000 /* Now we need to rewrite the constructor parms to append them to the
25001 class parms. */
25002 for (unsigned i = 0; i < flen; ++i)
25004 unsigned index = i + clen;
25005 unsigned level = 1;
25006 tree oldelt = TREE_VEC_ELT (ftparms, i);
25007 tree olddecl = TREE_VALUE (oldelt);
25008 tree newdecl = rewrite_template_parm (olddecl, index, level,
25009 tsubst_args, complain);
25010 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25011 tsubst_args, complain, ctor);
25012 tree list = build_tree_list (newdef, newdecl);
25013 TEMPLATE_PARM_CONSTRAINTS (list)
25014 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25015 tsubst_args, complain, ctor);
25016 TREE_VEC_ELT (new_vec, index) = list;
25017 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25020 /* Now we have a final set of template parms to substitute into the
25021 function signature. */
25022 targs = template_parms_to_args (tparms);
25023 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25024 complain, ctor);
25025 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25026 if (ci)
25027 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25029 current_template_parms = save_parms;
25030 --processing_template_decl;
25032 else
25034 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25035 tparms = copy_node (tparms);
25036 INNERMOST_TEMPLATE_PARMS (tparms)
25037 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25040 tree fntype = build_function_type (type, fparms);
25041 tree ded_fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (ctor),
25042 FUNCTION_DECL,
25043 dguide_name (type), fntype);
25044 DECL_ARGUMENTS (ded_fn) = fargs;
25045 DECL_ARTIFICIAL (ded_fn) = true;
25046 DECL_NONCONVERTING_P (ded_fn) = DECL_NONCONVERTING_P (ctor);
25047 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25048 DECL_ARTIFICIAL (ded_tmpl) = true;
25049 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25050 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25051 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25052 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25053 if (ci)
25054 set_constraints (ded_tmpl, ci);
25056 return ded_tmpl;
25059 /* Deduce template arguments for the class template placeholder PTYPE for
25060 template TMPL based on the initializer INIT, and return the resulting
25061 type. */
25063 static tree
25064 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25065 tsubst_flags_t complain)
25067 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25069 /* We should have handled this in the caller. */
25070 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25071 return ptype;
25072 if (complain & tf_error)
25073 error ("non-class template %qT used without template arguments", tmpl);
25074 return error_mark_node;
25077 tree type = TREE_TYPE (tmpl);
25079 vec<tree,va_gc> *args;
25080 if (TREE_CODE (init) == TREE_LIST)
25081 args = make_tree_vector_from_list (init);
25082 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25083 args = make_tree_vector_from_ctor (init);
25084 else
25085 args = make_tree_vector_single (init);
25087 if (args->length() == 1)
25089 /* First try to deduce directly, since we don't have implicitly-declared
25090 constructors yet. */
25091 tree parms = build_tree_list (NULL_TREE, type);
25092 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25093 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25094 int err = type_unification_real (tparms, targs, parms, &(*args)[0],
25095 1, /*subr*/false, DEDUCE_CALL,
25096 LOOKUP_NORMAL, NULL, /*explain*/false);
25097 if (err == 0)
25098 return tsubst (type, targs, complain, tmpl);
25101 tree dname = dguide_name (tmpl);
25102 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25103 /*type*/false, /*complain*/false,
25104 /*hidden*/false);
25105 if (cands == error_mark_node)
25106 cands = NULL_TREE;
25108 tree outer_args = NULL_TREE;
25109 if (DECL_CLASS_SCOPE_P (tmpl)
25110 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25112 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25113 type = TREE_TYPE (most_general_template (tmpl));
25116 if (CLASSTYPE_METHOD_VEC (type))
25117 // FIXME cache artificial deduction guides
25118 for (tree fns = CLASSTYPE_CONSTRUCTORS (type); fns; fns = OVL_NEXT (fns))
25120 tree fn = OVL_CURRENT (fns);
25121 tree guide = build_deduction_guide (fn, outer_args, complain);
25122 cands = ovl_cons (guide, cands);
25125 if (cands == NULL_TREE)
25127 error ("cannot deduce template arguments for %qT, as it has "
25128 "no deduction guides or user-declared constructors", type);
25129 return error_mark_node;
25132 /* Prune explicit deduction guides in copy-initialization context. */
25133 tree old_cands = cands;
25134 if (flags & LOOKUP_ONLYCONVERTING)
25136 tree t = cands;
25137 for (; t; t = OVL_NEXT (t))
25138 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (OVL_CURRENT (t))))
25139 break;
25140 if (t)
25142 tree pruned = NULL_TREE;
25143 for (t = cands; t; t = OVL_NEXT (t))
25145 tree f = OVL_CURRENT (t);
25146 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (f)))
25147 pruned = build_overload (f, pruned);
25149 cands = pruned;
25150 if (cands == NULL_TREE)
25152 error ("cannot deduce template arguments for copy-initialization"
25153 " of %qT, as it has no non-explicit deduction guides or "
25154 "user-declared constructors", type);
25155 return error_mark_node;
25160 ++cp_unevaluated_operand;
25161 tree t = build_new_function_call (cands, &args, /*koenig*/false,
25162 tf_decltype);
25164 if (t == error_mark_node && (complain & tf_warning_or_error))
25166 error ("class template argument deduction failed:");
25167 t = build_new_function_call (cands, &args, /*koenig*/false,
25168 complain | tf_decltype);
25169 if (old_cands != cands)
25170 inform (input_location, "explicit deduction guides not considered "
25171 "for copy-initialization");
25174 --cp_unevaluated_operand;
25175 release_tree_vector (args);
25177 return TREE_TYPE (t);
25180 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25181 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25183 tree
25184 do_auto_deduction (tree type, tree init, tree auto_node)
25186 return do_auto_deduction (type, init, auto_node,
25187 tf_warning_or_error,
25188 adc_unspecified);
25191 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25192 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25193 The CONTEXT determines the context in which auto deduction is performed
25194 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25195 OUTER_TARGS are used during template argument deduction
25196 (context == adc_unify) to properly substitute the result, and is ignored
25197 in other contexts.
25199 For partial-concept-ids, extra args may be appended to the list of deduced
25200 template arguments prior to determining constraint satisfaction. */
25202 tree
25203 do_auto_deduction (tree type, tree init, tree auto_node,
25204 tsubst_flags_t complain, auto_deduction_context context,
25205 tree outer_targs, int flags)
25207 tree targs;
25209 if (init == error_mark_node)
25210 return error_mark_node;
25212 if (type_dependent_expression_p (init)
25213 && context != adc_unify)
25214 /* Defining a subset of type-dependent expressions that we can deduce
25215 from ahead of time isn't worth the trouble. */
25216 return type;
25218 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25219 /* C++17 class template argument deduction. */
25220 return do_class_deduction (type, tmpl, init, flags, complain);
25222 if (TREE_TYPE (init) == NULL_TREE)
25223 /* Nothing we can do with this, even in deduction context. */
25224 return type;
25226 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25227 with either a new invented type template parameter U or, if the
25228 initializer is a braced-init-list (8.5.4), with
25229 std::initializer_list<U>. */
25230 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25232 if (!DIRECT_LIST_INIT_P (init))
25233 type = listify_autos (type, auto_node);
25234 else if (CONSTRUCTOR_NELTS (init) == 1)
25235 init = CONSTRUCTOR_ELT (init, 0)->value;
25236 else
25238 if (complain & tf_warning_or_error)
25240 if (permerror (input_location, "direct-list-initialization of "
25241 "%<auto%> requires exactly one element"))
25242 inform (input_location,
25243 "for deduction to %<std::initializer_list%>, use copy-"
25244 "list-initialization (i.e. add %<=%> before the %<{%>)");
25246 type = listify_autos (type, auto_node);
25250 if (type == error_mark_node)
25251 return error_mark_node;
25253 init = resolve_nondeduced_context (init, complain);
25255 if (context == adc_decomp_type
25256 && auto_node == type
25257 && init != error_mark_node
25258 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25259 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25260 and initializer has array type, deduce cv-qualified array type. */
25261 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25262 complain);
25263 else if (AUTO_IS_DECLTYPE (auto_node))
25265 bool id = (DECL_P (init)
25266 || ((TREE_CODE (init) == COMPONENT_REF
25267 || TREE_CODE (init) == SCOPE_REF)
25268 && !REF_PARENTHESIZED_P (init)));
25269 targs = make_tree_vec (1);
25270 TREE_VEC_ELT (targs, 0)
25271 = finish_decltype_type (init, id, tf_warning_or_error);
25272 if (type != auto_node)
25274 if (complain & tf_error)
25275 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25276 return error_mark_node;
25279 else
25281 tree parms = build_tree_list (NULL_TREE, type);
25282 tree tparms;
25284 if (flag_concepts)
25285 tparms = extract_autos (type);
25286 else
25288 tparms = make_tree_vec (1);
25289 TREE_VEC_ELT (tparms, 0)
25290 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25293 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25294 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25295 DEDUCE_CALL, LOOKUP_NORMAL,
25296 NULL, /*explain_p=*/false);
25297 if (val > 0)
25299 if (processing_template_decl)
25300 /* Try again at instantiation time. */
25301 return type;
25302 if (type && type != error_mark_node
25303 && (complain & tf_error))
25304 /* If type is error_mark_node a diagnostic must have been
25305 emitted by now. Also, having a mention to '<type error>'
25306 in the diagnostic is not really useful to the user. */
25308 if (cfun && auto_node == current_function_auto_return_pattern
25309 && LAMBDA_FUNCTION_P (current_function_decl))
25310 error ("unable to deduce lambda return type from %qE", init);
25311 else
25312 error ("unable to deduce %qT from %qE", type, init);
25313 type_unification_real (tparms, targs, parms, &init, 1, 0,
25314 DEDUCE_CALL, LOOKUP_NORMAL,
25315 NULL, /*explain_p=*/true);
25317 return error_mark_node;
25321 /* Check any placeholder constraints against the deduced type. */
25322 if (flag_concepts && !processing_template_decl)
25323 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25325 /* Use the deduced type to check the associated constraints. If we
25326 have a partial-concept-id, rebuild the argument list so that
25327 we check using the extra arguments. */
25328 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25329 tree cargs = CHECK_CONSTR_ARGS (constr);
25330 if (TREE_VEC_LENGTH (cargs) > 1)
25332 cargs = copy_node (cargs);
25333 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25335 else
25336 cargs = targs;
25337 if (!constraints_satisfied_p (constr, cargs))
25339 if (complain & tf_warning_or_error)
25341 switch (context)
25343 case adc_unspecified:
25344 case adc_unify:
25345 error("placeholder constraints not satisfied");
25346 break;
25347 case adc_variable_type:
25348 case adc_decomp_type:
25349 error ("deduced initializer does not satisfy "
25350 "placeholder constraints");
25351 break;
25352 case adc_return_type:
25353 error ("deduced return type does not satisfy "
25354 "placeholder constraints");
25355 break;
25356 case adc_requirement:
25357 error ("deduced expression type does not satisfy "
25358 "placeholder constraints");
25359 break;
25361 diagnose_constraints (input_location, constr, targs);
25363 return error_mark_node;
25367 if (processing_template_decl && context != adc_unify)
25368 outer_targs = current_template_args ();
25369 targs = add_to_template_args (outer_targs, targs);
25370 return tsubst (type, targs, complain, NULL_TREE);
25373 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25374 result. */
25376 tree
25377 splice_late_return_type (tree type, tree late_return_type)
25379 if (is_auto (type))
25381 if (late_return_type)
25382 return late_return_type;
25384 tree idx = get_template_parm_index (type);
25385 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25386 /* In an abbreviated function template we didn't know we were dealing
25387 with a function template when we saw the auto return type, so update
25388 it to have the correct level. */
25389 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25391 return type;
25394 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25395 'decltype(auto)' or a deduced class template. */
25397 bool
25398 is_auto (const_tree type)
25400 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25401 && (TYPE_IDENTIFIER (type) == auto_identifier
25402 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25403 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25404 return true;
25405 else
25406 return false;
25409 /* for_each_template_parm callback for type_uses_auto. */
25412 is_auto_r (tree tp, void */*data*/)
25414 return is_auto_or_concept (tp);
25417 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25418 a use of `auto'. Returns NULL_TREE otherwise. */
25420 tree
25421 type_uses_auto (tree type)
25423 if (type == NULL_TREE)
25424 return NULL_TREE;
25425 else if (flag_concepts)
25427 /* The Concepts TS allows multiple autos in one type-specifier; just
25428 return the first one we find, do_auto_deduction will collect all of
25429 them. */
25430 if (uses_template_parms (type))
25431 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25432 /*visited*/NULL, /*nondeduced*/true);
25433 else
25434 return NULL_TREE;
25436 else
25437 return find_type_usage (type, is_auto);
25440 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto',
25441 'decltype(auto)' or a concept. */
25443 bool
25444 is_auto_or_concept (const_tree type)
25446 return is_auto (type); // or concept
25449 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing a generic type (`auto' or
25450 a concept identifier) iff TYPE contains a use of a generic type. Returns
25451 NULL_TREE otherwise. */
25453 tree
25454 type_uses_auto_or_concept (tree type)
25456 return find_type_usage (type, is_auto_or_concept);
25460 /* For a given template T, return the vector of typedefs referenced
25461 in T for which access check is needed at T instantiation time.
25462 T is either a FUNCTION_DECL or a RECORD_TYPE.
25463 Those typedefs were added to T by the function
25464 append_type_to_template_for_access_check. */
25466 vec<qualified_typedef_usage_t, va_gc> *
25467 get_types_needing_access_check (tree t)
25469 tree ti;
25470 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25472 if (!t || t == error_mark_node)
25473 return NULL;
25475 if (!(ti = get_template_info (t)))
25476 return NULL;
25478 if (CLASS_TYPE_P (t)
25479 || TREE_CODE (t) == FUNCTION_DECL)
25481 if (!TI_TEMPLATE (ti))
25482 return NULL;
25484 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25487 return result;
25490 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25491 tied to T. That list of typedefs will be access checked at
25492 T instantiation time.
25493 T is either a FUNCTION_DECL or a RECORD_TYPE.
25494 TYPE_DECL is a TYPE_DECL node representing a typedef.
25495 SCOPE is the scope through which TYPE_DECL is accessed.
25496 LOCATION is the location of the usage point of TYPE_DECL.
25498 This function is a subroutine of
25499 append_type_to_template_for_access_check. */
25501 static void
25502 append_type_to_template_for_access_check_1 (tree t,
25503 tree type_decl,
25504 tree scope,
25505 location_t location)
25507 qualified_typedef_usage_t typedef_usage;
25508 tree ti;
25510 if (!t || t == error_mark_node)
25511 return;
25513 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25514 || CLASS_TYPE_P (t))
25515 && type_decl
25516 && TREE_CODE (type_decl) == TYPE_DECL
25517 && scope);
25519 if (!(ti = get_template_info (t)))
25520 return;
25522 gcc_assert (TI_TEMPLATE (ti));
25524 typedef_usage.typedef_decl = type_decl;
25525 typedef_usage.context = scope;
25526 typedef_usage.locus = location;
25528 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25531 /* Append TYPE_DECL to the template TEMPL.
25532 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25533 At TEMPL instanciation time, TYPE_DECL will be checked to see
25534 if it can be accessed through SCOPE.
25535 LOCATION is the location of the usage point of TYPE_DECL.
25537 e.g. consider the following code snippet:
25539 class C
25541 typedef int myint;
25544 template<class U> struct S
25546 C::myint mi; // <-- usage point of the typedef C::myint
25549 S<char> s;
25551 At S<char> instantiation time, we need to check the access of C::myint
25552 In other words, we need to check the access of the myint typedef through
25553 the C scope. For that purpose, this function will add the myint typedef
25554 and the scope C through which its being accessed to a list of typedefs
25555 tied to the template S. That list will be walked at template instantiation
25556 time and access check performed on each typedefs it contains.
25557 Note that this particular code snippet should yield an error because
25558 myint is private to C. */
25560 void
25561 append_type_to_template_for_access_check (tree templ,
25562 tree type_decl,
25563 tree scope,
25564 location_t location)
25566 qualified_typedef_usage_t *iter;
25567 unsigned i;
25569 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25571 /* Make sure we don't append the type to the template twice. */
25572 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25573 if (iter->typedef_decl == type_decl && scope == iter->context)
25574 return;
25576 append_type_to_template_for_access_check_1 (templ, type_decl,
25577 scope, location);
25580 /* Convert the generic type parameters in PARM that match the types given in the
25581 range [START_IDX, END_IDX) from the current_template_parms into generic type
25582 packs. */
25584 tree
25585 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25587 tree current = current_template_parms;
25588 int depth = TMPL_PARMS_DEPTH (current);
25589 current = INNERMOST_TEMPLATE_PARMS (current);
25590 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25592 for (int i = 0; i < start_idx; ++i)
25593 TREE_VEC_ELT (replacement, i)
25594 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25596 for (int i = start_idx; i < end_idx; ++i)
25598 /* Create a distinct parameter pack type from the current parm and add it
25599 to the replacement args to tsubst below into the generic function
25600 parameter. */
25602 tree o = TREE_TYPE (TREE_VALUE
25603 (TREE_VEC_ELT (current, i)));
25604 tree t = copy_type (o);
25605 TEMPLATE_TYPE_PARM_INDEX (t)
25606 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25607 o, 0, 0, tf_none);
25608 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25609 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25610 TYPE_MAIN_VARIANT (t) = t;
25611 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25612 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25613 TREE_VEC_ELT (replacement, i) = t;
25614 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25617 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25618 TREE_VEC_ELT (replacement, i)
25619 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25621 /* If there are more levels then build up the replacement with the outer
25622 template parms. */
25623 if (depth > 1)
25624 replacement = add_to_template_args (template_parms_to_args
25625 (TREE_CHAIN (current_template_parms)),
25626 replacement);
25628 return tsubst (parm, replacement, tf_none, NULL_TREE);
25631 /* Entries in the decl_constraint hash table. */
25632 struct GTY((for_user)) constr_entry
25634 tree decl;
25635 tree ci;
25638 /* Hashing function and equality for constraint entries. */
25639 struct constr_hasher : ggc_ptr_hash<constr_entry>
25641 static hashval_t hash (constr_entry *e)
25643 return (hashval_t)DECL_UID (e->decl);
25646 static bool equal (constr_entry *e1, constr_entry *e2)
25648 return e1->decl == e2->decl;
25652 /* A mapping from declarations to constraint information. Note that
25653 both templates and their underlying declarations are mapped to the
25654 same constraint information.
25656 FIXME: This is defined in pt.c because garbage collection
25657 code is not being generated for constraint.cc. */
25659 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25661 /* Returns the template constraints of declaration T. If T is not
25662 constrained, return NULL_TREE. Note that T must be non-null. */
25664 tree
25665 get_constraints (tree t)
25667 if (!flag_concepts)
25668 return NULL_TREE;
25670 gcc_assert (DECL_P (t));
25671 if (TREE_CODE (t) == TEMPLATE_DECL)
25672 t = DECL_TEMPLATE_RESULT (t);
25673 constr_entry elt = { t, NULL_TREE };
25674 constr_entry* found = decl_constraints->find (&elt);
25675 if (found)
25676 return found->ci;
25677 else
25678 return NULL_TREE;
25681 /* Associate the given constraint information CI with the declaration
25682 T. If T is a template, then the constraints are associated with
25683 its underlying declaration. Don't build associations if CI is
25684 NULL_TREE. */
25686 void
25687 set_constraints (tree t, tree ci)
25689 if (!ci)
25690 return;
25691 gcc_assert (t && flag_concepts);
25692 if (TREE_CODE (t) == TEMPLATE_DECL)
25693 t = DECL_TEMPLATE_RESULT (t);
25694 gcc_assert (!get_constraints (t));
25695 constr_entry elt = {t, ci};
25696 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25697 constr_entry* entry = ggc_alloc<constr_entry> ();
25698 *entry = elt;
25699 *slot = entry;
25702 /* Remove the associated constraints of the declaration T. */
25704 void
25705 remove_constraints (tree t)
25707 gcc_assert (DECL_P (t));
25708 if (TREE_CODE (t) == TEMPLATE_DECL)
25709 t = DECL_TEMPLATE_RESULT (t);
25711 constr_entry elt = {t, NULL_TREE};
25712 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25713 if (slot)
25714 decl_constraints->clear_slot (slot);
25717 /* Memoized satisfaction results for declarations. This
25718 maps the pair (constraint_info, arguments) to the result computed
25719 by constraints_satisfied_p. */
25721 struct GTY((for_user)) constraint_sat_entry
25723 tree ci;
25724 tree args;
25725 tree result;
25728 /* Hashing function and equality for constraint entries. */
25730 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25732 static hashval_t hash (constraint_sat_entry *e)
25734 hashval_t val = iterative_hash_object(e->ci, 0);
25735 return iterative_hash_template_arg (e->args, val);
25738 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25740 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25744 /* Memoized satisfaction results for concept checks. */
25746 struct GTY((for_user)) concept_spec_entry
25748 tree tmpl;
25749 tree args;
25750 tree result;
25753 /* Hashing function and equality for constraint entries. */
25755 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25757 static hashval_t hash (concept_spec_entry *e)
25759 return hash_tmpl_and_args (e->tmpl, e->args);
25762 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25764 ++comparing_specializations;
25765 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25766 --comparing_specializations;
25767 return eq;
25771 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25772 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25774 /* Search for a memoized satisfaction result. Returns one of the
25775 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25777 tree
25778 lookup_constraint_satisfaction (tree ci, tree args)
25780 constraint_sat_entry elt = { ci, args, NULL_TREE };
25781 constraint_sat_entry* found = constraint_memos->find (&elt);
25782 if (found)
25783 return found->result;
25784 else
25785 return NULL_TREE;
25788 /* Memoize the result of a satisfication test. Returns the saved result. */
25790 tree
25791 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25793 constraint_sat_entry elt = {ci, args, result};
25794 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25795 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25796 *entry = elt;
25797 *slot = entry;
25798 return result;
25801 /* Search for a memoized satisfaction result for a concept. */
25803 tree
25804 lookup_concept_satisfaction (tree tmpl, tree args)
25806 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25807 concept_spec_entry* found = concept_memos->find (&elt);
25808 if (found)
25809 return found->result;
25810 else
25811 return NULL_TREE;
25814 /* Memoize the result of a concept check. Returns the saved result. */
25816 tree
25817 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
25819 concept_spec_entry elt = {tmpl, args, result};
25820 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
25821 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25822 *entry = elt;
25823 *slot = entry;
25824 return result;
25827 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
25829 /* Returns a prior concept specialization. This returns the substituted
25830 and normalized constraints defined by the concept. */
25832 tree
25833 get_concept_expansion (tree tmpl, tree args)
25835 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25836 concept_spec_entry* found = concept_expansions->find (&elt);
25837 if (found)
25838 return found->result;
25839 else
25840 return NULL_TREE;
25843 /* Save a concept expansion for later. */
25845 tree
25846 save_concept_expansion (tree tmpl, tree args, tree def)
25848 concept_spec_entry elt = {tmpl, args, def};
25849 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
25850 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25851 *entry = elt;
25852 *slot = entry;
25853 return def;
25856 static hashval_t
25857 hash_subsumption_args (tree t1, tree t2)
25859 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
25860 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
25861 int val = 0;
25862 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
25863 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
25864 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
25865 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
25866 return val;
25869 /* Compare the constraints of two subsumption entries. The LEFT1 and
25870 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
25871 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
25873 static bool
25874 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
25876 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
25877 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
25878 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
25879 CHECK_CONSTR_ARGS (right1)))
25880 return comp_template_args (CHECK_CONSTR_ARGS (left2),
25881 CHECK_CONSTR_ARGS (right2));
25882 return false;
25885 /* Key/value pair for learning and memoizing subsumption results. This
25886 associates a pair of check constraints (including arguments) with
25887 a boolean value indicating the result. */
25889 struct GTY((for_user)) subsumption_entry
25891 tree t1;
25892 tree t2;
25893 bool result;
25896 /* Hashing function and equality for constraint entries. */
25898 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
25900 static hashval_t hash (subsumption_entry *e)
25902 return hash_subsumption_args (e->t1, e->t2);
25905 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
25907 ++comparing_specializations;
25908 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
25909 --comparing_specializations;
25910 return eq;
25914 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
25916 /* Search for a previously cached subsumption result. */
25918 bool*
25919 lookup_subsumption_result (tree t1, tree t2)
25921 subsumption_entry elt = { t1, t2, false };
25922 subsumption_entry* found = subsumption_table->find (&elt);
25923 if (found)
25924 return &found->result;
25925 else
25926 return 0;
25929 /* Save a subsumption result. */
25931 bool
25932 save_subsumption_result (tree t1, tree t2, bool result)
25934 subsumption_entry elt = {t1, t2, result};
25935 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
25936 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
25937 *entry = elt;
25938 *slot = entry;
25939 return result;
25942 /* Set up the hash table for constraint association. */
25944 void
25945 init_constraint_processing (void)
25947 if (!flag_concepts)
25948 return;
25950 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
25951 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
25952 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
25953 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
25954 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
25957 /* Set up the hash tables for template instantiations. */
25959 void
25960 init_template_processing (void)
25962 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
25963 type_specializations = hash_table<spec_hasher>::create_ggc (37);
25966 /* Print stats about the template hash tables for -fstats. */
25968 void
25969 print_template_statistics (void)
25971 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
25972 "%f collisions\n", (long) decl_specializations->size (),
25973 (long) decl_specializations->elements (),
25974 decl_specializations->collisions ());
25975 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
25976 "%f collisions\n", (long) type_specializations->size (),
25977 (long) type_specializations->elements (),
25978 type_specializations->collisions ());
25981 #include "gt-cp-pt.h"