PR c++/82029 - __PRETTY_FUNCTION__ in lambda in template
[official-gcc.git] / gcc / cp / pt.c
blobd5ab93985a3f0192c4749227c33b2f0adc48410a
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_FUNCTION_P (tmpl))
1198 bool generic = PRIMARY_TEMPLATE_P (tmpl);
1199 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)) > generic)
1200 return NULL_TREE;
1202 /* But generic lambda functions are instantiated normally, once their
1203 containing context is fully instantiated. */
1204 gcc_assert (generic);
1207 if (optimize_specialization_lookup_p (tmpl))
1209 /* The template arguments actually apply to the containing
1210 class. Find the class specialization with those
1211 arguments. */
1212 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1213 tree class_specialization
1214 = retrieve_specialization (class_template, args, 0);
1215 if (!class_specialization)
1216 return NULL_TREE;
1218 /* Find the instance of TMPL. */
1219 tree fns = lookup_fnfields_slot (class_specialization, DECL_NAME (tmpl));
1220 for (ovl_iterator iter (fns); iter; ++iter)
1222 tree fn = *iter;
1223 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1224 /* using-declarations can add base methods to the method vec,
1225 and we don't want those here. */
1226 && DECL_CONTEXT (fn) == class_specialization)
1227 return fn;
1229 return NULL_TREE;
1231 else
1233 spec_entry *found;
1234 spec_entry elt;
1235 hash_table<spec_hasher> *specializations;
1237 elt.tmpl = tmpl;
1238 elt.args = args;
1239 elt.spec = NULL_TREE;
1241 if (DECL_CLASS_TEMPLATE_P (tmpl))
1242 specializations = type_specializations;
1243 else
1244 specializations = decl_specializations;
1246 if (hash == 0)
1247 hash = spec_hasher::hash (&elt);
1248 found = specializations->find_with_hash (&elt, hash);
1249 if (found)
1250 return found->spec;
1253 return NULL_TREE;
1256 /* Like retrieve_specialization, but for local declarations. */
1258 tree
1259 retrieve_local_specialization (tree tmpl)
1261 if (local_specializations == NULL)
1262 return NULL_TREE;
1264 tree *slot = local_specializations->get (tmpl);
1265 return slot ? *slot : NULL_TREE;
1268 /* Returns nonzero iff DECL is a specialization of TMPL. */
1271 is_specialization_of (tree decl, tree tmpl)
1273 tree t;
1275 if (TREE_CODE (decl) == FUNCTION_DECL)
1277 for (t = decl;
1278 t != NULL_TREE;
1279 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1280 if (t == tmpl)
1281 return 1;
1283 else
1285 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1287 for (t = TREE_TYPE (decl);
1288 t != NULL_TREE;
1289 t = CLASSTYPE_USE_TEMPLATE (t)
1290 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1291 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1292 return 1;
1295 return 0;
1298 /* Returns nonzero iff DECL is a specialization of friend declaration
1299 FRIEND_DECL according to [temp.friend]. */
1301 bool
1302 is_specialization_of_friend (tree decl, tree friend_decl)
1304 bool need_template = true;
1305 int template_depth;
1307 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1308 || TREE_CODE (decl) == TYPE_DECL);
1310 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1311 of a template class, we want to check if DECL is a specialization
1312 if this. */
1313 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1314 && DECL_TEMPLATE_INFO (friend_decl)
1315 && !DECL_USE_TEMPLATE (friend_decl))
1317 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1318 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1319 need_template = false;
1321 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1322 && !PRIMARY_TEMPLATE_P (friend_decl))
1323 need_template = false;
1325 /* There is nothing to do if this is not a template friend. */
1326 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1327 return false;
1329 if (is_specialization_of (decl, friend_decl))
1330 return true;
1332 /* [temp.friend/6]
1333 A member of a class template may be declared to be a friend of a
1334 non-template class. In this case, the corresponding member of
1335 every specialization of the class template is a friend of the
1336 class granting friendship.
1338 For example, given a template friend declaration
1340 template <class T> friend void A<T>::f();
1342 the member function below is considered a friend
1344 template <> struct A<int> {
1345 void f();
1348 For this type of template friend, TEMPLATE_DEPTH below will be
1349 nonzero. To determine if DECL is a friend of FRIEND, we first
1350 check if the enclosing class is a specialization of another. */
1352 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1353 if (template_depth
1354 && DECL_CLASS_SCOPE_P (decl)
1355 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1356 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1358 /* Next, we check the members themselves. In order to handle
1359 a few tricky cases, such as when FRIEND_DECL's are
1361 template <class T> friend void A<T>::g(T t);
1362 template <class T> template <T t> friend void A<T>::h();
1364 and DECL's are
1366 void A<int>::g(int);
1367 template <int> void A<int>::h();
1369 we need to figure out ARGS, the template arguments from
1370 the context of DECL. This is required for template substitution
1371 of `T' in the function parameter of `g' and template parameter
1372 of `h' in the above examples. Here ARGS corresponds to `int'. */
1374 tree context = DECL_CONTEXT (decl);
1375 tree args = NULL_TREE;
1376 int current_depth = 0;
1378 while (current_depth < template_depth)
1380 if (CLASSTYPE_TEMPLATE_INFO (context))
1382 if (current_depth == 0)
1383 args = TYPE_TI_ARGS (context);
1384 else
1385 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1386 current_depth++;
1388 context = TYPE_CONTEXT (context);
1391 if (TREE_CODE (decl) == FUNCTION_DECL)
1393 bool is_template;
1394 tree friend_type;
1395 tree decl_type;
1396 tree friend_args_type;
1397 tree decl_args_type;
1399 /* Make sure that both DECL and FRIEND_DECL are templates or
1400 non-templates. */
1401 is_template = DECL_TEMPLATE_INFO (decl)
1402 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1403 if (need_template ^ is_template)
1404 return false;
1405 else if (is_template)
1407 /* If both are templates, check template parameter list. */
1408 tree friend_parms
1409 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1410 args, tf_none);
1411 if (!comp_template_parms
1412 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1413 friend_parms))
1414 return false;
1416 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1418 else
1419 decl_type = TREE_TYPE (decl);
1421 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1422 tf_none, NULL_TREE);
1423 if (friend_type == error_mark_node)
1424 return false;
1426 /* Check if return types match. */
1427 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1428 return false;
1430 /* Check if function parameter types match, ignoring the
1431 `this' parameter. */
1432 friend_args_type = TYPE_ARG_TYPES (friend_type);
1433 decl_args_type = TYPE_ARG_TYPES (decl_type);
1434 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1435 friend_args_type = TREE_CHAIN (friend_args_type);
1436 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1437 decl_args_type = TREE_CHAIN (decl_args_type);
1439 return compparms (decl_args_type, friend_args_type);
1441 else
1443 /* DECL is a TYPE_DECL */
1444 bool is_template;
1445 tree decl_type = TREE_TYPE (decl);
1447 /* Make sure that both DECL and FRIEND_DECL are templates or
1448 non-templates. */
1449 is_template
1450 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1451 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1453 if (need_template ^ is_template)
1454 return false;
1455 else if (is_template)
1457 tree friend_parms;
1458 /* If both are templates, check the name of the two
1459 TEMPLATE_DECL's first because is_friend didn't. */
1460 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1461 != DECL_NAME (friend_decl))
1462 return false;
1464 /* Now check template parameter list. */
1465 friend_parms
1466 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1467 args, tf_none);
1468 return comp_template_parms
1469 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1470 friend_parms);
1472 else
1473 return (DECL_NAME (decl)
1474 == DECL_NAME (friend_decl));
1477 return false;
1480 /* Register the specialization SPEC as a specialization of TMPL with
1481 the indicated ARGS. IS_FRIEND indicates whether the specialization
1482 is actually just a friend declaration. Returns SPEC, or an
1483 equivalent prior declaration, if available.
1485 We also store instantiations of field packs in the hash table, even
1486 though they are not themselves templates, to make lookup easier. */
1488 static tree
1489 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1490 hashval_t hash)
1492 tree fn;
1493 spec_entry **slot = NULL;
1494 spec_entry elt;
1496 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1497 || (TREE_CODE (tmpl) == FIELD_DECL
1498 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1500 if (TREE_CODE (spec) == FUNCTION_DECL
1501 && uses_template_parms (DECL_TI_ARGS (spec)))
1502 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1503 register it; we want the corresponding TEMPLATE_DECL instead.
1504 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1505 the more obvious `uses_template_parms (spec)' to avoid problems
1506 with default function arguments. In particular, given
1507 something like this:
1509 template <class T> void f(T t1, T t = T())
1511 the default argument expression is not substituted for in an
1512 instantiation unless and until it is actually needed. */
1513 return spec;
1515 if (optimize_specialization_lookup_p (tmpl))
1516 /* We don't put these specializations in the hash table, but we might
1517 want to give an error about a mismatch. */
1518 fn = retrieve_specialization (tmpl, args, 0);
1519 else
1521 elt.tmpl = tmpl;
1522 elt.args = args;
1523 elt.spec = spec;
1525 if (hash == 0)
1526 hash = spec_hasher::hash (&elt);
1528 slot =
1529 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1530 if (*slot)
1531 fn = ((spec_entry *) *slot)->spec;
1532 else
1533 fn = NULL_TREE;
1536 /* We can sometimes try to re-register a specialization that we've
1537 already got. In particular, regenerate_decl_from_template calls
1538 duplicate_decls which will update the specialization list. But,
1539 we'll still get called again here anyhow. It's more convenient
1540 to simply allow this than to try to prevent it. */
1541 if (fn == spec)
1542 return spec;
1543 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1545 if (DECL_TEMPLATE_INSTANTIATION (fn))
1547 if (DECL_ODR_USED (fn)
1548 || DECL_EXPLICIT_INSTANTIATION (fn))
1550 error ("specialization of %qD after instantiation",
1551 fn);
1552 return error_mark_node;
1554 else
1556 tree clone;
1557 /* This situation should occur only if the first
1558 specialization is an implicit instantiation, the
1559 second is an explicit specialization, and the
1560 implicit instantiation has not yet been used. That
1561 situation can occur if we have implicitly
1562 instantiated a member function and then specialized
1563 it later.
1565 We can also wind up here if a friend declaration that
1566 looked like an instantiation turns out to be a
1567 specialization:
1569 template <class T> void foo(T);
1570 class S { friend void foo<>(int) };
1571 template <> void foo(int);
1573 We transform the existing DECL in place so that any
1574 pointers to it become pointers to the updated
1575 declaration.
1577 If there was a definition for the template, but not
1578 for the specialization, we want this to look as if
1579 there were no definition, and vice versa. */
1580 DECL_INITIAL (fn) = NULL_TREE;
1581 duplicate_decls (spec, fn, is_friend);
1582 /* The call to duplicate_decls will have applied
1583 [temp.expl.spec]:
1585 An explicit specialization of a function template
1586 is inline only if it is explicitly declared to be,
1587 and independently of whether its function template
1590 to the primary function; now copy the inline bits to
1591 the various clones. */
1592 FOR_EACH_CLONE (clone, fn)
1594 DECL_DECLARED_INLINE_P (clone)
1595 = DECL_DECLARED_INLINE_P (fn);
1596 DECL_SOURCE_LOCATION (clone)
1597 = DECL_SOURCE_LOCATION (fn);
1598 DECL_DELETED_FN (clone)
1599 = DECL_DELETED_FN (fn);
1601 check_specialization_namespace (tmpl);
1603 return fn;
1606 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1608 tree dd = duplicate_decls (spec, fn, is_friend);
1609 if (dd == error_mark_node)
1610 /* We've already complained in duplicate_decls. */
1611 return error_mark_node;
1613 if (dd == NULL_TREE && DECL_INITIAL (spec))
1614 /* Dup decl failed, but this is a new definition. Set the
1615 line number so any errors match this new
1616 definition. */
1617 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1619 return fn;
1622 else if (fn)
1623 return duplicate_decls (spec, fn, is_friend);
1625 /* A specialization must be declared in the same namespace as the
1626 template it is specializing. */
1627 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1628 && !check_specialization_namespace (tmpl))
1629 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1631 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1633 spec_entry *entry = ggc_alloc<spec_entry> ();
1634 gcc_assert (tmpl && args && spec);
1635 *entry = elt;
1636 *slot = entry;
1637 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1638 && PRIMARY_TEMPLATE_P (tmpl)
1639 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1640 || variable_template_p (tmpl))
1641 /* If TMPL is a forward declaration of a template function, keep a list
1642 of all specializations in case we need to reassign them to a friend
1643 template later in tsubst_friend_function.
1645 Also keep a list of all variable template instantiations so that
1646 process_partial_specialization can check whether a later partial
1647 specialization would have used it. */
1648 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1649 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1652 return spec;
1655 /* Returns true iff two spec_entry nodes are equivalent. */
1657 int comparing_specializations;
1659 bool
1660 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1662 int equal;
1664 ++comparing_specializations;
1665 equal = (e1->tmpl == e2->tmpl
1666 && comp_template_args (e1->args, e2->args));
1667 if (equal && flag_concepts
1668 /* tmpl could be a FIELD_DECL for a capture pack. */
1669 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1670 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1671 && uses_template_parms (e1->args))
1673 /* Partial specializations of a variable template can be distinguished by
1674 constraints. */
1675 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1676 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1677 equal = equivalent_constraints (c1, c2);
1679 --comparing_specializations;
1681 return equal;
1684 /* Returns a hash for a template TMPL and template arguments ARGS. */
1686 static hashval_t
1687 hash_tmpl_and_args (tree tmpl, tree args)
1689 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1690 return iterative_hash_template_arg (args, val);
1693 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1694 ignoring SPEC. */
1696 hashval_t
1697 spec_hasher::hash (spec_entry *e)
1699 return hash_tmpl_and_args (e->tmpl, e->args);
1702 /* Recursively calculate a hash value for a template argument ARG, for use
1703 in the hash tables of template specializations. */
1705 hashval_t
1706 iterative_hash_template_arg (tree arg, hashval_t val)
1708 unsigned HOST_WIDE_INT i;
1709 enum tree_code code;
1710 char tclass;
1712 if (arg == NULL_TREE)
1713 return iterative_hash_object (arg, val);
1715 if (!TYPE_P (arg))
1716 STRIP_NOPS (arg);
1718 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1719 gcc_unreachable ();
1721 code = TREE_CODE (arg);
1722 tclass = TREE_CODE_CLASS (code);
1724 val = iterative_hash_object (code, val);
1726 switch (code)
1728 case ERROR_MARK:
1729 return val;
1731 case IDENTIFIER_NODE:
1732 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1734 case TREE_VEC:
1736 int i, len = TREE_VEC_LENGTH (arg);
1737 for (i = 0; i < len; ++i)
1738 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1739 return val;
1742 case TYPE_PACK_EXPANSION:
1743 case EXPR_PACK_EXPANSION:
1744 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1745 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1747 case TYPE_ARGUMENT_PACK:
1748 case NONTYPE_ARGUMENT_PACK:
1749 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1751 case TREE_LIST:
1752 for (; arg; arg = TREE_CHAIN (arg))
1753 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1754 return val;
1756 case OVERLOAD:
1757 for (lkp_iterator iter (arg); iter; ++iter)
1758 val = iterative_hash_template_arg (*iter, val);
1759 return val;
1761 case CONSTRUCTOR:
1763 tree field, value;
1764 iterative_hash_template_arg (TREE_TYPE (arg), val);
1765 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1767 val = iterative_hash_template_arg (field, val);
1768 val = iterative_hash_template_arg (value, val);
1770 return val;
1773 case PARM_DECL:
1774 if (!DECL_ARTIFICIAL (arg))
1776 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1777 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1779 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1781 case TARGET_EXPR:
1782 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1784 case PTRMEM_CST:
1785 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1786 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1788 case TEMPLATE_PARM_INDEX:
1789 val = iterative_hash_template_arg
1790 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1791 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1792 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1794 case TRAIT_EXPR:
1795 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1796 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1797 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1799 case BASELINK:
1800 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1801 val);
1802 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1803 val);
1805 case MODOP_EXPR:
1806 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1807 code = TREE_CODE (TREE_OPERAND (arg, 1));
1808 val = iterative_hash_object (code, val);
1809 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1811 case LAMBDA_EXPR:
1812 /* A lambda can't appear in a template arg, but don't crash on
1813 erroneous input. */
1814 gcc_assert (seen_error ());
1815 return val;
1817 case CAST_EXPR:
1818 case IMPLICIT_CONV_EXPR:
1819 case STATIC_CAST_EXPR:
1820 case REINTERPRET_CAST_EXPR:
1821 case CONST_CAST_EXPR:
1822 case DYNAMIC_CAST_EXPR:
1823 case NEW_EXPR:
1824 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1825 /* Now hash operands as usual. */
1826 break;
1828 default:
1829 break;
1832 switch (tclass)
1834 case tcc_type:
1835 if (alias_template_specialization_p (arg))
1837 // We want an alias specialization that survived strip_typedefs
1838 // to hash differently from its TYPE_CANONICAL, to avoid hash
1839 // collisions that compare as different in template_args_equal.
1840 // These could be dependent specializations that strip_typedefs
1841 // left alone, or untouched specializations because
1842 // coerce_template_parms returns the unconverted template
1843 // arguments if it sees incomplete argument packs.
1844 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1845 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1847 if (TYPE_CANONICAL (arg))
1848 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1849 val);
1850 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1851 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1852 /* Otherwise just compare the types during lookup. */
1853 return val;
1855 case tcc_declaration:
1856 case tcc_constant:
1857 return iterative_hash_expr (arg, val);
1859 default:
1860 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1862 unsigned n = cp_tree_operand_length (arg);
1863 for (i = 0; i < n; ++i)
1864 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1865 return val;
1868 gcc_unreachable ();
1869 return 0;
1872 /* Unregister the specialization SPEC as a specialization of TMPL.
1873 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1874 if the SPEC was listed as a specialization of TMPL.
1876 Note that SPEC has been ggc_freed, so we can't look inside it. */
1878 bool
1879 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1881 spec_entry *entry;
1882 spec_entry elt;
1884 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1885 elt.args = TI_ARGS (tinfo);
1886 elt.spec = NULL_TREE;
1888 entry = decl_specializations->find (&elt);
1889 if (entry != NULL)
1891 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1892 gcc_assert (new_spec != NULL_TREE);
1893 entry->spec = new_spec;
1894 return 1;
1897 return 0;
1900 /* Like register_specialization, but for local declarations. We are
1901 registering SPEC, an instantiation of TMPL. */
1903 void
1904 register_local_specialization (tree spec, tree tmpl)
1906 local_specializations->put (tmpl, spec);
1909 /* TYPE is a class type. Returns true if TYPE is an explicitly
1910 specialized class. */
1912 bool
1913 explicit_class_specialization_p (tree type)
1915 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1916 return false;
1917 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1920 /* Print the list of functions at FNS, going through all the overloads
1921 for each element of the list. Alternatively, FNS can not be a
1922 TREE_LIST, in which case it will be printed together with all the
1923 overloads.
1925 MORE and *STR should respectively be FALSE and NULL when the function
1926 is called from the outside. They are used internally on recursive
1927 calls. print_candidates manages the two parameters and leaves NULL
1928 in *STR when it ends. */
1930 static void
1931 print_candidates_1 (tree fns, char **str, bool more = false)
1933 if (TREE_CODE (fns) == TREE_LIST)
1934 for (; fns; fns = TREE_CHAIN (fns))
1935 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1936 else
1937 for (lkp_iterator iter (fns); iter;)
1939 tree cand = *iter;
1940 ++iter;
1942 const char *pfx = *str;
1943 if (!pfx)
1945 if (more || iter)
1946 pfx = _("candidates are:");
1947 else
1948 pfx = _("candidate is:");
1949 *str = get_spaces (pfx);
1951 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1955 /* Print the list of candidate FNS in an error message. FNS can also
1956 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1958 void
1959 print_candidates (tree fns)
1961 char *str = NULL;
1962 print_candidates_1 (fns, &str);
1963 free (str);
1966 /* Get a (possibly) constrained template declaration for the
1967 purpose of ordering candidates. */
1968 static tree
1969 get_template_for_ordering (tree list)
1971 gcc_assert (TREE_CODE (list) == TREE_LIST);
1972 tree f = TREE_VALUE (list);
1973 if (tree ti = DECL_TEMPLATE_INFO (f))
1974 return TI_TEMPLATE (ti);
1975 return f;
1978 /* Among candidates having the same signature, return the
1979 most constrained or NULL_TREE if there is no best candidate.
1980 If the signatures of candidates vary (e.g., template
1981 specialization vs. member function), then there can be no
1982 most constrained.
1984 Note that we don't compare constraints on the functions
1985 themselves, but rather those of their templates. */
1986 static tree
1987 most_constrained_function (tree candidates)
1989 // Try to find the best candidate in a first pass.
1990 tree champ = candidates;
1991 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1993 int winner = more_constrained (get_template_for_ordering (champ),
1994 get_template_for_ordering (c));
1995 if (winner == -1)
1996 champ = c; // The candidate is more constrained
1997 else if (winner == 0)
1998 return NULL_TREE; // Neither is more constrained
2001 // Verify that the champ is better than previous candidates.
2002 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2003 if (!more_constrained (get_template_for_ordering (champ),
2004 get_template_for_ordering (c)))
2005 return NULL_TREE;
2008 return champ;
2012 /* Returns the template (one of the functions given by TEMPLATE_ID)
2013 which can be specialized to match the indicated DECL with the
2014 explicit template args given in TEMPLATE_ID. The DECL may be
2015 NULL_TREE if none is available. In that case, the functions in
2016 TEMPLATE_ID are non-members.
2018 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2019 specialization of a member template.
2021 The TEMPLATE_COUNT is the number of references to qualifying
2022 template classes that appeared in the name of the function. See
2023 check_explicit_specialization for a more accurate description.
2025 TSK indicates what kind of template declaration (if any) is being
2026 declared. TSK_TEMPLATE indicates that the declaration given by
2027 DECL, though a FUNCTION_DECL, has template parameters, and is
2028 therefore a template function.
2030 The template args (those explicitly specified and those deduced)
2031 are output in a newly created vector *TARGS_OUT.
2033 If it is impossible to determine the result, an error message is
2034 issued. The error_mark_node is returned to indicate failure. */
2036 static tree
2037 determine_specialization (tree template_id,
2038 tree decl,
2039 tree* targs_out,
2040 int need_member_template,
2041 int template_count,
2042 tmpl_spec_kind tsk)
2044 tree fns;
2045 tree targs;
2046 tree explicit_targs;
2047 tree candidates = NULL_TREE;
2049 /* A TREE_LIST of templates of which DECL may be a specialization.
2050 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2051 corresponding TREE_PURPOSE is the set of template arguments that,
2052 when used to instantiate the template, would produce a function
2053 with the signature of DECL. */
2054 tree templates = NULL_TREE;
2055 int header_count;
2056 cp_binding_level *b;
2058 *targs_out = NULL_TREE;
2060 if (template_id == error_mark_node || decl == error_mark_node)
2061 return error_mark_node;
2063 /* We shouldn't be specializing a member template of an
2064 unspecialized class template; we already gave an error in
2065 check_specialization_scope, now avoid crashing. */
2066 if (template_count && DECL_CLASS_SCOPE_P (decl)
2067 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2069 gcc_assert (errorcount);
2070 return error_mark_node;
2073 fns = TREE_OPERAND (template_id, 0);
2074 explicit_targs = TREE_OPERAND (template_id, 1);
2076 if (fns == error_mark_node)
2077 return error_mark_node;
2079 /* Check for baselinks. */
2080 if (BASELINK_P (fns))
2081 fns = BASELINK_FUNCTIONS (fns);
2083 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2085 error ("%qD is not a function template", fns);
2086 return error_mark_node;
2088 else if (VAR_P (decl) && !variable_template_p (fns))
2090 error ("%qD is not a variable template", fns);
2091 return error_mark_node;
2094 /* Count the number of template headers specified for this
2095 specialization. */
2096 header_count = 0;
2097 for (b = current_binding_level;
2098 b->kind == sk_template_parms;
2099 b = b->level_chain)
2100 ++header_count;
2102 tree orig_fns = fns;
2104 if (variable_template_p (fns))
2106 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2107 targs = coerce_template_parms (parms, explicit_targs, fns,
2108 tf_warning_or_error,
2109 /*req_all*/true, /*use_defarg*/true);
2110 if (targs != error_mark_node)
2111 templates = tree_cons (targs, fns, templates);
2113 else for (lkp_iterator iter (fns); iter; ++iter)
2115 tree fn = *iter;
2117 if (TREE_CODE (fn) == TEMPLATE_DECL)
2119 tree decl_arg_types;
2120 tree fn_arg_types;
2121 tree insttype;
2123 /* In case of explicit specialization, we need to check if
2124 the number of template headers appearing in the specialization
2125 is correct. This is usually done in check_explicit_specialization,
2126 but the check done there cannot be exhaustive when specializing
2127 member functions. Consider the following code:
2129 template <> void A<int>::f(int);
2130 template <> template <> void A<int>::f(int);
2132 Assuming that A<int> is not itself an explicit specialization
2133 already, the first line specializes "f" which is a non-template
2134 member function, whilst the second line specializes "f" which
2135 is a template member function. So both lines are syntactically
2136 correct, and check_explicit_specialization does not reject
2137 them.
2139 Here, we can do better, as we are matching the specialization
2140 against the declarations. We count the number of template
2141 headers, and we check if they match TEMPLATE_COUNT + 1
2142 (TEMPLATE_COUNT is the number of qualifying template classes,
2143 plus there must be another header for the member template
2144 itself).
2146 Notice that if header_count is zero, this is not a
2147 specialization but rather a template instantiation, so there
2148 is no check we can perform here. */
2149 if (header_count && header_count != template_count + 1)
2150 continue;
2152 /* Check that the number of template arguments at the
2153 innermost level for DECL is the same as for FN. */
2154 if (current_binding_level->kind == sk_template_parms
2155 && !current_binding_level->explicit_spec_p
2156 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2157 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2158 (current_template_parms))))
2159 continue;
2161 /* DECL might be a specialization of FN. */
2162 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2163 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2165 /* For a non-static member function, we need to make sure
2166 that the const qualification is the same. Since
2167 get_bindings does not try to merge the "this" parameter,
2168 we must do the comparison explicitly. */
2169 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2170 && !same_type_p (TREE_VALUE (fn_arg_types),
2171 TREE_VALUE (decl_arg_types)))
2172 continue;
2174 /* Skip the "this" parameter and, for constructors of
2175 classes with virtual bases, the VTT parameter. A
2176 full specialization of a constructor will have a VTT
2177 parameter, but a template never will. */
2178 decl_arg_types
2179 = skip_artificial_parms_for (decl, decl_arg_types);
2180 fn_arg_types
2181 = skip_artificial_parms_for (fn, fn_arg_types);
2183 /* Function templates cannot be specializations; there are
2184 no partial specializations of functions. Therefore, if
2185 the type of DECL does not match FN, there is no
2186 match.
2188 Note that it should never be the case that we have both
2189 candidates added here, and for regular member functions
2190 below. */
2191 if (tsk == tsk_template)
2193 if (compparms (fn_arg_types, decl_arg_types))
2194 candidates = tree_cons (NULL_TREE, fn, candidates);
2195 continue;
2198 /* See whether this function might be a specialization of this
2199 template. Suppress access control because we might be trying
2200 to make this specialization a friend, and we have already done
2201 access control for the declaration of the specialization. */
2202 push_deferring_access_checks (dk_no_check);
2203 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2204 pop_deferring_access_checks ();
2206 if (!targs)
2207 /* We cannot deduce template arguments that when used to
2208 specialize TMPL will produce DECL. */
2209 continue;
2211 /* Remove, from the set of candidates, all those functions
2212 whose constraints are not satisfied. */
2213 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2214 continue;
2216 // Then, try to form the new function type.
2217 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2218 if (insttype == error_mark_node)
2219 continue;
2220 fn_arg_types
2221 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2222 if (!compparms (fn_arg_types, decl_arg_types))
2223 continue;
2225 /* Save this template, and the arguments deduced. */
2226 templates = tree_cons (targs, fn, templates);
2228 else if (need_member_template)
2229 /* FN is an ordinary member function, and we need a
2230 specialization of a member template. */
2232 else if (TREE_CODE (fn) != FUNCTION_DECL)
2233 /* We can get IDENTIFIER_NODEs here in certain erroneous
2234 cases. */
2236 else if (!DECL_FUNCTION_MEMBER_P (fn))
2237 /* This is just an ordinary non-member function. Nothing can
2238 be a specialization of that. */
2240 else if (DECL_ARTIFICIAL (fn))
2241 /* Cannot specialize functions that are created implicitly. */
2243 else
2245 tree decl_arg_types;
2247 /* This is an ordinary member function. However, since
2248 we're here, we can assume its enclosing class is a
2249 template class. For example,
2251 template <typename T> struct S { void f(); };
2252 template <> void S<int>::f() {}
2254 Here, S<int>::f is a non-template, but S<int> is a
2255 template class. If FN has the same type as DECL, we
2256 might be in business. */
2258 if (!DECL_TEMPLATE_INFO (fn))
2259 /* Its enclosing class is an explicit specialization
2260 of a template class. This is not a candidate. */
2261 continue;
2263 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2264 TREE_TYPE (TREE_TYPE (fn))))
2265 /* The return types differ. */
2266 continue;
2268 /* Adjust the type of DECL in case FN is a static member. */
2269 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2270 if (DECL_STATIC_FUNCTION_P (fn)
2271 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2272 decl_arg_types = TREE_CHAIN (decl_arg_types);
2274 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2275 decl_arg_types))
2276 continue;
2278 // If the deduced arguments do not satisfy the constraints,
2279 // this is not a candidate.
2280 if (flag_concepts && !constraints_satisfied_p (fn))
2281 continue;
2283 // Add the candidate.
2284 candidates = tree_cons (NULL_TREE, fn, candidates);
2288 if (templates && TREE_CHAIN (templates))
2290 /* We have:
2292 [temp.expl.spec]
2294 It is possible for a specialization with a given function
2295 signature to be instantiated from more than one function
2296 template. In such cases, explicit specification of the
2297 template arguments must be used to uniquely identify the
2298 function template specialization being specialized.
2300 Note that here, there's no suggestion that we're supposed to
2301 determine which of the candidate templates is most
2302 specialized. However, we, also have:
2304 [temp.func.order]
2306 Partial ordering of overloaded function template
2307 declarations is used in the following contexts to select
2308 the function template to which a function template
2309 specialization refers:
2311 -- when an explicit specialization refers to a function
2312 template.
2314 So, we do use the partial ordering rules, at least for now.
2315 This extension can only serve to make invalid programs valid,
2316 so it's safe. And, there is strong anecdotal evidence that
2317 the committee intended the partial ordering rules to apply;
2318 the EDG front end has that behavior, and John Spicer claims
2319 that the committee simply forgot to delete the wording in
2320 [temp.expl.spec]. */
2321 tree tmpl = most_specialized_instantiation (templates);
2322 if (tmpl != error_mark_node)
2324 templates = tmpl;
2325 TREE_CHAIN (templates) = NULL_TREE;
2329 // Concepts allows multiple declarations of member functions
2330 // with the same signature. Like above, we need to rely on
2331 // on the partial ordering of those candidates to determine which
2332 // is the best.
2333 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2335 if (tree cand = most_constrained_function (candidates))
2337 candidates = cand;
2338 TREE_CHAIN (cand) = NULL_TREE;
2342 if (templates == NULL_TREE && candidates == NULL_TREE)
2344 error ("template-id %qD for %q+D does not match any template "
2345 "declaration", template_id, decl);
2346 if (header_count && header_count != template_count + 1)
2347 inform (input_location, "saw %d %<template<>%>, need %d for "
2348 "specializing a member function template",
2349 header_count, template_count + 1);
2350 else
2351 print_candidates (orig_fns);
2352 return error_mark_node;
2354 else if ((templates && TREE_CHAIN (templates))
2355 || (candidates && TREE_CHAIN (candidates))
2356 || (templates && candidates))
2358 error ("ambiguous template specialization %qD for %q+D",
2359 template_id, decl);
2360 candidates = chainon (candidates, templates);
2361 print_candidates (candidates);
2362 return error_mark_node;
2365 /* We have one, and exactly one, match. */
2366 if (candidates)
2368 tree fn = TREE_VALUE (candidates);
2369 *targs_out = copy_node (DECL_TI_ARGS (fn));
2371 // Propagate the candidate's constraints to the declaration.
2372 set_constraints (decl, get_constraints (fn));
2374 /* DECL is a re-declaration or partial instantiation of a template
2375 function. */
2376 if (TREE_CODE (fn) == TEMPLATE_DECL)
2377 return fn;
2378 /* It was a specialization of an ordinary member function in a
2379 template class. */
2380 return DECL_TI_TEMPLATE (fn);
2383 /* It was a specialization of a template. */
2384 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2385 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2387 *targs_out = copy_node (targs);
2388 SET_TMPL_ARGS_LEVEL (*targs_out,
2389 TMPL_ARGS_DEPTH (*targs_out),
2390 TREE_PURPOSE (templates));
2392 else
2393 *targs_out = TREE_PURPOSE (templates);
2394 return TREE_VALUE (templates);
2397 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2398 but with the default argument values filled in from those in the
2399 TMPL_TYPES. */
2401 static tree
2402 copy_default_args_to_explicit_spec_1 (tree spec_types,
2403 tree tmpl_types)
2405 tree new_spec_types;
2407 if (!spec_types)
2408 return NULL_TREE;
2410 if (spec_types == void_list_node)
2411 return void_list_node;
2413 /* Substitute into the rest of the list. */
2414 new_spec_types =
2415 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2416 TREE_CHAIN (tmpl_types));
2418 /* Add the default argument for this parameter. */
2419 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2420 TREE_VALUE (spec_types),
2421 new_spec_types);
2424 /* DECL is an explicit specialization. Replicate default arguments
2425 from the template it specializes. (That way, code like:
2427 template <class T> void f(T = 3);
2428 template <> void f(double);
2429 void g () { f (); }
2431 works, as required.) An alternative approach would be to look up
2432 the correct default arguments at the call-site, but this approach
2433 is consistent with how implicit instantiations are handled. */
2435 static void
2436 copy_default_args_to_explicit_spec (tree decl)
2438 tree tmpl;
2439 tree spec_types;
2440 tree tmpl_types;
2441 tree new_spec_types;
2442 tree old_type;
2443 tree new_type;
2444 tree t;
2445 tree object_type = NULL_TREE;
2446 tree in_charge = NULL_TREE;
2447 tree vtt = NULL_TREE;
2449 /* See if there's anything we need to do. */
2450 tmpl = DECL_TI_TEMPLATE (decl);
2451 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2452 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2453 if (TREE_PURPOSE (t))
2454 break;
2455 if (!t)
2456 return;
2458 old_type = TREE_TYPE (decl);
2459 spec_types = TYPE_ARG_TYPES (old_type);
2461 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2463 /* Remove the this pointer, but remember the object's type for
2464 CV quals. */
2465 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2466 spec_types = TREE_CHAIN (spec_types);
2467 tmpl_types = TREE_CHAIN (tmpl_types);
2469 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2471 /* DECL may contain more parameters than TMPL due to the extra
2472 in-charge parameter in constructors and destructors. */
2473 in_charge = spec_types;
2474 spec_types = TREE_CHAIN (spec_types);
2476 if (DECL_HAS_VTT_PARM_P (decl))
2478 vtt = spec_types;
2479 spec_types = TREE_CHAIN (spec_types);
2483 /* Compute the merged default arguments. */
2484 new_spec_types =
2485 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2487 /* Compute the new FUNCTION_TYPE. */
2488 if (object_type)
2490 if (vtt)
2491 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2492 TREE_VALUE (vtt),
2493 new_spec_types);
2495 if (in_charge)
2496 /* Put the in-charge parameter back. */
2497 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2498 TREE_VALUE (in_charge),
2499 new_spec_types);
2501 new_type = build_method_type_directly (object_type,
2502 TREE_TYPE (old_type),
2503 new_spec_types);
2505 else
2506 new_type = build_function_type (TREE_TYPE (old_type),
2507 new_spec_types);
2508 new_type = cp_build_type_attribute_variant (new_type,
2509 TYPE_ATTRIBUTES (old_type));
2510 new_type = build_exception_variant (new_type,
2511 TYPE_RAISES_EXCEPTIONS (old_type));
2513 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2514 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2516 TREE_TYPE (decl) = new_type;
2519 /* Return the number of template headers we expect to see for a definition
2520 or specialization of CTYPE or one of its non-template members. */
2523 num_template_headers_for_class (tree ctype)
2525 int num_templates = 0;
2527 while (ctype && CLASS_TYPE_P (ctype))
2529 /* You're supposed to have one `template <...>' for every
2530 template class, but you don't need one for a full
2531 specialization. For example:
2533 template <class T> struct S{};
2534 template <> struct S<int> { void f(); };
2535 void S<int>::f () {}
2537 is correct; there shouldn't be a `template <>' for the
2538 definition of `S<int>::f'. */
2539 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2540 /* If CTYPE does not have template information of any
2541 kind, then it is not a template, nor is it nested
2542 within a template. */
2543 break;
2544 if (explicit_class_specialization_p (ctype))
2545 break;
2546 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2547 ++num_templates;
2549 ctype = TYPE_CONTEXT (ctype);
2552 return num_templates;
2555 /* Do a simple sanity check on the template headers that precede the
2556 variable declaration DECL. */
2558 void
2559 check_template_variable (tree decl)
2561 tree ctx = CP_DECL_CONTEXT (decl);
2562 int wanted = num_template_headers_for_class (ctx);
2563 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2564 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2566 if (cxx_dialect < cxx14)
2567 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2568 "variable templates only available with "
2569 "-std=c++14 or -std=gnu++14");
2571 // Namespace-scope variable templates should have a template header.
2572 ++wanted;
2574 if (template_header_count > wanted)
2576 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2577 "too many template headers for %qD "
2578 "(should be %d)",
2579 decl, wanted);
2580 if (warned && CLASS_TYPE_P (ctx)
2581 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2582 inform (DECL_SOURCE_LOCATION (decl),
2583 "members of an explicitly specialized class are defined "
2584 "without a template header");
2588 /* An explicit specialization whose declarator-id or class-head-name is not
2589 qualified shall be declared in the nearest enclosing namespace of the
2590 template, or, if the namespace is inline (7.3.1), any namespace from its
2591 enclosing namespace set.
2593 If the name declared in the explicit instantiation is an unqualified name,
2594 the explicit instantiation shall appear in the namespace where its template
2595 is declared or, if that namespace is inline (7.3.1), any namespace from its
2596 enclosing namespace set. */
2598 void
2599 check_unqualified_spec_or_inst (tree t, location_t loc)
2601 tree tmpl = most_general_template (t);
2602 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2603 && !is_nested_namespace (current_namespace,
2604 CP_DECL_CONTEXT (tmpl), true))
2606 if (processing_specialization)
2607 permerror (loc, "explicit specialization of %qD outside its "
2608 "namespace must use a nested-name-specifier", tmpl);
2609 else if (processing_explicit_instantiation
2610 && cxx_dialect >= cxx11)
2611 /* This was allowed in C++98, so only pedwarn. */
2612 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2613 "outside its namespace must use a nested-name-"
2614 "specifier", tmpl);
2618 /* Check to see if the function just declared, as indicated in
2619 DECLARATOR, and in DECL, is a specialization of a function
2620 template. We may also discover that the declaration is an explicit
2621 instantiation at this point.
2623 Returns DECL, or an equivalent declaration that should be used
2624 instead if all goes well. Issues an error message if something is
2625 amiss. Returns error_mark_node if the error is not easily
2626 recoverable.
2628 FLAGS is a bitmask consisting of the following flags:
2630 2: The function has a definition.
2631 4: The function is a friend.
2633 The TEMPLATE_COUNT is the number of references to qualifying
2634 template classes that appeared in the name of the function. For
2635 example, in
2637 template <class T> struct S { void f(); };
2638 void S<int>::f();
2640 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2641 classes are not counted in the TEMPLATE_COUNT, so that in
2643 template <class T> struct S {};
2644 template <> struct S<int> { void f(); }
2645 template <> void S<int>::f();
2647 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2648 invalid; there should be no template <>.)
2650 If the function is a specialization, it is marked as such via
2651 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2652 is set up correctly, and it is added to the list of specializations
2653 for that template. */
2655 tree
2656 check_explicit_specialization (tree declarator,
2657 tree decl,
2658 int template_count,
2659 int flags)
2661 int have_def = flags & 2;
2662 int is_friend = flags & 4;
2663 bool is_concept = flags & 8;
2664 int specialization = 0;
2665 int explicit_instantiation = 0;
2666 int member_specialization = 0;
2667 tree ctype = DECL_CLASS_CONTEXT (decl);
2668 tree dname = DECL_NAME (decl);
2669 tmpl_spec_kind tsk;
2671 if (is_friend)
2673 if (!processing_specialization)
2674 tsk = tsk_none;
2675 else
2676 tsk = tsk_excessive_parms;
2678 else
2679 tsk = current_tmpl_spec_kind (template_count);
2681 switch (tsk)
2683 case tsk_none:
2684 if (processing_specialization && !VAR_P (decl))
2686 specialization = 1;
2687 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2689 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2691 if (is_friend)
2692 /* This could be something like:
2694 template <class T> void f(T);
2695 class S { friend void f<>(int); } */
2696 specialization = 1;
2697 else
2699 /* This case handles bogus declarations like template <>
2700 template <class T> void f<int>(); */
2702 error ("template-id %qD in declaration of primary template",
2703 declarator);
2704 return decl;
2707 break;
2709 case tsk_invalid_member_spec:
2710 /* The error has already been reported in
2711 check_specialization_scope. */
2712 return error_mark_node;
2714 case tsk_invalid_expl_inst:
2715 error ("template parameter list used in explicit instantiation");
2717 /* Fall through. */
2719 case tsk_expl_inst:
2720 if (have_def)
2721 error ("definition provided for explicit instantiation");
2723 explicit_instantiation = 1;
2724 break;
2726 case tsk_excessive_parms:
2727 case tsk_insufficient_parms:
2728 if (tsk == tsk_excessive_parms)
2729 error ("too many template parameter lists in declaration of %qD",
2730 decl);
2731 else if (template_header_count)
2732 error("too few template parameter lists in declaration of %qD", decl);
2733 else
2734 error("explicit specialization of %qD must be introduced by "
2735 "%<template <>%>", decl);
2737 /* Fall through. */
2738 case tsk_expl_spec:
2739 if (is_concept)
2740 error ("explicit specialization declared %<concept%>");
2742 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2743 /* In cases like template<> constexpr bool v = true;
2744 We'll give an error in check_template_variable. */
2745 break;
2747 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2748 if (ctype)
2749 member_specialization = 1;
2750 else
2751 specialization = 1;
2752 break;
2754 case tsk_template:
2755 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2757 /* This case handles bogus declarations like template <>
2758 template <class T> void f<int>(); */
2760 if (!uses_template_parms (declarator))
2761 error ("template-id %qD in declaration of primary template",
2762 declarator);
2763 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2765 /* Partial specialization of variable template. */
2766 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2767 specialization = 1;
2768 goto ok;
2770 else if (cxx_dialect < cxx14)
2771 error ("non-type partial specialization %qD "
2772 "is not allowed", declarator);
2773 else
2774 error ("non-class, non-variable partial specialization %qD "
2775 "is not allowed", declarator);
2776 return decl;
2777 ok:;
2780 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2781 /* This is a specialization of a member template, without
2782 specialization the containing class. Something like:
2784 template <class T> struct S {
2785 template <class U> void f (U);
2787 template <> template <class U> void S<int>::f(U) {}
2789 That's a specialization -- but of the entire template. */
2790 specialization = 1;
2791 break;
2793 default:
2794 gcc_unreachable ();
2797 if ((specialization || member_specialization)
2798 /* This doesn't apply to variable templates. */
2799 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2800 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2802 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2803 for (; t; t = TREE_CHAIN (t))
2804 if (TREE_PURPOSE (t))
2806 permerror (input_location,
2807 "default argument specified in explicit specialization");
2808 break;
2812 if (specialization || member_specialization || explicit_instantiation)
2814 tree tmpl = NULL_TREE;
2815 tree targs = NULL_TREE;
2816 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2818 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2819 if (!was_template_id)
2821 tree fns;
2823 gcc_assert (identifier_p (declarator));
2824 if (ctype)
2825 fns = dname;
2826 else
2828 /* If there is no class context, the explicit instantiation
2829 must be at namespace scope. */
2830 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2832 /* Find the namespace binding, using the declaration
2833 context. */
2834 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2835 false, true);
2836 if (fns == error_mark_node)
2837 /* If lookup fails, look for a friend declaration so we can
2838 give a better diagnostic. */
2839 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2840 /*type*/false, /*complain*/true,
2841 /*hidden*/true);
2843 if (fns == error_mark_node || !is_overloaded_fn (fns))
2845 error ("%qD is not a template function", dname);
2846 fns = error_mark_node;
2850 declarator = lookup_template_function (fns, NULL_TREE);
2853 if (declarator == error_mark_node)
2854 return error_mark_node;
2856 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2858 if (!explicit_instantiation)
2859 /* A specialization in class scope. This is invalid,
2860 but the error will already have been flagged by
2861 check_specialization_scope. */
2862 return error_mark_node;
2863 else
2865 /* It's not valid to write an explicit instantiation in
2866 class scope, e.g.:
2868 class C { template void f(); }
2870 This case is caught by the parser. However, on
2871 something like:
2873 template class C { void f(); };
2875 (which is invalid) we can get here. The error will be
2876 issued later. */
2880 return decl;
2882 else if (ctype != NULL_TREE
2883 && (identifier_p (TREE_OPERAND (declarator, 0))))
2885 // We'll match variable templates in start_decl.
2886 if (VAR_P (decl))
2887 return decl;
2889 /* Find the list of functions in ctype that have the same
2890 name as the declared function. */
2891 tree name = TREE_OPERAND (declarator, 0);
2893 if (constructor_name_p (name, ctype))
2895 if (DECL_CONSTRUCTOR_P (decl)
2896 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2897 : !CLASSTYPE_DESTRUCTOR (ctype))
2899 /* From [temp.expl.spec]:
2901 If such an explicit specialization for the member
2902 of a class template names an implicitly-declared
2903 special member function (clause _special_), the
2904 program is ill-formed.
2906 Similar language is found in [temp.explicit]. */
2907 error ("specialization of implicitly-declared special member function");
2908 return error_mark_node;
2911 name = DECL_NAME (decl);
2914 /* For a type-conversion operator, We might be looking for
2915 `operator int' which will be a specialization of
2916 `operator T'. Grab all the conversion operators, and
2917 then select from them. */
2918 tree fns = lookup_fnfields_slot_nolazy (ctype,
2919 IDENTIFIER_CONV_OP_P (name)
2920 ? conv_op_identifier : name);
2922 if (fns == NULL_TREE)
2924 error ("no member function %qD declared in %qT", name, ctype);
2925 return error_mark_node;
2927 else
2928 TREE_OPERAND (declarator, 0) = fns;
2931 /* Figure out what exactly is being specialized at this point.
2932 Note that for an explicit instantiation, even one for a
2933 member function, we cannot tell a priori whether the
2934 instantiation is for a member template, or just a member
2935 function of a template class. Even if a member template is
2936 being instantiated, the member template arguments may be
2937 elided if they can be deduced from the rest of the
2938 declaration. */
2939 tmpl = determine_specialization (declarator, decl,
2940 &targs,
2941 member_specialization,
2942 template_count,
2943 tsk);
2945 if (!tmpl || tmpl == error_mark_node)
2946 /* We couldn't figure out what this declaration was
2947 specializing. */
2948 return error_mark_node;
2949 else
2951 if (TREE_CODE (decl) == FUNCTION_DECL
2952 && DECL_HIDDEN_FRIEND_P (tmpl))
2954 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2955 "friend declaration %qD is not visible to "
2956 "explicit specialization", tmpl))
2957 inform (DECL_SOURCE_LOCATION (tmpl),
2958 "friend declaration here");
2960 else if (!ctype && !is_friend
2961 && CP_DECL_CONTEXT (decl) == current_namespace)
2962 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2964 tree gen_tmpl = most_general_template (tmpl);
2966 if (explicit_instantiation)
2968 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2969 is done by do_decl_instantiation later. */
2971 int arg_depth = TMPL_ARGS_DEPTH (targs);
2972 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2974 if (arg_depth > parm_depth)
2976 /* If TMPL is not the most general template (for
2977 example, if TMPL is a friend template that is
2978 injected into namespace scope), then there will
2979 be too many levels of TARGS. Remove some of them
2980 here. */
2981 int i;
2982 tree new_targs;
2984 new_targs = make_tree_vec (parm_depth);
2985 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2986 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2987 = TREE_VEC_ELT (targs, i);
2988 targs = new_targs;
2991 return instantiate_template (tmpl, targs, tf_error);
2994 /* If we thought that the DECL was a member function, but it
2995 turns out to be specializing a static member function,
2996 make DECL a static member function as well. */
2997 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2998 && DECL_STATIC_FUNCTION_P (tmpl)
2999 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3000 revert_static_member_fn (decl);
3002 /* If this is a specialization of a member template of a
3003 template class, we want to return the TEMPLATE_DECL, not
3004 the specialization of it. */
3005 if (tsk == tsk_template && !was_template_id)
3007 tree result = DECL_TEMPLATE_RESULT (tmpl);
3008 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3009 DECL_INITIAL (result) = NULL_TREE;
3010 if (have_def)
3012 tree parm;
3013 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3014 DECL_SOURCE_LOCATION (result)
3015 = DECL_SOURCE_LOCATION (decl);
3016 /* We want to use the argument list specified in the
3017 definition, not in the original declaration. */
3018 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3019 for (parm = DECL_ARGUMENTS (result); parm;
3020 parm = DECL_CHAIN (parm))
3021 DECL_CONTEXT (parm) = result;
3023 return register_specialization (tmpl, gen_tmpl, targs,
3024 is_friend, 0);
3027 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3028 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3030 if (was_template_id)
3031 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3033 /* Inherit default function arguments from the template
3034 DECL is specializing. */
3035 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3036 copy_default_args_to_explicit_spec (decl);
3038 /* This specialization has the same protection as the
3039 template it specializes. */
3040 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3041 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3043 /* 7.1.1-1 [dcl.stc]
3045 A storage-class-specifier shall not be specified in an
3046 explicit specialization...
3048 The parser rejects these, so unless action is taken here,
3049 explicit function specializations will always appear with
3050 global linkage.
3052 The action recommended by the C++ CWG in response to C++
3053 defect report 605 is to make the storage class and linkage
3054 of the explicit specialization match the templated function:
3056 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3058 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3060 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3061 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3063 /* A concept cannot be specialized. */
3064 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3066 error ("explicit specialization of function concept %qD",
3067 gen_tmpl);
3068 return error_mark_node;
3071 /* This specialization has the same linkage and visibility as
3072 the function template it specializes. */
3073 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3074 if (! TREE_PUBLIC (decl))
3076 DECL_INTERFACE_KNOWN (decl) = 1;
3077 DECL_NOT_REALLY_EXTERN (decl) = 1;
3079 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3080 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3082 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3083 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3087 /* If DECL is a friend declaration, declared using an
3088 unqualified name, the namespace associated with DECL may
3089 have been set incorrectly. For example, in:
3091 template <typename T> void f(T);
3092 namespace N {
3093 struct S { friend void f<int>(int); }
3096 we will have set the DECL_CONTEXT for the friend
3097 declaration to N, rather than to the global namespace. */
3098 if (DECL_NAMESPACE_SCOPE_P (decl))
3099 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3101 if (is_friend && !have_def)
3102 /* This is not really a declaration of a specialization.
3103 It's just the name of an instantiation. But, it's not
3104 a request for an instantiation, either. */
3105 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3106 else if (TREE_CODE (decl) == FUNCTION_DECL)
3107 /* A specialization is not necessarily COMDAT. */
3108 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3109 && DECL_DECLARED_INLINE_P (decl));
3110 else if (VAR_P (decl))
3111 DECL_COMDAT (decl) = false;
3113 /* If this is a full specialization, register it so that we can find
3114 it again. Partial specializations will be registered in
3115 process_partial_specialization. */
3116 if (!processing_template_decl)
3117 decl = register_specialization (decl, gen_tmpl, targs,
3118 is_friend, 0);
3120 /* A 'structor should already have clones. */
3121 gcc_assert (decl == error_mark_node
3122 || variable_template_p (tmpl)
3123 || !(DECL_CONSTRUCTOR_P (decl)
3124 || DECL_DESTRUCTOR_P (decl))
3125 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3129 return decl;
3132 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3133 parameters. These are represented in the same format used for
3134 DECL_TEMPLATE_PARMS. */
3137 comp_template_parms (const_tree parms1, const_tree parms2)
3139 const_tree p1;
3140 const_tree p2;
3142 if (parms1 == parms2)
3143 return 1;
3145 for (p1 = parms1, p2 = parms2;
3146 p1 != NULL_TREE && p2 != NULL_TREE;
3147 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3149 tree t1 = TREE_VALUE (p1);
3150 tree t2 = TREE_VALUE (p2);
3151 int i;
3153 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3154 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3156 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3157 return 0;
3159 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3161 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3162 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3164 /* If either of the template parameters are invalid, assume
3165 they match for the sake of error recovery. */
3166 if (error_operand_p (parm1) || error_operand_p (parm2))
3167 return 1;
3169 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3170 return 0;
3172 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3173 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3174 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3175 continue;
3176 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3177 return 0;
3181 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3182 /* One set of parameters has more parameters lists than the
3183 other. */
3184 return 0;
3186 return 1;
3189 /* Determine whether PARM is a parameter pack. */
3191 bool
3192 template_parameter_pack_p (const_tree parm)
3194 /* Determine if we have a non-type template parameter pack. */
3195 if (TREE_CODE (parm) == PARM_DECL)
3196 return (DECL_TEMPLATE_PARM_P (parm)
3197 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3198 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3199 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3201 /* If this is a list of template parameters, we could get a
3202 TYPE_DECL or a TEMPLATE_DECL. */
3203 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3204 parm = TREE_TYPE (parm);
3206 /* Otherwise it must be a type template parameter. */
3207 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3208 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3209 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3212 /* Determine if T is a function parameter pack. */
3214 bool
3215 function_parameter_pack_p (const_tree t)
3217 if (t && TREE_CODE (t) == PARM_DECL)
3218 return DECL_PACK_P (t);
3219 return false;
3222 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3223 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3225 tree
3226 get_function_template_decl (const_tree primary_func_tmpl_inst)
3228 if (! primary_func_tmpl_inst
3229 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3230 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3231 return NULL;
3233 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3236 /* Return true iff the function parameter PARAM_DECL was expanded
3237 from the function parameter pack PACK. */
3239 bool
3240 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3242 if (DECL_ARTIFICIAL (param_decl)
3243 || !function_parameter_pack_p (pack))
3244 return false;
3246 /* The parameter pack and its pack arguments have the same
3247 DECL_PARM_INDEX. */
3248 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3251 /* Determine whether ARGS describes a variadic template args list,
3252 i.e., one that is terminated by a template argument pack. */
3254 static bool
3255 template_args_variadic_p (tree args)
3257 int nargs;
3258 tree last_parm;
3260 if (args == NULL_TREE)
3261 return false;
3263 args = INNERMOST_TEMPLATE_ARGS (args);
3264 nargs = TREE_VEC_LENGTH (args);
3266 if (nargs == 0)
3267 return false;
3269 last_parm = TREE_VEC_ELT (args, nargs - 1);
3271 return ARGUMENT_PACK_P (last_parm);
3274 /* Generate a new name for the parameter pack name NAME (an
3275 IDENTIFIER_NODE) that incorporates its */
3277 static tree
3278 make_ith_pack_parameter_name (tree name, int i)
3280 /* Munge the name to include the parameter index. */
3281 #define NUMBUF_LEN 128
3282 char numbuf[NUMBUF_LEN];
3283 char* newname;
3284 int newname_len;
3286 if (name == NULL_TREE)
3287 return name;
3288 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3289 newname_len = IDENTIFIER_LENGTH (name)
3290 + strlen (numbuf) + 2;
3291 newname = (char*)alloca (newname_len);
3292 snprintf (newname, newname_len,
3293 "%s#%i", IDENTIFIER_POINTER (name), i);
3294 return get_identifier (newname);
3297 /* Return true if T is a primary function, class or alias template
3298 instantiation. */
3300 bool
3301 primary_template_instantiation_p (const_tree t)
3303 if (!t)
3304 return false;
3306 if (TREE_CODE (t) == FUNCTION_DECL)
3307 return DECL_LANG_SPECIFIC (t)
3308 && DECL_TEMPLATE_INSTANTIATION (t)
3309 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3310 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3311 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3312 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3313 else if (alias_template_specialization_p (t))
3314 return true;
3315 return false;
3318 /* Return true if PARM is a template template parameter. */
3320 bool
3321 template_template_parameter_p (const_tree parm)
3323 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3326 /* Return true iff PARM is a DECL representing a type template
3327 parameter. */
3329 bool
3330 template_type_parameter_p (const_tree parm)
3332 return (parm
3333 && (TREE_CODE (parm) == TYPE_DECL
3334 || TREE_CODE (parm) == TEMPLATE_DECL)
3335 && DECL_TEMPLATE_PARM_P (parm));
3338 /* Return the template parameters of T if T is a
3339 primary template instantiation, NULL otherwise. */
3341 tree
3342 get_primary_template_innermost_parameters (const_tree t)
3344 tree parms = NULL, template_info = NULL;
3346 if ((template_info = get_template_info (t))
3347 && primary_template_instantiation_p (t))
3348 parms = INNERMOST_TEMPLATE_PARMS
3349 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3351 return parms;
3354 /* Return the template parameters of the LEVELth level from the full list
3355 of template parameters PARMS. */
3357 tree
3358 get_template_parms_at_level (tree parms, int level)
3360 tree p;
3361 if (!parms
3362 || TREE_CODE (parms) != TREE_LIST
3363 || level > TMPL_PARMS_DEPTH (parms))
3364 return NULL_TREE;
3366 for (p = parms; p; p = TREE_CHAIN (p))
3367 if (TMPL_PARMS_DEPTH (p) == level)
3368 return p;
3370 return NULL_TREE;
3373 /* Returns the template arguments of T if T is a template instantiation,
3374 NULL otherwise. */
3376 tree
3377 get_template_innermost_arguments (const_tree t)
3379 tree args = NULL, template_info = NULL;
3381 if ((template_info = get_template_info (t))
3382 && TI_ARGS (template_info))
3383 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3385 return args;
3388 /* Return the argument pack elements of T if T is a template argument pack,
3389 NULL otherwise. */
3391 tree
3392 get_template_argument_pack_elems (const_tree t)
3394 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3395 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3396 return NULL;
3398 return ARGUMENT_PACK_ARGS (t);
3401 /* True iff FN is a function representing a built-in variadic parameter
3402 pack. */
3404 bool
3405 builtin_pack_fn_p (tree fn)
3407 if (!fn
3408 || TREE_CODE (fn) != FUNCTION_DECL
3409 || !DECL_IS_BUILTIN (fn))
3410 return false;
3412 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3413 return true;
3415 return false;
3418 /* True iff CALL is a call to a function representing a built-in variadic
3419 parameter pack. */
3421 static bool
3422 builtin_pack_call_p (tree call)
3424 if (TREE_CODE (call) != CALL_EXPR)
3425 return false;
3426 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3429 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3431 static tree
3432 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3433 tree in_decl)
3435 tree ohi = CALL_EXPR_ARG (call, 0);
3436 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3437 false/*fn*/, true/*int_cst*/);
3439 if (value_dependent_expression_p (hi))
3441 if (hi != ohi)
3443 call = copy_node (call);
3444 CALL_EXPR_ARG (call, 0) = hi;
3446 tree ex = make_pack_expansion (call);
3447 tree vec = make_tree_vec (1);
3448 TREE_VEC_ELT (vec, 0) = ex;
3449 return vec;
3451 else
3453 hi = cxx_constant_value (hi);
3454 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3456 /* Calculate the largest value of len that won't make the size of the vec
3457 overflow an int. The compiler will exceed resource limits long before
3458 this, but it seems a decent place to diagnose. */
3459 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3461 if (len < 0 || len > max)
3463 if ((complain & tf_error)
3464 && hi != error_mark_node)
3465 error ("argument to __integer_pack must be between 0 and %d", max);
3466 return error_mark_node;
3469 tree vec = make_tree_vec (len);
3471 for (int i = 0; i < len; ++i)
3472 TREE_VEC_ELT (vec, i) = size_int (i);
3474 return vec;
3478 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3479 CALL. */
3481 static tree
3482 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3483 tree in_decl)
3485 if (!builtin_pack_call_p (call))
3486 return NULL_TREE;
3488 tree fn = CALL_EXPR_FN (call);
3490 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3491 return expand_integer_pack (call, args, complain, in_decl);
3493 return NULL_TREE;
3496 /* Structure used to track the progress of find_parameter_packs_r. */
3497 struct find_parameter_pack_data
3499 /* TREE_LIST that will contain all of the parameter packs found by
3500 the traversal. */
3501 tree* parameter_packs;
3503 /* Set of AST nodes that have been visited by the traversal. */
3504 hash_set<tree> *visited;
3506 /* True iff we're making a type pack expansion. */
3507 bool type_pack_expansion_p;
3510 /* Identifies all of the argument packs that occur in a template
3511 argument and appends them to the TREE_LIST inside DATA, which is a
3512 find_parameter_pack_data structure. This is a subroutine of
3513 make_pack_expansion and uses_parameter_packs. */
3514 static tree
3515 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3517 tree t = *tp;
3518 struct find_parameter_pack_data* ppd =
3519 (struct find_parameter_pack_data*)data;
3520 bool parameter_pack_p = false;
3522 /* Handle type aliases/typedefs. */
3523 if (TYPE_ALIAS_P (t))
3525 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3526 cp_walk_tree (&TI_ARGS (tinfo),
3527 &find_parameter_packs_r,
3528 ppd, ppd->visited);
3529 *walk_subtrees = 0;
3530 return NULL_TREE;
3533 /* Identify whether this is a parameter pack or not. */
3534 switch (TREE_CODE (t))
3536 case TEMPLATE_PARM_INDEX:
3537 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3538 parameter_pack_p = true;
3539 break;
3541 case TEMPLATE_TYPE_PARM:
3542 t = TYPE_MAIN_VARIANT (t);
3543 /* FALLTHRU */
3544 case TEMPLATE_TEMPLATE_PARM:
3545 /* If the placeholder appears in the decl-specifier-seq of a function
3546 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3547 is a pack expansion, the invented template parameter is a template
3548 parameter pack. */
3549 if (ppd->type_pack_expansion_p && is_auto (t))
3550 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3551 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3552 parameter_pack_p = true;
3553 break;
3555 case FIELD_DECL:
3556 case PARM_DECL:
3557 if (DECL_PACK_P (t))
3559 /* We don't want to walk into the type of a PARM_DECL,
3560 because we don't want to see the type parameter pack. */
3561 *walk_subtrees = 0;
3562 parameter_pack_p = true;
3564 break;
3566 /* Look through a lambda capture proxy to the field pack. */
3567 case VAR_DECL:
3568 if (DECL_HAS_VALUE_EXPR_P (t))
3570 tree v = DECL_VALUE_EXPR (t);
3571 cp_walk_tree (&v,
3572 &find_parameter_packs_r,
3573 ppd, ppd->visited);
3574 *walk_subtrees = 0;
3576 else if (variable_template_specialization_p (t))
3578 cp_walk_tree (&DECL_TI_ARGS (t),
3579 find_parameter_packs_r,
3580 ppd, ppd->visited);
3581 *walk_subtrees = 0;
3583 break;
3585 case CALL_EXPR:
3586 if (builtin_pack_call_p (t))
3587 parameter_pack_p = true;
3588 break;
3590 case BASES:
3591 parameter_pack_p = true;
3592 break;
3593 default:
3594 /* Not a parameter pack. */
3595 break;
3598 if (parameter_pack_p)
3600 /* Add this parameter pack to the list. */
3601 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3604 if (TYPE_P (t))
3605 cp_walk_tree (&TYPE_CONTEXT (t),
3606 &find_parameter_packs_r, ppd, ppd->visited);
3608 /* This switch statement will return immediately if we don't find a
3609 parameter pack. */
3610 switch (TREE_CODE (t))
3612 case TEMPLATE_PARM_INDEX:
3613 return NULL_TREE;
3615 case BOUND_TEMPLATE_TEMPLATE_PARM:
3616 /* Check the template itself. */
3617 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3618 &find_parameter_packs_r, ppd, ppd->visited);
3619 /* Check the template arguments. */
3620 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3621 ppd->visited);
3622 *walk_subtrees = 0;
3623 return NULL_TREE;
3625 case TEMPLATE_TYPE_PARM:
3626 case TEMPLATE_TEMPLATE_PARM:
3627 return NULL_TREE;
3629 case PARM_DECL:
3630 return NULL_TREE;
3632 case DECL_EXPR:
3633 /* Ignore the declaration of a capture proxy for a parameter pack. */
3634 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3635 *walk_subtrees = 0;
3636 return NULL_TREE;
3638 case RECORD_TYPE:
3639 if (TYPE_PTRMEMFUNC_P (t))
3640 return NULL_TREE;
3641 /* Fall through. */
3643 case UNION_TYPE:
3644 case ENUMERAL_TYPE:
3645 if (TYPE_TEMPLATE_INFO (t))
3646 cp_walk_tree (&TYPE_TI_ARGS (t),
3647 &find_parameter_packs_r, ppd, ppd->visited);
3649 *walk_subtrees = 0;
3650 return NULL_TREE;
3652 case TEMPLATE_DECL:
3653 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3654 return NULL_TREE;
3655 gcc_fallthrough();
3657 case CONSTRUCTOR:
3658 cp_walk_tree (&TREE_TYPE (t),
3659 &find_parameter_packs_r, ppd, ppd->visited);
3660 return NULL_TREE;
3662 case TYPENAME_TYPE:
3663 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3664 ppd, ppd->visited);
3665 *walk_subtrees = 0;
3666 return NULL_TREE;
3668 case TYPE_PACK_EXPANSION:
3669 case EXPR_PACK_EXPANSION:
3670 *walk_subtrees = 0;
3671 return NULL_TREE;
3673 case INTEGER_TYPE:
3674 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3675 ppd, ppd->visited);
3676 *walk_subtrees = 0;
3677 return NULL_TREE;
3679 case IDENTIFIER_NODE:
3680 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3681 ppd->visited);
3682 *walk_subtrees = 0;
3683 return NULL_TREE;
3685 case LAMBDA_EXPR:
3687 tree fn = lambda_function (t);
3688 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3689 ppd->visited);
3690 *walk_subtrees = 0;
3691 return NULL_TREE;
3694 case DECLTYPE_TYPE:
3696 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3697 type_pack_expansion_p to false so that any placeholders
3698 within the expression don't get marked as parameter packs. */
3699 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3700 ppd->type_pack_expansion_p = false;
3701 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3702 ppd, ppd->visited);
3703 ppd->type_pack_expansion_p = type_pack_expansion_p;
3704 *walk_subtrees = 0;
3705 return NULL_TREE;
3708 default:
3709 return NULL_TREE;
3712 return NULL_TREE;
3715 /* Determines if the expression or type T uses any parameter packs. */
3716 bool
3717 uses_parameter_packs (tree t)
3719 tree parameter_packs = NULL_TREE;
3720 struct find_parameter_pack_data ppd;
3721 ppd.parameter_packs = &parameter_packs;
3722 ppd.visited = new hash_set<tree>;
3723 ppd.type_pack_expansion_p = false;
3724 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3725 delete ppd.visited;
3726 return parameter_packs != NULL_TREE;
3729 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3730 representation a base-class initializer into a parameter pack
3731 expansion. If all goes well, the resulting node will be an
3732 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3733 respectively. */
3734 tree
3735 make_pack_expansion (tree arg)
3737 tree result;
3738 tree parameter_packs = NULL_TREE;
3739 bool for_types = false;
3740 struct find_parameter_pack_data ppd;
3742 if (!arg || arg == error_mark_node)
3743 return arg;
3745 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3747 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3748 class initializer. In this case, the TREE_PURPOSE will be a
3749 _TYPE node (representing the base class expansion we're
3750 initializing) and the TREE_VALUE will be a TREE_LIST
3751 containing the initialization arguments.
3753 The resulting expansion looks somewhat different from most
3754 expansions. Rather than returning just one _EXPANSION, we
3755 return a TREE_LIST whose TREE_PURPOSE is a
3756 TYPE_PACK_EXPANSION containing the bases that will be
3757 initialized. The TREE_VALUE will be identical to the
3758 original TREE_VALUE, which is a list of arguments that will
3759 be passed to each base. We do not introduce any new pack
3760 expansion nodes into the TREE_VALUE (although it is possible
3761 that some already exist), because the TREE_PURPOSE and
3762 TREE_VALUE all need to be expanded together with the same
3763 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3764 resulting TREE_PURPOSE will mention the parameter packs in
3765 both the bases and the arguments to the bases. */
3766 tree purpose;
3767 tree value;
3768 tree parameter_packs = NULL_TREE;
3770 /* Determine which parameter packs will be used by the base
3771 class expansion. */
3772 ppd.visited = new hash_set<tree>;
3773 ppd.parameter_packs = &parameter_packs;
3774 ppd.type_pack_expansion_p = true;
3775 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3776 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3777 &ppd, ppd.visited);
3779 if (parameter_packs == NULL_TREE)
3781 error ("base initializer expansion %qT contains no parameter packs", arg);
3782 delete ppd.visited;
3783 return error_mark_node;
3786 if (TREE_VALUE (arg) != void_type_node)
3788 /* Collect the sets of parameter packs used in each of the
3789 initialization arguments. */
3790 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3792 /* Determine which parameter packs will be expanded in this
3793 argument. */
3794 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3795 &ppd, ppd.visited);
3799 delete ppd.visited;
3801 /* Create the pack expansion type for the base type. */
3802 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3803 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3804 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3805 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3807 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3808 they will rarely be compared to anything. */
3809 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3811 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3814 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3815 for_types = true;
3817 /* Build the PACK_EXPANSION_* node. */
3818 result = for_types
3819 ? cxx_make_type (TYPE_PACK_EXPANSION)
3820 : make_node (EXPR_PACK_EXPANSION);
3821 SET_PACK_EXPANSION_PATTERN (result, arg);
3822 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3824 /* Propagate type and const-expression information. */
3825 TREE_TYPE (result) = TREE_TYPE (arg);
3826 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3827 /* Mark this read now, since the expansion might be length 0. */
3828 mark_exp_read (arg);
3830 else
3831 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3832 they will rarely be compared to anything. */
3833 SET_TYPE_STRUCTURAL_EQUALITY (result);
3835 /* Determine which parameter packs will be expanded. */
3836 ppd.parameter_packs = &parameter_packs;
3837 ppd.visited = new hash_set<tree>;
3838 ppd.type_pack_expansion_p = TYPE_P (arg);
3839 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3840 delete ppd.visited;
3842 /* Make sure we found some parameter packs. */
3843 if (parameter_packs == NULL_TREE)
3845 if (TYPE_P (arg))
3846 error ("expansion pattern %qT contains no argument packs", arg);
3847 else
3848 error ("expansion pattern %qE contains no argument packs", arg);
3849 return error_mark_node;
3851 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3853 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3855 return result;
3858 /* Checks T for any "bare" parameter packs, which have not yet been
3859 expanded, and issues an error if any are found. This operation can
3860 only be done on full expressions or types (e.g., an expression
3861 statement, "if" condition, etc.), because we could have expressions like:
3863 foo(f(g(h(args)))...)
3865 where "args" is a parameter pack. check_for_bare_parameter_packs
3866 should not be called for the subexpressions args, h(args),
3867 g(h(args)), or f(g(h(args))), because we would produce erroneous
3868 error messages.
3870 Returns TRUE and emits an error if there were bare parameter packs,
3871 returns FALSE otherwise. */
3872 bool
3873 check_for_bare_parameter_packs (tree t)
3875 tree parameter_packs = NULL_TREE;
3876 struct find_parameter_pack_data ppd;
3878 if (!processing_template_decl || !t || t == error_mark_node)
3879 return false;
3881 /* A lambda might use a parameter pack from the containing context. */
3882 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3883 return false;
3885 if (TREE_CODE (t) == TYPE_DECL)
3886 t = TREE_TYPE (t);
3888 ppd.parameter_packs = &parameter_packs;
3889 ppd.visited = new hash_set<tree>;
3890 ppd.type_pack_expansion_p = false;
3891 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3892 delete ppd.visited;
3894 if (parameter_packs)
3896 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3897 error_at (loc, "parameter packs not expanded with %<...%>:");
3898 while (parameter_packs)
3900 tree pack = TREE_VALUE (parameter_packs);
3901 tree name = NULL_TREE;
3903 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3904 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3905 name = TYPE_NAME (pack);
3906 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3907 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3908 else if (TREE_CODE (pack) == CALL_EXPR)
3909 name = DECL_NAME (CALL_EXPR_FN (pack));
3910 else
3911 name = DECL_NAME (pack);
3913 if (name)
3914 inform (loc, " %qD", name);
3915 else
3916 inform (loc, " <anonymous>");
3918 parameter_packs = TREE_CHAIN (parameter_packs);
3921 return true;
3924 return false;
3927 /* Expand any parameter packs that occur in the template arguments in
3928 ARGS. */
3929 tree
3930 expand_template_argument_pack (tree args)
3932 if (args == error_mark_node)
3933 return error_mark_node;
3935 tree result_args = NULL_TREE;
3936 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3937 int num_result_args = -1;
3938 int non_default_args_count = -1;
3940 /* First, determine if we need to expand anything, and the number of
3941 slots we'll need. */
3942 for (in_arg = 0; in_arg < nargs; ++in_arg)
3944 tree arg = TREE_VEC_ELT (args, in_arg);
3945 if (arg == NULL_TREE)
3946 return args;
3947 if (ARGUMENT_PACK_P (arg))
3949 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3950 if (num_result_args < 0)
3951 num_result_args = in_arg + num_packed;
3952 else
3953 num_result_args += num_packed;
3955 else
3957 if (num_result_args >= 0)
3958 num_result_args++;
3962 /* If no expansion is necessary, we're done. */
3963 if (num_result_args < 0)
3964 return args;
3966 /* Expand arguments. */
3967 result_args = make_tree_vec (num_result_args);
3968 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3969 non_default_args_count =
3970 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3971 for (in_arg = 0; in_arg < nargs; ++in_arg)
3973 tree arg = TREE_VEC_ELT (args, in_arg);
3974 if (ARGUMENT_PACK_P (arg))
3976 tree packed = ARGUMENT_PACK_ARGS (arg);
3977 int i, num_packed = TREE_VEC_LENGTH (packed);
3978 for (i = 0; i < num_packed; ++i, ++out_arg)
3979 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3980 if (non_default_args_count > 0)
3981 non_default_args_count += num_packed - 1;
3983 else
3985 TREE_VEC_ELT (result_args, out_arg) = arg;
3986 ++out_arg;
3989 if (non_default_args_count >= 0)
3990 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3991 return result_args;
3994 /* Checks if DECL shadows a template parameter.
3996 [temp.local]: A template-parameter shall not be redeclared within its
3997 scope (including nested scopes).
3999 Emits an error and returns TRUE if the DECL shadows a parameter,
4000 returns FALSE otherwise. */
4002 bool
4003 check_template_shadow (tree decl)
4005 tree olddecl;
4007 /* If we're not in a template, we can't possibly shadow a template
4008 parameter. */
4009 if (!current_template_parms)
4010 return true;
4012 /* Figure out what we're shadowing. */
4013 decl = OVL_FIRST (decl);
4014 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4016 /* If there's no previous binding for this name, we're not shadowing
4017 anything, let alone a template parameter. */
4018 if (!olddecl)
4019 return true;
4021 /* If we're not shadowing a template parameter, we're done. Note
4022 that OLDDECL might be an OVERLOAD (or perhaps even an
4023 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4024 node. */
4025 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4026 return true;
4028 /* We check for decl != olddecl to avoid bogus errors for using a
4029 name inside a class. We check TPFI to avoid duplicate errors for
4030 inline member templates. */
4031 if (decl == olddecl
4032 || (DECL_TEMPLATE_PARM_P (decl)
4033 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4034 return true;
4036 /* Don't complain about the injected class name, as we've already
4037 complained about the class itself. */
4038 if (DECL_SELF_REFERENCE_P (decl))
4039 return false;
4041 if (DECL_TEMPLATE_PARM_P (decl))
4042 error ("declaration of template parameter %q+D shadows "
4043 "template parameter", decl);
4044 else
4045 error ("declaration of %q+#D shadows template parameter", decl);
4046 inform (DECL_SOURCE_LOCATION (olddecl),
4047 "template parameter %qD declared here", olddecl);
4048 return false;
4051 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4052 ORIG_LEVEL, DECL, and TYPE. */
4054 static tree
4055 build_template_parm_index (int index,
4056 int level,
4057 int orig_level,
4058 tree decl,
4059 tree type)
4061 tree t = make_node (TEMPLATE_PARM_INDEX);
4062 TEMPLATE_PARM_IDX (t) = index;
4063 TEMPLATE_PARM_LEVEL (t) = level;
4064 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4065 TEMPLATE_PARM_DECL (t) = decl;
4066 TREE_TYPE (t) = type;
4067 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4068 TREE_READONLY (t) = TREE_READONLY (decl);
4070 return t;
4073 /* Find the canonical type parameter for the given template type
4074 parameter. Returns the canonical type parameter, which may be TYPE
4075 if no such parameter existed. */
4077 static tree
4078 canonical_type_parameter (tree type)
4080 tree list;
4081 int idx = TEMPLATE_TYPE_IDX (type);
4082 if (!canonical_template_parms)
4083 vec_alloc (canonical_template_parms, idx + 1);
4085 if (canonical_template_parms->length () <= (unsigned) idx)
4086 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4088 list = (*canonical_template_parms)[idx];
4089 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4090 list = TREE_CHAIN (list);
4092 if (list)
4093 return TREE_VALUE (list);
4094 else
4096 (*canonical_template_parms)[idx]
4097 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4098 return type;
4102 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4103 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4104 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4105 new one is created. */
4107 static tree
4108 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4109 tsubst_flags_t complain)
4111 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4112 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4113 != TEMPLATE_PARM_LEVEL (index) - levels)
4114 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4116 tree orig_decl = TEMPLATE_PARM_DECL (index);
4117 tree decl, t;
4119 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4120 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4121 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4122 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4123 DECL_ARTIFICIAL (decl) = 1;
4124 SET_DECL_TEMPLATE_PARM_P (decl);
4126 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4127 TEMPLATE_PARM_LEVEL (index) - levels,
4128 TEMPLATE_PARM_ORIG_LEVEL (index),
4129 decl, type);
4130 TEMPLATE_PARM_DESCENDANTS (index) = t;
4131 TEMPLATE_PARM_PARAMETER_PACK (t)
4132 = TEMPLATE_PARM_PARAMETER_PACK (index);
4134 /* Template template parameters need this. */
4135 if (TREE_CODE (decl) == TEMPLATE_DECL)
4137 DECL_TEMPLATE_RESULT (decl)
4138 = build_decl (DECL_SOURCE_LOCATION (decl),
4139 TYPE_DECL, DECL_NAME (decl), type);
4140 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4141 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4142 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4146 return TEMPLATE_PARM_DESCENDANTS (index);
4149 /* Process information from new template parameter PARM and append it
4150 to the LIST being built. This new parameter is a non-type
4151 parameter iff IS_NON_TYPE is true. This new parameter is a
4152 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4153 is in PARM_LOC. */
4155 tree
4156 process_template_parm (tree list, location_t parm_loc, tree parm,
4157 bool is_non_type, bool is_parameter_pack)
4159 tree decl = 0;
4160 int idx = 0;
4162 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4163 tree defval = TREE_PURPOSE (parm);
4164 tree constr = TREE_TYPE (parm);
4166 if (list)
4168 tree p = tree_last (list);
4170 if (p && TREE_VALUE (p) != error_mark_node)
4172 p = TREE_VALUE (p);
4173 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4174 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4175 else
4176 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4179 ++idx;
4182 if (is_non_type)
4184 parm = TREE_VALUE (parm);
4186 SET_DECL_TEMPLATE_PARM_P (parm);
4188 if (TREE_TYPE (parm) != error_mark_node)
4190 /* [temp.param]
4192 The top-level cv-qualifiers on the template-parameter are
4193 ignored when determining its type. */
4194 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4195 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4196 TREE_TYPE (parm) = error_mark_node;
4197 else if (uses_parameter_packs (TREE_TYPE (parm))
4198 && !is_parameter_pack
4199 /* If we're in a nested template parameter list, the template
4200 template parameter could be a parameter pack. */
4201 && processing_template_parmlist == 1)
4203 /* This template parameter is not a parameter pack, but it
4204 should be. Complain about "bare" parameter packs. */
4205 check_for_bare_parameter_packs (TREE_TYPE (parm));
4207 /* Recover by calling this a parameter pack. */
4208 is_parameter_pack = true;
4212 /* A template parameter is not modifiable. */
4213 TREE_CONSTANT (parm) = 1;
4214 TREE_READONLY (parm) = 1;
4215 decl = build_decl (parm_loc,
4216 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4217 TREE_CONSTANT (decl) = 1;
4218 TREE_READONLY (decl) = 1;
4219 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4220 = build_template_parm_index (idx, processing_template_decl,
4221 processing_template_decl,
4222 decl, TREE_TYPE (parm));
4224 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4225 = is_parameter_pack;
4227 else
4229 tree t;
4230 parm = TREE_VALUE (TREE_VALUE (parm));
4232 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4234 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4235 /* This is for distinguishing between real templates and template
4236 template parameters */
4237 TREE_TYPE (parm) = t;
4238 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4239 decl = parm;
4241 else
4243 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4244 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4245 decl = build_decl (parm_loc,
4246 TYPE_DECL, parm, t);
4249 TYPE_NAME (t) = decl;
4250 TYPE_STUB_DECL (t) = decl;
4251 parm = decl;
4252 TEMPLATE_TYPE_PARM_INDEX (t)
4253 = build_template_parm_index (idx, processing_template_decl,
4254 processing_template_decl,
4255 decl, TREE_TYPE (parm));
4256 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4257 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4259 DECL_ARTIFICIAL (decl) = 1;
4260 SET_DECL_TEMPLATE_PARM_P (decl);
4262 /* Build requirements for the type/template parameter.
4263 This must be done after SET_DECL_TEMPLATE_PARM_P or
4264 process_template_parm could fail. */
4265 tree reqs = finish_shorthand_constraint (parm, constr);
4267 pushdecl (decl);
4269 /* Build the parameter node linking the parameter declaration,
4270 its default argument (if any), and its constraints (if any). */
4271 parm = build_tree_list (defval, parm);
4272 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4274 return chainon (list, parm);
4277 /* The end of a template parameter list has been reached. Process the
4278 tree list into a parameter vector, converting each parameter into a more
4279 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4280 as PARM_DECLs. */
4282 tree
4283 end_template_parm_list (tree parms)
4285 int nparms;
4286 tree parm, next;
4287 tree saved_parmlist = make_tree_vec (list_length (parms));
4289 /* Pop the dummy parameter level and add the real one. */
4290 current_template_parms = TREE_CHAIN (current_template_parms);
4292 current_template_parms
4293 = tree_cons (size_int (processing_template_decl),
4294 saved_parmlist, current_template_parms);
4296 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4298 next = TREE_CHAIN (parm);
4299 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4300 TREE_CHAIN (parm) = NULL_TREE;
4303 --processing_template_parmlist;
4305 return saved_parmlist;
4308 // Explicitly indicate the end of the template parameter list. We assume
4309 // that the current template parameters have been constructed and/or
4310 // managed explicitly, as when creating new template template parameters
4311 // from a shorthand constraint.
4312 void
4313 end_template_parm_list ()
4315 --processing_template_parmlist;
4318 /* end_template_decl is called after a template declaration is seen. */
4320 void
4321 end_template_decl (void)
4323 reset_specialization ();
4325 if (! processing_template_decl)
4326 return;
4328 /* This matches the pushlevel in begin_template_parm_list. */
4329 finish_scope ();
4331 --processing_template_decl;
4332 current_template_parms = TREE_CHAIN (current_template_parms);
4335 /* Takes a TREE_LIST representing a template parameter and convert it
4336 into an argument suitable to be passed to the type substitution
4337 functions. Note that If the TREE_LIST contains an error_mark
4338 node, the returned argument is error_mark_node. */
4340 tree
4341 template_parm_to_arg (tree t)
4344 if (t == NULL_TREE
4345 || TREE_CODE (t) != TREE_LIST)
4346 return t;
4348 if (error_operand_p (TREE_VALUE (t)))
4349 return error_mark_node;
4351 t = TREE_VALUE (t);
4353 if (TREE_CODE (t) == TYPE_DECL
4354 || TREE_CODE (t) == TEMPLATE_DECL)
4356 t = TREE_TYPE (t);
4358 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4360 /* Turn this argument into a TYPE_ARGUMENT_PACK
4361 with a single element, which expands T. */
4362 tree vec = make_tree_vec (1);
4363 if (CHECKING_P)
4364 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4366 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4368 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4369 SET_ARGUMENT_PACK_ARGS (t, vec);
4372 else
4374 t = DECL_INITIAL (t);
4376 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4378 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4379 with a single element, which expands T. */
4380 tree vec = make_tree_vec (1);
4381 if (CHECKING_P)
4382 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4384 t = convert_from_reference (t);
4385 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4387 t = make_node (NONTYPE_ARGUMENT_PACK);
4388 SET_ARGUMENT_PACK_ARGS (t, vec);
4390 else
4391 t = convert_from_reference (t);
4393 return t;
4396 /* Given a single level of template parameters (a TREE_VEC), return it
4397 as a set of template arguments. */
4399 static tree
4400 template_parms_level_to_args (tree parms)
4402 tree a = copy_node (parms);
4403 TREE_TYPE (a) = NULL_TREE;
4404 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4405 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4407 if (CHECKING_P)
4408 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4410 return a;
4413 /* Given a set of template parameters, return them as a set of template
4414 arguments. The template parameters are represented as a TREE_VEC, in
4415 the form documented in cp-tree.h for template arguments. */
4417 static tree
4418 template_parms_to_args (tree parms)
4420 tree header;
4421 tree args = NULL_TREE;
4422 int length = TMPL_PARMS_DEPTH (parms);
4423 int l = length;
4425 /* If there is only one level of template parameters, we do not
4426 create a TREE_VEC of TREE_VECs. Instead, we return a single
4427 TREE_VEC containing the arguments. */
4428 if (length > 1)
4429 args = make_tree_vec (length);
4431 for (header = parms; header; header = TREE_CHAIN (header))
4433 tree a = template_parms_level_to_args (TREE_VALUE (header));
4435 if (length > 1)
4436 TREE_VEC_ELT (args, --l) = a;
4437 else
4438 args = a;
4441 return args;
4444 /* Within the declaration of a template, return the currently active
4445 template parameters as an argument TREE_VEC. */
4447 static tree
4448 current_template_args (void)
4450 return template_parms_to_args (current_template_parms);
4453 /* Update the declared TYPE by doing any lookups which were thought to be
4454 dependent, but are not now that we know the SCOPE of the declarator. */
4456 tree
4457 maybe_update_decl_type (tree orig_type, tree scope)
4459 tree type = orig_type;
4461 if (type == NULL_TREE)
4462 return type;
4464 if (TREE_CODE (orig_type) == TYPE_DECL)
4465 type = TREE_TYPE (type);
4467 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4468 && dependent_type_p (type)
4469 /* Don't bother building up the args in this case. */
4470 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4472 /* tsubst in the args corresponding to the template parameters,
4473 including auto if present. Most things will be unchanged, but
4474 make_typename_type and tsubst_qualified_id will resolve
4475 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4476 tree args = current_template_args ();
4477 tree auto_node = type_uses_auto (type);
4478 tree pushed;
4479 if (auto_node)
4481 tree auto_vec = make_tree_vec (1);
4482 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4483 args = add_to_template_args (args, auto_vec);
4485 pushed = push_scope (scope);
4486 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4487 if (pushed)
4488 pop_scope (scope);
4491 if (type == error_mark_node)
4492 return orig_type;
4494 if (TREE_CODE (orig_type) == TYPE_DECL)
4496 if (same_type_p (type, TREE_TYPE (orig_type)))
4497 type = orig_type;
4498 else
4499 type = TYPE_NAME (type);
4501 return type;
4504 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4505 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4506 the new template is a member template. */
4508 tree
4509 build_template_decl (tree decl, tree parms, bool member_template_p)
4511 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4512 DECL_TEMPLATE_PARMS (tmpl) = parms;
4513 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4514 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4515 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4517 return tmpl;
4520 struct template_parm_data
4522 /* The level of the template parameters we are currently
4523 processing. */
4524 int level;
4526 /* The index of the specialization argument we are currently
4527 processing. */
4528 int current_arg;
4530 /* An array whose size is the number of template parameters. The
4531 elements are nonzero if the parameter has been used in any one
4532 of the arguments processed so far. */
4533 int* parms;
4535 /* An array whose size is the number of template arguments. The
4536 elements are nonzero if the argument makes use of template
4537 parameters of this level. */
4538 int* arg_uses_template_parms;
4541 /* Subroutine of push_template_decl used to see if each template
4542 parameter in a partial specialization is used in the explicit
4543 argument list. If T is of the LEVEL given in DATA (which is
4544 treated as a template_parm_data*), then DATA->PARMS is marked
4545 appropriately. */
4547 static int
4548 mark_template_parm (tree t, void* data)
4550 int level;
4551 int idx;
4552 struct template_parm_data* tpd = (struct template_parm_data*) data;
4554 template_parm_level_and_index (t, &level, &idx);
4556 if (level == tpd->level)
4558 tpd->parms[idx] = 1;
4559 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4562 /* In C++17 the type of a non-type argument is a deduced context. */
4563 if (cxx_dialect >= cxx1z
4564 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4565 for_each_template_parm (TREE_TYPE (t),
4566 &mark_template_parm,
4567 data,
4568 NULL,
4569 /*include_nondeduced_p=*/false);
4571 /* Return zero so that for_each_template_parm will continue the
4572 traversal of the tree; we want to mark *every* template parm. */
4573 return 0;
4576 /* Process the partial specialization DECL. */
4578 static tree
4579 process_partial_specialization (tree decl)
4581 tree type = TREE_TYPE (decl);
4582 tree tinfo = get_template_info (decl);
4583 tree maintmpl = TI_TEMPLATE (tinfo);
4584 tree specargs = TI_ARGS (tinfo);
4585 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4586 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4587 tree inner_parms;
4588 tree inst;
4589 int nargs = TREE_VEC_LENGTH (inner_args);
4590 int ntparms;
4591 int i;
4592 bool did_error_intro = false;
4593 struct template_parm_data tpd;
4594 struct template_parm_data tpd2;
4596 gcc_assert (current_template_parms);
4598 /* A concept cannot be specialized. */
4599 if (flag_concepts && variable_concept_p (maintmpl))
4601 error ("specialization of variable concept %q#D", maintmpl);
4602 return error_mark_node;
4605 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4606 ntparms = TREE_VEC_LENGTH (inner_parms);
4608 /* We check that each of the template parameters given in the
4609 partial specialization is used in the argument list to the
4610 specialization. For example:
4612 template <class T> struct S;
4613 template <class T> struct S<T*>;
4615 The second declaration is OK because `T*' uses the template
4616 parameter T, whereas
4618 template <class T> struct S<int>;
4620 is no good. Even trickier is:
4622 template <class T>
4623 struct S1
4625 template <class U>
4626 struct S2;
4627 template <class U>
4628 struct S2<T>;
4631 The S2<T> declaration is actually invalid; it is a
4632 full-specialization. Of course,
4634 template <class U>
4635 struct S2<T (*)(U)>;
4637 or some such would have been OK. */
4638 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4639 tpd.parms = XALLOCAVEC (int, ntparms);
4640 memset (tpd.parms, 0, sizeof (int) * ntparms);
4642 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4643 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4644 for (i = 0; i < nargs; ++i)
4646 tpd.current_arg = i;
4647 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4648 &mark_template_parm,
4649 &tpd,
4650 NULL,
4651 /*include_nondeduced_p=*/false);
4653 for (i = 0; i < ntparms; ++i)
4654 if (tpd.parms[i] == 0)
4656 /* One of the template parms was not used in a deduced context in the
4657 specialization. */
4658 if (!did_error_intro)
4660 error ("template parameters not deducible in "
4661 "partial specialization:");
4662 did_error_intro = true;
4665 inform (input_location, " %qD",
4666 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4669 if (did_error_intro)
4670 return error_mark_node;
4672 /* [temp.class.spec]
4674 The argument list of the specialization shall not be identical to
4675 the implicit argument list of the primary template. */
4676 tree main_args
4677 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4678 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4679 && (!flag_concepts
4680 || !strictly_subsumes (current_template_constraints (),
4681 get_constraints (maintmpl))))
4683 if (!flag_concepts)
4684 error ("partial specialization %q+D does not specialize "
4685 "any template arguments", decl);
4686 else
4687 error ("partial specialization %q+D does not specialize any "
4688 "template arguments and is not more constrained than", decl);
4689 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4692 /* A partial specialization that replaces multiple parameters of the
4693 primary template with a pack expansion is less specialized for those
4694 parameters. */
4695 if (nargs < DECL_NTPARMS (maintmpl))
4697 error ("partial specialization is not more specialized than the "
4698 "primary template because it replaces multiple parameters "
4699 "with a pack expansion");
4700 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4701 /* Avoid crash in process_partial_specialization. */
4702 return decl;
4705 /* If we aren't in a dependent class, we can actually try deduction. */
4706 else if (tpd.level == 1
4707 /* FIXME we should be able to handle a partial specialization of a
4708 partial instantiation, but currently we can't (c++/41727). */
4709 && TMPL_ARGS_DEPTH (specargs) == 1
4710 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4712 if (permerror (input_location, "partial specialization %qD is not "
4713 "more specialized than", decl))
4714 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4715 maintmpl);
4718 /* [temp.class.spec]
4720 A partially specialized non-type argument expression shall not
4721 involve template parameters of the partial specialization except
4722 when the argument expression is a simple identifier.
4724 The type of a template parameter corresponding to a specialized
4725 non-type argument shall not be dependent on a parameter of the
4726 specialization.
4728 Also, we verify that pack expansions only occur at the
4729 end of the argument list. */
4730 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4731 tpd2.parms = 0;
4732 for (i = 0; i < nargs; ++i)
4734 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4735 tree arg = TREE_VEC_ELT (inner_args, i);
4736 tree packed_args = NULL_TREE;
4737 int j, len = 1;
4739 if (ARGUMENT_PACK_P (arg))
4741 /* Extract the arguments from the argument pack. We'll be
4742 iterating over these in the following loop. */
4743 packed_args = ARGUMENT_PACK_ARGS (arg);
4744 len = TREE_VEC_LENGTH (packed_args);
4747 for (j = 0; j < len; j++)
4749 if (packed_args)
4750 /* Get the Jth argument in the parameter pack. */
4751 arg = TREE_VEC_ELT (packed_args, j);
4753 if (PACK_EXPANSION_P (arg))
4755 /* Pack expansions must come at the end of the
4756 argument list. */
4757 if ((packed_args && j < len - 1)
4758 || (!packed_args && i < nargs - 1))
4760 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4761 error ("parameter pack argument %qE must be at the "
4762 "end of the template argument list", arg);
4763 else
4764 error ("parameter pack argument %qT must be at the "
4765 "end of the template argument list", arg);
4769 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4770 /* We only care about the pattern. */
4771 arg = PACK_EXPANSION_PATTERN (arg);
4773 if (/* These first two lines are the `non-type' bit. */
4774 !TYPE_P (arg)
4775 && TREE_CODE (arg) != TEMPLATE_DECL
4776 /* This next two lines are the `argument expression is not just a
4777 simple identifier' condition and also the `specialized
4778 non-type argument' bit. */
4779 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4780 && !(REFERENCE_REF_P (arg)
4781 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4783 if ((!packed_args && tpd.arg_uses_template_parms[i])
4784 || (packed_args && uses_template_parms (arg)))
4785 error ("template argument %qE involves template parameter(s)",
4786 arg);
4787 else
4789 /* Look at the corresponding template parameter,
4790 marking which template parameters its type depends
4791 upon. */
4792 tree type = TREE_TYPE (parm);
4794 if (!tpd2.parms)
4796 /* We haven't yet initialized TPD2. Do so now. */
4797 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4798 /* The number of parameters here is the number in the
4799 main template, which, as checked in the assertion
4800 above, is NARGS. */
4801 tpd2.parms = XALLOCAVEC (int, nargs);
4802 tpd2.level =
4803 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4806 /* Mark the template parameters. But this time, we're
4807 looking for the template parameters of the main
4808 template, not in the specialization. */
4809 tpd2.current_arg = i;
4810 tpd2.arg_uses_template_parms[i] = 0;
4811 memset (tpd2.parms, 0, sizeof (int) * nargs);
4812 for_each_template_parm (type,
4813 &mark_template_parm,
4814 &tpd2,
4815 NULL,
4816 /*include_nondeduced_p=*/false);
4818 if (tpd2.arg_uses_template_parms [i])
4820 /* The type depended on some template parameters.
4821 If they are fully specialized in the
4822 specialization, that's OK. */
4823 int j;
4824 int count = 0;
4825 for (j = 0; j < nargs; ++j)
4826 if (tpd2.parms[j] != 0
4827 && tpd.arg_uses_template_parms [j])
4828 ++count;
4829 if (count != 0)
4830 error_n (input_location, count,
4831 "type %qT of template argument %qE depends "
4832 "on a template parameter",
4833 "type %qT of template argument %qE depends "
4834 "on template parameters",
4835 type,
4836 arg);
4843 /* We should only get here once. */
4844 if (TREE_CODE (decl) == TYPE_DECL)
4845 gcc_assert (!COMPLETE_TYPE_P (type));
4847 // Build the template decl.
4848 tree tmpl = build_template_decl (decl, current_template_parms,
4849 DECL_MEMBER_TEMPLATE_P (maintmpl));
4850 TREE_TYPE (tmpl) = type;
4851 DECL_TEMPLATE_RESULT (tmpl) = decl;
4852 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4853 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4854 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4856 /* Give template template parms a DECL_CONTEXT of the template
4857 for which they are a parameter. */
4858 for (i = 0; i < ntparms; ++i)
4860 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4861 if (TREE_CODE (parm) == TEMPLATE_DECL)
4862 DECL_CONTEXT (parm) = tmpl;
4865 if (VAR_P (decl))
4866 /* We didn't register this in check_explicit_specialization so we could
4867 wait until the constraints were set. */
4868 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4869 else
4870 associate_classtype_constraints (type);
4872 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4873 = tree_cons (specargs, tmpl,
4874 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4875 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4877 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4878 inst = TREE_CHAIN (inst))
4880 tree instance = TREE_VALUE (inst);
4881 if (TYPE_P (instance)
4882 ? (COMPLETE_TYPE_P (instance)
4883 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4884 : DECL_TEMPLATE_INSTANTIATION (instance))
4886 tree spec = most_specialized_partial_spec (instance, tf_none);
4887 tree inst_decl = (DECL_P (instance)
4888 ? instance : TYPE_NAME (instance));
4889 if (!spec)
4890 /* OK */;
4891 else if (spec == error_mark_node)
4892 permerror (input_location,
4893 "declaration of %qD ambiguates earlier template "
4894 "instantiation for %qD", decl, inst_decl);
4895 else if (TREE_VALUE (spec) == tmpl)
4896 permerror (input_location,
4897 "partial specialization of %qD after instantiation "
4898 "of %qD", decl, inst_decl);
4902 return decl;
4905 /* PARM is a template parameter of some form; return the corresponding
4906 TEMPLATE_PARM_INDEX. */
4908 static tree
4909 get_template_parm_index (tree parm)
4911 if (TREE_CODE (parm) == PARM_DECL
4912 || TREE_CODE (parm) == CONST_DECL)
4913 parm = DECL_INITIAL (parm);
4914 else if (TREE_CODE (parm) == TYPE_DECL
4915 || TREE_CODE (parm) == TEMPLATE_DECL)
4916 parm = TREE_TYPE (parm);
4917 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4918 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4919 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4920 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4921 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4922 return parm;
4925 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4926 parameter packs used by the template parameter PARM. */
4928 static void
4929 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4931 /* A type parm can't refer to another parm. */
4932 if (TREE_CODE (parm) == TYPE_DECL)
4933 return;
4934 else if (TREE_CODE (parm) == PARM_DECL)
4936 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4937 ppd, ppd->visited);
4938 return;
4941 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4943 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4944 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4945 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4948 /* PARM is a template parameter pack. Return any parameter packs used in
4949 its type or the type of any of its template parameters. If there are
4950 any such packs, it will be instantiated into a fixed template parameter
4951 list by partial instantiation rather than be fully deduced. */
4953 tree
4954 fixed_parameter_pack_p (tree parm)
4956 /* This can only be true in a member template. */
4957 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4958 return NULL_TREE;
4959 /* This can only be true for a parameter pack. */
4960 if (!template_parameter_pack_p (parm))
4961 return NULL_TREE;
4962 /* A type parm can't refer to another parm. */
4963 if (TREE_CODE (parm) == TYPE_DECL)
4964 return NULL_TREE;
4966 tree parameter_packs = NULL_TREE;
4967 struct find_parameter_pack_data ppd;
4968 ppd.parameter_packs = &parameter_packs;
4969 ppd.visited = new hash_set<tree>;
4970 ppd.type_pack_expansion_p = false;
4972 fixed_parameter_pack_p_1 (parm, &ppd);
4974 delete ppd.visited;
4975 return parameter_packs;
4978 /* Check that a template declaration's use of default arguments and
4979 parameter packs is not invalid. Here, PARMS are the template
4980 parameters. IS_PRIMARY is true if DECL is the thing declared by
4981 a primary template. IS_PARTIAL is true if DECL is a partial
4982 specialization.
4984 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4985 declaration (but not a definition); 1 indicates a declaration, 2
4986 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4987 emitted for extraneous default arguments.
4989 Returns TRUE if there were no errors found, FALSE otherwise. */
4991 bool
4992 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4993 bool is_partial, int is_friend_decl)
4995 const char *msg;
4996 int last_level_to_check;
4997 tree parm_level;
4998 bool no_errors = true;
5000 /* [temp.param]
5002 A default template-argument shall not be specified in a
5003 function template declaration or a function template definition, nor
5004 in the template-parameter-list of the definition of a member of a
5005 class template. */
5007 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5008 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5009 /* You can't have a function template declaration in a local
5010 scope, nor you can you define a member of a class template in a
5011 local scope. */
5012 return true;
5014 if ((TREE_CODE (decl) == TYPE_DECL
5015 && TREE_TYPE (decl)
5016 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5017 || (TREE_CODE (decl) == FUNCTION_DECL
5018 && LAMBDA_FUNCTION_P (decl)))
5019 /* A lambda doesn't have an explicit declaration; don't complain
5020 about the parms of the enclosing class. */
5021 return true;
5023 if (current_class_type
5024 && !TYPE_BEING_DEFINED (current_class_type)
5025 && DECL_LANG_SPECIFIC (decl)
5026 && DECL_DECLARES_FUNCTION_P (decl)
5027 /* If this is either a friend defined in the scope of the class
5028 or a member function. */
5029 && (DECL_FUNCTION_MEMBER_P (decl)
5030 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5031 : DECL_FRIEND_CONTEXT (decl)
5032 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5033 : false)
5034 /* And, if it was a member function, it really was defined in
5035 the scope of the class. */
5036 && (!DECL_FUNCTION_MEMBER_P (decl)
5037 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5038 /* We already checked these parameters when the template was
5039 declared, so there's no need to do it again now. This function
5040 was defined in class scope, but we're processing its body now
5041 that the class is complete. */
5042 return true;
5044 /* Core issue 226 (C++0x only): the following only applies to class
5045 templates. */
5046 if (is_primary
5047 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5049 /* [temp.param]
5051 If a template-parameter has a default template-argument, all
5052 subsequent template-parameters shall have a default
5053 template-argument supplied. */
5054 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5056 tree inner_parms = TREE_VALUE (parm_level);
5057 int ntparms = TREE_VEC_LENGTH (inner_parms);
5058 int seen_def_arg_p = 0;
5059 int i;
5061 for (i = 0; i < ntparms; ++i)
5063 tree parm = TREE_VEC_ELT (inner_parms, i);
5065 if (parm == error_mark_node)
5066 continue;
5068 if (TREE_PURPOSE (parm))
5069 seen_def_arg_p = 1;
5070 else if (seen_def_arg_p
5071 && !template_parameter_pack_p (TREE_VALUE (parm)))
5073 error ("no default argument for %qD", TREE_VALUE (parm));
5074 /* For better subsequent error-recovery, we indicate that
5075 there should have been a default argument. */
5076 TREE_PURPOSE (parm) = error_mark_node;
5077 no_errors = false;
5079 else if (!is_partial
5080 && !is_friend_decl
5081 /* Don't complain about an enclosing partial
5082 specialization. */
5083 && parm_level == parms
5084 && TREE_CODE (decl) == TYPE_DECL
5085 && i < ntparms - 1
5086 && template_parameter_pack_p (TREE_VALUE (parm))
5087 /* A fixed parameter pack will be partially
5088 instantiated into a fixed length list. */
5089 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5091 /* A primary class template can only have one
5092 parameter pack, at the end of the template
5093 parameter list. */
5095 error ("parameter pack %q+D must be at the end of the"
5096 " template parameter list", TREE_VALUE (parm));
5098 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5099 = error_mark_node;
5100 no_errors = false;
5106 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5107 || is_partial
5108 || !is_primary
5109 || is_friend_decl)
5110 /* For an ordinary class template, default template arguments are
5111 allowed at the innermost level, e.g.:
5112 template <class T = int>
5113 struct S {};
5114 but, in a partial specialization, they're not allowed even
5115 there, as we have in [temp.class.spec]:
5117 The template parameter list of a specialization shall not
5118 contain default template argument values.
5120 So, for a partial specialization, or for a function template
5121 (in C++98/C++03), we look at all of them. */
5123 else
5124 /* But, for a primary class template that is not a partial
5125 specialization we look at all template parameters except the
5126 innermost ones. */
5127 parms = TREE_CHAIN (parms);
5129 /* Figure out what error message to issue. */
5130 if (is_friend_decl == 2)
5131 msg = G_("default template arguments may not be used in function template "
5132 "friend re-declaration");
5133 else if (is_friend_decl)
5134 msg = G_("default template arguments may not be used in function template "
5135 "friend declarations");
5136 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5137 msg = G_("default template arguments may not be used in function templates "
5138 "without -std=c++11 or -std=gnu++11");
5139 else if (is_partial)
5140 msg = G_("default template arguments may not be used in "
5141 "partial specializations");
5142 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5143 msg = G_("default argument for template parameter for class enclosing %qD");
5144 else
5145 /* Per [temp.param]/9, "A default template-argument shall not be
5146 specified in the template-parameter-lists of the definition of
5147 a member of a class template that appears outside of the member's
5148 class.", thus if we aren't handling a member of a class template
5149 there is no need to examine the parameters. */
5150 return true;
5152 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5153 /* If we're inside a class definition, there's no need to
5154 examine the parameters to the class itself. On the one
5155 hand, they will be checked when the class is defined, and,
5156 on the other, default arguments are valid in things like:
5157 template <class T = double>
5158 struct S { template <class U> void f(U); };
5159 Here the default argument for `S' has no bearing on the
5160 declaration of `f'. */
5161 last_level_to_check = template_class_depth (current_class_type) + 1;
5162 else
5163 /* Check everything. */
5164 last_level_to_check = 0;
5166 for (parm_level = parms;
5167 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5168 parm_level = TREE_CHAIN (parm_level))
5170 tree inner_parms = TREE_VALUE (parm_level);
5171 int i;
5172 int ntparms;
5174 ntparms = TREE_VEC_LENGTH (inner_parms);
5175 for (i = 0; i < ntparms; ++i)
5177 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5178 continue;
5180 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5182 if (msg)
5184 no_errors = false;
5185 if (is_friend_decl == 2)
5186 return no_errors;
5188 error (msg, decl);
5189 msg = 0;
5192 /* Clear out the default argument so that we are not
5193 confused later. */
5194 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5198 /* At this point, if we're still interested in issuing messages,
5199 they must apply to classes surrounding the object declared. */
5200 if (msg)
5201 msg = G_("default argument for template parameter for class "
5202 "enclosing %qD");
5205 return no_errors;
5208 /* Worker for push_template_decl_real, called via
5209 for_each_template_parm. DATA is really an int, indicating the
5210 level of the parameters we are interested in. If T is a template
5211 parameter of that level, return nonzero. */
5213 static int
5214 template_parm_this_level_p (tree t, void* data)
5216 int this_level = *(int *)data;
5217 int level;
5219 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5220 level = TEMPLATE_PARM_LEVEL (t);
5221 else
5222 level = TEMPLATE_TYPE_LEVEL (t);
5223 return level == this_level;
5226 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5227 DATA is really an int, indicating the innermost outer level of parameters.
5228 If T is a template parameter of that level or further out, return
5229 nonzero. */
5231 static int
5232 template_parm_outer_level (tree t, void *data)
5234 int this_level = *(int *)data;
5235 int level;
5237 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5238 level = TEMPLATE_PARM_LEVEL (t);
5239 else
5240 level = TEMPLATE_TYPE_LEVEL (t);
5241 return level <= this_level;
5244 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5245 parameters given by current_template_args, or reuses a
5246 previously existing one, if appropriate. Returns the DECL, or an
5247 equivalent one, if it is replaced via a call to duplicate_decls.
5249 If IS_FRIEND is true, DECL is a friend declaration. */
5251 tree
5252 push_template_decl_real (tree decl, bool is_friend)
5254 tree tmpl;
5255 tree args;
5256 tree info;
5257 tree ctx;
5258 bool is_primary;
5259 bool is_partial;
5260 int new_template_p = 0;
5261 /* True if the template is a member template, in the sense of
5262 [temp.mem]. */
5263 bool member_template_p = false;
5265 if (decl == error_mark_node || !current_template_parms)
5266 return error_mark_node;
5268 /* See if this is a partial specialization. */
5269 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5270 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5271 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5272 || (VAR_P (decl)
5273 && DECL_LANG_SPECIFIC (decl)
5274 && DECL_TEMPLATE_SPECIALIZATION (decl)
5275 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5277 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5278 is_friend = true;
5280 if (is_friend)
5281 /* For a friend, we want the context of the friend function, not
5282 the type of which it is a friend. */
5283 ctx = CP_DECL_CONTEXT (decl);
5284 else if (CP_DECL_CONTEXT (decl)
5285 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5286 /* In the case of a virtual function, we want the class in which
5287 it is defined. */
5288 ctx = CP_DECL_CONTEXT (decl);
5289 else
5290 /* Otherwise, if we're currently defining some class, the DECL
5291 is assumed to be a member of the class. */
5292 ctx = current_scope ();
5294 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5295 ctx = NULL_TREE;
5297 if (!DECL_CONTEXT (decl))
5298 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5300 /* See if this is a primary template. */
5301 if (is_friend && ctx
5302 && uses_template_parms_level (ctx, processing_template_decl))
5303 /* A friend template that specifies a class context, i.e.
5304 template <typename T> friend void A<T>::f();
5305 is not primary. */
5306 is_primary = false;
5307 else if (TREE_CODE (decl) == TYPE_DECL
5308 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5309 is_primary = false;
5310 else
5311 is_primary = template_parm_scope_p ();
5313 if (is_primary)
5315 warning (OPT_Wtemplates, "template %qD declared", decl);
5317 if (DECL_CLASS_SCOPE_P (decl))
5318 member_template_p = true;
5319 if (TREE_CODE (decl) == TYPE_DECL
5320 && anon_aggrname_p (DECL_NAME (decl)))
5322 error ("template class without a name");
5323 return error_mark_node;
5325 else if (TREE_CODE (decl) == FUNCTION_DECL)
5327 if (member_template_p)
5329 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5330 error ("member template %qD may not have virt-specifiers", decl);
5332 if (DECL_DESTRUCTOR_P (decl))
5334 /* [temp.mem]
5336 A destructor shall not be a member template. */
5337 error ("destructor %qD declared as member template", decl);
5338 return error_mark_node;
5340 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5341 && (!prototype_p (TREE_TYPE (decl))
5342 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5343 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5344 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5345 == void_list_node)))
5347 /* [basic.stc.dynamic.allocation]
5349 An allocation function can be a function
5350 template. ... Template allocation functions shall
5351 have two or more parameters. */
5352 error ("invalid template declaration of %qD", decl);
5353 return error_mark_node;
5356 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5357 && CLASS_TYPE_P (TREE_TYPE (decl)))
5359 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5360 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5361 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5363 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5364 if (TREE_CODE (t) == TYPE_DECL)
5365 t = TREE_TYPE (t);
5366 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5367 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5370 else if (TREE_CODE (decl) == TYPE_DECL
5371 && TYPE_DECL_ALIAS_P (decl))
5372 /* alias-declaration */
5373 gcc_assert (!DECL_ARTIFICIAL (decl));
5374 else if (VAR_P (decl))
5375 /* C++14 variable template. */;
5376 else
5378 error ("template declaration of %q#D", decl);
5379 return error_mark_node;
5383 /* Check to see that the rules regarding the use of default
5384 arguments are not being violated. */
5385 check_default_tmpl_args (decl, current_template_parms,
5386 is_primary, is_partial, /*is_friend_decl=*/0);
5388 /* Ensure that there are no parameter packs in the type of this
5389 declaration that have not been expanded. */
5390 if (TREE_CODE (decl) == FUNCTION_DECL)
5392 /* Check each of the arguments individually to see if there are
5393 any bare parameter packs. */
5394 tree type = TREE_TYPE (decl);
5395 tree arg = DECL_ARGUMENTS (decl);
5396 tree argtype = TYPE_ARG_TYPES (type);
5398 while (arg && argtype)
5400 if (!DECL_PACK_P (arg)
5401 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5403 /* This is a PARM_DECL that contains unexpanded parameter
5404 packs. We have already complained about this in the
5405 check_for_bare_parameter_packs call, so just replace
5406 these types with ERROR_MARK_NODE. */
5407 TREE_TYPE (arg) = error_mark_node;
5408 TREE_VALUE (argtype) = error_mark_node;
5411 arg = DECL_CHAIN (arg);
5412 argtype = TREE_CHAIN (argtype);
5415 /* Check for bare parameter packs in the return type and the
5416 exception specifiers. */
5417 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5418 /* Errors were already issued, set return type to int
5419 as the frontend doesn't expect error_mark_node as
5420 the return type. */
5421 TREE_TYPE (type) = integer_type_node;
5422 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5423 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5425 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5426 && TYPE_DECL_ALIAS_P (decl))
5427 ? DECL_ORIGINAL_TYPE (decl)
5428 : TREE_TYPE (decl)))
5430 TREE_TYPE (decl) = error_mark_node;
5431 return error_mark_node;
5434 if (is_partial)
5435 return process_partial_specialization (decl);
5437 args = current_template_args ();
5439 if (!ctx
5440 || TREE_CODE (ctx) == FUNCTION_DECL
5441 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5442 || (TREE_CODE (decl) == TYPE_DECL
5443 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5444 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5446 if (DECL_LANG_SPECIFIC (decl)
5447 && DECL_TEMPLATE_INFO (decl)
5448 && DECL_TI_TEMPLATE (decl))
5449 tmpl = DECL_TI_TEMPLATE (decl);
5450 /* If DECL is a TYPE_DECL for a class-template, then there won't
5451 be DECL_LANG_SPECIFIC. The information equivalent to
5452 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5453 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5454 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5455 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5457 /* Since a template declaration already existed for this
5458 class-type, we must be redeclaring it here. Make sure
5459 that the redeclaration is valid. */
5460 redeclare_class_template (TREE_TYPE (decl),
5461 current_template_parms,
5462 current_template_constraints ());
5463 /* We don't need to create a new TEMPLATE_DECL; just use the
5464 one we already had. */
5465 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5467 else
5469 tmpl = build_template_decl (decl, current_template_parms,
5470 member_template_p);
5471 new_template_p = 1;
5473 if (DECL_LANG_SPECIFIC (decl)
5474 && DECL_TEMPLATE_SPECIALIZATION (decl))
5476 /* A specialization of a member template of a template
5477 class. */
5478 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5479 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5480 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5484 else
5486 tree a, t, current, parms;
5487 int i;
5488 tree tinfo = get_template_info (decl);
5490 if (!tinfo)
5492 error ("template definition of non-template %q#D", decl);
5493 return error_mark_node;
5496 tmpl = TI_TEMPLATE (tinfo);
5498 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5499 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5500 && DECL_TEMPLATE_SPECIALIZATION (decl)
5501 && DECL_MEMBER_TEMPLATE_P (tmpl))
5503 tree new_tmpl;
5505 /* The declaration is a specialization of a member
5506 template, declared outside the class. Therefore, the
5507 innermost template arguments will be NULL, so we
5508 replace them with the arguments determined by the
5509 earlier call to check_explicit_specialization. */
5510 args = DECL_TI_ARGS (decl);
5512 new_tmpl
5513 = build_template_decl (decl, current_template_parms,
5514 member_template_p);
5515 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5516 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5517 DECL_TI_TEMPLATE (decl) = new_tmpl;
5518 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5519 DECL_TEMPLATE_INFO (new_tmpl)
5520 = build_template_info (tmpl, args);
5522 register_specialization (new_tmpl,
5523 most_general_template (tmpl),
5524 args,
5525 is_friend, 0);
5526 return decl;
5529 /* Make sure the template headers we got make sense. */
5531 parms = DECL_TEMPLATE_PARMS (tmpl);
5532 i = TMPL_PARMS_DEPTH (parms);
5533 if (TMPL_ARGS_DEPTH (args) != i)
5535 error ("expected %d levels of template parms for %q#D, got %d",
5536 i, decl, TMPL_ARGS_DEPTH (args));
5537 DECL_INTERFACE_KNOWN (decl) = 1;
5538 return error_mark_node;
5540 else
5541 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5543 a = TMPL_ARGS_LEVEL (args, i);
5544 t = INNERMOST_TEMPLATE_PARMS (parms);
5546 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5548 if (current == decl)
5549 error ("got %d template parameters for %q#D",
5550 TREE_VEC_LENGTH (a), decl);
5551 else
5552 error ("got %d template parameters for %q#T",
5553 TREE_VEC_LENGTH (a), current);
5554 error (" but %d required", TREE_VEC_LENGTH (t));
5555 /* Avoid crash in import_export_decl. */
5556 DECL_INTERFACE_KNOWN (decl) = 1;
5557 return error_mark_node;
5560 if (current == decl)
5561 current = ctx;
5562 else if (current == NULL_TREE)
5563 /* Can happen in erroneous input. */
5564 break;
5565 else
5566 current = get_containing_scope (current);
5569 /* Check that the parms are used in the appropriate qualifying scopes
5570 in the declarator. */
5571 if (!comp_template_args
5572 (TI_ARGS (tinfo),
5573 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5575 error ("\
5576 template arguments to %qD do not match original template %qD",
5577 decl, DECL_TEMPLATE_RESULT (tmpl));
5578 if (!uses_template_parms (TI_ARGS (tinfo)))
5579 inform (input_location, "use template<> for an explicit specialization");
5580 /* Avoid crash in import_export_decl. */
5581 DECL_INTERFACE_KNOWN (decl) = 1;
5582 return error_mark_node;
5586 DECL_TEMPLATE_RESULT (tmpl) = decl;
5587 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5589 /* Push template declarations for global functions and types. Note
5590 that we do not try to push a global template friend declared in a
5591 template class; such a thing may well depend on the template
5592 parameters of the class. */
5593 if (new_template_p && !ctx
5594 && !(is_friend && template_class_depth (current_class_type) > 0))
5596 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5597 if (tmpl == error_mark_node)
5598 return error_mark_node;
5600 /* Hide template friend classes that haven't been declared yet. */
5601 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5603 DECL_ANTICIPATED (tmpl) = 1;
5604 DECL_FRIEND_P (tmpl) = 1;
5608 if (is_primary)
5610 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5611 int i;
5613 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5614 if (DECL_CONV_FN_P (tmpl))
5616 int depth = TMPL_PARMS_DEPTH (parms);
5618 /* It is a conversion operator. See if the type converted to
5619 depends on innermost template operands. */
5621 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5622 depth))
5623 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5626 /* Give template template parms a DECL_CONTEXT of the template
5627 for which they are a parameter. */
5628 parms = INNERMOST_TEMPLATE_PARMS (parms);
5629 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5631 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5632 if (TREE_CODE (parm) == TEMPLATE_DECL)
5633 DECL_CONTEXT (parm) = tmpl;
5636 if (TREE_CODE (decl) == TYPE_DECL
5637 && TYPE_DECL_ALIAS_P (decl)
5638 && complex_alias_template_p (tmpl))
5639 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5642 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5643 back to its most general template. If TMPL is a specialization,
5644 ARGS may only have the innermost set of arguments. Add the missing
5645 argument levels if necessary. */
5646 if (DECL_TEMPLATE_INFO (tmpl))
5647 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5649 info = build_template_info (tmpl, args);
5651 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5652 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5653 else
5655 if (is_primary)
5656 retrofit_lang_decl (decl);
5657 if (DECL_LANG_SPECIFIC (decl))
5658 DECL_TEMPLATE_INFO (decl) = info;
5661 if (flag_implicit_templates
5662 && !is_friend
5663 && TREE_PUBLIC (decl)
5664 && VAR_OR_FUNCTION_DECL_P (decl))
5665 /* Set DECL_COMDAT on template instantiations; if we force
5666 them to be emitted by explicit instantiation or -frepo,
5667 mark_needed will tell cgraph to do the right thing. */
5668 DECL_COMDAT (decl) = true;
5670 return DECL_TEMPLATE_RESULT (tmpl);
5673 tree
5674 push_template_decl (tree decl)
5676 return push_template_decl_real (decl, false);
5679 /* FN is an inheriting constructor that inherits from the constructor
5680 template INHERITED; turn FN into a constructor template with a matching
5681 template header. */
5683 tree
5684 add_inherited_template_parms (tree fn, tree inherited)
5686 tree inner_parms
5687 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5688 inner_parms = copy_node (inner_parms);
5689 tree parms
5690 = tree_cons (size_int (processing_template_decl + 1),
5691 inner_parms, current_template_parms);
5692 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5693 tree args = template_parms_to_args (parms);
5694 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5695 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5696 DECL_TEMPLATE_RESULT (tmpl) = fn;
5697 DECL_ARTIFICIAL (tmpl) = true;
5698 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5699 return tmpl;
5702 /* Called when a class template TYPE is redeclared with the indicated
5703 template PARMS, e.g.:
5705 template <class T> struct S;
5706 template <class T> struct S {}; */
5708 bool
5709 redeclare_class_template (tree type, tree parms, tree cons)
5711 tree tmpl;
5712 tree tmpl_parms;
5713 int i;
5715 if (!TYPE_TEMPLATE_INFO (type))
5717 error ("%qT is not a template type", type);
5718 return false;
5721 tmpl = TYPE_TI_TEMPLATE (type);
5722 if (!PRIMARY_TEMPLATE_P (tmpl))
5723 /* The type is nested in some template class. Nothing to worry
5724 about here; there are no new template parameters for the nested
5725 type. */
5726 return true;
5728 if (!parms)
5730 error ("template specifiers not specified in declaration of %qD",
5731 tmpl);
5732 return false;
5735 parms = INNERMOST_TEMPLATE_PARMS (parms);
5736 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5738 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5740 error_n (input_location, TREE_VEC_LENGTH (parms),
5741 "redeclared with %d template parameter",
5742 "redeclared with %d template parameters",
5743 TREE_VEC_LENGTH (parms));
5744 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5745 "previous declaration %qD used %d template parameter",
5746 "previous declaration %qD used %d template parameters",
5747 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5748 return false;
5751 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5753 tree tmpl_parm;
5754 tree parm;
5755 tree tmpl_default;
5756 tree parm_default;
5758 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5759 || TREE_VEC_ELT (parms, i) == error_mark_node)
5760 continue;
5762 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5763 if (error_operand_p (tmpl_parm))
5764 return false;
5766 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5767 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5768 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5770 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5771 TEMPLATE_DECL. */
5772 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5773 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5774 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5775 || (TREE_CODE (tmpl_parm) != PARM_DECL
5776 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5777 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5778 || (TREE_CODE (tmpl_parm) == PARM_DECL
5779 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5780 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5782 error ("template parameter %q+#D", tmpl_parm);
5783 error ("redeclared here as %q#D", parm);
5784 return false;
5787 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5789 /* We have in [temp.param]:
5791 A template-parameter may not be given default arguments
5792 by two different declarations in the same scope. */
5793 error_at (input_location, "redefinition of default argument for %q#D", parm);
5794 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5795 "original definition appeared here");
5796 return false;
5799 if (parm_default != NULL_TREE)
5800 /* Update the previous template parameters (which are the ones
5801 that will really count) with the new default value. */
5802 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5803 else if (tmpl_default != NULL_TREE)
5804 /* Update the new parameters, too; they'll be used as the
5805 parameters for any members. */
5806 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5808 /* Give each template template parm in this redeclaration a
5809 DECL_CONTEXT of the template for which they are a parameter. */
5810 if (TREE_CODE (parm) == TEMPLATE_DECL)
5812 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5813 DECL_CONTEXT (parm) = tmpl;
5816 if (TREE_CODE (parm) == TYPE_DECL)
5817 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5820 // Cannot redeclare a class template with a different set of constraints.
5821 if (!equivalent_constraints (get_constraints (tmpl), cons))
5823 error_at (input_location, "redeclaration %q#D with different "
5824 "constraints", tmpl);
5825 inform (DECL_SOURCE_LOCATION (tmpl),
5826 "original declaration appeared here");
5829 return true;
5832 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5833 to be used when the caller has already checked
5834 (processing_template_decl
5835 && !instantiation_dependent_expression_p (expr)
5836 && potential_constant_expression (expr))
5837 and cleared processing_template_decl. */
5839 tree
5840 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5842 return tsubst_copy_and_build (expr,
5843 /*args=*/NULL_TREE,
5844 complain,
5845 /*in_decl=*/NULL_TREE,
5846 /*function_p=*/false,
5847 /*integral_constant_expression_p=*/true);
5850 /* Simplify EXPR if it is a non-dependent expression. Returns the
5851 (possibly simplified) expression. */
5853 tree
5854 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5856 if (expr == NULL_TREE)
5857 return NULL_TREE;
5859 /* If we're in a template, but EXPR isn't value dependent, simplify
5860 it. We're supposed to treat:
5862 template <typename T> void f(T[1 + 1]);
5863 template <typename T> void f(T[2]);
5865 as two declarations of the same function, for example. */
5866 if (processing_template_decl
5867 && is_nondependent_constant_expression (expr))
5869 processing_template_decl_sentinel s;
5870 expr = instantiate_non_dependent_expr_internal (expr, complain);
5872 return expr;
5875 tree
5876 instantiate_non_dependent_expr (tree expr)
5878 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5881 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5882 an uninstantiated expression. */
5884 tree
5885 instantiate_non_dependent_or_null (tree expr)
5887 if (expr == NULL_TREE)
5888 return NULL_TREE;
5889 if (processing_template_decl)
5891 if (!is_nondependent_constant_expression (expr))
5892 expr = NULL_TREE;
5893 else
5895 processing_template_decl_sentinel s;
5896 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5899 return expr;
5902 /* True iff T is a specialization of a variable template. */
5904 bool
5905 variable_template_specialization_p (tree t)
5907 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5908 return false;
5909 tree tmpl = DECL_TI_TEMPLATE (t);
5910 return variable_template_p (tmpl);
5913 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5914 template declaration, or a TYPE_DECL for an alias declaration. */
5916 bool
5917 alias_type_or_template_p (tree t)
5919 if (t == NULL_TREE)
5920 return false;
5921 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5922 || (TYPE_P (t)
5923 && TYPE_NAME (t)
5924 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5925 || DECL_ALIAS_TEMPLATE_P (t));
5928 /* Return TRUE iff T is a specialization of an alias template. */
5930 bool
5931 alias_template_specialization_p (const_tree t)
5933 /* It's an alias template specialization if it's an alias and its
5934 TYPE_NAME is a specialization of a primary template. */
5935 if (TYPE_ALIAS_P (t))
5936 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5937 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5939 return false;
5942 /* An alias template is complex from a SFINAE perspective if a template-id
5943 using that alias can be ill-formed when the expansion is not, as with
5944 the void_t template. We determine this by checking whether the
5945 expansion for the alias template uses all its template parameters. */
5947 struct uses_all_template_parms_data
5949 int level;
5950 bool *seen;
5953 static int
5954 uses_all_template_parms_r (tree t, void *data_)
5956 struct uses_all_template_parms_data &data
5957 = *(struct uses_all_template_parms_data*)data_;
5958 tree idx = get_template_parm_index (t);
5960 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5961 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5962 return 0;
5965 static bool
5966 complex_alias_template_p (const_tree tmpl)
5968 struct uses_all_template_parms_data data;
5969 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5970 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5971 data.level = TMPL_PARMS_DEPTH (parms);
5972 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5973 data.seen = XALLOCAVEC (bool, len);
5974 for (int i = 0; i < len; ++i)
5975 data.seen[i] = false;
5977 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5978 for (int i = 0; i < len; ++i)
5979 if (!data.seen[i])
5980 return true;
5981 return false;
5984 /* Return TRUE iff T is a specialization of a complex alias template with
5985 dependent template-arguments. */
5987 bool
5988 dependent_alias_template_spec_p (const_tree t)
5990 if (!alias_template_specialization_p (t))
5991 return false;
5993 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5994 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5995 return false;
5997 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5998 if (!any_dependent_template_arguments_p (args))
5999 return false;
6001 return true;
6004 /* Return the number of innermost template parameters in TMPL. */
6006 static int
6007 num_innermost_template_parms (tree tmpl)
6009 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6010 return TREE_VEC_LENGTH (parms);
6013 /* Return either TMPL or another template that it is equivalent to under DR
6014 1286: An alias that just changes the name of a template is equivalent to
6015 the other template. */
6017 static tree
6018 get_underlying_template (tree tmpl)
6020 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6021 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6023 /* Determine if the alias is equivalent to an underlying template. */
6024 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6025 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6026 if (!tinfo)
6027 break;
6029 tree underlying = TI_TEMPLATE (tinfo);
6030 if (!PRIMARY_TEMPLATE_P (underlying)
6031 || (num_innermost_template_parms (tmpl)
6032 != num_innermost_template_parms (underlying)))
6033 break;
6035 tree alias_args = INNERMOST_TEMPLATE_ARGS
6036 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6037 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6038 break;
6040 /* Alias is equivalent. Strip it and repeat. */
6041 tmpl = underlying;
6044 return tmpl;
6047 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6048 must be a reference-to-function or a pointer-to-function type, as specified
6049 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6050 and check that the resulting function has external linkage. */
6052 static tree
6053 convert_nontype_argument_function (tree type, tree expr,
6054 tsubst_flags_t complain)
6056 tree fns = expr;
6057 tree fn, fn_no_ptr;
6058 linkage_kind linkage;
6060 fn = instantiate_type (type, fns, tf_none);
6061 if (fn == error_mark_node)
6062 return error_mark_node;
6064 if (value_dependent_expression_p (fn))
6065 goto accept;
6067 fn_no_ptr = strip_fnptr_conv (fn);
6068 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6069 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6070 if (BASELINK_P (fn_no_ptr))
6071 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6073 /* [temp.arg.nontype]/1
6075 A template-argument for a non-type, non-template template-parameter
6076 shall be one of:
6077 [...]
6078 -- the address of an object or function with external [C++11: or
6079 internal] linkage. */
6081 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6083 if (complain & tf_error)
6085 error ("%qE is not a valid template argument for type %qT",
6086 expr, type);
6087 if (TYPE_PTR_P (type))
6088 inform (input_location, "it must be the address of a function "
6089 "with external linkage");
6090 else
6091 inform (input_location, "it must be the name of a function with "
6092 "external linkage");
6094 return NULL_TREE;
6097 linkage = decl_linkage (fn_no_ptr);
6098 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6100 if (complain & tf_error)
6102 if (cxx_dialect >= cxx11)
6103 error ("%qE is not a valid template argument for type %qT "
6104 "because %qD has no linkage",
6105 expr, type, fn_no_ptr);
6106 else
6107 error ("%qE is not a valid template argument for type %qT "
6108 "because %qD does not have external linkage",
6109 expr, type, fn_no_ptr);
6111 return NULL_TREE;
6114 accept:
6115 if (TREE_CODE (type) == REFERENCE_TYPE)
6116 fn = build_address (fn);
6117 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6118 fn = build_nop (type, fn);
6120 return fn;
6123 /* Subroutine of convert_nontype_argument.
6124 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6125 Emit an error otherwise. */
6127 static bool
6128 check_valid_ptrmem_cst_expr (tree type, tree expr,
6129 tsubst_flags_t complain)
6131 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6132 tree orig_expr = expr;
6133 STRIP_NOPS (expr);
6134 if (null_ptr_cst_p (expr))
6135 return true;
6136 if (TREE_CODE (expr) == PTRMEM_CST
6137 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6138 PTRMEM_CST_CLASS (expr)))
6139 return true;
6140 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6141 return true;
6142 if (processing_template_decl
6143 && TREE_CODE (expr) == ADDR_EXPR
6144 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6145 return true;
6146 if (complain & tf_error)
6148 error_at (loc, "%qE is not a valid template argument for type %qT",
6149 orig_expr, type);
6150 if (TREE_CODE (expr) != PTRMEM_CST)
6151 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6152 else
6153 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6155 return false;
6158 /* Returns TRUE iff the address of OP is value-dependent.
6160 14.6.2.4 [temp.dep.temp]:
6161 A non-integral non-type template-argument is dependent if its type is
6162 dependent or it has either of the following forms
6163 qualified-id
6164 & qualified-id
6165 and contains a nested-name-specifier which specifies a class-name that
6166 names a dependent type.
6168 We generalize this to just say that the address of a member of a
6169 dependent class is value-dependent; the above doesn't cover the
6170 address of a static data member named with an unqualified-id. */
6172 static bool
6173 has_value_dependent_address (tree op)
6175 /* We could use get_inner_reference here, but there's no need;
6176 this is only relevant for template non-type arguments, which
6177 can only be expressed as &id-expression. */
6178 if (DECL_P (op))
6180 tree ctx = CP_DECL_CONTEXT (op);
6181 if (TYPE_P (ctx) && dependent_type_p (ctx))
6182 return true;
6185 return false;
6188 /* The next set of functions are used for providing helpful explanatory
6189 diagnostics for failed overload resolution. Their messages should be
6190 indented by two spaces for consistency with the messages in
6191 call.c */
6193 static int
6194 unify_success (bool /*explain_p*/)
6196 return 0;
6199 /* Other failure functions should call this one, to provide a single function
6200 for setting a breakpoint on. */
6202 static int
6203 unify_invalid (bool /*explain_p*/)
6205 return 1;
6208 static int
6209 unify_parameter_deduction_failure (bool explain_p, tree parm)
6211 if (explain_p)
6212 inform (input_location,
6213 " couldn't deduce template parameter %qD", parm);
6214 return unify_invalid (explain_p);
6217 static int
6218 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6220 if (explain_p)
6221 inform (input_location,
6222 " types %qT and %qT have incompatible cv-qualifiers",
6223 parm, arg);
6224 return unify_invalid (explain_p);
6227 static int
6228 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6230 if (explain_p)
6231 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6232 return unify_invalid (explain_p);
6235 static int
6236 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6238 if (explain_p)
6239 inform (input_location,
6240 " template parameter %qD is not a parameter pack, but "
6241 "argument %qD is",
6242 parm, arg);
6243 return unify_invalid (explain_p);
6246 static int
6247 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6249 if (explain_p)
6250 inform (input_location,
6251 " template argument %qE does not match "
6252 "pointer-to-member constant %qE",
6253 arg, parm);
6254 return unify_invalid (explain_p);
6257 static int
6258 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6260 if (explain_p)
6261 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6262 return unify_invalid (explain_p);
6265 static int
6266 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6268 if (explain_p)
6269 inform (input_location,
6270 " inconsistent parameter pack deduction with %qT and %qT",
6271 old_arg, new_arg);
6272 return unify_invalid (explain_p);
6275 static int
6276 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6278 if (explain_p)
6280 if (TYPE_P (parm))
6281 inform (input_location,
6282 " deduced conflicting types for parameter %qT (%qT and %qT)",
6283 parm, first, second);
6284 else
6285 inform (input_location,
6286 " deduced conflicting values for non-type parameter "
6287 "%qE (%qE and %qE)", parm, first, second);
6289 return unify_invalid (explain_p);
6292 static int
6293 unify_vla_arg (bool explain_p, tree arg)
6295 if (explain_p)
6296 inform (input_location,
6297 " variable-sized array type %qT is not "
6298 "a valid template argument",
6299 arg);
6300 return unify_invalid (explain_p);
6303 static int
6304 unify_method_type_error (bool explain_p, tree arg)
6306 if (explain_p)
6307 inform (input_location,
6308 " member function type %qT is not a valid template argument",
6309 arg);
6310 return unify_invalid (explain_p);
6313 static int
6314 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6316 if (explain_p)
6318 if (least_p)
6319 inform_n (input_location, wanted,
6320 " candidate expects at least %d argument, %d provided",
6321 " candidate expects at least %d arguments, %d provided",
6322 wanted, have);
6323 else
6324 inform_n (input_location, wanted,
6325 " candidate expects %d argument, %d provided",
6326 " candidate expects %d arguments, %d provided",
6327 wanted, have);
6329 return unify_invalid (explain_p);
6332 static int
6333 unify_too_many_arguments (bool explain_p, int have, int wanted)
6335 return unify_arity (explain_p, have, wanted);
6338 static int
6339 unify_too_few_arguments (bool explain_p, int have, int wanted,
6340 bool least_p = false)
6342 return unify_arity (explain_p, have, wanted, least_p);
6345 static int
6346 unify_arg_conversion (bool explain_p, tree to_type,
6347 tree from_type, tree arg)
6349 if (explain_p)
6350 inform (EXPR_LOC_OR_LOC (arg, input_location),
6351 " cannot convert %qE (type %qT) to type %qT",
6352 arg, from_type, to_type);
6353 return unify_invalid (explain_p);
6356 static int
6357 unify_no_common_base (bool explain_p, enum template_base_result r,
6358 tree parm, tree arg)
6360 if (explain_p)
6361 switch (r)
6363 case tbr_ambiguous_baseclass:
6364 inform (input_location, " %qT is an ambiguous base class of %qT",
6365 parm, arg);
6366 break;
6367 default:
6368 inform (input_location, " %qT is not derived from %qT", arg, parm);
6369 break;
6371 return unify_invalid (explain_p);
6374 static int
6375 unify_inconsistent_template_template_parameters (bool explain_p)
6377 if (explain_p)
6378 inform (input_location,
6379 " template parameters of a template template argument are "
6380 "inconsistent with other deduced template arguments");
6381 return unify_invalid (explain_p);
6384 static int
6385 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6387 if (explain_p)
6388 inform (input_location,
6389 " can't deduce a template for %qT from non-template type %qT",
6390 parm, arg);
6391 return unify_invalid (explain_p);
6394 static int
6395 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6397 if (explain_p)
6398 inform (input_location,
6399 " template argument %qE does not match %qE", arg, parm);
6400 return unify_invalid (explain_p);
6403 /* Attempt to convert the non-type template parameter EXPR to the
6404 indicated TYPE. If the conversion is successful, return the
6405 converted value. If the conversion is unsuccessful, return
6406 NULL_TREE if we issued an error message, or error_mark_node if we
6407 did not. We issue error messages for out-and-out bad template
6408 parameters, but not simply because the conversion failed, since we
6409 might be just trying to do argument deduction. Both TYPE and EXPR
6410 must be non-dependent.
6412 The conversion follows the special rules described in
6413 [temp.arg.nontype], and it is much more strict than an implicit
6414 conversion.
6416 This function is called twice for each template argument (see
6417 lookup_template_class for a more accurate description of this
6418 problem). This means that we need to handle expressions which
6419 are not valid in a C++ source, but can be created from the
6420 first call (for instance, casts to perform conversions). These
6421 hacks can go away after we fix the double coercion problem. */
6423 static tree
6424 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6426 tree expr_type;
6427 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6428 tree orig_expr = expr;
6430 /* Detect immediately string literals as invalid non-type argument.
6431 This special-case is not needed for correctness (we would easily
6432 catch this later), but only to provide better diagnostic for this
6433 common user mistake. As suggested by DR 100, we do not mention
6434 linkage issues in the diagnostic as this is not the point. */
6435 /* FIXME we're making this OK. */
6436 if (TREE_CODE (expr) == STRING_CST)
6438 if (complain & tf_error)
6439 error ("%qE is not a valid template argument for type %qT "
6440 "because string literals can never be used in this context",
6441 expr, type);
6442 return NULL_TREE;
6445 /* Add the ADDR_EXPR now for the benefit of
6446 value_dependent_expression_p. */
6447 if (TYPE_PTROBV_P (type)
6448 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6450 expr = decay_conversion (expr, complain);
6451 if (expr == error_mark_node)
6452 return error_mark_node;
6455 /* If we are in a template, EXPR may be non-dependent, but still
6456 have a syntactic, rather than semantic, form. For example, EXPR
6457 might be a SCOPE_REF, rather than the VAR_DECL to which the
6458 SCOPE_REF refers. Preserving the qualifying scope is necessary
6459 so that access checking can be performed when the template is
6460 instantiated -- but here we need the resolved form so that we can
6461 convert the argument. */
6462 bool non_dep = false;
6463 if (TYPE_REF_OBJ_P (type)
6464 && has_value_dependent_address (expr))
6465 /* If we want the address and it's value-dependent, don't fold. */;
6466 else if (processing_template_decl
6467 && is_nondependent_constant_expression (expr))
6468 non_dep = true;
6469 if (error_operand_p (expr))
6470 return error_mark_node;
6471 expr_type = TREE_TYPE (expr);
6473 /* If the argument is non-dependent, perform any conversions in
6474 non-dependent context as well. */
6475 processing_template_decl_sentinel s (non_dep);
6476 if (non_dep)
6477 expr = instantiate_non_dependent_expr_internal (expr, complain);
6479 if (value_dependent_expression_p (expr))
6480 expr = canonicalize_expr_argument (expr, complain);
6482 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6483 to a non-type argument of "nullptr". */
6484 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6485 expr = fold_simple (convert (type, expr));
6487 /* In C++11, integral or enumeration non-type template arguments can be
6488 arbitrary constant expressions. Pointer and pointer to
6489 member arguments can be general constant expressions that evaluate
6490 to a null value, but otherwise still need to be of a specific form. */
6491 if (cxx_dialect >= cxx11)
6493 if (TREE_CODE (expr) == PTRMEM_CST)
6494 /* A PTRMEM_CST is already constant, and a valid template
6495 argument for a parameter of pointer to member type, we just want
6496 to leave it in that form rather than lower it to a
6497 CONSTRUCTOR. */;
6498 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6499 || cxx_dialect >= cxx1z)
6501 /* C++17: A template-argument for a non-type template-parameter shall
6502 be a converted constant expression (8.20) of the type of the
6503 template-parameter. */
6504 expr = build_converted_constant_expr (type, expr, complain);
6505 if (expr == error_mark_node)
6506 return error_mark_node;
6507 expr = maybe_constant_value (expr);
6508 expr = convert_from_reference (expr);
6510 else if (TYPE_PTR_OR_PTRMEM_P (type))
6512 tree folded = maybe_constant_value (expr);
6513 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6514 : null_member_pointer_value_p (folded))
6515 expr = folded;
6519 if (TREE_CODE (type) == REFERENCE_TYPE)
6520 expr = mark_lvalue_use (expr);
6521 else
6522 expr = mark_rvalue_use (expr);
6524 /* HACK: Due to double coercion, we can get a
6525 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6526 which is the tree that we built on the first call (see
6527 below when coercing to reference to object or to reference to
6528 function). We just strip everything and get to the arg.
6529 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6530 for examples. */
6531 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6533 tree probe_type, probe = expr;
6534 if (REFERENCE_REF_P (probe))
6535 probe = TREE_OPERAND (probe, 0);
6536 probe_type = TREE_TYPE (probe);
6537 if (TREE_CODE (probe) == NOP_EXPR)
6539 /* ??? Maybe we could use convert_from_reference here, but we
6540 would need to relax its constraints because the NOP_EXPR
6541 could actually change the type to something more cv-qualified,
6542 and this is not folded by convert_from_reference. */
6543 tree addr = TREE_OPERAND (probe, 0);
6544 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6545 && TREE_CODE (addr) == ADDR_EXPR
6546 && TYPE_PTR_P (TREE_TYPE (addr))
6547 && (same_type_ignoring_top_level_qualifiers_p
6548 (TREE_TYPE (probe_type),
6549 TREE_TYPE (TREE_TYPE (addr)))))
6551 expr = TREE_OPERAND (addr, 0);
6552 expr_type = TREE_TYPE (probe_type);
6557 /* [temp.arg.nontype]/5, bullet 1
6559 For a non-type template-parameter of integral or enumeration type,
6560 integral promotions (_conv.prom_) and integral conversions
6561 (_conv.integral_) are applied. */
6562 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6564 if (cxx_dialect < cxx11)
6566 tree t = build_converted_constant_expr (type, expr, complain);
6567 t = maybe_constant_value (t);
6568 if (t != error_mark_node)
6569 expr = t;
6572 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6573 return error_mark_node;
6575 /* Notice that there are constant expressions like '4 % 0' which
6576 do not fold into integer constants. */
6577 if (TREE_CODE (expr) != INTEGER_CST
6578 && !value_dependent_expression_p (expr))
6580 if (complain & tf_error)
6582 int errs = errorcount, warns = warningcount + werrorcount;
6583 if (!require_potential_constant_expression (expr))
6584 expr = error_mark_node;
6585 else
6586 expr = cxx_constant_value (expr);
6587 if (errorcount > errs || warningcount + werrorcount > warns)
6588 inform (loc, "in template argument for type %qT ", type);
6589 if (expr == error_mark_node)
6590 return NULL_TREE;
6591 /* else cxx_constant_value complained but gave us
6592 a real constant, so go ahead. */
6593 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6595 else
6596 return NULL_TREE;
6599 /* Avoid typedef problems. */
6600 if (TREE_TYPE (expr) != type)
6601 expr = fold_convert (type, expr);
6603 /* [temp.arg.nontype]/5, bullet 2
6605 For a non-type template-parameter of type pointer to object,
6606 qualification conversions (_conv.qual_) and the array-to-pointer
6607 conversion (_conv.array_) are applied. */
6608 else if (TYPE_PTROBV_P (type))
6610 tree decayed = expr;
6612 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6613 decay_conversion or an explicit cast. If it's a problematic cast,
6614 we'll complain about it below. */
6615 if (TREE_CODE (expr) == NOP_EXPR)
6617 tree probe = expr;
6618 STRIP_NOPS (probe);
6619 if (TREE_CODE (probe) == ADDR_EXPR
6620 && TYPE_PTR_P (TREE_TYPE (probe)))
6622 expr = probe;
6623 expr_type = TREE_TYPE (expr);
6627 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6629 A template-argument for a non-type, non-template template-parameter
6630 shall be one of: [...]
6632 -- the name of a non-type template-parameter;
6633 -- the address of an object or function with external linkage, [...]
6634 expressed as "& id-expression" where the & is optional if the name
6635 refers to a function or array, or if the corresponding
6636 template-parameter is a reference.
6638 Here, we do not care about functions, as they are invalid anyway
6639 for a parameter of type pointer-to-object. */
6641 if (value_dependent_expression_p (expr))
6642 /* Non-type template parameters are OK. */
6644 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6645 /* Null pointer values are OK in C++11. */;
6646 else if (TREE_CODE (expr) != ADDR_EXPR)
6648 if (VAR_P (expr))
6650 if (complain & tf_error)
6651 error ("%qD is not a valid template argument "
6652 "because %qD is a variable, not the address of "
6653 "a variable", orig_expr, expr);
6654 return NULL_TREE;
6656 if (POINTER_TYPE_P (expr_type))
6658 if (complain & tf_error)
6659 error ("%qE is not a valid template argument for %qT "
6660 "because it is not the address of a variable",
6661 orig_expr, type);
6662 return NULL_TREE;
6664 /* Other values, like integer constants, might be valid
6665 non-type arguments of some other type. */
6666 return error_mark_node;
6668 else
6670 tree decl = TREE_OPERAND (expr, 0);
6672 if (!VAR_P (decl))
6674 if (complain & tf_error)
6675 error ("%qE is not a valid template argument of type %qT "
6676 "because %qE is not a variable", orig_expr, type, decl);
6677 return NULL_TREE;
6679 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6681 if (complain & tf_error)
6682 error ("%qE is not a valid template argument of type %qT "
6683 "because %qD does not have external linkage",
6684 orig_expr, type, decl);
6685 return NULL_TREE;
6687 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx1z)
6688 && decl_linkage (decl) == lk_none)
6690 if (complain & tf_error)
6691 error ("%qE is not a valid template argument of type %qT "
6692 "because %qD has no linkage", orig_expr, type, decl);
6693 return NULL_TREE;
6695 /* C++17: For a non-type template-parameter of reference or pointer
6696 type, the value of the constant expression shall not refer to (or
6697 for a pointer type, shall not be the address of):
6698 * a subobject (4.5),
6699 * a temporary object (15.2),
6700 * a string literal (5.13.5),
6701 * the result of a typeid expression (8.2.8), or
6702 * a predefined __func__ variable (11.4.1). */
6703 else if (DECL_ARTIFICIAL (decl))
6705 if (complain & tf_error)
6706 error ("the address of %qD is not a valid template argument",
6707 decl);
6708 return NULL_TREE;
6710 else if (!same_type_ignoring_top_level_qualifiers_p
6711 (strip_array_types (TREE_TYPE (type)),
6712 strip_array_types (TREE_TYPE (decl))))
6714 if (complain & tf_error)
6715 error ("the address of the %qT subobject of %qD is not a "
6716 "valid template argument", TREE_TYPE (type), decl);
6717 return NULL_TREE;
6719 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6721 if (complain & tf_error)
6722 error ("the address of %qD is not a valid template argument "
6723 "because it does not have static storage duration",
6724 decl);
6725 return NULL_TREE;
6729 expr = decayed;
6731 expr = perform_qualification_conversions (type, expr);
6732 if (expr == error_mark_node)
6733 return error_mark_node;
6735 /* [temp.arg.nontype]/5, bullet 3
6737 For a non-type template-parameter of type reference to object, no
6738 conversions apply. The type referred to by the reference may be more
6739 cv-qualified than the (otherwise identical) type of the
6740 template-argument. The template-parameter is bound directly to the
6741 template-argument, which must be an lvalue. */
6742 else if (TYPE_REF_OBJ_P (type))
6744 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6745 expr_type))
6746 return error_mark_node;
6748 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6750 if (complain & tf_error)
6751 error ("%qE is not a valid template argument for type %qT "
6752 "because of conflicts in cv-qualification", expr, type);
6753 return NULL_TREE;
6756 if (!lvalue_p (expr))
6758 if (complain & tf_error)
6759 error ("%qE is not a valid template argument for type %qT "
6760 "because it is not an lvalue", expr, type);
6761 return NULL_TREE;
6764 /* [temp.arg.nontype]/1
6766 A template-argument for a non-type, non-template template-parameter
6767 shall be one of: [...]
6769 -- the address of an object or function with external linkage. */
6770 if (INDIRECT_REF_P (expr)
6771 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6773 expr = TREE_OPERAND (expr, 0);
6774 if (DECL_P (expr))
6776 if (complain & tf_error)
6777 error ("%q#D is not a valid template argument for type %qT "
6778 "because a reference variable does not have a constant "
6779 "address", expr, type);
6780 return NULL_TREE;
6784 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6785 && value_dependent_expression_p (expr))
6786 /* OK, dependent reference. We don't want to ask whether a DECL is
6787 itself value-dependent, since what we want here is its address. */;
6788 else
6790 if (!DECL_P (expr))
6792 if (complain & tf_error)
6793 error ("%qE is not a valid template argument for type %qT "
6794 "because it is not an object with linkage",
6795 expr, type);
6796 return NULL_TREE;
6799 /* DR 1155 allows internal linkage in C++11 and up. */
6800 linkage_kind linkage = decl_linkage (expr);
6801 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6803 if (complain & tf_error)
6804 error ("%qE is not a valid template argument for type %qT "
6805 "because object %qD does not have linkage",
6806 expr, type, expr);
6807 return NULL_TREE;
6810 expr = build_address (expr);
6813 if (!same_type_p (type, TREE_TYPE (expr)))
6814 expr = build_nop (type, expr);
6816 /* [temp.arg.nontype]/5, bullet 4
6818 For a non-type template-parameter of type pointer to function, only
6819 the function-to-pointer conversion (_conv.func_) is applied. If the
6820 template-argument represents a set of overloaded functions (or a
6821 pointer to such), the matching function is selected from the set
6822 (_over.over_). */
6823 else if (TYPE_PTRFN_P (type))
6825 /* If the argument is a template-id, we might not have enough
6826 context information to decay the pointer. */
6827 if (!type_unknown_p (expr_type))
6829 expr = decay_conversion (expr, complain);
6830 if (expr == error_mark_node)
6831 return error_mark_node;
6834 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6835 /* Null pointer values are OK in C++11. */
6836 return perform_qualification_conversions (type, expr);
6838 expr = convert_nontype_argument_function (type, expr, complain);
6839 if (!expr || expr == error_mark_node)
6840 return expr;
6842 /* [temp.arg.nontype]/5, bullet 5
6844 For a non-type template-parameter of type reference to function, no
6845 conversions apply. If the template-argument represents a set of
6846 overloaded functions, the matching function is selected from the set
6847 (_over.over_). */
6848 else if (TYPE_REFFN_P (type))
6850 if (TREE_CODE (expr) == ADDR_EXPR)
6852 if (complain & tf_error)
6854 error ("%qE is not a valid template argument for type %qT "
6855 "because it is a pointer", expr, type);
6856 inform (input_location, "try using %qE instead",
6857 TREE_OPERAND (expr, 0));
6859 return NULL_TREE;
6862 expr = convert_nontype_argument_function (type, expr, complain);
6863 if (!expr || expr == error_mark_node)
6864 return expr;
6866 /* [temp.arg.nontype]/5, bullet 6
6868 For a non-type template-parameter of type pointer to member function,
6869 no conversions apply. If the template-argument represents a set of
6870 overloaded member functions, the matching member function is selected
6871 from the set (_over.over_). */
6872 else if (TYPE_PTRMEMFUNC_P (type))
6874 expr = instantiate_type (type, expr, tf_none);
6875 if (expr == error_mark_node)
6876 return error_mark_node;
6878 /* [temp.arg.nontype] bullet 1 says the pointer to member
6879 expression must be a pointer-to-member constant. */
6880 if (!value_dependent_expression_p (expr)
6881 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6882 return NULL_TREE;
6884 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6885 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6886 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6887 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6889 /* [temp.arg.nontype]/5, bullet 7
6891 For a non-type template-parameter of type pointer to data member,
6892 qualification conversions (_conv.qual_) are applied. */
6893 else if (TYPE_PTRDATAMEM_P (type))
6895 /* [temp.arg.nontype] bullet 1 says the pointer to member
6896 expression must be a pointer-to-member constant. */
6897 if (!value_dependent_expression_p (expr)
6898 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6899 return NULL_TREE;
6901 expr = perform_qualification_conversions (type, expr);
6902 if (expr == error_mark_node)
6903 return expr;
6905 else if (NULLPTR_TYPE_P (type))
6907 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6909 if (complain & tf_error)
6910 error ("%qE is not a valid template argument for type %qT "
6911 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6912 return NULL_TREE;
6914 return expr;
6916 /* A template non-type parameter must be one of the above. */
6917 else
6918 gcc_unreachable ();
6920 /* Sanity check: did we actually convert the argument to the
6921 right type? */
6922 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6923 (type, TREE_TYPE (expr)));
6924 return convert_from_reference (expr);
6927 /* Subroutine of coerce_template_template_parms, which returns 1 if
6928 PARM_PARM and ARG_PARM match using the rule for the template
6929 parameters of template template parameters. Both PARM and ARG are
6930 template parameters; the rest of the arguments are the same as for
6931 coerce_template_template_parms.
6933 static int
6934 coerce_template_template_parm (tree parm,
6935 tree arg,
6936 tsubst_flags_t complain,
6937 tree in_decl,
6938 tree outer_args)
6940 if (arg == NULL_TREE || error_operand_p (arg)
6941 || parm == NULL_TREE || error_operand_p (parm))
6942 return 0;
6944 if (TREE_CODE (arg) != TREE_CODE (parm))
6945 return 0;
6947 switch (TREE_CODE (parm))
6949 case TEMPLATE_DECL:
6950 /* We encounter instantiations of templates like
6951 template <template <template <class> class> class TT>
6952 class C; */
6954 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6955 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6957 if (!coerce_template_template_parms
6958 (parmparm, argparm, complain, in_decl, outer_args))
6959 return 0;
6961 /* Fall through. */
6963 case TYPE_DECL:
6964 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6965 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6966 /* Argument is a parameter pack but parameter is not. */
6967 return 0;
6968 break;
6970 case PARM_DECL:
6971 /* The tsubst call is used to handle cases such as
6973 template <int> class C {};
6974 template <class T, template <T> class TT> class D {};
6975 D<int, C> d;
6977 i.e. the parameter list of TT depends on earlier parameters. */
6978 if (!uses_template_parms (TREE_TYPE (arg)))
6980 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6981 if (!uses_template_parms (t)
6982 && !same_type_p (t, TREE_TYPE (arg)))
6983 return 0;
6986 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6987 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6988 /* Argument is a parameter pack but parameter is not. */
6989 return 0;
6991 break;
6993 default:
6994 gcc_unreachable ();
6997 return 1;
7000 /* Coerce template argument list ARGLIST for use with template
7001 template-parameter TEMPL. */
7003 static tree
7004 coerce_template_args_for_ttp (tree templ, tree arglist,
7005 tsubst_flags_t complain)
7007 /* Consider an example where a template template parameter declared as
7009 template <class T, class U = std::allocator<T> > class TT
7011 The template parameter level of T and U are one level larger than
7012 of TT. To proper process the default argument of U, say when an
7013 instantiation `TT<int>' is seen, we need to build the full
7014 arguments containing {int} as the innermost level. Outer levels,
7015 available when not appearing as default template argument, can be
7016 obtained from the arguments of the enclosing template.
7018 Suppose that TT is later substituted with std::vector. The above
7019 instantiation is `TT<int, std::allocator<T> >' with TT at
7020 level 1, and T at level 2, while the template arguments at level 1
7021 becomes {std::vector} and the inner level 2 is {int}. */
7023 tree outer = DECL_CONTEXT (templ);
7024 if (outer)
7026 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7027 /* We want arguments for the partial specialization, not arguments for
7028 the primary template. */
7029 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7030 else
7031 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7033 else if (current_template_parms)
7035 /* This is an argument of the current template, so we haven't set
7036 DECL_CONTEXT yet. */
7037 tree relevant_template_parms;
7039 /* Parameter levels that are greater than the level of the given
7040 template template parm are irrelevant. */
7041 relevant_template_parms = current_template_parms;
7042 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7043 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7044 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7046 outer = template_parms_to_args (relevant_template_parms);
7049 if (outer)
7050 arglist = add_to_template_args (outer, arglist);
7052 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7053 return coerce_template_parms (parmlist, arglist, templ,
7054 complain,
7055 /*require_all_args=*/true,
7056 /*use_default_args=*/true);
7059 /* A cache of template template parameters with match-all default
7060 arguments. */
7061 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7062 static void
7063 store_defaulted_ttp (tree v, tree t)
7065 if (!defaulted_ttp_cache)
7066 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7067 defaulted_ttp_cache->put (v, t);
7069 static tree
7070 lookup_defaulted_ttp (tree v)
7072 if (defaulted_ttp_cache)
7073 if (tree *p = defaulted_ttp_cache->get (v))
7074 return *p;
7075 return NULL_TREE;
7078 /* T is a bound template template-parameter. Copy its arguments into default
7079 arguments of the template template-parameter's template parameters. */
7081 static tree
7082 add_defaults_to_ttp (tree otmpl)
7084 if (tree c = lookup_defaulted_ttp (otmpl))
7085 return c;
7087 tree ntmpl = copy_node (otmpl);
7089 tree ntype = copy_node (TREE_TYPE (otmpl));
7090 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7091 TYPE_MAIN_VARIANT (ntype) = ntype;
7092 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7093 TYPE_NAME (ntype) = ntmpl;
7094 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7096 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7097 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7098 TEMPLATE_PARM_DECL (idx) = ntmpl;
7099 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7101 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7102 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7103 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7104 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7105 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7107 tree o = TREE_VEC_ELT (vec, i);
7108 if (!template_parameter_pack_p (TREE_VALUE (o)))
7110 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7111 TREE_PURPOSE (n) = any_targ_node;
7115 store_defaulted_ttp (otmpl, ntmpl);
7116 return ntmpl;
7119 /* ARG is a bound potential template template-argument, and PARGS is a list
7120 of arguments for the corresponding template template-parameter. Adjust
7121 PARGS as appropriate for application to ARG's template, and if ARG is a
7122 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7123 arguments to the template template parameter. */
7125 static tree
7126 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7128 ++processing_template_decl;
7129 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7130 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7132 /* When comparing two template template-parameters in partial ordering,
7133 rewrite the one currently being used as an argument to have default
7134 arguments for all parameters. */
7135 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7136 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7137 if (pargs != error_mark_node)
7138 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7139 TYPE_TI_ARGS (arg));
7141 else
7143 tree aparms
7144 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7145 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7146 /*require_all*/true,
7147 /*use_default*/true);
7149 --processing_template_decl;
7150 return pargs;
7153 /* Subroutine of unify for the case when PARM is a
7154 BOUND_TEMPLATE_TEMPLATE_PARM. */
7156 static int
7157 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7158 bool explain_p)
7160 tree parmvec = TYPE_TI_ARGS (parm);
7161 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7163 /* The template template parm might be variadic and the argument
7164 not, so flatten both argument lists. */
7165 parmvec = expand_template_argument_pack (parmvec);
7166 argvec = expand_template_argument_pack (argvec);
7168 if (flag_new_ttp)
7170 /* In keeping with P0522R0, adjust P's template arguments
7171 to apply to A's template; then flatten it again. */
7172 tree nparmvec = parmvec;
7173 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7174 nparmvec = expand_template_argument_pack (nparmvec);
7176 if (unify (tparms, targs, nparmvec, argvec,
7177 UNIFY_ALLOW_NONE, explain_p))
7178 return 1;
7180 /* If the P0522 adjustment eliminated a pack expansion, deduce
7181 empty packs. */
7182 if (flag_new_ttp
7183 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7184 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7185 DEDUCE_EXACT, /*sub*/true, explain_p))
7186 return 1;
7188 else
7190 /* Deduce arguments T, i from TT<T> or TT<i>.
7191 We check each element of PARMVEC and ARGVEC individually
7192 rather than the whole TREE_VEC since they can have
7193 different number of elements, which is allowed under N2555. */
7195 int len = TREE_VEC_LENGTH (parmvec);
7197 /* Check if the parameters end in a pack, making them
7198 variadic. */
7199 int parm_variadic_p = 0;
7200 if (len > 0
7201 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7202 parm_variadic_p = 1;
7204 for (int i = 0; i < len - parm_variadic_p; ++i)
7205 /* If the template argument list of P contains a pack
7206 expansion that is not the last template argument, the
7207 entire template argument list is a non-deduced
7208 context. */
7209 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7210 return unify_success (explain_p);
7212 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7213 return unify_too_few_arguments (explain_p,
7214 TREE_VEC_LENGTH (argvec), len);
7216 for (int i = 0; i < len - parm_variadic_p; ++i)
7217 if (unify (tparms, targs,
7218 TREE_VEC_ELT (parmvec, i),
7219 TREE_VEC_ELT (argvec, i),
7220 UNIFY_ALLOW_NONE, explain_p))
7221 return 1;
7223 if (parm_variadic_p
7224 && unify_pack_expansion (tparms, targs,
7225 parmvec, argvec,
7226 DEDUCE_EXACT,
7227 /*subr=*/true, explain_p))
7228 return 1;
7231 return 0;
7234 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7235 template template parameters. Both PARM_PARMS and ARG_PARMS are
7236 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7237 or PARM_DECL.
7239 Consider the example:
7240 template <class T> class A;
7241 template<template <class U> class TT> class B;
7243 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7244 the parameters to A, and OUTER_ARGS contains A. */
7246 static int
7247 coerce_template_template_parms (tree parm_parms,
7248 tree arg_parms,
7249 tsubst_flags_t complain,
7250 tree in_decl,
7251 tree outer_args)
7253 int nparms, nargs, i;
7254 tree parm, arg;
7255 int variadic_p = 0;
7257 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7258 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7260 nparms = TREE_VEC_LENGTH (parm_parms);
7261 nargs = TREE_VEC_LENGTH (arg_parms);
7263 if (flag_new_ttp)
7265 /* P0522R0: A template template-parameter P is at least as specialized as
7266 a template template-argument A if, given the following rewrite to two
7267 function templates, the function template corresponding to P is at
7268 least as specialized as the function template corresponding to A
7269 according to the partial ordering rules for function templates
7270 ([temp.func.order]). Given an invented class template X with the
7271 template parameter list of A (including default arguments):
7273 * Each of the two function templates has the same template parameters,
7274 respectively, as P or A.
7276 * Each function template has a single function parameter whose type is
7277 a specialization of X with template arguments corresponding to the
7278 template parameters from the respective function template where, for
7279 each template parameter PP in the template parameter list of the
7280 function template, a corresponding template argument AA is formed. If
7281 PP declares a parameter pack, then AA is the pack expansion
7282 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7284 If the rewrite produces an invalid type, then P is not at least as
7285 specialized as A. */
7287 /* So coerce P's args to apply to A's parms, and then deduce between A's
7288 args and the converted args. If that succeeds, A is at least as
7289 specialized as P, so they match.*/
7290 tree pargs = template_parms_level_to_args (parm_parms);
7291 ++processing_template_decl;
7292 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7293 /*require_all*/true, /*use_default*/true);
7294 --processing_template_decl;
7295 if (pargs != error_mark_node)
7297 tree targs = make_tree_vec (nargs);
7298 tree aargs = template_parms_level_to_args (arg_parms);
7299 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7300 /*explain*/false))
7301 return 1;
7305 /* Determine whether we have a parameter pack at the end of the
7306 template template parameter's template parameter list. */
7307 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7309 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7311 if (error_operand_p (parm))
7312 return 0;
7314 switch (TREE_CODE (parm))
7316 case TEMPLATE_DECL:
7317 case TYPE_DECL:
7318 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7319 variadic_p = 1;
7320 break;
7322 case PARM_DECL:
7323 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7324 variadic_p = 1;
7325 break;
7327 default:
7328 gcc_unreachable ();
7332 if (nargs != nparms
7333 && !(variadic_p && nargs >= nparms - 1))
7334 return 0;
7336 /* Check all of the template parameters except the parameter pack at
7337 the end (if any). */
7338 for (i = 0; i < nparms - variadic_p; ++i)
7340 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7341 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7342 continue;
7344 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7345 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7347 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7348 outer_args))
7349 return 0;
7353 if (variadic_p)
7355 /* Check each of the template parameters in the template
7356 argument against the template parameter pack at the end of
7357 the template template parameter. */
7358 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7359 return 0;
7361 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7363 for (; i < nargs; ++i)
7365 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7366 continue;
7368 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7370 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7371 outer_args))
7372 return 0;
7376 return 1;
7379 /* Verifies that the deduced template arguments (in TARGS) for the
7380 template template parameters (in TPARMS) represent valid bindings,
7381 by comparing the template parameter list of each template argument
7382 to the template parameter list of its corresponding template
7383 template parameter, in accordance with DR150. This
7384 routine can only be called after all template arguments have been
7385 deduced. It will return TRUE if all of the template template
7386 parameter bindings are okay, FALSE otherwise. */
7387 bool
7388 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7390 int i, ntparms = TREE_VEC_LENGTH (tparms);
7391 bool ret = true;
7393 /* We're dealing with template parms in this process. */
7394 ++processing_template_decl;
7396 targs = INNERMOST_TEMPLATE_ARGS (targs);
7398 for (i = 0; i < ntparms; ++i)
7400 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7401 tree targ = TREE_VEC_ELT (targs, i);
7403 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7405 tree packed_args = NULL_TREE;
7406 int idx, len = 1;
7408 if (ARGUMENT_PACK_P (targ))
7410 /* Look inside the argument pack. */
7411 packed_args = ARGUMENT_PACK_ARGS (targ);
7412 len = TREE_VEC_LENGTH (packed_args);
7415 for (idx = 0; idx < len; ++idx)
7417 tree targ_parms = NULL_TREE;
7419 if (packed_args)
7420 /* Extract the next argument from the argument
7421 pack. */
7422 targ = TREE_VEC_ELT (packed_args, idx);
7424 if (PACK_EXPANSION_P (targ))
7425 /* Look at the pattern of the pack expansion. */
7426 targ = PACK_EXPANSION_PATTERN (targ);
7428 /* Extract the template parameters from the template
7429 argument. */
7430 if (TREE_CODE (targ) == TEMPLATE_DECL)
7431 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7432 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7433 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7435 /* Verify that we can coerce the template template
7436 parameters from the template argument to the template
7437 parameter. This requires an exact match. */
7438 if (targ_parms
7439 && !coerce_template_template_parms
7440 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7441 targ_parms,
7442 tf_none,
7443 tparm,
7444 targs))
7446 ret = false;
7447 goto out;
7453 out:
7455 --processing_template_decl;
7456 return ret;
7459 /* Since type attributes aren't mangled, we need to strip them from
7460 template type arguments. */
7462 static tree
7463 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7465 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7466 return arg;
7467 bool removed_attributes = false;
7468 tree canon = strip_typedefs (arg, &removed_attributes);
7469 if (removed_attributes
7470 && (complain & tf_warning))
7471 warning (OPT_Wignored_attributes,
7472 "ignoring attributes on template argument %qT", arg);
7473 return canon;
7476 /* And from inside dependent non-type arguments like sizeof(Type). */
7478 static tree
7479 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7481 if (!arg || arg == error_mark_node)
7482 return arg;
7483 bool removed_attributes = false;
7484 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7485 if (removed_attributes
7486 && (complain & tf_warning))
7487 warning (OPT_Wignored_attributes,
7488 "ignoring attributes in template argument %qE", arg);
7489 return canon;
7492 // A template declaration can be substituted for a constrained
7493 // template template parameter only when the argument is more
7494 // constrained than the parameter.
7495 static bool
7496 is_compatible_template_arg (tree parm, tree arg)
7498 tree parm_cons = get_constraints (parm);
7500 /* For now, allow constrained template template arguments
7501 and unconstrained template template parameters. */
7502 if (parm_cons == NULL_TREE)
7503 return true;
7505 tree arg_cons = get_constraints (arg);
7507 // If the template parameter is constrained, we need to rewrite its
7508 // constraints in terms of the ARG's template parameters. This ensures
7509 // that all of the template parameter types will have the same depth.
7511 // Note that this is only valid when coerce_template_template_parm is
7512 // true for the innermost template parameters of PARM and ARG. In other
7513 // words, because coercion is successful, this conversion will be valid.
7514 if (parm_cons)
7516 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7517 parm_cons = tsubst_constraint_info (parm_cons,
7518 INNERMOST_TEMPLATE_ARGS (args),
7519 tf_none, NULL_TREE);
7520 if (parm_cons == error_mark_node)
7521 return false;
7524 return subsumes (parm_cons, arg_cons);
7527 // Convert a placeholder argument into a binding to the original
7528 // parameter. The original parameter is saved as the TREE_TYPE of
7529 // ARG.
7530 static inline tree
7531 convert_wildcard_argument (tree parm, tree arg)
7533 TREE_TYPE (arg) = parm;
7534 return arg;
7537 /* Convert the indicated template ARG as necessary to match the
7538 indicated template PARM. Returns the converted ARG, or
7539 error_mark_node if the conversion was unsuccessful. Error and
7540 warning messages are issued under control of COMPLAIN. This
7541 conversion is for the Ith parameter in the parameter list. ARGS is
7542 the full set of template arguments deduced so far. */
7544 static tree
7545 convert_template_argument (tree parm,
7546 tree arg,
7547 tree args,
7548 tsubst_flags_t complain,
7549 int i,
7550 tree in_decl)
7552 tree orig_arg;
7553 tree val;
7554 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7556 if (parm == error_mark_node)
7557 return error_mark_node;
7559 /* Trivially convert placeholders. */
7560 if (TREE_CODE (arg) == WILDCARD_DECL)
7561 return convert_wildcard_argument (parm, arg);
7563 if (arg == any_targ_node)
7564 return arg;
7566 if (TREE_CODE (arg) == TREE_LIST
7567 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7569 /* The template argument was the name of some
7570 member function. That's usually
7571 invalid, but static members are OK. In any
7572 case, grab the underlying fields/functions
7573 and issue an error later if required. */
7574 orig_arg = TREE_VALUE (arg);
7575 TREE_TYPE (arg) = unknown_type_node;
7578 orig_arg = arg;
7580 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7581 requires_type = (TREE_CODE (parm) == TYPE_DECL
7582 || requires_tmpl_type);
7584 /* When determining whether an argument pack expansion is a template,
7585 look at the pattern. */
7586 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7587 arg = PACK_EXPANSION_PATTERN (arg);
7589 /* Deal with an injected-class-name used as a template template arg. */
7590 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7592 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7593 if (TREE_CODE (t) == TEMPLATE_DECL)
7595 if (cxx_dialect >= cxx11)
7596 /* OK under DR 1004. */;
7597 else if (complain & tf_warning_or_error)
7598 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7599 " used as template template argument", TYPE_NAME (arg));
7600 else if (flag_pedantic_errors)
7601 t = arg;
7603 arg = t;
7607 is_tmpl_type =
7608 ((TREE_CODE (arg) == TEMPLATE_DECL
7609 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7610 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7611 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7612 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7614 if (is_tmpl_type
7615 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7616 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7617 arg = TYPE_STUB_DECL (arg);
7619 is_type = TYPE_P (arg) || is_tmpl_type;
7621 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7622 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7624 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7626 if (complain & tf_error)
7627 error ("invalid use of destructor %qE as a type", orig_arg);
7628 return error_mark_node;
7631 permerror (input_location,
7632 "to refer to a type member of a template parameter, "
7633 "use %<typename %E%>", orig_arg);
7635 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7636 TREE_OPERAND (arg, 1),
7637 typename_type,
7638 complain);
7639 arg = orig_arg;
7640 is_type = 1;
7642 if (is_type != requires_type)
7644 if (in_decl)
7646 if (complain & tf_error)
7648 error ("type/value mismatch at argument %d in template "
7649 "parameter list for %qD",
7650 i + 1, in_decl);
7651 if (is_type)
7652 inform (input_location,
7653 " expected a constant of type %qT, got %qT",
7654 TREE_TYPE (parm),
7655 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7656 else if (requires_tmpl_type)
7657 inform (input_location,
7658 " expected a class template, got %qE", orig_arg);
7659 else
7660 inform (input_location,
7661 " expected a type, got %qE", orig_arg);
7664 return error_mark_node;
7666 if (is_tmpl_type ^ requires_tmpl_type)
7668 if (in_decl && (complain & tf_error))
7670 error ("type/value mismatch at argument %d in template "
7671 "parameter list for %qD",
7672 i + 1, in_decl);
7673 if (is_tmpl_type)
7674 inform (input_location,
7675 " expected a type, got %qT", DECL_NAME (arg));
7676 else
7677 inform (input_location,
7678 " expected a class template, got %qT", orig_arg);
7680 return error_mark_node;
7683 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7684 /* We already did the appropriate conversion when packing args. */
7685 val = orig_arg;
7686 else if (is_type)
7688 if (requires_tmpl_type)
7690 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7691 /* The number of argument required is not known yet.
7692 Just accept it for now. */
7693 val = orig_arg;
7694 else
7696 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7697 tree argparm;
7699 /* Strip alias templates that are equivalent to another
7700 template. */
7701 arg = get_underlying_template (arg);
7702 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7704 if (coerce_template_template_parms (parmparm, argparm,
7705 complain, in_decl,
7706 args))
7708 val = arg;
7710 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7711 TEMPLATE_DECL. */
7712 if (val != error_mark_node)
7714 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7715 val = TREE_TYPE (val);
7716 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7717 val = make_pack_expansion (val);
7720 else
7722 if (in_decl && (complain & tf_error))
7724 error ("type/value mismatch at argument %d in "
7725 "template parameter list for %qD",
7726 i + 1, in_decl);
7727 inform (input_location,
7728 " expected a template of type %qD, got %qT",
7729 parm, orig_arg);
7732 val = error_mark_node;
7735 // Check that the constraints are compatible before allowing the
7736 // substitution.
7737 if (val != error_mark_node)
7738 if (!is_compatible_template_arg (parm, arg))
7740 if (in_decl && (complain & tf_error))
7742 error ("constraint mismatch at argument %d in "
7743 "template parameter list for %qD",
7744 i + 1, in_decl);
7745 inform (input_location, " expected %qD but got %qD",
7746 parm, arg);
7748 val = error_mark_node;
7752 else
7753 val = orig_arg;
7754 /* We only form one instance of each template specialization.
7755 Therefore, if we use a non-canonical variant (i.e., a
7756 typedef), any future messages referring to the type will use
7757 the typedef, which is confusing if those future uses do not
7758 themselves also use the typedef. */
7759 if (TYPE_P (val))
7760 val = canonicalize_type_argument (val, complain);
7762 else
7764 tree t = TREE_TYPE (parm);
7766 if (tree a = type_uses_auto (t))
7768 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7769 if (t == error_mark_node)
7770 return error_mark_node;
7772 else
7773 t = tsubst (t, args, complain, in_decl);
7775 if (invalid_nontype_parm_type_p (t, complain))
7776 return error_mark_node;
7778 if (!type_dependent_expression_p (orig_arg)
7779 && !uses_template_parms (t))
7780 /* We used to call digest_init here. However, digest_init
7781 will report errors, which we don't want when complain
7782 is zero. More importantly, digest_init will try too
7783 hard to convert things: for example, `0' should not be
7784 converted to pointer type at this point according to
7785 the standard. Accepting this is not merely an
7786 extension, since deciding whether or not these
7787 conversions can occur is part of determining which
7788 function template to call, or whether a given explicit
7789 argument specification is valid. */
7790 val = convert_nontype_argument (t, orig_arg, complain);
7791 else
7792 val = canonicalize_expr_argument (orig_arg, complain);
7794 if (val == NULL_TREE)
7795 val = error_mark_node;
7796 else if (val == error_mark_node && (complain & tf_error))
7797 error ("could not convert template argument %qE from %qT to %qT",
7798 orig_arg, TREE_TYPE (orig_arg), t);
7800 if (INDIRECT_REF_P (val))
7802 /* Reject template arguments that are references to built-in
7803 functions with no library fallbacks. */
7804 const_tree inner = TREE_OPERAND (val, 0);
7805 const_tree innertype = TREE_TYPE (inner);
7806 if (innertype
7807 && TREE_CODE (innertype) == REFERENCE_TYPE
7808 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7809 && 0 < TREE_OPERAND_LENGTH (inner)
7810 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7811 return error_mark_node;
7814 if (TREE_CODE (val) == SCOPE_REF)
7816 /* Strip typedefs from the SCOPE_REF. */
7817 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7818 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7819 complain);
7820 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7821 QUALIFIED_NAME_IS_TEMPLATE (val));
7825 return val;
7828 /* Coerces the remaining template arguments in INNER_ARGS (from
7829 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7830 Returns the coerced argument pack. PARM_IDX is the position of this
7831 parameter in the template parameter list. ARGS is the original
7832 template argument list. */
7833 static tree
7834 coerce_template_parameter_pack (tree parms,
7835 int parm_idx,
7836 tree args,
7837 tree inner_args,
7838 int arg_idx,
7839 tree new_args,
7840 int* lost,
7841 tree in_decl,
7842 tsubst_flags_t complain)
7844 tree parm = TREE_VEC_ELT (parms, parm_idx);
7845 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7846 tree packed_args;
7847 tree argument_pack;
7848 tree packed_parms = NULL_TREE;
7850 if (arg_idx > nargs)
7851 arg_idx = nargs;
7853 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7855 /* When the template parameter is a non-type template parameter pack
7856 or template template parameter pack whose type or template
7857 parameters use parameter packs, we know exactly how many arguments
7858 we are looking for. Build a vector of the instantiated decls for
7859 these template parameters in PACKED_PARMS. */
7860 /* We can't use make_pack_expansion here because it would interpret a
7861 _DECL as a use rather than a declaration. */
7862 tree decl = TREE_VALUE (parm);
7863 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7864 SET_PACK_EXPANSION_PATTERN (exp, decl);
7865 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7866 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7868 TREE_VEC_LENGTH (args)--;
7869 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7870 TREE_VEC_LENGTH (args)++;
7872 if (packed_parms == error_mark_node)
7873 return error_mark_node;
7875 /* If we're doing a partial instantiation of a member template,
7876 verify that all of the types used for the non-type
7877 template parameter pack are, in fact, valid for non-type
7878 template parameters. */
7879 if (arg_idx < nargs
7880 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7882 int j, len = TREE_VEC_LENGTH (packed_parms);
7883 for (j = 0; j < len; ++j)
7885 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7886 if (invalid_nontype_parm_type_p (t, complain))
7887 return error_mark_node;
7889 /* We don't know how many args we have yet, just
7890 use the unconverted ones for now. */
7891 return NULL_TREE;
7894 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7896 /* Check if we have a placeholder pack, which indicates we're
7897 in the context of a introduction list. In that case we want
7898 to match this pack to the single placeholder. */
7899 else if (arg_idx < nargs
7900 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7901 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7903 nargs = arg_idx + 1;
7904 packed_args = make_tree_vec (1);
7906 else
7907 packed_args = make_tree_vec (nargs - arg_idx);
7909 /* Convert the remaining arguments, which will be a part of the
7910 parameter pack "parm". */
7911 int first_pack_arg = arg_idx;
7912 for (; arg_idx < nargs; ++arg_idx)
7914 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7915 tree actual_parm = TREE_VALUE (parm);
7916 int pack_idx = arg_idx - first_pack_arg;
7918 if (packed_parms)
7920 /* Once we've packed as many args as we have types, stop. */
7921 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7922 break;
7923 else if (PACK_EXPANSION_P (arg))
7924 /* We don't know how many args we have yet, just
7925 use the unconverted ones for now. */
7926 return NULL_TREE;
7927 else
7928 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7931 if (arg == error_mark_node)
7933 if (complain & tf_error)
7934 error ("template argument %d is invalid", arg_idx + 1);
7936 else
7937 arg = convert_template_argument (actual_parm,
7938 arg, new_args, complain, parm_idx,
7939 in_decl);
7940 if (arg == error_mark_node)
7941 (*lost)++;
7942 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7945 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7946 && TREE_VEC_LENGTH (packed_args) > 0)
7948 if (complain & tf_error)
7949 error ("wrong number of template arguments (%d, should be %d)",
7950 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7951 return error_mark_node;
7954 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7955 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7956 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7957 else
7959 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7960 TREE_CONSTANT (argument_pack) = 1;
7963 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7964 if (CHECKING_P)
7965 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7966 TREE_VEC_LENGTH (packed_args));
7967 return argument_pack;
7970 /* Returns the number of pack expansions in the template argument vector
7971 ARGS. */
7973 static int
7974 pack_expansion_args_count (tree args)
7976 int i;
7977 int count = 0;
7978 if (args)
7979 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7981 tree elt = TREE_VEC_ELT (args, i);
7982 if (elt && PACK_EXPANSION_P (elt))
7983 ++count;
7985 return count;
7988 /* Convert all template arguments to their appropriate types, and
7989 return a vector containing the innermost resulting template
7990 arguments. If any error occurs, return error_mark_node. Error and
7991 warning messages are issued under control of COMPLAIN.
7993 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7994 for arguments not specified in ARGS. Otherwise, if
7995 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7996 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7997 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7998 ARGS. */
8000 static tree
8001 coerce_template_parms (tree parms,
8002 tree args,
8003 tree in_decl,
8004 tsubst_flags_t complain,
8005 bool require_all_args,
8006 bool use_default_args)
8008 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8009 tree orig_inner_args;
8010 tree inner_args;
8011 tree new_args;
8012 tree new_inner_args;
8013 int saved_unevaluated_operand;
8014 int saved_inhibit_evaluation_warnings;
8016 /* When used as a boolean value, indicates whether this is a
8017 variadic template parameter list. Since it's an int, we can also
8018 subtract it from nparms to get the number of non-variadic
8019 parameters. */
8020 int variadic_p = 0;
8021 int variadic_args_p = 0;
8022 int post_variadic_parms = 0;
8024 /* Likewise for parameters with default arguments. */
8025 int default_p = 0;
8027 if (args == error_mark_node)
8028 return error_mark_node;
8030 nparms = TREE_VEC_LENGTH (parms);
8032 /* Determine if there are any parameter packs or default arguments. */
8033 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8035 tree parm = TREE_VEC_ELT (parms, parm_idx);
8036 if (variadic_p)
8037 ++post_variadic_parms;
8038 if (template_parameter_pack_p (TREE_VALUE (parm)))
8039 ++variadic_p;
8040 if (TREE_PURPOSE (parm))
8041 ++default_p;
8044 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8045 /* If there are no parameters that follow a parameter pack, we need to
8046 expand any argument packs so that we can deduce a parameter pack from
8047 some non-packed args followed by an argument pack, as in variadic85.C.
8048 If there are such parameters, we need to leave argument packs intact
8049 so the arguments are assigned properly. This can happen when dealing
8050 with a nested class inside a partial specialization of a class
8051 template, as in variadic92.C, or when deducing a template parameter pack
8052 from a sub-declarator, as in variadic114.C. */
8053 if (!post_variadic_parms)
8054 inner_args = expand_template_argument_pack (inner_args);
8056 /* Count any pack expansion args. */
8057 variadic_args_p = pack_expansion_args_count (inner_args);
8059 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8060 if ((nargs - variadic_args_p > nparms && !variadic_p)
8061 || (nargs < nparms - variadic_p
8062 && require_all_args
8063 && !variadic_args_p
8064 && (!use_default_args
8065 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8066 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8068 if (complain & tf_error)
8070 if (variadic_p || default_p)
8072 nparms -= variadic_p + default_p;
8073 error ("wrong number of template arguments "
8074 "(%d, should be at least %d)", nargs, nparms);
8076 else
8077 error ("wrong number of template arguments "
8078 "(%d, should be %d)", nargs, nparms);
8080 if (in_decl)
8081 inform (DECL_SOURCE_LOCATION (in_decl),
8082 "provided for %qD", in_decl);
8085 return error_mark_node;
8087 /* We can't pass a pack expansion to a non-pack parameter of an alias
8088 template (DR 1430). */
8089 else if (in_decl
8090 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8091 || concept_template_p (in_decl))
8092 && variadic_args_p
8093 && nargs - variadic_args_p < nparms - variadic_p)
8095 if (complain & tf_error)
8097 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8099 tree arg = TREE_VEC_ELT (inner_args, i);
8100 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8102 if (PACK_EXPANSION_P (arg)
8103 && !template_parameter_pack_p (parm))
8105 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8106 error_at (location_of (arg),
8107 "pack expansion argument for non-pack parameter "
8108 "%qD of alias template %qD", parm, in_decl);
8109 else
8110 error_at (location_of (arg),
8111 "pack expansion argument for non-pack parameter "
8112 "%qD of concept %qD", parm, in_decl);
8113 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8114 goto found;
8117 gcc_unreachable ();
8118 found:;
8120 return error_mark_node;
8123 /* We need to evaluate the template arguments, even though this
8124 template-id may be nested within a "sizeof". */
8125 saved_unevaluated_operand = cp_unevaluated_operand;
8126 cp_unevaluated_operand = 0;
8127 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8128 c_inhibit_evaluation_warnings = 0;
8129 new_inner_args = make_tree_vec (nparms);
8130 new_args = add_outermost_template_args (args, new_inner_args);
8131 int pack_adjust = 0;
8132 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8134 tree arg;
8135 tree parm;
8137 /* Get the Ith template parameter. */
8138 parm = TREE_VEC_ELT (parms, parm_idx);
8140 if (parm == error_mark_node)
8142 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8143 continue;
8146 /* Calculate the next argument. */
8147 if (arg_idx < nargs)
8148 arg = TREE_VEC_ELT (inner_args, arg_idx);
8149 else
8150 arg = NULL_TREE;
8152 if (template_parameter_pack_p (TREE_VALUE (parm))
8153 && !(arg && ARGUMENT_PACK_P (arg)))
8155 /* Some arguments will be placed in the
8156 template parameter pack PARM. */
8157 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8158 inner_args, arg_idx,
8159 new_args, &lost,
8160 in_decl, complain);
8162 if (arg == NULL_TREE)
8164 /* We don't know how many args we have yet, just use the
8165 unconverted (and still packed) ones for now. */
8166 new_inner_args = orig_inner_args;
8167 arg_idx = nargs;
8168 break;
8171 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8173 /* Store this argument. */
8174 if (arg == error_mark_node)
8176 lost++;
8177 /* We are done with all of the arguments. */
8178 arg_idx = nargs;
8180 else
8182 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8183 arg_idx += pack_adjust;
8186 continue;
8188 else if (arg)
8190 if (PACK_EXPANSION_P (arg))
8192 /* "If every valid specialization of a variadic template
8193 requires an empty template parameter pack, the template is
8194 ill-formed, no diagnostic required." So check that the
8195 pattern works with this parameter. */
8196 tree pattern = PACK_EXPANSION_PATTERN (arg);
8197 tree conv = convert_template_argument (TREE_VALUE (parm),
8198 pattern, new_args,
8199 complain, parm_idx,
8200 in_decl);
8201 if (conv == error_mark_node)
8203 if (complain & tf_error)
8204 inform (input_location, "so any instantiation with a "
8205 "non-empty parameter pack would be ill-formed");
8206 ++lost;
8208 else if (TYPE_P (conv) && !TYPE_P (pattern))
8209 /* Recover from missing typename. */
8210 TREE_VEC_ELT (inner_args, arg_idx)
8211 = make_pack_expansion (conv);
8213 /* We don't know how many args we have yet, just
8214 use the unconverted ones for now. */
8215 new_inner_args = inner_args;
8216 arg_idx = nargs;
8217 break;
8220 else if (require_all_args)
8222 /* There must be a default arg in this case. */
8223 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8224 complain, in_decl);
8225 /* The position of the first default template argument,
8226 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8227 Record that. */
8228 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8229 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8230 arg_idx - pack_adjust);
8232 else
8233 break;
8235 if (arg == error_mark_node)
8237 if (complain & tf_error)
8238 error ("template argument %d is invalid", arg_idx + 1);
8240 else if (!arg)
8241 /* This only occurs if there was an error in the template
8242 parameter list itself (which we would already have
8243 reported) that we are trying to recover from, e.g., a class
8244 template with a parameter list such as
8245 template<typename..., typename>. */
8246 ++lost;
8247 else
8248 arg = convert_template_argument (TREE_VALUE (parm),
8249 arg, new_args, complain,
8250 parm_idx, in_decl);
8252 if (arg == error_mark_node)
8253 lost++;
8254 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8256 cp_unevaluated_operand = saved_unevaluated_operand;
8257 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8259 if (variadic_p && arg_idx < nargs)
8261 if (complain & tf_error)
8263 error ("wrong number of template arguments "
8264 "(%d, should be %d)", nargs, arg_idx);
8265 if (in_decl)
8266 error ("provided for %q+D", in_decl);
8268 return error_mark_node;
8271 if (lost)
8272 return error_mark_node;
8274 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8275 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8276 TREE_VEC_LENGTH (new_inner_args));
8278 return new_inner_args;
8281 /* Convert all template arguments to their appropriate types, and
8282 return a vector containing the innermost resulting template
8283 arguments. If any error occurs, return error_mark_node. Error and
8284 warning messages are not issued.
8286 Note that no function argument deduction is performed, and default
8287 arguments are used to fill in unspecified arguments. */
8288 tree
8289 coerce_template_parms (tree parms, tree args, tree in_decl)
8291 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8294 /* Convert all template arguments to their appropriate type, and
8295 instantiate default arguments as needed. This returns a vector
8296 containing the innermost resulting template arguments, or
8297 error_mark_node if unsuccessful. */
8298 tree
8299 coerce_template_parms (tree parms, tree args, tree in_decl,
8300 tsubst_flags_t complain)
8302 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8305 /* Like coerce_template_parms. If PARMS represents all template
8306 parameters levels, this function returns a vector of vectors
8307 representing all the resulting argument levels. Note that in this
8308 case, only the innermost arguments are coerced because the
8309 outermost ones are supposed to have been coerced already.
8311 Otherwise, if PARMS represents only (the innermost) vector of
8312 parameters, this function returns a vector containing just the
8313 innermost resulting arguments. */
8315 static tree
8316 coerce_innermost_template_parms (tree parms,
8317 tree args,
8318 tree in_decl,
8319 tsubst_flags_t complain,
8320 bool require_all_args,
8321 bool use_default_args)
8323 int parms_depth = TMPL_PARMS_DEPTH (parms);
8324 int args_depth = TMPL_ARGS_DEPTH (args);
8325 tree coerced_args;
8327 if (parms_depth > 1)
8329 coerced_args = make_tree_vec (parms_depth);
8330 tree level;
8331 int cur_depth;
8333 for (level = parms, cur_depth = parms_depth;
8334 parms_depth > 0 && level != NULL_TREE;
8335 level = TREE_CHAIN (level), --cur_depth)
8337 tree l;
8338 if (cur_depth == args_depth)
8339 l = coerce_template_parms (TREE_VALUE (level),
8340 args, in_decl, complain,
8341 require_all_args,
8342 use_default_args);
8343 else
8344 l = TMPL_ARGS_LEVEL (args, cur_depth);
8346 if (l == error_mark_node)
8347 return error_mark_node;
8349 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8352 else
8353 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8354 args, in_decl, complain,
8355 require_all_args,
8356 use_default_args);
8357 return coerced_args;
8360 /* Returns 1 if template args OT and NT are equivalent. */
8363 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8365 if (nt == ot)
8366 return 1;
8367 if (nt == NULL_TREE || ot == NULL_TREE)
8368 return false;
8369 if (nt == any_targ_node || ot == any_targ_node)
8370 return true;
8372 if (TREE_CODE (nt) == TREE_VEC)
8373 /* For member templates */
8374 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8375 else if (PACK_EXPANSION_P (ot))
8376 return (PACK_EXPANSION_P (nt)
8377 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8378 PACK_EXPANSION_PATTERN (nt))
8379 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8380 PACK_EXPANSION_EXTRA_ARGS (nt)));
8381 else if (ARGUMENT_PACK_P (ot))
8383 int i, len;
8384 tree opack, npack;
8386 if (!ARGUMENT_PACK_P (nt))
8387 return 0;
8389 opack = ARGUMENT_PACK_ARGS (ot);
8390 npack = ARGUMENT_PACK_ARGS (nt);
8391 len = TREE_VEC_LENGTH (opack);
8392 if (TREE_VEC_LENGTH (npack) != len)
8393 return 0;
8394 for (i = 0; i < len; ++i)
8395 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8396 TREE_VEC_ELT (npack, i)))
8397 return 0;
8398 return 1;
8400 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8401 gcc_unreachable ();
8402 else if (TYPE_P (nt))
8404 if (!TYPE_P (ot))
8405 return false;
8406 /* Don't treat an alias template specialization with dependent
8407 arguments as equivalent to its underlying type when used as a
8408 template argument; we need them to be distinct so that we
8409 substitute into the specialization arguments at instantiation
8410 time. And aliases can't be equivalent without being ==, so
8411 we don't need to look any deeper.
8413 During partial ordering, however, we need to treat them normally so
8414 that we can order uses of the same alias with different
8415 cv-qualification (79960). */
8416 if (!partial_order
8417 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8418 return false;
8419 else
8420 return same_type_p (ot, nt);
8422 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8423 return 0;
8424 else
8426 /* Try to treat a template non-type argument that has been converted
8427 to the parameter type as equivalent to one that hasn't yet. */
8428 for (enum tree_code code1 = TREE_CODE (ot);
8429 CONVERT_EXPR_CODE_P (code1)
8430 || code1 == NON_LVALUE_EXPR;
8431 code1 = TREE_CODE (ot))
8432 ot = TREE_OPERAND (ot, 0);
8433 for (enum tree_code code2 = TREE_CODE (nt);
8434 CONVERT_EXPR_CODE_P (code2)
8435 || code2 == NON_LVALUE_EXPR;
8436 code2 = TREE_CODE (nt))
8437 nt = TREE_OPERAND (nt, 0);
8439 return cp_tree_equal (ot, nt);
8443 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8444 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8445 NEWARG_PTR with the offending arguments if they are non-NULL. */
8448 comp_template_args (tree oldargs, tree newargs,
8449 tree *oldarg_ptr, tree *newarg_ptr,
8450 bool partial_order)
8452 int i;
8454 if (oldargs == newargs)
8455 return 1;
8457 if (!oldargs || !newargs)
8458 return 0;
8460 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8461 return 0;
8463 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8465 tree nt = TREE_VEC_ELT (newargs, i);
8466 tree ot = TREE_VEC_ELT (oldargs, i);
8468 if (! template_args_equal (ot, nt, partial_order))
8470 if (oldarg_ptr != NULL)
8471 *oldarg_ptr = ot;
8472 if (newarg_ptr != NULL)
8473 *newarg_ptr = nt;
8474 return 0;
8477 return 1;
8480 inline bool
8481 comp_template_args_porder (tree oargs, tree nargs)
8483 return comp_template_args (oargs, nargs, NULL, NULL, true);
8486 static void
8487 add_pending_template (tree d)
8489 tree ti = (TYPE_P (d)
8490 ? CLASSTYPE_TEMPLATE_INFO (d)
8491 : DECL_TEMPLATE_INFO (d));
8492 struct pending_template *pt;
8493 int level;
8495 if (TI_PENDING_TEMPLATE_FLAG (ti))
8496 return;
8498 /* We are called both from instantiate_decl, where we've already had a
8499 tinst_level pushed, and instantiate_template, where we haven't.
8500 Compensate. */
8501 level = !current_tinst_level || current_tinst_level->decl != d;
8503 if (level)
8504 push_tinst_level (d);
8506 pt = ggc_alloc<pending_template> ();
8507 pt->next = NULL;
8508 pt->tinst = current_tinst_level;
8509 if (last_pending_template)
8510 last_pending_template->next = pt;
8511 else
8512 pending_templates = pt;
8514 last_pending_template = pt;
8516 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8518 if (level)
8519 pop_tinst_level ();
8523 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8524 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8525 documentation for TEMPLATE_ID_EXPR. */
8527 tree
8528 lookup_template_function (tree fns, tree arglist)
8530 tree type;
8532 if (fns == error_mark_node || arglist == error_mark_node)
8533 return error_mark_node;
8535 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8537 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8539 error ("%q#D is not a function template", fns);
8540 return error_mark_node;
8543 if (BASELINK_P (fns))
8545 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8546 unknown_type_node,
8547 BASELINK_FUNCTIONS (fns),
8548 arglist);
8549 return fns;
8552 type = TREE_TYPE (fns);
8553 if (TREE_CODE (fns) == OVERLOAD || !type)
8554 type = unknown_type_node;
8556 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8559 /* Within the scope of a template class S<T>, the name S gets bound
8560 (in build_self_reference) to a TYPE_DECL for the class, not a
8561 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8562 or one of its enclosing classes, and that type is a template,
8563 return the associated TEMPLATE_DECL. Otherwise, the original
8564 DECL is returned.
8566 Also handle the case when DECL is a TREE_LIST of ambiguous
8567 injected-class-names from different bases. */
8569 tree
8570 maybe_get_template_decl_from_type_decl (tree decl)
8572 if (decl == NULL_TREE)
8573 return decl;
8575 /* DR 176: A lookup that finds an injected-class-name (10.2
8576 [class.member.lookup]) can result in an ambiguity in certain cases
8577 (for example, if it is found in more than one base class). If all of
8578 the injected-class-names that are found refer to specializations of
8579 the same class template, and if the name is followed by a
8580 template-argument-list, the reference refers to the class template
8581 itself and not a specialization thereof, and is not ambiguous. */
8582 if (TREE_CODE (decl) == TREE_LIST)
8584 tree t, tmpl = NULL_TREE;
8585 for (t = decl; t; t = TREE_CHAIN (t))
8587 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8588 if (!tmpl)
8589 tmpl = elt;
8590 else if (tmpl != elt)
8591 break;
8593 if (tmpl && t == NULL_TREE)
8594 return tmpl;
8595 else
8596 return decl;
8599 return (decl != NULL_TREE
8600 && DECL_SELF_REFERENCE_P (decl)
8601 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8602 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8605 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8606 parameters, find the desired type.
8608 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8610 IN_DECL, if non-NULL, is the template declaration we are trying to
8611 instantiate.
8613 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8614 the class we are looking up.
8616 Issue error and warning messages under control of COMPLAIN.
8618 If the template class is really a local class in a template
8619 function, then the FUNCTION_CONTEXT is the function in which it is
8620 being instantiated.
8622 ??? Note that this function is currently called *twice* for each
8623 template-id: the first time from the parser, while creating the
8624 incomplete type (finish_template_type), and the second type during the
8625 real instantiation (instantiate_template_class). This is surely something
8626 that we want to avoid. It also causes some problems with argument
8627 coercion (see convert_nontype_argument for more information on this). */
8629 static tree
8630 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8631 int entering_scope, tsubst_flags_t complain)
8633 tree templ = NULL_TREE, parmlist;
8634 tree t;
8635 spec_entry **slot;
8636 spec_entry *entry;
8637 spec_entry elt;
8638 hashval_t hash;
8640 if (identifier_p (d1))
8642 tree value = innermost_non_namespace_value (d1);
8643 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8644 templ = value;
8645 else
8647 if (context)
8648 push_decl_namespace (context);
8649 templ = lookup_name (d1);
8650 templ = maybe_get_template_decl_from_type_decl (templ);
8651 if (context)
8652 pop_decl_namespace ();
8654 if (templ)
8655 context = DECL_CONTEXT (templ);
8657 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8659 tree type = TREE_TYPE (d1);
8661 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8662 an implicit typename for the second A. Deal with it. */
8663 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8664 type = TREE_TYPE (type);
8666 if (CLASSTYPE_TEMPLATE_INFO (type))
8668 templ = CLASSTYPE_TI_TEMPLATE (type);
8669 d1 = DECL_NAME (templ);
8672 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8673 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8675 templ = TYPE_TI_TEMPLATE (d1);
8676 d1 = DECL_NAME (templ);
8678 else if (DECL_TYPE_TEMPLATE_P (d1))
8680 templ = d1;
8681 d1 = DECL_NAME (templ);
8682 context = DECL_CONTEXT (templ);
8684 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8686 templ = d1;
8687 d1 = DECL_NAME (templ);
8690 /* Issue an error message if we didn't find a template. */
8691 if (! templ)
8693 if (complain & tf_error)
8694 error ("%qT is not a template", d1);
8695 return error_mark_node;
8698 if (TREE_CODE (templ) != TEMPLATE_DECL
8699 /* Make sure it's a user visible template, if it was named by
8700 the user. */
8701 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8702 && !PRIMARY_TEMPLATE_P (templ)))
8704 if (complain & tf_error)
8706 error ("non-template type %qT used as a template", d1);
8707 if (in_decl)
8708 error ("for template declaration %q+D", in_decl);
8710 return error_mark_node;
8713 complain &= ~tf_user;
8715 /* An alias that just changes the name of a template is equivalent to the
8716 other template, so if any of the arguments are pack expansions, strip
8717 the alias to avoid problems with a pack expansion passed to a non-pack
8718 alias template parameter (DR 1430). */
8719 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8720 templ = get_underlying_template (templ);
8722 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8724 tree parm;
8725 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8726 if (arglist2 == error_mark_node
8727 || (!uses_template_parms (arglist2)
8728 && check_instantiated_args (templ, arglist2, complain)))
8729 return error_mark_node;
8731 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8732 return parm;
8734 else
8736 tree template_type = TREE_TYPE (templ);
8737 tree gen_tmpl;
8738 tree type_decl;
8739 tree found = NULL_TREE;
8740 int arg_depth;
8741 int parm_depth;
8742 int is_dependent_type;
8743 int use_partial_inst_tmpl = false;
8745 if (template_type == error_mark_node)
8746 /* An error occurred while building the template TEMPL, and a
8747 diagnostic has most certainly been emitted for that
8748 already. Let's propagate that error. */
8749 return error_mark_node;
8751 gen_tmpl = most_general_template (templ);
8752 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8753 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8754 arg_depth = TMPL_ARGS_DEPTH (arglist);
8756 if (arg_depth == 1 && parm_depth > 1)
8758 /* We've been given an incomplete set of template arguments.
8759 For example, given:
8761 template <class T> struct S1 {
8762 template <class U> struct S2 {};
8763 template <class U> struct S2<U*> {};
8766 we will be called with an ARGLIST of `U*', but the
8767 TEMPLATE will be `template <class T> template
8768 <class U> struct S1<T>::S2'. We must fill in the missing
8769 arguments. */
8770 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8771 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8772 arg_depth = TMPL_ARGS_DEPTH (arglist);
8775 /* Now we should have enough arguments. */
8776 gcc_assert (parm_depth == arg_depth);
8778 /* From here on, we're only interested in the most general
8779 template. */
8781 /* Calculate the BOUND_ARGS. These will be the args that are
8782 actually tsubst'd into the definition to create the
8783 instantiation. */
8784 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8785 complain,
8786 /*require_all_args=*/true,
8787 /*use_default_args=*/true);
8789 if (arglist == error_mark_node)
8790 /* We were unable to bind the arguments. */
8791 return error_mark_node;
8793 /* In the scope of a template class, explicit references to the
8794 template class refer to the type of the template, not any
8795 instantiation of it. For example, in:
8797 template <class T> class C { void f(C<T>); }
8799 the `C<T>' is just the same as `C'. Outside of the
8800 class, however, such a reference is an instantiation. */
8801 if (entering_scope
8802 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8803 || currently_open_class (template_type))
8805 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8807 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8808 return template_type;
8811 /* If we already have this specialization, return it. */
8812 elt.tmpl = gen_tmpl;
8813 elt.args = arglist;
8814 elt.spec = NULL_TREE;
8815 hash = spec_hasher::hash (&elt);
8816 entry = type_specializations->find_with_hash (&elt, hash);
8818 if (entry)
8819 return entry->spec;
8821 /* If the the template's constraints are not satisfied,
8822 then we cannot form a valid type.
8824 Note that the check is deferred until after the hash
8825 lookup. This prevents redundant checks on previously
8826 instantiated specializations. */
8827 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8829 if (complain & tf_error)
8831 error ("template constraint failure");
8832 diagnose_constraints (input_location, gen_tmpl, arglist);
8834 return error_mark_node;
8837 is_dependent_type = uses_template_parms (arglist);
8839 /* If the deduced arguments are invalid, then the binding
8840 failed. */
8841 if (!is_dependent_type
8842 && check_instantiated_args (gen_tmpl,
8843 INNERMOST_TEMPLATE_ARGS (arglist),
8844 complain))
8845 return error_mark_node;
8847 if (!is_dependent_type
8848 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8849 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8850 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8852 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8853 DECL_NAME (gen_tmpl),
8854 /*tag_scope=*/ts_global);
8855 return found;
8858 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8859 complain, in_decl);
8860 if (context == error_mark_node)
8861 return error_mark_node;
8863 if (!context)
8864 context = global_namespace;
8866 /* Create the type. */
8867 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8869 /* The user referred to a specialization of an alias
8870 template represented by GEN_TMPL.
8872 [temp.alias]/2 says:
8874 When a template-id refers to the specialization of an
8875 alias template, it is equivalent to the associated
8876 type obtained by substitution of its
8877 template-arguments for the template-parameters in the
8878 type-id of the alias template. */
8880 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8881 /* Note that the call above (by indirectly calling
8882 register_specialization in tsubst_decl) registers the
8883 TYPE_DECL representing the specialization of the alias
8884 template. So next time someone substitutes ARGLIST for
8885 the template parms into the alias template (GEN_TMPL),
8886 she'll get that TYPE_DECL back. */
8888 if (t == error_mark_node)
8889 return t;
8891 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8893 if (!is_dependent_type)
8895 set_current_access_from_decl (TYPE_NAME (template_type));
8896 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8897 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8898 arglist, complain, in_decl),
8899 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8900 arglist, complain, in_decl),
8901 SCOPED_ENUM_P (template_type), NULL);
8903 if (t == error_mark_node)
8904 return t;
8906 else
8908 /* We don't want to call start_enum for this type, since
8909 the values for the enumeration constants may involve
8910 template parameters. And, no one should be interested
8911 in the enumeration constants for such a type. */
8912 t = cxx_make_type (ENUMERAL_TYPE);
8913 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8915 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8916 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8917 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8919 else if (CLASS_TYPE_P (template_type))
8921 t = make_class_type (TREE_CODE (template_type));
8922 CLASSTYPE_DECLARED_CLASS (t)
8923 = CLASSTYPE_DECLARED_CLASS (template_type);
8924 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8926 /* A local class. Make sure the decl gets registered properly. */
8927 if (context == current_function_decl)
8928 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8930 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8931 /* This instantiation is another name for the primary
8932 template type. Set the TYPE_CANONICAL field
8933 appropriately. */
8934 TYPE_CANONICAL (t) = template_type;
8935 else if (any_template_arguments_need_structural_equality_p (arglist))
8936 /* Some of the template arguments require structural
8937 equality testing, so this template class requires
8938 structural equality testing. */
8939 SET_TYPE_STRUCTURAL_EQUALITY (t);
8941 else
8942 gcc_unreachable ();
8944 /* If we called start_enum or pushtag above, this information
8945 will already be set up. */
8946 if (!TYPE_NAME (t))
8948 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8950 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8951 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8952 DECL_SOURCE_LOCATION (type_decl)
8953 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8955 else
8956 type_decl = TYPE_NAME (t);
8958 if (CLASS_TYPE_P (template_type))
8960 TREE_PRIVATE (type_decl)
8961 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8962 TREE_PROTECTED (type_decl)
8963 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8964 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8966 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8967 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8971 if (OVERLOAD_TYPE_P (t)
8972 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8974 static const char *tags[] = {"abi_tag", "may_alias"};
8976 for (unsigned ix = 0; ix != 2; ix++)
8978 tree attributes
8979 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8981 if (attributes)
8982 TYPE_ATTRIBUTES (t)
8983 = tree_cons (TREE_PURPOSE (attributes),
8984 TREE_VALUE (attributes),
8985 TYPE_ATTRIBUTES (t));
8989 /* Let's consider the explicit specialization of a member
8990 of a class template specialization that is implicitly instantiated,
8991 e.g.:
8992 template<class T>
8993 struct S
8995 template<class U> struct M {}; //#0
8998 template<>
8999 template<>
9000 struct S<int>::M<char> //#1
9002 int i;
9004 [temp.expl.spec]/4 says this is valid.
9006 In this case, when we write:
9007 S<int>::M<char> m;
9009 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9010 the one of #0.
9012 When we encounter #1, we want to store the partial instantiation
9013 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9015 For all cases other than this "explicit specialization of member of a
9016 class template", we just want to store the most general template into
9017 the CLASSTYPE_TI_TEMPLATE of M.
9019 This case of "explicit specialization of member of a class template"
9020 only happens when:
9021 1/ the enclosing class is an instantiation of, and therefore not
9022 the same as, the context of the most general template, and
9023 2/ we aren't looking at the partial instantiation itself, i.e.
9024 the innermost arguments are not the same as the innermost parms of
9025 the most general template.
9027 So it's only when 1/ and 2/ happens that we want to use the partial
9028 instantiation of the member template in lieu of its most general
9029 template. */
9031 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9032 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9033 /* the enclosing class must be an instantiation... */
9034 && CLASS_TYPE_P (context)
9035 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9037 TREE_VEC_LENGTH (arglist)--;
9038 ++processing_template_decl;
9039 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9040 tree partial_inst_args =
9041 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9042 arglist, complain, NULL_TREE);
9043 --processing_template_decl;
9044 TREE_VEC_LENGTH (arglist)++;
9045 if (partial_inst_args == error_mark_node)
9046 return error_mark_node;
9047 use_partial_inst_tmpl =
9048 /*...and we must not be looking at the partial instantiation
9049 itself. */
9050 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9051 partial_inst_args);
9054 if (!use_partial_inst_tmpl)
9055 /* This case is easy; there are no member templates involved. */
9056 found = gen_tmpl;
9057 else
9059 /* This is a full instantiation of a member template. Find
9060 the partial instantiation of which this is an instance. */
9062 /* Temporarily reduce by one the number of levels in the ARGLIST
9063 so as to avoid comparing the last set of arguments. */
9064 TREE_VEC_LENGTH (arglist)--;
9065 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9066 TREE_VEC_LENGTH (arglist)++;
9067 /* FOUND is either a proper class type, or an alias
9068 template specialization. In the later case, it's a
9069 TYPE_DECL, resulting from the substituting of arguments
9070 for parameters in the TYPE_DECL of the alias template
9071 done earlier. So be careful while getting the template
9072 of FOUND. */
9073 found = (TREE_CODE (found) == TEMPLATE_DECL
9074 ? found
9075 : (TREE_CODE (found) == TYPE_DECL
9076 ? DECL_TI_TEMPLATE (found)
9077 : CLASSTYPE_TI_TEMPLATE (found)));
9080 // Build template info for the new specialization.
9081 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9083 elt.spec = t;
9084 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9085 entry = ggc_alloc<spec_entry> ();
9086 *entry = elt;
9087 *slot = entry;
9089 /* Note this use of the partial instantiation so we can check it
9090 later in maybe_process_partial_specialization. */
9091 DECL_TEMPLATE_INSTANTIATIONS (found)
9092 = tree_cons (arglist, t,
9093 DECL_TEMPLATE_INSTANTIATIONS (found));
9095 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9096 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9097 /* Now that the type has been registered on the instantiations
9098 list, we set up the enumerators. Because the enumeration
9099 constants may involve the enumeration type itself, we make
9100 sure to register the type first, and then create the
9101 constants. That way, doing tsubst_expr for the enumeration
9102 constants won't result in recursive calls here; we'll find
9103 the instantiation and exit above. */
9104 tsubst_enum (template_type, t, arglist);
9106 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9107 /* If the type makes use of template parameters, the
9108 code that generates debugging information will crash. */
9109 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9111 /* Possibly limit visibility based on template args. */
9112 TREE_PUBLIC (type_decl) = 1;
9113 determine_visibility (type_decl);
9115 inherit_targ_abi_tags (t);
9117 return t;
9121 /* Wrapper for lookup_template_class_1. */
9123 tree
9124 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9125 int entering_scope, tsubst_flags_t complain)
9127 tree ret;
9128 timevar_push (TV_TEMPLATE_INST);
9129 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9130 entering_scope, complain);
9131 timevar_pop (TV_TEMPLATE_INST);
9132 return ret;
9135 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9137 tree
9138 lookup_template_variable (tree templ, tree arglist)
9140 /* The type of the expression is NULL_TREE since the template-id could refer
9141 to an explicit or partial specialization. */
9142 tree type = NULL_TREE;
9143 if (flag_concepts && variable_concept_p (templ))
9144 /* Except that concepts are always bool. */
9145 type = boolean_type_node;
9146 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9149 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9151 tree
9152 finish_template_variable (tree var, tsubst_flags_t complain)
9154 tree templ = TREE_OPERAND (var, 0);
9155 tree arglist = TREE_OPERAND (var, 1);
9157 /* We never want to return a VAR_DECL for a variable concept, since they
9158 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9159 bool concept_p = flag_concepts && variable_concept_p (templ);
9160 if (concept_p && processing_template_decl)
9161 return var;
9163 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9164 arglist = add_outermost_template_args (tmpl_args, arglist);
9166 templ = most_general_template (templ);
9167 tree parms = DECL_TEMPLATE_PARMS (templ);
9168 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9169 /*req_all*/true,
9170 /*use_default*/true);
9172 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9174 if (complain & tf_error)
9176 error ("use of invalid variable template %qE", var);
9177 diagnose_constraints (location_of (var), templ, arglist);
9179 return error_mark_node;
9182 /* If a template-id refers to a specialization of a variable
9183 concept, then the expression is true if and only if the
9184 concept's constraints are satisfied by the given template
9185 arguments.
9187 NOTE: This is an extension of Concepts Lite TS that
9188 allows constraints to be used in expressions. */
9189 if (concept_p)
9191 tree decl = DECL_TEMPLATE_RESULT (templ);
9192 return evaluate_variable_concept (decl, arglist);
9195 return instantiate_template (templ, arglist, complain);
9198 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9199 TARGS template args, and instantiate it if it's not dependent. */
9201 tree
9202 lookup_and_finish_template_variable (tree templ, tree targs,
9203 tsubst_flags_t complain)
9205 templ = lookup_template_variable (templ, targs);
9206 if (!any_dependent_template_arguments_p (targs))
9208 templ = finish_template_variable (templ, complain);
9209 mark_used (templ);
9212 return convert_from_reference (templ);
9216 struct pair_fn_data
9218 tree_fn_t fn;
9219 tree_fn_t any_fn;
9220 void *data;
9221 /* True when we should also visit template parameters that occur in
9222 non-deduced contexts. */
9223 bool include_nondeduced_p;
9224 hash_set<tree> *visited;
9227 /* Called from for_each_template_parm via walk_tree. */
9229 static tree
9230 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9232 tree t = *tp;
9233 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9234 tree_fn_t fn = pfd->fn;
9235 void *data = pfd->data;
9236 tree result = NULL_TREE;
9238 #define WALK_SUBTREE(NODE) \
9239 do \
9241 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9242 pfd->include_nondeduced_p, \
9243 pfd->any_fn); \
9244 if (result) goto out; \
9246 while (0)
9248 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9249 return t;
9251 if (TYPE_P (t)
9252 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9253 WALK_SUBTREE (TYPE_CONTEXT (t));
9255 switch (TREE_CODE (t))
9257 case RECORD_TYPE:
9258 if (TYPE_PTRMEMFUNC_P (t))
9259 break;
9260 /* Fall through. */
9262 case UNION_TYPE:
9263 case ENUMERAL_TYPE:
9264 if (!TYPE_TEMPLATE_INFO (t))
9265 *walk_subtrees = 0;
9266 else
9267 WALK_SUBTREE (TYPE_TI_ARGS (t));
9268 break;
9270 case INTEGER_TYPE:
9271 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9272 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9273 break;
9275 case METHOD_TYPE:
9276 /* Since we're not going to walk subtrees, we have to do this
9277 explicitly here. */
9278 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9279 /* Fall through. */
9281 case FUNCTION_TYPE:
9282 /* Check the return type. */
9283 WALK_SUBTREE (TREE_TYPE (t));
9285 /* Check the parameter types. Since default arguments are not
9286 instantiated until they are needed, the TYPE_ARG_TYPES may
9287 contain expressions that involve template parameters. But,
9288 no-one should be looking at them yet. And, once they're
9289 instantiated, they don't contain template parameters, so
9290 there's no point in looking at them then, either. */
9292 tree parm;
9294 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9295 WALK_SUBTREE (TREE_VALUE (parm));
9297 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9298 want walk_tree walking into them itself. */
9299 *walk_subtrees = 0;
9302 if (flag_noexcept_type)
9304 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9305 if (spec)
9306 WALK_SUBTREE (TREE_PURPOSE (spec));
9308 break;
9310 case TYPEOF_TYPE:
9311 case UNDERLYING_TYPE:
9312 if (pfd->include_nondeduced_p
9313 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9314 pfd->visited,
9315 pfd->include_nondeduced_p,
9316 pfd->any_fn))
9317 return error_mark_node;
9318 break;
9320 case FUNCTION_DECL:
9321 case VAR_DECL:
9322 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9323 WALK_SUBTREE (DECL_TI_ARGS (t));
9324 /* Fall through. */
9326 case PARM_DECL:
9327 case CONST_DECL:
9328 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9329 WALK_SUBTREE (DECL_INITIAL (t));
9330 if (DECL_CONTEXT (t)
9331 && pfd->include_nondeduced_p)
9332 WALK_SUBTREE (DECL_CONTEXT (t));
9333 break;
9335 case BOUND_TEMPLATE_TEMPLATE_PARM:
9336 /* Record template parameters such as `T' inside `TT<T>'. */
9337 WALK_SUBTREE (TYPE_TI_ARGS (t));
9338 /* Fall through. */
9340 case TEMPLATE_TEMPLATE_PARM:
9341 case TEMPLATE_TYPE_PARM:
9342 case TEMPLATE_PARM_INDEX:
9343 if (fn && (*fn)(t, data))
9344 return t;
9345 else if (!fn)
9346 return t;
9347 break;
9349 case TEMPLATE_DECL:
9350 /* A template template parameter is encountered. */
9351 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9352 WALK_SUBTREE (TREE_TYPE (t));
9354 /* Already substituted template template parameter */
9355 *walk_subtrees = 0;
9356 break;
9358 case TYPENAME_TYPE:
9359 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9360 partial instantiation. */
9361 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9362 break;
9364 case CONSTRUCTOR:
9365 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9366 && pfd->include_nondeduced_p)
9367 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9368 break;
9370 case INDIRECT_REF:
9371 case COMPONENT_REF:
9372 /* If there's no type, then this thing must be some expression
9373 involving template parameters. */
9374 if (!fn && !TREE_TYPE (t))
9375 return error_mark_node;
9376 break;
9378 case MODOP_EXPR:
9379 case CAST_EXPR:
9380 case IMPLICIT_CONV_EXPR:
9381 case REINTERPRET_CAST_EXPR:
9382 case CONST_CAST_EXPR:
9383 case STATIC_CAST_EXPR:
9384 case DYNAMIC_CAST_EXPR:
9385 case ARROW_EXPR:
9386 case DOTSTAR_EXPR:
9387 case TYPEID_EXPR:
9388 case PSEUDO_DTOR_EXPR:
9389 if (!fn)
9390 return error_mark_node;
9391 break;
9393 default:
9394 break;
9397 #undef WALK_SUBTREE
9399 /* We didn't find any template parameters we liked. */
9400 out:
9401 return result;
9404 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9405 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9406 call FN with the parameter and the DATA.
9407 If FN returns nonzero, the iteration is terminated, and
9408 for_each_template_parm returns 1. Otherwise, the iteration
9409 continues. If FN never returns a nonzero value, the value
9410 returned by for_each_template_parm is 0. If FN is NULL, it is
9411 considered to be the function which always returns 1.
9413 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9414 parameters that occur in non-deduced contexts. When false, only
9415 visits those template parameters that can be deduced. */
9417 static tree
9418 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9419 hash_set<tree> *visited,
9420 bool include_nondeduced_p,
9421 tree_fn_t any_fn)
9423 struct pair_fn_data pfd;
9424 tree result;
9426 /* Set up. */
9427 pfd.fn = fn;
9428 pfd.any_fn = any_fn;
9429 pfd.data = data;
9430 pfd.include_nondeduced_p = include_nondeduced_p;
9432 /* Walk the tree. (Conceptually, we would like to walk without
9433 duplicates, but for_each_template_parm_r recursively calls
9434 for_each_template_parm, so we would need to reorganize a fair
9435 bit to use walk_tree_without_duplicates, so we keep our own
9436 visited list.) */
9437 if (visited)
9438 pfd.visited = visited;
9439 else
9440 pfd.visited = new hash_set<tree>;
9441 result = cp_walk_tree (&t,
9442 for_each_template_parm_r,
9443 &pfd,
9444 pfd.visited);
9446 /* Clean up. */
9447 if (!visited)
9449 delete pfd.visited;
9450 pfd.visited = 0;
9453 return result;
9456 /* Returns true if T depends on any template parameter. */
9459 uses_template_parms (tree t)
9461 if (t == NULL_TREE)
9462 return false;
9464 bool dependent_p;
9465 int saved_processing_template_decl;
9467 saved_processing_template_decl = processing_template_decl;
9468 if (!saved_processing_template_decl)
9469 processing_template_decl = 1;
9470 if (TYPE_P (t))
9471 dependent_p = dependent_type_p (t);
9472 else if (TREE_CODE (t) == TREE_VEC)
9473 dependent_p = any_dependent_template_arguments_p (t);
9474 else if (TREE_CODE (t) == TREE_LIST)
9475 dependent_p = (uses_template_parms (TREE_VALUE (t))
9476 || uses_template_parms (TREE_CHAIN (t)));
9477 else if (TREE_CODE (t) == TYPE_DECL)
9478 dependent_p = dependent_type_p (TREE_TYPE (t));
9479 else if (DECL_P (t)
9480 || EXPR_P (t)
9481 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9482 || TREE_CODE (t) == OVERLOAD
9483 || BASELINK_P (t)
9484 || identifier_p (t)
9485 || TREE_CODE (t) == TRAIT_EXPR
9486 || TREE_CODE (t) == CONSTRUCTOR
9487 || CONSTANT_CLASS_P (t))
9488 dependent_p = (type_dependent_expression_p (t)
9489 || value_dependent_expression_p (t));
9490 else
9492 gcc_assert (t == error_mark_node);
9493 dependent_p = false;
9496 processing_template_decl = saved_processing_template_decl;
9498 return dependent_p;
9501 /* Returns true iff current_function_decl is an incompletely instantiated
9502 template. Useful instead of processing_template_decl because the latter
9503 is set to 0 during instantiate_non_dependent_expr. */
9505 bool
9506 in_template_function (void)
9508 tree fn = current_function_decl;
9509 bool ret;
9510 ++processing_template_decl;
9511 ret = (fn && DECL_LANG_SPECIFIC (fn)
9512 && DECL_TEMPLATE_INFO (fn)
9513 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9514 --processing_template_decl;
9515 return ret;
9518 /* Returns true if T depends on any template parameter with level LEVEL. */
9520 bool
9521 uses_template_parms_level (tree t, int level)
9523 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9524 /*include_nondeduced_p=*/true);
9527 /* Returns true if the signature of DECL depends on any template parameter from
9528 its enclosing class. */
9530 bool
9531 uses_outer_template_parms (tree decl)
9533 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9534 if (depth == 0)
9535 return false;
9536 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9537 &depth, NULL, /*include_nondeduced_p=*/true))
9538 return true;
9539 if (PRIMARY_TEMPLATE_P (decl)
9540 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9541 (DECL_TEMPLATE_PARMS (decl)),
9542 template_parm_outer_level,
9543 &depth, NULL, /*include_nondeduced_p=*/true))
9544 return true;
9545 tree ci = get_constraints (decl);
9546 if (ci)
9547 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9548 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9549 &depth, NULL, /*nondeduced*/true))
9550 return true;
9551 return false;
9554 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9555 ill-formed translation unit, i.e. a variable or function that isn't
9556 usable in a constant expression. */
9558 static inline bool
9559 neglectable_inst_p (tree d)
9561 return (DECL_P (d)
9562 && !undeduced_auto_decl (d)
9563 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9564 : decl_maybe_constant_var_p (d)));
9567 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9568 neglectable and instantiated from within an erroneous instantiation. */
9570 static bool
9571 limit_bad_template_recursion (tree decl)
9573 struct tinst_level *lev = current_tinst_level;
9574 int errs = errorcount + sorrycount;
9575 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9576 return false;
9578 for (; lev; lev = lev->next)
9579 if (neglectable_inst_p (lev->decl))
9580 break;
9582 return (lev && errs > lev->errors);
9585 static int tinst_depth;
9586 extern int max_tinst_depth;
9587 int depth_reached;
9589 static GTY(()) struct tinst_level *last_error_tinst_level;
9591 /* We're starting to instantiate D; record the template instantiation context
9592 for diagnostics and to restore it later. */
9594 bool
9595 push_tinst_level (tree d)
9597 return push_tinst_level_loc (d, input_location);
9600 /* We're starting to instantiate D; record the template instantiation context
9601 at LOC for diagnostics and to restore it later. */
9603 bool
9604 push_tinst_level_loc (tree d, location_t loc)
9606 struct tinst_level *new_level;
9608 if (tinst_depth >= max_tinst_depth)
9610 /* Tell error.c not to try to instantiate any templates. */
9611 at_eof = 2;
9612 fatal_error (input_location,
9613 "template instantiation depth exceeds maximum of %d"
9614 " (use -ftemplate-depth= to increase the maximum)",
9615 max_tinst_depth);
9616 return false;
9619 /* If the current instantiation caused problems, don't let it instantiate
9620 anything else. Do allow deduction substitution and decls usable in
9621 constant expressions. */
9622 if (limit_bad_template_recursion (d))
9623 return false;
9625 /* When not -quiet, dump template instantiations other than functions, since
9626 announce_function will take care of those. */
9627 if (!quiet_flag
9628 && TREE_CODE (d) != TREE_LIST
9629 && TREE_CODE (d) != FUNCTION_DECL)
9630 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9632 new_level = ggc_alloc<tinst_level> ();
9633 new_level->decl = d;
9634 new_level->locus = loc;
9635 new_level->errors = errorcount+sorrycount;
9636 new_level->in_system_header_p = in_system_header_at (input_location);
9637 new_level->next = current_tinst_level;
9638 current_tinst_level = new_level;
9640 ++tinst_depth;
9641 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9642 depth_reached = tinst_depth;
9644 return true;
9647 /* We're done instantiating this template; return to the instantiation
9648 context. */
9650 void
9651 pop_tinst_level (void)
9653 /* Restore the filename and line number stashed away when we started
9654 this instantiation. */
9655 input_location = current_tinst_level->locus;
9656 current_tinst_level = current_tinst_level->next;
9657 --tinst_depth;
9660 /* We're instantiating a deferred template; restore the template
9661 instantiation context in which the instantiation was requested, which
9662 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9664 static tree
9665 reopen_tinst_level (struct tinst_level *level)
9667 struct tinst_level *t;
9669 tinst_depth = 0;
9670 for (t = level; t; t = t->next)
9671 ++tinst_depth;
9673 current_tinst_level = level;
9674 pop_tinst_level ();
9675 if (current_tinst_level)
9676 current_tinst_level->errors = errorcount+sorrycount;
9677 return level->decl;
9680 /* Returns the TINST_LEVEL which gives the original instantiation
9681 context. */
9683 struct tinst_level *
9684 outermost_tinst_level (void)
9686 struct tinst_level *level = current_tinst_level;
9687 if (level)
9688 while (level->next)
9689 level = level->next;
9690 return level;
9693 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9694 vector of template arguments, as for tsubst.
9696 Returns an appropriate tsubst'd friend declaration. */
9698 static tree
9699 tsubst_friend_function (tree decl, tree args)
9701 tree new_friend;
9703 if (TREE_CODE (decl) == FUNCTION_DECL
9704 && DECL_TEMPLATE_INSTANTIATION (decl)
9705 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9706 /* This was a friend declared with an explicit template
9707 argument list, e.g.:
9709 friend void f<>(T);
9711 to indicate that f was a template instantiation, not a new
9712 function declaration. Now, we have to figure out what
9713 instantiation of what template. */
9715 tree template_id, arglist, fns;
9716 tree new_args;
9717 tree tmpl;
9718 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9720 /* Friend functions are looked up in the containing namespace scope.
9721 We must enter that scope, to avoid finding member functions of the
9722 current class with same name. */
9723 push_nested_namespace (ns);
9724 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9725 tf_warning_or_error, NULL_TREE,
9726 /*integral_constant_expression_p=*/false);
9727 pop_nested_namespace (ns);
9728 arglist = tsubst (DECL_TI_ARGS (decl), args,
9729 tf_warning_or_error, NULL_TREE);
9730 template_id = lookup_template_function (fns, arglist);
9732 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9733 tmpl = determine_specialization (template_id, new_friend,
9734 &new_args,
9735 /*need_member_template=*/0,
9736 TREE_VEC_LENGTH (args),
9737 tsk_none);
9738 return instantiate_template (tmpl, new_args, tf_error);
9741 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9743 /* The NEW_FRIEND will look like an instantiation, to the
9744 compiler, but is not an instantiation from the point of view of
9745 the language. For example, we might have had:
9747 template <class T> struct S {
9748 template <class U> friend void f(T, U);
9751 Then, in S<int>, template <class U> void f(int, U) is not an
9752 instantiation of anything. */
9753 if (new_friend == error_mark_node)
9754 return error_mark_node;
9756 DECL_USE_TEMPLATE (new_friend) = 0;
9757 if (TREE_CODE (decl) == TEMPLATE_DECL)
9759 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9760 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9761 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9764 /* The mangled name for the NEW_FRIEND is incorrect. The function
9765 is not a template instantiation and should not be mangled like
9766 one. Therefore, we forget the mangling here; we'll recompute it
9767 later if we need it. */
9768 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9770 SET_DECL_RTL (new_friend, NULL);
9771 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9774 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9776 tree old_decl;
9777 tree new_friend_template_info;
9778 tree new_friend_result_template_info;
9779 tree ns;
9780 int new_friend_is_defn;
9782 /* We must save some information from NEW_FRIEND before calling
9783 duplicate decls since that function will free NEW_FRIEND if
9784 possible. */
9785 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9786 new_friend_is_defn =
9787 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9788 (template_for_substitution (new_friend)))
9789 != NULL_TREE);
9790 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9792 /* This declaration is a `primary' template. */
9793 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9795 new_friend_result_template_info
9796 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9798 else
9799 new_friend_result_template_info = NULL_TREE;
9801 /* Inside pushdecl_namespace_level, we will push into the
9802 current namespace. However, the friend function should go
9803 into the namespace of the template. */
9804 ns = decl_namespace_context (new_friend);
9805 push_nested_namespace (ns);
9806 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9807 pop_nested_namespace (ns);
9809 if (old_decl == error_mark_node)
9810 return error_mark_node;
9812 if (old_decl != new_friend)
9814 /* This new friend declaration matched an existing
9815 declaration. For example, given:
9817 template <class T> void f(T);
9818 template <class U> class C {
9819 template <class T> friend void f(T) {}
9822 the friend declaration actually provides the definition
9823 of `f', once C has been instantiated for some type. So,
9824 old_decl will be the out-of-class template declaration,
9825 while new_friend is the in-class definition.
9827 But, if `f' was called before this point, the
9828 instantiation of `f' will have DECL_TI_ARGS corresponding
9829 to `T' but not to `U', references to which might appear
9830 in the definition of `f'. Previously, the most general
9831 template for an instantiation of `f' was the out-of-class
9832 version; now it is the in-class version. Therefore, we
9833 run through all specialization of `f', adding to their
9834 DECL_TI_ARGS appropriately. In particular, they need a
9835 new set of outer arguments, corresponding to the
9836 arguments for this class instantiation.
9838 The same situation can arise with something like this:
9840 friend void f(int);
9841 template <class T> class C {
9842 friend void f(T) {}
9845 when `C<int>' is instantiated. Now, `f(int)' is defined
9846 in the class. */
9848 if (!new_friend_is_defn)
9849 /* On the other hand, if the in-class declaration does
9850 *not* provide a definition, then we don't want to alter
9851 existing definitions. We can just leave everything
9852 alone. */
9854 else
9856 tree new_template = TI_TEMPLATE (new_friend_template_info);
9857 tree new_args = TI_ARGS (new_friend_template_info);
9859 /* Overwrite whatever template info was there before, if
9860 any, with the new template information pertaining to
9861 the declaration. */
9862 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9864 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9866 /* We should have called reregister_specialization in
9867 duplicate_decls. */
9868 gcc_assert (retrieve_specialization (new_template,
9869 new_args, 0)
9870 == old_decl);
9872 /* Instantiate it if the global has already been used. */
9873 if (DECL_ODR_USED (old_decl))
9874 instantiate_decl (old_decl, /*defer_ok=*/true,
9875 /*expl_inst_class_mem_p=*/false);
9877 else
9879 tree t;
9881 /* Indicate that the old function template is a partial
9882 instantiation. */
9883 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9884 = new_friend_result_template_info;
9886 gcc_assert (new_template
9887 == most_general_template (new_template));
9888 gcc_assert (new_template != old_decl);
9890 /* Reassign any specializations already in the hash table
9891 to the new more general template, and add the
9892 additional template args. */
9893 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9894 t != NULL_TREE;
9895 t = TREE_CHAIN (t))
9897 tree spec = TREE_VALUE (t);
9898 spec_entry elt;
9900 elt.tmpl = old_decl;
9901 elt.args = DECL_TI_ARGS (spec);
9902 elt.spec = NULL_TREE;
9904 decl_specializations->remove_elt (&elt);
9906 DECL_TI_ARGS (spec)
9907 = add_outermost_template_args (new_args,
9908 DECL_TI_ARGS (spec));
9910 register_specialization
9911 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9914 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9918 /* The information from NEW_FRIEND has been merged into OLD_DECL
9919 by duplicate_decls. */
9920 new_friend = old_decl;
9923 else
9925 tree context = DECL_CONTEXT (new_friend);
9926 bool dependent_p;
9928 /* In the code
9929 template <class T> class C {
9930 template <class U> friend void C1<U>::f (); // case 1
9931 friend void C2<T>::f (); // case 2
9933 we only need to make sure CONTEXT is a complete type for
9934 case 2. To distinguish between the two cases, we note that
9935 CONTEXT of case 1 remains dependent type after tsubst while
9936 this isn't true for case 2. */
9937 ++processing_template_decl;
9938 dependent_p = dependent_type_p (context);
9939 --processing_template_decl;
9941 if (!dependent_p
9942 && !complete_type_or_else (context, NULL_TREE))
9943 return error_mark_node;
9945 if (COMPLETE_TYPE_P (context))
9947 tree fn = new_friend;
9948 /* do_friend adds the TEMPLATE_DECL for any member friend
9949 template even if it isn't a member template, i.e.
9950 template <class T> friend A<T>::f();
9951 Look through it in that case. */
9952 if (TREE_CODE (fn) == TEMPLATE_DECL
9953 && !PRIMARY_TEMPLATE_P (fn))
9954 fn = DECL_TEMPLATE_RESULT (fn);
9955 /* Check to see that the declaration is really present, and,
9956 possibly obtain an improved declaration. */
9957 fn = check_classfn (context, fn, NULL_TREE);
9959 if (fn)
9960 new_friend = fn;
9964 return new_friend;
9967 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9968 template arguments, as for tsubst.
9970 Returns an appropriate tsubst'd friend type or error_mark_node on
9971 failure. */
9973 static tree
9974 tsubst_friend_class (tree friend_tmpl, tree args)
9976 tree friend_type;
9977 tree tmpl;
9978 tree context;
9980 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9982 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9983 return TREE_TYPE (t);
9986 context = CP_DECL_CONTEXT (friend_tmpl);
9988 if (context != global_namespace)
9990 if (TREE_CODE (context) == NAMESPACE_DECL)
9991 push_nested_namespace (context);
9992 else
9993 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9996 /* Look for a class template declaration. We look for hidden names
9997 because two friend declarations of the same template are the
9998 same. For example, in:
10000 struct A {
10001 template <typename> friend class F;
10003 template <typename> struct B {
10004 template <typename> friend class F;
10007 both F templates are the same. */
10008 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10009 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10011 /* But, if we don't find one, it might be because we're in a
10012 situation like this:
10014 template <class T>
10015 struct S {
10016 template <class U>
10017 friend struct S;
10020 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10021 for `S<int>', not the TEMPLATE_DECL. */
10022 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10024 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10025 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10028 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10030 /* The friend template has already been declared. Just
10031 check to see that the declarations match, and install any new
10032 default parameters. We must tsubst the default parameters,
10033 of course. We only need the innermost template parameters
10034 because that is all that redeclare_class_template will look
10035 at. */
10036 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10037 > TMPL_ARGS_DEPTH (args))
10039 tree parms;
10040 location_t saved_input_location;
10041 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10042 args, tf_warning_or_error);
10044 saved_input_location = input_location;
10045 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10046 tree cons = get_constraints (tmpl);
10047 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10048 input_location = saved_input_location;
10052 friend_type = TREE_TYPE (tmpl);
10054 else
10056 /* The friend template has not already been declared. In this
10057 case, the instantiation of the template class will cause the
10058 injection of this template into the global scope. */
10059 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10060 if (tmpl == error_mark_node)
10061 return error_mark_node;
10063 /* The new TMPL is not an instantiation of anything, so we
10064 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10065 the new type because that is supposed to be the corresponding
10066 template decl, i.e., TMPL. */
10067 DECL_USE_TEMPLATE (tmpl) = 0;
10068 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10069 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10070 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10071 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10073 /* Inject this template into the global scope. */
10074 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10077 if (context != global_namespace)
10079 if (TREE_CODE (context) == NAMESPACE_DECL)
10080 pop_nested_namespace (context);
10081 else
10082 pop_nested_class ();
10085 return friend_type;
10088 /* Returns zero if TYPE cannot be completed later due to circularity.
10089 Otherwise returns one. */
10091 static int
10092 can_complete_type_without_circularity (tree type)
10094 if (type == NULL_TREE || type == error_mark_node)
10095 return 0;
10096 else if (COMPLETE_TYPE_P (type))
10097 return 1;
10098 else if (TREE_CODE (type) == ARRAY_TYPE)
10099 return can_complete_type_without_circularity (TREE_TYPE (type));
10100 else if (CLASS_TYPE_P (type)
10101 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10102 return 0;
10103 else
10104 return 1;
10107 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10108 tsubst_flags_t, tree);
10110 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10111 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10113 static tree
10114 tsubst_attribute (tree t, tree *decl_p, tree args,
10115 tsubst_flags_t complain, tree in_decl)
10117 gcc_assert (ATTR_IS_DEPENDENT (t));
10119 tree val = TREE_VALUE (t);
10120 if (val == NULL_TREE)
10121 /* Nothing to do. */;
10122 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10123 && is_attribute_p ("omp declare simd",
10124 get_attribute_name (t)))
10126 tree clauses = TREE_VALUE (val);
10127 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10128 complain, in_decl);
10129 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10130 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10131 tree parms = DECL_ARGUMENTS (*decl_p);
10132 clauses
10133 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10134 if (clauses)
10135 val = build_tree_list (NULL_TREE, clauses);
10136 else
10137 val = NULL_TREE;
10139 /* If the first attribute argument is an identifier, don't
10140 pass it through tsubst. Attributes like mode, format,
10141 cleanup and several target specific attributes expect it
10142 unmodified. */
10143 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10145 tree chain
10146 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10147 /*integral_constant_expression_p=*/false);
10148 if (chain != TREE_CHAIN (val))
10149 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10151 else if (PACK_EXPANSION_P (val))
10153 /* An attribute pack expansion. */
10154 tree purp = TREE_PURPOSE (t);
10155 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10156 if (pack == error_mark_node)
10157 return error_mark_node;
10158 int len = TREE_VEC_LENGTH (pack);
10159 tree list = NULL_TREE;
10160 tree *q = &list;
10161 for (int i = 0; i < len; ++i)
10163 tree elt = TREE_VEC_ELT (pack, i);
10164 *q = build_tree_list (purp, elt);
10165 q = &TREE_CHAIN (*q);
10167 return list;
10169 else
10170 val = tsubst_expr (val, args, complain, in_decl,
10171 /*integral_constant_expression_p=*/false);
10173 if (val != TREE_VALUE (t))
10174 return build_tree_list (TREE_PURPOSE (t), val);
10175 return t;
10178 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10179 unchanged or a new TREE_LIST chain. */
10181 static tree
10182 tsubst_attributes (tree attributes, tree args,
10183 tsubst_flags_t complain, tree in_decl)
10185 tree last_dep = NULL_TREE;
10187 for (tree t = attributes; t; t = TREE_CHAIN (t))
10188 if (ATTR_IS_DEPENDENT (t))
10190 last_dep = t;
10191 attributes = copy_list (attributes);
10192 break;
10195 if (last_dep)
10196 for (tree *p = &attributes; *p; )
10198 tree t = *p;
10199 if (ATTR_IS_DEPENDENT (t))
10201 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10202 if (subst != t)
10204 *p = subst;
10206 p = &TREE_CHAIN (*p);
10207 while (*p);
10208 *p = TREE_CHAIN (t);
10209 continue;
10212 p = &TREE_CHAIN (*p);
10215 return attributes;
10218 /* Apply any attributes which had to be deferred until instantiation
10219 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10220 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10222 static void
10223 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10224 tree args, tsubst_flags_t complain, tree in_decl)
10226 tree last_dep = NULL_TREE;
10227 tree t;
10228 tree *p;
10230 if (attributes == NULL_TREE)
10231 return;
10233 if (DECL_P (*decl_p))
10235 if (TREE_TYPE (*decl_p) == error_mark_node)
10236 return;
10237 p = &DECL_ATTRIBUTES (*decl_p);
10238 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10239 to our attributes parameter. */
10240 gcc_assert (*p == attributes);
10242 else
10244 p = &TYPE_ATTRIBUTES (*decl_p);
10245 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10246 lookup_template_class_1, and should be preserved. */
10247 gcc_assert (*p != attributes);
10248 while (*p)
10249 p = &TREE_CHAIN (*p);
10252 for (t = attributes; t; t = TREE_CHAIN (t))
10253 if (ATTR_IS_DEPENDENT (t))
10255 last_dep = t;
10256 attributes = copy_list (attributes);
10257 break;
10260 *p = attributes;
10261 if (last_dep)
10263 tree late_attrs = NULL_TREE;
10264 tree *q = &late_attrs;
10266 for (; *p; )
10268 t = *p;
10269 if (ATTR_IS_DEPENDENT (t))
10271 *p = TREE_CHAIN (t);
10272 TREE_CHAIN (t) = NULL_TREE;
10273 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10275 q = &TREE_CHAIN (*q);
10276 while (*q);
10278 else
10279 p = &TREE_CHAIN (t);
10282 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10286 /* Perform (or defer) access check for typedefs that were referenced
10287 from within the template TMPL code.
10288 This is a subroutine of instantiate_decl and instantiate_class_template.
10289 TMPL is the template to consider and TARGS is the list of arguments of
10290 that template. */
10292 static void
10293 perform_typedefs_access_check (tree tmpl, tree targs)
10295 location_t saved_location;
10296 unsigned i;
10297 qualified_typedef_usage_t *iter;
10299 if (!tmpl
10300 || (!CLASS_TYPE_P (tmpl)
10301 && TREE_CODE (tmpl) != FUNCTION_DECL))
10302 return;
10304 saved_location = input_location;
10305 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10307 tree type_decl = iter->typedef_decl;
10308 tree type_scope = iter->context;
10310 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10311 continue;
10313 if (uses_template_parms (type_decl))
10314 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10315 if (uses_template_parms (type_scope))
10316 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10318 /* Make access check error messages point to the location
10319 of the use of the typedef. */
10320 input_location = iter->locus;
10321 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10322 type_decl, type_decl,
10323 tf_warning_or_error);
10325 input_location = saved_location;
10328 static tree
10329 instantiate_class_template_1 (tree type)
10331 tree templ, args, pattern, t, member;
10332 tree typedecl;
10333 tree pbinfo;
10334 tree base_list;
10335 unsigned int saved_maximum_field_alignment;
10336 tree fn_context;
10338 if (type == error_mark_node)
10339 return error_mark_node;
10341 if (COMPLETE_OR_OPEN_TYPE_P (type)
10342 || uses_template_parms (type))
10343 return type;
10345 /* Figure out which template is being instantiated. */
10346 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10347 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10349 /* Determine what specialization of the original template to
10350 instantiate. */
10351 t = most_specialized_partial_spec (type, tf_warning_or_error);
10352 if (t == error_mark_node)
10354 TYPE_BEING_DEFINED (type) = 1;
10355 return error_mark_node;
10357 else if (t)
10359 /* This TYPE is actually an instantiation of a partial
10360 specialization. We replace the innermost set of ARGS with
10361 the arguments appropriate for substitution. For example,
10362 given:
10364 template <class T> struct S {};
10365 template <class T> struct S<T*> {};
10367 and supposing that we are instantiating S<int*>, ARGS will
10368 presently be {int*} -- but we need {int}. */
10369 pattern = TREE_TYPE (t);
10370 args = TREE_PURPOSE (t);
10372 else
10374 pattern = TREE_TYPE (templ);
10375 args = CLASSTYPE_TI_ARGS (type);
10378 /* If the template we're instantiating is incomplete, then clearly
10379 there's nothing we can do. */
10380 if (!COMPLETE_TYPE_P (pattern))
10381 return type;
10383 /* If we've recursively instantiated too many templates, stop. */
10384 if (! push_tinst_level (type))
10385 return type;
10387 /* Now we're really doing the instantiation. Mark the type as in
10388 the process of being defined. */
10389 TYPE_BEING_DEFINED (type) = 1;
10391 /* We may be in the middle of deferred access check. Disable
10392 it now. */
10393 push_deferring_access_checks (dk_no_deferred);
10395 int saved_unevaluated_operand = cp_unevaluated_operand;
10396 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10398 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10399 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10400 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10401 fn_context = error_mark_node;
10402 if (!fn_context)
10403 push_to_top_level ();
10404 else
10406 cp_unevaluated_operand = 0;
10407 c_inhibit_evaluation_warnings = 0;
10409 /* Use #pragma pack from the template context. */
10410 saved_maximum_field_alignment = maximum_field_alignment;
10411 maximum_field_alignment = TYPE_PRECISION (pattern);
10413 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10415 /* Set the input location to the most specialized template definition.
10416 This is needed if tsubsting causes an error. */
10417 typedecl = TYPE_MAIN_DECL (pattern);
10418 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10419 DECL_SOURCE_LOCATION (typedecl);
10421 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10422 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10423 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10424 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10425 if (ANON_AGGR_TYPE_P (pattern))
10426 SET_ANON_AGGR_TYPE_P (type);
10427 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10429 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10430 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10431 /* Adjust visibility for template arguments. */
10432 determine_visibility (TYPE_MAIN_DECL (type));
10434 if (CLASS_TYPE_P (type))
10435 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10437 pbinfo = TYPE_BINFO (pattern);
10439 /* We should never instantiate a nested class before its enclosing
10440 class; we need to look up the nested class by name before we can
10441 instantiate it, and that lookup should instantiate the enclosing
10442 class. */
10443 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10444 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10446 base_list = NULL_TREE;
10447 if (BINFO_N_BASE_BINFOS (pbinfo))
10449 tree pbase_binfo;
10450 tree pushed_scope;
10451 int i;
10453 /* We must enter the scope containing the type, as that is where
10454 the accessibility of types named in dependent bases are
10455 looked up from. */
10456 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10458 /* Substitute into each of the bases to determine the actual
10459 basetypes. */
10460 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10462 tree base;
10463 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10464 tree expanded_bases = NULL_TREE;
10465 int idx, len = 1;
10467 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10469 expanded_bases =
10470 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10471 args, tf_error, NULL_TREE);
10472 if (expanded_bases == error_mark_node)
10473 continue;
10475 len = TREE_VEC_LENGTH (expanded_bases);
10478 for (idx = 0; idx < len; idx++)
10480 if (expanded_bases)
10481 /* Extract the already-expanded base class. */
10482 base = TREE_VEC_ELT (expanded_bases, idx);
10483 else
10484 /* Substitute to figure out the base class. */
10485 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10486 NULL_TREE);
10488 if (base == error_mark_node)
10489 continue;
10491 base_list = tree_cons (access, base, base_list);
10492 if (BINFO_VIRTUAL_P (pbase_binfo))
10493 TREE_TYPE (base_list) = integer_type_node;
10497 /* The list is now in reverse order; correct that. */
10498 base_list = nreverse (base_list);
10500 if (pushed_scope)
10501 pop_scope (pushed_scope);
10503 /* Now call xref_basetypes to set up all the base-class
10504 information. */
10505 xref_basetypes (type, base_list);
10507 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10508 (int) ATTR_FLAG_TYPE_IN_PLACE,
10509 args, tf_error, NULL_TREE);
10510 fixup_attribute_variants (type);
10512 /* Now that our base classes are set up, enter the scope of the
10513 class, so that name lookups into base classes, etc. will work
10514 correctly. This is precisely analogous to what we do in
10515 begin_class_definition when defining an ordinary non-template
10516 class, except we also need to push the enclosing classes. */
10517 push_nested_class (type);
10519 /* Now members are processed in the order of declaration. */
10520 for (member = CLASSTYPE_DECL_LIST (pattern);
10521 member; member = TREE_CHAIN (member))
10523 tree t = TREE_VALUE (member);
10525 if (TREE_PURPOSE (member))
10527 if (TYPE_P (t))
10529 /* Build new CLASSTYPE_NESTED_UTDS. */
10531 tree newtag;
10532 bool class_template_p;
10534 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10535 && TYPE_LANG_SPECIFIC (t)
10536 && CLASSTYPE_IS_TEMPLATE (t));
10537 /* If the member is a class template, then -- even after
10538 substitution -- there may be dependent types in the
10539 template argument list for the class. We increment
10540 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10541 that function will assume that no types are dependent
10542 when outside of a template. */
10543 if (class_template_p)
10544 ++processing_template_decl;
10545 newtag = tsubst (t, args, tf_error, NULL_TREE);
10546 if (class_template_p)
10547 --processing_template_decl;
10548 if (newtag == error_mark_node)
10549 continue;
10551 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10553 tree name = TYPE_IDENTIFIER (t);
10555 if (class_template_p)
10556 /* Unfortunately, lookup_template_class sets
10557 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10558 instantiation (i.e., for the type of a member
10559 template class nested within a template class.)
10560 This behavior is required for
10561 maybe_process_partial_specialization to work
10562 correctly, but is not accurate in this case;
10563 the TAG is not an instantiation of anything.
10564 (The corresponding TEMPLATE_DECL is an
10565 instantiation, but the TYPE is not.) */
10566 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10568 /* Now, we call pushtag to put this NEWTAG into the scope of
10569 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10570 pushtag calling push_template_decl. We don't have to do
10571 this for enums because it will already have been done in
10572 tsubst_enum. */
10573 if (name)
10574 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10575 pushtag (name, newtag, /*tag_scope=*/ts_current);
10578 else if (DECL_DECLARES_FUNCTION_P (t))
10580 tree r;
10582 if (TREE_CODE (t) == TEMPLATE_DECL)
10583 ++processing_template_decl;
10584 r = tsubst (t, args, tf_error, NULL_TREE);
10585 if (TREE_CODE (t) == TEMPLATE_DECL)
10586 --processing_template_decl;
10587 set_current_access_from_decl (r);
10588 finish_member_declaration (r);
10589 /* Instantiate members marked with attribute used. */
10590 if (r != error_mark_node && DECL_PRESERVE_P (r))
10591 mark_used (r);
10592 if (TREE_CODE (r) == FUNCTION_DECL
10593 && DECL_OMP_DECLARE_REDUCTION_P (r))
10594 cp_check_omp_declare_reduction (r);
10596 else if (DECL_CLASS_TEMPLATE_P (t)
10597 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10598 /* A closure type for a lambda in a default argument for a
10599 member template. Ignore it; it will be instantiated with
10600 the default argument. */;
10601 else
10603 /* Build new TYPE_FIELDS. */
10604 if (TREE_CODE (t) == STATIC_ASSERT)
10606 tree condition;
10608 ++c_inhibit_evaluation_warnings;
10609 condition =
10610 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10611 tf_warning_or_error, NULL_TREE,
10612 /*integral_constant_expression_p=*/true);
10613 --c_inhibit_evaluation_warnings;
10615 finish_static_assert (condition,
10616 STATIC_ASSERT_MESSAGE (t),
10617 STATIC_ASSERT_SOURCE_LOCATION (t),
10618 /*member_p=*/true);
10620 else if (TREE_CODE (t) != CONST_DECL)
10622 tree r;
10623 tree vec = NULL_TREE;
10624 int len = 1;
10626 /* The file and line for this declaration, to
10627 assist in error message reporting. Since we
10628 called push_tinst_level above, we don't need to
10629 restore these. */
10630 input_location = DECL_SOURCE_LOCATION (t);
10632 if (TREE_CODE (t) == TEMPLATE_DECL)
10633 ++processing_template_decl;
10634 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10635 if (TREE_CODE (t) == TEMPLATE_DECL)
10636 --processing_template_decl;
10638 if (TREE_CODE (r) == TREE_VEC)
10640 /* A capture pack became multiple fields. */
10641 vec = r;
10642 len = TREE_VEC_LENGTH (vec);
10645 for (int i = 0; i < len; ++i)
10647 if (vec)
10648 r = TREE_VEC_ELT (vec, i);
10649 if (VAR_P (r))
10651 /* In [temp.inst]:
10653 [t]he initialization (and any associated
10654 side-effects) of a static data member does
10655 not occur unless the static data member is
10656 itself used in a way that requires the
10657 definition of the static data member to
10658 exist.
10660 Therefore, we do not substitute into the
10661 initialized for the static data member here. */
10662 finish_static_data_member_decl
10664 /*init=*/NULL_TREE,
10665 /*init_const_expr_p=*/false,
10666 /*asmspec_tree=*/NULL_TREE,
10667 /*flags=*/0);
10668 /* Instantiate members marked with attribute used. */
10669 if (r != error_mark_node && DECL_PRESERVE_P (r))
10670 mark_used (r);
10672 else if (TREE_CODE (r) == FIELD_DECL)
10674 /* Determine whether R has a valid type and can be
10675 completed later. If R is invalid, then its type
10676 is replaced by error_mark_node. */
10677 tree rtype = TREE_TYPE (r);
10678 if (can_complete_type_without_circularity (rtype))
10679 complete_type (rtype);
10681 if (!complete_or_array_type_p (rtype))
10683 /* If R's type couldn't be completed and
10684 it isn't a flexible array member (whose
10685 type is incomplete by definition) give
10686 an error. */
10687 cxx_incomplete_type_error (r, rtype);
10688 TREE_TYPE (r) = error_mark_node;
10692 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10693 such a thing will already have been added to the field
10694 list by tsubst_enum in finish_member_declaration in the
10695 CLASSTYPE_NESTED_UTDS case above. */
10696 if (!(TREE_CODE (r) == TYPE_DECL
10697 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10698 && DECL_ARTIFICIAL (r)))
10700 set_current_access_from_decl (r);
10701 finish_member_declaration (r);
10707 else
10709 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10710 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10712 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10714 tree friend_type = t;
10715 bool adjust_processing_template_decl = false;
10717 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10719 /* template <class T> friend class C; */
10720 friend_type = tsubst_friend_class (friend_type, args);
10721 adjust_processing_template_decl = true;
10723 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10725 /* template <class T> friend class C::D; */
10726 friend_type = tsubst (friend_type, args,
10727 tf_warning_or_error, NULL_TREE);
10728 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10729 friend_type = TREE_TYPE (friend_type);
10730 adjust_processing_template_decl = true;
10732 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10733 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10735 /* This could be either
10737 friend class T::C;
10739 when dependent_type_p is false or
10741 template <class U> friend class T::C;
10743 otherwise. */
10744 /* Bump processing_template_decl in case this is something like
10745 template <class T> friend struct A<T>::B. */
10746 ++processing_template_decl;
10747 friend_type = tsubst (friend_type, args,
10748 tf_warning_or_error, NULL_TREE);
10749 if (dependent_type_p (friend_type))
10750 adjust_processing_template_decl = true;
10751 --processing_template_decl;
10753 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10754 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10755 && TYPE_HIDDEN_P (friend_type))
10757 /* friend class C;
10759 where C hasn't been declared yet. Let's lookup name
10760 from namespace scope directly, bypassing any name that
10761 come from dependent base class. */
10762 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10764 /* The call to xref_tag_from_type does injection for friend
10765 classes. */
10766 push_nested_namespace (ns);
10767 friend_type =
10768 xref_tag_from_type (friend_type, NULL_TREE,
10769 /*tag_scope=*/ts_current);
10770 pop_nested_namespace (ns);
10772 else if (uses_template_parms (friend_type))
10773 /* friend class C<T>; */
10774 friend_type = tsubst (friend_type, args,
10775 tf_warning_or_error, NULL_TREE);
10776 /* Otherwise it's
10778 friend class C;
10780 where C is already declared or
10782 friend class C<int>;
10784 We don't have to do anything in these cases. */
10786 if (adjust_processing_template_decl)
10787 /* Trick make_friend_class into realizing that the friend
10788 we're adding is a template, not an ordinary class. It's
10789 important that we use make_friend_class since it will
10790 perform some error-checking and output cross-reference
10791 information. */
10792 ++processing_template_decl;
10794 if (friend_type != error_mark_node)
10795 make_friend_class (type, friend_type, /*complain=*/false);
10797 if (adjust_processing_template_decl)
10798 --processing_template_decl;
10800 else
10802 /* Build new DECL_FRIENDLIST. */
10803 tree r;
10805 /* The file and line for this declaration, to
10806 assist in error message reporting. Since we
10807 called push_tinst_level above, we don't need to
10808 restore these. */
10809 input_location = DECL_SOURCE_LOCATION (t);
10811 if (TREE_CODE (t) == TEMPLATE_DECL)
10813 ++processing_template_decl;
10814 push_deferring_access_checks (dk_no_check);
10817 r = tsubst_friend_function (t, args);
10818 add_friend (type, r, /*complain=*/false);
10819 if (TREE_CODE (t) == TEMPLATE_DECL)
10821 pop_deferring_access_checks ();
10822 --processing_template_decl;
10828 if (fn_context)
10830 /* Restore these before substituting into the lambda capture
10831 initializers. */
10832 cp_unevaluated_operand = saved_unevaluated_operand;
10833 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10836 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10838 tree decl = lambda_function (type);
10839 if (decl)
10841 if (cxx_dialect >= cxx1z)
10842 CLASSTYPE_LITERAL_P (type) = true;
10844 if (!DECL_TEMPLATE_INFO (decl)
10845 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10847 /* Set function_depth to avoid garbage collection. */
10848 ++function_depth;
10849 instantiate_decl (decl, /*defer_ok=*/false, false);
10850 --function_depth;
10853 /* We need to instantiate the capture list from the template
10854 after we've instantiated the closure members, but before we
10855 consider adding the conversion op. Also keep any captures
10856 that may have been added during instantiation of the op(). */
10857 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10858 tree tmpl_cap
10859 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10860 args, tf_warning_or_error, NULL_TREE,
10861 false, false);
10863 LAMBDA_EXPR_CAPTURE_LIST (expr)
10864 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10866 maybe_add_lambda_conv_op (type);
10868 else
10869 gcc_assert (errorcount);
10872 /* Set the file and line number information to whatever is given for
10873 the class itself. This puts error messages involving generated
10874 implicit functions at a predictable point, and the same point
10875 that would be used for non-template classes. */
10876 input_location = DECL_SOURCE_LOCATION (typedecl);
10878 unreverse_member_declarations (type);
10879 finish_struct_1 (type);
10880 TYPE_BEING_DEFINED (type) = 0;
10882 /* We don't instantiate default arguments for member functions. 14.7.1:
10884 The implicit instantiation of a class template specialization causes
10885 the implicit instantiation of the declarations, but not of the
10886 definitions or default arguments, of the class member functions,
10887 member classes, static data members and member templates.... */
10889 /* Some typedefs referenced from within the template code need to be access
10890 checked at template instantiation time, i.e now. These types were
10891 added to the template at parsing time. Let's get those and perform
10892 the access checks then. */
10893 perform_typedefs_access_check (pattern, args);
10894 perform_deferred_access_checks (tf_warning_or_error);
10895 pop_nested_class ();
10896 maximum_field_alignment = saved_maximum_field_alignment;
10897 if (!fn_context)
10898 pop_from_top_level ();
10899 pop_deferring_access_checks ();
10900 pop_tinst_level ();
10902 /* The vtable for a template class can be emitted in any translation
10903 unit in which the class is instantiated. When there is no key
10904 method, however, finish_struct_1 will already have added TYPE to
10905 the keyed_classes. */
10906 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10907 vec_safe_push (keyed_classes, type);
10909 return type;
10912 /* Wrapper for instantiate_class_template_1. */
10914 tree
10915 instantiate_class_template (tree type)
10917 tree ret;
10918 timevar_push (TV_TEMPLATE_INST);
10919 ret = instantiate_class_template_1 (type);
10920 timevar_pop (TV_TEMPLATE_INST);
10921 return ret;
10924 static tree
10925 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10927 tree r;
10929 if (!t)
10930 r = t;
10931 else if (TYPE_P (t))
10932 r = tsubst (t, args, complain, in_decl);
10933 else
10935 if (!(complain & tf_warning))
10936 ++c_inhibit_evaluation_warnings;
10937 r = tsubst_expr (t, args, complain, in_decl,
10938 /*integral_constant_expression_p=*/true);
10939 if (!(complain & tf_warning))
10940 --c_inhibit_evaluation_warnings;
10942 return r;
10945 /* Given a function parameter pack TMPL_PARM and some function parameters
10946 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10947 and set *SPEC_P to point at the next point in the list. */
10949 tree
10950 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10952 /* Collect all of the extra "packed" parameters into an
10953 argument pack. */
10954 tree parmvec;
10955 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10956 tree spec_parm = *spec_p;
10957 int i, len;
10959 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10960 if (tmpl_parm
10961 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10962 break;
10964 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10965 parmvec = make_tree_vec (len);
10966 spec_parm = *spec_p;
10967 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10968 TREE_VEC_ELT (parmvec, i) = spec_parm;
10970 /* Build the argument packs. */
10971 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10972 *spec_p = spec_parm;
10974 return argpack;
10977 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10978 NONTYPE_ARGUMENT_PACK. */
10980 static tree
10981 make_fnparm_pack (tree spec_parm)
10983 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10986 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10987 pack expansion with no extra args, 2 if it has extra args, or 0
10988 if it is not a pack expansion. */
10990 static int
10991 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10993 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10994 if (i >= TREE_VEC_LENGTH (vec))
10995 return 0;
10996 tree elt = TREE_VEC_ELT (vec, i);
10997 if (DECL_P (elt))
10998 /* A decl pack is itself an expansion. */
10999 elt = TREE_TYPE (elt);
11000 if (!PACK_EXPANSION_P (elt))
11001 return 0;
11002 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11003 return 2;
11004 return 1;
11008 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11010 static tree
11011 make_argument_pack_select (tree arg_pack, unsigned index)
11013 tree aps = make_node (ARGUMENT_PACK_SELECT);
11015 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11016 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11018 return aps;
11021 /* This is a subroutine of tsubst_pack_expansion.
11023 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11024 mechanism to store the (non complete list of) arguments of the
11025 substitution and return a non substituted pack expansion, in order
11026 to wait for when we have enough arguments to really perform the
11027 substitution. */
11029 static bool
11030 use_pack_expansion_extra_args_p (tree parm_packs,
11031 int arg_pack_len,
11032 bool has_empty_arg)
11034 /* If one pack has an expansion and another pack has a normal
11035 argument or if one pack has an empty argument and an another
11036 one hasn't then tsubst_pack_expansion cannot perform the
11037 substitution and need to fall back on the
11038 PACK_EXPANSION_EXTRA mechanism. */
11039 if (parm_packs == NULL_TREE)
11040 return false;
11041 else if (has_empty_arg)
11042 return true;
11044 bool has_expansion_arg = false;
11045 for (int i = 0 ; i < arg_pack_len; ++i)
11047 bool has_non_expansion_arg = false;
11048 for (tree parm_pack = parm_packs;
11049 parm_pack;
11050 parm_pack = TREE_CHAIN (parm_pack))
11052 tree arg = TREE_VALUE (parm_pack);
11054 int exp = argument_pack_element_is_expansion_p (arg, i);
11055 if (exp == 2)
11056 /* We can't substitute a pack expansion with extra args into
11057 our pattern. */
11058 return true;
11059 else if (exp)
11060 has_expansion_arg = true;
11061 else
11062 has_non_expansion_arg = true;
11065 if (has_expansion_arg && has_non_expansion_arg)
11066 return true;
11068 return false;
11071 /* [temp.variadic]/6 says that:
11073 The instantiation of a pack expansion [...]
11074 produces a list E1,E2, ..., En, where N is the number of elements
11075 in the pack expansion parameters.
11077 This subroutine of tsubst_pack_expansion produces one of these Ei.
11079 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11080 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11081 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11082 INDEX is the index 'i' of the element Ei to produce. ARGS,
11083 COMPLAIN, and IN_DECL are the same parameters as for the
11084 tsubst_pack_expansion function.
11086 The function returns the resulting Ei upon successful completion,
11087 or error_mark_node.
11089 Note that this function possibly modifies the ARGS parameter, so
11090 it's the responsibility of the caller to restore it. */
11092 static tree
11093 gen_elem_of_pack_expansion_instantiation (tree pattern,
11094 tree parm_packs,
11095 unsigned index,
11096 tree args /* This parm gets
11097 modified. */,
11098 tsubst_flags_t complain,
11099 tree in_decl)
11101 tree t;
11102 bool ith_elem_is_expansion = false;
11104 /* For each parameter pack, change the substitution of the parameter
11105 pack to the ith argument in its argument pack, then expand the
11106 pattern. */
11107 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11109 tree parm = TREE_PURPOSE (pack);
11110 tree arg_pack = TREE_VALUE (pack);
11111 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11113 ith_elem_is_expansion |=
11114 argument_pack_element_is_expansion_p (arg_pack, index);
11116 /* Select the Ith argument from the pack. */
11117 if (TREE_CODE (parm) == PARM_DECL
11118 || TREE_CODE (parm) == FIELD_DECL)
11120 if (index == 0)
11122 aps = make_argument_pack_select (arg_pack, index);
11123 if (!mark_used (parm, complain) && !(complain & tf_error))
11124 return error_mark_node;
11125 register_local_specialization (aps, parm);
11127 else
11128 aps = retrieve_local_specialization (parm);
11130 else
11132 int idx, level;
11133 template_parm_level_and_index (parm, &level, &idx);
11135 if (index == 0)
11137 aps = make_argument_pack_select (arg_pack, index);
11138 /* Update the corresponding argument. */
11139 TMPL_ARG (args, level, idx) = aps;
11141 else
11142 /* Re-use the ARGUMENT_PACK_SELECT. */
11143 aps = TMPL_ARG (args, level, idx);
11145 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11148 /* Substitute into the PATTERN with the (possibly altered)
11149 arguments. */
11150 if (pattern == in_decl)
11151 /* Expanding a fixed parameter pack from
11152 coerce_template_parameter_pack. */
11153 t = tsubst_decl (pattern, args, complain);
11154 else if (pattern == error_mark_node)
11155 t = error_mark_node;
11156 else if (constraint_p (pattern))
11158 if (processing_template_decl)
11159 t = tsubst_constraint (pattern, args, complain, in_decl);
11160 else
11161 t = (constraints_satisfied_p (pattern, args)
11162 ? boolean_true_node : boolean_false_node);
11164 else if (!TYPE_P (pattern))
11165 t = tsubst_expr (pattern, args, complain, in_decl,
11166 /*integral_constant_expression_p=*/false);
11167 else
11168 t = tsubst (pattern, args, complain, in_decl);
11170 /* If the Ith argument pack element is a pack expansion, then
11171 the Ith element resulting from the substituting is going to
11172 be a pack expansion as well. */
11173 if (ith_elem_is_expansion)
11174 t = make_pack_expansion (t);
11176 return t;
11179 /* When the unexpanded parameter pack in a fold expression expands to an empty
11180 sequence, the value of the expression is as follows; the program is
11181 ill-formed if the operator is not listed in this table.
11183 && true
11184 || false
11185 , void() */
11187 tree
11188 expand_empty_fold (tree t, tsubst_flags_t complain)
11190 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11191 if (!FOLD_EXPR_MODIFY_P (t))
11192 switch (code)
11194 case TRUTH_ANDIF_EXPR:
11195 return boolean_true_node;
11196 case TRUTH_ORIF_EXPR:
11197 return boolean_false_node;
11198 case COMPOUND_EXPR:
11199 return void_node;
11200 default:
11201 break;
11204 if (complain & tf_error)
11205 error_at (location_of (t),
11206 "fold of empty expansion over %O", code);
11207 return error_mark_node;
11210 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11211 form an expression that combines the two terms using the
11212 operator of T. */
11214 static tree
11215 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11217 tree op = FOLD_EXPR_OP (t);
11218 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11220 // Handle compound assignment operators.
11221 if (FOLD_EXPR_MODIFY_P (t))
11222 return build_x_modify_expr (input_location, left, code, right, complain);
11224 switch (code)
11226 case COMPOUND_EXPR:
11227 return build_x_compound_expr (input_location, left, right, complain);
11228 case DOTSTAR_EXPR:
11229 return build_m_component_ref (left, right, complain);
11230 default:
11231 return build_x_binary_op (input_location, code,
11232 left, TREE_CODE (left),
11233 right, TREE_CODE (right),
11234 /*overload=*/NULL,
11235 complain);
11239 /* Substitute ARGS into the pack of a fold expression T. */
11241 static inline tree
11242 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11244 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11247 /* Substitute ARGS into the pack of a fold expression T. */
11249 static inline tree
11250 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11252 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11255 /* Expand a PACK of arguments into a grouped as left fold.
11256 Given a pack containing elements A0, A1, ..., An and an
11257 operator @, this builds the expression:
11259 ((A0 @ A1) @ A2) ... @ An
11261 Note that PACK must not be empty.
11263 The operator is defined by the original fold expression T. */
11265 static tree
11266 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11268 tree left = TREE_VEC_ELT (pack, 0);
11269 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11271 tree right = TREE_VEC_ELT (pack, i);
11272 left = fold_expression (t, left, right, complain);
11274 return left;
11277 /* Substitute into a unary left fold expression. */
11279 static tree
11280 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11281 tree in_decl)
11283 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11284 if (pack == error_mark_node)
11285 return error_mark_node;
11286 if (PACK_EXPANSION_P (pack))
11288 tree r = copy_node (t);
11289 FOLD_EXPR_PACK (r) = pack;
11290 return r;
11292 if (TREE_VEC_LENGTH (pack) == 0)
11293 return expand_empty_fold (t, complain);
11294 else
11295 return expand_left_fold (t, pack, complain);
11298 /* Substitute into a binary left fold expression.
11300 Do ths by building a single (non-empty) vector of argumnts and
11301 building the expression from those elements. */
11303 static tree
11304 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11305 tree in_decl)
11307 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11308 if (pack == error_mark_node)
11309 return error_mark_node;
11310 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11311 if (init == error_mark_node)
11312 return error_mark_node;
11314 if (PACK_EXPANSION_P (pack))
11316 tree r = copy_node (t);
11317 FOLD_EXPR_PACK (r) = pack;
11318 FOLD_EXPR_INIT (r) = init;
11319 return r;
11322 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11323 TREE_VEC_ELT (vec, 0) = init;
11324 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11325 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11327 return expand_left_fold (t, vec, complain);
11330 /* Expand a PACK of arguments into a grouped as right fold.
11331 Given a pack containing elementns A0, A1, ..., and an
11332 operator @, this builds the expression:
11334 A0@ ... (An-2 @ (An-1 @ An))
11336 Note that PACK must not be empty.
11338 The operator is defined by the original fold expression T. */
11340 tree
11341 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11343 // Build the expression.
11344 int n = TREE_VEC_LENGTH (pack);
11345 tree right = TREE_VEC_ELT (pack, n - 1);
11346 for (--n; n != 0; --n)
11348 tree left = TREE_VEC_ELT (pack, n - 1);
11349 right = fold_expression (t, left, right, complain);
11351 return right;
11354 /* Substitute into a unary right fold expression. */
11356 static tree
11357 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11358 tree in_decl)
11360 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11361 if (pack == error_mark_node)
11362 return error_mark_node;
11363 if (PACK_EXPANSION_P (pack))
11365 tree r = copy_node (t);
11366 FOLD_EXPR_PACK (r) = pack;
11367 return r;
11369 if (TREE_VEC_LENGTH (pack) == 0)
11370 return expand_empty_fold (t, complain);
11371 else
11372 return expand_right_fold (t, pack, complain);
11375 /* Substitute into a binary right fold expression.
11377 Do ths by building a single (non-empty) vector of arguments and
11378 building the expression from those elements. */
11380 static tree
11381 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11382 tree in_decl)
11384 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11385 if (pack == error_mark_node)
11386 return error_mark_node;
11387 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11388 if (init == error_mark_node)
11389 return error_mark_node;
11391 if (PACK_EXPANSION_P (pack))
11393 tree r = copy_node (t);
11394 FOLD_EXPR_PACK (r) = pack;
11395 FOLD_EXPR_INIT (r) = init;
11396 return r;
11399 int n = TREE_VEC_LENGTH (pack);
11400 tree vec = make_tree_vec (n + 1);
11401 for (int i = 0; i < n; ++i)
11402 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11403 TREE_VEC_ELT (vec, n) = init;
11405 return expand_right_fold (t, vec, complain);
11409 /* Substitute ARGS into T, which is an pack expansion
11410 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11411 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11412 (if only a partial substitution could be performed) or
11413 ERROR_MARK_NODE if there was an error. */
11414 tree
11415 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11416 tree in_decl)
11418 tree pattern;
11419 tree pack, packs = NULL_TREE;
11420 bool unsubstituted_packs = false;
11421 int i, len = -1;
11422 tree result;
11423 hash_map<tree, tree> *saved_local_specializations = NULL;
11424 bool need_local_specializations = false;
11425 int levels;
11427 gcc_assert (PACK_EXPANSION_P (t));
11428 pattern = PACK_EXPANSION_PATTERN (t);
11430 /* Add in any args remembered from an earlier partial instantiation. */
11431 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11433 levels = TMPL_ARGS_DEPTH (args);
11435 /* Determine the argument packs that will instantiate the parameter
11436 packs used in the expansion expression. While we're at it,
11437 compute the number of arguments to be expanded and make sure it
11438 is consistent. */
11439 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11440 pack = TREE_CHAIN (pack))
11442 tree parm_pack = TREE_VALUE (pack);
11443 tree arg_pack = NULL_TREE;
11444 tree orig_arg = NULL_TREE;
11445 int level = 0;
11447 if (TREE_CODE (parm_pack) == BASES)
11449 gcc_assert (parm_pack == pattern);
11450 if (BASES_DIRECT (parm_pack))
11451 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11452 args, complain, in_decl, false));
11453 else
11454 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11455 args, complain, in_decl, false));
11457 else if (builtin_pack_call_p (parm_pack))
11459 /* ??? Support use in other patterns. */
11460 gcc_assert (parm_pack == pattern);
11461 return expand_builtin_pack_call (parm_pack, args,
11462 complain, in_decl);
11464 else if (TREE_CODE (parm_pack) == PARM_DECL)
11466 /* We know we have correct local_specializations if this
11467 expansion is at function scope, or if we're dealing with a
11468 local parameter in a requires expression; for the latter,
11469 tsubst_requires_expr set it up appropriately. */
11470 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11471 arg_pack = retrieve_local_specialization (parm_pack);
11472 else
11473 /* We can't rely on local_specializations for a parameter
11474 name used later in a function declaration (such as in a
11475 late-specified return type). Even if it exists, it might
11476 have the wrong value for a recursive call. */
11477 need_local_specializations = true;
11479 if (!arg_pack)
11481 /* This parameter pack was used in an unevaluated context. Just
11482 make a dummy decl, since it's only used for its type. */
11483 arg_pack = tsubst_decl (parm_pack, args, complain);
11484 if (arg_pack && DECL_PACK_P (arg_pack))
11485 /* Partial instantiation of the parm_pack, we can't build
11486 up an argument pack yet. */
11487 arg_pack = NULL_TREE;
11488 else
11489 arg_pack = make_fnparm_pack (arg_pack);
11492 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11493 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11494 else
11496 int idx;
11497 template_parm_level_and_index (parm_pack, &level, &idx);
11499 if (level <= levels)
11500 arg_pack = TMPL_ARG (args, level, idx);
11503 orig_arg = arg_pack;
11504 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11505 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11507 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11508 /* This can only happen if we forget to expand an argument
11509 pack somewhere else. Just return an error, silently. */
11511 result = make_tree_vec (1);
11512 TREE_VEC_ELT (result, 0) = error_mark_node;
11513 return result;
11516 if (arg_pack)
11518 int my_len =
11519 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11521 /* Don't bother trying to do a partial substitution with
11522 incomplete packs; we'll try again after deduction. */
11523 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11524 return t;
11526 if (len < 0)
11527 len = my_len;
11528 else if (len != my_len)
11530 if (!(complain & tf_error))
11531 /* Fail quietly. */;
11532 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11533 error ("mismatched argument pack lengths while expanding %qT",
11534 pattern);
11535 else
11536 error ("mismatched argument pack lengths while expanding %qE",
11537 pattern);
11538 return error_mark_node;
11541 /* Keep track of the parameter packs and their corresponding
11542 argument packs. */
11543 packs = tree_cons (parm_pack, arg_pack, packs);
11544 TREE_TYPE (packs) = orig_arg;
11546 else
11548 /* We can't substitute for this parameter pack. We use a flag as
11549 well as the missing_level counter because function parameter
11550 packs don't have a level. */
11551 gcc_assert (processing_template_decl);
11552 unsubstituted_packs = true;
11556 /* If the expansion is just T..., return the matching argument pack, unless
11557 we need to call convert_from_reference on all the elements. This is an
11558 important optimization; see c++/68422. */
11559 if (!unsubstituted_packs
11560 && TREE_PURPOSE (packs) == pattern)
11562 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11563 /* Types need no adjustment, nor does sizeof..., and if we still have
11564 some pack expansion args we won't do anything yet. */
11565 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11566 || PACK_EXPANSION_SIZEOF_P (t)
11567 || pack_expansion_args_count (args))
11568 return args;
11569 /* Also optimize expression pack expansions if we can tell that the
11570 elements won't have reference type. */
11571 tree type = TREE_TYPE (pattern);
11572 if (type && TREE_CODE (type) != REFERENCE_TYPE
11573 && !PACK_EXPANSION_P (type)
11574 && !WILDCARD_TYPE_P (type))
11575 return args;
11576 /* Otherwise use the normal path so we get convert_from_reference. */
11579 /* We cannot expand this expansion expression, because we don't have
11580 all of the argument packs we need. */
11581 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11583 /* We got some full packs, but we can't substitute them in until we
11584 have values for all the packs. So remember these until then. */
11586 t = make_pack_expansion (pattern);
11587 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11588 return t;
11590 else if (unsubstituted_packs)
11592 /* There were no real arguments, we're just replacing a parameter
11593 pack with another version of itself. Substitute into the
11594 pattern and return a PACK_EXPANSION_*. The caller will need to
11595 deal with that. */
11596 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11597 t = tsubst_expr (pattern, args, complain, in_decl,
11598 /*integral_constant_expression_p=*/false);
11599 else
11600 t = tsubst (pattern, args, complain, in_decl);
11601 t = make_pack_expansion (t);
11602 return t;
11605 gcc_assert (len >= 0);
11607 if (need_local_specializations)
11609 /* We're in a late-specified return type, so create our own local
11610 specializations map; the current map is either NULL or (in the
11611 case of recursive unification) might have bindings that we don't
11612 want to use or alter. */
11613 saved_local_specializations = local_specializations;
11614 local_specializations = new hash_map<tree, tree>;
11617 /* For each argument in each argument pack, substitute into the
11618 pattern. */
11619 result = make_tree_vec (len);
11620 tree elem_args = copy_template_args (args);
11621 for (i = 0; i < len; ++i)
11623 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11625 elem_args, complain,
11626 in_decl);
11627 TREE_VEC_ELT (result, i) = t;
11628 if (t == error_mark_node)
11630 result = error_mark_node;
11631 break;
11635 /* Update ARGS to restore the substitution from parameter packs to
11636 their argument packs. */
11637 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11639 tree parm = TREE_PURPOSE (pack);
11641 if (TREE_CODE (parm) == PARM_DECL
11642 || TREE_CODE (parm) == FIELD_DECL)
11643 register_local_specialization (TREE_TYPE (pack), parm);
11644 else
11646 int idx, level;
11648 if (TREE_VALUE (pack) == NULL_TREE)
11649 continue;
11651 template_parm_level_and_index (parm, &level, &idx);
11653 /* Update the corresponding argument. */
11654 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11655 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11656 TREE_TYPE (pack);
11657 else
11658 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11662 if (need_local_specializations)
11664 delete local_specializations;
11665 local_specializations = saved_local_specializations;
11668 /* If the dependent pack arguments were such that we end up with only a
11669 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11670 if (len == 1 && TREE_CODE (result) == TREE_VEC
11671 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11672 return TREE_VEC_ELT (result, 0);
11674 return result;
11677 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11678 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11679 parameter packs; all parms generated from a function parameter pack will
11680 have the same DECL_PARM_INDEX. */
11682 tree
11683 get_pattern_parm (tree parm, tree tmpl)
11685 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11686 tree patparm;
11688 if (DECL_ARTIFICIAL (parm))
11690 for (patparm = DECL_ARGUMENTS (pattern);
11691 patparm; patparm = DECL_CHAIN (patparm))
11692 if (DECL_ARTIFICIAL (patparm)
11693 && DECL_NAME (parm) == DECL_NAME (patparm))
11694 break;
11696 else
11698 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11699 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11700 gcc_assert (DECL_PARM_INDEX (patparm)
11701 == DECL_PARM_INDEX (parm));
11704 return patparm;
11707 /* Make an argument pack out of the TREE_VEC VEC. */
11709 static tree
11710 make_argument_pack (tree vec)
11712 tree pack;
11713 tree elt = TREE_VEC_ELT (vec, 0);
11714 if (TYPE_P (elt))
11715 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11716 else
11718 pack = make_node (NONTYPE_ARGUMENT_PACK);
11719 TREE_CONSTANT (pack) = 1;
11721 SET_ARGUMENT_PACK_ARGS (pack, vec);
11722 return pack;
11725 /* Return an exact copy of template args T that can be modified
11726 independently. */
11728 static tree
11729 copy_template_args (tree t)
11731 if (t == error_mark_node)
11732 return t;
11734 int len = TREE_VEC_LENGTH (t);
11735 tree new_vec = make_tree_vec (len);
11737 for (int i = 0; i < len; ++i)
11739 tree elt = TREE_VEC_ELT (t, i);
11740 if (elt && TREE_CODE (elt) == TREE_VEC)
11741 elt = copy_template_args (elt);
11742 TREE_VEC_ELT (new_vec, i) = elt;
11745 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11746 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11748 return new_vec;
11751 /* Substitute ARGS into the vector or list of template arguments T. */
11753 static tree
11754 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11756 tree orig_t = t;
11757 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11758 tree *elts;
11760 if (t == error_mark_node)
11761 return error_mark_node;
11763 len = TREE_VEC_LENGTH (t);
11764 elts = XALLOCAVEC (tree, len);
11766 for (i = 0; i < len; i++)
11768 tree orig_arg = TREE_VEC_ELT (t, i);
11769 tree new_arg;
11771 if (TREE_CODE (orig_arg) == TREE_VEC)
11772 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11773 else if (PACK_EXPANSION_P (orig_arg))
11775 /* Substitute into an expansion expression. */
11776 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11778 if (TREE_CODE (new_arg) == TREE_VEC)
11779 /* Add to the expanded length adjustment the number of
11780 expanded arguments. We subtract one from this
11781 measurement, because the argument pack expression
11782 itself is already counted as 1 in
11783 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11784 the argument pack is empty. */
11785 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11787 else if (ARGUMENT_PACK_P (orig_arg))
11789 /* Substitute into each of the arguments. */
11790 new_arg = TYPE_P (orig_arg)
11791 ? cxx_make_type (TREE_CODE (orig_arg))
11792 : make_node (TREE_CODE (orig_arg));
11794 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11795 args, complain, in_decl);
11796 if (pack_args == error_mark_node)
11797 new_arg = error_mark_node;
11798 else
11799 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11801 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11802 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11804 else
11805 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11807 if (new_arg == error_mark_node)
11808 return error_mark_node;
11810 elts[i] = new_arg;
11811 if (new_arg != orig_arg)
11812 need_new = 1;
11815 if (!need_new)
11816 return t;
11818 /* Make space for the expanded arguments coming from template
11819 argument packs. */
11820 t = make_tree_vec (len + expanded_len_adjust);
11821 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11822 arguments for a member template.
11823 In that case each TREE_VEC in ORIG_T represents a level of template
11824 arguments, and ORIG_T won't carry any non defaulted argument count.
11825 It will rather be the nested TREE_VECs that will carry one.
11826 In other words, ORIG_T carries a non defaulted argument count only
11827 if it doesn't contain any nested TREE_VEC. */
11828 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11830 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11831 count += expanded_len_adjust;
11832 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11834 for (i = 0, out = 0; i < len; i++)
11836 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11837 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11838 && TREE_CODE (elts[i]) == TREE_VEC)
11840 int idx;
11842 /* Now expand the template argument pack "in place". */
11843 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11844 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11846 else
11848 TREE_VEC_ELT (t, out) = elts[i];
11849 out++;
11853 return t;
11856 /* Substitute ARGS into one level PARMS of template parameters. */
11858 static tree
11859 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11861 if (parms == error_mark_node)
11862 return error_mark_node;
11864 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11866 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11868 tree tuple = TREE_VEC_ELT (parms, i);
11870 if (tuple == error_mark_node)
11871 continue;
11873 TREE_VEC_ELT (new_vec, i) =
11874 tsubst_template_parm (tuple, args, complain);
11877 return new_vec;
11880 /* Return the result of substituting ARGS into the template parameters
11881 given by PARMS. If there are m levels of ARGS and m + n levels of
11882 PARMS, then the result will contain n levels of PARMS. For
11883 example, if PARMS is `template <class T> template <class U>
11884 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11885 result will be `template <int*, double, class V>'. */
11887 static tree
11888 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11890 tree r = NULL_TREE;
11891 tree* new_parms;
11893 /* When substituting into a template, we must set
11894 PROCESSING_TEMPLATE_DECL as the template parameters may be
11895 dependent if they are based on one-another, and the dependency
11896 predicates are short-circuit outside of templates. */
11897 ++processing_template_decl;
11899 for (new_parms = &r;
11900 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11901 new_parms = &(TREE_CHAIN (*new_parms)),
11902 parms = TREE_CHAIN (parms))
11904 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11905 args, complain);
11906 *new_parms =
11907 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11908 - TMPL_ARGS_DEPTH (args)),
11909 new_vec, NULL_TREE);
11912 --processing_template_decl;
11914 return r;
11917 /* Return the result of substituting ARGS into one template parameter
11918 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11919 parameter and which TREE_PURPOSE is the default argument of the
11920 template parameter. */
11922 static tree
11923 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11925 tree default_value, parm_decl;
11927 if (args == NULL_TREE
11928 || t == NULL_TREE
11929 || t == error_mark_node)
11930 return t;
11932 gcc_assert (TREE_CODE (t) == TREE_LIST);
11934 default_value = TREE_PURPOSE (t);
11935 parm_decl = TREE_VALUE (t);
11937 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11938 if (TREE_CODE (parm_decl) == PARM_DECL
11939 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11940 parm_decl = error_mark_node;
11941 default_value = tsubst_template_arg (default_value, args,
11942 complain, NULL_TREE);
11944 return build_tree_list (default_value, parm_decl);
11947 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11948 type T. If T is not an aggregate or enumeration type, it is
11949 handled as if by tsubst. IN_DECL is as for tsubst. If
11950 ENTERING_SCOPE is nonzero, T is the context for a template which
11951 we are presently tsubst'ing. Return the substituted value. */
11953 static tree
11954 tsubst_aggr_type (tree t,
11955 tree args,
11956 tsubst_flags_t complain,
11957 tree in_decl,
11958 int entering_scope)
11960 if (t == NULL_TREE)
11961 return NULL_TREE;
11963 switch (TREE_CODE (t))
11965 case RECORD_TYPE:
11966 if (TYPE_PTRMEMFUNC_P (t))
11967 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11969 /* Fall through. */
11970 case ENUMERAL_TYPE:
11971 case UNION_TYPE:
11972 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11974 tree argvec;
11975 tree context;
11976 tree r;
11977 int saved_unevaluated_operand;
11978 int saved_inhibit_evaluation_warnings;
11980 /* In "sizeof(X<I>)" we need to evaluate "I". */
11981 saved_unevaluated_operand = cp_unevaluated_operand;
11982 cp_unevaluated_operand = 0;
11983 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11984 c_inhibit_evaluation_warnings = 0;
11986 /* First, determine the context for the type we are looking
11987 up. */
11988 context = TYPE_CONTEXT (t);
11989 if (context && TYPE_P (context))
11991 context = tsubst_aggr_type (context, args, complain,
11992 in_decl, /*entering_scope=*/1);
11993 /* If context is a nested class inside a class template,
11994 it may still need to be instantiated (c++/33959). */
11995 context = complete_type (context);
11998 /* Then, figure out what arguments are appropriate for the
11999 type we are trying to find. For example, given:
12001 template <class T> struct S;
12002 template <class T, class U> void f(T, U) { S<U> su; }
12004 and supposing that we are instantiating f<int, double>,
12005 then our ARGS will be {int, double}, but, when looking up
12006 S we only want {double}. */
12007 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12008 complain, in_decl);
12009 if (argvec == error_mark_node)
12010 r = error_mark_node;
12011 else
12013 r = lookup_template_class (t, argvec, in_decl, context,
12014 entering_scope, complain);
12015 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12018 cp_unevaluated_operand = saved_unevaluated_operand;
12019 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12021 return r;
12023 else
12024 /* This is not a template type, so there's nothing to do. */
12025 return t;
12027 default:
12028 return tsubst (t, args, complain, in_decl);
12032 static GTY(()) hash_map<tree, tree> *defarg_inst;
12034 /* Substitute into the default argument ARG (a default argument for
12035 FN), which has the indicated TYPE. */
12037 tree
12038 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12039 tsubst_flags_t complain)
12041 tree saved_class_ptr = NULL_TREE;
12042 tree saved_class_ref = NULL_TREE;
12043 int errs = errorcount + sorrycount;
12045 /* This can happen in invalid code. */
12046 if (TREE_CODE (arg) == DEFAULT_ARG)
12047 return arg;
12049 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12050 parm = chain_index (parmnum, parm);
12051 tree parmtype = TREE_TYPE (parm);
12052 if (DECL_BY_REFERENCE (parm))
12053 parmtype = TREE_TYPE (parmtype);
12054 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12056 tree *slot;
12057 if (defarg_inst && (slot = defarg_inst->get (parm)))
12058 return *slot;
12060 /* This default argument came from a template. Instantiate the
12061 default argument here, not in tsubst. In the case of
12062 something like:
12064 template <class T>
12065 struct S {
12066 static T t();
12067 void f(T = t());
12070 we must be careful to do name lookup in the scope of S<T>,
12071 rather than in the current class. */
12072 push_access_scope (fn);
12073 /* The "this" pointer is not valid in a default argument. */
12074 if (cfun)
12076 saved_class_ptr = current_class_ptr;
12077 cp_function_chain->x_current_class_ptr = NULL_TREE;
12078 saved_class_ref = current_class_ref;
12079 cp_function_chain->x_current_class_ref = NULL_TREE;
12082 start_lambda_scope (parm);
12084 push_deferring_access_checks(dk_no_deferred);
12085 /* The default argument expression may cause implicitly defined
12086 member functions to be synthesized, which will result in garbage
12087 collection. We must treat this situation as if we were within
12088 the body of function so as to avoid collecting live data on the
12089 stack. */
12090 ++function_depth;
12091 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12092 complain, NULL_TREE,
12093 /*integral_constant_expression_p=*/false);
12094 --function_depth;
12095 pop_deferring_access_checks();
12097 finish_lambda_scope ();
12099 /* Restore the "this" pointer. */
12100 if (cfun)
12102 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12103 cp_function_chain->x_current_class_ref = saved_class_ref;
12106 if (errorcount+sorrycount > errs
12107 && (complain & tf_warning_or_error))
12108 inform (input_location,
12109 " when instantiating default argument for call to %qD", fn);
12111 /* Make sure the default argument is reasonable. */
12112 arg = check_default_argument (type, arg, complain);
12114 pop_access_scope (fn);
12116 if (arg != error_mark_node && !cp_unevaluated_operand)
12118 if (!defarg_inst)
12119 defarg_inst = hash_map<tree,tree>::create_ggc (37);
12120 defarg_inst->put (parm, arg);
12123 return arg;
12126 /* Substitute into all the default arguments for FN. */
12128 static void
12129 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12131 tree arg;
12132 tree tmpl_args;
12134 tmpl_args = DECL_TI_ARGS (fn);
12136 /* If this function is not yet instantiated, we certainly don't need
12137 its default arguments. */
12138 if (uses_template_parms (tmpl_args))
12139 return;
12140 /* Don't do this again for clones. */
12141 if (DECL_CLONED_FUNCTION_P (fn))
12142 return;
12144 int i = 0;
12145 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12146 arg;
12147 arg = TREE_CHAIN (arg), ++i)
12148 if (TREE_PURPOSE (arg))
12149 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12150 TREE_VALUE (arg),
12151 TREE_PURPOSE (arg),
12152 complain);
12155 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12157 static tree
12158 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12159 tree lambda_fntype)
12161 tree gen_tmpl, argvec;
12162 hashval_t hash = 0;
12163 tree in_decl = t;
12165 /* Nobody should be tsubst'ing into non-template functions. */
12166 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12168 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12170 /* If T is not dependent, just return it. */
12171 if (!uses_template_parms (DECL_TI_ARGS (t)))
12172 return t;
12174 /* Calculate the most general template of which R is a
12175 specialization, and the complete set of arguments used to
12176 specialize R. */
12177 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12178 argvec = tsubst_template_args (DECL_TI_ARGS
12179 (DECL_TEMPLATE_RESULT
12180 (DECL_TI_TEMPLATE (t))),
12181 args, complain, in_decl);
12182 if (argvec == error_mark_node)
12183 return error_mark_node;
12185 /* Check to see if we already have this specialization. */
12186 if (!lambda_fntype)
12188 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12189 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12190 return spec;
12193 /* We can see more levels of arguments than parameters if
12194 there was a specialization of a member template, like
12195 this:
12197 template <class T> struct S { template <class U> void f(); }
12198 template <> template <class U> void S<int>::f(U);
12200 Here, we'll be substituting into the specialization,
12201 because that's where we can find the code we actually
12202 want to generate, but we'll have enough arguments for
12203 the most general template.
12205 We also deal with the peculiar case:
12207 template <class T> struct S {
12208 template <class U> friend void f();
12210 template <class U> void f() {}
12211 template S<int>;
12212 template void f<double>();
12214 Here, the ARGS for the instantiation of will be {int,
12215 double}. But, we only need as many ARGS as there are
12216 levels of template parameters in CODE_PATTERN. We are
12217 careful not to get fooled into reducing the ARGS in
12218 situations like:
12220 template <class T> struct S { template <class U> void f(U); }
12221 template <class T> template <> void S<T>::f(int) {}
12223 which we can spot because the pattern will be a
12224 specialization in this case. */
12225 int args_depth = TMPL_ARGS_DEPTH (args);
12226 int parms_depth =
12227 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12229 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12230 args = get_innermost_template_args (args, parms_depth);
12232 else
12234 /* This special case arises when we have something like this:
12236 template <class T> struct S {
12237 friend void f<int>(int, double);
12240 Here, the DECL_TI_TEMPLATE for the friend declaration
12241 will be an IDENTIFIER_NODE. We are being called from
12242 tsubst_friend_function, and we want only to create a
12243 new decl (R) with appropriate types so that we can call
12244 determine_specialization. */
12245 gen_tmpl = NULL_TREE;
12246 argvec = NULL_TREE;
12249 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12250 : NULL_TREE);
12251 tree ctx = closure ? closure : DECL_CONTEXT (t);
12252 bool member = ctx && TYPE_P (ctx);
12254 if (member && !closure)
12255 ctx = tsubst_aggr_type (ctx, args,
12256 complain, t, /*entering_scope=*/1);
12258 tree type = (lambda_fntype ? lambda_fntype
12259 : tsubst (TREE_TYPE (t), args,
12260 complain | tf_fndecl_type, in_decl));
12261 if (type == error_mark_node)
12262 return error_mark_node;
12264 /* If we hit excessive deduction depth, the type is bogus even if
12265 it isn't error_mark_node, so don't build a decl. */
12266 if (excessive_deduction_depth)
12267 return error_mark_node;
12269 /* We do NOT check for matching decls pushed separately at this
12270 point, as they may not represent instantiations of this
12271 template, and in any case are considered separate under the
12272 discrete model. */
12273 tree r = copy_decl (t);
12274 DECL_USE_TEMPLATE (r) = 0;
12275 TREE_TYPE (r) = type;
12276 /* Clear out the mangled name and RTL for the instantiation. */
12277 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12278 SET_DECL_RTL (r, NULL);
12279 /* Leave DECL_INITIAL set on deleted instantiations. */
12280 if (!DECL_DELETED_FN (r))
12281 DECL_INITIAL (r) = NULL_TREE;
12282 DECL_CONTEXT (r) = ctx;
12284 /* OpenMP UDRs have the only argument a reference to the declared
12285 type. We want to diagnose if the declared type is a reference,
12286 which is invalid, but as references to references are usually
12287 quietly merged, diagnose it here. */
12288 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12290 tree argtype
12291 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12292 argtype = tsubst (argtype, args, complain, in_decl);
12293 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12294 error_at (DECL_SOURCE_LOCATION (t),
12295 "reference type %qT in "
12296 "%<#pragma omp declare reduction%>", argtype);
12297 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12298 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12299 argtype);
12302 if (member && DECL_CONV_FN_P (r))
12303 /* Type-conversion operator. Reconstruct the name, in
12304 case it's the name of one of the template's parameters. */
12305 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12307 tree parms = DECL_ARGUMENTS (t);
12308 if (closure)
12309 parms = DECL_CHAIN (parms);
12310 parms = tsubst (parms, args, complain, t);
12311 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12312 DECL_CONTEXT (parm) = r;
12313 if (closure)
12315 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12316 DECL_CHAIN (tparm) = parms;
12317 parms = tparm;
12319 DECL_ARGUMENTS (r) = parms;
12320 DECL_RESULT (r) = NULL_TREE;
12322 TREE_STATIC (r) = 0;
12323 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12324 DECL_EXTERNAL (r) = 1;
12325 /* If this is an instantiation of a function with internal
12326 linkage, we already know what object file linkage will be
12327 assigned to the instantiation. */
12328 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12329 DECL_DEFER_OUTPUT (r) = 0;
12330 DECL_CHAIN (r) = NULL_TREE;
12331 DECL_PENDING_INLINE_INFO (r) = 0;
12332 DECL_PENDING_INLINE_P (r) = 0;
12333 DECL_SAVED_TREE (r) = NULL_TREE;
12334 DECL_STRUCT_FUNCTION (r) = NULL;
12335 TREE_USED (r) = 0;
12336 /* We'll re-clone as appropriate in instantiate_template. */
12337 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12339 /* If we aren't complaining now, return on error before we register
12340 the specialization so that we'll complain eventually. */
12341 if ((complain & tf_error) == 0
12342 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12343 && !grok_op_properties (r, /*complain=*/false))
12344 return error_mark_node;
12346 /* When instantiating a constrained member, substitute
12347 into the constraints to create a new constraint. */
12348 if (tree ci = get_constraints (t))
12349 if (member)
12351 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12352 set_constraints (r, ci);
12355 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12356 this in the special friend case mentioned above where
12357 GEN_TMPL is NULL. */
12358 if (gen_tmpl && !closure)
12360 DECL_TEMPLATE_INFO (r)
12361 = build_template_info (gen_tmpl, argvec);
12362 SET_DECL_IMPLICIT_INSTANTIATION (r);
12364 tree new_r
12365 = register_specialization (r, gen_tmpl, argvec, false, hash);
12366 if (new_r != r)
12367 /* We instantiated this while substituting into
12368 the type earlier (template/friend54.C). */
12369 return new_r;
12371 /* We're not supposed to instantiate default arguments
12372 until they are called, for a template. But, for a
12373 declaration like:
12375 template <class T> void f ()
12376 { extern void g(int i = T()); }
12378 we should do the substitution when the template is
12379 instantiated. We handle the member function case in
12380 instantiate_class_template since the default arguments
12381 might refer to other members of the class. */
12382 if (!member
12383 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12384 && !uses_template_parms (argvec))
12385 tsubst_default_arguments (r, complain);
12387 else
12388 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12390 /* Copy the list of befriending classes. */
12391 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12392 *friends;
12393 friends = &TREE_CHAIN (*friends))
12395 *friends = copy_node (*friends);
12396 TREE_VALUE (*friends)
12397 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12400 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12402 maybe_retrofit_in_chrg (r);
12403 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12404 return error_mark_node;
12405 /* If this is an instantiation of a member template, clone it.
12406 If it isn't, that'll be handled by
12407 clone_constructors_and_destructors. */
12408 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12409 clone_function_decl (r, /*update_methods=*/false);
12411 else if ((complain & tf_error) != 0
12412 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12413 && !grok_op_properties (r, /*complain=*/true))
12414 return error_mark_node;
12416 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12417 SET_DECL_FRIEND_CONTEXT (r,
12418 tsubst (DECL_FRIEND_CONTEXT (t),
12419 args, complain, in_decl));
12421 /* Possibly limit visibility based on template args. */
12422 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12423 if (DECL_VISIBILITY_SPECIFIED (t))
12425 DECL_VISIBILITY_SPECIFIED (r) = 0;
12426 DECL_ATTRIBUTES (r)
12427 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12429 determine_visibility (r);
12430 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12431 && !processing_template_decl)
12432 defaulted_late_check (r);
12434 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12435 args, complain, in_decl);
12436 return r;
12439 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12441 static tree
12442 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12443 tree lambda_fntype)
12445 /* We can get here when processing a member function template,
12446 member class template, or template template parameter. */
12447 tree decl = DECL_TEMPLATE_RESULT (t);
12448 tree in_decl = t;
12449 tree spec;
12450 tree tmpl_args;
12451 tree full_args;
12452 tree r;
12453 hashval_t hash = 0;
12455 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12457 /* Template template parameter is treated here. */
12458 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12459 if (new_type == error_mark_node)
12460 r = error_mark_node;
12461 /* If we get a real template back, return it. This can happen in
12462 the context of most_specialized_partial_spec. */
12463 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12464 r = new_type;
12465 else
12466 /* The new TEMPLATE_DECL was built in
12467 reduce_template_parm_level. */
12468 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12469 return r;
12472 if (!lambda_fntype)
12474 /* We might already have an instance of this template.
12475 The ARGS are for the surrounding class type, so the
12476 full args contain the tsubst'd args for the context,
12477 plus the innermost args from the template decl. */
12478 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12479 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12480 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12481 /* Because this is a template, the arguments will still be
12482 dependent, even after substitution. If
12483 PROCESSING_TEMPLATE_DECL is not set, the dependency
12484 predicates will short-circuit. */
12485 ++processing_template_decl;
12486 full_args = tsubst_template_args (tmpl_args, args,
12487 complain, in_decl);
12488 --processing_template_decl;
12489 if (full_args == error_mark_node)
12490 return error_mark_node;
12492 /* If this is a default template template argument,
12493 tsubst might not have changed anything. */
12494 if (full_args == tmpl_args)
12495 return t;
12497 hash = hash_tmpl_and_args (t, full_args);
12498 spec = retrieve_specialization (t, full_args, hash);
12499 if (spec != NULL_TREE)
12500 return spec;
12503 /* Make a new template decl. It will be similar to the
12504 original, but will record the current template arguments.
12505 We also create a new function declaration, which is just
12506 like the old one, but points to this new template, rather
12507 than the old one. */
12508 r = copy_decl (t);
12509 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12510 DECL_CHAIN (r) = NULL_TREE;
12512 // Build new template info linking to the original template decl.
12513 if (!lambda_fntype)
12515 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12516 SET_DECL_IMPLICIT_INSTANTIATION (r);
12518 else
12519 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12521 /* The template parameters for this new template are all the
12522 template parameters for the old template, except the
12523 outermost level of parameters. */
12524 DECL_TEMPLATE_PARMS (r)
12525 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12526 complain);
12528 if (TREE_CODE (decl) == TYPE_DECL
12529 && !TYPE_DECL_ALIAS_P (decl))
12531 tree new_type;
12532 ++processing_template_decl;
12533 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12534 --processing_template_decl;
12535 if (new_type == error_mark_node)
12536 return error_mark_node;
12538 TREE_TYPE (r) = new_type;
12539 /* For a partial specialization, we need to keep pointing to
12540 the primary template. */
12541 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12542 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12543 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12544 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12545 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12547 else
12549 tree new_decl;
12550 ++processing_template_decl;
12551 if (TREE_CODE (decl) == FUNCTION_DECL)
12552 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12553 else
12554 new_decl = tsubst (decl, args, complain, in_decl);
12555 --processing_template_decl;
12556 if (new_decl == error_mark_node)
12557 return error_mark_node;
12559 DECL_TEMPLATE_RESULT (r) = new_decl;
12560 TREE_TYPE (r) = TREE_TYPE (new_decl);
12561 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12562 if (lambda_fntype)
12564 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12565 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12567 else
12569 DECL_TI_TEMPLATE (new_decl) = r;
12570 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12574 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12575 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12577 if (PRIMARY_TEMPLATE_P (t))
12578 DECL_PRIMARY_TEMPLATE (r) = r;
12580 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12581 && !lambda_fntype)
12582 /* Record this non-type partial instantiation. */
12583 register_specialization (r, t,
12584 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12585 false, hash);
12587 return r;
12590 /* True if FN is the op() for a lambda in an uninstantiated template. */
12592 bool
12593 lambda_fn_in_template_p (tree fn)
12595 if (!LAMBDA_FUNCTION_P (fn))
12596 return false;
12597 tree closure = DECL_CONTEXT (fn);
12598 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12601 /* True if FN is the op() for a lambda regenerated from a lambda in an
12602 uninstantiated template. */
12604 bool
12605 regenerated_lambda_fn_p (tree fn)
12607 return (LAMBDA_FUNCTION_P (fn)
12608 && !DECL_TEMPLATE_INSTANTIATION (fn));
12611 /* We're instantiating a variable from template function TCTX. Return the
12612 corresponding current enclosing scope. This gets complicated because lambda
12613 functions in templates are regenerated rather than instantiated, but generic
12614 lambda functions are subsequently instantiated. */
12616 static tree
12617 enclosing_instantiation_of (tree tctx)
12619 tree fn = current_function_decl;
12620 int lambda_count = 0;
12622 for (; tctx && lambda_fn_in_template_p (tctx);
12623 tctx = decl_function_context (tctx))
12624 ++lambda_count;
12625 for (; fn; fn = decl_function_context (fn))
12627 tree lambda = fn;
12628 int flambda_count = 0;
12629 for (; fn && regenerated_lambda_fn_p (fn);
12630 fn = decl_function_context (fn))
12631 ++flambda_count;
12632 if (DECL_TEMPLATE_INFO (fn)
12633 ? most_general_template (fn) != most_general_template (tctx)
12634 : fn != tctx)
12635 continue;
12636 if (lambda_count)
12638 fn = lambda;
12639 while (flambda_count-- > lambda_count)
12640 fn = decl_function_context (fn);
12642 return fn;
12644 gcc_unreachable ();
12647 /* Substitute the ARGS into the T, which is a _DECL. Return the
12648 result of the substitution. Issue error and warning messages under
12649 control of COMPLAIN. */
12651 static tree
12652 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12654 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12655 location_t saved_loc;
12656 tree r = NULL_TREE;
12657 tree in_decl = t;
12658 hashval_t hash = 0;
12660 /* Set the filename and linenumber to improve error-reporting. */
12661 saved_loc = input_location;
12662 input_location = DECL_SOURCE_LOCATION (t);
12664 switch (TREE_CODE (t))
12666 case TEMPLATE_DECL:
12667 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12668 break;
12670 case FUNCTION_DECL:
12671 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12672 break;
12674 case PARM_DECL:
12676 tree type = NULL_TREE;
12677 int i, len = 1;
12678 tree expanded_types = NULL_TREE;
12679 tree prev_r = NULL_TREE;
12680 tree first_r = NULL_TREE;
12682 if (DECL_PACK_P (t))
12684 /* If there is a local specialization that isn't a
12685 parameter pack, it means that we're doing a "simple"
12686 substitution from inside tsubst_pack_expansion. Just
12687 return the local specialization (which will be a single
12688 parm). */
12689 tree spec = retrieve_local_specialization (t);
12690 if (spec
12691 && TREE_CODE (spec) == PARM_DECL
12692 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12693 RETURN (spec);
12695 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12696 the parameters in this function parameter pack. */
12697 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12698 complain, in_decl);
12699 if (TREE_CODE (expanded_types) == TREE_VEC)
12701 len = TREE_VEC_LENGTH (expanded_types);
12703 /* Zero-length parameter packs are boring. Just substitute
12704 into the chain. */
12705 if (len == 0)
12706 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12707 TREE_CHAIN (t)));
12709 else
12711 /* All we did was update the type. Make a note of that. */
12712 type = expanded_types;
12713 expanded_types = NULL_TREE;
12717 /* Loop through all of the parameters we'll build. When T is
12718 a function parameter pack, LEN is the number of expanded
12719 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12720 r = NULL_TREE;
12721 for (i = 0; i < len; ++i)
12723 prev_r = r;
12724 r = copy_node (t);
12725 if (DECL_TEMPLATE_PARM_P (t))
12726 SET_DECL_TEMPLATE_PARM_P (r);
12728 if (expanded_types)
12729 /* We're on the Ith parameter of the function parameter
12730 pack. */
12732 /* Get the Ith type. */
12733 type = TREE_VEC_ELT (expanded_types, i);
12735 /* Rename the parameter to include the index. */
12736 DECL_NAME (r)
12737 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12739 else if (!type)
12740 /* We're dealing with a normal parameter. */
12741 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12743 type = type_decays_to (type);
12744 TREE_TYPE (r) = type;
12745 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12747 if (DECL_INITIAL (r))
12749 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12750 DECL_INITIAL (r) = TREE_TYPE (r);
12751 else
12752 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12753 complain, in_decl);
12756 DECL_CONTEXT (r) = NULL_TREE;
12758 if (!DECL_TEMPLATE_PARM_P (r))
12759 DECL_ARG_TYPE (r) = type_passed_as (type);
12761 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12762 args, complain, in_decl);
12764 /* Keep track of the first new parameter we
12765 generate. That's what will be returned to the
12766 caller. */
12767 if (!first_r)
12768 first_r = r;
12770 /* Build a proper chain of parameters when substituting
12771 into a function parameter pack. */
12772 if (prev_r)
12773 DECL_CHAIN (prev_r) = r;
12776 /* If cp_unevaluated_operand is set, we're just looking for a
12777 single dummy parameter, so don't keep going. */
12778 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12779 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12780 complain, DECL_CHAIN (t));
12782 /* FIRST_R contains the start of the chain we've built. */
12783 r = first_r;
12785 break;
12787 case FIELD_DECL:
12789 tree type = NULL_TREE;
12790 tree vec = NULL_TREE;
12791 tree expanded_types = NULL_TREE;
12792 int len = 1;
12794 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12796 /* This field is a lambda capture pack. Return a TREE_VEC of
12797 the expanded fields to instantiate_class_template_1 and
12798 store them in the specializations hash table as a
12799 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12800 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12801 complain, in_decl);
12802 if (TREE_CODE (expanded_types) == TREE_VEC)
12804 len = TREE_VEC_LENGTH (expanded_types);
12805 vec = make_tree_vec (len);
12807 else
12809 /* All we did was update the type. Make a note of that. */
12810 type = expanded_types;
12811 expanded_types = NULL_TREE;
12815 for (int i = 0; i < len; ++i)
12817 r = copy_decl (t);
12818 if (expanded_types)
12820 type = TREE_VEC_ELT (expanded_types, i);
12821 DECL_NAME (r)
12822 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12824 else if (!type)
12825 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12827 if (type == error_mark_node)
12828 RETURN (error_mark_node);
12829 TREE_TYPE (r) = type;
12830 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12832 if (DECL_C_BIT_FIELD (r))
12833 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12834 non-bit-fields DECL_INITIAL is a non-static data member
12835 initializer, which gets deferred instantiation. */
12836 DECL_INITIAL (r)
12837 = tsubst_expr (DECL_INITIAL (t), args,
12838 complain, in_decl,
12839 /*integral_constant_expression_p=*/true);
12840 else if (DECL_INITIAL (t))
12842 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12843 NSDMI in perform_member_init. Still set DECL_INITIAL
12844 so that we know there is one. */
12845 DECL_INITIAL (r) = void_node;
12846 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12847 retrofit_lang_decl (r);
12848 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12850 /* We don't have to set DECL_CONTEXT here; it is set by
12851 finish_member_declaration. */
12852 DECL_CHAIN (r) = NULL_TREE;
12854 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12855 args, complain, in_decl);
12857 if (vec)
12858 TREE_VEC_ELT (vec, i) = r;
12861 if (vec)
12863 r = vec;
12864 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12865 SET_ARGUMENT_PACK_ARGS (pack, vec);
12866 register_specialization (pack, t, args, false, 0);
12869 break;
12871 case USING_DECL:
12872 /* We reach here only for member using decls. We also need to check
12873 uses_template_parms because DECL_DEPENDENT_P is not set for a
12874 using-declaration that designates a member of the current
12875 instantiation (c++/53549). */
12876 if (DECL_DEPENDENT_P (t)
12877 || uses_template_parms (USING_DECL_SCOPE (t)))
12879 tree scope = USING_DECL_SCOPE (t);
12880 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12881 if (PACK_EXPANSION_P (scope))
12883 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12884 int len = TREE_VEC_LENGTH (vec);
12885 r = make_tree_vec (len);
12886 for (int i = 0; i < len; ++i)
12888 tree escope = TREE_VEC_ELT (vec, i);
12889 tree elt = do_class_using_decl (escope, name);
12890 if (!elt)
12892 r = error_mark_node;
12893 break;
12895 else
12897 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12898 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12900 TREE_VEC_ELT (r, i) = elt;
12903 else
12905 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12906 complain, in_decl);
12907 r = do_class_using_decl (inst_scope, name);
12908 if (!r)
12909 r = error_mark_node;
12910 else
12912 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12913 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12917 else
12919 r = copy_node (t);
12920 DECL_CHAIN (r) = NULL_TREE;
12922 break;
12924 case TYPE_DECL:
12925 case VAR_DECL:
12927 tree argvec = NULL_TREE;
12928 tree gen_tmpl = NULL_TREE;
12929 tree spec;
12930 tree tmpl = NULL_TREE;
12931 tree ctx;
12932 tree type = NULL_TREE;
12933 bool local_p;
12935 if (TREE_TYPE (t) == error_mark_node)
12936 RETURN (error_mark_node);
12938 if (TREE_CODE (t) == TYPE_DECL
12939 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12941 /* If this is the canonical decl, we don't have to
12942 mess with instantiations, and often we can't (for
12943 typename, template type parms and such). Note that
12944 TYPE_NAME is not correct for the above test if
12945 we've copied the type for a typedef. */
12946 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12947 if (type == error_mark_node)
12948 RETURN (error_mark_node);
12949 r = TYPE_NAME (type);
12950 break;
12953 /* Check to see if we already have the specialization we
12954 need. */
12955 spec = NULL_TREE;
12956 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12958 /* T is a static data member or namespace-scope entity.
12959 We have to substitute into namespace-scope variables
12960 (not just variable templates) because of cases like:
12962 template <class T> void f() { extern T t; }
12964 where the entity referenced is not known until
12965 instantiation time. */
12966 local_p = false;
12967 ctx = DECL_CONTEXT (t);
12968 if (DECL_CLASS_SCOPE_P (t))
12970 ctx = tsubst_aggr_type (ctx, args,
12971 complain,
12972 in_decl, /*entering_scope=*/1);
12973 /* If CTX is unchanged, then T is in fact the
12974 specialization we want. That situation occurs when
12975 referencing a static data member within in its own
12976 class. We can use pointer equality, rather than
12977 same_type_p, because DECL_CONTEXT is always
12978 canonical... */
12979 if (ctx == DECL_CONTEXT (t)
12980 /* ... unless T is a member template; in which
12981 case our caller can be willing to create a
12982 specialization of that template represented
12983 by T. */
12984 && !(DECL_TI_TEMPLATE (t)
12985 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12986 spec = t;
12989 if (!spec)
12991 tmpl = DECL_TI_TEMPLATE (t);
12992 gen_tmpl = most_general_template (tmpl);
12993 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12994 if (argvec != error_mark_node)
12995 argvec = (coerce_innermost_template_parms
12996 (DECL_TEMPLATE_PARMS (gen_tmpl),
12997 argvec, t, complain,
12998 /*all*/true, /*defarg*/true));
12999 if (argvec == error_mark_node)
13000 RETURN (error_mark_node);
13001 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13002 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13005 else
13007 /* A local variable. */
13008 local_p = true;
13009 /* Subsequent calls to pushdecl will fill this in. */
13010 ctx = NULL_TREE;
13011 /* Unless this is a reference to a static variable from an
13012 enclosing function, in which case we need to fill it in now. */
13013 if (TREE_STATIC (t))
13015 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13016 if (fn != current_function_decl)
13017 ctx = fn;
13019 spec = retrieve_local_specialization (t);
13021 /* If we already have the specialization we need, there is
13022 nothing more to do. */
13023 if (spec)
13025 r = spec;
13026 break;
13029 /* Create a new node for the specialization we need. */
13030 r = copy_decl (t);
13031 if (type == NULL_TREE)
13033 if (is_typedef_decl (t))
13034 type = DECL_ORIGINAL_TYPE (t);
13035 else
13036 type = TREE_TYPE (t);
13037 if (VAR_P (t)
13038 && VAR_HAD_UNKNOWN_BOUND (t)
13039 && type != error_mark_node)
13040 type = strip_array_domain (type);
13041 tree auto_node = type_uses_auto (type);
13042 int len = TREE_VEC_LENGTH (args);
13043 if (auto_node)
13044 /* Mask off any template args past the variable's context so we
13045 don't replace the auto with an unrelated argument. */
13046 TREE_VEC_LENGTH (args) = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13047 type = tsubst (type, args, complain, in_decl);
13048 if (auto_node)
13049 TREE_VEC_LENGTH (args) = len;
13051 if (VAR_P (r))
13053 /* Even if the original location is out of scope, the
13054 newly substituted one is not. */
13055 DECL_DEAD_FOR_LOCAL (r) = 0;
13056 DECL_INITIALIZED_P (r) = 0;
13057 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13058 if (type == error_mark_node)
13059 RETURN (error_mark_node);
13060 if (TREE_CODE (type) == FUNCTION_TYPE)
13062 /* It may seem that this case cannot occur, since:
13064 typedef void f();
13065 void g() { f x; }
13067 declares a function, not a variable. However:
13069 typedef void f();
13070 template <typename T> void g() { T t; }
13071 template void g<f>();
13073 is an attempt to declare a variable with function
13074 type. */
13075 error ("variable %qD has function type",
13076 /* R is not yet sufficiently initialized, so we
13077 just use its name. */
13078 DECL_NAME (r));
13079 RETURN (error_mark_node);
13081 type = complete_type (type);
13082 /* Wait until cp_finish_decl to set this again, to handle
13083 circular dependency (template/instantiate6.C). */
13084 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13085 type = check_var_type (DECL_NAME (r), type);
13087 if (DECL_HAS_VALUE_EXPR_P (t))
13089 tree ve = DECL_VALUE_EXPR (t);
13090 ve = tsubst_expr (ve, args, complain, in_decl,
13091 /*constant_expression_p=*/false);
13092 if (REFERENCE_REF_P (ve))
13094 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13095 ve = TREE_OPERAND (ve, 0);
13097 SET_DECL_VALUE_EXPR (r, ve);
13099 if (CP_DECL_THREAD_LOCAL_P (r)
13100 && !processing_template_decl)
13101 set_decl_tls_model (r, decl_default_tls_model (r));
13103 else if (DECL_SELF_REFERENCE_P (t))
13104 SET_DECL_SELF_REFERENCE_P (r);
13105 TREE_TYPE (r) = type;
13106 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13107 DECL_CONTEXT (r) = ctx;
13108 /* Clear out the mangled name and RTL for the instantiation. */
13109 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13110 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13111 SET_DECL_RTL (r, NULL);
13112 /* The initializer must not be expanded until it is required;
13113 see [temp.inst]. */
13114 DECL_INITIAL (r) = NULL_TREE;
13115 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13116 if (VAR_P (r))
13118 SET_DECL_MODE (r, VOIDmode);
13120 /* Possibly limit visibility based on template args. */
13121 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13122 if (DECL_VISIBILITY_SPECIFIED (t))
13124 DECL_VISIBILITY_SPECIFIED (r) = 0;
13125 DECL_ATTRIBUTES (r)
13126 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13128 determine_visibility (r);
13131 if (!local_p)
13133 /* A static data member declaration is always marked
13134 external when it is declared in-class, even if an
13135 initializer is present. We mimic the non-template
13136 processing here. */
13137 DECL_EXTERNAL (r) = 1;
13138 if (DECL_NAMESPACE_SCOPE_P (t))
13139 DECL_NOT_REALLY_EXTERN (r) = 1;
13141 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13142 SET_DECL_IMPLICIT_INSTANTIATION (r);
13143 register_specialization (r, gen_tmpl, argvec, false, hash);
13145 else
13147 if (DECL_LANG_SPECIFIC (r))
13148 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13149 if (!cp_unevaluated_operand)
13150 register_local_specialization (r, t);
13153 DECL_CHAIN (r) = NULL_TREE;
13155 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13156 /*flags=*/0,
13157 args, complain, in_decl);
13159 /* Preserve a typedef that names a type. */
13160 if (is_typedef_decl (r) && type != error_mark_node)
13162 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13163 set_underlying_type (r);
13164 if (TYPE_DECL_ALIAS_P (r))
13165 /* An alias template specialization can be dependent
13166 even if its underlying type is not. */
13167 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13170 layout_decl (r, 0);
13172 break;
13174 default:
13175 gcc_unreachable ();
13177 #undef RETURN
13179 out:
13180 /* Restore the file and line information. */
13181 input_location = saved_loc;
13183 return r;
13186 /* Substitute into the ARG_TYPES of a function type.
13187 If END is a TREE_CHAIN, leave it and any following types
13188 un-substituted. */
13190 static tree
13191 tsubst_arg_types (tree arg_types,
13192 tree args,
13193 tree end,
13194 tsubst_flags_t complain,
13195 tree in_decl)
13197 tree remaining_arg_types;
13198 tree type = NULL_TREE;
13199 int i = 1;
13200 tree expanded_args = NULL_TREE;
13201 tree default_arg;
13203 if (!arg_types || arg_types == void_list_node || arg_types == end)
13204 return arg_types;
13206 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13207 args, end, complain, in_decl);
13208 if (remaining_arg_types == error_mark_node)
13209 return error_mark_node;
13211 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13213 /* For a pack expansion, perform substitution on the
13214 entire expression. Later on, we'll handle the arguments
13215 one-by-one. */
13216 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13217 args, complain, in_decl);
13219 if (TREE_CODE (expanded_args) == TREE_VEC)
13220 /* So that we'll spin through the parameters, one by one. */
13221 i = TREE_VEC_LENGTH (expanded_args);
13222 else
13224 /* We only partially substituted into the parameter
13225 pack. Our type is TYPE_PACK_EXPANSION. */
13226 type = expanded_args;
13227 expanded_args = NULL_TREE;
13231 while (i > 0) {
13232 --i;
13234 if (expanded_args)
13235 type = TREE_VEC_ELT (expanded_args, i);
13236 else if (!type)
13237 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13239 if (type == error_mark_node)
13240 return error_mark_node;
13241 if (VOID_TYPE_P (type))
13243 if (complain & tf_error)
13245 error ("invalid parameter type %qT", type);
13246 if (in_decl)
13247 error ("in declaration %q+D", in_decl);
13249 return error_mark_node;
13251 /* DR 657. */
13252 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13253 return error_mark_node;
13255 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13256 top-level qualifiers as required. */
13257 type = cv_unqualified (type_decays_to (type));
13259 /* We do not substitute into default arguments here. The standard
13260 mandates that they be instantiated only when needed, which is
13261 done in build_over_call. */
13262 default_arg = TREE_PURPOSE (arg_types);
13264 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13266 /* We've instantiated a template before its default arguments
13267 have been parsed. This can happen for a nested template
13268 class, and is not an error unless we require the default
13269 argument in a call of this function. */
13270 remaining_arg_types =
13271 tree_cons (default_arg, type, remaining_arg_types);
13272 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13274 else
13275 remaining_arg_types =
13276 hash_tree_cons (default_arg, type, remaining_arg_types);
13279 return remaining_arg_types;
13282 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13283 *not* handle the exception-specification for FNTYPE, because the
13284 initial substitution of explicitly provided template parameters
13285 during argument deduction forbids substitution into the
13286 exception-specification:
13288 [temp.deduct]
13290 All references in the function type of the function template to the
13291 corresponding template parameters are replaced by the specified tem-
13292 plate argument values. If a substitution in a template parameter or
13293 in the function type of the function template results in an invalid
13294 type, type deduction fails. [Note: The equivalent substitution in
13295 exception specifications is done only when the function is instanti-
13296 ated, at which point a program is ill-formed if the substitution
13297 results in an invalid type.] */
13299 static tree
13300 tsubst_function_type (tree t,
13301 tree args,
13302 tsubst_flags_t complain,
13303 tree in_decl)
13305 tree return_type;
13306 tree arg_types = NULL_TREE;
13307 tree fntype;
13309 /* The TYPE_CONTEXT is not used for function/method types. */
13310 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13312 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13313 failure. */
13314 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13316 if (late_return_type_p)
13318 /* Substitute the argument types. */
13319 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13320 complain, in_decl);
13321 if (arg_types == error_mark_node)
13322 return error_mark_node;
13324 tree save_ccp = current_class_ptr;
13325 tree save_ccr = current_class_ref;
13326 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13327 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13328 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13329 if (do_inject)
13331 /* DR 1207: 'this' is in scope in the trailing return type. */
13332 inject_this_parameter (this_type, cp_type_quals (this_type));
13335 /* Substitute the return type. */
13336 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13338 if (do_inject)
13340 current_class_ptr = save_ccp;
13341 current_class_ref = save_ccr;
13344 else
13345 /* Substitute the return type. */
13346 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13348 if (return_type == error_mark_node)
13349 return error_mark_node;
13350 /* DR 486 clarifies that creation of a function type with an
13351 invalid return type is a deduction failure. */
13352 if (TREE_CODE (return_type) == ARRAY_TYPE
13353 || TREE_CODE (return_type) == FUNCTION_TYPE)
13355 if (complain & tf_error)
13357 if (TREE_CODE (return_type) == ARRAY_TYPE)
13358 error ("function returning an array");
13359 else
13360 error ("function returning a function");
13362 return error_mark_node;
13364 /* And DR 657. */
13365 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13366 return error_mark_node;
13368 if (!late_return_type_p)
13370 /* Substitute the argument types. */
13371 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13372 complain, in_decl);
13373 if (arg_types == error_mark_node)
13374 return error_mark_node;
13377 /* Construct a new type node and return it. */
13378 if (TREE_CODE (t) == FUNCTION_TYPE)
13380 fntype = build_function_type (return_type, arg_types);
13381 fntype = apply_memfn_quals (fntype,
13382 type_memfn_quals (t),
13383 type_memfn_rqual (t));
13385 else
13387 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13388 /* Don't pick up extra function qualifiers from the basetype. */
13389 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13390 if (! MAYBE_CLASS_TYPE_P (r))
13392 /* [temp.deduct]
13394 Type deduction may fail for any of the following
13395 reasons:
13397 -- Attempting to create "pointer to member of T" when T
13398 is not a class type. */
13399 if (complain & tf_error)
13400 error ("creating pointer to member function of non-class type %qT",
13402 return error_mark_node;
13405 fntype = build_method_type_directly (r, return_type,
13406 TREE_CHAIN (arg_types));
13407 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13409 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13411 if (late_return_type_p)
13412 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13414 return fntype;
13417 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13418 ARGS into that specification, and return the substituted
13419 specification. If there is no specification, return NULL_TREE. */
13421 static tree
13422 tsubst_exception_specification (tree fntype,
13423 tree args,
13424 tsubst_flags_t complain,
13425 tree in_decl,
13426 bool defer_ok)
13428 tree specs;
13429 tree new_specs;
13431 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13432 new_specs = NULL_TREE;
13433 if (specs && TREE_PURPOSE (specs))
13435 /* A noexcept-specifier. */
13436 tree expr = TREE_PURPOSE (specs);
13437 if (TREE_CODE (expr) == INTEGER_CST)
13438 new_specs = expr;
13439 else if (defer_ok)
13441 /* Defer instantiation of noexcept-specifiers to avoid
13442 excessive instantiations (c++/49107). */
13443 new_specs = make_node (DEFERRED_NOEXCEPT);
13444 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13446 /* We already partially instantiated this member template,
13447 so combine the new args with the old. */
13448 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13449 = DEFERRED_NOEXCEPT_PATTERN (expr);
13450 DEFERRED_NOEXCEPT_ARGS (new_specs)
13451 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13453 else
13455 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13456 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13459 else
13460 new_specs = tsubst_copy_and_build
13461 (expr, args, complain, in_decl, /*function_p=*/false,
13462 /*integral_constant_expression_p=*/true);
13463 new_specs = build_noexcept_spec (new_specs, complain);
13465 else if (specs)
13467 if (! TREE_VALUE (specs))
13468 new_specs = specs;
13469 else
13470 while (specs)
13472 tree spec;
13473 int i, len = 1;
13474 tree expanded_specs = NULL_TREE;
13476 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13478 /* Expand the pack expansion type. */
13479 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13480 args, complain,
13481 in_decl);
13483 if (expanded_specs == error_mark_node)
13484 return error_mark_node;
13485 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13486 len = TREE_VEC_LENGTH (expanded_specs);
13487 else
13489 /* We're substituting into a member template, so
13490 we got a TYPE_PACK_EXPANSION back. Add that
13491 expansion and move on. */
13492 gcc_assert (TREE_CODE (expanded_specs)
13493 == TYPE_PACK_EXPANSION);
13494 new_specs = add_exception_specifier (new_specs,
13495 expanded_specs,
13496 complain);
13497 specs = TREE_CHAIN (specs);
13498 continue;
13502 for (i = 0; i < len; ++i)
13504 if (expanded_specs)
13505 spec = TREE_VEC_ELT (expanded_specs, i);
13506 else
13507 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13508 if (spec == error_mark_node)
13509 return spec;
13510 new_specs = add_exception_specifier (new_specs, spec,
13511 complain);
13514 specs = TREE_CHAIN (specs);
13517 return new_specs;
13520 /* Take the tree structure T and replace template parameters used
13521 therein with the argument vector ARGS. IN_DECL is an associated
13522 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13523 Issue error and warning messages under control of COMPLAIN. Note
13524 that we must be relatively non-tolerant of extensions here, in
13525 order to preserve conformance; if we allow substitutions that
13526 should not be allowed, we may allow argument deductions that should
13527 not succeed, and therefore report ambiguous overload situations
13528 where there are none. In theory, we could allow the substitution,
13529 but indicate that it should have failed, and allow our caller to
13530 make sure that the right thing happens, but we don't try to do this
13531 yet.
13533 This function is used for dealing with types, decls and the like;
13534 for expressions, use tsubst_expr or tsubst_copy. */
13536 tree
13537 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13539 enum tree_code code;
13540 tree type, r = NULL_TREE;
13542 if (t == NULL_TREE || t == error_mark_node
13543 || t == integer_type_node
13544 || t == void_type_node
13545 || t == char_type_node
13546 || t == unknown_type_node
13547 || TREE_CODE (t) == NAMESPACE_DECL
13548 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13549 return t;
13551 if (DECL_P (t))
13552 return tsubst_decl (t, args, complain);
13554 if (args == NULL_TREE)
13555 return t;
13557 code = TREE_CODE (t);
13559 if (code == IDENTIFIER_NODE)
13560 type = IDENTIFIER_TYPE_VALUE (t);
13561 else
13562 type = TREE_TYPE (t);
13564 gcc_assert (type != unknown_type_node);
13566 /* Reuse typedefs. We need to do this to handle dependent attributes,
13567 such as attribute aligned. */
13568 if (TYPE_P (t)
13569 && typedef_variant_p (t))
13571 tree decl = TYPE_NAME (t);
13573 if (alias_template_specialization_p (t))
13575 /* DECL represents an alias template and we want to
13576 instantiate it. */
13577 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13578 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13579 r = instantiate_alias_template (tmpl, gen_args, complain);
13581 else if (DECL_CLASS_SCOPE_P (decl)
13582 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13583 && uses_template_parms (DECL_CONTEXT (decl)))
13585 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13586 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13587 r = retrieve_specialization (tmpl, gen_args, 0);
13589 else if (DECL_FUNCTION_SCOPE_P (decl)
13590 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13591 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13592 r = retrieve_local_specialization (decl);
13593 else
13594 /* The typedef is from a non-template context. */
13595 return t;
13597 if (r)
13599 r = TREE_TYPE (r);
13600 r = cp_build_qualified_type_real
13601 (r, cp_type_quals (t) | cp_type_quals (r),
13602 complain | tf_ignore_bad_quals);
13603 return r;
13605 else
13607 /* We don't have an instantiation yet, so drop the typedef. */
13608 int quals = cp_type_quals (t);
13609 t = DECL_ORIGINAL_TYPE (decl);
13610 t = cp_build_qualified_type_real (t, quals,
13611 complain | tf_ignore_bad_quals);
13615 bool fndecl_type = (complain & tf_fndecl_type);
13616 complain &= ~tf_fndecl_type;
13618 if (type
13619 && code != TYPENAME_TYPE
13620 && code != TEMPLATE_TYPE_PARM
13621 && code != TEMPLATE_PARM_INDEX
13622 && code != IDENTIFIER_NODE
13623 && code != FUNCTION_TYPE
13624 && code != METHOD_TYPE)
13625 type = tsubst (type, args, complain, in_decl);
13626 if (type == error_mark_node)
13627 return error_mark_node;
13629 switch (code)
13631 case RECORD_TYPE:
13632 case UNION_TYPE:
13633 case ENUMERAL_TYPE:
13634 return tsubst_aggr_type (t, args, complain, in_decl,
13635 /*entering_scope=*/0);
13637 case ERROR_MARK:
13638 case IDENTIFIER_NODE:
13639 case VOID_TYPE:
13640 case REAL_TYPE:
13641 case COMPLEX_TYPE:
13642 case VECTOR_TYPE:
13643 case BOOLEAN_TYPE:
13644 case NULLPTR_TYPE:
13645 case LANG_TYPE:
13646 return t;
13648 case INTEGER_TYPE:
13649 if (t == integer_type_node)
13650 return t;
13652 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13653 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13654 return t;
13657 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13659 max = tsubst_expr (omax, args, complain, in_decl,
13660 /*integral_constant_expression_p=*/false);
13662 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13663 needed. */
13664 if (TREE_CODE (max) == NOP_EXPR
13665 && TREE_SIDE_EFFECTS (omax)
13666 && !TREE_TYPE (max))
13667 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13669 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13670 with TREE_SIDE_EFFECTS that indicates this is not an integral
13671 constant expression. */
13672 if (processing_template_decl
13673 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13675 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13676 TREE_SIDE_EFFECTS (max) = 1;
13679 return compute_array_index_type (NULL_TREE, max, complain);
13682 case TEMPLATE_TYPE_PARM:
13683 case TEMPLATE_TEMPLATE_PARM:
13684 case BOUND_TEMPLATE_TEMPLATE_PARM:
13685 case TEMPLATE_PARM_INDEX:
13687 int idx;
13688 int level;
13689 int levels;
13690 tree arg = NULL_TREE;
13692 /* Early in template argument deduction substitution, we don't
13693 want to reduce the level of 'auto', or it will be confused
13694 with a normal template parm in subsequent deduction. */
13695 if (is_auto (t) && (complain & tf_partial))
13696 return t;
13698 r = NULL_TREE;
13700 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13701 template_parm_level_and_index (t, &level, &idx);
13703 levels = TMPL_ARGS_DEPTH (args);
13704 if (level <= levels
13705 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13707 arg = TMPL_ARG (args, level, idx);
13709 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13711 /* See through ARGUMENT_PACK_SELECT arguments. */
13712 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13713 /* If the selected argument is an expansion E, that most
13714 likely means we were called from
13715 gen_elem_of_pack_expansion_instantiation during the
13716 substituting of pack an argument pack (which Ith
13717 element is a pack expansion, where I is
13718 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13719 In this case, the Ith element resulting from this
13720 substituting is going to be a pack expansion, which
13721 pattern is the pattern of E. Let's return the
13722 pattern of E, and
13723 gen_elem_of_pack_expansion_instantiation will
13724 build the resulting pack expansion from it. */
13725 if (PACK_EXPANSION_P (arg))
13727 /* Make sure we aren't throwing away arg info. */
13728 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13729 arg = PACK_EXPANSION_PATTERN (arg);
13734 if (arg == error_mark_node)
13735 return error_mark_node;
13736 else if (arg != NULL_TREE)
13738 if (ARGUMENT_PACK_P (arg))
13739 /* If ARG is an argument pack, we don't actually want to
13740 perform a substitution here, because substitutions
13741 for argument packs are only done
13742 element-by-element. We can get to this point when
13743 substituting the type of a non-type template
13744 parameter pack, when that type actually contains
13745 template parameter packs from an outer template, e.g.,
13747 template<typename... Types> struct A {
13748 template<Types... Values> struct B { };
13749 }; */
13750 return t;
13752 if (code == TEMPLATE_TYPE_PARM)
13754 int quals;
13755 gcc_assert (TYPE_P (arg));
13757 quals = cp_type_quals (arg) | cp_type_quals (t);
13759 return cp_build_qualified_type_real
13760 (arg, quals, complain | tf_ignore_bad_quals);
13762 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13764 /* We are processing a type constructed from a
13765 template template parameter. */
13766 tree argvec = tsubst (TYPE_TI_ARGS (t),
13767 args, complain, in_decl);
13768 if (argvec == error_mark_node)
13769 return error_mark_node;
13771 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13772 || TREE_CODE (arg) == TEMPLATE_DECL
13773 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13775 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13776 /* Consider this code:
13778 template <template <class> class Template>
13779 struct Internal {
13780 template <class Arg> using Bind = Template<Arg>;
13783 template <template <class> class Template, class Arg>
13784 using Instantiate = Template<Arg>; //#0
13786 template <template <class> class Template,
13787 class Argument>
13788 using Bind =
13789 Instantiate<Internal<Template>::template Bind,
13790 Argument>; //#1
13792 When #1 is parsed, the
13793 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13794 parameter `Template' in #0 matches the
13795 UNBOUND_CLASS_TEMPLATE representing the argument
13796 `Internal<Template>::template Bind'; We then want
13797 to assemble the type `Bind<Argument>' that can't
13798 be fully created right now, because
13799 `Internal<Template>' not being complete, the Bind
13800 template cannot be looked up in that context. So
13801 we need to "store" `Bind<Argument>' for later
13802 when the context of Bind becomes complete. Let's
13803 store that in a TYPENAME_TYPE. */
13804 return make_typename_type (TYPE_CONTEXT (arg),
13805 build_nt (TEMPLATE_ID_EXPR,
13806 TYPE_IDENTIFIER (arg),
13807 argvec),
13808 typename_type,
13809 complain);
13811 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13812 are resolving nested-types in the signature of a
13813 member function templates. Otherwise ARG is a
13814 TEMPLATE_DECL and is the real template to be
13815 instantiated. */
13816 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13817 arg = TYPE_NAME (arg);
13819 r = lookup_template_class (arg,
13820 argvec, in_decl,
13821 DECL_CONTEXT (arg),
13822 /*entering_scope=*/0,
13823 complain);
13824 return cp_build_qualified_type_real
13825 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13827 else if (code == TEMPLATE_TEMPLATE_PARM)
13828 return arg;
13829 else
13830 /* TEMPLATE_PARM_INDEX. */
13831 return convert_from_reference (unshare_expr (arg));
13834 if (level == 1)
13835 /* This can happen during the attempted tsubst'ing in
13836 unify. This means that we don't yet have any information
13837 about the template parameter in question. */
13838 return t;
13840 /* If we get here, we must have been looking at a parm for a
13841 more deeply nested template. Make a new version of this
13842 template parameter, but with a lower level. */
13843 switch (code)
13845 case TEMPLATE_TYPE_PARM:
13846 case TEMPLATE_TEMPLATE_PARM:
13847 case BOUND_TEMPLATE_TEMPLATE_PARM:
13848 if (cp_type_quals (t))
13850 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13851 r = cp_build_qualified_type_real
13852 (r, cp_type_quals (t),
13853 complain | (code == TEMPLATE_TYPE_PARM
13854 ? tf_ignore_bad_quals : 0));
13856 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13857 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13858 && (r = (TEMPLATE_PARM_DESCENDANTS
13859 (TEMPLATE_TYPE_PARM_INDEX (t))))
13860 && (r = TREE_TYPE (r))
13861 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13862 /* Break infinite recursion when substituting the constraints
13863 of a constrained placeholder. */;
13864 else
13866 r = copy_type (t);
13867 TEMPLATE_TYPE_PARM_INDEX (r)
13868 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13869 r, levels, args, complain);
13870 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13871 TYPE_MAIN_VARIANT (r) = r;
13872 TYPE_POINTER_TO (r) = NULL_TREE;
13873 TYPE_REFERENCE_TO (r) = NULL_TREE;
13875 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13877 /* Propagate constraints on placeholders. */
13878 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13879 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13880 = tsubst_constraint (constr, args, complain, in_decl);
13881 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13883 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13884 pl = tsubst (pl, args, complain, in_decl);
13885 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13889 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13890 /* We have reduced the level of the template
13891 template parameter, but not the levels of its
13892 template parameters, so canonical_type_parameter
13893 will not be able to find the canonical template
13894 template parameter for this level. Thus, we
13895 require structural equality checking to compare
13896 TEMPLATE_TEMPLATE_PARMs. */
13897 SET_TYPE_STRUCTURAL_EQUALITY (r);
13898 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13899 SET_TYPE_STRUCTURAL_EQUALITY (r);
13900 else
13901 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13903 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13905 tree tinfo = TYPE_TEMPLATE_INFO (t);
13906 /* We might need to substitute into the types of non-type
13907 template parameters. */
13908 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13909 complain, in_decl);
13910 if (tmpl == error_mark_node)
13911 return error_mark_node;
13912 tree argvec = tsubst (TI_ARGS (tinfo), args,
13913 complain, in_decl);
13914 if (argvec == error_mark_node)
13915 return error_mark_node;
13917 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13918 = build_template_info (tmpl, argvec);
13921 break;
13923 case TEMPLATE_PARM_INDEX:
13924 /* OK, now substitute the type of the non-type parameter. We
13925 couldn't do it earlier because it might be an auto parameter,
13926 and we wouldn't need to if we had an argument. */
13927 type = tsubst (type, args, complain, in_decl);
13928 if (type == error_mark_node)
13929 return error_mark_node;
13930 r = reduce_template_parm_level (t, type, levels, args, complain);
13931 break;
13933 default:
13934 gcc_unreachable ();
13937 return r;
13940 case TREE_LIST:
13942 tree purpose, value, chain;
13944 if (t == void_list_node)
13945 return t;
13947 purpose = TREE_PURPOSE (t);
13948 if (purpose)
13950 purpose = tsubst (purpose, args, complain, in_decl);
13951 if (purpose == error_mark_node)
13952 return error_mark_node;
13954 value = TREE_VALUE (t);
13955 if (value)
13957 value = tsubst (value, args, complain, in_decl);
13958 if (value == error_mark_node)
13959 return error_mark_node;
13961 chain = TREE_CHAIN (t);
13962 if (chain && chain != void_type_node)
13964 chain = tsubst (chain, args, complain, in_decl);
13965 if (chain == error_mark_node)
13966 return error_mark_node;
13968 if (purpose == TREE_PURPOSE (t)
13969 && value == TREE_VALUE (t)
13970 && chain == TREE_CHAIN (t))
13971 return t;
13972 return hash_tree_cons (purpose, value, chain);
13975 case TREE_BINFO:
13976 /* We should never be tsubsting a binfo. */
13977 gcc_unreachable ();
13979 case TREE_VEC:
13980 /* A vector of template arguments. */
13981 gcc_assert (!type);
13982 return tsubst_template_args (t, args, complain, in_decl);
13984 case POINTER_TYPE:
13985 case REFERENCE_TYPE:
13987 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13988 return t;
13990 /* [temp.deduct]
13992 Type deduction may fail for any of the following
13993 reasons:
13995 -- Attempting to create a pointer to reference type.
13996 -- Attempting to create a reference to a reference type or
13997 a reference to void.
13999 Core issue 106 says that creating a reference to a reference
14000 during instantiation is no longer a cause for failure. We
14001 only enforce this check in strict C++98 mode. */
14002 if ((TREE_CODE (type) == REFERENCE_TYPE
14003 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14004 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14006 static location_t last_loc;
14008 /* We keep track of the last time we issued this error
14009 message to avoid spewing a ton of messages during a
14010 single bad template instantiation. */
14011 if (complain & tf_error
14012 && last_loc != input_location)
14014 if (VOID_TYPE_P (type))
14015 error ("forming reference to void");
14016 else if (code == POINTER_TYPE)
14017 error ("forming pointer to reference type %qT", type);
14018 else
14019 error ("forming reference to reference type %qT", type);
14020 last_loc = input_location;
14023 return error_mark_node;
14025 else if (TREE_CODE (type) == FUNCTION_TYPE
14026 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14027 || type_memfn_rqual (type) != REF_QUAL_NONE))
14029 if (complain & tf_error)
14031 if (code == POINTER_TYPE)
14032 error ("forming pointer to qualified function type %qT",
14033 type);
14034 else
14035 error ("forming reference to qualified function type %qT",
14036 type);
14038 return error_mark_node;
14040 else if (code == POINTER_TYPE)
14042 r = build_pointer_type (type);
14043 if (TREE_CODE (type) == METHOD_TYPE)
14044 r = build_ptrmemfunc_type (r);
14046 else if (TREE_CODE (type) == REFERENCE_TYPE)
14047 /* In C++0x, during template argument substitution, when there is an
14048 attempt to create a reference to a reference type, reference
14049 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14051 "If a template-argument for a template-parameter T names a type
14052 that is a reference to a type A, an attempt to create the type
14053 'lvalue reference to cv T' creates the type 'lvalue reference to
14054 A,' while an attempt to create the type type rvalue reference to
14055 cv T' creates the type T"
14057 r = cp_build_reference_type
14058 (TREE_TYPE (type),
14059 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14060 else
14061 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14062 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14064 if (r != error_mark_node)
14065 /* Will this ever be needed for TYPE_..._TO values? */
14066 layout_type (r);
14068 return r;
14070 case OFFSET_TYPE:
14072 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14073 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14075 /* [temp.deduct]
14077 Type deduction may fail for any of the following
14078 reasons:
14080 -- Attempting to create "pointer to member of T" when T
14081 is not a class type. */
14082 if (complain & tf_error)
14083 error ("creating pointer to member of non-class type %qT", r);
14084 return error_mark_node;
14086 if (TREE_CODE (type) == REFERENCE_TYPE)
14088 if (complain & tf_error)
14089 error ("creating pointer to member reference type %qT", type);
14090 return error_mark_node;
14092 if (VOID_TYPE_P (type))
14094 if (complain & tf_error)
14095 error ("creating pointer to member of type void");
14096 return error_mark_node;
14098 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14099 if (TREE_CODE (type) == FUNCTION_TYPE)
14101 /* The type of the implicit object parameter gets its
14102 cv-qualifiers from the FUNCTION_TYPE. */
14103 tree memptr;
14104 tree method_type
14105 = build_memfn_type (type, r, type_memfn_quals (type),
14106 type_memfn_rqual (type));
14107 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14108 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14109 complain);
14111 else
14112 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14113 cp_type_quals (t),
14114 complain);
14116 case FUNCTION_TYPE:
14117 case METHOD_TYPE:
14119 tree fntype;
14120 tree specs;
14121 fntype = tsubst_function_type (t, args, complain, in_decl);
14122 if (fntype == error_mark_node)
14123 return error_mark_node;
14125 /* Substitute the exception specification. */
14126 specs = tsubst_exception_specification (t, args, complain, in_decl,
14127 /*defer_ok*/fndecl_type);
14128 if (specs == error_mark_node)
14129 return error_mark_node;
14130 if (specs)
14131 fntype = build_exception_variant (fntype, specs);
14132 return fntype;
14134 case ARRAY_TYPE:
14136 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14137 if (domain == error_mark_node)
14138 return error_mark_node;
14140 /* As an optimization, we avoid regenerating the array type if
14141 it will obviously be the same as T. */
14142 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14143 return t;
14145 /* These checks should match the ones in create_array_type_for_decl.
14147 [temp.deduct]
14149 The deduction may fail for any of the following reasons:
14151 -- Attempting to create an array with an element type that
14152 is void, a function type, or a reference type, or [DR337]
14153 an abstract class type. */
14154 if (VOID_TYPE_P (type)
14155 || TREE_CODE (type) == FUNCTION_TYPE
14156 || (TREE_CODE (type) == ARRAY_TYPE
14157 && TYPE_DOMAIN (type) == NULL_TREE)
14158 || TREE_CODE (type) == REFERENCE_TYPE)
14160 if (complain & tf_error)
14161 error ("creating array of %qT", type);
14162 return error_mark_node;
14165 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14166 return error_mark_node;
14168 r = build_cplus_array_type (type, domain);
14170 if (TYPE_USER_ALIGN (t))
14172 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14173 TYPE_USER_ALIGN (r) = 1;
14176 return r;
14179 case TYPENAME_TYPE:
14181 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14182 in_decl, /*entering_scope=*/1);
14183 if (ctx == error_mark_node)
14184 return error_mark_node;
14186 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14187 complain, in_decl);
14188 if (f == error_mark_node)
14189 return error_mark_node;
14191 if (!MAYBE_CLASS_TYPE_P (ctx))
14193 if (complain & tf_error)
14194 error ("%qT is not a class, struct, or union type", ctx);
14195 return error_mark_node;
14197 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14199 /* Normally, make_typename_type does not require that the CTX
14200 have complete type in order to allow things like:
14202 template <class T> struct S { typename S<T>::X Y; };
14204 But, such constructs have already been resolved by this
14205 point, so here CTX really should have complete type, unless
14206 it's a partial instantiation. */
14207 ctx = complete_type (ctx);
14208 if (!COMPLETE_TYPE_P (ctx))
14210 if (complain & tf_error)
14211 cxx_incomplete_type_error (NULL_TREE, ctx);
14212 return error_mark_node;
14216 f = make_typename_type (ctx, f, typename_type,
14217 complain | tf_keep_type_decl);
14218 if (f == error_mark_node)
14219 return f;
14220 if (TREE_CODE (f) == TYPE_DECL)
14222 complain |= tf_ignore_bad_quals;
14223 f = TREE_TYPE (f);
14226 if (TREE_CODE (f) != TYPENAME_TYPE)
14228 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14230 if (complain & tf_error)
14231 error ("%qT resolves to %qT, which is not an enumeration type",
14232 t, f);
14233 else
14234 return error_mark_node;
14236 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14238 if (complain & tf_error)
14239 error ("%qT resolves to %qT, which is is not a class type",
14240 t, f);
14241 else
14242 return error_mark_node;
14246 return cp_build_qualified_type_real
14247 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14250 case UNBOUND_CLASS_TEMPLATE:
14252 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14253 in_decl, /*entering_scope=*/1);
14254 tree name = TYPE_IDENTIFIER (t);
14255 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14257 if (ctx == error_mark_node || name == error_mark_node)
14258 return error_mark_node;
14260 if (parm_list)
14261 parm_list = tsubst_template_parms (parm_list, args, complain);
14262 return make_unbound_class_template (ctx, name, parm_list, complain);
14265 case TYPEOF_TYPE:
14267 tree type;
14269 ++cp_unevaluated_operand;
14270 ++c_inhibit_evaluation_warnings;
14272 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14273 complain, in_decl,
14274 /*integral_constant_expression_p=*/false);
14276 --cp_unevaluated_operand;
14277 --c_inhibit_evaluation_warnings;
14279 type = finish_typeof (type);
14280 return cp_build_qualified_type_real (type,
14281 cp_type_quals (t)
14282 | cp_type_quals (type),
14283 complain);
14286 case DECLTYPE_TYPE:
14288 tree type;
14290 ++cp_unevaluated_operand;
14291 ++c_inhibit_evaluation_warnings;
14293 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14294 complain|tf_decltype, in_decl,
14295 /*function_p*/false,
14296 /*integral_constant_expression*/false);
14298 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14300 if (type == NULL_TREE)
14302 if (complain & tf_error)
14303 error ("empty initializer in lambda init-capture");
14304 type = error_mark_node;
14306 else if (TREE_CODE (type) == TREE_LIST)
14307 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14310 --cp_unevaluated_operand;
14311 --c_inhibit_evaluation_warnings;
14313 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14314 type = lambda_capture_field_type (type,
14315 DECLTYPE_FOR_INIT_CAPTURE (t),
14316 DECLTYPE_FOR_REF_CAPTURE (t));
14317 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14318 type = lambda_proxy_type (type);
14319 else
14321 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14322 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14323 && EXPR_P (type))
14324 /* In a template ~id could be either a complement expression
14325 or an unqualified-id naming a destructor; if instantiating
14326 it produces an expression, it's not an id-expression or
14327 member access. */
14328 id = false;
14329 type = finish_decltype_type (type, id, complain);
14331 return cp_build_qualified_type_real (type,
14332 cp_type_quals (t)
14333 | cp_type_quals (type),
14334 complain | tf_ignore_bad_quals);
14337 case UNDERLYING_TYPE:
14339 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14340 complain, in_decl);
14341 return finish_underlying_type (type);
14344 case TYPE_ARGUMENT_PACK:
14345 case NONTYPE_ARGUMENT_PACK:
14347 tree r;
14349 if (code == NONTYPE_ARGUMENT_PACK)
14350 r = make_node (code);
14351 else
14352 r = cxx_make_type (code);
14354 tree pack_args = ARGUMENT_PACK_ARGS (t);
14355 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14356 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14358 return r;
14361 case VOID_CST:
14362 case INTEGER_CST:
14363 case REAL_CST:
14364 case STRING_CST:
14365 case PLUS_EXPR:
14366 case MINUS_EXPR:
14367 case NEGATE_EXPR:
14368 case NOP_EXPR:
14369 case INDIRECT_REF:
14370 case ADDR_EXPR:
14371 case CALL_EXPR:
14372 case ARRAY_REF:
14373 case SCOPE_REF:
14374 /* We should use one of the expression tsubsts for these codes. */
14375 gcc_unreachable ();
14377 default:
14378 sorry ("use of %qs in template", get_tree_code_name (code));
14379 return error_mark_node;
14383 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14384 expression on the left-hand side of the "." or "->" operator. A
14385 baselink indicates a function from a base class. Both the
14386 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14387 bases of the template class, rather than the instantiated class.
14388 In addition, lookups that were not ambiguous before may be
14389 ambiguous now. Therefore, we perform the lookup again. */
14391 static tree
14392 tsubst_baselink (tree baselink, tree object_type,
14393 tree args, tsubst_flags_t complain, tree in_decl)
14395 bool qualified = BASELINK_QUALIFIED_P (baselink);
14397 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14398 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14400 tree optype = BASELINK_OPTYPE (baselink);
14401 optype = tsubst (optype, args, complain, in_decl);
14403 tree template_args = NULL_TREE;
14404 bool template_id_p = false;
14405 tree fns = BASELINK_FUNCTIONS (baselink);
14406 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14408 template_id_p = true;
14409 template_args = TREE_OPERAND (fns, 1);
14410 fns = TREE_OPERAND (fns, 0);
14411 if (template_args)
14412 template_args = tsubst_template_args (template_args, args,
14413 complain, in_decl);
14416 tree name = OVL_NAME (fns);
14417 if (IDENTIFIER_CONV_OP_P (name))
14418 name = make_conv_op_name (optype);
14420 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14421 if (!baselink)
14423 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14424 error ("cannot call constructor %<%T::%D%> directly",
14425 qualifying_scope, name);
14426 return error_mark_node;
14429 /* If lookup found a single function, mark it as used at this point.
14430 (If it lookup found multiple functions the one selected later by
14431 overload resolution will be marked as used at that point.) */
14432 if (BASELINK_P (baselink))
14433 fns = BASELINK_FUNCTIONS (baselink);
14434 if (!template_id_p && !really_overloaded_fn (fns)
14435 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14436 return error_mark_node;
14438 if (BASELINK_P (baselink))
14440 /* Add back the template arguments, if present. */
14441 if (template_id_p)
14442 BASELINK_FUNCTIONS (baselink)
14443 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14444 BASELINK_FUNCTIONS (baselink), template_args);
14446 /* Update the conversion operator type. */
14447 BASELINK_OPTYPE (baselink) = optype;
14450 if (!object_type)
14451 object_type = current_class_type;
14453 if (qualified || name == complete_dtor_identifier)
14455 baselink = adjust_result_of_qualified_name_lookup (baselink,
14456 qualifying_scope,
14457 object_type);
14458 if (!qualified)
14459 /* We need to call adjust_result_of_qualified_name_lookup in case the
14460 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14461 so that we still get virtual function binding. */
14462 BASELINK_QUALIFIED_P (baselink) = false;
14465 return baselink;
14468 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14469 true if the qualified-id will be a postfix-expression in-and-of
14470 itself; false if more of the postfix-expression follows the
14471 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14472 of "&". */
14474 static tree
14475 tsubst_qualified_id (tree qualified_id, tree args,
14476 tsubst_flags_t complain, tree in_decl,
14477 bool done, bool address_p)
14479 tree expr;
14480 tree scope;
14481 tree name;
14482 bool is_template;
14483 tree template_args;
14484 location_t loc = UNKNOWN_LOCATION;
14486 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14488 /* Figure out what name to look up. */
14489 name = TREE_OPERAND (qualified_id, 1);
14490 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14492 is_template = true;
14493 loc = EXPR_LOCATION (name);
14494 template_args = TREE_OPERAND (name, 1);
14495 if (template_args)
14496 template_args = tsubst_template_args (template_args, args,
14497 complain, in_decl);
14498 if (template_args == error_mark_node)
14499 return error_mark_node;
14500 name = TREE_OPERAND (name, 0);
14502 else
14504 is_template = false;
14505 template_args = NULL_TREE;
14508 /* Substitute into the qualifying scope. When there are no ARGS, we
14509 are just trying to simplify a non-dependent expression. In that
14510 case the qualifying scope may be dependent, and, in any case,
14511 substituting will not help. */
14512 scope = TREE_OPERAND (qualified_id, 0);
14513 if (args)
14515 scope = tsubst (scope, args, complain, in_decl);
14516 expr = tsubst_copy (name, args, complain, in_decl);
14518 else
14519 expr = name;
14521 if (dependent_scope_p (scope))
14523 if (is_template)
14524 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14525 tree r = build_qualified_name (NULL_TREE, scope, expr,
14526 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14527 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14528 return r;
14531 if (!BASELINK_P (name) && !DECL_P (expr))
14533 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14535 /* A BIT_NOT_EXPR is used to represent a destructor. */
14536 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14538 error ("qualifying type %qT does not match destructor name ~%qT",
14539 scope, TREE_OPERAND (expr, 0));
14540 expr = error_mark_node;
14542 else
14543 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14544 /*is_type_p=*/0, false);
14546 else
14547 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14548 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14549 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14551 if (complain & tf_error)
14553 error ("dependent-name %qE is parsed as a non-type, but "
14554 "instantiation yields a type", qualified_id);
14555 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14557 return error_mark_node;
14561 if (DECL_P (expr))
14563 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14564 scope);
14565 /* Remember that there was a reference to this entity. */
14566 if (!mark_used (expr, complain) && !(complain & tf_error))
14567 return error_mark_node;
14570 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14572 if (complain & tf_error)
14573 qualified_name_lookup_error (scope,
14574 TREE_OPERAND (qualified_id, 1),
14575 expr, input_location);
14576 return error_mark_node;
14579 if (is_template)
14581 if (variable_template_p (expr))
14582 expr = lookup_and_finish_template_variable (expr, template_args,
14583 complain);
14584 else
14585 expr = lookup_template_function (expr, template_args);
14588 if (expr == error_mark_node && complain & tf_error)
14589 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14590 expr, input_location);
14591 else if (TYPE_P (scope))
14593 expr = (adjust_result_of_qualified_name_lookup
14594 (expr, scope, current_nonlambda_class_type ()));
14595 expr = (finish_qualified_id_expr
14596 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14597 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14598 /*template_arg_p=*/false, complain));
14601 /* Expressions do not generally have reference type. */
14602 if (TREE_CODE (expr) != SCOPE_REF
14603 /* However, if we're about to form a pointer-to-member, we just
14604 want the referenced member referenced. */
14605 && TREE_CODE (expr) != OFFSET_REF)
14606 expr = convert_from_reference (expr);
14608 if (REF_PARENTHESIZED_P (qualified_id))
14609 expr = force_paren_expr (expr);
14611 return expr;
14614 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14615 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14616 for tsubst. */
14618 static tree
14619 tsubst_init (tree init, tree decl, tree args,
14620 tsubst_flags_t complain, tree in_decl)
14622 if (!init)
14623 return NULL_TREE;
14625 init = tsubst_expr (init, args, complain, in_decl, false);
14627 if (!init && TREE_TYPE (decl) != error_mark_node)
14629 /* If we had an initializer but it
14630 instantiated to nothing,
14631 value-initialize the object. This will
14632 only occur when the initializer was a
14633 pack expansion where the parameter packs
14634 used in that expansion were of length
14635 zero. */
14636 init = build_value_init (TREE_TYPE (decl),
14637 complain);
14638 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14639 init = get_target_expr_sfinae (init, complain);
14640 if (TREE_CODE (init) == TARGET_EXPR)
14641 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14644 return init;
14647 /* Like tsubst, but deals with expressions. This function just replaces
14648 template parms; to finish processing the resultant expression, use
14649 tsubst_copy_and_build or tsubst_expr. */
14651 static tree
14652 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14654 enum tree_code code;
14655 tree r;
14657 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14658 return t;
14660 code = TREE_CODE (t);
14662 switch (code)
14664 case PARM_DECL:
14665 r = retrieve_local_specialization (t);
14667 if (r == NULL_TREE)
14669 /* We get here for a use of 'this' in an NSDMI. */
14670 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14671 return current_class_ptr;
14673 /* This can happen for a parameter name used later in a function
14674 declaration (such as in a late-specified return type). Just
14675 make a dummy decl, since it's only used for its type. */
14676 gcc_assert (cp_unevaluated_operand != 0);
14677 r = tsubst_decl (t, args, complain);
14678 /* Give it the template pattern as its context; its true context
14679 hasn't been instantiated yet and this is good enough for
14680 mangling. */
14681 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14684 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14685 r = ARGUMENT_PACK_SELECT_ARG (r);
14686 if (!mark_used (r, complain) && !(complain & tf_error))
14687 return error_mark_node;
14688 return r;
14690 case CONST_DECL:
14692 tree enum_type;
14693 tree v;
14695 if (DECL_TEMPLATE_PARM_P (t))
14696 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14697 /* There is no need to substitute into namespace-scope
14698 enumerators. */
14699 if (DECL_NAMESPACE_SCOPE_P (t))
14700 return t;
14701 /* If ARGS is NULL, then T is known to be non-dependent. */
14702 if (args == NULL_TREE)
14703 return scalar_constant_value (t);
14705 /* Unfortunately, we cannot just call lookup_name here.
14706 Consider:
14708 template <int I> int f() {
14709 enum E { a = I };
14710 struct S { void g() { E e = a; } };
14713 When we instantiate f<7>::S::g(), say, lookup_name is not
14714 clever enough to find f<7>::a. */
14715 enum_type
14716 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14717 /*entering_scope=*/0);
14719 for (v = TYPE_VALUES (enum_type);
14720 v != NULL_TREE;
14721 v = TREE_CHAIN (v))
14722 if (TREE_PURPOSE (v) == DECL_NAME (t))
14723 return TREE_VALUE (v);
14725 /* We didn't find the name. That should never happen; if
14726 name-lookup found it during preliminary parsing, we
14727 should find it again here during instantiation. */
14728 gcc_unreachable ();
14730 return t;
14732 case FIELD_DECL:
14733 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14735 /* Check for a local specialization set up by
14736 tsubst_pack_expansion. */
14737 if (tree r = retrieve_local_specialization (t))
14739 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14740 r = ARGUMENT_PACK_SELECT_ARG (r);
14741 return r;
14744 /* When retrieving a capture pack from a generic lambda, remove the
14745 lambda call op's own template argument list from ARGS. Only the
14746 template arguments active for the closure type should be used to
14747 retrieve the pack specialization. */
14748 if (LAMBDA_FUNCTION_P (current_function_decl)
14749 && (template_class_depth (DECL_CONTEXT (t))
14750 != TMPL_ARGS_DEPTH (args)))
14751 args = strip_innermost_template_args (args, 1);
14753 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14754 tsubst_decl put in the hash table. */
14755 return retrieve_specialization (t, args, 0);
14758 if (DECL_CONTEXT (t))
14760 tree ctx;
14762 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14763 /*entering_scope=*/1);
14764 if (ctx != DECL_CONTEXT (t))
14766 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14767 if (!r)
14769 if (complain & tf_error)
14770 error ("using invalid field %qD", t);
14771 return error_mark_node;
14773 return r;
14777 return t;
14779 case VAR_DECL:
14780 case FUNCTION_DECL:
14781 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14782 r = tsubst (t, args, complain, in_decl);
14783 else if (local_variable_p (t)
14784 && uses_template_parms (DECL_CONTEXT (t)))
14786 r = retrieve_local_specialization (t);
14787 if (r == NULL_TREE)
14789 /* First try name lookup to find the instantiation. */
14790 r = lookup_name (DECL_NAME (t));
14791 if (r && !is_capture_proxy (r))
14793 /* Make sure that the one we found is the one we want. */
14794 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14795 if (ctx != DECL_CONTEXT (r))
14796 r = NULL_TREE;
14799 if (r)
14800 /* OK */;
14801 else
14803 /* This can happen for a variable used in a
14804 late-specified return type of a local lambda, or for a
14805 local static or constant. Building a new VAR_DECL
14806 should be OK in all those cases. */
14807 r = tsubst_decl (t, args, complain);
14808 if (local_specializations)
14809 /* Avoid infinite recursion (79640). */
14810 register_local_specialization (r, t);
14811 if (decl_maybe_constant_var_p (r))
14813 /* We can't call cp_finish_decl, so handle the
14814 initializer by hand. */
14815 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14816 complain, in_decl);
14817 if (!processing_template_decl)
14818 init = maybe_constant_init (init);
14819 if (processing_template_decl
14820 ? potential_constant_expression (init)
14821 : reduced_constant_expression_p (init))
14822 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14823 = TREE_CONSTANT (r) = true;
14824 DECL_INITIAL (r) = init;
14825 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14826 TREE_TYPE (r)
14827 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14828 complain, adc_variable_type);
14830 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14831 || decl_constant_var_p (r)
14832 || errorcount || sorrycount);
14833 if (!processing_template_decl
14834 && !TREE_STATIC (r))
14835 r = process_outer_var_ref (r, complain);
14837 /* Remember this for subsequent uses. */
14838 if (local_specializations)
14839 register_local_specialization (r, t);
14842 else
14843 r = t;
14844 if (!mark_used (r, complain))
14845 return error_mark_node;
14846 return r;
14848 case NAMESPACE_DECL:
14849 return t;
14851 case OVERLOAD:
14852 /* An OVERLOAD will always be a non-dependent overload set; an
14853 overload set from function scope will just be represented with an
14854 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14855 gcc_assert (!uses_template_parms (t));
14856 /* We must have marked any lookups as persistent. */
14857 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14858 return t;
14860 case BASELINK:
14861 return tsubst_baselink (t, current_nonlambda_class_type (),
14862 args, complain, in_decl);
14864 case TEMPLATE_DECL:
14865 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14866 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14867 args, complain, in_decl);
14868 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14869 return tsubst (t, args, complain, in_decl);
14870 else if (DECL_CLASS_SCOPE_P (t)
14871 && uses_template_parms (DECL_CONTEXT (t)))
14873 /* Template template argument like the following example need
14874 special treatment:
14876 template <template <class> class TT> struct C {};
14877 template <class T> struct D {
14878 template <class U> struct E {};
14879 C<E> c; // #1
14881 D<int> d; // #2
14883 We are processing the template argument `E' in #1 for
14884 the template instantiation #2. Originally, `E' is a
14885 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14886 have to substitute this with one having context `D<int>'. */
14888 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14889 if (dependent_scope_p (context))
14891 /* When rewriting a constructor into a deduction guide, a
14892 non-dependent name can become dependent, so memtmpl<args>
14893 becomes context::template memtmpl<args>. */
14894 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14895 return build_qualified_name (type, context, DECL_NAME (t),
14896 /*template*/true);
14898 return lookup_field (context, DECL_NAME(t), 0, false);
14900 else
14901 /* Ordinary template template argument. */
14902 return t;
14904 case CAST_EXPR:
14905 case REINTERPRET_CAST_EXPR:
14906 case CONST_CAST_EXPR:
14907 case STATIC_CAST_EXPR:
14908 case DYNAMIC_CAST_EXPR:
14909 case IMPLICIT_CONV_EXPR:
14910 case CONVERT_EXPR:
14911 case NOP_EXPR:
14913 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14914 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14915 return build1 (code, type, op0);
14918 case SIZEOF_EXPR:
14919 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14920 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14922 tree expanded, op = TREE_OPERAND (t, 0);
14923 int len = 0;
14925 if (SIZEOF_EXPR_TYPE_P (t))
14926 op = TREE_TYPE (op);
14928 ++cp_unevaluated_operand;
14929 ++c_inhibit_evaluation_warnings;
14930 /* We only want to compute the number of arguments. */
14931 if (PACK_EXPANSION_P (op))
14932 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14933 else
14934 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14935 args, complain, in_decl);
14936 --cp_unevaluated_operand;
14937 --c_inhibit_evaluation_warnings;
14939 if (TREE_CODE (expanded) == TREE_VEC)
14941 len = TREE_VEC_LENGTH (expanded);
14942 /* Set TREE_USED for the benefit of -Wunused. */
14943 for (int i = 0; i < len; i++)
14944 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14945 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14948 if (expanded == error_mark_node)
14949 return error_mark_node;
14950 else if (PACK_EXPANSION_P (expanded)
14951 || (TREE_CODE (expanded) == TREE_VEC
14952 && pack_expansion_args_count (expanded)))
14955 if (PACK_EXPANSION_P (expanded))
14956 /* OK. */;
14957 else if (TREE_VEC_LENGTH (expanded) == 1)
14958 expanded = TREE_VEC_ELT (expanded, 0);
14959 else
14960 expanded = make_argument_pack (expanded);
14962 if (TYPE_P (expanded))
14963 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14964 complain & tf_error);
14965 else
14966 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14967 complain & tf_error);
14969 else
14970 return build_int_cst (size_type_node, len);
14972 if (SIZEOF_EXPR_TYPE_P (t))
14974 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14975 args, complain, in_decl);
14976 r = build1 (NOP_EXPR, r, error_mark_node);
14977 r = build1 (SIZEOF_EXPR,
14978 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14979 SIZEOF_EXPR_TYPE_P (r) = 1;
14980 return r;
14982 /* Fall through */
14984 case INDIRECT_REF:
14985 case NEGATE_EXPR:
14986 case TRUTH_NOT_EXPR:
14987 case BIT_NOT_EXPR:
14988 case ADDR_EXPR:
14989 case UNARY_PLUS_EXPR: /* Unary + */
14990 case ALIGNOF_EXPR:
14991 case AT_ENCODE_EXPR:
14992 case ARROW_EXPR:
14993 case THROW_EXPR:
14994 case TYPEID_EXPR:
14995 case REALPART_EXPR:
14996 case IMAGPART_EXPR:
14997 case PAREN_EXPR:
14999 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15000 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15001 return build1 (code, type, op0);
15004 case COMPONENT_REF:
15006 tree object;
15007 tree name;
15009 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15010 name = TREE_OPERAND (t, 1);
15011 if (TREE_CODE (name) == BIT_NOT_EXPR)
15013 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15014 complain, in_decl);
15015 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15017 else if (TREE_CODE (name) == SCOPE_REF
15018 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15020 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15021 complain, in_decl);
15022 name = TREE_OPERAND (name, 1);
15023 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15024 complain, in_decl);
15025 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15026 name = build_qualified_name (/*type=*/NULL_TREE,
15027 base, name,
15028 /*template_p=*/false);
15030 else if (BASELINK_P (name))
15031 name = tsubst_baselink (name,
15032 non_reference (TREE_TYPE (object)),
15033 args, complain,
15034 in_decl);
15035 else
15036 name = tsubst_copy (name, args, complain, in_decl);
15037 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15040 case PLUS_EXPR:
15041 case MINUS_EXPR:
15042 case MULT_EXPR:
15043 case TRUNC_DIV_EXPR:
15044 case CEIL_DIV_EXPR:
15045 case FLOOR_DIV_EXPR:
15046 case ROUND_DIV_EXPR:
15047 case EXACT_DIV_EXPR:
15048 case BIT_AND_EXPR:
15049 case BIT_IOR_EXPR:
15050 case BIT_XOR_EXPR:
15051 case TRUNC_MOD_EXPR:
15052 case FLOOR_MOD_EXPR:
15053 case TRUTH_ANDIF_EXPR:
15054 case TRUTH_ORIF_EXPR:
15055 case TRUTH_AND_EXPR:
15056 case TRUTH_OR_EXPR:
15057 case RSHIFT_EXPR:
15058 case LSHIFT_EXPR:
15059 case RROTATE_EXPR:
15060 case LROTATE_EXPR:
15061 case EQ_EXPR:
15062 case NE_EXPR:
15063 case MAX_EXPR:
15064 case MIN_EXPR:
15065 case LE_EXPR:
15066 case GE_EXPR:
15067 case LT_EXPR:
15068 case GT_EXPR:
15069 case COMPOUND_EXPR:
15070 case DOTSTAR_EXPR:
15071 case MEMBER_REF:
15072 case PREDECREMENT_EXPR:
15073 case PREINCREMENT_EXPR:
15074 case POSTDECREMENT_EXPR:
15075 case POSTINCREMENT_EXPR:
15077 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15078 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15079 return build_nt (code, op0, op1);
15082 case SCOPE_REF:
15084 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15085 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15086 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15087 QUALIFIED_NAME_IS_TEMPLATE (t));
15090 case ARRAY_REF:
15092 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15093 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15094 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15097 case CALL_EXPR:
15099 int n = VL_EXP_OPERAND_LENGTH (t);
15100 tree result = build_vl_exp (CALL_EXPR, n);
15101 int i;
15102 for (i = 0; i < n; i++)
15103 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15104 complain, in_decl);
15105 return result;
15108 case COND_EXPR:
15109 case MODOP_EXPR:
15110 case PSEUDO_DTOR_EXPR:
15111 case VEC_PERM_EXPR:
15113 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15114 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15115 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15116 r = build_nt (code, op0, op1, op2);
15117 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15118 return r;
15121 case NEW_EXPR:
15123 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15124 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15125 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15126 r = build_nt (code, op0, op1, op2);
15127 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15128 return r;
15131 case DELETE_EXPR:
15133 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15134 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15135 r = build_nt (code, op0, op1);
15136 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15137 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15138 return r;
15141 case TEMPLATE_ID_EXPR:
15143 /* Substituted template arguments */
15144 tree fn = TREE_OPERAND (t, 0);
15145 tree targs = TREE_OPERAND (t, 1);
15147 fn = tsubst_copy (fn, args, complain, in_decl);
15148 if (targs)
15149 targs = tsubst_template_args (targs, args, complain, in_decl);
15151 return lookup_template_function (fn, targs);
15154 case TREE_LIST:
15156 tree purpose, value, chain;
15158 if (t == void_list_node)
15159 return t;
15161 purpose = TREE_PURPOSE (t);
15162 if (purpose)
15163 purpose = tsubst_copy (purpose, args, complain, in_decl);
15164 value = TREE_VALUE (t);
15165 if (value)
15166 value = tsubst_copy (value, args, complain, in_decl);
15167 chain = TREE_CHAIN (t);
15168 if (chain && chain != void_type_node)
15169 chain = tsubst_copy (chain, args, complain, in_decl);
15170 if (purpose == TREE_PURPOSE (t)
15171 && value == TREE_VALUE (t)
15172 && chain == TREE_CHAIN (t))
15173 return t;
15174 return tree_cons (purpose, value, chain);
15177 case RECORD_TYPE:
15178 case UNION_TYPE:
15179 case ENUMERAL_TYPE:
15180 case INTEGER_TYPE:
15181 case TEMPLATE_TYPE_PARM:
15182 case TEMPLATE_TEMPLATE_PARM:
15183 case BOUND_TEMPLATE_TEMPLATE_PARM:
15184 case TEMPLATE_PARM_INDEX:
15185 case POINTER_TYPE:
15186 case REFERENCE_TYPE:
15187 case OFFSET_TYPE:
15188 case FUNCTION_TYPE:
15189 case METHOD_TYPE:
15190 case ARRAY_TYPE:
15191 case TYPENAME_TYPE:
15192 case UNBOUND_CLASS_TEMPLATE:
15193 case TYPEOF_TYPE:
15194 case DECLTYPE_TYPE:
15195 case TYPE_DECL:
15196 return tsubst (t, args, complain, in_decl);
15198 case USING_DECL:
15199 t = DECL_NAME (t);
15200 /* Fall through. */
15201 case IDENTIFIER_NODE:
15202 if (IDENTIFIER_CONV_OP_P (t))
15204 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15205 return make_conv_op_name (new_type);
15207 else
15208 return t;
15210 case CONSTRUCTOR:
15211 /* This is handled by tsubst_copy_and_build. */
15212 gcc_unreachable ();
15214 case VA_ARG_EXPR:
15216 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15217 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15218 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15221 case CLEANUP_POINT_EXPR:
15222 /* We shouldn't have built any of these during initial template
15223 generation. Instead, they should be built during instantiation
15224 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15225 gcc_unreachable ();
15227 case OFFSET_REF:
15229 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15230 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15231 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15232 r = build2 (code, type, op0, op1);
15233 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15234 if (!mark_used (TREE_OPERAND (r, 1), complain)
15235 && !(complain & tf_error))
15236 return error_mark_node;
15237 return r;
15240 case EXPR_PACK_EXPANSION:
15241 error ("invalid use of pack expansion expression");
15242 return error_mark_node;
15244 case NONTYPE_ARGUMENT_PACK:
15245 error ("use %<...%> to expand argument pack");
15246 return error_mark_node;
15248 case VOID_CST:
15249 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15250 return t;
15252 case INTEGER_CST:
15253 case REAL_CST:
15254 case STRING_CST:
15255 case COMPLEX_CST:
15257 /* Instantiate any typedefs in the type. */
15258 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15259 r = fold_convert (type, t);
15260 gcc_assert (TREE_CODE (r) == code);
15261 return r;
15264 case PTRMEM_CST:
15265 /* These can sometimes show up in a partial instantiation, but never
15266 involve template parms. */
15267 gcc_assert (!uses_template_parms (t));
15268 return t;
15270 case UNARY_LEFT_FOLD_EXPR:
15271 return tsubst_unary_left_fold (t, args, complain, in_decl);
15272 case UNARY_RIGHT_FOLD_EXPR:
15273 return tsubst_unary_right_fold (t, args, complain, in_decl);
15274 case BINARY_LEFT_FOLD_EXPR:
15275 return tsubst_binary_left_fold (t, args, complain, in_decl);
15276 case BINARY_RIGHT_FOLD_EXPR:
15277 return tsubst_binary_right_fold (t, args, complain, in_decl);
15278 case PREDICT_EXPR:
15279 return t;
15281 default:
15282 /* We shouldn't get here, but keep going if !flag_checking. */
15283 if (flag_checking)
15284 gcc_unreachable ();
15285 return t;
15289 /* Helper function for tsubst_omp_clauses, used for instantiation of
15290 OMP_CLAUSE_DECL of clauses. */
15292 static tree
15293 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15294 tree in_decl)
15296 if (decl == NULL_TREE)
15297 return NULL_TREE;
15299 /* Handle an OpenMP array section represented as a TREE_LIST (or
15300 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15301 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15302 TREE_LIST. We can handle it exactly the same as an array section
15303 (purpose, value, and a chain), even though the nomenclature
15304 (low_bound, length, etc) is different. */
15305 if (TREE_CODE (decl) == TREE_LIST)
15307 tree low_bound
15308 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15309 /*integral_constant_expression_p=*/false);
15310 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15311 /*integral_constant_expression_p=*/false);
15312 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15313 in_decl);
15314 if (TREE_PURPOSE (decl) == low_bound
15315 && TREE_VALUE (decl) == length
15316 && TREE_CHAIN (decl) == chain)
15317 return decl;
15318 tree ret = tree_cons (low_bound, length, chain);
15319 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15320 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15321 return ret;
15323 tree ret = tsubst_expr (decl, args, complain, in_decl,
15324 /*integral_constant_expression_p=*/false);
15325 /* Undo convert_from_reference tsubst_expr could have called. */
15326 if (decl
15327 && REFERENCE_REF_P (ret)
15328 && !REFERENCE_REF_P (decl))
15329 ret = TREE_OPERAND (ret, 0);
15330 return ret;
15333 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15335 static tree
15336 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15337 tree args, tsubst_flags_t complain, tree in_decl)
15339 tree new_clauses = NULL_TREE, nc, oc;
15340 tree linear_no_step = NULL_TREE;
15342 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15344 nc = copy_node (oc);
15345 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15346 new_clauses = nc;
15348 switch (OMP_CLAUSE_CODE (nc))
15350 case OMP_CLAUSE_LASTPRIVATE:
15351 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15353 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15354 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15355 in_decl, /*integral_constant_expression_p=*/false);
15356 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15357 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15359 /* FALLTHRU */
15360 case OMP_CLAUSE_PRIVATE:
15361 case OMP_CLAUSE_SHARED:
15362 case OMP_CLAUSE_FIRSTPRIVATE:
15363 case OMP_CLAUSE_COPYIN:
15364 case OMP_CLAUSE_COPYPRIVATE:
15365 case OMP_CLAUSE_UNIFORM:
15366 case OMP_CLAUSE_DEPEND:
15367 case OMP_CLAUSE_FROM:
15368 case OMP_CLAUSE_TO:
15369 case OMP_CLAUSE_MAP:
15370 case OMP_CLAUSE_USE_DEVICE_PTR:
15371 case OMP_CLAUSE_IS_DEVICE_PTR:
15372 OMP_CLAUSE_DECL (nc)
15373 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15374 in_decl);
15375 break;
15376 case OMP_CLAUSE_TILE:
15377 case OMP_CLAUSE_IF:
15378 case OMP_CLAUSE_NUM_THREADS:
15379 case OMP_CLAUSE_SCHEDULE:
15380 case OMP_CLAUSE_COLLAPSE:
15381 case OMP_CLAUSE_FINAL:
15382 case OMP_CLAUSE_DEVICE:
15383 case OMP_CLAUSE_DIST_SCHEDULE:
15384 case OMP_CLAUSE_NUM_TEAMS:
15385 case OMP_CLAUSE_THREAD_LIMIT:
15386 case OMP_CLAUSE_SAFELEN:
15387 case OMP_CLAUSE_SIMDLEN:
15388 case OMP_CLAUSE_NUM_TASKS:
15389 case OMP_CLAUSE_GRAINSIZE:
15390 case OMP_CLAUSE_PRIORITY:
15391 case OMP_CLAUSE_ORDERED:
15392 case OMP_CLAUSE_HINT:
15393 case OMP_CLAUSE_NUM_GANGS:
15394 case OMP_CLAUSE_NUM_WORKERS:
15395 case OMP_CLAUSE_VECTOR_LENGTH:
15396 case OMP_CLAUSE_WORKER:
15397 case OMP_CLAUSE_VECTOR:
15398 case OMP_CLAUSE_ASYNC:
15399 case OMP_CLAUSE_WAIT:
15400 OMP_CLAUSE_OPERAND (nc, 0)
15401 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15402 in_decl, /*integral_constant_expression_p=*/false);
15403 break;
15404 case OMP_CLAUSE_REDUCTION:
15405 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15407 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15408 if (TREE_CODE (placeholder) == SCOPE_REF)
15410 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15411 complain, in_decl);
15412 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15413 = build_qualified_name (NULL_TREE, scope,
15414 TREE_OPERAND (placeholder, 1),
15415 false);
15417 else
15418 gcc_assert (identifier_p (placeholder));
15420 OMP_CLAUSE_DECL (nc)
15421 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15422 in_decl);
15423 break;
15424 case OMP_CLAUSE_GANG:
15425 case OMP_CLAUSE_ALIGNED:
15426 OMP_CLAUSE_DECL (nc)
15427 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15428 in_decl);
15429 OMP_CLAUSE_OPERAND (nc, 1)
15430 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15431 in_decl, /*integral_constant_expression_p=*/false);
15432 break;
15433 case OMP_CLAUSE_LINEAR:
15434 OMP_CLAUSE_DECL (nc)
15435 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15436 in_decl);
15437 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15439 gcc_assert (!linear_no_step);
15440 linear_no_step = nc;
15442 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15443 OMP_CLAUSE_LINEAR_STEP (nc)
15444 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15445 complain, in_decl);
15446 else
15447 OMP_CLAUSE_LINEAR_STEP (nc)
15448 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15449 in_decl,
15450 /*integral_constant_expression_p=*/false);
15451 break;
15452 case OMP_CLAUSE_NOWAIT:
15453 case OMP_CLAUSE_DEFAULT:
15454 case OMP_CLAUSE_UNTIED:
15455 case OMP_CLAUSE_MERGEABLE:
15456 case OMP_CLAUSE_INBRANCH:
15457 case OMP_CLAUSE_NOTINBRANCH:
15458 case OMP_CLAUSE_PROC_BIND:
15459 case OMP_CLAUSE_FOR:
15460 case OMP_CLAUSE_PARALLEL:
15461 case OMP_CLAUSE_SECTIONS:
15462 case OMP_CLAUSE_TASKGROUP:
15463 case OMP_CLAUSE_NOGROUP:
15464 case OMP_CLAUSE_THREADS:
15465 case OMP_CLAUSE_SIMD:
15466 case OMP_CLAUSE_DEFAULTMAP:
15467 case OMP_CLAUSE_INDEPENDENT:
15468 case OMP_CLAUSE_AUTO:
15469 case OMP_CLAUSE_SEQ:
15470 break;
15471 default:
15472 gcc_unreachable ();
15474 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15475 switch (OMP_CLAUSE_CODE (nc))
15477 case OMP_CLAUSE_SHARED:
15478 case OMP_CLAUSE_PRIVATE:
15479 case OMP_CLAUSE_FIRSTPRIVATE:
15480 case OMP_CLAUSE_LASTPRIVATE:
15481 case OMP_CLAUSE_COPYPRIVATE:
15482 case OMP_CLAUSE_LINEAR:
15483 case OMP_CLAUSE_REDUCTION:
15484 case OMP_CLAUSE_USE_DEVICE_PTR:
15485 case OMP_CLAUSE_IS_DEVICE_PTR:
15486 /* tsubst_expr on SCOPE_REF results in returning
15487 finish_non_static_data_member result. Undo that here. */
15488 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15489 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15490 == IDENTIFIER_NODE))
15492 tree t = OMP_CLAUSE_DECL (nc);
15493 tree v = t;
15494 while (v)
15495 switch (TREE_CODE (v))
15497 case COMPONENT_REF:
15498 case MEM_REF:
15499 case INDIRECT_REF:
15500 CASE_CONVERT:
15501 case POINTER_PLUS_EXPR:
15502 v = TREE_OPERAND (v, 0);
15503 continue;
15504 case PARM_DECL:
15505 if (DECL_CONTEXT (v) == current_function_decl
15506 && DECL_ARTIFICIAL (v)
15507 && DECL_NAME (v) == this_identifier)
15508 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15509 /* FALLTHRU */
15510 default:
15511 v = NULL_TREE;
15512 break;
15515 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15516 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15517 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15518 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15519 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15521 tree decl = OMP_CLAUSE_DECL (nc);
15522 if (VAR_P (decl))
15524 retrofit_lang_decl (decl);
15525 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15528 break;
15529 default:
15530 break;
15534 new_clauses = nreverse (new_clauses);
15535 if (ort != C_ORT_OMP_DECLARE_SIMD)
15537 new_clauses = finish_omp_clauses (new_clauses, ort);
15538 if (linear_no_step)
15539 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15540 if (nc == linear_no_step)
15542 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15543 break;
15546 return new_clauses;
15549 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15551 static tree
15552 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15553 tree in_decl)
15555 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15557 tree purpose, value, chain;
15559 if (t == NULL)
15560 return t;
15562 if (TREE_CODE (t) != TREE_LIST)
15563 return tsubst_copy_and_build (t, args, complain, in_decl,
15564 /*function_p=*/false,
15565 /*integral_constant_expression_p=*/false);
15567 if (t == void_list_node)
15568 return t;
15570 purpose = TREE_PURPOSE (t);
15571 if (purpose)
15572 purpose = RECUR (purpose);
15573 value = TREE_VALUE (t);
15574 if (value)
15576 if (TREE_CODE (value) != LABEL_DECL)
15577 value = RECUR (value);
15578 else
15580 value = lookup_label (DECL_NAME (value));
15581 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15582 TREE_USED (value) = 1;
15585 chain = TREE_CHAIN (t);
15586 if (chain && chain != void_type_node)
15587 chain = RECUR (chain);
15588 return tree_cons (purpose, value, chain);
15589 #undef RECUR
15592 /* Used to temporarily communicate the list of #pragma omp parallel
15593 clauses to #pragma omp for instantiation if they are combined
15594 together. */
15596 static tree *omp_parallel_combined_clauses;
15598 /* Substitute one OMP_FOR iterator. */
15600 static void
15601 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15602 tree initv, tree condv, tree incrv, tree *clauses,
15603 tree args, tsubst_flags_t complain, tree in_decl,
15604 bool integral_constant_expression_p)
15606 #define RECUR(NODE) \
15607 tsubst_expr ((NODE), args, complain, in_decl, \
15608 integral_constant_expression_p)
15609 tree decl, init, cond, incr;
15611 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15612 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15614 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15616 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15617 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15620 decl = TREE_OPERAND (init, 0);
15621 init = TREE_OPERAND (init, 1);
15622 tree decl_expr = NULL_TREE;
15623 if (init && TREE_CODE (init) == DECL_EXPR)
15625 /* We need to jump through some hoops to handle declarations in the
15626 init-statement, since we might need to handle auto deduction,
15627 but we need to keep control of initialization. */
15628 decl_expr = init;
15629 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15630 decl = tsubst_decl (decl, args, complain);
15632 else
15634 if (TREE_CODE (decl) == SCOPE_REF)
15636 decl = RECUR (decl);
15637 if (TREE_CODE (decl) == COMPONENT_REF)
15639 tree v = decl;
15640 while (v)
15641 switch (TREE_CODE (v))
15643 case COMPONENT_REF:
15644 case MEM_REF:
15645 case INDIRECT_REF:
15646 CASE_CONVERT:
15647 case POINTER_PLUS_EXPR:
15648 v = TREE_OPERAND (v, 0);
15649 continue;
15650 case PARM_DECL:
15651 if (DECL_CONTEXT (v) == current_function_decl
15652 && DECL_ARTIFICIAL (v)
15653 && DECL_NAME (v) == this_identifier)
15655 decl = TREE_OPERAND (decl, 1);
15656 decl = omp_privatize_field (decl, false);
15658 /* FALLTHRU */
15659 default:
15660 v = NULL_TREE;
15661 break;
15665 else
15666 decl = RECUR (decl);
15668 init = RECUR (init);
15670 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15671 if (auto_node && init)
15672 TREE_TYPE (decl)
15673 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15675 gcc_assert (!type_dependent_expression_p (decl));
15677 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15679 if (decl_expr)
15681 /* Declare the variable, but don't let that initialize it. */
15682 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15683 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15684 RECUR (decl_expr);
15685 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15688 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15689 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15690 if (TREE_CODE (incr) == MODIFY_EXPR)
15692 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15693 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15694 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15695 NOP_EXPR, rhs, complain);
15697 else
15698 incr = RECUR (incr);
15699 TREE_VEC_ELT (declv, i) = decl;
15700 TREE_VEC_ELT (initv, i) = init;
15701 TREE_VEC_ELT (condv, i) = cond;
15702 TREE_VEC_ELT (incrv, i) = incr;
15703 return;
15706 if (decl_expr)
15708 /* Declare and initialize the variable. */
15709 RECUR (decl_expr);
15710 init = NULL_TREE;
15712 else if (init)
15714 tree *pc;
15715 int j;
15716 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15718 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15720 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15721 && OMP_CLAUSE_DECL (*pc) == decl)
15722 break;
15723 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15724 && OMP_CLAUSE_DECL (*pc) == decl)
15726 if (j)
15727 break;
15728 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15729 tree c = *pc;
15730 *pc = OMP_CLAUSE_CHAIN (c);
15731 OMP_CLAUSE_CHAIN (c) = *clauses;
15732 *clauses = c;
15734 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15735 && OMP_CLAUSE_DECL (*pc) == decl)
15737 error ("iteration variable %qD should not be firstprivate",
15738 decl);
15739 *pc = OMP_CLAUSE_CHAIN (*pc);
15741 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15742 && OMP_CLAUSE_DECL (*pc) == decl)
15744 error ("iteration variable %qD should not be reduction",
15745 decl);
15746 *pc = OMP_CLAUSE_CHAIN (*pc);
15748 else
15749 pc = &OMP_CLAUSE_CHAIN (*pc);
15751 if (*pc)
15752 break;
15754 if (*pc == NULL_TREE)
15756 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15757 OMP_CLAUSE_DECL (c) = decl;
15758 c = finish_omp_clauses (c, C_ORT_OMP);
15759 if (c)
15761 OMP_CLAUSE_CHAIN (c) = *clauses;
15762 *clauses = c;
15766 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15767 if (COMPARISON_CLASS_P (cond))
15769 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15770 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15771 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15773 else
15774 cond = RECUR (cond);
15775 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15776 switch (TREE_CODE (incr))
15778 case PREINCREMENT_EXPR:
15779 case PREDECREMENT_EXPR:
15780 case POSTINCREMENT_EXPR:
15781 case POSTDECREMENT_EXPR:
15782 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15783 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15784 break;
15785 case MODIFY_EXPR:
15786 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15787 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15789 tree rhs = TREE_OPERAND (incr, 1);
15790 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15791 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15792 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15793 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15794 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15795 rhs0, rhs1));
15797 else
15798 incr = RECUR (incr);
15799 break;
15800 case MODOP_EXPR:
15801 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15802 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15804 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15805 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15806 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15807 TREE_TYPE (decl), lhs,
15808 RECUR (TREE_OPERAND (incr, 2))));
15810 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15811 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15812 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15814 tree rhs = TREE_OPERAND (incr, 2);
15815 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15816 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15817 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15818 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15819 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15820 rhs0, rhs1));
15822 else
15823 incr = RECUR (incr);
15824 break;
15825 default:
15826 incr = RECUR (incr);
15827 break;
15830 TREE_VEC_ELT (declv, i) = decl;
15831 TREE_VEC_ELT (initv, i) = init;
15832 TREE_VEC_ELT (condv, i) = cond;
15833 TREE_VEC_ELT (incrv, i) = incr;
15834 #undef RECUR
15837 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15838 of OMP_TARGET's body. */
15840 static tree
15841 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15843 *walk_subtrees = 0;
15844 switch (TREE_CODE (*tp))
15846 case OMP_TEAMS:
15847 return *tp;
15848 case BIND_EXPR:
15849 case STATEMENT_LIST:
15850 *walk_subtrees = 1;
15851 break;
15852 default:
15853 break;
15855 return NULL_TREE;
15858 /* Helper function for tsubst_expr. For decomposition declaration
15859 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15860 also the corresponding decls representing the identifiers
15861 of the decomposition declaration. Return DECL if successful
15862 or error_mark_node otherwise, set *FIRST to the first decl
15863 in the list chained through DECL_CHAIN and *CNT to the number
15864 of such decls. */
15866 static tree
15867 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15868 tsubst_flags_t complain, tree in_decl, tree *first,
15869 unsigned int *cnt)
15871 tree decl2, decl3, prev = decl;
15872 *cnt = 0;
15873 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15874 for (decl2 = DECL_CHAIN (pattern_decl);
15875 decl2
15876 && VAR_P (decl2)
15877 && DECL_DECOMPOSITION_P (decl2)
15878 && DECL_NAME (decl2);
15879 decl2 = DECL_CHAIN (decl2))
15881 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15883 gcc_assert (errorcount);
15884 return error_mark_node;
15886 (*cnt)++;
15887 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15888 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15889 tree v = DECL_VALUE_EXPR (decl2);
15890 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15891 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15892 decl3 = tsubst (decl2, args, complain, in_decl);
15893 SET_DECL_VALUE_EXPR (decl2, v);
15894 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15895 if (VAR_P (decl3))
15896 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15897 maybe_push_decl (decl3);
15898 if (error_operand_p (decl3))
15899 decl = error_mark_node;
15900 else if (decl != error_mark_node
15901 && DECL_CHAIN (decl3) != prev)
15903 gcc_assert (errorcount);
15904 decl = error_mark_node;
15906 else
15907 prev = decl3;
15909 *first = prev;
15910 return decl;
15913 /* Like tsubst_copy for expressions, etc. but also does semantic
15914 processing. */
15916 tree
15917 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15918 bool integral_constant_expression_p)
15920 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15921 #define RECUR(NODE) \
15922 tsubst_expr ((NODE), args, complain, in_decl, \
15923 integral_constant_expression_p)
15925 tree stmt, tmp;
15926 tree r;
15927 location_t loc;
15929 if (t == NULL_TREE || t == error_mark_node)
15930 return t;
15932 loc = input_location;
15933 if (EXPR_HAS_LOCATION (t))
15934 input_location = EXPR_LOCATION (t);
15935 if (STATEMENT_CODE_P (TREE_CODE (t)))
15936 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15938 switch (TREE_CODE (t))
15940 case STATEMENT_LIST:
15942 tree_stmt_iterator i;
15943 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15944 RECUR (tsi_stmt (i));
15945 break;
15948 case CTOR_INITIALIZER:
15949 finish_mem_initializers (tsubst_initializer_list
15950 (TREE_OPERAND (t, 0), args));
15951 break;
15953 case RETURN_EXPR:
15954 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15955 break;
15957 case EXPR_STMT:
15958 tmp = RECUR (EXPR_STMT_EXPR (t));
15959 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15960 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15961 else
15962 finish_expr_stmt (tmp);
15963 break;
15965 case USING_STMT:
15966 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15967 /*attribs=*/NULL_TREE);
15968 break;
15970 case DECL_EXPR:
15972 tree decl, pattern_decl;
15973 tree init;
15975 pattern_decl = decl = DECL_EXPR_DECL (t);
15976 if (TREE_CODE (decl) == LABEL_DECL)
15977 finish_label_decl (DECL_NAME (decl));
15978 else if (TREE_CODE (decl) == USING_DECL)
15980 tree scope = USING_DECL_SCOPE (decl);
15981 tree name = DECL_NAME (decl);
15983 scope = tsubst (scope, args, complain, in_decl);
15984 decl = lookup_qualified_name (scope, name,
15985 /*is_type_p=*/false,
15986 /*complain=*/false);
15987 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15988 qualified_name_lookup_error (scope, name, decl, input_location);
15989 else
15990 finish_local_using_decl (decl, scope, name);
15992 else if (DECL_PACK_P (decl))
15994 /* Don't build up decls for a variadic capture proxy, we'll
15995 instantiate the elements directly as needed. */
15996 break;
15998 else if (is_capture_proxy (decl)
15999 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16001 /* We're in tsubst_lambda_expr, we've already inserted new capture
16002 proxies, and uses will find them with lookup_name. */
16003 break;
16005 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16006 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16007 /* Don't copy the old closure; we'll create a new one in
16008 tsubst_lambda_expr. */
16009 break;
16010 else
16012 init = DECL_INITIAL (decl);
16013 decl = tsubst (decl, args, complain, in_decl);
16014 if (decl != error_mark_node)
16016 /* By marking the declaration as instantiated, we avoid
16017 trying to instantiate it. Since instantiate_decl can't
16018 handle local variables, and since we've already done
16019 all that needs to be done, that's the right thing to
16020 do. */
16021 if (VAR_P (decl))
16022 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16023 if (VAR_P (decl)
16024 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16025 /* Anonymous aggregates are a special case. */
16026 finish_anon_union (decl);
16027 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16029 DECL_CONTEXT (decl) = current_function_decl;
16030 if (DECL_NAME (decl) == this_identifier)
16032 tree lam = DECL_CONTEXT (current_function_decl);
16033 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16034 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16036 insert_capture_proxy (decl);
16038 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16039 /* We already did a pushtag. */;
16040 else if (TREE_CODE (decl) == FUNCTION_DECL
16041 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16042 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16044 DECL_CONTEXT (decl) = NULL_TREE;
16045 pushdecl (decl);
16046 DECL_CONTEXT (decl) = current_function_decl;
16047 cp_check_omp_declare_reduction (decl);
16049 else
16051 int const_init = false;
16052 maybe_push_decl (decl);
16053 if (VAR_P (decl)
16054 && DECL_PRETTY_FUNCTION_P (decl))
16056 /* For __PRETTY_FUNCTION__ we have to adjust the
16057 initializer. */
16058 const char *const name
16059 = cxx_printable_name (current_function_decl, 2);
16060 init = cp_fname_init (name, &TREE_TYPE (decl));
16062 else
16063 init = tsubst_init (init, decl, args, complain, in_decl);
16065 if (VAR_P (decl))
16066 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16067 (pattern_decl));
16068 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16069 if (VAR_P (decl)
16070 && DECL_DECOMPOSITION_P (decl)
16071 && TREE_TYPE (pattern_decl) != error_mark_node)
16073 unsigned int cnt;
16074 tree first;
16075 decl = tsubst_decomp_names (decl, pattern_decl, args,
16076 complain, in_decl, &first,
16077 &cnt);
16078 if (decl != error_mark_node)
16079 cp_finish_decomp (decl, first, cnt);
16085 break;
16088 case FOR_STMT:
16089 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16090 RECUR (FOR_INIT_STMT (t));
16091 finish_init_stmt (stmt);
16092 tmp = RECUR (FOR_COND (t));
16093 finish_for_cond (tmp, stmt, false);
16094 tmp = RECUR (FOR_EXPR (t));
16095 finish_for_expr (tmp, stmt);
16096 RECUR (FOR_BODY (t));
16097 finish_for_stmt (stmt);
16098 break;
16100 case RANGE_FOR_STMT:
16102 tree decl, expr;
16103 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16104 decl = RANGE_FOR_DECL (t);
16105 decl = tsubst (decl, args, complain, in_decl);
16106 maybe_push_decl (decl);
16107 expr = RECUR (RANGE_FOR_EXPR (t));
16108 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16110 unsigned int cnt;
16111 tree first;
16112 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16113 complain, in_decl, &first, &cnt);
16114 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16115 RANGE_FOR_IVDEP (t));
16117 else
16118 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16119 RANGE_FOR_IVDEP (t));
16120 RECUR (RANGE_FOR_BODY (t));
16121 finish_for_stmt (stmt);
16123 break;
16125 case WHILE_STMT:
16126 stmt = begin_while_stmt ();
16127 tmp = RECUR (WHILE_COND (t));
16128 finish_while_stmt_cond (tmp, stmt, false);
16129 RECUR (WHILE_BODY (t));
16130 finish_while_stmt (stmt);
16131 break;
16133 case DO_STMT:
16134 stmt = begin_do_stmt ();
16135 RECUR (DO_BODY (t));
16136 finish_do_body (stmt);
16137 tmp = RECUR (DO_COND (t));
16138 finish_do_stmt (tmp, stmt, false);
16139 break;
16141 case IF_STMT:
16142 stmt = begin_if_stmt ();
16143 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16144 tmp = RECUR (IF_COND (t));
16145 tmp = finish_if_stmt_cond (tmp, stmt);
16146 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16147 /* Don't instantiate the THEN_CLAUSE. */;
16148 else
16150 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16151 if (inhibit)
16152 ++c_inhibit_evaluation_warnings;
16153 RECUR (THEN_CLAUSE (t));
16154 if (inhibit)
16155 --c_inhibit_evaluation_warnings;
16157 finish_then_clause (stmt);
16159 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16160 /* Don't instantiate the ELSE_CLAUSE. */;
16161 else if (ELSE_CLAUSE (t))
16163 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16164 begin_else_clause (stmt);
16165 if (inhibit)
16166 ++c_inhibit_evaluation_warnings;
16167 RECUR (ELSE_CLAUSE (t));
16168 if (inhibit)
16169 --c_inhibit_evaluation_warnings;
16170 finish_else_clause (stmt);
16173 finish_if_stmt (stmt);
16174 break;
16176 case BIND_EXPR:
16177 if (BIND_EXPR_BODY_BLOCK (t))
16178 stmt = begin_function_body ();
16179 else
16180 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16181 ? BCS_TRY_BLOCK : 0);
16183 RECUR (BIND_EXPR_BODY (t));
16185 if (BIND_EXPR_BODY_BLOCK (t))
16186 finish_function_body (stmt);
16187 else
16188 finish_compound_stmt (stmt);
16189 break;
16191 case BREAK_STMT:
16192 finish_break_stmt ();
16193 break;
16195 case CONTINUE_STMT:
16196 finish_continue_stmt ();
16197 break;
16199 case SWITCH_STMT:
16200 stmt = begin_switch_stmt ();
16201 tmp = RECUR (SWITCH_STMT_COND (t));
16202 finish_switch_cond (tmp, stmt);
16203 RECUR (SWITCH_STMT_BODY (t));
16204 finish_switch_stmt (stmt);
16205 break;
16207 case CASE_LABEL_EXPR:
16209 tree low = RECUR (CASE_LOW (t));
16210 tree high = RECUR (CASE_HIGH (t));
16211 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16212 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16213 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16214 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16216 break;
16218 case LABEL_EXPR:
16220 tree decl = LABEL_EXPR_LABEL (t);
16221 tree label;
16223 label = finish_label_stmt (DECL_NAME (decl));
16224 if (TREE_CODE (label) == LABEL_DECL)
16225 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16226 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16227 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16229 break;
16231 case GOTO_EXPR:
16232 tmp = GOTO_DESTINATION (t);
16233 if (TREE_CODE (tmp) != LABEL_DECL)
16234 /* Computed goto's must be tsubst'd into. On the other hand,
16235 non-computed gotos must not be; the identifier in question
16236 will have no binding. */
16237 tmp = RECUR (tmp);
16238 else
16239 tmp = DECL_NAME (tmp);
16240 finish_goto_stmt (tmp);
16241 break;
16243 case ASM_EXPR:
16245 tree string = RECUR (ASM_STRING (t));
16246 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16247 complain, in_decl);
16248 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16249 complain, in_decl);
16250 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16251 complain, in_decl);
16252 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16253 complain, in_decl);
16254 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16255 clobbers, labels);
16256 tree asm_expr = tmp;
16257 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16258 asm_expr = TREE_OPERAND (asm_expr, 0);
16259 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16261 break;
16263 case TRY_BLOCK:
16264 if (CLEANUP_P (t))
16266 stmt = begin_try_block ();
16267 RECUR (TRY_STMTS (t));
16268 finish_cleanup_try_block (stmt);
16269 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16271 else
16273 tree compound_stmt = NULL_TREE;
16275 if (FN_TRY_BLOCK_P (t))
16276 stmt = begin_function_try_block (&compound_stmt);
16277 else
16278 stmt = begin_try_block ();
16280 RECUR (TRY_STMTS (t));
16282 if (FN_TRY_BLOCK_P (t))
16283 finish_function_try_block (stmt);
16284 else
16285 finish_try_block (stmt);
16287 RECUR (TRY_HANDLERS (t));
16288 if (FN_TRY_BLOCK_P (t))
16289 finish_function_handler_sequence (stmt, compound_stmt);
16290 else
16291 finish_handler_sequence (stmt);
16293 break;
16295 case HANDLER:
16297 tree decl = HANDLER_PARMS (t);
16299 if (decl)
16301 decl = tsubst (decl, args, complain, in_decl);
16302 /* Prevent instantiate_decl from trying to instantiate
16303 this variable. We've already done all that needs to be
16304 done. */
16305 if (decl != error_mark_node)
16306 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16308 stmt = begin_handler ();
16309 finish_handler_parms (decl, stmt);
16310 RECUR (HANDLER_BODY (t));
16311 finish_handler (stmt);
16313 break;
16315 case TAG_DEFN:
16316 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16317 if (CLASS_TYPE_P (tmp))
16319 /* Local classes are not independent templates; they are
16320 instantiated along with their containing function. And this
16321 way we don't have to deal with pushing out of one local class
16322 to instantiate a member of another local class. */
16323 /* Closures are handled by the LAMBDA_EXPR. */
16324 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16325 complete_type (tmp);
16326 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16327 if ((VAR_P (fld)
16328 || (TREE_CODE (fld) == FUNCTION_DECL
16329 && !DECL_ARTIFICIAL (fld)))
16330 && DECL_TEMPLATE_INSTANTIATION (fld))
16331 instantiate_decl (fld, /*defer_ok=*/false,
16332 /*expl_inst_class=*/false);
16334 break;
16336 case STATIC_ASSERT:
16338 tree condition;
16340 ++c_inhibit_evaluation_warnings;
16341 condition =
16342 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16343 args,
16344 complain, in_decl,
16345 /*integral_constant_expression_p=*/true);
16346 --c_inhibit_evaluation_warnings;
16348 finish_static_assert (condition,
16349 STATIC_ASSERT_MESSAGE (t),
16350 STATIC_ASSERT_SOURCE_LOCATION (t),
16351 /*member_p=*/false);
16353 break;
16355 case OACC_KERNELS:
16356 case OACC_PARALLEL:
16357 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16358 in_decl);
16359 stmt = begin_omp_parallel ();
16360 RECUR (OMP_BODY (t));
16361 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16362 break;
16364 case OMP_PARALLEL:
16365 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16366 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16367 complain, in_decl);
16368 if (OMP_PARALLEL_COMBINED (t))
16369 omp_parallel_combined_clauses = &tmp;
16370 stmt = begin_omp_parallel ();
16371 RECUR (OMP_PARALLEL_BODY (t));
16372 gcc_assert (omp_parallel_combined_clauses == NULL);
16373 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16374 = OMP_PARALLEL_COMBINED (t);
16375 pop_omp_privatization_clauses (r);
16376 break;
16378 case OMP_TASK:
16379 r = push_omp_privatization_clauses (false);
16380 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16381 complain, in_decl);
16382 stmt = begin_omp_task ();
16383 RECUR (OMP_TASK_BODY (t));
16384 finish_omp_task (tmp, stmt);
16385 pop_omp_privatization_clauses (r);
16386 break;
16388 case OMP_FOR:
16389 case OMP_SIMD:
16390 case CILK_SIMD:
16391 case CILK_FOR:
16392 case OMP_DISTRIBUTE:
16393 case OMP_TASKLOOP:
16394 case OACC_LOOP:
16396 tree clauses, body, pre_body;
16397 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16398 tree orig_declv = NULL_TREE;
16399 tree incrv = NULL_TREE;
16400 enum c_omp_region_type ort = C_ORT_OMP;
16401 int i;
16403 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16404 ort = C_ORT_CILK;
16405 else if (TREE_CODE (t) == OACC_LOOP)
16406 ort = C_ORT_ACC;
16408 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16409 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16410 in_decl);
16411 if (OMP_FOR_INIT (t) != NULL_TREE)
16413 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16414 if (OMP_FOR_ORIG_DECLS (t))
16415 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16416 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16417 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16418 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16421 stmt = begin_omp_structured_block ();
16423 pre_body = push_stmt_list ();
16424 RECUR (OMP_FOR_PRE_BODY (t));
16425 pre_body = pop_stmt_list (pre_body);
16427 if (OMP_FOR_INIT (t) != NULL_TREE)
16428 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16429 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16430 incrv, &clauses, args, complain, in_decl,
16431 integral_constant_expression_p);
16432 omp_parallel_combined_clauses = NULL;
16434 body = push_stmt_list ();
16435 RECUR (OMP_FOR_BODY (t));
16436 body = pop_stmt_list (body);
16438 if (OMP_FOR_INIT (t) != NULL_TREE)
16439 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16440 orig_declv, initv, condv, incrv, body, pre_body,
16441 NULL, clauses);
16442 else
16444 t = make_node (TREE_CODE (t));
16445 TREE_TYPE (t) = void_type_node;
16446 OMP_FOR_BODY (t) = body;
16447 OMP_FOR_PRE_BODY (t) = pre_body;
16448 OMP_FOR_CLAUSES (t) = clauses;
16449 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16450 add_stmt (t);
16453 add_stmt (finish_omp_structured_block (stmt));
16454 pop_omp_privatization_clauses (r);
16456 break;
16458 case OMP_SECTIONS:
16459 omp_parallel_combined_clauses = NULL;
16460 /* FALLTHRU */
16461 case OMP_SINGLE:
16462 case OMP_TEAMS:
16463 case OMP_CRITICAL:
16464 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16465 && OMP_TEAMS_COMBINED (t));
16466 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16467 in_decl);
16468 stmt = push_stmt_list ();
16469 RECUR (OMP_BODY (t));
16470 stmt = pop_stmt_list (stmt);
16472 t = copy_node (t);
16473 OMP_BODY (t) = stmt;
16474 OMP_CLAUSES (t) = tmp;
16475 add_stmt (t);
16476 pop_omp_privatization_clauses (r);
16477 break;
16479 case OACC_DATA:
16480 case OMP_TARGET_DATA:
16481 case OMP_TARGET:
16482 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16483 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16484 in_decl);
16485 keep_next_level (true);
16486 stmt = begin_omp_structured_block ();
16488 RECUR (OMP_BODY (t));
16489 stmt = finish_omp_structured_block (stmt);
16491 t = copy_node (t);
16492 OMP_BODY (t) = stmt;
16493 OMP_CLAUSES (t) = tmp;
16494 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16496 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16497 if (teams)
16499 /* For combined target teams, ensure the num_teams and
16500 thread_limit clause expressions are evaluated on the host,
16501 before entering the target construct. */
16502 tree c;
16503 for (c = OMP_TEAMS_CLAUSES (teams);
16504 c; c = OMP_CLAUSE_CHAIN (c))
16505 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16506 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16507 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16509 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16510 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16511 if (expr == error_mark_node)
16512 continue;
16513 tmp = TARGET_EXPR_SLOT (expr);
16514 add_stmt (expr);
16515 OMP_CLAUSE_OPERAND (c, 0) = expr;
16516 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16517 OMP_CLAUSE_FIRSTPRIVATE);
16518 OMP_CLAUSE_DECL (tc) = tmp;
16519 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16520 OMP_TARGET_CLAUSES (t) = tc;
16524 add_stmt (t);
16525 break;
16527 case OACC_DECLARE:
16528 t = copy_node (t);
16529 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16530 complain, in_decl);
16531 OACC_DECLARE_CLAUSES (t) = tmp;
16532 add_stmt (t);
16533 break;
16535 case OMP_TARGET_UPDATE:
16536 case OMP_TARGET_ENTER_DATA:
16537 case OMP_TARGET_EXIT_DATA:
16538 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16539 complain, in_decl);
16540 t = copy_node (t);
16541 OMP_STANDALONE_CLAUSES (t) = tmp;
16542 add_stmt (t);
16543 break;
16545 case OACC_ENTER_DATA:
16546 case OACC_EXIT_DATA:
16547 case OACC_UPDATE:
16548 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16549 complain, in_decl);
16550 t = copy_node (t);
16551 OMP_STANDALONE_CLAUSES (t) = tmp;
16552 add_stmt (t);
16553 break;
16555 case OMP_ORDERED:
16556 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16557 complain, in_decl);
16558 stmt = push_stmt_list ();
16559 RECUR (OMP_BODY (t));
16560 stmt = pop_stmt_list (stmt);
16562 t = copy_node (t);
16563 OMP_BODY (t) = stmt;
16564 OMP_ORDERED_CLAUSES (t) = tmp;
16565 add_stmt (t);
16566 break;
16568 case OMP_SECTION:
16569 case OMP_MASTER:
16570 case OMP_TASKGROUP:
16571 stmt = push_stmt_list ();
16572 RECUR (OMP_BODY (t));
16573 stmt = pop_stmt_list (stmt);
16575 t = copy_node (t);
16576 OMP_BODY (t) = stmt;
16577 add_stmt (t);
16578 break;
16580 case OMP_ATOMIC:
16581 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16582 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16584 tree op1 = TREE_OPERAND (t, 1);
16585 tree rhs1 = NULL_TREE;
16586 tree lhs, rhs;
16587 if (TREE_CODE (op1) == COMPOUND_EXPR)
16589 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16590 op1 = TREE_OPERAND (op1, 1);
16592 lhs = RECUR (TREE_OPERAND (op1, 0));
16593 rhs = RECUR (TREE_OPERAND (op1, 1));
16594 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16595 NULL_TREE, NULL_TREE, rhs1,
16596 OMP_ATOMIC_SEQ_CST (t));
16598 else
16600 tree op1 = TREE_OPERAND (t, 1);
16601 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16602 tree rhs1 = NULL_TREE;
16603 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16604 enum tree_code opcode = NOP_EXPR;
16605 if (code == OMP_ATOMIC_READ)
16607 v = RECUR (TREE_OPERAND (op1, 0));
16608 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16610 else if (code == OMP_ATOMIC_CAPTURE_OLD
16611 || code == OMP_ATOMIC_CAPTURE_NEW)
16613 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16614 v = RECUR (TREE_OPERAND (op1, 0));
16615 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16616 if (TREE_CODE (op11) == COMPOUND_EXPR)
16618 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16619 op11 = TREE_OPERAND (op11, 1);
16621 lhs = RECUR (TREE_OPERAND (op11, 0));
16622 rhs = RECUR (TREE_OPERAND (op11, 1));
16623 opcode = TREE_CODE (op11);
16624 if (opcode == MODIFY_EXPR)
16625 opcode = NOP_EXPR;
16627 else
16629 code = OMP_ATOMIC;
16630 lhs = RECUR (TREE_OPERAND (op1, 0));
16631 rhs = RECUR (TREE_OPERAND (op1, 1));
16633 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16634 OMP_ATOMIC_SEQ_CST (t));
16636 break;
16638 case TRANSACTION_EXPR:
16640 int flags = 0;
16641 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16642 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16644 if (TRANSACTION_EXPR_IS_STMT (t))
16646 tree body = TRANSACTION_EXPR_BODY (t);
16647 tree noex = NULL_TREE;
16648 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16650 noex = MUST_NOT_THROW_COND (body);
16651 if (noex == NULL_TREE)
16652 noex = boolean_true_node;
16653 body = TREE_OPERAND (body, 0);
16655 stmt = begin_transaction_stmt (input_location, NULL, flags);
16656 RECUR (body);
16657 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16659 else
16661 stmt = build_transaction_expr (EXPR_LOCATION (t),
16662 RECUR (TRANSACTION_EXPR_BODY (t)),
16663 flags, NULL_TREE);
16664 RETURN (stmt);
16667 break;
16669 case MUST_NOT_THROW_EXPR:
16671 tree op0 = RECUR (TREE_OPERAND (t, 0));
16672 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16673 RETURN (build_must_not_throw_expr (op0, cond));
16676 case EXPR_PACK_EXPANSION:
16677 error ("invalid use of pack expansion expression");
16678 RETURN (error_mark_node);
16680 case NONTYPE_ARGUMENT_PACK:
16681 error ("use %<...%> to expand argument pack");
16682 RETURN (error_mark_node);
16684 case CILK_SPAWN_STMT:
16685 cfun->calls_cilk_spawn = 1;
16686 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16688 case CILK_SYNC_STMT:
16689 RETURN (build_cilk_sync ());
16691 case COMPOUND_EXPR:
16692 tmp = RECUR (TREE_OPERAND (t, 0));
16693 if (tmp == NULL_TREE)
16694 /* If the first operand was a statement, we're done with it. */
16695 RETURN (RECUR (TREE_OPERAND (t, 1)));
16696 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16697 RECUR (TREE_OPERAND (t, 1)),
16698 complain));
16700 case ANNOTATE_EXPR:
16701 tmp = RECUR (TREE_OPERAND (t, 0));
16702 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16703 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16705 default:
16706 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16708 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16709 /*function_p=*/false,
16710 integral_constant_expression_p));
16713 RETURN (NULL_TREE);
16714 out:
16715 input_location = loc;
16716 return r;
16717 #undef RECUR
16718 #undef RETURN
16721 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16722 function. For description of the body see comment above
16723 cp_parser_omp_declare_reduction_exprs. */
16725 static void
16726 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16728 if (t == NULL_TREE || t == error_mark_node)
16729 return;
16731 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16733 tree_stmt_iterator tsi;
16734 int i;
16735 tree stmts[7];
16736 memset (stmts, 0, sizeof stmts);
16737 for (i = 0, tsi = tsi_start (t);
16738 i < 7 && !tsi_end_p (tsi);
16739 i++, tsi_next (&tsi))
16740 stmts[i] = tsi_stmt (tsi);
16741 gcc_assert (tsi_end_p (tsi));
16743 if (i >= 3)
16745 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16746 && TREE_CODE (stmts[1]) == DECL_EXPR);
16747 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16748 args, complain, in_decl);
16749 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16750 args, complain, in_decl);
16751 DECL_CONTEXT (omp_out) = current_function_decl;
16752 DECL_CONTEXT (omp_in) = current_function_decl;
16753 keep_next_level (true);
16754 tree block = begin_omp_structured_block ();
16755 tsubst_expr (stmts[2], args, complain, in_decl, false);
16756 block = finish_omp_structured_block (block);
16757 block = maybe_cleanup_point_expr_void (block);
16758 add_decl_expr (omp_out);
16759 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16760 TREE_NO_WARNING (omp_out) = 1;
16761 add_decl_expr (omp_in);
16762 finish_expr_stmt (block);
16764 if (i >= 6)
16766 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16767 && TREE_CODE (stmts[4]) == DECL_EXPR);
16768 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16769 args, complain, in_decl);
16770 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16771 args, complain, in_decl);
16772 DECL_CONTEXT (omp_priv) = current_function_decl;
16773 DECL_CONTEXT (omp_orig) = current_function_decl;
16774 keep_next_level (true);
16775 tree block = begin_omp_structured_block ();
16776 tsubst_expr (stmts[5], args, complain, in_decl, false);
16777 block = finish_omp_structured_block (block);
16778 block = maybe_cleanup_point_expr_void (block);
16779 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16780 add_decl_expr (omp_priv);
16781 add_decl_expr (omp_orig);
16782 finish_expr_stmt (block);
16783 if (i == 7)
16784 add_decl_expr (omp_orig);
16788 /* T is a postfix-expression that is not being used in a function
16789 call. Return the substituted version of T. */
16791 static tree
16792 tsubst_non_call_postfix_expression (tree t, tree args,
16793 tsubst_flags_t complain,
16794 tree in_decl)
16796 if (TREE_CODE (t) == SCOPE_REF)
16797 t = tsubst_qualified_id (t, args, complain, in_decl,
16798 /*done=*/false, /*address_p=*/false);
16799 else
16800 t = tsubst_copy_and_build (t, args, complain, in_decl,
16801 /*function_p=*/false,
16802 /*integral_constant_expression_p=*/false);
16804 return t;
16807 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16808 instantiation context. Instantiating a pack expansion containing a lambda
16809 might result in multiple lambdas all based on the same lambda in the
16810 template. */
16812 tree
16813 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16815 tree oldfn = lambda_function (t);
16816 in_decl = oldfn;
16818 tree r = build_lambda_expr ();
16820 LAMBDA_EXPR_LOCATION (r)
16821 = LAMBDA_EXPR_LOCATION (t);
16822 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16823 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16824 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16826 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16827 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16828 else
16829 record_lambda_scope (r);
16831 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16832 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16834 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16835 cap = TREE_CHAIN (cap))
16837 tree field = TREE_PURPOSE (cap);
16838 if (PACK_EXPANSION_P (field))
16839 field = PACK_EXPANSION_PATTERN (field);
16840 field = tsubst_decl (field, args, complain);
16842 if (field == error_mark_node)
16843 return error_mark_node;
16845 tree init = TREE_VALUE (cap);
16846 if (PACK_EXPANSION_P (init))
16847 init = tsubst_pack_expansion (init, args, complain, in_decl);
16848 else
16849 init = tsubst_copy_and_build (init, args, complain, in_decl,
16850 /*fn*/false, /*constexpr*/false);
16852 if (TREE_CODE (field) == TREE_VEC)
16854 int len = TREE_VEC_LENGTH (field);
16855 gcc_assert (TREE_CODE (init) == TREE_VEC
16856 && TREE_VEC_LENGTH (init) == len);
16857 for (int i = 0; i < len; ++i)
16858 LAMBDA_EXPR_CAPTURE_LIST (r)
16859 = tree_cons (TREE_VEC_ELT (field, i),
16860 TREE_VEC_ELT (init, i),
16861 LAMBDA_EXPR_CAPTURE_LIST (r));
16863 else
16865 LAMBDA_EXPR_CAPTURE_LIST (r)
16866 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16868 if (id_equal (DECL_NAME (field), "__this"))
16869 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16873 tree type = begin_lambda_type (r);
16875 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16876 determine_visibility (TYPE_NAME (type));
16878 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16880 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16881 ? DECL_TI_TEMPLATE (oldfn)
16882 : NULL_TREE);
16884 tree fntype = static_fn_type (oldfn);
16885 if (oldtmpl)
16886 ++processing_template_decl;
16887 fntype = tsubst (fntype, args, complain, in_decl);
16888 if (oldtmpl)
16889 --processing_template_decl;
16891 if (fntype == error_mark_node)
16892 r = error_mark_node;
16893 else
16895 /* Fix the type of 'this'. */
16896 fntype = build_memfn_type (fntype, type,
16897 type_memfn_quals (fntype),
16898 type_memfn_rqual (fntype));
16899 tree fn, tmpl;
16900 if (oldtmpl)
16902 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16903 fn = DECL_TEMPLATE_RESULT (tmpl);
16904 finish_member_declaration (tmpl);
16906 else
16908 tmpl = NULL_TREE;
16909 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16910 finish_member_declaration (fn);
16913 /* Let finish_function set this. */
16914 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16916 bool nested = cfun;
16917 if (nested)
16918 push_function_context ();
16920 tree body = start_lambda_function (fn, r);
16922 local_specialization_stack s (lss_copy);
16924 register_parameter_specializations (oldfn, fn);
16926 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16927 /*constexpr*/false);
16929 finish_lambda_function (body);
16931 if (nested)
16932 pop_function_context ();
16934 /* The capture list was built up in reverse order; fix that now. */
16935 LAMBDA_EXPR_CAPTURE_LIST (r)
16936 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16938 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16940 maybe_add_lambda_conv_op (type);
16943 finish_struct (type, /*attr*/NULL_TREE);
16945 insert_pending_capture_proxies ();
16947 return r;
16950 /* Like tsubst but deals with expressions and performs semantic
16951 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16953 tree
16954 tsubst_copy_and_build (tree t,
16955 tree args,
16956 tsubst_flags_t complain,
16957 tree in_decl,
16958 bool function_p,
16959 bool integral_constant_expression_p)
16961 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16962 #define RECUR(NODE) \
16963 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16964 /*function_p=*/false, \
16965 integral_constant_expression_p)
16967 tree retval, op1;
16968 location_t loc;
16970 if (t == NULL_TREE || t == error_mark_node)
16971 return t;
16973 loc = input_location;
16974 if (EXPR_HAS_LOCATION (t))
16975 input_location = EXPR_LOCATION (t);
16977 /* N3276 decltype magic only applies to calls at the top level or on the
16978 right side of a comma. */
16979 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16980 complain &= ~tf_decltype;
16982 switch (TREE_CODE (t))
16984 case USING_DECL:
16985 t = DECL_NAME (t);
16986 /* Fall through. */
16987 case IDENTIFIER_NODE:
16989 tree decl;
16990 cp_id_kind idk;
16991 bool non_integral_constant_expression_p;
16992 const char *error_msg;
16994 if (IDENTIFIER_CONV_OP_P (t))
16996 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16997 t = make_conv_op_name (new_type);
17000 /* Look up the name. */
17001 decl = lookup_name (t);
17003 /* By convention, expressions use ERROR_MARK_NODE to indicate
17004 failure, not NULL_TREE. */
17005 if (decl == NULL_TREE)
17006 decl = error_mark_node;
17008 decl = finish_id_expression (t, decl, NULL_TREE,
17009 &idk,
17010 integral_constant_expression_p,
17011 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17012 &non_integral_constant_expression_p,
17013 /*template_p=*/false,
17014 /*done=*/true,
17015 /*address_p=*/false,
17016 /*template_arg_p=*/false,
17017 &error_msg,
17018 input_location);
17019 if (error_msg)
17020 error (error_msg);
17021 if (!function_p && identifier_p (decl))
17023 if (complain & tf_error)
17024 unqualified_name_lookup_error (decl);
17025 decl = error_mark_node;
17027 RETURN (decl);
17030 case TEMPLATE_ID_EXPR:
17032 tree object;
17033 tree templ = RECUR (TREE_OPERAND (t, 0));
17034 tree targs = TREE_OPERAND (t, 1);
17036 if (targs)
17037 targs = tsubst_template_args (targs, args, complain, in_decl);
17038 if (targs == error_mark_node)
17039 return error_mark_node;
17041 if (TREE_CODE (templ) == SCOPE_REF)
17043 tree name = TREE_OPERAND (templ, 1);
17044 tree tid = lookup_template_function (name, targs);
17045 TREE_OPERAND (templ, 1) = tid;
17046 return templ;
17049 if (variable_template_p (templ))
17050 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17052 if (TREE_CODE (templ) == COMPONENT_REF)
17054 object = TREE_OPERAND (templ, 0);
17055 templ = TREE_OPERAND (templ, 1);
17057 else
17058 object = NULL_TREE;
17059 templ = lookup_template_function (templ, targs);
17061 if (object)
17062 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17063 object, templ, NULL_TREE));
17064 else
17065 RETURN (baselink_for_fns (templ));
17068 case INDIRECT_REF:
17070 tree r = RECUR (TREE_OPERAND (t, 0));
17072 if (REFERENCE_REF_P (t))
17074 /* A type conversion to reference type will be enclosed in
17075 such an indirect ref, but the substitution of the cast
17076 will have also added such an indirect ref. */
17077 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
17078 r = convert_from_reference (r);
17080 else
17081 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17082 complain|decltype_flag);
17084 if (TREE_CODE (r) == INDIRECT_REF)
17085 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17087 RETURN (r);
17090 case NOP_EXPR:
17092 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17093 tree op0 = RECUR (TREE_OPERAND (t, 0));
17094 RETURN (build_nop (type, op0));
17097 case IMPLICIT_CONV_EXPR:
17099 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17100 tree expr = RECUR (TREE_OPERAND (t, 0));
17101 int flags = LOOKUP_IMPLICIT;
17102 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17103 flags = LOOKUP_NORMAL;
17104 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17105 flags));
17108 case CONVERT_EXPR:
17110 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17111 tree op0 = RECUR (TREE_OPERAND (t, 0));
17112 RETURN (build1 (CONVERT_EXPR, type, op0));
17115 case CAST_EXPR:
17116 case REINTERPRET_CAST_EXPR:
17117 case CONST_CAST_EXPR:
17118 case DYNAMIC_CAST_EXPR:
17119 case STATIC_CAST_EXPR:
17121 tree type;
17122 tree op, r = NULL_TREE;
17124 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17125 if (integral_constant_expression_p
17126 && !cast_valid_in_integral_constant_expression_p (type))
17128 if (complain & tf_error)
17129 error ("a cast to a type other than an integral or "
17130 "enumeration type cannot appear in a constant-expression");
17131 RETURN (error_mark_node);
17134 op = RECUR (TREE_OPERAND (t, 0));
17136 warning_sentinel s(warn_useless_cast);
17137 switch (TREE_CODE (t))
17139 case CAST_EXPR:
17140 r = build_functional_cast (type, op, complain);
17141 break;
17142 case REINTERPRET_CAST_EXPR:
17143 r = build_reinterpret_cast (type, op, complain);
17144 break;
17145 case CONST_CAST_EXPR:
17146 r = build_const_cast (type, op, complain);
17147 break;
17148 case DYNAMIC_CAST_EXPR:
17149 r = build_dynamic_cast (type, op, complain);
17150 break;
17151 case STATIC_CAST_EXPR:
17152 r = build_static_cast (type, op, complain);
17153 break;
17154 default:
17155 gcc_unreachable ();
17158 RETURN (r);
17161 case POSTDECREMENT_EXPR:
17162 case POSTINCREMENT_EXPR:
17163 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17164 args, complain, in_decl);
17165 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17166 complain|decltype_flag));
17168 case PREDECREMENT_EXPR:
17169 case PREINCREMENT_EXPR:
17170 case NEGATE_EXPR:
17171 case BIT_NOT_EXPR:
17172 case ABS_EXPR:
17173 case TRUTH_NOT_EXPR:
17174 case UNARY_PLUS_EXPR: /* Unary + */
17175 case REALPART_EXPR:
17176 case IMAGPART_EXPR:
17177 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17178 RECUR (TREE_OPERAND (t, 0)),
17179 complain|decltype_flag));
17181 case FIX_TRUNC_EXPR:
17182 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17183 false, complain));
17185 case ADDR_EXPR:
17186 op1 = TREE_OPERAND (t, 0);
17187 if (TREE_CODE (op1) == LABEL_DECL)
17188 RETURN (finish_label_address_expr (DECL_NAME (op1),
17189 EXPR_LOCATION (op1)));
17190 if (TREE_CODE (op1) == SCOPE_REF)
17191 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17192 /*done=*/true, /*address_p=*/true);
17193 else
17194 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17195 in_decl);
17196 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17197 complain|decltype_flag));
17199 case PLUS_EXPR:
17200 case MINUS_EXPR:
17201 case MULT_EXPR:
17202 case TRUNC_DIV_EXPR:
17203 case CEIL_DIV_EXPR:
17204 case FLOOR_DIV_EXPR:
17205 case ROUND_DIV_EXPR:
17206 case EXACT_DIV_EXPR:
17207 case BIT_AND_EXPR:
17208 case BIT_IOR_EXPR:
17209 case BIT_XOR_EXPR:
17210 case TRUNC_MOD_EXPR:
17211 case FLOOR_MOD_EXPR:
17212 case TRUTH_ANDIF_EXPR:
17213 case TRUTH_ORIF_EXPR:
17214 case TRUTH_AND_EXPR:
17215 case TRUTH_OR_EXPR:
17216 case RSHIFT_EXPR:
17217 case LSHIFT_EXPR:
17218 case RROTATE_EXPR:
17219 case LROTATE_EXPR:
17220 case EQ_EXPR:
17221 case NE_EXPR:
17222 case MAX_EXPR:
17223 case MIN_EXPR:
17224 case LE_EXPR:
17225 case GE_EXPR:
17226 case LT_EXPR:
17227 case GT_EXPR:
17228 case MEMBER_REF:
17229 case DOTSTAR_EXPR:
17231 warning_sentinel s1(warn_type_limits);
17232 warning_sentinel s2(warn_div_by_zero);
17233 warning_sentinel s3(warn_logical_op);
17234 warning_sentinel s4(warn_tautological_compare);
17235 tree op0 = RECUR (TREE_OPERAND (t, 0));
17236 tree op1 = RECUR (TREE_OPERAND (t, 1));
17237 tree r = build_x_binary_op
17238 (input_location, TREE_CODE (t),
17239 op0,
17240 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17241 ? ERROR_MARK
17242 : TREE_CODE (TREE_OPERAND (t, 0))),
17243 op1,
17244 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17245 ? ERROR_MARK
17246 : TREE_CODE (TREE_OPERAND (t, 1))),
17247 /*overload=*/NULL,
17248 complain|decltype_flag);
17249 if (EXPR_P (r) && TREE_NO_WARNING (t))
17250 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17252 RETURN (r);
17255 case POINTER_PLUS_EXPR:
17257 tree op0 = RECUR (TREE_OPERAND (t, 0));
17258 tree op1 = RECUR (TREE_OPERAND (t, 1));
17259 return fold_build_pointer_plus (op0, op1);
17262 case SCOPE_REF:
17263 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17264 /*address_p=*/false));
17265 case ARRAY_REF:
17266 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17267 args, complain, in_decl);
17268 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17269 RECUR (TREE_OPERAND (t, 1)),
17270 complain|decltype_flag));
17272 case ARRAY_NOTATION_REF:
17274 tree start_index, length, stride;
17275 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
17276 args, complain, in_decl);
17277 start_index = RECUR (ARRAY_NOTATION_START (t));
17278 length = RECUR (ARRAY_NOTATION_LENGTH (t));
17279 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
17280 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17281 length, stride, TREE_TYPE (op1)));
17283 case SIZEOF_EXPR:
17284 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17285 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17286 RETURN (tsubst_copy (t, args, complain, in_decl));
17287 /* Fall through */
17289 case ALIGNOF_EXPR:
17291 tree r;
17293 op1 = TREE_OPERAND (t, 0);
17294 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17295 op1 = TREE_TYPE (op1);
17296 if (!args)
17298 /* When there are no ARGS, we are trying to evaluate a
17299 non-dependent expression from the parser. Trying to do
17300 the substitutions may not work. */
17301 if (!TYPE_P (op1))
17302 op1 = TREE_TYPE (op1);
17304 else
17306 ++cp_unevaluated_operand;
17307 ++c_inhibit_evaluation_warnings;
17308 if (TYPE_P (op1))
17309 op1 = tsubst (op1, args, complain, in_decl);
17310 else
17311 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17312 /*function_p=*/false,
17313 /*integral_constant_expression_p=*/
17314 false);
17315 --cp_unevaluated_operand;
17316 --c_inhibit_evaluation_warnings;
17318 if (TYPE_P (op1))
17319 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17320 complain & tf_error);
17321 else
17322 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17323 complain & tf_error);
17324 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17326 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17328 if (!processing_template_decl && TYPE_P (op1))
17330 r = build_min (SIZEOF_EXPR, size_type_node,
17331 build1 (NOP_EXPR, op1, error_mark_node));
17332 SIZEOF_EXPR_TYPE_P (r) = 1;
17334 else
17335 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17336 TREE_SIDE_EFFECTS (r) = 0;
17337 TREE_READONLY (r) = 1;
17339 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17341 RETURN (r);
17344 case AT_ENCODE_EXPR:
17346 op1 = TREE_OPERAND (t, 0);
17347 ++cp_unevaluated_operand;
17348 ++c_inhibit_evaluation_warnings;
17349 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17350 /*function_p=*/false,
17351 /*integral_constant_expression_p=*/false);
17352 --cp_unevaluated_operand;
17353 --c_inhibit_evaluation_warnings;
17354 RETURN (objc_build_encode_expr (op1));
17357 case NOEXCEPT_EXPR:
17358 op1 = TREE_OPERAND (t, 0);
17359 ++cp_unevaluated_operand;
17360 ++c_inhibit_evaluation_warnings;
17361 ++cp_noexcept_operand;
17362 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17363 /*function_p=*/false,
17364 /*integral_constant_expression_p=*/false);
17365 --cp_unevaluated_operand;
17366 --c_inhibit_evaluation_warnings;
17367 --cp_noexcept_operand;
17368 RETURN (finish_noexcept_expr (op1, complain));
17370 case MODOP_EXPR:
17372 warning_sentinel s(warn_div_by_zero);
17373 tree lhs = RECUR (TREE_OPERAND (t, 0));
17374 tree rhs = RECUR (TREE_OPERAND (t, 2));
17375 tree r = build_x_modify_expr
17376 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17377 complain|decltype_flag);
17378 /* TREE_NO_WARNING must be set if either the expression was
17379 parenthesized or it uses an operator such as >>= rather
17380 than plain assignment. In the former case, it was already
17381 set and must be copied. In the latter case,
17382 build_x_modify_expr sets it and it must not be reset
17383 here. */
17384 if (TREE_NO_WARNING (t))
17385 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17387 RETURN (r);
17390 case ARROW_EXPR:
17391 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17392 args, complain, in_decl);
17393 /* Remember that there was a reference to this entity. */
17394 if (DECL_P (op1)
17395 && !mark_used (op1, complain) && !(complain & tf_error))
17396 RETURN (error_mark_node);
17397 RETURN (build_x_arrow (input_location, op1, complain));
17399 case NEW_EXPR:
17401 tree placement = RECUR (TREE_OPERAND (t, 0));
17402 tree init = RECUR (TREE_OPERAND (t, 3));
17403 vec<tree, va_gc> *placement_vec;
17404 vec<tree, va_gc> *init_vec;
17405 tree ret;
17407 if (placement == NULL_TREE)
17408 placement_vec = NULL;
17409 else
17411 placement_vec = make_tree_vector ();
17412 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17413 vec_safe_push (placement_vec, TREE_VALUE (placement));
17416 /* If there was an initializer in the original tree, but it
17417 instantiated to an empty list, then we should pass a
17418 non-NULL empty vector to tell build_new that it was an
17419 empty initializer() rather than no initializer. This can
17420 only happen when the initializer is a pack expansion whose
17421 parameter packs are of length zero. */
17422 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17423 init_vec = NULL;
17424 else
17426 init_vec = make_tree_vector ();
17427 if (init == void_node)
17428 gcc_assert (init_vec != NULL);
17429 else
17431 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17432 vec_safe_push (init_vec, TREE_VALUE (init));
17436 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17437 tree op2 = RECUR (TREE_OPERAND (t, 2));
17438 ret = build_new (&placement_vec, op1, op2, &init_vec,
17439 NEW_EXPR_USE_GLOBAL (t),
17440 complain);
17442 if (placement_vec != NULL)
17443 release_tree_vector (placement_vec);
17444 if (init_vec != NULL)
17445 release_tree_vector (init_vec);
17447 RETURN (ret);
17450 case DELETE_EXPR:
17452 tree op0 = RECUR (TREE_OPERAND (t, 0));
17453 tree op1 = RECUR (TREE_OPERAND (t, 1));
17454 RETURN (delete_sanity (op0, op1,
17455 DELETE_EXPR_USE_VEC (t),
17456 DELETE_EXPR_USE_GLOBAL (t),
17457 complain));
17460 case COMPOUND_EXPR:
17462 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17463 complain & ~tf_decltype, in_decl,
17464 /*function_p=*/false,
17465 integral_constant_expression_p);
17466 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17467 op0,
17468 RECUR (TREE_OPERAND (t, 1)),
17469 complain|decltype_flag));
17472 case CALL_EXPR:
17474 tree function;
17475 vec<tree, va_gc> *call_args;
17476 unsigned int nargs, i;
17477 bool qualified_p;
17478 bool koenig_p;
17479 tree ret;
17481 function = CALL_EXPR_FN (t);
17482 /* Internal function with no arguments. */
17483 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17484 RETURN (t);
17486 /* When we parsed the expression, we determined whether or
17487 not Koenig lookup should be performed. */
17488 koenig_p = KOENIG_LOOKUP_P (t);
17489 if (function == NULL_TREE)
17491 koenig_p = false;
17492 qualified_p = false;
17494 else if (TREE_CODE (function) == SCOPE_REF)
17496 qualified_p = true;
17497 function = tsubst_qualified_id (function, args, complain, in_decl,
17498 /*done=*/false,
17499 /*address_p=*/false);
17501 else if (koenig_p && identifier_p (function))
17503 /* Do nothing; calling tsubst_copy_and_build on an identifier
17504 would incorrectly perform unqualified lookup again.
17506 Note that we can also have an IDENTIFIER_NODE if the earlier
17507 unqualified lookup found a member function; in that case
17508 koenig_p will be false and we do want to do the lookup
17509 again to find the instantiated member function.
17511 FIXME but doing that causes c++/15272, so we need to stop
17512 using IDENTIFIER_NODE in that situation. */
17513 qualified_p = false;
17515 else
17517 if (TREE_CODE (function) == COMPONENT_REF)
17519 tree op = TREE_OPERAND (function, 1);
17521 qualified_p = (TREE_CODE (op) == SCOPE_REF
17522 || (BASELINK_P (op)
17523 && BASELINK_QUALIFIED_P (op)));
17525 else
17526 qualified_p = false;
17528 if (TREE_CODE (function) == ADDR_EXPR
17529 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17530 /* Avoid error about taking the address of a constructor. */
17531 function = TREE_OPERAND (function, 0);
17533 function = tsubst_copy_and_build (function, args, complain,
17534 in_decl,
17535 !qualified_p,
17536 integral_constant_expression_p);
17538 if (BASELINK_P (function))
17539 qualified_p = true;
17542 nargs = call_expr_nargs (t);
17543 call_args = make_tree_vector ();
17544 for (i = 0; i < nargs; ++i)
17546 tree arg = CALL_EXPR_ARG (t, i);
17548 if (!PACK_EXPANSION_P (arg))
17549 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17550 else
17552 /* Expand the pack expansion and push each entry onto
17553 CALL_ARGS. */
17554 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17555 if (TREE_CODE (arg) == TREE_VEC)
17557 unsigned int len, j;
17559 len = TREE_VEC_LENGTH (arg);
17560 for (j = 0; j < len; ++j)
17562 tree value = TREE_VEC_ELT (arg, j);
17563 if (value != NULL_TREE)
17564 value = convert_from_reference (value);
17565 vec_safe_push (call_args, value);
17568 else
17570 /* A partial substitution. Add one entry. */
17571 vec_safe_push (call_args, arg);
17576 /* We do not perform argument-dependent lookup if normal
17577 lookup finds a non-function, in accordance with the
17578 expected resolution of DR 218. */
17579 if (koenig_p
17580 && ((is_overloaded_fn (function)
17581 /* If lookup found a member function, the Koenig lookup is
17582 not appropriate, even if an unqualified-name was used
17583 to denote the function. */
17584 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17585 || identifier_p (function))
17586 /* Only do this when substitution turns a dependent call
17587 into a non-dependent call. */
17588 && type_dependent_expression_p_push (t)
17589 && !any_type_dependent_arguments_p (call_args))
17590 function = perform_koenig_lookup (function, call_args, tf_none);
17592 if (function != NULL_TREE
17593 && identifier_p (function)
17594 && !any_type_dependent_arguments_p (call_args))
17596 if (koenig_p && (complain & tf_warning_or_error))
17598 /* For backwards compatibility and good diagnostics, try
17599 the unqualified lookup again if we aren't in SFINAE
17600 context. */
17601 tree unq = (tsubst_copy_and_build
17602 (function, args, complain, in_decl, true,
17603 integral_constant_expression_p));
17604 if (unq == error_mark_node)
17606 release_tree_vector (call_args);
17607 RETURN (error_mark_node);
17610 if (unq != function)
17612 /* In a lambda fn, we have to be careful to not
17613 introduce new this captures. Legacy code can't
17614 be using lambdas anyway, so it's ok to be
17615 stricter. */
17616 bool in_lambda = (current_class_type
17617 && LAMBDA_TYPE_P (current_class_type));
17618 char const *const msg
17619 = G_("%qD was not declared in this scope, "
17620 "and no declarations were found by "
17621 "argument-dependent lookup at the point "
17622 "of instantiation");
17624 bool diag = true;
17625 if (in_lambda)
17626 error_at (EXPR_LOC_OR_LOC (t, input_location),
17627 msg, function);
17628 else
17629 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17630 msg, function);
17631 if (diag)
17633 tree fn = unq;
17635 if (INDIRECT_REF_P (fn))
17636 fn = TREE_OPERAND (fn, 0);
17637 if (is_overloaded_fn (fn))
17638 fn = get_first_fn (fn);
17640 if (!DECL_P (fn))
17641 /* Can't say anything more. */;
17642 else if (DECL_CLASS_SCOPE_P (fn))
17644 location_t loc = EXPR_LOC_OR_LOC (t,
17645 input_location);
17646 inform (loc,
17647 "declarations in dependent base %qT are "
17648 "not found by unqualified lookup",
17649 DECL_CLASS_CONTEXT (fn));
17650 if (current_class_ptr)
17651 inform (loc,
17652 "use %<this->%D%> instead", function);
17653 else
17654 inform (loc,
17655 "use %<%T::%D%> instead",
17656 current_class_name, function);
17658 else
17659 inform (DECL_SOURCE_LOCATION (fn),
17660 "%qD declared here, later in the "
17661 "translation unit", fn);
17662 if (in_lambda)
17664 release_tree_vector (call_args);
17665 RETURN (error_mark_node);
17669 function = unq;
17672 if (identifier_p (function))
17674 if (complain & tf_error)
17675 unqualified_name_lookup_error (function);
17676 release_tree_vector (call_args);
17677 RETURN (error_mark_node);
17681 /* Remember that there was a reference to this entity. */
17682 if (function != NULL_TREE
17683 && DECL_P (function)
17684 && !mark_used (function, complain) && !(complain & tf_error))
17686 release_tree_vector (call_args);
17687 RETURN (error_mark_node);
17690 /* Put back tf_decltype for the actual call. */
17691 complain |= decltype_flag;
17693 if (function == NULL_TREE)
17694 switch (CALL_EXPR_IFN (t))
17696 case IFN_LAUNDER:
17697 gcc_assert (nargs == 1);
17698 if (vec_safe_length (call_args) != 1)
17700 error_at (EXPR_LOC_OR_LOC (t, input_location),
17701 "wrong number of arguments to "
17702 "%<__builtin_launder%>");
17703 ret = error_mark_node;
17705 else
17706 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17707 input_location),
17708 (*call_args)[0], complain);
17709 break;
17711 default:
17712 /* Unsupported internal function with arguments. */
17713 gcc_unreachable ();
17715 else if (TREE_CODE (function) == OFFSET_REF)
17716 ret = build_offset_ref_call_from_tree (function, &call_args,
17717 complain);
17718 else if (TREE_CODE (function) == COMPONENT_REF)
17720 tree instance = TREE_OPERAND (function, 0);
17721 tree fn = TREE_OPERAND (function, 1);
17723 if (processing_template_decl
17724 && (type_dependent_expression_p (instance)
17725 || (!BASELINK_P (fn)
17726 && TREE_CODE (fn) != FIELD_DECL)
17727 || type_dependent_expression_p (fn)
17728 || any_type_dependent_arguments_p (call_args)))
17729 ret = build_min_nt_call_vec (function, call_args);
17730 else if (!BASELINK_P (fn))
17731 ret = finish_call_expr (function, &call_args,
17732 /*disallow_virtual=*/false,
17733 /*koenig_p=*/false,
17734 complain);
17735 else
17736 ret = (build_new_method_call
17737 (instance, fn,
17738 &call_args, NULL_TREE,
17739 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17740 /*fn_p=*/NULL,
17741 complain));
17743 else
17744 ret = finish_call_expr (function, &call_args,
17745 /*disallow_virtual=*/qualified_p,
17746 koenig_p,
17747 complain);
17749 release_tree_vector (call_args);
17751 if (ret != error_mark_node)
17753 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17754 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17755 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17756 bool thk = CALL_FROM_THUNK_P (t);
17757 if (op || ord || rev || thk)
17759 function = extract_call_expr (ret);
17760 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17761 CALL_EXPR_ORDERED_ARGS (function) = ord;
17762 CALL_EXPR_REVERSE_ARGS (function) = rev;
17763 if (thk)
17765 CALL_FROM_THUNK_P (function) = true;
17766 /* The thunk location is not interesting. */
17767 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17772 RETURN (ret);
17775 case COND_EXPR:
17777 tree cond = RECUR (TREE_OPERAND (t, 0));
17778 tree folded_cond = fold_non_dependent_expr (cond);
17779 tree exp1, exp2;
17781 if (TREE_CODE (folded_cond) == INTEGER_CST)
17783 if (integer_zerop (folded_cond))
17785 ++c_inhibit_evaluation_warnings;
17786 exp1 = RECUR (TREE_OPERAND (t, 1));
17787 --c_inhibit_evaluation_warnings;
17788 exp2 = RECUR (TREE_OPERAND (t, 2));
17790 else
17792 exp1 = RECUR (TREE_OPERAND (t, 1));
17793 ++c_inhibit_evaluation_warnings;
17794 exp2 = RECUR (TREE_OPERAND (t, 2));
17795 --c_inhibit_evaluation_warnings;
17797 cond = folded_cond;
17799 else
17801 exp1 = RECUR (TREE_OPERAND (t, 1));
17802 exp2 = RECUR (TREE_OPERAND (t, 2));
17805 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17806 cond, exp1, exp2, complain));
17809 case PSEUDO_DTOR_EXPR:
17811 tree op0 = RECUR (TREE_OPERAND (t, 0));
17812 tree op1 = RECUR (TREE_OPERAND (t, 1));
17813 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17814 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17815 input_location));
17818 case TREE_LIST:
17820 tree purpose, value, chain;
17822 if (t == void_list_node)
17823 RETURN (t);
17825 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17826 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17828 /* We have pack expansions, so expand those and
17829 create a new list out of it. */
17830 tree purposevec = NULL_TREE;
17831 tree valuevec = NULL_TREE;
17832 tree chain;
17833 int i, len = -1;
17835 /* Expand the argument expressions. */
17836 if (TREE_PURPOSE (t))
17837 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17838 complain, in_decl);
17839 if (TREE_VALUE (t))
17840 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17841 complain, in_decl);
17843 /* Build the rest of the list. */
17844 chain = TREE_CHAIN (t);
17845 if (chain && chain != void_type_node)
17846 chain = RECUR (chain);
17848 /* Determine the number of arguments. */
17849 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17851 len = TREE_VEC_LENGTH (purposevec);
17852 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17854 else if (TREE_CODE (valuevec) == TREE_VEC)
17855 len = TREE_VEC_LENGTH (valuevec);
17856 else
17858 /* Since we only performed a partial substitution into
17859 the argument pack, we only RETURN (a single list
17860 node. */
17861 if (purposevec == TREE_PURPOSE (t)
17862 && valuevec == TREE_VALUE (t)
17863 && chain == TREE_CHAIN (t))
17864 RETURN (t);
17866 RETURN (tree_cons (purposevec, valuevec, chain));
17869 /* Convert the argument vectors into a TREE_LIST */
17870 i = len;
17871 while (i > 0)
17873 /* Grab the Ith values. */
17874 i--;
17875 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17876 : NULL_TREE;
17877 value
17878 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17879 : NULL_TREE;
17881 /* Build the list (backwards). */
17882 chain = tree_cons (purpose, value, chain);
17885 RETURN (chain);
17888 purpose = TREE_PURPOSE (t);
17889 if (purpose)
17890 purpose = RECUR (purpose);
17891 value = TREE_VALUE (t);
17892 if (value)
17893 value = RECUR (value);
17894 chain = TREE_CHAIN (t);
17895 if (chain && chain != void_type_node)
17896 chain = RECUR (chain);
17897 if (purpose == TREE_PURPOSE (t)
17898 && value == TREE_VALUE (t)
17899 && chain == TREE_CHAIN (t))
17900 RETURN (t);
17901 RETURN (tree_cons (purpose, value, chain));
17904 case COMPONENT_REF:
17906 tree object;
17907 tree object_type;
17908 tree member;
17909 tree r;
17911 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17912 args, complain, in_decl);
17913 /* Remember that there was a reference to this entity. */
17914 if (DECL_P (object)
17915 && !mark_used (object, complain) && !(complain & tf_error))
17916 RETURN (error_mark_node);
17917 object_type = TREE_TYPE (object);
17919 member = TREE_OPERAND (t, 1);
17920 if (BASELINK_P (member))
17921 member = tsubst_baselink (member,
17922 non_reference (TREE_TYPE (object)),
17923 args, complain, in_decl);
17924 else
17925 member = tsubst_copy (member, args, complain, in_decl);
17926 if (member == error_mark_node)
17927 RETURN (error_mark_node);
17929 if (TREE_CODE (member) == FIELD_DECL)
17931 r = finish_non_static_data_member (member, object, NULL_TREE);
17932 if (TREE_CODE (r) == COMPONENT_REF)
17933 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17934 RETURN (r);
17936 else if (type_dependent_expression_p (object))
17937 /* We can't do much here. */;
17938 else if (!CLASS_TYPE_P (object_type))
17940 if (scalarish_type_p (object_type))
17942 tree s = NULL_TREE;
17943 tree dtor = member;
17945 if (TREE_CODE (dtor) == SCOPE_REF)
17947 s = TREE_OPERAND (dtor, 0);
17948 dtor = TREE_OPERAND (dtor, 1);
17950 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17952 dtor = TREE_OPERAND (dtor, 0);
17953 if (TYPE_P (dtor))
17954 RETURN (finish_pseudo_destructor_expr
17955 (object, s, dtor, input_location));
17959 else if (TREE_CODE (member) == SCOPE_REF
17960 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17962 /* Lookup the template functions now that we know what the
17963 scope is. */
17964 tree scope = TREE_OPERAND (member, 0);
17965 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17966 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17967 member = lookup_qualified_name (scope, tmpl,
17968 /*is_type_p=*/false,
17969 /*complain=*/false);
17970 if (BASELINK_P (member))
17972 BASELINK_FUNCTIONS (member)
17973 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17974 args);
17975 member = (adjust_result_of_qualified_name_lookup
17976 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17977 object_type));
17979 else
17981 qualified_name_lookup_error (scope, tmpl, member,
17982 input_location);
17983 RETURN (error_mark_node);
17986 else if (TREE_CODE (member) == SCOPE_REF
17987 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17988 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17990 if (complain & tf_error)
17992 if (TYPE_P (TREE_OPERAND (member, 0)))
17993 error ("%qT is not a class or namespace",
17994 TREE_OPERAND (member, 0));
17995 else
17996 error ("%qD is not a class or namespace",
17997 TREE_OPERAND (member, 0));
17999 RETURN (error_mark_node);
18002 r = finish_class_member_access_expr (object, member,
18003 /*template_p=*/false,
18004 complain);
18005 if (TREE_CODE (r) == COMPONENT_REF)
18006 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18007 RETURN (r);
18010 case THROW_EXPR:
18011 RETURN (build_throw
18012 (RECUR (TREE_OPERAND (t, 0))));
18014 case CONSTRUCTOR:
18016 vec<constructor_elt, va_gc> *n;
18017 constructor_elt *ce;
18018 unsigned HOST_WIDE_INT idx;
18019 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18020 bool process_index_p;
18021 int newlen;
18022 bool need_copy_p = false;
18023 tree r;
18025 if (type == error_mark_node)
18026 RETURN (error_mark_node);
18028 /* digest_init will do the wrong thing if we let it. */
18029 if (type && TYPE_PTRMEMFUNC_P (type))
18030 RETURN (t);
18032 /* We do not want to process the index of aggregate
18033 initializers as they are identifier nodes which will be
18034 looked up by digest_init. */
18035 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18037 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18038 newlen = vec_safe_length (n);
18039 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18041 if (ce->index && process_index_p
18042 /* An identifier index is looked up in the type
18043 being initialized, not the current scope. */
18044 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18045 ce->index = RECUR (ce->index);
18047 if (PACK_EXPANSION_P (ce->value))
18049 /* Substitute into the pack expansion. */
18050 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18051 in_decl);
18053 if (ce->value == error_mark_node
18054 || PACK_EXPANSION_P (ce->value))
18056 else if (TREE_VEC_LENGTH (ce->value) == 1)
18057 /* Just move the argument into place. */
18058 ce->value = TREE_VEC_ELT (ce->value, 0);
18059 else
18061 /* Update the length of the final CONSTRUCTOR
18062 arguments vector, and note that we will need to
18063 copy.*/
18064 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18065 need_copy_p = true;
18068 else
18069 ce->value = RECUR (ce->value);
18072 if (need_copy_p)
18074 vec<constructor_elt, va_gc> *old_n = n;
18076 vec_alloc (n, newlen);
18077 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18079 if (TREE_CODE (ce->value) == TREE_VEC)
18081 int i, len = TREE_VEC_LENGTH (ce->value);
18082 for (i = 0; i < len; ++i)
18083 CONSTRUCTOR_APPEND_ELT (n, 0,
18084 TREE_VEC_ELT (ce->value, i));
18086 else
18087 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18091 r = build_constructor (init_list_type_node, n);
18092 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18094 if (TREE_HAS_CONSTRUCTOR (t))
18096 fcl_t cl = fcl_functional;
18097 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18098 cl = fcl_c99;
18099 RETURN (finish_compound_literal (type, r, complain, cl));
18102 TREE_TYPE (r) = type;
18103 RETURN (r);
18106 case TYPEID_EXPR:
18108 tree operand_0 = TREE_OPERAND (t, 0);
18109 if (TYPE_P (operand_0))
18111 operand_0 = tsubst (operand_0, args, complain, in_decl);
18112 RETURN (get_typeid (operand_0, complain));
18114 else
18116 operand_0 = RECUR (operand_0);
18117 RETURN (build_typeid (operand_0, complain));
18121 case VAR_DECL:
18122 if (!args)
18123 RETURN (t);
18124 else if (DECL_PACK_P (t))
18126 /* We don't build decls for an instantiation of a
18127 variadic capture proxy, we instantiate the elements
18128 when needed. */
18129 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18130 return RECUR (DECL_VALUE_EXPR (t));
18132 /* Fall through */
18134 case PARM_DECL:
18136 tree r = tsubst_copy (t, args, complain, in_decl);
18137 /* ??? We're doing a subset of finish_id_expression here. */
18138 if (VAR_P (r)
18139 && !processing_template_decl
18140 && !cp_unevaluated_operand
18141 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18142 && CP_DECL_THREAD_LOCAL_P (r))
18144 if (tree wrap = get_tls_wrapper_fn (r))
18145 /* Replace an evaluated use of the thread_local variable with
18146 a call to its wrapper. */
18147 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18149 else if (outer_automatic_var_p (r))
18151 r = process_outer_var_ref (r, complain);
18152 if (is_capture_proxy (r) && !DECL_PACK_P (t))
18153 register_local_specialization (r, t);
18156 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18157 /* If the original type was a reference, we'll be wrapped in
18158 the appropriate INDIRECT_REF. */
18159 r = convert_from_reference (r);
18160 RETURN (r);
18163 case VA_ARG_EXPR:
18165 tree op0 = RECUR (TREE_OPERAND (t, 0));
18166 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18167 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18170 case OFFSETOF_EXPR:
18172 tree object_ptr
18173 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18174 in_decl, /*function_p=*/false,
18175 /*integral_constant_expression_p=*/false);
18176 RETURN (finish_offsetof (object_ptr,
18177 RECUR (TREE_OPERAND (t, 0)),
18178 EXPR_LOCATION (t)));
18181 case ADDRESSOF_EXPR:
18182 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18183 RECUR (TREE_OPERAND (t, 0)), complain));
18185 case TRAIT_EXPR:
18187 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18188 complain, in_decl);
18190 tree type2 = TRAIT_EXPR_TYPE2 (t);
18191 if (type2 && TREE_CODE (type2) == TREE_LIST)
18192 type2 = RECUR (type2);
18193 else if (type2)
18194 type2 = tsubst (type2, args, complain, in_decl);
18196 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18199 case STMT_EXPR:
18201 tree old_stmt_expr = cur_stmt_expr;
18202 tree stmt_expr = begin_stmt_expr ();
18204 cur_stmt_expr = stmt_expr;
18205 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18206 integral_constant_expression_p);
18207 stmt_expr = finish_stmt_expr (stmt_expr, false);
18208 cur_stmt_expr = old_stmt_expr;
18210 /* If the resulting list of expression statement is empty,
18211 fold it further into void_node. */
18212 if (empty_expr_stmt_p (stmt_expr))
18213 stmt_expr = void_node;
18215 RETURN (stmt_expr);
18218 case LAMBDA_EXPR:
18220 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18222 RETURN (build_lambda_object (r));
18225 case TARGET_EXPR:
18226 /* We can get here for a constant initializer of non-dependent type.
18227 FIXME stop folding in cp_parser_initializer_clause. */
18229 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18230 complain);
18231 RETURN (r);
18234 case TRANSACTION_EXPR:
18235 RETURN (tsubst_expr(t, args, complain, in_decl,
18236 integral_constant_expression_p));
18238 case PAREN_EXPR:
18239 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18241 case VEC_PERM_EXPR:
18243 tree op0 = RECUR (TREE_OPERAND (t, 0));
18244 tree op1 = RECUR (TREE_OPERAND (t, 1));
18245 tree op2 = RECUR (TREE_OPERAND (t, 2));
18246 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18247 complain));
18250 case REQUIRES_EXPR:
18251 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18253 default:
18254 /* Handle Objective-C++ constructs, if appropriate. */
18256 tree subst
18257 = objcp_tsubst_copy_and_build (t, args, complain,
18258 in_decl, /*function_p=*/false);
18259 if (subst)
18260 RETURN (subst);
18262 RETURN (tsubst_copy (t, args, complain, in_decl));
18265 #undef RECUR
18266 #undef RETURN
18267 out:
18268 input_location = loc;
18269 return retval;
18272 /* Verify that the instantiated ARGS are valid. For type arguments,
18273 make sure that the type's linkage is ok. For non-type arguments,
18274 make sure they are constants if they are integral or enumerations.
18275 Emit an error under control of COMPLAIN, and return TRUE on error. */
18277 static bool
18278 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18280 if (dependent_template_arg_p (t))
18281 return false;
18282 if (ARGUMENT_PACK_P (t))
18284 tree vec = ARGUMENT_PACK_ARGS (t);
18285 int len = TREE_VEC_LENGTH (vec);
18286 bool result = false;
18287 int i;
18289 for (i = 0; i < len; ++i)
18290 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18291 result = true;
18292 return result;
18294 else if (TYPE_P (t))
18296 /* [basic.link]: A name with no linkage (notably, the name
18297 of a class or enumeration declared in a local scope)
18298 shall not be used to declare an entity with linkage.
18299 This implies that names with no linkage cannot be used as
18300 template arguments
18302 DR 757 relaxes this restriction for C++0x. */
18303 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18304 : no_linkage_check (t, /*relaxed_p=*/false));
18306 if (nt)
18308 /* DR 488 makes use of a type with no linkage cause
18309 type deduction to fail. */
18310 if (complain & tf_error)
18312 if (TYPE_UNNAMED_P (nt))
18313 error ("%qT is/uses unnamed type", t);
18314 else
18315 error ("template argument for %qD uses local type %qT",
18316 tmpl, t);
18318 return true;
18320 /* In order to avoid all sorts of complications, we do not
18321 allow variably-modified types as template arguments. */
18322 else if (variably_modified_type_p (t, NULL_TREE))
18324 if (complain & tf_error)
18325 error ("%qT is a variably modified type", t);
18326 return true;
18329 /* Class template and alias template arguments should be OK. */
18330 else if (DECL_TYPE_TEMPLATE_P (t))
18332 /* A non-type argument of integral or enumerated type must be a
18333 constant. */
18334 else if (TREE_TYPE (t)
18335 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18336 && !REFERENCE_REF_P (t)
18337 && !TREE_CONSTANT (t))
18339 if (complain & tf_error)
18340 error ("integral expression %qE is not constant", t);
18341 return true;
18343 return false;
18346 static bool
18347 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18349 int ix, len = DECL_NTPARMS (tmpl);
18350 bool result = false;
18352 for (ix = 0; ix != len; ix++)
18354 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18355 result = true;
18357 if (result && (complain & tf_error))
18358 error (" trying to instantiate %qD", tmpl);
18359 return result;
18362 /* We're out of SFINAE context now, so generate diagnostics for the access
18363 errors we saw earlier when instantiating D from TMPL and ARGS. */
18365 static void
18366 recheck_decl_substitution (tree d, tree tmpl, tree args)
18368 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18369 tree type = TREE_TYPE (pattern);
18370 location_t loc = input_location;
18372 push_access_scope (d);
18373 push_deferring_access_checks (dk_no_deferred);
18374 input_location = DECL_SOURCE_LOCATION (pattern);
18375 tsubst (type, args, tf_warning_or_error, d);
18376 input_location = loc;
18377 pop_deferring_access_checks ();
18378 pop_access_scope (d);
18381 /* Instantiate the indicated variable, function, or alias template TMPL with
18382 the template arguments in TARG_PTR. */
18384 static tree
18385 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18387 tree targ_ptr = orig_args;
18388 tree fndecl;
18389 tree gen_tmpl;
18390 tree spec;
18391 bool access_ok = true;
18393 if (tmpl == error_mark_node)
18394 return error_mark_node;
18396 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18398 /* If this function is a clone, handle it specially. */
18399 if (DECL_CLONED_FUNCTION_P (tmpl))
18401 tree spec;
18402 tree clone;
18404 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18405 DECL_CLONED_FUNCTION. */
18406 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18407 targ_ptr, complain);
18408 if (spec == error_mark_node)
18409 return error_mark_node;
18411 /* Look for the clone. */
18412 FOR_EACH_CLONE (clone, spec)
18413 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18414 return clone;
18415 /* We should always have found the clone by now. */
18416 gcc_unreachable ();
18417 return NULL_TREE;
18420 if (targ_ptr == error_mark_node)
18421 return error_mark_node;
18423 /* Check to see if we already have this specialization. */
18424 gen_tmpl = most_general_template (tmpl);
18425 if (TMPL_ARGS_DEPTH (targ_ptr)
18426 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18427 /* targ_ptr only has the innermost template args, so add the outer ones
18428 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18429 the case of a non-dependent call within a template definition). */
18430 targ_ptr = (add_outermost_template_args
18431 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18432 targ_ptr));
18434 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18435 but it doesn't seem to be on the hot path. */
18436 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18438 gcc_assert (tmpl == gen_tmpl
18439 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18440 == spec)
18441 || fndecl == NULL_TREE);
18443 if (spec != NULL_TREE)
18445 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18447 if (complain & tf_error)
18448 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18449 return error_mark_node;
18451 return spec;
18454 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18455 complain))
18456 return error_mark_node;
18458 /* We are building a FUNCTION_DECL, during which the access of its
18459 parameters and return types have to be checked. However this
18460 FUNCTION_DECL which is the desired context for access checking
18461 is not built yet. We solve this chicken-and-egg problem by
18462 deferring all checks until we have the FUNCTION_DECL. */
18463 push_deferring_access_checks (dk_deferred);
18465 /* Instantiation of the function happens in the context of the function
18466 template, not the context of the overload resolution we're doing. */
18467 push_to_top_level ();
18468 /* If there are dependent arguments, e.g. because we're doing partial
18469 ordering, make sure processing_template_decl stays set. */
18470 if (uses_template_parms (targ_ptr))
18471 ++processing_template_decl;
18472 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18474 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18475 complain, gen_tmpl, true);
18476 push_nested_class (ctx);
18479 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18481 fndecl = NULL_TREE;
18482 if (VAR_P (pattern))
18484 /* We need to determine if we're using a partial or explicit
18485 specialization now, because the type of the variable could be
18486 different. */
18487 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18488 tree elt = most_specialized_partial_spec (tid, complain);
18489 if (elt == error_mark_node)
18490 pattern = error_mark_node;
18491 else if (elt)
18493 tree partial_tmpl = TREE_VALUE (elt);
18494 tree partial_args = TREE_PURPOSE (elt);
18495 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18496 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18500 /* Substitute template parameters to obtain the specialization. */
18501 if (fndecl == NULL_TREE)
18502 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18503 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18504 pop_nested_class ();
18505 pop_from_top_level ();
18507 if (fndecl == error_mark_node)
18509 pop_deferring_access_checks ();
18510 return error_mark_node;
18513 /* The DECL_TI_TEMPLATE should always be the immediate parent
18514 template, not the most general template. */
18515 DECL_TI_TEMPLATE (fndecl) = tmpl;
18516 DECL_TI_ARGS (fndecl) = targ_ptr;
18518 /* Now we know the specialization, compute access previously
18519 deferred. Do no access control for inheriting constructors,
18520 as we already checked access for the inherited constructor. */
18521 if (!(flag_new_inheriting_ctors
18522 && DECL_INHERITED_CTOR (fndecl)))
18524 push_access_scope (fndecl);
18525 if (!perform_deferred_access_checks (complain))
18526 access_ok = false;
18527 pop_access_scope (fndecl);
18529 pop_deferring_access_checks ();
18531 /* If we've just instantiated the main entry point for a function,
18532 instantiate all the alternate entry points as well. We do this
18533 by cloning the instantiation of the main entry point, not by
18534 instantiating the template clones. */
18535 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18536 clone_function_decl (fndecl, /*update_methods=*/false);
18538 if (!access_ok)
18540 if (!(complain & tf_error))
18542 /* Remember to reinstantiate when we're out of SFINAE so the user
18543 can see the errors. */
18544 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18546 return error_mark_node;
18548 return fndecl;
18551 /* Wrapper for instantiate_template_1. */
18553 tree
18554 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18556 tree ret;
18557 timevar_push (TV_TEMPLATE_INST);
18558 ret = instantiate_template_1 (tmpl, orig_args, complain);
18559 timevar_pop (TV_TEMPLATE_INST);
18560 return ret;
18563 /* Instantiate the alias template TMPL with ARGS. Also push a template
18564 instantiation level, which instantiate_template doesn't do because
18565 functions and variables have sufficient context established by the
18566 callers. */
18568 static tree
18569 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18571 struct pending_template *old_last_pend = last_pending_template;
18572 struct tinst_level *old_error_tinst = last_error_tinst_level;
18573 if (tmpl == error_mark_node || args == error_mark_node)
18574 return error_mark_node;
18575 tree tinst = build_tree_list (tmpl, args);
18576 if (!push_tinst_level (tinst))
18578 ggc_free (tinst);
18579 return error_mark_node;
18582 args =
18583 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18584 args, tmpl, complain,
18585 /*require_all_args=*/true,
18586 /*use_default_args=*/true);
18588 tree r = instantiate_template (tmpl, args, complain);
18589 pop_tinst_level ();
18590 /* We can't free this if a pending_template entry or last_error_tinst_level
18591 is pointing at it. */
18592 if (last_pending_template == old_last_pend
18593 && last_error_tinst_level == old_error_tinst)
18594 ggc_free (tinst);
18596 return r;
18599 /* PARM is a template parameter pack for FN. Returns true iff
18600 PARM is used in a deducible way in the argument list of FN. */
18602 static bool
18603 pack_deducible_p (tree parm, tree fn)
18605 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18606 for (; t; t = TREE_CHAIN (t))
18608 tree type = TREE_VALUE (t);
18609 tree packs;
18610 if (!PACK_EXPANSION_P (type))
18611 continue;
18612 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18613 packs; packs = TREE_CHAIN (packs))
18614 if (template_args_equal (TREE_VALUE (packs), parm))
18616 /* The template parameter pack is used in a function parameter
18617 pack. If this is the end of the parameter list, the
18618 template parameter pack is deducible. */
18619 if (TREE_CHAIN (t) == void_list_node)
18620 return true;
18621 else
18622 /* Otherwise, not. Well, it could be deduced from
18623 a non-pack parameter, but doing so would end up with
18624 a deduction mismatch, so don't bother. */
18625 return false;
18628 /* The template parameter pack isn't used in any function parameter
18629 packs, but it might be used deeper, e.g. tuple<Args...>. */
18630 return true;
18633 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18634 NARGS elements of the arguments that are being used when calling
18635 it. TARGS is a vector into which the deduced template arguments
18636 are placed.
18638 Returns either a FUNCTION_DECL for the matching specialization of FN or
18639 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18640 true, diagnostics will be printed to explain why it failed.
18642 If FN is a conversion operator, or we are trying to produce a specific
18643 specialization, RETURN_TYPE is the return type desired.
18645 The EXPLICIT_TARGS are explicit template arguments provided via a
18646 template-id.
18648 The parameter STRICT is one of:
18650 DEDUCE_CALL:
18651 We are deducing arguments for a function call, as in
18652 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18653 deducing arguments for a call to the result of a conversion
18654 function template, as in [over.call.object].
18656 DEDUCE_CONV:
18657 We are deducing arguments for a conversion function, as in
18658 [temp.deduct.conv].
18660 DEDUCE_EXACT:
18661 We are deducing arguments when doing an explicit instantiation
18662 as in [temp.explicit], when determining an explicit specialization
18663 as in [temp.expl.spec], or when taking the address of a function
18664 template, as in [temp.deduct.funcaddr]. */
18666 tree
18667 fn_type_unification (tree fn,
18668 tree explicit_targs,
18669 tree targs,
18670 const tree *args,
18671 unsigned int nargs,
18672 tree return_type,
18673 unification_kind_t strict,
18674 int flags,
18675 bool explain_p,
18676 bool decltype_p)
18678 tree parms;
18679 tree fntype;
18680 tree decl = NULL_TREE;
18681 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18682 bool ok;
18683 static int deduction_depth;
18684 struct pending_template *old_last_pend = last_pending_template;
18685 struct tinst_level *old_error_tinst = last_error_tinst_level;
18687 tree orig_fn = fn;
18688 if (flag_new_inheriting_ctors)
18689 fn = strip_inheriting_ctors (fn);
18691 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18692 tree tinst;
18693 tree r = error_mark_node;
18695 tree full_targs = targs;
18696 if (TMPL_ARGS_DEPTH (targs)
18697 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18698 full_targs = (add_outermost_template_args
18699 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18700 targs));
18702 if (decltype_p)
18703 complain |= tf_decltype;
18705 /* In C++0x, it's possible to have a function template whose type depends
18706 on itself recursively. This is most obvious with decltype, but can also
18707 occur with enumeration scope (c++/48969). So we need to catch infinite
18708 recursion and reject the substitution at deduction time; this function
18709 will return error_mark_node for any repeated substitution.
18711 This also catches excessive recursion such as when f<N> depends on
18712 f<N-1> across all integers, and returns error_mark_node for all the
18713 substitutions back up to the initial one.
18715 This is, of course, not reentrant. */
18716 if (excessive_deduction_depth)
18717 return error_mark_node;
18718 tinst = build_tree_list (fn, NULL_TREE);
18719 ++deduction_depth;
18721 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18723 fntype = TREE_TYPE (fn);
18724 if (explicit_targs)
18726 /* [temp.deduct]
18728 The specified template arguments must match the template
18729 parameters in kind (i.e., type, nontype, template), and there
18730 must not be more arguments than there are parameters;
18731 otherwise type deduction fails.
18733 Nontype arguments must match the types of the corresponding
18734 nontype template parameters, or must be convertible to the
18735 types of the corresponding nontype parameters as specified in
18736 _temp.arg.nontype_, otherwise type deduction fails.
18738 All references in the function type of the function template
18739 to the corresponding template parameters are replaced by the
18740 specified template argument values. If a substitution in a
18741 template parameter or in the function type of the function
18742 template results in an invalid type, type deduction fails. */
18743 int i, len = TREE_VEC_LENGTH (tparms);
18744 location_t loc = input_location;
18745 bool incomplete = false;
18747 if (explicit_targs == error_mark_node)
18748 goto fail;
18750 if (TMPL_ARGS_DEPTH (explicit_targs)
18751 < TMPL_ARGS_DEPTH (full_targs))
18752 explicit_targs = add_outermost_template_args (full_targs,
18753 explicit_targs);
18755 /* Adjust any explicit template arguments before entering the
18756 substitution context. */
18757 explicit_targs
18758 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18759 complain,
18760 /*require_all_args=*/false,
18761 /*use_default_args=*/false));
18762 if (explicit_targs == error_mark_node)
18763 goto fail;
18765 /* Substitute the explicit args into the function type. This is
18766 necessary so that, for instance, explicitly declared function
18767 arguments can match null pointed constants. If we were given
18768 an incomplete set of explicit args, we must not do semantic
18769 processing during substitution as we could create partial
18770 instantiations. */
18771 for (i = 0; i < len; i++)
18773 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18774 bool parameter_pack = false;
18775 tree targ = TREE_VEC_ELT (explicit_targs, i);
18777 /* Dig out the actual parm. */
18778 if (TREE_CODE (parm) == TYPE_DECL
18779 || TREE_CODE (parm) == TEMPLATE_DECL)
18781 parm = TREE_TYPE (parm);
18782 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18784 else if (TREE_CODE (parm) == PARM_DECL)
18786 parm = DECL_INITIAL (parm);
18787 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18790 if (!parameter_pack && targ == NULL_TREE)
18791 /* No explicit argument for this template parameter. */
18792 incomplete = true;
18794 if (parameter_pack && pack_deducible_p (parm, fn))
18796 /* Mark the argument pack as "incomplete". We could
18797 still deduce more arguments during unification.
18798 We remove this mark in type_unification_real. */
18799 if (targ)
18801 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18802 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18803 = ARGUMENT_PACK_ARGS (targ);
18806 /* We have some incomplete argument packs. */
18807 incomplete = true;
18811 TREE_VALUE (tinst) = explicit_targs;
18812 if (!push_tinst_level (tinst))
18814 excessive_deduction_depth = true;
18815 goto fail;
18817 processing_template_decl += incomplete;
18818 input_location = DECL_SOURCE_LOCATION (fn);
18819 /* Ignore any access checks; we'll see them again in
18820 instantiate_template and they might have the wrong
18821 access path at this point. */
18822 push_deferring_access_checks (dk_deferred);
18823 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18824 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18825 pop_deferring_access_checks ();
18826 input_location = loc;
18827 processing_template_decl -= incomplete;
18828 pop_tinst_level ();
18830 if (fntype == error_mark_node)
18831 goto fail;
18833 /* Place the explicitly specified arguments in TARGS. */
18834 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18835 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18836 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18839 /* Never do unification on the 'this' parameter. */
18840 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18842 if (return_type && strict == DEDUCE_CALL)
18844 /* We're deducing for a call to the result of a template conversion
18845 function. The parms we really want are in return_type. */
18846 if (POINTER_TYPE_P (return_type))
18847 return_type = TREE_TYPE (return_type);
18848 parms = TYPE_ARG_TYPES (return_type);
18850 else if (return_type)
18852 tree *new_args;
18854 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18855 new_args = XALLOCAVEC (tree, nargs + 1);
18856 new_args[0] = return_type;
18857 memcpy (new_args + 1, args, nargs * sizeof (tree));
18858 args = new_args;
18859 ++nargs;
18862 /* We allow incomplete unification without an error message here
18863 because the standard doesn't seem to explicitly prohibit it. Our
18864 callers must be ready to deal with unification failures in any
18865 event. */
18867 TREE_VALUE (tinst) = targs;
18868 /* If we aren't explaining yet, push tinst context so we can see where
18869 any errors (e.g. from class instantiations triggered by instantiation
18870 of default template arguments) come from. If we are explaining, this
18871 context is redundant. */
18872 if (!explain_p && !push_tinst_level (tinst))
18874 excessive_deduction_depth = true;
18875 goto fail;
18878 /* type_unification_real will pass back any access checks from default
18879 template argument substitution. */
18880 vec<deferred_access_check, va_gc> *checks;
18881 checks = NULL;
18883 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18884 full_targs, parms, args, nargs, /*subr=*/0,
18885 strict, flags, &checks, explain_p);
18886 if (!explain_p)
18887 pop_tinst_level ();
18888 if (!ok)
18889 goto fail;
18891 /* Now that we have bindings for all of the template arguments,
18892 ensure that the arguments deduced for the template template
18893 parameters have compatible template parameter lists. We cannot
18894 check this property before we have deduced all template
18895 arguments, because the template parameter types of a template
18896 template parameter might depend on prior template parameters
18897 deduced after the template template parameter. The following
18898 ill-formed example illustrates this issue:
18900 template<typename T, template<T> class C> void f(C<5>, T);
18902 template<int N> struct X {};
18904 void g() {
18905 f(X<5>(), 5l); // error: template argument deduction fails
18908 The template parameter list of 'C' depends on the template type
18909 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18910 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18911 time that we deduce 'C'. */
18912 if (!template_template_parm_bindings_ok_p
18913 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18915 unify_inconsistent_template_template_parameters (explain_p);
18916 goto fail;
18919 /* All is well so far. Now, check:
18921 [temp.deduct]
18923 When all template arguments have been deduced, all uses of
18924 template parameters in nondeduced contexts are replaced with
18925 the corresponding deduced argument values. If the
18926 substitution results in an invalid type, as described above,
18927 type deduction fails. */
18928 TREE_VALUE (tinst) = targs;
18929 if (!push_tinst_level (tinst))
18931 excessive_deduction_depth = true;
18932 goto fail;
18935 /* Also collect access checks from the instantiation. */
18936 reopen_deferring_access_checks (checks);
18938 decl = instantiate_template (fn, targs, complain);
18940 checks = get_deferred_access_checks ();
18941 pop_deferring_access_checks ();
18943 pop_tinst_level ();
18945 if (decl == error_mark_node)
18946 goto fail;
18948 /* Now perform any access checks encountered during substitution. */
18949 push_access_scope (decl);
18950 ok = perform_access_checks (checks, complain);
18951 pop_access_scope (decl);
18952 if (!ok)
18953 goto fail;
18955 /* If we're looking for an exact match, check that what we got
18956 is indeed an exact match. It might not be if some template
18957 parameters are used in non-deduced contexts. But don't check
18958 for an exact match if we have dependent template arguments;
18959 in that case we're doing partial ordering, and we already know
18960 that we have two candidates that will provide the actual type. */
18961 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18963 tree substed = TREE_TYPE (decl);
18964 unsigned int i;
18966 tree sarg
18967 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18968 if (return_type)
18969 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18970 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18971 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18973 unify_type_mismatch (explain_p, args[i],
18974 TREE_VALUE (sarg));
18975 goto fail;
18979 /* After doing deduction with the inherited constructor, actually return an
18980 instantiation of the inheriting constructor. */
18981 if (orig_fn != fn)
18982 decl = instantiate_template (orig_fn, targs, complain);
18984 r = decl;
18986 fail:
18987 --deduction_depth;
18988 if (excessive_deduction_depth)
18990 if (deduction_depth == 0)
18991 /* Reset once we're all the way out. */
18992 excessive_deduction_depth = false;
18995 /* We can't free this if a pending_template entry or last_error_tinst_level
18996 is pointing at it. */
18997 if (last_pending_template == old_last_pend
18998 && last_error_tinst_level == old_error_tinst)
18999 ggc_free (tinst);
19001 return r;
19004 /* Adjust types before performing type deduction, as described in
19005 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19006 sections are symmetric. PARM is the type of a function parameter
19007 or the return type of the conversion function. ARG is the type of
19008 the argument passed to the call, or the type of the value
19009 initialized with the result of the conversion function.
19010 ARG_EXPR is the original argument expression, which may be null. */
19012 static int
19013 maybe_adjust_types_for_deduction (unification_kind_t strict,
19014 tree* parm,
19015 tree* arg,
19016 tree arg_expr)
19018 int result = 0;
19020 switch (strict)
19022 case DEDUCE_CALL:
19023 break;
19025 case DEDUCE_CONV:
19026 /* Swap PARM and ARG throughout the remainder of this
19027 function; the handling is precisely symmetric since PARM
19028 will initialize ARG rather than vice versa. */
19029 std::swap (parm, arg);
19030 break;
19032 case DEDUCE_EXACT:
19033 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19034 too, but here handle it by stripping the reference from PARM
19035 rather than by adding it to ARG. */
19036 if (TREE_CODE (*parm) == REFERENCE_TYPE
19037 && TYPE_REF_IS_RVALUE (*parm)
19038 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19039 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19040 && TREE_CODE (*arg) == REFERENCE_TYPE
19041 && !TYPE_REF_IS_RVALUE (*arg))
19042 *parm = TREE_TYPE (*parm);
19043 /* Nothing else to do in this case. */
19044 return 0;
19046 default:
19047 gcc_unreachable ();
19050 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19052 /* [temp.deduct.call]
19054 If P is not a reference type:
19056 --If A is an array type, the pointer type produced by the
19057 array-to-pointer standard conversion (_conv.array_) is
19058 used in place of A for type deduction; otherwise,
19060 --If A is a function type, the pointer type produced by
19061 the function-to-pointer standard conversion
19062 (_conv.func_) is used in place of A for type deduction;
19063 otherwise,
19065 --If A is a cv-qualified type, the top level
19066 cv-qualifiers of A's type are ignored for type
19067 deduction. */
19068 if (TREE_CODE (*arg) == ARRAY_TYPE)
19069 *arg = build_pointer_type (TREE_TYPE (*arg));
19070 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19071 *arg = build_pointer_type (*arg);
19072 else
19073 *arg = TYPE_MAIN_VARIANT (*arg);
19076 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19077 reference to a cv-unqualified template parameter that does not represent a
19078 template parameter of a class template (during class template argument
19079 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19080 an lvalue, the type "lvalue reference to A" is used in place of A for type
19081 deduction. */
19082 if (TREE_CODE (*parm) == REFERENCE_TYPE
19083 && TYPE_REF_IS_RVALUE (*parm)
19084 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19085 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19086 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19087 && (arg_expr ? lvalue_p (arg_expr)
19088 /* try_one_overload doesn't provide an arg_expr, but
19089 functions are always lvalues. */
19090 : TREE_CODE (*arg) == FUNCTION_TYPE))
19091 *arg = build_reference_type (*arg);
19093 /* [temp.deduct.call]
19095 If P is a cv-qualified type, the top level cv-qualifiers
19096 of P's type are ignored for type deduction. If P is a
19097 reference type, the type referred to by P is used for
19098 type deduction. */
19099 *parm = TYPE_MAIN_VARIANT (*parm);
19100 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19102 *parm = TREE_TYPE (*parm);
19103 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19106 /* DR 322. For conversion deduction, remove a reference type on parm
19107 too (which has been swapped into ARG). */
19108 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19109 *arg = TREE_TYPE (*arg);
19111 return result;
19114 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19115 template which does contain any deducible template parameters; check if
19116 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19117 unify_one_argument. */
19119 static int
19120 check_non_deducible_conversion (tree parm, tree arg, int strict,
19121 int flags, bool explain_p)
19123 tree type;
19125 if (!TYPE_P (arg))
19126 type = TREE_TYPE (arg);
19127 else
19128 type = arg;
19130 if (same_type_p (parm, type))
19131 return unify_success (explain_p);
19133 if (strict == DEDUCE_CONV)
19135 if (can_convert_arg (type, parm, NULL_TREE, flags,
19136 explain_p ? tf_warning_or_error : tf_none))
19137 return unify_success (explain_p);
19139 else if (strict != DEDUCE_EXACT)
19141 if (can_convert_arg (parm, type,
19142 TYPE_P (arg) ? NULL_TREE : arg,
19143 flags, explain_p ? tf_warning_or_error : tf_none))
19144 return unify_success (explain_p);
19147 if (strict == DEDUCE_EXACT)
19148 return unify_type_mismatch (explain_p, parm, arg);
19149 else
19150 return unify_arg_conversion (explain_p, parm, type, arg);
19153 static bool uses_deducible_template_parms (tree type);
19155 /* Returns true iff the expression EXPR is one from which a template
19156 argument can be deduced. In other words, if it's an undecorated
19157 use of a template non-type parameter. */
19159 static bool
19160 deducible_expression (tree expr)
19162 /* Strip implicit conversions. */
19163 while (CONVERT_EXPR_P (expr))
19164 expr = TREE_OPERAND (expr, 0);
19165 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19168 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19169 deducible way; that is, if it has a max value of <PARM> - 1. */
19171 static bool
19172 deducible_array_bound (tree domain)
19174 if (domain == NULL_TREE)
19175 return false;
19177 tree max = TYPE_MAX_VALUE (domain);
19178 if (TREE_CODE (max) != MINUS_EXPR)
19179 return false;
19181 return deducible_expression (TREE_OPERAND (max, 0));
19184 /* Returns true iff the template arguments ARGS use a template parameter
19185 in a deducible way. */
19187 static bool
19188 deducible_template_args (tree args)
19190 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19192 bool deducible;
19193 tree elt = TREE_VEC_ELT (args, i);
19194 if (ARGUMENT_PACK_P (elt))
19195 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19196 else
19198 if (PACK_EXPANSION_P (elt))
19199 elt = PACK_EXPANSION_PATTERN (elt);
19200 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19201 deducible = true;
19202 else if (TYPE_P (elt))
19203 deducible = uses_deducible_template_parms (elt);
19204 else
19205 deducible = deducible_expression (elt);
19207 if (deducible)
19208 return true;
19210 return false;
19213 /* Returns true iff TYPE contains any deducible references to template
19214 parameters, as per 14.8.2.5. */
19216 static bool
19217 uses_deducible_template_parms (tree type)
19219 if (PACK_EXPANSION_P (type))
19220 type = PACK_EXPANSION_PATTERN (type);
19222 /* T
19223 cv-list T
19224 TT<T>
19225 TT<i>
19226 TT<> */
19227 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19228 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19229 return true;
19231 /* T*
19233 T&& */
19234 if (POINTER_TYPE_P (type))
19235 return uses_deducible_template_parms (TREE_TYPE (type));
19237 /* T[integer-constant ]
19238 type [i] */
19239 if (TREE_CODE (type) == ARRAY_TYPE)
19240 return (uses_deducible_template_parms (TREE_TYPE (type))
19241 || deducible_array_bound (TYPE_DOMAIN (type)));
19243 /* T type ::*
19244 type T::*
19245 T T::*
19246 T (type ::*)()
19247 type (T::*)()
19248 type (type ::*)(T)
19249 type (T::*)(T)
19250 T (type ::*)(T)
19251 T (T::*)()
19252 T (T::*)(T) */
19253 if (TYPE_PTRMEM_P (type))
19254 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19255 || (uses_deducible_template_parms
19256 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19258 /* template-name <T> (where template-name refers to a class template)
19259 template-name <i> (where template-name refers to a class template) */
19260 if (CLASS_TYPE_P (type)
19261 && CLASSTYPE_TEMPLATE_INFO (type)
19262 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19263 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19264 (CLASSTYPE_TI_ARGS (type)));
19266 /* type (T)
19268 T(T) */
19269 if (TREE_CODE (type) == FUNCTION_TYPE
19270 || TREE_CODE (type) == METHOD_TYPE)
19272 if (uses_deducible_template_parms (TREE_TYPE (type)))
19273 return true;
19274 tree parm = TYPE_ARG_TYPES (type);
19275 if (TREE_CODE (type) == METHOD_TYPE)
19276 parm = TREE_CHAIN (parm);
19277 for (; parm; parm = TREE_CHAIN (parm))
19278 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19279 return true;
19282 return false;
19285 /* Subroutine of type_unification_real and unify_pack_expansion to
19286 handle unification of a single P/A pair. Parameters are as
19287 for those functions. */
19289 static int
19290 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19291 int subr, unification_kind_t strict,
19292 bool explain_p)
19294 tree arg_expr = NULL_TREE;
19295 int arg_strict;
19297 if (arg == error_mark_node || parm == error_mark_node)
19298 return unify_invalid (explain_p);
19299 if (arg == unknown_type_node)
19300 /* We can't deduce anything from this, but we might get all the
19301 template args from other function args. */
19302 return unify_success (explain_p);
19304 /* Implicit conversions (Clause 4) will be performed on a function
19305 argument to convert it to the type of the corresponding function
19306 parameter if the parameter type contains no template-parameters that
19307 participate in template argument deduction. */
19308 if (strict != DEDUCE_EXACT
19309 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19310 /* For function parameters with no deducible template parameters,
19311 just return. We'll check non-dependent conversions later. */
19312 return unify_success (explain_p);
19314 switch (strict)
19316 case DEDUCE_CALL:
19317 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19318 | UNIFY_ALLOW_MORE_CV_QUAL
19319 | UNIFY_ALLOW_DERIVED);
19320 break;
19322 case DEDUCE_CONV:
19323 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19324 break;
19326 case DEDUCE_EXACT:
19327 arg_strict = UNIFY_ALLOW_NONE;
19328 break;
19330 default:
19331 gcc_unreachable ();
19334 /* We only do these transformations if this is the top-level
19335 parameter_type_list in a call or declaration matching; in other
19336 situations (nested function declarators, template argument lists) we
19337 won't be comparing a type to an expression, and we don't do any type
19338 adjustments. */
19339 if (!subr)
19341 if (!TYPE_P (arg))
19343 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19344 if (type_unknown_p (arg))
19346 /* [temp.deduct.type] A template-argument can be
19347 deduced from a pointer to function or pointer
19348 to member function argument if the set of
19349 overloaded functions does not contain function
19350 templates and at most one of a set of
19351 overloaded functions provides a unique
19352 match. */
19353 resolve_overloaded_unification (tparms, targs, parm,
19354 arg, strict,
19355 arg_strict, explain_p);
19356 /* If a unique match was not found, this is a
19357 non-deduced context, so we still succeed. */
19358 return unify_success (explain_p);
19361 arg_expr = arg;
19362 arg = unlowered_expr_type (arg);
19363 if (arg == error_mark_node)
19364 return unify_invalid (explain_p);
19367 arg_strict |=
19368 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19370 else
19371 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19372 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19373 return unify_template_argument_mismatch (explain_p, parm, arg);
19375 /* For deduction from an init-list we need the actual list. */
19376 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19377 arg = arg_expr;
19378 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19381 /* for_each_template_parm callback that always returns 0. */
19383 static int
19384 zero_r (tree, void *)
19386 return 0;
19389 /* for_each_template_parm any_fn callback to handle deduction of a template
19390 type argument from the type of an array bound. */
19392 static int
19393 array_deduction_r (tree t, void *data)
19395 tree_pair_p d = (tree_pair_p)data;
19396 tree &tparms = d->purpose;
19397 tree &targs = d->value;
19399 if (TREE_CODE (t) == ARRAY_TYPE)
19400 if (tree dom = TYPE_DOMAIN (t))
19401 if (tree max = TYPE_MAX_VALUE (dom))
19403 if (TREE_CODE (max) == MINUS_EXPR)
19404 max = TREE_OPERAND (max, 0);
19405 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19406 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19407 UNIFY_ALLOW_NONE, /*explain*/false);
19410 /* Keep walking. */
19411 return 0;
19414 /* Try to deduce any not-yet-deduced template type arguments from the type of
19415 an array bound. This is handled separately from unify because 14.8.2.5 says
19416 "The type of a type parameter is only deduced from an array bound if it is
19417 not otherwise deduced." */
19419 static void
19420 try_array_deduction (tree tparms, tree targs, tree parm)
19422 tree_pair_s data = { tparms, targs };
19423 hash_set<tree> visited;
19424 for_each_template_parm (parm, zero_r, &data, &visited,
19425 /*nondeduced*/false, array_deduction_r);
19428 /* Most parms like fn_type_unification.
19430 If SUBR is 1, we're being called recursively (to unify the
19431 arguments of a function or method parameter of a function
19432 template).
19434 CHECKS is a pointer to a vector of access checks encountered while
19435 substituting default template arguments. */
19437 static int
19438 type_unification_real (tree tparms,
19439 tree full_targs,
19440 tree xparms,
19441 const tree *xargs,
19442 unsigned int xnargs,
19443 int subr,
19444 unification_kind_t strict,
19445 int flags,
19446 vec<deferred_access_check, va_gc> **checks,
19447 bool explain_p)
19449 tree parm, arg;
19450 int i;
19451 int ntparms = TREE_VEC_LENGTH (tparms);
19452 int saw_undeduced = 0;
19453 tree parms;
19454 const tree *args;
19455 unsigned int nargs;
19456 unsigned int ia;
19458 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19459 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19460 gcc_assert (ntparms > 0);
19462 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19464 /* Reset the number of non-defaulted template arguments contained
19465 in TARGS. */
19466 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19468 again:
19469 parms = xparms;
19470 args = xargs;
19471 nargs = xnargs;
19473 ia = 0;
19474 while (parms && parms != void_list_node
19475 && ia < nargs)
19477 parm = TREE_VALUE (parms);
19479 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19480 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19481 /* For a function parameter pack that occurs at the end of the
19482 parameter-declaration-list, the type A of each remaining
19483 argument of the call is compared with the type P of the
19484 declarator-id of the function parameter pack. */
19485 break;
19487 parms = TREE_CHAIN (parms);
19489 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19490 /* For a function parameter pack that does not occur at the
19491 end of the parameter-declaration-list, the type of the
19492 parameter pack is a non-deduced context. */
19493 continue;
19495 arg = args[ia];
19496 ++ia;
19498 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19499 explain_p))
19500 return 1;
19503 if (parms
19504 && parms != void_list_node
19505 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19507 /* Unify the remaining arguments with the pack expansion type. */
19508 tree argvec;
19509 tree parmvec = make_tree_vec (1);
19511 /* Allocate a TREE_VEC and copy in all of the arguments */
19512 argvec = make_tree_vec (nargs - ia);
19513 for (i = 0; ia < nargs; ++ia, ++i)
19514 TREE_VEC_ELT (argvec, i) = args[ia];
19516 /* Copy the parameter into parmvec. */
19517 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19518 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19519 /*subr=*/subr, explain_p))
19520 return 1;
19522 /* Advance to the end of the list of parameters. */
19523 parms = TREE_CHAIN (parms);
19526 /* Fail if we've reached the end of the parm list, and more args
19527 are present, and the parm list isn't variadic. */
19528 if (ia < nargs && parms == void_list_node)
19529 return unify_too_many_arguments (explain_p, nargs, ia);
19530 /* Fail if parms are left and they don't have default values and
19531 they aren't all deduced as empty packs (c++/57397). This is
19532 consistent with sufficient_parms_p. */
19533 if (parms && parms != void_list_node
19534 && TREE_PURPOSE (parms) == NULL_TREE)
19536 unsigned int count = nargs;
19537 tree p = parms;
19538 bool type_pack_p;
19541 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19542 if (!type_pack_p)
19543 count++;
19544 p = TREE_CHAIN (p);
19546 while (p && p != void_list_node);
19547 if (count != nargs)
19548 return unify_too_few_arguments (explain_p, ia, count,
19549 type_pack_p);
19552 if (!subr)
19554 tsubst_flags_t complain = (explain_p
19555 ? tf_warning_or_error
19556 : tf_none);
19557 bool tried_array_deduction = (cxx_dialect < cxx1z);
19559 for (i = 0; i < ntparms; i++)
19561 tree targ = TREE_VEC_ELT (targs, i);
19562 tree tparm = TREE_VEC_ELT (tparms, i);
19564 /* Clear the "incomplete" flags on all argument packs now so that
19565 substituting them into later default arguments works. */
19566 if (targ && ARGUMENT_PACK_P (targ))
19568 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19569 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19572 if (targ || tparm == error_mark_node)
19573 continue;
19574 tparm = TREE_VALUE (tparm);
19576 if (TREE_CODE (tparm) == TYPE_DECL
19577 && !tried_array_deduction)
19579 try_array_deduction (tparms, targs, xparms);
19580 tried_array_deduction = true;
19581 if (TREE_VEC_ELT (targs, i))
19582 continue;
19585 /* If this is an undeduced nontype parameter that depends on
19586 a type parameter, try another pass; its type may have been
19587 deduced from a later argument than the one from which
19588 this parameter can be deduced. */
19589 if (TREE_CODE (tparm) == PARM_DECL
19590 && uses_template_parms (TREE_TYPE (tparm))
19591 && saw_undeduced < 2)
19593 saw_undeduced = 1;
19594 continue;
19597 /* Core issue #226 (C++0x) [temp.deduct]:
19599 If a template argument has not been deduced, its
19600 default template argument, if any, is used.
19602 When we are in C++98 mode, TREE_PURPOSE will either
19603 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19604 to explicitly check cxx_dialect here. */
19605 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19606 /* OK, there is a default argument. Wait until after the
19607 conversion check to do substitution. */
19608 continue;
19610 /* If the type parameter is a parameter pack, then it will
19611 be deduced to an empty parameter pack. */
19612 if (template_parameter_pack_p (tparm))
19614 tree arg;
19616 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19618 arg = make_node (NONTYPE_ARGUMENT_PACK);
19619 TREE_CONSTANT (arg) = 1;
19621 else
19622 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19624 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19626 TREE_VEC_ELT (targs, i) = arg;
19627 continue;
19630 return unify_parameter_deduction_failure (explain_p, tparm);
19633 /* DR 1391: All parameters have args, now check non-dependent parms for
19634 convertibility. */
19635 if (saw_undeduced < 2)
19636 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19637 parms && parms != void_list_node && ia < nargs; )
19639 parm = TREE_VALUE (parms);
19641 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19642 && (!TREE_CHAIN (parms)
19643 || TREE_CHAIN (parms) == void_list_node))
19644 /* For a function parameter pack that occurs at the end of the
19645 parameter-declaration-list, the type A of each remaining
19646 argument of the call is compared with the type P of the
19647 declarator-id of the function parameter pack. */
19648 break;
19650 parms = TREE_CHAIN (parms);
19652 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19653 /* For a function parameter pack that does not occur at the
19654 end of the parameter-declaration-list, the type of the
19655 parameter pack is a non-deduced context. */
19656 continue;
19658 arg = args[ia];
19659 ++ia;
19661 if (uses_template_parms (parm))
19662 continue;
19663 if (check_non_deducible_conversion (parm, arg, strict, flags,
19664 explain_p))
19665 return 1;
19668 /* Now substitute into the default template arguments. */
19669 for (i = 0; i < ntparms; i++)
19671 tree targ = TREE_VEC_ELT (targs, i);
19672 tree tparm = TREE_VEC_ELT (tparms, i);
19674 if (targ || tparm == error_mark_node)
19675 continue;
19676 tree parm = TREE_VALUE (tparm);
19678 if (TREE_CODE (parm) == PARM_DECL
19679 && uses_template_parms (TREE_TYPE (parm))
19680 && saw_undeduced < 2)
19681 continue;
19683 tree arg = TREE_PURPOSE (tparm);
19684 reopen_deferring_access_checks (*checks);
19685 location_t save_loc = input_location;
19686 if (DECL_P (parm))
19687 input_location = DECL_SOURCE_LOCATION (parm);
19688 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19689 if (!uses_template_parms (arg))
19690 arg = convert_template_argument (parm, arg, full_targs, complain,
19691 i, NULL_TREE);
19692 else if (saw_undeduced < 2)
19693 arg = NULL_TREE;
19694 else
19695 arg = error_mark_node;
19696 input_location = save_loc;
19697 *checks = get_deferred_access_checks ();
19698 pop_deferring_access_checks ();
19699 if (arg == error_mark_node)
19700 return 1;
19701 else if (arg)
19703 TREE_VEC_ELT (targs, i) = arg;
19704 /* The position of the first default template argument,
19705 is also the number of non-defaulted arguments in TARGS.
19706 Record that. */
19707 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19708 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19712 if (saw_undeduced++ == 1)
19713 goto again;
19716 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19717 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19719 return unify_success (explain_p);
19722 /* Subroutine of type_unification_real. Args are like the variables
19723 at the call site. ARG is an overloaded function (or template-id);
19724 we try deducing template args from each of the overloads, and if
19725 only one succeeds, we go with that. Modifies TARGS and returns
19726 true on success. */
19728 static bool
19729 resolve_overloaded_unification (tree tparms,
19730 tree targs,
19731 tree parm,
19732 tree arg,
19733 unification_kind_t strict,
19734 int sub_strict,
19735 bool explain_p)
19737 tree tempargs = copy_node (targs);
19738 int good = 0;
19739 tree goodfn = NULL_TREE;
19740 bool addr_p;
19742 if (TREE_CODE (arg) == ADDR_EXPR)
19744 arg = TREE_OPERAND (arg, 0);
19745 addr_p = true;
19747 else
19748 addr_p = false;
19750 if (TREE_CODE (arg) == COMPONENT_REF)
19751 /* Handle `&x' where `x' is some static or non-static member
19752 function name. */
19753 arg = TREE_OPERAND (arg, 1);
19755 if (TREE_CODE (arg) == OFFSET_REF)
19756 arg = TREE_OPERAND (arg, 1);
19758 /* Strip baselink information. */
19759 if (BASELINK_P (arg))
19760 arg = BASELINK_FUNCTIONS (arg);
19762 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19764 /* If we got some explicit template args, we need to plug them into
19765 the affected templates before we try to unify, in case the
19766 explicit args will completely resolve the templates in question. */
19768 int ok = 0;
19769 tree expl_subargs = TREE_OPERAND (arg, 1);
19770 arg = TREE_OPERAND (arg, 0);
19772 for (lkp_iterator iter (arg); iter; ++iter)
19774 tree fn = *iter;
19775 tree subargs, elem;
19777 if (TREE_CODE (fn) != TEMPLATE_DECL)
19778 continue;
19780 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19781 expl_subargs, NULL_TREE, tf_none,
19782 /*require_all_args=*/true,
19783 /*use_default_args=*/true);
19784 if (subargs != error_mark_node
19785 && !any_dependent_template_arguments_p (subargs))
19787 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19788 if (try_one_overload (tparms, targs, tempargs, parm,
19789 elem, strict, sub_strict, addr_p, explain_p)
19790 && (!goodfn || !same_type_p (goodfn, elem)))
19792 goodfn = elem;
19793 ++good;
19796 else if (subargs)
19797 ++ok;
19799 /* If no templates (or more than one) are fully resolved by the
19800 explicit arguments, this template-id is a non-deduced context; it
19801 could still be OK if we deduce all template arguments for the
19802 enclosing call through other arguments. */
19803 if (good != 1)
19804 good = ok;
19806 else if (TREE_CODE (arg) != OVERLOAD
19807 && TREE_CODE (arg) != FUNCTION_DECL)
19808 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19809 -- but the deduction does not succeed because the expression is
19810 not just the function on its own. */
19811 return false;
19812 else
19813 for (lkp_iterator iter (arg); iter; ++iter)
19815 tree fn = *iter;
19816 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19817 strict, sub_strict, addr_p, explain_p)
19818 && (!goodfn || !decls_match (goodfn, fn)))
19820 goodfn = fn;
19821 ++good;
19825 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19826 to function or pointer to member function argument if the set of
19827 overloaded functions does not contain function templates and at most
19828 one of a set of overloaded functions provides a unique match.
19830 So if we found multiple possibilities, we return success but don't
19831 deduce anything. */
19833 if (good == 1)
19835 int i = TREE_VEC_LENGTH (targs);
19836 for (; i--; )
19837 if (TREE_VEC_ELT (tempargs, i))
19839 tree old = TREE_VEC_ELT (targs, i);
19840 tree new_ = TREE_VEC_ELT (tempargs, i);
19841 if (new_ && old && ARGUMENT_PACK_P (old)
19842 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19843 /* Don't forget explicit template arguments in a pack. */
19844 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19845 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19846 TREE_VEC_ELT (targs, i) = new_;
19849 if (good)
19850 return true;
19852 return false;
19855 /* Core DR 115: In contexts where deduction is done and fails, or in
19856 contexts where deduction is not done, if a template argument list is
19857 specified and it, along with any default template arguments, identifies
19858 a single function template specialization, then the template-id is an
19859 lvalue for the function template specialization. */
19861 tree
19862 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19864 tree expr, offset, baselink;
19865 bool addr;
19867 if (!type_unknown_p (orig_expr))
19868 return orig_expr;
19870 expr = orig_expr;
19871 addr = false;
19872 offset = NULL_TREE;
19873 baselink = NULL_TREE;
19875 if (TREE_CODE (expr) == ADDR_EXPR)
19877 expr = TREE_OPERAND (expr, 0);
19878 addr = true;
19880 if (TREE_CODE (expr) == OFFSET_REF)
19882 offset = expr;
19883 expr = TREE_OPERAND (expr, 1);
19885 if (BASELINK_P (expr))
19887 baselink = expr;
19888 expr = BASELINK_FUNCTIONS (expr);
19891 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19893 int good = 0;
19894 tree goodfn = NULL_TREE;
19896 /* If we got some explicit template args, we need to plug them into
19897 the affected templates before we try to unify, in case the
19898 explicit args will completely resolve the templates in question. */
19900 tree expl_subargs = TREE_OPERAND (expr, 1);
19901 tree arg = TREE_OPERAND (expr, 0);
19902 tree badfn = NULL_TREE;
19903 tree badargs = NULL_TREE;
19905 for (lkp_iterator iter (arg); iter; ++iter)
19907 tree fn = *iter;
19908 tree subargs, elem;
19910 if (TREE_CODE (fn) != TEMPLATE_DECL)
19911 continue;
19913 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19914 expl_subargs, NULL_TREE, tf_none,
19915 /*require_all_args=*/true,
19916 /*use_default_args=*/true);
19917 if (subargs != error_mark_node
19918 && !any_dependent_template_arguments_p (subargs))
19920 elem = instantiate_template (fn, subargs, tf_none);
19921 if (elem == error_mark_node)
19923 badfn = fn;
19924 badargs = subargs;
19926 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19928 goodfn = elem;
19929 ++good;
19933 if (good == 1)
19935 mark_used (goodfn);
19936 expr = goodfn;
19937 if (baselink)
19938 expr = build_baselink (BASELINK_BINFO (baselink),
19939 BASELINK_ACCESS_BINFO (baselink),
19940 expr, BASELINK_OPTYPE (baselink));
19941 if (offset)
19943 tree base
19944 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19945 expr = build_offset_ref (base, expr, addr, complain);
19947 if (addr)
19948 expr = cp_build_addr_expr (expr, complain);
19949 return expr;
19951 else if (good == 0 && badargs && (complain & tf_error))
19952 /* There were no good options and at least one bad one, so let the
19953 user know what the problem is. */
19954 instantiate_template (badfn, badargs, complain);
19956 return orig_expr;
19959 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19960 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19961 different overloads deduce different arguments for a given parm.
19962 ADDR_P is true if the expression for which deduction is being
19963 performed was of the form "& fn" rather than simply "fn".
19965 Returns 1 on success. */
19967 static int
19968 try_one_overload (tree tparms,
19969 tree orig_targs,
19970 tree targs,
19971 tree parm,
19972 tree arg,
19973 unification_kind_t strict,
19974 int sub_strict,
19975 bool addr_p,
19976 bool explain_p)
19978 int nargs;
19979 tree tempargs;
19980 int i;
19982 if (arg == error_mark_node)
19983 return 0;
19985 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19986 to function or pointer to member function argument if the set of
19987 overloaded functions does not contain function templates and at most
19988 one of a set of overloaded functions provides a unique match.
19990 So if this is a template, just return success. */
19992 if (uses_template_parms (arg))
19993 return 1;
19995 if (TREE_CODE (arg) == METHOD_TYPE)
19996 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19997 else if (addr_p)
19998 arg = build_pointer_type (arg);
20000 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20002 /* We don't copy orig_targs for this because if we have already deduced
20003 some template args from previous args, unify would complain when we
20004 try to deduce a template parameter for the same argument, even though
20005 there isn't really a conflict. */
20006 nargs = TREE_VEC_LENGTH (targs);
20007 tempargs = make_tree_vec (nargs);
20009 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20010 return 0;
20012 /* First make sure we didn't deduce anything that conflicts with
20013 explicitly specified args. */
20014 for (i = nargs; i--; )
20016 tree elt = TREE_VEC_ELT (tempargs, i);
20017 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20019 if (!elt)
20020 /*NOP*/;
20021 else if (uses_template_parms (elt))
20022 /* Since we're unifying against ourselves, we will fill in
20023 template args used in the function parm list with our own
20024 template parms. Discard them. */
20025 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20026 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20028 /* Check that the argument at each index of the deduced argument pack
20029 is equivalent to the corresponding explicitly specified argument.
20030 We may have deduced more arguments than were explicitly specified,
20031 and that's OK. */
20033 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20034 that's wrong if we deduce the same argument pack from multiple
20035 function arguments: it's only incomplete the first time. */
20037 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20038 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20040 if (TREE_VEC_LENGTH (deduced_pack)
20041 < TREE_VEC_LENGTH (explicit_pack))
20042 return 0;
20044 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20045 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20046 TREE_VEC_ELT (deduced_pack, j)))
20047 return 0;
20049 else if (oldelt && !template_args_equal (oldelt, elt))
20050 return 0;
20053 for (i = nargs; i--; )
20055 tree elt = TREE_VEC_ELT (tempargs, i);
20057 if (elt)
20058 TREE_VEC_ELT (targs, i) = elt;
20061 return 1;
20064 /* PARM is a template class (perhaps with unbound template
20065 parameters). ARG is a fully instantiated type. If ARG can be
20066 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20067 TARGS are as for unify. */
20069 static tree
20070 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20071 bool explain_p)
20073 tree copy_of_targs;
20075 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20076 return NULL_TREE;
20077 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20078 /* Matches anything. */;
20079 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20080 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20081 return NULL_TREE;
20083 /* We need to make a new template argument vector for the call to
20084 unify. If we used TARGS, we'd clutter it up with the result of
20085 the attempted unification, even if this class didn't work out.
20086 We also don't want to commit ourselves to all the unifications
20087 we've already done, since unification is supposed to be done on
20088 an argument-by-argument basis. In other words, consider the
20089 following pathological case:
20091 template <int I, int J, int K>
20092 struct S {};
20094 template <int I, int J>
20095 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20097 template <int I, int J, int K>
20098 void f(S<I, J, K>, S<I, I, I>);
20100 void g() {
20101 S<0, 0, 0> s0;
20102 S<0, 1, 2> s2;
20104 f(s0, s2);
20107 Now, by the time we consider the unification involving `s2', we
20108 already know that we must have `f<0, 0, 0>'. But, even though
20109 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20110 because there are two ways to unify base classes of S<0, 1, 2>
20111 with S<I, I, I>. If we kept the already deduced knowledge, we
20112 would reject the possibility I=1. */
20113 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20115 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20117 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20118 return NULL_TREE;
20119 return arg;
20122 /* If unification failed, we're done. */
20123 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20124 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20125 return NULL_TREE;
20127 return arg;
20130 /* Given a template type PARM and a class type ARG, find the unique
20131 base type in ARG that is an instance of PARM. We do not examine
20132 ARG itself; only its base-classes. If there is not exactly one
20133 appropriate base class, return NULL_TREE. PARM may be the type of
20134 a partial specialization, as well as a plain template type. Used
20135 by unify. */
20137 static enum template_base_result
20138 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20139 bool explain_p, tree *result)
20141 tree rval = NULL_TREE;
20142 tree binfo;
20144 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20146 binfo = TYPE_BINFO (complete_type (arg));
20147 if (!binfo)
20149 /* The type could not be completed. */
20150 *result = NULL_TREE;
20151 return tbr_incomplete_type;
20154 /* Walk in inheritance graph order. The search order is not
20155 important, and this avoids multiple walks of virtual bases. */
20156 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20158 tree r = try_class_unification (tparms, targs, parm,
20159 BINFO_TYPE (binfo), explain_p);
20161 if (r)
20163 /* If there is more than one satisfactory baseclass, then:
20165 [temp.deduct.call]
20167 If they yield more than one possible deduced A, the type
20168 deduction fails.
20170 applies. */
20171 if (rval && !same_type_p (r, rval))
20173 *result = NULL_TREE;
20174 return tbr_ambiguous_baseclass;
20177 rval = r;
20181 *result = rval;
20182 return tbr_success;
20185 /* Returns the level of DECL, which declares a template parameter. */
20187 static int
20188 template_decl_level (tree decl)
20190 switch (TREE_CODE (decl))
20192 case TYPE_DECL:
20193 case TEMPLATE_DECL:
20194 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20196 case PARM_DECL:
20197 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20199 default:
20200 gcc_unreachable ();
20202 return 0;
20205 /* Decide whether ARG can be unified with PARM, considering only the
20206 cv-qualifiers of each type, given STRICT as documented for unify.
20207 Returns nonzero iff the unification is OK on that basis. */
20209 static int
20210 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20212 int arg_quals = cp_type_quals (arg);
20213 int parm_quals = cp_type_quals (parm);
20215 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20216 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20218 /* Although a CVR qualifier is ignored when being applied to a
20219 substituted template parameter ([8.3.2]/1 for example), that
20220 does not allow us to unify "const T" with "int&" because both
20221 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20222 It is ok when we're allowing additional CV qualifiers
20223 at the outer level [14.8.2.1]/3,1st bullet. */
20224 if ((TREE_CODE (arg) == REFERENCE_TYPE
20225 || TREE_CODE (arg) == FUNCTION_TYPE
20226 || TREE_CODE (arg) == METHOD_TYPE)
20227 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20228 return 0;
20230 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20231 && (parm_quals & TYPE_QUAL_RESTRICT))
20232 return 0;
20235 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20236 && (arg_quals & parm_quals) != parm_quals)
20237 return 0;
20239 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20240 && (parm_quals & arg_quals) != arg_quals)
20241 return 0;
20243 return 1;
20246 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20247 void
20248 template_parm_level_and_index (tree parm, int* level, int* index)
20250 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20251 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20252 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20254 *index = TEMPLATE_TYPE_IDX (parm);
20255 *level = TEMPLATE_TYPE_LEVEL (parm);
20257 else
20259 *index = TEMPLATE_PARM_IDX (parm);
20260 *level = TEMPLATE_PARM_LEVEL (parm);
20264 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20265 do { \
20266 if (unify (TP, TA, P, A, S, EP)) \
20267 return 1; \
20268 } while (0)
20270 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20271 expansion at the end of PACKED_PARMS. Returns 0 if the type
20272 deduction succeeds, 1 otherwise. STRICT is the same as in
20273 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20274 function call argument list. We'll need to adjust the arguments to make them
20275 types. SUBR tells us if this is from a recursive call to
20276 type_unification_real, or for comparing two template argument
20277 lists. */
20279 static int
20280 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20281 tree packed_args, unification_kind_t strict,
20282 bool subr, bool explain_p)
20284 tree parm
20285 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20286 tree pattern = PACK_EXPANSION_PATTERN (parm);
20287 tree pack, packs = NULL_TREE;
20288 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20290 /* Add in any args remembered from an earlier partial instantiation. */
20291 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20293 packed_args = expand_template_argument_pack (packed_args);
20295 int len = TREE_VEC_LENGTH (packed_args);
20297 /* Determine the parameter packs we will be deducing from the
20298 pattern, and record their current deductions. */
20299 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20300 pack; pack = TREE_CHAIN (pack))
20302 tree parm_pack = TREE_VALUE (pack);
20303 int idx, level;
20305 /* Determine the index and level of this parameter pack. */
20306 template_parm_level_and_index (parm_pack, &level, &idx);
20308 /* Keep track of the parameter packs and their corresponding
20309 argument packs. */
20310 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20311 TREE_TYPE (packs) = make_tree_vec (len - start);
20314 /* Loop through all of the arguments that have not yet been
20315 unified and unify each with the pattern. */
20316 for (i = start; i < len; i++)
20318 tree parm;
20319 bool any_explicit = false;
20320 tree arg = TREE_VEC_ELT (packed_args, i);
20322 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20323 or the element of its argument pack at the current index if
20324 this argument was explicitly specified. */
20325 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20327 int idx, level;
20328 tree arg, pargs;
20329 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20331 arg = NULL_TREE;
20332 if (TREE_VALUE (pack)
20333 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20334 && (i - start < TREE_VEC_LENGTH (pargs)))
20336 any_explicit = true;
20337 arg = TREE_VEC_ELT (pargs, i - start);
20339 TMPL_ARG (targs, level, idx) = arg;
20342 /* If we had explicit template arguments, substitute them into the
20343 pattern before deduction. */
20344 if (any_explicit)
20346 /* Some arguments might still be unspecified or dependent. */
20347 bool dependent;
20348 ++processing_template_decl;
20349 dependent = any_dependent_template_arguments_p (targs);
20350 if (!dependent)
20351 --processing_template_decl;
20352 parm = tsubst (pattern, targs,
20353 explain_p ? tf_warning_or_error : tf_none,
20354 NULL_TREE);
20355 if (dependent)
20356 --processing_template_decl;
20357 if (parm == error_mark_node)
20358 return 1;
20360 else
20361 parm = pattern;
20363 /* Unify the pattern with the current argument. */
20364 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20365 explain_p))
20366 return 1;
20368 /* For each parameter pack, collect the deduced value. */
20369 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20371 int idx, level;
20372 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20374 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20375 TMPL_ARG (targs, level, idx);
20379 /* Verify that the results of unification with the parameter packs
20380 produce results consistent with what we've seen before, and make
20381 the deduced argument packs available. */
20382 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20384 tree old_pack = TREE_VALUE (pack);
20385 tree new_args = TREE_TYPE (pack);
20386 int i, len = TREE_VEC_LENGTH (new_args);
20387 int idx, level;
20388 bool nondeduced_p = false;
20390 /* By default keep the original deduced argument pack.
20391 If necessary, more specific code is going to update the
20392 resulting deduced argument later down in this function. */
20393 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20394 TMPL_ARG (targs, level, idx) = old_pack;
20396 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20397 actually deduce anything. */
20398 for (i = 0; i < len && !nondeduced_p; ++i)
20399 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20400 nondeduced_p = true;
20401 if (nondeduced_p)
20402 continue;
20404 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20406 /* If we had fewer function args than explicit template args,
20407 just use the explicits. */
20408 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20409 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20410 if (len < explicit_len)
20411 new_args = explicit_args;
20414 if (!old_pack)
20416 tree result;
20417 /* Build the deduced *_ARGUMENT_PACK. */
20418 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20420 result = make_node (NONTYPE_ARGUMENT_PACK);
20421 TREE_CONSTANT (result) = 1;
20423 else
20424 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20426 SET_ARGUMENT_PACK_ARGS (result, new_args);
20428 /* Note the deduced argument packs for this parameter
20429 pack. */
20430 TMPL_ARG (targs, level, idx) = result;
20432 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20433 && (ARGUMENT_PACK_ARGS (old_pack)
20434 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20436 /* We only had the explicitly-provided arguments before, but
20437 now we have a complete set of arguments. */
20438 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20440 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20441 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20442 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20444 else
20446 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20447 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20449 if (!comp_template_args (old_args, new_args,
20450 &bad_old_arg, &bad_new_arg))
20451 /* Inconsistent unification of this parameter pack. */
20452 return unify_parameter_pack_inconsistent (explain_p,
20453 bad_old_arg,
20454 bad_new_arg);
20458 return unify_success (explain_p);
20461 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20462 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20463 parameters and return value are as for unify. */
20465 static int
20466 unify_array_domain (tree tparms, tree targs,
20467 tree parm_dom, tree arg_dom,
20468 bool explain_p)
20470 tree parm_max;
20471 tree arg_max;
20472 bool parm_cst;
20473 bool arg_cst;
20475 /* Our representation of array types uses "N - 1" as the
20476 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20477 not an integer constant. We cannot unify arbitrarily
20478 complex expressions, so we eliminate the MINUS_EXPRs
20479 here. */
20480 parm_max = TYPE_MAX_VALUE (parm_dom);
20481 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20482 if (!parm_cst)
20484 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20485 parm_max = TREE_OPERAND (parm_max, 0);
20487 arg_max = TYPE_MAX_VALUE (arg_dom);
20488 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20489 if (!arg_cst)
20491 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20492 trying to unify the type of a variable with the type
20493 of a template parameter. For example:
20495 template <unsigned int N>
20496 void f (char (&) [N]);
20497 int g();
20498 void h(int i) {
20499 char a[g(i)];
20500 f(a);
20503 Here, the type of the ARG will be "int [g(i)]", and
20504 may be a SAVE_EXPR, etc. */
20505 if (TREE_CODE (arg_max) != MINUS_EXPR)
20506 return unify_vla_arg (explain_p, arg_dom);
20507 arg_max = TREE_OPERAND (arg_max, 0);
20510 /* If only one of the bounds used a MINUS_EXPR, compensate
20511 by adding one to the other bound. */
20512 if (parm_cst && !arg_cst)
20513 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20514 integer_type_node,
20515 parm_max,
20516 integer_one_node);
20517 else if (arg_cst && !parm_cst)
20518 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20519 integer_type_node,
20520 arg_max,
20521 integer_one_node);
20523 return unify (tparms, targs, parm_max, arg_max,
20524 UNIFY_ALLOW_INTEGER, explain_p);
20527 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20529 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20531 static pa_kind_t
20532 pa_kind (tree t)
20534 if (PACK_EXPANSION_P (t))
20535 t = PACK_EXPANSION_PATTERN (t);
20536 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20537 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20538 || DECL_TYPE_TEMPLATE_P (t))
20539 return pa_tmpl;
20540 else if (TYPE_P (t))
20541 return pa_type;
20542 else
20543 return pa_expr;
20546 /* Deduce the value of template parameters. TPARMS is the (innermost)
20547 set of template parameters to a template. TARGS is the bindings
20548 for those template parameters, as determined thus far; TARGS may
20549 include template arguments for outer levels of template parameters
20550 as well. PARM is a parameter to a template function, or a
20551 subcomponent of that parameter; ARG is the corresponding argument.
20552 This function attempts to match PARM with ARG in a manner
20553 consistent with the existing assignments in TARGS. If more values
20554 are deduced, then TARGS is updated.
20556 Returns 0 if the type deduction succeeds, 1 otherwise. The
20557 parameter STRICT is a bitwise or of the following flags:
20559 UNIFY_ALLOW_NONE:
20560 Require an exact match between PARM and ARG.
20561 UNIFY_ALLOW_MORE_CV_QUAL:
20562 Allow the deduced ARG to be more cv-qualified (by qualification
20563 conversion) than ARG.
20564 UNIFY_ALLOW_LESS_CV_QUAL:
20565 Allow the deduced ARG to be less cv-qualified than ARG.
20566 UNIFY_ALLOW_DERIVED:
20567 Allow the deduced ARG to be a template base class of ARG,
20568 or a pointer to a template base class of the type pointed to by
20569 ARG.
20570 UNIFY_ALLOW_INTEGER:
20571 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20572 case for more information.
20573 UNIFY_ALLOW_OUTER_LEVEL:
20574 This is the outermost level of a deduction. Used to determine validity
20575 of qualification conversions. A valid qualification conversion must
20576 have const qualified pointers leading up to the inner type which
20577 requires additional CV quals, except at the outer level, where const
20578 is not required [conv.qual]. It would be normal to set this flag in
20579 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20580 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20581 This is the outermost level of a deduction, and PARM can be more CV
20582 qualified at this point.
20583 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20584 This is the outermost level of a deduction, and PARM can be less CV
20585 qualified at this point. */
20587 static int
20588 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20589 bool explain_p)
20591 int idx;
20592 tree targ;
20593 tree tparm;
20594 int strict_in = strict;
20595 tsubst_flags_t complain = (explain_p
20596 ? tf_warning_or_error
20597 : tf_none);
20599 /* I don't think this will do the right thing with respect to types.
20600 But the only case I've seen it in so far has been array bounds, where
20601 signedness is the only information lost, and I think that will be
20602 okay. */
20603 while (CONVERT_EXPR_P (parm))
20604 parm = TREE_OPERAND (parm, 0);
20606 if (arg == error_mark_node)
20607 return unify_invalid (explain_p);
20608 if (arg == unknown_type_node
20609 || arg == init_list_type_node)
20610 /* We can't deduce anything from this, but we might get all the
20611 template args from other function args. */
20612 return unify_success (explain_p);
20614 if (parm == any_targ_node || arg == any_targ_node)
20615 return unify_success (explain_p);
20617 /* If PARM uses template parameters, then we can't bail out here,
20618 even if ARG == PARM, since we won't record unifications for the
20619 template parameters. We might need them if we're trying to
20620 figure out which of two things is more specialized. */
20621 if (arg == parm && !uses_template_parms (parm))
20622 return unify_success (explain_p);
20624 /* Handle init lists early, so the rest of the function can assume
20625 we're dealing with a type. */
20626 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20628 tree elt, elttype;
20629 unsigned i;
20630 tree orig_parm = parm;
20632 /* Replace T with std::initializer_list<T> for deduction. */
20633 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20634 && flag_deduce_init_list)
20635 parm = listify (parm);
20637 if (!is_std_init_list (parm)
20638 && TREE_CODE (parm) != ARRAY_TYPE)
20639 /* We can only deduce from an initializer list argument if the
20640 parameter is std::initializer_list or an array; otherwise this
20641 is a non-deduced context. */
20642 return unify_success (explain_p);
20644 if (TREE_CODE (parm) == ARRAY_TYPE)
20645 elttype = TREE_TYPE (parm);
20646 else
20648 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20649 /* Deduction is defined in terms of a single type, so just punt
20650 on the (bizarre) std::initializer_list<T...>. */
20651 if (PACK_EXPANSION_P (elttype))
20652 return unify_success (explain_p);
20655 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20657 int elt_strict = strict;
20659 if (elt == error_mark_node)
20660 return unify_invalid (explain_p);
20662 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20664 tree type = TREE_TYPE (elt);
20665 if (type == error_mark_node)
20666 return unify_invalid (explain_p);
20667 /* It should only be possible to get here for a call. */
20668 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20669 elt_strict |= maybe_adjust_types_for_deduction
20670 (DEDUCE_CALL, &elttype, &type, elt);
20671 elt = type;
20674 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20675 explain_p);
20678 if (TREE_CODE (parm) == ARRAY_TYPE
20679 && deducible_array_bound (TYPE_DOMAIN (parm)))
20681 /* Also deduce from the length of the initializer list. */
20682 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20683 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20684 if (idx == error_mark_node)
20685 return unify_invalid (explain_p);
20686 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20687 idx, explain_p);
20690 /* If the std::initializer_list<T> deduction worked, replace the
20691 deduced A with std::initializer_list<A>. */
20692 if (orig_parm != parm)
20694 idx = TEMPLATE_TYPE_IDX (orig_parm);
20695 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20696 targ = listify (targ);
20697 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20699 return unify_success (explain_p);
20702 /* If parm and arg aren't the same kind of thing (template, type, or
20703 expression), fail early. */
20704 if (pa_kind (parm) != pa_kind (arg))
20705 return unify_invalid (explain_p);
20707 /* Immediately reject some pairs that won't unify because of
20708 cv-qualification mismatches. */
20709 if (TREE_CODE (arg) == TREE_CODE (parm)
20710 && TYPE_P (arg)
20711 /* It is the elements of the array which hold the cv quals of an array
20712 type, and the elements might be template type parms. We'll check
20713 when we recurse. */
20714 && TREE_CODE (arg) != ARRAY_TYPE
20715 /* We check the cv-qualifiers when unifying with template type
20716 parameters below. We want to allow ARG `const T' to unify with
20717 PARM `T' for example, when computing which of two templates
20718 is more specialized, for example. */
20719 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20720 && !check_cv_quals_for_unify (strict_in, arg, parm))
20721 return unify_cv_qual_mismatch (explain_p, parm, arg);
20723 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20724 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20725 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20726 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20727 strict &= ~UNIFY_ALLOW_DERIVED;
20728 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20729 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20731 switch (TREE_CODE (parm))
20733 case TYPENAME_TYPE:
20734 case SCOPE_REF:
20735 case UNBOUND_CLASS_TEMPLATE:
20736 /* In a type which contains a nested-name-specifier, template
20737 argument values cannot be deduced for template parameters used
20738 within the nested-name-specifier. */
20739 return unify_success (explain_p);
20741 case TEMPLATE_TYPE_PARM:
20742 case TEMPLATE_TEMPLATE_PARM:
20743 case BOUND_TEMPLATE_TEMPLATE_PARM:
20744 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20745 if (error_operand_p (tparm))
20746 return unify_invalid (explain_p);
20748 if (TEMPLATE_TYPE_LEVEL (parm)
20749 != template_decl_level (tparm))
20750 /* The PARM is not one we're trying to unify. Just check
20751 to see if it matches ARG. */
20753 if (TREE_CODE (arg) == TREE_CODE (parm)
20754 && (is_auto (parm) ? is_auto (arg)
20755 : same_type_p (parm, arg)))
20756 return unify_success (explain_p);
20757 else
20758 return unify_type_mismatch (explain_p, parm, arg);
20760 idx = TEMPLATE_TYPE_IDX (parm);
20761 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20762 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20763 if (error_operand_p (tparm))
20764 return unify_invalid (explain_p);
20766 /* Check for mixed types and values. */
20767 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20768 && TREE_CODE (tparm) != TYPE_DECL)
20769 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20770 && TREE_CODE (tparm) != TEMPLATE_DECL))
20771 gcc_unreachable ();
20773 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20775 if ((strict_in & UNIFY_ALLOW_DERIVED)
20776 && CLASS_TYPE_P (arg))
20778 /* First try to match ARG directly. */
20779 tree t = try_class_unification (tparms, targs, parm, arg,
20780 explain_p);
20781 if (!t)
20783 /* Otherwise, look for a suitable base of ARG, as below. */
20784 enum template_base_result r;
20785 r = get_template_base (tparms, targs, parm, arg,
20786 explain_p, &t);
20787 if (!t)
20788 return unify_no_common_base (explain_p, r, parm, arg);
20789 arg = t;
20792 /* ARG must be constructed from a template class or a template
20793 template parameter. */
20794 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20795 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20796 return unify_template_deduction_failure (explain_p, parm, arg);
20798 /* Deduce arguments T, i from TT<T> or TT<i>. */
20799 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20800 return 1;
20802 arg = TYPE_TI_TEMPLATE (arg);
20804 /* Fall through to deduce template name. */
20807 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20808 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20810 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20812 /* Simple cases: Value already set, does match or doesn't. */
20813 if (targ != NULL_TREE && template_args_equal (targ, arg))
20814 return unify_success (explain_p);
20815 else if (targ)
20816 return unify_inconsistency (explain_p, parm, targ, arg);
20818 else
20820 /* If PARM is `const T' and ARG is only `int', we don't have
20821 a match unless we are allowing additional qualification.
20822 If ARG is `const int' and PARM is just `T' that's OK;
20823 that binds `const int' to `T'. */
20824 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20825 arg, parm))
20826 return unify_cv_qual_mismatch (explain_p, parm, arg);
20828 /* Consider the case where ARG is `const volatile int' and
20829 PARM is `const T'. Then, T should be `volatile int'. */
20830 arg = cp_build_qualified_type_real
20831 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20832 if (arg == error_mark_node)
20833 return unify_invalid (explain_p);
20835 /* Simple cases: Value already set, does match or doesn't. */
20836 if (targ != NULL_TREE && same_type_p (targ, arg))
20837 return unify_success (explain_p);
20838 else if (targ)
20839 return unify_inconsistency (explain_p, parm, targ, arg);
20841 /* Make sure that ARG is not a variable-sized array. (Note
20842 that were talking about variable-sized arrays (like
20843 `int[n]'), rather than arrays of unknown size (like
20844 `int[]').) We'll get very confused by such a type since
20845 the bound of the array is not constant, and therefore
20846 not mangleable. Besides, such types are not allowed in
20847 ISO C++, so we can do as we please here. We do allow
20848 them for 'auto' deduction, since that isn't ABI-exposed. */
20849 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20850 return unify_vla_arg (explain_p, arg);
20852 /* Strip typedefs as in convert_template_argument. */
20853 arg = canonicalize_type_argument (arg, tf_none);
20856 /* If ARG is a parameter pack or an expansion, we cannot unify
20857 against it unless PARM is also a parameter pack. */
20858 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20859 && !template_parameter_pack_p (parm))
20860 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20862 /* If the argument deduction results is a METHOD_TYPE,
20863 then there is a problem.
20864 METHOD_TYPE doesn't map to any real C++ type the result of
20865 the deduction can not be of that type. */
20866 if (TREE_CODE (arg) == METHOD_TYPE)
20867 return unify_method_type_error (explain_p, arg);
20869 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20870 return unify_success (explain_p);
20872 case TEMPLATE_PARM_INDEX:
20873 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20874 if (error_operand_p (tparm))
20875 return unify_invalid (explain_p);
20877 if (TEMPLATE_PARM_LEVEL (parm)
20878 != template_decl_level (tparm))
20880 /* The PARM is not one we're trying to unify. Just check
20881 to see if it matches ARG. */
20882 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20883 && cp_tree_equal (parm, arg));
20884 if (result)
20885 unify_expression_unequal (explain_p, parm, arg);
20886 return result;
20889 idx = TEMPLATE_PARM_IDX (parm);
20890 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20892 if (targ)
20894 int x = !cp_tree_equal (targ, arg);
20895 if (x)
20896 unify_inconsistency (explain_p, parm, targ, arg);
20897 return x;
20900 /* [temp.deduct.type] If, in the declaration of a function template
20901 with a non-type template-parameter, the non-type
20902 template-parameter is used in an expression in the function
20903 parameter-list and, if the corresponding template-argument is
20904 deduced, the template-argument type shall match the type of the
20905 template-parameter exactly, except that a template-argument
20906 deduced from an array bound may be of any integral type.
20907 The non-type parameter might use already deduced type parameters. */
20908 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20909 if (tree a = type_uses_auto (tparm))
20911 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20912 if (tparm == error_mark_node)
20913 return 1;
20916 if (!TREE_TYPE (arg))
20917 /* Template-parameter dependent expression. Just accept it for now.
20918 It will later be processed in convert_template_argument. */
20920 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20921 non_reference (tparm)))
20922 /* OK */;
20923 else if ((strict & UNIFY_ALLOW_INTEGER)
20924 && CP_INTEGRAL_TYPE_P (tparm))
20925 /* Convert the ARG to the type of PARM; the deduced non-type
20926 template argument must exactly match the types of the
20927 corresponding parameter. */
20928 arg = fold (build_nop (tparm, arg));
20929 else if (uses_template_parms (tparm))
20931 /* We haven't deduced the type of this parameter yet. */
20932 if (cxx_dialect >= cxx1z
20933 /* We deduce from array bounds in try_array_deduction. */
20934 && !(strict & UNIFY_ALLOW_INTEGER))
20936 /* Deduce it from the non-type argument. */
20937 tree atype = TREE_TYPE (arg);
20938 RECUR_AND_CHECK_FAILURE (tparms, targs,
20939 tparm, atype,
20940 UNIFY_ALLOW_NONE, explain_p);
20942 else
20943 /* Try again later. */
20944 return unify_success (explain_p);
20946 else
20947 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20949 /* If ARG is a parameter pack or an expansion, we cannot unify
20950 against it unless PARM is also a parameter pack. */
20951 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20952 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20953 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20956 bool removed_attr = false;
20957 arg = strip_typedefs_expr (arg, &removed_attr);
20959 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20960 return unify_success (explain_p);
20962 case PTRMEM_CST:
20964 /* A pointer-to-member constant can be unified only with
20965 another constant. */
20966 if (TREE_CODE (arg) != PTRMEM_CST)
20967 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20969 /* Just unify the class member. It would be useless (and possibly
20970 wrong, depending on the strict flags) to unify also
20971 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20972 arg refer to the same variable, even if through different
20973 classes. For instance:
20975 struct A { int x; };
20976 struct B : A { };
20978 Unification of &A::x and &B::x must succeed. */
20979 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20980 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20983 case POINTER_TYPE:
20985 if (!TYPE_PTR_P (arg))
20986 return unify_type_mismatch (explain_p, parm, arg);
20988 /* [temp.deduct.call]
20990 A can be another pointer or pointer to member type that can
20991 be converted to the deduced A via a qualification
20992 conversion (_conv.qual_).
20994 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20995 This will allow for additional cv-qualification of the
20996 pointed-to types if appropriate. */
20998 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20999 /* The derived-to-base conversion only persists through one
21000 level of pointers. */
21001 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21003 return unify (tparms, targs, TREE_TYPE (parm),
21004 TREE_TYPE (arg), strict, explain_p);
21007 case REFERENCE_TYPE:
21008 if (TREE_CODE (arg) != REFERENCE_TYPE)
21009 return unify_type_mismatch (explain_p, parm, arg);
21010 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21011 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21013 case ARRAY_TYPE:
21014 if (TREE_CODE (arg) != ARRAY_TYPE)
21015 return unify_type_mismatch (explain_p, parm, arg);
21016 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21017 != (TYPE_DOMAIN (arg) == NULL_TREE))
21018 return unify_type_mismatch (explain_p, parm, arg);
21019 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21020 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21021 if (TYPE_DOMAIN (parm) != NULL_TREE)
21022 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21023 TYPE_DOMAIN (arg), explain_p);
21024 return unify_success (explain_p);
21026 case REAL_TYPE:
21027 case COMPLEX_TYPE:
21028 case VECTOR_TYPE:
21029 case INTEGER_TYPE:
21030 case BOOLEAN_TYPE:
21031 case ENUMERAL_TYPE:
21032 case VOID_TYPE:
21033 case NULLPTR_TYPE:
21034 if (TREE_CODE (arg) != TREE_CODE (parm))
21035 return unify_type_mismatch (explain_p, parm, arg);
21037 /* We have already checked cv-qualification at the top of the
21038 function. */
21039 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21040 return unify_type_mismatch (explain_p, parm, arg);
21042 /* As far as unification is concerned, this wins. Later checks
21043 will invalidate it if necessary. */
21044 return unify_success (explain_p);
21046 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21047 /* Type INTEGER_CST can come from ordinary constant template args. */
21048 case INTEGER_CST:
21049 while (CONVERT_EXPR_P (arg))
21050 arg = TREE_OPERAND (arg, 0);
21052 if (TREE_CODE (arg) != INTEGER_CST)
21053 return unify_template_argument_mismatch (explain_p, parm, arg);
21054 return (tree_int_cst_equal (parm, arg)
21055 ? unify_success (explain_p)
21056 : unify_template_argument_mismatch (explain_p, parm, arg));
21058 case TREE_VEC:
21060 int i, len, argslen;
21061 int parm_variadic_p = 0;
21063 if (TREE_CODE (arg) != TREE_VEC)
21064 return unify_template_argument_mismatch (explain_p, parm, arg);
21066 len = TREE_VEC_LENGTH (parm);
21067 argslen = TREE_VEC_LENGTH (arg);
21069 /* Check for pack expansions in the parameters. */
21070 for (i = 0; i < len; ++i)
21072 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21074 if (i == len - 1)
21075 /* We can unify against something with a trailing
21076 parameter pack. */
21077 parm_variadic_p = 1;
21078 else
21079 /* [temp.deduct.type]/9: If the template argument list of
21080 P contains a pack expansion that is not the last
21081 template argument, the entire template argument list
21082 is a non-deduced context. */
21083 return unify_success (explain_p);
21087 /* If we don't have enough arguments to satisfy the parameters
21088 (not counting the pack expression at the end), or we have
21089 too many arguments for a parameter list that doesn't end in
21090 a pack expression, we can't unify. */
21091 if (parm_variadic_p
21092 ? argslen < len - parm_variadic_p
21093 : argslen != len)
21094 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21096 /* Unify all of the parameters that precede the (optional)
21097 pack expression. */
21098 for (i = 0; i < len - parm_variadic_p; ++i)
21100 RECUR_AND_CHECK_FAILURE (tparms, targs,
21101 TREE_VEC_ELT (parm, i),
21102 TREE_VEC_ELT (arg, i),
21103 UNIFY_ALLOW_NONE, explain_p);
21105 if (parm_variadic_p)
21106 return unify_pack_expansion (tparms, targs, parm, arg,
21107 DEDUCE_EXACT,
21108 /*subr=*/true, explain_p);
21109 return unify_success (explain_p);
21112 case RECORD_TYPE:
21113 case UNION_TYPE:
21114 if (TREE_CODE (arg) != TREE_CODE (parm))
21115 return unify_type_mismatch (explain_p, parm, arg);
21117 if (TYPE_PTRMEMFUNC_P (parm))
21119 if (!TYPE_PTRMEMFUNC_P (arg))
21120 return unify_type_mismatch (explain_p, parm, arg);
21122 return unify (tparms, targs,
21123 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21124 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21125 strict, explain_p);
21127 else if (TYPE_PTRMEMFUNC_P (arg))
21128 return unify_type_mismatch (explain_p, parm, arg);
21130 if (CLASSTYPE_TEMPLATE_INFO (parm))
21132 tree t = NULL_TREE;
21134 if (strict_in & UNIFY_ALLOW_DERIVED)
21136 /* First, we try to unify the PARM and ARG directly. */
21137 t = try_class_unification (tparms, targs,
21138 parm, arg, explain_p);
21140 if (!t)
21142 /* Fallback to the special case allowed in
21143 [temp.deduct.call]:
21145 If P is a class, and P has the form
21146 template-id, then A can be a derived class of
21147 the deduced A. Likewise, if P is a pointer to
21148 a class of the form template-id, A can be a
21149 pointer to a derived class pointed to by the
21150 deduced A. */
21151 enum template_base_result r;
21152 r = get_template_base (tparms, targs, parm, arg,
21153 explain_p, &t);
21155 if (!t)
21157 /* Don't give the derived diagnostic if we're
21158 already dealing with the same template. */
21159 bool same_template
21160 = (CLASSTYPE_TEMPLATE_INFO (arg)
21161 && (CLASSTYPE_TI_TEMPLATE (parm)
21162 == CLASSTYPE_TI_TEMPLATE (arg)));
21163 return unify_no_common_base (explain_p && !same_template,
21164 r, parm, arg);
21168 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21169 && (CLASSTYPE_TI_TEMPLATE (parm)
21170 == CLASSTYPE_TI_TEMPLATE (arg)))
21171 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21172 Then, we should unify `int' and `U'. */
21173 t = arg;
21174 else
21175 /* There's no chance of unification succeeding. */
21176 return unify_type_mismatch (explain_p, parm, arg);
21178 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21179 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21181 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21182 return unify_type_mismatch (explain_p, parm, arg);
21183 return unify_success (explain_p);
21185 case METHOD_TYPE:
21186 case FUNCTION_TYPE:
21188 unsigned int nargs;
21189 tree *args;
21190 tree a;
21191 unsigned int i;
21193 if (TREE_CODE (arg) != TREE_CODE (parm))
21194 return unify_type_mismatch (explain_p, parm, arg);
21196 /* CV qualifications for methods can never be deduced, they must
21197 match exactly. We need to check them explicitly here,
21198 because type_unification_real treats them as any other
21199 cv-qualified parameter. */
21200 if (TREE_CODE (parm) == METHOD_TYPE
21201 && (!check_cv_quals_for_unify
21202 (UNIFY_ALLOW_NONE,
21203 class_of_this_parm (arg),
21204 class_of_this_parm (parm))))
21205 return unify_cv_qual_mismatch (explain_p, parm, arg);
21206 if (TREE_CODE (arg) == FUNCTION_TYPE
21207 && type_memfn_quals (parm) != type_memfn_quals (arg))
21208 return unify_cv_qual_mismatch (explain_p, parm, arg);
21209 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21210 return unify_type_mismatch (explain_p, parm, arg);
21212 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21213 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21215 nargs = list_length (TYPE_ARG_TYPES (arg));
21216 args = XALLOCAVEC (tree, nargs);
21217 for (a = TYPE_ARG_TYPES (arg), i = 0;
21218 a != NULL_TREE && a != void_list_node;
21219 a = TREE_CHAIN (a), ++i)
21220 args[i] = TREE_VALUE (a);
21221 nargs = i;
21223 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21224 args, nargs, 1, DEDUCE_EXACT,
21225 LOOKUP_NORMAL, NULL, explain_p))
21226 return 1;
21228 if (flag_noexcept_type)
21230 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21231 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21232 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21233 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21234 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21235 && uses_template_parms (TREE_PURPOSE (pspec)))
21236 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21237 TREE_PURPOSE (aspec),
21238 UNIFY_ALLOW_NONE, explain_p);
21239 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21240 return unify_type_mismatch (explain_p, parm, arg);
21243 return 0;
21246 case OFFSET_TYPE:
21247 /* Unify a pointer to member with a pointer to member function, which
21248 deduces the type of the member as a function type. */
21249 if (TYPE_PTRMEMFUNC_P (arg))
21251 /* Check top-level cv qualifiers */
21252 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21253 return unify_cv_qual_mismatch (explain_p, parm, arg);
21255 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21256 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21257 UNIFY_ALLOW_NONE, explain_p);
21259 /* Determine the type of the function we are unifying against. */
21260 tree fntype = static_fn_type (arg);
21262 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21265 if (TREE_CODE (arg) != OFFSET_TYPE)
21266 return unify_type_mismatch (explain_p, parm, arg);
21267 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21268 TYPE_OFFSET_BASETYPE (arg),
21269 UNIFY_ALLOW_NONE, explain_p);
21270 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21271 strict, explain_p);
21273 case CONST_DECL:
21274 if (DECL_TEMPLATE_PARM_P (parm))
21275 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21276 if (arg != scalar_constant_value (parm))
21277 return unify_template_argument_mismatch (explain_p, parm, arg);
21278 return unify_success (explain_p);
21280 case FIELD_DECL:
21281 case TEMPLATE_DECL:
21282 /* Matched cases are handled by the ARG == PARM test above. */
21283 return unify_template_argument_mismatch (explain_p, parm, arg);
21285 case VAR_DECL:
21286 /* We might get a variable as a non-type template argument in parm if the
21287 corresponding parameter is type-dependent. Make any necessary
21288 adjustments based on whether arg is a reference. */
21289 if (CONSTANT_CLASS_P (arg))
21290 parm = fold_non_dependent_expr (parm);
21291 else if (REFERENCE_REF_P (arg))
21293 tree sub = TREE_OPERAND (arg, 0);
21294 STRIP_NOPS (sub);
21295 if (TREE_CODE (sub) == ADDR_EXPR)
21296 arg = TREE_OPERAND (sub, 0);
21298 /* Now use the normal expression code to check whether they match. */
21299 goto expr;
21301 case TYPE_ARGUMENT_PACK:
21302 case NONTYPE_ARGUMENT_PACK:
21303 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21304 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21306 case TYPEOF_TYPE:
21307 case DECLTYPE_TYPE:
21308 case UNDERLYING_TYPE:
21309 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21310 or UNDERLYING_TYPE nodes. */
21311 return unify_success (explain_p);
21313 case ERROR_MARK:
21314 /* Unification fails if we hit an error node. */
21315 return unify_invalid (explain_p);
21317 case INDIRECT_REF:
21318 if (REFERENCE_REF_P (parm))
21320 bool pexp = PACK_EXPANSION_P (arg);
21321 if (pexp)
21322 arg = PACK_EXPANSION_PATTERN (arg);
21323 if (REFERENCE_REF_P (arg))
21324 arg = TREE_OPERAND (arg, 0);
21325 if (pexp)
21326 arg = make_pack_expansion (arg);
21327 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21328 strict, explain_p);
21330 /* FALLTHRU */
21332 default:
21333 /* An unresolved overload is a nondeduced context. */
21334 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21335 return unify_success (explain_p);
21336 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21337 expr:
21338 /* We must be looking at an expression. This can happen with
21339 something like:
21341 template <int I>
21342 void foo(S<I>, S<I + 2>);
21344 This is a "nondeduced context":
21346 [deduct.type]
21348 The nondeduced contexts are:
21350 --A type that is a template-id in which one or more of
21351 the template-arguments is an expression that references
21352 a template-parameter.
21354 In these cases, we assume deduction succeeded, but don't
21355 actually infer any unifications. */
21357 if (!uses_template_parms (parm)
21358 && !template_args_equal (parm, arg))
21359 return unify_expression_unequal (explain_p, parm, arg);
21360 else
21361 return unify_success (explain_p);
21364 #undef RECUR_AND_CHECK_FAILURE
21366 /* Note that DECL can be defined in this translation unit, if
21367 required. */
21369 static void
21370 mark_definable (tree decl)
21372 tree clone;
21373 DECL_NOT_REALLY_EXTERN (decl) = 1;
21374 FOR_EACH_CLONE (clone, decl)
21375 DECL_NOT_REALLY_EXTERN (clone) = 1;
21378 /* Called if RESULT is explicitly instantiated, or is a member of an
21379 explicitly instantiated class. */
21381 void
21382 mark_decl_instantiated (tree result, int extern_p)
21384 SET_DECL_EXPLICIT_INSTANTIATION (result);
21386 /* If this entity has already been written out, it's too late to
21387 make any modifications. */
21388 if (TREE_ASM_WRITTEN (result))
21389 return;
21391 /* For anonymous namespace we don't need to do anything. */
21392 if (decl_anon_ns_mem_p (result))
21394 gcc_assert (!TREE_PUBLIC (result));
21395 return;
21398 if (TREE_CODE (result) != FUNCTION_DECL)
21399 /* The TREE_PUBLIC flag for function declarations will have been
21400 set correctly by tsubst. */
21401 TREE_PUBLIC (result) = 1;
21403 /* This might have been set by an earlier implicit instantiation. */
21404 DECL_COMDAT (result) = 0;
21406 if (extern_p)
21407 DECL_NOT_REALLY_EXTERN (result) = 0;
21408 else
21410 mark_definable (result);
21411 mark_needed (result);
21412 /* Always make artificials weak. */
21413 if (DECL_ARTIFICIAL (result) && flag_weak)
21414 comdat_linkage (result);
21415 /* For WIN32 we also want to put explicit instantiations in
21416 linkonce sections. */
21417 else if (TREE_PUBLIC (result))
21418 maybe_make_one_only (result);
21421 /* If EXTERN_P, then this function will not be emitted -- unless
21422 followed by an explicit instantiation, at which point its linkage
21423 will be adjusted. If !EXTERN_P, then this function will be
21424 emitted here. In neither circumstance do we want
21425 import_export_decl to adjust the linkage. */
21426 DECL_INTERFACE_KNOWN (result) = 1;
21429 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21430 important template arguments. If any are missing, we check whether
21431 they're important by using error_mark_node for substituting into any
21432 args that were used for partial ordering (the ones between ARGS and END)
21433 and seeing if it bubbles up. */
21435 static bool
21436 check_undeduced_parms (tree targs, tree args, tree end)
21438 bool found = false;
21439 int i;
21440 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21441 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21443 found = true;
21444 TREE_VEC_ELT (targs, i) = error_mark_node;
21446 if (found)
21448 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21449 if (substed == error_mark_node)
21450 return true;
21452 return false;
21455 /* Given two function templates PAT1 and PAT2, return:
21457 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21458 -1 if PAT2 is more specialized than PAT1.
21459 0 if neither is more specialized.
21461 LEN indicates the number of parameters we should consider
21462 (defaulted parameters should not be considered).
21464 The 1998 std underspecified function template partial ordering, and
21465 DR214 addresses the issue. We take pairs of arguments, one from
21466 each of the templates, and deduce them against each other. One of
21467 the templates will be more specialized if all the *other*
21468 template's arguments deduce against its arguments and at least one
21469 of its arguments *does* *not* deduce against the other template's
21470 corresponding argument. Deduction is done as for class templates.
21471 The arguments used in deduction have reference and top level cv
21472 qualifiers removed. Iff both arguments were originally reference
21473 types *and* deduction succeeds in both directions, an lvalue reference
21474 wins against an rvalue reference and otherwise the template
21475 with the more cv-qualified argument wins for that pairing (if
21476 neither is more cv-qualified, they both are equal). Unlike regular
21477 deduction, after all the arguments have been deduced in this way,
21478 we do *not* verify the deduced template argument values can be
21479 substituted into non-deduced contexts.
21481 The logic can be a bit confusing here, because we look at deduce1 and
21482 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21483 can find template arguments for pat1 to make arg1 look like arg2, that
21484 means that arg2 is at least as specialized as arg1. */
21487 more_specialized_fn (tree pat1, tree pat2, int len)
21489 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21490 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21491 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21492 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21493 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21494 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21495 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21496 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21497 tree origs1, origs2;
21498 bool lose1 = false;
21499 bool lose2 = false;
21501 /* Remove the this parameter from non-static member functions. If
21502 one is a non-static member function and the other is not a static
21503 member function, remove the first parameter from that function
21504 also. This situation occurs for operator functions where we
21505 locate both a member function (with this pointer) and non-member
21506 operator (with explicit first operand). */
21507 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21509 len--; /* LEN is the number of significant arguments for DECL1 */
21510 args1 = TREE_CHAIN (args1);
21511 if (!DECL_STATIC_FUNCTION_P (decl2))
21512 args2 = TREE_CHAIN (args2);
21514 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21516 args2 = TREE_CHAIN (args2);
21517 if (!DECL_STATIC_FUNCTION_P (decl1))
21519 len--;
21520 args1 = TREE_CHAIN (args1);
21524 /* If only one is a conversion operator, they are unordered. */
21525 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21526 return 0;
21528 /* Consider the return type for a conversion function */
21529 if (DECL_CONV_FN_P (decl1))
21531 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21532 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21533 len++;
21536 processing_template_decl++;
21538 origs1 = args1;
21539 origs2 = args2;
21541 while (len--
21542 /* Stop when an ellipsis is seen. */
21543 && args1 != NULL_TREE && args2 != NULL_TREE)
21545 tree arg1 = TREE_VALUE (args1);
21546 tree arg2 = TREE_VALUE (args2);
21547 int deduce1, deduce2;
21548 int quals1 = -1;
21549 int quals2 = -1;
21550 int ref1 = 0;
21551 int ref2 = 0;
21553 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21554 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21556 /* When both arguments are pack expansions, we need only
21557 unify the patterns themselves. */
21558 arg1 = PACK_EXPANSION_PATTERN (arg1);
21559 arg2 = PACK_EXPANSION_PATTERN (arg2);
21561 /* This is the last comparison we need to do. */
21562 len = 0;
21565 /* DR 1847: If a particular P contains no template-parameters that
21566 participate in template argument deduction, that P is not used to
21567 determine the ordering. */
21568 if (!uses_deducible_template_parms (arg1)
21569 && !uses_deducible_template_parms (arg2))
21570 goto next;
21572 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21574 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21575 arg1 = TREE_TYPE (arg1);
21576 quals1 = cp_type_quals (arg1);
21579 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21581 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21582 arg2 = TREE_TYPE (arg2);
21583 quals2 = cp_type_quals (arg2);
21586 arg1 = TYPE_MAIN_VARIANT (arg1);
21587 arg2 = TYPE_MAIN_VARIANT (arg2);
21589 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21591 int i, len2 = remaining_arguments (args2);
21592 tree parmvec = make_tree_vec (1);
21593 tree argvec = make_tree_vec (len2);
21594 tree ta = args2;
21596 /* Setup the parameter vector, which contains only ARG1. */
21597 TREE_VEC_ELT (parmvec, 0) = arg1;
21599 /* Setup the argument vector, which contains the remaining
21600 arguments. */
21601 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21602 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21604 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21605 argvec, DEDUCE_EXACT,
21606 /*subr=*/true, /*explain_p=*/false)
21607 == 0);
21609 /* We cannot deduce in the other direction, because ARG1 is
21610 a pack expansion but ARG2 is not. */
21611 deduce2 = 0;
21613 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21615 int i, len1 = remaining_arguments (args1);
21616 tree parmvec = make_tree_vec (1);
21617 tree argvec = make_tree_vec (len1);
21618 tree ta = args1;
21620 /* Setup the parameter vector, which contains only ARG1. */
21621 TREE_VEC_ELT (parmvec, 0) = arg2;
21623 /* Setup the argument vector, which contains the remaining
21624 arguments. */
21625 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21626 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21628 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21629 argvec, DEDUCE_EXACT,
21630 /*subr=*/true, /*explain_p=*/false)
21631 == 0);
21633 /* We cannot deduce in the other direction, because ARG2 is
21634 a pack expansion but ARG1 is not.*/
21635 deduce1 = 0;
21638 else
21640 /* The normal case, where neither argument is a pack
21641 expansion. */
21642 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21643 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21644 == 0);
21645 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21646 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21647 == 0);
21650 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21651 arg2, then arg2 is not as specialized as arg1. */
21652 if (!deduce1)
21653 lose2 = true;
21654 if (!deduce2)
21655 lose1 = true;
21657 /* "If, for a given type, deduction succeeds in both directions
21658 (i.e., the types are identical after the transformations above)
21659 and both P and A were reference types (before being replaced with
21660 the type referred to above):
21661 - if the type from the argument template was an lvalue reference and
21662 the type from the parameter template was not, the argument type is
21663 considered to be more specialized than the other; otherwise,
21664 - if the type from the argument template is more cv-qualified
21665 than the type from the parameter template (as described above),
21666 the argument type is considered to be more specialized than the other;
21667 otherwise,
21668 - neither type is more specialized than the other." */
21670 if (deduce1 && deduce2)
21672 if (ref1 && ref2 && ref1 != ref2)
21674 if (ref1 > ref2)
21675 lose1 = true;
21676 else
21677 lose2 = true;
21679 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21681 if ((quals1 & quals2) == quals2)
21682 lose2 = true;
21683 if ((quals1 & quals2) == quals1)
21684 lose1 = true;
21688 if (lose1 && lose2)
21689 /* We've failed to deduce something in either direction.
21690 These must be unordered. */
21691 break;
21693 next:
21695 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21696 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21697 /* We have already processed all of the arguments in our
21698 handing of the pack expansion type. */
21699 len = 0;
21701 args1 = TREE_CHAIN (args1);
21702 args2 = TREE_CHAIN (args2);
21705 /* "In most cases, all template parameters must have values in order for
21706 deduction to succeed, but for partial ordering purposes a template
21707 parameter may remain without a value provided it is not used in the
21708 types being used for partial ordering."
21710 Thus, if we are missing any of the targs1 we need to substitute into
21711 origs1, then pat2 is not as specialized as pat1. This can happen when
21712 there is a nondeduced context. */
21713 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21714 lose2 = true;
21715 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21716 lose1 = true;
21718 processing_template_decl--;
21720 /* If both deductions succeed, the partial ordering selects the more
21721 constrained template. */
21722 if (!lose1 && !lose2)
21724 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21725 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21726 lose1 = !subsumes_constraints (c1, c2);
21727 lose2 = !subsumes_constraints (c2, c1);
21730 /* All things being equal, if the next argument is a pack expansion
21731 for one function but not for the other, prefer the
21732 non-variadic function. FIXME this is bogus; see c++/41958. */
21733 if (lose1 == lose2
21734 && args1 && TREE_VALUE (args1)
21735 && args2 && TREE_VALUE (args2))
21737 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21738 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21741 if (lose1 == lose2)
21742 return 0;
21743 else if (!lose1)
21744 return 1;
21745 else
21746 return -1;
21749 /* Determine which of two partial specializations of TMPL is more
21750 specialized.
21752 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21753 to the first partial specialization. The TREE_PURPOSE is the
21754 innermost set of template parameters for the partial
21755 specialization. PAT2 is similar, but for the second template.
21757 Return 1 if the first partial specialization is more specialized;
21758 -1 if the second is more specialized; 0 if neither is more
21759 specialized.
21761 See [temp.class.order] for information about determining which of
21762 two templates is more specialized. */
21764 static int
21765 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21767 tree targs;
21768 int winner = 0;
21769 bool any_deductions = false;
21771 tree tmpl1 = TREE_VALUE (pat1);
21772 tree tmpl2 = TREE_VALUE (pat2);
21773 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21774 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21776 /* Just like what happens for functions, if we are ordering between
21777 different template specializations, we may encounter dependent
21778 types in the arguments, and we need our dependency check functions
21779 to behave correctly. */
21780 ++processing_template_decl;
21781 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21782 if (targs)
21784 --winner;
21785 any_deductions = true;
21788 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21789 if (targs)
21791 ++winner;
21792 any_deductions = true;
21794 --processing_template_decl;
21796 /* If both deductions succeed, the partial ordering selects the more
21797 constrained template. */
21798 if (!winner && any_deductions)
21799 return more_constrained (tmpl1, tmpl2);
21801 /* In the case of a tie where at least one of the templates
21802 has a parameter pack at the end, the template with the most
21803 non-packed parameters wins. */
21804 if (winner == 0
21805 && any_deductions
21806 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21807 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21809 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21810 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21811 int len1 = TREE_VEC_LENGTH (args1);
21812 int len2 = TREE_VEC_LENGTH (args2);
21814 /* We don't count the pack expansion at the end. */
21815 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21816 --len1;
21817 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21818 --len2;
21820 if (len1 > len2)
21821 return 1;
21822 else if (len1 < len2)
21823 return -1;
21826 return winner;
21829 /* Return the template arguments that will produce the function signature
21830 DECL from the function template FN, with the explicit template
21831 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21832 also match. Return NULL_TREE if no satisfactory arguments could be
21833 found. */
21835 static tree
21836 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21838 int ntparms = DECL_NTPARMS (fn);
21839 tree targs = make_tree_vec (ntparms);
21840 tree decl_type = TREE_TYPE (decl);
21841 tree decl_arg_types;
21842 tree *args;
21843 unsigned int nargs, ix;
21844 tree arg;
21846 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21848 /* Never do unification on the 'this' parameter. */
21849 decl_arg_types = skip_artificial_parms_for (decl,
21850 TYPE_ARG_TYPES (decl_type));
21852 nargs = list_length (decl_arg_types);
21853 args = XALLOCAVEC (tree, nargs);
21854 for (arg = decl_arg_types, ix = 0;
21855 arg != NULL_TREE && arg != void_list_node;
21856 arg = TREE_CHAIN (arg), ++ix)
21857 args[ix] = TREE_VALUE (arg);
21859 if (fn_type_unification (fn, explicit_args, targs,
21860 args, ix,
21861 (check_rettype || DECL_CONV_FN_P (fn)
21862 ? TREE_TYPE (decl_type) : NULL_TREE),
21863 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21864 /*decltype*/false)
21865 == error_mark_node)
21866 return NULL_TREE;
21868 return targs;
21871 /* Return the innermost template arguments that, when applied to a partial
21872 specialization SPEC_TMPL of TMPL, yield the ARGS.
21874 For example, suppose we have:
21876 template <class T, class U> struct S {};
21877 template <class T> struct S<T*, int> {};
21879 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21880 partial specialization and the ARGS will be {double*, int}. The resulting
21881 vector will be {double}, indicating that `T' is bound to `double'. */
21883 static tree
21884 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21886 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21887 tree spec_args
21888 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21889 int i, ntparms = TREE_VEC_LENGTH (tparms);
21890 tree deduced_args;
21891 tree innermost_deduced_args;
21893 innermost_deduced_args = make_tree_vec (ntparms);
21894 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21896 deduced_args = copy_node (args);
21897 SET_TMPL_ARGS_LEVEL (deduced_args,
21898 TMPL_ARGS_DEPTH (deduced_args),
21899 innermost_deduced_args);
21901 else
21902 deduced_args = innermost_deduced_args;
21904 bool tried_array_deduction = (cxx_dialect < cxx1z);
21905 again:
21906 if (unify (tparms, deduced_args,
21907 INNERMOST_TEMPLATE_ARGS (spec_args),
21908 INNERMOST_TEMPLATE_ARGS (args),
21909 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21910 return NULL_TREE;
21912 for (i = 0; i < ntparms; ++i)
21913 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21915 if (!tried_array_deduction)
21917 try_array_deduction (tparms, innermost_deduced_args,
21918 INNERMOST_TEMPLATE_ARGS (spec_args));
21919 tried_array_deduction = true;
21920 if (TREE_VEC_ELT (innermost_deduced_args, i))
21921 goto again;
21923 return NULL_TREE;
21926 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21927 if (!push_tinst_level (tinst))
21929 excessive_deduction_depth = true;
21930 return NULL_TREE;
21933 /* Verify that nondeduced template arguments agree with the type
21934 obtained from argument deduction.
21936 For example:
21938 struct A { typedef int X; };
21939 template <class T, class U> struct C {};
21940 template <class T> struct C<T, typename T::X> {};
21942 Then with the instantiation `C<A, int>', we can deduce that
21943 `T' is `A' but unify () does not check whether `typename T::X'
21944 is `int'. */
21945 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21947 if (spec_args != error_mark_node)
21948 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21949 INNERMOST_TEMPLATE_ARGS (spec_args),
21950 tmpl, tf_none, false, false);
21952 pop_tinst_level ();
21954 if (spec_args == error_mark_node
21955 /* We only need to check the innermost arguments; the other
21956 arguments will always agree. */
21957 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21958 INNERMOST_TEMPLATE_ARGS (args)))
21959 return NULL_TREE;
21961 /* Now that we have bindings for all of the template arguments,
21962 ensure that the arguments deduced for the template template
21963 parameters have compatible template parameter lists. See the use
21964 of template_template_parm_bindings_ok_p in fn_type_unification
21965 for more information. */
21966 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21967 return NULL_TREE;
21969 return deduced_args;
21972 // Compare two function templates T1 and T2 by deducing bindings
21973 // from one against the other. If both deductions succeed, compare
21974 // constraints to see which is more constrained.
21975 static int
21976 more_specialized_inst (tree t1, tree t2)
21978 int fate = 0;
21979 int count = 0;
21981 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21983 --fate;
21984 ++count;
21987 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21989 ++fate;
21990 ++count;
21993 // If both deductions succeed, then one may be more constrained.
21994 if (count == 2 && fate == 0)
21995 fate = more_constrained (t1, t2);
21997 return fate;
22000 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22001 Return the TREE_LIST node with the most specialized template, if
22002 any. If there is no most specialized template, the error_mark_node
22003 is returned.
22005 Note that this function does not look at, or modify, the
22006 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22007 returned is one of the elements of INSTANTIATIONS, callers may
22008 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22009 and retrieve it from the value returned. */
22011 tree
22012 most_specialized_instantiation (tree templates)
22014 tree fn, champ;
22016 ++processing_template_decl;
22018 champ = templates;
22019 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22021 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22022 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22023 if (fate == -1)
22024 champ = fn;
22025 else if (!fate)
22027 /* Equally specialized, move to next function. If there
22028 is no next function, nothing's most specialized. */
22029 fn = TREE_CHAIN (fn);
22030 champ = fn;
22031 if (!fn)
22032 break;
22036 if (champ)
22037 /* Now verify that champ is better than everything earlier in the
22038 instantiation list. */
22039 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22040 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22042 champ = NULL_TREE;
22043 break;
22047 processing_template_decl--;
22049 if (!champ)
22050 return error_mark_node;
22052 return champ;
22055 /* If DECL is a specialization of some template, return the most
22056 general such template. Otherwise, returns NULL_TREE.
22058 For example, given:
22060 template <class T> struct S { template <class U> void f(U); };
22062 if TMPL is `template <class U> void S<int>::f(U)' this will return
22063 the full template. This function will not trace past partial
22064 specializations, however. For example, given in addition:
22066 template <class T> struct S<T*> { template <class U> void f(U); };
22068 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22069 `template <class T> template <class U> S<T*>::f(U)'. */
22071 tree
22072 most_general_template (tree decl)
22074 if (TREE_CODE (decl) != TEMPLATE_DECL)
22076 if (tree tinfo = get_template_info (decl))
22077 decl = TI_TEMPLATE (tinfo);
22078 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22079 template friend, or a FIELD_DECL for a capture pack. */
22080 if (TREE_CODE (decl) != TEMPLATE_DECL)
22081 return NULL_TREE;
22084 /* Look for more and more general templates. */
22085 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22087 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22088 (See cp-tree.h for details.) */
22089 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22090 break;
22092 if (CLASS_TYPE_P (TREE_TYPE (decl))
22093 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22094 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22095 break;
22097 /* Stop if we run into an explicitly specialized class template. */
22098 if (!DECL_NAMESPACE_SCOPE_P (decl)
22099 && DECL_CONTEXT (decl)
22100 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22101 break;
22103 decl = DECL_TI_TEMPLATE (decl);
22106 return decl;
22109 /* Return the most specialized of the template partial specializations
22110 which can produce TARGET, a specialization of some class or variable
22111 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22112 a TEMPLATE_DECL node corresponding to the partial specialization, while
22113 the TREE_PURPOSE is the set of template arguments that must be
22114 substituted into the template pattern in order to generate TARGET.
22116 If the choice of partial specialization is ambiguous, a diagnostic
22117 is issued, and the error_mark_node is returned. If there are no
22118 partial specializations matching TARGET, then NULL_TREE is
22119 returned, indicating that the primary template should be used. */
22121 static tree
22122 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22124 tree list = NULL_TREE;
22125 tree t;
22126 tree champ;
22127 int fate;
22128 bool ambiguous_p;
22129 tree outer_args = NULL_TREE;
22130 tree tmpl, args;
22132 if (TYPE_P (target))
22134 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22135 tmpl = TI_TEMPLATE (tinfo);
22136 args = TI_ARGS (tinfo);
22138 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22140 tmpl = TREE_OPERAND (target, 0);
22141 args = TREE_OPERAND (target, 1);
22143 else if (VAR_P (target))
22145 tree tinfo = DECL_TEMPLATE_INFO (target);
22146 tmpl = TI_TEMPLATE (tinfo);
22147 args = TI_ARGS (tinfo);
22149 else
22150 gcc_unreachable ();
22152 tree main_tmpl = most_general_template (tmpl);
22154 /* For determining which partial specialization to use, only the
22155 innermost args are interesting. */
22156 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22158 outer_args = strip_innermost_template_args (args, 1);
22159 args = INNERMOST_TEMPLATE_ARGS (args);
22162 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22164 tree spec_args;
22165 tree spec_tmpl = TREE_VALUE (t);
22167 if (outer_args)
22169 /* Substitute in the template args from the enclosing class. */
22170 ++processing_template_decl;
22171 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22172 --processing_template_decl;
22175 if (spec_tmpl == error_mark_node)
22176 return error_mark_node;
22178 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22179 if (spec_args)
22181 if (outer_args)
22182 spec_args = add_to_template_args (outer_args, spec_args);
22184 /* Keep the candidate only if the constraints are satisfied,
22185 or if we're not compiling with concepts. */
22186 if (!flag_concepts
22187 || constraints_satisfied_p (spec_tmpl, spec_args))
22189 list = tree_cons (spec_args, TREE_VALUE (t), list);
22190 TREE_TYPE (list) = TREE_TYPE (t);
22195 if (! list)
22196 return NULL_TREE;
22198 ambiguous_p = false;
22199 t = list;
22200 champ = t;
22201 t = TREE_CHAIN (t);
22202 for (; t; t = TREE_CHAIN (t))
22204 fate = more_specialized_partial_spec (tmpl, champ, t);
22205 if (fate == 1)
22207 else
22209 if (fate == 0)
22211 t = TREE_CHAIN (t);
22212 if (! t)
22214 ambiguous_p = true;
22215 break;
22218 champ = t;
22222 if (!ambiguous_p)
22223 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22225 fate = more_specialized_partial_spec (tmpl, champ, t);
22226 if (fate != 1)
22228 ambiguous_p = true;
22229 break;
22233 if (ambiguous_p)
22235 const char *str;
22236 char *spaces = NULL;
22237 if (!(complain & tf_error))
22238 return error_mark_node;
22239 if (TYPE_P (target))
22240 error ("ambiguous template instantiation for %q#T", target);
22241 else
22242 error ("ambiguous template instantiation for %q#D", target);
22243 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22244 for (t = list; t; t = TREE_CHAIN (t))
22246 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22247 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22248 "%s %#qS", spaces ? spaces : str, subst);
22249 spaces = spaces ? spaces : get_spaces (str);
22251 free (spaces);
22252 return error_mark_node;
22255 return champ;
22258 /* Explicitly instantiate DECL. */
22260 void
22261 do_decl_instantiation (tree decl, tree storage)
22263 tree result = NULL_TREE;
22264 int extern_p = 0;
22266 if (!decl || decl == error_mark_node)
22267 /* An error occurred, for which grokdeclarator has already issued
22268 an appropriate message. */
22269 return;
22270 else if (! DECL_LANG_SPECIFIC (decl))
22272 error ("explicit instantiation of non-template %q#D", decl);
22273 return;
22276 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22277 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22279 if (VAR_P (decl) && !var_templ)
22281 /* There is an asymmetry here in the way VAR_DECLs and
22282 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22283 the latter, the DECL we get back will be marked as a
22284 template instantiation, and the appropriate
22285 DECL_TEMPLATE_INFO will be set up. This does not happen for
22286 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22287 should handle VAR_DECLs as it currently handles
22288 FUNCTION_DECLs. */
22289 if (!DECL_CLASS_SCOPE_P (decl))
22291 error ("%qD is not a static data member of a class template", decl);
22292 return;
22294 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22295 if (!result || !VAR_P (result))
22297 error ("no matching template for %qD found", decl);
22298 return;
22300 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22302 error ("type %qT for explicit instantiation %qD does not match "
22303 "declared type %qT", TREE_TYPE (result), decl,
22304 TREE_TYPE (decl));
22305 return;
22308 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22310 error ("explicit instantiation of %q#D", decl);
22311 return;
22313 else
22314 result = decl;
22316 /* Check for various error cases. Note that if the explicit
22317 instantiation is valid the RESULT will currently be marked as an
22318 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22319 until we get here. */
22321 if (DECL_TEMPLATE_SPECIALIZATION (result))
22323 /* DR 259 [temp.spec].
22325 Both an explicit instantiation and a declaration of an explicit
22326 specialization shall not appear in a program unless the explicit
22327 instantiation follows a declaration of the explicit specialization.
22329 For a given set of template parameters, if an explicit
22330 instantiation of a template appears after a declaration of an
22331 explicit specialization for that template, the explicit
22332 instantiation has no effect. */
22333 return;
22335 else if (DECL_EXPLICIT_INSTANTIATION (result))
22337 /* [temp.spec]
22339 No program shall explicitly instantiate any template more
22340 than once.
22342 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22343 the first instantiation was `extern' and the second is not,
22344 and EXTERN_P for the opposite case. */
22345 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22346 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22347 /* If an "extern" explicit instantiation follows an ordinary
22348 explicit instantiation, the template is instantiated. */
22349 if (extern_p)
22350 return;
22352 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22354 error ("no matching template for %qD found", result);
22355 return;
22357 else if (!DECL_TEMPLATE_INFO (result))
22359 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22360 return;
22363 if (storage == NULL_TREE)
22365 else if (storage == ridpointers[(int) RID_EXTERN])
22367 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22368 pedwarn (input_location, OPT_Wpedantic,
22369 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22370 "instantiations");
22371 extern_p = 1;
22373 else
22374 error ("storage class %qD applied to template instantiation", storage);
22376 check_explicit_instantiation_namespace (result);
22377 mark_decl_instantiated (result, extern_p);
22378 if (! extern_p)
22379 instantiate_decl (result, /*defer_ok=*/true,
22380 /*expl_inst_class_mem_p=*/false);
22383 static void
22384 mark_class_instantiated (tree t, int extern_p)
22386 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22387 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22388 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22389 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22390 if (! extern_p)
22392 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22393 rest_of_type_compilation (t, 1);
22397 /* Called from do_type_instantiation through binding_table_foreach to
22398 do recursive instantiation for the type bound in ENTRY. */
22399 static void
22400 bt_instantiate_type_proc (binding_entry entry, void *data)
22402 tree storage = *(tree *) data;
22404 if (MAYBE_CLASS_TYPE_P (entry->type)
22405 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22406 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22409 /* Perform an explicit instantiation of template class T. STORAGE, if
22410 non-null, is the RID for extern, inline or static. COMPLAIN is
22411 nonzero if this is called from the parser, zero if called recursively,
22412 since the standard is unclear (as detailed below). */
22414 void
22415 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22417 int extern_p = 0;
22418 int nomem_p = 0;
22419 int static_p = 0;
22420 int previous_instantiation_extern_p = 0;
22422 if (TREE_CODE (t) == TYPE_DECL)
22423 t = TREE_TYPE (t);
22425 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22427 tree tmpl =
22428 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22429 if (tmpl)
22430 error ("explicit instantiation of non-class template %qD", tmpl);
22431 else
22432 error ("explicit instantiation of non-template type %qT", t);
22433 return;
22436 complete_type (t);
22438 if (!COMPLETE_TYPE_P (t))
22440 if (complain & tf_error)
22441 error ("explicit instantiation of %q#T before definition of template",
22443 return;
22446 if (storage != NULL_TREE)
22448 if (!in_system_header_at (input_location))
22450 if (storage == ridpointers[(int) RID_EXTERN])
22452 if (cxx_dialect == cxx98)
22453 pedwarn (input_location, OPT_Wpedantic,
22454 "ISO C++ 1998 forbids the use of %<extern%> on "
22455 "explicit instantiations");
22457 else
22458 pedwarn (input_location, OPT_Wpedantic,
22459 "ISO C++ forbids the use of %qE"
22460 " on explicit instantiations", storage);
22463 if (storage == ridpointers[(int) RID_INLINE])
22464 nomem_p = 1;
22465 else if (storage == ridpointers[(int) RID_EXTERN])
22466 extern_p = 1;
22467 else if (storage == ridpointers[(int) RID_STATIC])
22468 static_p = 1;
22469 else
22471 error ("storage class %qD applied to template instantiation",
22472 storage);
22473 extern_p = 0;
22477 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22479 /* DR 259 [temp.spec].
22481 Both an explicit instantiation and a declaration of an explicit
22482 specialization shall not appear in a program unless the explicit
22483 instantiation follows a declaration of the explicit specialization.
22485 For a given set of template parameters, if an explicit
22486 instantiation of a template appears after a declaration of an
22487 explicit specialization for that template, the explicit
22488 instantiation has no effect. */
22489 return;
22491 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22493 /* [temp.spec]
22495 No program shall explicitly instantiate any template more
22496 than once.
22498 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22499 instantiation was `extern'. If EXTERN_P then the second is.
22500 These cases are OK. */
22501 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22503 if (!previous_instantiation_extern_p && !extern_p
22504 && (complain & tf_error))
22505 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22507 /* If we've already instantiated the template, just return now. */
22508 if (!CLASSTYPE_INTERFACE_ONLY (t))
22509 return;
22512 check_explicit_instantiation_namespace (TYPE_NAME (t));
22513 mark_class_instantiated (t, extern_p);
22515 if (nomem_p)
22516 return;
22518 /* In contrast to implicit instantiation, where only the
22519 declarations, and not the definitions, of members are
22520 instantiated, we have here:
22522 [temp.explicit]
22524 The explicit instantiation of a class template specialization
22525 implies the instantiation of all of its members not
22526 previously explicitly specialized in the translation unit
22527 containing the explicit instantiation.
22529 Of course, we can't instantiate member template classes, since we
22530 don't have any arguments for them. Note that the standard is
22531 unclear on whether the instantiation of the members are
22532 *explicit* instantiations or not. However, the most natural
22533 interpretation is that it should be an explicit
22534 instantiation. */
22535 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22536 if ((VAR_P (fld)
22537 || (TREE_CODE (fld) == FUNCTION_DECL
22538 && !static_p
22539 && user_provided_p (fld)))
22540 && DECL_TEMPLATE_INSTANTIATION (fld))
22542 mark_decl_instantiated (fld, extern_p);
22543 if (! extern_p)
22544 instantiate_decl (fld, /*defer_ok=*/true,
22545 /*expl_inst_class_mem_p=*/true);
22548 if (CLASSTYPE_NESTED_UTDS (t))
22549 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22550 bt_instantiate_type_proc, &storage);
22553 /* Given a function DECL, which is a specialization of TMPL, modify
22554 DECL to be a re-instantiation of TMPL with the same template
22555 arguments. TMPL should be the template into which tsubst'ing
22556 should occur for DECL, not the most general template.
22558 One reason for doing this is a scenario like this:
22560 template <class T>
22561 void f(const T&, int i);
22563 void g() { f(3, 7); }
22565 template <class T>
22566 void f(const T& t, const int i) { }
22568 Note that when the template is first instantiated, with
22569 instantiate_template, the resulting DECL will have no name for the
22570 first parameter, and the wrong type for the second. So, when we go
22571 to instantiate the DECL, we regenerate it. */
22573 static void
22574 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22576 /* The arguments used to instantiate DECL, from the most general
22577 template. */
22578 tree code_pattern;
22580 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22582 /* Make sure that we can see identifiers, and compute access
22583 correctly. */
22584 push_access_scope (decl);
22586 if (TREE_CODE (decl) == FUNCTION_DECL)
22588 tree decl_parm;
22589 tree pattern_parm;
22590 tree specs;
22591 int args_depth;
22592 int parms_depth;
22594 args_depth = TMPL_ARGS_DEPTH (args);
22595 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22596 if (args_depth > parms_depth)
22597 args = get_innermost_template_args (args, parms_depth);
22599 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22600 args, tf_error, NULL_TREE,
22601 /*defer_ok*/false);
22602 if (specs && specs != error_mark_node)
22603 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22604 specs);
22606 /* Merge parameter declarations. */
22607 decl_parm = skip_artificial_parms_for (decl,
22608 DECL_ARGUMENTS (decl));
22609 pattern_parm
22610 = skip_artificial_parms_for (code_pattern,
22611 DECL_ARGUMENTS (code_pattern));
22612 while (decl_parm && !DECL_PACK_P (pattern_parm))
22614 tree parm_type;
22615 tree attributes;
22617 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22618 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22619 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22620 NULL_TREE);
22621 parm_type = type_decays_to (parm_type);
22622 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22623 TREE_TYPE (decl_parm) = parm_type;
22624 attributes = DECL_ATTRIBUTES (pattern_parm);
22625 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22627 DECL_ATTRIBUTES (decl_parm) = attributes;
22628 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22630 decl_parm = DECL_CHAIN (decl_parm);
22631 pattern_parm = DECL_CHAIN (pattern_parm);
22633 /* Merge any parameters that match with the function parameter
22634 pack. */
22635 if (pattern_parm && DECL_PACK_P (pattern_parm))
22637 int i, len;
22638 tree expanded_types;
22639 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22640 the parameters in this function parameter pack. */
22641 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22642 args, tf_error, NULL_TREE);
22643 len = TREE_VEC_LENGTH (expanded_types);
22644 for (i = 0; i < len; i++)
22646 tree parm_type;
22647 tree attributes;
22649 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22650 /* Rename the parameter to include the index. */
22651 DECL_NAME (decl_parm) =
22652 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22653 parm_type = TREE_VEC_ELT (expanded_types, i);
22654 parm_type = type_decays_to (parm_type);
22655 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22656 TREE_TYPE (decl_parm) = parm_type;
22657 attributes = DECL_ATTRIBUTES (pattern_parm);
22658 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22660 DECL_ATTRIBUTES (decl_parm) = attributes;
22661 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22663 decl_parm = DECL_CHAIN (decl_parm);
22666 /* Merge additional specifiers from the CODE_PATTERN. */
22667 if (DECL_DECLARED_INLINE_P (code_pattern)
22668 && !DECL_DECLARED_INLINE_P (decl))
22669 DECL_DECLARED_INLINE_P (decl) = 1;
22671 else if (VAR_P (decl))
22673 start_lambda_scope (decl);
22674 DECL_INITIAL (decl) =
22675 tsubst_expr (DECL_INITIAL (code_pattern), args,
22676 tf_error, DECL_TI_TEMPLATE (decl),
22677 /*integral_constant_expression_p=*/false);
22678 finish_lambda_scope ();
22679 if (VAR_HAD_UNKNOWN_BOUND (decl))
22680 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22681 tf_error, DECL_TI_TEMPLATE (decl));
22683 else
22684 gcc_unreachable ();
22686 pop_access_scope (decl);
22689 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22690 substituted to get DECL. */
22692 tree
22693 template_for_substitution (tree decl)
22695 tree tmpl = DECL_TI_TEMPLATE (decl);
22697 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22698 for the instantiation. This is not always the most general
22699 template. Consider, for example:
22701 template <class T>
22702 struct S { template <class U> void f();
22703 template <> void f<int>(); };
22705 and an instantiation of S<double>::f<int>. We want TD to be the
22706 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22707 while (/* An instantiation cannot have a definition, so we need a
22708 more general template. */
22709 DECL_TEMPLATE_INSTANTIATION (tmpl)
22710 /* We must also deal with friend templates. Given:
22712 template <class T> struct S {
22713 template <class U> friend void f() {};
22716 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22717 so far as the language is concerned, but that's still
22718 where we get the pattern for the instantiation from. On
22719 other hand, if the definition comes outside the class, say:
22721 template <class T> struct S {
22722 template <class U> friend void f();
22724 template <class U> friend void f() {}
22726 we don't need to look any further. That's what the check for
22727 DECL_INITIAL is for. */
22728 || (TREE_CODE (decl) == FUNCTION_DECL
22729 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22730 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22732 /* The present template, TD, should not be a definition. If it
22733 were a definition, we should be using it! Note that we
22734 cannot restructure the loop to just keep going until we find
22735 a template with a definition, since that might go too far if
22736 a specialization was declared, but not defined. */
22738 /* Fetch the more general template. */
22739 tmpl = DECL_TI_TEMPLATE (tmpl);
22742 return tmpl;
22745 /* Returns true if we need to instantiate this template instance even if we
22746 know we aren't going to emit it. */
22748 bool
22749 always_instantiate_p (tree decl)
22751 /* We always instantiate inline functions so that we can inline them. An
22752 explicit instantiation declaration prohibits implicit instantiation of
22753 non-inline functions. With high levels of optimization, we would
22754 normally inline non-inline functions -- but we're not allowed to do
22755 that for "extern template" functions. Therefore, we check
22756 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22757 return ((TREE_CODE (decl) == FUNCTION_DECL
22758 && (DECL_DECLARED_INLINE_P (decl)
22759 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22760 /* And we need to instantiate static data members so that
22761 their initializers are available in integral constant
22762 expressions. */
22763 || (VAR_P (decl)
22764 && decl_maybe_constant_var_p (decl)));
22767 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22768 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22769 error, true otherwise. */
22771 bool
22772 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22774 tree fntype, spec, noex, clone;
22776 /* Don't instantiate a noexcept-specification from template context. */
22777 if (processing_template_decl)
22778 return true;
22780 if (DECL_CLONED_FUNCTION_P (fn))
22781 fn = DECL_CLONED_FUNCTION (fn);
22782 fntype = TREE_TYPE (fn);
22783 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22785 if (!spec || !TREE_PURPOSE (spec))
22786 return true;
22788 noex = TREE_PURPOSE (spec);
22790 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22792 static hash_set<tree>* fns = new hash_set<tree>;
22793 bool added = false;
22794 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22795 spec = get_defaulted_eh_spec (fn, complain);
22796 else if (!(added = !fns->add (fn)))
22798 /* If hash_set::add returns true, the element was already there. */
22799 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22800 DECL_SOURCE_LOCATION (fn));
22801 error_at (loc,
22802 "exception specification of %qD depends on itself",
22803 fn);
22804 spec = noexcept_false_spec;
22806 else if (push_tinst_level (fn))
22808 push_access_scope (fn);
22809 push_deferring_access_checks (dk_no_deferred);
22810 input_location = DECL_SOURCE_LOCATION (fn);
22811 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22812 DEFERRED_NOEXCEPT_ARGS (noex),
22813 tf_warning_or_error, fn,
22814 /*function_p=*/false,
22815 /*integral_constant_expression_p=*/true);
22816 pop_deferring_access_checks ();
22817 pop_access_scope (fn);
22818 pop_tinst_level ();
22819 spec = build_noexcept_spec (noex, tf_warning_or_error);
22820 if (spec == error_mark_node)
22821 spec = noexcept_false_spec;
22823 else
22824 spec = noexcept_false_spec;
22826 if (added)
22827 fns->remove (fn);
22829 if (spec == error_mark_node)
22830 return false;
22832 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22835 FOR_EACH_CLONE (clone, fn)
22837 if (TREE_TYPE (clone) == fntype)
22838 TREE_TYPE (clone) = TREE_TYPE (fn);
22839 else
22840 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22843 return true;
22846 /* We're starting to process the function INST, an instantiation of PATTERN;
22847 add their parameters to local_specializations. */
22849 static void
22850 register_parameter_specializations (tree pattern, tree inst)
22852 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22853 tree spec_parm = DECL_ARGUMENTS (inst);
22854 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22856 register_local_specialization (spec_parm, tmpl_parm);
22857 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22858 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22860 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22862 if (!DECL_PACK_P (tmpl_parm))
22864 register_local_specialization (spec_parm, tmpl_parm);
22865 spec_parm = DECL_CHAIN (spec_parm);
22867 else
22869 /* Register the (value) argument pack as a specialization of
22870 TMPL_PARM, then move on. */
22871 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22872 register_local_specialization (argpack, tmpl_parm);
22875 gcc_assert (!spec_parm);
22878 /* Produce the definition of D, a _DECL generated from a template. If
22879 DEFER_OK is true, then we don't have to actually do the
22880 instantiation now; we just have to do it sometime. Normally it is
22881 an error if this is an explicit instantiation but D is undefined.
22882 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22883 instantiated class template. */
22885 tree
22886 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22888 tree tmpl = DECL_TI_TEMPLATE (d);
22889 tree gen_args;
22890 tree args;
22891 tree td;
22892 tree code_pattern;
22893 tree spec;
22894 tree gen_tmpl;
22895 bool pattern_defined;
22896 location_t saved_loc = input_location;
22897 int saved_unevaluated_operand = cp_unevaluated_operand;
22898 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22899 bool external_p;
22900 bool deleted_p;
22902 /* This function should only be used to instantiate templates for
22903 functions and static member variables. */
22904 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22906 /* A concept is never instantiated. */
22907 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22909 /* Variables are never deferred; if instantiation is required, they
22910 are instantiated right away. That allows for better code in the
22911 case that an expression refers to the value of the variable --
22912 if the variable has a constant value the referring expression can
22913 take advantage of that fact. */
22914 if (VAR_P (d))
22915 defer_ok = false;
22917 /* Don't instantiate cloned functions. Instead, instantiate the
22918 functions they cloned. */
22919 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22920 d = DECL_CLONED_FUNCTION (d);
22922 if (DECL_TEMPLATE_INSTANTIATED (d)
22923 || (TREE_CODE (d) == FUNCTION_DECL
22924 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22925 || DECL_TEMPLATE_SPECIALIZATION (d))
22926 /* D has already been instantiated or explicitly specialized, so
22927 there's nothing for us to do here.
22929 It might seem reasonable to check whether or not D is an explicit
22930 instantiation, and, if so, stop here. But when an explicit
22931 instantiation is deferred until the end of the compilation,
22932 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22933 the instantiation. */
22934 return d;
22936 /* Check to see whether we know that this template will be
22937 instantiated in some other file, as with "extern template"
22938 extension. */
22939 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22941 /* In general, we do not instantiate such templates. */
22942 if (external_p && !always_instantiate_p (d))
22943 return d;
22945 gen_tmpl = most_general_template (tmpl);
22946 gen_args = DECL_TI_ARGS (d);
22948 if (tmpl != gen_tmpl)
22949 /* We should already have the extra args. */
22950 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22951 == TMPL_ARGS_DEPTH (gen_args));
22952 /* And what's in the hash table should match D. */
22953 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22954 || spec == NULL_TREE);
22956 /* This needs to happen before any tsubsting. */
22957 if (! push_tinst_level (d))
22958 return d;
22960 timevar_push (TV_TEMPLATE_INST);
22962 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22963 for the instantiation. */
22964 td = template_for_substitution (d);
22965 args = gen_args;
22967 if (VAR_P (d))
22969 /* Look up an explicit specialization, if any. */
22970 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22971 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22972 if (elt && elt != error_mark_node)
22974 td = TREE_VALUE (elt);
22975 args = TREE_PURPOSE (elt);
22979 code_pattern = DECL_TEMPLATE_RESULT (td);
22981 /* We should never be trying to instantiate a member of a class
22982 template or partial specialization. */
22983 gcc_assert (d != code_pattern);
22985 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22986 || DECL_TEMPLATE_SPECIALIZATION (td))
22987 /* In the case of a friend template whose definition is provided
22988 outside the class, we may have too many arguments. Drop the
22989 ones we don't need. The same is true for specializations. */
22990 args = get_innermost_template_args
22991 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22993 if (TREE_CODE (d) == FUNCTION_DECL)
22995 deleted_p = DECL_DELETED_FN (code_pattern);
22996 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22997 && DECL_INITIAL (code_pattern) != error_mark_node)
22998 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22999 || deleted_p);
23001 else
23003 deleted_p = false;
23004 if (DECL_CLASS_SCOPE_P (code_pattern))
23005 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23006 || DECL_INLINE_VAR_P (code_pattern));
23007 else
23008 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23011 /* We may be in the middle of deferred access check. Disable it now. */
23012 push_deferring_access_checks (dk_no_deferred);
23014 /* Unless an explicit instantiation directive has already determined
23015 the linkage of D, remember that a definition is available for
23016 this entity. */
23017 if (pattern_defined
23018 && !DECL_INTERFACE_KNOWN (d)
23019 && !DECL_NOT_REALLY_EXTERN (d))
23020 mark_definable (d);
23022 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23023 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23024 input_location = DECL_SOURCE_LOCATION (d);
23026 /* If D is a member of an explicitly instantiated class template,
23027 and no definition is available, treat it like an implicit
23028 instantiation. */
23029 if (!pattern_defined && expl_inst_class_mem_p
23030 && DECL_EXPLICIT_INSTANTIATION (d))
23032 /* Leave linkage flags alone on instantiations with anonymous
23033 visibility. */
23034 if (TREE_PUBLIC (d))
23036 DECL_NOT_REALLY_EXTERN (d) = 0;
23037 DECL_INTERFACE_KNOWN (d) = 0;
23039 SET_DECL_IMPLICIT_INSTANTIATION (d);
23042 /* Defer all other templates, unless we have been explicitly
23043 forbidden from doing so. */
23044 if (/* If there is no definition, we cannot instantiate the
23045 template. */
23046 ! pattern_defined
23047 /* If it's OK to postpone instantiation, do so. */
23048 || defer_ok
23049 /* If this is a static data member that will be defined
23050 elsewhere, we don't want to instantiate the entire data
23051 member, but we do want to instantiate the initializer so that
23052 we can substitute that elsewhere. */
23053 || (external_p && VAR_P (d))
23054 /* Handle here a deleted function too, avoid generating
23055 its body (c++/61080). */
23056 || deleted_p)
23058 /* The definition of the static data member is now required so
23059 we must substitute the initializer. */
23060 if (VAR_P (d)
23061 && !DECL_INITIAL (d)
23062 && DECL_INITIAL (code_pattern))
23064 tree ns;
23065 tree init;
23066 bool const_init = false;
23067 bool enter_context = DECL_CLASS_SCOPE_P (d);
23069 ns = decl_namespace_context (d);
23070 push_nested_namespace (ns);
23071 if (enter_context)
23072 push_nested_class (DECL_CONTEXT (d));
23073 init = tsubst_expr (DECL_INITIAL (code_pattern),
23074 args,
23075 tf_warning_or_error, NULL_TREE,
23076 /*integral_constant_expression_p=*/false);
23077 /* If instantiating the initializer involved instantiating this
23078 again, don't call cp_finish_decl twice. */
23079 if (!DECL_INITIAL (d))
23081 /* Make sure the initializer is still constant, in case of
23082 circular dependency (template/instantiate6.C). */
23083 const_init
23084 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23085 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23086 /*asmspec_tree=*/NULL_TREE,
23087 LOOKUP_ONLYCONVERTING);
23089 if (enter_context)
23090 pop_nested_class ();
23091 pop_nested_namespace (ns);
23094 /* We restore the source position here because it's used by
23095 add_pending_template. */
23096 input_location = saved_loc;
23098 if (at_eof && !pattern_defined
23099 && DECL_EXPLICIT_INSTANTIATION (d)
23100 && DECL_NOT_REALLY_EXTERN (d))
23101 /* [temp.explicit]
23103 The definition of a non-exported function template, a
23104 non-exported member function template, or a non-exported
23105 member function or static data member of a class template
23106 shall be present in every translation unit in which it is
23107 explicitly instantiated. */
23108 permerror (input_location, "explicit instantiation of %qD "
23109 "but no definition available", d);
23111 /* If we're in unevaluated context, we just wanted to get the
23112 constant value; this isn't an odr use, so don't queue
23113 a full instantiation. */
23114 if (cp_unevaluated_operand != 0)
23115 goto out;
23116 /* ??? Historically, we have instantiated inline functions, even
23117 when marked as "extern template". */
23118 if (!(external_p && VAR_P (d)))
23119 add_pending_template (d);
23120 goto out;
23122 /* Tell the repository that D is available in this translation unit
23123 -- and see if it is supposed to be instantiated here. */
23124 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23126 /* In a PCH file, despite the fact that the repository hasn't
23127 requested instantiation in the PCH it is still possible that
23128 an instantiation will be required in a file that includes the
23129 PCH. */
23130 if (pch_file)
23131 add_pending_template (d);
23132 /* Instantiate inline functions so that the inliner can do its
23133 job, even though we'll not be emitting a copy of this
23134 function. */
23135 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23136 goto out;
23139 bool push_to_top, nested;
23140 tree fn_context;
23141 fn_context = decl_function_context (d);
23142 nested = current_function_decl != NULL_TREE;
23143 push_to_top = !(nested && fn_context == current_function_decl);
23145 vec<tree> omp_privatization_save;
23146 if (nested)
23147 save_omp_privatization_clauses (omp_privatization_save);
23149 if (push_to_top)
23150 push_to_top_level ();
23151 else
23153 push_function_context ();
23154 cp_unevaluated_operand = 0;
23155 c_inhibit_evaluation_warnings = 0;
23158 /* Mark D as instantiated so that recursive calls to
23159 instantiate_decl do not try to instantiate it again. */
23160 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23162 /* Regenerate the declaration in case the template has been modified
23163 by a subsequent redeclaration. */
23164 regenerate_decl_from_template (d, td, args);
23166 /* We already set the file and line above. Reset them now in case
23167 they changed as a result of calling regenerate_decl_from_template. */
23168 input_location = DECL_SOURCE_LOCATION (d);
23170 if (VAR_P (d))
23172 tree init;
23173 bool const_init = false;
23175 /* Clear out DECL_RTL; whatever was there before may not be right
23176 since we've reset the type of the declaration. */
23177 SET_DECL_RTL (d, NULL);
23178 DECL_IN_AGGR_P (d) = 0;
23180 /* The initializer is placed in DECL_INITIAL by
23181 regenerate_decl_from_template so we don't need to
23182 push/pop_access_scope again here. Pull it out so that
23183 cp_finish_decl can process it. */
23184 init = DECL_INITIAL (d);
23185 DECL_INITIAL (d) = NULL_TREE;
23186 DECL_INITIALIZED_P (d) = 0;
23188 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23189 initializer. That function will defer actual emission until
23190 we have a chance to determine linkage. */
23191 DECL_EXTERNAL (d) = 0;
23193 /* Enter the scope of D so that access-checking works correctly. */
23194 bool enter_context = DECL_CLASS_SCOPE_P (d);
23195 if (enter_context)
23196 push_nested_class (DECL_CONTEXT (d));
23198 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23199 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23201 if (enter_context)
23202 pop_nested_class ();
23204 if (variable_template_p (gen_tmpl))
23205 note_variable_template_instantiation (d);
23207 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23208 synthesize_method (d);
23209 else if (TREE_CODE (d) == FUNCTION_DECL)
23211 hash_map<tree, tree> *saved_local_specializations;
23212 tree block = NULL_TREE;
23214 /* Save away the current list, in case we are instantiating one
23215 template from within the body of another. */
23216 saved_local_specializations = local_specializations;
23218 /* Set up the list of local specializations. */
23219 local_specializations = new hash_map<tree, tree>;
23221 /* Set up context. */
23222 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23223 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23224 block = push_stmt_list ();
23225 else
23226 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23228 /* Some typedefs referenced from within the template code need to be
23229 access checked at template instantiation time, i.e now. These
23230 types were added to the template at parsing time. Let's get those
23231 and perform the access checks then. */
23232 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23233 args);
23235 /* Create substitution entries for the parameters. */
23236 register_parameter_specializations (code_pattern, d);
23238 /* Substitute into the body of the function. */
23239 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23240 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23241 tf_warning_or_error, tmpl);
23242 else
23244 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23245 tf_warning_or_error, tmpl,
23246 /*integral_constant_expression_p=*/false);
23248 /* Set the current input_location to the end of the function
23249 so that finish_function knows where we are. */
23250 input_location
23251 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23253 /* Remember if we saw an infinite loop in the template. */
23254 current_function_infinite_loop
23255 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23258 /* We don't need the local specializations any more. */
23259 delete local_specializations;
23260 local_specializations = saved_local_specializations;
23262 /* Finish the function. */
23263 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23264 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23265 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23266 else
23268 d = finish_function (0);
23269 expand_or_defer_fn (d);
23272 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23273 cp_check_omp_declare_reduction (d);
23276 /* We're not deferring instantiation any more. */
23277 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23279 if (push_to_top)
23280 pop_from_top_level ();
23281 else
23282 pop_function_context ();
23284 if (nested)
23285 restore_omp_privatization_clauses (omp_privatization_save);
23287 out:
23288 pop_deferring_access_checks ();
23289 timevar_pop (TV_TEMPLATE_INST);
23290 pop_tinst_level ();
23291 input_location = saved_loc;
23292 cp_unevaluated_operand = saved_unevaluated_operand;
23293 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23295 return d;
23298 /* Run through the list of templates that we wish we could
23299 instantiate, and instantiate any we can. RETRIES is the
23300 number of times we retry pending template instantiation. */
23302 void
23303 instantiate_pending_templates (int retries)
23305 int reconsider;
23306 location_t saved_loc = input_location;
23308 /* Instantiating templates may trigger vtable generation. This in turn
23309 may require further template instantiations. We place a limit here
23310 to avoid infinite loop. */
23311 if (pending_templates && retries >= max_tinst_depth)
23313 tree decl = pending_templates->tinst->decl;
23315 fatal_error (input_location,
23316 "template instantiation depth exceeds maximum of %d"
23317 " instantiating %q+D, possibly from virtual table generation"
23318 " (use -ftemplate-depth= to increase the maximum)",
23319 max_tinst_depth, decl);
23320 if (TREE_CODE (decl) == FUNCTION_DECL)
23321 /* Pretend that we defined it. */
23322 DECL_INITIAL (decl) = error_mark_node;
23323 return;
23328 struct pending_template **t = &pending_templates;
23329 struct pending_template *last = NULL;
23330 reconsider = 0;
23331 while (*t)
23333 tree instantiation = reopen_tinst_level ((*t)->tinst);
23334 bool complete = false;
23336 if (TYPE_P (instantiation))
23338 if (!COMPLETE_TYPE_P (instantiation))
23340 instantiate_class_template (instantiation);
23341 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23342 for (tree fld = TYPE_FIELDS (instantiation);
23343 fld; fld = TREE_CHAIN (fld))
23344 if ((VAR_P (fld)
23345 || (TREE_CODE (fld) == FUNCTION_DECL
23346 && !DECL_ARTIFICIAL (fld)))
23347 && DECL_TEMPLATE_INSTANTIATION (fld))
23348 instantiate_decl (fld,
23349 /*defer_ok=*/false,
23350 /*expl_inst_class_mem_p=*/false);
23352 if (COMPLETE_TYPE_P (instantiation))
23353 reconsider = 1;
23356 complete = COMPLETE_TYPE_P (instantiation);
23358 else
23360 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23361 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23363 instantiation
23364 = instantiate_decl (instantiation,
23365 /*defer_ok=*/false,
23366 /*expl_inst_class_mem_p=*/false);
23367 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23368 reconsider = 1;
23371 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23372 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23375 if (complete)
23376 /* If INSTANTIATION has been instantiated, then we don't
23377 need to consider it again in the future. */
23378 *t = (*t)->next;
23379 else
23381 last = *t;
23382 t = &(*t)->next;
23384 tinst_depth = 0;
23385 current_tinst_level = NULL;
23387 last_pending_template = last;
23389 while (reconsider);
23391 input_location = saved_loc;
23394 /* Substitute ARGVEC into T, which is a list of initializers for
23395 either base class or a non-static data member. The TREE_PURPOSEs
23396 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23397 instantiate_decl. */
23399 static tree
23400 tsubst_initializer_list (tree t, tree argvec)
23402 tree inits = NULL_TREE;
23404 for (; t; t = TREE_CHAIN (t))
23406 tree decl;
23407 tree init;
23408 tree expanded_bases = NULL_TREE;
23409 tree expanded_arguments = NULL_TREE;
23410 int i, len = 1;
23412 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23414 tree expr;
23415 tree arg;
23417 /* Expand the base class expansion type into separate base
23418 classes. */
23419 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23420 tf_warning_or_error,
23421 NULL_TREE);
23422 if (expanded_bases == error_mark_node)
23423 continue;
23425 /* We'll be building separate TREE_LISTs of arguments for
23426 each base. */
23427 len = TREE_VEC_LENGTH (expanded_bases);
23428 expanded_arguments = make_tree_vec (len);
23429 for (i = 0; i < len; i++)
23430 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23432 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23433 expand each argument in the TREE_VALUE of t. */
23434 expr = make_node (EXPR_PACK_EXPANSION);
23435 PACK_EXPANSION_LOCAL_P (expr) = true;
23436 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23437 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23439 if (TREE_VALUE (t) == void_type_node)
23440 /* VOID_TYPE_NODE is used to indicate
23441 value-initialization. */
23443 for (i = 0; i < len; i++)
23444 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23446 else
23448 /* Substitute parameter packs into each argument in the
23449 TREE_LIST. */
23450 in_base_initializer = 1;
23451 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23453 tree expanded_exprs;
23455 /* Expand the argument. */
23456 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23457 expanded_exprs
23458 = tsubst_pack_expansion (expr, argvec,
23459 tf_warning_or_error,
23460 NULL_TREE);
23461 if (expanded_exprs == error_mark_node)
23462 continue;
23464 /* Prepend each of the expanded expressions to the
23465 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23466 for (i = 0; i < len; i++)
23468 TREE_VEC_ELT (expanded_arguments, i) =
23469 tree_cons (NULL_TREE,
23470 TREE_VEC_ELT (expanded_exprs, i),
23471 TREE_VEC_ELT (expanded_arguments, i));
23474 in_base_initializer = 0;
23476 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23477 since we built them backwards. */
23478 for (i = 0; i < len; i++)
23480 TREE_VEC_ELT (expanded_arguments, i) =
23481 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23486 for (i = 0; i < len; ++i)
23488 if (expanded_bases)
23490 decl = TREE_VEC_ELT (expanded_bases, i);
23491 decl = expand_member_init (decl);
23492 init = TREE_VEC_ELT (expanded_arguments, i);
23494 else
23496 tree tmp;
23497 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23498 tf_warning_or_error, NULL_TREE);
23500 decl = expand_member_init (decl);
23501 if (decl && !DECL_P (decl))
23502 in_base_initializer = 1;
23504 init = TREE_VALUE (t);
23505 tmp = init;
23506 if (init != void_type_node)
23507 init = tsubst_expr (init, argvec,
23508 tf_warning_or_error, NULL_TREE,
23509 /*integral_constant_expression_p=*/false);
23510 if (init == NULL_TREE && tmp != NULL_TREE)
23511 /* If we had an initializer but it instantiated to nothing,
23512 value-initialize the object. This will only occur when
23513 the initializer was a pack expansion where the parameter
23514 packs used in that expansion were of length zero. */
23515 init = void_type_node;
23516 in_base_initializer = 0;
23519 if (decl)
23521 init = build_tree_list (decl, init);
23522 TREE_CHAIN (init) = inits;
23523 inits = init;
23527 return inits;
23530 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23532 static void
23533 set_current_access_from_decl (tree decl)
23535 if (TREE_PRIVATE (decl))
23536 current_access_specifier = access_private_node;
23537 else if (TREE_PROTECTED (decl))
23538 current_access_specifier = access_protected_node;
23539 else
23540 current_access_specifier = access_public_node;
23543 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23544 is the instantiation (which should have been created with
23545 start_enum) and ARGS are the template arguments to use. */
23547 static void
23548 tsubst_enum (tree tag, tree newtag, tree args)
23550 tree e;
23552 if (SCOPED_ENUM_P (newtag))
23553 begin_scope (sk_scoped_enum, newtag);
23555 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23557 tree value;
23558 tree decl;
23560 decl = TREE_VALUE (e);
23561 /* Note that in a template enum, the TREE_VALUE is the
23562 CONST_DECL, not the corresponding INTEGER_CST. */
23563 value = tsubst_expr (DECL_INITIAL (decl),
23564 args, tf_warning_or_error, NULL_TREE,
23565 /*integral_constant_expression_p=*/true);
23567 /* Give this enumeration constant the correct access. */
23568 set_current_access_from_decl (decl);
23570 /* Actually build the enumerator itself. Here we're assuming that
23571 enumerators can't have dependent attributes. */
23572 build_enumerator (DECL_NAME (decl), value, newtag,
23573 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23576 if (SCOPED_ENUM_P (newtag))
23577 finish_scope ();
23579 finish_enum_value_list (newtag);
23580 finish_enum (newtag);
23582 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23583 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23586 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23587 its type -- but without substituting the innermost set of template
23588 arguments. So, innermost set of template parameters will appear in
23589 the type. */
23591 tree
23592 get_mostly_instantiated_function_type (tree decl)
23594 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23595 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23598 /* Return truthvalue if we're processing a template different from
23599 the last one involved in diagnostics. */
23600 bool
23601 problematic_instantiation_changed (void)
23603 return current_tinst_level != last_error_tinst_level;
23606 /* Remember current template involved in diagnostics. */
23607 void
23608 record_last_problematic_instantiation (void)
23610 last_error_tinst_level = current_tinst_level;
23613 struct tinst_level *
23614 current_instantiation (void)
23616 return current_tinst_level;
23619 /* Return TRUE if current_function_decl is being instantiated, false
23620 otherwise. */
23622 bool
23623 instantiating_current_function_p (void)
23625 return (current_instantiation ()
23626 && current_instantiation ()->decl == current_function_decl);
23629 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23630 type. Return zero for ok, nonzero for disallowed. Issue error and
23631 warning messages under control of COMPLAIN. */
23633 static int
23634 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23636 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23637 return 0;
23638 else if (POINTER_TYPE_P (type))
23639 return 0;
23640 else if (TYPE_PTRMEM_P (type))
23641 return 0;
23642 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23643 return 0;
23644 else if (TREE_CODE (type) == TYPENAME_TYPE)
23645 return 0;
23646 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23647 return 0;
23648 else if (TREE_CODE (type) == NULLPTR_TYPE)
23649 return 0;
23650 /* A bound template template parm could later be instantiated to have a valid
23651 nontype parm type via an alias template. */
23652 else if (cxx_dialect >= cxx11
23653 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23654 return 0;
23656 if (complain & tf_error)
23658 if (type == error_mark_node)
23659 inform (input_location, "invalid template non-type parameter");
23660 else
23661 error ("%q#T is not a valid type for a template non-type parameter",
23662 type);
23664 return 1;
23667 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23668 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23670 static bool
23671 dependent_type_p_r (tree type)
23673 tree scope;
23675 /* [temp.dep.type]
23677 A type is dependent if it is:
23679 -- a template parameter. Template template parameters are types
23680 for us (since TYPE_P holds true for them) so we handle
23681 them here. */
23682 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23683 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23684 return true;
23685 /* -- a qualified-id with a nested-name-specifier which contains a
23686 class-name that names a dependent type or whose unqualified-id
23687 names a dependent type. */
23688 if (TREE_CODE (type) == TYPENAME_TYPE)
23689 return true;
23691 /* An alias template specialization can be dependent even if the
23692 resulting type is not. */
23693 if (dependent_alias_template_spec_p (type))
23694 return true;
23696 /* -- a cv-qualified type where the cv-unqualified type is
23697 dependent.
23698 No code is necessary for this bullet; the code below handles
23699 cv-qualified types, and we don't want to strip aliases with
23700 TYPE_MAIN_VARIANT because of DR 1558. */
23701 /* -- a compound type constructed from any dependent type. */
23702 if (TYPE_PTRMEM_P (type))
23703 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23704 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23705 (type)));
23706 else if (TYPE_PTR_P (type)
23707 || TREE_CODE (type) == REFERENCE_TYPE)
23708 return dependent_type_p (TREE_TYPE (type));
23709 else if (TREE_CODE (type) == FUNCTION_TYPE
23710 || TREE_CODE (type) == METHOD_TYPE)
23712 tree arg_type;
23714 if (dependent_type_p (TREE_TYPE (type)))
23715 return true;
23716 for (arg_type = TYPE_ARG_TYPES (type);
23717 arg_type;
23718 arg_type = TREE_CHAIN (arg_type))
23719 if (dependent_type_p (TREE_VALUE (arg_type)))
23720 return true;
23721 if (cxx_dialect >= cxx1z)
23723 /* A value-dependent noexcept-specifier makes the type dependent. */
23724 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23725 if (spec && TREE_PURPOSE (spec)
23726 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23727 return true;
23729 return false;
23731 /* -- an array type constructed from any dependent type or whose
23732 size is specified by a constant expression that is
23733 value-dependent.
23735 We checked for type- and value-dependence of the bounds in
23736 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23737 if (TREE_CODE (type) == ARRAY_TYPE)
23739 if (TYPE_DOMAIN (type)
23740 && dependent_type_p (TYPE_DOMAIN (type)))
23741 return true;
23742 return dependent_type_p (TREE_TYPE (type));
23745 /* -- a template-id in which either the template name is a template
23746 parameter ... */
23747 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23748 return true;
23749 /* ... or any of the template arguments is a dependent type or
23750 an expression that is type-dependent or value-dependent. */
23751 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23752 && (any_dependent_template_arguments_p
23753 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23754 return true;
23756 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23757 dependent; if the argument of the `typeof' expression is not
23758 type-dependent, then it should already been have resolved. */
23759 if (TREE_CODE (type) == TYPEOF_TYPE
23760 || TREE_CODE (type) == DECLTYPE_TYPE
23761 || TREE_CODE (type) == UNDERLYING_TYPE)
23762 return true;
23764 /* A template argument pack is dependent if any of its packed
23765 arguments are. */
23766 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23768 tree args = ARGUMENT_PACK_ARGS (type);
23769 int i, len = TREE_VEC_LENGTH (args);
23770 for (i = 0; i < len; ++i)
23771 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23772 return true;
23775 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23776 be template parameters. */
23777 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23778 return true;
23780 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23781 return true;
23783 /* The standard does not specifically mention types that are local
23784 to template functions or local classes, but they should be
23785 considered dependent too. For example:
23787 template <int I> void f() {
23788 enum E { a = I };
23789 S<sizeof (E)> s;
23792 The size of `E' cannot be known until the value of `I' has been
23793 determined. Therefore, `E' must be considered dependent. */
23794 scope = TYPE_CONTEXT (type);
23795 if (scope && TYPE_P (scope))
23796 return dependent_type_p (scope);
23797 /* Don't use type_dependent_expression_p here, as it can lead
23798 to infinite recursion trying to determine whether a lambda
23799 nested in a lambda is dependent (c++/47687). */
23800 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23801 && DECL_LANG_SPECIFIC (scope)
23802 && DECL_TEMPLATE_INFO (scope)
23803 && (any_dependent_template_arguments_p
23804 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23805 return true;
23807 /* Other types are non-dependent. */
23808 return false;
23811 /* Returns TRUE if TYPE is dependent, in the sense of
23812 [temp.dep.type]. Note that a NULL type is considered dependent. */
23814 bool
23815 dependent_type_p (tree type)
23817 /* If there are no template parameters in scope, then there can't be
23818 any dependent types. */
23819 if (!processing_template_decl)
23821 /* If we are not processing a template, then nobody should be
23822 providing us with a dependent type. */
23823 gcc_assert (type);
23824 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23825 return false;
23828 /* If the type is NULL, we have not computed a type for the entity
23829 in question; in that case, the type is dependent. */
23830 if (!type)
23831 return true;
23833 /* Erroneous types can be considered non-dependent. */
23834 if (type == error_mark_node)
23835 return false;
23837 /* Getting here with global_type_node means we improperly called this
23838 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23839 gcc_checking_assert (type != global_type_node);
23841 /* If we have not already computed the appropriate value for TYPE,
23842 do so now. */
23843 if (!TYPE_DEPENDENT_P_VALID (type))
23845 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23846 TYPE_DEPENDENT_P_VALID (type) = 1;
23849 return TYPE_DEPENDENT_P (type);
23852 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23853 lookup. In other words, a dependent type that is not the current
23854 instantiation. */
23856 bool
23857 dependent_scope_p (tree scope)
23859 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23860 && !currently_open_class (scope));
23863 /* T is a SCOPE_REF; return whether we need to consider it
23864 instantiation-dependent so that we can check access at instantiation
23865 time even though we know which member it resolves to. */
23867 static bool
23868 instantiation_dependent_scope_ref_p (tree t)
23870 if (DECL_P (TREE_OPERAND (t, 1))
23871 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23872 && accessible_in_template_p (TREE_OPERAND (t, 0),
23873 TREE_OPERAND (t, 1)))
23874 return false;
23875 else
23876 return true;
23879 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23880 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23881 expression. */
23883 /* Note that this predicate is not appropriate for general expressions;
23884 only constant expressions (that satisfy potential_constant_expression)
23885 can be tested for value dependence. */
23887 bool
23888 value_dependent_expression_p (tree expression)
23890 if (!processing_template_decl || expression == NULL_TREE)
23891 return false;
23893 /* A name declared with a dependent type. */
23894 if (DECL_P (expression) && type_dependent_expression_p (expression))
23895 return true;
23897 switch (TREE_CODE (expression))
23899 case BASELINK:
23900 /* A dependent member function of the current instantiation. */
23901 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23903 case FUNCTION_DECL:
23904 /* A dependent member function of the current instantiation. */
23905 if (DECL_CLASS_SCOPE_P (expression)
23906 && dependent_type_p (DECL_CONTEXT (expression)))
23907 return true;
23908 break;
23910 case IDENTIFIER_NODE:
23911 /* A name that has not been looked up -- must be dependent. */
23912 return true;
23914 case TEMPLATE_PARM_INDEX:
23915 /* A non-type template parm. */
23916 return true;
23918 case CONST_DECL:
23919 /* A non-type template parm. */
23920 if (DECL_TEMPLATE_PARM_P (expression))
23921 return true;
23922 return value_dependent_expression_p (DECL_INITIAL (expression));
23924 case VAR_DECL:
23925 /* A constant with literal type and is initialized
23926 with an expression that is value-dependent.
23928 Note that a non-dependent parenthesized initializer will have
23929 already been replaced with its constant value, so if we see
23930 a TREE_LIST it must be dependent. */
23931 if (DECL_INITIAL (expression)
23932 && decl_constant_var_p (expression)
23933 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23934 /* cp_finish_decl doesn't fold reference initializers. */
23935 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23936 || type_dependent_expression_p (DECL_INITIAL (expression))
23937 || value_dependent_expression_p (DECL_INITIAL (expression))))
23938 return true;
23939 if (DECL_HAS_VALUE_EXPR_P (expression))
23941 tree value_expr = DECL_VALUE_EXPR (expression);
23942 if (type_dependent_expression_p (value_expr))
23943 return true;
23945 return false;
23947 case DYNAMIC_CAST_EXPR:
23948 case STATIC_CAST_EXPR:
23949 case CONST_CAST_EXPR:
23950 case REINTERPRET_CAST_EXPR:
23951 case CAST_EXPR:
23952 /* These expressions are value-dependent if the type to which
23953 the cast occurs is dependent or the expression being casted
23954 is value-dependent. */
23956 tree type = TREE_TYPE (expression);
23958 if (dependent_type_p (type))
23959 return true;
23961 /* A functional cast has a list of operands. */
23962 expression = TREE_OPERAND (expression, 0);
23963 if (!expression)
23965 /* If there are no operands, it must be an expression such
23966 as "int()". This should not happen for aggregate types
23967 because it would form non-constant expressions. */
23968 gcc_assert (cxx_dialect >= cxx11
23969 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23971 return false;
23974 if (TREE_CODE (expression) == TREE_LIST)
23975 return any_value_dependent_elements_p (expression);
23977 return value_dependent_expression_p (expression);
23980 case SIZEOF_EXPR:
23981 if (SIZEOF_EXPR_TYPE_P (expression))
23982 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23983 /* FALLTHRU */
23984 case ALIGNOF_EXPR:
23985 case TYPEID_EXPR:
23986 /* A `sizeof' expression is value-dependent if the operand is
23987 type-dependent or is a pack expansion. */
23988 expression = TREE_OPERAND (expression, 0);
23989 if (PACK_EXPANSION_P (expression))
23990 return true;
23991 else if (TYPE_P (expression))
23992 return dependent_type_p (expression);
23993 return instantiation_dependent_uneval_expression_p (expression);
23995 case AT_ENCODE_EXPR:
23996 /* An 'encode' expression is value-dependent if the operand is
23997 type-dependent. */
23998 expression = TREE_OPERAND (expression, 0);
23999 return dependent_type_p (expression);
24001 case NOEXCEPT_EXPR:
24002 expression = TREE_OPERAND (expression, 0);
24003 return instantiation_dependent_uneval_expression_p (expression);
24005 case SCOPE_REF:
24006 /* All instantiation-dependent expressions should also be considered
24007 value-dependent. */
24008 return instantiation_dependent_scope_ref_p (expression);
24010 case COMPONENT_REF:
24011 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24012 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24014 case NONTYPE_ARGUMENT_PACK:
24015 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24016 is value-dependent. */
24018 tree values = ARGUMENT_PACK_ARGS (expression);
24019 int i, len = TREE_VEC_LENGTH (values);
24021 for (i = 0; i < len; ++i)
24022 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24023 return true;
24025 return false;
24028 case TRAIT_EXPR:
24030 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24031 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
24032 || (type2 ? dependent_type_p (type2) : false));
24035 case MODOP_EXPR:
24036 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24037 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24039 case ARRAY_REF:
24040 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24041 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24043 case ADDR_EXPR:
24045 tree op = TREE_OPERAND (expression, 0);
24046 return (value_dependent_expression_p (op)
24047 || has_value_dependent_address (op));
24050 case REQUIRES_EXPR:
24051 /* Treat all requires-expressions as value-dependent so
24052 we don't try to fold them. */
24053 return true;
24055 case TYPE_REQ:
24056 return dependent_type_p (TREE_OPERAND (expression, 0));
24058 case CALL_EXPR:
24060 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24061 return true;
24062 tree fn = get_callee_fndecl (expression);
24063 int i, nargs;
24064 nargs = call_expr_nargs (expression);
24065 for (i = 0; i < nargs; ++i)
24067 tree op = CALL_EXPR_ARG (expression, i);
24068 /* In a call to a constexpr member function, look through the
24069 implicit ADDR_EXPR on the object argument so that it doesn't
24070 cause the call to be considered value-dependent. We also
24071 look through it in potential_constant_expression. */
24072 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24073 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24074 && TREE_CODE (op) == ADDR_EXPR)
24075 op = TREE_OPERAND (op, 0);
24076 if (value_dependent_expression_p (op))
24077 return true;
24079 return false;
24082 case TEMPLATE_ID_EXPR:
24083 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
24084 type-dependent. */
24085 return type_dependent_expression_p (expression)
24086 || variable_concept_p (TREE_OPERAND (expression, 0));
24088 case CONSTRUCTOR:
24090 unsigned ix;
24091 tree val;
24092 if (dependent_type_p (TREE_TYPE (expression)))
24093 return true;
24094 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24095 if (value_dependent_expression_p (val))
24096 return true;
24097 return false;
24100 case STMT_EXPR:
24101 /* Treat a GNU statement expression as dependent to avoid crashing
24102 under instantiate_non_dependent_expr; it can't be constant. */
24103 return true;
24105 default:
24106 /* A constant expression is value-dependent if any subexpression is
24107 value-dependent. */
24108 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24110 case tcc_reference:
24111 case tcc_unary:
24112 case tcc_comparison:
24113 case tcc_binary:
24114 case tcc_expression:
24115 case tcc_vl_exp:
24117 int i, len = cp_tree_operand_length (expression);
24119 for (i = 0; i < len; i++)
24121 tree t = TREE_OPERAND (expression, i);
24123 /* In some cases, some of the operands may be missing.
24124 (For example, in the case of PREDECREMENT_EXPR, the
24125 amount to increment by may be missing.) That doesn't
24126 make the expression dependent. */
24127 if (t && value_dependent_expression_p (t))
24128 return true;
24131 break;
24132 default:
24133 break;
24135 break;
24138 /* The expression is not value-dependent. */
24139 return false;
24142 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24143 [temp.dep.expr]. Note that an expression with no type is
24144 considered dependent. Other parts of the compiler arrange for an
24145 expression with type-dependent subexpressions to have no type, so
24146 this function doesn't have to be fully recursive. */
24148 bool
24149 type_dependent_expression_p (tree expression)
24151 if (!processing_template_decl)
24152 return false;
24154 if (expression == NULL_TREE || expression == error_mark_node)
24155 return false;
24157 /* An unresolved name is always dependent. */
24158 if (identifier_p (expression)
24159 || TREE_CODE (expression) == USING_DECL
24160 || TREE_CODE (expression) == WILDCARD_DECL)
24161 return true;
24163 /* A fold expression is type-dependent. */
24164 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24165 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24166 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24167 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24168 return true;
24170 /* Some expression forms are never type-dependent. */
24171 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24172 || TREE_CODE (expression) == SIZEOF_EXPR
24173 || TREE_CODE (expression) == ALIGNOF_EXPR
24174 || TREE_CODE (expression) == AT_ENCODE_EXPR
24175 || TREE_CODE (expression) == NOEXCEPT_EXPR
24176 || TREE_CODE (expression) == TRAIT_EXPR
24177 || TREE_CODE (expression) == TYPEID_EXPR
24178 || TREE_CODE (expression) == DELETE_EXPR
24179 || TREE_CODE (expression) == VEC_DELETE_EXPR
24180 || TREE_CODE (expression) == THROW_EXPR
24181 || TREE_CODE (expression) == REQUIRES_EXPR)
24182 return false;
24184 /* The types of these expressions depends only on the type to which
24185 the cast occurs. */
24186 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24187 || TREE_CODE (expression) == STATIC_CAST_EXPR
24188 || TREE_CODE (expression) == CONST_CAST_EXPR
24189 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24190 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24191 || TREE_CODE (expression) == CAST_EXPR)
24192 return dependent_type_p (TREE_TYPE (expression));
24194 /* The types of these expressions depends only on the type created
24195 by the expression. */
24196 if (TREE_CODE (expression) == NEW_EXPR
24197 || TREE_CODE (expression) == VEC_NEW_EXPR)
24199 /* For NEW_EXPR tree nodes created inside a template, either
24200 the object type itself or a TREE_LIST may appear as the
24201 operand 1. */
24202 tree type = TREE_OPERAND (expression, 1);
24203 if (TREE_CODE (type) == TREE_LIST)
24204 /* This is an array type. We need to check array dimensions
24205 as well. */
24206 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24207 || value_dependent_expression_p
24208 (TREE_OPERAND (TREE_VALUE (type), 1));
24209 else
24210 return dependent_type_p (type);
24213 if (TREE_CODE (expression) == SCOPE_REF)
24215 tree scope = TREE_OPERAND (expression, 0);
24216 tree name = TREE_OPERAND (expression, 1);
24218 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24219 contains an identifier associated by name lookup with one or more
24220 declarations declared with a dependent type, or...a
24221 nested-name-specifier or qualified-id that names a member of an
24222 unknown specialization. */
24223 return (type_dependent_expression_p (name)
24224 || dependent_scope_p (scope));
24227 if (TREE_CODE (expression) == TEMPLATE_DECL
24228 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24229 return uses_outer_template_parms (expression);
24231 if (TREE_CODE (expression) == STMT_EXPR)
24232 expression = stmt_expr_value_expr (expression);
24234 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24236 tree elt;
24237 unsigned i;
24239 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24241 if (type_dependent_expression_p (elt))
24242 return true;
24244 return false;
24247 /* A static data member of the current instantiation with incomplete
24248 array type is type-dependent, as the definition and specializations
24249 can have different bounds. */
24250 if (VAR_P (expression)
24251 && DECL_CLASS_SCOPE_P (expression)
24252 && dependent_type_p (DECL_CONTEXT (expression))
24253 && VAR_HAD_UNKNOWN_BOUND (expression))
24254 return true;
24256 /* An array of unknown bound depending on a variadic parameter, eg:
24258 template<typename... Args>
24259 void foo (Args... args)
24261 int arr[] = { args... };
24264 template<int... vals>
24265 void bar ()
24267 int arr[] = { vals... };
24270 If the array has no length and has an initializer, it must be that
24271 we couldn't determine its length in cp_complete_array_type because
24272 it is dependent. */
24273 if (VAR_P (expression)
24274 && TREE_TYPE (expression) != NULL_TREE
24275 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24276 && !TYPE_DOMAIN (TREE_TYPE (expression))
24277 && DECL_INITIAL (expression))
24278 return true;
24280 /* A function or variable template-id is type-dependent if it has any
24281 dependent template arguments. */
24282 if (VAR_OR_FUNCTION_DECL_P (expression)
24283 && DECL_LANG_SPECIFIC (expression)
24284 && DECL_TEMPLATE_INFO (expression))
24286 /* Consider the innermost template arguments, since those are the ones
24287 that come from the template-id; the template arguments for the
24288 enclosing class do not make it type-dependent unless they are used in
24289 the type of the decl. */
24290 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24291 && (any_dependent_template_arguments_p
24292 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24293 return true;
24295 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24296 type-dependent. Checking this is important for functions with auto
24297 return type, which looks like a dependent type. */
24298 if (TREE_CODE (expression) == FUNCTION_DECL
24299 && (!DECL_CLASS_SCOPE_P (expression)
24300 || !dependent_type_p (DECL_CONTEXT (expression)))
24301 && (!DECL_FRIEND_CONTEXT (expression)
24302 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression)))
24303 && !DECL_LOCAL_FUNCTION_P (expression))
24305 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24306 || undeduced_auto_decl (expression));
24307 return false;
24311 /* Always dependent, on the number of arguments if nothing else. */
24312 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24313 return true;
24315 if (TREE_TYPE (expression) == unknown_type_node)
24317 if (TREE_CODE (expression) == ADDR_EXPR)
24318 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24319 if (TREE_CODE (expression) == COMPONENT_REF
24320 || TREE_CODE (expression) == OFFSET_REF)
24322 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24323 return true;
24324 expression = TREE_OPERAND (expression, 1);
24325 if (identifier_p (expression))
24326 return false;
24328 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24329 if (TREE_CODE (expression) == SCOPE_REF)
24330 return false;
24332 if (BASELINK_P (expression))
24334 if (BASELINK_OPTYPE (expression)
24335 && dependent_type_p (BASELINK_OPTYPE (expression)))
24336 return true;
24337 expression = BASELINK_FUNCTIONS (expression);
24340 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24342 if (any_dependent_template_arguments_p
24343 (TREE_OPERAND (expression, 1)))
24344 return true;
24345 expression = TREE_OPERAND (expression, 0);
24346 if (identifier_p (expression))
24347 return true;
24350 gcc_assert (TREE_CODE (expression) == OVERLOAD
24351 || TREE_CODE (expression) == FUNCTION_DECL);
24353 for (lkp_iterator iter (expression); iter; ++iter)
24354 if (type_dependent_expression_p (*iter))
24355 return true;
24357 return false;
24360 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24362 /* Dependent type attributes might not have made it from the decl to
24363 the type yet. */
24364 if (DECL_P (expression)
24365 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24366 return true;
24368 return (dependent_type_p (TREE_TYPE (expression)));
24371 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24372 type-dependent if the expression refers to a member of the current
24373 instantiation and the type of the referenced member is dependent, or the
24374 class member access expression refers to a member of an unknown
24375 specialization.
24377 This function returns true if the OBJECT in such a class member access
24378 expression is of an unknown specialization. */
24380 bool
24381 type_dependent_object_expression_p (tree object)
24383 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24384 dependent. */
24385 if (TREE_CODE (object) == IDENTIFIER_NODE)
24386 return true;
24387 tree scope = TREE_TYPE (object);
24388 return (!scope || dependent_scope_p (scope));
24391 /* walk_tree callback function for instantiation_dependent_expression_p,
24392 below. Returns non-zero if a dependent subexpression is found. */
24394 static tree
24395 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24396 void * /*data*/)
24398 if (TYPE_P (*tp))
24400 /* We don't have to worry about decltype currently because decltype
24401 of an instantiation-dependent expr is a dependent type. This
24402 might change depending on the resolution of DR 1172. */
24403 *walk_subtrees = false;
24404 return NULL_TREE;
24406 enum tree_code code = TREE_CODE (*tp);
24407 switch (code)
24409 /* Don't treat an argument list as dependent just because it has no
24410 TREE_TYPE. */
24411 case TREE_LIST:
24412 case TREE_VEC:
24413 return NULL_TREE;
24415 case TEMPLATE_PARM_INDEX:
24416 return *tp;
24418 /* Handle expressions with type operands. */
24419 case SIZEOF_EXPR:
24420 case ALIGNOF_EXPR:
24421 case TYPEID_EXPR:
24422 case AT_ENCODE_EXPR:
24424 tree op = TREE_OPERAND (*tp, 0);
24425 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24426 op = TREE_TYPE (op);
24427 if (TYPE_P (op))
24429 if (dependent_type_p (op))
24430 return *tp;
24431 else
24433 *walk_subtrees = false;
24434 return NULL_TREE;
24437 break;
24440 case COMPONENT_REF:
24441 if (identifier_p (TREE_OPERAND (*tp, 1)))
24442 /* In a template, finish_class_member_access_expr creates a
24443 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24444 type-dependent, so that we can check access control at
24445 instantiation time (PR 42277). See also Core issue 1273. */
24446 return *tp;
24447 break;
24449 case SCOPE_REF:
24450 if (instantiation_dependent_scope_ref_p (*tp))
24451 return *tp;
24452 else
24453 break;
24455 /* Treat statement-expressions as dependent. */
24456 case BIND_EXPR:
24457 return *tp;
24459 /* Treat requires-expressions as dependent. */
24460 case REQUIRES_EXPR:
24461 return *tp;
24463 case CALL_EXPR:
24464 /* Treat calls to function concepts as dependent. */
24465 if (function_concept_check_p (*tp))
24466 return *tp;
24467 break;
24469 case TEMPLATE_ID_EXPR:
24470 /* And variable concepts. */
24471 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24472 return *tp;
24473 break;
24475 default:
24476 break;
24479 if (type_dependent_expression_p (*tp))
24480 return *tp;
24481 else
24482 return NULL_TREE;
24485 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24486 sense defined by the ABI:
24488 "An expression is instantiation-dependent if it is type-dependent
24489 or value-dependent, or it has a subexpression that is type-dependent
24490 or value-dependent."
24492 Except don't actually check value-dependence for unevaluated expressions,
24493 because in sizeof(i) we don't care about the value of i. Checking
24494 type-dependence will in turn check value-dependence of array bounds/template
24495 arguments as needed. */
24497 bool
24498 instantiation_dependent_uneval_expression_p (tree expression)
24500 tree result;
24502 if (!processing_template_decl)
24503 return false;
24505 if (expression == error_mark_node)
24506 return false;
24508 result = cp_walk_tree_without_duplicates (&expression,
24509 instantiation_dependent_r, NULL);
24510 return result != NULL_TREE;
24513 /* As above, but also check value-dependence of the expression as a whole. */
24515 bool
24516 instantiation_dependent_expression_p (tree expression)
24518 return (instantiation_dependent_uneval_expression_p (expression)
24519 || value_dependent_expression_p (expression));
24522 /* Like type_dependent_expression_p, but it also works while not processing
24523 a template definition, i.e. during substitution or mangling. */
24525 bool
24526 type_dependent_expression_p_push (tree expr)
24528 bool b;
24529 ++processing_template_decl;
24530 b = type_dependent_expression_p (expr);
24531 --processing_template_decl;
24532 return b;
24535 /* Returns TRUE if ARGS contains a type-dependent expression. */
24537 bool
24538 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24540 unsigned int i;
24541 tree arg;
24543 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24545 if (type_dependent_expression_p (arg))
24546 return true;
24548 return false;
24551 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24552 expressions) contains any type-dependent expressions. */
24554 bool
24555 any_type_dependent_elements_p (const_tree list)
24557 for (; list; list = TREE_CHAIN (list))
24558 if (type_dependent_expression_p (TREE_VALUE (list)))
24559 return true;
24561 return false;
24564 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24565 expressions) contains any value-dependent expressions. */
24567 bool
24568 any_value_dependent_elements_p (const_tree list)
24570 for (; list; list = TREE_CHAIN (list))
24571 if (value_dependent_expression_p (TREE_VALUE (list)))
24572 return true;
24574 return false;
24577 /* Returns TRUE if the ARG (a template argument) is dependent. */
24579 bool
24580 dependent_template_arg_p (tree arg)
24582 if (!processing_template_decl)
24583 return false;
24585 /* Assume a template argument that was wrongly written by the user
24586 is dependent. This is consistent with what
24587 any_dependent_template_arguments_p [that calls this function]
24588 does. */
24589 if (!arg || arg == error_mark_node)
24590 return true;
24592 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24593 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24595 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24596 return true;
24597 if (TREE_CODE (arg) == TEMPLATE_DECL)
24599 if (DECL_TEMPLATE_PARM_P (arg))
24600 return true;
24601 /* A member template of a dependent class is not necessarily
24602 type-dependent, but it is a dependent template argument because it
24603 will be a member of an unknown specialization to that template. */
24604 tree scope = CP_DECL_CONTEXT (arg);
24605 return TYPE_P (scope) && dependent_type_p (scope);
24607 else if (ARGUMENT_PACK_P (arg))
24609 tree args = ARGUMENT_PACK_ARGS (arg);
24610 int i, len = TREE_VEC_LENGTH (args);
24611 for (i = 0; i < len; ++i)
24613 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24614 return true;
24617 return false;
24619 else if (TYPE_P (arg))
24620 return dependent_type_p (arg);
24621 else
24622 return (type_dependent_expression_p (arg)
24623 || value_dependent_expression_p (arg));
24626 /* Returns true if ARGS (a collection of template arguments) contains
24627 any types that require structural equality testing. */
24629 bool
24630 any_template_arguments_need_structural_equality_p (tree args)
24632 int i;
24633 int j;
24635 if (!args)
24636 return false;
24637 if (args == error_mark_node)
24638 return true;
24640 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24642 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24643 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24645 tree arg = TREE_VEC_ELT (level, j);
24646 tree packed_args = NULL_TREE;
24647 int k, len = 1;
24649 if (ARGUMENT_PACK_P (arg))
24651 /* Look inside the argument pack. */
24652 packed_args = ARGUMENT_PACK_ARGS (arg);
24653 len = TREE_VEC_LENGTH (packed_args);
24656 for (k = 0; k < len; ++k)
24658 if (packed_args)
24659 arg = TREE_VEC_ELT (packed_args, k);
24661 if (error_operand_p (arg))
24662 return true;
24663 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24664 continue;
24665 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24666 return true;
24667 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24668 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24669 return true;
24674 return false;
24677 /* Returns true if ARGS (a collection of template arguments) contains
24678 any dependent arguments. */
24680 bool
24681 any_dependent_template_arguments_p (const_tree args)
24683 int i;
24684 int j;
24686 if (!args)
24687 return false;
24688 if (args == error_mark_node)
24689 return true;
24691 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24693 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24694 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24695 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24696 return true;
24699 return false;
24702 /* Returns TRUE if the template TMPL is type-dependent. */
24704 bool
24705 dependent_template_p (tree tmpl)
24707 if (TREE_CODE (tmpl) == OVERLOAD)
24709 for (lkp_iterator iter (tmpl); iter; ++iter)
24710 if (dependent_template_p (*iter))
24711 return true;
24712 return false;
24715 /* Template template parameters are dependent. */
24716 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24717 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24718 return true;
24719 /* So are names that have not been looked up. */
24720 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24721 return true;
24722 return false;
24725 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24727 bool
24728 dependent_template_id_p (tree tmpl, tree args)
24730 return (dependent_template_p (tmpl)
24731 || any_dependent_template_arguments_p (args));
24734 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24735 are dependent. */
24737 bool
24738 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24740 int i;
24742 if (!processing_template_decl)
24743 return false;
24745 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24747 tree decl = TREE_VEC_ELT (declv, i);
24748 tree init = TREE_VEC_ELT (initv, i);
24749 tree cond = TREE_VEC_ELT (condv, i);
24750 tree incr = TREE_VEC_ELT (incrv, i);
24752 if (type_dependent_expression_p (decl)
24753 || TREE_CODE (decl) == SCOPE_REF)
24754 return true;
24756 if (init && type_dependent_expression_p (init))
24757 return true;
24759 if (type_dependent_expression_p (cond))
24760 return true;
24762 if (COMPARISON_CLASS_P (cond)
24763 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24764 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24765 return true;
24767 if (TREE_CODE (incr) == MODOP_EXPR)
24769 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24770 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24771 return true;
24773 else if (type_dependent_expression_p (incr))
24774 return true;
24775 else if (TREE_CODE (incr) == MODIFY_EXPR)
24777 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24778 return true;
24779 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24781 tree t = TREE_OPERAND (incr, 1);
24782 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24783 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24784 return true;
24789 return false;
24792 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24793 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24794 no such TYPE can be found. Note that this function peers inside
24795 uninstantiated templates and therefore should be used only in
24796 extremely limited situations. ONLY_CURRENT_P restricts this
24797 peering to the currently open classes hierarchy (which is required
24798 when comparing types). */
24800 tree
24801 resolve_typename_type (tree type, bool only_current_p)
24803 tree scope;
24804 tree name;
24805 tree decl;
24806 int quals;
24807 tree pushed_scope;
24808 tree result;
24810 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24812 scope = TYPE_CONTEXT (type);
24813 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24814 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24815 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24816 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24817 identifier of the TYPENAME_TYPE anymore.
24818 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24819 TYPENAME_TYPE instead, we avoid messing up with a possible
24820 typedef variant case. */
24821 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24823 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24824 it first before we can figure out what NAME refers to. */
24825 if (TREE_CODE (scope) == TYPENAME_TYPE)
24827 if (TYPENAME_IS_RESOLVING_P (scope))
24828 /* Given a class template A with a dependent base with nested type C,
24829 typedef typename A::C::C C will land us here, as trying to resolve
24830 the initial A::C leads to the local C typedef, which leads back to
24831 A::C::C. So we break the recursion now. */
24832 return type;
24833 else
24834 scope = resolve_typename_type (scope, only_current_p);
24836 /* If we don't know what SCOPE refers to, then we cannot resolve the
24837 TYPENAME_TYPE. */
24838 if (!CLASS_TYPE_P (scope))
24839 return type;
24840 /* If this is a typedef, we don't want to look inside (c++/11987). */
24841 if (typedef_variant_p (type))
24842 return type;
24843 /* If SCOPE isn't the template itself, it will not have a valid
24844 TYPE_FIELDS list. */
24845 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24846 /* scope is either the template itself or a compatible instantiation
24847 like X<T>, so look up the name in the original template. */
24848 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24849 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24850 gcc_checking_assert (uses_template_parms (scope));
24851 /* If scope has no fields, it can't be a current instantiation. Check this
24852 before currently_open_class to avoid infinite recursion (71515). */
24853 if (!TYPE_FIELDS (scope))
24854 return type;
24855 /* If the SCOPE is not the current instantiation, there's no reason
24856 to look inside it. */
24857 if (only_current_p && !currently_open_class (scope))
24858 return type;
24859 /* Enter the SCOPE so that name lookup will be resolved as if we
24860 were in the class definition. In particular, SCOPE will no
24861 longer be considered a dependent type. */
24862 pushed_scope = push_scope (scope);
24863 /* Look up the declaration. */
24864 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24865 tf_warning_or_error);
24867 result = NULL_TREE;
24869 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24870 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24871 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24872 if (!decl)
24873 /*nop*/;
24874 else if (identifier_p (fullname)
24875 && TREE_CODE (decl) == TYPE_DECL)
24877 result = TREE_TYPE (decl);
24878 if (result == error_mark_node)
24879 result = NULL_TREE;
24881 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24882 && DECL_CLASS_TEMPLATE_P (decl))
24884 /* Obtain the template and the arguments. */
24885 tree tmpl = TREE_OPERAND (fullname, 0);
24886 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24888 /* We get here with a plain identifier because a previous tentative
24889 parse of the nested-name-specifier as part of a ptr-operator saw
24890 ::template X<A>. The use of ::template is necessary in a
24891 ptr-operator, but wrong in a declarator-id.
24893 [temp.names]: In a qualified-id of a declarator-id, the keyword
24894 template shall not appear at the top level. */
24895 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24896 "keyword %<template%> not allowed in declarator-id");
24897 tmpl = decl;
24899 tree args = TREE_OPERAND (fullname, 1);
24900 /* Instantiate the template. */
24901 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24902 /*entering_scope=*/true,
24903 tf_error | tf_user);
24904 if (result == error_mark_node)
24905 result = NULL_TREE;
24908 /* Leave the SCOPE. */
24909 if (pushed_scope)
24910 pop_scope (pushed_scope);
24912 /* If we failed to resolve it, return the original typename. */
24913 if (!result)
24914 return type;
24916 /* If lookup found a typename type, resolve that too. */
24917 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24919 /* Ill-formed programs can cause infinite recursion here, so we
24920 must catch that. */
24921 TYPENAME_IS_RESOLVING_P (result) = 1;
24922 result = resolve_typename_type (result, only_current_p);
24923 TYPENAME_IS_RESOLVING_P (result) = 0;
24926 /* Qualify the resulting type. */
24927 quals = cp_type_quals (type);
24928 if (quals)
24929 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24931 return result;
24934 /* EXPR is an expression which is not type-dependent. Return a proxy
24935 for EXPR that can be used to compute the types of larger
24936 expressions containing EXPR. */
24938 tree
24939 build_non_dependent_expr (tree expr)
24941 tree inner_expr;
24943 /* When checking, try to get a constant value for all non-dependent
24944 expressions in order to expose bugs in *_dependent_expression_p
24945 and constexpr. This can affect code generation, see PR70704, so
24946 only do this for -fchecking=2. */
24947 if (flag_checking > 1
24948 && cxx_dialect >= cxx11
24949 /* Don't do this during nsdmi parsing as it can lead to
24950 unexpected recursive instantiations. */
24951 && !parsing_nsdmi ()
24952 /* Don't do this during concept expansion either and for
24953 the same reason. */
24954 && !expanding_concept ())
24955 fold_non_dependent_expr (expr);
24957 /* Preserve OVERLOADs; the functions must be available to resolve
24958 types. */
24959 inner_expr = expr;
24960 if (TREE_CODE (inner_expr) == STMT_EXPR)
24961 inner_expr = stmt_expr_value_expr (inner_expr);
24962 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24963 inner_expr = TREE_OPERAND (inner_expr, 0);
24964 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24965 inner_expr = TREE_OPERAND (inner_expr, 1);
24966 if (is_overloaded_fn (inner_expr)
24967 || TREE_CODE (inner_expr) == OFFSET_REF)
24968 return expr;
24969 /* There is no need to return a proxy for a variable. */
24970 if (VAR_P (expr))
24971 return expr;
24972 /* Preserve string constants; conversions from string constants to
24973 "char *" are allowed, even though normally a "const char *"
24974 cannot be used to initialize a "char *". */
24975 if (TREE_CODE (expr) == STRING_CST)
24976 return expr;
24977 /* Preserve void and arithmetic constants, as an optimization -- there is no
24978 reason to create a new node. */
24979 if (TREE_CODE (expr) == VOID_CST
24980 || TREE_CODE (expr) == INTEGER_CST
24981 || TREE_CODE (expr) == REAL_CST)
24982 return expr;
24983 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24984 There is at least one place where we want to know that a
24985 particular expression is a throw-expression: when checking a ?:
24986 expression, there are special rules if the second or third
24987 argument is a throw-expression. */
24988 if (TREE_CODE (expr) == THROW_EXPR)
24989 return expr;
24991 /* Don't wrap an initializer list, we need to be able to look inside. */
24992 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24993 return expr;
24995 /* Don't wrap a dummy object, we need to be able to test for it. */
24996 if (is_dummy_object (expr))
24997 return expr;
24999 if (TREE_CODE (expr) == COND_EXPR)
25000 return build3 (COND_EXPR,
25001 TREE_TYPE (expr),
25002 TREE_OPERAND (expr, 0),
25003 (TREE_OPERAND (expr, 1)
25004 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25005 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25006 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25007 if (TREE_CODE (expr) == COMPOUND_EXPR
25008 && !COMPOUND_EXPR_OVERLOADED (expr))
25009 return build2 (COMPOUND_EXPR,
25010 TREE_TYPE (expr),
25011 TREE_OPERAND (expr, 0),
25012 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25014 /* If the type is unknown, it can't really be non-dependent */
25015 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25017 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25018 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25021 /* ARGS is a vector of expressions as arguments to a function call.
25022 Replace the arguments with equivalent non-dependent expressions.
25023 This modifies ARGS in place. */
25025 void
25026 make_args_non_dependent (vec<tree, va_gc> *args)
25028 unsigned int ix;
25029 tree arg;
25031 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25033 tree newarg = build_non_dependent_expr (arg);
25034 if (newarg != arg)
25035 (*args)[ix] = newarg;
25039 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25040 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25041 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25043 static tree
25044 make_auto_1 (tree name, bool set_canonical)
25046 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25047 TYPE_NAME (au) = build_decl (input_location,
25048 TYPE_DECL, name, au);
25049 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25050 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25051 (0, processing_template_decl + 1, processing_template_decl + 1,
25052 TYPE_NAME (au), NULL_TREE);
25053 if (set_canonical)
25054 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25055 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25056 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25058 return au;
25061 tree
25062 make_decltype_auto (void)
25064 return make_auto_1 (decltype_auto_identifier, true);
25067 tree
25068 make_auto (void)
25070 return make_auto_1 (auto_identifier, true);
25073 /* Return a C++17 deduction placeholder for class template TMPL. */
25075 tree
25076 make_template_placeholder (tree tmpl)
25078 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25079 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25080 return t;
25083 /* True iff T is a C++17 class template deduction placeholder. */
25085 bool
25086 template_placeholder_p (tree t)
25088 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25091 /* Make a "constrained auto" type-specifier. This is an
25092 auto type with constraints that must be associated after
25093 deduction. The constraint is formed from the given
25094 CONC and its optional sequence of arguments, which are
25095 non-null if written as partial-concept-id. */
25097 tree
25098 make_constrained_auto (tree con, tree args)
25100 tree type = make_auto_1 (auto_identifier, false);
25102 /* Build the constraint. */
25103 tree tmpl = DECL_TI_TEMPLATE (con);
25104 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25105 expr = build_concept_check (expr, type, args);
25107 tree constr = normalize_expression (expr);
25108 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25110 /* Our canonical type depends on the constraint. */
25111 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25113 /* Attach the constraint to the type declaration. */
25114 tree decl = TYPE_NAME (type);
25115 return decl;
25118 /* Given type ARG, return std::initializer_list<ARG>. */
25120 static tree
25121 listify (tree arg)
25123 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25125 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25127 gcc_rich_location richloc (input_location);
25128 maybe_add_include_fixit (&richloc, "<initializer_list>");
25129 error_at_rich_loc (&richloc,
25130 "deducing from brace-enclosed initializer list"
25131 " requires #include <initializer_list>");
25133 return error_mark_node;
25135 tree argvec = make_tree_vec (1);
25136 TREE_VEC_ELT (argvec, 0) = arg;
25138 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25139 NULL_TREE, 0, tf_warning_or_error);
25142 /* Replace auto in TYPE with std::initializer_list<auto>. */
25144 static tree
25145 listify_autos (tree type, tree auto_node)
25147 tree init_auto = listify (auto_node);
25148 tree argvec = make_tree_vec (1);
25149 TREE_VEC_ELT (argvec, 0) = init_auto;
25150 if (processing_template_decl)
25151 argvec = add_to_template_args (current_template_args (), argvec);
25152 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25155 /* Hash traits for hashing possibly constrained 'auto'
25156 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25158 struct auto_hash : default_hash_traits<tree>
25160 static inline hashval_t hash (tree);
25161 static inline bool equal (tree, tree);
25164 /* Hash the 'auto' T. */
25166 inline hashval_t
25167 auto_hash::hash (tree t)
25169 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25170 /* Matching constrained-type-specifiers denote the same template
25171 parameter, so hash the constraint. */
25172 return hash_placeholder_constraint (c);
25173 else
25174 /* But unconstrained autos are all separate, so just hash the pointer. */
25175 return iterative_hash_object (t, 0);
25178 /* Compare two 'auto's. */
25180 inline bool
25181 auto_hash::equal (tree t1, tree t2)
25183 if (t1 == t2)
25184 return true;
25186 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25187 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25189 /* Two unconstrained autos are distinct. */
25190 if (!c1 || !c2)
25191 return false;
25193 return equivalent_placeholder_constraints (c1, c2);
25196 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25197 constrained) auto, add it to the vector. */
25199 static int
25200 extract_autos_r (tree t, void *data)
25202 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25203 if (is_auto (t))
25205 /* All the autos were built with index 0; fix that up now. */
25206 tree *p = hash.find_slot (t, INSERT);
25207 unsigned idx;
25208 if (*p)
25209 /* If this is a repeated constrained-type-specifier, use the index we
25210 chose before. */
25211 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25212 else
25214 /* Otherwise this is new, so use the current count. */
25215 *p = t;
25216 idx = hash.elements () - 1;
25218 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25221 /* Always keep walking. */
25222 return 0;
25225 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25226 says they can appear anywhere in the type. */
25228 static tree
25229 extract_autos (tree type)
25231 hash_set<tree> visited;
25232 hash_table<auto_hash> hash (2);
25234 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25236 tree tree_vec = make_tree_vec (hash.elements());
25237 for (hash_table<auto_hash>::iterator iter = hash.begin();
25238 iter != hash.end(); ++iter)
25240 tree elt = *iter;
25241 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25242 TREE_VEC_ELT (tree_vec, i)
25243 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25246 return tree_vec;
25249 /* The stem for deduction guide names. */
25250 const char *const dguide_base = "__dguide_";
25252 /* Return the name for a deduction guide for class template TMPL. */
25254 tree
25255 dguide_name (tree tmpl)
25257 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25258 tree tname = TYPE_IDENTIFIER (type);
25259 char *buf = (char *) alloca (1 + strlen (dguide_base)
25260 + IDENTIFIER_LENGTH (tname));
25261 memcpy (buf, dguide_base, strlen (dguide_base));
25262 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25263 IDENTIFIER_LENGTH (tname) + 1);
25264 tree dname = get_identifier (buf);
25265 TREE_TYPE (dname) = type;
25266 return dname;
25269 /* True if NAME is the name of a deduction guide. */
25271 bool
25272 dguide_name_p (tree name)
25274 return (TREE_TYPE (name)
25275 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25276 strlen (dguide_base)));
25279 /* True if FN is a deduction guide. */
25281 bool
25282 deduction_guide_p (const_tree fn)
25284 if (DECL_P (fn))
25285 if (tree name = DECL_NAME (fn))
25286 return dguide_name_p (name);
25287 return false;
25290 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25292 bool
25293 copy_guide_p (const_tree fn)
25295 gcc_assert (deduction_guide_p (fn));
25296 if (!DECL_ARTIFICIAL (fn))
25297 return false;
25298 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25299 return (TREE_CHAIN (parms) == void_list_node
25300 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25303 /* True if FN is a guide generated from a constructor template. */
25305 bool
25306 template_guide_p (const_tree fn)
25308 gcc_assert (deduction_guide_p (fn));
25309 if (!DECL_ARTIFICIAL (fn))
25310 return false;
25311 tree tmpl = DECL_TI_TEMPLATE (fn);
25312 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25313 return PRIMARY_TEMPLATE_P (org);
25314 return false;
25317 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25318 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25319 template parameter types. Note that the handling of template template
25320 parameters relies on current_template_parms being set appropriately for the
25321 new template. */
25323 static tree
25324 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25325 tree tsubst_args, tsubst_flags_t complain)
25327 tree oldidx = get_template_parm_index (olddecl);
25329 tree newtype;
25330 if (TREE_CODE (olddecl) == TYPE_DECL
25331 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25333 tree oldtype = TREE_TYPE (olddecl);
25334 newtype = cxx_make_type (TREE_CODE (oldtype));
25335 TYPE_MAIN_VARIANT (newtype) = newtype;
25336 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25337 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25338 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25340 else
25341 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25342 complain, NULL_TREE);
25344 tree newdecl
25345 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25346 DECL_NAME (olddecl), newtype);
25347 SET_DECL_TEMPLATE_PARM_P (newdecl);
25349 tree newidx;
25350 if (TREE_CODE (olddecl) == TYPE_DECL
25351 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25353 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25354 = build_template_parm_index (index, level, level,
25355 newdecl, newtype);
25356 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25357 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25358 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25359 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25361 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25363 DECL_TEMPLATE_RESULT (newdecl)
25364 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25365 DECL_NAME (olddecl), newtype);
25366 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25367 // First create a copy (ttargs) of tsubst_args with an
25368 // additional level for the template template parameter's own
25369 // template parameters (ttparms).
25370 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25371 (DECL_TEMPLATE_PARMS (olddecl)));
25372 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25373 tree ttargs = make_tree_vec (depth + 1);
25374 for (int i = 0; i < depth; ++i)
25375 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25376 TREE_VEC_ELT (ttargs, depth)
25377 = template_parms_level_to_args (ttparms);
25378 // Substitute ttargs into ttparms to fix references to
25379 // other template parameters.
25380 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25381 complain);
25382 // Now substitute again with args based on tparms, to reduce
25383 // the level of the ttparms.
25384 ttargs = current_template_args ();
25385 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25386 complain);
25387 // Finally, tack the adjusted parms onto tparms.
25388 ttparms = tree_cons (size_int (depth), ttparms,
25389 current_template_parms);
25390 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25393 else
25395 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25396 tree newconst
25397 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25398 TREE_CODE (oldconst),
25399 DECL_NAME (oldconst), newtype);
25400 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25401 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25402 SET_DECL_TEMPLATE_PARM_P (newconst);
25403 newidx = build_template_parm_index (index, level, level,
25404 newconst, newtype);
25405 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25406 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25407 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25410 return newdecl;
25413 /* Returns a C++17 class deduction guide template based on the constructor
25414 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25415 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25417 static tree
25418 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25420 tree type, tparms, targs, fparms, fargs, ci;
25421 bool memtmpl = false;
25422 bool explicit_p;
25423 location_t loc;
25424 tree fn_tmpl = NULL_TREE;
25426 if (TYPE_P (ctor))
25428 type = ctor;
25429 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25430 if (copy_p)
25432 type = TREE_TYPE (type);
25433 fparms = tree_cons (NULL_TREE, type, void_list_node);
25435 else
25436 fparms = void_list_node;
25438 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25439 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25440 targs = CLASSTYPE_TI_ARGS (type);
25441 ci = NULL_TREE;
25442 fargs = NULL_TREE;
25443 loc = DECL_SOURCE_LOCATION (ctmpl);
25444 explicit_p = false;
25446 else
25448 ++processing_template_decl;
25450 fn_tmpl
25451 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25452 : DECL_TI_TEMPLATE (ctor));
25453 if (outer_args)
25454 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25455 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25457 type = DECL_CONTEXT (ctor);
25459 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25460 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25461 fully specialized args for the enclosing class. Strip those off, as
25462 the deduction guide won't have those template parameters. */
25463 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25464 TMPL_PARMS_DEPTH (tparms));
25465 /* Discard the 'this' parameter. */
25466 fparms = FUNCTION_ARG_CHAIN (ctor);
25467 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25468 ci = get_constraints (ctor);
25469 loc = DECL_SOURCE_LOCATION (ctor);
25470 explicit_p = DECL_NONCONVERTING_P (ctor);
25472 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25474 memtmpl = true;
25476 /* For a member template constructor, we need to flatten the two
25477 template parameter lists into one, and then adjust the function
25478 signature accordingly. This gets...complicated. */
25479 tree save_parms = current_template_parms;
25481 /* For a member template we should have two levels of parms/args, one
25482 for the class and one for the constructor. We stripped
25483 specialized args for further enclosing classes above. */
25484 const int depth = 2;
25485 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25487 /* Template args for translating references to the two-level template
25488 parameters into references to the one-level template parameters we
25489 are creating. */
25490 tree tsubst_args = copy_node (targs);
25491 TMPL_ARGS_LEVEL (tsubst_args, depth)
25492 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25494 /* Template parms for the constructor template. */
25495 tree ftparms = TREE_VALUE (tparms);
25496 unsigned flen = TREE_VEC_LENGTH (ftparms);
25497 /* Template parms for the class template. */
25498 tparms = TREE_CHAIN (tparms);
25499 tree ctparms = TREE_VALUE (tparms);
25500 unsigned clen = TREE_VEC_LENGTH (ctparms);
25501 /* Template parms for the deduction guide start as a copy of the
25502 template parms for the class. We set current_template_parms for
25503 lookup_template_class_1. */
25504 current_template_parms = tparms = copy_node (tparms);
25505 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25506 for (unsigned i = 0; i < clen; ++i)
25507 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25509 /* Now we need to rewrite the constructor parms to append them to the
25510 class parms. */
25511 for (unsigned i = 0; i < flen; ++i)
25513 unsigned index = i + clen;
25514 unsigned level = 1;
25515 tree oldelt = TREE_VEC_ELT (ftparms, i);
25516 tree olddecl = TREE_VALUE (oldelt);
25517 tree newdecl = rewrite_template_parm (olddecl, index, level,
25518 tsubst_args, complain);
25519 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25520 tsubst_args, complain, ctor);
25521 tree list = build_tree_list (newdef, newdecl);
25522 TEMPLATE_PARM_CONSTRAINTS (list)
25523 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25524 tsubst_args, complain, ctor);
25525 TREE_VEC_ELT (new_vec, index) = list;
25526 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25529 /* Now we have a final set of template parms to substitute into the
25530 function signature. */
25531 targs = template_parms_to_args (tparms);
25532 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25533 complain, ctor);
25534 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25535 if (ci)
25536 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25538 current_template_parms = save_parms;
25540 --processing_template_decl;
25543 if (!memtmpl)
25545 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25546 tparms = copy_node (tparms);
25547 INNERMOST_TEMPLATE_PARMS (tparms)
25548 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25551 tree fntype = build_function_type (type, fparms);
25552 tree ded_fn = build_lang_decl_loc (loc,
25553 FUNCTION_DECL,
25554 dguide_name (type), fntype);
25555 DECL_ARGUMENTS (ded_fn) = fargs;
25556 DECL_ARTIFICIAL (ded_fn) = true;
25557 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25558 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25559 DECL_ARTIFICIAL (ded_tmpl) = true;
25560 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25561 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25562 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25563 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25564 if (DECL_P (ctor))
25565 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25566 if (ci)
25567 set_constraints (ded_tmpl, ci);
25569 return ded_tmpl;
25572 /* Deduce template arguments for the class template placeholder PTYPE for
25573 template TMPL based on the initializer INIT, and return the resulting
25574 type. */
25576 static tree
25577 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25578 tsubst_flags_t complain)
25580 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25582 /* We should have handled this in the caller. */
25583 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25584 return ptype;
25585 if (complain & tf_error)
25586 error ("non-class template %qT used without template arguments", tmpl);
25587 return error_mark_node;
25590 tree type = TREE_TYPE (tmpl);
25592 bool try_list_ctor = false;
25594 vec<tree,va_gc> *args;
25595 if (init == NULL_TREE
25596 || TREE_CODE (init) == TREE_LIST)
25597 args = make_tree_vector_from_list (init);
25598 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25600 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25601 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25603 /* As an exception, the first phase in 16.3.1.7 (considering the
25604 initializer list as a single argument) is omitted if the
25605 initializer list consists of a single expression of type cv U,
25606 where U is a specialization of C or a class derived from a
25607 specialization of C. */
25608 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25609 tree etype = TREE_TYPE (elt);
25611 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25612 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25613 int err = unify (tparms, targs, type, etype,
25614 UNIFY_ALLOW_DERIVED, /*explain*/false);
25615 if (err == 0)
25616 try_list_ctor = false;
25617 ggc_free (targs);
25619 if (try_list_ctor || is_std_init_list (type))
25620 args = make_tree_vector_single (init);
25621 else
25622 args = make_tree_vector_from_ctor (init);
25624 else
25625 args = make_tree_vector_single (init);
25627 tree dname = dguide_name (tmpl);
25628 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25629 /*type*/false, /*complain*/false,
25630 /*hidden*/false);
25631 bool elided = false;
25632 if (cands == error_mark_node)
25633 cands = NULL_TREE;
25635 /* Prune explicit deduction guides in copy-initialization context. */
25636 if (flags & LOOKUP_ONLYCONVERTING)
25638 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25639 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25640 elided = true;
25642 if (elided)
25644 /* Found a nonconverting guide, prune the candidates. */
25645 tree pruned = NULL_TREE;
25646 for (lkp_iterator iter (cands); iter; ++iter)
25647 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25648 pruned = lookup_add (*iter, pruned);
25650 cands = pruned;
25654 tree outer_args = NULL_TREE;
25655 if (DECL_CLASS_SCOPE_P (tmpl)
25656 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25658 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25659 type = TREE_TYPE (most_general_template (tmpl));
25662 bool saw_ctor = false;
25663 if (CLASSTYPE_METHOD_VEC (type))
25664 // FIXME cache artificial deduction guides
25665 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25666 iter; ++iter)
25668 tree guide = build_deduction_guide (*iter, outer_args, complain);
25669 if ((flags & LOOKUP_ONLYCONVERTING)
25670 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25671 elided = true;
25672 else
25673 cands = lookup_add (guide, cands);
25675 saw_ctor = true;
25678 tree call = error_mark_node;
25680 /* If this is list-initialization and the class has a list constructor, first
25681 try deducing from the list as a single argument, as [over.match.list]. */
25682 tree list_cands = NULL_TREE;
25683 if (try_list_ctor && cands)
25684 for (lkp_iterator iter (cands); iter; ++iter)
25686 tree dg = *iter;
25687 if (is_list_ctor (dg))
25688 list_cands = lookup_add (dg, list_cands);
25690 if (list_cands)
25692 ++cp_unevaluated_operand;
25693 call = build_new_function_call (list_cands, &args, tf_decltype);
25694 --cp_unevaluated_operand;
25696 if (call == error_mark_node)
25698 /* That didn't work, now try treating the list as a sequence of
25699 arguments. */
25700 release_tree_vector (args);
25701 args = make_tree_vector_from_ctor (init);
25705 /* Maybe generate an implicit deduction guide. */
25706 if (call == error_mark_node && args->length () < 2)
25708 tree gtype = NULL_TREE;
25710 if (args->length () == 1)
25711 /* Generate a copy guide. */
25712 gtype = build_reference_type (type);
25713 else if (!saw_ctor)
25714 /* Generate a default guide. */
25715 gtype = type;
25717 if (gtype)
25719 tree guide = build_deduction_guide (gtype, outer_args, complain);
25720 cands = lookup_add (guide, cands);
25724 if (elided && !cands)
25726 error ("cannot deduce template arguments for copy-initialization"
25727 " of %qT, as it has no non-explicit deduction guides or "
25728 "user-declared constructors", type);
25729 return error_mark_node;
25731 else if (!cands && call == error_mark_node)
25733 error ("cannot deduce template arguments of %qT, as it has no viable "
25734 "deduction guides", type);
25735 return error_mark_node;
25738 if (call == error_mark_node)
25740 ++cp_unevaluated_operand;
25741 call = build_new_function_call (cands, &args, tf_decltype);
25742 --cp_unevaluated_operand;
25745 if (call == error_mark_node && (complain & tf_warning_or_error))
25747 error ("class template argument deduction failed:");
25749 ++cp_unevaluated_operand;
25750 call = build_new_function_call (cands, &args, complain | tf_decltype);
25751 --cp_unevaluated_operand;
25753 if (elided)
25754 inform (input_location, "explicit deduction guides not considered "
25755 "for copy-initialization");
25758 release_tree_vector (args);
25760 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25763 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25764 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25766 tree
25767 do_auto_deduction (tree type, tree init, tree auto_node)
25769 return do_auto_deduction (type, init, auto_node,
25770 tf_warning_or_error,
25771 adc_unspecified);
25774 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25775 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25776 The CONTEXT determines the context in which auto deduction is performed
25777 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25778 OUTER_TARGS are used during template argument deduction
25779 (context == adc_unify) to properly substitute the result, and is ignored
25780 in other contexts.
25782 For partial-concept-ids, extra args may be appended to the list of deduced
25783 template arguments prior to determining constraint satisfaction. */
25785 tree
25786 do_auto_deduction (tree type, tree init, tree auto_node,
25787 tsubst_flags_t complain, auto_deduction_context context,
25788 tree outer_targs, int flags)
25790 tree targs;
25792 if (init == error_mark_node)
25793 return error_mark_node;
25795 if (init && type_dependent_expression_p (init)
25796 && context != adc_unify)
25797 /* Defining a subset of type-dependent expressions that we can deduce
25798 from ahead of time isn't worth the trouble. */
25799 return type;
25801 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25802 /* C++17 class template argument deduction. */
25803 return do_class_deduction (type, tmpl, init, flags, complain);
25805 if (TREE_TYPE (init) == NULL_TREE)
25806 /* Nothing we can do with this, even in deduction context. */
25807 return type;
25809 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25810 with either a new invented type template parameter U or, if the
25811 initializer is a braced-init-list (8.5.4), with
25812 std::initializer_list<U>. */
25813 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25815 if (!DIRECT_LIST_INIT_P (init))
25816 type = listify_autos (type, auto_node);
25817 else if (CONSTRUCTOR_NELTS (init) == 1)
25818 init = CONSTRUCTOR_ELT (init, 0)->value;
25819 else
25821 if (complain & tf_warning_or_error)
25823 if (permerror (input_location, "direct-list-initialization of "
25824 "%<auto%> requires exactly one element"))
25825 inform (input_location,
25826 "for deduction to %<std::initializer_list%>, use copy-"
25827 "list-initialization (i.e. add %<=%> before the %<{%>)");
25829 type = listify_autos (type, auto_node);
25833 if (type == error_mark_node)
25834 return error_mark_node;
25836 init = resolve_nondeduced_context (init, complain);
25838 if (context == adc_decomp_type
25839 && auto_node == type
25840 && init != error_mark_node
25841 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25842 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25843 and initializer has array type, deduce cv-qualified array type. */
25844 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25845 complain);
25846 else if (AUTO_IS_DECLTYPE (auto_node))
25848 bool id = (DECL_P (init)
25849 || ((TREE_CODE (init) == COMPONENT_REF
25850 || TREE_CODE (init) == SCOPE_REF)
25851 && !REF_PARENTHESIZED_P (init)));
25852 targs = make_tree_vec (1);
25853 TREE_VEC_ELT (targs, 0)
25854 = finish_decltype_type (init, id, tf_warning_or_error);
25855 if (type != auto_node)
25857 if (complain & tf_error)
25858 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25859 return error_mark_node;
25862 else
25864 tree parms = build_tree_list (NULL_TREE, type);
25865 tree tparms;
25867 if (flag_concepts)
25868 tparms = extract_autos (type);
25869 else
25871 tparms = make_tree_vec (1);
25872 TREE_VEC_ELT (tparms, 0)
25873 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25876 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25877 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25878 DEDUCE_CALL, LOOKUP_NORMAL,
25879 NULL, /*explain_p=*/false);
25880 if (val > 0)
25882 if (processing_template_decl)
25883 /* Try again at instantiation time. */
25884 return type;
25885 if (type && type != error_mark_node
25886 && (complain & tf_error))
25887 /* If type is error_mark_node a diagnostic must have been
25888 emitted by now. Also, having a mention to '<type error>'
25889 in the diagnostic is not really useful to the user. */
25891 if (cfun && auto_node == current_function_auto_return_pattern
25892 && LAMBDA_FUNCTION_P (current_function_decl))
25893 error ("unable to deduce lambda return type from %qE", init);
25894 else
25895 error ("unable to deduce %qT from %qE", type, init);
25896 type_unification_real (tparms, targs, parms, &init, 1, 0,
25897 DEDUCE_CALL, LOOKUP_NORMAL,
25898 NULL, /*explain_p=*/true);
25900 return error_mark_node;
25904 /* Check any placeholder constraints against the deduced type. */
25905 if (flag_concepts && !processing_template_decl)
25906 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25908 /* Use the deduced type to check the associated constraints. If we
25909 have a partial-concept-id, rebuild the argument list so that
25910 we check using the extra arguments. */
25911 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25912 tree cargs = CHECK_CONSTR_ARGS (constr);
25913 if (TREE_VEC_LENGTH (cargs) > 1)
25915 cargs = copy_node (cargs);
25916 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25918 else
25919 cargs = targs;
25920 if (!constraints_satisfied_p (constr, cargs))
25922 if (complain & tf_warning_or_error)
25924 switch (context)
25926 case adc_unspecified:
25927 case adc_unify:
25928 error("placeholder constraints not satisfied");
25929 break;
25930 case adc_variable_type:
25931 case adc_decomp_type:
25932 error ("deduced initializer does not satisfy "
25933 "placeholder constraints");
25934 break;
25935 case adc_return_type:
25936 error ("deduced return type does not satisfy "
25937 "placeholder constraints");
25938 break;
25939 case adc_requirement:
25940 error ("deduced expression type does not satisfy "
25941 "placeholder constraints");
25942 break;
25944 diagnose_constraints (input_location, constr, targs);
25946 return error_mark_node;
25950 if (processing_template_decl && context != adc_unify)
25951 outer_targs = current_template_args ();
25952 targs = add_to_template_args (outer_targs, targs);
25953 return tsubst (type, targs, complain, NULL_TREE);
25956 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25957 result. */
25959 tree
25960 splice_late_return_type (tree type, tree late_return_type)
25962 if (is_auto (type))
25964 if (late_return_type)
25965 return late_return_type;
25967 tree idx = get_template_parm_index (type);
25968 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25969 /* In an abbreviated function template we didn't know we were dealing
25970 with a function template when we saw the auto return type, so update
25971 it to have the correct level. */
25972 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25974 return type;
25977 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25978 'decltype(auto)' or a deduced class template. */
25980 bool
25981 is_auto (const_tree type)
25983 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25984 && (TYPE_IDENTIFIER (type) == auto_identifier
25985 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25986 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25987 return true;
25988 else
25989 return false;
25992 /* for_each_template_parm callback for type_uses_auto. */
25995 is_auto_r (tree tp, void */*data*/)
25997 return is_auto (tp);
26000 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26001 a use of `auto'. Returns NULL_TREE otherwise. */
26003 tree
26004 type_uses_auto (tree type)
26006 if (type == NULL_TREE)
26007 return NULL_TREE;
26008 else if (flag_concepts)
26010 /* The Concepts TS allows multiple autos in one type-specifier; just
26011 return the first one we find, do_auto_deduction will collect all of
26012 them. */
26013 if (uses_template_parms (type))
26014 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26015 /*visited*/NULL, /*nondeduced*/true);
26016 else
26017 return NULL_TREE;
26019 else
26020 return find_type_usage (type, is_auto);
26023 /* For a given template T, return the vector of typedefs referenced
26024 in T for which access check is needed at T instantiation time.
26025 T is either a FUNCTION_DECL or a RECORD_TYPE.
26026 Those typedefs were added to T by the function
26027 append_type_to_template_for_access_check. */
26029 vec<qualified_typedef_usage_t, va_gc> *
26030 get_types_needing_access_check (tree t)
26032 tree ti;
26033 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26035 if (!t || t == error_mark_node)
26036 return NULL;
26038 if (!(ti = get_template_info (t)))
26039 return NULL;
26041 if (CLASS_TYPE_P (t)
26042 || TREE_CODE (t) == FUNCTION_DECL)
26044 if (!TI_TEMPLATE (ti))
26045 return NULL;
26047 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26050 return result;
26053 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26054 tied to T. That list of typedefs will be access checked at
26055 T instantiation time.
26056 T is either a FUNCTION_DECL or a RECORD_TYPE.
26057 TYPE_DECL is a TYPE_DECL node representing a typedef.
26058 SCOPE is the scope through which TYPE_DECL is accessed.
26059 LOCATION is the location of the usage point of TYPE_DECL.
26061 This function is a subroutine of
26062 append_type_to_template_for_access_check. */
26064 static void
26065 append_type_to_template_for_access_check_1 (tree t,
26066 tree type_decl,
26067 tree scope,
26068 location_t location)
26070 qualified_typedef_usage_t typedef_usage;
26071 tree ti;
26073 if (!t || t == error_mark_node)
26074 return;
26076 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26077 || CLASS_TYPE_P (t))
26078 && type_decl
26079 && TREE_CODE (type_decl) == TYPE_DECL
26080 && scope);
26082 if (!(ti = get_template_info (t)))
26083 return;
26085 gcc_assert (TI_TEMPLATE (ti));
26087 typedef_usage.typedef_decl = type_decl;
26088 typedef_usage.context = scope;
26089 typedef_usage.locus = location;
26091 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26094 /* Append TYPE_DECL to the template TEMPL.
26095 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26096 At TEMPL instanciation time, TYPE_DECL will be checked to see
26097 if it can be accessed through SCOPE.
26098 LOCATION is the location of the usage point of TYPE_DECL.
26100 e.g. consider the following code snippet:
26102 class C
26104 typedef int myint;
26107 template<class U> struct S
26109 C::myint mi; // <-- usage point of the typedef C::myint
26112 S<char> s;
26114 At S<char> instantiation time, we need to check the access of C::myint
26115 In other words, we need to check the access of the myint typedef through
26116 the C scope. For that purpose, this function will add the myint typedef
26117 and the scope C through which its being accessed to a list of typedefs
26118 tied to the template S. That list will be walked at template instantiation
26119 time and access check performed on each typedefs it contains.
26120 Note that this particular code snippet should yield an error because
26121 myint is private to C. */
26123 void
26124 append_type_to_template_for_access_check (tree templ,
26125 tree type_decl,
26126 tree scope,
26127 location_t location)
26129 qualified_typedef_usage_t *iter;
26130 unsigned i;
26132 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26134 /* Make sure we don't append the type to the template twice. */
26135 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26136 if (iter->typedef_decl == type_decl && scope == iter->context)
26137 return;
26139 append_type_to_template_for_access_check_1 (templ, type_decl,
26140 scope, location);
26143 /* Convert the generic type parameters in PARM that match the types given in the
26144 range [START_IDX, END_IDX) from the current_template_parms into generic type
26145 packs. */
26147 tree
26148 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26150 tree current = current_template_parms;
26151 int depth = TMPL_PARMS_DEPTH (current);
26152 current = INNERMOST_TEMPLATE_PARMS (current);
26153 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26155 for (int i = 0; i < start_idx; ++i)
26156 TREE_VEC_ELT (replacement, i)
26157 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26159 for (int i = start_idx; i < end_idx; ++i)
26161 /* Create a distinct parameter pack type from the current parm and add it
26162 to the replacement args to tsubst below into the generic function
26163 parameter. */
26165 tree o = TREE_TYPE (TREE_VALUE
26166 (TREE_VEC_ELT (current, i)));
26167 tree t = copy_type (o);
26168 TEMPLATE_TYPE_PARM_INDEX (t)
26169 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26170 o, 0, 0, tf_none);
26171 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26172 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26173 TYPE_MAIN_VARIANT (t) = t;
26174 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26175 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26176 TREE_VEC_ELT (replacement, i) = t;
26177 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26180 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26181 TREE_VEC_ELT (replacement, i)
26182 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26184 /* If there are more levels then build up the replacement with the outer
26185 template parms. */
26186 if (depth > 1)
26187 replacement = add_to_template_args (template_parms_to_args
26188 (TREE_CHAIN (current_template_parms)),
26189 replacement);
26191 return tsubst (parm, replacement, tf_none, NULL_TREE);
26194 /* Entries in the decl_constraint hash table. */
26195 struct GTY((for_user)) constr_entry
26197 tree decl;
26198 tree ci;
26201 /* Hashing function and equality for constraint entries. */
26202 struct constr_hasher : ggc_ptr_hash<constr_entry>
26204 static hashval_t hash (constr_entry *e)
26206 return (hashval_t)DECL_UID (e->decl);
26209 static bool equal (constr_entry *e1, constr_entry *e2)
26211 return e1->decl == e2->decl;
26215 /* A mapping from declarations to constraint information. Note that
26216 both templates and their underlying declarations are mapped to the
26217 same constraint information.
26219 FIXME: This is defined in pt.c because garbage collection
26220 code is not being generated for constraint.cc. */
26222 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26224 /* Returns the template constraints of declaration T. If T is not
26225 constrained, return NULL_TREE. Note that T must be non-null. */
26227 tree
26228 get_constraints (tree t)
26230 if (!flag_concepts)
26231 return NULL_TREE;
26233 gcc_assert (DECL_P (t));
26234 if (TREE_CODE (t) == TEMPLATE_DECL)
26235 t = DECL_TEMPLATE_RESULT (t);
26236 constr_entry elt = { t, NULL_TREE };
26237 constr_entry* found = decl_constraints->find (&elt);
26238 if (found)
26239 return found->ci;
26240 else
26241 return NULL_TREE;
26244 /* Associate the given constraint information CI with the declaration
26245 T. If T is a template, then the constraints are associated with
26246 its underlying declaration. Don't build associations if CI is
26247 NULL_TREE. */
26249 void
26250 set_constraints (tree t, tree ci)
26252 if (!ci)
26253 return;
26254 gcc_assert (t && flag_concepts);
26255 if (TREE_CODE (t) == TEMPLATE_DECL)
26256 t = DECL_TEMPLATE_RESULT (t);
26257 gcc_assert (!get_constraints (t));
26258 constr_entry elt = {t, ci};
26259 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26260 constr_entry* entry = ggc_alloc<constr_entry> ();
26261 *entry = elt;
26262 *slot = entry;
26265 /* Remove the associated constraints of the declaration T. */
26267 void
26268 remove_constraints (tree t)
26270 gcc_assert (DECL_P (t));
26271 if (TREE_CODE (t) == TEMPLATE_DECL)
26272 t = DECL_TEMPLATE_RESULT (t);
26274 constr_entry elt = {t, NULL_TREE};
26275 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26276 if (slot)
26277 decl_constraints->clear_slot (slot);
26280 /* Memoized satisfaction results for declarations. This
26281 maps the pair (constraint_info, arguments) to the result computed
26282 by constraints_satisfied_p. */
26284 struct GTY((for_user)) constraint_sat_entry
26286 tree ci;
26287 tree args;
26288 tree result;
26291 /* Hashing function and equality for constraint entries. */
26293 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26295 static hashval_t hash (constraint_sat_entry *e)
26297 hashval_t val = iterative_hash_object(e->ci, 0);
26298 return iterative_hash_template_arg (e->args, val);
26301 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26303 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26307 /* Memoized satisfaction results for concept checks. */
26309 struct GTY((for_user)) concept_spec_entry
26311 tree tmpl;
26312 tree args;
26313 tree result;
26316 /* Hashing function and equality for constraint entries. */
26318 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26320 static hashval_t hash (concept_spec_entry *e)
26322 return hash_tmpl_and_args (e->tmpl, e->args);
26325 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26327 ++comparing_specializations;
26328 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26329 --comparing_specializations;
26330 return eq;
26334 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26335 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26337 /* Search for a memoized satisfaction result. Returns one of the
26338 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26340 tree
26341 lookup_constraint_satisfaction (tree ci, tree args)
26343 constraint_sat_entry elt = { ci, args, NULL_TREE };
26344 constraint_sat_entry* found = constraint_memos->find (&elt);
26345 if (found)
26346 return found->result;
26347 else
26348 return NULL_TREE;
26351 /* Memoize the result of a satisfication test. Returns the saved result. */
26353 tree
26354 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26356 constraint_sat_entry elt = {ci, args, result};
26357 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26358 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26359 *entry = elt;
26360 *slot = entry;
26361 return result;
26364 /* Search for a memoized satisfaction result for a concept. */
26366 tree
26367 lookup_concept_satisfaction (tree tmpl, tree args)
26369 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26370 concept_spec_entry* found = concept_memos->find (&elt);
26371 if (found)
26372 return found->result;
26373 else
26374 return NULL_TREE;
26377 /* Memoize the result of a concept check. Returns the saved result. */
26379 tree
26380 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26382 concept_spec_entry elt = {tmpl, args, result};
26383 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26384 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26385 *entry = elt;
26386 *slot = entry;
26387 return result;
26390 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26392 /* Returns a prior concept specialization. This returns the substituted
26393 and normalized constraints defined by the concept. */
26395 tree
26396 get_concept_expansion (tree tmpl, tree args)
26398 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26399 concept_spec_entry* found = concept_expansions->find (&elt);
26400 if (found)
26401 return found->result;
26402 else
26403 return NULL_TREE;
26406 /* Save a concept expansion for later. */
26408 tree
26409 save_concept_expansion (tree tmpl, tree args, tree def)
26411 concept_spec_entry elt = {tmpl, args, def};
26412 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26413 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26414 *entry = elt;
26415 *slot = entry;
26416 return def;
26419 static hashval_t
26420 hash_subsumption_args (tree t1, tree t2)
26422 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26423 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26424 int val = 0;
26425 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26426 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26427 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26428 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26429 return val;
26432 /* Compare the constraints of two subsumption entries. The LEFT1 and
26433 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26434 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26436 static bool
26437 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26439 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26440 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26441 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26442 CHECK_CONSTR_ARGS (right1)))
26443 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26444 CHECK_CONSTR_ARGS (right2));
26445 return false;
26448 /* Key/value pair for learning and memoizing subsumption results. This
26449 associates a pair of check constraints (including arguments) with
26450 a boolean value indicating the result. */
26452 struct GTY((for_user)) subsumption_entry
26454 tree t1;
26455 tree t2;
26456 bool result;
26459 /* Hashing function and equality for constraint entries. */
26461 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26463 static hashval_t hash (subsumption_entry *e)
26465 return hash_subsumption_args (e->t1, e->t2);
26468 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26470 ++comparing_specializations;
26471 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26472 --comparing_specializations;
26473 return eq;
26477 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26479 /* Search for a previously cached subsumption result. */
26481 bool*
26482 lookup_subsumption_result (tree t1, tree t2)
26484 subsumption_entry elt = { t1, t2, false };
26485 subsumption_entry* found = subsumption_table->find (&elt);
26486 if (found)
26487 return &found->result;
26488 else
26489 return 0;
26492 /* Save a subsumption result. */
26494 bool
26495 save_subsumption_result (tree t1, tree t2, bool result)
26497 subsumption_entry elt = {t1, t2, result};
26498 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26499 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26500 *entry = elt;
26501 *slot = entry;
26502 return result;
26505 /* Set up the hash table for constraint association. */
26507 void
26508 init_constraint_processing (void)
26510 if (!flag_concepts)
26511 return;
26513 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26514 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26515 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26516 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26517 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26520 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26521 0..N-1. */
26523 void
26524 declare_integer_pack (void)
26526 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26527 build_function_type_list (integer_type_node,
26528 integer_type_node,
26529 NULL_TREE),
26530 NULL_TREE, ECF_CONST);
26531 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26532 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26535 /* Set up the hash tables for template instantiations. */
26537 void
26538 init_template_processing (void)
26540 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26541 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26543 if (cxx_dialect >= cxx11)
26544 declare_integer_pack ();
26547 /* Print stats about the template hash tables for -fstats. */
26549 void
26550 print_template_statistics (void)
26552 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26553 "%f collisions\n", (long) decl_specializations->size (),
26554 (long) decl_specializations->elements (),
26555 decl_specializations->collisions ());
26556 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26557 "%f collisions\n", (long) type_specializations->size (),
26558 (long) type_specializations->elements (),
26559 type_specializations->collisions ());
26562 #include "gt-cp-pt.h"