2017-09-30 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cp / pt.c
blobc29c779a14783b565bc357c3a8de46fa809215c0
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
45 /* The type of functions taking a tree, and some additional data, and
46 returning an int. */
47 typedef int (*tree_fn_t) (tree, void*);
49 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
50 instantiations have been deferred, either because their definitions
51 were not yet available, or because we were putting off doing the work. */
52 struct GTY ((chain_next ("%h.next"))) pending_template {
53 struct pending_template *next;
54 struct tinst_level *tinst;
57 static GTY(()) struct pending_template *pending_templates;
58 static GTY(()) struct pending_template *last_pending_template;
60 int processing_template_parmlist;
61 static int template_header_count;
63 static GTY(()) tree saved_trees;
64 static vec<int> inline_parm_levels;
66 static GTY(()) struct tinst_level *current_tinst_level;
68 static GTY(()) tree saved_access_scope;
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr;
75 // -------------------------------------------------------------------------- //
76 // Local Specialization Stack
78 // Implementation of the RAII helper for creating new local
79 // specializations.
80 local_specialization_stack::local_specialization_stack (lss_policy policy)
81 : saved (local_specializations)
83 if (policy == lss_blank || !saved)
84 local_specializations = new hash_map<tree, tree>;
85 else
86 local_specializations = new hash_map<tree, tree>(*saved);
89 local_specialization_stack::~local_specialization_stack ()
91 delete local_specializations;
92 local_specializations = saved;
95 /* True if we've recursed into fn_type_unification too many times. */
96 static bool excessive_deduction_depth;
98 struct GTY((for_user)) spec_entry
100 tree tmpl;
101 tree args;
102 tree spec;
105 struct spec_hasher : ggc_ptr_hash<spec_entry>
107 static hashval_t hash (spec_entry *);
108 static bool equal (spec_entry *, spec_entry *);
111 static GTY (()) hash_table<spec_hasher> *decl_specializations;
113 static GTY (()) hash_table<spec_hasher> *type_specializations;
115 /* Contains canonical template parameter types. The vector is indexed by
116 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
117 TREE_LIST, whose TREE_VALUEs contain the canonical template
118 parameters of various types and levels. */
119 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
121 #define UNIFY_ALLOW_NONE 0
122 #define UNIFY_ALLOW_MORE_CV_QUAL 1
123 #define UNIFY_ALLOW_LESS_CV_QUAL 2
124 #define UNIFY_ALLOW_DERIVED 4
125 #define UNIFY_ALLOW_INTEGER 8
126 #define UNIFY_ALLOW_OUTER_LEVEL 16
127 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
128 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
130 enum template_base_result {
131 tbr_incomplete_type,
132 tbr_ambiguous_baseclass,
133 tbr_success
136 static void push_access_scope (tree);
137 static void pop_access_scope (tree);
138 static bool resolve_overloaded_unification (tree, tree, tree, tree,
139 unification_kind_t, int,
140 bool);
141 static int try_one_overload (tree, tree, tree, tree, tree,
142 unification_kind_t, int, bool, bool);
143 static int unify (tree, tree, tree, tree, int, bool);
144 static void add_pending_template (tree);
145 static tree reopen_tinst_level (struct tinst_level *);
146 static tree tsubst_initializer_list (tree, tree);
147 static tree get_partial_spec_bindings (tree, tree, tree);
148 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
149 bool, bool);
150 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static void tsubst_enum (tree, tree, tree);
153 static tree add_to_template_args (tree, tree);
154 static tree add_outermost_template_args (tree, tree);
155 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
156 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
157 tree);
158 static int type_unification_real (tree, tree, tree, const tree *,
159 unsigned int, int, unification_kind_t, int,
160 vec<deferred_access_check, va_gc> **,
161 bool);
162 static void note_template_header (int);
163 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
164 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
165 static tree convert_template_argument (tree, tree, tree,
166 tsubst_flags_t, int, tree);
167 static tree for_each_template_parm (tree, tree_fn_t, void*,
168 hash_set<tree> *, bool, tree_fn_t = NULL);
169 static tree expand_template_argument_pack (tree);
170 static tree build_template_parm_index (int, int, int, tree, tree);
171 static bool inline_needs_template_parms (tree, bool);
172 static void push_inline_template_parms_recursive (tree, int);
173 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
174 static int mark_template_parm (tree, void *);
175 static int template_parm_this_level_p (tree, void *);
176 static tree tsubst_friend_function (tree, tree);
177 static tree tsubst_friend_class (tree, tree);
178 static int can_complete_type_without_circularity (tree);
179 static tree get_bindings (tree, tree, tree, bool);
180 static int template_decl_level (tree);
181 static int check_cv_quals_for_unify (int, tree, tree);
182 static void template_parm_level_and_index (tree, int*, int*);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
187 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
189 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
190 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
191 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
193 static bool check_specialization_scope (void);
194 static tree process_partial_specialization (tree);
195 static void set_current_access_from_decl (tree);
196 static enum template_base_result get_template_base (tree, tree, tree, tree,
197 bool , tree *);
198 static tree try_class_unification (tree, tree, tree, tree, bool);
199 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
200 tree, tree);
201 static bool template_template_parm_bindings_ok_p (tree, tree);
202 static void tsubst_default_arguments (tree, tsubst_flags_t);
203 static tree for_each_template_parm_r (tree *, int *, void *);
204 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
205 static void copy_default_args_to_explicit_spec (tree);
206 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
207 static bool dependent_template_arg_p (tree);
208 static bool any_template_arguments_need_structural_equality_p (tree);
209 static bool dependent_type_p_r (tree);
210 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
211 static tree tsubst_decl (tree, tree, tsubst_flags_t);
212 static void perform_typedefs_access_check (tree tmpl, tree targs);
213 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
214 location_t);
215 static tree listify (tree);
216 static tree listify_autos (tree, tree);
217 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
218 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
219 static bool complex_alias_template_p (const_tree tmpl);
220 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
221 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
222 static tree make_argument_pack (tree);
223 static void register_parameter_specializations (tree, tree);
225 /* Make the current scope suitable for access checking when we are
226 processing T. T can be FUNCTION_DECL for instantiated function
227 template, VAR_DECL for static member variable, or TYPE_DECL for
228 alias template (needed by instantiate_decl). */
230 static void
231 push_access_scope (tree t)
233 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
234 || TREE_CODE (t) == TYPE_DECL);
236 if (DECL_FRIEND_CONTEXT (t))
237 push_nested_class (DECL_FRIEND_CONTEXT (t));
238 else if (DECL_CLASS_SCOPE_P (t))
239 push_nested_class (DECL_CONTEXT (t));
240 else
241 push_to_top_level ();
243 if (TREE_CODE (t) == FUNCTION_DECL)
245 saved_access_scope = tree_cons
246 (NULL_TREE, current_function_decl, saved_access_scope);
247 current_function_decl = t;
251 /* Restore the scope set up by push_access_scope. T is the node we
252 are processing. */
254 static void
255 pop_access_scope (tree t)
257 if (TREE_CODE (t) == FUNCTION_DECL)
259 current_function_decl = TREE_VALUE (saved_access_scope);
260 saved_access_scope = TREE_CHAIN (saved_access_scope);
263 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
264 pop_nested_class ();
265 else
266 pop_from_top_level ();
269 /* Do any processing required when DECL (a member template
270 declaration) is finished. Returns the TEMPLATE_DECL corresponding
271 to DECL, unless it is a specialization, in which case the DECL
272 itself is returned. */
274 tree
275 finish_member_template_decl (tree decl)
277 if (decl == error_mark_node)
278 return error_mark_node;
280 gcc_assert (DECL_P (decl));
282 if (TREE_CODE (decl) == TYPE_DECL)
284 tree type;
286 type = TREE_TYPE (decl);
287 if (type == error_mark_node)
288 return error_mark_node;
289 if (MAYBE_CLASS_TYPE_P (type)
290 && CLASSTYPE_TEMPLATE_INFO (type)
291 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
293 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
294 check_member_template (tmpl);
295 return tmpl;
297 return NULL_TREE;
299 else if (TREE_CODE (decl) == FIELD_DECL)
300 error ("data member %qD cannot be a member template", decl);
301 else if (DECL_TEMPLATE_INFO (decl))
303 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
305 check_member_template (DECL_TI_TEMPLATE (decl));
306 return DECL_TI_TEMPLATE (decl);
308 else
309 return decl;
311 else
312 error ("invalid member template declaration %qD", decl);
314 return error_mark_node;
317 /* Create a template info node. */
319 tree
320 build_template_info (tree template_decl, tree template_args)
322 tree result = make_node (TEMPLATE_INFO);
323 TI_TEMPLATE (result) = template_decl;
324 TI_ARGS (result) = template_args;
325 return result;
328 /* Return the template info node corresponding to T, whatever T is. */
330 tree
331 get_template_info (const_tree t)
333 tree tinfo = NULL_TREE;
335 if (!t || t == error_mark_node)
336 return NULL;
338 if (TREE_CODE (t) == NAMESPACE_DECL
339 || TREE_CODE (t) == PARM_DECL)
340 return NULL;
342 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
343 tinfo = DECL_TEMPLATE_INFO (t);
345 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
346 t = TREE_TYPE (t);
348 if (OVERLOAD_TYPE_P (t))
349 tinfo = TYPE_TEMPLATE_INFO (t);
350 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
351 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
353 return tinfo;
356 /* Returns the template nesting level of the indicated class TYPE.
358 For example, in:
359 template <class T>
360 struct A
362 template <class U>
363 struct B {};
366 A<T>::B<U> has depth two, while A<T> has depth one.
367 Both A<T>::B<int> and A<int>::B<U> have depth one, if
368 they are instantiations, not specializations.
370 This function is guaranteed to return 0 if passed NULL_TREE so
371 that, for example, `template_class_depth (current_class_type)' is
372 always safe. */
375 template_class_depth (tree type)
377 int depth;
379 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
381 tree tinfo = get_template_info (type);
383 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
384 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
385 ++depth;
387 if (DECL_P (type))
388 type = CP_DECL_CONTEXT (type);
389 else if (LAMBDA_TYPE_P (type))
390 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
391 else
392 type = CP_TYPE_CONTEXT (type);
395 return depth;
398 /* Subroutine of maybe_begin_member_template_processing.
399 Returns true if processing DECL needs us to push template parms. */
401 static bool
402 inline_needs_template_parms (tree decl, bool nsdmi)
404 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
405 return false;
407 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
408 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
411 /* Subroutine of maybe_begin_member_template_processing.
412 Push the template parms in PARMS, starting from LEVELS steps into the
413 chain, and ending at the beginning, since template parms are listed
414 innermost first. */
416 static void
417 push_inline_template_parms_recursive (tree parmlist, int levels)
419 tree parms = TREE_VALUE (parmlist);
420 int i;
422 if (levels > 1)
423 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
425 ++processing_template_decl;
426 current_template_parms
427 = tree_cons (size_int (processing_template_decl),
428 parms, current_template_parms);
429 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
431 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
432 NULL);
433 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
435 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
437 if (error_operand_p (parm))
438 continue;
440 gcc_assert (DECL_P (parm));
442 switch (TREE_CODE (parm))
444 case TYPE_DECL:
445 case TEMPLATE_DECL:
446 pushdecl (parm);
447 break;
449 case PARM_DECL:
450 /* Push the CONST_DECL. */
451 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
452 break;
454 default:
455 gcc_unreachable ();
460 /* Restore the template parameter context for a member template, a
461 friend template defined in a class definition, or a non-template
462 member of template class. */
464 void
465 maybe_begin_member_template_processing (tree decl)
467 tree parms;
468 int levels = 0;
469 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
471 if (nsdmi)
473 tree ctx = DECL_CONTEXT (decl);
474 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
475 /* Disregard full specializations (c++/60999). */
476 && uses_template_parms (ctx)
477 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
480 if (inline_needs_template_parms (decl, nsdmi))
482 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
483 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
485 if (DECL_TEMPLATE_SPECIALIZATION (decl))
487 --levels;
488 parms = TREE_CHAIN (parms);
491 push_inline_template_parms_recursive (parms, levels);
494 /* Remember how many levels of template parameters we pushed so that
495 we can pop them later. */
496 inline_parm_levels.safe_push (levels);
499 /* Undo the effects of maybe_begin_member_template_processing. */
501 void
502 maybe_end_member_template_processing (void)
504 int i;
505 int last;
507 if (inline_parm_levels.length () == 0)
508 return;
510 last = inline_parm_levels.pop ();
511 for (i = 0; i < last; ++i)
513 --processing_template_decl;
514 current_template_parms = TREE_CHAIN (current_template_parms);
515 poplevel (0, 0, 0);
519 /* Return a new template argument vector which contains all of ARGS,
520 but has as its innermost set of arguments the EXTRA_ARGS. */
522 static tree
523 add_to_template_args (tree args, tree extra_args)
525 tree new_args;
526 int extra_depth;
527 int i;
528 int j;
530 if (args == NULL_TREE || extra_args == error_mark_node)
531 return extra_args;
533 extra_depth = TMPL_ARGS_DEPTH (extra_args);
534 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
536 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
537 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
539 for (j = 1; j <= extra_depth; ++j, ++i)
540 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
542 return new_args;
545 /* Like add_to_template_args, but only the outermost ARGS are added to
546 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
547 (EXTRA_ARGS) levels are added. This function is used to combine
548 the template arguments from a partial instantiation with the
549 template arguments used to attain the full instantiation from the
550 partial instantiation. */
552 static tree
553 add_outermost_template_args (tree args, tree extra_args)
555 tree new_args;
557 /* If there are more levels of EXTRA_ARGS than there are ARGS,
558 something very fishy is going on. */
559 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
561 /* If *all* the new arguments will be the EXTRA_ARGS, just return
562 them. */
563 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
564 return extra_args;
566 /* For the moment, we make ARGS look like it contains fewer levels. */
567 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
569 new_args = add_to_template_args (args, extra_args);
571 /* Now, we restore ARGS to its full dimensions. */
572 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
574 return new_args;
577 /* Return the N levels of innermost template arguments from the ARGS. */
579 tree
580 get_innermost_template_args (tree args, int n)
582 tree new_args;
583 int extra_levels;
584 int i;
586 gcc_assert (n >= 0);
588 /* If N is 1, just return the innermost set of template arguments. */
589 if (n == 1)
590 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
592 /* If we're not removing anything, just return the arguments we were
593 given. */
594 extra_levels = TMPL_ARGS_DEPTH (args) - n;
595 gcc_assert (extra_levels >= 0);
596 if (extra_levels == 0)
597 return args;
599 /* Make a new set of arguments, not containing the outer arguments. */
600 new_args = make_tree_vec (n);
601 for (i = 1; i <= n; ++i)
602 SET_TMPL_ARGS_LEVEL (new_args, i,
603 TMPL_ARGS_LEVEL (args, i + extra_levels));
605 return new_args;
608 /* The inverse of get_innermost_template_args: Return all but the innermost
609 EXTRA_LEVELS levels of template arguments from the ARGS. */
611 static tree
612 strip_innermost_template_args (tree args, int extra_levels)
614 tree new_args;
615 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
616 int i;
618 gcc_assert (n >= 0);
620 /* If N is 1, just return the outermost set of template arguments. */
621 if (n == 1)
622 return TMPL_ARGS_LEVEL (args, 1);
624 /* If we're not removing anything, just return the arguments we were
625 given. */
626 gcc_assert (extra_levels >= 0);
627 if (extra_levels == 0)
628 return args;
630 /* Make a new set of arguments, not containing the inner arguments. */
631 new_args = make_tree_vec (n);
632 for (i = 1; i <= n; ++i)
633 SET_TMPL_ARGS_LEVEL (new_args, i,
634 TMPL_ARGS_LEVEL (args, i));
636 return new_args;
639 /* We've got a template header coming up; push to a new level for storing
640 the parms. */
642 void
643 begin_template_parm_list (void)
645 /* We use a non-tag-transparent scope here, which causes pushtag to
646 put tags in this scope, rather than in the enclosing class or
647 namespace scope. This is the right thing, since we want
648 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
649 global template class, push_template_decl handles putting the
650 TEMPLATE_DECL into top-level scope. For a nested template class,
651 e.g.:
653 template <class T> struct S1 {
654 template <class T> struct S2 {};
657 pushtag contains special code to insert the TEMPLATE_DECL for S2
658 at the right scope. */
659 begin_scope (sk_template_parms, NULL);
660 ++processing_template_decl;
661 ++processing_template_parmlist;
662 note_template_header (0);
664 /* Add a dummy parameter level while we process the parameter list. */
665 current_template_parms
666 = tree_cons (size_int (processing_template_decl),
667 make_tree_vec (0),
668 current_template_parms);
671 /* This routine is called when a specialization is declared. If it is
672 invalid to declare a specialization here, an error is reported and
673 false is returned, otherwise this routine will return true. */
675 static bool
676 check_specialization_scope (void)
678 tree scope = current_scope ();
680 /* [temp.expl.spec]
682 An explicit specialization shall be declared in the namespace of
683 which the template is a member, or, for member templates, in the
684 namespace of which the enclosing class or enclosing class
685 template is a member. An explicit specialization of a member
686 function, member class or static data member of a class template
687 shall be declared in the namespace of which the class template
688 is a member. */
689 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
691 error ("explicit specialization in non-namespace scope %qD", scope);
692 return false;
695 /* [temp.expl.spec]
697 In an explicit specialization declaration for a member of a class
698 template or a member template that appears in namespace scope,
699 the member template and some of its enclosing class templates may
700 remain unspecialized, except that the declaration shall not
701 explicitly specialize a class member template if its enclosing
702 class templates are not explicitly specialized as well. */
703 if (current_template_parms)
705 error ("enclosing class templates are not explicitly specialized");
706 return false;
709 return true;
712 /* We've just seen template <>. */
714 bool
715 begin_specialization (void)
717 begin_scope (sk_template_spec, NULL);
718 note_template_header (1);
719 return check_specialization_scope ();
722 /* Called at then end of processing a declaration preceded by
723 template<>. */
725 void
726 end_specialization (void)
728 finish_scope ();
729 reset_specialization ();
732 /* Any template <>'s that we have seen thus far are not referring to a
733 function specialization. */
735 void
736 reset_specialization (void)
738 processing_specialization = 0;
739 template_header_count = 0;
742 /* We've just seen a template header. If SPECIALIZATION is nonzero,
743 it was of the form template <>. */
745 static void
746 note_template_header (int specialization)
748 processing_specialization = specialization;
749 template_header_count++;
752 /* We're beginning an explicit instantiation. */
754 void
755 begin_explicit_instantiation (void)
757 gcc_assert (!processing_explicit_instantiation);
758 processing_explicit_instantiation = true;
762 void
763 end_explicit_instantiation (void)
765 gcc_assert (processing_explicit_instantiation);
766 processing_explicit_instantiation = false;
769 /* An explicit specialization or partial specialization of TMPL is being
770 declared. Check that the namespace in which the specialization is
771 occurring is permissible. Returns false iff it is invalid to
772 specialize TMPL in the current namespace. */
774 static bool
775 check_specialization_namespace (tree tmpl)
777 tree tpl_ns = decl_namespace_context (tmpl);
779 /* [tmpl.expl.spec]
781 An explicit specialization shall be declared in a namespace enclosing the
782 specialized template. An explicit specialization whose declarator-id is
783 not qualified shall be declared in the nearest enclosing namespace of the
784 template, or, if the namespace is inline (7.3.1), any namespace from its
785 enclosing namespace set. */
786 if (current_scope() != DECL_CONTEXT (tmpl)
787 && !at_namespace_scope_p ())
789 error ("specialization of %qD must appear at namespace scope", tmpl);
790 return false;
793 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
794 /* Same or enclosing namespace. */
795 return true;
796 else
798 permerror (input_location,
799 "specialization of %qD in different namespace", tmpl);
800 inform (DECL_SOURCE_LOCATION (tmpl),
801 " from definition of %q#D", tmpl);
802 return false;
806 /* SPEC is an explicit instantiation. Check that it is valid to
807 perform this explicit instantiation in the current namespace. */
809 static void
810 check_explicit_instantiation_namespace (tree spec)
812 tree ns;
814 /* DR 275: An explicit instantiation shall appear in an enclosing
815 namespace of its template. */
816 ns = decl_namespace_context (spec);
817 if (!is_nested_namespace (current_namespace, ns))
818 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
819 "(which does not enclose namespace %qD)",
820 spec, current_namespace, ns);
823 // Returns the type of a template specialization only if that
824 // specialization needs to be defined. Otherwise (e.g., if the type has
825 // already been defined), the function returns NULL_TREE.
826 static tree
827 maybe_new_partial_specialization (tree type)
829 // An implicit instantiation of an incomplete type implies
830 // the definition of a new class template.
832 // template<typename T>
833 // struct S;
835 // template<typename T>
836 // struct S<T*>;
838 // Here, S<T*> is an implicit instantiation of S whose type
839 // is incomplete.
840 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
841 return type;
843 // It can also be the case that TYPE is a completed specialization.
844 // Continuing the previous example, suppose we also declare:
846 // template<typename T>
847 // requires Integral<T>
848 // struct S<T*>;
850 // Here, S<T*> refers to the specialization S<T*> defined
851 // above. However, we need to differentiate definitions because
852 // we intend to define a new partial specialization. In this case,
853 // we rely on the fact that the constraints are different for
854 // this declaration than that above.
856 // Note that we also get here for injected class names and
857 // late-parsed template definitions. We must ensure that we
858 // do not create new type declarations for those cases.
859 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
861 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
862 tree args = CLASSTYPE_TI_ARGS (type);
864 // If there are no template parameters, this cannot be a new
865 // partial template specializtion?
866 if (!current_template_parms)
867 return NULL_TREE;
869 // The injected-class-name is not a new partial specialization.
870 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
871 return NULL_TREE;
873 // If the constraints are not the same as those of the primary
874 // then, we can probably create a new specialization.
875 tree type_constr = current_template_constraints ();
877 if (type == TREE_TYPE (tmpl))
879 tree main_constr = get_constraints (tmpl);
880 if (equivalent_constraints (type_constr, main_constr))
881 return NULL_TREE;
884 // Also, if there's a pre-existing specialization with matching
885 // constraints, then this also isn't new.
886 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
887 while (specs)
889 tree spec_tmpl = TREE_VALUE (specs);
890 tree spec_args = TREE_PURPOSE (specs);
891 tree spec_constr = get_constraints (spec_tmpl);
892 if (comp_template_args (args, spec_args)
893 && equivalent_constraints (type_constr, spec_constr))
894 return NULL_TREE;
895 specs = TREE_CHAIN (specs);
898 // Create a new type node (and corresponding type decl)
899 // for the newly declared specialization.
900 tree t = make_class_type (TREE_CODE (type));
901 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
902 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
904 /* We only need a separate type node for storing the definition of this
905 partial specialization; uses of S<T*> are unconstrained, so all are
906 equivalent. So keep TYPE_CANONICAL the same. */
907 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
909 // Build the corresponding type decl.
910 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
911 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
912 DECL_SOURCE_LOCATION (d) = input_location;
914 return t;
917 return NULL_TREE;
920 /* The TYPE is being declared. If it is a template type, that means it
921 is a partial specialization. Do appropriate error-checking. */
923 tree
924 maybe_process_partial_specialization (tree type)
926 tree context;
928 if (type == error_mark_node)
929 return error_mark_node;
931 /* A lambda that appears in specialization context is not itself a
932 specialization. */
933 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
934 return type;
936 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
938 error ("name of class shadows template template parameter %qD",
939 TYPE_NAME (type));
940 return error_mark_node;
943 context = TYPE_CONTEXT (type);
945 if (TYPE_ALIAS_P (type))
947 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
949 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
950 error ("specialization of alias template %qD",
951 TI_TEMPLATE (tinfo));
952 else
953 error ("explicit specialization of non-template %qT", type);
954 return error_mark_node;
956 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
958 /* This is for ordinary explicit specialization and partial
959 specialization of a template class such as:
961 template <> class C<int>;
965 template <class T> class C<T*>;
967 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
969 if (tree t = maybe_new_partial_specialization (type))
971 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
972 && !at_namespace_scope_p ())
973 return error_mark_node;
974 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
975 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
976 if (processing_template_decl)
978 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
979 if (decl == error_mark_node)
980 return error_mark_node;
981 return TREE_TYPE (decl);
984 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
985 error ("specialization of %qT after instantiation", type);
986 else if (errorcount && !processing_specialization
987 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
988 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
989 /* Trying to define a specialization either without a template<> header
990 or in an inappropriate place. We've already given an error, so just
991 bail now so we don't actually define the specialization. */
992 return error_mark_node;
994 else if (CLASS_TYPE_P (type)
995 && !CLASSTYPE_USE_TEMPLATE (type)
996 && CLASSTYPE_TEMPLATE_INFO (type)
997 && context && CLASS_TYPE_P (context)
998 && CLASSTYPE_TEMPLATE_INFO (context))
1000 /* This is for an explicit specialization of member class
1001 template according to [temp.expl.spec/18]:
1003 template <> template <class U> class C<int>::D;
1005 The context `C<int>' must be an implicit instantiation.
1006 Otherwise this is just a member class template declared
1007 earlier like:
1009 template <> class C<int> { template <class U> class D; };
1010 template <> template <class U> class C<int>::D;
1012 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1013 while in the second case, `C<int>::D' is a primary template
1014 and `C<T>::D' may not exist. */
1016 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1017 && !COMPLETE_TYPE_P (type))
1019 tree t;
1020 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1022 if (current_namespace
1023 != decl_namespace_context (tmpl))
1025 permerror (input_location,
1026 "specializing %q#T in different namespace", type);
1027 permerror (DECL_SOURCE_LOCATION (tmpl),
1028 " from definition of %q#D", tmpl);
1031 /* Check for invalid specialization after instantiation:
1033 template <> template <> class C<int>::D<int>;
1034 template <> template <class U> class C<int>::D; */
1036 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1037 t; t = TREE_CHAIN (t))
1039 tree inst = TREE_VALUE (t);
1040 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1041 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1043 /* We already have a full specialization of this partial
1044 instantiation, or a full specialization has been
1045 looked up but not instantiated. Reassign it to the
1046 new member specialization template. */
1047 spec_entry elt;
1048 spec_entry *entry;
1050 elt.tmpl = most_general_template (tmpl);
1051 elt.args = CLASSTYPE_TI_ARGS (inst);
1052 elt.spec = inst;
1054 type_specializations->remove_elt (&elt);
1056 elt.tmpl = tmpl;
1057 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1059 spec_entry **slot
1060 = type_specializations->find_slot (&elt, INSERT);
1061 entry = ggc_alloc<spec_entry> ();
1062 *entry = elt;
1063 *slot = entry;
1065 else
1066 /* But if we've had an implicit instantiation, that's a
1067 problem ([temp.expl.spec]/6). */
1068 error ("specialization %qT after instantiation %qT",
1069 type, inst);
1072 /* Mark TYPE as a specialization. And as a result, we only
1073 have one level of template argument for the innermost
1074 class template. */
1075 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1076 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1077 CLASSTYPE_TI_ARGS (type)
1078 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1081 else if (processing_specialization)
1083 /* Someday C++0x may allow for enum template specialization. */
1084 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1085 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1086 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1087 "of %qD not allowed by ISO C++", type);
1088 else
1090 error ("explicit specialization of non-template %qT", type);
1091 return error_mark_node;
1095 return type;
1098 /* Returns nonzero if we can optimize the retrieval of specializations
1099 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1100 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1102 static inline bool
1103 optimize_specialization_lookup_p (tree tmpl)
1105 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1106 && DECL_CLASS_SCOPE_P (tmpl)
1107 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1108 parameter. */
1109 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1110 /* The optimized lookup depends on the fact that the
1111 template arguments for the member function template apply
1112 purely to the containing class, which is not true if the
1113 containing class is an explicit or partial
1114 specialization. */
1115 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1116 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1117 && !DECL_CONV_FN_P (tmpl)
1118 /* It is possible to have a template that is not a member
1119 template and is not a member of a template class:
1121 template <typename T>
1122 struct S { friend A::f(); };
1124 Here, the friend function is a template, but the context does
1125 not have template information. The optimized lookup relies
1126 on having ARGS be the template arguments for both the class
1127 and the function template. */
1128 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1131 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1132 gone through coerce_template_parms by now. */
1134 static void
1135 verify_unstripped_args (tree args)
1137 ++processing_template_decl;
1138 if (!any_dependent_template_arguments_p (args))
1140 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1141 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1143 tree arg = TREE_VEC_ELT (inner, i);
1144 if (TREE_CODE (arg) == TEMPLATE_DECL)
1145 /* OK */;
1146 else if (TYPE_P (arg))
1147 gcc_assert (strip_typedefs (arg, NULL) == arg);
1148 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1151 else
1152 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1155 --processing_template_decl;
1158 /* Retrieve the specialization (in the sense of [temp.spec] - a
1159 specialization is either an instantiation or an explicit
1160 specialization) of TMPL for the given template ARGS. If there is
1161 no such specialization, return NULL_TREE. The ARGS are a vector of
1162 arguments, or a vector of vectors of arguments, in the case of
1163 templates with more than one level of parameters.
1165 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1166 then we search for a partial specialization matching ARGS. This
1167 parameter is ignored if TMPL is not a class template.
1169 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1170 result is a NONTYPE_ARGUMENT_PACK. */
1172 static tree
1173 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1175 if (tmpl == NULL_TREE)
1176 return NULL_TREE;
1178 if (args == error_mark_node)
1179 return NULL_TREE;
1181 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1182 || TREE_CODE (tmpl) == FIELD_DECL);
1184 /* There should be as many levels of arguments as there are
1185 levels of parameters. */
1186 gcc_assert (TMPL_ARGS_DEPTH (args)
1187 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1188 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1189 : template_class_depth (DECL_CONTEXT (tmpl))));
1191 if (flag_checking)
1192 verify_unstripped_args (args);
1194 /* Lambda functions in templates aren't instantiated normally, but through
1195 tsubst_lambda_expr. */
1196 if (lambda_fn_in_template_p (tmpl))
1197 return NULL_TREE;
1199 if (optimize_specialization_lookup_p (tmpl))
1201 /* The template arguments actually apply to the containing
1202 class. Find the class specialization with those
1203 arguments. */
1204 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1205 tree class_specialization
1206 = retrieve_specialization (class_template, args, 0);
1207 if (!class_specialization)
1208 return NULL_TREE;
1210 /* Find the instance of TMPL. */
1211 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1212 for (ovl_iterator iter (fns); iter; ++iter)
1214 tree fn = *iter;
1215 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1216 /* using-declarations can add base methods to the method vec,
1217 and we don't want those here. */
1218 && DECL_CONTEXT (fn) == class_specialization)
1219 return fn;
1221 return NULL_TREE;
1223 else
1225 spec_entry *found;
1226 spec_entry elt;
1227 hash_table<spec_hasher> *specializations;
1229 elt.tmpl = tmpl;
1230 elt.args = args;
1231 elt.spec = NULL_TREE;
1233 if (DECL_CLASS_TEMPLATE_P (tmpl))
1234 specializations = type_specializations;
1235 else
1236 specializations = decl_specializations;
1238 if (hash == 0)
1239 hash = spec_hasher::hash (&elt);
1240 found = specializations->find_with_hash (&elt, hash);
1241 if (found)
1242 return found->spec;
1245 return NULL_TREE;
1248 /* Like retrieve_specialization, but for local declarations. */
1250 tree
1251 retrieve_local_specialization (tree tmpl)
1253 if (local_specializations == NULL)
1254 return NULL_TREE;
1256 tree *slot = local_specializations->get (tmpl);
1257 return slot ? *slot : NULL_TREE;
1260 /* Returns nonzero iff DECL is a specialization of TMPL. */
1263 is_specialization_of (tree decl, tree tmpl)
1265 tree t;
1267 if (TREE_CODE (decl) == FUNCTION_DECL)
1269 for (t = decl;
1270 t != NULL_TREE;
1271 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1272 if (t == tmpl)
1273 return 1;
1275 else
1277 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1279 for (t = TREE_TYPE (decl);
1280 t != NULL_TREE;
1281 t = CLASSTYPE_USE_TEMPLATE (t)
1282 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1283 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1284 return 1;
1287 return 0;
1290 /* Returns nonzero iff DECL is a specialization of friend declaration
1291 FRIEND_DECL according to [temp.friend]. */
1293 bool
1294 is_specialization_of_friend (tree decl, tree friend_decl)
1296 bool need_template = true;
1297 int template_depth;
1299 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1300 || TREE_CODE (decl) == TYPE_DECL);
1302 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1303 of a template class, we want to check if DECL is a specialization
1304 if this. */
1305 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1306 && DECL_TEMPLATE_INFO (friend_decl)
1307 && !DECL_USE_TEMPLATE (friend_decl))
1309 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1310 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1311 need_template = false;
1313 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1314 && !PRIMARY_TEMPLATE_P (friend_decl))
1315 need_template = false;
1317 /* There is nothing to do if this is not a template friend. */
1318 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1319 return false;
1321 if (is_specialization_of (decl, friend_decl))
1322 return true;
1324 /* [temp.friend/6]
1325 A member of a class template may be declared to be a friend of a
1326 non-template class. In this case, the corresponding member of
1327 every specialization of the class template is a friend of the
1328 class granting friendship.
1330 For example, given a template friend declaration
1332 template <class T> friend void A<T>::f();
1334 the member function below is considered a friend
1336 template <> struct A<int> {
1337 void f();
1340 For this type of template friend, TEMPLATE_DEPTH below will be
1341 nonzero. To determine if DECL is a friend of FRIEND, we first
1342 check if the enclosing class is a specialization of another. */
1344 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1345 if (template_depth
1346 && DECL_CLASS_SCOPE_P (decl)
1347 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1348 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1350 /* Next, we check the members themselves. In order to handle
1351 a few tricky cases, such as when FRIEND_DECL's are
1353 template <class T> friend void A<T>::g(T t);
1354 template <class T> template <T t> friend void A<T>::h();
1356 and DECL's are
1358 void A<int>::g(int);
1359 template <int> void A<int>::h();
1361 we need to figure out ARGS, the template arguments from
1362 the context of DECL. This is required for template substitution
1363 of `T' in the function parameter of `g' and template parameter
1364 of `h' in the above examples. Here ARGS corresponds to `int'. */
1366 tree context = DECL_CONTEXT (decl);
1367 tree args = NULL_TREE;
1368 int current_depth = 0;
1370 while (current_depth < template_depth)
1372 if (CLASSTYPE_TEMPLATE_INFO (context))
1374 if (current_depth == 0)
1375 args = TYPE_TI_ARGS (context);
1376 else
1377 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1378 current_depth++;
1380 context = TYPE_CONTEXT (context);
1383 if (TREE_CODE (decl) == FUNCTION_DECL)
1385 bool is_template;
1386 tree friend_type;
1387 tree decl_type;
1388 tree friend_args_type;
1389 tree decl_args_type;
1391 /* Make sure that both DECL and FRIEND_DECL are templates or
1392 non-templates. */
1393 is_template = DECL_TEMPLATE_INFO (decl)
1394 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1395 if (need_template ^ is_template)
1396 return false;
1397 else if (is_template)
1399 /* If both are templates, check template parameter list. */
1400 tree friend_parms
1401 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1402 args, tf_none);
1403 if (!comp_template_parms
1404 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1405 friend_parms))
1406 return false;
1408 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1410 else
1411 decl_type = TREE_TYPE (decl);
1413 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1414 tf_none, NULL_TREE);
1415 if (friend_type == error_mark_node)
1416 return false;
1418 /* Check if return types match. */
1419 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1420 return false;
1422 /* Check if function parameter types match, ignoring the
1423 `this' parameter. */
1424 friend_args_type = TYPE_ARG_TYPES (friend_type);
1425 decl_args_type = TYPE_ARG_TYPES (decl_type);
1426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1427 friend_args_type = TREE_CHAIN (friend_args_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1429 decl_args_type = TREE_CHAIN (decl_args_type);
1431 return compparms (decl_args_type, friend_args_type);
1433 else
1435 /* DECL is a TYPE_DECL */
1436 bool is_template;
1437 tree decl_type = TREE_TYPE (decl);
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1440 non-templates. */
1441 is_template
1442 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1445 if (need_template ^ is_template)
1446 return false;
1447 else if (is_template)
1449 tree friend_parms;
1450 /* If both are templates, check the name of the two
1451 TEMPLATE_DECL's first because is_friend didn't. */
1452 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1453 != DECL_NAME (friend_decl))
1454 return false;
1456 /* Now check template parameter list. */
1457 friend_parms
1458 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1459 args, tf_none);
1460 return comp_template_parms
1461 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1462 friend_parms);
1464 else
1465 return (DECL_NAME (decl)
1466 == DECL_NAME (friend_decl));
1469 return false;
1472 /* Register the specialization SPEC as a specialization of TMPL with
1473 the indicated ARGS. IS_FRIEND indicates whether the specialization
1474 is actually just a friend declaration. Returns SPEC, or an
1475 equivalent prior declaration, if available.
1477 We also store instantiations of field packs in the hash table, even
1478 though they are not themselves templates, to make lookup easier. */
1480 static tree
1481 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1482 hashval_t hash)
1484 tree fn;
1485 spec_entry **slot = NULL;
1486 spec_entry elt;
1488 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1489 || (TREE_CODE (tmpl) == FIELD_DECL
1490 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1492 if (TREE_CODE (spec) == FUNCTION_DECL
1493 && uses_template_parms (DECL_TI_ARGS (spec)))
1494 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1495 register it; we want the corresponding TEMPLATE_DECL instead.
1496 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1497 the more obvious `uses_template_parms (spec)' to avoid problems
1498 with default function arguments. In particular, given
1499 something like this:
1501 template <class T> void f(T t1, T t = T())
1503 the default argument expression is not substituted for in an
1504 instantiation unless and until it is actually needed. */
1505 return spec;
1507 if (optimize_specialization_lookup_p (tmpl))
1508 /* We don't put these specializations in the hash table, but we might
1509 want to give an error about a mismatch. */
1510 fn = retrieve_specialization (tmpl, args, 0);
1511 else
1513 elt.tmpl = tmpl;
1514 elt.args = args;
1515 elt.spec = spec;
1517 if (hash == 0)
1518 hash = spec_hasher::hash (&elt);
1520 slot =
1521 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1522 if (*slot)
1523 fn = ((spec_entry *) *slot)->spec;
1524 else
1525 fn = NULL_TREE;
1528 /* We can sometimes try to re-register a specialization that we've
1529 already got. In particular, regenerate_decl_from_template calls
1530 duplicate_decls which will update the specialization list. But,
1531 we'll still get called again here anyhow. It's more convenient
1532 to simply allow this than to try to prevent it. */
1533 if (fn == spec)
1534 return spec;
1535 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1537 if (DECL_TEMPLATE_INSTANTIATION (fn))
1539 if (DECL_ODR_USED (fn)
1540 || DECL_EXPLICIT_INSTANTIATION (fn))
1542 error ("specialization of %qD after instantiation",
1543 fn);
1544 return error_mark_node;
1546 else
1548 tree clone;
1549 /* This situation should occur only if the first
1550 specialization is an implicit instantiation, the
1551 second is an explicit specialization, and the
1552 implicit instantiation has not yet been used. That
1553 situation can occur if we have implicitly
1554 instantiated a member function and then specialized
1555 it later.
1557 We can also wind up here if a friend declaration that
1558 looked like an instantiation turns out to be a
1559 specialization:
1561 template <class T> void foo(T);
1562 class S { friend void foo<>(int) };
1563 template <> void foo(int);
1565 We transform the existing DECL in place so that any
1566 pointers to it become pointers to the updated
1567 declaration.
1569 If there was a definition for the template, but not
1570 for the specialization, we want this to look as if
1571 there were no definition, and vice versa. */
1572 DECL_INITIAL (fn) = NULL_TREE;
1573 duplicate_decls (spec, fn, is_friend);
1574 /* The call to duplicate_decls will have applied
1575 [temp.expl.spec]:
1577 An explicit specialization of a function template
1578 is inline only if it is explicitly declared to be,
1579 and independently of whether its function template
1582 to the primary function; now copy the inline bits to
1583 the various clones. */
1584 FOR_EACH_CLONE (clone, fn)
1586 DECL_DECLARED_INLINE_P (clone)
1587 = DECL_DECLARED_INLINE_P (fn);
1588 DECL_SOURCE_LOCATION (clone)
1589 = DECL_SOURCE_LOCATION (fn);
1590 DECL_DELETED_FN (clone)
1591 = DECL_DELETED_FN (fn);
1593 check_specialization_namespace (tmpl);
1595 return fn;
1598 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1600 tree dd = duplicate_decls (spec, fn, is_friend);
1601 if (dd == error_mark_node)
1602 /* We've already complained in duplicate_decls. */
1603 return error_mark_node;
1605 if (dd == NULL_TREE && DECL_INITIAL (spec))
1606 /* Dup decl failed, but this is a new definition. Set the
1607 line number so any errors match this new
1608 definition. */
1609 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1611 return fn;
1614 else if (fn)
1615 return duplicate_decls (spec, fn, is_friend);
1617 /* A specialization must be declared in the same namespace as the
1618 template it is specializing. */
1619 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1620 && !check_specialization_namespace (tmpl))
1621 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1623 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1625 spec_entry *entry = ggc_alloc<spec_entry> ();
1626 gcc_assert (tmpl && args && spec);
1627 *entry = elt;
1628 *slot = entry;
1629 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1630 && PRIMARY_TEMPLATE_P (tmpl)
1631 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1632 || variable_template_p (tmpl))
1633 /* If TMPL is a forward declaration of a template function, keep a list
1634 of all specializations in case we need to reassign them to a friend
1635 template later in tsubst_friend_function.
1637 Also keep a list of all variable template instantiations so that
1638 process_partial_specialization can check whether a later partial
1639 specialization would have used it. */
1640 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1641 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1644 return spec;
1647 /* Returns true iff two spec_entry nodes are equivalent. */
1649 int comparing_specializations;
1651 bool
1652 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1654 int equal;
1656 ++comparing_specializations;
1657 equal = (e1->tmpl == e2->tmpl
1658 && comp_template_args (e1->args, e2->args));
1659 if (equal && flag_concepts
1660 /* tmpl could be a FIELD_DECL for a capture pack. */
1661 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1662 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1663 && uses_template_parms (e1->args))
1665 /* Partial specializations of a variable template can be distinguished by
1666 constraints. */
1667 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1668 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1669 equal = equivalent_constraints (c1, c2);
1671 --comparing_specializations;
1673 return equal;
1676 /* Returns a hash for a template TMPL and template arguments ARGS. */
1678 static hashval_t
1679 hash_tmpl_and_args (tree tmpl, tree args)
1681 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1682 return iterative_hash_template_arg (args, val);
1685 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1686 ignoring SPEC. */
1688 hashval_t
1689 spec_hasher::hash (spec_entry *e)
1691 return hash_tmpl_and_args (e->tmpl, e->args);
1694 /* Recursively calculate a hash value for a template argument ARG, for use
1695 in the hash tables of template specializations. */
1697 hashval_t
1698 iterative_hash_template_arg (tree arg, hashval_t val)
1700 unsigned HOST_WIDE_INT i;
1701 enum tree_code code;
1702 char tclass;
1704 if (arg == NULL_TREE)
1705 return iterative_hash_object (arg, val);
1707 if (!TYPE_P (arg))
1708 STRIP_NOPS (arg);
1710 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1711 gcc_unreachable ();
1713 code = TREE_CODE (arg);
1714 tclass = TREE_CODE_CLASS (code);
1716 val = iterative_hash_object (code, val);
1718 switch (code)
1720 case ERROR_MARK:
1721 return val;
1723 case IDENTIFIER_NODE:
1724 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1726 case TREE_VEC:
1728 int i, len = TREE_VEC_LENGTH (arg);
1729 for (i = 0; i < len; ++i)
1730 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1731 return val;
1734 case TYPE_PACK_EXPANSION:
1735 case EXPR_PACK_EXPANSION:
1736 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1737 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1739 case TYPE_ARGUMENT_PACK:
1740 case NONTYPE_ARGUMENT_PACK:
1741 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1743 case TREE_LIST:
1744 for (; arg; arg = TREE_CHAIN (arg))
1745 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1746 return val;
1748 case OVERLOAD:
1749 for (lkp_iterator iter (arg); iter; ++iter)
1750 val = iterative_hash_template_arg (*iter, val);
1751 return val;
1753 case CONSTRUCTOR:
1755 tree field, value;
1756 iterative_hash_template_arg (TREE_TYPE (arg), val);
1757 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1759 val = iterative_hash_template_arg (field, val);
1760 val = iterative_hash_template_arg (value, val);
1762 return val;
1765 case PARM_DECL:
1766 if (!DECL_ARTIFICIAL (arg))
1768 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1769 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1771 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1773 case TARGET_EXPR:
1774 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1776 case PTRMEM_CST:
1777 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1778 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1780 case TEMPLATE_PARM_INDEX:
1781 val = iterative_hash_template_arg
1782 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1783 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1784 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1786 case TRAIT_EXPR:
1787 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1788 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1789 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1791 case BASELINK:
1792 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1793 val);
1794 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1795 val);
1797 case MODOP_EXPR:
1798 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1799 code = TREE_CODE (TREE_OPERAND (arg, 1));
1800 val = iterative_hash_object (code, val);
1801 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1803 case LAMBDA_EXPR:
1804 /* A lambda can't appear in a template arg, but don't crash on
1805 erroneous input. */
1806 gcc_assert (seen_error ());
1807 return val;
1809 case CAST_EXPR:
1810 case IMPLICIT_CONV_EXPR:
1811 case STATIC_CAST_EXPR:
1812 case REINTERPRET_CAST_EXPR:
1813 case CONST_CAST_EXPR:
1814 case DYNAMIC_CAST_EXPR:
1815 case NEW_EXPR:
1816 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1817 /* Now hash operands as usual. */
1818 break;
1820 default:
1821 break;
1824 switch (tclass)
1826 case tcc_type:
1827 if (alias_template_specialization_p (arg))
1829 // We want an alias specialization that survived strip_typedefs
1830 // to hash differently from its TYPE_CANONICAL, to avoid hash
1831 // collisions that compare as different in template_args_equal.
1832 // These could be dependent specializations that strip_typedefs
1833 // left alone, or untouched specializations because
1834 // coerce_template_parms returns the unconverted template
1835 // arguments if it sees incomplete argument packs.
1836 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1837 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1839 if (TYPE_CANONICAL (arg))
1840 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1841 val);
1842 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1843 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1844 /* Otherwise just compare the types during lookup. */
1845 return val;
1847 case tcc_declaration:
1848 case tcc_constant:
1849 return iterative_hash_expr (arg, val);
1851 default:
1852 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1854 unsigned n = cp_tree_operand_length (arg);
1855 for (i = 0; i < n; ++i)
1856 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1857 return val;
1860 gcc_unreachable ();
1861 return 0;
1864 /* Unregister the specialization SPEC as a specialization of TMPL.
1865 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1866 if the SPEC was listed as a specialization of TMPL.
1868 Note that SPEC has been ggc_freed, so we can't look inside it. */
1870 bool
1871 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1873 spec_entry *entry;
1874 spec_entry elt;
1876 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1877 elt.args = TI_ARGS (tinfo);
1878 elt.spec = NULL_TREE;
1880 entry = decl_specializations->find (&elt);
1881 if (entry != NULL)
1883 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1884 gcc_assert (new_spec != NULL_TREE);
1885 entry->spec = new_spec;
1886 return 1;
1889 return 0;
1892 /* Like register_specialization, but for local declarations. We are
1893 registering SPEC, an instantiation of TMPL. */
1895 void
1896 register_local_specialization (tree spec, tree tmpl)
1898 local_specializations->put (tmpl, spec);
1901 /* TYPE is a class type. Returns true if TYPE is an explicitly
1902 specialized class. */
1904 bool
1905 explicit_class_specialization_p (tree type)
1907 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1908 return false;
1909 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1912 /* Print the list of functions at FNS, going through all the overloads
1913 for each element of the list. Alternatively, FNS can not be a
1914 TREE_LIST, in which case it will be printed together with all the
1915 overloads.
1917 MORE and *STR should respectively be FALSE and NULL when the function
1918 is called from the outside. They are used internally on recursive
1919 calls. print_candidates manages the two parameters and leaves NULL
1920 in *STR when it ends. */
1922 static void
1923 print_candidates_1 (tree fns, char **str, bool more = false)
1925 if (TREE_CODE (fns) == TREE_LIST)
1926 for (; fns; fns = TREE_CHAIN (fns))
1927 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1928 else
1929 for (lkp_iterator iter (fns); iter;)
1931 tree cand = *iter;
1932 ++iter;
1934 const char *pfx = *str;
1935 if (!pfx)
1937 if (more || iter)
1938 pfx = _("candidates are:");
1939 else
1940 pfx = _("candidate is:");
1941 *str = get_spaces (pfx);
1943 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1947 /* Print the list of candidate FNS in an error message. FNS can also
1948 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1950 void
1951 print_candidates (tree fns)
1953 char *str = NULL;
1954 print_candidates_1 (fns, &str);
1955 free (str);
1958 /* Get a (possibly) constrained template declaration for the
1959 purpose of ordering candidates. */
1960 static tree
1961 get_template_for_ordering (tree list)
1963 gcc_assert (TREE_CODE (list) == TREE_LIST);
1964 tree f = TREE_VALUE (list);
1965 if (tree ti = DECL_TEMPLATE_INFO (f))
1966 return TI_TEMPLATE (ti);
1967 return f;
1970 /* Among candidates having the same signature, return the
1971 most constrained or NULL_TREE if there is no best candidate.
1972 If the signatures of candidates vary (e.g., template
1973 specialization vs. member function), then there can be no
1974 most constrained.
1976 Note that we don't compare constraints on the functions
1977 themselves, but rather those of their templates. */
1978 static tree
1979 most_constrained_function (tree candidates)
1981 // Try to find the best candidate in a first pass.
1982 tree champ = candidates;
1983 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1985 int winner = more_constrained (get_template_for_ordering (champ),
1986 get_template_for_ordering (c));
1987 if (winner == -1)
1988 champ = c; // The candidate is more constrained
1989 else if (winner == 0)
1990 return NULL_TREE; // Neither is more constrained
1993 // Verify that the champ is better than previous candidates.
1994 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1995 if (!more_constrained (get_template_for_ordering (champ),
1996 get_template_for_ordering (c)))
1997 return NULL_TREE;
2000 return champ;
2004 /* Returns the template (one of the functions given by TEMPLATE_ID)
2005 which can be specialized to match the indicated DECL with the
2006 explicit template args given in TEMPLATE_ID. The DECL may be
2007 NULL_TREE if none is available. In that case, the functions in
2008 TEMPLATE_ID are non-members.
2010 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2011 specialization of a member template.
2013 The TEMPLATE_COUNT is the number of references to qualifying
2014 template classes that appeared in the name of the function. See
2015 check_explicit_specialization for a more accurate description.
2017 TSK indicates what kind of template declaration (if any) is being
2018 declared. TSK_TEMPLATE indicates that the declaration given by
2019 DECL, though a FUNCTION_DECL, has template parameters, and is
2020 therefore a template function.
2022 The template args (those explicitly specified and those deduced)
2023 are output in a newly created vector *TARGS_OUT.
2025 If it is impossible to determine the result, an error message is
2026 issued. The error_mark_node is returned to indicate failure. */
2028 static tree
2029 determine_specialization (tree template_id,
2030 tree decl,
2031 tree* targs_out,
2032 int need_member_template,
2033 int template_count,
2034 tmpl_spec_kind tsk)
2036 tree fns;
2037 tree targs;
2038 tree explicit_targs;
2039 tree candidates = NULL_TREE;
2041 /* A TREE_LIST of templates of which DECL may be a specialization.
2042 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2043 corresponding TREE_PURPOSE is the set of template arguments that,
2044 when used to instantiate the template, would produce a function
2045 with the signature of DECL. */
2046 tree templates = NULL_TREE;
2047 int header_count;
2048 cp_binding_level *b;
2050 *targs_out = NULL_TREE;
2052 if (template_id == error_mark_node || decl == error_mark_node)
2053 return error_mark_node;
2055 /* We shouldn't be specializing a member template of an
2056 unspecialized class template; we already gave an error in
2057 check_specialization_scope, now avoid crashing. */
2058 if (template_count && DECL_CLASS_SCOPE_P (decl)
2059 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2061 gcc_assert (errorcount);
2062 return error_mark_node;
2065 fns = TREE_OPERAND (template_id, 0);
2066 explicit_targs = TREE_OPERAND (template_id, 1);
2068 if (fns == error_mark_node)
2069 return error_mark_node;
2071 /* Check for baselinks. */
2072 if (BASELINK_P (fns))
2073 fns = BASELINK_FUNCTIONS (fns);
2075 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2077 error ("%qD is not a function template", fns);
2078 return error_mark_node;
2080 else if (VAR_P (decl) && !variable_template_p (fns))
2082 error ("%qD is not a variable template", fns);
2083 return error_mark_node;
2086 /* Count the number of template headers specified for this
2087 specialization. */
2088 header_count = 0;
2089 for (b = current_binding_level;
2090 b->kind == sk_template_parms;
2091 b = b->level_chain)
2092 ++header_count;
2094 tree orig_fns = fns;
2096 if (variable_template_p (fns))
2098 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2099 targs = coerce_template_parms (parms, explicit_targs, fns,
2100 tf_warning_or_error,
2101 /*req_all*/true, /*use_defarg*/true);
2102 if (targs != error_mark_node)
2103 templates = tree_cons (targs, fns, templates);
2105 else for (lkp_iterator iter (fns); iter; ++iter)
2107 tree fn = *iter;
2109 if (TREE_CODE (fn) == TEMPLATE_DECL)
2111 tree decl_arg_types;
2112 tree fn_arg_types;
2113 tree insttype;
2115 /* In case of explicit specialization, we need to check if
2116 the number of template headers appearing in the specialization
2117 is correct. This is usually done in check_explicit_specialization,
2118 but the check done there cannot be exhaustive when specializing
2119 member functions. Consider the following code:
2121 template <> void A<int>::f(int);
2122 template <> template <> void A<int>::f(int);
2124 Assuming that A<int> is not itself an explicit specialization
2125 already, the first line specializes "f" which is a non-template
2126 member function, whilst the second line specializes "f" which
2127 is a template member function. So both lines are syntactically
2128 correct, and check_explicit_specialization does not reject
2129 them.
2131 Here, we can do better, as we are matching the specialization
2132 against the declarations. We count the number of template
2133 headers, and we check if they match TEMPLATE_COUNT + 1
2134 (TEMPLATE_COUNT is the number of qualifying template classes,
2135 plus there must be another header for the member template
2136 itself).
2138 Notice that if header_count is zero, this is not a
2139 specialization but rather a template instantiation, so there
2140 is no check we can perform here. */
2141 if (header_count && header_count != template_count + 1)
2142 continue;
2144 /* Check that the number of template arguments at the
2145 innermost level for DECL is the same as for FN. */
2146 if (current_binding_level->kind == sk_template_parms
2147 && !current_binding_level->explicit_spec_p
2148 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2149 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2150 (current_template_parms))))
2151 continue;
2153 /* DECL might be a specialization of FN. */
2154 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2155 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2157 /* For a non-static member function, we need to make sure
2158 that the const qualification is the same. Since
2159 get_bindings does not try to merge the "this" parameter,
2160 we must do the comparison explicitly. */
2161 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2162 && !same_type_p (TREE_VALUE (fn_arg_types),
2163 TREE_VALUE (decl_arg_types)))
2164 continue;
2166 /* Skip the "this" parameter and, for constructors of
2167 classes with virtual bases, the VTT parameter. A
2168 full specialization of a constructor will have a VTT
2169 parameter, but a template never will. */
2170 decl_arg_types
2171 = skip_artificial_parms_for (decl, decl_arg_types);
2172 fn_arg_types
2173 = skip_artificial_parms_for (fn, fn_arg_types);
2175 /* Function templates cannot be specializations; there are
2176 no partial specializations of functions. Therefore, if
2177 the type of DECL does not match FN, there is no
2178 match.
2180 Note that it should never be the case that we have both
2181 candidates added here, and for regular member functions
2182 below. */
2183 if (tsk == tsk_template)
2185 if (compparms (fn_arg_types, decl_arg_types))
2186 candidates = tree_cons (NULL_TREE, fn, candidates);
2187 continue;
2190 /* See whether this function might be a specialization of this
2191 template. Suppress access control because we might be trying
2192 to make this specialization a friend, and we have already done
2193 access control for the declaration of the specialization. */
2194 push_deferring_access_checks (dk_no_check);
2195 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2196 pop_deferring_access_checks ();
2198 if (!targs)
2199 /* We cannot deduce template arguments that when used to
2200 specialize TMPL will produce DECL. */
2201 continue;
2203 /* Remove, from the set of candidates, all those functions
2204 whose constraints are not satisfied. */
2205 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2206 continue;
2208 // Then, try to form the new function type.
2209 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2210 if (insttype == error_mark_node)
2211 continue;
2212 fn_arg_types
2213 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2214 if (!compparms (fn_arg_types, decl_arg_types))
2215 continue;
2217 /* Save this template, and the arguments deduced. */
2218 templates = tree_cons (targs, fn, templates);
2220 else if (need_member_template)
2221 /* FN is an ordinary member function, and we need a
2222 specialization of a member template. */
2224 else if (TREE_CODE (fn) != FUNCTION_DECL)
2225 /* We can get IDENTIFIER_NODEs here in certain erroneous
2226 cases. */
2228 else if (!DECL_FUNCTION_MEMBER_P (fn))
2229 /* This is just an ordinary non-member function. Nothing can
2230 be a specialization of that. */
2232 else if (DECL_ARTIFICIAL (fn))
2233 /* Cannot specialize functions that are created implicitly. */
2235 else
2237 tree decl_arg_types;
2239 /* This is an ordinary member function. However, since
2240 we're here, we can assume its enclosing class is a
2241 template class. For example,
2243 template <typename T> struct S { void f(); };
2244 template <> void S<int>::f() {}
2246 Here, S<int>::f is a non-template, but S<int> is a
2247 template class. If FN has the same type as DECL, we
2248 might be in business. */
2250 if (!DECL_TEMPLATE_INFO (fn))
2251 /* Its enclosing class is an explicit specialization
2252 of a template class. This is not a candidate. */
2253 continue;
2255 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2256 TREE_TYPE (TREE_TYPE (fn))))
2257 /* The return types differ. */
2258 continue;
2260 /* Adjust the type of DECL in case FN is a static member. */
2261 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2262 if (DECL_STATIC_FUNCTION_P (fn)
2263 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2264 decl_arg_types = TREE_CHAIN (decl_arg_types);
2266 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2267 decl_arg_types))
2268 continue;
2270 // If the deduced arguments do not satisfy the constraints,
2271 // this is not a candidate.
2272 if (flag_concepts && !constraints_satisfied_p (fn))
2273 continue;
2275 // Add the candidate.
2276 candidates = tree_cons (NULL_TREE, fn, candidates);
2280 if (templates && TREE_CHAIN (templates))
2282 /* We have:
2284 [temp.expl.spec]
2286 It is possible for a specialization with a given function
2287 signature to be instantiated from more than one function
2288 template. In such cases, explicit specification of the
2289 template arguments must be used to uniquely identify the
2290 function template specialization being specialized.
2292 Note that here, there's no suggestion that we're supposed to
2293 determine which of the candidate templates is most
2294 specialized. However, we, also have:
2296 [temp.func.order]
2298 Partial ordering of overloaded function template
2299 declarations is used in the following contexts to select
2300 the function template to which a function template
2301 specialization refers:
2303 -- when an explicit specialization refers to a function
2304 template.
2306 So, we do use the partial ordering rules, at least for now.
2307 This extension can only serve to make invalid programs valid,
2308 so it's safe. And, there is strong anecdotal evidence that
2309 the committee intended the partial ordering rules to apply;
2310 the EDG front end has that behavior, and John Spicer claims
2311 that the committee simply forgot to delete the wording in
2312 [temp.expl.spec]. */
2313 tree tmpl = most_specialized_instantiation (templates);
2314 if (tmpl != error_mark_node)
2316 templates = tmpl;
2317 TREE_CHAIN (templates) = NULL_TREE;
2321 // Concepts allows multiple declarations of member functions
2322 // with the same signature. Like above, we need to rely on
2323 // on the partial ordering of those candidates to determine which
2324 // is the best.
2325 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2327 if (tree cand = most_constrained_function (candidates))
2329 candidates = cand;
2330 TREE_CHAIN (cand) = NULL_TREE;
2334 if (templates == NULL_TREE && candidates == NULL_TREE)
2336 error ("template-id %qD for %q+D does not match any template "
2337 "declaration", template_id, decl);
2338 if (header_count && header_count != template_count + 1)
2339 inform (input_location, "saw %d %<template<>%>, need %d for "
2340 "specializing a member function template",
2341 header_count, template_count + 1);
2342 else
2343 print_candidates (orig_fns);
2344 return error_mark_node;
2346 else if ((templates && TREE_CHAIN (templates))
2347 || (candidates && TREE_CHAIN (candidates))
2348 || (templates && candidates))
2350 error ("ambiguous template specialization %qD for %q+D",
2351 template_id, decl);
2352 candidates = chainon (candidates, templates);
2353 print_candidates (candidates);
2354 return error_mark_node;
2357 /* We have one, and exactly one, match. */
2358 if (candidates)
2360 tree fn = TREE_VALUE (candidates);
2361 *targs_out = copy_node (DECL_TI_ARGS (fn));
2363 // Propagate the candidate's constraints to the declaration.
2364 set_constraints (decl, get_constraints (fn));
2366 /* DECL is a re-declaration or partial instantiation of a template
2367 function. */
2368 if (TREE_CODE (fn) == TEMPLATE_DECL)
2369 return fn;
2370 /* It was a specialization of an ordinary member function in a
2371 template class. */
2372 return DECL_TI_TEMPLATE (fn);
2375 /* It was a specialization of a template. */
2376 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2377 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2379 *targs_out = copy_node (targs);
2380 SET_TMPL_ARGS_LEVEL (*targs_out,
2381 TMPL_ARGS_DEPTH (*targs_out),
2382 TREE_PURPOSE (templates));
2384 else
2385 *targs_out = TREE_PURPOSE (templates);
2386 return TREE_VALUE (templates);
2389 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2390 but with the default argument values filled in from those in the
2391 TMPL_TYPES. */
2393 static tree
2394 copy_default_args_to_explicit_spec_1 (tree spec_types,
2395 tree tmpl_types)
2397 tree new_spec_types;
2399 if (!spec_types)
2400 return NULL_TREE;
2402 if (spec_types == void_list_node)
2403 return void_list_node;
2405 /* Substitute into the rest of the list. */
2406 new_spec_types =
2407 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2408 TREE_CHAIN (tmpl_types));
2410 /* Add the default argument for this parameter. */
2411 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2412 TREE_VALUE (spec_types),
2413 new_spec_types);
2416 /* DECL is an explicit specialization. Replicate default arguments
2417 from the template it specializes. (That way, code like:
2419 template <class T> void f(T = 3);
2420 template <> void f(double);
2421 void g () { f (); }
2423 works, as required.) An alternative approach would be to look up
2424 the correct default arguments at the call-site, but this approach
2425 is consistent with how implicit instantiations are handled. */
2427 static void
2428 copy_default_args_to_explicit_spec (tree decl)
2430 tree tmpl;
2431 tree spec_types;
2432 tree tmpl_types;
2433 tree new_spec_types;
2434 tree old_type;
2435 tree new_type;
2436 tree t;
2437 tree object_type = NULL_TREE;
2438 tree in_charge = NULL_TREE;
2439 tree vtt = NULL_TREE;
2441 /* See if there's anything we need to do. */
2442 tmpl = DECL_TI_TEMPLATE (decl);
2443 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2444 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2445 if (TREE_PURPOSE (t))
2446 break;
2447 if (!t)
2448 return;
2450 old_type = TREE_TYPE (decl);
2451 spec_types = TYPE_ARG_TYPES (old_type);
2453 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2455 /* Remove the this pointer, but remember the object's type for
2456 CV quals. */
2457 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2458 spec_types = TREE_CHAIN (spec_types);
2459 tmpl_types = TREE_CHAIN (tmpl_types);
2461 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2463 /* DECL may contain more parameters than TMPL due to the extra
2464 in-charge parameter in constructors and destructors. */
2465 in_charge = spec_types;
2466 spec_types = TREE_CHAIN (spec_types);
2468 if (DECL_HAS_VTT_PARM_P (decl))
2470 vtt = spec_types;
2471 spec_types = TREE_CHAIN (spec_types);
2475 /* Compute the merged default arguments. */
2476 new_spec_types =
2477 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2479 /* Compute the new FUNCTION_TYPE. */
2480 if (object_type)
2482 if (vtt)
2483 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2484 TREE_VALUE (vtt),
2485 new_spec_types);
2487 if (in_charge)
2488 /* Put the in-charge parameter back. */
2489 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2490 TREE_VALUE (in_charge),
2491 new_spec_types);
2493 new_type = build_method_type_directly (object_type,
2494 TREE_TYPE (old_type),
2495 new_spec_types);
2497 else
2498 new_type = build_function_type (TREE_TYPE (old_type),
2499 new_spec_types);
2500 new_type = cp_build_type_attribute_variant (new_type,
2501 TYPE_ATTRIBUTES (old_type));
2502 new_type = build_exception_variant (new_type,
2503 TYPE_RAISES_EXCEPTIONS (old_type));
2505 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2506 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2508 TREE_TYPE (decl) = new_type;
2511 /* Return the number of template headers we expect to see for a definition
2512 or specialization of CTYPE or one of its non-template members. */
2515 num_template_headers_for_class (tree ctype)
2517 int num_templates = 0;
2519 while (ctype && CLASS_TYPE_P (ctype))
2521 /* You're supposed to have one `template <...>' for every
2522 template class, but you don't need one for a full
2523 specialization. For example:
2525 template <class T> struct S{};
2526 template <> struct S<int> { void f(); };
2527 void S<int>::f () {}
2529 is correct; there shouldn't be a `template <>' for the
2530 definition of `S<int>::f'. */
2531 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2532 /* If CTYPE does not have template information of any
2533 kind, then it is not a template, nor is it nested
2534 within a template. */
2535 break;
2536 if (explicit_class_specialization_p (ctype))
2537 break;
2538 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2539 ++num_templates;
2541 ctype = TYPE_CONTEXT (ctype);
2544 return num_templates;
2547 /* Do a simple sanity check on the template headers that precede the
2548 variable declaration DECL. */
2550 void
2551 check_template_variable (tree decl)
2553 tree ctx = CP_DECL_CONTEXT (decl);
2554 int wanted = num_template_headers_for_class (ctx);
2555 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2556 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2558 if (cxx_dialect < cxx14)
2559 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2560 "variable templates only available with "
2561 "-std=c++14 or -std=gnu++14");
2563 // Namespace-scope variable templates should have a template header.
2564 ++wanted;
2566 if (template_header_count > wanted)
2568 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2569 "too many template headers for %qD "
2570 "(should be %d)",
2571 decl, wanted);
2572 if (warned && CLASS_TYPE_P (ctx)
2573 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2574 inform (DECL_SOURCE_LOCATION (decl),
2575 "members of an explicitly specialized class are defined "
2576 "without a template header");
2580 /* An explicit specialization whose declarator-id or class-head-name is not
2581 qualified shall be declared in the nearest enclosing namespace of the
2582 template, or, if the namespace is inline (7.3.1), any namespace from its
2583 enclosing namespace set.
2585 If the name declared in the explicit instantiation is an unqualified name,
2586 the explicit instantiation shall appear in the namespace where its template
2587 is declared or, if that namespace is inline (7.3.1), any namespace from its
2588 enclosing namespace set. */
2590 void
2591 check_unqualified_spec_or_inst (tree t, location_t loc)
2593 tree tmpl = most_general_template (t);
2594 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2595 && !is_nested_namespace (current_namespace,
2596 CP_DECL_CONTEXT (tmpl), true))
2598 if (processing_specialization)
2599 permerror (loc, "explicit specialization of %qD outside its "
2600 "namespace must use a nested-name-specifier", tmpl);
2601 else if (processing_explicit_instantiation
2602 && cxx_dialect >= cxx11)
2603 /* This was allowed in C++98, so only pedwarn. */
2604 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2605 "outside its namespace must use a nested-name-"
2606 "specifier", tmpl);
2610 /* Check to see if the function just declared, as indicated in
2611 DECLARATOR, and in DECL, is a specialization of a function
2612 template. We may also discover that the declaration is an explicit
2613 instantiation at this point.
2615 Returns DECL, or an equivalent declaration that should be used
2616 instead if all goes well. Issues an error message if something is
2617 amiss. Returns error_mark_node if the error is not easily
2618 recoverable.
2620 FLAGS is a bitmask consisting of the following flags:
2622 2: The function has a definition.
2623 4: The function is a friend.
2625 The TEMPLATE_COUNT is the number of references to qualifying
2626 template classes that appeared in the name of the function. For
2627 example, in
2629 template <class T> struct S { void f(); };
2630 void S<int>::f();
2632 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2633 classes are not counted in the TEMPLATE_COUNT, so that in
2635 template <class T> struct S {};
2636 template <> struct S<int> { void f(); }
2637 template <> void S<int>::f();
2639 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2640 invalid; there should be no template <>.)
2642 If the function is a specialization, it is marked as such via
2643 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2644 is set up correctly, and it is added to the list of specializations
2645 for that template. */
2647 tree
2648 check_explicit_specialization (tree declarator,
2649 tree decl,
2650 int template_count,
2651 int flags)
2653 int have_def = flags & 2;
2654 int is_friend = flags & 4;
2655 bool is_concept = flags & 8;
2656 int specialization = 0;
2657 int explicit_instantiation = 0;
2658 int member_specialization = 0;
2659 tree ctype = DECL_CLASS_CONTEXT (decl);
2660 tree dname = DECL_NAME (decl);
2661 tmpl_spec_kind tsk;
2663 if (is_friend)
2665 if (!processing_specialization)
2666 tsk = tsk_none;
2667 else
2668 tsk = tsk_excessive_parms;
2670 else
2671 tsk = current_tmpl_spec_kind (template_count);
2673 switch (tsk)
2675 case tsk_none:
2676 if (processing_specialization && !VAR_P (decl))
2678 specialization = 1;
2679 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2681 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2683 if (is_friend)
2684 /* This could be something like:
2686 template <class T> void f(T);
2687 class S { friend void f<>(int); } */
2688 specialization = 1;
2689 else
2691 /* This case handles bogus declarations like template <>
2692 template <class T> void f<int>(); */
2694 error ("template-id %qD in declaration of primary template",
2695 declarator);
2696 return decl;
2699 break;
2701 case tsk_invalid_member_spec:
2702 /* The error has already been reported in
2703 check_specialization_scope. */
2704 return error_mark_node;
2706 case tsk_invalid_expl_inst:
2707 error ("template parameter list used in explicit instantiation");
2709 /* Fall through. */
2711 case tsk_expl_inst:
2712 if (have_def)
2713 error ("definition provided for explicit instantiation");
2715 explicit_instantiation = 1;
2716 break;
2718 case tsk_excessive_parms:
2719 case tsk_insufficient_parms:
2720 if (tsk == tsk_excessive_parms)
2721 error ("too many template parameter lists in declaration of %qD",
2722 decl);
2723 else if (template_header_count)
2724 error("too few template parameter lists in declaration of %qD", decl);
2725 else
2726 error("explicit specialization of %qD must be introduced by "
2727 "%<template <>%>", decl);
2729 /* Fall through. */
2730 case tsk_expl_spec:
2731 if (is_concept)
2732 error ("explicit specialization declared %<concept%>");
2734 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2735 /* In cases like template<> constexpr bool v = true;
2736 We'll give an error in check_template_variable. */
2737 break;
2739 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2740 if (ctype)
2741 member_specialization = 1;
2742 else
2743 specialization = 1;
2744 break;
2746 case tsk_template:
2747 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2749 /* This case handles bogus declarations like template <>
2750 template <class T> void f<int>(); */
2752 if (!uses_template_parms (declarator))
2753 error ("template-id %qD in declaration of primary template",
2754 declarator);
2755 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2757 /* Partial specialization of variable template. */
2758 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2759 specialization = 1;
2760 goto ok;
2762 else if (cxx_dialect < cxx14)
2763 error ("non-type partial specialization %qD "
2764 "is not allowed", declarator);
2765 else
2766 error ("non-class, non-variable partial specialization %qD "
2767 "is not allowed", declarator);
2768 return decl;
2769 ok:;
2772 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2773 /* This is a specialization of a member template, without
2774 specialization the containing class. Something like:
2776 template <class T> struct S {
2777 template <class U> void f (U);
2779 template <> template <class U> void S<int>::f(U) {}
2781 That's a specialization -- but of the entire template. */
2782 specialization = 1;
2783 break;
2785 default:
2786 gcc_unreachable ();
2789 if ((specialization || member_specialization)
2790 /* This doesn't apply to variable templates. */
2791 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2792 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2794 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2795 for (; t; t = TREE_CHAIN (t))
2796 if (TREE_PURPOSE (t))
2798 permerror (input_location,
2799 "default argument specified in explicit specialization");
2800 break;
2804 if (specialization || member_specialization || explicit_instantiation)
2806 tree tmpl = NULL_TREE;
2807 tree targs = NULL_TREE;
2808 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2810 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2811 if (!was_template_id)
2813 tree fns;
2815 gcc_assert (identifier_p (declarator));
2816 if (ctype)
2817 fns = dname;
2818 else
2820 /* If there is no class context, the explicit instantiation
2821 must be at namespace scope. */
2822 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2824 /* Find the namespace binding, using the declaration
2825 context. */
2826 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2827 false, true);
2828 if (fns == error_mark_node)
2829 /* If lookup fails, look for a friend declaration so we can
2830 give a better diagnostic. */
2831 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2832 /*type*/false, /*complain*/true,
2833 /*hidden*/true);
2835 if (fns == error_mark_node || !is_overloaded_fn (fns))
2837 error ("%qD is not a template function", dname);
2838 fns = error_mark_node;
2842 declarator = lookup_template_function (fns, NULL_TREE);
2845 if (declarator == error_mark_node)
2846 return error_mark_node;
2848 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2850 if (!explicit_instantiation)
2851 /* A specialization in class scope. This is invalid,
2852 but the error will already have been flagged by
2853 check_specialization_scope. */
2854 return error_mark_node;
2855 else
2857 /* It's not valid to write an explicit instantiation in
2858 class scope, e.g.:
2860 class C { template void f(); }
2862 This case is caught by the parser. However, on
2863 something like:
2865 template class C { void f(); };
2867 (which is invalid) we can get here. The error will be
2868 issued later. */
2872 return decl;
2874 else if (ctype != NULL_TREE
2875 && (identifier_p (TREE_OPERAND (declarator, 0))))
2877 // We'll match variable templates in start_decl.
2878 if (VAR_P (decl))
2879 return decl;
2881 /* Find the list of functions in ctype that have the same
2882 name as the declared function. */
2883 tree name = TREE_OPERAND (declarator, 0);
2885 if (constructor_name_p (name, ctype))
2887 if (DECL_CONSTRUCTOR_P (decl)
2888 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2889 : !CLASSTYPE_DESTRUCTOR (ctype))
2891 /* From [temp.expl.spec]:
2893 If such an explicit specialization for the member
2894 of a class template names an implicitly-declared
2895 special member function (clause _special_), the
2896 program is ill-formed.
2898 Similar language is found in [temp.explicit]. */
2899 error ("specialization of implicitly-declared special member function");
2900 return error_mark_node;
2903 name = DECL_NAME (decl);
2906 /* For a type-conversion operator, We might be looking for
2907 `operator int' which will be a specialization of
2908 `operator T'. Grab all the conversion operators, and
2909 then select from them. */
2910 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
2911 ? conv_op_identifier : name);
2913 if (fns == NULL_TREE)
2915 error ("no member function %qD declared in %qT", name, ctype);
2916 return error_mark_node;
2918 else
2919 TREE_OPERAND (declarator, 0) = fns;
2922 /* Figure out what exactly is being specialized at this point.
2923 Note that for an explicit instantiation, even one for a
2924 member function, we cannot tell a priori whether the
2925 instantiation is for a member template, or just a member
2926 function of a template class. Even if a member template is
2927 being instantiated, the member template arguments may be
2928 elided if they can be deduced from the rest of the
2929 declaration. */
2930 tmpl = determine_specialization (declarator, decl,
2931 &targs,
2932 member_specialization,
2933 template_count,
2934 tsk);
2936 if (!tmpl || tmpl == error_mark_node)
2937 /* We couldn't figure out what this declaration was
2938 specializing. */
2939 return error_mark_node;
2940 else
2942 if (TREE_CODE (decl) == FUNCTION_DECL
2943 && DECL_HIDDEN_FRIEND_P (tmpl))
2945 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2946 "friend declaration %qD is not visible to "
2947 "explicit specialization", tmpl))
2948 inform (DECL_SOURCE_LOCATION (tmpl),
2949 "friend declaration here");
2951 else if (!ctype && !is_friend
2952 && CP_DECL_CONTEXT (decl) == current_namespace)
2953 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2955 tree gen_tmpl = most_general_template (tmpl);
2957 if (explicit_instantiation)
2959 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2960 is done by do_decl_instantiation later. */
2962 int arg_depth = TMPL_ARGS_DEPTH (targs);
2963 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2965 if (arg_depth > parm_depth)
2967 /* If TMPL is not the most general template (for
2968 example, if TMPL is a friend template that is
2969 injected into namespace scope), then there will
2970 be too many levels of TARGS. Remove some of them
2971 here. */
2972 int i;
2973 tree new_targs;
2975 new_targs = make_tree_vec (parm_depth);
2976 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2977 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2978 = TREE_VEC_ELT (targs, i);
2979 targs = new_targs;
2982 return instantiate_template (tmpl, targs, tf_error);
2985 /* If we thought that the DECL was a member function, but it
2986 turns out to be specializing a static member function,
2987 make DECL a static member function as well. */
2988 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2989 && DECL_STATIC_FUNCTION_P (tmpl)
2990 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2991 revert_static_member_fn (decl);
2993 /* If this is a specialization of a member template of a
2994 template class, we want to return the TEMPLATE_DECL, not
2995 the specialization of it. */
2996 if (tsk == tsk_template && !was_template_id)
2998 tree result = DECL_TEMPLATE_RESULT (tmpl);
2999 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3000 DECL_INITIAL (result) = NULL_TREE;
3001 if (have_def)
3003 tree parm;
3004 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3005 DECL_SOURCE_LOCATION (result)
3006 = DECL_SOURCE_LOCATION (decl);
3007 /* We want to use the argument list specified in the
3008 definition, not in the original declaration. */
3009 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3010 for (parm = DECL_ARGUMENTS (result); parm;
3011 parm = DECL_CHAIN (parm))
3012 DECL_CONTEXT (parm) = result;
3014 return register_specialization (tmpl, gen_tmpl, targs,
3015 is_friend, 0);
3018 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3019 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3021 if (was_template_id)
3022 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3024 /* Inherit default function arguments from the template
3025 DECL is specializing. */
3026 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3027 copy_default_args_to_explicit_spec (decl);
3029 /* This specialization has the same protection as the
3030 template it specializes. */
3031 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3032 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3034 /* 7.1.1-1 [dcl.stc]
3036 A storage-class-specifier shall not be specified in an
3037 explicit specialization...
3039 The parser rejects these, so unless action is taken here,
3040 explicit function specializations will always appear with
3041 global linkage.
3043 The action recommended by the C++ CWG in response to C++
3044 defect report 605 is to make the storage class and linkage
3045 of the explicit specialization match the templated function:
3047 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3049 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3051 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3052 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3054 /* A concept cannot be specialized. */
3055 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3057 error ("explicit specialization of function concept %qD",
3058 gen_tmpl);
3059 return error_mark_node;
3062 /* This specialization has the same linkage and visibility as
3063 the function template it specializes. */
3064 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3065 if (! TREE_PUBLIC (decl))
3067 DECL_INTERFACE_KNOWN (decl) = 1;
3068 DECL_NOT_REALLY_EXTERN (decl) = 1;
3070 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3071 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3073 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3074 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3078 /* If DECL is a friend declaration, declared using an
3079 unqualified name, the namespace associated with DECL may
3080 have been set incorrectly. For example, in:
3082 template <typename T> void f(T);
3083 namespace N {
3084 struct S { friend void f<int>(int); }
3087 we will have set the DECL_CONTEXT for the friend
3088 declaration to N, rather than to the global namespace. */
3089 if (DECL_NAMESPACE_SCOPE_P (decl))
3090 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3092 if (is_friend && !have_def)
3093 /* This is not really a declaration of a specialization.
3094 It's just the name of an instantiation. But, it's not
3095 a request for an instantiation, either. */
3096 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3097 else if (TREE_CODE (decl) == FUNCTION_DECL)
3098 /* A specialization is not necessarily COMDAT. */
3099 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3100 && DECL_DECLARED_INLINE_P (decl));
3101 else if (VAR_P (decl))
3102 DECL_COMDAT (decl) = false;
3104 /* If this is a full specialization, register it so that we can find
3105 it again. Partial specializations will be registered in
3106 process_partial_specialization. */
3107 if (!processing_template_decl)
3108 decl = register_specialization (decl, gen_tmpl, targs,
3109 is_friend, 0);
3111 /* A 'structor should already have clones. */
3112 gcc_assert (decl == error_mark_node
3113 || variable_template_p (tmpl)
3114 || !(DECL_CONSTRUCTOR_P (decl)
3115 || DECL_DESTRUCTOR_P (decl))
3116 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3120 return decl;
3123 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3124 parameters. These are represented in the same format used for
3125 DECL_TEMPLATE_PARMS. */
3128 comp_template_parms (const_tree parms1, const_tree parms2)
3130 const_tree p1;
3131 const_tree p2;
3133 if (parms1 == parms2)
3134 return 1;
3136 for (p1 = parms1, p2 = parms2;
3137 p1 != NULL_TREE && p2 != NULL_TREE;
3138 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3140 tree t1 = TREE_VALUE (p1);
3141 tree t2 = TREE_VALUE (p2);
3142 int i;
3144 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3145 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3147 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3148 return 0;
3150 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3152 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3153 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3155 /* If either of the template parameters are invalid, assume
3156 they match for the sake of error recovery. */
3157 if (error_operand_p (parm1) || error_operand_p (parm2))
3158 return 1;
3160 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3161 return 0;
3163 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3164 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3165 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3166 continue;
3167 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3168 return 0;
3172 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3173 /* One set of parameters has more parameters lists than the
3174 other. */
3175 return 0;
3177 return 1;
3180 /* Determine whether PARM is a parameter pack. */
3182 bool
3183 template_parameter_pack_p (const_tree parm)
3185 /* Determine if we have a non-type template parameter pack. */
3186 if (TREE_CODE (parm) == PARM_DECL)
3187 return (DECL_TEMPLATE_PARM_P (parm)
3188 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3189 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3190 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3192 /* If this is a list of template parameters, we could get a
3193 TYPE_DECL or a TEMPLATE_DECL. */
3194 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3195 parm = TREE_TYPE (parm);
3197 /* Otherwise it must be a type template parameter. */
3198 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3199 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3200 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3203 /* Determine if T is a function parameter pack. */
3205 bool
3206 function_parameter_pack_p (const_tree t)
3208 if (t && TREE_CODE (t) == PARM_DECL)
3209 return DECL_PACK_P (t);
3210 return false;
3213 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3214 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3216 tree
3217 get_function_template_decl (const_tree primary_func_tmpl_inst)
3219 if (! primary_func_tmpl_inst
3220 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3221 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3222 return NULL;
3224 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3227 /* Return true iff the function parameter PARAM_DECL was expanded
3228 from the function parameter pack PACK. */
3230 bool
3231 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3233 if (DECL_ARTIFICIAL (param_decl)
3234 || !function_parameter_pack_p (pack))
3235 return false;
3237 /* The parameter pack and its pack arguments have the same
3238 DECL_PARM_INDEX. */
3239 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3242 /* Determine whether ARGS describes a variadic template args list,
3243 i.e., one that is terminated by a template argument pack. */
3245 static bool
3246 template_args_variadic_p (tree args)
3248 int nargs;
3249 tree last_parm;
3251 if (args == NULL_TREE)
3252 return false;
3254 args = INNERMOST_TEMPLATE_ARGS (args);
3255 nargs = TREE_VEC_LENGTH (args);
3257 if (nargs == 0)
3258 return false;
3260 last_parm = TREE_VEC_ELT (args, nargs - 1);
3262 return ARGUMENT_PACK_P (last_parm);
3265 /* Generate a new name for the parameter pack name NAME (an
3266 IDENTIFIER_NODE) that incorporates its */
3268 static tree
3269 make_ith_pack_parameter_name (tree name, int i)
3271 /* Munge the name to include the parameter index. */
3272 #define NUMBUF_LEN 128
3273 char numbuf[NUMBUF_LEN];
3274 char* newname;
3275 int newname_len;
3277 if (name == NULL_TREE)
3278 return name;
3279 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3280 newname_len = IDENTIFIER_LENGTH (name)
3281 + strlen (numbuf) + 2;
3282 newname = (char*)alloca (newname_len);
3283 snprintf (newname, newname_len,
3284 "%s#%i", IDENTIFIER_POINTER (name), i);
3285 return get_identifier (newname);
3288 /* Return true if T is a primary function, class or alias template
3289 instantiation. */
3291 bool
3292 primary_template_instantiation_p (const_tree t)
3294 if (!t)
3295 return false;
3297 if (TREE_CODE (t) == FUNCTION_DECL)
3298 return DECL_LANG_SPECIFIC (t)
3299 && DECL_TEMPLATE_INSTANTIATION (t)
3300 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3301 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3302 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3303 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3304 else if (alias_template_specialization_p (t))
3305 return true;
3306 return false;
3309 /* Return true if PARM is a template template parameter. */
3311 bool
3312 template_template_parameter_p (const_tree parm)
3314 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3317 /* Return true iff PARM is a DECL representing a type template
3318 parameter. */
3320 bool
3321 template_type_parameter_p (const_tree parm)
3323 return (parm
3324 && (TREE_CODE (parm) == TYPE_DECL
3325 || TREE_CODE (parm) == TEMPLATE_DECL)
3326 && DECL_TEMPLATE_PARM_P (parm));
3329 /* Return the template parameters of T if T is a
3330 primary template instantiation, NULL otherwise. */
3332 tree
3333 get_primary_template_innermost_parameters (const_tree t)
3335 tree parms = NULL, template_info = NULL;
3337 if ((template_info = get_template_info (t))
3338 && primary_template_instantiation_p (t))
3339 parms = INNERMOST_TEMPLATE_PARMS
3340 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3342 return parms;
3345 /* Return the template parameters of the LEVELth level from the full list
3346 of template parameters PARMS. */
3348 tree
3349 get_template_parms_at_level (tree parms, int level)
3351 tree p;
3352 if (!parms
3353 || TREE_CODE (parms) != TREE_LIST
3354 || level > TMPL_PARMS_DEPTH (parms))
3355 return NULL_TREE;
3357 for (p = parms; p; p = TREE_CHAIN (p))
3358 if (TMPL_PARMS_DEPTH (p) == level)
3359 return p;
3361 return NULL_TREE;
3364 /* Returns the template arguments of T if T is a template instantiation,
3365 NULL otherwise. */
3367 tree
3368 get_template_innermost_arguments (const_tree t)
3370 tree args = NULL, template_info = NULL;
3372 if ((template_info = get_template_info (t))
3373 && TI_ARGS (template_info))
3374 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3376 return args;
3379 /* Return the argument pack elements of T if T is a template argument pack,
3380 NULL otherwise. */
3382 tree
3383 get_template_argument_pack_elems (const_tree t)
3385 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3386 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3387 return NULL;
3389 return ARGUMENT_PACK_ARGS (t);
3392 /* True iff FN is a function representing a built-in variadic parameter
3393 pack. */
3395 bool
3396 builtin_pack_fn_p (tree fn)
3398 if (!fn
3399 || TREE_CODE (fn) != FUNCTION_DECL
3400 || !DECL_IS_BUILTIN (fn))
3401 return false;
3403 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3404 return true;
3406 return false;
3409 /* True iff CALL is a call to a function representing a built-in variadic
3410 parameter pack. */
3412 static bool
3413 builtin_pack_call_p (tree call)
3415 if (TREE_CODE (call) != CALL_EXPR)
3416 return false;
3417 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3420 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3422 static tree
3423 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3424 tree in_decl)
3426 tree ohi = CALL_EXPR_ARG (call, 0);
3427 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3428 false/*fn*/, true/*int_cst*/);
3430 if (value_dependent_expression_p (hi))
3432 if (hi != ohi)
3434 call = copy_node (call);
3435 CALL_EXPR_ARG (call, 0) = hi;
3437 tree ex = make_pack_expansion (call);
3438 tree vec = make_tree_vec (1);
3439 TREE_VEC_ELT (vec, 0) = ex;
3440 return vec;
3442 else
3444 hi = cxx_constant_value (hi);
3445 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3447 /* Calculate the largest value of len that won't make the size of the vec
3448 overflow an int. The compiler will exceed resource limits long before
3449 this, but it seems a decent place to diagnose. */
3450 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3452 if (len < 0 || len > max)
3454 if ((complain & tf_error)
3455 && hi != error_mark_node)
3456 error ("argument to __integer_pack must be between 0 and %d", max);
3457 return error_mark_node;
3460 tree vec = make_tree_vec (len);
3462 for (int i = 0; i < len; ++i)
3463 TREE_VEC_ELT (vec, i) = size_int (i);
3465 return vec;
3469 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3470 CALL. */
3472 static tree
3473 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3474 tree in_decl)
3476 if (!builtin_pack_call_p (call))
3477 return NULL_TREE;
3479 tree fn = CALL_EXPR_FN (call);
3481 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3482 return expand_integer_pack (call, args, complain, in_decl);
3484 return NULL_TREE;
3487 /* Structure used to track the progress of find_parameter_packs_r. */
3488 struct find_parameter_pack_data
3490 /* TREE_LIST that will contain all of the parameter packs found by
3491 the traversal. */
3492 tree* parameter_packs;
3494 /* Set of AST nodes that have been visited by the traversal. */
3495 hash_set<tree> *visited;
3497 /* True iff we're making a type pack expansion. */
3498 bool type_pack_expansion_p;
3501 /* Identifies all of the argument packs that occur in a template
3502 argument and appends them to the TREE_LIST inside DATA, which is a
3503 find_parameter_pack_data structure. This is a subroutine of
3504 make_pack_expansion and uses_parameter_packs. */
3505 static tree
3506 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3508 tree t = *tp;
3509 struct find_parameter_pack_data* ppd =
3510 (struct find_parameter_pack_data*)data;
3511 bool parameter_pack_p = false;
3513 /* Handle type aliases/typedefs. */
3514 if (TYPE_ALIAS_P (t))
3516 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3517 cp_walk_tree (&TI_ARGS (tinfo),
3518 &find_parameter_packs_r,
3519 ppd, ppd->visited);
3520 *walk_subtrees = 0;
3521 return NULL_TREE;
3524 /* Identify whether this is a parameter pack or not. */
3525 switch (TREE_CODE (t))
3527 case TEMPLATE_PARM_INDEX:
3528 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3529 parameter_pack_p = true;
3530 break;
3532 case TEMPLATE_TYPE_PARM:
3533 t = TYPE_MAIN_VARIANT (t);
3534 /* FALLTHRU */
3535 case TEMPLATE_TEMPLATE_PARM:
3536 /* If the placeholder appears in the decl-specifier-seq of a function
3537 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3538 is a pack expansion, the invented template parameter is a template
3539 parameter pack. */
3540 if (ppd->type_pack_expansion_p && is_auto (t))
3541 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3542 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3543 parameter_pack_p = true;
3544 break;
3546 case FIELD_DECL:
3547 case PARM_DECL:
3548 if (DECL_PACK_P (t))
3550 /* We don't want to walk into the type of a PARM_DECL,
3551 because we don't want to see the type parameter pack. */
3552 *walk_subtrees = 0;
3553 parameter_pack_p = true;
3555 break;
3557 /* Look through a lambda capture proxy to the field pack. */
3558 case VAR_DECL:
3559 if (DECL_HAS_VALUE_EXPR_P (t))
3561 tree v = DECL_VALUE_EXPR (t);
3562 cp_walk_tree (&v,
3563 &find_parameter_packs_r,
3564 ppd, ppd->visited);
3565 *walk_subtrees = 0;
3567 else if (variable_template_specialization_p (t))
3569 cp_walk_tree (&DECL_TI_ARGS (t),
3570 find_parameter_packs_r,
3571 ppd, ppd->visited);
3572 *walk_subtrees = 0;
3574 break;
3576 case CALL_EXPR:
3577 if (builtin_pack_call_p (t))
3578 parameter_pack_p = true;
3579 break;
3581 case BASES:
3582 parameter_pack_p = true;
3583 break;
3584 default:
3585 /* Not a parameter pack. */
3586 break;
3589 if (parameter_pack_p)
3591 /* Add this parameter pack to the list. */
3592 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3595 if (TYPE_P (t))
3596 cp_walk_tree (&TYPE_CONTEXT (t),
3597 &find_parameter_packs_r, ppd, ppd->visited);
3599 /* This switch statement will return immediately if we don't find a
3600 parameter pack. */
3601 switch (TREE_CODE (t))
3603 case TEMPLATE_PARM_INDEX:
3604 return NULL_TREE;
3606 case BOUND_TEMPLATE_TEMPLATE_PARM:
3607 /* Check the template itself. */
3608 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3609 &find_parameter_packs_r, ppd, ppd->visited);
3610 /* Check the template arguments. */
3611 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3612 ppd->visited);
3613 *walk_subtrees = 0;
3614 return NULL_TREE;
3616 case TEMPLATE_TYPE_PARM:
3617 case TEMPLATE_TEMPLATE_PARM:
3618 return NULL_TREE;
3620 case PARM_DECL:
3621 return NULL_TREE;
3623 case DECL_EXPR:
3624 /* Ignore the declaration of a capture proxy for a parameter pack. */
3625 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3626 *walk_subtrees = 0;
3627 return NULL_TREE;
3629 case RECORD_TYPE:
3630 if (TYPE_PTRMEMFUNC_P (t))
3631 return NULL_TREE;
3632 /* Fall through. */
3634 case UNION_TYPE:
3635 case ENUMERAL_TYPE:
3636 if (TYPE_TEMPLATE_INFO (t))
3637 cp_walk_tree (&TYPE_TI_ARGS (t),
3638 &find_parameter_packs_r, ppd, ppd->visited);
3640 *walk_subtrees = 0;
3641 return NULL_TREE;
3643 case TEMPLATE_DECL:
3644 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3645 return NULL_TREE;
3646 gcc_fallthrough();
3648 case CONSTRUCTOR:
3649 cp_walk_tree (&TREE_TYPE (t),
3650 &find_parameter_packs_r, ppd, ppd->visited);
3651 return NULL_TREE;
3653 case TYPENAME_TYPE:
3654 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3655 ppd, ppd->visited);
3656 *walk_subtrees = 0;
3657 return NULL_TREE;
3659 case TYPE_PACK_EXPANSION:
3660 case EXPR_PACK_EXPANSION:
3661 *walk_subtrees = 0;
3662 return NULL_TREE;
3664 case INTEGER_TYPE:
3665 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3666 ppd, ppd->visited);
3667 *walk_subtrees = 0;
3668 return NULL_TREE;
3670 case IDENTIFIER_NODE:
3671 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3672 ppd->visited);
3673 *walk_subtrees = 0;
3674 return NULL_TREE;
3676 case LAMBDA_EXPR:
3678 tree fn = lambda_function (t);
3679 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3680 ppd->visited);
3681 *walk_subtrees = 0;
3682 return NULL_TREE;
3685 case DECLTYPE_TYPE:
3687 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3688 type_pack_expansion_p to false so that any placeholders
3689 within the expression don't get marked as parameter packs. */
3690 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3691 ppd->type_pack_expansion_p = false;
3692 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3693 ppd, ppd->visited);
3694 ppd->type_pack_expansion_p = type_pack_expansion_p;
3695 *walk_subtrees = 0;
3696 return NULL_TREE;
3699 default:
3700 return NULL_TREE;
3703 return NULL_TREE;
3706 /* Determines if the expression or type T uses any parameter packs. */
3707 bool
3708 uses_parameter_packs (tree t)
3710 tree parameter_packs = NULL_TREE;
3711 struct find_parameter_pack_data ppd;
3712 ppd.parameter_packs = &parameter_packs;
3713 ppd.visited = new hash_set<tree>;
3714 ppd.type_pack_expansion_p = false;
3715 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3716 delete ppd.visited;
3717 return parameter_packs != NULL_TREE;
3720 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3721 representation a base-class initializer into a parameter pack
3722 expansion. If all goes well, the resulting node will be an
3723 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3724 respectively. */
3725 tree
3726 make_pack_expansion (tree arg)
3728 tree result;
3729 tree parameter_packs = NULL_TREE;
3730 bool for_types = false;
3731 struct find_parameter_pack_data ppd;
3733 if (!arg || arg == error_mark_node)
3734 return arg;
3736 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3738 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3739 class initializer. In this case, the TREE_PURPOSE will be a
3740 _TYPE node (representing the base class expansion we're
3741 initializing) and the TREE_VALUE will be a TREE_LIST
3742 containing the initialization arguments.
3744 The resulting expansion looks somewhat different from most
3745 expansions. Rather than returning just one _EXPANSION, we
3746 return a TREE_LIST whose TREE_PURPOSE is a
3747 TYPE_PACK_EXPANSION containing the bases that will be
3748 initialized. The TREE_VALUE will be identical to the
3749 original TREE_VALUE, which is a list of arguments that will
3750 be passed to each base. We do not introduce any new pack
3751 expansion nodes into the TREE_VALUE (although it is possible
3752 that some already exist), because the TREE_PURPOSE and
3753 TREE_VALUE all need to be expanded together with the same
3754 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3755 resulting TREE_PURPOSE will mention the parameter packs in
3756 both the bases and the arguments to the bases. */
3757 tree purpose;
3758 tree value;
3759 tree parameter_packs = NULL_TREE;
3761 /* Determine which parameter packs will be used by the base
3762 class expansion. */
3763 ppd.visited = new hash_set<tree>;
3764 ppd.parameter_packs = &parameter_packs;
3765 ppd.type_pack_expansion_p = true;
3766 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3767 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3768 &ppd, ppd.visited);
3770 if (parameter_packs == NULL_TREE)
3772 error ("base initializer expansion %qT contains no parameter packs", arg);
3773 delete ppd.visited;
3774 return error_mark_node;
3777 if (TREE_VALUE (arg) != void_type_node)
3779 /* Collect the sets of parameter packs used in each of the
3780 initialization arguments. */
3781 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3783 /* Determine which parameter packs will be expanded in this
3784 argument. */
3785 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3786 &ppd, ppd.visited);
3790 delete ppd.visited;
3792 /* Create the pack expansion type for the base type. */
3793 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3794 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3795 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3796 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3798 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3799 they will rarely be compared to anything. */
3800 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3802 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3805 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3806 for_types = true;
3808 /* Build the PACK_EXPANSION_* node. */
3809 result = for_types
3810 ? cxx_make_type (TYPE_PACK_EXPANSION)
3811 : make_node (EXPR_PACK_EXPANSION);
3812 SET_PACK_EXPANSION_PATTERN (result, arg);
3813 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3815 /* Propagate type and const-expression information. */
3816 TREE_TYPE (result) = TREE_TYPE (arg);
3817 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3818 /* Mark this read now, since the expansion might be length 0. */
3819 mark_exp_read (arg);
3821 else
3822 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3823 they will rarely be compared to anything. */
3824 SET_TYPE_STRUCTURAL_EQUALITY (result);
3826 /* Determine which parameter packs will be expanded. */
3827 ppd.parameter_packs = &parameter_packs;
3828 ppd.visited = new hash_set<tree>;
3829 ppd.type_pack_expansion_p = TYPE_P (arg);
3830 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3831 delete ppd.visited;
3833 /* Make sure we found some parameter packs. */
3834 if (parameter_packs == NULL_TREE)
3836 if (TYPE_P (arg))
3837 error ("expansion pattern %qT contains no argument packs", arg);
3838 else
3839 error ("expansion pattern %qE contains no argument packs", arg);
3840 return error_mark_node;
3842 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3844 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3846 return result;
3849 /* Checks T for any "bare" parameter packs, which have not yet been
3850 expanded, and issues an error if any are found. This operation can
3851 only be done on full expressions or types (e.g., an expression
3852 statement, "if" condition, etc.), because we could have expressions like:
3854 foo(f(g(h(args)))...)
3856 where "args" is a parameter pack. check_for_bare_parameter_packs
3857 should not be called for the subexpressions args, h(args),
3858 g(h(args)), or f(g(h(args))), because we would produce erroneous
3859 error messages.
3861 Returns TRUE and emits an error if there were bare parameter packs,
3862 returns FALSE otherwise. */
3863 bool
3864 check_for_bare_parameter_packs (tree t)
3866 tree parameter_packs = NULL_TREE;
3867 struct find_parameter_pack_data ppd;
3869 if (!processing_template_decl || !t || t == error_mark_node)
3870 return false;
3872 /* A lambda might use a parameter pack from the containing context. */
3873 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3874 return false;
3876 if (TREE_CODE (t) == TYPE_DECL)
3877 t = TREE_TYPE (t);
3879 ppd.parameter_packs = &parameter_packs;
3880 ppd.visited = new hash_set<tree>;
3881 ppd.type_pack_expansion_p = false;
3882 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3883 delete ppd.visited;
3885 if (parameter_packs)
3887 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3888 error_at (loc, "parameter packs not expanded with %<...%>:");
3889 while (parameter_packs)
3891 tree pack = TREE_VALUE (parameter_packs);
3892 tree name = NULL_TREE;
3894 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3895 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3896 name = TYPE_NAME (pack);
3897 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3898 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3899 else if (TREE_CODE (pack) == CALL_EXPR)
3900 name = DECL_NAME (CALL_EXPR_FN (pack));
3901 else
3902 name = DECL_NAME (pack);
3904 if (name)
3905 inform (loc, " %qD", name);
3906 else
3907 inform (loc, " <anonymous>");
3909 parameter_packs = TREE_CHAIN (parameter_packs);
3912 return true;
3915 return false;
3918 /* Expand any parameter packs that occur in the template arguments in
3919 ARGS. */
3920 tree
3921 expand_template_argument_pack (tree args)
3923 if (args == error_mark_node)
3924 return error_mark_node;
3926 tree result_args = NULL_TREE;
3927 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3928 int num_result_args = -1;
3929 int non_default_args_count = -1;
3931 /* First, determine if we need to expand anything, and the number of
3932 slots we'll need. */
3933 for (in_arg = 0; in_arg < nargs; ++in_arg)
3935 tree arg = TREE_VEC_ELT (args, in_arg);
3936 if (arg == NULL_TREE)
3937 return args;
3938 if (ARGUMENT_PACK_P (arg))
3940 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3941 if (num_result_args < 0)
3942 num_result_args = in_arg + num_packed;
3943 else
3944 num_result_args += num_packed;
3946 else
3948 if (num_result_args >= 0)
3949 num_result_args++;
3953 /* If no expansion is necessary, we're done. */
3954 if (num_result_args < 0)
3955 return args;
3957 /* Expand arguments. */
3958 result_args = make_tree_vec (num_result_args);
3959 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3960 non_default_args_count =
3961 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3962 for (in_arg = 0; in_arg < nargs; ++in_arg)
3964 tree arg = TREE_VEC_ELT (args, in_arg);
3965 if (ARGUMENT_PACK_P (arg))
3967 tree packed = ARGUMENT_PACK_ARGS (arg);
3968 int i, num_packed = TREE_VEC_LENGTH (packed);
3969 for (i = 0; i < num_packed; ++i, ++out_arg)
3970 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3971 if (non_default_args_count > 0)
3972 non_default_args_count += num_packed - 1;
3974 else
3976 TREE_VEC_ELT (result_args, out_arg) = arg;
3977 ++out_arg;
3980 if (non_default_args_count >= 0)
3981 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3982 return result_args;
3985 /* Checks if DECL shadows a template parameter.
3987 [temp.local]: A template-parameter shall not be redeclared within its
3988 scope (including nested scopes).
3990 Emits an error and returns TRUE if the DECL shadows a parameter,
3991 returns FALSE otherwise. */
3993 bool
3994 check_template_shadow (tree decl)
3996 tree olddecl;
3998 /* If we're not in a template, we can't possibly shadow a template
3999 parameter. */
4000 if (!current_template_parms)
4001 return true;
4003 /* Figure out what we're shadowing. */
4004 decl = OVL_FIRST (decl);
4005 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4007 /* If there's no previous binding for this name, we're not shadowing
4008 anything, let alone a template parameter. */
4009 if (!olddecl)
4010 return true;
4012 /* If we're not shadowing a template parameter, we're done. Note
4013 that OLDDECL might be an OVERLOAD (or perhaps even an
4014 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4015 node. */
4016 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4017 return true;
4019 /* We check for decl != olddecl to avoid bogus errors for using a
4020 name inside a class. We check TPFI to avoid duplicate errors for
4021 inline member templates. */
4022 if (decl == olddecl
4023 || (DECL_TEMPLATE_PARM_P (decl)
4024 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4025 return true;
4027 /* Don't complain about the injected class name, as we've already
4028 complained about the class itself. */
4029 if (DECL_SELF_REFERENCE_P (decl))
4030 return false;
4032 if (DECL_TEMPLATE_PARM_P (decl))
4033 error ("declaration of template parameter %q+D shadows "
4034 "template parameter", decl);
4035 else
4036 error ("declaration of %q+#D shadows template parameter", decl);
4037 inform (DECL_SOURCE_LOCATION (olddecl),
4038 "template parameter %qD declared here", olddecl);
4039 return false;
4042 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4043 ORIG_LEVEL, DECL, and TYPE. */
4045 static tree
4046 build_template_parm_index (int index,
4047 int level,
4048 int orig_level,
4049 tree decl,
4050 tree type)
4052 tree t = make_node (TEMPLATE_PARM_INDEX);
4053 TEMPLATE_PARM_IDX (t) = index;
4054 TEMPLATE_PARM_LEVEL (t) = level;
4055 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4056 TEMPLATE_PARM_DECL (t) = decl;
4057 TREE_TYPE (t) = type;
4058 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4059 TREE_READONLY (t) = TREE_READONLY (decl);
4061 return t;
4064 /* Find the canonical type parameter for the given template type
4065 parameter. Returns the canonical type parameter, which may be TYPE
4066 if no such parameter existed. */
4068 static tree
4069 canonical_type_parameter (tree type)
4071 tree list;
4072 int idx = TEMPLATE_TYPE_IDX (type);
4073 if (!canonical_template_parms)
4074 vec_alloc (canonical_template_parms, idx + 1);
4076 if (canonical_template_parms->length () <= (unsigned) idx)
4077 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4079 list = (*canonical_template_parms)[idx];
4080 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4081 list = TREE_CHAIN (list);
4083 if (list)
4084 return TREE_VALUE (list);
4085 else
4087 (*canonical_template_parms)[idx]
4088 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4089 return type;
4093 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4094 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4095 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4096 new one is created. */
4098 static tree
4099 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4100 tsubst_flags_t complain)
4102 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4103 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4104 != TEMPLATE_PARM_LEVEL (index) - levels)
4105 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4107 tree orig_decl = TEMPLATE_PARM_DECL (index);
4108 tree decl, t;
4110 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4111 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4112 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4113 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4114 DECL_ARTIFICIAL (decl) = 1;
4115 SET_DECL_TEMPLATE_PARM_P (decl);
4117 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4118 TEMPLATE_PARM_LEVEL (index) - levels,
4119 TEMPLATE_PARM_ORIG_LEVEL (index),
4120 decl, type);
4121 TEMPLATE_PARM_DESCENDANTS (index) = t;
4122 TEMPLATE_PARM_PARAMETER_PACK (t)
4123 = TEMPLATE_PARM_PARAMETER_PACK (index);
4125 /* Template template parameters need this. */
4126 if (TREE_CODE (decl) == TEMPLATE_DECL)
4128 DECL_TEMPLATE_RESULT (decl)
4129 = build_decl (DECL_SOURCE_LOCATION (decl),
4130 TYPE_DECL, DECL_NAME (decl), type);
4131 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4132 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4133 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4137 return TEMPLATE_PARM_DESCENDANTS (index);
4140 /* Process information from new template parameter PARM and append it
4141 to the LIST being built. This new parameter is a non-type
4142 parameter iff IS_NON_TYPE is true. This new parameter is a
4143 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4144 is in PARM_LOC. */
4146 tree
4147 process_template_parm (tree list, location_t parm_loc, tree parm,
4148 bool is_non_type, bool is_parameter_pack)
4150 tree decl = 0;
4151 int idx = 0;
4153 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4154 tree defval = TREE_PURPOSE (parm);
4155 tree constr = TREE_TYPE (parm);
4157 if (list)
4159 tree p = tree_last (list);
4161 if (p && TREE_VALUE (p) != error_mark_node)
4163 p = TREE_VALUE (p);
4164 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4165 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4166 else
4167 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4170 ++idx;
4173 if (is_non_type)
4175 parm = TREE_VALUE (parm);
4177 SET_DECL_TEMPLATE_PARM_P (parm);
4179 if (TREE_TYPE (parm) != error_mark_node)
4181 /* [temp.param]
4183 The top-level cv-qualifiers on the template-parameter are
4184 ignored when determining its type. */
4185 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4186 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4187 TREE_TYPE (parm) = error_mark_node;
4188 else if (uses_parameter_packs (TREE_TYPE (parm))
4189 && !is_parameter_pack
4190 /* If we're in a nested template parameter list, the template
4191 template parameter could be a parameter pack. */
4192 && processing_template_parmlist == 1)
4194 /* This template parameter is not a parameter pack, but it
4195 should be. Complain about "bare" parameter packs. */
4196 check_for_bare_parameter_packs (TREE_TYPE (parm));
4198 /* Recover by calling this a parameter pack. */
4199 is_parameter_pack = true;
4203 /* A template parameter is not modifiable. */
4204 TREE_CONSTANT (parm) = 1;
4205 TREE_READONLY (parm) = 1;
4206 decl = build_decl (parm_loc,
4207 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4208 TREE_CONSTANT (decl) = 1;
4209 TREE_READONLY (decl) = 1;
4210 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4211 = build_template_parm_index (idx, processing_template_decl,
4212 processing_template_decl,
4213 decl, TREE_TYPE (parm));
4215 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4216 = is_parameter_pack;
4218 else
4220 tree t;
4221 parm = TREE_VALUE (TREE_VALUE (parm));
4223 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4225 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4226 /* This is for distinguishing between real templates and template
4227 template parameters */
4228 TREE_TYPE (parm) = t;
4229 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4230 decl = parm;
4232 else
4234 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4235 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4236 decl = build_decl (parm_loc,
4237 TYPE_DECL, parm, t);
4240 TYPE_NAME (t) = decl;
4241 TYPE_STUB_DECL (t) = decl;
4242 parm = decl;
4243 TEMPLATE_TYPE_PARM_INDEX (t)
4244 = build_template_parm_index (idx, processing_template_decl,
4245 processing_template_decl,
4246 decl, TREE_TYPE (parm));
4247 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4248 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4250 DECL_ARTIFICIAL (decl) = 1;
4251 SET_DECL_TEMPLATE_PARM_P (decl);
4253 /* Build requirements for the type/template parameter.
4254 This must be done after SET_DECL_TEMPLATE_PARM_P or
4255 process_template_parm could fail. */
4256 tree reqs = finish_shorthand_constraint (parm, constr);
4258 pushdecl (decl);
4260 /* Build the parameter node linking the parameter declaration,
4261 its default argument (if any), and its constraints (if any). */
4262 parm = build_tree_list (defval, parm);
4263 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4265 return chainon (list, parm);
4268 /* The end of a template parameter list has been reached. Process the
4269 tree list into a parameter vector, converting each parameter into a more
4270 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4271 as PARM_DECLs. */
4273 tree
4274 end_template_parm_list (tree parms)
4276 int nparms;
4277 tree parm, next;
4278 tree saved_parmlist = make_tree_vec (list_length (parms));
4280 /* Pop the dummy parameter level and add the real one. */
4281 current_template_parms = TREE_CHAIN (current_template_parms);
4283 current_template_parms
4284 = tree_cons (size_int (processing_template_decl),
4285 saved_parmlist, current_template_parms);
4287 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4289 next = TREE_CHAIN (parm);
4290 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4291 TREE_CHAIN (parm) = NULL_TREE;
4294 --processing_template_parmlist;
4296 return saved_parmlist;
4299 // Explicitly indicate the end of the template parameter list. We assume
4300 // that the current template parameters have been constructed and/or
4301 // managed explicitly, as when creating new template template parameters
4302 // from a shorthand constraint.
4303 void
4304 end_template_parm_list ()
4306 --processing_template_parmlist;
4309 /* end_template_decl is called after a template declaration is seen. */
4311 void
4312 end_template_decl (void)
4314 reset_specialization ();
4316 if (! processing_template_decl)
4317 return;
4319 /* This matches the pushlevel in begin_template_parm_list. */
4320 finish_scope ();
4322 --processing_template_decl;
4323 current_template_parms = TREE_CHAIN (current_template_parms);
4326 /* Takes a TREE_LIST representing a template parameter and convert it
4327 into an argument suitable to be passed to the type substitution
4328 functions. Note that If the TREE_LIST contains an error_mark
4329 node, the returned argument is error_mark_node. */
4331 tree
4332 template_parm_to_arg (tree t)
4335 if (t == NULL_TREE
4336 || TREE_CODE (t) != TREE_LIST)
4337 return t;
4339 if (error_operand_p (TREE_VALUE (t)))
4340 return error_mark_node;
4342 t = TREE_VALUE (t);
4344 if (TREE_CODE (t) == TYPE_DECL
4345 || TREE_CODE (t) == TEMPLATE_DECL)
4347 t = TREE_TYPE (t);
4349 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4351 /* Turn this argument into a TYPE_ARGUMENT_PACK
4352 with a single element, which expands T. */
4353 tree vec = make_tree_vec (1);
4354 if (CHECKING_P)
4355 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4357 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4359 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4360 SET_ARGUMENT_PACK_ARGS (t, vec);
4363 else
4365 t = DECL_INITIAL (t);
4367 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4369 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4370 with a single element, which expands T. */
4371 tree vec = make_tree_vec (1);
4372 if (CHECKING_P)
4373 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4375 t = convert_from_reference (t);
4376 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4378 t = make_node (NONTYPE_ARGUMENT_PACK);
4379 SET_ARGUMENT_PACK_ARGS (t, vec);
4381 else
4382 t = convert_from_reference (t);
4384 return t;
4387 /* Given a single level of template parameters (a TREE_VEC), return it
4388 as a set of template arguments. */
4390 static tree
4391 template_parms_level_to_args (tree parms)
4393 tree a = copy_node (parms);
4394 TREE_TYPE (a) = NULL_TREE;
4395 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4396 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4398 if (CHECKING_P)
4399 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4401 return a;
4404 /* Given a set of template parameters, return them as a set of template
4405 arguments. The template parameters are represented as a TREE_VEC, in
4406 the form documented in cp-tree.h for template arguments. */
4408 static tree
4409 template_parms_to_args (tree parms)
4411 tree header;
4412 tree args = NULL_TREE;
4413 int length = TMPL_PARMS_DEPTH (parms);
4414 int l = length;
4416 /* If there is only one level of template parameters, we do not
4417 create a TREE_VEC of TREE_VECs. Instead, we return a single
4418 TREE_VEC containing the arguments. */
4419 if (length > 1)
4420 args = make_tree_vec (length);
4422 for (header = parms; header; header = TREE_CHAIN (header))
4424 tree a = template_parms_level_to_args (TREE_VALUE (header));
4426 if (length > 1)
4427 TREE_VEC_ELT (args, --l) = a;
4428 else
4429 args = a;
4432 return args;
4435 /* Within the declaration of a template, return the currently active
4436 template parameters as an argument TREE_VEC. */
4438 static tree
4439 current_template_args (void)
4441 return template_parms_to_args (current_template_parms);
4444 /* Update the declared TYPE by doing any lookups which were thought to be
4445 dependent, but are not now that we know the SCOPE of the declarator. */
4447 tree
4448 maybe_update_decl_type (tree orig_type, tree scope)
4450 tree type = orig_type;
4452 if (type == NULL_TREE)
4453 return type;
4455 if (TREE_CODE (orig_type) == TYPE_DECL)
4456 type = TREE_TYPE (type);
4458 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4459 && dependent_type_p (type)
4460 /* Don't bother building up the args in this case. */
4461 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4463 /* tsubst in the args corresponding to the template parameters,
4464 including auto if present. Most things will be unchanged, but
4465 make_typename_type and tsubst_qualified_id will resolve
4466 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4467 tree args = current_template_args ();
4468 tree auto_node = type_uses_auto (type);
4469 tree pushed;
4470 if (auto_node)
4472 tree auto_vec = make_tree_vec (1);
4473 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4474 args = add_to_template_args (args, auto_vec);
4476 pushed = push_scope (scope);
4477 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4478 if (pushed)
4479 pop_scope (scope);
4482 if (type == error_mark_node)
4483 return orig_type;
4485 if (TREE_CODE (orig_type) == TYPE_DECL)
4487 if (same_type_p (type, TREE_TYPE (orig_type)))
4488 type = orig_type;
4489 else
4490 type = TYPE_NAME (type);
4492 return type;
4495 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4496 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4497 the new template is a member template. */
4499 tree
4500 build_template_decl (tree decl, tree parms, bool member_template_p)
4502 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4503 DECL_TEMPLATE_PARMS (tmpl) = parms;
4504 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4505 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4506 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4508 return tmpl;
4511 struct template_parm_data
4513 /* The level of the template parameters we are currently
4514 processing. */
4515 int level;
4517 /* The index of the specialization argument we are currently
4518 processing. */
4519 int current_arg;
4521 /* An array whose size is the number of template parameters. The
4522 elements are nonzero if the parameter has been used in any one
4523 of the arguments processed so far. */
4524 int* parms;
4526 /* An array whose size is the number of template arguments. The
4527 elements are nonzero if the argument makes use of template
4528 parameters of this level. */
4529 int* arg_uses_template_parms;
4532 /* Subroutine of push_template_decl used to see if each template
4533 parameter in a partial specialization is used in the explicit
4534 argument list. If T is of the LEVEL given in DATA (which is
4535 treated as a template_parm_data*), then DATA->PARMS is marked
4536 appropriately. */
4538 static int
4539 mark_template_parm (tree t, void* data)
4541 int level;
4542 int idx;
4543 struct template_parm_data* tpd = (struct template_parm_data*) data;
4545 template_parm_level_and_index (t, &level, &idx);
4547 if (level == tpd->level)
4549 tpd->parms[idx] = 1;
4550 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4553 /* In C++17 the type of a non-type argument is a deduced context. */
4554 if (cxx_dialect >= cxx17
4555 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4556 for_each_template_parm (TREE_TYPE (t),
4557 &mark_template_parm,
4558 data,
4559 NULL,
4560 /*include_nondeduced_p=*/false);
4562 /* Return zero so that for_each_template_parm will continue the
4563 traversal of the tree; we want to mark *every* template parm. */
4564 return 0;
4567 /* Process the partial specialization DECL. */
4569 static tree
4570 process_partial_specialization (tree decl)
4572 tree type = TREE_TYPE (decl);
4573 tree tinfo = get_template_info (decl);
4574 tree maintmpl = TI_TEMPLATE (tinfo);
4575 tree specargs = TI_ARGS (tinfo);
4576 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4577 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4578 tree inner_parms;
4579 tree inst;
4580 int nargs = TREE_VEC_LENGTH (inner_args);
4581 int ntparms;
4582 int i;
4583 bool did_error_intro = false;
4584 struct template_parm_data tpd;
4585 struct template_parm_data tpd2;
4587 gcc_assert (current_template_parms);
4589 /* A concept cannot be specialized. */
4590 if (flag_concepts && variable_concept_p (maintmpl))
4592 error ("specialization of variable concept %q#D", maintmpl);
4593 return error_mark_node;
4596 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4597 ntparms = TREE_VEC_LENGTH (inner_parms);
4599 /* We check that each of the template parameters given in the
4600 partial specialization is used in the argument list to the
4601 specialization. For example:
4603 template <class T> struct S;
4604 template <class T> struct S<T*>;
4606 The second declaration is OK because `T*' uses the template
4607 parameter T, whereas
4609 template <class T> struct S<int>;
4611 is no good. Even trickier is:
4613 template <class T>
4614 struct S1
4616 template <class U>
4617 struct S2;
4618 template <class U>
4619 struct S2<T>;
4622 The S2<T> declaration is actually invalid; it is a
4623 full-specialization. Of course,
4625 template <class U>
4626 struct S2<T (*)(U)>;
4628 or some such would have been OK. */
4629 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4630 tpd.parms = XALLOCAVEC (int, ntparms);
4631 memset (tpd.parms, 0, sizeof (int) * ntparms);
4633 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4634 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4635 for (i = 0; i < nargs; ++i)
4637 tpd.current_arg = i;
4638 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4639 &mark_template_parm,
4640 &tpd,
4641 NULL,
4642 /*include_nondeduced_p=*/false);
4644 for (i = 0; i < ntparms; ++i)
4645 if (tpd.parms[i] == 0)
4647 /* One of the template parms was not used in a deduced context in the
4648 specialization. */
4649 if (!did_error_intro)
4651 error ("template parameters not deducible in "
4652 "partial specialization:");
4653 did_error_intro = true;
4656 inform (input_location, " %qD",
4657 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4660 if (did_error_intro)
4661 return error_mark_node;
4663 /* [temp.class.spec]
4665 The argument list of the specialization shall not be identical to
4666 the implicit argument list of the primary template. */
4667 tree main_args
4668 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4669 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4670 && (!flag_concepts
4671 || !strictly_subsumes (current_template_constraints (),
4672 get_constraints (maintmpl))))
4674 if (!flag_concepts)
4675 error ("partial specialization %q+D does not specialize "
4676 "any template arguments", decl);
4677 else
4678 error ("partial specialization %q+D does not specialize any "
4679 "template arguments and is not more constrained than", decl);
4680 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4683 /* A partial specialization that replaces multiple parameters of the
4684 primary template with a pack expansion is less specialized for those
4685 parameters. */
4686 if (nargs < DECL_NTPARMS (maintmpl))
4688 error ("partial specialization is not more specialized than the "
4689 "primary template because it replaces multiple parameters "
4690 "with a pack expansion");
4691 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4692 /* Avoid crash in process_partial_specialization. */
4693 return decl;
4696 /* If we aren't in a dependent class, we can actually try deduction. */
4697 else if (tpd.level == 1
4698 /* FIXME we should be able to handle a partial specialization of a
4699 partial instantiation, but currently we can't (c++/41727). */
4700 && TMPL_ARGS_DEPTH (specargs) == 1
4701 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4703 if (permerror (input_location, "partial specialization %qD is not "
4704 "more specialized than", decl))
4705 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4706 maintmpl);
4709 /* [temp.class.spec]
4711 A partially specialized non-type argument expression shall not
4712 involve template parameters of the partial specialization except
4713 when the argument expression is a simple identifier.
4715 The type of a template parameter corresponding to a specialized
4716 non-type argument shall not be dependent on a parameter of the
4717 specialization.
4719 Also, we verify that pack expansions only occur at the
4720 end of the argument list. */
4721 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4722 tpd2.parms = 0;
4723 for (i = 0; i < nargs; ++i)
4725 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4726 tree arg = TREE_VEC_ELT (inner_args, i);
4727 tree packed_args = NULL_TREE;
4728 int j, len = 1;
4730 if (ARGUMENT_PACK_P (arg))
4732 /* Extract the arguments from the argument pack. We'll be
4733 iterating over these in the following loop. */
4734 packed_args = ARGUMENT_PACK_ARGS (arg);
4735 len = TREE_VEC_LENGTH (packed_args);
4738 for (j = 0; j < len; j++)
4740 if (packed_args)
4741 /* Get the Jth argument in the parameter pack. */
4742 arg = TREE_VEC_ELT (packed_args, j);
4744 if (PACK_EXPANSION_P (arg))
4746 /* Pack expansions must come at the end of the
4747 argument list. */
4748 if ((packed_args && j < len - 1)
4749 || (!packed_args && i < nargs - 1))
4751 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4752 error ("parameter pack argument %qE must be at the "
4753 "end of the template argument list", arg);
4754 else
4755 error ("parameter pack argument %qT must be at the "
4756 "end of the template argument list", arg);
4760 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4761 /* We only care about the pattern. */
4762 arg = PACK_EXPANSION_PATTERN (arg);
4764 if (/* These first two lines are the `non-type' bit. */
4765 !TYPE_P (arg)
4766 && TREE_CODE (arg) != TEMPLATE_DECL
4767 /* This next two lines are the `argument expression is not just a
4768 simple identifier' condition and also the `specialized
4769 non-type argument' bit. */
4770 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4771 && !(REFERENCE_REF_P (arg)
4772 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4774 if ((!packed_args && tpd.arg_uses_template_parms[i])
4775 || (packed_args && uses_template_parms (arg)))
4776 error ("template argument %qE involves template parameter(s)",
4777 arg);
4778 else
4780 /* Look at the corresponding template parameter,
4781 marking which template parameters its type depends
4782 upon. */
4783 tree type = TREE_TYPE (parm);
4785 if (!tpd2.parms)
4787 /* We haven't yet initialized TPD2. Do so now. */
4788 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4789 /* The number of parameters here is the number in the
4790 main template, which, as checked in the assertion
4791 above, is NARGS. */
4792 tpd2.parms = XALLOCAVEC (int, nargs);
4793 tpd2.level =
4794 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4797 /* Mark the template parameters. But this time, we're
4798 looking for the template parameters of the main
4799 template, not in the specialization. */
4800 tpd2.current_arg = i;
4801 tpd2.arg_uses_template_parms[i] = 0;
4802 memset (tpd2.parms, 0, sizeof (int) * nargs);
4803 for_each_template_parm (type,
4804 &mark_template_parm,
4805 &tpd2,
4806 NULL,
4807 /*include_nondeduced_p=*/false);
4809 if (tpd2.arg_uses_template_parms [i])
4811 /* The type depended on some template parameters.
4812 If they are fully specialized in the
4813 specialization, that's OK. */
4814 int j;
4815 int count = 0;
4816 for (j = 0; j < nargs; ++j)
4817 if (tpd2.parms[j] != 0
4818 && tpd.arg_uses_template_parms [j])
4819 ++count;
4820 if (count != 0)
4821 error_n (input_location, count,
4822 "type %qT of template argument %qE depends "
4823 "on a template parameter",
4824 "type %qT of template argument %qE depends "
4825 "on template parameters",
4826 type,
4827 arg);
4834 /* We should only get here once. */
4835 if (TREE_CODE (decl) == TYPE_DECL)
4836 gcc_assert (!COMPLETE_TYPE_P (type));
4838 // Build the template decl.
4839 tree tmpl = build_template_decl (decl, current_template_parms,
4840 DECL_MEMBER_TEMPLATE_P (maintmpl));
4841 TREE_TYPE (tmpl) = type;
4842 DECL_TEMPLATE_RESULT (tmpl) = decl;
4843 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4844 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4845 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4847 /* Give template template parms a DECL_CONTEXT of the template
4848 for which they are a parameter. */
4849 for (i = 0; i < ntparms; ++i)
4851 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4852 if (TREE_CODE (parm) == TEMPLATE_DECL)
4853 DECL_CONTEXT (parm) = tmpl;
4856 if (VAR_P (decl))
4857 /* We didn't register this in check_explicit_specialization so we could
4858 wait until the constraints were set. */
4859 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4860 else
4861 associate_classtype_constraints (type);
4863 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4864 = tree_cons (specargs, tmpl,
4865 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4866 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4868 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4869 inst = TREE_CHAIN (inst))
4871 tree instance = TREE_VALUE (inst);
4872 if (TYPE_P (instance)
4873 ? (COMPLETE_TYPE_P (instance)
4874 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4875 : DECL_TEMPLATE_INSTANTIATION (instance))
4877 tree spec = most_specialized_partial_spec (instance, tf_none);
4878 tree inst_decl = (DECL_P (instance)
4879 ? instance : TYPE_NAME (instance));
4880 if (!spec)
4881 /* OK */;
4882 else if (spec == error_mark_node)
4883 permerror (input_location,
4884 "declaration of %qD ambiguates earlier template "
4885 "instantiation for %qD", decl, inst_decl);
4886 else if (TREE_VALUE (spec) == tmpl)
4887 permerror (input_location,
4888 "partial specialization of %qD after instantiation "
4889 "of %qD", decl, inst_decl);
4893 return decl;
4896 /* PARM is a template parameter of some form; return the corresponding
4897 TEMPLATE_PARM_INDEX. */
4899 static tree
4900 get_template_parm_index (tree parm)
4902 if (TREE_CODE (parm) == PARM_DECL
4903 || TREE_CODE (parm) == CONST_DECL)
4904 parm = DECL_INITIAL (parm);
4905 else if (TREE_CODE (parm) == TYPE_DECL
4906 || TREE_CODE (parm) == TEMPLATE_DECL)
4907 parm = TREE_TYPE (parm);
4908 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4909 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4910 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4911 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4912 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4913 return parm;
4916 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4917 parameter packs used by the template parameter PARM. */
4919 static void
4920 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4922 /* A type parm can't refer to another parm. */
4923 if (TREE_CODE (parm) == TYPE_DECL)
4924 return;
4925 else if (TREE_CODE (parm) == PARM_DECL)
4927 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4928 ppd, ppd->visited);
4929 return;
4932 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4934 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4935 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4936 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4939 /* PARM is a template parameter pack. Return any parameter packs used in
4940 its type or the type of any of its template parameters. If there are
4941 any such packs, it will be instantiated into a fixed template parameter
4942 list by partial instantiation rather than be fully deduced. */
4944 tree
4945 fixed_parameter_pack_p (tree parm)
4947 /* This can only be true in a member template. */
4948 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4949 return NULL_TREE;
4950 /* This can only be true for a parameter pack. */
4951 if (!template_parameter_pack_p (parm))
4952 return NULL_TREE;
4953 /* A type parm can't refer to another parm. */
4954 if (TREE_CODE (parm) == TYPE_DECL)
4955 return NULL_TREE;
4957 tree parameter_packs = NULL_TREE;
4958 struct find_parameter_pack_data ppd;
4959 ppd.parameter_packs = &parameter_packs;
4960 ppd.visited = new hash_set<tree>;
4961 ppd.type_pack_expansion_p = false;
4963 fixed_parameter_pack_p_1 (parm, &ppd);
4965 delete ppd.visited;
4966 return parameter_packs;
4969 /* Check that a template declaration's use of default arguments and
4970 parameter packs is not invalid. Here, PARMS are the template
4971 parameters. IS_PRIMARY is true if DECL is the thing declared by
4972 a primary template. IS_PARTIAL is true if DECL is a partial
4973 specialization.
4975 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4976 declaration (but not a definition); 1 indicates a declaration, 2
4977 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4978 emitted for extraneous default arguments.
4980 Returns TRUE if there were no errors found, FALSE otherwise. */
4982 bool
4983 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4984 bool is_partial, int is_friend_decl)
4986 const char *msg;
4987 int last_level_to_check;
4988 tree parm_level;
4989 bool no_errors = true;
4991 /* [temp.param]
4993 A default template-argument shall not be specified in a
4994 function template declaration or a function template definition, nor
4995 in the template-parameter-list of the definition of a member of a
4996 class template. */
4998 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4999 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5000 /* You can't have a function template declaration in a local
5001 scope, nor you can you define a member of a class template in a
5002 local scope. */
5003 return true;
5005 if ((TREE_CODE (decl) == TYPE_DECL
5006 && TREE_TYPE (decl)
5007 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5008 || (TREE_CODE (decl) == FUNCTION_DECL
5009 && LAMBDA_FUNCTION_P (decl)))
5010 /* A lambda doesn't have an explicit declaration; don't complain
5011 about the parms of the enclosing class. */
5012 return true;
5014 if (current_class_type
5015 && !TYPE_BEING_DEFINED (current_class_type)
5016 && DECL_LANG_SPECIFIC (decl)
5017 && DECL_DECLARES_FUNCTION_P (decl)
5018 /* If this is either a friend defined in the scope of the class
5019 or a member function. */
5020 && (DECL_FUNCTION_MEMBER_P (decl)
5021 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5022 : DECL_FRIEND_CONTEXT (decl)
5023 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5024 : false)
5025 /* And, if it was a member function, it really was defined in
5026 the scope of the class. */
5027 && (!DECL_FUNCTION_MEMBER_P (decl)
5028 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5029 /* We already checked these parameters when the template was
5030 declared, so there's no need to do it again now. This function
5031 was defined in class scope, but we're processing its body now
5032 that the class is complete. */
5033 return true;
5035 /* Core issue 226 (C++0x only): the following only applies to class
5036 templates. */
5037 if (is_primary
5038 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5040 /* [temp.param]
5042 If a template-parameter has a default template-argument, all
5043 subsequent template-parameters shall have a default
5044 template-argument supplied. */
5045 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5047 tree inner_parms = TREE_VALUE (parm_level);
5048 int ntparms = TREE_VEC_LENGTH (inner_parms);
5049 int seen_def_arg_p = 0;
5050 int i;
5052 for (i = 0; i < ntparms; ++i)
5054 tree parm = TREE_VEC_ELT (inner_parms, i);
5056 if (parm == error_mark_node)
5057 continue;
5059 if (TREE_PURPOSE (parm))
5060 seen_def_arg_p = 1;
5061 else if (seen_def_arg_p
5062 && !template_parameter_pack_p (TREE_VALUE (parm)))
5064 error ("no default argument for %qD", TREE_VALUE (parm));
5065 /* For better subsequent error-recovery, we indicate that
5066 there should have been a default argument. */
5067 TREE_PURPOSE (parm) = error_mark_node;
5068 no_errors = false;
5070 else if (!is_partial
5071 && !is_friend_decl
5072 /* Don't complain about an enclosing partial
5073 specialization. */
5074 && parm_level == parms
5075 && TREE_CODE (decl) == TYPE_DECL
5076 && i < ntparms - 1
5077 && template_parameter_pack_p (TREE_VALUE (parm))
5078 /* A fixed parameter pack will be partially
5079 instantiated into a fixed length list. */
5080 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5082 /* A primary class template can only have one
5083 parameter pack, at the end of the template
5084 parameter list. */
5086 error ("parameter pack %q+D must be at the end of the"
5087 " template parameter list", TREE_VALUE (parm));
5089 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5090 = error_mark_node;
5091 no_errors = false;
5097 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5098 || is_partial
5099 || !is_primary
5100 || is_friend_decl)
5101 /* For an ordinary class template, default template arguments are
5102 allowed at the innermost level, e.g.:
5103 template <class T = int>
5104 struct S {};
5105 but, in a partial specialization, they're not allowed even
5106 there, as we have in [temp.class.spec]:
5108 The template parameter list of a specialization shall not
5109 contain default template argument values.
5111 So, for a partial specialization, or for a function template
5112 (in C++98/C++03), we look at all of them. */
5114 else
5115 /* But, for a primary class template that is not a partial
5116 specialization we look at all template parameters except the
5117 innermost ones. */
5118 parms = TREE_CHAIN (parms);
5120 /* Figure out what error message to issue. */
5121 if (is_friend_decl == 2)
5122 msg = G_("default template arguments may not be used in function template "
5123 "friend re-declaration");
5124 else if (is_friend_decl)
5125 msg = G_("default template arguments may not be used in function template "
5126 "friend declarations");
5127 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5128 msg = G_("default template arguments may not be used in function templates "
5129 "without -std=c++11 or -std=gnu++11");
5130 else if (is_partial)
5131 msg = G_("default template arguments may not be used in "
5132 "partial specializations");
5133 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5134 msg = G_("default argument for template parameter for class enclosing %qD");
5135 else
5136 /* Per [temp.param]/9, "A default template-argument shall not be
5137 specified in the template-parameter-lists of the definition of
5138 a member of a class template that appears outside of the member's
5139 class.", thus if we aren't handling a member of a class template
5140 there is no need to examine the parameters. */
5141 return true;
5143 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5144 /* If we're inside a class definition, there's no need to
5145 examine the parameters to the class itself. On the one
5146 hand, they will be checked when the class is defined, and,
5147 on the other, default arguments are valid in things like:
5148 template <class T = double>
5149 struct S { template <class U> void f(U); };
5150 Here the default argument for `S' has no bearing on the
5151 declaration of `f'. */
5152 last_level_to_check = template_class_depth (current_class_type) + 1;
5153 else
5154 /* Check everything. */
5155 last_level_to_check = 0;
5157 for (parm_level = parms;
5158 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5159 parm_level = TREE_CHAIN (parm_level))
5161 tree inner_parms = TREE_VALUE (parm_level);
5162 int i;
5163 int ntparms;
5165 ntparms = TREE_VEC_LENGTH (inner_parms);
5166 for (i = 0; i < ntparms; ++i)
5168 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5169 continue;
5171 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5173 if (msg)
5175 no_errors = false;
5176 if (is_friend_decl == 2)
5177 return no_errors;
5179 error (msg, decl);
5180 msg = 0;
5183 /* Clear out the default argument so that we are not
5184 confused later. */
5185 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5189 /* At this point, if we're still interested in issuing messages,
5190 they must apply to classes surrounding the object declared. */
5191 if (msg)
5192 msg = G_("default argument for template parameter for class "
5193 "enclosing %qD");
5196 return no_errors;
5199 /* Worker for push_template_decl_real, called via
5200 for_each_template_parm. DATA is really an int, indicating the
5201 level of the parameters we are interested in. If T is a template
5202 parameter of that level, return nonzero. */
5204 static int
5205 template_parm_this_level_p (tree t, void* data)
5207 int this_level = *(int *)data;
5208 int level;
5210 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5211 level = TEMPLATE_PARM_LEVEL (t);
5212 else
5213 level = TEMPLATE_TYPE_LEVEL (t);
5214 return level == this_level;
5217 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5218 DATA is really an int, indicating the innermost outer level of parameters.
5219 If T is a template parameter of that level or further out, return
5220 nonzero. */
5222 static int
5223 template_parm_outer_level (tree t, void *data)
5225 int this_level = *(int *)data;
5226 int level;
5228 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5229 level = TEMPLATE_PARM_LEVEL (t);
5230 else
5231 level = TEMPLATE_TYPE_LEVEL (t);
5232 return level <= this_level;
5235 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5236 parameters given by current_template_args, or reuses a
5237 previously existing one, if appropriate. Returns the DECL, or an
5238 equivalent one, if it is replaced via a call to duplicate_decls.
5240 If IS_FRIEND is true, DECL is a friend declaration. */
5242 tree
5243 push_template_decl_real (tree decl, bool is_friend)
5245 tree tmpl;
5246 tree args;
5247 tree info;
5248 tree ctx;
5249 bool is_primary;
5250 bool is_partial;
5251 int new_template_p = 0;
5252 /* True if the template is a member template, in the sense of
5253 [temp.mem]. */
5254 bool member_template_p = false;
5256 if (decl == error_mark_node || !current_template_parms)
5257 return error_mark_node;
5259 /* See if this is a partial specialization. */
5260 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5261 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5262 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5263 || (VAR_P (decl)
5264 && DECL_LANG_SPECIFIC (decl)
5265 && DECL_TEMPLATE_SPECIALIZATION (decl)
5266 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5268 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5269 is_friend = true;
5271 if (is_friend)
5272 /* For a friend, we want the context of the friend function, not
5273 the type of which it is a friend. */
5274 ctx = CP_DECL_CONTEXT (decl);
5275 else if (CP_DECL_CONTEXT (decl)
5276 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5277 /* In the case of a virtual function, we want the class in which
5278 it is defined. */
5279 ctx = CP_DECL_CONTEXT (decl);
5280 else
5281 /* Otherwise, if we're currently defining some class, the DECL
5282 is assumed to be a member of the class. */
5283 ctx = current_scope ();
5285 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5286 ctx = NULL_TREE;
5288 if (!DECL_CONTEXT (decl))
5289 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5291 /* See if this is a primary template. */
5292 if (is_friend && ctx
5293 && uses_template_parms_level (ctx, processing_template_decl))
5294 /* A friend template that specifies a class context, i.e.
5295 template <typename T> friend void A<T>::f();
5296 is not primary. */
5297 is_primary = false;
5298 else if (TREE_CODE (decl) == TYPE_DECL
5299 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5300 is_primary = false;
5301 else
5302 is_primary = template_parm_scope_p ();
5304 if (is_primary)
5306 warning (OPT_Wtemplates, "template %qD declared", decl);
5308 if (DECL_CLASS_SCOPE_P (decl))
5309 member_template_p = true;
5310 if (TREE_CODE (decl) == TYPE_DECL
5311 && anon_aggrname_p (DECL_NAME (decl)))
5313 error ("template class without a name");
5314 return error_mark_node;
5316 else if (TREE_CODE (decl) == FUNCTION_DECL)
5318 if (member_template_p)
5320 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5321 error ("member template %qD may not have virt-specifiers", decl);
5323 if (DECL_DESTRUCTOR_P (decl))
5325 /* [temp.mem]
5327 A destructor shall not be a member template. */
5328 error ("destructor %qD declared as member template", decl);
5329 return error_mark_node;
5331 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5332 && (!prototype_p (TREE_TYPE (decl))
5333 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5334 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5335 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5336 == void_list_node)))
5338 /* [basic.stc.dynamic.allocation]
5340 An allocation function can be a function
5341 template. ... Template allocation functions shall
5342 have two or more parameters. */
5343 error ("invalid template declaration of %qD", decl);
5344 return error_mark_node;
5347 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5348 && CLASS_TYPE_P (TREE_TYPE (decl)))
5350 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5351 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5352 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5354 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5355 if (TREE_CODE (t) == TYPE_DECL)
5356 t = TREE_TYPE (t);
5357 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5358 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5361 else if (TREE_CODE (decl) == TYPE_DECL
5362 && TYPE_DECL_ALIAS_P (decl))
5363 /* alias-declaration */
5364 gcc_assert (!DECL_ARTIFICIAL (decl));
5365 else if (VAR_P (decl))
5366 /* C++14 variable template. */;
5367 else
5369 error ("template declaration of %q#D", decl);
5370 return error_mark_node;
5374 /* Check to see that the rules regarding the use of default
5375 arguments are not being violated. */
5376 check_default_tmpl_args (decl, current_template_parms,
5377 is_primary, is_partial, /*is_friend_decl=*/0);
5379 /* Ensure that there are no parameter packs in the type of this
5380 declaration that have not been expanded. */
5381 if (TREE_CODE (decl) == FUNCTION_DECL)
5383 /* Check each of the arguments individually to see if there are
5384 any bare parameter packs. */
5385 tree type = TREE_TYPE (decl);
5386 tree arg = DECL_ARGUMENTS (decl);
5387 tree argtype = TYPE_ARG_TYPES (type);
5389 while (arg && argtype)
5391 if (!DECL_PACK_P (arg)
5392 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5394 /* This is a PARM_DECL that contains unexpanded parameter
5395 packs. We have already complained about this in the
5396 check_for_bare_parameter_packs call, so just replace
5397 these types with ERROR_MARK_NODE. */
5398 TREE_TYPE (arg) = error_mark_node;
5399 TREE_VALUE (argtype) = error_mark_node;
5402 arg = DECL_CHAIN (arg);
5403 argtype = TREE_CHAIN (argtype);
5406 /* Check for bare parameter packs in the return type and the
5407 exception specifiers. */
5408 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5409 /* Errors were already issued, set return type to int
5410 as the frontend doesn't expect error_mark_node as
5411 the return type. */
5412 TREE_TYPE (type) = integer_type_node;
5413 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5414 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5416 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5417 && TYPE_DECL_ALIAS_P (decl))
5418 ? DECL_ORIGINAL_TYPE (decl)
5419 : TREE_TYPE (decl)))
5421 TREE_TYPE (decl) = error_mark_node;
5422 return error_mark_node;
5425 if (is_partial)
5426 return process_partial_specialization (decl);
5428 args = current_template_args ();
5430 if (!ctx
5431 || TREE_CODE (ctx) == FUNCTION_DECL
5432 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5433 || (TREE_CODE (decl) == TYPE_DECL
5434 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5435 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5437 if (DECL_LANG_SPECIFIC (decl)
5438 && DECL_TEMPLATE_INFO (decl)
5439 && DECL_TI_TEMPLATE (decl))
5440 tmpl = DECL_TI_TEMPLATE (decl);
5441 /* If DECL is a TYPE_DECL for a class-template, then there won't
5442 be DECL_LANG_SPECIFIC. The information equivalent to
5443 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5444 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5445 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5446 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5448 /* Since a template declaration already existed for this
5449 class-type, we must be redeclaring it here. Make sure
5450 that the redeclaration is valid. */
5451 redeclare_class_template (TREE_TYPE (decl),
5452 current_template_parms,
5453 current_template_constraints ());
5454 /* We don't need to create a new TEMPLATE_DECL; just use the
5455 one we already had. */
5456 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5458 else
5460 tmpl = build_template_decl (decl, current_template_parms,
5461 member_template_p);
5462 new_template_p = 1;
5464 if (DECL_LANG_SPECIFIC (decl)
5465 && DECL_TEMPLATE_SPECIALIZATION (decl))
5467 /* A specialization of a member template of a template
5468 class. */
5469 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5470 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5471 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5475 else
5477 tree a, t, current, parms;
5478 int i;
5479 tree tinfo = get_template_info (decl);
5481 if (!tinfo)
5483 error ("template definition of non-template %q#D", decl);
5484 return error_mark_node;
5487 tmpl = TI_TEMPLATE (tinfo);
5489 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5490 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5491 && DECL_TEMPLATE_SPECIALIZATION (decl)
5492 && DECL_MEMBER_TEMPLATE_P (tmpl))
5494 tree new_tmpl;
5496 /* The declaration is a specialization of a member
5497 template, declared outside the class. Therefore, the
5498 innermost template arguments will be NULL, so we
5499 replace them with the arguments determined by the
5500 earlier call to check_explicit_specialization. */
5501 args = DECL_TI_ARGS (decl);
5503 new_tmpl
5504 = build_template_decl (decl, current_template_parms,
5505 member_template_p);
5506 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5507 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5508 DECL_TI_TEMPLATE (decl) = new_tmpl;
5509 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5510 DECL_TEMPLATE_INFO (new_tmpl)
5511 = build_template_info (tmpl, args);
5513 register_specialization (new_tmpl,
5514 most_general_template (tmpl),
5515 args,
5516 is_friend, 0);
5517 return decl;
5520 /* Make sure the template headers we got make sense. */
5522 parms = DECL_TEMPLATE_PARMS (tmpl);
5523 i = TMPL_PARMS_DEPTH (parms);
5524 if (TMPL_ARGS_DEPTH (args) != i)
5526 error ("expected %d levels of template parms for %q#D, got %d",
5527 i, decl, TMPL_ARGS_DEPTH (args));
5528 DECL_INTERFACE_KNOWN (decl) = 1;
5529 return error_mark_node;
5531 else
5532 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5534 a = TMPL_ARGS_LEVEL (args, i);
5535 t = INNERMOST_TEMPLATE_PARMS (parms);
5537 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5539 if (current == decl)
5540 error ("got %d template parameters for %q#D",
5541 TREE_VEC_LENGTH (a), decl);
5542 else
5543 error ("got %d template parameters for %q#T",
5544 TREE_VEC_LENGTH (a), current);
5545 error (" but %d required", TREE_VEC_LENGTH (t));
5546 /* Avoid crash in import_export_decl. */
5547 DECL_INTERFACE_KNOWN (decl) = 1;
5548 return error_mark_node;
5551 if (current == decl)
5552 current = ctx;
5553 else if (current == NULL_TREE)
5554 /* Can happen in erroneous input. */
5555 break;
5556 else
5557 current = get_containing_scope (current);
5560 /* Check that the parms are used in the appropriate qualifying scopes
5561 in the declarator. */
5562 if (!comp_template_args
5563 (TI_ARGS (tinfo),
5564 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5566 error ("template arguments to %qD do not match original"
5567 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5568 if (!uses_template_parms (TI_ARGS (tinfo)))
5569 inform (input_location, "use %<template<>%> for"
5570 " an explicit specialization");
5571 /* Avoid crash in import_export_decl. */
5572 DECL_INTERFACE_KNOWN (decl) = 1;
5573 return error_mark_node;
5577 DECL_TEMPLATE_RESULT (tmpl) = decl;
5578 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5580 /* Push template declarations for global functions and types. Note
5581 that we do not try to push a global template friend declared in a
5582 template class; such a thing may well depend on the template
5583 parameters of the class. */
5584 if (new_template_p && !ctx
5585 && !(is_friend && template_class_depth (current_class_type) > 0))
5587 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5588 if (tmpl == error_mark_node)
5589 return error_mark_node;
5591 /* Hide template friend classes that haven't been declared yet. */
5592 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5594 DECL_ANTICIPATED (tmpl) = 1;
5595 DECL_FRIEND_P (tmpl) = 1;
5599 if (is_primary)
5601 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5603 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5605 /* Give template template parms a DECL_CONTEXT of the template
5606 for which they are a parameter. */
5607 parms = INNERMOST_TEMPLATE_PARMS (parms);
5608 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5610 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5611 if (TREE_CODE (parm) == TEMPLATE_DECL)
5612 DECL_CONTEXT (parm) = tmpl;
5615 if (TREE_CODE (decl) == TYPE_DECL
5616 && TYPE_DECL_ALIAS_P (decl)
5617 && complex_alias_template_p (tmpl))
5618 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5621 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5622 back to its most general template. If TMPL is a specialization,
5623 ARGS may only have the innermost set of arguments. Add the missing
5624 argument levels if necessary. */
5625 if (DECL_TEMPLATE_INFO (tmpl))
5626 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5628 info = build_template_info (tmpl, args);
5630 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5631 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5632 else
5634 if (is_primary)
5635 retrofit_lang_decl (decl);
5636 if (DECL_LANG_SPECIFIC (decl))
5637 DECL_TEMPLATE_INFO (decl) = info;
5640 if (flag_implicit_templates
5641 && !is_friend
5642 && TREE_PUBLIC (decl)
5643 && VAR_OR_FUNCTION_DECL_P (decl))
5644 /* Set DECL_COMDAT on template instantiations; if we force
5645 them to be emitted by explicit instantiation or -frepo,
5646 mark_needed will tell cgraph to do the right thing. */
5647 DECL_COMDAT (decl) = true;
5649 return DECL_TEMPLATE_RESULT (tmpl);
5652 tree
5653 push_template_decl (tree decl)
5655 return push_template_decl_real (decl, false);
5658 /* FN is an inheriting constructor that inherits from the constructor
5659 template INHERITED; turn FN into a constructor template with a matching
5660 template header. */
5662 tree
5663 add_inherited_template_parms (tree fn, tree inherited)
5665 tree inner_parms
5666 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5667 inner_parms = copy_node (inner_parms);
5668 tree parms
5669 = tree_cons (size_int (processing_template_decl + 1),
5670 inner_parms, current_template_parms);
5671 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5672 tree args = template_parms_to_args (parms);
5673 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5674 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5675 DECL_TEMPLATE_RESULT (tmpl) = fn;
5676 DECL_ARTIFICIAL (tmpl) = true;
5677 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5678 return tmpl;
5681 /* Called when a class template TYPE is redeclared with the indicated
5682 template PARMS, e.g.:
5684 template <class T> struct S;
5685 template <class T> struct S {}; */
5687 bool
5688 redeclare_class_template (tree type, tree parms, tree cons)
5690 tree tmpl;
5691 tree tmpl_parms;
5692 int i;
5694 if (!TYPE_TEMPLATE_INFO (type))
5696 error ("%qT is not a template type", type);
5697 return false;
5700 tmpl = TYPE_TI_TEMPLATE (type);
5701 if (!PRIMARY_TEMPLATE_P (tmpl))
5702 /* The type is nested in some template class. Nothing to worry
5703 about here; there are no new template parameters for the nested
5704 type. */
5705 return true;
5707 if (!parms)
5709 error ("template specifiers not specified in declaration of %qD",
5710 tmpl);
5711 return false;
5714 parms = INNERMOST_TEMPLATE_PARMS (parms);
5715 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5717 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5719 error_n (input_location, TREE_VEC_LENGTH (parms),
5720 "redeclared with %d template parameter",
5721 "redeclared with %d template parameters",
5722 TREE_VEC_LENGTH (parms));
5723 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5724 "previous declaration %qD used %d template parameter",
5725 "previous declaration %qD used %d template parameters",
5726 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5727 return false;
5730 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5732 tree tmpl_parm;
5733 tree parm;
5734 tree tmpl_default;
5735 tree parm_default;
5737 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5738 || TREE_VEC_ELT (parms, i) == error_mark_node)
5739 continue;
5741 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5742 if (error_operand_p (tmpl_parm))
5743 return false;
5745 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5746 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5747 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5749 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5750 TEMPLATE_DECL. */
5751 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5752 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5753 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5754 || (TREE_CODE (tmpl_parm) != PARM_DECL
5755 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5756 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5757 || (TREE_CODE (tmpl_parm) == PARM_DECL
5758 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5759 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5761 error ("template parameter %q+#D", tmpl_parm);
5762 error ("redeclared here as %q#D", parm);
5763 return false;
5766 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5768 /* We have in [temp.param]:
5770 A template-parameter may not be given default arguments
5771 by two different declarations in the same scope. */
5772 error_at (input_location, "redefinition of default argument for %q#D", parm);
5773 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5774 "original definition appeared here");
5775 return false;
5778 if (parm_default != NULL_TREE)
5779 /* Update the previous template parameters (which are the ones
5780 that will really count) with the new default value. */
5781 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5782 else if (tmpl_default != NULL_TREE)
5783 /* Update the new parameters, too; they'll be used as the
5784 parameters for any members. */
5785 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5787 /* Give each template template parm in this redeclaration a
5788 DECL_CONTEXT of the template for which they are a parameter. */
5789 if (TREE_CODE (parm) == TEMPLATE_DECL)
5791 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5792 DECL_CONTEXT (parm) = tmpl;
5795 if (TREE_CODE (parm) == TYPE_DECL)
5796 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5799 // Cannot redeclare a class template with a different set of constraints.
5800 if (!equivalent_constraints (get_constraints (tmpl), cons))
5802 error_at (input_location, "redeclaration %q#D with different "
5803 "constraints", tmpl);
5804 inform (DECL_SOURCE_LOCATION (tmpl),
5805 "original declaration appeared here");
5808 return true;
5811 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5812 to be used when the caller has already checked
5813 (processing_template_decl
5814 && !instantiation_dependent_expression_p (expr)
5815 && potential_constant_expression (expr))
5816 and cleared processing_template_decl. */
5818 tree
5819 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5821 return tsubst_copy_and_build (expr,
5822 /*args=*/NULL_TREE,
5823 complain,
5824 /*in_decl=*/NULL_TREE,
5825 /*function_p=*/false,
5826 /*integral_constant_expression_p=*/true);
5829 /* Simplify EXPR if it is a non-dependent expression. Returns the
5830 (possibly simplified) expression. */
5832 tree
5833 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5835 if (expr == NULL_TREE)
5836 return NULL_TREE;
5838 /* If we're in a template, but EXPR isn't value dependent, simplify
5839 it. We're supposed to treat:
5841 template <typename T> void f(T[1 + 1]);
5842 template <typename T> void f(T[2]);
5844 as two declarations of the same function, for example. */
5845 if (processing_template_decl
5846 && is_nondependent_constant_expression (expr))
5848 processing_template_decl_sentinel s;
5849 expr = instantiate_non_dependent_expr_internal (expr, complain);
5851 return expr;
5854 tree
5855 instantiate_non_dependent_expr (tree expr)
5857 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5860 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5861 an uninstantiated expression. */
5863 tree
5864 instantiate_non_dependent_or_null (tree expr)
5866 if (expr == NULL_TREE)
5867 return NULL_TREE;
5868 if (processing_template_decl)
5870 if (!is_nondependent_constant_expression (expr))
5871 expr = NULL_TREE;
5872 else
5874 processing_template_decl_sentinel s;
5875 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5878 return expr;
5881 /* True iff T is a specialization of a variable template. */
5883 bool
5884 variable_template_specialization_p (tree t)
5886 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5887 return false;
5888 tree tmpl = DECL_TI_TEMPLATE (t);
5889 return variable_template_p (tmpl);
5892 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5893 template declaration, or a TYPE_DECL for an alias declaration. */
5895 bool
5896 alias_type_or_template_p (tree t)
5898 if (t == NULL_TREE)
5899 return false;
5900 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5901 || (TYPE_P (t)
5902 && TYPE_NAME (t)
5903 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5904 || DECL_ALIAS_TEMPLATE_P (t));
5907 /* Return TRUE iff T is a specialization of an alias template. */
5909 bool
5910 alias_template_specialization_p (const_tree t)
5912 /* It's an alias template specialization if it's an alias and its
5913 TYPE_NAME is a specialization of a primary template. */
5914 if (TYPE_ALIAS_P (t))
5915 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5916 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5918 return false;
5921 /* An alias template is complex from a SFINAE perspective if a template-id
5922 using that alias can be ill-formed when the expansion is not, as with
5923 the void_t template. We determine this by checking whether the
5924 expansion for the alias template uses all its template parameters. */
5926 struct uses_all_template_parms_data
5928 int level;
5929 bool *seen;
5932 static int
5933 uses_all_template_parms_r (tree t, void *data_)
5935 struct uses_all_template_parms_data &data
5936 = *(struct uses_all_template_parms_data*)data_;
5937 tree idx = get_template_parm_index (t);
5939 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5940 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5941 return 0;
5944 static bool
5945 complex_alias_template_p (const_tree tmpl)
5947 struct uses_all_template_parms_data data;
5948 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5949 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5950 data.level = TMPL_PARMS_DEPTH (parms);
5951 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5952 data.seen = XALLOCAVEC (bool, len);
5953 for (int i = 0; i < len; ++i)
5954 data.seen[i] = false;
5956 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5957 for (int i = 0; i < len; ++i)
5958 if (!data.seen[i])
5959 return true;
5960 return false;
5963 /* Return TRUE iff T is a specialization of a complex alias template with
5964 dependent template-arguments. */
5966 bool
5967 dependent_alias_template_spec_p (const_tree t)
5969 if (!alias_template_specialization_p (t))
5970 return false;
5972 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5973 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5974 return false;
5976 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5977 if (!any_dependent_template_arguments_p (args))
5978 return false;
5980 return true;
5983 /* Return the number of innermost template parameters in TMPL. */
5985 static int
5986 num_innermost_template_parms (tree tmpl)
5988 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5989 return TREE_VEC_LENGTH (parms);
5992 /* Return either TMPL or another template that it is equivalent to under DR
5993 1286: An alias that just changes the name of a template is equivalent to
5994 the other template. */
5996 static tree
5997 get_underlying_template (tree tmpl)
5999 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6000 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6002 /* Determine if the alias is equivalent to an underlying template. */
6003 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6004 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6005 if (!tinfo)
6006 break;
6008 tree underlying = TI_TEMPLATE (tinfo);
6009 if (!PRIMARY_TEMPLATE_P (underlying)
6010 || (num_innermost_template_parms (tmpl)
6011 != num_innermost_template_parms (underlying)))
6012 break;
6014 tree alias_args = INNERMOST_TEMPLATE_ARGS
6015 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6016 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6017 break;
6019 /* Alias is equivalent. Strip it and repeat. */
6020 tmpl = underlying;
6023 return tmpl;
6026 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6027 must be a reference-to-function or a pointer-to-function type, as specified
6028 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6029 and check that the resulting function has external linkage. */
6031 static tree
6032 convert_nontype_argument_function (tree type, tree expr,
6033 tsubst_flags_t complain)
6035 tree fns = expr;
6036 tree fn, fn_no_ptr;
6037 linkage_kind linkage;
6039 fn = instantiate_type (type, fns, tf_none);
6040 if (fn == error_mark_node)
6041 return error_mark_node;
6043 if (value_dependent_expression_p (fn))
6044 goto accept;
6046 fn_no_ptr = strip_fnptr_conv (fn);
6047 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6048 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6049 if (BASELINK_P (fn_no_ptr))
6050 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6052 /* [temp.arg.nontype]/1
6054 A template-argument for a non-type, non-template template-parameter
6055 shall be one of:
6056 [...]
6057 -- the address of an object or function with external [C++11: or
6058 internal] linkage. */
6060 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6062 if (complain & tf_error)
6064 error ("%qE is not a valid template argument for type %qT",
6065 expr, type);
6066 if (TYPE_PTR_P (type))
6067 inform (input_location, "it must be the address of a function "
6068 "with external linkage");
6069 else
6070 inform (input_location, "it must be the name of a function with "
6071 "external linkage");
6073 return NULL_TREE;
6076 linkage = decl_linkage (fn_no_ptr);
6077 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6079 if (complain & tf_error)
6081 if (cxx_dialect >= cxx11)
6082 error ("%qE is not a valid template argument for type %qT "
6083 "because %qD has no linkage",
6084 expr, type, fn_no_ptr);
6085 else
6086 error ("%qE is not a valid template argument for type %qT "
6087 "because %qD does not have external linkage",
6088 expr, type, fn_no_ptr);
6090 return NULL_TREE;
6093 accept:
6094 if (TREE_CODE (type) == REFERENCE_TYPE)
6095 fn = build_address (fn);
6096 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6097 fn = build_nop (type, fn);
6099 return fn;
6102 /* Subroutine of convert_nontype_argument.
6103 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6104 Emit an error otherwise. */
6106 static bool
6107 check_valid_ptrmem_cst_expr (tree type, tree expr,
6108 tsubst_flags_t complain)
6110 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6111 tree orig_expr = expr;
6112 STRIP_NOPS (expr);
6113 if (null_ptr_cst_p (expr))
6114 return true;
6115 if (TREE_CODE (expr) == PTRMEM_CST
6116 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6117 PTRMEM_CST_CLASS (expr)))
6118 return true;
6119 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6120 return true;
6121 if (processing_template_decl
6122 && TREE_CODE (expr) == ADDR_EXPR
6123 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6124 return true;
6125 if (complain & tf_error)
6127 error_at (loc, "%qE is not a valid template argument for type %qT",
6128 orig_expr, type);
6129 if (TREE_CODE (expr) != PTRMEM_CST)
6130 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6131 else
6132 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6134 return false;
6137 /* Returns TRUE iff the address of OP is value-dependent.
6139 14.6.2.4 [temp.dep.temp]:
6140 A non-integral non-type template-argument is dependent if its type is
6141 dependent or it has either of the following forms
6142 qualified-id
6143 & qualified-id
6144 and contains a nested-name-specifier which specifies a class-name that
6145 names a dependent type.
6147 We generalize this to just say that the address of a member of a
6148 dependent class is value-dependent; the above doesn't cover the
6149 address of a static data member named with an unqualified-id. */
6151 static bool
6152 has_value_dependent_address (tree op)
6154 /* We could use get_inner_reference here, but there's no need;
6155 this is only relevant for template non-type arguments, which
6156 can only be expressed as &id-expression. */
6157 if (DECL_P (op))
6159 tree ctx = CP_DECL_CONTEXT (op);
6160 if (TYPE_P (ctx) && dependent_type_p (ctx))
6161 return true;
6164 return false;
6167 /* The next set of functions are used for providing helpful explanatory
6168 diagnostics for failed overload resolution. Their messages should be
6169 indented by two spaces for consistency with the messages in
6170 call.c */
6172 static int
6173 unify_success (bool /*explain_p*/)
6175 return 0;
6178 /* Other failure functions should call this one, to provide a single function
6179 for setting a breakpoint on. */
6181 static int
6182 unify_invalid (bool /*explain_p*/)
6184 return 1;
6187 static int
6188 unify_parameter_deduction_failure (bool explain_p, tree parm)
6190 if (explain_p)
6191 inform (input_location,
6192 " couldn't deduce template parameter %qD", parm);
6193 return unify_invalid (explain_p);
6196 static int
6197 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6199 if (explain_p)
6200 inform (input_location,
6201 " types %qT and %qT have incompatible cv-qualifiers",
6202 parm, arg);
6203 return unify_invalid (explain_p);
6206 static int
6207 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6209 if (explain_p)
6210 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6211 return unify_invalid (explain_p);
6214 static int
6215 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6217 if (explain_p)
6218 inform (input_location,
6219 " template parameter %qD is not a parameter pack, but "
6220 "argument %qD is",
6221 parm, arg);
6222 return unify_invalid (explain_p);
6225 static int
6226 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6228 if (explain_p)
6229 inform (input_location,
6230 " template argument %qE does not match "
6231 "pointer-to-member constant %qE",
6232 arg, parm);
6233 return unify_invalid (explain_p);
6236 static int
6237 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6239 if (explain_p)
6240 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6241 return unify_invalid (explain_p);
6244 static int
6245 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6247 if (explain_p)
6248 inform (input_location,
6249 " inconsistent parameter pack deduction with %qT and %qT",
6250 old_arg, new_arg);
6251 return unify_invalid (explain_p);
6254 static int
6255 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6257 if (explain_p)
6259 if (TYPE_P (parm))
6260 inform (input_location,
6261 " deduced conflicting types for parameter %qT (%qT and %qT)",
6262 parm, first, second);
6263 else
6264 inform (input_location,
6265 " deduced conflicting values for non-type parameter "
6266 "%qE (%qE and %qE)", parm, first, second);
6268 return unify_invalid (explain_p);
6271 static int
6272 unify_vla_arg (bool explain_p, tree arg)
6274 if (explain_p)
6275 inform (input_location,
6276 " variable-sized array type %qT is not "
6277 "a valid template argument",
6278 arg);
6279 return unify_invalid (explain_p);
6282 static int
6283 unify_method_type_error (bool explain_p, tree arg)
6285 if (explain_p)
6286 inform (input_location,
6287 " member function type %qT is not a valid template argument",
6288 arg);
6289 return unify_invalid (explain_p);
6292 static int
6293 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6295 if (explain_p)
6297 if (least_p)
6298 inform_n (input_location, wanted,
6299 " candidate expects at least %d argument, %d provided",
6300 " candidate expects at least %d arguments, %d provided",
6301 wanted, have);
6302 else
6303 inform_n (input_location, wanted,
6304 " candidate expects %d argument, %d provided",
6305 " candidate expects %d arguments, %d provided",
6306 wanted, have);
6308 return unify_invalid (explain_p);
6311 static int
6312 unify_too_many_arguments (bool explain_p, int have, int wanted)
6314 return unify_arity (explain_p, have, wanted);
6317 static int
6318 unify_too_few_arguments (bool explain_p, int have, int wanted,
6319 bool least_p = false)
6321 return unify_arity (explain_p, have, wanted, least_p);
6324 static int
6325 unify_arg_conversion (bool explain_p, tree to_type,
6326 tree from_type, tree arg)
6328 if (explain_p)
6329 inform (EXPR_LOC_OR_LOC (arg, input_location),
6330 " cannot convert %qE (type %qT) to type %qT",
6331 arg, from_type, to_type);
6332 return unify_invalid (explain_p);
6335 static int
6336 unify_no_common_base (bool explain_p, enum template_base_result r,
6337 tree parm, tree arg)
6339 if (explain_p)
6340 switch (r)
6342 case tbr_ambiguous_baseclass:
6343 inform (input_location, " %qT is an ambiguous base class of %qT",
6344 parm, arg);
6345 break;
6346 default:
6347 inform (input_location, " %qT is not derived from %qT", arg, parm);
6348 break;
6350 return unify_invalid (explain_p);
6353 static int
6354 unify_inconsistent_template_template_parameters (bool explain_p)
6356 if (explain_p)
6357 inform (input_location,
6358 " template parameters of a template template argument are "
6359 "inconsistent with other deduced template arguments");
6360 return unify_invalid (explain_p);
6363 static int
6364 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6366 if (explain_p)
6367 inform (input_location,
6368 " can't deduce a template for %qT from non-template type %qT",
6369 parm, arg);
6370 return unify_invalid (explain_p);
6373 static int
6374 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6376 if (explain_p)
6377 inform (input_location,
6378 " template argument %qE does not match %qE", arg, parm);
6379 return unify_invalid (explain_p);
6382 /* Attempt to convert the non-type template parameter EXPR to the
6383 indicated TYPE. If the conversion is successful, return the
6384 converted value. If the conversion is unsuccessful, return
6385 NULL_TREE if we issued an error message, or error_mark_node if we
6386 did not. We issue error messages for out-and-out bad template
6387 parameters, but not simply because the conversion failed, since we
6388 might be just trying to do argument deduction. Both TYPE and EXPR
6389 must be non-dependent.
6391 The conversion follows the special rules described in
6392 [temp.arg.nontype], and it is much more strict than an implicit
6393 conversion.
6395 This function is called twice for each template argument (see
6396 lookup_template_class for a more accurate description of this
6397 problem). This means that we need to handle expressions which
6398 are not valid in a C++ source, but can be created from the
6399 first call (for instance, casts to perform conversions). These
6400 hacks can go away after we fix the double coercion problem. */
6402 static tree
6403 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6405 tree expr_type;
6406 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6407 tree orig_expr = expr;
6409 /* Detect immediately string literals as invalid non-type argument.
6410 This special-case is not needed for correctness (we would easily
6411 catch this later), but only to provide better diagnostic for this
6412 common user mistake. As suggested by DR 100, we do not mention
6413 linkage issues in the diagnostic as this is not the point. */
6414 /* FIXME we're making this OK. */
6415 if (TREE_CODE (expr) == STRING_CST)
6417 if (complain & tf_error)
6418 error ("%qE is not a valid template argument for type %qT "
6419 "because string literals can never be used in this context",
6420 expr, type);
6421 return NULL_TREE;
6424 /* Add the ADDR_EXPR now for the benefit of
6425 value_dependent_expression_p. */
6426 if (TYPE_PTROBV_P (type)
6427 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6429 expr = decay_conversion (expr, complain);
6430 if (expr == error_mark_node)
6431 return error_mark_node;
6434 /* If we are in a template, EXPR may be non-dependent, but still
6435 have a syntactic, rather than semantic, form. For example, EXPR
6436 might be a SCOPE_REF, rather than the VAR_DECL to which the
6437 SCOPE_REF refers. Preserving the qualifying scope is necessary
6438 so that access checking can be performed when the template is
6439 instantiated -- but here we need the resolved form so that we can
6440 convert the argument. */
6441 bool non_dep = false;
6442 if (TYPE_REF_OBJ_P (type)
6443 && has_value_dependent_address (expr))
6444 /* If we want the address and it's value-dependent, don't fold. */;
6445 else if (processing_template_decl
6446 && is_nondependent_constant_expression (expr))
6447 non_dep = true;
6448 if (error_operand_p (expr))
6449 return error_mark_node;
6450 expr_type = TREE_TYPE (expr);
6452 /* If the argument is non-dependent, perform any conversions in
6453 non-dependent context as well. */
6454 processing_template_decl_sentinel s (non_dep);
6455 if (non_dep)
6456 expr = instantiate_non_dependent_expr_internal (expr, complain);
6458 if (value_dependent_expression_p (expr))
6459 expr = canonicalize_expr_argument (expr, complain);
6461 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6462 to a non-type argument of "nullptr". */
6463 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6464 expr = fold_simple (convert (type, expr));
6466 /* In C++11, integral or enumeration non-type template arguments can be
6467 arbitrary constant expressions. Pointer and pointer to
6468 member arguments can be general constant expressions that evaluate
6469 to a null value, but otherwise still need to be of a specific form. */
6470 if (cxx_dialect >= cxx11)
6472 if (TREE_CODE (expr) == PTRMEM_CST)
6473 /* A PTRMEM_CST is already constant, and a valid template
6474 argument for a parameter of pointer to member type, we just want
6475 to leave it in that form rather than lower it to a
6476 CONSTRUCTOR. */;
6477 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6478 || cxx_dialect >= cxx17)
6480 /* C++17: A template-argument for a non-type template-parameter shall
6481 be a converted constant expression (8.20) of the type of the
6482 template-parameter. */
6483 expr = build_converted_constant_expr (type, expr, complain);
6484 if (expr == error_mark_node)
6485 return error_mark_node;
6486 expr = maybe_constant_value (expr);
6487 expr = convert_from_reference (expr);
6489 else if (TYPE_PTR_OR_PTRMEM_P (type))
6491 tree folded = maybe_constant_value (expr);
6492 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6493 : null_member_pointer_value_p (folded))
6494 expr = folded;
6498 if (TREE_CODE (type) == REFERENCE_TYPE)
6499 expr = mark_lvalue_use (expr);
6500 else
6501 expr = mark_rvalue_use (expr);
6503 /* HACK: Due to double coercion, we can get a
6504 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6505 which is the tree that we built on the first call (see
6506 below when coercing to reference to object or to reference to
6507 function). We just strip everything and get to the arg.
6508 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6509 for examples. */
6510 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6512 tree probe_type, probe = expr;
6513 if (REFERENCE_REF_P (probe))
6514 probe = TREE_OPERAND (probe, 0);
6515 probe_type = TREE_TYPE (probe);
6516 if (TREE_CODE (probe) == NOP_EXPR)
6518 /* ??? Maybe we could use convert_from_reference here, but we
6519 would need to relax its constraints because the NOP_EXPR
6520 could actually change the type to something more cv-qualified,
6521 and this is not folded by convert_from_reference. */
6522 tree addr = TREE_OPERAND (probe, 0);
6523 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6524 && TREE_CODE (addr) == ADDR_EXPR
6525 && TYPE_PTR_P (TREE_TYPE (addr))
6526 && (same_type_ignoring_top_level_qualifiers_p
6527 (TREE_TYPE (probe_type),
6528 TREE_TYPE (TREE_TYPE (addr)))))
6530 expr = TREE_OPERAND (addr, 0);
6531 expr_type = TREE_TYPE (probe_type);
6536 /* [temp.arg.nontype]/5, bullet 1
6538 For a non-type template-parameter of integral or enumeration type,
6539 integral promotions (_conv.prom_) and integral conversions
6540 (_conv.integral_) are applied. */
6541 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6543 if (cxx_dialect < cxx11)
6545 tree t = build_converted_constant_expr (type, expr, complain);
6546 t = maybe_constant_value (t);
6547 if (t != error_mark_node)
6548 expr = t;
6551 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6552 return error_mark_node;
6554 /* Notice that there are constant expressions like '4 % 0' which
6555 do not fold into integer constants. */
6556 if (TREE_CODE (expr) != INTEGER_CST
6557 && !value_dependent_expression_p (expr))
6559 if (complain & tf_error)
6561 int errs = errorcount, warns = warningcount + werrorcount;
6562 if (!require_potential_constant_expression (expr))
6563 expr = error_mark_node;
6564 else
6565 expr = cxx_constant_value (expr);
6566 if (errorcount > errs || warningcount + werrorcount > warns)
6567 inform (loc, "in template argument for type %qT ", type);
6568 if (expr == error_mark_node)
6569 return NULL_TREE;
6570 /* else cxx_constant_value complained but gave us
6571 a real constant, so go ahead. */
6572 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6574 else
6575 return NULL_TREE;
6578 /* Avoid typedef problems. */
6579 if (TREE_TYPE (expr) != type)
6580 expr = fold_convert (type, expr);
6582 /* [temp.arg.nontype]/5, bullet 2
6584 For a non-type template-parameter of type pointer to object,
6585 qualification conversions (_conv.qual_) and the array-to-pointer
6586 conversion (_conv.array_) are applied. */
6587 else if (TYPE_PTROBV_P (type))
6589 tree decayed = expr;
6591 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6592 decay_conversion or an explicit cast. If it's a problematic cast,
6593 we'll complain about it below. */
6594 if (TREE_CODE (expr) == NOP_EXPR)
6596 tree probe = expr;
6597 STRIP_NOPS (probe);
6598 if (TREE_CODE (probe) == ADDR_EXPR
6599 && TYPE_PTR_P (TREE_TYPE (probe)))
6601 expr = probe;
6602 expr_type = TREE_TYPE (expr);
6606 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6608 A template-argument for a non-type, non-template template-parameter
6609 shall be one of: [...]
6611 -- the name of a non-type template-parameter;
6612 -- the address of an object or function with external linkage, [...]
6613 expressed as "& id-expression" where the & is optional if the name
6614 refers to a function or array, or if the corresponding
6615 template-parameter is a reference.
6617 Here, we do not care about functions, as they are invalid anyway
6618 for a parameter of type pointer-to-object. */
6620 if (value_dependent_expression_p (expr))
6621 /* Non-type template parameters are OK. */
6623 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6624 /* Null pointer values are OK in C++11. */;
6625 else if (TREE_CODE (expr) != ADDR_EXPR)
6627 if (VAR_P (expr))
6629 if (complain & tf_error)
6630 error ("%qD is not a valid template argument "
6631 "because %qD is a variable, not the address of "
6632 "a variable", orig_expr, expr);
6633 return NULL_TREE;
6635 if (POINTER_TYPE_P (expr_type))
6637 if (complain & tf_error)
6638 error ("%qE is not a valid template argument for %qT "
6639 "because it is not the address of a variable",
6640 orig_expr, type);
6641 return NULL_TREE;
6643 /* Other values, like integer constants, might be valid
6644 non-type arguments of some other type. */
6645 return error_mark_node;
6647 else
6649 tree decl = TREE_OPERAND (expr, 0);
6651 if (!VAR_P (decl))
6653 if (complain & tf_error)
6654 error ("%qE is not a valid template argument of type %qT "
6655 "because %qE is not a variable", orig_expr, type, decl);
6656 return NULL_TREE;
6658 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6660 if (complain & tf_error)
6661 error ("%qE is not a valid template argument of type %qT "
6662 "because %qD does not have external linkage",
6663 orig_expr, type, decl);
6664 return NULL_TREE;
6666 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6667 && decl_linkage (decl) == lk_none)
6669 if (complain & tf_error)
6670 error ("%qE is not a valid template argument of type %qT "
6671 "because %qD has no linkage", orig_expr, type, decl);
6672 return NULL_TREE;
6674 /* C++17: For a non-type template-parameter of reference or pointer
6675 type, the value of the constant expression shall not refer to (or
6676 for a pointer type, shall not be the address of):
6677 * a subobject (4.5),
6678 * a temporary object (15.2),
6679 * a string literal (5.13.5),
6680 * the result of a typeid expression (8.2.8), or
6681 * a predefined __func__ variable (11.4.1). */
6682 else if (DECL_ARTIFICIAL (decl))
6684 if (complain & tf_error)
6685 error ("the address of %qD is not a valid template argument",
6686 decl);
6687 return NULL_TREE;
6689 else if (!same_type_ignoring_top_level_qualifiers_p
6690 (strip_array_types (TREE_TYPE (type)),
6691 strip_array_types (TREE_TYPE (decl))))
6693 if (complain & tf_error)
6694 error ("the address of the %qT subobject of %qD is not a "
6695 "valid template argument", TREE_TYPE (type), decl);
6696 return NULL_TREE;
6698 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6700 if (complain & tf_error)
6701 error ("the address of %qD is not a valid template argument "
6702 "because it does not have static storage duration",
6703 decl);
6704 return NULL_TREE;
6708 expr = decayed;
6710 expr = perform_qualification_conversions (type, expr);
6711 if (expr == error_mark_node)
6712 return error_mark_node;
6714 /* [temp.arg.nontype]/5, bullet 3
6716 For a non-type template-parameter of type reference to object, no
6717 conversions apply. The type referred to by the reference may be more
6718 cv-qualified than the (otherwise identical) type of the
6719 template-argument. The template-parameter is bound directly to the
6720 template-argument, which must be an lvalue. */
6721 else if (TYPE_REF_OBJ_P (type))
6723 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6724 expr_type))
6725 return error_mark_node;
6727 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6729 if (complain & tf_error)
6730 error ("%qE is not a valid template argument for type %qT "
6731 "because of conflicts in cv-qualification", expr, type);
6732 return NULL_TREE;
6735 if (!lvalue_p (expr))
6737 if (complain & tf_error)
6738 error ("%qE is not a valid template argument for type %qT "
6739 "because it is not an lvalue", expr, type);
6740 return NULL_TREE;
6743 /* [temp.arg.nontype]/1
6745 A template-argument for a non-type, non-template template-parameter
6746 shall be one of: [...]
6748 -- the address of an object or function with external linkage. */
6749 if (INDIRECT_REF_P (expr)
6750 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6752 expr = TREE_OPERAND (expr, 0);
6753 if (DECL_P (expr))
6755 if (complain & tf_error)
6756 error ("%q#D is not a valid template argument for type %qT "
6757 "because a reference variable does not have a constant "
6758 "address", expr, type);
6759 return NULL_TREE;
6763 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6764 && value_dependent_expression_p (expr))
6765 /* OK, dependent reference. We don't want to ask whether a DECL is
6766 itself value-dependent, since what we want here is its address. */;
6767 else
6769 if (!DECL_P (expr))
6771 if (complain & tf_error)
6772 error ("%qE is not a valid template argument for type %qT "
6773 "because it is not an object with linkage",
6774 expr, type);
6775 return NULL_TREE;
6778 /* DR 1155 allows internal linkage in C++11 and up. */
6779 linkage_kind linkage = decl_linkage (expr);
6780 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6782 if (complain & tf_error)
6783 error ("%qE is not a valid template argument for type %qT "
6784 "because object %qD does not have linkage",
6785 expr, type, expr);
6786 return NULL_TREE;
6789 expr = build_address (expr);
6792 if (!same_type_p (type, TREE_TYPE (expr)))
6793 expr = build_nop (type, expr);
6795 /* [temp.arg.nontype]/5, bullet 4
6797 For a non-type template-parameter of type pointer to function, only
6798 the function-to-pointer conversion (_conv.func_) is applied. If the
6799 template-argument represents a set of overloaded functions (or a
6800 pointer to such), the matching function is selected from the set
6801 (_over.over_). */
6802 else if (TYPE_PTRFN_P (type))
6804 /* If the argument is a template-id, we might not have enough
6805 context information to decay the pointer. */
6806 if (!type_unknown_p (expr_type))
6808 expr = decay_conversion (expr, complain);
6809 if (expr == error_mark_node)
6810 return error_mark_node;
6813 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6814 /* Null pointer values are OK in C++11. */
6815 return perform_qualification_conversions (type, expr);
6817 expr = convert_nontype_argument_function (type, expr, complain);
6818 if (!expr || expr == error_mark_node)
6819 return expr;
6821 /* [temp.arg.nontype]/5, bullet 5
6823 For a non-type template-parameter of type reference to function, no
6824 conversions apply. If the template-argument represents a set of
6825 overloaded functions, the matching function is selected from the set
6826 (_over.over_). */
6827 else if (TYPE_REFFN_P (type))
6829 if (TREE_CODE (expr) == ADDR_EXPR)
6831 if (complain & tf_error)
6833 error ("%qE is not a valid template argument for type %qT "
6834 "because it is a pointer", expr, type);
6835 inform (input_location, "try using %qE instead",
6836 TREE_OPERAND (expr, 0));
6838 return NULL_TREE;
6841 expr = convert_nontype_argument_function (type, expr, complain);
6842 if (!expr || expr == error_mark_node)
6843 return expr;
6845 /* [temp.arg.nontype]/5, bullet 6
6847 For a non-type template-parameter of type pointer to member function,
6848 no conversions apply. If the template-argument represents a set of
6849 overloaded member functions, the matching member function is selected
6850 from the set (_over.over_). */
6851 else if (TYPE_PTRMEMFUNC_P (type))
6853 expr = instantiate_type (type, expr, tf_none);
6854 if (expr == error_mark_node)
6855 return error_mark_node;
6857 /* [temp.arg.nontype] bullet 1 says the pointer to member
6858 expression must be a pointer-to-member constant. */
6859 if (!value_dependent_expression_p (expr)
6860 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6861 return NULL_TREE;
6863 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6864 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6865 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6866 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6868 /* [temp.arg.nontype]/5, bullet 7
6870 For a non-type template-parameter of type pointer to data member,
6871 qualification conversions (_conv.qual_) are applied. */
6872 else if (TYPE_PTRDATAMEM_P (type))
6874 /* [temp.arg.nontype] bullet 1 says the pointer to member
6875 expression must be a pointer-to-member constant. */
6876 if (!value_dependent_expression_p (expr)
6877 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6878 return NULL_TREE;
6880 expr = perform_qualification_conversions (type, expr);
6881 if (expr == error_mark_node)
6882 return expr;
6884 else if (NULLPTR_TYPE_P (type))
6886 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6888 if (complain & tf_error)
6889 error ("%qE is not a valid template argument for type %qT "
6890 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6891 return NULL_TREE;
6893 return expr;
6895 /* A template non-type parameter must be one of the above. */
6896 else
6897 gcc_unreachable ();
6899 /* Sanity check: did we actually convert the argument to the
6900 right type? */
6901 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6902 (type, TREE_TYPE (expr)));
6903 return convert_from_reference (expr);
6906 /* Subroutine of coerce_template_template_parms, which returns 1 if
6907 PARM_PARM and ARG_PARM match using the rule for the template
6908 parameters of template template parameters. Both PARM and ARG are
6909 template parameters; the rest of the arguments are the same as for
6910 coerce_template_template_parms.
6912 static int
6913 coerce_template_template_parm (tree parm,
6914 tree arg,
6915 tsubst_flags_t complain,
6916 tree in_decl,
6917 tree outer_args)
6919 if (arg == NULL_TREE || error_operand_p (arg)
6920 || parm == NULL_TREE || error_operand_p (parm))
6921 return 0;
6923 if (TREE_CODE (arg) != TREE_CODE (parm))
6924 return 0;
6926 switch (TREE_CODE (parm))
6928 case TEMPLATE_DECL:
6929 /* We encounter instantiations of templates like
6930 template <template <template <class> class> class TT>
6931 class C; */
6933 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6934 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6936 if (!coerce_template_template_parms
6937 (parmparm, argparm, complain, in_decl, outer_args))
6938 return 0;
6940 /* Fall through. */
6942 case TYPE_DECL:
6943 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6944 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6945 /* Argument is a parameter pack but parameter is not. */
6946 return 0;
6947 break;
6949 case PARM_DECL:
6950 /* The tsubst call is used to handle cases such as
6952 template <int> class C {};
6953 template <class T, template <T> class TT> class D {};
6954 D<int, C> d;
6956 i.e. the parameter list of TT depends on earlier parameters. */
6957 if (!uses_template_parms (TREE_TYPE (arg)))
6959 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6960 if (!uses_template_parms (t)
6961 && !same_type_p (t, TREE_TYPE (arg)))
6962 return 0;
6965 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6966 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6967 /* Argument is a parameter pack but parameter is not. */
6968 return 0;
6970 break;
6972 default:
6973 gcc_unreachable ();
6976 return 1;
6979 /* Coerce template argument list ARGLIST for use with template
6980 template-parameter TEMPL. */
6982 static tree
6983 coerce_template_args_for_ttp (tree templ, tree arglist,
6984 tsubst_flags_t complain)
6986 /* Consider an example where a template template parameter declared as
6988 template <class T, class U = std::allocator<T> > class TT
6990 The template parameter level of T and U are one level larger than
6991 of TT. To proper process the default argument of U, say when an
6992 instantiation `TT<int>' is seen, we need to build the full
6993 arguments containing {int} as the innermost level. Outer levels,
6994 available when not appearing as default template argument, can be
6995 obtained from the arguments of the enclosing template.
6997 Suppose that TT is later substituted with std::vector. The above
6998 instantiation is `TT<int, std::allocator<T> >' with TT at
6999 level 1, and T at level 2, while the template arguments at level 1
7000 becomes {std::vector} and the inner level 2 is {int}. */
7002 tree outer = DECL_CONTEXT (templ);
7003 if (outer)
7005 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7006 /* We want arguments for the partial specialization, not arguments for
7007 the primary template. */
7008 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7009 else
7010 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7012 else if (current_template_parms)
7014 /* This is an argument of the current template, so we haven't set
7015 DECL_CONTEXT yet. */
7016 tree relevant_template_parms;
7018 /* Parameter levels that are greater than the level of the given
7019 template template parm are irrelevant. */
7020 relevant_template_parms = current_template_parms;
7021 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7022 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7023 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7025 outer = template_parms_to_args (relevant_template_parms);
7028 if (outer)
7029 arglist = add_to_template_args (outer, arglist);
7031 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7032 return coerce_template_parms (parmlist, arglist, templ,
7033 complain,
7034 /*require_all_args=*/true,
7035 /*use_default_args=*/true);
7038 /* A cache of template template parameters with match-all default
7039 arguments. */
7040 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7041 static void
7042 store_defaulted_ttp (tree v, tree t)
7044 if (!defaulted_ttp_cache)
7045 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7046 defaulted_ttp_cache->put (v, t);
7048 static tree
7049 lookup_defaulted_ttp (tree v)
7051 if (defaulted_ttp_cache)
7052 if (tree *p = defaulted_ttp_cache->get (v))
7053 return *p;
7054 return NULL_TREE;
7057 /* T is a bound template template-parameter. Copy its arguments into default
7058 arguments of the template template-parameter's template parameters. */
7060 static tree
7061 add_defaults_to_ttp (tree otmpl)
7063 if (tree c = lookup_defaulted_ttp (otmpl))
7064 return c;
7066 tree ntmpl = copy_node (otmpl);
7068 tree ntype = copy_node (TREE_TYPE (otmpl));
7069 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7070 TYPE_MAIN_VARIANT (ntype) = ntype;
7071 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7072 TYPE_NAME (ntype) = ntmpl;
7073 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7075 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7076 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7077 TEMPLATE_PARM_DECL (idx) = ntmpl;
7078 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7080 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7081 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7082 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7083 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7084 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7086 tree o = TREE_VEC_ELT (vec, i);
7087 if (!template_parameter_pack_p (TREE_VALUE (o)))
7089 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7090 TREE_PURPOSE (n) = any_targ_node;
7094 store_defaulted_ttp (otmpl, ntmpl);
7095 return ntmpl;
7098 /* ARG is a bound potential template template-argument, and PARGS is a list
7099 of arguments for the corresponding template template-parameter. Adjust
7100 PARGS as appropriate for application to ARG's template, and if ARG is a
7101 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7102 arguments to the template template parameter. */
7104 static tree
7105 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7107 ++processing_template_decl;
7108 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7109 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7111 /* When comparing two template template-parameters in partial ordering,
7112 rewrite the one currently being used as an argument to have default
7113 arguments for all parameters. */
7114 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7115 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7116 if (pargs != error_mark_node)
7117 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7118 TYPE_TI_ARGS (arg));
7120 else
7122 tree aparms
7123 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7124 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7125 /*require_all*/true,
7126 /*use_default*/true);
7128 --processing_template_decl;
7129 return pargs;
7132 /* Subroutine of unify for the case when PARM is a
7133 BOUND_TEMPLATE_TEMPLATE_PARM. */
7135 static int
7136 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7137 bool explain_p)
7139 tree parmvec = TYPE_TI_ARGS (parm);
7140 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7142 /* The template template parm might be variadic and the argument
7143 not, so flatten both argument lists. */
7144 parmvec = expand_template_argument_pack (parmvec);
7145 argvec = expand_template_argument_pack (argvec);
7147 if (flag_new_ttp)
7149 /* In keeping with P0522R0, adjust P's template arguments
7150 to apply to A's template; then flatten it again. */
7151 tree nparmvec = parmvec;
7152 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7153 nparmvec = expand_template_argument_pack (nparmvec);
7155 if (unify (tparms, targs, nparmvec, argvec,
7156 UNIFY_ALLOW_NONE, explain_p))
7157 return 1;
7159 /* If the P0522 adjustment eliminated a pack expansion, deduce
7160 empty packs. */
7161 if (flag_new_ttp
7162 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7163 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7164 DEDUCE_EXACT, /*sub*/true, explain_p))
7165 return 1;
7167 else
7169 /* Deduce arguments T, i from TT<T> or TT<i>.
7170 We check each element of PARMVEC and ARGVEC individually
7171 rather than the whole TREE_VEC since they can have
7172 different number of elements, which is allowed under N2555. */
7174 int len = TREE_VEC_LENGTH (parmvec);
7176 /* Check if the parameters end in a pack, making them
7177 variadic. */
7178 int parm_variadic_p = 0;
7179 if (len > 0
7180 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7181 parm_variadic_p = 1;
7183 for (int i = 0; i < len - parm_variadic_p; ++i)
7184 /* If the template argument list of P contains a pack
7185 expansion that is not the last template argument, the
7186 entire template argument list is a non-deduced
7187 context. */
7188 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7189 return unify_success (explain_p);
7191 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7192 return unify_too_few_arguments (explain_p,
7193 TREE_VEC_LENGTH (argvec), len);
7195 for (int i = 0; i < len - parm_variadic_p; ++i)
7196 if (unify (tparms, targs,
7197 TREE_VEC_ELT (parmvec, i),
7198 TREE_VEC_ELT (argvec, i),
7199 UNIFY_ALLOW_NONE, explain_p))
7200 return 1;
7202 if (parm_variadic_p
7203 && unify_pack_expansion (tparms, targs,
7204 parmvec, argvec,
7205 DEDUCE_EXACT,
7206 /*subr=*/true, explain_p))
7207 return 1;
7210 return 0;
7213 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7214 template template parameters. Both PARM_PARMS and ARG_PARMS are
7215 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7216 or PARM_DECL.
7218 Consider the example:
7219 template <class T> class A;
7220 template<template <class U> class TT> class B;
7222 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7223 the parameters to A, and OUTER_ARGS contains A. */
7225 static int
7226 coerce_template_template_parms (tree parm_parms,
7227 tree arg_parms,
7228 tsubst_flags_t complain,
7229 tree in_decl,
7230 tree outer_args)
7232 int nparms, nargs, i;
7233 tree parm, arg;
7234 int variadic_p = 0;
7236 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7237 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7239 nparms = TREE_VEC_LENGTH (parm_parms);
7240 nargs = TREE_VEC_LENGTH (arg_parms);
7242 if (flag_new_ttp)
7244 /* P0522R0: A template template-parameter P is at least as specialized as
7245 a template template-argument A if, given the following rewrite to two
7246 function templates, the function template corresponding to P is at
7247 least as specialized as the function template corresponding to A
7248 according to the partial ordering rules for function templates
7249 ([temp.func.order]). Given an invented class template X with the
7250 template parameter list of A (including default arguments):
7252 * Each of the two function templates has the same template parameters,
7253 respectively, as P or A.
7255 * Each function template has a single function parameter whose type is
7256 a specialization of X with template arguments corresponding to the
7257 template parameters from the respective function template where, for
7258 each template parameter PP in the template parameter list of the
7259 function template, a corresponding template argument AA is formed. If
7260 PP declares a parameter pack, then AA is the pack expansion
7261 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7263 If the rewrite produces an invalid type, then P is not at least as
7264 specialized as A. */
7266 /* So coerce P's args to apply to A's parms, and then deduce between A's
7267 args and the converted args. If that succeeds, A is at least as
7268 specialized as P, so they match.*/
7269 tree pargs = template_parms_level_to_args (parm_parms);
7270 ++processing_template_decl;
7271 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7272 /*require_all*/true, /*use_default*/true);
7273 --processing_template_decl;
7274 if (pargs != error_mark_node)
7276 tree targs = make_tree_vec (nargs);
7277 tree aargs = template_parms_level_to_args (arg_parms);
7278 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7279 /*explain*/false))
7280 return 1;
7284 /* Determine whether we have a parameter pack at the end of the
7285 template template parameter's template parameter list. */
7286 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7288 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7290 if (error_operand_p (parm))
7291 return 0;
7293 switch (TREE_CODE (parm))
7295 case TEMPLATE_DECL:
7296 case TYPE_DECL:
7297 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7298 variadic_p = 1;
7299 break;
7301 case PARM_DECL:
7302 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7303 variadic_p = 1;
7304 break;
7306 default:
7307 gcc_unreachable ();
7311 if (nargs != nparms
7312 && !(variadic_p && nargs >= nparms - 1))
7313 return 0;
7315 /* Check all of the template parameters except the parameter pack at
7316 the end (if any). */
7317 for (i = 0; i < nparms - variadic_p; ++i)
7319 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7320 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7321 continue;
7323 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7324 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7326 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7327 outer_args))
7328 return 0;
7332 if (variadic_p)
7334 /* Check each of the template parameters in the template
7335 argument against the template parameter pack at the end of
7336 the template template parameter. */
7337 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7338 return 0;
7340 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7342 for (; i < nargs; ++i)
7344 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7345 continue;
7347 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7349 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7350 outer_args))
7351 return 0;
7355 return 1;
7358 /* Verifies that the deduced template arguments (in TARGS) for the
7359 template template parameters (in TPARMS) represent valid bindings,
7360 by comparing the template parameter list of each template argument
7361 to the template parameter list of its corresponding template
7362 template parameter, in accordance with DR150. This
7363 routine can only be called after all template arguments have been
7364 deduced. It will return TRUE if all of the template template
7365 parameter bindings are okay, FALSE otherwise. */
7366 bool
7367 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7369 int i, ntparms = TREE_VEC_LENGTH (tparms);
7370 bool ret = true;
7372 /* We're dealing with template parms in this process. */
7373 ++processing_template_decl;
7375 targs = INNERMOST_TEMPLATE_ARGS (targs);
7377 for (i = 0; i < ntparms; ++i)
7379 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7380 tree targ = TREE_VEC_ELT (targs, i);
7382 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7384 tree packed_args = NULL_TREE;
7385 int idx, len = 1;
7387 if (ARGUMENT_PACK_P (targ))
7389 /* Look inside the argument pack. */
7390 packed_args = ARGUMENT_PACK_ARGS (targ);
7391 len = TREE_VEC_LENGTH (packed_args);
7394 for (idx = 0; idx < len; ++idx)
7396 tree targ_parms = NULL_TREE;
7398 if (packed_args)
7399 /* Extract the next argument from the argument
7400 pack. */
7401 targ = TREE_VEC_ELT (packed_args, idx);
7403 if (PACK_EXPANSION_P (targ))
7404 /* Look at the pattern of the pack expansion. */
7405 targ = PACK_EXPANSION_PATTERN (targ);
7407 /* Extract the template parameters from the template
7408 argument. */
7409 if (TREE_CODE (targ) == TEMPLATE_DECL)
7410 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7411 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7412 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7414 /* Verify that we can coerce the template template
7415 parameters from the template argument to the template
7416 parameter. This requires an exact match. */
7417 if (targ_parms
7418 && !coerce_template_template_parms
7419 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7420 targ_parms,
7421 tf_none,
7422 tparm,
7423 targs))
7425 ret = false;
7426 goto out;
7432 out:
7434 --processing_template_decl;
7435 return ret;
7438 /* Since type attributes aren't mangled, we need to strip them from
7439 template type arguments. */
7441 static tree
7442 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7444 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7445 return arg;
7446 bool removed_attributes = false;
7447 tree canon = strip_typedefs (arg, &removed_attributes);
7448 if (removed_attributes
7449 && (complain & tf_warning))
7450 warning (OPT_Wignored_attributes,
7451 "ignoring attributes on template argument %qT", arg);
7452 return canon;
7455 /* And from inside dependent non-type arguments like sizeof(Type). */
7457 static tree
7458 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7460 if (!arg || arg == error_mark_node)
7461 return arg;
7462 bool removed_attributes = false;
7463 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7464 if (removed_attributes
7465 && (complain & tf_warning))
7466 warning (OPT_Wignored_attributes,
7467 "ignoring attributes in template argument %qE", arg);
7468 return canon;
7471 // A template declaration can be substituted for a constrained
7472 // template template parameter only when the argument is more
7473 // constrained than the parameter.
7474 static bool
7475 is_compatible_template_arg (tree parm, tree arg)
7477 tree parm_cons = get_constraints (parm);
7479 /* For now, allow constrained template template arguments
7480 and unconstrained template template parameters. */
7481 if (parm_cons == NULL_TREE)
7482 return true;
7484 tree arg_cons = get_constraints (arg);
7486 // If the template parameter is constrained, we need to rewrite its
7487 // constraints in terms of the ARG's template parameters. This ensures
7488 // that all of the template parameter types will have the same depth.
7490 // Note that this is only valid when coerce_template_template_parm is
7491 // true for the innermost template parameters of PARM and ARG. In other
7492 // words, because coercion is successful, this conversion will be valid.
7493 if (parm_cons)
7495 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7496 parm_cons = tsubst_constraint_info (parm_cons,
7497 INNERMOST_TEMPLATE_ARGS (args),
7498 tf_none, NULL_TREE);
7499 if (parm_cons == error_mark_node)
7500 return false;
7503 return subsumes (parm_cons, arg_cons);
7506 // Convert a placeholder argument into a binding to the original
7507 // parameter. The original parameter is saved as the TREE_TYPE of
7508 // ARG.
7509 static inline tree
7510 convert_wildcard_argument (tree parm, tree arg)
7512 TREE_TYPE (arg) = parm;
7513 return arg;
7516 /* Convert the indicated template ARG as necessary to match the
7517 indicated template PARM. Returns the converted ARG, or
7518 error_mark_node if the conversion was unsuccessful. Error and
7519 warning messages are issued under control of COMPLAIN. This
7520 conversion is for the Ith parameter in the parameter list. ARGS is
7521 the full set of template arguments deduced so far. */
7523 static tree
7524 convert_template_argument (tree parm,
7525 tree arg,
7526 tree args,
7527 tsubst_flags_t complain,
7528 int i,
7529 tree in_decl)
7531 tree orig_arg;
7532 tree val;
7533 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7535 if (parm == error_mark_node)
7536 return error_mark_node;
7538 /* Trivially convert placeholders. */
7539 if (TREE_CODE (arg) == WILDCARD_DECL)
7540 return convert_wildcard_argument (parm, arg);
7542 if (arg == any_targ_node)
7543 return arg;
7545 if (TREE_CODE (arg) == TREE_LIST
7546 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7548 /* The template argument was the name of some
7549 member function. That's usually
7550 invalid, but static members are OK. In any
7551 case, grab the underlying fields/functions
7552 and issue an error later if required. */
7553 orig_arg = TREE_VALUE (arg);
7554 TREE_TYPE (arg) = unknown_type_node;
7557 orig_arg = arg;
7559 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7560 requires_type = (TREE_CODE (parm) == TYPE_DECL
7561 || requires_tmpl_type);
7563 /* When determining whether an argument pack expansion is a template,
7564 look at the pattern. */
7565 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7566 arg = PACK_EXPANSION_PATTERN (arg);
7568 /* Deal with an injected-class-name used as a template template arg. */
7569 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7571 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7572 if (TREE_CODE (t) == TEMPLATE_DECL)
7574 if (cxx_dialect >= cxx11)
7575 /* OK under DR 1004. */;
7576 else if (complain & tf_warning_or_error)
7577 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7578 " used as template template argument", TYPE_NAME (arg));
7579 else if (flag_pedantic_errors)
7580 t = arg;
7582 arg = t;
7586 is_tmpl_type =
7587 ((TREE_CODE (arg) == TEMPLATE_DECL
7588 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7589 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7590 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7591 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7593 if (is_tmpl_type
7594 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7595 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7596 arg = TYPE_STUB_DECL (arg);
7598 is_type = TYPE_P (arg) || is_tmpl_type;
7600 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7601 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7603 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7605 if (complain & tf_error)
7606 error ("invalid use of destructor %qE as a type", orig_arg);
7607 return error_mark_node;
7610 permerror (input_location,
7611 "to refer to a type member of a template parameter, "
7612 "use %<typename %E%>", orig_arg);
7614 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7615 TREE_OPERAND (arg, 1),
7616 typename_type,
7617 complain);
7618 arg = orig_arg;
7619 is_type = 1;
7621 if (is_type != requires_type)
7623 if (in_decl)
7625 if (complain & tf_error)
7627 error ("type/value mismatch at argument %d in template "
7628 "parameter list for %qD",
7629 i + 1, in_decl);
7630 if (is_type)
7631 inform (input_location,
7632 " expected a constant of type %qT, got %qT",
7633 TREE_TYPE (parm),
7634 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7635 else if (requires_tmpl_type)
7636 inform (input_location,
7637 " expected a class template, got %qE", orig_arg);
7638 else
7639 inform (input_location,
7640 " expected a type, got %qE", orig_arg);
7643 return error_mark_node;
7645 if (is_tmpl_type ^ requires_tmpl_type)
7647 if (in_decl && (complain & tf_error))
7649 error ("type/value mismatch at argument %d in template "
7650 "parameter list for %qD",
7651 i + 1, in_decl);
7652 if (is_tmpl_type)
7653 inform (input_location,
7654 " expected a type, got %qT", DECL_NAME (arg));
7655 else
7656 inform (input_location,
7657 " expected a class template, got %qT", orig_arg);
7659 return error_mark_node;
7662 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7663 /* We already did the appropriate conversion when packing args. */
7664 val = orig_arg;
7665 else if (is_type)
7667 if (requires_tmpl_type)
7669 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7670 /* The number of argument required is not known yet.
7671 Just accept it for now. */
7672 val = orig_arg;
7673 else
7675 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7676 tree argparm;
7678 /* Strip alias templates that are equivalent to another
7679 template. */
7680 arg = get_underlying_template (arg);
7681 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7683 if (coerce_template_template_parms (parmparm, argparm,
7684 complain, in_decl,
7685 args))
7687 val = arg;
7689 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7690 TEMPLATE_DECL. */
7691 if (val != error_mark_node)
7693 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7694 val = TREE_TYPE (val);
7695 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7696 val = make_pack_expansion (val);
7699 else
7701 if (in_decl && (complain & tf_error))
7703 error ("type/value mismatch at argument %d in "
7704 "template parameter list for %qD",
7705 i + 1, in_decl);
7706 inform (input_location,
7707 " expected a template of type %qD, got %qT",
7708 parm, orig_arg);
7711 val = error_mark_node;
7714 // Check that the constraints are compatible before allowing the
7715 // substitution.
7716 if (val != error_mark_node)
7717 if (!is_compatible_template_arg (parm, arg))
7719 if (in_decl && (complain & tf_error))
7721 error ("constraint mismatch at argument %d in "
7722 "template parameter list for %qD",
7723 i + 1, in_decl);
7724 inform (input_location, " expected %qD but got %qD",
7725 parm, arg);
7727 val = error_mark_node;
7731 else
7732 val = orig_arg;
7733 /* We only form one instance of each template specialization.
7734 Therefore, if we use a non-canonical variant (i.e., a
7735 typedef), any future messages referring to the type will use
7736 the typedef, which is confusing if those future uses do not
7737 themselves also use the typedef. */
7738 if (TYPE_P (val))
7739 val = canonicalize_type_argument (val, complain);
7741 else
7743 tree t = TREE_TYPE (parm);
7745 if (tree a = type_uses_auto (t))
7747 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7748 if (t == error_mark_node)
7749 return error_mark_node;
7751 else
7752 t = tsubst (t, args, complain, in_decl);
7754 if (invalid_nontype_parm_type_p (t, complain))
7755 return error_mark_node;
7757 if (!type_dependent_expression_p (orig_arg)
7758 && !uses_template_parms (t))
7759 /* We used to call digest_init here. However, digest_init
7760 will report errors, which we don't want when complain
7761 is zero. More importantly, digest_init will try too
7762 hard to convert things: for example, `0' should not be
7763 converted to pointer type at this point according to
7764 the standard. Accepting this is not merely an
7765 extension, since deciding whether or not these
7766 conversions can occur is part of determining which
7767 function template to call, or whether a given explicit
7768 argument specification is valid. */
7769 val = convert_nontype_argument (t, orig_arg, complain);
7770 else
7771 val = canonicalize_expr_argument (orig_arg, complain);
7773 if (val == NULL_TREE)
7774 val = error_mark_node;
7775 else if (val == error_mark_node && (complain & tf_error))
7776 error ("could not convert template argument %qE from %qT to %qT",
7777 orig_arg, TREE_TYPE (orig_arg), t);
7779 if (INDIRECT_REF_P (val))
7781 /* Reject template arguments that are references to built-in
7782 functions with no library fallbacks. */
7783 const_tree inner = TREE_OPERAND (val, 0);
7784 const_tree innertype = TREE_TYPE (inner);
7785 if (innertype
7786 && TREE_CODE (innertype) == REFERENCE_TYPE
7787 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7788 && 0 < TREE_OPERAND_LENGTH (inner)
7789 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7790 return error_mark_node;
7793 if (TREE_CODE (val) == SCOPE_REF)
7795 /* Strip typedefs from the SCOPE_REF. */
7796 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7797 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7798 complain);
7799 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7800 QUALIFIED_NAME_IS_TEMPLATE (val));
7804 return val;
7807 /* Coerces the remaining template arguments in INNER_ARGS (from
7808 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7809 Returns the coerced argument pack. PARM_IDX is the position of this
7810 parameter in the template parameter list. ARGS is the original
7811 template argument list. */
7812 static tree
7813 coerce_template_parameter_pack (tree parms,
7814 int parm_idx,
7815 tree args,
7816 tree inner_args,
7817 int arg_idx,
7818 tree new_args,
7819 int* lost,
7820 tree in_decl,
7821 tsubst_flags_t complain)
7823 tree parm = TREE_VEC_ELT (parms, parm_idx);
7824 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7825 tree packed_args;
7826 tree argument_pack;
7827 tree packed_parms = NULL_TREE;
7829 if (arg_idx > nargs)
7830 arg_idx = nargs;
7832 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7834 /* When the template parameter is a non-type template parameter pack
7835 or template template parameter pack whose type or template
7836 parameters use parameter packs, we know exactly how many arguments
7837 we are looking for. Build a vector of the instantiated decls for
7838 these template parameters in PACKED_PARMS. */
7839 /* We can't use make_pack_expansion here because it would interpret a
7840 _DECL as a use rather than a declaration. */
7841 tree decl = TREE_VALUE (parm);
7842 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7843 SET_PACK_EXPANSION_PATTERN (exp, decl);
7844 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7845 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7847 TREE_VEC_LENGTH (args)--;
7848 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7849 TREE_VEC_LENGTH (args)++;
7851 if (packed_parms == error_mark_node)
7852 return error_mark_node;
7854 /* If we're doing a partial instantiation of a member template,
7855 verify that all of the types used for the non-type
7856 template parameter pack are, in fact, valid for non-type
7857 template parameters. */
7858 if (arg_idx < nargs
7859 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7861 int j, len = TREE_VEC_LENGTH (packed_parms);
7862 for (j = 0; j < len; ++j)
7864 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7865 if (invalid_nontype_parm_type_p (t, complain))
7866 return error_mark_node;
7868 /* We don't know how many args we have yet, just
7869 use the unconverted ones for now. */
7870 return NULL_TREE;
7873 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7875 /* Check if we have a placeholder pack, which indicates we're
7876 in the context of a introduction list. In that case we want
7877 to match this pack to the single placeholder. */
7878 else if (arg_idx < nargs
7879 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7880 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7882 nargs = arg_idx + 1;
7883 packed_args = make_tree_vec (1);
7885 else
7886 packed_args = make_tree_vec (nargs - arg_idx);
7888 /* Convert the remaining arguments, which will be a part of the
7889 parameter pack "parm". */
7890 int first_pack_arg = arg_idx;
7891 for (; arg_idx < nargs; ++arg_idx)
7893 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7894 tree actual_parm = TREE_VALUE (parm);
7895 int pack_idx = arg_idx - first_pack_arg;
7897 if (packed_parms)
7899 /* Once we've packed as many args as we have types, stop. */
7900 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7901 break;
7902 else if (PACK_EXPANSION_P (arg))
7903 /* We don't know how many args we have yet, just
7904 use the unconverted ones for now. */
7905 return NULL_TREE;
7906 else
7907 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7910 if (arg == error_mark_node)
7912 if (complain & tf_error)
7913 error ("template argument %d is invalid", arg_idx + 1);
7915 else
7916 arg = convert_template_argument (actual_parm,
7917 arg, new_args, complain, parm_idx,
7918 in_decl);
7919 if (arg == error_mark_node)
7920 (*lost)++;
7921 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7924 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7925 && TREE_VEC_LENGTH (packed_args) > 0)
7927 if (complain & tf_error)
7928 error ("wrong number of template arguments (%d, should be %d)",
7929 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7930 return error_mark_node;
7933 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7934 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7935 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7936 else
7938 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7939 TREE_CONSTANT (argument_pack) = 1;
7942 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7943 if (CHECKING_P)
7944 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7945 TREE_VEC_LENGTH (packed_args));
7946 return argument_pack;
7949 /* Returns the number of pack expansions in the template argument vector
7950 ARGS. */
7952 static int
7953 pack_expansion_args_count (tree args)
7955 int i;
7956 int count = 0;
7957 if (args)
7958 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7960 tree elt = TREE_VEC_ELT (args, i);
7961 if (elt && PACK_EXPANSION_P (elt))
7962 ++count;
7964 return count;
7967 /* Convert all template arguments to their appropriate types, and
7968 return a vector containing the innermost resulting template
7969 arguments. If any error occurs, return error_mark_node. Error and
7970 warning messages are issued under control of COMPLAIN.
7972 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7973 for arguments not specified in ARGS. Otherwise, if
7974 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7975 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7976 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7977 ARGS. */
7979 static tree
7980 coerce_template_parms (tree parms,
7981 tree args,
7982 tree in_decl,
7983 tsubst_flags_t complain,
7984 bool require_all_args,
7985 bool use_default_args)
7987 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7988 tree orig_inner_args;
7989 tree inner_args;
7990 tree new_args;
7991 tree new_inner_args;
7992 int saved_unevaluated_operand;
7993 int saved_inhibit_evaluation_warnings;
7995 /* When used as a boolean value, indicates whether this is a
7996 variadic template parameter list. Since it's an int, we can also
7997 subtract it from nparms to get the number of non-variadic
7998 parameters. */
7999 int variadic_p = 0;
8000 int variadic_args_p = 0;
8001 int post_variadic_parms = 0;
8003 /* Likewise for parameters with default arguments. */
8004 int default_p = 0;
8006 if (args == error_mark_node)
8007 return error_mark_node;
8009 nparms = TREE_VEC_LENGTH (parms);
8011 /* Determine if there are any parameter packs or default arguments. */
8012 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8014 tree parm = TREE_VEC_ELT (parms, parm_idx);
8015 if (variadic_p)
8016 ++post_variadic_parms;
8017 if (template_parameter_pack_p (TREE_VALUE (parm)))
8018 ++variadic_p;
8019 if (TREE_PURPOSE (parm))
8020 ++default_p;
8023 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8024 /* If there are no parameters that follow a parameter pack, we need to
8025 expand any argument packs so that we can deduce a parameter pack from
8026 some non-packed args followed by an argument pack, as in variadic85.C.
8027 If there are such parameters, we need to leave argument packs intact
8028 so the arguments are assigned properly. This can happen when dealing
8029 with a nested class inside a partial specialization of a class
8030 template, as in variadic92.C, or when deducing a template parameter pack
8031 from a sub-declarator, as in variadic114.C. */
8032 if (!post_variadic_parms)
8033 inner_args = expand_template_argument_pack (inner_args);
8035 /* Count any pack expansion args. */
8036 variadic_args_p = pack_expansion_args_count (inner_args);
8038 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8039 if ((nargs - variadic_args_p > nparms && !variadic_p)
8040 || (nargs < nparms - variadic_p
8041 && require_all_args
8042 && !variadic_args_p
8043 && (!use_default_args
8044 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8045 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8047 if (complain & tf_error)
8049 if (variadic_p || default_p)
8051 nparms -= variadic_p + default_p;
8052 error ("wrong number of template arguments "
8053 "(%d, should be at least %d)", nargs, nparms);
8055 else
8056 error ("wrong number of template arguments "
8057 "(%d, should be %d)", nargs, nparms);
8059 if (in_decl)
8060 inform (DECL_SOURCE_LOCATION (in_decl),
8061 "provided for %qD", in_decl);
8064 return error_mark_node;
8066 /* We can't pass a pack expansion to a non-pack parameter of an alias
8067 template (DR 1430). */
8068 else if (in_decl
8069 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8070 || concept_template_p (in_decl))
8071 && variadic_args_p
8072 && nargs - variadic_args_p < nparms - variadic_p)
8074 if (complain & tf_error)
8076 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8078 tree arg = TREE_VEC_ELT (inner_args, i);
8079 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8081 if (PACK_EXPANSION_P (arg)
8082 && !template_parameter_pack_p (parm))
8084 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8085 error_at (location_of (arg),
8086 "pack expansion argument for non-pack parameter "
8087 "%qD of alias template %qD", parm, in_decl);
8088 else
8089 error_at (location_of (arg),
8090 "pack expansion argument for non-pack parameter "
8091 "%qD of concept %qD", parm, in_decl);
8092 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8093 goto found;
8096 gcc_unreachable ();
8097 found:;
8099 return error_mark_node;
8102 /* We need to evaluate the template arguments, even though this
8103 template-id may be nested within a "sizeof". */
8104 saved_unevaluated_operand = cp_unevaluated_operand;
8105 cp_unevaluated_operand = 0;
8106 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8107 c_inhibit_evaluation_warnings = 0;
8108 new_inner_args = make_tree_vec (nparms);
8109 new_args = add_outermost_template_args (args, new_inner_args);
8110 int pack_adjust = 0;
8111 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8113 tree arg;
8114 tree parm;
8116 /* Get the Ith template parameter. */
8117 parm = TREE_VEC_ELT (parms, parm_idx);
8119 if (parm == error_mark_node)
8121 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8122 continue;
8125 /* Calculate the next argument. */
8126 if (arg_idx < nargs)
8127 arg = TREE_VEC_ELT (inner_args, arg_idx);
8128 else
8129 arg = NULL_TREE;
8131 if (template_parameter_pack_p (TREE_VALUE (parm))
8132 && !(arg && ARGUMENT_PACK_P (arg)))
8134 /* Some arguments will be placed in the
8135 template parameter pack PARM. */
8136 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8137 inner_args, arg_idx,
8138 new_args, &lost,
8139 in_decl, complain);
8141 if (arg == NULL_TREE)
8143 /* We don't know how many args we have yet, just use the
8144 unconverted (and still packed) ones for now. */
8145 new_inner_args = orig_inner_args;
8146 arg_idx = nargs;
8147 break;
8150 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8152 /* Store this argument. */
8153 if (arg == error_mark_node)
8155 lost++;
8156 /* We are done with all of the arguments. */
8157 arg_idx = nargs;
8159 else
8161 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8162 arg_idx += pack_adjust;
8165 continue;
8167 else if (arg)
8169 if (PACK_EXPANSION_P (arg))
8171 /* "If every valid specialization of a variadic template
8172 requires an empty template parameter pack, the template is
8173 ill-formed, no diagnostic required." So check that the
8174 pattern works with this parameter. */
8175 tree pattern = PACK_EXPANSION_PATTERN (arg);
8176 tree conv = convert_template_argument (TREE_VALUE (parm),
8177 pattern, new_args,
8178 complain, parm_idx,
8179 in_decl);
8180 if (conv == error_mark_node)
8182 if (complain & tf_error)
8183 inform (input_location, "so any instantiation with a "
8184 "non-empty parameter pack would be ill-formed");
8185 ++lost;
8187 else if (TYPE_P (conv) && !TYPE_P (pattern))
8188 /* Recover from missing typename. */
8189 TREE_VEC_ELT (inner_args, arg_idx)
8190 = make_pack_expansion (conv);
8192 /* We don't know how many args we have yet, just
8193 use the unconverted ones for now. */
8194 new_inner_args = inner_args;
8195 arg_idx = nargs;
8196 break;
8199 else if (require_all_args)
8201 /* There must be a default arg in this case. */
8202 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8203 complain, in_decl);
8204 /* The position of the first default template argument,
8205 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8206 Record that. */
8207 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8208 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8209 arg_idx - pack_adjust);
8211 else
8212 break;
8214 if (arg == error_mark_node)
8216 if (complain & tf_error)
8217 error ("template argument %d is invalid", arg_idx + 1);
8219 else if (!arg)
8220 /* This only occurs if there was an error in the template
8221 parameter list itself (which we would already have
8222 reported) that we are trying to recover from, e.g., a class
8223 template with a parameter list such as
8224 template<typename..., typename>. */
8225 ++lost;
8226 else
8227 arg = convert_template_argument (TREE_VALUE (parm),
8228 arg, new_args, complain,
8229 parm_idx, in_decl);
8231 if (arg == error_mark_node)
8232 lost++;
8233 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8235 cp_unevaluated_operand = saved_unevaluated_operand;
8236 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8238 if (variadic_p && arg_idx < nargs)
8240 if (complain & tf_error)
8242 error ("wrong number of template arguments "
8243 "(%d, should be %d)", nargs, arg_idx);
8244 if (in_decl)
8245 error ("provided for %q+D", in_decl);
8247 return error_mark_node;
8250 if (lost)
8251 return error_mark_node;
8253 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8254 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8255 TREE_VEC_LENGTH (new_inner_args));
8257 return new_inner_args;
8260 /* Convert all template arguments to their appropriate types, and
8261 return a vector containing the innermost resulting template
8262 arguments. If any error occurs, return error_mark_node. Error and
8263 warning messages are not issued.
8265 Note that no function argument deduction is performed, and default
8266 arguments are used to fill in unspecified arguments. */
8267 tree
8268 coerce_template_parms (tree parms, tree args, tree in_decl)
8270 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8273 /* Convert all template arguments to their appropriate type, and
8274 instantiate default arguments as needed. This returns a vector
8275 containing the innermost resulting template arguments, or
8276 error_mark_node if unsuccessful. */
8277 tree
8278 coerce_template_parms (tree parms, tree args, tree in_decl,
8279 tsubst_flags_t complain)
8281 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8284 /* Like coerce_template_parms. If PARMS represents all template
8285 parameters levels, this function returns a vector of vectors
8286 representing all the resulting argument levels. Note that in this
8287 case, only the innermost arguments are coerced because the
8288 outermost ones are supposed to have been coerced already.
8290 Otherwise, if PARMS represents only (the innermost) vector of
8291 parameters, this function returns a vector containing just the
8292 innermost resulting arguments. */
8294 static tree
8295 coerce_innermost_template_parms (tree parms,
8296 tree args,
8297 tree in_decl,
8298 tsubst_flags_t complain,
8299 bool require_all_args,
8300 bool use_default_args)
8302 int parms_depth = TMPL_PARMS_DEPTH (parms);
8303 int args_depth = TMPL_ARGS_DEPTH (args);
8304 tree coerced_args;
8306 if (parms_depth > 1)
8308 coerced_args = make_tree_vec (parms_depth);
8309 tree level;
8310 int cur_depth;
8312 for (level = parms, cur_depth = parms_depth;
8313 parms_depth > 0 && level != NULL_TREE;
8314 level = TREE_CHAIN (level), --cur_depth)
8316 tree l;
8317 if (cur_depth == args_depth)
8318 l = coerce_template_parms (TREE_VALUE (level),
8319 args, in_decl, complain,
8320 require_all_args,
8321 use_default_args);
8322 else
8323 l = TMPL_ARGS_LEVEL (args, cur_depth);
8325 if (l == error_mark_node)
8326 return error_mark_node;
8328 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8331 else
8332 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8333 args, in_decl, complain,
8334 require_all_args,
8335 use_default_args);
8336 return coerced_args;
8339 /* Returns 1 if template args OT and NT are equivalent. */
8342 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8344 if (nt == ot)
8345 return 1;
8346 if (nt == NULL_TREE || ot == NULL_TREE)
8347 return false;
8348 if (nt == any_targ_node || ot == any_targ_node)
8349 return true;
8351 if (TREE_CODE (nt) == TREE_VEC)
8352 /* For member templates */
8353 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8354 else if (PACK_EXPANSION_P (ot))
8355 return (PACK_EXPANSION_P (nt)
8356 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8357 PACK_EXPANSION_PATTERN (nt))
8358 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8359 PACK_EXPANSION_EXTRA_ARGS (nt)));
8360 else if (ARGUMENT_PACK_P (ot))
8362 int i, len;
8363 tree opack, npack;
8365 if (!ARGUMENT_PACK_P (nt))
8366 return 0;
8368 opack = ARGUMENT_PACK_ARGS (ot);
8369 npack = ARGUMENT_PACK_ARGS (nt);
8370 len = TREE_VEC_LENGTH (opack);
8371 if (TREE_VEC_LENGTH (npack) != len)
8372 return 0;
8373 for (i = 0; i < len; ++i)
8374 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8375 TREE_VEC_ELT (npack, i)))
8376 return 0;
8377 return 1;
8379 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8380 gcc_unreachable ();
8381 else if (TYPE_P (nt))
8383 if (!TYPE_P (ot))
8384 return false;
8385 /* Don't treat an alias template specialization with dependent
8386 arguments as equivalent to its underlying type when used as a
8387 template argument; we need them to be distinct so that we
8388 substitute into the specialization arguments at instantiation
8389 time. And aliases can't be equivalent without being ==, so
8390 we don't need to look any deeper.
8392 During partial ordering, however, we need to treat them normally so
8393 that we can order uses of the same alias with different
8394 cv-qualification (79960). */
8395 if (!partial_order
8396 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8397 return false;
8398 else
8399 return same_type_p (ot, nt);
8401 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8402 return 0;
8403 else
8405 /* Try to treat a template non-type argument that has been converted
8406 to the parameter type as equivalent to one that hasn't yet. */
8407 for (enum tree_code code1 = TREE_CODE (ot);
8408 CONVERT_EXPR_CODE_P (code1)
8409 || code1 == NON_LVALUE_EXPR;
8410 code1 = TREE_CODE (ot))
8411 ot = TREE_OPERAND (ot, 0);
8412 for (enum tree_code code2 = TREE_CODE (nt);
8413 CONVERT_EXPR_CODE_P (code2)
8414 || code2 == NON_LVALUE_EXPR;
8415 code2 = TREE_CODE (nt))
8416 nt = TREE_OPERAND (nt, 0);
8418 return cp_tree_equal (ot, nt);
8422 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8423 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8424 NEWARG_PTR with the offending arguments if they are non-NULL. */
8427 comp_template_args (tree oldargs, tree newargs,
8428 tree *oldarg_ptr, tree *newarg_ptr,
8429 bool partial_order)
8431 int i;
8433 if (oldargs == newargs)
8434 return 1;
8436 if (!oldargs || !newargs)
8437 return 0;
8439 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8440 return 0;
8442 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8444 tree nt = TREE_VEC_ELT (newargs, i);
8445 tree ot = TREE_VEC_ELT (oldargs, i);
8447 if (! template_args_equal (ot, nt, partial_order))
8449 if (oldarg_ptr != NULL)
8450 *oldarg_ptr = ot;
8451 if (newarg_ptr != NULL)
8452 *newarg_ptr = nt;
8453 return 0;
8456 return 1;
8459 inline bool
8460 comp_template_args_porder (tree oargs, tree nargs)
8462 return comp_template_args (oargs, nargs, NULL, NULL, true);
8465 static void
8466 add_pending_template (tree d)
8468 tree ti = (TYPE_P (d)
8469 ? CLASSTYPE_TEMPLATE_INFO (d)
8470 : DECL_TEMPLATE_INFO (d));
8471 struct pending_template *pt;
8472 int level;
8474 if (TI_PENDING_TEMPLATE_FLAG (ti))
8475 return;
8477 /* We are called both from instantiate_decl, where we've already had a
8478 tinst_level pushed, and instantiate_template, where we haven't.
8479 Compensate. */
8480 level = !current_tinst_level || current_tinst_level->decl != d;
8482 if (level)
8483 push_tinst_level (d);
8485 pt = ggc_alloc<pending_template> ();
8486 pt->next = NULL;
8487 pt->tinst = current_tinst_level;
8488 if (last_pending_template)
8489 last_pending_template->next = pt;
8490 else
8491 pending_templates = pt;
8493 last_pending_template = pt;
8495 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8497 if (level)
8498 pop_tinst_level ();
8502 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8503 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8504 documentation for TEMPLATE_ID_EXPR. */
8506 tree
8507 lookup_template_function (tree fns, tree arglist)
8509 tree type;
8511 if (fns == error_mark_node || arglist == error_mark_node)
8512 return error_mark_node;
8514 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8516 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8518 error ("%q#D is not a function template", fns);
8519 return error_mark_node;
8522 if (BASELINK_P (fns))
8524 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8525 unknown_type_node,
8526 BASELINK_FUNCTIONS (fns),
8527 arglist);
8528 return fns;
8531 type = TREE_TYPE (fns);
8532 if (TREE_CODE (fns) == OVERLOAD || !type)
8533 type = unknown_type_node;
8535 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8538 /* Within the scope of a template class S<T>, the name S gets bound
8539 (in build_self_reference) to a TYPE_DECL for the class, not a
8540 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8541 or one of its enclosing classes, and that type is a template,
8542 return the associated TEMPLATE_DECL. Otherwise, the original
8543 DECL is returned.
8545 Also handle the case when DECL is a TREE_LIST of ambiguous
8546 injected-class-names from different bases. */
8548 tree
8549 maybe_get_template_decl_from_type_decl (tree decl)
8551 if (decl == NULL_TREE)
8552 return decl;
8554 /* DR 176: A lookup that finds an injected-class-name (10.2
8555 [class.member.lookup]) can result in an ambiguity in certain cases
8556 (for example, if it is found in more than one base class). If all of
8557 the injected-class-names that are found refer to specializations of
8558 the same class template, and if the name is followed by a
8559 template-argument-list, the reference refers to the class template
8560 itself and not a specialization thereof, and is not ambiguous. */
8561 if (TREE_CODE (decl) == TREE_LIST)
8563 tree t, tmpl = NULL_TREE;
8564 for (t = decl; t; t = TREE_CHAIN (t))
8566 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8567 if (!tmpl)
8568 tmpl = elt;
8569 else if (tmpl != elt)
8570 break;
8572 if (tmpl && t == NULL_TREE)
8573 return tmpl;
8574 else
8575 return decl;
8578 return (decl != NULL_TREE
8579 && DECL_SELF_REFERENCE_P (decl)
8580 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8581 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8584 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8585 parameters, find the desired type.
8587 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8589 IN_DECL, if non-NULL, is the template declaration we are trying to
8590 instantiate.
8592 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8593 the class we are looking up.
8595 Issue error and warning messages under control of COMPLAIN.
8597 If the template class is really a local class in a template
8598 function, then the FUNCTION_CONTEXT is the function in which it is
8599 being instantiated.
8601 ??? Note that this function is currently called *twice* for each
8602 template-id: the first time from the parser, while creating the
8603 incomplete type (finish_template_type), and the second type during the
8604 real instantiation (instantiate_template_class). This is surely something
8605 that we want to avoid. It also causes some problems with argument
8606 coercion (see convert_nontype_argument for more information on this). */
8608 static tree
8609 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8610 int entering_scope, tsubst_flags_t complain)
8612 tree templ = NULL_TREE, parmlist;
8613 tree t;
8614 spec_entry **slot;
8615 spec_entry *entry;
8616 spec_entry elt;
8617 hashval_t hash;
8619 if (identifier_p (d1))
8621 tree value = innermost_non_namespace_value (d1);
8622 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8623 templ = value;
8624 else
8626 if (context)
8627 push_decl_namespace (context);
8628 templ = lookup_name (d1);
8629 templ = maybe_get_template_decl_from_type_decl (templ);
8630 if (context)
8631 pop_decl_namespace ();
8633 if (templ)
8634 context = DECL_CONTEXT (templ);
8636 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8638 tree type = TREE_TYPE (d1);
8640 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8641 an implicit typename for the second A. Deal with it. */
8642 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8643 type = TREE_TYPE (type);
8645 if (CLASSTYPE_TEMPLATE_INFO (type))
8647 templ = CLASSTYPE_TI_TEMPLATE (type);
8648 d1 = DECL_NAME (templ);
8651 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8652 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8654 templ = TYPE_TI_TEMPLATE (d1);
8655 d1 = DECL_NAME (templ);
8657 else if (DECL_TYPE_TEMPLATE_P (d1))
8659 templ = d1;
8660 d1 = DECL_NAME (templ);
8661 context = DECL_CONTEXT (templ);
8663 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8665 templ = d1;
8666 d1 = DECL_NAME (templ);
8669 /* Issue an error message if we didn't find a template. */
8670 if (! templ)
8672 if (complain & tf_error)
8673 error ("%qT is not a template", d1);
8674 return error_mark_node;
8677 if (TREE_CODE (templ) != TEMPLATE_DECL
8678 /* Make sure it's a user visible template, if it was named by
8679 the user. */
8680 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8681 && !PRIMARY_TEMPLATE_P (templ)))
8683 if (complain & tf_error)
8685 error ("non-template type %qT used as a template", d1);
8686 if (in_decl)
8687 error ("for template declaration %q+D", in_decl);
8689 return error_mark_node;
8692 complain &= ~tf_user;
8694 /* An alias that just changes the name of a template is equivalent to the
8695 other template, so if any of the arguments are pack expansions, strip
8696 the alias to avoid problems with a pack expansion passed to a non-pack
8697 alias template parameter (DR 1430). */
8698 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8699 templ = get_underlying_template (templ);
8701 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8703 tree parm;
8704 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8705 if (arglist2 == error_mark_node
8706 || (!uses_template_parms (arglist2)
8707 && check_instantiated_args (templ, arglist2, complain)))
8708 return error_mark_node;
8710 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8711 return parm;
8713 else
8715 tree template_type = TREE_TYPE (templ);
8716 tree gen_tmpl;
8717 tree type_decl;
8718 tree found = NULL_TREE;
8719 int arg_depth;
8720 int parm_depth;
8721 int is_dependent_type;
8722 int use_partial_inst_tmpl = false;
8724 if (template_type == error_mark_node)
8725 /* An error occurred while building the template TEMPL, and a
8726 diagnostic has most certainly been emitted for that
8727 already. Let's propagate that error. */
8728 return error_mark_node;
8730 gen_tmpl = most_general_template (templ);
8731 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8732 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8733 arg_depth = TMPL_ARGS_DEPTH (arglist);
8735 if (arg_depth == 1 && parm_depth > 1)
8737 /* We've been given an incomplete set of template arguments.
8738 For example, given:
8740 template <class T> struct S1 {
8741 template <class U> struct S2 {};
8742 template <class U> struct S2<U*> {};
8745 we will be called with an ARGLIST of `U*', but the
8746 TEMPLATE will be `template <class T> template
8747 <class U> struct S1<T>::S2'. We must fill in the missing
8748 arguments. */
8749 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8750 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8751 arg_depth = TMPL_ARGS_DEPTH (arglist);
8754 /* Now we should have enough arguments. */
8755 gcc_assert (parm_depth == arg_depth);
8757 /* From here on, we're only interested in the most general
8758 template. */
8760 /* Calculate the BOUND_ARGS. These will be the args that are
8761 actually tsubst'd into the definition to create the
8762 instantiation. */
8763 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8764 complain,
8765 /*require_all_args=*/true,
8766 /*use_default_args=*/true);
8768 if (arglist == error_mark_node)
8769 /* We were unable to bind the arguments. */
8770 return error_mark_node;
8772 /* In the scope of a template class, explicit references to the
8773 template class refer to the type of the template, not any
8774 instantiation of it. For example, in:
8776 template <class T> class C { void f(C<T>); }
8778 the `C<T>' is just the same as `C'. Outside of the
8779 class, however, such a reference is an instantiation. */
8780 if (entering_scope
8781 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8782 || currently_open_class (template_type))
8784 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8786 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8787 return template_type;
8790 /* If we already have this specialization, return it. */
8791 elt.tmpl = gen_tmpl;
8792 elt.args = arglist;
8793 elt.spec = NULL_TREE;
8794 hash = spec_hasher::hash (&elt);
8795 entry = type_specializations->find_with_hash (&elt, hash);
8797 if (entry)
8798 return entry->spec;
8800 /* If the the template's constraints are not satisfied,
8801 then we cannot form a valid type.
8803 Note that the check is deferred until after the hash
8804 lookup. This prevents redundant checks on previously
8805 instantiated specializations. */
8806 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8808 if (complain & tf_error)
8810 error ("template constraint failure");
8811 diagnose_constraints (input_location, gen_tmpl, arglist);
8813 return error_mark_node;
8816 is_dependent_type = uses_template_parms (arglist);
8818 /* If the deduced arguments are invalid, then the binding
8819 failed. */
8820 if (!is_dependent_type
8821 && check_instantiated_args (gen_tmpl,
8822 INNERMOST_TEMPLATE_ARGS (arglist),
8823 complain))
8824 return error_mark_node;
8826 if (!is_dependent_type
8827 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8828 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8829 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8831 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8832 DECL_NAME (gen_tmpl),
8833 /*tag_scope=*/ts_global);
8834 return found;
8837 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8838 complain, in_decl);
8839 if (context == error_mark_node)
8840 return error_mark_node;
8842 if (!context)
8843 context = global_namespace;
8845 /* Create the type. */
8846 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8848 /* The user referred to a specialization of an alias
8849 template represented by GEN_TMPL.
8851 [temp.alias]/2 says:
8853 When a template-id refers to the specialization of an
8854 alias template, it is equivalent to the associated
8855 type obtained by substitution of its
8856 template-arguments for the template-parameters in the
8857 type-id of the alias template. */
8859 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8860 /* Note that the call above (by indirectly calling
8861 register_specialization in tsubst_decl) registers the
8862 TYPE_DECL representing the specialization of the alias
8863 template. So next time someone substitutes ARGLIST for
8864 the template parms into the alias template (GEN_TMPL),
8865 she'll get that TYPE_DECL back. */
8867 if (t == error_mark_node)
8868 return t;
8870 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8872 if (!is_dependent_type)
8874 set_current_access_from_decl (TYPE_NAME (template_type));
8875 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8876 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8877 arglist, complain, in_decl),
8878 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8879 arglist, complain, in_decl),
8880 SCOPED_ENUM_P (template_type), NULL);
8882 if (t == error_mark_node)
8883 return t;
8885 else
8887 /* We don't want to call start_enum for this type, since
8888 the values for the enumeration constants may involve
8889 template parameters. And, no one should be interested
8890 in the enumeration constants for such a type. */
8891 t = cxx_make_type (ENUMERAL_TYPE);
8892 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8894 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8895 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8896 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8898 else if (CLASS_TYPE_P (template_type))
8900 t = make_class_type (TREE_CODE (template_type));
8901 CLASSTYPE_DECLARED_CLASS (t)
8902 = CLASSTYPE_DECLARED_CLASS (template_type);
8903 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8905 /* A local class. Make sure the decl gets registered properly. */
8906 if (context == current_function_decl)
8907 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8909 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8910 /* This instantiation is another name for the primary
8911 template type. Set the TYPE_CANONICAL field
8912 appropriately. */
8913 TYPE_CANONICAL (t) = template_type;
8914 else if (any_template_arguments_need_structural_equality_p (arglist))
8915 /* Some of the template arguments require structural
8916 equality testing, so this template class requires
8917 structural equality testing. */
8918 SET_TYPE_STRUCTURAL_EQUALITY (t);
8920 else
8921 gcc_unreachable ();
8923 /* If we called start_enum or pushtag above, this information
8924 will already be set up. */
8925 if (!TYPE_NAME (t))
8927 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8929 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8930 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8931 DECL_SOURCE_LOCATION (type_decl)
8932 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8934 else
8935 type_decl = TYPE_NAME (t);
8937 if (CLASS_TYPE_P (template_type))
8939 TREE_PRIVATE (type_decl)
8940 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8941 TREE_PROTECTED (type_decl)
8942 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8943 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8945 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8946 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8950 if (OVERLOAD_TYPE_P (t)
8951 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8953 static const char *tags[] = {"abi_tag", "may_alias"};
8955 for (unsigned ix = 0; ix != 2; ix++)
8957 tree attributes
8958 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8960 if (attributes)
8961 TYPE_ATTRIBUTES (t)
8962 = tree_cons (TREE_PURPOSE (attributes),
8963 TREE_VALUE (attributes),
8964 TYPE_ATTRIBUTES (t));
8968 /* Let's consider the explicit specialization of a member
8969 of a class template specialization that is implicitly instantiated,
8970 e.g.:
8971 template<class T>
8972 struct S
8974 template<class U> struct M {}; //#0
8977 template<>
8978 template<>
8979 struct S<int>::M<char> //#1
8981 int i;
8983 [temp.expl.spec]/4 says this is valid.
8985 In this case, when we write:
8986 S<int>::M<char> m;
8988 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8989 the one of #0.
8991 When we encounter #1, we want to store the partial instantiation
8992 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8994 For all cases other than this "explicit specialization of member of a
8995 class template", we just want to store the most general template into
8996 the CLASSTYPE_TI_TEMPLATE of M.
8998 This case of "explicit specialization of member of a class template"
8999 only happens when:
9000 1/ the enclosing class is an instantiation of, and therefore not
9001 the same as, the context of the most general template, and
9002 2/ we aren't looking at the partial instantiation itself, i.e.
9003 the innermost arguments are not the same as the innermost parms of
9004 the most general template.
9006 So it's only when 1/ and 2/ happens that we want to use the partial
9007 instantiation of the member template in lieu of its most general
9008 template. */
9010 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9011 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9012 /* the enclosing class must be an instantiation... */
9013 && CLASS_TYPE_P (context)
9014 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9016 TREE_VEC_LENGTH (arglist)--;
9017 ++processing_template_decl;
9018 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9019 tree partial_inst_args =
9020 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9021 arglist, complain, NULL_TREE);
9022 --processing_template_decl;
9023 TREE_VEC_LENGTH (arglist)++;
9024 if (partial_inst_args == error_mark_node)
9025 return error_mark_node;
9026 use_partial_inst_tmpl =
9027 /*...and we must not be looking at the partial instantiation
9028 itself. */
9029 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9030 partial_inst_args);
9033 if (!use_partial_inst_tmpl)
9034 /* This case is easy; there are no member templates involved. */
9035 found = gen_tmpl;
9036 else
9038 /* This is a full instantiation of a member template. Find
9039 the partial instantiation of which this is an instance. */
9041 /* Temporarily reduce by one the number of levels in the ARGLIST
9042 so as to avoid comparing the last set of arguments. */
9043 TREE_VEC_LENGTH (arglist)--;
9044 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9045 TREE_VEC_LENGTH (arglist)++;
9046 /* FOUND is either a proper class type, or an alias
9047 template specialization. In the later case, it's a
9048 TYPE_DECL, resulting from the substituting of arguments
9049 for parameters in the TYPE_DECL of the alias template
9050 done earlier. So be careful while getting the template
9051 of FOUND. */
9052 found = (TREE_CODE (found) == TEMPLATE_DECL
9053 ? found
9054 : (TREE_CODE (found) == TYPE_DECL
9055 ? DECL_TI_TEMPLATE (found)
9056 : CLASSTYPE_TI_TEMPLATE (found)));
9059 // Build template info for the new specialization.
9060 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9062 elt.spec = t;
9063 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9064 entry = ggc_alloc<spec_entry> ();
9065 *entry = elt;
9066 *slot = entry;
9068 /* Note this use of the partial instantiation so we can check it
9069 later in maybe_process_partial_specialization. */
9070 DECL_TEMPLATE_INSTANTIATIONS (found)
9071 = tree_cons (arglist, t,
9072 DECL_TEMPLATE_INSTANTIATIONS (found));
9074 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9075 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9076 /* Now that the type has been registered on the instantiations
9077 list, we set up the enumerators. Because the enumeration
9078 constants may involve the enumeration type itself, we make
9079 sure to register the type first, and then create the
9080 constants. That way, doing tsubst_expr for the enumeration
9081 constants won't result in recursive calls here; we'll find
9082 the instantiation and exit above. */
9083 tsubst_enum (template_type, t, arglist);
9085 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9086 /* If the type makes use of template parameters, the
9087 code that generates debugging information will crash. */
9088 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9090 /* Possibly limit visibility based on template args. */
9091 TREE_PUBLIC (type_decl) = 1;
9092 determine_visibility (type_decl);
9094 inherit_targ_abi_tags (t);
9096 return t;
9100 /* Wrapper for lookup_template_class_1. */
9102 tree
9103 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9104 int entering_scope, tsubst_flags_t complain)
9106 tree ret;
9107 timevar_push (TV_TEMPLATE_INST);
9108 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9109 entering_scope, complain);
9110 timevar_pop (TV_TEMPLATE_INST);
9111 return ret;
9114 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9116 tree
9117 lookup_template_variable (tree templ, tree arglist)
9119 /* The type of the expression is NULL_TREE since the template-id could refer
9120 to an explicit or partial specialization. */
9121 tree type = NULL_TREE;
9122 if (flag_concepts && variable_concept_p (templ))
9123 /* Except that concepts are always bool. */
9124 type = boolean_type_node;
9125 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9128 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9130 tree
9131 finish_template_variable (tree var, tsubst_flags_t complain)
9133 tree templ = TREE_OPERAND (var, 0);
9134 tree arglist = TREE_OPERAND (var, 1);
9136 /* We never want to return a VAR_DECL for a variable concept, since they
9137 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9138 bool concept_p = flag_concepts && variable_concept_p (templ);
9139 if (concept_p && processing_template_decl)
9140 return var;
9142 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9143 arglist = add_outermost_template_args (tmpl_args, arglist);
9145 templ = most_general_template (templ);
9146 tree parms = DECL_TEMPLATE_PARMS (templ);
9147 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9148 /*req_all*/true,
9149 /*use_default*/true);
9151 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9153 if (complain & tf_error)
9155 error ("use of invalid variable template %qE", var);
9156 diagnose_constraints (location_of (var), templ, arglist);
9158 return error_mark_node;
9161 /* If a template-id refers to a specialization of a variable
9162 concept, then the expression is true if and only if the
9163 concept's constraints are satisfied by the given template
9164 arguments.
9166 NOTE: This is an extension of Concepts Lite TS that
9167 allows constraints to be used in expressions. */
9168 if (concept_p)
9170 tree decl = DECL_TEMPLATE_RESULT (templ);
9171 return evaluate_variable_concept (decl, arglist);
9174 return instantiate_template (templ, arglist, complain);
9177 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9178 TARGS template args, and instantiate it if it's not dependent. */
9180 tree
9181 lookup_and_finish_template_variable (tree templ, tree targs,
9182 tsubst_flags_t complain)
9184 templ = lookup_template_variable (templ, targs);
9185 if (!any_dependent_template_arguments_p (targs))
9187 templ = finish_template_variable (templ, complain);
9188 mark_used (templ);
9191 return convert_from_reference (templ);
9195 struct pair_fn_data
9197 tree_fn_t fn;
9198 tree_fn_t any_fn;
9199 void *data;
9200 /* True when we should also visit template parameters that occur in
9201 non-deduced contexts. */
9202 bool include_nondeduced_p;
9203 hash_set<tree> *visited;
9206 /* Called from for_each_template_parm via walk_tree. */
9208 static tree
9209 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9211 tree t = *tp;
9212 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9213 tree_fn_t fn = pfd->fn;
9214 void *data = pfd->data;
9215 tree result = NULL_TREE;
9217 #define WALK_SUBTREE(NODE) \
9218 do \
9220 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9221 pfd->include_nondeduced_p, \
9222 pfd->any_fn); \
9223 if (result) goto out; \
9225 while (0)
9227 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9228 return t;
9230 if (TYPE_P (t)
9231 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9232 WALK_SUBTREE (TYPE_CONTEXT (t));
9234 switch (TREE_CODE (t))
9236 case RECORD_TYPE:
9237 if (TYPE_PTRMEMFUNC_P (t))
9238 break;
9239 /* Fall through. */
9241 case UNION_TYPE:
9242 case ENUMERAL_TYPE:
9243 if (!TYPE_TEMPLATE_INFO (t))
9244 *walk_subtrees = 0;
9245 else
9246 WALK_SUBTREE (TYPE_TI_ARGS (t));
9247 break;
9249 case INTEGER_TYPE:
9250 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9251 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9252 break;
9254 case METHOD_TYPE:
9255 /* Since we're not going to walk subtrees, we have to do this
9256 explicitly here. */
9257 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9258 /* Fall through. */
9260 case FUNCTION_TYPE:
9261 /* Check the return type. */
9262 WALK_SUBTREE (TREE_TYPE (t));
9264 /* Check the parameter types. Since default arguments are not
9265 instantiated until they are needed, the TYPE_ARG_TYPES may
9266 contain expressions that involve template parameters. But,
9267 no-one should be looking at them yet. And, once they're
9268 instantiated, they don't contain template parameters, so
9269 there's no point in looking at them then, either. */
9271 tree parm;
9273 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9274 WALK_SUBTREE (TREE_VALUE (parm));
9276 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9277 want walk_tree walking into them itself. */
9278 *walk_subtrees = 0;
9281 if (flag_noexcept_type)
9283 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9284 if (spec)
9285 WALK_SUBTREE (TREE_PURPOSE (spec));
9287 break;
9289 case TYPEOF_TYPE:
9290 case UNDERLYING_TYPE:
9291 if (pfd->include_nondeduced_p
9292 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9293 pfd->visited,
9294 pfd->include_nondeduced_p,
9295 pfd->any_fn))
9296 return error_mark_node;
9297 break;
9299 case FUNCTION_DECL:
9300 case VAR_DECL:
9301 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9302 WALK_SUBTREE (DECL_TI_ARGS (t));
9303 /* Fall through. */
9305 case PARM_DECL:
9306 case CONST_DECL:
9307 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9308 WALK_SUBTREE (DECL_INITIAL (t));
9309 if (DECL_CONTEXT (t)
9310 && pfd->include_nondeduced_p)
9311 WALK_SUBTREE (DECL_CONTEXT (t));
9312 break;
9314 case BOUND_TEMPLATE_TEMPLATE_PARM:
9315 /* Record template parameters such as `T' inside `TT<T>'. */
9316 WALK_SUBTREE (TYPE_TI_ARGS (t));
9317 /* Fall through. */
9319 case TEMPLATE_TEMPLATE_PARM:
9320 case TEMPLATE_TYPE_PARM:
9321 case TEMPLATE_PARM_INDEX:
9322 if (fn && (*fn)(t, data))
9323 return t;
9324 else if (!fn)
9325 return t;
9326 break;
9328 case TEMPLATE_DECL:
9329 /* A template template parameter is encountered. */
9330 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9331 WALK_SUBTREE (TREE_TYPE (t));
9333 /* Already substituted template template parameter */
9334 *walk_subtrees = 0;
9335 break;
9337 case TYPENAME_TYPE:
9338 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9339 partial instantiation. */
9340 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9341 break;
9343 case CONSTRUCTOR:
9344 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9345 && pfd->include_nondeduced_p)
9346 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9347 break;
9349 case INDIRECT_REF:
9350 case COMPONENT_REF:
9351 /* If there's no type, then this thing must be some expression
9352 involving template parameters. */
9353 if (!fn && !TREE_TYPE (t))
9354 return error_mark_node;
9355 break;
9357 case MODOP_EXPR:
9358 case CAST_EXPR:
9359 case IMPLICIT_CONV_EXPR:
9360 case REINTERPRET_CAST_EXPR:
9361 case CONST_CAST_EXPR:
9362 case STATIC_CAST_EXPR:
9363 case DYNAMIC_CAST_EXPR:
9364 case ARROW_EXPR:
9365 case DOTSTAR_EXPR:
9366 case TYPEID_EXPR:
9367 case PSEUDO_DTOR_EXPR:
9368 if (!fn)
9369 return error_mark_node;
9370 break;
9372 default:
9373 break;
9376 #undef WALK_SUBTREE
9378 /* We didn't find any template parameters we liked. */
9379 out:
9380 return result;
9383 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9384 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9385 call FN with the parameter and the DATA.
9386 If FN returns nonzero, the iteration is terminated, and
9387 for_each_template_parm returns 1. Otherwise, the iteration
9388 continues. If FN never returns a nonzero value, the value
9389 returned by for_each_template_parm is 0. If FN is NULL, it is
9390 considered to be the function which always returns 1.
9392 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9393 parameters that occur in non-deduced contexts. When false, only
9394 visits those template parameters that can be deduced. */
9396 static tree
9397 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9398 hash_set<tree> *visited,
9399 bool include_nondeduced_p,
9400 tree_fn_t any_fn)
9402 struct pair_fn_data pfd;
9403 tree result;
9405 /* Set up. */
9406 pfd.fn = fn;
9407 pfd.any_fn = any_fn;
9408 pfd.data = data;
9409 pfd.include_nondeduced_p = include_nondeduced_p;
9411 /* Walk the tree. (Conceptually, we would like to walk without
9412 duplicates, but for_each_template_parm_r recursively calls
9413 for_each_template_parm, so we would need to reorganize a fair
9414 bit to use walk_tree_without_duplicates, so we keep our own
9415 visited list.) */
9416 if (visited)
9417 pfd.visited = visited;
9418 else
9419 pfd.visited = new hash_set<tree>;
9420 result = cp_walk_tree (&t,
9421 for_each_template_parm_r,
9422 &pfd,
9423 pfd.visited);
9425 /* Clean up. */
9426 if (!visited)
9428 delete pfd.visited;
9429 pfd.visited = 0;
9432 return result;
9435 /* Returns true if T depends on any template parameter. */
9438 uses_template_parms (tree t)
9440 if (t == NULL_TREE)
9441 return false;
9443 bool dependent_p;
9444 int saved_processing_template_decl;
9446 saved_processing_template_decl = processing_template_decl;
9447 if (!saved_processing_template_decl)
9448 processing_template_decl = 1;
9449 if (TYPE_P (t))
9450 dependent_p = dependent_type_p (t);
9451 else if (TREE_CODE (t) == TREE_VEC)
9452 dependent_p = any_dependent_template_arguments_p (t);
9453 else if (TREE_CODE (t) == TREE_LIST)
9454 dependent_p = (uses_template_parms (TREE_VALUE (t))
9455 || uses_template_parms (TREE_CHAIN (t)));
9456 else if (TREE_CODE (t) == TYPE_DECL)
9457 dependent_p = dependent_type_p (TREE_TYPE (t));
9458 else if (DECL_P (t)
9459 || EXPR_P (t)
9460 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9461 || TREE_CODE (t) == OVERLOAD
9462 || BASELINK_P (t)
9463 || identifier_p (t)
9464 || TREE_CODE (t) == TRAIT_EXPR
9465 || TREE_CODE (t) == CONSTRUCTOR
9466 || CONSTANT_CLASS_P (t))
9467 dependent_p = (type_dependent_expression_p (t)
9468 || value_dependent_expression_p (t));
9469 else
9471 gcc_assert (t == error_mark_node);
9472 dependent_p = false;
9475 processing_template_decl = saved_processing_template_decl;
9477 return dependent_p;
9480 /* Returns true iff current_function_decl is an incompletely instantiated
9481 template. Useful instead of processing_template_decl because the latter
9482 is set to 0 during instantiate_non_dependent_expr. */
9484 bool
9485 in_template_function (void)
9487 tree fn = current_function_decl;
9488 bool ret;
9489 ++processing_template_decl;
9490 ret = (fn && DECL_LANG_SPECIFIC (fn)
9491 && DECL_TEMPLATE_INFO (fn)
9492 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9493 --processing_template_decl;
9494 return ret;
9497 /* Returns true iff we are currently within a template other than a generic
9498 lambda. We test this by finding the outermost closure type and checking
9499 whether it is dependent. */
9501 bool
9502 processing_nonlambda_template (void)
9504 if (!processing_template_decl)
9505 return false;
9507 tree outer_closure = NULL_TREE;
9508 for (tree t = current_class_type; t;
9509 t = decl_type_context (TYPE_MAIN_DECL (t)))
9511 if (LAMBDA_TYPE_P (t))
9512 outer_closure = t;
9513 else
9514 break;
9517 if (outer_closure)
9518 return dependent_type_p (outer_closure);
9519 else
9520 return true;
9523 /* Returns true if T depends on any template parameter with level LEVEL. */
9525 bool
9526 uses_template_parms_level (tree t, int level)
9528 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9529 /*include_nondeduced_p=*/true);
9532 /* Returns true if the signature of DECL depends on any template parameter from
9533 its enclosing class. */
9535 bool
9536 uses_outer_template_parms (tree decl)
9538 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9539 if (depth == 0)
9540 return false;
9541 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9542 &depth, NULL, /*include_nondeduced_p=*/true))
9543 return true;
9544 if (PRIMARY_TEMPLATE_P (decl)
9545 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9546 (DECL_TEMPLATE_PARMS (decl)),
9547 template_parm_outer_level,
9548 &depth, NULL, /*include_nondeduced_p=*/true))
9549 return true;
9550 tree ci = get_constraints (decl);
9551 if (ci)
9552 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9553 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9554 &depth, NULL, /*nondeduced*/true))
9555 return true;
9556 return false;
9559 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9560 ill-formed translation unit, i.e. a variable or function that isn't
9561 usable in a constant expression. */
9563 static inline bool
9564 neglectable_inst_p (tree d)
9566 return (DECL_P (d)
9567 && !undeduced_auto_decl (d)
9568 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9569 : decl_maybe_constant_var_p (d)));
9572 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9573 neglectable and instantiated from within an erroneous instantiation. */
9575 static bool
9576 limit_bad_template_recursion (tree decl)
9578 struct tinst_level *lev = current_tinst_level;
9579 int errs = errorcount + sorrycount;
9580 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9581 return false;
9583 for (; lev; lev = lev->next)
9584 if (neglectable_inst_p (lev->decl))
9585 break;
9587 return (lev && errs > lev->errors);
9590 static int tinst_depth;
9591 extern int max_tinst_depth;
9592 int depth_reached;
9594 static GTY(()) struct tinst_level *last_error_tinst_level;
9596 /* We're starting to instantiate D; record the template instantiation context
9597 for diagnostics and to restore it later. */
9599 bool
9600 push_tinst_level (tree d)
9602 return push_tinst_level_loc (d, input_location);
9605 /* We're starting to instantiate D; record the template instantiation context
9606 at LOC for diagnostics and to restore it later. */
9608 bool
9609 push_tinst_level_loc (tree d, location_t loc)
9611 struct tinst_level *new_level;
9613 if (tinst_depth >= max_tinst_depth)
9615 /* Tell error.c not to try to instantiate any templates. */
9616 at_eof = 2;
9617 fatal_error (input_location,
9618 "template instantiation depth exceeds maximum of %d"
9619 " (use -ftemplate-depth= to increase the maximum)",
9620 max_tinst_depth);
9621 return false;
9624 /* If the current instantiation caused problems, don't let it instantiate
9625 anything else. Do allow deduction substitution and decls usable in
9626 constant expressions. */
9627 if (limit_bad_template_recursion (d))
9628 return false;
9630 /* When not -quiet, dump template instantiations other than functions, since
9631 announce_function will take care of those. */
9632 if (!quiet_flag
9633 && TREE_CODE (d) != TREE_LIST
9634 && TREE_CODE (d) != FUNCTION_DECL)
9635 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9637 new_level = ggc_alloc<tinst_level> ();
9638 new_level->decl = d;
9639 new_level->locus = loc;
9640 new_level->errors = errorcount+sorrycount;
9641 new_level->in_system_header_p = in_system_header_at (input_location);
9642 new_level->next = current_tinst_level;
9643 current_tinst_level = new_level;
9645 ++tinst_depth;
9646 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9647 depth_reached = tinst_depth;
9649 return true;
9652 /* We're done instantiating this template; return to the instantiation
9653 context. */
9655 void
9656 pop_tinst_level (void)
9658 /* Restore the filename and line number stashed away when we started
9659 this instantiation. */
9660 input_location = current_tinst_level->locus;
9661 current_tinst_level = current_tinst_level->next;
9662 --tinst_depth;
9665 /* We're instantiating a deferred template; restore the template
9666 instantiation context in which the instantiation was requested, which
9667 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9669 static tree
9670 reopen_tinst_level (struct tinst_level *level)
9672 struct tinst_level *t;
9674 tinst_depth = 0;
9675 for (t = level; t; t = t->next)
9676 ++tinst_depth;
9678 current_tinst_level = level;
9679 pop_tinst_level ();
9680 if (current_tinst_level)
9681 current_tinst_level->errors = errorcount+sorrycount;
9682 return level->decl;
9685 /* Returns the TINST_LEVEL which gives the original instantiation
9686 context. */
9688 struct tinst_level *
9689 outermost_tinst_level (void)
9691 struct tinst_level *level = current_tinst_level;
9692 if (level)
9693 while (level->next)
9694 level = level->next;
9695 return level;
9698 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9699 vector of template arguments, as for tsubst.
9701 Returns an appropriate tsubst'd friend declaration. */
9703 static tree
9704 tsubst_friend_function (tree decl, tree args)
9706 tree new_friend;
9708 if (TREE_CODE (decl) == FUNCTION_DECL
9709 && DECL_TEMPLATE_INSTANTIATION (decl)
9710 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9711 /* This was a friend declared with an explicit template
9712 argument list, e.g.:
9714 friend void f<>(T);
9716 to indicate that f was a template instantiation, not a new
9717 function declaration. Now, we have to figure out what
9718 instantiation of what template. */
9720 tree template_id, arglist, fns;
9721 tree new_args;
9722 tree tmpl;
9723 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9725 /* Friend functions are looked up in the containing namespace scope.
9726 We must enter that scope, to avoid finding member functions of the
9727 current class with same name. */
9728 push_nested_namespace (ns);
9729 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9730 tf_warning_or_error, NULL_TREE,
9731 /*integral_constant_expression_p=*/false);
9732 pop_nested_namespace (ns);
9733 arglist = tsubst (DECL_TI_ARGS (decl), args,
9734 tf_warning_or_error, NULL_TREE);
9735 template_id = lookup_template_function (fns, arglist);
9737 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9738 tmpl = determine_specialization (template_id, new_friend,
9739 &new_args,
9740 /*need_member_template=*/0,
9741 TREE_VEC_LENGTH (args),
9742 tsk_none);
9743 return instantiate_template (tmpl, new_args, tf_error);
9746 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9748 /* The NEW_FRIEND will look like an instantiation, to the
9749 compiler, but is not an instantiation from the point of view of
9750 the language. For example, we might have had:
9752 template <class T> struct S {
9753 template <class U> friend void f(T, U);
9756 Then, in S<int>, template <class U> void f(int, U) is not an
9757 instantiation of anything. */
9758 if (new_friend == error_mark_node)
9759 return error_mark_node;
9761 DECL_USE_TEMPLATE (new_friend) = 0;
9762 if (TREE_CODE (decl) == TEMPLATE_DECL)
9764 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9765 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9766 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9769 /* The mangled name for the NEW_FRIEND is incorrect. The function
9770 is not a template instantiation and should not be mangled like
9771 one. Therefore, we forget the mangling here; we'll recompute it
9772 later if we need it. */
9773 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9775 SET_DECL_RTL (new_friend, NULL);
9776 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9779 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9781 tree old_decl;
9782 tree new_friend_template_info;
9783 tree new_friend_result_template_info;
9784 tree ns;
9785 int new_friend_is_defn;
9787 /* We must save some information from NEW_FRIEND before calling
9788 duplicate decls since that function will free NEW_FRIEND if
9789 possible. */
9790 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9791 new_friend_is_defn =
9792 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9793 (template_for_substitution (new_friend)))
9794 != NULL_TREE);
9795 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9797 /* This declaration is a `primary' template. */
9798 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9800 new_friend_result_template_info
9801 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9803 else
9804 new_friend_result_template_info = NULL_TREE;
9806 /* Inside pushdecl_namespace_level, we will push into the
9807 current namespace. However, the friend function should go
9808 into the namespace of the template. */
9809 ns = decl_namespace_context (new_friend);
9810 push_nested_namespace (ns);
9811 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9812 pop_nested_namespace (ns);
9814 if (old_decl == error_mark_node)
9815 return error_mark_node;
9817 if (old_decl != new_friend)
9819 /* This new friend declaration matched an existing
9820 declaration. For example, given:
9822 template <class T> void f(T);
9823 template <class U> class C {
9824 template <class T> friend void f(T) {}
9827 the friend declaration actually provides the definition
9828 of `f', once C has been instantiated for some type. So,
9829 old_decl will be the out-of-class template declaration,
9830 while new_friend is the in-class definition.
9832 But, if `f' was called before this point, the
9833 instantiation of `f' will have DECL_TI_ARGS corresponding
9834 to `T' but not to `U', references to which might appear
9835 in the definition of `f'. Previously, the most general
9836 template for an instantiation of `f' was the out-of-class
9837 version; now it is the in-class version. Therefore, we
9838 run through all specialization of `f', adding to their
9839 DECL_TI_ARGS appropriately. In particular, they need a
9840 new set of outer arguments, corresponding to the
9841 arguments for this class instantiation.
9843 The same situation can arise with something like this:
9845 friend void f(int);
9846 template <class T> class C {
9847 friend void f(T) {}
9850 when `C<int>' is instantiated. Now, `f(int)' is defined
9851 in the class. */
9853 if (!new_friend_is_defn)
9854 /* On the other hand, if the in-class declaration does
9855 *not* provide a definition, then we don't want to alter
9856 existing definitions. We can just leave everything
9857 alone. */
9859 else
9861 tree new_template = TI_TEMPLATE (new_friend_template_info);
9862 tree new_args = TI_ARGS (new_friend_template_info);
9864 /* Overwrite whatever template info was there before, if
9865 any, with the new template information pertaining to
9866 the declaration. */
9867 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9869 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9871 /* We should have called reregister_specialization in
9872 duplicate_decls. */
9873 gcc_assert (retrieve_specialization (new_template,
9874 new_args, 0)
9875 == old_decl);
9877 /* Instantiate it if the global has already been used. */
9878 if (DECL_ODR_USED (old_decl))
9879 instantiate_decl (old_decl, /*defer_ok=*/true,
9880 /*expl_inst_class_mem_p=*/false);
9882 else
9884 tree t;
9886 /* Indicate that the old function template is a partial
9887 instantiation. */
9888 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9889 = new_friend_result_template_info;
9891 gcc_assert (new_template
9892 == most_general_template (new_template));
9893 gcc_assert (new_template != old_decl);
9895 /* Reassign any specializations already in the hash table
9896 to the new more general template, and add the
9897 additional template args. */
9898 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9899 t != NULL_TREE;
9900 t = TREE_CHAIN (t))
9902 tree spec = TREE_VALUE (t);
9903 spec_entry elt;
9905 elt.tmpl = old_decl;
9906 elt.args = DECL_TI_ARGS (spec);
9907 elt.spec = NULL_TREE;
9909 decl_specializations->remove_elt (&elt);
9911 DECL_TI_ARGS (spec)
9912 = add_outermost_template_args (new_args,
9913 DECL_TI_ARGS (spec));
9915 register_specialization
9916 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9919 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9923 /* The information from NEW_FRIEND has been merged into OLD_DECL
9924 by duplicate_decls. */
9925 new_friend = old_decl;
9928 else
9930 tree context = DECL_CONTEXT (new_friend);
9931 bool dependent_p;
9933 /* In the code
9934 template <class T> class C {
9935 template <class U> friend void C1<U>::f (); // case 1
9936 friend void C2<T>::f (); // case 2
9938 we only need to make sure CONTEXT is a complete type for
9939 case 2. To distinguish between the two cases, we note that
9940 CONTEXT of case 1 remains dependent type after tsubst while
9941 this isn't true for case 2. */
9942 ++processing_template_decl;
9943 dependent_p = dependent_type_p (context);
9944 --processing_template_decl;
9946 if (!dependent_p
9947 && !complete_type_or_else (context, NULL_TREE))
9948 return error_mark_node;
9950 if (COMPLETE_TYPE_P (context))
9952 tree fn = new_friend;
9953 /* do_friend adds the TEMPLATE_DECL for any member friend
9954 template even if it isn't a member template, i.e.
9955 template <class T> friend A<T>::f();
9956 Look through it in that case. */
9957 if (TREE_CODE (fn) == TEMPLATE_DECL
9958 && !PRIMARY_TEMPLATE_P (fn))
9959 fn = DECL_TEMPLATE_RESULT (fn);
9960 /* Check to see that the declaration is really present, and,
9961 possibly obtain an improved declaration. */
9962 fn = check_classfn (context, fn, NULL_TREE);
9964 if (fn)
9965 new_friend = fn;
9969 return new_friend;
9972 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9973 template arguments, as for tsubst.
9975 Returns an appropriate tsubst'd friend type or error_mark_node on
9976 failure. */
9978 static tree
9979 tsubst_friend_class (tree friend_tmpl, tree args)
9981 tree friend_type;
9982 tree tmpl;
9983 tree context;
9985 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9987 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9988 return TREE_TYPE (t);
9991 context = CP_DECL_CONTEXT (friend_tmpl);
9993 if (context != global_namespace)
9995 if (TREE_CODE (context) == NAMESPACE_DECL)
9996 push_nested_namespace (context);
9997 else
9998 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
10001 /* Look for a class template declaration. We look for hidden names
10002 because two friend declarations of the same template are the
10003 same. For example, in:
10005 struct A {
10006 template <typename> friend class F;
10008 template <typename> struct B {
10009 template <typename> friend class F;
10012 both F templates are the same. */
10013 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10014 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10016 /* But, if we don't find one, it might be because we're in a
10017 situation like this:
10019 template <class T>
10020 struct S {
10021 template <class U>
10022 friend struct S;
10025 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10026 for `S<int>', not the TEMPLATE_DECL. */
10027 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10029 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10030 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10033 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10035 /* The friend template has already been declared. Just
10036 check to see that the declarations match, and install any new
10037 default parameters. We must tsubst the default parameters,
10038 of course. We only need the innermost template parameters
10039 because that is all that redeclare_class_template will look
10040 at. */
10041 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10042 > TMPL_ARGS_DEPTH (args))
10044 tree parms;
10045 location_t saved_input_location;
10046 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10047 args, tf_warning_or_error);
10049 saved_input_location = input_location;
10050 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10051 tree cons = get_constraints (tmpl);
10052 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10053 input_location = saved_input_location;
10057 friend_type = TREE_TYPE (tmpl);
10059 else
10061 /* The friend template has not already been declared. In this
10062 case, the instantiation of the template class will cause the
10063 injection of this template into the global scope. */
10064 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10065 if (tmpl == error_mark_node)
10066 return error_mark_node;
10068 /* The new TMPL is not an instantiation of anything, so we
10069 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10070 the new type because that is supposed to be the corresponding
10071 template decl, i.e., TMPL. */
10072 DECL_USE_TEMPLATE (tmpl) = 0;
10073 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10074 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10075 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10076 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10078 /* Inject this template into the global scope. */
10079 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10082 if (context != global_namespace)
10084 if (TREE_CODE (context) == NAMESPACE_DECL)
10085 pop_nested_namespace (context);
10086 else
10087 pop_nested_class ();
10090 return friend_type;
10093 /* Returns zero if TYPE cannot be completed later due to circularity.
10094 Otherwise returns one. */
10096 static int
10097 can_complete_type_without_circularity (tree type)
10099 if (type == NULL_TREE || type == error_mark_node)
10100 return 0;
10101 else if (COMPLETE_TYPE_P (type))
10102 return 1;
10103 else if (TREE_CODE (type) == ARRAY_TYPE)
10104 return can_complete_type_without_circularity (TREE_TYPE (type));
10105 else if (CLASS_TYPE_P (type)
10106 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10107 return 0;
10108 else
10109 return 1;
10112 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10113 tsubst_flags_t, tree);
10115 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10116 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10118 static tree
10119 tsubst_attribute (tree t, tree *decl_p, tree args,
10120 tsubst_flags_t complain, tree in_decl)
10122 gcc_assert (ATTR_IS_DEPENDENT (t));
10124 tree val = TREE_VALUE (t);
10125 if (val == NULL_TREE)
10126 /* Nothing to do. */;
10127 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10128 && is_attribute_p ("omp declare simd",
10129 get_attribute_name (t)))
10131 tree clauses = TREE_VALUE (val);
10132 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10133 complain, in_decl);
10134 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10135 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10136 tree parms = DECL_ARGUMENTS (*decl_p);
10137 clauses
10138 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10139 if (clauses)
10140 val = build_tree_list (NULL_TREE, clauses);
10141 else
10142 val = NULL_TREE;
10144 /* If the first attribute argument is an identifier, don't
10145 pass it through tsubst. Attributes like mode, format,
10146 cleanup and several target specific attributes expect it
10147 unmodified. */
10148 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10150 tree chain
10151 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10152 /*integral_constant_expression_p=*/false);
10153 if (chain != TREE_CHAIN (val))
10154 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10156 else if (PACK_EXPANSION_P (val))
10158 /* An attribute pack expansion. */
10159 tree purp = TREE_PURPOSE (t);
10160 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10161 if (pack == error_mark_node)
10162 return error_mark_node;
10163 int len = TREE_VEC_LENGTH (pack);
10164 tree list = NULL_TREE;
10165 tree *q = &list;
10166 for (int i = 0; i < len; ++i)
10168 tree elt = TREE_VEC_ELT (pack, i);
10169 *q = build_tree_list (purp, elt);
10170 q = &TREE_CHAIN (*q);
10172 return list;
10174 else
10175 val = tsubst_expr (val, args, complain, in_decl,
10176 /*integral_constant_expression_p=*/false);
10178 if (val != TREE_VALUE (t))
10179 return build_tree_list (TREE_PURPOSE (t), val);
10180 return t;
10183 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10184 unchanged or a new TREE_LIST chain. */
10186 static tree
10187 tsubst_attributes (tree attributes, tree args,
10188 tsubst_flags_t complain, tree in_decl)
10190 tree last_dep = NULL_TREE;
10192 for (tree t = attributes; t; t = TREE_CHAIN (t))
10193 if (ATTR_IS_DEPENDENT (t))
10195 last_dep = t;
10196 attributes = copy_list (attributes);
10197 break;
10200 if (last_dep)
10201 for (tree *p = &attributes; *p; )
10203 tree t = *p;
10204 if (ATTR_IS_DEPENDENT (t))
10206 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10207 if (subst != t)
10209 *p = subst;
10211 p = &TREE_CHAIN (*p);
10212 while (*p);
10213 *p = TREE_CHAIN (t);
10214 continue;
10217 p = &TREE_CHAIN (*p);
10220 return attributes;
10223 /* Apply any attributes which had to be deferred until instantiation
10224 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10225 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10227 static void
10228 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10229 tree args, tsubst_flags_t complain, tree in_decl)
10231 tree last_dep = NULL_TREE;
10232 tree t;
10233 tree *p;
10235 if (attributes == NULL_TREE)
10236 return;
10238 if (DECL_P (*decl_p))
10240 if (TREE_TYPE (*decl_p) == error_mark_node)
10241 return;
10242 p = &DECL_ATTRIBUTES (*decl_p);
10243 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10244 to our attributes parameter. */
10245 gcc_assert (*p == attributes);
10247 else
10249 p = &TYPE_ATTRIBUTES (*decl_p);
10250 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10251 lookup_template_class_1, and should be preserved. */
10252 gcc_assert (*p != attributes);
10253 while (*p)
10254 p = &TREE_CHAIN (*p);
10257 for (t = attributes; t; t = TREE_CHAIN (t))
10258 if (ATTR_IS_DEPENDENT (t))
10260 last_dep = t;
10261 attributes = copy_list (attributes);
10262 break;
10265 *p = attributes;
10266 if (last_dep)
10268 tree late_attrs = NULL_TREE;
10269 tree *q = &late_attrs;
10271 for (; *p; )
10273 t = *p;
10274 if (ATTR_IS_DEPENDENT (t))
10276 *p = TREE_CHAIN (t);
10277 TREE_CHAIN (t) = NULL_TREE;
10278 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10280 q = &TREE_CHAIN (*q);
10281 while (*q);
10283 else
10284 p = &TREE_CHAIN (t);
10287 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10291 /* Perform (or defer) access check for typedefs that were referenced
10292 from within the template TMPL code.
10293 This is a subroutine of instantiate_decl and instantiate_class_template.
10294 TMPL is the template to consider and TARGS is the list of arguments of
10295 that template. */
10297 static void
10298 perform_typedefs_access_check (tree tmpl, tree targs)
10300 location_t saved_location;
10301 unsigned i;
10302 qualified_typedef_usage_t *iter;
10304 if (!tmpl
10305 || (!CLASS_TYPE_P (tmpl)
10306 && TREE_CODE (tmpl) != FUNCTION_DECL))
10307 return;
10309 saved_location = input_location;
10310 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10312 tree type_decl = iter->typedef_decl;
10313 tree type_scope = iter->context;
10315 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10316 continue;
10318 if (uses_template_parms (type_decl))
10319 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10320 if (uses_template_parms (type_scope))
10321 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10323 /* Make access check error messages point to the location
10324 of the use of the typedef. */
10325 input_location = iter->locus;
10326 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10327 type_decl, type_decl,
10328 tf_warning_or_error);
10330 input_location = saved_location;
10333 static tree
10334 instantiate_class_template_1 (tree type)
10336 tree templ, args, pattern, t, member;
10337 tree typedecl;
10338 tree pbinfo;
10339 tree base_list;
10340 unsigned int saved_maximum_field_alignment;
10341 tree fn_context;
10343 if (type == error_mark_node)
10344 return error_mark_node;
10346 if (COMPLETE_OR_OPEN_TYPE_P (type)
10347 || uses_template_parms (type))
10348 return type;
10350 /* Figure out which template is being instantiated. */
10351 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10352 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10354 /* Determine what specialization of the original template to
10355 instantiate. */
10356 t = most_specialized_partial_spec (type, tf_warning_or_error);
10357 if (t == error_mark_node)
10359 TYPE_BEING_DEFINED (type) = 1;
10360 return error_mark_node;
10362 else if (t)
10364 /* This TYPE is actually an instantiation of a partial
10365 specialization. We replace the innermost set of ARGS with
10366 the arguments appropriate for substitution. For example,
10367 given:
10369 template <class T> struct S {};
10370 template <class T> struct S<T*> {};
10372 and supposing that we are instantiating S<int*>, ARGS will
10373 presently be {int*} -- but we need {int}. */
10374 pattern = TREE_TYPE (t);
10375 args = TREE_PURPOSE (t);
10377 else
10379 pattern = TREE_TYPE (templ);
10380 args = CLASSTYPE_TI_ARGS (type);
10383 /* If the template we're instantiating is incomplete, then clearly
10384 there's nothing we can do. */
10385 if (!COMPLETE_TYPE_P (pattern))
10386 return type;
10388 /* If we've recursively instantiated too many templates, stop. */
10389 if (! push_tinst_level (type))
10390 return type;
10392 /* Now we're really doing the instantiation. Mark the type as in
10393 the process of being defined. */
10394 TYPE_BEING_DEFINED (type) = 1;
10396 /* We may be in the middle of deferred access check. Disable
10397 it now. */
10398 push_deferring_access_checks (dk_no_deferred);
10400 int saved_unevaluated_operand = cp_unevaluated_operand;
10401 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10403 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10404 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10405 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10406 fn_context = error_mark_node;
10407 if (!fn_context)
10408 push_to_top_level ();
10409 else
10411 cp_unevaluated_operand = 0;
10412 c_inhibit_evaluation_warnings = 0;
10414 /* Use #pragma pack from the template context. */
10415 saved_maximum_field_alignment = maximum_field_alignment;
10416 maximum_field_alignment = TYPE_PRECISION (pattern);
10418 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10420 /* Set the input location to the most specialized template definition.
10421 This is needed if tsubsting causes an error. */
10422 typedecl = TYPE_MAIN_DECL (pattern);
10423 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10424 DECL_SOURCE_LOCATION (typedecl);
10426 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10427 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10428 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10429 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10430 if (ANON_AGGR_TYPE_P (pattern))
10431 SET_ANON_AGGR_TYPE_P (type);
10432 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10434 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10435 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10436 /* Adjust visibility for template arguments. */
10437 determine_visibility (TYPE_MAIN_DECL (type));
10439 if (CLASS_TYPE_P (type))
10440 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10442 pbinfo = TYPE_BINFO (pattern);
10444 /* We should never instantiate a nested class before its enclosing
10445 class; we need to look up the nested class by name before we can
10446 instantiate it, and that lookup should instantiate the enclosing
10447 class. */
10448 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10449 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10451 base_list = NULL_TREE;
10452 if (BINFO_N_BASE_BINFOS (pbinfo))
10454 tree pbase_binfo;
10455 tree pushed_scope;
10456 int i;
10458 /* We must enter the scope containing the type, as that is where
10459 the accessibility of types named in dependent bases are
10460 looked up from. */
10461 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10463 /* Substitute into each of the bases to determine the actual
10464 basetypes. */
10465 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10467 tree base;
10468 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10469 tree expanded_bases = NULL_TREE;
10470 int idx, len = 1;
10472 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10474 expanded_bases =
10475 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10476 args, tf_error, NULL_TREE);
10477 if (expanded_bases == error_mark_node)
10478 continue;
10480 len = TREE_VEC_LENGTH (expanded_bases);
10483 for (idx = 0; idx < len; idx++)
10485 if (expanded_bases)
10486 /* Extract the already-expanded base class. */
10487 base = TREE_VEC_ELT (expanded_bases, idx);
10488 else
10489 /* Substitute to figure out the base class. */
10490 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10491 NULL_TREE);
10493 if (base == error_mark_node)
10494 continue;
10496 base_list = tree_cons (access, base, base_list);
10497 if (BINFO_VIRTUAL_P (pbase_binfo))
10498 TREE_TYPE (base_list) = integer_type_node;
10502 /* The list is now in reverse order; correct that. */
10503 base_list = nreverse (base_list);
10505 if (pushed_scope)
10506 pop_scope (pushed_scope);
10508 /* Now call xref_basetypes to set up all the base-class
10509 information. */
10510 xref_basetypes (type, base_list);
10512 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10513 (int) ATTR_FLAG_TYPE_IN_PLACE,
10514 args, tf_error, NULL_TREE);
10515 fixup_attribute_variants (type);
10517 /* Now that our base classes are set up, enter the scope of the
10518 class, so that name lookups into base classes, etc. will work
10519 correctly. This is precisely analogous to what we do in
10520 begin_class_definition when defining an ordinary non-template
10521 class, except we also need to push the enclosing classes. */
10522 push_nested_class (type);
10524 /* Now members are processed in the order of declaration. */
10525 for (member = CLASSTYPE_DECL_LIST (pattern);
10526 member; member = TREE_CHAIN (member))
10528 tree t = TREE_VALUE (member);
10530 if (TREE_PURPOSE (member))
10532 if (TYPE_P (t))
10534 /* Build new CLASSTYPE_NESTED_UTDS. */
10536 tree newtag;
10537 bool class_template_p;
10539 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10540 && TYPE_LANG_SPECIFIC (t)
10541 && CLASSTYPE_IS_TEMPLATE (t));
10542 /* If the member is a class template, then -- even after
10543 substitution -- there may be dependent types in the
10544 template argument list for the class. We increment
10545 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10546 that function will assume that no types are dependent
10547 when outside of a template. */
10548 if (class_template_p)
10549 ++processing_template_decl;
10550 newtag = tsubst (t, args, tf_error, NULL_TREE);
10551 if (class_template_p)
10552 --processing_template_decl;
10553 if (newtag == error_mark_node)
10554 continue;
10556 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10558 tree name = TYPE_IDENTIFIER (t);
10560 if (class_template_p)
10561 /* Unfortunately, lookup_template_class sets
10562 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10563 instantiation (i.e., for the type of a member
10564 template class nested within a template class.)
10565 This behavior is required for
10566 maybe_process_partial_specialization to work
10567 correctly, but is not accurate in this case;
10568 the TAG is not an instantiation of anything.
10569 (The corresponding TEMPLATE_DECL is an
10570 instantiation, but the TYPE is not.) */
10571 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10573 /* Now, we call pushtag to put this NEWTAG into the scope of
10574 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10575 pushtag calling push_template_decl. We don't have to do
10576 this for enums because it will already have been done in
10577 tsubst_enum. */
10578 if (name)
10579 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10580 pushtag (name, newtag, /*tag_scope=*/ts_current);
10583 else if (DECL_DECLARES_FUNCTION_P (t))
10585 tree r;
10587 if (TREE_CODE (t) == TEMPLATE_DECL)
10588 ++processing_template_decl;
10589 r = tsubst (t, args, tf_error, NULL_TREE);
10590 if (TREE_CODE (t) == TEMPLATE_DECL)
10591 --processing_template_decl;
10592 set_current_access_from_decl (r);
10593 finish_member_declaration (r);
10594 /* Instantiate members marked with attribute used. */
10595 if (r != error_mark_node && DECL_PRESERVE_P (r))
10596 mark_used (r);
10597 if (TREE_CODE (r) == FUNCTION_DECL
10598 && DECL_OMP_DECLARE_REDUCTION_P (r))
10599 cp_check_omp_declare_reduction (r);
10601 else if (DECL_CLASS_TEMPLATE_P (t)
10602 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10603 /* A closure type for a lambda in a default argument for a
10604 member template. Ignore it; it will be instantiated with
10605 the default argument. */;
10606 else
10608 /* Build new TYPE_FIELDS. */
10609 if (TREE_CODE (t) == STATIC_ASSERT)
10611 tree condition;
10613 ++c_inhibit_evaluation_warnings;
10614 condition =
10615 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10616 tf_warning_or_error, NULL_TREE,
10617 /*integral_constant_expression_p=*/true);
10618 --c_inhibit_evaluation_warnings;
10620 finish_static_assert (condition,
10621 STATIC_ASSERT_MESSAGE (t),
10622 STATIC_ASSERT_SOURCE_LOCATION (t),
10623 /*member_p=*/true);
10625 else if (TREE_CODE (t) != CONST_DECL)
10627 tree r;
10628 tree vec = NULL_TREE;
10629 int len = 1;
10631 /* The file and line for this declaration, to
10632 assist in error message reporting. Since we
10633 called push_tinst_level above, we don't need to
10634 restore these. */
10635 input_location = DECL_SOURCE_LOCATION (t);
10637 if (TREE_CODE (t) == TEMPLATE_DECL)
10638 ++processing_template_decl;
10639 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10640 if (TREE_CODE (t) == TEMPLATE_DECL)
10641 --processing_template_decl;
10643 if (TREE_CODE (r) == TREE_VEC)
10645 /* A capture pack became multiple fields. */
10646 vec = r;
10647 len = TREE_VEC_LENGTH (vec);
10650 for (int i = 0; i < len; ++i)
10652 if (vec)
10653 r = TREE_VEC_ELT (vec, i);
10654 if (VAR_P (r))
10656 /* In [temp.inst]:
10658 [t]he initialization (and any associated
10659 side-effects) of a static data member does
10660 not occur unless the static data member is
10661 itself used in a way that requires the
10662 definition of the static data member to
10663 exist.
10665 Therefore, we do not substitute into the
10666 initialized for the static data member here. */
10667 finish_static_data_member_decl
10669 /*init=*/NULL_TREE,
10670 /*init_const_expr_p=*/false,
10671 /*asmspec_tree=*/NULL_TREE,
10672 /*flags=*/0);
10673 /* Instantiate members marked with attribute used. */
10674 if (r != error_mark_node && DECL_PRESERVE_P (r))
10675 mark_used (r);
10677 else if (TREE_CODE (r) == FIELD_DECL)
10679 /* Determine whether R has a valid type and can be
10680 completed later. If R is invalid, then its type
10681 is replaced by error_mark_node. */
10682 tree rtype = TREE_TYPE (r);
10683 if (can_complete_type_without_circularity (rtype))
10684 complete_type (rtype);
10686 if (!complete_or_array_type_p (rtype))
10688 /* If R's type couldn't be completed and
10689 it isn't a flexible array member (whose
10690 type is incomplete by definition) give
10691 an error. */
10692 cxx_incomplete_type_error (r, rtype);
10693 TREE_TYPE (r) = error_mark_node;
10697 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10698 such a thing will already have been added to the field
10699 list by tsubst_enum in finish_member_declaration in the
10700 CLASSTYPE_NESTED_UTDS case above. */
10701 if (!(TREE_CODE (r) == TYPE_DECL
10702 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10703 && DECL_ARTIFICIAL (r)))
10705 set_current_access_from_decl (r);
10706 finish_member_declaration (r);
10712 else
10714 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10715 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10717 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10719 tree friend_type = t;
10720 bool adjust_processing_template_decl = false;
10722 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10724 /* template <class T> friend class C; */
10725 friend_type = tsubst_friend_class (friend_type, args);
10726 adjust_processing_template_decl = true;
10728 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10730 /* template <class T> friend class C::D; */
10731 friend_type = tsubst (friend_type, args,
10732 tf_warning_or_error, NULL_TREE);
10733 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10734 friend_type = TREE_TYPE (friend_type);
10735 adjust_processing_template_decl = true;
10737 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10738 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10740 /* This could be either
10742 friend class T::C;
10744 when dependent_type_p is false or
10746 template <class U> friend class T::C;
10748 otherwise. */
10749 /* Bump processing_template_decl in case this is something like
10750 template <class T> friend struct A<T>::B. */
10751 ++processing_template_decl;
10752 friend_type = tsubst (friend_type, args,
10753 tf_warning_or_error, NULL_TREE);
10754 if (dependent_type_p (friend_type))
10755 adjust_processing_template_decl = true;
10756 --processing_template_decl;
10758 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10759 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10760 && TYPE_HIDDEN_P (friend_type))
10762 /* friend class C;
10764 where C hasn't been declared yet. Let's lookup name
10765 from namespace scope directly, bypassing any name that
10766 come from dependent base class. */
10767 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10769 /* The call to xref_tag_from_type does injection for friend
10770 classes. */
10771 push_nested_namespace (ns);
10772 friend_type =
10773 xref_tag_from_type (friend_type, NULL_TREE,
10774 /*tag_scope=*/ts_current);
10775 pop_nested_namespace (ns);
10777 else if (uses_template_parms (friend_type))
10778 /* friend class C<T>; */
10779 friend_type = tsubst (friend_type, args,
10780 tf_warning_or_error, NULL_TREE);
10781 /* Otherwise it's
10783 friend class C;
10785 where C is already declared or
10787 friend class C<int>;
10789 We don't have to do anything in these cases. */
10791 if (adjust_processing_template_decl)
10792 /* Trick make_friend_class into realizing that the friend
10793 we're adding is a template, not an ordinary class. It's
10794 important that we use make_friend_class since it will
10795 perform some error-checking and output cross-reference
10796 information. */
10797 ++processing_template_decl;
10799 if (friend_type != error_mark_node)
10800 make_friend_class (type, friend_type, /*complain=*/false);
10802 if (adjust_processing_template_decl)
10803 --processing_template_decl;
10805 else
10807 /* Build new DECL_FRIENDLIST. */
10808 tree r;
10810 /* The file and line for this declaration, to
10811 assist in error message reporting. Since we
10812 called push_tinst_level above, we don't need to
10813 restore these. */
10814 input_location = DECL_SOURCE_LOCATION (t);
10816 if (TREE_CODE (t) == TEMPLATE_DECL)
10818 ++processing_template_decl;
10819 push_deferring_access_checks (dk_no_check);
10822 r = tsubst_friend_function (t, args);
10823 add_friend (type, r, /*complain=*/false);
10824 if (TREE_CODE (t) == TEMPLATE_DECL)
10826 pop_deferring_access_checks ();
10827 --processing_template_decl;
10833 if (fn_context)
10835 /* Restore these before substituting into the lambda capture
10836 initializers. */
10837 cp_unevaluated_operand = saved_unevaluated_operand;
10838 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10841 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10843 tree decl = lambda_function (type);
10844 if (decl)
10846 if (cxx_dialect >= cxx17)
10847 CLASSTYPE_LITERAL_P (type) = true;
10849 if (!DECL_TEMPLATE_INFO (decl)
10850 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10852 /* Set function_depth to avoid garbage collection. */
10853 ++function_depth;
10854 instantiate_decl (decl, /*defer_ok=*/false, false);
10855 --function_depth;
10858 /* We need to instantiate the capture list from the template
10859 after we've instantiated the closure members, but before we
10860 consider adding the conversion op. Also keep any captures
10861 that may have been added during instantiation of the op(). */
10862 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10863 tree tmpl_cap
10864 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10865 args, tf_warning_or_error, NULL_TREE,
10866 false, false);
10868 LAMBDA_EXPR_CAPTURE_LIST (expr)
10869 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10871 maybe_add_lambda_conv_op (type);
10873 else
10874 gcc_assert (errorcount);
10877 /* Set the file and line number information to whatever is given for
10878 the class itself. This puts error messages involving generated
10879 implicit functions at a predictable point, and the same point
10880 that would be used for non-template classes. */
10881 input_location = DECL_SOURCE_LOCATION (typedecl);
10883 unreverse_member_declarations (type);
10884 finish_struct_1 (type);
10885 TYPE_BEING_DEFINED (type) = 0;
10887 /* We don't instantiate default arguments for member functions. 14.7.1:
10889 The implicit instantiation of a class template specialization causes
10890 the implicit instantiation of the declarations, but not of the
10891 definitions or default arguments, of the class member functions,
10892 member classes, static data members and member templates.... */
10894 /* Some typedefs referenced from within the template code need to be access
10895 checked at template instantiation time, i.e now. These types were
10896 added to the template at parsing time. Let's get those and perform
10897 the access checks then. */
10898 perform_typedefs_access_check (pattern, args);
10899 perform_deferred_access_checks (tf_warning_or_error);
10900 pop_nested_class ();
10901 maximum_field_alignment = saved_maximum_field_alignment;
10902 if (!fn_context)
10903 pop_from_top_level ();
10904 pop_deferring_access_checks ();
10905 pop_tinst_level ();
10907 /* The vtable for a template class can be emitted in any translation
10908 unit in which the class is instantiated. When there is no key
10909 method, however, finish_struct_1 will already have added TYPE to
10910 the keyed_classes. */
10911 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10912 vec_safe_push (keyed_classes, type);
10914 return type;
10917 /* Wrapper for instantiate_class_template_1. */
10919 tree
10920 instantiate_class_template (tree type)
10922 tree ret;
10923 timevar_push (TV_TEMPLATE_INST);
10924 ret = instantiate_class_template_1 (type);
10925 timevar_pop (TV_TEMPLATE_INST);
10926 return ret;
10929 static tree
10930 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10932 tree r;
10934 if (!t)
10935 r = t;
10936 else if (TYPE_P (t))
10937 r = tsubst (t, args, complain, in_decl);
10938 else
10940 if (!(complain & tf_warning))
10941 ++c_inhibit_evaluation_warnings;
10942 r = tsubst_expr (t, args, complain, in_decl,
10943 /*integral_constant_expression_p=*/true);
10944 if (!(complain & tf_warning))
10945 --c_inhibit_evaluation_warnings;
10947 return r;
10950 /* Given a function parameter pack TMPL_PARM and some function parameters
10951 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10952 and set *SPEC_P to point at the next point in the list. */
10954 tree
10955 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10957 /* Collect all of the extra "packed" parameters into an
10958 argument pack. */
10959 tree parmvec;
10960 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10961 tree spec_parm = *spec_p;
10962 int i, len;
10964 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10965 if (tmpl_parm
10966 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10967 break;
10969 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10970 parmvec = make_tree_vec (len);
10971 spec_parm = *spec_p;
10972 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10973 TREE_VEC_ELT (parmvec, i) = spec_parm;
10975 /* Build the argument packs. */
10976 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10977 *spec_p = spec_parm;
10979 return argpack;
10982 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10983 NONTYPE_ARGUMENT_PACK. */
10985 static tree
10986 make_fnparm_pack (tree spec_parm)
10988 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10991 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10992 pack expansion with no extra args, 2 if it has extra args, or 0
10993 if it is not a pack expansion. */
10995 static int
10996 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10998 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10999 if (i >= TREE_VEC_LENGTH (vec))
11000 return 0;
11001 tree elt = TREE_VEC_ELT (vec, i);
11002 if (DECL_P (elt))
11003 /* A decl pack is itself an expansion. */
11004 elt = TREE_TYPE (elt);
11005 if (!PACK_EXPANSION_P (elt))
11006 return 0;
11007 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11008 return 2;
11009 return 1;
11013 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11015 static tree
11016 make_argument_pack_select (tree arg_pack, unsigned index)
11018 tree aps = make_node (ARGUMENT_PACK_SELECT);
11020 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11021 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11023 return aps;
11026 /* This is a subroutine of tsubst_pack_expansion.
11028 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11029 mechanism to store the (non complete list of) arguments of the
11030 substitution and return a non substituted pack expansion, in order
11031 to wait for when we have enough arguments to really perform the
11032 substitution. */
11034 static bool
11035 use_pack_expansion_extra_args_p (tree parm_packs,
11036 int arg_pack_len,
11037 bool has_empty_arg)
11039 /* If one pack has an expansion and another pack has a normal
11040 argument or if one pack has an empty argument and an another
11041 one hasn't then tsubst_pack_expansion cannot perform the
11042 substitution and need to fall back on the
11043 PACK_EXPANSION_EXTRA mechanism. */
11044 if (parm_packs == NULL_TREE)
11045 return false;
11046 else if (has_empty_arg)
11047 return true;
11049 bool has_expansion_arg = false;
11050 for (int i = 0 ; i < arg_pack_len; ++i)
11052 bool has_non_expansion_arg = false;
11053 for (tree parm_pack = parm_packs;
11054 parm_pack;
11055 parm_pack = TREE_CHAIN (parm_pack))
11057 tree arg = TREE_VALUE (parm_pack);
11059 int exp = argument_pack_element_is_expansion_p (arg, i);
11060 if (exp == 2)
11061 /* We can't substitute a pack expansion with extra args into
11062 our pattern. */
11063 return true;
11064 else if (exp)
11065 has_expansion_arg = true;
11066 else
11067 has_non_expansion_arg = true;
11070 if (has_expansion_arg && has_non_expansion_arg)
11071 return true;
11073 return false;
11076 /* [temp.variadic]/6 says that:
11078 The instantiation of a pack expansion [...]
11079 produces a list E1,E2, ..., En, where N is the number of elements
11080 in the pack expansion parameters.
11082 This subroutine of tsubst_pack_expansion produces one of these Ei.
11084 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11085 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11086 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11087 INDEX is the index 'i' of the element Ei to produce. ARGS,
11088 COMPLAIN, and IN_DECL are the same parameters as for the
11089 tsubst_pack_expansion function.
11091 The function returns the resulting Ei upon successful completion,
11092 or error_mark_node.
11094 Note that this function possibly modifies the ARGS parameter, so
11095 it's the responsibility of the caller to restore it. */
11097 static tree
11098 gen_elem_of_pack_expansion_instantiation (tree pattern,
11099 tree parm_packs,
11100 unsigned index,
11101 tree args /* This parm gets
11102 modified. */,
11103 tsubst_flags_t complain,
11104 tree in_decl)
11106 tree t;
11107 bool ith_elem_is_expansion = false;
11109 /* For each parameter pack, change the substitution of the parameter
11110 pack to the ith argument in its argument pack, then expand the
11111 pattern. */
11112 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11114 tree parm = TREE_PURPOSE (pack);
11115 tree arg_pack = TREE_VALUE (pack);
11116 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11118 ith_elem_is_expansion |=
11119 argument_pack_element_is_expansion_p (arg_pack, index);
11121 /* Select the Ith argument from the pack. */
11122 if (TREE_CODE (parm) == PARM_DECL
11123 || TREE_CODE (parm) == FIELD_DECL)
11125 if (index == 0)
11127 aps = make_argument_pack_select (arg_pack, index);
11128 if (!mark_used (parm, complain) && !(complain & tf_error))
11129 return error_mark_node;
11130 register_local_specialization (aps, parm);
11132 else
11133 aps = retrieve_local_specialization (parm);
11135 else
11137 int idx, level;
11138 template_parm_level_and_index (parm, &level, &idx);
11140 if (index == 0)
11142 aps = make_argument_pack_select (arg_pack, index);
11143 /* Update the corresponding argument. */
11144 TMPL_ARG (args, level, idx) = aps;
11146 else
11147 /* Re-use the ARGUMENT_PACK_SELECT. */
11148 aps = TMPL_ARG (args, level, idx);
11150 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11153 /* Substitute into the PATTERN with the (possibly altered)
11154 arguments. */
11155 if (pattern == in_decl)
11156 /* Expanding a fixed parameter pack from
11157 coerce_template_parameter_pack. */
11158 t = tsubst_decl (pattern, args, complain);
11159 else if (pattern == error_mark_node)
11160 t = error_mark_node;
11161 else if (constraint_p (pattern))
11163 if (processing_template_decl)
11164 t = tsubst_constraint (pattern, args, complain, in_decl);
11165 else
11166 t = (constraints_satisfied_p (pattern, args)
11167 ? boolean_true_node : boolean_false_node);
11169 else if (!TYPE_P (pattern))
11170 t = tsubst_expr (pattern, args, complain, in_decl,
11171 /*integral_constant_expression_p=*/false);
11172 else
11173 t = tsubst (pattern, args, complain, in_decl);
11175 /* If the Ith argument pack element is a pack expansion, then
11176 the Ith element resulting from the substituting is going to
11177 be a pack expansion as well. */
11178 if (ith_elem_is_expansion)
11179 t = make_pack_expansion (t);
11181 return t;
11184 /* When the unexpanded parameter pack in a fold expression expands to an empty
11185 sequence, the value of the expression is as follows; the program is
11186 ill-formed if the operator is not listed in this table.
11188 && true
11189 || false
11190 , void() */
11192 tree
11193 expand_empty_fold (tree t, tsubst_flags_t complain)
11195 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11196 if (!FOLD_EXPR_MODIFY_P (t))
11197 switch (code)
11199 case TRUTH_ANDIF_EXPR:
11200 return boolean_true_node;
11201 case TRUTH_ORIF_EXPR:
11202 return boolean_false_node;
11203 case COMPOUND_EXPR:
11204 return void_node;
11205 default:
11206 break;
11209 if (complain & tf_error)
11210 error_at (location_of (t),
11211 "fold of empty expansion over %O", code);
11212 return error_mark_node;
11215 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11216 form an expression that combines the two terms using the
11217 operator of T. */
11219 static tree
11220 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11222 tree op = FOLD_EXPR_OP (t);
11223 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11225 // Handle compound assignment operators.
11226 if (FOLD_EXPR_MODIFY_P (t))
11227 return build_x_modify_expr (input_location, left, code, right, complain);
11229 switch (code)
11231 case COMPOUND_EXPR:
11232 return build_x_compound_expr (input_location, left, right, complain);
11233 case DOTSTAR_EXPR:
11234 return build_m_component_ref (left, right, complain);
11235 default:
11236 return build_x_binary_op (input_location, code,
11237 left, TREE_CODE (left),
11238 right, TREE_CODE (right),
11239 /*overload=*/NULL,
11240 complain);
11244 /* Substitute ARGS into the pack of a fold expression T. */
11246 static inline tree
11247 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11249 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11252 /* Substitute ARGS into the pack of a fold expression T. */
11254 static inline tree
11255 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11257 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11260 /* Expand a PACK of arguments into a grouped as left fold.
11261 Given a pack containing elements A0, A1, ..., An and an
11262 operator @, this builds the expression:
11264 ((A0 @ A1) @ A2) ... @ An
11266 Note that PACK must not be empty.
11268 The operator is defined by the original fold expression T. */
11270 static tree
11271 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11273 tree left = TREE_VEC_ELT (pack, 0);
11274 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11276 tree right = TREE_VEC_ELT (pack, i);
11277 left = fold_expression (t, left, right, complain);
11279 return left;
11282 /* Substitute into a unary left fold expression. */
11284 static tree
11285 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11286 tree in_decl)
11288 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11289 if (pack == error_mark_node)
11290 return error_mark_node;
11291 if (PACK_EXPANSION_P (pack))
11293 tree r = copy_node (t);
11294 FOLD_EXPR_PACK (r) = pack;
11295 return r;
11297 if (TREE_VEC_LENGTH (pack) == 0)
11298 return expand_empty_fold (t, complain);
11299 else
11300 return expand_left_fold (t, pack, complain);
11303 /* Substitute into a binary left fold expression.
11305 Do ths by building a single (non-empty) vector of argumnts and
11306 building the expression from those elements. */
11308 static tree
11309 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11310 tree in_decl)
11312 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11313 if (pack == error_mark_node)
11314 return error_mark_node;
11315 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11316 if (init == error_mark_node)
11317 return error_mark_node;
11319 if (PACK_EXPANSION_P (pack))
11321 tree r = copy_node (t);
11322 FOLD_EXPR_PACK (r) = pack;
11323 FOLD_EXPR_INIT (r) = init;
11324 return r;
11327 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11328 TREE_VEC_ELT (vec, 0) = init;
11329 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11330 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11332 return expand_left_fold (t, vec, complain);
11335 /* Expand a PACK of arguments into a grouped as right fold.
11336 Given a pack containing elementns A0, A1, ..., and an
11337 operator @, this builds the expression:
11339 A0@ ... (An-2 @ (An-1 @ An))
11341 Note that PACK must not be empty.
11343 The operator is defined by the original fold expression T. */
11345 tree
11346 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11348 // Build the expression.
11349 int n = TREE_VEC_LENGTH (pack);
11350 tree right = TREE_VEC_ELT (pack, n - 1);
11351 for (--n; n != 0; --n)
11353 tree left = TREE_VEC_ELT (pack, n - 1);
11354 right = fold_expression (t, left, right, complain);
11356 return right;
11359 /* Substitute into a unary right fold expression. */
11361 static tree
11362 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11363 tree in_decl)
11365 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11366 if (pack == error_mark_node)
11367 return error_mark_node;
11368 if (PACK_EXPANSION_P (pack))
11370 tree r = copy_node (t);
11371 FOLD_EXPR_PACK (r) = pack;
11372 return r;
11374 if (TREE_VEC_LENGTH (pack) == 0)
11375 return expand_empty_fold (t, complain);
11376 else
11377 return expand_right_fold (t, pack, complain);
11380 /* Substitute into a binary right fold expression.
11382 Do ths by building a single (non-empty) vector of arguments and
11383 building the expression from those elements. */
11385 static tree
11386 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11387 tree in_decl)
11389 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11390 if (pack == error_mark_node)
11391 return error_mark_node;
11392 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11393 if (init == error_mark_node)
11394 return error_mark_node;
11396 if (PACK_EXPANSION_P (pack))
11398 tree r = copy_node (t);
11399 FOLD_EXPR_PACK (r) = pack;
11400 FOLD_EXPR_INIT (r) = init;
11401 return r;
11404 int n = TREE_VEC_LENGTH (pack);
11405 tree vec = make_tree_vec (n + 1);
11406 for (int i = 0; i < n; ++i)
11407 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11408 TREE_VEC_ELT (vec, n) = init;
11410 return expand_right_fold (t, vec, complain);
11414 /* Substitute ARGS into T, which is an pack expansion
11415 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11416 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11417 (if only a partial substitution could be performed) or
11418 ERROR_MARK_NODE if there was an error. */
11419 tree
11420 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11421 tree in_decl)
11423 tree pattern;
11424 tree pack, packs = NULL_TREE;
11425 bool unsubstituted_packs = false;
11426 int i, len = -1;
11427 tree result;
11428 hash_map<tree, tree> *saved_local_specializations = NULL;
11429 bool need_local_specializations = false;
11430 int levels;
11432 gcc_assert (PACK_EXPANSION_P (t));
11433 pattern = PACK_EXPANSION_PATTERN (t);
11435 /* Add in any args remembered from an earlier partial instantiation. */
11436 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11438 levels = TMPL_ARGS_DEPTH (args);
11440 /* Determine the argument packs that will instantiate the parameter
11441 packs used in the expansion expression. While we're at it,
11442 compute the number of arguments to be expanded and make sure it
11443 is consistent. */
11444 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11445 pack = TREE_CHAIN (pack))
11447 tree parm_pack = TREE_VALUE (pack);
11448 tree arg_pack = NULL_TREE;
11449 tree orig_arg = NULL_TREE;
11450 int level = 0;
11452 if (TREE_CODE (parm_pack) == BASES)
11454 gcc_assert (parm_pack == pattern);
11455 if (BASES_DIRECT (parm_pack))
11456 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11457 args, complain, in_decl, false));
11458 else
11459 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11460 args, complain, in_decl, false));
11462 else if (builtin_pack_call_p (parm_pack))
11464 /* ??? Support use in other patterns. */
11465 gcc_assert (parm_pack == pattern);
11466 return expand_builtin_pack_call (parm_pack, args,
11467 complain, in_decl);
11469 else if (TREE_CODE (parm_pack) == PARM_DECL)
11471 /* We know we have correct local_specializations if this
11472 expansion is at function scope, or if we're dealing with a
11473 local parameter in a requires expression; for the latter,
11474 tsubst_requires_expr set it up appropriately. */
11475 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11476 arg_pack = retrieve_local_specialization (parm_pack);
11477 else
11478 /* We can't rely on local_specializations for a parameter
11479 name used later in a function declaration (such as in a
11480 late-specified return type). Even if it exists, it might
11481 have the wrong value for a recursive call. */
11482 need_local_specializations = true;
11484 if (!arg_pack)
11486 /* This parameter pack was used in an unevaluated context. Just
11487 make a dummy decl, since it's only used for its type. */
11488 arg_pack = tsubst_decl (parm_pack, args, complain);
11489 if (arg_pack && DECL_PACK_P (arg_pack))
11490 /* Partial instantiation of the parm_pack, we can't build
11491 up an argument pack yet. */
11492 arg_pack = NULL_TREE;
11493 else
11494 arg_pack = make_fnparm_pack (arg_pack);
11497 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11498 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11499 else
11501 int idx;
11502 template_parm_level_and_index (parm_pack, &level, &idx);
11504 if (level <= levels)
11505 arg_pack = TMPL_ARG (args, level, idx);
11508 orig_arg = arg_pack;
11509 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11510 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11512 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11513 /* This can only happen if we forget to expand an argument
11514 pack somewhere else. Just return an error, silently. */
11516 result = make_tree_vec (1);
11517 TREE_VEC_ELT (result, 0) = error_mark_node;
11518 return result;
11521 if (arg_pack)
11523 int my_len =
11524 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11526 /* Don't bother trying to do a partial substitution with
11527 incomplete packs; we'll try again after deduction. */
11528 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11529 return t;
11531 if (len < 0)
11532 len = my_len;
11533 else if (len != my_len)
11535 if (!(complain & tf_error))
11536 /* Fail quietly. */;
11537 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11538 error ("mismatched argument pack lengths while expanding %qT",
11539 pattern);
11540 else
11541 error ("mismatched argument pack lengths while expanding %qE",
11542 pattern);
11543 return error_mark_node;
11546 /* Keep track of the parameter packs and their corresponding
11547 argument packs. */
11548 packs = tree_cons (parm_pack, arg_pack, packs);
11549 TREE_TYPE (packs) = orig_arg;
11551 else
11553 /* We can't substitute for this parameter pack. We use a flag as
11554 well as the missing_level counter because function parameter
11555 packs don't have a level. */
11556 gcc_assert (processing_template_decl);
11557 unsubstituted_packs = true;
11561 /* If the expansion is just T..., return the matching argument pack, unless
11562 we need to call convert_from_reference on all the elements. This is an
11563 important optimization; see c++/68422. */
11564 if (!unsubstituted_packs
11565 && TREE_PURPOSE (packs) == pattern)
11567 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11568 /* Types need no adjustment, nor does sizeof..., and if we still have
11569 some pack expansion args we won't do anything yet. */
11570 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11571 || PACK_EXPANSION_SIZEOF_P (t)
11572 || pack_expansion_args_count (args))
11573 return args;
11574 /* Also optimize expression pack expansions if we can tell that the
11575 elements won't have reference type. */
11576 tree type = TREE_TYPE (pattern);
11577 if (type && TREE_CODE (type) != REFERENCE_TYPE
11578 && !PACK_EXPANSION_P (type)
11579 && !WILDCARD_TYPE_P (type))
11580 return args;
11581 /* Otherwise use the normal path so we get convert_from_reference. */
11584 /* We cannot expand this expansion expression, because we don't have
11585 all of the argument packs we need. */
11586 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11588 /* We got some full packs, but we can't substitute them in until we
11589 have values for all the packs. So remember these until then. */
11591 t = make_pack_expansion (pattern);
11592 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11593 return t;
11595 else if (unsubstituted_packs)
11597 /* There were no real arguments, we're just replacing a parameter
11598 pack with another version of itself. Substitute into the
11599 pattern and return a PACK_EXPANSION_*. The caller will need to
11600 deal with that. */
11601 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11602 t = tsubst_expr (pattern, args, complain, in_decl,
11603 /*integral_constant_expression_p=*/false);
11604 else
11605 t = tsubst (pattern, args, complain, in_decl);
11606 t = make_pack_expansion (t);
11607 return t;
11610 gcc_assert (len >= 0);
11612 if (need_local_specializations)
11614 /* We're in a late-specified return type, so create our own local
11615 specializations map; the current map is either NULL or (in the
11616 case of recursive unification) might have bindings that we don't
11617 want to use or alter. */
11618 saved_local_specializations = local_specializations;
11619 local_specializations = new hash_map<tree, tree>;
11622 /* For each argument in each argument pack, substitute into the
11623 pattern. */
11624 result = make_tree_vec (len);
11625 tree elem_args = copy_template_args (args);
11626 for (i = 0; i < len; ++i)
11628 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11630 elem_args, complain,
11631 in_decl);
11632 TREE_VEC_ELT (result, i) = t;
11633 if (t == error_mark_node)
11635 result = error_mark_node;
11636 break;
11640 /* Update ARGS to restore the substitution from parameter packs to
11641 their argument packs. */
11642 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11644 tree parm = TREE_PURPOSE (pack);
11646 if (TREE_CODE (parm) == PARM_DECL
11647 || TREE_CODE (parm) == FIELD_DECL)
11648 register_local_specialization (TREE_TYPE (pack), parm);
11649 else
11651 int idx, level;
11653 if (TREE_VALUE (pack) == NULL_TREE)
11654 continue;
11656 template_parm_level_and_index (parm, &level, &idx);
11658 /* Update the corresponding argument. */
11659 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11660 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11661 TREE_TYPE (pack);
11662 else
11663 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11667 if (need_local_specializations)
11669 delete local_specializations;
11670 local_specializations = saved_local_specializations;
11673 /* If the dependent pack arguments were such that we end up with only a
11674 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11675 if (len == 1 && TREE_CODE (result) == TREE_VEC
11676 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11677 return TREE_VEC_ELT (result, 0);
11679 return result;
11682 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11683 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11684 parameter packs; all parms generated from a function parameter pack will
11685 have the same DECL_PARM_INDEX. */
11687 tree
11688 get_pattern_parm (tree parm, tree tmpl)
11690 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11691 tree patparm;
11693 if (DECL_ARTIFICIAL (parm))
11695 for (patparm = DECL_ARGUMENTS (pattern);
11696 patparm; patparm = DECL_CHAIN (patparm))
11697 if (DECL_ARTIFICIAL (patparm)
11698 && DECL_NAME (parm) == DECL_NAME (patparm))
11699 break;
11701 else
11703 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11704 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11705 gcc_assert (DECL_PARM_INDEX (patparm)
11706 == DECL_PARM_INDEX (parm));
11709 return patparm;
11712 /* Make an argument pack out of the TREE_VEC VEC. */
11714 static tree
11715 make_argument_pack (tree vec)
11717 tree pack;
11718 tree elt = TREE_VEC_ELT (vec, 0);
11719 if (TYPE_P (elt))
11720 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11721 else
11723 pack = make_node (NONTYPE_ARGUMENT_PACK);
11724 TREE_CONSTANT (pack) = 1;
11726 SET_ARGUMENT_PACK_ARGS (pack, vec);
11727 return pack;
11730 /* Return an exact copy of template args T that can be modified
11731 independently. */
11733 static tree
11734 copy_template_args (tree t)
11736 if (t == error_mark_node)
11737 return t;
11739 int len = TREE_VEC_LENGTH (t);
11740 tree new_vec = make_tree_vec (len);
11742 for (int i = 0; i < len; ++i)
11744 tree elt = TREE_VEC_ELT (t, i);
11745 if (elt && TREE_CODE (elt) == TREE_VEC)
11746 elt = copy_template_args (elt);
11747 TREE_VEC_ELT (new_vec, i) = elt;
11750 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11751 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11753 return new_vec;
11756 /* Substitute ARGS into the vector or list of template arguments T. */
11758 static tree
11759 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11761 tree orig_t = t;
11762 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11763 tree *elts;
11765 if (t == error_mark_node)
11766 return error_mark_node;
11768 len = TREE_VEC_LENGTH (t);
11769 elts = XALLOCAVEC (tree, len);
11771 for (i = 0; i < len; i++)
11773 tree orig_arg = TREE_VEC_ELT (t, i);
11774 tree new_arg;
11776 if (TREE_CODE (orig_arg) == TREE_VEC)
11777 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11778 else if (PACK_EXPANSION_P (orig_arg))
11780 /* Substitute into an expansion expression. */
11781 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11783 if (TREE_CODE (new_arg) == TREE_VEC)
11784 /* Add to the expanded length adjustment the number of
11785 expanded arguments. We subtract one from this
11786 measurement, because the argument pack expression
11787 itself is already counted as 1 in
11788 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11789 the argument pack is empty. */
11790 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11792 else if (ARGUMENT_PACK_P (orig_arg))
11794 /* Substitute into each of the arguments. */
11795 new_arg = TYPE_P (orig_arg)
11796 ? cxx_make_type (TREE_CODE (orig_arg))
11797 : make_node (TREE_CODE (orig_arg));
11799 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11800 args, complain, in_decl);
11801 if (pack_args == error_mark_node)
11802 new_arg = error_mark_node;
11803 else
11804 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11806 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11807 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11809 else
11810 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11812 if (new_arg == error_mark_node)
11813 return error_mark_node;
11815 elts[i] = new_arg;
11816 if (new_arg != orig_arg)
11817 need_new = 1;
11820 if (!need_new)
11821 return t;
11823 /* Make space for the expanded arguments coming from template
11824 argument packs. */
11825 t = make_tree_vec (len + expanded_len_adjust);
11826 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11827 arguments for a member template.
11828 In that case each TREE_VEC in ORIG_T represents a level of template
11829 arguments, and ORIG_T won't carry any non defaulted argument count.
11830 It will rather be the nested TREE_VECs that will carry one.
11831 In other words, ORIG_T carries a non defaulted argument count only
11832 if it doesn't contain any nested TREE_VEC. */
11833 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11835 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11836 count += expanded_len_adjust;
11837 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11839 for (i = 0, out = 0; i < len; i++)
11841 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11842 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11843 && TREE_CODE (elts[i]) == TREE_VEC)
11845 int idx;
11847 /* Now expand the template argument pack "in place". */
11848 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11849 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11851 else
11853 TREE_VEC_ELT (t, out) = elts[i];
11854 out++;
11858 return t;
11861 /* Substitute ARGS into one level PARMS of template parameters. */
11863 static tree
11864 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11866 if (parms == error_mark_node)
11867 return error_mark_node;
11869 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11871 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11873 tree tuple = TREE_VEC_ELT (parms, i);
11875 if (tuple == error_mark_node)
11876 continue;
11878 TREE_VEC_ELT (new_vec, i) =
11879 tsubst_template_parm (tuple, args, complain);
11882 return new_vec;
11885 /* Return the result of substituting ARGS into the template parameters
11886 given by PARMS. If there are m levels of ARGS and m + n levels of
11887 PARMS, then the result will contain n levels of PARMS. For
11888 example, if PARMS is `template <class T> template <class U>
11889 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11890 result will be `template <int*, double, class V>'. */
11892 static tree
11893 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11895 tree r = NULL_TREE;
11896 tree* new_parms;
11898 /* When substituting into a template, we must set
11899 PROCESSING_TEMPLATE_DECL as the template parameters may be
11900 dependent if they are based on one-another, and the dependency
11901 predicates are short-circuit outside of templates. */
11902 ++processing_template_decl;
11904 for (new_parms = &r;
11905 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11906 new_parms = &(TREE_CHAIN (*new_parms)),
11907 parms = TREE_CHAIN (parms))
11909 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11910 args, complain);
11911 *new_parms =
11912 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11913 - TMPL_ARGS_DEPTH (args)),
11914 new_vec, NULL_TREE);
11917 --processing_template_decl;
11919 return r;
11922 /* Return the result of substituting ARGS into one template parameter
11923 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11924 parameter and which TREE_PURPOSE is the default argument of the
11925 template parameter. */
11927 static tree
11928 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11930 tree default_value, parm_decl;
11932 if (args == NULL_TREE
11933 || t == NULL_TREE
11934 || t == error_mark_node)
11935 return t;
11937 gcc_assert (TREE_CODE (t) == TREE_LIST);
11939 default_value = TREE_PURPOSE (t);
11940 parm_decl = TREE_VALUE (t);
11942 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11943 if (TREE_CODE (parm_decl) == PARM_DECL
11944 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11945 parm_decl = error_mark_node;
11946 default_value = tsubst_template_arg (default_value, args,
11947 complain, NULL_TREE);
11949 return build_tree_list (default_value, parm_decl);
11952 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11953 type T. If T is not an aggregate or enumeration type, it is
11954 handled as if by tsubst. IN_DECL is as for tsubst. If
11955 ENTERING_SCOPE is nonzero, T is the context for a template which
11956 we are presently tsubst'ing. Return the substituted value. */
11958 static tree
11959 tsubst_aggr_type (tree t,
11960 tree args,
11961 tsubst_flags_t complain,
11962 tree in_decl,
11963 int entering_scope)
11965 if (t == NULL_TREE)
11966 return NULL_TREE;
11968 switch (TREE_CODE (t))
11970 case RECORD_TYPE:
11971 if (TYPE_PTRMEMFUNC_P (t))
11972 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11974 /* Fall through. */
11975 case ENUMERAL_TYPE:
11976 case UNION_TYPE:
11977 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11979 tree argvec;
11980 tree context;
11981 tree r;
11982 int saved_unevaluated_operand;
11983 int saved_inhibit_evaluation_warnings;
11985 /* In "sizeof(X<I>)" we need to evaluate "I". */
11986 saved_unevaluated_operand = cp_unevaluated_operand;
11987 cp_unevaluated_operand = 0;
11988 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11989 c_inhibit_evaluation_warnings = 0;
11991 /* First, determine the context for the type we are looking
11992 up. */
11993 context = TYPE_CONTEXT (t);
11994 if (context && TYPE_P (context))
11996 context = tsubst_aggr_type (context, args, complain,
11997 in_decl, /*entering_scope=*/1);
11998 /* If context is a nested class inside a class template,
11999 it may still need to be instantiated (c++/33959). */
12000 context = complete_type (context);
12003 /* Then, figure out what arguments are appropriate for the
12004 type we are trying to find. For example, given:
12006 template <class T> struct S;
12007 template <class T, class U> void f(T, U) { S<U> su; }
12009 and supposing that we are instantiating f<int, double>,
12010 then our ARGS will be {int, double}, but, when looking up
12011 S we only want {double}. */
12012 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12013 complain, in_decl);
12014 if (argvec == error_mark_node)
12015 r = error_mark_node;
12016 else
12018 r = lookup_template_class (t, argvec, in_decl, context,
12019 entering_scope, complain);
12020 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12023 cp_unevaluated_operand = saved_unevaluated_operand;
12024 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12026 return r;
12028 else
12029 /* This is not a template type, so there's nothing to do. */
12030 return t;
12032 default:
12033 return tsubst (t, args, complain, in_decl);
12037 static GTY(()) hash_map<tree, tree> *defarg_inst;
12039 /* Substitute into the default argument ARG (a default argument for
12040 FN), which has the indicated TYPE. */
12042 tree
12043 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12044 tsubst_flags_t complain)
12046 tree saved_class_ptr = NULL_TREE;
12047 tree saved_class_ref = NULL_TREE;
12048 int errs = errorcount + sorrycount;
12050 /* This can happen in invalid code. */
12051 if (TREE_CODE (arg) == DEFAULT_ARG)
12052 return arg;
12054 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12055 parm = chain_index (parmnum, parm);
12056 tree parmtype = TREE_TYPE (parm);
12057 if (DECL_BY_REFERENCE (parm))
12058 parmtype = TREE_TYPE (parmtype);
12059 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12061 tree *slot;
12062 if (defarg_inst && (slot = defarg_inst->get (parm)))
12063 return *slot;
12065 /* This default argument came from a template. Instantiate the
12066 default argument here, not in tsubst. In the case of
12067 something like:
12069 template <class T>
12070 struct S {
12071 static T t();
12072 void f(T = t());
12075 we must be careful to do name lookup in the scope of S<T>,
12076 rather than in the current class. */
12077 push_access_scope (fn);
12078 /* The "this" pointer is not valid in a default argument. */
12079 if (cfun)
12081 saved_class_ptr = current_class_ptr;
12082 cp_function_chain->x_current_class_ptr = NULL_TREE;
12083 saved_class_ref = current_class_ref;
12084 cp_function_chain->x_current_class_ref = NULL_TREE;
12087 start_lambda_scope (parm);
12089 push_deferring_access_checks(dk_no_deferred);
12090 /* The default argument expression may cause implicitly defined
12091 member functions to be synthesized, which will result in garbage
12092 collection. We must treat this situation as if we were within
12093 the body of function so as to avoid collecting live data on the
12094 stack. */
12095 ++function_depth;
12096 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12097 complain, NULL_TREE,
12098 /*integral_constant_expression_p=*/false);
12099 --function_depth;
12100 pop_deferring_access_checks();
12102 finish_lambda_scope ();
12104 /* Restore the "this" pointer. */
12105 if (cfun)
12107 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12108 cp_function_chain->x_current_class_ref = saved_class_ref;
12111 if (errorcount+sorrycount > errs
12112 && (complain & tf_warning_or_error))
12113 inform (input_location,
12114 " when instantiating default argument for call to %qD", fn);
12116 /* Make sure the default argument is reasonable. */
12117 arg = check_default_argument (type, arg, complain);
12119 pop_access_scope (fn);
12121 if (arg != error_mark_node && !cp_unevaluated_operand)
12123 if (!defarg_inst)
12124 defarg_inst = hash_map<tree,tree>::create_ggc (37);
12125 defarg_inst->put (parm, arg);
12128 return arg;
12131 /* Substitute into all the default arguments for FN. */
12133 static void
12134 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12136 tree arg;
12137 tree tmpl_args;
12139 tmpl_args = DECL_TI_ARGS (fn);
12141 /* If this function is not yet instantiated, we certainly don't need
12142 its default arguments. */
12143 if (uses_template_parms (tmpl_args))
12144 return;
12145 /* Don't do this again for clones. */
12146 if (DECL_CLONED_FUNCTION_P (fn))
12147 return;
12149 int i = 0;
12150 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12151 arg;
12152 arg = TREE_CHAIN (arg), ++i)
12153 if (TREE_PURPOSE (arg))
12154 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12155 TREE_VALUE (arg),
12156 TREE_PURPOSE (arg),
12157 complain);
12160 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12162 static tree
12163 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12164 tree lambda_fntype)
12166 tree gen_tmpl, argvec;
12167 hashval_t hash = 0;
12168 tree in_decl = t;
12170 /* Nobody should be tsubst'ing into non-template functions. */
12171 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12173 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12175 /* If T is not dependent, just return it. */
12176 if (!uses_template_parms (DECL_TI_ARGS (t)))
12177 return t;
12179 /* Calculate the most general template of which R is a
12180 specialization, and the complete set of arguments used to
12181 specialize R. */
12182 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12183 argvec = tsubst_template_args (DECL_TI_ARGS
12184 (DECL_TEMPLATE_RESULT
12185 (DECL_TI_TEMPLATE (t))),
12186 args, complain, in_decl);
12187 if (argvec == error_mark_node)
12188 return error_mark_node;
12190 /* Check to see if we already have this specialization. */
12191 if (!lambda_fntype)
12193 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12194 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12195 return spec;
12198 /* We can see more levels of arguments than parameters if
12199 there was a specialization of a member template, like
12200 this:
12202 template <class T> struct S { template <class U> void f(); }
12203 template <> template <class U> void S<int>::f(U);
12205 Here, we'll be substituting into the specialization,
12206 because that's where we can find the code we actually
12207 want to generate, but we'll have enough arguments for
12208 the most general template.
12210 We also deal with the peculiar case:
12212 template <class T> struct S {
12213 template <class U> friend void f();
12215 template <class U> void f() {}
12216 template S<int>;
12217 template void f<double>();
12219 Here, the ARGS for the instantiation of will be {int,
12220 double}. But, we only need as many ARGS as there are
12221 levels of template parameters in CODE_PATTERN. We are
12222 careful not to get fooled into reducing the ARGS in
12223 situations like:
12225 template <class T> struct S { template <class U> void f(U); }
12226 template <class T> template <> void S<T>::f(int) {}
12228 which we can spot because the pattern will be a
12229 specialization in this case. */
12230 int args_depth = TMPL_ARGS_DEPTH (args);
12231 int parms_depth =
12232 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12234 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12235 args = get_innermost_template_args (args, parms_depth);
12237 else
12239 /* This special case arises when we have something like this:
12241 template <class T> struct S {
12242 friend void f<int>(int, double);
12245 Here, the DECL_TI_TEMPLATE for the friend declaration
12246 will be an IDENTIFIER_NODE. We are being called from
12247 tsubst_friend_function, and we want only to create a
12248 new decl (R) with appropriate types so that we can call
12249 determine_specialization. */
12250 gen_tmpl = NULL_TREE;
12251 argvec = NULL_TREE;
12254 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12255 : NULL_TREE);
12256 tree ctx = closure ? closure : DECL_CONTEXT (t);
12257 bool member = ctx && TYPE_P (ctx);
12259 if (member && !closure)
12260 ctx = tsubst_aggr_type (ctx, args,
12261 complain, t, /*entering_scope=*/1);
12263 tree type = (lambda_fntype ? lambda_fntype
12264 : tsubst (TREE_TYPE (t), args,
12265 complain | tf_fndecl_type, in_decl));
12266 if (type == error_mark_node)
12267 return error_mark_node;
12269 /* If we hit excessive deduction depth, the type is bogus even if
12270 it isn't error_mark_node, so don't build a decl. */
12271 if (excessive_deduction_depth)
12272 return error_mark_node;
12274 /* We do NOT check for matching decls pushed separately at this
12275 point, as they may not represent instantiations of this
12276 template, and in any case are considered separate under the
12277 discrete model. */
12278 tree r = copy_decl (t);
12279 DECL_USE_TEMPLATE (r) = 0;
12280 TREE_TYPE (r) = type;
12281 /* Clear out the mangled name and RTL for the instantiation. */
12282 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12283 SET_DECL_RTL (r, NULL);
12284 /* Leave DECL_INITIAL set on deleted instantiations. */
12285 if (!DECL_DELETED_FN (r))
12286 DECL_INITIAL (r) = NULL_TREE;
12287 DECL_CONTEXT (r) = ctx;
12289 /* OpenMP UDRs have the only argument a reference to the declared
12290 type. We want to diagnose if the declared type is a reference,
12291 which is invalid, but as references to references are usually
12292 quietly merged, diagnose it here. */
12293 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12295 tree argtype
12296 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12297 argtype = tsubst (argtype, args, complain, in_decl);
12298 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12299 error_at (DECL_SOURCE_LOCATION (t),
12300 "reference type %qT in "
12301 "%<#pragma omp declare reduction%>", argtype);
12302 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12303 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12304 argtype);
12307 if (member && DECL_CONV_FN_P (r))
12308 /* Type-conversion operator. Reconstruct the name, in
12309 case it's the name of one of the template's parameters. */
12310 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12312 tree parms = DECL_ARGUMENTS (t);
12313 if (closure)
12314 parms = DECL_CHAIN (parms);
12315 parms = tsubst (parms, args, complain, t);
12316 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12317 DECL_CONTEXT (parm) = r;
12318 if (closure)
12320 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12321 DECL_CHAIN (tparm) = parms;
12322 parms = tparm;
12324 DECL_ARGUMENTS (r) = parms;
12325 DECL_RESULT (r) = NULL_TREE;
12327 TREE_STATIC (r) = 0;
12328 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12329 DECL_EXTERNAL (r) = 1;
12330 /* If this is an instantiation of a function with internal
12331 linkage, we already know what object file linkage will be
12332 assigned to the instantiation. */
12333 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12334 DECL_DEFER_OUTPUT (r) = 0;
12335 DECL_CHAIN (r) = NULL_TREE;
12336 DECL_PENDING_INLINE_INFO (r) = 0;
12337 DECL_PENDING_INLINE_P (r) = 0;
12338 DECL_SAVED_TREE (r) = NULL_TREE;
12339 DECL_STRUCT_FUNCTION (r) = NULL;
12340 TREE_USED (r) = 0;
12341 /* We'll re-clone as appropriate in instantiate_template. */
12342 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12344 /* If we aren't complaining now, return on error before we register
12345 the specialization so that we'll complain eventually. */
12346 if ((complain & tf_error) == 0
12347 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12348 && !grok_op_properties (r, /*complain=*/false))
12349 return error_mark_node;
12351 /* When instantiating a constrained member, substitute
12352 into the constraints to create a new constraint. */
12353 if (tree ci = get_constraints (t))
12354 if (member)
12356 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12357 set_constraints (r, ci);
12360 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12361 this in the special friend case mentioned above where
12362 GEN_TMPL is NULL. */
12363 if (gen_tmpl && !closure)
12365 DECL_TEMPLATE_INFO (r)
12366 = build_template_info (gen_tmpl, argvec);
12367 SET_DECL_IMPLICIT_INSTANTIATION (r);
12369 tree new_r
12370 = register_specialization (r, gen_tmpl, argvec, false, hash);
12371 if (new_r != r)
12372 /* We instantiated this while substituting into
12373 the type earlier (template/friend54.C). */
12374 return new_r;
12376 /* We're not supposed to instantiate default arguments
12377 until they are called, for a template. But, for a
12378 declaration like:
12380 template <class T> void f ()
12381 { extern void g(int i = T()); }
12383 we should do the substitution when the template is
12384 instantiated. We handle the member function case in
12385 instantiate_class_template since the default arguments
12386 might refer to other members of the class. */
12387 if (!member
12388 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12389 && !uses_template_parms (argvec))
12390 tsubst_default_arguments (r, complain);
12392 else
12393 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12395 /* Copy the list of befriending classes. */
12396 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12397 *friends;
12398 friends = &TREE_CHAIN (*friends))
12400 *friends = copy_node (*friends);
12401 TREE_VALUE (*friends)
12402 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12405 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12407 maybe_retrofit_in_chrg (r);
12408 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12409 return error_mark_node;
12410 /* If this is an instantiation of a member template, clone it.
12411 If it isn't, that'll be handled by
12412 clone_constructors_and_destructors. */
12413 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12414 clone_function_decl (r, /*update_methods=*/false);
12416 else if ((complain & tf_error) != 0
12417 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12418 && !grok_op_properties (r, /*complain=*/true))
12419 return error_mark_node;
12421 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12422 SET_DECL_FRIEND_CONTEXT (r,
12423 tsubst (DECL_FRIEND_CONTEXT (t),
12424 args, complain, in_decl));
12426 /* Possibly limit visibility based on template args. */
12427 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12428 if (DECL_VISIBILITY_SPECIFIED (t))
12430 DECL_VISIBILITY_SPECIFIED (r) = 0;
12431 DECL_ATTRIBUTES (r)
12432 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12434 determine_visibility (r);
12435 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12436 && !processing_template_decl)
12437 defaulted_late_check (r);
12439 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12440 args, complain, in_decl);
12441 return r;
12444 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12446 static tree
12447 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12448 tree lambda_fntype)
12450 /* We can get here when processing a member function template,
12451 member class template, or template template parameter. */
12452 tree decl = DECL_TEMPLATE_RESULT (t);
12453 tree in_decl = t;
12454 tree spec;
12455 tree tmpl_args;
12456 tree full_args;
12457 tree r;
12458 hashval_t hash = 0;
12460 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12462 /* Template template parameter is treated here. */
12463 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12464 if (new_type == error_mark_node)
12465 r = error_mark_node;
12466 /* If we get a real template back, return it. This can happen in
12467 the context of most_specialized_partial_spec. */
12468 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12469 r = new_type;
12470 else
12471 /* The new TEMPLATE_DECL was built in
12472 reduce_template_parm_level. */
12473 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12474 return r;
12477 if (!lambda_fntype)
12479 /* We might already have an instance of this template.
12480 The ARGS are for the surrounding class type, so the
12481 full args contain the tsubst'd args for the context,
12482 plus the innermost args from the template decl. */
12483 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12484 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12485 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12486 /* Because this is a template, the arguments will still be
12487 dependent, even after substitution. If
12488 PROCESSING_TEMPLATE_DECL is not set, the dependency
12489 predicates will short-circuit. */
12490 ++processing_template_decl;
12491 full_args = tsubst_template_args (tmpl_args, args,
12492 complain, in_decl);
12493 --processing_template_decl;
12494 if (full_args == error_mark_node)
12495 return error_mark_node;
12497 /* If this is a default template template argument,
12498 tsubst might not have changed anything. */
12499 if (full_args == tmpl_args)
12500 return t;
12502 hash = hash_tmpl_and_args (t, full_args);
12503 spec = retrieve_specialization (t, full_args, hash);
12504 if (spec != NULL_TREE)
12505 return spec;
12508 /* Make a new template decl. It will be similar to the
12509 original, but will record the current template arguments.
12510 We also create a new function declaration, which is just
12511 like the old one, but points to this new template, rather
12512 than the old one. */
12513 r = copy_decl (t);
12514 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12515 DECL_CHAIN (r) = NULL_TREE;
12517 // Build new template info linking to the original template decl.
12518 if (!lambda_fntype)
12520 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12521 SET_DECL_IMPLICIT_INSTANTIATION (r);
12523 else
12524 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12526 /* The template parameters for this new template are all the
12527 template parameters for the old template, except the
12528 outermost level of parameters. */
12529 DECL_TEMPLATE_PARMS (r)
12530 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12531 complain);
12533 if (TREE_CODE (decl) == TYPE_DECL
12534 && !TYPE_DECL_ALIAS_P (decl))
12536 tree new_type;
12537 ++processing_template_decl;
12538 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12539 --processing_template_decl;
12540 if (new_type == error_mark_node)
12541 return error_mark_node;
12543 TREE_TYPE (r) = new_type;
12544 /* For a partial specialization, we need to keep pointing to
12545 the primary template. */
12546 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12547 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12548 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12549 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12550 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12552 else
12554 tree new_decl;
12555 ++processing_template_decl;
12556 if (TREE_CODE (decl) == FUNCTION_DECL)
12557 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12558 else
12559 new_decl = tsubst (decl, args, complain, in_decl);
12560 --processing_template_decl;
12561 if (new_decl == error_mark_node)
12562 return error_mark_node;
12564 DECL_TEMPLATE_RESULT (r) = new_decl;
12565 TREE_TYPE (r) = TREE_TYPE (new_decl);
12566 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12567 if (lambda_fntype)
12569 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12570 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12572 else
12574 DECL_TI_TEMPLATE (new_decl) = r;
12575 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12579 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12580 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12582 if (PRIMARY_TEMPLATE_P (t))
12583 DECL_PRIMARY_TEMPLATE (r) = r;
12585 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12586 && !lambda_fntype)
12587 /* Record this non-type partial instantiation. */
12588 register_specialization (r, t,
12589 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12590 false, hash);
12592 return r;
12595 /* True if FN is the op() for a lambda in an uninstantiated template. */
12597 bool
12598 lambda_fn_in_template_p (tree fn)
12600 if (!fn || !LAMBDA_FUNCTION_P (fn))
12601 return false;
12602 tree closure = DECL_CONTEXT (fn);
12603 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12606 /* True if FN is the op() for a lambda regenerated from a lambda in an
12607 uninstantiated template. */
12609 bool
12610 regenerated_lambda_fn_p (tree fn)
12612 return (LAMBDA_FUNCTION_P (fn)
12613 && !DECL_TEMPLATE_INSTANTIATION (fn));
12616 /* We're instantiating a variable from template function TCTX. Return the
12617 corresponding current enclosing scope. This gets complicated because lambda
12618 functions in templates are regenerated rather than instantiated, but generic
12619 lambda functions are subsequently instantiated. */
12621 static tree
12622 enclosing_instantiation_of (tree tctx)
12624 tree fn = current_function_decl;
12625 int lambda_count = 0;
12627 for (; tctx && lambda_fn_in_template_p (tctx);
12628 tctx = decl_function_context (tctx))
12629 ++lambda_count;
12630 for (; fn; fn = decl_function_context (fn))
12632 tree lambda = fn;
12633 int flambda_count = 0;
12634 for (; fn && regenerated_lambda_fn_p (fn);
12635 fn = decl_function_context (fn))
12636 ++flambda_count;
12637 if (DECL_TEMPLATE_INFO (fn)
12638 ? most_general_template (fn) != most_general_template (tctx)
12639 : fn != tctx)
12640 continue;
12641 if (lambda_count)
12643 fn = lambda;
12644 while (flambda_count-- > lambda_count)
12645 fn = decl_function_context (fn);
12647 return fn;
12649 gcc_unreachable ();
12652 /* Substitute the ARGS into the T, which is a _DECL. Return the
12653 result of the substitution. Issue error and warning messages under
12654 control of COMPLAIN. */
12656 static tree
12657 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12659 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12660 location_t saved_loc;
12661 tree r = NULL_TREE;
12662 tree in_decl = t;
12663 hashval_t hash = 0;
12665 /* Set the filename and linenumber to improve error-reporting. */
12666 saved_loc = input_location;
12667 input_location = DECL_SOURCE_LOCATION (t);
12669 switch (TREE_CODE (t))
12671 case TEMPLATE_DECL:
12672 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12673 break;
12675 case FUNCTION_DECL:
12676 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12677 break;
12679 case PARM_DECL:
12681 tree type = NULL_TREE;
12682 int i, len = 1;
12683 tree expanded_types = NULL_TREE;
12684 tree prev_r = NULL_TREE;
12685 tree first_r = NULL_TREE;
12687 if (DECL_PACK_P (t))
12689 /* If there is a local specialization that isn't a
12690 parameter pack, it means that we're doing a "simple"
12691 substitution from inside tsubst_pack_expansion. Just
12692 return the local specialization (which will be a single
12693 parm). */
12694 tree spec = retrieve_local_specialization (t);
12695 if (spec
12696 && TREE_CODE (spec) == PARM_DECL
12697 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12698 RETURN (spec);
12700 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12701 the parameters in this function parameter pack. */
12702 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12703 complain, in_decl);
12704 if (TREE_CODE (expanded_types) == TREE_VEC)
12706 len = TREE_VEC_LENGTH (expanded_types);
12708 /* Zero-length parameter packs are boring. Just substitute
12709 into the chain. */
12710 if (len == 0)
12711 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12712 TREE_CHAIN (t)));
12714 else
12716 /* All we did was update the type. Make a note of that. */
12717 type = expanded_types;
12718 expanded_types = NULL_TREE;
12722 /* Loop through all of the parameters we'll build. When T is
12723 a function parameter pack, LEN is the number of expanded
12724 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12725 r = NULL_TREE;
12726 for (i = 0; i < len; ++i)
12728 prev_r = r;
12729 r = copy_node (t);
12730 if (DECL_TEMPLATE_PARM_P (t))
12731 SET_DECL_TEMPLATE_PARM_P (r);
12733 if (expanded_types)
12734 /* We're on the Ith parameter of the function parameter
12735 pack. */
12737 /* Get the Ith type. */
12738 type = TREE_VEC_ELT (expanded_types, i);
12740 /* Rename the parameter to include the index. */
12741 DECL_NAME (r)
12742 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12744 else if (!type)
12745 /* We're dealing with a normal parameter. */
12746 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12748 type = type_decays_to (type);
12749 TREE_TYPE (r) = type;
12750 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12752 if (DECL_INITIAL (r))
12754 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12755 DECL_INITIAL (r) = TREE_TYPE (r);
12756 else
12757 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12758 complain, in_decl);
12761 DECL_CONTEXT (r) = NULL_TREE;
12763 if (!DECL_TEMPLATE_PARM_P (r))
12764 DECL_ARG_TYPE (r) = type_passed_as (type);
12766 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12767 args, complain, in_decl);
12769 /* Keep track of the first new parameter we
12770 generate. That's what will be returned to the
12771 caller. */
12772 if (!first_r)
12773 first_r = r;
12775 /* Build a proper chain of parameters when substituting
12776 into a function parameter pack. */
12777 if (prev_r)
12778 DECL_CHAIN (prev_r) = r;
12781 /* If cp_unevaluated_operand is set, we're just looking for a
12782 single dummy parameter, so don't keep going. */
12783 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12784 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12785 complain, DECL_CHAIN (t));
12787 /* FIRST_R contains the start of the chain we've built. */
12788 r = first_r;
12790 break;
12792 case FIELD_DECL:
12794 tree type = NULL_TREE;
12795 tree vec = NULL_TREE;
12796 tree expanded_types = NULL_TREE;
12797 int len = 1;
12799 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12801 /* This field is a lambda capture pack. Return a TREE_VEC of
12802 the expanded fields to instantiate_class_template_1 and
12803 store them in the specializations hash table as a
12804 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12805 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12806 complain, in_decl);
12807 if (TREE_CODE (expanded_types) == TREE_VEC)
12809 len = TREE_VEC_LENGTH (expanded_types);
12810 vec = make_tree_vec (len);
12812 else
12814 /* All we did was update the type. Make a note of that. */
12815 type = expanded_types;
12816 expanded_types = NULL_TREE;
12820 for (int i = 0; i < len; ++i)
12822 r = copy_decl (t);
12823 if (expanded_types)
12825 type = TREE_VEC_ELT (expanded_types, i);
12826 DECL_NAME (r)
12827 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12829 else if (!type)
12830 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12832 if (type == error_mark_node)
12833 RETURN (error_mark_node);
12834 TREE_TYPE (r) = type;
12835 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12837 if (DECL_C_BIT_FIELD (r))
12838 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12839 number of bits. */
12840 DECL_BIT_FIELD_REPRESENTATIVE (r)
12841 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12842 complain, in_decl,
12843 /*integral_constant_expression_p=*/true);
12844 if (DECL_INITIAL (t))
12846 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12847 NSDMI in perform_member_init. Still set DECL_INITIAL
12848 so that we know there is one. */
12849 DECL_INITIAL (r) = void_node;
12850 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12851 retrofit_lang_decl (r);
12852 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12854 /* We don't have to set DECL_CONTEXT here; it is set by
12855 finish_member_declaration. */
12856 DECL_CHAIN (r) = NULL_TREE;
12858 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12859 args, complain, in_decl);
12861 if (vec)
12862 TREE_VEC_ELT (vec, i) = r;
12865 if (vec)
12867 r = vec;
12868 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12869 SET_ARGUMENT_PACK_ARGS (pack, vec);
12870 register_specialization (pack, t, args, false, 0);
12873 break;
12875 case USING_DECL:
12876 /* We reach here only for member using decls. We also need to check
12877 uses_template_parms because DECL_DEPENDENT_P is not set for a
12878 using-declaration that designates a member of the current
12879 instantiation (c++/53549). */
12880 if (DECL_DEPENDENT_P (t)
12881 || uses_template_parms (USING_DECL_SCOPE (t)))
12883 tree scope = USING_DECL_SCOPE (t);
12884 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12885 if (PACK_EXPANSION_P (scope))
12887 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12888 int len = TREE_VEC_LENGTH (vec);
12889 r = make_tree_vec (len);
12890 for (int i = 0; i < len; ++i)
12892 tree escope = TREE_VEC_ELT (vec, i);
12893 tree elt = do_class_using_decl (escope, name);
12894 if (!elt)
12896 r = error_mark_node;
12897 break;
12899 else
12901 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12902 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12904 TREE_VEC_ELT (r, i) = elt;
12907 else
12909 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12910 complain, in_decl);
12911 r = do_class_using_decl (inst_scope, name);
12912 if (!r)
12913 r = error_mark_node;
12914 else
12916 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12917 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12921 else
12923 r = copy_node (t);
12924 DECL_CHAIN (r) = NULL_TREE;
12926 break;
12928 case TYPE_DECL:
12929 case VAR_DECL:
12931 tree argvec = NULL_TREE;
12932 tree gen_tmpl = NULL_TREE;
12933 tree spec;
12934 tree tmpl = NULL_TREE;
12935 tree ctx;
12936 tree type = NULL_TREE;
12937 bool local_p;
12939 if (TREE_TYPE (t) == error_mark_node)
12940 RETURN (error_mark_node);
12942 if (TREE_CODE (t) == TYPE_DECL
12943 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12945 /* If this is the canonical decl, we don't have to
12946 mess with instantiations, and often we can't (for
12947 typename, template type parms and such). Note that
12948 TYPE_NAME is not correct for the above test if
12949 we've copied the type for a typedef. */
12950 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12951 if (type == error_mark_node)
12952 RETURN (error_mark_node);
12953 r = TYPE_NAME (type);
12954 break;
12957 /* Check to see if we already have the specialization we
12958 need. */
12959 spec = NULL_TREE;
12960 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12962 /* T is a static data member or namespace-scope entity.
12963 We have to substitute into namespace-scope variables
12964 (not just variable templates) because of cases like:
12966 template <class T> void f() { extern T t; }
12968 where the entity referenced is not known until
12969 instantiation time. */
12970 local_p = false;
12971 ctx = DECL_CONTEXT (t);
12972 if (DECL_CLASS_SCOPE_P (t))
12974 ctx = tsubst_aggr_type (ctx, args,
12975 complain,
12976 in_decl, /*entering_scope=*/1);
12977 /* If CTX is unchanged, then T is in fact the
12978 specialization we want. That situation occurs when
12979 referencing a static data member within in its own
12980 class. We can use pointer equality, rather than
12981 same_type_p, because DECL_CONTEXT is always
12982 canonical... */
12983 if (ctx == DECL_CONTEXT (t)
12984 /* ... unless T is a member template; in which
12985 case our caller can be willing to create a
12986 specialization of that template represented
12987 by T. */
12988 && !(DECL_TI_TEMPLATE (t)
12989 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12990 spec = t;
12993 if (!spec)
12995 tmpl = DECL_TI_TEMPLATE (t);
12996 gen_tmpl = most_general_template (tmpl);
12997 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12998 if (argvec != error_mark_node)
12999 argvec = (coerce_innermost_template_parms
13000 (DECL_TEMPLATE_PARMS (gen_tmpl),
13001 argvec, t, complain,
13002 /*all*/true, /*defarg*/true));
13003 if (argvec == error_mark_node)
13004 RETURN (error_mark_node);
13005 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13006 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13009 else
13011 /* A local variable. */
13012 local_p = true;
13013 /* Subsequent calls to pushdecl will fill this in. */
13014 ctx = NULL_TREE;
13015 /* Unless this is a reference to a static variable from an
13016 enclosing function, in which case we need to fill it in now. */
13017 if (TREE_STATIC (t))
13019 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13020 if (fn != current_function_decl)
13021 ctx = fn;
13023 spec = retrieve_local_specialization (t);
13025 /* If we already have the specialization we need, there is
13026 nothing more to do. */
13027 if (spec)
13029 r = spec;
13030 break;
13033 /* Create a new node for the specialization we need. */
13034 r = copy_decl (t);
13035 if (type == NULL_TREE)
13037 if (is_typedef_decl (t))
13038 type = DECL_ORIGINAL_TYPE (t);
13039 else
13040 type = TREE_TYPE (t);
13041 if (VAR_P (t)
13042 && VAR_HAD_UNKNOWN_BOUND (t)
13043 && type != error_mark_node)
13044 type = strip_array_domain (type);
13045 tree auto_node = type_uses_auto (type);
13046 int len = TREE_VEC_LENGTH (args);
13047 if (auto_node)
13048 /* Mask off any template args past the variable's context so we
13049 don't replace the auto with an unrelated argument. */
13050 TREE_VEC_LENGTH (args) = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13051 type = tsubst (type, args, complain, in_decl);
13052 if (auto_node)
13053 TREE_VEC_LENGTH (args) = len;
13055 if (VAR_P (r))
13057 /* Even if the original location is out of scope, the
13058 newly substituted one is not. */
13059 DECL_DEAD_FOR_LOCAL (r) = 0;
13060 DECL_INITIALIZED_P (r) = 0;
13061 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13062 if (type == error_mark_node)
13063 RETURN (error_mark_node);
13064 if (TREE_CODE (type) == FUNCTION_TYPE)
13066 /* It may seem that this case cannot occur, since:
13068 typedef void f();
13069 void g() { f x; }
13071 declares a function, not a variable. However:
13073 typedef void f();
13074 template <typename T> void g() { T t; }
13075 template void g<f>();
13077 is an attempt to declare a variable with function
13078 type. */
13079 error ("variable %qD has function type",
13080 /* R is not yet sufficiently initialized, so we
13081 just use its name. */
13082 DECL_NAME (r));
13083 RETURN (error_mark_node);
13085 type = complete_type (type);
13086 /* Wait until cp_finish_decl to set this again, to handle
13087 circular dependency (template/instantiate6.C). */
13088 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13089 type = check_var_type (DECL_NAME (r), type);
13091 if (DECL_HAS_VALUE_EXPR_P (t))
13093 tree ve = DECL_VALUE_EXPR (t);
13094 ve = tsubst_expr (ve, args, complain, in_decl,
13095 /*constant_expression_p=*/false);
13096 if (REFERENCE_REF_P (ve))
13098 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13099 ve = TREE_OPERAND (ve, 0);
13101 SET_DECL_VALUE_EXPR (r, ve);
13103 if (CP_DECL_THREAD_LOCAL_P (r)
13104 && !processing_template_decl)
13105 set_decl_tls_model (r, decl_default_tls_model (r));
13107 else if (DECL_SELF_REFERENCE_P (t))
13108 SET_DECL_SELF_REFERENCE_P (r);
13109 TREE_TYPE (r) = type;
13110 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13111 DECL_CONTEXT (r) = ctx;
13112 /* Clear out the mangled name and RTL for the instantiation. */
13113 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13114 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13115 SET_DECL_RTL (r, NULL);
13116 /* The initializer must not be expanded until it is required;
13117 see [temp.inst]. */
13118 DECL_INITIAL (r) = NULL_TREE;
13119 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13120 if (VAR_P (r))
13122 SET_DECL_MODE (r, VOIDmode);
13124 /* Possibly limit visibility based on template args. */
13125 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13126 if (DECL_VISIBILITY_SPECIFIED (t))
13128 DECL_VISIBILITY_SPECIFIED (r) = 0;
13129 DECL_ATTRIBUTES (r)
13130 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13132 determine_visibility (r);
13135 if (!local_p)
13137 /* A static data member declaration is always marked
13138 external when it is declared in-class, even if an
13139 initializer is present. We mimic the non-template
13140 processing here. */
13141 DECL_EXTERNAL (r) = 1;
13142 if (DECL_NAMESPACE_SCOPE_P (t))
13143 DECL_NOT_REALLY_EXTERN (r) = 1;
13145 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13146 SET_DECL_IMPLICIT_INSTANTIATION (r);
13147 register_specialization (r, gen_tmpl, argvec, false, hash);
13149 else
13151 if (DECL_LANG_SPECIFIC (r))
13152 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13153 if (!cp_unevaluated_operand)
13154 register_local_specialization (r, t);
13157 DECL_CHAIN (r) = NULL_TREE;
13159 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13160 /*flags=*/0,
13161 args, complain, in_decl);
13163 /* Preserve a typedef that names a type. */
13164 if (is_typedef_decl (r) && type != error_mark_node)
13166 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13167 set_underlying_type (r);
13168 if (TYPE_DECL_ALIAS_P (r))
13169 /* An alias template specialization can be dependent
13170 even if its underlying type is not. */
13171 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13174 layout_decl (r, 0);
13176 break;
13178 default:
13179 gcc_unreachable ();
13181 #undef RETURN
13183 out:
13184 /* Restore the file and line information. */
13185 input_location = saved_loc;
13187 return r;
13190 /* Substitute into the ARG_TYPES of a function type.
13191 If END is a TREE_CHAIN, leave it and any following types
13192 un-substituted. */
13194 static tree
13195 tsubst_arg_types (tree arg_types,
13196 tree args,
13197 tree end,
13198 tsubst_flags_t complain,
13199 tree in_decl)
13201 tree remaining_arg_types;
13202 tree type = NULL_TREE;
13203 int i = 1;
13204 tree expanded_args = NULL_TREE;
13205 tree default_arg;
13207 if (!arg_types || arg_types == void_list_node || arg_types == end)
13208 return arg_types;
13210 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13211 args, end, complain, in_decl);
13212 if (remaining_arg_types == error_mark_node)
13213 return error_mark_node;
13215 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13217 /* For a pack expansion, perform substitution on the
13218 entire expression. Later on, we'll handle the arguments
13219 one-by-one. */
13220 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13221 args, complain, in_decl);
13223 if (TREE_CODE (expanded_args) == TREE_VEC)
13224 /* So that we'll spin through the parameters, one by one. */
13225 i = TREE_VEC_LENGTH (expanded_args);
13226 else
13228 /* We only partially substituted into the parameter
13229 pack. Our type is TYPE_PACK_EXPANSION. */
13230 type = expanded_args;
13231 expanded_args = NULL_TREE;
13235 while (i > 0) {
13236 --i;
13238 if (expanded_args)
13239 type = TREE_VEC_ELT (expanded_args, i);
13240 else if (!type)
13241 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13243 if (type == error_mark_node)
13244 return error_mark_node;
13245 if (VOID_TYPE_P (type))
13247 if (complain & tf_error)
13249 error ("invalid parameter type %qT", type);
13250 if (in_decl)
13251 error ("in declaration %q+D", in_decl);
13253 return error_mark_node;
13255 /* DR 657. */
13256 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13257 return error_mark_node;
13259 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13260 top-level qualifiers as required. */
13261 type = cv_unqualified (type_decays_to (type));
13263 /* We do not substitute into default arguments here. The standard
13264 mandates that they be instantiated only when needed, which is
13265 done in build_over_call. */
13266 default_arg = TREE_PURPOSE (arg_types);
13268 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13269 since the new op() won't have any associated template arguments for us
13270 to refer to later. */
13271 if (lambda_fn_in_template_p (in_decl))
13272 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13273 false/*fn*/, false/*constexpr*/);
13275 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13277 /* We've instantiated a template before its default arguments
13278 have been parsed. This can happen for a nested template
13279 class, and is not an error unless we require the default
13280 argument in a call of this function. */
13281 remaining_arg_types =
13282 tree_cons (default_arg, type, remaining_arg_types);
13283 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13285 else
13286 remaining_arg_types =
13287 hash_tree_cons (default_arg, type, remaining_arg_types);
13290 return remaining_arg_types;
13293 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13294 *not* handle the exception-specification for FNTYPE, because the
13295 initial substitution of explicitly provided template parameters
13296 during argument deduction forbids substitution into the
13297 exception-specification:
13299 [temp.deduct]
13301 All references in the function type of the function template to the
13302 corresponding template parameters are replaced by the specified tem-
13303 plate argument values. If a substitution in a template parameter or
13304 in the function type of the function template results in an invalid
13305 type, type deduction fails. [Note: The equivalent substitution in
13306 exception specifications is done only when the function is instanti-
13307 ated, at which point a program is ill-formed if the substitution
13308 results in an invalid type.] */
13310 static tree
13311 tsubst_function_type (tree t,
13312 tree args,
13313 tsubst_flags_t complain,
13314 tree in_decl)
13316 tree return_type;
13317 tree arg_types = NULL_TREE;
13318 tree fntype;
13320 /* The TYPE_CONTEXT is not used for function/method types. */
13321 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13323 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13324 failure. */
13325 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13327 if (late_return_type_p)
13329 /* Substitute the argument types. */
13330 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13331 complain, in_decl);
13332 if (arg_types == error_mark_node)
13333 return error_mark_node;
13335 tree save_ccp = current_class_ptr;
13336 tree save_ccr = current_class_ref;
13337 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13338 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13339 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13340 if (do_inject)
13342 /* DR 1207: 'this' is in scope in the trailing return type. */
13343 inject_this_parameter (this_type, cp_type_quals (this_type));
13346 /* Substitute the return type. */
13347 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13349 if (do_inject)
13351 current_class_ptr = save_ccp;
13352 current_class_ref = save_ccr;
13355 else
13356 /* Substitute the return type. */
13357 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13359 if (return_type == error_mark_node)
13360 return error_mark_node;
13361 /* DR 486 clarifies that creation of a function type with an
13362 invalid return type is a deduction failure. */
13363 if (TREE_CODE (return_type) == ARRAY_TYPE
13364 || TREE_CODE (return_type) == FUNCTION_TYPE)
13366 if (complain & tf_error)
13368 if (TREE_CODE (return_type) == ARRAY_TYPE)
13369 error ("function returning an array");
13370 else
13371 error ("function returning a function");
13373 return error_mark_node;
13375 /* And DR 657. */
13376 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13377 return error_mark_node;
13379 if (!late_return_type_p)
13381 /* Substitute the argument types. */
13382 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13383 complain, in_decl);
13384 if (arg_types == error_mark_node)
13385 return error_mark_node;
13388 /* Construct a new type node and return it. */
13389 if (TREE_CODE (t) == FUNCTION_TYPE)
13391 fntype = build_function_type (return_type, arg_types);
13392 fntype = apply_memfn_quals (fntype,
13393 type_memfn_quals (t),
13394 type_memfn_rqual (t));
13396 else
13398 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13399 /* Don't pick up extra function qualifiers from the basetype. */
13400 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13401 if (! MAYBE_CLASS_TYPE_P (r))
13403 /* [temp.deduct]
13405 Type deduction may fail for any of the following
13406 reasons:
13408 -- Attempting to create "pointer to member of T" when T
13409 is not a class type. */
13410 if (complain & tf_error)
13411 error ("creating pointer to member function of non-class type %qT",
13413 return error_mark_node;
13416 fntype = build_method_type_directly (r, return_type,
13417 TREE_CHAIN (arg_types));
13418 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13420 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13422 if (late_return_type_p)
13423 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13425 return fntype;
13428 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13429 ARGS into that specification, and return the substituted
13430 specification. If there is no specification, return NULL_TREE. */
13432 static tree
13433 tsubst_exception_specification (tree fntype,
13434 tree args,
13435 tsubst_flags_t complain,
13436 tree in_decl,
13437 bool defer_ok)
13439 tree specs;
13440 tree new_specs;
13442 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13443 new_specs = NULL_TREE;
13444 if (specs && TREE_PURPOSE (specs))
13446 /* A noexcept-specifier. */
13447 tree expr = TREE_PURPOSE (specs);
13448 if (TREE_CODE (expr) == INTEGER_CST)
13449 new_specs = expr;
13450 else if (defer_ok)
13452 /* Defer instantiation of noexcept-specifiers to avoid
13453 excessive instantiations (c++/49107). */
13454 new_specs = make_node (DEFERRED_NOEXCEPT);
13455 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13457 /* We already partially instantiated this member template,
13458 so combine the new args with the old. */
13459 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13460 = DEFERRED_NOEXCEPT_PATTERN (expr);
13461 DEFERRED_NOEXCEPT_ARGS (new_specs)
13462 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13464 else
13466 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13467 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13470 else
13471 new_specs = tsubst_copy_and_build
13472 (expr, args, complain, in_decl, /*function_p=*/false,
13473 /*integral_constant_expression_p=*/true);
13474 new_specs = build_noexcept_spec (new_specs, complain);
13476 else if (specs)
13478 if (! TREE_VALUE (specs))
13479 new_specs = specs;
13480 else
13481 while (specs)
13483 tree spec;
13484 int i, len = 1;
13485 tree expanded_specs = NULL_TREE;
13487 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13489 /* Expand the pack expansion type. */
13490 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13491 args, complain,
13492 in_decl);
13494 if (expanded_specs == error_mark_node)
13495 return error_mark_node;
13496 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13497 len = TREE_VEC_LENGTH (expanded_specs);
13498 else
13500 /* We're substituting into a member template, so
13501 we got a TYPE_PACK_EXPANSION back. Add that
13502 expansion and move on. */
13503 gcc_assert (TREE_CODE (expanded_specs)
13504 == TYPE_PACK_EXPANSION);
13505 new_specs = add_exception_specifier (new_specs,
13506 expanded_specs,
13507 complain);
13508 specs = TREE_CHAIN (specs);
13509 continue;
13513 for (i = 0; i < len; ++i)
13515 if (expanded_specs)
13516 spec = TREE_VEC_ELT (expanded_specs, i);
13517 else
13518 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13519 if (spec == error_mark_node)
13520 return spec;
13521 new_specs = add_exception_specifier (new_specs, spec,
13522 complain);
13525 specs = TREE_CHAIN (specs);
13528 return new_specs;
13531 /* Take the tree structure T and replace template parameters used
13532 therein with the argument vector ARGS. IN_DECL is an associated
13533 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13534 Issue error and warning messages under control of COMPLAIN. Note
13535 that we must be relatively non-tolerant of extensions here, in
13536 order to preserve conformance; if we allow substitutions that
13537 should not be allowed, we may allow argument deductions that should
13538 not succeed, and therefore report ambiguous overload situations
13539 where there are none. In theory, we could allow the substitution,
13540 but indicate that it should have failed, and allow our caller to
13541 make sure that the right thing happens, but we don't try to do this
13542 yet.
13544 This function is used for dealing with types, decls and the like;
13545 for expressions, use tsubst_expr or tsubst_copy. */
13547 tree
13548 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13550 enum tree_code code;
13551 tree type, r = NULL_TREE;
13553 if (t == NULL_TREE || t == error_mark_node
13554 || t == integer_type_node
13555 || t == void_type_node
13556 || t == char_type_node
13557 || t == unknown_type_node
13558 || TREE_CODE (t) == NAMESPACE_DECL
13559 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13560 return t;
13562 if (DECL_P (t))
13563 return tsubst_decl (t, args, complain);
13565 if (args == NULL_TREE)
13566 return t;
13568 code = TREE_CODE (t);
13570 if (code == IDENTIFIER_NODE)
13571 type = IDENTIFIER_TYPE_VALUE (t);
13572 else
13573 type = TREE_TYPE (t);
13575 gcc_assert (type != unknown_type_node);
13577 /* Reuse typedefs. We need to do this to handle dependent attributes,
13578 such as attribute aligned. */
13579 if (TYPE_P (t)
13580 && typedef_variant_p (t))
13582 tree decl = TYPE_NAME (t);
13584 if (alias_template_specialization_p (t))
13586 /* DECL represents an alias template and we want to
13587 instantiate it. */
13588 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13589 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13590 r = instantiate_alias_template (tmpl, gen_args, complain);
13592 else if (DECL_CLASS_SCOPE_P (decl)
13593 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13594 && uses_template_parms (DECL_CONTEXT (decl)))
13596 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13597 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13598 r = retrieve_specialization (tmpl, gen_args, 0);
13600 else if (DECL_FUNCTION_SCOPE_P (decl)
13601 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13602 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13603 r = retrieve_local_specialization (decl);
13604 else
13605 /* The typedef is from a non-template context. */
13606 return t;
13608 if (r)
13610 r = TREE_TYPE (r);
13611 r = cp_build_qualified_type_real
13612 (r, cp_type_quals (t) | cp_type_quals (r),
13613 complain | tf_ignore_bad_quals);
13614 return r;
13616 else
13618 /* We don't have an instantiation yet, so drop the typedef. */
13619 int quals = cp_type_quals (t);
13620 t = DECL_ORIGINAL_TYPE (decl);
13621 t = cp_build_qualified_type_real (t, quals,
13622 complain | tf_ignore_bad_quals);
13626 bool fndecl_type = (complain & tf_fndecl_type);
13627 complain &= ~tf_fndecl_type;
13629 if (type
13630 && code != TYPENAME_TYPE
13631 && code != TEMPLATE_TYPE_PARM
13632 && code != TEMPLATE_PARM_INDEX
13633 && code != IDENTIFIER_NODE
13634 && code != FUNCTION_TYPE
13635 && code != METHOD_TYPE)
13636 type = tsubst (type, args, complain, in_decl);
13637 if (type == error_mark_node)
13638 return error_mark_node;
13640 switch (code)
13642 case RECORD_TYPE:
13643 case UNION_TYPE:
13644 case ENUMERAL_TYPE:
13645 return tsubst_aggr_type (t, args, complain, in_decl,
13646 /*entering_scope=*/0);
13648 case ERROR_MARK:
13649 case IDENTIFIER_NODE:
13650 case VOID_TYPE:
13651 case REAL_TYPE:
13652 case COMPLEX_TYPE:
13653 case VECTOR_TYPE:
13654 case BOOLEAN_TYPE:
13655 case NULLPTR_TYPE:
13656 case LANG_TYPE:
13657 return t;
13659 case INTEGER_TYPE:
13660 if (t == integer_type_node)
13661 return t;
13663 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13664 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13665 return t;
13668 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13670 max = tsubst_expr (omax, args, complain, in_decl,
13671 /*integral_constant_expression_p=*/false);
13673 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13674 needed. */
13675 if (TREE_CODE (max) == NOP_EXPR
13676 && TREE_SIDE_EFFECTS (omax)
13677 && !TREE_TYPE (max))
13678 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13680 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13681 with TREE_SIDE_EFFECTS that indicates this is not an integral
13682 constant expression. */
13683 if (processing_template_decl
13684 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13686 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13687 TREE_SIDE_EFFECTS (max) = 1;
13690 return compute_array_index_type (NULL_TREE, max, complain);
13693 case TEMPLATE_TYPE_PARM:
13694 case TEMPLATE_TEMPLATE_PARM:
13695 case BOUND_TEMPLATE_TEMPLATE_PARM:
13696 case TEMPLATE_PARM_INDEX:
13698 int idx;
13699 int level;
13700 int levels;
13701 tree arg = NULL_TREE;
13703 /* Early in template argument deduction substitution, we don't
13704 want to reduce the level of 'auto', or it will be confused
13705 with a normal template parm in subsequent deduction. */
13706 if (is_auto (t) && (complain & tf_partial))
13707 return t;
13709 r = NULL_TREE;
13711 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13712 template_parm_level_and_index (t, &level, &idx);
13714 levels = TMPL_ARGS_DEPTH (args);
13715 if (level <= levels
13716 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13718 arg = TMPL_ARG (args, level, idx);
13720 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13722 /* See through ARGUMENT_PACK_SELECT arguments. */
13723 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13724 /* If the selected argument is an expansion E, that most
13725 likely means we were called from
13726 gen_elem_of_pack_expansion_instantiation during the
13727 substituting of pack an argument pack (which Ith
13728 element is a pack expansion, where I is
13729 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13730 In this case, the Ith element resulting from this
13731 substituting is going to be a pack expansion, which
13732 pattern is the pattern of E. Let's return the
13733 pattern of E, and
13734 gen_elem_of_pack_expansion_instantiation will
13735 build the resulting pack expansion from it. */
13736 if (PACK_EXPANSION_P (arg))
13738 /* Make sure we aren't throwing away arg info. */
13739 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13740 arg = PACK_EXPANSION_PATTERN (arg);
13745 if (arg == error_mark_node)
13746 return error_mark_node;
13747 else if (arg != NULL_TREE)
13749 if (ARGUMENT_PACK_P (arg))
13750 /* If ARG is an argument pack, we don't actually want to
13751 perform a substitution here, because substitutions
13752 for argument packs are only done
13753 element-by-element. We can get to this point when
13754 substituting the type of a non-type template
13755 parameter pack, when that type actually contains
13756 template parameter packs from an outer template, e.g.,
13758 template<typename... Types> struct A {
13759 template<Types... Values> struct B { };
13760 }; */
13761 return t;
13763 if (code == TEMPLATE_TYPE_PARM)
13765 int quals;
13766 gcc_assert (TYPE_P (arg));
13768 quals = cp_type_quals (arg) | cp_type_quals (t);
13770 return cp_build_qualified_type_real
13771 (arg, quals, complain | tf_ignore_bad_quals);
13773 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13775 /* We are processing a type constructed from a
13776 template template parameter. */
13777 tree argvec = tsubst (TYPE_TI_ARGS (t),
13778 args, complain, in_decl);
13779 if (argvec == error_mark_node)
13780 return error_mark_node;
13782 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13783 || TREE_CODE (arg) == TEMPLATE_DECL
13784 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13786 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13787 /* Consider this code:
13789 template <template <class> class Template>
13790 struct Internal {
13791 template <class Arg> using Bind = Template<Arg>;
13794 template <template <class> class Template, class Arg>
13795 using Instantiate = Template<Arg>; //#0
13797 template <template <class> class Template,
13798 class Argument>
13799 using Bind =
13800 Instantiate<Internal<Template>::template Bind,
13801 Argument>; //#1
13803 When #1 is parsed, the
13804 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13805 parameter `Template' in #0 matches the
13806 UNBOUND_CLASS_TEMPLATE representing the argument
13807 `Internal<Template>::template Bind'; We then want
13808 to assemble the type `Bind<Argument>' that can't
13809 be fully created right now, because
13810 `Internal<Template>' not being complete, the Bind
13811 template cannot be looked up in that context. So
13812 we need to "store" `Bind<Argument>' for later
13813 when the context of Bind becomes complete. Let's
13814 store that in a TYPENAME_TYPE. */
13815 return make_typename_type (TYPE_CONTEXT (arg),
13816 build_nt (TEMPLATE_ID_EXPR,
13817 TYPE_IDENTIFIER (arg),
13818 argvec),
13819 typename_type,
13820 complain);
13822 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13823 are resolving nested-types in the signature of a
13824 member function templates. Otherwise ARG is a
13825 TEMPLATE_DECL and is the real template to be
13826 instantiated. */
13827 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13828 arg = TYPE_NAME (arg);
13830 r = lookup_template_class (arg,
13831 argvec, in_decl,
13832 DECL_CONTEXT (arg),
13833 /*entering_scope=*/0,
13834 complain);
13835 return cp_build_qualified_type_real
13836 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13838 else if (code == TEMPLATE_TEMPLATE_PARM)
13839 return arg;
13840 else
13841 /* TEMPLATE_PARM_INDEX. */
13842 return convert_from_reference (unshare_expr (arg));
13845 if (level == 1)
13846 /* This can happen during the attempted tsubst'ing in
13847 unify. This means that we don't yet have any information
13848 about the template parameter in question. */
13849 return t;
13851 /* If we get here, we must have been looking at a parm for a
13852 more deeply nested template. Make a new version of this
13853 template parameter, but with a lower level. */
13854 switch (code)
13856 case TEMPLATE_TYPE_PARM:
13857 case TEMPLATE_TEMPLATE_PARM:
13858 case BOUND_TEMPLATE_TEMPLATE_PARM:
13859 if (cp_type_quals (t))
13861 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13862 r = cp_build_qualified_type_real
13863 (r, cp_type_quals (t),
13864 complain | (code == TEMPLATE_TYPE_PARM
13865 ? tf_ignore_bad_quals : 0));
13867 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13868 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13869 && (r = (TEMPLATE_PARM_DESCENDANTS
13870 (TEMPLATE_TYPE_PARM_INDEX (t))))
13871 && (r = TREE_TYPE (r))
13872 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13873 /* Break infinite recursion when substituting the constraints
13874 of a constrained placeholder. */;
13875 else
13877 r = copy_type (t);
13878 TEMPLATE_TYPE_PARM_INDEX (r)
13879 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13880 r, levels, args, complain);
13881 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13882 TYPE_MAIN_VARIANT (r) = r;
13883 TYPE_POINTER_TO (r) = NULL_TREE;
13884 TYPE_REFERENCE_TO (r) = NULL_TREE;
13886 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13888 /* Propagate constraints on placeholders. */
13889 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13890 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13891 = tsubst_constraint (constr, args, complain, in_decl);
13892 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13894 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13895 pl = tsubst (pl, args, complain, in_decl);
13896 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13900 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13901 /* We have reduced the level of the template
13902 template parameter, but not the levels of its
13903 template parameters, so canonical_type_parameter
13904 will not be able to find the canonical template
13905 template parameter for this level. Thus, we
13906 require structural equality checking to compare
13907 TEMPLATE_TEMPLATE_PARMs. */
13908 SET_TYPE_STRUCTURAL_EQUALITY (r);
13909 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13910 SET_TYPE_STRUCTURAL_EQUALITY (r);
13911 else
13912 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13914 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13916 tree tinfo = TYPE_TEMPLATE_INFO (t);
13917 /* We might need to substitute into the types of non-type
13918 template parameters. */
13919 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13920 complain, in_decl);
13921 if (tmpl == error_mark_node)
13922 return error_mark_node;
13923 tree argvec = tsubst (TI_ARGS (tinfo), args,
13924 complain, in_decl);
13925 if (argvec == error_mark_node)
13926 return error_mark_node;
13928 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13929 = build_template_info (tmpl, argvec);
13932 break;
13934 case TEMPLATE_PARM_INDEX:
13935 /* OK, now substitute the type of the non-type parameter. We
13936 couldn't do it earlier because it might be an auto parameter,
13937 and we wouldn't need to if we had an argument. */
13938 type = tsubst (type, args, complain, in_decl);
13939 if (type == error_mark_node)
13940 return error_mark_node;
13941 r = reduce_template_parm_level (t, type, levels, args, complain);
13942 break;
13944 default:
13945 gcc_unreachable ();
13948 return r;
13951 case TREE_LIST:
13953 tree purpose, value, chain;
13955 if (t == void_list_node)
13956 return t;
13958 purpose = TREE_PURPOSE (t);
13959 if (purpose)
13961 purpose = tsubst (purpose, args, complain, in_decl);
13962 if (purpose == error_mark_node)
13963 return error_mark_node;
13965 value = TREE_VALUE (t);
13966 if (value)
13968 value = tsubst (value, args, complain, in_decl);
13969 if (value == error_mark_node)
13970 return error_mark_node;
13972 chain = TREE_CHAIN (t);
13973 if (chain && chain != void_type_node)
13975 chain = tsubst (chain, args, complain, in_decl);
13976 if (chain == error_mark_node)
13977 return error_mark_node;
13979 if (purpose == TREE_PURPOSE (t)
13980 && value == TREE_VALUE (t)
13981 && chain == TREE_CHAIN (t))
13982 return t;
13983 return hash_tree_cons (purpose, value, chain);
13986 case TREE_BINFO:
13987 /* We should never be tsubsting a binfo. */
13988 gcc_unreachable ();
13990 case TREE_VEC:
13991 /* A vector of template arguments. */
13992 gcc_assert (!type);
13993 return tsubst_template_args (t, args, complain, in_decl);
13995 case POINTER_TYPE:
13996 case REFERENCE_TYPE:
13998 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13999 return t;
14001 /* [temp.deduct]
14003 Type deduction may fail for any of the following
14004 reasons:
14006 -- Attempting to create a pointer to reference type.
14007 -- Attempting to create a reference to a reference type or
14008 a reference to void.
14010 Core issue 106 says that creating a reference to a reference
14011 during instantiation is no longer a cause for failure. We
14012 only enforce this check in strict C++98 mode. */
14013 if ((TREE_CODE (type) == REFERENCE_TYPE
14014 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14015 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14017 static location_t last_loc;
14019 /* We keep track of the last time we issued this error
14020 message to avoid spewing a ton of messages during a
14021 single bad template instantiation. */
14022 if (complain & tf_error
14023 && last_loc != input_location)
14025 if (VOID_TYPE_P (type))
14026 error ("forming reference to void");
14027 else if (code == POINTER_TYPE)
14028 error ("forming pointer to reference type %qT", type);
14029 else
14030 error ("forming reference to reference type %qT", type);
14031 last_loc = input_location;
14034 return error_mark_node;
14036 else if (TREE_CODE (type) == FUNCTION_TYPE
14037 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14038 || type_memfn_rqual (type) != REF_QUAL_NONE))
14040 if (complain & tf_error)
14042 if (code == POINTER_TYPE)
14043 error ("forming pointer to qualified function type %qT",
14044 type);
14045 else
14046 error ("forming reference to qualified function type %qT",
14047 type);
14049 return error_mark_node;
14051 else if (code == POINTER_TYPE)
14053 r = build_pointer_type (type);
14054 if (TREE_CODE (type) == METHOD_TYPE)
14055 r = build_ptrmemfunc_type (r);
14057 else if (TREE_CODE (type) == REFERENCE_TYPE)
14058 /* In C++0x, during template argument substitution, when there is an
14059 attempt to create a reference to a reference type, reference
14060 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14062 "If a template-argument for a template-parameter T names a type
14063 that is a reference to a type A, an attempt to create the type
14064 'lvalue reference to cv T' creates the type 'lvalue reference to
14065 A,' while an attempt to create the type type rvalue reference to
14066 cv T' creates the type T"
14068 r = cp_build_reference_type
14069 (TREE_TYPE (type),
14070 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14071 else
14072 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14073 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14075 if (r != error_mark_node)
14076 /* Will this ever be needed for TYPE_..._TO values? */
14077 layout_type (r);
14079 return r;
14081 case OFFSET_TYPE:
14083 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14084 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14086 /* [temp.deduct]
14088 Type deduction may fail for any of the following
14089 reasons:
14091 -- Attempting to create "pointer to member of T" when T
14092 is not a class type. */
14093 if (complain & tf_error)
14094 error ("creating pointer to member of non-class type %qT", r);
14095 return error_mark_node;
14097 if (TREE_CODE (type) == REFERENCE_TYPE)
14099 if (complain & tf_error)
14100 error ("creating pointer to member reference type %qT", type);
14101 return error_mark_node;
14103 if (VOID_TYPE_P (type))
14105 if (complain & tf_error)
14106 error ("creating pointer to member of type void");
14107 return error_mark_node;
14109 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14110 if (TREE_CODE (type) == FUNCTION_TYPE)
14112 /* The type of the implicit object parameter gets its
14113 cv-qualifiers from the FUNCTION_TYPE. */
14114 tree memptr;
14115 tree method_type
14116 = build_memfn_type (type, r, type_memfn_quals (type),
14117 type_memfn_rqual (type));
14118 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14119 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14120 complain);
14122 else
14123 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14124 cp_type_quals (t),
14125 complain);
14127 case FUNCTION_TYPE:
14128 case METHOD_TYPE:
14130 tree fntype;
14131 tree specs;
14132 fntype = tsubst_function_type (t, args, complain, in_decl);
14133 if (fntype == error_mark_node)
14134 return error_mark_node;
14136 /* Substitute the exception specification. */
14137 specs = tsubst_exception_specification (t, args, complain, in_decl,
14138 /*defer_ok*/fndecl_type);
14139 if (specs == error_mark_node)
14140 return error_mark_node;
14141 if (specs)
14142 fntype = build_exception_variant (fntype, specs);
14143 return fntype;
14145 case ARRAY_TYPE:
14147 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14148 if (domain == error_mark_node)
14149 return error_mark_node;
14151 /* As an optimization, we avoid regenerating the array type if
14152 it will obviously be the same as T. */
14153 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14154 return t;
14156 /* These checks should match the ones in create_array_type_for_decl.
14158 [temp.deduct]
14160 The deduction may fail for any of the following reasons:
14162 -- Attempting to create an array with an element type that
14163 is void, a function type, or a reference type, or [DR337]
14164 an abstract class type. */
14165 if (VOID_TYPE_P (type)
14166 || TREE_CODE (type) == FUNCTION_TYPE
14167 || (TREE_CODE (type) == ARRAY_TYPE
14168 && TYPE_DOMAIN (type) == NULL_TREE)
14169 || TREE_CODE (type) == REFERENCE_TYPE)
14171 if (complain & tf_error)
14172 error ("creating array of %qT", type);
14173 return error_mark_node;
14176 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14177 return error_mark_node;
14179 r = build_cplus_array_type (type, domain);
14181 if (TYPE_USER_ALIGN (t))
14183 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14184 TYPE_USER_ALIGN (r) = 1;
14187 return r;
14190 case TYPENAME_TYPE:
14192 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14193 in_decl, /*entering_scope=*/1);
14194 if (ctx == error_mark_node)
14195 return error_mark_node;
14197 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14198 complain, in_decl);
14199 if (f == error_mark_node)
14200 return error_mark_node;
14202 if (!MAYBE_CLASS_TYPE_P (ctx))
14204 if (complain & tf_error)
14205 error ("%qT is not a class, struct, or union type", ctx);
14206 return error_mark_node;
14208 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14210 /* Normally, make_typename_type does not require that the CTX
14211 have complete type in order to allow things like:
14213 template <class T> struct S { typename S<T>::X Y; };
14215 But, such constructs have already been resolved by this
14216 point, so here CTX really should have complete type, unless
14217 it's a partial instantiation. */
14218 ctx = complete_type (ctx);
14219 if (!COMPLETE_TYPE_P (ctx))
14221 if (complain & tf_error)
14222 cxx_incomplete_type_error (NULL_TREE, ctx);
14223 return error_mark_node;
14227 f = make_typename_type (ctx, f, typename_type,
14228 complain | tf_keep_type_decl);
14229 if (f == error_mark_node)
14230 return f;
14231 if (TREE_CODE (f) == TYPE_DECL)
14233 complain |= tf_ignore_bad_quals;
14234 f = TREE_TYPE (f);
14237 if (TREE_CODE (f) != TYPENAME_TYPE)
14239 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14241 if (complain & tf_error)
14242 error ("%qT resolves to %qT, which is not an enumeration type",
14243 t, f);
14244 else
14245 return error_mark_node;
14247 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14249 if (complain & tf_error)
14250 error ("%qT resolves to %qT, which is is not a class type",
14251 t, f);
14252 else
14253 return error_mark_node;
14257 return cp_build_qualified_type_real
14258 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14261 case UNBOUND_CLASS_TEMPLATE:
14263 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14264 in_decl, /*entering_scope=*/1);
14265 tree name = TYPE_IDENTIFIER (t);
14266 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14268 if (ctx == error_mark_node || name == error_mark_node)
14269 return error_mark_node;
14271 if (parm_list)
14272 parm_list = tsubst_template_parms (parm_list, args, complain);
14273 return make_unbound_class_template (ctx, name, parm_list, complain);
14276 case TYPEOF_TYPE:
14278 tree type;
14280 ++cp_unevaluated_operand;
14281 ++c_inhibit_evaluation_warnings;
14283 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14284 complain, in_decl,
14285 /*integral_constant_expression_p=*/false);
14287 --cp_unevaluated_operand;
14288 --c_inhibit_evaluation_warnings;
14290 type = finish_typeof (type);
14291 return cp_build_qualified_type_real (type,
14292 cp_type_quals (t)
14293 | cp_type_quals (type),
14294 complain);
14297 case DECLTYPE_TYPE:
14299 tree type;
14301 ++cp_unevaluated_operand;
14302 ++c_inhibit_evaluation_warnings;
14304 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14305 complain|tf_decltype, in_decl,
14306 /*function_p*/false,
14307 /*integral_constant_expression*/false);
14309 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14311 if (type == NULL_TREE)
14313 if (complain & tf_error)
14314 error ("empty initializer in lambda init-capture");
14315 type = error_mark_node;
14317 else if (TREE_CODE (type) == TREE_LIST)
14318 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14321 --cp_unevaluated_operand;
14322 --c_inhibit_evaluation_warnings;
14324 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14325 type = lambda_capture_field_type (type,
14326 DECLTYPE_FOR_INIT_CAPTURE (t),
14327 DECLTYPE_FOR_REF_CAPTURE (t));
14328 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14329 type = lambda_proxy_type (type);
14330 else
14332 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14333 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14334 && EXPR_P (type))
14335 /* In a template ~id could be either a complement expression
14336 or an unqualified-id naming a destructor; if instantiating
14337 it produces an expression, it's not an id-expression or
14338 member access. */
14339 id = false;
14340 type = finish_decltype_type (type, id, complain);
14342 return cp_build_qualified_type_real (type,
14343 cp_type_quals (t)
14344 | cp_type_quals (type),
14345 complain | tf_ignore_bad_quals);
14348 case UNDERLYING_TYPE:
14350 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14351 complain, in_decl);
14352 return finish_underlying_type (type);
14355 case TYPE_ARGUMENT_PACK:
14356 case NONTYPE_ARGUMENT_PACK:
14358 tree r;
14360 if (code == NONTYPE_ARGUMENT_PACK)
14361 r = make_node (code);
14362 else
14363 r = cxx_make_type (code);
14365 tree pack_args = ARGUMENT_PACK_ARGS (t);
14366 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14367 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14369 return r;
14372 case VOID_CST:
14373 case INTEGER_CST:
14374 case REAL_CST:
14375 case STRING_CST:
14376 case PLUS_EXPR:
14377 case MINUS_EXPR:
14378 case NEGATE_EXPR:
14379 case NOP_EXPR:
14380 case INDIRECT_REF:
14381 case ADDR_EXPR:
14382 case CALL_EXPR:
14383 case ARRAY_REF:
14384 case SCOPE_REF:
14385 /* We should use one of the expression tsubsts for these codes. */
14386 gcc_unreachable ();
14388 default:
14389 sorry ("use of %qs in template", get_tree_code_name (code));
14390 return error_mark_node;
14394 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14395 expression on the left-hand side of the "." or "->" operator. A
14396 baselink indicates a function from a base class. Both the
14397 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14398 bases of the template class, rather than the instantiated class.
14399 In addition, lookups that were not ambiguous before may be
14400 ambiguous now. Therefore, we perform the lookup again. */
14402 static tree
14403 tsubst_baselink (tree baselink, tree object_type,
14404 tree args, tsubst_flags_t complain, tree in_decl)
14406 bool qualified = BASELINK_QUALIFIED_P (baselink);
14408 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14409 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14411 tree optype = BASELINK_OPTYPE (baselink);
14412 optype = tsubst (optype, args, complain, in_decl);
14414 tree template_args = NULL_TREE;
14415 bool template_id_p = false;
14416 tree fns = BASELINK_FUNCTIONS (baselink);
14417 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14419 template_id_p = true;
14420 template_args = TREE_OPERAND (fns, 1);
14421 fns = TREE_OPERAND (fns, 0);
14422 if (template_args)
14423 template_args = tsubst_template_args (template_args, args,
14424 complain, in_decl);
14427 tree name = OVL_NAME (fns);
14428 if (IDENTIFIER_CONV_OP_P (name))
14429 name = make_conv_op_name (optype);
14431 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14432 if (!baselink)
14434 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14435 error ("cannot call constructor %<%T::%D%> directly",
14436 qualifying_scope, name);
14437 return error_mark_node;
14440 /* If lookup found a single function, mark it as used at this point.
14441 (If it lookup found multiple functions the one selected later by
14442 overload resolution will be marked as used at that point.) */
14443 if (BASELINK_P (baselink))
14444 fns = BASELINK_FUNCTIONS (baselink);
14445 if (!template_id_p && !really_overloaded_fn (fns)
14446 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14447 return error_mark_node;
14449 if (BASELINK_P (baselink))
14451 /* Add back the template arguments, if present. */
14452 if (template_id_p)
14453 BASELINK_FUNCTIONS (baselink)
14454 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14455 BASELINK_FUNCTIONS (baselink), template_args);
14457 /* Update the conversion operator type. */
14458 BASELINK_OPTYPE (baselink) = optype;
14461 if (!object_type)
14462 object_type = current_class_type;
14464 if (qualified || name == complete_dtor_identifier)
14466 baselink = adjust_result_of_qualified_name_lookup (baselink,
14467 qualifying_scope,
14468 object_type);
14469 if (!qualified)
14470 /* We need to call adjust_result_of_qualified_name_lookup in case the
14471 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14472 so that we still get virtual function binding. */
14473 BASELINK_QUALIFIED_P (baselink) = false;
14476 return baselink;
14479 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14480 true if the qualified-id will be a postfix-expression in-and-of
14481 itself; false if more of the postfix-expression follows the
14482 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14483 of "&". */
14485 static tree
14486 tsubst_qualified_id (tree qualified_id, tree args,
14487 tsubst_flags_t complain, tree in_decl,
14488 bool done, bool address_p)
14490 tree expr;
14491 tree scope;
14492 tree name;
14493 bool is_template;
14494 tree template_args;
14495 location_t loc = UNKNOWN_LOCATION;
14497 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14499 /* Figure out what name to look up. */
14500 name = TREE_OPERAND (qualified_id, 1);
14501 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14503 is_template = true;
14504 loc = EXPR_LOCATION (name);
14505 template_args = TREE_OPERAND (name, 1);
14506 if (template_args)
14507 template_args = tsubst_template_args (template_args, args,
14508 complain, in_decl);
14509 if (template_args == error_mark_node)
14510 return error_mark_node;
14511 name = TREE_OPERAND (name, 0);
14513 else
14515 is_template = false;
14516 template_args = NULL_TREE;
14519 /* Substitute into the qualifying scope. When there are no ARGS, we
14520 are just trying to simplify a non-dependent expression. In that
14521 case the qualifying scope may be dependent, and, in any case,
14522 substituting will not help. */
14523 scope = TREE_OPERAND (qualified_id, 0);
14524 if (args)
14526 scope = tsubst (scope, args, complain, in_decl);
14527 expr = tsubst_copy (name, args, complain, in_decl);
14529 else
14530 expr = name;
14532 if (dependent_scope_p (scope))
14534 if (is_template)
14535 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14536 tree r = build_qualified_name (NULL_TREE, scope, expr,
14537 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14538 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14539 return r;
14542 if (!BASELINK_P (name) && !DECL_P (expr))
14544 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14546 /* A BIT_NOT_EXPR is used to represent a destructor. */
14547 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14549 error ("qualifying type %qT does not match destructor name ~%qT",
14550 scope, TREE_OPERAND (expr, 0));
14551 expr = error_mark_node;
14553 else
14554 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14555 /*is_type_p=*/0, false);
14557 else
14558 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14559 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14560 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14562 if (complain & tf_error)
14564 error ("dependent-name %qE is parsed as a non-type, but "
14565 "instantiation yields a type", qualified_id);
14566 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14568 return error_mark_node;
14572 if (DECL_P (expr))
14574 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14575 scope);
14576 /* Remember that there was a reference to this entity. */
14577 if (!mark_used (expr, complain) && !(complain & tf_error))
14578 return error_mark_node;
14581 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14583 if (complain & tf_error)
14584 qualified_name_lookup_error (scope,
14585 TREE_OPERAND (qualified_id, 1),
14586 expr, input_location);
14587 return error_mark_node;
14590 if (is_template)
14592 if (variable_template_p (expr))
14593 expr = lookup_and_finish_template_variable (expr, template_args,
14594 complain);
14595 else
14596 expr = lookup_template_function (expr, template_args);
14599 if (expr == error_mark_node && complain & tf_error)
14600 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14601 expr, input_location);
14602 else if (TYPE_P (scope))
14604 expr = (adjust_result_of_qualified_name_lookup
14605 (expr, scope, current_nonlambda_class_type ()));
14606 expr = (finish_qualified_id_expr
14607 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14608 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14609 /*template_arg_p=*/false, complain));
14612 /* Expressions do not generally have reference type. */
14613 if (TREE_CODE (expr) != SCOPE_REF
14614 /* However, if we're about to form a pointer-to-member, we just
14615 want the referenced member referenced. */
14616 && TREE_CODE (expr) != OFFSET_REF)
14617 expr = convert_from_reference (expr);
14619 if (REF_PARENTHESIZED_P (qualified_id))
14620 expr = force_paren_expr (expr);
14622 return expr;
14625 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14626 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14627 for tsubst. */
14629 static tree
14630 tsubst_init (tree init, tree decl, tree args,
14631 tsubst_flags_t complain, tree in_decl)
14633 if (!init)
14634 return NULL_TREE;
14636 init = tsubst_expr (init, args, complain, in_decl, false);
14638 if (!init && TREE_TYPE (decl) != error_mark_node)
14640 /* If we had an initializer but it
14641 instantiated to nothing,
14642 value-initialize the object. This will
14643 only occur when the initializer was a
14644 pack expansion where the parameter packs
14645 used in that expansion were of length
14646 zero. */
14647 init = build_value_init (TREE_TYPE (decl),
14648 complain);
14649 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14650 init = get_target_expr_sfinae (init, complain);
14651 if (TREE_CODE (init) == TARGET_EXPR)
14652 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14655 return init;
14658 /* Like tsubst, but deals with expressions. This function just replaces
14659 template parms; to finish processing the resultant expression, use
14660 tsubst_copy_and_build or tsubst_expr. */
14662 static tree
14663 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14665 enum tree_code code;
14666 tree r;
14668 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14669 return t;
14671 code = TREE_CODE (t);
14673 switch (code)
14675 case PARM_DECL:
14676 r = retrieve_local_specialization (t);
14678 if (r == NULL_TREE)
14680 /* We get here for a use of 'this' in an NSDMI. */
14681 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14682 return current_class_ptr;
14684 /* This can happen for a parameter name used later in a function
14685 declaration (such as in a late-specified return type). Just
14686 make a dummy decl, since it's only used for its type. */
14687 gcc_assert (cp_unevaluated_operand != 0);
14688 r = tsubst_decl (t, args, complain);
14689 /* Give it the template pattern as its context; its true context
14690 hasn't been instantiated yet and this is good enough for
14691 mangling. */
14692 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14695 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14696 r = ARGUMENT_PACK_SELECT_ARG (r);
14697 if (!mark_used (r, complain) && !(complain & tf_error))
14698 return error_mark_node;
14699 return r;
14701 case CONST_DECL:
14703 tree enum_type;
14704 tree v;
14706 if (DECL_TEMPLATE_PARM_P (t))
14707 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14708 /* There is no need to substitute into namespace-scope
14709 enumerators. */
14710 if (DECL_NAMESPACE_SCOPE_P (t))
14711 return t;
14712 /* If ARGS is NULL, then T is known to be non-dependent. */
14713 if (args == NULL_TREE)
14714 return scalar_constant_value (t);
14716 /* Unfortunately, we cannot just call lookup_name here.
14717 Consider:
14719 template <int I> int f() {
14720 enum E { a = I };
14721 struct S { void g() { E e = a; } };
14724 When we instantiate f<7>::S::g(), say, lookup_name is not
14725 clever enough to find f<7>::a. */
14726 enum_type
14727 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14728 /*entering_scope=*/0);
14730 for (v = TYPE_VALUES (enum_type);
14731 v != NULL_TREE;
14732 v = TREE_CHAIN (v))
14733 if (TREE_PURPOSE (v) == DECL_NAME (t))
14734 return TREE_VALUE (v);
14736 /* We didn't find the name. That should never happen; if
14737 name-lookup found it during preliminary parsing, we
14738 should find it again here during instantiation. */
14739 gcc_unreachable ();
14741 return t;
14743 case FIELD_DECL:
14744 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14746 /* Check for a local specialization set up by
14747 tsubst_pack_expansion. */
14748 if (tree r = retrieve_local_specialization (t))
14750 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14751 r = ARGUMENT_PACK_SELECT_ARG (r);
14752 return r;
14755 /* When retrieving a capture pack from a generic lambda, remove the
14756 lambda call op's own template argument list from ARGS. Only the
14757 template arguments active for the closure type should be used to
14758 retrieve the pack specialization. */
14759 if (LAMBDA_FUNCTION_P (current_function_decl)
14760 && (template_class_depth (DECL_CONTEXT (t))
14761 != TMPL_ARGS_DEPTH (args)))
14762 args = strip_innermost_template_args (args, 1);
14764 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14765 tsubst_decl put in the hash table. */
14766 return retrieve_specialization (t, args, 0);
14769 if (DECL_CONTEXT (t))
14771 tree ctx;
14773 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14774 /*entering_scope=*/1);
14775 if (ctx != DECL_CONTEXT (t))
14777 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14778 if (!r)
14780 if (complain & tf_error)
14781 error ("using invalid field %qD", t);
14782 return error_mark_node;
14784 return r;
14788 return t;
14790 case VAR_DECL:
14791 case FUNCTION_DECL:
14792 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14793 r = tsubst (t, args, complain, in_decl);
14794 else if (local_variable_p (t)
14795 && uses_template_parms (DECL_CONTEXT (t)))
14797 r = retrieve_local_specialization (t);
14798 if (r == NULL_TREE)
14800 /* First try name lookup to find the instantiation. */
14801 r = lookup_name (DECL_NAME (t));
14802 if (r && !is_capture_proxy (r))
14804 /* Make sure that the one we found is the one we want. */
14805 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14806 if (ctx != DECL_CONTEXT (r))
14807 r = NULL_TREE;
14810 if (r)
14811 /* OK */;
14812 else
14814 /* This can happen for a variable used in a
14815 late-specified return type of a local lambda, or for a
14816 local static or constant. Building a new VAR_DECL
14817 should be OK in all those cases. */
14818 r = tsubst_decl (t, args, complain);
14819 if (local_specializations)
14820 /* Avoid infinite recursion (79640). */
14821 register_local_specialization (r, t);
14822 if (decl_maybe_constant_var_p (r))
14824 /* We can't call cp_finish_decl, so handle the
14825 initializer by hand. */
14826 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14827 complain, in_decl);
14828 if (!processing_template_decl)
14829 init = maybe_constant_init (init);
14830 if (processing_template_decl
14831 ? potential_constant_expression (init)
14832 : reduced_constant_expression_p (init))
14833 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14834 = TREE_CONSTANT (r) = true;
14835 DECL_INITIAL (r) = init;
14836 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14837 TREE_TYPE (r)
14838 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14839 complain, adc_variable_type);
14841 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14842 || decl_constant_var_p (r)
14843 || errorcount || sorrycount);
14844 if (!processing_template_decl
14845 && !TREE_STATIC (r))
14846 r = process_outer_var_ref (r, complain);
14848 /* Remember this for subsequent uses. */
14849 if (local_specializations)
14850 register_local_specialization (r, t);
14853 else
14854 r = t;
14855 if (!mark_used (r, complain))
14856 return error_mark_node;
14857 return r;
14859 case NAMESPACE_DECL:
14860 return t;
14862 case OVERLOAD:
14863 /* An OVERLOAD will always be a non-dependent overload set; an
14864 overload set from function scope will just be represented with an
14865 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14866 gcc_assert (!uses_template_parms (t));
14867 /* We must have marked any lookups as persistent. */
14868 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14869 return t;
14871 case BASELINK:
14872 return tsubst_baselink (t, current_nonlambda_class_type (),
14873 args, complain, in_decl);
14875 case TEMPLATE_DECL:
14876 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14877 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14878 args, complain, in_decl);
14879 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14880 return tsubst (t, args, complain, in_decl);
14881 else if (DECL_CLASS_SCOPE_P (t)
14882 && uses_template_parms (DECL_CONTEXT (t)))
14884 /* Template template argument like the following example need
14885 special treatment:
14887 template <template <class> class TT> struct C {};
14888 template <class T> struct D {
14889 template <class U> struct E {};
14890 C<E> c; // #1
14892 D<int> d; // #2
14894 We are processing the template argument `E' in #1 for
14895 the template instantiation #2. Originally, `E' is a
14896 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14897 have to substitute this with one having context `D<int>'. */
14899 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14900 if (dependent_scope_p (context))
14902 /* When rewriting a constructor into a deduction guide, a
14903 non-dependent name can become dependent, so memtmpl<args>
14904 becomes context::template memtmpl<args>. */
14905 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14906 return build_qualified_name (type, context, DECL_NAME (t),
14907 /*template*/true);
14909 return lookup_field (context, DECL_NAME(t), 0, false);
14911 else
14912 /* Ordinary template template argument. */
14913 return t;
14915 case CAST_EXPR:
14916 case REINTERPRET_CAST_EXPR:
14917 case CONST_CAST_EXPR:
14918 case STATIC_CAST_EXPR:
14919 case DYNAMIC_CAST_EXPR:
14920 case IMPLICIT_CONV_EXPR:
14921 case CONVERT_EXPR:
14922 case NOP_EXPR:
14924 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14925 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14926 return build1 (code, type, op0);
14929 case SIZEOF_EXPR:
14930 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14931 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14933 tree expanded, op = TREE_OPERAND (t, 0);
14934 int len = 0;
14936 if (SIZEOF_EXPR_TYPE_P (t))
14937 op = TREE_TYPE (op);
14939 ++cp_unevaluated_operand;
14940 ++c_inhibit_evaluation_warnings;
14941 /* We only want to compute the number of arguments. */
14942 if (PACK_EXPANSION_P (op))
14943 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14944 else
14945 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14946 args, complain, in_decl);
14947 --cp_unevaluated_operand;
14948 --c_inhibit_evaluation_warnings;
14950 if (TREE_CODE (expanded) == TREE_VEC)
14952 len = TREE_VEC_LENGTH (expanded);
14953 /* Set TREE_USED for the benefit of -Wunused. */
14954 for (int i = 0; i < len; i++)
14955 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14956 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14959 if (expanded == error_mark_node)
14960 return error_mark_node;
14961 else if (PACK_EXPANSION_P (expanded)
14962 || (TREE_CODE (expanded) == TREE_VEC
14963 && pack_expansion_args_count (expanded)))
14966 if (PACK_EXPANSION_P (expanded))
14967 /* OK. */;
14968 else if (TREE_VEC_LENGTH (expanded) == 1)
14969 expanded = TREE_VEC_ELT (expanded, 0);
14970 else
14971 expanded = make_argument_pack (expanded);
14973 if (TYPE_P (expanded))
14974 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14975 complain & tf_error);
14976 else
14977 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14978 complain & tf_error);
14980 else
14981 return build_int_cst (size_type_node, len);
14983 if (SIZEOF_EXPR_TYPE_P (t))
14985 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14986 args, complain, in_decl);
14987 r = build1 (NOP_EXPR, r, error_mark_node);
14988 r = build1 (SIZEOF_EXPR,
14989 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14990 SIZEOF_EXPR_TYPE_P (r) = 1;
14991 return r;
14993 /* Fall through */
14995 case INDIRECT_REF:
14996 case NEGATE_EXPR:
14997 case TRUTH_NOT_EXPR:
14998 case BIT_NOT_EXPR:
14999 case ADDR_EXPR:
15000 case UNARY_PLUS_EXPR: /* Unary + */
15001 case ALIGNOF_EXPR:
15002 case AT_ENCODE_EXPR:
15003 case ARROW_EXPR:
15004 case THROW_EXPR:
15005 case TYPEID_EXPR:
15006 case REALPART_EXPR:
15007 case IMAGPART_EXPR:
15008 case PAREN_EXPR:
15010 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15011 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15012 return build1 (code, type, op0);
15015 case COMPONENT_REF:
15017 tree object;
15018 tree name;
15020 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15021 name = TREE_OPERAND (t, 1);
15022 if (TREE_CODE (name) == BIT_NOT_EXPR)
15024 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15025 complain, in_decl);
15026 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15028 else if (TREE_CODE (name) == SCOPE_REF
15029 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15031 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15032 complain, in_decl);
15033 name = TREE_OPERAND (name, 1);
15034 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15035 complain, in_decl);
15036 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15037 name = build_qualified_name (/*type=*/NULL_TREE,
15038 base, name,
15039 /*template_p=*/false);
15041 else if (BASELINK_P (name))
15042 name = tsubst_baselink (name,
15043 non_reference (TREE_TYPE (object)),
15044 args, complain,
15045 in_decl);
15046 else
15047 name = tsubst_copy (name, args, complain, in_decl);
15048 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15051 case PLUS_EXPR:
15052 case MINUS_EXPR:
15053 case MULT_EXPR:
15054 case TRUNC_DIV_EXPR:
15055 case CEIL_DIV_EXPR:
15056 case FLOOR_DIV_EXPR:
15057 case ROUND_DIV_EXPR:
15058 case EXACT_DIV_EXPR:
15059 case BIT_AND_EXPR:
15060 case BIT_IOR_EXPR:
15061 case BIT_XOR_EXPR:
15062 case TRUNC_MOD_EXPR:
15063 case FLOOR_MOD_EXPR:
15064 case TRUTH_ANDIF_EXPR:
15065 case TRUTH_ORIF_EXPR:
15066 case TRUTH_AND_EXPR:
15067 case TRUTH_OR_EXPR:
15068 case RSHIFT_EXPR:
15069 case LSHIFT_EXPR:
15070 case RROTATE_EXPR:
15071 case LROTATE_EXPR:
15072 case EQ_EXPR:
15073 case NE_EXPR:
15074 case MAX_EXPR:
15075 case MIN_EXPR:
15076 case LE_EXPR:
15077 case GE_EXPR:
15078 case LT_EXPR:
15079 case GT_EXPR:
15080 case COMPOUND_EXPR:
15081 case DOTSTAR_EXPR:
15082 case MEMBER_REF:
15083 case PREDECREMENT_EXPR:
15084 case PREINCREMENT_EXPR:
15085 case POSTDECREMENT_EXPR:
15086 case POSTINCREMENT_EXPR:
15088 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15089 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15090 return build_nt (code, op0, op1);
15093 case SCOPE_REF:
15095 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15096 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15097 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15098 QUALIFIED_NAME_IS_TEMPLATE (t));
15101 case ARRAY_REF:
15103 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15104 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15105 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15108 case CALL_EXPR:
15110 int n = VL_EXP_OPERAND_LENGTH (t);
15111 tree result = build_vl_exp (CALL_EXPR, n);
15112 int i;
15113 for (i = 0; i < n; i++)
15114 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15115 complain, in_decl);
15116 return result;
15119 case COND_EXPR:
15120 case MODOP_EXPR:
15121 case PSEUDO_DTOR_EXPR:
15122 case VEC_PERM_EXPR:
15124 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15125 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15126 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15127 r = build_nt (code, op0, op1, op2);
15128 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15129 return r;
15132 case NEW_EXPR:
15134 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15135 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15136 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15137 r = build_nt (code, op0, op1, op2);
15138 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15139 return r;
15142 case DELETE_EXPR:
15144 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15145 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15146 r = build_nt (code, op0, op1);
15147 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15148 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15149 return r;
15152 case TEMPLATE_ID_EXPR:
15154 /* Substituted template arguments */
15155 tree fn = TREE_OPERAND (t, 0);
15156 tree targs = TREE_OPERAND (t, 1);
15158 fn = tsubst_copy (fn, args, complain, in_decl);
15159 if (targs)
15160 targs = tsubst_template_args (targs, args, complain, in_decl);
15162 return lookup_template_function (fn, targs);
15165 case TREE_LIST:
15167 tree purpose, value, chain;
15169 if (t == void_list_node)
15170 return t;
15172 purpose = TREE_PURPOSE (t);
15173 if (purpose)
15174 purpose = tsubst_copy (purpose, args, complain, in_decl);
15175 value = TREE_VALUE (t);
15176 if (value)
15177 value = tsubst_copy (value, args, complain, in_decl);
15178 chain = TREE_CHAIN (t);
15179 if (chain && chain != void_type_node)
15180 chain = tsubst_copy (chain, args, complain, in_decl);
15181 if (purpose == TREE_PURPOSE (t)
15182 && value == TREE_VALUE (t)
15183 && chain == TREE_CHAIN (t))
15184 return t;
15185 return tree_cons (purpose, value, chain);
15188 case RECORD_TYPE:
15189 case UNION_TYPE:
15190 case ENUMERAL_TYPE:
15191 case INTEGER_TYPE:
15192 case TEMPLATE_TYPE_PARM:
15193 case TEMPLATE_TEMPLATE_PARM:
15194 case BOUND_TEMPLATE_TEMPLATE_PARM:
15195 case TEMPLATE_PARM_INDEX:
15196 case POINTER_TYPE:
15197 case REFERENCE_TYPE:
15198 case OFFSET_TYPE:
15199 case FUNCTION_TYPE:
15200 case METHOD_TYPE:
15201 case ARRAY_TYPE:
15202 case TYPENAME_TYPE:
15203 case UNBOUND_CLASS_TEMPLATE:
15204 case TYPEOF_TYPE:
15205 case DECLTYPE_TYPE:
15206 case TYPE_DECL:
15207 return tsubst (t, args, complain, in_decl);
15209 case USING_DECL:
15210 t = DECL_NAME (t);
15211 /* Fall through. */
15212 case IDENTIFIER_NODE:
15213 if (IDENTIFIER_CONV_OP_P (t))
15215 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15216 return make_conv_op_name (new_type);
15218 else
15219 return t;
15221 case CONSTRUCTOR:
15222 /* This is handled by tsubst_copy_and_build. */
15223 gcc_unreachable ();
15225 case VA_ARG_EXPR:
15227 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15228 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15229 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15232 case CLEANUP_POINT_EXPR:
15233 /* We shouldn't have built any of these during initial template
15234 generation. Instead, they should be built during instantiation
15235 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15236 gcc_unreachable ();
15238 case OFFSET_REF:
15240 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15241 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15242 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15243 r = build2 (code, type, op0, op1);
15244 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15245 if (!mark_used (TREE_OPERAND (r, 1), complain)
15246 && !(complain & tf_error))
15247 return error_mark_node;
15248 return r;
15251 case EXPR_PACK_EXPANSION:
15252 error ("invalid use of pack expansion expression");
15253 return error_mark_node;
15255 case NONTYPE_ARGUMENT_PACK:
15256 error ("use %<...%> to expand argument pack");
15257 return error_mark_node;
15259 case VOID_CST:
15260 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15261 return t;
15263 case INTEGER_CST:
15264 case REAL_CST:
15265 case STRING_CST:
15266 case COMPLEX_CST:
15268 /* Instantiate any typedefs in the type. */
15269 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15270 r = fold_convert (type, t);
15271 gcc_assert (TREE_CODE (r) == code);
15272 return r;
15275 case PTRMEM_CST:
15276 /* These can sometimes show up in a partial instantiation, but never
15277 involve template parms. */
15278 gcc_assert (!uses_template_parms (t));
15279 return t;
15281 case UNARY_LEFT_FOLD_EXPR:
15282 return tsubst_unary_left_fold (t, args, complain, in_decl);
15283 case UNARY_RIGHT_FOLD_EXPR:
15284 return tsubst_unary_right_fold (t, args, complain, in_decl);
15285 case BINARY_LEFT_FOLD_EXPR:
15286 return tsubst_binary_left_fold (t, args, complain, in_decl);
15287 case BINARY_RIGHT_FOLD_EXPR:
15288 return tsubst_binary_right_fold (t, args, complain, in_decl);
15289 case PREDICT_EXPR:
15290 return t;
15292 default:
15293 /* We shouldn't get here, but keep going if !flag_checking. */
15294 if (flag_checking)
15295 gcc_unreachable ();
15296 return t;
15300 /* Helper function for tsubst_omp_clauses, used for instantiation of
15301 OMP_CLAUSE_DECL of clauses. */
15303 static tree
15304 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15305 tree in_decl)
15307 if (decl == NULL_TREE)
15308 return NULL_TREE;
15310 /* Handle an OpenMP array section represented as a TREE_LIST (or
15311 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15312 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15313 TREE_LIST. We can handle it exactly the same as an array section
15314 (purpose, value, and a chain), even though the nomenclature
15315 (low_bound, length, etc) is different. */
15316 if (TREE_CODE (decl) == TREE_LIST)
15318 tree low_bound
15319 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15320 /*integral_constant_expression_p=*/false);
15321 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15322 /*integral_constant_expression_p=*/false);
15323 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15324 in_decl);
15325 if (TREE_PURPOSE (decl) == low_bound
15326 && TREE_VALUE (decl) == length
15327 && TREE_CHAIN (decl) == chain)
15328 return decl;
15329 tree ret = tree_cons (low_bound, length, chain);
15330 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15331 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15332 return ret;
15334 tree ret = tsubst_expr (decl, args, complain, in_decl,
15335 /*integral_constant_expression_p=*/false);
15336 /* Undo convert_from_reference tsubst_expr could have called. */
15337 if (decl
15338 && REFERENCE_REF_P (ret)
15339 && !REFERENCE_REF_P (decl))
15340 ret = TREE_OPERAND (ret, 0);
15341 return ret;
15344 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15346 static tree
15347 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15348 tree args, tsubst_flags_t complain, tree in_decl)
15350 tree new_clauses = NULL_TREE, nc, oc;
15351 tree linear_no_step = NULL_TREE;
15353 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15355 nc = copy_node (oc);
15356 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15357 new_clauses = nc;
15359 switch (OMP_CLAUSE_CODE (nc))
15361 case OMP_CLAUSE_LASTPRIVATE:
15362 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15364 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15365 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15366 in_decl, /*integral_constant_expression_p=*/false);
15367 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15368 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15370 /* FALLTHRU */
15371 case OMP_CLAUSE_PRIVATE:
15372 case OMP_CLAUSE_SHARED:
15373 case OMP_CLAUSE_FIRSTPRIVATE:
15374 case OMP_CLAUSE_COPYIN:
15375 case OMP_CLAUSE_COPYPRIVATE:
15376 case OMP_CLAUSE_UNIFORM:
15377 case OMP_CLAUSE_DEPEND:
15378 case OMP_CLAUSE_FROM:
15379 case OMP_CLAUSE_TO:
15380 case OMP_CLAUSE_MAP:
15381 case OMP_CLAUSE_USE_DEVICE_PTR:
15382 case OMP_CLAUSE_IS_DEVICE_PTR:
15383 OMP_CLAUSE_DECL (nc)
15384 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15385 in_decl);
15386 break;
15387 case OMP_CLAUSE_TILE:
15388 case OMP_CLAUSE_IF:
15389 case OMP_CLAUSE_NUM_THREADS:
15390 case OMP_CLAUSE_SCHEDULE:
15391 case OMP_CLAUSE_COLLAPSE:
15392 case OMP_CLAUSE_FINAL:
15393 case OMP_CLAUSE_DEVICE:
15394 case OMP_CLAUSE_DIST_SCHEDULE:
15395 case OMP_CLAUSE_NUM_TEAMS:
15396 case OMP_CLAUSE_THREAD_LIMIT:
15397 case OMP_CLAUSE_SAFELEN:
15398 case OMP_CLAUSE_SIMDLEN:
15399 case OMP_CLAUSE_NUM_TASKS:
15400 case OMP_CLAUSE_GRAINSIZE:
15401 case OMP_CLAUSE_PRIORITY:
15402 case OMP_CLAUSE_ORDERED:
15403 case OMP_CLAUSE_HINT:
15404 case OMP_CLAUSE_NUM_GANGS:
15405 case OMP_CLAUSE_NUM_WORKERS:
15406 case OMP_CLAUSE_VECTOR_LENGTH:
15407 case OMP_CLAUSE_WORKER:
15408 case OMP_CLAUSE_VECTOR:
15409 case OMP_CLAUSE_ASYNC:
15410 case OMP_CLAUSE_WAIT:
15411 OMP_CLAUSE_OPERAND (nc, 0)
15412 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15413 in_decl, /*integral_constant_expression_p=*/false);
15414 break;
15415 case OMP_CLAUSE_REDUCTION:
15416 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15418 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15419 if (TREE_CODE (placeholder) == SCOPE_REF)
15421 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15422 complain, in_decl);
15423 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15424 = build_qualified_name (NULL_TREE, scope,
15425 TREE_OPERAND (placeholder, 1),
15426 false);
15428 else
15429 gcc_assert (identifier_p (placeholder));
15431 OMP_CLAUSE_DECL (nc)
15432 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15433 in_decl);
15434 break;
15435 case OMP_CLAUSE_GANG:
15436 case OMP_CLAUSE_ALIGNED:
15437 OMP_CLAUSE_DECL (nc)
15438 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15439 in_decl);
15440 OMP_CLAUSE_OPERAND (nc, 1)
15441 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15442 in_decl, /*integral_constant_expression_p=*/false);
15443 break;
15444 case OMP_CLAUSE_LINEAR:
15445 OMP_CLAUSE_DECL (nc)
15446 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15447 in_decl);
15448 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15450 gcc_assert (!linear_no_step);
15451 linear_no_step = nc;
15453 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15454 OMP_CLAUSE_LINEAR_STEP (nc)
15455 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15456 complain, in_decl);
15457 else
15458 OMP_CLAUSE_LINEAR_STEP (nc)
15459 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15460 in_decl,
15461 /*integral_constant_expression_p=*/false);
15462 break;
15463 case OMP_CLAUSE_NOWAIT:
15464 case OMP_CLAUSE_DEFAULT:
15465 case OMP_CLAUSE_UNTIED:
15466 case OMP_CLAUSE_MERGEABLE:
15467 case OMP_CLAUSE_INBRANCH:
15468 case OMP_CLAUSE_NOTINBRANCH:
15469 case OMP_CLAUSE_PROC_BIND:
15470 case OMP_CLAUSE_FOR:
15471 case OMP_CLAUSE_PARALLEL:
15472 case OMP_CLAUSE_SECTIONS:
15473 case OMP_CLAUSE_TASKGROUP:
15474 case OMP_CLAUSE_NOGROUP:
15475 case OMP_CLAUSE_THREADS:
15476 case OMP_CLAUSE_SIMD:
15477 case OMP_CLAUSE_DEFAULTMAP:
15478 case OMP_CLAUSE_INDEPENDENT:
15479 case OMP_CLAUSE_AUTO:
15480 case OMP_CLAUSE_SEQ:
15481 break;
15482 default:
15483 gcc_unreachable ();
15485 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15486 switch (OMP_CLAUSE_CODE (nc))
15488 case OMP_CLAUSE_SHARED:
15489 case OMP_CLAUSE_PRIVATE:
15490 case OMP_CLAUSE_FIRSTPRIVATE:
15491 case OMP_CLAUSE_LASTPRIVATE:
15492 case OMP_CLAUSE_COPYPRIVATE:
15493 case OMP_CLAUSE_LINEAR:
15494 case OMP_CLAUSE_REDUCTION:
15495 case OMP_CLAUSE_USE_DEVICE_PTR:
15496 case OMP_CLAUSE_IS_DEVICE_PTR:
15497 /* tsubst_expr on SCOPE_REF results in returning
15498 finish_non_static_data_member result. Undo that here. */
15499 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15500 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15501 == IDENTIFIER_NODE))
15503 tree t = OMP_CLAUSE_DECL (nc);
15504 tree v = t;
15505 while (v)
15506 switch (TREE_CODE (v))
15508 case COMPONENT_REF:
15509 case MEM_REF:
15510 case INDIRECT_REF:
15511 CASE_CONVERT:
15512 case POINTER_PLUS_EXPR:
15513 v = TREE_OPERAND (v, 0);
15514 continue;
15515 case PARM_DECL:
15516 if (DECL_CONTEXT (v) == current_function_decl
15517 && DECL_ARTIFICIAL (v)
15518 && DECL_NAME (v) == this_identifier)
15519 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15520 /* FALLTHRU */
15521 default:
15522 v = NULL_TREE;
15523 break;
15526 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15527 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15528 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15529 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15530 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15532 tree decl = OMP_CLAUSE_DECL (nc);
15533 if (VAR_P (decl))
15535 retrofit_lang_decl (decl);
15536 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15539 break;
15540 default:
15541 break;
15545 new_clauses = nreverse (new_clauses);
15546 if (ort != C_ORT_OMP_DECLARE_SIMD)
15548 new_clauses = finish_omp_clauses (new_clauses, ort);
15549 if (linear_no_step)
15550 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15551 if (nc == linear_no_step)
15553 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15554 break;
15557 return new_clauses;
15560 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15562 static tree
15563 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15564 tree in_decl)
15566 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15568 tree purpose, value, chain;
15570 if (t == NULL)
15571 return t;
15573 if (TREE_CODE (t) != TREE_LIST)
15574 return tsubst_copy_and_build (t, args, complain, in_decl,
15575 /*function_p=*/false,
15576 /*integral_constant_expression_p=*/false);
15578 if (t == void_list_node)
15579 return t;
15581 purpose = TREE_PURPOSE (t);
15582 if (purpose)
15583 purpose = RECUR (purpose);
15584 value = TREE_VALUE (t);
15585 if (value)
15587 if (TREE_CODE (value) != LABEL_DECL)
15588 value = RECUR (value);
15589 else
15591 value = lookup_label (DECL_NAME (value));
15592 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15593 TREE_USED (value) = 1;
15596 chain = TREE_CHAIN (t);
15597 if (chain && chain != void_type_node)
15598 chain = RECUR (chain);
15599 return tree_cons (purpose, value, chain);
15600 #undef RECUR
15603 /* Used to temporarily communicate the list of #pragma omp parallel
15604 clauses to #pragma omp for instantiation if they are combined
15605 together. */
15607 static tree *omp_parallel_combined_clauses;
15609 /* Substitute one OMP_FOR iterator. */
15611 static void
15612 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15613 tree initv, tree condv, tree incrv, tree *clauses,
15614 tree args, tsubst_flags_t complain, tree in_decl,
15615 bool integral_constant_expression_p)
15617 #define RECUR(NODE) \
15618 tsubst_expr ((NODE), args, complain, in_decl, \
15619 integral_constant_expression_p)
15620 tree decl, init, cond, incr;
15622 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15623 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15625 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15627 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15628 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15631 decl = TREE_OPERAND (init, 0);
15632 init = TREE_OPERAND (init, 1);
15633 tree decl_expr = NULL_TREE;
15634 if (init && TREE_CODE (init) == DECL_EXPR)
15636 /* We need to jump through some hoops to handle declarations in the
15637 init-statement, since we might need to handle auto deduction,
15638 but we need to keep control of initialization. */
15639 decl_expr = init;
15640 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15641 decl = tsubst_decl (decl, args, complain);
15643 else
15645 if (TREE_CODE (decl) == SCOPE_REF)
15647 decl = RECUR (decl);
15648 if (TREE_CODE (decl) == COMPONENT_REF)
15650 tree v = decl;
15651 while (v)
15652 switch (TREE_CODE (v))
15654 case COMPONENT_REF:
15655 case MEM_REF:
15656 case INDIRECT_REF:
15657 CASE_CONVERT:
15658 case POINTER_PLUS_EXPR:
15659 v = TREE_OPERAND (v, 0);
15660 continue;
15661 case PARM_DECL:
15662 if (DECL_CONTEXT (v) == current_function_decl
15663 && DECL_ARTIFICIAL (v)
15664 && DECL_NAME (v) == this_identifier)
15666 decl = TREE_OPERAND (decl, 1);
15667 decl = omp_privatize_field (decl, false);
15669 /* FALLTHRU */
15670 default:
15671 v = NULL_TREE;
15672 break;
15676 else
15677 decl = RECUR (decl);
15679 init = RECUR (init);
15681 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15682 if (auto_node && init)
15683 TREE_TYPE (decl)
15684 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15686 gcc_assert (!type_dependent_expression_p (decl));
15688 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15690 if (decl_expr)
15692 /* Declare the variable, but don't let that initialize it. */
15693 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15694 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15695 RECUR (decl_expr);
15696 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15699 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15700 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15701 if (TREE_CODE (incr) == MODIFY_EXPR)
15703 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15704 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15705 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15706 NOP_EXPR, rhs, complain);
15708 else
15709 incr = RECUR (incr);
15710 TREE_VEC_ELT (declv, i) = decl;
15711 TREE_VEC_ELT (initv, i) = init;
15712 TREE_VEC_ELT (condv, i) = cond;
15713 TREE_VEC_ELT (incrv, i) = incr;
15714 return;
15717 if (decl_expr)
15719 /* Declare and initialize the variable. */
15720 RECUR (decl_expr);
15721 init = NULL_TREE;
15723 else if (init)
15725 tree *pc;
15726 int j;
15727 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15729 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15731 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15732 && OMP_CLAUSE_DECL (*pc) == decl)
15733 break;
15734 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15735 && OMP_CLAUSE_DECL (*pc) == decl)
15737 if (j)
15738 break;
15739 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15740 tree c = *pc;
15741 *pc = OMP_CLAUSE_CHAIN (c);
15742 OMP_CLAUSE_CHAIN (c) = *clauses;
15743 *clauses = c;
15745 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15746 && OMP_CLAUSE_DECL (*pc) == decl)
15748 error ("iteration variable %qD should not be firstprivate",
15749 decl);
15750 *pc = OMP_CLAUSE_CHAIN (*pc);
15752 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15753 && OMP_CLAUSE_DECL (*pc) == decl)
15755 error ("iteration variable %qD should not be reduction",
15756 decl);
15757 *pc = OMP_CLAUSE_CHAIN (*pc);
15759 else
15760 pc = &OMP_CLAUSE_CHAIN (*pc);
15762 if (*pc)
15763 break;
15765 if (*pc == NULL_TREE)
15767 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15768 OMP_CLAUSE_DECL (c) = decl;
15769 c = finish_omp_clauses (c, C_ORT_OMP);
15770 if (c)
15772 OMP_CLAUSE_CHAIN (c) = *clauses;
15773 *clauses = c;
15777 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15778 if (COMPARISON_CLASS_P (cond))
15780 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15781 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15782 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15784 else
15785 cond = RECUR (cond);
15786 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15787 switch (TREE_CODE (incr))
15789 case PREINCREMENT_EXPR:
15790 case PREDECREMENT_EXPR:
15791 case POSTINCREMENT_EXPR:
15792 case POSTDECREMENT_EXPR:
15793 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15794 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15795 break;
15796 case MODIFY_EXPR:
15797 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15798 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15800 tree rhs = TREE_OPERAND (incr, 1);
15801 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15802 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15803 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15804 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15805 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15806 rhs0, rhs1));
15808 else
15809 incr = RECUR (incr);
15810 break;
15811 case MODOP_EXPR:
15812 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15813 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15815 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15816 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15817 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15818 TREE_TYPE (decl), lhs,
15819 RECUR (TREE_OPERAND (incr, 2))));
15821 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15822 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15823 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15825 tree rhs = TREE_OPERAND (incr, 2);
15826 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15827 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15828 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15829 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15830 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15831 rhs0, rhs1));
15833 else
15834 incr = RECUR (incr);
15835 break;
15836 default:
15837 incr = RECUR (incr);
15838 break;
15841 TREE_VEC_ELT (declv, i) = decl;
15842 TREE_VEC_ELT (initv, i) = init;
15843 TREE_VEC_ELT (condv, i) = cond;
15844 TREE_VEC_ELT (incrv, i) = incr;
15845 #undef RECUR
15848 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15849 of OMP_TARGET's body. */
15851 static tree
15852 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15854 *walk_subtrees = 0;
15855 switch (TREE_CODE (*tp))
15857 case OMP_TEAMS:
15858 return *tp;
15859 case BIND_EXPR:
15860 case STATEMENT_LIST:
15861 *walk_subtrees = 1;
15862 break;
15863 default:
15864 break;
15866 return NULL_TREE;
15869 /* Helper function for tsubst_expr. For decomposition declaration
15870 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15871 also the corresponding decls representing the identifiers
15872 of the decomposition declaration. Return DECL if successful
15873 or error_mark_node otherwise, set *FIRST to the first decl
15874 in the list chained through DECL_CHAIN and *CNT to the number
15875 of such decls. */
15877 static tree
15878 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15879 tsubst_flags_t complain, tree in_decl, tree *first,
15880 unsigned int *cnt)
15882 tree decl2, decl3, prev = decl;
15883 *cnt = 0;
15884 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15885 for (decl2 = DECL_CHAIN (pattern_decl);
15886 decl2
15887 && VAR_P (decl2)
15888 && DECL_DECOMPOSITION_P (decl2)
15889 && DECL_NAME (decl2);
15890 decl2 = DECL_CHAIN (decl2))
15892 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15894 gcc_assert (errorcount);
15895 return error_mark_node;
15897 (*cnt)++;
15898 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15899 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15900 tree v = DECL_VALUE_EXPR (decl2);
15901 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15902 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15903 decl3 = tsubst (decl2, args, complain, in_decl);
15904 SET_DECL_VALUE_EXPR (decl2, v);
15905 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15906 if (VAR_P (decl3))
15907 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15908 maybe_push_decl (decl3);
15909 if (error_operand_p (decl3))
15910 decl = error_mark_node;
15911 else if (decl != error_mark_node
15912 && DECL_CHAIN (decl3) != prev)
15914 gcc_assert (errorcount);
15915 decl = error_mark_node;
15917 else
15918 prev = decl3;
15920 *first = prev;
15921 return decl;
15924 /* Like tsubst_copy for expressions, etc. but also does semantic
15925 processing. */
15927 tree
15928 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15929 bool integral_constant_expression_p)
15931 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15932 #define RECUR(NODE) \
15933 tsubst_expr ((NODE), args, complain, in_decl, \
15934 integral_constant_expression_p)
15936 tree stmt, tmp;
15937 tree r;
15938 location_t loc;
15940 if (t == NULL_TREE || t == error_mark_node)
15941 return t;
15943 loc = input_location;
15944 if (EXPR_HAS_LOCATION (t))
15945 input_location = EXPR_LOCATION (t);
15946 if (STATEMENT_CODE_P (TREE_CODE (t)))
15947 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15949 switch (TREE_CODE (t))
15951 case STATEMENT_LIST:
15953 tree_stmt_iterator i;
15954 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15955 RECUR (tsi_stmt (i));
15956 break;
15959 case CTOR_INITIALIZER:
15960 finish_mem_initializers (tsubst_initializer_list
15961 (TREE_OPERAND (t, 0), args));
15962 break;
15964 case RETURN_EXPR:
15965 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15966 break;
15968 case EXPR_STMT:
15969 tmp = RECUR (EXPR_STMT_EXPR (t));
15970 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15971 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15972 else
15973 finish_expr_stmt (tmp);
15974 break;
15976 case USING_STMT:
15977 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15978 /*attribs=*/NULL_TREE);
15979 break;
15981 case DECL_EXPR:
15983 tree decl, pattern_decl;
15984 tree init;
15986 pattern_decl = decl = DECL_EXPR_DECL (t);
15987 if (TREE_CODE (decl) == LABEL_DECL)
15988 finish_label_decl (DECL_NAME (decl));
15989 else if (TREE_CODE (decl) == USING_DECL)
15991 tree scope = USING_DECL_SCOPE (decl);
15992 tree name = DECL_NAME (decl);
15994 scope = tsubst (scope, args, complain, in_decl);
15995 decl = lookup_qualified_name (scope, name,
15996 /*is_type_p=*/false,
15997 /*complain=*/false);
15998 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15999 qualified_name_lookup_error (scope, name, decl, input_location);
16000 else
16001 finish_local_using_decl (decl, scope, name);
16003 else if (DECL_PACK_P (decl))
16005 /* Don't build up decls for a variadic capture proxy, we'll
16006 instantiate the elements directly as needed. */
16007 break;
16009 else if (is_capture_proxy (decl)
16010 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16012 /* We're in tsubst_lambda_expr, we've already inserted a new
16013 capture proxy, so look it up and register it. */
16014 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16015 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16016 gcc_assert (inst != decl && is_capture_proxy (inst));
16017 register_local_specialization (inst, decl);
16018 break;
16020 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16021 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16022 /* Don't copy the old closure; we'll create a new one in
16023 tsubst_lambda_expr. */
16024 break;
16025 else
16027 init = DECL_INITIAL (decl);
16028 decl = tsubst (decl, args, complain, in_decl);
16029 if (decl != error_mark_node)
16031 /* By marking the declaration as instantiated, we avoid
16032 trying to instantiate it. Since instantiate_decl can't
16033 handle local variables, and since we've already done
16034 all that needs to be done, that's the right thing to
16035 do. */
16036 if (VAR_P (decl))
16037 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16038 if (VAR_P (decl)
16039 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16040 /* Anonymous aggregates are a special case. */
16041 finish_anon_union (decl);
16042 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16044 DECL_CONTEXT (decl) = current_function_decl;
16045 if (DECL_NAME (decl) == this_identifier)
16047 tree lam = DECL_CONTEXT (current_function_decl);
16048 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16049 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16051 insert_capture_proxy (decl);
16053 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16054 /* We already did a pushtag. */;
16055 else if (TREE_CODE (decl) == FUNCTION_DECL
16056 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16057 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16059 DECL_CONTEXT (decl) = NULL_TREE;
16060 pushdecl (decl);
16061 DECL_CONTEXT (decl) = current_function_decl;
16062 cp_check_omp_declare_reduction (decl);
16064 else
16066 int const_init = false;
16067 maybe_push_decl (decl);
16068 if (VAR_P (decl)
16069 && DECL_PRETTY_FUNCTION_P (decl))
16071 /* For __PRETTY_FUNCTION__ we have to adjust the
16072 initializer. */
16073 const char *const name
16074 = cxx_printable_name (current_function_decl, 2);
16075 init = cp_fname_init (name, &TREE_TYPE (decl));
16077 else
16078 init = tsubst_init (init, decl, args, complain, in_decl);
16080 if (VAR_P (decl))
16081 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16082 (pattern_decl));
16083 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16084 if (VAR_P (decl)
16085 && DECL_DECOMPOSITION_P (decl)
16086 && TREE_TYPE (pattern_decl) != error_mark_node)
16088 unsigned int cnt;
16089 tree first;
16090 decl = tsubst_decomp_names (decl, pattern_decl, args,
16091 complain, in_decl, &first,
16092 &cnt);
16093 if (decl != error_mark_node)
16094 cp_finish_decomp (decl, first, cnt);
16100 break;
16103 case FOR_STMT:
16104 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16105 RECUR (FOR_INIT_STMT (t));
16106 finish_init_stmt (stmt);
16107 tmp = RECUR (FOR_COND (t));
16108 finish_for_cond (tmp, stmt, false);
16109 tmp = RECUR (FOR_EXPR (t));
16110 finish_for_expr (tmp, stmt);
16111 RECUR (FOR_BODY (t));
16112 finish_for_stmt (stmt);
16113 break;
16115 case RANGE_FOR_STMT:
16117 tree decl, expr;
16118 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16119 decl = RANGE_FOR_DECL (t);
16120 decl = tsubst (decl, args, complain, in_decl);
16121 maybe_push_decl (decl);
16122 expr = RECUR (RANGE_FOR_EXPR (t));
16123 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16125 unsigned int cnt;
16126 tree first;
16127 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16128 complain, in_decl, &first, &cnt);
16129 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16130 RANGE_FOR_IVDEP (t));
16132 else
16133 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16134 RANGE_FOR_IVDEP (t));
16135 RECUR (RANGE_FOR_BODY (t));
16136 finish_for_stmt (stmt);
16138 break;
16140 case WHILE_STMT:
16141 stmt = begin_while_stmt ();
16142 tmp = RECUR (WHILE_COND (t));
16143 finish_while_stmt_cond (tmp, stmt, false);
16144 RECUR (WHILE_BODY (t));
16145 finish_while_stmt (stmt);
16146 break;
16148 case DO_STMT:
16149 stmt = begin_do_stmt ();
16150 RECUR (DO_BODY (t));
16151 finish_do_body (stmt);
16152 tmp = RECUR (DO_COND (t));
16153 finish_do_stmt (tmp, stmt, false);
16154 break;
16156 case IF_STMT:
16157 stmt = begin_if_stmt ();
16158 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16159 tmp = RECUR (IF_COND (t));
16160 tmp = finish_if_stmt_cond (tmp, stmt);
16161 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16162 /* Don't instantiate the THEN_CLAUSE. */;
16163 else
16165 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16166 if (inhibit)
16167 ++c_inhibit_evaluation_warnings;
16168 RECUR (THEN_CLAUSE (t));
16169 if (inhibit)
16170 --c_inhibit_evaluation_warnings;
16172 finish_then_clause (stmt);
16174 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16175 /* Don't instantiate the ELSE_CLAUSE. */;
16176 else if (ELSE_CLAUSE (t))
16178 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16179 begin_else_clause (stmt);
16180 if (inhibit)
16181 ++c_inhibit_evaluation_warnings;
16182 RECUR (ELSE_CLAUSE (t));
16183 if (inhibit)
16184 --c_inhibit_evaluation_warnings;
16185 finish_else_clause (stmt);
16188 finish_if_stmt (stmt);
16189 break;
16191 case BIND_EXPR:
16192 if (BIND_EXPR_BODY_BLOCK (t))
16193 stmt = begin_function_body ();
16194 else
16195 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16196 ? BCS_TRY_BLOCK : 0);
16198 RECUR (BIND_EXPR_BODY (t));
16200 if (BIND_EXPR_BODY_BLOCK (t))
16201 finish_function_body (stmt);
16202 else
16203 finish_compound_stmt (stmt);
16204 break;
16206 case BREAK_STMT:
16207 finish_break_stmt ();
16208 break;
16210 case CONTINUE_STMT:
16211 finish_continue_stmt ();
16212 break;
16214 case SWITCH_STMT:
16215 stmt = begin_switch_stmt ();
16216 tmp = RECUR (SWITCH_STMT_COND (t));
16217 finish_switch_cond (tmp, stmt);
16218 RECUR (SWITCH_STMT_BODY (t));
16219 finish_switch_stmt (stmt);
16220 break;
16222 case CASE_LABEL_EXPR:
16224 tree low = RECUR (CASE_LOW (t));
16225 tree high = RECUR (CASE_HIGH (t));
16226 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16227 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16228 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16229 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16231 break;
16233 case LABEL_EXPR:
16235 tree decl = LABEL_EXPR_LABEL (t);
16236 tree label;
16238 label = finish_label_stmt (DECL_NAME (decl));
16239 if (TREE_CODE (label) == LABEL_DECL)
16240 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16241 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16242 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16244 break;
16246 case GOTO_EXPR:
16247 tmp = GOTO_DESTINATION (t);
16248 if (TREE_CODE (tmp) != LABEL_DECL)
16249 /* Computed goto's must be tsubst'd into. On the other hand,
16250 non-computed gotos must not be; the identifier in question
16251 will have no binding. */
16252 tmp = RECUR (tmp);
16253 else
16254 tmp = DECL_NAME (tmp);
16255 finish_goto_stmt (tmp);
16256 break;
16258 case ASM_EXPR:
16260 tree string = RECUR (ASM_STRING (t));
16261 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16262 complain, in_decl);
16263 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16264 complain, in_decl);
16265 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16266 complain, in_decl);
16267 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16268 complain, in_decl);
16269 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16270 clobbers, labels);
16271 tree asm_expr = tmp;
16272 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16273 asm_expr = TREE_OPERAND (asm_expr, 0);
16274 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16276 break;
16278 case TRY_BLOCK:
16279 if (CLEANUP_P (t))
16281 stmt = begin_try_block ();
16282 RECUR (TRY_STMTS (t));
16283 finish_cleanup_try_block (stmt);
16284 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16286 else
16288 tree compound_stmt = NULL_TREE;
16290 if (FN_TRY_BLOCK_P (t))
16291 stmt = begin_function_try_block (&compound_stmt);
16292 else
16293 stmt = begin_try_block ();
16295 RECUR (TRY_STMTS (t));
16297 if (FN_TRY_BLOCK_P (t))
16298 finish_function_try_block (stmt);
16299 else
16300 finish_try_block (stmt);
16302 RECUR (TRY_HANDLERS (t));
16303 if (FN_TRY_BLOCK_P (t))
16304 finish_function_handler_sequence (stmt, compound_stmt);
16305 else
16306 finish_handler_sequence (stmt);
16308 break;
16310 case HANDLER:
16312 tree decl = HANDLER_PARMS (t);
16314 if (decl)
16316 decl = tsubst (decl, args, complain, in_decl);
16317 /* Prevent instantiate_decl from trying to instantiate
16318 this variable. We've already done all that needs to be
16319 done. */
16320 if (decl != error_mark_node)
16321 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16323 stmt = begin_handler ();
16324 finish_handler_parms (decl, stmt);
16325 RECUR (HANDLER_BODY (t));
16326 finish_handler (stmt);
16328 break;
16330 case TAG_DEFN:
16331 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16332 if (CLASS_TYPE_P (tmp))
16334 /* Local classes are not independent templates; they are
16335 instantiated along with their containing function. And this
16336 way we don't have to deal with pushing out of one local class
16337 to instantiate a member of another local class. */
16338 /* Closures are handled by the LAMBDA_EXPR. */
16339 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16340 complete_type (tmp);
16341 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16342 if ((VAR_P (fld)
16343 || (TREE_CODE (fld) == FUNCTION_DECL
16344 && !DECL_ARTIFICIAL (fld)))
16345 && DECL_TEMPLATE_INSTANTIATION (fld))
16346 instantiate_decl (fld, /*defer_ok=*/false,
16347 /*expl_inst_class=*/false);
16349 break;
16351 case STATIC_ASSERT:
16353 tree condition;
16355 ++c_inhibit_evaluation_warnings;
16356 condition =
16357 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16358 args,
16359 complain, in_decl,
16360 /*integral_constant_expression_p=*/true);
16361 --c_inhibit_evaluation_warnings;
16363 finish_static_assert (condition,
16364 STATIC_ASSERT_MESSAGE (t),
16365 STATIC_ASSERT_SOURCE_LOCATION (t),
16366 /*member_p=*/false);
16368 break;
16370 case OACC_KERNELS:
16371 case OACC_PARALLEL:
16372 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16373 in_decl);
16374 stmt = begin_omp_parallel ();
16375 RECUR (OMP_BODY (t));
16376 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16377 break;
16379 case OMP_PARALLEL:
16380 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16381 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16382 complain, in_decl);
16383 if (OMP_PARALLEL_COMBINED (t))
16384 omp_parallel_combined_clauses = &tmp;
16385 stmt = begin_omp_parallel ();
16386 RECUR (OMP_PARALLEL_BODY (t));
16387 gcc_assert (omp_parallel_combined_clauses == NULL);
16388 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16389 = OMP_PARALLEL_COMBINED (t);
16390 pop_omp_privatization_clauses (r);
16391 break;
16393 case OMP_TASK:
16394 r = push_omp_privatization_clauses (false);
16395 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16396 complain, in_decl);
16397 stmt = begin_omp_task ();
16398 RECUR (OMP_TASK_BODY (t));
16399 finish_omp_task (tmp, stmt);
16400 pop_omp_privatization_clauses (r);
16401 break;
16403 case OMP_FOR:
16404 case OMP_SIMD:
16405 case CILK_SIMD:
16406 case CILK_FOR:
16407 case OMP_DISTRIBUTE:
16408 case OMP_TASKLOOP:
16409 case OACC_LOOP:
16411 tree clauses, body, pre_body;
16412 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16413 tree orig_declv = NULL_TREE;
16414 tree incrv = NULL_TREE;
16415 enum c_omp_region_type ort = C_ORT_OMP;
16416 int i;
16418 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16419 ort = C_ORT_CILK;
16420 else if (TREE_CODE (t) == OACC_LOOP)
16421 ort = C_ORT_ACC;
16423 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16424 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16425 in_decl);
16426 if (OMP_FOR_INIT (t) != NULL_TREE)
16428 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16429 if (OMP_FOR_ORIG_DECLS (t))
16430 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16431 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16432 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16433 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16436 stmt = begin_omp_structured_block ();
16438 pre_body = push_stmt_list ();
16439 RECUR (OMP_FOR_PRE_BODY (t));
16440 pre_body = pop_stmt_list (pre_body);
16442 if (OMP_FOR_INIT (t) != NULL_TREE)
16443 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16444 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16445 incrv, &clauses, args, complain, in_decl,
16446 integral_constant_expression_p);
16447 omp_parallel_combined_clauses = NULL;
16449 body = push_stmt_list ();
16450 RECUR (OMP_FOR_BODY (t));
16451 body = pop_stmt_list (body);
16453 if (OMP_FOR_INIT (t) != NULL_TREE)
16454 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16455 orig_declv, initv, condv, incrv, body, pre_body,
16456 NULL, clauses);
16457 else
16459 t = make_node (TREE_CODE (t));
16460 TREE_TYPE (t) = void_type_node;
16461 OMP_FOR_BODY (t) = body;
16462 OMP_FOR_PRE_BODY (t) = pre_body;
16463 OMP_FOR_CLAUSES (t) = clauses;
16464 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16465 add_stmt (t);
16468 add_stmt (finish_omp_structured_block (stmt));
16469 pop_omp_privatization_clauses (r);
16471 break;
16473 case OMP_SECTIONS:
16474 omp_parallel_combined_clauses = NULL;
16475 /* FALLTHRU */
16476 case OMP_SINGLE:
16477 case OMP_TEAMS:
16478 case OMP_CRITICAL:
16479 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16480 && OMP_TEAMS_COMBINED (t));
16481 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16482 in_decl);
16483 stmt = push_stmt_list ();
16484 RECUR (OMP_BODY (t));
16485 stmt = pop_stmt_list (stmt);
16487 t = copy_node (t);
16488 OMP_BODY (t) = stmt;
16489 OMP_CLAUSES (t) = tmp;
16490 add_stmt (t);
16491 pop_omp_privatization_clauses (r);
16492 break;
16494 case OACC_DATA:
16495 case OMP_TARGET_DATA:
16496 case OMP_TARGET:
16497 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16498 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16499 in_decl);
16500 keep_next_level (true);
16501 stmt = begin_omp_structured_block ();
16503 RECUR (OMP_BODY (t));
16504 stmt = finish_omp_structured_block (stmt);
16506 t = copy_node (t);
16507 OMP_BODY (t) = stmt;
16508 OMP_CLAUSES (t) = tmp;
16509 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16511 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16512 if (teams)
16514 /* For combined target teams, ensure the num_teams and
16515 thread_limit clause expressions are evaluated on the host,
16516 before entering the target construct. */
16517 tree c;
16518 for (c = OMP_TEAMS_CLAUSES (teams);
16519 c; c = OMP_CLAUSE_CHAIN (c))
16520 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16521 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16522 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16524 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16525 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16526 if (expr == error_mark_node)
16527 continue;
16528 tmp = TARGET_EXPR_SLOT (expr);
16529 add_stmt (expr);
16530 OMP_CLAUSE_OPERAND (c, 0) = expr;
16531 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16532 OMP_CLAUSE_FIRSTPRIVATE);
16533 OMP_CLAUSE_DECL (tc) = tmp;
16534 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16535 OMP_TARGET_CLAUSES (t) = tc;
16539 add_stmt (t);
16540 break;
16542 case OACC_DECLARE:
16543 t = copy_node (t);
16544 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16545 complain, in_decl);
16546 OACC_DECLARE_CLAUSES (t) = tmp;
16547 add_stmt (t);
16548 break;
16550 case OMP_TARGET_UPDATE:
16551 case OMP_TARGET_ENTER_DATA:
16552 case OMP_TARGET_EXIT_DATA:
16553 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16554 complain, in_decl);
16555 t = copy_node (t);
16556 OMP_STANDALONE_CLAUSES (t) = tmp;
16557 add_stmt (t);
16558 break;
16560 case OACC_ENTER_DATA:
16561 case OACC_EXIT_DATA:
16562 case OACC_UPDATE:
16563 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16564 complain, in_decl);
16565 t = copy_node (t);
16566 OMP_STANDALONE_CLAUSES (t) = tmp;
16567 add_stmt (t);
16568 break;
16570 case OMP_ORDERED:
16571 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16572 complain, in_decl);
16573 stmt = push_stmt_list ();
16574 RECUR (OMP_BODY (t));
16575 stmt = pop_stmt_list (stmt);
16577 t = copy_node (t);
16578 OMP_BODY (t) = stmt;
16579 OMP_ORDERED_CLAUSES (t) = tmp;
16580 add_stmt (t);
16581 break;
16583 case OMP_SECTION:
16584 case OMP_MASTER:
16585 case OMP_TASKGROUP:
16586 stmt = push_stmt_list ();
16587 RECUR (OMP_BODY (t));
16588 stmt = pop_stmt_list (stmt);
16590 t = copy_node (t);
16591 OMP_BODY (t) = stmt;
16592 add_stmt (t);
16593 break;
16595 case OMP_ATOMIC:
16596 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16597 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16599 tree op1 = TREE_OPERAND (t, 1);
16600 tree rhs1 = NULL_TREE;
16601 tree lhs, rhs;
16602 if (TREE_CODE (op1) == COMPOUND_EXPR)
16604 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16605 op1 = TREE_OPERAND (op1, 1);
16607 lhs = RECUR (TREE_OPERAND (op1, 0));
16608 rhs = RECUR (TREE_OPERAND (op1, 1));
16609 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16610 NULL_TREE, NULL_TREE, rhs1,
16611 OMP_ATOMIC_SEQ_CST (t));
16613 else
16615 tree op1 = TREE_OPERAND (t, 1);
16616 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16617 tree rhs1 = NULL_TREE;
16618 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16619 enum tree_code opcode = NOP_EXPR;
16620 if (code == OMP_ATOMIC_READ)
16622 v = RECUR (TREE_OPERAND (op1, 0));
16623 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16625 else if (code == OMP_ATOMIC_CAPTURE_OLD
16626 || code == OMP_ATOMIC_CAPTURE_NEW)
16628 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16629 v = RECUR (TREE_OPERAND (op1, 0));
16630 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16631 if (TREE_CODE (op11) == COMPOUND_EXPR)
16633 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16634 op11 = TREE_OPERAND (op11, 1);
16636 lhs = RECUR (TREE_OPERAND (op11, 0));
16637 rhs = RECUR (TREE_OPERAND (op11, 1));
16638 opcode = TREE_CODE (op11);
16639 if (opcode == MODIFY_EXPR)
16640 opcode = NOP_EXPR;
16642 else
16644 code = OMP_ATOMIC;
16645 lhs = RECUR (TREE_OPERAND (op1, 0));
16646 rhs = RECUR (TREE_OPERAND (op1, 1));
16648 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16649 OMP_ATOMIC_SEQ_CST (t));
16651 break;
16653 case TRANSACTION_EXPR:
16655 int flags = 0;
16656 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16657 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16659 if (TRANSACTION_EXPR_IS_STMT (t))
16661 tree body = TRANSACTION_EXPR_BODY (t);
16662 tree noex = NULL_TREE;
16663 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16665 noex = MUST_NOT_THROW_COND (body);
16666 if (noex == NULL_TREE)
16667 noex = boolean_true_node;
16668 body = TREE_OPERAND (body, 0);
16670 stmt = begin_transaction_stmt (input_location, NULL, flags);
16671 RECUR (body);
16672 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16674 else
16676 stmt = build_transaction_expr (EXPR_LOCATION (t),
16677 RECUR (TRANSACTION_EXPR_BODY (t)),
16678 flags, NULL_TREE);
16679 RETURN (stmt);
16682 break;
16684 case MUST_NOT_THROW_EXPR:
16686 tree op0 = RECUR (TREE_OPERAND (t, 0));
16687 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16688 RETURN (build_must_not_throw_expr (op0, cond));
16691 case EXPR_PACK_EXPANSION:
16692 error ("invalid use of pack expansion expression");
16693 RETURN (error_mark_node);
16695 case NONTYPE_ARGUMENT_PACK:
16696 error ("use %<...%> to expand argument pack");
16697 RETURN (error_mark_node);
16699 case CILK_SPAWN_STMT:
16700 cfun->calls_cilk_spawn = 1;
16701 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16703 case CILK_SYNC_STMT:
16704 RETURN (build_cilk_sync ());
16706 case COMPOUND_EXPR:
16707 tmp = RECUR (TREE_OPERAND (t, 0));
16708 if (tmp == NULL_TREE)
16709 /* If the first operand was a statement, we're done with it. */
16710 RETURN (RECUR (TREE_OPERAND (t, 1)));
16711 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16712 RECUR (TREE_OPERAND (t, 1)),
16713 complain));
16715 case ANNOTATE_EXPR:
16716 tmp = RECUR (TREE_OPERAND (t, 0));
16717 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16718 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16720 default:
16721 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16723 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16724 /*function_p=*/false,
16725 integral_constant_expression_p));
16728 RETURN (NULL_TREE);
16729 out:
16730 input_location = loc;
16731 return r;
16732 #undef RECUR
16733 #undef RETURN
16736 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16737 function. For description of the body see comment above
16738 cp_parser_omp_declare_reduction_exprs. */
16740 static void
16741 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16743 if (t == NULL_TREE || t == error_mark_node)
16744 return;
16746 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16748 tree_stmt_iterator tsi;
16749 int i;
16750 tree stmts[7];
16751 memset (stmts, 0, sizeof stmts);
16752 for (i = 0, tsi = tsi_start (t);
16753 i < 7 && !tsi_end_p (tsi);
16754 i++, tsi_next (&tsi))
16755 stmts[i] = tsi_stmt (tsi);
16756 gcc_assert (tsi_end_p (tsi));
16758 if (i >= 3)
16760 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16761 && TREE_CODE (stmts[1]) == DECL_EXPR);
16762 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16763 args, complain, in_decl);
16764 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16765 args, complain, in_decl);
16766 DECL_CONTEXT (omp_out) = current_function_decl;
16767 DECL_CONTEXT (omp_in) = current_function_decl;
16768 keep_next_level (true);
16769 tree block = begin_omp_structured_block ();
16770 tsubst_expr (stmts[2], args, complain, in_decl, false);
16771 block = finish_omp_structured_block (block);
16772 block = maybe_cleanup_point_expr_void (block);
16773 add_decl_expr (omp_out);
16774 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16775 TREE_NO_WARNING (omp_out) = 1;
16776 add_decl_expr (omp_in);
16777 finish_expr_stmt (block);
16779 if (i >= 6)
16781 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16782 && TREE_CODE (stmts[4]) == DECL_EXPR);
16783 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16784 args, complain, in_decl);
16785 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16786 args, complain, in_decl);
16787 DECL_CONTEXT (omp_priv) = current_function_decl;
16788 DECL_CONTEXT (omp_orig) = current_function_decl;
16789 keep_next_level (true);
16790 tree block = begin_omp_structured_block ();
16791 tsubst_expr (stmts[5], args, complain, in_decl, false);
16792 block = finish_omp_structured_block (block);
16793 block = maybe_cleanup_point_expr_void (block);
16794 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16795 add_decl_expr (omp_priv);
16796 add_decl_expr (omp_orig);
16797 finish_expr_stmt (block);
16798 if (i == 7)
16799 add_decl_expr (omp_orig);
16803 /* T is a postfix-expression that is not being used in a function
16804 call. Return the substituted version of T. */
16806 static tree
16807 tsubst_non_call_postfix_expression (tree t, tree args,
16808 tsubst_flags_t complain,
16809 tree in_decl)
16811 if (TREE_CODE (t) == SCOPE_REF)
16812 t = tsubst_qualified_id (t, args, complain, in_decl,
16813 /*done=*/false, /*address_p=*/false);
16814 else
16815 t = tsubst_copy_and_build (t, args, complain, in_decl,
16816 /*function_p=*/false,
16817 /*integral_constant_expression_p=*/false);
16819 return t;
16822 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16823 instantiation context. Instantiating a pack expansion containing a lambda
16824 might result in multiple lambdas all based on the same lambda in the
16825 template. */
16827 tree
16828 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16830 tree oldfn = lambda_function (t);
16831 in_decl = oldfn;
16833 tree r = build_lambda_expr ();
16835 LAMBDA_EXPR_LOCATION (r)
16836 = LAMBDA_EXPR_LOCATION (t);
16837 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16838 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16839 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16841 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16842 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16843 else
16844 record_lambda_scope (r);
16846 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16847 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16849 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16850 cap = TREE_CHAIN (cap))
16852 tree field = TREE_PURPOSE (cap);
16853 if (PACK_EXPANSION_P (field))
16854 field = PACK_EXPANSION_PATTERN (field);
16855 field = tsubst_decl (field, args, complain);
16857 if (field == error_mark_node)
16858 return error_mark_node;
16860 tree init = TREE_VALUE (cap);
16861 if (PACK_EXPANSION_P (init))
16862 init = tsubst_pack_expansion (init, args, complain, in_decl);
16863 else
16864 init = tsubst_copy_and_build (init, args, complain, in_decl,
16865 /*fn*/false, /*constexpr*/false);
16867 if (TREE_CODE (field) == TREE_VEC)
16869 int len = TREE_VEC_LENGTH (field);
16870 gcc_assert (TREE_CODE (init) == TREE_VEC
16871 && TREE_VEC_LENGTH (init) == len);
16872 for (int i = 0; i < len; ++i)
16873 LAMBDA_EXPR_CAPTURE_LIST (r)
16874 = tree_cons (TREE_VEC_ELT (field, i),
16875 TREE_VEC_ELT (init, i),
16876 LAMBDA_EXPR_CAPTURE_LIST (r));
16878 else
16880 LAMBDA_EXPR_CAPTURE_LIST (r)
16881 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16883 if (id_equal (DECL_NAME (field), "__this"))
16884 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16888 tree type = begin_lambda_type (r);
16890 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16891 determine_visibility (TYPE_NAME (type));
16893 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16895 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16896 ? DECL_TI_TEMPLATE (oldfn)
16897 : NULL_TREE);
16899 tree fntype = static_fn_type (oldfn);
16900 if (oldtmpl)
16901 ++processing_template_decl;
16902 fntype = tsubst (fntype, args, complain, in_decl);
16903 if (oldtmpl)
16904 --processing_template_decl;
16906 if (fntype == error_mark_node)
16907 r = error_mark_node;
16908 else
16910 /* Fix the type of 'this'. */
16911 fntype = build_memfn_type (fntype, type,
16912 type_memfn_quals (fntype),
16913 type_memfn_rqual (fntype));
16914 tree fn, tmpl;
16915 if (oldtmpl)
16917 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16918 fn = DECL_TEMPLATE_RESULT (tmpl);
16919 finish_member_declaration (tmpl);
16921 else
16923 tmpl = NULL_TREE;
16924 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16925 finish_member_declaration (fn);
16928 /* Let finish_function set this. */
16929 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16931 bool nested = cfun;
16932 if (nested)
16933 push_function_context ();
16935 local_specialization_stack s (lss_copy);
16937 tree body = start_lambda_function (fn, r);
16939 register_parameter_specializations (oldfn, fn);
16941 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16942 /*constexpr*/false);
16944 finish_lambda_function (body);
16946 if (nested)
16947 pop_function_context ();
16949 /* The capture list was built up in reverse order; fix that now. */
16950 LAMBDA_EXPR_CAPTURE_LIST (r)
16951 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16953 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16955 maybe_add_lambda_conv_op (type);
16958 finish_struct (type, /*attr*/NULL_TREE);
16960 insert_pending_capture_proxies ();
16962 return r;
16965 /* Like tsubst but deals with expressions and performs semantic
16966 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16968 tree
16969 tsubst_copy_and_build (tree t,
16970 tree args,
16971 tsubst_flags_t complain,
16972 tree in_decl,
16973 bool function_p,
16974 bool integral_constant_expression_p)
16976 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16977 #define RECUR(NODE) \
16978 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16979 /*function_p=*/false, \
16980 integral_constant_expression_p)
16982 tree retval, op1;
16983 location_t loc;
16985 if (t == NULL_TREE || t == error_mark_node)
16986 return t;
16988 loc = input_location;
16989 if (EXPR_HAS_LOCATION (t))
16990 input_location = EXPR_LOCATION (t);
16992 /* N3276 decltype magic only applies to calls at the top level or on the
16993 right side of a comma. */
16994 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16995 complain &= ~tf_decltype;
16997 switch (TREE_CODE (t))
16999 case USING_DECL:
17000 t = DECL_NAME (t);
17001 /* Fall through. */
17002 case IDENTIFIER_NODE:
17004 tree decl;
17005 cp_id_kind idk;
17006 bool non_integral_constant_expression_p;
17007 const char *error_msg;
17009 if (IDENTIFIER_CONV_OP_P (t))
17011 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17012 t = make_conv_op_name (new_type);
17015 /* Look up the name. */
17016 decl = lookup_name (t);
17018 /* By convention, expressions use ERROR_MARK_NODE to indicate
17019 failure, not NULL_TREE. */
17020 if (decl == NULL_TREE)
17021 decl = error_mark_node;
17023 decl = finish_id_expression (t, decl, NULL_TREE,
17024 &idk,
17025 integral_constant_expression_p,
17026 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17027 &non_integral_constant_expression_p,
17028 /*template_p=*/false,
17029 /*done=*/true,
17030 /*address_p=*/false,
17031 /*template_arg_p=*/false,
17032 &error_msg,
17033 input_location);
17034 if (error_msg)
17035 error (error_msg);
17036 if (!function_p && identifier_p (decl))
17038 if (complain & tf_error)
17039 unqualified_name_lookup_error (decl);
17040 decl = error_mark_node;
17042 RETURN (decl);
17045 case TEMPLATE_ID_EXPR:
17047 tree object;
17048 tree templ = RECUR (TREE_OPERAND (t, 0));
17049 tree targs = TREE_OPERAND (t, 1);
17051 if (targs)
17052 targs = tsubst_template_args (targs, args, complain, in_decl);
17053 if (targs == error_mark_node)
17054 return error_mark_node;
17056 if (TREE_CODE (templ) == SCOPE_REF)
17058 tree name = TREE_OPERAND (templ, 1);
17059 tree tid = lookup_template_function (name, targs);
17060 TREE_OPERAND (templ, 1) = tid;
17061 return templ;
17064 if (variable_template_p (templ))
17065 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17067 if (TREE_CODE (templ) == COMPONENT_REF)
17069 object = TREE_OPERAND (templ, 0);
17070 templ = TREE_OPERAND (templ, 1);
17072 else
17073 object = NULL_TREE;
17074 templ = lookup_template_function (templ, targs);
17076 if (object)
17077 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17078 object, templ, NULL_TREE));
17079 else
17080 RETURN (baselink_for_fns (templ));
17083 case INDIRECT_REF:
17085 tree r = RECUR (TREE_OPERAND (t, 0));
17087 if (REFERENCE_REF_P (t))
17089 /* A type conversion to reference type will be enclosed in
17090 such an indirect ref, but the substitution of the cast
17091 will have also added such an indirect ref. */
17092 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
17093 r = convert_from_reference (r);
17095 else
17096 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17097 complain|decltype_flag);
17099 if (TREE_CODE (r) == INDIRECT_REF)
17100 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17102 RETURN (r);
17105 case NOP_EXPR:
17107 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17108 tree op0 = RECUR (TREE_OPERAND (t, 0));
17109 RETURN (build_nop (type, op0));
17112 case IMPLICIT_CONV_EXPR:
17114 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17115 tree expr = RECUR (TREE_OPERAND (t, 0));
17116 int flags = LOOKUP_IMPLICIT;
17117 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17118 flags = LOOKUP_NORMAL;
17119 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17120 flags));
17123 case CONVERT_EXPR:
17125 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17126 tree op0 = RECUR (TREE_OPERAND (t, 0));
17127 RETURN (build1 (CONVERT_EXPR, type, op0));
17130 case CAST_EXPR:
17131 case REINTERPRET_CAST_EXPR:
17132 case CONST_CAST_EXPR:
17133 case DYNAMIC_CAST_EXPR:
17134 case STATIC_CAST_EXPR:
17136 tree type;
17137 tree op, r = NULL_TREE;
17139 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17140 if (integral_constant_expression_p
17141 && !cast_valid_in_integral_constant_expression_p (type))
17143 if (complain & tf_error)
17144 error ("a cast to a type other than an integral or "
17145 "enumeration type cannot appear in a constant-expression");
17146 RETURN (error_mark_node);
17149 op = RECUR (TREE_OPERAND (t, 0));
17151 warning_sentinel s(warn_useless_cast);
17152 switch (TREE_CODE (t))
17154 case CAST_EXPR:
17155 r = build_functional_cast (type, op, complain);
17156 break;
17157 case REINTERPRET_CAST_EXPR:
17158 r = build_reinterpret_cast (type, op, complain);
17159 break;
17160 case CONST_CAST_EXPR:
17161 r = build_const_cast (type, op, complain);
17162 break;
17163 case DYNAMIC_CAST_EXPR:
17164 r = build_dynamic_cast (type, op, complain);
17165 break;
17166 case STATIC_CAST_EXPR:
17167 r = build_static_cast (type, op, complain);
17168 break;
17169 default:
17170 gcc_unreachable ();
17173 RETURN (r);
17176 case POSTDECREMENT_EXPR:
17177 case POSTINCREMENT_EXPR:
17178 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17179 args, complain, in_decl);
17180 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17181 complain|decltype_flag));
17183 case PREDECREMENT_EXPR:
17184 case PREINCREMENT_EXPR:
17185 case NEGATE_EXPR:
17186 case BIT_NOT_EXPR:
17187 case ABS_EXPR:
17188 case TRUTH_NOT_EXPR:
17189 case UNARY_PLUS_EXPR: /* Unary + */
17190 case REALPART_EXPR:
17191 case IMAGPART_EXPR:
17192 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17193 RECUR (TREE_OPERAND (t, 0)),
17194 complain|decltype_flag));
17196 case FIX_TRUNC_EXPR:
17197 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17198 false, complain));
17200 case ADDR_EXPR:
17201 op1 = TREE_OPERAND (t, 0);
17202 if (TREE_CODE (op1) == LABEL_DECL)
17203 RETURN (finish_label_address_expr (DECL_NAME (op1),
17204 EXPR_LOCATION (op1)));
17205 if (TREE_CODE (op1) == SCOPE_REF)
17206 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17207 /*done=*/true, /*address_p=*/true);
17208 else
17209 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17210 in_decl);
17211 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17212 complain|decltype_flag));
17214 case PLUS_EXPR:
17215 case MINUS_EXPR:
17216 case MULT_EXPR:
17217 case TRUNC_DIV_EXPR:
17218 case CEIL_DIV_EXPR:
17219 case FLOOR_DIV_EXPR:
17220 case ROUND_DIV_EXPR:
17221 case EXACT_DIV_EXPR:
17222 case BIT_AND_EXPR:
17223 case BIT_IOR_EXPR:
17224 case BIT_XOR_EXPR:
17225 case TRUNC_MOD_EXPR:
17226 case FLOOR_MOD_EXPR:
17227 case TRUTH_ANDIF_EXPR:
17228 case TRUTH_ORIF_EXPR:
17229 case TRUTH_AND_EXPR:
17230 case TRUTH_OR_EXPR:
17231 case RSHIFT_EXPR:
17232 case LSHIFT_EXPR:
17233 case RROTATE_EXPR:
17234 case LROTATE_EXPR:
17235 case EQ_EXPR:
17236 case NE_EXPR:
17237 case MAX_EXPR:
17238 case MIN_EXPR:
17239 case LE_EXPR:
17240 case GE_EXPR:
17241 case LT_EXPR:
17242 case GT_EXPR:
17243 case MEMBER_REF:
17244 case DOTSTAR_EXPR:
17246 warning_sentinel s1(warn_type_limits);
17247 warning_sentinel s2(warn_div_by_zero);
17248 warning_sentinel s3(warn_logical_op);
17249 warning_sentinel s4(warn_tautological_compare);
17250 tree op0 = RECUR (TREE_OPERAND (t, 0));
17251 tree op1 = RECUR (TREE_OPERAND (t, 1));
17252 tree r = build_x_binary_op
17253 (input_location, TREE_CODE (t),
17254 op0,
17255 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17256 ? ERROR_MARK
17257 : TREE_CODE (TREE_OPERAND (t, 0))),
17258 op1,
17259 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17260 ? ERROR_MARK
17261 : TREE_CODE (TREE_OPERAND (t, 1))),
17262 /*overload=*/NULL,
17263 complain|decltype_flag);
17264 if (EXPR_P (r) && TREE_NO_WARNING (t))
17265 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17267 RETURN (r);
17270 case POINTER_PLUS_EXPR:
17272 tree op0 = RECUR (TREE_OPERAND (t, 0));
17273 tree op1 = RECUR (TREE_OPERAND (t, 1));
17274 return fold_build_pointer_plus (op0, op1);
17277 case SCOPE_REF:
17278 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17279 /*address_p=*/false));
17280 case ARRAY_REF:
17281 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17282 args, complain, in_decl);
17283 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17284 RECUR (TREE_OPERAND (t, 1)),
17285 complain|decltype_flag));
17287 case ARRAY_NOTATION_REF:
17289 tree start_index, length, stride;
17290 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
17291 args, complain, in_decl);
17292 start_index = RECUR (ARRAY_NOTATION_START (t));
17293 length = RECUR (ARRAY_NOTATION_LENGTH (t));
17294 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
17295 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17296 length, stride, TREE_TYPE (op1)));
17298 case SIZEOF_EXPR:
17299 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17300 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17301 RETURN (tsubst_copy (t, args, complain, in_decl));
17302 /* Fall through */
17304 case ALIGNOF_EXPR:
17306 tree r;
17308 op1 = TREE_OPERAND (t, 0);
17309 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17310 op1 = TREE_TYPE (op1);
17311 if (!args)
17313 /* When there are no ARGS, we are trying to evaluate a
17314 non-dependent expression from the parser. Trying to do
17315 the substitutions may not work. */
17316 if (!TYPE_P (op1))
17317 op1 = TREE_TYPE (op1);
17319 else
17321 ++cp_unevaluated_operand;
17322 ++c_inhibit_evaluation_warnings;
17323 if (TYPE_P (op1))
17324 op1 = tsubst (op1, args, complain, in_decl);
17325 else
17326 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17327 /*function_p=*/false,
17328 /*integral_constant_expression_p=*/
17329 false);
17330 --cp_unevaluated_operand;
17331 --c_inhibit_evaluation_warnings;
17333 if (TYPE_P (op1))
17334 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17335 complain & tf_error);
17336 else
17337 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17338 complain & tf_error);
17339 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17341 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17343 if (!processing_template_decl && TYPE_P (op1))
17345 r = build_min (SIZEOF_EXPR, size_type_node,
17346 build1 (NOP_EXPR, op1, error_mark_node));
17347 SIZEOF_EXPR_TYPE_P (r) = 1;
17349 else
17350 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17351 TREE_SIDE_EFFECTS (r) = 0;
17352 TREE_READONLY (r) = 1;
17354 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17356 RETURN (r);
17359 case AT_ENCODE_EXPR:
17361 op1 = TREE_OPERAND (t, 0);
17362 ++cp_unevaluated_operand;
17363 ++c_inhibit_evaluation_warnings;
17364 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17365 /*function_p=*/false,
17366 /*integral_constant_expression_p=*/false);
17367 --cp_unevaluated_operand;
17368 --c_inhibit_evaluation_warnings;
17369 RETURN (objc_build_encode_expr (op1));
17372 case NOEXCEPT_EXPR:
17373 op1 = TREE_OPERAND (t, 0);
17374 ++cp_unevaluated_operand;
17375 ++c_inhibit_evaluation_warnings;
17376 ++cp_noexcept_operand;
17377 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17378 /*function_p=*/false,
17379 /*integral_constant_expression_p=*/false);
17380 --cp_unevaluated_operand;
17381 --c_inhibit_evaluation_warnings;
17382 --cp_noexcept_operand;
17383 RETURN (finish_noexcept_expr (op1, complain));
17385 case MODOP_EXPR:
17387 warning_sentinel s(warn_div_by_zero);
17388 tree lhs = RECUR (TREE_OPERAND (t, 0));
17389 tree rhs = RECUR (TREE_OPERAND (t, 2));
17390 tree r = build_x_modify_expr
17391 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17392 complain|decltype_flag);
17393 /* TREE_NO_WARNING must be set if either the expression was
17394 parenthesized or it uses an operator such as >>= rather
17395 than plain assignment. In the former case, it was already
17396 set and must be copied. In the latter case,
17397 build_x_modify_expr sets it and it must not be reset
17398 here. */
17399 if (TREE_NO_WARNING (t))
17400 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17402 RETURN (r);
17405 case ARROW_EXPR:
17406 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17407 args, complain, in_decl);
17408 /* Remember that there was a reference to this entity. */
17409 if (DECL_P (op1)
17410 && !mark_used (op1, complain) && !(complain & tf_error))
17411 RETURN (error_mark_node);
17412 RETURN (build_x_arrow (input_location, op1, complain));
17414 case NEW_EXPR:
17416 tree placement = RECUR (TREE_OPERAND (t, 0));
17417 tree init = RECUR (TREE_OPERAND (t, 3));
17418 vec<tree, va_gc> *placement_vec;
17419 vec<tree, va_gc> *init_vec;
17420 tree ret;
17422 if (placement == NULL_TREE)
17423 placement_vec = NULL;
17424 else
17426 placement_vec = make_tree_vector ();
17427 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17428 vec_safe_push (placement_vec, TREE_VALUE (placement));
17431 /* If there was an initializer in the original tree, but it
17432 instantiated to an empty list, then we should pass a
17433 non-NULL empty vector to tell build_new that it was an
17434 empty initializer() rather than no initializer. This can
17435 only happen when the initializer is a pack expansion whose
17436 parameter packs are of length zero. */
17437 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17438 init_vec = NULL;
17439 else
17441 init_vec = make_tree_vector ();
17442 if (init == void_node)
17443 gcc_assert (init_vec != NULL);
17444 else
17446 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17447 vec_safe_push (init_vec, TREE_VALUE (init));
17451 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17452 tree op2 = RECUR (TREE_OPERAND (t, 2));
17453 ret = build_new (&placement_vec, op1, op2, &init_vec,
17454 NEW_EXPR_USE_GLOBAL (t),
17455 complain);
17457 if (placement_vec != NULL)
17458 release_tree_vector (placement_vec);
17459 if (init_vec != NULL)
17460 release_tree_vector (init_vec);
17462 RETURN (ret);
17465 case DELETE_EXPR:
17467 tree op0 = RECUR (TREE_OPERAND (t, 0));
17468 tree op1 = RECUR (TREE_OPERAND (t, 1));
17469 RETURN (delete_sanity (op0, op1,
17470 DELETE_EXPR_USE_VEC (t),
17471 DELETE_EXPR_USE_GLOBAL (t),
17472 complain));
17475 case COMPOUND_EXPR:
17477 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17478 complain & ~tf_decltype, in_decl,
17479 /*function_p=*/false,
17480 integral_constant_expression_p);
17481 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17482 op0,
17483 RECUR (TREE_OPERAND (t, 1)),
17484 complain|decltype_flag));
17487 case CALL_EXPR:
17489 tree function;
17490 vec<tree, va_gc> *call_args;
17491 unsigned int nargs, i;
17492 bool qualified_p;
17493 bool koenig_p;
17494 tree ret;
17496 function = CALL_EXPR_FN (t);
17497 /* Internal function with no arguments. */
17498 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17499 RETURN (t);
17501 /* When we parsed the expression, we determined whether or
17502 not Koenig lookup should be performed. */
17503 koenig_p = KOENIG_LOOKUP_P (t);
17504 if (function == NULL_TREE)
17506 koenig_p = false;
17507 qualified_p = false;
17509 else if (TREE_CODE (function) == SCOPE_REF)
17511 qualified_p = true;
17512 function = tsubst_qualified_id (function, args, complain, in_decl,
17513 /*done=*/false,
17514 /*address_p=*/false);
17516 else if (koenig_p && identifier_p (function))
17518 /* Do nothing; calling tsubst_copy_and_build on an identifier
17519 would incorrectly perform unqualified lookup again.
17521 Note that we can also have an IDENTIFIER_NODE if the earlier
17522 unqualified lookup found a member function; in that case
17523 koenig_p will be false and we do want to do the lookup
17524 again to find the instantiated member function.
17526 FIXME but doing that causes c++/15272, so we need to stop
17527 using IDENTIFIER_NODE in that situation. */
17528 qualified_p = false;
17530 else
17532 if (TREE_CODE (function) == COMPONENT_REF)
17534 tree op = TREE_OPERAND (function, 1);
17536 qualified_p = (TREE_CODE (op) == SCOPE_REF
17537 || (BASELINK_P (op)
17538 && BASELINK_QUALIFIED_P (op)));
17540 else
17541 qualified_p = false;
17543 if (TREE_CODE (function) == ADDR_EXPR
17544 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17545 /* Avoid error about taking the address of a constructor. */
17546 function = TREE_OPERAND (function, 0);
17548 function = tsubst_copy_and_build (function, args, complain,
17549 in_decl,
17550 !qualified_p,
17551 integral_constant_expression_p);
17553 if (BASELINK_P (function))
17554 qualified_p = true;
17557 nargs = call_expr_nargs (t);
17558 call_args = make_tree_vector ();
17559 for (i = 0; i < nargs; ++i)
17561 tree arg = CALL_EXPR_ARG (t, i);
17563 if (!PACK_EXPANSION_P (arg))
17564 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17565 else
17567 /* Expand the pack expansion and push each entry onto
17568 CALL_ARGS. */
17569 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17570 if (TREE_CODE (arg) == TREE_VEC)
17572 unsigned int len, j;
17574 len = TREE_VEC_LENGTH (arg);
17575 for (j = 0; j < len; ++j)
17577 tree value = TREE_VEC_ELT (arg, j);
17578 if (value != NULL_TREE)
17579 value = convert_from_reference (value);
17580 vec_safe_push (call_args, value);
17583 else
17585 /* A partial substitution. Add one entry. */
17586 vec_safe_push (call_args, arg);
17591 /* We do not perform argument-dependent lookup if normal
17592 lookup finds a non-function, in accordance with the
17593 expected resolution of DR 218. */
17594 if (koenig_p
17595 && ((is_overloaded_fn (function)
17596 /* If lookup found a member function, the Koenig lookup is
17597 not appropriate, even if an unqualified-name was used
17598 to denote the function. */
17599 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17600 || identifier_p (function))
17601 /* Only do this when substitution turns a dependent call
17602 into a non-dependent call. */
17603 && type_dependent_expression_p_push (t)
17604 && !any_type_dependent_arguments_p (call_args))
17605 function = perform_koenig_lookup (function, call_args, tf_none);
17607 if (function != NULL_TREE
17608 && identifier_p (function)
17609 && !any_type_dependent_arguments_p (call_args))
17611 if (koenig_p && (complain & tf_warning_or_error))
17613 /* For backwards compatibility and good diagnostics, try
17614 the unqualified lookup again if we aren't in SFINAE
17615 context. */
17616 tree unq = (tsubst_copy_and_build
17617 (function, args, complain, in_decl, true,
17618 integral_constant_expression_p));
17619 if (unq == error_mark_node)
17621 release_tree_vector (call_args);
17622 RETURN (error_mark_node);
17625 if (unq != function)
17627 /* In a lambda fn, we have to be careful to not
17628 introduce new this captures. Legacy code can't
17629 be using lambdas anyway, so it's ok to be
17630 stricter. */
17631 bool in_lambda = (current_class_type
17632 && LAMBDA_TYPE_P (current_class_type));
17633 char const *const msg
17634 = G_("%qD was not declared in this scope, "
17635 "and no declarations were found by "
17636 "argument-dependent lookup at the point "
17637 "of instantiation");
17639 bool diag = true;
17640 if (in_lambda)
17641 error_at (EXPR_LOC_OR_LOC (t, input_location),
17642 msg, function);
17643 else
17644 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17645 msg, function);
17646 if (diag)
17648 tree fn = unq;
17650 if (INDIRECT_REF_P (fn))
17651 fn = TREE_OPERAND (fn, 0);
17652 if (is_overloaded_fn (fn))
17653 fn = get_first_fn (fn);
17655 if (!DECL_P (fn))
17656 /* Can't say anything more. */;
17657 else if (DECL_CLASS_SCOPE_P (fn))
17659 location_t loc = EXPR_LOC_OR_LOC (t,
17660 input_location);
17661 inform (loc,
17662 "declarations in dependent base %qT are "
17663 "not found by unqualified lookup",
17664 DECL_CLASS_CONTEXT (fn));
17665 if (current_class_ptr)
17666 inform (loc,
17667 "use %<this->%D%> instead", function);
17668 else
17669 inform (loc,
17670 "use %<%T::%D%> instead",
17671 current_class_name, function);
17673 else
17674 inform (DECL_SOURCE_LOCATION (fn),
17675 "%qD declared here, later in the "
17676 "translation unit", fn);
17677 if (in_lambda)
17679 release_tree_vector (call_args);
17680 RETURN (error_mark_node);
17684 function = unq;
17687 if (identifier_p (function))
17689 if (complain & tf_error)
17690 unqualified_name_lookup_error (function);
17691 release_tree_vector (call_args);
17692 RETURN (error_mark_node);
17696 /* Remember that there was a reference to this entity. */
17697 if (function != NULL_TREE
17698 && DECL_P (function)
17699 && !mark_used (function, complain) && !(complain & tf_error))
17701 release_tree_vector (call_args);
17702 RETURN (error_mark_node);
17705 /* Put back tf_decltype for the actual call. */
17706 complain |= decltype_flag;
17708 if (function == NULL_TREE)
17709 switch (CALL_EXPR_IFN (t))
17711 case IFN_LAUNDER:
17712 gcc_assert (nargs == 1);
17713 if (vec_safe_length (call_args) != 1)
17715 error_at (EXPR_LOC_OR_LOC (t, input_location),
17716 "wrong number of arguments to "
17717 "%<__builtin_launder%>");
17718 ret = error_mark_node;
17720 else
17721 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17722 input_location),
17723 (*call_args)[0], complain);
17724 break;
17726 default:
17727 /* Unsupported internal function with arguments. */
17728 gcc_unreachable ();
17730 else if (TREE_CODE (function) == OFFSET_REF)
17731 ret = build_offset_ref_call_from_tree (function, &call_args,
17732 complain);
17733 else if (TREE_CODE (function) == COMPONENT_REF)
17735 tree instance = TREE_OPERAND (function, 0);
17736 tree fn = TREE_OPERAND (function, 1);
17738 if (processing_template_decl
17739 && (type_dependent_expression_p (instance)
17740 || (!BASELINK_P (fn)
17741 && TREE_CODE (fn) != FIELD_DECL)
17742 || type_dependent_expression_p (fn)
17743 || any_type_dependent_arguments_p (call_args)))
17744 ret = build_min_nt_call_vec (function, call_args);
17745 else if (!BASELINK_P (fn))
17746 ret = finish_call_expr (function, &call_args,
17747 /*disallow_virtual=*/false,
17748 /*koenig_p=*/false,
17749 complain);
17750 else
17751 ret = (build_new_method_call
17752 (instance, fn,
17753 &call_args, NULL_TREE,
17754 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17755 /*fn_p=*/NULL,
17756 complain));
17758 else
17759 ret = finish_call_expr (function, &call_args,
17760 /*disallow_virtual=*/qualified_p,
17761 koenig_p,
17762 complain);
17764 release_tree_vector (call_args);
17766 if (ret != error_mark_node)
17768 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17769 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17770 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17771 bool thk = CALL_FROM_THUNK_P (t);
17772 if (op || ord || rev || thk)
17774 function = extract_call_expr (ret);
17775 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17776 CALL_EXPR_ORDERED_ARGS (function) = ord;
17777 CALL_EXPR_REVERSE_ARGS (function) = rev;
17778 if (thk)
17780 CALL_FROM_THUNK_P (function) = true;
17781 /* The thunk location is not interesting. */
17782 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17787 RETURN (ret);
17790 case COND_EXPR:
17792 tree cond = RECUR (TREE_OPERAND (t, 0));
17793 tree folded_cond = fold_non_dependent_expr (cond);
17794 tree exp1, exp2;
17796 if (TREE_CODE (folded_cond) == INTEGER_CST)
17798 if (integer_zerop (folded_cond))
17800 ++c_inhibit_evaluation_warnings;
17801 exp1 = RECUR (TREE_OPERAND (t, 1));
17802 --c_inhibit_evaluation_warnings;
17803 exp2 = RECUR (TREE_OPERAND (t, 2));
17805 else
17807 exp1 = RECUR (TREE_OPERAND (t, 1));
17808 ++c_inhibit_evaluation_warnings;
17809 exp2 = RECUR (TREE_OPERAND (t, 2));
17810 --c_inhibit_evaluation_warnings;
17812 cond = folded_cond;
17814 else
17816 exp1 = RECUR (TREE_OPERAND (t, 1));
17817 exp2 = RECUR (TREE_OPERAND (t, 2));
17820 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17821 cond, exp1, exp2, complain));
17824 case PSEUDO_DTOR_EXPR:
17826 tree op0 = RECUR (TREE_OPERAND (t, 0));
17827 tree op1 = RECUR (TREE_OPERAND (t, 1));
17828 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17829 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17830 input_location));
17833 case TREE_LIST:
17835 tree purpose, value, chain;
17837 if (t == void_list_node)
17838 RETURN (t);
17840 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17841 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17843 /* We have pack expansions, so expand those and
17844 create a new list out of it. */
17845 tree purposevec = NULL_TREE;
17846 tree valuevec = NULL_TREE;
17847 tree chain;
17848 int i, len = -1;
17850 /* Expand the argument expressions. */
17851 if (TREE_PURPOSE (t))
17852 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17853 complain, in_decl);
17854 if (TREE_VALUE (t))
17855 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17856 complain, in_decl);
17858 /* Build the rest of the list. */
17859 chain = TREE_CHAIN (t);
17860 if (chain && chain != void_type_node)
17861 chain = RECUR (chain);
17863 /* Determine the number of arguments. */
17864 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17866 len = TREE_VEC_LENGTH (purposevec);
17867 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17869 else if (TREE_CODE (valuevec) == TREE_VEC)
17870 len = TREE_VEC_LENGTH (valuevec);
17871 else
17873 /* Since we only performed a partial substitution into
17874 the argument pack, we only RETURN (a single list
17875 node. */
17876 if (purposevec == TREE_PURPOSE (t)
17877 && valuevec == TREE_VALUE (t)
17878 && chain == TREE_CHAIN (t))
17879 RETURN (t);
17881 RETURN (tree_cons (purposevec, valuevec, chain));
17884 /* Convert the argument vectors into a TREE_LIST */
17885 i = len;
17886 while (i > 0)
17888 /* Grab the Ith values. */
17889 i--;
17890 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17891 : NULL_TREE;
17892 value
17893 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17894 : NULL_TREE;
17896 /* Build the list (backwards). */
17897 chain = tree_cons (purpose, value, chain);
17900 RETURN (chain);
17903 purpose = TREE_PURPOSE (t);
17904 if (purpose)
17905 purpose = RECUR (purpose);
17906 value = TREE_VALUE (t);
17907 if (value)
17908 value = RECUR (value);
17909 chain = TREE_CHAIN (t);
17910 if (chain && chain != void_type_node)
17911 chain = RECUR (chain);
17912 if (purpose == TREE_PURPOSE (t)
17913 && value == TREE_VALUE (t)
17914 && chain == TREE_CHAIN (t))
17915 RETURN (t);
17916 RETURN (tree_cons (purpose, value, chain));
17919 case COMPONENT_REF:
17921 tree object;
17922 tree object_type;
17923 tree member;
17924 tree r;
17926 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17927 args, complain, in_decl);
17928 /* Remember that there was a reference to this entity. */
17929 if (DECL_P (object)
17930 && !mark_used (object, complain) && !(complain & tf_error))
17931 RETURN (error_mark_node);
17932 object_type = TREE_TYPE (object);
17934 member = TREE_OPERAND (t, 1);
17935 if (BASELINK_P (member))
17936 member = tsubst_baselink (member,
17937 non_reference (TREE_TYPE (object)),
17938 args, complain, in_decl);
17939 else
17940 member = tsubst_copy (member, args, complain, in_decl);
17941 if (member == error_mark_node)
17942 RETURN (error_mark_node);
17944 if (TREE_CODE (member) == FIELD_DECL)
17946 r = finish_non_static_data_member (member, object, NULL_TREE);
17947 if (TREE_CODE (r) == COMPONENT_REF)
17948 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17949 RETURN (r);
17951 else if (type_dependent_expression_p (object))
17952 /* We can't do much here. */;
17953 else if (!CLASS_TYPE_P (object_type))
17955 if (scalarish_type_p (object_type))
17957 tree s = NULL_TREE;
17958 tree dtor = member;
17960 if (TREE_CODE (dtor) == SCOPE_REF)
17962 s = TREE_OPERAND (dtor, 0);
17963 dtor = TREE_OPERAND (dtor, 1);
17965 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17967 dtor = TREE_OPERAND (dtor, 0);
17968 if (TYPE_P (dtor))
17969 RETURN (finish_pseudo_destructor_expr
17970 (object, s, dtor, input_location));
17974 else if (TREE_CODE (member) == SCOPE_REF
17975 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17977 /* Lookup the template functions now that we know what the
17978 scope is. */
17979 tree scope = TREE_OPERAND (member, 0);
17980 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17981 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17982 member = lookup_qualified_name (scope, tmpl,
17983 /*is_type_p=*/false,
17984 /*complain=*/false);
17985 if (BASELINK_P (member))
17987 BASELINK_FUNCTIONS (member)
17988 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17989 args);
17990 member = (adjust_result_of_qualified_name_lookup
17991 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17992 object_type));
17994 else
17996 qualified_name_lookup_error (scope, tmpl, member,
17997 input_location);
17998 RETURN (error_mark_node);
18001 else if (TREE_CODE (member) == SCOPE_REF
18002 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18003 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18005 if (complain & tf_error)
18007 if (TYPE_P (TREE_OPERAND (member, 0)))
18008 error ("%qT is not a class or namespace",
18009 TREE_OPERAND (member, 0));
18010 else
18011 error ("%qD is not a class or namespace",
18012 TREE_OPERAND (member, 0));
18014 RETURN (error_mark_node);
18017 r = finish_class_member_access_expr (object, member,
18018 /*template_p=*/false,
18019 complain);
18020 if (TREE_CODE (r) == COMPONENT_REF)
18021 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18022 RETURN (r);
18025 case THROW_EXPR:
18026 RETURN (build_throw
18027 (RECUR (TREE_OPERAND (t, 0))));
18029 case CONSTRUCTOR:
18031 vec<constructor_elt, va_gc> *n;
18032 constructor_elt *ce;
18033 unsigned HOST_WIDE_INT idx;
18034 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18035 bool process_index_p;
18036 int newlen;
18037 bool need_copy_p = false;
18038 tree r;
18040 if (type == error_mark_node)
18041 RETURN (error_mark_node);
18043 /* digest_init will do the wrong thing if we let it. */
18044 if (type && TYPE_PTRMEMFUNC_P (type))
18045 RETURN (t);
18047 /* We do not want to process the index of aggregate
18048 initializers as they are identifier nodes which will be
18049 looked up by digest_init. */
18050 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18052 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18053 newlen = vec_safe_length (n);
18054 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18056 if (ce->index && process_index_p
18057 /* An identifier index is looked up in the type
18058 being initialized, not the current scope. */
18059 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18060 ce->index = RECUR (ce->index);
18062 if (PACK_EXPANSION_P (ce->value))
18064 /* Substitute into the pack expansion. */
18065 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18066 in_decl);
18068 if (ce->value == error_mark_node
18069 || PACK_EXPANSION_P (ce->value))
18071 else if (TREE_VEC_LENGTH (ce->value) == 1)
18072 /* Just move the argument into place. */
18073 ce->value = TREE_VEC_ELT (ce->value, 0);
18074 else
18076 /* Update the length of the final CONSTRUCTOR
18077 arguments vector, and note that we will need to
18078 copy.*/
18079 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18080 need_copy_p = true;
18083 else
18084 ce->value = RECUR (ce->value);
18087 if (need_copy_p)
18089 vec<constructor_elt, va_gc> *old_n = n;
18091 vec_alloc (n, newlen);
18092 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18094 if (TREE_CODE (ce->value) == TREE_VEC)
18096 int i, len = TREE_VEC_LENGTH (ce->value);
18097 for (i = 0; i < len; ++i)
18098 CONSTRUCTOR_APPEND_ELT (n, 0,
18099 TREE_VEC_ELT (ce->value, i));
18101 else
18102 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18106 r = build_constructor (init_list_type_node, n);
18107 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18109 if (TREE_HAS_CONSTRUCTOR (t))
18111 fcl_t cl = fcl_functional;
18112 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18113 cl = fcl_c99;
18114 RETURN (finish_compound_literal (type, r, complain, cl));
18117 TREE_TYPE (r) = type;
18118 RETURN (r);
18121 case TYPEID_EXPR:
18123 tree operand_0 = TREE_OPERAND (t, 0);
18124 if (TYPE_P (operand_0))
18126 operand_0 = tsubst (operand_0, args, complain, in_decl);
18127 RETURN (get_typeid (operand_0, complain));
18129 else
18131 operand_0 = RECUR (operand_0);
18132 RETURN (build_typeid (operand_0, complain));
18136 case VAR_DECL:
18137 if (!args)
18138 RETURN (t);
18139 else if (DECL_PACK_P (t))
18141 /* We don't build decls for an instantiation of a
18142 variadic capture proxy, we instantiate the elements
18143 when needed. */
18144 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18145 return RECUR (DECL_VALUE_EXPR (t));
18147 /* Fall through */
18149 case PARM_DECL:
18151 tree r = tsubst_copy (t, args, complain, in_decl);
18152 /* ??? We're doing a subset of finish_id_expression here. */
18153 if (VAR_P (r)
18154 && !processing_template_decl
18155 && !cp_unevaluated_operand
18156 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18157 && CP_DECL_THREAD_LOCAL_P (r))
18159 if (tree wrap = get_tls_wrapper_fn (r))
18160 /* Replace an evaluated use of the thread_local variable with
18161 a call to its wrapper. */
18162 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18164 else if (outer_automatic_var_p (r))
18165 r = process_outer_var_ref (r, complain);
18167 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18168 /* If the original type was a reference, we'll be wrapped in
18169 the appropriate INDIRECT_REF. */
18170 r = convert_from_reference (r);
18171 RETURN (r);
18174 case VA_ARG_EXPR:
18176 tree op0 = RECUR (TREE_OPERAND (t, 0));
18177 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18178 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18181 case OFFSETOF_EXPR:
18183 tree object_ptr
18184 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18185 in_decl, /*function_p=*/false,
18186 /*integral_constant_expression_p=*/false);
18187 RETURN (finish_offsetof (object_ptr,
18188 RECUR (TREE_OPERAND (t, 0)),
18189 EXPR_LOCATION (t)));
18192 case ADDRESSOF_EXPR:
18193 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18194 RECUR (TREE_OPERAND (t, 0)), complain));
18196 case TRAIT_EXPR:
18198 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18199 complain, in_decl);
18201 tree type2 = TRAIT_EXPR_TYPE2 (t);
18202 if (type2 && TREE_CODE (type2) == TREE_LIST)
18203 type2 = RECUR (type2);
18204 else if (type2)
18205 type2 = tsubst (type2, args, complain, in_decl);
18207 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18210 case STMT_EXPR:
18212 tree old_stmt_expr = cur_stmt_expr;
18213 tree stmt_expr = begin_stmt_expr ();
18215 cur_stmt_expr = stmt_expr;
18216 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18217 integral_constant_expression_p);
18218 stmt_expr = finish_stmt_expr (stmt_expr, false);
18219 cur_stmt_expr = old_stmt_expr;
18221 /* If the resulting list of expression statement is empty,
18222 fold it further into void_node. */
18223 if (empty_expr_stmt_p (stmt_expr))
18224 stmt_expr = void_node;
18226 RETURN (stmt_expr);
18229 case LAMBDA_EXPR:
18231 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18233 RETURN (build_lambda_object (r));
18236 case TARGET_EXPR:
18237 /* We can get here for a constant initializer of non-dependent type.
18238 FIXME stop folding in cp_parser_initializer_clause. */
18240 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18241 complain);
18242 RETURN (r);
18245 case TRANSACTION_EXPR:
18246 RETURN (tsubst_expr(t, args, complain, in_decl,
18247 integral_constant_expression_p));
18249 case PAREN_EXPR:
18250 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18252 case VEC_PERM_EXPR:
18254 tree op0 = RECUR (TREE_OPERAND (t, 0));
18255 tree op1 = RECUR (TREE_OPERAND (t, 1));
18256 tree op2 = RECUR (TREE_OPERAND (t, 2));
18257 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18258 complain));
18261 case REQUIRES_EXPR:
18262 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18264 default:
18265 /* Handle Objective-C++ constructs, if appropriate. */
18267 tree subst
18268 = objcp_tsubst_copy_and_build (t, args, complain,
18269 in_decl, /*function_p=*/false);
18270 if (subst)
18271 RETURN (subst);
18273 RETURN (tsubst_copy (t, args, complain, in_decl));
18276 #undef RECUR
18277 #undef RETURN
18278 out:
18279 input_location = loc;
18280 return retval;
18283 /* Verify that the instantiated ARGS are valid. For type arguments,
18284 make sure that the type's linkage is ok. For non-type arguments,
18285 make sure they are constants if they are integral or enumerations.
18286 Emit an error under control of COMPLAIN, and return TRUE on error. */
18288 static bool
18289 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18291 if (dependent_template_arg_p (t))
18292 return false;
18293 if (ARGUMENT_PACK_P (t))
18295 tree vec = ARGUMENT_PACK_ARGS (t);
18296 int len = TREE_VEC_LENGTH (vec);
18297 bool result = false;
18298 int i;
18300 for (i = 0; i < len; ++i)
18301 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18302 result = true;
18303 return result;
18305 else if (TYPE_P (t))
18307 /* [basic.link]: A name with no linkage (notably, the name
18308 of a class or enumeration declared in a local scope)
18309 shall not be used to declare an entity with linkage.
18310 This implies that names with no linkage cannot be used as
18311 template arguments
18313 DR 757 relaxes this restriction for C++0x. */
18314 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18315 : no_linkage_check (t, /*relaxed_p=*/false));
18317 if (nt)
18319 /* DR 488 makes use of a type with no linkage cause
18320 type deduction to fail. */
18321 if (complain & tf_error)
18323 if (TYPE_UNNAMED_P (nt))
18324 error ("%qT is/uses unnamed type", t);
18325 else
18326 error ("template argument for %qD uses local type %qT",
18327 tmpl, t);
18329 return true;
18331 /* In order to avoid all sorts of complications, we do not
18332 allow variably-modified types as template arguments. */
18333 else if (variably_modified_type_p (t, NULL_TREE))
18335 if (complain & tf_error)
18336 error ("%qT is a variably modified type", t);
18337 return true;
18340 /* Class template and alias template arguments should be OK. */
18341 else if (DECL_TYPE_TEMPLATE_P (t))
18343 /* A non-type argument of integral or enumerated type must be a
18344 constant. */
18345 else if (TREE_TYPE (t)
18346 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18347 && !REFERENCE_REF_P (t)
18348 && !TREE_CONSTANT (t))
18350 if (complain & tf_error)
18351 error ("integral expression %qE is not constant", t);
18352 return true;
18354 return false;
18357 static bool
18358 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18360 int ix, len = DECL_NTPARMS (tmpl);
18361 bool result = false;
18363 for (ix = 0; ix != len; ix++)
18365 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18366 result = true;
18368 if (result && (complain & tf_error))
18369 error (" trying to instantiate %qD", tmpl);
18370 return result;
18373 /* We're out of SFINAE context now, so generate diagnostics for the access
18374 errors we saw earlier when instantiating D from TMPL and ARGS. */
18376 static void
18377 recheck_decl_substitution (tree d, tree tmpl, tree args)
18379 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18380 tree type = TREE_TYPE (pattern);
18381 location_t loc = input_location;
18383 push_access_scope (d);
18384 push_deferring_access_checks (dk_no_deferred);
18385 input_location = DECL_SOURCE_LOCATION (pattern);
18386 tsubst (type, args, tf_warning_or_error, d);
18387 input_location = loc;
18388 pop_deferring_access_checks ();
18389 pop_access_scope (d);
18392 /* Instantiate the indicated variable, function, or alias template TMPL with
18393 the template arguments in TARG_PTR. */
18395 static tree
18396 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18398 tree targ_ptr = orig_args;
18399 tree fndecl;
18400 tree gen_tmpl;
18401 tree spec;
18402 bool access_ok = true;
18404 if (tmpl == error_mark_node)
18405 return error_mark_node;
18407 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18409 /* If this function is a clone, handle it specially. */
18410 if (DECL_CLONED_FUNCTION_P (tmpl))
18412 tree spec;
18413 tree clone;
18415 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18416 DECL_CLONED_FUNCTION. */
18417 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18418 targ_ptr, complain);
18419 if (spec == error_mark_node)
18420 return error_mark_node;
18422 /* Look for the clone. */
18423 FOR_EACH_CLONE (clone, spec)
18424 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18425 return clone;
18426 /* We should always have found the clone by now. */
18427 gcc_unreachable ();
18428 return NULL_TREE;
18431 if (targ_ptr == error_mark_node)
18432 return error_mark_node;
18434 /* Check to see if we already have this specialization. */
18435 gen_tmpl = most_general_template (tmpl);
18436 if (TMPL_ARGS_DEPTH (targ_ptr)
18437 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18438 /* targ_ptr only has the innermost template args, so add the outer ones
18439 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18440 the case of a non-dependent call within a template definition). */
18441 targ_ptr = (add_outermost_template_args
18442 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18443 targ_ptr));
18445 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18446 but it doesn't seem to be on the hot path. */
18447 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18449 gcc_assert (tmpl == gen_tmpl
18450 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18451 == spec)
18452 || fndecl == NULL_TREE);
18454 if (spec != NULL_TREE)
18456 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18458 if (complain & tf_error)
18459 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18460 return error_mark_node;
18462 return spec;
18465 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18466 complain))
18467 return error_mark_node;
18469 /* We are building a FUNCTION_DECL, during which the access of its
18470 parameters and return types have to be checked. However this
18471 FUNCTION_DECL which is the desired context for access checking
18472 is not built yet. We solve this chicken-and-egg problem by
18473 deferring all checks until we have the FUNCTION_DECL. */
18474 push_deferring_access_checks (dk_deferred);
18476 /* Instantiation of the function happens in the context of the function
18477 template, not the context of the overload resolution we're doing. */
18478 push_to_top_level ();
18479 /* If there are dependent arguments, e.g. because we're doing partial
18480 ordering, make sure processing_template_decl stays set. */
18481 if (uses_template_parms (targ_ptr))
18482 ++processing_template_decl;
18483 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18485 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18486 complain, gen_tmpl, true);
18487 push_nested_class (ctx);
18490 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18492 fndecl = NULL_TREE;
18493 if (VAR_P (pattern))
18495 /* We need to determine if we're using a partial or explicit
18496 specialization now, because the type of the variable could be
18497 different. */
18498 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18499 tree elt = most_specialized_partial_spec (tid, complain);
18500 if (elt == error_mark_node)
18501 pattern = error_mark_node;
18502 else if (elt)
18504 tree partial_tmpl = TREE_VALUE (elt);
18505 tree partial_args = TREE_PURPOSE (elt);
18506 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18507 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18511 /* Substitute template parameters to obtain the specialization. */
18512 if (fndecl == NULL_TREE)
18513 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18514 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18515 pop_nested_class ();
18516 pop_from_top_level ();
18518 if (fndecl == error_mark_node)
18520 pop_deferring_access_checks ();
18521 return error_mark_node;
18524 /* The DECL_TI_TEMPLATE should always be the immediate parent
18525 template, not the most general template. */
18526 DECL_TI_TEMPLATE (fndecl) = tmpl;
18527 DECL_TI_ARGS (fndecl) = targ_ptr;
18529 /* Now we know the specialization, compute access previously
18530 deferred. Do no access control for inheriting constructors,
18531 as we already checked access for the inherited constructor. */
18532 if (!(flag_new_inheriting_ctors
18533 && DECL_INHERITED_CTOR (fndecl)))
18535 push_access_scope (fndecl);
18536 if (!perform_deferred_access_checks (complain))
18537 access_ok = false;
18538 pop_access_scope (fndecl);
18540 pop_deferring_access_checks ();
18542 /* If we've just instantiated the main entry point for a function,
18543 instantiate all the alternate entry points as well. We do this
18544 by cloning the instantiation of the main entry point, not by
18545 instantiating the template clones. */
18546 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18547 clone_function_decl (fndecl, /*update_methods=*/false);
18549 if (!access_ok)
18551 if (!(complain & tf_error))
18553 /* Remember to reinstantiate when we're out of SFINAE so the user
18554 can see the errors. */
18555 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18557 return error_mark_node;
18559 return fndecl;
18562 /* Wrapper for instantiate_template_1. */
18564 tree
18565 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18567 tree ret;
18568 timevar_push (TV_TEMPLATE_INST);
18569 ret = instantiate_template_1 (tmpl, orig_args, complain);
18570 timevar_pop (TV_TEMPLATE_INST);
18571 return ret;
18574 /* Instantiate the alias template TMPL with ARGS. Also push a template
18575 instantiation level, which instantiate_template doesn't do because
18576 functions and variables have sufficient context established by the
18577 callers. */
18579 static tree
18580 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18582 struct pending_template *old_last_pend = last_pending_template;
18583 struct tinst_level *old_error_tinst = last_error_tinst_level;
18584 if (tmpl == error_mark_node || args == error_mark_node)
18585 return error_mark_node;
18586 tree tinst = build_tree_list (tmpl, args);
18587 if (!push_tinst_level (tinst))
18589 ggc_free (tinst);
18590 return error_mark_node;
18593 args =
18594 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18595 args, tmpl, complain,
18596 /*require_all_args=*/true,
18597 /*use_default_args=*/true);
18599 tree r = instantiate_template (tmpl, args, complain);
18600 pop_tinst_level ();
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 /* PARM is a template parameter pack for FN. Returns true iff
18611 PARM is used in a deducible way in the argument list of FN. */
18613 static bool
18614 pack_deducible_p (tree parm, tree fn)
18616 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18617 for (; t; t = TREE_CHAIN (t))
18619 tree type = TREE_VALUE (t);
18620 tree packs;
18621 if (!PACK_EXPANSION_P (type))
18622 continue;
18623 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18624 packs; packs = TREE_CHAIN (packs))
18625 if (template_args_equal (TREE_VALUE (packs), parm))
18627 /* The template parameter pack is used in a function parameter
18628 pack. If this is the end of the parameter list, the
18629 template parameter pack is deducible. */
18630 if (TREE_CHAIN (t) == void_list_node)
18631 return true;
18632 else
18633 /* Otherwise, not. Well, it could be deduced from
18634 a non-pack parameter, but doing so would end up with
18635 a deduction mismatch, so don't bother. */
18636 return false;
18639 /* The template parameter pack isn't used in any function parameter
18640 packs, but it might be used deeper, e.g. tuple<Args...>. */
18641 return true;
18644 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18645 NARGS elements of the arguments that are being used when calling
18646 it. TARGS is a vector into which the deduced template arguments
18647 are placed.
18649 Returns either a FUNCTION_DECL for the matching specialization of FN or
18650 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18651 true, diagnostics will be printed to explain why it failed.
18653 If FN is a conversion operator, or we are trying to produce a specific
18654 specialization, RETURN_TYPE is the return type desired.
18656 The EXPLICIT_TARGS are explicit template arguments provided via a
18657 template-id.
18659 The parameter STRICT is one of:
18661 DEDUCE_CALL:
18662 We are deducing arguments for a function call, as in
18663 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18664 deducing arguments for a call to the result of a conversion
18665 function template, as in [over.call.object].
18667 DEDUCE_CONV:
18668 We are deducing arguments for a conversion function, as in
18669 [temp.deduct.conv].
18671 DEDUCE_EXACT:
18672 We are deducing arguments when doing an explicit instantiation
18673 as in [temp.explicit], when determining an explicit specialization
18674 as in [temp.expl.spec], or when taking the address of a function
18675 template, as in [temp.deduct.funcaddr]. */
18677 tree
18678 fn_type_unification (tree fn,
18679 tree explicit_targs,
18680 tree targs,
18681 const tree *args,
18682 unsigned int nargs,
18683 tree return_type,
18684 unification_kind_t strict,
18685 int flags,
18686 bool explain_p,
18687 bool decltype_p)
18689 tree parms;
18690 tree fntype;
18691 tree decl = NULL_TREE;
18692 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18693 bool ok;
18694 static int deduction_depth;
18695 struct pending_template *old_last_pend = last_pending_template;
18696 struct tinst_level *old_error_tinst = last_error_tinst_level;
18698 tree orig_fn = fn;
18699 if (flag_new_inheriting_ctors)
18700 fn = strip_inheriting_ctors (fn);
18702 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18703 tree tinst;
18704 tree r = error_mark_node;
18706 tree full_targs = targs;
18707 if (TMPL_ARGS_DEPTH (targs)
18708 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18709 full_targs = (add_outermost_template_args
18710 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18711 targs));
18713 if (decltype_p)
18714 complain |= tf_decltype;
18716 /* In C++0x, it's possible to have a function template whose type depends
18717 on itself recursively. This is most obvious with decltype, but can also
18718 occur with enumeration scope (c++/48969). So we need to catch infinite
18719 recursion and reject the substitution at deduction time; this function
18720 will return error_mark_node for any repeated substitution.
18722 This also catches excessive recursion such as when f<N> depends on
18723 f<N-1> across all integers, and returns error_mark_node for all the
18724 substitutions back up to the initial one.
18726 This is, of course, not reentrant. */
18727 if (excessive_deduction_depth)
18728 return error_mark_node;
18729 tinst = build_tree_list (fn, NULL_TREE);
18730 ++deduction_depth;
18732 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18734 fntype = TREE_TYPE (fn);
18735 if (explicit_targs)
18737 /* [temp.deduct]
18739 The specified template arguments must match the template
18740 parameters in kind (i.e., type, nontype, template), and there
18741 must not be more arguments than there are parameters;
18742 otherwise type deduction fails.
18744 Nontype arguments must match the types of the corresponding
18745 nontype template parameters, or must be convertible to the
18746 types of the corresponding nontype parameters as specified in
18747 _temp.arg.nontype_, otherwise type deduction fails.
18749 All references in the function type of the function template
18750 to the corresponding template parameters are replaced by the
18751 specified template argument values. If a substitution in a
18752 template parameter or in the function type of the function
18753 template results in an invalid type, type deduction fails. */
18754 int i, len = TREE_VEC_LENGTH (tparms);
18755 location_t loc = input_location;
18756 bool incomplete = false;
18758 if (explicit_targs == error_mark_node)
18759 goto fail;
18761 if (TMPL_ARGS_DEPTH (explicit_targs)
18762 < TMPL_ARGS_DEPTH (full_targs))
18763 explicit_targs = add_outermost_template_args (full_targs,
18764 explicit_targs);
18766 /* Adjust any explicit template arguments before entering the
18767 substitution context. */
18768 explicit_targs
18769 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18770 complain,
18771 /*require_all_args=*/false,
18772 /*use_default_args=*/false));
18773 if (explicit_targs == error_mark_node)
18774 goto fail;
18776 /* Substitute the explicit args into the function type. This is
18777 necessary so that, for instance, explicitly declared function
18778 arguments can match null pointed constants. If we were given
18779 an incomplete set of explicit args, we must not do semantic
18780 processing during substitution as we could create partial
18781 instantiations. */
18782 for (i = 0; i < len; i++)
18784 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18785 bool parameter_pack = false;
18786 tree targ = TREE_VEC_ELT (explicit_targs, i);
18788 /* Dig out the actual parm. */
18789 if (TREE_CODE (parm) == TYPE_DECL
18790 || TREE_CODE (parm) == TEMPLATE_DECL)
18792 parm = TREE_TYPE (parm);
18793 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18795 else if (TREE_CODE (parm) == PARM_DECL)
18797 parm = DECL_INITIAL (parm);
18798 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18801 if (!parameter_pack && targ == NULL_TREE)
18802 /* No explicit argument for this template parameter. */
18803 incomplete = true;
18805 if (parameter_pack && pack_deducible_p (parm, fn))
18807 /* Mark the argument pack as "incomplete". We could
18808 still deduce more arguments during unification.
18809 We remove this mark in type_unification_real. */
18810 if (targ)
18812 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18813 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18814 = ARGUMENT_PACK_ARGS (targ);
18817 /* We have some incomplete argument packs. */
18818 incomplete = true;
18822 TREE_VALUE (tinst) = explicit_targs;
18823 if (!push_tinst_level (tinst))
18825 excessive_deduction_depth = true;
18826 goto fail;
18828 processing_template_decl += incomplete;
18829 input_location = DECL_SOURCE_LOCATION (fn);
18830 /* Ignore any access checks; we'll see them again in
18831 instantiate_template and they might have the wrong
18832 access path at this point. */
18833 push_deferring_access_checks (dk_deferred);
18834 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18835 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18836 pop_deferring_access_checks ();
18837 input_location = loc;
18838 processing_template_decl -= incomplete;
18839 pop_tinst_level ();
18841 if (fntype == error_mark_node)
18842 goto fail;
18844 /* Place the explicitly specified arguments in TARGS. */
18845 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18846 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18847 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18850 /* Never do unification on the 'this' parameter. */
18851 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18853 if (return_type && strict == DEDUCE_CALL)
18855 /* We're deducing for a call to the result of a template conversion
18856 function. The parms we really want are in return_type. */
18857 if (POINTER_TYPE_P (return_type))
18858 return_type = TREE_TYPE (return_type);
18859 parms = TYPE_ARG_TYPES (return_type);
18861 else if (return_type)
18863 tree *new_args;
18865 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18866 new_args = XALLOCAVEC (tree, nargs + 1);
18867 new_args[0] = return_type;
18868 memcpy (new_args + 1, args, nargs * sizeof (tree));
18869 args = new_args;
18870 ++nargs;
18873 /* We allow incomplete unification without an error message here
18874 because the standard doesn't seem to explicitly prohibit it. Our
18875 callers must be ready to deal with unification failures in any
18876 event. */
18878 TREE_VALUE (tinst) = targs;
18879 /* If we aren't explaining yet, push tinst context so we can see where
18880 any errors (e.g. from class instantiations triggered by instantiation
18881 of default template arguments) come from. If we are explaining, this
18882 context is redundant. */
18883 if (!explain_p && !push_tinst_level (tinst))
18885 excessive_deduction_depth = true;
18886 goto fail;
18889 /* type_unification_real will pass back any access checks from default
18890 template argument substitution. */
18891 vec<deferred_access_check, va_gc> *checks;
18892 checks = NULL;
18894 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18895 full_targs, parms, args, nargs, /*subr=*/0,
18896 strict, flags, &checks, explain_p);
18897 if (!explain_p)
18898 pop_tinst_level ();
18899 if (!ok)
18900 goto fail;
18902 /* Now that we have bindings for all of the template arguments,
18903 ensure that the arguments deduced for the template template
18904 parameters have compatible template parameter lists. We cannot
18905 check this property before we have deduced all template
18906 arguments, because the template parameter types of a template
18907 template parameter might depend on prior template parameters
18908 deduced after the template template parameter. The following
18909 ill-formed example illustrates this issue:
18911 template<typename T, template<T> class C> void f(C<5>, T);
18913 template<int N> struct X {};
18915 void g() {
18916 f(X<5>(), 5l); // error: template argument deduction fails
18919 The template parameter list of 'C' depends on the template type
18920 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18921 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18922 time that we deduce 'C'. */
18923 if (!template_template_parm_bindings_ok_p
18924 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18926 unify_inconsistent_template_template_parameters (explain_p);
18927 goto fail;
18930 /* All is well so far. Now, check:
18932 [temp.deduct]
18934 When all template arguments have been deduced, all uses of
18935 template parameters in nondeduced contexts are replaced with
18936 the corresponding deduced argument values. If the
18937 substitution results in an invalid type, as described above,
18938 type deduction fails. */
18939 TREE_VALUE (tinst) = targs;
18940 if (!push_tinst_level (tinst))
18942 excessive_deduction_depth = true;
18943 goto fail;
18946 /* Also collect access checks from the instantiation. */
18947 reopen_deferring_access_checks (checks);
18949 decl = instantiate_template (fn, targs, complain);
18951 checks = get_deferred_access_checks ();
18952 pop_deferring_access_checks ();
18954 pop_tinst_level ();
18956 if (decl == error_mark_node)
18957 goto fail;
18959 /* Now perform any access checks encountered during substitution. */
18960 push_access_scope (decl);
18961 ok = perform_access_checks (checks, complain);
18962 pop_access_scope (decl);
18963 if (!ok)
18964 goto fail;
18966 /* If we're looking for an exact match, check that what we got
18967 is indeed an exact match. It might not be if some template
18968 parameters are used in non-deduced contexts. But don't check
18969 for an exact match if we have dependent template arguments;
18970 in that case we're doing partial ordering, and we already know
18971 that we have two candidates that will provide the actual type. */
18972 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18974 tree substed = TREE_TYPE (decl);
18975 unsigned int i;
18977 tree sarg
18978 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18979 if (return_type)
18980 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18981 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18982 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18984 unify_type_mismatch (explain_p, args[i],
18985 TREE_VALUE (sarg));
18986 goto fail;
18990 /* After doing deduction with the inherited constructor, actually return an
18991 instantiation of the inheriting constructor. */
18992 if (orig_fn != fn)
18993 decl = instantiate_template (orig_fn, targs, complain);
18995 r = decl;
18997 fail:
18998 --deduction_depth;
18999 if (excessive_deduction_depth)
19001 if (deduction_depth == 0)
19002 /* Reset once we're all the way out. */
19003 excessive_deduction_depth = false;
19006 /* We can't free this if a pending_template entry or last_error_tinst_level
19007 is pointing at it. */
19008 if (last_pending_template == old_last_pend
19009 && last_error_tinst_level == old_error_tinst)
19010 ggc_free (tinst);
19012 return r;
19015 /* Adjust types before performing type deduction, as described in
19016 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19017 sections are symmetric. PARM is the type of a function parameter
19018 or the return type of the conversion function. ARG is the type of
19019 the argument passed to the call, or the type of the value
19020 initialized with the result of the conversion function.
19021 ARG_EXPR is the original argument expression, which may be null. */
19023 static int
19024 maybe_adjust_types_for_deduction (unification_kind_t strict,
19025 tree* parm,
19026 tree* arg,
19027 tree arg_expr)
19029 int result = 0;
19031 switch (strict)
19033 case DEDUCE_CALL:
19034 break;
19036 case DEDUCE_CONV:
19037 /* Swap PARM and ARG throughout the remainder of this
19038 function; the handling is precisely symmetric since PARM
19039 will initialize ARG rather than vice versa. */
19040 std::swap (parm, arg);
19041 break;
19043 case DEDUCE_EXACT:
19044 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19045 too, but here handle it by stripping the reference from PARM
19046 rather than by adding it to ARG. */
19047 if (TREE_CODE (*parm) == REFERENCE_TYPE
19048 && TYPE_REF_IS_RVALUE (*parm)
19049 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19050 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19051 && TREE_CODE (*arg) == REFERENCE_TYPE
19052 && !TYPE_REF_IS_RVALUE (*arg))
19053 *parm = TREE_TYPE (*parm);
19054 /* Nothing else to do in this case. */
19055 return 0;
19057 default:
19058 gcc_unreachable ();
19061 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19063 /* [temp.deduct.call]
19065 If P is not a reference type:
19067 --If A is an array type, the pointer type produced by the
19068 array-to-pointer standard conversion (_conv.array_) is
19069 used in place of A for type deduction; otherwise,
19071 --If A is a function type, the pointer type produced by
19072 the function-to-pointer standard conversion
19073 (_conv.func_) is used in place of A for type deduction;
19074 otherwise,
19076 --If A is a cv-qualified type, the top level
19077 cv-qualifiers of A's type are ignored for type
19078 deduction. */
19079 if (TREE_CODE (*arg) == ARRAY_TYPE)
19080 *arg = build_pointer_type (TREE_TYPE (*arg));
19081 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19082 *arg = build_pointer_type (*arg);
19083 else
19084 *arg = TYPE_MAIN_VARIANT (*arg);
19087 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19088 reference to a cv-unqualified template parameter that does not represent a
19089 template parameter of a class template (during class template argument
19090 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19091 an lvalue, the type "lvalue reference to A" is used in place of A for type
19092 deduction. */
19093 if (TREE_CODE (*parm) == REFERENCE_TYPE
19094 && TYPE_REF_IS_RVALUE (*parm)
19095 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19096 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19097 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19098 && (arg_expr ? lvalue_p (arg_expr)
19099 /* try_one_overload doesn't provide an arg_expr, but
19100 functions are always lvalues. */
19101 : TREE_CODE (*arg) == FUNCTION_TYPE))
19102 *arg = build_reference_type (*arg);
19104 /* [temp.deduct.call]
19106 If P is a cv-qualified type, the top level cv-qualifiers
19107 of P's type are ignored for type deduction. If P is a
19108 reference type, the type referred to by P is used for
19109 type deduction. */
19110 *parm = TYPE_MAIN_VARIANT (*parm);
19111 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19113 *parm = TREE_TYPE (*parm);
19114 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19117 /* DR 322. For conversion deduction, remove a reference type on parm
19118 too (which has been swapped into ARG). */
19119 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19120 *arg = TREE_TYPE (*arg);
19122 return result;
19125 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19126 template which does contain any deducible template parameters; check if
19127 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19128 unify_one_argument. */
19130 static int
19131 check_non_deducible_conversion (tree parm, tree arg, int strict,
19132 int flags, bool explain_p)
19134 tree type;
19136 if (!TYPE_P (arg))
19137 type = TREE_TYPE (arg);
19138 else
19139 type = arg;
19141 if (same_type_p (parm, type))
19142 return unify_success (explain_p);
19144 if (strict == DEDUCE_CONV)
19146 if (can_convert_arg (type, parm, NULL_TREE, flags,
19147 explain_p ? tf_warning_or_error : tf_none))
19148 return unify_success (explain_p);
19150 else if (strict != DEDUCE_EXACT)
19152 if (can_convert_arg (parm, type,
19153 TYPE_P (arg) ? NULL_TREE : arg,
19154 flags, explain_p ? tf_warning_or_error : tf_none))
19155 return unify_success (explain_p);
19158 if (strict == DEDUCE_EXACT)
19159 return unify_type_mismatch (explain_p, parm, arg);
19160 else
19161 return unify_arg_conversion (explain_p, parm, type, arg);
19164 static bool uses_deducible_template_parms (tree type);
19166 /* Returns true iff the expression EXPR is one from which a template
19167 argument can be deduced. In other words, if it's an undecorated
19168 use of a template non-type parameter. */
19170 static bool
19171 deducible_expression (tree expr)
19173 /* Strip implicit conversions. */
19174 while (CONVERT_EXPR_P (expr))
19175 expr = TREE_OPERAND (expr, 0);
19176 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19179 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19180 deducible way; that is, if it has a max value of <PARM> - 1. */
19182 static bool
19183 deducible_array_bound (tree domain)
19185 if (domain == NULL_TREE)
19186 return false;
19188 tree max = TYPE_MAX_VALUE (domain);
19189 if (TREE_CODE (max) != MINUS_EXPR)
19190 return false;
19192 return deducible_expression (TREE_OPERAND (max, 0));
19195 /* Returns true iff the template arguments ARGS use a template parameter
19196 in a deducible way. */
19198 static bool
19199 deducible_template_args (tree args)
19201 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19203 bool deducible;
19204 tree elt = TREE_VEC_ELT (args, i);
19205 if (ARGUMENT_PACK_P (elt))
19206 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19207 else
19209 if (PACK_EXPANSION_P (elt))
19210 elt = PACK_EXPANSION_PATTERN (elt);
19211 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19212 deducible = true;
19213 else if (TYPE_P (elt))
19214 deducible = uses_deducible_template_parms (elt);
19215 else
19216 deducible = deducible_expression (elt);
19218 if (deducible)
19219 return true;
19221 return false;
19224 /* Returns true iff TYPE contains any deducible references to template
19225 parameters, as per 14.8.2.5. */
19227 static bool
19228 uses_deducible_template_parms (tree type)
19230 if (PACK_EXPANSION_P (type))
19231 type = PACK_EXPANSION_PATTERN (type);
19233 /* T
19234 cv-list T
19235 TT<T>
19236 TT<i>
19237 TT<> */
19238 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19239 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19240 return true;
19242 /* T*
19244 T&& */
19245 if (POINTER_TYPE_P (type))
19246 return uses_deducible_template_parms (TREE_TYPE (type));
19248 /* T[integer-constant ]
19249 type [i] */
19250 if (TREE_CODE (type) == ARRAY_TYPE)
19251 return (uses_deducible_template_parms (TREE_TYPE (type))
19252 || deducible_array_bound (TYPE_DOMAIN (type)));
19254 /* T type ::*
19255 type T::*
19256 T T::*
19257 T (type ::*)()
19258 type (T::*)()
19259 type (type ::*)(T)
19260 type (T::*)(T)
19261 T (type ::*)(T)
19262 T (T::*)()
19263 T (T::*)(T) */
19264 if (TYPE_PTRMEM_P (type))
19265 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19266 || (uses_deducible_template_parms
19267 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19269 /* template-name <T> (where template-name refers to a class template)
19270 template-name <i> (where template-name refers to a class template) */
19271 if (CLASS_TYPE_P (type)
19272 && CLASSTYPE_TEMPLATE_INFO (type)
19273 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19274 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19275 (CLASSTYPE_TI_ARGS (type)));
19277 /* type (T)
19279 T(T) */
19280 if (TREE_CODE (type) == FUNCTION_TYPE
19281 || TREE_CODE (type) == METHOD_TYPE)
19283 if (uses_deducible_template_parms (TREE_TYPE (type)))
19284 return true;
19285 tree parm = TYPE_ARG_TYPES (type);
19286 if (TREE_CODE (type) == METHOD_TYPE)
19287 parm = TREE_CHAIN (parm);
19288 for (; parm; parm = TREE_CHAIN (parm))
19289 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19290 return true;
19293 return false;
19296 /* Subroutine of type_unification_real and unify_pack_expansion to
19297 handle unification of a single P/A pair. Parameters are as
19298 for those functions. */
19300 static int
19301 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19302 int subr, unification_kind_t strict,
19303 bool explain_p)
19305 tree arg_expr = NULL_TREE;
19306 int arg_strict;
19308 if (arg == error_mark_node || parm == error_mark_node)
19309 return unify_invalid (explain_p);
19310 if (arg == unknown_type_node)
19311 /* We can't deduce anything from this, but we might get all the
19312 template args from other function args. */
19313 return unify_success (explain_p);
19315 /* Implicit conversions (Clause 4) will be performed on a function
19316 argument to convert it to the type of the corresponding function
19317 parameter if the parameter type contains no template-parameters that
19318 participate in template argument deduction. */
19319 if (strict != DEDUCE_EXACT
19320 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19321 /* For function parameters with no deducible template parameters,
19322 just return. We'll check non-dependent conversions later. */
19323 return unify_success (explain_p);
19325 switch (strict)
19327 case DEDUCE_CALL:
19328 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19329 | UNIFY_ALLOW_MORE_CV_QUAL
19330 | UNIFY_ALLOW_DERIVED);
19331 break;
19333 case DEDUCE_CONV:
19334 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19335 break;
19337 case DEDUCE_EXACT:
19338 arg_strict = UNIFY_ALLOW_NONE;
19339 break;
19341 default:
19342 gcc_unreachable ();
19345 /* We only do these transformations if this is the top-level
19346 parameter_type_list in a call or declaration matching; in other
19347 situations (nested function declarators, template argument lists) we
19348 won't be comparing a type to an expression, and we don't do any type
19349 adjustments. */
19350 if (!subr)
19352 if (!TYPE_P (arg))
19354 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19355 if (type_unknown_p (arg))
19357 /* [temp.deduct.type] A template-argument can be
19358 deduced from a pointer to function or pointer
19359 to member function argument if the set of
19360 overloaded functions does not contain function
19361 templates and at most one of a set of
19362 overloaded functions provides a unique
19363 match. */
19364 resolve_overloaded_unification (tparms, targs, parm,
19365 arg, strict,
19366 arg_strict, explain_p);
19367 /* If a unique match was not found, this is a
19368 non-deduced context, so we still succeed. */
19369 return unify_success (explain_p);
19372 arg_expr = arg;
19373 arg = unlowered_expr_type (arg);
19374 if (arg == error_mark_node)
19375 return unify_invalid (explain_p);
19378 arg_strict |=
19379 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19381 else
19382 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19383 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19384 return unify_template_argument_mismatch (explain_p, parm, arg);
19386 /* For deduction from an init-list we need the actual list. */
19387 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19388 arg = arg_expr;
19389 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19392 /* for_each_template_parm callback that always returns 0. */
19394 static int
19395 zero_r (tree, void *)
19397 return 0;
19400 /* for_each_template_parm any_fn callback to handle deduction of a template
19401 type argument from the type of an array bound. */
19403 static int
19404 array_deduction_r (tree t, void *data)
19406 tree_pair_p d = (tree_pair_p)data;
19407 tree &tparms = d->purpose;
19408 tree &targs = d->value;
19410 if (TREE_CODE (t) == ARRAY_TYPE)
19411 if (tree dom = TYPE_DOMAIN (t))
19412 if (tree max = TYPE_MAX_VALUE (dom))
19414 if (TREE_CODE (max) == MINUS_EXPR)
19415 max = TREE_OPERAND (max, 0);
19416 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19417 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19418 UNIFY_ALLOW_NONE, /*explain*/false);
19421 /* Keep walking. */
19422 return 0;
19425 /* Try to deduce any not-yet-deduced template type arguments from the type of
19426 an array bound. This is handled separately from unify because 14.8.2.5 says
19427 "The type of a type parameter is only deduced from an array bound if it is
19428 not otherwise deduced." */
19430 static void
19431 try_array_deduction (tree tparms, tree targs, tree parm)
19433 tree_pair_s data = { tparms, targs };
19434 hash_set<tree> visited;
19435 for_each_template_parm (parm, zero_r, &data, &visited,
19436 /*nondeduced*/false, array_deduction_r);
19439 /* Most parms like fn_type_unification.
19441 If SUBR is 1, we're being called recursively (to unify the
19442 arguments of a function or method parameter of a function
19443 template).
19445 CHECKS is a pointer to a vector of access checks encountered while
19446 substituting default template arguments. */
19448 static int
19449 type_unification_real (tree tparms,
19450 tree full_targs,
19451 tree xparms,
19452 const tree *xargs,
19453 unsigned int xnargs,
19454 int subr,
19455 unification_kind_t strict,
19456 int flags,
19457 vec<deferred_access_check, va_gc> **checks,
19458 bool explain_p)
19460 tree parm, arg;
19461 int i;
19462 int ntparms = TREE_VEC_LENGTH (tparms);
19463 int saw_undeduced = 0;
19464 tree parms;
19465 const tree *args;
19466 unsigned int nargs;
19467 unsigned int ia;
19469 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19470 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19471 gcc_assert (ntparms > 0);
19473 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19475 /* Reset the number of non-defaulted template arguments contained
19476 in TARGS. */
19477 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19479 again:
19480 parms = xparms;
19481 args = xargs;
19482 nargs = xnargs;
19484 ia = 0;
19485 while (parms && parms != void_list_node
19486 && ia < nargs)
19488 parm = TREE_VALUE (parms);
19490 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19491 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19492 /* For a function parameter pack that occurs at the end of the
19493 parameter-declaration-list, the type A of each remaining
19494 argument of the call is compared with the type P of the
19495 declarator-id of the function parameter pack. */
19496 break;
19498 parms = TREE_CHAIN (parms);
19500 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19501 /* For a function parameter pack that does not occur at the
19502 end of the parameter-declaration-list, the type of the
19503 parameter pack is a non-deduced context. */
19504 continue;
19506 arg = args[ia];
19507 ++ia;
19509 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19510 explain_p))
19511 return 1;
19514 if (parms
19515 && parms != void_list_node
19516 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19518 /* Unify the remaining arguments with the pack expansion type. */
19519 tree argvec;
19520 tree parmvec = make_tree_vec (1);
19522 /* Allocate a TREE_VEC and copy in all of the arguments */
19523 argvec = make_tree_vec (nargs - ia);
19524 for (i = 0; ia < nargs; ++ia, ++i)
19525 TREE_VEC_ELT (argvec, i) = args[ia];
19527 /* Copy the parameter into parmvec. */
19528 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19529 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19530 /*subr=*/subr, explain_p))
19531 return 1;
19533 /* Advance to the end of the list of parameters. */
19534 parms = TREE_CHAIN (parms);
19537 /* Fail if we've reached the end of the parm list, and more args
19538 are present, and the parm list isn't variadic. */
19539 if (ia < nargs && parms == void_list_node)
19540 return unify_too_many_arguments (explain_p, nargs, ia);
19541 /* Fail if parms are left and they don't have default values and
19542 they aren't all deduced as empty packs (c++/57397). This is
19543 consistent with sufficient_parms_p. */
19544 if (parms && parms != void_list_node
19545 && TREE_PURPOSE (parms) == NULL_TREE)
19547 unsigned int count = nargs;
19548 tree p = parms;
19549 bool type_pack_p;
19552 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19553 if (!type_pack_p)
19554 count++;
19555 p = TREE_CHAIN (p);
19557 while (p && p != void_list_node);
19558 if (count != nargs)
19559 return unify_too_few_arguments (explain_p, ia, count,
19560 type_pack_p);
19563 if (!subr)
19565 tsubst_flags_t complain = (explain_p
19566 ? tf_warning_or_error
19567 : tf_none);
19568 bool tried_array_deduction = (cxx_dialect < cxx17);
19570 for (i = 0; i < ntparms; i++)
19572 tree targ = TREE_VEC_ELT (targs, i);
19573 tree tparm = TREE_VEC_ELT (tparms, i);
19575 /* Clear the "incomplete" flags on all argument packs now so that
19576 substituting them into later default arguments works. */
19577 if (targ && ARGUMENT_PACK_P (targ))
19579 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19580 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19583 if (targ || tparm == error_mark_node)
19584 continue;
19585 tparm = TREE_VALUE (tparm);
19587 if (TREE_CODE (tparm) == TYPE_DECL
19588 && !tried_array_deduction)
19590 try_array_deduction (tparms, targs, xparms);
19591 tried_array_deduction = true;
19592 if (TREE_VEC_ELT (targs, i))
19593 continue;
19596 /* If this is an undeduced nontype parameter that depends on
19597 a type parameter, try another pass; its type may have been
19598 deduced from a later argument than the one from which
19599 this parameter can be deduced. */
19600 if (TREE_CODE (tparm) == PARM_DECL
19601 && uses_template_parms (TREE_TYPE (tparm))
19602 && saw_undeduced < 2)
19604 saw_undeduced = 1;
19605 continue;
19608 /* Core issue #226 (C++0x) [temp.deduct]:
19610 If a template argument has not been deduced, its
19611 default template argument, if any, is used.
19613 When we are in C++98 mode, TREE_PURPOSE will either
19614 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19615 to explicitly check cxx_dialect here. */
19616 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19617 /* OK, there is a default argument. Wait until after the
19618 conversion check to do substitution. */
19619 continue;
19621 /* If the type parameter is a parameter pack, then it will
19622 be deduced to an empty parameter pack. */
19623 if (template_parameter_pack_p (tparm))
19625 tree arg;
19627 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19629 arg = make_node (NONTYPE_ARGUMENT_PACK);
19630 TREE_CONSTANT (arg) = 1;
19632 else
19633 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19635 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19637 TREE_VEC_ELT (targs, i) = arg;
19638 continue;
19641 return unify_parameter_deduction_failure (explain_p, tparm);
19644 /* DR 1391: All parameters have args, now check non-dependent parms for
19645 convertibility. */
19646 if (saw_undeduced < 2)
19647 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19648 parms && parms != void_list_node && ia < nargs; )
19650 parm = TREE_VALUE (parms);
19652 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19653 && (!TREE_CHAIN (parms)
19654 || TREE_CHAIN (parms) == void_list_node))
19655 /* For a function parameter pack that occurs at the end of the
19656 parameter-declaration-list, the type A of each remaining
19657 argument of the call is compared with the type P of the
19658 declarator-id of the function parameter pack. */
19659 break;
19661 parms = TREE_CHAIN (parms);
19663 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19664 /* For a function parameter pack that does not occur at the
19665 end of the parameter-declaration-list, the type of the
19666 parameter pack is a non-deduced context. */
19667 continue;
19669 arg = args[ia];
19670 ++ia;
19672 if (uses_template_parms (parm))
19673 continue;
19674 if (check_non_deducible_conversion (parm, arg, strict, flags,
19675 explain_p))
19676 return 1;
19679 /* Now substitute into the default template arguments. */
19680 for (i = 0; i < ntparms; i++)
19682 tree targ = TREE_VEC_ELT (targs, i);
19683 tree tparm = TREE_VEC_ELT (tparms, i);
19685 if (targ || tparm == error_mark_node)
19686 continue;
19687 tree parm = TREE_VALUE (tparm);
19689 if (TREE_CODE (parm) == PARM_DECL
19690 && uses_template_parms (TREE_TYPE (parm))
19691 && saw_undeduced < 2)
19692 continue;
19694 tree arg = TREE_PURPOSE (tparm);
19695 reopen_deferring_access_checks (*checks);
19696 location_t save_loc = input_location;
19697 if (DECL_P (parm))
19698 input_location = DECL_SOURCE_LOCATION (parm);
19699 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19700 if (!uses_template_parms (arg))
19701 arg = convert_template_argument (parm, arg, full_targs, complain,
19702 i, NULL_TREE);
19703 else if (saw_undeduced < 2)
19704 arg = NULL_TREE;
19705 else
19706 arg = error_mark_node;
19707 input_location = save_loc;
19708 *checks = get_deferred_access_checks ();
19709 pop_deferring_access_checks ();
19710 if (arg == error_mark_node)
19711 return 1;
19712 else if (arg)
19714 TREE_VEC_ELT (targs, i) = arg;
19715 /* The position of the first default template argument,
19716 is also the number of non-defaulted arguments in TARGS.
19717 Record that. */
19718 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19719 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19723 if (saw_undeduced++ == 1)
19724 goto again;
19727 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19728 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19730 return unify_success (explain_p);
19733 /* Subroutine of type_unification_real. Args are like the variables
19734 at the call site. ARG is an overloaded function (or template-id);
19735 we try deducing template args from each of the overloads, and if
19736 only one succeeds, we go with that. Modifies TARGS and returns
19737 true on success. */
19739 static bool
19740 resolve_overloaded_unification (tree tparms,
19741 tree targs,
19742 tree parm,
19743 tree arg,
19744 unification_kind_t strict,
19745 int sub_strict,
19746 bool explain_p)
19748 tree tempargs = copy_node (targs);
19749 int good = 0;
19750 tree goodfn = NULL_TREE;
19751 bool addr_p;
19753 if (TREE_CODE (arg) == ADDR_EXPR)
19755 arg = TREE_OPERAND (arg, 0);
19756 addr_p = true;
19758 else
19759 addr_p = false;
19761 if (TREE_CODE (arg) == COMPONENT_REF)
19762 /* Handle `&x' where `x' is some static or non-static member
19763 function name. */
19764 arg = TREE_OPERAND (arg, 1);
19766 if (TREE_CODE (arg) == OFFSET_REF)
19767 arg = TREE_OPERAND (arg, 1);
19769 /* Strip baselink information. */
19770 if (BASELINK_P (arg))
19771 arg = BASELINK_FUNCTIONS (arg);
19773 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19775 /* If we got some explicit template args, we need to plug them into
19776 the affected templates before we try to unify, in case the
19777 explicit args will completely resolve the templates in question. */
19779 int ok = 0;
19780 tree expl_subargs = TREE_OPERAND (arg, 1);
19781 arg = TREE_OPERAND (arg, 0);
19783 for (lkp_iterator iter (arg); iter; ++iter)
19785 tree fn = *iter;
19786 tree subargs, elem;
19788 if (TREE_CODE (fn) != TEMPLATE_DECL)
19789 continue;
19791 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19792 expl_subargs, NULL_TREE, tf_none,
19793 /*require_all_args=*/true,
19794 /*use_default_args=*/true);
19795 if (subargs != error_mark_node
19796 && !any_dependent_template_arguments_p (subargs))
19798 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19799 if (try_one_overload (tparms, targs, tempargs, parm,
19800 elem, strict, sub_strict, addr_p, explain_p)
19801 && (!goodfn || !same_type_p (goodfn, elem)))
19803 goodfn = elem;
19804 ++good;
19807 else if (subargs)
19808 ++ok;
19810 /* If no templates (or more than one) are fully resolved by the
19811 explicit arguments, this template-id is a non-deduced context; it
19812 could still be OK if we deduce all template arguments for the
19813 enclosing call through other arguments. */
19814 if (good != 1)
19815 good = ok;
19817 else if (TREE_CODE (arg) != OVERLOAD
19818 && TREE_CODE (arg) != FUNCTION_DECL)
19819 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19820 -- but the deduction does not succeed because the expression is
19821 not just the function on its own. */
19822 return false;
19823 else
19824 for (lkp_iterator iter (arg); iter; ++iter)
19826 tree fn = *iter;
19827 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19828 strict, sub_strict, addr_p, explain_p)
19829 && (!goodfn || !decls_match (goodfn, fn)))
19831 goodfn = fn;
19832 ++good;
19836 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19837 to function or pointer to member function argument if the set of
19838 overloaded functions does not contain function templates and at most
19839 one of a set of overloaded functions provides a unique match.
19841 So if we found multiple possibilities, we return success but don't
19842 deduce anything. */
19844 if (good == 1)
19846 int i = TREE_VEC_LENGTH (targs);
19847 for (; i--; )
19848 if (TREE_VEC_ELT (tempargs, i))
19850 tree old = TREE_VEC_ELT (targs, i);
19851 tree new_ = TREE_VEC_ELT (tempargs, i);
19852 if (new_ && old && ARGUMENT_PACK_P (old)
19853 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19854 /* Don't forget explicit template arguments in a pack. */
19855 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19856 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19857 TREE_VEC_ELT (targs, i) = new_;
19860 if (good)
19861 return true;
19863 return false;
19866 /* Core DR 115: In contexts where deduction is done and fails, or in
19867 contexts where deduction is not done, if a template argument list is
19868 specified and it, along with any default template arguments, identifies
19869 a single function template specialization, then the template-id is an
19870 lvalue for the function template specialization. */
19872 tree
19873 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19875 tree expr, offset, baselink;
19876 bool addr;
19878 if (!type_unknown_p (orig_expr))
19879 return orig_expr;
19881 expr = orig_expr;
19882 addr = false;
19883 offset = NULL_TREE;
19884 baselink = NULL_TREE;
19886 if (TREE_CODE (expr) == ADDR_EXPR)
19888 expr = TREE_OPERAND (expr, 0);
19889 addr = true;
19891 if (TREE_CODE (expr) == OFFSET_REF)
19893 offset = expr;
19894 expr = TREE_OPERAND (expr, 1);
19896 if (BASELINK_P (expr))
19898 baselink = expr;
19899 expr = BASELINK_FUNCTIONS (expr);
19902 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19904 int good = 0;
19905 tree goodfn = NULL_TREE;
19907 /* If we got some explicit template args, we need to plug them into
19908 the affected templates before we try to unify, in case the
19909 explicit args will completely resolve the templates in question. */
19911 tree expl_subargs = TREE_OPERAND (expr, 1);
19912 tree arg = TREE_OPERAND (expr, 0);
19913 tree badfn = NULL_TREE;
19914 tree badargs = NULL_TREE;
19916 for (lkp_iterator iter (arg); iter; ++iter)
19918 tree fn = *iter;
19919 tree subargs, elem;
19921 if (TREE_CODE (fn) != TEMPLATE_DECL)
19922 continue;
19924 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19925 expl_subargs, NULL_TREE, tf_none,
19926 /*require_all_args=*/true,
19927 /*use_default_args=*/true);
19928 if (subargs != error_mark_node
19929 && !any_dependent_template_arguments_p (subargs))
19931 elem = instantiate_template (fn, subargs, tf_none);
19932 if (elem == error_mark_node)
19934 badfn = fn;
19935 badargs = subargs;
19937 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19939 goodfn = elem;
19940 ++good;
19944 if (good == 1)
19946 mark_used (goodfn);
19947 expr = goodfn;
19948 if (baselink)
19949 expr = build_baselink (BASELINK_BINFO (baselink),
19950 BASELINK_ACCESS_BINFO (baselink),
19951 expr, BASELINK_OPTYPE (baselink));
19952 if (offset)
19954 tree base
19955 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19956 expr = build_offset_ref (base, expr, addr, complain);
19958 if (addr)
19959 expr = cp_build_addr_expr (expr, complain);
19960 return expr;
19962 else if (good == 0 && badargs && (complain & tf_error))
19963 /* There were no good options and at least one bad one, so let the
19964 user know what the problem is. */
19965 instantiate_template (badfn, badargs, complain);
19967 return orig_expr;
19970 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19971 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19972 different overloads deduce different arguments for a given parm.
19973 ADDR_P is true if the expression for which deduction is being
19974 performed was of the form "& fn" rather than simply "fn".
19976 Returns 1 on success. */
19978 static int
19979 try_one_overload (tree tparms,
19980 tree orig_targs,
19981 tree targs,
19982 tree parm,
19983 tree arg,
19984 unification_kind_t strict,
19985 int sub_strict,
19986 bool addr_p,
19987 bool explain_p)
19989 int nargs;
19990 tree tempargs;
19991 int i;
19993 if (arg == error_mark_node)
19994 return 0;
19996 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19997 to function or pointer to member function argument if the set of
19998 overloaded functions does not contain function templates and at most
19999 one of a set of overloaded functions provides a unique match.
20001 So if this is a template, just return success. */
20003 if (uses_template_parms (arg))
20004 return 1;
20006 if (TREE_CODE (arg) == METHOD_TYPE)
20007 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20008 else if (addr_p)
20009 arg = build_pointer_type (arg);
20011 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20013 /* We don't copy orig_targs for this because if we have already deduced
20014 some template args from previous args, unify would complain when we
20015 try to deduce a template parameter for the same argument, even though
20016 there isn't really a conflict. */
20017 nargs = TREE_VEC_LENGTH (targs);
20018 tempargs = make_tree_vec (nargs);
20020 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20021 return 0;
20023 /* First make sure we didn't deduce anything that conflicts with
20024 explicitly specified args. */
20025 for (i = nargs; i--; )
20027 tree elt = TREE_VEC_ELT (tempargs, i);
20028 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20030 if (!elt)
20031 /*NOP*/;
20032 else if (uses_template_parms (elt))
20033 /* Since we're unifying against ourselves, we will fill in
20034 template args used in the function parm list with our own
20035 template parms. Discard them. */
20036 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20037 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20039 /* Check that the argument at each index of the deduced argument pack
20040 is equivalent to the corresponding explicitly specified argument.
20041 We may have deduced more arguments than were explicitly specified,
20042 and that's OK. */
20044 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20045 that's wrong if we deduce the same argument pack from multiple
20046 function arguments: it's only incomplete the first time. */
20048 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20049 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20051 if (TREE_VEC_LENGTH (deduced_pack)
20052 < TREE_VEC_LENGTH (explicit_pack))
20053 return 0;
20055 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20056 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20057 TREE_VEC_ELT (deduced_pack, j)))
20058 return 0;
20060 else if (oldelt && !template_args_equal (oldelt, elt))
20061 return 0;
20064 for (i = nargs; i--; )
20066 tree elt = TREE_VEC_ELT (tempargs, i);
20068 if (elt)
20069 TREE_VEC_ELT (targs, i) = elt;
20072 return 1;
20075 /* PARM is a template class (perhaps with unbound template
20076 parameters). ARG is a fully instantiated type. If ARG can be
20077 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20078 TARGS are as for unify. */
20080 static tree
20081 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20082 bool explain_p)
20084 tree copy_of_targs;
20086 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20087 return NULL_TREE;
20088 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20089 /* Matches anything. */;
20090 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20091 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20092 return NULL_TREE;
20094 /* We need to make a new template argument vector for the call to
20095 unify. If we used TARGS, we'd clutter it up with the result of
20096 the attempted unification, even if this class didn't work out.
20097 We also don't want to commit ourselves to all the unifications
20098 we've already done, since unification is supposed to be done on
20099 an argument-by-argument basis. In other words, consider the
20100 following pathological case:
20102 template <int I, int J, int K>
20103 struct S {};
20105 template <int I, int J>
20106 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20108 template <int I, int J, int K>
20109 void f(S<I, J, K>, S<I, I, I>);
20111 void g() {
20112 S<0, 0, 0> s0;
20113 S<0, 1, 2> s2;
20115 f(s0, s2);
20118 Now, by the time we consider the unification involving `s2', we
20119 already know that we must have `f<0, 0, 0>'. But, even though
20120 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20121 because there are two ways to unify base classes of S<0, 1, 2>
20122 with S<I, I, I>. If we kept the already deduced knowledge, we
20123 would reject the possibility I=1. */
20124 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20126 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20128 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20129 return NULL_TREE;
20130 return arg;
20133 /* If unification failed, we're done. */
20134 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20135 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20136 return NULL_TREE;
20138 return arg;
20141 /* Given a template type PARM and a class type ARG, find the unique
20142 base type in ARG that is an instance of PARM. We do not examine
20143 ARG itself; only its base-classes. If there is not exactly one
20144 appropriate base class, return NULL_TREE. PARM may be the type of
20145 a partial specialization, as well as a plain template type. Used
20146 by unify. */
20148 static enum template_base_result
20149 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20150 bool explain_p, tree *result)
20152 tree rval = NULL_TREE;
20153 tree binfo;
20155 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20157 binfo = TYPE_BINFO (complete_type (arg));
20158 if (!binfo)
20160 /* The type could not be completed. */
20161 *result = NULL_TREE;
20162 return tbr_incomplete_type;
20165 /* Walk in inheritance graph order. The search order is not
20166 important, and this avoids multiple walks of virtual bases. */
20167 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20169 tree r = try_class_unification (tparms, targs, parm,
20170 BINFO_TYPE (binfo), explain_p);
20172 if (r)
20174 /* If there is more than one satisfactory baseclass, then:
20176 [temp.deduct.call]
20178 If they yield more than one possible deduced A, the type
20179 deduction fails.
20181 applies. */
20182 if (rval && !same_type_p (r, rval))
20184 *result = NULL_TREE;
20185 return tbr_ambiguous_baseclass;
20188 rval = r;
20192 *result = rval;
20193 return tbr_success;
20196 /* Returns the level of DECL, which declares a template parameter. */
20198 static int
20199 template_decl_level (tree decl)
20201 switch (TREE_CODE (decl))
20203 case TYPE_DECL:
20204 case TEMPLATE_DECL:
20205 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20207 case PARM_DECL:
20208 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20210 default:
20211 gcc_unreachable ();
20213 return 0;
20216 /* Decide whether ARG can be unified with PARM, considering only the
20217 cv-qualifiers of each type, given STRICT as documented for unify.
20218 Returns nonzero iff the unification is OK on that basis. */
20220 static int
20221 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20223 int arg_quals = cp_type_quals (arg);
20224 int parm_quals = cp_type_quals (parm);
20226 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20227 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20229 /* Although a CVR qualifier is ignored when being applied to a
20230 substituted template parameter ([8.3.2]/1 for example), that
20231 does not allow us to unify "const T" with "int&" because both
20232 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20233 It is ok when we're allowing additional CV qualifiers
20234 at the outer level [14.8.2.1]/3,1st bullet. */
20235 if ((TREE_CODE (arg) == REFERENCE_TYPE
20236 || TREE_CODE (arg) == FUNCTION_TYPE
20237 || TREE_CODE (arg) == METHOD_TYPE)
20238 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20239 return 0;
20241 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20242 && (parm_quals & TYPE_QUAL_RESTRICT))
20243 return 0;
20246 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20247 && (arg_quals & parm_quals) != parm_quals)
20248 return 0;
20250 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20251 && (parm_quals & arg_quals) != arg_quals)
20252 return 0;
20254 return 1;
20257 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20258 void
20259 template_parm_level_and_index (tree parm, int* level, int* index)
20261 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20262 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20263 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20265 *index = TEMPLATE_TYPE_IDX (parm);
20266 *level = TEMPLATE_TYPE_LEVEL (parm);
20268 else
20270 *index = TEMPLATE_PARM_IDX (parm);
20271 *level = TEMPLATE_PARM_LEVEL (parm);
20275 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20276 do { \
20277 if (unify (TP, TA, P, A, S, EP)) \
20278 return 1; \
20279 } while (0)
20281 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20282 expansion at the end of PACKED_PARMS. Returns 0 if the type
20283 deduction succeeds, 1 otherwise. STRICT is the same as in
20284 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20285 function call argument list. We'll need to adjust the arguments to make them
20286 types. SUBR tells us if this is from a recursive call to
20287 type_unification_real, or for comparing two template argument
20288 lists. */
20290 static int
20291 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20292 tree packed_args, unification_kind_t strict,
20293 bool subr, bool explain_p)
20295 tree parm
20296 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20297 tree pattern = PACK_EXPANSION_PATTERN (parm);
20298 tree pack, packs = NULL_TREE;
20299 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20301 /* Add in any args remembered from an earlier partial instantiation. */
20302 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20304 packed_args = expand_template_argument_pack (packed_args);
20306 int len = TREE_VEC_LENGTH (packed_args);
20308 /* Determine the parameter packs we will be deducing from the
20309 pattern, and record their current deductions. */
20310 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20311 pack; pack = TREE_CHAIN (pack))
20313 tree parm_pack = TREE_VALUE (pack);
20314 int idx, level;
20316 /* Determine the index and level of this parameter pack. */
20317 template_parm_level_and_index (parm_pack, &level, &idx);
20319 /* Keep track of the parameter packs and their corresponding
20320 argument packs. */
20321 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20322 TREE_TYPE (packs) = make_tree_vec (len - start);
20325 /* Loop through all of the arguments that have not yet been
20326 unified and unify each with the pattern. */
20327 for (i = start; i < len; i++)
20329 tree parm;
20330 bool any_explicit = false;
20331 tree arg = TREE_VEC_ELT (packed_args, i);
20333 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20334 or the element of its argument pack at the current index if
20335 this argument was explicitly specified. */
20336 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20338 int idx, level;
20339 tree arg, pargs;
20340 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20342 arg = NULL_TREE;
20343 if (TREE_VALUE (pack)
20344 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20345 && (i - start < TREE_VEC_LENGTH (pargs)))
20347 any_explicit = true;
20348 arg = TREE_VEC_ELT (pargs, i - start);
20350 TMPL_ARG (targs, level, idx) = arg;
20353 /* If we had explicit template arguments, substitute them into the
20354 pattern before deduction. */
20355 if (any_explicit)
20357 /* Some arguments might still be unspecified or dependent. */
20358 bool dependent;
20359 ++processing_template_decl;
20360 dependent = any_dependent_template_arguments_p (targs);
20361 if (!dependent)
20362 --processing_template_decl;
20363 parm = tsubst (pattern, targs,
20364 explain_p ? tf_warning_or_error : tf_none,
20365 NULL_TREE);
20366 if (dependent)
20367 --processing_template_decl;
20368 if (parm == error_mark_node)
20369 return 1;
20371 else
20372 parm = pattern;
20374 /* Unify the pattern with the current argument. */
20375 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20376 explain_p))
20377 return 1;
20379 /* For each parameter pack, collect the deduced value. */
20380 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20382 int idx, level;
20383 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20385 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20386 TMPL_ARG (targs, level, idx);
20390 /* Verify that the results of unification with the parameter packs
20391 produce results consistent with what we've seen before, and make
20392 the deduced argument packs available. */
20393 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20395 tree old_pack = TREE_VALUE (pack);
20396 tree new_args = TREE_TYPE (pack);
20397 int i, len = TREE_VEC_LENGTH (new_args);
20398 int idx, level;
20399 bool nondeduced_p = false;
20401 /* By default keep the original deduced argument pack.
20402 If necessary, more specific code is going to update the
20403 resulting deduced argument later down in this function. */
20404 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20405 TMPL_ARG (targs, level, idx) = old_pack;
20407 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20408 actually deduce anything. */
20409 for (i = 0; i < len && !nondeduced_p; ++i)
20410 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20411 nondeduced_p = true;
20412 if (nondeduced_p)
20413 continue;
20415 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20417 /* If we had fewer function args than explicit template args,
20418 just use the explicits. */
20419 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20420 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20421 if (len < explicit_len)
20422 new_args = explicit_args;
20425 if (!old_pack)
20427 tree result;
20428 /* Build the deduced *_ARGUMENT_PACK. */
20429 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20431 result = make_node (NONTYPE_ARGUMENT_PACK);
20432 TREE_CONSTANT (result) = 1;
20434 else
20435 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20437 SET_ARGUMENT_PACK_ARGS (result, new_args);
20439 /* Note the deduced argument packs for this parameter
20440 pack. */
20441 TMPL_ARG (targs, level, idx) = result;
20443 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20444 && (ARGUMENT_PACK_ARGS (old_pack)
20445 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20447 /* We only had the explicitly-provided arguments before, but
20448 now we have a complete set of arguments. */
20449 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20451 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20452 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20453 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20455 else
20457 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20458 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20460 if (!comp_template_args (old_args, new_args,
20461 &bad_old_arg, &bad_new_arg))
20462 /* Inconsistent unification of this parameter pack. */
20463 return unify_parameter_pack_inconsistent (explain_p,
20464 bad_old_arg,
20465 bad_new_arg);
20469 return unify_success (explain_p);
20472 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20473 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20474 parameters and return value are as for unify. */
20476 static int
20477 unify_array_domain (tree tparms, tree targs,
20478 tree parm_dom, tree arg_dom,
20479 bool explain_p)
20481 tree parm_max;
20482 tree arg_max;
20483 bool parm_cst;
20484 bool arg_cst;
20486 /* Our representation of array types uses "N - 1" as the
20487 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20488 not an integer constant. We cannot unify arbitrarily
20489 complex expressions, so we eliminate the MINUS_EXPRs
20490 here. */
20491 parm_max = TYPE_MAX_VALUE (parm_dom);
20492 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20493 if (!parm_cst)
20495 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20496 parm_max = TREE_OPERAND (parm_max, 0);
20498 arg_max = TYPE_MAX_VALUE (arg_dom);
20499 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20500 if (!arg_cst)
20502 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20503 trying to unify the type of a variable with the type
20504 of a template parameter. For example:
20506 template <unsigned int N>
20507 void f (char (&) [N]);
20508 int g();
20509 void h(int i) {
20510 char a[g(i)];
20511 f(a);
20514 Here, the type of the ARG will be "int [g(i)]", and
20515 may be a SAVE_EXPR, etc. */
20516 if (TREE_CODE (arg_max) != MINUS_EXPR)
20517 return unify_vla_arg (explain_p, arg_dom);
20518 arg_max = TREE_OPERAND (arg_max, 0);
20521 /* If only one of the bounds used a MINUS_EXPR, compensate
20522 by adding one to the other bound. */
20523 if (parm_cst && !arg_cst)
20524 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20525 integer_type_node,
20526 parm_max,
20527 integer_one_node);
20528 else if (arg_cst && !parm_cst)
20529 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20530 integer_type_node,
20531 arg_max,
20532 integer_one_node);
20534 return unify (tparms, targs, parm_max, arg_max,
20535 UNIFY_ALLOW_INTEGER, explain_p);
20538 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20540 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20542 static pa_kind_t
20543 pa_kind (tree t)
20545 if (PACK_EXPANSION_P (t))
20546 t = PACK_EXPANSION_PATTERN (t);
20547 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20548 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20549 || DECL_TYPE_TEMPLATE_P (t))
20550 return pa_tmpl;
20551 else if (TYPE_P (t))
20552 return pa_type;
20553 else
20554 return pa_expr;
20557 /* Deduce the value of template parameters. TPARMS is the (innermost)
20558 set of template parameters to a template. TARGS is the bindings
20559 for those template parameters, as determined thus far; TARGS may
20560 include template arguments for outer levels of template parameters
20561 as well. PARM is a parameter to a template function, or a
20562 subcomponent of that parameter; ARG is the corresponding argument.
20563 This function attempts to match PARM with ARG in a manner
20564 consistent with the existing assignments in TARGS. If more values
20565 are deduced, then TARGS is updated.
20567 Returns 0 if the type deduction succeeds, 1 otherwise. The
20568 parameter STRICT is a bitwise or of the following flags:
20570 UNIFY_ALLOW_NONE:
20571 Require an exact match between PARM and ARG.
20572 UNIFY_ALLOW_MORE_CV_QUAL:
20573 Allow the deduced ARG to be more cv-qualified (by qualification
20574 conversion) than ARG.
20575 UNIFY_ALLOW_LESS_CV_QUAL:
20576 Allow the deduced ARG to be less cv-qualified than ARG.
20577 UNIFY_ALLOW_DERIVED:
20578 Allow the deduced ARG to be a template base class of ARG,
20579 or a pointer to a template base class of the type pointed to by
20580 ARG.
20581 UNIFY_ALLOW_INTEGER:
20582 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20583 case for more information.
20584 UNIFY_ALLOW_OUTER_LEVEL:
20585 This is the outermost level of a deduction. Used to determine validity
20586 of qualification conversions. A valid qualification conversion must
20587 have const qualified pointers leading up to the inner type which
20588 requires additional CV quals, except at the outer level, where const
20589 is not required [conv.qual]. It would be normal to set this flag in
20590 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20591 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20592 This is the outermost level of a deduction, and PARM can be more CV
20593 qualified at this point.
20594 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20595 This is the outermost level of a deduction, and PARM can be less CV
20596 qualified at this point. */
20598 static int
20599 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20600 bool explain_p)
20602 int idx;
20603 tree targ;
20604 tree tparm;
20605 int strict_in = strict;
20606 tsubst_flags_t complain = (explain_p
20607 ? tf_warning_or_error
20608 : tf_none);
20610 /* I don't think this will do the right thing with respect to types.
20611 But the only case I've seen it in so far has been array bounds, where
20612 signedness is the only information lost, and I think that will be
20613 okay. */
20614 while (CONVERT_EXPR_P (parm))
20615 parm = TREE_OPERAND (parm, 0);
20617 if (arg == error_mark_node)
20618 return unify_invalid (explain_p);
20619 if (arg == unknown_type_node
20620 || arg == init_list_type_node)
20621 /* We can't deduce anything from this, but we might get all the
20622 template args from other function args. */
20623 return unify_success (explain_p);
20625 if (parm == any_targ_node || arg == any_targ_node)
20626 return unify_success (explain_p);
20628 /* If PARM uses template parameters, then we can't bail out here,
20629 even if ARG == PARM, since we won't record unifications for the
20630 template parameters. We might need them if we're trying to
20631 figure out which of two things is more specialized. */
20632 if (arg == parm && !uses_template_parms (parm))
20633 return unify_success (explain_p);
20635 /* Handle init lists early, so the rest of the function can assume
20636 we're dealing with a type. */
20637 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20639 tree elt, elttype;
20640 unsigned i;
20641 tree orig_parm = parm;
20643 /* Replace T with std::initializer_list<T> for deduction. */
20644 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20645 && flag_deduce_init_list)
20646 parm = listify (parm);
20648 if (!is_std_init_list (parm)
20649 && TREE_CODE (parm) != ARRAY_TYPE)
20650 /* We can only deduce from an initializer list argument if the
20651 parameter is std::initializer_list or an array; otherwise this
20652 is a non-deduced context. */
20653 return unify_success (explain_p);
20655 if (TREE_CODE (parm) == ARRAY_TYPE)
20656 elttype = TREE_TYPE (parm);
20657 else
20659 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20660 /* Deduction is defined in terms of a single type, so just punt
20661 on the (bizarre) std::initializer_list<T...>. */
20662 if (PACK_EXPANSION_P (elttype))
20663 return unify_success (explain_p);
20666 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20668 int elt_strict = strict;
20670 if (elt == error_mark_node)
20671 return unify_invalid (explain_p);
20673 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20675 tree type = TREE_TYPE (elt);
20676 if (type == error_mark_node)
20677 return unify_invalid (explain_p);
20678 /* It should only be possible to get here for a call. */
20679 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20680 elt_strict |= maybe_adjust_types_for_deduction
20681 (DEDUCE_CALL, &elttype, &type, elt);
20682 elt = type;
20685 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20686 explain_p);
20689 if (TREE_CODE (parm) == ARRAY_TYPE
20690 && deducible_array_bound (TYPE_DOMAIN (parm)))
20692 /* Also deduce from the length of the initializer list. */
20693 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20694 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20695 if (idx == error_mark_node)
20696 return unify_invalid (explain_p);
20697 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20698 idx, explain_p);
20701 /* If the std::initializer_list<T> deduction worked, replace the
20702 deduced A with std::initializer_list<A>. */
20703 if (orig_parm != parm)
20705 idx = TEMPLATE_TYPE_IDX (orig_parm);
20706 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20707 targ = listify (targ);
20708 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20710 return unify_success (explain_p);
20713 /* If parm and arg aren't the same kind of thing (template, type, or
20714 expression), fail early. */
20715 if (pa_kind (parm) != pa_kind (arg))
20716 return unify_invalid (explain_p);
20718 /* Immediately reject some pairs that won't unify because of
20719 cv-qualification mismatches. */
20720 if (TREE_CODE (arg) == TREE_CODE (parm)
20721 && TYPE_P (arg)
20722 /* It is the elements of the array which hold the cv quals of an array
20723 type, and the elements might be template type parms. We'll check
20724 when we recurse. */
20725 && TREE_CODE (arg) != ARRAY_TYPE
20726 /* We check the cv-qualifiers when unifying with template type
20727 parameters below. We want to allow ARG `const T' to unify with
20728 PARM `T' for example, when computing which of two templates
20729 is more specialized, for example. */
20730 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20731 && !check_cv_quals_for_unify (strict_in, arg, parm))
20732 return unify_cv_qual_mismatch (explain_p, parm, arg);
20734 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20735 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20736 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20737 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20738 strict &= ~UNIFY_ALLOW_DERIVED;
20739 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20740 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20742 switch (TREE_CODE (parm))
20744 case TYPENAME_TYPE:
20745 case SCOPE_REF:
20746 case UNBOUND_CLASS_TEMPLATE:
20747 /* In a type which contains a nested-name-specifier, template
20748 argument values cannot be deduced for template parameters used
20749 within the nested-name-specifier. */
20750 return unify_success (explain_p);
20752 case TEMPLATE_TYPE_PARM:
20753 case TEMPLATE_TEMPLATE_PARM:
20754 case BOUND_TEMPLATE_TEMPLATE_PARM:
20755 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20756 if (error_operand_p (tparm))
20757 return unify_invalid (explain_p);
20759 if (TEMPLATE_TYPE_LEVEL (parm)
20760 != template_decl_level (tparm))
20761 /* The PARM is not one we're trying to unify. Just check
20762 to see if it matches ARG. */
20764 if (TREE_CODE (arg) == TREE_CODE (parm)
20765 && (is_auto (parm) ? is_auto (arg)
20766 : same_type_p (parm, arg)))
20767 return unify_success (explain_p);
20768 else
20769 return unify_type_mismatch (explain_p, parm, arg);
20771 idx = TEMPLATE_TYPE_IDX (parm);
20772 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20773 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20774 if (error_operand_p (tparm))
20775 return unify_invalid (explain_p);
20777 /* Check for mixed types and values. */
20778 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20779 && TREE_CODE (tparm) != TYPE_DECL)
20780 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20781 && TREE_CODE (tparm) != TEMPLATE_DECL))
20782 gcc_unreachable ();
20784 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20786 if ((strict_in & UNIFY_ALLOW_DERIVED)
20787 && CLASS_TYPE_P (arg))
20789 /* First try to match ARG directly. */
20790 tree t = try_class_unification (tparms, targs, parm, arg,
20791 explain_p);
20792 if (!t)
20794 /* Otherwise, look for a suitable base of ARG, as below. */
20795 enum template_base_result r;
20796 r = get_template_base (tparms, targs, parm, arg,
20797 explain_p, &t);
20798 if (!t)
20799 return unify_no_common_base (explain_p, r, parm, arg);
20800 arg = t;
20803 /* ARG must be constructed from a template class or a template
20804 template parameter. */
20805 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20806 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20807 return unify_template_deduction_failure (explain_p, parm, arg);
20809 /* Deduce arguments T, i from TT<T> or TT<i>. */
20810 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20811 return 1;
20813 arg = TYPE_TI_TEMPLATE (arg);
20815 /* Fall through to deduce template name. */
20818 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20819 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20821 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20823 /* Simple cases: Value already set, does match or doesn't. */
20824 if (targ != NULL_TREE && template_args_equal (targ, arg))
20825 return unify_success (explain_p);
20826 else if (targ)
20827 return unify_inconsistency (explain_p, parm, targ, arg);
20829 else
20831 /* If PARM is `const T' and ARG is only `int', we don't have
20832 a match unless we are allowing additional qualification.
20833 If ARG is `const int' and PARM is just `T' that's OK;
20834 that binds `const int' to `T'. */
20835 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20836 arg, parm))
20837 return unify_cv_qual_mismatch (explain_p, parm, arg);
20839 /* Consider the case where ARG is `const volatile int' and
20840 PARM is `const T'. Then, T should be `volatile int'. */
20841 arg = cp_build_qualified_type_real
20842 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20843 if (arg == error_mark_node)
20844 return unify_invalid (explain_p);
20846 /* Simple cases: Value already set, does match or doesn't. */
20847 if (targ != NULL_TREE && same_type_p (targ, arg))
20848 return unify_success (explain_p);
20849 else if (targ)
20850 return unify_inconsistency (explain_p, parm, targ, arg);
20852 /* Make sure that ARG is not a variable-sized array. (Note
20853 that were talking about variable-sized arrays (like
20854 `int[n]'), rather than arrays of unknown size (like
20855 `int[]').) We'll get very confused by such a type since
20856 the bound of the array is not constant, and therefore
20857 not mangleable. Besides, such types are not allowed in
20858 ISO C++, so we can do as we please here. We do allow
20859 them for 'auto' deduction, since that isn't ABI-exposed. */
20860 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20861 return unify_vla_arg (explain_p, arg);
20863 /* Strip typedefs as in convert_template_argument. */
20864 arg = canonicalize_type_argument (arg, tf_none);
20867 /* If ARG is a parameter pack or an expansion, we cannot unify
20868 against it unless PARM is also a parameter pack. */
20869 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20870 && !template_parameter_pack_p (parm))
20871 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20873 /* If the argument deduction results is a METHOD_TYPE,
20874 then there is a problem.
20875 METHOD_TYPE doesn't map to any real C++ type the result of
20876 the deduction can not be of that type. */
20877 if (TREE_CODE (arg) == METHOD_TYPE)
20878 return unify_method_type_error (explain_p, arg);
20880 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20881 return unify_success (explain_p);
20883 case TEMPLATE_PARM_INDEX:
20884 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20885 if (error_operand_p (tparm))
20886 return unify_invalid (explain_p);
20888 if (TEMPLATE_PARM_LEVEL (parm)
20889 != template_decl_level (tparm))
20891 /* The PARM is not one we're trying to unify. Just check
20892 to see if it matches ARG. */
20893 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20894 && cp_tree_equal (parm, arg));
20895 if (result)
20896 unify_expression_unequal (explain_p, parm, arg);
20897 return result;
20900 idx = TEMPLATE_PARM_IDX (parm);
20901 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20903 if (targ)
20905 int x = !cp_tree_equal (targ, arg);
20906 if (x)
20907 unify_inconsistency (explain_p, parm, targ, arg);
20908 return x;
20911 /* [temp.deduct.type] If, in the declaration of a function template
20912 with a non-type template-parameter, the non-type
20913 template-parameter is used in an expression in the function
20914 parameter-list and, if the corresponding template-argument is
20915 deduced, the template-argument type shall match the type of the
20916 template-parameter exactly, except that a template-argument
20917 deduced from an array bound may be of any integral type.
20918 The non-type parameter might use already deduced type parameters. */
20919 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20920 if (tree a = type_uses_auto (tparm))
20922 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20923 if (tparm == error_mark_node)
20924 return 1;
20927 if (!TREE_TYPE (arg))
20928 /* Template-parameter dependent expression. Just accept it for now.
20929 It will later be processed in convert_template_argument. */
20931 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20932 non_reference (tparm)))
20933 /* OK */;
20934 else if ((strict & UNIFY_ALLOW_INTEGER)
20935 && CP_INTEGRAL_TYPE_P (tparm))
20936 /* Convert the ARG to the type of PARM; the deduced non-type
20937 template argument must exactly match the types of the
20938 corresponding parameter. */
20939 arg = fold (build_nop (tparm, arg));
20940 else if (uses_template_parms (tparm))
20942 /* We haven't deduced the type of this parameter yet. */
20943 if (cxx_dialect >= cxx17
20944 /* We deduce from array bounds in try_array_deduction. */
20945 && !(strict & UNIFY_ALLOW_INTEGER))
20947 /* Deduce it from the non-type argument. */
20948 tree atype = TREE_TYPE (arg);
20949 RECUR_AND_CHECK_FAILURE (tparms, targs,
20950 tparm, atype,
20951 UNIFY_ALLOW_NONE, explain_p);
20953 else
20954 /* Try again later. */
20955 return unify_success (explain_p);
20957 else
20958 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20960 /* If ARG is a parameter pack or an expansion, we cannot unify
20961 against it unless PARM is also a parameter pack. */
20962 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20963 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20964 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20967 bool removed_attr = false;
20968 arg = strip_typedefs_expr (arg, &removed_attr);
20970 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20971 return unify_success (explain_p);
20973 case PTRMEM_CST:
20975 /* A pointer-to-member constant can be unified only with
20976 another constant. */
20977 if (TREE_CODE (arg) != PTRMEM_CST)
20978 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20980 /* Just unify the class member. It would be useless (and possibly
20981 wrong, depending on the strict flags) to unify also
20982 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20983 arg refer to the same variable, even if through different
20984 classes. For instance:
20986 struct A { int x; };
20987 struct B : A { };
20989 Unification of &A::x and &B::x must succeed. */
20990 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20991 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20994 case POINTER_TYPE:
20996 if (!TYPE_PTR_P (arg))
20997 return unify_type_mismatch (explain_p, parm, arg);
20999 /* [temp.deduct.call]
21001 A can be another pointer or pointer to member type that can
21002 be converted to the deduced A via a qualification
21003 conversion (_conv.qual_).
21005 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21006 This will allow for additional cv-qualification of the
21007 pointed-to types if appropriate. */
21009 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21010 /* The derived-to-base conversion only persists through one
21011 level of pointers. */
21012 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21014 return unify (tparms, targs, TREE_TYPE (parm),
21015 TREE_TYPE (arg), strict, explain_p);
21018 case REFERENCE_TYPE:
21019 if (TREE_CODE (arg) != REFERENCE_TYPE)
21020 return unify_type_mismatch (explain_p, parm, arg);
21021 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21022 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21024 case ARRAY_TYPE:
21025 if (TREE_CODE (arg) != ARRAY_TYPE)
21026 return unify_type_mismatch (explain_p, parm, arg);
21027 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21028 != (TYPE_DOMAIN (arg) == NULL_TREE))
21029 return unify_type_mismatch (explain_p, parm, arg);
21030 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21031 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21032 if (TYPE_DOMAIN (parm) != NULL_TREE)
21033 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21034 TYPE_DOMAIN (arg), explain_p);
21035 return unify_success (explain_p);
21037 case REAL_TYPE:
21038 case COMPLEX_TYPE:
21039 case VECTOR_TYPE:
21040 case INTEGER_TYPE:
21041 case BOOLEAN_TYPE:
21042 case ENUMERAL_TYPE:
21043 case VOID_TYPE:
21044 case NULLPTR_TYPE:
21045 if (TREE_CODE (arg) != TREE_CODE (parm))
21046 return unify_type_mismatch (explain_p, parm, arg);
21048 /* We have already checked cv-qualification at the top of the
21049 function. */
21050 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21051 return unify_type_mismatch (explain_p, parm, arg);
21053 /* As far as unification is concerned, this wins. Later checks
21054 will invalidate it if necessary. */
21055 return unify_success (explain_p);
21057 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21058 /* Type INTEGER_CST can come from ordinary constant template args. */
21059 case INTEGER_CST:
21060 while (CONVERT_EXPR_P (arg))
21061 arg = TREE_OPERAND (arg, 0);
21063 if (TREE_CODE (arg) != INTEGER_CST)
21064 return unify_template_argument_mismatch (explain_p, parm, arg);
21065 return (tree_int_cst_equal (parm, arg)
21066 ? unify_success (explain_p)
21067 : unify_template_argument_mismatch (explain_p, parm, arg));
21069 case TREE_VEC:
21071 int i, len, argslen;
21072 int parm_variadic_p = 0;
21074 if (TREE_CODE (arg) != TREE_VEC)
21075 return unify_template_argument_mismatch (explain_p, parm, arg);
21077 len = TREE_VEC_LENGTH (parm);
21078 argslen = TREE_VEC_LENGTH (arg);
21080 /* Check for pack expansions in the parameters. */
21081 for (i = 0; i < len; ++i)
21083 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21085 if (i == len - 1)
21086 /* We can unify against something with a trailing
21087 parameter pack. */
21088 parm_variadic_p = 1;
21089 else
21090 /* [temp.deduct.type]/9: If the template argument list of
21091 P contains a pack expansion that is not the last
21092 template argument, the entire template argument list
21093 is a non-deduced context. */
21094 return unify_success (explain_p);
21098 /* If we don't have enough arguments to satisfy the parameters
21099 (not counting the pack expression at the end), or we have
21100 too many arguments for a parameter list that doesn't end in
21101 a pack expression, we can't unify. */
21102 if (parm_variadic_p
21103 ? argslen < len - parm_variadic_p
21104 : argslen != len)
21105 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21107 /* Unify all of the parameters that precede the (optional)
21108 pack expression. */
21109 for (i = 0; i < len - parm_variadic_p; ++i)
21111 RECUR_AND_CHECK_FAILURE (tparms, targs,
21112 TREE_VEC_ELT (parm, i),
21113 TREE_VEC_ELT (arg, i),
21114 UNIFY_ALLOW_NONE, explain_p);
21116 if (parm_variadic_p)
21117 return unify_pack_expansion (tparms, targs, parm, arg,
21118 DEDUCE_EXACT,
21119 /*subr=*/true, explain_p);
21120 return unify_success (explain_p);
21123 case RECORD_TYPE:
21124 case UNION_TYPE:
21125 if (TREE_CODE (arg) != TREE_CODE (parm))
21126 return unify_type_mismatch (explain_p, parm, arg);
21128 if (TYPE_PTRMEMFUNC_P (parm))
21130 if (!TYPE_PTRMEMFUNC_P (arg))
21131 return unify_type_mismatch (explain_p, parm, arg);
21133 return unify (tparms, targs,
21134 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21135 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21136 strict, explain_p);
21138 else if (TYPE_PTRMEMFUNC_P (arg))
21139 return unify_type_mismatch (explain_p, parm, arg);
21141 if (CLASSTYPE_TEMPLATE_INFO (parm))
21143 tree t = NULL_TREE;
21145 if (strict_in & UNIFY_ALLOW_DERIVED)
21147 /* First, we try to unify the PARM and ARG directly. */
21148 t = try_class_unification (tparms, targs,
21149 parm, arg, explain_p);
21151 if (!t)
21153 /* Fallback to the special case allowed in
21154 [temp.deduct.call]:
21156 If P is a class, and P has the form
21157 template-id, then A can be a derived class of
21158 the deduced A. Likewise, if P is a pointer to
21159 a class of the form template-id, A can be a
21160 pointer to a derived class pointed to by the
21161 deduced A. */
21162 enum template_base_result r;
21163 r = get_template_base (tparms, targs, parm, arg,
21164 explain_p, &t);
21166 if (!t)
21168 /* Don't give the derived diagnostic if we're
21169 already dealing with the same template. */
21170 bool same_template
21171 = (CLASSTYPE_TEMPLATE_INFO (arg)
21172 && (CLASSTYPE_TI_TEMPLATE (parm)
21173 == CLASSTYPE_TI_TEMPLATE (arg)));
21174 return unify_no_common_base (explain_p && !same_template,
21175 r, parm, arg);
21179 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21180 && (CLASSTYPE_TI_TEMPLATE (parm)
21181 == CLASSTYPE_TI_TEMPLATE (arg)))
21182 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21183 Then, we should unify `int' and `U'. */
21184 t = arg;
21185 else
21186 /* There's no chance of unification succeeding. */
21187 return unify_type_mismatch (explain_p, parm, arg);
21189 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21190 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21192 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21193 return unify_type_mismatch (explain_p, parm, arg);
21194 return unify_success (explain_p);
21196 case METHOD_TYPE:
21197 case FUNCTION_TYPE:
21199 unsigned int nargs;
21200 tree *args;
21201 tree a;
21202 unsigned int i;
21204 if (TREE_CODE (arg) != TREE_CODE (parm))
21205 return unify_type_mismatch (explain_p, parm, arg);
21207 /* CV qualifications for methods can never be deduced, they must
21208 match exactly. We need to check them explicitly here,
21209 because type_unification_real treats them as any other
21210 cv-qualified parameter. */
21211 if (TREE_CODE (parm) == METHOD_TYPE
21212 && (!check_cv_quals_for_unify
21213 (UNIFY_ALLOW_NONE,
21214 class_of_this_parm (arg),
21215 class_of_this_parm (parm))))
21216 return unify_cv_qual_mismatch (explain_p, parm, arg);
21217 if (TREE_CODE (arg) == FUNCTION_TYPE
21218 && type_memfn_quals (parm) != type_memfn_quals (arg))
21219 return unify_cv_qual_mismatch (explain_p, parm, arg);
21220 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21221 return unify_type_mismatch (explain_p, parm, arg);
21223 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21224 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21226 nargs = list_length (TYPE_ARG_TYPES (arg));
21227 args = XALLOCAVEC (tree, nargs);
21228 for (a = TYPE_ARG_TYPES (arg), i = 0;
21229 a != NULL_TREE && a != void_list_node;
21230 a = TREE_CHAIN (a), ++i)
21231 args[i] = TREE_VALUE (a);
21232 nargs = i;
21234 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21235 args, nargs, 1, DEDUCE_EXACT,
21236 LOOKUP_NORMAL, NULL, explain_p))
21237 return 1;
21239 if (flag_noexcept_type)
21241 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21242 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21243 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21244 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21245 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21246 && uses_template_parms (TREE_PURPOSE (pspec)))
21247 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21248 TREE_PURPOSE (aspec),
21249 UNIFY_ALLOW_NONE, explain_p);
21250 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21251 return unify_type_mismatch (explain_p, parm, arg);
21254 return 0;
21257 case OFFSET_TYPE:
21258 /* Unify a pointer to member with a pointer to member function, which
21259 deduces the type of the member as a function type. */
21260 if (TYPE_PTRMEMFUNC_P (arg))
21262 /* Check top-level cv qualifiers */
21263 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21264 return unify_cv_qual_mismatch (explain_p, parm, arg);
21266 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21267 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21268 UNIFY_ALLOW_NONE, explain_p);
21270 /* Determine the type of the function we are unifying against. */
21271 tree fntype = static_fn_type (arg);
21273 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21276 if (TREE_CODE (arg) != OFFSET_TYPE)
21277 return unify_type_mismatch (explain_p, parm, arg);
21278 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21279 TYPE_OFFSET_BASETYPE (arg),
21280 UNIFY_ALLOW_NONE, explain_p);
21281 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21282 strict, explain_p);
21284 case CONST_DECL:
21285 if (DECL_TEMPLATE_PARM_P (parm))
21286 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21287 if (arg != scalar_constant_value (parm))
21288 return unify_template_argument_mismatch (explain_p, parm, arg);
21289 return unify_success (explain_p);
21291 case FIELD_DECL:
21292 case TEMPLATE_DECL:
21293 /* Matched cases are handled by the ARG == PARM test above. */
21294 return unify_template_argument_mismatch (explain_p, parm, arg);
21296 case VAR_DECL:
21297 /* We might get a variable as a non-type template argument in parm if the
21298 corresponding parameter is type-dependent. Make any necessary
21299 adjustments based on whether arg is a reference. */
21300 if (CONSTANT_CLASS_P (arg))
21301 parm = fold_non_dependent_expr (parm);
21302 else if (REFERENCE_REF_P (arg))
21304 tree sub = TREE_OPERAND (arg, 0);
21305 STRIP_NOPS (sub);
21306 if (TREE_CODE (sub) == ADDR_EXPR)
21307 arg = TREE_OPERAND (sub, 0);
21309 /* Now use the normal expression code to check whether they match. */
21310 goto expr;
21312 case TYPE_ARGUMENT_PACK:
21313 case NONTYPE_ARGUMENT_PACK:
21314 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21315 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21317 case TYPEOF_TYPE:
21318 case DECLTYPE_TYPE:
21319 case UNDERLYING_TYPE:
21320 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21321 or UNDERLYING_TYPE nodes. */
21322 return unify_success (explain_p);
21324 case ERROR_MARK:
21325 /* Unification fails if we hit an error node. */
21326 return unify_invalid (explain_p);
21328 case INDIRECT_REF:
21329 if (REFERENCE_REF_P (parm))
21331 bool pexp = PACK_EXPANSION_P (arg);
21332 if (pexp)
21333 arg = PACK_EXPANSION_PATTERN (arg);
21334 if (REFERENCE_REF_P (arg))
21335 arg = TREE_OPERAND (arg, 0);
21336 if (pexp)
21337 arg = make_pack_expansion (arg);
21338 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21339 strict, explain_p);
21341 /* FALLTHRU */
21343 default:
21344 /* An unresolved overload is a nondeduced context. */
21345 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21346 return unify_success (explain_p);
21347 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21348 expr:
21349 /* We must be looking at an expression. This can happen with
21350 something like:
21352 template <int I>
21353 void foo(S<I>, S<I + 2>);
21355 This is a "nondeduced context":
21357 [deduct.type]
21359 The nondeduced contexts are:
21361 --A type that is a template-id in which one or more of
21362 the template-arguments is an expression that references
21363 a template-parameter.
21365 In these cases, we assume deduction succeeded, but don't
21366 actually infer any unifications. */
21368 if (!uses_template_parms (parm)
21369 && !template_args_equal (parm, arg))
21370 return unify_expression_unequal (explain_p, parm, arg);
21371 else
21372 return unify_success (explain_p);
21375 #undef RECUR_AND_CHECK_FAILURE
21377 /* Note that DECL can be defined in this translation unit, if
21378 required. */
21380 static void
21381 mark_definable (tree decl)
21383 tree clone;
21384 DECL_NOT_REALLY_EXTERN (decl) = 1;
21385 FOR_EACH_CLONE (clone, decl)
21386 DECL_NOT_REALLY_EXTERN (clone) = 1;
21389 /* Called if RESULT is explicitly instantiated, or is a member of an
21390 explicitly instantiated class. */
21392 void
21393 mark_decl_instantiated (tree result, int extern_p)
21395 SET_DECL_EXPLICIT_INSTANTIATION (result);
21397 /* If this entity has already been written out, it's too late to
21398 make any modifications. */
21399 if (TREE_ASM_WRITTEN (result))
21400 return;
21402 /* For anonymous namespace we don't need to do anything. */
21403 if (decl_anon_ns_mem_p (result))
21405 gcc_assert (!TREE_PUBLIC (result));
21406 return;
21409 if (TREE_CODE (result) != FUNCTION_DECL)
21410 /* The TREE_PUBLIC flag for function declarations will have been
21411 set correctly by tsubst. */
21412 TREE_PUBLIC (result) = 1;
21414 /* This might have been set by an earlier implicit instantiation. */
21415 DECL_COMDAT (result) = 0;
21417 if (extern_p)
21418 DECL_NOT_REALLY_EXTERN (result) = 0;
21419 else
21421 mark_definable (result);
21422 mark_needed (result);
21423 /* Always make artificials weak. */
21424 if (DECL_ARTIFICIAL (result) && flag_weak)
21425 comdat_linkage (result);
21426 /* For WIN32 we also want to put explicit instantiations in
21427 linkonce sections. */
21428 else if (TREE_PUBLIC (result))
21429 maybe_make_one_only (result);
21432 /* If EXTERN_P, then this function will not be emitted -- unless
21433 followed by an explicit instantiation, at which point its linkage
21434 will be adjusted. If !EXTERN_P, then this function will be
21435 emitted here. In neither circumstance do we want
21436 import_export_decl to adjust the linkage. */
21437 DECL_INTERFACE_KNOWN (result) = 1;
21440 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21441 important template arguments. If any are missing, we check whether
21442 they're important by using error_mark_node for substituting into any
21443 args that were used for partial ordering (the ones between ARGS and END)
21444 and seeing if it bubbles up. */
21446 static bool
21447 check_undeduced_parms (tree targs, tree args, tree end)
21449 bool found = false;
21450 int i;
21451 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21452 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21454 found = true;
21455 TREE_VEC_ELT (targs, i) = error_mark_node;
21457 if (found)
21459 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21460 if (substed == error_mark_node)
21461 return true;
21463 return false;
21466 /* Given two function templates PAT1 and PAT2, return:
21468 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21469 -1 if PAT2 is more specialized than PAT1.
21470 0 if neither is more specialized.
21472 LEN indicates the number of parameters we should consider
21473 (defaulted parameters should not be considered).
21475 The 1998 std underspecified function template partial ordering, and
21476 DR214 addresses the issue. We take pairs of arguments, one from
21477 each of the templates, and deduce them against each other. One of
21478 the templates will be more specialized if all the *other*
21479 template's arguments deduce against its arguments and at least one
21480 of its arguments *does* *not* deduce against the other template's
21481 corresponding argument. Deduction is done as for class templates.
21482 The arguments used in deduction have reference and top level cv
21483 qualifiers removed. Iff both arguments were originally reference
21484 types *and* deduction succeeds in both directions, an lvalue reference
21485 wins against an rvalue reference and otherwise the template
21486 with the more cv-qualified argument wins for that pairing (if
21487 neither is more cv-qualified, they both are equal). Unlike regular
21488 deduction, after all the arguments have been deduced in this way,
21489 we do *not* verify the deduced template argument values can be
21490 substituted into non-deduced contexts.
21492 The logic can be a bit confusing here, because we look at deduce1 and
21493 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21494 can find template arguments for pat1 to make arg1 look like arg2, that
21495 means that arg2 is at least as specialized as arg1. */
21498 more_specialized_fn (tree pat1, tree pat2, int len)
21500 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21501 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21502 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21503 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21504 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21505 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21506 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21507 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21508 tree origs1, origs2;
21509 bool lose1 = false;
21510 bool lose2 = false;
21512 /* Remove the this parameter from non-static member functions. If
21513 one is a non-static member function and the other is not a static
21514 member function, remove the first parameter from that function
21515 also. This situation occurs for operator functions where we
21516 locate both a member function (with this pointer) and non-member
21517 operator (with explicit first operand). */
21518 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21520 len--; /* LEN is the number of significant arguments for DECL1 */
21521 args1 = TREE_CHAIN (args1);
21522 if (!DECL_STATIC_FUNCTION_P (decl2))
21523 args2 = TREE_CHAIN (args2);
21525 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21527 args2 = TREE_CHAIN (args2);
21528 if (!DECL_STATIC_FUNCTION_P (decl1))
21530 len--;
21531 args1 = TREE_CHAIN (args1);
21535 /* If only one is a conversion operator, they are unordered. */
21536 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21537 return 0;
21539 /* Consider the return type for a conversion function */
21540 if (DECL_CONV_FN_P (decl1))
21542 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21543 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21544 len++;
21547 processing_template_decl++;
21549 origs1 = args1;
21550 origs2 = args2;
21552 while (len--
21553 /* Stop when an ellipsis is seen. */
21554 && args1 != NULL_TREE && args2 != NULL_TREE)
21556 tree arg1 = TREE_VALUE (args1);
21557 tree arg2 = TREE_VALUE (args2);
21558 int deduce1, deduce2;
21559 int quals1 = -1;
21560 int quals2 = -1;
21561 int ref1 = 0;
21562 int ref2 = 0;
21564 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21565 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21567 /* When both arguments are pack expansions, we need only
21568 unify the patterns themselves. */
21569 arg1 = PACK_EXPANSION_PATTERN (arg1);
21570 arg2 = PACK_EXPANSION_PATTERN (arg2);
21572 /* This is the last comparison we need to do. */
21573 len = 0;
21576 /* DR 1847: If a particular P contains no template-parameters that
21577 participate in template argument deduction, that P is not used to
21578 determine the ordering. */
21579 if (!uses_deducible_template_parms (arg1)
21580 && !uses_deducible_template_parms (arg2))
21581 goto next;
21583 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21585 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21586 arg1 = TREE_TYPE (arg1);
21587 quals1 = cp_type_quals (arg1);
21590 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21592 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21593 arg2 = TREE_TYPE (arg2);
21594 quals2 = cp_type_quals (arg2);
21597 arg1 = TYPE_MAIN_VARIANT (arg1);
21598 arg2 = TYPE_MAIN_VARIANT (arg2);
21600 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21602 int i, len2 = remaining_arguments (args2);
21603 tree parmvec = make_tree_vec (1);
21604 tree argvec = make_tree_vec (len2);
21605 tree ta = args2;
21607 /* Setup the parameter vector, which contains only ARG1. */
21608 TREE_VEC_ELT (parmvec, 0) = arg1;
21610 /* Setup the argument vector, which contains the remaining
21611 arguments. */
21612 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21613 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21615 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21616 argvec, DEDUCE_EXACT,
21617 /*subr=*/true, /*explain_p=*/false)
21618 == 0);
21620 /* We cannot deduce in the other direction, because ARG1 is
21621 a pack expansion but ARG2 is not. */
21622 deduce2 = 0;
21624 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21626 int i, len1 = remaining_arguments (args1);
21627 tree parmvec = make_tree_vec (1);
21628 tree argvec = make_tree_vec (len1);
21629 tree ta = args1;
21631 /* Setup the parameter vector, which contains only ARG1. */
21632 TREE_VEC_ELT (parmvec, 0) = arg2;
21634 /* Setup the argument vector, which contains the remaining
21635 arguments. */
21636 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21637 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21639 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21640 argvec, DEDUCE_EXACT,
21641 /*subr=*/true, /*explain_p=*/false)
21642 == 0);
21644 /* We cannot deduce in the other direction, because ARG2 is
21645 a pack expansion but ARG1 is not.*/
21646 deduce1 = 0;
21649 else
21651 /* The normal case, where neither argument is a pack
21652 expansion. */
21653 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21654 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21655 == 0);
21656 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21657 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21658 == 0);
21661 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21662 arg2, then arg2 is not as specialized as arg1. */
21663 if (!deduce1)
21664 lose2 = true;
21665 if (!deduce2)
21666 lose1 = true;
21668 /* "If, for a given type, deduction succeeds in both directions
21669 (i.e., the types are identical after the transformations above)
21670 and both P and A were reference types (before being replaced with
21671 the type referred to above):
21672 - if the type from the argument template was an lvalue reference and
21673 the type from the parameter template was not, the argument type is
21674 considered to be more specialized than the other; otherwise,
21675 - if the type from the argument template is more cv-qualified
21676 than the type from the parameter template (as described above),
21677 the argument type is considered to be more specialized than the other;
21678 otherwise,
21679 - neither type is more specialized than the other." */
21681 if (deduce1 && deduce2)
21683 if (ref1 && ref2 && ref1 != ref2)
21685 if (ref1 > ref2)
21686 lose1 = true;
21687 else
21688 lose2 = true;
21690 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21692 if ((quals1 & quals2) == quals2)
21693 lose2 = true;
21694 if ((quals1 & quals2) == quals1)
21695 lose1 = true;
21699 if (lose1 && lose2)
21700 /* We've failed to deduce something in either direction.
21701 These must be unordered. */
21702 break;
21704 next:
21706 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21707 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21708 /* We have already processed all of the arguments in our
21709 handing of the pack expansion type. */
21710 len = 0;
21712 args1 = TREE_CHAIN (args1);
21713 args2 = TREE_CHAIN (args2);
21716 /* "In most cases, all template parameters must have values in order for
21717 deduction to succeed, but for partial ordering purposes a template
21718 parameter may remain without a value provided it is not used in the
21719 types being used for partial ordering."
21721 Thus, if we are missing any of the targs1 we need to substitute into
21722 origs1, then pat2 is not as specialized as pat1. This can happen when
21723 there is a nondeduced context. */
21724 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21725 lose2 = true;
21726 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21727 lose1 = true;
21729 processing_template_decl--;
21731 /* If both deductions succeed, the partial ordering selects the more
21732 constrained template. */
21733 if (!lose1 && !lose2)
21735 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21736 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21737 lose1 = !subsumes_constraints (c1, c2);
21738 lose2 = !subsumes_constraints (c2, c1);
21741 /* All things being equal, if the next argument is a pack expansion
21742 for one function but not for the other, prefer the
21743 non-variadic function. FIXME this is bogus; see c++/41958. */
21744 if (lose1 == lose2
21745 && args1 && TREE_VALUE (args1)
21746 && args2 && TREE_VALUE (args2))
21748 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21749 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21752 if (lose1 == lose2)
21753 return 0;
21754 else if (!lose1)
21755 return 1;
21756 else
21757 return -1;
21760 /* Determine which of two partial specializations of TMPL is more
21761 specialized.
21763 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21764 to the first partial specialization. The TREE_PURPOSE is the
21765 innermost set of template parameters for the partial
21766 specialization. PAT2 is similar, but for the second template.
21768 Return 1 if the first partial specialization is more specialized;
21769 -1 if the second is more specialized; 0 if neither is more
21770 specialized.
21772 See [temp.class.order] for information about determining which of
21773 two templates is more specialized. */
21775 static int
21776 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21778 tree targs;
21779 int winner = 0;
21780 bool any_deductions = false;
21782 tree tmpl1 = TREE_VALUE (pat1);
21783 tree tmpl2 = TREE_VALUE (pat2);
21784 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21785 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21787 /* Just like what happens for functions, if we are ordering between
21788 different template specializations, we may encounter dependent
21789 types in the arguments, and we need our dependency check functions
21790 to behave correctly. */
21791 ++processing_template_decl;
21792 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21793 if (targs)
21795 --winner;
21796 any_deductions = true;
21799 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21800 if (targs)
21802 ++winner;
21803 any_deductions = true;
21805 --processing_template_decl;
21807 /* If both deductions succeed, the partial ordering selects the more
21808 constrained template. */
21809 if (!winner && any_deductions)
21810 return more_constrained (tmpl1, tmpl2);
21812 /* In the case of a tie where at least one of the templates
21813 has a parameter pack at the end, the template with the most
21814 non-packed parameters wins. */
21815 if (winner == 0
21816 && any_deductions
21817 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21818 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21820 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21821 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21822 int len1 = TREE_VEC_LENGTH (args1);
21823 int len2 = TREE_VEC_LENGTH (args2);
21825 /* We don't count the pack expansion at the end. */
21826 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21827 --len1;
21828 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21829 --len2;
21831 if (len1 > len2)
21832 return 1;
21833 else if (len1 < len2)
21834 return -1;
21837 return winner;
21840 /* Return the template arguments that will produce the function signature
21841 DECL from the function template FN, with the explicit template
21842 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21843 also match. Return NULL_TREE if no satisfactory arguments could be
21844 found. */
21846 static tree
21847 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21849 int ntparms = DECL_NTPARMS (fn);
21850 tree targs = make_tree_vec (ntparms);
21851 tree decl_type = TREE_TYPE (decl);
21852 tree decl_arg_types;
21853 tree *args;
21854 unsigned int nargs, ix;
21855 tree arg;
21857 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21859 /* Never do unification on the 'this' parameter. */
21860 decl_arg_types = skip_artificial_parms_for (decl,
21861 TYPE_ARG_TYPES (decl_type));
21863 nargs = list_length (decl_arg_types);
21864 args = XALLOCAVEC (tree, nargs);
21865 for (arg = decl_arg_types, ix = 0;
21866 arg != NULL_TREE && arg != void_list_node;
21867 arg = TREE_CHAIN (arg), ++ix)
21868 args[ix] = TREE_VALUE (arg);
21870 if (fn_type_unification (fn, explicit_args, targs,
21871 args, ix,
21872 (check_rettype || DECL_CONV_FN_P (fn)
21873 ? TREE_TYPE (decl_type) : NULL_TREE),
21874 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21875 /*decltype*/false)
21876 == error_mark_node)
21877 return NULL_TREE;
21879 return targs;
21882 /* Return the innermost template arguments that, when applied to a partial
21883 specialization SPEC_TMPL of TMPL, yield the ARGS.
21885 For example, suppose we have:
21887 template <class T, class U> struct S {};
21888 template <class T> struct S<T*, int> {};
21890 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21891 partial specialization and the ARGS will be {double*, int}. The resulting
21892 vector will be {double}, indicating that `T' is bound to `double'. */
21894 static tree
21895 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21897 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21898 tree spec_args
21899 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21900 int i, ntparms = TREE_VEC_LENGTH (tparms);
21901 tree deduced_args;
21902 tree innermost_deduced_args;
21904 innermost_deduced_args = make_tree_vec (ntparms);
21905 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21907 deduced_args = copy_node (args);
21908 SET_TMPL_ARGS_LEVEL (deduced_args,
21909 TMPL_ARGS_DEPTH (deduced_args),
21910 innermost_deduced_args);
21912 else
21913 deduced_args = innermost_deduced_args;
21915 bool tried_array_deduction = (cxx_dialect < cxx17);
21916 again:
21917 if (unify (tparms, deduced_args,
21918 INNERMOST_TEMPLATE_ARGS (spec_args),
21919 INNERMOST_TEMPLATE_ARGS (args),
21920 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21921 return NULL_TREE;
21923 for (i = 0; i < ntparms; ++i)
21924 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21926 if (!tried_array_deduction)
21928 try_array_deduction (tparms, innermost_deduced_args,
21929 INNERMOST_TEMPLATE_ARGS (spec_args));
21930 tried_array_deduction = true;
21931 if (TREE_VEC_ELT (innermost_deduced_args, i))
21932 goto again;
21934 return NULL_TREE;
21937 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21938 if (!push_tinst_level (tinst))
21940 excessive_deduction_depth = true;
21941 return NULL_TREE;
21944 /* Verify that nondeduced template arguments agree with the type
21945 obtained from argument deduction.
21947 For example:
21949 struct A { typedef int X; };
21950 template <class T, class U> struct C {};
21951 template <class T> struct C<T, typename T::X> {};
21953 Then with the instantiation `C<A, int>', we can deduce that
21954 `T' is `A' but unify () does not check whether `typename T::X'
21955 is `int'. */
21956 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21958 if (spec_args != error_mark_node)
21959 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21960 INNERMOST_TEMPLATE_ARGS (spec_args),
21961 tmpl, tf_none, false, false);
21963 pop_tinst_level ();
21965 if (spec_args == error_mark_node
21966 /* We only need to check the innermost arguments; the other
21967 arguments will always agree. */
21968 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21969 INNERMOST_TEMPLATE_ARGS (args)))
21970 return NULL_TREE;
21972 /* Now that we have bindings for all of the template arguments,
21973 ensure that the arguments deduced for the template template
21974 parameters have compatible template parameter lists. See the use
21975 of template_template_parm_bindings_ok_p in fn_type_unification
21976 for more information. */
21977 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21978 return NULL_TREE;
21980 return deduced_args;
21983 // Compare two function templates T1 and T2 by deducing bindings
21984 // from one against the other. If both deductions succeed, compare
21985 // constraints to see which is more constrained.
21986 static int
21987 more_specialized_inst (tree t1, tree t2)
21989 int fate = 0;
21990 int count = 0;
21992 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21994 --fate;
21995 ++count;
21998 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22000 ++fate;
22001 ++count;
22004 // If both deductions succeed, then one may be more constrained.
22005 if (count == 2 && fate == 0)
22006 fate = more_constrained (t1, t2);
22008 return fate;
22011 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22012 Return the TREE_LIST node with the most specialized template, if
22013 any. If there is no most specialized template, the error_mark_node
22014 is returned.
22016 Note that this function does not look at, or modify, the
22017 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22018 returned is one of the elements of INSTANTIATIONS, callers may
22019 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22020 and retrieve it from the value returned. */
22022 tree
22023 most_specialized_instantiation (tree templates)
22025 tree fn, champ;
22027 ++processing_template_decl;
22029 champ = templates;
22030 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22032 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22033 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22034 if (fate == -1)
22035 champ = fn;
22036 else if (!fate)
22038 /* Equally specialized, move to next function. If there
22039 is no next function, nothing's most specialized. */
22040 fn = TREE_CHAIN (fn);
22041 champ = fn;
22042 if (!fn)
22043 break;
22047 if (champ)
22048 /* Now verify that champ is better than everything earlier in the
22049 instantiation list. */
22050 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22051 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22053 champ = NULL_TREE;
22054 break;
22058 processing_template_decl--;
22060 if (!champ)
22061 return error_mark_node;
22063 return champ;
22066 /* If DECL is a specialization of some template, return the most
22067 general such template. Otherwise, returns NULL_TREE.
22069 For example, given:
22071 template <class T> struct S { template <class U> void f(U); };
22073 if TMPL is `template <class U> void S<int>::f(U)' this will return
22074 the full template. This function will not trace past partial
22075 specializations, however. For example, given in addition:
22077 template <class T> struct S<T*> { template <class U> void f(U); };
22079 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22080 `template <class T> template <class U> S<T*>::f(U)'. */
22082 tree
22083 most_general_template (tree decl)
22085 if (TREE_CODE (decl) != TEMPLATE_DECL)
22087 if (tree tinfo = get_template_info (decl))
22088 decl = TI_TEMPLATE (tinfo);
22089 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22090 template friend, or a FIELD_DECL for a capture pack. */
22091 if (TREE_CODE (decl) != TEMPLATE_DECL)
22092 return NULL_TREE;
22095 /* Look for more and more general templates. */
22096 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22098 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22099 (See cp-tree.h for details.) */
22100 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22101 break;
22103 if (CLASS_TYPE_P (TREE_TYPE (decl))
22104 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22105 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22106 break;
22108 /* Stop if we run into an explicitly specialized class template. */
22109 if (!DECL_NAMESPACE_SCOPE_P (decl)
22110 && DECL_CONTEXT (decl)
22111 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22112 break;
22114 decl = DECL_TI_TEMPLATE (decl);
22117 return decl;
22120 /* Return the most specialized of the template partial specializations
22121 which can produce TARGET, a specialization of some class or variable
22122 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22123 a TEMPLATE_DECL node corresponding to the partial specialization, while
22124 the TREE_PURPOSE is the set of template arguments that must be
22125 substituted into the template pattern in order to generate TARGET.
22127 If the choice of partial specialization is ambiguous, a diagnostic
22128 is issued, and the error_mark_node is returned. If there are no
22129 partial specializations matching TARGET, then NULL_TREE is
22130 returned, indicating that the primary template should be used. */
22132 static tree
22133 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22135 tree list = NULL_TREE;
22136 tree t;
22137 tree champ;
22138 int fate;
22139 bool ambiguous_p;
22140 tree outer_args = NULL_TREE;
22141 tree tmpl, args;
22143 if (TYPE_P (target))
22145 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22146 tmpl = TI_TEMPLATE (tinfo);
22147 args = TI_ARGS (tinfo);
22149 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22151 tmpl = TREE_OPERAND (target, 0);
22152 args = TREE_OPERAND (target, 1);
22154 else if (VAR_P (target))
22156 tree tinfo = DECL_TEMPLATE_INFO (target);
22157 tmpl = TI_TEMPLATE (tinfo);
22158 args = TI_ARGS (tinfo);
22160 else
22161 gcc_unreachable ();
22163 tree main_tmpl = most_general_template (tmpl);
22165 /* For determining which partial specialization to use, only the
22166 innermost args are interesting. */
22167 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22169 outer_args = strip_innermost_template_args (args, 1);
22170 args = INNERMOST_TEMPLATE_ARGS (args);
22173 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22175 tree spec_args;
22176 tree spec_tmpl = TREE_VALUE (t);
22178 if (outer_args)
22180 /* Substitute in the template args from the enclosing class. */
22181 ++processing_template_decl;
22182 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22183 --processing_template_decl;
22186 if (spec_tmpl == error_mark_node)
22187 return error_mark_node;
22189 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22190 if (spec_args)
22192 if (outer_args)
22193 spec_args = add_to_template_args (outer_args, spec_args);
22195 /* Keep the candidate only if the constraints are satisfied,
22196 or if we're not compiling with concepts. */
22197 if (!flag_concepts
22198 || constraints_satisfied_p (spec_tmpl, spec_args))
22200 list = tree_cons (spec_args, TREE_VALUE (t), list);
22201 TREE_TYPE (list) = TREE_TYPE (t);
22206 if (! list)
22207 return NULL_TREE;
22209 ambiguous_p = false;
22210 t = list;
22211 champ = t;
22212 t = TREE_CHAIN (t);
22213 for (; t; t = TREE_CHAIN (t))
22215 fate = more_specialized_partial_spec (tmpl, champ, t);
22216 if (fate == 1)
22218 else
22220 if (fate == 0)
22222 t = TREE_CHAIN (t);
22223 if (! t)
22225 ambiguous_p = true;
22226 break;
22229 champ = t;
22233 if (!ambiguous_p)
22234 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22236 fate = more_specialized_partial_spec (tmpl, champ, t);
22237 if (fate != 1)
22239 ambiguous_p = true;
22240 break;
22244 if (ambiguous_p)
22246 const char *str;
22247 char *spaces = NULL;
22248 if (!(complain & tf_error))
22249 return error_mark_node;
22250 if (TYPE_P (target))
22251 error ("ambiguous template instantiation for %q#T", target);
22252 else
22253 error ("ambiguous template instantiation for %q#D", target);
22254 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22255 for (t = list; t; t = TREE_CHAIN (t))
22257 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22258 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22259 "%s %#qS", spaces ? spaces : str, subst);
22260 spaces = spaces ? spaces : get_spaces (str);
22262 free (spaces);
22263 return error_mark_node;
22266 return champ;
22269 /* Explicitly instantiate DECL. */
22271 void
22272 do_decl_instantiation (tree decl, tree storage)
22274 tree result = NULL_TREE;
22275 int extern_p = 0;
22277 if (!decl || decl == error_mark_node)
22278 /* An error occurred, for which grokdeclarator has already issued
22279 an appropriate message. */
22280 return;
22281 else if (! DECL_LANG_SPECIFIC (decl))
22283 error ("explicit instantiation of non-template %q#D", decl);
22284 return;
22287 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22288 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22290 if (VAR_P (decl) && !var_templ)
22292 /* There is an asymmetry here in the way VAR_DECLs and
22293 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22294 the latter, the DECL we get back will be marked as a
22295 template instantiation, and the appropriate
22296 DECL_TEMPLATE_INFO will be set up. This does not happen for
22297 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22298 should handle VAR_DECLs as it currently handles
22299 FUNCTION_DECLs. */
22300 if (!DECL_CLASS_SCOPE_P (decl))
22302 error ("%qD is not a static data member of a class template", decl);
22303 return;
22305 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22306 if (!result || !VAR_P (result))
22308 error ("no matching template for %qD found", decl);
22309 return;
22311 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22313 error ("type %qT for explicit instantiation %qD does not match "
22314 "declared type %qT", TREE_TYPE (result), decl,
22315 TREE_TYPE (decl));
22316 return;
22319 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22321 error ("explicit instantiation of %q#D", decl);
22322 return;
22324 else
22325 result = decl;
22327 /* Check for various error cases. Note that if the explicit
22328 instantiation is valid the RESULT will currently be marked as an
22329 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22330 until we get here. */
22332 if (DECL_TEMPLATE_SPECIALIZATION (result))
22334 /* DR 259 [temp.spec].
22336 Both an explicit instantiation and a declaration of an explicit
22337 specialization shall not appear in a program unless the explicit
22338 instantiation follows a declaration of the explicit specialization.
22340 For a given set of template parameters, if an explicit
22341 instantiation of a template appears after a declaration of an
22342 explicit specialization for that template, the explicit
22343 instantiation has no effect. */
22344 return;
22346 else if (DECL_EXPLICIT_INSTANTIATION (result))
22348 /* [temp.spec]
22350 No program shall explicitly instantiate any template more
22351 than once.
22353 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22354 the first instantiation was `extern' and the second is not,
22355 and EXTERN_P for the opposite case. */
22356 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22357 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22358 /* If an "extern" explicit instantiation follows an ordinary
22359 explicit instantiation, the template is instantiated. */
22360 if (extern_p)
22361 return;
22363 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22365 error ("no matching template for %qD found", result);
22366 return;
22368 else if (!DECL_TEMPLATE_INFO (result))
22370 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22371 return;
22374 if (storage == NULL_TREE)
22376 else if (storage == ridpointers[(int) RID_EXTERN])
22378 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22379 pedwarn (input_location, OPT_Wpedantic,
22380 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22381 "instantiations");
22382 extern_p = 1;
22384 else
22385 error ("storage class %qD applied to template instantiation", storage);
22387 check_explicit_instantiation_namespace (result);
22388 mark_decl_instantiated (result, extern_p);
22389 if (! extern_p)
22390 instantiate_decl (result, /*defer_ok=*/true,
22391 /*expl_inst_class_mem_p=*/false);
22394 static void
22395 mark_class_instantiated (tree t, int extern_p)
22397 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22398 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22399 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22400 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22401 if (! extern_p)
22403 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22404 rest_of_type_compilation (t, 1);
22408 /* Called from do_type_instantiation through binding_table_foreach to
22409 do recursive instantiation for the type bound in ENTRY. */
22410 static void
22411 bt_instantiate_type_proc (binding_entry entry, void *data)
22413 tree storage = *(tree *) data;
22415 if (MAYBE_CLASS_TYPE_P (entry->type)
22416 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22417 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22420 /* Perform an explicit instantiation of template class T. STORAGE, if
22421 non-null, is the RID for extern, inline or static. COMPLAIN is
22422 nonzero if this is called from the parser, zero if called recursively,
22423 since the standard is unclear (as detailed below). */
22425 void
22426 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22428 int extern_p = 0;
22429 int nomem_p = 0;
22430 int static_p = 0;
22431 int previous_instantiation_extern_p = 0;
22433 if (TREE_CODE (t) == TYPE_DECL)
22434 t = TREE_TYPE (t);
22436 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22438 tree tmpl =
22439 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22440 if (tmpl)
22441 error ("explicit instantiation of non-class template %qD", tmpl);
22442 else
22443 error ("explicit instantiation of non-template type %qT", t);
22444 return;
22447 complete_type (t);
22449 if (!COMPLETE_TYPE_P (t))
22451 if (complain & tf_error)
22452 error ("explicit instantiation of %q#T before definition of template",
22454 return;
22457 if (storage != NULL_TREE)
22459 if (!in_system_header_at (input_location))
22461 if (storage == ridpointers[(int) RID_EXTERN])
22463 if (cxx_dialect == cxx98)
22464 pedwarn (input_location, OPT_Wpedantic,
22465 "ISO C++ 1998 forbids the use of %<extern%> on "
22466 "explicit instantiations");
22468 else
22469 pedwarn (input_location, OPT_Wpedantic,
22470 "ISO C++ forbids the use of %qE"
22471 " on explicit instantiations", storage);
22474 if (storage == ridpointers[(int) RID_INLINE])
22475 nomem_p = 1;
22476 else if (storage == ridpointers[(int) RID_EXTERN])
22477 extern_p = 1;
22478 else if (storage == ridpointers[(int) RID_STATIC])
22479 static_p = 1;
22480 else
22482 error ("storage class %qD applied to template instantiation",
22483 storage);
22484 extern_p = 0;
22488 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22490 /* DR 259 [temp.spec].
22492 Both an explicit instantiation and a declaration of an explicit
22493 specialization shall not appear in a program unless the explicit
22494 instantiation follows a declaration of the explicit specialization.
22496 For a given set of template parameters, if an explicit
22497 instantiation of a template appears after a declaration of an
22498 explicit specialization for that template, the explicit
22499 instantiation has no effect. */
22500 return;
22502 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22504 /* [temp.spec]
22506 No program shall explicitly instantiate any template more
22507 than once.
22509 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22510 instantiation was `extern'. If EXTERN_P then the second is.
22511 These cases are OK. */
22512 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22514 if (!previous_instantiation_extern_p && !extern_p
22515 && (complain & tf_error))
22516 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22518 /* If we've already instantiated the template, just return now. */
22519 if (!CLASSTYPE_INTERFACE_ONLY (t))
22520 return;
22523 check_explicit_instantiation_namespace (TYPE_NAME (t));
22524 mark_class_instantiated (t, extern_p);
22526 if (nomem_p)
22527 return;
22529 /* In contrast to implicit instantiation, where only the
22530 declarations, and not the definitions, of members are
22531 instantiated, we have here:
22533 [temp.explicit]
22535 The explicit instantiation of a class template specialization
22536 implies the instantiation of all of its members not
22537 previously explicitly specialized in the translation unit
22538 containing the explicit instantiation.
22540 Of course, we can't instantiate member template classes, since we
22541 don't have any arguments for them. Note that the standard is
22542 unclear on whether the instantiation of the members are
22543 *explicit* instantiations or not. However, the most natural
22544 interpretation is that it should be an explicit
22545 instantiation. */
22546 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22547 if ((VAR_P (fld)
22548 || (TREE_CODE (fld) == FUNCTION_DECL
22549 && !static_p
22550 && user_provided_p (fld)))
22551 && DECL_TEMPLATE_INSTANTIATION (fld))
22553 mark_decl_instantiated (fld, extern_p);
22554 if (! extern_p)
22555 instantiate_decl (fld, /*defer_ok=*/true,
22556 /*expl_inst_class_mem_p=*/true);
22559 if (CLASSTYPE_NESTED_UTDS (t))
22560 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22561 bt_instantiate_type_proc, &storage);
22564 /* Given a function DECL, which is a specialization of TMPL, modify
22565 DECL to be a re-instantiation of TMPL with the same template
22566 arguments. TMPL should be the template into which tsubst'ing
22567 should occur for DECL, not the most general template.
22569 One reason for doing this is a scenario like this:
22571 template <class T>
22572 void f(const T&, int i);
22574 void g() { f(3, 7); }
22576 template <class T>
22577 void f(const T& t, const int i) { }
22579 Note that when the template is first instantiated, with
22580 instantiate_template, the resulting DECL will have no name for the
22581 first parameter, and the wrong type for the second. So, when we go
22582 to instantiate the DECL, we regenerate it. */
22584 static void
22585 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22587 /* The arguments used to instantiate DECL, from the most general
22588 template. */
22589 tree code_pattern;
22591 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22593 /* Make sure that we can see identifiers, and compute access
22594 correctly. */
22595 push_access_scope (decl);
22597 if (TREE_CODE (decl) == FUNCTION_DECL)
22599 tree decl_parm;
22600 tree pattern_parm;
22601 tree specs;
22602 int args_depth;
22603 int parms_depth;
22605 args_depth = TMPL_ARGS_DEPTH (args);
22606 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22607 if (args_depth > parms_depth)
22608 args = get_innermost_template_args (args, parms_depth);
22610 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22611 args, tf_error, NULL_TREE,
22612 /*defer_ok*/false);
22613 if (specs && specs != error_mark_node)
22614 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22615 specs);
22617 /* Merge parameter declarations. */
22618 decl_parm = skip_artificial_parms_for (decl,
22619 DECL_ARGUMENTS (decl));
22620 pattern_parm
22621 = skip_artificial_parms_for (code_pattern,
22622 DECL_ARGUMENTS (code_pattern));
22623 while (decl_parm && !DECL_PACK_P (pattern_parm))
22625 tree parm_type;
22626 tree attributes;
22628 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22629 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22630 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22631 NULL_TREE);
22632 parm_type = type_decays_to (parm_type);
22633 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22634 TREE_TYPE (decl_parm) = parm_type;
22635 attributes = DECL_ATTRIBUTES (pattern_parm);
22636 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22638 DECL_ATTRIBUTES (decl_parm) = attributes;
22639 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22641 decl_parm = DECL_CHAIN (decl_parm);
22642 pattern_parm = DECL_CHAIN (pattern_parm);
22644 /* Merge any parameters that match with the function parameter
22645 pack. */
22646 if (pattern_parm && DECL_PACK_P (pattern_parm))
22648 int i, len;
22649 tree expanded_types;
22650 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22651 the parameters in this function parameter pack. */
22652 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22653 args, tf_error, NULL_TREE);
22654 len = TREE_VEC_LENGTH (expanded_types);
22655 for (i = 0; i < len; i++)
22657 tree parm_type;
22658 tree attributes;
22660 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22661 /* Rename the parameter to include the index. */
22662 DECL_NAME (decl_parm) =
22663 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22664 parm_type = TREE_VEC_ELT (expanded_types, i);
22665 parm_type = type_decays_to (parm_type);
22666 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22667 TREE_TYPE (decl_parm) = parm_type;
22668 attributes = DECL_ATTRIBUTES (pattern_parm);
22669 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22671 DECL_ATTRIBUTES (decl_parm) = attributes;
22672 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22674 decl_parm = DECL_CHAIN (decl_parm);
22677 /* Merge additional specifiers from the CODE_PATTERN. */
22678 if (DECL_DECLARED_INLINE_P (code_pattern)
22679 && !DECL_DECLARED_INLINE_P (decl))
22680 DECL_DECLARED_INLINE_P (decl) = 1;
22682 else if (VAR_P (decl))
22684 start_lambda_scope (decl);
22685 DECL_INITIAL (decl) =
22686 tsubst_expr (DECL_INITIAL (code_pattern), args,
22687 tf_error, DECL_TI_TEMPLATE (decl),
22688 /*integral_constant_expression_p=*/false);
22689 finish_lambda_scope ();
22690 if (VAR_HAD_UNKNOWN_BOUND (decl))
22691 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22692 tf_error, DECL_TI_TEMPLATE (decl));
22694 else
22695 gcc_unreachable ();
22697 pop_access_scope (decl);
22700 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22701 substituted to get DECL. */
22703 tree
22704 template_for_substitution (tree decl)
22706 tree tmpl = DECL_TI_TEMPLATE (decl);
22708 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22709 for the instantiation. This is not always the most general
22710 template. Consider, for example:
22712 template <class T>
22713 struct S { template <class U> void f();
22714 template <> void f<int>(); };
22716 and an instantiation of S<double>::f<int>. We want TD to be the
22717 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22718 while (/* An instantiation cannot have a definition, so we need a
22719 more general template. */
22720 DECL_TEMPLATE_INSTANTIATION (tmpl)
22721 /* We must also deal with friend templates. Given:
22723 template <class T> struct S {
22724 template <class U> friend void f() {};
22727 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22728 so far as the language is concerned, but that's still
22729 where we get the pattern for the instantiation from. On
22730 other hand, if the definition comes outside the class, say:
22732 template <class T> struct S {
22733 template <class U> friend void f();
22735 template <class U> friend void f() {}
22737 we don't need to look any further. That's what the check for
22738 DECL_INITIAL is for. */
22739 || (TREE_CODE (decl) == FUNCTION_DECL
22740 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22741 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22743 /* The present template, TD, should not be a definition. If it
22744 were a definition, we should be using it! Note that we
22745 cannot restructure the loop to just keep going until we find
22746 a template with a definition, since that might go too far if
22747 a specialization was declared, but not defined. */
22749 /* Fetch the more general template. */
22750 tmpl = DECL_TI_TEMPLATE (tmpl);
22753 return tmpl;
22756 /* Returns true if we need to instantiate this template instance even if we
22757 know we aren't going to emit it. */
22759 bool
22760 always_instantiate_p (tree decl)
22762 /* We always instantiate inline functions so that we can inline them. An
22763 explicit instantiation declaration prohibits implicit instantiation of
22764 non-inline functions. With high levels of optimization, we would
22765 normally inline non-inline functions -- but we're not allowed to do
22766 that for "extern template" functions. Therefore, we check
22767 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22768 return ((TREE_CODE (decl) == FUNCTION_DECL
22769 && (DECL_DECLARED_INLINE_P (decl)
22770 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22771 /* And we need to instantiate static data members so that
22772 their initializers are available in integral constant
22773 expressions. */
22774 || (VAR_P (decl)
22775 && decl_maybe_constant_var_p (decl)));
22778 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22779 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22780 error, true otherwise. */
22782 bool
22783 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22785 tree fntype, spec, noex, clone;
22787 /* Don't instantiate a noexcept-specification from template context. */
22788 if (processing_template_decl)
22789 return true;
22791 if (DECL_CLONED_FUNCTION_P (fn))
22792 fn = DECL_CLONED_FUNCTION (fn);
22793 fntype = TREE_TYPE (fn);
22794 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22796 if (!spec || !TREE_PURPOSE (spec))
22797 return true;
22799 noex = TREE_PURPOSE (spec);
22801 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22803 static hash_set<tree>* fns = new hash_set<tree>;
22804 bool added = false;
22805 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22806 spec = get_defaulted_eh_spec (fn, complain);
22807 else if (!(added = !fns->add (fn)))
22809 /* If hash_set::add returns true, the element was already there. */
22810 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22811 DECL_SOURCE_LOCATION (fn));
22812 error_at (loc,
22813 "exception specification of %qD depends on itself",
22814 fn);
22815 spec = noexcept_false_spec;
22817 else if (push_tinst_level (fn))
22819 push_access_scope (fn);
22820 push_deferring_access_checks (dk_no_deferred);
22821 input_location = DECL_SOURCE_LOCATION (fn);
22822 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22823 DEFERRED_NOEXCEPT_ARGS (noex),
22824 tf_warning_or_error, fn,
22825 /*function_p=*/false,
22826 /*integral_constant_expression_p=*/true);
22827 pop_deferring_access_checks ();
22828 pop_access_scope (fn);
22829 pop_tinst_level ();
22830 spec = build_noexcept_spec (noex, tf_warning_or_error);
22831 if (spec == error_mark_node)
22832 spec = noexcept_false_spec;
22834 else
22835 spec = noexcept_false_spec;
22837 if (added)
22838 fns->remove (fn);
22840 if (spec == error_mark_node)
22841 return false;
22843 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22846 FOR_EACH_CLONE (clone, fn)
22848 if (TREE_TYPE (clone) == fntype)
22849 TREE_TYPE (clone) = TREE_TYPE (fn);
22850 else
22851 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22854 return true;
22857 /* We're starting to process the function INST, an instantiation of PATTERN;
22858 add their parameters to local_specializations. */
22860 static void
22861 register_parameter_specializations (tree pattern, tree inst)
22863 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22864 tree spec_parm = DECL_ARGUMENTS (inst);
22865 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22867 register_local_specialization (spec_parm, tmpl_parm);
22868 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22869 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22871 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22873 if (!DECL_PACK_P (tmpl_parm))
22875 register_local_specialization (spec_parm, tmpl_parm);
22876 spec_parm = DECL_CHAIN (spec_parm);
22878 else
22880 /* Register the (value) argument pack as a specialization of
22881 TMPL_PARM, then move on. */
22882 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22883 register_local_specialization (argpack, tmpl_parm);
22886 gcc_assert (!spec_parm);
22889 /* Produce the definition of D, a _DECL generated from a template. If
22890 DEFER_OK is true, then we don't have to actually do the
22891 instantiation now; we just have to do it sometime. Normally it is
22892 an error if this is an explicit instantiation but D is undefined.
22893 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22894 instantiated class template. */
22896 tree
22897 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22899 tree tmpl = DECL_TI_TEMPLATE (d);
22900 tree gen_args;
22901 tree args;
22902 tree td;
22903 tree code_pattern;
22904 tree spec;
22905 tree gen_tmpl;
22906 bool pattern_defined;
22907 location_t saved_loc = input_location;
22908 int saved_unevaluated_operand = cp_unevaluated_operand;
22909 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22910 bool external_p;
22911 bool deleted_p;
22913 /* This function should only be used to instantiate templates for
22914 functions and static member variables. */
22915 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22917 /* A concept is never instantiated. */
22918 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22920 /* Variables are never deferred; if instantiation is required, they
22921 are instantiated right away. That allows for better code in the
22922 case that an expression refers to the value of the variable --
22923 if the variable has a constant value the referring expression can
22924 take advantage of that fact. */
22925 if (VAR_P (d))
22926 defer_ok = false;
22928 /* Don't instantiate cloned functions. Instead, instantiate the
22929 functions they cloned. */
22930 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22931 d = DECL_CLONED_FUNCTION (d);
22933 if (DECL_TEMPLATE_INSTANTIATED (d)
22934 || (TREE_CODE (d) == FUNCTION_DECL
22935 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22936 || DECL_TEMPLATE_SPECIALIZATION (d))
22937 /* D has already been instantiated or explicitly specialized, so
22938 there's nothing for us to do here.
22940 It might seem reasonable to check whether or not D is an explicit
22941 instantiation, and, if so, stop here. But when an explicit
22942 instantiation is deferred until the end of the compilation,
22943 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22944 the instantiation. */
22945 return d;
22947 /* Check to see whether we know that this template will be
22948 instantiated in some other file, as with "extern template"
22949 extension. */
22950 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22952 /* In general, we do not instantiate such templates. */
22953 if (external_p && !always_instantiate_p (d))
22954 return d;
22956 gen_tmpl = most_general_template (tmpl);
22957 gen_args = DECL_TI_ARGS (d);
22959 if (tmpl != gen_tmpl)
22960 /* We should already have the extra args. */
22961 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22962 == TMPL_ARGS_DEPTH (gen_args));
22963 /* And what's in the hash table should match D. */
22964 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22965 || spec == NULL_TREE);
22967 /* This needs to happen before any tsubsting. */
22968 if (! push_tinst_level (d))
22969 return d;
22971 timevar_push (TV_TEMPLATE_INST);
22973 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22974 for the instantiation. */
22975 td = template_for_substitution (d);
22976 args = gen_args;
22978 if (VAR_P (d))
22980 /* Look up an explicit specialization, if any. */
22981 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22982 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22983 if (elt && elt != error_mark_node)
22985 td = TREE_VALUE (elt);
22986 args = TREE_PURPOSE (elt);
22990 code_pattern = DECL_TEMPLATE_RESULT (td);
22992 /* We should never be trying to instantiate a member of a class
22993 template or partial specialization. */
22994 gcc_assert (d != code_pattern);
22996 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22997 || DECL_TEMPLATE_SPECIALIZATION (td))
22998 /* In the case of a friend template whose definition is provided
22999 outside the class, we may have too many arguments. Drop the
23000 ones we don't need. The same is true for specializations. */
23001 args = get_innermost_template_args
23002 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23004 if (TREE_CODE (d) == FUNCTION_DECL)
23006 deleted_p = DECL_DELETED_FN (code_pattern);
23007 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23008 && DECL_INITIAL (code_pattern) != error_mark_node)
23009 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23010 || deleted_p);
23012 else
23014 deleted_p = false;
23015 if (DECL_CLASS_SCOPE_P (code_pattern))
23016 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23017 || DECL_INLINE_VAR_P (code_pattern));
23018 else
23019 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23022 /* We may be in the middle of deferred access check. Disable it now. */
23023 push_deferring_access_checks (dk_no_deferred);
23025 /* Unless an explicit instantiation directive has already determined
23026 the linkage of D, remember that a definition is available for
23027 this entity. */
23028 if (pattern_defined
23029 && !DECL_INTERFACE_KNOWN (d)
23030 && !DECL_NOT_REALLY_EXTERN (d))
23031 mark_definable (d);
23033 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23034 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23035 input_location = DECL_SOURCE_LOCATION (d);
23037 /* If D is a member of an explicitly instantiated class template,
23038 and no definition is available, treat it like an implicit
23039 instantiation. */
23040 if (!pattern_defined && expl_inst_class_mem_p
23041 && DECL_EXPLICIT_INSTANTIATION (d))
23043 /* Leave linkage flags alone on instantiations with anonymous
23044 visibility. */
23045 if (TREE_PUBLIC (d))
23047 DECL_NOT_REALLY_EXTERN (d) = 0;
23048 DECL_INTERFACE_KNOWN (d) = 0;
23050 SET_DECL_IMPLICIT_INSTANTIATION (d);
23053 /* Defer all other templates, unless we have been explicitly
23054 forbidden from doing so. */
23055 if (/* If there is no definition, we cannot instantiate the
23056 template. */
23057 ! pattern_defined
23058 /* If it's OK to postpone instantiation, do so. */
23059 || defer_ok
23060 /* If this is a static data member that will be defined
23061 elsewhere, we don't want to instantiate the entire data
23062 member, but we do want to instantiate the initializer so that
23063 we can substitute that elsewhere. */
23064 || (external_p && VAR_P (d))
23065 /* Handle here a deleted function too, avoid generating
23066 its body (c++/61080). */
23067 || deleted_p)
23069 /* The definition of the static data member is now required so
23070 we must substitute the initializer. */
23071 if (VAR_P (d)
23072 && !DECL_INITIAL (d)
23073 && DECL_INITIAL (code_pattern))
23075 tree ns;
23076 tree init;
23077 bool const_init = false;
23078 bool enter_context = DECL_CLASS_SCOPE_P (d);
23080 ns = decl_namespace_context (d);
23081 push_nested_namespace (ns);
23082 if (enter_context)
23083 push_nested_class (DECL_CONTEXT (d));
23084 init = tsubst_expr (DECL_INITIAL (code_pattern),
23085 args,
23086 tf_warning_or_error, NULL_TREE,
23087 /*integral_constant_expression_p=*/false);
23088 /* If instantiating the initializer involved instantiating this
23089 again, don't call cp_finish_decl twice. */
23090 if (!DECL_INITIAL (d))
23092 /* Make sure the initializer is still constant, in case of
23093 circular dependency (template/instantiate6.C). */
23094 const_init
23095 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23096 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23097 /*asmspec_tree=*/NULL_TREE,
23098 LOOKUP_ONLYCONVERTING);
23100 if (enter_context)
23101 pop_nested_class ();
23102 pop_nested_namespace (ns);
23105 /* We restore the source position here because it's used by
23106 add_pending_template. */
23107 input_location = saved_loc;
23109 if (at_eof && !pattern_defined
23110 && DECL_EXPLICIT_INSTANTIATION (d)
23111 && DECL_NOT_REALLY_EXTERN (d))
23112 /* [temp.explicit]
23114 The definition of a non-exported function template, a
23115 non-exported member function template, or a non-exported
23116 member function or static data member of a class template
23117 shall be present in every translation unit in which it is
23118 explicitly instantiated. */
23119 permerror (input_location, "explicit instantiation of %qD "
23120 "but no definition available", d);
23122 /* If we're in unevaluated context, we just wanted to get the
23123 constant value; this isn't an odr use, so don't queue
23124 a full instantiation. */
23125 if (cp_unevaluated_operand != 0)
23126 goto out;
23127 /* ??? Historically, we have instantiated inline functions, even
23128 when marked as "extern template". */
23129 if (!(external_p && VAR_P (d)))
23130 add_pending_template (d);
23131 goto out;
23133 /* Tell the repository that D is available in this translation unit
23134 -- and see if it is supposed to be instantiated here. */
23135 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23137 /* In a PCH file, despite the fact that the repository hasn't
23138 requested instantiation in the PCH it is still possible that
23139 an instantiation will be required in a file that includes the
23140 PCH. */
23141 if (pch_file)
23142 add_pending_template (d);
23143 /* Instantiate inline functions so that the inliner can do its
23144 job, even though we'll not be emitting a copy of this
23145 function. */
23146 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23147 goto out;
23150 bool push_to_top, nested;
23151 tree fn_context;
23152 fn_context = decl_function_context (d);
23153 nested = current_function_decl != NULL_TREE;
23154 push_to_top = !(nested && fn_context == current_function_decl);
23156 vec<tree> omp_privatization_save;
23157 if (nested)
23158 save_omp_privatization_clauses (omp_privatization_save);
23160 if (push_to_top)
23161 push_to_top_level ();
23162 else
23164 push_function_context ();
23165 cp_unevaluated_operand = 0;
23166 c_inhibit_evaluation_warnings = 0;
23169 /* Mark D as instantiated so that recursive calls to
23170 instantiate_decl do not try to instantiate it again. */
23171 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23173 /* Regenerate the declaration in case the template has been modified
23174 by a subsequent redeclaration. */
23175 regenerate_decl_from_template (d, td, args);
23177 /* We already set the file and line above. Reset them now in case
23178 they changed as a result of calling regenerate_decl_from_template. */
23179 input_location = DECL_SOURCE_LOCATION (d);
23181 if (VAR_P (d))
23183 tree init;
23184 bool const_init = false;
23186 /* Clear out DECL_RTL; whatever was there before may not be right
23187 since we've reset the type of the declaration. */
23188 SET_DECL_RTL (d, NULL);
23189 DECL_IN_AGGR_P (d) = 0;
23191 /* The initializer is placed in DECL_INITIAL by
23192 regenerate_decl_from_template so we don't need to
23193 push/pop_access_scope again here. Pull it out so that
23194 cp_finish_decl can process it. */
23195 init = DECL_INITIAL (d);
23196 DECL_INITIAL (d) = NULL_TREE;
23197 DECL_INITIALIZED_P (d) = 0;
23199 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23200 initializer. That function will defer actual emission until
23201 we have a chance to determine linkage. */
23202 DECL_EXTERNAL (d) = 0;
23204 /* Enter the scope of D so that access-checking works correctly. */
23205 bool enter_context = DECL_CLASS_SCOPE_P (d);
23206 if (enter_context)
23207 push_nested_class (DECL_CONTEXT (d));
23209 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23210 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23212 if (enter_context)
23213 pop_nested_class ();
23215 if (variable_template_p (gen_tmpl))
23216 note_variable_template_instantiation (d);
23218 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23219 synthesize_method (d);
23220 else if (TREE_CODE (d) == FUNCTION_DECL)
23222 hash_map<tree, tree> *saved_local_specializations;
23223 tree block = NULL_TREE;
23225 /* Save away the current list, in case we are instantiating one
23226 template from within the body of another. */
23227 saved_local_specializations = local_specializations;
23229 /* Set up the list of local specializations. */
23230 local_specializations = new hash_map<tree, tree>;
23232 /* Set up context. */
23233 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23234 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23235 block = push_stmt_list ();
23236 else
23237 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23239 /* Some typedefs referenced from within the template code need to be
23240 access checked at template instantiation time, i.e now. These
23241 types were added to the template at parsing time. Let's get those
23242 and perform the access checks then. */
23243 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23244 args);
23246 /* Create substitution entries for the parameters. */
23247 register_parameter_specializations (code_pattern, d);
23249 /* Substitute into the body of the function. */
23250 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23251 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23252 tf_warning_or_error, tmpl);
23253 else
23255 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23256 tf_warning_or_error, tmpl,
23257 /*integral_constant_expression_p=*/false);
23259 /* Set the current input_location to the end of the function
23260 so that finish_function knows where we are. */
23261 input_location
23262 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23264 /* Remember if we saw an infinite loop in the template. */
23265 current_function_infinite_loop
23266 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23269 /* We don't need the local specializations any more. */
23270 delete local_specializations;
23271 local_specializations = saved_local_specializations;
23273 /* Finish the function. */
23274 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23275 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23276 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23277 else
23279 d = finish_function (0);
23280 expand_or_defer_fn (d);
23283 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23284 cp_check_omp_declare_reduction (d);
23287 /* We're not deferring instantiation any more. */
23288 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23290 if (push_to_top)
23291 pop_from_top_level ();
23292 else
23293 pop_function_context ();
23295 if (nested)
23296 restore_omp_privatization_clauses (omp_privatization_save);
23298 out:
23299 pop_deferring_access_checks ();
23300 timevar_pop (TV_TEMPLATE_INST);
23301 pop_tinst_level ();
23302 input_location = saved_loc;
23303 cp_unevaluated_operand = saved_unevaluated_operand;
23304 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23306 return d;
23309 /* Run through the list of templates that we wish we could
23310 instantiate, and instantiate any we can. RETRIES is the
23311 number of times we retry pending template instantiation. */
23313 void
23314 instantiate_pending_templates (int retries)
23316 int reconsider;
23317 location_t saved_loc = input_location;
23319 /* Instantiating templates may trigger vtable generation. This in turn
23320 may require further template instantiations. We place a limit here
23321 to avoid infinite loop. */
23322 if (pending_templates && retries >= max_tinst_depth)
23324 tree decl = pending_templates->tinst->decl;
23326 fatal_error (input_location,
23327 "template instantiation depth exceeds maximum of %d"
23328 " instantiating %q+D, possibly from virtual table generation"
23329 " (use -ftemplate-depth= to increase the maximum)",
23330 max_tinst_depth, decl);
23331 if (TREE_CODE (decl) == FUNCTION_DECL)
23332 /* Pretend that we defined it. */
23333 DECL_INITIAL (decl) = error_mark_node;
23334 return;
23339 struct pending_template **t = &pending_templates;
23340 struct pending_template *last = NULL;
23341 reconsider = 0;
23342 while (*t)
23344 tree instantiation = reopen_tinst_level ((*t)->tinst);
23345 bool complete = false;
23347 if (TYPE_P (instantiation))
23349 if (!COMPLETE_TYPE_P (instantiation))
23351 instantiate_class_template (instantiation);
23352 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23353 for (tree fld = TYPE_FIELDS (instantiation);
23354 fld; fld = TREE_CHAIN (fld))
23355 if ((VAR_P (fld)
23356 || (TREE_CODE (fld) == FUNCTION_DECL
23357 && !DECL_ARTIFICIAL (fld)))
23358 && DECL_TEMPLATE_INSTANTIATION (fld))
23359 instantiate_decl (fld,
23360 /*defer_ok=*/false,
23361 /*expl_inst_class_mem_p=*/false);
23363 if (COMPLETE_TYPE_P (instantiation))
23364 reconsider = 1;
23367 complete = COMPLETE_TYPE_P (instantiation);
23369 else
23371 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23372 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23374 instantiation
23375 = instantiate_decl (instantiation,
23376 /*defer_ok=*/false,
23377 /*expl_inst_class_mem_p=*/false);
23378 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23379 reconsider = 1;
23382 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23383 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23386 if (complete)
23387 /* If INSTANTIATION has been instantiated, then we don't
23388 need to consider it again in the future. */
23389 *t = (*t)->next;
23390 else
23392 last = *t;
23393 t = &(*t)->next;
23395 tinst_depth = 0;
23396 current_tinst_level = NULL;
23398 last_pending_template = last;
23400 while (reconsider);
23402 input_location = saved_loc;
23405 /* Substitute ARGVEC into T, which is a list of initializers for
23406 either base class or a non-static data member. The TREE_PURPOSEs
23407 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23408 instantiate_decl. */
23410 static tree
23411 tsubst_initializer_list (tree t, tree argvec)
23413 tree inits = NULL_TREE;
23415 for (; t; t = TREE_CHAIN (t))
23417 tree decl;
23418 tree init;
23419 tree expanded_bases = NULL_TREE;
23420 tree expanded_arguments = NULL_TREE;
23421 int i, len = 1;
23423 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23425 tree expr;
23426 tree arg;
23428 /* Expand the base class expansion type into separate base
23429 classes. */
23430 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23431 tf_warning_or_error,
23432 NULL_TREE);
23433 if (expanded_bases == error_mark_node)
23434 continue;
23436 /* We'll be building separate TREE_LISTs of arguments for
23437 each base. */
23438 len = TREE_VEC_LENGTH (expanded_bases);
23439 expanded_arguments = make_tree_vec (len);
23440 for (i = 0; i < len; i++)
23441 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23443 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23444 expand each argument in the TREE_VALUE of t. */
23445 expr = make_node (EXPR_PACK_EXPANSION);
23446 PACK_EXPANSION_LOCAL_P (expr) = true;
23447 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23448 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23450 if (TREE_VALUE (t) == void_type_node)
23451 /* VOID_TYPE_NODE is used to indicate
23452 value-initialization. */
23454 for (i = 0; i < len; i++)
23455 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23457 else
23459 /* Substitute parameter packs into each argument in the
23460 TREE_LIST. */
23461 in_base_initializer = 1;
23462 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23464 tree expanded_exprs;
23466 /* Expand the argument. */
23467 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23468 expanded_exprs
23469 = tsubst_pack_expansion (expr, argvec,
23470 tf_warning_or_error,
23471 NULL_TREE);
23472 if (expanded_exprs == error_mark_node)
23473 continue;
23475 /* Prepend each of the expanded expressions to the
23476 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23477 for (i = 0; i < len; i++)
23479 TREE_VEC_ELT (expanded_arguments, i) =
23480 tree_cons (NULL_TREE,
23481 TREE_VEC_ELT (expanded_exprs, i),
23482 TREE_VEC_ELT (expanded_arguments, i));
23485 in_base_initializer = 0;
23487 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23488 since we built them backwards. */
23489 for (i = 0; i < len; i++)
23491 TREE_VEC_ELT (expanded_arguments, i) =
23492 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23497 for (i = 0; i < len; ++i)
23499 if (expanded_bases)
23501 decl = TREE_VEC_ELT (expanded_bases, i);
23502 decl = expand_member_init (decl);
23503 init = TREE_VEC_ELT (expanded_arguments, i);
23505 else
23507 tree tmp;
23508 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23509 tf_warning_or_error, NULL_TREE);
23511 decl = expand_member_init (decl);
23512 if (decl && !DECL_P (decl))
23513 in_base_initializer = 1;
23515 init = TREE_VALUE (t);
23516 tmp = init;
23517 if (init != void_type_node)
23518 init = tsubst_expr (init, argvec,
23519 tf_warning_or_error, NULL_TREE,
23520 /*integral_constant_expression_p=*/false);
23521 if (init == NULL_TREE && tmp != NULL_TREE)
23522 /* If we had an initializer but it instantiated to nothing,
23523 value-initialize the object. This will only occur when
23524 the initializer was a pack expansion where the parameter
23525 packs used in that expansion were of length zero. */
23526 init = void_type_node;
23527 in_base_initializer = 0;
23530 if (decl)
23532 init = build_tree_list (decl, init);
23533 TREE_CHAIN (init) = inits;
23534 inits = init;
23538 return inits;
23541 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23543 static void
23544 set_current_access_from_decl (tree decl)
23546 if (TREE_PRIVATE (decl))
23547 current_access_specifier = access_private_node;
23548 else if (TREE_PROTECTED (decl))
23549 current_access_specifier = access_protected_node;
23550 else
23551 current_access_specifier = access_public_node;
23554 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23555 is the instantiation (which should have been created with
23556 start_enum) and ARGS are the template arguments to use. */
23558 static void
23559 tsubst_enum (tree tag, tree newtag, tree args)
23561 tree e;
23563 if (SCOPED_ENUM_P (newtag))
23564 begin_scope (sk_scoped_enum, newtag);
23566 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23568 tree value;
23569 tree decl;
23571 decl = TREE_VALUE (e);
23572 /* Note that in a template enum, the TREE_VALUE is the
23573 CONST_DECL, not the corresponding INTEGER_CST. */
23574 value = tsubst_expr (DECL_INITIAL (decl),
23575 args, tf_warning_or_error, NULL_TREE,
23576 /*integral_constant_expression_p=*/true);
23578 /* Give this enumeration constant the correct access. */
23579 set_current_access_from_decl (decl);
23581 /* Actually build the enumerator itself. Here we're assuming that
23582 enumerators can't have dependent attributes. */
23583 build_enumerator (DECL_NAME (decl), value, newtag,
23584 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23587 if (SCOPED_ENUM_P (newtag))
23588 finish_scope ();
23590 finish_enum_value_list (newtag);
23591 finish_enum (newtag);
23593 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23594 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23597 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23598 its type -- but without substituting the innermost set of template
23599 arguments. So, innermost set of template parameters will appear in
23600 the type. */
23602 tree
23603 get_mostly_instantiated_function_type (tree decl)
23605 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23606 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23609 /* Return truthvalue if we're processing a template different from
23610 the last one involved in diagnostics. */
23611 bool
23612 problematic_instantiation_changed (void)
23614 return current_tinst_level != last_error_tinst_level;
23617 /* Remember current template involved in diagnostics. */
23618 void
23619 record_last_problematic_instantiation (void)
23621 last_error_tinst_level = current_tinst_level;
23624 struct tinst_level *
23625 current_instantiation (void)
23627 return current_tinst_level;
23630 /* Return TRUE if current_function_decl is being instantiated, false
23631 otherwise. */
23633 bool
23634 instantiating_current_function_p (void)
23636 return (current_instantiation ()
23637 && current_instantiation ()->decl == current_function_decl);
23640 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23641 type. Return zero for ok, nonzero for disallowed. Issue error and
23642 warning messages under control of COMPLAIN. */
23644 static int
23645 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23647 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23648 return 0;
23649 else if (POINTER_TYPE_P (type))
23650 return 0;
23651 else if (TYPE_PTRMEM_P (type))
23652 return 0;
23653 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23654 return 0;
23655 else if (TREE_CODE (type) == TYPENAME_TYPE)
23656 return 0;
23657 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23658 return 0;
23659 else if (TREE_CODE (type) == NULLPTR_TYPE)
23660 return 0;
23661 /* A bound template template parm could later be instantiated to have a valid
23662 nontype parm type via an alias template. */
23663 else if (cxx_dialect >= cxx11
23664 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23665 return 0;
23667 if (complain & tf_error)
23669 if (type == error_mark_node)
23670 inform (input_location, "invalid template non-type parameter");
23671 else
23672 error ("%q#T is not a valid type for a template non-type parameter",
23673 type);
23675 return 1;
23678 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23679 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23681 static bool
23682 dependent_type_p_r (tree type)
23684 tree scope;
23686 /* [temp.dep.type]
23688 A type is dependent if it is:
23690 -- a template parameter. Template template parameters are types
23691 for us (since TYPE_P holds true for them) so we handle
23692 them here. */
23693 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23694 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23695 return true;
23696 /* -- a qualified-id with a nested-name-specifier which contains a
23697 class-name that names a dependent type or whose unqualified-id
23698 names a dependent type. */
23699 if (TREE_CODE (type) == TYPENAME_TYPE)
23700 return true;
23702 /* An alias template specialization can be dependent even if the
23703 resulting type is not. */
23704 if (dependent_alias_template_spec_p (type))
23705 return true;
23707 /* -- a cv-qualified type where the cv-unqualified type is
23708 dependent.
23709 No code is necessary for this bullet; the code below handles
23710 cv-qualified types, and we don't want to strip aliases with
23711 TYPE_MAIN_VARIANT because of DR 1558. */
23712 /* -- a compound type constructed from any dependent type. */
23713 if (TYPE_PTRMEM_P (type))
23714 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23715 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23716 (type)));
23717 else if (TYPE_PTR_P (type)
23718 || TREE_CODE (type) == REFERENCE_TYPE)
23719 return dependent_type_p (TREE_TYPE (type));
23720 else if (TREE_CODE (type) == FUNCTION_TYPE
23721 || TREE_CODE (type) == METHOD_TYPE)
23723 tree arg_type;
23725 if (dependent_type_p (TREE_TYPE (type)))
23726 return true;
23727 for (arg_type = TYPE_ARG_TYPES (type);
23728 arg_type;
23729 arg_type = TREE_CHAIN (arg_type))
23730 if (dependent_type_p (TREE_VALUE (arg_type)))
23731 return true;
23732 if (cxx_dialect >= cxx17)
23734 /* A value-dependent noexcept-specifier makes the type dependent. */
23735 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23736 if (spec && TREE_PURPOSE (spec)
23737 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23738 return true;
23740 return false;
23742 /* -- an array type constructed from any dependent type or whose
23743 size is specified by a constant expression that is
23744 value-dependent.
23746 We checked for type- and value-dependence of the bounds in
23747 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23748 if (TREE_CODE (type) == ARRAY_TYPE)
23750 if (TYPE_DOMAIN (type)
23751 && dependent_type_p (TYPE_DOMAIN (type)))
23752 return true;
23753 return dependent_type_p (TREE_TYPE (type));
23756 /* -- a template-id in which either the template name is a template
23757 parameter ... */
23758 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23759 return true;
23760 /* ... or any of the template arguments is a dependent type or
23761 an expression that is type-dependent or value-dependent. */
23762 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23763 && (any_dependent_template_arguments_p
23764 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23765 return true;
23767 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23768 dependent; if the argument of the `typeof' expression is not
23769 type-dependent, then it should already been have resolved. */
23770 if (TREE_CODE (type) == TYPEOF_TYPE
23771 || TREE_CODE (type) == DECLTYPE_TYPE
23772 || TREE_CODE (type) == UNDERLYING_TYPE)
23773 return true;
23775 /* A template argument pack is dependent if any of its packed
23776 arguments are. */
23777 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23779 tree args = ARGUMENT_PACK_ARGS (type);
23780 int i, len = TREE_VEC_LENGTH (args);
23781 for (i = 0; i < len; ++i)
23782 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23783 return true;
23786 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23787 be template parameters. */
23788 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23789 return true;
23791 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23792 return true;
23794 /* The standard does not specifically mention types that are local
23795 to template functions or local classes, but they should be
23796 considered dependent too. For example:
23798 template <int I> void f() {
23799 enum E { a = I };
23800 S<sizeof (E)> s;
23803 The size of `E' cannot be known until the value of `I' has been
23804 determined. Therefore, `E' must be considered dependent. */
23805 scope = TYPE_CONTEXT (type);
23806 if (scope && TYPE_P (scope))
23807 return dependent_type_p (scope);
23808 /* Don't use type_dependent_expression_p here, as it can lead
23809 to infinite recursion trying to determine whether a lambda
23810 nested in a lambda is dependent (c++/47687). */
23811 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23812 && DECL_LANG_SPECIFIC (scope)
23813 && DECL_TEMPLATE_INFO (scope)
23814 && (any_dependent_template_arguments_p
23815 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23816 return true;
23818 /* Other types are non-dependent. */
23819 return false;
23822 /* Returns TRUE if TYPE is dependent, in the sense of
23823 [temp.dep.type]. Note that a NULL type is considered dependent. */
23825 bool
23826 dependent_type_p (tree type)
23828 /* If there are no template parameters in scope, then there can't be
23829 any dependent types. */
23830 if (!processing_template_decl)
23832 /* If we are not processing a template, then nobody should be
23833 providing us with a dependent type. */
23834 gcc_assert (type);
23835 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23836 return false;
23839 /* If the type is NULL, we have not computed a type for the entity
23840 in question; in that case, the type is dependent. */
23841 if (!type)
23842 return true;
23844 /* Erroneous types can be considered non-dependent. */
23845 if (type == error_mark_node)
23846 return false;
23848 /* Getting here with global_type_node means we improperly called this
23849 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23850 gcc_checking_assert (type != global_type_node);
23852 /* If we have not already computed the appropriate value for TYPE,
23853 do so now. */
23854 if (!TYPE_DEPENDENT_P_VALID (type))
23856 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23857 TYPE_DEPENDENT_P_VALID (type) = 1;
23860 return TYPE_DEPENDENT_P (type);
23863 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23864 lookup. In other words, a dependent type that is not the current
23865 instantiation. */
23867 bool
23868 dependent_scope_p (tree scope)
23870 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23871 && !currently_open_class (scope));
23874 /* T is a SCOPE_REF; return whether we need to consider it
23875 instantiation-dependent so that we can check access at instantiation
23876 time even though we know which member it resolves to. */
23878 static bool
23879 instantiation_dependent_scope_ref_p (tree t)
23881 if (DECL_P (TREE_OPERAND (t, 1))
23882 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23883 && accessible_in_template_p (TREE_OPERAND (t, 0),
23884 TREE_OPERAND (t, 1)))
23885 return false;
23886 else
23887 return true;
23890 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23891 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23892 expression. */
23894 /* Note that this predicate is not appropriate for general expressions;
23895 only constant expressions (that satisfy potential_constant_expression)
23896 can be tested for value dependence. */
23898 bool
23899 value_dependent_expression_p (tree expression)
23901 if (!processing_template_decl || expression == NULL_TREE)
23902 return false;
23904 /* A name declared with a dependent type. */
23905 if (DECL_P (expression) && type_dependent_expression_p (expression))
23906 return true;
23908 switch (TREE_CODE (expression))
23910 case BASELINK:
23911 /* A dependent member function of the current instantiation. */
23912 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23914 case FUNCTION_DECL:
23915 /* A dependent member function of the current instantiation. */
23916 if (DECL_CLASS_SCOPE_P (expression)
23917 && dependent_type_p (DECL_CONTEXT (expression)))
23918 return true;
23919 break;
23921 case IDENTIFIER_NODE:
23922 /* A name that has not been looked up -- must be dependent. */
23923 return true;
23925 case TEMPLATE_PARM_INDEX:
23926 /* A non-type template parm. */
23927 return true;
23929 case CONST_DECL:
23930 /* A non-type template parm. */
23931 if (DECL_TEMPLATE_PARM_P (expression))
23932 return true;
23933 return value_dependent_expression_p (DECL_INITIAL (expression));
23935 case VAR_DECL:
23936 /* A constant with literal type and is initialized
23937 with an expression that is value-dependent.
23939 Note that a non-dependent parenthesized initializer will have
23940 already been replaced with its constant value, so if we see
23941 a TREE_LIST it must be dependent. */
23942 if (DECL_INITIAL (expression)
23943 && decl_constant_var_p (expression)
23944 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23945 /* cp_finish_decl doesn't fold reference initializers. */
23946 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23947 || type_dependent_expression_p (DECL_INITIAL (expression))
23948 || value_dependent_expression_p (DECL_INITIAL (expression))))
23949 return true;
23950 if (DECL_HAS_VALUE_EXPR_P (expression))
23952 tree value_expr = DECL_VALUE_EXPR (expression);
23953 if (type_dependent_expression_p (value_expr))
23954 return true;
23956 return false;
23958 case DYNAMIC_CAST_EXPR:
23959 case STATIC_CAST_EXPR:
23960 case CONST_CAST_EXPR:
23961 case REINTERPRET_CAST_EXPR:
23962 case CAST_EXPR:
23963 /* These expressions are value-dependent if the type to which
23964 the cast occurs is dependent or the expression being casted
23965 is value-dependent. */
23967 tree type = TREE_TYPE (expression);
23969 if (dependent_type_p (type))
23970 return true;
23972 /* A functional cast has a list of operands. */
23973 expression = TREE_OPERAND (expression, 0);
23974 if (!expression)
23976 /* If there are no operands, it must be an expression such
23977 as "int()". This should not happen for aggregate types
23978 because it would form non-constant expressions. */
23979 gcc_assert (cxx_dialect >= cxx11
23980 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23982 return false;
23985 if (TREE_CODE (expression) == TREE_LIST)
23986 return any_value_dependent_elements_p (expression);
23988 return value_dependent_expression_p (expression);
23991 case SIZEOF_EXPR:
23992 if (SIZEOF_EXPR_TYPE_P (expression))
23993 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23994 /* FALLTHRU */
23995 case ALIGNOF_EXPR:
23996 case TYPEID_EXPR:
23997 /* A `sizeof' expression is value-dependent if the operand is
23998 type-dependent or is a pack expansion. */
23999 expression = TREE_OPERAND (expression, 0);
24000 if (PACK_EXPANSION_P (expression))
24001 return true;
24002 else if (TYPE_P (expression))
24003 return dependent_type_p (expression);
24004 return instantiation_dependent_uneval_expression_p (expression);
24006 case AT_ENCODE_EXPR:
24007 /* An 'encode' expression is value-dependent if the operand is
24008 type-dependent. */
24009 expression = TREE_OPERAND (expression, 0);
24010 return dependent_type_p (expression);
24012 case NOEXCEPT_EXPR:
24013 expression = TREE_OPERAND (expression, 0);
24014 return instantiation_dependent_uneval_expression_p (expression);
24016 case SCOPE_REF:
24017 /* All instantiation-dependent expressions should also be considered
24018 value-dependent. */
24019 return instantiation_dependent_scope_ref_p (expression);
24021 case COMPONENT_REF:
24022 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24023 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24025 case NONTYPE_ARGUMENT_PACK:
24026 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24027 is value-dependent. */
24029 tree values = ARGUMENT_PACK_ARGS (expression);
24030 int i, len = TREE_VEC_LENGTH (values);
24032 for (i = 0; i < len; ++i)
24033 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24034 return true;
24036 return false;
24039 case TRAIT_EXPR:
24041 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24042 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
24043 || (type2 ? dependent_type_p (type2) : false));
24046 case MODOP_EXPR:
24047 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24048 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24050 case ARRAY_REF:
24051 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24052 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24054 case ADDR_EXPR:
24056 tree op = TREE_OPERAND (expression, 0);
24057 return (value_dependent_expression_p (op)
24058 || has_value_dependent_address (op));
24061 case REQUIRES_EXPR:
24062 /* Treat all requires-expressions as value-dependent so
24063 we don't try to fold them. */
24064 return true;
24066 case TYPE_REQ:
24067 return dependent_type_p (TREE_OPERAND (expression, 0));
24069 case CALL_EXPR:
24071 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24072 return true;
24073 tree fn = get_callee_fndecl (expression);
24074 int i, nargs;
24075 nargs = call_expr_nargs (expression);
24076 for (i = 0; i < nargs; ++i)
24078 tree op = CALL_EXPR_ARG (expression, i);
24079 /* In a call to a constexpr member function, look through the
24080 implicit ADDR_EXPR on the object argument so that it doesn't
24081 cause the call to be considered value-dependent. We also
24082 look through it in potential_constant_expression. */
24083 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24084 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24085 && TREE_CODE (op) == ADDR_EXPR)
24086 op = TREE_OPERAND (op, 0);
24087 if (value_dependent_expression_p (op))
24088 return true;
24090 return false;
24093 case TEMPLATE_ID_EXPR:
24094 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
24095 type-dependent. */
24096 return type_dependent_expression_p (expression)
24097 || variable_concept_p (TREE_OPERAND (expression, 0));
24099 case CONSTRUCTOR:
24101 unsigned ix;
24102 tree val;
24103 if (dependent_type_p (TREE_TYPE (expression)))
24104 return true;
24105 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24106 if (value_dependent_expression_p (val))
24107 return true;
24108 return false;
24111 case STMT_EXPR:
24112 /* Treat a GNU statement expression as dependent to avoid crashing
24113 under instantiate_non_dependent_expr; it can't be constant. */
24114 return true;
24116 default:
24117 /* A constant expression is value-dependent if any subexpression is
24118 value-dependent. */
24119 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24121 case tcc_reference:
24122 case tcc_unary:
24123 case tcc_comparison:
24124 case tcc_binary:
24125 case tcc_expression:
24126 case tcc_vl_exp:
24128 int i, len = cp_tree_operand_length (expression);
24130 for (i = 0; i < len; i++)
24132 tree t = TREE_OPERAND (expression, i);
24134 /* In some cases, some of the operands may be missing.
24135 (For example, in the case of PREDECREMENT_EXPR, the
24136 amount to increment by may be missing.) That doesn't
24137 make the expression dependent. */
24138 if (t && value_dependent_expression_p (t))
24139 return true;
24142 break;
24143 default:
24144 break;
24146 break;
24149 /* The expression is not value-dependent. */
24150 return false;
24153 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24154 [temp.dep.expr]. Note that an expression with no type is
24155 considered dependent. Other parts of the compiler arrange for an
24156 expression with type-dependent subexpressions to have no type, so
24157 this function doesn't have to be fully recursive. */
24159 bool
24160 type_dependent_expression_p (tree expression)
24162 if (!processing_template_decl)
24163 return false;
24165 if (expression == NULL_TREE || expression == error_mark_node)
24166 return false;
24168 /* An unresolved name is always dependent. */
24169 if (identifier_p (expression)
24170 || TREE_CODE (expression) == USING_DECL
24171 || TREE_CODE (expression) == WILDCARD_DECL)
24172 return true;
24174 /* A fold expression is type-dependent. */
24175 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24176 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24177 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24178 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24179 return true;
24181 /* Some expression forms are never type-dependent. */
24182 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24183 || TREE_CODE (expression) == SIZEOF_EXPR
24184 || TREE_CODE (expression) == ALIGNOF_EXPR
24185 || TREE_CODE (expression) == AT_ENCODE_EXPR
24186 || TREE_CODE (expression) == NOEXCEPT_EXPR
24187 || TREE_CODE (expression) == TRAIT_EXPR
24188 || TREE_CODE (expression) == TYPEID_EXPR
24189 || TREE_CODE (expression) == DELETE_EXPR
24190 || TREE_CODE (expression) == VEC_DELETE_EXPR
24191 || TREE_CODE (expression) == THROW_EXPR
24192 || TREE_CODE (expression) == REQUIRES_EXPR)
24193 return false;
24195 /* The types of these expressions depends only on the type to which
24196 the cast occurs. */
24197 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24198 || TREE_CODE (expression) == STATIC_CAST_EXPR
24199 || TREE_CODE (expression) == CONST_CAST_EXPR
24200 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24201 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24202 || TREE_CODE (expression) == CAST_EXPR)
24203 return dependent_type_p (TREE_TYPE (expression));
24205 /* The types of these expressions depends only on the type created
24206 by the expression. */
24207 if (TREE_CODE (expression) == NEW_EXPR
24208 || TREE_CODE (expression) == VEC_NEW_EXPR)
24210 /* For NEW_EXPR tree nodes created inside a template, either
24211 the object type itself or a TREE_LIST may appear as the
24212 operand 1. */
24213 tree type = TREE_OPERAND (expression, 1);
24214 if (TREE_CODE (type) == TREE_LIST)
24215 /* This is an array type. We need to check array dimensions
24216 as well. */
24217 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24218 || value_dependent_expression_p
24219 (TREE_OPERAND (TREE_VALUE (type), 1));
24220 else
24221 return dependent_type_p (type);
24224 if (TREE_CODE (expression) == SCOPE_REF)
24226 tree scope = TREE_OPERAND (expression, 0);
24227 tree name = TREE_OPERAND (expression, 1);
24229 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24230 contains an identifier associated by name lookup with one or more
24231 declarations declared with a dependent type, or...a
24232 nested-name-specifier or qualified-id that names a member of an
24233 unknown specialization. */
24234 return (type_dependent_expression_p (name)
24235 || dependent_scope_p (scope));
24238 if (TREE_CODE (expression) == TEMPLATE_DECL
24239 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24240 return uses_outer_template_parms (expression);
24242 if (TREE_CODE (expression) == STMT_EXPR)
24243 expression = stmt_expr_value_expr (expression);
24245 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24247 tree elt;
24248 unsigned i;
24250 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24252 if (type_dependent_expression_p (elt))
24253 return true;
24255 return false;
24258 /* A static data member of the current instantiation with incomplete
24259 array type is type-dependent, as the definition and specializations
24260 can have different bounds. */
24261 if (VAR_P (expression)
24262 && DECL_CLASS_SCOPE_P (expression)
24263 && dependent_type_p (DECL_CONTEXT (expression))
24264 && VAR_HAD_UNKNOWN_BOUND (expression))
24265 return true;
24267 /* An array of unknown bound depending on a variadic parameter, eg:
24269 template<typename... Args>
24270 void foo (Args... args)
24272 int arr[] = { args... };
24275 template<int... vals>
24276 void bar ()
24278 int arr[] = { vals... };
24281 If the array has no length and has an initializer, it must be that
24282 we couldn't determine its length in cp_complete_array_type because
24283 it is dependent. */
24284 if (VAR_P (expression)
24285 && TREE_TYPE (expression) != NULL_TREE
24286 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24287 && !TYPE_DOMAIN (TREE_TYPE (expression))
24288 && DECL_INITIAL (expression))
24289 return true;
24291 /* A function or variable template-id is type-dependent if it has any
24292 dependent template arguments. */
24293 if (VAR_OR_FUNCTION_DECL_P (expression)
24294 && DECL_LANG_SPECIFIC (expression)
24295 && DECL_TEMPLATE_INFO (expression))
24297 /* Consider the innermost template arguments, since those are the ones
24298 that come from the template-id; the template arguments for the
24299 enclosing class do not make it type-dependent unless they are used in
24300 the type of the decl. */
24301 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24302 && (any_dependent_template_arguments_p
24303 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24304 return true;
24306 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24307 type-dependent. Checking this is important for functions with auto
24308 return type, which looks like a dependent type. */
24309 if (TREE_CODE (expression) == FUNCTION_DECL
24310 && (!DECL_CLASS_SCOPE_P (expression)
24311 || !dependent_type_p (DECL_CONTEXT (expression)))
24312 && (!DECL_FRIEND_CONTEXT (expression)
24313 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression)))
24314 && !DECL_LOCAL_FUNCTION_P (expression))
24316 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24317 || undeduced_auto_decl (expression));
24318 return false;
24322 /* Always dependent, on the number of arguments if nothing else. */
24323 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24324 return true;
24326 if (TREE_TYPE (expression) == unknown_type_node)
24328 if (TREE_CODE (expression) == ADDR_EXPR)
24329 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24330 if (TREE_CODE (expression) == COMPONENT_REF
24331 || TREE_CODE (expression) == OFFSET_REF)
24333 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24334 return true;
24335 expression = TREE_OPERAND (expression, 1);
24336 if (identifier_p (expression))
24337 return false;
24339 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24340 if (TREE_CODE (expression) == SCOPE_REF)
24341 return false;
24343 if (BASELINK_P (expression))
24345 if (BASELINK_OPTYPE (expression)
24346 && dependent_type_p (BASELINK_OPTYPE (expression)))
24347 return true;
24348 expression = BASELINK_FUNCTIONS (expression);
24351 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24353 if (any_dependent_template_arguments_p
24354 (TREE_OPERAND (expression, 1)))
24355 return true;
24356 expression = TREE_OPERAND (expression, 0);
24357 if (identifier_p (expression))
24358 return true;
24361 gcc_assert (TREE_CODE (expression) == OVERLOAD
24362 || TREE_CODE (expression) == FUNCTION_DECL);
24364 for (lkp_iterator iter (expression); iter; ++iter)
24365 if (type_dependent_expression_p (*iter))
24366 return true;
24368 return false;
24371 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24373 /* Dependent type attributes might not have made it from the decl to
24374 the type yet. */
24375 if (DECL_P (expression)
24376 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24377 return true;
24379 return (dependent_type_p (TREE_TYPE (expression)));
24382 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24383 type-dependent if the expression refers to a member of the current
24384 instantiation and the type of the referenced member is dependent, or the
24385 class member access expression refers to a member of an unknown
24386 specialization.
24388 This function returns true if the OBJECT in such a class member access
24389 expression is of an unknown specialization. */
24391 bool
24392 type_dependent_object_expression_p (tree object)
24394 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24395 dependent. */
24396 if (TREE_CODE (object) == IDENTIFIER_NODE)
24397 return true;
24398 tree scope = TREE_TYPE (object);
24399 return (!scope || dependent_scope_p (scope));
24402 /* walk_tree callback function for instantiation_dependent_expression_p,
24403 below. Returns non-zero if a dependent subexpression is found. */
24405 static tree
24406 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24407 void * /*data*/)
24409 if (TYPE_P (*tp))
24411 /* We don't have to worry about decltype currently because decltype
24412 of an instantiation-dependent expr is a dependent type. This
24413 might change depending on the resolution of DR 1172. */
24414 *walk_subtrees = false;
24415 return NULL_TREE;
24417 enum tree_code code = TREE_CODE (*tp);
24418 switch (code)
24420 /* Don't treat an argument list as dependent just because it has no
24421 TREE_TYPE. */
24422 case TREE_LIST:
24423 case TREE_VEC:
24424 return NULL_TREE;
24426 case TEMPLATE_PARM_INDEX:
24427 return *tp;
24429 /* Handle expressions with type operands. */
24430 case SIZEOF_EXPR:
24431 case ALIGNOF_EXPR:
24432 case TYPEID_EXPR:
24433 case AT_ENCODE_EXPR:
24435 tree op = TREE_OPERAND (*tp, 0);
24436 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24437 op = TREE_TYPE (op);
24438 if (TYPE_P (op))
24440 if (dependent_type_p (op))
24441 return *tp;
24442 else
24444 *walk_subtrees = false;
24445 return NULL_TREE;
24448 break;
24451 case COMPONENT_REF:
24452 if (identifier_p (TREE_OPERAND (*tp, 1)))
24453 /* In a template, finish_class_member_access_expr creates a
24454 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24455 type-dependent, so that we can check access control at
24456 instantiation time (PR 42277). See also Core issue 1273. */
24457 return *tp;
24458 break;
24460 case SCOPE_REF:
24461 if (instantiation_dependent_scope_ref_p (*tp))
24462 return *tp;
24463 else
24464 break;
24466 /* Treat statement-expressions as dependent. */
24467 case BIND_EXPR:
24468 return *tp;
24470 /* Treat requires-expressions as dependent. */
24471 case REQUIRES_EXPR:
24472 return *tp;
24474 case CALL_EXPR:
24475 /* Treat calls to function concepts as dependent. */
24476 if (function_concept_check_p (*tp))
24477 return *tp;
24478 break;
24480 case TEMPLATE_ID_EXPR:
24481 /* And variable concepts. */
24482 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24483 return *tp;
24484 break;
24486 default:
24487 break;
24490 if (type_dependent_expression_p (*tp))
24491 return *tp;
24492 else
24493 return NULL_TREE;
24496 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24497 sense defined by the ABI:
24499 "An expression is instantiation-dependent if it is type-dependent
24500 or value-dependent, or it has a subexpression that is type-dependent
24501 or value-dependent."
24503 Except don't actually check value-dependence for unevaluated expressions,
24504 because in sizeof(i) we don't care about the value of i. Checking
24505 type-dependence will in turn check value-dependence of array bounds/template
24506 arguments as needed. */
24508 bool
24509 instantiation_dependent_uneval_expression_p (tree expression)
24511 tree result;
24513 if (!processing_template_decl)
24514 return false;
24516 if (expression == error_mark_node)
24517 return false;
24519 result = cp_walk_tree_without_duplicates (&expression,
24520 instantiation_dependent_r, NULL);
24521 return result != NULL_TREE;
24524 /* As above, but also check value-dependence of the expression as a whole. */
24526 bool
24527 instantiation_dependent_expression_p (tree expression)
24529 return (instantiation_dependent_uneval_expression_p (expression)
24530 || value_dependent_expression_p (expression));
24533 /* Like type_dependent_expression_p, but it also works while not processing
24534 a template definition, i.e. during substitution or mangling. */
24536 bool
24537 type_dependent_expression_p_push (tree expr)
24539 bool b;
24540 ++processing_template_decl;
24541 b = type_dependent_expression_p (expr);
24542 --processing_template_decl;
24543 return b;
24546 /* Returns TRUE if ARGS contains a type-dependent expression. */
24548 bool
24549 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24551 unsigned int i;
24552 tree arg;
24554 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24556 if (type_dependent_expression_p (arg))
24557 return true;
24559 return false;
24562 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24563 expressions) contains any type-dependent expressions. */
24565 bool
24566 any_type_dependent_elements_p (const_tree list)
24568 for (; list; list = TREE_CHAIN (list))
24569 if (type_dependent_expression_p (TREE_VALUE (list)))
24570 return true;
24572 return false;
24575 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24576 expressions) contains any value-dependent expressions. */
24578 bool
24579 any_value_dependent_elements_p (const_tree list)
24581 for (; list; list = TREE_CHAIN (list))
24582 if (value_dependent_expression_p (TREE_VALUE (list)))
24583 return true;
24585 return false;
24588 /* Returns TRUE if the ARG (a template argument) is dependent. */
24590 bool
24591 dependent_template_arg_p (tree arg)
24593 if (!processing_template_decl)
24594 return false;
24596 /* Assume a template argument that was wrongly written by the user
24597 is dependent. This is consistent with what
24598 any_dependent_template_arguments_p [that calls this function]
24599 does. */
24600 if (!arg || arg == error_mark_node)
24601 return true;
24603 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24604 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24606 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24607 return true;
24608 if (TREE_CODE (arg) == TEMPLATE_DECL)
24610 if (DECL_TEMPLATE_PARM_P (arg))
24611 return true;
24612 /* A member template of a dependent class is not necessarily
24613 type-dependent, but it is a dependent template argument because it
24614 will be a member of an unknown specialization to that template. */
24615 tree scope = CP_DECL_CONTEXT (arg);
24616 return TYPE_P (scope) && dependent_type_p (scope);
24618 else if (ARGUMENT_PACK_P (arg))
24620 tree args = ARGUMENT_PACK_ARGS (arg);
24621 int i, len = TREE_VEC_LENGTH (args);
24622 for (i = 0; i < len; ++i)
24624 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24625 return true;
24628 return false;
24630 else if (TYPE_P (arg))
24631 return dependent_type_p (arg);
24632 else
24633 return (type_dependent_expression_p (arg)
24634 || value_dependent_expression_p (arg));
24637 /* Returns true if ARGS (a collection of template arguments) contains
24638 any types that require structural equality testing. */
24640 bool
24641 any_template_arguments_need_structural_equality_p (tree args)
24643 int i;
24644 int j;
24646 if (!args)
24647 return false;
24648 if (args == error_mark_node)
24649 return true;
24651 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24653 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24654 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24656 tree arg = TREE_VEC_ELT (level, j);
24657 tree packed_args = NULL_TREE;
24658 int k, len = 1;
24660 if (ARGUMENT_PACK_P (arg))
24662 /* Look inside the argument pack. */
24663 packed_args = ARGUMENT_PACK_ARGS (arg);
24664 len = TREE_VEC_LENGTH (packed_args);
24667 for (k = 0; k < len; ++k)
24669 if (packed_args)
24670 arg = TREE_VEC_ELT (packed_args, k);
24672 if (error_operand_p (arg))
24673 return true;
24674 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24675 continue;
24676 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24677 return true;
24678 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24679 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24680 return true;
24685 return false;
24688 /* Returns true if ARGS (a collection of template arguments) contains
24689 any dependent arguments. */
24691 bool
24692 any_dependent_template_arguments_p (const_tree args)
24694 int i;
24695 int j;
24697 if (!args)
24698 return false;
24699 if (args == error_mark_node)
24700 return true;
24702 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24704 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24705 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24706 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24707 return true;
24710 return false;
24713 /* Returns TRUE if the template TMPL is type-dependent. */
24715 bool
24716 dependent_template_p (tree tmpl)
24718 if (TREE_CODE (tmpl) == OVERLOAD)
24720 for (lkp_iterator iter (tmpl); iter; ++iter)
24721 if (dependent_template_p (*iter))
24722 return true;
24723 return false;
24726 /* Template template parameters are dependent. */
24727 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24728 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24729 return true;
24730 /* So are names that have not been looked up. */
24731 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24732 return true;
24733 return false;
24736 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24738 bool
24739 dependent_template_id_p (tree tmpl, tree args)
24741 return (dependent_template_p (tmpl)
24742 || any_dependent_template_arguments_p (args));
24745 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24746 are dependent. */
24748 bool
24749 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24751 int i;
24753 if (!processing_template_decl)
24754 return false;
24756 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24758 tree decl = TREE_VEC_ELT (declv, i);
24759 tree init = TREE_VEC_ELT (initv, i);
24760 tree cond = TREE_VEC_ELT (condv, i);
24761 tree incr = TREE_VEC_ELT (incrv, i);
24763 if (type_dependent_expression_p (decl)
24764 || TREE_CODE (decl) == SCOPE_REF)
24765 return true;
24767 if (init && type_dependent_expression_p (init))
24768 return true;
24770 if (type_dependent_expression_p (cond))
24771 return true;
24773 if (COMPARISON_CLASS_P (cond)
24774 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24775 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24776 return true;
24778 if (TREE_CODE (incr) == MODOP_EXPR)
24780 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24781 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24782 return true;
24784 else if (type_dependent_expression_p (incr))
24785 return true;
24786 else if (TREE_CODE (incr) == MODIFY_EXPR)
24788 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24789 return true;
24790 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24792 tree t = TREE_OPERAND (incr, 1);
24793 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24794 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24795 return true;
24800 return false;
24803 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24804 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24805 no such TYPE can be found. Note that this function peers inside
24806 uninstantiated templates and therefore should be used only in
24807 extremely limited situations. ONLY_CURRENT_P restricts this
24808 peering to the currently open classes hierarchy (which is required
24809 when comparing types). */
24811 tree
24812 resolve_typename_type (tree type, bool only_current_p)
24814 tree scope;
24815 tree name;
24816 tree decl;
24817 int quals;
24818 tree pushed_scope;
24819 tree result;
24821 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24823 scope = TYPE_CONTEXT (type);
24824 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24825 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24826 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24827 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24828 identifier of the TYPENAME_TYPE anymore.
24829 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24830 TYPENAME_TYPE instead, we avoid messing up with a possible
24831 typedef variant case. */
24832 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24834 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24835 it first before we can figure out what NAME refers to. */
24836 if (TREE_CODE (scope) == TYPENAME_TYPE)
24838 if (TYPENAME_IS_RESOLVING_P (scope))
24839 /* Given a class template A with a dependent base with nested type C,
24840 typedef typename A::C::C C will land us here, as trying to resolve
24841 the initial A::C leads to the local C typedef, which leads back to
24842 A::C::C. So we break the recursion now. */
24843 return type;
24844 else
24845 scope = resolve_typename_type (scope, only_current_p);
24847 /* If we don't know what SCOPE refers to, then we cannot resolve the
24848 TYPENAME_TYPE. */
24849 if (!CLASS_TYPE_P (scope))
24850 return type;
24851 /* If this is a typedef, we don't want to look inside (c++/11987). */
24852 if (typedef_variant_p (type))
24853 return type;
24854 /* If SCOPE isn't the template itself, it will not have a valid
24855 TYPE_FIELDS list. */
24856 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24857 /* scope is either the template itself or a compatible instantiation
24858 like X<T>, so look up the name in the original template. */
24859 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24860 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24861 gcc_checking_assert (uses_template_parms (scope));
24862 /* If scope has no fields, it can't be a current instantiation. Check this
24863 before currently_open_class to avoid infinite recursion (71515). */
24864 if (!TYPE_FIELDS (scope))
24865 return type;
24866 /* If the SCOPE is not the current instantiation, there's no reason
24867 to look inside it. */
24868 if (only_current_p && !currently_open_class (scope))
24869 return type;
24870 /* Enter the SCOPE so that name lookup will be resolved as if we
24871 were in the class definition. In particular, SCOPE will no
24872 longer be considered a dependent type. */
24873 pushed_scope = push_scope (scope);
24874 /* Look up the declaration. */
24875 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24876 tf_warning_or_error);
24878 result = NULL_TREE;
24880 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24881 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24882 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24883 if (!decl)
24884 /*nop*/;
24885 else if (identifier_p (fullname)
24886 && TREE_CODE (decl) == TYPE_DECL)
24888 result = TREE_TYPE (decl);
24889 if (result == error_mark_node)
24890 result = NULL_TREE;
24892 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24893 && DECL_CLASS_TEMPLATE_P (decl))
24895 /* Obtain the template and the arguments. */
24896 tree tmpl = TREE_OPERAND (fullname, 0);
24897 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24899 /* We get here with a plain identifier because a previous tentative
24900 parse of the nested-name-specifier as part of a ptr-operator saw
24901 ::template X<A>. The use of ::template is necessary in a
24902 ptr-operator, but wrong in a declarator-id.
24904 [temp.names]: In a qualified-id of a declarator-id, the keyword
24905 template shall not appear at the top level. */
24906 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24907 "keyword %<template%> not allowed in declarator-id");
24908 tmpl = decl;
24910 tree args = TREE_OPERAND (fullname, 1);
24911 /* Instantiate the template. */
24912 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24913 /*entering_scope=*/true,
24914 tf_error | tf_user);
24915 if (result == error_mark_node)
24916 result = NULL_TREE;
24919 /* Leave the SCOPE. */
24920 if (pushed_scope)
24921 pop_scope (pushed_scope);
24923 /* If we failed to resolve it, return the original typename. */
24924 if (!result)
24925 return type;
24927 /* If lookup found a typename type, resolve that too. */
24928 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24930 /* Ill-formed programs can cause infinite recursion here, so we
24931 must catch that. */
24932 TYPENAME_IS_RESOLVING_P (result) = 1;
24933 result = resolve_typename_type (result, only_current_p);
24934 TYPENAME_IS_RESOLVING_P (result) = 0;
24937 /* Qualify the resulting type. */
24938 quals = cp_type_quals (type);
24939 if (quals)
24940 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24942 return result;
24945 /* EXPR is an expression which is not type-dependent. Return a proxy
24946 for EXPR that can be used to compute the types of larger
24947 expressions containing EXPR. */
24949 tree
24950 build_non_dependent_expr (tree expr)
24952 tree inner_expr;
24954 /* When checking, try to get a constant value for all non-dependent
24955 expressions in order to expose bugs in *_dependent_expression_p
24956 and constexpr. This can affect code generation, see PR70704, so
24957 only do this for -fchecking=2. */
24958 if (flag_checking > 1
24959 && cxx_dialect >= cxx11
24960 /* Don't do this during nsdmi parsing as it can lead to
24961 unexpected recursive instantiations. */
24962 && !parsing_nsdmi ()
24963 /* Don't do this during concept expansion either and for
24964 the same reason. */
24965 && !expanding_concept ())
24966 fold_non_dependent_expr (expr);
24968 /* Preserve OVERLOADs; the functions must be available to resolve
24969 types. */
24970 inner_expr = expr;
24971 if (TREE_CODE (inner_expr) == STMT_EXPR)
24972 inner_expr = stmt_expr_value_expr (inner_expr);
24973 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24974 inner_expr = TREE_OPERAND (inner_expr, 0);
24975 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24976 inner_expr = TREE_OPERAND (inner_expr, 1);
24977 if (is_overloaded_fn (inner_expr)
24978 || TREE_CODE (inner_expr) == OFFSET_REF)
24979 return expr;
24980 /* There is no need to return a proxy for a variable. */
24981 if (VAR_P (expr))
24982 return expr;
24983 /* Preserve string constants; conversions from string constants to
24984 "char *" are allowed, even though normally a "const char *"
24985 cannot be used to initialize a "char *". */
24986 if (TREE_CODE (expr) == STRING_CST)
24987 return expr;
24988 /* Preserve void and arithmetic constants, as an optimization -- there is no
24989 reason to create a new node. */
24990 if (TREE_CODE (expr) == VOID_CST
24991 || TREE_CODE (expr) == INTEGER_CST
24992 || TREE_CODE (expr) == REAL_CST)
24993 return expr;
24994 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24995 There is at least one place where we want to know that a
24996 particular expression is a throw-expression: when checking a ?:
24997 expression, there are special rules if the second or third
24998 argument is a throw-expression. */
24999 if (TREE_CODE (expr) == THROW_EXPR)
25000 return expr;
25002 /* Don't wrap an initializer list, we need to be able to look inside. */
25003 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25004 return expr;
25006 /* Don't wrap a dummy object, we need to be able to test for it. */
25007 if (is_dummy_object (expr))
25008 return expr;
25010 if (TREE_CODE (expr) == COND_EXPR)
25011 return build3 (COND_EXPR,
25012 TREE_TYPE (expr),
25013 TREE_OPERAND (expr, 0),
25014 (TREE_OPERAND (expr, 1)
25015 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25016 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25017 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25018 if (TREE_CODE (expr) == COMPOUND_EXPR
25019 && !COMPOUND_EXPR_OVERLOADED (expr))
25020 return build2 (COMPOUND_EXPR,
25021 TREE_TYPE (expr),
25022 TREE_OPERAND (expr, 0),
25023 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25025 /* If the type is unknown, it can't really be non-dependent */
25026 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25028 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25029 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25032 /* ARGS is a vector of expressions as arguments to a function call.
25033 Replace the arguments with equivalent non-dependent expressions.
25034 This modifies ARGS in place. */
25036 void
25037 make_args_non_dependent (vec<tree, va_gc> *args)
25039 unsigned int ix;
25040 tree arg;
25042 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25044 tree newarg = build_non_dependent_expr (arg);
25045 if (newarg != arg)
25046 (*args)[ix] = newarg;
25050 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25051 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25052 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25054 static tree
25055 make_auto_1 (tree name, bool set_canonical)
25057 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25058 TYPE_NAME (au) = build_decl (input_location,
25059 TYPE_DECL, name, au);
25060 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25061 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25062 (0, processing_template_decl + 1, processing_template_decl + 1,
25063 TYPE_NAME (au), NULL_TREE);
25064 if (set_canonical)
25065 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25066 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25067 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25069 return au;
25072 tree
25073 make_decltype_auto (void)
25075 return make_auto_1 (decltype_auto_identifier, true);
25078 tree
25079 make_auto (void)
25081 return make_auto_1 (auto_identifier, true);
25084 /* Return a C++17 deduction placeholder for class template TMPL. */
25086 tree
25087 make_template_placeholder (tree tmpl)
25089 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25090 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25091 return t;
25094 /* True iff T is a C++17 class template deduction placeholder. */
25096 bool
25097 template_placeholder_p (tree t)
25099 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25102 /* Make a "constrained auto" type-specifier. This is an
25103 auto type with constraints that must be associated after
25104 deduction. The constraint is formed from the given
25105 CONC and its optional sequence of arguments, which are
25106 non-null if written as partial-concept-id. */
25108 tree
25109 make_constrained_auto (tree con, tree args)
25111 tree type = make_auto_1 (auto_identifier, false);
25113 /* Build the constraint. */
25114 tree tmpl = DECL_TI_TEMPLATE (con);
25115 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25116 expr = build_concept_check (expr, type, args);
25118 tree constr = normalize_expression (expr);
25119 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25121 /* Our canonical type depends on the constraint. */
25122 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25124 /* Attach the constraint to the type declaration. */
25125 tree decl = TYPE_NAME (type);
25126 return decl;
25129 /* Given type ARG, return std::initializer_list<ARG>. */
25131 static tree
25132 listify (tree arg)
25134 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25136 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25138 gcc_rich_location richloc (input_location);
25139 maybe_add_include_fixit (&richloc, "<initializer_list>");
25140 error_at_rich_loc (&richloc,
25141 "deducing from brace-enclosed initializer list"
25142 " requires #include <initializer_list>");
25144 return error_mark_node;
25146 tree argvec = make_tree_vec (1);
25147 TREE_VEC_ELT (argvec, 0) = arg;
25149 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25150 NULL_TREE, 0, tf_warning_or_error);
25153 /* Replace auto in TYPE with std::initializer_list<auto>. */
25155 static tree
25156 listify_autos (tree type, tree auto_node)
25158 tree init_auto = listify (auto_node);
25159 tree argvec = make_tree_vec (1);
25160 TREE_VEC_ELT (argvec, 0) = init_auto;
25161 if (processing_template_decl)
25162 argvec = add_to_template_args (current_template_args (), argvec);
25163 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25166 /* Hash traits for hashing possibly constrained 'auto'
25167 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25169 struct auto_hash : default_hash_traits<tree>
25171 static inline hashval_t hash (tree);
25172 static inline bool equal (tree, tree);
25175 /* Hash the 'auto' T. */
25177 inline hashval_t
25178 auto_hash::hash (tree t)
25180 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25181 /* Matching constrained-type-specifiers denote the same template
25182 parameter, so hash the constraint. */
25183 return hash_placeholder_constraint (c);
25184 else
25185 /* But unconstrained autos are all separate, so just hash the pointer. */
25186 return iterative_hash_object (t, 0);
25189 /* Compare two 'auto's. */
25191 inline bool
25192 auto_hash::equal (tree t1, tree t2)
25194 if (t1 == t2)
25195 return true;
25197 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25198 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25200 /* Two unconstrained autos are distinct. */
25201 if (!c1 || !c2)
25202 return false;
25204 return equivalent_placeholder_constraints (c1, c2);
25207 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25208 constrained) auto, add it to the vector. */
25210 static int
25211 extract_autos_r (tree t, void *data)
25213 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25214 if (is_auto (t))
25216 /* All the autos were built with index 0; fix that up now. */
25217 tree *p = hash.find_slot (t, INSERT);
25218 unsigned idx;
25219 if (*p)
25220 /* If this is a repeated constrained-type-specifier, use the index we
25221 chose before. */
25222 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25223 else
25225 /* Otherwise this is new, so use the current count. */
25226 *p = t;
25227 idx = hash.elements () - 1;
25229 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25232 /* Always keep walking. */
25233 return 0;
25236 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25237 says they can appear anywhere in the type. */
25239 static tree
25240 extract_autos (tree type)
25242 hash_set<tree> visited;
25243 hash_table<auto_hash> hash (2);
25245 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25247 tree tree_vec = make_tree_vec (hash.elements());
25248 for (hash_table<auto_hash>::iterator iter = hash.begin();
25249 iter != hash.end(); ++iter)
25251 tree elt = *iter;
25252 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25253 TREE_VEC_ELT (tree_vec, i)
25254 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25257 return tree_vec;
25260 /* The stem for deduction guide names. */
25261 const char *const dguide_base = "__dguide_";
25263 /* Return the name for a deduction guide for class template TMPL. */
25265 tree
25266 dguide_name (tree tmpl)
25268 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25269 tree tname = TYPE_IDENTIFIER (type);
25270 char *buf = (char *) alloca (1 + strlen (dguide_base)
25271 + IDENTIFIER_LENGTH (tname));
25272 memcpy (buf, dguide_base, strlen (dguide_base));
25273 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25274 IDENTIFIER_LENGTH (tname) + 1);
25275 tree dname = get_identifier (buf);
25276 TREE_TYPE (dname) = type;
25277 return dname;
25280 /* True if NAME is the name of a deduction guide. */
25282 bool
25283 dguide_name_p (tree name)
25285 return (TREE_TYPE (name)
25286 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25287 strlen (dguide_base)));
25290 /* True if FN is a deduction guide. */
25292 bool
25293 deduction_guide_p (const_tree fn)
25295 if (DECL_P (fn))
25296 if (tree name = DECL_NAME (fn))
25297 return dguide_name_p (name);
25298 return false;
25301 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25303 bool
25304 copy_guide_p (const_tree fn)
25306 gcc_assert (deduction_guide_p (fn));
25307 if (!DECL_ARTIFICIAL (fn))
25308 return false;
25309 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25310 return (TREE_CHAIN (parms) == void_list_node
25311 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25314 /* True if FN is a guide generated from a constructor template. */
25316 bool
25317 template_guide_p (const_tree fn)
25319 gcc_assert (deduction_guide_p (fn));
25320 if (!DECL_ARTIFICIAL (fn))
25321 return false;
25322 tree tmpl = DECL_TI_TEMPLATE (fn);
25323 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25324 return PRIMARY_TEMPLATE_P (org);
25325 return false;
25328 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25329 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25330 template parameter types. Note that the handling of template template
25331 parameters relies on current_template_parms being set appropriately for the
25332 new template. */
25334 static tree
25335 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25336 tree tsubst_args, tsubst_flags_t complain)
25338 tree oldidx = get_template_parm_index (olddecl);
25340 tree newtype;
25341 if (TREE_CODE (olddecl) == TYPE_DECL
25342 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25344 tree oldtype = TREE_TYPE (olddecl);
25345 newtype = cxx_make_type (TREE_CODE (oldtype));
25346 TYPE_MAIN_VARIANT (newtype) = newtype;
25347 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25348 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25349 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25351 else
25352 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25353 complain, NULL_TREE);
25355 tree newdecl
25356 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25357 DECL_NAME (olddecl), newtype);
25358 SET_DECL_TEMPLATE_PARM_P (newdecl);
25360 tree newidx;
25361 if (TREE_CODE (olddecl) == TYPE_DECL
25362 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25364 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25365 = build_template_parm_index (index, level, level,
25366 newdecl, newtype);
25367 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25368 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25369 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25370 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25372 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25374 DECL_TEMPLATE_RESULT (newdecl)
25375 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25376 DECL_NAME (olddecl), newtype);
25377 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25378 // First create a copy (ttargs) of tsubst_args with an
25379 // additional level for the template template parameter's own
25380 // template parameters (ttparms).
25381 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25382 (DECL_TEMPLATE_PARMS (olddecl)));
25383 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25384 tree ttargs = make_tree_vec (depth + 1);
25385 for (int i = 0; i < depth; ++i)
25386 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25387 TREE_VEC_ELT (ttargs, depth)
25388 = template_parms_level_to_args (ttparms);
25389 // Substitute ttargs into ttparms to fix references to
25390 // other template parameters.
25391 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25392 complain);
25393 // Now substitute again with args based on tparms, to reduce
25394 // the level of the ttparms.
25395 ttargs = current_template_args ();
25396 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25397 complain);
25398 // Finally, tack the adjusted parms onto tparms.
25399 ttparms = tree_cons (size_int (depth), ttparms,
25400 current_template_parms);
25401 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25404 else
25406 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25407 tree newconst
25408 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25409 TREE_CODE (oldconst),
25410 DECL_NAME (oldconst), newtype);
25411 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25412 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25413 SET_DECL_TEMPLATE_PARM_P (newconst);
25414 newidx = build_template_parm_index (index, level, level,
25415 newconst, newtype);
25416 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25417 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25418 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25421 return newdecl;
25424 /* Returns a C++17 class deduction guide template based on the constructor
25425 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25426 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25428 static tree
25429 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25431 tree type, tparms, targs, fparms, fargs, ci;
25432 bool memtmpl = false;
25433 bool explicit_p;
25434 location_t loc;
25435 tree fn_tmpl = NULL_TREE;
25437 if (TYPE_P (ctor))
25439 type = ctor;
25440 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25441 if (copy_p)
25443 type = TREE_TYPE (type);
25444 fparms = tree_cons (NULL_TREE, type, void_list_node);
25446 else
25447 fparms = void_list_node;
25449 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25450 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25451 targs = CLASSTYPE_TI_ARGS (type);
25452 ci = NULL_TREE;
25453 fargs = NULL_TREE;
25454 loc = DECL_SOURCE_LOCATION (ctmpl);
25455 explicit_p = false;
25457 else
25459 ++processing_template_decl;
25461 fn_tmpl
25462 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25463 : DECL_TI_TEMPLATE (ctor));
25464 if (outer_args)
25465 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25466 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25468 type = DECL_CONTEXT (ctor);
25470 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25471 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25472 fully specialized args for the enclosing class. Strip those off, as
25473 the deduction guide won't have those template parameters. */
25474 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25475 TMPL_PARMS_DEPTH (tparms));
25476 /* Discard the 'this' parameter. */
25477 fparms = FUNCTION_ARG_CHAIN (ctor);
25478 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25479 ci = get_constraints (ctor);
25480 loc = DECL_SOURCE_LOCATION (ctor);
25481 explicit_p = DECL_NONCONVERTING_P (ctor);
25483 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25485 memtmpl = true;
25487 /* For a member template constructor, we need to flatten the two
25488 template parameter lists into one, and then adjust the function
25489 signature accordingly. This gets...complicated. */
25490 tree save_parms = current_template_parms;
25492 /* For a member template we should have two levels of parms/args, one
25493 for the class and one for the constructor. We stripped
25494 specialized args for further enclosing classes above. */
25495 const int depth = 2;
25496 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25498 /* Template args for translating references to the two-level template
25499 parameters into references to the one-level template parameters we
25500 are creating. */
25501 tree tsubst_args = copy_node (targs);
25502 TMPL_ARGS_LEVEL (tsubst_args, depth)
25503 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25505 /* Template parms for the constructor template. */
25506 tree ftparms = TREE_VALUE (tparms);
25507 unsigned flen = TREE_VEC_LENGTH (ftparms);
25508 /* Template parms for the class template. */
25509 tparms = TREE_CHAIN (tparms);
25510 tree ctparms = TREE_VALUE (tparms);
25511 unsigned clen = TREE_VEC_LENGTH (ctparms);
25512 /* Template parms for the deduction guide start as a copy of the
25513 template parms for the class. We set current_template_parms for
25514 lookup_template_class_1. */
25515 current_template_parms = tparms = copy_node (tparms);
25516 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25517 for (unsigned i = 0; i < clen; ++i)
25518 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25520 /* Now we need to rewrite the constructor parms to append them to the
25521 class parms. */
25522 for (unsigned i = 0; i < flen; ++i)
25524 unsigned index = i + clen;
25525 unsigned level = 1;
25526 tree oldelt = TREE_VEC_ELT (ftparms, i);
25527 tree olddecl = TREE_VALUE (oldelt);
25528 tree newdecl = rewrite_template_parm (olddecl, index, level,
25529 tsubst_args, complain);
25530 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25531 tsubst_args, complain, ctor);
25532 tree list = build_tree_list (newdef, newdecl);
25533 TEMPLATE_PARM_CONSTRAINTS (list)
25534 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25535 tsubst_args, complain, ctor);
25536 TREE_VEC_ELT (new_vec, index) = list;
25537 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25540 /* Now we have a final set of template parms to substitute into the
25541 function signature. */
25542 targs = template_parms_to_args (tparms);
25543 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25544 complain, ctor);
25545 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25546 if (ci)
25547 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25549 current_template_parms = save_parms;
25551 --processing_template_decl;
25554 if (!memtmpl)
25556 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25557 tparms = copy_node (tparms);
25558 INNERMOST_TEMPLATE_PARMS (tparms)
25559 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25562 tree fntype = build_function_type (type, fparms);
25563 tree ded_fn = build_lang_decl_loc (loc,
25564 FUNCTION_DECL,
25565 dguide_name (type), fntype);
25566 DECL_ARGUMENTS (ded_fn) = fargs;
25567 DECL_ARTIFICIAL (ded_fn) = true;
25568 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25569 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25570 DECL_ARTIFICIAL (ded_tmpl) = true;
25571 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25572 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25573 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25574 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25575 if (DECL_P (ctor))
25576 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25577 if (ci)
25578 set_constraints (ded_tmpl, ci);
25580 return ded_tmpl;
25583 /* Deduce template arguments for the class template placeholder PTYPE for
25584 template TMPL based on the initializer INIT, and return the resulting
25585 type. */
25587 static tree
25588 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25589 tsubst_flags_t complain)
25591 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25593 /* We should have handled this in the caller. */
25594 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25595 return ptype;
25596 if (complain & tf_error)
25597 error ("non-class template %qT used without template arguments", tmpl);
25598 return error_mark_node;
25601 tree type = TREE_TYPE (tmpl);
25603 bool try_list_ctor = false;
25605 vec<tree,va_gc> *args;
25606 if (init == NULL_TREE
25607 || TREE_CODE (init) == TREE_LIST)
25608 args = make_tree_vector_from_list (init);
25609 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25611 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25612 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25614 /* As an exception, the first phase in 16.3.1.7 (considering the
25615 initializer list as a single argument) is omitted if the
25616 initializer list consists of a single expression of type cv U,
25617 where U is a specialization of C or a class derived from a
25618 specialization of C. */
25619 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25620 tree etype = TREE_TYPE (elt);
25622 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25623 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25624 int err = unify (tparms, targs, type, etype,
25625 UNIFY_ALLOW_DERIVED, /*explain*/false);
25626 if (err == 0)
25627 try_list_ctor = false;
25628 ggc_free (targs);
25630 if (try_list_ctor || is_std_init_list (type))
25631 args = make_tree_vector_single (init);
25632 else
25633 args = make_tree_vector_from_ctor (init);
25635 else
25636 args = make_tree_vector_single (init);
25638 tree dname = dguide_name (tmpl);
25639 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25640 /*type*/false, /*complain*/false,
25641 /*hidden*/false);
25642 bool elided = false;
25643 if (cands == error_mark_node)
25644 cands = NULL_TREE;
25646 /* Prune explicit deduction guides in copy-initialization context. */
25647 if (flags & LOOKUP_ONLYCONVERTING)
25649 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25650 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25651 elided = true;
25653 if (elided)
25655 /* Found a nonconverting guide, prune the candidates. */
25656 tree pruned = NULL_TREE;
25657 for (lkp_iterator iter (cands); iter; ++iter)
25658 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25659 pruned = lookup_add (*iter, pruned);
25661 cands = pruned;
25665 tree outer_args = NULL_TREE;
25666 if (DECL_CLASS_SCOPE_P (tmpl)
25667 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25669 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25670 type = TREE_TYPE (most_general_template (tmpl));
25673 bool saw_ctor = false;
25674 // FIXME cache artificial deduction guides
25675 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25677 tree guide = build_deduction_guide (*iter, outer_args, complain);
25678 if ((flags & LOOKUP_ONLYCONVERTING)
25679 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25680 elided = true;
25681 else
25682 cands = lookup_add (guide, cands);
25684 saw_ctor = true;
25687 tree call = error_mark_node;
25689 /* If this is list-initialization and the class has a list constructor, first
25690 try deducing from the list as a single argument, as [over.match.list]. */
25691 tree list_cands = NULL_TREE;
25692 if (try_list_ctor && cands)
25693 for (lkp_iterator iter (cands); iter; ++iter)
25695 tree dg = *iter;
25696 if (is_list_ctor (dg))
25697 list_cands = lookup_add (dg, list_cands);
25699 if (list_cands)
25701 ++cp_unevaluated_operand;
25702 call = build_new_function_call (list_cands, &args, tf_decltype);
25703 --cp_unevaluated_operand;
25705 if (call == error_mark_node)
25707 /* That didn't work, now try treating the list as a sequence of
25708 arguments. */
25709 release_tree_vector (args);
25710 args = make_tree_vector_from_ctor (init);
25714 /* Maybe generate an implicit deduction guide. */
25715 if (call == error_mark_node && args->length () < 2)
25717 tree gtype = NULL_TREE;
25719 if (args->length () == 1)
25720 /* Generate a copy guide. */
25721 gtype = build_reference_type (type);
25722 else if (!saw_ctor)
25723 /* Generate a default guide. */
25724 gtype = type;
25726 if (gtype)
25728 tree guide = build_deduction_guide (gtype, outer_args, complain);
25729 cands = lookup_add (guide, cands);
25733 if (elided && !cands)
25735 error ("cannot deduce template arguments for copy-initialization"
25736 " of %qT, as it has no non-explicit deduction guides or "
25737 "user-declared constructors", type);
25738 return error_mark_node;
25740 else if (!cands && call == error_mark_node)
25742 error ("cannot deduce template arguments of %qT, as it has no viable "
25743 "deduction guides", type);
25744 return error_mark_node;
25747 if (call == error_mark_node)
25749 ++cp_unevaluated_operand;
25750 call = build_new_function_call (cands, &args, tf_decltype);
25751 --cp_unevaluated_operand;
25754 if (call == error_mark_node && (complain & tf_warning_or_error))
25756 error ("class template argument deduction failed:");
25758 ++cp_unevaluated_operand;
25759 call = build_new_function_call (cands, &args, complain | tf_decltype);
25760 --cp_unevaluated_operand;
25762 if (elided)
25763 inform (input_location, "explicit deduction guides not considered "
25764 "for copy-initialization");
25767 release_tree_vector (args);
25769 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25772 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25773 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25775 tree
25776 do_auto_deduction (tree type, tree init, tree auto_node)
25778 return do_auto_deduction (type, init, auto_node,
25779 tf_warning_or_error,
25780 adc_unspecified);
25783 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25784 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25785 The CONTEXT determines the context in which auto deduction is performed
25786 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25787 OUTER_TARGS are used during template argument deduction
25788 (context == adc_unify) to properly substitute the result, and is ignored
25789 in other contexts.
25791 For partial-concept-ids, extra args may be appended to the list of deduced
25792 template arguments prior to determining constraint satisfaction. */
25794 tree
25795 do_auto_deduction (tree type, tree init, tree auto_node,
25796 tsubst_flags_t complain, auto_deduction_context context,
25797 tree outer_targs, int flags)
25799 tree targs;
25801 if (init == error_mark_node)
25802 return error_mark_node;
25804 if (init && type_dependent_expression_p (init)
25805 && context != adc_unify)
25806 /* Defining a subset of type-dependent expressions that we can deduce
25807 from ahead of time isn't worth the trouble. */
25808 return type;
25810 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25811 /* C++17 class template argument deduction. */
25812 return do_class_deduction (type, tmpl, init, flags, complain);
25814 if (TREE_TYPE (init) == NULL_TREE)
25815 /* Nothing we can do with this, even in deduction context. */
25816 return type;
25818 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25819 with either a new invented type template parameter U or, if the
25820 initializer is a braced-init-list (8.5.4), with
25821 std::initializer_list<U>. */
25822 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25824 if (!DIRECT_LIST_INIT_P (init))
25825 type = listify_autos (type, auto_node);
25826 else if (CONSTRUCTOR_NELTS (init) == 1)
25827 init = CONSTRUCTOR_ELT (init, 0)->value;
25828 else
25830 if (complain & tf_warning_or_error)
25832 if (permerror (input_location, "direct-list-initialization of "
25833 "%<auto%> requires exactly one element"))
25834 inform (input_location,
25835 "for deduction to %<std::initializer_list%>, use copy-"
25836 "list-initialization (i.e. add %<=%> before the %<{%>)");
25838 type = listify_autos (type, auto_node);
25842 if (type == error_mark_node)
25843 return error_mark_node;
25845 init = resolve_nondeduced_context (init, complain);
25847 if (context == adc_decomp_type
25848 && auto_node == type
25849 && init != error_mark_node
25850 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25851 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25852 and initializer has array type, deduce cv-qualified array type. */
25853 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25854 complain);
25855 else if (AUTO_IS_DECLTYPE (auto_node))
25857 bool id = (DECL_P (init)
25858 || ((TREE_CODE (init) == COMPONENT_REF
25859 || TREE_CODE (init) == SCOPE_REF)
25860 && !REF_PARENTHESIZED_P (init)));
25861 targs = make_tree_vec (1);
25862 TREE_VEC_ELT (targs, 0)
25863 = finish_decltype_type (init, id, tf_warning_or_error);
25864 if (type != auto_node)
25866 if (complain & tf_error)
25867 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25868 return error_mark_node;
25871 else
25873 tree parms = build_tree_list (NULL_TREE, type);
25874 tree tparms;
25876 if (flag_concepts)
25877 tparms = extract_autos (type);
25878 else
25880 tparms = make_tree_vec (1);
25881 TREE_VEC_ELT (tparms, 0)
25882 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25885 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25886 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25887 DEDUCE_CALL, LOOKUP_NORMAL,
25888 NULL, /*explain_p=*/false);
25889 if (val > 0)
25891 if (processing_template_decl)
25892 /* Try again at instantiation time. */
25893 return type;
25894 if (type && type != error_mark_node
25895 && (complain & tf_error))
25896 /* If type is error_mark_node a diagnostic must have been
25897 emitted by now. Also, having a mention to '<type error>'
25898 in the diagnostic is not really useful to the user. */
25900 if (cfun && auto_node == current_function_auto_return_pattern
25901 && LAMBDA_FUNCTION_P (current_function_decl))
25902 error ("unable to deduce lambda return type from %qE", init);
25903 else
25904 error ("unable to deduce %qT from %qE", type, init);
25905 type_unification_real (tparms, targs, parms, &init, 1, 0,
25906 DEDUCE_CALL, LOOKUP_NORMAL,
25907 NULL, /*explain_p=*/true);
25909 return error_mark_node;
25913 /* Check any placeholder constraints against the deduced type. */
25914 if (flag_concepts && !processing_template_decl)
25915 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25917 /* Use the deduced type to check the associated constraints. If we
25918 have a partial-concept-id, rebuild the argument list so that
25919 we check using the extra arguments. */
25920 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25921 tree cargs = CHECK_CONSTR_ARGS (constr);
25922 if (TREE_VEC_LENGTH (cargs) > 1)
25924 cargs = copy_node (cargs);
25925 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25927 else
25928 cargs = targs;
25929 if (!constraints_satisfied_p (constr, cargs))
25931 if (complain & tf_warning_or_error)
25933 switch (context)
25935 case adc_unspecified:
25936 case adc_unify:
25937 error("placeholder constraints not satisfied");
25938 break;
25939 case adc_variable_type:
25940 case adc_decomp_type:
25941 error ("deduced initializer does not satisfy "
25942 "placeholder constraints");
25943 break;
25944 case adc_return_type:
25945 error ("deduced return type does not satisfy "
25946 "placeholder constraints");
25947 break;
25948 case adc_requirement:
25949 error ("deduced expression type does not satisfy "
25950 "placeholder constraints");
25951 break;
25953 diagnose_constraints (input_location, constr, targs);
25955 return error_mark_node;
25959 if (processing_template_decl && context != adc_unify)
25960 outer_targs = current_template_args ();
25961 targs = add_to_template_args (outer_targs, targs);
25962 return tsubst (type, targs, complain, NULL_TREE);
25965 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25966 result. */
25968 tree
25969 splice_late_return_type (tree type, tree late_return_type)
25971 if (is_auto (type))
25973 if (late_return_type)
25974 return late_return_type;
25976 tree idx = get_template_parm_index (type);
25977 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25978 /* In an abbreviated function template we didn't know we were dealing
25979 with a function template when we saw the auto return type, so update
25980 it to have the correct level. */
25981 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25983 return type;
25986 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25987 'decltype(auto)' or a deduced class template. */
25989 bool
25990 is_auto (const_tree type)
25992 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25993 && (TYPE_IDENTIFIER (type) == auto_identifier
25994 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25995 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25996 return true;
25997 else
25998 return false;
26001 /* for_each_template_parm callback for type_uses_auto. */
26004 is_auto_r (tree tp, void */*data*/)
26006 return is_auto (tp);
26009 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26010 a use of `auto'. Returns NULL_TREE otherwise. */
26012 tree
26013 type_uses_auto (tree type)
26015 if (type == NULL_TREE)
26016 return NULL_TREE;
26017 else if (flag_concepts)
26019 /* The Concepts TS allows multiple autos in one type-specifier; just
26020 return the first one we find, do_auto_deduction will collect all of
26021 them. */
26022 if (uses_template_parms (type))
26023 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26024 /*visited*/NULL, /*nondeduced*/true);
26025 else
26026 return NULL_TREE;
26028 else
26029 return find_type_usage (type, is_auto);
26032 /* For a given template T, return the vector of typedefs referenced
26033 in T for which access check is needed at T instantiation time.
26034 T is either a FUNCTION_DECL or a RECORD_TYPE.
26035 Those typedefs were added to T by the function
26036 append_type_to_template_for_access_check. */
26038 vec<qualified_typedef_usage_t, va_gc> *
26039 get_types_needing_access_check (tree t)
26041 tree ti;
26042 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26044 if (!t || t == error_mark_node)
26045 return NULL;
26047 if (!(ti = get_template_info (t)))
26048 return NULL;
26050 if (CLASS_TYPE_P (t)
26051 || TREE_CODE (t) == FUNCTION_DECL)
26053 if (!TI_TEMPLATE (ti))
26054 return NULL;
26056 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26059 return result;
26062 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26063 tied to T. That list of typedefs will be access checked at
26064 T instantiation time.
26065 T is either a FUNCTION_DECL or a RECORD_TYPE.
26066 TYPE_DECL is a TYPE_DECL node representing a typedef.
26067 SCOPE is the scope through which TYPE_DECL is accessed.
26068 LOCATION is the location of the usage point of TYPE_DECL.
26070 This function is a subroutine of
26071 append_type_to_template_for_access_check. */
26073 static void
26074 append_type_to_template_for_access_check_1 (tree t,
26075 tree type_decl,
26076 tree scope,
26077 location_t location)
26079 qualified_typedef_usage_t typedef_usage;
26080 tree ti;
26082 if (!t || t == error_mark_node)
26083 return;
26085 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26086 || CLASS_TYPE_P (t))
26087 && type_decl
26088 && TREE_CODE (type_decl) == TYPE_DECL
26089 && scope);
26091 if (!(ti = get_template_info (t)))
26092 return;
26094 gcc_assert (TI_TEMPLATE (ti));
26096 typedef_usage.typedef_decl = type_decl;
26097 typedef_usage.context = scope;
26098 typedef_usage.locus = location;
26100 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26103 /* Append TYPE_DECL to the template TEMPL.
26104 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26105 At TEMPL instanciation time, TYPE_DECL will be checked to see
26106 if it can be accessed through SCOPE.
26107 LOCATION is the location of the usage point of TYPE_DECL.
26109 e.g. consider the following code snippet:
26111 class C
26113 typedef int myint;
26116 template<class U> struct S
26118 C::myint mi; // <-- usage point of the typedef C::myint
26121 S<char> s;
26123 At S<char> instantiation time, we need to check the access of C::myint
26124 In other words, we need to check the access of the myint typedef through
26125 the C scope. For that purpose, this function will add the myint typedef
26126 and the scope C through which its being accessed to a list of typedefs
26127 tied to the template S. That list will be walked at template instantiation
26128 time and access check performed on each typedefs it contains.
26129 Note that this particular code snippet should yield an error because
26130 myint is private to C. */
26132 void
26133 append_type_to_template_for_access_check (tree templ,
26134 tree type_decl,
26135 tree scope,
26136 location_t location)
26138 qualified_typedef_usage_t *iter;
26139 unsigned i;
26141 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26143 /* Make sure we don't append the type to the template twice. */
26144 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26145 if (iter->typedef_decl == type_decl && scope == iter->context)
26146 return;
26148 append_type_to_template_for_access_check_1 (templ, type_decl,
26149 scope, location);
26152 /* Convert the generic type parameters in PARM that match the types given in the
26153 range [START_IDX, END_IDX) from the current_template_parms into generic type
26154 packs. */
26156 tree
26157 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26159 tree current = current_template_parms;
26160 int depth = TMPL_PARMS_DEPTH (current);
26161 current = INNERMOST_TEMPLATE_PARMS (current);
26162 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26164 for (int i = 0; i < start_idx; ++i)
26165 TREE_VEC_ELT (replacement, i)
26166 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26168 for (int i = start_idx; i < end_idx; ++i)
26170 /* Create a distinct parameter pack type from the current parm and add it
26171 to the replacement args to tsubst below into the generic function
26172 parameter. */
26174 tree o = TREE_TYPE (TREE_VALUE
26175 (TREE_VEC_ELT (current, i)));
26176 tree t = copy_type (o);
26177 TEMPLATE_TYPE_PARM_INDEX (t)
26178 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26179 o, 0, 0, tf_none);
26180 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26181 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26182 TYPE_MAIN_VARIANT (t) = t;
26183 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26184 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26185 TREE_VEC_ELT (replacement, i) = t;
26186 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26189 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26190 TREE_VEC_ELT (replacement, i)
26191 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26193 /* If there are more levels then build up the replacement with the outer
26194 template parms. */
26195 if (depth > 1)
26196 replacement = add_to_template_args (template_parms_to_args
26197 (TREE_CHAIN (current_template_parms)),
26198 replacement);
26200 return tsubst (parm, replacement, tf_none, NULL_TREE);
26203 /* Entries in the decl_constraint hash table. */
26204 struct GTY((for_user)) constr_entry
26206 tree decl;
26207 tree ci;
26210 /* Hashing function and equality for constraint entries. */
26211 struct constr_hasher : ggc_ptr_hash<constr_entry>
26213 static hashval_t hash (constr_entry *e)
26215 return (hashval_t)DECL_UID (e->decl);
26218 static bool equal (constr_entry *e1, constr_entry *e2)
26220 return e1->decl == e2->decl;
26224 /* A mapping from declarations to constraint information. Note that
26225 both templates and their underlying declarations are mapped to the
26226 same constraint information.
26228 FIXME: This is defined in pt.c because garbage collection
26229 code is not being generated for constraint.cc. */
26231 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26233 /* Returns the template constraints of declaration T. If T is not
26234 constrained, return NULL_TREE. Note that T must be non-null. */
26236 tree
26237 get_constraints (tree t)
26239 if (!flag_concepts)
26240 return NULL_TREE;
26242 gcc_assert (DECL_P (t));
26243 if (TREE_CODE (t) == TEMPLATE_DECL)
26244 t = DECL_TEMPLATE_RESULT (t);
26245 constr_entry elt = { t, NULL_TREE };
26246 constr_entry* found = decl_constraints->find (&elt);
26247 if (found)
26248 return found->ci;
26249 else
26250 return NULL_TREE;
26253 /* Associate the given constraint information CI with the declaration
26254 T. If T is a template, then the constraints are associated with
26255 its underlying declaration. Don't build associations if CI is
26256 NULL_TREE. */
26258 void
26259 set_constraints (tree t, tree ci)
26261 if (!ci)
26262 return;
26263 gcc_assert (t && flag_concepts);
26264 if (TREE_CODE (t) == TEMPLATE_DECL)
26265 t = DECL_TEMPLATE_RESULT (t);
26266 gcc_assert (!get_constraints (t));
26267 constr_entry elt = {t, ci};
26268 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26269 constr_entry* entry = ggc_alloc<constr_entry> ();
26270 *entry = elt;
26271 *slot = entry;
26274 /* Remove the associated constraints of the declaration T. */
26276 void
26277 remove_constraints (tree t)
26279 gcc_assert (DECL_P (t));
26280 if (TREE_CODE (t) == TEMPLATE_DECL)
26281 t = DECL_TEMPLATE_RESULT (t);
26283 constr_entry elt = {t, NULL_TREE};
26284 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26285 if (slot)
26286 decl_constraints->clear_slot (slot);
26289 /* Memoized satisfaction results for declarations. This
26290 maps the pair (constraint_info, arguments) to the result computed
26291 by constraints_satisfied_p. */
26293 struct GTY((for_user)) constraint_sat_entry
26295 tree ci;
26296 tree args;
26297 tree result;
26300 /* Hashing function and equality for constraint entries. */
26302 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26304 static hashval_t hash (constraint_sat_entry *e)
26306 hashval_t val = iterative_hash_object(e->ci, 0);
26307 return iterative_hash_template_arg (e->args, val);
26310 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26312 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26316 /* Memoized satisfaction results for concept checks. */
26318 struct GTY((for_user)) concept_spec_entry
26320 tree tmpl;
26321 tree args;
26322 tree result;
26325 /* Hashing function and equality for constraint entries. */
26327 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26329 static hashval_t hash (concept_spec_entry *e)
26331 return hash_tmpl_and_args (e->tmpl, e->args);
26334 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26336 ++comparing_specializations;
26337 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26338 --comparing_specializations;
26339 return eq;
26343 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26344 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26346 /* Search for a memoized satisfaction result. Returns one of the
26347 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26349 tree
26350 lookup_constraint_satisfaction (tree ci, tree args)
26352 constraint_sat_entry elt = { ci, args, NULL_TREE };
26353 constraint_sat_entry* found = constraint_memos->find (&elt);
26354 if (found)
26355 return found->result;
26356 else
26357 return NULL_TREE;
26360 /* Memoize the result of a satisfication test. Returns the saved result. */
26362 tree
26363 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26365 constraint_sat_entry elt = {ci, args, result};
26366 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26367 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26368 *entry = elt;
26369 *slot = entry;
26370 return result;
26373 /* Search for a memoized satisfaction result for a concept. */
26375 tree
26376 lookup_concept_satisfaction (tree tmpl, tree args)
26378 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26379 concept_spec_entry* found = concept_memos->find (&elt);
26380 if (found)
26381 return found->result;
26382 else
26383 return NULL_TREE;
26386 /* Memoize the result of a concept check. Returns the saved result. */
26388 tree
26389 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26391 concept_spec_entry elt = {tmpl, args, result};
26392 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26393 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26394 *entry = elt;
26395 *slot = entry;
26396 return result;
26399 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26401 /* Returns a prior concept specialization. This returns the substituted
26402 and normalized constraints defined by the concept. */
26404 tree
26405 get_concept_expansion (tree tmpl, tree args)
26407 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26408 concept_spec_entry* found = concept_expansions->find (&elt);
26409 if (found)
26410 return found->result;
26411 else
26412 return NULL_TREE;
26415 /* Save a concept expansion for later. */
26417 tree
26418 save_concept_expansion (tree tmpl, tree args, tree def)
26420 concept_spec_entry elt = {tmpl, args, def};
26421 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26422 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26423 *entry = elt;
26424 *slot = entry;
26425 return def;
26428 static hashval_t
26429 hash_subsumption_args (tree t1, tree t2)
26431 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26432 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26433 int val = 0;
26434 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26435 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26436 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26437 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26438 return val;
26441 /* Compare the constraints of two subsumption entries. The LEFT1 and
26442 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26443 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26445 static bool
26446 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26448 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26449 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26450 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26451 CHECK_CONSTR_ARGS (right1)))
26452 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26453 CHECK_CONSTR_ARGS (right2));
26454 return false;
26457 /* Key/value pair for learning and memoizing subsumption results. This
26458 associates a pair of check constraints (including arguments) with
26459 a boolean value indicating the result. */
26461 struct GTY((for_user)) subsumption_entry
26463 tree t1;
26464 tree t2;
26465 bool result;
26468 /* Hashing function and equality for constraint entries. */
26470 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26472 static hashval_t hash (subsumption_entry *e)
26474 return hash_subsumption_args (e->t1, e->t2);
26477 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26479 ++comparing_specializations;
26480 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26481 --comparing_specializations;
26482 return eq;
26486 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26488 /* Search for a previously cached subsumption result. */
26490 bool*
26491 lookup_subsumption_result (tree t1, tree t2)
26493 subsumption_entry elt = { t1, t2, false };
26494 subsumption_entry* found = subsumption_table->find (&elt);
26495 if (found)
26496 return &found->result;
26497 else
26498 return 0;
26501 /* Save a subsumption result. */
26503 bool
26504 save_subsumption_result (tree t1, tree t2, bool result)
26506 subsumption_entry elt = {t1, t2, result};
26507 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26508 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26509 *entry = elt;
26510 *slot = entry;
26511 return result;
26514 /* Set up the hash table for constraint association. */
26516 void
26517 init_constraint_processing (void)
26519 if (!flag_concepts)
26520 return;
26522 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26523 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26524 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26525 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26526 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26529 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26530 0..N-1. */
26532 void
26533 declare_integer_pack (void)
26535 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26536 build_function_type_list (integer_type_node,
26537 integer_type_node,
26538 NULL_TREE),
26539 NULL_TREE, ECF_CONST);
26540 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26541 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26544 /* Set up the hash tables for template instantiations. */
26546 void
26547 init_template_processing (void)
26549 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26550 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26552 if (cxx_dialect >= cxx11)
26553 declare_integer_pack ();
26556 /* Print stats about the template hash tables for -fstats. */
26558 void
26559 print_template_statistics (void)
26561 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26562 "%f collisions\n", (long) decl_specializations->size (),
26563 (long) decl_specializations->elements (),
26564 decl_specializations->collisions ());
26565 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26566 "%f collisions\n", (long) type_specializations->size (),
26567 (long) type_specializations->elements (),
26568 type_specializations->collisions ());
26571 #include "gt-cp-pt.h"