Various small C++ fixes.
[official-gcc.git] / gcc / cp / pt.c
blobd93d518c22d88de1a2277af3767c5a7733375a88
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
45 /* The type of functions taking a tree, and some additional data, and
46 returning an int. */
47 typedef int (*tree_fn_t) (tree, void*);
49 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
50 instantiations have been deferred, either because their definitions
51 were not yet available, or because we were putting off doing the work. */
52 struct GTY ((chain_next ("%h.next"))) pending_template {
53 struct pending_template *next;
54 struct tinst_level *tinst;
57 static GTY(()) struct pending_template *pending_templates;
58 static GTY(()) struct pending_template *last_pending_template;
60 int processing_template_parmlist;
61 static int template_header_count;
63 static GTY(()) tree saved_trees;
64 static vec<int> inline_parm_levels;
66 static GTY(()) struct tinst_level *current_tinst_level;
68 static GTY(()) tree saved_access_scope;
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr;
75 // -------------------------------------------------------------------------- //
76 // Local Specialization Stack
78 // Implementation of the RAII helper for creating new local
79 // specializations.
80 local_specialization_stack::local_specialization_stack (lss_policy policy)
81 : saved (local_specializations)
83 if (policy == lss_blank || !saved)
84 local_specializations = new hash_map<tree, tree>;
85 else
86 local_specializations = new hash_map<tree, tree>(*saved);
89 local_specialization_stack::~local_specialization_stack ()
91 delete local_specializations;
92 local_specializations = saved;
95 /* True if we've recursed into fn_type_unification too many times. */
96 static bool excessive_deduction_depth;
98 struct GTY((for_user)) spec_entry
100 tree tmpl;
101 tree args;
102 tree spec;
105 struct spec_hasher : ggc_ptr_hash<spec_entry>
107 static hashval_t hash (spec_entry *);
108 static bool equal (spec_entry *, spec_entry *);
111 static GTY (()) hash_table<spec_hasher> *decl_specializations;
113 static GTY (()) hash_table<spec_hasher> *type_specializations;
115 /* Contains canonical template parameter types. The vector is indexed by
116 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
117 TREE_LIST, whose TREE_VALUEs contain the canonical template
118 parameters of various types and levels. */
119 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
121 #define UNIFY_ALLOW_NONE 0
122 #define UNIFY_ALLOW_MORE_CV_QUAL 1
123 #define UNIFY_ALLOW_LESS_CV_QUAL 2
124 #define UNIFY_ALLOW_DERIVED 4
125 #define UNIFY_ALLOW_INTEGER 8
126 #define UNIFY_ALLOW_OUTER_LEVEL 16
127 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
128 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
130 enum template_base_result {
131 tbr_incomplete_type,
132 tbr_ambiguous_baseclass,
133 tbr_success
136 static void push_access_scope (tree);
137 static void pop_access_scope (tree);
138 static bool resolve_overloaded_unification (tree, tree, tree, tree,
139 unification_kind_t, int,
140 bool);
141 static int try_one_overload (tree, tree, tree, tree, tree,
142 unification_kind_t, int, bool, bool);
143 static int unify (tree, tree, tree, tree, int, bool);
144 static void add_pending_template (tree);
145 static tree reopen_tinst_level (struct tinst_level *);
146 static tree tsubst_initializer_list (tree, tree);
147 static tree get_partial_spec_bindings (tree, tree, tree);
148 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
149 bool, bool);
150 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static void tsubst_enum (tree, tree, tree);
153 static tree add_to_template_args (tree, tree);
154 static tree add_outermost_template_args (tree, tree);
155 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
156 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
157 tree);
158 static int type_unification_real (tree, tree, tree, const tree *,
159 unsigned int, int, unification_kind_t, int,
160 vec<deferred_access_check, va_gc> **,
161 bool);
162 static void note_template_header (int);
163 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
164 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
165 static tree convert_template_argument (tree, tree, tree,
166 tsubst_flags_t, int, tree);
167 static tree for_each_template_parm (tree, tree_fn_t, void*,
168 hash_set<tree> *, bool, tree_fn_t = NULL);
169 static tree expand_template_argument_pack (tree);
170 static tree build_template_parm_index (int, int, int, tree, tree);
171 static bool inline_needs_template_parms (tree, bool);
172 static void push_inline_template_parms_recursive (tree, int);
173 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
174 static int mark_template_parm (tree, void *);
175 static int template_parm_this_level_p (tree, void *);
176 static tree tsubst_friend_function (tree, tree);
177 static tree tsubst_friend_class (tree, tree);
178 static int can_complete_type_without_circularity (tree);
179 static tree get_bindings (tree, tree, tree, bool);
180 static int template_decl_level (tree);
181 static int check_cv_quals_for_unify (int, tree, tree);
182 static void template_parm_level_and_index (tree, int*, int*);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
187 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
189 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
190 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
191 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
193 static bool check_specialization_scope (void);
194 static tree process_partial_specialization (tree);
195 static void set_current_access_from_decl (tree);
196 static enum template_base_result get_template_base (tree, tree, tree, tree,
197 bool , tree *);
198 static tree try_class_unification (tree, tree, tree, tree, bool);
199 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
200 tree, tree);
201 static bool template_template_parm_bindings_ok_p (tree, tree);
202 static void tsubst_default_arguments (tree, tsubst_flags_t);
203 static tree for_each_template_parm_r (tree *, int *, void *);
204 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
205 static void copy_default_args_to_explicit_spec (tree);
206 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
207 static bool dependent_template_arg_p (tree);
208 static bool any_template_arguments_need_structural_equality_p (tree);
209 static bool dependent_type_p_r (tree);
210 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
211 static tree tsubst_decl (tree, tree, tsubst_flags_t);
212 static void perform_typedefs_access_check (tree tmpl, tree targs);
213 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
214 location_t);
215 static tree listify (tree);
216 static tree listify_autos (tree, tree);
217 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
218 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
219 static bool complex_alias_template_p (const_tree tmpl);
220 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
221 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
222 static tree make_argument_pack (tree);
223 static void register_parameter_specializations (tree, tree);
225 /* Make the current scope suitable for access checking when we are
226 processing T. T can be FUNCTION_DECL for instantiated function
227 template, VAR_DECL for static member variable, or TYPE_DECL for
228 alias template (needed by instantiate_decl). */
230 static void
231 push_access_scope (tree t)
233 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
234 || TREE_CODE (t) == TYPE_DECL);
236 if (DECL_FRIEND_CONTEXT (t))
237 push_nested_class (DECL_FRIEND_CONTEXT (t));
238 else if (DECL_CLASS_SCOPE_P (t))
239 push_nested_class (DECL_CONTEXT (t));
240 else
241 push_to_top_level ();
243 if (TREE_CODE (t) == FUNCTION_DECL)
245 saved_access_scope = tree_cons
246 (NULL_TREE, current_function_decl, saved_access_scope);
247 current_function_decl = t;
251 /* Restore the scope set up by push_access_scope. T is the node we
252 are processing. */
254 static void
255 pop_access_scope (tree t)
257 if (TREE_CODE (t) == FUNCTION_DECL)
259 current_function_decl = TREE_VALUE (saved_access_scope);
260 saved_access_scope = TREE_CHAIN (saved_access_scope);
263 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
264 pop_nested_class ();
265 else
266 pop_from_top_level ();
269 /* Do any processing required when DECL (a member template
270 declaration) is finished. Returns the TEMPLATE_DECL corresponding
271 to DECL, unless it is a specialization, in which case the DECL
272 itself is returned. */
274 tree
275 finish_member_template_decl (tree decl)
277 if (decl == error_mark_node)
278 return error_mark_node;
280 gcc_assert (DECL_P (decl));
282 if (TREE_CODE (decl) == TYPE_DECL)
284 tree type;
286 type = TREE_TYPE (decl);
287 if (type == error_mark_node)
288 return error_mark_node;
289 if (MAYBE_CLASS_TYPE_P (type)
290 && CLASSTYPE_TEMPLATE_INFO (type)
291 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
293 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
294 check_member_template (tmpl);
295 return tmpl;
297 return NULL_TREE;
299 else if (TREE_CODE (decl) == FIELD_DECL)
300 error ("data member %qD cannot be a member template", decl);
301 else if (DECL_TEMPLATE_INFO (decl))
303 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
305 check_member_template (DECL_TI_TEMPLATE (decl));
306 return DECL_TI_TEMPLATE (decl);
308 else
309 return decl;
311 else
312 error ("invalid member template declaration %qD", decl);
314 return error_mark_node;
317 /* Create a template info node. */
319 tree
320 build_template_info (tree template_decl, tree template_args)
322 tree result = make_node (TEMPLATE_INFO);
323 TI_TEMPLATE (result) = template_decl;
324 TI_ARGS (result) = template_args;
325 return result;
328 /* Return the template info node corresponding to T, whatever T is. */
330 tree
331 get_template_info (const_tree t)
333 tree tinfo = NULL_TREE;
335 if (!t || t == error_mark_node)
336 return NULL;
338 if (TREE_CODE (t) == NAMESPACE_DECL
339 || TREE_CODE (t) == PARM_DECL)
340 return NULL;
342 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
343 tinfo = DECL_TEMPLATE_INFO (t);
345 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
346 t = TREE_TYPE (t);
348 if (OVERLOAD_TYPE_P (t))
349 tinfo = TYPE_TEMPLATE_INFO (t);
350 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
351 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
353 return tinfo;
356 /* Returns the template nesting level of the indicated class TYPE.
358 For example, in:
359 template <class T>
360 struct A
362 template <class U>
363 struct B {};
366 A<T>::B<U> has depth two, while A<T> has depth one.
367 Both A<T>::B<int> and A<int>::B<U> have depth one, if
368 they are instantiations, not specializations.
370 This function is guaranteed to return 0 if passed NULL_TREE so
371 that, for example, `template_class_depth (current_class_type)' is
372 always safe. */
375 template_class_depth (tree type)
377 int depth;
379 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
381 tree tinfo = get_template_info (type);
383 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
384 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
385 ++depth;
387 if (DECL_P (type))
388 type = CP_DECL_CONTEXT (type);
389 else if (LAMBDA_TYPE_P (type))
390 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
391 else
392 type = CP_TYPE_CONTEXT (type);
395 return depth;
398 /* Subroutine of maybe_begin_member_template_processing.
399 Returns true if processing DECL needs us to push template parms. */
401 static bool
402 inline_needs_template_parms (tree decl, bool nsdmi)
404 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
405 return false;
407 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
408 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
411 /* Subroutine of maybe_begin_member_template_processing.
412 Push the template parms in PARMS, starting from LEVELS steps into the
413 chain, and ending at the beginning, since template parms are listed
414 innermost first. */
416 static void
417 push_inline_template_parms_recursive (tree parmlist, int levels)
419 tree parms = TREE_VALUE (parmlist);
420 int i;
422 if (levels > 1)
423 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
425 ++processing_template_decl;
426 current_template_parms
427 = tree_cons (size_int (processing_template_decl),
428 parms, current_template_parms);
429 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
431 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
432 NULL);
433 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
435 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
437 if (error_operand_p (parm))
438 continue;
440 gcc_assert (DECL_P (parm));
442 switch (TREE_CODE (parm))
444 case TYPE_DECL:
445 case TEMPLATE_DECL:
446 pushdecl (parm);
447 break;
449 case PARM_DECL:
450 /* Push the CONST_DECL. */
451 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
452 break;
454 default:
455 gcc_unreachable ();
460 /* Restore the template parameter context for a member template, a
461 friend template defined in a class definition, or a non-template
462 member of template class. */
464 void
465 maybe_begin_member_template_processing (tree decl)
467 tree parms;
468 int levels = 0;
469 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
471 if (nsdmi)
473 tree ctx = DECL_CONTEXT (decl);
474 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
475 /* Disregard full specializations (c++/60999). */
476 && uses_template_parms (ctx)
477 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
480 if (inline_needs_template_parms (decl, nsdmi))
482 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
483 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
485 if (DECL_TEMPLATE_SPECIALIZATION (decl))
487 --levels;
488 parms = TREE_CHAIN (parms);
491 push_inline_template_parms_recursive (parms, levels);
494 /* Remember how many levels of template parameters we pushed so that
495 we can pop them later. */
496 inline_parm_levels.safe_push (levels);
499 /* Undo the effects of maybe_begin_member_template_processing. */
501 void
502 maybe_end_member_template_processing (void)
504 int i;
505 int last;
507 if (inline_parm_levels.length () == 0)
508 return;
510 last = inline_parm_levels.pop ();
511 for (i = 0; i < last; ++i)
513 --processing_template_decl;
514 current_template_parms = TREE_CHAIN (current_template_parms);
515 poplevel (0, 0, 0);
519 /* Return a new template argument vector which contains all of ARGS,
520 but has as its innermost set of arguments the EXTRA_ARGS. */
522 static tree
523 add_to_template_args (tree args, tree extra_args)
525 tree new_args;
526 int extra_depth;
527 int i;
528 int j;
530 if (args == NULL_TREE || extra_args == error_mark_node)
531 return extra_args;
533 extra_depth = TMPL_ARGS_DEPTH (extra_args);
534 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
536 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
537 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
539 for (j = 1; j <= extra_depth; ++j, ++i)
540 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
542 return new_args;
545 /* Like add_to_template_args, but only the outermost ARGS are added to
546 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
547 (EXTRA_ARGS) levels are added. This function is used to combine
548 the template arguments from a partial instantiation with the
549 template arguments used to attain the full instantiation from the
550 partial instantiation. */
552 static tree
553 add_outermost_template_args (tree args, tree extra_args)
555 tree new_args;
557 /* If there are more levels of EXTRA_ARGS than there are ARGS,
558 something very fishy is going on. */
559 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
561 /* If *all* the new arguments will be the EXTRA_ARGS, just return
562 them. */
563 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
564 return extra_args;
566 /* For the moment, we make ARGS look like it contains fewer levels. */
567 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
569 new_args = add_to_template_args (args, extra_args);
571 /* Now, we restore ARGS to its full dimensions. */
572 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
574 return new_args;
577 /* Return the N levels of innermost template arguments from the ARGS. */
579 tree
580 get_innermost_template_args (tree args, int n)
582 tree new_args;
583 int extra_levels;
584 int i;
586 gcc_assert (n >= 0);
588 /* If N is 1, just return the innermost set of template arguments. */
589 if (n == 1)
590 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
592 /* If we're not removing anything, just return the arguments we were
593 given. */
594 extra_levels = TMPL_ARGS_DEPTH (args) - n;
595 gcc_assert (extra_levels >= 0);
596 if (extra_levels == 0)
597 return args;
599 /* Make a new set of arguments, not containing the outer arguments. */
600 new_args = make_tree_vec (n);
601 for (i = 1; i <= n; ++i)
602 SET_TMPL_ARGS_LEVEL (new_args, i,
603 TMPL_ARGS_LEVEL (args, i + extra_levels));
605 return new_args;
608 /* The inverse of get_innermost_template_args: Return all but the innermost
609 EXTRA_LEVELS levels of template arguments from the ARGS. */
611 static tree
612 strip_innermost_template_args (tree args, int extra_levels)
614 tree new_args;
615 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
616 int i;
618 gcc_assert (n >= 0);
620 /* If N is 1, just return the outermost set of template arguments. */
621 if (n == 1)
622 return TMPL_ARGS_LEVEL (args, 1);
624 /* If we're not removing anything, just return the arguments we were
625 given. */
626 gcc_assert (extra_levels >= 0);
627 if (extra_levels == 0)
628 return args;
630 /* Make a new set of arguments, not containing the inner arguments. */
631 new_args = make_tree_vec (n);
632 for (i = 1; i <= n; ++i)
633 SET_TMPL_ARGS_LEVEL (new_args, i,
634 TMPL_ARGS_LEVEL (args, i));
636 return new_args;
639 /* We've got a template header coming up; push to a new level for storing
640 the parms. */
642 void
643 begin_template_parm_list (void)
645 /* We use a non-tag-transparent scope here, which causes pushtag to
646 put tags in this scope, rather than in the enclosing class or
647 namespace scope. This is the right thing, since we want
648 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
649 global template class, push_template_decl handles putting the
650 TEMPLATE_DECL into top-level scope. For a nested template class,
651 e.g.:
653 template <class T> struct S1 {
654 template <class T> struct S2 {};
657 pushtag contains special code to insert the TEMPLATE_DECL for S2
658 at the right scope. */
659 begin_scope (sk_template_parms, NULL);
660 ++processing_template_decl;
661 ++processing_template_parmlist;
662 note_template_header (0);
664 /* Add a dummy parameter level while we process the parameter list. */
665 current_template_parms
666 = tree_cons (size_int (processing_template_decl),
667 make_tree_vec (0),
668 current_template_parms);
671 /* This routine is called when a specialization is declared. If it is
672 invalid to declare a specialization here, an error is reported and
673 false is returned, otherwise this routine will return true. */
675 static bool
676 check_specialization_scope (void)
678 tree scope = current_scope ();
680 /* [temp.expl.spec]
682 An explicit specialization shall be declared in the namespace of
683 which the template is a member, or, for member templates, in the
684 namespace of which the enclosing class or enclosing class
685 template is a member. An explicit specialization of a member
686 function, member class or static data member of a class template
687 shall be declared in the namespace of which the class template
688 is a member. */
689 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
691 error ("explicit specialization in non-namespace scope %qD", scope);
692 return false;
695 /* [temp.expl.spec]
697 In an explicit specialization declaration for a member of a class
698 template or a member template that appears in namespace scope,
699 the member template and some of its enclosing class templates may
700 remain unspecialized, except that the declaration shall not
701 explicitly specialize a class member template if its enclosing
702 class templates are not explicitly specialized as well. */
703 if (current_template_parms)
705 error ("enclosing class templates are not explicitly specialized");
706 return false;
709 return true;
712 /* We've just seen template <>. */
714 bool
715 begin_specialization (void)
717 begin_scope (sk_template_spec, NULL);
718 note_template_header (1);
719 return check_specialization_scope ();
722 /* Called at then end of processing a declaration preceded by
723 template<>. */
725 void
726 end_specialization (void)
728 finish_scope ();
729 reset_specialization ();
732 /* Any template <>'s that we have seen thus far are not referring to a
733 function specialization. */
735 void
736 reset_specialization (void)
738 processing_specialization = 0;
739 template_header_count = 0;
742 /* We've just seen a template header. If SPECIALIZATION is nonzero,
743 it was of the form template <>. */
745 static void
746 note_template_header (int specialization)
748 processing_specialization = specialization;
749 template_header_count++;
752 /* We're beginning an explicit instantiation. */
754 void
755 begin_explicit_instantiation (void)
757 gcc_assert (!processing_explicit_instantiation);
758 processing_explicit_instantiation = true;
762 void
763 end_explicit_instantiation (void)
765 gcc_assert (processing_explicit_instantiation);
766 processing_explicit_instantiation = false;
769 /* An explicit specialization or partial specialization of TMPL is being
770 declared. Check that the namespace in which the specialization is
771 occurring is permissible. Returns false iff it is invalid to
772 specialize TMPL in the current namespace. */
774 static bool
775 check_specialization_namespace (tree tmpl)
777 tree tpl_ns = decl_namespace_context (tmpl);
779 /* [tmpl.expl.spec]
781 An explicit specialization shall be declared in a namespace enclosing the
782 specialized template. An explicit specialization whose declarator-id is
783 not qualified shall be declared in the nearest enclosing namespace of the
784 template, or, if the namespace is inline (7.3.1), any namespace from its
785 enclosing namespace set. */
786 if (current_scope() != DECL_CONTEXT (tmpl)
787 && !at_namespace_scope_p ())
789 error ("specialization of %qD must appear at namespace scope", tmpl);
790 return false;
793 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
794 /* Same or enclosing namespace. */
795 return true;
796 else
798 permerror (input_location,
799 "specialization of %qD in different namespace", tmpl);
800 inform (DECL_SOURCE_LOCATION (tmpl),
801 " from definition of %q#D", tmpl);
802 return false;
806 /* SPEC is an explicit instantiation. Check that it is valid to
807 perform this explicit instantiation in the current namespace. */
809 static void
810 check_explicit_instantiation_namespace (tree spec)
812 tree ns;
814 /* DR 275: An explicit instantiation shall appear in an enclosing
815 namespace of its template. */
816 ns = decl_namespace_context (spec);
817 if (!is_nested_namespace (current_namespace, ns))
818 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
819 "(which does not enclose namespace %qD)",
820 spec, current_namespace, ns);
823 // Returns the type of a template specialization only if that
824 // specialization needs to be defined. Otherwise (e.g., if the type has
825 // already been defined), the function returns NULL_TREE.
826 static tree
827 maybe_new_partial_specialization (tree type)
829 // An implicit instantiation of an incomplete type implies
830 // the definition of a new class template.
832 // template<typename T>
833 // struct S;
835 // template<typename T>
836 // struct S<T*>;
838 // Here, S<T*> is an implicit instantiation of S whose type
839 // is incomplete.
840 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
841 return type;
843 // It can also be the case that TYPE is a completed specialization.
844 // Continuing the previous example, suppose we also declare:
846 // template<typename T>
847 // requires Integral<T>
848 // struct S<T*>;
850 // Here, S<T*> refers to the specialization S<T*> defined
851 // above. However, we need to differentiate definitions because
852 // we intend to define a new partial specialization. In this case,
853 // we rely on the fact that the constraints are different for
854 // this declaration than that above.
856 // Note that we also get here for injected class names and
857 // late-parsed template definitions. We must ensure that we
858 // do not create new type declarations for those cases.
859 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
861 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
862 tree args = CLASSTYPE_TI_ARGS (type);
864 // If there are no template parameters, this cannot be a new
865 // partial template specializtion?
866 if (!current_template_parms)
867 return NULL_TREE;
869 // The injected-class-name is not a new partial specialization.
870 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
871 return NULL_TREE;
873 // If the constraints are not the same as those of the primary
874 // then, we can probably create a new specialization.
875 tree type_constr = current_template_constraints ();
877 if (type == TREE_TYPE (tmpl))
879 tree main_constr = get_constraints (tmpl);
880 if (equivalent_constraints (type_constr, main_constr))
881 return NULL_TREE;
884 // Also, if there's a pre-existing specialization with matching
885 // constraints, then this also isn't new.
886 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
887 while (specs)
889 tree spec_tmpl = TREE_VALUE (specs);
890 tree spec_args = TREE_PURPOSE (specs);
891 tree spec_constr = get_constraints (spec_tmpl);
892 if (comp_template_args (args, spec_args)
893 && equivalent_constraints (type_constr, spec_constr))
894 return NULL_TREE;
895 specs = TREE_CHAIN (specs);
898 // Create a new type node (and corresponding type decl)
899 // for the newly declared specialization.
900 tree t = make_class_type (TREE_CODE (type));
901 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
902 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
904 /* We only need a separate type node for storing the definition of this
905 partial specialization; uses of S<T*> are unconstrained, so all are
906 equivalent. So keep TYPE_CANONICAL the same. */
907 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
909 // Build the corresponding type decl.
910 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
911 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
912 DECL_SOURCE_LOCATION (d) = input_location;
914 return t;
917 return NULL_TREE;
920 /* The TYPE is being declared. If it is a template type, that means it
921 is a partial specialization. Do appropriate error-checking. */
923 tree
924 maybe_process_partial_specialization (tree type)
926 tree context;
928 if (type == error_mark_node)
929 return error_mark_node;
931 /* A lambda that appears in specialization context is not itself a
932 specialization. */
933 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
934 return type;
936 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
938 error ("name of class shadows template template parameter %qD",
939 TYPE_NAME (type));
940 return error_mark_node;
943 context = TYPE_CONTEXT (type);
945 if (TYPE_ALIAS_P (type))
947 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
949 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
950 error ("specialization of alias template %qD",
951 TI_TEMPLATE (tinfo));
952 else
953 error ("explicit specialization of non-template %qT", type);
954 return error_mark_node;
956 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
958 /* This is for ordinary explicit specialization and partial
959 specialization of a template class such as:
961 template <> class C<int>;
965 template <class T> class C<T*>;
967 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
969 if (tree t = maybe_new_partial_specialization (type))
971 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
972 && !at_namespace_scope_p ())
973 return error_mark_node;
974 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
975 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
976 if (processing_template_decl)
978 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
979 if (decl == error_mark_node)
980 return error_mark_node;
981 return TREE_TYPE (decl);
984 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
985 error ("specialization of %qT after instantiation", type);
986 else if (errorcount && !processing_specialization
987 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
988 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
989 /* Trying to define a specialization either without a template<> header
990 or in an inappropriate place. We've already given an error, so just
991 bail now so we don't actually define the specialization. */
992 return error_mark_node;
994 else if (CLASS_TYPE_P (type)
995 && !CLASSTYPE_USE_TEMPLATE (type)
996 && CLASSTYPE_TEMPLATE_INFO (type)
997 && context && CLASS_TYPE_P (context)
998 && CLASSTYPE_TEMPLATE_INFO (context))
1000 /* This is for an explicit specialization of member class
1001 template according to [temp.expl.spec/18]:
1003 template <> template <class U> class C<int>::D;
1005 The context `C<int>' must be an implicit instantiation.
1006 Otherwise this is just a member class template declared
1007 earlier like:
1009 template <> class C<int> { template <class U> class D; };
1010 template <> template <class U> class C<int>::D;
1012 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1013 while in the second case, `C<int>::D' is a primary template
1014 and `C<T>::D' may not exist. */
1016 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1017 && !COMPLETE_TYPE_P (type))
1019 tree t;
1020 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1022 if (current_namespace
1023 != decl_namespace_context (tmpl))
1025 permerror (input_location,
1026 "specializing %q#T in different namespace", type);
1027 permerror (DECL_SOURCE_LOCATION (tmpl),
1028 " from definition of %q#D", tmpl);
1031 /* Check for invalid specialization after instantiation:
1033 template <> template <> class C<int>::D<int>;
1034 template <> template <class U> class C<int>::D; */
1036 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1037 t; t = TREE_CHAIN (t))
1039 tree inst = TREE_VALUE (t);
1040 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1041 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1043 /* We already have a full specialization of this partial
1044 instantiation, or a full specialization has been
1045 looked up but not instantiated. Reassign it to the
1046 new member specialization template. */
1047 spec_entry elt;
1048 spec_entry *entry;
1050 elt.tmpl = most_general_template (tmpl);
1051 elt.args = CLASSTYPE_TI_ARGS (inst);
1052 elt.spec = inst;
1054 type_specializations->remove_elt (&elt);
1056 elt.tmpl = tmpl;
1057 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1059 spec_entry **slot
1060 = type_specializations->find_slot (&elt, INSERT);
1061 entry = ggc_alloc<spec_entry> ();
1062 *entry = elt;
1063 *slot = entry;
1065 else
1066 /* But if we've had an implicit instantiation, that's a
1067 problem ([temp.expl.spec]/6). */
1068 error ("specialization %qT after instantiation %qT",
1069 type, inst);
1072 /* Mark TYPE as a specialization. And as a result, we only
1073 have one level of template argument for the innermost
1074 class template. */
1075 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1076 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1077 CLASSTYPE_TI_ARGS (type)
1078 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1081 else if (processing_specialization)
1083 /* Someday C++0x may allow for enum template specialization. */
1084 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1085 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1086 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1087 "of %qD not allowed by ISO C++", type);
1088 else
1090 error ("explicit specialization of non-template %qT", type);
1091 return error_mark_node;
1095 return type;
1098 /* Returns nonzero if we can optimize the retrieval of specializations
1099 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1100 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1102 static inline bool
1103 optimize_specialization_lookup_p (tree tmpl)
1105 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1106 && DECL_CLASS_SCOPE_P (tmpl)
1107 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1108 parameter. */
1109 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1110 /* The optimized lookup depends on the fact that the
1111 template arguments for the member function template apply
1112 purely to the containing class, which is not true if the
1113 containing class is an explicit or partial
1114 specialization. */
1115 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1116 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1117 && !DECL_CONV_FN_P (tmpl)
1118 /* It is possible to have a template that is not a member
1119 template and is not a member of a template class:
1121 template <typename T>
1122 struct S { friend A::f(); };
1124 Here, the friend function is a template, but the context does
1125 not have template information. The optimized lookup relies
1126 on having ARGS be the template arguments for both the class
1127 and the function template. */
1128 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1131 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1132 gone through coerce_template_parms by now. */
1134 static void
1135 verify_unstripped_args (tree args)
1137 ++processing_template_decl;
1138 if (!any_dependent_template_arguments_p (args))
1140 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1141 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1143 tree arg = TREE_VEC_ELT (inner, i);
1144 if (TREE_CODE (arg) == TEMPLATE_DECL)
1145 /* OK */;
1146 else if (TYPE_P (arg))
1147 gcc_assert (strip_typedefs (arg, NULL) == arg);
1148 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1151 else
1152 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1155 --processing_template_decl;
1158 /* Retrieve the specialization (in the sense of [temp.spec] - a
1159 specialization is either an instantiation or an explicit
1160 specialization) of TMPL for the given template ARGS. If there is
1161 no such specialization, return NULL_TREE. The ARGS are a vector of
1162 arguments, or a vector of vectors of arguments, in the case of
1163 templates with more than one level of parameters.
1165 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1166 then we search for a partial specialization matching ARGS. This
1167 parameter is ignored if TMPL is not a class template.
1169 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1170 result is a NONTYPE_ARGUMENT_PACK. */
1172 static tree
1173 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1175 if (tmpl == NULL_TREE)
1176 return NULL_TREE;
1178 if (args == error_mark_node)
1179 return NULL_TREE;
1181 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1182 || TREE_CODE (tmpl) == FIELD_DECL);
1184 /* There should be as many levels of arguments as there are
1185 levels of parameters. */
1186 gcc_assert (TMPL_ARGS_DEPTH (args)
1187 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1188 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1189 : template_class_depth (DECL_CONTEXT (tmpl))));
1191 if (flag_checking)
1192 verify_unstripped_args (args);
1194 /* Lambda functions in templates aren't instantiated normally, but through
1195 tsubst_lambda_expr. */
1196 if (lambda_fn_in_template_p (tmpl))
1197 return NULL_TREE;
1199 if (optimize_specialization_lookup_p (tmpl))
1201 /* The template arguments actually apply to the containing
1202 class. Find the class specialization with those
1203 arguments. */
1204 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1205 tree class_specialization
1206 = retrieve_specialization (class_template, args, 0);
1207 if (!class_specialization)
1208 return NULL_TREE;
1210 /* Find the instance of TMPL. */
1211 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1212 for (ovl_iterator iter (fns); iter; ++iter)
1214 tree fn = *iter;
1215 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1216 /* using-declarations can add base methods to the method vec,
1217 and we don't want those here. */
1218 && DECL_CONTEXT (fn) == class_specialization)
1219 return fn;
1221 return NULL_TREE;
1223 else
1225 spec_entry *found;
1226 spec_entry elt;
1227 hash_table<spec_hasher> *specializations;
1229 elt.tmpl = tmpl;
1230 elt.args = args;
1231 elt.spec = NULL_TREE;
1233 if (DECL_CLASS_TEMPLATE_P (tmpl))
1234 specializations = type_specializations;
1235 else
1236 specializations = decl_specializations;
1238 if (hash == 0)
1239 hash = spec_hasher::hash (&elt);
1240 found = specializations->find_with_hash (&elt, hash);
1241 if (found)
1242 return found->spec;
1245 return NULL_TREE;
1248 /* Like retrieve_specialization, but for local declarations. */
1250 tree
1251 retrieve_local_specialization (tree tmpl)
1253 if (local_specializations == NULL)
1254 return NULL_TREE;
1256 tree *slot = local_specializations->get (tmpl);
1257 return slot ? *slot : NULL_TREE;
1260 /* Returns nonzero iff DECL is a specialization of TMPL. */
1263 is_specialization_of (tree decl, tree tmpl)
1265 tree t;
1267 if (TREE_CODE (decl) == FUNCTION_DECL)
1269 for (t = decl;
1270 t != NULL_TREE;
1271 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1272 if (t == tmpl)
1273 return 1;
1275 else
1277 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1279 for (t = TREE_TYPE (decl);
1280 t != NULL_TREE;
1281 t = CLASSTYPE_USE_TEMPLATE (t)
1282 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1283 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1284 return 1;
1287 return 0;
1290 /* Returns nonzero iff DECL is a specialization of friend declaration
1291 FRIEND_DECL according to [temp.friend]. */
1293 bool
1294 is_specialization_of_friend (tree decl, tree friend_decl)
1296 bool need_template = true;
1297 int template_depth;
1299 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1300 || TREE_CODE (decl) == TYPE_DECL);
1302 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1303 of a template class, we want to check if DECL is a specialization
1304 if this. */
1305 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1306 && DECL_TEMPLATE_INFO (friend_decl)
1307 && !DECL_USE_TEMPLATE (friend_decl))
1309 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1310 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1311 need_template = false;
1313 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1314 && !PRIMARY_TEMPLATE_P (friend_decl))
1315 need_template = false;
1317 /* There is nothing to do if this is not a template friend. */
1318 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1319 return false;
1321 if (is_specialization_of (decl, friend_decl))
1322 return true;
1324 /* [temp.friend/6]
1325 A member of a class template may be declared to be a friend of a
1326 non-template class. In this case, the corresponding member of
1327 every specialization of the class template is a friend of the
1328 class granting friendship.
1330 For example, given a template friend declaration
1332 template <class T> friend void A<T>::f();
1334 the member function below is considered a friend
1336 template <> struct A<int> {
1337 void f();
1340 For this type of template friend, TEMPLATE_DEPTH below will be
1341 nonzero. To determine if DECL is a friend of FRIEND, we first
1342 check if the enclosing class is a specialization of another. */
1344 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1345 if (template_depth
1346 && DECL_CLASS_SCOPE_P (decl)
1347 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1348 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1350 /* Next, we check the members themselves. In order to handle
1351 a few tricky cases, such as when FRIEND_DECL's are
1353 template <class T> friend void A<T>::g(T t);
1354 template <class T> template <T t> friend void A<T>::h();
1356 and DECL's are
1358 void A<int>::g(int);
1359 template <int> void A<int>::h();
1361 we need to figure out ARGS, the template arguments from
1362 the context of DECL. This is required for template substitution
1363 of `T' in the function parameter of `g' and template parameter
1364 of `h' in the above examples. Here ARGS corresponds to `int'. */
1366 tree context = DECL_CONTEXT (decl);
1367 tree args = NULL_TREE;
1368 int current_depth = 0;
1370 while (current_depth < template_depth)
1372 if (CLASSTYPE_TEMPLATE_INFO (context))
1374 if (current_depth == 0)
1375 args = TYPE_TI_ARGS (context);
1376 else
1377 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1378 current_depth++;
1380 context = TYPE_CONTEXT (context);
1383 if (TREE_CODE (decl) == FUNCTION_DECL)
1385 bool is_template;
1386 tree friend_type;
1387 tree decl_type;
1388 tree friend_args_type;
1389 tree decl_args_type;
1391 /* Make sure that both DECL and FRIEND_DECL are templates or
1392 non-templates. */
1393 is_template = DECL_TEMPLATE_INFO (decl)
1394 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1395 if (need_template ^ is_template)
1396 return false;
1397 else if (is_template)
1399 /* If both are templates, check template parameter list. */
1400 tree friend_parms
1401 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1402 args, tf_none);
1403 if (!comp_template_parms
1404 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1405 friend_parms))
1406 return false;
1408 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1410 else
1411 decl_type = TREE_TYPE (decl);
1413 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1414 tf_none, NULL_TREE);
1415 if (friend_type == error_mark_node)
1416 return false;
1418 /* Check if return types match. */
1419 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1420 return false;
1422 /* Check if function parameter types match, ignoring the
1423 `this' parameter. */
1424 friend_args_type = TYPE_ARG_TYPES (friend_type);
1425 decl_args_type = TYPE_ARG_TYPES (decl_type);
1426 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1427 friend_args_type = TREE_CHAIN (friend_args_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1429 decl_args_type = TREE_CHAIN (decl_args_type);
1431 return compparms (decl_args_type, friend_args_type);
1433 else
1435 /* DECL is a TYPE_DECL */
1436 bool is_template;
1437 tree decl_type = TREE_TYPE (decl);
1439 /* Make sure that both DECL and FRIEND_DECL are templates or
1440 non-templates. */
1441 is_template
1442 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1443 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1445 if (need_template ^ is_template)
1446 return false;
1447 else if (is_template)
1449 tree friend_parms;
1450 /* If both are templates, check the name of the two
1451 TEMPLATE_DECL's first because is_friend didn't. */
1452 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1453 != DECL_NAME (friend_decl))
1454 return false;
1456 /* Now check template parameter list. */
1457 friend_parms
1458 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1459 args, tf_none);
1460 return comp_template_parms
1461 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1462 friend_parms);
1464 else
1465 return (DECL_NAME (decl)
1466 == DECL_NAME (friend_decl));
1469 return false;
1472 /* Register the specialization SPEC as a specialization of TMPL with
1473 the indicated ARGS. IS_FRIEND indicates whether the specialization
1474 is actually just a friend declaration. Returns SPEC, or an
1475 equivalent prior declaration, if available.
1477 We also store instantiations of field packs in the hash table, even
1478 though they are not themselves templates, to make lookup easier. */
1480 static tree
1481 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1482 hashval_t hash)
1484 tree fn;
1485 spec_entry **slot = NULL;
1486 spec_entry elt;
1488 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1489 || (TREE_CODE (tmpl) == FIELD_DECL
1490 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1492 if (TREE_CODE (spec) == FUNCTION_DECL
1493 && uses_template_parms (DECL_TI_ARGS (spec)))
1494 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1495 register it; we want the corresponding TEMPLATE_DECL instead.
1496 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1497 the more obvious `uses_template_parms (spec)' to avoid problems
1498 with default function arguments. In particular, given
1499 something like this:
1501 template <class T> void f(T t1, T t = T())
1503 the default argument expression is not substituted for in an
1504 instantiation unless and until it is actually needed. */
1505 return spec;
1507 if (optimize_specialization_lookup_p (tmpl))
1508 /* We don't put these specializations in the hash table, but we might
1509 want to give an error about a mismatch. */
1510 fn = retrieve_specialization (tmpl, args, 0);
1511 else
1513 elt.tmpl = tmpl;
1514 elt.args = args;
1515 elt.spec = spec;
1517 if (hash == 0)
1518 hash = spec_hasher::hash (&elt);
1520 slot =
1521 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1522 if (*slot)
1523 fn = ((spec_entry *) *slot)->spec;
1524 else
1525 fn = NULL_TREE;
1528 /* We can sometimes try to re-register a specialization that we've
1529 already got. In particular, regenerate_decl_from_template calls
1530 duplicate_decls which will update the specialization list. But,
1531 we'll still get called again here anyhow. It's more convenient
1532 to simply allow this than to try to prevent it. */
1533 if (fn == spec)
1534 return spec;
1535 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1537 if (DECL_TEMPLATE_INSTANTIATION (fn))
1539 if (DECL_ODR_USED (fn)
1540 || DECL_EXPLICIT_INSTANTIATION (fn))
1542 error ("specialization of %qD after instantiation",
1543 fn);
1544 return error_mark_node;
1546 else
1548 tree clone;
1549 /* This situation should occur only if the first
1550 specialization is an implicit instantiation, the
1551 second is an explicit specialization, and the
1552 implicit instantiation has not yet been used. That
1553 situation can occur if we have implicitly
1554 instantiated a member function and then specialized
1555 it later.
1557 We can also wind up here if a friend declaration that
1558 looked like an instantiation turns out to be a
1559 specialization:
1561 template <class T> void foo(T);
1562 class S { friend void foo<>(int) };
1563 template <> void foo(int);
1565 We transform the existing DECL in place so that any
1566 pointers to it become pointers to the updated
1567 declaration.
1569 If there was a definition for the template, but not
1570 for the specialization, we want this to look as if
1571 there were no definition, and vice versa. */
1572 DECL_INITIAL (fn) = NULL_TREE;
1573 duplicate_decls (spec, fn, is_friend);
1574 /* The call to duplicate_decls will have applied
1575 [temp.expl.spec]:
1577 An explicit specialization of a function template
1578 is inline only if it is explicitly declared to be,
1579 and independently of whether its function template
1582 to the primary function; now copy the inline bits to
1583 the various clones. */
1584 FOR_EACH_CLONE (clone, fn)
1586 DECL_DECLARED_INLINE_P (clone)
1587 = DECL_DECLARED_INLINE_P (fn);
1588 DECL_SOURCE_LOCATION (clone)
1589 = DECL_SOURCE_LOCATION (fn);
1590 DECL_DELETED_FN (clone)
1591 = DECL_DELETED_FN (fn);
1593 check_specialization_namespace (tmpl);
1595 return fn;
1598 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1600 tree dd = duplicate_decls (spec, fn, is_friend);
1601 if (dd == error_mark_node)
1602 /* We've already complained in duplicate_decls. */
1603 return error_mark_node;
1605 if (dd == NULL_TREE && DECL_INITIAL (spec))
1606 /* Dup decl failed, but this is a new definition. Set the
1607 line number so any errors match this new
1608 definition. */
1609 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1611 return fn;
1614 else if (fn)
1615 return duplicate_decls (spec, fn, is_friend);
1617 /* A specialization must be declared in the same namespace as the
1618 template it is specializing. */
1619 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1620 && !check_specialization_namespace (tmpl))
1621 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1623 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1625 spec_entry *entry = ggc_alloc<spec_entry> ();
1626 gcc_assert (tmpl && args && spec);
1627 *entry = elt;
1628 *slot = entry;
1629 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1630 && PRIMARY_TEMPLATE_P (tmpl)
1631 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1632 || variable_template_p (tmpl))
1633 /* If TMPL is a forward declaration of a template function, keep a list
1634 of all specializations in case we need to reassign them to a friend
1635 template later in tsubst_friend_function.
1637 Also keep a list of all variable template instantiations so that
1638 process_partial_specialization can check whether a later partial
1639 specialization would have used it. */
1640 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1641 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1644 return spec;
1647 /* Returns true iff two spec_entry nodes are equivalent. */
1649 int comparing_specializations;
1651 bool
1652 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1654 int equal;
1656 ++comparing_specializations;
1657 equal = (e1->tmpl == e2->tmpl
1658 && comp_template_args (e1->args, e2->args));
1659 if (equal && flag_concepts
1660 /* tmpl could be a FIELD_DECL for a capture pack. */
1661 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1662 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1663 && uses_template_parms (e1->args))
1665 /* Partial specializations of a variable template can be distinguished by
1666 constraints. */
1667 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1668 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1669 equal = equivalent_constraints (c1, c2);
1671 --comparing_specializations;
1673 return equal;
1676 /* Returns a hash for a template TMPL and template arguments ARGS. */
1678 static hashval_t
1679 hash_tmpl_and_args (tree tmpl, tree args)
1681 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1682 return iterative_hash_template_arg (args, val);
1685 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1686 ignoring SPEC. */
1688 hashval_t
1689 spec_hasher::hash (spec_entry *e)
1691 return hash_tmpl_and_args (e->tmpl, e->args);
1694 /* Recursively calculate a hash value for a template argument ARG, for use
1695 in the hash tables of template specializations. */
1697 hashval_t
1698 iterative_hash_template_arg (tree arg, hashval_t val)
1700 unsigned HOST_WIDE_INT i;
1701 enum tree_code code;
1702 char tclass;
1704 if (arg == NULL_TREE)
1705 return iterative_hash_object (arg, val);
1707 if (!TYPE_P (arg))
1708 STRIP_NOPS (arg);
1710 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1711 gcc_unreachable ();
1713 code = TREE_CODE (arg);
1714 tclass = TREE_CODE_CLASS (code);
1716 val = iterative_hash_object (code, val);
1718 switch (code)
1720 case ERROR_MARK:
1721 return val;
1723 case IDENTIFIER_NODE:
1724 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1726 case TREE_VEC:
1728 int i, len = TREE_VEC_LENGTH (arg);
1729 for (i = 0; i < len; ++i)
1730 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1731 return val;
1734 case TYPE_PACK_EXPANSION:
1735 case EXPR_PACK_EXPANSION:
1736 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1737 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1739 case TYPE_ARGUMENT_PACK:
1740 case NONTYPE_ARGUMENT_PACK:
1741 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1743 case TREE_LIST:
1744 for (; arg; arg = TREE_CHAIN (arg))
1745 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1746 return val;
1748 case OVERLOAD:
1749 for (lkp_iterator iter (arg); iter; ++iter)
1750 val = iterative_hash_template_arg (*iter, val);
1751 return val;
1753 case CONSTRUCTOR:
1755 tree field, value;
1756 iterative_hash_template_arg (TREE_TYPE (arg), val);
1757 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1759 val = iterative_hash_template_arg (field, val);
1760 val = iterative_hash_template_arg (value, val);
1762 return val;
1765 case PARM_DECL:
1766 if (!DECL_ARTIFICIAL (arg))
1768 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1769 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1771 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1773 case TARGET_EXPR:
1774 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1776 case PTRMEM_CST:
1777 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1778 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1780 case TEMPLATE_PARM_INDEX:
1781 val = iterative_hash_template_arg
1782 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1783 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1784 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1786 case TRAIT_EXPR:
1787 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1788 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1789 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1791 case BASELINK:
1792 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1793 val);
1794 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1795 val);
1797 case MODOP_EXPR:
1798 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1799 code = TREE_CODE (TREE_OPERAND (arg, 1));
1800 val = iterative_hash_object (code, val);
1801 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1803 case LAMBDA_EXPR:
1804 /* A lambda can't appear in a template arg, but don't crash on
1805 erroneous input. */
1806 gcc_assert (seen_error ());
1807 return val;
1809 case CAST_EXPR:
1810 case IMPLICIT_CONV_EXPR:
1811 case STATIC_CAST_EXPR:
1812 case REINTERPRET_CAST_EXPR:
1813 case CONST_CAST_EXPR:
1814 case DYNAMIC_CAST_EXPR:
1815 case NEW_EXPR:
1816 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1817 /* Now hash operands as usual. */
1818 break;
1820 default:
1821 break;
1824 switch (tclass)
1826 case tcc_type:
1827 if (alias_template_specialization_p (arg))
1829 // We want an alias specialization that survived strip_typedefs
1830 // to hash differently from its TYPE_CANONICAL, to avoid hash
1831 // collisions that compare as different in template_args_equal.
1832 // These could be dependent specializations that strip_typedefs
1833 // left alone, or untouched specializations because
1834 // coerce_template_parms returns the unconverted template
1835 // arguments if it sees incomplete argument packs.
1836 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1837 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1839 if (TYPE_CANONICAL (arg))
1840 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1841 val);
1842 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1843 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1844 /* Otherwise just compare the types during lookup. */
1845 return val;
1847 case tcc_declaration:
1848 case tcc_constant:
1849 return iterative_hash_expr (arg, val);
1851 default:
1852 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1854 unsigned n = cp_tree_operand_length (arg);
1855 for (i = 0; i < n; ++i)
1856 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1857 return val;
1860 gcc_unreachable ();
1861 return 0;
1864 /* Unregister the specialization SPEC as a specialization of TMPL.
1865 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1866 if the SPEC was listed as a specialization of TMPL.
1868 Note that SPEC has been ggc_freed, so we can't look inside it. */
1870 bool
1871 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1873 spec_entry *entry;
1874 spec_entry elt;
1876 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1877 elt.args = TI_ARGS (tinfo);
1878 elt.spec = NULL_TREE;
1880 entry = decl_specializations->find (&elt);
1881 if (entry != NULL)
1883 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1884 gcc_assert (new_spec != NULL_TREE);
1885 entry->spec = new_spec;
1886 return 1;
1889 return 0;
1892 /* Like register_specialization, but for local declarations. We are
1893 registering SPEC, an instantiation of TMPL. */
1895 void
1896 register_local_specialization (tree spec, tree tmpl)
1898 local_specializations->put (tmpl, spec);
1901 /* TYPE is a class type. Returns true if TYPE is an explicitly
1902 specialized class. */
1904 bool
1905 explicit_class_specialization_p (tree type)
1907 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1908 return false;
1909 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1912 /* Print the list of functions at FNS, going through all the overloads
1913 for each element of the list. Alternatively, FNS can not be a
1914 TREE_LIST, in which case it will be printed together with all the
1915 overloads.
1917 MORE and *STR should respectively be FALSE and NULL when the function
1918 is called from the outside. They are used internally on recursive
1919 calls. print_candidates manages the two parameters and leaves NULL
1920 in *STR when it ends. */
1922 static void
1923 print_candidates_1 (tree fns, char **str, bool more = false)
1925 if (TREE_CODE (fns) == TREE_LIST)
1926 for (; fns; fns = TREE_CHAIN (fns))
1927 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1928 else
1929 for (lkp_iterator iter (fns); iter;)
1931 tree cand = *iter;
1932 ++iter;
1934 const char *pfx = *str;
1935 if (!pfx)
1937 if (more || iter)
1938 pfx = _("candidates are:");
1939 else
1940 pfx = _("candidate is:");
1941 *str = get_spaces (pfx);
1943 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1947 /* Print the list of candidate FNS in an error message. FNS can also
1948 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1950 void
1951 print_candidates (tree fns)
1953 char *str = NULL;
1954 print_candidates_1 (fns, &str);
1955 free (str);
1958 /* Get a (possibly) constrained template declaration for the
1959 purpose of ordering candidates. */
1960 static tree
1961 get_template_for_ordering (tree list)
1963 gcc_assert (TREE_CODE (list) == TREE_LIST);
1964 tree f = TREE_VALUE (list);
1965 if (tree ti = DECL_TEMPLATE_INFO (f))
1966 return TI_TEMPLATE (ti);
1967 return f;
1970 /* Among candidates having the same signature, return the
1971 most constrained or NULL_TREE if there is no best candidate.
1972 If the signatures of candidates vary (e.g., template
1973 specialization vs. member function), then there can be no
1974 most constrained.
1976 Note that we don't compare constraints on the functions
1977 themselves, but rather those of their templates. */
1978 static tree
1979 most_constrained_function (tree candidates)
1981 // Try to find the best candidate in a first pass.
1982 tree champ = candidates;
1983 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1985 int winner = more_constrained (get_template_for_ordering (champ),
1986 get_template_for_ordering (c));
1987 if (winner == -1)
1988 champ = c; // The candidate is more constrained
1989 else if (winner == 0)
1990 return NULL_TREE; // Neither is more constrained
1993 // Verify that the champ is better than previous candidates.
1994 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1995 if (!more_constrained (get_template_for_ordering (champ),
1996 get_template_for_ordering (c)))
1997 return NULL_TREE;
2000 return champ;
2004 /* Returns the template (one of the functions given by TEMPLATE_ID)
2005 which can be specialized to match the indicated DECL with the
2006 explicit template args given in TEMPLATE_ID. The DECL may be
2007 NULL_TREE if none is available. In that case, the functions in
2008 TEMPLATE_ID are non-members.
2010 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2011 specialization of a member template.
2013 The TEMPLATE_COUNT is the number of references to qualifying
2014 template classes that appeared in the name of the function. See
2015 check_explicit_specialization for a more accurate description.
2017 TSK indicates what kind of template declaration (if any) is being
2018 declared. TSK_TEMPLATE indicates that the declaration given by
2019 DECL, though a FUNCTION_DECL, has template parameters, and is
2020 therefore a template function.
2022 The template args (those explicitly specified and those deduced)
2023 are output in a newly created vector *TARGS_OUT.
2025 If it is impossible to determine the result, an error message is
2026 issued. The error_mark_node is returned to indicate failure. */
2028 static tree
2029 determine_specialization (tree template_id,
2030 tree decl,
2031 tree* targs_out,
2032 int need_member_template,
2033 int template_count,
2034 tmpl_spec_kind tsk)
2036 tree fns;
2037 tree targs;
2038 tree explicit_targs;
2039 tree candidates = NULL_TREE;
2041 /* A TREE_LIST of templates of which DECL may be a specialization.
2042 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2043 corresponding TREE_PURPOSE is the set of template arguments that,
2044 when used to instantiate the template, would produce a function
2045 with the signature of DECL. */
2046 tree templates = NULL_TREE;
2047 int header_count;
2048 cp_binding_level *b;
2050 *targs_out = NULL_TREE;
2052 if (template_id == error_mark_node || decl == error_mark_node)
2053 return error_mark_node;
2055 /* We shouldn't be specializing a member template of an
2056 unspecialized class template; we already gave an error in
2057 check_specialization_scope, now avoid crashing. */
2058 if (template_count && DECL_CLASS_SCOPE_P (decl)
2059 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2061 gcc_assert (errorcount);
2062 return error_mark_node;
2065 fns = TREE_OPERAND (template_id, 0);
2066 explicit_targs = TREE_OPERAND (template_id, 1);
2068 if (fns == error_mark_node)
2069 return error_mark_node;
2071 /* Check for baselinks. */
2072 if (BASELINK_P (fns))
2073 fns = BASELINK_FUNCTIONS (fns);
2075 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2077 error ("%qD is not a function template", fns);
2078 return error_mark_node;
2080 else if (VAR_P (decl) && !variable_template_p (fns))
2082 error ("%qD is not a variable template", fns);
2083 return error_mark_node;
2086 /* Count the number of template headers specified for this
2087 specialization. */
2088 header_count = 0;
2089 for (b = current_binding_level;
2090 b->kind == sk_template_parms;
2091 b = b->level_chain)
2092 ++header_count;
2094 tree orig_fns = fns;
2096 if (variable_template_p (fns))
2098 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2099 targs = coerce_template_parms (parms, explicit_targs, fns,
2100 tf_warning_or_error,
2101 /*req_all*/true, /*use_defarg*/true);
2102 if (targs != error_mark_node)
2103 templates = tree_cons (targs, fns, templates);
2105 else for (lkp_iterator iter (fns); iter; ++iter)
2107 tree fn = *iter;
2109 if (TREE_CODE (fn) == TEMPLATE_DECL)
2111 tree decl_arg_types;
2112 tree fn_arg_types;
2113 tree insttype;
2115 /* In case of explicit specialization, we need to check if
2116 the number of template headers appearing in the specialization
2117 is correct. This is usually done in check_explicit_specialization,
2118 but the check done there cannot be exhaustive when specializing
2119 member functions. Consider the following code:
2121 template <> void A<int>::f(int);
2122 template <> template <> void A<int>::f(int);
2124 Assuming that A<int> is not itself an explicit specialization
2125 already, the first line specializes "f" which is a non-template
2126 member function, whilst the second line specializes "f" which
2127 is a template member function. So both lines are syntactically
2128 correct, and check_explicit_specialization does not reject
2129 them.
2131 Here, we can do better, as we are matching the specialization
2132 against the declarations. We count the number of template
2133 headers, and we check if they match TEMPLATE_COUNT + 1
2134 (TEMPLATE_COUNT is the number of qualifying template classes,
2135 plus there must be another header for the member template
2136 itself).
2138 Notice that if header_count is zero, this is not a
2139 specialization but rather a template instantiation, so there
2140 is no check we can perform here. */
2141 if (header_count && header_count != template_count + 1)
2142 continue;
2144 /* Check that the number of template arguments at the
2145 innermost level for DECL is the same as for FN. */
2146 if (current_binding_level->kind == sk_template_parms
2147 && !current_binding_level->explicit_spec_p
2148 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2149 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2150 (current_template_parms))))
2151 continue;
2153 /* DECL might be a specialization of FN. */
2154 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2155 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2157 /* For a non-static member function, we need to make sure
2158 that the const qualification is the same. Since
2159 get_bindings does not try to merge the "this" parameter,
2160 we must do the comparison explicitly. */
2161 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2162 && !same_type_p (TREE_VALUE (fn_arg_types),
2163 TREE_VALUE (decl_arg_types)))
2164 continue;
2166 /* Skip the "this" parameter and, for constructors of
2167 classes with virtual bases, the VTT parameter. A
2168 full specialization of a constructor will have a VTT
2169 parameter, but a template never will. */
2170 decl_arg_types
2171 = skip_artificial_parms_for (decl, decl_arg_types);
2172 fn_arg_types
2173 = skip_artificial_parms_for (fn, fn_arg_types);
2175 /* Function templates cannot be specializations; there are
2176 no partial specializations of functions. Therefore, if
2177 the type of DECL does not match FN, there is no
2178 match.
2180 Note that it should never be the case that we have both
2181 candidates added here, and for regular member functions
2182 below. */
2183 if (tsk == tsk_template)
2185 if (compparms (fn_arg_types, decl_arg_types))
2186 candidates = tree_cons (NULL_TREE, fn, candidates);
2187 continue;
2190 /* See whether this function might be a specialization of this
2191 template. Suppress access control because we might be trying
2192 to make this specialization a friend, and we have already done
2193 access control for the declaration of the specialization. */
2194 push_deferring_access_checks (dk_no_check);
2195 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2196 pop_deferring_access_checks ();
2198 if (!targs)
2199 /* We cannot deduce template arguments that when used to
2200 specialize TMPL will produce DECL. */
2201 continue;
2203 /* Remove, from the set of candidates, all those functions
2204 whose constraints are not satisfied. */
2205 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2206 continue;
2208 // Then, try to form the new function type.
2209 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2210 if (insttype == error_mark_node)
2211 continue;
2212 fn_arg_types
2213 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2214 if (!compparms (fn_arg_types, decl_arg_types))
2215 continue;
2217 /* Save this template, and the arguments deduced. */
2218 templates = tree_cons (targs, fn, templates);
2220 else if (need_member_template)
2221 /* FN is an ordinary member function, and we need a
2222 specialization of a member template. */
2224 else if (TREE_CODE (fn) != FUNCTION_DECL)
2225 /* We can get IDENTIFIER_NODEs here in certain erroneous
2226 cases. */
2228 else if (!DECL_FUNCTION_MEMBER_P (fn))
2229 /* This is just an ordinary non-member function. Nothing can
2230 be a specialization of that. */
2232 else if (DECL_ARTIFICIAL (fn))
2233 /* Cannot specialize functions that are created implicitly. */
2235 else
2237 tree decl_arg_types;
2239 /* This is an ordinary member function. However, since
2240 we're here, we can assume its enclosing class is a
2241 template class. For example,
2243 template <typename T> struct S { void f(); };
2244 template <> void S<int>::f() {}
2246 Here, S<int>::f is a non-template, but S<int> is a
2247 template class. If FN has the same type as DECL, we
2248 might be in business. */
2250 if (!DECL_TEMPLATE_INFO (fn))
2251 /* Its enclosing class is an explicit specialization
2252 of a template class. This is not a candidate. */
2253 continue;
2255 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2256 TREE_TYPE (TREE_TYPE (fn))))
2257 /* The return types differ. */
2258 continue;
2260 /* Adjust the type of DECL in case FN is a static member. */
2261 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2262 if (DECL_STATIC_FUNCTION_P (fn)
2263 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2264 decl_arg_types = TREE_CHAIN (decl_arg_types);
2266 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2267 decl_arg_types))
2268 continue;
2270 // If the deduced arguments do not satisfy the constraints,
2271 // this is not a candidate.
2272 if (flag_concepts && !constraints_satisfied_p (fn))
2273 continue;
2275 // Add the candidate.
2276 candidates = tree_cons (NULL_TREE, fn, candidates);
2280 if (templates && TREE_CHAIN (templates))
2282 /* We have:
2284 [temp.expl.spec]
2286 It is possible for a specialization with a given function
2287 signature to be instantiated from more than one function
2288 template. In such cases, explicit specification of the
2289 template arguments must be used to uniquely identify the
2290 function template specialization being specialized.
2292 Note that here, there's no suggestion that we're supposed to
2293 determine which of the candidate templates is most
2294 specialized. However, we, also have:
2296 [temp.func.order]
2298 Partial ordering of overloaded function template
2299 declarations is used in the following contexts to select
2300 the function template to which a function template
2301 specialization refers:
2303 -- when an explicit specialization refers to a function
2304 template.
2306 So, we do use the partial ordering rules, at least for now.
2307 This extension can only serve to make invalid programs valid,
2308 so it's safe. And, there is strong anecdotal evidence that
2309 the committee intended the partial ordering rules to apply;
2310 the EDG front end has that behavior, and John Spicer claims
2311 that the committee simply forgot to delete the wording in
2312 [temp.expl.spec]. */
2313 tree tmpl = most_specialized_instantiation (templates);
2314 if (tmpl != error_mark_node)
2316 templates = tmpl;
2317 TREE_CHAIN (templates) = NULL_TREE;
2321 // Concepts allows multiple declarations of member functions
2322 // with the same signature. Like above, we need to rely on
2323 // on the partial ordering of those candidates to determine which
2324 // is the best.
2325 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2327 if (tree cand = most_constrained_function (candidates))
2329 candidates = cand;
2330 TREE_CHAIN (cand) = NULL_TREE;
2334 if (templates == NULL_TREE && candidates == NULL_TREE)
2336 error ("template-id %qD for %q+D does not match any template "
2337 "declaration", template_id, decl);
2338 if (header_count && header_count != template_count + 1)
2339 inform (input_location, "saw %d %<template<>%>, need %d for "
2340 "specializing a member function template",
2341 header_count, template_count + 1);
2342 else
2343 print_candidates (orig_fns);
2344 return error_mark_node;
2346 else if ((templates && TREE_CHAIN (templates))
2347 || (candidates && TREE_CHAIN (candidates))
2348 || (templates && candidates))
2350 error ("ambiguous template specialization %qD for %q+D",
2351 template_id, decl);
2352 candidates = chainon (candidates, templates);
2353 print_candidates (candidates);
2354 return error_mark_node;
2357 /* We have one, and exactly one, match. */
2358 if (candidates)
2360 tree fn = TREE_VALUE (candidates);
2361 *targs_out = copy_node (DECL_TI_ARGS (fn));
2363 // Propagate the candidate's constraints to the declaration.
2364 set_constraints (decl, get_constraints (fn));
2366 /* DECL is a re-declaration or partial instantiation of a template
2367 function. */
2368 if (TREE_CODE (fn) == TEMPLATE_DECL)
2369 return fn;
2370 /* It was a specialization of an ordinary member function in a
2371 template class. */
2372 return DECL_TI_TEMPLATE (fn);
2375 /* It was a specialization of a template. */
2376 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2377 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2379 *targs_out = copy_node (targs);
2380 SET_TMPL_ARGS_LEVEL (*targs_out,
2381 TMPL_ARGS_DEPTH (*targs_out),
2382 TREE_PURPOSE (templates));
2384 else
2385 *targs_out = TREE_PURPOSE (templates);
2386 return TREE_VALUE (templates);
2389 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2390 but with the default argument values filled in from those in the
2391 TMPL_TYPES. */
2393 static tree
2394 copy_default_args_to_explicit_spec_1 (tree spec_types,
2395 tree tmpl_types)
2397 tree new_spec_types;
2399 if (!spec_types)
2400 return NULL_TREE;
2402 if (spec_types == void_list_node)
2403 return void_list_node;
2405 /* Substitute into the rest of the list. */
2406 new_spec_types =
2407 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2408 TREE_CHAIN (tmpl_types));
2410 /* Add the default argument for this parameter. */
2411 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2412 TREE_VALUE (spec_types),
2413 new_spec_types);
2416 /* DECL is an explicit specialization. Replicate default arguments
2417 from the template it specializes. (That way, code like:
2419 template <class T> void f(T = 3);
2420 template <> void f(double);
2421 void g () { f (); }
2423 works, as required.) An alternative approach would be to look up
2424 the correct default arguments at the call-site, but this approach
2425 is consistent with how implicit instantiations are handled. */
2427 static void
2428 copy_default_args_to_explicit_spec (tree decl)
2430 tree tmpl;
2431 tree spec_types;
2432 tree tmpl_types;
2433 tree new_spec_types;
2434 tree old_type;
2435 tree new_type;
2436 tree t;
2437 tree object_type = NULL_TREE;
2438 tree in_charge = NULL_TREE;
2439 tree vtt = NULL_TREE;
2441 /* See if there's anything we need to do. */
2442 tmpl = DECL_TI_TEMPLATE (decl);
2443 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2444 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2445 if (TREE_PURPOSE (t))
2446 break;
2447 if (!t)
2448 return;
2450 old_type = TREE_TYPE (decl);
2451 spec_types = TYPE_ARG_TYPES (old_type);
2453 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2455 /* Remove the this pointer, but remember the object's type for
2456 CV quals. */
2457 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2458 spec_types = TREE_CHAIN (spec_types);
2459 tmpl_types = TREE_CHAIN (tmpl_types);
2461 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2463 /* DECL may contain more parameters than TMPL due to the extra
2464 in-charge parameter in constructors and destructors. */
2465 in_charge = spec_types;
2466 spec_types = TREE_CHAIN (spec_types);
2468 if (DECL_HAS_VTT_PARM_P (decl))
2470 vtt = spec_types;
2471 spec_types = TREE_CHAIN (spec_types);
2475 /* Compute the merged default arguments. */
2476 new_spec_types =
2477 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2479 /* Compute the new FUNCTION_TYPE. */
2480 if (object_type)
2482 if (vtt)
2483 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2484 TREE_VALUE (vtt),
2485 new_spec_types);
2487 if (in_charge)
2488 /* Put the in-charge parameter back. */
2489 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2490 TREE_VALUE (in_charge),
2491 new_spec_types);
2493 new_type = build_method_type_directly (object_type,
2494 TREE_TYPE (old_type),
2495 new_spec_types);
2497 else
2498 new_type = build_function_type (TREE_TYPE (old_type),
2499 new_spec_types);
2500 new_type = cp_build_type_attribute_variant (new_type,
2501 TYPE_ATTRIBUTES (old_type));
2502 new_type = build_exception_variant (new_type,
2503 TYPE_RAISES_EXCEPTIONS (old_type));
2505 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2506 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2508 TREE_TYPE (decl) = new_type;
2511 /* Return the number of template headers we expect to see for a definition
2512 or specialization of CTYPE or one of its non-template members. */
2515 num_template_headers_for_class (tree ctype)
2517 int num_templates = 0;
2519 while (ctype && CLASS_TYPE_P (ctype))
2521 /* You're supposed to have one `template <...>' for every
2522 template class, but you don't need one for a full
2523 specialization. For example:
2525 template <class T> struct S{};
2526 template <> struct S<int> { void f(); };
2527 void S<int>::f () {}
2529 is correct; there shouldn't be a `template <>' for the
2530 definition of `S<int>::f'. */
2531 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2532 /* If CTYPE does not have template information of any
2533 kind, then it is not a template, nor is it nested
2534 within a template. */
2535 break;
2536 if (explicit_class_specialization_p (ctype))
2537 break;
2538 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2539 ++num_templates;
2541 ctype = TYPE_CONTEXT (ctype);
2544 return num_templates;
2547 /* Do a simple sanity check on the template headers that precede the
2548 variable declaration DECL. */
2550 void
2551 check_template_variable (tree decl)
2553 tree ctx = CP_DECL_CONTEXT (decl);
2554 int wanted = num_template_headers_for_class (ctx);
2555 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2556 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2558 if (cxx_dialect < cxx14)
2559 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2560 "variable templates only available with "
2561 "-std=c++14 or -std=gnu++14");
2563 // Namespace-scope variable templates should have a template header.
2564 ++wanted;
2566 if (template_header_count > wanted)
2568 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2569 "too many template headers for %qD "
2570 "(should be %d)",
2571 decl, wanted);
2572 if (warned && CLASS_TYPE_P (ctx)
2573 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2574 inform (DECL_SOURCE_LOCATION (decl),
2575 "members of an explicitly specialized class are defined "
2576 "without a template header");
2580 /* An explicit specialization whose declarator-id or class-head-name is not
2581 qualified shall be declared in the nearest enclosing namespace of the
2582 template, or, if the namespace is inline (7.3.1), any namespace from its
2583 enclosing namespace set.
2585 If the name declared in the explicit instantiation is an unqualified name,
2586 the explicit instantiation shall appear in the namespace where its template
2587 is declared or, if that namespace is inline (7.3.1), any namespace from its
2588 enclosing namespace set. */
2590 void
2591 check_unqualified_spec_or_inst (tree t, location_t loc)
2593 tree tmpl = most_general_template (t);
2594 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2595 && !is_nested_namespace (current_namespace,
2596 CP_DECL_CONTEXT (tmpl), true))
2598 if (processing_specialization)
2599 permerror (loc, "explicit specialization of %qD outside its "
2600 "namespace must use a nested-name-specifier", tmpl);
2601 else if (processing_explicit_instantiation
2602 && cxx_dialect >= cxx11)
2603 /* This was allowed in C++98, so only pedwarn. */
2604 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2605 "outside its namespace must use a nested-name-"
2606 "specifier", tmpl);
2610 /* Check to see if the function just declared, as indicated in
2611 DECLARATOR, and in DECL, is a specialization of a function
2612 template. We may also discover that the declaration is an explicit
2613 instantiation at this point.
2615 Returns DECL, or an equivalent declaration that should be used
2616 instead if all goes well. Issues an error message if something is
2617 amiss. Returns error_mark_node if the error is not easily
2618 recoverable.
2620 FLAGS is a bitmask consisting of the following flags:
2622 2: The function has a definition.
2623 4: The function is a friend.
2625 The TEMPLATE_COUNT is the number of references to qualifying
2626 template classes that appeared in the name of the function. For
2627 example, in
2629 template <class T> struct S { void f(); };
2630 void S<int>::f();
2632 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2633 classes are not counted in the TEMPLATE_COUNT, so that in
2635 template <class T> struct S {};
2636 template <> struct S<int> { void f(); }
2637 template <> void S<int>::f();
2639 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2640 invalid; there should be no template <>.)
2642 If the function is a specialization, it is marked as such via
2643 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2644 is set up correctly, and it is added to the list of specializations
2645 for that template. */
2647 tree
2648 check_explicit_specialization (tree declarator,
2649 tree decl,
2650 int template_count,
2651 int flags)
2653 int have_def = flags & 2;
2654 int is_friend = flags & 4;
2655 bool is_concept = flags & 8;
2656 int specialization = 0;
2657 int explicit_instantiation = 0;
2658 int member_specialization = 0;
2659 tree ctype = DECL_CLASS_CONTEXT (decl);
2660 tree dname = DECL_NAME (decl);
2661 tmpl_spec_kind tsk;
2663 if (is_friend)
2665 if (!processing_specialization)
2666 tsk = tsk_none;
2667 else
2668 tsk = tsk_excessive_parms;
2670 else
2671 tsk = current_tmpl_spec_kind (template_count);
2673 switch (tsk)
2675 case tsk_none:
2676 if (processing_specialization && !VAR_P (decl))
2678 specialization = 1;
2679 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2681 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2683 if (is_friend)
2684 /* This could be something like:
2686 template <class T> void f(T);
2687 class S { friend void f<>(int); } */
2688 specialization = 1;
2689 else
2691 /* This case handles bogus declarations like template <>
2692 template <class T> void f<int>(); */
2694 error ("template-id %qD in declaration of primary template",
2695 declarator);
2696 return decl;
2699 break;
2701 case tsk_invalid_member_spec:
2702 /* The error has already been reported in
2703 check_specialization_scope. */
2704 return error_mark_node;
2706 case tsk_invalid_expl_inst:
2707 error ("template parameter list used in explicit instantiation");
2709 /* Fall through. */
2711 case tsk_expl_inst:
2712 if (have_def)
2713 error ("definition provided for explicit instantiation");
2715 explicit_instantiation = 1;
2716 break;
2718 case tsk_excessive_parms:
2719 case tsk_insufficient_parms:
2720 if (tsk == tsk_excessive_parms)
2721 error ("too many template parameter lists in declaration of %qD",
2722 decl);
2723 else if (template_header_count)
2724 error("too few template parameter lists in declaration of %qD", decl);
2725 else
2726 error("explicit specialization of %qD must be introduced by "
2727 "%<template <>%>", decl);
2729 /* Fall through. */
2730 case tsk_expl_spec:
2731 if (is_concept)
2732 error ("explicit specialization declared %<concept%>");
2734 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2735 /* In cases like template<> constexpr bool v = true;
2736 We'll give an error in check_template_variable. */
2737 break;
2739 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2740 if (ctype)
2741 member_specialization = 1;
2742 else
2743 specialization = 1;
2744 break;
2746 case tsk_template:
2747 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2749 /* This case handles bogus declarations like template <>
2750 template <class T> void f<int>(); */
2752 if (!uses_template_parms (declarator))
2753 error ("template-id %qD in declaration of primary template",
2754 declarator);
2755 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2757 /* Partial specialization of variable template. */
2758 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2759 specialization = 1;
2760 goto ok;
2762 else if (cxx_dialect < cxx14)
2763 error ("non-type partial specialization %qD "
2764 "is not allowed", declarator);
2765 else
2766 error ("non-class, non-variable partial specialization %qD "
2767 "is not allowed", declarator);
2768 return decl;
2769 ok:;
2772 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2773 /* This is a specialization of a member template, without
2774 specialization the containing class. Something like:
2776 template <class T> struct S {
2777 template <class U> void f (U);
2779 template <> template <class U> void S<int>::f(U) {}
2781 That's a specialization -- but of the entire template. */
2782 specialization = 1;
2783 break;
2785 default:
2786 gcc_unreachable ();
2789 if ((specialization || member_specialization)
2790 /* This doesn't apply to variable templates. */
2791 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2792 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2794 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2795 for (; t; t = TREE_CHAIN (t))
2796 if (TREE_PURPOSE (t))
2798 permerror (input_location,
2799 "default argument specified in explicit specialization");
2800 break;
2804 if (specialization || member_specialization || explicit_instantiation)
2806 tree tmpl = NULL_TREE;
2807 tree targs = NULL_TREE;
2808 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2810 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2811 if (!was_template_id)
2813 tree fns;
2815 gcc_assert (identifier_p (declarator));
2816 if (ctype)
2817 fns = dname;
2818 else
2820 /* If there is no class context, the explicit instantiation
2821 must be at namespace scope. */
2822 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2824 /* Find the namespace binding, using the declaration
2825 context. */
2826 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2827 false, true);
2828 if (fns == error_mark_node)
2829 /* If lookup fails, look for a friend declaration so we can
2830 give a better diagnostic. */
2831 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2832 /*type*/false, /*complain*/true,
2833 /*hidden*/true);
2835 if (fns == error_mark_node || !is_overloaded_fn (fns))
2837 error ("%qD is not a template function", dname);
2838 fns = error_mark_node;
2842 declarator = lookup_template_function (fns, NULL_TREE);
2845 if (declarator == error_mark_node)
2846 return error_mark_node;
2848 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2850 if (!explicit_instantiation)
2851 /* A specialization in class scope. This is invalid,
2852 but the error will already have been flagged by
2853 check_specialization_scope. */
2854 return error_mark_node;
2855 else
2857 /* It's not valid to write an explicit instantiation in
2858 class scope, e.g.:
2860 class C { template void f(); }
2862 This case is caught by the parser. However, on
2863 something like:
2865 template class C { void f(); };
2867 (which is invalid) we can get here. The error will be
2868 issued later. */
2872 return decl;
2874 else if (ctype != NULL_TREE
2875 && (identifier_p (TREE_OPERAND (declarator, 0))))
2877 // We'll match variable templates in start_decl.
2878 if (VAR_P (decl))
2879 return decl;
2881 /* Find the list of functions in ctype that have the same
2882 name as the declared function. */
2883 tree name = TREE_OPERAND (declarator, 0);
2885 if (constructor_name_p (name, ctype))
2887 if (DECL_CONSTRUCTOR_P (decl)
2888 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2889 : !CLASSTYPE_DESTRUCTOR (ctype))
2891 /* From [temp.expl.spec]:
2893 If such an explicit specialization for the member
2894 of a class template names an implicitly-declared
2895 special member function (clause _special_), the
2896 program is ill-formed.
2898 Similar language is found in [temp.explicit]. */
2899 error ("specialization of implicitly-declared special member function");
2900 return error_mark_node;
2903 name = DECL_NAME (decl);
2906 /* For a type-conversion operator, We might be looking for
2907 `operator int' which will be a specialization of
2908 `operator T'. Grab all the conversion operators, and
2909 then select from them. */
2910 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
2911 ? conv_op_identifier : name);
2913 if (fns == NULL_TREE)
2915 error ("no member function %qD declared in %qT", name, ctype);
2916 return error_mark_node;
2918 else
2919 TREE_OPERAND (declarator, 0) = fns;
2922 /* Figure out what exactly is being specialized at this point.
2923 Note that for an explicit instantiation, even one for a
2924 member function, we cannot tell a priori whether the
2925 instantiation is for a member template, or just a member
2926 function of a template class. Even if a member template is
2927 being instantiated, the member template arguments may be
2928 elided if they can be deduced from the rest of the
2929 declaration. */
2930 tmpl = determine_specialization (declarator, decl,
2931 &targs,
2932 member_specialization,
2933 template_count,
2934 tsk);
2936 if (!tmpl || tmpl == error_mark_node)
2937 /* We couldn't figure out what this declaration was
2938 specializing. */
2939 return error_mark_node;
2940 else
2942 if (TREE_CODE (decl) == FUNCTION_DECL
2943 && DECL_HIDDEN_FRIEND_P (tmpl))
2945 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2946 "friend declaration %qD is not visible to "
2947 "explicit specialization", tmpl))
2948 inform (DECL_SOURCE_LOCATION (tmpl),
2949 "friend declaration here");
2951 else if (!ctype && !is_friend
2952 && CP_DECL_CONTEXT (decl) == current_namespace)
2953 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2955 tree gen_tmpl = most_general_template (tmpl);
2957 if (explicit_instantiation)
2959 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2960 is done by do_decl_instantiation later. */
2962 int arg_depth = TMPL_ARGS_DEPTH (targs);
2963 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2965 if (arg_depth > parm_depth)
2967 /* If TMPL is not the most general template (for
2968 example, if TMPL is a friend template that is
2969 injected into namespace scope), then there will
2970 be too many levels of TARGS. Remove some of them
2971 here. */
2972 int i;
2973 tree new_targs;
2975 new_targs = make_tree_vec (parm_depth);
2976 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2977 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2978 = TREE_VEC_ELT (targs, i);
2979 targs = new_targs;
2982 return instantiate_template (tmpl, targs, tf_error);
2985 /* If we thought that the DECL was a member function, but it
2986 turns out to be specializing a static member function,
2987 make DECL a static member function as well. */
2988 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2989 && DECL_STATIC_FUNCTION_P (tmpl)
2990 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2991 revert_static_member_fn (decl);
2993 /* If this is a specialization of a member template of a
2994 template class, we want to return the TEMPLATE_DECL, not
2995 the specialization of it. */
2996 if (tsk == tsk_template && !was_template_id)
2998 tree result = DECL_TEMPLATE_RESULT (tmpl);
2999 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3000 DECL_INITIAL (result) = NULL_TREE;
3001 if (have_def)
3003 tree parm;
3004 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3005 DECL_SOURCE_LOCATION (result)
3006 = DECL_SOURCE_LOCATION (decl);
3007 /* We want to use the argument list specified in the
3008 definition, not in the original declaration. */
3009 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3010 for (parm = DECL_ARGUMENTS (result); parm;
3011 parm = DECL_CHAIN (parm))
3012 DECL_CONTEXT (parm) = result;
3014 return register_specialization (tmpl, gen_tmpl, targs,
3015 is_friend, 0);
3018 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3019 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3021 if (was_template_id)
3022 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3024 /* Inherit default function arguments from the template
3025 DECL is specializing. */
3026 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3027 copy_default_args_to_explicit_spec (decl);
3029 /* This specialization has the same protection as the
3030 template it specializes. */
3031 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3032 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3034 /* 7.1.1-1 [dcl.stc]
3036 A storage-class-specifier shall not be specified in an
3037 explicit specialization...
3039 The parser rejects these, so unless action is taken here,
3040 explicit function specializations will always appear with
3041 global linkage.
3043 The action recommended by the C++ CWG in response to C++
3044 defect report 605 is to make the storage class and linkage
3045 of the explicit specialization match the templated function:
3047 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3049 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3051 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3052 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3054 /* A concept cannot be specialized. */
3055 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3057 error ("explicit specialization of function concept %qD",
3058 gen_tmpl);
3059 return error_mark_node;
3062 /* This specialization has the same linkage and visibility as
3063 the function template it specializes. */
3064 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3065 if (! TREE_PUBLIC (decl))
3067 DECL_INTERFACE_KNOWN (decl) = 1;
3068 DECL_NOT_REALLY_EXTERN (decl) = 1;
3070 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3071 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3073 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3074 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3078 /* If DECL is a friend declaration, declared using an
3079 unqualified name, the namespace associated with DECL may
3080 have been set incorrectly. For example, in:
3082 template <typename T> void f(T);
3083 namespace N {
3084 struct S { friend void f<int>(int); }
3087 we will have set the DECL_CONTEXT for the friend
3088 declaration to N, rather than to the global namespace. */
3089 if (DECL_NAMESPACE_SCOPE_P (decl))
3090 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3092 if (is_friend && !have_def)
3093 /* This is not really a declaration of a specialization.
3094 It's just the name of an instantiation. But, it's not
3095 a request for an instantiation, either. */
3096 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3097 else if (TREE_CODE (decl) == FUNCTION_DECL)
3098 /* A specialization is not necessarily COMDAT. */
3099 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3100 && DECL_DECLARED_INLINE_P (decl));
3101 else if (VAR_P (decl))
3102 DECL_COMDAT (decl) = false;
3104 /* If this is a full specialization, register it so that we can find
3105 it again. Partial specializations will be registered in
3106 process_partial_specialization. */
3107 if (!processing_template_decl)
3108 decl = register_specialization (decl, gen_tmpl, targs,
3109 is_friend, 0);
3111 /* A 'structor should already have clones. */
3112 gcc_assert (decl == error_mark_node
3113 || variable_template_p (tmpl)
3114 || !(DECL_CONSTRUCTOR_P (decl)
3115 || DECL_DESTRUCTOR_P (decl))
3116 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3120 return decl;
3123 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3124 parameters. These are represented in the same format used for
3125 DECL_TEMPLATE_PARMS. */
3128 comp_template_parms (const_tree parms1, const_tree parms2)
3130 const_tree p1;
3131 const_tree p2;
3133 if (parms1 == parms2)
3134 return 1;
3136 for (p1 = parms1, p2 = parms2;
3137 p1 != NULL_TREE && p2 != NULL_TREE;
3138 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3140 tree t1 = TREE_VALUE (p1);
3141 tree t2 = TREE_VALUE (p2);
3142 int i;
3144 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3145 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3147 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3148 return 0;
3150 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3152 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3153 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3155 /* If either of the template parameters are invalid, assume
3156 they match for the sake of error recovery. */
3157 if (error_operand_p (parm1) || error_operand_p (parm2))
3158 return 1;
3160 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3161 return 0;
3163 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3164 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3165 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3166 continue;
3167 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3168 return 0;
3172 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3173 /* One set of parameters has more parameters lists than the
3174 other. */
3175 return 0;
3177 return 1;
3180 /* Determine whether PARM is a parameter pack. */
3182 bool
3183 template_parameter_pack_p (const_tree parm)
3185 /* Determine if we have a non-type template parameter pack. */
3186 if (TREE_CODE (parm) == PARM_DECL)
3187 return (DECL_TEMPLATE_PARM_P (parm)
3188 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3189 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3190 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3192 /* If this is a list of template parameters, we could get a
3193 TYPE_DECL or a TEMPLATE_DECL. */
3194 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3195 parm = TREE_TYPE (parm);
3197 /* Otherwise it must be a type template parameter. */
3198 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3199 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3200 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3203 /* Determine if T is a function parameter pack. */
3205 bool
3206 function_parameter_pack_p (const_tree t)
3208 if (t && TREE_CODE (t) == PARM_DECL)
3209 return DECL_PACK_P (t);
3210 return false;
3213 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3214 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3216 tree
3217 get_function_template_decl (const_tree primary_func_tmpl_inst)
3219 if (! primary_func_tmpl_inst
3220 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3221 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3222 return NULL;
3224 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3227 /* Return true iff the function parameter PARAM_DECL was expanded
3228 from the function parameter pack PACK. */
3230 bool
3231 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3233 if (DECL_ARTIFICIAL (param_decl)
3234 || !function_parameter_pack_p (pack))
3235 return false;
3237 /* The parameter pack and its pack arguments have the same
3238 DECL_PARM_INDEX. */
3239 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3242 /* Determine whether ARGS describes a variadic template args list,
3243 i.e., one that is terminated by a template argument pack. */
3245 static bool
3246 template_args_variadic_p (tree args)
3248 int nargs;
3249 tree last_parm;
3251 if (args == NULL_TREE)
3252 return false;
3254 args = INNERMOST_TEMPLATE_ARGS (args);
3255 nargs = TREE_VEC_LENGTH (args);
3257 if (nargs == 0)
3258 return false;
3260 last_parm = TREE_VEC_ELT (args, nargs - 1);
3262 return ARGUMENT_PACK_P (last_parm);
3265 /* Generate a new name for the parameter pack name NAME (an
3266 IDENTIFIER_NODE) that incorporates its */
3268 static tree
3269 make_ith_pack_parameter_name (tree name, int i)
3271 /* Munge the name to include the parameter index. */
3272 #define NUMBUF_LEN 128
3273 char numbuf[NUMBUF_LEN];
3274 char* newname;
3275 int newname_len;
3277 if (name == NULL_TREE)
3278 return name;
3279 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3280 newname_len = IDENTIFIER_LENGTH (name)
3281 + strlen (numbuf) + 2;
3282 newname = (char*)alloca (newname_len);
3283 snprintf (newname, newname_len,
3284 "%s#%i", IDENTIFIER_POINTER (name), i);
3285 return get_identifier (newname);
3288 /* Return true if T is a primary function, class or alias template
3289 instantiation. */
3291 bool
3292 primary_template_instantiation_p (const_tree t)
3294 if (!t)
3295 return false;
3297 if (TREE_CODE (t) == FUNCTION_DECL)
3298 return DECL_LANG_SPECIFIC (t)
3299 && DECL_TEMPLATE_INSTANTIATION (t)
3300 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3301 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3302 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3303 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3304 else if (alias_template_specialization_p (t))
3305 return true;
3306 return false;
3309 /* Return true if PARM is a template template parameter. */
3311 bool
3312 template_template_parameter_p (const_tree parm)
3314 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3317 /* Return true iff PARM is a DECL representing a type template
3318 parameter. */
3320 bool
3321 template_type_parameter_p (const_tree parm)
3323 return (parm
3324 && (TREE_CODE (parm) == TYPE_DECL
3325 || TREE_CODE (parm) == TEMPLATE_DECL)
3326 && DECL_TEMPLATE_PARM_P (parm));
3329 /* Return the template parameters of T if T is a
3330 primary template instantiation, NULL otherwise. */
3332 tree
3333 get_primary_template_innermost_parameters (const_tree t)
3335 tree parms = NULL, template_info = NULL;
3337 if ((template_info = get_template_info (t))
3338 && primary_template_instantiation_p (t))
3339 parms = INNERMOST_TEMPLATE_PARMS
3340 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3342 return parms;
3345 /* Return the template parameters of the LEVELth level from the full list
3346 of template parameters PARMS. */
3348 tree
3349 get_template_parms_at_level (tree parms, int level)
3351 tree p;
3352 if (!parms
3353 || TREE_CODE (parms) != TREE_LIST
3354 || level > TMPL_PARMS_DEPTH (parms))
3355 return NULL_TREE;
3357 for (p = parms; p; p = TREE_CHAIN (p))
3358 if (TMPL_PARMS_DEPTH (p) == level)
3359 return p;
3361 return NULL_TREE;
3364 /* Returns the template arguments of T if T is a template instantiation,
3365 NULL otherwise. */
3367 tree
3368 get_template_innermost_arguments (const_tree t)
3370 tree args = NULL, template_info = NULL;
3372 if ((template_info = get_template_info (t))
3373 && TI_ARGS (template_info))
3374 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3376 return args;
3379 /* Return the argument pack elements of T if T is a template argument pack,
3380 NULL otherwise. */
3382 tree
3383 get_template_argument_pack_elems (const_tree t)
3385 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3386 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3387 return NULL;
3389 return ARGUMENT_PACK_ARGS (t);
3392 /* True iff FN is a function representing a built-in variadic parameter
3393 pack. */
3395 bool
3396 builtin_pack_fn_p (tree fn)
3398 if (!fn
3399 || TREE_CODE (fn) != FUNCTION_DECL
3400 || !DECL_IS_BUILTIN (fn))
3401 return false;
3403 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3404 return true;
3406 return false;
3409 /* True iff CALL is a call to a function representing a built-in variadic
3410 parameter pack. */
3412 static bool
3413 builtin_pack_call_p (tree call)
3415 if (TREE_CODE (call) != CALL_EXPR)
3416 return false;
3417 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3420 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3422 static tree
3423 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3424 tree in_decl)
3426 tree ohi = CALL_EXPR_ARG (call, 0);
3427 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3428 false/*fn*/, true/*int_cst*/);
3430 if (value_dependent_expression_p (hi))
3432 if (hi != ohi)
3434 call = copy_node (call);
3435 CALL_EXPR_ARG (call, 0) = hi;
3437 tree ex = make_pack_expansion (call);
3438 tree vec = make_tree_vec (1);
3439 TREE_VEC_ELT (vec, 0) = ex;
3440 return vec;
3442 else
3444 hi = cxx_constant_value (hi);
3445 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3447 /* Calculate the largest value of len that won't make the size of the vec
3448 overflow an int. The compiler will exceed resource limits long before
3449 this, but it seems a decent place to diagnose. */
3450 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3452 if (len < 0 || len > max)
3454 if ((complain & tf_error)
3455 && hi != error_mark_node)
3456 error ("argument to __integer_pack must be between 0 and %d", max);
3457 return error_mark_node;
3460 tree vec = make_tree_vec (len);
3462 for (int i = 0; i < len; ++i)
3463 TREE_VEC_ELT (vec, i) = size_int (i);
3465 return vec;
3469 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3470 CALL. */
3472 static tree
3473 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3474 tree in_decl)
3476 if (!builtin_pack_call_p (call))
3477 return NULL_TREE;
3479 tree fn = CALL_EXPR_FN (call);
3481 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3482 return expand_integer_pack (call, args, complain, in_decl);
3484 return NULL_TREE;
3487 /* Structure used to track the progress of find_parameter_packs_r. */
3488 struct find_parameter_pack_data
3490 /* TREE_LIST that will contain all of the parameter packs found by
3491 the traversal. */
3492 tree* parameter_packs;
3494 /* Set of AST nodes that have been visited by the traversal. */
3495 hash_set<tree> *visited;
3497 /* True iff we're making a type pack expansion. */
3498 bool type_pack_expansion_p;
3501 /* Identifies all of the argument packs that occur in a template
3502 argument and appends them to the TREE_LIST inside DATA, which is a
3503 find_parameter_pack_data structure. This is a subroutine of
3504 make_pack_expansion and uses_parameter_packs. */
3505 static tree
3506 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3508 tree t = *tp;
3509 struct find_parameter_pack_data* ppd =
3510 (struct find_parameter_pack_data*)data;
3511 bool parameter_pack_p = false;
3513 /* Handle type aliases/typedefs. */
3514 if (TYPE_ALIAS_P (t))
3516 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3517 cp_walk_tree (&TI_ARGS (tinfo),
3518 &find_parameter_packs_r,
3519 ppd, ppd->visited);
3520 *walk_subtrees = 0;
3521 return NULL_TREE;
3524 /* Identify whether this is a parameter pack or not. */
3525 switch (TREE_CODE (t))
3527 case TEMPLATE_PARM_INDEX:
3528 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3529 parameter_pack_p = true;
3530 break;
3532 case TEMPLATE_TYPE_PARM:
3533 t = TYPE_MAIN_VARIANT (t);
3534 /* FALLTHRU */
3535 case TEMPLATE_TEMPLATE_PARM:
3536 /* If the placeholder appears in the decl-specifier-seq of a function
3537 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3538 is a pack expansion, the invented template parameter is a template
3539 parameter pack. */
3540 if (ppd->type_pack_expansion_p && is_auto (t))
3541 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3542 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3543 parameter_pack_p = true;
3544 break;
3546 case FIELD_DECL:
3547 case PARM_DECL:
3548 if (DECL_PACK_P (t))
3550 /* We don't want to walk into the type of a PARM_DECL,
3551 because we don't want to see the type parameter pack. */
3552 *walk_subtrees = 0;
3553 parameter_pack_p = true;
3555 break;
3557 /* Look through a lambda capture proxy to the field pack. */
3558 case VAR_DECL:
3559 if (DECL_HAS_VALUE_EXPR_P (t))
3561 tree v = DECL_VALUE_EXPR (t);
3562 cp_walk_tree (&v,
3563 &find_parameter_packs_r,
3564 ppd, ppd->visited);
3565 *walk_subtrees = 0;
3567 else if (variable_template_specialization_p (t))
3569 cp_walk_tree (&DECL_TI_ARGS (t),
3570 find_parameter_packs_r,
3571 ppd, ppd->visited);
3572 *walk_subtrees = 0;
3574 break;
3576 case CALL_EXPR:
3577 if (builtin_pack_call_p (t))
3578 parameter_pack_p = true;
3579 break;
3581 case BASES:
3582 parameter_pack_p = true;
3583 break;
3584 default:
3585 /* Not a parameter pack. */
3586 break;
3589 if (parameter_pack_p)
3591 /* Add this parameter pack to the list. */
3592 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3595 if (TYPE_P (t))
3596 cp_walk_tree (&TYPE_CONTEXT (t),
3597 &find_parameter_packs_r, ppd, ppd->visited);
3599 /* This switch statement will return immediately if we don't find a
3600 parameter pack. */
3601 switch (TREE_CODE (t))
3603 case TEMPLATE_PARM_INDEX:
3604 return NULL_TREE;
3606 case BOUND_TEMPLATE_TEMPLATE_PARM:
3607 /* Check the template itself. */
3608 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3609 &find_parameter_packs_r, ppd, ppd->visited);
3610 /* Check the template arguments. */
3611 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3612 ppd->visited);
3613 *walk_subtrees = 0;
3614 return NULL_TREE;
3616 case TEMPLATE_TYPE_PARM:
3617 case TEMPLATE_TEMPLATE_PARM:
3618 return NULL_TREE;
3620 case PARM_DECL:
3621 return NULL_TREE;
3623 case DECL_EXPR:
3624 /* Ignore the declaration of a capture proxy for a parameter pack. */
3625 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3626 *walk_subtrees = 0;
3627 return NULL_TREE;
3629 case RECORD_TYPE:
3630 if (TYPE_PTRMEMFUNC_P (t))
3631 return NULL_TREE;
3632 /* Fall through. */
3634 case UNION_TYPE:
3635 case ENUMERAL_TYPE:
3636 if (TYPE_TEMPLATE_INFO (t))
3637 cp_walk_tree (&TYPE_TI_ARGS (t),
3638 &find_parameter_packs_r, ppd, ppd->visited);
3640 *walk_subtrees = 0;
3641 return NULL_TREE;
3643 case TEMPLATE_DECL:
3644 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3645 return NULL_TREE;
3646 gcc_fallthrough();
3648 case CONSTRUCTOR:
3649 cp_walk_tree (&TREE_TYPE (t),
3650 &find_parameter_packs_r, ppd, ppd->visited);
3651 return NULL_TREE;
3653 case TYPENAME_TYPE:
3654 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3655 ppd, ppd->visited);
3656 *walk_subtrees = 0;
3657 return NULL_TREE;
3659 case TYPE_PACK_EXPANSION:
3660 case EXPR_PACK_EXPANSION:
3661 *walk_subtrees = 0;
3662 return NULL_TREE;
3664 case INTEGER_TYPE:
3665 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3666 ppd, ppd->visited);
3667 *walk_subtrees = 0;
3668 return NULL_TREE;
3670 case IDENTIFIER_NODE:
3671 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3672 ppd->visited);
3673 *walk_subtrees = 0;
3674 return NULL_TREE;
3676 case LAMBDA_EXPR:
3678 tree fn = lambda_function (t);
3679 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3680 ppd->visited);
3681 *walk_subtrees = 0;
3682 return NULL_TREE;
3685 case DECLTYPE_TYPE:
3687 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3688 type_pack_expansion_p to false so that any placeholders
3689 within the expression don't get marked as parameter packs. */
3690 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3691 ppd->type_pack_expansion_p = false;
3692 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3693 ppd, ppd->visited);
3694 ppd->type_pack_expansion_p = type_pack_expansion_p;
3695 *walk_subtrees = 0;
3696 return NULL_TREE;
3699 default:
3700 return NULL_TREE;
3703 return NULL_TREE;
3706 /* Determines if the expression or type T uses any parameter packs. */
3707 bool
3708 uses_parameter_packs (tree t)
3710 tree parameter_packs = NULL_TREE;
3711 struct find_parameter_pack_data ppd;
3712 ppd.parameter_packs = &parameter_packs;
3713 ppd.visited = new hash_set<tree>;
3714 ppd.type_pack_expansion_p = false;
3715 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3716 delete ppd.visited;
3717 return parameter_packs != NULL_TREE;
3720 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3721 representation a base-class initializer into a parameter pack
3722 expansion. If all goes well, the resulting node will be an
3723 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3724 respectively. */
3725 tree
3726 make_pack_expansion (tree arg)
3728 tree result;
3729 tree parameter_packs = NULL_TREE;
3730 bool for_types = false;
3731 struct find_parameter_pack_data ppd;
3733 if (!arg || arg == error_mark_node)
3734 return arg;
3736 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3738 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3739 class initializer. In this case, the TREE_PURPOSE will be a
3740 _TYPE node (representing the base class expansion we're
3741 initializing) and the TREE_VALUE will be a TREE_LIST
3742 containing the initialization arguments.
3744 The resulting expansion looks somewhat different from most
3745 expansions. Rather than returning just one _EXPANSION, we
3746 return a TREE_LIST whose TREE_PURPOSE is a
3747 TYPE_PACK_EXPANSION containing the bases that will be
3748 initialized. The TREE_VALUE will be identical to the
3749 original TREE_VALUE, which is a list of arguments that will
3750 be passed to each base. We do not introduce any new pack
3751 expansion nodes into the TREE_VALUE (although it is possible
3752 that some already exist), because the TREE_PURPOSE and
3753 TREE_VALUE all need to be expanded together with the same
3754 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3755 resulting TREE_PURPOSE will mention the parameter packs in
3756 both the bases and the arguments to the bases. */
3757 tree purpose;
3758 tree value;
3759 tree parameter_packs = NULL_TREE;
3761 /* Determine which parameter packs will be used by the base
3762 class expansion. */
3763 ppd.visited = new hash_set<tree>;
3764 ppd.parameter_packs = &parameter_packs;
3765 ppd.type_pack_expansion_p = true;
3766 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3767 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3768 &ppd, ppd.visited);
3770 if (parameter_packs == NULL_TREE)
3772 error ("base initializer expansion %qT contains no parameter packs", arg);
3773 delete ppd.visited;
3774 return error_mark_node;
3777 if (TREE_VALUE (arg) != void_type_node)
3779 /* Collect the sets of parameter packs used in each of the
3780 initialization arguments. */
3781 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3783 /* Determine which parameter packs will be expanded in this
3784 argument. */
3785 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3786 &ppd, ppd.visited);
3790 delete ppd.visited;
3792 /* Create the pack expansion type for the base type. */
3793 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3794 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3795 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3796 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3798 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3799 they will rarely be compared to anything. */
3800 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3802 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3805 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3806 for_types = true;
3808 /* Build the PACK_EXPANSION_* node. */
3809 result = for_types
3810 ? cxx_make_type (TYPE_PACK_EXPANSION)
3811 : make_node (EXPR_PACK_EXPANSION);
3812 SET_PACK_EXPANSION_PATTERN (result, arg);
3813 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3815 /* Propagate type and const-expression information. */
3816 TREE_TYPE (result) = TREE_TYPE (arg);
3817 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3818 /* Mark this read now, since the expansion might be length 0. */
3819 mark_exp_read (arg);
3821 else
3822 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3823 they will rarely be compared to anything. */
3824 SET_TYPE_STRUCTURAL_EQUALITY (result);
3826 /* Determine which parameter packs will be expanded. */
3827 ppd.parameter_packs = &parameter_packs;
3828 ppd.visited = new hash_set<tree>;
3829 ppd.type_pack_expansion_p = TYPE_P (arg);
3830 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3831 delete ppd.visited;
3833 /* Make sure we found some parameter packs. */
3834 if (parameter_packs == NULL_TREE)
3836 if (TYPE_P (arg))
3837 error ("expansion pattern %qT contains no argument packs", arg);
3838 else
3839 error ("expansion pattern %qE contains no argument packs", arg);
3840 return error_mark_node;
3842 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3844 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3846 return result;
3849 /* Checks T for any "bare" parameter packs, which have not yet been
3850 expanded, and issues an error if any are found. This operation can
3851 only be done on full expressions or types (e.g., an expression
3852 statement, "if" condition, etc.), because we could have expressions like:
3854 foo(f(g(h(args)))...)
3856 where "args" is a parameter pack. check_for_bare_parameter_packs
3857 should not be called for the subexpressions args, h(args),
3858 g(h(args)), or f(g(h(args))), because we would produce erroneous
3859 error messages.
3861 Returns TRUE and emits an error if there were bare parameter packs,
3862 returns FALSE otherwise. */
3863 bool
3864 check_for_bare_parameter_packs (tree t)
3866 tree parameter_packs = NULL_TREE;
3867 struct find_parameter_pack_data ppd;
3869 if (!processing_template_decl || !t || t == error_mark_node)
3870 return false;
3872 /* A lambda might use a parameter pack from the containing context. */
3873 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3874 return false;
3876 if (TREE_CODE (t) == TYPE_DECL)
3877 t = TREE_TYPE (t);
3879 ppd.parameter_packs = &parameter_packs;
3880 ppd.visited = new hash_set<tree>;
3881 ppd.type_pack_expansion_p = false;
3882 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3883 delete ppd.visited;
3885 if (parameter_packs)
3887 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3888 error_at (loc, "parameter packs not expanded with %<...%>:");
3889 while (parameter_packs)
3891 tree pack = TREE_VALUE (parameter_packs);
3892 tree name = NULL_TREE;
3894 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3895 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3896 name = TYPE_NAME (pack);
3897 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3898 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3899 else if (TREE_CODE (pack) == CALL_EXPR)
3900 name = DECL_NAME (CALL_EXPR_FN (pack));
3901 else
3902 name = DECL_NAME (pack);
3904 if (name)
3905 inform (loc, " %qD", name);
3906 else
3907 inform (loc, " <anonymous>");
3909 parameter_packs = TREE_CHAIN (parameter_packs);
3912 return true;
3915 return false;
3918 /* Expand any parameter packs that occur in the template arguments in
3919 ARGS. */
3920 tree
3921 expand_template_argument_pack (tree args)
3923 if (args == error_mark_node)
3924 return error_mark_node;
3926 tree result_args = NULL_TREE;
3927 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3928 int num_result_args = -1;
3929 int non_default_args_count = -1;
3931 /* First, determine if we need to expand anything, and the number of
3932 slots we'll need. */
3933 for (in_arg = 0; in_arg < nargs; ++in_arg)
3935 tree arg = TREE_VEC_ELT (args, in_arg);
3936 if (arg == NULL_TREE)
3937 return args;
3938 if (ARGUMENT_PACK_P (arg))
3940 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3941 if (num_result_args < 0)
3942 num_result_args = in_arg + num_packed;
3943 else
3944 num_result_args += num_packed;
3946 else
3948 if (num_result_args >= 0)
3949 num_result_args++;
3953 /* If no expansion is necessary, we're done. */
3954 if (num_result_args < 0)
3955 return args;
3957 /* Expand arguments. */
3958 result_args = make_tree_vec (num_result_args);
3959 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3960 non_default_args_count =
3961 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3962 for (in_arg = 0; in_arg < nargs; ++in_arg)
3964 tree arg = TREE_VEC_ELT (args, in_arg);
3965 if (ARGUMENT_PACK_P (arg))
3967 tree packed = ARGUMENT_PACK_ARGS (arg);
3968 int i, num_packed = TREE_VEC_LENGTH (packed);
3969 for (i = 0; i < num_packed; ++i, ++out_arg)
3970 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3971 if (non_default_args_count > 0)
3972 non_default_args_count += num_packed - 1;
3974 else
3976 TREE_VEC_ELT (result_args, out_arg) = arg;
3977 ++out_arg;
3980 if (non_default_args_count >= 0)
3981 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3982 return result_args;
3985 /* Checks if DECL shadows a template parameter.
3987 [temp.local]: A template-parameter shall not be redeclared within its
3988 scope (including nested scopes).
3990 Emits an error and returns TRUE if the DECL shadows a parameter,
3991 returns FALSE otherwise. */
3993 bool
3994 check_template_shadow (tree decl)
3996 tree olddecl;
3998 /* If we're not in a template, we can't possibly shadow a template
3999 parameter. */
4000 if (!current_template_parms)
4001 return true;
4003 /* Figure out what we're shadowing. */
4004 decl = OVL_FIRST (decl);
4005 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4007 /* If there's no previous binding for this name, we're not shadowing
4008 anything, let alone a template parameter. */
4009 if (!olddecl)
4010 return true;
4012 /* If we're not shadowing a template parameter, we're done. Note
4013 that OLDDECL might be an OVERLOAD (or perhaps even an
4014 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4015 node. */
4016 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4017 return true;
4019 /* We check for decl != olddecl to avoid bogus errors for using a
4020 name inside a class. We check TPFI to avoid duplicate errors for
4021 inline member templates. */
4022 if (decl == olddecl
4023 || (DECL_TEMPLATE_PARM_P (decl)
4024 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4025 return true;
4027 /* Don't complain about the injected class name, as we've already
4028 complained about the class itself. */
4029 if (DECL_SELF_REFERENCE_P (decl))
4030 return false;
4032 if (DECL_TEMPLATE_PARM_P (decl))
4033 error ("declaration of template parameter %q+D shadows "
4034 "template parameter", decl);
4035 else
4036 error ("declaration of %q+#D shadows template parameter", decl);
4037 inform (DECL_SOURCE_LOCATION (olddecl),
4038 "template parameter %qD declared here", olddecl);
4039 return false;
4042 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4043 ORIG_LEVEL, DECL, and TYPE. */
4045 static tree
4046 build_template_parm_index (int index,
4047 int level,
4048 int orig_level,
4049 tree decl,
4050 tree type)
4052 tree t = make_node (TEMPLATE_PARM_INDEX);
4053 TEMPLATE_PARM_IDX (t) = index;
4054 TEMPLATE_PARM_LEVEL (t) = level;
4055 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4056 TEMPLATE_PARM_DECL (t) = decl;
4057 TREE_TYPE (t) = type;
4058 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4059 TREE_READONLY (t) = TREE_READONLY (decl);
4061 return t;
4064 /* Find the canonical type parameter for the given template type
4065 parameter. Returns the canonical type parameter, which may be TYPE
4066 if no such parameter existed. */
4068 static tree
4069 canonical_type_parameter (tree type)
4071 tree list;
4072 int idx = TEMPLATE_TYPE_IDX (type);
4073 if (!canonical_template_parms)
4074 vec_alloc (canonical_template_parms, idx + 1);
4076 if (canonical_template_parms->length () <= (unsigned) idx)
4077 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4079 list = (*canonical_template_parms)[idx];
4080 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4081 list = TREE_CHAIN (list);
4083 if (list)
4084 return TREE_VALUE (list);
4085 else
4087 (*canonical_template_parms)[idx]
4088 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4089 return type;
4093 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4094 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4095 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4096 new one is created. */
4098 static tree
4099 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4100 tsubst_flags_t complain)
4102 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4103 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4104 != TEMPLATE_PARM_LEVEL (index) - levels)
4105 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4107 tree orig_decl = TEMPLATE_PARM_DECL (index);
4108 tree decl, t;
4110 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4111 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4112 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4113 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4114 DECL_ARTIFICIAL (decl) = 1;
4115 SET_DECL_TEMPLATE_PARM_P (decl);
4117 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4118 TEMPLATE_PARM_LEVEL (index) - levels,
4119 TEMPLATE_PARM_ORIG_LEVEL (index),
4120 decl, type);
4121 TEMPLATE_PARM_DESCENDANTS (index) = t;
4122 TEMPLATE_PARM_PARAMETER_PACK (t)
4123 = TEMPLATE_PARM_PARAMETER_PACK (index);
4125 /* Template template parameters need this. */
4126 if (TREE_CODE (decl) == TEMPLATE_DECL)
4128 DECL_TEMPLATE_RESULT (decl)
4129 = build_decl (DECL_SOURCE_LOCATION (decl),
4130 TYPE_DECL, DECL_NAME (decl), type);
4131 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4132 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4133 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4137 return TEMPLATE_PARM_DESCENDANTS (index);
4140 /* Process information from new template parameter PARM and append it
4141 to the LIST being built. This new parameter is a non-type
4142 parameter iff IS_NON_TYPE is true. This new parameter is a
4143 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4144 is in PARM_LOC. */
4146 tree
4147 process_template_parm (tree list, location_t parm_loc, tree parm,
4148 bool is_non_type, bool is_parameter_pack)
4150 tree decl = 0;
4151 int idx = 0;
4153 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4154 tree defval = TREE_PURPOSE (parm);
4155 tree constr = TREE_TYPE (parm);
4157 if (list)
4159 tree p = tree_last (list);
4161 if (p && TREE_VALUE (p) != error_mark_node)
4163 p = TREE_VALUE (p);
4164 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4165 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4166 else
4167 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4170 ++idx;
4173 if (is_non_type)
4175 parm = TREE_VALUE (parm);
4177 SET_DECL_TEMPLATE_PARM_P (parm);
4179 if (TREE_TYPE (parm) != error_mark_node)
4181 /* [temp.param]
4183 The top-level cv-qualifiers on the template-parameter are
4184 ignored when determining its type. */
4185 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4186 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4187 TREE_TYPE (parm) = error_mark_node;
4188 else if (uses_parameter_packs (TREE_TYPE (parm))
4189 && !is_parameter_pack
4190 /* If we're in a nested template parameter list, the template
4191 template parameter could be a parameter pack. */
4192 && processing_template_parmlist == 1)
4194 /* This template parameter is not a parameter pack, but it
4195 should be. Complain about "bare" parameter packs. */
4196 check_for_bare_parameter_packs (TREE_TYPE (parm));
4198 /* Recover by calling this a parameter pack. */
4199 is_parameter_pack = true;
4203 /* A template parameter is not modifiable. */
4204 TREE_CONSTANT (parm) = 1;
4205 TREE_READONLY (parm) = 1;
4206 decl = build_decl (parm_loc,
4207 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4208 TREE_CONSTANT (decl) = 1;
4209 TREE_READONLY (decl) = 1;
4210 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4211 = build_template_parm_index (idx, processing_template_decl,
4212 processing_template_decl,
4213 decl, TREE_TYPE (parm));
4215 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4216 = is_parameter_pack;
4218 else
4220 tree t;
4221 parm = TREE_VALUE (TREE_VALUE (parm));
4223 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4225 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4226 /* This is for distinguishing between real templates and template
4227 template parameters */
4228 TREE_TYPE (parm) = t;
4229 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4230 decl = parm;
4232 else
4234 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4235 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4236 decl = build_decl (parm_loc,
4237 TYPE_DECL, parm, t);
4240 TYPE_NAME (t) = decl;
4241 TYPE_STUB_DECL (t) = decl;
4242 parm = decl;
4243 TEMPLATE_TYPE_PARM_INDEX (t)
4244 = build_template_parm_index (idx, processing_template_decl,
4245 processing_template_decl,
4246 decl, TREE_TYPE (parm));
4247 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4248 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4250 DECL_ARTIFICIAL (decl) = 1;
4251 SET_DECL_TEMPLATE_PARM_P (decl);
4253 /* Build requirements for the type/template parameter.
4254 This must be done after SET_DECL_TEMPLATE_PARM_P or
4255 process_template_parm could fail. */
4256 tree reqs = finish_shorthand_constraint (parm, constr);
4258 pushdecl (decl);
4260 /* Build the parameter node linking the parameter declaration,
4261 its default argument (if any), and its constraints (if any). */
4262 parm = build_tree_list (defval, parm);
4263 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4265 return chainon (list, parm);
4268 /* The end of a template parameter list has been reached. Process the
4269 tree list into a parameter vector, converting each parameter into a more
4270 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4271 as PARM_DECLs. */
4273 tree
4274 end_template_parm_list (tree parms)
4276 int nparms;
4277 tree parm, next;
4278 tree saved_parmlist = make_tree_vec (list_length (parms));
4280 /* Pop the dummy parameter level and add the real one. */
4281 current_template_parms = TREE_CHAIN (current_template_parms);
4283 current_template_parms
4284 = tree_cons (size_int (processing_template_decl),
4285 saved_parmlist, current_template_parms);
4287 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4289 next = TREE_CHAIN (parm);
4290 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4291 TREE_CHAIN (parm) = NULL_TREE;
4294 --processing_template_parmlist;
4296 return saved_parmlist;
4299 // Explicitly indicate the end of the template parameter list. We assume
4300 // that the current template parameters have been constructed and/or
4301 // managed explicitly, as when creating new template template parameters
4302 // from a shorthand constraint.
4303 void
4304 end_template_parm_list ()
4306 --processing_template_parmlist;
4309 /* end_template_decl is called after a template declaration is seen. */
4311 void
4312 end_template_decl (void)
4314 reset_specialization ();
4316 if (! processing_template_decl)
4317 return;
4319 /* This matches the pushlevel in begin_template_parm_list. */
4320 finish_scope ();
4322 --processing_template_decl;
4323 current_template_parms = TREE_CHAIN (current_template_parms);
4326 /* Takes a TREE_LIST representing a template parameter and convert it
4327 into an argument suitable to be passed to the type substitution
4328 functions. Note that If the TREE_LIST contains an error_mark
4329 node, the returned argument is error_mark_node. */
4331 tree
4332 template_parm_to_arg (tree t)
4335 if (t == NULL_TREE
4336 || TREE_CODE (t) != TREE_LIST)
4337 return t;
4339 if (error_operand_p (TREE_VALUE (t)))
4340 return error_mark_node;
4342 t = TREE_VALUE (t);
4344 if (TREE_CODE (t) == TYPE_DECL
4345 || TREE_CODE (t) == TEMPLATE_DECL)
4347 t = TREE_TYPE (t);
4349 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4351 /* Turn this argument into a TYPE_ARGUMENT_PACK
4352 with a single element, which expands T. */
4353 tree vec = make_tree_vec (1);
4354 if (CHECKING_P)
4355 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4357 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4359 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4360 SET_ARGUMENT_PACK_ARGS (t, vec);
4363 else
4365 t = DECL_INITIAL (t);
4367 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4369 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4370 with a single element, which expands T. */
4371 tree vec = make_tree_vec (1);
4372 if (CHECKING_P)
4373 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4375 t = convert_from_reference (t);
4376 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4378 t = make_node (NONTYPE_ARGUMENT_PACK);
4379 SET_ARGUMENT_PACK_ARGS (t, vec);
4381 else
4382 t = convert_from_reference (t);
4384 return t;
4387 /* Given a single level of template parameters (a TREE_VEC), return it
4388 as a set of template arguments. */
4390 static tree
4391 template_parms_level_to_args (tree parms)
4393 tree a = copy_node (parms);
4394 TREE_TYPE (a) = NULL_TREE;
4395 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4396 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4398 if (CHECKING_P)
4399 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4401 return a;
4404 /* Given a set of template parameters, return them as a set of template
4405 arguments. The template parameters are represented as a TREE_VEC, in
4406 the form documented in cp-tree.h for template arguments. */
4408 static tree
4409 template_parms_to_args (tree parms)
4411 tree header;
4412 tree args = NULL_TREE;
4413 int length = TMPL_PARMS_DEPTH (parms);
4414 int l = length;
4416 /* If there is only one level of template parameters, we do not
4417 create a TREE_VEC of TREE_VECs. Instead, we return a single
4418 TREE_VEC containing the arguments. */
4419 if (length > 1)
4420 args = make_tree_vec (length);
4422 for (header = parms; header; header = TREE_CHAIN (header))
4424 tree a = template_parms_level_to_args (TREE_VALUE (header));
4426 if (length > 1)
4427 TREE_VEC_ELT (args, --l) = a;
4428 else
4429 args = a;
4432 return args;
4435 /* Within the declaration of a template, return the currently active
4436 template parameters as an argument TREE_VEC. */
4438 static tree
4439 current_template_args (void)
4441 return template_parms_to_args (current_template_parms);
4444 /* Update the declared TYPE by doing any lookups which were thought to be
4445 dependent, but are not now that we know the SCOPE of the declarator. */
4447 tree
4448 maybe_update_decl_type (tree orig_type, tree scope)
4450 tree type = orig_type;
4452 if (type == NULL_TREE)
4453 return type;
4455 if (TREE_CODE (orig_type) == TYPE_DECL)
4456 type = TREE_TYPE (type);
4458 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4459 && dependent_type_p (type)
4460 /* Don't bother building up the args in this case. */
4461 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4463 /* tsubst in the args corresponding to the template parameters,
4464 including auto if present. Most things will be unchanged, but
4465 make_typename_type and tsubst_qualified_id will resolve
4466 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4467 tree args = current_template_args ();
4468 tree auto_node = type_uses_auto (type);
4469 tree pushed;
4470 if (auto_node)
4472 tree auto_vec = make_tree_vec (1);
4473 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4474 args = add_to_template_args (args, auto_vec);
4476 pushed = push_scope (scope);
4477 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4478 if (pushed)
4479 pop_scope (scope);
4482 if (type == error_mark_node)
4483 return orig_type;
4485 if (TREE_CODE (orig_type) == TYPE_DECL)
4487 if (same_type_p (type, TREE_TYPE (orig_type)))
4488 type = orig_type;
4489 else
4490 type = TYPE_NAME (type);
4492 return type;
4495 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4496 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4497 the new template is a member template. */
4499 tree
4500 build_template_decl (tree decl, tree parms, bool member_template_p)
4502 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4503 DECL_TEMPLATE_PARMS (tmpl) = parms;
4504 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4505 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4506 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4508 return tmpl;
4511 struct template_parm_data
4513 /* The level of the template parameters we are currently
4514 processing. */
4515 int level;
4517 /* The index of the specialization argument we are currently
4518 processing. */
4519 int current_arg;
4521 /* An array whose size is the number of template parameters. The
4522 elements are nonzero if the parameter has been used in any one
4523 of the arguments processed so far. */
4524 int* parms;
4526 /* An array whose size is the number of template arguments. The
4527 elements are nonzero if the argument makes use of template
4528 parameters of this level. */
4529 int* arg_uses_template_parms;
4532 /* Subroutine of push_template_decl used to see if each template
4533 parameter in a partial specialization is used in the explicit
4534 argument list. If T is of the LEVEL given in DATA (which is
4535 treated as a template_parm_data*), then DATA->PARMS is marked
4536 appropriately. */
4538 static int
4539 mark_template_parm (tree t, void* data)
4541 int level;
4542 int idx;
4543 struct template_parm_data* tpd = (struct template_parm_data*) data;
4545 template_parm_level_and_index (t, &level, &idx);
4547 if (level == tpd->level)
4549 tpd->parms[idx] = 1;
4550 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4553 /* In C++17 the type of a non-type argument is a deduced context. */
4554 if (cxx_dialect >= cxx17
4555 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4556 for_each_template_parm (TREE_TYPE (t),
4557 &mark_template_parm,
4558 data,
4559 NULL,
4560 /*include_nondeduced_p=*/false);
4562 /* Return zero so that for_each_template_parm will continue the
4563 traversal of the tree; we want to mark *every* template parm. */
4564 return 0;
4567 /* Process the partial specialization DECL. */
4569 static tree
4570 process_partial_specialization (tree decl)
4572 tree type = TREE_TYPE (decl);
4573 tree tinfo = get_template_info (decl);
4574 tree maintmpl = TI_TEMPLATE (tinfo);
4575 tree specargs = TI_ARGS (tinfo);
4576 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4577 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4578 tree inner_parms;
4579 tree inst;
4580 int nargs = TREE_VEC_LENGTH (inner_args);
4581 int ntparms;
4582 int i;
4583 bool did_error_intro = false;
4584 struct template_parm_data tpd;
4585 struct template_parm_data tpd2;
4587 gcc_assert (current_template_parms);
4589 /* A concept cannot be specialized. */
4590 if (flag_concepts && variable_concept_p (maintmpl))
4592 error ("specialization of variable concept %q#D", maintmpl);
4593 return error_mark_node;
4596 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4597 ntparms = TREE_VEC_LENGTH (inner_parms);
4599 /* We check that each of the template parameters given in the
4600 partial specialization is used in the argument list to the
4601 specialization. For example:
4603 template <class T> struct S;
4604 template <class T> struct S<T*>;
4606 The second declaration is OK because `T*' uses the template
4607 parameter T, whereas
4609 template <class T> struct S<int>;
4611 is no good. Even trickier is:
4613 template <class T>
4614 struct S1
4616 template <class U>
4617 struct S2;
4618 template <class U>
4619 struct S2<T>;
4622 The S2<T> declaration is actually invalid; it is a
4623 full-specialization. Of course,
4625 template <class U>
4626 struct S2<T (*)(U)>;
4628 or some such would have been OK. */
4629 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4630 tpd.parms = XALLOCAVEC (int, ntparms);
4631 memset (tpd.parms, 0, sizeof (int) * ntparms);
4633 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4634 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4635 for (i = 0; i < nargs; ++i)
4637 tpd.current_arg = i;
4638 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4639 &mark_template_parm,
4640 &tpd,
4641 NULL,
4642 /*include_nondeduced_p=*/false);
4644 for (i = 0; i < ntparms; ++i)
4645 if (tpd.parms[i] == 0)
4647 /* One of the template parms was not used in a deduced context in the
4648 specialization. */
4649 if (!did_error_intro)
4651 error ("template parameters not deducible in "
4652 "partial specialization:");
4653 did_error_intro = true;
4656 inform (input_location, " %qD",
4657 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4660 if (did_error_intro)
4661 return error_mark_node;
4663 /* [temp.class.spec]
4665 The argument list of the specialization shall not be identical to
4666 the implicit argument list of the primary template. */
4667 tree main_args
4668 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4669 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4670 && (!flag_concepts
4671 || !strictly_subsumes (current_template_constraints (),
4672 get_constraints (maintmpl))))
4674 if (!flag_concepts)
4675 error ("partial specialization %q+D does not specialize "
4676 "any template arguments", decl);
4677 else
4678 error ("partial specialization %q+D does not specialize any "
4679 "template arguments and is not more constrained than", decl);
4680 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4683 /* A partial specialization that replaces multiple parameters of the
4684 primary template with a pack expansion is less specialized for those
4685 parameters. */
4686 if (nargs < DECL_NTPARMS (maintmpl))
4688 error ("partial specialization is not more specialized than the "
4689 "primary template because it replaces multiple parameters "
4690 "with a pack expansion");
4691 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4692 /* Avoid crash in process_partial_specialization. */
4693 return decl;
4696 /* If we aren't in a dependent class, we can actually try deduction. */
4697 else if (tpd.level == 1
4698 /* FIXME we should be able to handle a partial specialization of a
4699 partial instantiation, but currently we can't (c++/41727). */
4700 && TMPL_ARGS_DEPTH (specargs) == 1
4701 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4703 if (permerror (input_location, "partial specialization %qD is not "
4704 "more specialized than", decl))
4705 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4706 maintmpl);
4709 /* [temp.class.spec]
4711 A partially specialized non-type argument expression shall not
4712 involve template parameters of the partial specialization except
4713 when the argument expression is a simple identifier.
4715 The type of a template parameter corresponding to a specialized
4716 non-type argument shall not be dependent on a parameter of the
4717 specialization.
4719 Also, we verify that pack expansions only occur at the
4720 end of the argument list. */
4721 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4722 tpd2.parms = 0;
4723 for (i = 0; i < nargs; ++i)
4725 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4726 tree arg = TREE_VEC_ELT (inner_args, i);
4727 tree packed_args = NULL_TREE;
4728 int j, len = 1;
4730 if (ARGUMENT_PACK_P (arg))
4732 /* Extract the arguments from the argument pack. We'll be
4733 iterating over these in the following loop. */
4734 packed_args = ARGUMENT_PACK_ARGS (arg);
4735 len = TREE_VEC_LENGTH (packed_args);
4738 for (j = 0; j < len; j++)
4740 if (packed_args)
4741 /* Get the Jth argument in the parameter pack. */
4742 arg = TREE_VEC_ELT (packed_args, j);
4744 if (PACK_EXPANSION_P (arg))
4746 /* Pack expansions must come at the end of the
4747 argument list. */
4748 if ((packed_args && j < len - 1)
4749 || (!packed_args && i < nargs - 1))
4751 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4752 error ("parameter pack argument %qE must be at the "
4753 "end of the template argument list", arg);
4754 else
4755 error ("parameter pack argument %qT must be at the "
4756 "end of the template argument list", arg);
4760 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4761 /* We only care about the pattern. */
4762 arg = PACK_EXPANSION_PATTERN (arg);
4764 if (/* These first two lines are the `non-type' bit. */
4765 !TYPE_P (arg)
4766 && TREE_CODE (arg) != TEMPLATE_DECL
4767 /* This next two lines are the `argument expression is not just a
4768 simple identifier' condition and also the `specialized
4769 non-type argument' bit. */
4770 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4771 && !(REFERENCE_REF_P (arg)
4772 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4774 if ((!packed_args && tpd.arg_uses_template_parms[i])
4775 || (packed_args && uses_template_parms (arg)))
4776 error ("template argument %qE involves template parameter(s)",
4777 arg);
4778 else
4780 /* Look at the corresponding template parameter,
4781 marking which template parameters its type depends
4782 upon. */
4783 tree type = TREE_TYPE (parm);
4785 if (!tpd2.parms)
4787 /* We haven't yet initialized TPD2. Do so now. */
4788 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4789 /* The number of parameters here is the number in the
4790 main template, which, as checked in the assertion
4791 above, is NARGS. */
4792 tpd2.parms = XALLOCAVEC (int, nargs);
4793 tpd2.level =
4794 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4797 /* Mark the template parameters. But this time, we're
4798 looking for the template parameters of the main
4799 template, not in the specialization. */
4800 tpd2.current_arg = i;
4801 tpd2.arg_uses_template_parms[i] = 0;
4802 memset (tpd2.parms, 0, sizeof (int) * nargs);
4803 for_each_template_parm (type,
4804 &mark_template_parm,
4805 &tpd2,
4806 NULL,
4807 /*include_nondeduced_p=*/false);
4809 if (tpd2.arg_uses_template_parms [i])
4811 /* The type depended on some template parameters.
4812 If they are fully specialized in the
4813 specialization, that's OK. */
4814 int j;
4815 int count = 0;
4816 for (j = 0; j < nargs; ++j)
4817 if (tpd2.parms[j] != 0
4818 && tpd.arg_uses_template_parms [j])
4819 ++count;
4820 if (count != 0)
4821 error_n (input_location, count,
4822 "type %qT of template argument %qE depends "
4823 "on a template parameter",
4824 "type %qT of template argument %qE depends "
4825 "on template parameters",
4826 type,
4827 arg);
4834 /* We should only get here once. */
4835 if (TREE_CODE (decl) == TYPE_DECL)
4836 gcc_assert (!COMPLETE_TYPE_P (type));
4838 // Build the template decl.
4839 tree tmpl = build_template_decl (decl, current_template_parms,
4840 DECL_MEMBER_TEMPLATE_P (maintmpl));
4841 TREE_TYPE (tmpl) = type;
4842 DECL_TEMPLATE_RESULT (tmpl) = decl;
4843 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4844 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4845 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4847 /* Give template template parms a DECL_CONTEXT of the template
4848 for which they are a parameter. */
4849 for (i = 0; i < ntparms; ++i)
4851 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4852 if (TREE_CODE (parm) == TEMPLATE_DECL)
4853 DECL_CONTEXT (parm) = tmpl;
4856 if (VAR_P (decl))
4857 /* We didn't register this in check_explicit_specialization so we could
4858 wait until the constraints were set. */
4859 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4860 else
4861 associate_classtype_constraints (type);
4863 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4864 = tree_cons (specargs, tmpl,
4865 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4866 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4868 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4869 inst = TREE_CHAIN (inst))
4871 tree instance = TREE_VALUE (inst);
4872 if (TYPE_P (instance)
4873 ? (COMPLETE_TYPE_P (instance)
4874 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4875 : DECL_TEMPLATE_INSTANTIATION (instance))
4877 tree spec = most_specialized_partial_spec (instance, tf_none);
4878 tree inst_decl = (DECL_P (instance)
4879 ? instance : TYPE_NAME (instance));
4880 if (!spec)
4881 /* OK */;
4882 else if (spec == error_mark_node)
4883 permerror (input_location,
4884 "declaration of %qD ambiguates earlier template "
4885 "instantiation for %qD", decl, inst_decl);
4886 else if (TREE_VALUE (spec) == tmpl)
4887 permerror (input_location,
4888 "partial specialization of %qD after instantiation "
4889 "of %qD", decl, inst_decl);
4893 return decl;
4896 /* PARM is a template parameter of some form; return the corresponding
4897 TEMPLATE_PARM_INDEX. */
4899 static tree
4900 get_template_parm_index (tree parm)
4902 if (TREE_CODE (parm) == PARM_DECL
4903 || TREE_CODE (parm) == CONST_DECL)
4904 parm = DECL_INITIAL (parm);
4905 else if (TREE_CODE (parm) == TYPE_DECL
4906 || TREE_CODE (parm) == TEMPLATE_DECL)
4907 parm = TREE_TYPE (parm);
4908 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4909 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4910 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4911 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4912 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4913 return parm;
4916 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4917 parameter packs used by the template parameter PARM. */
4919 static void
4920 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4922 /* A type parm can't refer to another parm. */
4923 if (TREE_CODE (parm) == TYPE_DECL)
4924 return;
4925 else if (TREE_CODE (parm) == PARM_DECL)
4927 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4928 ppd, ppd->visited);
4929 return;
4932 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4934 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4935 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4936 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4939 /* PARM is a template parameter pack. Return any parameter packs used in
4940 its type or the type of any of its template parameters. If there are
4941 any such packs, it will be instantiated into a fixed template parameter
4942 list by partial instantiation rather than be fully deduced. */
4944 tree
4945 fixed_parameter_pack_p (tree parm)
4947 /* This can only be true in a member template. */
4948 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4949 return NULL_TREE;
4950 /* This can only be true for a parameter pack. */
4951 if (!template_parameter_pack_p (parm))
4952 return NULL_TREE;
4953 /* A type parm can't refer to another parm. */
4954 if (TREE_CODE (parm) == TYPE_DECL)
4955 return NULL_TREE;
4957 tree parameter_packs = NULL_TREE;
4958 struct find_parameter_pack_data ppd;
4959 ppd.parameter_packs = &parameter_packs;
4960 ppd.visited = new hash_set<tree>;
4961 ppd.type_pack_expansion_p = false;
4963 fixed_parameter_pack_p_1 (parm, &ppd);
4965 delete ppd.visited;
4966 return parameter_packs;
4969 /* Check that a template declaration's use of default arguments and
4970 parameter packs is not invalid. Here, PARMS are the template
4971 parameters. IS_PRIMARY is true if DECL is the thing declared by
4972 a primary template. IS_PARTIAL is true if DECL is a partial
4973 specialization.
4975 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4976 declaration (but not a definition); 1 indicates a declaration, 2
4977 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4978 emitted for extraneous default arguments.
4980 Returns TRUE if there were no errors found, FALSE otherwise. */
4982 bool
4983 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4984 bool is_partial, int is_friend_decl)
4986 const char *msg;
4987 int last_level_to_check;
4988 tree parm_level;
4989 bool no_errors = true;
4991 /* [temp.param]
4993 A default template-argument shall not be specified in a
4994 function template declaration or a function template definition, nor
4995 in the template-parameter-list of the definition of a member of a
4996 class template. */
4998 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
4999 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5000 /* You can't have a function template declaration in a local
5001 scope, nor you can you define a member of a class template in a
5002 local scope. */
5003 return true;
5005 if ((TREE_CODE (decl) == TYPE_DECL
5006 && TREE_TYPE (decl)
5007 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5008 || (TREE_CODE (decl) == FUNCTION_DECL
5009 && LAMBDA_FUNCTION_P (decl)))
5010 /* A lambda doesn't have an explicit declaration; don't complain
5011 about the parms of the enclosing class. */
5012 return true;
5014 if (current_class_type
5015 && !TYPE_BEING_DEFINED (current_class_type)
5016 && DECL_LANG_SPECIFIC (decl)
5017 && DECL_DECLARES_FUNCTION_P (decl)
5018 /* If this is either a friend defined in the scope of the class
5019 or a member function. */
5020 && (DECL_FUNCTION_MEMBER_P (decl)
5021 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5022 : DECL_FRIEND_CONTEXT (decl)
5023 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5024 : false)
5025 /* And, if it was a member function, it really was defined in
5026 the scope of the class. */
5027 && (!DECL_FUNCTION_MEMBER_P (decl)
5028 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5029 /* We already checked these parameters when the template was
5030 declared, so there's no need to do it again now. This function
5031 was defined in class scope, but we're processing its body now
5032 that the class is complete. */
5033 return true;
5035 /* Core issue 226 (C++0x only): the following only applies to class
5036 templates. */
5037 if (is_primary
5038 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5040 /* [temp.param]
5042 If a template-parameter has a default template-argument, all
5043 subsequent template-parameters shall have a default
5044 template-argument supplied. */
5045 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5047 tree inner_parms = TREE_VALUE (parm_level);
5048 int ntparms = TREE_VEC_LENGTH (inner_parms);
5049 int seen_def_arg_p = 0;
5050 int i;
5052 for (i = 0; i < ntparms; ++i)
5054 tree parm = TREE_VEC_ELT (inner_parms, i);
5056 if (parm == error_mark_node)
5057 continue;
5059 if (TREE_PURPOSE (parm))
5060 seen_def_arg_p = 1;
5061 else if (seen_def_arg_p
5062 && !template_parameter_pack_p (TREE_VALUE (parm)))
5064 error ("no default argument for %qD", TREE_VALUE (parm));
5065 /* For better subsequent error-recovery, we indicate that
5066 there should have been a default argument. */
5067 TREE_PURPOSE (parm) = error_mark_node;
5068 no_errors = false;
5070 else if (!is_partial
5071 && !is_friend_decl
5072 /* Don't complain about an enclosing partial
5073 specialization. */
5074 && parm_level == parms
5075 && TREE_CODE (decl) == TYPE_DECL
5076 && i < ntparms - 1
5077 && template_parameter_pack_p (TREE_VALUE (parm))
5078 /* A fixed parameter pack will be partially
5079 instantiated into a fixed length list. */
5080 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5082 /* A primary class template can only have one
5083 parameter pack, at the end of the template
5084 parameter list. */
5086 error ("parameter pack %q+D must be at the end of the"
5087 " template parameter list", TREE_VALUE (parm));
5089 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5090 = error_mark_node;
5091 no_errors = false;
5097 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5098 || is_partial
5099 || !is_primary
5100 || is_friend_decl)
5101 /* For an ordinary class template, default template arguments are
5102 allowed at the innermost level, e.g.:
5103 template <class T = int>
5104 struct S {};
5105 but, in a partial specialization, they're not allowed even
5106 there, as we have in [temp.class.spec]:
5108 The template parameter list of a specialization shall not
5109 contain default template argument values.
5111 So, for a partial specialization, or for a function template
5112 (in C++98/C++03), we look at all of them. */
5114 else
5115 /* But, for a primary class template that is not a partial
5116 specialization we look at all template parameters except the
5117 innermost ones. */
5118 parms = TREE_CHAIN (parms);
5120 /* Figure out what error message to issue. */
5121 if (is_friend_decl == 2)
5122 msg = G_("default template arguments may not be used in function template "
5123 "friend re-declaration");
5124 else if (is_friend_decl)
5125 msg = G_("default template arguments may not be used in function template "
5126 "friend declarations");
5127 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5128 msg = G_("default template arguments may not be used in function templates "
5129 "without -std=c++11 or -std=gnu++11");
5130 else if (is_partial)
5131 msg = G_("default template arguments may not be used in "
5132 "partial specializations");
5133 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5134 msg = G_("default argument for template parameter for class enclosing %qD");
5135 else
5136 /* Per [temp.param]/9, "A default template-argument shall not be
5137 specified in the template-parameter-lists of the definition of
5138 a member of a class template that appears outside of the member's
5139 class.", thus if we aren't handling a member of a class template
5140 there is no need to examine the parameters. */
5141 return true;
5143 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5144 /* If we're inside a class definition, there's no need to
5145 examine the parameters to the class itself. On the one
5146 hand, they will be checked when the class is defined, and,
5147 on the other, default arguments are valid in things like:
5148 template <class T = double>
5149 struct S { template <class U> void f(U); };
5150 Here the default argument for `S' has no bearing on the
5151 declaration of `f'. */
5152 last_level_to_check = template_class_depth (current_class_type) + 1;
5153 else
5154 /* Check everything. */
5155 last_level_to_check = 0;
5157 for (parm_level = parms;
5158 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5159 parm_level = TREE_CHAIN (parm_level))
5161 tree inner_parms = TREE_VALUE (parm_level);
5162 int i;
5163 int ntparms;
5165 ntparms = TREE_VEC_LENGTH (inner_parms);
5166 for (i = 0; i < ntparms; ++i)
5168 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5169 continue;
5171 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5173 if (msg)
5175 no_errors = false;
5176 if (is_friend_decl == 2)
5177 return no_errors;
5179 error (msg, decl);
5180 msg = 0;
5183 /* Clear out the default argument so that we are not
5184 confused later. */
5185 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5189 /* At this point, if we're still interested in issuing messages,
5190 they must apply to classes surrounding the object declared. */
5191 if (msg)
5192 msg = G_("default argument for template parameter for class "
5193 "enclosing %qD");
5196 return no_errors;
5199 /* Worker for push_template_decl_real, called via
5200 for_each_template_parm. DATA is really an int, indicating the
5201 level of the parameters we are interested in. If T is a template
5202 parameter of that level, return nonzero. */
5204 static int
5205 template_parm_this_level_p (tree t, void* data)
5207 int this_level = *(int *)data;
5208 int level;
5210 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5211 level = TEMPLATE_PARM_LEVEL (t);
5212 else
5213 level = TEMPLATE_TYPE_LEVEL (t);
5214 return level == this_level;
5217 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5218 DATA is really an int, indicating the innermost outer level of parameters.
5219 If T is a template parameter of that level or further out, return
5220 nonzero. */
5222 static int
5223 template_parm_outer_level (tree t, void *data)
5225 int this_level = *(int *)data;
5226 int level;
5228 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5229 level = TEMPLATE_PARM_LEVEL (t);
5230 else
5231 level = TEMPLATE_TYPE_LEVEL (t);
5232 return level <= this_level;
5235 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5236 parameters given by current_template_args, or reuses a
5237 previously existing one, if appropriate. Returns the DECL, or an
5238 equivalent one, if it is replaced via a call to duplicate_decls.
5240 If IS_FRIEND is true, DECL is a friend declaration. */
5242 tree
5243 push_template_decl_real (tree decl, bool is_friend)
5245 tree tmpl;
5246 tree args;
5247 tree info;
5248 tree ctx;
5249 bool is_primary;
5250 bool is_partial;
5251 int new_template_p = 0;
5252 /* True if the template is a member template, in the sense of
5253 [temp.mem]. */
5254 bool member_template_p = false;
5256 if (decl == error_mark_node || !current_template_parms)
5257 return error_mark_node;
5259 /* See if this is a partial specialization. */
5260 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5261 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5262 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5263 || (VAR_P (decl)
5264 && DECL_LANG_SPECIFIC (decl)
5265 && DECL_TEMPLATE_SPECIALIZATION (decl)
5266 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5268 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5269 is_friend = true;
5271 if (is_friend)
5272 /* For a friend, we want the context of the friend function, not
5273 the type of which it is a friend. */
5274 ctx = CP_DECL_CONTEXT (decl);
5275 else if (CP_DECL_CONTEXT (decl)
5276 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5277 /* In the case of a virtual function, we want the class in which
5278 it is defined. */
5279 ctx = CP_DECL_CONTEXT (decl);
5280 else
5281 /* Otherwise, if we're currently defining some class, the DECL
5282 is assumed to be a member of the class. */
5283 ctx = current_scope ();
5285 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5286 ctx = NULL_TREE;
5288 if (!DECL_CONTEXT (decl))
5289 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5291 /* See if this is a primary template. */
5292 if (is_friend && ctx
5293 && uses_template_parms_level (ctx, processing_template_decl))
5294 /* A friend template that specifies a class context, i.e.
5295 template <typename T> friend void A<T>::f();
5296 is not primary. */
5297 is_primary = false;
5298 else if (TREE_CODE (decl) == TYPE_DECL
5299 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5300 is_primary = false;
5301 else
5302 is_primary = template_parm_scope_p ();
5304 if (is_primary)
5306 warning (OPT_Wtemplates, "template %qD declared", decl);
5308 if (DECL_CLASS_SCOPE_P (decl))
5309 member_template_p = true;
5310 if (TREE_CODE (decl) == TYPE_DECL
5311 && anon_aggrname_p (DECL_NAME (decl)))
5313 error ("template class without a name");
5314 return error_mark_node;
5316 else if (TREE_CODE (decl) == FUNCTION_DECL)
5318 if (member_template_p)
5320 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5321 error ("member template %qD may not have virt-specifiers", decl);
5323 if (DECL_DESTRUCTOR_P (decl))
5325 /* [temp.mem]
5327 A destructor shall not be a member template. */
5328 error ("destructor %qD declared as member template", decl);
5329 return error_mark_node;
5331 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5332 && (!prototype_p (TREE_TYPE (decl))
5333 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5334 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5335 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5336 == void_list_node)))
5338 /* [basic.stc.dynamic.allocation]
5340 An allocation function can be a function
5341 template. ... Template allocation functions shall
5342 have two or more parameters. */
5343 error ("invalid template declaration of %qD", decl);
5344 return error_mark_node;
5347 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5348 && CLASS_TYPE_P (TREE_TYPE (decl)))
5350 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5351 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5352 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5354 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5355 if (TREE_CODE (t) == TYPE_DECL)
5356 t = TREE_TYPE (t);
5357 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5358 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5361 else if (TREE_CODE (decl) == TYPE_DECL
5362 && TYPE_DECL_ALIAS_P (decl))
5363 /* alias-declaration */
5364 gcc_assert (!DECL_ARTIFICIAL (decl));
5365 else if (VAR_P (decl))
5366 /* C++14 variable template. */;
5367 else
5369 error ("template declaration of %q#D", decl);
5370 return error_mark_node;
5374 /* Check to see that the rules regarding the use of default
5375 arguments are not being violated. */
5376 check_default_tmpl_args (decl, current_template_parms,
5377 is_primary, is_partial, /*is_friend_decl=*/0);
5379 /* Ensure that there are no parameter packs in the type of this
5380 declaration that have not been expanded. */
5381 if (TREE_CODE (decl) == FUNCTION_DECL)
5383 /* Check each of the arguments individually to see if there are
5384 any bare parameter packs. */
5385 tree type = TREE_TYPE (decl);
5386 tree arg = DECL_ARGUMENTS (decl);
5387 tree argtype = TYPE_ARG_TYPES (type);
5389 while (arg && argtype)
5391 if (!DECL_PACK_P (arg)
5392 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5394 /* This is a PARM_DECL that contains unexpanded parameter
5395 packs. We have already complained about this in the
5396 check_for_bare_parameter_packs call, so just replace
5397 these types with ERROR_MARK_NODE. */
5398 TREE_TYPE (arg) = error_mark_node;
5399 TREE_VALUE (argtype) = error_mark_node;
5402 arg = DECL_CHAIN (arg);
5403 argtype = TREE_CHAIN (argtype);
5406 /* Check for bare parameter packs in the return type and the
5407 exception specifiers. */
5408 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5409 /* Errors were already issued, set return type to int
5410 as the frontend doesn't expect error_mark_node as
5411 the return type. */
5412 TREE_TYPE (type) = integer_type_node;
5413 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5414 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5416 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5417 && TYPE_DECL_ALIAS_P (decl))
5418 ? DECL_ORIGINAL_TYPE (decl)
5419 : TREE_TYPE (decl)))
5421 TREE_TYPE (decl) = error_mark_node;
5422 return error_mark_node;
5425 if (is_partial)
5426 return process_partial_specialization (decl);
5428 args = current_template_args ();
5430 if (!ctx
5431 || TREE_CODE (ctx) == FUNCTION_DECL
5432 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5433 || (TREE_CODE (decl) == TYPE_DECL
5434 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5435 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5437 if (DECL_LANG_SPECIFIC (decl)
5438 && DECL_TEMPLATE_INFO (decl)
5439 && DECL_TI_TEMPLATE (decl))
5440 tmpl = DECL_TI_TEMPLATE (decl);
5441 /* If DECL is a TYPE_DECL for a class-template, then there won't
5442 be DECL_LANG_SPECIFIC. The information equivalent to
5443 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5444 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5445 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5446 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5448 /* Since a template declaration already existed for this
5449 class-type, we must be redeclaring it here. Make sure
5450 that the redeclaration is valid. */
5451 redeclare_class_template (TREE_TYPE (decl),
5452 current_template_parms,
5453 current_template_constraints ());
5454 /* We don't need to create a new TEMPLATE_DECL; just use the
5455 one we already had. */
5456 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5458 else
5460 tmpl = build_template_decl (decl, current_template_parms,
5461 member_template_p);
5462 new_template_p = 1;
5464 if (DECL_LANG_SPECIFIC (decl)
5465 && DECL_TEMPLATE_SPECIALIZATION (decl))
5467 /* A specialization of a member template of a template
5468 class. */
5469 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5470 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5471 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5475 else
5477 tree a, t, current, parms;
5478 int i;
5479 tree tinfo = get_template_info (decl);
5481 if (!tinfo)
5483 error ("template definition of non-template %q#D", decl);
5484 return error_mark_node;
5487 tmpl = TI_TEMPLATE (tinfo);
5489 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5490 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5491 && DECL_TEMPLATE_SPECIALIZATION (decl)
5492 && DECL_MEMBER_TEMPLATE_P (tmpl))
5494 tree new_tmpl;
5496 /* The declaration is a specialization of a member
5497 template, declared outside the class. Therefore, the
5498 innermost template arguments will be NULL, so we
5499 replace them with the arguments determined by the
5500 earlier call to check_explicit_specialization. */
5501 args = DECL_TI_ARGS (decl);
5503 new_tmpl
5504 = build_template_decl (decl, current_template_parms,
5505 member_template_p);
5506 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5507 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5508 DECL_TI_TEMPLATE (decl) = new_tmpl;
5509 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5510 DECL_TEMPLATE_INFO (new_tmpl)
5511 = build_template_info (tmpl, args);
5513 register_specialization (new_tmpl,
5514 most_general_template (tmpl),
5515 args,
5516 is_friend, 0);
5517 return decl;
5520 /* Make sure the template headers we got make sense. */
5522 parms = DECL_TEMPLATE_PARMS (tmpl);
5523 i = TMPL_PARMS_DEPTH (parms);
5524 if (TMPL_ARGS_DEPTH (args) != i)
5526 error ("expected %d levels of template parms for %q#D, got %d",
5527 i, decl, TMPL_ARGS_DEPTH (args));
5528 DECL_INTERFACE_KNOWN (decl) = 1;
5529 return error_mark_node;
5531 else
5532 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5534 a = TMPL_ARGS_LEVEL (args, i);
5535 t = INNERMOST_TEMPLATE_PARMS (parms);
5537 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5539 if (current == decl)
5540 error ("got %d template parameters for %q#D",
5541 TREE_VEC_LENGTH (a), decl);
5542 else
5543 error ("got %d template parameters for %q#T",
5544 TREE_VEC_LENGTH (a), current);
5545 error (" but %d required", TREE_VEC_LENGTH (t));
5546 /* Avoid crash in import_export_decl. */
5547 DECL_INTERFACE_KNOWN (decl) = 1;
5548 return error_mark_node;
5551 if (current == decl)
5552 current = ctx;
5553 else if (current == NULL_TREE)
5554 /* Can happen in erroneous input. */
5555 break;
5556 else
5557 current = get_containing_scope (current);
5560 /* Check that the parms are used in the appropriate qualifying scopes
5561 in the declarator. */
5562 if (!comp_template_args
5563 (TI_ARGS (tinfo),
5564 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5566 error ("template arguments to %qD do not match original"
5567 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5568 if (!uses_template_parms (TI_ARGS (tinfo)))
5569 inform (input_location, "use %<template<>%> for"
5570 " an explicit specialization");
5571 /* Avoid crash in import_export_decl. */
5572 DECL_INTERFACE_KNOWN (decl) = 1;
5573 return error_mark_node;
5577 DECL_TEMPLATE_RESULT (tmpl) = decl;
5578 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5580 /* Push template declarations for global functions and types. Note
5581 that we do not try to push a global template friend declared in a
5582 template class; such a thing may well depend on the template
5583 parameters of the class. */
5584 if (new_template_p && !ctx
5585 && !(is_friend && template_class_depth (current_class_type) > 0))
5587 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5588 if (tmpl == error_mark_node)
5589 return error_mark_node;
5591 /* Hide template friend classes that haven't been declared yet. */
5592 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5594 DECL_ANTICIPATED (tmpl) = 1;
5595 DECL_FRIEND_P (tmpl) = 1;
5599 if (is_primary)
5601 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5603 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5605 /* Give template template parms a DECL_CONTEXT of the template
5606 for which they are a parameter. */
5607 parms = INNERMOST_TEMPLATE_PARMS (parms);
5608 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5610 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5611 if (TREE_CODE (parm) == TEMPLATE_DECL)
5612 DECL_CONTEXT (parm) = tmpl;
5615 if (TREE_CODE (decl) == TYPE_DECL
5616 && TYPE_DECL_ALIAS_P (decl)
5617 && complex_alias_template_p (tmpl))
5618 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5621 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5622 back to its most general template. If TMPL is a specialization,
5623 ARGS may only have the innermost set of arguments. Add the missing
5624 argument levels if necessary. */
5625 if (DECL_TEMPLATE_INFO (tmpl))
5626 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5628 info = build_template_info (tmpl, args);
5630 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5631 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5632 else
5634 if (is_primary)
5635 retrofit_lang_decl (decl);
5636 if (DECL_LANG_SPECIFIC (decl))
5637 DECL_TEMPLATE_INFO (decl) = info;
5640 if (flag_implicit_templates
5641 && !is_friend
5642 && TREE_PUBLIC (decl)
5643 && VAR_OR_FUNCTION_DECL_P (decl))
5644 /* Set DECL_COMDAT on template instantiations; if we force
5645 them to be emitted by explicit instantiation or -frepo,
5646 mark_needed will tell cgraph to do the right thing. */
5647 DECL_COMDAT (decl) = true;
5649 return DECL_TEMPLATE_RESULT (tmpl);
5652 tree
5653 push_template_decl (tree decl)
5655 return push_template_decl_real (decl, false);
5658 /* FN is an inheriting constructor that inherits from the constructor
5659 template INHERITED; turn FN into a constructor template with a matching
5660 template header. */
5662 tree
5663 add_inherited_template_parms (tree fn, tree inherited)
5665 tree inner_parms
5666 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5667 inner_parms = copy_node (inner_parms);
5668 tree parms
5669 = tree_cons (size_int (processing_template_decl + 1),
5670 inner_parms, current_template_parms);
5671 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5672 tree args = template_parms_to_args (parms);
5673 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5674 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5675 DECL_TEMPLATE_RESULT (tmpl) = fn;
5676 DECL_ARTIFICIAL (tmpl) = true;
5677 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5678 return tmpl;
5681 /* Called when a class template TYPE is redeclared with the indicated
5682 template PARMS, e.g.:
5684 template <class T> struct S;
5685 template <class T> struct S {}; */
5687 bool
5688 redeclare_class_template (tree type, tree parms, tree cons)
5690 tree tmpl;
5691 tree tmpl_parms;
5692 int i;
5694 if (!TYPE_TEMPLATE_INFO (type))
5696 error ("%qT is not a template type", type);
5697 return false;
5700 tmpl = TYPE_TI_TEMPLATE (type);
5701 if (!PRIMARY_TEMPLATE_P (tmpl))
5702 /* The type is nested in some template class. Nothing to worry
5703 about here; there are no new template parameters for the nested
5704 type. */
5705 return true;
5707 if (!parms)
5709 error ("template specifiers not specified in declaration of %qD",
5710 tmpl);
5711 return false;
5714 parms = INNERMOST_TEMPLATE_PARMS (parms);
5715 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5717 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5719 error_n (input_location, TREE_VEC_LENGTH (parms),
5720 "redeclared with %d template parameter",
5721 "redeclared with %d template parameters",
5722 TREE_VEC_LENGTH (parms));
5723 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5724 "previous declaration %qD used %d template parameter",
5725 "previous declaration %qD used %d template parameters",
5726 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5727 return false;
5730 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5732 tree tmpl_parm;
5733 tree parm;
5734 tree tmpl_default;
5735 tree parm_default;
5737 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5738 || TREE_VEC_ELT (parms, i) == error_mark_node)
5739 continue;
5741 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5742 if (error_operand_p (tmpl_parm))
5743 return false;
5745 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5746 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5747 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5749 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5750 TEMPLATE_DECL. */
5751 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5752 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5753 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5754 || (TREE_CODE (tmpl_parm) != PARM_DECL
5755 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5756 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5757 || (TREE_CODE (tmpl_parm) == PARM_DECL
5758 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5759 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5761 error ("template parameter %q+#D", tmpl_parm);
5762 error ("redeclared here as %q#D", parm);
5763 return false;
5766 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5768 /* We have in [temp.param]:
5770 A template-parameter may not be given default arguments
5771 by two different declarations in the same scope. */
5772 error_at (input_location, "redefinition of default argument for %q#D", parm);
5773 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5774 "original definition appeared here");
5775 return false;
5778 if (parm_default != NULL_TREE)
5779 /* Update the previous template parameters (which are the ones
5780 that will really count) with the new default value. */
5781 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5782 else if (tmpl_default != NULL_TREE)
5783 /* Update the new parameters, too; they'll be used as the
5784 parameters for any members. */
5785 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5787 /* Give each template template parm in this redeclaration a
5788 DECL_CONTEXT of the template for which they are a parameter. */
5789 if (TREE_CODE (parm) == TEMPLATE_DECL)
5791 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5792 DECL_CONTEXT (parm) = tmpl;
5795 if (TREE_CODE (parm) == TYPE_DECL)
5796 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5799 // Cannot redeclare a class template with a different set of constraints.
5800 if (!equivalent_constraints (get_constraints (tmpl), cons))
5802 error_at (input_location, "redeclaration %q#D with different "
5803 "constraints", tmpl);
5804 inform (DECL_SOURCE_LOCATION (tmpl),
5805 "original declaration appeared here");
5808 return true;
5811 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5812 to be used when the caller has already checked
5813 (processing_template_decl
5814 && !instantiation_dependent_expression_p (expr)
5815 && potential_constant_expression (expr))
5816 and cleared processing_template_decl. */
5818 tree
5819 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5821 return tsubst_copy_and_build (expr,
5822 /*args=*/NULL_TREE,
5823 complain,
5824 /*in_decl=*/NULL_TREE,
5825 /*function_p=*/false,
5826 /*integral_constant_expression_p=*/true);
5829 /* Simplify EXPR if it is a non-dependent expression. Returns the
5830 (possibly simplified) expression. */
5832 tree
5833 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5835 if (expr == NULL_TREE)
5836 return NULL_TREE;
5838 /* If we're in a template, but EXPR isn't value dependent, simplify
5839 it. We're supposed to treat:
5841 template <typename T> void f(T[1 + 1]);
5842 template <typename T> void f(T[2]);
5844 as two declarations of the same function, for example. */
5845 if (processing_template_decl
5846 && is_nondependent_constant_expression (expr))
5848 processing_template_decl_sentinel s;
5849 expr = instantiate_non_dependent_expr_internal (expr, complain);
5851 return expr;
5854 tree
5855 instantiate_non_dependent_expr (tree expr)
5857 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5860 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5861 an uninstantiated expression. */
5863 tree
5864 instantiate_non_dependent_or_null (tree expr)
5866 if (expr == NULL_TREE)
5867 return NULL_TREE;
5868 if (processing_template_decl)
5870 if (!is_nondependent_constant_expression (expr))
5871 expr = NULL_TREE;
5872 else
5874 processing_template_decl_sentinel s;
5875 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5878 return expr;
5881 /* True iff T is a specialization of a variable template. */
5883 bool
5884 variable_template_specialization_p (tree t)
5886 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5887 return false;
5888 tree tmpl = DECL_TI_TEMPLATE (t);
5889 return variable_template_p (tmpl);
5892 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5893 template declaration, or a TYPE_DECL for an alias declaration. */
5895 bool
5896 alias_type_or_template_p (tree t)
5898 if (t == NULL_TREE)
5899 return false;
5900 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5901 || (TYPE_P (t)
5902 && TYPE_NAME (t)
5903 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5904 || DECL_ALIAS_TEMPLATE_P (t));
5907 /* Return TRUE iff T is a specialization of an alias template. */
5909 bool
5910 alias_template_specialization_p (const_tree t)
5912 /* It's an alias template specialization if it's an alias and its
5913 TYPE_NAME is a specialization of a primary template. */
5914 if (TYPE_ALIAS_P (t))
5915 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5916 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5918 return false;
5921 /* An alias template is complex from a SFINAE perspective if a template-id
5922 using that alias can be ill-formed when the expansion is not, as with
5923 the void_t template. We determine this by checking whether the
5924 expansion for the alias template uses all its template parameters. */
5926 struct uses_all_template_parms_data
5928 int level;
5929 bool *seen;
5932 static int
5933 uses_all_template_parms_r (tree t, void *data_)
5935 struct uses_all_template_parms_data &data
5936 = *(struct uses_all_template_parms_data*)data_;
5937 tree idx = get_template_parm_index (t);
5939 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5940 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5941 return 0;
5944 static bool
5945 complex_alias_template_p (const_tree tmpl)
5947 struct uses_all_template_parms_data data;
5948 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5949 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5950 data.level = TMPL_PARMS_DEPTH (parms);
5951 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5952 data.seen = XALLOCAVEC (bool, len);
5953 for (int i = 0; i < len; ++i)
5954 data.seen[i] = false;
5956 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5957 for (int i = 0; i < len; ++i)
5958 if (!data.seen[i])
5959 return true;
5960 return false;
5963 /* Return TRUE iff T is a specialization of a complex alias template with
5964 dependent template-arguments. */
5966 bool
5967 dependent_alias_template_spec_p (const_tree t)
5969 if (!alias_template_specialization_p (t))
5970 return false;
5972 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5973 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5974 return false;
5976 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5977 if (!any_dependent_template_arguments_p (args))
5978 return false;
5980 return true;
5983 /* Return the number of innermost template parameters in TMPL. */
5985 static int
5986 num_innermost_template_parms (tree tmpl)
5988 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
5989 return TREE_VEC_LENGTH (parms);
5992 /* Return either TMPL or another template that it is equivalent to under DR
5993 1286: An alias that just changes the name of a template is equivalent to
5994 the other template. */
5996 static tree
5997 get_underlying_template (tree tmpl)
5999 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6000 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6002 /* Determine if the alias is equivalent to an underlying template. */
6003 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6004 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6005 if (!tinfo)
6006 break;
6008 tree underlying = TI_TEMPLATE (tinfo);
6009 if (!PRIMARY_TEMPLATE_P (underlying)
6010 || (num_innermost_template_parms (tmpl)
6011 != num_innermost_template_parms (underlying)))
6012 break;
6014 tree alias_args = INNERMOST_TEMPLATE_ARGS
6015 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6016 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6017 break;
6019 /* Alias is equivalent. Strip it and repeat. */
6020 tmpl = underlying;
6023 return tmpl;
6026 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6027 must be a reference-to-function or a pointer-to-function type, as specified
6028 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6029 and check that the resulting function has external linkage. */
6031 static tree
6032 convert_nontype_argument_function (tree type, tree expr,
6033 tsubst_flags_t complain)
6035 tree fns = expr;
6036 tree fn, fn_no_ptr;
6037 linkage_kind linkage;
6039 fn = instantiate_type (type, fns, tf_none);
6040 if (fn == error_mark_node)
6041 return error_mark_node;
6043 if (value_dependent_expression_p (fn))
6044 goto accept;
6046 fn_no_ptr = strip_fnptr_conv (fn);
6047 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6048 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6049 if (BASELINK_P (fn_no_ptr))
6050 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6052 /* [temp.arg.nontype]/1
6054 A template-argument for a non-type, non-template template-parameter
6055 shall be one of:
6056 [...]
6057 -- the address of an object or function with external [C++11: or
6058 internal] linkage. */
6060 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6062 if (complain & tf_error)
6064 error ("%qE is not a valid template argument for type %qT",
6065 expr, type);
6066 if (TYPE_PTR_P (type))
6067 inform (input_location, "it must be the address of a function "
6068 "with external linkage");
6069 else
6070 inform (input_location, "it must be the name of a function with "
6071 "external linkage");
6073 return NULL_TREE;
6076 linkage = decl_linkage (fn_no_ptr);
6077 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6079 if (complain & tf_error)
6081 if (cxx_dialect >= cxx11)
6082 error ("%qE is not a valid template argument for type %qT "
6083 "because %qD has no linkage",
6084 expr, type, fn_no_ptr);
6085 else
6086 error ("%qE is not a valid template argument for type %qT "
6087 "because %qD does not have external linkage",
6088 expr, type, fn_no_ptr);
6090 return NULL_TREE;
6093 accept:
6094 if (TREE_CODE (type) == REFERENCE_TYPE)
6095 fn = build_address (fn);
6096 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6097 fn = build_nop (type, fn);
6099 return fn;
6102 /* Subroutine of convert_nontype_argument.
6103 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6104 Emit an error otherwise. */
6106 static bool
6107 check_valid_ptrmem_cst_expr (tree type, tree expr,
6108 tsubst_flags_t complain)
6110 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6111 tree orig_expr = expr;
6112 STRIP_NOPS (expr);
6113 if (null_ptr_cst_p (expr))
6114 return true;
6115 if (TREE_CODE (expr) == PTRMEM_CST
6116 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6117 PTRMEM_CST_CLASS (expr)))
6118 return true;
6119 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6120 return true;
6121 if (processing_template_decl
6122 && TREE_CODE (expr) == ADDR_EXPR
6123 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6124 return true;
6125 if (complain & tf_error)
6127 error_at (loc, "%qE is not a valid template argument for type %qT",
6128 orig_expr, type);
6129 if (TREE_CODE (expr) != PTRMEM_CST)
6130 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6131 else
6132 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6134 return false;
6137 /* Returns TRUE iff the address of OP is value-dependent.
6139 14.6.2.4 [temp.dep.temp]:
6140 A non-integral non-type template-argument is dependent if its type is
6141 dependent or it has either of the following forms
6142 qualified-id
6143 & qualified-id
6144 and contains a nested-name-specifier which specifies a class-name that
6145 names a dependent type.
6147 We generalize this to just say that the address of a member of a
6148 dependent class is value-dependent; the above doesn't cover the
6149 address of a static data member named with an unqualified-id. */
6151 static bool
6152 has_value_dependent_address (tree op)
6154 /* We could use get_inner_reference here, but there's no need;
6155 this is only relevant for template non-type arguments, which
6156 can only be expressed as &id-expression. */
6157 if (DECL_P (op))
6159 tree ctx = CP_DECL_CONTEXT (op);
6160 if (TYPE_P (ctx) && dependent_type_p (ctx))
6161 return true;
6164 return false;
6167 /* The next set of functions are used for providing helpful explanatory
6168 diagnostics for failed overload resolution. Their messages should be
6169 indented by two spaces for consistency with the messages in
6170 call.c */
6172 static int
6173 unify_success (bool /*explain_p*/)
6175 return 0;
6178 /* Other failure functions should call this one, to provide a single function
6179 for setting a breakpoint on. */
6181 static int
6182 unify_invalid (bool /*explain_p*/)
6184 return 1;
6187 static int
6188 unify_parameter_deduction_failure (bool explain_p, tree parm)
6190 if (explain_p)
6191 inform (input_location,
6192 " couldn't deduce template parameter %qD", parm);
6193 return unify_invalid (explain_p);
6196 static int
6197 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6199 if (explain_p)
6200 inform (input_location,
6201 " types %qT and %qT have incompatible cv-qualifiers",
6202 parm, arg);
6203 return unify_invalid (explain_p);
6206 static int
6207 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6209 if (explain_p)
6210 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6211 return unify_invalid (explain_p);
6214 static int
6215 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6217 if (explain_p)
6218 inform (input_location,
6219 " template parameter %qD is not a parameter pack, but "
6220 "argument %qD is",
6221 parm, arg);
6222 return unify_invalid (explain_p);
6225 static int
6226 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6228 if (explain_p)
6229 inform (input_location,
6230 " template argument %qE does not match "
6231 "pointer-to-member constant %qE",
6232 arg, parm);
6233 return unify_invalid (explain_p);
6236 static int
6237 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6239 if (explain_p)
6240 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6241 return unify_invalid (explain_p);
6244 static int
6245 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6247 if (explain_p)
6248 inform (input_location,
6249 " inconsistent parameter pack deduction with %qT and %qT",
6250 old_arg, new_arg);
6251 return unify_invalid (explain_p);
6254 static int
6255 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6257 if (explain_p)
6259 if (TYPE_P (parm))
6260 inform (input_location,
6261 " deduced conflicting types for parameter %qT (%qT and %qT)",
6262 parm, first, second);
6263 else
6264 inform (input_location,
6265 " deduced conflicting values for non-type parameter "
6266 "%qE (%qE and %qE)", parm, first, second);
6268 return unify_invalid (explain_p);
6271 static int
6272 unify_vla_arg (bool explain_p, tree arg)
6274 if (explain_p)
6275 inform (input_location,
6276 " variable-sized array type %qT is not "
6277 "a valid template argument",
6278 arg);
6279 return unify_invalid (explain_p);
6282 static int
6283 unify_method_type_error (bool explain_p, tree arg)
6285 if (explain_p)
6286 inform (input_location,
6287 " member function type %qT is not a valid template argument",
6288 arg);
6289 return unify_invalid (explain_p);
6292 static int
6293 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6295 if (explain_p)
6297 if (least_p)
6298 inform_n (input_location, wanted,
6299 " candidate expects at least %d argument, %d provided",
6300 " candidate expects at least %d arguments, %d provided",
6301 wanted, have);
6302 else
6303 inform_n (input_location, wanted,
6304 " candidate expects %d argument, %d provided",
6305 " candidate expects %d arguments, %d provided",
6306 wanted, have);
6308 return unify_invalid (explain_p);
6311 static int
6312 unify_too_many_arguments (bool explain_p, int have, int wanted)
6314 return unify_arity (explain_p, have, wanted);
6317 static int
6318 unify_too_few_arguments (bool explain_p, int have, int wanted,
6319 bool least_p = false)
6321 return unify_arity (explain_p, have, wanted, least_p);
6324 static int
6325 unify_arg_conversion (bool explain_p, tree to_type,
6326 tree from_type, tree arg)
6328 if (explain_p)
6329 inform (EXPR_LOC_OR_LOC (arg, input_location),
6330 " cannot convert %qE (type %qT) to type %qT",
6331 arg, from_type, to_type);
6332 return unify_invalid (explain_p);
6335 static int
6336 unify_no_common_base (bool explain_p, enum template_base_result r,
6337 tree parm, tree arg)
6339 if (explain_p)
6340 switch (r)
6342 case tbr_ambiguous_baseclass:
6343 inform (input_location, " %qT is an ambiguous base class of %qT",
6344 parm, arg);
6345 break;
6346 default:
6347 inform (input_location, " %qT is not derived from %qT", arg, parm);
6348 break;
6350 return unify_invalid (explain_p);
6353 static int
6354 unify_inconsistent_template_template_parameters (bool explain_p)
6356 if (explain_p)
6357 inform (input_location,
6358 " template parameters of a template template argument are "
6359 "inconsistent with other deduced template arguments");
6360 return unify_invalid (explain_p);
6363 static int
6364 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6366 if (explain_p)
6367 inform (input_location,
6368 " can't deduce a template for %qT from non-template type %qT",
6369 parm, arg);
6370 return unify_invalid (explain_p);
6373 static int
6374 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6376 if (explain_p)
6377 inform (input_location,
6378 " template argument %qE does not match %qE", arg, parm);
6379 return unify_invalid (explain_p);
6382 /* Attempt to convert the non-type template parameter EXPR to the
6383 indicated TYPE. If the conversion is successful, return the
6384 converted value. If the conversion is unsuccessful, return
6385 NULL_TREE if we issued an error message, or error_mark_node if we
6386 did not. We issue error messages for out-and-out bad template
6387 parameters, but not simply because the conversion failed, since we
6388 might be just trying to do argument deduction. Both TYPE and EXPR
6389 must be non-dependent.
6391 The conversion follows the special rules described in
6392 [temp.arg.nontype], and it is much more strict than an implicit
6393 conversion.
6395 This function is called twice for each template argument (see
6396 lookup_template_class for a more accurate description of this
6397 problem). This means that we need to handle expressions which
6398 are not valid in a C++ source, but can be created from the
6399 first call (for instance, casts to perform conversions). These
6400 hacks can go away after we fix the double coercion problem. */
6402 static tree
6403 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6405 tree expr_type;
6406 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6407 tree orig_expr = expr;
6409 /* Detect immediately string literals as invalid non-type argument.
6410 This special-case is not needed for correctness (we would easily
6411 catch this later), but only to provide better diagnostic for this
6412 common user mistake. As suggested by DR 100, we do not mention
6413 linkage issues in the diagnostic as this is not the point. */
6414 /* FIXME we're making this OK. */
6415 if (TREE_CODE (expr) == STRING_CST)
6417 if (complain & tf_error)
6418 error ("%qE is not a valid template argument for type %qT "
6419 "because string literals can never be used in this context",
6420 expr, type);
6421 return NULL_TREE;
6424 /* Add the ADDR_EXPR now for the benefit of
6425 value_dependent_expression_p. */
6426 if (TYPE_PTROBV_P (type)
6427 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6429 expr = decay_conversion (expr, complain);
6430 if (expr == error_mark_node)
6431 return error_mark_node;
6434 /* If we are in a template, EXPR may be non-dependent, but still
6435 have a syntactic, rather than semantic, form. For example, EXPR
6436 might be a SCOPE_REF, rather than the VAR_DECL to which the
6437 SCOPE_REF refers. Preserving the qualifying scope is necessary
6438 so that access checking can be performed when the template is
6439 instantiated -- but here we need the resolved form so that we can
6440 convert the argument. */
6441 bool non_dep = false;
6442 if (TYPE_REF_OBJ_P (type)
6443 && has_value_dependent_address (expr))
6444 /* If we want the address and it's value-dependent, don't fold. */;
6445 else if (processing_template_decl
6446 && is_nondependent_constant_expression (expr))
6447 non_dep = true;
6448 if (error_operand_p (expr))
6449 return error_mark_node;
6450 expr_type = TREE_TYPE (expr);
6452 /* If the argument is non-dependent, perform any conversions in
6453 non-dependent context as well. */
6454 processing_template_decl_sentinel s (non_dep);
6455 if (non_dep)
6456 expr = instantiate_non_dependent_expr_internal (expr, complain);
6458 if (value_dependent_expression_p (expr))
6459 expr = canonicalize_expr_argument (expr, complain);
6461 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6462 to a non-type argument of "nullptr". */
6463 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6464 expr = fold_simple (convert (type, expr));
6466 /* In C++11, integral or enumeration non-type template arguments can be
6467 arbitrary constant expressions. Pointer and pointer to
6468 member arguments can be general constant expressions that evaluate
6469 to a null value, but otherwise still need to be of a specific form. */
6470 if (cxx_dialect >= cxx11)
6472 if (TREE_CODE (expr) == PTRMEM_CST)
6473 /* A PTRMEM_CST is already constant, and a valid template
6474 argument for a parameter of pointer to member type, we just want
6475 to leave it in that form rather than lower it to a
6476 CONSTRUCTOR. */;
6477 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6478 || cxx_dialect >= cxx17)
6480 /* C++17: A template-argument for a non-type template-parameter shall
6481 be a converted constant expression (8.20) of the type of the
6482 template-parameter. */
6483 expr = build_converted_constant_expr (type, expr, complain);
6484 if (expr == error_mark_node)
6485 return error_mark_node;
6486 expr = maybe_constant_value (expr);
6487 expr = convert_from_reference (expr);
6489 else if (TYPE_PTR_OR_PTRMEM_P (type))
6491 tree folded = maybe_constant_value (expr);
6492 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6493 : null_member_pointer_value_p (folded))
6494 expr = folded;
6498 if (TREE_CODE (type) == REFERENCE_TYPE)
6499 expr = mark_lvalue_use (expr);
6500 else
6501 expr = mark_rvalue_use (expr);
6503 /* HACK: Due to double coercion, we can get a
6504 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6505 which is the tree that we built on the first call (see
6506 below when coercing to reference to object or to reference to
6507 function). We just strip everything and get to the arg.
6508 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6509 for examples. */
6510 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6512 tree probe_type, probe = expr;
6513 if (REFERENCE_REF_P (probe))
6514 probe = TREE_OPERAND (probe, 0);
6515 probe_type = TREE_TYPE (probe);
6516 if (TREE_CODE (probe) == NOP_EXPR)
6518 /* ??? Maybe we could use convert_from_reference here, but we
6519 would need to relax its constraints because the NOP_EXPR
6520 could actually change the type to something more cv-qualified,
6521 and this is not folded by convert_from_reference. */
6522 tree addr = TREE_OPERAND (probe, 0);
6523 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6524 && TREE_CODE (addr) == ADDR_EXPR
6525 && TYPE_PTR_P (TREE_TYPE (addr))
6526 && (same_type_ignoring_top_level_qualifiers_p
6527 (TREE_TYPE (probe_type),
6528 TREE_TYPE (TREE_TYPE (addr)))))
6530 expr = TREE_OPERAND (addr, 0);
6531 expr_type = TREE_TYPE (probe_type);
6536 /* [temp.arg.nontype]/5, bullet 1
6538 For a non-type template-parameter of integral or enumeration type,
6539 integral promotions (_conv.prom_) and integral conversions
6540 (_conv.integral_) are applied. */
6541 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6543 if (cxx_dialect < cxx11)
6545 tree t = build_converted_constant_expr (type, expr, complain);
6546 t = maybe_constant_value (t);
6547 if (t != error_mark_node)
6548 expr = t;
6551 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6552 return error_mark_node;
6554 /* Notice that there are constant expressions like '4 % 0' which
6555 do not fold into integer constants. */
6556 if (TREE_CODE (expr) != INTEGER_CST
6557 && !value_dependent_expression_p (expr))
6559 if (complain & tf_error)
6561 int errs = errorcount, warns = warningcount + werrorcount;
6562 if (!require_potential_constant_expression (expr))
6563 expr = error_mark_node;
6564 else
6565 expr = cxx_constant_value (expr);
6566 if (errorcount > errs || warningcount + werrorcount > warns)
6567 inform (loc, "in template argument for type %qT ", type);
6568 if (expr == error_mark_node)
6569 return NULL_TREE;
6570 /* else cxx_constant_value complained but gave us
6571 a real constant, so go ahead. */
6572 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6574 else
6575 return NULL_TREE;
6578 /* Avoid typedef problems. */
6579 if (TREE_TYPE (expr) != type)
6580 expr = fold_convert (type, expr);
6582 /* [temp.arg.nontype]/5, bullet 2
6584 For a non-type template-parameter of type pointer to object,
6585 qualification conversions (_conv.qual_) and the array-to-pointer
6586 conversion (_conv.array_) are applied. */
6587 else if (TYPE_PTROBV_P (type))
6589 tree decayed = expr;
6591 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6592 decay_conversion or an explicit cast. If it's a problematic cast,
6593 we'll complain about it below. */
6594 if (TREE_CODE (expr) == NOP_EXPR)
6596 tree probe = expr;
6597 STRIP_NOPS (probe);
6598 if (TREE_CODE (probe) == ADDR_EXPR
6599 && TYPE_PTR_P (TREE_TYPE (probe)))
6601 expr = probe;
6602 expr_type = TREE_TYPE (expr);
6606 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6608 A template-argument for a non-type, non-template template-parameter
6609 shall be one of: [...]
6611 -- the name of a non-type template-parameter;
6612 -- the address of an object or function with external linkage, [...]
6613 expressed as "& id-expression" where the & is optional if the name
6614 refers to a function or array, or if the corresponding
6615 template-parameter is a reference.
6617 Here, we do not care about functions, as they are invalid anyway
6618 for a parameter of type pointer-to-object. */
6620 if (value_dependent_expression_p (expr))
6621 /* Non-type template parameters are OK. */
6623 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6624 /* Null pointer values are OK in C++11. */;
6625 else if (TREE_CODE (expr) != ADDR_EXPR)
6627 if (VAR_P (expr))
6629 if (complain & tf_error)
6630 error ("%qD is not a valid template argument "
6631 "because %qD is a variable, not the address of "
6632 "a variable", orig_expr, expr);
6633 return NULL_TREE;
6635 if (POINTER_TYPE_P (expr_type))
6637 if (complain & tf_error)
6638 error ("%qE is not a valid template argument for %qT "
6639 "because it is not the address of a variable",
6640 orig_expr, type);
6641 return NULL_TREE;
6643 /* Other values, like integer constants, might be valid
6644 non-type arguments of some other type. */
6645 return error_mark_node;
6647 else
6649 tree decl = TREE_OPERAND (expr, 0);
6651 if (!VAR_P (decl))
6653 if (complain & tf_error)
6654 error ("%qE is not a valid template argument of type %qT "
6655 "because %qE is not a variable", orig_expr, type, decl);
6656 return NULL_TREE;
6658 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6660 if (complain & tf_error)
6661 error ("%qE is not a valid template argument of type %qT "
6662 "because %qD does not have external linkage",
6663 orig_expr, type, decl);
6664 return NULL_TREE;
6666 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6667 && decl_linkage (decl) == lk_none)
6669 if (complain & tf_error)
6670 error ("%qE is not a valid template argument of type %qT "
6671 "because %qD has no linkage", orig_expr, type, decl);
6672 return NULL_TREE;
6674 /* C++17: For a non-type template-parameter of reference or pointer
6675 type, the value of the constant expression shall not refer to (or
6676 for a pointer type, shall not be the address of):
6677 * a subobject (4.5),
6678 * a temporary object (15.2),
6679 * a string literal (5.13.5),
6680 * the result of a typeid expression (8.2.8), or
6681 * a predefined __func__ variable (11.4.1). */
6682 else if (DECL_ARTIFICIAL (decl))
6684 if (complain & tf_error)
6685 error ("the address of %qD is not a valid template argument",
6686 decl);
6687 return NULL_TREE;
6689 else if (!same_type_ignoring_top_level_qualifiers_p
6690 (strip_array_types (TREE_TYPE (type)),
6691 strip_array_types (TREE_TYPE (decl))))
6693 if (complain & tf_error)
6694 error ("the address of the %qT subobject of %qD is not a "
6695 "valid template argument", TREE_TYPE (type), decl);
6696 return NULL_TREE;
6698 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6700 if (complain & tf_error)
6701 error ("the address of %qD is not a valid template argument "
6702 "because it does not have static storage duration",
6703 decl);
6704 return NULL_TREE;
6708 expr = decayed;
6710 expr = perform_qualification_conversions (type, expr);
6711 if (expr == error_mark_node)
6712 return error_mark_node;
6714 /* [temp.arg.nontype]/5, bullet 3
6716 For a non-type template-parameter of type reference to object, no
6717 conversions apply. The type referred to by the reference may be more
6718 cv-qualified than the (otherwise identical) type of the
6719 template-argument. The template-parameter is bound directly to the
6720 template-argument, which must be an lvalue. */
6721 else if (TYPE_REF_OBJ_P (type))
6723 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6724 expr_type))
6725 return error_mark_node;
6727 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6729 if (complain & tf_error)
6730 error ("%qE is not a valid template argument for type %qT "
6731 "because of conflicts in cv-qualification", expr, type);
6732 return NULL_TREE;
6735 if (!lvalue_p (expr))
6737 if (complain & tf_error)
6738 error ("%qE is not a valid template argument for type %qT "
6739 "because it is not an lvalue", expr, type);
6740 return NULL_TREE;
6743 /* [temp.arg.nontype]/1
6745 A template-argument for a non-type, non-template template-parameter
6746 shall be one of: [...]
6748 -- the address of an object or function with external linkage. */
6749 if (INDIRECT_REF_P (expr)
6750 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6752 expr = TREE_OPERAND (expr, 0);
6753 if (DECL_P (expr))
6755 if (complain & tf_error)
6756 error ("%q#D is not a valid template argument for type %qT "
6757 "because a reference variable does not have a constant "
6758 "address", expr, type);
6759 return NULL_TREE;
6763 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6764 && value_dependent_expression_p (expr))
6765 /* OK, dependent reference. We don't want to ask whether a DECL is
6766 itself value-dependent, since what we want here is its address. */;
6767 else
6769 if (!DECL_P (expr))
6771 if (complain & tf_error)
6772 error ("%qE is not a valid template argument for type %qT "
6773 "because it is not an object with linkage",
6774 expr, type);
6775 return NULL_TREE;
6778 /* DR 1155 allows internal linkage in C++11 and up. */
6779 linkage_kind linkage = decl_linkage (expr);
6780 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6782 if (complain & tf_error)
6783 error ("%qE is not a valid template argument for type %qT "
6784 "because object %qD does not have linkage",
6785 expr, type, expr);
6786 return NULL_TREE;
6789 expr = build_address (expr);
6792 if (!same_type_p (type, TREE_TYPE (expr)))
6793 expr = build_nop (type, expr);
6795 /* [temp.arg.nontype]/5, bullet 4
6797 For a non-type template-parameter of type pointer to function, only
6798 the function-to-pointer conversion (_conv.func_) is applied. If the
6799 template-argument represents a set of overloaded functions (or a
6800 pointer to such), the matching function is selected from the set
6801 (_over.over_). */
6802 else if (TYPE_PTRFN_P (type))
6804 /* If the argument is a template-id, we might not have enough
6805 context information to decay the pointer. */
6806 if (!type_unknown_p (expr_type))
6808 expr = decay_conversion (expr, complain);
6809 if (expr == error_mark_node)
6810 return error_mark_node;
6813 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6814 /* Null pointer values are OK in C++11. */
6815 return perform_qualification_conversions (type, expr);
6817 expr = convert_nontype_argument_function (type, expr, complain);
6818 if (!expr || expr == error_mark_node)
6819 return expr;
6821 /* [temp.arg.nontype]/5, bullet 5
6823 For a non-type template-parameter of type reference to function, no
6824 conversions apply. If the template-argument represents a set of
6825 overloaded functions, the matching function is selected from the set
6826 (_over.over_). */
6827 else if (TYPE_REFFN_P (type))
6829 if (TREE_CODE (expr) == ADDR_EXPR)
6831 if (complain & tf_error)
6833 error ("%qE is not a valid template argument for type %qT "
6834 "because it is a pointer", expr, type);
6835 inform (input_location, "try using %qE instead",
6836 TREE_OPERAND (expr, 0));
6838 return NULL_TREE;
6841 expr = convert_nontype_argument_function (type, expr, complain);
6842 if (!expr || expr == error_mark_node)
6843 return expr;
6845 /* [temp.arg.nontype]/5, bullet 6
6847 For a non-type template-parameter of type pointer to member function,
6848 no conversions apply. If the template-argument represents a set of
6849 overloaded member functions, the matching member function is selected
6850 from the set (_over.over_). */
6851 else if (TYPE_PTRMEMFUNC_P (type))
6853 expr = instantiate_type (type, expr, tf_none);
6854 if (expr == error_mark_node)
6855 return error_mark_node;
6857 /* [temp.arg.nontype] bullet 1 says the pointer to member
6858 expression must be a pointer-to-member constant. */
6859 if (!value_dependent_expression_p (expr)
6860 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6861 return NULL_TREE;
6863 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6864 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6865 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6866 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6868 /* [temp.arg.nontype]/5, bullet 7
6870 For a non-type template-parameter of type pointer to data member,
6871 qualification conversions (_conv.qual_) are applied. */
6872 else if (TYPE_PTRDATAMEM_P (type))
6874 /* [temp.arg.nontype] bullet 1 says the pointer to member
6875 expression must be a pointer-to-member constant. */
6876 if (!value_dependent_expression_p (expr)
6877 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6878 return NULL_TREE;
6880 expr = perform_qualification_conversions (type, expr);
6881 if (expr == error_mark_node)
6882 return expr;
6884 else if (NULLPTR_TYPE_P (type))
6886 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6888 if (complain & tf_error)
6889 error ("%qE is not a valid template argument for type %qT "
6890 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6891 return NULL_TREE;
6893 return expr;
6895 /* A template non-type parameter must be one of the above. */
6896 else
6897 gcc_unreachable ();
6899 /* Sanity check: did we actually convert the argument to the
6900 right type? */
6901 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6902 (type, TREE_TYPE (expr)));
6903 return convert_from_reference (expr);
6906 /* Subroutine of coerce_template_template_parms, which returns 1 if
6907 PARM_PARM and ARG_PARM match using the rule for the template
6908 parameters of template template parameters. Both PARM and ARG are
6909 template parameters; the rest of the arguments are the same as for
6910 coerce_template_template_parms.
6912 static int
6913 coerce_template_template_parm (tree parm,
6914 tree arg,
6915 tsubst_flags_t complain,
6916 tree in_decl,
6917 tree outer_args)
6919 if (arg == NULL_TREE || error_operand_p (arg)
6920 || parm == NULL_TREE || error_operand_p (parm))
6921 return 0;
6923 if (TREE_CODE (arg) != TREE_CODE (parm))
6924 return 0;
6926 switch (TREE_CODE (parm))
6928 case TEMPLATE_DECL:
6929 /* We encounter instantiations of templates like
6930 template <template <template <class> class> class TT>
6931 class C; */
6933 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6934 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6936 if (!coerce_template_template_parms
6937 (parmparm, argparm, complain, in_decl, outer_args))
6938 return 0;
6940 /* Fall through. */
6942 case TYPE_DECL:
6943 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6944 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6945 /* Argument is a parameter pack but parameter is not. */
6946 return 0;
6947 break;
6949 case PARM_DECL:
6950 /* The tsubst call is used to handle cases such as
6952 template <int> class C {};
6953 template <class T, template <T> class TT> class D {};
6954 D<int, C> d;
6956 i.e. the parameter list of TT depends on earlier parameters. */
6957 if (!uses_template_parms (TREE_TYPE (arg)))
6959 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6960 if (!uses_template_parms (t)
6961 && !same_type_p (t, TREE_TYPE (arg)))
6962 return 0;
6965 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6966 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6967 /* Argument is a parameter pack but parameter is not. */
6968 return 0;
6970 break;
6972 default:
6973 gcc_unreachable ();
6976 return 1;
6979 /* Coerce template argument list ARGLIST for use with template
6980 template-parameter TEMPL. */
6982 static tree
6983 coerce_template_args_for_ttp (tree templ, tree arglist,
6984 tsubst_flags_t complain)
6986 /* Consider an example where a template template parameter declared as
6988 template <class T, class U = std::allocator<T> > class TT
6990 The template parameter level of T and U are one level larger than
6991 of TT. To proper process the default argument of U, say when an
6992 instantiation `TT<int>' is seen, we need to build the full
6993 arguments containing {int} as the innermost level. Outer levels,
6994 available when not appearing as default template argument, can be
6995 obtained from the arguments of the enclosing template.
6997 Suppose that TT is later substituted with std::vector. The above
6998 instantiation is `TT<int, std::allocator<T> >' with TT at
6999 level 1, and T at level 2, while the template arguments at level 1
7000 becomes {std::vector} and the inner level 2 is {int}. */
7002 tree outer = DECL_CONTEXT (templ);
7003 if (outer)
7005 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7006 /* We want arguments for the partial specialization, not arguments for
7007 the primary template. */
7008 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7009 else
7010 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7012 else if (current_template_parms)
7014 /* This is an argument of the current template, so we haven't set
7015 DECL_CONTEXT yet. */
7016 tree relevant_template_parms;
7018 /* Parameter levels that are greater than the level of the given
7019 template template parm are irrelevant. */
7020 relevant_template_parms = current_template_parms;
7021 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7022 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7023 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7025 outer = template_parms_to_args (relevant_template_parms);
7028 if (outer)
7029 arglist = add_to_template_args (outer, arglist);
7031 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7032 return coerce_template_parms (parmlist, arglist, templ,
7033 complain,
7034 /*require_all_args=*/true,
7035 /*use_default_args=*/true);
7038 /* A cache of template template parameters with match-all default
7039 arguments. */
7040 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7041 static void
7042 store_defaulted_ttp (tree v, tree t)
7044 if (!defaulted_ttp_cache)
7045 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7046 defaulted_ttp_cache->put (v, t);
7048 static tree
7049 lookup_defaulted_ttp (tree v)
7051 if (defaulted_ttp_cache)
7052 if (tree *p = defaulted_ttp_cache->get (v))
7053 return *p;
7054 return NULL_TREE;
7057 /* T is a bound template template-parameter. Copy its arguments into default
7058 arguments of the template template-parameter's template parameters. */
7060 static tree
7061 add_defaults_to_ttp (tree otmpl)
7063 if (tree c = lookup_defaulted_ttp (otmpl))
7064 return c;
7066 tree ntmpl = copy_node (otmpl);
7068 tree ntype = copy_node (TREE_TYPE (otmpl));
7069 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7070 TYPE_MAIN_VARIANT (ntype) = ntype;
7071 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7072 TYPE_NAME (ntype) = ntmpl;
7073 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7075 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7076 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7077 TEMPLATE_PARM_DECL (idx) = ntmpl;
7078 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7080 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7081 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7082 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7083 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7084 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7086 tree o = TREE_VEC_ELT (vec, i);
7087 if (!template_parameter_pack_p (TREE_VALUE (o)))
7089 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7090 TREE_PURPOSE (n) = any_targ_node;
7094 store_defaulted_ttp (otmpl, ntmpl);
7095 return ntmpl;
7098 /* ARG is a bound potential template template-argument, and PARGS is a list
7099 of arguments for the corresponding template template-parameter. Adjust
7100 PARGS as appropriate for application to ARG's template, and if ARG is a
7101 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7102 arguments to the template template parameter. */
7104 static tree
7105 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7107 ++processing_template_decl;
7108 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7109 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7111 /* When comparing two template template-parameters in partial ordering,
7112 rewrite the one currently being used as an argument to have default
7113 arguments for all parameters. */
7114 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7115 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7116 if (pargs != error_mark_node)
7117 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7118 TYPE_TI_ARGS (arg));
7120 else
7122 tree aparms
7123 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7124 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7125 /*require_all*/true,
7126 /*use_default*/true);
7128 --processing_template_decl;
7129 return pargs;
7132 /* Subroutine of unify for the case when PARM is a
7133 BOUND_TEMPLATE_TEMPLATE_PARM. */
7135 static int
7136 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7137 bool explain_p)
7139 tree parmvec = TYPE_TI_ARGS (parm);
7140 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7142 /* The template template parm might be variadic and the argument
7143 not, so flatten both argument lists. */
7144 parmvec = expand_template_argument_pack (parmvec);
7145 argvec = expand_template_argument_pack (argvec);
7147 if (flag_new_ttp)
7149 /* In keeping with P0522R0, adjust P's template arguments
7150 to apply to A's template; then flatten it again. */
7151 tree nparmvec = parmvec;
7152 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7153 nparmvec = expand_template_argument_pack (nparmvec);
7155 if (unify (tparms, targs, nparmvec, argvec,
7156 UNIFY_ALLOW_NONE, explain_p))
7157 return 1;
7159 /* If the P0522 adjustment eliminated a pack expansion, deduce
7160 empty packs. */
7161 if (flag_new_ttp
7162 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7163 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7164 DEDUCE_EXACT, /*sub*/true, explain_p))
7165 return 1;
7167 else
7169 /* Deduce arguments T, i from TT<T> or TT<i>.
7170 We check each element of PARMVEC and ARGVEC individually
7171 rather than the whole TREE_VEC since they can have
7172 different number of elements, which is allowed under N2555. */
7174 int len = TREE_VEC_LENGTH (parmvec);
7176 /* Check if the parameters end in a pack, making them
7177 variadic. */
7178 int parm_variadic_p = 0;
7179 if (len > 0
7180 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7181 parm_variadic_p = 1;
7183 for (int i = 0; i < len - parm_variadic_p; ++i)
7184 /* If the template argument list of P contains a pack
7185 expansion that is not the last template argument, the
7186 entire template argument list is a non-deduced
7187 context. */
7188 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7189 return unify_success (explain_p);
7191 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7192 return unify_too_few_arguments (explain_p,
7193 TREE_VEC_LENGTH (argvec), len);
7195 for (int i = 0; i < len - parm_variadic_p; ++i)
7196 if (unify (tparms, targs,
7197 TREE_VEC_ELT (parmvec, i),
7198 TREE_VEC_ELT (argvec, i),
7199 UNIFY_ALLOW_NONE, explain_p))
7200 return 1;
7202 if (parm_variadic_p
7203 && unify_pack_expansion (tparms, targs,
7204 parmvec, argvec,
7205 DEDUCE_EXACT,
7206 /*subr=*/true, explain_p))
7207 return 1;
7210 return 0;
7213 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7214 template template parameters. Both PARM_PARMS and ARG_PARMS are
7215 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7216 or PARM_DECL.
7218 Consider the example:
7219 template <class T> class A;
7220 template<template <class U> class TT> class B;
7222 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7223 the parameters to A, and OUTER_ARGS contains A. */
7225 static int
7226 coerce_template_template_parms (tree parm_parms,
7227 tree arg_parms,
7228 tsubst_flags_t complain,
7229 tree in_decl,
7230 tree outer_args)
7232 int nparms, nargs, i;
7233 tree parm, arg;
7234 int variadic_p = 0;
7236 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7237 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7239 nparms = TREE_VEC_LENGTH (parm_parms);
7240 nargs = TREE_VEC_LENGTH (arg_parms);
7242 if (flag_new_ttp)
7244 /* P0522R0: A template template-parameter P is at least as specialized as
7245 a template template-argument A if, given the following rewrite to two
7246 function templates, the function template corresponding to P is at
7247 least as specialized as the function template corresponding to A
7248 according to the partial ordering rules for function templates
7249 ([temp.func.order]). Given an invented class template X with the
7250 template parameter list of A (including default arguments):
7252 * Each of the two function templates has the same template parameters,
7253 respectively, as P or A.
7255 * Each function template has a single function parameter whose type is
7256 a specialization of X with template arguments corresponding to the
7257 template parameters from the respective function template where, for
7258 each template parameter PP in the template parameter list of the
7259 function template, a corresponding template argument AA is formed. If
7260 PP declares a parameter pack, then AA is the pack expansion
7261 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7263 If the rewrite produces an invalid type, then P is not at least as
7264 specialized as A. */
7266 /* So coerce P's args to apply to A's parms, and then deduce between A's
7267 args and the converted args. If that succeeds, A is at least as
7268 specialized as P, so they match.*/
7269 tree pargs = template_parms_level_to_args (parm_parms);
7270 ++processing_template_decl;
7271 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7272 /*require_all*/true, /*use_default*/true);
7273 --processing_template_decl;
7274 if (pargs != error_mark_node)
7276 tree targs = make_tree_vec (nargs);
7277 tree aargs = template_parms_level_to_args (arg_parms);
7278 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7279 /*explain*/false))
7280 return 1;
7284 /* Determine whether we have a parameter pack at the end of the
7285 template template parameter's template parameter list. */
7286 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7288 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7290 if (error_operand_p (parm))
7291 return 0;
7293 switch (TREE_CODE (parm))
7295 case TEMPLATE_DECL:
7296 case TYPE_DECL:
7297 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7298 variadic_p = 1;
7299 break;
7301 case PARM_DECL:
7302 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7303 variadic_p = 1;
7304 break;
7306 default:
7307 gcc_unreachable ();
7311 if (nargs != nparms
7312 && !(variadic_p && nargs >= nparms - 1))
7313 return 0;
7315 /* Check all of the template parameters except the parameter pack at
7316 the end (if any). */
7317 for (i = 0; i < nparms - variadic_p; ++i)
7319 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7320 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7321 continue;
7323 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7324 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7326 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7327 outer_args))
7328 return 0;
7332 if (variadic_p)
7334 /* Check each of the template parameters in the template
7335 argument against the template parameter pack at the end of
7336 the template template parameter. */
7337 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7338 return 0;
7340 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7342 for (; i < nargs; ++i)
7344 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7345 continue;
7347 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7349 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7350 outer_args))
7351 return 0;
7355 return 1;
7358 /* Verifies that the deduced template arguments (in TARGS) for the
7359 template template parameters (in TPARMS) represent valid bindings,
7360 by comparing the template parameter list of each template argument
7361 to the template parameter list of its corresponding template
7362 template parameter, in accordance with DR150. This
7363 routine can only be called after all template arguments have been
7364 deduced. It will return TRUE if all of the template template
7365 parameter bindings are okay, FALSE otherwise. */
7366 bool
7367 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7369 int i, ntparms = TREE_VEC_LENGTH (tparms);
7370 bool ret = true;
7372 /* We're dealing with template parms in this process. */
7373 ++processing_template_decl;
7375 targs = INNERMOST_TEMPLATE_ARGS (targs);
7377 for (i = 0; i < ntparms; ++i)
7379 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7380 tree targ = TREE_VEC_ELT (targs, i);
7382 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7384 tree packed_args = NULL_TREE;
7385 int idx, len = 1;
7387 if (ARGUMENT_PACK_P (targ))
7389 /* Look inside the argument pack. */
7390 packed_args = ARGUMENT_PACK_ARGS (targ);
7391 len = TREE_VEC_LENGTH (packed_args);
7394 for (idx = 0; idx < len; ++idx)
7396 tree targ_parms = NULL_TREE;
7398 if (packed_args)
7399 /* Extract the next argument from the argument
7400 pack. */
7401 targ = TREE_VEC_ELT (packed_args, idx);
7403 if (PACK_EXPANSION_P (targ))
7404 /* Look at the pattern of the pack expansion. */
7405 targ = PACK_EXPANSION_PATTERN (targ);
7407 /* Extract the template parameters from the template
7408 argument. */
7409 if (TREE_CODE (targ) == TEMPLATE_DECL)
7410 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7411 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7412 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7414 /* Verify that we can coerce the template template
7415 parameters from the template argument to the template
7416 parameter. This requires an exact match. */
7417 if (targ_parms
7418 && !coerce_template_template_parms
7419 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7420 targ_parms,
7421 tf_none,
7422 tparm,
7423 targs))
7425 ret = false;
7426 goto out;
7432 out:
7434 --processing_template_decl;
7435 return ret;
7438 /* Since type attributes aren't mangled, we need to strip them from
7439 template type arguments. */
7441 static tree
7442 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7444 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7445 return arg;
7446 bool removed_attributes = false;
7447 tree canon = strip_typedefs (arg, &removed_attributes);
7448 if (removed_attributes
7449 && (complain & tf_warning))
7450 warning (OPT_Wignored_attributes,
7451 "ignoring attributes on template argument %qT", arg);
7452 return canon;
7455 /* And from inside dependent non-type arguments like sizeof(Type). */
7457 static tree
7458 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7460 if (!arg || arg == error_mark_node)
7461 return arg;
7462 bool removed_attributes = false;
7463 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7464 if (removed_attributes
7465 && (complain & tf_warning))
7466 warning (OPT_Wignored_attributes,
7467 "ignoring attributes in template argument %qE", arg);
7468 return canon;
7471 // A template declaration can be substituted for a constrained
7472 // template template parameter only when the argument is more
7473 // constrained than the parameter.
7474 static bool
7475 is_compatible_template_arg (tree parm, tree arg)
7477 tree parm_cons = get_constraints (parm);
7479 /* For now, allow constrained template template arguments
7480 and unconstrained template template parameters. */
7481 if (parm_cons == NULL_TREE)
7482 return true;
7484 tree arg_cons = get_constraints (arg);
7486 // If the template parameter is constrained, we need to rewrite its
7487 // constraints in terms of the ARG's template parameters. This ensures
7488 // that all of the template parameter types will have the same depth.
7490 // Note that this is only valid when coerce_template_template_parm is
7491 // true for the innermost template parameters of PARM and ARG. In other
7492 // words, because coercion is successful, this conversion will be valid.
7493 if (parm_cons)
7495 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7496 parm_cons = tsubst_constraint_info (parm_cons,
7497 INNERMOST_TEMPLATE_ARGS (args),
7498 tf_none, NULL_TREE);
7499 if (parm_cons == error_mark_node)
7500 return false;
7503 return subsumes (parm_cons, arg_cons);
7506 // Convert a placeholder argument into a binding to the original
7507 // parameter. The original parameter is saved as the TREE_TYPE of
7508 // ARG.
7509 static inline tree
7510 convert_wildcard_argument (tree parm, tree arg)
7512 TREE_TYPE (arg) = parm;
7513 return arg;
7516 /* Convert the indicated template ARG as necessary to match the
7517 indicated template PARM. Returns the converted ARG, or
7518 error_mark_node if the conversion was unsuccessful. Error and
7519 warning messages are issued under control of COMPLAIN. This
7520 conversion is for the Ith parameter in the parameter list. ARGS is
7521 the full set of template arguments deduced so far. */
7523 static tree
7524 convert_template_argument (tree parm,
7525 tree arg,
7526 tree args,
7527 tsubst_flags_t complain,
7528 int i,
7529 tree in_decl)
7531 tree orig_arg;
7532 tree val;
7533 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7535 if (parm == error_mark_node)
7536 return error_mark_node;
7538 /* Trivially convert placeholders. */
7539 if (TREE_CODE (arg) == WILDCARD_DECL)
7540 return convert_wildcard_argument (parm, arg);
7542 if (arg == any_targ_node)
7543 return arg;
7545 if (TREE_CODE (arg) == TREE_LIST
7546 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7548 /* The template argument was the name of some
7549 member function. That's usually
7550 invalid, but static members are OK. In any
7551 case, grab the underlying fields/functions
7552 and issue an error later if required. */
7553 orig_arg = TREE_VALUE (arg);
7554 TREE_TYPE (arg) = unknown_type_node;
7557 orig_arg = arg;
7559 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7560 requires_type = (TREE_CODE (parm) == TYPE_DECL
7561 || requires_tmpl_type);
7563 /* When determining whether an argument pack expansion is a template,
7564 look at the pattern. */
7565 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7566 arg = PACK_EXPANSION_PATTERN (arg);
7568 /* Deal with an injected-class-name used as a template template arg. */
7569 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7571 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7572 if (TREE_CODE (t) == TEMPLATE_DECL)
7574 if (cxx_dialect >= cxx11)
7575 /* OK under DR 1004. */;
7576 else if (complain & tf_warning_or_error)
7577 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7578 " used as template template argument", TYPE_NAME (arg));
7579 else if (flag_pedantic_errors)
7580 t = arg;
7582 arg = t;
7586 is_tmpl_type =
7587 ((TREE_CODE (arg) == TEMPLATE_DECL
7588 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7589 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7590 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7591 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7593 if (is_tmpl_type
7594 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7595 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7596 arg = TYPE_STUB_DECL (arg);
7598 is_type = TYPE_P (arg) || is_tmpl_type;
7600 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7601 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7603 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7605 if (complain & tf_error)
7606 error ("invalid use of destructor %qE as a type", orig_arg);
7607 return error_mark_node;
7610 permerror (input_location,
7611 "to refer to a type member of a template parameter, "
7612 "use %<typename %E%>", orig_arg);
7614 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7615 TREE_OPERAND (arg, 1),
7616 typename_type,
7617 complain);
7618 arg = orig_arg;
7619 is_type = 1;
7621 if (is_type != requires_type)
7623 if (in_decl)
7625 if (complain & tf_error)
7627 error ("type/value mismatch at argument %d in template "
7628 "parameter list for %qD",
7629 i + 1, in_decl);
7630 if (is_type)
7631 inform (input_location,
7632 " expected a constant of type %qT, got %qT",
7633 TREE_TYPE (parm),
7634 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7635 else if (requires_tmpl_type)
7636 inform (input_location,
7637 " expected a class template, got %qE", orig_arg);
7638 else
7639 inform (input_location,
7640 " expected a type, got %qE", orig_arg);
7643 return error_mark_node;
7645 if (is_tmpl_type ^ requires_tmpl_type)
7647 if (in_decl && (complain & tf_error))
7649 error ("type/value mismatch at argument %d in template "
7650 "parameter list for %qD",
7651 i + 1, in_decl);
7652 if (is_tmpl_type)
7653 inform (input_location,
7654 " expected a type, got %qT", DECL_NAME (arg));
7655 else
7656 inform (input_location,
7657 " expected a class template, got %qT", orig_arg);
7659 return error_mark_node;
7662 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7663 /* We already did the appropriate conversion when packing args. */
7664 val = orig_arg;
7665 else if (is_type)
7667 if (requires_tmpl_type)
7669 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7670 /* The number of argument required is not known yet.
7671 Just accept it for now. */
7672 val = orig_arg;
7673 else
7675 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7676 tree argparm;
7678 /* Strip alias templates that are equivalent to another
7679 template. */
7680 arg = get_underlying_template (arg);
7681 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7683 if (coerce_template_template_parms (parmparm, argparm,
7684 complain, in_decl,
7685 args))
7687 val = arg;
7689 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7690 TEMPLATE_DECL. */
7691 if (val != error_mark_node)
7693 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7694 val = TREE_TYPE (val);
7695 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7696 val = make_pack_expansion (val);
7699 else
7701 if (in_decl && (complain & tf_error))
7703 error ("type/value mismatch at argument %d in "
7704 "template parameter list for %qD",
7705 i + 1, in_decl);
7706 inform (input_location,
7707 " expected a template of type %qD, got %qT",
7708 parm, orig_arg);
7711 val = error_mark_node;
7714 // Check that the constraints are compatible before allowing the
7715 // substitution.
7716 if (val != error_mark_node)
7717 if (!is_compatible_template_arg (parm, arg))
7719 if (in_decl && (complain & tf_error))
7721 error ("constraint mismatch at argument %d in "
7722 "template parameter list for %qD",
7723 i + 1, in_decl);
7724 inform (input_location, " expected %qD but got %qD",
7725 parm, arg);
7727 val = error_mark_node;
7731 else
7732 val = orig_arg;
7733 /* We only form one instance of each template specialization.
7734 Therefore, if we use a non-canonical variant (i.e., a
7735 typedef), any future messages referring to the type will use
7736 the typedef, which is confusing if those future uses do not
7737 themselves also use the typedef. */
7738 if (TYPE_P (val))
7739 val = canonicalize_type_argument (val, complain);
7741 else
7743 tree t = TREE_TYPE (parm);
7745 if (tree a = type_uses_auto (t))
7747 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7748 if (t == error_mark_node)
7749 return error_mark_node;
7751 else
7752 t = tsubst (t, args, complain, in_decl);
7754 if (invalid_nontype_parm_type_p (t, complain))
7755 return error_mark_node;
7757 if (!type_dependent_expression_p (orig_arg)
7758 && !uses_template_parms (t))
7759 /* We used to call digest_init here. However, digest_init
7760 will report errors, which we don't want when complain
7761 is zero. More importantly, digest_init will try too
7762 hard to convert things: for example, `0' should not be
7763 converted to pointer type at this point according to
7764 the standard. Accepting this is not merely an
7765 extension, since deciding whether or not these
7766 conversions can occur is part of determining which
7767 function template to call, or whether a given explicit
7768 argument specification is valid. */
7769 val = convert_nontype_argument (t, orig_arg, complain);
7770 else
7771 val = canonicalize_expr_argument (orig_arg, complain);
7773 if (val == NULL_TREE)
7774 val = error_mark_node;
7775 else if (val == error_mark_node && (complain & tf_error))
7776 error ("could not convert template argument %qE from %qT to %qT",
7777 orig_arg, TREE_TYPE (orig_arg), t);
7779 if (INDIRECT_REF_P (val))
7781 /* Reject template arguments that are references to built-in
7782 functions with no library fallbacks. */
7783 const_tree inner = TREE_OPERAND (val, 0);
7784 const_tree innertype = TREE_TYPE (inner);
7785 if (innertype
7786 && TREE_CODE (innertype) == REFERENCE_TYPE
7787 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7788 && 0 < TREE_OPERAND_LENGTH (inner)
7789 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7790 return error_mark_node;
7793 if (TREE_CODE (val) == SCOPE_REF)
7795 /* Strip typedefs from the SCOPE_REF. */
7796 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7797 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7798 complain);
7799 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7800 QUALIFIED_NAME_IS_TEMPLATE (val));
7804 return val;
7807 /* Coerces the remaining template arguments in INNER_ARGS (from
7808 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7809 Returns the coerced argument pack. PARM_IDX is the position of this
7810 parameter in the template parameter list. ARGS is the original
7811 template argument list. */
7812 static tree
7813 coerce_template_parameter_pack (tree parms,
7814 int parm_idx,
7815 tree args,
7816 tree inner_args,
7817 int arg_idx,
7818 tree new_args,
7819 int* lost,
7820 tree in_decl,
7821 tsubst_flags_t complain)
7823 tree parm = TREE_VEC_ELT (parms, parm_idx);
7824 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7825 tree packed_args;
7826 tree argument_pack;
7827 tree packed_parms = NULL_TREE;
7829 if (arg_idx > nargs)
7830 arg_idx = nargs;
7832 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7834 /* When the template parameter is a non-type template parameter pack
7835 or template template parameter pack whose type or template
7836 parameters use parameter packs, we know exactly how many arguments
7837 we are looking for. Build a vector of the instantiated decls for
7838 these template parameters in PACKED_PARMS. */
7839 /* We can't use make_pack_expansion here because it would interpret a
7840 _DECL as a use rather than a declaration. */
7841 tree decl = TREE_VALUE (parm);
7842 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7843 SET_PACK_EXPANSION_PATTERN (exp, decl);
7844 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7845 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7847 TREE_VEC_LENGTH (args)--;
7848 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7849 TREE_VEC_LENGTH (args)++;
7851 if (packed_parms == error_mark_node)
7852 return error_mark_node;
7854 /* If we're doing a partial instantiation of a member template,
7855 verify that all of the types used for the non-type
7856 template parameter pack are, in fact, valid for non-type
7857 template parameters. */
7858 if (arg_idx < nargs
7859 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7861 int j, len = TREE_VEC_LENGTH (packed_parms);
7862 for (j = 0; j < len; ++j)
7864 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7865 if (invalid_nontype_parm_type_p (t, complain))
7866 return error_mark_node;
7868 /* We don't know how many args we have yet, just
7869 use the unconverted ones for now. */
7870 return NULL_TREE;
7873 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7875 /* Check if we have a placeholder pack, which indicates we're
7876 in the context of a introduction list. In that case we want
7877 to match this pack to the single placeholder. */
7878 else if (arg_idx < nargs
7879 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7880 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7882 nargs = arg_idx + 1;
7883 packed_args = make_tree_vec (1);
7885 else
7886 packed_args = make_tree_vec (nargs - arg_idx);
7888 /* Convert the remaining arguments, which will be a part of the
7889 parameter pack "parm". */
7890 int first_pack_arg = arg_idx;
7891 for (; arg_idx < nargs; ++arg_idx)
7893 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7894 tree actual_parm = TREE_VALUE (parm);
7895 int pack_idx = arg_idx - first_pack_arg;
7897 if (packed_parms)
7899 /* Once we've packed as many args as we have types, stop. */
7900 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7901 break;
7902 else if (PACK_EXPANSION_P (arg))
7903 /* We don't know how many args we have yet, just
7904 use the unconverted ones for now. */
7905 return NULL_TREE;
7906 else
7907 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7910 if (arg == error_mark_node)
7912 if (complain & tf_error)
7913 error ("template argument %d is invalid", arg_idx + 1);
7915 else
7916 arg = convert_template_argument (actual_parm,
7917 arg, new_args, complain, parm_idx,
7918 in_decl);
7919 if (arg == error_mark_node)
7920 (*lost)++;
7921 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7924 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7925 && TREE_VEC_LENGTH (packed_args) > 0)
7927 if (complain & tf_error)
7928 error ("wrong number of template arguments (%d, should be %d)",
7929 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7930 return error_mark_node;
7933 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7934 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7935 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7936 else
7938 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7939 TREE_CONSTANT (argument_pack) = 1;
7942 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7943 if (CHECKING_P)
7944 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7945 TREE_VEC_LENGTH (packed_args));
7946 return argument_pack;
7949 /* Returns the number of pack expansions in the template argument vector
7950 ARGS. */
7952 static int
7953 pack_expansion_args_count (tree args)
7955 int i;
7956 int count = 0;
7957 if (args)
7958 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7960 tree elt = TREE_VEC_ELT (args, i);
7961 if (elt && PACK_EXPANSION_P (elt))
7962 ++count;
7964 return count;
7967 /* Convert all template arguments to their appropriate types, and
7968 return a vector containing the innermost resulting template
7969 arguments. If any error occurs, return error_mark_node. Error and
7970 warning messages are issued under control of COMPLAIN.
7972 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7973 for arguments not specified in ARGS. Otherwise, if
7974 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7975 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7976 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7977 ARGS. */
7979 static tree
7980 coerce_template_parms (tree parms,
7981 tree args,
7982 tree in_decl,
7983 tsubst_flags_t complain,
7984 bool require_all_args,
7985 bool use_default_args)
7987 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7988 tree orig_inner_args;
7989 tree inner_args;
7990 tree new_args;
7991 tree new_inner_args;
7992 int saved_unevaluated_operand;
7993 int saved_inhibit_evaluation_warnings;
7995 /* When used as a boolean value, indicates whether this is a
7996 variadic template parameter list. Since it's an int, we can also
7997 subtract it from nparms to get the number of non-variadic
7998 parameters. */
7999 int variadic_p = 0;
8000 int variadic_args_p = 0;
8001 int post_variadic_parms = 0;
8003 /* Likewise for parameters with default arguments. */
8004 int default_p = 0;
8006 if (args == error_mark_node)
8007 return error_mark_node;
8009 nparms = TREE_VEC_LENGTH (parms);
8011 /* Determine if there are any parameter packs or default arguments. */
8012 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8014 tree parm = TREE_VEC_ELT (parms, parm_idx);
8015 if (variadic_p)
8016 ++post_variadic_parms;
8017 if (template_parameter_pack_p (TREE_VALUE (parm)))
8018 ++variadic_p;
8019 if (TREE_PURPOSE (parm))
8020 ++default_p;
8023 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8024 /* If there are no parameters that follow a parameter pack, we need to
8025 expand any argument packs so that we can deduce a parameter pack from
8026 some non-packed args followed by an argument pack, as in variadic85.C.
8027 If there are such parameters, we need to leave argument packs intact
8028 so the arguments are assigned properly. This can happen when dealing
8029 with a nested class inside a partial specialization of a class
8030 template, as in variadic92.C, or when deducing a template parameter pack
8031 from a sub-declarator, as in variadic114.C. */
8032 if (!post_variadic_parms)
8033 inner_args = expand_template_argument_pack (inner_args);
8035 /* Count any pack expansion args. */
8036 variadic_args_p = pack_expansion_args_count (inner_args);
8038 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8039 if ((nargs - variadic_args_p > nparms && !variadic_p)
8040 || (nargs < nparms - variadic_p
8041 && require_all_args
8042 && !variadic_args_p
8043 && (!use_default_args
8044 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8045 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8047 if (complain & tf_error)
8049 if (variadic_p || default_p)
8051 nparms -= variadic_p + default_p;
8052 error ("wrong number of template arguments "
8053 "(%d, should be at least %d)", nargs, nparms);
8055 else
8056 error ("wrong number of template arguments "
8057 "(%d, should be %d)", nargs, nparms);
8059 if (in_decl)
8060 inform (DECL_SOURCE_LOCATION (in_decl),
8061 "provided for %qD", in_decl);
8064 return error_mark_node;
8066 /* We can't pass a pack expansion to a non-pack parameter of an alias
8067 template (DR 1430). */
8068 else if (in_decl
8069 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8070 || concept_template_p (in_decl))
8071 && variadic_args_p
8072 && nargs - variadic_args_p < nparms - variadic_p)
8074 if (complain & tf_error)
8076 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8078 tree arg = TREE_VEC_ELT (inner_args, i);
8079 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8081 if (PACK_EXPANSION_P (arg)
8082 && !template_parameter_pack_p (parm))
8084 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8085 error_at (location_of (arg),
8086 "pack expansion argument for non-pack parameter "
8087 "%qD of alias template %qD", parm, in_decl);
8088 else
8089 error_at (location_of (arg),
8090 "pack expansion argument for non-pack parameter "
8091 "%qD of concept %qD", parm, in_decl);
8092 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8093 goto found;
8096 gcc_unreachable ();
8097 found:;
8099 return error_mark_node;
8102 /* We need to evaluate the template arguments, even though this
8103 template-id may be nested within a "sizeof". */
8104 saved_unevaluated_operand = cp_unevaluated_operand;
8105 cp_unevaluated_operand = 0;
8106 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8107 c_inhibit_evaluation_warnings = 0;
8108 new_inner_args = make_tree_vec (nparms);
8109 new_args = add_outermost_template_args (args, new_inner_args);
8110 int pack_adjust = 0;
8111 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8113 tree arg;
8114 tree parm;
8116 /* Get the Ith template parameter. */
8117 parm = TREE_VEC_ELT (parms, parm_idx);
8119 if (parm == error_mark_node)
8121 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8122 continue;
8125 /* Calculate the next argument. */
8126 if (arg_idx < nargs)
8127 arg = TREE_VEC_ELT (inner_args, arg_idx);
8128 else
8129 arg = NULL_TREE;
8131 if (template_parameter_pack_p (TREE_VALUE (parm))
8132 && !(arg && ARGUMENT_PACK_P (arg)))
8134 /* Some arguments will be placed in the
8135 template parameter pack PARM. */
8136 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8137 inner_args, arg_idx,
8138 new_args, &lost,
8139 in_decl, complain);
8141 if (arg == NULL_TREE)
8143 /* We don't know how many args we have yet, just use the
8144 unconverted (and still packed) ones for now. */
8145 new_inner_args = orig_inner_args;
8146 arg_idx = nargs;
8147 break;
8150 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8152 /* Store this argument. */
8153 if (arg == error_mark_node)
8155 lost++;
8156 /* We are done with all of the arguments. */
8157 arg_idx = nargs;
8159 else
8161 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8162 arg_idx += pack_adjust;
8165 continue;
8167 else if (arg)
8169 if (PACK_EXPANSION_P (arg))
8171 /* "If every valid specialization of a variadic template
8172 requires an empty template parameter pack, the template is
8173 ill-formed, no diagnostic required." So check that the
8174 pattern works with this parameter. */
8175 tree pattern = PACK_EXPANSION_PATTERN (arg);
8176 tree conv = convert_template_argument (TREE_VALUE (parm),
8177 pattern, new_args,
8178 complain, parm_idx,
8179 in_decl);
8180 if (conv == error_mark_node)
8182 if (complain & tf_error)
8183 inform (input_location, "so any instantiation with a "
8184 "non-empty parameter pack would be ill-formed");
8185 ++lost;
8187 else if (TYPE_P (conv) && !TYPE_P (pattern))
8188 /* Recover from missing typename. */
8189 TREE_VEC_ELT (inner_args, arg_idx)
8190 = make_pack_expansion (conv);
8192 /* We don't know how many args we have yet, just
8193 use the unconverted ones for now. */
8194 new_inner_args = inner_args;
8195 arg_idx = nargs;
8196 break;
8199 else if (require_all_args)
8201 /* There must be a default arg in this case. */
8202 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8203 complain, in_decl);
8204 /* The position of the first default template argument,
8205 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8206 Record that. */
8207 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8208 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8209 arg_idx - pack_adjust);
8211 else
8212 break;
8214 if (arg == error_mark_node)
8216 if (complain & tf_error)
8217 error ("template argument %d is invalid", arg_idx + 1);
8219 else if (!arg)
8220 /* This only occurs if there was an error in the template
8221 parameter list itself (which we would already have
8222 reported) that we are trying to recover from, e.g., a class
8223 template with a parameter list such as
8224 template<typename..., typename>. */
8225 ++lost;
8226 else
8227 arg = convert_template_argument (TREE_VALUE (parm),
8228 arg, new_args, complain,
8229 parm_idx, in_decl);
8231 if (arg == error_mark_node)
8232 lost++;
8233 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8235 cp_unevaluated_operand = saved_unevaluated_operand;
8236 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8238 if (variadic_p && arg_idx < nargs)
8240 if (complain & tf_error)
8242 error ("wrong number of template arguments "
8243 "(%d, should be %d)", nargs, arg_idx);
8244 if (in_decl)
8245 error ("provided for %q+D", in_decl);
8247 return error_mark_node;
8250 if (lost)
8251 return error_mark_node;
8253 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8254 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8255 TREE_VEC_LENGTH (new_inner_args));
8257 return new_inner_args;
8260 /* Convert all template arguments to their appropriate types, and
8261 return a vector containing the innermost resulting template
8262 arguments. If any error occurs, return error_mark_node. Error and
8263 warning messages are not issued.
8265 Note that no function argument deduction is performed, and default
8266 arguments are used to fill in unspecified arguments. */
8267 tree
8268 coerce_template_parms (tree parms, tree args, tree in_decl)
8270 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8273 /* Convert all template arguments to their appropriate type, and
8274 instantiate default arguments as needed. This returns a vector
8275 containing the innermost resulting template arguments, or
8276 error_mark_node if unsuccessful. */
8277 tree
8278 coerce_template_parms (tree parms, tree args, tree in_decl,
8279 tsubst_flags_t complain)
8281 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8284 /* Like coerce_template_parms. If PARMS represents all template
8285 parameters levels, this function returns a vector of vectors
8286 representing all the resulting argument levels. Note that in this
8287 case, only the innermost arguments are coerced because the
8288 outermost ones are supposed to have been coerced already.
8290 Otherwise, if PARMS represents only (the innermost) vector of
8291 parameters, this function returns a vector containing just the
8292 innermost resulting arguments. */
8294 static tree
8295 coerce_innermost_template_parms (tree parms,
8296 tree args,
8297 tree in_decl,
8298 tsubst_flags_t complain,
8299 bool require_all_args,
8300 bool use_default_args)
8302 int parms_depth = TMPL_PARMS_DEPTH (parms);
8303 int args_depth = TMPL_ARGS_DEPTH (args);
8304 tree coerced_args;
8306 if (parms_depth > 1)
8308 coerced_args = make_tree_vec (parms_depth);
8309 tree level;
8310 int cur_depth;
8312 for (level = parms, cur_depth = parms_depth;
8313 parms_depth > 0 && level != NULL_TREE;
8314 level = TREE_CHAIN (level), --cur_depth)
8316 tree l;
8317 if (cur_depth == args_depth)
8318 l = coerce_template_parms (TREE_VALUE (level),
8319 args, in_decl, complain,
8320 require_all_args,
8321 use_default_args);
8322 else
8323 l = TMPL_ARGS_LEVEL (args, cur_depth);
8325 if (l == error_mark_node)
8326 return error_mark_node;
8328 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8331 else
8332 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8333 args, in_decl, complain,
8334 require_all_args,
8335 use_default_args);
8336 return coerced_args;
8339 /* Returns 1 if template args OT and NT are equivalent. */
8342 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8344 if (nt == ot)
8345 return 1;
8346 if (nt == NULL_TREE || ot == NULL_TREE)
8347 return false;
8348 if (nt == any_targ_node || ot == any_targ_node)
8349 return true;
8351 if (TREE_CODE (nt) == TREE_VEC)
8352 /* For member templates */
8353 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8354 else if (PACK_EXPANSION_P (ot))
8355 return (PACK_EXPANSION_P (nt)
8356 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8357 PACK_EXPANSION_PATTERN (nt))
8358 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8359 PACK_EXPANSION_EXTRA_ARGS (nt)));
8360 else if (ARGUMENT_PACK_P (ot))
8362 int i, len;
8363 tree opack, npack;
8365 if (!ARGUMENT_PACK_P (nt))
8366 return 0;
8368 opack = ARGUMENT_PACK_ARGS (ot);
8369 npack = ARGUMENT_PACK_ARGS (nt);
8370 len = TREE_VEC_LENGTH (opack);
8371 if (TREE_VEC_LENGTH (npack) != len)
8372 return 0;
8373 for (i = 0; i < len; ++i)
8374 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8375 TREE_VEC_ELT (npack, i)))
8376 return 0;
8377 return 1;
8379 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8380 gcc_unreachable ();
8381 else if (TYPE_P (nt))
8383 if (!TYPE_P (ot))
8384 return false;
8385 /* Don't treat an alias template specialization with dependent
8386 arguments as equivalent to its underlying type when used as a
8387 template argument; we need them to be distinct so that we
8388 substitute into the specialization arguments at instantiation
8389 time. And aliases can't be equivalent without being ==, so
8390 we don't need to look any deeper.
8392 During partial ordering, however, we need to treat them normally so
8393 that we can order uses of the same alias with different
8394 cv-qualification (79960). */
8395 if (!partial_order
8396 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8397 return false;
8398 else
8399 return same_type_p (ot, nt);
8401 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8402 return 0;
8403 else
8405 /* Try to treat a template non-type argument that has been converted
8406 to the parameter type as equivalent to one that hasn't yet. */
8407 for (enum tree_code code1 = TREE_CODE (ot);
8408 CONVERT_EXPR_CODE_P (code1)
8409 || code1 == NON_LVALUE_EXPR;
8410 code1 = TREE_CODE (ot))
8411 ot = TREE_OPERAND (ot, 0);
8412 for (enum tree_code code2 = TREE_CODE (nt);
8413 CONVERT_EXPR_CODE_P (code2)
8414 || code2 == NON_LVALUE_EXPR;
8415 code2 = TREE_CODE (nt))
8416 nt = TREE_OPERAND (nt, 0);
8418 return cp_tree_equal (ot, nt);
8422 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8423 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8424 NEWARG_PTR with the offending arguments if they are non-NULL. */
8427 comp_template_args (tree oldargs, tree newargs,
8428 tree *oldarg_ptr, tree *newarg_ptr,
8429 bool partial_order)
8431 int i;
8433 if (oldargs == newargs)
8434 return 1;
8436 if (!oldargs || !newargs)
8437 return 0;
8439 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8440 return 0;
8442 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8444 tree nt = TREE_VEC_ELT (newargs, i);
8445 tree ot = TREE_VEC_ELT (oldargs, i);
8447 if (! template_args_equal (ot, nt, partial_order))
8449 if (oldarg_ptr != NULL)
8450 *oldarg_ptr = ot;
8451 if (newarg_ptr != NULL)
8452 *newarg_ptr = nt;
8453 return 0;
8456 return 1;
8459 inline bool
8460 comp_template_args_porder (tree oargs, tree nargs)
8462 return comp_template_args (oargs, nargs, NULL, NULL, true);
8465 static void
8466 add_pending_template (tree d)
8468 tree ti = (TYPE_P (d)
8469 ? CLASSTYPE_TEMPLATE_INFO (d)
8470 : DECL_TEMPLATE_INFO (d));
8471 struct pending_template *pt;
8472 int level;
8474 if (TI_PENDING_TEMPLATE_FLAG (ti))
8475 return;
8477 /* We are called both from instantiate_decl, where we've already had a
8478 tinst_level pushed, and instantiate_template, where we haven't.
8479 Compensate. */
8480 level = !current_tinst_level || current_tinst_level->decl != d;
8482 if (level)
8483 push_tinst_level (d);
8485 pt = ggc_alloc<pending_template> ();
8486 pt->next = NULL;
8487 pt->tinst = current_tinst_level;
8488 if (last_pending_template)
8489 last_pending_template->next = pt;
8490 else
8491 pending_templates = pt;
8493 last_pending_template = pt;
8495 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8497 if (level)
8498 pop_tinst_level ();
8502 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8503 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8504 documentation for TEMPLATE_ID_EXPR. */
8506 tree
8507 lookup_template_function (tree fns, tree arglist)
8509 tree type;
8511 if (fns == error_mark_node || arglist == error_mark_node)
8512 return error_mark_node;
8514 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8516 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8518 error ("%q#D is not a function template", fns);
8519 return error_mark_node;
8522 if (BASELINK_P (fns))
8524 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8525 unknown_type_node,
8526 BASELINK_FUNCTIONS (fns),
8527 arglist);
8528 return fns;
8531 type = TREE_TYPE (fns);
8532 if (TREE_CODE (fns) == OVERLOAD || !type)
8533 type = unknown_type_node;
8535 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8538 /* Within the scope of a template class S<T>, the name S gets bound
8539 (in build_self_reference) to a TYPE_DECL for the class, not a
8540 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8541 or one of its enclosing classes, and that type is a template,
8542 return the associated TEMPLATE_DECL. Otherwise, the original
8543 DECL is returned.
8545 Also handle the case when DECL is a TREE_LIST of ambiguous
8546 injected-class-names from different bases. */
8548 tree
8549 maybe_get_template_decl_from_type_decl (tree decl)
8551 if (decl == NULL_TREE)
8552 return decl;
8554 /* DR 176: A lookup that finds an injected-class-name (10.2
8555 [class.member.lookup]) can result in an ambiguity in certain cases
8556 (for example, if it is found in more than one base class). If all of
8557 the injected-class-names that are found refer to specializations of
8558 the same class template, and if the name is followed by a
8559 template-argument-list, the reference refers to the class template
8560 itself and not a specialization thereof, and is not ambiguous. */
8561 if (TREE_CODE (decl) == TREE_LIST)
8563 tree t, tmpl = NULL_TREE;
8564 for (t = decl; t; t = TREE_CHAIN (t))
8566 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8567 if (!tmpl)
8568 tmpl = elt;
8569 else if (tmpl != elt)
8570 break;
8572 if (tmpl && t == NULL_TREE)
8573 return tmpl;
8574 else
8575 return decl;
8578 return (decl != NULL_TREE
8579 && DECL_SELF_REFERENCE_P (decl)
8580 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8581 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8584 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8585 parameters, find the desired type.
8587 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8589 IN_DECL, if non-NULL, is the template declaration we are trying to
8590 instantiate.
8592 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8593 the class we are looking up.
8595 Issue error and warning messages under control of COMPLAIN.
8597 If the template class is really a local class in a template
8598 function, then the FUNCTION_CONTEXT is the function in which it is
8599 being instantiated.
8601 ??? Note that this function is currently called *twice* for each
8602 template-id: the first time from the parser, while creating the
8603 incomplete type (finish_template_type), and the second type during the
8604 real instantiation (instantiate_template_class). This is surely something
8605 that we want to avoid. It also causes some problems with argument
8606 coercion (see convert_nontype_argument for more information on this). */
8608 static tree
8609 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8610 int entering_scope, tsubst_flags_t complain)
8612 tree templ = NULL_TREE, parmlist;
8613 tree t;
8614 spec_entry **slot;
8615 spec_entry *entry;
8616 spec_entry elt;
8617 hashval_t hash;
8619 if (identifier_p (d1))
8621 tree value = innermost_non_namespace_value (d1);
8622 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8623 templ = value;
8624 else
8626 if (context)
8627 push_decl_namespace (context);
8628 templ = lookup_name (d1);
8629 templ = maybe_get_template_decl_from_type_decl (templ);
8630 if (context)
8631 pop_decl_namespace ();
8633 if (templ)
8634 context = DECL_CONTEXT (templ);
8636 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8638 tree type = TREE_TYPE (d1);
8640 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8641 an implicit typename for the second A. Deal with it. */
8642 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8643 type = TREE_TYPE (type);
8645 if (CLASSTYPE_TEMPLATE_INFO (type))
8647 templ = CLASSTYPE_TI_TEMPLATE (type);
8648 d1 = DECL_NAME (templ);
8651 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8652 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8654 templ = TYPE_TI_TEMPLATE (d1);
8655 d1 = DECL_NAME (templ);
8657 else if (DECL_TYPE_TEMPLATE_P (d1))
8659 templ = d1;
8660 d1 = DECL_NAME (templ);
8661 context = DECL_CONTEXT (templ);
8663 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8665 templ = d1;
8666 d1 = DECL_NAME (templ);
8669 /* Issue an error message if we didn't find a template. */
8670 if (! templ)
8672 if (complain & tf_error)
8673 error ("%qT is not a template", d1);
8674 return error_mark_node;
8677 if (TREE_CODE (templ) != TEMPLATE_DECL
8678 /* Make sure it's a user visible template, if it was named by
8679 the user. */
8680 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8681 && !PRIMARY_TEMPLATE_P (templ)))
8683 if (complain & tf_error)
8685 error ("non-template type %qT used as a template", d1);
8686 if (in_decl)
8687 error ("for template declaration %q+D", in_decl);
8689 return error_mark_node;
8692 complain &= ~tf_user;
8694 /* An alias that just changes the name of a template is equivalent to the
8695 other template, so if any of the arguments are pack expansions, strip
8696 the alias to avoid problems with a pack expansion passed to a non-pack
8697 alias template parameter (DR 1430). */
8698 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8699 templ = get_underlying_template (templ);
8701 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8703 tree parm;
8704 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8705 if (arglist2 == error_mark_node
8706 || (!uses_template_parms (arglist2)
8707 && check_instantiated_args (templ, arglist2, complain)))
8708 return error_mark_node;
8710 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8711 return parm;
8713 else
8715 tree template_type = TREE_TYPE (templ);
8716 tree gen_tmpl;
8717 tree type_decl;
8718 tree found = NULL_TREE;
8719 int arg_depth;
8720 int parm_depth;
8721 int is_dependent_type;
8722 int use_partial_inst_tmpl = false;
8724 if (template_type == error_mark_node)
8725 /* An error occurred while building the template TEMPL, and a
8726 diagnostic has most certainly been emitted for that
8727 already. Let's propagate that error. */
8728 return error_mark_node;
8730 gen_tmpl = most_general_template (templ);
8731 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8732 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8733 arg_depth = TMPL_ARGS_DEPTH (arglist);
8735 if (arg_depth == 1 && parm_depth > 1)
8737 /* We've been given an incomplete set of template arguments.
8738 For example, given:
8740 template <class T> struct S1 {
8741 template <class U> struct S2 {};
8742 template <class U> struct S2<U*> {};
8745 we will be called with an ARGLIST of `U*', but the
8746 TEMPLATE will be `template <class T> template
8747 <class U> struct S1<T>::S2'. We must fill in the missing
8748 arguments. */
8749 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8750 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8751 arg_depth = TMPL_ARGS_DEPTH (arglist);
8754 /* Now we should have enough arguments. */
8755 gcc_assert (parm_depth == arg_depth);
8757 /* From here on, we're only interested in the most general
8758 template. */
8760 /* Calculate the BOUND_ARGS. These will be the args that are
8761 actually tsubst'd into the definition to create the
8762 instantiation. */
8763 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8764 complain,
8765 /*require_all_args=*/true,
8766 /*use_default_args=*/true);
8768 if (arglist == error_mark_node)
8769 /* We were unable to bind the arguments. */
8770 return error_mark_node;
8772 /* In the scope of a template class, explicit references to the
8773 template class refer to the type of the template, not any
8774 instantiation of it. For example, in:
8776 template <class T> class C { void f(C<T>); }
8778 the `C<T>' is just the same as `C'. Outside of the
8779 class, however, such a reference is an instantiation. */
8780 if (entering_scope
8781 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8782 || currently_open_class (template_type))
8784 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8786 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8787 return template_type;
8790 /* If we already have this specialization, return it. */
8791 elt.tmpl = gen_tmpl;
8792 elt.args = arglist;
8793 elt.spec = NULL_TREE;
8794 hash = spec_hasher::hash (&elt);
8795 entry = type_specializations->find_with_hash (&elt, hash);
8797 if (entry)
8798 return entry->spec;
8800 /* If the the template's constraints are not satisfied,
8801 then we cannot form a valid type.
8803 Note that the check is deferred until after the hash
8804 lookup. This prevents redundant checks on previously
8805 instantiated specializations. */
8806 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8808 if (complain & tf_error)
8810 error ("template constraint failure");
8811 diagnose_constraints (input_location, gen_tmpl, arglist);
8813 return error_mark_node;
8816 is_dependent_type = uses_template_parms (arglist);
8818 /* If the deduced arguments are invalid, then the binding
8819 failed. */
8820 if (!is_dependent_type
8821 && check_instantiated_args (gen_tmpl,
8822 INNERMOST_TEMPLATE_ARGS (arglist),
8823 complain))
8824 return error_mark_node;
8826 if (!is_dependent_type
8827 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8828 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8829 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8831 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8832 DECL_NAME (gen_tmpl),
8833 /*tag_scope=*/ts_global);
8834 return found;
8837 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8838 complain, in_decl);
8839 if (context == error_mark_node)
8840 return error_mark_node;
8842 if (!context)
8843 context = global_namespace;
8845 /* Create the type. */
8846 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8848 /* The user referred to a specialization of an alias
8849 template represented by GEN_TMPL.
8851 [temp.alias]/2 says:
8853 When a template-id refers to the specialization of an
8854 alias template, it is equivalent to the associated
8855 type obtained by substitution of its
8856 template-arguments for the template-parameters in the
8857 type-id of the alias template. */
8859 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8860 /* Note that the call above (by indirectly calling
8861 register_specialization in tsubst_decl) registers the
8862 TYPE_DECL representing the specialization of the alias
8863 template. So next time someone substitutes ARGLIST for
8864 the template parms into the alias template (GEN_TMPL),
8865 she'll get that TYPE_DECL back. */
8867 if (t == error_mark_node)
8868 return t;
8870 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8872 if (!is_dependent_type)
8874 set_current_access_from_decl (TYPE_NAME (template_type));
8875 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8876 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8877 arglist, complain, in_decl),
8878 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8879 arglist, complain, in_decl),
8880 SCOPED_ENUM_P (template_type), NULL);
8882 if (t == error_mark_node)
8883 return t;
8885 else
8887 /* We don't want to call start_enum for this type, since
8888 the values for the enumeration constants may involve
8889 template parameters. And, no one should be interested
8890 in the enumeration constants for such a type. */
8891 t = cxx_make_type (ENUMERAL_TYPE);
8892 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8894 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8895 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8896 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8898 else if (CLASS_TYPE_P (template_type))
8900 t = make_class_type (TREE_CODE (template_type));
8901 CLASSTYPE_DECLARED_CLASS (t)
8902 = CLASSTYPE_DECLARED_CLASS (template_type);
8903 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8905 /* A local class. Make sure the decl gets registered properly. */
8906 if (context == current_function_decl)
8907 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8909 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8910 /* This instantiation is another name for the primary
8911 template type. Set the TYPE_CANONICAL field
8912 appropriately. */
8913 TYPE_CANONICAL (t) = template_type;
8914 else if (any_template_arguments_need_structural_equality_p (arglist))
8915 /* Some of the template arguments require structural
8916 equality testing, so this template class requires
8917 structural equality testing. */
8918 SET_TYPE_STRUCTURAL_EQUALITY (t);
8920 else
8921 gcc_unreachable ();
8923 /* If we called start_enum or pushtag above, this information
8924 will already be set up. */
8925 if (!TYPE_NAME (t))
8927 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8929 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8930 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8931 DECL_SOURCE_LOCATION (type_decl)
8932 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8934 else
8935 type_decl = TYPE_NAME (t);
8937 if (CLASS_TYPE_P (template_type))
8939 TREE_PRIVATE (type_decl)
8940 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8941 TREE_PROTECTED (type_decl)
8942 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8943 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8945 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8946 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8950 if (OVERLOAD_TYPE_P (t)
8951 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8953 static const char *tags[] = {"abi_tag", "may_alias"};
8955 for (unsigned ix = 0; ix != 2; ix++)
8957 tree attributes
8958 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8960 if (attributes)
8961 TYPE_ATTRIBUTES (t)
8962 = tree_cons (TREE_PURPOSE (attributes),
8963 TREE_VALUE (attributes),
8964 TYPE_ATTRIBUTES (t));
8968 /* Let's consider the explicit specialization of a member
8969 of a class template specialization that is implicitly instantiated,
8970 e.g.:
8971 template<class T>
8972 struct S
8974 template<class U> struct M {}; //#0
8977 template<>
8978 template<>
8979 struct S<int>::M<char> //#1
8981 int i;
8983 [temp.expl.spec]/4 says this is valid.
8985 In this case, when we write:
8986 S<int>::M<char> m;
8988 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8989 the one of #0.
8991 When we encounter #1, we want to store the partial instantiation
8992 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8994 For all cases other than this "explicit specialization of member of a
8995 class template", we just want to store the most general template into
8996 the CLASSTYPE_TI_TEMPLATE of M.
8998 This case of "explicit specialization of member of a class template"
8999 only happens when:
9000 1/ the enclosing class is an instantiation of, and therefore not
9001 the same as, the context of the most general template, and
9002 2/ we aren't looking at the partial instantiation itself, i.e.
9003 the innermost arguments are not the same as the innermost parms of
9004 the most general template.
9006 So it's only when 1/ and 2/ happens that we want to use the partial
9007 instantiation of the member template in lieu of its most general
9008 template. */
9010 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9011 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9012 /* the enclosing class must be an instantiation... */
9013 && CLASS_TYPE_P (context)
9014 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9016 TREE_VEC_LENGTH (arglist)--;
9017 ++processing_template_decl;
9018 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9019 tree partial_inst_args =
9020 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9021 arglist, complain, NULL_TREE);
9022 --processing_template_decl;
9023 TREE_VEC_LENGTH (arglist)++;
9024 if (partial_inst_args == error_mark_node)
9025 return error_mark_node;
9026 use_partial_inst_tmpl =
9027 /*...and we must not be looking at the partial instantiation
9028 itself. */
9029 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9030 partial_inst_args);
9033 if (!use_partial_inst_tmpl)
9034 /* This case is easy; there are no member templates involved. */
9035 found = gen_tmpl;
9036 else
9038 /* This is a full instantiation of a member template. Find
9039 the partial instantiation of which this is an instance. */
9041 /* Temporarily reduce by one the number of levels in the ARGLIST
9042 so as to avoid comparing the last set of arguments. */
9043 TREE_VEC_LENGTH (arglist)--;
9044 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9045 TREE_VEC_LENGTH (arglist)++;
9046 /* FOUND is either a proper class type, or an alias
9047 template specialization. In the later case, it's a
9048 TYPE_DECL, resulting from the substituting of arguments
9049 for parameters in the TYPE_DECL of the alias template
9050 done earlier. So be careful while getting the template
9051 of FOUND. */
9052 found = (TREE_CODE (found) == TEMPLATE_DECL
9053 ? found
9054 : (TREE_CODE (found) == TYPE_DECL
9055 ? DECL_TI_TEMPLATE (found)
9056 : CLASSTYPE_TI_TEMPLATE (found)));
9059 // Build template info for the new specialization.
9060 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9062 elt.spec = t;
9063 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9064 entry = ggc_alloc<spec_entry> ();
9065 *entry = elt;
9066 *slot = entry;
9068 /* Note this use of the partial instantiation so we can check it
9069 later in maybe_process_partial_specialization. */
9070 DECL_TEMPLATE_INSTANTIATIONS (found)
9071 = tree_cons (arglist, t,
9072 DECL_TEMPLATE_INSTANTIATIONS (found));
9074 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9075 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9076 /* Now that the type has been registered on the instantiations
9077 list, we set up the enumerators. Because the enumeration
9078 constants may involve the enumeration type itself, we make
9079 sure to register the type first, and then create the
9080 constants. That way, doing tsubst_expr for the enumeration
9081 constants won't result in recursive calls here; we'll find
9082 the instantiation and exit above. */
9083 tsubst_enum (template_type, t, arglist);
9085 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9086 /* If the type makes use of template parameters, the
9087 code that generates debugging information will crash. */
9088 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9090 /* Possibly limit visibility based on template args. */
9091 TREE_PUBLIC (type_decl) = 1;
9092 determine_visibility (type_decl);
9094 inherit_targ_abi_tags (t);
9096 return t;
9100 /* Wrapper for lookup_template_class_1. */
9102 tree
9103 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9104 int entering_scope, tsubst_flags_t complain)
9106 tree ret;
9107 timevar_push (TV_TEMPLATE_INST);
9108 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9109 entering_scope, complain);
9110 timevar_pop (TV_TEMPLATE_INST);
9111 return ret;
9114 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9116 tree
9117 lookup_template_variable (tree templ, tree arglist)
9119 /* The type of the expression is NULL_TREE since the template-id could refer
9120 to an explicit or partial specialization. */
9121 tree type = NULL_TREE;
9122 if (flag_concepts && variable_concept_p (templ))
9123 /* Except that concepts are always bool. */
9124 type = boolean_type_node;
9125 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9128 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9130 tree
9131 finish_template_variable (tree var, tsubst_flags_t complain)
9133 tree templ = TREE_OPERAND (var, 0);
9134 tree arglist = TREE_OPERAND (var, 1);
9136 /* We never want to return a VAR_DECL for a variable concept, since they
9137 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9138 bool concept_p = flag_concepts && variable_concept_p (templ);
9139 if (concept_p && processing_template_decl)
9140 return var;
9142 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9143 arglist = add_outermost_template_args (tmpl_args, arglist);
9145 templ = most_general_template (templ);
9146 tree parms = DECL_TEMPLATE_PARMS (templ);
9147 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9148 /*req_all*/true,
9149 /*use_default*/true);
9151 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9153 if (complain & tf_error)
9155 error ("use of invalid variable template %qE", var);
9156 diagnose_constraints (location_of (var), templ, arglist);
9158 return error_mark_node;
9161 /* If a template-id refers to a specialization of a variable
9162 concept, then the expression is true if and only if the
9163 concept's constraints are satisfied by the given template
9164 arguments.
9166 NOTE: This is an extension of Concepts Lite TS that
9167 allows constraints to be used in expressions. */
9168 if (concept_p)
9170 tree decl = DECL_TEMPLATE_RESULT (templ);
9171 return evaluate_variable_concept (decl, arglist);
9174 return instantiate_template (templ, arglist, complain);
9177 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9178 TARGS template args, and instantiate it if it's not dependent. */
9180 tree
9181 lookup_and_finish_template_variable (tree templ, tree targs,
9182 tsubst_flags_t complain)
9184 templ = lookup_template_variable (templ, targs);
9185 if (!any_dependent_template_arguments_p (targs))
9187 templ = finish_template_variable (templ, complain);
9188 mark_used (templ);
9191 return convert_from_reference (templ);
9195 struct pair_fn_data
9197 tree_fn_t fn;
9198 tree_fn_t any_fn;
9199 void *data;
9200 /* True when we should also visit template parameters that occur in
9201 non-deduced contexts. */
9202 bool include_nondeduced_p;
9203 hash_set<tree> *visited;
9206 /* Called from for_each_template_parm via walk_tree. */
9208 static tree
9209 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9211 tree t = *tp;
9212 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9213 tree_fn_t fn = pfd->fn;
9214 void *data = pfd->data;
9215 tree result = NULL_TREE;
9217 #define WALK_SUBTREE(NODE) \
9218 do \
9220 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9221 pfd->include_nondeduced_p, \
9222 pfd->any_fn); \
9223 if (result) goto out; \
9225 while (0)
9227 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9228 return t;
9230 if (TYPE_P (t)
9231 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9232 WALK_SUBTREE (TYPE_CONTEXT (t));
9234 switch (TREE_CODE (t))
9236 case RECORD_TYPE:
9237 if (TYPE_PTRMEMFUNC_P (t))
9238 break;
9239 /* Fall through. */
9241 case UNION_TYPE:
9242 case ENUMERAL_TYPE:
9243 if (!TYPE_TEMPLATE_INFO (t))
9244 *walk_subtrees = 0;
9245 else
9246 WALK_SUBTREE (TYPE_TI_ARGS (t));
9247 break;
9249 case INTEGER_TYPE:
9250 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9251 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9252 break;
9254 case METHOD_TYPE:
9255 /* Since we're not going to walk subtrees, we have to do this
9256 explicitly here. */
9257 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9258 /* Fall through. */
9260 case FUNCTION_TYPE:
9261 /* Check the return type. */
9262 WALK_SUBTREE (TREE_TYPE (t));
9264 /* Check the parameter types. Since default arguments are not
9265 instantiated until they are needed, the TYPE_ARG_TYPES may
9266 contain expressions that involve template parameters. But,
9267 no-one should be looking at them yet. And, once they're
9268 instantiated, they don't contain template parameters, so
9269 there's no point in looking at them then, either. */
9271 tree parm;
9273 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9274 WALK_SUBTREE (TREE_VALUE (parm));
9276 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9277 want walk_tree walking into them itself. */
9278 *walk_subtrees = 0;
9281 if (flag_noexcept_type)
9283 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9284 if (spec)
9285 WALK_SUBTREE (TREE_PURPOSE (spec));
9287 break;
9289 case TYPEOF_TYPE:
9290 case UNDERLYING_TYPE:
9291 if (pfd->include_nondeduced_p
9292 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9293 pfd->visited,
9294 pfd->include_nondeduced_p,
9295 pfd->any_fn))
9296 return error_mark_node;
9297 break;
9299 case FUNCTION_DECL:
9300 case VAR_DECL:
9301 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9302 WALK_SUBTREE (DECL_TI_ARGS (t));
9303 /* Fall through. */
9305 case PARM_DECL:
9306 case CONST_DECL:
9307 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9308 WALK_SUBTREE (DECL_INITIAL (t));
9309 if (DECL_CONTEXT (t)
9310 && pfd->include_nondeduced_p)
9311 WALK_SUBTREE (DECL_CONTEXT (t));
9312 break;
9314 case BOUND_TEMPLATE_TEMPLATE_PARM:
9315 /* Record template parameters such as `T' inside `TT<T>'. */
9316 WALK_SUBTREE (TYPE_TI_ARGS (t));
9317 /* Fall through. */
9319 case TEMPLATE_TEMPLATE_PARM:
9320 case TEMPLATE_TYPE_PARM:
9321 case TEMPLATE_PARM_INDEX:
9322 if (fn && (*fn)(t, data))
9323 return t;
9324 else if (!fn)
9325 return t;
9326 break;
9328 case TEMPLATE_DECL:
9329 /* A template template parameter is encountered. */
9330 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9331 WALK_SUBTREE (TREE_TYPE (t));
9333 /* Already substituted template template parameter */
9334 *walk_subtrees = 0;
9335 break;
9337 case TYPENAME_TYPE:
9338 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9339 partial instantiation. */
9340 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9341 break;
9343 case CONSTRUCTOR:
9344 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9345 && pfd->include_nondeduced_p)
9346 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9347 break;
9349 case INDIRECT_REF:
9350 case COMPONENT_REF:
9351 /* If there's no type, then this thing must be some expression
9352 involving template parameters. */
9353 if (!fn && !TREE_TYPE (t))
9354 return error_mark_node;
9355 break;
9357 case MODOP_EXPR:
9358 case CAST_EXPR:
9359 case IMPLICIT_CONV_EXPR:
9360 case REINTERPRET_CAST_EXPR:
9361 case CONST_CAST_EXPR:
9362 case STATIC_CAST_EXPR:
9363 case DYNAMIC_CAST_EXPR:
9364 case ARROW_EXPR:
9365 case DOTSTAR_EXPR:
9366 case TYPEID_EXPR:
9367 case PSEUDO_DTOR_EXPR:
9368 if (!fn)
9369 return error_mark_node;
9370 break;
9372 default:
9373 break;
9376 #undef WALK_SUBTREE
9378 /* We didn't find any template parameters we liked. */
9379 out:
9380 return result;
9383 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9384 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9385 call FN with the parameter and the DATA.
9386 If FN returns nonzero, the iteration is terminated, and
9387 for_each_template_parm returns 1. Otherwise, the iteration
9388 continues. If FN never returns a nonzero value, the value
9389 returned by for_each_template_parm is 0. If FN is NULL, it is
9390 considered to be the function which always returns 1.
9392 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9393 parameters that occur in non-deduced contexts. When false, only
9394 visits those template parameters that can be deduced. */
9396 static tree
9397 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9398 hash_set<tree> *visited,
9399 bool include_nondeduced_p,
9400 tree_fn_t any_fn)
9402 struct pair_fn_data pfd;
9403 tree result;
9405 /* Set up. */
9406 pfd.fn = fn;
9407 pfd.any_fn = any_fn;
9408 pfd.data = data;
9409 pfd.include_nondeduced_p = include_nondeduced_p;
9411 /* Walk the tree. (Conceptually, we would like to walk without
9412 duplicates, but for_each_template_parm_r recursively calls
9413 for_each_template_parm, so we would need to reorganize a fair
9414 bit to use walk_tree_without_duplicates, so we keep our own
9415 visited list.) */
9416 if (visited)
9417 pfd.visited = visited;
9418 else
9419 pfd.visited = new hash_set<tree>;
9420 result = cp_walk_tree (&t,
9421 for_each_template_parm_r,
9422 &pfd,
9423 pfd.visited);
9425 /* Clean up. */
9426 if (!visited)
9428 delete pfd.visited;
9429 pfd.visited = 0;
9432 return result;
9435 /* Returns true if T depends on any template parameter. */
9438 uses_template_parms (tree t)
9440 if (t == NULL_TREE)
9441 return false;
9443 bool dependent_p;
9444 int saved_processing_template_decl;
9446 saved_processing_template_decl = processing_template_decl;
9447 if (!saved_processing_template_decl)
9448 processing_template_decl = 1;
9449 if (TYPE_P (t))
9450 dependent_p = dependent_type_p (t);
9451 else if (TREE_CODE (t) == TREE_VEC)
9452 dependent_p = any_dependent_template_arguments_p (t);
9453 else if (TREE_CODE (t) == TREE_LIST)
9454 dependent_p = (uses_template_parms (TREE_VALUE (t))
9455 || uses_template_parms (TREE_CHAIN (t)));
9456 else if (TREE_CODE (t) == TYPE_DECL)
9457 dependent_p = dependent_type_p (TREE_TYPE (t));
9458 else if (DECL_P (t)
9459 || EXPR_P (t)
9460 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9461 || TREE_CODE (t) == OVERLOAD
9462 || BASELINK_P (t)
9463 || identifier_p (t)
9464 || TREE_CODE (t) == TRAIT_EXPR
9465 || TREE_CODE (t) == CONSTRUCTOR
9466 || CONSTANT_CLASS_P (t))
9467 dependent_p = (type_dependent_expression_p (t)
9468 || value_dependent_expression_p (t));
9469 else
9471 gcc_assert (t == error_mark_node);
9472 dependent_p = false;
9475 processing_template_decl = saved_processing_template_decl;
9477 return dependent_p;
9480 /* Returns true iff current_function_decl is an incompletely instantiated
9481 template. Useful instead of processing_template_decl because the latter
9482 is set to 0 during instantiate_non_dependent_expr. */
9484 bool
9485 in_template_function (void)
9487 tree fn = current_function_decl;
9488 bool ret;
9489 ++processing_template_decl;
9490 ret = (fn && DECL_LANG_SPECIFIC (fn)
9491 && DECL_TEMPLATE_INFO (fn)
9492 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9493 --processing_template_decl;
9494 return ret;
9497 /* Returns true iff we are currently within a template other than a generic
9498 lambda. We test this by finding the outermost closure type and checking
9499 whether it is dependent. */
9501 bool
9502 processing_nonlambda_template (void)
9504 if (!processing_template_decl)
9505 return false;
9507 tree outer_closure = NULL_TREE;
9508 for (tree t = current_class_type; t;
9509 t = decl_type_context (TYPE_MAIN_DECL (t)))
9511 if (LAMBDA_TYPE_P (t))
9512 outer_closure = t;
9513 else
9514 break;
9517 if (outer_closure)
9518 return dependent_type_p (outer_closure);
9519 else
9520 return true;
9523 /* Returns true if T depends on any template parameter with level LEVEL. */
9525 bool
9526 uses_template_parms_level (tree t, int level)
9528 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9529 /*include_nondeduced_p=*/true);
9532 /* Returns true if the signature of DECL depends on any template parameter from
9533 its enclosing class. */
9535 bool
9536 uses_outer_template_parms (tree decl)
9538 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9539 if (depth == 0)
9540 return false;
9541 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9542 &depth, NULL, /*include_nondeduced_p=*/true))
9543 return true;
9544 if (PRIMARY_TEMPLATE_P (decl)
9545 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9546 (DECL_TEMPLATE_PARMS (decl)),
9547 template_parm_outer_level,
9548 &depth, NULL, /*include_nondeduced_p=*/true))
9549 return true;
9550 tree ci = get_constraints (decl);
9551 if (ci)
9552 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9553 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9554 &depth, NULL, /*nondeduced*/true))
9555 return true;
9556 return false;
9559 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9560 ill-formed translation unit, i.e. a variable or function that isn't
9561 usable in a constant expression. */
9563 static inline bool
9564 neglectable_inst_p (tree d)
9566 return (DECL_P (d)
9567 && !undeduced_auto_decl (d)
9568 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9569 : decl_maybe_constant_var_p (d)));
9572 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9573 neglectable and instantiated from within an erroneous instantiation. */
9575 static bool
9576 limit_bad_template_recursion (tree decl)
9578 struct tinst_level *lev = current_tinst_level;
9579 int errs = errorcount + sorrycount;
9580 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9581 return false;
9583 for (; lev; lev = lev->next)
9584 if (neglectable_inst_p (lev->decl))
9585 break;
9587 return (lev && errs > lev->errors);
9590 static int tinst_depth;
9591 extern int max_tinst_depth;
9592 int depth_reached;
9594 static GTY(()) struct tinst_level *last_error_tinst_level;
9596 /* We're starting to instantiate D; record the template instantiation context
9597 for diagnostics and to restore it later. */
9599 bool
9600 push_tinst_level (tree d)
9602 return push_tinst_level_loc (d, input_location);
9605 /* We're starting to instantiate D; record the template instantiation context
9606 at LOC for diagnostics and to restore it later. */
9608 bool
9609 push_tinst_level_loc (tree d, location_t loc)
9611 struct tinst_level *new_level;
9613 if (tinst_depth >= max_tinst_depth)
9615 /* Tell error.c not to try to instantiate any templates. */
9616 at_eof = 2;
9617 fatal_error (input_location,
9618 "template instantiation depth exceeds maximum of %d"
9619 " (use -ftemplate-depth= to increase the maximum)",
9620 max_tinst_depth);
9621 return false;
9624 /* If the current instantiation caused problems, don't let it instantiate
9625 anything else. Do allow deduction substitution and decls usable in
9626 constant expressions. */
9627 if (limit_bad_template_recursion (d))
9628 return false;
9630 /* When not -quiet, dump template instantiations other than functions, since
9631 announce_function will take care of those. */
9632 if (!quiet_flag
9633 && TREE_CODE (d) != TREE_LIST
9634 && TREE_CODE (d) != FUNCTION_DECL)
9635 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9637 new_level = ggc_alloc<tinst_level> ();
9638 new_level->decl = d;
9639 new_level->locus = loc;
9640 new_level->errors = errorcount+sorrycount;
9641 new_level->in_system_header_p = in_system_header_at (input_location);
9642 new_level->next = current_tinst_level;
9643 current_tinst_level = new_level;
9645 ++tinst_depth;
9646 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9647 depth_reached = tinst_depth;
9649 return true;
9652 /* We're done instantiating this template; return to the instantiation
9653 context. */
9655 void
9656 pop_tinst_level (void)
9658 /* Restore the filename and line number stashed away when we started
9659 this instantiation. */
9660 input_location = current_tinst_level->locus;
9661 current_tinst_level = current_tinst_level->next;
9662 --tinst_depth;
9665 /* We're instantiating a deferred template; restore the template
9666 instantiation context in which the instantiation was requested, which
9667 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9669 static tree
9670 reopen_tinst_level (struct tinst_level *level)
9672 struct tinst_level *t;
9674 tinst_depth = 0;
9675 for (t = level; t; t = t->next)
9676 ++tinst_depth;
9678 current_tinst_level = level;
9679 pop_tinst_level ();
9680 if (current_tinst_level)
9681 current_tinst_level->errors = errorcount+sorrycount;
9682 return level->decl;
9685 /* Returns the TINST_LEVEL which gives the original instantiation
9686 context. */
9688 struct tinst_level *
9689 outermost_tinst_level (void)
9691 struct tinst_level *level = current_tinst_level;
9692 if (level)
9693 while (level->next)
9694 level = level->next;
9695 return level;
9698 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9699 vector of template arguments, as for tsubst.
9701 Returns an appropriate tsubst'd friend declaration. */
9703 static tree
9704 tsubst_friend_function (tree decl, tree args)
9706 tree new_friend;
9708 if (TREE_CODE (decl) == FUNCTION_DECL
9709 && DECL_TEMPLATE_INSTANTIATION (decl)
9710 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9711 /* This was a friend declared with an explicit template
9712 argument list, e.g.:
9714 friend void f<>(T);
9716 to indicate that f was a template instantiation, not a new
9717 function declaration. Now, we have to figure out what
9718 instantiation of what template. */
9720 tree template_id, arglist, fns;
9721 tree new_args;
9722 tree tmpl;
9723 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9725 /* Friend functions are looked up in the containing namespace scope.
9726 We must enter that scope, to avoid finding member functions of the
9727 current class with same name. */
9728 push_nested_namespace (ns);
9729 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9730 tf_warning_or_error, NULL_TREE,
9731 /*integral_constant_expression_p=*/false);
9732 pop_nested_namespace (ns);
9733 arglist = tsubst (DECL_TI_ARGS (decl), args,
9734 tf_warning_or_error, NULL_TREE);
9735 template_id = lookup_template_function (fns, arglist);
9737 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9738 tmpl = determine_specialization (template_id, new_friend,
9739 &new_args,
9740 /*need_member_template=*/0,
9741 TREE_VEC_LENGTH (args),
9742 tsk_none);
9743 return instantiate_template (tmpl, new_args, tf_error);
9746 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9748 /* The NEW_FRIEND will look like an instantiation, to the
9749 compiler, but is not an instantiation from the point of view of
9750 the language. For example, we might have had:
9752 template <class T> struct S {
9753 template <class U> friend void f(T, U);
9756 Then, in S<int>, template <class U> void f(int, U) is not an
9757 instantiation of anything. */
9758 if (new_friend == error_mark_node)
9759 return error_mark_node;
9761 DECL_USE_TEMPLATE (new_friend) = 0;
9762 if (TREE_CODE (decl) == TEMPLATE_DECL)
9764 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9765 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9766 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9769 /* The mangled name for the NEW_FRIEND is incorrect. The function
9770 is not a template instantiation and should not be mangled like
9771 one. Therefore, we forget the mangling here; we'll recompute it
9772 later if we need it. */
9773 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9775 SET_DECL_RTL (new_friend, NULL);
9776 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9779 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9781 tree old_decl;
9782 tree new_friend_template_info;
9783 tree new_friend_result_template_info;
9784 tree ns;
9785 int new_friend_is_defn;
9787 /* We must save some information from NEW_FRIEND before calling
9788 duplicate decls since that function will free NEW_FRIEND if
9789 possible. */
9790 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9791 new_friend_is_defn =
9792 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9793 (template_for_substitution (new_friend)))
9794 != NULL_TREE);
9795 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9797 /* This declaration is a `primary' template. */
9798 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9800 new_friend_result_template_info
9801 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9803 else
9804 new_friend_result_template_info = NULL_TREE;
9806 /* Inside pushdecl_namespace_level, we will push into the
9807 current namespace. However, the friend function should go
9808 into the namespace of the template. */
9809 ns = decl_namespace_context (new_friend);
9810 push_nested_namespace (ns);
9811 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9812 pop_nested_namespace (ns);
9814 if (old_decl == error_mark_node)
9815 return error_mark_node;
9817 if (old_decl != new_friend)
9819 /* This new friend declaration matched an existing
9820 declaration. For example, given:
9822 template <class T> void f(T);
9823 template <class U> class C {
9824 template <class T> friend void f(T) {}
9827 the friend declaration actually provides the definition
9828 of `f', once C has been instantiated for some type. So,
9829 old_decl will be the out-of-class template declaration,
9830 while new_friend is the in-class definition.
9832 But, if `f' was called before this point, the
9833 instantiation of `f' will have DECL_TI_ARGS corresponding
9834 to `T' but not to `U', references to which might appear
9835 in the definition of `f'. Previously, the most general
9836 template for an instantiation of `f' was the out-of-class
9837 version; now it is the in-class version. Therefore, we
9838 run through all specialization of `f', adding to their
9839 DECL_TI_ARGS appropriately. In particular, they need a
9840 new set of outer arguments, corresponding to the
9841 arguments for this class instantiation.
9843 The same situation can arise with something like this:
9845 friend void f(int);
9846 template <class T> class C {
9847 friend void f(T) {}
9850 when `C<int>' is instantiated. Now, `f(int)' is defined
9851 in the class. */
9853 if (!new_friend_is_defn)
9854 /* On the other hand, if the in-class declaration does
9855 *not* provide a definition, then we don't want to alter
9856 existing definitions. We can just leave everything
9857 alone. */
9859 else
9861 tree new_template = TI_TEMPLATE (new_friend_template_info);
9862 tree new_args = TI_ARGS (new_friend_template_info);
9864 /* Overwrite whatever template info was there before, if
9865 any, with the new template information pertaining to
9866 the declaration. */
9867 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9869 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9871 /* We should have called reregister_specialization in
9872 duplicate_decls. */
9873 gcc_assert (retrieve_specialization (new_template,
9874 new_args, 0)
9875 == old_decl);
9877 /* Instantiate it if the global has already been used. */
9878 if (DECL_ODR_USED (old_decl))
9879 instantiate_decl (old_decl, /*defer_ok=*/true,
9880 /*expl_inst_class_mem_p=*/false);
9882 else
9884 tree t;
9886 /* Indicate that the old function template is a partial
9887 instantiation. */
9888 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9889 = new_friend_result_template_info;
9891 gcc_assert (new_template
9892 == most_general_template (new_template));
9893 gcc_assert (new_template != old_decl);
9895 /* Reassign any specializations already in the hash table
9896 to the new more general template, and add the
9897 additional template args. */
9898 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9899 t != NULL_TREE;
9900 t = TREE_CHAIN (t))
9902 tree spec = TREE_VALUE (t);
9903 spec_entry elt;
9905 elt.tmpl = old_decl;
9906 elt.args = DECL_TI_ARGS (spec);
9907 elt.spec = NULL_TREE;
9909 decl_specializations->remove_elt (&elt);
9911 DECL_TI_ARGS (spec)
9912 = add_outermost_template_args (new_args,
9913 DECL_TI_ARGS (spec));
9915 register_specialization
9916 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9919 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9923 /* The information from NEW_FRIEND has been merged into OLD_DECL
9924 by duplicate_decls. */
9925 new_friend = old_decl;
9928 else
9930 tree context = DECL_CONTEXT (new_friend);
9931 bool dependent_p;
9933 /* In the code
9934 template <class T> class C {
9935 template <class U> friend void C1<U>::f (); // case 1
9936 friend void C2<T>::f (); // case 2
9938 we only need to make sure CONTEXT is a complete type for
9939 case 2. To distinguish between the two cases, we note that
9940 CONTEXT of case 1 remains dependent type after tsubst while
9941 this isn't true for case 2. */
9942 ++processing_template_decl;
9943 dependent_p = dependent_type_p (context);
9944 --processing_template_decl;
9946 if (!dependent_p
9947 && !complete_type_or_else (context, NULL_TREE))
9948 return error_mark_node;
9950 if (COMPLETE_TYPE_P (context))
9952 tree fn = new_friend;
9953 /* do_friend adds the TEMPLATE_DECL for any member friend
9954 template even if it isn't a member template, i.e.
9955 template <class T> friend A<T>::f();
9956 Look through it in that case. */
9957 if (TREE_CODE (fn) == TEMPLATE_DECL
9958 && !PRIMARY_TEMPLATE_P (fn))
9959 fn = DECL_TEMPLATE_RESULT (fn);
9960 /* Check to see that the declaration is really present, and,
9961 possibly obtain an improved declaration. */
9962 fn = check_classfn (context, fn, NULL_TREE);
9964 if (fn)
9965 new_friend = fn;
9969 return new_friend;
9972 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9973 template arguments, as for tsubst.
9975 Returns an appropriate tsubst'd friend type or error_mark_node on
9976 failure. */
9978 static tree
9979 tsubst_friend_class (tree friend_tmpl, tree args)
9981 tree friend_type;
9982 tree tmpl;
9983 tree context;
9985 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9987 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9988 return TREE_TYPE (t);
9991 context = CP_DECL_CONTEXT (friend_tmpl);
9993 if (context != global_namespace)
9995 if (TREE_CODE (context) == NAMESPACE_DECL)
9996 push_nested_namespace (context);
9997 else
9998 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
10001 /* Look for a class template declaration. We look for hidden names
10002 because two friend declarations of the same template are the
10003 same. For example, in:
10005 struct A {
10006 template <typename> friend class F;
10008 template <typename> struct B {
10009 template <typename> friend class F;
10012 both F templates are the same. */
10013 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10014 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10016 /* But, if we don't find one, it might be because we're in a
10017 situation like this:
10019 template <class T>
10020 struct S {
10021 template <class U>
10022 friend struct S;
10025 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10026 for `S<int>', not the TEMPLATE_DECL. */
10027 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10029 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10030 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10033 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10035 /* The friend template has already been declared. Just
10036 check to see that the declarations match, and install any new
10037 default parameters. We must tsubst the default parameters,
10038 of course. We only need the innermost template parameters
10039 because that is all that redeclare_class_template will look
10040 at. */
10041 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10042 > TMPL_ARGS_DEPTH (args))
10044 tree parms;
10045 location_t saved_input_location;
10046 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10047 args, tf_warning_or_error);
10049 saved_input_location = input_location;
10050 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10051 tree cons = get_constraints (tmpl);
10052 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10053 input_location = saved_input_location;
10057 friend_type = TREE_TYPE (tmpl);
10059 else
10061 /* The friend template has not already been declared. In this
10062 case, the instantiation of the template class will cause the
10063 injection of this template into the global scope. */
10064 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10065 if (tmpl == error_mark_node)
10066 return error_mark_node;
10068 /* The new TMPL is not an instantiation of anything, so we
10069 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10070 the new type because that is supposed to be the corresponding
10071 template decl, i.e., TMPL. */
10072 DECL_USE_TEMPLATE (tmpl) = 0;
10073 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10074 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10075 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10076 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10078 /* Inject this template into the global scope. */
10079 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10082 if (context != global_namespace)
10084 if (TREE_CODE (context) == NAMESPACE_DECL)
10085 pop_nested_namespace (context);
10086 else
10087 pop_nested_class ();
10090 return friend_type;
10093 /* Returns zero if TYPE cannot be completed later due to circularity.
10094 Otherwise returns one. */
10096 static int
10097 can_complete_type_without_circularity (tree type)
10099 if (type == NULL_TREE || type == error_mark_node)
10100 return 0;
10101 else if (COMPLETE_TYPE_P (type))
10102 return 1;
10103 else if (TREE_CODE (type) == ARRAY_TYPE)
10104 return can_complete_type_without_circularity (TREE_TYPE (type));
10105 else if (CLASS_TYPE_P (type)
10106 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10107 return 0;
10108 else
10109 return 1;
10112 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10113 tsubst_flags_t, tree);
10115 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10116 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10118 static tree
10119 tsubst_attribute (tree t, tree *decl_p, tree args,
10120 tsubst_flags_t complain, tree in_decl)
10122 gcc_assert (ATTR_IS_DEPENDENT (t));
10124 tree val = TREE_VALUE (t);
10125 if (val == NULL_TREE)
10126 /* Nothing to do. */;
10127 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10128 && is_attribute_p ("omp declare simd",
10129 get_attribute_name (t)))
10131 tree clauses = TREE_VALUE (val);
10132 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10133 complain, in_decl);
10134 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10135 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10136 tree parms = DECL_ARGUMENTS (*decl_p);
10137 clauses
10138 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10139 if (clauses)
10140 val = build_tree_list (NULL_TREE, clauses);
10141 else
10142 val = NULL_TREE;
10144 /* If the first attribute argument is an identifier, don't
10145 pass it through tsubst. Attributes like mode, format,
10146 cleanup and several target specific attributes expect it
10147 unmodified. */
10148 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10150 tree chain
10151 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10152 /*integral_constant_expression_p=*/false);
10153 if (chain != TREE_CHAIN (val))
10154 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10156 else if (PACK_EXPANSION_P (val))
10158 /* An attribute pack expansion. */
10159 tree purp = TREE_PURPOSE (t);
10160 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10161 if (pack == error_mark_node)
10162 return error_mark_node;
10163 int len = TREE_VEC_LENGTH (pack);
10164 tree list = NULL_TREE;
10165 tree *q = &list;
10166 for (int i = 0; i < len; ++i)
10168 tree elt = TREE_VEC_ELT (pack, i);
10169 *q = build_tree_list (purp, elt);
10170 q = &TREE_CHAIN (*q);
10172 return list;
10174 else
10175 val = tsubst_expr (val, args, complain, in_decl,
10176 /*integral_constant_expression_p=*/false);
10178 if (val != TREE_VALUE (t))
10179 return build_tree_list (TREE_PURPOSE (t), val);
10180 return t;
10183 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10184 unchanged or a new TREE_LIST chain. */
10186 static tree
10187 tsubst_attributes (tree attributes, tree args,
10188 tsubst_flags_t complain, tree in_decl)
10190 tree last_dep = NULL_TREE;
10192 for (tree t = attributes; t; t = TREE_CHAIN (t))
10193 if (ATTR_IS_DEPENDENT (t))
10195 last_dep = t;
10196 attributes = copy_list (attributes);
10197 break;
10200 if (last_dep)
10201 for (tree *p = &attributes; *p; )
10203 tree t = *p;
10204 if (ATTR_IS_DEPENDENT (t))
10206 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10207 if (subst != t)
10209 *p = subst;
10211 p = &TREE_CHAIN (*p);
10212 while (*p);
10213 *p = TREE_CHAIN (t);
10214 continue;
10217 p = &TREE_CHAIN (*p);
10220 return attributes;
10223 /* Apply any attributes which had to be deferred until instantiation
10224 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10225 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10227 static void
10228 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10229 tree args, tsubst_flags_t complain, tree in_decl)
10231 tree last_dep = NULL_TREE;
10232 tree t;
10233 tree *p;
10235 if (attributes == NULL_TREE)
10236 return;
10238 if (DECL_P (*decl_p))
10240 if (TREE_TYPE (*decl_p) == error_mark_node)
10241 return;
10242 p = &DECL_ATTRIBUTES (*decl_p);
10243 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10244 to our attributes parameter. */
10245 gcc_assert (*p == attributes);
10247 else
10249 p = &TYPE_ATTRIBUTES (*decl_p);
10250 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10251 lookup_template_class_1, and should be preserved. */
10252 gcc_assert (*p != attributes);
10253 while (*p)
10254 p = &TREE_CHAIN (*p);
10257 for (t = attributes; t; t = TREE_CHAIN (t))
10258 if (ATTR_IS_DEPENDENT (t))
10260 last_dep = t;
10261 attributes = copy_list (attributes);
10262 break;
10265 *p = attributes;
10266 if (last_dep)
10268 tree late_attrs = NULL_TREE;
10269 tree *q = &late_attrs;
10271 for (; *p; )
10273 t = *p;
10274 if (ATTR_IS_DEPENDENT (t))
10276 *p = TREE_CHAIN (t);
10277 TREE_CHAIN (t) = NULL_TREE;
10278 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10280 q = &TREE_CHAIN (*q);
10281 while (*q);
10283 else
10284 p = &TREE_CHAIN (t);
10287 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10291 /* Perform (or defer) access check for typedefs that were referenced
10292 from within the template TMPL code.
10293 This is a subroutine of instantiate_decl and instantiate_class_template.
10294 TMPL is the template to consider and TARGS is the list of arguments of
10295 that template. */
10297 static void
10298 perform_typedefs_access_check (tree tmpl, tree targs)
10300 location_t saved_location;
10301 unsigned i;
10302 qualified_typedef_usage_t *iter;
10304 if (!tmpl
10305 || (!CLASS_TYPE_P (tmpl)
10306 && TREE_CODE (tmpl) != FUNCTION_DECL))
10307 return;
10309 saved_location = input_location;
10310 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10312 tree type_decl = iter->typedef_decl;
10313 tree type_scope = iter->context;
10315 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10316 continue;
10318 if (uses_template_parms (type_decl))
10319 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10320 if (uses_template_parms (type_scope))
10321 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10323 /* Make access check error messages point to the location
10324 of the use of the typedef. */
10325 input_location = iter->locus;
10326 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10327 type_decl, type_decl,
10328 tf_warning_or_error);
10330 input_location = saved_location;
10333 static tree
10334 instantiate_class_template_1 (tree type)
10336 tree templ, args, pattern, t, member;
10337 tree typedecl;
10338 tree pbinfo;
10339 tree base_list;
10340 unsigned int saved_maximum_field_alignment;
10341 tree fn_context;
10343 if (type == error_mark_node)
10344 return error_mark_node;
10346 if (COMPLETE_OR_OPEN_TYPE_P (type)
10347 || uses_template_parms (type))
10348 return type;
10350 /* Figure out which template is being instantiated. */
10351 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10352 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10354 /* Determine what specialization of the original template to
10355 instantiate. */
10356 t = most_specialized_partial_spec (type, tf_warning_or_error);
10357 if (t == error_mark_node)
10359 TYPE_BEING_DEFINED (type) = 1;
10360 return error_mark_node;
10362 else if (t)
10364 /* This TYPE is actually an instantiation of a partial
10365 specialization. We replace the innermost set of ARGS with
10366 the arguments appropriate for substitution. For example,
10367 given:
10369 template <class T> struct S {};
10370 template <class T> struct S<T*> {};
10372 and supposing that we are instantiating S<int*>, ARGS will
10373 presently be {int*} -- but we need {int}. */
10374 pattern = TREE_TYPE (t);
10375 args = TREE_PURPOSE (t);
10377 else
10379 pattern = TREE_TYPE (templ);
10380 args = CLASSTYPE_TI_ARGS (type);
10383 /* If the template we're instantiating is incomplete, then clearly
10384 there's nothing we can do. */
10385 if (!COMPLETE_TYPE_P (pattern))
10386 return type;
10388 /* If we've recursively instantiated too many templates, stop. */
10389 if (! push_tinst_level (type))
10390 return type;
10392 /* Now we're really doing the instantiation. Mark the type as in
10393 the process of being defined. */
10394 TYPE_BEING_DEFINED (type) = 1;
10396 /* We may be in the middle of deferred access check. Disable
10397 it now. */
10398 push_deferring_access_checks (dk_no_deferred);
10400 int saved_unevaluated_operand = cp_unevaluated_operand;
10401 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10403 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10404 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10405 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10406 fn_context = error_mark_node;
10407 if (!fn_context)
10408 push_to_top_level ();
10409 else
10411 cp_unevaluated_operand = 0;
10412 c_inhibit_evaluation_warnings = 0;
10414 /* Use #pragma pack from the template context. */
10415 saved_maximum_field_alignment = maximum_field_alignment;
10416 maximum_field_alignment = TYPE_PRECISION (pattern);
10418 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10420 /* Set the input location to the most specialized template definition.
10421 This is needed if tsubsting causes an error. */
10422 typedecl = TYPE_MAIN_DECL (pattern);
10423 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10424 DECL_SOURCE_LOCATION (typedecl);
10426 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10427 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10428 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10429 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10430 if (ANON_AGGR_TYPE_P (pattern))
10431 SET_ANON_AGGR_TYPE_P (type);
10432 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10434 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10435 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10436 /* Adjust visibility for template arguments. */
10437 determine_visibility (TYPE_MAIN_DECL (type));
10439 if (CLASS_TYPE_P (type))
10440 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10442 pbinfo = TYPE_BINFO (pattern);
10444 /* We should never instantiate a nested class before its enclosing
10445 class; we need to look up the nested class by name before we can
10446 instantiate it, and that lookup should instantiate the enclosing
10447 class. */
10448 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10449 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10451 base_list = NULL_TREE;
10452 if (BINFO_N_BASE_BINFOS (pbinfo))
10454 tree pbase_binfo;
10455 tree pushed_scope;
10456 int i;
10458 /* We must enter the scope containing the type, as that is where
10459 the accessibility of types named in dependent bases are
10460 looked up from. */
10461 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10463 /* Substitute into each of the bases to determine the actual
10464 basetypes. */
10465 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10467 tree base;
10468 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10469 tree expanded_bases = NULL_TREE;
10470 int idx, len = 1;
10472 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10474 expanded_bases =
10475 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10476 args, tf_error, NULL_TREE);
10477 if (expanded_bases == error_mark_node)
10478 continue;
10480 len = TREE_VEC_LENGTH (expanded_bases);
10483 for (idx = 0; idx < len; idx++)
10485 if (expanded_bases)
10486 /* Extract the already-expanded base class. */
10487 base = TREE_VEC_ELT (expanded_bases, idx);
10488 else
10489 /* Substitute to figure out the base class. */
10490 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10491 NULL_TREE);
10493 if (base == error_mark_node)
10494 continue;
10496 base_list = tree_cons (access, base, base_list);
10497 if (BINFO_VIRTUAL_P (pbase_binfo))
10498 TREE_TYPE (base_list) = integer_type_node;
10502 /* The list is now in reverse order; correct that. */
10503 base_list = nreverse (base_list);
10505 if (pushed_scope)
10506 pop_scope (pushed_scope);
10508 /* Now call xref_basetypes to set up all the base-class
10509 information. */
10510 xref_basetypes (type, base_list);
10512 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10513 (int) ATTR_FLAG_TYPE_IN_PLACE,
10514 args, tf_error, NULL_TREE);
10515 fixup_attribute_variants (type);
10517 /* Now that our base classes are set up, enter the scope of the
10518 class, so that name lookups into base classes, etc. will work
10519 correctly. This is precisely analogous to what we do in
10520 begin_class_definition when defining an ordinary non-template
10521 class, except we also need to push the enclosing classes. */
10522 push_nested_class (type);
10524 /* Now members are processed in the order of declaration. */
10525 for (member = CLASSTYPE_DECL_LIST (pattern);
10526 member; member = TREE_CHAIN (member))
10528 tree t = TREE_VALUE (member);
10530 if (TREE_PURPOSE (member))
10532 if (TYPE_P (t))
10534 /* Build new CLASSTYPE_NESTED_UTDS. */
10536 tree newtag;
10537 bool class_template_p;
10539 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10540 && TYPE_LANG_SPECIFIC (t)
10541 && CLASSTYPE_IS_TEMPLATE (t));
10542 /* If the member is a class template, then -- even after
10543 substitution -- there may be dependent types in the
10544 template argument list for the class. We increment
10545 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10546 that function will assume that no types are dependent
10547 when outside of a template. */
10548 if (class_template_p)
10549 ++processing_template_decl;
10550 newtag = tsubst (t, args, tf_error, NULL_TREE);
10551 if (class_template_p)
10552 --processing_template_decl;
10553 if (newtag == error_mark_node)
10554 continue;
10556 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10558 tree name = TYPE_IDENTIFIER (t);
10560 if (class_template_p)
10561 /* Unfortunately, lookup_template_class sets
10562 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10563 instantiation (i.e., for the type of a member
10564 template class nested within a template class.)
10565 This behavior is required for
10566 maybe_process_partial_specialization to work
10567 correctly, but is not accurate in this case;
10568 the TAG is not an instantiation of anything.
10569 (The corresponding TEMPLATE_DECL is an
10570 instantiation, but the TYPE is not.) */
10571 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10573 /* Now, we call pushtag to put this NEWTAG into the scope of
10574 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10575 pushtag calling push_template_decl. We don't have to do
10576 this for enums because it will already have been done in
10577 tsubst_enum. */
10578 if (name)
10579 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10580 pushtag (name, newtag, /*tag_scope=*/ts_current);
10583 else if (DECL_DECLARES_FUNCTION_P (t))
10585 tree r;
10587 if (TREE_CODE (t) == TEMPLATE_DECL)
10588 ++processing_template_decl;
10589 r = tsubst (t, args, tf_error, NULL_TREE);
10590 if (TREE_CODE (t) == TEMPLATE_DECL)
10591 --processing_template_decl;
10592 set_current_access_from_decl (r);
10593 finish_member_declaration (r);
10594 /* Instantiate members marked with attribute used. */
10595 if (r != error_mark_node && DECL_PRESERVE_P (r))
10596 mark_used (r);
10597 if (TREE_CODE (r) == FUNCTION_DECL
10598 && DECL_OMP_DECLARE_REDUCTION_P (r))
10599 cp_check_omp_declare_reduction (r);
10601 else if (DECL_CLASS_TEMPLATE_P (t)
10602 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10603 /* A closure type for a lambda in a default argument for a
10604 member template. Ignore it; it will be instantiated with
10605 the default argument. */;
10606 else
10608 /* Build new TYPE_FIELDS. */
10609 if (TREE_CODE (t) == STATIC_ASSERT)
10611 tree condition;
10613 ++c_inhibit_evaluation_warnings;
10614 condition =
10615 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10616 tf_warning_or_error, NULL_TREE,
10617 /*integral_constant_expression_p=*/true);
10618 --c_inhibit_evaluation_warnings;
10620 finish_static_assert (condition,
10621 STATIC_ASSERT_MESSAGE (t),
10622 STATIC_ASSERT_SOURCE_LOCATION (t),
10623 /*member_p=*/true);
10625 else if (TREE_CODE (t) != CONST_DECL)
10627 tree r;
10628 tree vec = NULL_TREE;
10629 int len = 1;
10631 /* The file and line for this declaration, to
10632 assist in error message reporting. Since we
10633 called push_tinst_level above, we don't need to
10634 restore these. */
10635 input_location = DECL_SOURCE_LOCATION (t);
10637 if (TREE_CODE (t) == TEMPLATE_DECL)
10638 ++processing_template_decl;
10639 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10640 if (TREE_CODE (t) == TEMPLATE_DECL)
10641 --processing_template_decl;
10643 if (TREE_CODE (r) == TREE_VEC)
10645 /* A capture pack became multiple fields. */
10646 vec = r;
10647 len = TREE_VEC_LENGTH (vec);
10650 for (int i = 0; i < len; ++i)
10652 if (vec)
10653 r = TREE_VEC_ELT (vec, i);
10654 if (VAR_P (r))
10656 /* In [temp.inst]:
10658 [t]he initialization (and any associated
10659 side-effects) of a static data member does
10660 not occur unless the static data member is
10661 itself used in a way that requires the
10662 definition of the static data member to
10663 exist.
10665 Therefore, we do not substitute into the
10666 initialized for the static data member here. */
10667 finish_static_data_member_decl
10669 /*init=*/NULL_TREE,
10670 /*init_const_expr_p=*/false,
10671 /*asmspec_tree=*/NULL_TREE,
10672 /*flags=*/0);
10673 /* Instantiate members marked with attribute used. */
10674 if (r != error_mark_node && DECL_PRESERVE_P (r))
10675 mark_used (r);
10677 else if (TREE_CODE (r) == FIELD_DECL)
10679 /* Determine whether R has a valid type and can be
10680 completed later. If R is invalid, then its type
10681 is replaced by error_mark_node. */
10682 tree rtype = TREE_TYPE (r);
10683 if (can_complete_type_without_circularity (rtype))
10684 complete_type (rtype);
10686 if (!complete_or_array_type_p (rtype))
10688 /* If R's type couldn't be completed and
10689 it isn't a flexible array member (whose
10690 type is incomplete by definition) give
10691 an error. */
10692 cxx_incomplete_type_error (r, rtype);
10693 TREE_TYPE (r) = error_mark_node;
10697 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10698 such a thing will already have been added to the field
10699 list by tsubst_enum in finish_member_declaration in the
10700 CLASSTYPE_NESTED_UTDS case above. */
10701 if (!(TREE_CODE (r) == TYPE_DECL
10702 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10703 && DECL_ARTIFICIAL (r)))
10705 set_current_access_from_decl (r);
10706 finish_member_declaration (r);
10712 else
10714 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10715 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10717 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10719 tree friend_type = t;
10720 bool adjust_processing_template_decl = false;
10722 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10724 /* template <class T> friend class C; */
10725 friend_type = tsubst_friend_class (friend_type, args);
10726 adjust_processing_template_decl = true;
10728 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10730 /* template <class T> friend class C::D; */
10731 friend_type = tsubst (friend_type, args,
10732 tf_warning_or_error, NULL_TREE);
10733 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10734 friend_type = TREE_TYPE (friend_type);
10735 adjust_processing_template_decl = true;
10737 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10738 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10740 /* This could be either
10742 friend class T::C;
10744 when dependent_type_p is false or
10746 template <class U> friend class T::C;
10748 otherwise. */
10749 /* Bump processing_template_decl in case this is something like
10750 template <class T> friend struct A<T>::B. */
10751 ++processing_template_decl;
10752 friend_type = tsubst (friend_type, args,
10753 tf_warning_or_error, NULL_TREE);
10754 if (dependent_type_p (friend_type))
10755 adjust_processing_template_decl = true;
10756 --processing_template_decl;
10758 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10759 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10760 && TYPE_HIDDEN_P (friend_type))
10762 /* friend class C;
10764 where C hasn't been declared yet. Let's lookup name
10765 from namespace scope directly, bypassing any name that
10766 come from dependent base class. */
10767 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10769 /* The call to xref_tag_from_type does injection for friend
10770 classes. */
10771 push_nested_namespace (ns);
10772 friend_type =
10773 xref_tag_from_type (friend_type, NULL_TREE,
10774 /*tag_scope=*/ts_current);
10775 pop_nested_namespace (ns);
10777 else if (uses_template_parms (friend_type))
10778 /* friend class C<T>; */
10779 friend_type = tsubst (friend_type, args,
10780 tf_warning_or_error, NULL_TREE);
10781 /* Otherwise it's
10783 friend class C;
10785 where C is already declared or
10787 friend class C<int>;
10789 We don't have to do anything in these cases. */
10791 if (adjust_processing_template_decl)
10792 /* Trick make_friend_class into realizing that the friend
10793 we're adding is a template, not an ordinary class. It's
10794 important that we use make_friend_class since it will
10795 perform some error-checking and output cross-reference
10796 information. */
10797 ++processing_template_decl;
10799 if (friend_type != error_mark_node)
10800 make_friend_class (type, friend_type, /*complain=*/false);
10802 if (adjust_processing_template_decl)
10803 --processing_template_decl;
10805 else
10807 /* Build new DECL_FRIENDLIST. */
10808 tree r;
10810 /* The file and line for this declaration, to
10811 assist in error message reporting. Since we
10812 called push_tinst_level above, we don't need to
10813 restore these. */
10814 input_location = DECL_SOURCE_LOCATION (t);
10816 if (TREE_CODE (t) == TEMPLATE_DECL)
10818 ++processing_template_decl;
10819 push_deferring_access_checks (dk_no_check);
10822 r = tsubst_friend_function (t, args);
10823 add_friend (type, r, /*complain=*/false);
10824 if (TREE_CODE (t) == TEMPLATE_DECL)
10826 pop_deferring_access_checks ();
10827 --processing_template_decl;
10833 if (fn_context)
10835 /* Restore these before substituting into the lambda capture
10836 initializers. */
10837 cp_unevaluated_operand = saved_unevaluated_operand;
10838 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10841 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10843 tree decl = lambda_function (type);
10844 if (decl)
10846 if (cxx_dialect >= cxx17)
10847 CLASSTYPE_LITERAL_P (type) = true;
10849 if (!DECL_TEMPLATE_INFO (decl)
10850 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10852 /* Set function_depth to avoid garbage collection. */
10853 ++function_depth;
10854 instantiate_decl (decl, /*defer_ok=*/false, false);
10855 --function_depth;
10858 /* We need to instantiate the capture list from the template
10859 after we've instantiated the closure members, but before we
10860 consider adding the conversion op. Also keep any captures
10861 that may have been added during instantiation of the op(). */
10862 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10863 tree tmpl_cap
10864 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10865 args, tf_warning_or_error, NULL_TREE,
10866 false, false);
10868 LAMBDA_EXPR_CAPTURE_LIST (expr)
10869 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10871 maybe_add_lambda_conv_op (type);
10873 else
10874 gcc_assert (errorcount);
10877 /* Set the file and line number information to whatever is given for
10878 the class itself. This puts error messages involving generated
10879 implicit functions at a predictable point, and the same point
10880 that would be used for non-template classes. */
10881 input_location = DECL_SOURCE_LOCATION (typedecl);
10883 unreverse_member_declarations (type);
10884 finish_struct_1 (type);
10885 TYPE_BEING_DEFINED (type) = 0;
10887 /* We don't instantiate default arguments for member functions. 14.7.1:
10889 The implicit instantiation of a class template specialization causes
10890 the implicit instantiation of the declarations, but not of the
10891 definitions or default arguments, of the class member functions,
10892 member classes, static data members and member templates.... */
10894 /* Some typedefs referenced from within the template code need to be access
10895 checked at template instantiation time, i.e now. These types were
10896 added to the template at parsing time. Let's get those and perform
10897 the access checks then. */
10898 perform_typedefs_access_check (pattern, args);
10899 perform_deferred_access_checks (tf_warning_or_error);
10900 pop_nested_class ();
10901 maximum_field_alignment = saved_maximum_field_alignment;
10902 if (!fn_context)
10903 pop_from_top_level ();
10904 pop_deferring_access_checks ();
10905 pop_tinst_level ();
10907 /* The vtable for a template class can be emitted in any translation
10908 unit in which the class is instantiated. When there is no key
10909 method, however, finish_struct_1 will already have added TYPE to
10910 the keyed_classes. */
10911 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10912 vec_safe_push (keyed_classes, type);
10914 return type;
10917 /* Wrapper for instantiate_class_template_1. */
10919 tree
10920 instantiate_class_template (tree type)
10922 tree ret;
10923 timevar_push (TV_TEMPLATE_INST);
10924 ret = instantiate_class_template_1 (type);
10925 timevar_pop (TV_TEMPLATE_INST);
10926 return ret;
10929 static tree
10930 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10932 tree r;
10934 if (!t)
10935 r = t;
10936 else if (TYPE_P (t))
10937 r = tsubst (t, args, complain, in_decl);
10938 else
10940 if (!(complain & tf_warning))
10941 ++c_inhibit_evaluation_warnings;
10942 r = tsubst_expr (t, args, complain, in_decl,
10943 /*integral_constant_expression_p=*/true);
10944 if (!(complain & tf_warning))
10945 --c_inhibit_evaluation_warnings;
10947 return r;
10950 /* Given a function parameter pack TMPL_PARM and some function parameters
10951 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10952 and set *SPEC_P to point at the next point in the list. */
10954 tree
10955 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10957 /* Collect all of the extra "packed" parameters into an
10958 argument pack. */
10959 tree parmvec;
10960 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10961 tree spec_parm = *spec_p;
10962 int i, len;
10964 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10965 if (tmpl_parm
10966 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10967 break;
10969 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10970 parmvec = make_tree_vec (len);
10971 spec_parm = *spec_p;
10972 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10973 TREE_VEC_ELT (parmvec, i) = spec_parm;
10975 /* Build the argument packs. */
10976 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10977 *spec_p = spec_parm;
10979 return argpack;
10982 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10983 NONTYPE_ARGUMENT_PACK. */
10985 static tree
10986 make_fnparm_pack (tree spec_parm)
10988 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10991 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10992 pack expansion with no extra args, 2 if it has extra args, or 0
10993 if it is not a pack expansion. */
10995 static int
10996 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10998 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10999 if (i >= TREE_VEC_LENGTH (vec))
11000 return 0;
11001 tree elt = TREE_VEC_ELT (vec, i);
11002 if (DECL_P (elt))
11003 /* A decl pack is itself an expansion. */
11004 elt = TREE_TYPE (elt);
11005 if (!PACK_EXPANSION_P (elt))
11006 return 0;
11007 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11008 return 2;
11009 return 1;
11013 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11015 static tree
11016 make_argument_pack_select (tree arg_pack, unsigned index)
11018 tree aps = make_node (ARGUMENT_PACK_SELECT);
11020 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11021 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11023 return aps;
11026 /* This is a subroutine of tsubst_pack_expansion.
11028 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11029 mechanism to store the (non complete list of) arguments of the
11030 substitution and return a non substituted pack expansion, in order
11031 to wait for when we have enough arguments to really perform the
11032 substitution. */
11034 static bool
11035 use_pack_expansion_extra_args_p (tree parm_packs,
11036 int arg_pack_len,
11037 bool has_empty_arg)
11039 /* If one pack has an expansion and another pack has a normal
11040 argument or if one pack has an empty argument and an another
11041 one hasn't then tsubst_pack_expansion cannot perform the
11042 substitution and need to fall back on the
11043 PACK_EXPANSION_EXTRA mechanism. */
11044 if (parm_packs == NULL_TREE)
11045 return false;
11046 else if (has_empty_arg)
11047 return true;
11049 bool has_expansion_arg = false;
11050 for (int i = 0 ; i < arg_pack_len; ++i)
11052 bool has_non_expansion_arg = false;
11053 for (tree parm_pack = parm_packs;
11054 parm_pack;
11055 parm_pack = TREE_CHAIN (parm_pack))
11057 tree arg = TREE_VALUE (parm_pack);
11059 int exp = argument_pack_element_is_expansion_p (arg, i);
11060 if (exp == 2)
11061 /* We can't substitute a pack expansion with extra args into
11062 our pattern. */
11063 return true;
11064 else if (exp)
11065 has_expansion_arg = true;
11066 else
11067 has_non_expansion_arg = true;
11070 if (has_expansion_arg && has_non_expansion_arg)
11071 return true;
11073 return false;
11076 /* [temp.variadic]/6 says that:
11078 The instantiation of a pack expansion [...]
11079 produces a list E1,E2, ..., En, where N is the number of elements
11080 in the pack expansion parameters.
11082 This subroutine of tsubst_pack_expansion produces one of these Ei.
11084 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11085 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11086 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11087 INDEX is the index 'i' of the element Ei to produce. ARGS,
11088 COMPLAIN, and IN_DECL are the same parameters as for the
11089 tsubst_pack_expansion function.
11091 The function returns the resulting Ei upon successful completion,
11092 or error_mark_node.
11094 Note that this function possibly modifies the ARGS parameter, so
11095 it's the responsibility of the caller to restore it. */
11097 static tree
11098 gen_elem_of_pack_expansion_instantiation (tree pattern,
11099 tree parm_packs,
11100 unsigned index,
11101 tree args /* This parm gets
11102 modified. */,
11103 tsubst_flags_t complain,
11104 tree in_decl)
11106 tree t;
11107 bool ith_elem_is_expansion = false;
11109 /* For each parameter pack, change the substitution of the parameter
11110 pack to the ith argument in its argument pack, then expand the
11111 pattern. */
11112 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11114 tree parm = TREE_PURPOSE (pack);
11115 tree arg_pack = TREE_VALUE (pack);
11116 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11118 ith_elem_is_expansion |=
11119 argument_pack_element_is_expansion_p (arg_pack, index);
11121 /* Select the Ith argument from the pack. */
11122 if (TREE_CODE (parm) == PARM_DECL
11123 || TREE_CODE (parm) == FIELD_DECL)
11125 if (index == 0)
11127 aps = make_argument_pack_select (arg_pack, index);
11128 if (!mark_used (parm, complain) && !(complain & tf_error))
11129 return error_mark_node;
11130 register_local_specialization (aps, parm);
11132 else
11133 aps = retrieve_local_specialization (parm);
11135 else
11137 int idx, level;
11138 template_parm_level_and_index (parm, &level, &idx);
11140 if (index == 0)
11142 aps = make_argument_pack_select (arg_pack, index);
11143 /* Update the corresponding argument. */
11144 TMPL_ARG (args, level, idx) = aps;
11146 else
11147 /* Re-use the ARGUMENT_PACK_SELECT. */
11148 aps = TMPL_ARG (args, level, idx);
11150 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11153 /* Substitute into the PATTERN with the (possibly altered)
11154 arguments. */
11155 if (pattern == in_decl)
11156 /* Expanding a fixed parameter pack from
11157 coerce_template_parameter_pack. */
11158 t = tsubst_decl (pattern, args, complain);
11159 else if (pattern == error_mark_node)
11160 t = error_mark_node;
11161 else if (constraint_p (pattern))
11163 if (processing_template_decl)
11164 t = tsubst_constraint (pattern, args, complain, in_decl);
11165 else
11166 t = (constraints_satisfied_p (pattern, args)
11167 ? boolean_true_node : boolean_false_node);
11169 else if (!TYPE_P (pattern))
11170 t = tsubst_expr (pattern, args, complain, in_decl,
11171 /*integral_constant_expression_p=*/false);
11172 else
11173 t = tsubst (pattern, args, complain, in_decl);
11175 /* If the Ith argument pack element is a pack expansion, then
11176 the Ith element resulting from the substituting is going to
11177 be a pack expansion as well. */
11178 if (ith_elem_is_expansion)
11179 t = make_pack_expansion (t);
11181 return t;
11184 /* When the unexpanded parameter pack in a fold expression expands to an empty
11185 sequence, the value of the expression is as follows; the program is
11186 ill-formed if the operator is not listed in this table.
11188 && true
11189 || false
11190 , void() */
11192 tree
11193 expand_empty_fold (tree t, tsubst_flags_t complain)
11195 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11196 if (!FOLD_EXPR_MODIFY_P (t))
11197 switch (code)
11199 case TRUTH_ANDIF_EXPR:
11200 return boolean_true_node;
11201 case TRUTH_ORIF_EXPR:
11202 return boolean_false_node;
11203 case COMPOUND_EXPR:
11204 return void_node;
11205 default:
11206 break;
11209 if (complain & tf_error)
11210 error_at (location_of (t),
11211 "fold of empty expansion over %O", code);
11212 return error_mark_node;
11215 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11216 form an expression that combines the two terms using the
11217 operator of T. */
11219 static tree
11220 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11222 tree op = FOLD_EXPR_OP (t);
11223 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11225 // Handle compound assignment operators.
11226 if (FOLD_EXPR_MODIFY_P (t))
11227 return build_x_modify_expr (input_location, left, code, right, complain);
11229 switch (code)
11231 case COMPOUND_EXPR:
11232 return build_x_compound_expr (input_location, left, right, complain);
11233 case DOTSTAR_EXPR:
11234 return build_m_component_ref (left, right, complain);
11235 default:
11236 return build_x_binary_op (input_location, code,
11237 left, TREE_CODE (left),
11238 right, TREE_CODE (right),
11239 /*overload=*/NULL,
11240 complain);
11244 /* Substitute ARGS into the pack of a fold expression T. */
11246 static inline tree
11247 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11249 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11252 /* Substitute ARGS into the pack of a fold expression T. */
11254 static inline tree
11255 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11257 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11260 /* Expand a PACK of arguments into a grouped as left fold.
11261 Given a pack containing elements A0, A1, ..., An and an
11262 operator @, this builds the expression:
11264 ((A0 @ A1) @ A2) ... @ An
11266 Note that PACK must not be empty.
11268 The operator is defined by the original fold expression T. */
11270 static tree
11271 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11273 tree left = TREE_VEC_ELT (pack, 0);
11274 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11276 tree right = TREE_VEC_ELT (pack, i);
11277 left = fold_expression (t, left, right, complain);
11279 return left;
11282 /* Substitute into a unary left fold expression. */
11284 static tree
11285 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11286 tree in_decl)
11288 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11289 if (pack == error_mark_node)
11290 return error_mark_node;
11291 if (PACK_EXPANSION_P (pack))
11293 tree r = copy_node (t);
11294 FOLD_EXPR_PACK (r) = pack;
11295 return r;
11297 if (TREE_VEC_LENGTH (pack) == 0)
11298 return expand_empty_fold (t, complain);
11299 else
11300 return expand_left_fold (t, pack, complain);
11303 /* Substitute into a binary left fold expression.
11305 Do ths by building a single (non-empty) vector of argumnts and
11306 building the expression from those elements. */
11308 static tree
11309 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11310 tree in_decl)
11312 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11313 if (pack == error_mark_node)
11314 return error_mark_node;
11315 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11316 if (init == error_mark_node)
11317 return error_mark_node;
11319 if (PACK_EXPANSION_P (pack))
11321 tree r = copy_node (t);
11322 FOLD_EXPR_PACK (r) = pack;
11323 FOLD_EXPR_INIT (r) = init;
11324 return r;
11327 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11328 TREE_VEC_ELT (vec, 0) = init;
11329 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11330 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11332 return expand_left_fold (t, vec, complain);
11335 /* Expand a PACK of arguments into a grouped as right fold.
11336 Given a pack containing elementns A0, A1, ..., and an
11337 operator @, this builds the expression:
11339 A0@ ... (An-2 @ (An-1 @ An))
11341 Note that PACK must not be empty.
11343 The operator is defined by the original fold expression T. */
11345 tree
11346 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11348 // Build the expression.
11349 int n = TREE_VEC_LENGTH (pack);
11350 tree right = TREE_VEC_ELT (pack, n - 1);
11351 for (--n; n != 0; --n)
11353 tree left = TREE_VEC_ELT (pack, n - 1);
11354 right = fold_expression (t, left, right, complain);
11356 return right;
11359 /* Substitute into a unary right fold expression. */
11361 static tree
11362 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11363 tree in_decl)
11365 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11366 if (pack == error_mark_node)
11367 return error_mark_node;
11368 if (PACK_EXPANSION_P (pack))
11370 tree r = copy_node (t);
11371 FOLD_EXPR_PACK (r) = pack;
11372 return r;
11374 if (TREE_VEC_LENGTH (pack) == 0)
11375 return expand_empty_fold (t, complain);
11376 else
11377 return expand_right_fold (t, pack, complain);
11380 /* Substitute into a binary right fold expression.
11382 Do ths by building a single (non-empty) vector of arguments and
11383 building the expression from those elements. */
11385 static tree
11386 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11387 tree in_decl)
11389 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11390 if (pack == error_mark_node)
11391 return error_mark_node;
11392 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11393 if (init == error_mark_node)
11394 return error_mark_node;
11396 if (PACK_EXPANSION_P (pack))
11398 tree r = copy_node (t);
11399 FOLD_EXPR_PACK (r) = pack;
11400 FOLD_EXPR_INIT (r) = init;
11401 return r;
11404 int n = TREE_VEC_LENGTH (pack);
11405 tree vec = make_tree_vec (n + 1);
11406 for (int i = 0; i < n; ++i)
11407 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11408 TREE_VEC_ELT (vec, n) = init;
11410 return expand_right_fold (t, vec, complain);
11414 /* Substitute ARGS into T, which is an pack expansion
11415 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11416 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11417 (if only a partial substitution could be performed) or
11418 ERROR_MARK_NODE if there was an error. */
11419 tree
11420 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11421 tree in_decl)
11423 tree pattern;
11424 tree pack, packs = NULL_TREE;
11425 bool unsubstituted_packs = false;
11426 int i, len = -1;
11427 tree result;
11428 hash_map<tree, tree> *saved_local_specializations = NULL;
11429 bool need_local_specializations = false;
11430 int levels;
11432 gcc_assert (PACK_EXPANSION_P (t));
11433 pattern = PACK_EXPANSION_PATTERN (t);
11435 /* Add in any args remembered from an earlier partial instantiation. */
11436 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11438 levels = TMPL_ARGS_DEPTH (args);
11440 /* Determine the argument packs that will instantiate the parameter
11441 packs used in the expansion expression. While we're at it,
11442 compute the number of arguments to be expanded and make sure it
11443 is consistent. */
11444 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11445 pack = TREE_CHAIN (pack))
11447 tree parm_pack = TREE_VALUE (pack);
11448 tree arg_pack = NULL_TREE;
11449 tree orig_arg = NULL_TREE;
11450 int level = 0;
11452 if (TREE_CODE (parm_pack) == BASES)
11454 gcc_assert (parm_pack == pattern);
11455 if (BASES_DIRECT (parm_pack))
11456 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11457 args, complain, in_decl, false));
11458 else
11459 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11460 args, complain, in_decl, false));
11462 else if (builtin_pack_call_p (parm_pack))
11464 /* ??? Support use in other patterns. */
11465 gcc_assert (parm_pack == pattern);
11466 return expand_builtin_pack_call (parm_pack, args,
11467 complain, in_decl);
11469 else if (TREE_CODE (parm_pack) == PARM_DECL)
11471 /* We know we have correct local_specializations if this
11472 expansion is at function scope, or if we're dealing with a
11473 local parameter in a requires expression; for the latter,
11474 tsubst_requires_expr set it up appropriately. */
11475 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11476 arg_pack = retrieve_local_specialization (parm_pack);
11477 else
11478 /* We can't rely on local_specializations for a parameter
11479 name used later in a function declaration (such as in a
11480 late-specified return type). Even if it exists, it might
11481 have the wrong value for a recursive call. */
11482 need_local_specializations = true;
11484 if (!arg_pack)
11486 /* This parameter pack was used in an unevaluated context. Just
11487 make a dummy decl, since it's only used for its type. */
11488 arg_pack = tsubst_decl (parm_pack, args, complain);
11489 if (arg_pack && DECL_PACK_P (arg_pack))
11490 /* Partial instantiation of the parm_pack, we can't build
11491 up an argument pack yet. */
11492 arg_pack = NULL_TREE;
11493 else
11494 arg_pack = make_fnparm_pack (arg_pack);
11497 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11498 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11499 else
11501 int idx;
11502 template_parm_level_and_index (parm_pack, &level, &idx);
11504 if (level <= levels)
11505 arg_pack = TMPL_ARG (args, level, idx);
11508 orig_arg = arg_pack;
11509 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11510 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11512 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11513 /* This can only happen if we forget to expand an argument
11514 pack somewhere else. Just return an error, silently. */
11516 result = make_tree_vec (1);
11517 TREE_VEC_ELT (result, 0) = error_mark_node;
11518 return result;
11521 if (arg_pack)
11523 int my_len =
11524 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11526 /* Don't bother trying to do a partial substitution with
11527 incomplete packs; we'll try again after deduction. */
11528 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11529 return t;
11531 if (len < 0)
11532 len = my_len;
11533 else if (len != my_len)
11535 if (!(complain & tf_error))
11536 /* Fail quietly. */;
11537 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11538 error ("mismatched argument pack lengths while expanding %qT",
11539 pattern);
11540 else
11541 error ("mismatched argument pack lengths while expanding %qE",
11542 pattern);
11543 return error_mark_node;
11546 /* Keep track of the parameter packs and their corresponding
11547 argument packs. */
11548 packs = tree_cons (parm_pack, arg_pack, packs);
11549 TREE_TYPE (packs) = orig_arg;
11551 else
11553 /* We can't substitute for this parameter pack. We use a flag as
11554 well as the missing_level counter because function parameter
11555 packs don't have a level. */
11556 gcc_assert (processing_template_decl);
11557 unsubstituted_packs = true;
11561 /* If the expansion is just T..., return the matching argument pack, unless
11562 we need to call convert_from_reference on all the elements. This is an
11563 important optimization; see c++/68422. */
11564 if (!unsubstituted_packs
11565 && TREE_PURPOSE (packs) == pattern)
11567 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11568 /* Types need no adjustment, nor does sizeof..., and if we still have
11569 some pack expansion args we won't do anything yet. */
11570 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11571 || PACK_EXPANSION_SIZEOF_P (t)
11572 || pack_expansion_args_count (args))
11573 return args;
11574 /* Also optimize expression pack expansions if we can tell that the
11575 elements won't have reference type. */
11576 tree type = TREE_TYPE (pattern);
11577 if (type && TREE_CODE (type) != REFERENCE_TYPE
11578 && !PACK_EXPANSION_P (type)
11579 && !WILDCARD_TYPE_P (type))
11580 return args;
11581 /* Otherwise use the normal path so we get convert_from_reference. */
11584 /* We cannot expand this expansion expression, because we don't have
11585 all of the argument packs we need. */
11586 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11588 /* We got some full packs, but we can't substitute them in until we
11589 have values for all the packs. So remember these until then. */
11591 t = make_pack_expansion (pattern);
11592 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11593 return t;
11595 else if (unsubstituted_packs)
11597 /* There were no real arguments, we're just replacing a parameter
11598 pack with another version of itself. Substitute into the
11599 pattern and return a PACK_EXPANSION_*. The caller will need to
11600 deal with that. */
11601 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11602 t = tsubst_expr (pattern, args, complain, in_decl,
11603 /*integral_constant_expression_p=*/false);
11604 else
11605 t = tsubst (pattern, args, complain, in_decl);
11606 t = make_pack_expansion (t);
11607 return t;
11610 gcc_assert (len >= 0);
11612 if (need_local_specializations)
11614 /* We're in a late-specified return type, so create our own local
11615 specializations map; the current map is either NULL or (in the
11616 case of recursive unification) might have bindings that we don't
11617 want to use or alter. */
11618 saved_local_specializations = local_specializations;
11619 local_specializations = new hash_map<tree, tree>;
11622 /* For each argument in each argument pack, substitute into the
11623 pattern. */
11624 result = make_tree_vec (len);
11625 tree elem_args = copy_template_args (args);
11626 for (i = 0; i < len; ++i)
11628 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11630 elem_args, complain,
11631 in_decl);
11632 TREE_VEC_ELT (result, i) = t;
11633 if (t == error_mark_node)
11635 result = error_mark_node;
11636 break;
11640 /* Update ARGS to restore the substitution from parameter packs to
11641 their argument packs. */
11642 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11644 tree parm = TREE_PURPOSE (pack);
11646 if (TREE_CODE (parm) == PARM_DECL
11647 || TREE_CODE (parm) == FIELD_DECL)
11648 register_local_specialization (TREE_TYPE (pack), parm);
11649 else
11651 int idx, level;
11653 if (TREE_VALUE (pack) == NULL_TREE)
11654 continue;
11656 template_parm_level_and_index (parm, &level, &idx);
11658 /* Update the corresponding argument. */
11659 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11660 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11661 TREE_TYPE (pack);
11662 else
11663 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11667 if (need_local_specializations)
11669 delete local_specializations;
11670 local_specializations = saved_local_specializations;
11673 /* If the dependent pack arguments were such that we end up with only a
11674 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11675 if (len == 1 && TREE_CODE (result) == TREE_VEC
11676 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11677 return TREE_VEC_ELT (result, 0);
11679 return result;
11682 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11683 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11684 parameter packs; all parms generated from a function parameter pack will
11685 have the same DECL_PARM_INDEX. */
11687 tree
11688 get_pattern_parm (tree parm, tree tmpl)
11690 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11691 tree patparm;
11693 if (DECL_ARTIFICIAL (parm))
11695 for (patparm = DECL_ARGUMENTS (pattern);
11696 patparm; patparm = DECL_CHAIN (patparm))
11697 if (DECL_ARTIFICIAL (patparm)
11698 && DECL_NAME (parm) == DECL_NAME (patparm))
11699 break;
11701 else
11703 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11704 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11705 gcc_assert (DECL_PARM_INDEX (patparm)
11706 == DECL_PARM_INDEX (parm));
11709 return patparm;
11712 /* Make an argument pack out of the TREE_VEC VEC. */
11714 static tree
11715 make_argument_pack (tree vec)
11717 tree pack;
11718 tree elt = TREE_VEC_ELT (vec, 0);
11719 if (TYPE_P (elt))
11720 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11721 else
11723 pack = make_node (NONTYPE_ARGUMENT_PACK);
11724 TREE_CONSTANT (pack) = 1;
11726 SET_ARGUMENT_PACK_ARGS (pack, vec);
11727 return pack;
11730 /* Return an exact copy of template args T that can be modified
11731 independently. */
11733 static tree
11734 copy_template_args (tree t)
11736 if (t == error_mark_node)
11737 return t;
11739 int len = TREE_VEC_LENGTH (t);
11740 tree new_vec = make_tree_vec (len);
11742 for (int i = 0; i < len; ++i)
11744 tree elt = TREE_VEC_ELT (t, i);
11745 if (elt && TREE_CODE (elt) == TREE_VEC)
11746 elt = copy_template_args (elt);
11747 TREE_VEC_ELT (new_vec, i) = elt;
11750 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11751 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11753 return new_vec;
11756 /* Substitute ARGS into the vector or list of template arguments T. */
11758 static tree
11759 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11761 tree orig_t = t;
11762 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11763 tree *elts;
11765 if (t == error_mark_node)
11766 return error_mark_node;
11768 len = TREE_VEC_LENGTH (t);
11769 elts = XALLOCAVEC (tree, len);
11771 for (i = 0; i < len; i++)
11773 tree orig_arg = TREE_VEC_ELT (t, i);
11774 tree new_arg;
11776 if (TREE_CODE (orig_arg) == TREE_VEC)
11777 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11778 else if (PACK_EXPANSION_P (orig_arg))
11780 /* Substitute into an expansion expression. */
11781 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11783 if (TREE_CODE (new_arg) == TREE_VEC)
11784 /* Add to the expanded length adjustment the number of
11785 expanded arguments. We subtract one from this
11786 measurement, because the argument pack expression
11787 itself is already counted as 1 in
11788 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11789 the argument pack is empty. */
11790 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11792 else if (ARGUMENT_PACK_P (orig_arg))
11794 /* Substitute into each of the arguments. */
11795 new_arg = TYPE_P (orig_arg)
11796 ? cxx_make_type (TREE_CODE (orig_arg))
11797 : make_node (TREE_CODE (orig_arg));
11799 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11800 args, complain, in_decl);
11801 if (pack_args == error_mark_node)
11802 new_arg = error_mark_node;
11803 else
11804 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11806 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11807 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11809 else
11810 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11812 if (new_arg == error_mark_node)
11813 return error_mark_node;
11815 elts[i] = new_arg;
11816 if (new_arg != orig_arg)
11817 need_new = 1;
11820 if (!need_new)
11821 return t;
11823 /* Make space for the expanded arguments coming from template
11824 argument packs. */
11825 t = make_tree_vec (len + expanded_len_adjust);
11826 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11827 arguments for a member template.
11828 In that case each TREE_VEC in ORIG_T represents a level of template
11829 arguments, and ORIG_T won't carry any non defaulted argument count.
11830 It will rather be the nested TREE_VECs that will carry one.
11831 In other words, ORIG_T carries a non defaulted argument count only
11832 if it doesn't contain any nested TREE_VEC. */
11833 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11835 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11836 count += expanded_len_adjust;
11837 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11839 for (i = 0, out = 0; i < len; i++)
11841 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11842 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11843 && TREE_CODE (elts[i]) == TREE_VEC)
11845 int idx;
11847 /* Now expand the template argument pack "in place". */
11848 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11849 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11851 else
11853 TREE_VEC_ELT (t, out) = elts[i];
11854 out++;
11858 return t;
11861 /* Substitute ARGS into one level PARMS of template parameters. */
11863 static tree
11864 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11866 if (parms == error_mark_node)
11867 return error_mark_node;
11869 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11871 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11873 tree tuple = TREE_VEC_ELT (parms, i);
11875 if (tuple == error_mark_node)
11876 continue;
11878 TREE_VEC_ELT (new_vec, i) =
11879 tsubst_template_parm (tuple, args, complain);
11882 return new_vec;
11885 /* Return the result of substituting ARGS into the template parameters
11886 given by PARMS. If there are m levels of ARGS and m + n levels of
11887 PARMS, then the result will contain n levels of PARMS. For
11888 example, if PARMS is `template <class T> template <class U>
11889 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11890 result will be `template <int*, double, class V>'. */
11892 static tree
11893 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11895 tree r = NULL_TREE;
11896 tree* new_parms;
11898 /* When substituting into a template, we must set
11899 PROCESSING_TEMPLATE_DECL as the template parameters may be
11900 dependent if they are based on one-another, and the dependency
11901 predicates are short-circuit outside of templates. */
11902 ++processing_template_decl;
11904 for (new_parms = &r;
11905 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11906 new_parms = &(TREE_CHAIN (*new_parms)),
11907 parms = TREE_CHAIN (parms))
11909 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11910 args, complain);
11911 *new_parms =
11912 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11913 - TMPL_ARGS_DEPTH (args)),
11914 new_vec, NULL_TREE);
11917 --processing_template_decl;
11919 return r;
11922 /* Return the result of substituting ARGS into one template parameter
11923 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11924 parameter and which TREE_PURPOSE is the default argument of the
11925 template parameter. */
11927 static tree
11928 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11930 tree default_value, parm_decl;
11932 if (args == NULL_TREE
11933 || t == NULL_TREE
11934 || t == error_mark_node)
11935 return t;
11937 gcc_assert (TREE_CODE (t) == TREE_LIST);
11939 default_value = TREE_PURPOSE (t);
11940 parm_decl = TREE_VALUE (t);
11942 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11943 if (TREE_CODE (parm_decl) == PARM_DECL
11944 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11945 parm_decl = error_mark_node;
11946 default_value = tsubst_template_arg (default_value, args,
11947 complain, NULL_TREE);
11949 return build_tree_list (default_value, parm_decl);
11952 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11953 type T. If T is not an aggregate or enumeration type, it is
11954 handled as if by tsubst. IN_DECL is as for tsubst. If
11955 ENTERING_SCOPE is nonzero, T is the context for a template which
11956 we are presently tsubst'ing. Return the substituted value. */
11958 static tree
11959 tsubst_aggr_type (tree t,
11960 tree args,
11961 tsubst_flags_t complain,
11962 tree in_decl,
11963 int entering_scope)
11965 if (t == NULL_TREE)
11966 return NULL_TREE;
11968 switch (TREE_CODE (t))
11970 case RECORD_TYPE:
11971 if (TYPE_PTRMEMFUNC_P (t))
11972 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11974 /* Fall through. */
11975 case ENUMERAL_TYPE:
11976 case UNION_TYPE:
11977 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11979 tree argvec;
11980 tree context;
11981 tree r;
11982 int saved_unevaluated_operand;
11983 int saved_inhibit_evaluation_warnings;
11985 /* In "sizeof(X<I>)" we need to evaluate "I". */
11986 saved_unevaluated_operand = cp_unevaluated_operand;
11987 cp_unevaluated_operand = 0;
11988 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11989 c_inhibit_evaluation_warnings = 0;
11991 /* First, determine the context for the type we are looking
11992 up. */
11993 context = TYPE_CONTEXT (t);
11994 if (context && TYPE_P (context))
11996 context = tsubst_aggr_type (context, args, complain,
11997 in_decl, /*entering_scope=*/1);
11998 /* If context is a nested class inside a class template,
11999 it may still need to be instantiated (c++/33959). */
12000 context = complete_type (context);
12003 /* Then, figure out what arguments are appropriate for the
12004 type we are trying to find. For example, given:
12006 template <class T> struct S;
12007 template <class T, class U> void f(T, U) { S<U> su; }
12009 and supposing that we are instantiating f<int, double>,
12010 then our ARGS will be {int, double}, but, when looking up
12011 S we only want {double}. */
12012 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12013 complain, in_decl);
12014 if (argvec == error_mark_node)
12015 r = error_mark_node;
12016 else
12018 r = lookup_template_class (t, argvec, in_decl, context,
12019 entering_scope, complain);
12020 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12023 cp_unevaluated_operand = saved_unevaluated_operand;
12024 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12026 return r;
12028 else
12029 /* This is not a template type, so there's nothing to do. */
12030 return t;
12032 default:
12033 return tsubst (t, args, complain, in_decl);
12037 static GTY(()) hash_map<tree, tree> *defarg_inst;
12039 /* Substitute into the default argument ARG (a default argument for
12040 FN), which has the indicated TYPE. */
12042 tree
12043 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12044 tsubst_flags_t complain)
12046 tree saved_class_ptr = NULL_TREE;
12047 tree saved_class_ref = NULL_TREE;
12048 int errs = errorcount + sorrycount;
12050 /* This can happen in invalid code. */
12051 if (TREE_CODE (arg) == DEFAULT_ARG)
12052 return arg;
12054 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12055 parm = chain_index (parmnum, parm);
12056 tree parmtype = TREE_TYPE (parm);
12057 if (DECL_BY_REFERENCE (parm))
12058 parmtype = TREE_TYPE (parmtype);
12059 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12061 tree *slot;
12062 if (defarg_inst && (slot = defarg_inst->get (parm)))
12063 return *slot;
12065 /* This default argument came from a template. Instantiate the
12066 default argument here, not in tsubst. In the case of
12067 something like:
12069 template <class T>
12070 struct S {
12071 static T t();
12072 void f(T = t());
12075 we must be careful to do name lookup in the scope of S<T>,
12076 rather than in the current class. */
12077 push_access_scope (fn);
12078 /* The "this" pointer is not valid in a default argument. */
12079 if (cfun)
12081 saved_class_ptr = current_class_ptr;
12082 cp_function_chain->x_current_class_ptr = NULL_TREE;
12083 saved_class_ref = current_class_ref;
12084 cp_function_chain->x_current_class_ref = NULL_TREE;
12087 start_lambda_scope (parm);
12089 push_deferring_access_checks(dk_no_deferred);
12090 /* The default argument expression may cause implicitly defined
12091 member functions to be synthesized, which will result in garbage
12092 collection. We must treat this situation as if we were within
12093 the body of function so as to avoid collecting live data on the
12094 stack. */
12095 ++function_depth;
12096 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12097 complain, NULL_TREE,
12098 /*integral_constant_expression_p=*/false);
12099 --function_depth;
12100 pop_deferring_access_checks();
12102 finish_lambda_scope ();
12104 /* Restore the "this" pointer. */
12105 if (cfun)
12107 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12108 cp_function_chain->x_current_class_ref = saved_class_ref;
12111 if (errorcount+sorrycount > errs
12112 && (complain & tf_warning_or_error))
12113 inform (input_location,
12114 " when instantiating default argument for call to %qD", fn);
12116 /* Make sure the default argument is reasonable. */
12117 arg = check_default_argument (type, arg, complain);
12119 pop_access_scope (fn);
12121 if (arg != error_mark_node && !cp_unevaluated_operand)
12123 if (!defarg_inst)
12124 defarg_inst = hash_map<tree,tree>::create_ggc (37);
12125 defarg_inst->put (parm, arg);
12128 return arg;
12131 /* Substitute into all the default arguments for FN. */
12133 static void
12134 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12136 tree arg;
12137 tree tmpl_args;
12139 tmpl_args = DECL_TI_ARGS (fn);
12141 /* If this function is not yet instantiated, we certainly don't need
12142 its default arguments. */
12143 if (uses_template_parms (tmpl_args))
12144 return;
12145 /* Don't do this again for clones. */
12146 if (DECL_CLONED_FUNCTION_P (fn))
12147 return;
12149 int i = 0;
12150 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12151 arg;
12152 arg = TREE_CHAIN (arg), ++i)
12153 if (TREE_PURPOSE (arg))
12154 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12155 TREE_VALUE (arg),
12156 TREE_PURPOSE (arg),
12157 complain);
12160 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12162 static tree
12163 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12164 tree lambda_fntype)
12166 tree gen_tmpl, argvec;
12167 hashval_t hash = 0;
12168 tree in_decl = t;
12170 /* Nobody should be tsubst'ing into non-template functions. */
12171 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12173 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12175 /* If T is not dependent, just return it. */
12176 if (!uses_template_parms (DECL_TI_ARGS (t)))
12177 return t;
12179 /* Calculate the most general template of which R is a
12180 specialization, and the complete set of arguments used to
12181 specialize R. */
12182 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12183 argvec = tsubst_template_args (DECL_TI_ARGS
12184 (DECL_TEMPLATE_RESULT
12185 (DECL_TI_TEMPLATE (t))),
12186 args, complain, in_decl);
12187 if (argvec == error_mark_node)
12188 return error_mark_node;
12190 /* Check to see if we already have this specialization. */
12191 if (!lambda_fntype)
12193 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12194 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12195 return spec;
12198 /* We can see more levels of arguments than parameters if
12199 there was a specialization of a member template, like
12200 this:
12202 template <class T> struct S { template <class U> void f(); }
12203 template <> template <class U> void S<int>::f(U);
12205 Here, we'll be substituting into the specialization,
12206 because that's where we can find the code we actually
12207 want to generate, but we'll have enough arguments for
12208 the most general template.
12210 We also deal with the peculiar case:
12212 template <class T> struct S {
12213 template <class U> friend void f();
12215 template <class U> void f() {}
12216 template S<int>;
12217 template void f<double>();
12219 Here, the ARGS for the instantiation of will be {int,
12220 double}. But, we only need as many ARGS as there are
12221 levels of template parameters in CODE_PATTERN. We are
12222 careful not to get fooled into reducing the ARGS in
12223 situations like:
12225 template <class T> struct S { template <class U> void f(U); }
12226 template <class T> template <> void S<T>::f(int) {}
12228 which we can spot because the pattern will be a
12229 specialization in this case. */
12230 int args_depth = TMPL_ARGS_DEPTH (args);
12231 int parms_depth =
12232 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12234 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12235 args = get_innermost_template_args (args, parms_depth);
12237 else
12239 /* This special case arises when we have something like this:
12241 template <class T> struct S {
12242 friend void f<int>(int, double);
12245 Here, the DECL_TI_TEMPLATE for the friend declaration
12246 will be an IDENTIFIER_NODE. We are being called from
12247 tsubst_friend_function, and we want only to create a
12248 new decl (R) with appropriate types so that we can call
12249 determine_specialization. */
12250 gen_tmpl = NULL_TREE;
12251 argvec = NULL_TREE;
12254 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12255 : NULL_TREE);
12256 tree ctx = closure ? closure : DECL_CONTEXT (t);
12257 bool member = ctx && TYPE_P (ctx);
12259 if (member && !closure)
12260 ctx = tsubst_aggr_type (ctx, args,
12261 complain, t, /*entering_scope=*/1);
12263 tree type = (lambda_fntype ? lambda_fntype
12264 : tsubst (TREE_TYPE (t), args,
12265 complain | tf_fndecl_type, in_decl));
12266 if (type == error_mark_node)
12267 return error_mark_node;
12269 /* If we hit excessive deduction depth, the type is bogus even if
12270 it isn't error_mark_node, so don't build a decl. */
12271 if (excessive_deduction_depth)
12272 return error_mark_node;
12274 /* We do NOT check for matching decls pushed separately at this
12275 point, as they may not represent instantiations of this
12276 template, and in any case are considered separate under the
12277 discrete model. */
12278 tree r = copy_decl (t);
12279 DECL_USE_TEMPLATE (r) = 0;
12280 TREE_TYPE (r) = type;
12281 /* Clear out the mangled name and RTL for the instantiation. */
12282 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12283 SET_DECL_RTL (r, NULL);
12284 /* Leave DECL_INITIAL set on deleted instantiations. */
12285 if (!DECL_DELETED_FN (r))
12286 DECL_INITIAL (r) = NULL_TREE;
12287 DECL_CONTEXT (r) = ctx;
12289 /* OpenMP UDRs have the only argument a reference to the declared
12290 type. We want to diagnose if the declared type is a reference,
12291 which is invalid, but as references to references are usually
12292 quietly merged, diagnose it here. */
12293 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12295 tree argtype
12296 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12297 argtype = tsubst (argtype, args, complain, in_decl);
12298 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12299 error_at (DECL_SOURCE_LOCATION (t),
12300 "reference type %qT in "
12301 "%<#pragma omp declare reduction%>", argtype);
12302 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12303 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12304 argtype);
12307 if (member && DECL_CONV_FN_P (r))
12308 /* Type-conversion operator. Reconstruct the name, in
12309 case it's the name of one of the template's parameters. */
12310 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12312 tree parms = DECL_ARGUMENTS (t);
12313 if (closure)
12314 parms = DECL_CHAIN (parms);
12315 parms = tsubst (parms, args, complain, t);
12316 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12317 DECL_CONTEXT (parm) = r;
12318 if (closure)
12320 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12321 DECL_CHAIN (tparm) = parms;
12322 parms = tparm;
12324 DECL_ARGUMENTS (r) = parms;
12325 DECL_RESULT (r) = NULL_TREE;
12327 TREE_STATIC (r) = 0;
12328 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12329 DECL_EXTERNAL (r) = 1;
12330 /* If this is an instantiation of a function with internal
12331 linkage, we already know what object file linkage will be
12332 assigned to the instantiation. */
12333 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12334 DECL_DEFER_OUTPUT (r) = 0;
12335 DECL_CHAIN (r) = NULL_TREE;
12336 DECL_PENDING_INLINE_INFO (r) = 0;
12337 DECL_PENDING_INLINE_P (r) = 0;
12338 DECL_SAVED_TREE (r) = NULL_TREE;
12339 DECL_STRUCT_FUNCTION (r) = NULL;
12340 TREE_USED (r) = 0;
12341 /* We'll re-clone as appropriate in instantiate_template. */
12342 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12344 /* If we aren't complaining now, return on error before we register
12345 the specialization so that we'll complain eventually. */
12346 if ((complain & tf_error) == 0
12347 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12348 && !grok_op_properties (r, /*complain=*/false))
12349 return error_mark_node;
12351 /* When instantiating a constrained member, substitute
12352 into the constraints to create a new constraint. */
12353 if (tree ci = get_constraints (t))
12354 if (member)
12356 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12357 set_constraints (r, ci);
12360 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12361 this in the special friend case mentioned above where
12362 GEN_TMPL is NULL. */
12363 if (gen_tmpl && !closure)
12365 DECL_TEMPLATE_INFO (r)
12366 = build_template_info (gen_tmpl, argvec);
12367 SET_DECL_IMPLICIT_INSTANTIATION (r);
12369 tree new_r
12370 = register_specialization (r, gen_tmpl, argvec, false, hash);
12371 if (new_r != r)
12372 /* We instantiated this while substituting into
12373 the type earlier (template/friend54.C). */
12374 return new_r;
12376 /* We're not supposed to instantiate default arguments
12377 until they are called, for a template. But, for a
12378 declaration like:
12380 template <class T> void f ()
12381 { extern void g(int i = T()); }
12383 we should do the substitution when the template is
12384 instantiated. We handle the member function case in
12385 instantiate_class_template since the default arguments
12386 might refer to other members of the class. */
12387 if (!member
12388 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12389 && !uses_template_parms (argvec))
12390 tsubst_default_arguments (r, complain);
12392 else
12393 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12395 /* Copy the list of befriending classes. */
12396 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12397 *friends;
12398 friends = &TREE_CHAIN (*friends))
12400 *friends = copy_node (*friends);
12401 TREE_VALUE (*friends)
12402 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12405 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12407 maybe_retrofit_in_chrg (r);
12408 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12409 return error_mark_node;
12410 /* If this is an instantiation of a member template, clone it.
12411 If it isn't, that'll be handled by
12412 clone_constructors_and_destructors. */
12413 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12414 clone_function_decl (r, /*update_methods=*/false);
12416 else if ((complain & tf_error) != 0
12417 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12418 && !grok_op_properties (r, /*complain=*/true))
12419 return error_mark_node;
12421 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12422 SET_DECL_FRIEND_CONTEXT (r,
12423 tsubst (DECL_FRIEND_CONTEXT (t),
12424 args, complain, in_decl));
12426 /* Possibly limit visibility based on template args. */
12427 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12428 if (DECL_VISIBILITY_SPECIFIED (t))
12430 DECL_VISIBILITY_SPECIFIED (r) = 0;
12431 DECL_ATTRIBUTES (r)
12432 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12434 determine_visibility (r);
12435 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12436 && !processing_template_decl)
12437 defaulted_late_check (r);
12439 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12440 args, complain, in_decl);
12441 return r;
12444 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12446 static tree
12447 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12448 tree lambda_fntype)
12450 /* We can get here when processing a member function template,
12451 member class template, or template template parameter. */
12452 tree decl = DECL_TEMPLATE_RESULT (t);
12453 tree in_decl = t;
12454 tree spec;
12455 tree tmpl_args;
12456 tree full_args;
12457 tree r;
12458 hashval_t hash = 0;
12460 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12462 /* Template template parameter is treated here. */
12463 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12464 if (new_type == error_mark_node)
12465 r = error_mark_node;
12466 /* If we get a real template back, return it. This can happen in
12467 the context of most_specialized_partial_spec. */
12468 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12469 r = new_type;
12470 else
12471 /* The new TEMPLATE_DECL was built in
12472 reduce_template_parm_level. */
12473 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12474 return r;
12477 if (!lambda_fntype)
12479 /* We might already have an instance of this template.
12480 The ARGS are for the surrounding class type, so the
12481 full args contain the tsubst'd args for the context,
12482 plus the innermost args from the template decl. */
12483 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12484 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12485 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12486 /* Because this is a template, the arguments will still be
12487 dependent, even after substitution. If
12488 PROCESSING_TEMPLATE_DECL is not set, the dependency
12489 predicates will short-circuit. */
12490 ++processing_template_decl;
12491 full_args = tsubst_template_args (tmpl_args, args,
12492 complain, in_decl);
12493 --processing_template_decl;
12494 if (full_args == error_mark_node)
12495 return error_mark_node;
12497 /* If this is a default template template argument,
12498 tsubst might not have changed anything. */
12499 if (full_args == tmpl_args)
12500 return t;
12502 hash = hash_tmpl_and_args (t, full_args);
12503 spec = retrieve_specialization (t, full_args, hash);
12504 if (spec != NULL_TREE)
12505 return spec;
12508 /* Make a new template decl. It will be similar to the
12509 original, but will record the current template arguments.
12510 We also create a new function declaration, which is just
12511 like the old one, but points to this new template, rather
12512 than the old one. */
12513 r = copy_decl (t);
12514 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12515 DECL_CHAIN (r) = NULL_TREE;
12517 // Build new template info linking to the original template decl.
12518 if (!lambda_fntype)
12520 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12521 SET_DECL_IMPLICIT_INSTANTIATION (r);
12523 else
12524 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12526 /* The template parameters for this new template are all the
12527 template parameters for the old template, except the
12528 outermost level of parameters. */
12529 DECL_TEMPLATE_PARMS (r)
12530 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12531 complain);
12533 if (TREE_CODE (decl) == TYPE_DECL
12534 && !TYPE_DECL_ALIAS_P (decl))
12536 tree new_type;
12537 ++processing_template_decl;
12538 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12539 --processing_template_decl;
12540 if (new_type == error_mark_node)
12541 return error_mark_node;
12543 TREE_TYPE (r) = new_type;
12544 /* For a partial specialization, we need to keep pointing to
12545 the primary template. */
12546 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12547 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12548 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12549 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12550 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12552 else
12554 tree new_decl;
12555 ++processing_template_decl;
12556 if (TREE_CODE (decl) == FUNCTION_DECL)
12557 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12558 else
12559 new_decl = tsubst (decl, args, complain, in_decl);
12560 --processing_template_decl;
12561 if (new_decl == error_mark_node)
12562 return error_mark_node;
12564 DECL_TEMPLATE_RESULT (r) = new_decl;
12565 TREE_TYPE (r) = TREE_TYPE (new_decl);
12566 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12567 if (lambda_fntype)
12569 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12570 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12572 else
12574 DECL_TI_TEMPLATE (new_decl) = r;
12575 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12579 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12580 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12582 if (PRIMARY_TEMPLATE_P (t))
12583 DECL_PRIMARY_TEMPLATE (r) = r;
12585 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12586 && !lambda_fntype)
12587 /* Record this non-type partial instantiation. */
12588 register_specialization (r, t,
12589 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12590 false, hash);
12592 return r;
12595 /* True if FN is the op() for a lambda in an uninstantiated template. */
12597 bool
12598 lambda_fn_in_template_p (tree fn)
12600 if (!fn || !LAMBDA_FUNCTION_P (fn))
12601 return false;
12602 tree closure = DECL_CONTEXT (fn);
12603 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12606 /* True if FN is the op() for a lambda regenerated from a lambda in an
12607 uninstantiated template. */
12609 bool
12610 regenerated_lambda_fn_p (tree fn)
12612 return (LAMBDA_FUNCTION_P (fn)
12613 && !DECL_TEMPLATE_INSTANTIATION (fn));
12616 /* We're instantiating a variable from template function TCTX. Return the
12617 corresponding current enclosing scope. This gets complicated because lambda
12618 functions in templates are regenerated rather than instantiated, but generic
12619 lambda functions are subsequently instantiated. */
12621 static tree
12622 enclosing_instantiation_of (tree tctx)
12624 tree fn = current_function_decl;
12625 int lambda_count = 0;
12627 for (; tctx && lambda_fn_in_template_p (tctx);
12628 tctx = decl_function_context (tctx))
12629 ++lambda_count;
12630 for (; fn; fn = decl_function_context (fn))
12632 tree lambda = fn;
12633 int flambda_count = 0;
12634 for (; fn && regenerated_lambda_fn_p (fn);
12635 fn = decl_function_context (fn))
12636 ++flambda_count;
12637 if (DECL_TEMPLATE_INFO (fn)
12638 ? most_general_template (fn) != most_general_template (tctx)
12639 : fn != tctx)
12640 continue;
12641 if (lambda_count)
12643 fn = lambda;
12644 while (flambda_count-- > lambda_count)
12645 fn = decl_function_context (fn);
12647 return fn;
12649 gcc_unreachable ();
12652 /* Substitute the ARGS into the T, which is a _DECL. Return the
12653 result of the substitution. Issue error and warning messages under
12654 control of COMPLAIN. */
12656 static tree
12657 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12659 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12660 location_t saved_loc;
12661 tree r = NULL_TREE;
12662 tree in_decl = t;
12663 hashval_t hash = 0;
12665 /* Set the filename and linenumber to improve error-reporting. */
12666 saved_loc = input_location;
12667 input_location = DECL_SOURCE_LOCATION (t);
12669 switch (TREE_CODE (t))
12671 case TEMPLATE_DECL:
12672 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12673 break;
12675 case FUNCTION_DECL:
12676 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12677 break;
12679 case PARM_DECL:
12681 tree type = NULL_TREE;
12682 int i, len = 1;
12683 tree expanded_types = NULL_TREE;
12684 tree prev_r = NULL_TREE;
12685 tree first_r = NULL_TREE;
12687 if (DECL_PACK_P (t))
12689 /* If there is a local specialization that isn't a
12690 parameter pack, it means that we're doing a "simple"
12691 substitution from inside tsubst_pack_expansion. Just
12692 return the local specialization (which will be a single
12693 parm). */
12694 tree spec = retrieve_local_specialization (t);
12695 if (spec
12696 && TREE_CODE (spec) == PARM_DECL
12697 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12698 RETURN (spec);
12700 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12701 the parameters in this function parameter pack. */
12702 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12703 complain, in_decl);
12704 if (TREE_CODE (expanded_types) == TREE_VEC)
12706 len = TREE_VEC_LENGTH (expanded_types);
12708 /* Zero-length parameter packs are boring. Just substitute
12709 into the chain. */
12710 if (len == 0)
12711 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12712 TREE_CHAIN (t)));
12714 else
12716 /* All we did was update the type. Make a note of that. */
12717 type = expanded_types;
12718 expanded_types = NULL_TREE;
12722 /* Loop through all of the parameters we'll build. When T is
12723 a function parameter pack, LEN is the number of expanded
12724 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12725 r = NULL_TREE;
12726 for (i = 0; i < len; ++i)
12728 prev_r = r;
12729 r = copy_node (t);
12730 if (DECL_TEMPLATE_PARM_P (t))
12731 SET_DECL_TEMPLATE_PARM_P (r);
12733 if (expanded_types)
12734 /* We're on the Ith parameter of the function parameter
12735 pack. */
12737 /* Get the Ith type. */
12738 type = TREE_VEC_ELT (expanded_types, i);
12740 /* Rename the parameter to include the index. */
12741 DECL_NAME (r)
12742 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12744 else if (!type)
12745 /* We're dealing with a normal parameter. */
12746 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12748 type = type_decays_to (type);
12749 TREE_TYPE (r) = type;
12750 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12752 if (DECL_INITIAL (r))
12754 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12755 DECL_INITIAL (r) = TREE_TYPE (r);
12756 else
12757 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12758 complain, in_decl);
12761 DECL_CONTEXT (r) = NULL_TREE;
12763 if (!DECL_TEMPLATE_PARM_P (r))
12764 DECL_ARG_TYPE (r) = type_passed_as (type);
12766 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12767 args, complain, in_decl);
12769 /* Keep track of the first new parameter we
12770 generate. That's what will be returned to the
12771 caller. */
12772 if (!first_r)
12773 first_r = r;
12775 /* Build a proper chain of parameters when substituting
12776 into a function parameter pack. */
12777 if (prev_r)
12778 DECL_CHAIN (prev_r) = r;
12781 /* If cp_unevaluated_operand is set, we're just looking for a
12782 single dummy parameter, so don't keep going. */
12783 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12784 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12785 complain, DECL_CHAIN (t));
12787 /* FIRST_R contains the start of the chain we've built. */
12788 r = first_r;
12790 break;
12792 case FIELD_DECL:
12794 tree type = NULL_TREE;
12795 tree vec = NULL_TREE;
12796 tree expanded_types = NULL_TREE;
12797 int len = 1;
12799 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12801 /* This field is a lambda capture pack. Return a TREE_VEC of
12802 the expanded fields to instantiate_class_template_1 and
12803 store them in the specializations hash table as a
12804 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12805 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12806 complain, in_decl);
12807 if (TREE_CODE (expanded_types) == TREE_VEC)
12809 len = TREE_VEC_LENGTH (expanded_types);
12810 vec = make_tree_vec (len);
12812 else
12814 /* All we did was update the type. Make a note of that. */
12815 type = expanded_types;
12816 expanded_types = NULL_TREE;
12820 for (int i = 0; i < len; ++i)
12822 r = copy_decl (t);
12823 if (expanded_types)
12825 type = TREE_VEC_ELT (expanded_types, i);
12826 DECL_NAME (r)
12827 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12829 else if (!type)
12830 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12832 if (type == error_mark_node)
12833 RETURN (error_mark_node);
12834 TREE_TYPE (r) = type;
12835 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12837 if (DECL_C_BIT_FIELD (r))
12838 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
12839 number of bits. */
12840 DECL_BIT_FIELD_REPRESENTATIVE (r)
12841 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
12842 complain, in_decl,
12843 /*integral_constant_expression_p=*/true);
12844 if (DECL_INITIAL (t))
12846 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12847 NSDMI in perform_member_init. Still set DECL_INITIAL
12848 so that we know there is one. */
12849 DECL_INITIAL (r) = void_node;
12850 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12851 retrofit_lang_decl (r);
12852 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12854 /* We don't have to set DECL_CONTEXT here; it is set by
12855 finish_member_declaration. */
12856 DECL_CHAIN (r) = NULL_TREE;
12858 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12859 args, complain, in_decl);
12861 if (vec)
12862 TREE_VEC_ELT (vec, i) = r;
12865 if (vec)
12867 r = vec;
12868 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12869 SET_ARGUMENT_PACK_ARGS (pack, vec);
12870 register_specialization (pack, t, args, false, 0);
12873 break;
12875 case USING_DECL:
12876 /* We reach here only for member using decls. We also need to check
12877 uses_template_parms because DECL_DEPENDENT_P is not set for a
12878 using-declaration that designates a member of the current
12879 instantiation (c++/53549). */
12880 if (DECL_DEPENDENT_P (t)
12881 || uses_template_parms (USING_DECL_SCOPE (t)))
12883 tree scope = USING_DECL_SCOPE (t);
12884 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12885 if (PACK_EXPANSION_P (scope))
12887 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12888 int len = TREE_VEC_LENGTH (vec);
12889 r = make_tree_vec (len);
12890 for (int i = 0; i < len; ++i)
12892 tree escope = TREE_VEC_ELT (vec, i);
12893 tree elt = do_class_using_decl (escope, name);
12894 if (!elt)
12896 r = error_mark_node;
12897 break;
12899 else
12901 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12902 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12904 TREE_VEC_ELT (r, i) = elt;
12907 else
12909 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12910 complain, in_decl);
12911 r = do_class_using_decl (inst_scope, name);
12912 if (!r)
12913 r = error_mark_node;
12914 else
12916 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12917 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12921 else
12923 r = copy_node (t);
12924 DECL_CHAIN (r) = NULL_TREE;
12926 break;
12928 case TYPE_DECL:
12929 case VAR_DECL:
12931 tree argvec = NULL_TREE;
12932 tree gen_tmpl = NULL_TREE;
12933 tree spec;
12934 tree tmpl = NULL_TREE;
12935 tree ctx;
12936 tree type = NULL_TREE;
12937 bool local_p;
12939 if (TREE_TYPE (t) == error_mark_node)
12940 RETURN (error_mark_node);
12942 if (TREE_CODE (t) == TYPE_DECL
12943 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12945 /* If this is the canonical decl, we don't have to
12946 mess with instantiations, and often we can't (for
12947 typename, template type parms and such). Note that
12948 TYPE_NAME is not correct for the above test if
12949 we've copied the type for a typedef. */
12950 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12951 if (type == error_mark_node)
12952 RETURN (error_mark_node);
12953 r = TYPE_NAME (type);
12954 break;
12957 /* Check to see if we already have the specialization we
12958 need. */
12959 spec = NULL_TREE;
12960 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12962 /* T is a static data member or namespace-scope entity.
12963 We have to substitute into namespace-scope variables
12964 (not just variable templates) because of cases like:
12966 template <class T> void f() { extern T t; }
12968 where the entity referenced is not known until
12969 instantiation time. */
12970 local_p = false;
12971 ctx = DECL_CONTEXT (t);
12972 if (DECL_CLASS_SCOPE_P (t))
12974 ctx = tsubst_aggr_type (ctx, args,
12975 complain,
12976 in_decl, /*entering_scope=*/1);
12977 /* If CTX is unchanged, then T is in fact the
12978 specialization we want. That situation occurs when
12979 referencing a static data member within in its own
12980 class. We can use pointer equality, rather than
12981 same_type_p, because DECL_CONTEXT is always
12982 canonical... */
12983 if (ctx == DECL_CONTEXT (t)
12984 /* ... unless T is a member template; in which
12985 case our caller can be willing to create a
12986 specialization of that template represented
12987 by T. */
12988 && !(DECL_TI_TEMPLATE (t)
12989 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12990 spec = t;
12993 if (!spec)
12995 tmpl = DECL_TI_TEMPLATE (t);
12996 gen_tmpl = most_general_template (tmpl);
12997 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12998 if (argvec != error_mark_node)
12999 argvec = (coerce_innermost_template_parms
13000 (DECL_TEMPLATE_PARMS (gen_tmpl),
13001 argvec, t, complain,
13002 /*all*/true, /*defarg*/true));
13003 if (argvec == error_mark_node)
13004 RETURN (error_mark_node);
13005 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13006 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13009 else
13011 /* A local variable. */
13012 local_p = true;
13013 /* Subsequent calls to pushdecl will fill this in. */
13014 ctx = NULL_TREE;
13015 /* Unless this is a reference to a static variable from an
13016 enclosing function, in which case we need to fill it in now. */
13017 if (TREE_STATIC (t))
13019 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13020 if (fn != current_function_decl)
13021 ctx = fn;
13023 spec = retrieve_local_specialization (t);
13025 /* If we already have the specialization we need, there is
13026 nothing more to do. */
13027 if (spec)
13029 r = spec;
13030 break;
13033 /* Create a new node for the specialization we need. */
13034 r = copy_decl (t);
13035 if (type == NULL_TREE)
13037 if (is_typedef_decl (t))
13038 type = DECL_ORIGINAL_TYPE (t);
13039 else
13040 type = TREE_TYPE (t);
13041 if (VAR_P (t)
13042 && VAR_HAD_UNKNOWN_BOUND (t)
13043 && type != error_mark_node)
13044 type = strip_array_domain (type);
13045 tree sub_args = args;
13046 if (tree auto_node = type_uses_auto (type))
13048 /* Mask off any template args past the variable's context so we
13049 don't replace the auto with an unrelated argument. */
13050 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13051 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13052 if (extra > 0)
13053 /* This should never happen with the new lambda instantiation
13054 model, but keep the handling just in case. */
13055 gcc_assert (!CHECKING_P),
13056 sub_args = strip_innermost_template_args (args, extra);
13058 type = tsubst (type, sub_args, complain, in_decl);
13060 if (VAR_P (r))
13062 /* Even if the original location is out of scope, the
13063 newly substituted one is not. */
13064 DECL_DEAD_FOR_LOCAL (r) = 0;
13065 DECL_INITIALIZED_P (r) = 0;
13066 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13067 if (type == error_mark_node)
13068 RETURN (error_mark_node);
13069 if (TREE_CODE (type) == FUNCTION_TYPE)
13071 /* It may seem that this case cannot occur, since:
13073 typedef void f();
13074 void g() { f x; }
13076 declares a function, not a variable. However:
13078 typedef void f();
13079 template <typename T> void g() { T t; }
13080 template void g<f>();
13082 is an attempt to declare a variable with function
13083 type. */
13084 error ("variable %qD has function type",
13085 /* R is not yet sufficiently initialized, so we
13086 just use its name. */
13087 DECL_NAME (r));
13088 RETURN (error_mark_node);
13090 type = complete_type (type);
13091 /* Wait until cp_finish_decl to set this again, to handle
13092 circular dependency (template/instantiate6.C). */
13093 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13094 type = check_var_type (DECL_NAME (r), type);
13096 if (DECL_HAS_VALUE_EXPR_P (t))
13098 tree ve = DECL_VALUE_EXPR (t);
13099 ve = tsubst_expr (ve, args, complain, in_decl,
13100 /*constant_expression_p=*/false);
13101 if (REFERENCE_REF_P (ve))
13103 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13104 ve = TREE_OPERAND (ve, 0);
13106 SET_DECL_VALUE_EXPR (r, ve);
13108 if (CP_DECL_THREAD_LOCAL_P (r)
13109 && !processing_template_decl)
13110 set_decl_tls_model (r, decl_default_tls_model (r));
13112 else if (DECL_SELF_REFERENCE_P (t))
13113 SET_DECL_SELF_REFERENCE_P (r);
13114 TREE_TYPE (r) = type;
13115 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13116 DECL_CONTEXT (r) = ctx;
13117 /* Clear out the mangled name and RTL for the instantiation. */
13118 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13119 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13120 SET_DECL_RTL (r, NULL);
13121 /* The initializer must not be expanded until it is required;
13122 see [temp.inst]. */
13123 DECL_INITIAL (r) = NULL_TREE;
13124 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13125 if (VAR_P (r))
13127 SET_DECL_MODE (r, VOIDmode);
13129 /* Possibly limit visibility based on template args. */
13130 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13131 if (DECL_VISIBILITY_SPECIFIED (t))
13133 DECL_VISIBILITY_SPECIFIED (r) = 0;
13134 DECL_ATTRIBUTES (r)
13135 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13137 determine_visibility (r);
13140 if (!local_p)
13142 /* A static data member declaration is always marked
13143 external when it is declared in-class, even if an
13144 initializer is present. We mimic the non-template
13145 processing here. */
13146 DECL_EXTERNAL (r) = 1;
13147 if (DECL_NAMESPACE_SCOPE_P (t))
13148 DECL_NOT_REALLY_EXTERN (r) = 1;
13150 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13151 SET_DECL_IMPLICIT_INSTANTIATION (r);
13152 register_specialization (r, gen_tmpl, argvec, false, hash);
13154 else
13156 if (DECL_LANG_SPECIFIC (r))
13157 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13158 if (!cp_unevaluated_operand)
13159 register_local_specialization (r, t);
13162 DECL_CHAIN (r) = NULL_TREE;
13164 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13165 /*flags=*/0,
13166 args, complain, in_decl);
13168 /* Preserve a typedef that names a type. */
13169 if (is_typedef_decl (r) && type != error_mark_node)
13171 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13172 set_underlying_type (r);
13173 if (TYPE_DECL_ALIAS_P (r))
13174 /* An alias template specialization can be dependent
13175 even if its underlying type is not. */
13176 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13179 layout_decl (r, 0);
13181 break;
13183 default:
13184 gcc_unreachable ();
13186 #undef RETURN
13188 out:
13189 /* Restore the file and line information. */
13190 input_location = saved_loc;
13192 return r;
13195 /* Substitute into the ARG_TYPES of a function type.
13196 If END is a TREE_CHAIN, leave it and any following types
13197 un-substituted. */
13199 static tree
13200 tsubst_arg_types (tree arg_types,
13201 tree args,
13202 tree end,
13203 tsubst_flags_t complain,
13204 tree in_decl)
13206 tree remaining_arg_types;
13207 tree type = NULL_TREE;
13208 int i = 1;
13209 tree expanded_args = NULL_TREE;
13210 tree default_arg;
13212 if (!arg_types || arg_types == void_list_node || arg_types == end)
13213 return arg_types;
13215 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13216 args, end, complain, in_decl);
13217 if (remaining_arg_types == error_mark_node)
13218 return error_mark_node;
13220 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13222 /* For a pack expansion, perform substitution on the
13223 entire expression. Later on, we'll handle the arguments
13224 one-by-one. */
13225 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13226 args, complain, in_decl);
13228 if (TREE_CODE (expanded_args) == TREE_VEC)
13229 /* So that we'll spin through the parameters, one by one. */
13230 i = TREE_VEC_LENGTH (expanded_args);
13231 else
13233 /* We only partially substituted into the parameter
13234 pack. Our type is TYPE_PACK_EXPANSION. */
13235 type = expanded_args;
13236 expanded_args = NULL_TREE;
13240 while (i > 0) {
13241 --i;
13243 if (expanded_args)
13244 type = TREE_VEC_ELT (expanded_args, i);
13245 else if (!type)
13246 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13248 if (type == error_mark_node)
13249 return error_mark_node;
13250 if (VOID_TYPE_P (type))
13252 if (complain & tf_error)
13254 error ("invalid parameter type %qT", type);
13255 if (in_decl)
13256 error ("in declaration %q+D", in_decl);
13258 return error_mark_node;
13260 /* DR 657. */
13261 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13262 return error_mark_node;
13264 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13265 top-level qualifiers as required. */
13266 type = cv_unqualified (type_decays_to (type));
13268 /* We do not substitute into default arguments here. The standard
13269 mandates that they be instantiated only when needed, which is
13270 done in build_over_call. */
13271 default_arg = TREE_PURPOSE (arg_types);
13273 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13274 since the new op() won't have any associated template arguments for us
13275 to refer to later. */
13276 if (lambda_fn_in_template_p (in_decl))
13277 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13278 false/*fn*/, false/*constexpr*/);
13280 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13282 /* We've instantiated a template before its default arguments
13283 have been parsed. This can happen for a nested template
13284 class, and is not an error unless we require the default
13285 argument in a call of this function. */
13286 remaining_arg_types =
13287 tree_cons (default_arg, type, remaining_arg_types);
13288 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13290 else
13291 remaining_arg_types =
13292 hash_tree_cons (default_arg, type, remaining_arg_types);
13295 return remaining_arg_types;
13298 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13299 *not* handle the exception-specification for FNTYPE, because the
13300 initial substitution of explicitly provided template parameters
13301 during argument deduction forbids substitution into the
13302 exception-specification:
13304 [temp.deduct]
13306 All references in the function type of the function template to the
13307 corresponding template parameters are replaced by the specified tem-
13308 plate argument values. If a substitution in a template parameter or
13309 in the function type of the function template results in an invalid
13310 type, type deduction fails. [Note: The equivalent substitution in
13311 exception specifications is done only when the function is instanti-
13312 ated, at which point a program is ill-formed if the substitution
13313 results in an invalid type.] */
13315 static tree
13316 tsubst_function_type (tree t,
13317 tree args,
13318 tsubst_flags_t complain,
13319 tree in_decl)
13321 tree return_type;
13322 tree arg_types = NULL_TREE;
13323 tree fntype;
13325 /* The TYPE_CONTEXT is not used for function/method types. */
13326 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13328 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13329 failure. */
13330 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13332 if (late_return_type_p)
13334 /* Substitute the argument types. */
13335 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13336 complain, in_decl);
13337 if (arg_types == error_mark_node)
13338 return error_mark_node;
13340 tree save_ccp = current_class_ptr;
13341 tree save_ccr = current_class_ref;
13342 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13343 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13344 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13345 if (do_inject)
13347 /* DR 1207: 'this' is in scope in the trailing return type. */
13348 inject_this_parameter (this_type, cp_type_quals (this_type));
13351 /* Substitute the return type. */
13352 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13354 if (do_inject)
13356 current_class_ptr = save_ccp;
13357 current_class_ref = save_ccr;
13360 else
13361 /* Substitute the return type. */
13362 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13364 if (return_type == error_mark_node)
13365 return error_mark_node;
13366 /* DR 486 clarifies that creation of a function type with an
13367 invalid return type is a deduction failure. */
13368 if (TREE_CODE (return_type) == ARRAY_TYPE
13369 || TREE_CODE (return_type) == FUNCTION_TYPE)
13371 if (complain & tf_error)
13373 if (TREE_CODE (return_type) == ARRAY_TYPE)
13374 error ("function returning an array");
13375 else
13376 error ("function returning a function");
13378 return error_mark_node;
13380 /* And DR 657. */
13381 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13382 return error_mark_node;
13384 if (!late_return_type_p)
13386 /* Substitute the argument types. */
13387 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13388 complain, in_decl);
13389 if (arg_types == error_mark_node)
13390 return error_mark_node;
13393 /* Construct a new type node and return it. */
13394 if (TREE_CODE (t) == FUNCTION_TYPE)
13396 fntype = build_function_type (return_type, arg_types);
13397 fntype = apply_memfn_quals (fntype,
13398 type_memfn_quals (t),
13399 type_memfn_rqual (t));
13401 else
13403 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13404 /* Don't pick up extra function qualifiers from the basetype. */
13405 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13406 if (! MAYBE_CLASS_TYPE_P (r))
13408 /* [temp.deduct]
13410 Type deduction may fail for any of the following
13411 reasons:
13413 -- Attempting to create "pointer to member of T" when T
13414 is not a class type. */
13415 if (complain & tf_error)
13416 error ("creating pointer to member function of non-class type %qT",
13418 return error_mark_node;
13421 fntype = build_method_type_directly (r, return_type,
13422 TREE_CHAIN (arg_types));
13423 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13425 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13427 if (late_return_type_p)
13428 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13430 return fntype;
13433 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13434 ARGS into that specification, and return the substituted
13435 specification. If there is no specification, return NULL_TREE. */
13437 static tree
13438 tsubst_exception_specification (tree fntype,
13439 tree args,
13440 tsubst_flags_t complain,
13441 tree in_decl,
13442 bool defer_ok)
13444 tree specs;
13445 tree new_specs;
13447 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13448 new_specs = NULL_TREE;
13449 if (specs && TREE_PURPOSE (specs))
13451 /* A noexcept-specifier. */
13452 tree expr = TREE_PURPOSE (specs);
13453 if (TREE_CODE (expr) == INTEGER_CST)
13454 new_specs = expr;
13455 else if (defer_ok)
13457 /* Defer instantiation of noexcept-specifiers to avoid
13458 excessive instantiations (c++/49107). */
13459 new_specs = make_node (DEFERRED_NOEXCEPT);
13460 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13462 /* We already partially instantiated this member template,
13463 so combine the new args with the old. */
13464 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13465 = DEFERRED_NOEXCEPT_PATTERN (expr);
13466 DEFERRED_NOEXCEPT_ARGS (new_specs)
13467 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13469 else
13471 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13472 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13475 else
13476 new_specs = tsubst_copy_and_build
13477 (expr, args, complain, in_decl, /*function_p=*/false,
13478 /*integral_constant_expression_p=*/true);
13479 new_specs = build_noexcept_spec (new_specs, complain);
13481 else if (specs)
13483 if (! TREE_VALUE (specs))
13484 new_specs = specs;
13485 else
13486 while (specs)
13488 tree spec;
13489 int i, len = 1;
13490 tree expanded_specs = NULL_TREE;
13492 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13494 /* Expand the pack expansion type. */
13495 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13496 args, complain,
13497 in_decl);
13499 if (expanded_specs == error_mark_node)
13500 return error_mark_node;
13501 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13502 len = TREE_VEC_LENGTH (expanded_specs);
13503 else
13505 /* We're substituting into a member template, so
13506 we got a TYPE_PACK_EXPANSION back. Add that
13507 expansion and move on. */
13508 gcc_assert (TREE_CODE (expanded_specs)
13509 == TYPE_PACK_EXPANSION);
13510 new_specs = add_exception_specifier (new_specs,
13511 expanded_specs,
13512 complain);
13513 specs = TREE_CHAIN (specs);
13514 continue;
13518 for (i = 0; i < len; ++i)
13520 if (expanded_specs)
13521 spec = TREE_VEC_ELT (expanded_specs, i);
13522 else
13523 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13524 if (spec == error_mark_node)
13525 return spec;
13526 new_specs = add_exception_specifier (new_specs, spec,
13527 complain);
13530 specs = TREE_CHAIN (specs);
13533 return new_specs;
13536 /* Take the tree structure T and replace template parameters used
13537 therein with the argument vector ARGS. IN_DECL is an associated
13538 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13539 Issue error and warning messages under control of COMPLAIN. Note
13540 that we must be relatively non-tolerant of extensions here, in
13541 order to preserve conformance; if we allow substitutions that
13542 should not be allowed, we may allow argument deductions that should
13543 not succeed, and therefore report ambiguous overload situations
13544 where there are none. In theory, we could allow the substitution,
13545 but indicate that it should have failed, and allow our caller to
13546 make sure that the right thing happens, but we don't try to do this
13547 yet.
13549 This function is used for dealing with types, decls and the like;
13550 for expressions, use tsubst_expr or tsubst_copy. */
13552 tree
13553 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13555 enum tree_code code;
13556 tree type, r = NULL_TREE;
13558 if (t == NULL_TREE || t == error_mark_node
13559 || t == integer_type_node
13560 || t == void_type_node
13561 || t == char_type_node
13562 || t == unknown_type_node
13563 || TREE_CODE (t) == NAMESPACE_DECL
13564 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13565 return t;
13567 if (DECL_P (t))
13568 return tsubst_decl (t, args, complain);
13570 if (args == NULL_TREE)
13571 return t;
13573 code = TREE_CODE (t);
13575 if (code == IDENTIFIER_NODE)
13576 type = IDENTIFIER_TYPE_VALUE (t);
13577 else
13578 type = TREE_TYPE (t);
13580 gcc_assert (type != unknown_type_node);
13582 /* Reuse typedefs. We need to do this to handle dependent attributes,
13583 such as attribute aligned. */
13584 if (TYPE_P (t)
13585 && typedef_variant_p (t))
13587 tree decl = TYPE_NAME (t);
13589 if (alias_template_specialization_p (t))
13591 /* DECL represents an alias template and we want to
13592 instantiate it. */
13593 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13594 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13595 r = instantiate_alias_template (tmpl, gen_args, complain);
13597 else if (DECL_CLASS_SCOPE_P (decl)
13598 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13599 && uses_template_parms (DECL_CONTEXT (decl)))
13601 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13602 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13603 r = retrieve_specialization (tmpl, gen_args, 0);
13605 else if (DECL_FUNCTION_SCOPE_P (decl)
13606 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13607 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13608 r = retrieve_local_specialization (decl);
13609 else
13610 /* The typedef is from a non-template context. */
13611 return t;
13613 if (r)
13615 r = TREE_TYPE (r);
13616 r = cp_build_qualified_type_real
13617 (r, cp_type_quals (t) | cp_type_quals (r),
13618 complain | tf_ignore_bad_quals);
13619 return r;
13621 else
13623 /* We don't have an instantiation yet, so drop the typedef. */
13624 int quals = cp_type_quals (t);
13625 t = DECL_ORIGINAL_TYPE (decl);
13626 t = cp_build_qualified_type_real (t, quals,
13627 complain | tf_ignore_bad_quals);
13631 bool fndecl_type = (complain & tf_fndecl_type);
13632 complain &= ~tf_fndecl_type;
13634 if (type
13635 && code != TYPENAME_TYPE
13636 && code != TEMPLATE_TYPE_PARM
13637 && code != TEMPLATE_PARM_INDEX
13638 && code != IDENTIFIER_NODE
13639 && code != FUNCTION_TYPE
13640 && code != METHOD_TYPE)
13641 type = tsubst (type, args, complain, in_decl);
13642 if (type == error_mark_node)
13643 return error_mark_node;
13645 switch (code)
13647 case RECORD_TYPE:
13648 case UNION_TYPE:
13649 case ENUMERAL_TYPE:
13650 return tsubst_aggr_type (t, args, complain, in_decl,
13651 /*entering_scope=*/0);
13653 case ERROR_MARK:
13654 case IDENTIFIER_NODE:
13655 case VOID_TYPE:
13656 case REAL_TYPE:
13657 case COMPLEX_TYPE:
13658 case VECTOR_TYPE:
13659 case BOOLEAN_TYPE:
13660 case NULLPTR_TYPE:
13661 case LANG_TYPE:
13662 return t;
13664 case INTEGER_TYPE:
13665 if (t == integer_type_node)
13666 return t;
13668 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13669 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13670 return t;
13673 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13675 max = tsubst_expr (omax, args, complain, in_decl,
13676 /*integral_constant_expression_p=*/false);
13678 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13679 needed. */
13680 if (TREE_CODE (max) == NOP_EXPR
13681 && TREE_SIDE_EFFECTS (omax)
13682 && !TREE_TYPE (max))
13683 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13685 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13686 with TREE_SIDE_EFFECTS that indicates this is not an integral
13687 constant expression. */
13688 if (processing_template_decl
13689 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13691 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13692 TREE_SIDE_EFFECTS (max) = 1;
13695 return compute_array_index_type (NULL_TREE, max, complain);
13698 case TEMPLATE_TYPE_PARM:
13699 case TEMPLATE_TEMPLATE_PARM:
13700 case BOUND_TEMPLATE_TEMPLATE_PARM:
13701 case TEMPLATE_PARM_INDEX:
13703 int idx;
13704 int level;
13705 int levels;
13706 tree arg = NULL_TREE;
13708 /* Early in template argument deduction substitution, we don't
13709 want to reduce the level of 'auto', or it will be confused
13710 with a normal template parm in subsequent deduction. */
13711 if (is_auto (t) && (complain & tf_partial))
13712 return t;
13714 r = NULL_TREE;
13716 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13717 template_parm_level_and_index (t, &level, &idx);
13719 levels = TMPL_ARGS_DEPTH (args);
13720 if (level <= levels
13721 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13723 arg = TMPL_ARG (args, level, idx);
13725 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13727 /* See through ARGUMENT_PACK_SELECT arguments. */
13728 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13729 /* If the selected argument is an expansion E, that most
13730 likely means we were called from
13731 gen_elem_of_pack_expansion_instantiation during the
13732 substituting of pack an argument pack (which Ith
13733 element is a pack expansion, where I is
13734 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13735 In this case, the Ith element resulting from this
13736 substituting is going to be a pack expansion, which
13737 pattern is the pattern of E. Let's return the
13738 pattern of E, and
13739 gen_elem_of_pack_expansion_instantiation will
13740 build the resulting pack expansion from it. */
13741 if (PACK_EXPANSION_P (arg))
13743 /* Make sure we aren't throwing away arg info. */
13744 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13745 arg = PACK_EXPANSION_PATTERN (arg);
13750 if (arg == error_mark_node)
13751 return error_mark_node;
13752 else if (arg != NULL_TREE)
13754 if (ARGUMENT_PACK_P (arg))
13755 /* If ARG is an argument pack, we don't actually want to
13756 perform a substitution here, because substitutions
13757 for argument packs are only done
13758 element-by-element. We can get to this point when
13759 substituting the type of a non-type template
13760 parameter pack, when that type actually contains
13761 template parameter packs from an outer template, e.g.,
13763 template<typename... Types> struct A {
13764 template<Types... Values> struct B { };
13765 }; */
13766 return t;
13768 if (code == TEMPLATE_TYPE_PARM)
13770 int quals;
13771 gcc_assert (TYPE_P (arg));
13773 quals = cp_type_quals (arg) | cp_type_quals (t);
13775 return cp_build_qualified_type_real
13776 (arg, quals, complain | tf_ignore_bad_quals);
13778 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13780 /* We are processing a type constructed from a
13781 template template parameter. */
13782 tree argvec = tsubst (TYPE_TI_ARGS (t),
13783 args, complain, in_decl);
13784 if (argvec == error_mark_node)
13785 return error_mark_node;
13787 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13788 || TREE_CODE (arg) == TEMPLATE_DECL
13789 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13791 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13792 /* Consider this code:
13794 template <template <class> class Template>
13795 struct Internal {
13796 template <class Arg> using Bind = Template<Arg>;
13799 template <template <class> class Template, class Arg>
13800 using Instantiate = Template<Arg>; //#0
13802 template <template <class> class Template,
13803 class Argument>
13804 using Bind =
13805 Instantiate<Internal<Template>::template Bind,
13806 Argument>; //#1
13808 When #1 is parsed, the
13809 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13810 parameter `Template' in #0 matches the
13811 UNBOUND_CLASS_TEMPLATE representing the argument
13812 `Internal<Template>::template Bind'; We then want
13813 to assemble the type `Bind<Argument>' that can't
13814 be fully created right now, because
13815 `Internal<Template>' not being complete, the Bind
13816 template cannot be looked up in that context. So
13817 we need to "store" `Bind<Argument>' for later
13818 when the context of Bind becomes complete. Let's
13819 store that in a TYPENAME_TYPE. */
13820 return make_typename_type (TYPE_CONTEXT (arg),
13821 build_nt (TEMPLATE_ID_EXPR,
13822 TYPE_IDENTIFIER (arg),
13823 argvec),
13824 typename_type,
13825 complain);
13827 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13828 are resolving nested-types in the signature of a
13829 member function templates. Otherwise ARG is a
13830 TEMPLATE_DECL and is the real template to be
13831 instantiated. */
13832 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13833 arg = TYPE_NAME (arg);
13835 r = lookup_template_class (arg,
13836 argvec, in_decl,
13837 DECL_CONTEXT (arg),
13838 /*entering_scope=*/0,
13839 complain);
13840 return cp_build_qualified_type_real
13841 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13843 else if (code == TEMPLATE_TEMPLATE_PARM)
13844 return arg;
13845 else
13846 /* TEMPLATE_PARM_INDEX. */
13847 return convert_from_reference (unshare_expr (arg));
13850 if (level == 1)
13851 /* This can happen during the attempted tsubst'ing in
13852 unify. This means that we don't yet have any information
13853 about the template parameter in question. */
13854 return t;
13856 /* If we get here, we must have been looking at a parm for a
13857 more deeply nested template. Make a new version of this
13858 template parameter, but with a lower level. */
13859 switch (code)
13861 case TEMPLATE_TYPE_PARM:
13862 case TEMPLATE_TEMPLATE_PARM:
13863 case BOUND_TEMPLATE_TEMPLATE_PARM:
13864 if (cp_type_quals (t))
13866 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13867 r = cp_build_qualified_type_real
13868 (r, cp_type_quals (t),
13869 complain | (code == TEMPLATE_TYPE_PARM
13870 ? tf_ignore_bad_quals : 0));
13872 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13873 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13874 && (r = (TEMPLATE_PARM_DESCENDANTS
13875 (TEMPLATE_TYPE_PARM_INDEX (t))))
13876 && (r = TREE_TYPE (r))
13877 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13878 /* Break infinite recursion when substituting the constraints
13879 of a constrained placeholder. */;
13880 else
13882 r = copy_type (t);
13883 TEMPLATE_TYPE_PARM_INDEX (r)
13884 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13885 r, levels, args, complain);
13886 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13887 TYPE_MAIN_VARIANT (r) = r;
13888 TYPE_POINTER_TO (r) = NULL_TREE;
13889 TYPE_REFERENCE_TO (r) = NULL_TREE;
13891 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13893 /* Propagate constraints on placeholders. */
13894 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13895 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13896 = tsubst_constraint (constr, args, complain, in_decl);
13897 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13899 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13900 pl = tsubst (pl, args, complain, in_decl);
13901 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13905 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13906 /* We have reduced the level of the template
13907 template parameter, but not the levels of its
13908 template parameters, so canonical_type_parameter
13909 will not be able to find the canonical template
13910 template parameter for this level. Thus, we
13911 require structural equality checking to compare
13912 TEMPLATE_TEMPLATE_PARMs. */
13913 SET_TYPE_STRUCTURAL_EQUALITY (r);
13914 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13915 SET_TYPE_STRUCTURAL_EQUALITY (r);
13916 else
13917 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13919 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13921 tree tinfo = TYPE_TEMPLATE_INFO (t);
13922 /* We might need to substitute into the types of non-type
13923 template parameters. */
13924 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13925 complain, in_decl);
13926 if (tmpl == error_mark_node)
13927 return error_mark_node;
13928 tree argvec = tsubst (TI_ARGS (tinfo), args,
13929 complain, in_decl);
13930 if (argvec == error_mark_node)
13931 return error_mark_node;
13933 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13934 = build_template_info (tmpl, argvec);
13937 break;
13939 case TEMPLATE_PARM_INDEX:
13940 /* OK, now substitute the type of the non-type parameter. We
13941 couldn't do it earlier because it might be an auto parameter,
13942 and we wouldn't need to if we had an argument. */
13943 type = tsubst (type, args, complain, in_decl);
13944 if (type == error_mark_node)
13945 return error_mark_node;
13946 r = reduce_template_parm_level (t, type, levels, args, complain);
13947 break;
13949 default:
13950 gcc_unreachable ();
13953 return r;
13956 case TREE_LIST:
13958 tree purpose, value, chain;
13960 if (t == void_list_node)
13961 return t;
13963 purpose = TREE_PURPOSE (t);
13964 if (purpose)
13966 purpose = tsubst (purpose, args, complain, in_decl);
13967 if (purpose == error_mark_node)
13968 return error_mark_node;
13970 value = TREE_VALUE (t);
13971 if (value)
13973 value = tsubst (value, args, complain, in_decl);
13974 if (value == error_mark_node)
13975 return error_mark_node;
13977 chain = TREE_CHAIN (t);
13978 if (chain && chain != void_type_node)
13980 chain = tsubst (chain, args, complain, in_decl);
13981 if (chain == error_mark_node)
13982 return error_mark_node;
13984 if (purpose == TREE_PURPOSE (t)
13985 && value == TREE_VALUE (t)
13986 && chain == TREE_CHAIN (t))
13987 return t;
13988 return hash_tree_cons (purpose, value, chain);
13991 case TREE_BINFO:
13992 /* We should never be tsubsting a binfo. */
13993 gcc_unreachable ();
13995 case TREE_VEC:
13996 /* A vector of template arguments. */
13997 gcc_assert (!type);
13998 return tsubst_template_args (t, args, complain, in_decl);
14000 case POINTER_TYPE:
14001 case REFERENCE_TYPE:
14003 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14004 return t;
14006 /* [temp.deduct]
14008 Type deduction may fail for any of the following
14009 reasons:
14011 -- Attempting to create a pointer to reference type.
14012 -- Attempting to create a reference to a reference type or
14013 a reference to void.
14015 Core issue 106 says that creating a reference to a reference
14016 during instantiation is no longer a cause for failure. We
14017 only enforce this check in strict C++98 mode. */
14018 if ((TREE_CODE (type) == REFERENCE_TYPE
14019 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14020 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14022 static location_t last_loc;
14024 /* We keep track of the last time we issued this error
14025 message to avoid spewing a ton of messages during a
14026 single bad template instantiation. */
14027 if (complain & tf_error
14028 && last_loc != input_location)
14030 if (VOID_TYPE_P (type))
14031 error ("forming reference to void");
14032 else if (code == POINTER_TYPE)
14033 error ("forming pointer to reference type %qT", type);
14034 else
14035 error ("forming reference to reference type %qT", type);
14036 last_loc = input_location;
14039 return error_mark_node;
14041 else if (TREE_CODE (type) == FUNCTION_TYPE
14042 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14043 || type_memfn_rqual (type) != REF_QUAL_NONE))
14045 if (complain & tf_error)
14047 if (code == POINTER_TYPE)
14048 error ("forming pointer to qualified function type %qT",
14049 type);
14050 else
14051 error ("forming reference to qualified function type %qT",
14052 type);
14054 return error_mark_node;
14056 else if (code == POINTER_TYPE)
14058 r = build_pointer_type (type);
14059 if (TREE_CODE (type) == METHOD_TYPE)
14060 r = build_ptrmemfunc_type (r);
14062 else if (TREE_CODE (type) == REFERENCE_TYPE)
14063 /* In C++0x, during template argument substitution, when there is an
14064 attempt to create a reference to a reference type, reference
14065 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14067 "If a template-argument for a template-parameter T names a type
14068 that is a reference to a type A, an attempt to create the type
14069 'lvalue reference to cv T' creates the type 'lvalue reference to
14070 A,' while an attempt to create the type type rvalue reference to
14071 cv T' creates the type T"
14073 r = cp_build_reference_type
14074 (TREE_TYPE (type),
14075 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14076 else
14077 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14078 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14080 if (r != error_mark_node)
14081 /* Will this ever be needed for TYPE_..._TO values? */
14082 layout_type (r);
14084 return r;
14086 case OFFSET_TYPE:
14088 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14089 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14091 /* [temp.deduct]
14093 Type deduction may fail for any of the following
14094 reasons:
14096 -- Attempting to create "pointer to member of T" when T
14097 is not a class type. */
14098 if (complain & tf_error)
14099 error ("creating pointer to member of non-class type %qT", r);
14100 return error_mark_node;
14102 if (TREE_CODE (type) == REFERENCE_TYPE)
14104 if (complain & tf_error)
14105 error ("creating pointer to member reference type %qT", type);
14106 return error_mark_node;
14108 if (VOID_TYPE_P (type))
14110 if (complain & tf_error)
14111 error ("creating pointer to member of type void");
14112 return error_mark_node;
14114 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14115 if (TREE_CODE (type) == FUNCTION_TYPE)
14117 /* The type of the implicit object parameter gets its
14118 cv-qualifiers from the FUNCTION_TYPE. */
14119 tree memptr;
14120 tree method_type
14121 = build_memfn_type (type, r, type_memfn_quals (type),
14122 type_memfn_rqual (type));
14123 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14124 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14125 complain);
14127 else
14128 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14129 cp_type_quals (t),
14130 complain);
14132 case FUNCTION_TYPE:
14133 case METHOD_TYPE:
14135 tree fntype;
14136 tree specs;
14137 fntype = tsubst_function_type (t, args, complain, in_decl);
14138 if (fntype == error_mark_node)
14139 return error_mark_node;
14141 /* Substitute the exception specification. */
14142 specs = tsubst_exception_specification (t, args, complain, in_decl,
14143 /*defer_ok*/fndecl_type);
14144 if (specs == error_mark_node)
14145 return error_mark_node;
14146 if (specs)
14147 fntype = build_exception_variant (fntype, specs);
14148 return fntype;
14150 case ARRAY_TYPE:
14152 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14153 if (domain == error_mark_node)
14154 return error_mark_node;
14156 /* As an optimization, we avoid regenerating the array type if
14157 it will obviously be the same as T. */
14158 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14159 return t;
14161 /* These checks should match the ones in create_array_type_for_decl.
14163 [temp.deduct]
14165 The deduction may fail for any of the following reasons:
14167 -- Attempting to create an array with an element type that
14168 is void, a function type, or a reference type, or [DR337]
14169 an abstract class type. */
14170 if (VOID_TYPE_P (type)
14171 || TREE_CODE (type) == FUNCTION_TYPE
14172 || (TREE_CODE (type) == ARRAY_TYPE
14173 && TYPE_DOMAIN (type) == NULL_TREE)
14174 || TREE_CODE (type) == REFERENCE_TYPE)
14176 if (complain & tf_error)
14177 error ("creating array of %qT", type);
14178 return error_mark_node;
14181 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14182 return error_mark_node;
14184 r = build_cplus_array_type (type, domain);
14186 if (TYPE_USER_ALIGN (t))
14188 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14189 TYPE_USER_ALIGN (r) = 1;
14192 return r;
14195 case TYPENAME_TYPE:
14197 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14198 in_decl, /*entering_scope=*/1);
14199 if (ctx == error_mark_node)
14200 return error_mark_node;
14202 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14203 complain, in_decl);
14204 if (f == error_mark_node)
14205 return error_mark_node;
14207 if (!MAYBE_CLASS_TYPE_P (ctx))
14209 if (complain & tf_error)
14210 error ("%qT is not a class, struct, or union type", ctx);
14211 return error_mark_node;
14213 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14215 /* Normally, make_typename_type does not require that the CTX
14216 have complete type in order to allow things like:
14218 template <class T> struct S { typename S<T>::X Y; };
14220 But, such constructs have already been resolved by this
14221 point, so here CTX really should have complete type, unless
14222 it's a partial instantiation. */
14223 ctx = complete_type (ctx);
14224 if (!COMPLETE_TYPE_P (ctx))
14226 if (complain & tf_error)
14227 cxx_incomplete_type_error (NULL_TREE, ctx);
14228 return error_mark_node;
14232 f = make_typename_type (ctx, f, typename_type,
14233 complain | tf_keep_type_decl);
14234 if (f == error_mark_node)
14235 return f;
14236 if (TREE_CODE (f) == TYPE_DECL)
14238 complain |= tf_ignore_bad_quals;
14239 f = TREE_TYPE (f);
14242 if (TREE_CODE (f) != TYPENAME_TYPE)
14244 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14246 if (complain & tf_error)
14247 error ("%qT resolves to %qT, which is not an enumeration type",
14248 t, f);
14249 else
14250 return error_mark_node;
14252 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14254 if (complain & tf_error)
14255 error ("%qT resolves to %qT, which is is not a class type",
14256 t, f);
14257 else
14258 return error_mark_node;
14262 return cp_build_qualified_type_real
14263 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14266 case UNBOUND_CLASS_TEMPLATE:
14268 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14269 in_decl, /*entering_scope=*/1);
14270 tree name = TYPE_IDENTIFIER (t);
14271 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14273 if (ctx == error_mark_node || name == error_mark_node)
14274 return error_mark_node;
14276 if (parm_list)
14277 parm_list = tsubst_template_parms (parm_list, args, complain);
14278 return make_unbound_class_template (ctx, name, parm_list, complain);
14281 case TYPEOF_TYPE:
14283 tree type;
14285 ++cp_unevaluated_operand;
14286 ++c_inhibit_evaluation_warnings;
14288 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14289 complain, in_decl,
14290 /*integral_constant_expression_p=*/false);
14292 --cp_unevaluated_operand;
14293 --c_inhibit_evaluation_warnings;
14295 type = finish_typeof (type);
14296 return cp_build_qualified_type_real (type,
14297 cp_type_quals (t)
14298 | cp_type_quals (type),
14299 complain);
14302 case DECLTYPE_TYPE:
14304 tree type;
14306 ++cp_unevaluated_operand;
14307 ++c_inhibit_evaluation_warnings;
14309 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14310 complain|tf_decltype, in_decl,
14311 /*function_p*/false,
14312 /*integral_constant_expression*/false);
14314 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14316 if (type == NULL_TREE)
14318 if (complain & tf_error)
14319 error ("empty initializer in lambda init-capture");
14320 type = error_mark_node;
14322 else if (TREE_CODE (type) == TREE_LIST)
14323 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14326 --cp_unevaluated_operand;
14327 --c_inhibit_evaluation_warnings;
14329 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14330 type = lambda_capture_field_type (type,
14331 DECLTYPE_FOR_INIT_CAPTURE (t),
14332 DECLTYPE_FOR_REF_CAPTURE (t));
14333 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14334 type = lambda_proxy_type (type);
14335 else
14337 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14338 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14339 && EXPR_P (type))
14340 /* In a template ~id could be either a complement expression
14341 or an unqualified-id naming a destructor; if instantiating
14342 it produces an expression, it's not an id-expression or
14343 member access. */
14344 id = false;
14345 type = finish_decltype_type (type, id, complain);
14347 return cp_build_qualified_type_real (type,
14348 cp_type_quals (t)
14349 | cp_type_quals (type),
14350 complain | tf_ignore_bad_quals);
14353 case UNDERLYING_TYPE:
14355 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14356 complain, in_decl);
14357 return finish_underlying_type (type);
14360 case TYPE_ARGUMENT_PACK:
14361 case NONTYPE_ARGUMENT_PACK:
14363 tree r;
14365 if (code == NONTYPE_ARGUMENT_PACK)
14366 r = make_node (code);
14367 else
14368 r = cxx_make_type (code);
14370 tree pack_args = ARGUMENT_PACK_ARGS (t);
14371 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14372 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14374 return r;
14377 case VOID_CST:
14378 case INTEGER_CST:
14379 case REAL_CST:
14380 case STRING_CST:
14381 case PLUS_EXPR:
14382 case MINUS_EXPR:
14383 case NEGATE_EXPR:
14384 case NOP_EXPR:
14385 case INDIRECT_REF:
14386 case ADDR_EXPR:
14387 case CALL_EXPR:
14388 case ARRAY_REF:
14389 case SCOPE_REF:
14390 /* We should use one of the expression tsubsts for these codes. */
14391 gcc_unreachable ();
14393 default:
14394 sorry ("use of %qs in template", get_tree_code_name (code));
14395 return error_mark_node;
14399 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14400 expression on the left-hand side of the "." or "->" operator. A
14401 baselink indicates a function from a base class. Both the
14402 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14403 bases of the template class, rather than the instantiated class.
14404 In addition, lookups that were not ambiguous before may be
14405 ambiguous now. Therefore, we perform the lookup again. */
14407 static tree
14408 tsubst_baselink (tree baselink, tree object_type,
14409 tree args, tsubst_flags_t complain, tree in_decl)
14411 bool qualified = BASELINK_QUALIFIED_P (baselink);
14413 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14414 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14416 tree optype = BASELINK_OPTYPE (baselink);
14417 optype = tsubst (optype, args, complain, in_decl);
14419 tree template_args = NULL_TREE;
14420 bool template_id_p = false;
14421 tree fns = BASELINK_FUNCTIONS (baselink);
14422 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14424 template_id_p = true;
14425 template_args = TREE_OPERAND (fns, 1);
14426 fns = TREE_OPERAND (fns, 0);
14427 if (template_args)
14428 template_args = tsubst_template_args (template_args, args,
14429 complain, in_decl);
14432 tree name = OVL_NAME (fns);
14433 if (IDENTIFIER_CONV_OP_P (name))
14434 name = make_conv_op_name (optype);
14436 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14437 if (!baselink)
14439 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14440 error ("cannot call constructor %<%T::%D%> directly",
14441 qualifying_scope, name);
14442 return error_mark_node;
14445 /* If lookup found a single function, mark it as used at this point.
14446 (If it lookup found multiple functions the one selected later by
14447 overload resolution will be marked as used at that point.) */
14448 if (BASELINK_P (baselink))
14449 fns = BASELINK_FUNCTIONS (baselink);
14450 if (!template_id_p && !really_overloaded_fn (fns)
14451 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14452 return error_mark_node;
14454 if (BASELINK_P (baselink))
14456 /* Add back the template arguments, if present. */
14457 if (template_id_p)
14458 BASELINK_FUNCTIONS (baselink)
14459 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14460 BASELINK_FUNCTIONS (baselink), template_args);
14462 /* Update the conversion operator type. */
14463 BASELINK_OPTYPE (baselink) = optype;
14466 if (!object_type)
14467 object_type = current_class_type;
14469 if (qualified || name == complete_dtor_identifier)
14471 baselink = adjust_result_of_qualified_name_lookup (baselink,
14472 qualifying_scope,
14473 object_type);
14474 if (!qualified)
14475 /* We need to call adjust_result_of_qualified_name_lookup in case the
14476 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14477 so that we still get virtual function binding. */
14478 BASELINK_QUALIFIED_P (baselink) = false;
14481 return baselink;
14484 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14485 true if the qualified-id will be a postfix-expression in-and-of
14486 itself; false if more of the postfix-expression follows the
14487 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14488 of "&". */
14490 static tree
14491 tsubst_qualified_id (tree qualified_id, tree args,
14492 tsubst_flags_t complain, tree in_decl,
14493 bool done, bool address_p)
14495 tree expr;
14496 tree scope;
14497 tree name;
14498 bool is_template;
14499 tree template_args;
14500 location_t loc = UNKNOWN_LOCATION;
14502 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14504 /* Figure out what name to look up. */
14505 name = TREE_OPERAND (qualified_id, 1);
14506 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14508 is_template = true;
14509 loc = EXPR_LOCATION (name);
14510 template_args = TREE_OPERAND (name, 1);
14511 if (template_args)
14512 template_args = tsubst_template_args (template_args, args,
14513 complain, in_decl);
14514 if (template_args == error_mark_node)
14515 return error_mark_node;
14516 name = TREE_OPERAND (name, 0);
14518 else
14520 is_template = false;
14521 template_args = NULL_TREE;
14524 /* Substitute into the qualifying scope. When there are no ARGS, we
14525 are just trying to simplify a non-dependent expression. In that
14526 case the qualifying scope may be dependent, and, in any case,
14527 substituting will not help. */
14528 scope = TREE_OPERAND (qualified_id, 0);
14529 if (args)
14531 scope = tsubst (scope, args, complain, in_decl);
14532 expr = tsubst_copy (name, args, complain, in_decl);
14534 else
14535 expr = name;
14537 if (dependent_scope_p (scope))
14539 if (is_template)
14540 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14541 tree r = build_qualified_name (NULL_TREE, scope, expr,
14542 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14543 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14544 return r;
14547 if (!BASELINK_P (name) && !DECL_P (expr))
14549 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14551 /* A BIT_NOT_EXPR is used to represent a destructor. */
14552 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14554 error ("qualifying type %qT does not match destructor name ~%qT",
14555 scope, TREE_OPERAND (expr, 0));
14556 expr = error_mark_node;
14558 else
14559 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14560 /*is_type_p=*/0, false);
14562 else
14563 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14564 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14565 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14567 if (complain & tf_error)
14569 error ("dependent-name %qE is parsed as a non-type, but "
14570 "instantiation yields a type", qualified_id);
14571 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14573 return error_mark_node;
14577 if (DECL_P (expr))
14579 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14580 scope);
14581 /* Remember that there was a reference to this entity. */
14582 if (!mark_used (expr, complain) && !(complain & tf_error))
14583 return error_mark_node;
14586 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14588 if (complain & tf_error)
14589 qualified_name_lookup_error (scope,
14590 TREE_OPERAND (qualified_id, 1),
14591 expr, input_location);
14592 return error_mark_node;
14595 if (is_template)
14597 if (variable_template_p (expr))
14598 expr = lookup_and_finish_template_variable (expr, template_args,
14599 complain);
14600 else
14601 expr = lookup_template_function (expr, template_args);
14604 if (expr == error_mark_node && complain & tf_error)
14605 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14606 expr, input_location);
14607 else if (TYPE_P (scope))
14609 expr = (adjust_result_of_qualified_name_lookup
14610 (expr, scope, current_nonlambda_class_type ()));
14611 expr = (finish_qualified_id_expr
14612 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14613 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14614 /*template_arg_p=*/false, complain));
14617 /* Expressions do not generally have reference type. */
14618 if (TREE_CODE (expr) != SCOPE_REF
14619 /* However, if we're about to form a pointer-to-member, we just
14620 want the referenced member referenced. */
14621 && TREE_CODE (expr) != OFFSET_REF)
14622 expr = convert_from_reference (expr);
14624 if (REF_PARENTHESIZED_P (qualified_id))
14625 expr = force_paren_expr (expr);
14627 return expr;
14630 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14631 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14632 for tsubst. */
14634 static tree
14635 tsubst_init (tree init, tree decl, tree args,
14636 tsubst_flags_t complain, tree in_decl)
14638 if (!init)
14639 return NULL_TREE;
14641 init = tsubst_expr (init, args, complain, in_decl, false);
14643 if (!init && TREE_TYPE (decl) != error_mark_node)
14645 /* If we had an initializer but it
14646 instantiated to nothing,
14647 value-initialize the object. This will
14648 only occur when the initializer was a
14649 pack expansion where the parameter packs
14650 used in that expansion were of length
14651 zero. */
14652 init = build_value_init (TREE_TYPE (decl),
14653 complain);
14654 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14655 init = get_target_expr_sfinae (init, complain);
14656 if (TREE_CODE (init) == TARGET_EXPR)
14657 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14660 return init;
14663 /* Like tsubst, but deals with expressions. This function just replaces
14664 template parms; to finish processing the resultant expression, use
14665 tsubst_copy_and_build or tsubst_expr. */
14667 static tree
14668 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14670 enum tree_code code;
14671 tree r;
14673 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14674 return t;
14676 code = TREE_CODE (t);
14678 switch (code)
14680 case PARM_DECL:
14681 r = retrieve_local_specialization (t);
14683 if (r == NULL_TREE)
14685 /* We get here for a use of 'this' in an NSDMI. */
14686 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14687 return current_class_ptr;
14689 /* This can happen for a parameter name used later in a function
14690 declaration (such as in a late-specified return type). Just
14691 make a dummy decl, since it's only used for its type. */
14692 gcc_assert (cp_unevaluated_operand != 0);
14693 r = tsubst_decl (t, args, complain);
14694 /* Give it the template pattern as its context; its true context
14695 hasn't been instantiated yet and this is good enough for
14696 mangling. */
14697 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14700 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14701 r = ARGUMENT_PACK_SELECT_ARG (r);
14702 if (!mark_used (r, complain) && !(complain & tf_error))
14703 return error_mark_node;
14704 return r;
14706 case CONST_DECL:
14708 tree enum_type;
14709 tree v;
14711 if (DECL_TEMPLATE_PARM_P (t))
14712 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14713 /* There is no need to substitute into namespace-scope
14714 enumerators. */
14715 if (DECL_NAMESPACE_SCOPE_P (t))
14716 return t;
14717 /* If ARGS is NULL, then T is known to be non-dependent. */
14718 if (args == NULL_TREE)
14719 return scalar_constant_value (t);
14721 /* Unfortunately, we cannot just call lookup_name here.
14722 Consider:
14724 template <int I> int f() {
14725 enum E { a = I };
14726 struct S { void g() { E e = a; } };
14729 When we instantiate f<7>::S::g(), say, lookup_name is not
14730 clever enough to find f<7>::a. */
14731 enum_type
14732 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14733 /*entering_scope=*/0);
14735 for (v = TYPE_VALUES (enum_type);
14736 v != NULL_TREE;
14737 v = TREE_CHAIN (v))
14738 if (TREE_PURPOSE (v) == DECL_NAME (t))
14739 return TREE_VALUE (v);
14741 /* We didn't find the name. That should never happen; if
14742 name-lookup found it during preliminary parsing, we
14743 should find it again here during instantiation. */
14744 gcc_unreachable ();
14746 return t;
14748 case FIELD_DECL:
14749 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14751 /* Check for a local specialization set up by
14752 tsubst_pack_expansion. */
14753 if (tree r = retrieve_local_specialization (t))
14755 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14756 r = ARGUMENT_PACK_SELECT_ARG (r);
14757 return r;
14760 /* When retrieving a capture pack from a generic lambda, remove the
14761 lambda call op's own template argument list from ARGS. Only the
14762 template arguments active for the closure type should be used to
14763 retrieve the pack specialization. */
14764 if (LAMBDA_FUNCTION_P (current_function_decl)
14765 && (template_class_depth (DECL_CONTEXT (t))
14766 != TMPL_ARGS_DEPTH (args)))
14767 args = strip_innermost_template_args (args, 1);
14769 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14770 tsubst_decl put in the hash table. */
14771 return retrieve_specialization (t, args, 0);
14774 if (DECL_CONTEXT (t))
14776 tree ctx;
14778 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14779 /*entering_scope=*/1);
14780 if (ctx != DECL_CONTEXT (t))
14782 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14783 if (!r)
14785 if (complain & tf_error)
14786 error ("using invalid field %qD", t);
14787 return error_mark_node;
14789 return r;
14793 return t;
14795 case VAR_DECL:
14796 case FUNCTION_DECL:
14797 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14798 r = tsubst (t, args, complain, in_decl);
14799 else if (local_variable_p (t)
14800 && uses_template_parms (DECL_CONTEXT (t)))
14802 r = retrieve_local_specialization (t);
14803 if (r == NULL_TREE)
14805 /* First try name lookup to find the instantiation. */
14806 r = lookup_name (DECL_NAME (t));
14807 if (r && !is_capture_proxy (r))
14809 /* Make sure that the one we found is the one we want. */
14810 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
14811 if (ctx != DECL_CONTEXT (r))
14812 r = NULL_TREE;
14815 if (r)
14816 /* OK */;
14817 else
14819 /* This can happen for a variable used in a
14820 late-specified return type of a local lambda, or for a
14821 local static or constant. Building a new VAR_DECL
14822 should be OK in all those cases. */
14823 r = tsubst_decl (t, args, complain);
14824 if (local_specializations)
14825 /* Avoid infinite recursion (79640). */
14826 register_local_specialization (r, t);
14827 if (decl_maybe_constant_var_p (r))
14829 /* We can't call cp_finish_decl, so handle the
14830 initializer by hand. */
14831 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14832 complain, in_decl);
14833 if (!processing_template_decl)
14834 init = maybe_constant_init (init);
14835 if (processing_template_decl
14836 ? potential_constant_expression (init)
14837 : reduced_constant_expression_p (init))
14838 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14839 = TREE_CONSTANT (r) = true;
14840 DECL_INITIAL (r) = init;
14841 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14842 TREE_TYPE (r)
14843 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14844 complain, adc_variable_type);
14846 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14847 || decl_constant_var_p (r)
14848 || errorcount || sorrycount);
14849 if (!processing_template_decl
14850 && !TREE_STATIC (r))
14851 r = process_outer_var_ref (r, complain);
14853 /* Remember this for subsequent uses. */
14854 if (local_specializations)
14855 register_local_specialization (r, t);
14858 else
14859 r = t;
14860 if (!mark_used (r, complain))
14861 return error_mark_node;
14862 return r;
14864 case NAMESPACE_DECL:
14865 return t;
14867 case OVERLOAD:
14868 /* An OVERLOAD will always be a non-dependent overload set; an
14869 overload set from function scope will just be represented with an
14870 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14871 gcc_assert (!uses_template_parms (t));
14872 /* We must have marked any lookups as persistent. */
14873 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14874 return t;
14876 case BASELINK:
14877 return tsubst_baselink (t, current_nonlambda_class_type (),
14878 args, complain, in_decl);
14880 case TEMPLATE_DECL:
14881 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14882 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14883 args, complain, in_decl);
14884 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14885 return tsubst (t, args, complain, in_decl);
14886 else if (DECL_CLASS_SCOPE_P (t)
14887 && uses_template_parms (DECL_CONTEXT (t)))
14889 /* Template template argument like the following example need
14890 special treatment:
14892 template <template <class> class TT> struct C {};
14893 template <class T> struct D {
14894 template <class U> struct E {};
14895 C<E> c; // #1
14897 D<int> d; // #2
14899 We are processing the template argument `E' in #1 for
14900 the template instantiation #2. Originally, `E' is a
14901 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14902 have to substitute this with one having context `D<int>'. */
14904 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14905 if (dependent_scope_p (context))
14907 /* When rewriting a constructor into a deduction guide, a
14908 non-dependent name can become dependent, so memtmpl<args>
14909 becomes context::template memtmpl<args>. */
14910 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14911 return build_qualified_name (type, context, DECL_NAME (t),
14912 /*template*/true);
14914 return lookup_field (context, DECL_NAME(t), 0, false);
14916 else
14917 /* Ordinary template template argument. */
14918 return t;
14920 case CAST_EXPR:
14921 case REINTERPRET_CAST_EXPR:
14922 case CONST_CAST_EXPR:
14923 case STATIC_CAST_EXPR:
14924 case DYNAMIC_CAST_EXPR:
14925 case IMPLICIT_CONV_EXPR:
14926 case CONVERT_EXPR:
14927 case NOP_EXPR:
14929 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14930 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14931 return build1 (code, type, op0);
14934 case SIZEOF_EXPR:
14935 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14936 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14938 tree expanded, op = TREE_OPERAND (t, 0);
14939 int len = 0;
14941 if (SIZEOF_EXPR_TYPE_P (t))
14942 op = TREE_TYPE (op);
14944 ++cp_unevaluated_operand;
14945 ++c_inhibit_evaluation_warnings;
14946 /* We only want to compute the number of arguments. */
14947 if (PACK_EXPANSION_P (op))
14948 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14949 else
14950 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14951 args, complain, in_decl);
14952 --cp_unevaluated_operand;
14953 --c_inhibit_evaluation_warnings;
14955 if (TREE_CODE (expanded) == TREE_VEC)
14957 len = TREE_VEC_LENGTH (expanded);
14958 /* Set TREE_USED for the benefit of -Wunused. */
14959 for (int i = 0; i < len; i++)
14960 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14961 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14964 if (expanded == error_mark_node)
14965 return error_mark_node;
14966 else if (PACK_EXPANSION_P (expanded)
14967 || (TREE_CODE (expanded) == TREE_VEC
14968 && pack_expansion_args_count (expanded)))
14971 if (PACK_EXPANSION_P (expanded))
14972 /* OK. */;
14973 else if (TREE_VEC_LENGTH (expanded) == 1)
14974 expanded = TREE_VEC_ELT (expanded, 0);
14975 else
14976 expanded = make_argument_pack (expanded);
14978 if (TYPE_P (expanded))
14979 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14980 complain & tf_error);
14981 else
14982 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14983 complain & tf_error);
14985 else
14986 return build_int_cst (size_type_node, len);
14988 if (SIZEOF_EXPR_TYPE_P (t))
14990 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14991 args, complain, in_decl);
14992 r = build1 (NOP_EXPR, r, error_mark_node);
14993 r = build1 (SIZEOF_EXPR,
14994 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14995 SIZEOF_EXPR_TYPE_P (r) = 1;
14996 return r;
14998 /* Fall through */
15000 case INDIRECT_REF:
15001 case NEGATE_EXPR:
15002 case TRUTH_NOT_EXPR:
15003 case BIT_NOT_EXPR:
15004 case ADDR_EXPR:
15005 case UNARY_PLUS_EXPR: /* Unary + */
15006 case ALIGNOF_EXPR:
15007 case AT_ENCODE_EXPR:
15008 case ARROW_EXPR:
15009 case THROW_EXPR:
15010 case TYPEID_EXPR:
15011 case REALPART_EXPR:
15012 case IMAGPART_EXPR:
15013 case PAREN_EXPR:
15015 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15016 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15017 return build1 (code, type, op0);
15020 case COMPONENT_REF:
15022 tree object;
15023 tree name;
15025 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15026 name = TREE_OPERAND (t, 1);
15027 if (TREE_CODE (name) == BIT_NOT_EXPR)
15029 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15030 complain, in_decl);
15031 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15033 else if (TREE_CODE (name) == SCOPE_REF
15034 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15036 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15037 complain, in_decl);
15038 name = TREE_OPERAND (name, 1);
15039 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15040 complain, in_decl);
15041 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15042 name = build_qualified_name (/*type=*/NULL_TREE,
15043 base, name,
15044 /*template_p=*/false);
15046 else if (BASELINK_P (name))
15047 name = tsubst_baselink (name,
15048 non_reference (TREE_TYPE (object)),
15049 args, complain,
15050 in_decl);
15051 else
15052 name = tsubst_copy (name, args, complain, in_decl);
15053 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15056 case PLUS_EXPR:
15057 case MINUS_EXPR:
15058 case MULT_EXPR:
15059 case TRUNC_DIV_EXPR:
15060 case CEIL_DIV_EXPR:
15061 case FLOOR_DIV_EXPR:
15062 case ROUND_DIV_EXPR:
15063 case EXACT_DIV_EXPR:
15064 case BIT_AND_EXPR:
15065 case BIT_IOR_EXPR:
15066 case BIT_XOR_EXPR:
15067 case TRUNC_MOD_EXPR:
15068 case FLOOR_MOD_EXPR:
15069 case TRUTH_ANDIF_EXPR:
15070 case TRUTH_ORIF_EXPR:
15071 case TRUTH_AND_EXPR:
15072 case TRUTH_OR_EXPR:
15073 case RSHIFT_EXPR:
15074 case LSHIFT_EXPR:
15075 case RROTATE_EXPR:
15076 case LROTATE_EXPR:
15077 case EQ_EXPR:
15078 case NE_EXPR:
15079 case MAX_EXPR:
15080 case MIN_EXPR:
15081 case LE_EXPR:
15082 case GE_EXPR:
15083 case LT_EXPR:
15084 case GT_EXPR:
15085 case COMPOUND_EXPR:
15086 case DOTSTAR_EXPR:
15087 case MEMBER_REF:
15088 case PREDECREMENT_EXPR:
15089 case PREINCREMENT_EXPR:
15090 case POSTDECREMENT_EXPR:
15091 case POSTINCREMENT_EXPR:
15093 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15094 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15095 return build_nt (code, op0, op1);
15098 case SCOPE_REF:
15100 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15101 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15102 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15103 QUALIFIED_NAME_IS_TEMPLATE (t));
15106 case ARRAY_REF:
15108 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15109 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15110 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15113 case CALL_EXPR:
15115 int n = VL_EXP_OPERAND_LENGTH (t);
15116 tree result = build_vl_exp (CALL_EXPR, n);
15117 int i;
15118 for (i = 0; i < n; i++)
15119 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15120 complain, in_decl);
15121 return result;
15124 case COND_EXPR:
15125 case MODOP_EXPR:
15126 case PSEUDO_DTOR_EXPR:
15127 case VEC_PERM_EXPR:
15129 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15130 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15131 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15132 r = build_nt (code, op0, op1, op2);
15133 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15134 return r;
15137 case NEW_EXPR:
15139 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15140 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15141 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15142 r = build_nt (code, op0, op1, op2);
15143 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15144 return r;
15147 case DELETE_EXPR:
15149 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15150 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15151 r = build_nt (code, op0, op1);
15152 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15153 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15154 return r;
15157 case TEMPLATE_ID_EXPR:
15159 /* Substituted template arguments */
15160 tree fn = TREE_OPERAND (t, 0);
15161 tree targs = TREE_OPERAND (t, 1);
15163 fn = tsubst_copy (fn, args, complain, in_decl);
15164 if (targs)
15165 targs = tsubst_template_args (targs, args, complain, in_decl);
15167 return lookup_template_function (fn, targs);
15170 case TREE_LIST:
15172 tree purpose, value, chain;
15174 if (t == void_list_node)
15175 return t;
15177 purpose = TREE_PURPOSE (t);
15178 if (purpose)
15179 purpose = tsubst_copy (purpose, args, complain, in_decl);
15180 value = TREE_VALUE (t);
15181 if (value)
15182 value = tsubst_copy (value, args, complain, in_decl);
15183 chain = TREE_CHAIN (t);
15184 if (chain && chain != void_type_node)
15185 chain = tsubst_copy (chain, args, complain, in_decl);
15186 if (purpose == TREE_PURPOSE (t)
15187 && value == TREE_VALUE (t)
15188 && chain == TREE_CHAIN (t))
15189 return t;
15190 return tree_cons (purpose, value, chain);
15193 case RECORD_TYPE:
15194 case UNION_TYPE:
15195 case ENUMERAL_TYPE:
15196 case INTEGER_TYPE:
15197 case TEMPLATE_TYPE_PARM:
15198 case TEMPLATE_TEMPLATE_PARM:
15199 case BOUND_TEMPLATE_TEMPLATE_PARM:
15200 case TEMPLATE_PARM_INDEX:
15201 case POINTER_TYPE:
15202 case REFERENCE_TYPE:
15203 case OFFSET_TYPE:
15204 case FUNCTION_TYPE:
15205 case METHOD_TYPE:
15206 case ARRAY_TYPE:
15207 case TYPENAME_TYPE:
15208 case UNBOUND_CLASS_TEMPLATE:
15209 case TYPEOF_TYPE:
15210 case DECLTYPE_TYPE:
15211 case TYPE_DECL:
15212 return tsubst (t, args, complain, in_decl);
15214 case USING_DECL:
15215 t = DECL_NAME (t);
15216 /* Fall through. */
15217 case IDENTIFIER_NODE:
15218 if (IDENTIFIER_CONV_OP_P (t))
15220 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15221 return make_conv_op_name (new_type);
15223 else
15224 return t;
15226 case CONSTRUCTOR:
15227 /* This is handled by tsubst_copy_and_build. */
15228 gcc_unreachable ();
15230 case VA_ARG_EXPR:
15232 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15233 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15234 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15237 case CLEANUP_POINT_EXPR:
15238 /* We shouldn't have built any of these during initial template
15239 generation. Instead, they should be built during instantiation
15240 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15241 gcc_unreachable ();
15243 case OFFSET_REF:
15245 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15246 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15247 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15248 r = build2 (code, type, op0, op1);
15249 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15250 if (!mark_used (TREE_OPERAND (r, 1), complain)
15251 && !(complain & tf_error))
15252 return error_mark_node;
15253 return r;
15256 case EXPR_PACK_EXPANSION:
15257 error ("invalid use of pack expansion expression");
15258 return error_mark_node;
15260 case NONTYPE_ARGUMENT_PACK:
15261 error ("use %<...%> to expand argument pack");
15262 return error_mark_node;
15264 case VOID_CST:
15265 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15266 return t;
15268 case INTEGER_CST:
15269 case REAL_CST:
15270 case STRING_CST:
15271 case COMPLEX_CST:
15273 /* Instantiate any typedefs in the type. */
15274 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15275 r = fold_convert (type, t);
15276 gcc_assert (TREE_CODE (r) == code);
15277 return r;
15280 case PTRMEM_CST:
15281 /* These can sometimes show up in a partial instantiation, but never
15282 involve template parms. */
15283 gcc_assert (!uses_template_parms (t));
15284 return t;
15286 case UNARY_LEFT_FOLD_EXPR:
15287 return tsubst_unary_left_fold (t, args, complain, in_decl);
15288 case UNARY_RIGHT_FOLD_EXPR:
15289 return tsubst_unary_right_fold (t, args, complain, in_decl);
15290 case BINARY_LEFT_FOLD_EXPR:
15291 return tsubst_binary_left_fold (t, args, complain, in_decl);
15292 case BINARY_RIGHT_FOLD_EXPR:
15293 return tsubst_binary_right_fold (t, args, complain, in_decl);
15294 case PREDICT_EXPR:
15295 return t;
15297 default:
15298 /* We shouldn't get here, but keep going if !flag_checking. */
15299 if (flag_checking)
15300 gcc_unreachable ();
15301 return t;
15305 /* Helper function for tsubst_omp_clauses, used for instantiation of
15306 OMP_CLAUSE_DECL of clauses. */
15308 static tree
15309 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15310 tree in_decl)
15312 if (decl == NULL_TREE)
15313 return NULL_TREE;
15315 /* Handle an OpenMP array section represented as a TREE_LIST (or
15316 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15317 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15318 TREE_LIST. We can handle it exactly the same as an array section
15319 (purpose, value, and a chain), even though the nomenclature
15320 (low_bound, length, etc) is different. */
15321 if (TREE_CODE (decl) == TREE_LIST)
15323 tree low_bound
15324 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15325 /*integral_constant_expression_p=*/false);
15326 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15327 /*integral_constant_expression_p=*/false);
15328 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15329 in_decl);
15330 if (TREE_PURPOSE (decl) == low_bound
15331 && TREE_VALUE (decl) == length
15332 && TREE_CHAIN (decl) == chain)
15333 return decl;
15334 tree ret = tree_cons (low_bound, length, chain);
15335 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15336 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15337 return ret;
15339 tree ret = tsubst_expr (decl, args, complain, in_decl,
15340 /*integral_constant_expression_p=*/false);
15341 /* Undo convert_from_reference tsubst_expr could have called. */
15342 if (decl
15343 && REFERENCE_REF_P (ret)
15344 && !REFERENCE_REF_P (decl))
15345 ret = TREE_OPERAND (ret, 0);
15346 return ret;
15349 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15351 static tree
15352 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15353 tree args, tsubst_flags_t complain, tree in_decl)
15355 tree new_clauses = NULL_TREE, nc, oc;
15356 tree linear_no_step = NULL_TREE;
15358 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15360 nc = copy_node (oc);
15361 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15362 new_clauses = nc;
15364 switch (OMP_CLAUSE_CODE (nc))
15366 case OMP_CLAUSE_LASTPRIVATE:
15367 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15369 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15370 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15371 in_decl, /*integral_constant_expression_p=*/false);
15372 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15373 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15375 /* FALLTHRU */
15376 case OMP_CLAUSE_PRIVATE:
15377 case OMP_CLAUSE_SHARED:
15378 case OMP_CLAUSE_FIRSTPRIVATE:
15379 case OMP_CLAUSE_COPYIN:
15380 case OMP_CLAUSE_COPYPRIVATE:
15381 case OMP_CLAUSE_UNIFORM:
15382 case OMP_CLAUSE_DEPEND:
15383 case OMP_CLAUSE_FROM:
15384 case OMP_CLAUSE_TO:
15385 case OMP_CLAUSE_MAP:
15386 case OMP_CLAUSE_USE_DEVICE_PTR:
15387 case OMP_CLAUSE_IS_DEVICE_PTR:
15388 OMP_CLAUSE_DECL (nc)
15389 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15390 in_decl);
15391 break;
15392 case OMP_CLAUSE_TILE:
15393 case OMP_CLAUSE_IF:
15394 case OMP_CLAUSE_NUM_THREADS:
15395 case OMP_CLAUSE_SCHEDULE:
15396 case OMP_CLAUSE_COLLAPSE:
15397 case OMP_CLAUSE_FINAL:
15398 case OMP_CLAUSE_DEVICE:
15399 case OMP_CLAUSE_DIST_SCHEDULE:
15400 case OMP_CLAUSE_NUM_TEAMS:
15401 case OMP_CLAUSE_THREAD_LIMIT:
15402 case OMP_CLAUSE_SAFELEN:
15403 case OMP_CLAUSE_SIMDLEN:
15404 case OMP_CLAUSE_NUM_TASKS:
15405 case OMP_CLAUSE_GRAINSIZE:
15406 case OMP_CLAUSE_PRIORITY:
15407 case OMP_CLAUSE_ORDERED:
15408 case OMP_CLAUSE_HINT:
15409 case OMP_CLAUSE_NUM_GANGS:
15410 case OMP_CLAUSE_NUM_WORKERS:
15411 case OMP_CLAUSE_VECTOR_LENGTH:
15412 case OMP_CLAUSE_WORKER:
15413 case OMP_CLAUSE_VECTOR:
15414 case OMP_CLAUSE_ASYNC:
15415 case OMP_CLAUSE_WAIT:
15416 OMP_CLAUSE_OPERAND (nc, 0)
15417 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15418 in_decl, /*integral_constant_expression_p=*/false);
15419 break;
15420 case OMP_CLAUSE_REDUCTION:
15421 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15423 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15424 if (TREE_CODE (placeholder) == SCOPE_REF)
15426 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15427 complain, in_decl);
15428 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15429 = build_qualified_name (NULL_TREE, scope,
15430 TREE_OPERAND (placeholder, 1),
15431 false);
15433 else
15434 gcc_assert (identifier_p (placeholder));
15436 OMP_CLAUSE_DECL (nc)
15437 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15438 in_decl);
15439 break;
15440 case OMP_CLAUSE_GANG:
15441 case OMP_CLAUSE_ALIGNED:
15442 OMP_CLAUSE_DECL (nc)
15443 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15444 in_decl);
15445 OMP_CLAUSE_OPERAND (nc, 1)
15446 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15447 in_decl, /*integral_constant_expression_p=*/false);
15448 break;
15449 case OMP_CLAUSE_LINEAR:
15450 OMP_CLAUSE_DECL (nc)
15451 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15452 in_decl);
15453 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15455 gcc_assert (!linear_no_step);
15456 linear_no_step = nc;
15458 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15459 OMP_CLAUSE_LINEAR_STEP (nc)
15460 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15461 complain, in_decl);
15462 else
15463 OMP_CLAUSE_LINEAR_STEP (nc)
15464 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15465 in_decl,
15466 /*integral_constant_expression_p=*/false);
15467 break;
15468 case OMP_CLAUSE_NOWAIT:
15469 case OMP_CLAUSE_DEFAULT:
15470 case OMP_CLAUSE_UNTIED:
15471 case OMP_CLAUSE_MERGEABLE:
15472 case OMP_CLAUSE_INBRANCH:
15473 case OMP_CLAUSE_NOTINBRANCH:
15474 case OMP_CLAUSE_PROC_BIND:
15475 case OMP_CLAUSE_FOR:
15476 case OMP_CLAUSE_PARALLEL:
15477 case OMP_CLAUSE_SECTIONS:
15478 case OMP_CLAUSE_TASKGROUP:
15479 case OMP_CLAUSE_NOGROUP:
15480 case OMP_CLAUSE_THREADS:
15481 case OMP_CLAUSE_SIMD:
15482 case OMP_CLAUSE_DEFAULTMAP:
15483 case OMP_CLAUSE_INDEPENDENT:
15484 case OMP_CLAUSE_AUTO:
15485 case OMP_CLAUSE_SEQ:
15486 break;
15487 default:
15488 gcc_unreachable ();
15490 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15491 switch (OMP_CLAUSE_CODE (nc))
15493 case OMP_CLAUSE_SHARED:
15494 case OMP_CLAUSE_PRIVATE:
15495 case OMP_CLAUSE_FIRSTPRIVATE:
15496 case OMP_CLAUSE_LASTPRIVATE:
15497 case OMP_CLAUSE_COPYPRIVATE:
15498 case OMP_CLAUSE_LINEAR:
15499 case OMP_CLAUSE_REDUCTION:
15500 case OMP_CLAUSE_USE_DEVICE_PTR:
15501 case OMP_CLAUSE_IS_DEVICE_PTR:
15502 /* tsubst_expr on SCOPE_REF results in returning
15503 finish_non_static_data_member result. Undo that here. */
15504 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15505 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15506 == IDENTIFIER_NODE))
15508 tree t = OMP_CLAUSE_DECL (nc);
15509 tree v = t;
15510 while (v)
15511 switch (TREE_CODE (v))
15513 case COMPONENT_REF:
15514 case MEM_REF:
15515 case INDIRECT_REF:
15516 CASE_CONVERT:
15517 case POINTER_PLUS_EXPR:
15518 v = TREE_OPERAND (v, 0);
15519 continue;
15520 case PARM_DECL:
15521 if (DECL_CONTEXT (v) == current_function_decl
15522 && DECL_ARTIFICIAL (v)
15523 && DECL_NAME (v) == this_identifier)
15524 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15525 /* FALLTHRU */
15526 default:
15527 v = NULL_TREE;
15528 break;
15531 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15532 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15533 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15534 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15535 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15537 tree decl = OMP_CLAUSE_DECL (nc);
15538 if (VAR_P (decl))
15540 retrofit_lang_decl (decl);
15541 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15544 break;
15545 default:
15546 break;
15550 new_clauses = nreverse (new_clauses);
15551 if (ort != C_ORT_OMP_DECLARE_SIMD)
15553 new_clauses = finish_omp_clauses (new_clauses, ort);
15554 if (linear_no_step)
15555 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15556 if (nc == linear_no_step)
15558 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15559 break;
15562 return new_clauses;
15565 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15567 static tree
15568 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15569 tree in_decl)
15571 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15573 tree purpose, value, chain;
15575 if (t == NULL)
15576 return t;
15578 if (TREE_CODE (t) != TREE_LIST)
15579 return tsubst_copy_and_build (t, args, complain, in_decl,
15580 /*function_p=*/false,
15581 /*integral_constant_expression_p=*/false);
15583 if (t == void_list_node)
15584 return t;
15586 purpose = TREE_PURPOSE (t);
15587 if (purpose)
15588 purpose = RECUR (purpose);
15589 value = TREE_VALUE (t);
15590 if (value)
15592 if (TREE_CODE (value) != LABEL_DECL)
15593 value = RECUR (value);
15594 else
15596 value = lookup_label (DECL_NAME (value));
15597 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15598 TREE_USED (value) = 1;
15601 chain = TREE_CHAIN (t);
15602 if (chain && chain != void_type_node)
15603 chain = RECUR (chain);
15604 return tree_cons (purpose, value, chain);
15605 #undef RECUR
15608 /* Used to temporarily communicate the list of #pragma omp parallel
15609 clauses to #pragma omp for instantiation if they are combined
15610 together. */
15612 static tree *omp_parallel_combined_clauses;
15614 /* Substitute one OMP_FOR iterator. */
15616 static void
15617 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15618 tree initv, tree condv, tree incrv, tree *clauses,
15619 tree args, tsubst_flags_t complain, tree in_decl,
15620 bool integral_constant_expression_p)
15622 #define RECUR(NODE) \
15623 tsubst_expr ((NODE), args, complain, in_decl, \
15624 integral_constant_expression_p)
15625 tree decl, init, cond, incr;
15627 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15628 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15630 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15632 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15633 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15636 decl = TREE_OPERAND (init, 0);
15637 init = TREE_OPERAND (init, 1);
15638 tree decl_expr = NULL_TREE;
15639 if (init && TREE_CODE (init) == DECL_EXPR)
15641 /* We need to jump through some hoops to handle declarations in the
15642 init-statement, since we might need to handle auto deduction,
15643 but we need to keep control of initialization. */
15644 decl_expr = init;
15645 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15646 decl = tsubst_decl (decl, args, complain);
15648 else
15650 if (TREE_CODE (decl) == SCOPE_REF)
15652 decl = RECUR (decl);
15653 if (TREE_CODE (decl) == COMPONENT_REF)
15655 tree v = decl;
15656 while (v)
15657 switch (TREE_CODE (v))
15659 case COMPONENT_REF:
15660 case MEM_REF:
15661 case INDIRECT_REF:
15662 CASE_CONVERT:
15663 case POINTER_PLUS_EXPR:
15664 v = TREE_OPERAND (v, 0);
15665 continue;
15666 case PARM_DECL:
15667 if (DECL_CONTEXT (v) == current_function_decl
15668 && DECL_ARTIFICIAL (v)
15669 && DECL_NAME (v) == this_identifier)
15671 decl = TREE_OPERAND (decl, 1);
15672 decl = omp_privatize_field (decl, false);
15674 /* FALLTHRU */
15675 default:
15676 v = NULL_TREE;
15677 break;
15681 else
15682 decl = RECUR (decl);
15684 init = RECUR (init);
15686 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15687 if (auto_node && init)
15688 TREE_TYPE (decl)
15689 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15691 gcc_assert (!type_dependent_expression_p (decl));
15693 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15695 if (decl_expr)
15697 /* Declare the variable, but don't let that initialize it. */
15698 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15699 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15700 RECUR (decl_expr);
15701 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15704 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15705 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15706 if (TREE_CODE (incr) == MODIFY_EXPR)
15708 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15709 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15710 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15711 NOP_EXPR, rhs, complain);
15713 else
15714 incr = RECUR (incr);
15715 TREE_VEC_ELT (declv, i) = decl;
15716 TREE_VEC_ELT (initv, i) = init;
15717 TREE_VEC_ELT (condv, i) = cond;
15718 TREE_VEC_ELT (incrv, i) = incr;
15719 return;
15722 if (decl_expr)
15724 /* Declare and initialize the variable. */
15725 RECUR (decl_expr);
15726 init = NULL_TREE;
15728 else if (init)
15730 tree *pc;
15731 int j;
15732 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15734 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15736 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15737 && OMP_CLAUSE_DECL (*pc) == decl)
15738 break;
15739 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15740 && OMP_CLAUSE_DECL (*pc) == decl)
15742 if (j)
15743 break;
15744 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15745 tree c = *pc;
15746 *pc = OMP_CLAUSE_CHAIN (c);
15747 OMP_CLAUSE_CHAIN (c) = *clauses;
15748 *clauses = c;
15750 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15751 && OMP_CLAUSE_DECL (*pc) == decl)
15753 error ("iteration variable %qD should not be firstprivate",
15754 decl);
15755 *pc = OMP_CLAUSE_CHAIN (*pc);
15757 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15758 && OMP_CLAUSE_DECL (*pc) == decl)
15760 error ("iteration variable %qD should not be reduction",
15761 decl);
15762 *pc = OMP_CLAUSE_CHAIN (*pc);
15764 else
15765 pc = &OMP_CLAUSE_CHAIN (*pc);
15767 if (*pc)
15768 break;
15770 if (*pc == NULL_TREE)
15772 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15773 OMP_CLAUSE_DECL (c) = decl;
15774 c = finish_omp_clauses (c, C_ORT_OMP);
15775 if (c)
15777 OMP_CLAUSE_CHAIN (c) = *clauses;
15778 *clauses = c;
15782 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15783 if (COMPARISON_CLASS_P (cond))
15785 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15786 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15787 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15789 else
15790 cond = RECUR (cond);
15791 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15792 switch (TREE_CODE (incr))
15794 case PREINCREMENT_EXPR:
15795 case PREDECREMENT_EXPR:
15796 case POSTINCREMENT_EXPR:
15797 case POSTDECREMENT_EXPR:
15798 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15799 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15800 break;
15801 case MODIFY_EXPR:
15802 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15803 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15805 tree rhs = TREE_OPERAND (incr, 1);
15806 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15807 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15808 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15809 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15810 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15811 rhs0, rhs1));
15813 else
15814 incr = RECUR (incr);
15815 break;
15816 case MODOP_EXPR:
15817 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15818 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15820 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15821 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15822 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15823 TREE_TYPE (decl), lhs,
15824 RECUR (TREE_OPERAND (incr, 2))));
15826 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15827 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15828 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15830 tree rhs = TREE_OPERAND (incr, 2);
15831 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15832 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15833 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15834 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15835 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15836 rhs0, rhs1));
15838 else
15839 incr = RECUR (incr);
15840 break;
15841 default:
15842 incr = RECUR (incr);
15843 break;
15846 TREE_VEC_ELT (declv, i) = decl;
15847 TREE_VEC_ELT (initv, i) = init;
15848 TREE_VEC_ELT (condv, i) = cond;
15849 TREE_VEC_ELT (incrv, i) = incr;
15850 #undef RECUR
15853 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15854 of OMP_TARGET's body. */
15856 static tree
15857 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15859 *walk_subtrees = 0;
15860 switch (TREE_CODE (*tp))
15862 case OMP_TEAMS:
15863 return *tp;
15864 case BIND_EXPR:
15865 case STATEMENT_LIST:
15866 *walk_subtrees = 1;
15867 break;
15868 default:
15869 break;
15871 return NULL_TREE;
15874 /* Helper function for tsubst_expr. For decomposition declaration
15875 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15876 also the corresponding decls representing the identifiers
15877 of the decomposition declaration. Return DECL if successful
15878 or error_mark_node otherwise, set *FIRST to the first decl
15879 in the list chained through DECL_CHAIN and *CNT to the number
15880 of such decls. */
15882 static tree
15883 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15884 tsubst_flags_t complain, tree in_decl, tree *first,
15885 unsigned int *cnt)
15887 tree decl2, decl3, prev = decl;
15888 *cnt = 0;
15889 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15890 for (decl2 = DECL_CHAIN (pattern_decl);
15891 decl2
15892 && VAR_P (decl2)
15893 && DECL_DECOMPOSITION_P (decl2)
15894 && DECL_NAME (decl2);
15895 decl2 = DECL_CHAIN (decl2))
15897 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15899 gcc_assert (errorcount);
15900 return error_mark_node;
15902 (*cnt)++;
15903 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15904 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15905 tree v = DECL_VALUE_EXPR (decl2);
15906 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15907 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15908 decl3 = tsubst (decl2, args, complain, in_decl);
15909 SET_DECL_VALUE_EXPR (decl2, v);
15910 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15911 if (VAR_P (decl3))
15912 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15913 maybe_push_decl (decl3);
15914 if (error_operand_p (decl3))
15915 decl = error_mark_node;
15916 else if (decl != error_mark_node
15917 && DECL_CHAIN (decl3) != prev)
15919 gcc_assert (errorcount);
15920 decl = error_mark_node;
15922 else
15923 prev = decl3;
15925 *first = prev;
15926 return decl;
15929 /* Like tsubst_copy for expressions, etc. but also does semantic
15930 processing. */
15932 tree
15933 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15934 bool integral_constant_expression_p)
15936 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15937 #define RECUR(NODE) \
15938 tsubst_expr ((NODE), args, complain, in_decl, \
15939 integral_constant_expression_p)
15941 tree stmt, tmp;
15942 tree r;
15943 location_t loc;
15945 if (t == NULL_TREE || t == error_mark_node)
15946 return t;
15948 loc = input_location;
15949 if (EXPR_HAS_LOCATION (t))
15950 input_location = EXPR_LOCATION (t);
15951 if (STATEMENT_CODE_P (TREE_CODE (t)))
15952 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15954 switch (TREE_CODE (t))
15956 case STATEMENT_LIST:
15958 tree_stmt_iterator i;
15959 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15960 RECUR (tsi_stmt (i));
15961 break;
15964 case CTOR_INITIALIZER:
15965 finish_mem_initializers (tsubst_initializer_list
15966 (TREE_OPERAND (t, 0), args));
15967 break;
15969 case RETURN_EXPR:
15970 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15971 break;
15973 case EXPR_STMT:
15974 tmp = RECUR (EXPR_STMT_EXPR (t));
15975 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15976 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15977 else
15978 finish_expr_stmt (tmp);
15979 break;
15981 case USING_STMT:
15982 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15983 /*attribs=*/NULL_TREE);
15984 break;
15986 case DECL_EXPR:
15988 tree decl, pattern_decl;
15989 tree init;
15991 pattern_decl = decl = DECL_EXPR_DECL (t);
15992 if (TREE_CODE (decl) == LABEL_DECL)
15993 finish_label_decl (DECL_NAME (decl));
15994 else if (TREE_CODE (decl) == USING_DECL)
15996 tree scope = USING_DECL_SCOPE (decl);
15997 tree name = DECL_NAME (decl);
15999 scope = tsubst (scope, args, complain, in_decl);
16000 decl = lookup_qualified_name (scope, name,
16001 /*is_type_p=*/false,
16002 /*complain=*/false);
16003 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16004 qualified_name_lookup_error (scope, name, decl, input_location);
16005 else
16006 finish_local_using_decl (decl, scope, name);
16008 else if (DECL_PACK_P (decl))
16010 /* Don't build up decls for a variadic capture proxy, we'll
16011 instantiate the elements directly as needed. */
16012 break;
16014 else if (is_capture_proxy (decl)
16015 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16017 /* We're in tsubst_lambda_expr, we've already inserted a new
16018 capture proxy, so look it up and register it. */
16019 tree inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16020 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16021 gcc_assert (inst != decl && is_capture_proxy (inst));
16022 register_local_specialization (inst, decl);
16023 break;
16025 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16026 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16027 /* Don't copy the old closure; we'll create a new one in
16028 tsubst_lambda_expr. */
16029 break;
16030 else
16032 init = DECL_INITIAL (decl);
16033 decl = tsubst (decl, args, complain, in_decl);
16034 if (decl != error_mark_node)
16036 /* By marking the declaration as instantiated, we avoid
16037 trying to instantiate it. Since instantiate_decl can't
16038 handle local variables, and since we've already done
16039 all that needs to be done, that's the right thing to
16040 do. */
16041 if (VAR_P (decl))
16042 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16043 if (VAR_P (decl)
16044 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16045 /* Anonymous aggregates are a special case. */
16046 finish_anon_union (decl);
16047 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16049 DECL_CONTEXT (decl) = current_function_decl;
16050 if (DECL_NAME (decl) == this_identifier)
16052 tree lam = DECL_CONTEXT (current_function_decl);
16053 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16054 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16056 insert_capture_proxy (decl);
16058 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16059 /* We already did a pushtag. */;
16060 else if (TREE_CODE (decl) == FUNCTION_DECL
16061 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16062 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16064 DECL_CONTEXT (decl) = NULL_TREE;
16065 pushdecl (decl);
16066 DECL_CONTEXT (decl) = current_function_decl;
16067 cp_check_omp_declare_reduction (decl);
16069 else
16071 int const_init = false;
16072 maybe_push_decl (decl);
16073 if (VAR_P (decl)
16074 && DECL_PRETTY_FUNCTION_P (decl))
16076 /* For __PRETTY_FUNCTION__ we have to adjust the
16077 initializer. */
16078 const char *const name
16079 = cxx_printable_name (current_function_decl, 2);
16080 init = cp_fname_init (name, &TREE_TYPE (decl));
16082 else
16083 init = tsubst_init (init, decl, args, complain, in_decl);
16085 if (VAR_P (decl))
16086 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16087 (pattern_decl));
16088 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16089 if (VAR_P (decl)
16090 && DECL_DECOMPOSITION_P (decl)
16091 && TREE_TYPE (pattern_decl) != error_mark_node)
16093 unsigned int cnt;
16094 tree first;
16095 decl = tsubst_decomp_names (decl, pattern_decl, args,
16096 complain, in_decl, &first,
16097 &cnt);
16098 if (decl != error_mark_node)
16099 cp_finish_decomp (decl, first, cnt);
16105 break;
16108 case FOR_STMT:
16109 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16110 RECUR (FOR_INIT_STMT (t));
16111 finish_init_stmt (stmt);
16112 tmp = RECUR (FOR_COND (t));
16113 finish_for_cond (tmp, stmt, false);
16114 tmp = RECUR (FOR_EXPR (t));
16115 finish_for_expr (tmp, stmt);
16116 RECUR (FOR_BODY (t));
16117 finish_for_stmt (stmt);
16118 break;
16120 case RANGE_FOR_STMT:
16122 tree decl, expr;
16123 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16124 decl = RANGE_FOR_DECL (t);
16125 decl = tsubst (decl, args, complain, in_decl);
16126 maybe_push_decl (decl);
16127 expr = RECUR (RANGE_FOR_EXPR (t));
16128 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16130 unsigned int cnt;
16131 tree first;
16132 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16133 complain, in_decl, &first, &cnt);
16134 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16135 RANGE_FOR_IVDEP (t));
16137 else
16138 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16139 RANGE_FOR_IVDEP (t));
16140 RECUR (RANGE_FOR_BODY (t));
16141 finish_for_stmt (stmt);
16143 break;
16145 case WHILE_STMT:
16146 stmt = begin_while_stmt ();
16147 tmp = RECUR (WHILE_COND (t));
16148 finish_while_stmt_cond (tmp, stmt, false);
16149 RECUR (WHILE_BODY (t));
16150 finish_while_stmt (stmt);
16151 break;
16153 case DO_STMT:
16154 stmt = begin_do_stmt ();
16155 RECUR (DO_BODY (t));
16156 finish_do_body (stmt);
16157 tmp = RECUR (DO_COND (t));
16158 finish_do_stmt (tmp, stmt, false);
16159 break;
16161 case IF_STMT:
16162 stmt = begin_if_stmt ();
16163 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16164 tmp = RECUR (IF_COND (t));
16165 tmp = finish_if_stmt_cond (tmp, stmt);
16166 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16167 /* Don't instantiate the THEN_CLAUSE. */;
16168 else
16170 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16171 if (inhibit)
16172 ++c_inhibit_evaluation_warnings;
16173 RECUR (THEN_CLAUSE (t));
16174 if (inhibit)
16175 --c_inhibit_evaluation_warnings;
16177 finish_then_clause (stmt);
16179 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16180 /* Don't instantiate the ELSE_CLAUSE. */;
16181 else if (ELSE_CLAUSE (t))
16183 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16184 begin_else_clause (stmt);
16185 if (inhibit)
16186 ++c_inhibit_evaluation_warnings;
16187 RECUR (ELSE_CLAUSE (t));
16188 if (inhibit)
16189 --c_inhibit_evaluation_warnings;
16190 finish_else_clause (stmt);
16193 finish_if_stmt (stmt);
16194 break;
16196 case BIND_EXPR:
16197 if (BIND_EXPR_BODY_BLOCK (t))
16198 stmt = begin_function_body ();
16199 else
16200 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16201 ? BCS_TRY_BLOCK : 0);
16203 RECUR (BIND_EXPR_BODY (t));
16205 if (BIND_EXPR_BODY_BLOCK (t))
16206 finish_function_body (stmt);
16207 else
16208 finish_compound_stmt (stmt);
16209 break;
16211 case BREAK_STMT:
16212 finish_break_stmt ();
16213 break;
16215 case CONTINUE_STMT:
16216 finish_continue_stmt ();
16217 break;
16219 case SWITCH_STMT:
16220 stmt = begin_switch_stmt ();
16221 tmp = RECUR (SWITCH_STMT_COND (t));
16222 finish_switch_cond (tmp, stmt);
16223 RECUR (SWITCH_STMT_BODY (t));
16224 finish_switch_stmt (stmt);
16225 break;
16227 case CASE_LABEL_EXPR:
16229 tree low = RECUR (CASE_LOW (t));
16230 tree high = RECUR (CASE_HIGH (t));
16231 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16232 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16233 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16234 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16236 break;
16238 case LABEL_EXPR:
16240 tree decl = LABEL_EXPR_LABEL (t);
16241 tree label;
16243 label = finish_label_stmt (DECL_NAME (decl));
16244 if (TREE_CODE (label) == LABEL_DECL)
16245 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16246 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16247 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16249 break;
16251 case GOTO_EXPR:
16252 tmp = GOTO_DESTINATION (t);
16253 if (TREE_CODE (tmp) != LABEL_DECL)
16254 /* Computed goto's must be tsubst'd into. On the other hand,
16255 non-computed gotos must not be; the identifier in question
16256 will have no binding. */
16257 tmp = RECUR (tmp);
16258 else
16259 tmp = DECL_NAME (tmp);
16260 finish_goto_stmt (tmp);
16261 break;
16263 case ASM_EXPR:
16265 tree string = RECUR (ASM_STRING (t));
16266 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16267 complain, in_decl);
16268 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16269 complain, in_decl);
16270 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16271 complain, in_decl);
16272 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16273 complain, in_decl);
16274 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16275 clobbers, labels);
16276 tree asm_expr = tmp;
16277 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16278 asm_expr = TREE_OPERAND (asm_expr, 0);
16279 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16281 break;
16283 case TRY_BLOCK:
16284 if (CLEANUP_P (t))
16286 stmt = begin_try_block ();
16287 RECUR (TRY_STMTS (t));
16288 finish_cleanup_try_block (stmt);
16289 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16291 else
16293 tree compound_stmt = NULL_TREE;
16295 if (FN_TRY_BLOCK_P (t))
16296 stmt = begin_function_try_block (&compound_stmt);
16297 else
16298 stmt = begin_try_block ();
16300 RECUR (TRY_STMTS (t));
16302 if (FN_TRY_BLOCK_P (t))
16303 finish_function_try_block (stmt);
16304 else
16305 finish_try_block (stmt);
16307 RECUR (TRY_HANDLERS (t));
16308 if (FN_TRY_BLOCK_P (t))
16309 finish_function_handler_sequence (stmt, compound_stmt);
16310 else
16311 finish_handler_sequence (stmt);
16313 break;
16315 case HANDLER:
16317 tree decl = HANDLER_PARMS (t);
16319 if (decl)
16321 decl = tsubst (decl, args, complain, in_decl);
16322 /* Prevent instantiate_decl from trying to instantiate
16323 this variable. We've already done all that needs to be
16324 done. */
16325 if (decl != error_mark_node)
16326 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16328 stmt = begin_handler ();
16329 finish_handler_parms (decl, stmt);
16330 RECUR (HANDLER_BODY (t));
16331 finish_handler (stmt);
16333 break;
16335 case TAG_DEFN:
16336 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16337 if (CLASS_TYPE_P (tmp))
16339 /* Local classes are not independent templates; they are
16340 instantiated along with their containing function. And this
16341 way we don't have to deal with pushing out of one local class
16342 to instantiate a member of another local class. */
16343 /* Closures are handled by the LAMBDA_EXPR. */
16344 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16345 complete_type (tmp);
16346 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16347 if ((VAR_P (fld)
16348 || (TREE_CODE (fld) == FUNCTION_DECL
16349 && !DECL_ARTIFICIAL (fld)))
16350 && DECL_TEMPLATE_INSTANTIATION (fld))
16351 instantiate_decl (fld, /*defer_ok=*/false,
16352 /*expl_inst_class=*/false);
16354 break;
16356 case STATIC_ASSERT:
16358 tree condition;
16360 ++c_inhibit_evaluation_warnings;
16361 condition =
16362 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16363 args,
16364 complain, in_decl,
16365 /*integral_constant_expression_p=*/true);
16366 --c_inhibit_evaluation_warnings;
16368 finish_static_assert (condition,
16369 STATIC_ASSERT_MESSAGE (t),
16370 STATIC_ASSERT_SOURCE_LOCATION (t),
16371 /*member_p=*/false);
16373 break;
16375 case OACC_KERNELS:
16376 case OACC_PARALLEL:
16377 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16378 in_decl);
16379 stmt = begin_omp_parallel ();
16380 RECUR (OMP_BODY (t));
16381 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16382 break;
16384 case OMP_PARALLEL:
16385 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16386 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16387 complain, in_decl);
16388 if (OMP_PARALLEL_COMBINED (t))
16389 omp_parallel_combined_clauses = &tmp;
16390 stmt = begin_omp_parallel ();
16391 RECUR (OMP_PARALLEL_BODY (t));
16392 gcc_assert (omp_parallel_combined_clauses == NULL);
16393 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16394 = OMP_PARALLEL_COMBINED (t);
16395 pop_omp_privatization_clauses (r);
16396 break;
16398 case OMP_TASK:
16399 r = push_omp_privatization_clauses (false);
16400 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16401 complain, in_decl);
16402 stmt = begin_omp_task ();
16403 RECUR (OMP_TASK_BODY (t));
16404 finish_omp_task (tmp, stmt);
16405 pop_omp_privatization_clauses (r);
16406 break;
16408 case OMP_FOR:
16409 case OMP_SIMD:
16410 case CILK_SIMD:
16411 case CILK_FOR:
16412 case OMP_DISTRIBUTE:
16413 case OMP_TASKLOOP:
16414 case OACC_LOOP:
16416 tree clauses, body, pre_body;
16417 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16418 tree orig_declv = NULL_TREE;
16419 tree incrv = NULL_TREE;
16420 enum c_omp_region_type ort = C_ORT_OMP;
16421 int i;
16423 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16424 ort = C_ORT_CILK;
16425 else if (TREE_CODE (t) == OACC_LOOP)
16426 ort = C_ORT_ACC;
16428 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16429 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16430 in_decl);
16431 if (OMP_FOR_INIT (t) != NULL_TREE)
16433 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16434 if (OMP_FOR_ORIG_DECLS (t))
16435 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16436 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16437 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16438 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16441 stmt = begin_omp_structured_block ();
16443 pre_body = push_stmt_list ();
16444 RECUR (OMP_FOR_PRE_BODY (t));
16445 pre_body = pop_stmt_list (pre_body);
16447 if (OMP_FOR_INIT (t) != NULL_TREE)
16448 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16449 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16450 incrv, &clauses, args, complain, in_decl,
16451 integral_constant_expression_p);
16452 omp_parallel_combined_clauses = NULL;
16454 body = push_stmt_list ();
16455 RECUR (OMP_FOR_BODY (t));
16456 body = pop_stmt_list (body);
16458 if (OMP_FOR_INIT (t) != NULL_TREE)
16459 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16460 orig_declv, initv, condv, incrv, body, pre_body,
16461 NULL, clauses);
16462 else
16464 t = make_node (TREE_CODE (t));
16465 TREE_TYPE (t) = void_type_node;
16466 OMP_FOR_BODY (t) = body;
16467 OMP_FOR_PRE_BODY (t) = pre_body;
16468 OMP_FOR_CLAUSES (t) = clauses;
16469 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16470 add_stmt (t);
16473 add_stmt (finish_omp_structured_block (stmt));
16474 pop_omp_privatization_clauses (r);
16476 break;
16478 case OMP_SECTIONS:
16479 omp_parallel_combined_clauses = NULL;
16480 /* FALLTHRU */
16481 case OMP_SINGLE:
16482 case OMP_TEAMS:
16483 case OMP_CRITICAL:
16484 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16485 && OMP_TEAMS_COMBINED (t));
16486 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16487 in_decl);
16488 stmt = push_stmt_list ();
16489 RECUR (OMP_BODY (t));
16490 stmt = pop_stmt_list (stmt);
16492 t = copy_node (t);
16493 OMP_BODY (t) = stmt;
16494 OMP_CLAUSES (t) = tmp;
16495 add_stmt (t);
16496 pop_omp_privatization_clauses (r);
16497 break;
16499 case OACC_DATA:
16500 case OMP_TARGET_DATA:
16501 case OMP_TARGET:
16502 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16503 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16504 in_decl);
16505 keep_next_level (true);
16506 stmt = begin_omp_structured_block ();
16508 RECUR (OMP_BODY (t));
16509 stmt = finish_omp_structured_block (stmt);
16511 t = copy_node (t);
16512 OMP_BODY (t) = stmt;
16513 OMP_CLAUSES (t) = tmp;
16514 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16516 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16517 if (teams)
16519 /* For combined target teams, ensure the num_teams and
16520 thread_limit clause expressions are evaluated on the host,
16521 before entering the target construct. */
16522 tree c;
16523 for (c = OMP_TEAMS_CLAUSES (teams);
16524 c; c = OMP_CLAUSE_CHAIN (c))
16525 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16526 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16527 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16529 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16530 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16531 if (expr == error_mark_node)
16532 continue;
16533 tmp = TARGET_EXPR_SLOT (expr);
16534 add_stmt (expr);
16535 OMP_CLAUSE_OPERAND (c, 0) = expr;
16536 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16537 OMP_CLAUSE_FIRSTPRIVATE);
16538 OMP_CLAUSE_DECL (tc) = tmp;
16539 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16540 OMP_TARGET_CLAUSES (t) = tc;
16544 add_stmt (t);
16545 break;
16547 case OACC_DECLARE:
16548 t = copy_node (t);
16549 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16550 complain, in_decl);
16551 OACC_DECLARE_CLAUSES (t) = tmp;
16552 add_stmt (t);
16553 break;
16555 case OMP_TARGET_UPDATE:
16556 case OMP_TARGET_ENTER_DATA:
16557 case OMP_TARGET_EXIT_DATA:
16558 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16559 complain, in_decl);
16560 t = copy_node (t);
16561 OMP_STANDALONE_CLAUSES (t) = tmp;
16562 add_stmt (t);
16563 break;
16565 case OACC_ENTER_DATA:
16566 case OACC_EXIT_DATA:
16567 case OACC_UPDATE:
16568 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16569 complain, in_decl);
16570 t = copy_node (t);
16571 OMP_STANDALONE_CLAUSES (t) = tmp;
16572 add_stmt (t);
16573 break;
16575 case OMP_ORDERED:
16576 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16577 complain, in_decl);
16578 stmt = push_stmt_list ();
16579 RECUR (OMP_BODY (t));
16580 stmt = pop_stmt_list (stmt);
16582 t = copy_node (t);
16583 OMP_BODY (t) = stmt;
16584 OMP_ORDERED_CLAUSES (t) = tmp;
16585 add_stmt (t);
16586 break;
16588 case OMP_SECTION:
16589 case OMP_MASTER:
16590 case OMP_TASKGROUP:
16591 stmt = push_stmt_list ();
16592 RECUR (OMP_BODY (t));
16593 stmt = pop_stmt_list (stmt);
16595 t = copy_node (t);
16596 OMP_BODY (t) = stmt;
16597 add_stmt (t);
16598 break;
16600 case OMP_ATOMIC:
16601 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16602 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16604 tree op1 = TREE_OPERAND (t, 1);
16605 tree rhs1 = NULL_TREE;
16606 tree lhs, rhs;
16607 if (TREE_CODE (op1) == COMPOUND_EXPR)
16609 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16610 op1 = TREE_OPERAND (op1, 1);
16612 lhs = RECUR (TREE_OPERAND (op1, 0));
16613 rhs = RECUR (TREE_OPERAND (op1, 1));
16614 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16615 NULL_TREE, NULL_TREE, rhs1,
16616 OMP_ATOMIC_SEQ_CST (t));
16618 else
16620 tree op1 = TREE_OPERAND (t, 1);
16621 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16622 tree rhs1 = NULL_TREE;
16623 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16624 enum tree_code opcode = NOP_EXPR;
16625 if (code == OMP_ATOMIC_READ)
16627 v = RECUR (TREE_OPERAND (op1, 0));
16628 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16630 else if (code == OMP_ATOMIC_CAPTURE_OLD
16631 || code == OMP_ATOMIC_CAPTURE_NEW)
16633 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16634 v = RECUR (TREE_OPERAND (op1, 0));
16635 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16636 if (TREE_CODE (op11) == COMPOUND_EXPR)
16638 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16639 op11 = TREE_OPERAND (op11, 1);
16641 lhs = RECUR (TREE_OPERAND (op11, 0));
16642 rhs = RECUR (TREE_OPERAND (op11, 1));
16643 opcode = TREE_CODE (op11);
16644 if (opcode == MODIFY_EXPR)
16645 opcode = NOP_EXPR;
16647 else
16649 code = OMP_ATOMIC;
16650 lhs = RECUR (TREE_OPERAND (op1, 0));
16651 rhs = RECUR (TREE_OPERAND (op1, 1));
16653 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16654 OMP_ATOMIC_SEQ_CST (t));
16656 break;
16658 case TRANSACTION_EXPR:
16660 int flags = 0;
16661 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16662 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16664 if (TRANSACTION_EXPR_IS_STMT (t))
16666 tree body = TRANSACTION_EXPR_BODY (t);
16667 tree noex = NULL_TREE;
16668 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16670 noex = MUST_NOT_THROW_COND (body);
16671 if (noex == NULL_TREE)
16672 noex = boolean_true_node;
16673 body = TREE_OPERAND (body, 0);
16675 stmt = begin_transaction_stmt (input_location, NULL, flags);
16676 RECUR (body);
16677 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16679 else
16681 stmt = build_transaction_expr (EXPR_LOCATION (t),
16682 RECUR (TRANSACTION_EXPR_BODY (t)),
16683 flags, NULL_TREE);
16684 RETURN (stmt);
16687 break;
16689 case MUST_NOT_THROW_EXPR:
16691 tree op0 = RECUR (TREE_OPERAND (t, 0));
16692 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16693 RETURN (build_must_not_throw_expr (op0, cond));
16696 case EXPR_PACK_EXPANSION:
16697 error ("invalid use of pack expansion expression");
16698 RETURN (error_mark_node);
16700 case NONTYPE_ARGUMENT_PACK:
16701 error ("use %<...%> to expand argument pack");
16702 RETURN (error_mark_node);
16704 case CILK_SPAWN_STMT:
16705 cfun->calls_cilk_spawn = 1;
16706 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16708 case CILK_SYNC_STMT:
16709 RETURN (build_cilk_sync ());
16711 case COMPOUND_EXPR:
16712 tmp = RECUR (TREE_OPERAND (t, 0));
16713 if (tmp == NULL_TREE)
16714 /* If the first operand was a statement, we're done with it. */
16715 RETURN (RECUR (TREE_OPERAND (t, 1)));
16716 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16717 RECUR (TREE_OPERAND (t, 1)),
16718 complain));
16720 case ANNOTATE_EXPR:
16721 tmp = RECUR (TREE_OPERAND (t, 0));
16722 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16723 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16725 default:
16726 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16728 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16729 /*function_p=*/false,
16730 integral_constant_expression_p));
16733 RETURN (NULL_TREE);
16734 out:
16735 input_location = loc;
16736 return r;
16737 #undef RECUR
16738 #undef RETURN
16741 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16742 function. For description of the body see comment above
16743 cp_parser_omp_declare_reduction_exprs. */
16745 static void
16746 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16748 if (t == NULL_TREE || t == error_mark_node)
16749 return;
16751 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16753 tree_stmt_iterator tsi;
16754 int i;
16755 tree stmts[7];
16756 memset (stmts, 0, sizeof stmts);
16757 for (i = 0, tsi = tsi_start (t);
16758 i < 7 && !tsi_end_p (tsi);
16759 i++, tsi_next (&tsi))
16760 stmts[i] = tsi_stmt (tsi);
16761 gcc_assert (tsi_end_p (tsi));
16763 if (i >= 3)
16765 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16766 && TREE_CODE (stmts[1]) == DECL_EXPR);
16767 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16768 args, complain, in_decl);
16769 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16770 args, complain, in_decl);
16771 DECL_CONTEXT (omp_out) = current_function_decl;
16772 DECL_CONTEXT (omp_in) = current_function_decl;
16773 keep_next_level (true);
16774 tree block = begin_omp_structured_block ();
16775 tsubst_expr (stmts[2], args, complain, in_decl, false);
16776 block = finish_omp_structured_block (block);
16777 block = maybe_cleanup_point_expr_void (block);
16778 add_decl_expr (omp_out);
16779 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16780 TREE_NO_WARNING (omp_out) = 1;
16781 add_decl_expr (omp_in);
16782 finish_expr_stmt (block);
16784 if (i >= 6)
16786 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16787 && TREE_CODE (stmts[4]) == DECL_EXPR);
16788 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16789 args, complain, in_decl);
16790 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16791 args, complain, in_decl);
16792 DECL_CONTEXT (omp_priv) = current_function_decl;
16793 DECL_CONTEXT (omp_orig) = current_function_decl;
16794 keep_next_level (true);
16795 tree block = begin_omp_structured_block ();
16796 tsubst_expr (stmts[5], args, complain, in_decl, false);
16797 block = finish_omp_structured_block (block);
16798 block = maybe_cleanup_point_expr_void (block);
16799 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16800 add_decl_expr (omp_priv);
16801 add_decl_expr (omp_orig);
16802 finish_expr_stmt (block);
16803 if (i == 7)
16804 add_decl_expr (omp_orig);
16808 /* T is a postfix-expression that is not being used in a function
16809 call. Return the substituted version of T. */
16811 static tree
16812 tsubst_non_call_postfix_expression (tree t, tree args,
16813 tsubst_flags_t complain,
16814 tree in_decl)
16816 if (TREE_CODE (t) == SCOPE_REF)
16817 t = tsubst_qualified_id (t, args, complain, in_decl,
16818 /*done=*/false, /*address_p=*/false);
16819 else
16820 t = tsubst_copy_and_build (t, args, complain, in_decl,
16821 /*function_p=*/false,
16822 /*integral_constant_expression_p=*/false);
16824 return t;
16827 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16828 instantiation context. Instantiating a pack expansion containing a lambda
16829 might result in multiple lambdas all based on the same lambda in the
16830 template. */
16832 tree
16833 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16835 tree oldfn = lambda_function (t);
16836 in_decl = oldfn;
16838 tree r = build_lambda_expr ();
16840 LAMBDA_EXPR_LOCATION (r)
16841 = LAMBDA_EXPR_LOCATION (t);
16842 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16843 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16844 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16846 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16847 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16848 else
16849 record_lambda_scope (r);
16851 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16852 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16854 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16855 cap = TREE_CHAIN (cap))
16857 tree field = TREE_PURPOSE (cap);
16858 if (PACK_EXPANSION_P (field))
16859 field = PACK_EXPANSION_PATTERN (field);
16860 field = tsubst_decl (field, args, complain);
16862 if (field == error_mark_node)
16863 return error_mark_node;
16865 tree init = TREE_VALUE (cap);
16866 if (PACK_EXPANSION_P (init))
16867 init = tsubst_pack_expansion (init, args, complain, in_decl);
16868 else
16869 init = tsubst_copy_and_build (init, args, complain, in_decl,
16870 /*fn*/false, /*constexpr*/false);
16872 if (TREE_CODE (field) == TREE_VEC)
16874 int len = TREE_VEC_LENGTH (field);
16875 gcc_assert (TREE_CODE (init) == TREE_VEC
16876 && TREE_VEC_LENGTH (init) == len);
16877 for (int i = 0; i < len; ++i)
16878 LAMBDA_EXPR_CAPTURE_LIST (r)
16879 = tree_cons (TREE_VEC_ELT (field, i),
16880 TREE_VEC_ELT (init, i),
16881 LAMBDA_EXPR_CAPTURE_LIST (r));
16883 else
16885 LAMBDA_EXPR_CAPTURE_LIST (r)
16886 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16888 if (id_equal (DECL_NAME (field), "__this"))
16889 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16893 tree type = begin_lambda_type (r);
16895 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16896 determine_visibility (TYPE_NAME (type));
16898 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16900 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16901 ? DECL_TI_TEMPLATE (oldfn)
16902 : NULL_TREE);
16904 tree fntype = static_fn_type (oldfn);
16905 if (oldtmpl)
16906 ++processing_template_decl;
16907 fntype = tsubst (fntype, args, complain, in_decl);
16908 if (oldtmpl)
16909 --processing_template_decl;
16911 if (fntype == error_mark_node)
16912 r = error_mark_node;
16913 else
16915 /* Fix the type of 'this'. */
16916 fntype = build_memfn_type (fntype, type,
16917 type_memfn_quals (fntype),
16918 type_memfn_rqual (fntype));
16919 tree fn, tmpl;
16920 if (oldtmpl)
16922 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16923 fn = DECL_TEMPLATE_RESULT (tmpl);
16924 finish_member_declaration (tmpl);
16926 else
16928 tmpl = NULL_TREE;
16929 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16930 finish_member_declaration (fn);
16933 /* Let finish_function set this. */
16934 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16936 bool nested = cfun;
16937 if (nested)
16938 push_function_context ();
16940 local_specialization_stack s (lss_copy);
16942 tree body = start_lambda_function (fn, r);
16944 register_parameter_specializations (oldfn, fn);
16946 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16947 /*constexpr*/false);
16949 finish_lambda_function (body);
16951 if (nested)
16952 pop_function_context ();
16954 /* The capture list was built up in reverse order; fix that now. */
16955 LAMBDA_EXPR_CAPTURE_LIST (r)
16956 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16958 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16960 maybe_add_lambda_conv_op (type);
16963 finish_struct (type, /*attr*/NULL_TREE);
16965 insert_pending_capture_proxies ();
16967 return r;
16970 /* Like tsubst but deals with expressions and performs semantic
16971 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16973 tree
16974 tsubst_copy_and_build (tree t,
16975 tree args,
16976 tsubst_flags_t complain,
16977 tree in_decl,
16978 bool function_p,
16979 bool integral_constant_expression_p)
16981 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16982 #define RECUR(NODE) \
16983 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16984 /*function_p=*/false, \
16985 integral_constant_expression_p)
16987 tree retval, op1;
16988 location_t loc;
16990 if (t == NULL_TREE || t == error_mark_node)
16991 return t;
16993 loc = input_location;
16994 if (EXPR_HAS_LOCATION (t))
16995 input_location = EXPR_LOCATION (t);
16997 /* N3276 decltype magic only applies to calls at the top level or on the
16998 right side of a comma. */
16999 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17000 complain &= ~tf_decltype;
17002 switch (TREE_CODE (t))
17004 case USING_DECL:
17005 t = DECL_NAME (t);
17006 /* Fall through. */
17007 case IDENTIFIER_NODE:
17009 tree decl;
17010 cp_id_kind idk;
17011 bool non_integral_constant_expression_p;
17012 const char *error_msg;
17014 if (IDENTIFIER_CONV_OP_P (t))
17016 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17017 t = make_conv_op_name (new_type);
17020 /* Look up the name. */
17021 decl = lookup_name (t);
17023 /* By convention, expressions use ERROR_MARK_NODE to indicate
17024 failure, not NULL_TREE. */
17025 if (decl == NULL_TREE)
17026 decl = error_mark_node;
17028 decl = finish_id_expression (t, decl, NULL_TREE,
17029 &idk,
17030 integral_constant_expression_p,
17031 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17032 &non_integral_constant_expression_p,
17033 /*template_p=*/false,
17034 /*done=*/true,
17035 /*address_p=*/false,
17036 /*template_arg_p=*/false,
17037 &error_msg,
17038 input_location);
17039 if (error_msg)
17040 error (error_msg);
17041 if (!function_p && identifier_p (decl))
17043 if (complain & tf_error)
17044 unqualified_name_lookup_error (decl);
17045 decl = error_mark_node;
17047 RETURN (decl);
17050 case TEMPLATE_ID_EXPR:
17052 tree object;
17053 tree templ = RECUR (TREE_OPERAND (t, 0));
17054 tree targs = TREE_OPERAND (t, 1);
17056 if (targs)
17057 targs = tsubst_template_args (targs, args, complain, in_decl);
17058 if (targs == error_mark_node)
17059 return error_mark_node;
17061 if (TREE_CODE (templ) == SCOPE_REF)
17063 tree name = TREE_OPERAND (templ, 1);
17064 tree tid = lookup_template_function (name, targs);
17065 TREE_OPERAND (templ, 1) = tid;
17066 return templ;
17069 if (variable_template_p (templ))
17070 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17072 if (TREE_CODE (templ) == COMPONENT_REF)
17074 object = TREE_OPERAND (templ, 0);
17075 templ = TREE_OPERAND (templ, 1);
17077 else
17078 object = NULL_TREE;
17079 templ = lookup_template_function (templ, targs);
17081 if (object)
17082 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17083 object, templ, NULL_TREE));
17084 else
17085 RETURN (baselink_for_fns (templ));
17088 case INDIRECT_REF:
17090 tree r = RECUR (TREE_OPERAND (t, 0));
17092 if (REFERENCE_REF_P (t))
17094 /* A type conversion to reference type will be enclosed in
17095 such an indirect ref, but the substitution of the cast
17096 will have also added such an indirect ref. */
17097 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
17098 r = convert_from_reference (r);
17100 else
17101 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17102 complain|decltype_flag);
17104 if (TREE_CODE (r) == INDIRECT_REF)
17105 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17107 RETURN (r);
17110 case NOP_EXPR:
17112 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17113 tree op0 = RECUR (TREE_OPERAND (t, 0));
17114 RETURN (build_nop (type, op0));
17117 case IMPLICIT_CONV_EXPR:
17119 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17120 tree expr = RECUR (TREE_OPERAND (t, 0));
17121 int flags = LOOKUP_IMPLICIT;
17122 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17123 flags = LOOKUP_NORMAL;
17124 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17125 flags));
17128 case CONVERT_EXPR:
17130 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17131 tree op0 = RECUR (TREE_OPERAND (t, 0));
17132 RETURN (build1 (CONVERT_EXPR, type, op0));
17135 case CAST_EXPR:
17136 case REINTERPRET_CAST_EXPR:
17137 case CONST_CAST_EXPR:
17138 case DYNAMIC_CAST_EXPR:
17139 case STATIC_CAST_EXPR:
17141 tree type;
17142 tree op, r = NULL_TREE;
17144 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17145 if (integral_constant_expression_p
17146 && !cast_valid_in_integral_constant_expression_p (type))
17148 if (complain & tf_error)
17149 error ("a cast to a type other than an integral or "
17150 "enumeration type cannot appear in a constant-expression");
17151 RETURN (error_mark_node);
17154 op = RECUR (TREE_OPERAND (t, 0));
17156 warning_sentinel s(warn_useless_cast);
17157 switch (TREE_CODE (t))
17159 case CAST_EXPR:
17160 r = build_functional_cast (type, op, complain);
17161 break;
17162 case REINTERPRET_CAST_EXPR:
17163 r = build_reinterpret_cast (type, op, complain);
17164 break;
17165 case CONST_CAST_EXPR:
17166 r = build_const_cast (type, op, complain);
17167 break;
17168 case DYNAMIC_CAST_EXPR:
17169 r = build_dynamic_cast (type, op, complain);
17170 break;
17171 case STATIC_CAST_EXPR:
17172 r = build_static_cast (type, op, complain);
17173 break;
17174 default:
17175 gcc_unreachable ();
17178 RETURN (r);
17181 case POSTDECREMENT_EXPR:
17182 case POSTINCREMENT_EXPR:
17183 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17184 args, complain, in_decl);
17185 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17186 complain|decltype_flag));
17188 case PREDECREMENT_EXPR:
17189 case PREINCREMENT_EXPR:
17190 case NEGATE_EXPR:
17191 case BIT_NOT_EXPR:
17192 case ABS_EXPR:
17193 case TRUTH_NOT_EXPR:
17194 case UNARY_PLUS_EXPR: /* Unary + */
17195 case REALPART_EXPR:
17196 case IMAGPART_EXPR:
17197 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17198 RECUR (TREE_OPERAND (t, 0)),
17199 complain|decltype_flag));
17201 case FIX_TRUNC_EXPR:
17202 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17203 false, complain));
17205 case ADDR_EXPR:
17206 op1 = TREE_OPERAND (t, 0);
17207 if (TREE_CODE (op1) == LABEL_DECL)
17208 RETURN (finish_label_address_expr (DECL_NAME (op1),
17209 EXPR_LOCATION (op1)));
17210 if (TREE_CODE (op1) == SCOPE_REF)
17211 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17212 /*done=*/true, /*address_p=*/true);
17213 else
17214 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17215 in_decl);
17216 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17217 complain|decltype_flag));
17219 case PLUS_EXPR:
17220 case MINUS_EXPR:
17221 case MULT_EXPR:
17222 case TRUNC_DIV_EXPR:
17223 case CEIL_DIV_EXPR:
17224 case FLOOR_DIV_EXPR:
17225 case ROUND_DIV_EXPR:
17226 case EXACT_DIV_EXPR:
17227 case BIT_AND_EXPR:
17228 case BIT_IOR_EXPR:
17229 case BIT_XOR_EXPR:
17230 case TRUNC_MOD_EXPR:
17231 case FLOOR_MOD_EXPR:
17232 case TRUTH_ANDIF_EXPR:
17233 case TRUTH_ORIF_EXPR:
17234 case TRUTH_AND_EXPR:
17235 case TRUTH_OR_EXPR:
17236 case RSHIFT_EXPR:
17237 case LSHIFT_EXPR:
17238 case RROTATE_EXPR:
17239 case LROTATE_EXPR:
17240 case EQ_EXPR:
17241 case NE_EXPR:
17242 case MAX_EXPR:
17243 case MIN_EXPR:
17244 case LE_EXPR:
17245 case GE_EXPR:
17246 case LT_EXPR:
17247 case GT_EXPR:
17248 case MEMBER_REF:
17249 case DOTSTAR_EXPR:
17251 warning_sentinel s1(warn_type_limits);
17252 warning_sentinel s2(warn_div_by_zero);
17253 warning_sentinel s3(warn_logical_op);
17254 warning_sentinel s4(warn_tautological_compare);
17255 tree op0 = RECUR (TREE_OPERAND (t, 0));
17256 tree op1 = RECUR (TREE_OPERAND (t, 1));
17257 tree r = build_x_binary_op
17258 (input_location, TREE_CODE (t),
17259 op0,
17260 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17261 ? ERROR_MARK
17262 : TREE_CODE (TREE_OPERAND (t, 0))),
17263 op1,
17264 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17265 ? ERROR_MARK
17266 : TREE_CODE (TREE_OPERAND (t, 1))),
17267 /*overload=*/NULL,
17268 complain|decltype_flag);
17269 if (EXPR_P (r) && TREE_NO_WARNING (t))
17270 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17272 RETURN (r);
17275 case POINTER_PLUS_EXPR:
17277 tree op0 = RECUR (TREE_OPERAND (t, 0));
17278 tree op1 = RECUR (TREE_OPERAND (t, 1));
17279 return fold_build_pointer_plus (op0, op1);
17282 case SCOPE_REF:
17283 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17284 /*address_p=*/false));
17285 case ARRAY_REF:
17286 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17287 args, complain, in_decl);
17288 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17289 RECUR (TREE_OPERAND (t, 1)),
17290 complain|decltype_flag));
17292 case ARRAY_NOTATION_REF:
17294 tree start_index, length, stride;
17295 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
17296 args, complain, in_decl);
17297 start_index = RECUR (ARRAY_NOTATION_START (t));
17298 length = RECUR (ARRAY_NOTATION_LENGTH (t));
17299 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
17300 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17301 length, stride, TREE_TYPE (op1)));
17303 case SIZEOF_EXPR:
17304 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17305 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17306 RETURN (tsubst_copy (t, args, complain, in_decl));
17307 /* Fall through */
17309 case ALIGNOF_EXPR:
17311 tree r;
17313 op1 = TREE_OPERAND (t, 0);
17314 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17315 op1 = TREE_TYPE (op1);
17316 if (!args)
17318 /* When there are no ARGS, we are trying to evaluate a
17319 non-dependent expression from the parser. Trying to do
17320 the substitutions may not work. */
17321 if (!TYPE_P (op1))
17322 op1 = TREE_TYPE (op1);
17324 else
17326 ++cp_unevaluated_operand;
17327 ++c_inhibit_evaluation_warnings;
17328 if (TYPE_P (op1))
17329 op1 = tsubst (op1, args, complain, in_decl);
17330 else
17331 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17332 /*function_p=*/false,
17333 /*integral_constant_expression_p=*/
17334 false);
17335 --cp_unevaluated_operand;
17336 --c_inhibit_evaluation_warnings;
17338 if (TYPE_P (op1))
17339 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17340 complain & tf_error);
17341 else
17342 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17343 complain & tf_error);
17344 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17346 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17348 if (!processing_template_decl && TYPE_P (op1))
17350 r = build_min (SIZEOF_EXPR, size_type_node,
17351 build1 (NOP_EXPR, op1, error_mark_node));
17352 SIZEOF_EXPR_TYPE_P (r) = 1;
17354 else
17355 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17356 TREE_SIDE_EFFECTS (r) = 0;
17357 TREE_READONLY (r) = 1;
17359 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17361 RETURN (r);
17364 case AT_ENCODE_EXPR:
17366 op1 = TREE_OPERAND (t, 0);
17367 ++cp_unevaluated_operand;
17368 ++c_inhibit_evaluation_warnings;
17369 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17370 /*function_p=*/false,
17371 /*integral_constant_expression_p=*/false);
17372 --cp_unevaluated_operand;
17373 --c_inhibit_evaluation_warnings;
17374 RETURN (objc_build_encode_expr (op1));
17377 case NOEXCEPT_EXPR:
17378 op1 = TREE_OPERAND (t, 0);
17379 ++cp_unevaluated_operand;
17380 ++c_inhibit_evaluation_warnings;
17381 ++cp_noexcept_operand;
17382 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17383 /*function_p=*/false,
17384 /*integral_constant_expression_p=*/false);
17385 --cp_unevaluated_operand;
17386 --c_inhibit_evaluation_warnings;
17387 --cp_noexcept_operand;
17388 RETURN (finish_noexcept_expr (op1, complain));
17390 case MODOP_EXPR:
17392 warning_sentinel s(warn_div_by_zero);
17393 tree lhs = RECUR (TREE_OPERAND (t, 0));
17394 tree rhs = RECUR (TREE_OPERAND (t, 2));
17395 tree r = build_x_modify_expr
17396 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17397 complain|decltype_flag);
17398 /* TREE_NO_WARNING must be set if either the expression was
17399 parenthesized or it uses an operator such as >>= rather
17400 than plain assignment. In the former case, it was already
17401 set and must be copied. In the latter case,
17402 build_x_modify_expr sets it and it must not be reset
17403 here. */
17404 if (TREE_NO_WARNING (t))
17405 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17407 RETURN (r);
17410 case ARROW_EXPR:
17411 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17412 args, complain, in_decl);
17413 /* Remember that there was a reference to this entity. */
17414 if (DECL_P (op1)
17415 && !mark_used (op1, complain) && !(complain & tf_error))
17416 RETURN (error_mark_node);
17417 RETURN (build_x_arrow (input_location, op1, complain));
17419 case NEW_EXPR:
17421 tree placement = RECUR (TREE_OPERAND (t, 0));
17422 tree init = RECUR (TREE_OPERAND (t, 3));
17423 vec<tree, va_gc> *placement_vec;
17424 vec<tree, va_gc> *init_vec;
17425 tree ret;
17427 if (placement == NULL_TREE)
17428 placement_vec = NULL;
17429 else
17431 placement_vec = make_tree_vector ();
17432 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17433 vec_safe_push (placement_vec, TREE_VALUE (placement));
17436 /* If there was an initializer in the original tree, but it
17437 instantiated to an empty list, then we should pass a
17438 non-NULL empty vector to tell build_new that it was an
17439 empty initializer() rather than no initializer. This can
17440 only happen when the initializer is a pack expansion whose
17441 parameter packs are of length zero. */
17442 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17443 init_vec = NULL;
17444 else
17446 init_vec = make_tree_vector ();
17447 if (init == void_node)
17448 gcc_assert (init_vec != NULL);
17449 else
17451 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17452 vec_safe_push (init_vec, TREE_VALUE (init));
17456 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17457 tree op2 = RECUR (TREE_OPERAND (t, 2));
17458 ret = build_new (&placement_vec, op1, op2, &init_vec,
17459 NEW_EXPR_USE_GLOBAL (t),
17460 complain);
17462 if (placement_vec != NULL)
17463 release_tree_vector (placement_vec);
17464 if (init_vec != NULL)
17465 release_tree_vector (init_vec);
17467 RETURN (ret);
17470 case DELETE_EXPR:
17472 tree op0 = RECUR (TREE_OPERAND (t, 0));
17473 tree op1 = RECUR (TREE_OPERAND (t, 1));
17474 RETURN (delete_sanity (op0, op1,
17475 DELETE_EXPR_USE_VEC (t),
17476 DELETE_EXPR_USE_GLOBAL (t),
17477 complain));
17480 case COMPOUND_EXPR:
17482 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17483 complain & ~tf_decltype, in_decl,
17484 /*function_p=*/false,
17485 integral_constant_expression_p);
17486 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17487 op0,
17488 RECUR (TREE_OPERAND (t, 1)),
17489 complain|decltype_flag));
17492 case CALL_EXPR:
17494 tree function;
17495 vec<tree, va_gc> *call_args;
17496 unsigned int nargs, i;
17497 bool qualified_p;
17498 bool koenig_p;
17499 tree ret;
17501 function = CALL_EXPR_FN (t);
17502 /* Internal function with no arguments. */
17503 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17504 RETURN (t);
17506 /* When we parsed the expression, we determined whether or
17507 not Koenig lookup should be performed. */
17508 koenig_p = KOENIG_LOOKUP_P (t);
17509 if (function == NULL_TREE)
17511 koenig_p = false;
17512 qualified_p = false;
17514 else if (TREE_CODE (function) == SCOPE_REF)
17516 qualified_p = true;
17517 function = tsubst_qualified_id (function, args, complain, in_decl,
17518 /*done=*/false,
17519 /*address_p=*/false);
17521 else if (koenig_p && identifier_p (function))
17523 /* Do nothing; calling tsubst_copy_and_build on an identifier
17524 would incorrectly perform unqualified lookup again.
17526 Note that we can also have an IDENTIFIER_NODE if the earlier
17527 unqualified lookup found a member function; in that case
17528 koenig_p will be false and we do want to do the lookup
17529 again to find the instantiated member function.
17531 FIXME but doing that causes c++/15272, so we need to stop
17532 using IDENTIFIER_NODE in that situation. */
17533 qualified_p = false;
17535 else
17537 if (TREE_CODE (function) == COMPONENT_REF)
17539 tree op = TREE_OPERAND (function, 1);
17541 qualified_p = (TREE_CODE (op) == SCOPE_REF
17542 || (BASELINK_P (op)
17543 && BASELINK_QUALIFIED_P (op)));
17545 else
17546 qualified_p = false;
17548 if (TREE_CODE (function) == ADDR_EXPR
17549 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17550 /* Avoid error about taking the address of a constructor. */
17551 function = TREE_OPERAND (function, 0);
17553 function = tsubst_copy_and_build (function, args, complain,
17554 in_decl,
17555 !qualified_p,
17556 integral_constant_expression_p);
17558 if (BASELINK_P (function))
17559 qualified_p = true;
17562 nargs = call_expr_nargs (t);
17563 call_args = make_tree_vector ();
17564 for (i = 0; i < nargs; ++i)
17566 tree arg = CALL_EXPR_ARG (t, i);
17568 if (!PACK_EXPANSION_P (arg))
17569 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17570 else
17572 /* Expand the pack expansion and push each entry onto
17573 CALL_ARGS. */
17574 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17575 if (TREE_CODE (arg) == TREE_VEC)
17577 unsigned int len, j;
17579 len = TREE_VEC_LENGTH (arg);
17580 for (j = 0; j < len; ++j)
17582 tree value = TREE_VEC_ELT (arg, j);
17583 if (value != NULL_TREE)
17584 value = convert_from_reference (value);
17585 vec_safe_push (call_args, value);
17588 else
17590 /* A partial substitution. Add one entry. */
17591 vec_safe_push (call_args, arg);
17596 /* We do not perform argument-dependent lookup if normal
17597 lookup finds a non-function, in accordance with the
17598 expected resolution of DR 218. */
17599 if (koenig_p
17600 && ((is_overloaded_fn (function)
17601 /* If lookup found a member function, the Koenig lookup is
17602 not appropriate, even if an unqualified-name was used
17603 to denote the function. */
17604 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17605 || identifier_p (function))
17606 /* Only do this when substitution turns a dependent call
17607 into a non-dependent call. */
17608 && type_dependent_expression_p_push (t)
17609 && !any_type_dependent_arguments_p (call_args))
17610 function = perform_koenig_lookup (function, call_args, tf_none);
17612 if (function != NULL_TREE
17613 && identifier_p (function)
17614 && !any_type_dependent_arguments_p (call_args))
17616 if (koenig_p && (complain & tf_warning_or_error))
17618 /* For backwards compatibility and good diagnostics, try
17619 the unqualified lookup again if we aren't in SFINAE
17620 context. */
17621 tree unq = (tsubst_copy_and_build
17622 (function, args, complain, in_decl, true,
17623 integral_constant_expression_p));
17624 if (unq == error_mark_node)
17626 release_tree_vector (call_args);
17627 RETURN (error_mark_node);
17630 if (unq != function)
17632 /* In a lambda fn, we have to be careful to not
17633 introduce new this captures. Legacy code can't
17634 be using lambdas anyway, so it's ok to be
17635 stricter. */
17636 bool in_lambda = (current_class_type
17637 && LAMBDA_TYPE_P (current_class_type));
17638 char const *const msg
17639 = G_("%qD was not declared in this scope, "
17640 "and no declarations were found by "
17641 "argument-dependent lookup at the point "
17642 "of instantiation");
17644 bool diag = true;
17645 if (in_lambda)
17646 error_at (EXPR_LOC_OR_LOC (t, input_location),
17647 msg, function);
17648 else
17649 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17650 msg, function);
17651 if (diag)
17653 tree fn = unq;
17655 if (INDIRECT_REF_P (fn))
17656 fn = TREE_OPERAND (fn, 0);
17657 if (is_overloaded_fn (fn))
17658 fn = get_first_fn (fn);
17660 if (!DECL_P (fn))
17661 /* Can't say anything more. */;
17662 else if (DECL_CLASS_SCOPE_P (fn))
17664 location_t loc = EXPR_LOC_OR_LOC (t,
17665 input_location);
17666 inform (loc,
17667 "declarations in dependent base %qT are "
17668 "not found by unqualified lookup",
17669 DECL_CLASS_CONTEXT (fn));
17670 if (current_class_ptr)
17671 inform (loc,
17672 "use %<this->%D%> instead", function);
17673 else
17674 inform (loc,
17675 "use %<%T::%D%> instead",
17676 current_class_name, function);
17678 else
17679 inform (DECL_SOURCE_LOCATION (fn),
17680 "%qD declared here, later in the "
17681 "translation unit", fn);
17682 if (in_lambda)
17684 release_tree_vector (call_args);
17685 RETURN (error_mark_node);
17689 function = unq;
17692 if (identifier_p (function))
17694 if (complain & tf_error)
17695 unqualified_name_lookup_error (function);
17696 release_tree_vector (call_args);
17697 RETURN (error_mark_node);
17701 /* Remember that there was a reference to this entity. */
17702 if (function != NULL_TREE
17703 && DECL_P (function)
17704 && !mark_used (function, complain) && !(complain & tf_error))
17706 release_tree_vector (call_args);
17707 RETURN (error_mark_node);
17710 /* Put back tf_decltype for the actual call. */
17711 complain |= decltype_flag;
17713 if (function == NULL_TREE)
17714 switch (CALL_EXPR_IFN (t))
17716 case IFN_LAUNDER:
17717 gcc_assert (nargs == 1);
17718 if (vec_safe_length (call_args) != 1)
17720 error_at (EXPR_LOC_OR_LOC (t, input_location),
17721 "wrong number of arguments to "
17722 "%<__builtin_launder%>");
17723 ret = error_mark_node;
17725 else
17726 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17727 input_location),
17728 (*call_args)[0], complain);
17729 break;
17731 default:
17732 /* Unsupported internal function with arguments. */
17733 gcc_unreachable ();
17735 else if (TREE_CODE (function) == OFFSET_REF)
17736 ret = build_offset_ref_call_from_tree (function, &call_args,
17737 complain);
17738 else if (TREE_CODE (function) == COMPONENT_REF)
17740 tree instance = TREE_OPERAND (function, 0);
17741 tree fn = TREE_OPERAND (function, 1);
17743 if (processing_template_decl
17744 && (type_dependent_expression_p (instance)
17745 || (!BASELINK_P (fn)
17746 && TREE_CODE (fn) != FIELD_DECL)
17747 || type_dependent_expression_p (fn)
17748 || any_type_dependent_arguments_p (call_args)))
17749 ret = build_min_nt_call_vec (function, call_args);
17750 else if (!BASELINK_P (fn))
17751 ret = finish_call_expr (function, &call_args,
17752 /*disallow_virtual=*/false,
17753 /*koenig_p=*/false,
17754 complain);
17755 else
17756 ret = (build_new_method_call
17757 (instance, fn,
17758 &call_args, NULL_TREE,
17759 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17760 /*fn_p=*/NULL,
17761 complain));
17763 else
17764 ret = finish_call_expr (function, &call_args,
17765 /*disallow_virtual=*/qualified_p,
17766 koenig_p,
17767 complain);
17769 release_tree_vector (call_args);
17771 if (ret != error_mark_node)
17773 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17774 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17775 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17776 bool thk = CALL_FROM_THUNK_P (t);
17777 if (op || ord || rev || thk)
17779 function = extract_call_expr (ret);
17780 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17781 CALL_EXPR_ORDERED_ARGS (function) = ord;
17782 CALL_EXPR_REVERSE_ARGS (function) = rev;
17783 if (thk)
17785 CALL_FROM_THUNK_P (function) = true;
17786 /* The thunk location is not interesting. */
17787 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17792 RETURN (ret);
17795 case COND_EXPR:
17797 tree cond = RECUR (TREE_OPERAND (t, 0));
17798 tree folded_cond = fold_non_dependent_expr (cond);
17799 tree exp1, exp2;
17801 if (TREE_CODE (folded_cond) == INTEGER_CST)
17803 if (integer_zerop (folded_cond))
17805 ++c_inhibit_evaluation_warnings;
17806 exp1 = RECUR (TREE_OPERAND (t, 1));
17807 --c_inhibit_evaluation_warnings;
17808 exp2 = RECUR (TREE_OPERAND (t, 2));
17810 else
17812 exp1 = RECUR (TREE_OPERAND (t, 1));
17813 ++c_inhibit_evaluation_warnings;
17814 exp2 = RECUR (TREE_OPERAND (t, 2));
17815 --c_inhibit_evaluation_warnings;
17817 cond = folded_cond;
17819 else
17821 exp1 = RECUR (TREE_OPERAND (t, 1));
17822 exp2 = RECUR (TREE_OPERAND (t, 2));
17825 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17826 cond, exp1, exp2, complain));
17829 case PSEUDO_DTOR_EXPR:
17831 tree op0 = RECUR (TREE_OPERAND (t, 0));
17832 tree op1 = RECUR (TREE_OPERAND (t, 1));
17833 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17834 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17835 input_location));
17838 case TREE_LIST:
17840 tree purpose, value, chain;
17842 if (t == void_list_node)
17843 RETURN (t);
17845 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17846 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17848 /* We have pack expansions, so expand those and
17849 create a new list out of it. */
17850 tree purposevec = NULL_TREE;
17851 tree valuevec = NULL_TREE;
17852 tree chain;
17853 int i, len = -1;
17855 /* Expand the argument expressions. */
17856 if (TREE_PURPOSE (t))
17857 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17858 complain, in_decl);
17859 if (TREE_VALUE (t))
17860 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17861 complain, in_decl);
17863 /* Build the rest of the list. */
17864 chain = TREE_CHAIN (t);
17865 if (chain && chain != void_type_node)
17866 chain = RECUR (chain);
17868 /* Determine the number of arguments. */
17869 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17871 len = TREE_VEC_LENGTH (purposevec);
17872 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17874 else if (TREE_CODE (valuevec) == TREE_VEC)
17875 len = TREE_VEC_LENGTH (valuevec);
17876 else
17878 /* Since we only performed a partial substitution into
17879 the argument pack, we only RETURN (a single list
17880 node. */
17881 if (purposevec == TREE_PURPOSE (t)
17882 && valuevec == TREE_VALUE (t)
17883 && chain == TREE_CHAIN (t))
17884 RETURN (t);
17886 RETURN (tree_cons (purposevec, valuevec, chain));
17889 /* Convert the argument vectors into a TREE_LIST */
17890 i = len;
17891 while (i > 0)
17893 /* Grab the Ith values. */
17894 i--;
17895 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17896 : NULL_TREE;
17897 value
17898 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17899 : NULL_TREE;
17901 /* Build the list (backwards). */
17902 chain = tree_cons (purpose, value, chain);
17905 RETURN (chain);
17908 purpose = TREE_PURPOSE (t);
17909 if (purpose)
17910 purpose = RECUR (purpose);
17911 value = TREE_VALUE (t);
17912 if (value)
17913 value = RECUR (value);
17914 chain = TREE_CHAIN (t);
17915 if (chain && chain != void_type_node)
17916 chain = RECUR (chain);
17917 if (purpose == TREE_PURPOSE (t)
17918 && value == TREE_VALUE (t)
17919 && chain == TREE_CHAIN (t))
17920 RETURN (t);
17921 RETURN (tree_cons (purpose, value, chain));
17924 case COMPONENT_REF:
17926 tree object;
17927 tree object_type;
17928 tree member;
17929 tree r;
17931 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17932 args, complain, in_decl);
17933 /* Remember that there was a reference to this entity. */
17934 if (DECL_P (object)
17935 && !mark_used (object, complain) && !(complain & tf_error))
17936 RETURN (error_mark_node);
17937 object_type = TREE_TYPE (object);
17939 member = TREE_OPERAND (t, 1);
17940 if (BASELINK_P (member))
17941 member = tsubst_baselink (member,
17942 non_reference (TREE_TYPE (object)),
17943 args, complain, in_decl);
17944 else
17945 member = tsubst_copy (member, args, complain, in_decl);
17946 if (member == error_mark_node)
17947 RETURN (error_mark_node);
17949 if (TREE_CODE (member) == FIELD_DECL)
17951 r = finish_non_static_data_member (member, object, NULL_TREE);
17952 if (TREE_CODE (r) == COMPONENT_REF)
17953 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17954 RETURN (r);
17956 else if (type_dependent_expression_p (object))
17957 /* We can't do much here. */;
17958 else if (!CLASS_TYPE_P (object_type))
17960 if (scalarish_type_p (object_type))
17962 tree s = NULL_TREE;
17963 tree dtor = member;
17965 if (TREE_CODE (dtor) == SCOPE_REF)
17967 s = TREE_OPERAND (dtor, 0);
17968 dtor = TREE_OPERAND (dtor, 1);
17970 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17972 dtor = TREE_OPERAND (dtor, 0);
17973 if (TYPE_P (dtor))
17974 RETURN (finish_pseudo_destructor_expr
17975 (object, s, dtor, input_location));
17979 else if (TREE_CODE (member) == SCOPE_REF
17980 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17982 /* Lookup the template functions now that we know what the
17983 scope is. */
17984 tree scope = TREE_OPERAND (member, 0);
17985 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17986 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17987 member = lookup_qualified_name (scope, tmpl,
17988 /*is_type_p=*/false,
17989 /*complain=*/false);
17990 if (BASELINK_P (member))
17992 BASELINK_FUNCTIONS (member)
17993 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17994 args);
17995 member = (adjust_result_of_qualified_name_lookup
17996 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17997 object_type));
17999 else
18001 qualified_name_lookup_error (scope, tmpl, member,
18002 input_location);
18003 RETURN (error_mark_node);
18006 else if (TREE_CODE (member) == SCOPE_REF
18007 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18008 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18010 if (complain & tf_error)
18012 if (TYPE_P (TREE_OPERAND (member, 0)))
18013 error ("%qT is not a class or namespace",
18014 TREE_OPERAND (member, 0));
18015 else
18016 error ("%qD is not a class or namespace",
18017 TREE_OPERAND (member, 0));
18019 RETURN (error_mark_node);
18022 r = finish_class_member_access_expr (object, member,
18023 /*template_p=*/false,
18024 complain);
18025 if (TREE_CODE (r) == COMPONENT_REF)
18026 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18027 RETURN (r);
18030 case THROW_EXPR:
18031 RETURN (build_throw
18032 (RECUR (TREE_OPERAND (t, 0))));
18034 case CONSTRUCTOR:
18036 vec<constructor_elt, va_gc> *n;
18037 constructor_elt *ce;
18038 unsigned HOST_WIDE_INT idx;
18039 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18040 bool process_index_p;
18041 int newlen;
18042 bool need_copy_p = false;
18043 tree r;
18045 if (type == error_mark_node)
18046 RETURN (error_mark_node);
18048 /* digest_init will do the wrong thing if we let it. */
18049 if (type && TYPE_PTRMEMFUNC_P (type))
18050 RETURN (t);
18052 /* We do not want to process the index of aggregate
18053 initializers as they are identifier nodes which will be
18054 looked up by digest_init. */
18055 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18057 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18058 newlen = vec_safe_length (n);
18059 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18061 if (ce->index && process_index_p
18062 /* An identifier index is looked up in the type
18063 being initialized, not the current scope. */
18064 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18065 ce->index = RECUR (ce->index);
18067 if (PACK_EXPANSION_P (ce->value))
18069 /* Substitute into the pack expansion. */
18070 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18071 in_decl);
18073 if (ce->value == error_mark_node
18074 || PACK_EXPANSION_P (ce->value))
18076 else if (TREE_VEC_LENGTH (ce->value) == 1)
18077 /* Just move the argument into place. */
18078 ce->value = TREE_VEC_ELT (ce->value, 0);
18079 else
18081 /* Update the length of the final CONSTRUCTOR
18082 arguments vector, and note that we will need to
18083 copy.*/
18084 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18085 need_copy_p = true;
18088 else
18089 ce->value = RECUR (ce->value);
18092 if (need_copy_p)
18094 vec<constructor_elt, va_gc> *old_n = n;
18096 vec_alloc (n, newlen);
18097 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18099 if (TREE_CODE (ce->value) == TREE_VEC)
18101 int i, len = TREE_VEC_LENGTH (ce->value);
18102 for (i = 0; i < len; ++i)
18103 CONSTRUCTOR_APPEND_ELT (n, 0,
18104 TREE_VEC_ELT (ce->value, i));
18106 else
18107 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18111 r = build_constructor (init_list_type_node, n);
18112 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18114 if (TREE_HAS_CONSTRUCTOR (t))
18116 fcl_t cl = fcl_functional;
18117 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18118 cl = fcl_c99;
18119 RETURN (finish_compound_literal (type, r, complain, cl));
18122 TREE_TYPE (r) = type;
18123 RETURN (r);
18126 case TYPEID_EXPR:
18128 tree operand_0 = TREE_OPERAND (t, 0);
18129 if (TYPE_P (operand_0))
18131 operand_0 = tsubst (operand_0, args, complain, in_decl);
18132 RETURN (get_typeid (operand_0, complain));
18134 else
18136 operand_0 = RECUR (operand_0);
18137 RETURN (build_typeid (operand_0, complain));
18141 case VAR_DECL:
18142 if (!args)
18143 RETURN (t);
18144 else if (DECL_PACK_P (t))
18146 /* We don't build decls for an instantiation of a
18147 variadic capture proxy, we instantiate the elements
18148 when needed. */
18149 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18150 return RECUR (DECL_VALUE_EXPR (t));
18152 /* Fall through */
18154 case PARM_DECL:
18156 tree r = tsubst_copy (t, args, complain, in_decl);
18157 /* ??? We're doing a subset of finish_id_expression here. */
18158 if (VAR_P (r)
18159 && !processing_template_decl
18160 && !cp_unevaluated_operand
18161 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18162 && CP_DECL_THREAD_LOCAL_P (r))
18164 if (tree wrap = get_tls_wrapper_fn (r))
18165 /* Replace an evaluated use of the thread_local variable with
18166 a call to its wrapper. */
18167 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18169 else if (outer_automatic_var_p (r))
18170 r = process_outer_var_ref (r, complain);
18172 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18173 /* If the original type was a reference, we'll be wrapped in
18174 the appropriate INDIRECT_REF. */
18175 r = convert_from_reference (r);
18176 RETURN (r);
18179 case VA_ARG_EXPR:
18181 tree op0 = RECUR (TREE_OPERAND (t, 0));
18182 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18183 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18186 case OFFSETOF_EXPR:
18188 tree object_ptr
18189 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18190 in_decl, /*function_p=*/false,
18191 /*integral_constant_expression_p=*/false);
18192 RETURN (finish_offsetof (object_ptr,
18193 RECUR (TREE_OPERAND (t, 0)),
18194 EXPR_LOCATION (t)));
18197 case ADDRESSOF_EXPR:
18198 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18199 RECUR (TREE_OPERAND (t, 0)), complain));
18201 case TRAIT_EXPR:
18203 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18204 complain, in_decl);
18206 tree type2 = TRAIT_EXPR_TYPE2 (t);
18207 if (type2 && TREE_CODE (type2) == TREE_LIST)
18208 type2 = RECUR (type2);
18209 else if (type2)
18210 type2 = tsubst (type2, args, complain, in_decl);
18212 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18215 case STMT_EXPR:
18217 tree old_stmt_expr = cur_stmt_expr;
18218 tree stmt_expr = begin_stmt_expr ();
18220 cur_stmt_expr = stmt_expr;
18221 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18222 integral_constant_expression_p);
18223 stmt_expr = finish_stmt_expr (stmt_expr, false);
18224 cur_stmt_expr = old_stmt_expr;
18226 /* If the resulting list of expression statement is empty,
18227 fold it further into void_node. */
18228 if (empty_expr_stmt_p (stmt_expr))
18229 stmt_expr = void_node;
18231 RETURN (stmt_expr);
18234 case LAMBDA_EXPR:
18236 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18238 RETURN (build_lambda_object (r));
18241 case TARGET_EXPR:
18242 /* We can get here for a constant initializer of non-dependent type.
18243 FIXME stop folding in cp_parser_initializer_clause. */
18245 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18246 complain);
18247 RETURN (r);
18250 case TRANSACTION_EXPR:
18251 RETURN (tsubst_expr(t, args, complain, in_decl,
18252 integral_constant_expression_p));
18254 case PAREN_EXPR:
18255 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18257 case VEC_PERM_EXPR:
18259 tree op0 = RECUR (TREE_OPERAND (t, 0));
18260 tree op1 = RECUR (TREE_OPERAND (t, 1));
18261 tree op2 = RECUR (TREE_OPERAND (t, 2));
18262 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18263 complain));
18266 case REQUIRES_EXPR:
18267 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18269 default:
18270 /* Handle Objective-C++ constructs, if appropriate. */
18272 tree subst
18273 = objcp_tsubst_copy_and_build (t, args, complain,
18274 in_decl, /*function_p=*/false);
18275 if (subst)
18276 RETURN (subst);
18278 RETURN (tsubst_copy (t, args, complain, in_decl));
18281 #undef RECUR
18282 #undef RETURN
18283 out:
18284 input_location = loc;
18285 return retval;
18288 /* Verify that the instantiated ARGS are valid. For type arguments,
18289 make sure that the type's linkage is ok. For non-type arguments,
18290 make sure they are constants if they are integral or enumerations.
18291 Emit an error under control of COMPLAIN, and return TRUE on error. */
18293 static bool
18294 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18296 if (dependent_template_arg_p (t))
18297 return false;
18298 if (ARGUMENT_PACK_P (t))
18300 tree vec = ARGUMENT_PACK_ARGS (t);
18301 int len = TREE_VEC_LENGTH (vec);
18302 bool result = false;
18303 int i;
18305 for (i = 0; i < len; ++i)
18306 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18307 result = true;
18308 return result;
18310 else if (TYPE_P (t))
18312 /* [basic.link]: A name with no linkage (notably, the name
18313 of a class or enumeration declared in a local scope)
18314 shall not be used to declare an entity with linkage.
18315 This implies that names with no linkage cannot be used as
18316 template arguments
18318 DR 757 relaxes this restriction for C++0x. */
18319 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18320 : no_linkage_check (t, /*relaxed_p=*/false));
18322 if (nt)
18324 /* DR 488 makes use of a type with no linkage cause
18325 type deduction to fail. */
18326 if (complain & tf_error)
18328 if (TYPE_UNNAMED_P (nt))
18329 error ("%qT is/uses unnamed type", t);
18330 else
18331 error ("template argument for %qD uses local type %qT",
18332 tmpl, t);
18334 return true;
18336 /* In order to avoid all sorts of complications, we do not
18337 allow variably-modified types as template arguments. */
18338 else if (variably_modified_type_p (t, NULL_TREE))
18340 if (complain & tf_error)
18341 error ("%qT is a variably modified type", t);
18342 return true;
18345 /* Class template and alias template arguments should be OK. */
18346 else if (DECL_TYPE_TEMPLATE_P (t))
18348 /* A non-type argument of integral or enumerated type must be a
18349 constant. */
18350 else if (TREE_TYPE (t)
18351 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18352 && !REFERENCE_REF_P (t)
18353 && !TREE_CONSTANT (t))
18355 if (complain & tf_error)
18356 error ("integral expression %qE is not constant", t);
18357 return true;
18359 return false;
18362 static bool
18363 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18365 int ix, len = DECL_NTPARMS (tmpl);
18366 bool result = false;
18368 for (ix = 0; ix != len; ix++)
18370 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18371 result = true;
18373 if (result && (complain & tf_error))
18374 error (" trying to instantiate %qD", tmpl);
18375 return result;
18378 /* We're out of SFINAE context now, so generate diagnostics for the access
18379 errors we saw earlier when instantiating D from TMPL and ARGS. */
18381 static void
18382 recheck_decl_substitution (tree d, tree tmpl, tree args)
18384 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18385 tree type = TREE_TYPE (pattern);
18386 location_t loc = input_location;
18388 push_access_scope (d);
18389 push_deferring_access_checks (dk_no_deferred);
18390 input_location = DECL_SOURCE_LOCATION (pattern);
18391 tsubst (type, args, tf_warning_or_error, d);
18392 input_location = loc;
18393 pop_deferring_access_checks ();
18394 pop_access_scope (d);
18397 /* Instantiate the indicated variable, function, or alias template TMPL with
18398 the template arguments in TARG_PTR. */
18400 static tree
18401 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18403 tree targ_ptr = orig_args;
18404 tree fndecl;
18405 tree gen_tmpl;
18406 tree spec;
18407 bool access_ok = true;
18409 if (tmpl == error_mark_node)
18410 return error_mark_node;
18412 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18414 /* If this function is a clone, handle it specially. */
18415 if (DECL_CLONED_FUNCTION_P (tmpl))
18417 tree spec;
18418 tree clone;
18420 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18421 DECL_CLONED_FUNCTION. */
18422 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18423 targ_ptr, complain);
18424 if (spec == error_mark_node)
18425 return error_mark_node;
18427 /* Look for the clone. */
18428 FOR_EACH_CLONE (clone, spec)
18429 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18430 return clone;
18431 /* We should always have found the clone by now. */
18432 gcc_unreachable ();
18433 return NULL_TREE;
18436 if (targ_ptr == error_mark_node)
18437 return error_mark_node;
18439 /* Check to see if we already have this specialization. */
18440 gen_tmpl = most_general_template (tmpl);
18441 if (TMPL_ARGS_DEPTH (targ_ptr)
18442 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18443 /* targ_ptr only has the innermost template args, so add the outer ones
18444 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18445 the case of a non-dependent call within a template definition). */
18446 targ_ptr = (add_outermost_template_args
18447 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18448 targ_ptr));
18450 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18451 but it doesn't seem to be on the hot path. */
18452 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18454 gcc_assert (tmpl == gen_tmpl
18455 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18456 == spec)
18457 || fndecl == NULL_TREE);
18459 if (spec != NULL_TREE)
18461 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18463 if (complain & tf_error)
18464 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18465 return error_mark_node;
18467 return spec;
18470 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18471 complain))
18472 return error_mark_node;
18474 /* We are building a FUNCTION_DECL, during which the access of its
18475 parameters and return types have to be checked. However this
18476 FUNCTION_DECL which is the desired context for access checking
18477 is not built yet. We solve this chicken-and-egg problem by
18478 deferring all checks until we have the FUNCTION_DECL. */
18479 push_deferring_access_checks (dk_deferred);
18481 /* Instantiation of the function happens in the context of the function
18482 template, not the context of the overload resolution we're doing. */
18483 push_to_top_level ();
18484 /* If there are dependent arguments, e.g. because we're doing partial
18485 ordering, make sure processing_template_decl stays set. */
18486 if (uses_template_parms (targ_ptr))
18487 ++processing_template_decl;
18488 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18490 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18491 complain, gen_tmpl, true);
18492 push_nested_class (ctx);
18495 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18497 fndecl = NULL_TREE;
18498 if (VAR_P (pattern))
18500 /* We need to determine if we're using a partial or explicit
18501 specialization now, because the type of the variable could be
18502 different. */
18503 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18504 tree elt = most_specialized_partial_spec (tid, complain);
18505 if (elt == error_mark_node)
18506 pattern = error_mark_node;
18507 else if (elt)
18509 tree partial_tmpl = TREE_VALUE (elt);
18510 tree partial_args = TREE_PURPOSE (elt);
18511 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18512 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18516 /* Substitute template parameters to obtain the specialization. */
18517 if (fndecl == NULL_TREE)
18518 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18519 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18520 pop_nested_class ();
18521 pop_from_top_level ();
18523 if (fndecl == error_mark_node)
18525 pop_deferring_access_checks ();
18526 return error_mark_node;
18529 /* The DECL_TI_TEMPLATE should always be the immediate parent
18530 template, not the most general template. */
18531 DECL_TI_TEMPLATE (fndecl) = tmpl;
18532 DECL_TI_ARGS (fndecl) = targ_ptr;
18534 /* Now we know the specialization, compute access previously
18535 deferred. Do no access control for inheriting constructors,
18536 as we already checked access for the inherited constructor. */
18537 if (!(flag_new_inheriting_ctors
18538 && DECL_INHERITED_CTOR (fndecl)))
18540 push_access_scope (fndecl);
18541 if (!perform_deferred_access_checks (complain))
18542 access_ok = false;
18543 pop_access_scope (fndecl);
18545 pop_deferring_access_checks ();
18547 /* If we've just instantiated the main entry point for a function,
18548 instantiate all the alternate entry points as well. We do this
18549 by cloning the instantiation of the main entry point, not by
18550 instantiating the template clones. */
18551 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18552 clone_function_decl (fndecl, /*update_methods=*/false);
18554 if (!access_ok)
18556 if (!(complain & tf_error))
18558 /* Remember to reinstantiate when we're out of SFINAE so the user
18559 can see the errors. */
18560 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18562 return error_mark_node;
18564 return fndecl;
18567 /* Wrapper for instantiate_template_1. */
18569 tree
18570 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18572 tree ret;
18573 timevar_push (TV_TEMPLATE_INST);
18574 ret = instantiate_template_1 (tmpl, orig_args, complain);
18575 timevar_pop (TV_TEMPLATE_INST);
18576 return ret;
18579 /* Instantiate the alias template TMPL with ARGS. Also push a template
18580 instantiation level, which instantiate_template doesn't do because
18581 functions and variables have sufficient context established by the
18582 callers. */
18584 static tree
18585 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18587 struct pending_template *old_last_pend = last_pending_template;
18588 struct tinst_level *old_error_tinst = last_error_tinst_level;
18589 if (tmpl == error_mark_node || args == error_mark_node)
18590 return error_mark_node;
18591 tree tinst = build_tree_list (tmpl, args);
18592 if (!push_tinst_level (tinst))
18594 ggc_free (tinst);
18595 return error_mark_node;
18598 args =
18599 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18600 args, tmpl, complain,
18601 /*require_all_args=*/true,
18602 /*use_default_args=*/true);
18604 tree r = instantiate_template (tmpl, args, complain);
18605 pop_tinst_level ();
18606 /* We can't free this if a pending_template entry or last_error_tinst_level
18607 is pointing at it. */
18608 if (last_pending_template == old_last_pend
18609 && last_error_tinst_level == old_error_tinst)
18610 ggc_free (tinst);
18612 return r;
18615 /* PARM is a template parameter pack for FN. Returns true iff
18616 PARM is used in a deducible way in the argument list of FN. */
18618 static bool
18619 pack_deducible_p (tree parm, tree fn)
18621 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18622 for (; t; t = TREE_CHAIN (t))
18624 tree type = TREE_VALUE (t);
18625 tree packs;
18626 if (!PACK_EXPANSION_P (type))
18627 continue;
18628 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18629 packs; packs = TREE_CHAIN (packs))
18630 if (template_args_equal (TREE_VALUE (packs), parm))
18632 /* The template parameter pack is used in a function parameter
18633 pack. If this is the end of the parameter list, the
18634 template parameter pack is deducible. */
18635 if (TREE_CHAIN (t) == void_list_node)
18636 return true;
18637 else
18638 /* Otherwise, not. Well, it could be deduced from
18639 a non-pack parameter, but doing so would end up with
18640 a deduction mismatch, so don't bother. */
18641 return false;
18644 /* The template parameter pack isn't used in any function parameter
18645 packs, but it might be used deeper, e.g. tuple<Args...>. */
18646 return true;
18649 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18650 NARGS elements of the arguments that are being used when calling
18651 it. TARGS is a vector into which the deduced template arguments
18652 are placed.
18654 Returns either a FUNCTION_DECL for the matching specialization of FN or
18655 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18656 true, diagnostics will be printed to explain why it failed.
18658 If FN is a conversion operator, or we are trying to produce a specific
18659 specialization, RETURN_TYPE is the return type desired.
18661 The EXPLICIT_TARGS are explicit template arguments provided via a
18662 template-id.
18664 The parameter STRICT is one of:
18666 DEDUCE_CALL:
18667 We are deducing arguments for a function call, as in
18668 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18669 deducing arguments for a call to the result of a conversion
18670 function template, as in [over.call.object].
18672 DEDUCE_CONV:
18673 We are deducing arguments for a conversion function, as in
18674 [temp.deduct.conv].
18676 DEDUCE_EXACT:
18677 We are deducing arguments when doing an explicit instantiation
18678 as in [temp.explicit], when determining an explicit specialization
18679 as in [temp.expl.spec], or when taking the address of a function
18680 template, as in [temp.deduct.funcaddr]. */
18682 tree
18683 fn_type_unification (tree fn,
18684 tree explicit_targs,
18685 tree targs,
18686 const tree *args,
18687 unsigned int nargs,
18688 tree return_type,
18689 unification_kind_t strict,
18690 int flags,
18691 bool explain_p,
18692 bool decltype_p)
18694 tree parms;
18695 tree fntype;
18696 tree decl = NULL_TREE;
18697 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18698 bool ok;
18699 static int deduction_depth;
18700 struct pending_template *old_last_pend = last_pending_template;
18701 struct tinst_level *old_error_tinst = last_error_tinst_level;
18703 tree orig_fn = fn;
18704 if (flag_new_inheriting_ctors)
18705 fn = strip_inheriting_ctors (fn);
18707 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18708 tree tinst;
18709 tree r = error_mark_node;
18711 tree full_targs = targs;
18712 if (TMPL_ARGS_DEPTH (targs)
18713 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18714 full_targs = (add_outermost_template_args
18715 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18716 targs));
18718 if (decltype_p)
18719 complain |= tf_decltype;
18721 /* In C++0x, it's possible to have a function template whose type depends
18722 on itself recursively. This is most obvious with decltype, but can also
18723 occur with enumeration scope (c++/48969). So we need to catch infinite
18724 recursion and reject the substitution at deduction time; this function
18725 will return error_mark_node for any repeated substitution.
18727 This also catches excessive recursion such as when f<N> depends on
18728 f<N-1> across all integers, and returns error_mark_node for all the
18729 substitutions back up to the initial one.
18731 This is, of course, not reentrant. */
18732 if (excessive_deduction_depth)
18733 return error_mark_node;
18734 tinst = build_tree_list (fn, NULL_TREE);
18735 ++deduction_depth;
18737 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18739 fntype = TREE_TYPE (fn);
18740 if (explicit_targs)
18742 /* [temp.deduct]
18744 The specified template arguments must match the template
18745 parameters in kind (i.e., type, nontype, template), and there
18746 must not be more arguments than there are parameters;
18747 otherwise type deduction fails.
18749 Nontype arguments must match the types of the corresponding
18750 nontype template parameters, or must be convertible to the
18751 types of the corresponding nontype parameters as specified in
18752 _temp.arg.nontype_, otherwise type deduction fails.
18754 All references in the function type of the function template
18755 to the corresponding template parameters are replaced by the
18756 specified template argument values. If a substitution in a
18757 template parameter or in the function type of the function
18758 template results in an invalid type, type deduction fails. */
18759 int i, len = TREE_VEC_LENGTH (tparms);
18760 location_t loc = input_location;
18761 bool incomplete = false;
18763 if (explicit_targs == error_mark_node)
18764 goto fail;
18766 if (TMPL_ARGS_DEPTH (explicit_targs)
18767 < TMPL_ARGS_DEPTH (full_targs))
18768 explicit_targs = add_outermost_template_args (full_targs,
18769 explicit_targs);
18771 /* Adjust any explicit template arguments before entering the
18772 substitution context. */
18773 explicit_targs
18774 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18775 complain,
18776 /*require_all_args=*/false,
18777 /*use_default_args=*/false));
18778 if (explicit_targs == error_mark_node)
18779 goto fail;
18781 /* Substitute the explicit args into the function type. This is
18782 necessary so that, for instance, explicitly declared function
18783 arguments can match null pointed constants. If we were given
18784 an incomplete set of explicit args, we must not do semantic
18785 processing during substitution as we could create partial
18786 instantiations. */
18787 for (i = 0; i < len; i++)
18789 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18790 bool parameter_pack = false;
18791 tree targ = TREE_VEC_ELT (explicit_targs, i);
18793 /* Dig out the actual parm. */
18794 if (TREE_CODE (parm) == TYPE_DECL
18795 || TREE_CODE (parm) == TEMPLATE_DECL)
18797 parm = TREE_TYPE (parm);
18798 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18800 else if (TREE_CODE (parm) == PARM_DECL)
18802 parm = DECL_INITIAL (parm);
18803 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18806 if (!parameter_pack && targ == NULL_TREE)
18807 /* No explicit argument for this template parameter. */
18808 incomplete = true;
18810 if (parameter_pack && pack_deducible_p (parm, fn))
18812 /* Mark the argument pack as "incomplete". We could
18813 still deduce more arguments during unification.
18814 We remove this mark in type_unification_real. */
18815 if (targ)
18817 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18818 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18819 = ARGUMENT_PACK_ARGS (targ);
18822 /* We have some incomplete argument packs. */
18823 incomplete = true;
18827 TREE_VALUE (tinst) = explicit_targs;
18828 if (!push_tinst_level (tinst))
18830 excessive_deduction_depth = true;
18831 goto fail;
18833 processing_template_decl += incomplete;
18834 input_location = DECL_SOURCE_LOCATION (fn);
18835 /* Ignore any access checks; we'll see them again in
18836 instantiate_template and they might have the wrong
18837 access path at this point. */
18838 push_deferring_access_checks (dk_deferred);
18839 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18840 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18841 pop_deferring_access_checks ();
18842 input_location = loc;
18843 processing_template_decl -= incomplete;
18844 pop_tinst_level ();
18846 if (fntype == error_mark_node)
18847 goto fail;
18849 /* Place the explicitly specified arguments in TARGS. */
18850 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18851 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18852 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18855 /* Never do unification on the 'this' parameter. */
18856 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18858 if (return_type && strict == DEDUCE_CALL)
18860 /* We're deducing for a call to the result of a template conversion
18861 function. The parms we really want are in return_type. */
18862 if (POINTER_TYPE_P (return_type))
18863 return_type = TREE_TYPE (return_type);
18864 parms = TYPE_ARG_TYPES (return_type);
18866 else if (return_type)
18868 tree *new_args;
18870 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18871 new_args = XALLOCAVEC (tree, nargs + 1);
18872 new_args[0] = return_type;
18873 memcpy (new_args + 1, args, nargs * sizeof (tree));
18874 args = new_args;
18875 ++nargs;
18878 /* We allow incomplete unification without an error message here
18879 because the standard doesn't seem to explicitly prohibit it. Our
18880 callers must be ready to deal with unification failures in any
18881 event. */
18883 TREE_VALUE (tinst) = targs;
18884 /* If we aren't explaining yet, push tinst context so we can see where
18885 any errors (e.g. from class instantiations triggered by instantiation
18886 of default template arguments) come from. If we are explaining, this
18887 context is redundant. */
18888 if (!explain_p && !push_tinst_level (tinst))
18890 excessive_deduction_depth = true;
18891 goto fail;
18894 /* type_unification_real will pass back any access checks from default
18895 template argument substitution. */
18896 vec<deferred_access_check, va_gc> *checks;
18897 checks = NULL;
18899 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18900 full_targs, parms, args, nargs, /*subr=*/0,
18901 strict, flags, &checks, explain_p);
18902 if (!explain_p)
18903 pop_tinst_level ();
18904 if (!ok)
18905 goto fail;
18907 /* Now that we have bindings for all of the template arguments,
18908 ensure that the arguments deduced for the template template
18909 parameters have compatible template parameter lists. We cannot
18910 check this property before we have deduced all template
18911 arguments, because the template parameter types of a template
18912 template parameter might depend on prior template parameters
18913 deduced after the template template parameter. The following
18914 ill-formed example illustrates this issue:
18916 template<typename T, template<T> class C> void f(C<5>, T);
18918 template<int N> struct X {};
18920 void g() {
18921 f(X<5>(), 5l); // error: template argument deduction fails
18924 The template parameter list of 'C' depends on the template type
18925 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18926 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18927 time that we deduce 'C'. */
18928 if (!template_template_parm_bindings_ok_p
18929 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18931 unify_inconsistent_template_template_parameters (explain_p);
18932 goto fail;
18935 /* All is well so far. Now, check:
18937 [temp.deduct]
18939 When all template arguments have been deduced, all uses of
18940 template parameters in nondeduced contexts are replaced with
18941 the corresponding deduced argument values. If the
18942 substitution results in an invalid type, as described above,
18943 type deduction fails. */
18944 TREE_VALUE (tinst) = targs;
18945 if (!push_tinst_level (tinst))
18947 excessive_deduction_depth = true;
18948 goto fail;
18951 /* Also collect access checks from the instantiation. */
18952 reopen_deferring_access_checks (checks);
18954 decl = instantiate_template (fn, targs, complain);
18956 checks = get_deferred_access_checks ();
18957 pop_deferring_access_checks ();
18959 pop_tinst_level ();
18961 if (decl == error_mark_node)
18962 goto fail;
18964 /* Now perform any access checks encountered during substitution. */
18965 push_access_scope (decl);
18966 ok = perform_access_checks (checks, complain);
18967 pop_access_scope (decl);
18968 if (!ok)
18969 goto fail;
18971 /* If we're looking for an exact match, check that what we got
18972 is indeed an exact match. It might not be if some template
18973 parameters are used in non-deduced contexts. But don't check
18974 for an exact match if we have dependent template arguments;
18975 in that case we're doing partial ordering, and we already know
18976 that we have two candidates that will provide the actual type. */
18977 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18979 tree substed = TREE_TYPE (decl);
18980 unsigned int i;
18982 tree sarg
18983 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18984 if (return_type)
18985 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18986 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18987 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18989 unify_type_mismatch (explain_p, args[i],
18990 TREE_VALUE (sarg));
18991 goto fail;
18995 /* After doing deduction with the inherited constructor, actually return an
18996 instantiation of the inheriting constructor. */
18997 if (orig_fn != fn)
18998 decl = instantiate_template (orig_fn, targs, complain);
19000 r = decl;
19002 fail:
19003 --deduction_depth;
19004 if (excessive_deduction_depth)
19006 if (deduction_depth == 0)
19007 /* Reset once we're all the way out. */
19008 excessive_deduction_depth = false;
19011 /* We can't free this if a pending_template entry or last_error_tinst_level
19012 is pointing at it. */
19013 if (last_pending_template == old_last_pend
19014 && last_error_tinst_level == old_error_tinst)
19015 ggc_free (tinst);
19017 return r;
19020 /* Adjust types before performing type deduction, as described in
19021 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19022 sections are symmetric. PARM is the type of a function parameter
19023 or the return type of the conversion function. ARG is the type of
19024 the argument passed to the call, or the type of the value
19025 initialized with the result of the conversion function.
19026 ARG_EXPR is the original argument expression, which may be null. */
19028 static int
19029 maybe_adjust_types_for_deduction (unification_kind_t strict,
19030 tree* parm,
19031 tree* arg,
19032 tree arg_expr)
19034 int result = 0;
19036 switch (strict)
19038 case DEDUCE_CALL:
19039 break;
19041 case DEDUCE_CONV:
19042 /* Swap PARM and ARG throughout the remainder of this
19043 function; the handling is precisely symmetric since PARM
19044 will initialize ARG rather than vice versa. */
19045 std::swap (parm, arg);
19046 break;
19048 case DEDUCE_EXACT:
19049 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19050 too, but here handle it by stripping the reference from PARM
19051 rather than by adding it to ARG. */
19052 if (TREE_CODE (*parm) == REFERENCE_TYPE
19053 && TYPE_REF_IS_RVALUE (*parm)
19054 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19055 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19056 && TREE_CODE (*arg) == REFERENCE_TYPE
19057 && !TYPE_REF_IS_RVALUE (*arg))
19058 *parm = TREE_TYPE (*parm);
19059 /* Nothing else to do in this case. */
19060 return 0;
19062 default:
19063 gcc_unreachable ();
19066 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19068 /* [temp.deduct.call]
19070 If P is not a reference type:
19072 --If A is an array type, the pointer type produced by the
19073 array-to-pointer standard conversion (_conv.array_) is
19074 used in place of A for type deduction; otherwise,
19076 --If A is a function type, the pointer type produced by
19077 the function-to-pointer standard conversion
19078 (_conv.func_) is used in place of A for type deduction;
19079 otherwise,
19081 --If A is a cv-qualified type, the top level
19082 cv-qualifiers of A's type are ignored for type
19083 deduction. */
19084 if (TREE_CODE (*arg) == ARRAY_TYPE)
19085 *arg = build_pointer_type (TREE_TYPE (*arg));
19086 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19087 *arg = build_pointer_type (*arg);
19088 else
19089 *arg = TYPE_MAIN_VARIANT (*arg);
19092 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19093 reference to a cv-unqualified template parameter that does not represent a
19094 template parameter of a class template (during class template argument
19095 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19096 an lvalue, the type "lvalue reference to A" is used in place of A for type
19097 deduction. */
19098 if (TREE_CODE (*parm) == REFERENCE_TYPE
19099 && TYPE_REF_IS_RVALUE (*parm)
19100 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19101 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19102 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19103 && (arg_expr ? lvalue_p (arg_expr)
19104 /* try_one_overload doesn't provide an arg_expr, but
19105 functions are always lvalues. */
19106 : TREE_CODE (*arg) == FUNCTION_TYPE))
19107 *arg = build_reference_type (*arg);
19109 /* [temp.deduct.call]
19111 If P is a cv-qualified type, the top level cv-qualifiers
19112 of P's type are ignored for type deduction. If P is a
19113 reference type, the type referred to by P is used for
19114 type deduction. */
19115 *parm = TYPE_MAIN_VARIANT (*parm);
19116 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19118 *parm = TREE_TYPE (*parm);
19119 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19122 /* DR 322. For conversion deduction, remove a reference type on parm
19123 too (which has been swapped into ARG). */
19124 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19125 *arg = TREE_TYPE (*arg);
19127 return result;
19130 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19131 template which does contain any deducible template parameters; check if
19132 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19133 unify_one_argument. */
19135 static int
19136 check_non_deducible_conversion (tree parm, tree arg, int strict,
19137 int flags, bool explain_p)
19139 tree type;
19141 if (!TYPE_P (arg))
19142 type = TREE_TYPE (arg);
19143 else
19144 type = arg;
19146 if (same_type_p (parm, type))
19147 return unify_success (explain_p);
19149 if (strict == DEDUCE_CONV)
19151 if (can_convert_arg (type, parm, NULL_TREE, flags,
19152 explain_p ? tf_warning_or_error : tf_none))
19153 return unify_success (explain_p);
19155 else if (strict != DEDUCE_EXACT)
19157 if (can_convert_arg (parm, type,
19158 TYPE_P (arg) ? NULL_TREE : arg,
19159 flags, explain_p ? tf_warning_or_error : tf_none))
19160 return unify_success (explain_p);
19163 if (strict == DEDUCE_EXACT)
19164 return unify_type_mismatch (explain_p, parm, arg);
19165 else
19166 return unify_arg_conversion (explain_p, parm, type, arg);
19169 static bool uses_deducible_template_parms (tree type);
19171 /* Returns true iff the expression EXPR is one from which a template
19172 argument can be deduced. In other words, if it's an undecorated
19173 use of a template non-type parameter. */
19175 static bool
19176 deducible_expression (tree expr)
19178 /* Strip implicit conversions. */
19179 while (CONVERT_EXPR_P (expr))
19180 expr = TREE_OPERAND (expr, 0);
19181 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19184 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19185 deducible way; that is, if it has a max value of <PARM> - 1. */
19187 static bool
19188 deducible_array_bound (tree domain)
19190 if (domain == NULL_TREE)
19191 return false;
19193 tree max = TYPE_MAX_VALUE (domain);
19194 if (TREE_CODE (max) != MINUS_EXPR)
19195 return false;
19197 return deducible_expression (TREE_OPERAND (max, 0));
19200 /* Returns true iff the template arguments ARGS use a template parameter
19201 in a deducible way. */
19203 static bool
19204 deducible_template_args (tree args)
19206 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19208 bool deducible;
19209 tree elt = TREE_VEC_ELT (args, i);
19210 if (ARGUMENT_PACK_P (elt))
19211 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19212 else
19214 if (PACK_EXPANSION_P (elt))
19215 elt = PACK_EXPANSION_PATTERN (elt);
19216 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19217 deducible = true;
19218 else if (TYPE_P (elt))
19219 deducible = uses_deducible_template_parms (elt);
19220 else
19221 deducible = deducible_expression (elt);
19223 if (deducible)
19224 return true;
19226 return false;
19229 /* Returns true iff TYPE contains any deducible references to template
19230 parameters, as per 14.8.2.5. */
19232 static bool
19233 uses_deducible_template_parms (tree type)
19235 if (PACK_EXPANSION_P (type))
19236 type = PACK_EXPANSION_PATTERN (type);
19238 /* T
19239 cv-list T
19240 TT<T>
19241 TT<i>
19242 TT<> */
19243 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19244 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19245 return true;
19247 /* T*
19249 T&& */
19250 if (POINTER_TYPE_P (type))
19251 return uses_deducible_template_parms (TREE_TYPE (type));
19253 /* T[integer-constant ]
19254 type [i] */
19255 if (TREE_CODE (type) == ARRAY_TYPE)
19256 return (uses_deducible_template_parms (TREE_TYPE (type))
19257 || deducible_array_bound (TYPE_DOMAIN (type)));
19259 /* T type ::*
19260 type T::*
19261 T T::*
19262 T (type ::*)()
19263 type (T::*)()
19264 type (type ::*)(T)
19265 type (T::*)(T)
19266 T (type ::*)(T)
19267 T (T::*)()
19268 T (T::*)(T) */
19269 if (TYPE_PTRMEM_P (type))
19270 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19271 || (uses_deducible_template_parms
19272 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19274 /* template-name <T> (where template-name refers to a class template)
19275 template-name <i> (where template-name refers to a class template) */
19276 if (CLASS_TYPE_P (type)
19277 && CLASSTYPE_TEMPLATE_INFO (type)
19278 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19279 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19280 (CLASSTYPE_TI_ARGS (type)));
19282 /* type (T)
19284 T(T) */
19285 if (TREE_CODE (type) == FUNCTION_TYPE
19286 || TREE_CODE (type) == METHOD_TYPE)
19288 if (uses_deducible_template_parms (TREE_TYPE (type)))
19289 return true;
19290 tree parm = TYPE_ARG_TYPES (type);
19291 if (TREE_CODE (type) == METHOD_TYPE)
19292 parm = TREE_CHAIN (parm);
19293 for (; parm; parm = TREE_CHAIN (parm))
19294 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19295 return true;
19298 return false;
19301 /* Subroutine of type_unification_real and unify_pack_expansion to
19302 handle unification of a single P/A pair. Parameters are as
19303 for those functions. */
19305 static int
19306 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19307 int subr, unification_kind_t strict,
19308 bool explain_p)
19310 tree arg_expr = NULL_TREE;
19311 int arg_strict;
19313 if (arg == error_mark_node || parm == error_mark_node)
19314 return unify_invalid (explain_p);
19315 if (arg == unknown_type_node)
19316 /* We can't deduce anything from this, but we might get all the
19317 template args from other function args. */
19318 return unify_success (explain_p);
19320 /* Implicit conversions (Clause 4) will be performed on a function
19321 argument to convert it to the type of the corresponding function
19322 parameter if the parameter type contains no template-parameters that
19323 participate in template argument deduction. */
19324 if (strict != DEDUCE_EXACT
19325 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19326 /* For function parameters with no deducible template parameters,
19327 just return. We'll check non-dependent conversions later. */
19328 return unify_success (explain_p);
19330 switch (strict)
19332 case DEDUCE_CALL:
19333 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19334 | UNIFY_ALLOW_MORE_CV_QUAL
19335 | UNIFY_ALLOW_DERIVED);
19336 break;
19338 case DEDUCE_CONV:
19339 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19340 break;
19342 case DEDUCE_EXACT:
19343 arg_strict = UNIFY_ALLOW_NONE;
19344 break;
19346 default:
19347 gcc_unreachable ();
19350 /* We only do these transformations if this is the top-level
19351 parameter_type_list in a call or declaration matching; in other
19352 situations (nested function declarators, template argument lists) we
19353 won't be comparing a type to an expression, and we don't do any type
19354 adjustments. */
19355 if (!subr)
19357 if (!TYPE_P (arg))
19359 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19360 if (type_unknown_p (arg))
19362 /* [temp.deduct.type] A template-argument can be
19363 deduced from a pointer to function or pointer
19364 to member function argument if the set of
19365 overloaded functions does not contain function
19366 templates and at most one of a set of
19367 overloaded functions provides a unique
19368 match. */
19369 resolve_overloaded_unification (tparms, targs, parm,
19370 arg, strict,
19371 arg_strict, explain_p);
19372 /* If a unique match was not found, this is a
19373 non-deduced context, so we still succeed. */
19374 return unify_success (explain_p);
19377 arg_expr = arg;
19378 arg = unlowered_expr_type (arg);
19379 if (arg == error_mark_node)
19380 return unify_invalid (explain_p);
19383 arg_strict |=
19384 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19386 else
19387 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19388 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19389 return unify_template_argument_mismatch (explain_p, parm, arg);
19391 /* For deduction from an init-list we need the actual list. */
19392 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19393 arg = arg_expr;
19394 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19397 /* for_each_template_parm callback that always returns 0. */
19399 static int
19400 zero_r (tree, void *)
19402 return 0;
19405 /* for_each_template_parm any_fn callback to handle deduction of a template
19406 type argument from the type of an array bound. */
19408 static int
19409 array_deduction_r (tree t, void *data)
19411 tree_pair_p d = (tree_pair_p)data;
19412 tree &tparms = d->purpose;
19413 tree &targs = d->value;
19415 if (TREE_CODE (t) == ARRAY_TYPE)
19416 if (tree dom = TYPE_DOMAIN (t))
19417 if (tree max = TYPE_MAX_VALUE (dom))
19419 if (TREE_CODE (max) == MINUS_EXPR)
19420 max = TREE_OPERAND (max, 0);
19421 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19422 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19423 UNIFY_ALLOW_NONE, /*explain*/false);
19426 /* Keep walking. */
19427 return 0;
19430 /* Try to deduce any not-yet-deduced template type arguments from the type of
19431 an array bound. This is handled separately from unify because 14.8.2.5 says
19432 "The type of a type parameter is only deduced from an array bound if it is
19433 not otherwise deduced." */
19435 static void
19436 try_array_deduction (tree tparms, tree targs, tree parm)
19438 tree_pair_s data = { tparms, targs };
19439 hash_set<tree> visited;
19440 for_each_template_parm (parm, zero_r, &data, &visited,
19441 /*nondeduced*/false, array_deduction_r);
19444 /* Most parms like fn_type_unification.
19446 If SUBR is 1, we're being called recursively (to unify the
19447 arguments of a function or method parameter of a function
19448 template).
19450 CHECKS is a pointer to a vector of access checks encountered while
19451 substituting default template arguments. */
19453 static int
19454 type_unification_real (tree tparms,
19455 tree full_targs,
19456 tree xparms,
19457 const tree *xargs,
19458 unsigned int xnargs,
19459 int subr,
19460 unification_kind_t strict,
19461 int flags,
19462 vec<deferred_access_check, va_gc> **checks,
19463 bool explain_p)
19465 tree parm, arg;
19466 int i;
19467 int ntparms = TREE_VEC_LENGTH (tparms);
19468 int saw_undeduced = 0;
19469 tree parms;
19470 const tree *args;
19471 unsigned int nargs;
19472 unsigned int ia;
19474 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19475 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19476 gcc_assert (ntparms > 0);
19478 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19480 /* Reset the number of non-defaulted template arguments contained
19481 in TARGS. */
19482 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19484 again:
19485 parms = xparms;
19486 args = xargs;
19487 nargs = xnargs;
19489 ia = 0;
19490 while (parms && parms != void_list_node
19491 && ia < nargs)
19493 parm = TREE_VALUE (parms);
19495 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19496 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19497 /* For a function parameter pack that occurs at the end of the
19498 parameter-declaration-list, the type A of each remaining
19499 argument of the call is compared with the type P of the
19500 declarator-id of the function parameter pack. */
19501 break;
19503 parms = TREE_CHAIN (parms);
19505 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19506 /* For a function parameter pack that does not occur at the
19507 end of the parameter-declaration-list, the type of the
19508 parameter pack is a non-deduced context. */
19509 continue;
19511 arg = args[ia];
19512 ++ia;
19514 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19515 explain_p))
19516 return 1;
19519 if (parms
19520 && parms != void_list_node
19521 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19523 /* Unify the remaining arguments with the pack expansion type. */
19524 tree argvec;
19525 tree parmvec = make_tree_vec (1);
19527 /* Allocate a TREE_VEC and copy in all of the arguments */
19528 argvec = make_tree_vec (nargs - ia);
19529 for (i = 0; ia < nargs; ++ia, ++i)
19530 TREE_VEC_ELT (argvec, i) = args[ia];
19532 /* Copy the parameter into parmvec. */
19533 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19534 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19535 /*subr=*/subr, explain_p))
19536 return 1;
19538 /* Advance to the end of the list of parameters. */
19539 parms = TREE_CHAIN (parms);
19542 /* Fail if we've reached the end of the parm list, and more args
19543 are present, and the parm list isn't variadic. */
19544 if (ia < nargs && parms == void_list_node)
19545 return unify_too_many_arguments (explain_p, nargs, ia);
19546 /* Fail if parms are left and they don't have default values and
19547 they aren't all deduced as empty packs (c++/57397). This is
19548 consistent with sufficient_parms_p. */
19549 if (parms && parms != void_list_node
19550 && TREE_PURPOSE (parms) == NULL_TREE)
19552 unsigned int count = nargs;
19553 tree p = parms;
19554 bool type_pack_p;
19557 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19558 if (!type_pack_p)
19559 count++;
19560 p = TREE_CHAIN (p);
19562 while (p && p != void_list_node);
19563 if (count != nargs)
19564 return unify_too_few_arguments (explain_p, ia, count,
19565 type_pack_p);
19568 if (!subr)
19570 tsubst_flags_t complain = (explain_p
19571 ? tf_warning_or_error
19572 : tf_none);
19573 bool tried_array_deduction = (cxx_dialect < cxx17);
19575 for (i = 0; i < ntparms; i++)
19577 tree targ = TREE_VEC_ELT (targs, i);
19578 tree tparm = TREE_VEC_ELT (tparms, i);
19580 /* Clear the "incomplete" flags on all argument packs now so that
19581 substituting them into later default arguments works. */
19582 if (targ && ARGUMENT_PACK_P (targ))
19584 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19585 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19588 if (targ || tparm == error_mark_node)
19589 continue;
19590 tparm = TREE_VALUE (tparm);
19592 if (TREE_CODE (tparm) == TYPE_DECL
19593 && !tried_array_deduction)
19595 try_array_deduction (tparms, targs, xparms);
19596 tried_array_deduction = true;
19597 if (TREE_VEC_ELT (targs, i))
19598 continue;
19601 /* If this is an undeduced nontype parameter that depends on
19602 a type parameter, try another pass; its type may have been
19603 deduced from a later argument than the one from which
19604 this parameter can be deduced. */
19605 if (TREE_CODE (tparm) == PARM_DECL
19606 && uses_template_parms (TREE_TYPE (tparm))
19607 && saw_undeduced < 2)
19609 saw_undeduced = 1;
19610 continue;
19613 /* Core issue #226 (C++0x) [temp.deduct]:
19615 If a template argument has not been deduced, its
19616 default template argument, if any, is used.
19618 When we are in C++98 mode, TREE_PURPOSE will either
19619 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19620 to explicitly check cxx_dialect here. */
19621 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19622 /* OK, there is a default argument. Wait until after the
19623 conversion check to do substitution. */
19624 continue;
19626 /* If the type parameter is a parameter pack, then it will
19627 be deduced to an empty parameter pack. */
19628 if (template_parameter_pack_p (tparm))
19630 tree arg;
19632 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19634 arg = make_node (NONTYPE_ARGUMENT_PACK);
19635 TREE_CONSTANT (arg) = 1;
19637 else
19638 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19640 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19642 TREE_VEC_ELT (targs, i) = arg;
19643 continue;
19646 return unify_parameter_deduction_failure (explain_p, tparm);
19649 /* DR 1391: All parameters have args, now check non-dependent parms for
19650 convertibility. */
19651 if (saw_undeduced < 2)
19652 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19653 parms && parms != void_list_node && ia < nargs; )
19655 parm = TREE_VALUE (parms);
19657 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19658 && (!TREE_CHAIN (parms)
19659 || TREE_CHAIN (parms) == void_list_node))
19660 /* For a function parameter pack that occurs at the end of the
19661 parameter-declaration-list, the type A of each remaining
19662 argument of the call is compared with the type P of the
19663 declarator-id of the function parameter pack. */
19664 break;
19666 parms = TREE_CHAIN (parms);
19668 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19669 /* For a function parameter pack that does not occur at the
19670 end of the parameter-declaration-list, the type of the
19671 parameter pack is a non-deduced context. */
19672 continue;
19674 arg = args[ia];
19675 ++ia;
19677 if (uses_template_parms (parm))
19678 continue;
19679 if (check_non_deducible_conversion (parm, arg, strict, flags,
19680 explain_p))
19681 return 1;
19684 /* Now substitute into the default template arguments. */
19685 for (i = 0; i < ntparms; i++)
19687 tree targ = TREE_VEC_ELT (targs, i);
19688 tree tparm = TREE_VEC_ELT (tparms, i);
19690 if (targ || tparm == error_mark_node)
19691 continue;
19692 tree parm = TREE_VALUE (tparm);
19694 if (TREE_CODE (parm) == PARM_DECL
19695 && uses_template_parms (TREE_TYPE (parm))
19696 && saw_undeduced < 2)
19697 continue;
19699 tree arg = TREE_PURPOSE (tparm);
19700 reopen_deferring_access_checks (*checks);
19701 location_t save_loc = input_location;
19702 if (DECL_P (parm))
19703 input_location = DECL_SOURCE_LOCATION (parm);
19704 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19705 if (!uses_template_parms (arg))
19706 arg = convert_template_argument (parm, arg, full_targs, complain,
19707 i, NULL_TREE);
19708 else if (saw_undeduced < 2)
19709 arg = NULL_TREE;
19710 else
19711 arg = error_mark_node;
19712 input_location = save_loc;
19713 *checks = get_deferred_access_checks ();
19714 pop_deferring_access_checks ();
19715 if (arg == error_mark_node)
19716 return 1;
19717 else if (arg)
19719 TREE_VEC_ELT (targs, i) = arg;
19720 /* The position of the first default template argument,
19721 is also the number of non-defaulted arguments in TARGS.
19722 Record that. */
19723 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19724 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19728 if (saw_undeduced++ == 1)
19729 goto again;
19732 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19733 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19735 return unify_success (explain_p);
19738 /* Subroutine of type_unification_real. Args are like the variables
19739 at the call site. ARG is an overloaded function (or template-id);
19740 we try deducing template args from each of the overloads, and if
19741 only one succeeds, we go with that. Modifies TARGS and returns
19742 true on success. */
19744 static bool
19745 resolve_overloaded_unification (tree tparms,
19746 tree targs,
19747 tree parm,
19748 tree arg,
19749 unification_kind_t strict,
19750 int sub_strict,
19751 bool explain_p)
19753 tree tempargs = copy_node (targs);
19754 int good = 0;
19755 tree goodfn = NULL_TREE;
19756 bool addr_p;
19758 if (TREE_CODE (arg) == ADDR_EXPR)
19760 arg = TREE_OPERAND (arg, 0);
19761 addr_p = true;
19763 else
19764 addr_p = false;
19766 if (TREE_CODE (arg) == COMPONENT_REF)
19767 /* Handle `&x' where `x' is some static or non-static member
19768 function name. */
19769 arg = TREE_OPERAND (arg, 1);
19771 if (TREE_CODE (arg) == OFFSET_REF)
19772 arg = TREE_OPERAND (arg, 1);
19774 /* Strip baselink information. */
19775 if (BASELINK_P (arg))
19776 arg = BASELINK_FUNCTIONS (arg);
19778 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19780 /* If we got some explicit template args, we need to plug them into
19781 the affected templates before we try to unify, in case the
19782 explicit args will completely resolve the templates in question. */
19784 int ok = 0;
19785 tree expl_subargs = TREE_OPERAND (arg, 1);
19786 arg = TREE_OPERAND (arg, 0);
19788 for (lkp_iterator iter (arg); iter; ++iter)
19790 tree fn = *iter;
19791 tree subargs, elem;
19793 if (TREE_CODE (fn) != TEMPLATE_DECL)
19794 continue;
19796 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19797 expl_subargs, NULL_TREE, tf_none,
19798 /*require_all_args=*/true,
19799 /*use_default_args=*/true);
19800 if (subargs != error_mark_node
19801 && !any_dependent_template_arguments_p (subargs))
19803 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19804 if (try_one_overload (tparms, targs, tempargs, parm,
19805 elem, strict, sub_strict, addr_p, explain_p)
19806 && (!goodfn || !same_type_p (goodfn, elem)))
19808 goodfn = elem;
19809 ++good;
19812 else if (subargs)
19813 ++ok;
19815 /* If no templates (or more than one) are fully resolved by the
19816 explicit arguments, this template-id is a non-deduced context; it
19817 could still be OK if we deduce all template arguments for the
19818 enclosing call through other arguments. */
19819 if (good != 1)
19820 good = ok;
19822 else if (TREE_CODE (arg) != OVERLOAD
19823 && TREE_CODE (arg) != FUNCTION_DECL)
19824 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19825 -- but the deduction does not succeed because the expression is
19826 not just the function on its own. */
19827 return false;
19828 else
19829 for (lkp_iterator iter (arg); iter; ++iter)
19831 tree fn = *iter;
19832 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19833 strict, sub_strict, addr_p, explain_p)
19834 && (!goodfn || !decls_match (goodfn, fn)))
19836 goodfn = fn;
19837 ++good;
19841 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19842 to function or pointer to member function argument if the set of
19843 overloaded functions does not contain function templates and at most
19844 one of a set of overloaded functions provides a unique match.
19846 So if we found multiple possibilities, we return success but don't
19847 deduce anything. */
19849 if (good == 1)
19851 int i = TREE_VEC_LENGTH (targs);
19852 for (; i--; )
19853 if (TREE_VEC_ELT (tempargs, i))
19855 tree old = TREE_VEC_ELT (targs, i);
19856 tree new_ = TREE_VEC_ELT (tempargs, i);
19857 if (new_ && old && ARGUMENT_PACK_P (old)
19858 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19859 /* Don't forget explicit template arguments in a pack. */
19860 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19861 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19862 TREE_VEC_ELT (targs, i) = new_;
19865 if (good)
19866 return true;
19868 return false;
19871 /* Core DR 115: In contexts where deduction is done and fails, or in
19872 contexts where deduction is not done, if a template argument list is
19873 specified and it, along with any default template arguments, identifies
19874 a single function template specialization, then the template-id is an
19875 lvalue for the function template specialization. */
19877 tree
19878 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19880 tree expr, offset, baselink;
19881 bool addr;
19883 if (!type_unknown_p (orig_expr))
19884 return orig_expr;
19886 expr = orig_expr;
19887 addr = false;
19888 offset = NULL_TREE;
19889 baselink = NULL_TREE;
19891 if (TREE_CODE (expr) == ADDR_EXPR)
19893 expr = TREE_OPERAND (expr, 0);
19894 addr = true;
19896 if (TREE_CODE (expr) == OFFSET_REF)
19898 offset = expr;
19899 expr = TREE_OPERAND (expr, 1);
19901 if (BASELINK_P (expr))
19903 baselink = expr;
19904 expr = BASELINK_FUNCTIONS (expr);
19907 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19909 int good = 0;
19910 tree goodfn = NULL_TREE;
19912 /* If we got some explicit template args, we need to plug them into
19913 the affected templates before we try to unify, in case the
19914 explicit args will completely resolve the templates in question. */
19916 tree expl_subargs = TREE_OPERAND (expr, 1);
19917 tree arg = TREE_OPERAND (expr, 0);
19918 tree badfn = NULL_TREE;
19919 tree badargs = NULL_TREE;
19921 for (lkp_iterator iter (arg); iter; ++iter)
19923 tree fn = *iter;
19924 tree subargs, elem;
19926 if (TREE_CODE (fn) != TEMPLATE_DECL)
19927 continue;
19929 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19930 expl_subargs, NULL_TREE, tf_none,
19931 /*require_all_args=*/true,
19932 /*use_default_args=*/true);
19933 if (subargs != error_mark_node
19934 && !any_dependent_template_arguments_p (subargs))
19936 elem = instantiate_template (fn, subargs, tf_none);
19937 if (elem == error_mark_node)
19939 badfn = fn;
19940 badargs = subargs;
19942 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19944 goodfn = elem;
19945 ++good;
19949 if (good == 1)
19951 mark_used (goodfn);
19952 expr = goodfn;
19953 if (baselink)
19954 expr = build_baselink (BASELINK_BINFO (baselink),
19955 BASELINK_ACCESS_BINFO (baselink),
19956 expr, BASELINK_OPTYPE (baselink));
19957 if (offset)
19959 tree base
19960 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19961 expr = build_offset_ref (base, expr, addr, complain);
19963 if (addr)
19964 expr = cp_build_addr_expr (expr, complain);
19965 return expr;
19967 else if (good == 0 && badargs && (complain & tf_error))
19968 /* There were no good options and at least one bad one, so let the
19969 user know what the problem is. */
19970 instantiate_template (badfn, badargs, complain);
19972 return orig_expr;
19975 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19976 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19977 different overloads deduce different arguments for a given parm.
19978 ADDR_P is true if the expression for which deduction is being
19979 performed was of the form "& fn" rather than simply "fn".
19981 Returns 1 on success. */
19983 static int
19984 try_one_overload (tree tparms,
19985 tree orig_targs,
19986 tree targs,
19987 tree parm,
19988 tree arg,
19989 unification_kind_t strict,
19990 int sub_strict,
19991 bool addr_p,
19992 bool explain_p)
19994 int nargs;
19995 tree tempargs;
19996 int i;
19998 if (arg == error_mark_node)
19999 return 0;
20001 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20002 to function or pointer to member function argument if the set of
20003 overloaded functions does not contain function templates and at most
20004 one of a set of overloaded functions provides a unique match.
20006 So if this is a template, just return success. */
20008 if (uses_template_parms (arg))
20009 return 1;
20011 if (TREE_CODE (arg) == METHOD_TYPE)
20012 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20013 else if (addr_p)
20014 arg = build_pointer_type (arg);
20016 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20018 /* We don't copy orig_targs for this because if we have already deduced
20019 some template args from previous args, unify would complain when we
20020 try to deduce a template parameter for the same argument, even though
20021 there isn't really a conflict. */
20022 nargs = TREE_VEC_LENGTH (targs);
20023 tempargs = make_tree_vec (nargs);
20025 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20026 return 0;
20028 /* First make sure we didn't deduce anything that conflicts with
20029 explicitly specified args. */
20030 for (i = nargs; i--; )
20032 tree elt = TREE_VEC_ELT (tempargs, i);
20033 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20035 if (!elt)
20036 /*NOP*/;
20037 else if (uses_template_parms (elt))
20038 /* Since we're unifying against ourselves, we will fill in
20039 template args used in the function parm list with our own
20040 template parms. Discard them. */
20041 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20042 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20044 /* Check that the argument at each index of the deduced argument pack
20045 is equivalent to the corresponding explicitly specified argument.
20046 We may have deduced more arguments than were explicitly specified,
20047 and that's OK. */
20049 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20050 that's wrong if we deduce the same argument pack from multiple
20051 function arguments: it's only incomplete the first time. */
20053 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20054 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20056 if (TREE_VEC_LENGTH (deduced_pack)
20057 < TREE_VEC_LENGTH (explicit_pack))
20058 return 0;
20060 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20061 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20062 TREE_VEC_ELT (deduced_pack, j)))
20063 return 0;
20065 else if (oldelt && !template_args_equal (oldelt, elt))
20066 return 0;
20069 for (i = nargs; i--; )
20071 tree elt = TREE_VEC_ELT (tempargs, i);
20073 if (elt)
20074 TREE_VEC_ELT (targs, i) = elt;
20077 return 1;
20080 /* PARM is a template class (perhaps with unbound template
20081 parameters). ARG is a fully instantiated type. If ARG can be
20082 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20083 TARGS are as for unify. */
20085 static tree
20086 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20087 bool explain_p)
20089 tree copy_of_targs;
20091 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20092 return NULL_TREE;
20093 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20094 /* Matches anything. */;
20095 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20096 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20097 return NULL_TREE;
20099 /* We need to make a new template argument vector for the call to
20100 unify. If we used TARGS, we'd clutter it up with the result of
20101 the attempted unification, even if this class didn't work out.
20102 We also don't want to commit ourselves to all the unifications
20103 we've already done, since unification is supposed to be done on
20104 an argument-by-argument basis. In other words, consider the
20105 following pathological case:
20107 template <int I, int J, int K>
20108 struct S {};
20110 template <int I, int J>
20111 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20113 template <int I, int J, int K>
20114 void f(S<I, J, K>, S<I, I, I>);
20116 void g() {
20117 S<0, 0, 0> s0;
20118 S<0, 1, 2> s2;
20120 f(s0, s2);
20123 Now, by the time we consider the unification involving `s2', we
20124 already know that we must have `f<0, 0, 0>'. But, even though
20125 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20126 because there are two ways to unify base classes of S<0, 1, 2>
20127 with S<I, I, I>. If we kept the already deduced knowledge, we
20128 would reject the possibility I=1. */
20129 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20131 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20133 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20134 return NULL_TREE;
20135 return arg;
20138 /* If unification failed, we're done. */
20139 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20140 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20141 return NULL_TREE;
20143 return arg;
20146 /* Given a template type PARM and a class type ARG, find the unique
20147 base type in ARG that is an instance of PARM. We do not examine
20148 ARG itself; only its base-classes. If there is not exactly one
20149 appropriate base class, return NULL_TREE. PARM may be the type of
20150 a partial specialization, as well as a plain template type. Used
20151 by unify. */
20153 static enum template_base_result
20154 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20155 bool explain_p, tree *result)
20157 tree rval = NULL_TREE;
20158 tree binfo;
20160 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20162 binfo = TYPE_BINFO (complete_type (arg));
20163 if (!binfo)
20165 /* The type could not be completed. */
20166 *result = NULL_TREE;
20167 return tbr_incomplete_type;
20170 /* Walk in inheritance graph order. The search order is not
20171 important, and this avoids multiple walks of virtual bases. */
20172 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20174 tree r = try_class_unification (tparms, targs, parm,
20175 BINFO_TYPE (binfo), explain_p);
20177 if (r)
20179 /* If there is more than one satisfactory baseclass, then:
20181 [temp.deduct.call]
20183 If they yield more than one possible deduced A, the type
20184 deduction fails.
20186 applies. */
20187 if (rval && !same_type_p (r, rval))
20189 *result = NULL_TREE;
20190 return tbr_ambiguous_baseclass;
20193 rval = r;
20197 *result = rval;
20198 return tbr_success;
20201 /* Returns the level of DECL, which declares a template parameter. */
20203 static int
20204 template_decl_level (tree decl)
20206 switch (TREE_CODE (decl))
20208 case TYPE_DECL:
20209 case TEMPLATE_DECL:
20210 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20212 case PARM_DECL:
20213 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20215 default:
20216 gcc_unreachable ();
20218 return 0;
20221 /* Decide whether ARG can be unified with PARM, considering only the
20222 cv-qualifiers of each type, given STRICT as documented for unify.
20223 Returns nonzero iff the unification is OK on that basis. */
20225 static int
20226 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20228 int arg_quals = cp_type_quals (arg);
20229 int parm_quals = cp_type_quals (parm);
20231 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20232 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20234 /* Although a CVR qualifier is ignored when being applied to a
20235 substituted template parameter ([8.3.2]/1 for example), that
20236 does not allow us to unify "const T" with "int&" because both
20237 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20238 It is ok when we're allowing additional CV qualifiers
20239 at the outer level [14.8.2.1]/3,1st bullet. */
20240 if ((TREE_CODE (arg) == REFERENCE_TYPE
20241 || TREE_CODE (arg) == FUNCTION_TYPE
20242 || TREE_CODE (arg) == METHOD_TYPE)
20243 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20244 return 0;
20246 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20247 && (parm_quals & TYPE_QUAL_RESTRICT))
20248 return 0;
20251 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20252 && (arg_quals & parm_quals) != parm_quals)
20253 return 0;
20255 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20256 && (parm_quals & arg_quals) != arg_quals)
20257 return 0;
20259 return 1;
20262 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20263 void
20264 template_parm_level_and_index (tree parm, int* level, int* index)
20266 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20267 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20268 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20270 *index = TEMPLATE_TYPE_IDX (parm);
20271 *level = TEMPLATE_TYPE_LEVEL (parm);
20273 else
20275 *index = TEMPLATE_PARM_IDX (parm);
20276 *level = TEMPLATE_PARM_LEVEL (parm);
20280 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20281 do { \
20282 if (unify (TP, TA, P, A, S, EP)) \
20283 return 1; \
20284 } while (0)
20286 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20287 expansion at the end of PACKED_PARMS. Returns 0 if the type
20288 deduction succeeds, 1 otherwise. STRICT is the same as in
20289 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20290 function call argument list. We'll need to adjust the arguments to make them
20291 types. SUBR tells us if this is from a recursive call to
20292 type_unification_real, or for comparing two template argument
20293 lists. */
20295 static int
20296 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20297 tree packed_args, unification_kind_t strict,
20298 bool subr, bool explain_p)
20300 tree parm
20301 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20302 tree pattern = PACK_EXPANSION_PATTERN (parm);
20303 tree pack, packs = NULL_TREE;
20304 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20306 /* Add in any args remembered from an earlier partial instantiation. */
20307 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20309 packed_args = expand_template_argument_pack (packed_args);
20311 int len = TREE_VEC_LENGTH (packed_args);
20313 /* Determine the parameter packs we will be deducing from the
20314 pattern, and record their current deductions. */
20315 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20316 pack; pack = TREE_CHAIN (pack))
20318 tree parm_pack = TREE_VALUE (pack);
20319 int idx, level;
20321 /* Determine the index and level of this parameter pack. */
20322 template_parm_level_and_index (parm_pack, &level, &idx);
20324 /* Keep track of the parameter packs and their corresponding
20325 argument packs. */
20326 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20327 TREE_TYPE (packs) = make_tree_vec (len - start);
20330 /* Loop through all of the arguments that have not yet been
20331 unified and unify each with the pattern. */
20332 for (i = start; i < len; i++)
20334 tree parm;
20335 bool any_explicit = false;
20336 tree arg = TREE_VEC_ELT (packed_args, i);
20338 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20339 or the element of its argument pack at the current index if
20340 this argument was explicitly specified. */
20341 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20343 int idx, level;
20344 tree arg, pargs;
20345 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20347 arg = NULL_TREE;
20348 if (TREE_VALUE (pack)
20349 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20350 && (i - start < TREE_VEC_LENGTH (pargs)))
20352 any_explicit = true;
20353 arg = TREE_VEC_ELT (pargs, i - start);
20355 TMPL_ARG (targs, level, idx) = arg;
20358 /* If we had explicit template arguments, substitute them into the
20359 pattern before deduction. */
20360 if (any_explicit)
20362 /* Some arguments might still be unspecified or dependent. */
20363 bool dependent;
20364 ++processing_template_decl;
20365 dependent = any_dependent_template_arguments_p (targs);
20366 if (!dependent)
20367 --processing_template_decl;
20368 parm = tsubst (pattern, targs,
20369 explain_p ? tf_warning_or_error : tf_none,
20370 NULL_TREE);
20371 if (dependent)
20372 --processing_template_decl;
20373 if (parm == error_mark_node)
20374 return 1;
20376 else
20377 parm = pattern;
20379 /* Unify the pattern with the current argument. */
20380 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20381 explain_p))
20382 return 1;
20384 /* For each parameter pack, collect the deduced value. */
20385 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20387 int idx, level;
20388 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20390 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20391 TMPL_ARG (targs, level, idx);
20395 /* Verify that the results of unification with the parameter packs
20396 produce results consistent with what we've seen before, and make
20397 the deduced argument packs available. */
20398 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20400 tree old_pack = TREE_VALUE (pack);
20401 tree new_args = TREE_TYPE (pack);
20402 int i, len = TREE_VEC_LENGTH (new_args);
20403 int idx, level;
20404 bool nondeduced_p = false;
20406 /* By default keep the original deduced argument pack.
20407 If necessary, more specific code is going to update the
20408 resulting deduced argument later down in this function. */
20409 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20410 TMPL_ARG (targs, level, idx) = old_pack;
20412 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20413 actually deduce anything. */
20414 for (i = 0; i < len && !nondeduced_p; ++i)
20415 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20416 nondeduced_p = true;
20417 if (nondeduced_p)
20418 continue;
20420 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20422 /* If we had fewer function args than explicit template args,
20423 just use the explicits. */
20424 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20425 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20426 if (len < explicit_len)
20427 new_args = explicit_args;
20430 if (!old_pack)
20432 tree result;
20433 /* Build the deduced *_ARGUMENT_PACK. */
20434 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20436 result = make_node (NONTYPE_ARGUMENT_PACK);
20437 TREE_CONSTANT (result) = 1;
20439 else
20440 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20442 SET_ARGUMENT_PACK_ARGS (result, new_args);
20444 /* Note the deduced argument packs for this parameter
20445 pack. */
20446 TMPL_ARG (targs, level, idx) = result;
20448 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20449 && (ARGUMENT_PACK_ARGS (old_pack)
20450 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20452 /* We only had the explicitly-provided arguments before, but
20453 now we have a complete set of arguments. */
20454 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20456 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20457 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20458 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20460 else
20462 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20463 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20465 if (!comp_template_args (old_args, new_args,
20466 &bad_old_arg, &bad_new_arg))
20467 /* Inconsistent unification of this parameter pack. */
20468 return unify_parameter_pack_inconsistent (explain_p,
20469 bad_old_arg,
20470 bad_new_arg);
20474 return unify_success (explain_p);
20477 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20478 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20479 parameters and return value are as for unify. */
20481 static int
20482 unify_array_domain (tree tparms, tree targs,
20483 tree parm_dom, tree arg_dom,
20484 bool explain_p)
20486 tree parm_max;
20487 tree arg_max;
20488 bool parm_cst;
20489 bool arg_cst;
20491 /* Our representation of array types uses "N - 1" as the
20492 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20493 not an integer constant. We cannot unify arbitrarily
20494 complex expressions, so we eliminate the MINUS_EXPRs
20495 here. */
20496 parm_max = TYPE_MAX_VALUE (parm_dom);
20497 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20498 if (!parm_cst)
20500 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20501 parm_max = TREE_OPERAND (parm_max, 0);
20503 arg_max = TYPE_MAX_VALUE (arg_dom);
20504 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20505 if (!arg_cst)
20507 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20508 trying to unify the type of a variable with the type
20509 of a template parameter. For example:
20511 template <unsigned int N>
20512 void f (char (&) [N]);
20513 int g();
20514 void h(int i) {
20515 char a[g(i)];
20516 f(a);
20519 Here, the type of the ARG will be "int [g(i)]", and
20520 may be a SAVE_EXPR, etc. */
20521 if (TREE_CODE (arg_max) != MINUS_EXPR)
20522 return unify_vla_arg (explain_p, arg_dom);
20523 arg_max = TREE_OPERAND (arg_max, 0);
20526 /* If only one of the bounds used a MINUS_EXPR, compensate
20527 by adding one to the other bound. */
20528 if (parm_cst && !arg_cst)
20529 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20530 integer_type_node,
20531 parm_max,
20532 integer_one_node);
20533 else if (arg_cst && !parm_cst)
20534 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20535 integer_type_node,
20536 arg_max,
20537 integer_one_node);
20539 return unify (tparms, targs, parm_max, arg_max,
20540 UNIFY_ALLOW_INTEGER, explain_p);
20543 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20545 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20547 static pa_kind_t
20548 pa_kind (tree t)
20550 if (PACK_EXPANSION_P (t))
20551 t = PACK_EXPANSION_PATTERN (t);
20552 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20553 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20554 || DECL_TYPE_TEMPLATE_P (t))
20555 return pa_tmpl;
20556 else if (TYPE_P (t))
20557 return pa_type;
20558 else
20559 return pa_expr;
20562 /* Deduce the value of template parameters. TPARMS is the (innermost)
20563 set of template parameters to a template. TARGS is the bindings
20564 for those template parameters, as determined thus far; TARGS may
20565 include template arguments for outer levels of template parameters
20566 as well. PARM is a parameter to a template function, or a
20567 subcomponent of that parameter; ARG is the corresponding argument.
20568 This function attempts to match PARM with ARG in a manner
20569 consistent with the existing assignments in TARGS. If more values
20570 are deduced, then TARGS is updated.
20572 Returns 0 if the type deduction succeeds, 1 otherwise. The
20573 parameter STRICT is a bitwise or of the following flags:
20575 UNIFY_ALLOW_NONE:
20576 Require an exact match between PARM and ARG.
20577 UNIFY_ALLOW_MORE_CV_QUAL:
20578 Allow the deduced ARG to be more cv-qualified (by qualification
20579 conversion) than ARG.
20580 UNIFY_ALLOW_LESS_CV_QUAL:
20581 Allow the deduced ARG to be less cv-qualified than ARG.
20582 UNIFY_ALLOW_DERIVED:
20583 Allow the deduced ARG to be a template base class of ARG,
20584 or a pointer to a template base class of the type pointed to by
20585 ARG.
20586 UNIFY_ALLOW_INTEGER:
20587 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20588 case for more information.
20589 UNIFY_ALLOW_OUTER_LEVEL:
20590 This is the outermost level of a deduction. Used to determine validity
20591 of qualification conversions. A valid qualification conversion must
20592 have const qualified pointers leading up to the inner type which
20593 requires additional CV quals, except at the outer level, where const
20594 is not required [conv.qual]. It would be normal to set this flag in
20595 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20596 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20597 This is the outermost level of a deduction, and PARM can be more CV
20598 qualified at this point.
20599 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20600 This is the outermost level of a deduction, and PARM can be less CV
20601 qualified at this point. */
20603 static int
20604 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20605 bool explain_p)
20607 int idx;
20608 tree targ;
20609 tree tparm;
20610 int strict_in = strict;
20611 tsubst_flags_t complain = (explain_p
20612 ? tf_warning_or_error
20613 : tf_none);
20615 /* I don't think this will do the right thing with respect to types.
20616 But the only case I've seen it in so far has been array bounds, where
20617 signedness is the only information lost, and I think that will be
20618 okay. */
20619 while (CONVERT_EXPR_P (parm))
20620 parm = TREE_OPERAND (parm, 0);
20622 if (arg == error_mark_node)
20623 return unify_invalid (explain_p);
20624 if (arg == unknown_type_node
20625 || arg == init_list_type_node)
20626 /* We can't deduce anything from this, but we might get all the
20627 template args from other function args. */
20628 return unify_success (explain_p);
20630 if (parm == any_targ_node || arg == any_targ_node)
20631 return unify_success (explain_p);
20633 /* If PARM uses template parameters, then we can't bail out here,
20634 even if ARG == PARM, since we won't record unifications for the
20635 template parameters. We might need them if we're trying to
20636 figure out which of two things is more specialized. */
20637 if (arg == parm && !uses_template_parms (parm))
20638 return unify_success (explain_p);
20640 /* Handle init lists early, so the rest of the function can assume
20641 we're dealing with a type. */
20642 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20644 tree elt, elttype;
20645 unsigned i;
20646 tree orig_parm = parm;
20648 /* Replace T with std::initializer_list<T> for deduction. */
20649 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20650 && flag_deduce_init_list)
20651 parm = listify (parm);
20653 if (!is_std_init_list (parm)
20654 && TREE_CODE (parm) != ARRAY_TYPE)
20655 /* We can only deduce from an initializer list argument if the
20656 parameter is std::initializer_list or an array; otherwise this
20657 is a non-deduced context. */
20658 return unify_success (explain_p);
20660 if (TREE_CODE (parm) == ARRAY_TYPE)
20661 elttype = TREE_TYPE (parm);
20662 else
20664 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20665 /* Deduction is defined in terms of a single type, so just punt
20666 on the (bizarre) std::initializer_list<T...>. */
20667 if (PACK_EXPANSION_P (elttype))
20668 return unify_success (explain_p);
20671 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20673 int elt_strict = strict;
20675 if (elt == error_mark_node)
20676 return unify_invalid (explain_p);
20678 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20680 tree type = TREE_TYPE (elt);
20681 if (type == error_mark_node)
20682 return unify_invalid (explain_p);
20683 /* It should only be possible to get here for a call. */
20684 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20685 elt_strict |= maybe_adjust_types_for_deduction
20686 (DEDUCE_CALL, &elttype, &type, elt);
20687 elt = type;
20690 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20691 explain_p);
20694 if (TREE_CODE (parm) == ARRAY_TYPE
20695 && deducible_array_bound (TYPE_DOMAIN (parm)))
20697 /* Also deduce from the length of the initializer list. */
20698 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20699 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20700 if (idx == error_mark_node)
20701 return unify_invalid (explain_p);
20702 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20703 idx, explain_p);
20706 /* If the std::initializer_list<T> deduction worked, replace the
20707 deduced A with std::initializer_list<A>. */
20708 if (orig_parm != parm)
20710 idx = TEMPLATE_TYPE_IDX (orig_parm);
20711 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20712 targ = listify (targ);
20713 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20715 return unify_success (explain_p);
20718 /* If parm and arg aren't the same kind of thing (template, type, or
20719 expression), fail early. */
20720 if (pa_kind (parm) != pa_kind (arg))
20721 return unify_invalid (explain_p);
20723 /* Immediately reject some pairs that won't unify because of
20724 cv-qualification mismatches. */
20725 if (TREE_CODE (arg) == TREE_CODE (parm)
20726 && TYPE_P (arg)
20727 /* It is the elements of the array which hold the cv quals of an array
20728 type, and the elements might be template type parms. We'll check
20729 when we recurse. */
20730 && TREE_CODE (arg) != ARRAY_TYPE
20731 /* We check the cv-qualifiers when unifying with template type
20732 parameters below. We want to allow ARG `const T' to unify with
20733 PARM `T' for example, when computing which of two templates
20734 is more specialized, for example. */
20735 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20736 && !check_cv_quals_for_unify (strict_in, arg, parm))
20737 return unify_cv_qual_mismatch (explain_p, parm, arg);
20739 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20740 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20741 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20742 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20743 strict &= ~UNIFY_ALLOW_DERIVED;
20744 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20745 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20747 switch (TREE_CODE (parm))
20749 case TYPENAME_TYPE:
20750 case SCOPE_REF:
20751 case UNBOUND_CLASS_TEMPLATE:
20752 /* In a type which contains a nested-name-specifier, template
20753 argument values cannot be deduced for template parameters used
20754 within the nested-name-specifier. */
20755 return unify_success (explain_p);
20757 case TEMPLATE_TYPE_PARM:
20758 case TEMPLATE_TEMPLATE_PARM:
20759 case BOUND_TEMPLATE_TEMPLATE_PARM:
20760 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20761 if (error_operand_p (tparm))
20762 return unify_invalid (explain_p);
20764 if (TEMPLATE_TYPE_LEVEL (parm)
20765 != template_decl_level (tparm))
20766 /* The PARM is not one we're trying to unify. Just check
20767 to see if it matches ARG. */
20769 if (TREE_CODE (arg) == TREE_CODE (parm)
20770 && (is_auto (parm) ? is_auto (arg)
20771 : same_type_p (parm, arg)))
20772 return unify_success (explain_p);
20773 else
20774 return unify_type_mismatch (explain_p, parm, arg);
20776 idx = TEMPLATE_TYPE_IDX (parm);
20777 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20778 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20779 if (error_operand_p (tparm))
20780 return unify_invalid (explain_p);
20782 /* Check for mixed types and values. */
20783 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20784 && TREE_CODE (tparm) != TYPE_DECL)
20785 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20786 && TREE_CODE (tparm) != TEMPLATE_DECL))
20787 gcc_unreachable ();
20789 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20791 if ((strict_in & UNIFY_ALLOW_DERIVED)
20792 && CLASS_TYPE_P (arg))
20794 /* First try to match ARG directly. */
20795 tree t = try_class_unification (tparms, targs, parm, arg,
20796 explain_p);
20797 if (!t)
20799 /* Otherwise, look for a suitable base of ARG, as below. */
20800 enum template_base_result r;
20801 r = get_template_base (tparms, targs, parm, arg,
20802 explain_p, &t);
20803 if (!t)
20804 return unify_no_common_base (explain_p, r, parm, arg);
20805 arg = t;
20808 /* ARG must be constructed from a template class or a template
20809 template parameter. */
20810 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20811 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20812 return unify_template_deduction_failure (explain_p, parm, arg);
20814 /* Deduce arguments T, i from TT<T> or TT<i>. */
20815 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20816 return 1;
20818 arg = TYPE_TI_TEMPLATE (arg);
20820 /* Fall through to deduce template name. */
20823 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20824 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20826 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20828 /* Simple cases: Value already set, does match or doesn't. */
20829 if (targ != NULL_TREE && template_args_equal (targ, arg))
20830 return unify_success (explain_p);
20831 else if (targ)
20832 return unify_inconsistency (explain_p, parm, targ, arg);
20834 else
20836 /* If PARM is `const T' and ARG is only `int', we don't have
20837 a match unless we are allowing additional qualification.
20838 If ARG is `const int' and PARM is just `T' that's OK;
20839 that binds `const int' to `T'. */
20840 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20841 arg, parm))
20842 return unify_cv_qual_mismatch (explain_p, parm, arg);
20844 /* Consider the case where ARG is `const volatile int' and
20845 PARM is `const T'. Then, T should be `volatile int'. */
20846 arg = cp_build_qualified_type_real
20847 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20848 if (arg == error_mark_node)
20849 return unify_invalid (explain_p);
20851 /* Simple cases: Value already set, does match or doesn't. */
20852 if (targ != NULL_TREE && same_type_p (targ, arg))
20853 return unify_success (explain_p);
20854 else if (targ)
20855 return unify_inconsistency (explain_p, parm, targ, arg);
20857 /* Make sure that ARG is not a variable-sized array. (Note
20858 that were talking about variable-sized arrays (like
20859 `int[n]'), rather than arrays of unknown size (like
20860 `int[]').) We'll get very confused by such a type since
20861 the bound of the array is not constant, and therefore
20862 not mangleable. Besides, such types are not allowed in
20863 ISO C++, so we can do as we please here. We do allow
20864 them for 'auto' deduction, since that isn't ABI-exposed. */
20865 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20866 return unify_vla_arg (explain_p, arg);
20868 /* Strip typedefs as in convert_template_argument. */
20869 arg = canonicalize_type_argument (arg, tf_none);
20872 /* If ARG is a parameter pack or an expansion, we cannot unify
20873 against it unless PARM is also a parameter pack. */
20874 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20875 && !template_parameter_pack_p (parm))
20876 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20878 /* If the argument deduction results is a METHOD_TYPE,
20879 then there is a problem.
20880 METHOD_TYPE doesn't map to any real C++ type the result of
20881 the deduction can not be of that type. */
20882 if (TREE_CODE (arg) == METHOD_TYPE)
20883 return unify_method_type_error (explain_p, arg);
20885 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20886 return unify_success (explain_p);
20888 case TEMPLATE_PARM_INDEX:
20889 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20890 if (error_operand_p (tparm))
20891 return unify_invalid (explain_p);
20893 if (TEMPLATE_PARM_LEVEL (parm)
20894 != template_decl_level (tparm))
20896 /* The PARM is not one we're trying to unify. Just check
20897 to see if it matches ARG. */
20898 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20899 && cp_tree_equal (parm, arg));
20900 if (result)
20901 unify_expression_unequal (explain_p, parm, arg);
20902 return result;
20905 idx = TEMPLATE_PARM_IDX (parm);
20906 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20908 if (targ)
20910 int x = !cp_tree_equal (targ, arg);
20911 if (x)
20912 unify_inconsistency (explain_p, parm, targ, arg);
20913 return x;
20916 /* [temp.deduct.type] If, in the declaration of a function template
20917 with a non-type template-parameter, the non-type
20918 template-parameter is used in an expression in the function
20919 parameter-list and, if the corresponding template-argument is
20920 deduced, the template-argument type shall match the type of the
20921 template-parameter exactly, except that a template-argument
20922 deduced from an array bound may be of any integral type.
20923 The non-type parameter might use already deduced type parameters. */
20924 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20925 if (tree a = type_uses_auto (tparm))
20927 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20928 if (tparm == error_mark_node)
20929 return 1;
20932 if (!TREE_TYPE (arg))
20933 /* Template-parameter dependent expression. Just accept it for now.
20934 It will later be processed in convert_template_argument. */
20936 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20937 non_reference (tparm)))
20938 /* OK */;
20939 else if ((strict & UNIFY_ALLOW_INTEGER)
20940 && CP_INTEGRAL_TYPE_P (tparm))
20941 /* Convert the ARG to the type of PARM; the deduced non-type
20942 template argument must exactly match the types of the
20943 corresponding parameter. */
20944 arg = fold (build_nop (tparm, arg));
20945 else if (uses_template_parms (tparm))
20947 /* We haven't deduced the type of this parameter yet. */
20948 if (cxx_dialect >= cxx17
20949 /* We deduce from array bounds in try_array_deduction. */
20950 && !(strict & UNIFY_ALLOW_INTEGER))
20952 /* Deduce it from the non-type argument. */
20953 tree atype = TREE_TYPE (arg);
20954 RECUR_AND_CHECK_FAILURE (tparms, targs,
20955 tparm, atype,
20956 UNIFY_ALLOW_NONE, explain_p);
20958 else
20959 /* Try again later. */
20960 return unify_success (explain_p);
20962 else
20963 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20965 /* If ARG is a parameter pack or an expansion, we cannot unify
20966 against it unless PARM is also a parameter pack. */
20967 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20968 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20969 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20972 bool removed_attr = false;
20973 arg = strip_typedefs_expr (arg, &removed_attr);
20975 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20976 return unify_success (explain_p);
20978 case PTRMEM_CST:
20980 /* A pointer-to-member constant can be unified only with
20981 another constant. */
20982 if (TREE_CODE (arg) != PTRMEM_CST)
20983 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20985 /* Just unify the class member. It would be useless (and possibly
20986 wrong, depending on the strict flags) to unify also
20987 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20988 arg refer to the same variable, even if through different
20989 classes. For instance:
20991 struct A { int x; };
20992 struct B : A { };
20994 Unification of &A::x and &B::x must succeed. */
20995 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20996 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20999 case POINTER_TYPE:
21001 if (!TYPE_PTR_P (arg))
21002 return unify_type_mismatch (explain_p, parm, arg);
21004 /* [temp.deduct.call]
21006 A can be another pointer or pointer to member type that can
21007 be converted to the deduced A via a qualification
21008 conversion (_conv.qual_).
21010 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21011 This will allow for additional cv-qualification of the
21012 pointed-to types if appropriate. */
21014 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21015 /* The derived-to-base conversion only persists through one
21016 level of pointers. */
21017 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21019 return unify (tparms, targs, TREE_TYPE (parm),
21020 TREE_TYPE (arg), strict, explain_p);
21023 case REFERENCE_TYPE:
21024 if (TREE_CODE (arg) != REFERENCE_TYPE)
21025 return unify_type_mismatch (explain_p, parm, arg);
21026 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21027 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21029 case ARRAY_TYPE:
21030 if (TREE_CODE (arg) != ARRAY_TYPE)
21031 return unify_type_mismatch (explain_p, parm, arg);
21032 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21033 != (TYPE_DOMAIN (arg) == NULL_TREE))
21034 return unify_type_mismatch (explain_p, parm, arg);
21035 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21036 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21037 if (TYPE_DOMAIN (parm) != NULL_TREE)
21038 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21039 TYPE_DOMAIN (arg), explain_p);
21040 return unify_success (explain_p);
21042 case REAL_TYPE:
21043 case COMPLEX_TYPE:
21044 case VECTOR_TYPE:
21045 case INTEGER_TYPE:
21046 case BOOLEAN_TYPE:
21047 case ENUMERAL_TYPE:
21048 case VOID_TYPE:
21049 case NULLPTR_TYPE:
21050 if (TREE_CODE (arg) != TREE_CODE (parm))
21051 return unify_type_mismatch (explain_p, parm, arg);
21053 /* We have already checked cv-qualification at the top of the
21054 function. */
21055 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21056 return unify_type_mismatch (explain_p, parm, arg);
21058 /* As far as unification is concerned, this wins. Later checks
21059 will invalidate it if necessary. */
21060 return unify_success (explain_p);
21062 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21063 /* Type INTEGER_CST can come from ordinary constant template args. */
21064 case INTEGER_CST:
21065 while (CONVERT_EXPR_P (arg))
21066 arg = TREE_OPERAND (arg, 0);
21068 if (TREE_CODE (arg) != INTEGER_CST)
21069 return unify_template_argument_mismatch (explain_p, parm, arg);
21070 return (tree_int_cst_equal (parm, arg)
21071 ? unify_success (explain_p)
21072 : unify_template_argument_mismatch (explain_p, parm, arg));
21074 case TREE_VEC:
21076 int i, len, argslen;
21077 int parm_variadic_p = 0;
21079 if (TREE_CODE (arg) != TREE_VEC)
21080 return unify_template_argument_mismatch (explain_p, parm, arg);
21082 len = TREE_VEC_LENGTH (parm);
21083 argslen = TREE_VEC_LENGTH (arg);
21085 /* Check for pack expansions in the parameters. */
21086 for (i = 0; i < len; ++i)
21088 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21090 if (i == len - 1)
21091 /* We can unify against something with a trailing
21092 parameter pack. */
21093 parm_variadic_p = 1;
21094 else
21095 /* [temp.deduct.type]/9: If the template argument list of
21096 P contains a pack expansion that is not the last
21097 template argument, the entire template argument list
21098 is a non-deduced context. */
21099 return unify_success (explain_p);
21103 /* If we don't have enough arguments to satisfy the parameters
21104 (not counting the pack expression at the end), or we have
21105 too many arguments for a parameter list that doesn't end in
21106 a pack expression, we can't unify. */
21107 if (parm_variadic_p
21108 ? argslen < len - parm_variadic_p
21109 : argslen != len)
21110 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21112 /* Unify all of the parameters that precede the (optional)
21113 pack expression. */
21114 for (i = 0; i < len - parm_variadic_p; ++i)
21116 RECUR_AND_CHECK_FAILURE (tparms, targs,
21117 TREE_VEC_ELT (parm, i),
21118 TREE_VEC_ELT (arg, i),
21119 UNIFY_ALLOW_NONE, explain_p);
21121 if (parm_variadic_p)
21122 return unify_pack_expansion (tparms, targs, parm, arg,
21123 DEDUCE_EXACT,
21124 /*subr=*/true, explain_p);
21125 return unify_success (explain_p);
21128 case RECORD_TYPE:
21129 case UNION_TYPE:
21130 if (TREE_CODE (arg) != TREE_CODE (parm))
21131 return unify_type_mismatch (explain_p, parm, arg);
21133 if (TYPE_PTRMEMFUNC_P (parm))
21135 if (!TYPE_PTRMEMFUNC_P (arg))
21136 return unify_type_mismatch (explain_p, parm, arg);
21138 return unify (tparms, targs,
21139 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21140 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21141 strict, explain_p);
21143 else if (TYPE_PTRMEMFUNC_P (arg))
21144 return unify_type_mismatch (explain_p, parm, arg);
21146 if (CLASSTYPE_TEMPLATE_INFO (parm))
21148 tree t = NULL_TREE;
21150 if (strict_in & UNIFY_ALLOW_DERIVED)
21152 /* First, we try to unify the PARM and ARG directly. */
21153 t = try_class_unification (tparms, targs,
21154 parm, arg, explain_p);
21156 if (!t)
21158 /* Fallback to the special case allowed in
21159 [temp.deduct.call]:
21161 If P is a class, and P has the form
21162 template-id, then A can be a derived class of
21163 the deduced A. Likewise, if P is a pointer to
21164 a class of the form template-id, A can be a
21165 pointer to a derived class pointed to by the
21166 deduced A. */
21167 enum template_base_result r;
21168 r = get_template_base (tparms, targs, parm, arg,
21169 explain_p, &t);
21171 if (!t)
21173 /* Don't give the derived diagnostic if we're
21174 already dealing with the same template. */
21175 bool same_template
21176 = (CLASSTYPE_TEMPLATE_INFO (arg)
21177 && (CLASSTYPE_TI_TEMPLATE (parm)
21178 == CLASSTYPE_TI_TEMPLATE (arg)));
21179 return unify_no_common_base (explain_p && !same_template,
21180 r, parm, arg);
21184 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21185 && (CLASSTYPE_TI_TEMPLATE (parm)
21186 == CLASSTYPE_TI_TEMPLATE (arg)))
21187 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21188 Then, we should unify `int' and `U'. */
21189 t = arg;
21190 else
21191 /* There's no chance of unification succeeding. */
21192 return unify_type_mismatch (explain_p, parm, arg);
21194 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21195 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21197 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21198 return unify_type_mismatch (explain_p, parm, arg);
21199 return unify_success (explain_p);
21201 case METHOD_TYPE:
21202 case FUNCTION_TYPE:
21204 unsigned int nargs;
21205 tree *args;
21206 tree a;
21207 unsigned int i;
21209 if (TREE_CODE (arg) != TREE_CODE (parm))
21210 return unify_type_mismatch (explain_p, parm, arg);
21212 /* CV qualifications for methods can never be deduced, they must
21213 match exactly. We need to check them explicitly here,
21214 because type_unification_real treats them as any other
21215 cv-qualified parameter. */
21216 if (TREE_CODE (parm) == METHOD_TYPE
21217 && (!check_cv_quals_for_unify
21218 (UNIFY_ALLOW_NONE,
21219 class_of_this_parm (arg),
21220 class_of_this_parm (parm))))
21221 return unify_cv_qual_mismatch (explain_p, parm, arg);
21222 if (TREE_CODE (arg) == FUNCTION_TYPE
21223 && type_memfn_quals (parm) != type_memfn_quals (arg))
21224 return unify_cv_qual_mismatch (explain_p, parm, arg);
21225 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21226 return unify_type_mismatch (explain_p, parm, arg);
21228 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21229 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21231 nargs = list_length (TYPE_ARG_TYPES (arg));
21232 args = XALLOCAVEC (tree, nargs);
21233 for (a = TYPE_ARG_TYPES (arg), i = 0;
21234 a != NULL_TREE && a != void_list_node;
21235 a = TREE_CHAIN (a), ++i)
21236 args[i] = TREE_VALUE (a);
21237 nargs = i;
21239 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21240 args, nargs, 1, DEDUCE_EXACT,
21241 LOOKUP_NORMAL, NULL, explain_p))
21242 return 1;
21244 if (flag_noexcept_type)
21246 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21247 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21248 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21249 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21250 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21251 && uses_template_parms (TREE_PURPOSE (pspec)))
21252 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21253 TREE_PURPOSE (aspec),
21254 UNIFY_ALLOW_NONE, explain_p);
21255 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21256 return unify_type_mismatch (explain_p, parm, arg);
21259 return 0;
21262 case OFFSET_TYPE:
21263 /* Unify a pointer to member with a pointer to member function, which
21264 deduces the type of the member as a function type. */
21265 if (TYPE_PTRMEMFUNC_P (arg))
21267 /* Check top-level cv qualifiers */
21268 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21269 return unify_cv_qual_mismatch (explain_p, parm, arg);
21271 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21272 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21273 UNIFY_ALLOW_NONE, explain_p);
21275 /* Determine the type of the function we are unifying against. */
21276 tree fntype = static_fn_type (arg);
21278 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21281 if (TREE_CODE (arg) != OFFSET_TYPE)
21282 return unify_type_mismatch (explain_p, parm, arg);
21283 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21284 TYPE_OFFSET_BASETYPE (arg),
21285 UNIFY_ALLOW_NONE, explain_p);
21286 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21287 strict, explain_p);
21289 case CONST_DECL:
21290 if (DECL_TEMPLATE_PARM_P (parm))
21291 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21292 if (arg != scalar_constant_value (parm))
21293 return unify_template_argument_mismatch (explain_p, parm, arg);
21294 return unify_success (explain_p);
21296 case FIELD_DECL:
21297 case TEMPLATE_DECL:
21298 /* Matched cases are handled by the ARG == PARM test above. */
21299 return unify_template_argument_mismatch (explain_p, parm, arg);
21301 case VAR_DECL:
21302 /* We might get a variable as a non-type template argument in parm if the
21303 corresponding parameter is type-dependent. Make any necessary
21304 adjustments based on whether arg is a reference. */
21305 if (CONSTANT_CLASS_P (arg))
21306 parm = fold_non_dependent_expr (parm);
21307 else if (REFERENCE_REF_P (arg))
21309 tree sub = TREE_OPERAND (arg, 0);
21310 STRIP_NOPS (sub);
21311 if (TREE_CODE (sub) == ADDR_EXPR)
21312 arg = TREE_OPERAND (sub, 0);
21314 /* Now use the normal expression code to check whether they match. */
21315 goto expr;
21317 case TYPE_ARGUMENT_PACK:
21318 case NONTYPE_ARGUMENT_PACK:
21319 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21320 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21322 case TYPEOF_TYPE:
21323 case DECLTYPE_TYPE:
21324 case UNDERLYING_TYPE:
21325 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21326 or UNDERLYING_TYPE nodes. */
21327 return unify_success (explain_p);
21329 case ERROR_MARK:
21330 /* Unification fails if we hit an error node. */
21331 return unify_invalid (explain_p);
21333 case INDIRECT_REF:
21334 if (REFERENCE_REF_P (parm))
21336 bool pexp = PACK_EXPANSION_P (arg);
21337 if (pexp)
21338 arg = PACK_EXPANSION_PATTERN (arg);
21339 if (REFERENCE_REF_P (arg))
21340 arg = TREE_OPERAND (arg, 0);
21341 if (pexp)
21342 arg = make_pack_expansion (arg);
21343 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21344 strict, explain_p);
21346 /* FALLTHRU */
21348 default:
21349 /* An unresolved overload is a nondeduced context. */
21350 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21351 return unify_success (explain_p);
21352 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21353 expr:
21354 /* We must be looking at an expression. This can happen with
21355 something like:
21357 template <int I>
21358 void foo(S<I>, S<I + 2>);
21360 This is a "nondeduced context":
21362 [deduct.type]
21364 The nondeduced contexts are:
21366 --A type that is a template-id in which one or more of
21367 the template-arguments is an expression that references
21368 a template-parameter.
21370 In these cases, we assume deduction succeeded, but don't
21371 actually infer any unifications. */
21373 if (!uses_template_parms (parm)
21374 && !template_args_equal (parm, arg))
21375 return unify_expression_unequal (explain_p, parm, arg);
21376 else
21377 return unify_success (explain_p);
21380 #undef RECUR_AND_CHECK_FAILURE
21382 /* Note that DECL can be defined in this translation unit, if
21383 required. */
21385 static void
21386 mark_definable (tree decl)
21388 tree clone;
21389 DECL_NOT_REALLY_EXTERN (decl) = 1;
21390 FOR_EACH_CLONE (clone, decl)
21391 DECL_NOT_REALLY_EXTERN (clone) = 1;
21394 /* Called if RESULT is explicitly instantiated, or is a member of an
21395 explicitly instantiated class. */
21397 void
21398 mark_decl_instantiated (tree result, int extern_p)
21400 SET_DECL_EXPLICIT_INSTANTIATION (result);
21402 /* If this entity has already been written out, it's too late to
21403 make any modifications. */
21404 if (TREE_ASM_WRITTEN (result))
21405 return;
21407 /* For anonymous namespace we don't need to do anything. */
21408 if (decl_anon_ns_mem_p (result))
21410 gcc_assert (!TREE_PUBLIC (result));
21411 return;
21414 if (TREE_CODE (result) != FUNCTION_DECL)
21415 /* The TREE_PUBLIC flag for function declarations will have been
21416 set correctly by tsubst. */
21417 TREE_PUBLIC (result) = 1;
21419 /* This might have been set by an earlier implicit instantiation. */
21420 DECL_COMDAT (result) = 0;
21422 if (extern_p)
21423 DECL_NOT_REALLY_EXTERN (result) = 0;
21424 else
21426 mark_definable (result);
21427 mark_needed (result);
21428 /* Always make artificials weak. */
21429 if (DECL_ARTIFICIAL (result) && flag_weak)
21430 comdat_linkage (result);
21431 /* For WIN32 we also want to put explicit instantiations in
21432 linkonce sections. */
21433 else if (TREE_PUBLIC (result))
21434 maybe_make_one_only (result);
21437 /* If EXTERN_P, then this function will not be emitted -- unless
21438 followed by an explicit instantiation, at which point its linkage
21439 will be adjusted. If !EXTERN_P, then this function will be
21440 emitted here. In neither circumstance do we want
21441 import_export_decl to adjust the linkage. */
21442 DECL_INTERFACE_KNOWN (result) = 1;
21445 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21446 important template arguments. If any are missing, we check whether
21447 they're important by using error_mark_node for substituting into any
21448 args that were used for partial ordering (the ones between ARGS and END)
21449 and seeing if it bubbles up. */
21451 static bool
21452 check_undeduced_parms (tree targs, tree args, tree end)
21454 bool found = false;
21455 int i;
21456 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21457 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21459 found = true;
21460 TREE_VEC_ELT (targs, i) = error_mark_node;
21462 if (found)
21464 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21465 if (substed == error_mark_node)
21466 return true;
21468 return false;
21471 /* Given two function templates PAT1 and PAT2, return:
21473 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21474 -1 if PAT2 is more specialized than PAT1.
21475 0 if neither is more specialized.
21477 LEN indicates the number of parameters we should consider
21478 (defaulted parameters should not be considered).
21480 The 1998 std underspecified function template partial ordering, and
21481 DR214 addresses the issue. We take pairs of arguments, one from
21482 each of the templates, and deduce them against each other. One of
21483 the templates will be more specialized if all the *other*
21484 template's arguments deduce against its arguments and at least one
21485 of its arguments *does* *not* deduce against the other template's
21486 corresponding argument. Deduction is done as for class templates.
21487 The arguments used in deduction have reference and top level cv
21488 qualifiers removed. Iff both arguments were originally reference
21489 types *and* deduction succeeds in both directions, an lvalue reference
21490 wins against an rvalue reference and otherwise the template
21491 with the more cv-qualified argument wins for that pairing (if
21492 neither is more cv-qualified, they both are equal). Unlike regular
21493 deduction, after all the arguments have been deduced in this way,
21494 we do *not* verify the deduced template argument values can be
21495 substituted into non-deduced contexts.
21497 The logic can be a bit confusing here, because we look at deduce1 and
21498 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21499 can find template arguments for pat1 to make arg1 look like arg2, that
21500 means that arg2 is at least as specialized as arg1. */
21503 more_specialized_fn (tree pat1, tree pat2, int len)
21505 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21506 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21507 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21508 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21509 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21510 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21511 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21512 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21513 tree origs1, origs2;
21514 bool lose1 = false;
21515 bool lose2 = false;
21517 /* Remove the this parameter from non-static member functions. If
21518 one is a non-static member function and the other is not a static
21519 member function, remove the first parameter from that function
21520 also. This situation occurs for operator functions where we
21521 locate both a member function (with this pointer) and non-member
21522 operator (with explicit first operand). */
21523 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21525 len--; /* LEN is the number of significant arguments for DECL1 */
21526 args1 = TREE_CHAIN (args1);
21527 if (!DECL_STATIC_FUNCTION_P (decl2))
21528 args2 = TREE_CHAIN (args2);
21530 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21532 args2 = TREE_CHAIN (args2);
21533 if (!DECL_STATIC_FUNCTION_P (decl1))
21535 len--;
21536 args1 = TREE_CHAIN (args1);
21540 /* If only one is a conversion operator, they are unordered. */
21541 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21542 return 0;
21544 /* Consider the return type for a conversion function */
21545 if (DECL_CONV_FN_P (decl1))
21547 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21548 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21549 len++;
21552 processing_template_decl++;
21554 origs1 = args1;
21555 origs2 = args2;
21557 while (len--
21558 /* Stop when an ellipsis is seen. */
21559 && args1 != NULL_TREE && args2 != NULL_TREE)
21561 tree arg1 = TREE_VALUE (args1);
21562 tree arg2 = TREE_VALUE (args2);
21563 int deduce1, deduce2;
21564 int quals1 = -1;
21565 int quals2 = -1;
21566 int ref1 = 0;
21567 int ref2 = 0;
21569 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21570 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21572 /* When both arguments are pack expansions, we need only
21573 unify the patterns themselves. */
21574 arg1 = PACK_EXPANSION_PATTERN (arg1);
21575 arg2 = PACK_EXPANSION_PATTERN (arg2);
21577 /* This is the last comparison we need to do. */
21578 len = 0;
21581 /* DR 1847: If a particular P contains no template-parameters that
21582 participate in template argument deduction, that P is not used to
21583 determine the ordering. */
21584 if (!uses_deducible_template_parms (arg1)
21585 && !uses_deducible_template_parms (arg2))
21586 goto next;
21588 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21590 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21591 arg1 = TREE_TYPE (arg1);
21592 quals1 = cp_type_quals (arg1);
21595 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21597 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21598 arg2 = TREE_TYPE (arg2);
21599 quals2 = cp_type_quals (arg2);
21602 arg1 = TYPE_MAIN_VARIANT (arg1);
21603 arg2 = TYPE_MAIN_VARIANT (arg2);
21605 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21607 int i, len2 = remaining_arguments (args2);
21608 tree parmvec = make_tree_vec (1);
21609 tree argvec = make_tree_vec (len2);
21610 tree ta = args2;
21612 /* Setup the parameter vector, which contains only ARG1. */
21613 TREE_VEC_ELT (parmvec, 0) = arg1;
21615 /* Setup the argument vector, which contains the remaining
21616 arguments. */
21617 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21618 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21620 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21621 argvec, DEDUCE_EXACT,
21622 /*subr=*/true, /*explain_p=*/false)
21623 == 0);
21625 /* We cannot deduce in the other direction, because ARG1 is
21626 a pack expansion but ARG2 is not. */
21627 deduce2 = 0;
21629 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21631 int i, len1 = remaining_arguments (args1);
21632 tree parmvec = make_tree_vec (1);
21633 tree argvec = make_tree_vec (len1);
21634 tree ta = args1;
21636 /* Setup the parameter vector, which contains only ARG1. */
21637 TREE_VEC_ELT (parmvec, 0) = arg2;
21639 /* Setup the argument vector, which contains the remaining
21640 arguments. */
21641 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21642 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21644 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21645 argvec, DEDUCE_EXACT,
21646 /*subr=*/true, /*explain_p=*/false)
21647 == 0);
21649 /* We cannot deduce in the other direction, because ARG2 is
21650 a pack expansion but ARG1 is not.*/
21651 deduce1 = 0;
21654 else
21656 /* The normal case, where neither argument is a pack
21657 expansion. */
21658 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21659 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21660 == 0);
21661 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21662 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21663 == 0);
21666 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21667 arg2, then arg2 is not as specialized as arg1. */
21668 if (!deduce1)
21669 lose2 = true;
21670 if (!deduce2)
21671 lose1 = true;
21673 /* "If, for a given type, deduction succeeds in both directions
21674 (i.e., the types are identical after the transformations above)
21675 and both P and A were reference types (before being replaced with
21676 the type referred to above):
21677 - if the type from the argument template was an lvalue reference and
21678 the type from the parameter template was not, the argument type is
21679 considered to be more specialized than the other; otherwise,
21680 - if the type from the argument template is more cv-qualified
21681 than the type from the parameter template (as described above),
21682 the argument type is considered to be more specialized than the other;
21683 otherwise,
21684 - neither type is more specialized than the other." */
21686 if (deduce1 && deduce2)
21688 if (ref1 && ref2 && ref1 != ref2)
21690 if (ref1 > ref2)
21691 lose1 = true;
21692 else
21693 lose2 = true;
21695 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21697 if ((quals1 & quals2) == quals2)
21698 lose2 = true;
21699 if ((quals1 & quals2) == quals1)
21700 lose1 = true;
21704 if (lose1 && lose2)
21705 /* We've failed to deduce something in either direction.
21706 These must be unordered. */
21707 break;
21709 next:
21711 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21712 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21713 /* We have already processed all of the arguments in our
21714 handing of the pack expansion type. */
21715 len = 0;
21717 args1 = TREE_CHAIN (args1);
21718 args2 = TREE_CHAIN (args2);
21721 /* "In most cases, all template parameters must have values in order for
21722 deduction to succeed, but for partial ordering purposes a template
21723 parameter may remain without a value provided it is not used in the
21724 types being used for partial ordering."
21726 Thus, if we are missing any of the targs1 we need to substitute into
21727 origs1, then pat2 is not as specialized as pat1. This can happen when
21728 there is a nondeduced context. */
21729 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21730 lose2 = true;
21731 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21732 lose1 = true;
21734 processing_template_decl--;
21736 /* If both deductions succeed, the partial ordering selects the more
21737 constrained template. */
21738 if (!lose1 && !lose2)
21740 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21741 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21742 lose1 = !subsumes_constraints (c1, c2);
21743 lose2 = !subsumes_constraints (c2, c1);
21746 /* All things being equal, if the next argument is a pack expansion
21747 for one function but not for the other, prefer the
21748 non-variadic function. FIXME this is bogus; see c++/41958. */
21749 if (lose1 == lose2
21750 && args1 && TREE_VALUE (args1)
21751 && args2 && TREE_VALUE (args2))
21753 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21754 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21757 if (lose1 == lose2)
21758 return 0;
21759 else if (!lose1)
21760 return 1;
21761 else
21762 return -1;
21765 /* Determine which of two partial specializations of TMPL is more
21766 specialized.
21768 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21769 to the first partial specialization. The TREE_PURPOSE is the
21770 innermost set of template parameters for the partial
21771 specialization. PAT2 is similar, but for the second template.
21773 Return 1 if the first partial specialization is more specialized;
21774 -1 if the second is more specialized; 0 if neither is more
21775 specialized.
21777 See [temp.class.order] for information about determining which of
21778 two templates is more specialized. */
21780 static int
21781 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21783 tree targs;
21784 int winner = 0;
21785 bool any_deductions = false;
21787 tree tmpl1 = TREE_VALUE (pat1);
21788 tree tmpl2 = TREE_VALUE (pat2);
21789 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21790 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21792 /* Just like what happens for functions, if we are ordering between
21793 different template specializations, we may encounter dependent
21794 types in the arguments, and we need our dependency check functions
21795 to behave correctly. */
21796 ++processing_template_decl;
21797 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21798 if (targs)
21800 --winner;
21801 any_deductions = true;
21804 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21805 if (targs)
21807 ++winner;
21808 any_deductions = true;
21810 --processing_template_decl;
21812 /* If both deductions succeed, the partial ordering selects the more
21813 constrained template. */
21814 if (!winner && any_deductions)
21815 return more_constrained (tmpl1, tmpl2);
21817 /* In the case of a tie where at least one of the templates
21818 has a parameter pack at the end, the template with the most
21819 non-packed parameters wins. */
21820 if (winner == 0
21821 && any_deductions
21822 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21823 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21825 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21826 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21827 int len1 = TREE_VEC_LENGTH (args1);
21828 int len2 = TREE_VEC_LENGTH (args2);
21830 /* We don't count the pack expansion at the end. */
21831 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21832 --len1;
21833 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21834 --len2;
21836 if (len1 > len2)
21837 return 1;
21838 else if (len1 < len2)
21839 return -1;
21842 return winner;
21845 /* Return the template arguments that will produce the function signature
21846 DECL from the function template FN, with the explicit template
21847 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21848 also match. Return NULL_TREE if no satisfactory arguments could be
21849 found. */
21851 static tree
21852 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21854 int ntparms = DECL_NTPARMS (fn);
21855 tree targs = make_tree_vec (ntparms);
21856 tree decl_type = TREE_TYPE (decl);
21857 tree decl_arg_types;
21858 tree *args;
21859 unsigned int nargs, ix;
21860 tree arg;
21862 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21864 /* Never do unification on the 'this' parameter. */
21865 decl_arg_types = skip_artificial_parms_for (decl,
21866 TYPE_ARG_TYPES (decl_type));
21868 nargs = list_length (decl_arg_types);
21869 args = XALLOCAVEC (tree, nargs);
21870 for (arg = decl_arg_types, ix = 0;
21871 arg != NULL_TREE && arg != void_list_node;
21872 arg = TREE_CHAIN (arg), ++ix)
21873 args[ix] = TREE_VALUE (arg);
21875 if (fn_type_unification (fn, explicit_args, targs,
21876 args, ix,
21877 (check_rettype || DECL_CONV_FN_P (fn)
21878 ? TREE_TYPE (decl_type) : NULL_TREE),
21879 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21880 /*decltype*/false)
21881 == error_mark_node)
21882 return NULL_TREE;
21884 return targs;
21887 /* Return the innermost template arguments that, when applied to a partial
21888 specialization SPEC_TMPL of TMPL, yield the ARGS.
21890 For example, suppose we have:
21892 template <class T, class U> struct S {};
21893 template <class T> struct S<T*, int> {};
21895 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21896 partial specialization and the ARGS will be {double*, int}. The resulting
21897 vector will be {double}, indicating that `T' is bound to `double'. */
21899 static tree
21900 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21902 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21903 tree spec_args
21904 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21905 int i, ntparms = TREE_VEC_LENGTH (tparms);
21906 tree deduced_args;
21907 tree innermost_deduced_args;
21909 innermost_deduced_args = make_tree_vec (ntparms);
21910 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21912 deduced_args = copy_node (args);
21913 SET_TMPL_ARGS_LEVEL (deduced_args,
21914 TMPL_ARGS_DEPTH (deduced_args),
21915 innermost_deduced_args);
21917 else
21918 deduced_args = innermost_deduced_args;
21920 bool tried_array_deduction = (cxx_dialect < cxx17);
21921 again:
21922 if (unify (tparms, deduced_args,
21923 INNERMOST_TEMPLATE_ARGS (spec_args),
21924 INNERMOST_TEMPLATE_ARGS (args),
21925 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21926 return NULL_TREE;
21928 for (i = 0; i < ntparms; ++i)
21929 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21931 if (!tried_array_deduction)
21933 try_array_deduction (tparms, innermost_deduced_args,
21934 INNERMOST_TEMPLATE_ARGS (spec_args));
21935 tried_array_deduction = true;
21936 if (TREE_VEC_ELT (innermost_deduced_args, i))
21937 goto again;
21939 return NULL_TREE;
21942 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21943 if (!push_tinst_level (tinst))
21945 excessive_deduction_depth = true;
21946 return NULL_TREE;
21949 /* Verify that nondeduced template arguments agree with the type
21950 obtained from argument deduction.
21952 For example:
21954 struct A { typedef int X; };
21955 template <class T, class U> struct C {};
21956 template <class T> struct C<T, typename T::X> {};
21958 Then with the instantiation `C<A, int>', we can deduce that
21959 `T' is `A' but unify () does not check whether `typename T::X'
21960 is `int'. */
21961 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21963 if (spec_args != error_mark_node)
21964 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21965 INNERMOST_TEMPLATE_ARGS (spec_args),
21966 tmpl, tf_none, false, false);
21968 pop_tinst_level ();
21970 if (spec_args == error_mark_node
21971 /* We only need to check the innermost arguments; the other
21972 arguments will always agree. */
21973 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21974 INNERMOST_TEMPLATE_ARGS (args)))
21975 return NULL_TREE;
21977 /* Now that we have bindings for all of the template arguments,
21978 ensure that the arguments deduced for the template template
21979 parameters have compatible template parameter lists. See the use
21980 of template_template_parm_bindings_ok_p in fn_type_unification
21981 for more information. */
21982 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21983 return NULL_TREE;
21985 return deduced_args;
21988 // Compare two function templates T1 and T2 by deducing bindings
21989 // from one against the other. If both deductions succeed, compare
21990 // constraints to see which is more constrained.
21991 static int
21992 more_specialized_inst (tree t1, tree t2)
21994 int fate = 0;
21995 int count = 0;
21997 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21999 --fate;
22000 ++count;
22003 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22005 ++fate;
22006 ++count;
22009 // If both deductions succeed, then one may be more constrained.
22010 if (count == 2 && fate == 0)
22011 fate = more_constrained (t1, t2);
22013 return fate;
22016 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22017 Return the TREE_LIST node with the most specialized template, if
22018 any. If there is no most specialized template, the error_mark_node
22019 is returned.
22021 Note that this function does not look at, or modify, the
22022 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22023 returned is one of the elements of INSTANTIATIONS, callers may
22024 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22025 and retrieve it from the value returned. */
22027 tree
22028 most_specialized_instantiation (tree templates)
22030 tree fn, champ;
22032 ++processing_template_decl;
22034 champ = templates;
22035 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22037 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22038 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22039 if (fate == -1)
22040 champ = fn;
22041 else if (!fate)
22043 /* Equally specialized, move to next function. If there
22044 is no next function, nothing's most specialized. */
22045 fn = TREE_CHAIN (fn);
22046 champ = fn;
22047 if (!fn)
22048 break;
22052 if (champ)
22053 /* Now verify that champ is better than everything earlier in the
22054 instantiation list. */
22055 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22056 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22058 champ = NULL_TREE;
22059 break;
22063 processing_template_decl--;
22065 if (!champ)
22066 return error_mark_node;
22068 return champ;
22071 /* If DECL is a specialization of some template, return the most
22072 general such template. Otherwise, returns NULL_TREE.
22074 For example, given:
22076 template <class T> struct S { template <class U> void f(U); };
22078 if TMPL is `template <class U> void S<int>::f(U)' this will return
22079 the full template. This function will not trace past partial
22080 specializations, however. For example, given in addition:
22082 template <class T> struct S<T*> { template <class U> void f(U); };
22084 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22085 `template <class T> template <class U> S<T*>::f(U)'. */
22087 tree
22088 most_general_template (tree decl)
22090 if (TREE_CODE (decl) != TEMPLATE_DECL)
22092 if (tree tinfo = get_template_info (decl))
22093 decl = TI_TEMPLATE (tinfo);
22094 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22095 template friend, or a FIELD_DECL for a capture pack. */
22096 if (TREE_CODE (decl) != TEMPLATE_DECL)
22097 return NULL_TREE;
22100 /* Look for more and more general templates. */
22101 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22103 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22104 (See cp-tree.h for details.) */
22105 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22106 break;
22108 if (CLASS_TYPE_P (TREE_TYPE (decl))
22109 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22110 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22111 break;
22113 /* Stop if we run into an explicitly specialized class template. */
22114 if (!DECL_NAMESPACE_SCOPE_P (decl)
22115 && DECL_CONTEXT (decl)
22116 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22117 break;
22119 decl = DECL_TI_TEMPLATE (decl);
22122 return decl;
22125 /* Return the most specialized of the template partial specializations
22126 which can produce TARGET, a specialization of some class or variable
22127 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22128 a TEMPLATE_DECL node corresponding to the partial specialization, while
22129 the TREE_PURPOSE is the set of template arguments that must be
22130 substituted into the template pattern in order to generate TARGET.
22132 If the choice of partial specialization is ambiguous, a diagnostic
22133 is issued, and the error_mark_node is returned. If there are no
22134 partial specializations matching TARGET, then NULL_TREE is
22135 returned, indicating that the primary template should be used. */
22137 static tree
22138 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22140 tree list = NULL_TREE;
22141 tree t;
22142 tree champ;
22143 int fate;
22144 bool ambiguous_p;
22145 tree outer_args = NULL_TREE;
22146 tree tmpl, args;
22148 if (TYPE_P (target))
22150 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22151 tmpl = TI_TEMPLATE (tinfo);
22152 args = TI_ARGS (tinfo);
22154 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22156 tmpl = TREE_OPERAND (target, 0);
22157 args = TREE_OPERAND (target, 1);
22159 else if (VAR_P (target))
22161 tree tinfo = DECL_TEMPLATE_INFO (target);
22162 tmpl = TI_TEMPLATE (tinfo);
22163 args = TI_ARGS (tinfo);
22165 else
22166 gcc_unreachable ();
22168 tree main_tmpl = most_general_template (tmpl);
22170 /* For determining which partial specialization to use, only the
22171 innermost args are interesting. */
22172 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22174 outer_args = strip_innermost_template_args (args, 1);
22175 args = INNERMOST_TEMPLATE_ARGS (args);
22178 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22180 tree spec_args;
22181 tree spec_tmpl = TREE_VALUE (t);
22183 if (outer_args)
22185 /* Substitute in the template args from the enclosing class. */
22186 ++processing_template_decl;
22187 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22188 --processing_template_decl;
22191 if (spec_tmpl == error_mark_node)
22192 return error_mark_node;
22194 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22195 if (spec_args)
22197 if (outer_args)
22198 spec_args = add_to_template_args (outer_args, spec_args);
22200 /* Keep the candidate only if the constraints are satisfied,
22201 or if we're not compiling with concepts. */
22202 if (!flag_concepts
22203 || constraints_satisfied_p (spec_tmpl, spec_args))
22205 list = tree_cons (spec_args, TREE_VALUE (t), list);
22206 TREE_TYPE (list) = TREE_TYPE (t);
22211 if (! list)
22212 return NULL_TREE;
22214 ambiguous_p = false;
22215 t = list;
22216 champ = t;
22217 t = TREE_CHAIN (t);
22218 for (; t; t = TREE_CHAIN (t))
22220 fate = more_specialized_partial_spec (tmpl, champ, t);
22221 if (fate == 1)
22223 else
22225 if (fate == 0)
22227 t = TREE_CHAIN (t);
22228 if (! t)
22230 ambiguous_p = true;
22231 break;
22234 champ = t;
22238 if (!ambiguous_p)
22239 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22241 fate = more_specialized_partial_spec (tmpl, champ, t);
22242 if (fate != 1)
22244 ambiguous_p = true;
22245 break;
22249 if (ambiguous_p)
22251 const char *str;
22252 char *spaces = NULL;
22253 if (!(complain & tf_error))
22254 return error_mark_node;
22255 if (TYPE_P (target))
22256 error ("ambiguous template instantiation for %q#T", target);
22257 else
22258 error ("ambiguous template instantiation for %q#D", target);
22259 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22260 for (t = list; t; t = TREE_CHAIN (t))
22262 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22263 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22264 "%s %#qS", spaces ? spaces : str, subst);
22265 spaces = spaces ? spaces : get_spaces (str);
22267 free (spaces);
22268 return error_mark_node;
22271 return champ;
22274 /* Explicitly instantiate DECL. */
22276 void
22277 do_decl_instantiation (tree decl, tree storage)
22279 tree result = NULL_TREE;
22280 int extern_p = 0;
22282 if (!decl || decl == error_mark_node)
22283 /* An error occurred, for which grokdeclarator has already issued
22284 an appropriate message. */
22285 return;
22286 else if (! DECL_LANG_SPECIFIC (decl))
22288 error ("explicit instantiation of non-template %q#D", decl);
22289 return;
22292 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22293 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22295 if (VAR_P (decl) && !var_templ)
22297 /* There is an asymmetry here in the way VAR_DECLs and
22298 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22299 the latter, the DECL we get back will be marked as a
22300 template instantiation, and the appropriate
22301 DECL_TEMPLATE_INFO will be set up. This does not happen for
22302 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22303 should handle VAR_DECLs as it currently handles
22304 FUNCTION_DECLs. */
22305 if (!DECL_CLASS_SCOPE_P (decl))
22307 error ("%qD is not a static data member of a class template", decl);
22308 return;
22310 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22311 if (!result || !VAR_P (result))
22313 error ("no matching template for %qD found", decl);
22314 return;
22316 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22318 error ("type %qT for explicit instantiation %qD does not match "
22319 "declared type %qT", TREE_TYPE (result), decl,
22320 TREE_TYPE (decl));
22321 return;
22324 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22326 error ("explicit instantiation of %q#D", decl);
22327 return;
22329 else
22330 result = decl;
22332 /* Check for various error cases. Note that if the explicit
22333 instantiation is valid the RESULT will currently be marked as an
22334 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22335 until we get here. */
22337 if (DECL_TEMPLATE_SPECIALIZATION (result))
22339 /* DR 259 [temp.spec].
22341 Both an explicit instantiation and a declaration of an explicit
22342 specialization shall not appear in a program unless the explicit
22343 instantiation follows a declaration of the explicit specialization.
22345 For a given set of template parameters, if an explicit
22346 instantiation of a template appears after a declaration of an
22347 explicit specialization for that template, the explicit
22348 instantiation has no effect. */
22349 return;
22351 else if (DECL_EXPLICIT_INSTANTIATION (result))
22353 /* [temp.spec]
22355 No program shall explicitly instantiate any template more
22356 than once.
22358 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22359 the first instantiation was `extern' and the second is not,
22360 and EXTERN_P for the opposite case. */
22361 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22362 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22363 /* If an "extern" explicit instantiation follows an ordinary
22364 explicit instantiation, the template is instantiated. */
22365 if (extern_p)
22366 return;
22368 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22370 error ("no matching template for %qD found", result);
22371 return;
22373 else if (!DECL_TEMPLATE_INFO (result))
22375 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22376 return;
22379 if (storage == NULL_TREE)
22381 else if (storage == ridpointers[(int) RID_EXTERN])
22383 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22384 pedwarn (input_location, OPT_Wpedantic,
22385 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22386 "instantiations");
22387 extern_p = 1;
22389 else
22390 error ("storage class %qD applied to template instantiation", storage);
22392 check_explicit_instantiation_namespace (result);
22393 mark_decl_instantiated (result, extern_p);
22394 if (! extern_p)
22395 instantiate_decl (result, /*defer_ok=*/true,
22396 /*expl_inst_class_mem_p=*/false);
22399 static void
22400 mark_class_instantiated (tree t, int extern_p)
22402 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22403 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22404 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22405 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22406 if (! extern_p)
22408 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22409 rest_of_type_compilation (t, 1);
22413 /* Called from do_type_instantiation through binding_table_foreach to
22414 do recursive instantiation for the type bound in ENTRY. */
22415 static void
22416 bt_instantiate_type_proc (binding_entry entry, void *data)
22418 tree storage = *(tree *) data;
22420 if (MAYBE_CLASS_TYPE_P (entry->type)
22421 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22422 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22425 /* Perform an explicit instantiation of template class T. STORAGE, if
22426 non-null, is the RID for extern, inline or static. COMPLAIN is
22427 nonzero if this is called from the parser, zero if called recursively,
22428 since the standard is unclear (as detailed below). */
22430 void
22431 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22433 int extern_p = 0;
22434 int nomem_p = 0;
22435 int static_p = 0;
22436 int previous_instantiation_extern_p = 0;
22438 if (TREE_CODE (t) == TYPE_DECL)
22439 t = TREE_TYPE (t);
22441 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22443 tree tmpl =
22444 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22445 if (tmpl)
22446 error ("explicit instantiation of non-class template %qD", tmpl);
22447 else
22448 error ("explicit instantiation of non-template type %qT", t);
22449 return;
22452 complete_type (t);
22454 if (!COMPLETE_TYPE_P (t))
22456 if (complain & tf_error)
22457 error ("explicit instantiation of %q#T before definition of template",
22459 return;
22462 if (storage != NULL_TREE)
22464 if (!in_system_header_at (input_location))
22466 if (storage == ridpointers[(int) RID_EXTERN])
22468 if (cxx_dialect == cxx98)
22469 pedwarn (input_location, OPT_Wpedantic,
22470 "ISO C++ 1998 forbids the use of %<extern%> on "
22471 "explicit instantiations");
22473 else
22474 pedwarn (input_location, OPT_Wpedantic,
22475 "ISO C++ forbids the use of %qE"
22476 " on explicit instantiations", storage);
22479 if (storage == ridpointers[(int) RID_INLINE])
22480 nomem_p = 1;
22481 else if (storage == ridpointers[(int) RID_EXTERN])
22482 extern_p = 1;
22483 else if (storage == ridpointers[(int) RID_STATIC])
22484 static_p = 1;
22485 else
22487 error ("storage class %qD applied to template instantiation",
22488 storage);
22489 extern_p = 0;
22493 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22495 /* DR 259 [temp.spec].
22497 Both an explicit instantiation and a declaration of an explicit
22498 specialization shall not appear in a program unless the explicit
22499 instantiation follows a declaration of the explicit specialization.
22501 For a given set of template parameters, if an explicit
22502 instantiation of a template appears after a declaration of an
22503 explicit specialization for that template, the explicit
22504 instantiation has no effect. */
22505 return;
22507 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22509 /* [temp.spec]
22511 No program shall explicitly instantiate any template more
22512 than once.
22514 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22515 instantiation was `extern'. If EXTERN_P then the second is.
22516 These cases are OK. */
22517 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22519 if (!previous_instantiation_extern_p && !extern_p
22520 && (complain & tf_error))
22521 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22523 /* If we've already instantiated the template, just return now. */
22524 if (!CLASSTYPE_INTERFACE_ONLY (t))
22525 return;
22528 check_explicit_instantiation_namespace (TYPE_NAME (t));
22529 mark_class_instantiated (t, extern_p);
22531 if (nomem_p)
22532 return;
22534 /* In contrast to implicit instantiation, where only the
22535 declarations, and not the definitions, of members are
22536 instantiated, we have here:
22538 [temp.explicit]
22540 The explicit instantiation of a class template specialization
22541 implies the instantiation of all of its members not
22542 previously explicitly specialized in the translation unit
22543 containing the explicit instantiation.
22545 Of course, we can't instantiate member template classes, since we
22546 don't have any arguments for them. Note that the standard is
22547 unclear on whether the instantiation of the members are
22548 *explicit* instantiations or not. However, the most natural
22549 interpretation is that it should be an explicit
22550 instantiation. */
22551 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22552 if ((VAR_P (fld)
22553 || (TREE_CODE (fld) == FUNCTION_DECL
22554 && !static_p
22555 && user_provided_p (fld)))
22556 && DECL_TEMPLATE_INSTANTIATION (fld))
22558 mark_decl_instantiated (fld, extern_p);
22559 if (! extern_p)
22560 instantiate_decl (fld, /*defer_ok=*/true,
22561 /*expl_inst_class_mem_p=*/true);
22564 if (CLASSTYPE_NESTED_UTDS (t))
22565 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22566 bt_instantiate_type_proc, &storage);
22569 /* Given a function DECL, which is a specialization of TMPL, modify
22570 DECL to be a re-instantiation of TMPL with the same template
22571 arguments. TMPL should be the template into which tsubst'ing
22572 should occur for DECL, not the most general template.
22574 One reason for doing this is a scenario like this:
22576 template <class T>
22577 void f(const T&, int i);
22579 void g() { f(3, 7); }
22581 template <class T>
22582 void f(const T& t, const int i) { }
22584 Note that when the template is first instantiated, with
22585 instantiate_template, the resulting DECL will have no name for the
22586 first parameter, and the wrong type for the second. So, when we go
22587 to instantiate the DECL, we regenerate it. */
22589 static void
22590 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22592 /* The arguments used to instantiate DECL, from the most general
22593 template. */
22594 tree code_pattern;
22596 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22598 /* Make sure that we can see identifiers, and compute access
22599 correctly. */
22600 push_access_scope (decl);
22602 if (TREE_CODE (decl) == FUNCTION_DECL)
22604 tree decl_parm;
22605 tree pattern_parm;
22606 tree specs;
22607 int args_depth;
22608 int parms_depth;
22610 args_depth = TMPL_ARGS_DEPTH (args);
22611 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22612 if (args_depth > parms_depth)
22613 args = get_innermost_template_args (args, parms_depth);
22615 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22616 args, tf_error, NULL_TREE,
22617 /*defer_ok*/false);
22618 if (specs && specs != error_mark_node)
22619 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22620 specs);
22622 /* Merge parameter declarations. */
22623 decl_parm = skip_artificial_parms_for (decl,
22624 DECL_ARGUMENTS (decl));
22625 pattern_parm
22626 = skip_artificial_parms_for (code_pattern,
22627 DECL_ARGUMENTS (code_pattern));
22628 while (decl_parm && !DECL_PACK_P (pattern_parm))
22630 tree parm_type;
22631 tree attributes;
22633 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22634 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22635 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22636 NULL_TREE);
22637 parm_type = type_decays_to (parm_type);
22638 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22639 TREE_TYPE (decl_parm) = parm_type;
22640 attributes = DECL_ATTRIBUTES (pattern_parm);
22641 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22643 DECL_ATTRIBUTES (decl_parm) = attributes;
22644 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22646 decl_parm = DECL_CHAIN (decl_parm);
22647 pattern_parm = DECL_CHAIN (pattern_parm);
22649 /* Merge any parameters that match with the function parameter
22650 pack. */
22651 if (pattern_parm && DECL_PACK_P (pattern_parm))
22653 int i, len;
22654 tree expanded_types;
22655 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22656 the parameters in this function parameter pack. */
22657 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22658 args, tf_error, NULL_TREE);
22659 len = TREE_VEC_LENGTH (expanded_types);
22660 for (i = 0; i < len; i++)
22662 tree parm_type;
22663 tree attributes;
22665 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22666 /* Rename the parameter to include the index. */
22667 DECL_NAME (decl_parm) =
22668 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22669 parm_type = TREE_VEC_ELT (expanded_types, i);
22670 parm_type = type_decays_to (parm_type);
22671 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22672 TREE_TYPE (decl_parm) = parm_type;
22673 attributes = DECL_ATTRIBUTES (pattern_parm);
22674 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22676 DECL_ATTRIBUTES (decl_parm) = attributes;
22677 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22679 decl_parm = DECL_CHAIN (decl_parm);
22682 /* Merge additional specifiers from the CODE_PATTERN. */
22683 if (DECL_DECLARED_INLINE_P (code_pattern)
22684 && !DECL_DECLARED_INLINE_P (decl))
22685 DECL_DECLARED_INLINE_P (decl) = 1;
22687 else if (VAR_P (decl))
22689 start_lambda_scope (decl);
22690 DECL_INITIAL (decl) =
22691 tsubst_expr (DECL_INITIAL (code_pattern), args,
22692 tf_error, DECL_TI_TEMPLATE (decl),
22693 /*integral_constant_expression_p=*/false);
22694 finish_lambda_scope ();
22695 if (VAR_HAD_UNKNOWN_BOUND (decl))
22696 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22697 tf_error, DECL_TI_TEMPLATE (decl));
22699 else
22700 gcc_unreachable ();
22702 pop_access_scope (decl);
22705 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22706 substituted to get DECL. */
22708 tree
22709 template_for_substitution (tree decl)
22711 tree tmpl = DECL_TI_TEMPLATE (decl);
22713 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22714 for the instantiation. This is not always the most general
22715 template. Consider, for example:
22717 template <class T>
22718 struct S { template <class U> void f();
22719 template <> void f<int>(); };
22721 and an instantiation of S<double>::f<int>. We want TD to be the
22722 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22723 while (/* An instantiation cannot have a definition, so we need a
22724 more general template. */
22725 DECL_TEMPLATE_INSTANTIATION (tmpl)
22726 /* We must also deal with friend templates. Given:
22728 template <class T> struct S {
22729 template <class U> friend void f() {};
22732 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22733 so far as the language is concerned, but that's still
22734 where we get the pattern for the instantiation from. On
22735 other hand, if the definition comes outside the class, say:
22737 template <class T> struct S {
22738 template <class U> friend void f();
22740 template <class U> friend void f() {}
22742 we don't need to look any further. That's what the check for
22743 DECL_INITIAL is for. */
22744 || (TREE_CODE (decl) == FUNCTION_DECL
22745 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22746 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22748 /* The present template, TD, should not be a definition. If it
22749 were a definition, we should be using it! Note that we
22750 cannot restructure the loop to just keep going until we find
22751 a template with a definition, since that might go too far if
22752 a specialization was declared, but not defined. */
22754 /* Fetch the more general template. */
22755 tmpl = DECL_TI_TEMPLATE (tmpl);
22758 return tmpl;
22761 /* Returns true if we need to instantiate this template instance even if we
22762 know we aren't going to emit it. */
22764 bool
22765 always_instantiate_p (tree decl)
22767 /* We always instantiate inline functions so that we can inline them. An
22768 explicit instantiation declaration prohibits implicit instantiation of
22769 non-inline functions. With high levels of optimization, we would
22770 normally inline non-inline functions -- but we're not allowed to do
22771 that for "extern template" functions. Therefore, we check
22772 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22773 return ((TREE_CODE (decl) == FUNCTION_DECL
22774 && (DECL_DECLARED_INLINE_P (decl)
22775 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22776 /* And we need to instantiate static data members so that
22777 their initializers are available in integral constant
22778 expressions. */
22779 || (VAR_P (decl)
22780 && decl_maybe_constant_var_p (decl)));
22783 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22784 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22785 error, true otherwise. */
22787 bool
22788 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22790 tree fntype, spec, noex, clone;
22792 /* Don't instantiate a noexcept-specification from template context. */
22793 if (processing_template_decl)
22794 return true;
22796 if (DECL_CLONED_FUNCTION_P (fn))
22797 fn = DECL_CLONED_FUNCTION (fn);
22798 fntype = TREE_TYPE (fn);
22799 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22801 if (!spec || !TREE_PURPOSE (spec))
22802 return true;
22804 noex = TREE_PURPOSE (spec);
22806 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22808 static hash_set<tree>* fns = new hash_set<tree>;
22809 bool added = false;
22810 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22811 spec = get_defaulted_eh_spec (fn, complain);
22812 else if (!(added = !fns->add (fn)))
22814 /* If hash_set::add returns true, the element was already there. */
22815 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22816 DECL_SOURCE_LOCATION (fn));
22817 error_at (loc,
22818 "exception specification of %qD depends on itself",
22819 fn);
22820 spec = noexcept_false_spec;
22822 else if (push_tinst_level (fn))
22824 push_access_scope (fn);
22825 push_deferring_access_checks (dk_no_deferred);
22826 input_location = DECL_SOURCE_LOCATION (fn);
22827 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22828 DEFERRED_NOEXCEPT_ARGS (noex),
22829 tf_warning_or_error, fn,
22830 /*function_p=*/false,
22831 /*integral_constant_expression_p=*/true);
22832 pop_deferring_access_checks ();
22833 pop_access_scope (fn);
22834 pop_tinst_level ();
22835 spec = build_noexcept_spec (noex, tf_warning_or_error);
22836 if (spec == error_mark_node)
22837 spec = noexcept_false_spec;
22839 else
22840 spec = noexcept_false_spec;
22842 if (added)
22843 fns->remove (fn);
22845 if (spec == error_mark_node)
22846 return false;
22848 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22851 FOR_EACH_CLONE (clone, fn)
22853 if (TREE_TYPE (clone) == fntype)
22854 TREE_TYPE (clone) = TREE_TYPE (fn);
22855 else
22856 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22859 return true;
22862 /* We're starting to process the function INST, an instantiation of PATTERN;
22863 add their parameters to local_specializations. */
22865 static void
22866 register_parameter_specializations (tree pattern, tree inst)
22868 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22869 tree spec_parm = DECL_ARGUMENTS (inst);
22870 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22872 register_local_specialization (spec_parm, tmpl_parm);
22873 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22874 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22876 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22878 if (!DECL_PACK_P (tmpl_parm))
22880 register_local_specialization (spec_parm, tmpl_parm);
22881 spec_parm = DECL_CHAIN (spec_parm);
22883 else
22885 /* Register the (value) argument pack as a specialization of
22886 TMPL_PARM, then move on. */
22887 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22888 register_local_specialization (argpack, tmpl_parm);
22891 gcc_assert (!spec_parm);
22894 /* Produce the definition of D, a _DECL generated from a template. If
22895 DEFER_OK is true, then we don't have to actually do the
22896 instantiation now; we just have to do it sometime. Normally it is
22897 an error if this is an explicit instantiation but D is undefined.
22898 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22899 instantiated class template. */
22901 tree
22902 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22904 tree tmpl = DECL_TI_TEMPLATE (d);
22905 tree gen_args;
22906 tree args;
22907 tree td;
22908 tree code_pattern;
22909 tree spec;
22910 tree gen_tmpl;
22911 bool pattern_defined;
22912 location_t saved_loc = input_location;
22913 int saved_unevaluated_operand = cp_unevaluated_operand;
22914 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22915 bool external_p;
22916 bool deleted_p;
22918 /* This function should only be used to instantiate templates for
22919 functions and static member variables. */
22920 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22922 /* A concept is never instantiated. */
22923 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22925 /* Variables are never deferred; if instantiation is required, they
22926 are instantiated right away. That allows for better code in the
22927 case that an expression refers to the value of the variable --
22928 if the variable has a constant value the referring expression can
22929 take advantage of that fact. */
22930 if (VAR_P (d))
22931 defer_ok = false;
22933 /* Don't instantiate cloned functions. Instead, instantiate the
22934 functions they cloned. */
22935 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22936 d = DECL_CLONED_FUNCTION (d);
22938 if (DECL_TEMPLATE_INSTANTIATED (d)
22939 || (TREE_CODE (d) == FUNCTION_DECL
22940 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22941 || DECL_TEMPLATE_SPECIALIZATION (d))
22942 /* D has already been instantiated or explicitly specialized, so
22943 there's nothing for us to do here.
22945 It might seem reasonable to check whether or not D is an explicit
22946 instantiation, and, if so, stop here. But when an explicit
22947 instantiation is deferred until the end of the compilation,
22948 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22949 the instantiation. */
22950 return d;
22952 /* Check to see whether we know that this template will be
22953 instantiated in some other file, as with "extern template"
22954 extension. */
22955 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22957 /* In general, we do not instantiate such templates. */
22958 if (external_p && !always_instantiate_p (d))
22959 return d;
22961 gen_tmpl = most_general_template (tmpl);
22962 gen_args = DECL_TI_ARGS (d);
22964 if (tmpl != gen_tmpl)
22965 /* We should already have the extra args. */
22966 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22967 == TMPL_ARGS_DEPTH (gen_args));
22968 /* And what's in the hash table should match D. */
22969 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22970 || spec == NULL_TREE);
22972 /* This needs to happen before any tsubsting. */
22973 if (! push_tinst_level (d))
22974 return d;
22976 timevar_push (TV_TEMPLATE_INST);
22978 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22979 for the instantiation. */
22980 td = template_for_substitution (d);
22981 args = gen_args;
22983 if (VAR_P (d))
22985 /* Look up an explicit specialization, if any. */
22986 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22987 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22988 if (elt && elt != error_mark_node)
22990 td = TREE_VALUE (elt);
22991 args = TREE_PURPOSE (elt);
22995 code_pattern = DECL_TEMPLATE_RESULT (td);
22997 /* We should never be trying to instantiate a member of a class
22998 template or partial specialization. */
22999 gcc_assert (d != code_pattern);
23001 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23002 || DECL_TEMPLATE_SPECIALIZATION (td))
23003 /* In the case of a friend template whose definition is provided
23004 outside the class, we may have too many arguments. Drop the
23005 ones we don't need. The same is true for specializations. */
23006 args = get_innermost_template_args
23007 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23009 if (TREE_CODE (d) == FUNCTION_DECL)
23011 deleted_p = DECL_DELETED_FN (code_pattern);
23012 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23013 && DECL_INITIAL (code_pattern) != error_mark_node)
23014 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23015 || deleted_p);
23017 else
23019 deleted_p = false;
23020 if (DECL_CLASS_SCOPE_P (code_pattern))
23021 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23022 || DECL_INLINE_VAR_P (code_pattern));
23023 else
23024 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23027 /* We may be in the middle of deferred access check. Disable it now. */
23028 push_deferring_access_checks (dk_no_deferred);
23030 /* Unless an explicit instantiation directive has already determined
23031 the linkage of D, remember that a definition is available for
23032 this entity. */
23033 if (pattern_defined
23034 && !DECL_INTERFACE_KNOWN (d)
23035 && !DECL_NOT_REALLY_EXTERN (d))
23036 mark_definable (d);
23038 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23039 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23040 input_location = DECL_SOURCE_LOCATION (d);
23042 /* If D is a member of an explicitly instantiated class template,
23043 and no definition is available, treat it like an implicit
23044 instantiation. */
23045 if (!pattern_defined && expl_inst_class_mem_p
23046 && DECL_EXPLICIT_INSTANTIATION (d))
23048 /* Leave linkage flags alone on instantiations with anonymous
23049 visibility. */
23050 if (TREE_PUBLIC (d))
23052 DECL_NOT_REALLY_EXTERN (d) = 0;
23053 DECL_INTERFACE_KNOWN (d) = 0;
23055 SET_DECL_IMPLICIT_INSTANTIATION (d);
23058 /* Defer all other templates, unless we have been explicitly
23059 forbidden from doing so. */
23060 if (/* If there is no definition, we cannot instantiate the
23061 template. */
23062 ! pattern_defined
23063 /* If it's OK to postpone instantiation, do so. */
23064 || defer_ok
23065 /* If this is a static data member that will be defined
23066 elsewhere, we don't want to instantiate the entire data
23067 member, but we do want to instantiate the initializer so that
23068 we can substitute that elsewhere. */
23069 || (external_p && VAR_P (d))
23070 /* Handle here a deleted function too, avoid generating
23071 its body (c++/61080). */
23072 || deleted_p)
23074 /* The definition of the static data member is now required so
23075 we must substitute the initializer. */
23076 if (VAR_P (d)
23077 && !DECL_INITIAL (d)
23078 && DECL_INITIAL (code_pattern))
23080 tree ns;
23081 tree init;
23082 bool const_init = false;
23083 bool enter_context = DECL_CLASS_SCOPE_P (d);
23085 ns = decl_namespace_context (d);
23086 push_nested_namespace (ns);
23087 if (enter_context)
23088 push_nested_class (DECL_CONTEXT (d));
23089 init = tsubst_expr (DECL_INITIAL (code_pattern),
23090 args,
23091 tf_warning_or_error, NULL_TREE,
23092 /*integral_constant_expression_p=*/false);
23093 /* If instantiating the initializer involved instantiating this
23094 again, don't call cp_finish_decl twice. */
23095 if (!DECL_INITIAL (d))
23097 /* Make sure the initializer is still constant, in case of
23098 circular dependency (template/instantiate6.C). */
23099 const_init
23100 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23101 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23102 /*asmspec_tree=*/NULL_TREE,
23103 LOOKUP_ONLYCONVERTING);
23105 if (enter_context)
23106 pop_nested_class ();
23107 pop_nested_namespace (ns);
23110 /* We restore the source position here because it's used by
23111 add_pending_template. */
23112 input_location = saved_loc;
23114 if (at_eof && !pattern_defined
23115 && DECL_EXPLICIT_INSTANTIATION (d)
23116 && DECL_NOT_REALLY_EXTERN (d))
23117 /* [temp.explicit]
23119 The definition of a non-exported function template, a
23120 non-exported member function template, or a non-exported
23121 member function or static data member of a class template
23122 shall be present in every translation unit in which it is
23123 explicitly instantiated. */
23124 permerror (input_location, "explicit instantiation of %qD "
23125 "but no definition available", d);
23127 /* If we're in unevaluated context, we just wanted to get the
23128 constant value; this isn't an odr use, so don't queue
23129 a full instantiation. */
23130 if (cp_unevaluated_operand != 0)
23131 goto out;
23132 /* ??? Historically, we have instantiated inline functions, even
23133 when marked as "extern template". */
23134 if (!(external_p && VAR_P (d)))
23135 add_pending_template (d);
23136 goto out;
23138 /* Tell the repository that D is available in this translation unit
23139 -- and see if it is supposed to be instantiated here. */
23140 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23142 /* In a PCH file, despite the fact that the repository hasn't
23143 requested instantiation in the PCH it is still possible that
23144 an instantiation will be required in a file that includes the
23145 PCH. */
23146 if (pch_file)
23147 add_pending_template (d);
23148 /* Instantiate inline functions so that the inliner can do its
23149 job, even though we'll not be emitting a copy of this
23150 function. */
23151 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23152 goto out;
23155 bool push_to_top, nested;
23156 tree fn_context;
23157 fn_context = decl_function_context (d);
23158 nested = current_function_decl != NULL_TREE;
23159 push_to_top = !(nested && fn_context == current_function_decl);
23161 vec<tree> omp_privatization_save;
23162 if (nested)
23163 save_omp_privatization_clauses (omp_privatization_save);
23165 if (push_to_top)
23166 push_to_top_level ();
23167 else
23169 push_function_context ();
23170 cp_unevaluated_operand = 0;
23171 c_inhibit_evaluation_warnings = 0;
23174 /* Mark D as instantiated so that recursive calls to
23175 instantiate_decl do not try to instantiate it again. */
23176 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23178 /* Regenerate the declaration in case the template has been modified
23179 by a subsequent redeclaration. */
23180 regenerate_decl_from_template (d, td, args);
23182 /* We already set the file and line above. Reset them now in case
23183 they changed as a result of calling regenerate_decl_from_template. */
23184 input_location = DECL_SOURCE_LOCATION (d);
23186 if (VAR_P (d))
23188 tree init;
23189 bool const_init = false;
23191 /* Clear out DECL_RTL; whatever was there before may not be right
23192 since we've reset the type of the declaration. */
23193 SET_DECL_RTL (d, NULL);
23194 DECL_IN_AGGR_P (d) = 0;
23196 /* The initializer is placed in DECL_INITIAL by
23197 regenerate_decl_from_template so we don't need to
23198 push/pop_access_scope again here. Pull it out so that
23199 cp_finish_decl can process it. */
23200 init = DECL_INITIAL (d);
23201 DECL_INITIAL (d) = NULL_TREE;
23202 DECL_INITIALIZED_P (d) = 0;
23204 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23205 initializer. That function will defer actual emission until
23206 we have a chance to determine linkage. */
23207 DECL_EXTERNAL (d) = 0;
23209 /* Enter the scope of D so that access-checking works correctly. */
23210 bool enter_context = DECL_CLASS_SCOPE_P (d);
23211 if (enter_context)
23212 push_nested_class (DECL_CONTEXT (d));
23214 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23215 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23217 if (enter_context)
23218 pop_nested_class ();
23220 if (variable_template_p (gen_tmpl))
23221 note_variable_template_instantiation (d);
23223 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23224 synthesize_method (d);
23225 else if (TREE_CODE (d) == FUNCTION_DECL)
23227 /* Set up the list of local specializations. */
23228 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23229 tree block = NULL_TREE;
23231 /* Set up context. */
23232 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23233 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23234 block = push_stmt_list ();
23235 else
23236 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23238 /* Some typedefs referenced from within the template code need to be
23239 access checked at template instantiation time, i.e now. These
23240 types were added to the template at parsing time. Let's get those
23241 and perform the access checks then. */
23242 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23243 args);
23245 /* Create substitution entries for the parameters. */
23246 register_parameter_specializations (code_pattern, d);
23248 /* Substitute into the body of the function. */
23249 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23250 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23251 tf_warning_or_error, tmpl);
23252 else
23254 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23255 tf_warning_or_error, tmpl,
23256 /*integral_constant_expression_p=*/false);
23258 /* Set the current input_location to the end of the function
23259 so that finish_function knows where we are. */
23260 input_location
23261 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23263 /* Remember if we saw an infinite loop in the template. */
23264 current_function_infinite_loop
23265 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23268 /* Finish the function. */
23269 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23270 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23271 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23272 else
23274 d = finish_function (/*inline_p=*/false);
23275 expand_or_defer_fn (d);
23278 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23279 cp_check_omp_declare_reduction (d);
23282 /* We're not deferring instantiation any more. */
23283 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23285 if (push_to_top)
23286 pop_from_top_level ();
23287 else
23288 pop_function_context ();
23290 if (nested)
23291 restore_omp_privatization_clauses (omp_privatization_save);
23293 out:
23294 pop_deferring_access_checks ();
23295 timevar_pop (TV_TEMPLATE_INST);
23296 pop_tinst_level ();
23297 input_location = saved_loc;
23298 cp_unevaluated_operand = saved_unevaluated_operand;
23299 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23301 return d;
23304 /* Run through the list of templates that we wish we could
23305 instantiate, and instantiate any we can. RETRIES is the
23306 number of times we retry pending template instantiation. */
23308 void
23309 instantiate_pending_templates (int retries)
23311 int reconsider;
23312 location_t saved_loc = input_location;
23314 /* Instantiating templates may trigger vtable generation. This in turn
23315 may require further template instantiations. We place a limit here
23316 to avoid infinite loop. */
23317 if (pending_templates && retries >= max_tinst_depth)
23319 tree decl = pending_templates->tinst->decl;
23321 fatal_error (input_location,
23322 "template instantiation depth exceeds maximum of %d"
23323 " instantiating %q+D, possibly from virtual table generation"
23324 " (use -ftemplate-depth= to increase the maximum)",
23325 max_tinst_depth, decl);
23326 if (TREE_CODE (decl) == FUNCTION_DECL)
23327 /* Pretend that we defined it. */
23328 DECL_INITIAL (decl) = error_mark_node;
23329 return;
23334 struct pending_template **t = &pending_templates;
23335 struct pending_template *last = NULL;
23336 reconsider = 0;
23337 while (*t)
23339 tree instantiation = reopen_tinst_level ((*t)->tinst);
23340 bool complete = false;
23342 if (TYPE_P (instantiation))
23344 if (!COMPLETE_TYPE_P (instantiation))
23346 instantiate_class_template (instantiation);
23347 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23348 for (tree fld = TYPE_FIELDS (instantiation);
23349 fld; fld = TREE_CHAIN (fld))
23350 if ((VAR_P (fld)
23351 || (TREE_CODE (fld) == FUNCTION_DECL
23352 && !DECL_ARTIFICIAL (fld)))
23353 && DECL_TEMPLATE_INSTANTIATION (fld))
23354 instantiate_decl (fld,
23355 /*defer_ok=*/false,
23356 /*expl_inst_class_mem_p=*/false);
23358 if (COMPLETE_TYPE_P (instantiation))
23359 reconsider = 1;
23362 complete = COMPLETE_TYPE_P (instantiation);
23364 else
23366 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23367 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23369 instantiation
23370 = instantiate_decl (instantiation,
23371 /*defer_ok=*/false,
23372 /*expl_inst_class_mem_p=*/false);
23373 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23374 reconsider = 1;
23377 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23378 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23381 if (complete)
23382 /* If INSTANTIATION has been instantiated, then we don't
23383 need to consider it again in the future. */
23384 *t = (*t)->next;
23385 else
23387 last = *t;
23388 t = &(*t)->next;
23390 tinst_depth = 0;
23391 current_tinst_level = NULL;
23393 last_pending_template = last;
23395 while (reconsider);
23397 input_location = saved_loc;
23400 /* Substitute ARGVEC into T, which is a list of initializers for
23401 either base class or a non-static data member. The TREE_PURPOSEs
23402 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23403 instantiate_decl. */
23405 static tree
23406 tsubst_initializer_list (tree t, tree argvec)
23408 tree inits = NULL_TREE;
23410 for (; t; t = TREE_CHAIN (t))
23412 tree decl;
23413 tree init;
23414 tree expanded_bases = NULL_TREE;
23415 tree expanded_arguments = NULL_TREE;
23416 int i, len = 1;
23418 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23420 tree expr;
23421 tree arg;
23423 /* Expand the base class expansion type into separate base
23424 classes. */
23425 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23426 tf_warning_or_error,
23427 NULL_TREE);
23428 if (expanded_bases == error_mark_node)
23429 continue;
23431 /* We'll be building separate TREE_LISTs of arguments for
23432 each base. */
23433 len = TREE_VEC_LENGTH (expanded_bases);
23434 expanded_arguments = make_tree_vec (len);
23435 for (i = 0; i < len; i++)
23436 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23438 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23439 expand each argument in the TREE_VALUE of t. */
23440 expr = make_node (EXPR_PACK_EXPANSION);
23441 PACK_EXPANSION_LOCAL_P (expr) = true;
23442 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23443 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23445 if (TREE_VALUE (t) == void_type_node)
23446 /* VOID_TYPE_NODE is used to indicate
23447 value-initialization. */
23449 for (i = 0; i < len; i++)
23450 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23452 else
23454 /* Substitute parameter packs into each argument in the
23455 TREE_LIST. */
23456 in_base_initializer = 1;
23457 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23459 tree expanded_exprs;
23461 /* Expand the argument. */
23462 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23463 expanded_exprs
23464 = tsubst_pack_expansion (expr, argvec,
23465 tf_warning_or_error,
23466 NULL_TREE);
23467 if (expanded_exprs == error_mark_node)
23468 continue;
23470 /* Prepend each of the expanded expressions to the
23471 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23472 for (i = 0; i < len; i++)
23474 TREE_VEC_ELT (expanded_arguments, i) =
23475 tree_cons (NULL_TREE,
23476 TREE_VEC_ELT (expanded_exprs, i),
23477 TREE_VEC_ELT (expanded_arguments, i));
23480 in_base_initializer = 0;
23482 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23483 since we built them backwards. */
23484 for (i = 0; i < len; i++)
23486 TREE_VEC_ELT (expanded_arguments, i) =
23487 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23492 for (i = 0; i < len; ++i)
23494 if (expanded_bases)
23496 decl = TREE_VEC_ELT (expanded_bases, i);
23497 decl = expand_member_init (decl);
23498 init = TREE_VEC_ELT (expanded_arguments, i);
23500 else
23502 tree tmp;
23503 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23504 tf_warning_or_error, NULL_TREE);
23506 decl = expand_member_init (decl);
23507 if (decl && !DECL_P (decl))
23508 in_base_initializer = 1;
23510 init = TREE_VALUE (t);
23511 tmp = init;
23512 if (init != void_type_node)
23513 init = tsubst_expr (init, argvec,
23514 tf_warning_or_error, NULL_TREE,
23515 /*integral_constant_expression_p=*/false);
23516 if (init == NULL_TREE && tmp != NULL_TREE)
23517 /* If we had an initializer but it instantiated to nothing,
23518 value-initialize the object. This will only occur when
23519 the initializer was a pack expansion where the parameter
23520 packs used in that expansion were of length zero. */
23521 init = void_type_node;
23522 in_base_initializer = 0;
23525 if (decl)
23527 init = build_tree_list (decl, init);
23528 TREE_CHAIN (init) = inits;
23529 inits = init;
23533 return inits;
23536 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23538 static void
23539 set_current_access_from_decl (tree decl)
23541 if (TREE_PRIVATE (decl))
23542 current_access_specifier = access_private_node;
23543 else if (TREE_PROTECTED (decl))
23544 current_access_specifier = access_protected_node;
23545 else
23546 current_access_specifier = access_public_node;
23549 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23550 is the instantiation (which should have been created with
23551 start_enum) and ARGS are the template arguments to use. */
23553 static void
23554 tsubst_enum (tree tag, tree newtag, tree args)
23556 tree e;
23558 if (SCOPED_ENUM_P (newtag))
23559 begin_scope (sk_scoped_enum, newtag);
23561 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23563 tree value;
23564 tree decl;
23566 decl = TREE_VALUE (e);
23567 /* Note that in a template enum, the TREE_VALUE is the
23568 CONST_DECL, not the corresponding INTEGER_CST. */
23569 value = tsubst_expr (DECL_INITIAL (decl),
23570 args, tf_warning_or_error, NULL_TREE,
23571 /*integral_constant_expression_p=*/true);
23573 /* Give this enumeration constant the correct access. */
23574 set_current_access_from_decl (decl);
23576 /* Actually build the enumerator itself. Here we're assuming that
23577 enumerators can't have dependent attributes. */
23578 build_enumerator (DECL_NAME (decl), value, newtag,
23579 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23582 if (SCOPED_ENUM_P (newtag))
23583 finish_scope ();
23585 finish_enum_value_list (newtag);
23586 finish_enum (newtag);
23588 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23589 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23592 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23593 its type -- but without substituting the innermost set of template
23594 arguments. So, innermost set of template parameters will appear in
23595 the type. */
23597 tree
23598 get_mostly_instantiated_function_type (tree decl)
23600 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23601 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23604 /* Return truthvalue if we're processing a template different from
23605 the last one involved in diagnostics. */
23606 bool
23607 problematic_instantiation_changed (void)
23609 return current_tinst_level != last_error_tinst_level;
23612 /* Remember current template involved in diagnostics. */
23613 void
23614 record_last_problematic_instantiation (void)
23616 last_error_tinst_level = current_tinst_level;
23619 struct tinst_level *
23620 current_instantiation (void)
23622 return current_tinst_level;
23625 /* Return TRUE if current_function_decl is being instantiated, false
23626 otherwise. */
23628 bool
23629 instantiating_current_function_p (void)
23631 return (current_instantiation ()
23632 && current_instantiation ()->decl == current_function_decl);
23635 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23636 type. Return zero for ok, nonzero for disallowed. Issue error and
23637 warning messages under control of COMPLAIN. */
23639 static int
23640 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23642 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23643 return 0;
23644 else if (POINTER_TYPE_P (type))
23645 return 0;
23646 else if (TYPE_PTRMEM_P (type))
23647 return 0;
23648 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23649 return 0;
23650 else if (TREE_CODE (type) == TYPENAME_TYPE)
23651 return 0;
23652 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23653 return 0;
23654 else if (TREE_CODE (type) == NULLPTR_TYPE)
23655 return 0;
23656 /* A bound template template parm could later be instantiated to have a valid
23657 nontype parm type via an alias template. */
23658 else if (cxx_dialect >= cxx11
23659 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23660 return 0;
23662 if (complain & tf_error)
23664 if (type == error_mark_node)
23665 inform (input_location, "invalid template non-type parameter");
23666 else
23667 error ("%q#T is not a valid type for a template non-type parameter",
23668 type);
23670 return 1;
23673 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23674 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23676 static bool
23677 dependent_type_p_r (tree type)
23679 tree scope;
23681 /* [temp.dep.type]
23683 A type is dependent if it is:
23685 -- a template parameter. Template template parameters are types
23686 for us (since TYPE_P holds true for them) so we handle
23687 them here. */
23688 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23689 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23690 return true;
23691 /* -- a qualified-id with a nested-name-specifier which contains a
23692 class-name that names a dependent type or whose unqualified-id
23693 names a dependent type. */
23694 if (TREE_CODE (type) == TYPENAME_TYPE)
23695 return true;
23697 /* An alias template specialization can be dependent even if the
23698 resulting type is not. */
23699 if (dependent_alias_template_spec_p (type))
23700 return true;
23702 /* -- a cv-qualified type where the cv-unqualified type is
23703 dependent.
23704 No code is necessary for this bullet; the code below handles
23705 cv-qualified types, and we don't want to strip aliases with
23706 TYPE_MAIN_VARIANT because of DR 1558. */
23707 /* -- a compound type constructed from any dependent type. */
23708 if (TYPE_PTRMEM_P (type))
23709 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23710 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23711 (type)));
23712 else if (TYPE_PTR_P (type)
23713 || TREE_CODE (type) == REFERENCE_TYPE)
23714 return dependent_type_p (TREE_TYPE (type));
23715 else if (TREE_CODE (type) == FUNCTION_TYPE
23716 || TREE_CODE (type) == METHOD_TYPE)
23718 tree arg_type;
23720 if (dependent_type_p (TREE_TYPE (type)))
23721 return true;
23722 for (arg_type = TYPE_ARG_TYPES (type);
23723 arg_type;
23724 arg_type = TREE_CHAIN (arg_type))
23725 if (dependent_type_p (TREE_VALUE (arg_type)))
23726 return true;
23727 if (cxx_dialect >= cxx17)
23729 /* A value-dependent noexcept-specifier makes the type dependent. */
23730 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23731 if (spec && TREE_PURPOSE (spec)
23732 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23733 return true;
23735 return false;
23737 /* -- an array type constructed from any dependent type or whose
23738 size is specified by a constant expression that is
23739 value-dependent.
23741 We checked for type- and value-dependence of the bounds in
23742 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23743 if (TREE_CODE (type) == ARRAY_TYPE)
23745 if (TYPE_DOMAIN (type)
23746 && dependent_type_p (TYPE_DOMAIN (type)))
23747 return true;
23748 return dependent_type_p (TREE_TYPE (type));
23751 /* -- a template-id in which either the template name is a template
23752 parameter ... */
23753 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23754 return true;
23755 /* ... or any of the template arguments is a dependent type or
23756 an expression that is type-dependent or value-dependent. */
23757 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23758 && (any_dependent_template_arguments_p
23759 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23760 return true;
23762 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23763 dependent; if the argument of the `typeof' expression is not
23764 type-dependent, then it should already been have resolved. */
23765 if (TREE_CODE (type) == TYPEOF_TYPE
23766 || TREE_CODE (type) == DECLTYPE_TYPE
23767 || TREE_CODE (type) == UNDERLYING_TYPE)
23768 return true;
23770 /* A template argument pack is dependent if any of its packed
23771 arguments are. */
23772 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23774 tree args = ARGUMENT_PACK_ARGS (type);
23775 int i, len = TREE_VEC_LENGTH (args);
23776 for (i = 0; i < len; ++i)
23777 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23778 return true;
23781 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23782 be template parameters. */
23783 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23784 return true;
23786 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23787 return true;
23789 /* The standard does not specifically mention types that are local
23790 to template functions or local classes, but they should be
23791 considered dependent too. For example:
23793 template <int I> void f() {
23794 enum E { a = I };
23795 S<sizeof (E)> s;
23798 The size of `E' cannot be known until the value of `I' has been
23799 determined. Therefore, `E' must be considered dependent. */
23800 scope = TYPE_CONTEXT (type);
23801 if (scope && TYPE_P (scope))
23802 return dependent_type_p (scope);
23803 /* Don't use type_dependent_expression_p here, as it can lead
23804 to infinite recursion trying to determine whether a lambda
23805 nested in a lambda is dependent (c++/47687). */
23806 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23807 && DECL_LANG_SPECIFIC (scope)
23808 && DECL_TEMPLATE_INFO (scope)
23809 && (any_dependent_template_arguments_p
23810 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23811 return true;
23813 /* Other types are non-dependent. */
23814 return false;
23817 /* Returns TRUE if TYPE is dependent, in the sense of
23818 [temp.dep.type]. Note that a NULL type is considered dependent. */
23820 bool
23821 dependent_type_p (tree type)
23823 /* If there are no template parameters in scope, then there can't be
23824 any dependent types. */
23825 if (!processing_template_decl)
23827 /* If we are not processing a template, then nobody should be
23828 providing us with a dependent type. */
23829 gcc_assert (type);
23830 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23831 return false;
23834 /* If the type is NULL, we have not computed a type for the entity
23835 in question; in that case, the type is dependent. */
23836 if (!type)
23837 return true;
23839 /* Erroneous types can be considered non-dependent. */
23840 if (type == error_mark_node)
23841 return false;
23843 /* Getting here with global_type_node means we improperly called this
23844 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23845 gcc_checking_assert (type != global_type_node);
23847 /* If we have not already computed the appropriate value for TYPE,
23848 do so now. */
23849 if (!TYPE_DEPENDENT_P_VALID (type))
23851 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23852 TYPE_DEPENDENT_P_VALID (type) = 1;
23855 return TYPE_DEPENDENT_P (type);
23858 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23859 lookup. In other words, a dependent type that is not the current
23860 instantiation. */
23862 bool
23863 dependent_scope_p (tree scope)
23865 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23866 && !currently_open_class (scope));
23869 /* T is a SCOPE_REF; return whether we need to consider it
23870 instantiation-dependent so that we can check access at instantiation
23871 time even though we know which member it resolves to. */
23873 static bool
23874 instantiation_dependent_scope_ref_p (tree t)
23876 if (DECL_P (TREE_OPERAND (t, 1))
23877 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23878 && accessible_in_template_p (TREE_OPERAND (t, 0),
23879 TREE_OPERAND (t, 1)))
23880 return false;
23881 else
23882 return true;
23885 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23886 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23887 expression. */
23889 /* Note that this predicate is not appropriate for general expressions;
23890 only constant expressions (that satisfy potential_constant_expression)
23891 can be tested for value dependence. */
23893 bool
23894 value_dependent_expression_p (tree expression)
23896 if (!processing_template_decl || expression == NULL_TREE)
23897 return false;
23899 /* A name declared with a dependent type. */
23900 if (DECL_P (expression) && type_dependent_expression_p (expression))
23901 return true;
23903 switch (TREE_CODE (expression))
23905 case BASELINK:
23906 /* A dependent member function of the current instantiation. */
23907 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23909 case FUNCTION_DECL:
23910 /* A dependent member function of the current instantiation. */
23911 if (DECL_CLASS_SCOPE_P (expression)
23912 && dependent_type_p (DECL_CONTEXT (expression)))
23913 return true;
23914 break;
23916 case IDENTIFIER_NODE:
23917 /* A name that has not been looked up -- must be dependent. */
23918 return true;
23920 case TEMPLATE_PARM_INDEX:
23921 /* A non-type template parm. */
23922 return true;
23924 case CONST_DECL:
23925 /* A non-type template parm. */
23926 if (DECL_TEMPLATE_PARM_P (expression))
23927 return true;
23928 return value_dependent_expression_p (DECL_INITIAL (expression));
23930 case VAR_DECL:
23931 /* A constant with literal type and is initialized
23932 with an expression that is value-dependent.
23934 Note that a non-dependent parenthesized initializer will have
23935 already been replaced with its constant value, so if we see
23936 a TREE_LIST it must be dependent. */
23937 if (DECL_INITIAL (expression)
23938 && decl_constant_var_p (expression)
23939 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23940 /* cp_finish_decl doesn't fold reference initializers. */
23941 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23942 || type_dependent_expression_p (DECL_INITIAL (expression))
23943 || value_dependent_expression_p (DECL_INITIAL (expression))))
23944 return true;
23945 if (DECL_HAS_VALUE_EXPR_P (expression))
23947 tree value_expr = DECL_VALUE_EXPR (expression);
23948 if (type_dependent_expression_p (value_expr))
23949 return true;
23951 return false;
23953 case DYNAMIC_CAST_EXPR:
23954 case STATIC_CAST_EXPR:
23955 case CONST_CAST_EXPR:
23956 case REINTERPRET_CAST_EXPR:
23957 case CAST_EXPR:
23958 /* These expressions are value-dependent if the type to which
23959 the cast occurs is dependent or the expression being casted
23960 is value-dependent. */
23962 tree type = TREE_TYPE (expression);
23964 if (dependent_type_p (type))
23965 return true;
23967 /* A functional cast has a list of operands. */
23968 expression = TREE_OPERAND (expression, 0);
23969 if (!expression)
23971 /* If there are no operands, it must be an expression such
23972 as "int()". This should not happen for aggregate types
23973 because it would form non-constant expressions. */
23974 gcc_assert (cxx_dialect >= cxx11
23975 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23977 return false;
23980 if (TREE_CODE (expression) == TREE_LIST)
23981 return any_value_dependent_elements_p (expression);
23983 return value_dependent_expression_p (expression);
23986 case SIZEOF_EXPR:
23987 if (SIZEOF_EXPR_TYPE_P (expression))
23988 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23989 /* FALLTHRU */
23990 case ALIGNOF_EXPR:
23991 case TYPEID_EXPR:
23992 /* A `sizeof' expression is value-dependent if the operand is
23993 type-dependent or is a pack expansion. */
23994 expression = TREE_OPERAND (expression, 0);
23995 if (PACK_EXPANSION_P (expression))
23996 return true;
23997 else if (TYPE_P (expression))
23998 return dependent_type_p (expression);
23999 return instantiation_dependent_uneval_expression_p (expression);
24001 case AT_ENCODE_EXPR:
24002 /* An 'encode' expression is value-dependent if the operand is
24003 type-dependent. */
24004 expression = TREE_OPERAND (expression, 0);
24005 return dependent_type_p (expression);
24007 case NOEXCEPT_EXPR:
24008 expression = TREE_OPERAND (expression, 0);
24009 return instantiation_dependent_uneval_expression_p (expression);
24011 case SCOPE_REF:
24012 /* All instantiation-dependent expressions should also be considered
24013 value-dependent. */
24014 return instantiation_dependent_scope_ref_p (expression);
24016 case COMPONENT_REF:
24017 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24018 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24020 case NONTYPE_ARGUMENT_PACK:
24021 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24022 is value-dependent. */
24024 tree values = ARGUMENT_PACK_ARGS (expression);
24025 int i, len = TREE_VEC_LENGTH (values);
24027 for (i = 0; i < len; ++i)
24028 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24029 return true;
24031 return false;
24034 case TRAIT_EXPR:
24036 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24037 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
24038 || (type2 ? dependent_type_p (type2) : false));
24041 case MODOP_EXPR:
24042 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24043 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24045 case ARRAY_REF:
24046 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24047 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24049 case ADDR_EXPR:
24051 tree op = TREE_OPERAND (expression, 0);
24052 return (value_dependent_expression_p (op)
24053 || has_value_dependent_address (op));
24056 case REQUIRES_EXPR:
24057 /* Treat all requires-expressions as value-dependent so
24058 we don't try to fold them. */
24059 return true;
24061 case TYPE_REQ:
24062 return dependent_type_p (TREE_OPERAND (expression, 0));
24064 case CALL_EXPR:
24066 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24067 return true;
24068 tree fn = get_callee_fndecl (expression);
24069 int i, nargs;
24070 nargs = call_expr_nargs (expression);
24071 for (i = 0; i < nargs; ++i)
24073 tree op = CALL_EXPR_ARG (expression, i);
24074 /* In a call to a constexpr member function, look through the
24075 implicit ADDR_EXPR on the object argument so that it doesn't
24076 cause the call to be considered value-dependent. We also
24077 look through it in potential_constant_expression. */
24078 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24079 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24080 && TREE_CODE (op) == ADDR_EXPR)
24081 op = TREE_OPERAND (op, 0);
24082 if (value_dependent_expression_p (op))
24083 return true;
24085 return false;
24088 case TEMPLATE_ID_EXPR:
24089 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
24090 type-dependent. */
24091 return type_dependent_expression_p (expression)
24092 || variable_concept_p (TREE_OPERAND (expression, 0));
24094 case CONSTRUCTOR:
24096 unsigned ix;
24097 tree val;
24098 if (dependent_type_p (TREE_TYPE (expression)))
24099 return true;
24100 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24101 if (value_dependent_expression_p (val))
24102 return true;
24103 return false;
24106 case STMT_EXPR:
24107 /* Treat a GNU statement expression as dependent to avoid crashing
24108 under instantiate_non_dependent_expr; it can't be constant. */
24109 return true;
24111 default:
24112 /* A constant expression is value-dependent if any subexpression is
24113 value-dependent. */
24114 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24116 case tcc_reference:
24117 case tcc_unary:
24118 case tcc_comparison:
24119 case tcc_binary:
24120 case tcc_expression:
24121 case tcc_vl_exp:
24123 int i, len = cp_tree_operand_length (expression);
24125 for (i = 0; i < len; i++)
24127 tree t = TREE_OPERAND (expression, i);
24129 /* In some cases, some of the operands may be missing.
24130 (For example, in the case of PREDECREMENT_EXPR, the
24131 amount to increment by may be missing.) That doesn't
24132 make the expression dependent. */
24133 if (t && value_dependent_expression_p (t))
24134 return true;
24137 break;
24138 default:
24139 break;
24141 break;
24144 /* The expression is not value-dependent. */
24145 return false;
24148 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24149 [temp.dep.expr]. Note that an expression with no type is
24150 considered dependent. Other parts of the compiler arrange for an
24151 expression with type-dependent subexpressions to have no type, so
24152 this function doesn't have to be fully recursive. */
24154 bool
24155 type_dependent_expression_p (tree expression)
24157 if (!processing_template_decl)
24158 return false;
24160 if (expression == NULL_TREE || expression == error_mark_node)
24161 return false;
24163 /* An unresolved name is always dependent. */
24164 if (identifier_p (expression)
24165 || TREE_CODE (expression) == USING_DECL
24166 || TREE_CODE (expression) == WILDCARD_DECL)
24167 return true;
24169 /* A fold expression is type-dependent. */
24170 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24171 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24172 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24173 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24174 return true;
24176 /* Some expression forms are never type-dependent. */
24177 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24178 || TREE_CODE (expression) == SIZEOF_EXPR
24179 || TREE_CODE (expression) == ALIGNOF_EXPR
24180 || TREE_CODE (expression) == AT_ENCODE_EXPR
24181 || TREE_CODE (expression) == NOEXCEPT_EXPR
24182 || TREE_CODE (expression) == TRAIT_EXPR
24183 || TREE_CODE (expression) == TYPEID_EXPR
24184 || TREE_CODE (expression) == DELETE_EXPR
24185 || TREE_CODE (expression) == VEC_DELETE_EXPR
24186 || TREE_CODE (expression) == THROW_EXPR
24187 || TREE_CODE (expression) == REQUIRES_EXPR)
24188 return false;
24190 /* The types of these expressions depends only on the type to which
24191 the cast occurs. */
24192 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24193 || TREE_CODE (expression) == STATIC_CAST_EXPR
24194 || TREE_CODE (expression) == CONST_CAST_EXPR
24195 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24196 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24197 || TREE_CODE (expression) == CAST_EXPR)
24198 return dependent_type_p (TREE_TYPE (expression));
24200 /* The types of these expressions depends only on the type created
24201 by the expression. */
24202 if (TREE_CODE (expression) == NEW_EXPR
24203 || TREE_CODE (expression) == VEC_NEW_EXPR)
24205 /* For NEW_EXPR tree nodes created inside a template, either
24206 the object type itself or a TREE_LIST may appear as the
24207 operand 1. */
24208 tree type = TREE_OPERAND (expression, 1);
24209 if (TREE_CODE (type) == TREE_LIST)
24210 /* This is an array type. We need to check array dimensions
24211 as well. */
24212 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24213 || value_dependent_expression_p
24214 (TREE_OPERAND (TREE_VALUE (type), 1));
24215 else
24216 return dependent_type_p (type);
24219 if (TREE_CODE (expression) == SCOPE_REF)
24221 tree scope = TREE_OPERAND (expression, 0);
24222 tree name = TREE_OPERAND (expression, 1);
24224 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24225 contains an identifier associated by name lookup with one or more
24226 declarations declared with a dependent type, or...a
24227 nested-name-specifier or qualified-id that names a member of an
24228 unknown specialization. */
24229 return (type_dependent_expression_p (name)
24230 || dependent_scope_p (scope));
24233 if (TREE_CODE (expression) == TEMPLATE_DECL
24234 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24235 return uses_outer_template_parms (expression);
24237 if (TREE_CODE (expression) == STMT_EXPR)
24238 expression = stmt_expr_value_expr (expression);
24240 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24242 tree elt;
24243 unsigned i;
24245 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24247 if (type_dependent_expression_p (elt))
24248 return true;
24250 return false;
24253 /* A static data member of the current instantiation with incomplete
24254 array type is type-dependent, as the definition and specializations
24255 can have different bounds. */
24256 if (VAR_P (expression)
24257 && DECL_CLASS_SCOPE_P (expression)
24258 && dependent_type_p (DECL_CONTEXT (expression))
24259 && VAR_HAD_UNKNOWN_BOUND (expression))
24260 return true;
24262 /* An array of unknown bound depending on a variadic parameter, eg:
24264 template<typename... Args>
24265 void foo (Args... args)
24267 int arr[] = { args... };
24270 template<int... vals>
24271 void bar ()
24273 int arr[] = { vals... };
24276 If the array has no length and has an initializer, it must be that
24277 we couldn't determine its length in cp_complete_array_type because
24278 it is dependent. */
24279 if (VAR_P (expression)
24280 && TREE_TYPE (expression) != NULL_TREE
24281 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24282 && !TYPE_DOMAIN (TREE_TYPE (expression))
24283 && DECL_INITIAL (expression))
24284 return true;
24286 /* A function or variable template-id is type-dependent if it has any
24287 dependent template arguments. */
24288 if (VAR_OR_FUNCTION_DECL_P (expression)
24289 && DECL_LANG_SPECIFIC (expression)
24290 && DECL_TEMPLATE_INFO (expression))
24292 /* Consider the innermost template arguments, since those are the ones
24293 that come from the template-id; the template arguments for the
24294 enclosing class do not make it type-dependent unless they are used in
24295 the type of the decl. */
24296 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24297 && (any_dependent_template_arguments_p
24298 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24299 return true;
24302 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24303 type-dependent. Checking this is important for functions with auto return
24304 type, which looks like a dependent type. */
24305 if (TREE_CODE (expression) == FUNCTION_DECL
24306 && !(DECL_CLASS_SCOPE_P (expression)
24307 && dependent_type_p (DECL_CONTEXT (expression)))
24308 && !(DECL_FRIEND_P (expression)
24309 && (!DECL_FRIEND_CONTEXT (expression)
24310 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24311 && !DECL_LOCAL_FUNCTION_P (expression))
24313 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24314 || undeduced_auto_decl (expression));
24315 return false;
24318 /* Always dependent, on the number of arguments if nothing else. */
24319 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24320 return true;
24322 if (TREE_TYPE (expression) == unknown_type_node)
24324 if (TREE_CODE (expression) == ADDR_EXPR)
24325 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24326 if (TREE_CODE (expression) == COMPONENT_REF
24327 || TREE_CODE (expression) == OFFSET_REF)
24329 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24330 return true;
24331 expression = TREE_OPERAND (expression, 1);
24332 if (identifier_p (expression))
24333 return false;
24335 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24336 if (TREE_CODE (expression) == SCOPE_REF)
24337 return false;
24339 if (BASELINK_P (expression))
24341 if (BASELINK_OPTYPE (expression)
24342 && dependent_type_p (BASELINK_OPTYPE (expression)))
24343 return true;
24344 expression = BASELINK_FUNCTIONS (expression);
24347 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24349 if (any_dependent_template_arguments_p
24350 (TREE_OPERAND (expression, 1)))
24351 return true;
24352 expression = TREE_OPERAND (expression, 0);
24353 if (identifier_p (expression))
24354 return true;
24357 gcc_assert (TREE_CODE (expression) == OVERLOAD
24358 || TREE_CODE (expression) == FUNCTION_DECL);
24360 for (lkp_iterator iter (expression); iter; ++iter)
24361 if (type_dependent_expression_p (*iter))
24362 return true;
24364 return false;
24367 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24369 /* Dependent type attributes might not have made it from the decl to
24370 the type yet. */
24371 if (DECL_P (expression)
24372 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24373 return true;
24375 return (dependent_type_p (TREE_TYPE (expression)));
24378 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24379 type-dependent if the expression refers to a member of the current
24380 instantiation and the type of the referenced member is dependent, or the
24381 class member access expression refers to a member of an unknown
24382 specialization.
24384 This function returns true if the OBJECT in such a class member access
24385 expression is of an unknown specialization. */
24387 bool
24388 type_dependent_object_expression_p (tree object)
24390 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24391 dependent. */
24392 if (TREE_CODE (object) == IDENTIFIER_NODE)
24393 return true;
24394 tree scope = TREE_TYPE (object);
24395 return (!scope || dependent_scope_p (scope));
24398 /* walk_tree callback function for instantiation_dependent_expression_p,
24399 below. Returns non-zero if a dependent subexpression is found. */
24401 static tree
24402 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24403 void * /*data*/)
24405 if (TYPE_P (*tp))
24407 /* We don't have to worry about decltype currently because decltype
24408 of an instantiation-dependent expr is a dependent type. This
24409 might change depending on the resolution of DR 1172. */
24410 *walk_subtrees = false;
24411 return NULL_TREE;
24413 enum tree_code code = TREE_CODE (*tp);
24414 switch (code)
24416 /* Don't treat an argument list as dependent just because it has no
24417 TREE_TYPE. */
24418 case TREE_LIST:
24419 case TREE_VEC:
24420 return NULL_TREE;
24422 case TEMPLATE_PARM_INDEX:
24423 return *tp;
24425 /* Handle expressions with type operands. */
24426 case SIZEOF_EXPR:
24427 case ALIGNOF_EXPR:
24428 case TYPEID_EXPR:
24429 case AT_ENCODE_EXPR:
24431 tree op = TREE_OPERAND (*tp, 0);
24432 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24433 op = TREE_TYPE (op);
24434 if (TYPE_P (op))
24436 if (dependent_type_p (op))
24437 return *tp;
24438 else
24440 *walk_subtrees = false;
24441 return NULL_TREE;
24444 break;
24447 case COMPONENT_REF:
24448 if (identifier_p (TREE_OPERAND (*tp, 1)))
24449 /* In a template, finish_class_member_access_expr creates a
24450 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24451 type-dependent, so that we can check access control at
24452 instantiation time (PR 42277). See also Core issue 1273. */
24453 return *tp;
24454 break;
24456 case SCOPE_REF:
24457 if (instantiation_dependent_scope_ref_p (*tp))
24458 return *tp;
24459 else
24460 break;
24462 /* Treat statement-expressions as dependent. */
24463 case BIND_EXPR:
24464 return *tp;
24466 /* Treat requires-expressions as dependent. */
24467 case REQUIRES_EXPR:
24468 return *tp;
24470 case CALL_EXPR:
24471 /* Treat calls to function concepts as dependent. */
24472 if (function_concept_check_p (*tp))
24473 return *tp;
24474 break;
24476 case TEMPLATE_ID_EXPR:
24477 /* And variable concepts. */
24478 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24479 return *tp;
24480 break;
24482 default:
24483 break;
24486 if (type_dependent_expression_p (*tp))
24487 return *tp;
24488 else
24489 return NULL_TREE;
24492 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24493 sense defined by the ABI:
24495 "An expression is instantiation-dependent if it is type-dependent
24496 or value-dependent, or it has a subexpression that is type-dependent
24497 or value-dependent."
24499 Except don't actually check value-dependence for unevaluated expressions,
24500 because in sizeof(i) we don't care about the value of i. Checking
24501 type-dependence will in turn check value-dependence of array bounds/template
24502 arguments as needed. */
24504 bool
24505 instantiation_dependent_uneval_expression_p (tree expression)
24507 tree result;
24509 if (!processing_template_decl)
24510 return false;
24512 if (expression == error_mark_node)
24513 return false;
24515 result = cp_walk_tree_without_duplicates (&expression,
24516 instantiation_dependent_r, NULL);
24517 return result != NULL_TREE;
24520 /* As above, but also check value-dependence of the expression as a whole. */
24522 bool
24523 instantiation_dependent_expression_p (tree expression)
24525 return (instantiation_dependent_uneval_expression_p (expression)
24526 || value_dependent_expression_p (expression));
24529 /* Like type_dependent_expression_p, but it also works while not processing
24530 a template definition, i.e. during substitution or mangling. */
24532 bool
24533 type_dependent_expression_p_push (tree expr)
24535 bool b;
24536 ++processing_template_decl;
24537 b = type_dependent_expression_p (expr);
24538 --processing_template_decl;
24539 return b;
24542 /* Returns TRUE if ARGS contains a type-dependent expression. */
24544 bool
24545 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24547 unsigned int i;
24548 tree arg;
24550 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24552 if (type_dependent_expression_p (arg))
24553 return true;
24555 return false;
24558 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24559 expressions) contains any type-dependent expressions. */
24561 bool
24562 any_type_dependent_elements_p (const_tree list)
24564 for (; list; list = TREE_CHAIN (list))
24565 if (type_dependent_expression_p (TREE_VALUE (list)))
24566 return true;
24568 return false;
24571 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24572 expressions) contains any value-dependent expressions. */
24574 bool
24575 any_value_dependent_elements_p (const_tree list)
24577 for (; list; list = TREE_CHAIN (list))
24578 if (value_dependent_expression_p (TREE_VALUE (list)))
24579 return true;
24581 return false;
24584 /* Returns TRUE if the ARG (a template argument) is dependent. */
24586 bool
24587 dependent_template_arg_p (tree arg)
24589 if (!processing_template_decl)
24590 return false;
24592 /* Assume a template argument that was wrongly written by the user
24593 is dependent. This is consistent with what
24594 any_dependent_template_arguments_p [that calls this function]
24595 does. */
24596 if (!arg || arg == error_mark_node)
24597 return true;
24599 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24600 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24602 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24603 return true;
24604 if (TREE_CODE (arg) == TEMPLATE_DECL)
24606 if (DECL_TEMPLATE_PARM_P (arg))
24607 return true;
24608 /* A member template of a dependent class is not necessarily
24609 type-dependent, but it is a dependent template argument because it
24610 will be a member of an unknown specialization to that template. */
24611 tree scope = CP_DECL_CONTEXT (arg);
24612 return TYPE_P (scope) && dependent_type_p (scope);
24614 else if (ARGUMENT_PACK_P (arg))
24616 tree args = ARGUMENT_PACK_ARGS (arg);
24617 int i, len = TREE_VEC_LENGTH (args);
24618 for (i = 0; i < len; ++i)
24620 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24621 return true;
24624 return false;
24626 else if (TYPE_P (arg))
24627 return dependent_type_p (arg);
24628 else
24629 return (type_dependent_expression_p (arg)
24630 || value_dependent_expression_p (arg));
24633 /* Returns true if ARGS (a collection of template arguments) contains
24634 any types that require structural equality testing. */
24636 bool
24637 any_template_arguments_need_structural_equality_p (tree args)
24639 int i;
24640 int j;
24642 if (!args)
24643 return false;
24644 if (args == error_mark_node)
24645 return true;
24647 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24649 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24650 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24652 tree arg = TREE_VEC_ELT (level, j);
24653 tree packed_args = NULL_TREE;
24654 int k, len = 1;
24656 if (ARGUMENT_PACK_P (arg))
24658 /* Look inside the argument pack. */
24659 packed_args = ARGUMENT_PACK_ARGS (arg);
24660 len = TREE_VEC_LENGTH (packed_args);
24663 for (k = 0; k < len; ++k)
24665 if (packed_args)
24666 arg = TREE_VEC_ELT (packed_args, k);
24668 if (error_operand_p (arg))
24669 return true;
24670 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24671 continue;
24672 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24673 return true;
24674 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24675 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24676 return true;
24681 return false;
24684 /* Returns true if ARGS (a collection of template arguments) contains
24685 any dependent arguments. */
24687 bool
24688 any_dependent_template_arguments_p (const_tree args)
24690 int i;
24691 int j;
24693 if (!args)
24694 return false;
24695 if (args == error_mark_node)
24696 return true;
24698 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24700 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24701 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24702 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24703 return true;
24706 return false;
24709 /* Returns TRUE if the template TMPL is type-dependent. */
24711 bool
24712 dependent_template_p (tree tmpl)
24714 if (TREE_CODE (tmpl) == OVERLOAD)
24716 for (lkp_iterator iter (tmpl); iter; ++iter)
24717 if (dependent_template_p (*iter))
24718 return true;
24719 return false;
24722 /* Template template parameters are dependent. */
24723 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24724 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24725 return true;
24726 /* So are names that have not been looked up. */
24727 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24728 return true;
24729 return false;
24732 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24734 bool
24735 dependent_template_id_p (tree tmpl, tree args)
24737 return (dependent_template_p (tmpl)
24738 || any_dependent_template_arguments_p (args));
24741 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24742 are dependent. */
24744 bool
24745 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24747 int i;
24749 if (!processing_template_decl)
24750 return false;
24752 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24754 tree decl = TREE_VEC_ELT (declv, i);
24755 tree init = TREE_VEC_ELT (initv, i);
24756 tree cond = TREE_VEC_ELT (condv, i);
24757 tree incr = TREE_VEC_ELT (incrv, i);
24759 if (type_dependent_expression_p (decl)
24760 || TREE_CODE (decl) == SCOPE_REF)
24761 return true;
24763 if (init && type_dependent_expression_p (init))
24764 return true;
24766 if (type_dependent_expression_p (cond))
24767 return true;
24769 if (COMPARISON_CLASS_P (cond)
24770 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24771 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24772 return true;
24774 if (TREE_CODE (incr) == MODOP_EXPR)
24776 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24777 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24778 return true;
24780 else if (type_dependent_expression_p (incr))
24781 return true;
24782 else if (TREE_CODE (incr) == MODIFY_EXPR)
24784 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24785 return true;
24786 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24788 tree t = TREE_OPERAND (incr, 1);
24789 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24790 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24791 return true;
24796 return false;
24799 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24800 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24801 no such TYPE can be found. Note that this function peers inside
24802 uninstantiated templates and therefore should be used only in
24803 extremely limited situations. ONLY_CURRENT_P restricts this
24804 peering to the currently open classes hierarchy (which is required
24805 when comparing types). */
24807 tree
24808 resolve_typename_type (tree type, bool only_current_p)
24810 tree scope;
24811 tree name;
24812 tree decl;
24813 int quals;
24814 tree pushed_scope;
24815 tree result;
24817 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24819 scope = TYPE_CONTEXT (type);
24820 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24821 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24822 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24823 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24824 identifier of the TYPENAME_TYPE anymore.
24825 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24826 TYPENAME_TYPE instead, we avoid messing up with a possible
24827 typedef variant case. */
24828 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24830 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24831 it first before we can figure out what NAME refers to. */
24832 if (TREE_CODE (scope) == TYPENAME_TYPE)
24834 if (TYPENAME_IS_RESOLVING_P (scope))
24835 /* Given a class template A with a dependent base with nested type C,
24836 typedef typename A::C::C C will land us here, as trying to resolve
24837 the initial A::C leads to the local C typedef, which leads back to
24838 A::C::C. So we break the recursion now. */
24839 return type;
24840 else
24841 scope = resolve_typename_type (scope, only_current_p);
24843 /* If we don't know what SCOPE refers to, then we cannot resolve the
24844 TYPENAME_TYPE. */
24845 if (!CLASS_TYPE_P (scope))
24846 return type;
24847 /* If this is a typedef, we don't want to look inside (c++/11987). */
24848 if (typedef_variant_p (type))
24849 return type;
24850 /* If SCOPE isn't the template itself, it will not have a valid
24851 TYPE_FIELDS list. */
24852 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24853 /* scope is either the template itself or a compatible instantiation
24854 like X<T>, so look up the name in the original template. */
24855 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24856 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24857 gcc_checking_assert (uses_template_parms (scope));
24858 /* If scope has no fields, it can't be a current instantiation. Check this
24859 before currently_open_class to avoid infinite recursion (71515). */
24860 if (!TYPE_FIELDS (scope))
24861 return type;
24862 /* If the SCOPE is not the current instantiation, there's no reason
24863 to look inside it. */
24864 if (only_current_p && !currently_open_class (scope))
24865 return type;
24866 /* Enter the SCOPE so that name lookup will be resolved as if we
24867 were in the class definition. In particular, SCOPE will no
24868 longer be considered a dependent type. */
24869 pushed_scope = push_scope (scope);
24870 /* Look up the declaration. */
24871 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24872 tf_warning_or_error);
24874 result = NULL_TREE;
24876 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24877 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24878 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24879 if (!decl)
24880 /*nop*/;
24881 else if (identifier_p (fullname)
24882 && TREE_CODE (decl) == TYPE_DECL)
24884 result = TREE_TYPE (decl);
24885 if (result == error_mark_node)
24886 result = NULL_TREE;
24888 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24889 && DECL_CLASS_TEMPLATE_P (decl))
24891 /* Obtain the template and the arguments. */
24892 tree tmpl = TREE_OPERAND (fullname, 0);
24893 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24895 /* We get here with a plain identifier because a previous tentative
24896 parse of the nested-name-specifier as part of a ptr-operator saw
24897 ::template X<A>. The use of ::template is necessary in a
24898 ptr-operator, but wrong in a declarator-id.
24900 [temp.names]: In a qualified-id of a declarator-id, the keyword
24901 template shall not appear at the top level. */
24902 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24903 "keyword %<template%> not allowed in declarator-id");
24904 tmpl = decl;
24906 tree args = TREE_OPERAND (fullname, 1);
24907 /* Instantiate the template. */
24908 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24909 /*entering_scope=*/true,
24910 tf_error | tf_user);
24911 if (result == error_mark_node)
24912 result = NULL_TREE;
24915 /* Leave the SCOPE. */
24916 if (pushed_scope)
24917 pop_scope (pushed_scope);
24919 /* If we failed to resolve it, return the original typename. */
24920 if (!result)
24921 return type;
24923 /* If lookup found a typename type, resolve that too. */
24924 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24926 /* Ill-formed programs can cause infinite recursion here, so we
24927 must catch that. */
24928 TYPENAME_IS_RESOLVING_P (result) = 1;
24929 result = resolve_typename_type (result, only_current_p);
24930 TYPENAME_IS_RESOLVING_P (result) = 0;
24933 /* Qualify the resulting type. */
24934 quals = cp_type_quals (type);
24935 if (quals)
24936 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24938 return result;
24941 /* EXPR is an expression which is not type-dependent. Return a proxy
24942 for EXPR that can be used to compute the types of larger
24943 expressions containing EXPR. */
24945 tree
24946 build_non_dependent_expr (tree expr)
24948 tree inner_expr;
24950 /* When checking, try to get a constant value for all non-dependent
24951 expressions in order to expose bugs in *_dependent_expression_p
24952 and constexpr. This can affect code generation, see PR70704, so
24953 only do this for -fchecking=2. */
24954 if (flag_checking > 1
24955 && cxx_dialect >= cxx11
24956 /* Don't do this during nsdmi parsing as it can lead to
24957 unexpected recursive instantiations. */
24958 && !parsing_nsdmi ()
24959 /* Don't do this during concept expansion either and for
24960 the same reason. */
24961 && !expanding_concept ())
24962 fold_non_dependent_expr (expr);
24964 /* Preserve OVERLOADs; the functions must be available to resolve
24965 types. */
24966 inner_expr = expr;
24967 if (TREE_CODE (inner_expr) == STMT_EXPR)
24968 inner_expr = stmt_expr_value_expr (inner_expr);
24969 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24970 inner_expr = TREE_OPERAND (inner_expr, 0);
24971 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24972 inner_expr = TREE_OPERAND (inner_expr, 1);
24973 if (is_overloaded_fn (inner_expr)
24974 || TREE_CODE (inner_expr) == OFFSET_REF)
24975 return expr;
24976 /* There is no need to return a proxy for a variable. */
24977 if (VAR_P (expr))
24978 return expr;
24979 /* Preserve string constants; conversions from string constants to
24980 "char *" are allowed, even though normally a "const char *"
24981 cannot be used to initialize a "char *". */
24982 if (TREE_CODE (expr) == STRING_CST)
24983 return expr;
24984 /* Preserve void and arithmetic constants, as an optimization -- there is no
24985 reason to create a new node. */
24986 if (TREE_CODE (expr) == VOID_CST
24987 || TREE_CODE (expr) == INTEGER_CST
24988 || TREE_CODE (expr) == REAL_CST)
24989 return expr;
24990 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24991 There is at least one place where we want to know that a
24992 particular expression is a throw-expression: when checking a ?:
24993 expression, there are special rules if the second or third
24994 argument is a throw-expression. */
24995 if (TREE_CODE (expr) == THROW_EXPR)
24996 return expr;
24998 /* Don't wrap an initializer list, we need to be able to look inside. */
24999 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25000 return expr;
25002 /* Don't wrap a dummy object, we need to be able to test for it. */
25003 if (is_dummy_object (expr))
25004 return expr;
25006 if (TREE_CODE (expr) == COND_EXPR)
25007 return build3 (COND_EXPR,
25008 TREE_TYPE (expr),
25009 TREE_OPERAND (expr, 0),
25010 (TREE_OPERAND (expr, 1)
25011 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25012 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25013 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25014 if (TREE_CODE (expr) == COMPOUND_EXPR
25015 && !COMPOUND_EXPR_OVERLOADED (expr))
25016 return build2 (COMPOUND_EXPR,
25017 TREE_TYPE (expr),
25018 TREE_OPERAND (expr, 0),
25019 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25021 /* If the type is unknown, it can't really be non-dependent */
25022 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25024 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25025 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25028 /* ARGS is a vector of expressions as arguments to a function call.
25029 Replace the arguments with equivalent non-dependent expressions.
25030 This modifies ARGS in place. */
25032 void
25033 make_args_non_dependent (vec<tree, va_gc> *args)
25035 unsigned int ix;
25036 tree arg;
25038 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25040 tree newarg = build_non_dependent_expr (arg);
25041 if (newarg != arg)
25042 (*args)[ix] = newarg;
25046 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25047 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25048 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25050 static tree
25051 make_auto_1 (tree name, bool set_canonical)
25053 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25054 TYPE_NAME (au) = build_decl (input_location,
25055 TYPE_DECL, name, au);
25056 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25057 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25058 (0, processing_template_decl + 1, processing_template_decl + 1,
25059 TYPE_NAME (au), NULL_TREE);
25060 if (set_canonical)
25061 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25062 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25063 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25065 return au;
25068 tree
25069 make_decltype_auto (void)
25071 return make_auto_1 (decltype_auto_identifier, true);
25074 tree
25075 make_auto (void)
25077 return make_auto_1 (auto_identifier, true);
25080 /* Return a C++17 deduction placeholder for class template TMPL. */
25082 tree
25083 make_template_placeholder (tree tmpl)
25085 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25086 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25087 return t;
25090 /* True iff T is a C++17 class template deduction placeholder. */
25092 bool
25093 template_placeholder_p (tree t)
25095 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25098 /* Make a "constrained auto" type-specifier. This is an
25099 auto type with constraints that must be associated after
25100 deduction. The constraint is formed from the given
25101 CONC and its optional sequence of arguments, which are
25102 non-null if written as partial-concept-id. */
25104 tree
25105 make_constrained_auto (tree con, tree args)
25107 tree type = make_auto_1 (auto_identifier, false);
25109 /* Build the constraint. */
25110 tree tmpl = DECL_TI_TEMPLATE (con);
25111 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25112 expr = build_concept_check (expr, type, args);
25114 tree constr = normalize_expression (expr);
25115 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25117 /* Our canonical type depends on the constraint. */
25118 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25120 /* Attach the constraint to the type declaration. */
25121 tree decl = TYPE_NAME (type);
25122 return decl;
25125 /* Given type ARG, return std::initializer_list<ARG>. */
25127 static tree
25128 listify (tree arg)
25130 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25132 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25134 gcc_rich_location richloc (input_location);
25135 maybe_add_include_fixit (&richloc, "<initializer_list>");
25136 error_at_rich_loc (&richloc,
25137 "deducing from brace-enclosed initializer list"
25138 " requires #include <initializer_list>");
25140 return error_mark_node;
25142 tree argvec = make_tree_vec (1);
25143 TREE_VEC_ELT (argvec, 0) = arg;
25145 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25146 NULL_TREE, 0, tf_warning_or_error);
25149 /* Replace auto in TYPE with std::initializer_list<auto>. */
25151 static tree
25152 listify_autos (tree type, tree auto_node)
25154 tree init_auto = listify (auto_node);
25155 tree argvec = make_tree_vec (1);
25156 TREE_VEC_ELT (argvec, 0) = init_auto;
25157 if (processing_template_decl)
25158 argvec = add_to_template_args (current_template_args (), argvec);
25159 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25162 /* Hash traits for hashing possibly constrained 'auto'
25163 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25165 struct auto_hash : default_hash_traits<tree>
25167 static inline hashval_t hash (tree);
25168 static inline bool equal (tree, tree);
25171 /* Hash the 'auto' T. */
25173 inline hashval_t
25174 auto_hash::hash (tree t)
25176 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25177 /* Matching constrained-type-specifiers denote the same template
25178 parameter, so hash the constraint. */
25179 return hash_placeholder_constraint (c);
25180 else
25181 /* But unconstrained autos are all separate, so just hash the pointer. */
25182 return iterative_hash_object (t, 0);
25185 /* Compare two 'auto's. */
25187 inline bool
25188 auto_hash::equal (tree t1, tree t2)
25190 if (t1 == t2)
25191 return true;
25193 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25194 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25196 /* Two unconstrained autos are distinct. */
25197 if (!c1 || !c2)
25198 return false;
25200 return equivalent_placeholder_constraints (c1, c2);
25203 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25204 constrained) auto, add it to the vector. */
25206 static int
25207 extract_autos_r (tree t, void *data)
25209 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25210 if (is_auto (t))
25212 /* All the autos were built with index 0; fix that up now. */
25213 tree *p = hash.find_slot (t, INSERT);
25214 unsigned idx;
25215 if (*p)
25216 /* If this is a repeated constrained-type-specifier, use the index we
25217 chose before. */
25218 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25219 else
25221 /* Otherwise this is new, so use the current count. */
25222 *p = t;
25223 idx = hash.elements () - 1;
25225 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25228 /* Always keep walking. */
25229 return 0;
25232 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25233 says they can appear anywhere in the type. */
25235 static tree
25236 extract_autos (tree type)
25238 hash_set<tree> visited;
25239 hash_table<auto_hash> hash (2);
25241 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25243 tree tree_vec = make_tree_vec (hash.elements());
25244 for (hash_table<auto_hash>::iterator iter = hash.begin();
25245 iter != hash.end(); ++iter)
25247 tree elt = *iter;
25248 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25249 TREE_VEC_ELT (tree_vec, i)
25250 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25253 return tree_vec;
25256 /* The stem for deduction guide names. */
25257 const char *const dguide_base = "__dguide_";
25259 /* Return the name for a deduction guide for class template TMPL. */
25261 tree
25262 dguide_name (tree tmpl)
25264 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25265 tree tname = TYPE_IDENTIFIER (type);
25266 char *buf = (char *) alloca (1 + strlen (dguide_base)
25267 + IDENTIFIER_LENGTH (tname));
25268 memcpy (buf, dguide_base, strlen (dguide_base));
25269 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25270 IDENTIFIER_LENGTH (tname) + 1);
25271 tree dname = get_identifier (buf);
25272 TREE_TYPE (dname) = type;
25273 return dname;
25276 /* True if NAME is the name of a deduction guide. */
25278 bool
25279 dguide_name_p (tree name)
25281 return (TREE_TYPE (name)
25282 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25283 strlen (dguide_base)));
25286 /* True if FN is a deduction guide. */
25288 bool
25289 deduction_guide_p (const_tree fn)
25291 if (DECL_P (fn))
25292 if (tree name = DECL_NAME (fn))
25293 return dguide_name_p (name);
25294 return false;
25297 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25299 bool
25300 copy_guide_p (const_tree fn)
25302 gcc_assert (deduction_guide_p (fn));
25303 if (!DECL_ARTIFICIAL (fn))
25304 return false;
25305 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25306 return (TREE_CHAIN (parms) == void_list_node
25307 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25310 /* True if FN is a guide generated from a constructor template. */
25312 bool
25313 template_guide_p (const_tree fn)
25315 gcc_assert (deduction_guide_p (fn));
25316 if (!DECL_ARTIFICIAL (fn))
25317 return false;
25318 tree tmpl = DECL_TI_TEMPLATE (fn);
25319 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25320 return PRIMARY_TEMPLATE_P (org);
25321 return false;
25324 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25325 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25326 template parameter types. Note that the handling of template template
25327 parameters relies on current_template_parms being set appropriately for the
25328 new template. */
25330 static tree
25331 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25332 tree tsubst_args, tsubst_flags_t complain)
25334 tree oldidx = get_template_parm_index (olddecl);
25336 tree newtype;
25337 if (TREE_CODE (olddecl) == TYPE_DECL
25338 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25340 tree oldtype = TREE_TYPE (olddecl);
25341 newtype = cxx_make_type (TREE_CODE (oldtype));
25342 TYPE_MAIN_VARIANT (newtype) = newtype;
25343 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25344 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25345 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25347 else
25348 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25349 complain, NULL_TREE);
25351 tree newdecl
25352 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25353 DECL_NAME (olddecl), newtype);
25354 SET_DECL_TEMPLATE_PARM_P (newdecl);
25356 tree newidx;
25357 if (TREE_CODE (olddecl) == TYPE_DECL
25358 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25360 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25361 = build_template_parm_index (index, level, level,
25362 newdecl, newtype);
25363 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25364 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25365 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25366 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25368 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25370 DECL_TEMPLATE_RESULT (newdecl)
25371 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25372 DECL_NAME (olddecl), newtype);
25373 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25374 // First create a copy (ttargs) of tsubst_args with an
25375 // additional level for the template template parameter's own
25376 // template parameters (ttparms).
25377 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25378 (DECL_TEMPLATE_PARMS (olddecl)));
25379 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25380 tree ttargs = make_tree_vec (depth + 1);
25381 for (int i = 0; i < depth; ++i)
25382 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25383 TREE_VEC_ELT (ttargs, depth)
25384 = template_parms_level_to_args (ttparms);
25385 // Substitute ttargs into ttparms to fix references to
25386 // other template parameters.
25387 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25388 complain);
25389 // Now substitute again with args based on tparms, to reduce
25390 // the level of the ttparms.
25391 ttargs = current_template_args ();
25392 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25393 complain);
25394 // Finally, tack the adjusted parms onto tparms.
25395 ttparms = tree_cons (size_int (depth), ttparms,
25396 current_template_parms);
25397 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25400 else
25402 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25403 tree newconst
25404 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25405 TREE_CODE (oldconst),
25406 DECL_NAME (oldconst), newtype);
25407 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25408 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25409 SET_DECL_TEMPLATE_PARM_P (newconst);
25410 newidx = build_template_parm_index (index, level, level,
25411 newconst, newtype);
25412 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25413 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25414 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25417 return newdecl;
25420 /* Returns a C++17 class deduction guide template based on the constructor
25421 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25422 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25424 static tree
25425 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25427 tree type, tparms, targs, fparms, fargs, ci;
25428 bool memtmpl = false;
25429 bool explicit_p;
25430 location_t loc;
25431 tree fn_tmpl = NULL_TREE;
25433 if (TYPE_P (ctor))
25435 type = ctor;
25436 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25437 if (copy_p)
25439 type = TREE_TYPE (type);
25440 fparms = tree_cons (NULL_TREE, type, void_list_node);
25442 else
25443 fparms = void_list_node;
25445 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25446 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25447 targs = CLASSTYPE_TI_ARGS (type);
25448 ci = NULL_TREE;
25449 fargs = NULL_TREE;
25450 loc = DECL_SOURCE_LOCATION (ctmpl);
25451 explicit_p = false;
25453 else
25455 ++processing_template_decl;
25457 fn_tmpl
25458 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25459 : DECL_TI_TEMPLATE (ctor));
25460 if (outer_args)
25461 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25462 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25464 type = DECL_CONTEXT (ctor);
25466 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25467 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25468 fully specialized args for the enclosing class. Strip those off, as
25469 the deduction guide won't have those template parameters. */
25470 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25471 TMPL_PARMS_DEPTH (tparms));
25472 /* Discard the 'this' parameter. */
25473 fparms = FUNCTION_ARG_CHAIN (ctor);
25474 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25475 ci = get_constraints (ctor);
25476 loc = DECL_SOURCE_LOCATION (ctor);
25477 explicit_p = DECL_NONCONVERTING_P (ctor);
25479 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25481 memtmpl = true;
25483 /* For a member template constructor, we need to flatten the two
25484 template parameter lists into one, and then adjust the function
25485 signature accordingly. This gets...complicated. */
25486 tree save_parms = current_template_parms;
25488 /* For a member template we should have two levels of parms/args, one
25489 for the class and one for the constructor. We stripped
25490 specialized args for further enclosing classes above. */
25491 const int depth = 2;
25492 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25494 /* Template args for translating references to the two-level template
25495 parameters into references to the one-level template parameters we
25496 are creating. */
25497 tree tsubst_args = copy_node (targs);
25498 TMPL_ARGS_LEVEL (tsubst_args, depth)
25499 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25501 /* Template parms for the constructor template. */
25502 tree ftparms = TREE_VALUE (tparms);
25503 unsigned flen = TREE_VEC_LENGTH (ftparms);
25504 /* Template parms for the class template. */
25505 tparms = TREE_CHAIN (tparms);
25506 tree ctparms = TREE_VALUE (tparms);
25507 unsigned clen = TREE_VEC_LENGTH (ctparms);
25508 /* Template parms for the deduction guide start as a copy of the
25509 template parms for the class. We set current_template_parms for
25510 lookup_template_class_1. */
25511 current_template_parms = tparms = copy_node (tparms);
25512 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25513 for (unsigned i = 0; i < clen; ++i)
25514 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25516 /* Now we need to rewrite the constructor parms to append them to the
25517 class parms. */
25518 for (unsigned i = 0; i < flen; ++i)
25520 unsigned index = i + clen;
25521 unsigned level = 1;
25522 tree oldelt = TREE_VEC_ELT (ftparms, i);
25523 tree olddecl = TREE_VALUE (oldelt);
25524 tree newdecl = rewrite_template_parm (olddecl, index, level,
25525 tsubst_args, complain);
25526 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25527 tsubst_args, complain, ctor);
25528 tree list = build_tree_list (newdef, newdecl);
25529 TEMPLATE_PARM_CONSTRAINTS (list)
25530 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25531 tsubst_args, complain, ctor);
25532 TREE_VEC_ELT (new_vec, index) = list;
25533 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25536 /* Now we have a final set of template parms to substitute into the
25537 function signature. */
25538 targs = template_parms_to_args (tparms);
25539 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25540 complain, ctor);
25541 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25542 if (ci)
25543 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25545 current_template_parms = save_parms;
25547 --processing_template_decl;
25550 if (!memtmpl)
25552 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25553 tparms = copy_node (tparms);
25554 INNERMOST_TEMPLATE_PARMS (tparms)
25555 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25558 tree fntype = build_function_type (type, fparms);
25559 tree ded_fn = build_lang_decl_loc (loc,
25560 FUNCTION_DECL,
25561 dguide_name (type), fntype);
25562 DECL_ARGUMENTS (ded_fn) = fargs;
25563 DECL_ARTIFICIAL (ded_fn) = true;
25564 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25565 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25566 DECL_ARTIFICIAL (ded_tmpl) = true;
25567 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25568 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25569 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25570 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25571 if (DECL_P (ctor))
25572 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25573 if (ci)
25574 set_constraints (ded_tmpl, ci);
25576 return ded_tmpl;
25579 /* Deduce template arguments for the class template placeholder PTYPE for
25580 template TMPL based on the initializer INIT, and return the resulting
25581 type. */
25583 static tree
25584 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25585 tsubst_flags_t complain)
25587 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25589 /* We should have handled this in the caller. */
25590 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25591 return ptype;
25592 if (complain & tf_error)
25593 error ("non-class template %qT used without template arguments", tmpl);
25594 return error_mark_node;
25597 tree type = TREE_TYPE (tmpl);
25599 bool try_list_ctor = false;
25601 vec<tree,va_gc> *args;
25602 if (init == NULL_TREE
25603 || TREE_CODE (init) == TREE_LIST)
25604 args = make_tree_vector_from_list (init);
25605 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25607 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25608 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25610 /* As an exception, the first phase in 16.3.1.7 (considering the
25611 initializer list as a single argument) is omitted if the
25612 initializer list consists of a single expression of type cv U,
25613 where U is a specialization of C or a class derived from a
25614 specialization of C. */
25615 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25616 tree etype = TREE_TYPE (elt);
25618 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25619 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25620 int err = unify (tparms, targs, type, etype,
25621 UNIFY_ALLOW_DERIVED, /*explain*/false);
25622 if (err == 0)
25623 try_list_ctor = false;
25624 ggc_free (targs);
25626 if (try_list_ctor || is_std_init_list (type))
25627 args = make_tree_vector_single (init);
25628 else
25629 args = make_tree_vector_from_ctor (init);
25631 else
25632 args = make_tree_vector_single (init);
25634 tree dname = dguide_name (tmpl);
25635 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25636 /*type*/false, /*complain*/false,
25637 /*hidden*/false);
25638 bool elided = false;
25639 if (cands == error_mark_node)
25640 cands = NULL_TREE;
25642 /* Prune explicit deduction guides in copy-initialization context. */
25643 if (flags & LOOKUP_ONLYCONVERTING)
25645 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25646 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25647 elided = true;
25649 if (elided)
25651 /* Found a nonconverting guide, prune the candidates. */
25652 tree pruned = NULL_TREE;
25653 for (lkp_iterator iter (cands); iter; ++iter)
25654 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25655 pruned = lookup_add (*iter, pruned);
25657 cands = pruned;
25661 tree outer_args = NULL_TREE;
25662 if (DECL_CLASS_SCOPE_P (tmpl)
25663 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25665 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25666 type = TREE_TYPE (most_general_template (tmpl));
25669 bool saw_ctor = false;
25670 // FIXME cache artificial deduction guides
25671 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25673 tree guide = build_deduction_guide (*iter, outer_args, complain);
25674 if ((flags & LOOKUP_ONLYCONVERTING)
25675 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25676 elided = true;
25677 else
25678 cands = lookup_add (guide, cands);
25680 saw_ctor = true;
25683 tree call = error_mark_node;
25685 /* If this is list-initialization and the class has a list constructor, first
25686 try deducing from the list as a single argument, as [over.match.list]. */
25687 tree list_cands = NULL_TREE;
25688 if (try_list_ctor && cands)
25689 for (lkp_iterator iter (cands); iter; ++iter)
25691 tree dg = *iter;
25692 if (is_list_ctor (dg))
25693 list_cands = lookup_add (dg, list_cands);
25695 if (list_cands)
25697 ++cp_unevaluated_operand;
25698 call = build_new_function_call (list_cands, &args, tf_decltype);
25699 --cp_unevaluated_operand;
25701 if (call == error_mark_node)
25703 /* That didn't work, now try treating the list as a sequence of
25704 arguments. */
25705 release_tree_vector (args);
25706 args = make_tree_vector_from_ctor (init);
25710 /* Maybe generate an implicit deduction guide. */
25711 if (call == error_mark_node && args->length () < 2)
25713 tree gtype = NULL_TREE;
25715 if (args->length () == 1)
25716 /* Generate a copy guide. */
25717 gtype = build_reference_type (type);
25718 else if (!saw_ctor)
25719 /* Generate a default guide. */
25720 gtype = type;
25722 if (gtype)
25724 tree guide = build_deduction_guide (gtype, outer_args, complain);
25725 cands = lookup_add (guide, cands);
25729 if (elided && !cands)
25731 error ("cannot deduce template arguments for copy-initialization"
25732 " of %qT, as it has no non-explicit deduction guides or "
25733 "user-declared constructors", type);
25734 return error_mark_node;
25736 else if (!cands && call == error_mark_node)
25738 error ("cannot deduce template arguments of %qT, as it has no viable "
25739 "deduction guides", type);
25740 return error_mark_node;
25743 if (call == error_mark_node)
25745 ++cp_unevaluated_operand;
25746 call = build_new_function_call (cands, &args, tf_decltype);
25747 --cp_unevaluated_operand;
25750 if (call == error_mark_node && (complain & tf_warning_or_error))
25752 error ("class template argument deduction failed:");
25754 ++cp_unevaluated_operand;
25755 call = build_new_function_call (cands, &args, complain | tf_decltype);
25756 --cp_unevaluated_operand;
25758 if (elided)
25759 inform (input_location, "explicit deduction guides not considered "
25760 "for copy-initialization");
25763 release_tree_vector (args);
25765 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25768 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25769 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25771 tree
25772 do_auto_deduction (tree type, tree init, tree auto_node)
25774 return do_auto_deduction (type, init, auto_node,
25775 tf_warning_or_error,
25776 adc_unspecified);
25779 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25780 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25781 The CONTEXT determines the context in which auto deduction is performed
25782 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25783 OUTER_TARGS are used during template argument deduction
25784 (context == adc_unify) to properly substitute the result, and is ignored
25785 in other contexts.
25787 For partial-concept-ids, extra args may be appended to the list of deduced
25788 template arguments prior to determining constraint satisfaction. */
25790 tree
25791 do_auto_deduction (tree type, tree init, tree auto_node,
25792 tsubst_flags_t complain, auto_deduction_context context,
25793 tree outer_targs, int flags)
25795 tree targs;
25797 if (init == error_mark_node)
25798 return error_mark_node;
25800 if (init && type_dependent_expression_p (init)
25801 && context != adc_unify)
25802 /* Defining a subset of type-dependent expressions that we can deduce
25803 from ahead of time isn't worth the trouble. */
25804 return type;
25806 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25807 /* C++17 class template argument deduction. */
25808 return do_class_deduction (type, tmpl, init, flags, complain);
25810 if (TREE_TYPE (init) == NULL_TREE)
25811 /* Nothing we can do with this, even in deduction context. */
25812 return type;
25814 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25815 with either a new invented type template parameter U or, if the
25816 initializer is a braced-init-list (8.5.4), with
25817 std::initializer_list<U>. */
25818 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25820 if (!DIRECT_LIST_INIT_P (init))
25821 type = listify_autos (type, auto_node);
25822 else if (CONSTRUCTOR_NELTS (init) == 1)
25823 init = CONSTRUCTOR_ELT (init, 0)->value;
25824 else
25826 if (complain & tf_warning_or_error)
25828 if (permerror (input_location, "direct-list-initialization of "
25829 "%<auto%> requires exactly one element"))
25830 inform (input_location,
25831 "for deduction to %<std::initializer_list%>, use copy-"
25832 "list-initialization (i.e. add %<=%> before the %<{%>)");
25834 type = listify_autos (type, auto_node);
25838 if (type == error_mark_node)
25839 return error_mark_node;
25841 init = resolve_nondeduced_context (init, complain);
25843 if (context == adc_decomp_type
25844 && auto_node == type
25845 && init != error_mark_node
25846 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25847 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25848 and initializer has array type, deduce cv-qualified array type. */
25849 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25850 complain);
25851 else if (AUTO_IS_DECLTYPE (auto_node))
25853 bool id = (DECL_P (init)
25854 || ((TREE_CODE (init) == COMPONENT_REF
25855 || TREE_CODE (init) == SCOPE_REF)
25856 && !REF_PARENTHESIZED_P (init)));
25857 targs = make_tree_vec (1);
25858 TREE_VEC_ELT (targs, 0)
25859 = finish_decltype_type (init, id, tf_warning_or_error);
25860 if (type != auto_node)
25862 if (complain & tf_error)
25863 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25864 return error_mark_node;
25867 else
25869 tree parms = build_tree_list (NULL_TREE, type);
25870 tree tparms;
25872 if (flag_concepts)
25873 tparms = extract_autos (type);
25874 else
25876 tparms = make_tree_vec (1);
25877 TREE_VEC_ELT (tparms, 0)
25878 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25881 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25882 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25883 DEDUCE_CALL, LOOKUP_NORMAL,
25884 NULL, /*explain_p=*/false);
25885 if (val > 0)
25887 if (processing_template_decl)
25888 /* Try again at instantiation time. */
25889 return type;
25890 if (type && type != error_mark_node
25891 && (complain & tf_error))
25892 /* If type is error_mark_node a diagnostic must have been
25893 emitted by now. Also, having a mention to '<type error>'
25894 in the diagnostic is not really useful to the user. */
25896 if (cfun && auto_node == current_function_auto_return_pattern
25897 && LAMBDA_FUNCTION_P (current_function_decl))
25898 error ("unable to deduce lambda return type from %qE", init);
25899 else
25900 error ("unable to deduce %qT from %qE", type, init);
25901 type_unification_real (tparms, targs, parms, &init, 1, 0,
25902 DEDUCE_CALL, LOOKUP_NORMAL,
25903 NULL, /*explain_p=*/true);
25905 return error_mark_node;
25909 /* Check any placeholder constraints against the deduced type. */
25910 if (flag_concepts && !processing_template_decl)
25911 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25913 /* Use the deduced type to check the associated constraints. If we
25914 have a partial-concept-id, rebuild the argument list so that
25915 we check using the extra arguments. */
25916 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25917 tree cargs = CHECK_CONSTR_ARGS (constr);
25918 if (TREE_VEC_LENGTH (cargs) > 1)
25920 cargs = copy_node (cargs);
25921 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25923 else
25924 cargs = targs;
25925 if (!constraints_satisfied_p (constr, cargs))
25927 if (complain & tf_warning_or_error)
25929 switch (context)
25931 case adc_unspecified:
25932 case adc_unify:
25933 error("placeholder constraints not satisfied");
25934 break;
25935 case adc_variable_type:
25936 case adc_decomp_type:
25937 error ("deduced initializer does not satisfy "
25938 "placeholder constraints");
25939 break;
25940 case adc_return_type:
25941 error ("deduced return type does not satisfy "
25942 "placeholder constraints");
25943 break;
25944 case adc_requirement:
25945 error ("deduced expression type does not satisfy "
25946 "placeholder constraints");
25947 break;
25949 diagnose_constraints (input_location, constr, targs);
25951 return error_mark_node;
25955 if (processing_template_decl && context != adc_unify)
25956 outer_targs = current_template_args ();
25957 targs = add_to_template_args (outer_targs, targs);
25958 return tsubst (type, targs, complain, NULL_TREE);
25961 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25962 result. */
25964 tree
25965 splice_late_return_type (tree type, tree late_return_type)
25967 if (is_auto (type))
25969 if (late_return_type)
25970 return late_return_type;
25972 tree idx = get_template_parm_index (type);
25973 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25974 /* In an abbreviated function template we didn't know we were dealing
25975 with a function template when we saw the auto return type, so update
25976 it to have the correct level. */
25977 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25979 return type;
25982 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25983 'decltype(auto)' or a deduced class template. */
25985 bool
25986 is_auto (const_tree type)
25988 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25989 && (TYPE_IDENTIFIER (type) == auto_identifier
25990 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25991 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25992 return true;
25993 else
25994 return false;
25997 /* for_each_template_parm callback for type_uses_auto. */
26000 is_auto_r (tree tp, void */*data*/)
26002 return is_auto (tp);
26005 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26006 a use of `auto'. Returns NULL_TREE otherwise. */
26008 tree
26009 type_uses_auto (tree type)
26011 if (type == NULL_TREE)
26012 return NULL_TREE;
26013 else if (flag_concepts)
26015 /* The Concepts TS allows multiple autos in one type-specifier; just
26016 return the first one we find, do_auto_deduction will collect all of
26017 them. */
26018 if (uses_template_parms (type))
26019 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26020 /*visited*/NULL, /*nondeduced*/true);
26021 else
26022 return NULL_TREE;
26024 else
26025 return find_type_usage (type, is_auto);
26028 /* For a given template T, return the vector of typedefs referenced
26029 in T for which access check is needed at T instantiation time.
26030 T is either a FUNCTION_DECL or a RECORD_TYPE.
26031 Those typedefs were added to T by the function
26032 append_type_to_template_for_access_check. */
26034 vec<qualified_typedef_usage_t, va_gc> *
26035 get_types_needing_access_check (tree t)
26037 tree ti;
26038 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26040 if (!t || t == error_mark_node)
26041 return NULL;
26043 if (!(ti = get_template_info (t)))
26044 return NULL;
26046 if (CLASS_TYPE_P (t)
26047 || TREE_CODE (t) == FUNCTION_DECL)
26049 if (!TI_TEMPLATE (ti))
26050 return NULL;
26052 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26055 return result;
26058 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26059 tied to T. That list of typedefs will be access checked at
26060 T instantiation time.
26061 T is either a FUNCTION_DECL or a RECORD_TYPE.
26062 TYPE_DECL is a TYPE_DECL node representing a typedef.
26063 SCOPE is the scope through which TYPE_DECL is accessed.
26064 LOCATION is the location of the usage point of TYPE_DECL.
26066 This function is a subroutine of
26067 append_type_to_template_for_access_check. */
26069 static void
26070 append_type_to_template_for_access_check_1 (tree t,
26071 tree type_decl,
26072 tree scope,
26073 location_t location)
26075 qualified_typedef_usage_t typedef_usage;
26076 tree ti;
26078 if (!t || t == error_mark_node)
26079 return;
26081 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26082 || CLASS_TYPE_P (t))
26083 && type_decl
26084 && TREE_CODE (type_decl) == TYPE_DECL
26085 && scope);
26087 if (!(ti = get_template_info (t)))
26088 return;
26090 gcc_assert (TI_TEMPLATE (ti));
26092 typedef_usage.typedef_decl = type_decl;
26093 typedef_usage.context = scope;
26094 typedef_usage.locus = location;
26096 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26099 /* Append TYPE_DECL to the template TEMPL.
26100 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26101 At TEMPL instanciation time, TYPE_DECL will be checked to see
26102 if it can be accessed through SCOPE.
26103 LOCATION is the location of the usage point of TYPE_DECL.
26105 e.g. consider the following code snippet:
26107 class C
26109 typedef int myint;
26112 template<class U> struct S
26114 C::myint mi; // <-- usage point of the typedef C::myint
26117 S<char> s;
26119 At S<char> instantiation time, we need to check the access of C::myint
26120 In other words, we need to check the access of the myint typedef through
26121 the C scope. For that purpose, this function will add the myint typedef
26122 and the scope C through which its being accessed to a list of typedefs
26123 tied to the template S. That list will be walked at template instantiation
26124 time and access check performed on each typedefs it contains.
26125 Note that this particular code snippet should yield an error because
26126 myint is private to C. */
26128 void
26129 append_type_to_template_for_access_check (tree templ,
26130 tree type_decl,
26131 tree scope,
26132 location_t location)
26134 qualified_typedef_usage_t *iter;
26135 unsigned i;
26137 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26139 /* Make sure we don't append the type to the template twice. */
26140 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26141 if (iter->typedef_decl == type_decl && scope == iter->context)
26142 return;
26144 append_type_to_template_for_access_check_1 (templ, type_decl,
26145 scope, location);
26148 /* Convert the generic type parameters in PARM that match the types given in the
26149 range [START_IDX, END_IDX) from the current_template_parms into generic type
26150 packs. */
26152 tree
26153 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26155 tree current = current_template_parms;
26156 int depth = TMPL_PARMS_DEPTH (current);
26157 current = INNERMOST_TEMPLATE_PARMS (current);
26158 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26160 for (int i = 0; i < start_idx; ++i)
26161 TREE_VEC_ELT (replacement, i)
26162 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26164 for (int i = start_idx; i < end_idx; ++i)
26166 /* Create a distinct parameter pack type from the current parm and add it
26167 to the replacement args to tsubst below into the generic function
26168 parameter. */
26170 tree o = TREE_TYPE (TREE_VALUE
26171 (TREE_VEC_ELT (current, i)));
26172 tree t = copy_type (o);
26173 TEMPLATE_TYPE_PARM_INDEX (t)
26174 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26175 o, 0, 0, tf_none);
26176 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26177 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26178 TYPE_MAIN_VARIANT (t) = t;
26179 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26180 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26181 TREE_VEC_ELT (replacement, i) = t;
26182 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26185 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26186 TREE_VEC_ELT (replacement, i)
26187 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26189 /* If there are more levels then build up the replacement with the outer
26190 template parms. */
26191 if (depth > 1)
26192 replacement = add_to_template_args (template_parms_to_args
26193 (TREE_CHAIN (current_template_parms)),
26194 replacement);
26196 return tsubst (parm, replacement, tf_none, NULL_TREE);
26199 /* Entries in the decl_constraint hash table. */
26200 struct GTY((for_user)) constr_entry
26202 tree decl;
26203 tree ci;
26206 /* Hashing function and equality for constraint entries. */
26207 struct constr_hasher : ggc_ptr_hash<constr_entry>
26209 static hashval_t hash (constr_entry *e)
26211 return (hashval_t)DECL_UID (e->decl);
26214 static bool equal (constr_entry *e1, constr_entry *e2)
26216 return e1->decl == e2->decl;
26220 /* A mapping from declarations to constraint information. Note that
26221 both templates and their underlying declarations are mapped to the
26222 same constraint information.
26224 FIXME: This is defined in pt.c because garbage collection
26225 code is not being generated for constraint.cc. */
26227 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26229 /* Returns the template constraints of declaration T. If T is not
26230 constrained, return NULL_TREE. Note that T must be non-null. */
26232 tree
26233 get_constraints (tree t)
26235 if (!flag_concepts)
26236 return NULL_TREE;
26238 gcc_assert (DECL_P (t));
26239 if (TREE_CODE (t) == TEMPLATE_DECL)
26240 t = DECL_TEMPLATE_RESULT (t);
26241 constr_entry elt = { t, NULL_TREE };
26242 constr_entry* found = decl_constraints->find (&elt);
26243 if (found)
26244 return found->ci;
26245 else
26246 return NULL_TREE;
26249 /* Associate the given constraint information CI with the declaration
26250 T. If T is a template, then the constraints are associated with
26251 its underlying declaration. Don't build associations if CI is
26252 NULL_TREE. */
26254 void
26255 set_constraints (tree t, tree ci)
26257 if (!ci)
26258 return;
26259 gcc_assert (t && flag_concepts);
26260 if (TREE_CODE (t) == TEMPLATE_DECL)
26261 t = DECL_TEMPLATE_RESULT (t);
26262 gcc_assert (!get_constraints (t));
26263 constr_entry elt = {t, ci};
26264 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26265 constr_entry* entry = ggc_alloc<constr_entry> ();
26266 *entry = elt;
26267 *slot = entry;
26270 /* Remove the associated constraints of the declaration T. */
26272 void
26273 remove_constraints (tree t)
26275 gcc_assert (DECL_P (t));
26276 if (TREE_CODE (t) == TEMPLATE_DECL)
26277 t = DECL_TEMPLATE_RESULT (t);
26279 constr_entry elt = {t, NULL_TREE};
26280 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26281 if (slot)
26282 decl_constraints->clear_slot (slot);
26285 /* Memoized satisfaction results for declarations. This
26286 maps the pair (constraint_info, arguments) to the result computed
26287 by constraints_satisfied_p. */
26289 struct GTY((for_user)) constraint_sat_entry
26291 tree ci;
26292 tree args;
26293 tree result;
26296 /* Hashing function and equality for constraint entries. */
26298 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26300 static hashval_t hash (constraint_sat_entry *e)
26302 hashval_t val = iterative_hash_object(e->ci, 0);
26303 return iterative_hash_template_arg (e->args, val);
26306 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26308 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26312 /* Memoized satisfaction results for concept checks. */
26314 struct GTY((for_user)) concept_spec_entry
26316 tree tmpl;
26317 tree args;
26318 tree result;
26321 /* Hashing function and equality for constraint entries. */
26323 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26325 static hashval_t hash (concept_spec_entry *e)
26327 return hash_tmpl_and_args (e->tmpl, e->args);
26330 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26332 ++comparing_specializations;
26333 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26334 --comparing_specializations;
26335 return eq;
26339 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26340 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26342 /* Search for a memoized satisfaction result. Returns one of the
26343 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26345 tree
26346 lookup_constraint_satisfaction (tree ci, tree args)
26348 constraint_sat_entry elt = { ci, args, NULL_TREE };
26349 constraint_sat_entry* found = constraint_memos->find (&elt);
26350 if (found)
26351 return found->result;
26352 else
26353 return NULL_TREE;
26356 /* Memoize the result of a satisfication test. Returns the saved result. */
26358 tree
26359 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26361 constraint_sat_entry elt = {ci, args, result};
26362 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26363 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26364 *entry = elt;
26365 *slot = entry;
26366 return result;
26369 /* Search for a memoized satisfaction result for a concept. */
26371 tree
26372 lookup_concept_satisfaction (tree tmpl, tree args)
26374 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26375 concept_spec_entry* found = concept_memos->find (&elt);
26376 if (found)
26377 return found->result;
26378 else
26379 return NULL_TREE;
26382 /* Memoize the result of a concept check. Returns the saved result. */
26384 tree
26385 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26387 concept_spec_entry elt = {tmpl, args, result};
26388 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26389 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26390 *entry = elt;
26391 *slot = entry;
26392 return result;
26395 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26397 /* Returns a prior concept specialization. This returns the substituted
26398 and normalized constraints defined by the concept. */
26400 tree
26401 get_concept_expansion (tree tmpl, tree args)
26403 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26404 concept_spec_entry* found = concept_expansions->find (&elt);
26405 if (found)
26406 return found->result;
26407 else
26408 return NULL_TREE;
26411 /* Save a concept expansion for later. */
26413 tree
26414 save_concept_expansion (tree tmpl, tree args, tree def)
26416 concept_spec_entry elt = {tmpl, args, def};
26417 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26418 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26419 *entry = elt;
26420 *slot = entry;
26421 return def;
26424 static hashval_t
26425 hash_subsumption_args (tree t1, tree t2)
26427 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26428 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26429 int val = 0;
26430 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26431 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26432 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26433 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26434 return val;
26437 /* Compare the constraints of two subsumption entries. The LEFT1 and
26438 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26439 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26441 static bool
26442 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26444 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26445 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26446 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26447 CHECK_CONSTR_ARGS (right1)))
26448 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26449 CHECK_CONSTR_ARGS (right2));
26450 return false;
26453 /* Key/value pair for learning and memoizing subsumption results. This
26454 associates a pair of check constraints (including arguments) with
26455 a boolean value indicating the result. */
26457 struct GTY((for_user)) subsumption_entry
26459 tree t1;
26460 tree t2;
26461 bool result;
26464 /* Hashing function and equality for constraint entries. */
26466 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26468 static hashval_t hash (subsumption_entry *e)
26470 return hash_subsumption_args (e->t1, e->t2);
26473 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26475 ++comparing_specializations;
26476 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26477 --comparing_specializations;
26478 return eq;
26482 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26484 /* Search for a previously cached subsumption result. */
26486 bool*
26487 lookup_subsumption_result (tree t1, tree t2)
26489 subsumption_entry elt = { t1, t2, false };
26490 subsumption_entry* found = subsumption_table->find (&elt);
26491 if (found)
26492 return &found->result;
26493 else
26494 return 0;
26497 /* Save a subsumption result. */
26499 bool
26500 save_subsumption_result (tree t1, tree t2, bool result)
26502 subsumption_entry elt = {t1, t2, result};
26503 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26504 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26505 *entry = elt;
26506 *slot = entry;
26507 return result;
26510 /* Set up the hash table for constraint association. */
26512 void
26513 init_constraint_processing (void)
26515 if (!flag_concepts)
26516 return;
26518 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26519 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26520 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26521 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26522 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26525 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26526 0..N-1. */
26528 void
26529 declare_integer_pack (void)
26531 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26532 build_function_type_list (integer_type_node,
26533 integer_type_node,
26534 NULL_TREE),
26535 NULL_TREE, ECF_CONST);
26536 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26537 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26540 /* Set up the hash tables for template instantiations. */
26542 void
26543 init_template_processing (void)
26545 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26546 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26548 if (cxx_dialect >= cxx11)
26549 declare_integer_pack ();
26552 /* Print stats about the template hash tables for -fstats. */
26554 void
26555 print_template_statistics (void)
26557 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26558 "%f collisions\n", (long) decl_specializations->size (),
26559 (long) decl_specializations->elements (),
26560 decl_specializations->collisions ());
26561 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26562 "%f collisions\n", (long) type_specializations->size (),
26563 (long) type_specializations->elements (),
26564 type_specializations->collisions ());
26567 #include "gt-cp-pt.h"