* cp-tree.h (OVL_ARG_DEPENDENT): Delete.
[official-gcc.git] / gcc / cp / pt.c
blobeef6c0e72dc00c2f0b43251bdaaf2f21501903b8
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 (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
788 /* Same or enclosing namespace. */
789 return true;
790 else
792 permerror (input_location,
793 "specialization of %qD in different namespace", tmpl);
794 inform (DECL_SOURCE_LOCATION (tmpl),
795 " from definition of %q#D", tmpl);
796 return false;
800 /* SPEC is an explicit instantiation. Check that it is valid to
801 perform this explicit instantiation in the current namespace. */
803 static void
804 check_explicit_instantiation_namespace (tree spec)
806 tree ns;
808 /* DR 275: An explicit instantiation shall appear in an enclosing
809 namespace of its template. */
810 ns = decl_namespace_context (spec);
811 if (!is_nested_namespace (current_namespace, ns))
812 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
813 "(which does not enclose namespace %qD)",
814 spec, current_namespace, ns);
817 // Returns the type of a template specialization only if that
818 // specialization needs to be defined. Otherwise (e.g., if the type has
819 // already been defined), the function returns NULL_TREE.
820 static tree
821 maybe_new_partial_specialization (tree type)
823 // An implicit instantiation of an incomplete type implies
824 // the definition of a new class template.
826 // template<typename T>
827 // struct S;
829 // template<typename T>
830 // struct S<T*>;
832 // Here, S<T*> is an implicit instantiation of S whose type
833 // is incomplete.
834 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
835 return type;
837 // It can also be the case that TYPE is a completed specialization.
838 // Continuing the previous example, suppose we also declare:
840 // template<typename T>
841 // requires Integral<T>
842 // struct S<T*>;
844 // Here, S<T*> refers to the specialization S<T*> defined
845 // above. However, we need to differentiate definitions because
846 // we intend to define a new partial specialization. In this case,
847 // we rely on the fact that the constraints are different for
848 // this declaration than that above.
850 // Note that we also get here for injected class names and
851 // late-parsed template definitions. We must ensure that we
852 // do not create new type declarations for those cases.
853 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
855 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
856 tree args = CLASSTYPE_TI_ARGS (type);
858 // If there are no template parameters, this cannot be a new
859 // partial template specializtion?
860 if (!current_template_parms)
861 return NULL_TREE;
863 // The injected-class-name is not a new partial specialization.
864 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
865 return NULL_TREE;
867 // If the constraints are not the same as those of the primary
868 // then, we can probably create a new specialization.
869 tree type_constr = current_template_constraints ();
871 if (type == TREE_TYPE (tmpl))
873 tree main_constr = get_constraints (tmpl);
874 if (equivalent_constraints (type_constr, main_constr))
875 return NULL_TREE;
878 // Also, if there's a pre-existing specialization with matching
879 // constraints, then this also isn't new.
880 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
881 while (specs)
883 tree spec_tmpl = TREE_VALUE (specs);
884 tree spec_args = TREE_PURPOSE (specs);
885 tree spec_constr = get_constraints (spec_tmpl);
886 if (comp_template_args (args, spec_args)
887 && equivalent_constraints (type_constr, spec_constr))
888 return NULL_TREE;
889 specs = TREE_CHAIN (specs);
892 // Create a new type node (and corresponding type decl)
893 // for the newly declared specialization.
894 tree t = make_class_type (TREE_CODE (type));
895 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
896 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
898 /* We only need a separate type node for storing the definition of this
899 partial specialization; uses of S<T*> are unconstrained, so all are
900 equivalent. So keep TYPE_CANONICAL the same. */
901 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
903 // Build the corresponding type decl.
904 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
905 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
906 DECL_SOURCE_LOCATION (d) = input_location;
908 return t;
911 return NULL_TREE;
914 /* The TYPE is being declared. If it is a template type, that means it
915 is a partial specialization. Do appropriate error-checking. */
917 tree
918 maybe_process_partial_specialization (tree type)
920 tree context;
922 if (type == error_mark_node)
923 return error_mark_node;
925 /* A lambda that appears in specialization context is not itself a
926 specialization. */
927 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
928 return type;
930 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
932 error ("name of class shadows template template parameter %qD",
933 TYPE_NAME (type));
934 return error_mark_node;
937 context = TYPE_CONTEXT (type);
939 if (TYPE_ALIAS_P (type))
941 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
943 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
944 error ("specialization of alias template %qD",
945 TI_TEMPLATE (tinfo));
946 else
947 error ("explicit specialization of non-template %qT", type);
948 return error_mark_node;
950 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
952 /* This is for ordinary explicit specialization and partial
953 specialization of a template class such as:
955 template <> class C<int>;
959 template <class T> class C<T*>;
961 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
963 if (tree t = maybe_new_partial_specialization (type))
965 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
966 && !at_namespace_scope_p ())
967 return error_mark_node;
968 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
969 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
970 if (processing_template_decl)
972 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
973 if (decl == error_mark_node)
974 return error_mark_node;
975 return TREE_TYPE (decl);
978 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
979 error ("specialization of %qT after instantiation", type);
980 else if (errorcount && !processing_specialization
981 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
982 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
983 /* Trying to define a specialization either without a template<> header
984 or in an inappropriate place. We've already given an error, so just
985 bail now so we don't actually define the specialization. */
986 return error_mark_node;
988 else if (CLASS_TYPE_P (type)
989 && !CLASSTYPE_USE_TEMPLATE (type)
990 && CLASSTYPE_TEMPLATE_INFO (type)
991 && context && CLASS_TYPE_P (context)
992 && CLASSTYPE_TEMPLATE_INFO (context))
994 /* This is for an explicit specialization of member class
995 template according to [temp.expl.spec/18]:
997 template <> template <class U> class C<int>::D;
999 The context `C<int>' must be an implicit instantiation.
1000 Otherwise this is just a member class template declared
1001 earlier like:
1003 template <> class C<int> { template <class U> class D; };
1004 template <> template <class U> class C<int>::D;
1006 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1007 while in the second case, `C<int>::D' is a primary template
1008 and `C<T>::D' may not exist. */
1010 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1011 && !COMPLETE_TYPE_P (type))
1013 tree t;
1014 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1016 if (current_namespace
1017 != decl_namespace_context (tmpl))
1019 permerror (input_location,
1020 "specializing %q#T in different namespace", type);
1021 permerror (DECL_SOURCE_LOCATION (tmpl),
1022 " from definition of %q#D", tmpl);
1025 /* Check for invalid specialization after instantiation:
1027 template <> template <> class C<int>::D<int>;
1028 template <> template <class U> class C<int>::D; */
1030 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1031 t; t = TREE_CHAIN (t))
1033 tree inst = TREE_VALUE (t);
1034 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1035 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1037 /* We already have a full specialization of this partial
1038 instantiation, or a full specialization has been
1039 looked up but not instantiated. Reassign it to the
1040 new member specialization template. */
1041 spec_entry elt;
1042 spec_entry *entry;
1044 elt.tmpl = most_general_template (tmpl);
1045 elt.args = CLASSTYPE_TI_ARGS (inst);
1046 elt.spec = inst;
1048 type_specializations->remove_elt (&elt);
1050 elt.tmpl = tmpl;
1051 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1053 spec_entry **slot
1054 = type_specializations->find_slot (&elt, INSERT);
1055 entry = ggc_alloc<spec_entry> ();
1056 *entry = elt;
1057 *slot = entry;
1059 else
1060 /* But if we've had an implicit instantiation, that's a
1061 problem ([temp.expl.spec]/6). */
1062 error ("specialization %qT after instantiation %qT",
1063 type, inst);
1066 /* Mark TYPE as a specialization. And as a result, we only
1067 have one level of template argument for the innermost
1068 class template. */
1069 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1070 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1071 CLASSTYPE_TI_ARGS (type)
1072 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1075 else if (processing_specialization)
1077 /* Someday C++0x may allow for enum template specialization. */
1078 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1079 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1080 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1081 "of %qD not allowed by ISO C++", type);
1082 else
1084 error ("explicit specialization of non-template %qT", type);
1085 return error_mark_node;
1089 return type;
1092 /* Returns nonzero if we can optimize the retrieval of specializations
1093 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1094 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1096 static inline bool
1097 optimize_specialization_lookup_p (tree tmpl)
1099 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1100 && DECL_CLASS_SCOPE_P (tmpl)
1101 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1102 parameter. */
1103 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1104 /* The optimized lookup depends on the fact that the
1105 template arguments for the member function template apply
1106 purely to the containing class, which is not true if the
1107 containing class is an explicit or partial
1108 specialization. */
1109 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1110 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1111 && !DECL_CONV_FN_P (tmpl)
1112 /* It is possible to have a template that is not a member
1113 template and is not a member of a template class:
1115 template <typename T>
1116 struct S { friend A::f(); };
1118 Here, the friend function is a template, but the context does
1119 not have template information. The optimized lookup relies
1120 on having ARGS be the template arguments for both the class
1121 and the function template. */
1122 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1125 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1126 gone through coerce_template_parms by now. */
1128 static void
1129 verify_unstripped_args (tree args)
1131 ++processing_template_decl;
1132 if (!any_dependent_template_arguments_p (args))
1134 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1135 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1137 tree arg = TREE_VEC_ELT (inner, i);
1138 if (TREE_CODE (arg) == TEMPLATE_DECL)
1139 /* OK */;
1140 else if (TYPE_P (arg))
1141 gcc_assert (strip_typedefs (arg, NULL) == arg);
1142 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1143 /* Allow typedefs on the type of a non-type argument, since a
1144 parameter can have them. */;
1145 else
1146 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1149 --processing_template_decl;
1152 /* Retrieve the specialization (in the sense of [temp.spec] - a
1153 specialization is either an instantiation or an explicit
1154 specialization) of TMPL for the given template ARGS. If there is
1155 no such specialization, return NULL_TREE. The ARGS are a vector of
1156 arguments, or a vector of vectors of arguments, in the case of
1157 templates with more than one level of parameters.
1159 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1160 then we search for a partial specialization matching ARGS. This
1161 parameter is ignored if TMPL is not a class template.
1163 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1164 result is a NONTYPE_ARGUMENT_PACK. */
1166 static tree
1167 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1169 if (tmpl == NULL_TREE)
1170 return NULL_TREE;
1172 if (args == error_mark_node)
1173 return NULL_TREE;
1175 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1176 || TREE_CODE (tmpl) == FIELD_DECL);
1178 /* There should be as many levels of arguments as there are
1179 levels of parameters. */
1180 gcc_assert (TMPL_ARGS_DEPTH (args)
1181 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1182 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1183 : template_class_depth (DECL_CONTEXT (tmpl))));
1185 if (flag_checking)
1186 verify_unstripped_args (args);
1188 if (optimize_specialization_lookup_p (tmpl))
1190 /* The template arguments actually apply to the containing
1191 class. Find the class specialization with those
1192 arguments. */
1193 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1194 tree class_specialization
1195 = retrieve_specialization (class_template, args, 0);
1196 if (!class_specialization)
1197 return NULL_TREE;
1198 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1199 for the specialization. */
1200 int idx = class_method_index_for_fn (class_specialization, tmpl);
1201 if (idx == -1)
1202 return NULL_TREE;
1203 /* Iterate through the methods with the indicated name, looking
1204 for the one that has an instance of TMPL. */
1205 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_specialization);
1206 for (ovl_iterator iter ((*methods)[idx]); iter; ++iter)
1208 tree fn = *iter;
1209 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1210 /* using-declarations can add base methods to the method vec,
1211 and we don't want those here. */
1212 && DECL_CONTEXT (fn) == class_specialization)
1213 return fn;
1215 return NULL_TREE;
1217 else
1219 spec_entry *found;
1220 spec_entry elt;
1221 hash_table<spec_hasher> *specializations;
1223 elt.tmpl = tmpl;
1224 elt.args = args;
1225 elt.spec = NULL_TREE;
1227 if (DECL_CLASS_TEMPLATE_P (tmpl))
1228 specializations = type_specializations;
1229 else
1230 specializations = decl_specializations;
1232 if (hash == 0)
1233 hash = spec_hasher::hash (&elt);
1234 found = specializations->find_with_hash (&elt, hash);
1235 if (found)
1236 return found->spec;
1239 return NULL_TREE;
1242 /* Like retrieve_specialization, but for local declarations. */
1244 tree
1245 retrieve_local_specialization (tree tmpl)
1247 if (local_specializations == NULL)
1248 return NULL_TREE;
1250 tree *slot = local_specializations->get (tmpl);
1251 return slot ? *slot : NULL_TREE;
1254 /* Returns nonzero iff DECL is a specialization of TMPL. */
1257 is_specialization_of (tree decl, tree tmpl)
1259 tree t;
1261 if (TREE_CODE (decl) == FUNCTION_DECL)
1263 for (t = decl;
1264 t != NULL_TREE;
1265 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1266 if (t == tmpl)
1267 return 1;
1269 else
1271 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1273 for (t = TREE_TYPE (decl);
1274 t != NULL_TREE;
1275 t = CLASSTYPE_USE_TEMPLATE (t)
1276 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1277 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1278 return 1;
1281 return 0;
1284 /* Returns nonzero iff DECL is a specialization of friend declaration
1285 FRIEND_DECL according to [temp.friend]. */
1287 bool
1288 is_specialization_of_friend (tree decl, tree friend_decl)
1290 bool need_template = true;
1291 int template_depth;
1293 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1294 || TREE_CODE (decl) == TYPE_DECL);
1296 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1297 of a template class, we want to check if DECL is a specialization
1298 if this. */
1299 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1300 && DECL_TEMPLATE_INFO (friend_decl)
1301 && !DECL_USE_TEMPLATE (friend_decl))
1303 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1304 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1305 need_template = false;
1307 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1308 && !PRIMARY_TEMPLATE_P (friend_decl))
1309 need_template = false;
1311 /* There is nothing to do if this is not a template friend. */
1312 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1313 return false;
1315 if (is_specialization_of (decl, friend_decl))
1316 return true;
1318 /* [temp.friend/6]
1319 A member of a class template may be declared to be a friend of a
1320 non-template class. In this case, the corresponding member of
1321 every specialization of the class template is a friend of the
1322 class granting friendship.
1324 For example, given a template friend declaration
1326 template <class T> friend void A<T>::f();
1328 the member function below is considered a friend
1330 template <> struct A<int> {
1331 void f();
1334 For this type of template friend, TEMPLATE_DEPTH below will be
1335 nonzero. To determine if DECL is a friend of FRIEND, we first
1336 check if the enclosing class is a specialization of another. */
1338 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1339 if (template_depth
1340 && DECL_CLASS_SCOPE_P (decl)
1341 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1342 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1344 /* Next, we check the members themselves. In order to handle
1345 a few tricky cases, such as when FRIEND_DECL's are
1347 template <class T> friend void A<T>::g(T t);
1348 template <class T> template <T t> friend void A<T>::h();
1350 and DECL's are
1352 void A<int>::g(int);
1353 template <int> void A<int>::h();
1355 we need to figure out ARGS, the template arguments from
1356 the context of DECL. This is required for template substitution
1357 of `T' in the function parameter of `g' and template parameter
1358 of `h' in the above examples. Here ARGS corresponds to `int'. */
1360 tree context = DECL_CONTEXT (decl);
1361 tree args = NULL_TREE;
1362 int current_depth = 0;
1364 while (current_depth < template_depth)
1366 if (CLASSTYPE_TEMPLATE_INFO (context))
1368 if (current_depth == 0)
1369 args = TYPE_TI_ARGS (context);
1370 else
1371 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1372 current_depth++;
1374 context = TYPE_CONTEXT (context);
1377 if (TREE_CODE (decl) == FUNCTION_DECL)
1379 bool is_template;
1380 tree friend_type;
1381 tree decl_type;
1382 tree friend_args_type;
1383 tree decl_args_type;
1385 /* Make sure that both DECL and FRIEND_DECL are templates or
1386 non-templates. */
1387 is_template = DECL_TEMPLATE_INFO (decl)
1388 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1389 if (need_template ^ is_template)
1390 return false;
1391 else if (is_template)
1393 /* If both are templates, check template parameter list. */
1394 tree friend_parms
1395 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1396 args, tf_none);
1397 if (!comp_template_parms
1398 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1399 friend_parms))
1400 return false;
1402 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1404 else
1405 decl_type = TREE_TYPE (decl);
1407 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1408 tf_none, NULL_TREE);
1409 if (friend_type == error_mark_node)
1410 return false;
1412 /* Check if return types match. */
1413 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1414 return false;
1416 /* Check if function parameter types match, ignoring the
1417 `this' parameter. */
1418 friend_args_type = TYPE_ARG_TYPES (friend_type);
1419 decl_args_type = TYPE_ARG_TYPES (decl_type);
1420 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1421 friend_args_type = TREE_CHAIN (friend_args_type);
1422 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1423 decl_args_type = TREE_CHAIN (decl_args_type);
1425 return compparms (decl_args_type, friend_args_type);
1427 else
1429 /* DECL is a TYPE_DECL */
1430 bool is_template;
1431 tree decl_type = TREE_TYPE (decl);
1433 /* Make sure that both DECL and FRIEND_DECL are templates or
1434 non-templates. */
1435 is_template
1436 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1437 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1439 if (need_template ^ is_template)
1440 return false;
1441 else if (is_template)
1443 tree friend_parms;
1444 /* If both are templates, check the name of the two
1445 TEMPLATE_DECL's first because is_friend didn't. */
1446 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1447 != DECL_NAME (friend_decl))
1448 return false;
1450 /* Now check template parameter list. */
1451 friend_parms
1452 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1453 args, tf_none);
1454 return comp_template_parms
1455 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1456 friend_parms);
1458 else
1459 return (DECL_NAME (decl)
1460 == DECL_NAME (friend_decl));
1463 return false;
1466 /* Register the specialization SPEC as a specialization of TMPL with
1467 the indicated ARGS. IS_FRIEND indicates whether the specialization
1468 is actually just a friend declaration. Returns SPEC, or an
1469 equivalent prior declaration, if available.
1471 We also store instantiations of field packs in the hash table, even
1472 though they are not themselves templates, to make lookup easier. */
1474 static tree
1475 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1476 hashval_t hash)
1478 tree fn;
1479 spec_entry **slot = NULL;
1480 spec_entry elt;
1482 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1483 || (TREE_CODE (tmpl) == FIELD_DECL
1484 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1486 if (TREE_CODE (spec) == FUNCTION_DECL
1487 && uses_template_parms (DECL_TI_ARGS (spec)))
1488 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1489 register it; we want the corresponding TEMPLATE_DECL instead.
1490 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1491 the more obvious `uses_template_parms (spec)' to avoid problems
1492 with default function arguments. In particular, given
1493 something like this:
1495 template <class T> void f(T t1, T t = T())
1497 the default argument expression is not substituted for in an
1498 instantiation unless and until it is actually needed. */
1499 return spec;
1501 if (optimize_specialization_lookup_p (tmpl))
1502 /* We don't put these specializations in the hash table, but we might
1503 want to give an error about a mismatch. */
1504 fn = retrieve_specialization (tmpl, args, 0);
1505 else
1507 elt.tmpl = tmpl;
1508 elt.args = args;
1509 elt.spec = spec;
1511 if (hash == 0)
1512 hash = spec_hasher::hash (&elt);
1514 slot =
1515 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1516 if (*slot)
1517 fn = ((spec_entry *) *slot)->spec;
1518 else
1519 fn = NULL_TREE;
1522 /* We can sometimes try to re-register a specialization that we've
1523 already got. In particular, regenerate_decl_from_template calls
1524 duplicate_decls which will update the specialization list. But,
1525 we'll still get called again here anyhow. It's more convenient
1526 to simply allow this than to try to prevent it. */
1527 if (fn == spec)
1528 return spec;
1529 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1531 if (DECL_TEMPLATE_INSTANTIATION (fn))
1533 if (DECL_ODR_USED (fn)
1534 || DECL_EXPLICIT_INSTANTIATION (fn))
1536 error ("specialization of %qD after instantiation",
1537 fn);
1538 return error_mark_node;
1540 else
1542 tree clone;
1543 /* This situation should occur only if the first
1544 specialization is an implicit instantiation, the
1545 second is an explicit specialization, and the
1546 implicit instantiation has not yet been used. That
1547 situation can occur if we have implicitly
1548 instantiated a member function and then specialized
1549 it later.
1551 We can also wind up here if a friend declaration that
1552 looked like an instantiation turns out to be a
1553 specialization:
1555 template <class T> void foo(T);
1556 class S { friend void foo<>(int) };
1557 template <> void foo(int);
1559 We transform the existing DECL in place so that any
1560 pointers to it become pointers to the updated
1561 declaration.
1563 If there was a definition for the template, but not
1564 for the specialization, we want this to look as if
1565 there were no definition, and vice versa. */
1566 DECL_INITIAL (fn) = NULL_TREE;
1567 duplicate_decls (spec, fn, is_friend);
1568 /* The call to duplicate_decls will have applied
1569 [temp.expl.spec]:
1571 An explicit specialization of a function template
1572 is inline only if it is explicitly declared to be,
1573 and independently of whether its function template
1576 to the primary function; now copy the inline bits to
1577 the various clones. */
1578 FOR_EACH_CLONE (clone, fn)
1580 DECL_DECLARED_INLINE_P (clone)
1581 = DECL_DECLARED_INLINE_P (fn);
1582 DECL_SOURCE_LOCATION (clone)
1583 = DECL_SOURCE_LOCATION (fn);
1584 DECL_DELETED_FN (clone)
1585 = DECL_DELETED_FN (fn);
1587 check_specialization_namespace (tmpl);
1589 return fn;
1592 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1594 tree dd = duplicate_decls (spec, fn, is_friend);
1595 if (dd == error_mark_node)
1596 /* We've already complained in duplicate_decls. */
1597 return error_mark_node;
1599 if (dd == NULL_TREE && DECL_INITIAL (spec))
1600 /* Dup decl failed, but this is a new definition. Set the
1601 line number so any errors match this new
1602 definition. */
1603 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1605 return fn;
1608 else if (fn)
1609 return duplicate_decls (spec, fn, is_friend);
1611 /* A specialization must be declared in the same namespace as the
1612 template it is specializing. */
1613 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1614 && !check_specialization_namespace (tmpl))
1615 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1617 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1619 spec_entry *entry = ggc_alloc<spec_entry> ();
1620 gcc_assert (tmpl && args && spec);
1621 *entry = elt;
1622 *slot = entry;
1623 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1624 && PRIMARY_TEMPLATE_P (tmpl)
1625 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1626 || variable_template_p (tmpl))
1627 /* If TMPL is a forward declaration of a template function, keep a list
1628 of all specializations in case we need to reassign them to a friend
1629 template later in tsubst_friend_function.
1631 Also keep a list of all variable template instantiations so that
1632 process_partial_specialization can check whether a later partial
1633 specialization would have used it. */
1634 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1635 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1638 return spec;
1641 /* Returns true iff two spec_entry nodes are equivalent. */
1643 int comparing_specializations;
1645 bool
1646 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1648 int equal;
1650 ++comparing_specializations;
1651 equal = (e1->tmpl == e2->tmpl
1652 && comp_template_args (e1->args, e2->args));
1653 if (equal && flag_concepts
1654 /* tmpl could be a FIELD_DECL for a capture pack. */
1655 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1656 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1657 && uses_template_parms (e1->args))
1659 /* Partial specializations of a variable template can be distinguished by
1660 constraints. */
1661 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1662 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1663 equal = equivalent_constraints (c1, c2);
1665 --comparing_specializations;
1667 return equal;
1670 /* Returns a hash for a template TMPL and template arguments ARGS. */
1672 static hashval_t
1673 hash_tmpl_and_args (tree tmpl, tree args)
1675 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1676 return iterative_hash_template_arg (args, val);
1679 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1680 ignoring SPEC. */
1682 hashval_t
1683 spec_hasher::hash (spec_entry *e)
1685 return hash_tmpl_and_args (e->tmpl, e->args);
1688 /* Recursively calculate a hash value for a template argument ARG, for use
1689 in the hash tables of template specializations. */
1691 hashval_t
1692 iterative_hash_template_arg (tree arg, hashval_t val)
1694 unsigned HOST_WIDE_INT i;
1695 enum tree_code code;
1696 char tclass;
1698 if (arg == NULL_TREE)
1699 return iterative_hash_object (arg, val);
1701 if (!TYPE_P (arg))
1702 STRIP_NOPS (arg);
1704 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1705 gcc_unreachable ();
1707 code = TREE_CODE (arg);
1708 tclass = TREE_CODE_CLASS (code);
1710 val = iterative_hash_object (code, val);
1712 switch (code)
1714 case ERROR_MARK:
1715 return val;
1717 case IDENTIFIER_NODE:
1718 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1720 case TREE_VEC:
1722 int i, len = TREE_VEC_LENGTH (arg);
1723 for (i = 0; i < len; ++i)
1724 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1725 return val;
1728 case TYPE_PACK_EXPANSION:
1729 case EXPR_PACK_EXPANSION:
1730 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1731 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1733 case TYPE_ARGUMENT_PACK:
1734 case NONTYPE_ARGUMENT_PACK:
1735 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1737 case TREE_LIST:
1738 for (; arg; arg = TREE_CHAIN (arg))
1739 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1740 return val;
1742 case OVERLOAD:
1743 for (lkp_iterator iter (arg); iter; ++iter)
1744 val = iterative_hash_template_arg (*iter, val);
1745 return val;
1747 case CONSTRUCTOR:
1749 tree field, value;
1750 iterative_hash_template_arg (TREE_TYPE (arg), val);
1751 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1753 val = iterative_hash_template_arg (field, val);
1754 val = iterative_hash_template_arg (value, val);
1756 return val;
1759 case PARM_DECL:
1760 if (!DECL_ARTIFICIAL (arg))
1762 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1763 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1765 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1767 case TARGET_EXPR:
1768 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1770 case PTRMEM_CST:
1771 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1772 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1774 case TEMPLATE_PARM_INDEX:
1775 val = iterative_hash_template_arg
1776 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1777 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1778 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1780 case TRAIT_EXPR:
1781 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1782 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1783 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1785 case BASELINK:
1786 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1787 val);
1788 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1789 val);
1791 case MODOP_EXPR:
1792 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1793 code = TREE_CODE (TREE_OPERAND (arg, 1));
1794 val = iterative_hash_object (code, val);
1795 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1797 case LAMBDA_EXPR:
1798 /* A lambda can't appear in a template arg, but don't crash on
1799 erroneous input. */
1800 gcc_assert (seen_error ());
1801 return val;
1803 case CAST_EXPR:
1804 case IMPLICIT_CONV_EXPR:
1805 case STATIC_CAST_EXPR:
1806 case REINTERPRET_CAST_EXPR:
1807 case CONST_CAST_EXPR:
1808 case DYNAMIC_CAST_EXPR:
1809 case NEW_EXPR:
1810 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1811 /* Now hash operands as usual. */
1812 break;
1814 default:
1815 break;
1818 switch (tclass)
1820 case tcc_type:
1821 if (alias_template_specialization_p (arg))
1823 // We want an alias specialization that survived strip_typedefs
1824 // to hash differently from its TYPE_CANONICAL, to avoid hash
1825 // collisions that compare as different in template_args_equal.
1826 // These could be dependent specializations that strip_typedefs
1827 // left alone, or untouched specializations because
1828 // coerce_template_parms returns the unconverted template
1829 // arguments if it sees incomplete argument packs.
1830 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1831 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1833 if (TYPE_CANONICAL (arg))
1834 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1835 val);
1836 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1837 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1838 /* Otherwise just compare the types during lookup. */
1839 return val;
1841 case tcc_declaration:
1842 case tcc_constant:
1843 return iterative_hash_expr (arg, val);
1845 default:
1846 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1848 unsigned n = cp_tree_operand_length (arg);
1849 for (i = 0; i < n; ++i)
1850 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1851 return val;
1854 gcc_unreachable ();
1855 return 0;
1858 /* Unregister the specialization SPEC as a specialization of TMPL.
1859 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1860 if the SPEC was listed as a specialization of TMPL.
1862 Note that SPEC has been ggc_freed, so we can't look inside it. */
1864 bool
1865 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1867 spec_entry *entry;
1868 spec_entry elt;
1870 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1871 elt.args = TI_ARGS (tinfo);
1872 elt.spec = NULL_TREE;
1874 entry = decl_specializations->find (&elt);
1875 if (entry != NULL)
1877 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1878 gcc_assert (new_spec != NULL_TREE);
1879 entry->spec = new_spec;
1880 return 1;
1883 return 0;
1886 /* Like register_specialization, but for local declarations. We are
1887 registering SPEC, an instantiation of TMPL. */
1889 void
1890 register_local_specialization (tree spec, tree tmpl)
1892 local_specializations->put (tmpl, spec);
1895 /* TYPE is a class type. Returns true if TYPE is an explicitly
1896 specialized class. */
1898 bool
1899 explicit_class_specialization_p (tree type)
1901 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1902 return false;
1903 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1906 /* Print the list of functions at FNS, going through all the overloads
1907 for each element of the list. Alternatively, FNS can not be a
1908 TREE_LIST, in which case it will be printed together with all the
1909 overloads.
1911 MORE and *STR should respectively be FALSE and NULL when the function
1912 is called from the outside. They are used internally on recursive
1913 calls. print_candidates manages the two parameters and leaves NULL
1914 in *STR when it ends. */
1916 static void
1917 print_candidates_1 (tree fns, char **str, bool more = false)
1919 if (TREE_CODE (fns) == TREE_LIST)
1920 for (; fns; fns = TREE_CHAIN (fns))
1921 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1922 else
1923 for (lkp_iterator iter (fns); iter;)
1925 tree cand = *iter;
1926 ++iter;
1928 const char *pfx = *str;
1929 if (!pfx)
1931 if (more || iter)
1932 pfx = _("candidates are:");
1933 else
1934 pfx = _("candidate is:");
1935 *str = get_spaces (pfx);
1937 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1941 /* Print the list of candidate FNS in an error message. FNS can also
1942 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1944 void
1945 print_candidates (tree fns)
1947 char *str = NULL;
1948 print_candidates_1 (fns, &str);
1949 free (str);
1952 /* Get a (possibly) constrained template declaration for the
1953 purpose of ordering candidates. */
1954 static tree
1955 get_template_for_ordering (tree list)
1957 gcc_assert (TREE_CODE (list) == TREE_LIST);
1958 tree f = TREE_VALUE (list);
1959 if (tree ti = DECL_TEMPLATE_INFO (f))
1960 return TI_TEMPLATE (ti);
1961 return f;
1964 /* Among candidates having the same signature, return the
1965 most constrained or NULL_TREE if there is no best candidate.
1966 If the signatures of candidates vary (e.g., template
1967 specialization vs. member function), then there can be no
1968 most constrained.
1970 Note that we don't compare constraints on the functions
1971 themselves, but rather those of their templates. */
1972 static tree
1973 most_constrained_function (tree candidates)
1975 // Try to find the best candidate in a first pass.
1976 tree champ = candidates;
1977 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1979 int winner = more_constrained (get_template_for_ordering (champ),
1980 get_template_for_ordering (c));
1981 if (winner == -1)
1982 champ = c; // The candidate is more constrained
1983 else if (winner == 0)
1984 return NULL_TREE; // Neither is more constrained
1987 // Verify that the champ is better than previous candidates.
1988 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1989 if (!more_constrained (get_template_for_ordering (champ),
1990 get_template_for_ordering (c)))
1991 return NULL_TREE;
1994 return champ;
1998 /* Returns the template (one of the functions given by TEMPLATE_ID)
1999 which can be specialized to match the indicated DECL with the
2000 explicit template args given in TEMPLATE_ID. The DECL may be
2001 NULL_TREE if none is available. In that case, the functions in
2002 TEMPLATE_ID are non-members.
2004 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2005 specialization of a member template.
2007 The TEMPLATE_COUNT is the number of references to qualifying
2008 template classes that appeared in the name of the function. See
2009 check_explicit_specialization for a more accurate description.
2011 TSK indicates what kind of template declaration (if any) is being
2012 declared. TSK_TEMPLATE indicates that the declaration given by
2013 DECL, though a FUNCTION_DECL, has template parameters, and is
2014 therefore a template function.
2016 The template args (those explicitly specified and those deduced)
2017 are output in a newly created vector *TARGS_OUT.
2019 If it is impossible to determine the result, an error message is
2020 issued. The error_mark_node is returned to indicate failure. */
2022 static tree
2023 determine_specialization (tree template_id,
2024 tree decl,
2025 tree* targs_out,
2026 int need_member_template,
2027 int template_count,
2028 tmpl_spec_kind tsk)
2030 tree fns;
2031 tree targs;
2032 tree explicit_targs;
2033 tree candidates = NULL_TREE;
2035 /* A TREE_LIST of templates of which DECL may be a specialization.
2036 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2037 corresponding TREE_PURPOSE is the set of template arguments that,
2038 when used to instantiate the template, would produce a function
2039 with the signature of DECL. */
2040 tree templates = NULL_TREE;
2041 int header_count;
2042 cp_binding_level *b;
2044 *targs_out = NULL_TREE;
2046 if (template_id == error_mark_node || decl == error_mark_node)
2047 return error_mark_node;
2049 /* We shouldn't be specializing a member template of an
2050 unspecialized class template; we already gave an error in
2051 check_specialization_scope, now avoid crashing. */
2052 if (template_count && DECL_CLASS_SCOPE_P (decl)
2053 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2055 gcc_assert (errorcount);
2056 return error_mark_node;
2059 fns = TREE_OPERAND (template_id, 0);
2060 explicit_targs = TREE_OPERAND (template_id, 1);
2062 if (fns == error_mark_node)
2063 return error_mark_node;
2065 /* Check for baselinks. */
2066 if (BASELINK_P (fns))
2067 fns = BASELINK_FUNCTIONS (fns);
2069 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2071 error ("%qD is not a function template", fns);
2072 return error_mark_node;
2074 else if (VAR_P (decl) && !variable_template_p (fns))
2076 error ("%qD is not a variable template", fns);
2077 return error_mark_node;
2080 /* Count the number of template headers specified for this
2081 specialization. */
2082 header_count = 0;
2083 for (b = current_binding_level;
2084 b->kind == sk_template_parms;
2085 b = b->level_chain)
2086 ++header_count;
2088 tree orig_fns = fns;
2090 if (variable_template_p (fns))
2092 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2093 targs = coerce_template_parms (parms, explicit_targs, fns,
2094 tf_warning_or_error,
2095 /*req_all*/true, /*use_defarg*/true);
2096 if (targs != error_mark_node)
2097 templates = tree_cons (targs, fns, templates);
2099 else for (lkp_iterator iter (fns); iter; ++iter)
2101 tree fn = *iter;
2103 if (TREE_CODE (fn) == TEMPLATE_DECL)
2105 tree decl_arg_types;
2106 tree fn_arg_types;
2107 tree insttype;
2109 /* In case of explicit specialization, we need to check if
2110 the number of template headers appearing in the specialization
2111 is correct. This is usually done in check_explicit_specialization,
2112 but the check done there cannot be exhaustive when specializing
2113 member functions. Consider the following code:
2115 template <> void A<int>::f(int);
2116 template <> template <> void A<int>::f(int);
2118 Assuming that A<int> is not itself an explicit specialization
2119 already, the first line specializes "f" which is a non-template
2120 member function, whilst the second line specializes "f" which
2121 is a template member function. So both lines are syntactically
2122 correct, and check_explicit_specialization does not reject
2123 them.
2125 Here, we can do better, as we are matching the specialization
2126 against the declarations. We count the number of template
2127 headers, and we check if they match TEMPLATE_COUNT + 1
2128 (TEMPLATE_COUNT is the number of qualifying template classes,
2129 plus there must be another header for the member template
2130 itself).
2132 Notice that if header_count is zero, this is not a
2133 specialization but rather a template instantiation, so there
2134 is no check we can perform here. */
2135 if (header_count && header_count != template_count + 1)
2136 continue;
2138 /* Check that the number of template arguments at the
2139 innermost level for DECL is the same as for FN. */
2140 if (current_binding_level->kind == sk_template_parms
2141 && !current_binding_level->explicit_spec_p
2142 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2143 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2144 (current_template_parms))))
2145 continue;
2147 /* DECL might be a specialization of FN. */
2148 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2149 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2151 /* For a non-static member function, we need to make sure
2152 that the const qualification is the same. Since
2153 get_bindings does not try to merge the "this" parameter,
2154 we must do the comparison explicitly. */
2155 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2156 && !same_type_p (TREE_VALUE (fn_arg_types),
2157 TREE_VALUE (decl_arg_types)))
2158 continue;
2160 /* Skip the "this" parameter and, for constructors of
2161 classes with virtual bases, the VTT parameter. A
2162 full specialization of a constructor will have a VTT
2163 parameter, but a template never will. */
2164 decl_arg_types
2165 = skip_artificial_parms_for (decl, decl_arg_types);
2166 fn_arg_types
2167 = skip_artificial_parms_for (fn, fn_arg_types);
2169 /* Function templates cannot be specializations; there are
2170 no partial specializations of functions. Therefore, if
2171 the type of DECL does not match FN, there is no
2172 match.
2174 Note that it should never be the case that we have both
2175 candidates added here, and for regular member functions
2176 below. */
2177 if (tsk == tsk_template)
2179 if (compparms (fn_arg_types, decl_arg_types))
2180 candidates = tree_cons (NULL_TREE, fn, candidates);
2181 continue;
2184 /* See whether this function might be a specialization of this
2185 template. Suppress access control because we might be trying
2186 to make this specialization a friend, and we have already done
2187 access control for the declaration of the specialization. */
2188 push_deferring_access_checks (dk_no_check);
2189 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2190 pop_deferring_access_checks ();
2192 if (!targs)
2193 /* We cannot deduce template arguments that when used to
2194 specialize TMPL will produce DECL. */
2195 continue;
2197 /* Remove, from the set of candidates, all those functions
2198 whose constraints are not satisfied. */
2199 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2200 continue;
2202 // Then, try to form the new function type.
2203 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2204 if (insttype == error_mark_node)
2205 continue;
2206 fn_arg_types
2207 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2208 if (!compparms (fn_arg_types, decl_arg_types))
2209 continue;
2211 /* Save this template, and the arguments deduced. */
2212 templates = tree_cons (targs, fn, templates);
2214 else if (need_member_template)
2215 /* FN is an ordinary member function, and we need a
2216 specialization of a member template. */
2218 else if (TREE_CODE (fn) != FUNCTION_DECL)
2219 /* We can get IDENTIFIER_NODEs here in certain erroneous
2220 cases. */
2222 else if (!DECL_FUNCTION_MEMBER_P (fn))
2223 /* This is just an ordinary non-member function. Nothing can
2224 be a specialization of that. */
2226 else if (DECL_ARTIFICIAL (fn))
2227 /* Cannot specialize functions that are created implicitly. */
2229 else
2231 tree decl_arg_types;
2233 /* This is an ordinary member function. However, since
2234 we're here, we can assume its enclosing class is a
2235 template class. For example,
2237 template <typename T> struct S { void f(); };
2238 template <> void S<int>::f() {}
2240 Here, S<int>::f is a non-template, but S<int> is a
2241 template class. If FN has the same type as DECL, we
2242 might be in business. */
2244 if (!DECL_TEMPLATE_INFO (fn))
2245 /* Its enclosing class is an explicit specialization
2246 of a template class. This is not a candidate. */
2247 continue;
2249 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2250 TREE_TYPE (TREE_TYPE (fn))))
2251 /* The return types differ. */
2252 continue;
2254 /* Adjust the type of DECL in case FN is a static member. */
2255 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2256 if (DECL_STATIC_FUNCTION_P (fn)
2257 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2258 decl_arg_types = TREE_CHAIN (decl_arg_types);
2260 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2261 decl_arg_types))
2262 continue;
2264 // If the deduced arguments do not satisfy the constraints,
2265 // this is not a candidate.
2266 if (flag_concepts && !constraints_satisfied_p (fn))
2267 continue;
2269 // Add the candidate.
2270 candidates = tree_cons (NULL_TREE, fn, candidates);
2274 if (templates && TREE_CHAIN (templates))
2276 /* We have:
2278 [temp.expl.spec]
2280 It is possible for a specialization with a given function
2281 signature to be instantiated from more than one function
2282 template. In such cases, explicit specification of the
2283 template arguments must be used to uniquely identify the
2284 function template specialization being specialized.
2286 Note that here, there's no suggestion that we're supposed to
2287 determine which of the candidate templates is most
2288 specialized. However, we, also have:
2290 [temp.func.order]
2292 Partial ordering of overloaded function template
2293 declarations is used in the following contexts to select
2294 the function template to which a function template
2295 specialization refers:
2297 -- when an explicit specialization refers to a function
2298 template.
2300 So, we do use the partial ordering rules, at least for now.
2301 This extension can only serve to make invalid programs valid,
2302 so it's safe. And, there is strong anecdotal evidence that
2303 the committee intended the partial ordering rules to apply;
2304 the EDG front end has that behavior, and John Spicer claims
2305 that the committee simply forgot to delete the wording in
2306 [temp.expl.spec]. */
2307 tree tmpl = most_specialized_instantiation (templates);
2308 if (tmpl != error_mark_node)
2310 templates = tmpl;
2311 TREE_CHAIN (templates) = NULL_TREE;
2315 // Concepts allows multiple declarations of member functions
2316 // with the same signature. Like above, we need to rely on
2317 // on the partial ordering of those candidates to determine which
2318 // is the best.
2319 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2321 if (tree cand = most_constrained_function (candidates))
2323 candidates = cand;
2324 TREE_CHAIN (cand) = NULL_TREE;
2328 if (templates == NULL_TREE && candidates == NULL_TREE)
2330 error ("template-id %qD for %q+D does not match any template "
2331 "declaration", template_id, decl);
2332 if (header_count && header_count != template_count + 1)
2333 inform (input_location, "saw %d %<template<>%>, need %d for "
2334 "specializing a member function template",
2335 header_count, template_count + 1);
2336 else
2337 print_candidates (orig_fns);
2338 return error_mark_node;
2340 else if ((templates && TREE_CHAIN (templates))
2341 || (candidates && TREE_CHAIN (candidates))
2342 || (templates && candidates))
2344 error ("ambiguous template specialization %qD for %q+D",
2345 template_id, decl);
2346 candidates = chainon (candidates, templates);
2347 print_candidates (candidates);
2348 return error_mark_node;
2351 /* We have one, and exactly one, match. */
2352 if (candidates)
2354 tree fn = TREE_VALUE (candidates);
2355 *targs_out = copy_node (DECL_TI_ARGS (fn));
2357 // Propagate the candidate's constraints to the declaration.
2358 set_constraints (decl, get_constraints (fn));
2360 /* DECL is a re-declaration or partial instantiation of a template
2361 function. */
2362 if (TREE_CODE (fn) == TEMPLATE_DECL)
2363 return fn;
2364 /* It was a specialization of an ordinary member function in a
2365 template class. */
2366 return DECL_TI_TEMPLATE (fn);
2369 /* It was a specialization of a template. */
2370 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2371 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2373 *targs_out = copy_node (targs);
2374 SET_TMPL_ARGS_LEVEL (*targs_out,
2375 TMPL_ARGS_DEPTH (*targs_out),
2376 TREE_PURPOSE (templates));
2378 else
2379 *targs_out = TREE_PURPOSE (templates);
2380 return TREE_VALUE (templates);
2383 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2384 but with the default argument values filled in from those in the
2385 TMPL_TYPES. */
2387 static tree
2388 copy_default_args_to_explicit_spec_1 (tree spec_types,
2389 tree tmpl_types)
2391 tree new_spec_types;
2393 if (!spec_types)
2394 return NULL_TREE;
2396 if (spec_types == void_list_node)
2397 return void_list_node;
2399 /* Substitute into the rest of the list. */
2400 new_spec_types =
2401 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2402 TREE_CHAIN (tmpl_types));
2404 /* Add the default argument for this parameter. */
2405 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2406 TREE_VALUE (spec_types),
2407 new_spec_types);
2410 /* DECL is an explicit specialization. Replicate default arguments
2411 from the template it specializes. (That way, code like:
2413 template <class T> void f(T = 3);
2414 template <> void f(double);
2415 void g () { f (); }
2417 works, as required.) An alternative approach would be to look up
2418 the correct default arguments at the call-site, but this approach
2419 is consistent with how implicit instantiations are handled. */
2421 static void
2422 copy_default_args_to_explicit_spec (tree decl)
2424 tree tmpl;
2425 tree spec_types;
2426 tree tmpl_types;
2427 tree new_spec_types;
2428 tree old_type;
2429 tree new_type;
2430 tree t;
2431 tree object_type = NULL_TREE;
2432 tree in_charge = NULL_TREE;
2433 tree vtt = NULL_TREE;
2435 /* See if there's anything we need to do. */
2436 tmpl = DECL_TI_TEMPLATE (decl);
2437 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2438 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2439 if (TREE_PURPOSE (t))
2440 break;
2441 if (!t)
2442 return;
2444 old_type = TREE_TYPE (decl);
2445 spec_types = TYPE_ARG_TYPES (old_type);
2447 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2449 /* Remove the this pointer, but remember the object's type for
2450 CV quals. */
2451 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2452 spec_types = TREE_CHAIN (spec_types);
2453 tmpl_types = TREE_CHAIN (tmpl_types);
2455 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2457 /* DECL may contain more parameters than TMPL due to the extra
2458 in-charge parameter in constructors and destructors. */
2459 in_charge = spec_types;
2460 spec_types = TREE_CHAIN (spec_types);
2462 if (DECL_HAS_VTT_PARM_P (decl))
2464 vtt = spec_types;
2465 spec_types = TREE_CHAIN (spec_types);
2469 /* Compute the merged default arguments. */
2470 new_spec_types =
2471 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2473 /* Compute the new FUNCTION_TYPE. */
2474 if (object_type)
2476 if (vtt)
2477 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2478 TREE_VALUE (vtt),
2479 new_spec_types);
2481 if (in_charge)
2482 /* Put the in-charge parameter back. */
2483 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2484 TREE_VALUE (in_charge),
2485 new_spec_types);
2487 new_type = build_method_type_directly (object_type,
2488 TREE_TYPE (old_type),
2489 new_spec_types);
2491 else
2492 new_type = build_function_type (TREE_TYPE (old_type),
2493 new_spec_types);
2494 new_type = cp_build_type_attribute_variant (new_type,
2495 TYPE_ATTRIBUTES (old_type));
2496 new_type = build_exception_variant (new_type,
2497 TYPE_RAISES_EXCEPTIONS (old_type));
2499 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2500 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2502 TREE_TYPE (decl) = new_type;
2505 /* Return the number of template headers we expect to see for a definition
2506 or specialization of CTYPE or one of its non-template members. */
2509 num_template_headers_for_class (tree ctype)
2511 int num_templates = 0;
2513 while (ctype && CLASS_TYPE_P (ctype))
2515 /* You're supposed to have one `template <...>' for every
2516 template class, but you don't need one for a full
2517 specialization. For example:
2519 template <class T> struct S{};
2520 template <> struct S<int> { void f(); };
2521 void S<int>::f () {}
2523 is correct; there shouldn't be a `template <>' for the
2524 definition of `S<int>::f'. */
2525 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2526 /* If CTYPE does not have template information of any
2527 kind, then it is not a template, nor is it nested
2528 within a template. */
2529 break;
2530 if (explicit_class_specialization_p (ctype))
2531 break;
2532 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2533 ++num_templates;
2535 ctype = TYPE_CONTEXT (ctype);
2538 return num_templates;
2541 /* Do a simple sanity check on the template headers that precede the
2542 variable declaration DECL. */
2544 void
2545 check_template_variable (tree decl)
2547 tree ctx = CP_DECL_CONTEXT (decl);
2548 int wanted = num_template_headers_for_class (ctx);
2549 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2550 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2552 if (cxx_dialect < cxx14)
2553 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2554 "variable templates only available with "
2555 "-std=c++14 or -std=gnu++14");
2557 // Namespace-scope variable templates should have a template header.
2558 ++wanted;
2560 if (template_header_count > wanted)
2562 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2563 "too many template headers for %qD "
2564 "(should be %d)",
2565 decl, wanted);
2566 if (warned && CLASS_TYPE_P (ctx)
2567 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2568 inform (DECL_SOURCE_LOCATION (decl),
2569 "members of an explicitly specialized class are defined "
2570 "without a template header");
2574 /* An explicit specialization whose declarator-id or class-head-name is not
2575 qualified shall be declared in the nearest enclosing namespace of the
2576 template, or, if the namespace is inline (7.3.1), any namespace from its
2577 enclosing namespace set.
2579 If the name declared in the explicit instantiation is an unqualified name,
2580 the explicit instantiation shall appear in the namespace where its template
2581 is declared or, if that namespace is inline (7.3.1), any namespace from its
2582 enclosing namespace set. */
2584 void
2585 check_unqualified_spec_or_inst (tree t, location_t loc)
2587 tree tmpl = most_general_template (t);
2588 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2589 && !is_nested_namespace (current_namespace,
2590 CP_DECL_CONTEXT (tmpl), true))
2592 if (processing_specialization)
2593 permerror (loc, "explicit specialization of %qD outside its "
2594 "namespace must use a nested-name-specifier", tmpl);
2595 else if (processing_explicit_instantiation
2596 && cxx_dialect >= cxx11)
2597 /* This was allowed in C++98, so only pedwarn. */
2598 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2599 "outside its namespace must use a nested-name-"
2600 "specifier", tmpl);
2604 /* Check to see if the function just declared, as indicated in
2605 DECLARATOR, and in DECL, is a specialization of a function
2606 template. We may also discover that the declaration is an explicit
2607 instantiation at this point.
2609 Returns DECL, or an equivalent declaration that should be used
2610 instead if all goes well. Issues an error message if something is
2611 amiss. Returns error_mark_node if the error is not easily
2612 recoverable.
2614 FLAGS is a bitmask consisting of the following flags:
2616 2: The function has a definition.
2617 4: The function is a friend.
2619 The TEMPLATE_COUNT is the number of references to qualifying
2620 template classes that appeared in the name of the function. For
2621 example, in
2623 template <class T> struct S { void f(); };
2624 void S<int>::f();
2626 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2627 classes are not counted in the TEMPLATE_COUNT, so that in
2629 template <class T> struct S {};
2630 template <> struct S<int> { void f(); }
2631 template <> void S<int>::f();
2633 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2634 invalid; there should be no template <>.)
2636 If the function is a specialization, it is marked as such via
2637 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2638 is set up correctly, and it is added to the list of specializations
2639 for that template. */
2641 tree
2642 check_explicit_specialization (tree declarator,
2643 tree decl,
2644 int template_count,
2645 int flags)
2647 int have_def = flags & 2;
2648 int is_friend = flags & 4;
2649 bool is_concept = flags & 8;
2650 int specialization = 0;
2651 int explicit_instantiation = 0;
2652 int member_specialization = 0;
2653 tree ctype = DECL_CLASS_CONTEXT (decl);
2654 tree dname = DECL_NAME (decl);
2655 tmpl_spec_kind tsk;
2657 if (is_friend)
2659 if (!processing_specialization)
2660 tsk = tsk_none;
2661 else
2662 tsk = tsk_excessive_parms;
2664 else
2665 tsk = current_tmpl_spec_kind (template_count);
2667 switch (tsk)
2669 case tsk_none:
2670 if (processing_specialization && !VAR_P (decl))
2672 specialization = 1;
2673 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2675 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2677 if (is_friend)
2678 /* This could be something like:
2680 template <class T> void f(T);
2681 class S { friend void f<>(int); } */
2682 specialization = 1;
2683 else
2685 /* This case handles bogus declarations like template <>
2686 template <class T> void f<int>(); */
2688 error ("template-id %qD in declaration of primary template",
2689 declarator);
2690 return decl;
2693 break;
2695 case tsk_invalid_member_spec:
2696 /* The error has already been reported in
2697 check_specialization_scope. */
2698 return error_mark_node;
2700 case tsk_invalid_expl_inst:
2701 error ("template parameter list used in explicit instantiation");
2703 /* Fall through. */
2705 case tsk_expl_inst:
2706 if (have_def)
2707 error ("definition provided for explicit instantiation");
2709 explicit_instantiation = 1;
2710 break;
2712 case tsk_excessive_parms:
2713 case tsk_insufficient_parms:
2714 if (tsk == tsk_excessive_parms)
2715 error ("too many template parameter lists in declaration of %qD",
2716 decl);
2717 else if (template_header_count)
2718 error("too few template parameter lists in declaration of %qD", decl);
2719 else
2720 error("explicit specialization of %qD must be introduced by "
2721 "%<template <>%>", decl);
2723 /* Fall through. */
2724 case tsk_expl_spec:
2725 if (is_concept)
2726 error ("explicit specialization declared %<concept%>");
2728 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2729 /* In cases like template<> constexpr bool v = true;
2730 We'll give an error in check_template_variable. */
2731 break;
2733 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2734 if (ctype)
2735 member_specialization = 1;
2736 else
2737 specialization = 1;
2738 break;
2740 case tsk_template:
2741 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2743 /* This case handles bogus declarations like template <>
2744 template <class T> void f<int>(); */
2746 if (!uses_template_parms (declarator))
2747 error ("template-id %qD in declaration of primary template",
2748 declarator);
2749 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2751 /* Partial specialization of variable template. */
2752 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2753 specialization = 1;
2754 goto ok;
2756 else if (cxx_dialect < cxx14)
2757 error ("non-type partial specialization %qD "
2758 "is not allowed", declarator);
2759 else
2760 error ("non-class, non-variable partial specialization %qD "
2761 "is not allowed", declarator);
2762 return decl;
2763 ok:;
2766 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2767 /* This is a specialization of a member template, without
2768 specialization the containing class. Something like:
2770 template <class T> struct S {
2771 template <class U> void f (U);
2773 template <> template <class U> void S<int>::f(U) {}
2775 That's a specialization -- but of the entire template. */
2776 specialization = 1;
2777 break;
2779 default:
2780 gcc_unreachable ();
2783 if ((specialization || member_specialization)
2784 /* This doesn't apply to variable templates. */
2785 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2786 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2788 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2789 for (; t; t = TREE_CHAIN (t))
2790 if (TREE_PURPOSE (t))
2792 permerror (input_location,
2793 "default argument specified in explicit specialization");
2794 break;
2798 if (specialization || member_specialization || explicit_instantiation)
2800 tree tmpl = NULL_TREE;
2801 tree targs = NULL_TREE;
2802 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2804 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2805 if (!was_template_id)
2807 tree fns;
2809 gcc_assert (identifier_p (declarator));
2810 if (ctype)
2811 fns = dname;
2812 else
2814 /* If there is no class context, the explicit instantiation
2815 must be at namespace scope. */
2816 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2818 /* Find the namespace binding, using the declaration
2819 context. */
2820 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2821 false, true);
2822 if (fns == error_mark_node)
2823 /* If lookup fails, look for a friend declaration so we can
2824 give a better diagnostic. */
2825 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2826 /*type*/false, /*complain*/true,
2827 /*hidden*/true);
2829 if (fns == error_mark_node || !is_overloaded_fn (fns))
2831 error ("%qD is not a template function", dname);
2832 fns = error_mark_node;
2836 declarator = lookup_template_function (fns, NULL_TREE);
2839 if (declarator == error_mark_node)
2840 return error_mark_node;
2842 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2844 if (!explicit_instantiation)
2845 /* A specialization in class scope. This is invalid,
2846 but the error will already have been flagged by
2847 check_specialization_scope. */
2848 return error_mark_node;
2849 else
2851 /* It's not valid to write an explicit instantiation in
2852 class scope, e.g.:
2854 class C { template void f(); }
2856 This case is caught by the parser. However, on
2857 something like:
2859 template class C { void f(); };
2861 (which is invalid) we can get here. The error will be
2862 issued later. */
2866 return decl;
2868 else if (ctype != NULL_TREE
2869 && (identifier_p (TREE_OPERAND (declarator, 0))))
2871 // We'll match variable templates in start_decl.
2872 if (VAR_P (decl))
2873 return decl;
2875 /* Find the list of functions in ctype that have the same
2876 name as the declared function. */
2877 tree name = TREE_OPERAND (declarator, 0);
2878 tree fns = NULL_TREE;
2879 int idx;
2881 if (constructor_name_p (name, ctype))
2883 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2885 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2886 : !CLASSTYPE_DESTRUCTORS (ctype))
2888 /* From [temp.expl.spec]:
2890 If such an explicit specialization for the member
2891 of a class template names an implicitly-declared
2892 special member function (clause _special_), the
2893 program is ill-formed.
2895 Similar language is found in [temp.explicit]. */
2896 error ("specialization of implicitly-declared special member function");
2897 return error_mark_node;
2900 name = is_constructor ? ctor_identifier : dtor_identifier;
2903 if (!DECL_CONV_FN_P (decl))
2905 idx = lookup_fnfields_1 (ctype, name);
2906 if (idx >= 0)
2907 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2909 else
2911 vec<tree, va_gc> *methods;
2912 tree ovl;
2914 /* For a type-conversion operator, we cannot do a
2915 name-based lookup. We might be looking for `operator
2916 int' which will be a specialization of `operator T'.
2917 So, we find *all* the conversion operators, and then
2918 select from them. */
2919 fns = NULL_TREE;
2921 methods = CLASSTYPE_METHOD_VEC (ctype);
2922 if (methods)
2923 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2924 methods->iterate (idx, &ovl);
2925 ++idx)
2927 if (!DECL_CONV_FN_P (OVL_FIRST (ovl)))
2928 /* There are no more conversion functions. */
2929 break;
2931 /* Glue all these conversion functions together
2932 with those we already have. */
2933 for (; ovl; ovl = OVL_NEXT (ovl))
2934 fns = lookup_add (OVL_CURRENT (ovl), fns);
2938 if (fns == NULL_TREE)
2940 error ("no member function %qD declared in %qT", name, ctype);
2941 return error_mark_node;
2943 else
2944 TREE_OPERAND (declarator, 0) = fns;
2947 /* Figure out what exactly is being specialized at this point.
2948 Note that for an explicit instantiation, even one for a
2949 member function, we cannot tell a priori whether the
2950 instantiation is for a member template, or just a member
2951 function of a template class. Even if a member template is
2952 being instantiated, the member template arguments may be
2953 elided if they can be deduced from the rest of the
2954 declaration. */
2955 tmpl = determine_specialization (declarator, decl,
2956 &targs,
2957 member_specialization,
2958 template_count,
2959 tsk);
2961 if (!tmpl || tmpl == error_mark_node)
2962 /* We couldn't figure out what this declaration was
2963 specializing. */
2964 return error_mark_node;
2965 else
2967 if (TREE_CODE (decl) == FUNCTION_DECL
2968 && DECL_HIDDEN_FRIEND_P (tmpl))
2970 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2971 "friend declaration %qD is not visible to "
2972 "explicit specialization", tmpl))
2973 inform (DECL_SOURCE_LOCATION (tmpl),
2974 "friend declaration here");
2976 else if (!ctype && !is_friend
2977 && CP_DECL_CONTEXT (decl) == current_namespace)
2978 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2980 tree gen_tmpl = most_general_template (tmpl);
2982 if (explicit_instantiation)
2984 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2985 is done by do_decl_instantiation later. */
2987 int arg_depth = TMPL_ARGS_DEPTH (targs);
2988 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2990 if (arg_depth > parm_depth)
2992 /* If TMPL is not the most general template (for
2993 example, if TMPL is a friend template that is
2994 injected into namespace scope), then there will
2995 be too many levels of TARGS. Remove some of them
2996 here. */
2997 int i;
2998 tree new_targs;
3000 new_targs = make_tree_vec (parm_depth);
3001 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3002 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3003 = TREE_VEC_ELT (targs, i);
3004 targs = new_targs;
3007 return instantiate_template (tmpl, targs, tf_error);
3010 /* If we thought that the DECL was a member function, but it
3011 turns out to be specializing a static member function,
3012 make DECL a static member function as well. */
3013 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3014 && DECL_STATIC_FUNCTION_P (tmpl)
3015 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3016 revert_static_member_fn (decl);
3018 /* If this is a specialization of a member template of a
3019 template class, we want to return the TEMPLATE_DECL, not
3020 the specialization of it. */
3021 if (tsk == tsk_template && !was_template_id)
3023 tree result = DECL_TEMPLATE_RESULT (tmpl);
3024 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3025 DECL_INITIAL (result) = NULL_TREE;
3026 if (have_def)
3028 tree parm;
3029 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3030 DECL_SOURCE_LOCATION (result)
3031 = DECL_SOURCE_LOCATION (decl);
3032 /* We want to use the argument list specified in the
3033 definition, not in the original declaration. */
3034 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3035 for (parm = DECL_ARGUMENTS (result); parm;
3036 parm = DECL_CHAIN (parm))
3037 DECL_CONTEXT (parm) = result;
3039 return register_specialization (tmpl, gen_tmpl, targs,
3040 is_friend, 0);
3043 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3044 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3046 if (was_template_id)
3047 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3049 /* Inherit default function arguments from the template
3050 DECL is specializing. */
3051 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3052 copy_default_args_to_explicit_spec (decl);
3054 /* This specialization has the same protection as the
3055 template it specializes. */
3056 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3057 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3059 /* 7.1.1-1 [dcl.stc]
3061 A storage-class-specifier shall not be specified in an
3062 explicit specialization...
3064 The parser rejects these, so unless action is taken here,
3065 explicit function specializations will always appear with
3066 global linkage.
3068 The action recommended by the C++ CWG in response to C++
3069 defect report 605 is to make the storage class and linkage
3070 of the explicit specialization match the templated function:
3072 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3074 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3076 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3077 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3079 /* A concept cannot be specialized. */
3080 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3082 error ("explicit specialization of function concept %qD",
3083 gen_tmpl);
3084 return error_mark_node;
3087 /* This specialization has the same linkage and visibility as
3088 the function template it specializes. */
3089 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3090 if (! TREE_PUBLIC (decl))
3092 DECL_INTERFACE_KNOWN (decl) = 1;
3093 DECL_NOT_REALLY_EXTERN (decl) = 1;
3095 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3096 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3098 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3099 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3103 /* If DECL is a friend declaration, declared using an
3104 unqualified name, the namespace associated with DECL may
3105 have been set incorrectly. For example, in:
3107 template <typename T> void f(T);
3108 namespace N {
3109 struct S { friend void f<int>(int); }
3112 we will have set the DECL_CONTEXT for the friend
3113 declaration to N, rather than to the global namespace. */
3114 if (DECL_NAMESPACE_SCOPE_P (decl))
3115 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3117 if (is_friend && !have_def)
3118 /* This is not really a declaration of a specialization.
3119 It's just the name of an instantiation. But, it's not
3120 a request for an instantiation, either. */
3121 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3122 else if (TREE_CODE (decl) == FUNCTION_DECL)
3123 /* A specialization is not necessarily COMDAT. */
3124 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3125 && DECL_DECLARED_INLINE_P (decl));
3126 else if (VAR_P (decl))
3127 DECL_COMDAT (decl) = false;
3129 /* If this is a full specialization, register it so that we can find
3130 it again. Partial specializations will be registered in
3131 process_partial_specialization. */
3132 if (!processing_template_decl)
3133 decl = register_specialization (decl, gen_tmpl, targs,
3134 is_friend, 0);
3136 /* A 'structor should already have clones. */
3137 gcc_assert (decl == error_mark_node
3138 || variable_template_p (tmpl)
3139 || !(DECL_CONSTRUCTOR_P (decl)
3140 || DECL_DESTRUCTOR_P (decl))
3141 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3145 return decl;
3148 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3149 parameters. These are represented in the same format used for
3150 DECL_TEMPLATE_PARMS. */
3153 comp_template_parms (const_tree parms1, const_tree parms2)
3155 const_tree p1;
3156 const_tree p2;
3158 if (parms1 == parms2)
3159 return 1;
3161 for (p1 = parms1, p2 = parms2;
3162 p1 != NULL_TREE && p2 != NULL_TREE;
3163 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3165 tree t1 = TREE_VALUE (p1);
3166 tree t2 = TREE_VALUE (p2);
3167 int i;
3169 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3170 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3172 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3173 return 0;
3175 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3177 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3178 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3180 /* If either of the template parameters are invalid, assume
3181 they match for the sake of error recovery. */
3182 if (error_operand_p (parm1) || error_operand_p (parm2))
3183 return 1;
3185 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3186 return 0;
3188 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3189 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3190 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3191 continue;
3192 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3193 return 0;
3197 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3198 /* One set of parameters has more parameters lists than the
3199 other. */
3200 return 0;
3202 return 1;
3205 /* Determine whether PARM is a parameter pack. */
3207 bool
3208 template_parameter_pack_p (const_tree parm)
3210 /* Determine if we have a non-type template parameter pack. */
3211 if (TREE_CODE (parm) == PARM_DECL)
3212 return (DECL_TEMPLATE_PARM_P (parm)
3213 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3214 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3215 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3217 /* If this is a list of template parameters, we could get a
3218 TYPE_DECL or a TEMPLATE_DECL. */
3219 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3220 parm = TREE_TYPE (parm);
3222 /* Otherwise it must be a type template parameter. */
3223 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3224 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3225 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3228 /* Determine if T is a function parameter pack. */
3230 bool
3231 function_parameter_pack_p (const_tree t)
3233 if (t && TREE_CODE (t) == PARM_DECL)
3234 return DECL_PACK_P (t);
3235 return false;
3238 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3239 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3241 tree
3242 get_function_template_decl (const_tree primary_func_tmpl_inst)
3244 if (! primary_func_tmpl_inst
3245 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3246 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3247 return NULL;
3249 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3252 /* Return true iff the function parameter PARAM_DECL was expanded
3253 from the function parameter pack PACK. */
3255 bool
3256 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3258 if (DECL_ARTIFICIAL (param_decl)
3259 || !function_parameter_pack_p (pack))
3260 return false;
3262 /* The parameter pack and its pack arguments have the same
3263 DECL_PARM_INDEX. */
3264 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3267 /* Determine whether ARGS describes a variadic template args list,
3268 i.e., one that is terminated by a template argument pack. */
3270 static bool
3271 template_args_variadic_p (tree args)
3273 int nargs;
3274 tree last_parm;
3276 if (args == NULL_TREE)
3277 return false;
3279 args = INNERMOST_TEMPLATE_ARGS (args);
3280 nargs = TREE_VEC_LENGTH (args);
3282 if (nargs == 0)
3283 return false;
3285 last_parm = TREE_VEC_ELT (args, nargs - 1);
3287 return ARGUMENT_PACK_P (last_parm);
3290 /* Generate a new name for the parameter pack name NAME (an
3291 IDENTIFIER_NODE) that incorporates its */
3293 static tree
3294 make_ith_pack_parameter_name (tree name, int i)
3296 /* Munge the name to include the parameter index. */
3297 #define NUMBUF_LEN 128
3298 char numbuf[NUMBUF_LEN];
3299 char* newname;
3300 int newname_len;
3302 if (name == NULL_TREE)
3303 return name;
3304 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3305 newname_len = IDENTIFIER_LENGTH (name)
3306 + strlen (numbuf) + 2;
3307 newname = (char*)alloca (newname_len);
3308 snprintf (newname, newname_len,
3309 "%s#%i", IDENTIFIER_POINTER (name), i);
3310 return get_identifier (newname);
3313 /* Return true if T is a primary function, class or alias template
3314 instantiation. */
3316 bool
3317 primary_template_instantiation_p (const_tree t)
3319 if (!t)
3320 return false;
3322 if (TREE_CODE (t) == FUNCTION_DECL)
3323 return DECL_LANG_SPECIFIC (t)
3324 && DECL_TEMPLATE_INSTANTIATION (t)
3325 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3326 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3327 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3328 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3329 else if (alias_template_specialization_p (t))
3330 return true;
3331 return false;
3334 /* Return true if PARM is a template template parameter. */
3336 bool
3337 template_template_parameter_p (const_tree parm)
3339 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3342 /* Return true iff PARM is a DECL representing a type template
3343 parameter. */
3345 bool
3346 template_type_parameter_p (const_tree parm)
3348 return (parm
3349 && (TREE_CODE (parm) == TYPE_DECL
3350 || TREE_CODE (parm) == TEMPLATE_DECL)
3351 && DECL_TEMPLATE_PARM_P (parm));
3354 /* Return the template parameters of T if T is a
3355 primary template instantiation, NULL otherwise. */
3357 tree
3358 get_primary_template_innermost_parameters (const_tree t)
3360 tree parms = NULL, template_info = NULL;
3362 if ((template_info = get_template_info (t))
3363 && primary_template_instantiation_p (t))
3364 parms = INNERMOST_TEMPLATE_PARMS
3365 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3367 return parms;
3370 /* Return the template parameters of the LEVELth level from the full list
3371 of template parameters PARMS. */
3373 tree
3374 get_template_parms_at_level (tree parms, int level)
3376 tree p;
3377 if (!parms
3378 || TREE_CODE (parms) != TREE_LIST
3379 || level > TMPL_PARMS_DEPTH (parms))
3380 return NULL_TREE;
3382 for (p = parms; p; p = TREE_CHAIN (p))
3383 if (TMPL_PARMS_DEPTH (p) == level)
3384 return p;
3386 return NULL_TREE;
3389 /* Returns the template arguments of T if T is a template instantiation,
3390 NULL otherwise. */
3392 tree
3393 get_template_innermost_arguments (const_tree t)
3395 tree args = NULL, template_info = NULL;
3397 if ((template_info = get_template_info (t))
3398 && TI_ARGS (template_info))
3399 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3401 return args;
3404 /* Return the argument pack elements of T if T is a template argument pack,
3405 NULL otherwise. */
3407 tree
3408 get_template_argument_pack_elems (const_tree t)
3410 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3411 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3412 return NULL;
3414 return ARGUMENT_PACK_ARGS (t);
3417 /* Structure used to track the progress of find_parameter_packs_r. */
3418 struct find_parameter_pack_data
3420 /* TREE_LIST that will contain all of the parameter packs found by
3421 the traversal. */
3422 tree* parameter_packs;
3424 /* Set of AST nodes that have been visited by the traversal. */
3425 hash_set<tree> *visited;
3427 /* True iff we're making a type pack expansion. */
3428 bool type_pack_expansion_p;
3431 /* Identifies all of the argument packs that occur in a template
3432 argument and appends them to the TREE_LIST inside DATA, which is a
3433 find_parameter_pack_data structure. This is a subroutine of
3434 make_pack_expansion and uses_parameter_packs. */
3435 static tree
3436 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3438 tree t = *tp;
3439 struct find_parameter_pack_data* ppd =
3440 (struct find_parameter_pack_data*)data;
3441 bool parameter_pack_p = false;
3443 /* Handle type aliases/typedefs. */
3444 if (TYPE_ALIAS_P (t))
3446 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3447 cp_walk_tree (&TI_ARGS (tinfo),
3448 &find_parameter_packs_r,
3449 ppd, ppd->visited);
3450 *walk_subtrees = 0;
3451 return NULL_TREE;
3454 /* Identify whether this is a parameter pack or not. */
3455 switch (TREE_CODE (t))
3457 case TEMPLATE_PARM_INDEX:
3458 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3459 parameter_pack_p = true;
3460 break;
3462 case TEMPLATE_TYPE_PARM:
3463 t = TYPE_MAIN_VARIANT (t);
3464 /* FALLTHRU */
3465 case TEMPLATE_TEMPLATE_PARM:
3466 /* If the placeholder appears in the decl-specifier-seq of a function
3467 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3468 is a pack expansion, the invented template parameter is a template
3469 parameter pack. */
3470 if (ppd->type_pack_expansion_p && is_auto (t))
3471 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3472 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3473 parameter_pack_p = true;
3474 break;
3476 case FIELD_DECL:
3477 case PARM_DECL:
3478 if (DECL_PACK_P (t))
3480 /* We don't want to walk into the type of a PARM_DECL,
3481 because we don't want to see the type parameter pack. */
3482 *walk_subtrees = 0;
3483 parameter_pack_p = true;
3485 break;
3487 /* Look through a lambda capture proxy to the field pack. */
3488 case VAR_DECL:
3489 if (DECL_HAS_VALUE_EXPR_P (t))
3491 tree v = DECL_VALUE_EXPR (t);
3492 cp_walk_tree (&v,
3493 &find_parameter_packs_r,
3494 ppd, ppd->visited);
3495 *walk_subtrees = 0;
3497 else if (variable_template_specialization_p (t))
3499 cp_walk_tree (&DECL_TI_ARGS (t),
3500 find_parameter_packs_r,
3501 ppd, ppd->visited);
3502 *walk_subtrees = 0;
3504 break;
3506 case BASES:
3507 parameter_pack_p = true;
3508 break;
3509 default:
3510 /* Not a parameter pack. */
3511 break;
3514 if (parameter_pack_p)
3516 /* Add this parameter pack to the list. */
3517 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3520 if (TYPE_P (t))
3521 cp_walk_tree (&TYPE_CONTEXT (t),
3522 &find_parameter_packs_r, ppd, ppd->visited);
3524 /* This switch statement will return immediately if we don't find a
3525 parameter pack. */
3526 switch (TREE_CODE (t))
3528 case TEMPLATE_PARM_INDEX:
3529 return NULL_TREE;
3531 case BOUND_TEMPLATE_TEMPLATE_PARM:
3532 /* Check the template itself. */
3533 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3534 &find_parameter_packs_r, ppd, ppd->visited);
3535 /* Check the template arguments. */
3536 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3537 ppd->visited);
3538 *walk_subtrees = 0;
3539 return NULL_TREE;
3541 case TEMPLATE_TYPE_PARM:
3542 case TEMPLATE_TEMPLATE_PARM:
3543 return NULL_TREE;
3545 case PARM_DECL:
3546 return NULL_TREE;
3548 case RECORD_TYPE:
3549 if (TYPE_PTRMEMFUNC_P (t))
3550 return NULL_TREE;
3551 /* Fall through. */
3553 case UNION_TYPE:
3554 case ENUMERAL_TYPE:
3555 if (TYPE_TEMPLATE_INFO (t))
3556 cp_walk_tree (&TYPE_TI_ARGS (t),
3557 &find_parameter_packs_r, ppd, ppd->visited);
3559 *walk_subtrees = 0;
3560 return NULL_TREE;
3562 case TEMPLATE_DECL:
3563 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3564 return NULL_TREE;
3565 gcc_fallthrough();
3567 case CONSTRUCTOR:
3568 cp_walk_tree (&TREE_TYPE (t),
3569 &find_parameter_packs_r, ppd, ppd->visited);
3570 return NULL_TREE;
3572 case TYPENAME_TYPE:
3573 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3574 ppd, ppd->visited);
3575 *walk_subtrees = 0;
3576 return NULL_TREE;
3578 case TYPE_PACK_EXPANSION:
3579 case EXPR_PACK_EXPANSION:
3580 *walk_subtrees = 0;
3581 return NULL_TREE;
3583 case INTEGER_TYPE:
3584 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3585 ppd, ppd->visited);
3586 *walk_subtrees = 0;
3587 return NULL_TREE;
3589 case IDENTIFIER_NODE:
3590 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3591 ppd->visited);
3592 *walk_subtrees = 0;
3593 return NULL_TREE;
3595 case DECLTYPE_TYPE:
3597 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3598 type_pack_expansion_p to false so that any placeholders
3599 within the expression don't get marked as parameter packs. */
3600 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3601 ppd->type_pack_expansion_p = false;
3602 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3603 ppd, ppd->visited);
3604 ppd->type_pack_expansion_p = type_pack_expansion_p;
3605 *walk_subtrees = 0;
3606 return NULL_TREE;
3609 default:
3610 return NULL_TREE;
3613 return NULL_TREE;
3616 /* Determines if the expression or type T uses any parameter packs. */
3617 bool
3618 uses_parameter_packs (tree t)
3620 tree parameter_packs = NULL_TREE;
3621 struct find_parameter_pack_data ppd;
3622 ppd.parameter_packs = &parameter_packs;
3623 ppd.visited = new hash_set<tree>;
3624 ppd.type_pack_expansion_p = false;
3625 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3626 delete ppd.visited;
3627 return parameter_packs != NULL_TREE;
3630 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3631 representation a base-class initializer into a parameter pack
3632 expansion. If all goes well, the resulting node will be an
3633 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3634 respectively. */
3635 tree
3636 make_pack_expansion (tree arg)
3638 tree result;
3639 tree parameter_packs = NULL_TREE;
3640 bool for_types = false;
3641 struct find_parameter_pack_data ppd;
3643 if (!arg || arg == error_mark_node)
3644 return arg;
3646 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3648 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3649 class initializer. In this case, the TREE_PURPOSE will be a
3650 _TYPE node (representing the base class expansion we're
3651 initializing) and the TREE_VALUE will be a TREE_LIST
3652 containing the initialization arguments.
3654 The resulting expansion looks somewhat different from most
3655 expansions. Rather than returning just one _EXPANSION, we
3656 return a TREE_LIST whose TREE_PURPOSE is a
3657 TYPE_PACK_EXPANSION containing the bases that will be
3658 initialized. The TREE_VALUE will be identical to the
3659 original TREE_VALUE, which is a list of arguments that will
3660 be passed to each base. We do not introduce any new pack
3661 expansion nodes into the TREE_VALUE (although it is possible
3662 that some already exist), because the TREE_PURPOSE and
3663 TREE_VALUE all need to be expanded together with the same
3664 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3665 resulting TREE_PURPOSE will mention the parameter packs in
3666 both the bases and the arguments to the bases. */
3667 tree purpose;
3668 tree value;
3669 tree parameter_packs = NULL_TREE;
3671 /* Determine which parameter packs will be used by the base
3672 class expansion. */
3673 ppd.visited = new hash_set<tree>;
3674 ppd.parameter_packs = &parameter_packs;
3675 ppd.type_pack_expansion_p = true;
3676 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3677 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3678 &ppd, ppd.visited);
3680 if (parameter_packs == NULL_TREE)
3682 error ("base initializer expansion %qT contains no parameter packs", arg);
3683 delete ppd.visited;
3684 return error_mark_node;
3687 if (TREE_VALUE (arg) != void_type_node)
3689 /* Collect the sets of parameter packs used in each of the
3690 initialization arguments. */
3691 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3693 /* Determine which parameter packs will be expanded in this
3694 argument. */
3695 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3696 &ppd, ppd.visited);
3700 delete ppd.visited;
3702 /* Create the pack expansion type for the base type. */
3703 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3704 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3705 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3707 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3708 they will rarely be compared to anything. */
3709 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3711 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3714 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3715 for_types = true;
3717 /* Build the PACK_EXPANSION_* node. */
3718 result = for_types
3719 ? cxx_make_type (TYPE_PACK_EXPANSION)
3720 : make_node (EXPR_PACK_EXPANSION);
3721 SET_PACK_EXPANSION_PATTERN (result, arg);
3722 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3724 /* Propagate type and const-expression information. */
3725 TREE_TYPE (result) = TREE_TYPE (arg);
3726 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3727 /* Mark this read now, since the expansion might be length 0. */
3728 mark_exp_read (arg);
3730 else
3731 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3732 they will rarely be compared to anything. */
3733 SET_TYPE_STRUCTURAL_EQUALITY (result);
3735 /* Determine which parameter packs will be expanded. */
3736 ppd.parameter_packs = &parameter_packs;
3737 ppd.visited = new hash_set<tree>;
3738 ppd.type_pack_expansion_p = TYPE_P (arg);
3739 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3740 delete ppd.visited;
3742 /* Make sure we found some parameter packs. */
3743 if (parameter_packs == NULL_TREE)
3745 if (TYPE_P (arg))
3746 error ("expansion pattern %qT contains no argument packs", arg);
3747 else
3748 error ("expansion pattern %qE contains no argument packs", arg);
3749 return error_mark_node;
3751 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3753 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3755 return result;
3758 /* Checks T for any "bare" parameter packs, which have not yet been
3759 expanded, and issues an error if any are found. This operation can
3760 only be done on full expressions or types (e.g., an expression
3761 statement, "if" condition, etc.), because we could have expressions like:
3763 foo(f(g(h(args)))...)
3765 where "args" is a parameter pack. check_for_bare_parameter_packs
3766 should not be called for the subexpressions args, h(args),
3767 g(h(args)), or f(g(h(args))), because we would produce erroneous
3768 error messages.
3770 Returns TRUE and emits an error if there were bare parameter packs,
3771 returns FALSE otherwise. */
3772 bool
3773 check_for_bare_parameter_packs (tree t)
3775 tree parameter_packs = NULL_TREE;
3776 struct find_parameter_pack_data ppd;
3778 if (!processing_template_decl || !t || t == error_mark_node)
3779 return false;
3781 if (TREE_CODE (t) == TYPE_DECL)
3782 t = TREE_TYPE (t);
3784 ppd.parameter_packs = &parameter_packs;
3785 ppd.visited = new hash_set<tree>;
3786 ppd.type_pack_expansion_p = false;
3787 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3788 delete ppd.visited;
3790 if (parameter_packs)
3792 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3793 error_at (loc, "parameter packs not expanded with %<...%>:");
3794 while (parameter_packs)
3796 tree pack = TREE_VALUE (parameter_packs);
3797 tree name = NULL_TREE;
3799 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3800 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3801 name = TYPE_NAME (pack);
3802 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3803 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3804 else
3805 name = DECL_NAME (pack);
3807 if (name)
3808 inform (loc, " %qD", name);
3809 else
3810 inform (loc, " <anonymous>");
3812 parameter_packs = TREE_CHAIN (parameter_packs);
3815 return true;
3818 return false;
3821 /* Expand any parameter packs that occur in the template arguments in
3822 ARGS. */
3823 tree
3824 expand_template_argument_pack (tree args)
3826 if (args == error_mark_node)
3827 return error_mark_node;
3829 tree result_args = NULL_TREE;
3830 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3831 int num_result_args = -1;
3832 int non_default_args_count = -1;
3834 /* First, determine if we need to expand anything, and the number of
3835 slots we'll need. */
3836 for (in_arg = 0; in_arg < nargs; ++in_arg)
3838 tree arg = TREE_VEC_ELT (args, in_arg);
3839 if (arg == NULL_TREE)
3840 return args;
3841 if (ARGUMENT_PACK_P (arg))
3843 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3844 if (num_result_args < 0)
3845 num_result_args = in_arg + num_packed;
3846 else
3847 num_result_args += num_packed;
3849 else
3851 if (num_result_args >= 0)
3852 num_result_args++;
3856 /* If no expansion is necessary, we're done. */
3857 if (num_result_args < 0)
3858 return args;
3860 /* Expand arguments. */
3861 result_args = make_tree_vec (num_result_args);
3862 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3863 non_default_args_count =
3864 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3865 for (in_arg = 0; in_arg < nargs; ++in_arg)
3867 tree arg = TREE_VEC_ELT (args, in_arg);
3868 if (ARGUMENT_PACK_P (arg))
3870 tree packed = ARGUMENT_PACK_ARGS (arg);
3871 int i, num_packed = TREE_VEC_LENGTH (packed);
3872 for (i = 0; i < num_packed; ++i, ++out_arg)
3873 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3874 if (non_default_args_count > 0)
3875 non_default_args_count += num_packed - 1;
3877 else
3879 TREE_VEC_ELT (result_args, out_arg) = arg;
3880 ++out_arg;
3883 if (non_default_args_count >= 0)
3884 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3885 return result_args;
3888 /* Checks if DECL shadows a template parameter.
3890 [temp.local]: A template-parameter shall not be redeclared within its
3891 scope (including nested scopes).
3893 Emits an error and returns TRUE if the DECL shadows a parameter,
3894 returns FALSE otherwise. */
3896 bool
3897 check_template_shadow (tree decl)
3899 tree olddecl;
3901 /* If we're not in a template, we can't possibly shadow a template
3902 parameter. */
3903 if (!current_template_parms)
3904 return true;
3906 /* Figure out what we're shadowing. */
3907 decl = OVL_FIRST (decl);
3908 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
3910 /* If there's no previous binding for this name, we're not shadowing
3911 anything, let alone a template parameter. */
3912 if (!olddecl)
3913 return true;
3915 /* If we're not shadowing a template parameter, we're done. Note
3916 that OLDDECL might be an OVERLOAD (or perhaps even an
3917 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3918 node. */
3919 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
3920 return true;
3922 /* We check for decl != olddecl to avoid bogus errors for using a
3923 name inside a class. We check TPFI to avoid duplicate errors for
3924 inline member templates. */
3925 if (decl == olddecl
3926 || (DECL_TEMPLATE_PARM_P (decl)
3927 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
3928 return true;
3930 /* Don't complain about the injected class name, as we've already
3931 complained about the class itself. */
3932 if (DECL_SELF_REFERENCE_P (decl))
3933 return false;
3935 if (DECL_TEMPLATE_PARM_P (decl))
3936 error ("declaration of template parameter %q+D shadows "
3937 "template parameter", decl);
3938 else
3939 error ("declaration of %q+#D shadows template parameter", decl);
3940 inform (DECL_SOURCE_LOCATION (olddecl),
3941 "template parameter %qD declared here", olddecl);
3942 return false;
3945 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
3946 ORIG_LEVEL, DECL, and TYPE. */
3948 static tree
3949 build_template_parm_index (int index,
3950 int level,
3951 int orig_level,
3952 tree decl,
3953 tree type)
3955 tree t = make_node (TEMPLATE_PARM_INDEX);
3956 TEMPLATE_PARM_IDX (t) = index;
3957 TEMPLATE_PARM_LEVEL (t) = level;
3958 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3959 TEMPLATE_PARM_DECL (t) = decl;
3960 TREE_TYPE (t) = type;
3961 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3962 TREE_READONLY (t) = TREE_READONLY (decl);
3964 return t;
3967 /* Find the canonical type parameter for the given template type
3968 parameter. Returns the canonical type parameter, which may be TYPE
3969 if no such parameter existed. */
3971 static tree
3972 canonical_type_parameter (tree type)
3974 tree list;
3975 int idx = TEMPLATE_TYPE_IDX (type);
3976 if (!canonical_template_parms)
3977 vec_alloc (canonical_template_parms, idx + 1);
3979 if (canonical_template_parms->length () <= (unsigned) idx)
3980 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
3982 list = (*canonical_template_parms)[idx];
3983 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
3984 list = TREE_CHAIN (list);
3986 if (list)
3987 return TREE_VALUE (list);
3988 else
3990 (*canonical_template_parms)[idx]
3991 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
3992 return type;
3996 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
3997 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
3998 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3999 new one is created. */
4001 static tree
4002 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4003 tsubst_flags_t complain)
4005 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4006 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4007 != TEMPLATE_PARM_LEVEL (index) - levels)
4008 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4010 tree orig_decl = TEMPLATE_PARM_DECL (index);
4011 tree decl, t;
4013 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4014 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4015 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4016 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4017 DECL_ARTIFICIAL (decl) = 1;
4018 SET_DECL_TEMPLATE_PARM_P (decl);
4020 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4021 TEMPLATE_PARM_LEVEL (index) - levels,
4022 TEMPLATE_PARM_ORIG_LEVEL (index),
4023 decl, type);
4024 TEMPLATE_PARM_DESCENDANTS (index) = t;
4025 TEMPLATE_PARM_PARAMETER_PACK (t)
4026 = TEMPLATE_PARM_PARAMETER_PACK (index);
4028 /* Template template parameters need this. */
4029 if (TREE_CODE (decl) == TEMPLATE_DECL)
4031 DECL_TEMPLATE_RESULT (decl)
4032 = build_decl (DECL_SOURCE_LOCATION (decl),
4033 TYPE_DECL, DECL_NAME (decl), type);
4034 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4035 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4036 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4040 return TEMPLATE_PARM_DESCENDANTS (index);
4043 /* Process information from new template parameter PARM and append it
4044 to the LIST being built. This new parameter is a non-type
4045 parameter iff IS_NON_TYPE is true. This new parameter is a
4046 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4047 is in PARM_LOC. */
4049 tree
4050 process_template_parm (tree list, location_t parm_loc, tree parm,
4051 bool is_non_type, bool is_parameter_pack)
4053 tree decl = 0;
4054 int idx = 0;
4056 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4057 tree defval = TREE_PURPOSE (parm);
4058 tree constr = TREE_TYPE (parm);
4060 if (list)
4062 tree p = tree_last (list);
4064 if (p && TREE_VALUE (p) != error_mark_node)
4066 p = TREE_VALUE (p);
4067 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4068 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4069 else
4070 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4073 ++idx;
4076 if (is_non_type)
4078 parm = TREE_VALUE (parm);
4080 SET_DECL_TEMPLATE_PARM_P (parm);
4082 if (TREE_TYPE (parm) != error_mark_node)
4084 /* [temp.param]
4086 The top-level cv-qualifiers on the template-parameter are
4087 ignored when determining its type. */
4088 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4089 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4090 TREE_TYPE (parm) = error_mark_node;
4091 else if (uses_parameter_packs (TREE_TYPE (parm))
4092 && !is_parameter_pack
4093 /* If we're in a nested template parameter list, the template
4094 template parameter could be a parameter pack. */
4095 && processing_template_parmlist == 1)
4097 /* This template parameter is not a parameter pack, but it
4098 should be. Complain about "bare" parameter packs. */
4099 check_for_bare_parameter_packs (TREE_TYPE (parm));
4101 /* Recover by calling this a parameter pack. */
4102 is_parameter_pack = true;
4106 /* A template parameter is not modifiable. */
4107 TREE_CONSTANT (parm) = 1;
4108 TREE_READONLY (parm) = 1;
4109 decl = build_decl (parm_loc,
4110 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4111 TREE_CONSTANT (decl) = 1;
4112 TREE_READONLY (decl) = 1;
4113 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4114 = build_template_parm_index (idx, processing_template_decl,
4115 processing_template_decl,
4116 decl, TREE_TYPE (parm));
4118 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4119 = is_parameter_pack;
4121 else
4123 tree t;
4124 parm = TREE_VALUE (TREE_VALUE (parm));
4126 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4128 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4129 /* This is for distinguishing between real templates and template
4130 template parameters */
4131 TREE_TYPE (parm) = t;
4132 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4133 decl = parm;
4135 else
4137 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4138 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4139 decl = build_decl (parm_loc,
4140 TYPE_DECL, parm, t);
4143 TYPE_NAME (t) = decl;
4144 TYPE_STUB_DECL (t) = decl;
4145 parm = decl;
4146 TEMPLATE_TYPE_PARM_INDEX (t)
4147 = build_template_parm_index (idx, processing_template_decl,
4148 processing_template_decl,
4149 decl, TREE_TYPE (parm));
4150 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4151 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4153 DECL_ARTIFICIAL (decl) = 1;
4154 SET_DECL_TEMPLATE_PARM_P (decl);
4156 /* Build requirements for the type/template parameter.
4157 This must be done after SET_DECL_TEMPLATE_PARM_P or
4158 process_template_parm could fail. */
4159 tree reqs = finish_shorthand_constraint (parm, constr);
4161 pushdecl (decl);
4163 /* Build the parameter node linking the parameter declaration,
4164 its default argument (if any), and its constraints (if any). */
4165 parm = build_tree_list (defval, parm);
4166 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4168 return chainon (list, parm);
4171 /* The end of a template parameter list has been reached. Process the
4172 tree list into a parameter vector, converting each parameter into a more
4173 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4174 as PARM_DECLs. */
4176 tree
4177 end_template_parm_list (tree parms)
4179 int nparms;
4180 tree parm, next;
4181 tree saved_parmlist = make_tree_vec (list_length (parms));
4183 /* Pop the dummy parameter level and add the real one. */
4184 current_template_parms = TREE_CHAIN (current_template_parms);
4186 current_template_parms
4187 = tree_cons (size_int (processing_template_decl),
4188 saved_parmlist, current_template_parms);
4190 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4192 next = TREE_CHAIN (parm);
4193 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4194 TREE_CHAIN (parm) = NULL_TREE;
4197 --processing_template_parmlist;
4199 return saved_parmlist;
4202 // Explicitly indicate the end of the template parameter list. We assume
4203 // that the current template parameters have been constructed and/or
4204 // managed explicitly, as when creating new template template parameters
4205 // from a shorthand constraint.
4206 void
4207 end_template_parm_list ()
4209 --processing_template_parmlist;
4212 /* end_template_decl is called after a template declaration is seen. */
4214 void
4215 end_template_decl (void)
4217 reset_specialization ();
4219 if (! processing_template_decl)
4220 return;
4222 /* This matches the pushlevel in begin_template_parm_list. */
4223 finish_scope ();
4225 --processing_template_decl;
4226 current_template_parms = TREE_CHAIN (current_template_parms);
4229 /* Takes a TREE_LIST representing a template parameter and convert it
4230 into an argument suitable to be passed to the type substitution
4231 functions. Note that If the TREE_LIST contains an error_mark
4232 node, the returned argument is error_mark_node. */
4234 tree
4235 template_parm_to_arg (tree t)
4238 if (t == NULL_TREE
4239 || TREE_CODE (t) != TREE_LIST)
4240 return t;
4242 if (error_operand_p (TREE_VALUE (t)))
4243 return error_mark_node;
4245 t = TREE_VALUE (t);
4247 if (TREE_CODE (t) == TYPE_DECL
4248 || TREE_CODE (t) == TEMPLATE_DECL)
4250 t = TREE_TYPE (t);
4252 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4254 /* Turn this argument into a TYPE_ARGUMENT_PACK
4255 with a single element, which expands T. */
4256 tree vec = make_tree_vec (1);
4257 if (CHECKING_P)
4258 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4260 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4262 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4263 SET_ARGUMENT_PACK_ARGS (t, vec);
4266 else
4268 t = DECL_INITIAL (t);
4270 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4272 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4273 with a single element, which expands T. */
4274 tree vec = make_tree_vec (1);
4275 if (CHECKING_P)
4276 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4278 t = convert_from_reference (t);
4279 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4281 t = make_node (NONTYPE_ARGUMENT_PACK);
4282 SET_ARGUMENT_PACK_ARGS (t, vec);
4284 else
4285 t = convert_from_reference (t);
4287 return t;
4290 /* Given a single level of template parameters (a TREE_VEC), return it
4291 as a set of template arguments. */
4293 static tree
4294 template_parms_level_to_args (tree parms)
4296 tree a = copy_node (parms);
4297 TREE_TYPE (a) = NULL_TREE;
4298 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4299 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4301 if (CHECKING_P)
4302 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4304 return a;
4307 /* Given a set of template parameters, return them as a set of template
4308 arguments. The template parameters are represented as a TREE_VEC, in
4309 the form documented in cp-tree.h for template arguments. */
4311 static tree
4312 template_parms_to_args (tree parms)
4314 tree header;
4315 tree args = NULL_TREE;
4316 int length = TMPL_PARMS_DEPTH (parms);
4317 int l = length;
4319 /* If there is only one level of template parameters, we do not
4320 create a TREE_VEC of TREE_VECs. Instead, we return a single
4321 TREE_VEC containing the arguments. */
4322 if (length > 1)
4323 args = make_tree_vec (length);
4325 for (header = parms; header; header = TREE_CHAIN (header))
4327 tree a = template_parms_level_to_args (TREE_VALUE (header));
4329 if (length > 1)
4330 TREE_VEC_ELT (args, --l) = a;
4331 else
4332 args = a;
4335 return args;
4338 /* Within the declaration of a template, return the currently active
4339 template parameters as an argument TREE_VEC. */
4341 static tree
4342 current_template_args (void)
4344 return template_parms_to_args (current_template_parms);
4347 /* Update the declared TYPE by doing any lookups which were thought to be
4348 dependent, but are not now that we know the SCOPE of the declarator. */
4350 tree
4351 maybe_update_decl_type (tree orig_type, tree scope)
4353 tree type = orig_type;
4355 if (type == NULL_TREE)
4356 return type;
4358 if (TREE_CODE (orig_type) == TYPE_DECL)
4359 type = TREE_TYPE (type);
4361 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4362 && dependent_type_p (type)
4363 /* Don't bother building up the args in this case. */
4364 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4366 /* tsubst in the args corresponding to the template parameters,
4367 including auto if present. Most things will be unchanged, but
4368 make_typename_type and tsubst_qualified_id will resolve
4369 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4370 tree args = current_template_args ();
4371 tree auto_node = type_uses_auto (type);
4372 tree pushed;
4373 if (auto_node)
4375 tree auto_vec = make_tree_vec (1);
4376 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4377 args = add_to_template_args (args, auto_vec);
4379 pushed = push_scope (scope);
4380 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4381 if (pushed)
4382 pop_scope (scope);
4385 if (type == error_mark_node)
4386 return orig_type;
4388 if (TREE_CODE (orig_type) == TYPE_DECL)
4390 if (same_type_p (type, TREE_TYPE (orig_type)))
4391 type = orig_type;
4392 else
4393 type = TYPE_NAME (type);
4395 return type;
4398 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4399 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4400 the new template is a member template. */
4402 tree
4403 build_template_decl (tree decl, tree parms, bool member_template_p)
4405 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4406 DECL_TEMPLATE_PARMS (tmpl) = parms;
4407 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4408 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4409 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4411 return tmpl;
4414 struct template_parm_data
4416 /* The level of the template parameters we are currently
4417 processing. */
4418 int level;
4420 /* The index of the specialization argument we are currently
4421 processing. */
4422 int current_arg;
4424 /* An array whose size is the number of template parameters. The
4425 elements are nonzero if the parameter has been used in any one
4426 of the arguments processed so far. */
4427 int* parms;
4429 /* An array whose size is the number of template arguments. The
4430 elements are nonzero if the argument makes use of template
4431 parameters of this level. */
4432 int* arg_uses_template_parms;
4435 /* Subroutine of push_template_decl used to see if each template
4436 parameter in a partial specialization is used in the explicit
4437 argument list. If T is of the LEVEL given in DATA (which is
4438 treated as a template_parm_data*), then DATA->PARMS is marked
4439 appropriately. */
4441 static int
4442 mark_template_parm (tree t, void* data)
4444 int level;
4445 int idx;
4446 struct template_parm_data* tpd = (struct template_parm_data*) data;
4448 template_parm_level_and_index (t, &level, &idx);
4450 if (level == tpd->level)
4452 tpd->parms[idx] = 1;
4453 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4456 /* In C++17 the type of a non-type argument is a deduced context. */
4457 if (cxx_dialect >= cxx1z
4458 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4459 for_each_template_parm (TREE_TYPE (t),
4460 &mark_template_parm,
4461 data,
4462 NULL,
4463 /*include_nondeduced_p=*/false);
4465 /* Return zero so that for_each_template_parm will continue the
4466 traversal of the tree; we want to mark *every* template parm. */
4467 return 0;
4470 /* Process the partial specialization DECL. */
4472 static tree
4473 process_partial_specialization (tree decl)
4475 tree type = TREE_TYPE (decl);
4476 tree tinfo = get_template_info (decl);
4477 tree maintmpl = TI_TEMPLATE (tinfo);
4478 tree specargs = TI_ARGS (tinfo);
4479 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4480 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4481 tree inner_parms;
4482 tree inst;
4483 int nargs = TREE_VEC_LENGTH (inner_args);
4484 int ntparms;
4485 int i;
4486 bool did_error_intro = false;
4487 struct template_parm_data tpd;
4488 struct template_parm_data tpd2;
4490 gcc_assert (current_template_parms);
4492 /* A concept cannot be specialized. */
4493 if (flag_concepts && variable_concept_p (maintmpl))
4495 error ("specialization of variable concept %q#D", maintmpl);
4496 return error_mark_node;
4499 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4500 ntparms = TREE_VEC_LENGTH (inner_parms);
4502 /* We check that each of the template parameters given in the
4503 partial specialization is used in the argument list to the
4504 specialization. For example:
4506 template <class T> struct S;
4507 template <class T> struct S<T*>;
4509 The second declaration is OK because `T*' uses the template
4510 parameter T, whereas
4512 template <class T> struct S<int>;
4514 is no good. Even trickier is:
4516 template <class T>
4517 struct S1
4519 template <class U>
4520 struct S2;
4521 template <class U>
4522 struct S2<T>;
4525 The S2<T> declaration is actually invalid; it is a
4526 full-specialization. Of course,
4528 template <class U>
4529 struct S2<T (*)(U)>;
4531 or some such would have been OK. */
4532 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4533 tpd.parms = XALLOCAVEC (int, ntparms);
4534 memset (tpd.parms, 0, sizeof (int) * ntparms);
4536 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4537 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4538 for (i = 0; i < nargs; ++i)
4540 tpd.current_arg = i;
4541 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4542 &mark_template_parm,
4543 &tpd,
4544 NULL,
4545 /*include_nondeduced_p=*/false);
4547 for (i = 0; i < ntparms; ++i)
4548 if (tpd.parms[i] == 0)
4550 /* One of the template parms was not used in a deduced context in the
4551 specialization. */
4552 if (!did_error_intro)
4554 error ("template parameters not deducible in "
4555 "partial specialization:");
4556 did_error_intro = true;
4559 inform (input_location, " %qD",
4560 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4563 if (did_error_intro)
4564 return error_mark_node;
4566 /* [temp.class.spec]
4568 The argument list of the specialization shall not be identical to
4569 the implicit argument list of the primary template. */
4570 tree main_args
4571 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4572 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4573 && (!flag_concepts
4574 || !strictly_subsumes (current_template_constraints (),
4575 get_constraints (maintmpl))))
4577 if (!flag_concepts)
4578 error ("partial specialization %q+D does not specialize "
4579 "any template arguments", decl);
4580 else
4581 error ("partial specialization %q+D does not specialize any "
4582 "template arguments and is not more constrained than", decl);
4583 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4586 /* A partial specialization that replaces multiple parameters of the
4587 primary template with a pack expansion is less specialized for those
4588 parameters. */
4589 if (nargs < DECL_NTPARMS (maintmpl))
4591 error ("partial specialization is not more specialized than the "
4592 "primary template because it replaces multiple parameters "
4593 "with a pack expansion");
4594 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4595 /* Avoid crash in process_partial_specialization. */
4596 return decl;
4599 /* If we aren't in a dependent class, we can actually try deduction. */
4600 else if (tpd.level == 1
4601 /* FIXME we should be able to handle a partial specialization of a
4602 partial instantiation, but currently we can't (c++/41727). */
4603 && TMPL_ARGS_DEPTH (specargs) == 1
4604 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4606 if (permerror (input_location, "partial specialization %qD is not "
4607 "more specialized than", decl))
4608 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4609 maintmpl);
4612 /* [temp.class.spec]
4614 A partially specialized non-type argument expression shall not
4615 involve template parameters of the partial specialization except
4616 when the argument expression is a simple identifier.
4618 The type of a template parameter corresponding to a specialized
4619 non-type argument shall not be dependent on a parameter of the
4620 specialization.
4622 Also, we verify that pack expansions only occur at the
4623 end of the argument list. */
4624 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4625 tpd2.parms = 0;
4626 for (i = 0; i < nargs; ++i)
4628 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4629 tree arg = TREE_VEC_ELT (inner_args, i);
4630 tree packed_args = NULL_TREE;
4631 int j, len = 1;
4633 if (ARGUMENT_PACK_P (arg))
4635 /* Extract the arguments from the argument pack. We'll be
4636 iterating over these in the following loop. */
4637 packed_args = ARGUMENT_PACK_ARGS (arg);
4638 len = TREE_VEC_LENGTH (packed_args);
4641 for (j = 0; j < len; j++)
4643 if (packed_args)
4644 /* Get the Jth argument in the parameter pack. */
4645 arg = TREE_VEC_ELT (packed_args, j);
4647 if (PACK_EXPANSION_P (arg))
4649 /* Pack expansions must come at the end of the
4650 argument list. */
4651 if ((packed_args && j < len - 1)
4652 || (!packed_args && i < nargs - 1))
4654 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4655 error ("parameter pack argument %qE must be at the "
4656 "end of the template argument list", arg);
4657 else
4658 error ("parameter pack argument %qT must be at the "
4659 "end of the template argument list", arg);
4663 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4664 /* We only care about the pattern. */
4665 arg = PACK_EXPANSION_PATTERN (arg);
4667 if (/* These first two lines are the `non-type' bit. */
4668 !TYPE_P (arg)
4669 && TREE_CODE (arg) != TEMPLATE_DECL
4670 /* This next two lines are the `argument expression is not just a
4671 simple identifier' condition and also the `specialized
4672 non-type argument' bit. */
4673 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4674 && !(REFERENCE_REF_P (arg)
4675 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4677 if ((!packed_args && tpd.arg_uses_template_parms[i])
4678 || (packed_args && uses_template_parms (arg)))
4679 error ("template argument %qE involves template parameter(s)",
4680 arg);
4681 else
4683 /* Look at the corresponding template parameter,
4684 marking which template parameters its type depends
4685 upon. */
4686 tree type = TREE_TYPE (parm);
4688 if (!tpd2.parms)
4690 /* We haven't yet initialized TPD2. Do so now. */
4691 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4692 /* The number of parameters here is the number in the
4693 main template, which, as checked in the assertion
4694 above, is NARGS. */
4695 tpd2.parms = XALLOCAVEC (int, nargs);
4696 tpd2.level =
4697 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4700 /* Mark the template parameters. But this time, we're
4701 looking for the template parameters of the main
4702 template, not in the specialization. */
4703 tpd2.current_arg = i;
4704 tpd2.arg_uses_template_parms[i] = 0;
4705 memset (tpd2.parms, 0, sizeof (int) * nargs);
4706 for_each_template_parm (type,
4707 &mark_template_parm,
4708 &tpd2,
4709 NULL,
4710 /*include_nondeduced_p=*/false);
4712 if (tpd2.arg_uses_template_parms [i])
4714 /* The type depended on some template parameters.
4715 If they are fully specialized in the
4716 specialization, that's OK. */
4717 int j;
4718 int count = 0;
4719 for (j = 0; j < nargs; ++j)
4720 if (tpd2.parms[j] != 0
4721 && tpd.arg_uses_template_parms [j])
4722 ++count;
4723 if (count != 0)
4724 error_n (input_location, count,
4725 "type %qT of template argument %qE depends "
4726 "on a template parameter",
4727 "type %qT of template argument %qE depends "
4728 "on template parameters",
4729 type,
4730 arg);
4737 /* We should only get here once. */
4738 if (TREE_CODE (decl) == TYPE_DECL)
4739 gcc_assert (!COMPLETE_TYPE_P (type));
4741 // Build the template decl.
4742 tree tmpl = build_template_decl (decl, current_template_parms,
4743 DECL_MEMBER_TEMPLATE_P (maintmpl));
4744 TREE_TYPE (tmpl) = type;
4745 DECL_TEMPLATE_RESULT (tmpl) = decl;
4746 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4747 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4748 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4750 /* Give template template parms a DECL_CONTEXT of the template
4751 for which they are a parameter. */
4752 for (i = 0; i < ntparms; ++i)
4754 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4755 if (TREE_CODE (parm) == TEMPLATE_DECL)
4756 DECL_CONTEXT (parm) = tmpl;
4759 if (VAR_P (decl))
4760 /* We didn't register this in check_explicit_specialization so we could
4761 wait until the constraints were set. */
4762 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4763 else
4764 associate_classtype_constraints (type);
4766 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4767 = tree_cons (specargs, tmpl,
4768 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4769 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4771 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4772 inst = TREE_CHAIN (inst))
4774 tree instance = TREE_VALUE (inst);
4775 if (TYPE_P (instance)
4776 ? (COMPLETE_TYPE_P (instance)
4777 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4778 : DECL_TEMPLATE_INSTANTIATION (instance))
4780 tree spec = most_specialized_partial_spec (instance, tf_none);
4781 tree inst_decl = (DECL_P (instance)
4782 ? instance : TYPE_NAME (instance));
4783 if (!spec)
4784 /* OK */;
4785 else if (spec == error_mark_node)
4786 permerror (input_location,
4787 "declaration of %qD ambiguates earlier template "
4788 "instantiation for %qD", decl, inst_decl);
4789 else if (TREE_VALUE (spec) == tmpl)
4790 permerror (input_location,
4791 "partial specialization of %qD after instantiation "
4792 "of %qD", decl, inst_decl);
4796 return decl;
4799 /* PARM is a template parameter of some form; return the corresponding
4800 TEMPLATE_PARM_INDEX. */
4802 static tree
4803 get_template_parm_index (tree parm)
4805 if (TREE_CODE (parm) == PARM_DECL
4806 || TREE_CODE (parm) == CONST_DECL)
4807 parm = DECL_INITIAL (parm);
4808 else if (TREE_CODE (parm) == TYPE_DECL
4809 || TREE_CODE (parm) == TEMPLATE_DECL)
4810 parm = TREE_TYPE (parm);
4811 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4812 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4813 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4814 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4815 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4816 return parm;
4819 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4820 parameter packs used by the template parameter PARM. */
4822 static void
4823 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4825 /* A type parm can't refer to another parm. */
4826 if (TREE_CODE (parm) == TYPE_DECL)
4827 return;
4828 else if (TREE_CODE (parm) == PARM_DECL)
4830 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4831 ppd, ppd->visited);
4832 return;
4835 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4837 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4838 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4839 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4842 /* PARM is a template parameter pack. Return any parameter packs used in
4843 its type or the type of any of its template parameters. If there are
4844 any such packs, it will be instantiated into a fixed template parameter
4845 list by partial instantiation rather than be fully deduced. */
4847 tree
4848 fixed_parameter_pack_p (tree parm)
4850 /* This can only be true in a member template. */
4851 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4852 return NULL_TREE;
4853 /* This can only be true for a parameter pack. */
4854 if (!template_parameter_pack_p (parm))
4855 return NULL_TREE;
4856 /* A type parm can't refer to another parm. */
4857 if (TREE_CODE (parm) == TYPE_DECL)
4858 return NULL_TREE;
4860 tree parameter_packs = NULL_TREE;
4861 struct find_parameter_pack_data ppd;
4862 ppd.parameter_packs = &parameter_packs;
4863 ppd.visited = new hash_set<tree>;
4864 ppd.type_pack_expansion_p = false;
4866 fixed_parameter_pack_p_1 (parm, &ppd);
4868 delete ppd.visited;
4869 return parameter_packs;
4872 /* Check that a template declaration's use of default arguments and
4873 parameter packs is not invalid. Here, PARMS are the template
4874 parameters. IS_PRIMARY is true if DECL is the thing declared by
4875 a primary template. IS_PARTIAL is true if DECL is a partial
4876 specialization.
4878 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4879 declaration (but not a definition); 1 indicates a declaration, 2
4880 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4881 emitted for extraneous default arguments.
4883 Returns TRUE if there were no errors found, FALSE otherwise. */
4885 bool
4886 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4887 bool is_partial, int is_friend_decl)
4889 const char *msg;
4890 int last_level_to_check;
4891 tree parm_level;
4892 bool no_errors = true;
4894 /* [temp.param]
4896 A default template-argument shall not be specified in a
4897 function template declaration or a function template definition, nor
4898 in the template-parameter-list of the definition of a member of a
4899 class template. */
4901 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4902 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
4903 /* You can't have a function template declaration in a local
4904 scope, nor you can you define a member of a class template in a
4905 local scope. */
4906 return true;
4908 if ((TREE_CODE (decl) == TYPE_DECL
4909 && TREE_TYPE (decl)
4910 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
4911 || (TREE_CODE (decl) == FUNCTION_DECL
4912 && LAMBDA_FUNCTION_P (decl)))
4913 /* A lambda doesn't have an explicit declaration; don't complain
4914 about the parms of the enclosing class. */
4915 return true;
4917 if (current_class_type
4918 && !TYPE_BEING_DEFINED (current_class_type)
4919 && DECL_LANG_SPECIFIC (decl)
4920 && DECL_DECLARES_FUNCTION_P (decl)
4921 /* If this is either a friend defined in the scope of the class
4922 or a member function. */
4923 && (DECL_FUNCTION_MEMBER_P (decl)
4924 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
4925 : DECL_FRIEND_CONTEXT (decl)
4926 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
4927 : false)
4928 /* And, if it was a member function, it really was defined in
4929 the scope of the class. */
4930 && (!DECL_FUNCTION_MEMBER_P (decl)
4931 || DECL_INITIALIZED_IN_CLASS_P (decl)))
4932 /* We already checked these parameters when the template was
4933 declared, so there's no need to do it again now. This function
4934 was defined in class scope, but we're processing its body now
4935 that the class is complete. */
4936 return true;
4938 /* Core issue 226 (C++0x only): the following only applies to class
4939 templates. */
4940 if (is_primary
4941 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
4943 /* [temp.param]
4945 If a template-parameter has a default template-argument, all
4946 subsequent template-parameters shall have a default
4947 template-argument supplied. */
4948 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
4950 tree inner_parms = TREE_VALUE (parm_level);
4951 int ntparms = TREE_VEC_LENGTH (inner_parms);
4952 int seen_def_arg_p = 0;
4953 int i;
4955 for (i = 0; i < ntparms; ++i)
4957 tree parm = TREE_VEC_ELT (inner_parms, i);
4959 if (parm == error_mark_node)
4960 continue;
4962 if (TREE_PURPOSE (parm))
4963 seen_def_arg_p = 1;
4964 else if (seen_def_arg_p
4965 && !template_parameter_pack_p (TREE_VALUE (parm)))
4967 error ("no default argument for %qD", TREE_VALUE (parm));
4968 /* For better subsequent error-recovery, we indicate that
4969 there should have been a default argument. */
4970 TREE_PURPOSE (parm) = error_mark_node;
4971 no_errors = false;
4973 else if (!is_partial
4974 && !is_friend_decl
4975 /* Don't complain about an enclosing partial
4976 specialization. */
4977 && parm_level == parms
4978 && TREE_CODE (decl) == TYPE_DECL
4979 && i < ntparms - 1
4980 && template_parameter_pack_p (TREE_VALUE (parm))
4981 /* A fixed parameter pack will be partially
4982 instantiated into a fixed length list. */
4983 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
4985 /* A primary class template can only have one
4986 parameter pack, at the end of the template
4987 parameter list. */
4989 error ("parameter pack %q+D must be at the end of the"
4990 " template parameter list", TREE_VALUE (parm));
4992 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
4993 = error_mark_node;
4994 no_errors = false;
5000 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5001 || is_partial
5002 || !is_primary
5003 || is_friend_decl)
5004 /* For an ordinary class template, default template arguments are
5005 allowed at the innermost level, e.g.:
5006 template <class T = int>
5007 struct S {};
5008 but, in a partial specialization, they're not allowed even
5009 there, as we have in [temp.class.spec]:
5011 The template parameter list of a specialization shall not
5012 contain default template argument values.
5014 So, for a partial specialization, or for a function template
5015 (in C++98/C++03), we look at all of them. */
5017 else
5018 /* But, for a primary class template that is not a partial
5019 specialization we look at all template parameters except the
5020 innermost ones. */
5021 parms = TREE_CHAIN (parms);
5023 /* Figure out what error message to issue. */
5024 if (is_friend_decl == 2)
5025 msg = G_("default template arguments may not be used in function template "
5026 "friend re-declaration");
5027 else if (is_friend_decl)
5028 msg = G_("default template arguments may not be used in function template "
5029 "friend declarations");
5030 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5031 msg = G_("default template arguments may not be used in function templates "
5032 "without -std=c++11 or -std=gnu++11");
5033 else if (is_partial)
5034 msg = G_("default template arguments may not be used in "
5035 "partial specializations");
5036 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5037 msg = G_("default argument for template parameter for class enclosing %qD");
5038 else
5039 /* Per [temp.param]/9, "A default template-argument shall not be
5040 specified in the template-parameter-lists of the definition of
5041 a member of a class template that appears outside of the member's
5042 class.", thus if we aren't handling a member of a class template
5043 there is no need to examine the parameters. */
5044 return true;
5046 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5047 /* If we're inside a class definition, there's no need to
5048 examine the parameters to the class itself. On the one
5049 hand, they will be checked when the class is defined, and,
5050 on the other, default arguments are valid in things like:
5051 template <class T = double>
5052 struct S { template <class U> void f(U); };
5053 Here the default argument for `S' has no bearing on the
5054 declaration of `f'. */
5055 last_level_to_check = template_class_depth (current_class_type) + 1;
5056 else
5057 /* Check everything. */
5058 last_level_to_check = 0;
5060 for (parm_level = parms;
5061 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5062 parm_level = TREE_CHAIN (parm_level))
5064 tree inner_parms = TREE_VALUE (parm_level);
5065 int i;
5066 int ntparms;
5068 ntparms = TREE_VEC_LENGTH (inner_parms);
5069 for (i = 0; i < ntparms; ++i)
5071 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5072 continue;
5074 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5076 if (msg)
5078 no_errors = false;
5079 if (is_friend_decl == 2)
5080 return no_errors;
5082 error (msg, decl);
5083 msg = 0;
5086 /* Clear out the default argument so that we are not
5087 confused later. */
5088 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5092 /* At this point, if we're still interested in issuing messages,
5093 they must apply to classes surrounding the object declared. */
5094 if (msg)
5095 msg = G_("default argument for template parameter for class "
5096 "enclosing %qD");
5099 return no_errors;
5102 /* Worker for push_template_decl_real, called via
5103 for_each_template_parm. DATA is really an int, indicating the
5104 level of the parameters we are interested in. If T is a template
5105 parameter of that level, return nonzero. */
5107 static int
5108 template_parm_this_level_p (tree t, void* data)
5110 int this_level = *(int *)data;
5111 int level;
5113 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5114 level = TEMPLATE_PARM_LEVEL (t);
5115 else
5116 level = TEMPLATE_TYPE_LEVEL (t);
5117 return level == this_level;
5120 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5121 DATA is really an int, indicating the innermost outer level of parameters.
5122 If T is a template parameter of that level or further out, return
5123 nonzero. */
5125 static int
5126 template_parm_outer_level (tree t, void *data)
5128 int this_level = *(int *)data;
5129 int level;
5131 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5132 level = TEMPLATE_PARM_LEVEL (t);
5133 else
5134 level = TEMPLATE_TYPE_LEVEL (t);
5135 return level <= this_level;
5138 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5139 parameters given by current_template_args, or reuses a
5140 previously existing one, if appropriate. Returns the DECL, or an
5141 equivalent one, if it is replaced via a call to duplicate_decls.
5143 If IS_FRIEND is true, DECL is a friend declaration. */
5145 tree
5146 push_template_decl_real (tree decl, bool is_friend)
5148 tree tmpl;
5149 tree args;
5150 tree info;
5151 tree ctx;
5152 bool is_primary;
5153 bool is_partial;
5154 int new_template_p = 0;
5155 /* True if the template is a member template, in the sense of
5156 [temp.mem]. */
5157 bool member_template_p = false;
5159 if (decl == error_mark_node || !current_template_parms)
5160 return error_mark_node;
5162 /* See if this is a partial specialization. */
5163 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5164 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5165 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5166 || (VAR_P (decl)
5167 && DECL_LANG_SPECIFIC (decl)
5168 && DECL_TEMPLATE_SPECIALIZATION (decl)
5169 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5171 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5172 is_friend = true;
5174 if (is_friend)
5175 /* For a friend, we want the context of the friend function, not
5176 the type of which it is a friend. */
5177 ctx = CP_DECL_CONTEXT (decl);
5178 else if (CP_DECL_CONTEXT (decl)
5179 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5180 /* In the case of a virtual function, we want the class in which
5181 it is defined. */
5182 ctx = CP_DECL_CONTEXT (decl);
5183 else
5184 /* Otherwise, if we're currently defining some class, the DECL
5185 is assumed to be a member of the class. */
5186 ctx = current_scope ();
5188 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5189 ctx = NULL_TREE;
5191 if (!DECL_CONTEXT (decl))
5192 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5194 /* See if this is a primary template. */
5195 if (is_friend && ctx
5196 && uses_template_parms_level (ctx, processing_template_decl))
5197 /* A friend template that specifies a class context, i.e.
5198 template <typename T> friend void A<T>::f();
5199 is not primary. */
5200 is_primary = false;
5201 else if (TREE_CODE (decl) == TYPE_DECL
5202 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5203 is_primary = false;
5204 else
5205 is_primary = template_parm_scope_p ();
5207 if (is_primary)
5209 warning (OPT_Wtemplates, "template %qD declared", decl);
5211 if (DECL_CLASS_SCOPE_P (decl))
5212 member_template_p = true;
5213 if (TREE_CODE (decl) == TYPE_DECL
5214 && anon_aggrname_p (DECL_NAME (decl)))
5216 error ("template class without a name");
5217 return error_mark_node;
5219 else if (TREE_CODE (decl) == FUNCTION_DECL)
5221 if (member_template_p)
5223 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5224 error ("member template %qD may not have virt-specifiers", decl);
5226 if (DECL_DESTRUCTOR_P (decl))
5228 /* [temp.mem]
5230 A destructor shall not be a member template. */
5231 error ("destructor %qD declared as member template", decl);
5232 return error_mark_node;
5234 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
5235 && (!prototype_p (TREE_TYPE (decl))
5236 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5237 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5238 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
5239 == void_list_node)))
5241 /* [basic.stc.dynamic.allocation]
5243 An allocation function can be a function
5244 template. ... Template allocation functions shall
5245 have two or more parameters. */
5246 error ("invalid template declaration of %qD", decl);
5247 return error_mark_node;
5250 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5251 && CLASS_TYPE_P (TREE_TYPE (decl)))
5253 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5254 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5255 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5257 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5258 if (TREE_CODE (t) == TYPE_DECL)
5259 t = TREE_TYPE (t);
5260 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5261 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5264 else if (TREE_CODE (decl) == TYPE_DECL
5265 && TYPE_DECL_ALIAS_P (decl))
5266 /* alias-declaration */
5267 gcc_assert (!DECL_ARTIFICIAL (decl));
5268 else if (VAR_P (decl))
5269 /* C++14 variable template. */;
5270 else
5272 error ("template declaration of %q#D", decl);
5273 return error_mark_node;
5277 /* Check to see that the rules regarding the use of default
5278 arguments are not being violated. */
5279 check_default_tmpl_args (decl, current_template_parms,
5280 is_primary, is_partial, /*is_friend_decl=*/0);
5282 /* Ensure that there are no parameter packs in the type of this
5283 declaration that have not been expanded. */
5284 if (TREE_CODE (decl) == FUNCTION_DECL)
5286 /* Check each of the arguments individually to see if there are
5287 any bare parameter packs. */
5288 tree type = TREE_TYPE (decl);
5289 tree arg = DECL_ARGUMENTS (decl);
5290 tree argtype = TYPE_ARG_TYPES (type);
5292 while (arg && argtype)
5294 if (!DECL_PACK_P (arg)
5295 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5297 /* This is a PARM_DECL that contains unexpanded parameter
5298 packs. We have already complained about this in the
5299 check_for_bare_parameter_packs call, so just replace
5300 these types with ERROR_MARK_NODE. */
5301 TREE_TYPE (arg) = error_mark_node;
5302 TREE_VALUE (argtype) = error_mark_node;
5305 arg = DECL_CHAIN (arg);
5306 argtype = TREE_CHAIN (argtype);
5309 /* Check for bare parameter packs in the return type and the
5310 exception specifiers. */
5311 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5312 /* Errors were already issued, set return type to int
5313 as the frontend doesn't expect error_mark_node as
5314 the return type. */
5315 TREE_TYPE (type) = integer_type_node;
5316 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5317 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5319 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5320 && TYPE_DECL_ALIAS_P (decl))
5321 ? DECL_ORIGINAL_TYPE (decl)
5322 : TREE_TYPE (decl)))
5324 TREE_TYPE (decl) = error_mark_node;
5325 return error_mark_node;
5328 if (is_partial)
5329 return process_partial_specialization (decl);
5331 args = current_template_args ();
5333 if (!ctx
5334 || TREE_CODE (ctx) == FUNCTION_DECL
5335 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5336 || (TREE_CODE (decl) == TYPE_DECL
5337 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5338 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5340 if (DECL_LANG_SPECIFIC (decl)
5341 && DECL_TEMPLATE_INFO (decl)
5342 && DECL_TI_TEMPLATE (decl))
5343 tmpl = DECL_TI_TEMPLATE (decl);
5344 /* If DECL is a TYPE_DECL for a class-template, then there won't
5345 be DECL_LANG_SPECIFIC. The information equivalent to
5346 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5347 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5348 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5349 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5351 /* Since a template declaration already existed for this
5352 class-type, we must be redeclaring it here. Make sure
5353 that the redeclaration is valid. */
5354 redeclare_class_template (TREE_TYPE (decl),
5355 current_template_parms,
5356 current_template_constraints ());
5357 /* We don't need to create a new TEMPLATE_DECL; just use the
5358 one we already had. */
5359 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5361 else
5363 tmpl = build_template_decl (decl, current_template_parms,
5364 member_template_p);
5365 new_template_p = 1;
5367 if (DECL_LANG_SPECIFIC (decl)
5368 && DECL_TEMPLATE_SPECIALIZATION (decl))
5370 /* A specialization of a member template of a template
5371 class. */
5372 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5373 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5374 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5378 else
5380 tree a, t, current, parms;
5381 int i;
5382 tree tinfo = get_template_info (decl);
5384 if (!tinfo)
5386 error ("template definition of non-template %q#D", decl);
5387 return error_mark_node;
5390 tmpl = TI_TEMPLATE (tinfo);
5392 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5393 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5394 && DECL_TEMPLATE_SPECIALIZATION (decl)
5395 && DECL_MEMBER_TEMPLATE_P (tmpl))
5397 tree new_tmpl;
5399 /* The declaration is a specialization of a member
5400 template, declared outside the class. Therefore, the
5401 innermost template arguments will be NULL, so we
5402 replace them with the arguments determined by the
5403 earlier call to check_explicit_specialization. */
5404 args = DECL_TI_ARGS (decl);
5406 new_tmpl
5407 = build_template_decl (decl, current_template_parms,
5408 member_template_p);
5409 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5410 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5411 DECL_TI_TEMPLATE (decl) = new_tmpl;
5412 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5413 DECL_TEMPLATE_INFO (new_tmpl)
5414 = build_template_info (tmpl, args);
5416 register_specialization (new_tmpl,
5417 most_general_template (tmpl),
5418 args,
5419 is_friend, 0);
5420 return decl;
5423 /* Make sure the template headers we got make sense. */
5425 parms = DECL_TEMPLATE_PARMS (tmpl);
5426 i = TMPL_PARMS_DEPTH (parms);
5427 if (TMPL_ARGS_DEPTH (args) != i)
5429 error ("expected %d levels of template parms for %q#D, got %d",
5430 i, decl, TMPL_ARGS_DEPTH (args));
5431 DECL_INTERFACE_KNOWN (decl) = 1;
5432 return error_mark_node;
5434 else
5435 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5437 a = TMPL_ARGS_LEVEL (args, i);
5438 t = INNERMOST_TEMPLATE_PARMS (parms);
5440 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5442 if (current == decl)
5443 error ("got %d template parameters for %q#D",
5444 TREE_VEC_LENGTH (a), decl);
5445 else
5446 error ("got %d template parameters for %q#T",
5447 TREE_VEC_LENGTH (a), current);
5448 error (" but %d required", TREE_VEC_LENGTH (t));
5449 /* Avoid crash in import_export_decl. */
5450 DECL_INTERFACE_KNOWN (decl) = 1;
5451 return error_mark_node;
5454 if (current == decl)
5455 current = ctx;
5456 else if (current == NULL_TREE)
5457 /* Can happen in erroneous input. */
5458 break;
5459 else
5460 current = get_containing_scope (current);
5463 /* Check that the parms are used in the appropriate qualifying scopes
5464 in the declarator. */
5465 if (!comp_template_args
5466 (TI_ARGS (tinfo),
5467 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5469 error ("\
5470 template arguments to %qD do not match original template %qD",
5471 decl, DECL_TEMPLATE_RESULT (tmpl));
5472 if (!uses_template_parms (TI_ARGS (tinfo)))
5473 inform (input_location, "use template<> for an explicit specialization");
5474 /* Avoid crash in import_export_decl. */
5475 DECL_INTERFACE_KNOWN (decl) = 1;
5476 return error_mark_node;
5480 DECL_TEMPLATE_RESULT (tmpl) = decl;
5481 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5483 /* Push template declarations for global functions and types. Note
5484 that we do not try to push a global template friend declared in a
5485 template class; such a thing may well depend on the template
5486 parameters of the class. */
5487 if (new_template_p && !ctx
5488 && !(is_friend && template_class_depth (current_class_type) > 0))
5490 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5491 if (tmpl == error_mark_node)
5492 return error_mark_node;
5494 /* Hide template friend classes that haven't been declared yet. */
5495 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5497 DECL_ANTICIPATED (tmpl) = 1;
5498 DECL_FRIEND_P (tmpl) = 1;
5502 if (is_primary)
5504 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5505 int i;
5507 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5508 if (DECL_CONV_FN_P (tmpl))
5510 int depth = TMPL_PARMS_DEPTH (parms);
5512 /* It is a conversion operator. See if the type converted to
5513 depends on innermost template operands. */
5515 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5516 depth))
5517 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5520 /* Give template template parms a DECL_CONTEXT of the template
5521 for which they are a parameter. */
5522 parms = INNERMOST_TEMPLATE_PARMS (parms);
5523 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5525 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5526 if (TREE_CODE (parm) == TEMPLATE_DECL)
5527 DECL_CONTEXT (parm) = tmpl;
5530 if (TREE_CODE (decl) == TYPE_DECL
5531 && TYPE_DECL_ALIAS_P (decl)
5532 && complex_alias_template_p (tmpl))
5533 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5536 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5537 back to its most general template. If TMPL is a specialization,
5538 ARGS may only have the innermost set of arguments. Add the missing
5539 argument levels if necessary. */
5540 if (DECL_TEMPLATE_INFO (tmpl))
5541 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5543 info = build_template_info (tmpl, args);
5545 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5546 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5547 else
5549 if (is_primary)
5550 retrofit_lang_decl (decl);
5551 if (DECL_LANG_SPECIFIC (decl))
5552 DECL_TEMPLATE_INFO (decl) = info;
5555 if (flag_implicit_templates
5556 && !is_friend
5557 && TREE_PUBLIC (decl)
5558 && VAR_OR_FUNCTION_DECL_P (decl))
5559 /* Set DECL_COMDAT on template instantiations; if we force
5560 them to be emitted by explicit instantiation or -frepo,
5561 mark_needed will tell cgraph to do the right thing. */
5562 DECL_COMDAT (decl) = true;
5564 return DECL_TEMPLATE_RESULT (tmpl);
5567 tree
5568 push_template_decl (tree decl)
5570 return push_template_decl_real (decl, false);
5573 /* FN is an inheriting constructor that inherits from the constructor
5574 template INHERITED; turn FN into a constructor template with a matching
5575 template header. */
5577 tree
5578 add_inherited_template_parms (tree fn, tree inherited)
5580 tree inner_parms
5581 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5582 inner_parms = copy_node (inner_parms);
5583 tree parms
5584 = tree_cons (size_int (processing_template_decl + 1),
5585 inner_parms, current_template_parms);
5586 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5587 tree args = template_parms_to_args (parms);
5588 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5589 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5590 DECL_TEMPLATE_RESULT (tmpl) = fn;
5591 DECL_ARTIFICIAL (tmpl) = true;
5592 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5593 return tmpl;
5596 /* Called when a class template TYPE is redeclared with the indicated
5597 template PARMS, e.g.:
5599 template <class T> struct S;
5600 template <class T> struct S {}; */
5602 bool
5603 redeclare_class_template (tree type, tree parms, tree cons)
5605 tree tmpl;
5606 tree tmpl_parms;
5607 int i;
5609 if (!TYPE_TEMPLATE_INFO (type))
5611 error ("%qT is not a template type", type);
5612 return false;
5615 tmpl = TYPE_TI_TEMPLATE (type);
5616 if (!PRIMARY_TEMPLATE_P (tmpl))
5617 /* The type is nested in some template class. Nothing to worry
5618 about here; there are no new template parameters for the nested
5619 type. */
5620 return true;
5622 if (!parms)
5624 error ("template specifiers not specified in declaration of %qD",
5625 tmpl);
5626 return false;
5629 parms = INNERMOST_TEMPLATE_PARMS (parms);
5630 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5632 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5634 error_n (input_location, TREE_VEC_LENGTH (parms),
5635 "redeclared with %d template parameter",
5636 "redeclared with %d template parameters",
5637 TREE_VEC_LENGTH (parms));
5638 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5639 "previous declaration %qD used %d template parameter",
5640 "previous declaration %qD used %d template parameters",
5641 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5642 return false;
5645 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5647 tree tmpl_parm;
5648 tree parm;
5649 tree tmpl_default;
5650 tree parm_default;
5652 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5653 || TREE_VEC_ELT (parms, i) == error_mark_node)
5654 continue;
5656 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5657 if (error_operand_p (tmpl_parm))
5658 return false;
5660 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5661 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5662 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5664 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5665 TEMPLATE_DECL. */
5666 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5667 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5668 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5669 || (TREE_CODE (tmpl_parm) != PARM_DECL
5670 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5671 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5672 || (TREE_CODE (tmpl_parm) == PARM_DECL
5673 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5674 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5676 error ("template parameter %q+#D", tmpl_parm);
5677 error ("redeclared here as %q#D", parm);
5678 return false;
5681 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5683 /* We have in [temp.param]:
5685 A template-parameter may not be given default arguments
5686 by two different declarations in the same scope. */
5687 error_at (input_location, "redefinition of default argument for %q#D", parm);
5688 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5689 "original definition appeared here");
5690 return false;
5693 if (parm_default != NULL_TREE)
5694 /* Update the previous template parameters (which are the ones
5695 that will really count) with the new default value. */
5696 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5697 else if (tmpl_default != NULL_TREE)
5698 /* Update the new parameters, too; they'll be used as the
5699 parameters for any members. */
5700 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5702 /* Give each template template parm in this redeclaration a
5703 DECL_CONTEXT of the template for which they are a parameter. */
5704 if (TREE_CODE (parm) == TEMPLATE_DECL)
5706 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5707 DECL_CONTEXT (parm) = tmpl;
5710 if (TREE_CODE (parm) == TYPE_DECL)
5711 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5714 // Cannot redeclare a class template with a different set of constraints.
5715 if (!equivalent_constraints (get_constraints (tmpl), cons))
5717 error_at (input_location, "redeclaration %q#D with different "
5718 "constraints", tmpl);
5719 inform (DECL_SOURCE_LOCATION (tmpl),
5720 "original declaration appeared here");
5723 return true;
5726 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5727 to be used when the caller has already checked
5728 (processing_template_decl
5729 && !instantiation_dependent_expression_p (expr)
5730 && potential_constant_expression (expr))
5731 and cleared processing_template_decl. */
5733 tree
5734 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5736 return tsubst_copy_and_build (expr,
5737 /*args=*/NULL_TREE,
5738 complain,
5739 /*in_decl=*/NULL_TREE,
5740 /*function_p=*/false,
5741 /*integral_constant_expression_p=*/true);
5744 /* Simplify EXPR if it is a non-dependent expression. Returns the
5745 (possibly simplified) expression. */
5747 tree
5748 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5750 if (expr == NULL_TREE)
5751 return NULL_TREE;
5753 /* If we're in a template, but EXPR isn't value dependent, simplify
5754 it. We're supposed to treat:
5756 template <typename T> void f(T[1 + 1]);
5757 template <typename T> void f(T[2]);
5759 as two declarations of the same function, for example. */
5760 if (processing_template_decl
5761 && potential_nondependent_constant_expression (expr))
5763 processing_template_decl_sentinel s;
5764 expr = instantiate_non_dependent_expr_internal (expr, complain);
5766 return expr;
5769 tree
5770 instantiate_non_dependent_expr (tree expr)
5772 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5775 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5776 an uninstantiated expression. */
5778 tree
5779 instantiate_non_dependent_or_null (tree expr)
5781 if (expr == NULL_TREE)
5782 return NULL_TREE;
5783 if (processing_template_decl)
5785 if (!potential_nondependent_constant_expression (expr))
5786 expr = NULL_TREE;
5787 else
5789 processing_template_decl_sentinel s;
5790 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5793 return expr;
5796 /* True iff T is a specialization of a variable template. */
5798 bool
5799 variable_template_specialization_p (tree t)
5801 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5802 return false;
5803 tree tmpl = DECL_TI_TEMPLATE (t);
5804 return variable_template_p (tmpl);
5807 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5808 template declaration, or a TYPE_DECL for an alias declaration. */
5810 bool
5811 alias_type_or_template_p (tree t)
5813 if (t == NULL_TREE)
5814 return false;
5815 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5816 || (TYPE_P (t)
5817 && TYPE_NAME (t)
5818 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5819 || DECL_ALIAS_TEMPLATE_P (t));
5822 /* Return TRUE iff T is a specialization of an alias template. */
5824 bool
5825 alias_template_specialization_p (const_tree t)
5827 /* It's an alias template specialization if it's an alias and its
5828 TYPE_NAME is a specialization of a primary template. */
5829 if (TYPE_ALIAS_P (t))
5830 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5831 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5833 return false;
5836 /* An alias template is complex from a SFINAE perspective if a template-id
5837 using that alias can be ill-formed when the expansion is not, as with
5838 the void_t template. We determine this by checking whether the
5839 expansion for the alias template uses all its template parameters. */
5841 struct uses_all_template_parms_data
5843 int level;
5844 bool *seen;
5847 static int
5848 uses_all_template_parms_r (tree t, void *data_)
5850 struct uses_all_template_parms_data &data
5851 = *(struct uses_all_template_parms_data*)data_;
5852 tree idx = get_template_parm_index (t);
5854 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5855 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5856 return 0;
5859 static bool
5860 complex_alias_template_p (const_tree tmpl)
5862 struct uses_all_template_parms_data data;
5863 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5864 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5865 data.level = TMPL_PARMS_DEPTH (parms);
5866 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5867 data.seen = XALLOCAVEC (bool, len);
5868 for (int i = 0; i < len; ++i)
5869 data.seen[i] = false;
5871 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5872 for (int i = 0; i < len; ++i)
5873 if (!data.seen[i])
5874 return true;
5875 return false;
5878 /* Return TRUE iff T is a specialization of a complex alias template with
5879 dependent template-arguments. */
5881 bool
5882 dependent_alias_template_spec_p (const_tree t)
5884 if (!alias_template_specialization_p (t))
5885 return false;
5887 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5888 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5889 return false;
5891 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5892 if (!any_dependent_template_arguments_p (args))
5893 return false;
5895 return true;
5898 /* Return the number of innermost template parameters in TMPL. */
5900 static int
5901 num_innermost_template_parms (tree tmpl)
5903 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5904 return TREE_VEC_LENGTH (parms);
5907 /* Return either TMPL or another template that it is equivalent to under DR
5908 1286: An alias that just changes the name of a template is equivalent to
5909 the other template. */
5911 static tree
5912 get_underlying_template (tree tmpl)
5914 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
5915 while (DECL_ALIAS_TEMPLATE_P (tmpl))
5917 /* Determine if the alias is equivalent to an underlying template. */
5918 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5919 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
5920 if (!tinfo)
5921 break;
5923 tree underlying = TI_TEMPLATE (tinfo);
5924 if (!PRIMARY_TEMPLATE_P (underlying)
5925 || (num_innermost_template_parms (tmpl)
5926 != num_innermost_template_parms (underlying)))
5927 break;
5929 tree alias_args = INNERMOST_TEMPLATE_ARGS
5930 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
5931 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
5932 break;
5934 /* Alias is equivalent. Strip it and repeat. */
5935 tmpl = underlying;
5938 return tmpl;
5941 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
5942 must be a reference-to-function or a pointer-to-function type, as specified
5943 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
5944 and check that the resulting function has external linkage. */
5946 static tree
5947 convert_nontype_argument_function (tree type, tree expr,
5948 tsubst_flags_t complain)
5950 tree fns = expr;
5951 tree fn, fn_no_ptr;
5952 linkage_kind linkage;
5954 fn = instantiate_type (type, fns, tf_none);
5955 if (fn == error_mark_node)
5956 return error_mark_node;
5958 if (value_dependent_expression_p (fn))
5959 goto accept;
5961 fn_no_ptr = strip_fnptr_conv (fn);
5962 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
5963 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
5964 if (BASELINK_P (fn_no_ptr))
5965 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
5967 /* [temp.arg.nontype]/1
5969 A template-argument for a non-type, non-template template-parameter
5970 shall be one of:
5971 [...]
5972 -- the address of an object or function with external [C++11: or
5973 internal] linkage. */
5975 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
5977 if (complain & tf_error)
5979 error ("%qE is not a valid template argument for type %qT",
5980 expr, type);
5981 if (TYPE_PTR_P (type))
5982 inform (input_location, "it must be the address of a function "
5983 "with external linkage");
5984 else
5985 inform (input_location, "it must be the name of a function with "
5986 "external linkage");
5988 return NULL_TREE;
5991 linkage = decl_linkage (fn_no_ptr);
5992 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
5994 if (complain & tf_error)
5996 if (cxx_dialect >= cxx11)
5997 error ("%qE is not a valid template argument for type %qT "
5998 "because %qD has no linkage",
5999 expr, type, fn_no_ptr);
6000 else
6001 error ("%qE is not a valid template argument for type %qT "
6002 "because %qD does not have external linkage",
6003 expr, type, fn_no_ptr);
6005 return NULL_TREE;
6008 accept:
6009 if (TREE_CODE (type) == REFERENCE_TYPE)
6010 fn = build_address (fn);
6011 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6012 fn = build_nop (type, fn);
6014 return fn;
6017 /* Subroutine of convert_nontype_argument.
6018 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6019 Emit an error otherwise. */
6021 static bool
6022 check_valid_ptrmem_cst_expr (tree type, tree expr,
6023 tsubst_flags_t complain)
6025 STRIP_NOPS (expr);
6026 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
6027 return true;
6028 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6029 return true;
6030 if (processing_template_decl
6031 && TREE_CODE (expr) == ADDR_EXPR
6032 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6033 return true;
6034 if (complain & tf_error)
6036 error ("%qE is not a valid template argument for type %qT",
6037 expr, type);
6038 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6040 return false;
6043 /* Returns TRUE iff the address of OP is value-dependent.
6045 14.6.2.4 [temp.dep.temp]:
6046 A non-integral non-type template-argument is dependent if its type is
6047 dependent or it has either of the following forms
6048 qualified-id
6049 & qualified-id
6050 and contains a nested-name-specifier which specifies a class-name that
6051 names a dependent type.
6053 We generalize this to just say that the address of a member of a
6054 dependent class is value-dependent; the above doesn't cover the
6055 address of a static data member named with an unqualified-id. */
6057 static bool
6058 has_value_dependent_address (tree op)
6060 /* We could use get_inner_reference here, but there's no need;
6061 this is only relevant for template non-type arguments, which
6062 can only be expressed as &id-expression. */
6063 if (DECL_P (op))
6065 tree ctx = CP_DECL_CONTEXT (op);
6066 if (TYPE_P (ctx) && dependent_type_p (ctx))
6067 return true;
6070 return false;
6073 /* The next set of functions are used for providing helpful explanatory
6074 diagnostics for failed overload resolution. Their messages should be
6075 indented by two spaces for consistency with the messages in
6076 call.c */
6078 static int
6079 unify_success (bool /*explain_p*/)
6081 return 0;
6084 /* Other failure functions should call this one, to provide a single function
6085 for setting a breakpoint on. */
6087 static int
6088 unify_invalid (bool /*explain_p*/)
6090 return 1;
6093 static int
6094 unify_parameter_deduction_failure (bool explain_p, tree parm)
6096 if (explain_p)
6097 inform (input_location,
6098 " couldn't deduce template parameter %qD", parm);
6099 return unify_invalid (explain_p);
6102 static int
6103 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6105 if (explain_p)
6106 inform (input_location,
6107 " types %qT and %qT have incompatible cv-qualifiers",
6108 parm, arg);
6109 return unify_invalid (explain_p);
6112 static int
6113 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6115 if (explain_p)
6116 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6117 return unify_invalid (explain_p);
6120 static int
6121 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6123 if (explain_p)
6124 inform (input_location,
6125 " template parameter %qD is not a parameter pack, but "
6126 "argument %qD is",
6127 parm, arg);
6128 return unify_invalid (explain_p);
6131 static int
6132 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6134 if (explain_p)
6135 inform (input_location,
6136 " template argument %qE does not match "
6137 "pointer-to-member constant %qE",
6138 arg, parm);
6139 return unify_invalid (explain_p);
6142 static int
6143 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6145 if (explain_p)
6146 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6147 return unify_invalid (explain_p);
6150 static int
6151 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6153 if (explain_p)
6154 inform (input_location,
6155 " inconsistent parameter pack deduction with %qT and %qT",
6156 old_arg, new_arg);
6157 return unify_invalid (explain_p);
6160 static int
6161 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6163 if (explain_p)
6165 if (TYPE_P (parm))
6166 inform (input_location,
6167 " deduced conflicting types for parameter %qT (%qT and %qT)",
6168 parm, first, second);
6169 else
6170 inform (input_location,
6171 " deduced conflicting values for non-type parameter "
6172 "%qE (%qE and %qE)", parm, first, second);
6174 return unify_invalid (explain_p);
6177 static int
6178 unify_vla_arg (bool explain_p, tree arg)
6180 if (explain_p)
6181 inform (input_location,
6182 " variable-sized array type %qT is not "
6183 "a valid template argument",
6184 arg);
6185 return unify_invalid (explain_p);
6188 static int
6189 unify_method_type_error (bool explain_p, tree arg)
6191 if (explain_p)
6192 inform (input_location,
6193 " member function type %qT is not a valid template argument",
6194 arg);
6195 return unify_invalid (explain_p);
6198 static int
6199 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6201 if (explain_p)
6203 if (least_p)
6204 inform_n (input_location, wanted,
6205 " candidate expects at least %d argument, %d provided",
6206 " candidate expects at least %d arguments, %d provided",
6207 wanted, have);
6208 else
6209 inform_n (input_location, wanted,
6210 " candidate expects %d argument, %d provided",
6211 " candidate expects %d arguments, %d provided",
6212 wanted, have);
6214 return unify_invalid (explain_p);
6217 static int
6218 unify_too_many_arguments (bool explain_p, int have, int wanted)
6220 return unify_arity (explain_p, have, wanted);
6223 static int
6224 unify_too_few_arguments (bool explain_p, int have, int wanted,
6225 bool least_p = false)
6227 return unify_arity (explain_p, have, wanted, least_p);
6230 static int
6231 unify_arg_conversion (bool explain_p, tree to_type,
6232 tree from_type, tree arg)
6234 if (explain_p)
6235 inform (EXPR_LOC_OR_LOC (arg, input_location),
6236 " cannot convert %qE (type %qT) to type %qT",
6237 arg, from_type, to_type);
6238 return unify_invalid (explain_p);
6241 static int
6242 unify_no_common_base (bool explain_p, enum template_base_result r,
6243 tree parm, tree arg)
6245 if (explain_p)
6246 switch (r)
6248 case tbr_ambiguous_baseclass:
6249 inform (input_location, " %qT is an ambiguous base class of %qT",
6250 parm, arg);
6251 break;
6252 default:
6253 inform (input_location, " %qT is not derived from %qT", arg, parm);
6254 break;
6256 return unify_invalid (explain_p);
6259 static int
6260 unify_inconsistent_template_template_parameters (bool explain_p)
6262 if (explain_p)
6263 inform (input_location,
6264 " template parameters of a template template argument are "
6265 "inconsistent with other deduced template arguments");
6266 return unify_invalid (explain_p);
6269 static int
6270 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6272 if (explain_p)
6273 inform (input_location,
6274 " can't deduce a template for %qT from non-template type %qT",
6275 parm, arg);
6276 return unify_invalid (explain_p);
6279 static int
6280 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6282 if (explain_p)
6283 inform (input_location,
6284 " template argument %qE does not match %qE", arg, parm);
6285 return unify_invalid (explain_p);
6288 static int
6289 unify_overload_resolution_failure (bool explain_p, tree arg)
6291 if (explain_p)
6292 inform (input_location,
6293 " could not resolve address from overloaded function %qE",
6294 arg);
6295 return unify_invalid (explain_p);
6298 /* Attempt to convert the non-type template parameter EXPR to the
6299 indicated TYPE. If the conversion is successful, return the
6300 converted value. If the conversion is unsuccessful, return
6301 NULL_TREE if we issued an error message, or error_mark_node if we
6302 did not. We issue error messages for out-and-out bad template
6303 parameters, but not simply because the conversion failed, since we
6304 might be just trying to do argument deduction. Both TYPE and EXPR
6305 must be non-dependent.
6307 The conversion follows the special rules described in
6308 [temp.arg.nontype], and it is much more strict than an implicit
6309 conversion.
6311 This function is called twice for each template argument (see
6312 lookup_template_class for a more accurate description of this
6313 problem). This means that we need to handle expressions which
6314 are not valid in a C++ source, but can be created from the
6315 first call (for instance, casts to perform conversions). These
6316 hacks can go away after we fix the double coercion problem. */
6318 static tree
6319 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6321 tree expr_type;
6323 /* Detect immediately string literals as invalid non-type argument.
6324 This special-case is not needed for correctness (we would easily
6325 catch this later), but only to provide better diagnostic for this
6326 common user mistake. As suggested by DR 100, we do not mention
6327 linkage issues in the diagnostic as this is not the point. */
6328 /* FIXME we're making this OK. */
6329 if (TREE_CODE (expr) == STRING_CST)
6331 if (complain & tf_error)
6332 error ("%qE is not a valid template argument for type %qT "
6333 "because string literals can never be used in this context",
6334 expr, type);
6335 return NULL_TREE;
6338 /* Add the ADDR_EXPR now for the benefit of
6339 value_dependent_expression_p. */
6340 if (TYPE_PTROBV_P (type)
6341 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6343 expr = decay_conversion (expr, complain);
6344 if (expr == error_mark_node)
6345 return error_mark_node;
6348 /* If we are in a template, EXPR may be non-dependent, but still
6349 have a syntactic, rather than semantic, form. For example, EXPR
6350 might be a SCOPE_REF, rather than the VAR_DECL to which the
6351 SCOPE_REF refers. Preserving the qualifying scope is necessary
6352 so that access checking can be performed when the template is
6353 instantiated -- but here we need the resolved form so that we can
6354 convert the argument. */
6355 bool non_dep = false;
6356 if (TYPE_REF_OBJ_P (type)
6357 && has_value_dependent_address (expr))
6358 /* If we want the address and it's value-dependent, don't fold. */;
6359 else if (processing_template_decl
6360 && potential_nondependent_constant_expression (expr))
6361 non_dep = true;
6362 if (error_operand_p (expr))
6363 return error_mark_node;
6364 expr_type = TREE_TYPE (expr);
6365 if (TREE_CODE (type) == REFERENCE_TYPE)
6366 expr = mark_lvalue_use (expr);
6367 else
6368 expr = mark_rvalue_use (expr);
6370 /* If the argument is non-dependent, perform any conversions in
6371 non-dependent context as well. */
6372 processing_template_decl_sentinel s (non_dep);
6373 if (non_dep)
6374 expr = instantiate_non_dependent_expr_internal (expr, complain);
6376 if (value_dependent_expression_p (expr))
6377 expr = canonicalize_expr_argument (expr, complain);
6379 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6380 to a non-type argument of "nullptr". */
6381 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6382 expr = fold_simple (convert (type, expr));
6384 /* In C++11, integral or enumeration non-type template arguments can be
6385 arbitrary constant expressions. Pointer and pointer to
6386 member arguments can be general constant expressions that evaluate
6387 to a null value, but otherwise still need to be of a specific form. */
6388 if (cxx_dialect >= cxx11)
6390 if (TREE_CODE (expr) == PTRMEM_CST)
6391 /* A PTRMEM_CST is already constant, and a valid template
6392 argument for a parameter of pointer to member type, we just want
6393 to leave it in that form rather than lower it to a
6394 CONSTRUCTOR. */;
6395 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6396 /* Constant value checking is done later with type conversion. */;
6397 else if (cxx_dialect >= cxx1z)
6399 if (TREE_CODE (type) != REFERENCE_TYPE)
6400 expr = maybe_constant_value (expr);
6401 else if (REFERENCE_REF_P (expr))
6403 expr = TREE_OPERAND (expr, 0);
6404 expr = maybe_constant_value (expr);
6405 expr = convert_from_reference (expr);
6408 else if (TYPE_PTR_OR_PTRMEM_P (type))
6410 tree folded = maybe_constant_value (expr);
6411 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6412 : null_member_pointer_value_p (folded))
6413 expr = folded;
6417 /* HACK: Due to double coercion, we can get a
6418 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6419 which is the tree that we built on the first call (see
6420 below when coercing to reference to object or to reference to
6421 function). We just strip everything and get to the arg.
6422 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6423 for examples. */
6424 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6426 tree probe_type, probe = expr;
6427 if (REFERENCE_REF_P (probe))
6428 probe = TREE_OPERAND (probe, 0);
6429 probe_type = TREE_TYPE (probe);
6430 if (TREE_CODE (probe) == NOP_EXPR)
6432 /* ??? Maybe we could use convert_from_reference here, but we
6433 would need to relax its constraints because the NOP_EXPR
6434 could actually change the type to something more cv-qualified,
6435 and this is not folded by convert_from_reference. */
6436 tree addr = TREE_OPERAND (probe, 0);
6437 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6438 && TREE_CODE (addr) == ADDR_EXPR
6439 && TYPE_PTR_P (TREE_TYPE (addr))
6440 && (same_type_ignoring_top_level_qualifiers_p
6441 (TREE_TYPE (probe_type),
6442 TREE_TYPE (TREE_TYPE (addr)))))
6444 expr = TREE_OPERAND (addr, 0);
6445 expr_type = TREE_TYPE (probe_type);
6450 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6451 parameter is a pointer to object, through decay and
6452 qualification conversion. Let's strip everything. */
6453 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6455 tree probe = expr;
6456 STRIP_NOPS (probe);
6457 if (TREE_CODE (probe) == ADDR_EXPR
6458 && TYPE_PTR_P (TREE_TYPE (probe)))
6460 /* Skip the ADDR_EXPR only if it is part of the decay for
6461 an array. Otherwise, it is part of the original argument
6462 in the source code. */
6463 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6464 probe = TREE_OPERAND (probe, 0);
6465 expr = probe;
6466 expr_type = TREE_TYPE (expr);
6470 /* [temp.arg.nontype]/5, bullet 1
6472 For a non-type template-parameter of integral or enumeration type,
6473 integral promotions (_conv.prom_) and integral conversions
6474 (_conv.integral_) are applied. */
6475 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6477 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6478 t = maybe_constant_value (t);
6479 if (t != error_mark_node)
6480 expr = t;
6482 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6483 return error_mark_node;
6485 /* Notice that there are constant expressions like '4 % 0' which
6486 do not fold into integer constants. */
6487 if (TREE_CODE (expr) != INTEGER_CST
6488 && !value_dependent_expression_p (expr))
6490 if (complain & tf_error)
6492 int errs = errorcount, warns = warningcount + werrorcount;
6493 if (processing_template_decl
6494 && !require_potential_constant_expression (expr))
6495 return NULL_TREE;
6496 expr = cxx_constant_value (expr);
6497 if (errorcount > errs || warningcount + werrorcount > warns)
6498 inform (EXPR_LOC_OR_LOC (expr, input_location),
6499 "in template argument for type %qT ", type);
6500 if (expr == error_mark_node)
6501 return NULL_TREE;
6502 /* else cxx_constant_value complained but gave us
6503 a real constant, so go ahead. */
6504 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6506 else
6507 return NULL_TREE;
6510 /* Avoid typedef problems. */
6511 if (TREE_TYPE (expr) != type)
6512 expr = fold_convert (type, expr);
6514 /* [temp.arg.nontype]/5, bullet 2
6516 For a non-type template-parameter of type pointer to object,
6517 qualification conversions (_conv.qual_) and the array-to-pointer
6518 conversion (_conv.array_) are applied. */
6519 else if (TYPE_PTROBV_P (type))
6521 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6523 A template-argument for a non-type, non-template template-parameter
6524 shall be one of: [...]
6526 -- the name of a non-type template-parameter;
6527 -- the address of an object or function with external linkage, [...]
6528 expressed as "& id-expression" where the & is optional if the name
6529 refers to a function or array, or if the corresponding
6530 template-parameter is a reference.
6532 Here, we do not care about functions, as they are invalid anyway
6533 for a parameter of type pointer-to-object. */
6535 if (value_dependent_expression_p (expr))
6536 /* Non-type template parameters are OK. */
6538 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6539 /* Null pointer values are OK in C++11. */;
6540 else if (TREE_CODE (expr) != ADDR_EXPR
6541 && TREE_CODE (expr_type) != ARRAY_TYPE)
6543 if (VAR_P (expr))
6545 if (complain & tf_error)
6546 error ("%qD is not a valid template argument "
6547 "because %qD is a variable, not the address of "
6548 "a variable", expr, expr);
6549 return NULL_TREE;
6551 if (POINTER_TYPE_P (expr_type))
6553 if (complain & tf_error)
6554 error ("%qE is not a valid template argument for %qT "
6555 "because it is not the address of a variable",
6556 expr, type);
6557 return NULL_TREE;
6559 /* Other values, like integer constants, might be valid
6560 non-type arguments of some other type. */
6561 return error_mark_node;
6563 else
6565 tree decl;
6567 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6568 ? TREE_OPERAND (expr, 0) : expr);
6569 if (!VAR_P (decl))
6571 if (complain & tf_error)
6572 error ("%qE is not a valid template argument of type %qT "
6573 "because %qE is not a variable", expr, type, decl);
6574 return NULL_TREE;
6576 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6578 if (complain & tf_error)
6579 error ("%qE is not a valid template argument of type %qT "
6580 "because %qD does not have external linkage",
6581 expr, type, decl);
6582 return NULL_TREE;
6584 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6586 if (complain & tf_error)
6587 error ("%qE is not a valid template argument of type %qT "
6588 "because %qD has no linkage", expr, type, decl);
6589 return NULL_TREE;
6593 expr = decay_conversion (expr, complain);
6594 if (expr == error_mark_node)
6595 return error_mark_node;
6597 expr = perform_qualification_conversions (type, expr);
6598 if (expr == error_mark_node)
6599 return error_mark_node;
6601 /* [temp.arg.nontype]/5, bullet 3
6603 For a non-type template-parameter of type reference to object, no
6604 conversions apply. The type referred to by the reference may be more
6605 cv-qualified than the (otherwise identical) type of the
6606 template-argument. The template-parameter is bound directly to the
6607 template-argument, which must be an lvalue. */
6608 else if (TYPE_REF_OBJ_P (type))
6610 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6611 expr_type))
6612 return error_mark_node;
6614 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6616 if (complain & tf_error)
6617 error ("%qE is not a valid template argument for type %qT "
6618 "because of conflicts in cv-qualification", expr, type);
6619 return NULL_TREE;
6622 if (!lvalue_p (expr))
6624 if (complain & tf_error)
6625 error ("%qE is not a valid template argument for type %qT "
6626 "because it is not an lvalue", expr, type);
6627 return NULL_TREE;
6630 /* [temp.arg.nontype]/1
6632 A template-argument for a non-type, non-template template-parameter
6633 shall be one of: [...]
6635 -- the address of an object or function with external linkage. */
6636 if (INDIRECT_REF_P (expr)
6637 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6639 expr = TREE_OPERAND (expr, 0);
6640 if (DECL_P (expr))
6642 if (complain & tf_error)
6643 error ("%q#D is not a valid template argument for type %qT "
6644 "because a reference variable does not have a constant "
6645 "address", expr, type);
6646 return NULL_TREE;
6650 if (!value_dependent_expression_p (expr))
6652 if (!DECL_P (expr))
6654 if (complain & tf_error)
6655 error ("%qE is not a valid template argument for type %qT "
6656 "because it is not an object with linkage",
6657 expr, type);
6658 return NULL_TREE;
6661 /* DR 1155 allows internal linkage in C++11 and up. */
6662 linkage_kind linkage = decl_linkage (expr);
6663 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6665 if (complain & tf_error)
6666 error ("%qE is not a valid template argument for type %qT "
6667 "because object %qD does not have linkage",
6668 expr, type, expr);
6669 return NULL_TREE;
6672 expr = build_nop (type, build_address (expr));
6675 /* [temp.arg.nontype]/5, bullet 4
6677 For a non-type template-parameter of type pointer to function, only
6678 the function-to-pointer conversion (_conv.func_) is applied. If the
6679 template-argument represents a set of overloaded functions (or a
6680 pointer to such), the matching function is selected from the set
6681 (_over.over_). */
6682 else if (TYPE_PTRFN_P (type))
6684 /* If the argument is a template-id, we might not have enough
6685 context information to decay the pointer. */
6686 if (!type_unknown_p (expr_type))
6688 expr = decay_conversion (expr, complain);
6689 if (expr == error_mark_node)
6690 return error_mark_node;
6693 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6694 /* Null pointer values are OK in C++11. */
6695 return perform_qualification_conversions (type, expr);
6697 expr = convert_nontype_argument_function (type, expr, complain);
6698 if (!expr || expr == error_mark_node)
6699 return expr;
6701 /* [temp.arg.nontype]/5, bullet 5
6703 For a non-type template-parameter of type reference to function, no
6704 conversions apply. If the template-argument represents a set of
6705 overloaded functions, the matching function is selected from the set
6706 (_over.over_). */
6707 else if (TYPE_REFFN_P (type))
6709 if (TREE_CODE (expr) == ADDR_EXPR)
6711 if (complain & tf_error)
6713 error ("%qE is not a valid template argument for type %qT "
6714 "because it is a pointer", expr, type);
6715 inform (input_location, "try using %qE instead",
6716 TREE_OPERAND (expr, 0));
6718 return NULL_TREE;
6721 expr = convert_nontype_argument_function (type, expr, complain);
6722 if (!expr || expr == error_mark_node)
6723 return expr;
6725 /* [temp.arg.nontype]/5, bullet 6
6727 For a non-type template-parameter of type pointer to member function,
6728 no conversions apply. If the template-argument represents a set of
6729 overloaded member functions, the matching member function is selected
6730 from the set (_over.over_). */
6731 else if (TYPE_PTRMEMFUNC_P (type))
6733 expr = instantiate_type (type, expr, tf_none);
6734 if (expr == error_mark_node)
6735 return error_mark_node;
6737 /* [temp.arg.nontype] bullet 1 says the pointer to member
6738 expression must be a pointer-to-member constant. */
6739 if (!value_dependent_expression_p (expr)
6740 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6741 return error_mark_node;
6743 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6744 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6745 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6746 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6748 /* There is no way to disable standard conversions in
6749 resolve_address_of_overloaded_function (called by
6750 instantiate_type). It is possible that the call succeeded by
6751 converting &B::I to &D::I (where B is a base of D), so we need
6752 to reject this conversion here.
6754 Actually, even if there was a way to disable standard conversions,
6755 it would still be better to reject them here so that we can
6756 provide a superior diagnostic. */
6757 if (!same_type_p (TREE_TYPE (expr), type))
6759 if (complain & tf_error)
6761 error ("%qE is not a valid template argument for type %qT "
6762 "because it is of type %qT", expr, type,
6763 TREE_TYPE (expr));
6764 /* If we are just one standard conversion off, explain. */
6765 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6766 inform (input_location,
6767 "standard conversions are not allowed in this context");
6769 return NULL_TREE;
6772 /* [temp.arg.nontype]/5, bullet 7
6774 For a non-type template-parameter of type pointer to data member,
6775 qualification conversions (_conv.qual_) are applied. */
6776 else if (TYPE_PTRDATAMEM_P (type))
6778 /* [temp.arg.nontype] bullet 1 says the pointer to member
6779 expression must be a pointer-to-member constant. */
6780 if (!value_dependent_expression_p (expr)
6781 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6782 return error_mark_node;
6784 expr = perform_qualification_conversions (type, expr);
6785 if (expr == error_mark_node)
6786 return expr;
6788 else if (NULLPTR_TYPE_P (type))
6790 if (expr != nullptr_node)
6792 if (complain & tf_error)
6793 error ("%qE is not a valid template argument for type %qT "
6794 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6795 return NULL_TREE;
6797 return expr;
6799 /* A template non-type parameter must be one of the above. */
6800 else
6801 gcc_unreachable ();
6803 /* Sanity check: did we actually convert the argument to the
6804 right type? */
6805 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6806 (type, TREE_TYPE (expr)));
6807 return convert_from_reference (expr);
6810 /* Subroutine of coerce_template_template_parms, which returns 1 if
6811 PARM_PARM and ARG_PARM match using the rule for the template
6812 parameters of template template parameters. Both PARM and ARG are
6813 template parameters; the rest of the arguments are the same as for
6814 coerce_template_template_parms.
6816 static int
6817 coerce_template_template_parm (tree parm,
6818 tree arg,
6819 tsubst_flags_t complain,
6820 tree in_decl,
6821 tree outer_args)
6823 if (arg == NULL_TREE || error_operand_p (arg)
6824 || parm == NULL_TREE || error_operand_p (parm))
6825 return 0;
6827 if (TREE_CODE (arg) != TREE_CODE (parm))
6828 return 0;
6830 switch (TREE_CODE (parm))
6832 case TEMPLATE_DECL:
6833 /* We encounter instantiations of templates like
6834 template <template <template <class> class> class TT>
6835 class C; */
6837 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6838 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6840 if (!coerce_template_template_parms
6841 (parmparm, argparm, complain, in_decl, outer_args))
6842 return 0;
6844 /* Fall through. */
6846 case TYPE_DECL:
6847 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6848 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6849 /* Argument is a parameter pack but parameter is not. */
6850 return 0;
6851 break;
6853 case PARM_DECL:
6854 /* The tsubst call is used to handle cases such as
6856 template <int> class C {};
6857 template <class T, template <T> class TT> class D {};
6858 D<int, C> d;
6860 i.e. the parameter list of TT depends on earlier parameters. */
6861 if (!uses_template_parms (TREE_TYPE (arg)))
6863 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6864 if (!uses_template_parms (t)
6865 && !same_type_p (t, TREE_TYPE (arg)))
6866 return 0;
6869 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6870 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6871 /* Argument is a parameter pack but parameter is not. */
6872 return 0;
6874 break;
6876 default:
6877 gcc_unreachable ();
6880 return 1;
6883 /* Coerce template argument list ARGLIST for use with template
6884 template-parameter TEMPL. */
6886 static tree
6887 coerce_template_args_for_ttp (tree templ, tree arglist,
6888 tsubst_flags_t complain)
6890 /* Consider an example where a template template parameter declared as
6892 template <class T, class U = std::allocator<T> > class TT
6894 The template parameter level of T and U are one level larger than
6895 of TT. To proper process the default argument of U, say when an
6896 instantiation `TT<int>' is seen, we need to build the full
6897 arguments containing {int} as the innermost level. Outer levels,
6898 available when not appearing as default template argument, can be
6899 obtained from the arguments of the enclosing template.
6901 Suppose that TT is later substituted with std::vector. The above
6902 instantiation is `TT<int, std::allocator<T> >' with TT at
6903 level 1, and T at level 2, while the template arguments at level 1
6904 becomes {std::vector} and the inner level 2 is {int}. */
6906 tree outer = DECL_CONTEXT (templ);
6907 if (outer)
6909 if (DECL_TEMPLATE_SPECIALIZATION (outer))
6910 /* We want arguments for the partial specialization, not arguments for
6911 the primary template. */
6912 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
6913 else
6914 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6916 else if (current_template_parms)
6918 /* This is an argument of the current template, so we haven't set
6919 DECL_CONTEXT yet. */
6920 tree relevant_template_parms;
6922 /* Parameter levels that are greater than the level of the given
6923 template template parm are irrelevant. */
6924 relevant_template_parms = current_template_parms;
6925 while (TMPL_PARMS_DEPTH (relevant_template_parms)
6926 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
6927 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
6929 outer = template_parms_to_args (relevant_template_parms);
6932 if (outer)
6933 arglist = add_to_template_args (outer, arglist);
6935 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
6936 return coerce_template_parms (parmlist, arglist, templ,
6937 complain,
6938 /*require_all_args=*/true,
6939 /*use_default_args=*/true);
6942 /* A cache of template template parameters with match-all default
6943 arguments. */
6944 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
6945 static void
6946 store_defaulted_ttp (tree v, tree t)
6948 if (!defaulted_ttp_cache)
6949 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
6950 defaulted_ttp_cache->put (v, t);
6952 static tree
6953 lookup_defaulted_ttp (tree v)
6955 if (defaulted_ttp_cache)
6956 if (tree *p = defaulted_ttp_cache->get (v))
6957 return *p;
6958 return NULL_TREE;
6961 /* T is a bound template template-parameter. Copy its arguments into default
6962 arguments of the template template-parameter's template parameters. */
6964 static tree
6965 add_defaults_to_ttp (tree otmpl)
6967 if (tree c = lookup_defaulted_ttp (otmpl))
6968 return c;
6970 tree ntmpl = copy_node (otmpl);
6972 tree ntype = copy_node (TREE_TYPE (otmpl));
6973 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
6974 TYPE_MAIN_VARIANT (ntype) = ntype;
6975 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
6976 TYPE_NAME (ntype) = ntmpl;
6977 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
6979 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
6980 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
6981 TEMPLATE_PARM_DECL (idx) = ntmpl;
6982 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
6984 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
6985 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
6986 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
6987 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
6988 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
6990 tree o = TREE_VEC_ELT (vec, i);
6991 if (!template_parameter_pack_p (TREE_VALUE (o)))
6993 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
6994 TREE_PURPOSE (n) = any_targ_node;
6998 store_defaulted_ttp (otmpl, ntmpl);
6999 return ntmpl;
7002 /* ARG is a bound potential template template-argument, and PARGS is a list
7003 of arguments for the corresponding template template-parameter. Adjust
7004 PARGS as appropriate for application to ARG's template, and if ARG is a
7005 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7006 arguments to the template template parameter. */
7008 static tree
7009 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7011 ++processing_template_decl;
7012 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7013 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7015 /* When comparing two template template-parameters in partial ordering,
7016 rewrite the one currently being used as an argument to have default
7017 arguments for all parameters. */
7018 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7019 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7020 if (pargs != error_mark_node)
7021 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7022 TYPE_TI_ARGS (arg));
7024 else
7026 tree aparms
7027 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7028 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7029 /*require_all*/true,
7030 /*use_default*/true);
7032 --processing_template_decl;
7033 return pargs;
7036 /* Subroutine of unify for the case when PARM is a
7037 BOUND_TEMPLATE_TEMPLATE_PARM. */
7039 static int
7040 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7041 bool explain_p)
7043 tree parmvec = TYPE_TI_ARGS (parm);
7044 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7046 /* The template template parm might be variadic and the argument
7047 not, so flatten both argument lists. */
7048 parmvec = expand_template_argument_pack (parmvec);
7049 argvec = expand_template_argument_pack (argvec);
7051 tree nparmvec = parmvec;
7052 if (flag_new_ttp)
7054 /* In keeping with P0522R0, adjust P's template arguments
7055 to apply to A's template; then flatten it again. */
7056 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7057 nparmvec = expand_template_argument_pack (nparmvec);
7060 if (unify (tparms, targs, nparmvec, argvec,
7061 UNIFY_ALLOW_NONE, explain_p))
7062 return 1;
7064 /* If the P0522 adjustment eliminated a pack expansion, deduce
7065 empty packs. */
7066 if (flag_new_ttp
7067 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7068 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7069 DEDUCE_EXACT, /*sub*/true, explain_p))
7070 return 1;
7072 return 0;
7075 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7076 template template parameters. Both PARM_PARMS and ARG_PARMS are
7077 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7078 or PARM_DECL.
7080 Consider the example:
7081 template <class T> class A;
7082 template<template <class U> class TT> class B;
7084 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7085 the parameters to A, and OUTER_ARGS contains A. */
7087 static int
7088 coerce_template_template_parms (tree parm_parms,
7089 tree arg_parms,
7090 tsubst_flags_t complain,
7091 tree in_decl,
7092 tree outer_args)
7094 int nparms, nargs, i;
7095 tree parm, arg;
7096 int variadic_p = 0;
7098 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7099 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7101 nparms = TREE_VEC_LENGTH (parm_parms);
7102 nargs = TREE_VEC_LENGTH (arg_parms);
7104 if (flag_new_ttp)
7106 /* P0522R0: A template template-parameter P is at least as specialized as
7107 a template template-argument A if, given the following rewrite to two
7108 function templates, the function template corresponding to P is at
7109 least as specialized as the function template corresponding to A
7110 according to the partial ordering rules for function templates
7111 ([temp.func.order]). Given an invented class template X with the
7112 template parameter list of A (including default arguments):
7114 * Each of the two function templates has the same template parameters,
7115 respectively, as P or A.
7117 * Each function template has a single function parameter whose type is
7118 a specialization of X with template arguments corresponding to the
7119 template parameters from the respective function template where, for
7120 each template parameter PP in the template parameter list of the
7121 function template, a corresponding template argument AA is formed. If
7122 PP declares a parameter pack, then AA is the pack expansion
7123 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7125 If the rewrite produces an invalid type, then P is not at least as
7126 specialized as A. */
7128 /* So coerce P's args to apply to A's parms, and then deduce between A's
7129 args and the converted args. If that succeeds, A is at least as
7130 specialized as P, so they match.*/
7131 tree pargs = template_parms_level_to_args (parm_parms);
7132 ++processing_template_decl;
7133 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7134 /*require_all*/true, /*use_default*/true);
7135 --processing_template_decl;
7136 if (pargs != error_mark_node)
7138 tree targs = make_tree_vec (nargs);
7139 tree aargs = template_parms_level_to_args (arg_parms);
7140 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7141 /*explain*/false))
7142 return 1;
7146 /* Determine whether we have a parameter pack at the end of the
7147 template template parameter's template parameter list. */
7148 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7150 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7152 if (error_operand_p (parm))
7153 return 0;
7155 switch (TREE_CODE (parm))
7157 case TEMPLATE_DECL:
7158 case TYPE_DECL:
7159 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7160 variadic_p = 1;
7161 break;
7163 case PARM_DECL:
7164 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7165 variadic_p = 1;
7166 break;
7168 default:
7169 gcc_unreachable ();
7173 if (nargs != nparms
7174 && !(variadic_p && nargs >= nparms - 1))
7175 return 0;
7177 /* Check all of the template parameters except the parameter pack at
7178 the end (if any). */
7179 for (i = 0; i < nparms - variadic_p; ++i)
7181 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7182 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7183 continue;
7185 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7186 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7188 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7189 outer_args))
7190 return 0;
7194 if (variadic_p)
7196 /* Check each of the template parameters in the template
7197 argument against the template parameter pack at the end of
7198 the template template parameter. */
7199 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7200 return 0;
7202 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7204 for (; i < nargs; ++i)
7206 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7207 continue;
7209 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7211 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7212 outer_args))
7213 return 0;
7217 return 1;
7220 /* Verifies that the deduced template arguments (in TARGS) for the
7221 template template parameters (in TPARMS) represent valid bindings,
7222 by comparing the template parameter list of each template argument
7223 to the template parameter list of its corresponding template
7224 template parameter, in accordance with DR150. This
7225 routine can only be called after all template arguments have been
7226 deduced. It will return TRUE if all of the template template
7227 parameter bindings are okay, FALSE otherwise. */
7228 bool
7229 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7231 int i, ntparms = TREE_VEC_LENGTH (tparms);
7232 bool ret = true;
7234 /* We're dealing with template parms in this process. */
7235 ++processing_template_decl;
7237 targs = INNERMOST_TEMPLATE_ARGS (targs);
7239 for (i = 0; i < ntparms; ++i)
7241 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7242 tree targ = TREE_VEC_ELT (targs, i);
7244 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7246 tree packed_args = NULL_TREE;
7247 int idx, len = 1;
7249 if (ARGUMENT_PACK_P (targ))
7251 /* Look inside the argument pack. */
7252 packed_args = ARGUMENT_PACK_ARGS (targ);
7253 len = TREE_VEC_LENGTH (packed_args);
7256 for (idx = 0; idx < len; ++idx)
7258 tree targ_parms = NULL_TREE;
7260 if (packed_args)
7261 /* Extract the next argument from the argument
7262 pack. */
7263 targ = TREE_VEC_ELT (packed_args, idx);
7265 if (PACK_EXPANSION_P (targ))
7266 /* Look at the pattern of the pack expansion. */
7267 targ = PACK_EXPANSION_PATTERN (targ);
7269 /* Extract the template parameters from the template
7270 argument. */
7271 if (TREE_CODE (targ) == TEMPLATE_DECL)
7272 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7273 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7274 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7276 /* Verify that we can coerce the template template
7277 parameters from the template argument to the template
7278 parameter. This requires an exact match. */
7279 if (targ_parms
7280 && !coerce_template_template_parms
7281 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7282 targ_parms,
7283 tf_none,
7284 tparm,
7285 targs))
7287 ret = false;
7288 goto out;
7294 out:
7296 --processing_template_decl;
7297 return ret;
7300 /* Since type attributes aren't mangled, we need to strip them from
7301 template type arguments. */
7303 static tree
7304 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7306 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7307 return arg;
7308 bool removed_attributes = false;
7309 tree canon = strip_typedefs (arg, &removed_attributes);
7310 if (removed_attributes
7311 && (complain & tf_warning))
7312 warning (OPT_Wignored_attributes,
7313 "ignoring attributes on template argument %qT", arg);
7314 return canon;
7317 /* And from inside dependent non-type arguments like sizeof(Type). */
7319 static tree
7320 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7322 if (!arg || arg == error_mark_node)
7323 return arg;
7324 bool removed_attributes = false;
7325 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7326 if (removed_attributes
7327 && (complain & tf_warning))
7328 warning (OPT_Wignored_attributes,
7329 "ignoring attributes in template argument %qE", arg);
7330 return canon;
7333 // A template declaration can be substituted for a constrained
7334 // template template parameter only when the argument is more
7335 // constrained than the parameter.
7336 static bool
7337 is_compatible_template_arg (tree parm, tree arg)
7339 tree parm_cons = get_constraints (parm);
7341 /* For now, allow constrained template template arguments
7342 and unconstrained template template parameters. */
7343 if (parm_cons == NULL_TREE)
7344 return true;
7346 tree arg_cons = get_constraints (arg);
7348 // If the template parameter is constrained, we need to rewrite its
7349 // constraints in terms of the ARG's template parameters. This ensures
7350 // that all of the template parameter types will have the same depth.
7352 // Note that this is only valid when coerce_template_template_parm is
7353 // true for the innermost template parameters of PARM and ARG. In other
7354 // words, because coercion is successful, this conversion will be valid.
7355 if (parm_cons)
7357 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7358 parm_cons = tsubst_constraint_info (parm_cons,
7359 INNERMOST_TEMPLATE_ARGS (args),
7360 tf_none, NULL_TREE);
7361 if (parm_cons == error_mark_node)
7362 return false;
7365 return subsumes (parm_cons, arg_cons);
7368 // Convert a placeholder argument into a binding to the original
7369 // parameter. The original parameter is saved as the TREE_TYPE of
7370 // ARG.
7371 static inline tree
7372 convert_wildcard_argument (tree parm, tree arg)
7374 TREE_TYPE (arg) = parm;
7375 return arg;
7378 /* Convert the indicated template ARG as necessary to match the
7379 indicated template PARM. Returns the converted ARG, or
7380 error_mark_node if the conversion was unsuccessful. Error and
7381 warning messages are issued under control of COMPLAIN. This
7382 conversion is for the Ith parameter in the parameter list. ARGS is
7383 the full set of template arguments deduced so far. */
7385 static tree
7386 convert_template_argument (tree parm,
7387 tree arg,
7388 tree args,
7389 tsubst_flags_t complain,
7390 int i,
7391 tree in_decl)
7393 tree orig_arg;
7394 tree val;
7395 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7397 if (parm == error_mark_node)
7398 return error_mark_node;
7400 /* Trivially convert placeholders. */
7401 if (TREE_CODE (arg) == WILDCARD_DECL)
7402 return convert_wildcard_argument (parm, arg);
7404 if (arg == any_targ_node)
7405 return arg;
7407 if (TREE_CODE (arg) == TREE_LIST
7408 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7410 /* The template argument was the name of some
7411 member function. That's usually
7412 invalid, but static members are OK. In any
7413 case, grab the underlying fields/functions
7414 and issue an error later if required. */
7415 orig_arg = TREE_VALUE (arg);
7416 TREE_TYPE (arg) = unknown_type_node;
7419 orig_arg = arg;
7421 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7422 requires_type = (TREE_CODE (parm) == TYPE_DECL
7423 || requires_tmpl_type);
7425 /* When determining whether an argument pack expansion is a template,
7426 look at the pattern. */
7427 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7428 arg = PACK_EXPANSION_PATTERN (arg);
7430 /* Deal with an injected-class-name used as a template template arg. */
7431 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7433 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7434 if (TREE_CODE (t) == TEMPLATE_DECL)
7436 if (cxx_dialect >= cxx11)
7437 /* OK under DR 1004. */;
7438 else if (complain & tf_warning_or_error)
7439 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7440 " used as template template argument", TYPE_NAME (arg));
7441 else if (flag_pedantic_errors)
7442 t = arg;
7444 arg = t;
7448 is_tmpl_type =
7449 ((TREE_CODE (arg) == TEMPLATE_DECL
7450 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7451 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7452 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7453 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7455 if (is_tmpl_type
7456 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7457 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7458 arg = TYPE_STUB_DECL (arg);
7460 is_type = TYPE_P (arg) || is_tmpl_type;
7462 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7463 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7465 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7467 if (complain & tf_error)
7468 error ("invalid use of destructor %qE as a type", orig_arg);
7469 return error_mark_node;
7472 permerror (input_location,
7473 "to refer to a type member of a template parameter, "
7474 "use %<typename %E%>", orig_arg);
7476 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7477 TREE_OPERAND (arg, 1),
7478 typename_type,
7479 complain);
7480 arg = orig_arg;
7481 is_type = 1;
7483 if (is_type != requires_type)
7485 if (in_decl)
7487 if (complain & tf_error)
7489 error ("type/value mismatch at argument %d in template "
7490 "parameter list for %qD",
7491 i + 1, in_decl);
7492 if (is_type)
7493 inform (input_location,
7494 " expected a constant of type %qT, got %qT",
7495 TREE_TYPE (parm),
7496 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7497 else if (requires_tmpl_type)
7498 inform (input_location,
7499 " expected a class template, got %qE", orig_arg);
7500 else
7501 inform (input_location,
7502 " expected a type, got %qE", orig_arg);
7505 return error_mark_node;
7507 if (is_tmpl_type ^ requires_tmpl_type)
7509 if (in_decl && (complain & tf_error))
7511 error ("type/value mismatch at argument %d in template "
7512 "parameter list for %qD",
7513 i + 1, in_decl);
7514 if (is_tmpl_type)
7515 inform (input_location,
7516 " expected a type, got %qT", DECL_NAME (arg));
7517 else
7518 inform (input_location,
7519 " expected a class template, got %qT", orig_arg);
7521 return error_mark_node;
7524 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7525 /* We already did the appropriate conversion when packing args. */
7526 val = orig_arg;
7527 else if (is_type)
7529 if (requires_tmpl_type)
7531 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7532 /* The number of argument required is not known yet.
7533 Just accept it for now. */
7534 val = TREE_TYPE (arg);
7535 else
7537 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7538 tree argparm;
7540 /* Strip alias templates that are equivalent to another
7541 template. */
7542 arg = get_underlying_template (arg);
7543 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7545 if (coerce_template_template_parms (parmparm, argparm,
7546 complain, in_decl,
7547 args))
7549 val = arg;
7551 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7552 TEMPLATE_DECL. */
7553 if (val != error_mark_node)
7555 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7556 val = TREE_TYPE (val);
7557 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7558 val = make_pack_expansion (val);
7561 else
7563 if (in_decl && (complain & tf_error))
7565 error ("type/value mismatch at argument %d in "
7566 "template parameter list for %qD",
7567 i + 1, in_decl);
7568 inform (input_location,
7569 " expected a template of type %qD, got %qT",
7570 parm, orig_arg);
7573 val = error_mark_node;
7576 // Check that the constraints are compatible before allowing the
7577 // substitution.
7578 if (val != error_mark_node)
7579 if (!is_compatible_template_arg (parm, arg))
7581 if (in_decl && (complain & tf_error))
7583 error ("constraint mismatch at argument %d in "
7584 "template parameter list for %qD",
7585 i + 1, in_decl);
7586 inform (input_location, " expected %qD but got %qD",
7587 parm, arg);
7589 val = error_mark_node;
7593 else
7594 val = orig_arg;
7595 /* We only form one instance of each template specialization.
7596 Therefore, if we use a non-canonical variant (i.e., a
7597 typedef), any future messages referring to the type will use
7598 the typedef, which is confusing if those future uses do not
7599 themselves also use the typedef. */
7600 if (TYPE_P (val))
7601 val = canonicalize_type_argument (val, complain);
7603 else
7605 tree t = TREE_TYPE (parm);
7607 if (tree a = type_uses_auto (t))
7609 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7610 if (t == error_mark_node)
7611 return error_mark_node;
7613 else
7614 t = tsubst (t, args, complain, in_decl);
7616 if (invalid_nontype_parm_type_p (t, complain))
7617 return error_mark_node;
7619 if (!type_dependent_expression_p (orig_arg)
7620 && !uses_template_parms (t))
7621 /* We used to call digest_init here. However, digest_init
7622 will report errors, which we don't want when complain
7623 is zero. More importantly, digest_init will try too
7624 hard to convert things: for example, `0' should not be
7625 converted to pointer type at this point according to
7626 the standard. Accepting this is not merely an
7627 extension, since deciding whether or not these
7628 conversions can occur is part of determining which
7629 function template to call, or whether a given explicit
7630 argument specification is valid. */
7631 val = convert_nontype_argument (t, orig_arg, complain);
7632 else
7633 val = canonicalize_expr_argument (orig_arg, complain);
7635 if (val == NULL_TREE)
7636 val = error_mark_node;
7637 else if (val == error_mark_node && (complain & tf_error))
7638 error ("could not convert template argument %qE from %qT to %qT",
7639 orig_arg, TREE_TYPE (orig_arg), t);
7641 if (INDIRECT_REF_P (val))
7643 /* Reject template arguments that are references to built-in
7644 functions with no library fallbacks. */
7645 const_tree inner = TREE_OPERAND (val, 0);
7646 const_tree innertype = TREE_TYPE (inner);
7647 if (innertype
7648 && TREE_CODE (innertype) == REFERENCE_TYPE
7649 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7650 && 0 < TREE_OPERAND_LENGTH (inner)
7651 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7652 return error_mark_node;
7655 if (TREE_CODE (val) == SCOPE_REF)
7657 /* Strip typedefs from the SCOPE_REF. */
7658 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7659 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7660 complain);
7661 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7662 QUALIFIED_NAME_IS_TEMPLATE (val));
7666 return val;
7669 /* Coerces the remaining template arguments in INNER_ARGS (from
7670 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7671 Returns the coerced argument pack. PARM_IDX is the position of this
7672 parameter in the template parameter list. ARGS is the original
7673 template argument list. */
7674 static tree
7675 coerce_template_parameter_pack (tree parms,
7676 int parm_idx,
7677 tree args,
7678 tree inner_args,
7679 int arg_idx,
7680 tree new_args,
7681 int* lost,
7682 tree in_decl,
7683 tsubst_flags_t complain)
7685 tree parm = TREE_VEC_ELT (parms, parm_idx);
7686 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7687 tree packed_args;
7688 tree argument_pack;
7689 tree packed_parms = NULL_TREE;
7691 if (arg_idx > nargs)
7692 arg_idx = nargs;
7694 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7696 /* When the template parameter is a non-type template parameter pack
7697 or template template parameter pack whose type or template
7698 parameters use parameter packs, we know exactly how many arguments
7699 we are looking for. Build a vector of the instantiated decls for
7700 these template parameters in PACKED_PARMS. */
7701 /* We can't use make_pack_expansion here because it would interpret a
7702 _DECL as a use rather than a declaration. */
7703 tree decl = TREE_VALUE (parm);
7704 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7705 SET_PACK_EXPANSION_PATTERN (exp, decl);
7706 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7707 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7709 TREE_VEC_LENGTH (args)--;
7710 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7711 TREE_VEC_LENGTH (args)++;
7713 if (packed_parms == error_mark_node)
7714 return error_mark_node;
7716 /* If we're doing a partial instantiation of a member template,
7717 verify that all of the types used for the non-type
7718 template parameter pack are, in fact, valid for non-type
7719 template parameters. */
7720 if (arg_idx < nargs
7721 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7723 int j, len = TREE_VEC_LENGTH (packed_parms);
7724 for (j = 0; j < len; ++j)
7726 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7727 if (invalid_nontype_parm_type_p (t, complain))
7728 return error_mark_node;
7730 /* We don't know how many args we have yet, just
7731 use the unconverted ones for now. */
7732 return NULL_TREE;
7735 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7737 /* Check if we have a placeholder pack, which indicates we're
7738 in the context of a introduction list. In that case we want
7739 to match this pack to the single placeholder. */
7740 else if (arg_idx < nargs
7741 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7742 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7744 nargs = arg_idx + 1;
7745 packed_args = make_tree_vec (1);
7747 else
7748 packed_args = make_tree_vec (nargs - arg_idx);
7750 /* Convert the remaining arguments, which will be a part of the
7751 parameter pack "parm". */
7752 int first_pack_arg = arg_idx;
7753 for (; arg_idx < nargs; ++arg_idx)
7755 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7756 tree actual_parm = TREE_VALUE (parm);
7757 int pack_idx = arg_idx - first_pack_arg;
7759 if (packed_parms)
7761 /* Once we've packed as many args as we have types, stop. */
7762 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7763 break;
7764 else if (PACK_EXPANSION_P (arg))
7765 /* We don't know how many args we have yet, just
7766 use the unconverted ones for now. */
7767 return NULL_TREE;
7768 else
7769 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7772 if (arg == error_mark_node)
7774 if (complain & tf_error)
7775 error ("template argument %d is invalid", arg_idx + 1);
7777 else
7778 arg = convert_template_argument (actual_parm,
7779 arg, new_args, complain, parm_idx,
7780 in_decl);
7781 if (arg == error_mark_node)
7782 (*lost)++;
7783 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7786 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7787 && TREE_VEC_LENGTH (packed_args) > 0)
7789 if (complain & tf_error)
7790 error ("wrong number of template arguments (%d, should be %d)",
7791 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7792 return error_mark_node;
7795 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7796 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7797 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7798 else
7800 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7801 TREE_CONSTANT (argument_pack) = 1;
7804 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7805 if (CHECKING_P)
7806 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7807 TREE_VEC_LENGTH (packed_args));
7808 return argument_pack;
7811 /* Returns the number of pack expansions in the template argument vector
7812 ARGS. */
7814 static int
7815 pack_expansion_args_count (tree args)
7817 int i;
7818 int count = 0;
7819 if (args)
7820 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7822 tree elt = TREE_VEC_ELT (args, i);
7823 if (elt && PACK_EXPANSION_P (elt))
7824 ++count;
7826 return count;
7829 /* Convert all template arguments to their appropriate types, and
7830 return a vector containing the innermost resulting template
7831 arguments. If any error occurs, return error_mark_node. Error and
7832 warning messages are issued under control of COMPLAIN.
7834 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7835 for arguments not specified in ARGS. Otherwise, if
7836 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7837 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7838 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7839 ARGS. */
7841 static tree
7842 coerce_template_parms (tree parms,
7843 tree args,
7844 tree in_decl,
7845 tsubst_flags_t complain,
7846 bool require_all_args,
7847 bool use_default_args)
7849 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7850 tree orig_inner_args;
7851 tree inner_args;
7852 tree new_args;
7853 tree new_inner_args;
7854 int saved_unevaluated_operand;
7855 int saved_inhibit_evaluation_warnings;
7857 /* When used as a boolean value, indicates whether this is a
7858 variadic template parameter list. Since it's an int, we can also
7859 subtract it from nparms to get the number of non-variadic
7860 parameters. */
7861 int variadic_p = 0;
7862 int variadic_args_p = 0;
7863 int post_variadic_parms = 0;
7865 /* Likewise for parameters with default arguments. */
7866 int default_p = 0;
7868 if (args == error_mark_node)
7869 return error_mark_node;
7871 nparms = TREE_VEC_LENGTH (parms);
7873 /* Determine if there are any parameter packs or default arguments. */
7874 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7876 tree parm = TREE_VEC_ELT (parms, parm_idx);
7877 if (variadic_p)
7878 ++post_variadic_parms;
7879 if (template_parameter_pack_p (TREE_VALUE (parm)))
7880 ++variadic_p;
7881 if (TREE_PURPOSE (parm))
7882 ++default_p;
7885 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7886 /* If there are no parameters that follow a parameter pack, we need to
7887 expand any argument packs so that we can deduce a parameter pack from
7888 some non-packed args followed by an argument pack, as in variadic85.C.
7889 If there are such parameters, we need to leave argument packs intact
7890 so the arguments are assigned properly. This can happen when dealing
7891 with a nested class inside a partial specialization of a class
7892 template, as in variadic92.C, or when deducing a template parameter pack
7893 from a sub-declarator, as in variadic114.C. */
7894 if (!post_variadic_parms)
7895 inner_args = expand_template_argument_pack (inner_args);
7897 /* Count any pack expansion args. */
7898 variadic_args_p = pack_expansion_args_count (inner_args);
7900 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7901 if ((nargs - variadic_args_p > nparms && !variadic_p)
7902 || (nargs < nparms - variadic_p
7903 && require_all_args
7904 && !variadic_args_p
7905 && (!use_default_args
7906 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
7907 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
7909 if (complain & tf_error)
7911 if (variadic_p || default_p)
7913 nparms -= variadic_p + default_p;
7914 error ("wrong number of template arguments "
7915 "(%d, should be at least %d)", nargs, nparms);
7917 else
7918 error ("wrong number of template arguments "
7919 "(%d, should be %d)", nargs, nparms);
7921 if (in_decl)
7922 inform (DECL_SOURCE_LOCATION (in_decl),
7923 "provided for %qD", in_decl);
7926 return error_mark_node;
7928 /* We can't pass a pack expansion to a non-pack parameter of an alias
7929 template (DR 1430). */
7930 else if (in_decl
7931 && (DECL_ALIAS_TEMPLATE_P (in_decl)
7932 || concept_template_p (in_decl))
7933 && variadic_args_p
7934 && nargs - variadic_args_p < nparms - variadic_p)
7936 if (complain & tf_error)
7938 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
7940 tree arg = TREE_VEC_ELT (inner_args, i);
7941 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
7943 if (PACK_EXPANSION_P (arg)
7944 && !template_parameter_pack_p (parm))
7946 if (DECL_ALIAS_TEMPLATE_P (in_decl))
7947 error_at (location_of (arg),
7948 "pack expansion argument for non-pack parameter "
7949 "%qD of alias template %qD", parm, in_decl);
7950 else
7951 error_at (location_of (arg),
7952 "pack expansion argument for non-pack parameter "
7953 "%qD of concept %qD", parm, in_decl);
7954 inform (DECL_SOURCE_LOCATION (parm), "declared here");
7955 goto found;
7958 gcc_unreachable ();
7959 found:;
7961 return error_mark_node;
7964 /* We need to evaluate the template arguments, even though this
7965 template-id may be nested within a "sizeof". */
7966 saved_unevaluated_operand = cp_unevaluated_operand;
7967 cp_unevaluated_operand = 0;
7968 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
7969 c_inhibit_evaluation_warnings = 0;
7970 new_inner_args = make_tree_vec (nparms);
7971 new_args = add_outermost_template_args (args, new_inner_args);
7972 int pack_adjust = 0;
7973 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
7975 tree arg;
7976 tree parm;
7978 /* Get the Ith template parameter. */
7979 parm = TREE_VEC_ELT (parms, parm_idx);
7981 if (parm == error_mark_node)
7983 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
7984 continue;
7987 /* Calculate the next argument. */
7988 if (arg_idx < nargs)
7989 arg = TREE_VEC_ELT (inner_args, arg_idx);
7990 else
7991 arg = NULL_TREE;
7993 if (template_parameter_pack_p (TREE_VALUE (parm))
7994 && !(arg && ARGUMENT_PACK_P (arg)))
7996 /* Some arguments will be placed in the
7997 template parameter pack PARM. */
7998 arg = coerce_template_parameter_pack (parms, parm_idx, args,
7999 inner_args, arg_idx,
8000 new_args, &lost,
8001 in_decl, complain);
8003 if (arg == NULL_TREE)
8005 /* We don't know how many args we have yet, just use the
8006 unconverted (and still packed) ones for now. */
8007 new_inner_args = orig_inner_args;
8008 arg_idx = nargs;
8009 break;
8012 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8014 /* Store this argument. */
8015 if (arg == error_mark_node)
8017 lost++;
8018 /* We are done with all of the arguments. */
8019 arg_idx = nargs;
8021 else
8023 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8024 arg_idx += pack_adjust;
8027 continue;
8029 else if (arg)
8031 if (PACK_EXPANSION_P (arg))
8033 /* "If every valid specialization of a variadic template
8034 requires an empty template parameter pack, the template is
8035 ill-formed, no diagnostic required." So check that the
8036 pattern works with this parameter. */
8037 tree pattern = PACK_EXPANSION_PATTERN (arg);
8038 tree conv = convert_template_argument (TREE_VALUE (parm),
8039 pattern, new_args,
8040 complain, parm_idx,
8041 in_decl);
8042 if (conv == error_mark_node)
8044 if (complain & tf_error)
8045 inform (input_location, "so any instantiation with a "
8046 "non-empty parameter pack would be ill-formed");
8047 ++lost;
8049 else if (TYPE_P (conv) && !TYPE_P (pattern))
8050 /* Recover from missing typename. */
8051 TREE_VEC_ELT (inner_args, arg_idx)
8052 = make_pack_expansion (conv);
8054 /* We don't know how many args we have yet, just
8055 use the unconverted ones for now. */
8056 new_inner_args = inner_args;
8057 arg_idx = nargs;
8058 break;
8061 else if (require_all_args)
8063 /* There must be a default arg in this case. */
8064 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8065 complain, in_decl);
8066 /* The position of the first default template argument,
8067 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8068 Record that. */
8069 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8070 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8071 arg_idx - pack_adjust);
8073 else
8074 break;
8076 if (arg == error_mark_node)
8078 if (complain & tf_error)
8079 error ("template argument %d is invalid", arg_idx + 1);
8081 else if (!arg)
8082 /* This only occurs if there was an error in the template
8083 parameter list itself (which we would already have
8084 reported) that we are trying to recover from, e.g., a class
8085 template with a parameter list such as
8086 template<typename..., typename>. */
8087 ++lost;
8088 else
8089 arg = convert_template_argument (TREE_VALUE (parm),
8090 arg, new_args, complain,
8091 parm_idx, in_decl);
8093 if (arg == error_mark_node)
8094 lost++;
8095 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8097 cp_unevaluated_operand = saved_unevaluated_operand;
8098 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8100 if (variadic_p && arg_idx < nargs)
8102 if (complain & tf_error)
8104 error ("wrong number of template arguments "
8105 "(%d, should be %d)", nargs, arg_idx);
8106 if (in_decl)
8107 error ("provided for %q+D", in_decl);
8109 return error_mark_node;
8112 if (lost)
8113 return error_mark_node;
8115 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8116 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8117 TREE_VEC_LENGTH (new_inner_args));
8119 return new_inner_args;
8122 /* Convert all template arguments to their appropriate types, and
8123 return a vector containing the innermost resulting template
8124 arguments. If any error occurs, return error_mark_node. Error and
8125 warning messages are not issued.
8127 Note that no function argument deduction is performed, and default
8128 arguments are used to fill in unspecified arguments. */
8129 tree
8130 coerce_template_parms (tree parms, tree args, tree in_decl)
8132 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8135 /* Convert all template arguments to their appropriate type, and
8136 instantiate default arguments as needed. This returns a vector
8137 containing the innermost resulting template arguments, or
8138 error_mark_node if unsuccessful. */
8139 tree
8140 coerce_template_parms (tree parms, tree args, tree in_decl,
8141 tsubst_flags_t complain)
8143 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8146 /* Like coerce_template_parms. If PARMS represents all template
8147 parameters levels, this function returns a vector of vectors
8148 representing all the resulting argument levels. Note that in this
8149 case, only the innermost arguments are coerced because the
8150 outermost ones are supposed to have been coerced already.
8152 Otherwise, if PARMS represents only (the innermost) vector of
8153 parameters, this function returns a vector containing just the
8154 innermost resulting arguments. */
8156 static tree
8157 coerce_innermost_template_parms (tree parms,
8158 tree args,
8159 tree in_decl,
8160 tsubst_flags_t complain,
8161 bool require_all_args,
8162 bool use_default_args)
8164 int parms_depth = TMPL_PARMS_DEPTH (parms);
8165 int args_depth = TMPL_ARGS_DEPTH (args);
8166 tree coerced_args;
8168 if (parms_depth > 1)
8170 coerced_args = make_tree_vec (parms_depth);
8171 tree level;
8172 int cur_depth;
8174 for (level = parms, cur_depth = parms_depth;
8175 parms_depth > 0 && level != NULL_TREE;
8176 level = TREE_CHAIN (level), --cur_depth)
8178 tree l;
8179 if (cur_depth == args_depth)
8180 l = coerce_template_parms (TREE_VALUE (level),
8181 args, in_decl, complain,
8182 require_all_args,
8183 use_default_args);
8184 else
8185 l = TMPL_ARGS_LEVEL (args, cur_depth);
8187 if (l == error_mark_node)
8188 return error_mark_node;
8190 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8193 else
8194 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8195 args, in_decl, complain,
8196 require_all_args,
8197 use_default_args);
8198 return coerced_args;
8201 /* Returns 1 if template args OT and NT are equivalent. */
8204 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8206 if (nt == ot)
8207 return 1;
8208 if (nt == NULL_TREE || ot == NULL_TREE)
8209 return false;
8210 if (nt == any_targ_node || ot == any_targ_node)
8211 return true;
8213 if (TREE_CODE (nt) == TREE_VEC)
8214 /* For member templates */
8215 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8216 else if (PACK_EXPANSION_P (ot))
8217 return (PACK_EXPANSION_P (nt)
8218 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8219 PACK_EXPANSION_PATTERN (nt))
8220 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8221 PACK_EXPANSION_EXTRA_ARGS (nt)));
8222 else if (ARGUMENT_PACK_P (ot))
8224 int i, len;
8225 tree opack, npack;
8227 if (!ARGUMENT_PACK_P (nt))
8228 return 0;
8230 opack = ARGUMENT_PACK_ARGS (ot);
8231 npack = ARGUMENT_PACK_ARGS (nt);
8232 len = TREE_VEC_LENGTH (opack);
8233 if (TREE_VEC_LENGTH (npack) != len)
8234 return 0;
8235 for (i = 0; i < len; ++i)
8236 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8237 TREE_VEC_ELT (npack, i)))
8238 return 0;
8239 return 1;
8241 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8242 gcc_unreachable ();
8243 else if (TYPE_P (nt))
8245 if (!TYPE_P (ot))
8246 return false;
8247 /* Don't treat an alias template specialization with dependent
8248 arguments as equivalent to its underlying type when used as a
8249 template argument; we need them to be distinct so that we
8250 substitute into the specialization arguments at instantiation
8251 time. And aliases can't be equivalent without being ==, so
8252 we don't need to look any deeper.
8254 During partial ordering, however, we need to treat them normally so
8255 that we can order uses of the same alias with different
8256 cv-qualification (79960). */
8257 if (!partial_order
8258 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8259 return false;
8260 else
8261 return same_type_p (ot, nt);
8263 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8264 return 0;
8265 else
8267 /* Try to treat a template non-type argument that has been converted
8268 to the parameter type as equivalent to one that hasn't yet. */
8269 for (enum tree_code code1 = TREE_CODE (ot);
8270 CONVERT_EXPR_CODE_P (code1)
8271 || code1 == NON_LVALUE_EXPR;
8272 code1 = TREE_CODE (ot))
8273 ot = TREE_OPERAND (ot, 0);
8274 for (enum tree_code code2 = TREE_CODE (nt);
8275 CONVERT_EXPR_CODE_P (code2)
8276 || code2 == NON_LVALUE_EXPR;
8277 code2 = TREE_CODE (nt))
8278 nt = TREE_OPERAND (nt, 0);
8280 return cp_tree_equal (ot, nt);
8284 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8285 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8286 NEWARG_PTR with the offending arguments if they are non-NULL. */
8289 comp_template_args (tree oldargs, tree newargs,
8290 tree *oldarg_ptr, tree *newarg_ptr,
8291 bool partial_order)
8293 int i;
8295 if (oldargs == newargs)
8296 return 1;
8298 if (!oldargs || !newargs)
8299 return 0;
8301 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8302 return 0;
8304 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8306 tree nt = TREE_VEC_ELT (newargs, i);
8307 tree ot = TREE_VEC_ELT (oldargs, i);
8309 if (! template_args_equal (ot, nt, partial_order))
8311 if (oldarg_ptr != NULL)
8312 *oldarg_ptr = ot;
8313 if (newarg_ptr != NULL)
8314 *newarg_ptr = nt;
8315 return 0;
8318 return 1;
8321 inline bool
8322 comp_template_args_porder (tree oargs, tree nargs)
8324 return comp_template_args (oargs, nargs, NULL, NULL, true);
8327 static void
8328 add_pending_template (tree d)
8330 tree ti = (TYPE_P (d)
8331 ? CLASSTYPE_TEMPLATE_INFO (d)
8332 : DECL_TEMPLATE_INFO (d));
8333 struct pending_template *pt;
8334 int level;
8336 if (TI_PENDING_TEMPLATE_FLAG (ti))
8337 return;
8339 /* We are called both from instantiate_decl, where we've already had a
8340 tinst_level pushed, and instantiate_template, where we haven't.
8341 Compensate. */
8342 level = !current_tinst_level || current_tinst_level->decl != d;
8344 if (level)
8345 push_tinst_level (d);
8347 pt = ggc_alloc<pending_template> ();
8348 pt->next = NULL;
8349 pt->tinst = current_tinst_level;
8350 if (last_pending_template)
8351 last_pending_template->next = pt;
8352 else
8353 pending_templates = pt;
8355 last_pending_template = pt;
8357 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8359 if (level)
8360 pop_tinst_level ();
8364 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8365 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8366 documentation for TEMPLATE_ID_EXPR. */
8368 tree
8369 lookup_template_function (tree fns, tree arglist)
8371 tree type;
8373 if (fns == error_mark_node || arglist == error_mark_node)
8374 return error_mark_node;
8376 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8378 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8380 error ("%q#D is not a function template", fns);
8381 return error_mark_node;
8384 if (BASELINK_P (fns))
8386 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8387 unknown_type_node,
8388 BASELINK_FUNCTIONS (fns),
8389 arglist);
8390 return fns;
8393 type = TREE_TYPE (fns);
8394 if (TREE_CODE (fns) == OVERLOAD || !type)
8395 type = unknown_type_node;
8397 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8400 /* Within the scope of a template class S<T>, the name S gets bound
8401 (in build_self_reference) to a TYPE_DECL for the class, not a
8402 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8403 or one of its enclosing classes, and that type is a template,
8404 return the associated TEMPLATE_DECL. Otherwise, the original
8405 DECL is returned.
8407 Also handle the case when DECL is a TREE_LIST of ambiguous
8408 injected-class-names from different bases. */
8410 tree
8411 maybe_get_template_decl_from_type_decl (tree decl)
8413 if (decl == NULL_TREE)
8414 return decl;
8416 /* DR 176: A lookup that finds an injected-class-name (10.2
8417 [class.member.lookup]) can result in an ambiguity in certain cases
8418 (for example, if it is found in more than one base class). If all of
8419 the injected-class-names that are found refer to specializations of
8420 the same class template, and if the name is followed by a
8421 template-argument-list, the reference refers to the class template
8422 itself and not a specialization thereof, and is not ambiguous. */
8423 if (TREE_CODE (decl) == TREE_LIST)
8425 tree t, tmpl = NULL_TREE;
8426 for (t = decl; t; t = TREE_CHAIN (t))
8428 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8429 if (!tmpl)
8430 tmpl = elt;
8431 else if (tmpl != elt)
8432 break;
8434 if (tmpl && t == NULL_TREE)
8435 return tmpl;
8436 else
8437 return decl;
8440 return (decl != NULL_TREE
8441 && DECL_SELF_REFERENCE_P (decl)
8442 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8443 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8446 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8447 parameters, find the desired type.
8449 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8451 IN_DECL, if non-NULL, is the template declaration we are trying to
8452 instantiate.
8454 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8455 the class we are looking up.
8457 Issue error and warning messages under control of COMPLAIN.
8459 If the template class is really a local class in a template
8460 function, then the FUNCTION_CONTEXT is the function in which it is
8461 being instantiated.
8463 ??? Note that this function is currently called *twice* for each
8464 template-id: the first time from the parser, while creating the
8465 incomplete type (finish_template_type), and the second type during the
8466 real instantiation (instantiate_template_class). This is surely something
8467 that we want to avoid. It also causes some problems with argument
8468 coercion (see convert_nontype_argument for more information on this). */
8470 static tree
8471 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8472 int entering_scope, tsubst_flags_t complain)
8474 tree templ = NULL_TREE, parmlist;
8475 tree t;
8476 spec_entry **slot;
8477 spec_entry *entry;
8478 spec_entry elt;
8479 hashval_t hash;
8481 if (identifier_p (d1))
8483 tree value = innermost_non_namespace_value (d1);
8484 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8485 templ = value;
8486 else
8488 if (context)
8489 push_decl_namespace (context);
8490 templ = lookup_name (d1);
8491 templ = maybe_get_template_decl_from_type_decl (templ);
8492 if (context)
8493 pop_decl_namespace ();
8495 if (templ)
8496 context = DECL_CONTEXT (templ);
8498 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8500 tree type = TREE_TYPE (d1);
8502 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8503 an implicit typename for the second A. Deal with it. */
8504 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8505 type = TREE_TYPE (type);
8507 if (CLASSTYPE_TEMPLATE_INFO (type))
8509 templ = CLASSTYPE_TI_TEMPLATE (type);
8510 d1 = DECL_NAME (templ);
8513 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8514 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8516 templ = TYPE_TI_TEMPLATE (d1);
8517 d1 = DECL_NAME (templ);
8519 else if (DECL_TYPE_TEMPLATE_P (d1))
8521 templ = d1;
8522 d1 = DECL_NAME (templ);
8523 context = DECL_CONTEXT (templ);
8525 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8527 templ = d1;
8528 d1 = DECL_NAME (templ);
8531 /* Issue an error message if we didn't find a template. */
8532 if (! templ)
8534 if (complain & tf_error)
8535 error ("%qT is not a template", d1);
8536 return error_mark_node;
8539 if (TREE_CODE (templ) != TEMPLATE_DECL
8540 /* Make sure it's a user visible template, if it was named by
8541 the user. */
8542 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8543 && !PRIMARY_TEMPLATE_P (templ)))
8545 if (complain & tf_error)
8547 error ("non-template type %qT used as a template", d1);
8548 if (in_decl)
8549 error ("for template declaration %q+D", in_decl);
8551 return error_mark_node;
8554 complain &= ~tf_user;
8556 /* An alias that just changes the name of a template is equivalent to the
8557 other template, so if any of the arguments are pack expansions, strip
8558 the alias to avoid problems with a pack expansion passed to a non-pack
8559 alias template parameter (DR 1430). */
8560 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8561 templ = get_underlying_template (templ);
8563 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8565 tree parm;
8566 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8567 if (arglist2 == error_mark_node
8568 || (!uses_template_parms (arglist2)
8569 && check_instantiated_args (templ, arglist2, complain)))
8570 return error_mark_node;
8572 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8573 return parm;
8575 else
8577 tree template_type = TREE_TYPE (templ);
8578 tree gen_tmpl;
8579 tree type_decl;
8580 tree found = NULL_TREE;
8581 int arg_depth;
8582 int parm_depth;
8583 int is_dependent_type;
8584 int use_partial_inst_tmpl = false;
8586 if (template_type == error_mark_node)
8587 /* An error occurred while building the template TEMPL, and a
8588 diagnostic has most certainly been emitted for that
8589 already. Let's propagate that error. */
8590 return error_mark_node;
8592 gen_tmpl = most_general_template (templ);
8593 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8594 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8595 arg_depth = TMPL_ARGS_DEPTH (arglist);
8597 if (arg_depth == 1 && parm_depth > 1)
8599 /* We've been given an incomplete set of template arguments.
8600 For example, given:
8602 template <class T> struct S1 {
8603 template <class U> struct S2 {};
8604 template <class U> struct S2<U*> {};
8607 we will be called with an ARGLIST of `U*', but the
8608 TEMPLATE will be `template <class T> template
8609 <class U> struct S1<T>::S2'. We must fill in the missing
8610 arguments. */
8611 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8612 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8613 arg_depth = TMPL_ARGS_DEPTH (arglist);
8616 /* Now we should have enough arguments. */
8617 gcc_assert (parm_depth == arg_depth);
8619 /* From here on, we're only interested in the most general
8620 template. */
8622 /* Calculate the BOUND_ARGS. These will be the args that are
8623 actually tsubst'd into the definition to create the
8624 instantiation. */
8625 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8626 complain,
8627 /*require_all_args=*/true,
8628 /*use_default_args=*/true);
8630 if (arglist == error_mark_node)
8631 /* We were unable to bind the arguments. */
8632 return error_mark_node;
8634 /* In the scope of a template class, explicit references to the
8635 template class refer to the type of the template, not any
8636 instantiation of it. For example, in:
8638 template <class T> class C { void f(C<T>); }
8640 the `C<T>' is just the same as `C'. Outside of the
8641 class, however, such a reference is an instantiation. */
8642 if (entering_scope
8643 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8644 || currently_open_class (template_type))
8646 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8648 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8649 return template_type;
8652 /* If we already have this specialization, return it. */
8653 elt.tmpl = gen_tmpl;
8654 elt.args = arglist;
8655 elt.spec = NULL_TREE;
8656 hash = spec_hasher::hash (&elt);
8657 entry = type_specializations->find_with_hash (&elt, hash);
8659 if (entry)
8660 return entry->spec;
8662 /* If the the template's constraints are not satisfied,
8663 then we cannot form a valid type.
8665 Note that the check is deferred until after the hash
8666 lookup. This prevents redundant checks on previously
8667 instantiated specializations. */
8668 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8670 if (complain & tf_error)
8672 error ("template constraint failure");
8673 diagnose_constraints (input_location, gen_tmpl, arglist);
8675 return error_mark_node;
8678 is_dependent_type = uses_template_parms (arglist);
8680 /* If the deduced arguments are invalid, then the binding
8681 failed. */
8682 if (!is_dependent_type
8683 && check_instantiated_args (gen_tmpl,
8684 INNERMOST_TEMPLATE_ARGS (arglist),
8685 complain))
8686 return error_mark_node;
8688 if (!is_dependent_type
8689 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8690 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8691 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8693 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8694 DECL_NAME (gen_tmpl),
8695 /*tag_scope=*/ts_global);
8696 return found;
8699 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8700 complain, in_decl);
8701 if (context == error_mark_node)
8702 return error_mark_node;
8704 if (!context)
8705 context = global_namespace;
8707 /* Create the type. */
8708 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8710 /* The user referred to a specialization of an alias
8711 template represented by GEN_TMPL.
8713 [temp.alias]/2 says:
8715 When a template-id refers to the specialization of an
8716 alias template, it is equivalent to the associated
8717 type obtained by substitution of its
8718 template-arguments for the template-parameters in the
8719 type-id of the alias template. */
8721 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8722 /* Note that the call above (by indirectly calling
8723 register_specialization in tsubst_decl) registers the
8724 TYPE_DECL representing the specialization of the alias
8725 template. So next time someone substitutes ARGLIST for
8726 the template parms into the alias template (GEN_TMPL),
8727 she'll get that TYPE_DECL back. */
8729 if (t == error_mark_node)
8730 return t;
8732 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8734 if (!is_dependent_type)
8736 set_current_access_from_decl (TYPE_NAME (template_type));
8737 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8738 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8739 arglist, complain, in_decl),
8740 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8741 arglist, complain, in_decl),
8742 SCOPED_ENUM_P (template_type), NULL);
8744 if (t == error_mark_node)
8745 return t;
8747 else
8749 /* We don't want to call start_enum for this type, since
8750 the values for the enumeration constants may involve
8751 template parameters. And, no one should be interested
8752 in the enumeration constants for such a type. */
8753 t = cxx_make_type (ENUMERAL_TYPE);
8754 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8756 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8757 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8758 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8760 else if (CLASS_TYPE_P (template_type))
8762 t = make_class_type (TREE_CODE (template_type));
8763 CLASSTYPE_DECLARED_CLASS (t)
8764 = CLASSTYPE_DECLARED_CLASS (template_type);
8765 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8767 /* A local class. Make sure the decl gets registered properly. */
8768 if (context == current_function_decl)
8769 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8771 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8772 /* This instantiation is another name for the primary
8773 template type. Set the TYPE_CANONICAL field
8774 appropriately. */
8775 TYPE_CANONICAL (t) = template_type;
8776 else if (any_template_arguments_need_structural_equality_p (arglist))
8777 /* Some of the template arguments require structural
8778 equality testing, so this template class requires
8779 structural equality testing. */
8780 SET_TYPE_STRUCTURAL_EQUALITY (t);
8782 else
8783 gcc_unreachable ();
8785 /* If we called start_enum or pushtag above, this information
8786 will already be set up. */
8787 if (!TYPE_NAME (t))
8789 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8791 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8792 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8793 DECL_SOURCE_LOCATION (type_decl)
8794 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8796 else
8797 type_decl = TYPE_NAME (t);
8799 if (CLASS_TYPE_P (template_type))
8801 TREE_PRIVATE (type_decl)
8802 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8803 TREE_PROTECTED (type_decl)
8804 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8805 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8807 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8808 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8812 if (OVERLOAD_TYPE_P (t)
8813 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8815 static const char *tags[] = {"abi_tag", "may_alias"};
8817 for (unsigned ix = 0; ix != 2; ix++)
8819 tree attributes
8820 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8822 if (attributes)
8823 TYPE_ATTRIBUTES (t)
8824 = tree_cons (TREE_PURPOSE (attributes),
8825 TREE_VALUE (attributes),
8826 TYPE_ATTRIBUTES (t));
8830 /* Let's consider the explicit specialization of a member
8831 of a class template specialization that is implicitly instantiated,
8832 e.g.:
8833 template<class T>
8834 struct S
8836 template<class U> struct M {}; //#0
8839 template<>
8840 template<>
8841 struct S<int>::M<char> //#1
8843 int i;
8845 [temp.expl.spec]/4 says this is valid.
8847 In this case, when we write:
8848 S<int>::M<char> m;
8850 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8851 the one of #0.
8853 When we encounter #1, we want to store the partial instantiation
8854 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8856 For all cases other than this "explicit specialization of member of a
8857 class template", we just want to store the most general template into
8858 the CLASSTYPE_TI_TEMPLATE of M.
8860 This case of "explicit specialization of member of a class template"
8861 only happens when:
8862 1/ the enclosing class is an instantiation of, and therefore not
8863 the same as, the context of the most general template, and
8864 2/ we aren't looking at the partial instantiation itself, i.e.
8865 the innermost arguments are not the same as the innermost parms of
8866 the most general template.
8868 So it's only when 1/ and 2/ happens that we want to use the partial
8869 instantiation of the member template in lieu of its most general
8870 template. */
8872 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8873 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8874 /* the enclosing class must be an instantiation... */
8875 && CLASS_TYPE_P (context)
8876 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8878 TREE_VEC_LENGTH (arglist)--;
8879 ++processing_template_decl;
8880 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
8881 tree partial_inst_args =
8882 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
8883 arglist, complain, NULL_TREE);
8884 --processing_template_decl;
8885 TREE_VEC_LENGTH (arglist)++;
8886 if (partial_inst_args == error_mark_node)
8887 return error_mark_node;
8888 use_partial_inst_tmpl =
8889 /*...and we must not be looking at the partial instantiation
8890 itself. */
8891 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
8892 partial_inst_args);
8895 if (!use_partial_inst_tmpl)
8896 /* This case is easy; there are no member templates involved. */
8897 found = gen_tmpl;
8898 else
8900 /* This is a full instantiation of a member template. Find
8901 the partial instantiation of which this is an instance. */
8903 /* Temporarily reduce by one the number of levels in the ARGLIST
8904 so as to avoid comparing the last set of arguments. */
8905 TREE_VEC_LENGTH (arglist)--;
8906 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
8907 TREE_VEC_LENGTH (arglist)++;
8908 /* FOUND is either a proper class type, or an alias
8909 template specialization. In the later case, it's a
8910 TYPE_DECL, resulting from the substituting of arguments
8911 for parameters in the TYPE_DECL of the alias template
8912 done earlier. So be careful while getting the template
8913 of FOUND. */
8914 found = (TREE_CODE (found) == TEMPLATE_DECL
8915 ? found
8916 : (TREE_CODE (found) == TYPE_DECL
8917 ? DECL_TI_TEMPLATE (found)
8918 : CLASSTYPE_TI_TEMPLATE (found)));
8921 // Build template info for the new specialization.
8922 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
8924 elt.spec = t;
8925 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
8926 entry = ggc_alloc<spec_entry> ();
8927 *entry = elt;
8928 *slot = entry;
8930 /* Note this use of the partial instantiation so we can check it
8931 later in maybe_process_partial_specialization. */
8932 DECL_TEMPLATE_INSTANTIATIONS (found)
8933 = tree_cons (arglist, t,
8934 DECL_TEMPLATE_INSTANTIATIONS (found));
8936 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
8937 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8938 /* Now that the type has been registered on the instantiations
8939 list, we set up the enumerators. Because the enumeration
8940 constants may involve the enumeration type itself, we make
8941 sure to register the type first, and then create the
8942 constants. That way, doing tsubst_expr for the enumeration
8943 constants won't result in recursive calls here; we'll find
8944 the instantiation and exit above. */
8945 tsubst_enum (template_type, t, arglist);
8947 if (CLASS_TYPE_P (template_type) && is_dependent_type)
8948 /* If the type makes use of template parameters, the
8949 code that generates debugging information will crash. */
8950 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
8952 /* Possibly limit visibility based on template args. */
8953 TREE_PUBLIC (type_decl) = 1;
8954 determine_visibility (type_decl);
8956 inherit_targ_abi_tags (t);
8958 return t;
8962 /* Wrapper for lookup_template_class_1. */
8964 tree
8965 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
8966 int entering_scope, tsubst_flags_t complain)
8968 tree ret;
8969 timevar_push (TV_TEMPLATE_INST);
8970 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
8971 entering_scope, complain);
8972 timevar_pop (TV_TEMPLATE_INST);
8973 return ret;
8976 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
8978 tree
8979 lookup_template_variable (tree templ, tree arglist)
8981 /* The type of the expression is NULL_TREE since the template-id could refer
8982 to an explicit or partial specialization. */
8983 tree type = NULL_TREE;
8984 if (flag_concepts && variable_concept_p (templ))
8985 /* Except that concepts are always bool. */
8986 type = boolean_type_node;
8987 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
8990 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
8992 tree
8993 finish_template_variable (tree var, tsubst_flags_t complain)
8995 tree templ = TREE_OPERAND (var, 0);
8996 tree arglist = TREE_OPERAND (var, 1);
8998 /* We never want to return a VAR_DECL for a variable concept, since they
8999 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9000 bool concept_p = flag_concepts && variable_concept_p (templ);
9001 if (concept_p && processing_template_decl)
9002 return var;
9004 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9005 arglist = add_outermost_template_args (tmpl_args, arglist);
9007 templ = most_general_template (templ);
9008 tree parms = DECL_TEMPLATE_PARMS (templ);
9009 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9010 /*req_all*/true,
9011 /*use_default*/true);
9013 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9015 if (complain & tf_error)
9017 error ("use of invalid variable template %qE", var);
9018 diagnose_constraints (location_of (var), templ, arglist);
9020 return error_mark_node;
9023 /* If a template-id refers to a specialization of a variable
9024 concept, then the expression is true if and only if the
9025 concept's constraints are satisfied by the given template
9026 arguments.
9028 NOTE: This is an extension of Concepts Lite TS that
9029 allows constraints to be used in expressions. */
9030 if (concept_p)
9032 tree decl = DECL_TEMPLATE_RESULT (templ);
9033 return evaluate_variable_concept (decl, arglist);
9036 return instantiate_template (templ, arglist, complain);
9039 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9040 TARGS template args, and instantiate it if it's not dependent. */
9042 tree
9043 lookup_and_finish_template_variable (tree templ, tree targs,
9044 tsubst_flags_t complain)
9046 templ = lookup_template_variable (templ, targs);
9047 if (!any_dependent_template_arguments_p (targs))
9049 templ = finish_template_variable (templ, complain);
9050 mark_used (templ);
9053 return convert_from_reference (templ);
9057 struct pair_fn_data
9059 tree_fn_t fn;
9060 tree_fn_t any_fn;
9061 void *data;
9062 /* True when we should also visit template parameters that occur in
9063 non-deduced contexts. */
9064 bool include_nondeduced_p;
9065 hash_set<tree> *visited;
9068 /* Called from for_each_template_parm via walk_tree. */
9070 static tree
9071 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9073 tree t = *tp;
9074 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9075 tree_fn_t fn = pfd->fn;
9076 void *data = pfd->data;
9077 tree result = NULL_TREE;
9079 #define WALK_SUBTREE(NODE) \
9080 do \
9082 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9083 pfd->include_nondeduced_p, \
9084 pfd->any_fn); \
9085 if (result) goto out; \
9087 while (0)
9089 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9090 return t;
9092 if (TYPE_P (t)
9093 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9094 WALK_SUBTREE (TYPE_CONTEXT (t));
9096 switch (TREE_CODE (t))
9098 case RECORD_TYPE:
9099 if (TYPE_PTRMEMFUNC_P (t))
9100 break;
9101 /* Fall through. */
9103 case UNION_TYPE:
9104 case ENUMERAL_TYPE:
9105 if (!TYPE_TEMPLATE_INFO (t))
9106 *walk_subtrees = 0;
9107 else
9108 WALK_SUBTREE (TYPE_TI_ARGS (t));
9109 break;
9111 case INTEGER_TYPE:
9112 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9113 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9114 break;
9116 case METHOD_TYPE:
9117 /* Since we're not going to walk subtrees, we have to do this
9118 explicitly here. */
9119 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9120 /* Fall through. */
9122 case FUNCTION_TYPE:
9123 /* Check the return type. */
9124 WALK_SUBTREE (TREE_TYPE (t));
9126 /* Check the parameter types. Since default arguments are not
9127 instantiated until they are needed, the TYPE_ARG_TYPES may
9128 contain expressions that involve template parameters. But,
9129 no-one should be looking at them yet. And, once they're
9130 instantiated, they don't contain template parameters, so
9131 there's no point in looking at them then, either. */
9133 tree parm;
9135 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9136 WALK_SUBTREE (TREE_VALUE (parm));
9138 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9139 want walk_tree walking into them itself. */
9140 *walk_subtrees = 0;
9143 if (flag_noexcept_type)
9145 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9146 if (spec)
9147 WALK_SUBTREE (TREE_PURPOSE (spec));
9149 break;
9151 case TYPEOF_TYPE:
9152 case UNDERLYING_TYPE:
9153 if (pfd->include_nondeduced_p
9154 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9155 pfd->visited,
9156 pfd->include_nondeduced_p,
9157 pfd->any_fn))
9158 return error_mark_node;
9159 break;
9161 case FUNCTION_DECL:
9162 case VAR_DECL:
9163 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9164 WALK_SUBTREE (DECL_TI_ARGS (t));
9165 /* Fall through. */
9167 case PARM_DECL:
9168 case CONST_DECL:
9169 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9170 WALK_SUBTREE (DECL_INITIAL (t));
9171 if (DECL_CONTEXT (t)
9172 && pfd->include_nondeduced_p)
9173 WALK_SUBTREE (DECL_CONTEXT (t));
9174 break;
9176 case BOUND_TEMPLATE_TEMPLATE_PARM:
9177 /* Record template parameters such as `T' inside `TT<T>'. */
9178 WALK_SUBTREE (TYPE_TI_ARGS (t));
9179 /* Fall through. */
9181 case TEMPLATE_TEMPLATE_PARM:
9182 case TEMPLATE_TYPE_PARM:
9183 case TEMPLATE_PARM_INDEX:
9184 if (fn && (*fn)(t, data))
9185 return t;
9186 else if (!fn)
9187 return t;
9188 break;
9190 case TEMPLATE_DECL:
9191 /* A template template parameter is encountered. */
9192 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9193 WALK_SUBTREE (TREE_TYPE (t));
9195 /* Already substituted template template parameter */
9196 *walk_subtrees = 0;
9197 break;
9199 case TYPENAME_TYPE:
9200 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9201 partial instantiation. */
9202 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9203 break;
9205 case CONSTRUCTOR:
9206 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9207 && pfd->include_nondeduced_p)
9208 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9209 break;
9211 case INDIRECT_REF:
9212 case COMPONENT_REF:
9213 /* If there's no type, then this thing must be some expression
9214 involving template parameters. */
9215 if (!fn && !TREE_TYPE (t))
9216 return error_mark_node;
9217 break;
9219 case MODOP_EXPR:
9220 case CAST_EXPR:
9221 case IMPLICIT_CONV_EXPR:
9222 case REINTERPRET_CAST_EXPR:
9223 case CONST_CAST_EXPR:
9224 case STATIC_CAST_EXPR:
9225 case DYNAMIC_CAST_EXPR:
9226 case ARROW_EXPR:
9227 case DOTSTAR_EXPR:
9228 case TYPEID_EXPR:
9229 case PSEUDO_DTOR_EXPR:
9230 if (!fn)
9231 return error_mark_node;
9232 break;
9234 default:
9235 break;
9238 #undef WALK_SUBTREE
9240 /* We didn't find any template parameters we liked. */
9241 out:
9242 return result;
9245 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9246 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9247 call FN with the parameter and the DATA.
9248 If FN returns nonzero, the iteration is terminated, and
9249 for_each_template_parm returns 1. Otherwise, the iteration
9250 continues. If FN never returns a nonzero value, the value
9251 returned by for_each_template_parm is 0. If FN is NULL, it is
9252 considered to be the function which always returns 1.
9254 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9255 parameters that occur in non-deduced contexts. When false, only
9256 visits those template parameters that can be deduced. */
9258 static tree
9259 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9260 hash_set<tree> *visited,
9261 bool include_nondeduced_p,
9262 tree_fn_t any_fn)
9264 struct pair_fn_data pfd;
9265 tree result;
9267 /* Set up. */
9268 pfd.fn = fn;
9269 pfd.any_fn = any_fn;
9270 pfd.data = data;
9271 pfd.include_nondeduced_p = include_nondeduced_p;
9273 /* Walk the tree. (Conceptually, we would like to walk without
9274 duplicates, but for_each_template_parm_r recursively calls
9275 for_each_template_parm, so we would need to reorganize a fair
9276 bit to use walk_tree_without_duplicates, so we keep our own
9277 visited list.) */
9278 if (visited)
9279 pfd.visited = visited;
9280 else
9281 pfd.visited = new hash_set<tree>;
9282 result = cp_walk_tree (&t,
9283 for_each_template_parm_r,
9284 &pfd,
9285 pfd.visited);
9287 /* Clean up. */
9288 if (!visited)
9290 delete pfd.visited;
9291 pfd.visited = 0;
9294 return result;
9297 /* Returns true if T depends on any template parameter. */
9300 uses_template_parms (tree t)
9302 if (t == NULL_TREE)
9303 return false;
9305 bool dependent_p;
9306 int saved_processing_template_decl;
9308 saved_processing_template_decl = processing_template_decl;
9309 if (!saved_processing_template_decl)
9310 processing_template_decl = 1;
9311 if (TYPE_P (t))
9312 dependent_p = dependent_type_p (t);
9313 else if (TREE_CODE (t) == TREE_VEC)
9314 dependent_p = any_dependent_template_arguments_p (t);
9315 else if (TREE_CODE (t) == TREE_LIST)
9316 dependent_p = (uses_template_parms (TREE_VALUE (t))
9317 || uses_template_parms (TREE_CHAIN (t)));
9318 else if (TREE_CODE (t) == TYPE_DECL)
9319 dependent_p = dependent_type_p (TREE_TYPE (t));
9320 else if (DECL_P (t)
9321 || EXPR_P (t)
9322 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9323 || TREE_CODE (t) == OVERLOAD
9324 || BASELINK_P (t)
9325 || identifier_p (t)
9326 || TREE_CODE (t) == TRAIT_EXPR
9327 || TREE_CODE (t) == CONSTRUCTOR
9328 || CONSTANT_CLASS_P (t))
9329 dependent_p = (type_dependent_expression_p (t)
9330 || value_dependent_expression_p (t));
9331 else
9333 gcc_assert (t == error_mark_node);
9334 dependent_p = false;
9337 processing_template_decl = saved_processing_template_decl;
9339 return dependent_p;
9342 /* Returns true iff current_function_decl is an incompletely instantiated
9343 template. Useful instead of processing_template_decl because the latter
9344 is set to 0 during instantiate_non_dependent_expr. */
9346 bool
9347 in_template_function (void)
9349 tree fn = current_function_decl;
9350 bool ret;
9351 ++processing_template_decl;
9352 ret = (fn && DECL_LANG_SPECIFIC (fn)
9353 && DECL_TEMPLATE_INFO (fn)
9354 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9355 --processing_template_decl;
9356 return ret;
9359 /* Returns true if T depends on any template parameter with level LEVEL. */
9361 bool
9362 uses_template_parms_level (tree t, int level)
9364 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9365 /*include_nondeduced_p=*/true);
9368 /* Returns true if the signature of DECL depends on any template parameter from
9369 its enclosing class. */
9371 bool
9372 uses_outer_template_parms (tree decl)
9374 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9375 if (depth == 0)
9376 return false;
9377 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9378 &depth, NULL, /*include_nondeduced_p=*/true))
9379 return true;
9380 if (PRIMARY_TEMPLATE_P (decl)
9381 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9382 (DECL_TEMPLATE_PARMS (decl)),
9383 template_parm_outer_level,
9384 &depth, NULL, /*include_nondeduced_p=*/true))
9385 return true;
9386 tree ci = get_constraints (decl);
9387 if (ci)
9388 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9389 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9390 &depth, NULL, /*nondeduced*/true))
9391 return true;
9392 return false;
9395 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9396 ill-formed translation unit, i.e. a variable or function that isn't
9397 usable in a constant expression. */
9399 static inline bool
9400 neglectable_inst_p (tree d)
9402 return (DECL_P (d)
9403 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9404 : decl_maybe_constant_var_p (d)));
9407 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9408 neglectable and instantiated from within an erroneous instantiation. */
9410 static bool
9411 limit_bad_template_recursion (tree decl)
9413 struct tinst_level *lev = current_tinst_level;
9414 int errs = errorcount + sorrycount;
9415 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9416 return false;
9418 for (; lev; lev = lev->next)
9419 if (neglectable_inst_p (lev->decl))
9420 break;
9422 return (lev && errs > lev->errors);
9425 static int tinst_depth;
9426 extern int max_tinst_depth;
9427 int depth_reached;
9429 static GTY(()) struct tinst_level *last_error_tinst_level;
9431 /* We're starting to instantiate D; record the template instantiation context
9432 for diagnostics and to restore it later. */
9434 bool
9435 push_tinst_level (tree d)
9437 return push_tinst_level_loc (d, input_location);
9440 /* We're starting to instantiate D; record the template instantiation context
9441 at LOC for diagnostics and to restore it later. */
9443 bool
9444 push_tinst_level_loc (tree d, location_t loc)
9446 struct tinst_level *new_level;
9448 if (tinst_depth >= max_tinst_depth)
9450 /* Tell error.c not to try to instantiate any templates. */
9451 at_eof = 2;
9452 fatal_error (input_location,
9453 "template instantiation depth exceeds maximum of %d"
9454 " (use -ftemplate-depth= to increase the maximum)",
9455 max_tinst_depth);
9456 return false;
9459 /* If the current instantiation caused problems, don't let it instantiate
9460 anything else. Do allow deduction substitution and decls usable in
9461 constant expressions. */
9462 if (limit_bad_template_recursion (d))
9463 return false;
9465 /* When not -quiet, dump template instantiations other than functions, since
9466 announce_function will take care of those. */
9467 if (!quiet_flag
9468 && TREE_CODE (d) != TREE_LIST
9469 && TREE_CODE (d) != FUNCTION_DECL)
9470 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9472 new_level = ggc_alloc<tinst_level> ();
9473 new_level->decl = d;
9474 new_level->locus = loc;
9475 new_level->errors = errorcount+sorrycount;
9476 new_level->in_system_header_p = in_system_header_at (input_location);
9477 new_level->next = current_tinst_level;
9478 current_tinst_level = new_level;
9480 ++tinst_depth;
9481 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9482 depth_reached = tinst_depth;
9484 return true;
9487 /* We're done instantiating this template; return to the instantiation
9488 context. */
9490 void
9491 pop_tinst_level (void)
9493 /* Restore the filename and line number stashed away when we started
9494 this instantiation. */
9495 input_location = current_tinst_level->locus;
9496 current_tinst_level = current_tinst_level->next;
9497 --tinst_depth;
9500 /* We're instantiating a deferred template; restore the template
9501 instantiation context in which the instantiation was requested, which
9502 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9504 static tree
9505 reopen_tinst_level (struct tinst_level *level)
9507 struct tinst_level *t;
9509 tinst_depth = 0;
9510 for (t = level; t; t = t->next)
9511 ++tinst_depth;
9513 current_tinst_level = level;
9514 pop_tinst_level ();
9515 if (current_tinst_level)
9516 current_tinst_level->errors = errorcount+sorrycount;
9517 return level->decl;
9520 /* Returns the TINST_LEVEL which gives the original instantiation
9521 context. */
9523 struct tinst_level *
9524 outermost_tinst_level (void)
9526 struct tinst_level *level = current_tinst_level;
9527 if (level)
9528 while (level->next)
9529 level = level->next;
9530 return level;
9533 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9534 vector of template arguments, as for tsubst.
9536 Returns an appropriate tsubst'd friend declaration. */
9538 static tree
9539 tsubst_friend_function (tree decl, tree args)
9541 tree new_friend;
9543 if (TREE_CODE (decl) == FUNCTION_DECL
9544 && DECL_TEMPLATE_INSTANTIATION (decl)
9545 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9546 /* This was a friend declared with an explicit template
9547 argument list, e.g.:
9549 friend void f<>(T);
9551 to indicate that f was a template instantiation, not a new
9552 function declaration. Now, we have to figure out what
9553 instantiation of what template. */
9555 tree template_id, arglist, fns;
9556 tree new_args;
9557 tree tmpl;
9558 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9560 /* Friend functions are looked up in the containing namespace scope.
9561 We must enter that scope, to avoid finding member functions of the
9562 current class with same name. */
9563 push_nested_namespace (ns);
9564 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9565 tf_warning_or_error, NULL_TREE,
9566 /*integral_constant_expression_p=*/false);
9567 pop_nested_namespace (ns);
9568 arglist = tsubst (DECL_TI_ARGS (decl), args,
9569 tf_warning_or_error, NULL_TREE);
9570 template_id = lookup_template_function (fns, arglist);
9572 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9573 tmpl = determine_specialization (template_id, new_friend,
9574 &new_args,
9575 /*need_member_template=*/0,
9576 TREE_VEC_LENGTH (args),
9577 tsk_none);
9578 return instantiate_template (tmpl, new_args, tf_error);
9581 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9583 /* The NEW_FRIEND will look like an instantiation, to the
9584 compiler, but is not an instantiation from the point of view of
9585 the language. For example, we might have had:
9587 template <class T> struct S {
9588 template <class U> friend void f(T, U);
9591 Then, in S<int>, template <class U> void f(int, U) is not an
9592 instantiation of anything. */
9593 if (new_friend == error_mark_node)
9594 return error_mark_node;
9596 DECL_USE_TEMPLATE (new_friend) = 0;
9597 if (TREE_CODE (decl) == TEMPLATE_DECL)
9599 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9600 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9601 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9604 /* The mangled name for the NEW_FRIEND is incorrect. The function
9605 is not a template instantiation and should not be mangled like
9606 one. Therefore, we forget the mangling here; we'll recompute it
9607 later if we need it. */
9608 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9610 SET_DECL_RTL (new_friend, NULL);
9611 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9614 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9616 tree old_decl;
9617 tree new_friend_template_info;
9618 tree new_friend_result_template_info;
9619 tree ns;
9620 int new_friend_is_defn;
9622 /* We must save some information from NEW_FRIEND before calling
9623 duplicate decls since that function will free NEW_FRIEND if
9624 possible. */
9625 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9626 new_friend_is_defn =
9627 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9628 (template_for_substitution (new_friend)))
9629 != NULL_TREE);
9630 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9632 /* This declaration is a `primary' template. */
9633 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9635 new_friend_result_template_info
9636 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9638 else
9639 new_friend_result_template_info = NULL_TREE;
9641 /* Inside pushdecl_namespace_level, we will push into the
9642 current namespace. However, the friend function should go
9643 into the namespace of the template. */
9644 ns = decl_namespace_context (new_friend);
9645 push_nested_namespace (ns);
9646 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9647 pop_nested_namespace (ns);
9649 if (old_decl == error_mark_node)
9650 return error_mark_node;
9652 if (old_decl != new_friend)
9654 /* This new friend declaration matched an existing
9655 declaration. For example, given:
9657 template <class T> void f(T);
9658 template <class U> class C {
9659 template <class T> friend void f(T) {}
9662 the friend declaration actually provides the definition
9663 of `f', once C has been instantiated for some type. So,
9664 old_decl will be the out-of-class template declaration,
9665 while new_friend is the in-class definition.
9667 But, if `f' was called before this point, the
9668 instantiation of `f' will have DECL_TI_ARGS corresponding
9669 to `T' but not to `U', references to which might appear
9670 in the definition of `f'. Previously, the most general
9671 template for an instantiation of `f' was the out-of-class
9672 version; now it is the in-class version. Therefore, we
9673 run through all specialization of `f', adding to their
9674 DECL_TI_ARGS appropriately. In particular, they need a
9675 new set of outer arguments, corresponding to the
9676 arguments for this class instantiation.
9678 The same situation can arise with something like this:
9680 friend void f(int);
9681 template <class T> class C {
9682 friend void f(T) {}
9685 when `C<int>' is instantiated. Now, `f(int)' is defined
9686 in the class. */
9688 if (!new_friend_is_defn)
9689 /* On the other hand, if the in-class declaration does
9690 *not* provide a definition, then we don't want to alter
9691 existing definitions. We can just leave everything
9692 alone. */
9694 else
9696 tree new_template = TI_TEMPLATE (new_friend_template_info);
9697 tree new_args = TI_ARGS (new_friend_template_info);
9699 /* Overwrite whatever template info was there before, if
9700 any, with the new template information pertaining to
9701 the declaration. */
9702 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9704 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9706 /* We should have called reregister_specialization in
9707 duplicate_decls. */
9708 gcc_assert (retrieve_specialization (new_template,
9709 new_args, 0)
9710 == old_decl);
9712 /* Instantiate it if the global has already been used. */
9713 if (DECL_ODR_USED (old_decl))
9714 instantiate_decl (old_decl, /*defer_ok=*/true,
9715 /*expl_inst_class_mem_p=*/false);
9717 else
9719 tree t;
9721 /* Indicate that the old function template is a partial
9722 instantiation. */
9723 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9724 = new_friend_result_template_info;
9726 gcc_assert (new_template
9727 == most_general_template (new_template));
9728 gcc_assert (new_template != old_decl);
9730 /* Reassign any specializations already in the hash table
9731 to the new more general template, and add the
9732 additional template args. */
9733 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9734 t != NULL_TREE;
9735 t = TREE_CHAIN (t))
9737 tree spec = TREE_VALUE (t);
9738 spec_entry elt;
9740 elt.tmpl = old_decl;
9741 elt.args = DECL_TI_ARGS (spec);
9742 elt.spec = NULL_TREE;
9744 decl_specializations->remove_elt (&elt);
9746 DECL_TI_ARGS (spec)
9747 = add_outermost_template_args (new_args,
9748 DECL_TI_ARGS (spec));
9750 register_specialization
9751 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9754 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9758 /* The information from NEW_FRIEND has been merged into OLD_DECL
9759 by duplicate_decls. */
9760 new_friend = old_decl;
9763 else
9765 tree context = DECL_CONTEXT (new_friend);
9766 bool dependent_p;
9768 /* In the code
9769 template <class T> class C {
9770 template <class U> friend void C1<U>::f (); // case 1
9771 friend void C2<T>::f (); // case 2
9773 we only need to make sure CONTEXT is a complete type for
9774 case 2. To distinguish between the two cases, we note that
9775 CONTEXT of case 1 remains dependent type after tsubst while
9776 this isn't true for case 2. */
9777 ++processing_template_decl;
9778 dependent_p = dependent_type_p (context);
9779 --processing_template_decl;
9781 if (!dependent_p
9782 && !complete_type_or_else (context, NULL_TREE))
9783 return error_mark_node;
9785 if (COMPLETE_TYPE_P (context))
9787 tree fn = new_friend;
9788 /* do_friend adds the TEMPLATE_DECL for any member friend
9789 template even if it isn't a member template, i.e.
9790 template <class T> friend A<T>::f();
9791 Look through it in that case. */
9792 if (TREE_CODE (fn) == TEMPLATE_DECL
9793 && !PRIMARY_TEMPLATE_P (fn))
9794 fn = DECL_TEMPLATE_RESULT (fn);
9795 /* Check to see that the declaration is really present, and,
9796 possibly obtain an improved declaration. */
9797 fn = check_classfn (context, fn, NULL_TREE);
9799 if (fn)
9800 new_friend = fn;
9804 return new_friend;
9807 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9808 template arguments, as for tsubst.
9810 Returns an appropriate tsubst'd friend type or error_mark_node on
9811 failure. */
9813 static tree
9814 tsubst_friend_class (tree friend_tmpl, tree args)
9816 tree friend_type;
9817 tree tmpl;
9818 tree context;
9820 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9822 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9823 return TREE_TYPE (t);
9826 context = CP_DECL_CONTEXT (friend_tmpl);
9828 if (context != global_namespace)
9830 if (TREE_CODE (context) == NAMESPACE_DECL)
9831 push_nested_namespace (context);
9832 else
9833 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9836 /* Look for a class template declaration. We look for hidden names
9837 because two friend declarations of the same template are the
9838 same. For example, in:
9840 struct A {
9841 template <typename> friend class F;
9843 template <typename> struct B {
9844 template <typename> friend class F;
9847 both F templates are the same. */
9848 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9849 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9851 /* But, if we don't find one, it might be because we're in a
9852 situation like this:
9854 template <class T>
9855 struct S {
9856 template <class U>
9857 friend struct S;
9860 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9861 for `S<int>', not the TEMPLATE_DECL. */
9862 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9864 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9865 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9868 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9870 /* The friend template has already been declared. Just
9871 check to see that the declarations match, and install any new
9872 default parameters. We must tsubst the default parameters,
9873 of course. We only need the innermost template parameters
9874 because that is all that redeclare_class_template will look
9875 at. */
9876 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9877 > TMPL_ARGS_DEPTH (args))
9879 tree parms;
9880 location_t saved_input_location;
9881 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9882 args, tf_warning_or_error);
9884 saved_input_location = input_location;
9885 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9886 tree cons = get_constraints (tmpl);
9887 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9888 input_location = saved_input_location;
9892 friend_type = TREE_TYPE (tmpl);
9894 else
9896 /* The friend template has not already been declared. In this
9897 case, the instantiation of the template class will cause the
9898 injection of this template into the global scope. */
9899 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
9900 if (tmpl == error_mark_node)
9901 return error_mark_node;
9903 /* The new TMPL is not an instantiation of anything, so we
9904 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
9905 the new type because that is supposed to be the corresponding
9906 template decl, i.e., TMPL. */
9907 DECL_USE_TEMPLATE (tmpl) = 0;
9908 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
9909 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
9910 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
9911 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
9913 /* Inject this template into the global scope. */
9914 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
9917 if (context != global_namespace)
9919 if (TREE_CODE (context) == NAMESPACE_DECL)
9920 pop_nested_namespace (context);
9921 else
9922 pop_nested_class ();
9925 return friend_type;
9928 /* Returns zero if TYPE cannot be completed later due to circularity.
9929 Otherwise returns one. */
9931 static int
9932 can_complete_type_without_circularity (tree type)
9934 if (type == NULL_TREE || type == error_mark_node)
9935 return 0;
9936 else if (COMPLETE_TYPE_P (type))
9937 return 1;
9938 else if (TREE_CODE (type) == ARRAY_TYPE)
9939 return can_complete_type_without_circularity (TREE_TYPE (type));
9940 else if (CLASS_TYPE_P (type)
9941 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
9942 return 0;
9943 else
9944 return 1;
9947 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
9948 tsubst_flags_t, tree);
9950 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
9951 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
9953 static tree
9954 tsubst_attribute (tree t, tree *decl_p, tree args,
9955 tsubst_flags_t complain, tree in_decl)
9957 gcc_assert (ATTR_IS_DEPENDENT (t));
9959 tree val = TREE_VALUE (t);
9960 if (val == NULL_TREE)
9961 /* Nothing to do. */;
9962 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
9963 && is_attribute_p ("omp declare simd",
9964 get_attribute_name (t)))
9966 tree clauses = TREE_VALUE (val);
9967 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
9968 complain, in_decl);
9969 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
9970 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
9971 tree parms = DECL_ARGUMENTS (*decl_p);
9972 clauses
9973 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
9974 if (clauses)
9975 val = build_tree_list (NULL_TREE, clauses);
9976 else
9977 val = NULL_TREE;
9979 /* If the first attribute argument is an identifier, don't
9980 pass it through tsubst. Attributes like mode, format,
9981 cleanup and several target specific attributes expect it
9982 unmodified. */
9983 else if (attribute_takes_identifier_p (get_attribute_name (t)))
9985 tree chain
9986 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
9987 /*integral_constant_expression_p=*/false);
9988 if (chain != TREE_CHAIN (val))
9989 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
9991 else if (PACK_EXPANSION_P (val))
9993 /* An attribute pack expansion. */
9994 tree purp = TREE_PURPOSE (t);
9995 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
9996 if (pack == error_mark_node)
9997 return error_mark_node;
9998 int len = TREE_VEC_LENGTH (pack);
9999 tree list = NULL_TREE;
10000 tree *q = &list;
10001 for (int i = 0; i < len; ++i)
10003 tree elt = TREE_VEC_ELT (pack, i);
10004 *q = build_tree_list (purp, elt);
10005 q = &TREE_CHAIN (*q);
10007 return list;
10009 else
10010 val = tsubst_expr (val, args, complain, in_decl,
10011 /*integral_constant_expression_p=*/false);
10013 if (val != TREE_VALUE (t))
10014 return build_tree_list (TREE_PURPOSE (t), val);
10015 return t;
10018 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10019 unchanged or a new TREE_LIST chain. */
10021 static tree
10022 tsubst_attributes (tree attributes, tree args,
10023 tsubst_flags_t complain, tree in_decl)
10025 tree last_dep = NULL_TREE;
10027 for (tree t = attributes; t; t = TREE_CHAIN (t))
10028 if (ATTR_IS_DEPENDENT (t))
10030 last_dep = t;
10031 attributes = copy_list (attributes);
10032 break;
10035 if (last_dep)
10036 for (tree *p = &attributes; *p; )
10038 tree t = *p;
10039 if (ATTR_IS_DEPENDENT (t))
10041 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10042 if (subst != t)
10044 *p = subst;
10046 p = &TREE_CHAIN (*p);
10047 while (*p);
10048 *p = TREE_CHAIN (t);
10049 continue;
10052 p = &TREE_CHAIN (*p);
10055 return attributes;
10058 /* Apply any attributes which had to be deferred until instantiation
10059 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10060 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10062 static void
10063 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10064 tree args, tsubst_flags_t complain, tree in_decl)
10066 tree last_dep = NULL_TREE;
10067 tree t;
10068 tree *p;
10070 if (attributes == NULL_TREE)
10071 return;
10073 if (DECL_P (*decl_p))
10075 if (TREE_TYPE (*decl_p) == error_mark_node)
10076 return;
10077 p = &DECL_ATTRIBUTES (*decl_p);
10078 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10079 to our attributes parameter. */
10080 gcc_assert (*p == attributes);
10082 else
10084 p = &TYPE_ATTRIBUTES (*decl_p);
10085 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10086 lookup_template_class_1, and should be preserved. */
10087 gcc_assert (*p != attributes);
10088 while (*p)
10089 p = &TREE_CHAIN (*p);
10092 for (t = attributes; t; t = TREE_CHAIN (t))
10093 if (ATTR_IS_DEPENDENT (t))
10095 last_dep = t;
10096 attributes = copy_list (attributes);
10097 break;
10100 *p = attributes;
10101 if (last_dep)
10103 tree late_attrs = NULL_TREE;
10104 tree *q = &late_attrs;
10106 for (; *p; )
10108 t = *p;
10109 if (ATTR_IS_DEPENDENT (t))
10111 *p = TREE_CHAIN (t);
10112 TREE_CHAIN (t) = NULL_TREE;
10113 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10115 q = &TREE_CHAIN (*q);
10116 while (*q);
10118 else
10119 p = &TREE_CHAIN (t);
10122 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10126 /* Perform (or defer) access check for typedefs that were referenced
10127 from within the template TMPL code.
10128 This is a subroutine of instantiate_decl and instantiate_class_template.
10129 TMPL is the template to consider and TARGS is the list of arguments of
10130 that template. */
10132 static void
10133 perform_typedefs_access_check (tree tmpl, tree targs)
10135 location_t saved_location;
10136 unsigned i;
10137 qualified_typedef_usage_t *iter;
10139 if (!tmpl
10140 || (!CLASS_TYPE_P (tmpl)
10141 && TREE_CODE (tmpl) != FUNCTION_DECL))
10142 return;
10144 saved_location = input_location;
10145 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10147 tree type_decl = iter->typedef_decl;
10148 tree type_scope = iter->context;
10150 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10151 continue;
10153 if (uses_template_parms (type_decl))
10154 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10155 if (uses_template_parms (type_scope))
10156 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10158 /* Make access check error messages point to the location
10159 of the use of the typedef. */
10160 input_location = iter->locus;
10161 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10162 type_decl, type_decl,
10163 tf_warning_or_error);
10165 input_location = saved_location;
10168 static tree
10169 instantiate_class_template_1 (tree type)
10171 tree templ, args, pattern, t, member;
10172 tree typedecl;
10173 tree pbinfo;
10174 tree base_list;
10175 unsigned int saved_maximum_field_alignment;
10176 tree fn_context;
10178 if (type == error_mark_node)
10179 return error_mark_node;
10181 if (COMPLETE_OR_OPEN_TYPE_P (type)
10182 || uses_template_parms (type))
10183 return type;
10185 /* Figure out which template is being instantiated. */
10186 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10187 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10189 /* Determine what specialization of the original template to
10190 instantiate. */
10191 t = most_specialized_partial_spec (type, tf_warning_or_error);
10192 if (t == error_mark_node)
10194 TYPE_BEING_DEFINED (type) = 1;
10195 return error_mark_node;
10197 else if (t)
10199 /* This TYPE is actually an instantiation of a partial
10200 specialization. We replace the innermost set of ARGS with
10201 the arguments appropriate for substitution. For example,
10202 given:
10204 template <class T> struct S {};
10205 template <class T> struct S<T*> {};
10207 and supposing that we are instantiating S<int*>, ARGS will
10208 presently be {int*} -- but we need {int}. */
10209 pattern = TREE_TYPE (t);
10210 args = TREE_PURPOSE (t);
10212 else
10214 pattern = TREE_TYPE (templ);
10215 args = CLASSTYPE_TI_ARGS (type);
10218 /* If the template we're instantiating is incomplete, then clearly
10219 there's nothing we can do. */
10220 if (!COMPLETE_TYPE_P (pattern))
10221 return type;
10223 /* If we've recursively instantiated too many templates, stop. */
10224 if (! push_tinst_level (type))
10225 return type;
10227 /* Now we're really doing the instantiation. Mark the type as in
10228 the process of being defined. */
10229 TYPE_BEING_DEFINED (type) = 1;
10231 /* We may be in the middle of deferred access check. Disable
10232 it now. */
10233 push_deferring_access_checks (dk_no_deferred);
10235 int saved_unevaluated_operand = cp_unevaluated_operand;
10236 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10238 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10239 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10240 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10241 fn_context = error_mark_node;
10242 if (!fn_context)
10243 push_to_top_level ();
10244 else
10246 cp_unevaluated_operand = 0;
10247 c_inhibit_evaluation_warnings = 0;
10249 /* Use #pragma pack from the template context. */
10250 saved_maximum_field_alignment = maximum_field_alignment;
10251 maximum_field_alignment = TYPE_PRECISION (pattern);
10253 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10255 /* Set the input location to the most specialized template definition.
10256 This is needed if tsubsting causes an error. */
10257 typedecl = TYPE_MAIN_DECL (pattern);
10258 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10259 DECL_SOURCE_LOCATION (typedecl);
10261 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10262 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10263 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10264 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10265 if (ANON_AGGR_TYPE_P (pattern))
10266 SET_ANON_AGGR_TYPE_P (type);
10267 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10269 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10270 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10271 /* Adjust visibility for template arguments. */
10272 determine_visibility (TYPE_MAIN_DECL (type));
10274 if (CLASS_TYPE_P (type))
10275 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10277 pbinfo = TYPE_BINFO (pattern);
10279 /* We should never instantiate a nested class before its enclosing
10280 class; we need to look up the nested class by name before we can
10281 instantiate it, and that lookup should instantiate the enclosing
10282 class. */
10283 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10284 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10286 base_list = NULL_TREE;
10287 if (BINFO_N_BASE_BINFOS (pbinfo))
10289 tree pbase_binfo;
10290 tree pushed_scope;
10291 int i;
10293 /* We must enter the scope containing the type, as that is where
10294 the accessibility of types named in dependent bases are
10295 looked up from. */
10296 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10298 /* Substitute into each of the bases to determine the actual
10299 basetypes. */
10300 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10302 tree base;
10303 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10304 tree expanded_bases = NULL_TREE;
10305 int idx, len = 1;
10307 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10309 expanded_bases =
10310 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10311 args, tf_error, NULL_TREE);
10312 if (expanded_bases == error_mark_node)
10313 continue;
10315 len = TREE_VEC_LENGTH (expanded_bases);
10318 for (idx = 0; idx < len; idx++)
10320 if (expanded_bases)
10321 /* Extract the already-expanded base class. */
10322 base = TREE_VEC_ELT (expanded_bases, idx);
10323 else
10324 /* Substitute to figure out the base class. */
10325 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10326 NULL_TREE);
10328 if (base == error_mark_node)
10329 continue;
10331 base_list = tree_cons (access, base, base_list);
10332 if (BINFO_VIRTUAL_P (pbase_binfo))
10333 TREE_TYPE (base_list) = integer_type_node;
10337 /* The list is now in reverse order; correct that. */
10338 base_list = nreverse (base_list);
10340 if (pushed_scope)
10341 pop_scope (pushed_scope);
10343 /* Now call xref_basetypes to set up all the base-class
10344 information. */
10345 xref_basetypes (type, base_list);
10347 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10348 (int) ATTR_FLAG_TYPE_IN_PLACE,
10349 args, tf_error, NULL_TREE);
10350 fixup_attribute_variants (type);
10352 /* Now that our base classes are set up, enter the scope of the
10353 class, so that name lookups into base classes, etc. will work
10354 correctly. This is precisely analogous to what we do in
10355 begin_class_definition when defining an ordinary non-template
10356 class, except we also need to push the enclosing classes. */
10357 push_nested_class (type);
10359 /* Now members are processed in the order of declaration. */
10360 for (member = CLASSTYPE_DECL_LIST (pattern);
10361 member; member = TREE_CHAIN (member))
10363 tree t = TREE_VALUE (member);
10365 if (TREE_PURPOSE (member))
10367 if (TYPE_P (t))
10369 /* Build new CLASSTYPE_NESTED_UTDS. */
10371 tree newtag;
10372 bool class_template_p;
10374 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10375 && TYPE_LANG_SPECIFIC (t)
10376 && CLASSTYPE_IS_TEMPLATE (t));
10377 /* If the member is a class template, then -- even after
10378 substitution -- there may be dependent types in the
10379 template argument list for the class. We increment
10380 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10381 that function will assume that no types are dependent
10382 when outside of a template. */
10383 if (class_template_p)
10384 ++processing_template_decl;
10385 newtag = tsubst (t, args, tf_error, NULL_TREE);
10386 if (class_template_p)
10387 --processing_template_decl;
10388 if (newtag == error_mark_node)
10389 continue;
10391 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10393 tree name = TYPE_IDENTIFIER (t);
10395 if (class_template_p)
10396 /* Unfortunately, lookup_template_class sets
10397 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10398 instantiation (i.e., for the type of a member
10399 template class nested within a template class.)
10400 This behavior is required for
10401 maybe_process_partial_specialization to work
10402 correctly, but is not accurate in this case;
10403 the TAG is not an instantiation of anything.
10404 (The corresponding TEMPLATE_DECL is an
10405 instantiation, but the TYPE is not.) */
10406 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10408 /* Now, we call pushtag to put this NEWTAG into the scope of
10409 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10410 pushtag calling push_template_decl. We don't have to do
10411 this for enums because it will already have been done in
10412 tsubst_enum. */
10413 if (name)
10414 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10415 pushtag (name, newtag, /*tag_scope=*/ts_current);
10418 else if (DECL_DECLARES_FUNCTION_P (t))
10420 /* Build new TYPE_METHODS. */
10421 tree r;
10423 if (TREE_CODE (t) == TEMPLATE_DECL)
10424 ++processing_template_decl;
10425 r = tsubst (t, args, tf_error, NULL_TREE);
10426 if (TREE_CODE (t) == TEMPLATE_DECL)
10427 --processing_template_decl;
10428 set_current_access_from_decl (r);
10429 finish_member_declaration (r);
10430 /* Instantiate members marked with attribute used. */
10431 if (r != error_mark_node && DECL_PRESERVE_P (r))
10432 mark_used (r);
10433 if (TREE_CODE (r) == FUNCTION_DECL
10434 && DECL_OMP_DECLARE_REDUCTION_P (r))
10435 cp_check_omp_declare_reduction (r);
10437 else if (DECL_CLASS_TEMPLATE_P (t)
10438 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10439 /* A closure type for a lambda in a default argument for a
10440 member template. Ignore it; it will be instantiated with
10441 the default argument. */;
10442 else
10444 /* Build new TYPE_FIELDS. */
10445 if (TREE_CODE (t) == STATIC_ASSERT)
10447 tree condition;
10449 ++c_inhibit_evaluation_warnings;
10450 condition =
10451 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10452 tf_warning_or_error, NULL_TREE,
10453 /*integral_constant_expression_p=*/true);
10454 --c_inhibit_evaluation_warnings;
10456 finish_static_assert (condition,
10457 STATIC_ASSERT_MESSAGE (t),
10458 STATIC_ASSERT_SOURCE_LOCATION (t),
10459 /*member_p=*/true);
10461 else if (TREE_CODE (t) != CONST_DECL)
10463 tree r;
10464 tree vec = NULL_TREE;
10465 int len = 1;
10467 /* The file and line for this declaration, to
10468 assist in error message reporting. Since we
10469 called push_tinst_level above, we don't need to
10470 restore these. */
10471 input_location = DECL_SOURCE_LOCATION (t);
10473 if (TREE_CODE (t) == TEMPLATE_DECL)
10474 ++processing_template_decl;
10475 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10476 if (TREE_CODE (t) == TEMPLATE_DECL)
10477 --processing_template_decl;
10479 if (TREE_CODE (r) == TREE_VEC)
10481 /* A capture pack became multiple fields. */
10482 vec = r;
10483 len = TREE_VEC_LENGTH (vec);
10486 for (int i = 0; i < len; ++i)
10488 if (vec)
10489 r = TREE_VEC_ELT (vec, i);
10490 if (VAR_P (r))
10492 /* In [temp.inst]:
10494 [t]he initialization (and any associated
10495 side-effects) of a static data member does
10496 not occur unless the static data member is
10497 itself used in a way that requires the
10498 definition of the static data member to
10499 exist.
10501 Therefore, we do not substitute into the
10502 initialized for the static data member here. */
10503 finish_static_data_member_decl
10505 /*init=*/NULL_TREE,
10506 /*init_const_expr_p=*/false,
10507 /*asmspec_tree=*/NULL_TREE,
10508 /*flags=*/0);
10509 /* Instantiate members marked with attribute used. */
10510 if (r != error_mark_node && DECL_PRESERVE_P (r))
10511 mark_used (r);
10513 else if (TREE_CODE (r) == FIELD_DECL)
10515 /* Determine whether R has a valid type and can be
10516 completed later. If R is invalid, then its type
10517 is replaced by error_mark_node. */
10518 tree rtype = TREE_TYPE (r);
10519 if (can_complete_type_without_circularity (rtype))
10520 complete_type (rtype);
10522 if (!complete_or_array_type_p (rtype))
10524 /* If R's type couldn't be completed and
10525 it isn't a flexible array member (whose
10526 type is incomplete by definition) give
10527 an error. */
10528 cxx_incomplete_type_error (r, rtype);
10529 TREE_TYPE (r) = error_mark_node;
10533 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10534 such a thing will already have been added to the field
10535 list by tsubst_enum in finish_member_declaration in the
10536 CLASSTYPE_NESTED_UTDS case above. */
10537 if (!(TREE_CODE (r) == TYPE_DECL
10538 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10539 && DECL_ARTIFICIAL (r)))
10541 set_current_access_from_decl (r);
10542 finish_member_declaration (r);
10548 else
10550 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10551 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10553 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10555 tree friend_type = t;
10556 bool adjust_processing_template_decl = false;
10558 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10560 /* template <class T> friend class C; */
10561 friend_type = tsubst_friend_class (friend_type, args);
10562 adjust_processing_template_decl = true;
10564 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10566 /* template <class T> friend class C::D; */
10567 friend_type = tsubst (friend_type, args,
10568 tf_warning_or_error, NULL_TREE);
10569 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10570 friend_type = TREE_TYPE (friend_type);
10571 adjust_processing_template_decl = true;
10573 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10574 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10576 /* This could be either
10578 friend class T::C;
10580 when dependent_type_p is false or
10582 template <class U> friend class T::C;
10584 otherwise. */
10585 /* Bump processing_template_decl in case this is something like
10586 template <class T> friend struct A<T>::B. */
10587 ++processing_template_decl;
10588 friend_type = tsubst (friend_type, args,
10589 tf_warning_or_error, NULL_TREE);
10590 if (dependent_type_p (friend_type))
10591 adjust_processing_template_decl = true;
10592 --processing_template_decl;
10594 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10595 && hidden_name_p (TYPE_NAME (friend_type)))
10597 /* friend class C;
10599 where C hasn't been declared yet. Let's lookup name
10600 from namespace scope directly, bypassing any name that
10601 come from dependent base class. */
10602 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10604 /* The call to xref_tag_from_type does injection for friend
10605 classes. */
10606 push_nested_namespace (ns);
10607 friend_type =
10608 xref_tag_from_type (friend_type, NULL_TREE,
10609 /*tag_scope=*/ts_current);
10610 pop_nested_namespace (ns);
10612 else if (uses_template_parms (friend_type))
10613 /* friend class C<T>; */
10614 friend_type = tsubst (friend_type, args,
10615 tf_warning_or_error, NULL_TREE);
10616 /* Otherwise it's
10618 friend class C;
10620 where C is already declared or
10622 friend class C<int>;
10624 We don't have to do anything in these cases. */
10626 if (adjust_processing_template_decl)
10627 /* Trick make_friend_class into realizing that the friend
10628 we're adding is a template, not an ordinary class. It's
10629 important that we use make_friend_class since it will
10630 perform some error-checking and output cross-reference
10631 information. */
10632 ++processing_template_decl;
10634 if (friend_type != error_mark_node)
10635 make_friend_class (type, friend_type, /*complain=*/false);
10637 if (adjust_processing_template_decl)
10638 --processing_template_decl;
10640 else
10642 /* Build new DECL_FRIENDLIST. */
10643 tree r;
10645 /* The file and line for this declaration, to
10646 assist in error message reporting. Since we
10647 called push_tinst_level above, we don't need to
10648 restore these. */
10649 input_location = DECL_SOURCE_LOCATION (t);
10651 if (TREE_CODE (t) == TEMPLATE_DECL)
10653 ++processing_template_decl;
10654 push_deferring_access_checks (dk_no_check);
10657 r = tsubst_friend_function (t, args);
10658 add_friend (type, r, /*complain=*/false);
10659 if (TREE_CODE (t) == TEMPLATE_DECL)
10661 pop_deferring_access_checks ();
10662 --processing_template_decl;
10668 if (fn_context)
10670 /* Restore these before substituting into the lambda capture
10671 initializers. */
10672 cp_unevaluated_operand = saved_unevaluated_operand;
10673 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10676 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10678 tree decl = lambda_function (type);
10679 if (decl)
10681 if (cxx_dialect >= cxx1z)
10682 CLASSTYPE_LITERAL_P (type) = true;
10684 if (!DECL_TEMPLATE_INFO (decl)
10685 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10687 /* Set function_depth to avoid garbage collection. */
10688 ++function_depth;
10689 instantiate_decl (decl, /*defer_ok=*/false, false);
10690 --function_depth;
10693 /* We need to instantiate the capture list from the template
10694 after we've instantiated the closure members, but before we
10695 consider adding the conversion op. Also keep any captures
10696 that may have been added during instantiation of the op(). */
10697 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10698 tree tmpl_cap
10699 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10700 args, tf_warning_or_error, NULL_TREE,
10701 false, false);
10703 LAMBDA_EXPR_CAPTURE_LIST (expr)
10704 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10706 maybe_add_lambda_conv_op (type);
10708 else
10709 gcc_assert (errorcount);
10712 /* Set the file and line number information to whatever is given for
10713 the class itself. This puts error messages involving generated
10714 implicit functions at a predictable point, and the same point
10715 that would be used for non-template classes. */
10716 input_location = DECL_SOURCE_LOCATION (typedecl);
10718 unreverse_member_declarations (type);
10719 finish_struct_1 (type);
10720 TYPE_BEING_DEFINED (type) = 0;
10722 /* We don't instantiate default arguments for member functions. 14.7.1:
10724 The implicit instantiation of a class template specialization causes
10725 the implicit instantiation of the declarations, but not of the
10726 definitions or default arguments, of the class member functions,
10727 member classes, static data members and member templates.... */
10729 /* Some typedefs referenced from within the template code need to be access
10730 checked at template instantiation time, i.e now. These types were
10731 added to the template at parsing time. Let's get those and perform
10732 the access checks then. */
10733 perform_typedefs_access_check (pattern, args);
10734 perform_deferred_access_checks (tf_warning_or_error);
10735 pop_nested_class ();
10736 maximum_field_alignment = saved_maximum_field_alignment;
10737 if (!fn_context)
10738 pop_from_top_level ();
10739 pop_deferring_access_checks ();
10740 pop_tinst_level ();
10742 /* The vtable for a template class can be emitted in any translation
10743 unit in which the class is instantiated. When there is no key
10744 method, however, finish_struct_1 will already have added TYPE to
10745 the keyed_classes list. */
10746 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10747 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10749 return type;
10752 /* Wrapper for instantiate_class_template_1. */
10754 tree
10755 instantiate_class_template (tree type)
10757 tree ret;
10758 timevar_push (TV_TEMPLATE_INST);
10759 ret = instantiate_class_template_1 (type);
10760 timevar_pop (TV_TEMPLATE_INST);
10761 return ret;
10764 static tree
10765 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10767 tree r;
10769 if (!t)
10770 r = t;
10771 else if (TYPE_P (t))
10772 r = tsubst (t, args, complain, in_decl);
10773 else
10775 if (!(complain & tf_warning))
10776 ++c_inhibit_evaluation_warnings;
10777 r = tsubst_expr (t, args, complain, in_decl,
10778 /*integral_constant_expression_p=*/true);
10779 if (!(complain & tf_warning))
10780 --c_inhibit_evaluation_warnings;
10782 return r;
10785 /* Given a function parameter pack TMPL_PARM and some function parameters
10786 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10787 and set *SPEC_P to point at the next point in the list. */
10789 tree
10790 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10792 /* Collect all of the extra "packed" parameters into an
10793 argument pack. */
10794 tree parmvec;
10795 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10796 tree spec_parm = *spec_p;
10797 int i, len;
10799 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10800 if (tmpl_parm
10801 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10802 break;
10804 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10805 parmvec = make_tree_vec (len);
10806 spec_parm = *spec_p;
10807 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10808 TREE_VEC_ELT (parmvec, i) = spec_parm;
10810 /* Build the argument packs. */
10811 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10812 *spec_p = spec_parm;
10814 return argpack;
10817 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10818 NONTYPE_ARGUMENT_PACK. */
10820 static tree
10821 make_fnparm_pack (tree spec_parm)
10823 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10826 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10827 pack expansion with no extra args, 2 if it has extra args, or 0
10828 if it is not a pack expansion. */
10830 static int
10831 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10833 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10834 if (i >= TREE_VEC_LENGTH (vec))
10835 return 0;
10836 tree elt = TREE_VEC_ELT (vec, i);
10837 if (DECL_P (elt))
10838 /* A decl pack is itself an expansion. */
10839 elt = TREE_TYPE (elt);
10840 if (!PACK_EXPANSION_P (elt))
10841 return 0;
10842 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10843 return 2;
10844 return 1;
10848 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10850 static tree
10851 make_argument_pack_select (tree arg_pack, unsigned index)
10853 tree aps = make_node (ARGUMENT_PACK_SELECT);
10855 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10856 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10858 return aps;
10861 /* This is a subroutine of tsubst_pack_expansion.
10863 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10864 mechanism to store the (non complete list of) arguments of the
10865 substitution and return a non substituted pack expansion, in order
10866 to wait for when we have enough arguments to really perform the
10867 substitution. */
10869 static bool
10870 use_pack_expansion_extra_args_p (tree parm_packs,
10871 int arg_pack_len,
10872 bool has_empty_arg)
10874 /* If one pack has an expansion and another pack has a normal
10875 argument or if one pack has an empty argument and an another
10876 one hasn't then tsubst_pack_expansion cannot perform the
10877 substitution and need to fall back on the
10878 PACK_EXPANSION_EXTRA mechanism. */
10879 if (parm_packs == NULL_TREE)
10880 return false;
10881 else if (has_empty_arg)
10882 return true;
10884 bool has_expansion_arg = false;
10885 for (int i = 0 ; i < arg_pack_len; ++i)
10887 bool has_non_expansion_arg = false;
10888 for (tree parm_pack = parm_packs;
10889 parm_pack;
10890 parm_pack = TREE_CHAIN (parm_pack))
10892 tree arg = TREE_VALUE (parm_pack);
10894 int exp = argument_pack_element_is_expansion_p (arg, i);
10895 if (exp == 2)
10896 /* We can't substitute a pack expansion with extra args into
10897 our pattern. */
10898 return true;
10899 else if (exp)
10900 has_expansion_arg = true;
10901 else
10902 has_non_expansion_arg = true;
10905 if (has_expansion_arg && has_non_expansion_arg)
10906 return true;
10908 return false;
10911 /* [temp.variadic]/6 says that:
10913 The instantiation of a pack expansion [...]
10914 produces a list E1,E2, ..., En, where N is the number of elements
10915 in the pack expansion parameters.
10917 This subroutine of tsubst_pack_expansion produces one of these Ei.
10919 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
10920 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
10921 PATTERN, and each TREE_VALUE is its corresponding argument pack.
10922 INDEX is the index 'i' of the element Ei to produce. ARGS,
10923 COMPLAIN, and IN_DECL are the same parameters as for the
10924 tsubst_pack_expansion function.
10926 The function returns the resulting Ei upon successful completion,
10927 or error_mark_node.
10929 Note that this function possibly modifies the ARGS parameter, so
10930 it's the responsibility of the caller to restore it. */
10932 static tree
10933 gen_elem_of_pack_expansion_instantiation (tree pattern,
10934 tree parm_packs,
10935 unsigned index,
10936 tree args /* This parm gets
10937 modified. */,
10938 tsubst_flags_t complain,
10939 tree in_decl)
10941 tree t;
10942 bool ith_elem_is_expansion = false;
10944 /* For each parameter pack, change the substitution of the parameter
10945 pack to the ith argument in its argument pack, then expand the
10946 pattern. */
10947 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
10949 tree parm = TREE_PURPOSE (pack);
10950 tree arg_pack = TREE_VALUE (pack);
10951 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
10953 ith_elem_is_expansion |=
10954 argument_pack_element_is_expansion_p (arg_pack, index);
10956 /* Select the Ith argument from the pack. */
10957 if (TREE_CODE (parm) == PARM_DECL
10958 || TREE_CODE (parm) == FIELD_DECL)
10960 if (index == 0)
10962 aps = make_argument_pack_select (arg_pack, index);
10963 if (!mark_used (parm, complain) && !(complain & tf_error))
10964 return error_mark_node;
10965 register_local_specialization (aps, parm);
10967 else
10968 aps = retrieve_local_specialization (parm);
10970 else
10972 int idx, level;
10973 template_parm_level_and_index (parm, &level, &idx);
10975 if (index == 0)
10977 aps = make_argument_pack_select (arg_pack, index);
10978 /* Update the corresponding argument. */
10979 TMPL_ARG (args, level, idx) = aps;
10981 else
10982 /* Re-use the ARGUMENT_PACK_SELECT. */
10983 aps = TMPL_ARG (args, level, idx);
10985 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10988 /* Substitute into the PATTERN with the (possibly altered)
10989 arguments. */
10990 if (pattern == in_decl)
10991 /* Expanding a fixed parameter pack from
10992 coerce_template_parameter_pack. */
10993 t = tsubst_decl (pattern, args, complain);
10994 else if (pattern == error_mark_node)
10995 t = error_mark_node;
10996 else if (constraint_p (pattern))
10998 if (processing_template_decl)
10999 t = tsubst_constraint (pattern, args, complain, in_decl);
11000 else
11001 t = (constraints_satisfied_p (pattern, args)
11002 ? boolean_true_node : boolean_false_node);
11004 else if (!TYPE_P (pattern))
11005 t = tsubst_expr (pattern, args, complain, in_decl,
11006 /*integral_constant_expression_p=*/false);
11007 else
11008 t = tsubst (pattern, args, complain, in_decl);
11010 /* If the Ith argument pack element is a pack expansion, then
11011 the Ith element resulting from the substituting is going to
11012 be a pack expansion as well. */
11013 if (ith_elem_is_expansion)
11014 t = make_pack_expansion (t);
11016 return t;
11019 /* When the unexpanded parameter pack in a fold expression expands to an empty
11020 sequence, the value of the expression is as follows; the program is
11021 ill-formed if the operator is not listed in this table.
11023 && true
11024 || false
11025 , void() */
11027 tree
11028 expand_empty_fold (tree t, tsubst_flags_t complain)
11030 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11031 if (!FOLD_EXPR_MODIFY_P (t))
11032 switch (code)
11034 case TRUTH_ANDIF_EXPR:
11035 return boolean_true_node;
11036 case TRUTH_ORIF_EXPR:
11037 return boolean_false_node;
11038 case COMPOUND_EXPR:
11039 return void_node;
11040 default:
11041 break;
11044 if (complain & tf_error)
11045 error_at (location_of (t),
11046 "fold of empty expansion over %O", code);
11047 return error_mark_node;
11050 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11051 form an expression that combines the two terms using the
11052 operator of T. */
11054 static tree
11055 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11057 tree op = FOLD_EXPR_OP (t);
11058 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11060 // Handle compound assignment operators.
11061 if (FOLD_EXPR_MODIFY_P (t))
11062 return build_x_modify_expr (input_location, left, code, right, complain);
11064 switch (code)
11066 case COMPOUND_EXPR:
11067 return build_x_compound_expr (input_location, left, right, complain);
11068 case DOTSTAR_EXPR:
11069 return build_m_component_ref (left, right, complain);
11070 default:
11071 return build_x_binary_op (input_location, code,
11072 left, TREE_CODE (left),
11073 right, TREE_CODE (right),
11074 /*overload=*/NULL,
11075 complain);
11079 /* Substitute ARGS into the pack of a fold expression T. */
11081 static inline tree
11082 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11084 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11087 /* Substitute ARGS into the pack of a fold expression T. */
11089 static inline tree
11090 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11092 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11095 /* Expand a PACK of arguments into a grouped as left fold.
11096 Given a pack containing elements A0, A1, ..., An and an
11097 operator @, this builds the expression:
11099 ((A0 @ A1) @ A2) ... @ An
11101 Note that PACK must not be empty.
11103 The operator is defined by the original fold expression T. */
11105 static tree
11106 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11108 tree left = TREE_VEC_ELT (pack, 0);
11109 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11111 tree right = TREE_VEC_ELT (pack, i);
11112 left = fold_expression (t, left, right, complain);
11114 return left;
11117 /* Substitute into a unary left fold expression. */
11119 static tree
11120 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11121 tree in_decl)
11123 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11124 if (pack == error_mark_node)
11125 return error_mark_node;
11126 if (PACK_EXPANSION_P (pack))
11128 tree r = copy_node (t);
11129 FOLD_EXPR_PACK (r) = pack;
11130 return r;
11132 if (TREE_VEC_LENGTH (pack) == 0)
11133 return expand_empty_fold (t, complain);
11134 else
11135 return expand_left_fold (t, pack, complain);
11138 /* Substitute into a binary left fold expression.
11140 Do ths by building a single (non-empty) vector of argumnts and
11141 building the expression from those elements. */
11143 static tree
11144 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11145 tree in_decl)
11147 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11148 if (pack == error_mark_node)
11149 return error_mark_node;
11150 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11151 if (init == error_mark_node)
11152 return error_mark_node;
11154 if (PACK_EXPANSION_P (pack))
11156 tree r = copy_node (t);
11157 FOLD_EXPR_PACK (r) = pack;
11158 FOLD_EXPR_INIT (r) = init;
11159 return r;
11162 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11163 TREE_VEC_ELT (vec, 0) = init;
11164 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11165 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11167 return expand_left_fold (t, vec, complain);
11170 /* Expand a PACK of arguments into a grouped as right fold.
11171 Given a pack containing elementns A0, A1, ..., and an
11172 operator @, this builds the expression:
11174 A0@ ... (An-2 @ (An-1 @ An))
11176 Note that PACK must not be empty.
11178 The operator is defined by the original fold expression T. */
11180 tree
11181 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11183 // Build the expression.
11184 int n = TREE_VEC_LENGTH (pack);
11185 tree right = TREE_VEC_ELT (pack, n - 1);
11186 for (--n; n != 0; --n)
11188 tree left = TREE_VEC_ELT (pack, n - 1);
11189 right = fold_expression (t, left, right, complain);
11191 return right;
11194 /* Substitute into a unary right fold expression. */
11196 static tree
11197 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11198 tree in_decl)
11200 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11201 if (pack == error_mark_node)
11202 return error_mark_node;
11203 if (PACK_EXPANSION_P (pack))
11205 tree r = copy_node (t);
11206 FOLD_EXPR_PACK (r) = pack;
11207 return r;
11209 if (TREE_VEC_LENGTH (pack) == 0)
11210 return expand_empty_fold (t, complain);
11211 else
11212 return expand_right_fold (t, pack, complain);
11215 /* Substitute into a binary right fold expression.
11217 Do ths by building a single (non-empty) vector of arguments and
11218 building the expression from those elements. */
11220 static tree
11221 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11222 tree in_decl)
11224 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11225 if (pack == error_mark_node)
11226 return error_mark_node;
11227 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11228 if (init == error_mark_node)
11229 return error_mark_node;
11231 if (PACK_EXPANSION_P (pack))
11233 tree r = copy_node (t);
11234 FOLD_EXPR_PACK (r) = pack;
11235 FOLD_EXPR_INIT (r) = init;
11236 return r;
11239 int n = TREE_VEC_LENGTH (pack);
11240 tree vec = make_tree_vec (n + 1);
11241 for (int i = 0; i < n; ++i)
11242 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11243 TREE_VEC_ELT (vec, n) = init;
11245 return expand_right_fold (t, vec, complain);
11249 /* Substitute ARGS into T, which is an pack expansion
11250 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11251 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11252 (if only a partial substitution could be performed) or
11253 ERROR_MARK_NODE if there was an error. */
11254 tree
11255 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11256 tree in_decl)
11258 tree pattern;
11259 tree pack, packs = NULL_TREE;
11260 bool unsubstituted_packs = false;
11261 int i, len = -1;
11262 tree result;
11263 hash_map<tree, tree> *saved_local_specializations = NULL;
11264 bool need_local_specializations = false;
11265 int levels;
11267 gcc_assert (PACK_EXPANSION_P (t));
11268 pattern = PACK_EXPANSION_PATTERN (t);
11270 /* Add in any args remembered from an earlier partial instantiation. */
11271 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11273 levels = TMPL_ARGS_DEPTH (args);
11275 /* Determine the argument packs that will instantiate the parameter
11276 packs used in the expansion expression. While we're at it,
11277 compute the number of arguments to be expanded and make sure it
11278 is consistent. */
11279 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11280 pack = TREE_CHAIN (pack))
11282 tree parm_pack = TREE_VALUE (pack);
11283 tree arg_pack = NULL_TREE;
11284 tree orig_arg = NULL_TREE;
11285 int level = 0;
11287 if (TREE_CODE (parm_pack) == BASES)
11289 if (BASES_DIRECT (parm_pack))
11290 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11291 args, complain, in_decl, false));
11292 else
11293 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11294 args, complain, in_decl, false));
11296 if (TREE_CODE (parm_pack) == PARM_DECL)
11298 /* We know we have correct local_specializations if this
11299 expansion is at function scope, or if we're dealing with a
11300 local parameter in a requires expression; for the latter,
11301 tsubst_requires_expr set it up appropriately. */
11302 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11303 arg_pack = retrieve_local_specialization (parm_pack);
11304 else
11305 /* We can't rely on local_specializations for a parameter
11306 name used later in a function declaration (such as in a
11307 late-specified return type). Even if it exists, it might
11308 have the wrong value for a recursive call. */
11309 need_local_specializations = true;
11311 if (!arg_pack)
11313 /* This parameter pack was used in an unevaluated context. Just
11314 make a dummy decl, since it's only used for its type. */
11315 arg_pack = tsubst_decl (parm_pack, args, complain);
11316 if (arg_pack && DECL_PACK_P (arg_pack))
11317 /* Partial instantiation of the parm_pack, we can't build
11318 up an argument pack yet. */
11319 arg_pack = NULL_TREE;
11320 else
11321 arg_pack = make_fnparm_pack (arg_pack);
11324 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11325 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11326 else
11328 int idx;
11329 template_parm_level_and_index (parm_pack, &level, &idx);
11331 if (level <= levels)
11332 arg_pack = TMPL_ARG (args, level, idx);
11335 orig_arg = arg_pack;
11336 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11337 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11339 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11340 /* This can only happen if we forget to expand an argument
11341 pack somewhere else. Just return an error, silently. */
11343 result = make_tree_vec (1);
11344 TREE_VEC_ELT (result, 0) = error_mark_node;
11345 return result;
11348 if (arg_pack)
11350 int my_len =
11351 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11353 /* Don't bother trying to do a partial substitution with
11354 incomplete packs; we'll try again after deduction. */
11355 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11356 return t;
11358 if (len < 0)
11359 len = my_len;
11360 else if (len != my_len)
11362 if (!(complain & tf_error))
11363 /* Fail quietly. */;
11364 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11365 error ("mismatched argument pack lengths while expanding %qT",
11366 pattern);
11367 else
11368 error ("mismatched argument pack lengths while expanding %qE",
11369 pattern);
11370 return error_mark_node;
11373 /* Keep track of the parameter packs and their corresponding
11374 argument packs. */
11375 packs = tree_cons (parm_pack, arg_pack, packs);
11376 TREE_TYPE (packs) = orig_arg;
11378 else
11380 /* We can't substitute for this parameter pack. We use a flag as
11381 well as the missing_level counter because function parameter
11382 packs don't have a level. */
11383 gcc_assert (processing_template_decl);
11384 unsubstituted_packs = true;
11388 /* If the expansion is just T..., return the matching argument pack, unless
11389 we need to call convert_from_reference on all the elements. This is an
11390 important optimization; see c++/68422. */
11391 if (!unsubstituted_packs
11392 && TREE_PURPOSE (packs) == pattern)
11394 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11395 /* Types need no adjustment, nor does sizeof..., and if we still have
11396 some pack expansion args we won't do anything yet. */
11397 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11398 || PACK_EXPANSION_SIZEOF_P (t)
11399 || pack_expansion_args_count (args))
11400 return args;
11401 /* Also optimize expression pack expansions if we can tell that the
11402 elements won't have reference type. */
11403 tree type = TREE_TYPE (pattern);
11404 if (type && TREE_CODE (type) != REFERENCE_TYPE
11405 && !PACK_EXPANSION_P (type)
11406 && !WILDCARD_TYPE_P (type))
11407 return args;
11408 /* Otherwise use the normal path so we get convert_from_reference. */
11411 /* We cannot expand this expansion expression, because we don't have
11412 all of the argument packs we need. */
11413 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11415 /* We got some full packs, but we can't substitute them in until we
11416 have values for all the packs. So remember these until then. */
11418 t = make_pack_expansion (pattern);
11419 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11420 return t;
11422 else if (unsubstituted_packs)
11424 /* There were no real arguments, we're just replacing a parameter
11425 pack with another version of itself. Substitute into the
11426 pattern and return a PACK_EXPANSION_*. The caller will need to
11427 deal with that. */
11428 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11429 t = tsubst_expr (pattern, args, complain, in_decl,
11430 /*integral_constant_expression_p=*/false);
11431 else
11432 t = tsubst (pattern, args, complain, in_decl);
11433 t = make_pack_expansion (t);
11434 return t;
11437 gcc_assert (len >= 0);
11439 if (need_local_specializations)
11441 /* We're in a late-specified return type, so create our own local
11442 specializations map; the current map is either NULL or (in the
11443 case of recursive unification) might have bindings that we don't
11444 want to use or alter. */
11445 saved_local_specializations = local_specializations;
11446 local_specializations = new hash_map<tree, tree>;
11449 /* For each argument in each argument pack, substitute into the
11450 pattern. */
11451 result = make_tree_vec (len);
11452 tree elem_args = copy_template_args (args);
11453 for (i = 0; i < len; ++i)
11455 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11457 elem_args, complain,
11458 in_decl);
11459 TREE_VEC_ELT (result, i) = t;
11460 if (t == error_mark_node)
11462 result = error_mark_node;
11463 break;
11467 /* Update ARGS to restore the substitution from parameter packs to
11468 their argument packs. */
11469 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11471 tree parm = TREE_PURPOSE (pack);
11473 if (TREE_CODE (parm) == PARM_DECL
11474 || TREE_CODE (parm) == FIELD_DECL)
11475 register_local_specialization (TREE_TYPE (pack), parm);
11476 else
11478 int idx, level;
11480 if (TREE_VALUE (pack) == NULL_TREE)
11481 continue;
11483 template_parm_level_and_index (parm, &level, &idx);
11485 /* Update the corresponding argument. */
11486 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11487 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11488 TREE_TYPE (pack);
11489 else
11490 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11494 if (need_local_specializations)
11496 delete local_specializations;
11497 local_specializations = saved_local_specializations;
11500 /* If the dependent pack arguments were such that we end up with only a
11501 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11502 if (len == 1 && TREE_CODE (result) == TREE_VEC
11503 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11504 return TREE_VEC_ELT (result, 0);
11506 return result;
11509 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11510 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11511 parameter packs; all parms generated from a function parameter pack will
11512 have the same DECL_PARM_INDEX. */
11514 tree
11515 get_pattern_parm (tree parm, tree tmpl)
11517 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11518 tree patparm;
11520 if (DECL_ARTIFICIAL (parm))
11522 for (patparm = DECL_ARGUMENTS (pattern);
11523 patparm; patparm = DECL_CHAIN (patparm))
11524 if (DECL_ARTIFICIAL (patparm)
11525 && DECL_NAME (parm) == DECL_NAME (patparm))
11526 break;
11528 else
11530 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11531 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11532 gcc_assert (DECL_PARM_INDEX (patparm)
11533 == DECL_PARM_INDEX (parm));
11536 return patparm;
11539 /* Make an argument pack out of the TREE_VEC VEC. */
11541 static tree
11542 make_argument_pack (tree vec)
11544 tree pack;
11545 tree elt = TREE_VEC_ELT (vec, 0);
11546 if (TYPE_P (elt))
11547 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11548 else
11550 pack = make_node (NONTYPE_ARGUMENT_PACK);
11551 TREE_CONSTANT (pack) = 1;
11553 SET_ARGUMENT_PACK_ARGS (pack, vec);
11554 return pack;
11557 /* Return an exact copy of template args T that can be modified
11558 independently. */
11560 static tree
11561 copy_template_args (tree t)
11563 if (t == error_mark_node)
11564 return t;
11566 int len = TREE_VEC_LENGTH (t);
11567 tree new_vec = make_tree_vec (len);
11569 for (int i = 0; i < len; ++i)
11571 tree elt = TREE_VEC_ELT (t, i);
11572 if (elt && TREE_CODE (elt) == TREE_VEC)
11573 elt = copy_template_args (elt);
11574 TREE_VEC_ELT (new_vec, i) = elt;
11577 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11578 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11580 return new_vec;
11583 /* Substitute ARGS into the vector or list of template arguments T. */
11585 static tree
11586 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11588 tree orig_t = t;
11589 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11590 tree *elts;
11592 if (t == error_mark_node)
11593 return error_mark_node;
11595 len = TREE_VEC_LENGTH (t);
11596 elts = XALLOCAVEC (tree, len);
11598 for (i = 0; i < len; i++)
11600 tree orig_arg = TREE_VEC_ELT (t, i);
11601 tree new_arg;
11603 if (TREE_CODE (orig_arg) == TREE_VEC)
11604 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11605 else if (PACK_EXPANSION_P (orig_arg))
11607 /* Substitute into an expansion expression. */
11608 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11610 if (TREE_CODE (new_arg) == TREE_VEC)
11611 /* Add to the expanded length adjustment the number of
11612 expanded arguments. We subtract one from this
11613 measurement, because the argument pack expression
11614 itself is already counted as 1 in
11615 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11616 the argument pack is empty. */
11617 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11619 else if (ARGUMENT_PACK_P (orig_arg))
11621 /* Substitute into each of the arguments. */
11622 new_arg = TYPE_P (orig_arg)
11623 ? cxx_make_type (TREE_CODE (orig_arg))
11624 : make_node (TREE_CODE (orig_arg));
11626 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11627 args, complain, in_decl);
11628 if (pack_args == error_mark_node)
11629 new_arg = error_mark_node;
11630 else
11631 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11633 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11634 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11636 else
11637 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11639 if (new_arg == error_mark_node)
11640 return error_mark_node;
11642 elts[i] = new_arg;
11643 if (new_arg != orig_arg)
11644 need_new = 1;
11647 if (!need_new)
11648 return t;
11650 /* Make space for the expanded arguments coming from template
11651 argument packs. */
11652 t = make_tree_vec (len + expanded_len_adjust);
11653 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11654 arguments for a member template.
11655 In that case each TREE_VEC in ORIG_T represents a level of template
11656 arguments, and ORIG_T won't carry any non defaulted argument count.
11657 It will rather be the nested TREE_VECs that will carry one.
11658 In other words, ORIG_T carries a non defaulted argument count only
11659 if it doesn't contain any nested TREE_VEC. */
11660 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11662 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11663 count += expanded_len_adjust;
11664 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11666 for (i = 0, out = 0; i < len; i++)
11668 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11669 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11670 && TREE_CODE (elts[i]) == TREE_VEC)
11672 int idx;
11674 /* Now expand the template argument pack "in place". */
11675 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11676 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11678 else
11680 TREE_VEC_ELT (t, out) = elts[i];
11681 out++;
11685 return t;
11688 /* Substitute ARGS into one level PARMS of template parameters. */
11690 static tree
11691 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11693 if (parms == error_mark_node)
11694 return error_mark_node;
11696 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11698 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11700 tree tuple = TREE_VEC_ELT (parms, i);
11702 if (tuple == error_mark_node)
11703 continue;
11705 TREE_VEC_ELT (new_vec, i) =
11706 tsubst_template_parm (tuple, args, complain);
11709 return new_vec;
11712 /* Return the result of substituting ARGS into the template parameters
11713 given by PARMS. If there are m levels of ARGS and m + n levels of
11714 PARMS, then the result will contain n levels of PARMS. For
11715 example, if PARMS is `template <class T> template <class U>
11716 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11717 result will be `template <int*, double, class V>'. */
11719 static tree
11720 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11722 tree r = NULL_TREE;
11723 tree* new_parms;
11725 /* When substituting into a template, we must set
11726 PROCESSING_TEMPLATE_DECL as the template parameters may be
11727 dependent if they are based on one-another, and the dependency
11728 predicates are short-circuit outside of templates. */
11729 ++processing_template_decl;
11731 for (new_parms = &r;
11732 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11733 new_parms = &(TREE_CHAIN (*new_parms)),
11734 parms = TREE_CHAIN (parms))
11736 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11737 args, complain);
11738 *new_parms =
11739 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11740 - TMPL_ARGS_DEPTH (args)),
11741 new_vec, NULL_TREE);
11744 --processing_template_decl;
11746 return r;
11749 /* Return the result of substituting ARGS into one template parameter
11750 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11751 parameter and which TREE_PURPOSE is the default argument of the
11752 template parameter. */
11754 static tree
11755 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11757 tree default_value, parm_decl;
11759 if (args == NULL_TREE
11760 || t == NULL_TREE
11761 || t == error_mark_node)
11762 return t;
11764 gcc_assert (TREE_CODE (t) == TREE_LIST);
11766 default_value = TREE_PURPOSE (t);
11767 parm_decl = TREE_VALUE (t);
11769 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11770 if (TREE_CODE (parm_decl) == PARM_DECL
11771 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11772 parm_decl = error_mark_node;
11773 default_value = tsubst_template_arg (default_value, args,
11774 complain, NULL_TREE);
11776 return build_tree_list (default_value, parm_decl);
11779 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11780 type T. If T is not an aggregate or enumeration type, it is
11781 handled as if by tsubst. IN_DECL is as for tsubst. If
11782 ENTERING_SCOPE is nonzero, T is the context for a template which
11783 we are presently tsubst'ing. Return the substituted value. */
11785 static tree
11786 tsubst_aggr_type (tree t,
11787 tree args,
11788 tsubst_flags_t complain,
11789 tree in_decl,
11790 int entering_scope)
11792 if (t == NULL_TREE)
11793 return NULL_TREE;
11795 switch (TREE_CODE (t))
11797 case RECORD_TYPE:
11798 if (TYPE_PTRMEMFUNC_P (t))
11799 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11801 /* Fall through. */
11802 case ENUMERAL_TYPE:
11803 case UNION_TYPE:
11804 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11806 tree argvec;
11807 tree context;
11808 tree r;
11809 int saved_unevaluated_operand;
11810 int saved_inhibit_evaluation_warnings;
11812 /* In "sizeof(X<I>)" we need to evaluate "I". */
11813 saved_unevaluated_operand = cp_unevaluated_operand;
11814 cp_unevaluated_operand = 0;
11815 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11816 c_inhibit_evaluation_warnings = 0;
11818 /* First, determine the context for the type we are looking
11819 up. */
11820 context = TYPE_CONTEXT (t);
11821 if (context && TYPE_P (context))
11823 context = tsubst_aggr_type (context, args, complain,
11824 in_decl, /*entering_scope=*/1);
11825 /* If context is a nested class inside a class template,
11826 it may still need to be instantiated (c++/33959). */
11827 context = complete_type (context);
11830 /* Then, figure out what arguments are appropriate for the
11831 type we are trying to find. For example, given:
11833 template <class T> struct S;
11834 template <class T, class U> void f(T, U) { S<U> su; }
11836 and supposing that we are instantiating f<int, double>,
11837 then our ARGS will be {int, double}, but, when looking up
11838 S we only want {double}. */
11839 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11840 complain, in_decl);
11841 if (argvec == error_mark_node)
11842 r = error_mark_node;
11843 else
11845 r = lookup_template_class (t, argvec, in_decl, context,
11846 entering_scope, complain);
11847 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11850 cp_unevaluated_operand = saved_unevaluated_operand;
11851 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11853 return r;
11855 else
11856 /* This is not a template type, so there's nothing to do. */
11857 return t;
11859 default:
11860 return tsubst (t, args, complain, in_decl);
11864 /* Substitute into the default argument ARG (a default argument for
11865 FN), which has the indicated TYPE. */
11867 tree
11868 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11870 tree saved_class_ptr = NULL_TREE;
11871 tree saved_class_ref = NULL_TREE;
11872 int errs = errorcount + sorrycount;
11874 /* This can happen in invalid code. */
11875 if (TREE_CODE (arg) == DEFAULT_ARG)
11876 return arg;
11878 /* This default argument came from a template. Instantiate the
11879 default argument here, not in tsubst. In the case of
11880 something like:
11882 template <class T>
11883 struct S {
11884 static T t();
11885 void f(T = t());
11888 we must be careful to do name lookup in the scope of S<T>,
11889 rather than in the current class. */
11890 push_access_scope (fn);
11891 /* The "this" pointer is not valid in a default argument. */
11892 if (cfun)
11894 saved_class_ptr = current_class_ptr;
11895 cp_function_chain->x_current_class_ptr = NULL_TREE;
11896 saved_class_ref = current_class_ref;
11897 cp_function_chain->x_current_class_ref = NULL_TREE;
11900 push_deferring_access_checks(dk_no_deferred);
11901 /* The default argument expression may cause implicitly defined
11902 member functions to be synthesized, which will result in garbage
11903 collection. We must treat this situation as if we were within
11904 the body of function so as to avoid collecting live data on the
11905 stack. */
11906 ++function_depth;
11907 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
11908 complain, NULL_TREE,
11909 /*integral_constant_expression_p=*/false);
11910 --function_depth;
11911 pop_deferring_access_checks();
11913 /* Restore the "this" pointer. */
11914 if (cfun)
11916 cp_function_chain->x_current_class_ptr = saved_class_ptr;
11917 cp_function_chain->x_current_class_ref = saved_class_ref;
11920 if (errorcount+sorrycount > errs
11921 && (complain & tf_warning_or_error))
11922 inform (input_location,
11923 " when instantiating default argument for call to %qD", fn);
11925 /* Make sure the default argument is reasonable. */
11926 arg = check_default_argument (type, arg, complain);
11928 pop_access_scope (fn);
11930 return arg;
11933 /* Substitute into all the default arguments for FN. */
11935 static void
11936 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
11938 tree arg;
11939 tree tmpl_args;
11941 tmpl_args = DECL_TI_ARGS (fn);
11943 /* If this function is not yet instantiated, we certainly don't need
11944 its default arguments. */
11945 if (uses_template_parms (tmpl_args))
11946 return;
11947 /* Don't do this again for clones. */
11948 if (DECL_CLONED_FUNCTION_P (fn))
11949 return;
11951 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
11952 arg;
11953 arg = TREE_CHAIN (arg))
11954 if (TREE_PURPOSE (arg))
11955 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
11956 TREE_VALUE (arg),
11957 TREE_PURPOSE (arg),
11958 complain);
11961 /* Substitute the ARGS into the T, which is a _DECL. Return the
11962 result of the substitution. Issue error and warning messages under
11963 control of COMPLAIN. */
11965 static tree
11966 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
11968 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
11969 location_t saved_loc;
11970 tree r = NULL_TREE;
11971 tree in_decl = t;
11972 hashval_t hash = 0;
11974 /* Set the filename and linenumber to improve error-reporting. */
11975 saved_loc = input_location;
11976 input_location = DECL_SOURCE_LOCATION (t);
11978 switch (TREE_CODE (t))
11980 case TEMPLATE_DECL:
11982 /* We can get here when processing a member function template,
11983 member class template, or template template parameter. */
11984 tree decl = DECL_TEMPLATE_RESULT (t);
11985 tree spec;
11986 tree tmpl_args;
11987 tree full_args;
11989 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11991 /* Template template parameter is treated here. */
11992 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11993 if (new_type == error_mark_node)
11994 r = error_mark_node;
11995 /* If we get a real template back, return it. This can happen in
11996 the context of most_specialized_partial_spec. */
11997 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
11998 r = new_type;
11999 else
12000 /* The new TEMPLATE_DECL was built in
12001 reduce_template_parm_level. */
12002 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12003 break;
12006 /* We might already have an instance of this template.
12007 The ARGS are for the surrounding class type, so the
12008 full args contain the tsubst'd args for the context,
12009 plus the innermost args from the template decl. */
12010 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12011 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12012 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12013 /* Because this is a template, the arguments will still be
12014 dependent, even after substitution. If
12015 PROCESSING_TEMPLATE_DECL is not set, the dependency
12016 predicates will short-circuit. */
12017 ++processing_template_decl;
12018 full_args = tsubst_template_args (tmpl_args, args,
12019 complain, in_decl);
12020 --processing_template_decl;
12021 if (full_args == error_mark_node)
12022 RETURN (error_mark_node);
12024 /* If this is a default template template argument,
12025 tsubst might not have changed anything. */
12026 if (full_args == tmpl_args)
12027 RETURN (t);
12029 hash = hash_tmpl_and_args (t, full_args);
12030 spec = retrieve_specialization (t, full_args, hash);
12031 if (spec != NULL_TREE)
12033 r = spec;
12034 break;
12037 /* Make a new template decl. It will be similar to the
12038 original, but will record the current template arguments.
12039 We also create a new function declaration, which is just
12040 like the old one, but points to this new template, rather
12041 than the old one. */
12042 r = copy_decl (t);
12043 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12044 DECL_CHAIN (r) = NULL_TREE;
12046 // Build new template info linking to the original template decl.
12047 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12049 if (TREE_CODE (decl) == TYPE_DECL
12050 && !TYPE_DECL_ALIAS_P (decl))
12052 tree new_type;
12053 ++processing_template_decl;
12054 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12055 --processing_template_decl;
12056 if (new_type == error_mark_node)
12057 RETURN (error_mark_node);
12059 TREE_TYPE (r) = new_type;
12060 /* For a partial specialization, we need to keep pointing to
12061 the primary template. */
12062 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12063 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12064 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12065 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12066 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12068 else
12070 tree new_decl;
12071 ++processing_template_decl;
12072 new_decl = tsubst (decl, args, complain, in_decl);
12073 --processing_template_decl;
12074 if (new_decl == error_mark_node)
12075 RETURN (error_mark_node);
12077 DECL_TEMPLATE_RESULT (r) = new_decl;
12078 DECL_TI_TEMPLATE (new_decl) = r;
12079 TREE_TYPE (r) = TREE_TYPE (new_decl);
12080 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12081 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12084 SET_DECL_IMPLICIT_INSTANTIATION (r);
12085 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12086 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12088 /* The template parameters for this new template are all the
12089 template parameters for the old template, except the
12090 outermost level of parameters. */
12091 DECL_TEMPLATE_PARMS (r)
12092 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12093 complain);
12095 if (PRIMARY_TEMPLATE_P (t))
12096 DECL_PRIMARY_TEMPLATE (r) = r;
12098 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12099 /* Record this non-type partial instantiation. */
12100 register_specialization (r, t,
12101 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12102 false, hash);
12104 break;
12106 case FUNCTION_DECL:
12108 tree ctx;
12109 tree argvec = NULL_TREE;
12110 tree *friends;
12111 tree gen_tmpl;
12112 tree type;
12113 int member;
12114 int args_depth;
12115 int parms_depth;
12117 /* Nobody should be tsubst'ing into non-template functions. */
12118 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12120 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12122 tree spec;
12124 /* If T is not dependent, just return it. */
12125 if (!uses_template_parms (DECL_TI_ARGS (t)))
12126 RETURN (t);
12128 /* Calculate the most general template of which R is a
12129 specialization, and the complete set of arguments used to
12130 specialize R. */
12131 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12132 argvec = tsubst_template_args (DECL_TI_ARGS
12133 (DECL_TEMPLATE_RESULT
12134 (DECL_TI_TEMPLATE (t))),
12135 args, complain, in_decl);
12136 if (argvec == error_mark_node)
12137 RETURN (error_mark_node);
12139 /* Check to see if we already have this specialization. */
12140 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12141 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12143 if (spec)
12145 r = spec;
12146 break;
12149 /* We can see more levels of arguments than parameters if
12150 there was a specialization of a member template, like
12151 this:
12153 template <class T> struct S { template <class U> void f(); }
12154 template <> template <class U> void S<int>::f(U);
12156 Here, we'll be substituting into the specialization,
12157 because that's where we can find the code we actually
12158 want to generate, but we'll have enough arguments for
12159 the most general template.
12161 We also deal with the peculiar case:
12163 template <class T> struct S {
12164 template <class U> friend void f();
12166 template <class U> void f() {}
12167 template S<int>;
12168 template void f<double>();
12170 Here, the ARGS for the instantiation of will be {int,
12171 double}. But, we only need as many ARGS as there are
12172 levels of template parameters in CODE_PATTERN. We are
12173 careful not to get fooled into reducing the ARGS in
12174 situations like:
12176 template <class T> struct S { template <class U> void f(U); }
12177 template <class T> template <> void S<T>::f(int) {}
12179 which we can spot because the pattern will be a
12180 specialization in this case. */
12181 args_depth = TMPL_ARGS_DEPTH (args);
12182 parms_depth =
12183 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12184 if (args_depth > parms_depth
12185 && !DECL_TEMPLATE_SPECIALIZATION (t))
12186 args = get_innermost_template_args (args, parms_depth);
12188 else
12190 /* This special case arises when we have something like this:
12192 template <class T> struct S {
12193 friend void f<int>(int, double);
12196 Here, the DECL_TI_TEMPLATE for the friend declaration
12197 will be an IDENTIFIER_NODE. We are being called from
12198 tsubst_friend_function, and we want only to create a
12199 new decl (R) with appropriate types so that we can call
12200 determine_specialization. */
12201 gen_tmpl = NULL_TREE;
12204 if (DECL_CLASS_SCOPE_P (t))
12206 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12207 member = 2;
12208 else
12209 member = 1;
12210 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12211 complain, t, /*entering_scope=*/1);
12213 else
12215 member = 0;
12216 ctx = DECL_CONTEXT (t);
12218 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12219 if (type == error_mark_node)
12220 RETURN (error_mark_node);
12222 /* If we hit excessive deduction depth, the type is bogus even if
12223 it isn't error_mark_node, so don't build a decl. */
12224 if (excessive_deduction_depth)
12225 RETURN (error_mark_node);
12227 /* We do NOT check for matching decls pushed separately at this
12228 point, as they may not represent instantiations of this
12229 template, and in any case are considered separate under the
12230 discrete model. */
12231 r = copy_decl (t);
12232 DECL_USE_TEMPLATE (r) = 0;
12233 TREE_TYPE (r) = type;
12234 /* Clear out the mangled name and RTL for the instantiation. */
12235 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12236 SET_DECL_RTL (r, NULL);
12237 /* Leave DECL_INITIAL set on deleted instantiations. */
12238 if (!DECL_DELETED_FN (r))
12239 DECL_INITIAL (r) = NULL_TREE;
12240 DECL_CONTEXT (r) = ctx;
12242 /* OpenMP UDRs have the only argument a reference to the declared
12243 type. We want to diagnose if the declared type is a reference,
12244 which is invalid, but as references to references are usually
12245 quietly merged, diagnose it here. */
12246 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12248 tree argtype
12249 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12250 argtype = tsubst (argtype, args, complain, in_decl);
12251 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12252 error_at (DECL_SOURCE_LOCATION (t),
12253 "reference type %qT in "
12254 "%<#pragma omp declare reduction%>", argtype);
12255 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12256 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12257 argtype);
12260 if (member && DECL_CONV_FN_P (r))
12261 /* Type-conversion operator. Reconstruct the name, in
12262 case it's the name of one of the template's parameters. */
12263 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12265 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12266 complain, t);
12267 DECL_RESULT (r) = NULL_TREE;
12269 TREE_STATIC (r) = 0;
12270 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12271 DECL_EXTERNAL (r) = 1;
12272 /* If this is an instantiation of a function with internal
12273 linkage, we already know what object file linkage will be
12274 assigned to the instantiation. */
12275 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12276 DECL_DEFER_OUTPUT (r) = 0;
12277 DECL_CHAIN (r) = NULL_TREE;
12278 DECL_PENDING_INLINE_INFO (r) = 0;
12279 DECL_PENDING_INLINE_P (r) = 0;
12280 DECL_SAVED_TREE (r) = NULL_TREE;
12281 DECL_STRUCT_FUNCTION (r) = NULL;
12282 TREE_USED (r) = 0;
12283 /* We'll re-clone as appropriate in instantiate_template. */
12284 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12286 /* If we aren't complaining now, return on error before we register
12287 the specialization so that we'll complain eventually. */
12288 if ((complain & tf_error) == 0
12289 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12290 && !grok_op_properties (r, /*complain=*/false))
12291 RETURN (error_mark_node);
12293 /* When instantiating a constrained member, substitute
12294 into the constraints to create a new constraint. */
12295 if (tree ci = get_constraints (t))
12296 if (member)
12298 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12299 set_constraints (r, ci);
12302 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12303 this in the special friend case mentioned above where
12304 GEN_TMPL is NULL. */
12305 if (gen_tmpl)
12307 DECL_TEMPLATE_INFO (r)
12308 = build_template_info (gen_tmpl, argvec);
12309 SET_DECL_IMPLICIT_INSTANTIATION (r);
12311 tree new_r
12312 = register_specialization (r, gen_tmpl, argvec, false, hash);
12313 if (new_r != r)
12314 /* We instantiated this while substituting into
12315 the type earlier (template/friend54.C). */
12316 RETURN (new_r);
12318 /* We're not supposed to instantiate default arguments
12319 until they are called, for a template. But, for a
12320 declaration like:
12322 template <class T> void f ()
12323 { extern void g(int i = T()); }
12325 we should do the substitution when the template is
12326 instantiated. We handle the member function case in
12327 instantiate_class_template since the default arguments
12328 might refer to other members of the class. */
12329 if (!member
12330 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12331 && !uses_template_parms (argvec))
12332 tsubst_default_arguments (r, complain);
12334 else
12335 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12337 /* Copy the list of befriending classes. */
12338 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12339 *friends;
12340 friends = &TREE_CHAIN (*friends))
12342 *friends = copy_node (*friends);
12343 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12344 args, complain,
12345 in_decl);
12348 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12350 maybe_retrofit_in_chrg (r);
12351 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12352 RETURN (error_mark_node);
12353 /* If this is an instantiation of a member template, clone it.
12354 If it isn't, that'll be handled by
12355 clone_constructors_and_destructors. */
12356 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12357 clone_function_decl (r, /*update_methods=*/false);
12359 else if ((complain & tf_error) != 0
12360 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12361 && !grok_op_properties (r, /*complain=*/true))
12362 RETURN (error_mark_node);
12364 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12365 SET_DECL_FRIEND_CONTEXT (r,
12366 tsubst (DECL_FRIEND_CONTEXT (t),
12367 args, complain, in_decl));
12369 /* Possibly limit visibility based on template args. */
12370 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12371 if (DECL_VISIBILITY_SPECIFIED (t))
12373 DECL_VISIBILITY_SPECIFIED (r) = 0;
12374 DECL_ATTRIBUTES (r)
12375 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12377 determine_visibility (r);
12378 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12379 && !processing_template_decl)
12380 defaulted_late_check (r);
12382 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12383 args, complain, in_decl);
12385 break;
12387 case PARM_DECL:
12389 tree type = NULL_TREE;
12390 int i, len = 1;
12391 tree expanded_types = NULL_TREE;
12392 tree prev_r = NULL_TREE;
12393 tree first_r = NULL_TREE;
12395 if (DECL_PACK_P (t))
12397 /* If there is a local specialization that isn't a
12398 parameter pack, it means that we're doing a "simple"
12399 substitution from inside tsubst_pack_expansion. Just
12400 return the local specialization (which will be a single
12401 parm). */
12402 tree spec = retrieve_local_specialization (t);
12403 if (spec
12404 && TREE_CODE (spec) == PARM_DECL
12405 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12406 RETURN (spec);
12408 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12409 the parameters in this function parameter pack. */
12410 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12411 complain, in_decl);
12412 if (TREE_CODE (expanded_types) == TREE_VEC)
12414 len = TREE_VEC_LENGTH (expanded_types);
12416 /* Zero-length parameter packs are boring. Just substitute
12417 into the chain. */
12418 if (len == 0)
12419 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12420 TREE_CHAIN (t)));
12422 else
12424 /* All we did was update the type. Make a note of that. */
12425 type = expanded_types;
12426 expanded_types = NULL_TREE;
12430 /* Loop through all of the parameters we'll build. When T is
12431 a function parameter pack, LEN is the number of expanded
12432 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12433 r = NULL_TREE;
12434 for (i = 0; i < len; ++i)
12436 prev_r = r;
12437 r = copy_node (t);
12438 if (DECL_TEMPLATE_PARM_P (t))
12439 SET_DECL_TEMPLATE_PARM_P (r);
12441 if (expanded_types)
12442 /* We're on the Ith parameter of the function parameter
12443 pack. */
12445 /* Get the Ith type. */
12446 type = TREE_VEC_ELT (expanded_types, i);
12448 /* Rename the parameter to include the index. */
12449 DECL_NAME (r)
12450 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12452 else if (!type)
12453 /* We're dealing with a normal parameter. */
12454 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12456 type = type_decays_to (type);
12457 TREE_TYPE (r) = type;
12458 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12460 if (DECL_INITIAL (r))
12462 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12463 DECL_INITIAL (r) = TREE_TYPE (r);
12464 else
12465 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12466 complain, in_decl);
12469 DECL_CONTEXT (r) = NULL_TREE;
12471 if (!DECL_TEMPLATE_PARM_P (r))
12472 DECL_ARG_TYPE (r) = type_passed_as (type);
12474 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12475 args, complain, in_decl);
12477 /* Keep track of the first new parameter we
12478 generate. That's what will be returned to the
12479 caller. */
12480 if (!first_r)
12481 first_r = r;
12483 /* Build a proper chain of parameters when substituting
12484 into a function parameter pack. */
12485 if (prev_r)
12486 DECL_CHAIN (prev_r) = r;
12489 /* If cp_unevaluated_operand is set, we're just looking for a
12490 single dummy parameter, so don't keep going. */
12491 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12492 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12493 complain, DECL_CHAIN (t));
12495 /* FIRST_R contains the start of the chain we've built. */
12496 r = first_r;
12498 break;
12500 case FIELD_DECL:
12502 tree type = NULL_TREE;
12503 tree vec = NULL_TREE;
12504 tree expanded_types = NULL_TREE;
12505 int len = 1;
12507 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12509 /* This field is a lambda capture pack. Return a TREE_VEC of
12510 the expanded fields to instantiate_class_template_1 and
12511 store them in the specializations hash table as a
12512 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12513 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12514 complain, in_decl);
12515 if (TREE_CODE (expanded_types) == TREE_VEC)
12517 len = TREE_VEC_LENGTH (expanded_types);
12518 vec = make_tree_vec (len);
12520 else
12522 /* All we did was update the type. Make a note of that. */
12523 type = expanded_types;
12524 expanded_types = NULL_TREE;
12528 for (int i = 0; i < len; ++i)
12530 r = copy_decl (t);
12531 if (expanded_types)
12533 type = TREE_VEC_ELT (expanded_types, i);
12534 DECL_NAME (r)
12535 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12537 else if (!type)
12538 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12540 if (type == error_mark_node)
12541 RETURN (error_mark_node);
12542 TREE_TYPE (r) = type;
12543 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12545 if (DECL_C_BIT_FIELD (r))
12546 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12547 non-bit-fields DECL_INITIAL is a non-static data member
12548 initializer, which gets deferred instantiation. */
12549 DECL_INITIAL (r)
12550 = tsubst_expr (DECL_INITIAL (t), args,
12551 complain, in_decl,
12552 /*integral_constant_expression_p=*/true);
12553 else if (DECL_INITIAL (t))
12555 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12556 NSDMI in perform_member_init. Still set DECL_INITIAL
12557 so that we know there is one. */
12558 DECL_INITIAL (r) = void_node;
12559 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12560 retrofit_lang_decl (r);
12561 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12563 /* We don't have to set DECL_CONTEXT here; it is set by
12564 finish_member_declaration. */
12565 DECL_CHAIN (r) = NULL_TREE;
12567 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12568 args, complain, in_decl);
12570 if (vec)
12571 TREE_VEC_ELT (vec, i) = r;
12574 if (vec)
12576 r = vec;
12577 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12578 SET_ARGUMENT_PACK_ARGS (pack, vec);
12579 register_specialization (pack, t, args, false, 0);
12582 break;
12584 case USING_DECL:
12585 /* We reach here only for member using decls. We also need to check
12586 uses_template_parms because DECL_DEPENDENT_P is not set for a
12587 using-declaration that designates a member of the current
12588 instantiation (c++/53549). */
12589 if (DECL_DEPENDENT_P (t)
12590 || uses_template_parms (USING_DECL_SCOPE (t)))
12592 tree scope = USING_DECL_SCOPE (t);
12593 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12594 if (PACK_EXPANSION_P (scope))
12596 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12597 int len = TREE_VEC_LENGTH (vec);
12598 r = make_tree_vec (len);
12599 for (int i = 0; i < len; ++i)
12601 tree escope = TREE_VEC_ELT (vec, i);
12602 tree elt = do_class_using_decl (escope, name);
12603 if (!elt)
12605 r = error_mark_node;
12606 break;
12608 else
12610 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12611 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12613 TREE_VEC_ELT (r, i) = elt;
12616 else
12618 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12619 complain, in_decl);
12620 r = do_class_using_decl (inst_scope, name);
12621 if (!r)
12622 r = error_mark_node;
12623 else
12625 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12626 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12630 else
12632 r = copy_node (t);
12633 DECL_CHAIN (r) = NULL_TREE;
12635 break;
12637 case TYPE_DECL:
12638 case VAR_DECL:
12640 tree argvec = NULL_TREE;
12641 tree gen_tmpl = NULL_TREE;
12642 tree spec;
12643 tree tmpl = NULL_TREE;
12644 tree ctx;
12645 tree type = NULL_TREE;
12646 bool local_p;
12648 if (TREE_TYPE (t) == error_mark_node)
12649 RETURN (error_mark_node);
12651 if (TREE_CODE (t) == TYPE_DECL
12652 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12654 /* If this is the canonical decl, we don't have to
12655 mess with instantiations, and often we can't (for
12656 typename, template type parms and such). Note that
12657 TYPE_NAME is not correct for the above test if
12658 we've copied the type for a typedef. */
12659 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12660 if (type == error_mark_node)
12661 RETURN (error_mark_node);
12662 r = TYPE_NAME (type);
12663 break;
12666 /* Check to see if we already have the specialization we
12667 need. */
12668 spec = NULL_TREE;
12669 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12671 /* T is a static data member or namespace-scope entity.
12672 We have to substitute into namespace-scope variables
12673 (not just variable templates) because of cases like:
12675 template <class T> void f() { extern T t; }
12677 where the entity referenced is not known until
12678 instantiation time. */
12679 local_p = false;
12680 ctx = DECL_CONTEXT (t);
12681 if (DECL_CLASS_SCOPE_P (t))
12683 ctx = tsubst_aggr_type (ctx, args,
12684 complain,
12685 in_decl, /*entering_scope=*/1);
12686 /* If CTX is unchanged, then T is in fact the
12687 specialization we want. That situation occurs when
12688 referencing a static data member within in its own
12689 class. We can use pointer equality, rather than
12690 same_type_p, because DECL_CONTEXT is always
12691 canonical... */
12692 if (ctx == DECL_CONTEXT (t)
12693 /* ... unless T is a member template; in which
12694 case our caller can be willing to create a
12695 specialization of that template represented
12696 by T. */
12697 && !(DECL_TI_TEMPLATE (t)
12698 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12699 spec = t;
12702 if (!spec)
12704 tmpl = DECL_TI_TEMPLATE (t);
12705 gen_tmpl = most_general_template (tmpl);
12706 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12707 if (argvec != error_mark_node)
12708 argvec = (coerce_innermost_template_parms
12709 (DECL_TEMPLATE_PARMS (gen_tmpl),
12710 argvec, t, complain,
12711 /*all*/true, /*defarg*/true));
12712 if (argvec == error_mark_node)
12713 RETURN (error_mark_node);
12714 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12715 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12718 else
12720 /* A local variable. */
12721 local_p = true;
12722 /* Subsequent calls to pushdecl will fill this in. */
12723 ctx = NULL_TREE;
12724 /* Unless this is a reference to a static variable from an
12725 enclosing function, in which case we need to fill it in now. */
12726 if (TREE_STATIC (t))
12728 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12729 if (fn != current_function_decl)
12730 ctx = fn;
12732 spec = retrieve_local_specialization (t);
12734 /* If we already have the specialization we need, there is
12735 nothing more to do. */
12736 if (spec)
12738 r = spec;
12739 break;
12742 /* Create a new node for the specialization we need. */
12743 r = copy_decl (t);
12744 if (type == NULL_TREE)
12746 if (is_typedef_decl (t))
12747 type = DECL_ORIGINAL_TYPE (t);
12748 else
12749 type = TREE_TYPE (t);
12750 if (VAR_P (t)
12751 && VAR_HAD_UNKNOWN_BOUND (t)
12752 && type != error_mark_node)
12753 type = strip_array_domain (type);
12754 type = tsubst (type, args, complain, in_decl);
12756 if (VAR_P (r))
12758 /* Even if the original location is out of scope, the
12759 newly substituted one is not. */
12760 DECL_DEAD_FOR_LOCAL (r) = 0;
12761 DECL_INITIALIZED_P (r) = 0;
12762 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12763 if (type == error_mark_node)
12764 RETURN (error_mark_node);
12765 if (TREE_CODE (type) == FUNCTION_TYPE)
12767 /* It may seem that this case cannot occur, since:
12769 typedef void f();
12770 void g() { f x; }
12772 declares a function, not a variable. However:
12774 typedef void f();
12775 template <typename T> void g() { T t; }
12776 template void g<f>();
12778 is an attempt to declare a variable with function
12779 type. */
12780 error ("variable %qD has function type",
12781 /* R is not yet sufficiently initialized, so we
12782 just use its name. */
12783 DECL_NAME (r));
12784 RETURN (error_mark_node);
12786 type = complete_type (type);
12787 /* Wait until cp_finish_decl to set this again, to handle
12788 circular dependency (template/instantiate6.C). */
12789 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12790 type = check_var_type (DECL_NAME (r), type);
12792 if (DECL_HAS_VALUE_EXPR_P (t))
12794 tree ve = DECL_VALUE_EXPR (t);
12795 ve = tsubst_expr (ve, args, complain, in_decl,
12796 /*constant_expression_p=*/false);
12797 if (REFERENCE_REF_P (ve))
12799 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12800 ve = TREE_OPERAND (ve, 0);
12802 SET_DECL_VALUE_EXPR (r, ve);
12804 if (CP_DECL_THREAD_LOCAL_P (r)
12805 && !processing_template_decl)
12806 set_decl_tls_model (r, decl_default_tls_model (r));
12808 else if (DECL_SELF_REFERENCE_P (t))
12809 SET_DECL_SELF_REFERENCE_P (r);
12810 TREE_TYPE (r) = type;
12811 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12812 DECL_CONTEXT (r) = ctx;
12813 /* Clear out the mangled name and RTL for the instantiation. */
12814 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12815 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12816 SET_DECL_RTL (r, NULL);
12817 /* The initializer must not be expanded until it is required;
12818 see [temp.inst]. */
12819 DECL_INITIAL (r) = NULL_TREE;
12820 if (VAR_P (r))
12821 SET_DECL_MODE (r, VOIDmode);
12822 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12823 SET_DECL_RTL (r, NULL);
12824 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12825 if (VAR_P (r))
12827 /* Possibly limit visibility based on template args. */
12828 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12829 if (DECL_VISIBILITY_SPECIFIED (t))
12831 DECL_VISIBILITY_SPECIFIED (r) = 0;
12832 DECL_ATTRIBUTES (r)
12833 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12835 determine_visibility (r);
12838 if (!local_p)
12840 /* A static data member declaration is always marked
12841 external when it is declared in-class, even if an
12842 initializer is present. We mimic the non-template
12843 processing here. */
12844 DECL_EXTERNAL (r) = 1;
12845 if (DECL_NAMESPACE_SCOPE_P (t))
12846 DECL_NOT_REALLY_EXTERN (r) = 1;
12848 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12849 SET_DECL_IMPLICIT_INSTANTIATION (r);
12850 register_specialization (r, gen_tmpl, argvec, false, hash);
12852 else
12854 if (DECL_LANG_SPECIFIC (r))
12855 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12856 if (!cp_unevaluated_operand)
12857 register_local_specialization (r, t);
12860 DECL_CHAIN (r) = NULL_TREE;
12862 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12863 /*flags=*/0,
12864 args, complain, in_decl);
12866 /* Preserve a typedef that names a type. */
12867 if (is_typedef_decl (r) && type != error_mark_node)
12869 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12870 set_underlying_type (r);
12871 if (TYPE_DECL_ALIAS_P (r))
12872 /* An alias template specialization can be dependent
12873 even if its underlying type is not. */
12874 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12877 layout_decl (r, 0);
12879 break;
12881 default:
12882 gcc_unreachable ();
12884 #undef RETURN
12886 out:
12887 /* Restore the file and line information. */
12888 input_location = saved_loc;
12890 return r;
12893 /* Substitute into the ARG_TYPES of a function type.
12894 If END is a TREE_CHAIN, leave it and any following types
12895 un-substituted. */
12897 static tree
12898 tsubst_arg_types (tree arg_types,
12899 tree args,
12900 tree end,
12901 tsubst_flags_t complain,
12902 tree in_decl)
12904 tree remaining_arg_types;
12905 tree type = NULL_TREE;
12906 int i = 1;
12907 tree expanded_args = NULL_TREE;
12908 tree default_arg;
12910 if (!arg_types || arg_types == void_list_node || arg_types == end)
12911 return arg_types;
12913 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
12914 args, end, complain, in_decl);
12915 if (remaining_arg_types == error_mark_node)
12916 return error_mark_node;
12918 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
12920 /* For a pack expansion, perform substitution on the
12921 entire expression. Later on, we'll handle the arguments
12922 one-by-one. */
12923 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
12924 args, complain, in_decl);
12926 if (TREE_CODE (expanded_args) == TREE_VEC)
12927 /* So that we'll spin through the parameters, one by one. */
12928 i = TREE_VEC_LENGTH (expanded_args);
12929 else
12931 /* We only partially substituted into the parameter
12932 pack. Our type is TYPE_PACK_EXPANSION. */
12933 type = expanded_args;
12934 expanded_args = NULL_TREE;
12938 while (i > 0) {
12939 --i;
12941 if (expanded_args)
12942 type = TREE_VEC_ELT (expanded_args, i);
12943 else if (!type)
12944 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
12946 if (type == error_mark_node)
12947 return error_mark_node;
12948 if (VOID_TYPE_P (type))
12950 if (complain & tf_error)
12952 error ("invalid parameter type %qT", type);
12953 if (in_decl)
12954 error ("in declaration %q+D", in_decl);
12956 return error_mark_node;
12958 /* DR 657. */
12959 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
12960 return error_mark_node;
12962 /* Do array-to-pointer, function-to-pointer conversion, and ignore
12963 top-level qualifiers as required. */
12964 type = cv_unqualified (type_decays_to (type));
12966 /* We do not substitute into default arguments here. The standard
12967 mandates that they be instantiated only when needed, which is
12968 done in build_over_call. */
12969 default_arg = TREE_PURPOSE (arg_types);
12971 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
12973 /* We've instantiated a template before its default arguments
12974 have been parsed. This can happen for a nested template
12975 class, and is not an error unless we require the default
12976 argument in a call of this function. */
12977 remaining_arg_types =
12978 tree_cons (default_arg, type, remaining_arg_types);
12979 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
12981 else
12982 remaining_arg_types =
12983 hash_tree_cons (default_arg, type, remaining_arg_types);
12986 return remaining_arg_types;
12989 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
12990 *not* handle the exception-specification for FNTYPE, because the
12991 initial substitution of explicitly provided template parameters
12992 during argument deduction forbids substitution into the
12993 exception-specification:
12995 [temp.deduct]
12997 All references in the function type of the function template to the
12998 corresponding template parameters are replaced by the specified tem-
12999 plate argument values. If a substitution in a template parameter or
13000 in the function type of the function template results in an invalid
13001 type, type deduction fails. [Note: The equivalent substitution in
13002 exception specifications is done only when the function is instanti-
13003 ated, at which point a program is ill-formed if the substitution
13004 results in an invalid type.] */
13006 static tree
13007 tsubst_function_type (tree t,
13008 tree args,
13009 tsubst_flags_t complain,
13010 tree in_decl)
13012 tree return_type;
13013 tree arg_types = NULL_TREE;
13014 tree fntype;
13016 /* The TYPE_CONTEXT is not used for function/method types. */
13017 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13019 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13020 failure. */
13021 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13023 if (late_return_type_p)
13025 /* Substitute the argument types. */
13026 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13027 complain, in_decl);
13028 if (arg_types == error_mark_node)
13029 return error_mark_node;
13031 tree save_ccp = current_class_ptr;
13032 tree save_ccr = current_class_ref;
13033 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13034 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13035 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13036 if (do_inject)
13038 /* DR 1207: 'this' is in scope in the trailing return type. */
13039 inject_this_parameter (this_type, cp_type_quals (this_type));
13042 /* Substitute the return type. */
13043 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13045 if (do_inject)
13047 current_class_ptr = save_ccp;
13048 current_class_ref = save_ccr;
13051 else
13052 /* Substitute the return type. */
13053 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13055 if (return_type == error_mark_node)
13056 return error_mark_node;
13057 /* DR 486 clarifies that creation of a function type with an
13058 invalid return type is a deduction failure. */
13059 if (TREE_CODE (return_type) == ARRAY_TYPE
13060 || TREE_CODE (return_type) == FUNCTION_TYPE)
13062 if (complain & tf_error)
13064 if (TREE_CODE (return_type) == ARRAY_TYPE)
13065 error ("function returning an array");
13066 else
13067 error ("function returning a function");
13069 return error_mark_node;
13071 /* And DR 657. */
13072 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13073 return error_mark_node;
13075 if (!late_return_type_p)
13077 /* Substitute the argument types. */
13078 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13079 complain, in_decl);
13080 if (arg_types == error_mark_node)
13081 return error_mark_node;
13084 /* Construct a new type node and return it. */
13085 if (TREE_CODE (t) == FUNCTION_TYPE)
13087 fntype = build_function_type (return_type, arg_types);
13088 fntype = apply_memfn_quals (fntype,
13089 type_memfn_quals (t),
13090 type_memfn_rqual (t));
13092 else
13094 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13095 /* Don't pick up extra function qualifiers from the basetype. */
13096 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13097 if (! MAYBE_CLASS_TYPE_P (r))
13099 /* [temp.deduct]
13101 Type deduction may fail for any of the following
13102 reasons:
13104 -- Attempting to create "pointer to member of T" when T
13105 is not a class type. */
13106 if (complain & tf_error)
13107 error ("creating pointer to member function of non-class type %qT",
13109 return error_mark_node;
13112 fntype = build_method_type_directly (r, return_type,
13113 TREE_CHAIN (arg_types));
13114 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13116 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13118 if (late_return_type_p)
13119 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13121 return fntype;
13124 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13125 ARGS into that specification, and return the substituted
13126 specification. If there is no specification, return NULL_TREE. */
13128 static tree
13129 tsubst_exception_specification (tree fntype,
13130 tree args,
13131 tsubst_flags_t complain,
13132 tree in_decl,
13133 bool defer_ok)
13135 tree specs;
13136 tree new_specs;
13138 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13139 new_specs = NULL_TREE;
13140 if (specs && TREE_PURPOSE (specs))
13142 /* A noexcept-specifier. */
13143 tree expr = TREE_PURPOSE (specs);
13144 if (TREE_CODE (expr) == INTEGER_CST)
13145 new_specs = expr;
13146 else if (defer_ok)
13148 /* Defer instantiation of noexcept-specifiers to avoid
13149 excessive instantiations (c++/49107). */
13150 new_specs = make_node (DEFERRED_NOEXCEPT);
13151 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13153 /* We already partially instantiated this member template,
13154 so combine the new args with the old. */
13155 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13156 = DEFERRED_NOEXCEPT_PATTERN (expr);
13157 DEFERRED_NOEXCEPT_ARGS (new_specs)
13158 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13160 else
13162 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13163 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13166 else
13167 new_specs = tsubst_copy_and_build
13168 (expr, args, complain, in_decl, /*function_p=*/false,
13169 /*integral_constant_expression_p=*/true);
13170 new_specs = build_noexcept_spec (new_specs, complain);
13172 else if (specs)
13174 if (! TREE_VALUE (specs))
13175 new_specs = specs;
13176 else
13177 while (specs)
13179 tree spec;
13180 int i, len = 1;
13181 tree expanded_specs = NULL_TREE;
13183 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13185 /* Expand the pack expansion type. */
13186 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13187 args, complain,
13188 in_decl);
13190 if (expanded_specs == error_mark_node)
13191 return error_mark_node;
13192 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13193 len = TREE_VEC_LENGTH (expanded_specs);
13194 else
13196 /* We're substituting into a member template, so
13197 we got a TYPE_PACK_EXPANSION back. Add that
13198 expansion and move on. */
13199 gcc_assert (TREE_CODE (expanded_specs)
13200 == TYPE_PACK_EXPANSION);
13201 new_specs = add_exception_specifier (new_specs,
13202 expanded_specs,
13203 complain);
13204 specs = TREE_CHAIN (specs);
13205 continue;
13209 for (i = 0; i < len; ++i)
13211 if (expanded_specs)
13212 spec = TREE_VEC_ELT (expanded_specs, i);
13213 else
13214 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13215 if (spec == error_mark_node)
13216 return spec;
13217 new_specs = add_exception_specifier (new_specs, spec,
13218 complain);
13221 specs = TREE_CHAIN (specs);
13224 return new_specs;
13227 /* Take the tree structure T and replace template parameters used
13228 therein with the argument vector ARGS. IN_DECL is an associated
13229 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13230 Issue error and warning messages under control of COMPLAIN. Note
13231 that we must be relatively non-tolerant of extensions here, in
13232 order to preserve conformance; if we allow substitutions that
13233 should not be allowed, we may allow argument deductions that should
13234 not succeed, and therefore report ambiguous overload situations
13235 where there are none. In theory, we could allow the substitution,
13236 but indicate that it should have failed, and allow our caller to
13237 make sure that the right thing happens, but we don't try to do this
13238 yet.
13240 This function is used for dealing with types, decls and the like;
13241 for expressions, use tsubst_expr or tsubst_copy. */
13243 tree
13244 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13246 enum tree_code code;
13247 tree type, r = NULL_TREE;
13249 if (t == NULL_TREE || t == error_mark_node
13250 || t == integer_type_node
13251 || t == void_type_node
13252 || t == char_type_node
13253 || t == unknown_type_node
13254 || TREE_CODE (t) == NAMESPACE_DECL
13255 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13256 return t;
13258 if (DECL_P (t))
13259 return tsubst_decl (t, args, complain);
13261 if (args == NULL_TREE)
13262 return t;
13264 code = TREE_CODE (t);
13266 if (code == IDENTIFIER_NODE)
13267 type = IDENTIFIER_TYPE_VALUE (t);
13268 else
13269 type = TREE_TYPE (t);
13271 gcc_assert (type != unknown_type_node);
13273 /* Reuse typedefs. We need to do this to handle dependent attributes,
13274 such as attribute aligned. */
13275 if (TYPE_P (t)
13276 && typedef_variant_p (t))
13278 tree decl = TYPE_NAME (t);
13280 if (alias_template_specialization_p (t))
13282 /* DECL represents an alias template and we want to
13283 instantiate it. */
13284 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13285 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13286 r = instantiate_alias_template (tmpl, gen_args, complain);
13288 else if (DECL_CLASS_SCOPE_P (decl)
13289 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13290 && uses_template_parms (DECL_CONTEXT (decl)))
13292 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13293 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13294 r = retrieve_specialization (tmpl, gen_args, 0);
13296 else if (DECL_FUNCTION_SCOPE_P (decl)
13297 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13298 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13299 r = retrieve_local_specialization (decl);
13300 else
13301 /* The typedef is from a non-template context. */
13302 return t;
13304 if (r)
13306 r = TREE_TYPE (r);
13307 r = cp_build_qualified_type_real
13308 (r, cp_type_quals (t) | cp_type_quals (r),
13309 complain | tf_ignore_bad_quals);
13310 return r;
13312 else
13314 /* We don't have an instantiation yet, so drop the typedef. */
13315 int quals = cp_type_quals (t);
13316 t = DECL_ORIGINAL_TYPE (decl);
13317 t = cp_build_qualified_type_real (t, quals,
13318 complain | tf_ignore_bad_quals);
13322 bool fndecl_type = (complain & tf_fndecl_type);
13323 complain &= ~tf_fndecl_type;
13325 if (type
13326 && code != TYPENAME_TYPE
13327 && code != TEMPLATE_TYPE_PARM
13328 && code != TEMPLATE_PARM_INDEX
13329 && code != IDENTIFIER_NODE
13330 && code != FUNCTION_TYPE
13331 && code != METHOD_TYPE)
13332 type = tsubst (type, args, complain, in_decl);
13333 if (type == error_mark_node)
13334 return error_mark_node;
13336 switch (code)
13338 case RECORD_TYPE:
13339 case UNION_TYPE:
13340 case ENUMERAL_TYPE:
13341 return tsubst_aggr_type (t, args, complain, in_decl,
13342 /*entering_scope=*/0);
13344 case ERROR_MARK:
13345 case IDENTIFIER_NODE:
13346 case VOID_TYPE:
13347 case REAL_TYPE:
13348 case COMPLEX_TYPE:
13349 case VECTOR_TYPE:
13350 case BOOLEAN_TYPE:
13351 case NULLPTR_TYPE:
13352 case LANG_TYPE:
13353 return t;
13355 case INTEGER_TYPE:
13356 if (t == integer_type_node)
13357 return t;
13359 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13360 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13361 return t;
13364 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13366 max = tsubst_expr (omax, args, complain, in_decl,
13367 /*integral_constant_expression_p=*/false);
13369 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13370 needed. */
13371 if (TREE_CODE (max) == NOP_EXPR
13372 && TREE_SIDE_EFFECTS (omax)
13373 && !TREE_TYPE (max))
13374 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13376 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13377 with TREE_SIDE_EFFECTS that indicates this is not an integral
13378 constant expression. */
13379 if (processing_template_decl
13380 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13382 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13383 TREE_SIDE_EFFECTS (max) = 1;
13386 return compute_array_index_type (NULL_TREE, max, complain);
13389 case TEMPLATE_TYPE_PARM:
13390 case TEMPLATE_TEMPLATE_PARM:
13391 case BOUND_TEMPLATE_TEMPLATE_PARM:
13392 case TEMPLATE_PARM_INDEX:
13394 int idx;
13395 int level;
13396 int levels;
13397 tree arg = NULL_TREE;
13399 /* Early in template argument deduction substitution, we don't
13400 want to reduce the level of 'auto', or it will be confused
13401 with a normal template parm in subsequent deduction. */
13402 if (is_auto (t) && (complain & tf_partial))
13403 return t;
13405 r = NULL_TREE;
13407 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13408 template_parm_level_and_index (t, &level, &idx);
13410 levels = TMPL_ARGS_DEPTH (args);
13411 if (level <= levels
13412 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13414 arg = TMPL_ARG (args, level, idx);
13416 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13418 /* See through ARGUMENT_PACK_SELECT arguments. */
13419 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13420 /* If the selected argument is an expansion E, that most
13421 likely means we were called from
13422 gen_elem_of_pack_expansion_instantiation during the
13423 substituting of pack an argument pack (which Ith
13424 element is a pack expansion, where I is
13425 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13426 In this case, the Ith element resulting from this
13427 substituting is going to be a pack expansion, which
13428 pattern is the pattern of E. Let's return the
13429 pattern of E, and
13430 gen_elem_of_pack_expansion_instantiation will
13431 build the resulting pack expansion from it. */
13432 if (PACK_EXPANSION_P (arg))
13434 /* Make sure we aren't throwing away arg info. */
13435 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13436 arg = PACK_EXPANSION_PATTERN (arg);
13441 if (arg == error_mark_node)
13442 return error_mark_node;
13443 else if (arg != NULL_TREE)
13445 if (ARGUMENT_PACK_P (arg))
13446 /* If ARG is an argument pack, we don't actually want to
13447 perform a substitution here, because substitutions
13448 for argument packs are only done
13449 element-by-element. We can get to this point when
13450 substituting the type of a non-type template
13451 parameter pack, when that type actually contains
13452 template parameter packs from an outer template, e.g.,
13454 template<typename... Types> struct A {
13455 template<Types... Values> struct B { };
13456 }; */
13457 return t;
13459 if (code == TEMPLATE_TYPE_PARM)
13461 int quals;
13462 gcc_assert (TYPE_P (arg));
13464 quals = cp_type_quals (arg) | cp_type_quals (t);
13466 return cp_build_qualified_type_real
13467 (arg, quals, complain | tf_ignore_bad_quals);
13469 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13471 /* We are processing a type constructed from a
13472 template template parameter. */
13473 tree argvec = tsubst (TYPE_TI_ARGS (t),
13474 args, complain, in_decl);
13475 if (argvec == error_mark_node)
13476 return error_mark_node;
13478 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13479 || TREE_CODE (arg) == TEMPLATE_DECL
13480 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13482 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13483 /* Consider this code:
13485 template <template <class> class Template>
13486 struct Internal {
13487 template <class Arg> using Bind = Template<Arg>;
13490 template <template <class> class Template, class Arg>
13491 using Instantiate = Template<Arg>; //#0
13493 template <template <class> class Template,
13494 class Argument>
13495 using Bind =
13496 Instantiate<Internal<Template>::template Bind,
13497 Argument>; //#1
13499 When #1 is parsed, the
13500 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13501 parameter `Template' in #0 matches the
13502 UNBOUND_CLASS_TEMPLATE representing the argument
13503 `Internal<Template>::template Bind'; We then want
13504 to assemble the type `Bind<Argument>' that can't
13505 be fully created right now, because
13506 `Internal<Template>' not being complete, the Bind
13507 template cannot be looked up in that context. So
13508 we need to "store" `Bind<Argument>' for later
13509 when the context of Bind becomes complete. Let's
13510 store that in a TYPENAME_TYPE. */
13511 return make_typename_type (TYPE_CONTEXT (arg),
13512 build_nt (TEMPLATE_ID_EXPR,
13513 TYPE_IDENTIFIER (arg),
13514 argvec),
13515 typename_type,
13516 complain);
13518 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13519 are resolving nested-types in the signature of a
13520 member function templates. Otherwise ARG is a
13521 TEMPLATE_DECL and is the real template to be
13522 instantiated. */
13523 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13524 arg = TYPE_NAME (arg);
13526 r = lookup_template_class (arg,
13527 argvec, in_decl,
13528 DECL_CONTEXT (arg),
13529 /*entering_scope=*/0,
13530 complain);
13531 return cp_build_qualified_type_real
13532 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13534 else if (code == TEMPLATE_TEMPLATE_PARM)
13535 return arg;
13536 else
13537 /* TEMPLATE_PARM_INDEX. */
13538 return convert_from_reference (unshare_expr (arg));
13541 if (level == 1)
13542 /* This can happen during the attempted tsubst'ing in
13543 unify. This means that we don't yet have any information
13544 about the template parameter in question. */
13545 return t;
13547 /* If we get here, we must have been looking at a parm for a
13548 more deeply nested template. Make a new version of this
13549 template parameter, but with a lower level. */
13550 switch (code)
13552 case TEMPLATE_TYPE_PARM:
13553 case TEMPLATE_TEMPLATE_PARM:
13554 case BOUND_TEMPLATE_TEMPLATE_PARM:
13555 if (cp_type_quals (t))
13557 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13558 r = cp_build_qualified_type_real
13559 (r, cp_type_quals (t),
13560 complain | (code == TEMPLATE_TYPE_PARM
13561 ? tf_ignore_bad_quals : 0));
13563 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13564 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13565 && (r = (TEMPLATE_PARM_DESCENDANTS
13566 (TEMPLATE_TYPE_PARM_INDEX (t))))
13567 && (r = TREE_TYPE (r))
13568 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13569 /* Break infinite recursion when substituting the constraints
13570 of a constrained placeholder. */;
13571 else
13573 r = copy_type (t);
13574 TEMPLATE_TYPE_PARM_INDEX (r)
13575 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13576 r, levels, args, complain);
13577 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13578 TYPE_MAIN_VARIANT (r) = r;
13579 TYPE_POINTER_TO (r) = NULL_TREE;
13580 TYPE_REFERENCE_TO (r) = NULL_TREE;
13582 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13584 /* Propagate constraints on placeholders. */
13585 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13586 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13587 = tsubst_constraint (constr, args, complain, in_decl);
13588 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13590 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13591 pl = tsubst (pl, args, complain, in_decl);
13592 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13596 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13597 /* We have reduced the level of the template
13598 template parameter, but not the levels of its
13599 template parameters, so canonical_type_parameter
13600 will not be able to find the canonical template
13601 template parameter for this level. Thus, we
13602 require structural equality checking to compare
13603 TEMPLATE_TEMPLATE_PARMs. */
13604 SET_TYPE_STRUCTURAL_EQUALITY (r);
13605 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13606 SET_TYPE_STRUCTURAL_EQUALITY (r);
13607 else
13608 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13610 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13612 tree tinfo = TYPE_TEMPLATE_INFO (t);
13613 /* We might need to substitute into the types of non-type
13614 template parameters. */
13615 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13616 complain, in_decl);
13617 if (tmpl == error_mark_node)
13618 return error_mark_node;
13619 tree argvec = tsubst (TI_ARGS (tinfo), args,
13620 complain, in_decl);
13621 if (argvec == error_mark_node)
13622 return error_mark_node;
13624 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13625 = build_template_info (tmpl, argvec);
13628 break;
13630 case TEMPLATE_PARM_INDEX:
13631 /* OK, now substitute the type of the non-type parameter. We
13632 couldn't do it earlier because it might be an auto parameter,
13633 and we wouldn't need to if we had an argument. */
13634 type = tsubst (type, args, complain, in_decl);
13635 r = reduce_template_parm_level (t, type, levels, args, complain);
13636 break;
13638 default:
13639 gcc_unreachable ();
13642 return r;
13645 case TREE_LIST:
13647 tree purpose, value, chain;
13649 if (t == void_list_node)
13650 return t;
13652 purpose = TREE_PURPOSE (t);
13653 if (purpose)
13655 purpose = tsubst (purpose, args, complain, in_decl);
13656 if (purpose == error_mark_node)
13657 return error_mark_node;
13659 value = TREE_VALUE (t);
13660 if (value)
13662 value = tsubst (value, args, complain, in_decl);
13663 if (value == error_mark_node)
13664 return error_mark_node;
13666 chain = TREE_CHAIN (t);
13667 if (chain && chain != void_type_node)
13669 chain = tsubst (chain, args, complain, in_decl);
13670 if (chain == error_mark_node)
13671 return error_mark_node;
13673 if (purpose == TREE_PURPOSE (t)
13674 && value == TREE_VALUE (t)
13675 && chain == TREE_CHAIN (t))
13676 return t;
13677 return hash_tree_cons (purpose, value, chain);
13680 case TREE_BINFO:
13681 /* We should never be tsubsting a binfo. */
13682 gcc_unreachable ();
13684 case TREE_VEC:
13685 /* A vector of template arguments. */
13686 gcc_assert (!type);
13687 return tsubst_template_args (t, args, complain, in_decl);
13689 case POINTER_TYPE:
13690 case REFERENCE_TYPE:
13692 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13693 return t;
13695 /* [temp.deduct]
13697 Type deduction may fail for any of the following
13698 reasons:
13700 -- Attempting to create a pointer to reference type.
13701 -- Attempting to create a reference to a reference type or
13702 a reference to void.
13704 Core issue 106 says that creating a reference to a reference
13705 during instantiation is no longer a cause for failure. We
13706 only enforce this check in strict C++98 mode. */
13707 if ((TREE_CODE (type) == REFERENCE_TYPE
13708 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13709 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13711 static location_t last_loc;
13713 /* We keep track of the last time we issued this error
13714 message to avoid spewing a ton of messages during a
13715 single bad template instantiation. */
13716 if (complain & tf_error
13717 && last_loc != input_location)
13719 if (VOID_TYPE_P (type))
13720 error ("forming reference to void");
13721 else if (code == POINTER_TYPE)
13722 error ("forming pointer to reference type %qT", type);
13723 else
13724 error ("forming reference to reference type %qT", type);
13725 last_loc = input_location;
13728 return error_mark_node;
13730 else if (TREE_CODE (type) == FUNCTION_TYPE
13731 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13732 || type_memfn_rqual (type) != REF_QUAL_NONE))
13734 if (complain & tf_error)
13736 if (code == POINTER_TYPE)
13737 error ("forming pointer to qualified function type %qT",
13738 type);
13739 else
13740 error ("forming reference to qualified function type %qT",
13741 type);
13743 return error_mark_node;
13745 else if (code == POINTER_TYPE)
13747 r = build_pointer_type (type);
13748 if (TREE_CODE (type) == METHOD_TYPE)
13749 r = build_ptrmemfunc_type (r);
13751 else if (TREE_CODE (type) == REFERENCE_TYPE)
13752 /* In C++0x, during template argument substitution, when there is an
13753 attempt to create a reference to a reference type, reference
13754 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13756 "If a template-argument for a template-parameter T names a type
13757 that is a reference to a type A, an attempt to create the type
13758 'lvalue reference to cv T' creates the type 'lvalue reference to
13759 A,' while an attempt to create the type type rvalue reference to
13760 cv T' creates the type T"
13762 r = cp_build_reference_type
13763 (TREE_TYPE (type),
13764 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13765 else
13766 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13767 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13769 if (r != error_mark_node)
13770 /* Will this ever be needed for TYPE_..._TO values? */
13771 layout_type (r);
13773 return r;
13775 case OFFSET_TYPE:
13777 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13778 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13780 /* [temp.deduct]
13782 Type deduction may fail for any of the following
13783 reasons:
13785 -- Attempting to create "pointer to member of T" when T
13786 is not a class type. */
13787 if (complain & tf_error)
13788 error ("creating pointer to member of non-class type %qT", r);
13789 return error_mark_node;
13791 if (TREE_CODE (type) == REFERENCE_TYPE)
13793 if (complain & tf_error)
13794 error ("creating pointer to member reference type %qT", type);
13795 return error_mark_node;
13797 if (VOID_TYPE_P (type))
13799 if (complain & tf_error)
13800 error ("creating pointer to member of type void");
13801 return error_mark_node;
13803 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13804 if (TREE_CODE (type) == FUNCTION_TYPE)
13806 /* The type of the implicit object parameter gets its
13807 cv-qualifiers from the FUNCTION_TYPE. */
13808 tree memptr;
13809 tree method_type
13810 = build_memfn_type (type, r, type_memfn_quals (type),
13811 type_memfn_rqual (type));
13812 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13813 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13814 complain);
13816 else
13817 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13818 cp_type_quals (t),
13819 complain);
13821 case FUNCTION_TYPE:
13822 case METHOD_TYPE:
13824 tree fntype;
13825 tree specs;
13826 fntype = tsubst_function_type (t, args, complain, in_decl);
13827 if (fntype == error_mark_node)
13828 return error_mark_node;
13830 /* Substitute the exception specification. */
13831 specs = tsubst_exception_specification (t, args, complain, in_decl,
13832 /*defer_ok*/fndecl_type);
13833 if (specs == error_mark_node)
13834 return error_mark_node;
13835 if (specs)
13836 fntype = build_exception_variant (fntype, specs);
13837 return fntype;
13839 case ARRAY_TYPE:
13841 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13842 if (domain == error_mark_node)
13843 return error_mark_node;
13845 /* As an optimization, we avoid regenerating the array type if
13846 it will obviously be the same as T. */
13847 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13848 return t;
13850 /* These checks should match the ones in create_array_type_for_decl.
13852 [temp.deduct]
13854 The deduction may fail for any of the following reasons:
13856 -- Attempting to create an array with an element type that
13857 is void, a function type, or a reference type, or [DR337]
13858 an abstract class type. */
13859 if (VOID_TYPE_P (type)
13860 || TREE_CODE (type) == FUNCTION_TYPE
13861 || (TREE_CODE (type) == ARRAY_TYPE
13862 && TYPE_DOMAIN (type) == NULL_TREE)
13863 || TREE_CODE (type) == REFERENCE_TYPE)
13865 if (complain & tf_error)
13866 error ("creating array of %qT", type);
13867 return error_mark_node;
13870 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13871 return error_mark_node;
13873 r = build_cplus_array_type (type, domain);
13875 if (TYPE_USER_ALIGN (t))
13877 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13878 TYPE_USER_ALIGN (r) = 1;
13881 return r;
13884 case TYPENAME_TYPE:
13886 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13887 in_decl, /*entering_scope=*/1);
13888 if (ctx == error_mark_node)
13889 return error_mark_node;
13891 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
13892 complain, in_decl);
13893 if (f == error_mark_node)
13894 return error_mark_node;
13896 if (!MAYBE_CLASS_TYPE_P (ctx))
13898 if (complain & tf_error)
13899 error ("%qT is not a class, struct, or union type", ctx);
13900 return error_mark_node;
13902 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
13904 /* Normally, make_typename_type does not require that the CTX
13905 have complete type in order to allow things like:
13907 template <class T> struct S { typename S<T>::X Y; };
13909 But, such constructs have already been resolved by this
13910 point, so here CTX really should have complete type, unless
13911 it's a partial instantiation. */
13912 ctx = complete_type (ctx);
13913 if (!COMPLETE_TYPE_P (ctx))
13915 if (complain & tf_error)
13916 cxx_incomplete_type_error (NULL_TREE, ctx);
13917 return error_mark_node;
13921 f = make_typename_type (ctx, f, typename_type,
13922 complain | tf_keep_type_decl);
13923 if (f == error_mark_node)
13924 return f;
13925 if (TREE_CODE (f) == TYPE_DECL)
13927 complain |= tf_ignore_bad_quals;
13928 f = TREE_TYPE (f);
13931 if (TREE_CODE (f) != TYPENAME_TYPE)
13933 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
13935 if (complain & tf_error)
13936 error ("%qT resolves to %qT, which is not an enumeration type",
13937 t, f);
13938 else
13939 return error_mark_node;
13941 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
13943 if (complain & tf_error)
13944 error ("%qT resolves to %qT, which is is not a class type",
13945 t, f);
13946 else
13947 return error_mark_node;
13951 return cp_build_qualified_type_real
13952 (f, cp_type_quals (f) | cp_type_quals (t), complain);
13955 case UNBOUND_CLASS_TEMPLATE:
13957 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
13958 in_decl, /*entering_scope=*/1);
13959 tree name = TYPE_IDENTIFIER (t);
13960 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
13962 if (ctx == error_mark_node || name == error_mark_node)
13963 return error_mark_node;
13965 if (parm_list)
13966 parm_list = tsubst_template_parms (parm_list, args, complain);
13967 return make_unbound_class_template (ctx, name, parm_list, complain);
13970 case TYPEOF_TYPE:
13972 tree type;
13974 ++cp_unevaluated_operand;
13975 ++c_inhibit_evaluation_warnings;
13977 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
13978 complain, in_decl,
13979 /*integral_constant_expression_p=*/false);
13981 --cp_unevaluated_operand;
13982 --c_inhibit_evaluation_warnings;
13984 type = finish_typeof (type);
13985 return cp_build_qualified_type_real (type,
13986 cp_type_quals (t)
13987 | cp_type_quals (type),
13988 complain);
13991 case DECLTYPE_TYPE:
13993 tree type;
13995 ++cp_unevaluated_operand;
13996 ++c_inhibit_evaluation_warnings;
13998 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
13999 complain|tf_decltype, in_decl,
14000 /*function_p*/false,
14001 /*integral_constant_expression*/false);
14003 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14005 if (type == NULL_TREE)
14007 if (complain & tf_error)
14008 error ("empty initializer in lambda init-capture");
14009 type = error_mark_node;
14011 else if (TREE_CODE (type) == TREE_LIST)
14012 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14015 --cp_unevaluated_operand;
14016 --c_inhibit_evaluation_warnings;
14018 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14019 type = lambda_capture_field_type (type,
14020 DECLTYPE_FOR_INIT_CAPTURE (t),
14021 DECLTYPE_FOR_REF_CAPTURE (t));
14022 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14023 type = lambda_proxy_type (type);
14024 else
14026 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14027 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14028 && EXPR_P (type))
14029 /* In a template ~id could be either a complement expression
14030 or an unqualified-id naming a destructor; if instantiating
14031 it produces an expression, it's not an id-expression or
14032 member access. */
14033 id = false;
14034 type = finish_decltype_type (type, id, complain);
14036 return cp_build_qualified_type_real (type,
14037 cp_type_quals (t)
14038 | cp_type_quals (type),
14039 complain | tf_ignore_bad_quals);
14042 case UNDERLYING_TYPE:
14044 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14045 complain, in_decl);
14046 return finish_underlying_type (type);
14049 case TYPE_ARGUMENT_PACK:
14050 case NONTYPE_ARGUMENT_PACK:
14052 tree r;
14054 if (code == NONTYPE_ARGUMENT_PACK)
14055 r = make_node (code);
14056 else
14057 r = cxx_make_type (code);
14059 tree pack_args = ARGUMENT_PACK_ARGS (t);
14060 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14061 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14063 return r;
14066 case VOID_CST:
14067 case INTEGER_CST:
14068 case REAL_CST:
14069 case STRING_CST:
14070 case PLUS_EXPR:
14071 case MINUS_EXPR:
14072 case NEGATE_EXPR:
14073 case NOP_EXPR:
14074 case INDIRECT_REF:
14075 case ADDR_EXPR:
14076 case CALL_EXPR:
14077 case ARRAY_REF:
14078 case SCOPE_REF:
14079 /* We should use one of the expression tsubsts for these codes. */
14080 gcc_unreachable ();
14082 default:
14083 sorry ("use of %qs in template", get_tree_code_name (code));
14084 return error_mark_node;
14088 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14089 type of the expression on the left-hand side of the "." or "->"
14090 operator. */
14092 static tree
14093 tsubst_baselink (tree baselink, tree object_type,
14094 tree args, tsubst_flags_t complain, tree in_decl)
14096 tree name;
14097 tree qualifying_scope;
14098 tree fns;
14099 tree optype;
14100 tree template_args = 0;
14101 bool template_id_p = false;
14102 bool qualified = BASELINK_QUALIFIED_P (baselink);
14104 /* A baselink indicates a function from a base class. Both the
14105 BASELINK_ACCESS_BINFO and the base class referenced may
14106 indicate bases of the template class, rather than the
14107 instantiated class. In addition, lookups that were not
14108 ambiguous before may be ambiguous now. Therefore, we perform
14109 the lookup again. */
14110 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14111 qualifying_scope = tsubst (qualifying_scope, args,
14112 complain, in_decl);
14113 fns = BASELINK_FUNCTIONS (baselink);
14114 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
14115 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14117 template_id_p = true;
14118 template_args = TREE_OPERAND (fns, 1);
14119 fns = TREE_OPERAND (fns, 0);
14120 if (template_args)
14121 template_args = tsubst_template_args (template_args, args,
14122 complain, in_decl);
14124 name = OVL_NAME (fns);
14125 if (IDENTIFIER_TYPENAME_P (name))
14126 name = mangle_conv_op_name_for_type (optype);
14127 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14128 if (!baselink)
14130 if (constructor_name_p (name, qualifying_scope))
14132 if (complain & tf_error)
14133 error ("cannot call constructor %<%T::%D%> directly",
14134 qualifying_scope, name);
14136 return error_mark_node;
14139 /* If lookup found a single function, mark it as used at this
14140 point. (If it lookup found multiple functions the one selected
14141 later by overload resolution will be marked as used at that
14142 point.) */
14143 if (BASELINK_P (baselink))
14144 fns = BASELINK_FUNCTIONS (baselink);
14145 if (!template_id_p && !really_overloaded_fn (fns)
14146 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14147 return error_mark_node;
14149 /* Add back the template arguments, if present. */
14150 if (BASELINK_P (baselink) && template_id_p)
14151 BASELINK_FUNCTIONS (baselink)
14152 = build2 (TEMPLATE_ID_EXPR,
14153 unknown_type_node,
14154 BASELINK_FUNCTIONS (baselink),
14155 template_args);
14156 /* Update the conversion operator type. */
14157 if (BASELINK_P (baselink))
14158 BASELINK_OPTYPE (baselink) = optype;
14160 if (!object_type)
14161 object_type = current_class_type;
14163 if (qualified || name == complete_dtor_identifier)
14165 baselink = adjust_result_of_qualified_name_lookup (baselink,
14166 qualifying_scope,
14167 object_type);
14168 if (!qualified)
14169 /* We need to call adjust_result_of_qualified_name_lookup in case the
14170 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14171 so that we still get virtual function binding. */
14172 BASELINK_QUALIFIED_P (baselink) = false;
14174 return baselink;
14177 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14178 true if the qualified-id will be a postfix-expression in-and-of
14179 itself; false if more of the postfix-expression follows the
14180 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14181 of "&". */
14183 static tree
14184 tsubst_qualified_id (tree qualified_id, tree args,
14185 tsubst_flags_t complain, tree in_decl,
14186 bool done, bool address_p)
14188 tree expr;
14189 tree scope;
14190 tree name;
14191 bool is_template;
14192 tree template_args;
14193 location_t loc = UNKNOWN_LOCATION;
14195 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14197 /* Figure out what name to look up. */
14198 name = TREE_OPERAND (qualified_id, 1);
14199 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14201 is_template = true;
14202 loc = EXPR_LOCATION (name);
14203 template_args = TREE_OPERAND (name, 1);
14204 if (template_args)
14205 template_args = tsubst_template_args (template_args, args,
14206 complain, in_decl);
14207 if (template_args == error_mark_node)
14208 return error_mark_node;
14209 name = TREE_OPERAND (name, 0);
14211 else
14213 is_template = false;
14214 template_args = NULL_TREE;
14217 /* Substitute into the qualifying scope. When there are no ARGS, we
14218 are just trying to simplify a non-dependent expression. In that
14219 case the qualifying scope may be dependent, and, in any case,
14220 substituting will not help. */
14221 scope = TREE_OPERAND (qualified_id, 0);
14222 if (args)
14224 scope = tsubst (scope, args, complain, in_decl);
14225 expr = tsubst_copy (name, args, complain, in_decl);
14227 else
14228 expr = name;
14230 if (dependent_scope_p (scope))
14232 if (is_template)
14233 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14234 tree r = build_qualified_name (NULL_TREE, scope, expr,
14235 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14236 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14237 return r;
14240 if (!BASELINK_P (name) && !DECL_P (expr))
14242 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14244 /* A BIT_NOT_EXPR is used to represent a destructor. */
14245 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14247 error ("qualifying type %qT does not match destructor name ~%qT",
14248 scope, TREE_OPERAND (expr, 0));
14249 expr = error_mark_node;
14251 else
14252 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14253 /*is_type_p=*/0, false);
14255 else
14256 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14257 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14258 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14260 if (complain & tf_error)
14262 error ("dependent-name %qE is parsed as a non-type, but "
14263 "instantiation yields a type", qualified_id);
14264 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14266 return error_mark_node;
14270 if (DECL_P (expr))
14272 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14273 scope);
14274 /* Remember that there was a reference to this entity. */
14275 if (!mark_used (expr, complain) && !(complain & tf_error))
14276 return error_mark_node;
14279 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14281 if (complain & tf_error)
14282 qualified_name_lookup_error (scope,
14283 TREE_OPERAND (qualified_id, 1),
14284 expr, input_location);
14285 return error_mark_node;
14288 if (is_template)
14290 if (variable_template_p (expr))
14291 expr = lookup_and_finish_template_variable (expr, template_args,
14292 complain);
14293 else
14294 expr = lookup_template_function (expr, template_args);
14297 if (expr == error_mark_node && complain & tf_error)
14298 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14299 expr, input_location);
14300 else if (TYPE_P (scope))
14302 expr = (adjust_result_of_qualified_name_lookup
14303 (expr, scope, current_nonlambda_class_type ()));
14304 expr = (finish_qualified_id_expr
14305 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14306 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14307 /*template_arg_p=*/false, complain));
14310 /* Expressions do not generally have reference type. */
14311 if (TREE_CODE (expr) != SCOPE_REF
14312 /* However, if we're about to form a pointer-to-member, we just
14313 want the referenced member referenced. */
14314 && TREE_CODE (expr) != OFFSET_REF)
14315 expr = convert_from_reference (expr);
14317 if (REF_PARENTHESIZED_P (qualified_id))
14318 expr = force_paren_expr (expr);
14320 return expr;
14323 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14324 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14325 for tsubst. */
14327 static tree
14328 tsubst_init (tree init, tree decl, tree args,
14329 tsubst_flags_t complain, tree in_decl)
14331 if (!init)
14332 return NULL_TREE;
14334 init = tsubst_expr (init, args, complain, in_decl, false);
14336 if (!init && TREE_TYPE (decl) != error_mark_node)
14338 /* If we had an initializer but it
14339 instantiated to nothing,
14340 value-initialize the object. This will
14341 only occur when the initializer was a
14342 pack expansion where the parameter packs
14343 used in that expansion were of length
14344 zero. */
14345 init = build_value_init (TREE_TYPE (decl),
14346 complain);
14347 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14348 init = get_target_expr_sfinae (init, complain);
14349 if (TREE_CODE (init) == TARGET_EXPR)
14350 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14353 return init;
14356 /* Like tsubst, but deals with expressions. This function just replaces
14357 template parms; to finish processing the resultant expression, use
14358 tsubst_copy_and_build or tsubst_expr. */
14360 static tree
14361 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14363 enum tree_code code;
14364 tree r;
14366 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14367 return t;
14369 code = TREE_CODE (t);
14371 switch (code)
14373 case PARM_DECL:
14374 r = retrieve_local_specialization (t);
14376 if (r == NULL_TREE)
14378 /* We get here for a use of 'this' in an NSDMI as part of a
14379 constructor call or as part of an aggregate initialization. */
14380 if (DECL_NAME (t) == this_identifier
14381 && ((current_function_decl
14382 && DECL_CONSTRUCTOR_P (current_function_decl))
14383 || (current_class_ref
14384 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14385 return current_class_ptr;
14387 /* This can happen for a parameter name used later in a function
14388 declaration (such as in a late-specified return type). Just
14389 make a dummy decl, since it's only used for its type. */
14390 gcc_assert (cp_unevaluated_operand != 0);
14391 r = tsubst_decl (t, args, complain);
14392 /* Give it the template pattern as its context; its true context
14393 hasn't been instantiated yet and this is good enough for
14394 mangling. */
14395 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14398 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14399 r = ARGUMENT_PACK_SELECT_ARG (r);
14400 if (!mark_used (r, complain) && !(complain & tf_error))
14401 return error_mark_node;
14402 return r;
14404 case CONST_DECL:
14406 tree enum_type;
14407 tree v;
14409 if (DECL_TEMPLATE_PARM_P (t))
14410 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14411 /* There is no need to substitute into namespace-scope
14412 enumerators. */
14413 if (DECL_NAMESPACE_SCOPE_P (t))
14414 return t;
14415 /* If ARGS is NULL, then T is known to be non-dependent. */
14416 if (args == NULL_TREE)
14417 return scalar_constant_value (t);
14419 /* Unfortunately, we cannot just call lookup_name here.
14420 Consider:
14422 template <int I> int f() {
14423 enum E { a = I };
14424 struct S { void g() { E e = a; } };
14427 When we instantiate f<7>::S::g(), say, lookup_name is not
14428 clever enough to find f<7>::a. */
14429 enum_type
14430 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14431 /*entering_scope=*/0);
14433 for (v = TYPE_VALUES (enum_type);
14434 v != NULL_TREE;
14435 v = TREE_CHAIN (v))
14436 if (TREE_PURPOSE (v) == DECL_NAME (t))
14437 return TREE_VALUE (v);
14439 /* We didn't find the name. That should never happen; if
14440 name-lookup found it during preliminary parsing, we
14441 should find it again here during instantiation. */
14442 gcc_unreachable ();
14444 return t;
14446 case FIELD_DECL:
14447 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14449 /* Check for a local specialization set up by
14450 tsubst_pack_expansion. */
14451 if (tree r = retrieve_local_specialization (t))
14453 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14454 r = ARGUMENT_PACK_SELECT_ARG (r);
14455 return r;
14458 /* When retrieving a capture pack from a generic lambda, remove the
14459 lambda call op's own template argument list from ARGS. Only the
14460 template arguments active for the closure type should be used to
14461 retrieve the pack specialization. */
14462 if (LAMBDA_FUNCTION_P (current_function_decl)
14463 && (template_class_depth (DECL_CONTEXT (t))
14464 != TMPL_ARGS_DEPTH (args)))
14465 args = strip_innermost_template_args (args, 1);
14467 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14468 tsubst_decl put in the hash table. */
14469 return retrieve_specialization (t, args, 0);
14472 if (DECL_CONTEXT (t))
14474 tree ctx;
14476 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14477 /*entering_scope=*/1);
14478 if (ctx != DECL_CONTEXT (t))
14480 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14481 if (!r)
14483 if (complain & tf_error)
14484 error ("using invalid field %qD", t);
14485 return error_mark_node;
14487 return r;
14491 return t;
14493 case VAR_DECL:
14494 case FUNCTION_DECL:
14495 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14496 r = tsubst (t, args, complain, in_decl);
14497 else if (local_variable_p (t)
14498 && uses_template_parms (DECL_CONTEXT (t)))
14500 r = retrieve_local_specialization (t);
14501 if (r == NULL_TREE)
14503 /* First try name lookup to find the instantiation. */
14504 r = lookup_name (DECL_NAME (t));
14505 if (r && !is_capture_proxy (r))
14507 /* Make sure that the one we found is the one we want. */
14508 tree ctx = DECL_CONTEXT (t);
14509 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14510 ctx = tsubst (ctx, args, complain, in_decl);
14511 if (ctx != DECL_CONTEXT (r))
14512 r = NULL_TREE;
14515 if (r)
14516 /* OK */;
14517 else
14519 /* This can happen for a variable used in a
14520 late-specified return type of a local lambda, or for a
14521 local static or constant. Building a new VAR_DECL
14522 should be OK in all those cases. */
14523 r = tsubst_decl (t, args, complain);
14524 if (local_specializations)
14525 /* Avoid infinite recursion (79640). */
14526 register_local_specialization (r, t);
14527 if (decl_maybe_constant_var_p (r))
14529 /* We can't call cp_finish_decl, so handle the
14530 initializer by hand. */
14531 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14532 complain, in_decl);
14533 if (!processing_template_decl)
14534 init = maybe_constant_init (init);
14535 if (processing_template_decl
14536 ? potential_constant_expression (init)
14537 : reduced_constant_expression_p (init))
14538 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14539 = TREE_CONSTANT (r) = true;
14540 DECL_INITIAL (r) = init;
14542 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14543 || decl_constant_var_p (r)
14544 || errorcount || sorrycount);
14545 if (!processing_template_decl
14546 && !TREE_STATIC (r))
14547 r = process_outer_var_ref (r, complain);
14549 /* Remember this for subsequent uses. */
14550 if (local_specializations)
14551 register_local_specialization (r, t);
14554 else
14555 r = t;
14556 if (!mark_used (r, complain))
14557 return error_mark_node;
14558 return r;
14560 case NAMESPACE_DECL:
14561 return t;
14563 case OVERLOAD:
14564 /* An OVERLOAD will always be a non-dependent overload set; an
14565 overload set from function scope will just be represented with an
14566 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14567 gcc_assert (!uses_template_parms (t));
14568 /* We must have marked any lookups as persistent. */
14569 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14570 return t;
14572 case BASELINK:
14573 return tsubst_baselink (t, current_nonlambda_class_type (),
14574 args, complain, in_decl);
14576 case TEMPLATE_DECL:
14577 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14578 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14579 args, complain, in_decl);
14580 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14581 return tsubst (t, args, complain, in_decl);
14582 else if (DECL_CLASS_SCOPE_P (t)
14583 && uses_template_parms (DECL_CONTEXT (t)))
14585 /* Template template argument like the following example need
14586 special treatment:
14588 template <template <class> class TT> struct C {};
14589 template <class T> struct D {
14590 template <class U> struct E {};
14591 C<E> c; // #1
14593 D<int> d; // #2
14595 We are processing the template argument `E' in #1 for
14596 the template instantiation #2. Originally, `E' is a
14597 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14598 have to substitute this with one having context `D<int>'. */
14600 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14601 if (dependent_scope_p (context))
14603 /* When rewriting a constructor into a deduction guide, a
14604 non-dependent name can become dependent, so memtmpl<args>
14605 becomes context::template memtmpl<args>. */
14606 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14607 return build_qualified_name (type, context, DECL_NAME (t),
14608 /*template*/true);
14610 return lookup_field (context, DECL_NAME(t), 0, false);
14612 else
14613 /* Ordinary template template argument. */
14614 return t;
14616 case CAST_EXPR:
14617 case REINTERPRET_CAST_EXPR:
14618 case CONST_CAST_EXPR:
14619 case STATIC_CAST_EXPR:
14620 case DYNAMIC_CAST_EXPR:
14621 case IMPLICIT_CONV_EXPR:
14622 case CONVERT_EXPR:
14623 case NOP_EXPR:
14625 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14626 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14627 return build1 (code, type, op0);
14630 case SIZEOF_EXPR:
14631 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14632 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14634 tree expanded, op = TREE_OPERAND (t, 0);
14635 int len = 0;
14637 if (SIZEOF_EXPR_TYPE_P (t))
14638 op = TREE_TYPE (op);
14640 ++cp_unevaluated_operand;
14641 ++c_inhibit_evaluation_warnings;
14642 /* We only want to compute the number of arguments. */
14643 if (PACK_EXPANSION_P (op))
14644 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14645 else
14646 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14647 args, complain, in_decl);
14648 --cp_unevaluated_operand;
14649 --c_inhibit_evaluation_warnings;
14651 if (TREE_CODE (expanded) == TREE_VEC)
14653 len = TREE_VEC_LENGTH (expanded);
14654 /* Set TREE_USED for the benefit of -Wunused. */
14655 for (int i = 0; i < len; i++)
14656 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14657 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14660 if (expanded == error_mark_node)
14661 return error_mark_node;
14662 else if (PACK_EXPANSION_P (expanded)
14663 || (TREE_CODE (expanded) == TREE_VEC
14664 && pack_expansion_args_count (expanded)))
14667 if (PACK_EXPANSION_P (expanded))
14668 /* OK. */;
14669 else if (TREE_VEC_LENGTH (expanded) == 1)
14670 expanded = TREE_VEC_ELT (expanded, 0);
14671 else
14672 expanded = make_argument_pack (expanded);
14674 if (TYPE_P (expanded))
14675 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14676 complain & tf_error);
14677 else
14678 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14679 complain & tf_error);
14681 else
14682 return build_int_cst (size_type_node, len);
14684 if (SIZEOF_EXPR_TYPE_P (t))
14686 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14687 args, complain, in_decl);
14688 r = build1 (NOP_EXPR, r, error_mark_node);
14689 r = build1 (SIZEOF_EXPR,
14690 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14691 SIZEOF_EXPR_TYPE_P (r) = 1;
14692 return r;
14694 /* Fall through */
14696 case INDIRECT_REF:
14697 case NEGATE_EXPR:
14698 case TRUTH_NOT_EXPR:
14699 case BIT_NOT_EXPR:
14700 case ADDR_EXPR:
14701 case UNARY_PLUS_EXPR: /* Unary + */
14702 case ALIGNOF_EXPR:
14703 case AT_ENCODE_EXPR:
14704 case ARROW_EXPR:
14705 case THROW_EXPR:
14706 case TYPEID_EXPR:
14707 case REALPART_EXPR:
14708 case IMAGPART_EXPR:
14709 case PAREN_EXPR:
14711 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14712 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14713 return build1 (code, type, op0);
14716 case COMPONENT_REF:
14718 tree object;
14719 tree name;
14721 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14722 name = TREE_OPERAND (t, 1);
14723 if (TREE_CODE (name) == BIT_NOT_EXPR)
14725 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14726 complain, in_decl);
14727 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14729 else if (TREE_CODE (name) == SCOPE_REF
14730 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14732 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14733 complain, in_decl);
14734 name = TREE_OPERAND (name, 1);
14735 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14736 complain, in_decl);
14737 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14738 name = build_qualified_name (/*type=*/NULL_TREE,
14739 base, name,
14740 /*template_p=*/false);
14742 else if (BASELINK_P (name))
14743 name = tsubst_baselink (name,
14744 non_reference (TREE_TYPE (object)),
14745 args, complain,
14746 in_decl);
14747 else
14748 name = tsubst_copy (name, args, complain, in_decl);
14749 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14752 case PLUS_EXPR:
14753 case MINUS_EXPR:
14754 case MULT_EXPR:
14755 case TRUNC_DIV_EXPR:
14756 case CEIL_DIV_EXPR:
14757 case FLOOR_DIV_EXPR:
14758 case ROUND_DIV_EXPR:
14759 case EXACT_DIV_EXPR:
14760 case BIT_AND_EXPR:
14761 case BIT_IOR_EXPR:
14762 case BIT_XOR_EXPR:
14763 case TRUNC_MOD_EXPR:
14764 case FLOOR_MOD_EXPR:
14765 case TRUTH_ANDIF_EXPR:
14766 case TRUTH_ORIF_EXPR:
14767 case TRUTH_AND_EXPR:
14768 case TRUTH_OR_EXPR:
14769 case RSHIFT_EXPR:
14770 case LSHIFT_EXPR:
14771 case RROTATE_EXPR:
14772 case LROTATE_EXPR:
14773 case EQ_EXPR:
14774 case NE_EXPR:
14775 case MAX_EXPR:
14776 case MIN_EXPR:
14777 case LE_EXPR:
14778 case GE_EXPR:
14779 case LT_EXPR:
14780 case GT_EXPR:
14781 case COMPOUND_EXPR:
14782 case DOTSTAR_EXPR:
14783 case MEMBER_REF:
14784 case PREDECREMENT_EXPR:
14785 case PREINCREMENT_EXPR:
14786 case POSTDECREMENT_EXPR:
14787 case POSTINCREMENT_EXPR:
14789 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14790 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14791 return build_nt (code, op0, op1);
14794 case SCOPE_REF:
14796 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14797 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14798 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14799 QUALIFIED_NAME_IS_TEMPLATE (t));
14802 case ARRAY_REF:
14804 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14805 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14806 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14809 case CALL_EXPR:
14811 int n = VL_EXP_OPERAND_LENGTH (t);
14812 tree result = build_vl_exp (CALL_EXPR, n);
14813 int i;
14814 for (i = 0; i < n; i++)
14815 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14816 complain, in_decl);
14817 return result;
14820 case COND_EXPR:
14821 case MODOP_EXPR:
14822 case PSEUDO_DTOR_EXPR:
14823 case VEC_PERM_EXPR:
14825 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14826 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14827 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14828 r = build_nt (code, op0, op1, op2);
14829 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14830 return r;
14833 case NEW_EXPR:
14835 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14836 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14837 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14838 r = build_nt (code, op0, op1, op2);
14839 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14840 return r;
14843 case DELETE_EXPR:
14845 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14846 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14847 r = build_nt (code, op0, op1);
14848 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14849 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14850 return r;
14853 case TEMPLATE_ID_EXPR:
14855 /* Substituted template arguments */
14856 tree fn = TREE_OPERAND (t, 0);
14857 tree targs = TREE_OPERAND (t, 1);
14859 fn = tsubst_copy (fn, args, complain, in_decl);
14860 if (targs)
14861 targs = tsubst_template_args (targs, args, complain, in_decl);
14863 return lookup_template_function (fn, targs);
14866 case TREE_LIST:
14868 tree purpose, value, chain;
14870 if (t == void_list_node)
14871 return t;
14873 purpose = TREE_PURPOSE (t);
14874 if (purpose)
14875 purpose = tsubst_copy (purpose, args, complain, in_decl);
14876 value = TREE_VALUE (t);
14877 if (value)
14878 value = tsubst_copy (value, args, complain, in_decl);
14879 chain = TREE_CHAIN (t);
14880 if (chain && chain != void_type_node)
14881 chain = tsubst_copy (chain, args, complain, in_decl);
14882 if (purpose == TREE_PURPOSE (t)
14883 && value == TREE_VALUE (t)
14884 && chain == TREE_CHAIN (t))
14885 return t;
14886 return tree_cons (purpose, value, chain);
14889 case RECORD_TYPE:
14890 case UNION_TYPE:
14891 case ENUMERAL_TYPE:
14892 case INTEGER_TYPE:
14893 case TEMPLATE_TYPE_PARM:
14894 case TEMPLATE_TEMPLATE_PARM:
14895 case BOUND_TEMPLATE_TEMPLATE_PARM:
14896 case TEMPLATE_PARM_INDEX:
14897 case POINTER_TYPE:
14898 case REFERENCE_TYPE:
14899 case OFFSET_TYPE:
14900 case FUNCTION_TYPE:
14901 case METHOD_TYPE:
14902 case ARRAY_TYPE:
14903 case TYPENAME_TYPE:
14904 case UNBOUND_CLASS_TEMPLATE:
14905 case TYPEOF_TYPE:
14906 case DECLTYPE_TYPE:
14907 case TYPE_DECL:
14908 return tsubst (t, args, complain, in_decl);
14910 case USING_DECL:
14911 t = DECL_NAME (t);
14912 /* Fall through. */
14913 case IDENTIFIER_NODE:
14914 if (IDENTIFIER_TYPENAME_P (t))
14916 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14917 return mangle_conv_op_name_for_type (new_type);
14919 else
14920 return t;
14922 case CONSTRUCTOR:
14923 /* This is handled by tsubst_copy_and_build. */
14924 gcc_unreachable ();
14926 case VA_ARG_EXPR:
14928 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14929 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14930 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
14933 case CLEANUP_POINT_EXPR:
14934 /* We shouldn't have built any of these during initial template
14935 generation. Instead, they should be built during instantiation
14936 in response to the saved STMT_IS_FULL_EXPR_P setting. */
14937 gcc_unreachable ();
14939 case OFFSET_REF:
14941 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14942 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14943 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14944 r = build2 (code, type, op0, op1);
14945 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
14946 if (!mark_used (TREE_OPERAND (r, 1), complain)
14947 && !(complain & tf_error))
14948 return error_mark_node;
14949 return r;
14952 case EXPR_PACK_EXPANSION:
14953 error ("invalid use of pack expansion expression");
14954 return error_mark_node;
14956 case NONTYPE_ARGUMENT_PACK:
14957 error ("use %<...%> to expand argument pack");
14958 return error_mark_node;
14960 case VOID_CST:
14961 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
14962 return t;
14964 case INTEGER_CST:
14965 case REAL_CST:
14966 case STRING_CST:
14967 case COMPLEX_CST:
14969 /* Instantiate any typedefs in the type. */
14970 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14971 r = fold_convert (type, t);
14972 gcc_assert (TREE_CODE (r) == code);
14973 return r;
14976 case PTRMEM_CST:
14977 /* These can sometimes show up in a partial instantiation, but never
14978 involve template parms. */
14979 gcc_assert (!uses_template_parms (t));
14980 return t;
14982 case UNARY_LEFT_FOLD_EXPR:
14983 return tsubst_unary_left_fold (t, args, complain, in_decl);
14984 case UNARY_RIGHT_FOLD_EXPR:
14985 return tsubst_unary_right_fold (t, args, complain, in_decl);
14986 case BINARY_LEFT_FOLD_EXPR:
14987 return tsubst_binary_left_fold (t, args, complain, in_decl);
14988 case BINARY_RIGHT_FOLD_EXPR:
14989 return tsubst_binary_right_fold (t, args, complain, in_decl);
14991 default:
14992 /* We shouldn't get here, but keep going if !flag_checking. */
14993 if (flag_checking)
14994 gcc_unreachable ();
14995 return t;
14999 /* Helper function for tsubst_omp_clauses, used for instantiation of
15000 OMP_CLAUSE_DECL of clauses. */
15002 static tree
15003 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15004 tree in_decl)
15006 if (decl == NULL_TREE)
15007 return NULL_TREE;
15009 /* Handle an OpenMP array section represented as a TREE_LIST (or
15010 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15011 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15012 TREE_LIST. We can handle it exactly the same as an array section
15013 (purpose, value, and a chain), even though the nomenclature
15014 (low_bound, length, etc) is different. */
15015 if (TREE_CODE (decl) == TREE_LIST)
15017 tree low_bound
15018 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15019 /*integral_constant_expression_p=*/false);
15020 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15021 /*integral_constant_expression_p=*/false);
15022 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15023 in_decl);
15024 if (TREE_PURPOSE (decl) == low_bound
15025 && TREE_VALUE (decl) == length
15026 && TREE_CHAIN (decl) == chain)
15027 return decl;
15028 tree ret = tree_cons (low_bound, length, chain);
15029 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15030 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15031 return ret;
15033 tree ret = tsubst_expr (decl, args, complain, in_decl,
15034 /*integral_constant_expression_p=*/false);
15035 /* Undo convert_from_reference tsubst_expr could have called. */
15036 if (decl
15037 && REFERENCE_REF_P (ret)
15038 && !REFERENCE_REF_P (decl))
15039 ret = TREE_OPERAND (ret, 0);
15040 return ret;
15043 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15045 static tree
15046 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15047 tree args, tsubst_flags_t complain, tree in_decl)
15049 tree new_clauses = NULL_TREE, nc, oc;
15050 tree linear_no_step = NULL_TREE;
15052 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15054 nc = copy_node (oc);
15055 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15056 new_clauses = nc;
15058 switch (OMP_CLAUSE_CODE (nc))
15060 case OMP_CLAUSE_LASTPRIVATE:
15061 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15063 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15064 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15065 in_decl, /*integral_constant_expression_p=*/false);
15066 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15067 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15069 /* FALLTHRU */
15070 case OMP_CLAUSE_PRIVATE:
15071 case OMP_CLAUSE_SHARED:
15072 case OMP_CLAUSE_FIRSTPRIVATE:
15073 case OMP_CLAUSE_COPYIN:
15074 case OMP_CLAUSE_COPYPRIVATE:
15075 case OMP_CLAUSE_UNIFORM:
15076 case OMP_CLAUSE_DEPEND:
15077 case OMP_CLAUSE_FROM:
15078 case OMP_CLAUSE_TO:
15079 case OMP_CLAUSE_MAP:
15080 case OMP_CLAUSE_USE_DEVICE_PTR:
15081 case OMP_CLAUSE_IS_DEVICE_PTR:
15082 OMP_CLAUSE_DECL (nc)
15083 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15084 in_decl);
15085 break;
15086 case OMP_CLAUSE_TILE:
15087 case OMP_CLAUSE_IF:
15088 case OMP_CLAUSE_NUM_THREADS:
15089 case OMP_CLAUSE_SCHEDULE:
15090 case OMP_CLAUSE_COLLAPSE:
15091 case OMP_CLAUSE_FINAL:
15092 case OMP_CLAUSE_DEVICE:
15093 case OMP_CLAUSE_DIST_SCHEDULE:
15094 case OMP_CLAUSE_NUM_TEAMS:
15095 case OMP_CLAUSE_THREAD_LIMIT:
15096 case OMP_CLAUSE_SAFELEN:
15097 case OMP_CLAUSE_SIMDLEN:
15098 case OMP_CLAUSE_NUM_TASKS:
15099 case OMP_CLAUSE_GRAINSIZE:
15100 case OMP_CLAUSE_PRIORITY:
15101 case OMP_CLAUSE_ORDERED:
15102 case OMP_CLAUSE_HINT:
15103 case OMP_CLAUSE_NUM_GANGS:
15104 case OMP_CLAUSE_NUM_WORKERS:
15105 case OMP_CLAUSE_VECTOR_LENGTH:
15106 case OMP_CLAUSE_WORKER:
15107 case OMP_CLAUSE_VECTOR:
15108 case OMP_CLAUSE_ASYNC:
15109 case OMP_CLAUSE_WAIT:
15110 OMP_CLAUSE_OPERAND (nc, 0)
15111 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15112 in_decl, /*integral_constant_expression_p=*/false);
15113 break;
15114 case OMP_CLAUSE_REDUCTION:
15115 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15117 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15118 if (TREE_CODE (placeholder) == SCOPE_REF)
15120 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15121 complain, in_decl);
15122 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15123 = build_qualified_name (NULL_TREE, scope,
15124 TREE_OPERAND (placeholder, 1),
15125 false);
15127 else
15128 gcc_assert (identifier_p (placeholder));
15130 OMP_CLAUSE_DECL (nc)
15131 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15132 in_decl);
15133 break;
15134 case OMP_CLAUSE_GANG:
15135 case OMP_CLAUSE_ALIGNED:
15136 OMP_CLAUSE_DECL (nc)
15137 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15138 in_decl);
15139 OMP_CLAUSE_OPERAND (nc, 1)
15140 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15141 in_decl, /*integral_constant_expression_p=*/false);
15142 break;
15143 case OMP_CLAUSE_LINEAR:
15144 OMP_CLAUSE_DECL (nc)
15145 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15146 in_decl);
15147 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15149 gcc_assert (!linear_no_step);
15150 linear_no_step = nc;
15152 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15153 OMP_CLAUSE_LINEAR_STEP (nc)
15154 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15155 complain, in_decl);
15156 else
15157 OMP_CLAUSE_LINEAR_STEP (nc)
15158 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15159 in_decl,
15160 /*integral_constant_expression_p=*/false);
15161 break;
15162 case OMP_CLAUSE_NOWAIT:
15163 case OMP_CLAUSE_DEFAULT:
15164 case OMP_CLAUSE_UNTIED:
15165 case OMP_CLAUSE_MERGEABLE:
15166 case OMP_CLAUSE_INBRANCH:
15167 case OMP_CLAUSE_NOTINBRANCH:
15168 case OMP_CLAUSE_PROC_BIND:
15169 case OMP_CLAUSE_FOR:
15170 case OMP_CLAUSE_PARALLEL:
15171 case OMP_CLAUSE_SECTIONS:
15172 case OMP_CLAUSE_TASKGROUP:
15173 case OMP_CLAUSE_NOGROUP:
15174 case OMP_CLAUSE_THREADS:
15175 case OMP_CLAUSE_SIMD:
15176 case OMP_CLAUSE_DEFAULTMAP:
15177 case OMP_CLAUSE_INDEPENDENT:
15178 case OMP_CLAUSE_AUTO:
15179 case OMP_CLAUSE_SEQ:
15180 break;
15181 default:
15182 gcc_unreachable ();
15184 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15185 switch (OMP_CLAUSE_CODE (nc))
15187 case OMP_CLAUSE_SHARED:
15188 case OMP_CLAUSE_PRIVATE:
15189 case OMP_CLAUSE_FIRSTPRIVATE:
15190 case OMP_CLAUSE_LASTPRIVATE:
15191 case OMP_CLAUSE_COPYPRIVATE:
15192 case OMP_CLAUSE_LINEAR:
15193 case OMP_CLAUSE_REDUCTION:
15194 case OMP_CLAUSE_USE_DEVICE_PTR:
15195 case OMP_CLAUSE_IS_DEVICE_PTR:
15196 /* tsubst_expr on SCOPE_REF results in returning
15197 finish_non_static_data_member result. Undo that here. */
15198 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15199 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15200 == IDENTIFIER_NODE))
15202 tree t = OMP_CLAUSE_DECL (nc);
15203 tree v = t;
15204 while (v)
15205 switch (TREE_CODE (v))
15207 case COMPONENT_REF:
15208 case MEM_REF:
15209 case INDIRECT_REF:
15210 CASE_CONVERT:
15211 case POINTER_PLUS_EXPR:
15212 v = TREE_OPERAND (v, 0);
15213 continue;
15214 case PARM_DECL:
15215 if (DECL_CONTEXT (v) == current_function_decl
15216 && DECL_ARTIFICIAL (v)
15217 && DECL_NAME (v) == this_identifier)
15218 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15219 /* FALLTHRU */
15220 default:
15221 v = NULL_TREE;
15222 break;
15225 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15226 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15227 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15228 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15229 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15231 tree decl = OMP_CLAUSE_DECL (nc);
15232 if (VAR_P (decl))
15234 retrofit_lang_decl (decl);
15235 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15238 break;
15239 default:
15240 break;
15244 new_clauses = nreverse (new_clauses);
15245 if (ort != C_ORT_OMP_DECLARE_SIMD)
15247 new_clauses = finish_omp_clauses (new_clauses, ort);
15248 if (linear_no_step)
15249 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15250 if (nc == linear_no_step)
15252 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15253 break;
15256 return new_clauses;
15259 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15261 static tree
15262 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15263 tree in_decl)
15265 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15267 tree purpose, value, chain;
15269 if (t == NULL)
15270 return t;
15272 if (TREE_CODE (t) != TREE_LIST)
15273 return tsubst_copy_and_build (t, args, complain, in_decl,
15274 /*function_p=*/false,
15275 /*integral_constant_expression_p=*/false);
15277 if (t == void_list_node)
15278 return t;
15280 purpose = TREE_PURPOSE (t);
15281 if (purpose)
15282 purpose = RECUR (purpose);
15283 value = TREE_VALUE (t);
15284 if (value)
15286 if (TREE_CODE (value) != LABEL_DECL)
15287 value = RECUR (value);
15288 else
15290 value = lookup_label (DECL_NAME (value));
15291 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15292 TREE_USED (value) = 1;
15295 chain = TREE_CHAIN (t);
15296 if (chain && chain != void_type_node)
15297 chain = RECUR (chain);
15298 return tree_cons (purpose, value, chain);
15299 #undef RECUR
15302 /* Used to temporarily communicate the list of #pragma omp parallel
15303 clauses to #pragma omp for instantiation if they are combined
15304 together. */
15306 static tree *omp_parallel_combined_clauses;
15308 /* Substitute one OMP_FOR iterator. */
15310 static void
15311 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15312 tree initv, tree condv, tree incrv, tree *clauses,
15313 tree args, tsubst_flags_t complain, tree in_decl,
15314 bool integral_constant_expression_p)
15316 #define RECUR(NODE) \
15317 tsubst_expr ((NODE), args, complain, in_decl, \
15318 integral_constant_expression_p)
15319 tree decl, init, cond, incr;
15321 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15322 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15324 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15326 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15327 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15330 decl = TREE_OPERAND (init, 0);
15331 init = TREE_OPERAND (init, 1);
15332 tree decl_expr = NULL_TREE;
15333 if (init && TREE_CODE (init) == DECL_EXPR)
15335 /* We need to jump through some hoops to handle declarations in the
15336 init-statement, since we might need to handle auto deduction,
15337 but we need to keep control of initialization. */
15338 decl_expr = init;
15339 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15340 decl = tsubst_decl (decl, args, complain);
15342 else
15344 if (TREE_CODE (decl) == SCOPE_REF)
15346 decl = RECUR (decl);
15347 if (TREE_CODE (decl) == COMPONENT_REF)
15349 tree v = decl;
15350 while (v)
15351 switch (TREE_CODE (v))
15353 case COMPONENT_REF:
15354 case MEM_REF:
15355 case INDIRECT_REF:
15356 CASE_CONVERT:
15357 case POINTER_PLUS_EXPR:
15358 v = TREE_OPERAND (v, 0);
15359 continue;
15360 case PARM_DECL:
15361 if (DECL_CONTEXT (v) == current_function_decl
15362 && DECL_ARTIFICIAL (v)
15363 && DECL_NAME (v) == this_identifier)
15365 decl = TREE_OPERAND (decl, 1);
15366 decl = omp_privatize_field (decl, false);
15368 /* FALLTHRU */
15369 default:
15370 v = NULL_TREE;
15371 break;
15375 else
15376 decl = RECUR (decl);
15378 init = RECUR (init);
15380 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15381 if (auto_node && init)
15382 TREE_TYPE (decl)
15383 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15385 gcc_assert (!type_dependent_expression_p (decl));
15387 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15389 if (decl_expr)
15391 /* Declare the variable, but don't let that initialize it. */
15392 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15393 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15394 RECUR (decl_expr);
15395 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15398 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15399 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15400 if (TREE_CODE (incr) == MODIFY_EXPR)
15402 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15403 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15404 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15405 NOP_EXPR, rhs, complain);
15407 else
15408 incr = RECUR (incr);
15409 TREE_VEC_ELT (declv, i) = decl;
15410 TREE_VEC_ELT (initv, i) = init;
15411 TREE_VEC_ELT (condv, i) = cond;
15412 TREE_VEC_ELT (incrv, i) = incr;
15413 return;
15416 if (decl_expr)
15418 /* Declare and initialize the variable. */
15419 RECUR (decl_expr);
15420 init = NULL_TREE;
15422 else if (init)
15424 tree *pc;
15425 int j;
15426 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15428 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15430 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15431 && OMP_CLAUSE_DECL (*pc) == decl)
15432 break;
15433 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15434 && OMP_CLAUSE_DECL (*pc) == decl)
15436 if (j)
15437 break;
15438 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15439 tree c = *pc;
15440 *pc = OMP_CLAUSE_CHAIN (c);
15441 OMP_CLAUSE_CHAIN (c) = *clauses;
15442 *clauses = c;
15444 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15445 && OMP_CLAUSE_DECL (*pc) == decl)
15447 error ("iteration variable %qD should not be firstprivate",
15448 decl);
15449 *pc = OMP_CLAUSE_CHAIN (*pc);
15451 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15452 && OMP_CLAUSE_DECL (*pc) == decl)
15454 error ("iteration variable %qD should not be reduction",
15455 decl);
15456 *pc = OMP_CLAUSE_CHAIN (*pc);
15458 else
15459 pc = &OMP_CLAUSE_CHAIN (*pc);
15461 if (*pc)
15462 break;
15464 if (*pc == NULL_TREE)
15466 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15467 OMP_CLAUSE_DECL (c) = decl;
15468 c = finish_omp_clauses (c, C_ORT_OMP);
15469 if (c)
15471 OMP_CLAUSE_CHAIN (c) = *clauses;
15472 *clauses = c;
15476 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15477 if (COMPARISON_CLASS_P (cond))
15479 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15480 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15481 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15483 else
15484 cond = RECUR (cond);
15485 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15486 switch (TREE_CODE (incr))
15488 case PREINCREMENT_EXPR:
15489 case PREDECREMENT_EXPR:
15490 case POSTINCREMENT_EXPR:
15491 case POSTDECREMENT_EXPR:
15492 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15493 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15494 break;
15495 case MODIFY_EXPR:
15496 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15497 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15499 tree rhs = TREE_OPERAND (incr, 1);
15500 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15501 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15502 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15503 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15504 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15505 rhs0, rhs1));
15507 else
15508 incr = RECUR (incr);
15509 break;
15510 case MODOP_EXPR:
15511 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15512 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15514 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15515 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15516 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15517 TREE_TYPE (decl), lhs,
15518 RECUR (TREE_OPERAND (incr, 2))));
15520 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15521 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15522 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15524 tree rhs = TREE_OPERAND (incr, 2);
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 default:
15536 incr = RECUR (incr);
15537 break;
15540 TREE_VEC_ELT (declv, i) = decl;
15541 TREE_VEC_ELT (initv, i) = init;
15542 TREE_VEC_ELT (condv, i) = cond;
15543 TREE_VEC_ELT (incrv, i) = incr;
15544 #undef RECUR
15547 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15548 of OMP_TARGET's body. */
15550 static tree
15551 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15553 *walk_subtrees = 0;
15554 switch (TREE_CODE (*tp))
15556 case OMP_TEAMS:
15557 return *tp;
15558 case BIND_EXPR:
15559 case STATEMENT_LIST:
15560 *walk_subtrees = 1;
15561 break;
15562 default:
15563 break;
15565 return NULL_TREE;
15568 /* Helper function for tsubst_expr. For decomposition declaration
15569 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15570 also the corresponding decls representing the identifiers
15571 of the decomposition declaration. Return DECL if successful
15572 or error_mark_node otherwise, set *FIRST to the first decl
15573 in the list chained through DECL_CHAIN and *CNT to the number
15574 of such decls. */
15576 static tree
15577 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15578 tsubst_flags_t complain, tree in_decl, tree *first,
15579 unsigned int *cnt)
15581 tree decl2, decl3, prev = decl;
15582 *cnt = 0;
15583 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15584 for (decl2 = DECL_CHAIN (pattern_decl);
15585 decl2
15586 && VAR_P (decl2)
15587 && DECL_DECOMPOSITION_P (decl2)
15588 && DECL_NAME (decl2);
15589 decl2 = DECL_CHAIN (decl2))
15591 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15593 gcc_assert (errorcount);
15594 return error_mark_node;
15596 (*cnt)++;
15597 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15598 tree v = DECL_VALUE_EXPR (decl2);
15599 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15600 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15601 decl3 = tsubst (decl2, args, complain, in_decl);
15602 SET_DECL_VALUE_EXPR (decl2, v);
15603 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15604 if (VAR_P (decl3))
15605 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15606 maybe_push_decl (decl3);
15607 if (error_operand_p (decl3))
15608 decl = error_mark_node;
15609 else if (decl != error_mark_node
15610 && DECL_CHAIN (decl3) != prev)
15612 gcc_assert (errorcount);
15613 decl = error_mark_node;
15615 else
15616 prev = decl3;
15618 *first = prev;
15619 return decl;
15622 /* Like tsubst_copy for expressions, etc. but also does semantic
15623 processing. */
15625 tree
15626 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15627 bool integral_constant_expression_p)
15629 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15630 #define RECUR(NODE) \
15631 tsubst_expr ((NODE), args, complain, in_decl, \
15632 integral_constant_expression_p)
15634 tree stmt, tmp;
15635 tree r;
15636 location_t loc;
15638 if (t == NULL_TREE || t == error_mark_node)
15639 return t;
15641 loc = input_location;
15642 if (EXPR_HAS_LOCATION (t))
15643 input_location = EXPR_LOCATION (t);
15644 if (STATEMENT_CODE_P (TREE_CODE (t)))
15645 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15647 switch (TREE_CODE (t))
15649 case STATEMENT_LIST:
15651 tree_stmt_iterator i;
15652 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15653 RECUR (tsi_stmt (i));
15654 break;
15657 case CTOR_INITIALIZER:
15658 finish_mem_initializers (tsubst_initializer_list
15659 (TREE_OPERAND (t, 0), args));
15660 break;
15662 case RETURN_EXPR:
15663 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15664 break;
15666 case EXPR_STMT:
15667 tmp = RECUR (EXPR_STMT_EXPR (t));
15668 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15669 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15670 else
15671 finish_expr_stmt (tmp);
15672 break;
15674 case USING_STMT:
15675 do_using_directive (USING_STMT_NAMESPACE (t));
15676 break;
15678 case DECL_EXPR:
15680 tree decl, pattern_decl;
15681 tree init;
15683 pattern_decl = decl = DECL_EXPR_DECL (t);
15684 if (TREE_CODE (decl) == LABEL_DECL)
15685 finish_label_decl (DECL_NAME (decl));
15686 else if (TREE_CODE (decl) == USING_DECL)
15688 tree scope = USING_DECL_SCOPE (decl);
15689 tree name = DECL_NAME (decl);
15691 scope = tsubst (scope, args, complain, in_decl);
15692 decl = lookup_qualified_name (scope, name,
15693 /*is_type_p=*/false,
15694 /*complain=*/false);
15695 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15696 qualified_name_lookup_error (scope, name, decl, input_location);
15697 else
15698 do_local_using_decl (decl, scope, name);
15700 else if (DECL_PACK_P (decl))
15702 /* Don't build up decls for a variadic capture proxy, we'll
15703 instantiate the elements directly as needed. */
15704 break;
15706 else
15708 init = DECL_INITIAL (decl);
15709 decl = tsubst (decl, args, complain, in_decl);
15710 if (decl != error_mark_node)
15712 /* By marking the declaration as instantiated, we avoid
15713 trying to instantiate it. Since instantiate_decl can't
15714 handle local variables, and since we've already done
15715 all that needs to be done, that's the right thing to
15716 do. */
15717 if (VAR_P (decl))
15718 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15719 if (VAR_P (decl)
15720 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15721 /* Anonymous aggregates are a special case. */
15722 finish_anon_union (decl);
15723 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15725 DECL_CONTEXT (decl) = current_function_decl;
15726 if (DECL_NAME (decl) == this_identifier)
15728 tree lam = DECL_CONTEXT (current_function_decl);
15729 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15730 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15732 insert_capture_proxy (decl);
15734 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15735 /* We already did a pushtag. */;
15736 else if (TREE_CODE (decl) == FUNCTION_DECL
15737 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15738 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15740 DECL_CONTEXT (decl) = NULL_TREE;
15741 pushdecl (decl);
15742 DECL_CONTEXT (decl) = current_function_decl;
15743 cp_check_omp_declare_reduction (decl);
15745 else
15747 int const_init = false;
15748 maybe_push_decl (decl);
15749 if (VAR_P (decl)
15750 && DECL_PRETTY_FUNCTION_P (decl))
15752 /* For __PRETTY_FUNCTION__ we have to adjust the
15753 initializer. */
15754 const char *const name
15755 = cxx_printable_name (current_function_decl, 2);
15756 init = cp_fname_init (name, &TREE_TYPE (decl));
15758 else
15759 init = tsubst_init (init, decl, args, complain, in_decl);
15761 if (VAR_P (decl))
15762 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15763 (pattern_decl));
15764 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15765 if (VAR_P (decl)
15766 && DECL_DECOMPOSITION_P (decl)
15767 && TREE_TYPE (pattern_decl) != error_mark_node)
15769 unsigned int cnt;
15770 tree first;
15771 decl = tsubst_decomp_names (decl, pattern_decl, args,
15772 complain, in_decl, &first,
15773 &cnt);
15774 if (decl != error_mark_node)
15775 cp_finish_decomp (decl, first, cnt);
15781 break;
15784 case FOR_STMT:
15785 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15786 RECUR (FOR_INIT_STMT (t));
15787 finish_init_stmt (stmt);
15788 tmp = RECUR (FOR_COND (t));
15789 finish_for_cond (tmp, stmt, false);
15790 tmp = RECUR (FOR_EXPR (t));
15791 finish_for_expr (tmp, stmt);
15792 RECUR (FOR_BODY (t));
15793 finish_for_stmt (stmt);
15794 break;
15796 case RANGE_FOR_STMT:
15798 tree decl, expr;
15799 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15800 decl = RANGE_FOR_DECL (t);
15801 decl = tsubst (decl, args, complain, in_decl);
15802 maybe_push_decl (decl);
15803 expr = RECUR (RANGE_FOR_EXPR (t));
15804 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15806 unsigned int cnt;
15807 tree first;
15808 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15809 complain, in_decl, &first, &cnt);
15810 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15811 RANGE_FOR_IVDEP (t));
15813 else
15814 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15815 RANGE_FOR_IVDEP (t));
15816 RECUR (RANGE_FOR_BODY (t));
15817 finish_for_stmt (stmt);
15819 break;
15821 case WHILE_STMT:
15822 stmt = begin_while_stmt ();
15823 tmp = RECUR (WHILE_COND (t));
15824 finish_while_stmt_cond (tmp, stmt, false);
15825 RECUR (WHILE_BODY (t));
15826 finish_while_stmt (stmt);
15827 break;
15829 case DO_STMT:
15830 stmt = begin_do_stmt ();
15831 RECUR (DO_BODY (t));
15832 finish_do_body (stmt);
15833 tmp = RECUR (DO_COND (t));
15834 finish_do_stmt (tmp, stmt, false);
15835 break;
15837 case IF_STMT:
15838 stmt = begin_if_stmt ();
15839 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15840 tmp = RECUR (IF_COND (t));
15841 tmp = finish_if_stmt_cond (tmp, stmt);
15842 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15843 /* Don't instantiate the THEN_CLAUSE. */;
15844 else
15846 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15847 if (inhibit)
15848 ++c_inhibit_evaluation_warnings;
15849 RECUR (THEN_CLAUSE (t));
15850 if (inhibit)
15851 --c_inhibit_evaluation_warnings;
15853 finish_then_clause (stmt);
15855 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15856 /* Don't instantiate the ELSE_CLAUSE. */;
15857 else if (ELSE_CLAUSE (t))
15859 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15860 begin_else_clause (stmt);
15861 if (inhibit)
15862 ++c_inhibit_evaluation_warnings;
15863 RECUR (ELSE_CLAUSE (t));
15864 if (inhibit)
15865 --c_inhibit_evaluation_warnings;
15866 finish_else_clause (stmt);
15869 finish_if_stmt (stmt);
15870 break;
15872 case BIND_EXPR:
15873 if (BIND_EXPR_BODY_BLOCK (t))
15874 stmt = begin_function_body ();
15875 else
15876 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15877 ? BCS_TRY_BLOCK : 0);
15879 RECUR (BIND_EXPR_BODY (t));
15881 if (BIND_EXPR_BODY_BLOCK (t))
15882 finish_function_body (stmt);
15883 else
15884 finish_compound_stmt (stmt);
15885 break;
15887 case BREAK_STMT:
15888 finish_break_stmt ();
15889 break;
15891 case CONTINUE_STMT:
15892 finish_continue_stmt ();
15893 break;
15895 case SWITCH_STMT:
15896 stmt = begin_switch_stmt ();
15897 tmp = RECUR (SWITCH_STMT_COND (t));
15898 finish_switch_cond (tmp, stmt);
15899 RECUR (SWITCH_STMT_BODY (t));
15900 finish_switch_stmt (stmt);
15901 break;
15903 case CASE_LABEL_EXPR:
15905 tree low = RECUR (CASE_LOW (t));
15906 tree high = RECUR (CASE_HIGH (t));
15907 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
15908 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
15909 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
15910 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
15912 break;
15914 case LABEL_EXPR:
15916 tree decl = LABEL_EXPR_LABEL (t);
15917 tree label;
15919 label = finish_label_stmt (DECL_NAME (decl));
15920 if (TREE_CODE (label) == LABEL_DECL)
15921 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
15922 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
15923 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
15925 break;
15927 case GOTO_EXPR:
15928 tmp = GOTO_DESTINATION (t);
15929 if (TREE_CODE (tmp) != LABEL_DECL)
15930 /* Computed goto's must be tsubst'd into. On the other hand,
15931 non-computed gotos must not be; the identifier in question
15932 will have no binding. */
15933 tmp = RECUR (tmp);
15934 else
15935 tmp = DECL_NAME (tmp);
15936 finish_goto_stmt (tmp);
15937 break;
15939 case ASM_EXPR:
15941 tree string = RECUR (ASM_STRING (t));
15942 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
15943 complain, in_decl);
15944 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
15945 complain, in_decl);
15946 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
15947 complain, in_decl);
15948 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
15949 complain, in_decl);
15950 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
15951 clobbers, labels);
15952 tree asm_expr = tmp;
15953 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
15954 asm_expr = TREE_OPERAND (asm_expr, 0);
15955 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
15957 break;
15959 case TRY_BLOCK:
15960 if (CLEANUP_P (t))
15962 stmt = begin_try_block ();
15963 RECUR (TRY_STMTS (t));
15964 finish_cleanup_try_block (stmt);
15965 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
15967 else
15969 tree compound_stmt = NULL_TREE;
15971 if (FN_TRY_BLOCK_P (t))
15972 stmt = begin_function_try_block (&compound_stmt);
15973 else
15974 stmt = begin_try_block ();
15976 RECUR (TRY_STMTS (t));
15978 if (FN_TRY_BLOCK_P (t))
15979 finish_function_try_block (stmt);
15980 else
15981 finish_try_block (stmt);
15983 RECUR (TRY_HANDLERS (t));
15984 if (FN_TRY_BLOCK_P (t))
15985 finish_function_handler_sequence (stmt, compound_stmt);
15986 else
15987 finish_handler_sequence (stmt);
15989 break;
15991 case HANDLER:
15993 tree decl = HANDLER_PARMS (t);
15995 if (decl)
15997 decl = tsubst (decl, args, complain, in_decl);
15998 /* Prevent instantiate_decl from trying to instantiate
15999 this variable. We've already done all that needs to be
16000 done. */
16001 if (decl != error_mark_node)
16002 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16004 stmt = begin_handler ();
16005 finish_handler_parms (decl, stmt);
16006 RECUR (HANDLER_BODY (t));
16007 finish_handler (stmt);
16009 break;
16011 case TAG_DEFN:
16012 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16013 if (CLASS_TYPE_P (tmp))
16015 /* Local classes are not independent templates; they are
16016 instantiated along with their containing function. And this
16017 way we don't have to deal with pushing out of one local class
16018 to instantiate a member of another local class. */
16019 tree fn;
16020 /* Closures are handled by the LAMBDA_EXPR. */
16021 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16022 complete_type (tmp);
16023 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16024 if (!DECL_ARTIFICIAL (fn))
16025 instantiate_decl (fn, /*defer_ok=*/false,
16026 /*expl_inst_class=*/false);
16028 break;
16030 case STATIC_ASSERT:
16032 tree condition;
16034 ++c_inhibit_evaluation_warnings;
16035 condition =
16036 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16037 args,
16038 complain, in_decl,
16039 /*integral_constant_expression_p=*/true);
16040 --c_inhibit_evaluation_warnings;
16042 finish_static_assert (condition,
16043 STATIC_ASSERT_MESSAGE (t),
16044 STATIC_ASSERT_SOURCE_LOCATION (t),
16045 /*member_p=*/false);
16047 break;
16049 case OACC_KERNELS:
16050 case OACC_PARALLEL:
16051 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16052 in_decl);
16053 stmt = begin_omp_parallel ();
16054 RECUR (OMP_BODY (t));
16055 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16056 break;
16058 case OMP_PARALLEL:
16059 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16060 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16061 complain, in_decl);
16062 if (OMP_PARALLEL_COMBINED (t))
16063 omp_parallel_combined_clauses = &tmp;
16064 stmt = begin_omp_parallel ();
16065 RECUR (OMP_PARALLEL_BODY (t));
16066 gcc_assert (omp_parallel_combined_clauses == NULL);
16067 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16068 = OMP_PARALLEL_COMBINED (t);
16069 pop_omp_privatization_clauses (r);
16070 break;
16072 case OMP_TASK:
16073 r = push_omp_privatization_clauses (false);
16074 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16075 complain, in_decl);
16076 stmt = begin_omp_task ();
16077 RECUR (OMP_TASK_BODY (t));
16078 finish_omp_task (tmp, stmt);
16079 pop_omp_privatization_clauses (r);
16080 break;
16082 case OMP_FOR:
16083 case OMP_SIMD:
16084 case CILK_SIMD:
16085 case CILK_FOR:
16086 case OMP_DISTRIBUTE:
16087 case OMP_TASKLOOP:
16088 case OACC_LOOP:
16090 tree clauses, body, pre_body;
16091 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16092 tree orig_declv = NULL_TREE;
16093 tree incrv = NULL_TREE;
16094 enum c_omp_region_type ort = C_ORT_OMP;
16095 int i;
16097 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16098 ort = C_ORT_CILK;
16099 else if (TREE_CODE (t) == OACC_LOOP)
16100 ort = C_ORT_ACC;
16102 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16103 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16104 in_decl);
16105 if (OMP_FOR_INIT (t) != NULL_TREE)
16107 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16108 if (OMP_FOR_ORIG_DECLS (t))
16109 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16110 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16111 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16112 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16115 stmt = begin_omp_structured_block ();
16117 pre_body = push_stmt_list ();
16118 RECUR (OMP_FOR_PRE_BODY (t));
16119 pre_body = pop_stmt_list (pre_body);
16121 if (OMP_FOR_INIT (t) != NULL_TREE)
16122 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16123 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16124 incrv, &clauses, args, complain, in_decl,
16125 integral_constant_expression_p);
16126 omp_parallel_combined_clauses = NULL;
16128 body = push_stmt_list ();
16129 RECUR (OMP_FOR_BODY (t));
16130 body = pop_stmt_list (body);
16132 if (OMP_FOR_INIT (t) != NULL_TREE)
16133 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16134 orig_declv, initv, condv, incrv, body, pre_body,
16135 NULL, clauses);
16136 else
16138 t = make_node (TREE_CODE (t));
16139 TREE_TYPE (t) = void_type_node;
16140 OMP_FOR_BODY (t) = body;
16141 OMP_FOR_PRE_BODY (t) = pre_body;
16142 OMP_FOR_CLAUSES (t) = clauses;
16143 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16144 add_stmt (t);
16147 add_stmt (finish_omp_structured_block (stmt));
16148 pop_omp_privatization_clauses (r);
16150 break;
16152 case OMP_SECTIONS:
16153 omp_parallel_combined_clauses = NULL;
16154 /* FALLTHRU */
16155 case OMP_SINGLE:
16156 case OMP_TEAMS:
16157 case OMP_CRITICAL:
16158 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16159 && OMP_TEAMS_COMBINED (t));
16160 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16161 in_decl);
16162 stmt = push_stmt_list ();
16163 RECUR (OMP_BODY (t));
16164 stmt = pop_stmt_list (stmt);
16166 t = copy_node (t);
16167 OMP_BODY (t) = stmt;
16168 OMP_CLAUSES (t) = tmp;
16169 add_stmt (t);
16170 pop_omp_privatization_clauses (r);
16171 break;
16173 case OACC_DATA:
16174 case OMP_TARGET_DATA:
16175 case OMP_TARGET:
16176 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16177 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16178 in_decl);
16179 keep_next_level (true);
16180 stmt = begin_omp_structured_block ();
16182 RECUR (OMP_BODY (t));
16183 stmt = finish_omp_structured_block (stmt);
16185 t = copy_node (t);
16186 OMP_BODY (t) = stmt;
16187 OMP_CLAUSES (t) = tmp;
16188 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16190 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16191 if (teams)
16193 /* For combined target teams, ensure the num_teams and
16194 thread_limit clause expressions are evaluated on the host,
16195 before entering the target construct. */
16196 tree c;
16197 for (c = OMP_TEAMS_CLAUSES (teams);
16198 c; c = OMP_CLAUSE_CHAIN (c))
16199 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16200 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16201 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16203 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16204 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16205 if (expr == error_mark_node)
16206 continue;
16207 tmp = TARGET_EXPR_SLOT (expr);
16208 add_stmt (expr);
16209 OMP_CLAUSE_OPERAND (c, 0) = expr;
16210 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16211 OMP_CLAUSE_FIRSTPRIVATE);
16212 OMP_CLAUSE_DECL (tc) = tmp;
16213 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16214 OMP_TARGET_CLAUSES (t) = tc;
16218 add_stmt (t);
16219 break;
16221 case OACC_DECLARE:
16222 t = copy_node (t);
16223 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16224 complain, in_decl);
16225 OACC_DECLARE_CLAUSES (t) = tmp;
16226 add_stmt (t);
16227 break;
16229 case OMP_TARGET_UPDATE:
16230 case OMP_TARGET_ENTER_DATA:
16231 case OMP_TARGET_EXIT_DATA:
16232 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16233 complain, in_decl);
16234 t = copy_node (t);
16235 OMP_STANDALONE_CLAUSES (t) = tmp;
16236 add_stmt (t);
16237 break;
16239 case OACC_ENTER_DATA:
16240 case OACC_EXIT_DATA:
16241 case OACC_UPDATE:
16242 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16243 complain, in_decl);
16244 t = copy_node (t);
16245 OMP_STANDALONE_CLAUSES (t) = tmp;
16246 add_stmt (t);
16247 break;
16249 case OMP_ORDERED:
16250 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16251 complain, in_decl);
16252 stmt = push_stmt_list ();
16253 RECUR (OMP_BODY (t));
16254 stmt = pop_stmt_list (stmt);
16256 t = copy_node (t);
16257 OMP_BODY (t) = stmt;
16258 OMP_ORDERED_CLAUSES (t) = tmp;
16259 add_stmt (t);
16260 break;
16262 case OMP_SECTION:
16263 case OMP_MASTER:
16264 case OMP_TASKGROUP:
16265 stmt = push_stmt_list ();
16266 RECUR (OMP_BODY (t));
16267 stmt = pop_stmt_list (stmt);
16269 t = copy_node (t);
16270 OMP_BODY (t) = stmt;
16271 add_stmt (t);
16272 break;
16274 case OMP_ATOMIC:
16275 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16276 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16278 tree op1 = TREE_OPERAND (t, 1);
16279 tree rhs1 = NULL_TREE;
16280 tree lhs, rhs;
16281 if (TREE_CODE (op1) == COMPOUND_EXPR)
16283 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16284 op1 = TREE_OPERAND (op1, 1);
16286 lhs = RECUR (TREE_OPERAND (op1, 0));
16287 rhs = RECUR (TREE_OPERAND (op1, 1));
16288 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16289 NULL_TREE, NULL_TREE, rhs1,
16290 OMP_ATOMIC_SEQ_CST (t));
16292 else
16294 tree op1 = TREE_OPERAND (t, 1);
16295 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16296 tree rhs1 = NULL_TREE;
16297 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16298 enum tree_code opcode = NOP_EXPR;
16299 if (code == OMP_ATOMIC_READ)
16301 v = RECUR (TREE_OPERAND (op1, 0));
16302 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16304 else if (code == OMP_ATOMIC_CAPTURE_OLD
16305 || code == OMP_ATOMIC_CAPTURE_NEW)
16307 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16308 v = RECUR (TREE_OPERAND (op1, 0));
16309 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16310 if (TREE_CODE (op11) == COMPOUND_EXPR)
16312 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16313 op11 = TREE_OPERAND (op11, 1);
16315 lhs = RECUR (TREE_OPERAND (op11, 0));
16316 rhs = RECUR (TREE_OPERAND (op11, 1));
16317 opcode = TREE_CODE (op11);
16318 if (opcode == MODIFY_EXPR)
16319 opcode = NOP_EXPR;
16321 else
16323 code = OMP_ATOMIC;
16324 lhs = RECUR (TREE_OPERAND (op1, 0));
16325 rhs = RECUR (TREE_OPERAND (op1, 1));
16327 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16328 OMP_ATOMIC_SEQ_CST (t));
16330 break;
16332 case TRANSACTION_EXPR:
16334 int flags = 0;
16335 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16336 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16338 if (TRANSACTION_EXPR_IS_STMT (t))
16340 tree body = TRANSACTION_EXPR_BODY (t);
16341 tree noex = NULL_TREE;
16342 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16344 noex = MUST_NOT_THROW_COND (body);
16345 if (noex == NULL_TREE)
16346 noex = boolean_true_node;
16347 body = TREE_OPERAND (body, 0);
16349 stmt = begin_transaction_stmt (input_location, NULL, flags);
16350 RECUR (body);
16351 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16353 else
16355 stmt = build_transaction_expr (EXPR_LOCATION (t),
16356 RECUR (TRANSACTION_EXPR_BODY (t)),
16357 flags, NULL_TREE);
16358 RETURN (stmt);
16361 break;
16363 case MUST_NOT_THROW_EXPR:
16365 tree op0 = RECUR (TREE_OPERAND (t, 0));
16366 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16367 RETURN (build_must_not_throw_expr (op0, cond));
16370 case EXPR_PACK_EXPANSION:
16371 error ("invalid use of pack expansion expression");
16372 RETURN (error_mark_node);
16374 case NONTYPE_ARGUMENT_PACK:
16375 error ("use %<...%> to expand argument pack");
16376 RETURN (error_mark_node);
16378 case CILK_SPAWN_STMT:
16379 cfun->calls_cilk_spawn = 1;
16380 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16382 case CILK_SYNC_STMT:
16383 RETURN (build_cilk_sync ());
16385 case COMPOUND_EXPR:
16386 tmp = RECUR (TREE_OPERAND (t, 0));
16387 if (tmp == NULL_TREE)
16388 /* If the first operand was a statement, we're done with it. */
16389 RETURN (RECUR (TREE_OPERAND (t, 1)));
16390 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16391 RECUR (TREE_OPERAND (t, 1)),
16392 complain));
16394 case ANNOTATE_EXPR:
16395 tmp = RECUR (TREE_OPERAND (t, 0));
16396 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16397 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16399 default:
16400 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16402 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16403 /*function_p=*/false,
16404 integral_constant_expression_p));
16407 RETURN (NULL_TREE);
16408 out:
16409 input_location = loc;
16410 return r;
16411 #undef RECUR
16412 #undef RETURN
16415 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16416 function. For description of the body see comment above
16417 cp_parser_omp_declare_reduction_exprs. */
16419 static void
16420 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16422 if (t == NULL_TREE || t == error_mark_node)
16423 return;
16425 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16427 tree_stmt_iterator tsi;
16428 int i;
16429 tree stmts[7];
16430 memset (stmts, 0, sizeof stmts);
16431 for (i = 0, tsi = tsi_start (t);
16432 i < 7 && !tsi_end_p (tsi);
16433 i++, tsi_next (&tsi))
16434 stmts[i] = tsi_stmt (tsi);
16435 gcc_assert (tsi_end_p (tsi));
16437 if (i >= 3)
16439 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16440 && TREE_CODE (stmts[1]) == DECL_EXPR);
16441 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16442 args, complain, in_decl);
16443 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16444 args, complain, in_decl);
16445 DECL_CONTEXT (omp_out) = current_function_decl;
16446 DECL_CONTEXT (omp_in) = current_function_decl;
16447 keep_next_level (true);
16448 tree block = begin_omp_structured_block ();
16449 tsubst_expr (stmts[2], args, complain, in_decl, false);
16450 block = finish_omp_structured_block (block);
16451 block = maybe_cleanup_point_expr_void (block);
16452 add_decl_expr (omp_out);
16453 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16454 TREE_NO_WARNING (omp_out) = 1;
16455 add_decl_expr (omp_in);
16456 finish_expr_stmt (block);
16458 if (i >= 6)
16460 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16461 && TREE_CODE (stmts[4]) == DECL_EXPR);
16462 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16463 args, complain, in_decl);
16464 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16465 args, complain, in_decl);
16466 DECL_CONTEXT (omp_priv) = current_function_decl;
16467 DECL_CONTEXT (omp_orig) = current_function_decl;
16468 keep_next_level (true);
16469 tree block = begin_omp_structured_block ();
16470 tsubst_expr (stmts[5], args, complain, in_decl, false);
16471 block = finish_omp_structured_block (block);
16472 block = maybe_cleanup_point_expr_void (block);
16473 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16474 add_decl_expr (omp_priv);
16475 add_decl_expr (omp_orig);
16476 finish_expr_stmt (block);
16477 if (i == 7)
16478 add_decl_expr (omp_orig);
16482 /* T is a postfix-expression that is not being used in a function
16483 call. Return the substituted version of T. */
16485 static tree
16486 tsubst_non_call_postfix_expression (tree t, tree args,
16487 tsubst_flags_t complain,
16488 tree in_decl)
16490 if (TREE_CODE (t) == SCOPE_REF)
16491 t = tsubst_qualified_id (t, args, complain, in_decl,
16492 /*done=*/false, /*address_p=*/false);
16493 else
16494 t = tsubst_copy_and_build (t, args, complain, in_decl,
16495 /*function_p=*/false,
16496 /*integral_constant_expression_p=*/false);
16498 return t;
16501 /* Like tsubst but deals with expressions and performs semantic
16502 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16504 tree
16505 tsubst_copy_and_build (tree t,
16506 tree args,
16507 tsubst_flags_t complain,
16508 tree in_decl,
16509 bool function_p,
16510 bool integral_constant_expression_p)
16512 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16513 #define RECUR(NODE) \
16514 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16515 /*function_p=*/false, \
16516 integral_constant_expression_p)
16518 tree retval, op1;
16519 location_t loc;
16521 if (t == NULL_TREE || t == error_mark_node)
16522 return t;
16524 loc = input_location;
16525 if (EXPR_HAS_LOCATION (t))
16526 input_location = EXPR_LOCATION (t);
16528 /* N3276 decltype magic only applies to calls at the top level or on the
16529 right side of a comma. */
16530 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16531 complain &= ~tf_decltype;
16533 switch (TREE_CODE (t))
16535 case USING_DECL:
16536 t = DECL_NAME (t);
16537 /* Fall through. */
16538 case IDENTIFIER_NODE:
16540 tree decl;
16541 cp_id_kind idk;
16542 bool non_integral_constant_expression_p;
16543 const char *error_msg;
16545 if (IDENTIFIER_TYPENAME_P (t))
16547 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16548 t = mangle_conv_op_name_for_type (new_type);
16551 /* Look up the name. */
16552 decl = lookup_name (t);
16554 /* By convention, expressions use ERROR_MARK_NODE to indicate
16555 failure, not NULL_TREE. */
16556 if (decl == NULL_TREE)
16557 decl = error_mark_node;
16559 decl = finish_id_expression (t, decl, NULL_TREE,
16560 &idk,
16561 integral_constant_expression_p,
16562 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16563 &non_integral_constant_expression_p,
16564 /*template_p=*/false,
16565 /*done=*/true,
16566 /*address_p=*/false,
16567 /*template_arg_p=*/false,
16568 &error_msg,
16569 input_location);
16570 if (error_msg)
16571 error (error_msg);
16572 if (!function_p && identifier_p (decl))
16574 if (complain & tf_error)
16575 unqualified_name_lookup_error (decl);
16576 decl = error_mark_node;
16578 RETURN (decl);
16581 case TEMPLATE_ID_EXPR:
16583 tree object;
16584 tree templ = RECUR (TREE_OPERAND (t, 0));
16585 tree targs = TREE_OPERAND (t, 1);
16587 if (targs)
16588 targs = tsubst_template_args (targs, args, complain, in_decl);
16589 if (targs == error_mark_node)
16590 return error_mark_node;
16592 if (TREE_CODE (templ) == SCOPE_REF)
16594 tree name = TREE_OPERAND (templ, 1);
16595 tree tid = lookup_template_function (name, targs);
16596 TREE_OPERAND (templ, 1) = tid;
16597 return templ;
16600 if (variable_template_p (templ))
16601 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16603 if (TREE_CODE (templ) == COMPONENT_REF)
16605 object = TREE_OPERAND (templ, 0);
16606 templ = TREE_OPERAND (templ, 1);
16608 else
16609 object = NULL_TREE;
16610 templ = lookup_template_function (templ, targs);
16612 if (object)
16613 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16614 object, templ, NULL_TREE));
16615 else
16616 RETURN (baselink_for_fns (templ));
16619 case INDIRECT_REF:
16621 tree r = RECUR (TREE_OPERAND (t, 0));
16623 if (REFERENCE_REF_P (t))
16625 /* A type conversion to reference type will be enclosed in
16626 such an indirect ref, but the substitution of the cast
16627 will have also added such an indirect ref. */
16628 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16629 r = convert_from_reference (r);
16631 else
16632 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16633 complain|decltype_flag);
16635 if (TREE_CODE (r) == INDIRECT_REF)
16636 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16638 RETURN (r);
16641 case NOP_EXPR:
16643 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16644 tree op0 = RECUR (TREE_OPERAND (t, 0));
16645 RETURN (build_nop (type, op0));
16648 case IMPLICIT_CONV_EXPR:
16650 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16651 tree expr = RECUR (TREE_OPERAND (t, 0));
16652 int flags = LOOKUP_IMPLICIT;
16653 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16654 flags = LOOKUP_NORMAL;
16655 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16656 flags));
16659 case CONVERT_EXPR:
16661 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16662 tree op0 = RECUR (TREE_OPERAND (t, 0));
16663 RETURN (build1 (CONVERT_EXPR, type, op0));
16666 case CAST_EXPR:
16667 case REINTERPRET_CAST_EXPR:
16668 case CONST_CAST_EXPR:
16669 case DYNAMIC_CAST_EXPR:
16670 case STATIC_CAST_EXPR:
16672 tree type;
16673 tree op, r = NULL_TREE;
16675 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16676 if (integral_constant_expression_p
16677 && !cast_valid_in_integral_constant_expression_p (type))
16679 if (complain & tf_error)
16680 error ("a cast to a type other than an integral or "
16681 "enumeration type cannot appear in a constant-expression");
16682 RETURN (error_mark_node);
16685 op = RECUR (TREE_OPERAND (t, 0));
16687 warning_sentinel s(warn_useless_cast);
16688 switch (TREE_CODE (t))
16690 case CAST_EXPR:
16691 r = build_functional_cast (type, op, complain);
16692 break;
16693 case REINTERPRET_CAST_EXPR:
16694 r = build_reinterpret_cast (type, op, complain);
16695 break;
16696 case CONST_CAST_EXPR:
16697 r = build_const_cast (type, op, complain);
16698 break;
16699 case DYNAMIC_CAST_EXPR:
16700 r = build_dynamic_cast (type, op, complain);
16701 break;
16702 case STATIC_CAST_EXPR:
16703 r = build_static_cast (type, op, complain);
16704 break;
16705 default:
16706 gcc_unreachable ();
16709 RETURN (r);
16712 case POSTDECREMENT_EXPR:
16713 case POSTINCREMENT_EXPR:
16714 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16715 args, complain, in_decl);
16716 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16717 complain|decltype_flag));
16719 case PREDECREMENT_EXPR:
16720 case PREINCREMENT_EXPR:
16721 case NEGATE_EXPR:
16722 case BIT_NOT_EXPR:
16723 case ABS_EXPR:
16724 case TRUTH_NOT_EXPR:
16725 case UNARY_PLUS_EXPR: /* Unary + */
16726 case REALPART_EXPR:
16727 case IMAGPART_EXPR:
16728 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16729 RECUR (TREE_OPERAND (t, 0)),
16730 complain|decltype_flag));
16732 case FIX_TRUNC_EXPR:
16733 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16734 false, complain));
16736 case ADDR_EXPR:
16737 op1 = TREE_OPERAND (t, 0);
16738 if (TREE_CODE (op1) == LABEL_DECL)
16739 RETURN (finish_label_address_expr (DECL_NAME (op1),
16740 EXPR_LOCATION (op1)));
16741 if (TREE_CODE (op1) == SCOPE_REF)
16742 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16743 /*done=*/true, /*address_p=*/true);
16744 else
16745 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16746 in_decl);
16747 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16748 complain|decltype_flag));
16750 case PLUS_EXPR:
16751 case MINUS_EXPR:
16752 case MULT_EXPR:
16753 case TRUNC_DIV_EXPR:
16754 case CEIL_DIV_EXPR:
16755 case FLOOR_DIV_EXPR:
16756 case ROUND_DIV_EXPR:
16757 case EXACT_DIV_EXPR:
16758 case BIT_AND_EXPR:
16759 case BIT_IOR_EXPR:
16760 case BIT_XOR_EXPR:
16761 case TRUNC_MOD_EXPR:
16762 case FLOOR_MOD_EXPR:
16763 case TRUTH_ANDIF_EXPR:
16764 case TRUTH_ORIF_EXPR:
16765 case TRUTH_AND_EXPR:
16766 case TRUTH_OR_EXPR:
16767 case RSHIFT_EXPR:
16768 case LSHIFT_EXPR:
16769 case RROTATE_EXPR:
16770 case LROTATE_EXPR:
16771 case EQ_EXPR:
16772 case NE_EXPR:
16773 case MAX_EXPR:
16774 case MIN_EXPR:
16775 case LE_EXPR:
16776 case GE_EXPR:
16777 case LT_EXPR:
16778 case GT_EXPR:
16779 case MEMBER_REF:
16780 case DOTSTAR_EXPR:
16782 warning_sentinel s1(warn_type_limits);
16783 warning_sentinel s2(warn_div_by_zero);
16784 warning_sentinel s3(warn_logical_op);
16785 warning_sentinel s4(warn_tautological_compare);
16786 tree op0 = RECUR (TREE_OPERAND (t, 0));
16787 tree op1 = RECUR (TREE_OPERAND (t, 1));
16788 tree r = build_x_binary_op
16789 (input_location, TREE_CODE (t),
16790 op0,
16791 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16792 ? ERROR_MARK
16793 : TREE_CODE (TREE_OPERAND (t, 0))),
16794 op1,
16795 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16796 ? ERROR_MARK
16797 : TREE_CODE (TREE_OPERAND (t, 1))),
16798 /*overload=*/NULL,
16799 complain|decltype_flag);
16800 if (EXPR_P (r) && TREE_NO_WARNING (t))
16801 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16803 RETURN (r);
16806 case POINTER_PLUS_EXPR:
16808 tree op0 = RECUR (TREE_OPERAND (t, 0));
16809 tree op1 = RECUR (TREE_OPERAND (t, 1));
16810 return fold_build_pointer_plus (op0, op1);
16813 case SCOPE_REF:
16814 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16815 /*address_p=*/false));
16816 case ARRAY_REF:
16817 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16818 args, complain, in_decl);
16819 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16820 RECUR (TREE_OPERAND (t, 1)),
16821 complain|decltype_flag));
16823 case ARRAY_NOTATION_REF:
16825 tree start_index, length, stride;
16826 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16827 args, complain, in_decl);
16828 start_index = RECUR (ARRAY_NOTATION_START (t));
16829 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16830 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16831 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16832 length, stride, TREE_TYPE (op1)));
16834 case SIZEOF_EXPR:
16835 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16836 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16837 RETURN (tsubst_copy (t, args, complain, in_decl));
16838 /* Fall through */
16840 case ALIGNOF_EXPR:
16842 tree r;
16844 op1 = TREE_OPERAND (t, 0);
16845 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16846 op1 = TREE_TYPE (op1);
16847 if (!args)
16849 /* When there are no ARGS, we are trying to evaluate a
16850 non-dependent expression from the parser. Trying to do
16851 the substitutions may not work. */
16852 if (!TYPE_P (op1))
16853 op1 = TREE_TYPE (op1);
16855 else
16857 ++cp_unevaluated_operand;
16858 ++c_inhibit_evaluation_warnings;
16859 if (TYPE_P (op1))
16860 op1 = tsubst (op1, args, complain, in_decl);
16861 else
16862 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16863 /*function_p=*/false,
16864 /*integral_constant_expression_p=*/
16865 false);
16866 --cp_unevaluated_operand;
16867 --c_inhibit_evaluation_warnings;
16869 if (TYPE_P (op1))
16870 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16871 complain & tf_error);
16872 else
16873 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16874 complain & tf_error);
16875 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16877 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16879 if (!processing_template_decl && TYPE_P (op1))
16881 r = build_min (SIZEOF_EXPR, size_type_node,
16882 build1 (NOP_EXPR, op1, error_mark_node));
16883 SIZEOF_EXPR_TYPE_P (r) = 1;
16885 else
16886 r = build_min (SIZEOF_EXPR, size_type_node, op1);
16887 TREE_SIDE_EFFECTS (r) = 0;
16888 TREE_READONLY (r) = 1;
16890 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16892 RETURN (r);
16895 case AT_ENCODE_EXPR:
16897 op1 = TREE_OPERAND (t, 0);
16898 ++cp_unevaluated_operand;
16899 ++c_inhibit_evaluation_warnings;
16900 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16901 /*function_p=*/false,
16902 /*integral_constant_expression_p=*/false);
16903 --cp_unevaluated_operand;
16904 --c_inhibit_evaluation_warnings;
16905 RETURN (objc_build_encode_expr (op1));
16908 case NOEXCEPT_EXPR:
16909 op1 = TREE_OPERAND (t, 0);
16910 ++cp_unevaluated_operand;
16911 ++c_inhibit_evaluation_warnings;
16912 ++cp_noexcept_operand;
16913 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16914 /*function_p=*/false,
16915 /*integral_constant_expression_p=*/false);
16916 --cp_unevaluated_operand;
16917 --c_inhibit_evaluation_warnings;
16918 --cp_noexcept_operand;
16919 RETURN (finish_noexcept_expr (op1, complain));
16921 case MODOP_EXPR:
16923 warning_sentinel s(warn_div_by_zero);
16924 tree lhs = RECUR (TREE_OPERAND (t, 0));
16925 tree rhs = RECUR (TREE_OPERAND (t, 2));
16926 tree r = build_x_modify_expr
16927 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
16928 complain|decltype_flag);
16929 /* TREE_NO_WARNING must be set if either the expression was
16930 parenthesized or it uses an operator such as >>= rather
16931 than plain assignment. In the former case, it was already
16932 set and must be copied. In the latter case,
16933 build_x_modify_expr sets it and it must not be reset
16934 here. */
16935 if (TREE_NO_WARNING (t))
16936 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16938 RETURN (r);
16941 case ARROW_EXPR:
16942 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16943 args, complain, in_decl);
16944 /* Remember that there was a reference to this entity. */
16945 if (DECL_P (op1)
16946 && !mark_used (op1, complain) && !(complain & tf_error))
16947 RETURN (error_mark_node);
16948 RETURN (build_x_arrow (input_location, op1, complain));
16950 case NEW_EXPR:
16952 tree placement = RECUR (TREE_OPERAND (t, 0));
16953 tree init = RECUR (TREE_OPERAND (t, 3));
16954 vec<tree, va_gc> *placement_vec;
16955 vec<tree, va_gc> *init_vec;
16956 tree ret;
16958 if (placement == NULL_TREE)
16959 placement_vec = NULL;
16960 else
16962 placement_vec = make_tree_vector ();
16963 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
16964 vec_safe_push (placement_vec, TREE_VALUE (placement));
16967 /* If there was an initializer in the original tree, but it
16968 instantiated to an empty list, then we should pass a
16969 non-NULL empty vector to tell build_new that it was an
16970 empty initializer() rather than no initializer. This can
16971 only happen when the initializer is a pack expansion whose
16972 parameter packs are of length zero. */
16973 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
16974 init_vec = NULL;
16975 else
16977 init_vec = make_tree_vector ();
16978 if (init == void_node)
16979 gcc_assert (init_vec != NULL);
16980 else
16982 for (; init != NULL_TREE; init = TREE_CHAIN (init))
16983 vec_safe_push (init_vec, TREE_VALUE (init));
16987 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
16988 tree op2 = RECUR (TREE_OPERAND (t, 2));
16989 ret = build_new (&placement_vec, op1, op2, &init_vec,
16990 NEW_EXPR_USE_GLOBAL (t),
16991 complain);
16993 if (placement_vec != NULL)
16994 release_tree_vector (placement_vec);
16995 if (init_vec != NULL)
16996 release_tree_vector (init_vec);
16998 RETURN (ret);
17001 case DELETE_EXPR:
17003 tree op0 = RECUR (TREE_OPERAND (t, 0));
17004 tree op1 = RECUR (TREE_OPERAND (t, 1));
17005 RETURN (delete_sanity (op0, op1,
17006 DELETE_EXPR_USE_VEC (t),
17007 DELETE_EXPR_USE_GLOBAL (t),
17008 complain));
17011 case COMPOUND_EXPR:
17013 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17014 complain & ~tf_decltype, in_decl,
17015 /*function_p=*/false,
17016 integral_constant_expression_p);
17017 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17018 op0,
17019 RECUR (TREE_OPERAND (t, 1)),
17020 complain|decltype_flag));
17023 case CALL_EXPR:
17025 tree function;
17026 vec<tree, va_gc> *call_args;
17027 unsigned int nargs, i;
17028 bool qualified_p;
17029 bool koenig_p;
17030 tree ret;
17032 function = CALL_EXPR_FN (t);
17033 /* Internal function with no arguments. */
17034 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17035 RETURN (t);
17037 /* When we parsed the expression, we determined whether or
17038 not Koenig lookup should be performed. */
17039 koenig_p = KOENIG_LOOKUP_P (t);
17040 if (function == NULL_TREE)
17042 koenig_p = false;
17043 qualified_p = false;
17045 else if (TREE_CODE (function) == SCOPE_REF)
17047 qualified_p = true;
17048 function = tsubst_qualified_id (function, args, complain, in_decl,
17049 /*done=*/false,
17050 /*address_p=*/false);
17052 else if (koenig_p && identifier_p (function))
17054 /* Do nothing; calling tsubst_copy_and_build on an identifier
17055 would incorrectly perform unqualified lookup again.
17057 Note that we can also have an IDENTIFIER_NODE if the earlier
17058 unqualified lookup found a member function; in that case
17059 koenig_p will be false and we do want to do the lookup
17060 again to find the instantiated member function.
17062 FIXME but doing that causes c++/15272, so we need to stop
17063 using IDENTIFIER_NODE in that situation. */
17064 qualified_p = false;
17066 else
17068 if (TREE_CODE (function) == COMPONENT_REF)
17070 tree op = TREE_OPERAND (function, 1);
17072 qualified_p = (TREE_CODE (op) == SCOPE_REF
17073 || (BASELINK_P (op)
17074 && BASELINK_QUALIFIED_P (op)));
17076 else
17077 qualified_p = false;
17079 if (TREE_CODE (function) == ADDR_EXPR
17080 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17081 /* Avoid error about taking the address of a constructor. */
17082 function = TREE_OPERAND (function, 0);
17084 function = tsubst_copy_and_build (function, args, complain,
17085 in_decl,
17086 !qualified_p,
17087 integral_constant_expression_p);
17089 if (BASELINK_P (function))
17090 qualified_p = true;
17093 nargs = call_expr_nargs (t);
17094 call_args = make_tree_vector ();
17095 for (i = 0; i < nargs; ++i)
17097 tree arg = CALL_EXPR_ARG (t, i);
17099 if (!PACK_EXPANSION_P (arg))
17100 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17101 else
17103 /* Expand the pack expansion and push each entry onto
17104 CALL_ARGS. */
17105 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17106 if (TREE_CODE (arg) == TREE_VEC)
17108 unsigned int len, j;
17110 len = TREE_VEC_LENGTH (arg);
17111 for (j = 0; j < len; ++j)
17113 tree value = TREE_VEC_ELT (arg, j);
17114 if (value != NULL_TREE)
17115 value = convert_from_reference (value);
17116 vec_safe_push (call_args, value);
17119 else
17121 /* A partial substitution. Add one entry. */
17122 vec_safe_push (call_args, arg);
17127 /* We do not perform argument-dependent lookup if normal
17128 lookup finds a non-function, in accordance with the
17129 expected resolution of DR 218. */
17130 if (koenig_p
17131 && ((is_overloaded_fn (function)
17132 /* If lookup found a member function, the Koenig lookup is
17133 not appropriate, even if an unqualified-name was used
17134 to denote the function. */
17135 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17136 || identifier_p (function))
17137 /* Only do this when substitution turns a dependent call
17138 into a non-dependent call. */
17139 && type_dependent_expression_p_push (t)
17140 && !any_type_dependent_arguments_p (call_args))
17141 function = perform_koenig_lookup (function, call_args, tf_none);
17143 if (function != NULL_TREE
17144 && identifier_p (function)
17145 && !any_type_dependent_arguments_p (call_args))
17147 if (koenig_p && (complain & tf_warning_or_error))
17149 /* For backwards compatibility and good diagnostics, try
17150 the unqualified lookup again if we aren't in SFINAE
17151 context. */
17152 tree unq = (tsubst_copy_and_build
17153 (function, args, complain, in_decl, true,
17154 integral_constant_expression_p));
17155 if (unq == error_mark_node)
17157 release_tree_vector (call_args);
17158 RETURN (error_mark_node);
17161 if (unq != function)
17163 /* In a lambda fn, we have to be careful to not
17164 introduce new this captures. Legacy code can't
17165 be using lambdas anyway, so it's ok to be
17166 stricter. */
17167 bool in_lambda = (current_class_type
17168 && LAMBDA_TYPE_P (current_class_type));
17169 char const *const msg
17170 = G_("%qD was not declared in this scope, "
17171 "and no declarations were found by "
17172 "argument-dependent lookup at the point "
17173 "of instantiation");
17175 bool diag = true;
17176 if (in_lambda)
17177 error_at (EXPR_LOC_OR_LOC (t, input_location),
17178 msg, function);
17179 else
17180 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17181 msg, function);
17182 if (diag)
17184 tree fn = unq;
17186 if (INDIRECT_REF_P (fn))
17187 fn = TREE_OPERAND (fn, 0);
17188 if (is_overloaded_fn (fn))
17189 fn = get_first_fn (fn);
17191 if (!DECL_P (fn))
17192 /* Can't say anything more. */;
17193 else if (DECL_CLASS_SCOPE_P (fn))
17195 location_t loc = EXPR_LOC_OR_LOC (t,
17196 input_location);
17197 inform (loc,
17198 "declarations in dependent base %qT are "
17199 "not found by unqualified lookup",
17200 DECL_CLASS_CONTEXT (fn));
17201 if (current_class_ptr)
17202 inform (loc,
17203 "use %<this->%D%> instead", function);
17204 else
17205 inform (loc,
17206 "use %<%T::%D%> instead",
17207 current_class_name, function);
17209 else
17210 inform (DECL_SOURCE_LOCATION (fn),
17211 "%qD declared here, later in the "
17212 "translation unit", fn);
17213 if (in_lambda)
17215 release_tree_vector (call_args);
17216 RETURN (error_mark_node);
17220 function = unq;
17223 if (identifier_p (function))
17225 if (complain & tf_error)
17226 unqualified_name_lookup_error (function);
17227 release_tree_vector (call_args);
17228 RETURN (error_mark_node);
17232 /* Remember that there was a reference to this entity. */
17233 if (function != NULL_TREE
17234 && DECL_P (function)
17235 && !mark_used (function, complain) && !(complain & tf_error))
17237 release_tree_vector (call_args);
17238 RETURN (error_mark_node);
17241 /* Put back tf_decltype for the actual call. */
17242 complain |= decltype_flag;
17244 if (function == NULL_TREE)
17245 switch (CALL_EXPR_IFN (t))
17247 case IFN_LAUNDER:
17248 gcc_assert (nargs == 1);
17249 if (vec_safe_length (call_args) != 1)
17251 error_at (EXPR_LOC_OR_LOC (t, input_location),
17252 "wrong number of arguments to "
17253 "%<__builtin_launder%>");
17254 ret = error_mark_node;
17256 else
17257 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17258 input_location),
17259 (*call_args)[0], complain);
17260 break;
17262 default:
17263 /* Unsupported internal function with arguments. */
17264 gcc_unreachable ();
17266 else if (TREE_CODE (function) == OFFSET_REF)
17267 ret = build_offset_ref_call_from_tree (function, &call_args,
17268 complain);
17269 else if (TREE_CODE (function) == COMPONENT_REF)
17271 tree instance = TREE_OPERAND (function, 0);
17272 tree fn = TREE_OPERAND (function, 1);
17274 if (processing_template_decl
17275 && (type_dependent_expression_p (instance)
17276 || (!BASELINK_P (fn)
17277 && TREE_CODE (fn) != FIELD_DECL)
17278 || type_dependent_expression_p (fn)
17279 || any_type_dependent_arguments_p (call_args)))
17280 ret = build_nt_call_vec (function, call_args);
17281 else if (!BASELINK_P (fn))
17282 ret = finish_call_expr (function, &call_args,
17283 /*disallow_virtual=*/false,
17284 /*koenig_p=*/false,
17285 complain);
17286 else
17287 ret = (build_new_method_call
17288 (instance, fn,
17289 &call_args, NULL_TREE,
17290 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17291 /*fn_p=*/NULL,
17292 complain));
17294 else
17295 ret = finish_call_expr (function, &call_args,
17296 /*disallow_virtual=*/qualified_p,
17297 koenig_p,
17298 complain);
17300 release_tree_vector (call_args);
17302 if (ret != error_mark_node)
17304 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17305 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17306 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17307 bool thk = CALL_FROM_THUNK_P (t);
17308 if (op || ord || rev || thk)
17310 function = extract_call_expr (ret);
17311 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17312 CALL_EXPR_ORDERED_ARGS (function) = ord;
17313 CALL_EXPR_REVERSE_ARGS (function) = rev;
17314 if (thk)
17316 CALL_FROM_THUNK_P (function) = true;
17317 /* The thunk location is not interesting. */
17318 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17323 RETURN (ret);
17326 case COND_EXPR:
17328 tree cond = RECUR (TREE_OPERAND (t, 0));
17329 tree folded_cond = fold_non_dependent_expr (cond);
17330 tree exp1, exp2;
17332 if (TREE_CODE (folded_cond) == INTEGER_CST)
17334 if (integer_zerop (folded_cond))
17336 ++c_inhibit_evaluation_warnings;
17337 exp1 = RECUR (TREE_OPERAND (t, 1));
17338 --c_inhibit_evaluation_warnings;
17339 exp2 = RECUR (TREE_OPERAND (t, 2));
17341 else
17343 exp1 = RECUR (TREE_OPERAND (t, 1));
17344 ++c_inhibit_evaluation_warnings;
17345 exp2 = RECUR (TREE_OPERAND (t, 2));
17346 --c_inhibit_evaluation_warnings;
17348 cond = folded_cond;
17350 else
17352 exp1 = RECUR (TREE_OPERAND (t, 1));
17353 exp2 = RECUR (TREE_OPERAND (t, 2));
17356 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17357 cond, exp1, exp2, complain));
17360 case PSEUDO_DTOR_EXPR:
17362 tree op0 = RECUR (TREE_OPERAND (t, 0));
17363 tree op1 = RECUR (TREE_OPERAND (t, 1));
17364 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17365 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17366 input_location));
17369 case TREE_LIST:
17371 tree purpose, value, chain;
17373 if (t == void_list_node)
17374 RETURN (t);
17376 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17377 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17379 /* We have pack expansions, so expand those and
17380 create a new list out of it. */
17381 tree purposevec = NULL_TREE;
17382 tree valuevec = NULL_TREE;
17383 tree chain;
17384 int i, len = -1;
17386 /* Expand the argument expressions. */
17387 if (TREE_PURPOSE (t))
17388 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17389 complain, in_decl);
17390 if (TREE_VALUE (t))
17391 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17392 complain, in_decl);
17394 /* Build the rest of the list. */
17395 chain = TREE_CHAIN (t);
17396 if (chain && chain != void_type_node)
17397 chain = RECUR (chain);
17399 /* Determine the number of arguments. */
17400 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17402 len = TREE_VEC_LENGTH (purposevec);
17403 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17405 else if (TREE_CODE (valuevec) == TREE_VEC)
17406 len = TREE_VEC_LENGTH (valuevec);
17407 else
17409 /* Since we only performed a partial substitution into
17410 the argument pack, we only RETURN (a single list
17411 node. */
17412 if (purposevec == TREE_PURPOSE (t)
17413 && valuevec == TREE_VALUE (t)
17414 && chain == TREE_CHAIN (t))
17415 RETURN (t);
17417 RETURN (tree_cons (purposevec, valuevec, chain));
17420 /* Convert the argument vectors into a TREE_LIST */
17421 i = len;
17422 while (i > 0)
17424 /* Grab the Ith values. */
17425 i--;
17426 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17427 : NULL_TREE;
17428 value
17429 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17430 : NULL_TREE;
17432 /* Build the list (backwards). */
17433 chain = tree_cons (purpose, value, chain);
17436 RETURN (chain);
17439 purpose = TREE_PURPOSE (t);
17440 if (purpose)
17441 purpose = RECUR (purpose);
17442 value = TREE_VALUE (t);
17443 if (value)
17444 value = RECUR (value);
17445 chain = TREE_CHAIN (t);
17446 if (chain && chain != void_type_node)
17447 chain = RECUR (chain);
17448 if (purpose == TREE_PURPOSE (t)
17449 && value == TREE_VALUE (t)
17450 && chain == TREE_CHAIN (t))
17451 RETURN (t);
17452 RETURN (tree_cons (purpose, value, chain));
17455 case COMPONENT_REF:
17457 tree object;
17458 tree object_type;
17459 tree member;
17460 tree r;
17462 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17463 args, complain, in_decl);
17464 /* Remember that there was a reference to this entity. */
17465 if (DECL_P (object)
17466 && !mark_used (object, complain) && !(complain & tf_error))
17467 RETURN (error_mark_node);
17468 object_type = TREE_TYPE (object);
17470 member = TREE_OPERAND (t, 1);
17471 if (BASELINK_P (member))
17472 member = tsubst_baselink (member,
17473 non_reference (TREE_TYPE (object)),
17474 args, complain, in_decl);
17475 else
17476 member = tsubst_copy (member, args, complain, in_decl);
17477 if (member == error_mark_node)
17478 RETURN (error_mark_node);
17480 if (TREE_CODE (member) == FIELD_DECL)
17482 r = finish_non_static_data_member (member, object, NULL_TREE);
17483 if (TREE_CODE (r) == COMPONENT_REF)
17484 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17485 RETURN (r);
17487 else if (type_dependent_expression_p (object))
17488 /* We can't do much here. */;
17489 else if (!CLASS_TYPE_P (object_type))
17491 if (scalarish_type_p (object_type))
17493 tree s = NULL_TREE;
17494 tree dtor = member;
17496 if (TREE_CODE (dtor) == SCOPE_REF)
17498 s = TREE_OPERAND (dtor, 0);
17499 dtor = TREE_OPERAND (dtor, 1);
17501 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17503 dtor = TREE_OPERAND (dtor, 0);
17504 if (TYPE_P (dtor))
17505 RETURN (finish_pseudo_destructor_expr
17506 (object, s, dtor, input_location));
17510 else if (TREE_CODE (member) == SCOPE_REF
17511 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17513 /* Lookup the template functions now that we know what the
17514 scope is. */
17515 tree scope = TREE_OPERAND (member, 0);
17516 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17517 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17518 member = lookup_qualified_name (scope, tmpl,
17519 /*is_type_p=*/false,
17520 /*complain=*/false);
17521 if (BASELINK_P (member))
17523 BASELINK_FUNCTIONS (member)
17524 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17525 args);
17526 member = (adjust_result_of_qualified_name_lookup
17527 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17528 object_type));
17530 else
17532 qualified_name_lookup_error (scope, tmpl, member,
17533 input_location);
17534 RETURN (error_mark_node);
17537 else if (TREE_CODE (member) == SCOPE_REF
17538 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17539 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17541 if (complain & tf_error)
17543 if (TYPE_P (TREE_OPERAND (member, 0)))
17544 error ("%qT is not a class or namespace",
17545 TREE_OPERAND (member, 0));
17546 else
17547 error ("%qD is not a class or namespace",
17548 TREE_OPERAND (member, 0));
17550 RETURN (error_mark_node);
17553 r = finish_class_member_access_expr (object, member,
17554 /*template_p=*/false,
17555 complain);
17556 if (TREE_CODE (r) == COMPONENT_REF)
17557 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17558 RETURN (r);
17561 case THROW_EXPR:
17562 RETURN (build_throw
17563 (RECUR (TREE_OPERAND (t, 0))));
17565 case CONSTRUCTOR:
17567 vec<constructor_elt, va_gc> *n;
17568 constructor_elt *ce;
17569 unsigned HOST_WIDE_INT idx;
17570 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17571 bool process_index_p;
17572 int newlen;
17573 bool need_copy_p = false;
17574 tree r;
17576 if (type == error_mark_node)
17577 RETURN (error_mark_node);
17579 /* digest_init will do the wrong thing if we let it. */
17580 if (type && TYPE_PTRMEMFUNC_P (type))
17581 RETURN (t);
17583 /* We do not want to process the index of aggregate
17584 initializers as they are identifier nodes which will be
17585 looked up by digest_init. */
17586 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17588 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17589 newlen = vec_safe_length (n);
17590 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17592 if (ce->index && process_index_p
17593 /* An identifier index is looked up in the type
17594 being initialized, not the current scope. */
17595 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17596 ce->index = RECUR (ce->index);
17598 if (PACK_EXPANSION_P (ce->value))
17600 /* Substitute into the pack expansion. */
17601 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17602 in_decl);
17604 if (ce->value == error_mark_node
17605 || PACK_EXPANSION_P (ce->value))
17607 else if (TREE_VEC_LENGTH (ce->value) == 1)
17608 /* Just move the argument into place. */
17609 ce->value = TREE_VEC_ELT (ce->value, 0);
17610 else
17612 /* Update the length of the final CONSTRUCTOR
17613 arguments vector, and note that we will need to
17614 copy.*/
17615 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17616 need_copy_p = true;
17619 else
17620 ce->value = RECUR (ce->value);
17623 if (need_copy_p)
17625 vec<constructor_elt, va_gc> *old_n = n;
17627 vec_alloc (n, newlen);
17628 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17630 if (TREE_CODE (ce->value) == TREE_VEC)
17632 int i, len = TREE_VEC_LENGTH (ce->value);
17633 for (i = 0; i < len; ++i)
17634 CONSTRUCTOR_APPEND_ELT (n, 0,
17635 TREE_VEC_ELT (ce->value, i));
17637 else
17638 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17642 r = build_constructor (init_list_type_node, n);
17643 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17645 if (TREE_HAS_CONSTRUCTOR (t))
17647 fcl_t cl = fcl_functional;
17648 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
17649 cl = fcl_c99;
17650 RETURN (finish_compound_literal (type, r, complain, cl));
17653 TREE_TYPE (r) = type;
17654 RETURN (r);
17657 case TYPEID_EXPR:
17659 tree operand_0 = TREE_OPERAND (t, 0);
17660 if (TYPE_P (operand_0))
17662 operand_0 = tsubst (operand_0, args, complain, in_decl);
17663 RETURN (get_typeid (operand_0, complain));
17665 else
17667 operand_0 = RECUR (operand_0);
17668 RETURN (build_typeid (operand_0, complain));
17672 case VAR_DECL:
17673 if (!args)
17674 RETURN (t);
17675 else if (DECL_PACK_P (t))
17677 /* We don't build decls for an instantiation of a
17678 variadic capture proxy, we instantiate the elements
17679 when needed. */
17680 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17681 return RECUR (DECL_VALUE_EXPR (t));
17683 /* Fall through */
17685 case PARM_DECL:
17687 tree r = tsubst_copy (t, args, complain, in_decl);
17688 /* ??? We're doing a subset of finish_id_expression here. */
17689 if (VAR_P (r)
17690 && !processing_template_decl
17691 && !cp_unevaluated_operand
17692 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17693 && CP_DECL_THREAD_LOCAL_P (r))
17695 if (tree wrap = get_tls_wrapper_fn (r))
17696 /* Replace an evaluated use of the thread_local variable with
17697 a call to its wrapper. */
17698 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17700 else if (outer_automatic_var_p (r))
17702 r = process_outer_var_ref (r, complain);
17703 if (is_capture_proxy (r))
17704 register_local_specialization (r, t);
17707 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17708 /* If the original type was a reference, we'll be wrapped in
17709 the appropriate INDIRECT_REF. */
17710 r = convert_from_reference (r);
17711 RETURN (r);
17714 case VA_ARG_EXPR:
17716 tree op0 = RECUR (TREE_OPERAND (t, 0));
17717 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17718 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17721 case OFFSETOF_EXPR:
17723 tree object_ptr
17724 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17725 in_decl, /*function_p=*/false,
17726 /*integral_constant_expression_p=*/false);
17727 RETURN (finish_offsetof (object_ptr,
17728 RECUR (TREE_OPERAND (t, 0)),
17729 EXPR_LOCATION (t)));
17732 case ADDRESSOF_EXPR:
17733 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17734 RECUR (TREE_OPERAND (t, 0)), complain));
17736 case TRAIT_EXPR:
17738 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17739 complain, in_decl);
17741 tree type2 = TRAIT_EXPR_TYPE2 (t);
17742 if (type2 && TREE_CODE (type2) == TREE_LIST)
17743 type2 = RECUR (type2);
17744 else if (type2)
17745 type2 = tsubst (type2, args, complain, in_decl);
17747 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17750 case STMT_EXPR:
17752 tree old_stmt_expr = cur_stmt_expr;
17753 tree stmt_expr = begin_stmt_expr ();
17755 cur_stmt_expr = stmt_expr;
17756 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17757 integral_constant_expression_p);
17758 stmt_expr = finish_stmt_expr (stmt_expr, false);
17759 cur_stmt_expr = old_stmt_expr;
17761 /* If the resulting list of expression statement is empty,
17762 fold it further into void_node. */
17763 if (empty_expr_stmt_p (stmt_expr))
17764 stmt_expr = void_node;
17766 RETURN (stmt_expr);
17769 case LAMBDA_EXPR:
17771 tree r = build_lambda_expr ();
17773 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17774 LAMBDA_EXPR_CLOSURE (r) = type;
17775 CLASSTYPE_LAMBDA_EXPR (type) = r;
17777 LAMBDA_EXPR_LOCATION (r)
17778 = LAMBDA_EXPR_LOCATION (t);
17779 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17780 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17781 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17782 LAMBDA_EXPR_DISCRIMINATOR (r)
17783 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17784 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17785 if (!scope)
17786 /* No substitution needed. */;
17787 else if (VAR_OR_FUNCTION_DECL_P (scope))
17788 /* For a function or variable scope, we want to use tsubst so that we
17789 don't complain about referring to an auto before deduction. */
17790 scope = tsubst (scope, args, complain, in_decl);
17791 else if (TREE_CODE (scope) == PARM_DECL)
17793 /* Look up the parameter we want directly, as tsubst_copy
17794 doesn't do what we need. */
17795 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17796 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17797 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17798 parm = DECL_CHAIN (parm);
17799 scope = parm;
17800 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17801 if (DECL_CONTEXT (scope) == NULL_TREE)
17802 DECL_CONTEXT (scope) = fn;
17804 else if (TREE_CODE (scope) == FIELD_DECL)
17805 /* For a field, use tsubst_copy so that we look up the existing field
17806 rather than build a new one. */
17807 scope = RECUR (scope);
17808 else
17809 gcc_unreachable ();
17810 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17812 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17813 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17815 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17816 determine_visibility (TYPE_NAME (type));
17817 /* Now that we know visibility, instantiate the type so we have a
17818 declaration of the op() for later calls to lambda_function. */
17819 complete_type (type);
17821 if (tree fn = lambda_function (type))
17822 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17824 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17826 insert_pending_capture_proxies ();
17828 RETURN (build_lambda_object (r));
17831 case TARGET_EXPR:
17832 /* We can get here for a constant initializer of non-dependent type.
17833 FIXME stop folding in cp_parser_initializer_clause. */
17835 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17836 complain);
17837 RETURN (r);
17840 case TRANSACTION_EXPR:
17841 RETURN (tsubst_expr(t, args, complain, in_decl,
17842 integral_constant_expression_p));
17844 case PAREN_EXPR:
17845 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17847 case VEC_PERM_EXPR:
17849 tree op0 = RECUR (TREE_OPERAND (t, 0));
17850 tree op1 = RECUR (TREE_OPERAND (t, 1));
17851 tree op2 = RECUR (TREE_OPERAND (t, 2));
17852 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17853 complain));
17856 case REQUIRES_EXPR:
17857 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17859 default:
17860 /* Handle Objective-C++ constructs, if appropriate. */
17862 tree subst
17863 = objcp_tsubst_copy_and_build (t, args, complain,
17864 in_decl, /*function_p=*/false);
17865 if (subst)
17866 RETURN (subst);
17868 RETURN (tsubst_copy (t, args, complain, in_decl));
17871 #undef RECUR
17872 #undef RETURN
17873 out:
17874 input_location = loc;
17875 return retval;
17878 /* Verify that the instantiated ARGS are valid. For type arguments,
17879 make sure that the type's linkage is ok. For non-type arguments,
17880 make sure they are constants if they are integral or enumerations.
17881 Emit an error under control of COMPLAIN, and return TRUE on error. */
17883 static bool
17884 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
17886 if (dependent_template_arg_p (t))
17887 return false;
17888 if (ARGUMENT_PACK_P (t))
17890 tree vec = ARGUMENT_PACK_ARGS (t);
17891 int len = TREE_VEC_LENGTH (vec);
17892 bool result = false;
17893 int i;
17895 for (i = 0; i < len; ++i)
17896 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
17897 result = true;
17898 return result;
17900 else if (TYPE_P (t))
17902 /* [basic.link]: A name with no linkage (notably, the name
17903 of a class or enumeration declared in a local scope)
17904 shall not be used to declare an entity with linkage.
17905 This implies that names with no linkage cannot be used as
17906 template arguments
17908 DR 757 relaxes this restriction for C++0x. */
17909 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
17910 : no_linkage_check (t, /*relaxed_p=*/false));
17912 if (nt)
17914 /* DR 488 makes use of a type with no linkage cause
17915 type deduction to fail. */
17916 if (complain & tf_error)
17918 if (TYPE_UNNAMED_P (nt))
17919 error ("%qT is/uses unnamed type", t);
17920 else
17921 error ("template argument for %qD uses local type %qT",
17922 tmpl, t);
17924 return true;
17926 /* In order to avoid all sorts of complications, we do not
17927 allow variably-modified types as template arguments. */
17928 else if (variably_modified_type_p (t, NULL_TREE))
17930 if (complain & tf_error)
17931 error ("%qT is a variably modified type", t);
17932 return true;
17935 /* Class template and alias template arguments should be OK. */
17936 else if (DECL_TYPE_TEMPLATE_P (t))
17938 /* A non-type argument of integral or enumerated type must be a
17939 constant. */
17940 else if (TREE_TYPE (t)
17941 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
17942 && !REFERENCE_REF_P (t)
17943 && !TREE_CONSTANT (t))
17945 if (complain & tf_error)
17946 error ("integral expression %qE is not constant", t);
17947 return true;
17949 return false;
17952 static bool
17953 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
17955 int ix, len = DECL_NTPARMS (tmpl);
17956 bool result = false;
17958 for (ix = 0; ix != len; ix++)
17960 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
17961 result = true;
17963 if (result && (complain & tf_error))
17964 error (" trying to instantiate %qD", tmpl);
17965 return result;
17968 /* We're out of SFINAE context now, so generate diagnostics for the access
17969 errors we saw earlier when instantiating D from TMPL and ARGS. */
17971 static void
17972 recheck_decl_substitution (tree d, tree tmpl, tree args)
17974 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
17975 tree type = TREE_TYPE (pattern);
17976 location_t loc = input_location;
17978 push_access_scope (d);
17979 push_deferring_access_checks (dk_no_deferred);
17980 input_location = DECL_SOURCE_LOCATION (pattern);
17981 tsubst (type, args, tf_warning_or_error, d);
17982 input_location = loc;
17983 pop_deferring_access_checks ();
17984 pop_access_scope (d);
17987 /* Instantiate the indicated variable, function, or alias template TMPL with
17988 the template arguments in TARG_PTR. */
17990 static tree
17991 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
17993 tree targ_ptr = orig_args;
17994 tree fndecl;
17995 tree gen_tmpl;
17996 tree spec;
17997 bool access_ok = true;
17999 if (tmpl == error_mark_node)
18000 return error_mark_node;
18002 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18004 /* If this function is a clone, handle it specially. */
18005 if (DECL_CLONED_FUNCTION_P (tmpl))
18007 tree spec;
18008 tree clone;
18010 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18011 DECL_CLONED_FUNCTION. */
18012 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18013 targ_ptr, complain);
18014 if (spec == error_mark_node)
18015 return error_mark_node;
18017 /* Look for the clone. */
18018 FOR_EACH_CLONE (clone, spec)
18019 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18020 return clone;
18021 /* We should always have found the clone by now. */
18022 gcc_unreachable ();
18023 return NULL_TREE;
18026 if (targ_ptr == error_mark_node)
18027 return error_mark_node;
18029 /* Check to see if we already have this specialization. */
18030 gen_tmpl = most_general_template (tmpl);
18031 if (TMPL_ARGS_DEPTH (targ_ptr)
18032 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18033 /* targ_ptr only has the innermost template args, so add the outer ones
18034 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18035 the case of a non-dependent call within a template definition). */
18036 targ_ptr = (add_outermost_template_args
18037 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18038 targ_ptr));
18040 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18041 but it doesn't seem to be on the hot path. */
18042 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18044 gcc_assert (tmpl == gen_tmpl
18045 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18046 == spec)
18047 || fndecl == NULL_TREE);
18049 if (spec != NULL_TREE)
18051 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18053 if (complain & tf_error)
18054 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18055 return error_mark_node;
18057 return spec;
18060 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18061 complain))
18062 return error_mark_node;
18064 /* We are building a FUNCTION_DECL, during which the access of its
18065 parameters and return types have to be checked. However this
18066 FUNCTION_DECL which is the desired context for access checking
18067 is not built yet. We solve this chicken-and-egg problem by
18068 deferring all checks until we have the FUNCTION_DECL. */
18069 push_deferring_access_checks (dk_deferred);
18071 /* Instantiation of the function happens in the context of the function
18072 template, not the context of the overload resolution we're doing. */
18073 push_to_top_level ();
18074 /* If there are dependent arguments, e.g. because we're doing partial
18075 ordering, make sure processing_template_decl stays set. */
18076 if (uses_template_parms (targ_ptr))
18077 ++processing_template_decl;
18078 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18080 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18081 complain, gen_tmpl, true);
18082 push_nested_class (ctx);
18085 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18087 fndecl = NULL_TREE;
18088 if (VAR_P (pattern))
18090 /* We need to determine if we're using a partial or explicit
18091 specialization now, because the type of the variable could be
18092 different. */
18093 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18094 tree elt = most_specialized_partial_spec (tid, complain);
18095 if (elt == error_mark_node)
18096 pattern = error_mark_node;
18097 else if (elt)
18099 tree partial_tmpl = TREE_VALUE (elt);
18100 tree partial_args = TREE_PURPOSE (elt);
18101 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18102 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18106 /* Substitute template parameters to obtain the specialization. */
18107 if (fndecl == NULL_TREE)
18108 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18109 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18110 pop_nested_class ();
18111 pop_from_top_level ();
18113 if (fndecl == error_mark_node)
18115 pop_deferring_access_checks ();
18116 return error_mark_node;
18119 /* The DECL_TI_TEMPLATE should always be the immediate parent
18120 template, not the most general template. */
18121 DECL_TI_TEMPLATE (fndecl) = tmpl;
18122 DECL_TI_ARGS (fndecl) = targ_ptr;
18124 /* Now we know the specialization, compute access previously
18125 deferred. Do no access control for inheriting constructors,
18126 as we already checked access for the inherited constructor. */
18127 if (!(flag_new_inheriting_ctors
18128 && DECL_INHERITED_CTOR (fndecl)))
18130 push_access_scope (fndecl);
18131 if (!perform_deferred_access_checks (complain))
18132 access_ok = false;
18133 pop_access_scope (fndecl);
18135 pop_deferring_access_checks ();
18137 /* If we've just instantiated the main entry point for a function,
18138 instantiate all the alternate entry points as well. We do this
18139 by cloning the instantiation of the main entry point, not by
18140 instantiating the template clones. */
18141 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18142 clone_function_decl (fndecl, /*update_methods=*/false);
18144 if (!access_ok)
18146 if (!(complain & tf_error))
18148 /* Remember to reinstantiate when we're out of SFINAE so the user
18149 can see the errors. */
18150 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18152 return error_mark_node;
18154 return fndecl;
18157 /* Wrapper for instantiate_template_1. */
18159 tree
18160 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18162 tree ret;
18163 timevar_push (TV_TEMPLATE_INST);
18164 ret = instantiate_template_1 (tmpl, orig_args, complain);
18165 timevar_pop (TV_TEMPLATE_INST);
18166 return ret;
18169 /* Instantiate the alias template TMPL with ARGS. Also push a template
18170 instantiation level, which instantiate_template doesn't do because
18171 functions and variables have sufficient context established by the
18172 callers. */
18174 static tree
18175 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18177 struct pending_template *old_last_pend = last_pending_template;
18178 struct tinst_level *old_error_tinst = last_error_tinst_level;
18179 if (tmpl == error_mark_node || args == error_mark_node)
18180 return error_mark_node;
18181 tree tinst = build_tree_list (tmpl, args);
18182 if (!push_tinst_level (tinst))
18184 ggc_free (tinst);
18185 return error_mark_node;
18188 args =
18189 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18190 args, tmpl, complain,
18191 /*require_all_args=*/true,
18192 /*use_default_args=*/true);
18194 tree r = instantiate_template (tmpl, args, complain);
18195 pop_tinst_level ();
18196 /* We can't free this if a pending_template entry or last_error_tinst_level
18197 is pointing at it. */
18198 if (last_pending_template == old_last_pend
18199 && last_error_tinst_level == old_error_tinst)
18200 ggc_free (tinst);
18202 return r;
18205 /* PARM is a template parameter pack for FN. Returns true iff
18206 PARM is used in a deducible way in the argument list of FN. */
18208 static bool
18209 pack_deducible_p (tree parm, tree fn)
18211 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18212 for (; t; t = TREE_CHAIN (t))
18214 tree type = TREE_VALUE (t);
18215 tree packs;
18216 if (!PACK_EXPANSION_P (type))
18217 continue;
18218 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18219 packs; packs = TREE_CHAIN (packs))
18220 if (template_args_equal (TREE_VALUE (packs), parm))
18222 /* The template parameter pack is used in a function parameter
18223 pack. If this is the end of the parameter list, the
18224 template parameter pack is deducible. */
18225 if (TREE_CHAIN (t) == void_list_node)
18226 return true;
18227 else
18228 /* Otherwise, not. Well, it could be deduced from
18229 a non-pack parameter, but doing so would end up with
18230 a deduction mismatch, so don't bother. */
18231 return false;
18234 /* The template parameter pack isn't used in any function parameter
18235 packs, but it might be used deeper, e.g. tuple<Args...>. */
18236 return true;
18239 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18240 NARGS elements of the arguments that are being used when calling
18241 it. TARGS is a vector into which the deduced template arguments
18242 are placed.
18244 Returns either a FUNCTION_DECL for the matching specialization of FN or
18245 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18246 true, diagnostics will be printed to explain why it failed.
18248 If FN is a conversion operator, or we are trying to produce a specific
18249 specialization, RETURN_TYPE is the return type desired.
18251 The EXPLICIT_TARGS are explicit template arguments provided via a
18252 template-id.
18254 The parameter STRICT is one of:
18256 DEDUCE_CALL:
18257 We are deducing arguments for a function call, as in
18258 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18259 deducing arguments for a call to the result of a conversion
18260 function template, as in [over.call.object].
18262 DEDUCE_CONV:
18263 We are deducing arguments for a conversion function, as in
18264 [temp.deduct.conv].
18266 DEDUCE_EXACT:
18267 We are deducing arguments when doing an explicit instantiation
18268 as in [temp.explicit], when determining an explicit specialization
18269 as in [temp.expl.spec], or when taking the address of a function
18270 template, as in [temp.deduct.funcaddr]. */
18272 tree
18273 fn_type_unification (tree fn,
18274 tree explicit_targs,
18275 tree targs,
18276 const tree *args,
18277 unsigned int nargs,
18278 tree return_type,
18279 unification_kind_t strict,
18280 int flags,
18281 bool explain_p,
18282 bool decltype_p)
18284 tree parms;
18285 tree fntype;
18286 tree decl = NULL_TREE;
18287 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18288 bool ok;
18289 static int deduction_depth;
18290 struct pending_template *old_last_pend = last_pending_template;
18291 struct tinst_level *old_error_tinst = last_error_tinst_level;
18293 tree orig_fn = fn;
18294 if (flag_new_inheriting_ctors)
18295 fn = strip_inheriting_ctors (fn);
18297 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18298 tree tinst;
18299 tree r = error_mark_node;
18301 tree full_targs = targs;
18302 if (TMPL_ARGS_DEPTH (targs)
18303 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18304 full_targs = (add_outermost_template_args
18305 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18306 targs));
18308 if (decltype_p)
18309 complain |= tf_decltype;
18311 /* In C++0x, it's possible to have a function template whose type depends
18312 on itself recursively. This is most obvious with decltype, but can also
18313 occur with enumeration scope (c++/48969). So we need to catch infinite
18314 recursion and reject the substitution at deduction time; this function
18315 will return error_mark_node for any repeated substitution.
18317 This also catches excessive recursion such as when f<N> depends on
18318 f<N-1> across all integers, and returns error_mark_node for all the
18319 substitutions back up to the initial one.
18321 This is, of course, not reentrant. */
18322 if (excessive_deduction_depth)
18323 return error_mark_node;
18324 tinst = build_tree_list (fn, NULL_TREE);
18325 ++deduction_depth;
18327 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18329 fntype = TREE_TYPE (fn);
18330 if (explicit_targs)
18332 /* [temp.deduct]
18334 The specified template arguments must match the template
18335 parameters in kind (i.e., type, nontype, template), and there
18336 must not be more arguments than there are parameters;
18337 otherwise type deduction fails.
18339 Nontype arguments must match the types of the corresponding
18340 nontype template parameters, or must be convertible to the
18341 types of the corresponding nontype parameters as specified in
18342 _temp.arg.nontype_, otherwise type deduction fails.
18344 All references in the function type of the function template
18345 to the corresponding template parameters are replaced by the
18346 specified template argument values. If a substitution in a
18347 template parameter or in the function type of the function
18348 template results in an invalid type, type deduction fails. */
18349 int i, len = TREE_VEC_LENGTH (tparms);
18350 location_t loc = input_location;
18351 bool incomplete = false;
18353 if (explicit_targs == error_mark_node)
18354 goto fail;
18356 if (TMPL_ARGS_DEPTH (explicit_targs)
18357 < TMPL_ARGS_DEPTH (full_targs))
18358 explicit_targs = add_outermost_template_args (full_targs,
18359 explicit_targs);
18361 /* Adjust any explicit template arguments before entering the
18362 substitution context. */
18363 explicit_targs
18364 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18365 complain,
18366 /*require_all_args=*/false,
18367 /*use_default_args=*/false));
18368 if (explicit_targs == error_mark_node)
18369 goto fail;
18371 /* Substitute the explicit args into the function type. This is
18372 necessary so that, for instance, explicitly declared function
18373 arguments can match null pointed constants. If we were given
18374 an incomplete set of explicit args, we must not do semantic
18375 processing during substitution as we could create partial
18376 instantiations. */
18377 for (i = 0; i < len; i++)
18379 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18380 bool parameter_pack = false;
18381 tree targ = TREE_VEC_ELT (explicit_targs, i);
18383 /* Dig out the actual parm. */
18384 if (TREE_CODE (parm) == TYPE_DECL
18385 || TREE_CODE (parm) == TEMPLATE_DECL)
18387 parm = TREE_TYPE (parm);
18388 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18390 else if (TREE_CODE (parm) == PARM_DECL)
18392 parm = DECL_INITIAL (parm);
18393 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18396 if (!parameter_pack && targ == NULL_TREE)
18397 /* No explicit argument for this template parameter. */
18398 incomplete = true;
18400 if (parameter_pack && pack_deducible_p (parm, fn))
18402 /* Mark the argument pack as "incomplete". We could
18403 still deduce more arguments during unification.
18404 We remove this mark in type_unification_real. */
18405 if (targ)
18407 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18408 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18409 = ARGUMENT_PACK_ARGS (targ);
18412 /* We have some incomplete argument packs. */
18413 incomplete = true;
18417 TREE_VALUE (tinst) = explicit_targs;
18418 if (!push_tinst_level (tinst))
18420 excessive_deduction_depth = true;
18421 goto fail;
18423 processing_template_decl += incomplete;
18424 input_location = DECL_SOURCE_LOCATION (fn);
18425 /* Ignore any access checks; we'll see them again in
18426 instantiate_template and they might have the wrong
18427 access path at this point. */
18428 push_deferring_access_checks (dk_deferred);
18429 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18430 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18431 pop_deferring_access_checks ();
18432 input_location = loc;
18433 processing_template_decl -= incomplete;
18434 pop_tinst_level ();
18436 if (fntype == error_mark_node)
18437 goto fail;
18439 /* Place the explicitly specified arguments in TARGS. */
18440 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18441 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18442 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18445 /* Never do unification on the 'this' parameter. */
18446 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18448 if (return_type && strict == DEDUCE_CALL)
18450 /* We're deducing for a call to the result of a template conversion
18451 function. The parms we really want are in return_type. */
18452 if (POINTER_TYPE_P (return_type))
18453 return_type = TREE_TYPE (return_type);
18454 parms = TYPE_ARG_TYPES (return_type);
18456 else if (return_type)
18458 tree *new_args;
18460 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18461 new_args = XALLOCAVEC (tree, nargs + 1);
18462 new_args[0] = return_type;
18463 memcpy (new_args + 1, args, nargs * sizeof (tree));
18464 args = new_args;
18465 ++nargs;
18468 /* We allow incomplete unification without an error message here
18469 because the standard doesn't seem to explicitly prohibit it. Our
18470 callers must be ready to deal with unification failures in any
18471 event. */
18473 TREE_VALUE (tinst) = targs;
18474 /* If we aren't explaining yet, push tinst context so we can see where
18475 any errors (e.g. from class instantiations triggered by instantiation
18476 of default template arguments) come from. If we are explaining, this
18477 context is redundant. */
18478 if (!explain_p && !push_tinst_level (tinst))
18480 excessive_deduction_depth = true;
18481 goto fail;
18484 /* type_unification_real will pass back any access checks from default
18485 template argument substitution. */
18486 vec<deferred_access_check, va_gc> *checks;
18487 checks = NULL;
18489 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18490 full_targs, parms, args, nargs, /*subr=*/0,
18491 strict, flags, &checks, explain_p);
18492 if (!explain_p)
18493 pop_tinst_level ();
18494 if (!ok)
18495 goto fail;
18497 /* Now that we have bindings for all of the template arguments,
18498 ensure that the arguments deduced for the template template
18499 parameters have compatible template parameter lists. We cannot
18500 check this property before we have deduced all template
18501 arguments, because the template parameter types of a template
18502 template parameter might depend on prior template parameters
18503 deduced after the template template parameter. The following
18504 ill-formed example illustrates this issue:
18506 template<typename T, template<T> class C> void f(C<5>, T);
18508 template<int N> struct X {};
18510 void g() {
18511 f(X<5>(), 5l); // error: template argument deduction fails
18514 The template parameter list of 'C' depends on the template type
18515 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18516 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18517 time that we deduce 'C'. */
18518 if (!template_template_parm_bindings_ok_p
18519 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18521 unify_inconsistent_template_template_parameters (explain_p);
18522 goto fail;
18525 /* All is well so far. Now, check:
18527 [temp.deduct]
18529 When all template arguments have been deduced, all uses of
18530 template parameters in nondeduced contexts are replaced with
18531 the corresponding deduced argument values. If the
18532 substitution results in an invalid type, as described above,
18533 type deduction fails. */
18534 TREE_VALUE (tinst) = targs;
18535 if (!push_tinst_level (tinst))
18537 excessive_deduction_depth = true;
18538 goto fail;
18541 /* Also collect access checks from the instantiation. */
18542 reopen_deferring_access_checks (checks);
18544 decl = instantiate_template (fn, targs, complain);
18546 checks = get_deferred_access_checks ();
18547 pop_deferring_access_checks ();
18549 pop_tinst_level ();
18551 if (decl == error_mark_node)
18552 goto fail;
18554 /* Now perform any access checks encountered during substitution. */
18555 push_access_scope (decl);
18556 ok = perform_access_checks (checks, complain);
18557 pop_access_scope (decl);
18558 if (!ok)
18559 goto fail;
18561 /* If we're looking for an exact match, check that what we got
18562 is indeed an exact match. It might not be if some template
18563 parameters are used in non-deduced contexts. But don't check
18564 for an exact match if we have dependent template arguments;
18565 in that case we're doing partial ordering, and we already know
18566 that we have two candidates that will provide the actual type. */
18567 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18569 tree substed = TREE_TYPE (decl);
18570 unsigned int i;
18572 tree sarg
18573 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18574 if (return_type)
18575 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18576 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18577 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18579 unify_type_mismatch (explain_p, args[i],
18580 TREE_VALUE (sarg));
18581 goto fail;
18585 /* After doing deduction with the inherited constructor, actually return an
18586 instantiation of the inheriting constructor. */
18587 if (orig_fn != fn)
18588 decl = instantiate_template (orig_fn, targs, complain);
18590 r = decl;
18592 fail:
18593 --deduction_depth;
18594 if (excessive_deduction_depth)
18596 if (deduction_depth == 0)
18597 /* Reset once we're all the way out. */
18598 excessive_deduction_depth = false;
18601 /* We can't free this if a pending_template entry or last_error_tinst_level
18602 is pointing at it. */
18603 if (last_pending_template == old_last_pend
18604 && last_error_tinst_level == old_error_tinst)
18605 ggc_free (tinst);
18607 return r;
18610 /* Adjust types before performing type deduction, as described in
18611 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18612 sections are symmetric. PARM is the type of a function parameter
18613 or the return type of the conversion function. ARG is the type of
18614 the argument passed to the call, or the type of the value
18615 initialized with the result of the conversion function.
18616 ARG_EXPR is the original argument expression, which may be null. */
18618 static int
18619 maybe_adjust_types_for_deduction (unification_kind_t strict,
18620 tree* parm,
18621 tree* arg,
18622 tree arg_expr)
18624 int result = 0;
18626 switch (strict)
18628 case DEDUCE_CALL:
18629 break;
18631 case DEDUCE_CONV:
18632 /* Swap PARM and ARG throughout the remainder of this
18633 function; the handling is precisely symmetric since PARM
18634 will initialize ARG rather than vice versa. */
18635 std::swap (parm, arg);
18636 break;
18638 case DEDUCE_EXACT:
18639 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18640 too, but here handle it by stripping the reference from PARM
18641 rather than by adding it to ARG. */
18642 if (TREE_CODE (*parm) == REFERENCE_TYPE
18643 && TYPE_REF_IS_RVALUE (*parm)
18644 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18645 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18646 && TREE_CODE (*arg) == REFERENCE_TYPE
18647 && !TYPE_REF_IS_RVALUE (*arg))
18648 *parm = TREE_TYPE (*parm);
18649 /* Nothing else to do in this case. */
18650 return 0;
18652 default:
18653 gcc_unreachable ();
18656 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18658 /* [temp.deduct.call]
18660 If P is not a reference type:
18662 --If A is an array type, the pointer type produced by the
18663 array-to-pointer standard conversion (_conv.array_) is
18664 used in place of A for type deduction; otherwise,
18666 --If A is a function type, the pointer type produced by
18667 the function-to-pointer standard conversion
18668 (_conv.func_) is used in place of A for type deduction;
18669 otherwise,
18671 --If A is a cv-qualified type, the top level
18672 cv-qualifiers of A's type are ignored for type
18673 deduction. */
18674 if (TREE_CODE (*arg) == ARRAY_TYPE)
18675 *arg = build_pointer_type (TREE_TYPE (*arg));
18676 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18677 *arg = build_pointer_type (*arg);
18678 else
18679 *arg = TYPE_MAIN_VARIANT (*arg);
18682 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18683 reference to a cv-unqualified template parameter that does not represent a
18684 template parameter of a class template (during class template argument
18685 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18686 an lvalue, the type "lvalue reference to A" is used in place of A for type
18687 deduction. */
18688 if (TREE_CODE (*parm) == REFERENCE_TYPE
18689 && TYPE_REF_IS_RVALUE (*parm)
18690 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18691 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18692 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18693 && (arg_expr ? lvalue_p (arg_expr)
18694 /* try_one_overload doesn't provide an arg_expr, but
18695 functions are always lvalues. */
18696 : TREE_CODE (*arg) == FUNCTION_TYPE))
18697 *arg = build_reference_type (*arg);
18699 /* [temp.deduct.call]
18701 If P is a cv-qualified type, the top level cv-qualifiers
18702 of P's type are ignored for type deduction. If P is a
18703 reference type, the type referred to by P is used for
18704 type deduction. */
18705 *parm = TYPE_MAIN_VARIANT (*parm);
18706 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18708 *parm = TREE_TYPE (*parm);
18709 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18712 /* DR 322. For conversion deduction, remove a reference type on parm
18713 too (which has been swapped into ARG). */
18714 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18715 *arg = TREE_TYPE (*arg);
18717 return result;
18720 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18721 template which does contain any deducible template parameters; check if
18722 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18723 unify_one_argument. */
18725 static int
18726 check_non_deducible_conversion (tree parm, tree arg, int strict,
18727 int flags, bool explain_p)
18729 tree type;
18731 if (!TYPE_P (arg))
18732 type = TREE_TYPE (arg);
18733 else
18734 type = arg;
18736 if (same_type_p (parm, type))
18737 return unify_success (explain_p);
18739 if (strict == DEDUCE_CONV)
18741 if (can_convert_arg (type, parm, NULL_TREE, flags,
18742 explain_p ? tf_warning_or_error : tf_none))
18743 return unify_success (explain_p);
18745 else if (strict != DEDUCE_EXACT)
18747 if (can_convert_arg (parm, type,
18748 TYPE_P (arg) ? NULL_TREE : arg,
18749 flags, explain_p ? tf_warning_or_error : tf_none))
18750 return unify_success (explain_p);
18753 if (strict == DEDUCE_EXACT)
18754 return unify_type_mismatch (explain_p, parm, arg);
18755 else
18756 return unify_arg_conversion (explain_p, parm, type, arg);
18759 static bool uses_deducible_template_parms (tree type);
18761 /* Returns true iff the expression EXPR is one from which a template
18762 argument can be deduced. In other words, if it's an undecorated
18763 use of a template non-type parameter. */
18765 static bool
18766 deducible_expression (tree expr)
18768 /* Strip implicit conversions. */
18769 while (CONVERT_EXPR_P (expr))
18770 expr = TREE_OPERAND (expr, 0);
18771 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18774 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18775 deducible way; that is, if it has a max value of <PARM> - 1. */
18777 static bool
18778 deducible_array_bound (tree domain)
18780 if (domain == NULL_TREE)
18781 return false;
18783 tree max = TYPE_MAX_VALUE (domain);
18784 if (TREE_CODE (max) != MINUS_EXPR)
18785 return false;
18787 return deducible_expression (TREE_OPERAND (max, 0));
18790 /* Returns true iff the template arguments ARGS use a template parameter
18791 in a deducible way. */
18793 static bool
18794 deducible_template_args (tree args)
18796 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18798 bool deducible;
18799 tree elt = TREE_VEC_ELT (args, i);
18800 if (ARGUMENT_PACK_P (elt))
18801 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18802 else
18804 if (PACK_EXPANSION_P (elt))
18805 elt = PACK_EXPANSION_PATTERN (elt);
18806 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18807 deducible = true;
18808 else if (TYPE_P (elt))
18809 deducible = uses_deducible_template_parms (elt);
18810 else
18811 deducible = deducible_expression (elt);
18813 if (deducible)
18814 return true;
18816 return false;
18819 /* Returns true iff TYPE contains any deducible references to template
18820 parameters, as per 14.8.2.5. */
18822 static bool
18823 uses_deducible_template_parms (tree type)
18825 if (PACK_EXPANSION_P (type))
18826 type = PACK_EXPANSION_PATTERN (type);
18828 /* T
18829 cv-list T
18830 TT<T>
18831 TT<i>
18832 TT<> */
18833 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18834 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18835 return true;
18837 /* T*
18839 T&& */
18840 if (POINTER_TYPE_P (type))
18841 return uses_deducible_template_parms (TREE_TYPE (type));
18843 /* T[integer-constant ]
18844 type [i] */
18845 if (TREE_CODE (type) == ARRAY_TYPE)
18846 return (uses_deducible_template_parms (TREE_TYPE (type))
18847 || deducible_array_bound (TYPE_DOMAIN (type)));
18849 /* T type ::*
18850 type T::*
18851 T T::*
18852 T (type ::*)()
18853 type (T::*)()
18854 type (type ::*)(T)
18855 type (T::*)(T)
18856 T (type ::*)(T)
18857 T (T::*)()
18858 T (T::*)(T) */
18859 if (TYPE_PTRMEM_P (type))
18860 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18861 || (uses_deducible_template_parms
18862 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18864 /* template-name <T> (where template-name refers to a class template)
18865 template-name <i> (where template-name refers to a class template) */
18866 if (CLASS_TYPE_P (type)
18867 && CLASSTYPE_TEMPLATE_INFO (type)
18868 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18869 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18870 (CLASSTYPE_TI_ARGS (type)));
18872 /* type (T)
18874 T(T) */
18875 if (TREE_CODE (type) == FUNCTION_TYPE
18876 || TREE_CODE (type) == METHOD_TYPE)
18878 if (uses_deducible_template_parms (TREE_TYPE (type)))
18879 return true;
18880 tree parm = TYPE_ARG_TYPES (type);
18881 if (TREE_CODE (type) == METHOD_TYPE)
18882 parm = TREE_CHAIN (parm);
18883 for (; parm; parm = TREE_CHAIN (parm))
18884 if (uses_deducible_template_parms (TREE_VALUE (parm)))
18885 return true;
18888 return false;
18891 /* Subroutine of type_unification_real and unify_pack_expansion to
18892 handle unification of a single P/A pair. Parameters are as
18893 for those functions. */
18895 static int
18896 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
18897 int subr, unification_kind_t strict,
18898 bool explain_p)
18900 tree arg_expr = NULL_TREE;
18901 int arg_strict;
18903 if (arg == error_mark_node || parm == error_mark_node)
18904 return unify_invalid (explain_p);
18905 if (arg == unknown_type_node)
18906 /* We can't deduce anything from this, but we might get all the
18907 template args from other function args. */
18908 return unify_success (explain_p);
18910 /* Implicit conversions (Clause 4) will be performed on a function
18911 argument to convert it to the type of the corresponding function
18912 parameter if the parameter type contains no template-parameters that
18913 participate in template argument deduction. */
18914 if (strict != DEDUCE_EXACT
18915 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
18916 /* For function parameters with no deducible template parameters,
18917 just return. We'll check non-dependent conversions later. */
18918 return unify_success (explain_p);
18920 switch (strict)
18922 case DEDUCE_CALL:
18923 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
18924 | UNIFY_ALLOW_MORE_CV_QUAL
18925 | UNIFY_ALLOW_DERIVED);
18926 break;
18928 case DEDUCE_CONV:
18929 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
18930 break;
18932 case DEDUCE_EXACT:
18933 arg_strict = UNIFY_ALLOW_NONE;
18934 break;
18936 default:
18937 gcc_unreachable ();
18940 /* We only do these transformations if this is the top-level
18941 parameter_type_list in a call or declaration matching; in other
18942 situations (nested function declarators, template argument lists) we
18943 won't be comparing a type to an expression, and we don't do any type
18944 adjustments. */
18945 if (!subr)
18947 if (!TYPE_P (arg))
18949 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
18950 if (type_unknown_p (arg))
18952 /* [temp.deduct.type] A template-argument can be
18953 deduced from a pointer to function or pointer
18954 to member function argument if the set of
18955 overloaded functions does not contain function
18956 templates and at most one of a set of
18957 overloaded functions provides a unique
18958 match. */
18960 if (resolve_overloaded_unification
18961 (tparms, targs, parm, arg, strict,
18962 arg_strict, explain_p))
18963 return unify_success (explain_p);
18964 return unify_overload_resolution_failure (explain_p, arg);
18967 arg_expr = arg;
18968 arg = unlowered_expr_type (arg);
18969 if (arg == error_mark_node)
18970 return unify_invalid (explain_p);
18973 arg_strict |=
18974 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
18976 else
18977 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
18978 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
18979 return unify_template_argument_mismatch (explain_p, parm, arg);
18981 /* For deduction from an init-list we need the actual list. */
18982 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
18983 arg = arg_expr;
18984 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
18987 /* for_each_template_parm callback that always returns 0. */
18989 static int
18990 zero_r (tree, void *)
18992 return 0;
18995 /* for_each_template_parm any_fn callback to handle deduction of a template
18996 type argument from the type of an array bound. */
18998 static int
18999 array_deduction_r (tree t, void *data)
19001 tree_pair_p d = (tree_pair_p)data;
19002 tree &tparms = d->purpose;
19003 tree &targs = d->value;
19005 if (TREE_CODE (t) == ARRAY_TYPE)
19006 if (tree dom = TYPE_DOMAIN (t))
19007 if (tree max = TYPE_MAX_VALUE (dom))
19009 if (TREE_CODE (max) == MINUS_EXPR)
19010 max = TREE_OPERAND (max, 0);
19011 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19012 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19013 UNIFY_ALLOW_NONE, /*explain*/false);
19016 /* Keep walking. */
19017 return 0;
19020 /* Try to deduce any not-yet-deduced template type arguments from the type of
19021 an array bound. This is handled separately from unify because 14.8.2.5 says
19022 "The type of a type parameter is only deduced from an array bound if it is
19023 not otherwise deduced." */
19025 static void
19026 try_array_deduction (tree tparms, tree targs, tree parm)
19028 tree_pair_s data = { tparms, targs };
19029 hash_set<tree> visited;
19030 for_each_template_parm (parm, zero_r, &data, &visited,
19031 /*nondeduced*/false, array_deduction_r);
19034 /* Most parms like fn_type_unification.
19036 If SUBR is 1, we're being called recursively (to unify the
19037 arguments of a function or method parameter of a function
19038 template).
19040 CHECKS is a pointer to a vector of access checks encountered while
19041 substituting default template arguments. */
19043 static int
19044 type_unification_real (tree tparms,
19045 tree full_targs,
19046 tree xparms,
19047 const tree *xargs,
19048 unsigned int xnargs,
19049 int subr,
19050 unification_kind_t strict,
19051 int flags,
19052 vec<deferred_access_check, va_gc> **checks,
19053 bool explain_p)
19055 tree parm, arg;
19056 int i;
19057 int ntparms = TREE_VEC_LENGTH (tparms);
19058 int saw_undeduced = 0;
19059 tree parms;
19060 const tree *args;
19061 unsigned int nargs;
19062 unsigned int ia;
19064 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19065 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19066 gcc_assert (ntparms > 0);
19068 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19070 /* Reset the number of non-defaulted template arguments contained
19071 in TARGS. */
19072 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19074 again:
19075 parms = xparms;
19076 args = xargs;
19077 nargs = xnargs;
19079 ia = 0;
19080 while (parms && parms != void_list_node
19081 && ia < nargs)
19083 parm = TREE_VALUE (parms);
19085 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19086 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19087 /* For a function parameter pack that occurs at the end of the
19088 parameter-declaration-list, the type A of each remaining
19089 argument of the call is compared with the type P of the
19090 declarator-id of the function parameter pack. */
19091 break;
19093 parms = TREE_CHAIN (parms);
19095 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19096 /* For a function parameter pack that does not occur at the
19097 end of the parameter-declaration-list, the type of the
19098 parameter pack is a non-deduced context. */
19099 continue;
19101 arg = args[ia];
19102 ++ia;
19104 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19105 explain_p))
19106 return 1;
19109 if (parms
19110 && parms != void_list_node
19111 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19113 /* Unify the remaining arguments with the pack expansion type. */
19114 tree argvec;
19115 tree parmvec = make_tree_vec (1);
19117 /* Allocate a TREE_VEC and copy in all of the arguments */
19118 argvec = make_tree_vec (nargs - ia);
19119 for (i = 0; ia < nargs; ++ia, ++i)
19120 TREE_VEC_ELT (argvec, i) = args[ia];
19122 /* Copy the parameter into parmvec. */
19123 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19124 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19125 /*subr=*/subr, explain_p))
19126 return 1;
19128 /* Advance to the end of the list of parameters. */
19129 parms = TREE_CHAIN (parms);
19132 /* Fail if we've reached the end of the parm list, and more args
19133 are present, and the parm list isn't variadic. */
19134 if (ia < nargs && parms == void_list_node)
19135 return unify_too_many_arguments (explain_p, nargs, ia);
19136 /* Fail if parms are left and they don't have default values and
19137 they aren't all deduced as empty packs (c++/57397). This is
19138 consistent with sufficient_parms_p. */
19139 if (parms && parms != void_list_node
19140 && TREE_PURPOSE (parms) == NULL_TREE)
19142 unsigned int count = nargs;
19143 tree p = parms;
19144 bool type_pack_p;
19147 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19148 if (!type_pack_p)
19149 count++;
19150 p = TREE_CHAIN (p);
19152 while (p && p != void_list_node);
19153 if (count != nargs)
19154 return unify_too_few_arguments (explain_p, ia, count,
19155 type_pack_p);
19158 if (!subr)
19160 tsubst_flags_t complain = (explain_p
19161 ? tf_warning_or_error
19162 : tf_none);
19163 bool tried_array_deduction = (cxx_dialect < cxx1z);
19165 for (i = 0; i < ntparms; i++)
19167 tree targ = TREE_VEC_ELT (targs, i);
19168 tree tparm = TREE_VEC_ELT (tparms, i);
19170 /* Clear the "incomplete" flags on all argument packs now so that
19171 substituting them into later default arguments works. */
19172 if (targ && ARGUMENT_PACK_P (targ))
19174 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19175 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19178 if (targ || tparm == error_mark_node)
19179 continue;
19180 tparm = TREE_VALUE (tparm);
19182 if (TREE_CODE (tparm) == TYPE_DECL
19183 && !tried_array_deduction)
19185 try_array_deduction (tparms, targs, xparms);
19186 tried_array_deduction = true;
19187 if (TREE_VEC_ELT (targs, i))
19188 continue;
19191 /* If this is an undeduced nontype parameter that depends on
19192 a type parameter, try another pass; its type may have been
19193 deduced from a later argument than the one from which
19194 this parameter can be deduced. */
19195 if (TREE_CODE (tparm) == PARM_DECL
19196 && uses_template_parms (TREE_TYPE (tparm))
19197 && saw_undeduced < 2)
19199 saw_undeduced = 1;
19200 continue;
19203 /* Core issue #226 (C++0x) [temp.deduct]:
19205 If a template argument has not been deduced, its
19206 default template argument, if any, is used.
19208 When we are in C++98 mode, TREE_PURPOSE will either
19209 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19210 to explicitly check cxx_dialect here. */
19211 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19212 /* OK, there is a default argument. Wait until after the
19213 conversion check to do substitution. */
19214 continue;
19216 /* If the type parameter is a parameter pack, then it will
19217 be deduced to an empty parameter pack. */
19218 if (template_parameter_pack_p (tparm))
19220 tree arg;
19222 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19224 arg = make_node (NONTYPE_ARGUMENT_PACK);
19225 TREE_CONSTANT (arg) = 1;
19227 else
19228 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19230 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19232 TREE_VEC_ELT (targs, i) = arg;
19233 continue;
19236 return unify_parameter_deduction_failure (explain_p, tparm);
19239 /* DR 1391: All parameters have args, now check non-dependent parms for
19240 convertibility. */
19241 if (saw_undeduced < 2)
19242 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19243 parms && parms != void_list_node && ia < nargs; )
19245 parm = TREE_VALUE (parms);
19247 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19248 && (!TREE_CHAIN (parms)
19249 || TREE_CHAIN (parms) == void_list_node))
19250 /* For a function parameter pack that occurs at the end of the
19251 parameter-declaration-list, the type A of each remaining
19252 argument of the call is compared with the type P of the
19253 declarator-id of the function parameter pack. */
19254 break;
19256 parms = TREE_CHAIN (parms);
19258 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19259 /* For a function parameter pack that does not occur at the
19260 end of the parameter-declaration-list, the type of the
19261 parameter pack is a non-deduced context. */
19262 continue;
19264 arg = args[ia];
19265 ++ia;
19267 if (uses_template_parms (parm))
19268 continue;
19269 if (check_non_deducible_conversion (parm, arg, strict, flags,
19270 explain_p))
19271 return 1;
19274 /* Now substitute into the default template arguments. */
19275 for (i = 0; i < ntparms; i++)
19277 tree targ = TREE_VEC_ELT (targs, i);
19278 tree tparm = TREE_VEC_ELT (tparms, i);
19280 if (targ || tparm == error_mark_node)
19281 continue;
19282 tree parm = TREE_VALUE (tparm);
19284 if (TREE_CODE (parm) == PARM_DECL
19285 && uses_template_parms (TREE_TYPE (parm))
19286 && saw_undeduced < 2)
19287 continue;
19289 tree arg = TREE_PURPOSE (tparm);
19290 reopen_deferring_access_checks (*checks);
19291 location_t save_loc = input_location;
19292 if (DECL_P (parm))
19293 input_location = DECL_SOURCE_LOCATION (parm);
19294 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19295 if (!uses_template_parms (arg))
19296 arg = convert_template_argument (parm, arg, full_targs, complain,
19297 i, NULL_TREE);
19298 else if (saw_undeduced < 2)
19299 arg = NULL_TREE;
19300 else
19301 arg = error_mark_node;
19302 input_location = save_loc;
19303 *checks = get_deferred_access_checks ();
19304 pop_deferring_access_checks ();
19305 if (arg == error_mark_node)
19306 return 1;
19307 else if (arg)
19309 TREE_VEC_ELT (targs, i) = arg;
19310 /* The position of the first default template argument,
19311 is also the number of non-defaulted arguments in TARGS.
19312 Record that. */
19313 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19314 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19318 if (saw_undeduced++ == 1)
19319 goto again;
19322 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19323 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19325 return unify_success (explain_p);
19328 /* Subroutine of type_unification_real. Args are like the variables
19329 at the call site. ARG is an overloaded function (or template-id);
19330 we try deducing template args from each of the overloads, and if
19331 only one succeeds, we go with that. Modifies TARGS and returns
19332 true on success. */
19334 static bool
19335 resolve_overloaded_unification (tree tparms,
19336 tree targs,
19337 tree parm,
19338 tree arg,
19339 unification_kind_t strict,
19340 int sub_strict,
19341 bool explain_p)
19343 tree tempargs = copy_node (targs);
19344 int good = 0;
19345 tree goodfn = NULL_TREE;
19346 bool addr_p;
19348 if (TREE_CODE (arg) == ADDR_EXPR)
19350 arg = TREE_OPERAND (arg, 0);
19351 addr_p = true;
19353 else
19354 addr_p = false;
19356 if (TREE_CODE (arg) == COMPONENT_REF)
19357 /* Handle `&x' where `x' is some static or non-static member
19358 function name. */
19359 arg = TREE_OPERAND (arg, 1);
19361 if (TREE_CODE (arg) == OFFSET_REF)
19362 arg = TREE_OPERAND (arg, 1);
19364 /* Strip baselink information. */
19365 if (BASELINK_P (arg))
19366 arg = BASELINK_FUNCTIONS (arg);
19368 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19370 /* If we got some explicit template args, we need to plug them into
19371 the affected templates before we try to unify, in case the
19372 explicit args will completely resolve the templates in question. */
19374 int ok = 0;
19375 tree expl_subargs = TREE_OPERAND (arg, 1);
19376 arg = TREE_OPERAND (arg, 0);
19378 for (lkp_iterator iter (arg); iter; ++iter)
19380 tree fn = *iter;
19381 tree subargs, elem;
19383 if (TREE_CODE (fn) != TEMPLATE_DECL)
19384 continue;
19386 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19387 expl_subargs, NULL_TREE, tf_none,
19388 /*require_all_args=*/true,
19389 /*use_default_args=*/true);
19390 if (subargs != error_mark_node
19391 && !any_dependent_template_arguments_p (subargs))
19393 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19394 if (try_one_overload (tparms, targs, tempargs, parm,
19395 elem, strict, sub_strict, addr_p, explain_p)
19396 && (!goodfn || !same_type_p (goodfn, elem)))
19398 goodfn = elem;
19399 ++good;
19402 else if (subargs)
19403 ++ok;
19405 /* If no templates (or more than one) are fully resolved by the
19406 explicit arguments, this template-id is a non-deduced context; it
19407 could still be OK if we deduce all template arguments for the
19408 enclosing call through other arguments. */
19409 if (good != 1)
19410 good = ok;
19412 else if (TREE_CODE (arg) != OVERLOAD
19413 && TREE_CODE (arg) != FUNCTION_DECL)
19414 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19415 -- but the deduction does not succeed because the expression is
19416 not just the function on its own. */
19417 return false;
19418 else
19419 for (lkp_iterator iter (arg); iter; ++iter)
19421 tree fn = *iter;
19422 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19423 strict, sub_strict, addr_p, explain_p)
19424 && (!goodfn || !decls_match (goodfn, fn)))
19426 goodfn = fn;
19427 ++good;
19431 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19432 to function or pointer to member function argument if the set of
19433 overloaded functions does not contain function templates and at most
19434 one of a set of overloaded functions provides a unique match.
19436 So if we found multiple possibilities, we return success but don't
19437 deduce anything. */
19439 if (good == 1)
19441 int i = TREE_VEC_LENGTH (targs);
19442 for (; i--; )
19443 if (TREE_VEC_ELT (tempargs, i))
19445 tree old = TREE_VEC_ELT (targs, i);
19446 tree new_ = TREE_VEC_ELT (tempargs, i);
19447 if (new_ && old && ARGUMENT_PACK_P (old)
19448 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19449 /* Don't forget explicit template arguments in a pack. */
19450 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19451 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19452 TREE_VEC_ELT (targs, i) = new_;
19455 if (good)
19456 return true;
19458 return false;
19461 /* Core DR 115: In contexts where deduction is done and fails, or in
19462 contexts where deduction is not done, if a template argument list is
19463 specified and it, along with any default template arguments, identifies
19464 a single function template specialization, then the template-id is an
19465 lvalue for the function template specialization. */
19467 tree
19468 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19470 tree expr, offset, baselink;
19471 bool addr;
19473 if (!type_unknown_p (orig_expr))
19474 return orig_expr;
19476 expr = orig_expr;
19477 addr = false;
19478 offset = NULL_TREE;
19479 baselink = NULL_TREE;
19481 if (TREE_CODE (expr) == ADDR_EXPR)
19483 expr = TREE_OPERAND (expr, 0);
19484 addr = true;
19486 if (TREE_CODE (expr) == OFFSET_REF)
19488 offset = expr;
19489 expr = TREE_OPERAND (expr, 1);
19491 if (BASELINK_P (expr))
19493 baselink = expr;
19494 expr = BASELINK_FUNCTIONS (expr);
19497 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19499 int good = 0;
19500 tree goodfn = NULL_TREE;
19502 /* If we got some explicit template args, we need to plug them into
19503 the affected templates before we try to unify, in case the
19504 explicit args will completely resolve the templates in question. */
19506 tree expl_subargs = TREE_OPERAND (expr, 1);
19507 tree arg = TREE_OPERAND (expr, 0);
19508 tree badfn = NULL_TREE;
19509 tree badargs = NULL_TREE;
19511 for (lkp_iterator iter (arg); iter; ++iter)
19513 tree fn = *iter;
19514 tree subargs, elem;
19516 if (TREE_CODE (fn) != TEMPLATE_DECL)
19517 continue;
19519 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19520 expl_subargs, NULL_TREE, tf_none,
19521 /*require_all_args=*/true,
19522 /*use_default_args=*/true);
19523 if (subargs != error_mark_node
19524 && !any_dependent_template_arguments_p (subargs))
19526 elem = instantiate_template (fn, subargs, tf_none);
19527 if (elem == error_mark_node)
19529 badfn = fn;
19530 badargs = subargs;
19532 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19534 goodfn = elem;
19535 ++good;
19539 if (good == 1)
19541 mark_used (goodfn);
19542 expr = goodfn;
19543 if (baselink)
19544 expr = build_baselink (BASELINK_BINFO (baselink),
19545 BASELINK_ACCESS_BINFO (baselink),
19546 expr, BASELINK_OPTYPE (baselink));
19547 if (offset)
19549 tree base
19550 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19551 expr = build_offset_ref (base, expr, addr, complain);
19553 if (addr)
19554 expr = cp_build_addr_expr (expr, complain);
19555 return expr;
19557 else if (good == 0 && badargs && (complain & tf_error))
19558 /* There were no good options and at least one bad one, so let the
19559 user know what the problem is. */
19560 instantiate_template (badfn, badargs, complain);
19562 return orig_expr;
19565 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19566 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19567 different overloads deduce different arguments for a given parm.
19568 ADDR_P is true if the expression for which deduction is being
19569 performed was of the form "& fn" rather than simply "fn".
19571 Returns 1 on success. */
19573 static int
19574 try_one_overload (tree tparms,
19575 tree orig_targs,
19576 tree targs,
19577 tree parm,
19578 tree arg,
19579 unification_kind_t strict,
19580 int sub_strict,
19581 bool addr_p,
19582 bool explain_p)
19584 int nargs;
19585 tree tempargs;
19586 int i;
19588 if (arg == error_mark_node)
19589 return 0;
19591 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19592 to function or pointer to member function argument if the set of
19593 overloaded functions does not contain function templates and at most
19594 one of a set of overloaded functions provides a unique match.
19596 So if this is a template, just return success. */
19598 if (uses_template_parms (arg))
19599 return 1;
19601 if (TREE_CODE (arg) == METHOD_TYPE)
19602 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19603 else if (addr_p)
19604 arg = build_pointer_type (arg);
19606 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19608 /* We don't copy orig_targs for this because if we have already deduced
19609 some template args from previous args, unify would complain when we
19610 try to deduce a template parameter for the same argument, even though
19611 there isn't really a conflict. */
19612 nargs = TREE_VEC_LENGTH (targs);
19613 tempargs = make_tree_vec (nargs);
19615 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19616 return 0;
19618 /* First make sure we didn't deduce anything that conflicts with
19619 explicitly specified args. */
19620 for (i = nargs; i--; )
19622 tree elt = TREE_VEC_ELT (tempargs, i);
19623 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19625 if (!elt)
19626 /*NOP*/;
19627 else if (uses_template_parms (elt))
19628 /* Since we're unifying against ourselves, we will fill in
19629 template args used in the function parm list with our own
19630 template parms. Discard them. */
19631 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19632 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19634 /* Check that the argument at each index of the deduced argument pack
19635 is equivalent to the corresponding explicitly specified argument.
19636 We may have deduced more arguments than were explicitly specified,
19637 and that's OK. */
19639 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19640 that's wrong if we deduce the same argument pack from multiple
19641 function arguments: it's only incomplete the first time. */
19643 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19644 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19646 if (TREE_VEC_LENGTH (deduced_pack)
19647 < TREE_VEC_LENGTH (explicit_pack))
19648 return 0;
19650 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19651 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19652 TREE_VEC_ELT (deduced_pack, j)))
19653 return 0;
19655 else if (oldelt && !template_args_equal (oldelt, elt))
19656 return 0;
19659 for (i = nargs; i--; )
19661 tree elt = TREE_VEC_ELT (tempargs, i);
19663 if (elt)
19664 TREE_VEC_ELT (targs, i) = elt;
19667 return 1;
19670 /* PARM is a template class (perhaps with unbound template
19671 parameters). ARG is a fully instantiated type. If ARG can be
19672 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19673 TARGS are as for unify. */
19675 static tree
19676 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19677 bool explain_p)
19679 tree copy_of_targs;
19681 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19682 return NULL_TREE;
19683 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19684 /* Matches anything. */;
19685 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19686 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19687 return NULL_TREE;
19689 /* We need to make a new template argument vector for the call to
19690 unify. If we used TARGS, we'd clutter it up with the result of
19691 the attempted unification, even if this class didn't work out.
19692 We also don't want to commit ourselves to all the unifications
19693 we've already done, since unification is supposed to be done on
19694 an argument-by-argument basis. In other words, consider the
19695 following pathological case:
19697 template <int I, int J, int K>
19698 struct S {};
19700 template <int I, int J>
19701 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19703 template <int I, int J, int K>
19704 void f(S<I, J, K>, S<I, I, I>);
19706 void g() {
19707 S<0, 0, 0> s0;
19708 S<0, 1, 2> s2;
19710 f(s0, s2);
19713 Now, by the time we consider the unification involving `s2', we
19714 already know that we must have `f<0, 0, 0>'. But, even though
19715 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19716 because there are two ways to unify base classes of S<0, 1, 2>
19717 with S<I, I, I>. If we kept the already deduced knowledge, we
19718 would reject the possibility I=1. */
19719 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19721 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19723 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19724 return NULL_TREE;
19725 return arg;
19728 /* If unification failed, we're done. */
19729 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19730 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19731 return NULL_TREE;
19733 return arg;
19736 /* Given a template type PARM and a class type ARG, find the unique
19737 base type in ARG that is an instance of PARM. We do not examine
19738 ARG itself; only its base-classes. If there is not exactly one
19739 appropriate base class, return NULL_TREE. PARM may be the type of
19740 a partial specialization, as well as a plain template type. Used
19741 by unify. */
19743 static enum template_base_result
19744 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19745 bool explain_p, tree *result)
19747 tree rval = NULL_TREE;
19748 tree binfo;
19750 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19752 binfo = TYPE_BINFO (complete_type (arg));
19753 if (!binfo)
19755 /* The type could not be completed. */
19756 *result = NULL_TREE;
19757 return tbr_incomplete_type;
19760 /* Walk in inheritance graph order. The search order is not
19761 important, and this avoids multiple walks of virtual bases. */
19762 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19764 tree r = try_class_unification (tparms, targs, parm,
19765 BINFO_TYPE (binfo), explain_p);
19767 if (r)
19769 /* If there is more than one satisfactory baseclass, then:
19771 [temp.deduct.call]
19773 If they yield more than one possible deduced A, the type
19774 deduction fails.
19776 applies. */
19777 if (rval && !same_type_p (r, rval))
19779 *result = NULL_TREE;
19780 return tbr_ambiguous_baseclass;
19783 rval = r;
19787 *result = rval;
19788 return tbr_success;
19791 /* Returns the level of DECL, which declares a template parameter. */
19793 static int
19794 template_decl_level (tree decl)
19796 switch (TREE_CODE (decl))
19798 case TYPE_DECL:
19799 case TEMPLATE_DECL:
19800 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19802 case PARM_DECL:
19803 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19805 default:
19806 gcc_unreachable ();
19808 return 0;
19811 /* Decide whether ARG can be unified with PARM, considering only the
19812 cv-qualifiers of each type, given STRICT as documented for unify.
19813 Returns nonzero iff the unification is OK on that basis. */
19815 static int
19816 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19818 int arg_quals = cp_type_quals (arg);
19819 int parm_quals = cp_type_quals (parm);
19821 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19822 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19824 /* Although a CVR qualifier is ignored when being applied to a
19825 substituted template parameter ([8.3.2]/1 for example), that
19826 does not allow us to unify "const T" with "int&" because both
19827 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19828 It is ok when we're allowing additional CV qualifiers
19829 at the outer level [14.8.2.1]/3,1st bullet. */
19830 if ((TREE_CODE (arg) == REFERENCE_TYPE
19831 || TREE_CODE (arg) == FUNCTION_TYPE
19832 || TREE_CODE (arg) == METHOD_TYPE)
19833 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19834 return 0;
19836 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19837 && (parm_quals & TYPE_QUAL_RESTRICT))
19838 return 0;
19841 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19842 && (arg_quals & parm_quals) != parm_quals)
19843 return 0;
19845 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19846 && (parm_quals & arg_quals) != arg_quals)
19847 return 0;
19849 return 1;
19852 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19853 void
19854 template_parm_level_and_index (tree parm, int* level, int* index)
19856 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19857 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19858 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19860 *index = TEMPLATE_TYPE_IDX (parm);
19861 *level = TEMPLATE_TYPE_LEVEL (parm);
19863 else
19865 *index = TEMPLATE_PARM_IDX (parm);
19866 *level = TEMPLATE_PARM_LEVEL (parm);
19870 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19871 do { \
19872 if (unify (TP, TA, P, A, S, EP)) \
19873 return 1; \
19874 } while (0)
19876 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19877 expansion at the end of PACKED_PARMS. Returns 0 if the type
19878 deduction succeeds, 1 otherwise. STRICT is the same as in
19879 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19880 function call argument list. We'll need to adjust the arguments to make them
19881 types. SUBR tells us if this is from a recursive call to
19882 type_unification_real, or for comparing two template argument
19883 lists. */
19885 static int
19886 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
19887 tree packed_args, unification_kind_t strict,
19888 bool subr, bool explain_p)
19890 tree parm
19891 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
19892 tree pattern = PACK_EXPANSION_PATTERN (parm);
19893 tree pack, packs = NULL_TREE;
19894 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
19896 packed_args = expand_template_argument_pack (packed_args);
19898 int len = TREE_VEC_LENGTH (packed_args);
19900 /* Determine the parameter packs we will be deducing from the
19901 pattern, and record their current deductions. */
19902 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
19903 pack; pack = TREE_CHAIN (pack))
19905 tree parm_pack = TREE_VALUE (pack);
19906 int idx, level;
19908 /* Determine the index and level of this parameter pack. */
19909 template_parm_level_and_index (parm_pack, &level, &idx);
19911 /* Keep track of the parameter packs and their corresponding
19912 argument packs. */
19913 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
19914 TREE_TYPE (packs) = make_tree_vec (len - start);
19917 /* Loop through all of the arguments that have not yet been
19918 unified and unify each with the pattern. */
19919 for (i = start; i < len; i++)
19921 tree parm;
19922 bool any_explicit = false;
19923 tree arg = TREE_VEC_ELT (packed_args, i);
19925 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
19926 or the element of its argument pack at the current index if
19927 this argument was explicitly specified. */
19928 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19930 int idx, level;
19931 tree arg, pargs;
19932 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19934 arg = NULL_TREE;
19935 if (TREE_VALUE (pack)
19936 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
19937 && (i - start < TREE_VEC_LENGTH (pargs)))
19939 any_explicit = true;
19940 arg = TREE_VEC_ELT (pargs, i - start);
19942 TMPL_ARG (targs, level, idx) = arg;
19945 /* If we had explicit template arguments, substitute them into the
19946 pattern before deduction. */
19947 if (any_explicit)
19949 /* Some arguments might still be unspecified or dependent. */
19950 bool dependent;
19951 ++processing_template_decl;
19952 dependent = any_dependent_template_arguments_p (targs);
19953 if (!dependent)
19954 --processing_template_decl;
19955 parm = tsubst (pattern, targs,
19956 explain_p ? tf_warning_or_error : tf_none,
19957 NULL_TREE);
19958 if (dependent)
19959 --processing_template_decl;
19960 if (parm == error_mark_node)
19961 return 1;
19963 else
19964 parm = pattern;
19966 /* Unify the pattern with the current argument. */
19967 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
19968 explain_p))
19969 return 1;
19971 /* For each parameter pack, collect the deduced value. */
19972 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19974 int idx, level;
19975 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19977 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
19978 TMPL_ARG (targs, level, idx);
19982 /* Verify that the results of unification with the parameter packs
19983 produce results consistent with what we've seen before, and make
19984 the deduced argument packs available. */
19985 for (pack = packs; pack; pack = TREE_CHAIN (pack))
19987 tree old_pack = TREE_VALUE (pack);
19988 tree new_args = TREE_TYPE (pack);
19989 int i, len = TREE_VEC_LENGTH (new_args);
19990 int idx, level;
19991 bool nondeduced_p = false;
19993 /* By default keep the original deduced argument pack.
19994 If necessary, more specific code is going to update the
19995 resulting deduced argument later down in this function. */
19996 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
19997 TMPL_ARG (targs, level, idx) = old_pack;
19999 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20000 actually deduce anything. */
20001 for (i = 0; i < len && !nondeduced_p; ++i)
20002 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20003 nondeduced_p = true;
20004 if (nondeduced_p)
20005 continue;
20007 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20009 /* If we had fewer function args than explicit template args,
20010 just use the explicits. */
20011 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20012 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20013 if (len < explicit_len)
20014 new_args = explicit_args;
20017 if (!old_pack)
20019 tree result;
20020 /* Build the deduced *_ARGUMENT_PACK. */
20021 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20023 result = make_node (NONTYPE_ARGUMENT_PACK);
20024 TREE_CONSTANT (result) = 1;
20026 else
20027 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20029 SET_ARGUMENT_PACK_ARGS (result, new_args);
20031 /* Note the deduced argument packs for this parameter
20032 pack. */
20033 TMPL_ARG (targs, level, idx) = result;
20035 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20036 && (ARGUMENT_PACK_ARGS (old_pack)
20037 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20039 /* We only had the explicitly-provided arguments before, but
20040 now we have a complete set of arguments. */
20041 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20043 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20044 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20045 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20047 else
20049 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20050 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20052 if (!comp_template_args (old_args, new_args,
20053 &bad_old_arg, &bad_new_arg))
20054 /* Inconsistent unification of this parameter pack. */
20055 return unify_parameter_pack_inconsistent (explain_p,
20056 bad_old_arg,
20057 bad_new_arg);
20061 return unify_success (explain_p);
20064 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20065 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20066 parameters and return value are as for unify. */
20068 static int
20069 unify_array_domain (tree tparms, tree targs,
20070 tree parm_dom, tree arg_dom,
20071 bool explain_p)
20073 tree parm_max;
20074 tree arg_max;
20075 bool parm_cst;
20076 bool arg_cst;
20078 /* Our representation of array types uses "N - 1" as the
20079 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20080 not an integer constant. We cannot unify arbitrarily
20081 complex expressions, so we eliminate the MINUS_EXPRs
20082 here. */
20083 parm_max = TYPE_MAX_VALUE (parm_dom);
20084 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20085 if (!parm_cst)
20087 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20088 parm_max = TREE_OPERAND (parm_max, 0);
20090 arg_max = TYPE_MAX_VALUE (arg_dom);
20091 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20092 if (!arg_cst)
20094 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20095 trying to unify the type of a variable with the type
20096 of a template parameter. For example:
20098 template <unsigned int N>
20099 void f (char (&) [N]);
20100 int g();
20101 void h(int i) {
20102 char a[g(i)];
20103 f(a);
20106 Here, the type of the ARG will be "int [g(i)]", and
20107 may be a SAVE_EXPR, etc. */
20108 if (TREE_CODE (arg_max) != MINUS_EXPR)
20109 return unify_vla_arg (explain_p, arg_dom);
20110 arg_max = TREE_OPERAND (arg_max, 0);
20113 /* If only one of the bounds used a MINUS_EXPR, compensate
20114 by adding one to the other bound. */
20115 if (parm_cst && !arg_cst)
20116 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20117 integer_type_node,
20118 parm_max,
20119 integer_one_node);
20120 else if (arg_cst && !parm_cst)
20121 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20122 integer_type_node,
20123 arg_max,
20124 integer_one_node);
20126 return unify (tparms, targs, parm_max, arg_max,
20127 UNIFY_ALLOW_INTEGER, explain_p);
20130 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20132 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20134 static pa_kind_t
20135 pa_kind (tree t)
20137 if (PACK_EXPANSION_P (t))
20138 t = PACK_EXPANSION_PATTERN (t);
20139 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20140 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20141 || DECL_TYPE_TEMPLATE_P (t))
20142 return pa_tmpl;
20143 else if (TYPE_P (t))
20144 return pa_type;
20145 else
20146 return pa_expr;
20149 /* Deduce the value of template parameters. TPARMS is the (innermost)
20150 set of template parameters to a template. TARGS is the bindings
20151 for those template parameters, as determined thus far; TARGS may
20152 include template arguments for outer levels of template parameters
20153 as well. PARM is a parameter to a template function, or a
20154 subcomponent of that parameter; ARG is the corresponding argument.
20155 This function attempts to match PARM with ARG in a manner
20156 consistent with the existing assignments in TARGS. If more values
20157 are deduced, then TARGS is updated.
20159 Returns 0 if the type deduction succeeds, 1 otherwise. The
20160 parameter STRICT is a bitwise or of the following flags:
20162 UNIFY_ALLOW_NONE:
20163 Require an exact match between PARM and ARG.
20164 UNIFY_ALLOW_MORE_CV_QUAL:
20165 Allow the deduced ARG to be more cv-qualified (by qualification
20166 conversion) than ARG.
20167 UNIFY_ALLOW_LESS_CV_QUAL:
20168 Allow the deduced ARG to be less cv-qualified than ARG.
20169 UNIFY_ALLOW_DERIVED:
20170 Allow the deduced ARG to be a template base class of ARG,
20171 or a pointer to a template base class of the type pointed to by
20172 ARG.
20173 UNIFY_ALLOW_INTEGER:
20174 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20175 case for more information.
20176 UNIFY_ALLOW_OUTER_LEVEL:
20177 This is the outermost level of a deduction. Used to determine validity
20178 of qualification conversions. A valid qualification conversion must
20179 have const qualified pointers leading up to the inner type which
20180 requires additional CV quals, except at the outer level, where const
20181 is not required [conv.qual]. It would be normal to set this flag in
20182 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20183 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20184 This is the outermost level of a deduction, and PARM can be more CV
20185 qualified at this point.
20186 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20187 This is the outermost level of a deduction, and PARM can be less CV
20188 qualified at this point. */
20190 static int
20191 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20192 bool explain_p)
20194 int idx;
20195 tree targ;
20196 tree tparm;
20197 int strict_in = strict;
20198 tsubst_flags_t complain = (explain_p
20199 ? tf_warning_or_error
20200 : tf_none);
20202 /* I don't think this will do the right thing with respect to types.
20203 But the only case I've seen it in so far has been array bounds, where
20204 signedness is the only information lost, and I think that will be
20205 okay. */
20206 while (CONVERT_EXPR_P (parm))
20207 parm = TREE_OPERAND (parm, 0);
20209 if (arg == error_mark_node)
20210 return unify_invalid (explain_p);
20211 if (arg == unknown_type_node
20212 || arg == init_list_type_node)
20213 /* We can't deduce anything from this, but we might get all the
20214 template args from other function args. */
20215 return unify_success (explain_p);
20217 if (parm == any_targ_node || arg == any_targ_node)
20218 return unify_success (explain_p);
20220 /* If PARM uses template parameters, then we can't bail out here,
20221 even if ARG == PARM, since we won't record unifications for the
20222 template parameters. We might need them if we're trying to
20223 figure out which of two things is more specialized. */
20224 if (arg == parm && !uses_template_parms (parm))
20225 return unify_success (explain_p);
20227 /* Handle init lists early, so the rest of the function can assume
20228 we're dealing with a type. */
20229 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20231 tree elt, elttype;
20232 unsigned i;
20233 tree orig_parm = parm;
20235 /* Replace T with std::initializer_list<T> for deduction. */
20236 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20237 && flag_deduce_init_list)
20238 parm = listify (parm);
20240 if (!is_std_init_list (parm)
20241 && TREE_CODE (parm) != ARRAY_TYPE)
20242 /* We can only deduce from an initializer list argument if the
20243 parameter is std::initializer_list or an array; otherwise this
20244 is a non-deduced context. */
20245 return unify_success (explain_p);
20247 if (TREE_CODE (parm) == ARRAY_TYPE)
20248 elttype = TREE_TYPE (parm);
20249 else
20251 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20252 /* Deduction is defined in terms of a single type, so just punt
20253 on the (bizarre) std::initializer_list<T...>. */
20254 if (PACK_EXPANSION_P (elttype))
20255 return unify_success (explain_p);
20258 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20260 int elt_strict = strict;
20262 if (elt == error_mark_node)
20263 return unify_invalid (explain_p);
20265 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20267 tree type = TREE_TYPE (elt);
20268 if (type == error_mark_node)
20269 return unify_invalid (explain_p);
20270 /* It should only be possible to get here for a call. */
20271 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20272 elt_strict |= maybe_adjust_types_for_deduction
20273 (DEDUCE_CALL, &elttype, &type, elt);
20274 elt = type;
20277 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20278 explain_p);
20281 if (TREE_CODE (parm) == ARRAY_TYPE
20282 && deducible_array_bound (TYPE_DOMAIN (parm)))
20284 /* Also deduce from the length of the initializer list. */
20285 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20286 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20287 if (idx == error_mark_node)
20288 return unify_invalid (explain_p);
20289 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20290 idx, explain_p);
20293 /* If the std::initializer_list<T> deduction worked, replace the
20294 deduced A with std::initializer_list<A>. */
20295 if (orig_parm != parm)
20297 idx = TEMPLATE_TYPE_IDX (orig_parm);
20298 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20299 targ = listify (targ);
20300 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20302 return unify_success (explain_p);
20305 /* If parm and arg aren't the same kind of thing (template, type, or
20306 expression), fail early. */
20307 if (pa_kind (parm) != pa_kind (arg))
20308 return unify_invalid (explain_p);
20310 /* Immediately reject some pairs that won't unify because of
20311 cv-qualification mismatches. */
20312 if (TREE_CODE (arg) == TREE_CODE (parm)
20313 && TYPE_P (arg)
20314 /* It is the elements of the array which hold the cv quals of an array
20315 type, and the elements might be template type parms. We'll check
20316 when we recurse. */
20317 && TREE_CODE (arg) != ARRAY_TYPE
20318 /* We check the cv-qualifiers when unifying with template type
20319 parameters below. We want to allow ARG `const T' to unify with
20320 PARM `T' for example, when computing which of two templates
20321 is more specialized, for example. */
20322 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20323 && !check_cv_quals_for_unify (strict_in, arg, parm))
20324 return unify_cv_qual_mismatch (explain_p, parm, arg);
20326 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20327 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20328 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20329 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20330 strict &= ~UNIFY_ALLOW_DERIVED;
20331 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20332 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20334 switch (TREE_CODE (parm))
20336 case TYPENAME_TYPE:
20337 case SCOPE_REF:
20338 case UNBOUND_CLASS_TEMPLATE:
20339 /* In a type which contains a nested-name-specifier, template
20340 argument values cannot be deduced for template parameters used
20341 within the nested-name-specifier. */
20342 return unify_success (explain_p);
20344 case TEMPLATE_TYPE_PARM:
20345 case TEMPLATE_TEMPLATE_PARM:
20346 case BOUND_TEMPLATE_TEMPLATE_PARM:
20347 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20348 if (error_operand_p (tparm))
20349 return unify_invalid (explain_p);
20351 if (TEMPLATE_TYPE_LEVEL (parm)
20352 != template_decl_level (tparm))
20353 /* The PARM is not one we're trying to unify. Just check
20354 to see if it matches ARG. */
20356 if (TREE_CODE (arg) == TREE_CODE (parm)
20357 && (is_auto (parm) ? is_auto (arg)
20358 : same_type_p (parm, arg)))
20359 return unify_success (explain_p);
20360 else
20361 return unify_type_mismatch (explain_p, parm, arg);
20363 idx = TEMPLATE_TYPE_IDX (parm);
20364 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20365 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20366 if (error_operand_p (tparm))
20367 return unify_invalid (explain_p);
20369 /* Check for mixed types and values. */
20370 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20371 && TREE_CODE (tparm) != TYPE_DECL)
20372 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20373 && TREE_CODE (tparm) != TEMPLATE_DECL))
20374 gcc_unreachable ();
20376 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20378 if ((strict_in & UNIFY_ALLOW_DERIVED)
20379 && CLASS_TYPE_P (arg))
20381 /* First try to match ARG directly. */
20382 tree t = try_class_unification (tparms, targs, parm, arg,
20383 explain_p);
20384 if (!t)
20386 /* Otherwise, look for a suitable base of ARG, as below. */
20387 enum template_base_result r;
20388 r = get_template_base (tparms, targs, parm, arg,
20389 explain_p, &t);
20390 if (!t)
20391 return unify_no_common_base (explain_p, r, parm, arg);
20392 arg = t;
20395 /* ARG must be constructed from a template class or a template
20396 template parameter. */
20397 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20398 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20399 return unify_template_deduction_failure (explain_p, parm, arg);
20401 /* Deduce arguments T, i from TT<T> or TT<i>. */
20402 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20403 return 1;
20405 arg = TYPE_TI_TEMPLATE (arg);
20407 /* Fall through to deduce template name. */
20410 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20411 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20413 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20415 /* Simple cases: Value already set, does match or doesn't. */
20416 if (targ != NULL_TREE && template_args_equal (targ, arg))
20417 return unify_success (explain_p);
20418 else if (targ)
20419 return unify_inconsistency (explain_p, parm, targ, arg);
20421 else
20423 /* If PARM is `const T' and ARG is only `int', we don't have
20424 a match unless we are allowing additional qualification.
20425 If ARG is `const int' and PARM is just `T' that's OK;
20426 that binds `const int' to `T'. */
20427 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20428 arg, parm))
20429 return unify_cv_qual_mismatch (explain_p, parm, arg);
20431 /* Consider the case where ARG is `const volatile int' and
20432 PARM is `const T'. Then, T should be `volatile int'. */
20433 arg = cp_build_qualified_type_real
20434 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20435 if (arg == error_mark_node)
20436 return unify_invalid (explain_p);
20438 /* Simple cases: Value already set, does match or doesn't. */
20439 if (targ != NULL_TREE && same_type_p (targ, arg))
20440 return unify_success (explain_p);
20441 else if (targ)
20442 return unify_inconsistency (explain_p, parm, targ, arg);
20444 /* Make sure that ARG is not a variable-sized array. (Note
20445 that were talking about variable-sized arrays (like
20446 `int[n]'), rather than arrays of unknown size (like
20447 `int[]').) We'll get very confused by such a type since
20448 the bound of the array is not constant, and therefore
20449 not mangleable. Besides, such types are not allowed in
20450 ISO C++, so we can do as we please here. We do allow
20451 them for 'auto' deduction, since that isn't ABI-exposed. */
20452 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20453 return unify_vla_arg (explain_p, arg);
20455 /* Strip typedefs as in convert_template_argument. */
20456 arg = canonicalize_type_argument (arg, tf_none);
20459 /* If ARG is a parameter pack or an expansion, we cannot unify
20460 against it unless PARM is also a parameter pack. */
20461 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20462 && !template_parameter_pack_p (parm))
20463 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20465 /* If the argument deduction results is a METHOD_TYPE,
20466 then there is a problem.
20467 METHOD_TYPE doesn't map to any real C++ type the result of
20468 the deduction can not be of that type. */
20469 if (TREE_CODE (arg) == METHOD_TYPE)
20470 return unify_method_type_error (explain_p, arg);
20472 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20473 return unify_success (explain_p);
20475 case TEMPLATE_PARM_INDEX:
20476 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20477 if (error_operand_p (tparm))
20478 return unify_invalid (explain_p);
20480 if (TEMPLATE_PARM_LEVEL (parm)
20481 != template_decl_level (tparm))
20483 /* The PARM is not one we're trying to unify. Just check
20484 to see if it matches ARG. */
20485 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20486 && cp_tree_equal (parm, arg));
20487 if (result)
20488 unify_expression_unequal (explain_p, parm, arg);
20489 return result;
20492 idx = TEMPLATE_PARM_IDX (parm);
20493 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20495 if (targ)
20497 int x = !cp_tree_equal (targ, arg);
20498 if (x)
20499 unify_inconsistency (explain_p, parm, targ, arg);
20500 return x;
20503 if (cxx_dialect >= cxx1z
20504 /* We deduce from array bounds in try_array_deduction. */
20505 && !(strict & UNIFY_ALLOW_INTEGER)
20506 && uses_template_parms (TREE_TYPE (parm))
20507 && !type_uses_auto (TREE_TYPE (parm)))
20509 tree atype = TREE_TYPE (arg);
20510 RECUR_AND_CHECK_FAILURE (tparms, targs,
20511 TREE_TYPE (parm), atype,
20512 UNIFY_ALLOW_NONE, explain_p);
20515 /* [temp.deduct.type] If, in the declaration of a function template
20516 with a non-type template-parameter, the non-type
20517 template-parameter is used in an expression in the function
20518 parameter-list and, if the corresponding template-argument is
20519 deduced, the template-argument type shall match the type of the
20520 template-parameter exactly, except that a template-argument
20521 deduced from an array bound may be of any integral type.
20522 The non-type parameter might use already deduced type parameters. */
20523 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20524 if (tree a = type_uses_auto (tparm))
20526 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20527 if (tparm == error_mark_node)
20528 return 1;
20531 if (!TREE_TYPE (arg))
20532 /* Template-parameter dependent expression. Just accept it for now.
20533 It will later be processed in convert_template_argument. */
20535 else if (same_type_p (TREE_TYPE (arg), tparm))
20536 /* OK */;
20537 else if ((strict & UNIFY_ALLOW_INTEGER)
20538 && CP_INTEGRAL_TYPE_P (tparm))
20539 /* Convert the ARG to the type of PARM; the deduced non-type
20540 template argument must exactly match the types of the
20541 corresponding parameter. */
20542 arg = fold (build_nop (tparm, arg));
20543 else if (uses_template_parms (tparm))
20544 /* We haven't deduced the type of this parameter yet. Try again
20545 later. */
20546 return unify_success (explain_p);
20547 else
20548 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20550 /* If ARG is a parameter pack or an expansion, we cannot unify
20551 against it unless PARM is also a parameter pack. */
20552 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20553 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20554 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20557 bool removed_attr = false;
20558 arg = strip_typedefs_expr (arg, &removed_attr);
20560 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20561 return unify_success (explain_p);
20563 case PTRMEM_CST:
20565 /* A pointer-to-member constant can be unified only with
20566 another constant. */
20567 if (TREE_CODE (arg) != PTRMEM_CST)
20568 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20570 /* Just unify the class member. It would be useless (and possibly
20571 wrong, depending on the strict flags) to unify also
20572 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20573 arg refer to the same variable, even if through different
20574 classes. For instance:
20576 struct A { int x; };
20577 struct B : A { };
20579 Unification of &A::x and &B::x must succeed. */
20580 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20581 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20584 case POINTER_TYPE:
20586 if (!TYPE_PTR_P (arg))
20587 return unify_type_mismatch (explain_p, parm, arg);
20589 /* [temp.deduct.call]
20591 A can be another pointer or pointer to member type that can
20592 be converted to the deduced A via a qualification
20593 conversion (_conv.qual_).
20595 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20596 This will allow for additional cv-qualification of the
20597 pointed-to types if appropriate. */
20599 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20600 /* The derived-to-base conversion only persists through one
20601 level of pointers. */
20602 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20604 return unify (tparms, targs, TREE_TYPE (parm),
20605 TREE_TYPE (arg), strict, explain_p);
20608 case REFERENCE_TYPE:
20609 if (TREE_CODE (arg) != REFERENCE_TYPE)
20610 return unify_type_mismatch (explain_p, parm, arg);
20611 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20612 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20614 case ARRAY_TYPE:
20615 if (TREE_CODE (arg) != ARRAY_TYPE)
20616 return unify_type_mismatch (explain_p, parm, arg);
20617 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20618 != (TYPE_DOMAIN (arg) == NULL_TREE))
20619 return unify_type_mismatch (explain_p, parm, arg);
20620 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20621 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20622 if (TYPE_DOMAIN (parm) != NULL_TREE)
20623 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20624 TYPE_DOMAIN (arg), explain_p);
20625 return unify_success (explain_p);
20627 case REAL_TYPE:
20628 case COMPLEX_TYPE:
20629 case VECTOR_TYPE:
20630 case INTEGER_TYPE:
20631 case BOOLEAN_TYPE:
20632 case ENUMERAL_TYPE:
20633 case VOID_TYPE:
20634 case NULLPTR_TYPE:
20635 if (TREE_CODE (arg) != TREE_CODE (parm))
20636 return unify_type_mismatch (explain_p, parm, arg);
20638 /* We have already checked cv-qualification at the top of the
20639 function. */
20640 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20641 return unify_type_mismatch (explain_p, parm, arg);
20643 /* As far as unification is concerned, this wins. Later checks
20644 will invalidate it if necessary. */
20645 return unify_success (explain_p);
20647 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20648 /* Type INTEGER_CST can come from ordinary constant template args. */
20649 case INTEGER_CST:
20650 while (CONVERT_EXPR_P (arg))
20651 arg = TREE_OPERAND (arg, 0);
20653 if (TREE_CODE (arg) != INTEGER_CST)
20654 return unify_template_argument_mismatch (explain_p, parm, arg);
20655 return (tree_int_cst_equal (parm, arg)
20656 ? unify_success (explain_p)
20657 : unify_template_argument_mismatch (explain_p, parm, arg));
20659 case TREE_VEC:
20661 int i, len, argslen;
20662 int parm_variadic_p = 0;
20664 if (TREE_CODE (arg) != TREE_VEC)
20665 return unify_template_argument_mismatch (explain_p, parm, arg);
20667 len = TREE_VEC_LENGTH (parm);
20668 argslen = TREE_VEC_LENGTH (arg);
20670 /* Check for pack expansions in the parameters. */
20671 for (i = 0; i < len; ++i)
20673 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20675 if (i == len - 1)
20676 /* We can unify against something with a trailing
20677 parameter pack. */
20678 parm_variadic_p = 1;
20679 else
20680 /* [temp.deduct.type]/9: If the template argument list of
20681 P contains a pack expansion that is not the last
20682 template argument, the entire template argument list
20683 is a non-deduced context. */
20684 return unify_success (explain_p);
20688 /* If we don't have enough arguments to satisfy the parameters
20689 (not counting the pack expression at the end), or we have
20690 too many arguments for a parameter list that doesn't end in
20691 a pack expression, we can't unify. */
20692 if (parm_variadic_p
20693 ? argslen < len - parm_variadic_p
20694 : argslen != len)
20695 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20697 /* Unify all of the parameters that precede the (optional)
20698 pack expression. */
20699 for (i = 0; i < len - parm_variadic_p; ++i)
20701 RECUR_AND_CHECK_FAILURE (tparms, targs,
20702 TREE_VEC_ELT (parm, i),
20703 TREE_VEC_ELT (arg, i),
20704 UNIFY_ALLOW_NONE, explain_p);
20706 if (parm_variadic_p)
20707 return unify_pack_expansion (tparms, targs, parm, arg,
20708 DEDUCE_EXACT,
20709 /*subr=*/true, explain_p);
20710 return unify_success (explain_p);
20713 case RECORD_TYPE:
20714 case UNION_TYPE:
20715 if (TREE_CODE (arg) != TREE_CODE (parm))
20716 return unify_type_mismatch (explain_p, parm, arg);
20718 if (TYPE_PTRMEMFUNC_P (parm))
20720 if (!TYPE_PTRMEMFUNC_P (arg))
20721 return unify_type_mismatch (explain_p, parm, arg);
20723 return unify (tparms, targs,
20724 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20725 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20726 strict, explain_p);
20728 else if (TYPE_PTRMEMFUNC_P (arg))
20729 return unify_type_mismatch (explain_p, parm, arg);
20731 if (CLASSTYPE_TEMPLATE_INFO (parm))
20733 tree t = NULL_TREE;
20735 if (strict_in & UNIFY_ALLOW_DERIVED)
20737 /* First, we try to unify the PARM and ARG directly. */
20738 t = try_class_unification (tparms, targs,
20739 parm, arg, explain_p);
20741 if (!t)
20743 /* Fallback to the special case allowed in
20744 [temp.deduct.call]:
20746 If P is a class, and P has the form
20747 template-id, then A can be a derived class of
20748 the deduced A. Likewise, if P is a pointer to
20749 a class of the form template-id, A can be a
20750 pointer to a derived class pointed to by the
20751 deduced A. */
20752 enum template_base_result r;
20753 r = get_template_base (tparms, targs, parm, arg,
20754 explain_p, &t);
20756 if (!t)
20758 /* Don't give the derived diagnostic if we're
20759 already dealing with the same template. */
20760 bool same_template
20761 = (CLASSTYPE_TEMPLATE_INFO (arg)
20762 && (CLASSTYPE_TI_TEMPLATE (parm)
20763 == CLASSTYPE_TI_TEMPLATE (arg)));
20764 return unify_no_common_base (explain_p && !same_template,
20765 r, parm, arg);
20769 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20770 && (CLASSTYPE_TI_TEMPLATE (parm)
20771 == CLASSTYPE_TI_TEMPLATE (arg)))
20772 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20773 Then, we should unify `int' and `U'. */
20774 t = arg;
20775 else
20776 /* There's no chance of unification succeeding. */
20777 return unify_type_mismatch (explain_p, parm, arg);
20779 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20780 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20782 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20783 return unify_type_mismatch (explain_p, parm, arg);
20784 return unify_success (explain_p);
20786 case METHOD_TYPE:
20787 case FUNCTION_TYPE:
20789 unsigned int nargs;
20790 tree *args;
20791 tree a;
20792 unsigned int i;
20794 if (TREE_CODE (arg) != TREE_CODE (parm))
20795 return unify_type_mismatch (explain_p, parm, arg);
20797 /* CV qualifications for methods can never be deduced, they must
20798 match exactly. We need to check them explicitly here,
20799 because type_unification_real treats them as any other
20800 cv-qualified parameter. */
20801 if (TREE_CODE (parm) == METHOD_TYPE
20802 && (!check_cv_quals_for_unify
20803 (UNIFY_ALLOW_NONE,
20804 class_of_this_parm (arg),
20805 class_of_this_parm (parm))))
20806 return unify_cv_qual_mismatch (explain_p, parm, arg);
20807 if (TREE_CODE (arg) == FUNCTION_TYPE
20808 && type_memfn_quals (parm) != type_memfn_quals (arg))
20809 return unify_cv_qual_mismatch (explain_p, parm, arg);
20810 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20811 return unify_type_mismatch (explain_p, parm, arg);
20813 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20814 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20816 nargs = list_length (TYPE_ARG_TYPES (arg));
20817 args = XALLOCAVEC (tree, nargs);
20818 for (a = TYPE_ARG_TYPES (arg), i = 0;
20819 a != NULL_TREE && a != void_list_node;
20820 a = TREE_CHAIN (a), ++i)
20821 args[i] = TREE_VALUE (a);
20822 nargs = i;
20824 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20825 args, nargs, 1, DEDUCE_EXACT,
20826 LOOKUP_NORMAL, NULL, explain_p))
20827 return 1;
20829 if (flag_noexcept_type)
20831 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20832 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20833 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20834 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20835 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20836 && uses_template_parms (TREE_PURPOSE (pspec)))
20837 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20838 TREE_PURPOSE (aspec),
20839 UNIFY_ALLOW_NONE, explain_p);
20840 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20841 return unify_type_mismatch (explain_p, parm, arg);
20844 return 0;
20847 case OFFSET_TYPE:
20848 /* Unify a pointer to member with a pointer to member function, which
20849 deduces the type of the member as a function type. */
20850 if (TYPE_PTRMEMFUNC_P (arg))
20852 /* Check top-level cv qualifiers */
20853 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20854 return unify_cv_qual_mismatch (explain_p, parm, arg);
20856 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20857 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20858 UNIFY_ALLOW_NONE, explain_p);
20860 /* Determine the type of the function we are unifying against. */
20861 tree fntype = static_fn_type (arg);
20863 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20866 if (TREE_CODE (arg) != OFFSET_TYPE)
20867 return unify_type_mismatch (explain_p, parm, arg);
20868 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20869 TYPE_OFFSET_BASETYPE (arg),
20870 UNIFY_ALLOW_NONE, explain_p);
20871 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20872 strict, explain_p);
20874 case CONST_DECL:
20875 if (DECL_TEMPLATE_PARM_P (parm))
20876 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20877 if (arg != scalar_constant_value (parm))
20878 return unify_template_argument_mismatch (explain_p, parm, arg);
20879 return unify_success (explain_p);
20881 case FIELD_DECL:
20882 case TEMPLATE_DECL:
20883 /* Matched cases are handled by the ARG == PARM test above. */
20884 return unify_template_argument_mismatch (explain_p, parm, arg);
20886 case VAR_DECL:
20887 /* We might get a variable as a non-type template argument in parm if the
20888 corresponding parameter is type-dependent. Make any necessary
20889 adjustments based on whether arg is a reference. */
20890 if (CONSTANT_CLASS_P (arg))
20891 parm = fold_non_dependent_expr (parm);
20892 else if (REFERENCE_REF_P (arg))
20894 tree sub = TREE_OPERAND (arg, 0);
20895 STRIP_NOPS (sub);
20896 if (TREE_CODE (sub) == ADDR_EXPR)
20897 arg = TREE_OPERAND (sub, 0);
20899 /* Now use the normal expression code to check whether they match. */
20900 goto expr;
20902 case TYPE_ARGUMENT_PACK:
20903 case NONTYPE_ARGUMENT_PACK:
20904 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
20905 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
20907 case TYPEOF_TYPE:
20908 case DECLTYPE_TYPE:
20909 case UNDERLYING_TYPE:
20910 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
20911 or UNDERLYING_TYPE nodes. */
20912 return unify_success (explain_p);
20914 case ERROR_MARK:
20915 /* Unification fails if we hit an error node. */
20916 return unify_invalid (explain_p);
20918 case INDIRECT_REF:
20919 if (REFERENCE_REF_P (parm))
20921 bool pexp = PACK_EXPANSION_P (arg);
20922 if (pexp)
20923 arg = PACK_EXPANSION_PATTERN (arg);
20924 if (REFERENCE_REF_P (arg))
20925 arg = TREE_OPERAND (arg, 0);
20926 if (pexp)
20927 arg = make_pack_expansion (arg);
20928 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
20929 strict, explain_p);
20931 /* FALLTHRU */
20933 default:
20934 /* An unresolved overload is a nondeduced context. */
20935 if (is_overloaded_fn (parm) || type_unknown_p (parm))
20936 return unify_success (explain_p);
20937 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
20938 expr:
20939 /* We must be looking at an expression. This can happen with
20940 something like:
20942 template <int I>
20943 void foo(S<I>, S<I + 2>);
20945 This is a "nondeduced context":
20947 [deduct.type]
20949 The nondeduced contexts are:
20951 --A type that is a template-id in which one or more of
20952 the template-arguments is an expression that references
20953 a template-parameter.
20955 In these cases, we assume deduction succeeded, but don't
20956 actually infer any unifications. */
20958 if (!uses_template_parms (parm)
20959 && !template_args_equal (parm, arg))
20960 return unify_expression_unequal (explain_p, parm, arg);
20961 else
20962 return unify_success (explain_p);
20965 #undef RECUR_AND_CHECK_FAILURE
20967 /* Note that DECL can be defined in this translation unit, if
20968 required. */
20970 static void
20971 mark_definable (tree decl)
20973 tree clone;
20974 DECL_NOT_REALLY_EXTERN (decl) = 1;
20975 FOR_EACH_CLONE (clone, decl)
20976 DECL_NOT_REALLY_EXTERN (clone) = 1;
20979 /* Called if RESULT is explicitly instantiated, or is a member of an
20980 explicitly instantiated class. */
20982 void
20983 mark_decl_instantiated (tree result, int extern_p)
20985 SET_DECL_EXPLICIT_INSTANTIATION (result);
20987 /* If this entity has already been written out, it's too late to
20988 make any modifications. */
20989 if (TREE_ASM_WRITTEN (result))
20990 return;
20992 /* For anonymous namespace we don't need to do anything. */
20993 if (decl_anon_ns_mem_p (result))
20995 gcc_assert (!TREE_PUBLIC (result));
20996 return;
20999 if (TREE_CODE (result) != FUNCTION_DECL)
21000 /* The TREE_PUBLIC flag for function declarations will have been
21001 set correctly by tsubst. */
21002 TREE_PUBLIC (result) = 1;
21004 /* This might have been set by an earlier implicit instantiation. */
21005 DECL_COMDAT (result) = 0;
21007 if (extern_p)
21008 DECL_NOT_REALLY_EXTERN (result) = 0;
21009 else
21011 mark_definable (result);
21012 mark_needed (result);
21013 /* Always make artificials weak. */
21014 if (DECL_ARTIFICIAL (result) && flag_weak)
21015 comdat_linkage (result);
21016 /* For WIN32 we also want to put explicit instantiations in
21017 linkonce sections. */
21018 else if (TREE_PUBLIC (result))
21019 maybe_make_one_only (result);
21022 /* If EXTERN_P, then this function will not be emitted -- unless
21023 followed by an explicit instantiation, at which point its linkage
21024 will be adjusted. If !EXTERN_P, then this function will be
21025 emitted here. In neither circumstance do we want
21026 import_export_decl to adjust the linkage. */
21027 DECL_INTERFACE_KNOWN (result) = 1;
21030 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21031 important template arguments. If any are missing, we check whether
21032 they're important by using error_mark_node for substituting into any
21033 args that were used for partial ordering (the ones between ARGS and END)
21034 and seeing if it bubbles up. */
21036 static bool
21037 check_undeduced_parms (tree targs, tree args, tree end)
21039 bool found = false;
21040 int i;
21041 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21042 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21044 found = true;
21045 TREE_VEC_ELT (targs, i) = error_mark_node;
21047 if (found)
21049 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21050 if (substed == error_mark_node)
21051 return true;
21053 return false;
21056 /* Given two function templates PAT1 and PAT2, return:
21058 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21059 -1 if PAT2 is more specialized than PAT1.
21060 0 if neither is more specialized.
21062 LEN indicates the number of parameters we should consider
21063 (defaulted parameters should not be considered).
21065 The 1998 std underspecified function template partial ordering, and
21066 DR214 addresses the issue. We take pairs of arguments, one from
21067 each of the templates, and deduce them against each other. One of
21068 the templates will be more specialized if all the *other*
21069 template's arguments deduce against its arguments and at least one
21070 of its arguments *does* *not* deduce against the other template's
21071 corresponding argument. Deduction is done as for class templates.
21072 The arguments used in deduction have reference and top level cv
21073 qualifiers removed. Iff both arguments were originally reference
21074 types *and* deduction succeeds in both directions, an lvalue reference
21075 wins against an rvalue reference and otherwise the template
21076 with the more cv-qualified argument wins for that pairing (if
21077 neither is more cv-qualified, they both are equal). Unlike regular
21078 deduction, after all the arguments have been deduced in this way,
21079 we do *not* verify the deduced template argument values can be
21080 substituted into non-deduced contexts.
21082 The logic can be a bit confusing here, because we look at deduce1 and
21083 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21084 can find template arguments for pat1 to make arg1 look like arg2, that
21085 means that arg2 is at least as specialized as arg1. */
21088 more_specialized_fn (tree pat1, tree pat2, int len)
21090 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21091 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21092 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21093 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21094 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21095 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21096 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21097 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21098 tree origs1, origs2;
21099 bool lose1 = false;
21100 bool lose2 = false;
21102 /* Remove the this parameter from non-static member functions. If
21103 one is a non-static member function and the other is not a static
21104 member function, remove the first parameter from that function
21105 also. This situation occurs for operator functions where we
21106 locate both a member function (with this pointer) and non-member
21107 operator (with explicit first operand). */
21108 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21110 len--; /* LEN is the number of significant arguments for DECL1 */
21111 args1 = TREE_CHAIN (args1);
21112 if (!DECL_STATIC_FUNCTION_P (decl2))
21113 args2 = TREE_CHAIN (args2);
21115 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21117 args2 = TREE_CHAIN (args2);
21118 if (!DECL_STATIC_FUNCTION_P (decl1))
21120 len--;
21121 args1 = TREE_CHAIN (args1);
21125 /* If only one is a conversion operator, they are unordered. */
21126 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21127 return 0;
21129 /* Consider the return type for a conversion function */
21130 if (DECL_CONV_FN_P (decl1))
21132 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21133 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21134 len++;
21137 processing_template_decl++;
21139 origs1 = args1;
21140 origs2 = args2;
21142 while (len--
21143 /* Stop when an ellipsis is seen. */
21144 && args1 != NULL_TREE && args2 != NULL_TREE)
21146 tree arg1 = TREE_VALUE (args1);
21147 tree arg2 = TREE_VALUE (args2);
21148 int deduce1, deduce2;
21149 int quals1 = -1;
21150 int quals2 = -1;
21151 int ref1 = 0;
21152 int ref2 = 0;
21154 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21155 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21157 /* When both arguments are pack expansions, we need only
21158 unify the patterns themselves. */
21159 arg1 = PACK_EXPANSION_PATTERN (arg1);
21160 arg2 = PACK_EXPANSION_PATTERN (arg2);
21162 /* This is the last comparison we need to do. */
21163 len = 0;
21166 /* DR 1847: If a particular P contains no template-parameters that
21167 participate in template argument deduction, that P is not used to
21168 determine the ordering. */
21169 if (!uses_deducible_template_parms (arg1)
21170 && !uses_deducible_template_parms (arg2))
21171 goto next;
21173 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21175 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21176 arg1 = TREE_TYPE (arg1);
21177 quals1 = cp_type_quals (arg1);
21180 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21182 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21183 arg2 = TREE_TYPE (arg2);
21184 quals2 = cp_type_quals (arg2);
21187 arg1 = TYPE_MAIN_VARIANT (arg1);
21188 arg2 = TYPE_MAIN_VARIANT (arg2);
21190 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21192 int i, len2 = remaining_arguments (args2);
21193 tree parmvec = make_tree_vec (1);
21194 tree argvec = make_tree_vec (len2);
21195 tree ta = args2;
21197 /* Setup the parameter vector, which contains only ARG1. */
21198 TREE_VEC_ELT (parmvec, 0) = arg1;
21200 /* Setup the argument vector, which contains the remaining
21201 arguments. */
21202 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21203 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21205 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21206 argvec, DEDUCE_EXACT,
21207 /*subr=*/true, /*explain_p=*/false)
21208 == 0);
21210 /* We cannot deduce in the other direction, because ARG1 is
21211 a pack expansion but ARG2 is not. */
21212 deduce2 = 0;
21214 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21216 int i, len1 = remaining_arguments (args1);
21217 tree parmvec = make_tree_vec (1);
21218 tree argvec = make_tree_vec (len1);
21219 tree ta = args1;
21221 /* Setup the parameter vector, which contains only ARG1. */
21222 TREE_VEC_ELT (parmvec, 0) = arg2;
21224 /* Setup the argument vector, which contains the remaining
21225 arguments. */
21226 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21227 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21229 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21230 argvec, DEDUCE_EXACT,
21231 /*subr=*/true, /*explain_p=*/false)
21232 == 0);
21234 /* We cannot deduce in the other direction, because ARG2 is
21235 a pack expansion but ARG1 is not.*/
21236 deduce1 = 0;
21239 else
21241 /* The normal case, where neither argument is a pack
21242 expansion. */
21243 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21244 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21245 == 0);
21246 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21247 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21248 == 0);
21251 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21252 arg2, then arg2 is not as specialized as arg1. */
21253 if (!deduce1)
21254 lose2 = true;
21255 if (!deduce2)
21256 lose1 = true;
21258 /* "If, for a given type, deduction succeeds in both directions
21259 (i.e., the types are identical after the transformations above)
21260 and both P and A were reference types (before being replaced with
21261 the type referred to above):
21262 - if the type from the argument template was an lvalue reference and
21263 the type from the parameter template was not, the argument type is
21264 considered to be more specialized than the other; otherwise,
21265 - if the type from the argument template is more cv-qualified
21266 than the type from the parameter template (as described above),
21267 the argument type is considered to be more specialized than the other;
21268 otherwise,
21269 - neither type is more specialized than the other." */
21271 if (deduce1 && deduce2)
21273 if (ref1 && ref2 && ref1 != ref2)
21275 if (ref1 > ref2)
21276 lose1 = true;
21277 else
21278 lose2 = true;
21280 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21282 if ((quals1 & quals2) == quals2)
21283 lose2 = true;
21284 if ((quals1 & quals2) == quals1)
21285 lose1 = true;
21289 if (lose1 && lose2)
21290 /* We've failed to deduce something in either direction.
21291 These must be unordered. */
21292 break;
21294 next:
21296 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21297 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21298 /* We have already processed all of the arguments in our
21299 handing of the pack expansion type. */
21300 len = 0;
21302 args1 = TREE_CHAIN (args1);
21303 args2 = TREE_CHAIN (args2);
21306 /* "In most cases, all template parameters must have values in order for
21307 deduction to succeed, but for partial ordering purposes a template
21308 parameter may remain without a value provided it is not used in the
21309 types being used for partial ordering."
21311 Thus, if we are missing any of the targs1 we need to substitute into
21312 origs1, then pat2 is not as specialized as pat1. This can happen when
21313 there is a nondeduced context. */
21314 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21315 lose2 = true;
21316 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21317 lose1 = true;
21319 processing_template_decl--;
21321 /* If both deductions succeed, the partial ordering selects the more
21322 constrained template. */
21323 if (!lose1 && !lose2)
21325 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21326 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21327 lose1 = !subsumes_constraints (c1, c2);
21328 lose2 = !subsumes_constraints (c2, c1);
21331 /* All things being equal, if the next argument is a pack expansion
21332 for one function but not for the other, prefer the
21333 non-variadic function. FIXME this is bogus; see c++/41958. */
21334 if (lose1 == lose2
21335 && args1 && TREE_VALUE (args1)
21336 && args2 && TREE_VALUE (args2))
21338 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21339 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21342 if (lose1 == lose2)
21343 return 0;
21344 else if (!lose1)
21345 return 1;
21346 else
21347 return -1;
21350 /* Determine which of two partial specializations of TMPL is more
21351 specialized.
21353 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21354 to the first partial specialization. The TREE_PURPOSE is the
21355 innermost set of template parameters for the partial
21356 specialization. PAT2 is similar, but for the second template.
21358 Return 1 if the first partial specialization is more specialized;
21359 -1 if the second is more specialized; 0 if neither is more
21360 specialized.
21362 See [temp.class.order] for information about determining which of
21363 two templates is more specialized. */
21365 static int
21366 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21368 tree targs;
21369 int winner = 0;
21370 bool any_deductions = false;
21372 tree tmpl1 = TREE_VALUE (pat1);
21373 tree tmpl2 = TREE_VALUE (pat2);
21374 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21375 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21377 /* Just like what happens for functions, if we are ordering between
21378 different template specializations, we may encounter dependent
21379 types in the arguments, and we need our dependency check functions
21380 to behave correctly. */
21381 ++processing_template_decl;
21382 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21383 if (targs)
21385 --winner;
21386 any_deductions = true;
21389 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21390 if (targs)
21392 ++winner;
21393 any_deductions = true;
21395 --processing_template_decl;
21397 /* If both deductions succeed, the partial ordering selects the more
21398 constrained template. */
21399 if (!winner && any_deductions)
21400 return more_constrained (tmpl1, tmpl2);
21402 /* In the case of a tie where at least one of the templates
21403 has a parameter pack at the end, the template with the most
21404 non-packed parameters wins. */
21405 if (winner == 0
21406 && any_deductions
21407 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21408 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21410 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21411 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21412 int len1 = TREE_VEC_LENGTH (args1);
21413 int len2 = TREE_VEC_LENGTH (args2);
21415 /* We don't count the pack expansion at the end. */
21416 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21417 --len1;
21418 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21419 --len2;
21421 if (len1 > len2)
21422 return 1;
21423 else if (len1 < len2)
21424 return -1;
21427 return winner;
21430 /* Return the template arguments that will produce the function signature
21431 DECL from the function template FN, with the explicit template
21432 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21433 also match. Return NULL_TREE if no satisfactory arguments could be
21434 found. */
21436 static tree
21437 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21439 int ntparms = DECL_NTPARMS (fn);
21440 tree targs = make_tree_vec (ntparms);
21441 tree decl_type = TREE_TYPE (decl);
21442 tree decl_arg_types;
21443 tree *args;
21444 unsigned int nargs, ix;
21445 tree arg;
21447 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21449 /* Never do unification on the 'this' parameter. */
21450 decl_arg_types = skip_artificial_parms_for (decl,
21451 TYPE_ARG_TYPES (decl_type));
21453 nargs = list_length (decl_arg_types);
21454 args = XALLOCAVEC (tree, nargs);
21455 for (arg = decl_arg_types, ix = 0;
21456 arg != NULL_TREE && arg != void_list_node;
21457 arg = TREE_CHAIN (arg), ++ix)
21458 args[ix] = TREE_VALUE (arg);
21460 if (fn_type_unification (fn, explicit_args, targs,
21461 args, ix,
21462 (check_rettype || DECL_CONV_FN_P (fn)
21463 ? TREE_TYPE (decl_type) : NULL_TREE),
21464 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21465 /*decltype*/false)
21466 == error_mark_node)
21467 return NULL_TREE;
21469 return targs;
21472 /* Return the innermost template arguments that, when applied to a partial
21473 specialization SPEC_TMPL of TMPL, yield the ARGS.
21475 For example, suppose we have:
21477 template <class T, class U> struct S {};
21478 template <class T> struct S<T*, int> {};
21480 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21481 partial specialization and the ARGS will be {double*, int}. The resulting
21482 vector will be {double}, indicating that `T' is bound to `double'. */
21484 static tree
21485 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21487 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21488 tree spec_args
21489 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21490 int i, ntparms = TREE_VEC_LENGTH (tparms);
21491 tree deduced_args;
21492 tree innermost_deduced_args;
21494 innermost_deduced_args = make_tree_vec (ntparms);
21495 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21497 deduced_args = copy_node (args);
21498 SET_TMPL_ARGS_LEVEL (deduced_args,
21499 TMPL_ARGS_DEPTH (deduced_args),
21500 innermost_deduced_args);
21502 else
21503 deduced_args = innermost_deduced_args;
21505 bool tried_array_deduction = (cxx_dialect < cxx1z);
21506 again:
21507 if (unify (tparms, deduced_args,
21508 INNERMOST_TEMPLATE_ARGS (spec_args),
21509 INNERMOST_TEMPLATE_ARGS (args),
21510 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21511 return NULL_TREE;
21513 for (i = 0; i < ntparms; ++i)
21514 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21516 if (!tried_array_deduction)
21518 try_array_deduction (tparms, innermost_deduced_args,
21519 INNERMOST_TEMPLATE_ARGS (spec_args));
21520 tried_array_deduction = true;
21521 if (TREE_VEC_ELT (innermost_deduced_args, i))
21522 goto again;
21524 return NULL_TREE;
21527 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21528 if (!push_tinst_level (tinst))
21530 excessive_deduction_depth = true;
21531 return NULL_TREE;
21534 /* Verify that nondeduced template arguments agree with the type
21535 obtained from argument deduction.
21537 For example:
21539 struct A { typedef int X; };
21540 template <class T, class U> struct C {};
21541 template <class T> struct C<T, typename T::X> {};
21543 Then with the instantiation `C<A, int>', we can deduce that
21544 `T' is `A' but unify () does not check whether `typename T::X'
21545 is `int'. */
21546 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21547 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21548 spec_args, tmpl,
21549 tf_none, false, false);
21551 pop_tinst_level ();
21553 if (spec_args == error_mark_node
21554 /* We only need to check the innermost arguments; the other
21555 arguments will always agree. */
21556 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21557 INNERMOST_TEMPLATE_ARGS (args)))
21558 return NULL_TREE;
21560 /* Now that we have bindings for all of the template arguments,
21561 ensure that the arguments deduced for the template template
21562 parameters have compatible template parameter lists. See the use
21563 of template_template_parm_bindings_ok_p in fn_type_unification
21564 for more information. */
21565 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21566 return NULL_TREE;
21568 return deduced_args;
21571 // Compare two function templates T1 and T2 by deducing bindings
21572 // from one against the other. If both deductions succeed, compare
21573 // constraints to see which is more constrained.
21574 static int
21575 more_specialized_inst (tree t1, tree t2)
21577 int fate = 0;
21578 int count = 0;
21580 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21582 --fate;
21583 ++count;
21586 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21588 ++fate;
21589 ++count;
21592 // If both deductions succeed, then one may be more constrained.
21593 if (count == 2 && fate == 0)
21594 fate = more_constrained (t1, t2);
21596 return fate;
21599 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21600 Return the TREE_LIST node with the most specialized template, if
21601 any. If there is no most specialized template, the error_mark_node
21602 is returned.
21604 Note that this function does not look at, or modify, the
21605 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21606 returned is one of the elements of INSTANTIATIONS, callers may
21607 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21608 and retrieve it from the value returned. */
21610 tree
21611 most_specialized_instantiation (tree templates)
21613 tree fn, champ;
21615 ++processing_template_decl;
21617 champ = templates;
21618 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21620 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21621 if (fate == -1)
21622 champ = fn;
21623 else if (!fate)
21625 /* Equally specialized, move to next function. If there
21626 is no next function, nothing's most specialized. */
21627 fn = TREE_CHAIN (fn);
21628 champ = fn;
21629 if (!fn)
21630 break;
21634 if (champ)
21635 /* Now verify that champ is better than everything earlier in the
21636 instantiation list. */
21637 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21638 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21640 champ = NULL_TREE;
21641 break;
21645 processing_template_decl--;
21647 if (!champ)
21648 return error_mark_node;
21650 return champ;
21653 /* If DECL is a specialization of some template, return the most
21654 general such template. Otherwise, returns NULL_TREE.
21656 For example, given:
21658 template <class T> struct S { template <class U> void f(U); };
21660 if TMPL is `template <class U> void S<int>::f(U)' this will return
21661 the full template. This function will not trace past partial
21662 specializations, however. For example, given in addition:
21664 template <class T> struct S<T*> { template <class U> void f(U); };
21666 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21667 `template <class T> template <class U> S<T*>::f(U)'. */
21669 tree
21670 most_general_template (tree decl)
21672 if (TREE_CODE (decl) != TEMPLATE_DECL)
21674 if (tree tinfo = get_template_info (decl))
21675 decl = TI_TEMPLATE (tinfo);
21676 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21677 template friend, or a FIELD_DECL for a capture pack. */
21678 if (TREE_CODE (decl) != TEMPLATE_DECL)
21679 return NULL_TREE;
21682 /* Look for more and more general templates. */
21683 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21685 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21686 (See cp-tree.h for details.) */
21687 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21688 break;
21690 if (CLASS_TYPE_P (TREE_TYPE (decl))
21691 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21692 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21693 break;
21695 /* Stop if we run into an explicitly specialized class template. */
21696 if (!DECL_NAMESPACE_SCOPE_P (decl)
21697 && DECL_CONTEXT (decl)
21698 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21699 break;
21701 decl = DECL_TI_TEMPLATE (decl);
21704 return decl;
21707 /* Return the most specialized of the template partial specializations
21708 which can produce TARGET, a specialization of some class or variable
21709 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21710 a TEMPLATE_DECL node corresponding to the partial specialization, while
21711 the TREE_PURPOSE is the set of template arguments that must be
21712 substituted into the template pattern in order to generate TARGET.
21714 If the choice of partial specialization is ambiguous, a diagnostic
21715 is issued, and the error_mark_node is returned. If there are no
21716 partial specializations matching TARGET, then NULL_TREE is
21717 returned, indicating that the primary template should be used. */
21719 static tree
21720 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21722 tree list = NULL_TREE;
21723 tree t;
21724 tree champ;
21725 int fate;
21726 bool ambiguous_p;
21727 tree outer_args = NULL_TREE;
21728 tree tmpl, args;
21730 if (TYPE_P (target))
21732 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21733 tmpl = TI_TEMPLATE (tinfo);
21734 args = TI_ARGS (tinfo);
21736 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21738 tmpl = TREE_OPERAND (target, 0);
21739 args = TREE_OPERAND (target, 1);
21741 else if (VAR_P (target))
21743 tree tinfo = DECL_TEMPLATE_INFO (target);
21744 tmpl = TI_TEMPLATE (tinfo);
21745 args = TI_ARGS (tinfo);
21747 else
21748 gcc_unreachable ();
21750 tree main_tmpl = most_general_template (tmpl);
21752 /* For determining which partial specialization to use, only the
21753 innermost args are interesting. */
21754 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21756 outer_args = strip_innermost_template_args (args, 1);
21757 args = INNERMOST_TEMPLATE_ARGS (args);
21760 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21762 tree spec_args;
21763 tree spec_tmpl = TREE_VALUE (t);
21765 if (outer_args)
21767 /* Substitute in the template args from the enclosing class. */
21768 ++processing_template_decl;
21769 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21770 --processing_template_decl;
21773 if (spec_tmpl == error_mark_node)
21774 return error_mark_node;
21776 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21777 if (spec_args)
21779 if (outer_args)
21780 spec_args = add_to_template_args (outer_args, spec_args);
21782 /* Keep the candidate only if the constraints are satisfied,
21783 or if we're not compiling with concepts. */
21784 if (!flag_concepts
21785 || constraints_satisfied_p (spec_tmpl, spec_args))
21787 list = tree_cons (spec_args, TREE_VALUE (t), list);
21788 TREE_TYPE (list) = TREE_TYPE (t);
21793 if (! list)
21794 return NULL_TREE;
21796 ambiguous_p = false;
21797 t = list;
21798 champ = t;
21799 t = TREE_CHAIN (t);
21800 for (; t; t = TREE_CHAIN (t))
21802 fate = more_specialized_partial_spec (tmpl, champ, t);
21803 if (fate == 1)
21805 else
21807 if (fate == 0)
21809 t = TREE_CHAIN (t);
21810 if (! t)
21812 ambiguous_p = true;
21813 break;
21816 champ = t;
21820 if (!ambiguous_p)
21821 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21823 fate = more_specialized_partial_spec (tmpl, champ, t);
21824 if (fate != 1)
21826 ambiguous_p = true;
21827 break;
21831 if (ambiguous_p)
21833 const char *str;
21834 char *spaces = NULL;
21835 if (!(complain & tf_error))
21836 return error_mark_node;
21837 if (TYPE_P (target))
21838 error ("ambiguous template instantiation for %q#T", target);
21839 else
21840 error ("ambiguous template instantiation for %q#D", target);
21841 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21842 for (t = list; t; t = TREE_CHAIN (t))
21844 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21845 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21846 "%s %#qS", spaces ? spaces : str, subst);
21847 spaces = spaces ? spaces : get_spaces (str);
21849 free (spaces);
21850 return error_mark_node;
21853 return champ;
21856 /* Explicitly instantiate DECL. */
21858 void
21859 do_decl_instantiation (tree decl, tree storage)
21861 tree result = NULL_TREE;
21862 int extern_p = 0;
21864 if (!decl || decl == error_mark_node)
21865 /* An error occurred, for which grokdeclarator has already issued
21866 an appropriate message. */
21867 return;
21868 else if (! DECL_LANG_SPECIFIC (decl))
21870 error ("explicit instantiation of non-template %q#D", decl);
21871 return;
21874 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21875 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21877 if (VAR_P (decl) && !var_templ)
21879 /* There is an asymmetry here in the way VAR_DECLs and
21880 FUNCTION_DECLs are handled by grokdeclarator. In the case of
21881 the latter, the DECL we get back will be marked as a
21882 template instantiation, and the appropriate
21883 DECL_TEMPLATE_INFO will be set up. This does not happen for
21884 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
21885 should handle VAR_DECLs as it currently handles
21886 FUNCTION_DECLs. */
21887 if (!DECL_CLASS_SCOPE_P (decl))
21889 error ("%qD is not a static data member of a class template", decl);
21890 return;
21892 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
21893 if (!result || !VAR_P (result))
21895 error ("no matching template for %qD found", decl);
21896 return;
21898 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
21900 error ("type %qT for explicit instantiation %qD does not match "
21901 "declared type %qT", TREE_TYPE (result), decl,
21902 TREE_TYPE (decl));
21903 return;
21906 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
21908 error ("explicit instantiation of %q#D", decl);
21909 return;
21911 else
21912 result = decl;
21914 /* Check for various error cases. Note that if the explicit
21915 instantiation is valid the RESULT will currently be marked as an
21916 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
21917 until we get here. */
21919 if (DECL_TEMPLATE_SPECIALIZATION (result))
21921 /* DR 259 [temp.spec].
21923 Both an explicit instantiation and a declaration of an explicit
21924 specialization shall not appear in a program unless the explicit
21925 instantiation follows a declaration of the explicit specialization.
21927 For a given set of template parameters, if an explicit
21928 instantiation of a template appears after a declaration of an
21929 explicit specialization for that template, the explicit
21930 instantiation has no effect. */
21931 return;
21933 else if (DECL_EXPLICIT_INSTANTIATION (result))
21935 /* [temp.spec]
21937 No program shall explicitly instantiate any template more
21938 than once.
21940 We check DECL_NOT_REALLY_EXTERN so as not to complain when
21941 the first instantiation was `extern' and the second is not,
21942 and EXTERN_P for the opposite case. */
21943 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
21944 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
21945 /* If an "extern" explicit instantiation follows an ordinary
21946 explicit instantiation, the template is instantiated. */
21947 if (extern_p)
21948 return;
21950 else if (!DECL_IMPLICIT_INSTANTIATION (result))
21952 error ("no matching template for %qD found", result);
21953 return;
21955 else if (!DECL_TEMPLATE_INFO (result))
21957 permerror (input_location, "explicit instantiation of non-template %q#D", result);
21958 return;
21961 if (storage == NULL_TREE)
21963 else if (storage == ridpointers[(int) RID_EXTERN])
21965 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
21966 pedwarn (input_location, OPT_Wpedantic,
21967 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
21968 "instantiations");
21969 extern_p = 1;
21971 else
21972 error ("storage class %qD applied to template instantiation", storage);
21974 check_explicit_instantiation_namespace (result);
21975 mark_decl_instantiated (result, extern_p);
21976 if (! extern_p)
21977 instantiate_decl (result, /*defer_ok=*/true,
21978 /*expl_inst_class_mem_p=*/false);
21981 static void
21982 mark_class_instantiated (tree t, int extern_p)
21984 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
21985 SET_CLASSTYPE_INTERFACE_KNOWN (t);
21986 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
21987 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
21988 if (! extern_p)
21990 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
21991 rest_of_type_compilation (t, 1);
21995 /* Called from do_type_instantiation through binding_table_foreach to
21996 do recursive instantiation for the type bound in ENTRY. */
21997 static void
21998 bt_instantiate_type_proc (binding_entry entry, void *data)
22000 tree storage = *(tree *) data;
22002 if (MAYBE_CLASS_TYPE_P (entry->type)
22003 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22004 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22007 /* Called from do_type_instantiation to instantiate a member
22008 (a member function or a static member variable) of an
22009 explicitly instantiated class template. */
22010 static void
22011 instantiate_class_member (tree decl, int extern_p)
22013 mark_decl_instantiated (decl, extern_p);
22014 if (! extern_p)
22015 instantiate_decl (decl, /*defer_ok=*/true,
22016 /*expl_inst_class_mem_p=*/true);
22019 /* Perform an explicit instantiation of template class T. STORAGE, if
22020 non-null, is the RID for extern, inline or static. COMPLAIN is
22021 nonzero if this is called from the parser, zero if called recursively,
22022 since the standard is unclear (as detailed below). */
22024 void
22025 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22027 int extern_p = 0;
22028 int nomem_p = 0;
22029 int static_p = 0;
22030 int previous_instantiation_extern_p = 0;
22032 if (TREE_CODE (t) == TYPE_DECL)
22033 t = TREE_TYPE (t);
22035 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22037 tree tmpl =
22038 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22039 if (tmpl)
22040 error ("explicit instantiation of non-class template %qD", tmpl);
22041 else
22042 error ("explicit instantiation of non-template type %qT", t);
22043 return;
22046 complete_type (t);
22048 if (!COMPLETE_TYPE_P (t))
22050 if (complain & tf_error)
22051 error ("explicit instantiation of %q#T before definition of template",
22053 return;
22056 if (storage != NULL_TREE)
22058 if (!in_system_header_at (input_location))
22060 if (storage == ridpointers[(int) RID_EXTERN])
22062 if (cxx_dialect == cxx98)
22063 pedwarn (input_location, OPT_Wpedantic,
22064 "ISO C++ 1998 forbids the use of %<extern%> on "
22065 "explicit instantiations");
22067 else
22068 pedwarn (input_location, OPT_Wpedantic,
22069 "ISO C++ forbids the use of %qE"
22070 " on explicit instantiations", storage);
22073 if (storage == ridpointers[(int) RID_INLINE])
22074 nomem_p = 1;
22075 else if (storage == ridpointers[(int) RID_EXTERN])
22076 extern_p = 1;
22077 else if (storage == ridpointers[(int) RID_STATIC])
22078 static_p = 1;
22079 else
22081 error ("storage class %qD applied to template instantiation",
22082 storage);
22083 extern_p = 0;
22087 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22089 /* DR 259 [temp.spec].
22091 Both an explicit instantiation and a declaration of an explicit
22092 specialization shall not appear in a program unless the explicit
22093 instantiation follows a declaration of the explicit specialization.
22095 For a given set of template parameters, if an explicit
22096 instantiation of a template appears after a declaration of an
22097 explicit specialization for that template, the explicit
22098 instantiation has no effect. */
22099 return;
22101 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22103 /* [temp.spec]
22105 No program shall explicitly instantiate any template more
22106 than once.
22108 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22109 instantiation was `extern'. If EXTERN_P then the second is.
22110 These cases are OK. */
22111 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22113 if (!previous_instantiation_extern_p && !extern_p
22114 && (complain & tf_error))
22115 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22117 /* If we've already instantiated the template, just return now. */
22118 if (!CLASSTYPE_INTERFACE_ONLY (t))
22119 return;
22122 check_explicit_instantiation_namespace (TYPE_NAME (t));
22123 mark_class_instantiated (t, extern_p);
22125 if (nomem_p)
22126 return;
22129 tree tmp;
22131 /* In contrast to implicit instantiation, where only the
22132 declarations, and not the definitions, of members are
22133 instantiated, we have here:
22135 [temp.explicit]
22137 The explicit instantiation of a class template specialization
22138 implies the instantiation of all of its members not
22139 previously explicitly specialized in the translation unit
22140 containing the explicit instantiation.
22142 Of course, we can't instantiate member template classes, since
22143 we don't have any arguments for them. Note that the standard
22144 is unclear on whether the instantiation of the members are
22145 *explicit* instantiations or not. However, the most natural
22146 interpretation is that it should be an explicit instantiation. */
22148 if (! static_p)
22149 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22150 if (TREE_CODE (tmp) == FUNCTION_DECL
22151 && DECL_TEMPLATE_INSTANTIATION (tmp)
22152 && user_provided_p (tmp))
22153 instantiate_class_member (tmp, extern_p);
22155 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22156 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22157 instantiate_class_member (tmp, extern_p);
22159 if (CLASSTYPE_NESTED_UTDS (t))
22160 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22161 bt_instantiate_type_proc, &storage);
22165 /* Given a function DECL, which is a specialization of TMPL, modify
22166 DECL to be a re-instantiation of TMPL with the same template
22167 arguments. TMPL should be the template into which tsubst'ing
22168 should occur for DECL, not the most general template.
22170 One reason for doing this is a scenario like this:
22172 template <class T>
22173 void f(const T&, int i);
22175 void g() { f(3, 7); }
22177 template <class T>
22178 void f(const T& t, const int i) { }
22180 Note that when the template is first instantiated, with
22181 instantiate_template, the resulting DECL will have no name for the
22182 first parameter, and the wrong type for the second. So, when we go
22183 to instantiate the DECL, we regenerate it. */
22185 static void
22186 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22188 /* The arguments used to instantiate DECL, from the most general
22189 template. */
22190 tree code_pattern;
22192 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22194 /* Make sure that we can see identifiers, and compute access
22195 correctly. */
22196 push_access_scope (decl);
22198 if (TREE_CODE (decl) == FUNCTION_DECL)
22200 tree decl_parm;
22201 tree pattern_parm;
22202 tree specs;
22203 int args_depth;
22204 int parms_depth;
22206 args_depth = TMPL_ARGS_DEPTH (args);
22207 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22208 if (args_depth > parms_depth)
22209 args = get_innermost_template_args (args, parms_depth);
22211 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22212 args, tf_error, NULL_TREE,
22213 /*defer_ok*/false);
22214 if (specs && specs != error_mark_node)
22215 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22216 specs);
22218 /* Merge parameter declarations. */
22219 decl_parm = skip_artificial_parms_for (decl,
22220 DECL_ARGUMENTS (decl));
22221 pattern_parm
22222 = skip_artificial_parms_for (code_pattern,
22223 DECL_ARGUMENTS (code_pattern));
22224 while (decl_parm && !DECL_PACK_P (pattern_parm))
22226 tree parm_type;
22227 tree attributes;
22229 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22230 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22231 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22232 NULL_TREE);
22233 parm_type = type_decays_to (parm_type);
22234 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22235 TREE_TYPE (decl_parm) = parm_type;
22236 attributes = DECL_ATTRIBUTES (pattern_parm);
22237 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22239 DECL_ATTRIBUTES (decl_parm) = attributes;
22240 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22242 decl_parm = DECL_CHAIN (decl_parm);
22243 pattern_parm = DECL_CHAIN (pattern_parm);
22245 /* Merge any parameters that match with the function parameter
22246 pack. */
22247 if (pattern_parm && DECL_PACK_P (pattern_parm))
22249 int i, len;
22250 tree expanded_types;
22251 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22252 the parameters in this function parameter pack. */
22253 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22254 args, tf_error, NULL_TREE);
22255 len = TREE_VEC_LENGTH (expanded_types);
22256 for (i = 0; i < len; i++)
22258 tree parm_type;
22259 tree attributes;
22261 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22262 /* Rename the parameter to include the index. */
22263 DECL_NAME (decl_parm) =
22264 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22265 parm_type = TREE_VEC_ELT (expanded_types, i);
22266 parm_type = type_decays_to (parm_type);
22267 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22268 TREE_TYPE (decl_parm) = parm_type;
22269 attributes = DECL_ATTRIBUTES (pattern_parm);
22270 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22272 DECL_ATTRIBUTES (decl_parm) = attributes;
22273 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22275 decl_parm = DECL_CHAIN (decl_parm);
22278 /* Merge additional specifiers from the CODE_PATTERN. */
22279 if (DECL_DECLARED_INLINE_P (code_pattern)
22280 && !DECL_DECLARED_INLINE_P (decl))
22281 DECL_DECLARED_INLINE_P (decl) = 1;
22283 else if (VAR_P (decl))
22285 DECL_INITIAL (decl) =
22286 tsubst_expr (DECL_INITIAL (code_pattern), args,
22287 tf_error, DECL_TI_TEMPLATE (decl),
22288 /*integral_constant_expression_p=*/false);
22289 if (VAR_HAD_UNKNOWN_BOUND (decl))
22290 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22291 tf_error, DECL_TI_TEMPLATE (decl));
22293 else
22294 gcc_unreachable ();
22296 pop_access_scope (decl);
22299 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22300 substituted to get DECL. */
22302 tree
22303 template_for_substitution (tree decl)
22305 tree tmpl = DECL_TI_TEMPLATE (decl);
22307 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22308 for the instantiation. This is not always the most general
22309 template. Consider, for example:
22311 template <class T>
22312 struct S { template <class U> void f();
22313 template <> void f<int>(); };
22315 and an instantiation of S<double>::f<int>. We want TD to be the
22316 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22317 while (/* An instantiation cannot have a definition, so we need a
22318 more general template. */
22319 DECL_TEMPLATE_INSTANTIATION (tmpl)
22320 /* We must also deal with friend templates. Given:
22322 template <class T> struct S {
22323 template <class U> friend void f() {};
22326 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22327 so far as the language is concerned, but that's still
22328 where we get the pattern for the instantiation from. On
22329 other hand, if the definition comes outside the class, say:
22331 template <class T> struct S {
22332 template <class U> friend void f();
22334 template <class U> friend void f() {}
22336 we don't need to look any further. That's what the check for
22337 DECL_INITIAL is for. */
22338 || (TREE_CODE (decl) == FUNCTION_DECL
22339 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22340 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22342 /* The present template, TD, should not be a definition. If it
22343 were a definition, we should be using it! Note that we
22344 cannot restructure the loop to just keep going until we find
22345 a template with a definition, since that might go too far if
22346 a specialization was declared, but not defined. */
22348 /* Fetch the more general template. */
22349 tmpl = DECL_TI_TEMPLATE (tmpl);
22352 return tmpl;
22355 /* Returns true if we need to instantiate this template instance even if we
22356 know we aren't going to emit it. */
22358 bool
22359 always_instantiate_p (tree decl)
22361 /* We always instantiate inline functions so that we can inline them. An
22362 explicit instantiation declaration prohibits implicit instantiation of
22363 non-inline functions. With high levels of optimization, we would
22364 normally inline non-inline functions -- but we're not allowed to do
22365 that for "extern template" functions. Therefore, we check
22366 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22367 return ((TREE_CODE (decl) == FUNCTION_DECL
22368 && (DECL_DECLARED_INLINE_P (decl)
22369 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22370 /* And we need to instantiate static data members so that
22371 their initializers are available in integral constant
22372 expressions. */
22373 || (VAR_P (decl)
22374 && decl_maybe_constant_var_p (decl)));
22377 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22378 instantiate it now, modifying TREE_TYPE (fn). */
22380 void
22381 maybe_instantiate_noexcept (tree fn)
22383 tree fntype, spec, noex, clone;
22385 /* Don't instantiate a noexcept-specification from template context. */
22386 if (processing_template_decl)
22387 return;
22389 if (DECL_CLONED_FUNCTION_P (fn))
22390 fn = DECL_CLONED_FUNCTION (fn);
22391 fntype = TREE_TYPE (fn);
22392 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22394 if (!spec || !TREE_PURPOSE (spec))
22395 return;
22397 noex = TREE_PURPOSE (spec);
22399 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22401 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22402 spec = get_defaulted_eh_spec (fn);
22403 else if (push_tinst_level (fn))
22405 push_access_scope (fn);
22406 push_deferring_access_checks (dk_no_deferred);
22407 input_location = DECL_SOURCE_LOCATION (fn);
22408 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22409 DEFERRED_NOEXCEPT_ARGS (noex),
22410 tf_warning_or_error, fn,
22411 /*function_p=*/false,
22412 /*integral_constant_expression_p=*/true);
22413 pop_deferring_access_checks ();
22414 pop_access_scope (fn);
22415 pop_tinst_level ();
22416 spec = build_noexcept_spec (noex, tf_warning_or_error);
22417 if (spec == error_mark_node)
22418 spec = noexcept_false_spec;
22420 else
22421 spec = noexcept_false_spec;
22423 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22426 FOR_EACH_CLONE (clone, fn)
22428 if (TREE_TYPE (clone) == fntype)
22429 TREE_TYPE (clone) = TREE_TYPE (fn);
22430 else
22431 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22435 /* Produce the definition of D, a _DECL generated from a template. If
22436 DEFER_OK is true, then we don't have to actually do the
22437 instantiation now; we just have to do it sometime. Normally it is
22438 an error if this is an explicit instantiation but D is undefined.
22439 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22440 instantiated class template. */
22442 tree
22443 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22445 tree tmpl = DECL_TI_TEMPLATE (d);
22446 tree gen_args;
22447 tree args;
22448 tree td;
22449 tree code_pattern;
22450 tree spec;
22451 tree gen_tmpl;
22452 bool pattern_defined;
22453 location_t saved_loc = input_location;
22454 int saved_unevaluated_operand = cp_unevaluated_operand;
22455 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22456 bool external_p;
22457 bool deleted_p;
22459 /* This function should only be used to instantiate templates for
22460 functions and static member variables. */
22461 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22463 /* A concept is never instantiated. */
22464 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22466 /* Variables are never deferred; if instantiation is required, they
22467 are instantiated right away. That allows for better code in the
22468 case that an expression refers to the value of the variable --
22469 if the variable has a constant value the referring expression can
22470 take advantage of that fact. */
22471 if (VAR_P (d))
22472 defer_ok = false;
22474 /* Don't instantiate cloned functions. Instead, instantiate the
22475 functions they cloned. */
22476 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22477 d = DECL_CLONED_FUNCTION (d);
22479 if (DECL_TEMPLATE_INSTANTIATED (d)
22480 || (TREE_CODE (d) == FUNCTION_DECL
22481 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22482 || DECL_TEMPLATE_SPECIALIZATION (d))
22483 /* D has already been instantiated or explicitly specialized, so
22484 there's nothing for us to do here.
22486 It might seem reasonable to check whether or not D is an explicit
22487 instantiation, and, if so, stop here. But when an explicit
22488 instantiation is deferred until the end of the compilation,
22489 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22490 the instantiation. */
22491 return d;
22493 /* Check to see whether we know that this template will be
22494 instantiated in some other file, as with "extern template"
22495 extension. */
22496 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22498 /* In general, we do not instantiate such templates. */
22499 if (external_p && !always_instantiate_p (d))
22500 return d;
22502 gen_tmpl = most_general_template (tmpl);
22503 gen_args = DECL_TI_ARGS (d);
22505 if (tmpl != gen_tmpl)
22506 /* We should already have the extra args. */
22507 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22508 == TMPL_ARGS_DEPTH (gen_args));
22509 /* And what's in the hash table should match D. */
22510 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22511 || spec == NULL_TREE);
22513 /* This needs to happen before any tsubsting. */
22514 if (! push_tinst_level (d))
22515 return d;
22517 timevar_push (TV_TEMPLATE_INST);
22519 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22520 for the instantiation. */
22521 td = template_for_substitution (d);
22522 args = gen_args;
22524 if (VAR_P (d))
22526 /* Look up an explicit specialization, if any. */
22527 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22528 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22529 if (elt && elt != error_mark_node)
22531 td = TREE_VALUE (elt);
22532 args = TREE_PURPOSE (elt);
22536 code_pattern = DECL_TEMPLATE_RESULT (td);
22538 /* We should never be trying to instantiate a member of a class
22539 template or partial specialization. */
22540 gcc_assert (d != code_pattern);
22542 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22543 || DECL_TEMPLATE_SPECIALIZATION (td))
22544 /* In the case of a friend template whose definition is provided
22545 outside the class, we may have too many arguments. Drop the
22546 ones we don't need. The same is true for specializations. */
22547 args = get_innermost_template_args
22548 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22550 if (TREE_CODE (d) == FUNCTION_DECL)
22552 deleted_p = DECL_DELETED_FN (code_pattern);
22553 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22554 && DECL_INITIAL (code_pattern) != error_mark_node)
22555 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22556 || deleted_p);
22558 else
22560 deleted_p = false;
22561 if (DECL_CLASS_SCOPE_P (code_pattern))
22562 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22563 || DECL_INLINE_VAR_P (code_pattern));
22564 else
22565 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22568 /* We may be in the middle of deferred access check. Disable it now. */
22569 push_deferring_access_checks (dk_no_deferred);
22571 /* Unless an explicit instantiation directive has already determined
22572 the linkage of D, remember that a definition is available for
22573 this entity. */
22574 if (pattern_defined
22575 && !DECL_INTERFACE_KNOWN (d)
22576 && !DECL_NOT_REALLY_EXTERN (d))
22577 mark_definable (d);
22579 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22580 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22581 input_location = DECL_SOURCE_LOCATION (d);
22583 /* If D is a member of an explicitly instantiated class template,
22584 and no definition is available, treat it like an implicit
22585 instantiation. */
22586 if (!pattern_defined && expl_inst_class_mem_p
22587 && DECL_EXPLICIT_INSTANTIATION (d))
22589 /* Leave linkage flags alone on instantiations with anonymous
22590 visibility. */
22591 if (TREE_PUBLIC (d))
22593 DECL_NOT_REALLY_EXTERN (d) = 0;
22594 DECL_INTERFACE_KNOWN (d) = 0;
22596 SET_DECL_IMPLICIT_INSTANTIATION (d);
22599 /* Defer all other templates, unless we have been explicitly
22600 forbidden from doing so. */
22601 if (/* If there is no definition, we cannot instantiate the
22602 template. */
22603 ! pattern_defined
22604 /* If it's OK to postpone instantiation, do so. */
22605 || defer_ok
22606 /* If this is a static data member that will be defined
22607 elsewhere, we don't want to instantiate the entire data
22608 member, but we do want to instantiate the initializer so that
22609 we can substitute that elsewhere. */
22610 || (external_p && VAR_P (d))
22611 /* Handle here a deleted function too, avoid generating
22612 its body (c++/61080). */
22613 || deleted_p)
22615 /* The definition of the static data member is now required so
22616 we must substitute the initializer. */
22617 if (VAR_P (d)
22618 && !DECL_INITIAL (d)
22619 && DECL_INITIAL (code_pattern))
22621 tree ns;
22622 tree init;
22623 bool const_init = false;
22624 bool enter_context = DECL_CLASS_SCOPE_P (d);
22626 ns = decl_namespace_context (d);
22627 push_nested_namespace (ns);
22628 if (enter_context)
22629 push_nested_class (DECL_CONTEXT (d));
22630 init = tsubst_expr (DECL_INITIAL (code_pattern),
22631 args,
22632 tf_warning_or_error, NULL_TREE,
22633 /*integral_constant_expression_p=*/false);
22634 /* If instantiating the initializer involved instantiating this
22635 again, don't call cp_finish_decl twice. */
22636 if (!DECL_INITIAL (d))
22638 /* Make sure the initializer is still constant, in case of
22639 circular dependency (template/instantiate6.C). */
22640 const_init
22641 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22642 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22643 /*asmspec_tree=*/NULL_TREE,
22644 LOOKUP_ONLYCONVERTING);
22646 if (enter_context)
22647 pop_nested_class ();
22648 pop_nested_namespace (ns);
22651 /* We restore the source position here because it's used by
22652 add_pending_template. */
22653 input_location = saved_loc;
22655 if (at_eof && !pattern_defined
22656 && DECL_EXPLICIT_INSTANTIATION (d)
22657 && DECL_NOT_REALLY_EXTERN (d))
22658 /* [temp.explicit]
22660 The definition of a non-exported function template, a
22661 non-exported member function template, or a non-exported
22662 member function or static data member of a class template
22663 shall be present in every translation unit in which it is
22664 explicitly instantiated. */
22665 permerror (input_location, "explicit instantiation of %qD "
22666 "but no definition available", d);
22668 /* If we're in unevaluated context, we just wanted to get the
22669 constant value; this isn't an odr use, so don't queue
22670 a full instantiation. */
22671 if (cp_unevaluated_operand != 0)
22672 goto out;
22673 /* ??? Historically, we have instantiated inline functions, even
22674 when marked as "extern template". */
22675 if (!(external_p && VAR_P (d)))
22676 add_pending_template (d);
22677 goto out;
22679 /* Tell the repository that D is available in this translation unit
22680 -- and see if it is supposed to be instantiated here. */
22681 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22683 /* In a PCH file, despite the fact that the repository hasn't
22684 requested instantiation in the PCH it is still possible that
22685 an instantiation will be required in a file that includes the
22686 PCH. */
22687 if (pch_file)
22688 add_pending_template (d);
22689 /* Instantiate inline functions so that the inliner can do its
22690 job, even though we'll not be emitting a copy of this
22691 function. */
22692 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22693 goto out;
22696 bool push_to_top, nested;
22697 tree fn_context;
22698 fn_context = decl_function_context (d);
22699 nested = current_function_decl != NULL_TREE;
22700 push_to_top = !(nested && fn_context == current_function_decl);
22702 vec<tree> omp_privatization_save;
22703 if (nested)
22704 save_omp_privatization_clauses (omp_privatization_save);
22706 if (push_to_top)
22707 push_to_top_level ();
22708 else
22710 push_function_context ();
22711 cp_unevaluated_operand = 0;
22712 c_inhibit_evaluation_warnings = 0;
22715 /* Mark D as instantiated so that recursive calls to
22716 instantiate_decl do not try to instantiate it again. */
22717 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22719 /* Regenerate the declaration in case the template has been modified
22720 by a subsequent redeclaration. */
22721 regenerate_decl_from_template (d, td, args);
22723 /* We already set the file and line above. Reset them now in case
22724 they changed as a result of calling regenerate_decl_from_template. */
22725 input_location = DECL_SOURCE_LOCATION (d);
22727 if (VAR_P (d))
22729 tree init;
22730 bool const_init = false;
22732 /* Clear out DECL_RTL; whatever was there before may not be right
22733 since we've reset the type of the declaration. */
22734 SET_DECL_RTL (d, NULL);
22735 DECL_IN_AGGR_P (d) = 0;
22737 /* The initializer is placed in DECL_INITIAL by
22738 regenerate_decl_from_template so we don't need to
22739 push/pop_access_scope again here. Pull it out so that
22740 cp_finish_decl can process it. */
22741 init = DECL_INITIAL (d);
22742 DECL_INITIAL (d) = NULL_TREE;
22743 DECL_INITIALIZED_P (d) = 0;
22745 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22746 initializer. That function will defer actual emission until
22747 we have a chance to determine linkage. */
22748 DECL_EXTERNAL (d) = 0;
22750 /* Enter the scope of D so that access-checking works correctly. */
22751 bool enter_context = DECL_CLASS_SCOPE_P (d);
22752 if (enter_context)
22753 push_nested_class (DECL_CONTEXT (d));
22755 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22756 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22758 if (enter_context)
22759 pop_nested_class ();
22761 if (variable_template_p (gen_tmpl))
22762 note_variable_template_instantiation (d);
22764 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22765 synthesize_method (d);
22766 else if (TREE_CODE (d) == FUNCTION_DECL)
22768 hash_map<tree, tree> *saved_local_specializations;
22769 tree tmpl_parm;
22770 tree spec_parm;
22771 tree block = NULL_TREE;
22772 tree lambda_ctx = NULL_TREE;
22774 /* Save away the current list, in case we are instantiating one
22775 template from within the body of another. */
22776 saved_local_specializations = local_specializations;
22778 /* Set up the list of local specializations. */
22779 local_specializations = new hash_map<tree, tree>;
22781 /* Set up context. */
22782 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22783 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22784 block = push_stmt_list ();
22785 else
22787 if (push_to_top && LAMBDA_FUNCTION_P (d))
22789 /* When instantiating a lambda's templated function
22790 operator, we need to push the non-lambda class scope
22791 of the lambda itself so that the nested function
22792 stack is sufficiently correct to deal with this
22793 capture. */
22794 lambda_ctx = DECL_CONTEXT (d);
22796 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22797 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22798 if (lambda_ctx)
22799 push_nested_class (lambda_ctx);
22801 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22804 /* Some typedefs referenced from within the template code need to be
22805 access checked at template instantiation time, i.e now. These
22806 types were added to the template at parsing time. Let's get those
22807 and perform the access checks then. */
22808 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22809 args);
22811 /* Create substitution entries for the parameters. */
22812 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22813 spec_parm = DECL_ARGUMENTS (d);
22814 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22816 register_local_specialization (spec_parm, tmpl_parm);
22817 spec_parm = skip_artificial_parms_for (d, spec_parm);
22818 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22820 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22822 if (!DECL_PACK_P (tmpl_parm))
22824 register_local_specialization (spec_parm, tmpl_parm);
22825 spec_parm = DECL_CHAIN (spec_parm);
22827 else
22829 /* Register the (value) argument pack as a specialization of
22830 TMPL_PARM, then move on. */
22831 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22832 register_local_specialization (argpack, tmpl_parm);
22835 gcc_assert (!spec_parm);
22837 /* Substitute into the body of the function. */
22838 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22839 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22840 tf_warning_or_error, tmpl);
22841 else
22843 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22844 tf_warning_or_error, tmpl,
22845 /*integral_constant_expression_p=*/false);
22847 /* Set the current input_location to the end of the function
22848 so that finish_function knows where we are. */
22849 input_location
22850 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22852 /* Remember if we saw an infinite loop in the template. */
22853 current_function_infinite_loop
22854 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22857 /* We don't need the local specializations any more. */
22858 delete local_specializations;
22859 local_specializations = saved_local_specializations;
22861 /* Finish the function. */
22862 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22863 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22864 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22865 else
22867 d = finish_function (0);
22868 expand_or_defer_fn (d);
22870 if (lambda_ctx)
22871 pop_nested_class ();
22873 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22874 cp_check_omp_declare_reduction (d);
22877 /* We're not deferring instantiation any more. */
22878 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
22880 if (push_to_top)
22881 pop_from_top_level ();
22882 else
22883 pop_function_context ();
22885 if (nested)
22886 restore_omp_privatization_clauses (omp_privatization_save);
22888 out:
22889 pop_deferring_access_checks ();
22890 timevar_pop (TV_TEMPLATE_INST);
22891 pop_tinst_level ();
22892 input_location = saved_loc;
22893 cp_unevaluated_operand = saved_unevaluated_operand;
22894 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
22896 return d;
22899 /* Run through the list of templates that we wish we could
22900 instantiate, and instantiate any we can. RETRIES is the
22901 number of times we retry pending template instantiation. */
22903 void
22904 instantiate_pending_templates (int retries)
22906 int reconsider;
22907 location_t saved_loc = input_location;
22909 /* Instantiating templates may trigger vtable generation. This in turn
22910 may require further template instantiations. We place a limit here
22911 to avoid infinite loop. */
22912 if (pending_templates && retries >= max_tinst_depth)
22914 tree decl = pending_templates->tinst->decl;
22916 fatal_error (input_location,
22917 "template instantiation depth exceeds maximum of %d"
22918 " instantiating %q+D, possibly from virtual table generation"
22919 " (use -ftemplate-depth= to increase the maximum)",
22920 max_tinst_depth, decl);
22921 if (TREE_CODE (decl) == FUNCTION_DECL)
22922 /* Pretend that we defined it. */
22923 DECL_INITIAL (decl) = error_mark_node;
22924 return;
22929 struct pending_template **t = &pending_templates;
22930 struct pending_template *last = NULL;
22931 reconsider = 0;
22932 while (*t)
22934 tree instantiation = reopen_tinst_level ((*t)->tinst);
22935 bool complete = false;
22937 if (TYPE_P (instantiation))
22939 tree fn;
22941 if (!COMPLETE_TYPE_P (instantiation))
22943 instantiate_class_template (instantiation);
22944 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
22945 for (fn = TYPE_METHODS (instantiation);
22947 fn = TREE_CHAIN (fn))
22948 if (! DECL_ARTIFICIAL (fn))
22949 instantiate_decl (fn,
22950 /*defer_ok=*/false,
22951 /*expl_inst_class_mem_p=*/false);
22952 if (COMPLETE_TYPE_P (instantiation))
22953 reconsider = 1;
22956 complete = COMPLETE_TYPE_P (instantiation);
22958 else
22960 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
22961 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
22963 instantiation
22964 = instantiate_decl (instantiation,
22965 /*defer_ok=*/false,
22966 /*expl_inst_class_mem_p=*/false);
22967 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
22968 reconsider = 1;
22971 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
22972 || DECL_TEMPLATE_INSTANTIATED (instantiation));
22975 if (complete)
22976 /* If INSTANTIATION has been instantiated, then we don't
22977 need to consider it again in the future. */
22978 *t = (*t)->next;
22979 else
22981 last = *t;
22982 t = &(*t)->next;
22984 tinst_depth = 0;
22985 current_tinst_level = NULL;
22987 last_pending_template = last;
22989 while (reconsider);
22991 input_location = saved_loc;
22994 /* Substitute ARGVEC into T, which is a list of initializers for
22995 either base class or a non-static data member. The TREE_PURPOSEs
22996 are DECLs, and the TREE_VALUEs are the initializer values. Used by
22997 instantiate_decl. */
22999 static tree
23000 tsubst_initializer_list (tree t, tree argvec)
23002 tree inits = NULL_TREE;
23004 for (; t; t = TREE_CHAIN (t))
23006 tree decl;
23007 tree init;
23008 tree expanded_bases = NULL_TREE;
23009 tree expanded_arguments = NULL_TREE;
23010 int i, len = 1;
23012 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23014 tree expr;
23015 tree arg;
23017 /* Expand the base class expansion type into separate base
23018 classes. */
23019 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23020 tf_warning_or_error,
23021 NULL_TREE);
23022 if (expanded_bases == error_mark_node)
23023 continue;
23025 /* We'll be building separate TREE_LISTs of arguments for
23026 each base. */
23027 len = TREE_VEC_LENGTH (expanded_bases);
23028 expanded_arguments = make_tree_vec (len);
23029 for (i = 0; i < len; i++)
23030 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23032 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23033 expand each argument in the TREE_VALUE of t. */
23034 expr = make_node (EXPR_PACK_EXPANSION);
23035 PACK_EXPANSION_LOCAL_P (expr) = true;
23036 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23037 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23039 if (TREE_VALUE (t) == void_type_node)
23040 /* VOID_TYPE_NODE is used to indicate
23041 value-initialization. */
23043 for (i = 0; i < len; i++)
23044 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23046 else
23048 /* Substitute parameter packs into each argument in the
23049 TREE_LIST. */
23050 in_base_initializer = 1;
23051 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23053 tree expanded_exprs;
23055 /* Expand the argument. */
23056 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23057 expanded_exprs
23058 = tsubst_pack_expansion (expr, argvec,
23059 tf_warning_or_error,
23060 NULL_TREE);
23061 if (expanded_exprs == error_mark_node)
23062 continue;
23064 /* Prepend each of the expanded expressions to the
23065 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23066 for (i = 0; i < len; i++)
23068 TREE_VEC_ELT (expanded_arguments, i) =
23069 tree_cons (NULL_TREE,
23070 TREE_VEC_ELT (expanded_exprs, i),
23071 TREE_VEC_ELT (expanded_arguments, i));
23074 in_base_initializer = 0;
23076 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23077 since we built them backwards. */
23078 for (i = 0; i < len; i++)
23080 TREE_VEC_ELT (expanded_arguments, i) =
23081 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23086 for (i = 0; i < len; ++i)
23088 if (expanded_bases)
23090 decl = TREE_VEC_ELT (expanded_bases, i);
23091 decl = expand_member_init (decl);
23092 init = TREE_VEC_ELT (expanded_arguments, i);
23094 else
23096 tree tmp;
23097 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23098 tf_warning_or_error, NULL_TREE);
23100 decl = expand_member_init (decl);
23101 if (decl && !DECL_P (decl))
23102 in_base_initializer = 1;
23104 init = TREE_VALUE (t);
23105 tmp = init;
23106 if (init != void_type_node)
23107 init = tsubst_expr (init, argvec,
23108 tf_warning_or_error, NULL_TREE,
23109 /*integral_constant_expression_p=*/false);
23110 if (init == NULL_TREE && tmp != NULL_TREE)
23111 /* If we had an initializer but it instantiated to nothing,
23112 value-initialize the object. This will only occur when
23113 the initializer was a pack expansion where the parameter
23114 packs used in that expansion were of length zero. */
23115 init = void_type_node;
23116 in_base_initializer = 0;
23119 if (decl)
23121 init = build_tree_list (decl, init);
23122 TREE_CHAIN (init) = inits;
23123 inits = init;
23127 return inits;
23130 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23132 static void
23133 set_current_access_from_decl (tree decl)
23135 if (TREE_PRIVATE (decl))
23136 current_access_specifier = access_private_node;
23137 else if (TREE_PROTECTED (decl))
23138 current_access_specifier = access_protected_node;
23139 else
23140 current_access_specifier = access_public_node;
23143 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23144 is the instantiation (which should have been created with
23145 start_enum) and ARGS are the template arguments to use. */
23147 static void
23148 tsubst_enum (tree tag, tree newtag, tree args)
23150 tree e;
23152 if (SCOPED_ENUM_P (newtag))
23153 begin_scope (sk_scoped_enum, newtag);
23155 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23157 tree value;
23158 tree decl;
23160 decl = TREE_VALUE (e);
23161 /* Note that in a template enum, the TREE_VALUE is the
23162 CONST_DECL, not the corresponding INTEGER_CST. */
23163 value = tsubst_expr (DECL_INITIAL (decl),
23164 args, tf_warning_or_error, NULL_TREE,
23165 /*integral_constant_expression_p=*/true);
23167 /* Give this enumeration constant the correct access. */
23168 set_current_access_from_decl (decl);
23170 /* Actually build the enumerator itself. Here we're assuming that
23171 enumerators can't have dependent attributes. */
23172 build_enumerator (DECL_NAME (decl), value, newtag,
23173 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23176 if (SCOPED_ENUM_P (newtag))
23177 finish_scope ();
23179 finish_enum_value_list (newtag);
23180 finish_enum (newtag);
23182 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23183 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23186 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23187 its type -- but without substituting the innermost set of template
23188 arguments. So, innermost set of template parameters will appear in
23189 the type. */
23191 tree
23192 get_mostly_instantiated_function_type (tree decl)
23194 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23195 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23198 /* Return truthvalue if we're processing a template different from
23199 the last one involved in diagnostics. */
23200 bool
23201 problematic_instantiation_changed (void)
23203 return current_tinst_level != last_error_tinst_level;
23206 /* Remember current template involved in diagnostics. */
23207 void
23208 record_last_problematic_instantiation (void)
23210 last_error_tinst_level = current_tinst_level;
23213 struct tinst_level *
23214 current_instantiation (void)
23216 return current_tinst_level;
23219 /* Return TRUE if current_function_decl is being instantiated, false
23220 otherwise. */
23222 bool
23223 instantiating_current_function_p (void)
23225 return (current_instantiation ()
23226 && current_instantiation ()->decl == current_function_decl);
23229 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23230 type. Return zero for ok, nonzero for disallowed. Issue error and
23231 warning messages under control of COMPLAIN. */
23233 static int
23234 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23236 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23237 return 0;
23238 else if (POINTER_TYPE_P (type))
23239 return 0;
23240 else if (TYPE_PTRMEM_P (type))
23241 return 0;
23242 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23243 return 0;
23244 else if (TREE_CODE (type) == TYPENAME_TYPE)
23245 return 0;
23246 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23247 return 0;
23248 else if (TREE_CODE (type) == NULLPTR_TYPE)
23249 return 0;
23250 /* A bound template template parm could later be instantiated to have a valid
23251 nontype parm type via an alias template. */
23252 else if (cxx_dialect >= cxx11
23253 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23254 return 0;
23256 if (complain & tf_error)
23258 if (type == error_mark_node)
23259 inform (input_location, "invalid template non-type parameter");
23260 else
23261 error ("%q#T is not a valid type for a template non-type parameter",
23262 type);
23264 return 1;
23267 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23268 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23270 static bool
23271 dependent_type_p_r (tree type)
23273 tree scope;
23275 /* [temp.dep.type]
23277 A type is dependent if it is:
23279 -- a template parameter. Template template parameters are types
23280 for us (since TYPE_P holds true for them) so we handle
23281 them here. */
23282 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23283 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23284 return true;
23285 /* -- a qualified-id with a nested-name-specifier which contains a
23286 class-name that names a dependent type or whose unqualified-id
23287 names a dependent type. */
23288 if (TREE_CODE (type) == TYPENAME_TYPE)
23289 return true;
23291 /* An alias template specialization can be dependent even if the
23292 resulting type is not. */
23293 if (dependent_alias_template_spec_p (type))
23294 return true;
23296 /* -- a cv-qualified type where the cv-unqualified type is
23297 dependent.
23298 No code is necessary for this bullet; the code below handles
23299 cv-qualified types, and we don't want to strip aliases with
23300 TYPE_MAIN_VARIANT because of DR 1558. */
23301 /* -- a compound type constructed from any dependent type. */
23302 if (TYPE_PTRMEM_P (type))
23303 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23304 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23305 (type)));
23306 else if (TYPE_PTR_P (type)
23307 || TREE_CODE (type) == REFERENCE_TYPE)
23308 return dependent_type_p (TREE_TYPE (type));
23309 else if (TREE_CODE (type) == FUNCTION_TYPE
23310 || TREE_CODE (type) == METHOD_TYPE)
23312 tree arg_type;
23314 if (dependent_type_p (TREE_TYPE (type)))
23315 return true;
23316 for (arg_type = TYPE_ARG_TYPES (type);
23317 arg_type;
23318 arg_type = TREE_CHAIN (arg_type))
23319 if (dependent_type_p (TREE_VALUE (arg_type)))
23320 return true;
23321 return false;
23323 /* -- an array type constructed from any dependent type or whose
23324 size is specified by a constant expression that is
23325 value-dependent.
23327 We checked for type- and value-dependence of the bounds in
23328 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23329 if (TREE_CODE (type) == ARRAY_TYPE)
23331 if (TYPE_DOMAIN (type)
23332 && dependent_type_p (TYPE_DOMAIN (type)))
23333 return true;
23334 return dependent_type_p (TREE_TYPE (type));
23337 /* -- a template-id in which either the template name is a template
23338 parameter ... */
23339 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23340 return true;
23341 /* ... or any of the template arguments is a dependent type or
23342 an expression that is type-dependent or value-dependent. */
23343 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23344 && (any_dependent_template_arguments_p
23345 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23346 return true;
23348 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23349 dependent; if the argument of the `typeof' expression is not
23350 type-dependent, then it should already been have resolved. */
23351 if (TREE_CODE (type) == TYPEOF_TYPE
23352 || TREE_CODE (type) == DECLTYPE_TYPE
23353 || TREE_CODE (type) == UNDERLYING_TYPE)
23354 return true;
23356 /* A template argument pack is dependent if any of its packed
23357 arguments are. */
23358 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23360 tree args = ARGUMENT_PACK_ARGS (type);
23361 int i, len = TREE_VEC_LENGTH (args);
23362 for (i = 0; i < len; ++i)
23363 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23364 return true;
23367 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23368 be template parameters. */
23369 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23370 return true;
23372 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23373 return true;
23375 /* The standard does not specifically mention types that are local
23376 to template functions or local classes, but they should be
23377 considered dependent too. For example:
23379 template <int I> void f() {
23380 enum E { a = I };
23381 S<sizeof (E)> s;
23384 The size of `E' cannot be known until the value of `I' has been
23385 determined. Therefore, `E' must be considered dependent. */
23386 scope = TYPE_CONTEXT (type);
23387 if (scope && TYPE_P (scope))
23388 return dependent_type_p (scope);
23389 /* Don't use type_dependent_expression_p here, as it can lead
23390 to infinite recursion trying to determine whether a lambda
23391 nested in a lambda is dependent (c++/47687). */
23392 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23393 && DECL_LANG_SPECIFIC (scope)
23394 && DECL_TEMPLATE_INFO (scope)
23395 && (any_dependent_template_arguments_p
23396 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23397 return true;
23399 /* Other types are non-dependent. */
23400 return false;
23403 /* Returns TRUE if TYPE is dependent, in the sense of
23404 [temp.dep.type]. Note that a NULL type is considered dependent. */
23406 bool
23407 dependent_type_p (tree type)
23409 /* If there are no template parameters in scope, then there can't be
23410 any dependent types. */
23411 if (!processing_template_decl)
23413 /* If we are not processing a template, then nobody should be
23414 providing us with a dependent type. */
23415 gcc_assert (type);
23416 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23417 return false;
23420 /* If the type is NULL, we have not computed a type for the entity
23421 in question; in that case, the type is dependent. */
23422 if (!type)
23423 return true;
23425 /* Erroneous types can be considered non-dependent. */
23426 if (type == error_mark_node)
23427 return false;
23429 /* Getting here with global_type_node means we improperly called this
23430 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23431 gcc_checking_assert (type != global_type_node);
23433 /* If we have not already computed the appropriate value for TYPE,
23434 do so now. */
23435 if (!TYPE_DEPENDENT_P_VALID (type))
23437 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23438 TYPE_DEPENDENT_P_VALID (type) = 1;
23441 return TYPE_DEPENDENT_P (type);
23444 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23445 lookup. In other words, a dependent type that is not the current
23446 instantiation. */
23448 bool
23449 dependent_scope_p (tree scope)
23451 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23452 && !currently_open_class (scope));
23455 /* T is a SCOPE_REF; return whether we need to consider it
23456 instantiation-dependent so that we can check access at instantiation
23457 time even though we know which member it resolves to. */
23459 static bool
23460 instantiation_dependent_scope_ref_p (tree t)
23462 if (DECL_P (TREE_OPERAND (t, 1))
23463 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23464 && accessible_in_template_p (TREE_OPERAND (t, 0),
23465 TREE_OPERAND (t, 1)))
23466 return false;
23467 else
23468 return true;
23471 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23472 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23473 expression. */
23475 /* Note that this predicate is not appropriate for general expressions;
23476 only constant expressions (that satisfy potential_constant_expression)
23477 can be tested for value dependence. */
23479 bool
23480 value_dependent_expression_p (tree expression)
23482 if (!processing_template_decl || expression == NULL_TREE)
23483 return false;
23485 /* A name declared with a dependent type. */
23486 if (DECL_P (expression) && type_dependent_expression_p (expression))
23487 return true;
23489 switch (TREE_CODE (expression))
23491 case BASELINK:
23492 /* A dependent member function of the current instantiation. */
23493 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23495 case FUNCTION_DECL:
23496 /* A dependent member function of the current instantiation. */
23497 if (DECL_CLASS_SCOPE_P (expression)
23498 && dependent_type_p (DECL_CONTEXT (expression)))
23499 return true;
23500 break;
23502 case IDENTIFIER_NODE:
23503 /* A name that has not been looked up -- must be dependent. */
23504 return true;
23506 case TEMPLATE_PARM_INDEX:
23507 /* A non-type template parm. */
23508 return true;
23510 case CONST_DECL:
23511 /* A non-type template parm. */
23512 if (DECL_TEMPLATE_PARM_P (expression))
23513 return true;
23514 return value_dependent_expression_p (DECL_INITIAL (expression));
23516 case VAR_DECL:
23517 /* A constant with literal type and is initialized
23518 with an expression that is value-dependent.
23520 Note that a non-dependent parenthesized initializer will have
23521 already been replaced with its constant value, so if we see
23522 a TREE_LIST it must be dependent. */
23523 if (DECL_INITIAL (expression)
23524 && decl_constant_var_p (expression)
23525 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23526 /* cp_finish_decl doesn't fold reference initializers. */
23527 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23528 || type_dependent_expression_p (DECL_INITIAL (expression))
23529 || value_dependent_expression_p (DECL_INITIAL (expression))))
23530 return true;
23531 if (DECL_HAS_VALUE_EXPR_P (expression))
23533 tree value_expr = DECL_VALUE_EXPR (expression);
23534 if (type_dependent_expression_p (value_expr))
23535 return true;
23537 return false;
23539 case DYNAMIC_CAST_EXPR:
23540 case STATIC_CAST_EXPR:
23541 case CONST_CAST_EXPR:
23542 case REINTERPRET_CAST_EXPR:
23543 case CAST_EXPR:
23544 /* These expressions are value-dependent if the type to which
23545 the cast occurs is dependent or the expression being casted
23546 is value-dependent. */
23548 tree type = TREE_TYPE (expression);
23550 if (dependent_type_p (type))
23551 return true;
23553 /* A functional cast has a list of operands. */
23554 expression = TREE_OPERAND (expression, 0);
23555 if (!expression)
23557 /* If there are no operands, it must be an expression such
23558 as "int()". This should not happen for aggregate types
23559 because it would form non-constant expressions. */
23560 gcc_assert (cxx_dialect >= cxx11
23561 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23563 return false;
23566 if (TREE_CODE (expression) == TREE_LIST)
23567 return any_value_dependent_elements_p (expression);
23569 return value_dependent_expression_p (expression);
23572 case SIZEOF_EXPR:
23573 if (SIZEOF_EXPR_TYPE_P (expression))
23574 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23575 /* FALLTHRU */
23576 case ALIGNOF_EXPR:
23577 case TYPEID_EXPR:
23578 /* A `sizeof' expression is value-dependent if the operand is
23579 type-dependent or is a pack expansion. */
23580 expression = TREE_OPERAND (expression, 0);
23581 if (PACK_EXPANSION_P (expression))
23582 return true;
23583 else if (TYPE_P (expression))
23584 return dependent_type_p (expression);
23585 return instantiation_dependent_uneval_expression_p (expression);
23587 case AT_ENCODE_EXPR:
23588 /* An 'encode' expression is value-dependent if the operand is
23589 type-dependent. */
23590 expression = TREE_OPERAND (expression, 0);
23591 return dependent_type_p (expression);
23593 case NOEXCEPT_EXPR:
23594 expression = TREE_OPERAND (expression, 0);
23595 return instantiation_dependent_uneval_expression_p (expression);
23597 case SCOPE_REF:
23598 /* All instantiation-dependent expressions should also be considered
23599 value-dependent. */
23600 return instantiation_dependent_scope_ref_p (expression);
23602 case COMPONENT_REF:
23603 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23604 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23606 case NONTYPE_ARGUMENT_PACK:
23607 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23608 is value-dependent. */
23610 tree values = ARGUMENT_PACK_ARGS (expression);
23611 int i, len = TREE_VEC_LENGTH (values);
23613 for (i = 0; i < len; ++i)
23614 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23615 return true;
23617 return false;
23620 case TRAIT_EXPR:
23622 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23623 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23624 || (type2 ? dependent_type_p (type2) : false));
23627 case MODOP_EXPR:
23628 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23629 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23631 case ARRAY_REF:
23632 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23633 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23635 case ADDR_EXPR:
23637 tree op = TREE_OPERAND (expression, 0);
23638 return (value_dependent_expression_p (op)
23639 || has_value_dependent_address (op));
23642 case REQUIRES_EXPR:
23643 /* Treat all requires-expressions as value-dependent so
23644 we don't try to fold them. */
23645 return true;
23647 case TYPE_REQ:
23648 return dependent_type_p (TREE_OPERAND (expression, 0));
23650 case CALL_EXPR:
23652 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23653 return true;
23654 tree fn = get_callee_fndecl (expression);
23655 int i, nargs;
23656 nargs = call_expr_nargs (expression);
23657 for (i = 0; i < nargs; ++i)
23659 tree op = CALL_EXPR_ARG (expression, i);
23660 /* In a call to a constexpr member function, look through the
23661 implicit ADDR_EXPR on the object argument so that it doesn't
23662 cause the call to be considered value-dependent. We also
23663 look through it in potential_constant_expression. */
23664 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23665 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23666 && TREE_CODE (op) == ADDR_EXPR)
23667 op = TREE_OPERAND (op, 0);
23668 if (value_dependent_expression_p (op))
23669 return true;
23671 return false;
23674 case TEMPLATE_ID_EXPR:
23675 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23676 type-dependent. */
23677 return type_dependent_expression_p (expression)
23678 || variable_concept_p (TREE_OPERAND (expression, 0));
23680 case CONSTRUCTOR:
23682 unsigned ix;
23683 tree val;
23684 if (dependent_type_p (TREE_TYPE (expression)))
23685 return true;
23686 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23687 if (value_dependent_expression_p (val))
23688 return true;
23689 return false;
23692 case STMT_EXPR:
23693 /* Treat a GNU statement expression as dependent to avoid crashing
23694 under instantiate_non_dependent_expr; it can't be constant. */
23695 return true;
23697 default:
23698 /* A constant expression is value-dependent if any subexpression is
23699 value-dependent. */
23700 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23702 case tcc_reference:
23703 case tcc_unary:
23704 case tcc_comparison:
23705 case tcc_binary:
23706 case tcc_expression:
23707 case tcc_vl_exp:
23709 int i, len = cp_tree_operand_length (expression);
23711 for (i = 0; i < len; i++)
23713 tree t = TREE_OPERAND (expression, i);
23715 /* In some cases, some of the operands may be missing.l
23716 (For example, in the case of PREDECREMENT_EXPR, the
23717 amount to increment by may be missing.) That doesn't
23718 make the expression dependent. */
23719 if (t && value_dependent_expression_p (t))
23720 return true;
23723 break;
23724 default:
23725 break;
23727 break;
23730 /* The expression is not value-dependent. */
23731 return false;
23734 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23735 [temp.dep.expr]. Note that an expression with no type is
23736 considered dependent. Other parts of the compiler arrange for an
23737 expression with type-dependent subexpressions to have no type, so
23738 this function doesn't have to be fully recursive. */
23740 bool
23741 type_dependent_expression_p (tree expression)
23743 if (!processing_template_decl)
23744 return false;
23746 if (expression == NULL_TREE || expression == error_mark_node)
23747 return false;
23749 /* An unresolved name is always dependent. */
23750 if (identifier_p (expression)
23751 || TREE_CODE (expression) == USING_DECL
23752 || TREE_CODE (expression) == WILDCARD_DECL)
23753 return true;
23755 /* A fold expression is type-dependent. */
23756 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23757 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23758 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23759 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23760 return true;
23762 /* Some expression forms are never type-dependent. */
23763 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23764 || TREE_CODE (expression) == SIZEOF_EXPR
23765 || TREE_CODE (expression) == ALIGNOF_EXPR
23766 || TREE_CODE (expression) == AT_ENCODE_EXPR
23767 || TREE_CODE (expression) == NOEXCEPT_EXPR
23768 || TREE_CODE (expression) == TRAIT_EXPR
23769 || TREE_CODE (expression) == TYPEID_EXPR
23770 || TREE_CODE (expression) == DELETE_EXPR
23771 || TREE_CODE (expression) == VEC_DELETE_EXPR
23772 || TREE_CODE (expression) == THROW_EXPR
23773 || TREE_CODE (expression) == REQUIRES_EXPR)
23774 return false;
23776 /* The types of these expressions depends only on the type to which
23777 the cast occurs. */
23778 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23779 || TREE_CODE (expression) == STATIC_CAST_EXPR
23780 || TREE_CODE (expression) == CONST_CAST_EXPR
23781 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23782 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23783 || TREE_CODE (expression) == CAST_EXPR)
23784 return dependent_type_p (TREE_TYPE (expression));
23786 /* The types of these expressions depends only on the type created
23787 by the expression. */
23788 if (TREE_CODE (expression) == NEW_EXPR
23789 || TREE_CODE (expression) == VEC_NEW_EXPR)
23791 /* For NEW_EXPR tree nodes created inside a template, either
23792 the object type itself or a TREE_LIST may appear as the
23793 operand 1. */
23794 tree type = TREE_OPERAND (expression, 1);
23795 if (TREE_CODE (type) == TREE_LIST)
23796 /* This is an array type. We need to check array dimensions
23797 as well. */
23798 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23799 || value_dependent_expression_p
23800 (TREE_OPERAND (TREE_VALUE (type), 1));
23801 else
23802 return dependent_type_p (type);
23805 if (TREE_CODE (expression) == SCOPE_REF)
23807 tree scope = TREE_OPERAND (expression, 0);
23808 tree name = TREE_OPERAND (expression, 1);
23810 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23811 contains an identifier associated by name lookup with one or more
23812 declarations declared with a dependent type, or...a
23813 nested-name-specifier or qualified-id that names a member of an
23814 unknown specialization. */
23815 return (type_dependent_expression_p (name)
23816 || dependent_scope_p (scope));
23819 if (TREE_CODE (expression) == TEMPLATE_DECL
23820 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23821 return uses_outer_template_parms (expression);
23823 if (TREE_CODE (expression) == STMT_EXPR)
23824 expression = stmt_expr_value_expr (expression);
23826 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23828 tree elt;
23829 unsigned i;
23831 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23833 if (type_dependent_expression_p (elt))
23834 return true;
23836 return false;
23839 /* A static data member of the current instantiation with incomplete
23840 array type is type-dependent, as the definition and specializations
23841 can have different bounds. */
23842 if (VAR_P (expression)
23843 && DECL_CLASS_SCOPE_P (expression)
23844 && dependent_type_p (DECL_CONTEXT (expression))
23845 && VAR_HAD_UNKNOWN_BOUND (expression))
23846 return true;
23848 /* An array of unknown bound depending on a variadic parameter, eg:
23850 template<typename... Args>
23851 void foo (Args... args)
23853 int arr[] = { args... };
23856 template<int... vals>
23857 void bar ()
23859 int arr[] = { vals... };
23862 If the array has no length and has an initializer, it must be that
23863 we couldn't determine its length in cp_complete_array_type because
23864 it is dependent. */
23865 if (VAR_P (expression)
23866 && TREE_TYPE (expression) != NULL_TREE
23867 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23868 && !TYPE_DOMAIN (TREE_TYPE (expression))
23869 && DECL_INITIAL (expression))
23870 return true;
23872 /* A function or variable template-id is type-dependent if it has any
23873 dependent template arguments. Note that we only consider the innermost
23874 template arguments here, since those are the ones that come from the
23875 template-id; the template arguments for the enclosing class do not make it
23876 type-dependent, they only make a member function value-dependent. */
23877 if (VAR_OR_FUNCTION_DECL_P (expression)
23878 && DECL_LANG_SPECIFIC (expression)
23879 && DECL_TEMPLATE_INFO (expression)
23880 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
23881 && (any_dependent_template_arguments_p
23882 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
23883 return true;
23885 /* Always dependent, on the number of arguments if nothing else. */
23886 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
23887 return true;
23889 if (TREE_TYPE (expression) == unknown_type_node)
23891 if (TREE_CODE (expression) == ADDR_EXPR)
23892 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
23893 if (TREE_CODE (expression) == COMPONENT_REF
23894 || TREE_CODE (expression) == OFFSET_REF)
23896 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
23897 return true;
23898 expression = TREE_OPERAND (expression, 1);
23899 if (identifier_p (expression))
23900 return false;
23902 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
23903 if (TREE_CODE (expression) == SCOPE_REF)
23904 return false;
23906 if (BASELINK_P (expression))
23908 if (BASELINK_OPTYPE (expression)
23909 && dependent_type_p (BASELINK_OPTYPE (expression)))
23910 return true;
23911 expression = BASELINK_FUNCTIONS (expression);
23914 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
23916 if (any_dependent_template_arguments_p
23917 (TREE_OPERAND (expression, 1)))
23918 return true;
23919 expression = TREE_OPERAND (expression, 0);
23920 if (identifier_p (expression))
23921 return true;
23924 gcc_assert (TREE_CODE (expression) == OVERLOAD
23925 || TREE_CODE (expression) == FUNCTION_DECL);
23927 for (lkp_iterator iter (expression); iter; ++iter)
23928 if (type_dependent_expression_p (*iter))
23929 return true;
23931 return false;
23934 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
23936 /* Dependent type attributes might not have made it from the decl to
23937 the type yet. */
23938 if (DECL_P (expression)
23939 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
23940 return true;
23942 return (dependent_type_p (TREE_TYPE (expression)));
23945 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
23946 type-dependent if the expression refers to a member of the current
23947 instantiation and the type of the referenced member is dependent, or the
23948 class member access expression refers to a member of an unknown
23949 specialization.
23951 This function returns true if the OBJECT in such a class member access
23952 expression is of an unknown specialization. */
23954 bool
23955 type_dependent_object_expression_p (tree object)
23957 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
23958 dependent. */
23959 if (TREE_CODE (object) == IDENTIFIER_NODE)
23960 return true;
23961 tree scope = TREE_TYPE (object);
23962 return (!scope || dependent_scope_p (scope));
23965 /* walk_tree callback function for instantiation_dependent_expression_p,
23966 below. Returns non-zero if a dependent subexpression is found. */
23968 static tree
23969 instantiation_dependent_r (tree *tp, int *walk_subtrees,
23970 void * /*data*/)
23972 if (TYPE_P (*tp))
23974 /* We don't have to worry about decltype currently because decltype
23975 of an instantiation-dependent expr is a dependent type. This
23976 might change depending on the resolution of DR 1172. */
23977 *walk_subtrees = false;
23978 return NULL_TREE;
23980 enum tree_code code = TREE_CODE (*tp);
23981 switch (code)
23983 /* Don't treat an argument list as dependent just because it has no
23984 TREE_TYPE. */
23985 case TREE_LIST:
23986 case TREE_VEC:
23987 return NULL_TREE;
23989 case TEMPLATE_PARM_INDEX:
23990 return *tp;
23992 /* Handle expressions with type operands. */
23993 case SIZEOF_EXPR:
23994 case ALIGNOF_EXPR:
23995 case TYPEID_EXPR:
23996 case AT_ENCODE_EXPR:
23998 tree op = TREE_OPERAND (*tp, 0);
23999 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24000 op = TREE_TYPE (op);
24001 if (TYPE_P (op))
24003 if (dependent_type_p (op))
24004 return *tp;
24005 else
24007 *walk_subtrees = false;
24008 return NULL_TREE;
24011 break;
24014 case COMPONENT_REF:
24015 if (identifier_p (TREE_OPERAND (*tp, 1)))
24016 /* In a template, finish_class_member_access_expr creates a
24017 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24018 type-dependent, so that we can check access control at
24019 instantiation time (PR 42277). See also Core issue 1273. */
24020 return *tp;
24021 break;
24023 case SCOPE_REF:
24024 if (instantiation_dependent_scope_ref_p (*tp))
24025 return *tp;
24026 else
24027 break;
24029 /* Treat statement-expressions as dependent. */
24030 case BIND_EXPR:
24031 return *tp;
24033 /* Treat requires-expressions as dependent. */
24034 case REQUIRES_EXPR:
24035 return *tp;
24037 case CALL_EXPR:
24038 /* Treat calls to function concepts as dependent. */
24039 if (function_concept_check_p (*tp))
24040 return *tp;
24041 break;
24043 case TEMPLATE_ID_EXPR:
24044 /* And variable concepts. */
24045 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24046 return *tp;
24047 break;
24049 default:
24050 break;
24053 if (type_dependent_expression_p (*tp))
24054 return *tp;
24055 else
24056 return NULL_TREE;
24059 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24060 sense defined by the ABI:
24062 "An expression is instantiation-dependent if it is type-dependent
24063 or value-dependent, or it has a subexpression that is type-dependent
24064 or value-dependent."
24066 Except don't actually check value-dependence for unevaluated expressions,
24067 because in sizeof(i) we don't care about the value of i. Checking
24068 type-dependence will in turn check value-dependence of array bounds/template
24069 arguments as needed. */
24071 bool
24072 instantiation_dependent_uneval_expression_p (tree expression)
24074 tree result;
24076 if (!processing_template_decl)
24077 return false;
24079 if (expression == error_mark_node)
24080 return false;
24082 result = cp_walk_tree_without_duplicates (&expression,
24083 instantiation_dependent_r, NULL);
24084 return result != NULL_TREE;
24087 /* As above, but also check value-dependence of the expression as a whole. */
24089 bool
24090 instantiation_dependent_expression_p (tree expression)
24092 return (instantiation_dependent_uneval_expression_p (expression)
24093 || value_dependent_expression_p (expression));
24096 /* Like type_dependent_expression_p, but it also works while not processing
24097 a template definition, i.e. during substitution or mangling. */
24099 bool
24100 type_dependent_expression_p_push (tree expr)
24102 bool b;
24103 ++processing_template_decl;
24104 b = type_dependent_expression_p (expr);
24105 --processing_template_decl;
24106 return b;
24109 /* Returns TRUE if ARGS contains a type-dependent expression. */
24111 bool
24112 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24114 unsigned int i;
24115 tree arg;
24117 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24119 if (type_dependent_expression_p (arg))
24120 return true;
24122 return false;
24125 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24126 expressions) contains any type-dependent expressions. */
24128 bool
24129 any_type_dependent_elements_p (const_tree list)
24131 for (; list; list = TREE_CHAIN (list))
24132 if (type_dependent_expression_p (TREE_VALUE (list)))
24133 return true;
24135 return false;
24138 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24139 expressions) contains any value-dependent expressions. */
24141 bool
24142 any_value_dependent_elements_p (const_tree list)
24144 for (; list; list = TREE_CHAIN (list))
24145 if (value_dependent_expression_p (TREE_VALUE (list)))
24146 return true;
24148 return false;
24151 /* Returns TRUE if the ARG (a template argument) is dependent. */
24153 bool
24154 dependent_template_arg_p (tree arg)
24156 if (!processing_template_decl)
24157 return false;
24159 /* Assume a template argument that was wrongly written by the user
24160 is dependent. This is consistent with what
24161 any_dependent_template_arguments_p [that calls this function]
24162 does. */
24163 if (!arg || arg == error_mark_node)
24164 return true;
24166 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24167 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24169 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24170 return true;
24171 if (TREE_CODE (arg) == TEMPLATE_DECL)
24173 if (DECL_TEMPLATE_PARM_P (arg))
24174 return true;
24175 /* A member template of a dependent class is not necessarily
24176 type-dependent, but it is a dependent template argument because it
24177 will be a member of an unknown specialization to that template. */
24178 tree scope = CP_DECL_CONTEXT (arg);
24179 return TYPE_P (scope) && dependent_type_p (scope);
24181 else if (ARGUMENT_PACK_P (arg))
24183 tree args = ARGUMENT_PACK_ARGS (arg);
24184 int i, len = TREE_VEC_LENGTH (args);
24185 for (i = 0; i < len; ++i)
24187 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24188 return true;
24191 return false;
24193 else if (TYPE_P (arg))
24194 return dependent_type_p (arg);
24195 else
24196 return (type_dependent_expression_p (arg)
24197 || value_dependent_expression_p (arg));
24200 /* Returns true if ARGS (a collection of template arguments) contains
24201 any types that require structural equality testing. */
24203 bool
24204 any_template_arguments_need_structural_equality_p (tree args)
24206 int i;
24207 int j;
24209 if (!args)
24210 return false;
24211 if (args == error_mark_node)
24212 return true;
24214 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24216 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24217 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24219 tree arg = TREE_VEC_ELT (level, j);
24220 tree packed_args = NULL_TREE;
24221 int k, len = 1;
24223 if (ARGUMENT_PACK_P (arg))
24225 /* Look inside the argument pack. */
24226 packed_args = ARGUMENT_PACK_ARGS (arg);
24227 len = TREE_VEC_LENGTH (packed_args);
24230 for (k = 0; k < len; ++k)
24232 if (packed_args)
24233 arg = TREE_VEC_ELT (packed_args, k);
24235 if (error_operand_p (arg))
24236 return true;
24237 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24238 continue;
24239 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24240 return true;
24241 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24242 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24243 return true;
24248 return false;
24251 /* Returns true if ARGS (a collection of template arguments) contains
24252 any dependent arguments. */
24254 bool
24255 any_dependent_template_arguments_p (const_tree args)
24257 int i;
24258 int j;
24260 if (!args)
24261 return false;
24262 if (args == error_mark_node)
24263 return true;
24265 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24267 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24268 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24269 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24270 return true;
24273 return false;
24276 /* Returns TRUE if the template TMPL is type-dependent. */
24278 bool
24279 dependent_template_p (tree tmpl)
24281 if (TREE_CODE (tmpl) == OVERLOAD)
24283 for (lkp_iterator iter (tmpl); iter; ++iter)
24284 if (dependent_template_p (*iter))
24285 return true;
24286 return false;
24289 /* Template template parameters are dependent. */
24290 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24291 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24292 return true;
24293 /* So are names that have not been looked up. */
24294 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24295 return true;
24296 return false;
24299 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24301 bool
24302 dependent_template_id_p (tree tmpl, tree args)
24304 return (dependent_template_p (tmpl)
24305 || any_dependent_template_arguments_p (args));
24308 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24309 are dependent. */
24311 bool
24312 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24314 int i;
24316 if (!processing_template_decl)
24317 return false;
24319 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24321 tree decl = TREE_VEC_ELT (declv, i);
24322 tree init = TREE_VEC_ELT (initv, i);
24323 tree cond = TREE_VEC_ELT (condv, i);
24324 tree incr = TREE_VEC_ELT (incrv, i);
24326 if (type_dependent_expression_p (decl)
24327 || TREE_CODE (decl) == SCOPE_REF)
24328 return true;
24330 if (init && type_dependent_expression_p (init))
24331 return true;
24333 if (type_dependent_expression_p (cond))
24334 return true;
24336 if (COMPARISON_CLASS_P (cond)
24337 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24338 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24339 return true;
24341 if (TREE_CODE (incr) == MODOP_EXPR)
24343 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24344 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24345 return true;
24347 else if (type_dependent_expression_p (incr))
24348 return true;
24349 else if (TREE_CODE (incr) == MODIFY_EXPR)
24351 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24352 return true;
24353 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24355 tree t = TREE_OPERAND (incr, 1);
24356 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24357 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24358 return true;
24363 return false;
24366 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24367 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24368 no such TYPE can be found. Note that this function peers inside
24369 uninstantiated templates and therefore should be used only in
24370 extremely limited situations. ONLY_CURRENT_P restricts this
24371 peering to the currently open classes hierarchy (which is required
24372 when comparing types). */
24374 tree
24375 resolve_typename_type (tree type, bool only_current_p)
24377 tree scope;
24378 tree name;
24379 tree decl;
24380 int quals;
24381 tree pushed_scope;
24382 tree result;
24384 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24386 scope = TYPE_CONTEXT (type);
24387 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24388 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24389 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24390 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24391 identifier of the TYPENAME_TYPE anymore.
24392 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24393 TYPENAME_TYPE instead, we avoid messing up with a possible
24394 typedef variant case. */
24395 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24397 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24398 it first before we can figure out what NAME refers to. */
24399 if (TREE_CODE (scope) == TYPENAME_TYPE)
24401 if (TYPENAME_IS_RESOLVING_P (scope))
24402 /* Given a class template A with a dependent base with nested type C,
24403 typedef typename A::C::C C will land us here, as trying to resolve
24404 the initial A::C leads to the local C typedef, which leads back to
24405 A::C::C. So we break the recursion now. */
24406 return type;
24407 else
24408 scope = resolve_typename_type (scope, only_current_p);
24410 /* If we don't know what SCOPE refers to, then we cannot resolve the
24411 TYPENAME_TYPE. */
24412 if (!CLASS_TYPE_P (scope))
24413 return type;
24414 /* If this is a typedef, we don't want to look inside (c++/11987). */
24415 if (typedef_variant_p (type))
24416 return type;
24417 /* If SCOPE isn't the template itself, it will not have a valid
24418 TYPE_FIELDS list. */
24419 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24420 /* scope is either the template itself or a compatible instantiation
24421 like X<T>, so look up the name in the original template. */
24422 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24423 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24424 gcc_checking_assert (uses_template_parms (scope));
24425 /* If scope has no fields, it can't be a current instantiation. Check this
24426 before currently_open_class to avoid infinite recursion (71515). */
24427 if (!TYPE_FIELDS (scope))
24428 return type;
24429 /* If the SCOPE is not the current instantiation, there's no reason
24430 to look inside it. */
24431 if (only_current_p && !currently_open_class (scope))
24432 return type;
24433 /* Enter the SCOPE so that name lookup will be resolved as if we
24434 were in the class definition. In particular, SCOPE will no
24435 longer be considered a dependent type. */
24436 pushed_scope = push_scope (scope);
24437 /* Look up the declaration. */
24438 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24439 tf_warning_or_error);
24441 result = NULL_TREE;
24443 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24444 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24445 if (!decl)
24446 /*nop*/;
24447 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24448 && TREE_CODE (decl) == TYPE_DECL)
24450 result = TREE_TYPE (decl);
24451 if (result == error_mark_node)
24452 result = NULL_TREE;
24454 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24455 && DECL_CLASS_TEMPLATE_P (decl))
24457 tree tmpl;
24458 tree args;
24459 /* Obtain the template and the arguments. */
24460 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24461 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24462 /* Instantiate the template. */
24463 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24464 /*entering_scope=*/0,
24465 tf_error | tf_user);
24466 if (result == error_mark_node)
24467 result = NULL_TREE;
24470 /* Leave the SCOPE. */
24471 if (pushed_scope)
24472 pop_scope (pushed_scope);
24474 /* If we failed to resolve it, return the original typename. */
24475 if (!result)
24476 return type;
24478 /* If lookup found a typename type, resolve that too. */
24479 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24481 /* Ill-formed programs can cause infinite recursion here, so we
24482 must catch that. */
24483 TYPENAME_IS_RESOLVING_P (result) = 1;
24484 result = resolve_typename_type (result, only_current_p);
24485 TYPENAME_IS_RESOLVING_P (result) = 0;
24488 /* Qualify the resulting type. */
24489 quals = cp_type_quals (type);
24490 if (quals)
24491 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24493 return result;
24496 /* EXPR is an expression which is not type-dependent. Return a proxy
24497 for EXPR that can be used to compute the types of larger
24498 expressions containing EXPR. */
24500 tree
24501 build_non_dependent_expr (tree expr)
24503 tree inner_expr;
24505 /* When checking, try to get a constant value for all non-dependent
24506 expressions in order to expose bugs in *_dependent_expression_p
24507 and constexpr. This can affect code generation, see PR70704, so
24508 only do this for -fchecking=2. */
24509 if (flag_checking > 1
24510 && cxx_dialect >= cxx11
24511 /* Don't do this during nsdmi parsing as it can lead to
24512 unexpected recursive instantiations. */
24513 && !parsing_nsdmi ()
24514 /* Don't do this during concept expansion either and for
24515 the same reason. */
24516 && !expanding_concept ())
24517 fold_non_dependent_expr (expr);
24519 /* Preserve OVERLOADs; the functions must be available to resolve
24520 types. */
24521 inner_expr = expr;
24522 if (TREE_CODE (inner_expr) == STMT_EXPR)
24523 inner_expr = stmt_expr_value_expr (inner_expr);
24524 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24525 inner_expr = TREE_OPERAND (inner_expr, 0);
24526 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24527 inner_expr = TREE_OPERAND (inner_expr, 1);
24528 if (is_overloaded_fn (inner_expr)
24529 || TREE_CODE (inner_expr) == OFFSET_REF)
24530 return expr;
24531 /* There is no need to return a proxy for a variable. */
24532 if (VAR_P (expr))
24533 return expr;
24534 /* Preserve string constants; conversions from string constants to
24535 "char *" are allowed, even though normally a "const char *"
24536 cannot be used to initialize a "char *". */
24537 if (TREE_CODE (expr) == STRING_CST)
24538 return expr;
24539 /* Preserve void and arithmetic constants, as an optimization -- there is no
24540 reason to create a new node. */
24541 if (TREE_CODE (expr) == VOID_CST
24542 || TREE_CODE (expr) == INTEGER_CST
24543 || TREE_CODE (expr) == REAL_CST)
24544 return expr;
24545 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24546 There is at least one place where we want to know that a
24547 particular expression is a throw-expression: when checking a ?:
24548 expression, there are special rules if the second or third
24549 argument is a throw-expression. */
24550 if (TREE_CODE (expr) == THROW_EXPR)
24551 return expr;
24553 /* Don't wrap an initializer list, we need to be able to look inside. */
24554 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24555 return expr;
24557 /* Don't wrap a dummy object, we need to be able to test for it. */
24558 if (is_dummy_object (expr))
24559 return expr;
24561 if (TREE_CODE (expr) == COND_EXPR)
24562 return build3 (COND_EXPR,
24563 TREE_TYPE (expr),
24564 TREE_OPERAND (expr, 0),
24565 (TREE_OPERAND (expr, 1)
24566 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24567 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24568 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24569 if (TREE_CODE (expr) == COMPOUND_EXPR
24570 && !COMPOUND_EXPR_OVERLOADED (expr))
24571 return build2 (COMPOUND_EXPR,
24572 TREE_TYPE (expr),
24573 TREE_OPERAND (expr, 0),
24574 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24576 /* If the type is unknown, it can't really be non-dependent */
24577 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24579 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24580 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24583 /* ARGS is a vector of expressions as arguments to a function call.
24584 Replace the arguments with equivalent non-dependent expressions.
24585 This modifies ARGS in place. */
24587 void
24588 make_args_non_dependent (vec<tree, va_gc> *args)
24590 unsigned int ix;
24591 tree arg;
24593 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24595 tree newarg = build_non_dependent_expr (arg);
24596 if (newarg != arg)
24597 (*args)[ix] = newarg;
24601 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24602 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24603 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24605 static tree
24606 make_auto_1 (tree name, bool set_canonical)
24608 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24609 TYPE_NAME (au) = build_decl (input_location,
24610 TYPE_DECL, name, au);
24611 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24612 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24613 (0, processing_template_decl + 1, processing_template_decl + 1,
24614 TYPE_NAME (au), NULL_TREE);
24615 if (set_canonical)
24616 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24617 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24618 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24620 return au;
24623 tree
24624 make_decltype_auto (void)
24626 return make_auto_1 (decltype_auto_identifier, true);
24629 tree
24630 make_auto (void)
24632 return make_auto_1 (auto_identifier, true);
24635 /* Return a C++17 deduction placeholder for class template TMPL. */
24637 tree
24638 make_template_placeholder (tree tmpl)
24640 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24641 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24642 return t;
24645 /* Make a "constrained auto" type-specifier. This is an
24646 auto type with constraints that must be associated after
24647 deduction. The constraint is formed from the given
24648 CONC and its optional sequence of arguments, which are
24649 non-null if written as partial-concept-id. */
24651 tree
24652 make_constrained_auto (tree con, tree args)
24654 tree type = make_auto_1 (auto_identifier, false);
24656 /* Build the constraint. */
24657 tree tmpl = DECL_TI_TEMPLATE (con);
24658 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
24659 expr = build_concept_check (expr, type, args);
24661 tree constr = normalize_expression (expr);
24662 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24664 /* Our canonical type depends on the constraint. */
24665 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24667 /* Attach the constraint to the type declaration. */
24668 tree decl = TYPE_NAME (type);
24669 return decl;
24672 /* Given type ARG, return std::initializer_list<ARG>. */
24674 static tree
24675 listify (tree arg)
24677 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
24678 tree argvec;
24679 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24681 error ("deducing from brace-enclosed initializer list requires "
24682 "#include <initializer_list>");
24683 return error_mark_node;
24685 argvec = make_tree_vec (1);
24686 TREE_VEC_ELT (argvec, 0) = arg;
24687 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24688 NULL_TREE, 0, tf_warning_or_error);
24691 /* Replace auto in TYPE with std::initializer_list<auto>. */
24693 static tree
24694 listify_autos (tree type, tree auto_node)
24696 tree init_auto = listify (auto_node);
24697 tree argvec = make_tree_vec (1);
24698 TREE_VEC_ELT (argvec, 0) = init_auto;
24699 if (processing_template_decl)
24700 argvec = add_to_template_args (current_template_args (), argvec);
24701 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24704 /* Hash traits for hashing possibly constrained 'auto'
24705 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24707 struct auto_hash : default_hash_traits<tree>
24709 static inline hashval_t hash (tree);
24710 static inline bool equal (tree, tree);
24713 /* Hash the 'auto' T. */
24715 inline hashval_t
24716 auto_hash::hash (tree t)
24718 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24719 /* Matching constrained-type-specifiers denote the same template
24720 parameter, so hash the constraint. */
24721 return hash_placeholder_constraint (c);
24722 else
24723 /* But unconstrained autos are all separate, so just hash the pointer. */
24724 return iterative_hash_object (t, 0);
24727 /* Compare two 'auto's. */
24729 inline bool
24730 auto_hash::equal (tree t1, tree t2)
24732 if (t1 == t2)
24733 return true;
24735 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24736 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24738 /* Two unconstrained autos are distinct. */
24739 if (!c1 || !c2)
24740 return false;
24742 return equivalent_placeholder_constraints (c1, c2);
24745 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24746 constrained) auto, add it to the vector. */
24748 static int
24749 extract_autos_r (tree t, void *data)
24751 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24752 if (is_auto (t))
24754 /* All the autos were built with index 0; fix that up now. */
24755 tree *p = hash.find_slot (t, INSERT);
24756 unsigned idx;
24757 if (*p)
24758 /* If this is a repeated constrained-type-specifier, use the index we
24759 chose before. */
24760 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24761 else
24763 /* Otherwise this is new, so use the current count. */
24764 *p = t;
24765 idx = hash.elements () - 1;
24767 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24770 /* Always keep walking. */
24771 return 0;
24774 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24775 says they can appear anywhere in the type. */
24777 static tree
24778 extract_autos (tree type)
24780 hash_set<tree> visited;
24781 hash_table<auto_hash> hash (2);
24783 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24785 tree tree_vec = make_tree_vec (hash.elements());
24786 for (hash_table<auto_hash>::iterator iter = hash.begin();
24787 iter != hash.end(); ++iter)
24789 tree elt = *iter;
24790 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24791 TREE_VEC_ELT (tree_vec, i)
24792 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24795 return tree_vec;
24798 /* The stem for deduction guide names. */
24799 const char *const dguide_base = "__dguide_";
24801 /* Return the name for a deduction guide for class template TMPL. */
24803 tree
24804 dguide_name (tree tmpl)
24806 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24807 tree tname = TYPE_IDENTIFIER (type);
24808 char *buf = (char *) alloca (1 + strlen (dguide_base)
24809 + IDENTIFIER_LENGTH (tname));
24810 memcpy (buf, dguide_base, strlen (dguide_base));
24811 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24812 IDENTIFIER_LENGTH (tname) + 1);
24813 tree dname = get_identifier (buf);
24814 TREE_TYPE (dname) = type;
24815 return dname;
24818 /* True if NAME is the name of a deduction guide. */
24820 bool
24821 dguide_name_p (tree name)
24823 return (TREE_TYPE (name)
24824 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24825 strlen (dguide_base)));
24828 /* True if FN is a deduction guide. */
24830 bool
24831 deduction_guide_p (const_tree fn)
24833 if (DECL_P (fn))
24834 if (tree name = DECL_NAME (fn))
24835 return dguide_name_p (name);
24836 return false;
24839 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
24841 bool
24842 copy_guide_p (const_tree fn)
24844 gcc_assert (deduction_guide_p (fn));
24845 if (!DECL_ARTIFICIAL (fn))
24846 return false;
24847 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
24848 return (TREE_CHAIN (parms) == void_list_node
24849 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
24852 /* True if FN is a guide generated from a constructor template. */
24854 bool
24855 template_guide_p (const_tree fn)
24857 gcc_assert (deduction_guide_p (fn));
24858 if (!DECL_ARTIFICIAL (fn))
24859 return false;
24860 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
24862 tree tmpl = DECL_TI_TEMPLATE (ctor);
24863 return PRIMARY_TEMPLATE_P (tmpl);
24865 return false;
24868 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24869 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24870 template parameter types. Note that the handling of template template
24871 parameters relies on current_template_parms being set appropriately for the
24872 new template. */
24874 static tree
24875 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
24876 tree tsubst_args, tsubst_flags_t complain)
24878 tree oldidx = get_template_parm_index (olddecl);
24880 tree newtype;
24881 if (TREE_CODE (olddecl) == TYPE_DECL
24882 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24884 tree oldtype = TREE_TYPE (olddecl);
24885 newtype = cxx_make_type (TREE_CODE (oldtype));
24886 TYPE_MAIN_VARIANT (newtype) = newtype;
24887 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
24888 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
24889 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
24891 else
24892 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
24893 complain, NULL_TREE);
24895 tree newdecl
24896 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
24897 DECL_NAME (olddecl), newtype);
24898 SET_DECL_TEMPLATE_PARM_P (newdecl);
24900 tree newidx;
24901 if (TREE_CODE (olddecl) == TYPE_DECL
24902 || TREE_CODE (olddecl) == TEMPLATE_DECL)
24904 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
24905 = build_template_parm_index (index, level, level,
24906 newdecl, newtype);
24907 TEMPLATE_PARM_PARAMETER_PACK (newidx)
24908 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
24909 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
24910 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
24912 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
24914 DECL_TEMPLATE_RESULT (newdecl)
24915 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
24916 DECL_NAME (olddecl), newtype);
24917 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
24918 // First create a copy (ttargs) of tsubst_args with an
24919 // additional level for the template template parameter's own
24920 // template parameters (ttparms).
24921 tree ttparms = (INNERMOST_TEMPLATE_PARMS
24922 (DECL_TEMPLATE_PARMS (olddecl)));
24923 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
24924 tree ttargs = make_tree_vec (depth + 1);
24925 for (int i = 0; i < depth; ++i)
24926 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
24927 TREE_VEC_ELT (ttargs, depth)
24928 = template_parms_level_to_args (ttparms);
24929 // Substitute ttargs into ttparms to fix references to
24930 // other template parameters.
24931 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24932 complain);
24933 // Now substitute again with args based on tparms, to reduce
24934 // the level of the ttparms.
24935 ttargs = current_template_args ();
24936 ttparms = tsubst_template_parms_level (ttparms, ttargs,
24937 complain);
24938 // Finally, tack the adjusted parms onto tparms.
24939 ttparms = tree_cons (size_int (depth), ttparms,
24940 current_template_parms);
24941 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
24944 else
24946 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
24947 tree newconst
24948 = build_decl (DECL_SOURCE_LOCATION (oldconst),
24949 TREE_CODE (oldconst),
24950 DECL_NAME (oldconst), newtype);
24951 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
24952 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
24953 SET_DECL_TEMPLATE_PARM_P (newconst);
24954 newidx = build_template_parm_index (index, level, level,
24955 newconst, newtype);
24956 TEMPLATE_PARM_PARAMETER_PACK (newidx)
24957 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
24958 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
24961 return newdecl;
24964 /* Returns a C++17 class deduction guide template based on the constructor
24965 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
24966 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
24968 static tree
24969 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
24971 tree type, tparms, targs, fparms, fargs, ci;
24972 bool memtmpl = false;
24973 bool explicit_p;
24974 location_t loc;
24976 if (TYPE_P (ctor))
24978 type = ctor;
24979 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
24980 if (copy_p)
24982 type = TREE_TYPE (type);
24983 fparms = tree_cons (NULL_TREE, type, void_list_node);
24985 else
24986 fparms = void_list_node;
24988 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
24989 tparms = DECL_TEMPLATE_PARMS (ctmpl);
24990 targs = CLASSTYPE_TI_ARGS (type);
24991 ci = NULL_TREE;
24992 fargs = NULL_TREE;
24993 loc = DECL_SOURCE_LOCATION (ctmpl);
24994 explicit_p = false;
24996 else
24998 if (outer_args)
24999 ctor = tsubst (ctor, outer_args, complain, ctor);
25000 type = DECL_CONTEXT (ctor);
25001 tree fn_tmpl;
25002 if (TREE_CODE (ctor) == TEMPLATE_DECL)
25004 fn_tmpl = ctor;
25005 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25007 else
25008 fn_tmpl = DECL_TI_TEMPLATE (ctor);
25010 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25011 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25012 fully specialized args for the enclosing class. Strip those off, as
25013 the deduction guide won't have those template parameters. */
25014 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25015 TMPL_PARMS_DEPTH (tparms));
25016 /* Discard the 'this' parameter. */
25017 fparms = FUNCTION_ARG_CHAIN (ctor);
25018 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25019 ci = get_constraints (ctor);
25020 loc = DECL_SOURCE_LOCATION (ctor);
25021 explicit_p = DECL_NONCONVERTING_P (ctor);
25023 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25025 memtmpl = true;
25027 /* For a member template constructor, we need to flatten the two
25028 template parameter lists into one, and then adjust the function
25029 signature accordingly. This gets...complicated. */
25030 ++processing_template_decl;
25031 tree save_parms = current_template_parms;
25033 /* For a member template we should have two levels of parms/args, one
25034 for the class and one for the constructor. We stripped
25035 specialized args for further enclosing classes above. */
25036 const int depth = 2;
25037 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25039 /* Template args for translating references to the two-level template
25040 parameters into references to the one-level template parameters we
25041 are creating. */
25042 tree tsubst_args = copy_node (targs);
25043 TMPL_ARGS_LEVEL (tsubst_args, depth)
25044 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25046 /* Template parms for the constructor template. */
25047 tree ftparms = TREE_VALUE (tparms);
25048 unsigned flen = TREE_VEC_LENGTH (ftparms);
25049 /* Template parms for the class template. */
25050 tparms = TREE_CHAIN (tparms);
25051 tree ctparms = TREE_VALUE (tparms);
25052 unsigned clen = TREE_VEC_LENGTH (ctparms);
25053 /* Template parms for the deduction guide start as a copy of the
25054 template parms for the class. We set current_template_parms for
25055 lookup_template_class_1. */
25056 current_template_parms = tparms = copy_node (tparms);
25057 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25058 for (unsigned i = 0; i < clen; ++i)
25059 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25061 /* Now we need to rewrite the constructor parms to append them to the
25062 class parms. */
25063 for (unsigned i = 0; i < flen; ++i)
25065 unsigned index = i + clen;
25066 unsigned level = 1;
25067 tree oldelt = TREE_VEC_ELT (ftparms, i);
25068 tree olddecl = TREE_VALUE (oldelt);
25069 tree newdecl = rewrite_template_parm (olddecl, index, level,
25070 tsubst_args, complain);
25071 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25072 tsubst_args, complain, ctor);
25073 tree list = build_tree_list (newdef, newdecl);
25074 TEMPLATE_PARM_CONSTRAINTS (list)
25075 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25076 tsubst_args, complain, ctor);
25077 TREE_VEC_ELT (new_vec, index) = list;
25078 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25081 /* Now we have a final set of template parms to substitute into the
25082 function signature. */
25083 targs = template_parms_to_args (tparms);
25084 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25085 complain, ctor);
25086 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25087 if (ci)
25088 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25090 current_template_parms = save_parms;
25091 --processing_template_decl;
25095 if (!memtmpl)
25097 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25098 tparms = copy_node (tparms);
25099 INNERMOST_TEMPLATE_PARMS (tparms)
25100 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25103 tree fntype = build_function_type (type, fparms);
25104 tree ded_fn = build_lang_decl_loc (loc,
25105 FUNCTION_DECL,
25106 dguide_name (type), fntype);
25107 DECL_ARGUMENTS (ded_fn) = fargs;
25108 DECL_ARTIFICIAL (ded_fn) = true;
25109 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25110 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25111 DECL_ARTIFICIAL (ded_tmpl) = true;
25112 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25113 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25114 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25115 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25116 if (DECL_P (ctor))
25117 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25118 if (ci)
25119 set_constraints (ded_tmpl, ci);
25121 return ded_tmpl;
25124 /* Deduce template arguments for the class template placeholder PTYPE for
25125 template TMPL based on the initializer INIT, and return the resulting
25126 type. */
25128 static tree
25129 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25130 tsubst_flags_t complain)
25132 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25134 /* We should have handled this in the caller. */
25135 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25136 return ptype;
25137 if (complain & tf_error)
25138 error ("non-class template %qT used without template arguments", tmpl);
25139 return error_mark_node;
25142 tree type = TREE_TYPE (tmpl);
25144 vec<tree,va_gc> *args;
25145 if (init == NULL_TREE
25146 || TREE_CODE (init) == TREE_LIST)
25147 args = make_tree_vector_from_list (init);
25148 else if (BRACE_ENCLOSED_INITIALIZER_P (init)
25149 && !TYPE_HAS_LIST_CTOR (type)
25150 && !is_std_init_list (type))
25151 args = make_tree_vector_from_ctor (init);
25152 else
25153 args = make_tree_vector_single (init);
25155 tree dname = dguide_name (tmpl);
25156 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25157 /*type*/false, /*complain*/false,
25158 /*hidden*/false);
25159 bool elided = false;
25160 if (cands == error_mark_node)
25161 cands = NULL_TREE;
25163 /* Prune explicit deduction guides in copy-initialization context. */
25164 if (flags & LOOKUP_ONLYCONVERTING)
25166 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25167 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25168 elided = true;
25170 if (elided)
25172 /* Found a nonconverting guide, prune the candidates. */
25173 tree pruned = NULL_TREE;
25174 for (lkp_iterator iter (cands); iter; ++iter)
25175 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25176 pruned = lookup_add (*iter, pruned);
25178 cands = pruned;
25182 tree outer_args = NULL_TREE;
25183 if (DECL_CLASS_SCOPE_P (tmpl)
25184 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25186 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25187 type = TREE_TYPE (most_general_template (tmpl));
25190 bool saw_ctor = false;
25191 if (CLASSTYPE_METHOD_VEC (type))
25192 // FIXME cache artificial deduction guides
25193 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25194 iter; ++iter)
25196 tree guide = build_deduction_guide (*iter, outer_args, complain);
25197 if ((flags & LOOKUP_ONLYCONVERTING)
25198 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25199 elided = true;
25200 else
25201 cands = lookup_add (guide, cands);
25203 saw_ctor = true;
25206 if (args->length () < 2)
25208 tree gtype = NULL_TREE;
25210 if (args->length () == 1)
25211 gtype = build_reference_type (type);
25212 else if (!saw_ctor)
25213 gtype = type;
25215 if (gtype)
25217 tree guide = build_deduction_guide (gtype, outer_args, complain);
25218 if ((flags & LOOKUP_ONLYCONVERTING)
25219 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25220 elided = true;
25221 else
25222 cands = lookup_add (guide, cands);
25226 if (elided && !cands)
25228 error ("cannot deduce template arguments for copy-initialization"
25229 " of %qT, as it has no non-explicit deduction guides or "
25230 "user-declared constructors", type);
25231 return error_mark_node;
25234 ++cp_unevaluated_operand;
25235 tree t = build_new_function_call (cands, &args, tf_decltype);
25237 if (t == error_mark_node && (complain & tf_warning_or_error))
25239 error ("class template argument deduction failed:");
25240 t = build_new_function_call (cands, &args, complain | tf_decltype);
25241 if (elided)
25242 inform (input_location, "explicit deduction guides not considered "
25243 "for copy-initialization");
25246 --cp_unevaluated_operand;
25247 release_tree_vector (args);
25249 return TREE_TYPE (t);
25252 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25253 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25255 tree
25256 do_auto_deduction (tree type, tree init, tree auto_node)
25258 return do_auto_deduction (type, init, auto_node,
25259 tf_warning_or_error,
25260 adc_unspecified);
25263 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25264 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25265 The CONTEXT determines the context in which auto deduction is performed
25266 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25267 OUTER_TARGS are used during template argument deduction
25268 (context == adc_unify) to properly substitute the result, and is ignored
25269 in other contexts.
25271 For partial-concept-ids, extra args may be appended to the list of deduced
25272 template arguments prior to determining constraint satisfaction. */
25274 tree
25275 do_auto_deduction (tree type, tree init, tree auto_node,
25276 tsubst_flags_t complain, auto_deduction_context context,
25277 tree outer_targs, int flags)
25279 tree targs;
25281 if (init == error_mark_node)
25282 return error_mark_node;
25284 if (init && type_dependent_expression_p (init)
25285 && context != adc_unify)
25286 /* Defining a subset of type-dependent expressions that we can deduce
25287 from ahead of time isn't worth the trouble. */
25288 return type;
25290 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25291 /* C++17 class template argument deduction. */
25292 return do_class_deduction (type, tmpl, init, flags, complain);
25294 if (TREE_TYPE (init) == NULL_TREE)
25295 /* Nothing we can do with this, even in deduction context. */
25296 return type;
25298 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25299 with either a new invented type template parameter U or, if the
25300 initializer is a braced-init-list (8.5.4), with
25301 std::initializer_list<U>. */
25302 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25304 if (!DIRECT_LIST_INIT_P (init))
25305 type = listify_autos (type, auto_node);
25306 else if (CONSTRUCTOR_NELTS (init) == 1)
25307 init = CONSTRUCTOR_ELT (init, 0)->value;
25308 else
25310 if (complain & tf_warning_or_error)
25312 if (permerror (input_location, "direct-list-initialization of "
25313 "%<auto%> requires exactly one element"))
25314 inform (input_location,
25315 "for deduction to %<std::initializer_list%>, use copy-"
25316 "list-initialization (i.e. add %<=%> before the %<{%>)");
25318 type = listify_autos (type, auto_node);
25322 if (type == error_mark_node)
25323 return error_mark_node;
25325 init = resolve_nondeduced_context (init, complain);
25327 if (context == adc_decomp_type
25328 && auto_node == type
25329 && init != error_mark_node
25330 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25331 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25332 and initializer has array type, deduce cv-qualified array type. */
25333 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25334 complain);
25335 else if (AUTO_IS_DECLTYPE (auto_node))
25337 bool id = (DECL_P (init)
25338 || ((TREE_CODE (init) == COMPONENT_REF
25339 || TREE_CODE (init) == SCOPE_REF)
25340 && !REF_PARENTHESIZED_P (init)));
25341 targs = make_tree_vec (1);
25342 TREE_VEC_ELT (targs, 0)
25343 = finish_decltype_type (init, id, tf_warning_or_error);
25344 if (type != auto_node)
25346 if (complain & tf_error)
25347 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25348 return error_mark_node;
25351 else
25353 tree parms = build_tree_list (NULL_TREE, type);
25354 tree tparms;
25356 if (flag_concepts)
25357 tparms = extract_autos (type);
25358 else
25360 tparms = make_tree_vec (1);
25361 TREE_VEC_ELT (tparms, 0)
25362 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25365 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25366 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25367 DEDUCE_CALL, LOOKUP_NORMAL,
25368 NULL, /*explain_p=*/false);
25369 if (val > 0)
25371 if (processing_template_decl)
25372 /* Try again at instantiation time. */
25373 return type;
25374 if (type && type != error_mark_node
25375 && (complain & tf_error))
25376 /* If type is error_mark_node a diagnostic must have been
25377 emitted by now. Also, having a mention to '<type error>'
25378 in the diagnostic is not really useful to the user. */
25380 if (cfun && auto_node == current_function_auto_return_pattern
25381 && LAMBDA_FUNCTION_P (current_function_decl))
25382 error ("unable to deduce lambda return type from %qE", init);
25383 else
25384 error ("unable to deduce %qT from %qE", type, init);
25385 type_unification_real (tparms, targs, parms, &init, 1, 0,
25386 DEDUCE_CALL, LOOKUP_NORMAL,
25387 NULL, /*explain_p=*/true);
25389 return error_mark_node;
25393 /* Check any placeholder constraints against the deduced type. */
25394 if (flag_concepts && !processing_template_decl)
25395 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25397 /* Use the deduced type to check the associated constraints. If we
25398 have a partial-concept-id, rebuild the argument list so that
25399 we check using the extra arguments. */
25400 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25401 tree cargs = CHECK_CONSTR_ARGS (constr);
25402 if (TREE_VEC_LENGTH (cargs) > 1)
25404 cargs = copy_node (cargs);
25405 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25407 else
25408 cargs = targs;
25409 if (!constraints_satisfied_p (constr, cargs))
25411 if (complain & tf_warning_or_error)
25413 switch (context)
25415 case adc_unspecified:
25416 case adc_unify:
25417 error("placeholder constraints not satisfied");
25418 break;
25419 case adc_variable_type:
25420 case adc_decomp_type:
25421 error ("deduced initializer does not satisfy "
25422 "placeholder constraints");
25423 break;
25424 case adc_return_type:
25425 error ("deduced return type does not satisfy "
25426 "placeholder constraints");
25427 break;
25428 case adc_requirement:
25429 error ("deduced expression type does not satisfy "
25430 "placeholder constraints");
25431 break;
25433 diagnose_constraints (input_location, constr, targs);
25435 return error_mark_node;
25439 if (processing_template_decl && context != adc_unify)
25440 outer_targs = current_template_args ();
25441 targs = add_to_template_args (outer_targs, targs);
25442 return tsubst (type, targs, complain, NULL_TREE);
25445 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25446 result. */
25448 tree
25449 splice_late_return_type (tree type, tree late_return_type)
25451 if (is_auto (type))
25453 if (late_return_type)
25454 return late_return_type;
25456 tree idx = get_template_parm_index (type);
25457 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25458 /* In an abbreviated function template we didn't know we were dealing
25459 with a function template when we saw the auto return type, so update
25460 it to have the correct level. */
25461 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25463 return type;
25466 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25467 'decltype(auto)' or a deduced class template. */
25469 bool
25470 is_auto (const_tree type)
25472 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25473 && (TYPE_IDENTIFIER (type) == auto_identifier
25474 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25475 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25476 return true;
25477 else
25478 return false;
25481 /* for_each_template_parm callback for type_uses_auto. */
25484 is_auto_r (tree tp, void */*data*/)
25486 return is_auto (tp);
25489 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25490 a use of `auto'. Returns NULL_TREE otherwise. */
25492 tree
25493 type_uses_auto (tree type)
25495 if (type == NULL_TREE)
25496 return NULL_TREE;
25497 else if (flag_concepts)
25499 /* The Concepts TS allows multiple autos in one type-specifier; just
25500 return the first one we find, do_auto_deduction will collect all of
25501 them. */
25502 if (uses_template_parms (type))
25503 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25504 /*visited*/NULL, /*nondeduced*/true);
25505 else
25506 return NULL_TREE;
25508 else
25509 return find_type_usage (type, is_auto);
25512 /* For a given template T, return the vector of typedefs referenced
25513 in T for which access check is needed at T instantiation time.
25514 T is either a FUNCTION_DECL or a RECORD_TYPE.
25515 Those typedefs were added to T by the function
25516 append_type_to_template_for_access_check. */
25518 vec<qualified_typedef_usage_t, va_gc> *
25519 get_types_needing_access_check (tree t)
25521 tree ti;
25522 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25524 if (!t || t == error_mark_node)
25525 return NULL;
25527 if (!(ti = get_template_info (t)))
25528 return NULL;
25530 if (CLASS_TYPE_P (t)
25531 || TREE_CODE (t) == FUNCTION_DECL)
25533 if (!TI_TEMPLATE (ti))
25534 return NULL;
25536 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25539 return result;
25542 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25543 tied to T. That list of typedefs will be access checked at
25544 T instantiation time.
25545 T is either a FUNCTION_DECL or a RECORD_TYPE.
25546 TYPE_DECL is a TYPE_DECL node representing a typedef.
25547 SCOPE is the scope through which TYPE_DECL is accessed.
25548 LOCATION is the location of the usage point of TYPE_DECL.
25550 This function is a subroutine of
25551 append_type_to_template_for_access_check. */
25553 static void
25554 append_type_to_template_for_access_check_1 (tree t,
25555 tree type_decl,
25556 tree scope,
25557 location_t location)
25559 qualified_typedef_usage_t typedef_usage;
25560 tree ti;
25562 if (!t || t == error_mark_node)
25563 return;
25565 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25566 || CLASS_TYPE_P (t))
25567 && type_decl
25568 && TREE_CODE (type_decl) == TYPE_DECL
25569 && scope);
25571 if (!(ti = get_template_info (t)))
25572 return;
25574 gcc_assert (TI_TEMPLATE (ti));
25576 typedef_usage.typedef_decl = type_decl;
25577 typedef_usage.context = scope;
25578 typedef_usage.locus = location;
25580 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25583 /* Append TYPE_DECL to the template TEMPL.
25584 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25585 At TEMPL instanciation time, TYPE_DECL will be checked to see
25586 if it can be accessed through SCOPE.
25587 LOCATION is the location of the usage point of TYPE_DECL.
25589 e.g. consider the following code snippet:
25591 class C
25593 typedef int myint;
25596 template<class U> struct S
25598 C::myint mi; // <-- usage point of the typedef C::myint
25601 S<char> s;
25603 At S<char> instantiation time, we need to check the access of C::myint
25604 In other words, we need to check the access of the myint typedef through
25605 the C scope. For that purpose, this function will add the myint typedef
25606 and the scope C through which its being accessed to a list of typedefs
25607 tied to the template S. That list will be walked at template instantiation
25608 time and access check performed on each typedefs it contains.
25609 Note that this particular code snippet should yield an error because
25610 myint is private to C. */
25612 void
25613 append_type_to_template_for_access_check (tree templ,
25614 tree type_decl,
25615 tree scope,
25616 location_t location)
25618 qualified_typedef_usage_t *iter;
25619 unsigned i;
25621 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25623 /* Make sure we don't append the type to the template twice. */
25624 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25625 if (iter->typedef_decl == type_decl && scope == iter->context)
25626 return;
25628 append_type_to_template_for_access_check_1 (templ, type_decl,
25629 scope, location);
25632 /* Convert the generic type parameters in PARM that match the types given in the
25633 range [START_IDX, END_IDX) from the current_template_parms into generic type
25634 packs. */
25636 tree
25637 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25639 tree current = current_template_parms;
25640 int depth = TMPL_PARMS_DEPTH (current);
25641 current = INNERMOST_TEMPLATE_PARMS (current);
25642 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25644 for (int i = 0; i < start_idx; ++i)
25645 TREE_VEC_ELT (replacement, i)
25646 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25648 for (int i = start_idx; i < end_idx; ++i)
25650 /* Create a distinct parameter pack type from the current parm and add it
25651 to the replacement args to tsubst below into the generic function
25652 parameter. */
25654 tree o = TREE_TYPE (TREE_VALUE
25655 (TREE_VEC_ELT (current, i)));
25656 tree t = copy_type (o);
25657 TEMPLATE_TYPE_PARM_INDEX (t)
25658 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25659 o, 0, 0, tf_none);
25660 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25661 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25662 TYPE_MAIN_VARIANT (t) = t;
25663 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25664 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25665 TREE_VEC_ELT (replacement, i) = t;
25666 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25669 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25670 TREE_VEC_ELT (replacement, i)
25671 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25673 /* If there are more levels then build up the replacement with the outer
25674 template parms. */
25675 if (depth > 1)
25676 replacement = add_to_template_args (template_parms_to_args
25677 (TREE_CHAIN (current_template_parms)),
25678 replacement);
25680 return tsubst (parm, replacement, tf_none, NULL_TREE);
25683 /* Entries in the decl_constraint hash table. */
25684 struct GTY((for_user)) constr_entry
25686 tree decl;
25687 tree ci;
25690 /* Hashing function and equality for constraint entries. */
25691 struct constr_hasher : ggc_ptr_hash<constr_entry>
25693 static hashval_t hash (constr_entry *e)
25695 return (hashval_t)DECL_UID (e->decl);
25698 static bool equal (constr_entry *e1, constr_entry *e2)
25700 return e1->decl == e2->decl;
25704 /* A mapping from declarations to constraint information. Note that
25705 both templates and their underlying declarations are mapped to the
25706 same constraint information.
25708 FIXME: This is defined in pt.c because garbage collection
25709 code is not being generated for constraint.cc. */
25711 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25713 /* Returns the template constraints of declaration T. If T is not
25714 constrained, return NULL_TREE. Note that T must be non-null. */
25716 tree
25717 get_constraints (tree t)
25719 if (!flag_concepts)
25720 return NULL_TREE;
25722 gcc_assert (DECL_P (t));
25723 if (TREE_CODE (t) == TEMPLATE_DECL)
25724 t = DECL_TEMPLATE_RESULT (t);
25725 constr_entry elt = { t, NULL_TREE };
25726 constr_entry* found = decl_constraints->find (&elt);
25727 if (found)
25728 return found->ci;
25729 else
25730 return NULL_TREE;
25733 /* Associate the given constraint information CI with the declaration
25734 T. If T is a template, then the constraints are associated with
25735 its underlying declaration. Don't build associations if CI is
25736 NULL_TREE. */
25738 void
25739 set_constraints (tree t, tree ci)
25741 if (!ci)
25742 return;
25743 gcc_assert (t && flag_concepts);
25744 if (TREE_CODE (t) == TEMPLATE_DECL)
25745 t = DECL_TEMPLATE_RESULT (t);
25746 gcc_assert (!get_constraints (t));
25747 constr_entry elt = {t, ci};
25748 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25749 constr_entry* entry = ggc_alloc<constr_entry> ();
25750 *entry = elt;
25751 *slot = entry;
25754 /* Remove the associated constraints of the declaration T. */
25756 void
25757 remove_constraints (tree t)
25759 gcc_assert (DECL_P (t));
25760 if (TREE_CODE (t) == TEMPLATE_DECL)
25761 t = DECL_TEMPLATE_RESULT (t);
25763 constr_entry elt = {t, NULL_TREE};
25764 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25765 if (slot)
25766 decl_constraints->clear_slot (slot);
25769 /* Memoized satisfaction results for declarations. This
25770 maps the pair (constraint_info, arguments) to the result computed
25771 by constraints_satisfied_p. */
25773 struct GTY((for_user)) constraint_sat_entry
25775 tree ci;
25776 tree args;
25777 tree result;
25780 /* Hashing function and equality for constraint entries. */
25782 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25784 static hashval_t hash (constraint_sat_entry *e)
25786 hashval_t val = iterative_hash_object(e->ci, 0);
25787 return iterative_hash_template_arg (e->args, val);
25790 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25792 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25796 /* Memoized satisfaction results for concept checks. */
25798 struct GTY((for_user)) concept_spec_entry
25800 tree tmpl;
25801 tree args;
25802 tree result;
25805 /* Hashing function and equality for constraint entries. */
25807 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25809 static hashval_t hash (concept_spec_entry *e)
25811 return hash_tmpl_and_args (e->tmpl, e->args);
25814 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25816 ++comparing_specializations;
25817 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25818 --comparing_specializations;
25819 return eq;
25823 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25824 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25826 /* Search for a memoized satisfaction result. Returns one of the
25827 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25829 tree
25830 lookup_constraint_satisfaction (tree ci, tree args)
25832 constraint_sat_entry elt = { ci, args, NULL_TREE };
25833 constraint_sat_entry* found = constraint_memos->find (&elt);
25834 if (found)
25835 return found->result;
25836 else
25837 return NULL_TREE;
25840 /* Memoize the result of a satisfication test. Returns the saved result. */
25842 tree
25843 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25845 constraint_sat_entry elt = {ci, args, result};
25846 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25847 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25848 *entry = elt;
25849 *slot = entry;
25850 return result;
25853 /* Search for a memoized satisfaction result for a concept. */
25855 tree
25856 lookup_concept_satisfaction (tree tmpl, tree args)
25858 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25859 concept_spec_entry* found = concept_memos->find (&elt);
25860 if (found)
25861 return found->result;
25862 else
25863 return NULL_TREE;
25866 /* Memoize the result of a concept check. Returns the saved result. */
25868 tree
25869 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
25871 concept_spec_entry elt = {tmpl, args, result};
25872 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
25873 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25874 *entry = elt;
25875 *slot = entry;
25876 return result;
25879 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
25881 /* Returns a prior concept specialization. This returns the substituted
25882 and normalized constraints defined by the concept. */
25884 tree
25885 get_concept_expansion (tree tmpl, tree args)
25887 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25888 concept_spec_entry* found = concept_expansions->find (&elt);
25889 if (found)
25890 return found->result;
25891 else
25892 return NULL_TREE;
25895 /* Save a concept expansion for later. */
25897 tree
25898 save_concept_expansion (tree tmpl, tree args, tree def)
25900 concept_spec_entry elt = {tmpl, args, def};
25901 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
25902 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
25903 *entry = elt;
25904 *slot = entry;
25905 return def;
25908 static hashval_t
25909 hash_subsumption_args (tree t1, tree t2)
25911 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
25912 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
25913 int val = 0;
25914 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
25915 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
25916 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
25917 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
25918 return val;
25921 /* Compare the constraints of two subsumption entries. The LEFT1 and
25922 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
25923 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
25925 static bool
25926 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
25928 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
25929 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
25930 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
25931 CHECK_CONSTR_ARGS (right1)))
25932 return comp_template_args (CHECK_CONSTR_ARGS (left2),
25933 CHECK_CONSTR_ARGS (right2));
25934 return false;
25937 /* Key/value pair for learning and memoizing subsumption results. This
25938 associates a pair of check constraints (including arguments) with
25939 a boolean value indicating the result. */
25941 struct GTY((for_user)) subsumption_entry
25943 tree t1;
25944 tree t2;
25945 bool result;
25948 /* Hashing function and equality for constraint entries. */
25950 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
25952 static hashval_t hash (subsumption_entry *e)
25954 return hash_subsumption_args (e->t1, e->t2);
25957 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
25959 ++comparing_specializations;
25960 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
25961 --comparing_specializations;
25962 return eq;
25966 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
25968 /* Search for a previously cached subsumption result. */
25970 bool*
25971 lookup_subsumption_result (tree t1, tree t2)
25973 subsumption_entry elt = { t1, t2, false };
25974 subsumption_entry* found = subsumption_table->find (&elt);
25975 if (found)
25976 return &found->result;
25977 else
25978 return 0;
25981 /* Save a subsumption result. */
25983 bool
25984 save_subsumption_result (tree t1, tree t2, bool result)
25986 subsumption_entry elt = {t1, t2, result};
25987 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
25988 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
25989 *entry = elt;
25990 *slot = entry;
25991 return result;
25994 /* Set up the hash table for constraint association. */
25996 void
25997 init_constraint_processing (void)
25999 if (!flag_concepts)
26000 return;
26002 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26003 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26004 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26005 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26006 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26009 /* Set up the hash tables for template instantiations. */
26011 void
26012 init_template_processing (void)
26014 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26015 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26018 /* Print stats about the template hash tables for -fstats. */
26020 void
26021 print_template_statistics (void)
26023 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26024 "%f collisions\n", (long) decl_specializations->size (),
26025 (long) decl_specializations->elements (),
26026 decl_specializations->collisions ());
26027 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26028 "%f collisions\n", (long) type_specializations->size (),
26029 (long) type_specializations->elements (),
26030 type_specializations->collisions ());
26033 #include "gt-cp-pt.h"