Split out parts of scompare_loc_descriptor and emit_store_flag
[official-gcc.git] / gcc / cp / pt.c
blob141b4d7564a8de9d1a2c0041cdfbce5ea5bda404
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
45 /* The type of functions taking a tree, and some additional data, and
46 returning an int. */
47 typedef int (*tree_fn_t) (tree, void*);
49 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
50 instantiations have been deferred, either because their definitions
51 were not yet available, or because we were putting off doing the work. */
52 struct GTY ((chain_next ("%h.next"))) pending_template {
53 struct pending_template *next;
54 struct tinst_level *tinst;
57 static GTY(()) struct pending_template *pending_templates;
58 static GTY(()) struct pending_template *last_pending_template;
60 int processing_template_parmlist;
61 static int template_header_count;
63 static GTY(()) tree saved_trees;
64 static vec<int> inline_parm_levels;
66 static GTY(()) struct tinst_level *current_tinst_level;
68 static GTY(()) tree saved_access_scope;
70 /* Live only within one (recursive) call to tsubst_expr. We use
71 this to pass the statement expression node from the STMT_EXPR
72 to the EXPR_STMT that is its result. */
73 static tree cur_stmt_expr;
75 // -------------------------------------------------------------------------- //
76 // Local Specialization Stack
78 // Implementation of the RAII helper for creating new local
79 // specializations.
80 local_specialization_stack::local_specialization_stack (lss_policy policy)
81 : saved (local_specializations)
83 if (policy == lss_blank || !saved)
84 local_specializations = new hash_map<tree, tree>;
85 else
86 local_specializations = new hash_map<tree, tree>(*saved);
89 local_specialization_stack::~local_specialization_stack ()
91 delete local_specializations;
92 local_specializations = saved;
95 /* True if we've recursed into fn_type_unification too many times. */
96 static bool excessive_deduction_depth;
98 struct GTY((for_user)) spec_entry
100 tree tmpl;
101 tree args;
102 tree spec;
105 struct spec_hasher : ggc_ptr_hash<spec_entry>
107 static hashval_t hash (spec_entry *);
108 static bool equal (spec_entry *, spec_entry *);
111 static GTY (()) hash_table<spec_hasher> *decl_specializations;
113 static GTY (()) hash_table<spec_hasher> *type_specializations;
115 /* Contains canonical template parameter types. The vector is indexed by
116 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
117 TREE_LIST, whose TREE_VALUEs contain the canonical template
118 parameters of various types and levels. */
119 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
121 #define UNIFY_ALLOW_NONE 0
122 #define UNIFY_ALLOW_MORE_CV_QUAL 1
123 #define UNIFY_ALLOW_LESS_CV_QUAL 2
124 #define UNIFY_ALLOW_DERIVED 4
125 #define UNIFY_ALLOW_INTEGER 8
126 #define UNIFY_ALLOW_OUTER_LEVEL 16
127 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
128 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
130 enum template_base_result {
131 tbr_incomplete_type,
132 tbr_ambiguous_baseclass,
133 tbr_success
136 static void push_access_scope (tree);
137 static void pop_access_scope (tree);
138 static bool resolve_overloaded_unification (tree, tree, tree, tree,
139 unification_kind_t, int,
140 bool);
141 static int try_one_overload (tree, tree, tree, tree, tree,
142 unification_kind_t, int, bool, bool);
143 static int unify (tree, tree, tree, tree, int, bool);
144 static void add_pending_template (tree);
145 static tree reopen_tinst_level (struct tinst_level *);
146 static tree tsubst_initializer_list (tree, tree);
147 static tree get_partial_spec_bindings (tree, tree, tree);
148 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
149 bool, bool);
150 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
151 bool, bool);
152 static void tsubst_enum (tree, tree, tree);
153 static tree add_to_template_args (tree, tree);
154 static tree add_outermost_template_args (tree, tree);
155 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
156 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
157 tree);
158 static int type_unification_real (tree, tree, tree, const tree *,
159 unsigned int, int, unification_kind_t, int,
160 vec<deferred_access_check, va_gc> **,
161 bool);
162 static void note_template_header (int);
163 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
164 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
165 static tree convert_template_argument (tree, tree, tree,
166 tsubst_flags_t, int, tree);
167 static tree for_each_template_parm (tree, tree_fn_t, void*,
168 hash_set<tree> *, bool, tree_fn_t = NULL);
169 static tree expand_template_argument_pack (tree);
170 static tree build_template_parm_index (int, int, int, tree, tree);
171 static bool inline_needs_template_parms (tree, bool);
172 static void push_inline_template_parms_recursive (tree, int);
173 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
174 static int mark_template_parm (tree, void *);
175 static int template_parm_this_level_p (tree, void *);
176 static tree tsubst_friend_function (tree, tree);
177 static tree tsubst_friend_class (tree, tree);
178 static int can_complete_type_without_circularity (tree);
179 static tree get_bindings (tree, tree, tree, bool);
180 static int template_decl_level (tree);
181 static int check_cv_quals_for_unify (int, tree, tree);
182 static void template_parm_level_and_index (tree, int*, int*);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
187 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
189 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
190 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
191 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
192 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
193 static bool check_specialization_scope (void);
194 static tree process_partial_specialization (tree);
195 static void set_current_access_from_decl (tree);
196 static enum template_base_result get_template_base (tree, tree, tree, tree,
197 bool , tree *);
198 static tree try_class_unification (tree, tree, tree, tree, bool);
199 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
200 tree, tree);
201 static bool template_template_parm_bindings_ok_p (tree, tree);
202 static void tsubst_default_arguments (tree, tsubst_flags_t);
203 static tree for_each_template_parm_r (tree *, int *, void *);
204 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
205 static void copy_default_args_to_explicit_spec (tree);
206 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
207 static bool dependent_template_arg_p (tree);
208 static bool any_template_arguments_need_structural_equality_p (tree);
209 static bool dependent_type_p_r (tree);
210 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
211 static tree tsubst_decl (tree, tree, tsubst_flags_t);
212 static void perform_typedefs_access_check (tree tmpl, tree targs);
213 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
214 location_t);
215 static tree listify (tree);
216 static tree listify_autos (tree, tree);
217 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
218 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
219 static bool complex_alias_template_p (const_tree tmpl);
220 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
221 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
222 static tree make_argument_pack (tree);
223 static void register_parameter_specializations (tree, tree);
225 /* Make the current scope suitable for access checking when we are
226 processing T. T can be FUNCTION_DECL for instantiated function
227 template, VAR_DECL for static member variable, or TYPE_DECL for
228 alias template (needed by instantiate_decl). */
230 static void
231 push_access_scope (tree t)
233 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
234 || TREE_CODE (t) == TYPE_DECL);
236 if (DECL_FRIEND_CONTEXT (t))
237 push_nested_class (DECL_FRIEND_CONTEXT (t));
238 else if (DECL_CLASS_SCOPE_P (t))
239 push_nested_class (DECL_CONTEXT (t));
240 else
241 push_to_top_level ();
243 if (TREE_CODE (t) == FUNCTION_DECL)
245 saved_access_scope = tree_cons
246 (NULL_TREE, current_function_decl, saved_access_scope);
247 current_function_decl = t;
251 /* Restore the scope set up by push_access_scope. T is the node we
252 are processing. */
254 static void
255 pop_access_scope (tree t)
257 if (TREE_CODE (t) == FUNCTION_DECL)
259 current_function_decl = TREE_VALUE (saved_access_scope);
260 saved_access_scope = TREE_CHAIN (saved_access_scope);
263 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
264 pop_nested_class ();
265 else
266 pop_from_top_level ();
269 /* Do any processing required when DECL (a member template
270 declaration) is finished. Returns the TEMPLATE_DECL corresponding
271 to DECL, unless it is a specialization, in which case the DECL
272 itself is returned. */
274 tree
275 finish_member_template_decl (tree decl)
277 if (decl == error_mark_node)
278 return error_mark_node;
280 gcc_assert (DECL_P (decl));
282 if (TREE_CODE (decl) == TYPE_DECL)
284 tree type;
286 type = TREE_TYPE (decl);
287 if (type == error_mark_node)
288 return error_mark_node;
289 if (MAYBE_CLASS_TYPE_P (type)
290 && CLASSTYPE_TEMPLATE_INFO (type)
291 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
293 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
294 check_member_template (tmpl);
295 return tmpl;
297 return NULL_TREE;
299 else if (TREE_CODE (decl) == FIELD_DECL)
300 error ("data member %qD cannot be a member template", decl);
301 else if (DECL_TEMPLATE_INFO (decl))
303 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
305 check_member_template (DECL_TI_TEMPLATE (decl));
306 return DECL_TI_TEMPLATE (decl);
308 else
309 return decl;
311 else
312 error ("invalid member template declaration %qD", decl);
314 return error_mark_node;
317 /* Create a template info node. */
319 tree
320 build_template_info (tree template_decl, tree template_args)
322 tree result = make_node (TEMPLATE_INFO);
323 TI_TEMPLATE (result) = template_decl;
324 TI_ARGS (result) = template_args;
325 return result;
328 /* Return the template info node corresponding to T, whatever T is. */
330 tree
331 get_template_info (const_tree t)
333 tree tinfo = NULL_TREE;
335 if (!t || t == error_mark_node)
336 return NULL;
338 if (TREE_CODE (t) == NAMESPACE_DECL
339 || TREE_CODE (t) == PARM_DECL)
340 return NULL;
342 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
343 tinfo = DECL_TEMPLATE_INFO (t);
345 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
346 t = TREE_TYPE (t);
348 if (OVERLOAD_TYPE_P (t))
349 tinfo = TYPE_TEMPLATE_INFO (t);
350 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
351 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
353 return tinfo;
356 /* Returns the template nesting level of the indicated class TYPE.
358 For example, in:
359 template <class T>
360 struct A
362 template <class U>
363 struct B {};
366 A<T>::B<U> has depth two, while A<T> has depth one.
367 Both A<T>::B<int> and A<int>::B<U> have depth one, if
368 they are instantiations, not specializations.
370 This function is guaranteed to return 0 if passed NULL_TREE so
371 that, for example, `template_class_depth (current_class_type)' is
372 always safe. */
375 template_class_depth (tree type)
377 int depth;
379 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
381 tree tinfo = get_template_info (type);
383 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
384 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
385 ++depth;
387 if (DECL_P (type))
388 type = CP_DECL_CONTEXT (type);
389 else if (LAMBDA_TYPE_P (type))
390 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
391 else
392 type = CP_TYPE_CONTEXT (type);
395 return depth;
398 /* Subroutine of maybe_begin_member_template_processing.
399 Returns true if processing DECL needs us to push template parms. */
401 static bool
402 inline_needs_template_parms (tree decl, bool nsdmi)
404 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
405 return false;
407 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
408 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
411 /* Subroutine of maybe_begin_member_template_processing.
412 Push the template parms in PARMS, starting from LEVELS steps into the
413 chain, and ending at the beginning, since template parms are listed
414 innermost first. */
416 static void
417 push_inline_template_parms_recursive (tree parmlist, int levels)
419 tree parms = TREE_VALUE (parmlist);
420 int i;
422 if (levels > 1)
423 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
425 ++processing_template_decl;
426 current_template_parms
427 = tree_cons (size_int (processing_template_decl),
428 parms, current_template_parms);
429 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
431 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
432 NULL);
433 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
435 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
437 if (error_operand_p (parm))
438 continue;
440 gcc_assert (DECL_P (parm));
442 switch (TREE_CODE (parm))
444 case TYPE_DECL:
445 case TEMPLATE_DECL:
446 pushdecl (parm);
447 break;
449 case PARM_DECL:
450 /* Push the CONST_DECL. */
451 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
452 break;
454 default:
455 gcc_unreachable ();
460 /* Restore the template parameter context for a member template, a
461 friend template defined in a class definition, or a non-template
462 member of template class. */
464 void
465 maybe_begin_member_template_processing (tree decl)
467 tree parms;
468 int levels = 0;
469 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
471 if (nsdmi)
473 tree ctx = DECL_CONTEXT (decl);
474 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
475 /* Disregard full specializations (c++/60999). */
476 && uses_template_parms (ctx)
477 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
480 if (inline_needs_template_parms (decl, nsdmi))
482 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
483 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
485 if (DECL_TEMPLATE_SPECIALIZATION (decl))
487 --levels;
488 parms = TREE_CHAIN (parms);
491 push_inline_template_parms_recursive (parms, levels);
494 /* Remember how many levels of template parameters we pushed so that
495 we can pop them later. */
496 inline_parm_levels.safe_push (levels);
499 /* Undo the effects of maybe_begin_member_template_processing. */
501 void
502 maybe_end_member_template_processing (void)
504 int i;
505 int last;
507 if (inline_parm_levels.length () == 0)
508 return;
510 last = inline_parm_levels.pop ();
511 for (i = 0; i < last; ++i)
513 --processing_template_decl;
514 current_template_parms = TREE_CHAIN (current_template_parms);
515 poplevel (0, 0, 0);
519 /* Return a new template argument vector which contains all of ARGS,
520 but has as its innermost set of arguments the EXTRA_ARGS. */
522 static tree
523 add_to_template_args (tree args, tree extra_args)
525 tree new_args;
526 int extra_depth;
527 int i;
528 int j;
530 if (args == NULL_TREE || extra_args == error_mark_node)
531 return extra_args;
533 extra_depth = TMPL_ARGS_DEPTH (extra_args);
534 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
536 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
537 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
539 for (j = 1; j <= extra_depth; ++j, ++i)
540 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
542 return new_args;
545 /* Like add_to_template_args, but only the outermost ARGS are added to
546 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
547 (EXTRA_ARGS) levels are added. This function is used to combine
548 the template arguments from a partial instantiation with the
549 template arguments used to attain the full instantiation from the
550 partial instantiation. */
552 static tree
553 add_outermost_template_args (tree args, tree extra_args)
555 tree new_args;
557 /* If there are more levels of EXTRA_ARGS than there are ARGS,
558 something very fishy is going on. */
559 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
561 /* If *all* the new arguments will be the EXTRA_ARGS, just return
562 them. */
563 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
564 return extra_args;
566 /* For the moment, we make ARGS look like it contains fewer levels. */
567 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
569 new_args = add_to_template_args (args, extra_args);
571 /* Now, we restore ARGS to its full dimensions. */
572 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
574 return new_args;
577 /* Return the N levels of innermost template arguments from the ARGS. */
579 tree
580 get_innermost_template_args (tree args, int n)
582 tree new_args;
583 int extra_levels;
584 int i;
586 gcc_assert (n >= 0);
588 /* If N is 1, just return the innermost set of template arguments. */
589 if (n == 1)
590 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
592 /* If we're not removing anything, just return the arguments we were
593 given. */
594 extra_levels = TMPL_ARGS_DEPTH (args) - n;
595 gcc_assert (extra_levels >= 0);
596 if (extra_levels == 0)
597 return args;
599 /* Make a new set of arguments, not containing the outer arguments. */
600 new_args = make_tree_vec (n);
601 for (i = 1; i <= n; ++i)
602 SET_TMPL_ARGS_LEVEL (new_args, i,
603 TMPL_ARGS_LEVEL (args, i + extra_levels));
605 return new_args;
608 /* The inverse of get_innermost_template_args: Return all but the innermost
609 EXTRA_LEVELS levels of template arguments from the ARGS. */
611 static tree
612 strip_innermost_template_args (tree args, int extra_levels)
614 tree new_args;
615 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
616 int i;
618 gcc_assert (n >= 0);
620 /* If N is 1, just return the outermost set of template arguments. */
621 if (n == 1)
622 return TMPL_ARGS_LEVEL (args, 1);
624 /* If we're not removing anything, just return the arguments we were
625 given. */
626 gcc_assert (extra_levels >= 0);
627 if (extra_levels == 0)
628 return args;
630 /* Make a new set of arguments, not containing the inner arguments. */
631 new_args = make_tree_vec (n);
632 for (i = 1; i <= n; ++i)
633 SET_TMPL_ARGS_LEVEL (new_args, i,
634 TMPL_ARGS_LEVEL (args, i));
636 return new_args;
639 /* We've got a template header coming up; push to a new level for storing
640 the parms. */
642 void
643 begin_template_parm_list (void)
645 /* We use a non-tag-transparent scope here, which causes pushtag to
646 put tags in this scope, rather than in the enclosing class or
647 namespace scope. This is the right thing, since we want
648 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
649 global template class, push_template_decl handles putting the
650 TEMPLATE_DECL into top-level scope. For a nested template class,
651 e.g.:
653 template <class T> struct S1 {
654 template <class T> struct S2 {};
657 pushtag contains special code to insert the TEMPLATE_DECL for S2
658 at the right scope. */
659 begin_scope (sk_template_parms, NULL);
660 ++processing_template_decl;
661 ++processing_template_parmlist;
662 note_template_header (0);
664 /* Add a dummy parameter level while we process the parameter list. */
665 current_template_parms
666 = tree_cons (size_int (processing_template_decl),
667 make_tree_vec (0),
668 current_template_parms);
671 /* This routine is called when a specialization is declared. If it is
672 invalid to declare a specialization here, an error is reported and
673 false is returned, otherwise this routine will return true. */
675 static bool
676 check_specialization_scope (void)
678 tree scope = current_scope ();
680 /* [temp.expl.spec]
682 An explicit specialization shall be declared in the namespace of
683 which the template is a member, or, for member templates, in the
684 namespace of which the enclosing class or enclosing class
685 template is a member. An explicit specialization of a member
686 function, member class or static data member of a class template
687 shall be declared in the namespace of which the class template
688 is a member. */
689 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
691 error ("explicit specialization in non-namespace scope %qD", scope);
692 return false;
695 /* [temp.expl.spec]
697 In an explicit specialization declaration for a member of a class
698 template or a member template that appears in namespace scope,
699 the member template and some of its enclosing class templates may
700 remain unspecialized, except that the declaration shall not
701 explicitly specialize a class member template if its enclosing
702 class templates are not explicitly specialized as well. */
703 if (current_template_parms)
705 error ("enclosing class templates are not explicitly specialized");
706 return false;
709 return true;
712 /* We've just seen template <>. */
714 bool
715 begin_specialization (void)
717 begin_scope (sk_template_spec, NULL);
718 note_template_header (1);
719 return check_specialization_scope ();
722 /* Called at then end of processing a declaration preceded by
723 template<>. */
725 void
726 end_specialization (void)
728 finish_scope ();
729 reset_specialization ();
732 /* Any template <>'s that we have seen thus far are not referring to a
733 function specialization. */
735 void
736 reset_specialization (void)
738 processing_specialization = 0;
739 template_header_count = 0;
742 /* We've just seen a template header. If SPECIALIZATION is nonzero,
743 it was of the form template <>. */
745 static void
746 note_template_header (int specialization)
748 processing_specialization = specialization;
749 template_header_count++;
752 /* We're beginning an explicit instantiation. */
754 void
755 begin_explicit_instantiation (void)
757 gcc_assert (!processing_explicit_instantiation);
758 processing_explicit_instantiation = true;
762 void
763 end_explicit_instantiation (void)
765 gcc_assert (processing_explicit_instantiation);
766 processing_explicit_instantiation = false;
769 /* An explicit specialization or partial specialization of TMPL is being
770 declared. Check that the namespace in which the specialization is
771 occurring is permissible. Returns false iff it is invalid to
772 specialize TMPL in the current namespace. */
774 static bool
775 check_specialization_namespace (tree tmpl)
777 tree tpl_ns = decl_namespace_context (tmpl);
779 /* [tmpl.expl.spec]
781 An explicit specialization shall be declared in a namespace enclosing the
782 specialized template. An explicit specialization whose declarator-id is
783 not qualified shall be declared in the nearest enclosing namespace of the
784 template, or, if the namespace is inline (7.3.1), any namespace from its
785 enclosing namespace set. */
786 if (current_scope() != DECL_CONTEXT (tmpl)
787 && !at_namespace_scope_p ())
789 error ("specialization of %qD must appear at namespace scope", tmpl);
790 return false;
793 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
794 /* Same or enclosing namespace. */
795 return true;
796 else
798 permerror (input_location,
799 "specialization of %qD in different namespace", tmpl);
800 inform (DECL_SOURCE_LOCATION (tmpl),
801 " from definition of %q#D", tmpl);
802 return false;
806 /* SPEC is an explicit instantiation. Check that it is valid to
807 perform this explicit instantiation in the current namespace. */
809 static void
810 check_explicit_instantiation_namespace (tree spec)
812 tree ns;
814 /* DR 275: An explicit instantiation shall appear in an enclosing
815 namespace of its template. */
816 ns = decl_namespace_context (spec);
817 if (!is_nested_namespace (current_namespace, ns))
818 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
819 "(which does not enclose namespace %qD)",
820 spec, current_namespace, ns);
823 // Returns the type of a template specialization only if that
824 // specialization needs to be defined. Otherwise (e.g., if the type has
825 // already been defined), the function returns NULL_TREE.
826 static tree
827 maybe_new_partial_specialization (tree type)
829 // An implicit instantiation of an incomplete type implies
830 // the definition of a new class template.
832 // template<typename T>
833 // struct S;
835 // template<typename T>
836 // struct S<T*>;
838 // Here, S<T*> is an implicit instantiation of S whose type
839 // is incomplete.
840 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
841 return type;
843 // It can also be the case that TYPE is a completed specialization.
844 // Continuing the previous example, suppose we also declare:
846 // template<typename T>
847 // requires Integral<T>
848 // struct S<T*>;
850 // Here, S<T*> refers to the specialization S<T*> defined
851 // above. However, we need to differentiate definitions because
852 // we intend to define a new partial specialization. In this case,
853 // we rely on the fact that the constraints are different for
854 // this declaration than that above.
856 // Note that we also get here for injected class names and
857 // late-parsed template definitions. We must ensure that we
858 // do not create new type declarations for those cases.
859 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
861 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
862 tree args = CLASSTYPE_TI_ARGS (type);
864 // If there are no template parameters, this cannot be a new
865 // partial template specializtion?
866 if (!current_template_parms)
867 return NULL_TREE;
869 // The injected-class-name is not a new partial specialization.
870 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
871 return NULL_TREE;
873 // If the constraints are not the same as those of the primary
874 // then, we can probably create a new specialization.
875 tree type_constr = current_template_constraints ();
877 if (type == TREE_TYPE (tmpl))
879 tree main_constr = get_constraints (tmpl);
880 if (equivalent_constraints (type_constr, main_constr))
881 return NULL_TREE;
884 // Also, if there's a pre-existing specialization with matching
885 // constraints, then this also isn't new.
886 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
887 while (specs)
889 tree spec_tmpl = TREE_VALUE (specs);
890 tree spec_args = TREE_PURPOSE (specs);
891 tree spec_constr = get_constraints (spec_tmpl);
892 if (comp_template_args (args, spec_args)
893 && equivalent_constraints (type_constr, spec_constr))
894 return NULL_TREE;
895 specs = TREE_CHAIN (specs);
898 // Create a new type node (and corresponding type decl)
899 // for the newly declared specialization.
900 tree t = make_class_type (TREE_CODE (type));
901 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
902 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
904 /* We only need a separate type node for storing the definition of this
905 partial specialization; uses of S<T*> are unconstrained, so all are
906 equivalent. So keep TYPE_CANONICAL the same. */
907 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
909 // Build the corresponding type decl.
910 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
911 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
912 DECL_SOURCE_LOCATION (d) = input_location;
914 return t;
917 return NULL_TREE;
920 /* The TYPE is being declared. If it is a template type, that means it
921 is a partial specialization. Do appropriate error-checking. */
923 tree
924 maybe_process_partial_specialization (tree type)
926 tree context;
928 if (type == error_mark_node)
929 return error_mark_node;
931 /* A lambda that appears in specialization context is not itself a
932 specialization. */
933 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
934 return type;
936 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
938 error ("name of class shadows template template parameter %qD",
939 TYPE_NAME (type));
940 return error_mark_node;
943 context = TYPE_CONTEXT (type);
945 if (TYPE_ALIAS_P (type))
947 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
949 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
950 error ("specialization of alias template %qD",
951 TI_TEMPLATE (tinfo));
952 else
953 error ("explicit specialization of non-template %qT", type);
954 return error_mark_node;
956 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
958 /* This is for ordinary explicit specialization and partial
959 specialization of a template class such as:
961 template <> class C<int>;
965 template <class T> class C<T*>;
967 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
969 if (tree t = maybe_new_partial_specialization (type))
971 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
972 && !at_namespace_scope_p ())
973 return error_mark_node;
974 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
975 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
976 if (processing_template_decl)
978 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
979 if (decl == error_mark_node)
980 return error_mark_node;
981 return TREE_TYPE (decl);
984 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
985 error ("specialization of %qT after instantiation", type);
986 else if (errorcount && !processing_specialization
987 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
988 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
989 /* Trying to define a specialization either without a template<> header
990 or in an inappropriate place. We've already given an error, so just
991 bail now so we don't actually define the specialization. */
992 return error_mark_node;
994 else if (CLASS_TYPE_P (type)
995 && !CLASSTYPE_USE_TEMPLATE (type)
996 && CLASSTYPE_TEMPLATE_INFO (type)
997 && context && CLASS_TYPE_P (context)
998 && CLASSTYPE_TEMPLATE_INFO (context))
1000 /* This is for an explicit specialization of member class
1001 template according to [temp.expl.spec/18]:
1003 template <> template <class U> class C<int>::D;
1005 The context `C<int>' must be an implicit instantiation.
1006 Otherwise this is just a member class template declared
1007 earlier like:
1009 template <> class C<int> { template <class U> class D; };
1010 template <> template <class U> class C<int>::D;
1012 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1013 while in the second case, `C<int>::D' is a primary template
1014 and `C<T>::D' may not exist. */
1016 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1017 && !COMPLETE_TYPE_P (type))
1019 tree t;
1020 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1022 if (current_namespace
1023 != decl_namespace_context (tmpl))
1025 permerror (input_location,
1026 "specializing %q#T in different namespace", type);
1027 permerror (DECL_SOURCE_LOCATION (tmpl),
1028 " from definition of %q#D", tmpl);
1031 /* Check for invalid specialization after instantiation:
1033 template <> template <> class C<int>::D<int>;
1034 template <> template <class U> class C<int>::D; */
1036 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1037 t; t = TREE_CHAIN (t))
1039 tree inst = TREE_VALUE (t);
1040 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1041 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1043 /* We already have a full specialization of this partial
1044 instantiation, or a full specialization has been
1045 looked up but not instantiated. Reassign it to the
1046 new member specialization template. */
1047 spec_entry elt;
1048 spec_entry *entry;
1050 elt.tmpl = most_general_template (tmpl);
1051 elt.args = CLASSTYPE_TI_ARGS (inst);
1052 elt.spec = inst;
1054 type_specializations->remove_elt (&elt);
1056 elt.tmpl = tmpl;
1057 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1059 spec_entry **slot
1060 = type_specializations->find_slot (&elt, INSERT);
1061 entry = ggc_alloc<spec_entry> ();
1062 *entry = elt;
1063 *slot = entry;
1065 else
1066 /* But if we've had an implicit instantiation, that's a
1067 problem ([temp.expl.spec]/6). */
1068 error ("specialization %qT after instantiation %qT",
1069 type, inst);
1072 /* Mark TYPE as a specialization. And as a result, we only
1073 have one level of template argument for the innermost
1074 class template. */
1075 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1076 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1077 CLASSTYPE_TI_ARGS (type)
1078 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1081 else if (processing_specialization)
1083 /* Someday C++0x may allow for enum template specialization. */
1084 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1085 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1086 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1087 "of %qD not allowed by ISO C++", type);
1088 else
1090 error ("explicit specialization of non-template %qT", type);
1091 return error_mark_node;
1095 return type;
1098 /* Returns nonzero if we can optimize the retrieval of specializations
1099 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1100 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1102 static inline bool
1103 optimize_specialization_lookup_p (tree tmpl)
1105 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1106 && DECL_CLASS_SCOPE_P (tmpl)
1107 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1108 parameter. */
1109 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1110 /* The optimized lookup depends on the fact that the
1111 template arguments for the member function template apply
1112 purely to the containing class, which is not true if the
1113 containing class is an explicit or partial
1114 specialization. */
1115 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1116 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1117 && !DECL_CONV_FN_P (tmpl)
1118 /* It is possible to have a template that is not a member
1119 template and is not a member of a template class:
1121 template <typename T>
1122 struct S { friend A::f(); };
1124 Here, the friend function is a template, but the context does
1125 not have template information. The optimized lookup relies
1126 on having ARGS be the template arguments for both the class
1127 and the function template. */
1128 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1131 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1132 gone through coerce_template_parms by now. */
1134 static void
1135 verify_unstripped_args (tree args)
1137 ++processing_template_decl;
1138 if (!any_dependent_template_arguments_p (args))
1140 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1141 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1143 tree arg = TREE_VEC_ELT (inner, i);
1144 if (TREE_CODE (arg) == TEMPLATE_DECL)
1145 /* OK */;
1146 else if (TYPE_P (arg))
1147 gcc_assert (strip_typedefs (arg, NULL) == arg);
1148 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1151 else
1152 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1155 --processing_template_decl;
1158 /* Retrieve the specialization (in the sense of [temp.spec] - a
1159 specialization is either an instantiation or an explicit
1160 specialization) of TMPL for the given template ARGS. If there is
1161 no such specialization, return NULL_TREE. The ARGS are a vector of
1162 arguments, or a vector of vectors of arguments, in the case of
1163 templates with more than one level of parameters.
1165 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1166 then we search for a partial specialization matching ARGS. This
1167 parameter is ignored if TMPL is not a class template.
1169 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1170 result is a NONTYPE_ARGUMENT_PACK. */
1172 static tree
1173 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1175 if (tmpl == NULL_TREE)
1176 return NULL_TREE;
1178 if (args == error_mark_node)
1179 return NULL_TREE;
1181 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1182 || TREE_CODE (tmpl) == FIELD_DECL);
1184 /* There should be as many levels of arguments as there are
1185 levels of parameters. */
1186 gcc_assert (TMPL_ARGS_DEPTH (args)
1187 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1188 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1189 : template_class_depth (DECL_CONTEXT (tmpl))));
1191 if (flag_checking)
1192 verify_unstripped_args (args);
1194 /* Lambda functions in templates aren't instantiated normally, but through
1195 tsubst_lambda_expr. */
1196 if (LAMBDA_FUNCTION_P (tmpl))
1198 bool generic = PRIMARY_TEMPLATE_P (tmpl);
1199 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)) > generic)
1200 return NULL_TREE;
1202 /* But generic lambda functions are instantiated normally, once their
1203 containing context is fully instantiated. */
1204 gcc_assert (generic);
1207 if (optimize_specialization_lookup_p (tmpl))
1209 /* The template arguments actually apply to the containing
1210 class. Find the class specialization with those
1211 arguments. */
1212 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1213 tree class_specialization
1214 = retrieve_specialization (class_template, args, 0);
1215 if (!class_specialization)
1216 return NULL_TREE;
1218 /* Find the instance of TMPL. */
1219 tree fns = lookup_fnfields_slot (class_specialization, DECL_NAME (tmpl));
1220 for (ovl_iterator iter (fns); iter; ++iter)
1222 tree fn = *iter;
1223 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1224 /* using-declarations can add base methods to the method vec,
1225 and we don't want those here. */
1226 && DECL_CONTEXT (fn) == class_specialization)
1227 return fn;
1229 return NULL_TREE;
1231 else
1233 spec_entry *found;
1234 spec_entry elt;
1235 hash_table<spec_hasher> *specializations;
1237 elt.tmpl = tmpl;
1238 elt.args = args;
1239 elt.spec = NULL_TREE;
1241 if (DECL_CLASS_TEMPLATE_P (tmpl))
1242 specializations = type_specializations;
1243 else
1244 specializations = decl_specializations;
1246 if (hash == 0)
1247 hash = spec_hasher::hash (&elt);
1248 found = specializations->find_with_hash (&elt, hash);
1249 if (found)
1250 return found->spec;
1253 return NULL_TREE;
1256 /* Like retrieve_specialization, but for local declarations. */
1258 tree
1259 retrieve_local_specialization (tree tmpl)
1261 if (local_specializations == NULL)
1262 return NULL_TREE;
1264 tree *slot = local_specializations->get (tmpl);
1265 return slot ? *slot : NULL_TREE;
1268 /* Returns nonzero iff DECL is a specialization of TMPL. */
1271 is_specialization_of (tree decl, tree tmpl)
1273 tree t;
1275 if (TREE_CODE (decl) == FUNCTION_DECL)
1277 for (t = decl;
1278 t != NULL_TREE;
1279 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1280 if (t == tmpl)
1281 return 1;
1283 else
1285 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1287 for (t = TREE_TYPE (decl);
1288 t != NULL_TREE;
1289 t = CLASSTYPE_USE_TEMPLATE (t)
1290 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1291 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1292 return 1;
1295 return 0;
1298 /* Returns nonzero iff DECL is a specialization of friend declaration
1299 FRIEND_DECL according to [temp.friend]. */
1301 bool
1302 is_specialization_of_friend (tree decl, tree friend_decl)
1304 bool need_template = true;
1305 int template_depth;
1307 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1308 || TREE_CODE (decl) == TYPE_DECL);
1310 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1311 of a template class, we want to check if DECL is a specialization
1312 if this. */
1313 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1314 && DECL_TEMPLATE_INFO (friend_decl)
1315 && !DECL_USE_TEMPLATE (friend_decl))
1317 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1318 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1319 need_template = false;
1321 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1322 && !PRIMARY_TEMPLATE_P (friend_decl))
1323 need_template = false;
1325 /* There is nothing to do if this is not a template friend. */
1326 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1327 return false;
1329 if (is_specialization_of (decl, friend_decl))
1330 return true;
1332 /* [temp.friend/6]
1333 A member of a class template may be declared to be a friend of a
1334 non-template class. In this case, the corresponding member of
1335 every specialization of the class template is a friend of the
1336 class granting friendship.
1338 For example, given a template friend declaration
1340 template <class T> friend void A<T>::f();
1342 the member function below is considered a friend
1344 template <> struct A<int> {
1345 void f();
1348 For this type of template friend, TEMPLATE_DEPTH below will be
1349 nonzero. To determine if DECL is a friend of FRIEND, we first
1350 check if the enclosing class is a specialization of another. */
1352 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1353 if (template_depth
1354 && DECL_CLASS_SCOPE_P (decl)
1355 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1356 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1358 /* Next, we check the members themselves. In order to handle
1359 a few tricky cases, such as when FRIEND_DECL's are
1361 template <class T> friend void A<T>::g(T t);
1362 template <class T> template <T t> friend void A<T>::h();
1364 and DECL's are
1366 void A<int>::g(int);
1367 template <int> void A<int>::h();
1369 we need to figure out ARGS, the template arguments from
1370 the context of DECL. This is required for template substitution
1371 of `T' in the function parameter of `g' and template parameter
1372 of `h' in the above examples. Here ARGS corresponds to `int'. */
1374 tree context = DECL_CONTEXT (decl);
1375 tree args = NULL_TREE;
1376 int current_depth = 0;
1378 while (current_depth < template_depth)
1380 if (CLASSTYPE_TEMPLATE_INFO (context))
1382 if (current_depth == 0)
1383 args = TYPE_TI_ARGS (context);
1384 else
1385 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1386 current_depth++;
1388 context = TYPE_CONTEXT (context);
1391 if (TREE_CODE (decl) == FUNCTION_DECL)
1393 bool is_template;
1394 tree friend_type;
1395 tree decl_type;
1396 tree friend_args_type;
1397 tree decl_args_type;
1399 /* Make sure that both DECL and FRIEND_DECL are templates or
1400 non-templates. */
1401 is_template = DECL_TEMPLATE_INFO (decl)
1402 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1403 if (need_template ^ is_template)
1404 return false;
1405 else if (is_template)
1407 /* If both are templates, check template parameter list. */
1408 tree friend_parms
1409 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1410 args, tf_none);
1411 if (!comp_template_parms
1412 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1413 friend_parms))
1414 return false;
1416 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1418 else
1419 decl_type = TREE_TYPE (decl);
1421 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1422 tf_none, NULL_TREE);
1423 if (friend_type == error_mark_node)
1424 return false;
1426 /* Check if return types match. */
1427 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1428 return false;
1430 /* Check if function parameter types match, ignoring the
1431 `this' parameter. */
1432 friend_args_type = TYPE_ARG_TYPES (friend_type);
1433 decl_args_type = TYPE_ARG_TYPES (decl_type);
1434 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1435 friend_args_type = TREE_CHAIN (friend_args_type);
1436 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1437 decl_args_type = TREE_CHAIN (decl_args_type);
1439 return compparms (decl_args_type, friend_args_type);
1441 else
1443 /* DECL is a TYPE_DECL */
1444 bool is_template;
1445 tree decl_type = TREE_TYPE (decl);
1447 /* Make sure that both DECL and FRIEND_DECL are templates or
1448 non-templates. */
1449 is_template
1450 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1451 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1453 if (need_template ^ is_template)
1454 return false;
1455 else if (is_template)
1457 tree friend_parms;
1458 /* If both are templates, check the name of the two
1459 TEMPLATE_DECL's first because is_friend didn't. */
1460 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1461 != DECL_NAME (friend_decl))
1462 return false;
1464 /* Now check template parameter list. */
1465 friend_parms
1466 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1467 args, tf_none);
1468 return comp_template_parms
1469 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1470 friend_parms);
1472 else
1473 return (DECL_NAME (decl)
1474 == DECL_NAME (friend_decl));
1477 return false;
1480 /* Register the specialization SPEC as a specialization of TMPL with
1481 the indicated ARGS. IS_FRIEND indicates whether the specialization
1482 is actually just a friend declaration. Returns SPEC, or an
1483 equivalent prior declaration, if available.
1485 We also store instantiations of field packs in the hash table, even
1486 though they are not themselves templates, to make lookup easier. */
1488 static tree
1489 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1490 hashval_t hash)
1492 tree fn;
1493 spec_entry **slot = NULL;
1494 spec_entry elt;
1496 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1497 || (TREE_CODE (tmpl) == FIELD_DECL
1498 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1500 if (TREE_CODE (spec) == FUNCTION_DECL
1501 && uses_template_parms (DECL_TI_ARGS (spec)))
1502 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1503 register it; we want the corresponding TEMPLATE_DECL instead.
1504 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1505 the more obvious `uses_template_parms (spec)' to avoid problems
1506 with default function arguments. In particular, given
1507 something like this:
1509 template <class T> void f(T t1, T t = T())
1511 the default argument expression is not substituted for in an
1512 instantiation unless and until it is actually needed. */
1513 return spec;
1515 if (optimize_specialization_lookup_p (tmpl))
1516 /* We don't put these specializations in the hash table, but we might
1517 want to give an error about a mismatch. */
1518 fn = retrieve_specialization (tmpl, args, 0);
1519 else
1521 elt.tmpl = tmpl;
1522 elt.args = args;
1523 elt.spec = spec;
1525 if (hash == 0)
1526 hash = spec_hasher::hash (&elt);
1528 slot =
1529 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1530 if (*slot)
1531 fn = ((spec_entry *) *slot)->spec;
1532 else
1533 fn = NULL_TREE;
1536 /* We can sometimes try to re-register a specialization that we've
1537 already got. In particular, regenerate_decl_from_template calls
1538 duplicate_decls which will update the specialization list. But,
1539 we'll still get called again here anyhow. It's more convenient
1540 to simply allow this than to try to prevent it. */
1541 if (fn == spec)
1542 return spec;
1543 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1545 if (DECL_TEMPLATE_INSTANTIATION (fn))
1547 if (DECL_ODR_USED (fn)
1548 || DECL_EXPLICIT_INSTANTIATION (fn))
1550 error ("specialization of %qD after instantiation",
1551 fn);
1552 return error_mark_node;
1554 else
1556 tree clone;
1557 /* This situation should occur only if the first
1558 specialization is an implicit instantiation, the
1559 second is an explicit specialization, and the
1560 implicit instantiation has not yet been used. That
1561 situation can occur if we have implicitly
1562 instantiated a member function and then specialized
1563 it later.
1565 We can also wind up here if a friend declaration that
1566 looked like an instantiation turns out to be a
1567 specialization:
1569 template <class T> void foo(T);
1570 class S { friend void foo<>(int) };
1571 template <> void foo(int);
1573 We transform the existing DECL in place so that any
1574 pointers to it become pointers to the updated
1575 declaration.
1577 If there was a definition for the template, but not
1578 for the specialization, we want this to look as if
1579 there were no definition, and vice versa. */
1580 DECL_INITIAL (fn) = NULL_TREE;
1581 duplicate_decls (spec, fn, is_friend);
1582 /* The call to duplicate_decls will have applied
1583 [temp.expl.spec]:
1585 An explicit specialization of a function template
1586 is inline only if it is explicitly declared to be,
1587 and independently of whether its function template
1590 to the primary function; now copy the inline bits to
1591 the various clones. */
1592 FOR_EACH_CLONE (clone, fn)
1594 DECL_DECLARED_INLINE_P (clone)
1595 = DECL_DECLARED_INLINE_P (fn);
1596 DECL_SOURCE_LOCATION (clone)
1597 = DECL_SOURCE_LOCATION (fn);
1598 DECL_DELETED_FN (clone)
1599 = DECL_DELETED_FN (fn);
1601 check_specialization_namespace (tmpl);
1603 return fn;
1606 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1608 tree dd = duplicate_decls (spec, fn, is_friend);
1609 if (dd == error_mark_node)
1610 /* We've already complained in duplicate_decls. */
1611 return error_mark_node;
1613 if (dd == NULL_TREE && DECL_INITIAL (spec))
1614 /* Dup decl failed, but this is a new definition. Set the
1615 line number so any errors match this new
1616 definition. */
1617 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1619 return fn;
1622 else if (fn)
1623 return duplicate_decls (spec, fn, is_friend);
1625 /* A specialization must be declared in the same namespace as the
1626 template it is specializing. */
1627 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1628 && !check_specialization_namespace (tmpl))
1629 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1631 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1633 spec_entry *entry = ggc_alloc<spec_entry> ();
1634 gcc_assert (tmpl && args && spec);
1635 *entry = elt;
1636 *slot = entry;
1637 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1638 && PRIMARY_TEMPLATE_P (tmpl)
1639 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1640 || variable_template_p (tmpl))
1641 /* If TMPL is a forward declaration of a template function, keep a list
1642 of all specializations in case we need to reassign them to a friend
1643 template later in tsubst_friend_function.
1645 Also keep a list of all variable template instantiations so that
1646 process_partial_specialization can check whether a later partial
1647 specialization would have used it. */
1648 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1649 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1652 return spec;
1655 /* Returns true iff two spec_entry nodes are equivalent. */
1657 int comparing_specializations;
1659 bool
1660 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1662 int equal;
1664 ++comparing_specializations;
1665 equal = (e1->tmpl == e2->tmpl
1666 && comp_template_args (e1->args, e2->args));
1667 if (equal && flag_concepts
1668 /* tmpl could be a FIELD_DECL for a capture pack. */
1669 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1670 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1671 && uses_template_parms (e1->args))
1673 /* Partial specializations of a variable template can be distinguished by
1674 constraints. */
1675 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1676 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1677 equal = equivalent_constraints (c1, c2);
1679 --comparing_specializations;
1681 return equal;
1684 /* Returns a hash for a template TMPL and template arguments ARGS. */
1686 static hashval_t
1687 hash_tmpl_and_args (tree tmpl, tree args)
1689 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1690 return iterative_hash_template_arg (args, val);
1693 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1694 ignoring SPEC. */
1696 hashval_t
1697 spec_hasher::hash (spec_entry *e)
1699 return hash_tmpl_and_args (e->tmpl, e->args);
1702 /* Recursively calculate a hash value for a template argument ARG, for use
1703 in the hash tables of template specializations. */
1705 hashval_t
1706 iterative_hash_template_arg (tree arg, hashval_t val)
1708 unsigned HOST_WIDE_INT i;
1709 enum tree_code code;
1710 char tclass;
1712 if (arg == NULL_TREE)
1713 return iterative_hash_object (arg, val);
1715 if (!TYPE_P (arg))
1716 STRIP_NOPS (arg);
1718 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1719 gcc_unreachable ();
1721 code = TREE_CODE (arg);
1722 tclass = TREE_CODE_CLASS (code);
1724 val = iterative_hash_object (code, val);
1726 switch (code)
1728 case ERROR_MARK:
1729 return val;
1731 case IDENTIFIER_NODE:
1732 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1734 case TREE_VEC:
1736 int i, len = TREE_VEC_LENGTH (arg);
1737 for (i = 0; i < len; ++i)
1738 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1739 return val;
1742 case TYPE_PACK_EXPANSION:
1743 case EXPR_PACK_EXPANSION:
1744 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1745 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1747 case TYPE_ARGUMENT_PACK:
1748 case NONTYPE_ARGUMENT_PACK:
1749 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1751 case TREE_LIST:
1752 for (; arg; arg = TREE_CHAIN (arg))
1753 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1754 return val;
1756 case OVERLOAD:
1757 for (lkp_iterator iter (arg); iter; ++iter)
1758 val = iterative_hash_template_arg (*iter, val);
1759 return val;
1761 case CONSTRUCTOR:
1763 tree field, value;
1764 iterative_hash_template_arg (TREE_TYPE (arg), val);
1765 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1767 val = iterative_hash_template_arg (field, val);
1768 val = iterative_hash_template_arg (value, val);
1770 return val;
1773 case PARM_DECL:
1774 if (!DECL_ARTIFICIAL (arg))
1776 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1777 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1779 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1781 case TARGET_EXPR:
1782 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1784 case PTRMEM_CST:
1785 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1786 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1788 case TEMPLATE_PARM_INDEX:
1789 val = iterative_hash_template_arg
1790 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1791 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1792 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1794 case TRAIT_EXPR:
1795 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1796 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1797 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1799 case BASELINK:
1800 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1801 val);
1802 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1803 val);
1805 case MODOP_EXPR:
1806 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1807 code = TREE_CODE (TREE_OPERAND (arg, 1));
1808 val = iterative_hash_object (code, val);
1809 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1811 case LAMBDA_EXPR:
1812 /* A lambda can't appear in a template arg, but don't crash on
1813 erroneous input. */
1814 gcc_assert (seen_error ());
1815 return val;
1817 case CAST_EXPR:
1818 case IMPLICIT_CONV_EXPR:
1819 case STATIC_CAST_EXPR:
1820 case REINTERPRET_CAST_EXPR:
1821 case CONST_CAST_EXPR:
1822 case DYNAMIC_CAST_EXPR:
1823 case NEW_EXPR:
1824 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1825 /* Now hash operands as usual. */
1826 break;
1828 default:
1829 break;
1832 switch (tclass)
1834 case tcc_type:
1835 if (alias_template_specialization_p (arg))
1837 // We want an alias specialization that survived strip_typedefs
1838 // to hash differently from its TYPE_CANONICAL, to avoid hash
1839 // collisions that compare as different in template_args_equal.
1840 // These could be dependent specializations that strip_typedefs
1841 // left alone, or untouched specializations because
1842 // coerce_template_parms returns the unconverted template
1843 // arguments if it sees incomplete argument packs.
1844 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1845 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1847 if (TYPE_CANONICAL (arg))
1848 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1849 val);
1850 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1851 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1852 /* Otherwise just compare the types during lookup. */
1853 return val;
1855 case tcc_declaration:
1856 case tcc_constant:
1857 return iterative_hash_expr (arg, val);
1859 default:
1860 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1862 unsigned n = cp_tree_operand_length (arg);
1863 for (i = 0; i < n; ++i)
1864 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1865 return val;
1868 gcc_unreachable ();
1869 return 0;
1872 /* Unregister the specialization SPEC as a specialization of TMPL.
1873 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1874 if the SPEC was listed as a specialization of TMPL.
1876 Note that SPEC has been ggc_freed, so we can't look inside it. */
1878 bool
1879 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1881 spec_entry *entry;
1882 spec_entry elt;
1884 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1885 elt.args = TI_ARGS (tinfo);
1886 elt.spec = NULL_TREE;
1888 entry = decl_specializations->find (&elt);
1889 if (entry != NULL)
1891 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1892 gcc_assert (new_spec != NULL_TREE);
1893 entry->spec = new_spec;
1894 return 1;
1897 return 0;
1900 /* Like register_specialization, but for local declarations. We are
1901 registering SPEC, an instantiation of TMPL. */
1903 void
1904 register_local_specialization (tree spec, tree tmpl)
1906 local_specializations->put (tmpl, spec);
1909 /* TYPE is a class type. Returns true if TYPE is an explicitly
1910 specialized class. */
1912 bool
1913 explicit_class_specialization_p (tree type)
1915 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1916 return false;
1917 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1920 /* Print the list of functions at FNS, going through all the overloads
1921 for each element of the list. Alternatively, FNS can not be a
1922 TREE_LIST, in which case it will be printed together with all the
1923 overloads.
1925 MORE and *STR should respectively be FALSE and NULL when the function
1926 is called from the outside. They are used internally on recursive
1927 calls. print_candidates manages the two parameters and leaves NULL
1928 in *STR when it ends. */
1930 static void
1931 print_candidates_1 (tree fns, char **str, bool more = false)
1933 if (TREE_CODE (fns) == TREE_LIST)
1934 for (; fns; fns = TREE_CHAIN (fns))
1935 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1936 else
1937 for (lkp_iterator iter (fns); iter;)
1939 tree cand = *iter;
1940 ++iter;
1942 const char *pfx = *str;
1943 if (!pfx)
1945 if (more || iter)
1946 pfx = _("candidates are:");
1947 else
1948 pfx = _("candidate is:");
1949 *str = get_spaces (pfx);
1951 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1955 /* Print the list of candidate FNS in an error message. FNS can also
1956 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1958 void
1959 print_candidates (tree fns)
1961 char *str = NULL;
1962 print_candidates_1 (fns, &str);
1963 free (str);
1966 /* Get a (possibly) constrained template declaration for the
1967 purpose of ordering candidates. */
1968 static tree
1969 get_template_for_ordering (tree list)
1971 gcc_assert (TREE_CODE (list) == TREE_LIST);
1972 tree f = TREE_VALUE (list);
1973 if (tree ti = DECL_TEMPLATE_INFO (f))
1974 return TI_TEMPLATE (ti);
1975 return f;
1978 /* Among candidates having the same signature, return the
1979 most constrained or NULL_TREE if there is no best candidate.
1980 If the signatures of candidates vary (e.g., template
1981 specialization vs. member function), then there can be no
1982 most constrained.
1984 Note that we don't compare constraints on the functions
1985 themselves, but rather those of their templates. */
1986 static tree
1987 most_constrained_function (tree candidates)
1989 // Try to find the best candidate in a first pass.
1990 tree champ = candidates;
1991 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1993 int winner = more_constrained (get_template_for_ordering (champ),
1994 get_template_for_ordering (c));
1995 if (winner == -1)
1996 champ = c; // The candidate is more constrained
1997 else if (winner == 0)
1998 return NULL_TREE; // Neither is more constrained
2001 // Verify that the champ is better than previous candidates.
2002 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2003 if (!more_constrained (get_template_for_ordering (champ),
2004 get_template_for_ordering (c)))
2005 return NULL_TREE;
2008 return champ;
2012 /* Returns the template (one of the functions given by TEMPLATE_ID)
2013 which can be specialized to match the indicated DECL with the
2014 explicit template args given in TEMPLATE_ID. The DECL may be
2015 NULL_TREE if none is available. In that case, the functions in
2016 TEMPLATE_ID are non-members.
2018 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2019 specialization of a member template.
2021 The TEMPLATE_COUNT is the number of references to qualifying
2022 template classes that appeared in the name of the function. See
2023 check_explicit_specialization for a more accurate description.
2025 TSK indicates what kind of template declaration (if any) is being
2026 declared. TSK_TEMPLATE indicates that the declaration given by
2027 DECL, though a FUNCTION_DECL, has template parameters, and is
2028 therefore a template function.
2030 The template args (those explicitly specified and those deduced)
2031 are output in a newly created vector *TARGS_OUT.
2033 If it is impossible to determine the result, an error message is
2034 issued. The error_mark_node is returned to indicate failure. */
2036 static tree
2037 determine_specialization (tree template_id,
2038 tree decl,
2039 tree* targs_out,
2040 int need_member_template,
2041 int template_count,
2042 tmpl_spec_kind tsk)
2044 tree fns;
2045 tree targs;
2046 tree explicit_targs;
2047 tree candidates = NULL_TREE;
2049 /* A TREE_LIST of templates of which DECL may be a specialization.
2050 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2051 corresponding TREE_PURPOSE is the set of template arguments that,
2052 when used to instantiate the template, would produce a function
2053 with the signature of DECL. */
2054 tree templates = NULL_TREE;
2055 int header_count;
2056 cp_binding_level *b;
2058 *targs_out = NULL_TREE;
2060 if (template_id == error_mark_node || decl == error_mark_node)
2061 return error_mark_node;
2063 /* We shouldn't be specializing a member template of an
2064 unspecialized class template; we already gave an error in
2065 check_specialization_scope, now avoid crashing. */
2066 if (template_count && DECL_CLASS_SCOPE_P (decl)
2067 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2069 gcc_assert (errorcount);
2070 return error_mark_node;
2073 fns = TREE_OPERAND (template_id, 0);
2074 explicit_targs = TREE_OPERAND (template_id, 1);
2076 if (fns == error_mark_node)
2077 return error_mark_node;
2079 /* Check for baselinks. */
2080 if (BASELINK_P (fns))
2081 fns = BASELINK_FUNCTIONS (fns);
2083 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2085 error ("%qD is not a function template", fns);
2086 return error_mark_node;
2088 else if (VAR_P (decl) && !variable_template_p (fns))
2090 error ("%qD is not a variable template", fns);
2091 return error_mark_node;
2094 /* Count the number of template headers specified for this
2095 specialization. */
2096 header_count = 0;
2097 for (b = current_binding_level;
2098 b->kind == sk_template_parms;
2099 b = b->level_chain)
2100 ++header_count;
2102 tree orig_fns = fns;
2104 if (variable_template_p (fns))
2106 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2107 targs = coerce_template_parms (parms, explicit_targs, fns,
2108 tf_warning_or_error,
2109 /*req_all*/true, /*use_defarg*/true);
2110 if (targs != error_mark_node)
2111 templates = tree_cons (targs, fns, templates);
2113 else for (lkp_iterator iter (fns); iter; ++iter)
2115 tree fn = *iter;
2117 if (TREE_CODE (fn) == TEMPLATE_DECL)
2119 tree decl_arg_types;
2120 tree fn_arg_types;
2121 tree insttype;
2123 /* In case of explicit specialization, we need to check if
2124 the number of template headers appearing in the specialization
2125 is correct. This is usually done in check_explicit_specialization,
2126 but the check done there cannot be exhaustive when specializing
2127 member functions. Consider the following code:
2129 template <> void A<int>::f(int);
2130 template <> template <> void A<int>::f(int);
2132 Assuming that A<int> is not itself an explicit specialization
2133 already, the first line specializes "f" which is a non-template
2134 member function, whilst the second line specializes "f" which
2135 is a template member function. So both lines are syntactically
2136 correct, and check_explicit_specialization does not reject
2137 them.
2139 Here, we can do better, as we are matching the specialization
2140 against the declarations. We count the number of template
2141 headers, and we check if they match TEMPLATE_COUNT + 1
2142 (TEMPLATE_COUNT is the number of qualifying template classes,
2143 plus there must be another header for the member template
2144 itself).
2146 Notice that if header_count is zero, this is not a
2147 specialization but rather a template instantiation, so there
2148 is no check we can perform here. */
2149 if (header_count && header_count != template_count + 1)
2150 continue;
2152 /* Check that the number of template arguments at the
2153 innermost level for DECL is the same as for FN. */
2154 if (current_binding_level->kind == sk_template_parms
2155 && !current_binding_level->explicit_spec_p
2156 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2157 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2158 (current_template_parms))))
2159 continue;
2161 /* DECL might be a specialization of FN. */
2162 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2163 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2165 /* For a non-static member function, we need to make sure
2166 that the const qualification is the same. Since
2167 get_bindings does not try to merge the "this" parameter,
2168 we must do the comparison explicitly. */
2169 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2170 && !same_type_p (TREE_VALUE (fn_arg_types),
2171 TREE_VALUE (decl_arg_types)))
2172 continue;
2174 /* Skip the "this" parameter and, for constructors of
2175 classes with virtual bases, the VTT parameter. A
2176 full specialization of a constructor will have a VTT
2177 parameter, but a template never will. */
2178 decl_arg_types
2179 = skip_artificial_parms_for (decl, decl_arg_types);
2180 fn_arg_types
2181 = skip_artificial_parms_for (fn, fn_arg_types);
2183 /* Function templates cannot be specializations; there are
2184 no partial specializations of functions. Therefore, if
2185 the type of DECL does not match FN, there is no
2186 match.
2188 Note that it should never be the case that we have both
2189 candidates added here, and for regular member functions
2190 below. */
2191 if (tsk == tsk_template)
2193 if (compparms (fn_arg_types, decl_arg_types))
2194 candidates = tree_cons (NULL_TREE, fn, candidates);
2195 continue;
2198 /* See whether this function might be a specialization of this
2199 template. Suppress access control because we might be trying
2200 to make this specialization a friend, and we have already done
2201 access control for the declaration of the specialization. */
2202 push_deferring_access_checks (dk_no_check);
2203 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2204 pop_deferring_access_checks ();
2206 if (!targs)
2207 /* We cannot deduce template arguments that when used to
2208 specialize TMPL will produce DECL. */
2209 continue;
2211 /* Remove, from the set of candidates, all those functions
2212 whose constraints are not satisfied. */
2213 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2214 continue;
2216 // Then, try to form the new function type.
2217 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2218 if (insttype == error_mark_node)
2219 continue;
2220 fn_arg_types
2221 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2222 if (!compparms (fn_arg_types, decl_arg_types))
2223 continue;
2225 /* Save this template, and the arguments deduced. */
2226 templates = tree_cons (targs, fn, templates);
2228 else if (need_member_template)
2229 /* FN is an ordinary member function, and we need a
2230 specialization of a member template. */
2232 else if (TREE_CODE (fn) != FUNCTION_DECL)
2233 /* We can get IDENTIFIER_NODEs here in certain erroneous
2234 cases. */
2236 else if (!DECL_FUNCTION_MEMBER_P (fn))
2237 /* This is just an ordinary non-member function. Nothing can
2238 be a specialization of that. */
2240 else if (DECL_ARTIFICIAL (fn))
2241 /* Cannot specialize functions that are created implicitly. */
2243 else
2245 tree decl_arg_types;
2247 /* This is an ordinary member function. However, since
2248 we're here, we can assume its enclosing class is a
2249 template class. For example,
2251 template <typename T> struct S { void f(); };
2252 template <> void S<int>::f() {}
2254 Here, S<int>::f is a non-template, but S<int> is a
2255 template class. If FN has the same type as DECL, we
2256 might be in business. */
2258 if (!DECL_TEMPLATE_INFO (fn))
2259 /* Its enclosing class is an explicit specialization
2260 of a template class. This is not a candidate. */
2261 continue;
2263 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2264 TREE_TYPE (TREE_TYPE (fn))))
2265 /* The return types differ. */
2266 continue;
2268 /* Adjust the type of DECL in case FN is a static member. */
2269 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2270 if (DECL_STATIC_FUNCTION_P (fn)
2271 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2272 decl_arg_types = TREE_CHAIN (decl_arg_types);
2274 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2275 decl_arg_types))
2276 continue;
2278 // If the deduced arguments do not satisfy the constraints,
2279 // this is not a candidate.
2280 if (flag_concepts && !constraints_satisfied_p (fn))
2281 continue;
2283 // Add the candidate.
2284 candidates = tree_cons (NULL_TREE, fn, candidates);
2288 if (templates && TREE_CHAIN (templates))
2290 /* We have:
2292 [temp.expl.spec]
2294 It is possible for a specialization with a given function
2295 signature to be instantiated from more than one function
2296 template. In such cases, explicit specification of the
2297 template arguments must be used to uniquely identify the
2298 function template specialization being specialized.
2300 Note that here, there's no suggestion that we're supposed to
2301 determine which of the candidate templates is most
2302 specialized. However, we, also have:
2304 [temp.func.order]
2306 Partial ordering of overloaded function template
2307 declarations is used in the following contexts to select
2308 the function template to which a function template
2309 specialization refers:
2311 -- when an explicit specialization refers to a function
2312 template.
2314 So, we do use the partial ordering rules, at least for now.
2315 This extension can only serve to make invalid programs valid,
2316 so it's safe. And, there is strong anecdotal evidence that
2317 the committee intended the partial ordering rules to apply;
2318 the EDG front end has that behavior, and John Spicer claims
2319 that the committee simply forgot to delete the wording in
2320 [temp.expl.spec]. */
2321 tree tmpl = most_specialized_instantiation (templates);
2322 if (tmpl != error_mark_node)
2324 templates = tmpl;
2325 TREE_CHAIN (templates) = NULL_TREE;
2329 // Concepts allows multiple declarations of member functions
2330 // with the same signature. Like above, we need to rely on
2331 // on the partial ordering of those candidates to determine which
2332 // is the best.
2333 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2335 if (tree cand = most_constrained_function (candidates))
2337 candidates = cand;
2338 TREE_CHAIN (cand) = NULL_TREE;
2342 if (templates == NULL_TREE && candidates == NULL_TREE)
2344 error ("template-id %qD for %q+D does not match any template "
2345 "declaration", template_id, decl);
2346 if (header_count && header_count != template_count + 1)
2347 inform (input_location, "saw %d %<template<>%>, need %d for "
2348 "specializing a member function template",
2349 header_count, template_count + 1);
2350 else
2351 print_candidates (orig_fns);
2352 return error_mark_node;
2354 else if ((templates && TREE_CHAIN (templates))
2355 || (candidates && TREE_CHAIN (candidates))
2356 || (templates && candidates))
2358 error ("ambiguous template specialization %qD for %q+D",
2359 template_id, decl);
2360 candidates = chainon (candidates, templates);
2361 print_candidates (candidates);
2362 return error_mark_node;
2365 /* We have one, and exactly one, match. */
2366 if (candidates)
2368 tree fn = TREE_VALUE (candidates);
2369 *targs_out = copy_node (DECL_TI_ARGS (fn));
2371 // Propagate the candidate's constraints to the declaration.
2372 set_constraints (decl, get_constraints (fn));
2374 /* DECL is a re-declaration or partial instantiation of a template
2375 function. */
2376 if (TREE_CODE (fn) == TEMPLATE_DECL)
2377 return fn;
2378 /* It was a specialization of an ordinary member function in a
2379 template class. */
2380 return DECL_TI_TEMPLATE (fn);
2383 /* It was a specialization of a template. */
2384 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2385 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2387 *targs_out = copy_node (targs);
2388 SET_TMPL_ARGS_LEVEL (*targs_out,
2389 TMPL_ARGS_DEPTH (*targs_out),
2390 TREE_PURPOSE (templates));
2392 else
2393 *targs_out = TREE_PURPOSE (templates);
2394 return TREE_VALUE (templates);
2397 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2398 but with the default argument values filled in from those in the
2399 TMPL_TYPES. */
2401 static tree
2402 copy_default_args_to_explicit_spec_1 (tree spec_types,
2403 tree tmpl_types)
2405 tree new_spec_types;
2407 if (!spec_types)
2408 return NULL_TREE;
2410 if (spec_types == void_list_node)
2411 return void_list_node;
2413 /* Substitute into the rest of the list. */
2414 new_spec_types =
2415 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2416 TREE_CHAIN (tmpl_types));
2418 /* Add the default argument for this parameter. */
2419 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2420 TREE_VALUE (spec_types),
2421 new_spec_types);
2424 /* DECL is an explicit specialization. Replicate default arguments
2425 from the template it specializes. (That way, code like:
2427 template <class T> void f(T = 3);
2428 template <> void f(double);
2429 void g () { f (); }
2431 works, as required.) An alternative approach would be to look up
2432 the correct default arguments at the call-site, but this approach
2433 is consistent with how implicit instantiations are handled. */
2435 static void
2436 copy_default_args_to_explicit_spec (tree decl)
2438 tree tmpl;
2439 tree spec_types;
2440 tree tmpl_types;
2441 tree new_spec_types;
2442 tree old_type;
2443 tree new_type;
2444 tree t;
2445 tree object_type = NULL_TREE;
2446 tree in_charge = NULL_TREE;
2447 tree vtt = NULL_TREE;
2449 /* See if there's anything we need to do. */
2450 tmpl = DECL_TI_TEMPLATE (decl);
2451 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2452 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2453 if (TREE_PURPOSE (t))
2454 break;
2455 if (!t)
2456 return;
2458 old_type = TREE_TYPE (decl);
2459 spec_types = TYPE_ARG_TYPES (old_type);
2461 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2463 /* Remove the this pointer, but remember the object's type for
2464 CV quals. */
2465 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2466 spec_types = TREE_CHAIN (spec_types);
2467 tmpl_types = TREE_CHAIN (tmpl_types);
2469 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2471 /* DECL may contain more parameters than TMPL due to the extra
2472 in-charge parameter in constructors and destructors. */
2473 in_charge = spec_types;
2474 spec_types = TREE_CHAIN (spec_types);
2476 if (DECL_HAS_VTT_PARM_P (decl))
2478 vtt = spec_types;
2479 spec_types = TREE_CHAIN (spec_types);
2483 /* Compute the merged default arguments. */
2484 new_spec_types =
2485 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2487 /* Compute the new FUNCTION_TYPE. */
2488 if (object_type)
2490 if (vtt)
2491 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2492 TREE_VALUE (vtt),
2493 new_spec_types);
2495 if (in_charge)
2496 /* Put the in-charge parameter back. */
2497 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2498 TREE_VALUE (in_charge),
2499 new_spec_types);
2501 new_type = build_method_type_directly (object_type,
2502 TREE_TYPE (old_type),
2503 new_spec_types);
2505 else
2506 new_type = build_function_type (TREE_TYPE (old_type),
2507 new_spec_types);
2508 new_type = cp_build_type_attribute_variant (new_type,
2509 TYPE_ATTRIBUTES (old_type));
2510 new_type = build_exception_variant (new_type,
2511 TYPE_RAISES_EXCEPTIONS (old_type));
2513 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2514 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2516 TREE_TYPE (decl) = new_type;
2519 /* Return the number of template headers we expect to see for a definition
2520 or specialization of CTYPE or one of its non-template members. */
2523 num_template_headers_for_class (tree ctype)
2525 int num_templates = 0;
2527 while (ctype && CLASS_TYPE_P (ctype))
2529 /* You're supposed to have one `template <...>' for every
2530 template class, but you don't need one for a full
2531 specialization. For example:
2533 template <class T> struct S{};
2534 template <> struct S<int> { void f(); };
2535 void S<int>::f () {}
2537 is correct; there shouldn't be a `template <>' for the
2538 definition of `S<int>::f'. */
2539 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2540 /* If CTYPE does not have template information of any
2541 kind, then it is not a template, nor is it nested
2542 within a template. */
2543 break;
2544 if (explicit_class_specialization_p (ctype))
2545 break;
2546 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2547 ++num_templates;
2549 ctype = TYPE_CONTEXT (ctype);
2552 return num_templates;
2555 /* Do a simple sanity check on the template headers that precede the
2556 variable declaration DECL. */
2558 void
2559 check_template_variable (tree decl)
2561 tree ctx = CP_DECL_CONTEXT (decl);
2562 int wanted = num_template_headers_for_class (ctx);
2563 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2564 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2566 if (cxx_dialect < cxx14)
2567 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2568 "variable templates only available with "
2569 "-std=c++14 or -std=gnu++14");
2571 // Namespace-scope variable templates should have a template header.
2572 ++wanted;
2574 if (template_header_count > wanted)
2576 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2577 "too many template headers for %qD "
2578 "(should be %d)",
2579 decl, wanted);
2580 if (warned && CLASS_TYPE_P (ctx)
2581 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2582 inform (DECL_SOURCE_LOCATION (decl),
2583 "members of an explicitly specialized class are defined "
2584 "without a template header");
2588 /* An explicit specialization whose declarator-id or class-head-name is not
2589 qualified shall be declared in the nearest enclosing namespace of the
2590 template, or, if the namespace is inline (7.3.1), any namespace from its
2591 enclosing namespace set.
2593 If the name declared in the explicit instantiation is an unqualified name,
2594 the explicit instantiation shall appear in the namespace where its template
2595 is declared or, if that namespace is inline (7.3.1), any namespace from its
2596 enclosing namespace set. */
2598 void
2599 check_unqualified_spec_or_inst (tree t, location_t loc)
2601 tree tmpl = most_general_template (t);
2602 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2603 && !is_nested_namespace (current_namespace,
2604 CP_DECL_CONTEXT (tmpl), true))
2606 if (processing_specialization)
2607 permerror (loc, "explicit specialization of %qD outside its "
2608 "namespace must use a nested-name-specifier", tmpl);
2609 else if (processing_explicit_instantiation
2610 && cxx_dialect >= cxx11)
2611 /* This was allowed in C++98, so only pedwarn. */
2612 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2613 "outside its namespace must use a nested-name-"
2614 "specifier", tmpl);
2618 /* Check to see if the function just declared, as indicated in
2619 DECLARATOR, and in DECL, is a specialization of a function
2620 template. We may also discover that the declaration is an explicit
2621 instantiation at this point.
2623 Returns DECL, or an equivalent declaration that should be used
2624 instead if all goes well. Issues an error message if something is
2625 amiss. Returns error_mark_node if the error is not easily
2626 recoverable.
2628 FLAGS is a bitmask consisting of the following flags:
2630 2: The function has a definition.
2631 4: The function is a friend.
2633 The TEMPLATE_COUNT is the number of references to qualifying
2634 template classes that appeared in the name of the function. For
2635 example, in
2637 template <class T> struct S { void f(); };
2638 void S<int>::f();
2640 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2641 classes are not counted in the TEMPLATE_COUNT, so that in
2643 template <class T> struct S {};
2644 template <> struct S<int> { void f(); }
2645 template <> void S<int>::f();
2647 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2648 invalid; there should be no template <>.)
2650 If the function is a specialization, it is marked as such via
2651 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2652 is set up correctly, and it is added to the list of specializations
2653 for that template. */
2655 tree
2656 check_explicit_specialization (tree declarator,
2657 tree decl,
2658 int template_count,
2659 int flags)
2661 int have_def = flags & 2;
2662 int is_friend = flags & 4;
2663 bool is_concept = flags & 8;
2664 int specialization = 0;
2665 int explicit_instantiation = 0;
2666 int member_specialization = 0;
2667 tree ctype = DECL_CLASS_CONTEXT (decl);
2668 tree dname = DECL_NAME (decl);
2669 tmpl_spec_kind tsk;
2671 if (is_friend)
2673 if (!processing_specialization)
2674 tsk = tsk_none;
2675 else
2676 tsk = tsk_excessive_parms;
2678 else
2679 tsk = current_tmpl_spec_kind (template_count);
2681 switch (tsk)
2683 case tsk_none:
2684 if (processing_specialization && !VAR_P (decl))
2686 specialization = 1;
2687 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2689 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2691 if (is_friend)
2692 /* This could be something like:
2694 template <class T> void f(T);
2695 class S { friend void f<>(int); } */
2696 specialization = 1;
2697 else
2699 /* This case handles bogus declarations like template <>
2700 template <class T> void f<int>(); */
2702 error ("template-id %qD in declaration of primary template",
2703 declarator);
2704 return decl;
2707 break;
2709 case tsk_invalid_member_spec:
2710 /* The error has already been reported in
2711 check_specialization_scope. */
2712 return error_mark_node;
2714 case tsk_invalid_expl_inst:
2715 error ("template parameter list used in explicit instantiation");
2717 /* Fall through. */
2719 case tsk_expl_inst:
2720 if (have_def)
2721 error ("definition provided for explicit instantiation");
2723 explicit_instantiation = 1;
2724 break;
2726 case tsk_excessive_parms:
2727 case tsk_insufficient_parms:
2728 if (tsk == tsk_excessive_parms)
2729 error ("too many template parameter lists in declaration of %qD",
2730 decl);
2731 else if (template_header_count)
2732 error("too few template parameter lists in declaration of %qD", decl);
2733 else
2734 error("explicit specialization of %qD must be introduced by "
2735 "%<template <>%>", decl);
2737 /* Fall through. */
2738 case tsk_expl_spec:
2739 if (is_concept)
2740 error ("explicit specialization declared %<concept%>");
2742 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2743 /* In cases like template<> constexpr bool v = true;
2744 We'll give an error in check_template_variable. */
2745 break;
2747 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2748 if (ctype)
2749 member_specialization = 1;
2750 else
2751 specialization = 1;
2752 break;
2754 case tsk_template:
2755 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2757 /* This case handles bogus declarations like template <>
2758 template <class T> void f<int>(); */
2760 if (!uses_template_parms (declarator))
2761 error ("template-id %qD in declaration of primary template",
2762 declarator);
2763 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2765 /* Partial specialization of variable template. */
2766 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2767 specialization = 1;
2768 goto ok;
2770 else if (cxx_dialect < cxx14)
2771 error ("non-type partial specialization %qD "
2772 "is not allowed", declarator);
2773 else
2774 error ("non-class, non-variable partial specialization %qD "
2775 "is not allowed", declarator);
2776 return decl;
2777 ok:;
2780 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2781 /* This is a specialization of a member template, without
2782 specialization the containing class. Something like:
2784 template <class T> struct S {
2785 template <class U> void f (U);
2787 template <> template <class U> void S<int>::f(U) {}
2789 That's a specialization -- but of the entire template. */
2790 specialization = 1;
2791 break;
2793 default:
2794 gcc_unreachable ();
2797 if ((specialization || member_specialization)
2798 /* This doesn't apply to variable templates. */
2799 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2800 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2802 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2803 for (; t; t = TREE_CHAIN (t))
2804 if (TREE_PURPOSE (t))
2806 permerror (input_location,
2807 "default argument specified in explicit specialization");
2808 break;
2812 if (specialization || member_specialization || explicit_instantiation)
2814 tree tmpl = NULL_TREE;
2815 tree targs = NULL_TREE;
2816 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2818 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2819 if (!was_template_id)
2821 tree fns;
2823 gcc_assert (identifier_p (declarator));
2824 if (ctype)
2825 fns = dname;
2826 else
2828 /* If there is no class context, the explicit instantiation
2829 must be at namespace scope. */
2830 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2832 /* Find the namespace binding, using the declaration
2833 context. */
2834 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2835 false, true);
2836 if (fns == error_mark_node)
2837 /* If lookup fails, look for a friend declaration so we can
2838 give a better diagnostic. */
2839 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2840 /*type*/false, /*complain*/true,
2841 /*hidden*/true);
2843 if (fns == error_mark_node || !is_overloaded_fn (fns))
2845 error ("%qD is not a template function", dname);
2846 fns = error_mark_node;
2850 declarator = lookup_template_function (fns, NULL_TREE);
2853 if (declarator == error_mark_node)
2854 return error_mark_node;
2856 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2858 if (!explicit_instantiation)
2859 /* A specialization in class scope. This is invalid,
2860 but the error will already have been flagged by
2861 check_specialization_scope. */
2862 return error_mark_node;
2863 else
2865 /* It's not valid to write an explicit instantiation in
2866 class scope, e.g.:
2868 class C { template void f(); }
2870 This case is caught by the parser. However, on
2871 something like:
2873 template class C { void f(); };
2875 (which is invalid) we can get here. The error will be
2876 issued later. */
2880 return decl;
2882 else if (ctype != NULL_TREE
2883 && (identifier_p (TREE_OPERAND (declarator, 0))))
2885 // We'll match variable templates in start_decl.
2886 if (VAR_P (decl))
2887 return decl;
2889 /* Find the list of functions in ctype that have the same
2890 name as the declared function. */
2891 tree name = TREE_OPERAND (declarator, 0);
2893 if (constructor_name_p (name, ctype))
2895 if (DECL_CONSTRUCTOR_P (decl)
2896 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2897 : !CLASSTYPE_DESTRUCTOR (ctype))
2899 /* From [temp.expl.spec]:
2901 If such an explicit specialization for the member
2902 of a class template names an implicitly-declared
2903 special member function (clause _special_), the
2904 program is ill-formed.
2906 Similar language is found in [temp.explicit]. */
2907 error ("specialization of implicitly-declared special member function");
2908 return error_mark_node;
2911 name = DECL_NAME (decl);
2914 /* For a type-conversion operator, We might be looking for
2915 `operator int' which will be a specialization of
2916 `operator T'. Grab all the conversion operators, and
2917 then select from them. */
2918 tree fns = lookup_fnfields_slot_nolazy (ctype,
2919 IDENTIFIER_CONV_OP_P (name)
2920 ? conv_op_identifier : name);
2922 if (fns == NULL_TREE)
2924 error ("no member function %qD declared in %qT", name, ctype);
2925 return error_mark_node;
2927 else
2928 TREE_OPERAND (declarator, 0) = fns;
2931 /* Figure out what exactly is being specialized at this point.
2932 Note that for an explicit instantiation, even one for a
2933 member function, we cannot tell a priori whether the
2934 instantiation is for a member template, or just a member
2935 function of a template class. Even if a member template is
2936 being instantiated, the member template arguments may be
2937 elided if they can be deduced from the rest of the
2938 declaration. */
2939 tmpl = determine_specialization (declarator, decl,
2940 &targs,
2941 member_specialization,
2942 template_count,
2943 tsk);
2945 if (!tmpl || tmpl == error_mark_node)
2946 /* We couldn't figure out what this declaration was
2947 specializing. */
2948 return error_mark_node;
2949 else
2951 if (TREE_CODE (decl) == FUNCTION_DECL
2952 && DECL_HIDDEN_FRIEND_P (tmpl))
2954 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2955 "friend declaration %qD is not visible to "
2956 "explicit specialization", tmpl))
2957 inform (DECL_SOURCE_LOCATION (tmpl),
2958 "friend declaration here");
2960 else if (!ctype && !is_friend
2961 && CP_DECL_CONTEXT (decl) == current_namespace)
2962 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2964 tree gen_tmpl = most_general_template (tmpl);
2966 if (explicit_instantiation)
2968 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2969 is done by do_decl_instantiation later. */
2971 int arg_depth = TMPL_ARGS_DEPTH (targs);
2972 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2974 if (arg_depth > parm_depth)
2976 /* If TMPL is not the most general template (for
2977 example, if TMPL is a friend template that is
2978 injected into namespace scope), then there will
2979 be too many levels of TARGS. Remove some of them
2980 here. */
2981 int i;
2982 tree new_targs;
2984 new_targs = make_tree_vec (parm_depth);
2985 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2986 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2987 = TREE_VEC_ELT (targs, i);
2988 targs = new_targs;
2991 return instantiate_template (tmpl, targs, tf_error);
2994 /* If we thought that the DECL was a member function, but it
2995 turns out to be specializing a static member function,
2996 make DECL a static member function as well. */
2997 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
2998 && DECL_STATIC_FUNCTION_P (tmpl)
2999 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3000 revert_static_member_fn (decl);
3002 /* If this is a specialization of a member template of a
3003 template class, we want to return the TEMPLATE_DECL, not
3004 the specialization of it. */
3005 if (tsk == tsk_template && !was_template_id)
3007 tree result = DECL_TEMPLATE_RESULT (tmpl);
3008 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3009 DECL_INITIAL (result) = NULL_TREE;
3010 if (have_def)
3012 tree parm;
3013 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3014 DECL_SOURCE_LOCATION (result)
3015 = DECL_SOURCE_LOCATION (decl);
3016 /* We want to use the argument list specified in the
3017 definition, not in the original declaration. */
3018 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3019 for (parm = DECL_ARGUMENTS (result); parm;
3020 parm = DECL_CHAIN (parm))
3021 DECL_CONTEXT (parm) = result;
3023 return register_specialization (tmpl, gen_tmpl, targs,
3024 is_friend, 0);
3027 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3028 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3030 if (was_template_id)
3031 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3033 /* Inherit default function arguments from the template
3034 DECL is specializing. */
3035 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3036 copy_default_args_to_explicit_spec (decl);
3038 /* This specialization has the same protection as the
3039 template it specializes. */
3040 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3041 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3043 /* 7.1.1-1 [dcl.stc]
3045 A storage-class-specifier shall not be specified in an
3046 explicit specialization...
3048 The parser rejects these, so unless action is taken here,
3049 explicit function specializations will always appear with
3050 global linkage.
3052 The action recommended by the C++ CWG in response to C++
3053 defect report 605 is to make the storage class and linkage
3054 of the explicit specialization match the templated function:
3056 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3058 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3060 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3061 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3063 /* A concept cannot be specialized. */
3064 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3066 error ("explicit specialization of function concept %qD",
3067 gen_tmpl);
3068 return error_mark_node;
3071 /* This specialization has the same linkage and visibility as
3072 the function template it specializes. */
3073 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3074 if (! TREE_PUBLIC (decl))
3076 DECL_INTERFACE_KNOWN (decl) = 1;
3077 DECL_NOT_REALLY_EXTERN (decl) = 1;
3079 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3080 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3082 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3083 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3087 /* If DECL is a friend declaration, declared using an
3088 unqualified name, the namespace associated with DECL may
3089 have been set incorrectly. For example, in:
3091 template <typename T> void f(T);
3092 namespace N {
3093 struct S { friend void f<int>(int); }
3096 we will have set the DECL_CONTEXT for the friend
3097 declaration to N, rather than to the global namespace. */
3098 if (DECL_NAMESPACE_SCOPE_P (decl))
3099 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3101 if (is_friend && !have_def)
3102 /* This is not really a declaration of a specialization.
3103 It's just the name of an instantiation. But, it's not
3104 a request for an instantiation, either. */
3105 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3106 else if (TREE_CODE (decl) == FUNCTION_DECL)
3107 /* A specialization is not necessarily COMDAT. */
3108 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3109 && DECL_DECLARED_INLINE_P (decl));
3110 else if (VAR_P (decl))
3111 DECL_COMDAT (decl) = false;
3113 /* If this is a full specialization, register it so that we can find
3114 it again. Partial specializations will be registered in
3115 process_partial_specialization. */
3116 if (!processing_template_decl)
3117 decl = register_specialization (decl, gen_tmpl, targs,
3118 is_friend, 0);
3120 /* A 'structor should already have clones. */
3121 gcc_assert (decl == error_mark_node
3122 || variable_template_p (tmpl)
3123 || !(DECL_CONSTRUCTOR_P (decl)
3124 || DECL_DESTRUCTOR_P (decl))
3125 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3129 return decl;
3132 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3133 parameters. These are represented in the same format used for
3134 DECL_TEMPLATE_PARMS. */
3137 comp_template_parms (const_tree parms1, const_tree parms2)
3139 const_tree p1;
3140 const_tree p2;
3142 if (parms1 == parms2)
3143 return 1;
3145 for (p1 = parms1, p2 = parms2;
3146 p1 != NULL_TREE && p2 != NULL_TREE;
3147 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3149 tree t1 = TREE_VALUE (p1);
3150 tree t2 = TREE_VALUE (p2);
3151 int i;
3153 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3154 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3156 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3157 return 0;
3159 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3161 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3162 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3164 /* If either of the template parameters are invalid, assume
3165 they match for the sake of error recovery. */
3166 if (error_operand_p (parm1) || error_operand_p (parm2))
3167 return 1;
3169 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3170 return 0;
3172 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3173 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3174 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3175 continue;
3176 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3177 return 0;
3181 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3182 /* One set of parameters has more parameters lists than the
3183 other. */
3184 return 0;
3186 return 1;
3189 /* Determine whether PARM is a parameter pack. */
3191 bool
3192 template_parameter_pack_p (const_tree parm)
3194 /* Determine if we have a non-type template parameter pack. */
3195 if (TREE_CODE (parm) == PARM_DECL)
3196 return (DECL_TEMPLATE_PARM_P (parm)
3197 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3198 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3199 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3201 /* If this is a list of template parameters, we could get a
3202 TYPE_DECL or a TEMPLATE_DECL. */
3203 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3204 parm = TREE_TYPE (parm);
3206 /* Otherwise it must be a type template parameter. */
3207 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3208 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3209 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3212 /* Determine if T is a function parameter pack. */
3214 bool
3215 function_parameter_pack_p (const_tree t)
3217 if (t && TREE_CODE (t) == PARM_DECL)
3218 return DECL_PACK_P (t);
3219 return false;
3222 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3223 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3225 tree
3226 get_function_template_decl (const_tree primary_func_tmpl_inst)
3228 if (! primary_func_tmpl_inst
3229 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3230 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3231 return NULL;
3233 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3236 /* Return true iff the function parameter PARAM_DECL was expanded
3237 from the function parameter pack PACK. */
3239 bool
3240 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3242 if (DECL_ARTIFICIAL (param_decl)
3243 || !function_parameter_pack_p (pack))
3244 return false;
3246 /* The parameter pack and its pack arguments have the same
3247 DECL_PARM_INDEX. */
3248 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3251 /* Determine whether ARGS describes a variadic template args list,
3252 i.e., one that is terminated by a template argument pack. */
3254 static bool
3255 template_args_variadic_p (tree args)
3257 int nargs;
3258 tree last_parm;
3260 if (args == NULL_TREE)
3261 return false;
3263 args = INNERMOST_TEMPLATE_ARGS (args);
3264 nargs = TREE_VEC_LENGTH (args);
3266 if (nargs == 0)
3267 return false;
3269 last_parm = TREE_VEC_ELT (args, nargs - 1);
3271 return ARGUMENT_PACK_P (last_parm);
3274 /* Generate a new name for the parameter pack name NAME (an
3275 IDENTIFIER_NODE) that incorporates its */
3277 static tree
3278 make_ith_pack_parameter_name (tree name, int i)
3280 /* Munge the name to include the parameter index. */
3281 #define NUMBUF_LEN 128
3282 char numbuf[NUMBUF_LEN];
3283 char* newname;
3284 int newname_len;
3286 if (name == NULL_TREE)
3287 return name;
3288 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3289 newname_len = IDENTIFIER_LENGTH (name)
3290 + strlen (numbuf) + 2;
3291 newname = (char*)alloca (newname_len);
3292 snprintf (newname, newname_len,
3293 "%s#%i", IDENTIFIER_POINTER (name), i);
3294 return get_identifier (newname);
3297 /* Return true if T is a primary function, class or alias template
3298 instantiation. */
3300 bool
3301 primary_template_instantiation_p (const_tree t)
3303 if (!t)
3304 return false;
3306 if (TREE_CODE (t) == FUNCTION_DECL)
3307 return DECL_LANG_SPECIFIC (t)
3308 && DECL_TEMPLATE_INSTANTIATION (t)
3309 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3310 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3311 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3312 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3313 else if (alias_template_specialization_p (t))
3314 return true;
3315 return false;
3318 /* Return true if PARM is a template template parameter. */
3320 bool
3321 template_template_parameter_p (const_tree parm)
3323 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3326 /* Return true iff PARM is a DECL representing a type template
3327 parameter. */
3329 bool
3330 template_type_parameter_p (const_tree parm)
3332 return (parm
3333 && (TREE_CODE (parm) == TYPE_DECL
3334 || TREE_CODE (parm) == TEMPLATE_DECL)
3335 && DECL_TEMPLATE_PARM_P (parm));
3338 /* Return the template parameters of T if T is a
3339 primary template instantiation, NULL otherwise. */
3341 tree
3342 get_primary_template_innermost_parameters (const_tree t)
3344 tree parms = NULL, template_info = NULL;
3346 if ((template_info = get_template_info (t))
3347 && primary_template_instantiation_p (t))
3348 parms = INNERMOST_TEMPLATE_PARMS
3349 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3351 return parms;
3354 /* Return the template parameters of the LEVELth level from the full list
3355 of template parameters PARMS. */
3357 tree
3358 get_template_parms_at_level (tree parms, int level)
3360 tree p;
3361 if (!parms
3362 || TREE_CODE (parms) != TREE_LIST
3363 || level > TMPL_PARMS_DEPTH (parms))
3364 return NULL_TREE;
3366 for (p = parms; p; p = TREE_CHAIN (p))
3367 if (TMPL_PARMS_DEPTH (p) == level)
3368 return p;
3370 return NULL_TREE;
3373 /* Returns the template arguments of T if T is a template instantiation,
3374 NULL otherwise. */
3376 tree
3377 get_template_innermost_arguments (const_tree t)
3379 tree args = NULL, template_info = NULL;
3381 if ((template_info = get_template_info (t))
3382 && TI_ARGS (template_info))
3383 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3385 return args;
3388 /* Return the argument pack elements of T if T is a template argument pack,
3389 NULL otherwise. */
3391 tree
3392 get_template_argument_pack_elems (const_tree t)
3394 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3395 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3396 return NULL;
3398 return ARGUMENT_PACK_ARGS (t);
3401 /* True iff FN is a function representing a built-in variadic parameter
3402 pack. */
3404 bool
3405 builtin_pack_fn_p (tree fn)
3407 if (!fn
3408 || TREE_CODE (fn) != FUNCTION_DECL
3409 || !DECL_IS_BUILTIN (fn))
3410 return false;
3412 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3413 return true;
3415 return false;
3418 /* True iff CALL is a call to a function representing a built-in variadic
3419 parameter pack. */
3421 static bool
3422 builtin_pack_call_p (tree call)
3424 if (TREE_CODE (call) != CALL_EXPR)
3425 return false;
3426 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3429 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3431 static tree
3432 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3433 tree in_decl)
3435 tree ohi = CALL_EXPR_ARG (call, 0);
3436 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3437 false/*fn*/, true/*int_cst*/);
3439 if (value_dependent_expression_p (hi))
3441 if (hi != ohi)
3443 call = copy_node (call);
3444 CALL_EXPR_ARG (call, 0) = hi;
3446 tree ex = make_pack_expansion (call);
3447 tree vec = make_tree_vec (1);
3448 TREE_VEC_ELT (vec, 0) = ex;
3449 return vec;
3451 else
3453 hi = cxx_constant_value (hi);
3454 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3456 /* Calculate the largest value of len that won't make the size of the vec
3457 overflow an int. The compiler will exceed resource limits long before
3458 this, but it seems a decent place to diagnose. */
3459 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3461 if (len < 0 || len > max)
3463 if ((complain & tf_error)
3464 && hi != error_mark_node)
3465 error ("argument to __integer_pack must be between 0 and %d", max);
3466 return error_mark_node;
3469 tree vec = make_tree_vec (len);
3471 for (int i = 0; i < len; ++i)
3472 TREE_VEC_ELT (vec, i) = size_int (i);
3474 return vec;
3478 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3479 CALL. */
3481 static tree
3482 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3483 tree in_decl)
3485 if (!builtin_pack_call_p (call))
3486 return NULL_TREE;
3488 tree fn = CALL_EXPR_FN (call);
3490 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3491 return expand_integer_pack (call, args, complain, in_decl);
3493 return NULL_TREE;
3496 /* Structure used to track the progress of find_parameter_packs_r. */
3497 struct find_parameter_pack_data
3499 /* TREE_LIST that will contain all of the parameter packs found by
3500 the traversal. */
3501 tree* parameter_packs;
3503 /* Set of AST nodes that have been visited by the traversal. */
3504 hash_set<tree> *visited;
3506 /* True iff we're making a type pack expansion. */
3507 bool type_pack_expansion_p;
3510 /* Identifies all of the argument packs that occur in a template
3511 argument and appends them to the TREE_LIST inside DATA, which is a
3512 find_parameter_pack_data structure. This is a subroutine of
3513 make_pack_expansion and uses_parameter_packs. */
3514 static tree
3515 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3517 tree t = *tp;
3518 struct find_parameter_pack_data* ppd =
3519 (struct find_parameter_pack_data*)data;
3520 bool parameter_pack_p = false;
3522 /* Handle type aliases/typedefs. */
3523 if (TYPE_ALIAS_P (t))
3525 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3526 cp_walk_tree (&TI_ARGS (tinfo),
3527 &find_parameter_packs_r,
3528 ppd, ppd->visited);
3529 *walk_subtrees = 0;
3530 return NULL_TREE;
3533 /* Identify whether this is a parameter pack or not. */
3534 switch (TREE_CODE (t))
3536 case TEMPLATE_PARM_INDEX:
3537 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3538 parameter_pack_p = true;
3539 break;
3541 case TEMPLATE_TYPE_PARM:
3542 t = TYPE_MAIN_VARIANT (t);
3543 /* FALLTHRU */
3544 case TEMPLATE_TEMPLATE_PARM:
3545 /* If the placeholder appears in the decl-specifier-seq of a function
3546 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3547 is a pack expansion, the invented template parameter is a template
3548 parameter pack. */
3549 if (ppd->type_pack_expansion_p && is_auto (t))
3550 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3551 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3552 parameter_pack_p = true;
3553 break;
3555 case FIELD_DECL:
3556 case PARM_DECL:
3557 if (DECL_PACK_P (t))
3559 /* We don't want to walk into the type of a PARM_DECL,
3560 because we don't want to see the type parameter pack. */
3561 *walk_subtrees = 0;
3562 parameter_pack_p = true;
3564 break;
3566 /* Look through a lambda capture proxy to the field pack. */
3567 case VAR_DECL:
3568 if (DECL_HAS_VALUE_EXPR_P (t))
3570 tree v = DECL_VALUE_EXPR (t);
3571 cp_walk_tree (&v,
3572 &find_parameter_packs_r,
3573 ppd, ppd->visited);
3574 *walk_subtrees = 0;
3576 else if (variable_template_specialization_p (t))
3578 cp_walk_tree (&DECL_TI_ARGS (t),
3579 find_parameter_packs_r,
3580 ppd, ppd->visited);
3581 *walk_subtrees = 0;
3583 break;
3585 case CALL_EXPR:
3586 if (builtin_pack_call_p (t))
3587 parameter_pack_p = true;
3588 break;
3590 case BASES:
3591 parameter_pack_p = true;
3592 break;
3593 default:
3594 /* Not a parameter pack. */
3595 break;
3598 if (parameter_pack_p)
3600 /* Add this parameter pack to the list. */
3601 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3604 if (TYPE_P (t))
3605 cp_walk_tree (&TYPE_CONTEXT (t),
3606 &find_parameter_packs_r, ppd, ppd->visited);
3608 /* This switch statement will return immediately if we don't find a
3609 parameter pack. */
3610 switch (TREE_CODE (t))
3612 case TEMPLATE_PARM_INDEX:
3613 return NULL_TREE;
3615 case BOUND_TEMPLATE_TEMPLATE_PARM:
3616 /* Check the template itself. */
3617 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3618 &find_parameter_packs_r, ppd, ppd->visited);
3619 /* Check the template arguments. */
3620 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3621 ppd->visited);
3622 *walk_subtrees = 0;
3623 return NULL_TREE;
3625 case TEMPLATE_TYPE_PARM:
3626 case TEMPLATE_TEMPLATE_PARM:
3627 return NULL_TREE;
3629 case PARM_DECL:
3630 return NULL_TREE;
3632 case DECL_EXPR:
3633 /* Ignore the declaration of a capture proxy for a parameter pack. */
3634 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3635 *walk_subtrees = 0;
3636 return NULL_TREE;
3638 case RECORD_TYPE:
3639 if (TYPE_PTRMEMFUNC_P (t))
3640 return NULL_TREE;
3641 /* Fall through. */
3643 case UNION_TYPE:
3644 case ENUMERAL_TYPE:
3645 if (TYPE_TEMPLATE_INFO (t))
3646 cp_walk_tree (&TYPE_TI_ARGS (t),
3647 &find_parameter_packs_r, ppd, ppd->visited);
3649 *walk_subtrees = 0;
3650 return NULL_TREE;
3652 case TEMPLATE_DECL:
3653 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3654 return NULL_TREE;
3655 gcc_fallthrough();
3657 case CONSTRUCTOR:
3658 cp_walk_tree (&TREE_TYPE (t),
3659 &find_parameter_packs_r, ppd, ppd->visited);
3660 return NULL_TREE;
3662 case TYPENAME_TYPE:
3663 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3664 ppd, ppd->visited);
3665 *walk_subtrees = 0;
3666 return NULL_TREE;
3668 case TYPE_PACK_EXPANSION:
3669 case EXPR_PACK_EXPANSION:
3670 *walk_subtrees = 0;
3671 return NULL_TREE;
3673 case INTEGER_TYPE:
3674 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3675 ppd, ppd->visited);
3676 *walk_subtrees = 0;
3677 return NULL_TREE;
3679 case IDENTIFIER_NODE:
3680 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3681 ppd->visited);
3682 *walk_subtrees = 0;
3683 return NULL_TREE;
3685 case LAMBDA_EXPR:
3687 tree fn = lambda_function (t);
3688 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3689 ppd->visited);
3690 *walk_subtrees = 0;
3691 return NULL_TREE;
3694 case DECLTYPE_TYPE:
3696 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3697 type_pack_expansion_p to false so that any placeholders
3698 within the expression don't get marked as parameter packs. */
3699 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3700 ppd->type_pack_expansion_p = false;
3701 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3702 ppd, ppd->visited);
3703 ppd->type_pack_expansion_p = type_pack_expansion_p;
3704 *walk_subtrees = 0;
3705 return NULL_TREE;
3708 default:
3709 return NULL_TREE;
3712 return NULL_TREE;
3715 /* Determines if the expression or type T uses any parameter packs. */
3716 bool
3717 uses_parameter_packs (tree t)
3719 tree parameter_packs = NULL_TREE;
3720 struct find_parameter_pack_data ppd;
3721 ppd.parameter_packs = &parameter_packs;
3722 ppd.visited = new hash_set<tree>;
3723 ppd.type_pack_expansion_p = false;
3724 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3725 delete ppd.visited;
3726 return parameter_packs != NULL_TREE;
3729 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3730 representation a base-class initializer into a parameter pack
3731 expansion. If all goes well, the resulting node will be an
3732 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3733 respectively. */
3734 tree
3735 make_pack_expansion (tree arg)
3737 tree result;
3738 tree parameter_packs = NULL_TREE;
3739 bool for_types = false;
3740 struct find_parameter_pack_data ppd;
3742 if (!arg || arg == error_mark_node)
3743 return arg;
3745 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3747 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3748 class initializer. In this case, the TREE_PURPOSE will be a
3749 _TYPE node (representing the base class expansion we're
3750 initializing) and the TREE_VALUE will be a TREE_LIST
3751 containing the initialization arguments.
3753 The resulting expansion looks somewhat different from most
3754 expansions. Rather than returning just one _EXPANSION, we
3755 return a TREE_LIST whose TREE_PURPOSE is a
3756 TYPE_PACK_EXPANSION containing the bases that will be
3757 initialized. The TREE_VALUE will be identical to the
3758 original TREE_VALUE, which is a list of arguments that will
3759 be passed to each base. We do not introduce any new pack
3760 expansion nodes into the TREE_VALUE (although it is possible
3761 that some already exist), because the TREE_PURPOSE and
3762 TREE_VALUE all need to be expanded together with the same
3763 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3764 resulting TREE_PURPOSE will mention the parameter packs in
3765 both the bases and the arguments to the bases. */
3766 tree purpose;
3767 tree value;
3768 tree parameter_packs = NULL_TREE;
3770 /* Determine which parameter packs will be used by the base
3771 class expansion. */
3772 ppd.visited = new hash_set<tree>;
3773 ppd.parameter_packs = &parameter_packs;
3774 ppd.type_pack_expansion_p = true;
3775 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3776 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3777 &ppd, ppd.visited);
3779 if (parameter_packs == NULL_TREE)
3781 error ("base initializer expansion %qT contains no parameter packs", arg);
3782 delete ppd.visited;
3783 return error_mark_node;
3786 if (TREE_VALUE (arg) != void_type_node)
3788 /* Collect the sets of parameter packs used in each of the
3789 initialization arguments. */
3790 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3792 /* Determine which parameter packs will be expanded in this
3793 argument. */
3794 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3795 &ppd, ppd.visited);
3799 delete ppd.visited;
3801 /* Create the pack expansion type for the base type. */
3802 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3803 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3804 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3805 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3807 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3808 they will rarely be compared to anything. */
3809 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3811 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3814 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3815 for_types = true;
3817 /* Build the PACK_EXPANSION_* node. */
3818 result = for_types
3819 ? cxx_make_type (TYPE_PACK_EXPANSION)
3820 : make_node (EXPR_PACK_EXPANSION);
3821 SET_PACK_EXPANSION_PATTERN (result, arg);
3822 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3824 /* Propagate type and const-expression information. */
3825 TREE_TYPE (result) = TREE_TYPE (arg);
3826 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3827 /* Mark this read now, since the expansion might be length 0. */
3828 mark_exp_read (arg);
3830 else
3831 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3832 they will rarely be compared to anything. */
3833 SET_TYPE_STRUCTURAL_EQUALITY (result);
3835 /* Determine which parameter packs will be expanded. */
3836 ppd.parameter_packs = &parameter_packs;
3837 ppd.visited = new hash_set<tree>;
3838 ppd.type_pack_expansion_p = TYPE_P (arg);
3839 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3840 delete ppd.visited;
3842 /* Make sure we found some parameter packs. */
3843 if (parameter_packs == NULL_TREE)
3845 if (TYPE_P (arg))
3846 error ("expansion pattern %qT contains no argument packs", arg);
3847 else
3848 error ("expansion pattern %qE contains no argument packs", arg);
3849 return error_mark_node;
3851 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3853 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3855 return result;
3858 /* Checks T for any "bare" parameter packs, which have not yet been
3859 expanded, and issues an error if any are found. This operation can
3860 only be done on full expressions or types (e.g., an expression
3861 statement, "if" condition, etc.), because we could have expressions like:
3863 foo(f(g(h(args)))...)
3865 where "args" is a parameter pack. check_for_bare_parameter_packs
3866 should not be called for the subexpressions args, h(args),
3867 g(h(args)), or f(g(h(args))), because we would produce erroneous
3868 error messages.
3870 Returns TRUE and emits an error if there were bare parameter packs,
3871 returns FALSE otherwise. */
3872 bool
3873 check_for_bare_parameter_packs (tree t)
3875 tree parameter_packs = NULL_TREE;
3876 struct find_parameter_pack_data ppd;
3878 if (!processing_template_decl || !t || t == error_mark_node)
3879 return false;
3881 /* A lambda might use a parameter pack from the containing context. */
3882 if (current_function_decl && LAMBDA_FUNCTION_P (current_function_decl))
3883 return false;
3885 if (TREE_CODE (t) == TYPE_DECL)
3886 t = TREE_TYPE (t);
3888 ppd.parameter_packs = &parameter_packs;
3889 ppd.visited = new hash_set<tree>;
3890 ppd.type_pack_expansion_p = false;
3891 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3892 delete ppd.visited;
3894 if (parameter_packs)
3896 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3897 error_at (loc, "parameter packs not expanded with %<...%>:");
3898 while (parameter_packs)
3900 tree pack = TREE_VALUE (parameter_packs);
3901 tree name = NULL_TREE;
3903 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3904 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3905 name = TYPE_NAME (pack);
3906 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3907 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3908 else if (TREE_CODE (pack) == CALL_EXPR)
3909 name = DECL_NAME (CALL_EXPR_FN (pack));
3910 else
3911 name = DECL_NAME (pack);
3913 if (name)
3914 inform (loc, " %qD", name);
3915 else
3916 inform (loc, " <anonymous>");
3918 parameter_packs = TREE_CHAIN (parameter_packs);
3921 return true;
3924 return false;
3927 /* Expand any parameter packs that occur in the template arguments in
3928 ARGS. */
3929 tree
3930 expand_template_argument_pack (tree args)
3932 if (args == error_mark_node)
3933 return error_mark_node;
3935 tree result_args = NULL_TREE;
3936 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3937 int num_result_args = -1;
3938 int non_default_args_count = -1;
3940 /* First, determine if we need to expand anything, and the number of
3941 slots we'll need. */
3942 for (in_arg = 0; in_arg < nargs; ++in_arg)
3944 tree arg = TREE_VEC_ELT (args, in_arg);
3945 if (arg == NULL_TREE)
3946 return args;
3947 if (ARGUMENT_PACK_P (arg))
3949 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3950 if (num_result_args < 0)
3951 num_result_args = in_arg + num_packed;
3952 else
3953 num_result_args += num_packed;
3955 else
3957 if (num_result_args >= 0)
3958 num_result_args++;
3962 /* If no expansion is necessary, we're done. */
3963 if (num_result_args < 0)
3964 return args;
3966 /* Expand arguments. */
3967 result_args = make_tree_vec (num_result_args);
3968 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3969 non_default_args_count =
3970 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3971 for (in_arg = 0; in_arg < nargs; ++in_arg)
3973 tree arg = TREE_VEC_ELT (args, in_arg);
3974 if (ARGUMENT_PACK_P (arg))
3976 tree packed = ARGUMENT_PACK_ARGS (arg);
3977 int i, num_packed = TREE_VEC_LENGTH (packed);
3978 for (i = 0; i < num_packed; ++i, ++out_arg)
3979 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3980 if (non_default_args_count > 0)
3981 non_default_args_count += num_packed - 1;
3983 else
3985 TREE_VEC_ELT (result_args, out_arg) = arg;
3986 ++out_arg;
3989 if (non_default_args_count >= 0)
3990 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3991 return result_args;
3994 /* Checks if DECL shadows a template parameter.
3996 [temp.local]: A template-parameter shall not be redeclared within its
3997 scope (including nested scopes).
3999 Emits an error and returns TRUE if the DECL shadows a parameter,
4000 returns FALSE otherwise. */
4002 bool
4003 check_template_shadow (tree decl)
4005 tree olddecl;
4007 /* If we're not in a template, we can't possibly shadow a template
4008 parameter. */
4009 if (!current_template_parms)
4010 return true;
4012 /* Figure out what we're shadowing. */
4013 decl = OVL_FIRST (decl);
4014 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4016 /* If there's no previous binding for this name, we're not shadowing
4017 anything, let alone a template parameter. */
4018 if (!olddecl)
4019 return true;
4021 /* If we're not shadowing a template parameter, we're done. Note
4022 that OLDDECL might be an OVERLOAD (or perhaps even an
4023 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4024 node. */
4025 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4026 return true;
4028 /* We check for decl != olddecl to avoid bogus errors for using a
4029 name inside a class. We check TPFI to avoid duplicate errors for
4030 inline member templates. */
4031 if (decl == olddecl
4032 || (DECL_TEMPLATE_PARM_P (decl)
4033 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4034 return true;
4036 /* Don't complain about the injected class name, as we've already
4037 complained about the class itself. */
4038 if (DECL_SELF_REFERENCE_P (decl))
4039 return false;
4041 if (DECL_TEMPLATE_PARM_P (decl))
4042 error ("declaration of template parameter %q+D shadows "
4043 "template parameter", decl);
4044 else
4045 error ("declaration of %q+#D shadows template parameter", decl);
4046 inform (DECL_SOURCE_LOCATION (olddecl),
4047 "template parameter %qD declared here", olddecl);
4048 return false;
4051 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4052 ORIG_LEVEL, DECL, and TYPE. */
4054 static tree
4055 build_template_parm_index (int index,
4056 int level,
4057 int orig_level,
4058 tree decl,
4059 tree type)
4061 tree t = make_node (TEMPLATE_PARM_INDEX);
4062 TEMPLATE_PARM_IDX (t) = index;
4063 TEMPLATE_PARM_LEVEL (t) = level;
4064 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4065 TEMPLATE_PARM_DECL (t) = decl;
4066 TREE_TYPE (t) = type;
4067 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4068 TREE_READONLY (t) = TREE_READONLY (decl);
4070 return t;
4073 /* Find the canonical type parameter for the given template type
4074 parameter. Returns the canonical type parameter, which may be TYPE
4075 if no such parameter existed. */
4077 static tree
4078 canonical_type_parameter (tree type)
4080 tree list;
4081 int idx = TEMPLATE_TYPE_IDX (type);
4082 if (!canonical_template_parms)
4083 vec_alloc (canonical_template_parms, idx + 1);
4085 if (canonical_template_parms->length () <= (unsigned) idx)
4086 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4088 list = (*canonical_template_parms)[idx];
4089 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4090 list = TREE_CHAIN (list);
4092 if (list)
4093 return TREE_VALUE (list);
4094 else
4096 (*canonical_template_parms)[idx]
4097 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4098 return type;
4102 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4103 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4104 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4105 new one is created. */
4107 static tree
4108 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4109 tsubst_flags_t complain)
4111 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4112 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4113 != TEMPLATE_PARM_LEVEL (index) - levels)
4114 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4116 tree orig_decl = TEMPLATE_PARM_DECL (index);
4117 tree decl, t;
4119 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4120 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4121 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4122 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4123 DECL_ARTIFICIAL (decl) = 1;
4124 SET_DECL_TEMPLATE_PARM_P (decl);
4126 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4127 TEMPLATE_PARM_LEVEL (index) - levels,
4128 TEMPLATE_PARM_ORIG_LEVEL (index),
4129 decl, type);
4130 TEMPLATE_PARM_DESCENDANTS (index) = t;
4131 TEMPLATE_PARM_PARAMETER_PACK (t)
4132 = TEMPLATE_PARM_PARAMETER_PACK (index);
4134 /* Template template parameters need this. */
4135 if (TREE_CODE (decl) == TEMPLATE_DECL)
4137 DECL_TEMPLATE_RESULT (decl)
4138 = build_decl (DECL_SOURCE_LOCATION (decl),
4139 TYPE_DECL, DECL_NAME (decl), type);
4140 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4141 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4142 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4146 return TEMPLATE_PARM_DESCENDANTS (index);
4149 /* Process information from new template parameter PARM and append it
4150 to the LIST being built. This new parameter is a non-type
4151 parameter iff IS_NON_TYPE is true. This new parameter is a
4152 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4153 is in PARM_LOC. */
4155 tree
4156 process_template_parm (tree list, location_t parm_loc, tree parm,
4157 bool is_non_type, bool is_parameter_pack)
4159 tree decl = 0;
4160 int idx = 0;
4162 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4163 tree defval = TREE_PURPOSE (parm);
4164 tree constr = TREE_TYPE (parm);
4166 if (list)
4168 tree p = tree_last (list);
4170 if (p && TREE_VALUE (p) != error_mark_node)
4172 p = TREE_VALUE (p);
4173 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4174 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4175 else
4176 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4179 ++idx;
4182 if (is_non_type)
4184 parm = TREE_VALUE (parm);
4186 SET_DECL_TEMPLATE_PARM_P (parm);
4188 if (TREE_TYPE (parm) != error_mark_node)
4190 /* [temp.param]
4192 The top-level cv-qualifiers on the template-parameter are
4193 ignored when determining its type. */
4194 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4195 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4196 TREE_TYPE (parm) = error_mark_node;
4197 else if (uses_parameter_packs (TREE_TYPE (parm))
4198 && !is_parameter_pack
4199 /* If we're in a nested template parameter list, the template
4200 template parameter could be a parameter pack. */
4201 && processing_template_parmlist == 1)
4203 /* This template parameter is not a parameter pack, but it
4204 should be. Complain about "bare" parameter packs. */
4205 check_for_bare_parameter_packs (TREE_TYPE (parm));
4207 /* Recover by calling this a parameter pack. */
4208 is_parameter_pack = true;
4212 /* A template parameter is not modifiable. */
4213 TREE_CONSTANT (parm) = 1;
4214 TREE_READONLY (parm) = 1;
4215 decl = build_decl (parm_loc,
4216 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4217 TREE_CONSTANT (decl) = 1;
4218 TREE_READONLY (decl) = 1;
4219 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4220 = build_template_parm_index (idx, processing_template_decl,
4221 processing_template_decl,
4222 decl, TREE_TYPE (parm));
4224 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4225 = is_parameter_pack;
4227 else
4229 tree t;
4230 parm = TREE_VALUE (TREE_VALUE (parm));
4232 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4234 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4235 /* This is for distinguishing between real templates and template
4236 template parameters */
4237 TREE_TYPE (parm) = t;
4238 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4239 decl = parm;
4241 else
4243 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4244 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4245 decl = build_decl (parm_loc,
4246 TYPE_DECL, parm, t);
4249 TYPE_NAME (t) = decl;
4250 TYPE_STUB_DECL (t) = decl;
4251 parm = decl;
4252 TEMPLATE_TYPE_PARM_INDEX (t)
4253 = build_template_parm_index (idx, processing_template_decl,
4254 processing_template_decl,
4255 decl, TREE_TYPE (parm));
4256 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4257 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4259 DECL_ARTIFICIAL (decl) = 1;
4260 SET_DECL_TEMPLATE_PARM_P (decl);
4262 /* Build requirements for the type/template parameter.
4263 This must be done after SET_DECL_TEMPLATE_PARM_P or
4264 process_template_parm could fail. */
4265 tree reqs = finish_shorthand_constraint (parm, constr);
4267 pushdecl (decl);
4269 /* Build the parameter node linking the parameter declaration,
4270 its default argument (if any), and its constraints (if any). */
4271 parm = build_tree_list (defval, parm);
4272 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4274 return chainon (list, parm);
4277 /* The end of a template parameter list has been reached. Process the
4278 tree list into a parameter vector, converting each parameter into a more
4279 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4280 as PARM_DECLs. */
4282 tree
4283 end_template_parm_list (tree parms)
4285 int nparms;
4286 tree parm, next;
4287 tree saved_parmlist = make_tree_vec (list_length (parms));
4289 /* Pop the dummy parameter level and add the real one. */
4290 current_template_parms = TREE_CHAIN (current_template_parms);
4292 current_template_parms
4293 = tree_cons (size_int (processing_template_decl),
4294 saved_parmlist, current_template_parms);
4296 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4298 next = TREE_CHAIN (parm);
4299 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4300 TREE_CHAIN (parm) = NULL_TREE;
4303 --processing_template_parmlist;
4305 return saved_parmlist;
4308 // Explicitly indicate the end of the template parameter list. We assume
4309 // that the current template parameters have been constructed and/or
4310 // managed explicitly, as when creating new template template parameters
4311 // from a shorthand constraint.
4312 void
4313 end_template_parm_list ()
4315 --processing_template_parmlist;
4318 /* end_template_decl is called after a template declaration is seen. */
4320 void
4321 end_template_decl (void)
4323 reset_specialization ();
4325 if (! processing_template_decl)
4326 return;
4328 /* This matches the pushlevel in begin_template_parm_list. */
4329 finish_scope ();
4331 --processing_template_decl;
4332 current_template_parms = TREE_CHAIN (current_template_parms);
4335 /* Takes a TREE_LIST representing a template parameter and convert it
4336 into an argument suitable to be passed to the type substitution
4337 functions. Note that If the TREE_LIST contains an error_mark
4338 node, the returned argument is error_mark_node. */
4340 tree
4341 template_parm_to_arg (tree t)
4344 if (t == NULL_TREE
4345 || TREE_CODE (t) != TREE_LIST)
4346 return t;
4348 if (error_operand_p (TREE_VALUE (t)))
4349 return error_mark_node;
4351 t = TREE_VALUE (t);
4353 if (TREE_CODE (t) == TYPE_DECL
4354 || TREE_CODE (t) == TEMPLATE_DECL)
4356 t = TREE_TYPE (t);
4358 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4360 /* Turn this argument into a TYPE_ARGUMENT_PACK
4361 with a single element, which expands T. */
4362 tree vec = make_tree_vec (1);
4363 if (CHECKING_P)
4364 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4366 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4368 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4369 SET_ARGUMENT_PACK_ARGS (t, vec);
4372 else
4374 t = DECL_INITIAL (t);
4376 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4378 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4379 with a single element, which expands T. */
4380 tree vec = make_tree_vec (1);
4381 if (CHECKING_P)
4382 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4384 t = convert_from_reference (t);
4385 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4387 t = make_node (NONTYPE_ARGUMENT_PACK);
4388 SET_ARGUMENT_PACK_ARGS (t, vec);
4390 else
4391 t = convert_from_reference (t);
4393 return t;
4396 /* Given a single level of template parameters (a TREE_VEC), return it
4397 as a set of template arguments. */
4399 static tree
4400 template_parms_level_to_args (tree parms)
4402 tree a = copy_node (parms);
4403 TREE_TYPE (a) = NULL_TREE;
4404 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4405 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4407 if (CHECKING_P)
4408 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4410 return a;
4413 /* Given a set of template parameters, return them as a set of template
4414 arguments. The template parameters are represented as a TREE_VEC, in
4415 the form documented in cp-tree.h for template arguments. */
4417 static tree
4418 template_parms_to_args (tree parms)
4420 tree header;
4421 tree args = NULL_TREE;
4422 int length = TMPL_PARMS_DEPTH (parms);
4423 int l = length;
4425 /* If there is only one level of template parameters, we do not
4426 create a TREE_VEC of TREE_VECs. Instead, we return a single
4427 TREE_VEC containing the arguments. */
4428 if (length > 1)
4429 args = make_tree_vec (length);
4431 for (header = parms; header; header = TREE_CHAIN (header))
4433 tree a = template_parms_level_to_args (TREE_VALUE (header));
4435 if (length > 1)
4436 TREE_VEC_ELT (args, --l) = a;
4437 else
4438 args = a;
4441 return args;
4444 /* Within the declaration of a template, return the currently active
4445 template parameters as an argument TREE_VEC. */
4447 static tree
4448 current_template_args (void)
4450 return template_parms_to_args (current_template_parms);
4453 /* Update the declared TYPE by doing any lookups which were thought to be
4454 dependent, but are not now that we know the SCOPE of the declarator. */
4456 tree
4457 maybe_update_decl_type (tree orig_type, tree scope)
4459 tree type = orig_type;
4461 if (type == NULL_TREE)
4462 return type;
4464 if (TREE_CODE (orig_type) == TYPE_DECL)
4465 type = TREE_TYPE (type);
4467 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4468 && dependent_type_p (type)
4469 /* Don't bother building up the args in this case. */
4470 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4472 /* tsubst in the args corresponding to the template parameters,
4473 including auto if present. Most things will be unchanged, but
4474 make_typename_type and tsubst_qualified_id will resolve
4475 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4476 tree args = current_template_args ();
4477 tree auto_node = type_uses_auto (type);
4478 tree pushed;
4479 if (auto_node)
4481 tree auto_vec = make_tree_vec (1);
4482 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4483 args = add_to_template_args (args, auto_vec);
4485 pushed = push_scope (scope);
4486 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4487 if (pushed)
4488 pop_scope (scope);
4491 if (type == error_mark_node)
4492 return orig_type;
4494 if (TREE_CODE (orig_type) == TYPE_DECL)
4496 if (same_type_p (type, TREE_TYPE (orig_type)))
4497 type = orig_type;
4498 else
4499 type = TYPE_NAME (type);
4501 return type;
4504 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4505 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4506 the new template is a member template. */
4508 tree
4509 build_template_decl (tree decl, tree parms, bool member_template_p)
4511 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4512 DECL_TEMPLATE_PARMS (tmpl) = parms;
4513 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4514 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4515 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4517 return tmpl;
4520 struct template_parm_data
4522 /* The level of the template parameters we are currently
4523 processing. */
4524 int level;
4526 /* The index of the specialization argument we are currently
4527 processing. */
4528 int current_arg;
4530 /* An array whose size is the number of template parameters. The
4531 elements are nonzero if the parameter has been used in any one
4532 of the arguments processed so far. */
4533 int* parms;
4535 /* An array whose size is the number of template arguments. The
4536 elements are nonzero if the argument makes use of template
4537 parameters of this level. */
4538 int* arg_uses_template_parms;
4541 /* Subroutine of push_template_decl used to see if each template
4542 parameter in a partial specialization is used in the explicit
4543 argument list. If T is of the LEVEL given in DATA (which is
4544 treated as a template_parm_data*), then DATA->PARMS is marked
4545 appropriately. */
4547 static int
4548 mark_template_parm (tree t, void* data)
4550 int level;
4551 int idx;
4552 struct template_parm_data* tpd = (struct template_parm_data*) data;
4554 template_parm_level_and_index (t, &level, &idx);
4556 if (level == tpd->level)
4558 tpd->parms[idx] = 1;
4559 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4562 /* In C++17 the type of a non-type argument is a deduced context. */
4563 if (cxx_dialect >= cxx1z
4564 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4565 for_each_template_parm (TREE_TYPE (t),
4566 &mark_template_parm,
4567 data,
4568 NULL,
4569 /*include_nondeduced_p=*/false);
4571 /* Return zero so that for_each_template_parm will continue the
4572 traversal of the tree; we want to mark *every* template parm. */
4573 return 0;
4576 /* Process the partial specialization DECL. */
4578 static tree
4579 process_partial_specialization (tree decl)
4581 tree type = TREE_TYPE (decl);
4582 tree tinfo = get_template_info (decl);
4583 tree maintmpl = TI_TEMPLATE (tinfo);
4584 tree specargs = TI_ARGS (tinfo);
4585 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4586 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4587 tree inner_parms;
4588 tree inst;
4589 int nargs = TREE_VEC_LENGTH (inner_args);
4590 int ntparms;
4591 int i;
4592 bool did_error_intro = false;
4593 struct template_parm_data tpd;
4594 struct template_parm_data tpd2;
4596 gcc_assert (current_template_parms);
4598 /* A concept cannot be specialized. */
4599 if (flag_concepts && variable_concept_p (maintmpl))
4601 error ("specialization of variable concept %q#D", maintmpl);
4602 return error_mark_node;
4605 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4606 ntparms = TREE_VEC_LENGTH (inner_parms);
4608 /* We check that each of the template parameters given in the
4609 partial specialization is used in the argument list to the
4610 specialization. For example:
4612 template <class T> struct S;
4613 template <class T> struct S<T*>;
4615 The second declaration is OK because `T*' uses the template
4616 parameter T, whereas
4618 template <class T> struct S<int>;
4620 is no good. Even trickier is:
4622 template <class T>
4623 struct S1
4625 template <class U>
4626 struct S2;
4627 template <class U>
4628 struct S2<T>;
4631 The S2<T> declaration is actually invalid; it is a
4632 full-specialization. Of course,
4634 template <class U>
4635 struct S2<T (*)(U)>;
4637 or some such would have been OK. */
4638 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4639 tpd.parms = XALLOCAVEC (int, ntparms);
4640 memset (tpd.parms, 0, sizeof (int) * ntparms);
4642 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4643 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4644 for (i = 0; i < nargs; ++i)
4646 tpd.current_arg = i;
4647 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4648 &mark_template_parm,
4649 &tpd,
4650 NULL,
4651 /*include_nondeduced_p=*/false);
4653 for (i = 0; i < ntparms; ++i)
4654 if (tpd.parms[i] == 0)
4656 /* One of the template parms was not used in a deduced context in the
4657 specialization. */
4658 if (!did_error_intro)
4660 error ("template parameters not deducible in "
4661 "partial specialization:");
4662 did_error_intro = true;
4665 inform (input_location, " %qD",
4666 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4669 if (did_error_intro)
4670 return error_mark_node;
4672 /* [temp.class.spec]
4674 The argument list of the specialization shall not be identical to
4675 the implicit argument list of the primary template. */
4676 tree main_args
4677 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4678 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4679 && (!flag_concepts
4680 || !strictly_subsumes (current_template_constraints (),
4681 get_constraints (maintmpl))))
4683 if (!flag_concepts)
4684 error ("partial specialization %q+D does not specialize "
4685 "any template arguments", decl);
4686 else
4687 error ("partial specialization %q+D does not specialize any "
4688 "template arguments and is not more constrained than", decl);
4689 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4692 /* A partial specialization that replaces multiple parameters of the
4693 primary template with a pack expansion is less specialized for those
4694 parameters. */
4695 if (nargs < DECL_NTPARMS (maintmpl))
4697 error ("partial specialization is not more specialized than the "
4698 "primary template because it replaces multiple parameters "
4699 "with a pack expansion");
4700 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4701 /* Avoid crash in process_partial_specialization. */
4702 return decl;
4705 /* If we aren't in a dependent class, we can actually try deduction. */
4706 else if (tpd.level == 1
4707 /* FIXME we should be able to handle a partial specialization of a
4708 partial instantiation, but currently we can't (c++/41727). */
4709 && TMPL_ARGS_DEPTH (specargs) == 1
4710 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4712 if (permerror (input_location, "partial specialization %qD is not "
4713 "more specialized than", decl))
4714 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4715 maintmpl);
4718 /* [temp.class.spec]
4720 A partially specialized non-type argument expression shall not
4721 involve template parameters of the partial specialization except
4722 when the argument expression is a simple identifier.
4724 The type of a template parameter corresponding to a specialized
4725 non-type argument shall not be dependent on a parameter of the
4726 specialization.
4728 Also, we verify that pack expansions only occur at the
4729 end of the argument list. */
4730 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4731 tpd2.parms = 0;
4732 for (i = 0; i < nargs; ++i)
4734 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4735 tree arg = TREE_VEC_ELT (inner_args, i);
4736 tree packed_args = NULL_TREE;
4737 int j, len = 1;
4739 if (ARGUMENT_PACK_P (arg))
4741 /* Extract the arguments from the argument pack. We'll be
4742 iterating over these in the following loop. */
4743 packed_args = ARGUMENT_PACK_ARGS (arg);
4744 len = TREE_VEC_LENGTH (packed_args);
4747 for (j = 0; j < len; j++)
4749 if (packed_args)
4750 /* Get the Jth argument in the parameter pack. */
4751 arg = TREE_VEC_ELT (packed_args, j);
4753 if (PACK_EXPANSION_P (arg))
4755 /* Pack expansions must come at the end of the
4756 argument list. */
4757 if ((packed_args && j < len - 1)
4758 || (!packed_args && i < nargs - 1))
4760 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4761 error ("parameter pack argument %qE must be at the "
4762 "end of the template argument list", arg);
4763 else
4764 error ("parameter pack argument %qT must be at the "
4765 "end of the template argument list", arg);
4769 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4770 /* We only care about the pattern. */
4771 arg = PACK_EXPANSION_PATTERN (arg);
4773 if (/* These first two lines are the `non-type' bit. */
4774 !TYPE_P (arg)
4775 && TREE_CODE (arg) != TEMPLATE_DECL
4776 /* This next two lines are the `argument expression is not just a
4777 simple identifier' condition and also the `specialized
4778 non-type argument' bit. */
4779 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4780 && !(REFERENCE_REF_P (arg)
4781 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4783 if ((!packed_args && tpd.arg_uses_template_parms[i])
4784 || (packed_args && uses_template_parms (arg)))
4785 error ("template argument %qE involves template parameter(s)",
4786 arg);
4787 else
4789 /* Look at the corresponding template parameter,
4790 marking which template parameters its type depends
4791 upon. */
4792 tree type = TREE_TYPE (parm);
4794 if (!tpd2.parms)
4796 /* We haven't yet initialized TPD2. Do so now. */
4797 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4798 /* The number of parameters here is the number in the
4799 main template, which, as checked in the assertion
4800 above, is NARGS. */
4801 tpd2.parms = XALLOCAVEC (int, nargs);
4802 tpd2.level =
4803 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4806 /* Mark the template parameters. But this time, we're
4807 looking for the template parameters of the main
4808 template, not in the specialization. */
4809 tpd2.current_arg = i;
4810 tpd2.arg_uses_template_parms[i] = 0;
4811 memset (tpd2.parms, 0, sizeof (int) * nargs);
4812 for_each_template_parm (type,
4813 &mark_template_parm,
4814 &tpd2,
4815 NULL,
4816 /*include_nondeduced_p=*/false);
4818 if (tpd2.arg_uses_template_parms [i])
4820 /* The type depended on some template parameters.
4821 If they are fully specialized in the
4822 specialization, that's OK. */
4823 int j;
4824 int count = 0;
4825 for (j = 0; j < nargs; ++j)
4826 if (tpd2.parms[j] != 0
4827 && tpd.arg_uses_template_parms [j])
4828 ++count;
4829 if (count != 0)
4830 error_n (input_location, count,
4831 "type %qT of template argument %qE depends "
4832 "on a template parameter",
4833 "type %qT of template argument %qE depends "
4834 "on template parameters",
4835 type,
4836 arg);
4843 /* We should only get here once. */
4844 if (TREE_CODE (decl) == TYPE_DECL)
4845 gcc_assert (!COMPLETE_TYPE_P (type));
4847 // Build the template decl.
4848 tree tmpl = build_template_decl (decl, current_template_parms,
4849 DECL_MEMBER_TEMPLATE_P (maintmpl));
4850 TREE_TYPE (tmpl) = type;
4851 DECL_TEMPLATE_RESULT (tmpl) = decl;
4852 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4853 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4854 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4856 /* Give template template parms a DECL_CONTEXT of the template
4857 for which they are a parameter. */
4858 for (i = 0; i < ntparms; ++i)
4860 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4861 if (TREE_CODE (parm) == TEMPLATE_DECL)
4862 DECL_CONTEXT (parm) = tmpl;
4865 if (VAR_P (decl))
4866 /* We didn't register this in check_explicit_specialization so we could
4867 wait until the constraints were set. */
4868 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4869 else
4870 associate_classtype_constraints (type);
4872 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4873 = tree_cons (specargs, tmpl,
4874 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4875 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4877 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4878 inst = TREE_CHAIN (inst))
4880 tree instance = TREE_VALUE (inst);
4881 if (TYPE_P (instance)
4882 ? (COMPLETE_TYPE_P (instance)
4883 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4884 : DECL_TEMPLATE_INSTANTIATION (instance))
4886 tree spec = most_specialized_partial_spec (instance, tf_none);
4887 tree inst_decl = (DECL_P (instance)
4888 ? instance : TYPE_NAME (instance));
4889 if (!spec)
4890 /* OK */;
4891 else if (spec == error_mark_node)
4892 permerror (input_location,
4893 "declaration of %qD ambiguates earlier template "
4894 "instantiation for %qD", decl, inst_decl);
4895 else if (TREE_VALUE (spec) == tmpl)
4896 permerror (input_location,
4897 "partial specialization of %qD after instantiation "
4898 "of %qD", decl, inst_decl);
4902 return decl;
4905 /* PARM is a template parameter of some form; return the corresponding
4906 TEMPLATE_PARM_INDEX. */
4908 static tree
4909 get_template_parm_index (tree parm)
4911 if (TREE_CODE (parm) == PARM_DECL
4912 || TREE_CODE (parm) == CONST_DECL)
4913 parm = DECL_INITIAL (parm);
4914 else if (TREE_CODE (parm) == TYPE_DECL
4915 || TREE_CODE (parm) == TEMPLATE_DECL)
4916 parm = TREE_TYPE (parm);
4917 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4918 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4919 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4920 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4921 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4922 return parm;
4925 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4926 parameter packs used by the template parameter PARM. */
4928 static void
4929 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4931 /* A type parm can't refer to another parm. */
4932 if (TREE_CODE (parm) == TYPE_DECL)
4933 return;
4934 else if (TREE_CODE (parm) == PARM_DECL)
4936 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4937 ppd, ppd->visited);
4938 return;
4941 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4943 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4944 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4945 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4948 /* PARM is a template parameter pack. Return any parameter packs used in
4949 its type or the type of any of its template parameters. If there are
4950 any such packs, it will be instantiated into a fixed template parameter
4951 list by partial instantiation rather than be fully deduced. */
4953 tree
4954 fixed_parameter_pack_p (tree parm)
4956 /* This can only be true in a member template. */
4957 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4958 return NULL_TREE;
4959 /* This can only be true for a parameter pack. */
4960 if (!template_parameter_pack_p (parm))
4961 return NULL_TREE;
4962 /* A type parm can't refer to another parm. */
4963 if (TREE_CODE (parm) == TYPE_DECL)
4964 return NULL_TREE;
4966 tree parameter_packs = NULL_TREE;
4967 struct find_parameter_pack_data ppd;
4968 ppd.parameter_packs = &parameter_packs;
4969 ppd.visited = new hash_set<tree>;
4970 ppd.type_pack_expansion_p = false;
4972 fixed_parameter_pack_p_1 (parm, &ppd);
4974 delete ppd.visited;
4975 return parameter_packs;
4978 /* Check that a template declaration's use of default arguments and
4979 parameter packs is not invalid. Here, PARMS are the template
4980 parameters. IS_PRIMARY is true if DECL is the thing declared by
4981 a primary template. IS_PARTIAL is true if DECL is a partial
4982 specialization.
4984 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4985 declaration (but not a definition); 1 indicates a declaration, 2
4986 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4987 emitted for extraneous default arguments.
4989 Returns TRUE if there were no errors found, FALSE otherwise. */
4991 bool
4992 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4993 bool is_partial, int is_friend_decl)
4995 const char *msg;
4996 int last_level_to_check;
4997 tree parm_level;
4998 bool no_errors = true;
5000 /* [temp.param]
5002 A default template-argument shall not be specified in a
5003 function template declaration or a function template definition, nor
5004 in the template-parameter-list of the definition of a member of a
5005 class template. */
5007 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5008 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5009 /* You can't have a function template declaration in a local
5010 scope, nor you can you define a member of a class template in a
5011 local scope. */
5012 return true;
5014 if ((TREE_CODE (decl) == TYPE_DECL
5015 && TREE_TYPE (decl)
5016 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5017 || (TREE_CODE (decl) == FUNCTION_DECL
5018 && LAMBDA_FUNCTION_P (decl)))
5019 /* A lambda doesn't have an explicit declaration; don't complain
5020 about the parms of the enclosing class. */
5021 return true;
5023 if (current_class_type
5024 && !TYPE_BEING_DEFINED (current_class_type)
5025 && DECL_LANG_SPECIFIC (decl)
5026 && DECL_DECLARES_FUNCTION_P (decl)
5027 /* If this is either a friend defined in the scope of the class
5028 or a member function. */
5029 && (DECL_FUNCTION_MEMBER_P (decl)
5030 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5031 : DECL_FRIEND_CONTEXT (decl)
5032 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5033 : false)
5034 /* And, if it was a member function, it really was defined in
5035 the scope of the class. */
5036 && (!DECL_FUNCTION_MEMBER_P (decl)
5037 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5038 /* We already checked these parameters when the template was
5039 declared, so there's no need to do it again now. This function
5040 was defined in class scope, but we're processing its body now
5041 that the class is complete. */
5042 return true;
5044 /* Core issue 226 (C++0x only): the following only applies to class
5045 templates. */
5046 if (is_primary
5047 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5049 /* [temp.param]
5051 If a template-parameter has a default template-argument, all
5052 subsequent template-parameters shall have a default
5053 template-argument supplied. */
5054 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5056 tree inner_parms = TREE_VALUE (parm_level);
5057 int ntparms = TREE_VEC_LENGTH (inner_parms);
5058 int seen_def_arg_p = 0;
5059 int i;
5061 for (i = 0; i < ntparms; ++i)
5063 tree parm = TREE_VEC_ELT (inner_parms, i);
5065 if (parm == error_mark_node)
5066 continue;
5068 if (TREE_PURPOSE (parm))
5069 seen_def_arg_p = 1;
5070 else if (seen_def_arg_p
5071 && !template_parameter_pack_p (TREE_VALUE (parm)))
5073 error ("no default argument for %qD", TREE_VALUE (parm));
5074 /* For better subsequent error-recovery, we indicate that
5075 there should have been a default argument. */
5076 TREE_PURPOSE (parm) = error_mark_node;
5077 no_errors = false;
5079 else if (!is_partial
5080 && !is_friend_decl
5081 /* Don't complain about an enclosing partial
5082 specialization. */
5083 && parm_level == parms
5084 && TREE_CODE (decl) == TYPE_DECL
5085 && i < ntparms - 1
5086 && template_parameter_pack_p (TREE_VALUE (parm))
5087 /* A fixed parameter pack will be partially
5088 instantiated into a fixed length list. */
5089 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5091 /* A primary class template can only have one
5092 parameter pack, at the end of the template
5093 parameter list. */
5095 error ("parameter pack %q+D must be at the end of the"
5096 " template parameter list", TREE_VALUE (parm));
5098 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5099 = error_mark_node;
5100 no_errors = false;
5106 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5107 || is_partial
5108 || !is_primary
5109 || is_friend_decl)
5110 /* For an ordinary class template, default template arguments are
5111 allowed at the innermost level, e.g.:
5112 template <class T = int>
5113 struct S {};
5114 but, in a partial specialization, they're not allowed even
5115 there, as we have in [temp.class.spec]:
5117 The template parameter list of a specialization shall not
5118 contain default template argument values.
5120 So, for a partial specialization, or for a function template
5121 (in C++98/C++03), we look at all of them. */
5123 else
5124 /* But, for a primary class template that is not a partial
5125 specialization we look at all template parameters except the
5126 innermost ones. */
5127 parms = TREE_CHAIN (parms);
5129 /* Figure out what error message to issue. */
5130 if (is_friend_decl == 2)
5131 msg = G_("default template arguments may not be used in function template "
5132 "friend re-declaration");
5133 else if (is_friend_decl)
5134 msg = G_("default template arguments may not be used in function template "
5135 "friend declarations");
5136 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5137 msg = G_("default template arguments may not be used in function templates "
5138 "without -std=c++11 or -std=gnu++11");
5139 else if (is_partial)
5140 msg = G_("default template arguments may not be used in "
5141 "partial specializations");
5142 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5143 msg = G_("default argument for template parameter for class enclosing %qD");
5144 else
5145 /* Per [temp.param]/9, "A default template-argument shall not be
5146 specified in the template-parameter-lists of the definition of
5147 a member of a class template that appears outside of the member's
5148 class.", thus if we aren't handling a member of a class template
5149 there is no need to examine the parameters. */
5150 return true;
5152 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5153 /* If we're inside a class definition, there's no need to
5154 examine the parameters to the class itself. On the one
5155 hand, they will be checked when the class is defined, and,
5156 on the other, default arguments are valid in things like:
5157 template <class T = double>
5158 struct S { template <class U> void f(U); };
5159 Here the default argument for `S' has no bearing on the
5160 declaration of `f'. */
5161 last_level_to_check = template_class_depth (current_class_type) + 1;
5162 else
5163 /* Check everything. */
5164 last_level_to_check = 0;
5166 for (parm_level = parms;
5167 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5168 parm_level = TREE_CHAIN (parm_level))
5170 tree inner_parms = TREE_VALUE (parm_level);
5171 int i;
5172 int ntparms;
5174 ntparms = TREE_VEC_LENGTH (inner_parms);
5175 for (i = 0; i < ntparms; ++i)
5177 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5178 continue;
5180 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5182 if (msg)
5184 no_errors = false;
5185 if (is_friend_decl == 2)
5186 return no_errors;
5188 error (msg, decl);
5189 msg = 0;
5192 /* Clear out the default argument so that we are not
5193 confused later. */
5194 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5198 /* At this point, if we're still interested in issuing messages,
5199 they must apply to classes surrounding the object declared. */
5200 if (msg)
5201 msg = G_("default argument for template parameter for class "
5202 "enclosing %qD");
5205 return no_errors;
5208 /* Worker for push_template_decl_real, called via
5209 for_each_template_parm. DATA is really an int, indicating the
5210 level of the parameters we are interested in. If T is a template
5211 parameter of that level, return nonzero. */
5213 static int
5214 template_parm_this_level_p (tree t, void* data)
5216 int this_level = *(int *)data;
5217 int level;
5219 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5220 level = TEMPLATE_PARM_LEVEL (t);
5221 else
5222 level = TEMPLATE_TYPE_LEVEL (t);
5223 return level == this_level;
5226 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5227 DATA is really an int, indicating the innermost outer level of parameters.
5228 If T is a template parameter of that level or further out, return
5229 nonzero. */
5231 static int
5232 template_parm_outer_level (tree t, void *data)
5234 int this_level = *(int *)data;
5235 int level;
5237 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5238 level = TEMPLATE_PARM_LEVEL (t);
5239 else
5240 level = TEMPLATE_TYPE_LEVEL (t);
5241 return level <= this_level;
5244 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5245 parameters given by current_template_args, or reuses a
5246 previously existing one, if appropriate. Returns the DECL, or an
5247 equivalent one, if it is replaced via a call to duplicate_decls.
5249 If IS_FRIEND is true, DECL is a friend declaration. */
5251 tree
5252 push_template_decl_real (tree decl, bool is_friend)
5254 tree tmpl;
5255 tree args;
5256 tree info;
5257 tree ctx;
5258 bool is_primary;
5259 bool is_partial;
5260 int new_template_p = 0;
5261 /* True if the template is a member template, in the sense of
5262 [temp.mem]. */
5263 bool member_template_p = false;
5265 if (decl == error_mark_node || !current_template_parms)
5266 return error_mark_node;
5268 /* See if this is a partial specialization. */
5269 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5270 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5271 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5272 || (VAR_P (decl)
5273 && DECL_LANG_SPECIFIC (decl)
5274 && DECL_TEMPLATE_SPECIALIZATION (decl)
5275 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5277 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5278 is_friend = true;
5280 if (is_friend)
5281 /* For a friend, we want the context of the friend function, not
5282 the type of which it is a friend. */
5283 ctx = CP_DECL_CONTEXT (decl);
5284 else if (CP_DECL_CONTEXT (decl)
5285 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5286 /* In the case of a virtual function, we want the class in which
5287 it is defined. */
5288 ctx = CP_DECL_CONTEXT (decl);
5289 else
5290 /* Otherwise, if we're currently defining some class, the DECL
5291 is assumed to be a member of the class. */
5292 ctx = current_scope ();
5294 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5295 ctx = NULL_TREE;
5297 if (!DECL_CONTEXT (decl))
5298 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5300 /* See if this is a primary template. */
5301 if (is_friend && ctx
5302 && uses_template_parms_level (ctx, processing_template_decl))
5303 /* A friend template that specifies a class context, i.e.
5304 template <typename T> friend void A<T>::f();
5305 is not primary. */
5306 is_primary = false;
5307 else if (TREE_CODE (decl) == TYPE_DECL
5308 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5309 is_primary = false;
5310 else
5311 is_primary = template_parm_scope_p ();
5313 if (is_primary)
5315 warning (OPT_Wtemplates, "template %qD declared", decl);
5317 if (DECL_CLASS_SCOPE_P (decl))
5318 member_template_p = true;
5319 if (TREE_CODE (decl) == TYPE_DECL
5320 && anon_aggrname_p (DECL_NAME (decl)))
5322 error ("template class without a name");
5323 return error_mark_node;
5325 else if (TREE_CODE (decl) == FUNCTION_DECL)
5327 if (member_template_p)
5329 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5330 error ("member template %qD may not have virt-specifiers", decl);
5332 if (DECL_DESTRUCTOR_P (decl))
5334 /* [temp.mem]
5336 A destructor shall not be a member template. */
5337 error ("destructor %qD declared as member template", decl);
5338 return error_mark_node;
5340 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5341 && (!prototype_p (TREE_TYPE (decl))
5342 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5343 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5344 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5345 == void_list_node)))
5347 /* [basic.stc.dynamic.allocation]
5349 An allocation function can be a function
5350 template. ... Template allocation functions shall
5351 have two or more parameters. */
5352 error ("invalid template declaration of %qD", decl);
5353 return error_mark_node;
5356 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5357 && CLASS_TYPE_P (TREE_TYPE (decl)))
5359 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5360 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5361 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5363 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5364 if (TREE_CODE (t) == TYPE_DECL)
5365 t = TREE_TYPE (t);
5366 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5367 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5370 else if (TREE_CODE (decl) == TYPE_DECL
5371 && TYPE_DECL_ALIAS_P (decl))
5372 /* alias-declaration */
5373 gcc_assert (!DECL_ARTIFICIAL (decl));
5374 else if (VAR_P (decl))
5375 /* C++14 variable template. */;
5376 else
5378 error ("template declaration of %q#D", decl);
5379 return error_mark_node;
5383 /* Check to see that the rules regarding the use of default
5384 arguments are not being violated. */
5385 check_default_tmpl_args (decl, current_template_parms,
5386 is_primary, is_partial, /*is_friend_decl=*/0);
5388 /* Ensure that there are no parameter packs in the type of this
5389 declaration that have not been expanded. */
5390 if (TREE_CODE (decl) == FUNCTION_DECL)
5392 /* Check each of the arguments individually to see if there are
5393 any bare parameter packs. */
5394 tree type = TREE_TYPE (decl);
5395 tree arg = DECL_ARGUMENTS (decl);
5396 tree argtype = TYPE_ARG_TYPES (type);
5398 while (arg && argtype)
5400 if (!DECL_PACK_P (arg)
5401 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5403 /* This is a PARM_DECL that contains unexpanded parameter
5404 packs. We have already complained about this in the
5405 check_for_bare_parameter_packs call, so just replace
5406 these types with ERROR_MARK_NODE. */
5407 TREE_TYPE (arg) = error_mark_node;
5408 TREE_VALUE (argtype) = error_mark_node;
5411 arg = DECL_CHAIN (arg);
5412 argtype = TREE_CHAIN (argtype);
5415 /* Check for bare parameter packs in the return type and the
5416 exception specifiers. */
5417 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5418 /* Errors were already issued, set return type to int
5419 as the frontend doesn't expect error_mark_node as
5420 the return type. */
5421 TREE_TYPE (type) = integer_type_node;
5422 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5423 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5425 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5426 && TYPE_DECL_ALIAS_P (decl))
5427 ? DECL_ORIGINAL_TYPE (decl)
5428 : TREE_TYPE (decl)))
5430 TREE_TYPE (decl) = error_mark_node;
5431 return error_mark_node;
5434 if (is_partial)
5435 return process_partial_specialization (decl);
5437 args = current_template_args ();
5439 if (!ctx
5440 || TREE_CODE (ctx) == FUNCTION_DECL
5441 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5442 || (TREE_CODE (decl) == TYPE_DECL
5443 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5444 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5446 if (DECL_LANG_SPECIFIC (decl)
5447 && DECL_TEMPLATE_INFO (decl)
5448 && DECL_TI_TEMPLATE (decl))
5449 tmpl = DECL_TI_TEMPLATE (decl);
5450 /* If DECL is a TYPE_DECL for a class-template, then there won't
5451 be DECL_LANG_SPECIFIC. The information equivalent to
5452 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5453 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5454 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5455 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5457 /* Since a template declaration already existed for this
5458 class-type, we must be redeclaring it here. Make sure
5459 that the redeclaration is valid. */
5460 redeclare_class_template (TREE_TYPE (decl),
5461 current_template_parms,
5462 current_template_constraints ());
5463 /* We don't need to create a new TEMPLATE_DECL; just use the
5464 one we already had. */
5465 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5467 else
5469 tmpl = build_template_decl (decl, current_template_parms,
5470 member_template_p);
5471 new_template_p = 1;
5473 if (DECL_LANG_SPECIFIC (decl)
5474 && DECL_TEMPLATE_SPECIALIZATION (decl))
5476 /* A specialization of a member template of a template
5477 class. */
5478 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5479 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5480 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5484 else
5486 tree a, t, current, parms;
5487 int i;
5488 tree tinfo = get_template_info (decl);
5490 if (!tinfo)
5492 error ("template definition of non-template %q#D", decl);
5493 return error_mark_node;
5496 tmpl = TI_TEMPLATE (tinfo);
5498 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5499 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5500 && DECL_TEMPLATE_SPECIALIZATION (decl)
5501 && DECL_MEMBER_TEMPLATE_P (tmpl))
5503 tree new_tmpl;
5505 /* The declaration is a specialization of a member
5506 template, declared outside the class. Therefore, the
5507 innermost template arguments will be NULL, so we
5508 replace them with the arguments determined by the
5509 earlier call to check_explicit_specialization. */
5510 args = DECL_TI_ARGS (decl);
5512 new_tmpl
5513 = build_template_decl (decl, current_template_parms,
5514 member_template_p);
5515 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5516 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5517 DECL_TI_TEMPLATE (decl) = new_tmpl;
5518 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5519 DECL_TEMPLATE_INFO (new_tmpl)
5520 = build_template_info (tmpl, args);
5522 register_specialization (new_tmpl,
5523 most_general_template (tmpl),
5524 args,
5525 is_friend, 0);
5526 return decl;
5529 /* Make sure the template headers we got make sense. */
5531 parms = DECL_TEMPLATE_PARMS (tmpl);
5532 i = TMPL_PARMS_DEPTH (parms);
5533 if (TMPL_ARGS_DEPTH (args) != i)
5535 error ("expected %d levels of template parms for %q#D, got %d",
5536 i, decl, TMPL_ARGS_DEPTH (args));
5537 DECL_INTERFACE_KNOWN (decl) = 1;
5538 return error_mark_node;
5540 else
5541 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5543 a = TMPL_ARGS_LEVEL (args, i);
5544 t = INNERMOST_TEMPLATE_PARMS (parms);
5546 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5548 if (current == decl)
5549 error ("got %d template parameters for %q#D",
5550 TREE_VEC_LENGTH (a), decl);
5551 else
5552 error ("got %d template parameters for %q#T",
5553 TREE_VEC_LENGTH (a), current);
5554 error (" but %d required", TREE_VEC_LENGTH (t));
5555 /* Avoid crash in import_export_decl. */
5556 DECL_INTERFACE_KNOWN (decl) = 1;
5557 return error_mark_node;
5560 if (current == decl)
5561 current = ctx;
5562 else if (current == NULL_TREE)
5563 /* Can happen in erroneous input. */
5564 break;
5565 else
5566 current = get_containing_scope (current);
5569 /* Check that the parms are used in the appropriate qualifying scopes
5570 in the declarator. */
5571 if (!comp_template_args
5572 (TI_ARGS (tinfo),
5573 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5575 error ("\
5576 template arguments to %qD do not match original template %qD",
5577 decl, DECL_TEMPLATE_RESULT (tmpl));
5578 if (!uses_template_parms (TI_ARGS (tinfo)))
5579 inform (input_location, "use template<> for an explicit specialization");
5580 /* Avoid crash in import_export_decl. */
5581 DECL_INTERFACE_KNOWN (decl) = 1;
5582 return error_mark_node;
5586 DECL_TEMPLATE_RESULT (tmpl) = decl;
5587 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5589 /* Push template declarations for global functions and types. Note
5590 that we do not try to push a global template friend declared in a
5591 template class; such a thing may well depend on the template
5592 parameters of the class. */
5593 if (new_template_p && !ctx
5594 && !(is_friend && template_class_depth (current_class_type) > 0))
5596 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5597 if (tmpl == error_mark_node)
5598 return error_mark_node;
5600 /* Hide template friend classes that haven't been declared yet. */
5601 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5603 DECL_ANTICIPATED (tmpl) = 1;
5604 DECL_FRIEND_P (tmpl) = 1;
5608 if (is_primary)
5610 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5611 int i;
5613 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5614 if (DECL_CONV_FN_P (tmpl))
5616 int depth = TMPL_PARMS_DEPTH (parms);
5618 /* It is a conversion operator. See if the type converted to
5619 depends on innermost template operands. */
5621 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5622 depth))
5623 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5626 /* Give template template parms a DECL_CONTEXT of the template
5627 for which they are a parameter. */
5628 parms = INNERMOST_TEMPLATE_PARMS (parms);
5629 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5631 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5632 if (TREE_CODE (parm) == TEMPLATE_DECL)
5633 DECL_CONTEXT (parm) = tmpl;
5636 if (TREE_CODE (decl) == TYPE_DECL
5637 && TYPE_DECL_ALIAS_P (decl)
5638 && complex_alias_template_p (tmpl))
5639 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5642 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5643 back to its most general template. If TMPL is a specialization,
5644 ARGS may only have the innermost set of arguments. Add the missing
5645 argument levels if necessary. */
5646 if (DECL_TEMPLATE_INFO (tmpl))
5647 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5649 info = build_template_info (tmpl, args);
5651 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5652 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5653 else
5655 if (is_primary)
5656 retrofit_lang_decl (decl);
5657 if (DECL_LANG_SPECIFIC (decl))
5658 DECL_TEMPLATE_INFO (decl) = info;
5661 if (flag_implicit_templates
5662 && !is_friend
5663 && TREE_PUBLIC (decl)
5664 && VAR_OR_FUNCTION_DECL_P (decl))
5665 /* Set DECL_COMDAT on template instantiations; if we force
5666 them to be emitted by explicit instantiation or -frepo,
5667 mark_needed will tell cgraph to do the right thing. */
5668 DECL_COMDAT (decl) = true;
5670 return DECL_TEMPLATE_RESULT (tmpl);
5673 tree
5674 push_template_decl (tree decl)
5676 return push_template_decl_real (decl, false);
5679 /* FN is an inheriting constructor that inherits from the constructor
5680 template INHERITED; turn FN into a constructor template with a matching
5681 template header. */
5683 tree
5684 add_inherited_template_parms (tree fn, tree inherited)
5686 tree inner_parms
5687 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5688 inner_parms = copy_node (inner_parms);
5689 tree parms
5690 = tree_cons (size_int (processing_template_decl + 1),
5691 inner_parms, current_template_parms);
5692 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5693 tree args = template_parms_to_args (parms);
5694 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5695 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5696 DECL_TEMPLATE_RESULT (tmpl) = fn;
5697 DECL_ARTIFICIAL (tmpl) = true;
5698 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5699 return tmpl;
5702 /* Called when a class template TYPE is redeclared with the indicated
5703 template PARMS, e.g.:
5705 template <class T> struct S;
5706 template <class T> struct S {}; */
5708 bool
5709 redeclare_class_template (tree type, tree parms, tree cons)
5711 tree tmpl;
5712 tree tmpl_parms;
5713 int i;
5715 if (!TYPE_TEMPLATE_INFO (type))
5717 error ("%qT is not a template type", type);
5718 return false;
5721 tmpl = TYPE_TI_TEMPLATE (type);
5722 if (!PRIMARY_TEMPLATE_P (tmpl))
5723 /* The type is nested in some template class. Nothing to worry
5724 about here; there are no new template parameters for the nested
5725 type. */
5726 return true;
5728 if (!parms)
5730 error ("template specifiers not specified in declaration of %qD",
5731 tmpl);
5732 return false;
5735 parms = INNERMOST_TEMPLATE_PARMS (parms);
5736 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5738 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5740 error_n (input_location, TREE_VEC_LENGTH (parms),
5741 "redeclared with %d template parameter",
5742 "redeclared with %d template parameters",
5743 TREE_VEC_LENGTH (parms));
5744 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5745 "previous declaration %qD used %d template parameter",
5746 "previous declaration %qD used %d template parameters",
5747 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5748 return false;
5751 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5753 tree tmpl_parm;
5754 tree parm;
5755 tree tmpl_default;
5756 tree parm_default;
5758 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5759 || TREE_VEC_ELT (parms, i) == error_mark_node)
5760 continue;
5762 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5763 if (error_operand_p (tmpl_parm))
5764 return false;
5766 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5767 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5768 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5770 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5771 TEMPLATE_DECL. */
5772 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5773 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5774 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5775 || (TREE_CODE (tmpl_parm) != PARM_DECL
5776 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5777 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5778 || (TREE_CODE (tmpl_parm) == PARM_DECL
5779 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5780 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5782 error ("template parameter %q+#D", tmpl_parm);
5783 error ("redeclared here as %q#D", parm);
5784 return false;
5787 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5789 /* We have in [temp.param]:
5791 A template-parameter may not be given default arguments
5792 by two different declarations in the same scope. */
5793 error_at (input_location, "redefinition of default argument for %q#D", parm);
5794 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5795 "original definition appeared here");
5796 return false;
5799 if (parm_default != NULL_TREE)
5800 /* Update the previous template parameters (which are the ones
5801 that will really count) with the new default value. */
5802 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5803 else if (tmpl_default != NULL_TREE)
5804 /* Update the new parameters, too; they'll be used as the
5805 parameters for any members. */
5806 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5808 /* Give each template template parm in this redeclaration a
5809 DECL_CONTEXT of the template for which they are a parameter. */
5810 if (TREE_CODE (parm) == TEMPLATE_DECL)
5812 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5813 DECL_CONTEXT (parm) = tmpl;
5816 if (TREE_CODE (parm) == TYPE_DECL)
5817 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5820 // Cannot redeclare a class template with a different set of constraints.
5821 if (!equivalent_constraints (get_constraints (tmpl), cons))
5823 error_at (input_location, "redeclaration %q#D with different "
5824 "constraints", tmpl);
5825 inform (DECL_SOURCE_LOCATION (tmpl),
5826 "original declaration appeared here");
5829 return true;
5832 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5833 to be used when the caller has already checked
5834 (processing_template_decl
5835 && !instantiation_dependent_expression_p (expr)
5836 && potential_constant_expression (expr))
5837 and cleared processing_template_decl. */
5839 tree
5840 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5842 return tsubst_copy_and_build (expr,
5843 /*args=*/NULL_TREE,
5844 complain,
5845 /*in_decl=*/NULL_TREE,
5846 /*function_p=*/false,
5847 /*integral_constant_expression_p=*/true);
5850 /* Simplify EXPR if it is a non-dependent expression. Returns the
5851 (possibly simplified) expression. */
5853 tree
5854 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5856 if (expr == NULL_TREE)
5857 return NULL_TREE;
5859 /* If we're in a template, but EXPR isn't value dependent, simplify
5860 it. We're supposed to treat:
5862 template <typename T> void f(T[1 + 1]);
5863 template <typename T> void f(T[2]);
5865 as two declarations of the same function, for example. */
5866 if (processing_template_decl
5867 && is_nondependent_constant_expression (expr))
5869 processing_template_decl_sentinel s;
5870 expr = instantiate_non_dependent_expr_internal (expr, complain);
5872 return expr;
5875 tree
5876 instantiate_non_dependent_expr (tree expr)
5878 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5881 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5882 an uninstantiated expression. */
5884 tree
5885 instantiate_non_dependent_or_null (tree expr)
5887 if (expr == NULL_TREE)
5888 return NULL_TREE;
5889 if (processing_template_decl)
5891 if (!is_nondependent_constant_expression (expr))
5892 expr = NULL_TREE;
5893 else
5895 processing_template_decl_sentinel s;
5896 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5899 return expr;
5902 /* True iff T is a specialization of a variable template. */
5904 bool
5905 variable_template_specialization_p (tree t)
5907 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5908 return false;
5909 tree tmpl = DECL_TI_TEMPLATE (t);
5910 return variable_template_p (tmpl);
5913 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5914 template declaration, or a TYPE_DECL for an alias declaration. */
5916 bool
5917 alias_type_or_template_p (tree t)
5919 if (t == NULL_TREE)
5920 return false;
5921 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5922 || (TYPE_P (t)
5923 && TYPE_NAME (t)
5924 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5925 || DECL_ALIAS_TEMPLATE_P (t));
5928 /* Return TRUE iff T is a specialization of an alias template. */
5930 bool
5931 alias_template_specialization_p (const_tree t)
5933 /* It's an alias template specialization if it's an alias and its
5934 TYPE_NAME is a specialization of a primary template. */
5935 if (TYPE_ALIAS_P (t))
5936 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5937 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5939 return false;
5942 /* An alias template is complex from a SFINAE perspective if a template-id
5943 using that alias can be ill-formed when the expansion is not, as with
5944 the void_t template. We determine this by checking whether the
5945 expansion for the alias template uses all its template parameters. */
5947 struct uses_all_template_parms_data
5949 int level;
5950 bool *seen;
5953 static int
5954 uses_all_template_parms_r (tree t, void *data_)
5956 struct uses_all_template_parms_data &data
5957 = *(struct uses_all_template_parms_data*)data_;
5958 tree idx = get_template_parm_index (t);
5960 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5961 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5962 return 0;
5965 static bool
5966 complex_alias_template_p (const_tree tmpl)
5968 struct uses_all_template_parms_data data;
5969 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5970 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5971 data.level = TMPL_PARMS_DEPTH (parms);
5972 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5973 data.seen = XALLOCAVEC (bool, len);
5974 for (int i = 0; i < len; ++i)
5975 data.seen[i] = false;
5977 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5978 for (int i = 0; i < len; ++i)
5979 if (!data.seen[i])
5980 return true;
5981 return false;
5984 /* Return TRUE iff T is a specialization of a complex alias template with
5985 dependent template-arguments. */
5987 bool
5988 dependent_alias_template_spec_p (const_tree t)
5990 if (!alias_template_specialization_p (t))
5991 return false;
5993 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5994 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5995 return false;
5997 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5998 if (!any_dependent_template_arguments_p (args))
5999 return false;
6001 return true;
6004 /* Return the number of innermost template parameters in TMPL. */
6006 static int
6007 num_innermost_template_parms (tree tmpl)
6009 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6010 return TREE_VEC_LENGTH (parms);
6013 /* Return either TMPL or another template that it is equivalent to under DR
6014 1286: An alias that just changes the name of a template is equivalent to
6015 the other template. */
6017 static tree
6018 get_underlying_template (tree tmpl)
6020 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6021 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6023 /* Determine if the alias is equivalent to an underlying template. */
6024 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6025 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6026 if (!tinfo)
6027 break;
6029 tree underlying = TI_TEMPLATE (tinfo);
6030 if (!PRIMARY_TEMPLATE_P (underlying)
6031 || (num_innermost_template_parms (tmpl)
6032 != num_innermost_template_parms (underlying)))
6033 break;
6035 tree alias_args = INNERMOST_TEMPLATE_ARGS
6036 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6037 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6038 break;
6040 /* Alias is equivalent. Strip it and repeat. */
6041 tmpl = underlying;
6044 return tmpl;
6047 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6048 must be a reference-to-function or a pointer-to-function type, as specified
6049 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6050 and check that the resulting function has external linkage. */
6052 static tree
6053 convert_nontype_argument_function (tree type, tree expr,
6054 tsubst_flags_t complain)
6056 tree fns = expr;
6057 tree fn, fn_no_ptr;
6058 linkage_kind linkage;
6060 fn = instantiate_type (type, fns, tf_none);
6061 if (fn == error_mark_node)
6062 return error_mark_node;
6064 if (value_dependent_expression_p (fn))
6065 goto accept;
6067 fn_no_ptr = strip_fnptr_conv (fn);
6068 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6069 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6070 if (BASELINK_P (fn_no_ptr))
6071 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6073 /* [temp.arg.nontype]/1
6075 A template-argument for a non-type, non-template template-parameter
6076 shall be one of:
6077 [...]
6078 -- the address of an object or function with external [C++11: or
6079 internal] linkage. */
6081 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6083 if (complain & tf_error)
6085 error ("%qE is not a valid template argument for type %qT",
6086 expr, type);
6087 if (TYPE_PTR_P (type))
6088 inform (input_location, "it must be the address of a function "
6089 "with external linkage");
6090 else
6091 inform (input_location, "it must be the name of a function with "
6092 "external linkage");
6094 return NULL_TREE;
6097 linkage = decl_linkage (fn_no_ptr);
6098 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6100 if (complain & tf_error)
6102 if (cxx_dialect >= cxx11)
6103 error ("%qE is not a valid template argument for type %qT "
6104 "because %qD has no linkage",
6105 expr, type, fn_no_ptr);
6106 else
6107 error ("%qE is not a valid template argument for type %qT "
6108 "because %qD does not have external linkage",
6109 expr, type, fn_no_ptr);
6111 return NULL_TREE;
6114 accept:
6115 if (TREE_CODE (type) == REFERENCE_TYPE)
6116 fn = build_address (fn);
6117 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6118 fn = build_nop (type, fn);
6120 return fn;
6123 /* Subroutine of convert_nontype_argument.
6124 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6125 Emit an error otherwise. */
6127 static bool
6128 check_valid_ptrmem_cst_expr (tree type, tree expr,
6129 tsubst_flags_t complain)
6131 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6132 tree orig_expr = expr;
6133 STRIP_NOPS (expr);
6134 if (null_ptr_cst_p (expr))
6135 return true;
6136 if (TREE_CODE (expr) == PTRMEM_CST
6137 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6138 PTRMEM_CST_CLASS (expr)))
6139 return true;
6140 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6141 return true;
6142 if (processing_template_decl
6143 && TREE_CODE (expr) == ADDR_EXPR
6144 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6145 return true;
6146 if (complain & tf_error)
6148 error_at (loc, "%qE is not a valid template argument for type %qT",
6149 orig_expr, type);
6150 if (TREE_CODE (expr) != PTRMEM_CST)
6151 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6152 else
6153 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6155 return false;
6158 /* Returns TRUE iff the address of OP is value-dependent.
6160 14.6.2.4 [temp.dep.temp]:
6161 A non-integral non-type template-argument is dependent if its type is
6162 dependent or it has either of the following forms
6163 qualified-id
6164 & qualified-id
6165 and contains a nested-name-specifier which specifies a class-name that
6166 names a dependent type.
6168 We generalize this to just say that the address of a member of a
6169 dependent class is value-dependent; the above doesn't cover the
6170 address of a static data member named with an unqualified-id. */
6172 static bool
6173 has_value_dependent_address (tree op)
6175 /* We could use get_inner_reference here, but there's no need;
6176 this is only relevant for template non-type arguments, which
6177 can only be expressed as &id-expression. */
6178 if (DECL_P (op))
6180 tree ctx = CP_DECL_CONTEXT (op);
6181 if (TYPE_P (ctx) && dependent_type_p (ctx))
6182 return true;
6185 return false;
6188 /* The next set of functions are used for providing helpful explanatory
6189 diagnostics for failed overload resolution. Their messages should be
6190 indented by two spaces for consistency with the messages in
6191 call.c */
6193 static int
6194 unify_success (bool /*explain_p*/)
6196 return 0;
6199 /* Other failure functions should call this one, to provide a single function
6200 for setting a breakpoint on. */
6202 static int
6203 unify_invalid (bool /*explain_p*/)
6205 return 1;
6208 static int
6209 unify_parameter_deduction_failure (bool explain_p, tree parm)
6211 if (explain_p)
6212 inform (input_location,
6213 " couldn't deduce template parameter %qD", parm);
6214 return unify_invalid (explain_p);
6217 static int
6218 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6220 if (explain_p)
6221 inform (input_location,
6222 " types %qT and %qT have incompatible cv-qualifiers",
6223 parm, arg);
6224 return unify_invalid (explain_p);
6227 static int
6228 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6230 if (explain_p)
6231 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6232 return unify_invalid (explain_p);
6235 static int
6236 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6238 if (explain_p)
6239 inform (input_location,
6240 " template parameter %qD is not a parameter pack, but "
6241 "argument %qD is",
6242 parm, arg);
6243 return unify_invalid (explain_p);
6246 static int
6247 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6249 if (explain_p)
6250 inform (input_location,
6251 " template argument %qE does not match "
6252 "pointer-to-member constant %qE",
6253 arg, parm);
6254 return unify_invalid (explain_p);
6257 static int
6258 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6260 if (explain_p)
6261 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6262 return unify_invalid (explain_p);
6265 static int
6266 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6268 if (explain_p)
6269 inform (input_location,
6270 " inconsistent parameter pack deduction with %qT and %qT",
6271 old_arg, new_arg);
6272 return unify_invalid (explain_p);
6275 static int
6276 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6278 if (explain_p)
6280 if (TYPE_P (parm))
6281 inform (input_location,
6282 " deduced conflicting types for parameter %qT (%qT and %qT)",
6283 parm, first, second);
6284 else
6285 inform (input_location,
6286 " deduced conflicting values for non-type parameter "
6287 "%qE (%qE and %qE)", parm, first, second);
6289 return unify_invalid (explain_p);
6292 static int
6293 unify_vla_arg (bool explain_p, tree arg)
6295 if (explain_p)
6296 inform (input_location,
6297 " variable-sized array type %qT is not "
6298 "a valid template argument",
6299 arg);
6300 return unify_invalid (explain_p);
6303 static int
6304 unify_method_type_error (bool explain_p, tree arg)
6306 if (explain_p)
6307 inform (input_location,
6308 " member function type %qT is not a valid template argument",
6309 arg);
6310 return unify_invalid (explain_p);
6313 static int
6314 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6316 if (explain_p)
6318 if (least_p)
6319 inform_n (input_location, wanted,
6320 " candidate expects at least %d argument, %d provided",
6321 " candidate expects at least %d arguments, %d provided",
6322 wanted, have);
6323 else
6324 inform_n (input_location, wanted,
6325 " candidate expects %d argument, %d provided",
6326 " candidate expects %d arguments, %d provided",
6327 wanted, have);
6329 return unify_invalid (explain_p);
6332 static int
6333 unify_too_many_arguments (bool explain_p, int have, int wanted)
6335 return unify_arity (explain_p, have, wanted);
6338 static int
6339 unify_too_few_arguments (bool explain_p, int have, int wanted,
6340 bool least_p = false)
6342 return unify_arity (explain_p, have, wanted, least_p);
6345 static int
6346 unify_arg_conversion (bool explain_p, tree to_type,
6347 tree from_type, tree arg)
6349 if (explain_p)
6350 inform (EXPR_LOC_OR_LOC (arg, input_location),
6351 " cannot convert %qE (type %qT) to type %qT",
6352 arg, from_type, to_type);
6353 return unify_invalid (explain_p);
6356 static int
6357 unify_no_common_base (bool explain_p, enum template_base_result r,
6358 tree parm, tree arg)
6360 if (explain_p)
6361 switch (r)
6363 case tbr_ambiguous_baseclass:
6364 inform (input_location, " %qT is an ambiguous base class of %qT",
6365 parm, arg);
6366 break;
6367 default:
6368 inform (input_location, " %qT is not derived from %qT", arg, parm);
6369 break;
6371 return unify_invalid (explain_p);
6374 static int
6375 unify_inconsistent_template_template_parameters (bool explain_p)
6377 if (explain_p)
6378 inform (input_location,
6379 " template parameters of a template template argument are "
6380 "inconsistent with other deduced template arguments");
6381 return unify_invalid (explain_p);
6384 static int
6385 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6387 if (explain_p)
6388 inform (input_location,
6389 " can't deduce a template for %qT from non-template type %qT",
6390 parm, arg);
6391 return unify_invalid (explain_p);
6394 static int
6395 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6397 if (explain_p)
6398 inform (input_location,
6399 " template argument %qE does not match %qE", arg, parm);
6400 return unify_invalid (explain_p);
6403 static int
6404 unify_overload_resolution_failure (bool explain_p, tree arg)
6406 if (explain_p)
6407 inform (input_location,
6408 " could not resolve address from overloaded function %qE",
6409 arg);
6410 return unify_invalid (explain_p);
6413 /* Attempt to convert the non-type template parameter EXPR to the
6414 indicated TYPE. If the conversion is successful, return the
6415 converted value. If the conversion is unsuccessful, return
6416 NULL_TREE if we issued an error message, or error_mark_node if we
6417 did not. We issue error messages for out-and-out bad template
6418 parameters, but not simply because the conversion failed, since we
6419 might be just trying to do argument deduction. Both TYPE and EXPR
6420 must be non-dependent.
6422 The conversion follows the special rules described in
6423 [temp.arg.nontype], and it is much more strict than an implicit
6424 conversion.
6426 This function is called twice for each template argument (see
6427 lookup_template_class for a more accurate description of this
6428 problem). This means that we need to handle expressions which
6429 are not valid in a C++ source, but can be created from the
6430 first call (for instance, casts to perform conversions). These
6431 hacks can go away after we fix the double coercion problem. */
6433 static tree
6434 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6436 tree expr_type;
6437 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6438 tree orig_expr = expr;
6440 /* Detect immediately string literals as invalid non-type argument.
6441 This special-case is not needed for correctness (we would easily
6442 catch this later), but only to provide better diagnostic for this
6443 common user mistake. As suggested by DR 100, we do not mention
6444 linkage issues in the diagnostic as this is not the point. */
6445 /* FIXME we're making this OK. */
6446 if (TREE_CODE (expr) == STRING_CST)
6448 if (complain & tf_error)
6449 error ("%qE is not a valid template argument for type %qT "
6450 "because string literals can never be used in this context",
6451 expr, type);
6452 return NULL_TREE;
6455 /* Add the ADDR_EXPR now for the benefit of
6456 value_dependent_expression_p. */
6457 if (TYPE_PTROBV_P (type)
6458 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6460 expr = decay_conversion (expr, complain);
6461 if (expr == error_mark_node)
6462 return error_mark_node;
6465 /* If we are in a template, EXPR may be non-dependent, but still
6466 have a syntactic, rather than semantic, form. For example, EXPR
6467 might be a SCOPE_REF, rather than the VAR_DECL to which the
6468 SCOPE_REF refers. Preserving the qualifying scope is necessary
6469 so that access checking can be performed when the template is
6470 instantiated -- but here we need the resolved form so that we can
6471 convert the argument. */
6472 bool non_dep = false;
6473 if (TYPE_REF_OBJ_P (type)
6474 && has_value_dependent_address (expr))
6475 /* If we want the address and it's value-dependent, don't fold. */;
6476 else if (processing_template_decl
6477 && is_nondependent_constant_expression (expr))
6478 non_dep = true;
6479 if (error_operand_p (expr))
6480 return error_mark_node;
6481 expr_type = TREE_TYPE (expr);
6483 /* If the argument is non-dependent, perform any conversions in
6484 non-dependent context as well. */
6485 processing_template_decl_sentinel s (non_dep);
6486 if (non_dep)
6487 expr = instantiate_non_dependent_expr_internal (expr, complain);
6489 if (value_dependent_expression_p (expr))
6490 expr = canonicalize_expr_argument (expr, complain);
6492 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6493 to a non-type argument of "nullptr". */
6494 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6495 expr = fold_simple (convert (type, expr));
6497 /* In C++11, integral or enumeration non-type template arguments can be
6498 arbitrary constant expressions. Pointer and pointer to
6499 member arguments can be general constant expressions that evaluate
6500 to a null value, but otherwise still need to be of a specific form. */
6501 if (cxx_dialect >= cxx11)
6503 if (TREE_CODE (expr) == PTRMEM_CST)
6504 /* A PTRMEM_CST is already constant, and a valid template
6505 argument for a parameter of pointer to member type, we just want
6506 to leave it in that form rather than lower it to a
6507 CONSTRUCTOR. */;
6508 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6509 || cxx_dialect >= cxx1z)
6511 /* C++17: A template-argument for a non-type template-parameter shall
6512 be a converted constant expression (8.20) of the type of the
6513 template-parameter. */
6514 expr = build_converted_constant_expr (type, expr, complain);
6515 if (expr == error_mark_node)
6516 return error_mark_node;
6517 expr = maybe_constant_value (expr);
6518 expr = convert_from_reference (expr);
6520 else if (TYPE_PTR_OR_PTRMEM_P (type))
6522 tree folded = maybe_constant_value (expr);
6523 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6524 : null_member_pointer_value_p (folded))
6525 expr = folded;
6529 if (TREE_CODE (type) == REFERENCE_TYPE)
6530 expr = mark_lvalue_use (expr);
6531 else
6532 expr = mark_rvalue_use (expr);
6534 /* HACK: Due to double coercion, we can get a
6535 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6536 which is the tree that we built on the first call (see
6537 below when coercing to reference to object or to reference to
6538 function). We just strip everything and get to the arg.
6539 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6540 for examples. */
6541 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6543 tree probe_type, probe = expr;
6544 if (REFERENCE_REF_P (probe))
6545 probe = TREE_OPERAND (probe, 0);
6546 probe_type = TREE_TYPE (probe);
6547 if (TREE_CODE (probe) == NOP_EXPR)
6549 /* ??? Maybe we could use convert_from_reference here, but we
6550 would need to relax its constraints because the NOP_EXPR
6551 could actually change the type to something more cv-qualified,
6552 and this is not folded by convert_from_reference. */
6553 tree addr = TREE_OPERAND (probe, 0);
6554 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6555 && TREE_CODE (addr) == ADDR_EXPR
6556 && TYPE_PTR_P (TREE_TYPE (addr))
6557 && (same_type_ignoring_top_level_qualifiers_p
6558 (TREE_TYPE (probe_type),
6559 TREE_TYPE (TREE_TYPE (addr)))))
6561 expr = TREE_OPERAND (addr, 0);
6562 expr_type = TREE_TYPE (probe_type);
6567 /* [temp.arg.nontype]/5, bullet 1
6569 For a non-type template-parameter of integral or enumeration type,
6570 integral promotions (_conv.prom_) and integral conversions
6571 (_conv.integral_) are applied. */
6572 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6574 if (cxx_dialect < cxx11)
6576 tree t = build_converted_constant_expr (type, expr, complain);
6577 t = maybe_constant_value (t);
6578 if (t != error_mark_node)
6579 expr = t;
6582 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6583 return error_mark_node;
6585 /* Notice that there are constant expressions like '4 % 0' which
6586 do not fold into integer constants. */
6587 if (TREE_CODE (expr) != INTEGER_CST
6588 && !value_dependent_expression_p (expr))
6590 if (complain & tf_error)
6592 int errs = errorcount, warns = warningcount + werrorcount;
6593 if (!require_potential_constant_expression (expr))
6594 expr = error_mark_node;
6595 else
6596 expr = cxx_constant_value (expr);
6597 if (errorcount > errs || warningcount + werrorcount > warns)
6598 inform (loc, "in template argument for type %qT ", type);
6599 if (expr == error_mark_node)
6600 return NULL_TREE;
6601 /* else cxx_constant_value complained but gave us
6602 a real constant, so go ahead. */
6603 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6605 else
6606 return NULL_TREE;
6609 /* Avoid typedef problems. */
6610 if (TREE_TYPE (expr) != type)
6611 expr = fold_convert (type, expr);
6613 /* [temp.arg.nontype]/5, bullet 2
6615 For a non-type template-parameter of type pointer to object,
6616 qualification conversions (_conv.qual_) and the array-to-pointer
6617 conversion (_conv.array_) are applied. */
6618 else if (TYPE_PTROBV_P (type))
6620 tree decayed = expr;
6622 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6623 decay_conversion or an explicit cast. If it's a problematic cast,
6624 we'll complain about it below. */
6625 if (TREE_CODE (expr) == NOP_EXPR)
6627 tree probe = expr;
6628 STRIP_NOPS (probe);
6629 if (TREE_CODE (probe) == ADDR_EXPR
6630 && TYPE_PTR_P (TREE_TYPE (probe)))
6632 expr = probe;
6633 expr_type = TREE_TYPE (expr);
6637 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6639 A template-argument for a non-type, non-template template-parameter
6640 shall be one of: [...]
6642 -- the name of a non-type template-parameter;
6643 -- the address of an object or function with external linkage, [...]
6644 expressed as "& id-expression" where the & is optional if the name
6645 refers to a function or array, or if the corresponding
6646 template-parameter is a reference.
6648 Here, we do not care about functions, as they are invalid anyway
6649 for a parameter of type pointer-to-object. */
6651 if (value_dependent_expression_p (expr))
6652 /* Non-type template parameters are OK. */
6654 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6655 /* Null pointer values are OK in C++11. */;
6656 else if (TREE_CODE (expr) != ADDR_EXPR)
6658 if (VAR_P (expr))
6660 if (complain & tf_error)
6661 error ("%qD is not a valid template argument "
6662 "because %qD is a variable, not the address of "
6663 "a variable", orig_expr, expr);
6664 return NULL_TREE;
6666 if (POINTER_TYPE_P (expr_type))
6668 if (complain & tf_error)
6669 error ("%qE is not a valid template argument for %qT "
6670 "because it is not the address of a variable",
6671 orig_expr, type);
6672 return NULL_TREE;
6674 /* Other values, like integer constants, might be valid
6675 non-type arguments of some other type. */
6676 return error_mark_node;
6678 else
6680 tree decl = TREE_OPERAND (expr, 0);
6682 if (!VAR_P (decl))
6684 if (complain & tf_error)
6685 error ("%qE is not a valid template argument of type %qT "
6686 "because %qE is not a variable", orig_expr, type, decl);
6687 return NULL_TREE;
6689 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6691 if (complain & tf_error)
6692 error ("%qE is not a valid template argument of type %qT "
6693 "because %qD does not have external linkage",
6694 orig_expr, type, decl);
6695 return NULL_TREE;
6697 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx1z)
6698 && decl_linkage (decl) == lk_none)
6700 if (complain & tf_error)
6701 error ("%qE is not a valid template argument of type %qT "
6702 "because %qD has no linkage", orig_expr, type, decl);
6703 return NULL_TREE;
6705 /* C++17: For a non-type template-parameter of reference or pointer
6706 type, the value of the constant expression shall not refer to (or
6707 for a pointer type, shall not be the address of):
6708 * a subobject (4.5),
6709 * a temporary object (15.2),
6710 * a string literal (5.13.5),
6711 * the result of a typeid expression (8.2.8), or
6712 * a predefined __func__ variable (11.4.1). */
6713 else if (DECL_ARTIFICIAL (decl))
6715 if (complain & tf_error)
6716 error ("the address of %qD is not a valid template argument",
6717 decl);
6718 return NULL_TREE;
6720 else if (!same_type_ignoring_top_level_qualifiers_p
6721 (strip_array_types (TREE_TYPE (type)),
6722 strip_array_types (TREE_TYPE (decl))))
6724 if (complain & tf_error)
6725 error ("the address of the %qT subobject of %qD is not a "
6726 "valid template argument", TREE_TYPE (type), decl);
6727 return NULL_TREE;
6729 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6731 if (complain & tf_error)
6732 error ("the address of %qD is not a valid template argument "
6733 "because it does not have static storage duration",
6734 decl);
6735 return NULL_TREE;
6739 expr = decayed;
6741 expr = perform_qualification_conversions (type, expr);
6742 if (expr == error_mark_node)
6743 return error_mark_node;
6745 /* [temp.arg.nontype]/5, bullet 3
6747 For a non-type template-parameter of type reference to object, no
6748 conversions apply. The type referred to by the reference may be more
6749 cv-qualified than the (otherwise identical) type of the
6750 template-argument. The template-parameter is bound directly to the
6751 template-argument, which must be an lvalue. */
6752 else if (TYPE_REF_OBJ_P (type))
6754 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6755 expr_type))
6756 return error_mark_node;
6758 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6760 if (complain & tf_error)
6761 error ("%qE is not a valid template argument for type %qT "
6762 "because of conflicts in cv-qualification", expr, type);
6763 return NULL_TREE;
6766 if (!lvalue_p (expr))
6768 if (complain & tf_error)
6769 error ("%qE is not a valid template argument for type %qT "
6770 "because it is not an lvalue", expr, type);
6771 return NULL_TREE;
6774 /* [temp.arg.nontype]/1
6776 A template-argument for a non-type, non-template template-parameter
6777 shall be one of: [...]
6779 -- the address of an object or function with external linkage. */
6780 if (INDIRECT_REF_P (expr)
6781 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6783 expr = TREE_OPERAND (expr, 0);
6784 if (DECL_P (expr))
6786 if (complain & tf_error)
6787 error ("%q#D is not a valid template argument for type %qT "
6788 "because a reference variable does not have a constant "
6789 "address", expr, type);
6790 return NULL_TREE;
6794 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6795 && value_dependent_expression_p (expr))
6796 /* OK, dependent reference. We don't want to ask whether a DECL is
6797 itself value-dependent, since what we want here is its address. */;
6798 else
6800 if (!DECL_P (expr))
6802 if (complain & tf_error)
6803 error ("%qE is not a valid template argument for type %qT "
6804 "because it is not an object with linkage",
6805 expr, type);
6806 return NULL_TREE;
6809 /* DR 1155 allows internal linkage in C++11 and up. */
6810 linkage_kind linkage = decl_linkage (expr);
6811 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6813 if (complain & tf_error)
6814 error ("%qE is not a valid template argument for type %qT "
6815 "because object %qD does not have linkage",
6816 expr, type, expr);
6817 return NULL_TREE;
6820 expr = build_address (expr);
6823 if (!same_type_p (type, TREE_TYPE (expr)))
6824 expr = build_nop (type, expr);
6826 /* [temp.arg.nontype]/5, bullet 4
6828 For a non-type template-parameter of type pointer to function, only
6829 the function-to-pointer conversion (_conv.func_) is applied. If the
6830 template-argument represents a set of overloaded functions (or a
6831 pointer to such), the matching function is selected from the set
6832 (_over.over_). */
6833 else if (TYPE_PTRFN_P (type))
6835 /* If the argument is a template-id, we might not have enough
6836 context information to decay the pointer. */
6837 if (!type_unknown_p (expr_type))
6839 expr = decay_conversion (expr, complain);
6840 if (expr == error_mark_node)
6841 return error_mark_node;
6844 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6845 /* Null pointer values are OK in C++11. */
6846 return perform_qualification_conversions (type, expr);
6848 expr = convert_nontype_argument_function (type, expr, complain);
6849 if (!expr || expr == error_mark_node)
6850 return expr;
6852 /* [temp.arg.nontype]/5, bullet 5
6854 For a non-type template-parameter of type reference to function, no
6855 conversions apply. If the template-argument represents a set of
6856 overloaded functions, the matching function is selected from the set
6857 (_over.over_). */
6858 else if (TYPE_REFFN_P (type))
6860 if (TREE_CODE (expr) == ADDR_EXPR)
6862 if (complain & tf_error)
6864 error ("%qE is not a valid template argument for type %qT "
6865 "because it is a pointer", expr, type);
6866 inform (input_location, "try using %qE instead",
6867 TREE_OPERAND (expr, 0));
6869 return NULL_TREE;
6872 expr = convert_nontype_argument_function (type, expr, complain);
6873 if (!expr || expr == error_mark_node)
6874 return expr;
6876 /* [temp.arg.nontype]/5, bullet 6
6878 For a non-type template-parameter of type pointer to member function,
6879 no conversions apply. If the template-argument represents a set of
6880 overloaded member functions, the matching member function is selected
6881 from the set (_over.over_). */
6882 else if (TYPE_PTRMEMFUNC_P (type))
6884 expr = instantiate_type (type, expr, tf_none);
6885 if (expr == error_mark_node)
6886 return error_mark_node;
6888 /* [temp.arg.nontype] bullet 1 says the pointer to member
6889 expression must be a pointer-to-member constant. */
6890 if (!value_dependent_expression_p (expr)
6891 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6892 return NULL_TREE;
6894 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6895 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6896 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6897 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6899 /* [temp.arg.nontype]/5, bullet 7
6901 For a non-type template-parameter of type pointer to data member,
6902 qualification conversions (_conv.qual_) are applied. */
6903 else if (TYPE_PTRDATAMEM_P (type))
6905 /* [temp.arg.nontype] bullet 1 says the pointer to member
6906 expression must be a pointer-to-member constant. */
6907 if (!value_dependent_expression_p (expr)
6908 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6909 return NULL_TREE;
6911 expr = perform_qualification_conversions (type, expr);
6912 if (expr == error_mark_node)
6913 return expr;
6915 else if (NULLPTR_TYPE_P (type))
6917 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
6919 if (complain & tf_error)
6920 error ("%qE is not a valid template argument for type %qT "
6921 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6922 return NULL_TREE;
6924 return expr;
6926 /* A template non-type parameter must be one of the above. */
6927 else
6928 gcc_unreachable ();
6930 /* Sanity check: did we actually convert the argument to the
6931 right type? */
6932 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6933 (type, TREE_TYPE (expr)));
6934 return convert_from_reference (expr);
6937 /* Subroutine of coerce_template_template_parms, which returns 1 if
6938 PARM_PARM and ARG_PARM match using the rule for the template
6939 parameters of template template parameters. Both PARM and ARG are
6940 template parameters; the rest of the arguments are the same as for
6941 coerce_template_template_parms.
6943 static int
6944 coerce_template_template_parm (tree parm,
6945 tree arg,
6946 tsubst_flags_t complain,
6947 tree in_decl,
6948 tree outer_args)
6950 if (arg == NULL_TREE || error_operand_p (arg)
6951 || parm == NULL_TREE || error_operand_p (parm))
6952 return 0;
6954 if (TREE_CODE (arg) != TREE_CODE (parm))
6955 return 0;
6957 switch (TREE_CODE (parm))
6959 case TEMPLATE_DECL:
6960 /* We encounter instantiations of templates like
6961 template <template <template <class> class> class TT>
6962 class C; */
6964 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6965 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6967 if (!coerce_template_template_parms
6968 (parmparm, argparm, complain, in_decl, outer_args))
6969 return 0;
6971 /* Fall through. */
6973 case TYPE_DECL:
6974 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6975 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6976 /* Argument is a parameter pack but parameter is not. */
6977 return 0;
6978 break;
6980 case PARM_DECL:
6981 /* The tsubst call is used to handle cases such as
6983 template <int> class C {};
6984 template <class T, template <T> class TT> class D {};
6985 D<int, C> d;
6987 i.e. the parameter list of TT depends on earlier parameters. */
6988 if (!uses_template_parms (TREE_TYPE (arg)))
6990 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6991 if (!uses_template_parms (t)
6992 && !same_type_p (t, TREE_TYPE (arg)))
6993 return 0;
6996 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6997 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6998 /* Argument is a parameter pack but parameter is not. */
6999 return 0;
7001 break;
7003 default:
7004 gcc_unreachable ();
7007 return 1;
7010 /* Coerce template argument list ARGLIST for use with template
7011 template-parameter TEMPL. */
7013 static tree
7014 coerce_template_args_for_ttp (tree templ, tree arglist,
7015 tsubst_flags_t complain)
7017 /* Consider an example where a template template parameter declared as
7019 template <class T, class U = std::allocator<T> > class TT
7021 The template parameter level of T and U are one level larger than
7022 of TT. To proper process the default argument of U, say when an
7023 instantiation `TT<int>' is seen, we need to build the full
7024 arguments containing {int} as the innermost level. Outer levels,
7025 available when not appearing as default template argument, can be
7026 obtained from the arguments of the enclosing template.
7028 Suppose that TT is later substituted with std::vector. The above
7029 instantiation is `TT<int, std::allocator<T> >' with TT at
7030 level 1, and T at level 2, while the template arguments at level 1
7031 becomes {std::vector} and the inner level 2 is {int}. */
7033 tree outer = DECL_CONTEXT (templ);
7034 if (outer)
7036 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7037 /* We want arguments for the partial specialization, not arguments for
7038 the primary template. */
7039 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7040 else
7041 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7043 else if (current_template_parms)
7045 /* This is an argument of the current template, so we haven't set
7046 DECL_CONTEXT yet. */
7047 tree relevant_template_parms;
7049 /* Parameter levels that are greater than the level of the given
7050 template template parm are irrelevant. */
7051 relevant_template_parms = current_template_parms;
7052 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7053 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7054 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7056 outer = template_parms_to_args (relevant_template_parms);
7059 if (outer)
7060 arglist = add_to_template_args (outer, arglist);
7062 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7063 return coerce_template_parms (parmlist, arglist, templ,
7064 complain,
7065 /*require_all_args=*/true,
7066 /*use_default_args=*/true);
7069 /* A cache of template template parameters with match-all default
7070 arguments. */
7071 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7072 static void
7073 store_defaulted_ttp (tree v, tree t)
7075 if (!defaulted_ttp_cache)
7076 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7077 defaulted_ttp_cache->put (v, t);
7079 static tree
7080 lookup_defaulted_ttp (tree v)
7082 if (defaulted_ttp_cache)
7083 if (tree *p = defaulted_ttp_cache->get (v))
7084 return *p;
7085 return NULL_TREE;
7088 /* T is a bound template template-parameter. Copy its arguments into default
7089 arguments of the template template-parameter's template parameters. */
7091 static tree
7092 add_defaults_to_ttp (tree otmpl)
7094 if (tree c = lookup_defaulted_ttp (otmpl))
7095 return c;
7097 tree ntmpl = copy_node (otmpl);
7099 tree ntype = copy_node (TREE_TYPE (otmpl));
7100 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7101 TYPE_MAIN_VARIANT (ntype) = ntype;
7102 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7103 TYPE_NAME (ntype) = ntmpl;
7104 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7106 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7107 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7108 TEMPLATE_PARM_DECL (idx) = ntmpl;
7109 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7111 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7112 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7113 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7114 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7115 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7117 tree o = TREE_VEC_ELT (vec, i);
7118 if (!template_parameter_pack_p (TREE_VALUE (o)))
7120 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7121 TREE_PURPOSE (n) = any_targ_node;
7125 store_defaulted_ttp (otmpl, ntmpl);
7126 return ntmpl;
7129 /* ARG is a bound potential template template-argument, and PARGS is a list
7130 of arguments for the corresponding template template-parameter. Adjust
7131 PARGS as appropriate for application to ARG's template, and if ARG is a
7132 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7133 arguments to the template template parameter. */
7135 static tree
7136 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7138 ++processing_template_decl;
7139 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7140 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7142 /* When comparing two template template-parameters in partial ordering,
7143 rewrite the one currently being used as an argument to have default
7144 arguments for all parameters. */
7145 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7146 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7147 if (pargs != error_mark_node)
7148 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7149 TYPE_TI_ARGS (arg));
7151 else
7153 tree aparms
7154 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7155 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7156 /*require_all*/true,
7157 /*use_default*/true);
7159 --processing_template_decl;
7160 return pargs;
7163 /* Subroutine of unify for the case when PARM is a
7164 BOUND_TEMPLATE_TEMPLATE_PARM. */
7166 static int
7167 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7168 bool explain_p)
7170 tree parmvec = TYPE_TI_ARGS (parm);
7171 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7173 /* The template template parm might be variadic and the argument
7174 not, so flatten both argument lists. */
7175 parmvec = expand_template_argument_pack (parmvec);
7176 argvec = expand_template_argument_pack (argvec);
7178 if (flag_new_ttp)
7180 /* In keeping with P0522R0, adjust P's template arguments
7181 to apply to A's template; then flatten it again. */
7182 tree nparmvec = parmvec;
7183 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7184 nparmvec = expand_template_argument_pack (nparmvec);
7186 if (unify (tparms, targs, nparmvec, argvec,
7187 UNIFY_ALLOW_NONE, explain_p))
7188 return 1;
7190 /* If the P0522 adjustment eliminated a pack expansion, deduce
7191 empty packs. */
7192 if (flag_new_ttp
7193 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7194 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7195 DEDUCE_EXACT, /*sub*/true, explain_p))
7196 return 1;
7198 else
7200 /* Deduce arguments T, i from TT<T> or TT<i>.
7201 We check each element of PARMVEC and ARGVEC individually
7202 rather than the whole TREE_VEC since they can have
7203 different number of elements, which is allowed under N2555. */
7205 int len = TREE_VEC_LENGTH (parmvec);
7207 /* Check if the parameters end in a pack, making them
7208 variadic. */
7209 int parm_variadic_p = 0;
7210 if (len > 0
7211 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7212 parm_variadic_p = 1;
7214 for (int i = 0; i < len - parm_variadic_p; ++i)
7215 /* If the template argument list of P contains a pack
7216 expansion that is not the last template argument, the
7217 entire template argument list is a non-deduced
7218 context. */
7219 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7220 return unify_success (explain_p);
7222 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7223 return unify_too_few_arguments (explain_p,
7224 TREE_VEC_LENGTH (argvec), len);
7226 for (int i = 0; i < len - parm_variadic_p; ++i)
7227 if (unify (tparms, targs,
7228 TREE_VEC_ELT (parmvec, i),
7229 TREE_VEC_ELT (argvec, i),
7230 UNIFY_ALLOW_NONE, explain_p))
7231 return 1;
7233 if (parm_variadic_p
7234 && unify_pack_expansion (tparms, targs,
7235 parmvec, argvec,
7236 DEDUCE_EXACT,
7237 /*subr=*/true, explain_p))
7238 return 1;
7241 return 0;
7244 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7245 template template parameters. Both PARM_PARMS and ARG_PARMS are
7246 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7247 or PARM_DECL.
7249 Consider the example:
7250 template <class T> class A;
7251 template<template <class U> class TT> class B;
7253 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7254 the parameters to A, and OUTER_ARGS contains A. */
7256 static int
7257 coerce_template_template_parms (tree parm_parms,
7258 tree arg_parms,
7259 tsubst_flags_t complain,
7260 tree in_decl,
7261 tree outer_args)
7263 int nparms, nargs, i;
7264 tree parm, arg;
7265 int variadic_p = 0;
7267 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7268 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7270 nparms = TREE_VEC_LENGTH (parm_parms);
7271 nargs = TREE_VEC_LENGTH (arg_parms);
7273 if (flag_new_ttp)
7275 /* P0522R0: A template template-parameter P is at least as specialized as
7276 a template template-argument A if, given the following rewrite to two
7277 function templates, the function template corresponding to P is at
7278 least as specialized as the function template corresponding to A
7279 according to the partial ordering rules for function templates
7280 ([temp.func.order]). Given an invented class template X with the
7281 template parameter list of A (including default arguments):
7283 * Each of the two function templates has the same template parameters,
7284 respectively, as P or A.
7286 * Each function template has a single function parameter whose type is
7287 a specialization of X with template arguments corresponding to the
7288 template parameters from the respective function template where, for
7289 each template parameter PP in the template parameter list of the
7290 function template, a corresponding template argument AA is formed. If
7291 PP declares a parameter pack, then AA is the pack expansion
7292 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7294 If the rewrite produces an invalid type, then P is not at least as
7295 specialized as A. */
7297 /* So coerce P's args to apply to A's parms, and then deduce between A's
7298 args and the converted args. If that succeeds, A is at least as
7299 specialized as P, so they match.*/
7300 tree pargs = template_parms_level_to_args (parm_parms);
7301 ++processing_template_decl;
7302 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7303 /*require_all*/true, /*use_default*/true);
7304 --processing_template_decl;
7305 if (pargs != error_mark_node)
7307 tree targs = make_tree_vec (nargs);
7308 tree aargs = template_parms_level_to_args (arg_parms);
7309 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7310 /*explain*/false))
7311 return 1;
7315 /* Determine whether we have a parameter pack at the end of the
7316 template template parameter's template parameter list. */
7317 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7319 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7321 if (error_operand_p (parm))
7322 return 0;
7324 switch (TREE_CODE (parm))
7326 case TEMPLATE_DECL:
7327 case TYPE_DECL:
7328 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7329 variadic_p = 1;
7330 break;
7332 case PARM_DECL:
7333 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7334 variadic_p = 1;
7335 break;
7337 default:
7338 gcc_unreachable ();
7342 if (nargs != nparms
7343 && !(variadic_p && nargs >= nparms - 1))
7344 return 0;
7346 /* Check all of the template parameters except the parameter pack at
7347 the end (if any). */
7348 for (i = 0; i < nparms - variadic_p; ++i)
7350 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7351 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7352 continue;
7354 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7355 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7357 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7358 outer_args))
7359 return 0;
7363 if (variadic_p)
7365 /* Check each of the template parameters in the template
7366 argument against the template parameter pack at the end of
7367 the template template parameter. */
7368 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7369 return 0;
7371 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7373 for (; i < nargs; ++i)
7375 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7376 continue;
7378 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7380 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7381 outer_args))
7382 return 0;
7386 return 1;
7389 /* Verifies that the deduced template arguments (in TARGS) for the
7390 template template parameters (in TPARMS) represent valid bindings,
7391 by comparing the template parameter list of each template argument
7392 to the template parameter list of its corresponding template
7393 template parameter, in accordance with DR150. This
7394 routine can only be called after all template arguments have been
7395 deduced. It will return TRUE if all of the template template
7396 parameter bindings are okay, FALSE otherwise. */
7397 bool
7398 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7400 int i, ntparms = TREE_VEC_LENGTH (tparms);
7401 bool ret = true;
7403 /* We're dealing with template parms in this process. */
7404 ++processing_template_decl;
7406 targs = INNERMOST_TEMPLATE_ARGS (targs);
7408 for (i = 0; i < ntparms; ++i)
7410 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7411 tree targ = TREE_VEC_ELT (targs, i);
7413 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7415 tree packed_args = NULL_TREE;
7416 int idx, len = 1;
7418 if (ARGUMENT_PACK_P (targ))
7420 /* Look inside the argument pack. */
7421 packed_args = ARGUMENT_PACK_ARGS (targ);
7422 len = TREE_VEC_LENGTH (packed_args);
7425 for (idx = 0; idx < len; ++idx)
7427 tree targ_parms = NULL_TREE;
7429 if (packed_args)
7430 /* Extract the next argument from the argument
7431 pack. */
7432 targ = TREE_VEC_ELT (packed_args, idx);
7434 if (PACK_EXPANSION_P (targ))
7435 /* Look at the pattern of the pack expansion. */
7436 targ = PACK_EXPANSION_PATTERN (targ);
7438 /* Extract the template parameters from the template
7439 argument. */
7440 if (TREE_CODE (targ) == TEMPLATE_DECL)
7441 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7442 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7443 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7445 /* Verify that we can coerce the template template
7446 parameters from the template argument to the template
7447 parameter. This requires an exact match. */
7448 if (targ_parms
7449 && !coerce_template_template_parms
7450 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7451 targ_parms,
7452 tf_none,
7453 tparm,
7454 targs))
7456 ret = false;
7457 goto out;
7463 out:
7465 --processing_template_decl;
7466 return ret;
7469 /* Since type attributes aren't mangled, we need to strip them from
7470 template type arguments. */
7472 static tree
7473 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7475 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7476 return arg;
7477 bool removed_attributes = false;
7478 tree canon = strip_typedefs (arg, &removed_attributes);
7479 if (removed_attributes
7480 && (complain & tf_warning))
7481 warning (OPT_Wignored_attributes,
7482 "ignoring attributes on template argument %qT", arg);
7483 return canon;
7486 /* And from inside dependent non-type arguments like sizeof(Type). */
7488 static tree
7489 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7491 if (!arg || arg == error_mark_node)
7492 return arg;
7493 bool removed_attributes = false;
7494 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7495 if (removed_attributes
7496 && (complain & tf_warning))
7497 warning (OPT_Wignored_attributes,
7498 "ignoring attributes in template argument %qE", arg);
7499 return canon;
7502 // A template declaration can be substituted for a constrained
7503 // template template parameter only when the argument is more
7504 // constrained than the parameter.
7505 static bool
7506 is_compatible_template_arg (tree parm, tree arg)
7508 tree parm_cons = get_constraints (parm);
7510 /* For now, allow constrained template template arguments
7511 and unconstrained template template parameters. */
7512 if (parm_cons == NULL_TREE)
7513 return true;
7515 tree arg_cons = get_constraints (arg);
7517 // If the template parameter is constrained, we need to rewrite its
7518 // constraints in terms of the ARG's template parameters. This ensures
7519 // that all of the template parameter types will have the same depth.
7521 // Note that this is only valid when coerce_template_template_parm is
7522 // true for the innermost template parameters of PARM and ARG. In other
7523 // words, because coercion is successful, this conversion will be valid.
7524 if (parm_cons)
7526 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7527 parm_cons = tsubst_constraint_info (parm_cons,
7528 INNERMOST_TEMPLATE_ARGS (args),
7529 tf_none, NULL_TREE);
7530 if (parm_cons == error_mark_node)
7531 return false;
7534 return subsumes (parm_cons, arg_cons);
7537 // Convert a placeholder argument into a binding to the original
7538 // parameter. The original parameter is saved as the TREE_TYPE of
7539 // ARG.
7540 static inline tree
7541 convert_wildcard_argument (tree parm, tree arg)
7543 TREE_TYPE (arg) = parm;
7544 return arg;
7547 /* Convert the indicated template ARG as necessary to match the
7548 indicated template PARM. Returns the converted ARG, or
7549 error_mark_node if the conversion was unsuccessful. Error and
7550 warning messages are issued under control of COMPLAIN. This
7551 conversion is for the Ith parameter in the parameter list. ARGS is
7552 the full set of template arguments deduced so far. */
7554 static tree
7555 convert_template_argument (tree parm,
7556 tree arg,
7557 tree args,
7558 tsubst_flags_t complain,
7559 int i,
7560 tree in_decl)
7562 tree orig_arg;
7563 tree val;
7564 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7566 if (parm == error_mark_node)
7567 return error_mark_node;
7569 /* Trivially convert placeholders. */
7570 if (TREE_CODE (arg) == WILDCARD_DECL)
7571 return convert_wildcard_argument (parm, arg);
7573 if (arg == any_targ_node)
7574 return arg;
7576 if (TREE_CODE (arg) == TREE_LIST
7577 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7579 /* The template argument was the name of some
7580 member function. That's usually
7581 invalid, but static members are OK. In any
7582 case, grab the underlying fields/functions
7583 and issue an error later if required. */
7584 orig_arg = TREE_VALUE (arg);
7585 TREE_TYPE (arg) = unknown_type_node;
7588 orig_arg = arg;
7590 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7591 requires_type = (TREE_CODE (parm) == TYPE_DECL
7592 || requires_tmpl_type);
7594 /* When determining whether an argument pack expansion is a template,
7595 look at the pattern. */
7596 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7597 arg = PACK_EXPANSION_PATTERN (arg);
7599 /* Deal with an injected-class-name used as a template template arg. */
7600 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7602 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7603 if (TREE_CODE (t) == TEMPLATE_DECL)
7605 if (cxx_dialect >= cxx11)
7606 /* OK under DR 1004. */;
7607 else if (complain & tf_warning_or_error)
7608 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7609 " used as template template argument", TYPE_NAME (arg));
7610 else if (flag_pedantic_errors)
7611 t = arg;
7613 arg = t;
7617 is_tmpl_type =
7618 ((TREE_CODE (arg) == TEMPLATE_DECL
7619 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7620 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7621 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7622 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7624 if (is_tmpl_type
7625 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7626 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7627 arg = TYPE_STUB_DECL (arg);
7629 is_type = TYPE_P (arg) || is_tmpl_type;
7631 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7632 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7634 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7636 if (complain & tf_error)
7637 error ("invalid use of destructor %qE as a type", orig_arg);
7638 return error_mark_node;
7641 permerror (input_location,
7642 "to refer to a type member of a template parameter, "
7643 "use %<typename %E%>", orig_arg);
7645 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7646 TREE_OPERAND (arg, 1),
7647 typename_type,
7648 complain);
7649 arg = orig_arg;
7650 is_type = 1;
7652 if (is_type != requires_type)
7654 if (in_decl)
7656 if (complain & tf_error)
7658 error ("type/value mismatch at argument %d in template "
7659 "parameter list for %qD",
7660 i + 1, in_decl);
7661 if (is_type)
7662 inform (input_location,
7663 " expected a constant of type %qT, got %qT",
7664 TREE_TYPE (parm),
7665 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7666 else if (requires_tmpl_type)
7667 inform (input_location,
7668 " expected a class template, got %qE", orig_arg);
7669 else
7670 inform (input_location,
7671 " expected a type, got %qE", orig_arg);
7674 return error_mark_node;
7676 if (is_tmpl_type ^ requires_tmpl_type)
7678 if (in_decl && (complain & tf_error))
7680 error ("type/value mismatch at argument %d in template "
7681 "parameter list for %qD",
7682 i + 1, in_decl);
7683 if (is_tmpl_type)
7684 inform (input_location,
7685 " expected a type, got %qT", DECL_NAME (arg));
7686 else
7687 inform (input_location,
7688 " expected a class template, got %qT", orig_arg);
7690 return error_mark_node;
7693 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7694 /* We already did the appropriate conversion when packing args. */
7695 val = orig_arg;
7696 else if (is_type)
7698 if (requires_tmpl_type)
7700 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7701 /* The number of argument required is not known yet.
7702 Just accept it for now. */
7703 val = orig_arg;
7704 else
7706 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7707 tree argparm;
7709 /* Strip alias templates that are equivalent to another
7710 template. */
7711 arg = get_underlying_template (arg);
7712 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7714 if (coerce_template_template_parms (parmparm, argparm,
7715 complain, in_decl,
7716 args))
7718 val = arg;
7720 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7721 TEMPLATE_DECL. */
7722 if (val != error_mark_node)
7724 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7725 val = TREE_TYPE (val);
7726 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7727 val = make_pack_expansion (val);
7730 else
7732 if (in_decl && (complain & tf_error))
7734 error ("type/value mismatch at argument %d in "
7735 "template parameter list for %qD",
7736 i + 1, in_decl);
7737 inform (input_location,
7738 " expected a template of type %qD, got %qT",
7739 parm, orig_arg);
7742 val = error_mark_node;
7745 // Check that the constraints are compatible before allowing the
7746 // substitution.
7747 if (val != error_mark_node)
7748 if (!is_compatible_template_arg (parm, arg))
7750 if (in_decl && (complain & tf_error))
7752 error ("constraint mismatch at argument %d in "
7753 "template parameter list for %qD",
7754 i + 1, in_decl);
7755 inform (input_location, " expected %qD but got %qD",
7756 parm, arg);
7758 val = error_mark_node;
7762 else
7763 val = orig_arg;
7764 /* We only form one instance of each template specialization.
7765 Therefore, if we use a non-canonical variant (i.e., a
7766 typedef), any future messages referring to the type will use
7767 the typedef, which is confusing if those future uses do not
7768 themselves also use the typedef. */
7769 if (TYPE_P (val))
7770 val = canonicalize_type_argument (val, complain);
7772 else
7774 tree t = TREE_TYPE (parm);
7776 if (tree a = type_uses_auto (t))
7778 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7779 if (t == error_mark_node)
7780 return error_mark_node;
7782 else
7783 t = tsubst (t, args, complain, in_decl);
7785 if (invalid_nontype_parm_type_p (t, complain))
7786 return error_mark_node;
7788 if (!type_dependent_expression_p (orig_arg)
7789 && !uses_template_parms (t))
7790 /* We used to call digest_init here. However, digest_init
7791 will report errors, which we don't want when complain
7792 is zero. More importantly, digest_init will try too
7793 hard to convert things: for example, `0' should not be
7794 converted to pointer type at this point according to
7795 the standard. Accepting this is not merely an
7796 extension, since deciding whether or not these
7797 conversions can occur is part of determining which
7798 function template to call, or whether a given explicit
7799 argument specification is valid. */
7800 val = convert_nontype_argument (t, orig_arg, complain);
7801 else
7802 val = canonicalize_expr_argument (orig_arg, complain);
7804 if (val == NULL_TREE)
7805 val = error_mark_node;
7806 else if (val == error_mark_node && (complain & tf_error))
7807 error ("could not convert template argument %qE from %qT to %qT",
7808 orig_arg, TREE_TYPE (orig_arg), t);
7810 if (INDIRECT_REF_P (val))
7812 /* Reject template arguments that are references to built-in
7813 functions with no library fallbacks. */
7814 const_tree inner = TREE_OPERAND (val, 0);
7815 const_tree innertype = TREE_TYPE (inner);
7816 if (innertype
7817 && TREE_CODE (innertype) == REFERENCE_TYPE
7818 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7819 && 0 < TREE_OPERAND_LENGTH (inner)
7820 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7821 return error_mark_node;
7824 if (TREE_CODE (val) == SCOPE_REF)
7826 /* Strip typedefs from the SCOPE_REF. */
7827 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7828 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7829 complain);
7830 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7831 QUALIFIED_NAME_IS_TEMPLATE (val));
7835 return val;
7838 /* Coerces the remaining template arguments in INNER_ARGS (from
7839 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7840 Returns the coerced argument pack. PARM_IDX is the position of this
7841 parameter in the template parameter list. ARGS is the original
7842 template argument list. */
7843 static tree
7844 coerce_template_parameter_pack (tree parms,
7845 int parm_idx,
7846 tree args,
7847 tree inner_args,
7848 int arg_idx,
7849 tree new_args,
7850 int* lost,
7851 tree in_decl,
7852 tsubst_flags_t complain)
7854 tree parm = TREE_VEC_ELT (parms, parm_idx);
7855 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7856 tree packed_args;
7857 tree argument_pack;
7858 tree packed_parms = NULL_TREE;
7860 if (arg_idx > nargs)
7861 arg_idx = nargs;
7863 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7865 /* When the template parameter is a non-type template parameter pack
7866 or template template parameter pack whose type or template
7867 parameters use parameter packs, we know exactly how many arguments
7868 we are looking for. Build a vector of the instantiated decls for
7869 these template parameters in PACKED_PARMS. */
7870 /* We can't use make_pack_expansion here because it would interpret a
7871 _DECL as a use rather than a declaration. */
7872 tree decl = TREE_VALUE (parm);
7873 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7874 SET_PACK_EXPANSION_PATTERN (exp, decl);
7875 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7876 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7878 TREE_VEC_LENGTH (args)--;
7879 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7880 TREE_VEC_LENGTH (args)++;
7882 if (packed_parms == error_mark_node)
7883 return error_mark_node;
7885 /* If we're doing a partial instantiation of a member template,
7886 verify that all of the types used for the non-type
7887 template parameter pack are, in fact, valid for non-type
7888 template parameters. */
7889 if (arg_idx < nargs
7890 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7892 int j, len = TREE_VEC_LENGTH (packed_parms);
7893 for (j = 0; j < len; ++j)
7895 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7896 if (invalid_nontype_parm_type_p (t, complain))
7897 return error_mark_node;
7899 /* We don't know how many args we have yet, just
7900 use the unconverted ones for now. */
7901 return NULL_TREE;
7904 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7906 /* Check if we have a placeholder pack, which indicates we're
7907 in the context of a introduction list. In that case we want
7908 to match this pack to the single placeholder. */
7909 else if (arg_idx < nargs
7910 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7911 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7913 nargs = arg_idx + 1;
7914 packed_args = make_tree_vec (1);
7916 else
7917 packed_args = make_tree_vec (nargs - arg_idx);
7919 /* Convert the remaining arguments, which will be a part of the
7920 parameter pack "parm". */
7921 int first_pack_arg = arg_idx;
7922 for (; arg_idx < nargs; ++arg_idx)
7924 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7925 tree actual_parm = TREE_VALUE (parm);
7926 int pack_idx = arg_idx - first_pack_arg;
7928 if (packed_parms)
7930 /* Once we've packed as many args as we have types, stop. */
7931 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7932 break;
7933 else if (PACK_EXPANSION_P (arg))
7934 /* We don't know how many args we have yet, just
7935 use the unconverted ones for now. */
7936 return NULL_TREE;
7937 else
7938 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7941 if (arg == error_mark_node)
7943 if (complain & tf_error)
7944 error ("template argument %d is invalid", arg_idx + 1);
7946 else
7947 arg = convert_template_argument (actual_parm,
7948 arg, new_args, complain, parm_idx,
7949 in_decl);
7950 if (arg == error_mark_node)
7951 (*lost)++;
7952 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7955 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7956 && TREE_VEC_LENGTH (packed_args) > 0)
7958 if (complain & tf_error)
7959 error ("wrong number of template arguments (%d, should be %d)",
7960 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7961 return error_mark_node;
7964 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7965 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7966 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7967 else
7969 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7970 TREE_CONSTANT (argument_pack) = 1;
7973 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7974 if (CHECKING_P)
7975 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7976 TREE_VEC_LENGTH (packed_args));
7977 return argument_pack;
7980 /* Returns the number of pack expansions in the template argument vector
7981 ARGS. */
7983 static int
7984 pack_expansion_args_count (tree args)
7986 int i;
7987 int count = 0;
7988 if (args)
7989 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7991 tree elt = TREE_VEC_ELT (args, i);
7992 if (elt && PACK_EXPANSION_P (elt))
7993 ++count;
7995 return count;
7998 /* Convert all template arguments to their appropriate types, and
7999 return a vector containing the innermost resulting template
8000 arguments. If any error occurs, return error_mark_node. Error and
8001 warning messages are issued under control of COMPLAIN.
8003 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8004 for arguments not specified in ARGS. Otherwise, if
8005 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8006 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8007 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8008 ARGS. */
8010 static tree
8011 coerce_template_parms (tree parms,
8012 tree args,
8013 tree in_decl,
8014 tsubst_flags_t complain,
8015 bool require_all_args,
8016 bool use_default_args)
8018 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8019 tree orig_inner_args;
8020 tree inner_args;
8021 tree new_args;
8022 tree new_inner_args;
8023 int saved_unevaluated_operand;
8024 int saved_inhibit_evaluation_warnings;
8026 /* When used as a boolean value, indicates whether this is a
8027 variadic template parameter list. Since it's an int, we can also
8028 subtract it from nparms to get the number of non-variadic
8029 parameters. */
8030 int variadic_p = 0;
8031 int variadic_args_p = 0;
8032 int post_variadic_parms = 0;
8034 /* Likewise for parameters with default arguments. */
8035 int default_p = 0;
8037 if (args == error_mark_node)
8038 return error_mark_node;
8040 nparms = TREE_VEC_LENGTH (parms);
8042 /* Determine if there are any parameter packs or default arguments. */
8043 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8045 tree parm = TREE_VEC_ELT (parms, parm_idx);
8046 if (variadic_p)
8047 ++post_variadic_parms;
8048 if (template_parameter_pack_p (TREE_VALUE (parm)))
8049 ++variadic_p;
8050 if (TREE_PURPOSE (parm))
8051 ++default_p;
8054 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8055 /* If there are no parameters that follow a parameter pack, we need to
8056 expand any argument packs so that we can deduce a parameter pack from
8057 some non-packed args followed by an argument pack, as in variadic85.C.
8058 If there are such parameters, we need to leave argument packs intact
8059 so the arguments are assigned properly. This can happen when dealing
8060 with a nested class inside a partial specialization of a class
8061 template, as in variadic92.C, or when deducing a template parameter pack
8062 from a sub-declarator, as in variadic114.C. */
8063 if (!post_variadic_parms)
8064 inner_args = expand_template_argument_pack (inner_args);
8066 /* Count any pack expansion args. */
8067 variadic_args_p = pack_expansion_args_count (inner_args);
8069 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8070 if ((nargs - variadic_args_p > nparms && !variadic_p)
8071 || (nargs < nparms - variadic_p
8072 && require_all_args
8073 && !variadic_args_p
8074 && (!use_default_args
8075 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8076 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8078 if (complain & tf_error)
8080 if (variadic_p || default_p)
8082 nparms -= variadic_p + default_p;
8083 error ("wrong number of template arguments "
8084 "(%d, should be at least %d)", nargs, nparms);
8086 else
8087 error ("wrong number of template arguments "
8088 "(%d, should be %d)", nargs, nparms);
8090 if (in_decl)
8091 inform (DECL_SOURCE_LOCATION (in_decl),
8092 "provided for %qD", in_decl);
8095 return error_mark_node;
8097 /* We can't pass a pack expansion to a non-pack parameter of an alias
8098 template (DR 1430). */
8099 else if (in_decl
8100 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8101 || concept_template_p (in_decl))
8102 && variadic_args_p
8103 && nargs - variadic_args_p < nparms - variadic_p)
8105 if (complain & tf_error)
8107 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8109 tree arg = TREE_VEC_ELT (inner_args, i);
8110 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8112 if (PACK_EXPANSION_P (arg)
8113 && !template_parameter_pack_p (parm))
8115 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8116 error_at (location_of (arg),
8117 "pack expansion argument for non-pack parameter "
8118 "%qD of alias template %qD", parm, in_decl);
8119 else
8120 error_at (location_of (arg),
8121 "pack expansion argument for non-pack parameter "
8122 "%qD of concept %qD", parm, in_decl);
8123 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8124 goto found;
8127 gcc_unreachable ();
8128 found:;
8130 return error_mark_node;
8133 /* We need to evaluate the template arguments, even though this
8134 template-id may be nested within a "sizeof". */
8135 saved_unevaluated_operand = cp_unevaluated_operand;
8136 cp_unevaluated_operand = 0;
8137 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8138 c_inhibit_evaluation_warnings = 0;
8139 new_inner_args = make_tree_vec (nparms);
8140 new_args = add_outermost_template_args (args, new_inner_args);
8141 int pack_adjust = 0;
8142 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8144 tree arg;
8145 tree parm;
8147 /* Get the Ith template parameter. */
8148 parm = TREE_VEC_ELT (parms, parm_idx);
8150 if (parm == error_mark_node)
8152 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8153 continue;
8156 /* Calculate the next argument. */
8157 if (arg_idx < nargs)
8158 arg = TREE_VEC_ELT (inner_args, arg_idx);
8159 else
8160 arg = NULL_TREE;
8162 if (template_parameter_pack_p (TREE_VALUE (parm))
8163 && !(arg && ARGUMENT_PACK_P (arg)))
8165 /* Some arguments will be placed in the
8166 template parameter pack PARM. */
8167 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8168 inner_args, arg_idx,
8169 new_args, &lost,
8170 in_decl, complain);
8172 if (arg == NULL_TREE)
8174 /* We don't know how many args we have yet, just use the
8175 unconverted (and still packed) ones for now. */
8176 new_inner_args = orig_inner_args;
8177 arg_idx = nargs;
8178 break;
8181 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8183 /* Store this argument. */
8184 if (arg == error_mark_node)
8186 lost++;
8187 /* We are done with all of the arguments. */
8188 arg_idx = nargs;
8190 else
8192 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8193 arg_idx += pack_adjust;
8196 continue;
8198 else if (arg)
8200 if (PACK_EXPANSION_P (arg))
8202 /* "If every valid specialization of a variadic template
8203 requires an empty template parameter pack, the template is
8204 ill-formed, no diagnostic required." So check that the
8205 pattern works with this parameter. */
8206 tree pattern = PACK_EXPANSION_PATTERN (arg);
8207 tree conv = convert_template_argument (TREE_VALUE (parm),
8208 pattern, new_args,
8209 complain, parm_idx,
8210 in_decl);
8211 if (conv == error_mark_node)
8213 if (complain & tf_error)
8214 inform (input_location, "so any instantiation with a "
8215 "non-empty parameter pack would be ill-formed");
8216 ++lost;
8218 else if (TYPE_P (conv) && !TYPE_P (pattern))
8219 /* Recover from missing typename. */
8220 TREE_VEC_ELT (inner_args, arg_idx)
8221 = make_pack_expansion (conv);
8223 /* We don't know how many args we have yet, just
8224 use the unconverted ones for now. */
8225 new_inner_args = inner_args;
8226 arg_idx = nargs;
8227 break;
8230 else if (require_all_args)
8232 /* There must be a default arg in this case. */
8233 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8234 complain, in_decl);
8235 /* The position of the first default template argument,
8236 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8237 Record that. */
8238 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8239 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8240 arg_idx - pack_adjust);
8242 else
8243 break;
8245 if (arg == error_mark_node)
8247 if (complain & tf_error)
8248 error ("template argument %d is invalid", arg_idx + 1);
8250 else if (!arg)
8251 /* This only occurs if there was an error in the template
8252 parameter list itself (which we would already have
8253 reported) that we are trying to recover from, e.g., a class
8254 template with a parameter list such as
8255 template<typename..., typename>. */
8256 ++lost;
8257 else
8258 arg = convert_template_argument (TREE_VALUE (parm),
8259 arg, new_args, complain,
8260 parm_idx, in_decl);
8262 if (arg == error_mark_node)
8263 lost++;
8264 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8266 cp_unevaluated_operand = saved_unevaluated_operand;
8267 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8269 if (variadic_p && arg_idx < nargs)
8271 if (complain & tf_error)
8273 error ("wrong number of template arguments "
8274 "(%d, should be %d)", nargs, arg_idx);
8275 if (in_decl)
8276 error ("provided for %q+D", in_decl);
8278 return error_mark_node;
8281 if (lost)
8282 return error_mark_node;
8284 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8285 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8286 TREE_VEC_LENGTH (new_inner_args));
8288 return new_inner_args;
8291 /* Convert all template arguments to their appropriate types, and
8292 return a vector containing the innermost resulting template
8293 arguments. If any error occurs, return error_mark_node. Error and
8294 warning messages are not issued.
8296 Note that no function argument deduction is performed, and default
8297 arguments are used to fill in unspecified arguments. */
8298 tree
8299 coerce_template_parms (tree parms, tree args, tree in_decl)
8301 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8304 /* Convert all template arguments to their appropriate type, and
8305 instantiate default arguments as needed. This returns a vector
8306 containing the innermost resulting template arguments, or
8307 error_mark_node if unsuccessful. */
8308 tree
8309 coerce_template_parms (tree parms, tree args, tree in_decl,
8310 tsubst_flags_t complain)
8312 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8315 /* Like coerce_template_parms. If PARMS represents all template
8316 parameters levels, this function returns a vector of vectors
8317 representing all the resulting argument levels. Note that in this
8318 case, only the innermost arguments are coerced because the
8319 outermost ones are supposed to have been coerced already.
8321 Otherwise, if PARMS represents only (the innermost) vector of
8322 parameters, this function returns a vector containing just the
8323 innermost resulting arguments. */
8325 static tree
8326 coerce_innermost_template_parms (tree parms,
8327 tree args,
8328 tree in_decl,
8329 tsubst_flags_t complain,
8330 bool require_all_args,
8331 bool use_default_args)
8333 int parms_depth = TMPL_PARMS_DEPTH (parms);
8334 int args_depth = TMPL_ARGS_DEPTH (args);
8335 tree coerced_args;
8337 if (parms_depth > 1)
8339 coerced_args = make_tree_vec (parms_depth);
8340 tree level;
8341 int cur_depth;
8343 for (level = parms, cur_depth = parms_depth;
8344 parms_depth > 0 && level != NULL_TREE;
8345 level = TREE_CHAIN (level), --cur_depth)
8347 tree l;
8348 if (cur_depth == args_depth)
8349 l = coerce_template_parms (TREE_VALUE (level),
8350 args, in_decl, complain,
8351 require_all_args,
8352 use_default_args);
8353 else
8354 l = TMPL_ARGS_LEVEL (args, cur_depth);
8356 if (l == error_mark_node)
8357 return error_mark_node;
8359 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8362 else
8363 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8364 args, in_decl, complain,
8365 require_all_args,
8366 use_default_args);
8367 return coerced_args;
8370 /* Returns 1 if template args OT and NT are equivalent. */
8373 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8375 if (nt == ot)
8376 return 1;
8377 if (nt == NULL_TREE || ot == NULL_TREE)
8378 return false;
8379 if (nt == any_targ_node || ot == any_targ_node)
8380 return true;
8382 if (TREE_CODE (nt) == TREE_VEC)
8383 /* For member templates */
8384 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8385 else if (PACK_EXPANSION_P (ot))
8386 return (PACK_EXPANSION_P (nt)
8387 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8388 PACK_EXPANSION_PATTERN (nt))
8389 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8390 PACK_EXPANSION_EXTRA_ARGS (nt)));
8391 else if (ARGUMENT_PACK_P (ot))
8393 int i, len;
8394 tree opack, npack;
8396 if (!ARGUMENT_PACK_P (nt))
8397 return 0;
8399 opack = ARGUMENT_PACK_ARGS (ot);
8400 npack = ARGUMENT_PACK_ARGS (nt);
8401 len = TREE_VEC_LENGTH (opack);
8402 if (TREE_VEC_LENGTH (npack) != len)
8403 return 0;
8404 for (i = 0; i < len; ++i)
8405 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8406 TREE_VEC_ELT (npack, i)))
8407 return 0;
8408 return 1;
8410 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8411 gcc_unreachable ();
8412 else if (TYPE_P (nt))
8414 if (!TYPE_P (ot))
8415 return false;
8416 /* Don't treat an alias template specialization with dependent
8417 arguments as equivalent to its underlying type when used as a
8418 template argument; we need them to be distinct so that we
8419 substitute into the specialization arguments at instantiation
8420 time. And aliases can't be equivalent without being ==, so
8421 we don't need to look any deeper.
8423 During partial ordering, however, we need to treat them normally so
8424 that we can order uses of the same alias with different
8425 cv-qualification (79960). */
8426 if (!partial_order
8427 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8428 return false;
8429 else
8430 return same_type_p (ot, nt);
8432 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8433 return 0;
8434 else
8436 /* Try to treat a template non-type argument that has been converted
8437 to the parameter type as equivalent to one that hasn't yet. */
8438 for (enum tree_code code1 = TREE_CODE (ot);
8439 CONVERT_EXPR_CODE_P (code1)
8440 || code1 == NON_LVALUE_EXPR;
8441 code1 = TREE_CODE (ot))
8442 ot = TREE_OPERAND (ot, 0);
8443 for (enum tree_code code2 = TREE_CODE (nt);
8444 CONVERT_EXPR_CODE_P (code2)
8445 || code2 == NON_LVALUE_EXPR;
8446 code2 = TREE_CODE (nt))
8447 nt = TREE_OPERAND (nt, 0);
8449 return cp_tree_equal (ot, nt);
8453 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8454 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8455 NEWARG_PTR with the offending arguments if they are non-NULL. */
8458 comp_template_args (tree oldargs, tree newargs,
8459 tree *oldarg_ptr, tree *newarg_ptr,
8460 bool partial_order)
8462 int i;
8464 if (oldargs == newargs)
8465 return 1;
8467 if (!oldargs || !newargs)
8468 return 0;
8470 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8471 return 0;
8473 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8475 tree nt = TREE_VEC_ELT (newargs, i);
8476 tree ot = TREE_VEC_ELT (oldargs, i);
8478 if (! template_args_equal (ot, nt, partial_order))
8480 if (oldarg_ptr != NULL)
8481 *oldarg_ptr = ot;
8482 if (newarg_ptr != NULL)
8483 *newarg_ptr = nt;
8484 return 0;
8487 return 1;
8490 inline bool
8491 comp_template_args_porder (tree oargs, tree nargs)
8493 return comp_template_args (oargs, nargs, NULL, NULL, true);
8496 static void
8497 add_pending_template (tree d)
8499 tree ti = (TYPE_P (d)
8500 ? CLASSTYPE_TEMPLATE_INFO (d)
8501 : DECL_TEMPLATE_INFO (d));
8502 struct pending_template *pt;
8503 int level;
8505 if (TI_PENDING_TEMPLATE_FLAG (ti))
8506 return;
8508 /* We are called both from instantiate_decl, where we've already had a
8509 tinst_level pushed, and instantiate_template, where we haven't.
8510 Compensate. */
8511 level = !current_tinst_level || current_tinst_level->decl != d;
8513 if (level)
8514 push_tinst_level (d);
8516 pt = ggc_alloc<pending_template> ();
8517 pt->next = NULL;
8518 pt->tinst = current_tinst_level;
8519 if (last_pending_template)
8520 last_pending_template->next = pt;
8521 else
8522 pending_templates = pt;
8524 last_pending_template = pt;
8526 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8528 if (level)
8529 pop_tinst_level ();
8533 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8534 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8535 documentation for TEMPLATE_ID_EXPR. */
8537 tree
8538 lookup_template_function (tree fns, tree arglist)
8540 tree type;
8542 if (fns == error_mark_node || arglist == error_mark_node)
8543 return error_mark_node;
8545 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8547 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8549 error ("%q#D is not a function template", fns);
8550 return error_mark_node;
8553 if (BASELINK_P (fns))
8555 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8556 unknown_type_node,
8557 BASELINK_FUNCTIONS (fns),
8558 arglist);
8559 return fns;
8562 type = TREE_TYPE (fns);
8563 if (TREE_CODE (fns) == OVERLOAD || !type)
8564 type = unknown_type_node;
8566 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8569 /* Within the scope of a template class S<T>, the name S gets bound
8570 (in build_self_reference) to a TYPE_DECL for the class, not a
8571 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8572 or one of its enclosing classes, and that type is a template,
8573 return the associated TEMPLATE_DECL. Otherwise, the original
8574 DECL is returned.
8576 Also handle the case when DECL is a TREE_LIST of ambiguous
8577 injected-class-names from different bases. */
8579 tree
8580 maybe_get_template_decl_from_type_decl (tree decl)
8582 if (decl == NULL_TREE)
8583 return decl;
8585 /* DR 176: A lookup that finds an injected-class-name (10.2
8586 [class.member.lookup]) can result in an ambiguity in certain cases
8587 (for example, if it is found in more than one base class). If all of
8588 the injected-class-names that are found refer to specializations of
8589 the same class template, and if the name is followed by a
8590 template-argument-list, the reference refers to the class template
8591 itself and not a specialization thereof, and is not ambiguous. */
8592 if (TREE_CODE (decl) == TREE_LIST)
8594 tree t, tmpl = NULL_TREE;
8595 for (t = decl; t; t = TREE_CHAIN (t))
8597 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8598 if (!tmpl)
8599 tmpl = elt;
8600 else if (tmpl != elt)
8601 break;
8603 if (tmpl && t == NULL_TREE)
8604 return tmpl;
8605 else
8606 return decl;
8609 return (decl != NULL_TREE
8610 && DECL_SELF_REFERENCE_P (decl)
8611 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8612 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8615 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8616 parameters, find the desired type.
8618 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8620 IN_DECL, if non-NULL, is the template declaration we are trying to
8621 instantiate.
8623 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8624 the class we are looking up.
8626 Issue error and warning messages under control of COMPLAIN.
8628 If the template class is really a local class in a template
8629 function, then the FUNCTION_CONTEXT is the function in which it is
8630 being instantiated.
8632 ??? Note that this function is currently called *twice* for each
8633 template-id: the first time from the parser, while creating the
8634 incomplete type (finish_template_type), and the second type during the
8635 real instantiation (instantiate_template_class). This is surely something
8636 that we want to avoid. It also causes some problems with argument
8637 coercion (see convert_nontype_argument for more information on this). */
8639 static tree
8640 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8641 int entering_scope, tsubst_flags_t complain)
8643 tree templ = NULL_TREE, parmlist;
8644 tree t;
8645 spec_entry **slot;
8646 spec_entry *entry;
8647 spec_entry elt;
8648 hashval_t hash;
8650 if (identifier_p (d1))
8652 tree value = innermost_non_namespace_value (d1);
8653 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8654 templ = value;
8655 else
8657 if (context)
8658 push_decl_namespace (context);
8659 templ = lookup_name (d1);
8660 templ = maybe_get_template_decl_from_type_decl (templ);
8661 if (context)
8662 pop_decl_namespace ();
8664 if (templ)
8665 context = DECL_CONTEXT (templ);
8667 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8669 tree type = TREE_TYPE (d1);
8671 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8672 an implicit typename for the second A. Deal with it. */
8673 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8674 type = TREE_TYPE (type);
8676 if (CLASSTYPE_TEMPLATE_INFO (type))
8678 templ = CLASSTYPE_TI_TEMPLATE (type);
8679 d1 = DECL_NAME (templ);
8682 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8683 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8685 templ = TYPE_TI_TEMPLATE (d1);
8686 d1 = DECL_NAME (templ);
8688 else if (DECL_TYPE_TEMPLATE_P (d1))
8690 templ = d1;
8691 d1 = DECL_NAME (templ);
8692 context = DECL_CONTEXT (templ);
8694 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8696 templ = d1;
8697 d1 = DECL_NAME (templ);
8700 /* Issue an error message if we didn't find a template. */
8701 if (! templ)
8703 if (complain & tf_error)
8704 error ("%qT is not a template", d1);
8705 return error_mark_node;
8708 if (TREE_CODE (templ) != TEMPLATE_DECL
8709 /* Make sure it's a user visible template, if it was named by
8710 the user. */
8711 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8712 && !PRIMARY_TEMPLATE_P (templ)))
8714 if (complain & tf_error)
8716 error ("non-template type %qT used as a template", d1);
8717 if (in_decl)
8718 error ("for template declaration %q+D", in_decl);
8720 return error_mark_node;
8723 complain &= ~tf_user;
8725 /* An alias that just changes the name of a template is equivalent to the
8726 other template, so if any of the arguments are pack expansions, strip
8727 the alias to avoid problems with a pack expansion passed to a non-pack
8728 alias template parameter (DR 1430). */
8729 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8730 templ = get_underlying_template (templ);
8732 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8734 tree parm;
8735 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8736 if (arglist2 == error_mark_node
8737 || (!uses_template_parms (arglist2)
8738 && check_instantiated_args (templ, arglist2, complain)))
8739 return error_mark_node;
8741 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8742 return parm;
8744 else
8746 tree template_type = TREE_TYPE (templ);
8747 tree gen_tmpl;
8748 tree type_decl;
8749 tree found = NULL_TREE;
8750 int arg_depth;
8751 int parm_depth;
8752 int is_dependent_type;
8753 int use_partial_inst_tmpl = false;
8755 if (template_type == error_mark_node)
8756 /* An error occurred while building the template TEMPL, and a
8757 diagnostic has most certainly been emitted for that
8758 already. Let's propagate that error. */
8759 return error_mark_node;
8761 gen_tmpl = most_general_template (templ);
8762 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8763 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8764 arg_depth = TMPL_ARGS_DEPTH (arglist);
8766 if (arg_depth == 1 && parm_depth > 1)
8768 /* We've been given an incomplete set of template arguments.
8769 For example, given:
8771 template <class T> struct S1 {
8772 template <class U> struct S2 {};
8773 template <class U> struct S2<U*> {};
8776 we will be called with an ARGLIST of `U*', but the
8777 TEMPLATE will be `template <class T> template
8778 <class U> struct S1<T>::S2'. We must fill in the missing
8779 arguments. */
8780 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8781 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8782 arg_depth = TMPL_ARGS_DEPTH (arglist);
8785 /* Now we should have enough arguments. */
8786 gcc_assert (parm_depth == arg_depth);
8788 /* From here on, we're only interested in the most general
8789 template. */
8791 /* Calculate the BOUND_ARGS. These will be the args that are
8792 actually tsubst'd into the definition to create the
8793 instantiation. */
8794 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8795 complain,
8796 /*require_all_args=*/true,
8797 /*use_default_args=*/true);
8799 if (arglist == error_mark_node)
8800 /* We were unable to bind the arguments. */
8801 return error_mark_node;
8803 /* In the scope of a template class, explicit references to the
8804 template class refer to the type of the template, not any
8805 instantiation of it. For example, in:
8807 template <class T> class C { void f(C<T>); }
8809 the `C<T>' is just the same as `C'. Outside of the
8810 class, however, such a reference is an instantiation. */
8811 if (entering_scope
8812 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8813 || currently_open_class (template_type))
8815 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8817 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8818 return template_type;
8821 /* If we already have this specialization, return it. */
8822 elt.tmpl = gen_tmpl;
8823 elt.args = arglist;
8824 elt.spec = NULL_TREE;
8825 hash = spec_hasher::hash (&elt);
8826 entry = type_specializations->find_with_hash (&elt, hash);
8828 if (entry)
8829 return entry->spec;
8831 /* If the the template's constraints are not satisfied,
8832 then we cannot form a valid type.
8834 Note that the check is deferred until after the hash
8835 lookup. This prevents redundant checks on previously
8836 instantiated specializations. */
8837 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8839 if (complain & tf_error)
8841 error ("template constraint failure");
8842 diagnose_constraints (input_location, gen_tmpl, arglist);
8844 return error_mark_node;
8847 is_dependent_type = uses_template_parms (arglist);
8849 /* If the deduced arguments are invalid, then the binding
8850 failed. */
8851 if (!is_dependent_type
8852 && check_instantiated_args (gen_tmpl,
8853 INNERMOST_TEMPLATE_ARGS (arglist),
8854 complain))
8855 return error_mark_node;
8857 if (!is_dependent_type
8858 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8859 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8860 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8862 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8863 DECL_NAME (gen_tmpl),
8864 /*tag_scope=*/ts_global);
8865 return found;
8868 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8869 complain, in_decl);
8870 if (context == error_mark_node)
8871 return error_mark_node;
8873 if (!context)
8874 context = global_namespace;
8876 /* Create the type. */
8877 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8879 /* The user referred to a specialization of an alias
8880 template represented by GEN_TMPL.
8882 [temp.alias]/2 says:
8884 When a template-id refers to the specialization of an
8885 alias template, it is equivalent to the associated
8886 type obtained by substitution of its
8887 template-arguments for the template-parameters in the
8888 type-id of the alias template. */
8890 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8891 /* Note that the call above (by indirectly calling
8892 register_specialization in tsubst_decl) registers the
8893 TYPE_DECL representing the specialization of the alias
8894 template. So next time someone substitutes ARGLIST for
8895 the template parms into the alias template (GEN_TMPL),
8896 she'll get that TYPE_DECL back. */
8898 if (t == error_mark_node)
8899 return t;
8901 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8903 if (!is_dependent_type)
8905 set_current_access_from_decl (TYPE_NAME (template_type));
8906 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8907 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8908 arglist, complain, in_decl),
8909 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8910 arglist, complain, in_decl),
8911 SCOPED_ENUM_P (template_type), NULL);
8913 if (t == error_mark_node)
8914 return t;
8916 else
8918 /* We don't want to call start_enum for this type, since
8919 the values for the enumeration constants may involve
8920 template parameters. And, no one should be interested
8921 in the enumeration constants for such a type. */
8922 t = cxx_make_type (ENUMERAL_TYPE);
8923 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8925 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8926 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8927 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8929 else if (CLASS_TYPE_P (template_type))
8931 t = make_class_type (TREE_CODE (template_type));
8932 CLASSTYPE_DECLARED_CLASS (t)
8933 = CLASSTYPE_DECLARED_CLASS (template_type);
8934 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8936 /* A local class. Make sure the decl gets registered properly. */
8937 if (context == current_function_decl)
8938 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8940 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8941 /* This instantiation is another name for the primary
8942 template type. Set the TYPE_CANONICAL field
8943 appropriately. */
8944 TYPE_CANONICAL (t) = template_type;
8945 else if (any_template_arguments_need_structural_equality_p (arglist))
8946 /* Some of the template arguments require structural
8947 equality testing, so this template class requires
8948 structural equality testing. */
8949 SET_TYPE_STRUCTURAL_EQUALITY (t);
8951 else
8952 gcc_unreachable ();
8954 /* If we called start_enum or pushtag above, this information
8955 will already be set up. */
8956 if (!TYPE_NAME (t))
8958 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8960 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8961 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8962 DECL_SOURCE_LOCATION (type_decl)
8963 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8965 else
8966 type_decl = TYPE_NAME (t);
8968 if (CLASS_TYPE_P (template_type))
8970 TREE_PRIVATE (type_decl)
8971 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8972 TREE_PROTECTED (type_decl)
8973 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8974 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8976 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8977 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8981 if (OVERLOAD_TYPE_P (t)
8982 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8984 static const char *tags[] = {"abi_tag", "may_alias"};
8986 for (unsigned ix = 0; ix != 2; ix++)
8988 tree attributes
8989 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8991 if (attributes)
8992 TYPE_ATTRIBUTES (t)
8993 = tree_cons (TREE_PURPOSE (attributes),
8994 TREE_VALUE (attributes),
8995 TYPE_ATTRIBUTES (t));
8999 /* Let's consider the explicit specialization of a member
9000 of a class template specialization that is implicitly instantiated,
9001 e.g.:
9002 template<class T>
9003 struct S
9005 template<class U> struct M {}; //#0
9008 template<>
9009 template<>
9010 struct S<int>::M<char> //#1
9012 int i;
9014 [temp.expl.spec]/4 says this is valid.
9016 In this case, when we write:
9017 S<int>::M<char> m;
9019 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9020 the one of #0.
9022 When we encounter #1, we want to store the partial instantiation
9023 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9025 For all cases other than this "explicit specialization of member of a
9026 class template", we just want to store the most general template into
9027 the CLASSTYPE_TI_TEMPLATE of M.
9029 This case of "explicit specialization of member of a class template"
9030 only happens when:
9031 1/ the enclosing class is an instantiation of, and therefore not
9032 the same as, the context of the most general template, and
9033 2/ we aren't looking at the partial instantiation itself, i.e.
9034 the innermost arguments are not the same as the innermost parms of
9035 the most general template.
9037 So it's only when 1/ and 2/ happens that we want to use the partial
9038 instantiation of the member template in lieu of its most general
9039 template. */
9041 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9042 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9043 /* the enclosing class must be an instantiation... */
9044 && CLASS_TYPE_P (context)
9045 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9047 TREE_VEC_LENGTH (arglist)--;
9048 ++processing_template_decl;
9049 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9050 tree partial_inst_args =
9051 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9052 arglist, complain, NULL_TREE);
9053 --processing_template_decl;
9054 TREE_VEC_LENGTH (arglist)++;
9055 if (partial_inst_args == error_mark_node)
9056 return error_mark_node;
9057 use_partial_inst_tmpl =
9058 /*...and we must not be looking at the partial instantiation
9059 itself. */
9060 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9061 partial_inst_args);
9064 if (!use_partial_inst_tmpl)
9065 /* This case is easy; there are no member templates involved. */
9066 found = gen_tmpl;
9067 else
9069 /* This is a full instantiation of a member template. Find
9070 the partial instantiation of which this is an instance. */
9072 /* Temporarily reduce by one the number of levels in the ARGLIST
9073 so as to avoid comparing the last set of arguments. */
9074 TREE_VEC_LENGTH (arglist)--;
9075 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9076 TREE_VEC_LENGTH (arglist)++;
9077 /* FOUND is either a proper class type, or an alias
9078 template specialization. In the later case, it's a
9079 TYPE_DECL, resulting from the substituting of arguments
9080 for parameters in the TYPE_DECL of the alias template
9081 done earlier. So be careful while getting the template
9082 of FOUND. */
9083 found = (TREE_CODE (found) == TEMPLATE_DECL
9084 ? found
9085 : (TREE_CODE (found) == TYPE_DECL
9086 ? DECL_TI_TEMPLATE (found)
9087 : CLASSTYPE_TI_TEMPLATE (found)));
9090 // Build template info for the new specialization.
9091 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9093 elt.spec = t;
9094 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9095 entry = ggc_alloc<spec_entry> ();
9096 *entry = elt;
9097 *slot = entry;
9099 /* Note this use of the partial instantiation so we can check it
9100 later in maybe_process_partial_specialization. */
9101 DECL_TEMPLATE_INSTANTIATIONS (found)
9102 = tree_cons (arglist, t,
9103 DECL_TEMPLATE_INSTANTIATIONS (found));
9105 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9106 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9107 /* Now that the type has been registered on the instantiations
9108 list, we set up the enumerators. Because the enumeration
9109 constants may involve the enumeration type itself, we make
9110 sure to register the type first, and then create the
9111 constants. That way, doing tsubst_expr for the enumeration
9112 constants won't result in recursive calls here; we'll find
9113 the instantiation and exit above. */
9114 tsubst_enum (template_type, t, arglist);
9116 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9117 /* If the type makes use of template parameters, the
9118 code that generates debugging information will crash. */
9119 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9121 /* Possibly limit visibility based on template args. */
9122 TREE_PUBLIC (type_decl) = 1;
9123 determine_visibility (type_decl);
9125 inherit_targ_abi_tags (t);
9127 return t;
9131 /* Wrapper for lookup_template_class_1. */
9133 tree
9134 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9135 int entering_scope, tsubst_flags_t complain)
9137 tree ret;
9138 timevar_push (TV_TEMPLATE_INST);
9139 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9140 entering_scope, complain);
9141 timevar_pop (TV_TEMPLATE_INST);
9142 return ret;
9145 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9147 tree
9148 lookup_template_variable (tree templ, tree arglist)
9150 /* The type of the expression is NULL_TREE since the template-id could refer
9151 to an explicit or partial specialization. */
9152 tree type = NULL_TREE;
9153 if (flag_concepts && variable_concept_p (templ))
9154 /* Except that concepts are always bool. */
9155 type = boolean_type_node;
9156 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9159 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9161 tree
9162 finish_template_variable (tree var, tsubst_flags_t complain)
9164 tree templ = TREE_OPERAND (var, 0);
9165 tree arglist = TREE_OPERAND (var, 1);
9167 /* We never want to return a VAR_DECL for a variable concept, since they
9168 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9169 bool concept_p = flag_concepts && variable_concept_p (templ);
9170 if (concept_p && processing_template_decl)
9171 return var;
9173 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9174 arglist = add_outermost_template_args (tmpl_args, arglist);
9176 templ = most_general_template (templ);
9177 tree parms = DECL_TEMPLATE_PARMS (templ);
9178 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9179 /*req_all*/true,
9180 /*use_default*/true);
9182 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9184 if (complain & tf_error)
9186 error ("use of invalid variable template %qE", var);
9187 diagnose_constraints (location_of (var), templ, arglist);
9189 return error_mark_node;
9192 /* If a template-id refers to a specialization of a variable
9193 concept, then the expression is true if and only if the
9194 concept's constraints are satisfied by the given template
9195 arguments.
9197 NOTE: This is an extension of Concepts Lite TS that
9198 allows constraints to be used in expressions. */
9199 if (concept_p)
9201 tree decl = DECL_TEMPLATE_RESULT (templ);
9202 return evaluate_variable_concept (decl, arglist);
9205 return instantiate_template (templ, arglist, complain);
9208 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9209 TARGS template args, and instantiate it if it's not dependent. */
9211 tree
9212 lookup_and_finish_template_variable (tree templ, tree targs,
9213 tsubst_flags_t complain)
9215 templ = lookup_template_variable (templ, targs);
9216 if (!any_dependent_template_arguments_p (targs))
9218 templ = finish_template_variable (templ, complain);
9219 mark_used (templ);
9222 return convert_from_reference (templ);
9226 struct pair_fn_data
9228 tree_fn_t fn;
9229 tree_fn_t any_fn;
9230 void *data;
9231 /* True when we should also visit template parameters that occur in
9232 non-deduced contexts. */
9233 bool include_nondeduced_p;
9234 hash_set<tree> *visited;
9237 /* Called from for_each_template_parm via walk_tree. */
9239 static tree
9240 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9242 tree t = *tp;
9243 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9244 tree_fn_t fn = pfd->fn;
9245 void *data = pfd->data;
9246 tree result = NULL_TREE;
9248 #define WALK_SUBTREE(NODE) \
9249 do \
9251 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9252 pfd->include_nondeduced_p, \
9253 pfd->any_fn); \
9254 if (result) goto out; \
9256 while (0)
9258 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9259 return t;
9261 if (TYPE_P (t)
9262 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9263 WALK_SUBTREE (TYPE_CONTEXT (t));
9265 switch (TREE_CODE (t))
9267 case RECORD_TYPE:
9268 if (TYPE_PTRMEMFUNC_P (t))
9269 break;
9270 /* Fall through. */
9272 case UNION_TYPE:
9273 case ENUMERAL_TYPE:
9274 if (!TYPE_TEMPLATE_INFO (t))
9275 *walk_subtrees = 0;
9276 else
9277 WALK_SUBTREE (TYPE_TI_ARGS (t));
9278 break;
9280 case INTEGER_TYPE:
9281 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9282 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9283 break;
9285 case METHOD_TYPE:
9286 /* Since we're not going to walk subtrees, we have to do this
9287 explicitly here. */
9288 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9289 /* Fall through. */
9291 case FUNCTION_TYPE:
9292 /* Check the return type. */
9293 WALK_SUBTREE (TREE_TYPE (t));
9295 /* Check the parameter types. Since default arguments are not
9296 instantiated until they are needed, the TYPE_ARG_TYPES may
9297 contain expressions that involve template parameters. But,
9298 no-one should be looking at them yet. And, once they're
9299 instantiated, they don't contain template parameters, so
9300 there's no point in looking at them then, either. */
9302 tree parm;
9304 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9305 WALK_SUBTREE (TREE_VALUE (parm));
9307 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9308 want walk_tree walking into them itself. */
9309 *walk_subtrees = 0;
9312 if (flag_noexcept_type)
9314 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9315 if (spec)
9316 WALK_SUBTREE (TREE_PURPOSE (spec));
9318 break;
9320 case TYPEOF_TYPE:
9321 case UNDERLYING_TYPE:
9322 if (pfd->include_nondeduced_p
9323 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9324 pfd->visited,
9325 pfd->include_nondeduced_p,
9326 pfd->any_fn))
9327 return error_mark_node;
9328 break;
9330 case FUNCTION_DECL:
9331 case VAR_DECL:
9332 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9333 WALK_SUBTREE (DECL_TI_ARGS (t));
9334 /* Fall through. */
9336 case PARM_DECL:
9337 case CONST_DECL:
9338 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9339 WALK_SUBTREE (DECL_INITIAL (t));
9340 if (DECL_CONTEXT (t)
9341 && pfd->include_nondeduced_p)
9342 WALK_SUBTREE (DECL_CONTEXT (t));
9343 break;
9345 case BOUND_TEMPLATE_TEMPLATE_PARM:
9346 /* Record template parameters such as `T' inside `TT<T>'. */
9347 WALK_SUBTREE (TYPE_TI_ARGS (t));
9348 /* Fall through. */
9350 case TEMPLATE_TEMPLATE_PARM:
9351 case TEMPLATE_TYPE_PARM:
9352 case TEMPLATE_PARM_INDEX:
9353 if (fn && (*fn)(t, data))
9354 return t;
9355 else if (!fn)
9356 return t;
9357 break;
9359 case TEMPLATE_DECL:
9360 /* A template template parameter is encountered. */
9361 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9362 WALK_SUBTREE (TREE_TYPE (t));
9364 /* Already substituted template template parameter */
9365 *walk_subtrees = 0;
9366 break;
9368 case TYPENAME_TYPE:
9369 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9370 partial instantiation. */
9371 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9372 break;
9374 case CONSTRUCTOR:
9375 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9376 && pfd->include_nondeduced_p)
9377 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9378 break;
9380 case INDIRECT_REF:
9381 case COMPONENT_REF:
9382 /* If there's no type, then this thing must be some expression
9383 involving template parameters. */
9384 if (!fn && !TREE_TYPE (t))
9385 return error_mark_node;
9386 break;
9388 case MODOP_EXPR:
9389 case CAST_EXPR:
9390 case IMPLICIT_CONV_EXPR:
9391 case REINTERPRET_CAST_EXPR:
9392 case CONST_CAST_EXPR:
9393 case STATIC_CAST_EXPR:
9394 case DYNAMIC_CAST_EXPR:
9395 case ARROW_EXPR:
9396 case DOTSTAR_EXPR:
9397 case TYPEID_EXPR:
9398 case PSEUDO_DTOR_EXPR:
9399 if (!fn)
9400 return error_mark_node;
9401 break;
9403 default:
9404 break;
9407 #undef WALK_SUBTREE
9409 /* We didn't find any template parameters we liked. */
9410 out:
9411 return result;
9414 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9415 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9416 call FN with the parameter and the DATA.
9417 If FN returns nonzero, the iteration is terminated, and
9418 for_each_template_parm returns 1. Otherwise, the iteration
9419 continues. If FN never returns a nonzero value, the value
9420 returned by for_each_template_parm is 0. If FN is NULL, it is
9421 considered to be the function which always returns 1.
9423 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9424 parameters that occur in non-deduced contexts. When false, only
9425 visits those template parameters that can be deduced. */
9427 static tree
9428 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9429 hash_set<tree> *visited,
9430 bool include_nondeduced_p,
9431 tree_fn_t any_fn)
9433 struct pair_fn_data pfd;
9434 tree result;
9436 /* Set up. */
9437 pfd.fn = fn;
9438 pfd.any_fn = any_fn;
9439 pfd.data = data;
9440 pfd.include_nondeduced_p = include_nondeduced_p;
9442 /* Walk the tree. (Conceptually, we would like to walk without
9443 duplicates, but for_each_template_parm_r recursively calls
9444 for_each_template_parm, so we would need to reorganize a fair
9445 bit to use walk_tree_without_duplicates, so we keep our own
9446 visited list.) */
9447 if (visited)
9448 pfd.visited = visited;
9449 else
9450 pfd.visited = new hash_set<tree>;
9451 result = cp_walk_tree (&t,
9452 for_each_template_parm_r,
9453 &pfd,
9454 pfd.visited);
9456 /* Clean up. */
9457 if (!visited)
9459 delete pfd.visited;
9460 pfd.visited = 0;
9463 return result;
9466 /* Returns true if T depends on any template parameter. */
9469 uses_template_parms (tree t)
9471 if (t == NULL_TREE)
9472 return false;
9474 bool dependent_p;
9475 int saved_processing_template_decl;
9477 saved_processing_template_decl = processing_template_decl;
9478 if (!saved_processing_template_decl)
9479 processing_template_decl = 1;
9480 if (TYPE_P (t))
9481 dependent_p = dependent_type_p (t);
9482 else if (TREE_CODE (t) == TREE_VEC)
9483 dependent_p = any_dependent_template_arguments_p (t);
9484 else if (TREE_CODE (t) == TREE_LIST)
9485 dependent_p = (uses_template_parms (TREE_VALUE (t))
9486 || uses_template_parms (TREE_CHAIN (t)));
9487 else if (TREE_CODE (t) == TYPE_DECL)
9488 dependent_p = dependent_type_p (TREE_TYPE (t));
9489 else if (DECL_P (t)
9490 || EXPR_P (t)
9491 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9492 || TREE_CODE (t) == OVERLOAD
9493 || BASELINK_P (t)
9494 || identifier_p (t)
9495 || TREE_CODE (t) == TRAIT_EXPR
9496 || TREE_CODE (t) == CONSTRUCTOR
9497 || CONSTANT_CLASS_P (t))
9498 dependent_p = (type_dependent_expression_p (t)
9499 || value_dependent_expression_p (t));
9500 else
9502 gcc_assert (t == error_mark_node);
9503 dependent_p = false;
9506 processing_template_decl = saved_processing_template_decl;
9508 return dependent_p;
9511 /* Returns true iff current_function_decl is an incompletely instantiated
9512 template. Useful instead of processing_template_decl because the latter
9513 is set to 0 during instantiate_non_dependent_expr. */
9515 bool
9516 in_template_function (void)
9518 tree fn = current_function_decl;
9519 bool ret;
9520 ++processing_template_decl;
9521 ret = (fn && DECL_LANG_SPECIFIC (fn)
9522 && DECL_TEMPLATE_INFO (fn)
9523 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9524 --processing_template_decl;
9525 return ret;
9528 /* Returns true if T depends on any template parameter with level LEVEL. */
9530 bool
9531 uses_template_parms_level (tree t, int level)
9533 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9534 /*include_nondeduced_p=*/true);
9537 /* Returns true if the signature of DECL depends on any template parameter from
9538 its enclosing class. */
9540 bool
9541 uses_outer_template_parms (tree decl)
9543 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9544 if (depth == 0)
9545 return false;
9546 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9547 &depth, NULL, /*include_nondeduced_p=*/true))
9548 return true;
9549 if (PRIMARY_TEMPLATE_P (decl)
9550 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9551 (DECL_TEMPLATE_PARMS (decl)),
9552 template_parm_outer_level,
9553 &depth, NULL, /*include_nondeduced_p=*/true))
9554 return true;
9555 tree ci = get_constraints (decl);
9556 if (ci)
9557 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9558 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9559 &depth, NULL, /*nondeduced*/true))
9560 return true;
9561 return false;
9564 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9565 ill-formed translation unit, i.e. a variable or function that isn't
9566 usable in a constant expression. */
9568 static inline bool
9569 neglectable_inst_p (tree d)
9571 return (DECL_P (d)
9572 && !undeduced_auto_decl (d)
9573 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9574 : decl_maybe_constant_var_p (d)));
9577 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9578 neglectable and instantiated from within an erroneous instantiation. */
9580 static bool
9581 limit_bad_template_recursion (tree decl)
9583 struct tinst_level *lev = current_tinst_level;
9584 int errs = errorcount + sorrycount;
9585 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9586 return false;
9588 for (; lev; lev = lev->next)
9589 if (neglectable_inst_p (lev->decl))
9590 break;
9592 return (lev && errs > lev->errors);
9595 static int tinst_depth;
9596 extern int max_tinst_depth;
9597 int depth_reached;
9599 static GTY(()) struct tinst_level *last_error_tinst_level;
9601 /* We're starting to instantiate D; record the template instantiation context
9602 for diagnostics and to restore it later. */
9604 bool
9605 push_tinst_level (tree d)
9607 return push_tinst_level_loc (d, input_location);
9610 /* We're starting to instantiate D; record the template instantiation context
9611 at LOC for diagnostics and to restore it later. */
9613 bool
9614 push_tinst_level_loc (tree d, location_t loc)
9616 struct tinst_level *new_level;
9618 if (tinst_depth >= max_tinst_depth)
9620 /* Tell error.c not to try to instantiate any templates. */
9621 at_eof = 2;
9622 fatal_error (input_location,
9623 "template instantiation depth exceeds maximum of %d"
9624 " (use -ftemplate-depth= to increase the maximum)",
9625 max_tinst_depth);
9626 return false;
9629 /* If the current instantiation caused problems, don't let it instantiate
9630 anything else. Do allow deduction substitution and decls usable in
9631 constant expressions. */
9632 if (limit_bad_template_recursion (d))
9633 return false;
9635 /* When not -quiet, dump template instantiations other than functions, since
9636 announce_function will take care of those. */
9637 if (!quiet_flag
9638 && TREE_CODE (d) != TREE_LIST
9639 && TREE_CODE (d) != FUNCTION_DECL)
9640 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9642 new_level = ggc_alloc<tinst_level> ();
9643 new_level->decl = d;
9644 new_level->locus = loc;
9645 new_level->errors = errorcount+sorrycount;
9646 new_level->in_system_header_p = in_system_header_at (input_location);
9647 new_level->next = current_tinst_level;
9648 current_tinst_level = new_level;
9650 ++tinst_depth;
9651 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9652 depth_reached = tinst_depth;
9654 return true;
9657 /* We're done instantiating this template; return to the instantiation
9658 context. */
9660 void
9661 pop_tinst_level (void)
9663 /* Restore the filename and line number stashed away when we started
9664 this instantiation. */
9665 input_location = current_tinst_level->locus;
9666 current_tinst_level = current_tinst_level->next;
9667 --tinst_depth;
9670 /* We're instantiating a deferred template; restore the template
9671 instantiation context in which the instantiation was requested, which
9672 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9674 static tree
9675 reopen_tinst_level (struct tinst_level *level)
9677 struct tinst_level *t;
9679 tinst_depth = 0;
9680 for (t = level; t; t = t->next)
9681 ++tinst_depth;
9683 current_tinst_level = level;
9684 pop_tinst_level ();
9685 if (current_tinst_level)
9686 current_tinst_level->errors = errorcount+sorrycount;
9687 return level->decl;
9690 /* Returns the TINST_LEVEL which gives the original instantiation
9691 context. */
9693 struct tinst_level *
9694 outermost_tinst_level (void)
9696 struct tinst_level *level = current_tinst_level;
9697 if (level)
9698 while (level->next)
9699 level = level->next;
9700 return level;
9703 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9704 vector of template arguments, as for tsubst.
9706 Returns an appropriate tsubst'd friend declaration. */
9708 static tree
9709 tsubst_friend_function (tree decl, tree args)
9711 tree new_friend;
9713 if (TREE_CODE (decl) == FUNCTION_DECL
9714 && DECL_TEMPLATE_INSTANTIATION (decl)
9715 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9716 /* This was a friend declared with an explicit template
9717 argument list, e.g.:
9719 friend void f<>(T);
9721 to indicate that f was a template instantiation, not a new
9722 function declaration. Now, we have to figure out what
9723 instantiation of what template. */
9725 tree template_id, arglist, fns;
9726 tree new_args;
9727 tree tmpl;
9728 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9730 /* Friend functions are looked up in the containing namespace scope.
9731 We must enter that scope, to avoid finding member functions of the
9732 current class with same name. */
9733 push_nested_namespace (ns);
9734 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9735 tf_warning_or_error, NULL_TREE,
9736 /*integral_constant_expression_p=*/false);
9737 pop_nested_namespace (ns);
9738 arglist = tsubst (DECL_TI_ARGS (decl), args,
9739 tf_warning_or_error, NULL_TREE);
9740 template_id = lookup_template_function (fns, arglist);
9742 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9743 tmpl = determine_specialization (template_id, new_friend,
9744 &new_args,
9745 /*need_member_template=*/0,
9746 TREE_VEC_LENGTH (args),
9747 tsk_none);
9748 return instantiate_template (tmpl, new_args, tf_error);
9751 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9753 /* The NEW_FRIEND will look like an instantiation, to the
9754 compiler, but is not an instantiation from the point of view of
9755 the language. For example, we might have had:
9757 template <class T> struct S {
9758 template <class U> friend void f(T, U);
9761 Then, in S<int>, template <class U> void f(int, U) is not an
9762 instantiation of anything. */
9763 if (new_friend == error_mark_node)
9764 return error_mark_node;
9766 DECL_USE_TEMPLATE (new_friend) = 0;
9767 if (TREE_CODE (decl) == TEMPLATE_DECL)
9769 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9770 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9771 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9774 /* The mangled name for the NEW_FRIEND is incorrect. The function
9775 is not a template instantiation and should not be mangled like
9776 one. Therefore, we forget the mangling here; we'll recompute it
9777 later if we need it. */
9778 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9780 SET_DECL_RTL (new_friend, NULL);
9781 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9784 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9786 tree old_decl;
9787 tree new_friend_template_info;
9788 tree new_friend_result_template_info;
9789 tree ns;
9790 int new_friend_is_defn;
9792 /* We must save some information from NEW_FRIEND before calling
9793 duplicate decls since that function will free NEW_FRIEND if
9794 possible. */
9795 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9796 new_friend_is_defn =
9797 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9798 (template_for_substitution (new_friend)))
9799 != NULL_TREE);
9800 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9802 /* This declaration is a `primary' template. */
9803 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9805 new_friend_result_template_info
9806 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9808 else
9809 new_friend_result_template_info = NULL_TREE;
9811 /* Inside pushdecl_namespace_level, we will push into the
9812 current namespace. However, the friend function should go
9813 into the namespace of the template. */
9814 ns = decl_namespace_context (new_friend);
9815 push_nested_namespace (ns);
9816 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9817 pop_nested_namespace (ns);
9819 if (old_decl == error_mark_node)
9820 return error_mark_node;
9822 if (old_decl != new_friend)
9824 /* This new friend declaration matched an existing
9825 declaration. For example, given:
9827 template <class T> void f(T);
9828 template <class U> class C {
9829 template <class T> friend void f(T) {}
9832 the friend declaration actually provides the definition
9833 of `f', once C has been instantiated for some type. So,
9834 old_decl will be the out-of-class template declaration,
9835 while new_friend is the in-class definition.
9837 But, if `f' was called before this point, the
9838 instantiation of `f' will have DECL_TI_ARGS corresponding
9839 to `T' but not to `U', references to which might appear
9840 in the definition of `f'. Previously, the most general
9841 template for an instantiation of `f' was the out-of-class
9842 version; now it is the in-class version. Therefore, we
9843 run through all specialization of `f', adding to their
9844 DECL_TI_ARGS appropriately. In particular, they need a
9845 new set of outer arguments, corresponding to the
9846 arguments for this class instantiation.
9848 The same situation can arise with something like this:
9850 friend void f(int);
9851 template <class T> class C {
9852 friend void f(T) {}
9855 when `C<int>' is instantiated. Now, `f(int)' is defined
9856 in the class. */
9858 if (!new_friend_is_defn)
9859 /* On the other hand, if the in-class declaration does
9860 *not* provide a definition, then we don't want to alter
9861 existing definitions. We can just leave everything
9862 alone. */
9864 else
9866 tree new_template = TI_TEMPLATE (new_friend_template_info);
9867 tree new_args = TI_ARGS (new_friend_template_info);
9869 /* Overwrite whatever template info was there before, if
9870 any, with the new template information pertaining to
9871 the declaration. */
9872 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9874 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9876 /* We should have called reregister_specialization in
9877 duplicate_decls. */
9878 gcc_assert (retrieve_specialization (new_template,
9879 new_args, 0)
9880 == old_decl);
9882 /* Instantiate it if the global has already been used. */
9883 if (DECL_ODR_USED (old_decl))
9884 instantiate_decl (old_decl, /*defer_ok=*/true,
9885 /*expl_inst_class_mem_p=*/false);
9887 else
9889 tree t;
9891 /* Indicate that the old function template is a partial
9892 instantiation. */
9893 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9894 = new_friend_result_template_info;
9896 gcc_assert (new_template
9897 == most_general_template (new_template));
9898 gcc_assert (new_template != old_decl);
9900 /* Reassign any specializations already in the hash table
9901 to the new more general template, and add the
9902 additional template args. */
9903 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9904 t != NULL_TREE;
9905 t = TREE_CHAIN (t))
9907 tree spec = TREE_VALUE (t);
9908 spec_entry elt;
9910 elt.tmpl = old_decl;
9911 elt.args = DECL_TI_ARGS (spec);
9912 elt.spec = NULL_TREE;
9914 decl_specializations->remove_elt (&elt);
9916 DECL_TI_ARGS (spec)
9917 = add_outermost_template_args (new_args,
9918 DECL_TI_ARGS (spec));
9920 register_specialization
9921 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9924 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9928 /* The information from NEW_FRIEND has been merged into OLD_DECL
9929 by duplicate_decls. */
9930 new_friend = old_decl;
9933 else
9935 tree context = DECL_CONTEXT (new_friend);
9936 bool dependent_p;
9938 /* In the code
9939 template <class T> class C {
9940 template <class U> friend void C1<U>::f (); // case 1
9941 friend void C2<T>::f (); // case 2
9943 we only need to make sure CONTEXT is a complete type for
9944 case 2. To distinguish between the two cases, we note that
9945 CONTEXT of case 1 remains dependent type after tsubst while
9946 this isn't true for case 2. */
9947 ++processing_template_decl;
9948 dependent_p = dependent_type_p (context);
9949 --processing_template_decl;
9951 if (!dependent_p
9952 && !complete_type_or_else (context, NULL_TREE))
9953 return error_mark_node;
9955 if (COMPLETE_TYPE_P (context))
9957 tree fn = new_friend;
9958 /* do_friend adds the TEMPLATE_DECL for any member friend
9959 template even if it isn't a member template, i.e.
9960 template <class T> friend A<T>::f();
9961 Look through it in that case. */
9962 if (TREE_CODE (fn) == TEMPLATE_DECL
9963 && !PRIMARY_TEMPLATE_P (fn))
9964 fn = DECL_TEMPLATE_RESULT (fn);
9965 /* Check to see that the declaration is really present, and,
9966 possibly obtain an improved declaration. */
9967 fn = check_classfn (context, fn, NULL_TREE);
9969 if (fn)
9970 new_friend = fn;
9974 return new_friend;
9977 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9978 template arguments, as for tsubst.
9980 Returns an appropriate tsubst'd friend type or error_mark_node on
9981 failure. */
9983 static tree
9984 tsubst_friend_class (tree friend_tmpl, tree args)
9986 tree friend_type;
9987 tree tmpl;
9988 tree context;
9990 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9992 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9993 return TREE_TYPE (t);
9996 context = CP_DECL_CONTEXT (friend_tmpl);
9998 if (context != global_namespace)
10000 if (TREE_CODE (context) == NAMESPACE_DECL)
10001 push_nested_namespace (context);
10002 else
10003 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
10006 /* Look for a class template declaration. We look for hidden names
10007 because two friend declarations of the same template are the
10008 same. For example, in:
10010 struct A {
10011 template <typename> friend class F;
10013 template <typename> struct B {
10014 template <typename> friend class F;
10017 both F templates are the same. */
10018 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10019 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10021 /* But, if we don't find one, it might be because we're in a
10022 situation like this:
10024 template <class T>
10025 struct S {
10026 template <class U>
10027 friend struct S;
10030 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10031 for `S<int>', not the TEMPLATE_DECL. */
10032 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10034 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10035 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10038 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10040 /* The friend template has already been declared. Just
10041 check to see that the declarations match, and install any new
10042 default parameters. We must tsubst the default parameters,
10043 of course. We only need the innermost template parameters
10044 because that is all that redeclare_class_template will look
10045 at. */
10046 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10047 > TMPL_ARGS_DEPTH (args))
10049 tree parms;
10050 location_t saved_input_location;
10051 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10052 args, tf_warning_or_error);
10054 saved_input_location = input_location;
10055 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10056 tree cons = get_constraints (tmpl);
10057 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10058 input_location = saved_input_location;
10062 friend_type = TREE_TYPE (tmpl);
10064 else
10066 /* The friend template has not already been declared. In this
10067 case, the instantiation of the template class will cause the
10068 injection of this template into the global scope. */
10069 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10070 if (tmpl == error_mark_node)
10071 return error_mark_node;
10073 /* The new TMPL is not an instantiation of anything, so we
10074 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10075 the new type because that is supposed to be the corresponding
10076 template decl, i.e., TMPL. */
10077 DECL_USE_TEMPLATE (tmpl) = 0;
10078 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10079 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10080 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10081 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10083 /* Inject this template into the global scope. */
10084 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10087 if (context != global_namespace)
10089 if (TREE_CODE (context) == NAMESPACE_DECL)
10090 pop_nested_namespace (context);
10091 else
10092 pop_nested_class ();
10095 return friend_type;
10098 /* Returns zero if TYPE cannot be completed later due to circularity.
10099 Otherwise returns one. */
10101 static int
10102 can_complete_type_without_circularity (tree type)
10104 if (type == NULL_TREE || type == error_mark_node)
10105 return 0;
10106 else if (COMPLETE_TYPE_P (type))
10107 return 1;
10108 else if (TREE_CODE (type) == ARRAY_TYPE)
10109 return can_complete_type_without_circularity (TREE_TYPE (type));
10110 else if (CLASS_TYPE_P (type)
10111 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10112 return 0;
10113 else
10114 return 1;
10117 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10118 tsubst_flags_t, tree);
10120 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10121 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10123 static tree
10124 tsubst_attribute (tree t, tree *decl_p, tree args,
10125 tsubst_flags_t complain, tree in_decl)
10127 gcc_assert (ATTR_IS_DEPENDENT (t));
10129 tree val = TREE_VALUE (t);
10130 if (val == NULL_TREE)
10131 /* Nothing to do. */;
10132 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10133 && is_attribute_p ("omp declare simd",
10134 get_attribute_name (t)))
10136 tree clauses = TREE_VALUE (val);
10137 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10138 complain, in_decl);
10139 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10140 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10141 tree parms = DECL_ARGUMENTS (*decl_p);
10142 clauses
10143 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10144 if (clauses)
10145 val = build_tree_list (NULL_TREE, clauses);
10146 else
10147 val = NULL_TREE;
10149 /* If the first attribute argument is an identifier, don't
10150 pass it through tsubst. Attributes like mode, format,
10151 cleanup and several target specific attributes expect it
10152 unmodified. */
10153 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10155 tree chain
10156 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10157 /*integral_constant_expression_p=*/false);
10158 if (chain != TREE_CHAIN (val))
10159 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10161 else if (PACK_EXPANSION_P (val))
10163 /* An attribute pack expansion. */
10164 tree purp = TREE_PURPOSE (t);
10165 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10166 if (pack == error_mark_node)
10167 return error_mark_node;
10168 int len = TREE_VEC_LENGTH (pack);
10169 tree list = NULL_TREE;
10170 tree *q = &list;
10171 for (int i = 0; i < len; ++i)
10173 tree elt = TREE_VEC_ELT (pack, i);
10174 *q = build_tree_list (purp, elt);
10175 q = &TREE_CHAIN (*q);
10177 return list;
10179 else
10180 val = tsubst_expr (val, args, complain, in_decl,
10181 /*integral_constant_expression_p=*/false);
10183 if (val != TREE_VALUE (t))
10184 return build_tree_list (TREE_PURPOSE (t), val);
10185 return t;
10188 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10189 unchanged or a new TREE_LIST chain. */
10191 static tree
10192 tsubst_attributes (tree attributes, tree args,
10193 tsubst_flags_t complain, tree in_decl)
10195 tree last_dep = NULL_TREE;
10197 for (tree t = attributes; t; t = TREE_CHAIN (t))
10198 if (ATTR_IS_DEPENDENT (t))
10200 last_dep = t;
10201 attributes = copy_list (attributes);
10202 break;
10205 if (last_dep)
10206 for (tree *p = &attributes; *p; )
10208 tree t = *p;
10209 if (ATTR_IS_DEPENDENT (t))
10211 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10212 if (subst != t)
10214 *p = subst;
10216 p = &TREE_CHAIN (*p);
10217 while (*p);
10218 *p = TREE_CHAIN (t);
10219 continue;
10222 p = &TREE_CHAIN (*p);
10225 return attributes;
10228 /* Apply any attributes which had to be deferred until instantiation
10229 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10230 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10232 static void
10233 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10234 tree args, tsubst_flags_t complain, tree in_decl)
10236 tree last_dep = NULL_TREE;
10237 tree t;
10238 tree *p;
10240 if (attributes == NULL_TREE)
10241 return;
10243 if (DECL_P (*decl_p))
10245 if (TREE_TYPE (*decl_p) == error_mark_node)
10246 return;
10247 p = &DECL_ATTRIBUTES (*decl_p);
10248 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10249 to our attributes parameter. */
10250 gcc_assert (*p == attributes);
10252 else
10254 p = &TYPE_ATTRIBUTES (*decl_p);
10255 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10256 lookup_template_class_1, and should be preserved. */
10257 gcc_assert (*p != attributes);
10258 while (*p)
10259 p = &TREE_CHAIN (*p);
10262 for (t = attributes; t; t = TREE_CHAIN (t))
10263 if (ATTR_IS_DEPENDENT (t))
10265 last_dep = t;
10266 attributes = copy_list (attributes);
10267 break;
10270 *p = attributes;
10271 if (last_dep)
10273 tree late_attrs = NULL_TREE;
10274 tree *q = &late_attrs;
10276 for (; *p; )
10278 t = *p;
10279 if (ATTR_IS_DEPENDENT (t))
10281 *p = TREE_CHAIN (t);
10282 TREE_CHAIN (t) = NULL_TREE;
10283 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10285 q = &TREE_CHAIN (*q);
10286 while (*q);
10288 else
10289 p = &TREE_CHAIN (t);
10292 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10296 /* Perform (or defer) access check for typedefs that were referenced
10297 from within the template TMPL code.
10298 This is a subroutine of instantiate_decl and instantiate_class_template.
10299 TMPL is the template to consider and TARGS is the list of arguments of
10300 that template. */
10302 static void
10303 perform_typedefs_access_check (tree tmpl, tree targs)
10305 location_t saved_location;
10306 unsigned i;
10307 qualified_typedef_usage_t *iter;
10309 if (!tmpl
10310 || (!CLASS_TYPE_P (tmpl)
10311 && TREE_CODE (tmpl) != FUNCTION_DECL))
10312 return;
10314 saved_location = input_location;
10315 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10317 tree type_decl = iter->typedef_decl;
10318 tree type_scope = iter->context;
10320 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10321 continue;
10323 if (uses_template_parms (type_decl))
10324 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10325 if (uses_template_parms (type_scope))
10326 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10328 /* Make access check error messages point to the location
10329 of the use of the typedef. */
10330 input_location = iter->locus;
10331 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10332 type_decl, type_decl,
10333 tf_warning_or_error);
10335 input_location = saved_location;
10338 static tree
10339 instantiate_class_template_1 (tree type)
10341 tree templ, args, pattern, t, member;
10342 tree typedecl;
10343 tree pbinfo;
10344 tree base_list;
10345 unsigned int saved_maximum_field_alignment;
10346 tree fn_context;
10348 if (type == error_mark_node)
10349 return error_mark_node;
10351 if (COMPLETE_OR_OPEN_TYPE_P (type)
10352 || uses_template_parms (type))
10353 return type;
10355 /* Figure out which template is being instantiated. */
10356 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10357 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10359 /* Determine what specialization of the original template to
10360 instantiate. */
10361 t = most_specialized_partial_spec (type, tf_warning_or_error);
10362 if (t == error_mark_node)
10364 TYPE_BEING_DEFINED (type) = 1;
10365 return error_mark_node;
10367 else if (t)
10369 /* This TYPE is actually an instantiation of a partial
10370 specialization. We replace the innermost set of ARGS with
10371 the arguments appropriate for substitution. For example,
10372 given:
10374 template <class T> struct S {};
10375 template <class T> struct S<T*> {};
10377 and supposing that we are instantiating S<int*>, ARGS will
10378 presently be {int*} -- but we need {int}. */
10379 pattern = TREE_TYPE (t);
10380 args = TREE_PURPOSE (t);
10382 else
10384 pattern = TREE_TYPE (templ);
10385 args = CLASSTYPE_TI_ARGS (type);
10388 /* If the template we're instantiating is incomplete, then clearly
10389 there's nothing we can do. */
10390 if (!COMPLETE_TYPE_P (pattern))
10391 return type;
10393 /* If we've recursively instantiated too many templates, stop. */
10394 if (! push_tinst_level (type))
10395 return type;
10397 /* Now we're really doing the instantiation. Mark the type as in
10398 the process of being defined. */
10399 TYPE_BEING_DEFINED (type) = 1;
10401 /* We may be in the middle of deferred access check. Disable
10402 it now. */
10403 push_deferring_access_checks (dk_no_deferred);
10405 int saved_unevaluated_operand = cp_unevaluated_operand;
10406 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10408 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10409 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10410 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10411 fn_context = error_mark_node;
10412 if (!fn_context)
10413 push_to_top_level ();
10414 else
10416 cp_unevaluated_operand = 0;
10417 c_inhibit_evaluation_warnings = 0;
10419 /* Use #pragma pack from the template context. */
10420 saved_maximum_field_alignment = maximum_field_alignment;
10421 maximum_field_alignment = TYPE_PRECISION (pattern);
10423 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10425 /* Set the input location to the most specialized template definition.
10426 This is needed if tsubsting causes an error. */
10427 typedecl = TYPE_MAIN_DECL (pattern);
10428 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10429 DECL_SOURCE_LOCATION (typedecl);
10431 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10432 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10433 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10434 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10435 if (ANON_AGGR_TYPE_P (pattern))
10436 SET_ANON_AGGR_TYPE_P (type);
10437 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10439 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10440 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10441 /* Adjust visibility for template arguments. */
10442 determine_visibility (TYPE_MAIN_DECL (type));
10444 if (CLASS_TYPE_P (type))
10445 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10447 pbinfo = TYPE_BINFO (pattern);
10449 /* We should never instantiate a nested class before its enclosing
10450 class; we need to look up the nested class by name before we can
10451 instantiate it, and that lookup should instantiate the enclosing
10452 class. */
10453 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10454 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10456 base_list = NULL_TREE;
10457 if (BINFO_N_BASE_BINFOS (pbinfo))
10459 tree pbase_binfo;
10460 tree pushed_scope;
10461 int i;
10463 /* We must enter the scope containing the type, as that is where
10464 the accessibility of types named in dependent bases are
10465 looked up from. */
10466 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10468 /* Substitute into each of the bases to determine the actual
10469 basetypes. */
10470 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10472 tree base;
10473 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10474 tree expanded_bases = NULL_TREE;
10475 int idx, len = 1;
10477 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10479 expanded_bases =
10480 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10481 args, tf_error, NULL_TREE);
10482 if (expanded_bases == error_mark_node)
10483 continue;
10485 len = TREE_VEC_LENGTH (expanded_bases);
10488 for (idx = 0; idx < len; idx++)
10490 if (expanded_bases)
10491 /* Extract the already-expanded base class. */
10492 base = TREE_VEC_ELT (expanded_bases, idx);
10493 else
10494 /* Substitute to figure out the base class. */
10495 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10496 NULL_TREE);
10498 if (base == error_mark_node)
10499 continue;
10501 base_list = tree_cons (access, base, base_list);
10502 if (BINFO_VIRTUAL_P (pbase_binfo))
10503 TREE_TYPE (base_list) = integer_type_node;
10507 /* The list is now in reverse order; correct that. */
10508 base_list = nreverse (base_list);
10510 if (pushed_scope)
10511 pop_scope (pushed_scope);
10513 /* Now call xref_basetypes to set up all the base-class
10514 information. */
10515 xref_basetypes (type, base_list);
10517 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10518 (int) ATTR_FLAG_TYPE_IN_PLACE,
10519 args, tf_error, NULL_TREE);
10520 fixup_attribute_variants (type);
10522 /* Now that our base classes are set up, enter the scope of the
10523 class, so that name lookups into base classes, etc. will work
10524 correctly. This is precisely analogous to what we do in
10525 begin_class_definition when defining an ordinary non-template
10526 class, except we also need to push the enclosing classes. */
10527 push_nested_class (type);
10529 /* Now members are processed in the order of declaration. */
10530 for (member = CLASSTYPE_DECL_LIST (pattern);
10531 member; member = TREE_CHAIN (member))
10533 tree t = TREE_VALUE (member);
10535 if (TREE_PURPOSE (member))
10537 if (TYPE_P (t))
10539 /* Build new CLASSTYPE_NESTED_UTDS. */
10541 tree newtag;
10542 bool class_template_p;
10544 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10545 && TYPE_LANG_SPECIFIC (t)
10546 && CLASSTYPE_IS_TEMPLATE (t));
10547 /* If the member is a class template, then -- even after
10548 substitution -- there may be dependent types in the
10549 template argument list for the class. We increment
10550 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10551 that function will assume that no types are dependent
10552 when outside of a template. */
10553 if (class_template_p)
10554 ++processing_template_decl;
10555 newtag = tsubst (t, args, tf_error, NULL_TREE);
10556 if (class_template_p)
10557 --processing_template_decl;
10558 if (newtag == error_mark_node)
10559 continue;
10561 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10563 tree name = TYPE_IDENTIFIER (t);
10565 if (class_template_p)
10566 /* Unfortunately, lookup_template_class sets
10567 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10568 instantiation (i.e., for the type of a member
10569 template class nested within a template class.)
10570 This behavior is required for
10571 maybe_process_partial_specialization to work
10572 correctly, but is not accurate in this case;
10573 the TAG is not an instantiation of anything.
10574 (The corresponding TEMPLATE_DECL is an
10575 instantiation, but the TYPE is not.) */
10576 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10578 /* Now, we call pushtag to put this NEWTAG into the scope of
10579 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10580 pushtag calling push_template_decl. We don't have to do
10581 this for enums because it will already have been done in
10582 tsubst_enum. */
10583 if (name)
10584 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10585 pushtag (name, newtag, /*tag_scope=*/ts_current);
10588 else if (DECL_DECLARES_FUNCTION_P (t))
10590 tree r;
10592 if (TREE_CODE (t) == TEMPLATE_DECL)
10593 ++processing_template_decl;
10594 r = tsubst (t, args, tf_error, NULL_TREE);
10595 if (TREE_CODE (t) == TEMPLATE_DECL)
10596 --processing_template_decl;
10597 set_current_access_from_decl (r);
10598 finish_member_declaration (r);
10599 /* Instantiate members marked with attribute used. */
10600 if (r != error_mark_node && DECL_PRESERVE_P (r))
10601 mark_used (r);
10602 if (TREE_CODE (r) == FUNCTION_DECL
10603 && DECL_OMP_DECLARE_REDUCTION_P (r))
10604 cp_check_omp_declare_reduction (r);
10606 else if (DECL_CLASS_TEMPLATE_P (t)
10607 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10608 /* A closure type for a lambda in a default argument for a
10609 member template. Ignore it; it will be instantiated with
10610 the default argument. */;
10611 else
10613 /* Build new TYPE_FIELDS. */
10614 if (TREE_CODE (t) == STATIC_ASSERT)
10616 tree condition;
10618 ++c_inhibit_evaluation_warnings;
10619 condition =
10620 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10621 tf_warning_or_error, NULL_TREE,
10622 /*integral_constant_expression_p=*/true);
10623 --c_inhibit_evaluation_warnings;
10625 finish_static_assert (condition,
10626 STATIC_ASSERT_MESSAGE (t),
10627 STATIC_ASSERT_SOURCE_LOCATION (t),
10628 /*member_p=*/true);
10630 else if (TREE_CODE (t) != CONST_DECL)
10632 tree r;
10633 tree vec = NULL_TREE;
10634 int len = 1;
10636 /* The file and line for this declaration, to
10637 assist in error message reporting. Since we
10638 called push_tinst_level above, we don't need to
10639 restore these. */
10640 input_location = DECL_SOURCE_LOCATION (t);
10642 if (TREE_CODE (t) == TEMPLATE_DECL)
10643 ++processing_template_decl;
10644 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10645 if (TREE_CODE (t) == TEMPLATE_DECL)
10646 --processing_template_decl;
10648 if (TREE_CODE (r) == TREE_VEC)
10650 /* A capture pack became multiple fields. */
10651 vec = r;
10652 len = TREE_VEC_LENGTH (vec);
10655 for (int i = 0; i < len; ++i)
10657 if (vec)
10658 r = TREE_VEC_ELT (vec, i);
10659 if (VAR_P (r))
10661 /* In [temp.inst]:
10663 [t]he initialization (and any associated
10664 side-effects) of a static data member does
10665 not occur unless the static data member is
10666 itself used in a way that requires the
10667 definition of the static data member to
10668 exist.
10670 Therefore, we do not substitute into the
10671 initialized for the static data member here. */
10672 finish_static_data_member_decl
10674 /*init=*/NULL_TREE,
10675 /*init_const_expr_p=*/false,
10676 /*asmspec_tree=*/NULL_TREE,
10677 /*flags=*/0);
10678 /* Instantiate members marked with attribute used. */
10679 if (r != error_mark_node && DECL_PRESERVE_P (r))
10680 mark_used (r);
10682 else if (TREE_CODE (r) == FIELD_DECL)
10684 /* Determine whether R has a valid type and can be
10685 completed later. If R is invalid, then its type
10686 is replaced by error_mark_node. */
10687 tree rtype = TREE_TYPE (r);
10688 if (can_complete_type_without_circularity (rtype))
10689 complete_type (rtype);
10691 if (!complete_or_array_type_p (rtype))
10693 /* If R's type couldn't be completed and
10694 it isn't a flexible array member (whose
10695 type is incomplete by definition) give
10696 an error. */
10697 cxx_incomplete_type_error (r, rtype);
10698 TREE_TYPE (r) = error_mark_node;
10702 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10703 such a thing will already have been added to the field
10704 list by tsubst_enum in finish_member_declaration in the
10705 CLASSTYPE_NESTED_UTDS case above. */
10706 if (!(TREE_CODE (r) == TYPE_DECL
10707 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10708 && DECL_ARTIFICIAL (r)))
10710 set_current_access_from_decl (r);
10711 finish_member_declaration (r);
10717 else
10719 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10720 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10722 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10724 tree friend_type = t;
10725 bool adjust_processing_template_decl = false;
10727 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10729 /* template <class T> friend class C; */
10730 friend_type = tsubst_friend_class (friend_type, args);
10731 adjust_processing_template_decl = true;
10733 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10735 /* template <class T> friend class C::D; */
10736 friend_type = tsubst (friend_type, args,
10737 tf_warning_or_error, NULL_TREE);
10738 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10739 friend_type = TREE_TYPE (friend_type);
10740 adjust_processing_template_decl = true;
10742 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10743 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10745 /* This could be either
10747 friend class T::C;
10749 when dependent_type_p is false or
10751 template <class U> friend class T::C;
10753 otherwise. */
10754 /* Bump processing_template_decl in case this is something like
10755 template <class T> friend struct A<T>::B. */
10756 ++processing_template_decl;
10757 friend_type = tsubst (friend_type, args,
10758 tf_warning_or_error, NULL_TREE);
10759 if (dependent_type_p (friend_type))
10760 adjust_processing_template_decl = true;
10761 --processing_template_decl;
10763 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10764 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10765 && TYPE_HIDDEN_P (friend_type))
10767 /* friend class C;
10769 where C hasn't been declared yet. Let's lookup name
10770 from namespace scope directly, bypassing any name that
10771 come from dependent base class. */
10772 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10774 /* The call to xref_tag_from_type does injection for friend
10775 classes. */
10776 push_nested_namespace (ns);
10777 friend_type =
10778 xref_tag_from_type (friend_type, NULL_TREE,
10779 /*tag_scope=*/ts_current);
10780 pop_nested_namespace (ns);
10782 else if (uses_template_parms (friend_type))
10783 /* friend class C<T>; */
10784 friend_type = tsubst (friend_type, args,
10785 tf_warning_or_error, NULL_TREE);
10786 /* Otherwise it's
10788 friend class C;
10790 where C is already declared or
10792 friend class C<int>;
10794 We don't have to do anything in these cases. */
10796 if (adjust_processing_template_decl)
10797 /* Trick make_friend_class into realizing that the friend
10798 we're adding is a template, not an ordinary class. It's
10799 important that we use make_friend_class since it will
10800 perform some error-checking and output cross-reference
10801 information. */
10802 ++processing_template_decl;
10804 if (friend_type != error_mark_node)
10805 make_friend_class (type, friend_type, /*complain=*/false);
10807 if (adjust_processing_template_decl)
10808 --processing_template_decl;
10810 else
10812 /* Build new DECL_FRIENDLIST. */
10813 tree r;
10815 /* The file and line for this declaration, to
10816 assist in error message reporting. Since we
10817 called push_tinst_level above, we don't need to
10818 restore these. */
10819 input_location = DECL_SOURCE_LOCATION (t);
10821 if (TREE_CODE (t) == TEMPLATE_DECL)
10823 ++processing_template_decl;
10824 push_deferring_access_checks (dk_no_check);
10827 r = tsubst_friend_function (t, args);
10828 add_friend (type, r, /*complain=*/false);
10829 if (TREE_CODE (t) == TEMPLATE_DECL)
10831 pop_deferring_access_checks ();
10832 --processing_template_decl;
10838 if (fn_context)
10840 /* Restore these before substituting into the lambda capture
10841 initializers. */
10842 cp_unevaluated_operand = saved_unevaluated_operand;
10843 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10846 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10848 tree decl = lambda_function (type);
10849 if (decl)
10851 if (cxx_dialect >= cxx1z)
10852 CLASSTYPE_LITERAL_P (type) = true;
10854 if (!DECL_TEMPLATE_INFO (decl)
10855 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10857 /* Set function_depth to avoid garbage collection. */
10858 ++function_depth;
10859 instantiate_decl (decl, /*defer_ok=*/false, false);
10860 --function_depth;
10863 /* We need to instantiate the capture list from the template
10864 after we've instantiated the closure members, but before we
10865 consider adding the conversion op. Also keep any captures
10866 that may have been added during instantiation of the op(). */
10867 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10868 tree tmpl_cap
10869 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10870 args, tf_warning_or_error, NULL_TREE,
10871 false, false);
10873 LAMBDA_EXPR_CAPTURE_LIST (expr)
10874 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10876 maybe_add_lambda_conv_op (type);
10878 else
10879 gcc_assert (errorcount);
10882 /* Set the file and line number information to whatever is given for
10883 the class itself. This puts error messages involving generated
10884 implicit functions at a predictable point, and the same point
10885 that would be used for non-template classes. */
10886 input_location = DECL_SOURCE_LOCATION (typedecl);
10888 unreverse_member_declarations (type);
10889 finish_struct_1 (type);
10890 TYPE_BEING_DEFINED (type) = 0;
10892 /* We don't instantiate default arguments for member functions. 14.7.1:
10894 The implicit instantiation of a class template specialization causes
10895 the implicit instantiation of the declarations, but not of the
10896 definitions or default arguments, of the class member functions,
10897 member classes, static data members and member templates.... */
10899 /* Some typedefs referenced from within the template code need to be access
10900 checked at template instantiation time, i.e now. These types were
10901 added to the template at parsing time. Let's get those and perform
10902 the access checks then. */
10903 perform_typedefs_access_check (pattern, args);
10904 perform_deferred_access_checks (tf_warning_or_error);
10905 pop_nested_class ();
10906 maximum_field_alignment = saved_maximum_field_alignment;
10907 if (!fn_context)
10908 pop_from_top_level ();
10909 pop_deferring_access_checks ();
10910 pop_tinst_level ();
10912 /* The vtable for a template class can be emitted in any translation
10913 unit in which the class is instantiated. When there is no key
10914 method, however, finish_struct_1 will already have added TYPE to
10915 the keyed_classes. */
10916 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10917 vec_safe_push (keyed_classes, type);
10919 return type;
10922 /* Wrapper for instantiate_class_template_1. */
10924 tree
10925 instantiate_class_template (tree type)
10927 tree ret;
10928 timevar_push (TV_TEMPLATE_INST);
10929 ret = instantiate_class_template_1 (type);
10930 timevar_pop (TV_TEMPLATE_INST);
10931 return ret;
10934 static tree
10935 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10937 tree r;
10939 if (!t)
10940 r = t;
10941 else if (TYPE_P (t))
10942 r = tsubst (t, args, complain, in_decl);
10943 else
10945 if (!(complain & tf_warning))
10946 ++c_inhibit_evaluation_warnings;
10947 r = tsubst_expr (t, args, complain, in_decl,
10948 /*integral_constant_expression_p=*/true);
10949 if (!(complain & tf_warning))
10950 --c_inhibit_evaluation_warnings;
10952 return r;
10955 /* Given a function parameter pack TMPL_PARM and some function parameters
10956 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10957 and set *SPEC_P to point at the next point in the list. */
10959 tree
10960 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10962 /* Collect all of the extra "packed" parameters into an
10963 argument pack. */
10964 tree parmvec;
10965 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10966 tree spec_parm = *spec_p;
10967 int i, len;
10969 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10970 if (tmpl_parm
10971 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10972 break;
10974 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10975 parmvec = make_tree_vec (len);
10976 spec_parm = *spec_p;
10977 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10978 TREE_VEC_ELT (parmvec, i) = spec_parm;
10980 /* Build the argument packs. */
10981 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10982 *spec_p = spec_parm;
10984 return argpack;
10987 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10988 NONTYPE_ARGUMENT_PACK. */
10990 static tree
10991 make_fnparm_pack (tree spec_parm)
10993 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10996 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10997 pack expansion with no extra args, 2 if it has extra args, or 0
10998 if it is not a pack expansion. */
11000 static int
11001 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11003 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11004 if (i >= TREE_VEC_LENGTH (vec))
11005 return 0;
11006 tree elt = TREE_VEC_ELT (vec, i);
11007 if (DECL_P (elt))
11008 /* A decl pack is itself an expansion. */
11009 elt = TREE_TYPE (elt);
11010 if (!PACK_EXPANSION_P (elt))
11011 return 0;
11012 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11013 return 2;
11014 return 1;
11018 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11020 static tree
11021 make_argument_pack_select (tree arg_pack, unsigned index)
11023 tree aps = make_node (ARGUMENT_PACK_SELECT);
11025 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11026 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11028 return aps;
11031 /* This is a subroutine of tsubst_pack_expansion.
11033 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11034 mechanism to store the (non complete list of) arguments of the
11035 substitution and return a non substituted pack expansion, in order
11036 to wait for when we have enough arguments to really perform the
11037 substitution. */
11039 static bool
11040 use_pack_expansion_extra_args_p (tree parm_packs,
11041 int arg_pack_len,
11042 bool has_empty_arg)
11044 /* If one pack has an expansion and another pack has a normal
11045 argument or if one pack has an empty argument and an another
11046 one hasn't then tsubst_pack_expansion cannot perform the
11047 substitution and need to fall back on the
11048 PACK_EXPANSION_EXTRA mechanism. */
11049 if (parm_packs == NULL_TREE)
11050 return false;
11051 else if (has_empty_arg)
11052 return true;
11054 bool has_expansion_arg = false;
11055 for (int i = 0 ; i < arg_pack_len; ++i)
11057 bool has_non_expansion_arg = false;
11058 for (tree parm_pack = parm_packs;
11059 parm_pack;
11060 parm_pack = TREE_CHAIN (parm_pack))
11062 tree arg = TREE_VALUE (parm_pack);
11064 int exp = argument_pack_element_is_expansion_p (arg, i);
11065 if (exp == 2)
11066 /* We can't substitute a pack expansion with extra args into
11067 our pattern. */
11068 return true;
11069 else if (exp)
11070 has_expansion_arg = true;
11071 else
11072 has_non_expansion_arg = true;
11075 if (has_expansion_arg && has_non_expansion_arg)
11076 return true;
11078 return false;
11081 /* [temp.variadic]/6 says that:
11083 The instantiation of a pack expansion [...]
11084 produces a list E1,E2, ..., En, where N is the number of elements
11085 in the pack expansion parameters.
11087 This subroutine of tsubst_pack_expansion produces one of these Ei.
11089 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11090 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11091 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11092 INDEX is the index 'i' of the element Ei to produce. ARGS,
11093 COMPLAIN, and IN_DECL are the same parameters as for the
11094 tsubst_pack_expansion function.
11096 The function returns the resulting Ei upon successful completion,
11097 or error_mark_node.
11099 Note that this function possibly modifies the ARGS parameter, so
11100 it's the responsibility of the caller to restore it. */
11102 static tree
11103 gen_elem_of_pack_expansion_instantiation (tree pattern,
11104 tree parm_packs,
11105 unsigned index,
11106 tree args /* This parm gets
11107 modified. */,
11108 tsubst_flags_t complain,
11109 tree in_decl)
11111 tree t;
11112 bool ith_elem_is_expansion = false;
11114 /* For each parameter pack, change the substitution of the parameter
11115 pack to the ith argument in its argument pack, then expand the
11116 pattern. */
11117 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11119 tree parm = TREE_PURPOSE (pack);
11120 tree arg_pack = TREE_VALUE (pack);
11121 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11123 ith_elem_is_expansion |=
11124 argument_pack_element_is_expansion_p (arg_pack, index);
11126 /* Select the Ith argument from the pack. */
11127 if (TREE_CODE (parm) == PARM_DECL
11128 || TREE_CODE (parm) == FIELD_DECL)
11130 if (index == 0)
11132 aps = make_argument_pack_select (arg_pack, index);
11133 if (!mark_used (parm, complain) && !(complain & tf_error))
11134 return error_mark_node;
11135 register_local_specialization (aps, parm);
11137 else
11138 aps = retrieve_local_specialization (parm);
11140 else
11142 int idx, level;
11143 template_parm_level_and_index (parm, &level, &idx);
11145 if (index == 0)
11147 aps = make_argument_pack_select (arg_pack, index);
11148 /* Update the corresponding argument. */
11149 TMPL_ARG (args, level, idx) = aps;
11151 else
11152 /* Re-use the ARGUMENT_PACK_SELECT. */
11153 aps = TMPL_ARG (args, level, idx);
11155 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11158 /* Substitute into the PATTERN with the (possibly altered)
11159 arguments. */
11160 if (pattern == in_decl)
11161 /* Expanding a fixed parameter pack from
11162 coerce_template_parameter_pack. */
11163 t = tsubst_decl (pattern, args, complain);
11164 else if (pattern == error_mark_node)
11165 t = error_mark_node;
11166 else if (constraint_p (pattern))
11168 if (processing_template_decl)
11169 t = tsubst_constraint (pattern, args, complain, in_decl);
11170 else
11171 t = (constraints_satisfied_p (pattern, args)
11172 ? boolean_true_node : boolean_false_node);
11174 else if (!TYPE_P (pattern))
11175 t = tsubst_expr (pattern, args, complain, in_decl,
11176 /*integral_constant_expression_p=*/false);
11177 else
11178 t = tsubst (pattern, args, complain, in_decl);
11180 /* If the Ith argument pack element is a pack expansion, then
11181 the Ith element resulting from the substituting is going to
11182 be a pack expansion as well. */
11183 if (ith_elem_is_expansion)
11184 t = make_pack_expansion (t);
11186 return t;
11189 /* When the unexpanded parameter pack in a fold expression expands to an empty
11190 sequence, the value of the expression is as follows; the program is
11191 ill-formed if the operator is not listed in this table.
11193 && true
11194 || false
11195 , void() */
11197 tree
11198 expand_empty_fold (tree t, tsubst_flags_t complain)
11200 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11201 if (!FOLD_EXPR_MODIFY_P (t))
11202 switch (code)
11204 case TRUTH_ANDIF_EXPR:
11205 return boolean_true_node;
11206 case TRUTH_ORIF_EXPR:
11207 return boolean_false_node;
11208 case COMPOUND_EXPR:
11209 return void_node;
11210 default:
11211 break;
11214 if (complain & tf_error)
11215 error_at (location_of (t),
11216 "fold of empty expansion over %O", code);
11217 return error_mark_node;
11220 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11221 form an expression that combines the two terms using the
11222 operator of T. */
11224 static tree
11225 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11227 tree op = FOLD_EXPR_OP (t);
11228 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11230 // Handle compound assignment operators.
11231 if (FOLD_EXPR_MODIFY_P (t))
11232 return build_x_modify_expr (input_location, left, code, right, complain);
11234 switch (code)
11236 case COMPOUND_EXPR:
11237 return build_x_compound_expr (input_location, left, right, complain);
11238 case DOTSTAR_EXPR:
11239 return build_m_component_ref (left, right, complain);
11240 default:
11241 return build_x_binary_op (input_location, code,
11242 left, TREE_CODE (left),
11243 right, TREE_CODE (right),
11244 /*overload=*/NULL,
11245 complain);
11249 /* Substitute ARGS into the pack of a fold expression T. */
11251 static inline tree
11252 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11254 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11257 /* Substitute ARGS into the pack of a fold expression T. */
11259 static inline tree
11260 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11262 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11265 /* Expand a PACK of arguments into a grouped as left fold.
11266 Given a pack containing elements A0, A1, ..., An and an
11267 operator @, this builds the expression:
11269 ((A0 @ A1) @ A2) ... @ An
11271 Note that PACK must not be empty.
11273 The operator is defined by the original fold expression T. */
11275 static tree
11276 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11278 tree left = TREE_VEC_ELT (pack, 0);
11279 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11281 tree right = TREE_VEC_ELT (pack, i);
11282 left = fold_expression (t, left, right, complain);
11284 return left;
11287 /* Substitute into a unary left fold expression. */
11289 static tree
11290 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11291 tree in_decl)
11293 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11294 if (pack == error_mark_node)
11295 return error_mark_node;
11296 if (PACK_EXPANSION_P (pack))
11298 tree r = copy_node (t);
11299 FOLD_EXPR_PACK (r) = pack;
11300 return r;
11302 if (TREE_VEC_LENGTH (pack) == 0)
11303 return expand_empty_fold (t, complain);
11304 else
11305 return expand_left_fold (t, pack, complain);
11308 /* Substitute into a binary left fold expression.
11310 Do ths by building a single (non-empty) vector of argumnts and
11311 building the expression from those elements. */
11313 static tree
11314 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11315 tree in_decl)
11317 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11318 if (pack == error_mark_node)
11319 return error_mark_node;
11320 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11321 if (init == error_mark_node)
11322 return error_mark_node;
11324 if (PACK_EXPANSION_P (pack))
11326 tree r = copy_node (t);
11327 FOLD_EXPR_PACK (r) = pack;
11328 FOLD_EXPR_INIT (r) = init;
11329 return r;
11332 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11333 TREE_VEC_ELT (vec, 0) = init;
11334 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11335 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11337 return expand_left_fold (t, vec, complain);
11340 /* Expand a PACK of arguments into a grouped as right fold.
11341 Given a pack containing elementns A0, A1, ..., and an
11342 operator @, this builds the expression:
11344 A0@ ... (An-2 @ (An-1 @ An))
11346 Note that PACK must not be empty.
11348 The operator is defined by the original fold expression T. */
11350 tree
11351 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11353 // Build the expression.
11354 int n = TREE_VEC_LENGTH (pack);
11355 tree right = TREE_VEC_ELT (pack, n - 1);
11356 for (--n; n != 0; --n)
11358 tree left = TREE_VEC_ELT (pack, n - 1);
11359 right = fold_expression (t, left, right, complain);
11361 return right;
11364 /* Substitute into a unary right fold expression. */
11366 static tree
11367 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11368 tree in_decl)
11370 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11371 if (pack == error_mark_node)
11372 return error_mark_node;
11373 if (PACK_EXPANSION_P (pack))
11375 tree r = copy_node (t);
11376 FOLD_EXPR_PACK (r) = pack;
11377 return r;
11379 if (TREE_VEC_LENGTH (pack) == 0)
11380 return expand_empty_fold (t, complain);
11381 else
11382 return expand_right_fold (t, pack, complain);
11385 /* Substitute into a binary right fold expression.
11387 Do ths by building a single (non-empty) vector of arguments and
11388 building the expression from those elements. */
11390 static tree
11391 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11392 tree in_decl)
11394 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11395 if (pack == error_mark_node)
11396 return error_mark_node;
11397 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11398 if (init == error_mark_node)
11399 return error_mark_node;
11401 if (PACK_EXPANSION_P (pack))
11403 tree r = copy_node (t);
11404 FOLD_EXPR_PACK (r) = pack;
11405 FOLD_EXPR_INIT (r) = init;
11406 return r;
11409 int n = TREE_VEC_LENGTH (pack);
11410 tree vec = make_tree_vec (n + 1);
11411 for (int i = 0; i < n; ++i)
11412 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11413 TREE_VEC_ELT (vec, n) = init;
11415 return expand_right_fold (t, vec, complain);
11419 /* Substitute ARGS into T, which is an pack expansion
11420 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11421 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11422 (if only a partial substitution could be performed) or
11423 ERROR_MARK_NODE if there was an error. */
11424 tree
11425 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11426 tree in_decl)
11428 tree pattern;
11429 tree pack, packs = NULL_TREE;
11430 bool unsubstituted_packs = false;
11431 int i, len = -1;
11432 tree result;
11433 hash_map<tree, tree> *saved_local_specializations = NULL;
11434 bool need_local_specializations = false;
11435 int levels;
11437 gcc_assert (PACK_EXPANSION_P (t));
11438 pattern = PACK_EXPANSION_PATTERN (t);
11440 /* Add in any args remembered from an earlier partial instantiation. */
11441 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11443 levels = TMPL_ARGS_DEPTH (args);
11445 /* Determine the argument packs that will instantiate the parameter
11446 packs used in the expansion expression. While we're at it,
11447 compute the number of arguments to be expanded and make sure it
11448 is consistent. */
11449 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11450 pack = TREE_CHAIN (pack))
11452 tree parm_pack = TREE_VALUE (pack);
11453 tree arg_pack = NULL_TREE;
11454 tree orig_arg = NULL_TREE;
11455 int level = 0;
11457 if (TREE_CODE (parm_pack) == BASES)
11459 gcc_assert (parm_pack == pattern);
11460 if (BASES_DIRECT (parm_pack))
11461 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11462 args, complain, in_decl, false));
11463 else
11464 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11465 args, complain, in_decl, false));
11467 else if (builtin_pack_call_p (parm_pack))
11469 /* ??? Support use in other patterns. */
11470 gcc_assert (parm_pack == pattern);
11471 return expand_builtin_pack_call (parm_pack, args,
11472 complain, in_decl);
11474 else if (TREE_CODE (parm_pack) == PARM_DECL)
11476 /* We know we have correct local_specializations if this
11477 expansion is at function scope, or if we're dealing with a
11478 local parameter in a requires expression; for the latter,
11479 tsubst_requires_expr set it up appropriately. */
11480 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11481 arg_pack = retrieve_local_specialization (parm_pack);
11482 else
11483 /* We can't rely on local_specializations for a parameter
11484 name used later in a function declaration (such as in a
11485 late-specified return type). Even if it exists, it might
11486 have the wrong value for a recursive call. */
11487 need_local_specializations = true;
11489 if (!arg_pack)
11491 /* This parameter pack was used in an unevaluated context. Just
11492 make a dummy decl, since it's only used for its type. */
11493 arg_pack = tsubst_decl (parm_pack, args, complain);
11494 if (arg_pack && DECL_PACK_P (arg_pack))
11495 /* Partial instantiation of the parm_pack, we can't build
11496 up an argument pack yet. */
11497 arg_pack = NULL_TREE;
11498 else
11499 arg_pack = make_fnparm_pack (arg_pack);
11502 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11503 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11504 else
11506 int idx;
11507 template_parm_level_and_index (parm_pack, &level, &idx);
11509 if (level <= levels)
11510 arg_pack = TMPL_ARG (args, level, idx);
11513 orig_arg = arg_pack;
11514 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11515 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11517 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11518 /* This can only happen if we forget to expand an argument
11519 pack somewhere else. Just return an error, silently. */
11521 result = make_tree_vec (1);
11522 TREE_VEC_ELT (result, 0) = error_mark_node;
11523 return result;
11526 if (arg_pack)
11528 int my_len =
11529 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11531 /* Don't bother trying to do a partial substitution with
11532 incomplete packs; we'll try again after deduction. */
11533 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11534 return t;
11536 if (len < 0)
11537 len = my_len;
11538 else if (len != my_len)
11540 if (!(complain & tf_error))
11541 /* Fail quietly. */;
11542 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11543 error ("mismatched argument pack lengths while expanding %qT",
11544 pattern);
11545 else
11546 error ("mismatched argument pack lengths while expanding %qE",
11547 pattern);
11548 return error_mark_node;
11551 /* Keep track of the parameter packs and their corresponding
11552 argument packs. */
11553 packs = tree_cons (parm_pack, arg_pack, packs);
11554 TREE_TYPE (packs) = orig_arg;
11556 else
11558 /* We can't substitute for this parameter pack. We use a flag as
11559 well as the missing_level counter because function parameter
11560 packs don't have a level. */
11561 gcc_assert (processing_template_decl);
11562 unsubstituted_packs = true;
11566 /* If the expansion is just T..., return the matching argument pack, unless
11567 we need to call convert_from_reference on all the elements. This is an
11568 important optimization; see c++/68422. */
11569 if (!unsubstituted_packs
11570 && TREE_PURPOSE (packs) == pattern)
11572 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11573 /* Types need no adjustment, nor does sizeof..., and if we still have
11574 some pack expansion args we won't do anything yet. */
11575 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11576 || PACK_EXPANSION_SIZEOF_P (t)
11577 || pack_expansion_args_count (args))
11578 return args;
11579 /* Also optimize expression pack expansions if we can tell that the
11580 elements won't have reference type. */
11581 tree type = TREE_TYPE (pattern);
11582 if (type && TREE_CODE (type) != REFERENCE_TYPE
11583 && !PACK_EXPANSION_P (type)
11584 && !WILDCARD_TYPE_P (type))
11585 return args;
11586 /* Otherwise use the normal path so we get convert_from_reference. */
11589 /* We cannot expand this expansion expression, because we don't have
11590 all of the argument packs we need. */
11591 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11593 /* We got some full packs, but we can't substitute them in until we
11594 have values for all the packs. So remember these until then. */
11596 t = make_pack_expansion (pattern);
11597 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11598 return t;
11600 else if (unsubstituted_packs)
11602 /* There were no real arguments, we're just replacing a parameter
11603 pack with another version of itself. Substitute into the
11604 pattern and return a PACK_EXPANSION_*. The caller will need to
11605 deal with that. */
11606 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11607 t = tsubst_expr (pattern, args, complain, in_decl,
11608 /*integral_constant_expression_p=*/false);
11609 else
11610 t = tsubst (pattern, args, complain, in_decl);
11611 t = make_pack_expansion (t);
11612 return t;
11615 gcc_assert (len >= 0);
11617 if (need_local_specializations)
11619 /* We're in a late-specified return type, so create our own local
11620 specializations map; the current map is either NULL or (in the
11621 case of recursive unification) might have bindings that we don't
11622 want to use or alter. */
11623 saved_local_specializations = local_specializations;
11624 local_specializations = new hash_map<tree, tree>;
11627 /* For each argument in each argument pack, substitute into the
11628 pattern. */
11629 result = make_tree_vec (len);
11630 tree elem_args = copy_template_args (args);
11631 for (i = 0; i < len; ++i)
11633 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11635 elem_args, complain,
11636 in_decl);
11637 TREE_VEC_ELT (result, i) = t;
11638 if (t == error_mark_node)
11640 result = error_mark_node;
11641 break;
11645 /* Update ARGS to restore the substitution from parameter packs to
11646 their argument packs. */
11647 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11649 tree parm = TREE_PURPOSE (pack);
11651 if (TREE_CODE (parm) == PARM_DECL
11652 || TREE_CODE (parm) == FIELD_DECL)
11653 register_local_specialization (TREE_TYPE (pack), parm);
11654 else
11656 int idx, level;
11658 if (TREE_VALUE (pack) == NULL_TREE)
11659 continue;
11661 template_parm_level_and_index (parm, &level, &idx);
11663 /* Update the corresponding argument. */
11664 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11665 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11666 TREE_TYPE (pack);
11667 else
11668 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11672 if (need_local_specializations)
11674 delete local_specializations;
11675 local_specializations = saved_local_specializations;
11678 /* If the dependent pack arguments were such that we end up with only a
11679 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11680 if (len == 1 && TREE_CODE (result) == TREE_VEC
11681 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11682 return TREE_VEC_ELT (result, 0);
11684 return result;
11687 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11688 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11689 parameter packs; all parms generated from a function parameter pack will
11690 have the same DECL_PARM_INDEX. */
11692 tree
11693 get_pattern_parm (tree parm, tree tmpl)
11695 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11696 tree patparm;
11698 if (DECL_ARTIFICIAL (parm))
11700 for (patparm = DECL_ARGUMENTS (pattern);
11701 patparm; patparm = DECL_CHAIN (patparm))
11702 if (DECL_ARTIFICIAL (patparm)
11703 && DECL_NAME (parm) == DECL_NAME (patparm))
11704 break;
11706 else
11708 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11709 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11710 gcc_assert (DECL_PARM_INDEX (patparm)
11711 == DECL_PARM_INDEX (parm));
11714 return patparm;
11717 /* Make an argument pack out of the TREE_VEC VEC. */
11719 static tree
11720 make_argument_pack (tree vec)
11722 tree pack;
11723 tree elt = TREE_VEC_ELT (vec, 0);
11724 if (TYPE_P (elt))
11725 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11726 else
11728 pack = make_node (NONTYPE_ARGUMENT_PACK);
11729 TREE_CONSTANT (pack) = 1;
11731 SET_ARGUMENT_PACK_ARGS (pack, vec);
11732 return pack;
11735 /* Return an exact copy of template args T that can be modified
11736 independently. */
11738 static tree
11739 copy_template_args (tree t)
11741 if (t == error_mark_node)
11742 return t;
11744 int len = TREE_VEC_LENGTH (t);
11745 tree new_vec = make_tree_vec (len);
11747 for (int i = 0; i < len; ++i)
11749 tree elt = TREE_VEC_ELT (t, i);
11750 if (elt && TREE_CODE (elt) == TREE_VEC)
11751 elt = copy_template_args (elt);
11752 TREE_VEC_ELT (new_vec, i) = elt;
11755 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11756 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11758 return new_vec;
11761 /* Substitute ARGS into the vector or list of template arguments T. */
11763 static tree
11764 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11766 tree orig_t = t;
11767 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11768 tree *elts;
11770 if (t == error_mark_node)
11771 return error_mark_node;
11773 len = TREE_VEC_LENGTH (t);
11774 elts = XALLOCAVEC (tree, len);
11776 for (i = 0; i < len; i++)
11778 tree orig_arg = TREE_VEC_ELT (t, i);
11779 tree new_arg;
11781 if (TREE_CODE (orig_arg) == TREE_VEC)
11782 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11783 else if (PACK_EXPANSION_P (orig_arg))
11785 /* Substitute into an expansion expression. */
11786 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11788 if (TREE_CODE (new_arg) == TREE_VEC)
11789 /* Add to the expanded length adjustment the number of
11790 expanded arguments. We subtract one from this
11791 measurement, because the argument pack expression
11792 itself is already counted as 1 in
11793 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11794 the argument pack is empty. */
11795 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11797 else if (ARGUMENT_PACK_P (orig_arg))
11799 /* Substitute into each of the arguments. */
11800 new_arg = TYPE_P (orig_arg)
11801 ? cxx_make_type (TREE_CODE (orig_arg))
11802 : make_node (TREE_CODE (orig_arg));
11804 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11805 args, complain, in_decl);
11806 if (pack_args == error_mark_node)
11807 new_arg = error_mark_node;
11808 else
11809 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11811 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11812 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11814 else
11815 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11817 if (new_arg == error_mark_node)
11818 return error_mark_node;
11820 elts[i] = new_arg;
11821 if (new_arg != orig_arg)
11822 need_new = 1;
11825 if (!need_new)
11826 return t;
11828 /* Make space for the expanded arguments coming from template
11829 argument packs. */
11830 t = make_tree_vec (len + expanded_len_adjust);
11831 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11832 arguments for a member template.
11833 In that case each TREE_VEC in ORIG_T represents a level of template
11834 arguments, and ORIG_T won't carry any non defaulted argument count.
11835 It will rather be the nested TREE_VECs that will carry one.
11836 In other words, ORIG_T carries a non defaulted argument count only
11837 if it doesn't contain any nested TREE_VEC. */
11838 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11840 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11841 count += expanded_len_adjust;
11842 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11844 for (i = 0, out = 0; i < len; i++)
11846 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11847 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11848 && TREE_CODE (elts[i]) == TREE_VEC)
11850 int idx;
11852 /* Now expand the template argument pack "in place". */
11853 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11854 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11856 else
11858 TREE_VEC_ELT (t, out) = elts[i];
11859 out++;
11863 return t;
11866 /* Substitute ARGS into one level PARMS of template parameters. */
11868 static tree
11869 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11871 if (parms == error_mark_node)
11872 return error_mark_node;
11874 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11876 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11878 tree tuple = TREE_VEC_ELT (parms, i);
11880 if (tuple == error_mark_node)
11881 continue;
11883 TREE_VEC_ELT (new_vec, i) =
11884 tsubst_template_parm (tuple, args, complain);
11887 return new_vec;
11890 /* Return the result of substituting ARGS into the template parameters
11891 given by PARMS. If there are m levels of ARGS and m + n levels of
11892 PARMS, then the result will contain n levels of PARMS. For
11893 example, if PARMS is `template <class T> template <class U>
11894 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11895 result will be `template <int*, double, class V>'. */
11897 static tree
11898 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11900 tree r = NULL_TREE;
11901 tree* new_parms;
11903 /* When substituting into a template, we must set
11904 PROCESSING_TEMPLATE_DECL as the template parameters may be
11905 dependent if they are based on one-another, and the dependency
11906 predicates are short-circuit outside of templates. */
11907 ++processing_template_decl;
11909 for (new_parms = &r;
11910 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11911 new_parms = &(TREE_CHAIN (*new_parms)),
11912 parms = TREE_CHAIN (parms))
11914 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11915 args, complain);
11916 *new_parms =
11917 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11918 - TMPL_ARGS_DEPTH (args)),
11919 new_vec, NULL_TREE);
11922 --processing_template_decl;
11924 return r;
11927 /* Return the result of substituting ARGS into one template parameter
11928 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11929 parameter and which TREE_PURPOSE is the default argument of the
11930 template parameter. */
11932 static tree
11933 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11935 tree default_value, parm_decl;
11937 if (args == NULL_TREE
11938 || t == NULL_TREE
11939 || t == error_mark_node)
11940 return t;
11942 gcc_assert (TREE_CODE (t) == TREE_LIST);
11944 default_value = TREE_PURPOSE (t);
11945 parm_decl = TREE_VALUE (t);
11947 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11948 if (TREE_CODE (parm_decl) == PARM_DECL
11949 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11950 parm_decl = error_mark_node;
11951 default_value = tsubst_template_arg (default_value, args,
11952 complain, NULL_TREE);
11954 return build_tree_list (default_value, parm_decl);
11957 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11958 type T. If T is not an aggregate or enumeration type, it is
11959 handled as if by tsubst. IN_DECL is as for tsubst. If
11960 ENTERING_SCOPE is nonzero, T is the context for a template which
11961 we are presently tsubst'ing. Return the substituted value. */
11963 static tree
11964 tsubst_aggr_type (tree t,
11965 tree args,
11966 tsubst_flags_t complain,
11967 tree in_decl,
11968 int entering_scope)
11970 if (t == NULL_TREE)
11971 return NULL_TREE;
11973 switch (TREE_CODE (t))
11975 case RECORD_TYPE:
11976 if (TYPE_PTRMEMFUNC_P (t))
11977 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11979 /* Fall through. */
11980 case ENUMERAL_TYPE:
11981 case UNION_TYPE:
11982 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11984 tree argvec;
11985 tree context;
11986 tree r;
11987 int saved_unevaluated_operand;
11988 int saved_inhibit_evaluation_warnings;
11990 /* In "sizeof(X<I>)" we need to evaluate "I". */
11991 saved_unevaluated_operand = cp_unevaluated_operand;
11992 cp_unevaluated_operand = 0;
11993 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11994 c_inhibit_evaluation_warnings = 0;
11996 /* First, determine the context for the type we are looking
11997 up. */
11998 context = TYPE_CONTEXT (t);
11999 if (context && TYPE_P (context))
12001 context = tsubst_aggr_type (context, args, complain,
12002 in_decl, /*entering_scope=*/1);
12003 /* If context is a nested class inside a class template,
12004 it may still need to be instantiated (c++/33959). */
12005 context = complete_type (context);
12008 /* Then, figure out what arguments are appropriate for the
12009 type we are trying to find. For example, given:
12011 template <class T> struct S;
12012 template <class T, class U> void f(T, U) { S<U> su; }
12014 and supposing that we are instantiating f<int, double>,
12015 then our ARGS will be {int, double}, but, when looking up
12016 S we only want {double}. */
12017 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12018 complain, in_decl);
12019 if (argvec == error_mark_node)
12020 r = error_mark_node;
12021 else
12023 r = lookup_template_class (t, argvec, in_decl, context,
12024 entering_scope, complain);
12025 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12028 cp_unevaluated_operand = saved_unevaluated_operand;
12029 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12031 return r;
12033 else
12034 /* This is not a template type, so there's nothing to do. */
12035 return t;
12037 default:
12038 return tsubst (t, args, complain, in_decl);
12042 static GTY(()) hash_map<tree, tree> *defarg_inst;
12044 /* Substitute into the default argument ARG (a default argument for
12045 FN), which has the indicated TYPE. */
12047 tree
12048 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12049 tsubst_flags_t complain)
12051 tree saved_class_ptr = NULL_TREE;
12052 tree saved_class_ref = NULL_TREE;
12053 int errs = errorcount + sorrycount;
12055 /* This can happen in invalid code. */
12056 if (TREE_CODE (arg) == DEFAULT_ARG)
12057 return arg;
12059 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12060 parm = chain_index (parmnum, parm);
12061 tree parmtype = TREE_TYPE (parm);
12062 if (DECL_BY_REFERENCE (parm))
12063 parmtype = TREE_TYPE (parmtype);
12064 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12066 tree *slot;
12067 if (defarg_inst && (slot = defarg_inst->get (parm)))
12068 return *slot;
12070 /* This default argument came from a template. Instantiate the
12071 default argument here, not in tsubst. In the case of
12072 something like:
12074 template <class T>
12075 struct S {
12076 static T t();
12077 void f(T = t());
12080 we must be careful to do name lookup in the scope of S<T>,
12081 rather than in the current class. */
12082 push_access_scope (fn);
12083 /* The "this" pointer is not valid in a default argument. */
12084 if (cfun)
12086 saved_class_ptr = current_class_ptr;
12087 cp_function_chain->x_current_class_ptr = NULL_TREE;
12088 saved_class_ref = current_class_ref;
12089 cp_function_chain->x_current_class_ref = NULL_TREE;
12092 start_lambda_scope (parm);
12094 push_deferring_access_checks(dk_no_deferred);
12095 /* The default argument expression may cause implicitly defined
12096 member functions to be synthesized, which will result in garbage
12097 collection. We must treat this situation as if we were within
12098 the body of function so as to avoid collecting live data on the
12099 stack. */
12100 ++function_depth;
12101 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12102 complain, NULL_TREE,
12103 /*integral_constant_expression_p=*/false);
12104 --function_depth;
12105 pop_deferring_access_checks();
12107 finish_lambda_scope ();
12109 /* Restore the "this" pointer. */
12110 if (cfun)
12112 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12113 cp_function_chain->x_current_class_ref = saved_class_ref;
12116 if (errorcount+sorrycount > errs
12117 && (complain & tf_warning_or_error))
12118 inform (input_location,
12119 " when instantiating default argument for call to %qD", fn);
12121 /* Make sure the default argument is reasonable. */
12122 arg = check_default_argument (type, arg, complain);
12124 pop_access_scope (fn);
12126 if (arg != error_mark_node && !cp_unevaluated_operand)
12128 if (!defarg_inst)
12129 defarg_inst = hash_map<tree,tree>::create_ggc (37);
12130 defarg_inst->put (parm, arg);
12133 return arg;
12136 /* Substitute into all the default arguments for FN. */
12138 static void
12139 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12141 tree arg;
12142 tree tmpl_args;
12144 tmpl_args = DECL_TI_ARGS (fn);
12146 /* If this function is not yet instantiated, we certainly don't need
12147 its default arguments. */
12148 if (uses_template_parms (tmpl_args))
12149 return;
12150 /* Don't do this again for clones. */
12151 if (DECL_CLONED_FUNCTION_P (fn))
12152 return;
12154 int i = 0;
12155 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12156 arg;
12157 arg = TREE_CHAIN (arg), ++i)
12158 if (TREE_PURPOSE (arg))
12159 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12160 TREE_VALUE (arg),
12161 TREE_PURPOSE (arg),
12162 complain);
12165 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12167 static tree
12168 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12169 tree lambda_fntype)
12171 tree gen_tmpl, argvec;
12172 hashval_t hash = 0;
12173 tree in_decl = t;
12175 /* Nobody should be tsubst'ing into non-template functions. */
12176 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12178 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12180 /* If T is not dependent, just return it. */
12181 if (!uses_template_parms (DECL_TI_ARGS (t)))
12182 return t;
12184 /* Calculate the most general template of which R is a
12185 specialization, and the complete set of arguments used to
12186 specialize R. */
12187 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12188 argvec = tsubst_template_args (DECL_TI_ARGS
12189 (DECL_TEMPLATE_RESULT
12190 (DECL_TI_TEMPLATE (t))),
12191 args, complain, in_decl);
12192 if (argvec == error_mark_node)
12193 return error_mark_node;
12195 /* Check to see if we already have this specialization. */
12196 if (!lambda_fntype)
12198 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12199 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12200 return spec;
12203 /* We can see more levels of arguments than parameters if
12204 there was a specialization of a member template, like
12205 this:
12207 template <class T> struct S { template <class U> void f(); }
12208 template <> template <class U> void S<int>::f(U);
12210 Here, we'll be substituting into the specialization,
12211 because that's where we can find the code we actually
12212 want to generate, but we'll have enough arguments for
12213 the most general template.
12215 We also deal with the peculiar case:
12217 template <class T> struct S {
12218 template <class U> friend void f();
12220 template <class U> void f() {}
12221 template S<int>;
12222 template void f<double>();
12224 Here, the ARGS for the instantiation of will be {int,
12225 double}. But, we only need as many ARGS as there are
12226 levels of template parameters in CODE_PATTERN. We are
12227 careful not to get fooled into reducing the ARGS in
12228 situations like:
12230 template <class T> struct S { template <class U> void f(U); }
12231 template <class T> template <> void S<T>::f(int) {}
12233 which we can spot because the pattern will be a
12234 specialization in this case. */
12235 int args_depth = TMPL_ARGS_DEPTH (args);
12236 int parms_depth =
12237 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12239 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12240 args = get_innermost_template_args (args, parms_depth);
12242 else
12244 /* This special case arises when we have something like this:
12246 template <class T> struct S {
12247 friend void f<int>(int, double);
12250 Here, the DECL_TI_TEMPLATE for the friend declaration
12251 will be an IDENTIFIER_NODE. We are being called from
12252 tsubst_friend_function, and we want only to create a
12253 new decl (R) with appropriate types so that we can call
12254 determine_specialization. */
12255 gen_tmpl = NULL_TREE;
12256 argvec = NULL_TREE;
12259 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12260 : NULL_TREE);
12261 tree ctx = closure ? closure : DECL_CONTEXT (t);
12262 bool member = ctx && TYPE_P (ctx);
12264 if (member && !closure)
12265 ctx = tsubst_aggr_type (ctx, args,
12266 complain, t, /*entering_scope=*/1);
12268 tree type = (lambda_fntype ? lambda_fntype
12269 : tsubst (TREE_TYPE (t), args,
12270 complain | tf_fndecl_type, in_decl));
12271 if (type == error_mark_node)
12272 return error_mark_node;
12274 /* If we hit excessive deduction depth, the type is bogus even if
12275 it isn't error_mark_node, so don't build a decl. */
12276 if (excessive_deduction_depth)
12277 return error_mark_node;
12279 /* We do NOT check for matching decls pushed separately at this
12280 point, as they may not represent instantiations of this
12281 template, and in any case are considered separate under the
12282 discrete model. */
12283 tree r = copy_decl (t);
12284 DECL_USE_TEMPLATE (r) = 0;
12285 TREE_TYPE (r) = type;
12286 /* Clear out the mangled name and RTL for the instantiation. */
12287 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12288 SET_DECL_RTL (r, NULL);
12289 /* Leave DECL_INITIAL set on deleted instantiations. */
12290 if (!DECL_DELETED_FN (r))
12291 DECL_INITIAL (r) = NULL_TREE;
12292 DECL_CONTEXT (r) = ctx;
12294 /* OpenMP UDRs have the only argument a reference to the declared
12295 type. We want to diagnose if the declared type is a reference,
12296 which is invalid, but as references to references are usually
12297 quietly merged, diagnose it here. */
12298 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12300 tree argtype
12301 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12302 argtype = tsubst (argtype, args, complain, in_decl);
12303 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12304 error_at (DECL_SOURCE_LOCATION (t),
12305 "reference type %qT in "
12306 "%<#pragma omp declare reduction%>", argtype);
12307 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12308 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12309 argtype);
12312 if (member && DECL_CONV_FN_P (r))
12313 /* Type-conversion operator. Reconstruct the name, in
12314 case it's the name of one of the template's parameters. */
12315 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12317 tree parms = DECL_ARGUMENTS (t);
12318 if (closure)
12319 parms = DECL_CHAIN (parms);
12320 parms = tsubst (parms, args, complain, t);
12321 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12322 DECL_CONTEXT (parm) = r;
12323 if (closure)
12325 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12326 DECL_CHAIN (tparm) = parms;
12327 parms = tparm;
12329 DECL_ARGUMENTS (r) = parms;
12330 DECL_RESULT (r) = NULL_TREE;
12332 TREE_STATIC (r) = 0;
12333 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12334 DECL_EXTERNAL (r) = 1;
12335 /* If this is an instantiation of a function with internal
12336 linkage, we already know what object file linkage will be
12337 assigned to the instantiation. */
12338 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12339 DECL_DEFER_OUTPUT (r) = 0;
12340 DECL_CHAIN (r) = NULL_TREE;
12341 DECL_PENDING_INLINE_INFO (r) = 0;
12342 DECL_PENDING_INLINE_P (r) = 0;
12343 DECL_SAVED_TREE (r) = NULL_TREE;
12344 DECL_STRUCT_FUNCTION (r) = NULL;
12345 TREE_USED (r) = 0;
12346 /* We'll re-clone as appropriate in instantiate_template. */
12347 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12349 /* If we aren't complaining now, return on error before we register
12350 the specialization so that we'll complain eventually. */
12351 if ((complain & tf_error) == 0
12352 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12353 && !grok_op_properties (r, /*complain=*/false))
12354 return error_mark_node;
12356 /* When instantiating a constrained member, substitute
12357 into the constraints to create a new constraint. */
12358 if (tree ci = get_constraints (t))
12359 if (member)
12361 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12362 set_constraints (r, ci);
12365 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12366 this in the special friend case mentioned above where
12367 GEN_TMPL is NULL. */
12368 if (gen_tmpl && !closure)
12370 DECL_TEMPLATE_INFO (r)
12371 = build_template_info (gen_tmpl, argvec);
12372 SET_DECL_IMPLICIT_INSTANTIATION (r);
12374 tree new_r
12375 = register_specialization (r, gen_tmpl, argvec, false, hash);
12376 if (new_r != r)
12377 /* We instantiated this while substituting into
12378 the type earlier (template/friend54.C). */
12379 return new_r;
12381 /* We're not supposed to instantiate default arguments
12382 until they are called, for a template. But, for a
12383 declaration like:
12385 template <class T> void f ()
12386 { extern void g(int i = T()); }
12388 we should do the substitution when the template is
12389 instantiated. We handle the member function case in
12390 instantiate_class_template since the default arguments
12391 might refer to other members of the class. */
12392 if (!member
12393 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12394 && !uses_template_parms (argvec))
12395 tsubst_default_arguments (r, complain);
12397 else
12398 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12400 /* Copy the list of befriending classes. */
12401 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12402 *friends;
12403 friends = &TREE_CHAIN (*friends))
12405 *friends = copy_node (*friends);
12406 TREE_VALUE (*friends)
12407 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12410 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12412 maybe_retrofit_in_chrg (r);
12413 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12414 return error_mark_node;
12415 /* If this is an instantiation of a member template, clone it.
12416 If it isn't, that'll be handled by
12417 clone_constructors_and_destructors. */
12418 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12419 clone_function_decl (r, /*update_methods=*/false);
12421 else if ((complain & tf_error) != 0
12422 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12423 && !grok_op_properties (r, /*complain=*/true))
12424 return error_mark_node;
12426 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12427 SET_DECL_FRIEND_CONTEXT (r,
12428 tsubst (DECL_FRIEND_CONTEXT (t),
12429 args, complain, in_decl));
12431 /* Possibly limit visibility based on template args. */
12432 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12433 if (DECL_VISIBILITY_SPECIFIED (t))
12435 DECL_VISIBILITY_SPECIFIED (r) = 0;
12436 DECL_ATTRIBUTES (r)
12437 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12439 determine_visibility (r);
12440 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12441 && !processing_template_decl)
12442 defaulted_late_check (r);
12444 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12445 args, complain, in_decl);
12446 return r;
12449 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12451 static tree
12452 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12453 tree lambda_fntype)
12455 /* We can get here when processing a member function template,
12456 member class template, or template template parameter. */
12457 tree decl = DECL_TEMPLATE_RESULT (t);
12458 tree in_decl = t;
12459 tree spec;
12460 tree tmpl_args;
12461 tree full_args;
12462 tree r;
12463 hashval_t hash = 0;
12465 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12467 /* Template template parameter is treated here. */
12468 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12469 if (new_type == error_mark_node)
12470 r = error_mark_node;
12471 /* If we get a real template back, return it. This can happen in
12472 the context of most_specialized_partial_spec. */
12473 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12474 r = new_type;
12475 else
12476 /* The new TEMPLATE_DECL was built in
12477 reduce_template_parm_level. */
12478 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12479 return r;
12482 if (!lambda_fntype)
12484 /* We might already have an instance of this template.
12485 The ARGS are for the surrounding class type, so the
12486 full args contain the tsubst'd args for the context,
12487 plus the innermost args from the template decl. */
12488 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12489 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12490 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12491 /* Because this is a template, the arguments will still be
12492 dependent, even after substitution. If
12493 PROCESSING_TEMPLATE_DECL is not set, the dependency
12494 predicates will short-circuit. */
12495 ++processing_template_decl;
12496 full_args = tsubst_template_args (tmpl_args, args,
12497 complain, in_decl);
12498 --processing_template_decl;
12499 if (full_args == error_mark_node)
12500 return error_mark_node;
12502 /* If this is a default template template argument,
12503 tsubst might not have changed anything. */
12504 if (full_args == tmpl_args)
12505 return t;
12507 hash = hash_tmpl_and_args (t, full_args);
12508 spec = retrieve_specialization (t, full_args, hash);
12509 if (spec != NULL_TREE)
12510 return spec;
12513 /* Make a new template decl. It will be similar to the
12514 original, but will record the current template arguments.
12515 We also create a new function declaration, which is just
12516 like the old one, but points to this new template, rather
12517 than the old one. */
12518 r = copy_decl (t);
12519 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12520 DECL_CHAIN (r) = NULL_TREE;
12522 // Build new template info linking to the original template decl.
12523 if (!lambda_fntype)
12525 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12526 SET_DECL_IMPLICIT_INSTANTIATION (r);
12528 else
12529 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12531 /* The template parameters for this new template are all the
12532 template parameters for the old template, except the
12533 outermost level of parameters. */
12534 DECL_TEMPLATE_PARMS (r)
12535 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12536 complain);
12538 if (TREE_CODE (decl) == TYPE_DECL
12539 && !TYPE_DECL_ALIAS_P (decl))
12541 tree new_type;
12542 ++processing_template_decl;
12543 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12544 --processing_template_decl;
12545 if (new_type == error_mark_node)
12546 return error_mark_node;
12548 TREE_TYPE (r) = new_type;
12549 /* For a partial specialization, we need to keep pointing to
12550 the primary template. */
12551 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12552 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12553 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12554 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12555 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12557 else
12559 tree new_decl;
12560 ++processing_template_decl;
12561 if (TREE_CODE (decl) == FUNCTION_DECL)
12562 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12563 else
12564 new_decl = tsubst (decl, args, complain, in_decl);
12565 --processing_template_decl;
12566 if (new_decl == error_mark_node)
12567 return error_mark_node;
12569 DECL_TEMPLATE_RESULT (r) = new_decl;
12570 TREE_TYPE (r) = TREE_TYPE (new_decl);
12571 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12572 if (lambda_fntype)
12574 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12575 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12577 else
12579 DECL_TI_TEMPLATE (new_decl) = r;
12580 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12584 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12585 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12587 if (PRIMARY_TEMPLATE_P (t))
12588 DECL_PRIMARY_TEMPLATE (r) = r;
12590 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12591 && !lambda_fntype)
12592 /* Record this non-type partial instantiation. */
12593 register_specialization (r, t,
12594 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12595 false, hash);
12597 return r;
12600 /* Substitute the ARGS into the T, which is a _DECL. Return the
12601 result of the substitution. Issue error and warning messages under
12602 control of COMPLAIN. */
12604 static tree
12605 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12607 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12608 location_t saved_loc;
12609 tree r = NULL_TREE;
12610 tree in_decl = t;
12611 hashval_t hash = 0;
12613 /* Set the filename and linenumber to improve error-reporting. */
12614 saved_loc = input_location;
12615 input_location = DECL_SOURCE_LOCATION (t);
12617 switch (TREE_CODE (t))
12619 case TEMPLATE_DECL:
12620 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12621 break;
12623 case FUNCTION_DECL:
12624 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12625 break;
12627 case PARM_DECL:
12629 tree type = NULL_TREE;
12630 int i, len = 1;
12631 tree expanded_types = NULL_TREE;
12632 tree prev_r = NULL_TREE;
12633 tree first_r = NULL_TREE;
12635 if (DECL_PACK_P (t))
12637 /* If there is a local specialization that isn't a
12638 parameter pack, it means that we're doing a "simple"
12639 substitution from inside tsubst_pack_expansion. Just
12640 return the local specialization (which will be a single
12641 parm). */
12642 tree spec = retrieve_local_specialization (t);
12643 if (spec
12644 && TREE_CODE (spec) == PARM_DECL
12645 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12646 RETURN (spec);
12648 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12649 the parameters in this function parameter pack. */
12650 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12651 complain, in_decl);
12652 if (TREE_CODE (expanded_types) == TREE_VEC)
12654 len = TREE_VEC_LENGTH (expanded_types);
12656 /* Zero-length parameter packs are boring. Just substitute
12657 into the chain. */
12658 if (len == 0)
12659 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12660 TREE_CHAIN (t)));
12662 else
12664 /* All we did was update the type. Make a note of that. */
12665 type = expanded_types;
12666 expanded_types = NULL_TREE;
12670 /* Loop through all of the parameters we'll build. When T is
12671 a function parameter pack, LEN is the number of expanded
12672 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12673 r = NULL_TREE;
12674 for (i = 0; i < len; ++i)
12676 prev_r = r;
12677 r = copy_node (t);
12678 if (DECL_TEMPLATE_PARM_P (t))
12679 SET_DECL_TEMPLATE_PARM_P (r);
12681 if (expanded_types)
12682 /* We're on the Ith parameter of the function parameter
12683 pack. */
12685 /* Get the Ith type. */
12686 type = TREE_VEC_ELT (expanded_types, i);
12688 /* Rename the parameter to include the index. */
12689 DECL_NAME (r)
12690 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12692 else if (!type)
12693 /* We're dealing with a normal parameter. */
12694 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12696 type = type_decays_to (type);
12697 TREE_TYPE (r) = type;
12698 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12700 if (DECL_INITIAL (r))
12702 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12703 DECL_INITIAL (r) = TREE_TYPE (r);
12704 else
12705 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12706 complain, in_decl);
12709 DECL_CONTEXT (r) = NULL_TREE;
12711 if (!DECL_TEMPLATE_PARM_P (r))
12712 DECL_ARG_TYPE (r) = type_passed_as (type);
12714 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12715 args, complain, in_decl);
12717 /* Keep track of the first new parameter we
12718 generate. That's what will be returned to the
12719 caller. */
12720 if (!first_r)
12721 first_r = r;
12723 /* Build a proper chain of parameters when substituting
12724 into a function parameter pack. */
12725 if (prev_r)
12726 DECL_CHAIN (prev_r) = r;
12729 /* If cp_unevaluated_operand is set, we're just looking for a
12730 single dummy parameter, so don't keep going. */
12731 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12732 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12733 complain, DECL_CHAIN (t));
12735 /* FIRST_R contains the start of the chain we've built. */
12736 r = first_r;
12738 break;
12740 case FIELD_DECL:
12742 tree type = NULL_TREE;
12743 tree vec = NULL_TREE;
12744 tree expanded_types = NULL_TREE;
12745 int len = 1;
12747 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12749 /* This field is a lambda capture pack. Return a TREE_VEC of
12750 the expanded fields to instantiate_class_template_1 and
12751 store them in the specializations hash table as a
12752 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12753 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12754 complain, in_decl);
12755 if (TREE_CODE (expanded_types) == TREE_VEC)
12757 len = TREE_VEC_LENGTH (expanded_types);
12758 vec = make_tree_vec (len);
12760 else
12762 /* All we did was update the type. Make a note of that. */
12763 type = expanded_types;
12764 expanded_types = NULL_TREE;
12768 for (int i = 0; i < len; ++i)
12770 r = copy_decl (t);
12771 if (expanded_types)
12773 type = TREE_VEC_ELT (expanded_types, i);
12774 DECL_NAME (r)
12775 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12777 else if (!type)
12778 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12780 if (type == error_mark_node)
12781 RETURN (error_mark_node);
12782 TREE_TYPE (r) = type;
12783 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12785 if (DECL_C_BIT_FIELD (r))
12786 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12787 non-bit-fields DECL_INITIAL is a non-static data member
12788 initializer, which gets deferred instantiation. */
12789 DECL_INITIAL (r)
12790 = tsubst_expr (DECL_INITIAL (t), args,
12791 complain, in_decl,
12792 /*integral_constant_expression_p=*/true);
12793 else if (DECL_INITIAL (t))
12795 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12796 NSDMI in perform_member_init. Still set DECL_INITIAL
12797 so that we know there is one. */
12798 DECL_INITIAL (r) = void_node;
12799 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12800 retrofit_lang_decl (r);
12801 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12803 /* We don't have to set DECL_CONTEXT here; it is set by
12804 finish_member_declaration. */
12805 DECL_CHAIN (r) = NULL_TREE;
12807 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12808 args, complain, in_decl);
12810 if (vec)
12811 TREE_VEC_ELT (vec, i) = r;
12814 if (vec)
12816 r = vec;
12817 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12818 SET_ARGUMENT_PACK_ARGS (pack, vec);
12819 register_specialization (pack, t, args, false, 0);
12822 break;
12824 case USING_DECL:
12825 /* We reach here only for member using decls. We also need to check
12826 uses_template_parms because DECL_DEPENDENT_P is not set for a
12827 using-declaration that designates a member of the current
12828 instantiation (c++/53549). */
12829 if (DECL_DEPENDENT_P (t)
12830 || uses_template_parms (USING_DECL_SCOPE (t)))
12832 tree scope = USING_DECL_SCOPE (t);
12833 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12834 if (PACK_EXPANSION_P (scope))
12836 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12837 int len = TREE_VEC_LENGTH (vec);
12838 r = make_tree_vec (len);
12839 for (int i = 0; i < len; ++i)
12841 tree escope = TREE_VEC_ELT (vec, i);
12842 tree elt = do_class_using_decl (escope, name);
12843 if (!elt)
12845 r = error_mark_node;
12846 break;
12848 else
12850 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12851 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12853 TREE_VEC_ELT (r, i) = elt;
12856 else
12858 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12859 complain, in_decl);
12860 r = do_class_using_decl (inst_scope, name);
12861 if (!r)
12862 r = error_mark_node;
12863 else
12865 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12866 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12870 else
12872 r = copy_node (t);
12873 DECL_CHAIN (r) = NULL_TREE;
12875 break;
12877 case TYPE_DECL:
12878 case VAR_DECL:
12880 tree argvec = NULL_TREE;
12881 tree gen_tmpl = NULL_TREE;
12882 tree spec;
12883 tree tmpl = NULL_TREE;
12884 tree ctx;
12885 tree type = NULL_TREE;
12886 bool local_p;
12888 if (TREE_TYPE (t) == error_mark_node)
12889 RETURN (error_mark_node);
12891 if (TREE_CODE (t) == TYPE_DECL
12892 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12894 /* If this is the canonical decl, we don't have to
12895 mess with instantiations, and often we can't (for
12896 typename, template type parms and such). Note that
12897 TYPE_NAME is not correct for the above test if
12898 we've copied the type for a typedef. */
12899 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12900 if (type == error_mark_node)
12901 RETURN (error_mark_node);
12902 r = TYPE_NAME (type);
12903 break;
12906 /* Check to see if we already have the specialization we
12907 need. */
12908 spec = NULL_TREE;
12909 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12911 /* T is a static data member or namespace-scope entity.
12912 We have to substitute into namespace-scope variables
12913 (not just variable templates) because of cases like:
12915 template <class T> void f() { extern T t; }
12917 where the entity referenced is not known until
12918 instantiation time. */
12919 local_p = false;
12920 ctx = DECL_CONTEXT (t);
12921 if (DECL_CLASS_SCOPE_P (t))
12923 ctx = tsubst_aggr_type (ctx, args,
12924 complain,
12925 in_decl, /*entering_scope=*/1);
12926 /* If CTX is unchanged, then T is in fact the
12927 specialization we want. That situation occurs when
12928 referencing a static data member within in its own
12929 class. We can use pointer equality, rather than
12930 same_type_p, because DECL_CONTEXT is always
12931 canonical... */
12932 if (ctx == DECL_CONTEXT (t)
12933 /* ... unless T is a member template; in which
12934 case our caller can be willing to create a
12935 specialization of that template represented
12936 by T. */
12937 && !(DECL_TI_TEMPLATE (t)
12938 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12939 spec = t;
12942 if (!spec)
12944 tmpl = DECL_TI_TEMPLATE (t);
12945 gen_tmpl = most_general_template (tmpl);
12946 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12947 if (argvec != error_mark_node)
12948 argvec = (coerce_innermost_template_parms
12949 (DECL_TEMPLATE_PARMS (gen_tmpl),
12950 argvec, t, complain,
12951 /*all*/true, /*defarg*/true));
12952 if (argvec == error_mark_node)
12953 RETURN (error_mark_node);
12954 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12955 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12958 else
12960 /* A local variable. */
12961 local_p = true;
12962 /* Subsequent calls to pushdecl will fill this in. */
12963 ctx = NULL_TREE;
12964 /* Unless this is a reference to a static variable from an
12965 enclosing function, in which case we need to fill it in now. */
12966 if (TREE_STATIC (t))
12968 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12969 if (fn != current_function_decl)
12970 ctx = fn;
12972 spec = retrieve_local_specialization (t);
12974 /* If we already have the specialization we need, there is
12975 nothing more to do. */
12976 if (spec)
12978 r = spec;
12979 break;
12982 /* Create a new node for the specialization we need. */
12983 r = copy_decl (t);
12984 if (type == NULL_TREE)
12986 if (is_typedef_decl (t))
12987 type = DECL_ORIGINAL_TYPE (t);
12988 else
12989 type = TREE_TYPE (t);
12990 if (VAR_P (t)
12991 && VAR_HAD_UNKNOWN_BOUND (t)
12992 && type != error_mark_node)
12993 type = strip_array_domain (type);
12994 tree auto_node = type_uses_auto (type);
12995 int len = TREE_VEC_LENGTH (args);
12996 if (auto_node)
12997 /* Mask off any template args past the variable's context so we
12998 don't replace the auto with an unrelated argument. */
12999 TREE_VEC_LENGTH (args) = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13000 type = tsubst (type, args, complain, in_decl);
13001 if (auto_node)
13002 TREE_VEC_LENGTH (args) = len;
13004 if (VAR_P (r))
13006 /* Even if the original location is out of scope, the
13007 newly substituted one is not. */
13008 DECL_DEAD_FOR_LOCAL (r) = 0;
13009 DECL_INITIALIZED_P (r) = 0;
13010 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13011 if (type == error_mark_node)
13012 RETURN (error_mark_node);
13013 if (TREE_CODE (type) == FUNCTION_TYPE)
13015 /* It may seem that this case cannot occur, since:
13017 typedef void f();
13018 void g() { f x; }
13020 declares a function, not a variable. However:
13022 typedef void f();
13023 template <typename T> void g() { T t; }
13024 template void g<f>();
13026 is an attempt to declare a variable with function
13027 type. */
13028 error ("variable %qD has function type",
13029 /* R is not yet sufficiently initialized, so we
13030 just use its name. */
13031 DECL_NAME (r));
13032 RETURN (error_mark_node);
13034 type = complete_type (type);
13035 /* Wait until cp_finish_decl to set this again, to handle
13036 circular dependency (template/instantiate6.C). */
13037 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13038 type = check_var_type (DECL_NAME (r), type);
13040 if (DECL_HAS_VALUE_EXPR_P (t))
13042 tree ve = DECL_VALUE_EXPR (t);
13043 ve = tsubst_expr (ve, args, complain, in_decl,
13044 /*constant_expression_p=*/false);
13045 if (REFERENCE_REF_P (ve))
13047 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13048 ve = TREE_OPERAND (ve, 0);
13050 SET_DECL_VALUE_EXPR (r, ve);
13052 if (CP_DECL_THREAD_LOCAL_P (r)
13053 && !processing_template_decl)
13054 set_decl_tls_model (r, decl_default_tls_model (r));
13056 else if (DECL_SELF_REFERENCE_P (t))
13057 SET_DECL_SELF_REFERENCE_P (r);
13058 TREE_TYPE (r) = type;
13059 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13060 DECL_CONTEXT (r) = ctx;
13061 /* Clear out the mangled name and RTL for the instantiation. */
13062 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13063 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13064 SET_DECL_RTL (r, NULL);
13065 /* The initializer must not be expanded until it is required;
13066 see [temp.inst]. */
13067 DECL_INITIAL (r) = NULL_TREE;
13068 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13069 if (VAR_P (r))
13071 SET_DECL_MODE (r, VOIDmode);
13073 /* Possibly limit visibility based on template args. */
13074 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13075 if (DECL_VISIBILITY_SPECIFIED (t))
13077 DECL_VISIBILITY_SPECIFIED (r) = 0;
13078 DECL_ATTRIBUTES (r)
13079 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13081 determine_visibility (r);
13084 if (!local_p)
13086 /* A static data member declaration is always marked
13087 external when it is declared in-class, even if an
13088 initializer is present. We mimic the non-template
13089 processing here. */
13090 DECL_EXTERNAL (r) = 1;
13091 if (DECL_NAMESPACE_SCOPE_P (t))
13092 DECL_NOT_REALLY_EXTERN (r) = 1;
13094 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13095 SET_DECL_IMPLICIT_INSTANTIATION (r);
13096 register_specialization (r, gen_tmpl, argvec, false, hash);
13098 else
13100 if (DECL_LANG_SPECIFIC (r))
13101 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13102 if (!cp_unevaluated_operand)
13103 register_local_specialization (r, t);
13106 DECL_CHAIN (r) = NULL_TREE;
13108 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13109 /*flags=*/0,
13110 args, complain, in_decl);
13112 /* Preserve a typedef that names a type. */
13113 if (is_typedef_decl (r) && type != error_mark_node)
13115 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13116 set_underlying_type (r);
13117 if (TYPE_DECL_ALIAS_P (r))
13118 /* An alias template specialization can be dependent
13119 even if its underlying type is not. */
13120 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13123 layout_decl (r, 0);
13125 break;
13127 default:
13128 gcc_unreachable ();
13130 #undef RETURN
13132 out:
13133 /* Restore the file and line information. */
13134 input_location = saved_loc;
13136 return r;
13139 /* Substitute into the ARG_TYPES of a function type.
13140 If END is a TREE_CHAIN, leave it and any following types
13141 un-substituted. */
13143 static tree
13144 tsubst_arg_types (tree arg_types,
13145 tree args,
13146 tree end,
13147 tsubst_flags_t complain,
13148 tree in_decl)
13150 tree remaining_arg_types;
13151 tree type = NULL_TREE;
13152 int i = 1;
13153 tree expanded_args = NULL_TREE;
13154 tree default_arg;
13156 if (!arg_types || arg_types == void_list_node || arg_types == end)
13157 return arg_types;
13159 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13160 args, end, complain, in_decl);
13161 if (remaining_arg_types == error_mark_node)
13162 return error_mark_node;
13164 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13166 /* For a pack expansion, perform substitution on the
13167 entire expression. Later on, we'll handle the arguments
13168 one-by-one. */
13169 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13170 args, complain, in_decl);
13172 if (TREE_CODE (expanded_args) == TREE_VEC)
13173 /* So that we'll spin through the parameters, one by one. */
13174 i = TREE_VEC_LENGTH (expanded_args);
13175 else
13177 /* We only partially substituted into the parameter
13178 pack. Our type is TYPE_PACK_EXPANSION. */
13179 type = expanded_args;
13180 expanded_args = NULL_TREE;
13184 while (i > 0) {
13185 --i;
13187 if (expanded_args)
13188 type = TREE_VEC_ELT (expanded_args, i);
13189 else if (!type)
13190 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13192 if (type == error_mark_node)
13193 return error_mark_node;
13194 if (VOID_TYPE_P (type))
13196 if (complain & tf_error)
13198 error ("invalid parameter type %qT", type);
13199 if (in_decl)
13200 error ("in declaration %q+D", in_decl);
13202 return error_mark_node;
13204 /* DR 657. */
13205 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13206 return error_mark_node;
13208 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13209 top-level qualifiers as required. */
13210 type = cv_unqualified (type_decays_to (type));
13212 /* We do not substitute into default arguments here. The standard
13213 mandates that they be instantiated only when needed, which is
13214 done in build_over_call. */
13215 default_arg = TREE_PURPOSE (arg_types);
13217 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13219 /* We've instantiated a template before its default arguments
13220 have been parsed. This can happen for a nested template
13221 class, and is not an error unless we require the default
13222 argument in a call of this function. */
13223 remaining_arg_types =
13224 tree_cons (default_arg, type, remaining_arg_types);
13225 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13227 else
13228 remaining_arg_types =
13229 hash_tree_cons (default_arg, type, remaining_arg_types);
13232 return remaining_arg_types;
13235 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13236 *not* handle the exception-specification for FNTYPE, because the
13237 initial substitution of explicitly provided template parameters
13238 during argument deduction forbids substitution into the
13239 exception-specification:
13241 [temp.deduct]
13243 All references in the function type of the function template to the
13244 corresponding template parameters are replaced by the specified tem-
13245 plate argument values. If a substitution in a template parameter or
13246 in the function type of the function template results in an invalid
13247 type, type deduction fails. [Note: The equivalent substitution in
13248 exception specifications is done only when the function is instanti-
13249 ated, at which point a program is ill-formed if the substitution
13250 results in an invalid type.] */
13252 static tree
13253 tsubst_function_type (tree t,
13254 tree args,
13255 tsubst_flags_t complain,
13256 tree in_decl)
13258 tree return_type;
13259 tree arg_types = NULL_TREE;
13260 tree fntype;
13262 /* The TYPE_CONTEXT is not used for function/method types. */
13263 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13265 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13266 failure. */
13267 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13269 if (late_return_type_p)
13271 /* Substitute the argument types. */
13272 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13273 complain, in_decl);
13274 if (arg_types == error_mark_node)
13275 return error_mark_node;
13277 tree save_ccp = current_class_ptr;
13278 tree save_ccr = current_class_ref;
13279 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13280 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13281 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13282 if (do_inject)
13284 /* DR 1207: 'this' is in scope in the trailing return type. */
13285 inject_this_parameter (this_type, cp_type_quals (this_type));
13288 /* Substitute the return type. */
13289 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13291 if (do_inject)
13293 current_class_ptr = save_ccp;
13294 current_class_ref = save_ccr;
13297 else
13298 /* Substitute the return type. */
13299 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13301 if (return_type == error_mark_node)
13302 return error_mark_node;
13303 /* DR 486 clarifies that creation of a function type with an
13304 invalid return type is a deduction failure. */
13305 if (TREE_CODE (return_type) == ARRAY_TYPE
13306 || TREE_CODE (return_type) == FUNCTION_TYPE)
13308 if (complain & tf_error)
13310 if (TREE_CODE (return_type) == ARRAY_TYPE)
13311 error ("function returning an array");
13312 else
13313 error ("function returning a function");
13315 return error_mark_node;
13317 /* And DR 657. */
13318 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13319 return error_mark_node;
13321 if (!late_return_type_p)
13323 /* Substitute the argument types. */
13324 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13325 complain, in_decl);
13326 if (arg_types == error_mark_node)
13327 return error_mark_node;
13330 /* Construct a new type node and return it. */
13331 if (TREE_CODE (t) == FUNCTION_TYPE)
13333 fntype = build_function_type (return_type, arg_types);
13334 fntype = apply_memfn_quals (fntype,
13335 type_memfn_quals (t),
13336 type_memfn_rqual (t));
13338 else
13340 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13341 /* Don't pick up extra function qualifiers from the basetype. */
13342 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13343 if (! MAYBE_CLASS_TYPE_P (r))
13345 /* [temp.deduct]
13347 Type deduction may fail for any of the following
13348 reasons:
13350 -- Attempting to create "pointer to member of T" when T
13351 is not a class type. */
13352 if (complain & tf_error)
13353 error ("creating pointer to member function of non-class type %qT",
13355 return error_mark_node;
13358 fntype = build_method_type_directly (r, return_type,
13359 TREE_CHAIN (arg_types));
13360 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13362 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13364 if (late_return_type_p)
13365 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13367 return fntype;
13370 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13371 ARGS into that specification, and return the substituted
13372 specification. If there is no specification, return NULL_TREE. */
13374 static tree
13375 tsubst_exception_specification (tree fntype,
13376 tree args,
13377 tsubst_flags_t complain,
13378 tree in_decl,
13379 bool defer_ok)
13381 tree specs;
13382 tree new_specs;
13384 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13385 new_specs = NULL_TREE;
13386 if (specs && TREE_PURPOSE (specs))
13388 /* A noexcept-specifier. */
13389 tree expr = TREE_PURPOSE (specs);
13390 if (TREE_CODE (expr) == INTEGER_CST)
13391 new_specs = expr;
13392 else if (defer_ok)
13394 /* Defer instantiation of noexcept-specifiers to avoid
13395 excessive instantiations (c++/49107). */
13396 new_specs = make_node (DEFERRED_NOEXCEPT);
13397 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13399 /* We already partially instantiated this member template,
13400 so combine the new args with the old. */
13401 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13402 = DEFERRED_NOEXCEPT_PATTERN (expr);
13403 DEFERRED_NOEXCEPT_ARGS (new_specs)
13404 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13406 else
13408 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13409 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13412 else
13413 new_specs = tsubst_copy_and_build
13414 (expr, args, complain, in_decl, /*function_p=*/false,
13415 /*integral_constant_expression_p=*/true);
13416 new_specs = build_noexcept_spec (new_specs, complain);
13418 else if (specs)
13420 if (! TREE_VALUE (specs))
13421 new_specs = specs;
13422 else
13423 while (specs)
13425 tree spec;
13426 int i, len = 1;
13427 tree expanded_specs = NULL_TREE;
13429 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13431 /* Expand the pack expansion type. */
13432 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13433 args, complain,
13434 in_decl);
13436 if (expanded_specs == error_mark_node)
13437 return error_mark_node;
13438 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13439 len = TREE_VEC_LENGTH (expanded_specs);
13440 else
13442 /* We're substituting into a member template, so
13443 we got a TYPE_PACK_EXPANSION back. Add that
13444 expansion and move on. */
13445 gcc_assert (TREE_CODE (expanded_specs)
13446 == TYPE_PACK_EXPANSION);
13447 new_specs = add_exception_specifier (new_specs,
13448 expanded_specs,
13449 complain);
13450 specs = TREE_CHAIN (specs);
13451 continue;
13455 for (i = 0; i < len; ++i)
13457 if (expanded_specs)
13458 spec = TREE_VEC_ELT (expanded_specs, i);
13459 else
13460 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13461 if (spec == error_mark_node)
13462 return spec;
13463 new_specs = add_exception_specifier (new_specs, spec,
13464 complain);
13467 specs = TREE_CHAIN (specs);
13470 return new_specs;
13473 /* Take the tree structure T and replace template parameters used
13474 therein with the argument vector ARGS. IN_DECL is an associated
13475 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13476 Issue error and warning messages under control of COMPLAIN. Note
13477 that we must be relatively non-tolerant of extensions here, in
13478 order to preserve conformance; if we allow substitutions that
13479 should not be allowed, we may allow argument deductions that should
13480 not succeed, and therefore report ambiguous overload situations
13481 where there are none. In theory, we could allow the substitution,
13482 but indicate that it should have failed, and allow our caller to
13483 make sure that the right thing happens, but we don't try to do this
13484 yet.
13486 This function is used for dealing with types, decls and the like;
13487 for expressions, use tsubst_expr or tsubst_copy. */
13489 tree
13490 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13492 enum tree_code code;
13493 tree type, r = NULL_TREE;
13495 if (t == NULL_TREE || t == error_mark_node
13496 || t == integer_type_node
13497 || t == void_type_node
13498 || t == char_type_node
13499 || t == unknown_type_node
13500 || TREE_CODE (t) == NAMESPACE_DECL
13501 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13502 return t;
13504 if (DECL_P (t))
13505 return tsubst_decl (t, args, complain);
13507 if (args == NULL_TREE)
13508 return t;
13510 code = TREE_CODE (t);
13512 if (code == IDENTIFIER_NODE)
13513 type = IDENTIFIER_TYPE_VALUE (t);
13514 else
13515 type = TREE_TYPE (t);
13517 gcc_assert (type != unknown_type_node);
13519 /* Reuse typedefs. We need to do this to handle dependent attributes,
13520 such as attribute aligned. */
13521 if (TYPE_P (t)
13522 && typedef_variant_p (t))
13524 tree decl = TYPE_NAME (t);
13526 if (alias_template_specialization_p (t))
13528 /* DECL represents an alias template and we want to
13529 instantiate it. */
13530 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13531 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13532 r = instantiate_alias_template (tmpl, gen_args, complain);
13534 else if (DECL_CLASS_SCOPE_P (decl)
13535 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13536 && uses_template_parms (DECL_CONTEXT (decl)))
13538 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13539 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13540 r = retrieve_specialization (tmpl, gen_args, 0);
13542 else if (DECL_FUNCTION_SCOPE_P (decl)
13543 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13544 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13545 r = retrieve_local_specialization (decl);
13546 else
13547 /* The typedef is from a non-template context. */
13548 return t;
13550 if (r)
13552 r = TREE_TYPE (r);
13553 r = cp_build_qualified_type_real
13554 (r, cp_type_quals (t) | cp_type_quals (r),
13555 complain | tf_ignore_bad_quals);
13556 return r;
13558 else
13560 /* We don't have an instantiation yet, so drop the typedef. */
13561 int quals = cp_type_quals (t);
13562 t = DECL_ORIGINAL_TYPE (decl);
13563 t = cp_build_qualified_type_real (t, quals,
13564 complain | tf_ignore_bad_quals);
13568 bool fndecl_type = (complain & tf_fndecl_type);
13569 complain &= ~tf_fndecl_type;
13571 if (type
13572 && code != TYPENAME_TYPE
13573 && code != TEMPLATE_TYPE_PARM
13574 && code != TEMPLATE_PARM_INDEX
13575 && code != IDENTIFIER_NODE
13576 && code != FUNCTION_TYPE
13577 && code != METHOD_TYPE)
13578 type = tsubst (type, args, complain, in_decl);
13579 if (type == error_mark_node)
13580 return error_mark_node;
13582 switch (code)
13584 case RECORD_TYPE:
13585 case UNION_TYPE:
13586 case ENUMERAL_TYPE:
13587 return tsubst_aggr_type (t, args, complain, in_decl,
13588 /*entering_scope=*/0);
13590 case ERROR_MARK:
13591 case IDENTIFIER_NODE:
13592 case VOID_TYPE:
13593 case REAL_TYPE:
13594 case COMPLEX_TYPE:
13595 case VECTOR_TYPE:
13596 case BOOLEAN_TYPE:
13597 case NULLPTR_TYPE:
13598 case LANG_TYPE:
13599 return t;
13601 case INTEGER_TYPE:
13602 if (t == integer_type_node)
13603 return t;
13605 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13606 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13607 return t;
13610 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13612 max = tsubst_expr (omax, args, complain, in_decl,
13613 /*integral_constant_expression_p=*/false);
13615 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13616 needed. */
13617 if (TREE_CODE (max) == NOP_EXPR
13618 && TREE_SIDE_EFFECTS (omax)
13619 && !TREE_TYPE (max))
13620 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13622 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13623 with TREE_SIDE_EFFECTS that indicates this is not an integral
13624 constant expression. */
13625 if (processing_template_decl
13626 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13628 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13629 TREE_SIDE_EFFECTS (max) = 1;
13632 return compute_array_index_type (NULL_TREE, max, complain);
13635 case TEMPLATE_TYPE_PARM:
13636 case TEMPLATE_TEMPLATE_PARM:
13637 case BOUND_TEMPLATE_TEMPLATE_PARM:
13638 case TEMPLATE_PARM_INDEX:
13640 int idx;
13641 int level;
13642 int levels;
13643 tree arg = NULL_TREE;
13645 /* Early in template argument deduction substitution, we don't
13646 want to reduce the level of 'auto', or it will be confused
13647 with a normal template parm in subsequent deduction. */
13648 if (is_auto (t) && (complain & tf_partial))
13649 return t;
13651 r = NULL_TREE;
13653 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13654 template_parm_level_and_index (t, &level, &idx);
13656 levels = TMPL_ARGS_DEPTH (args);
13657 if (level <= levels
13658 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13660 arg = TMPL_ARG (args, level, idx);
13662 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13664 /* See through ARGUMENT_PACK_SELECT arguments. */
13665 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13666 /* If the selected argument is an expansion E, that most
13667 likely means we were called from
13668 gen_elem_of_pack_expansion_instantiation during the
13669 substituting of pack an argument pack (which Ith
13670 element is a pack expansion, where I is
13671 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13672 In this case, the Ith element resulting from this
13673 substituting is going to be a pack expansion, which
13674 pattern is the pattern of E. Let's return the
13675 pattern of E, and
13676 gen_elem_of_pack_expansion_instantiation will
13677 build the resulting pack expansion from it. */
13678 if (PACK_EXPANSION_P (arg))
13680 /* Make sure we aren't throwing away arg info. */
13681 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13682 arg = PACK_EXPANSION_PATTERN (arg);
13687 if (arg == error_mark_node)
13688 return error_mark_node;
13689 else if (arg != NULL_TREE)
13691 if (ARGUMENT_PACK_P (arg))
13692 /* If ARG is an argument pack, we don't actually want to
13693 perform a substitution here, because substitutions
13694 for argument packs are only done
13695 element-by-element. We can get to this point when
13696 substituting the type of a non-type template
13697 parameter pack, when that type actually contains
13698 template parameter packs from an outer template, e.g.,
13700 template<typename... Types> struct A {
13701 template<Types... Values> struct B { };
13702 }; */
13703 return t;
13705 if (code == TEMPLATE_TYPE_PARM)
13707 int quals;
13708 gcc_assert (TYPE_P (arg));
13710 quals = cp_type_quals (arg) | cp_type_quals (t);
13712 return cp_build_qualified_type_real
13713 (arg, quals, complain | tf_ignore_bad_quals);
13715 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13717 /* We are processing a type constructed from a
13718 template template parameter. */
13719 tree argvec = tsubst (TYPE_TI_ARGS (t),
13720 args, complain, in_decl);
13721 if (argvec == error_mark_node)
13722 return error_mark_node;
13724 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13725 || TREE_CODE (arg) == TEMPLATE_DECL
13726 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13728 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13729 /* Consider this code:
13731 template <template <class> class Template>
13732 struct Internal {
13733 template <class Arg> using Bind = Template<Arg>;
13736 template <template <class> class Template, class Arg>
13737 using Instantiate = Template<Arg>; //#0
13739 template <template <class> class Template,
13740 class Argument>
13741 using Bind =
13742 Instantiate<Internal<Template>::template Bind,
13743 Argument>; //#1
13745 When #1 is parsed, the
13746 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13747 parameter `Template' in #0 matches the
13748 UNBOUND_CLASS_TEMPLATE representing the argument
13749 `Internal<Template>::template Bind'; We then want
13750 to assemble the type `Bind<Argument>' that can't
13751 be fully created right now, because
13752 `Internal<Template>' not being complete, the Bind
13753 template cannot be looked up in that context. So
13754 we need to "store" `Bind<Argument>' for later
13755 when the context of Bind becomes complete. Let's
13756 store that in a TYPENAME_TYPE. */
13757 return make_typename_type (TYPE_CONTEXT (arg),
13758 build_nt (TEMPLATE_ID_EXPR,
13759 TYPE_IDENTIFIER (arg),
13760 argvec),
13761 typename_type,
13762 complain);
13764 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13765 are resolving nested-types in the signature of a
13766 member function templates. Otherwise ARG is a
13767 TEMPLATE_DECL and is the real template to be
13768 instantiated. */
13769 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13770 arg = TYPE_NAME (arg);
13772 r = lookup_template_class (arg,
13773 argvec, in_decl,
13774 DECL_CONTEXT (arg),
13775 /*entering_scope=*/0,
13776 complain);
13777 return cp_build_qualified_type_real
13778 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13780 else if (code == TEMPLATE_TEMPLATE_PARM)
13781 return arg;
13782 else
13783 /* TEMPLATE_PARM_INDEX. */
13784 return convert_from_reference (unshare_expr (arg));
13787 if (level == 1)
13788 /* This can happen during the attempted tsubst'ing in
13789 unify. This means that we don't yet have any information
13790 about the template parameter in question. */
13791 return t;
13793 /* If we get here, we must have been looking at a parm for a
13794 more deeply nested template. Make a new version of this
13795 template parameter, but with a lower level. */
13796 switch (code)
13798 case TEMPLATE_TYPE_PARM:
13799 case TEMPLATE_TEMPLATE_PARM:
13800 case BOUND_TEMPLATE_TEMPLATE_PARM:
13801 if (cp_type_quals (t))
13803 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13804 r = cp_build_qualified_type_real
13805 (r, cp_type_quals (t),
13806 complain | (code == TEMPLATE_TYPE_PARM
13807 ? tf_ignore_bad_quals : 0));
13809 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13810 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13811 && (r = (TEMPLATE_PARM_DESCENDANTS
13812 (TEMPLATE_TYPE_PARM_INDEX (t))))
13813 && (r = TREE_TYPE (r))
13814 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13815 /* Break infinite recursion when substituting the constraints
13816 of a constrained placeholder. */;
13817 else
13819 r = copy_type (t);
13820 TEMPLATE_TYPE_PARM_INDEX (r)
13821 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13822 r, levels, args, complain);
13823 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13824 TYPE_MAIN_VARIANT (r) = r;
13825 TYPE_POINTER_TO (r) = NULL_TREE;
13826 TYPE_REFERENCE_TO (r) = NULL_TREE;
13828 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13830 /* Propagate constraints on placeholders. */
13831 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13832 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13833 = tsubst_constraint (constr, args, complain, in_decl);
13834 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13836 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13837 pl = tsubst (pl, args, complain, in_decl);
13838 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13842 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13843 /* We have reduced the level of the template
13844 template parameter, but not the levels of its
13845 template parameters, so canonical_type_parameter
13846 will not be able to find the canonical template
13847 template parameter for this level. Thus, we
13848 require structural equality checking to compare
13849 TEMPLATE_TEMPLATE_PARMs. */
13850 SET_TYPE_STRUCTURAL_EQUALITY (r);
13851 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13852 SET_TYPE_STRUCTURAL_EQUALITY (r);
13853 else
13854 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13856 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13858 tree tinfo = TYPE_TEMPLATE_INFO (t);
13859 /* We might need to substitute into the types of non-type
13860 template parameters. */
13861 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13862 complain, in_decl);
13863 if (tmpl == error_mark_node)
13864 return error_mark_node;
13865 tree argvec = tsubst (TI_ARGS (tinfo), args,
13866 complain, in_decl);
13867 if (argvec == error_mark_node)
13868 return error_mark_node;
13870 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13871 = build_template_info (tmpl, argvec);
13874 break;
13876 case TEMPLATE_PARM_INDEX:
13877 /* OK, now substitute the type of the non-type parameter. We
13878 couldn't do it earlier because it might be an auto parameter,
13879 and we wouldn't need to if we had an argument. */
13880 type = tsubst (type, args, complain, in_decl);
13881 if (type == error_mark_node)
13882 return error_mark_node;
13883 r = reduce_template_parm_level (t, type, levels, args, complain);
13884 break;
13886 default:
13887 gcc_unreachable ();
13890 return r;
13893 case TREE_LIST:
13895 tree purpose, value, chain;
13897 if (t == void_list_node)
13898 return t;
13900 purpose = TREE_PURPOSE (t);
13901 if (purpose)
13903 purpose = tsubst (purpose, args, complain, in_decl);
13904 if (purpose == error_mark_node)
13905 return error_mark_node;
13907 value = TREE_VALUE (t);
13908 if (value)
13910 value = tsubst (value, args, complain, in_decl);
13911 if (value == error_mark_node)
13912 return error_mark_node;
13914 chain = TREE_CHAIN (t);
13915 if (chain && chain != void_type_node)
13917 chain = tsubst (chain, args, complain, in_decl);
13918 if (chain == error_mark_node)
13919 return error_mark_node;
13921 if (purpose == TREE_PURPOSE (t)
13922 && value == TREE_VALUE (t)
13923 && chain == TREE_CHAIN (t))
13924 return t;
13925 return hash_tree_cons (purpose, value, chain);
13928 case TREE_BINFO:
13929 /* We should never be tsubsting a binfo. */
13930 gcc_unreachable ();
13932 case TREE_VEC:
13933 /* A vector of template arguments. */
13934 gcc_assert (!type);
13935 return tsubst_template_args (t, args, complain, in_decl);
13937 case POINTER_TYPE:
13938 case REFERENCE_TYPE:
13940 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13941 return t;
13943 /* [temp.deduct]
13945 Type deduction may fail for any of the following
13946 reasons:
13948 -- Attempting to create a pointer to reference type.
13949 -- Attempting to create a reference to a reference type or
13950 a reference to void.
13952 Core issue 106 says that creating a reference to a reference
13953 during instantiation is no longer a cause for failure. We
13954 only enforce this check in strict C++98 mode. */
13955 if ((TREE_CODE (type) == REFERENCE_TYPE
13956 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13957 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13959 static location_t last_loc;
13961 /* We keep track of the last time we issued this error
13962 message to avoid spewing a ton of messages during a
13963 single bad template instantiation. */
13964 if (complain & tf_error
13965 && last_loc != input_location)
13967 if (VOID_TYPE_P (type))
13968 error ("forming reference to void");
13969 else if (code == POINTER_TYPE)
13970 error ("forming pointer to reference type %qT", type);
13971 else
13972 error ("forming reference to reference type %qT", type);
13973 last_loc = input_location;
13976 return error_mark_node;
13978 else if (TREE_CODE (type) == FUNCTION_TYPE
13979 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13980 || type_memfn_rqual (type) != REF_QUAL_NONE))
13982 if (complain & tf_error)
13984 if (code == POINTER_TYPE)
13985 error ("forming pointer to qualified function type %qT",
13986 type);
13987 else
13988 error ("forming reference to qualified function type %qT",
13989 type);
13991 return error_mark_node;
13993 else if (code == POINTER_TYPE)
13995 r = build_pointer_type (type);
13996 if (TREE_CODE (type) == METHOD_TYPE)
13997 r = build_ptrmemfunc_type (r);
13999 else if (TREE_CODE (type) == REFERENCE_TYPE)
14000 /* In C++0x, during template argument substitution, when there is an
14001 attempt to create a reference to a reference type, reference
14002 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14004 "If a template-argument for a template-parameter T names a type
14005 that is a reference to a type A, an attempt to create the type
14006 'lvalue reference to cv T' creates the type 'lvalue reference to
14007 A,' while an attempt to create the type type rvalue reference to
14008 cv T' creates the type T"
14010 r = cp_build_reference_type
14011 (TREE_TYPE (type),
14012 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14013 else
14014 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14015 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14017 if (r != error_mark_node)
14018 /* Will this ever be needed for TYPE_..._TO values? */
14019 layout_type (r);
14021 return r;
14023 case OFFSET_TYPE:
14025 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14026 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14028 /* [temp.deduct]
14030 Type deduction may fail for any of the following
14031 reasons:
14033 -- Attempting to create "pointer to member of T" when T
14034 is not a class type. */
14035 if (complain & tf_error)
14036 error ("creating pointer to member of non-class type %qT", r);
14037 return error_mark_node;
14039 if (TREE_CODE (type) == REFERENCE_TYPE)
14041 if (complain & tf_error)
14042 error ("creating pointer to member reference type %qT", type);
14043 return error_mark_node;
14045 if (VOID_TYPE_P (type))
14047 if (complain & tf_error)
14048 error ("creating pointer to member of type void");
14049 return error_mark_node;
14051 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14052 if (TREE_CODE (type) == FUNCTION_TYPE)
14054 /* The type of the implicit object parameter gets its
14055 cv-qualifiers from the FUNCTION_TYPE. */
14056 tree memptr;
14057 tree method_type
14058 = build_memfn_type (type, r, type_memfn_quals (type),
14059 type_memfn_rqual (type));
14060 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14061 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14062 complain);
14064 else
14065 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14066 cp_type_quals (t),
14067 complain);
14069 case FUNCTION_TYPE:
14070 case METHOD_TYPE:
14072 tree fntype;
14073 tree specs;
14074 fntype = tsubst_function_type (t, args, complain, in_decl);
14075 if (fntype == error_mark_node)
14076 return error_mark_node;
14078 /* Substitute the exception specification. */
14079 specs = tsubst_exception_specification (t, args, complain, in_decl,
14080 /*defer_ok*/fndecl_type);
14081 if (specs == error_mark_node)
14082 return error_mark_node;
14083 if (specs)
14084 fntype = build_exception_variant (fntype, specs);
14085 return fntype;
14087 case ARRAY_TYPE:
14089 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14090 if (domain == error_mark_node)
14091 return error_mark_node;
14093 /* As an optimization, we avoid regenerating the array type if
14094 it will obviously be the same as T. */
14095 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14096 return t;
14098 /* These checks should match the ones in create_array_type_for_decl.
14100 [temp.deduct]
14102 The deduction may fail for any of the following reasons:
14104 -- Attempting to create an array with an element type that
14105 is void, a function type, or a reference type, or [DR337]
14106 an abstract class type. */
14107 if (VOID_TYPE_P (type)
14108 || TREE_CODE (type) == FUNCTION_TYPE
14109 || (TREE_CODE (type) == ARRAY_TYPE
14110 && TYPE_DOMAIN (type) == NULL_TREE)
14111 || TREE_CODE (type) == REFERENCE_TYPE)
14113 if (complain & tf_error)
14114 error ("creating array of %qT", type);
14115 return error_mark_node;
14118 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14119 return error_mark_node;
14121 r = build_cplus_array_type (type, domain);
14123 if (TYPE_USER_ALIGN (t))
14125 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14126 TYPE_USER_ALIGN (r) = 1;
14129 return r;
14132 case TYPENAME_TYPE:
14134 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14135 in_decl, /*entering_scope=*/1);
14136 if (ctx == error_mark_node)
14137 return error_mark_node;
14139 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14140 complain, in_decl);
14141 if (f == error_mark_node)
14142 return error_mark_node;
14144 if (!MAYBE_CLASS_TYPE_P (ctx))
14146 if (complain & tf_error)
14147 error ("%qT is not a class, struct, or union type", ctx);
14148 return error_mark_node;
14150 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14152 /* Normally, make_typename_type does not require that the CTX
14153 have complete type in order to allow things like:
14155 template <class T> struct S { typename S<T>::X Y; };
14157 But, such constructs have already been resolved by this
14158 point, so here CTX really should have complete type, unless
14159 it's a partial instantiation. */
14160 ctx = complete_type (ctx);
14161 if (!COMPLETE_TYPE_P (ctx))
14163 if (complain & tf_error)
14164 cxx_incomplete_type_error (NULL_TREE, ctx);
14165 return error_mark_node;
14169 f = make_typename_type (ctx, f, typename_type,
14170 complain | tf_keep_type_decl);
14171 if (f == error_mark_node)
14172 return f;
14173 if (TREE_CODE (f) == TYPE_DECL)
14175 complain |= tf_ignore_bad_quals;
14176 f = TREE_TYPE (f);
14179 if (TREE_CODE (f) != TYPENAME_TYPE)
14181 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14183 if (complain & tf_error)
14184 error ("%qT resolves to %qT, which is not an enumeration type",
14185 t, f);
14186 else
14187 return error_mark_node;
14189 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14191 if (complain & tf_error)
14192 error ("%qT resolves to %qT, which is is not a class type",
14193 t, f);
14194 else
14195 return error_mark_node;
14199 return cp_build_qualified_type_real
14200 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14203 case UNBOUND_CLASS_TEMPLATE:
14205 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14206 in_decl, /*entering_scope=*/1);
14207 tree name = TYPE_IDENTIFIER (t);
14208 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14210 if (ctx == error_mark_node || name == error_mark_node)
14211 return error_mark_node;
14213 if (parm_list)
14214 parm_list = tsubst_template_parms (parm_list, args, complain);
14215 return make_unbound_class_template (ctx, name, parm_list, complain);
14218 case TYPEOF_TYPE:
14220 tree type;
14222 ++cp_unevaluated_operand;
14223 ++c_inhibit_evaluation_warnings;
14225 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14226 complain, in_decl,
14227 /*integral_constant_expression_p=*/false);
14229 --cp_unevaluated_operand;
14230 --c_inhibit_evaluation_warnings;
14232 type = finish_typeof (type);
14233 return cp_build_qualified_type_real (type,
14234 cp_type_quals (t)
14235 | cp_type_quals (type),
14236 complain);
14239 case DECLTYPE_TYPE:
14241 tree type;
14243 ++cp_unevaluated_operand;
14244 ++c_inhibit_evaluation_warnings;
14246 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14247 complain|tf_decltype, in_decl,
14248 /*function_p*/false,
14249 /*integral_constant_expression*/false);
14251 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14253 if (type == NULL_TREE)
14255 if (complain & tf_error)
14256 error ("empty initializer in lambda init-capture");
14257 type = error_mark_node;
14259 else if (TREE_CODE (type) == TREE_LIST)
14260 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14263 --cp_unevaluated_operand;
14264 --c_inhibit_evaluation_warnings;
14266 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14267 type = lambda_capture_field_type (type,
14268 DECLTYPE_FOR_INIT_CAPTURE (t),
14269 DECLTYPE_FOR_REF_CAPTURE (t));
14270 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14271 type = lambda_proxy_type (type);
14272 else
14274 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14275 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14276 && EXPR_P (type))
14277 /* In a template ~id could be either a complement expression
14278 or an unqualified-id naming a destructor; if instantiating
14279 it produces an expression, it's not an id-expression or
14280 member access. */
14281 id = false;
14282 type = finish_decltype_type (type, id, complain);
14284 return cp_build_qualified_type_real (type,
14285 cp_type_quals (t)
14286 | cp_type_quals (type),
14287 complain | tf_ignore_bad_quals);
14290 case UNDERLYING_TYPE:
14292 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14293 complain, in_decl);
14294 return finish_underlying_type (type);
14297 case TYPE_ARGUMENT_PACK:
14298 case NONTYPE_ARGUMENT_PACK:
14300 tree r;
14302 if (code == NONTYPE_ARGUMENT_PACK)
14303 r = make_node (code);
14304 else
14305 r = cxx_make_type (code);
14307 tree pack_args = ARGUMENT_PACK_ARGS (t);
14308 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14309 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14311 return r;
14314 case VOID_CST:
14315 case INTEGER_CST:
14316 case REAL_CST:
14317 case STRING_CST:
14318 case PLUS_EXPR:
14319 case MINUS_EXPR:
14320 case NEGATE_EXPR:
14321 case NOP_EXPR:
14322 case INDIRECT_REF:
14323 case ADDR_EXPR:
14324 case CALL_EXPR:
14325 case ARRAY_REF:
14326 case SCOPE_REF:
14327 /* We should use one of the expression tsubsts for these codes. */
14328 gcc_unreachable ();
14330 default:
14331 sorry ("use of %qs in template", get_tree_code_name (code));
14332 return error_mark_node;
14336 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14337 expression on the left-hand side of the "." or "->" operator. A
14338 baselink indicates a function from a base class. Both the
14339 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14340 bases of the template class, rather than the instantiated class.
14341 In addition, lookups that were not ambiguous before may be
14342 ambiguous now. Therefore, we perform the lookup again. */
14344 static tree
14345 tsubst_baselink (tree baselink, tree object_type,
14346 tree args, tsubst_flags_t complain, tree in_decl)
14348 bool qualified = BASELINK_QUALIFIED_P (baselink);
14350 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14351 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14353 tree optype = BASELINK_OPTYPE (baselink);
14354 optype = tsubst (optype, args, complain, in_decl);
14356 tree template_args = NULL_TREE;
14357 bool template_id_p = false;
14358 tree fns = BASELINK_FUNCTIONS (baselink);
14359 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14361 template_id_p = true;
14362 template_args = TREE_OPERAND (fns, 1);
14363 fns = TREE_OPERAND (fns, 0);
14364 if (template_args)
14365 template_args = tsubst_template_args (template_args, args,
14366 complain, in_decl);
14369 tree name = OVL_NAME (fns);
14370 if (IDENTIFIER_CONV_OP_P (name))
14371 name = make_conv_op_name (optype);
14373 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14374 if (!baselink)
14376 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14377 error ("cannot call constructor %<%T::%D%> directly",
14378 qualifying_scope, name);
14379 return error_mark_node;
14382 /* If lookup found a single function, mark it as used at this point.
14383 (If it lookup found multiple functions the one selected later by
14384 overload resolution will be marked as used at that point.) */
14385 if (BASELINK_P (baselink))
14386 fns = BASELINK_FUNCTIONS (baselink);
14387 if (!template_id_p && !really_overloaded_fn (fns)
14388 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14389 return error_mark_node;
14391 if (BASELINK_P (baselink))
14393 /* Add back the template arguments, if present. */
14394 if (template_id_p)
14395 BASELINK_FUNCTIONS (baselink)
14396 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14397 BASELINK_FUNCTIONS (baselink), template_args);
14399 /* Update the conversion operator type. */
14400 BASELINK_OPTYPE (baselink) = optype;
14403 if (!object_type)
14404 object_type = current_class_type;
14406 if (qualified || name == complete_dtor_identifier)
14408 baselink = adjust_result_of_qualified_name_lookup (baselink,
14409 qualifying_scope,
14410 object_type);
14411 if (!qualified)
14412 /* We need to call adjust_result_of_qualified_name_lookup in case the
14413 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14414 so that we still get virtual function binding. */
14415 BASELINK_QUALIFIED_P (baselink) = false;
14418 return baselink;
14421 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14422 true if the qualified-id will be a postfix-expression in-and-of
14423 itself; false if more of the postfix-expression follows the
14424 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14425 of "&". */
14427 static tree
14428 tsubst_qualified_id (tree qualified_id, tree args,
14429 tsubst_flags_t complain, tree in_decl,
14430 bool done, bool address_p)
14432 tree expr;
14433 tree scope;
14434 tree name;
14435 bool is_template;
14436 tree template_args;
14437 location_t loc = UNKNOWN_LOCATION;
14439 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14441 /* Figure out what name to look up. */
14442 name = TREE_OPERAND (qualified_id, 1);
14443 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14445 is_template = true;
14446 loc = EXPR_LOCATION (name);
14447 template_args = TREE_OPERAND (name, 1);
14448 if (template_args)
14449 template_args = tsubst_template_args (template_args, args,
14450 complain, in_decl);
14451 if (template_args == error_mark_node)
14452 return error_mark_node;
14453 name = TREE_OPERAND (name, 0);
14455 else
14457 is_template = false;
14458 template_args = NULL_TREE;
14461 /* Substitute into the qualifying scope. When there are no ARGS, we
14462 are just trying to simplify a non-dependent expression. In that
14463 case the qualifying scope may be dependent, and, in any case,
14464 substituting will not help. */
14465 scope = TREE_OPERAND (qualified_id, 0);
14466 if (args)
14468 scope = tsubst (scope, args, complain, in_decl);
14469 expr = tsubst_copy (name, args, complain, in_decl);
14471 else
14472 expr = name;
14474 if (dependent_scope_p (scope))
14476 if (is_template)
14477 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14478 tree r = build_qualified_name (NULL_TREE, scope, expr,
14479 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14480 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14481 return r;
14484 if (!BASELINK_P (name) && !DECL_P (expr))
14486 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14488 /* A BIT_NOT_EXPR is used to represent a destructor. */
14489 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14491 error ("qualifying type %qT does not match destructor name ~%qT",
14492 scope, TREE_OPERAND (expr, 0));
14493 expr = error_mark_node;
14495 else
14496 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14497 /*is_type_p=*/0, false);
14499 else
14500 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14501 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14502 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14504 if (complain & tf_error)
14506 error ("dependent-name %qE is parsed as a non-type, but "
14507 "instantiation yields a type", qualified_id);
14508 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14510 return error_mark_node;
14514 if (DECL_P (expr))
14516 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14517 scope);
14518 /* Remember that there was a reference to this entity. */
14519 if (!mark_used (expr, complain) && !(complain & tf_error))
14520 return error_mark_node;
14523 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14525 if (complain & tf_error)
14526 qualified_name_lookup_error (scope,
14527 TREE_OPERAND (qualified_id, 1),
14528 expr, input_location);
14529 return error_mark_node;
14532 if (is_template)
14534 if (variable_template_p (expr))
14535 expr = lookup_and_finish_template_variable (expr, template_args,
14536 complain);
14537 else
14538 expr = lookup_template_function (expr, template_args);
14541 if (expr == error_mark_node && complain & tf_error)
14542 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14543 expr, input_location);
14544 else if (TYPE_P (scope))
14546 expr = (adjust_result_of_qualified_name_lookup
14547 (expr, scope, current_nonlambda_class_type ()));
14548 expr = (finish_qualified_id_expr
14549 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14550 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14551 /*template_arg_p=*/false, complain));
14554 /* Expressions do not generally have reference type. */
14555 if (TREE_CODE (expr) != SCOPE_REF
14556 /* However, if we're about to form a pointer-to-member, we just
14557 want the referenced member referenced. */
14558 && TREE_CODE (expr) != OFFSET_REF)
14559 expr = convert_from_reference (expr);
14561 if (REF_PARENTHESIZED_P (qualified_id))
14562 expr = force_paren_expr (expr);
14564 return expr;
14567 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14568 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14569 for tsubst. */
14571 static tree
14572 tsubst_init (tree init, tree decl, tree args,
14573 tsubst_flags_t complain, tree in_decl)
14575 if (!init)
14576 return NULL_TREE;
14578 init = tsubst_expr (init, args, complain, in_decl, false);
14580 if (!init && TREE_TYPE (decl) != error_mark_node)
14582 /* If we had an initializer but it
14583 instantiated to nothing,
14584 value-initialize the object. This will
14585 only occur when the initializer was a
14586 pack expansion where the parameter packs
14587 used in that expansion were of length
14588 zero. */
14589 init = build_value_init (TREE_TYPE (decl),
14590 complain);
14591 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14592 init = get_target_expr_sfinae (init, complain);
14593 if (TREE_CODE (init) == TARGET_EXPR)
14594 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14597 return init;
14600 /* Like tsubst, but deals with expressions. This function just replaces
14601 template parms; to finish processing the resultant expression, use
14602 tsubst_copy_and_build or tsubst_expr. */
14604 static tree
14605 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14607 enum tree_code code;
14608 tree r;
14610 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14611 return t;
14613 code = TREE_CODE (t);
14615 switch (code)
14617 case PARM_DECL:
14618 r = retrieve_local_specialization (t);
14620 if (r == NULL_TREE)
14622 /* We get here for a use of 'this' in an NSDMI. */
14623 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14624 return current_class_ptr;
14626 /* This can happen for a parameter name used later in a function
14627 declaration (such as in a late-specified return type). Just
14628 make a dummy decl, since it's only used for its type. */
14629 gcc_assert (cp_unevaluated_operand != 0);
14630 r = tsubst_decl (t, args, complain);
14631 /* Give it the template pattern as its context; its true context
14632 hasn't been instantiated yet and this is good enough for
14633 mangling. */
14634 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14637 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14638 r = ARGUMENT_PACK_SELECT_ARG (r);
14639 if (!mark_used (r, complain) && !(complain & tf_error))
14640 return error_mark_node;
14641 return r;
14643 case CONST_DECL:
14645 tree enum_type;
14646 tree v;
14648 if (DECL_TEMPLATE_PARM_P (t))
14649 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14650 /* There is no need to substitute into namespace-scope
14651 enumerators. */
14652 if (DECL_NAMESPACE_SCOPE_P (t))
14653 return t;
14654 /* If ARGS is NULL, then T is known to be non-dependent. */
14655 if (args == NULL_TREE)
14656 return scalar_constant_value (t);
14658 /* Unfortunately, we cannot just call lookup_name here.
14659 Consider:
14661 template <int I> int f() {
14662 enum E { a = I };
14663 struct S { void g() { E e = a; } };
14666 When we instantiate f<7>::S::g(), say, lookup_name is not
14667 clever enough to find f<7>::a. */
14668 enum_type
14669 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14670 /*entering_scope=*/0);
14672 for (v = TYPE_VALUES (enum_type);
14673 v != NULL_TREE;
14674 v = TREE_CHAIN (v))
14675 if (TREE_PURPOSE (v) == DECL_NAME (t))
14676 return TREE_VALUE (v);
14678 /* We didn't find the name. That should never happen; if
14679 name-lookup found it during preliminary parsing, we
14680 should find it again here during instantiation. */
14681 gcc_unreachable ();
14683 return t;
14685 case FIELD_DECL:
14686 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14688 /* Check for a local specialization set up by
14689 tsubst_pack_expansion. */
14690 if (tree r = retrieve_local_specialization (t))
14692 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14693 r = ARGUMENT_PACK_SELECT_ARG (r);
14694 return r;
14697 /* When retrieving a capture pack from a generic lambda, remove the
14698 lambda call op's own template argument list from ARGS. Only the
14699 template arguments active for the closure type should be used to
14700 retrieve the pack specialization. */
14701 if (LAMBDA_FUNCTION_P (current_function_decl)
14702 && (template_class_depth (DECL_CONTEXT (t))
14703 != TMPL_ARGS_DEPTH (args)))
14704 args = strip_innermost_template_args (args, 1);
14706 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14707 tsubst_decl put in the hash table. */
14708 return retrieve_specialization (t, args, 0);
14711 if (DECL_CONTEXT (t))
14713 tree ctx;
14715 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14716 /*entering_scope=*/1);
14717 if (ctx != DECL_CONTEXT (t))
14719 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14720 if (!r)
14722 if (complain & tf_error)
14723 error ("using invalid field %qD", t);
14724 return error_mark_node;
14726 return r;
14730 return t;
14732 case VAR_DECL:
14733 case FUNCTION_DECL:
14734 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14735 r = tsubst (t, args, complain, in_decl);
14736 else if (local_variable_p (t)
14737 && uses_template_parms (DECL_CONTEXT (t)))
14739 r = retrieve_local_specialization (t);
14740 if (r == NULL_TREE)
14742 /* First try name lookup to find the instantiation. */
14743 r = lookup_name (DECL_NAME (t));
14744 if (r && !is_capture_proxy (r))
14746 /* Make sure that the one we found is the one we want. */
14747 tree ctx = DECL_CONTEXT (t);
14748 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14749 ctx = tsubst (ctx, args, complain, in_decl);
14750 if (ctx != DECL_CONTEXT (r))
14751 r = NULL_TREE;
14754 if (r)
14755 /* OK */;
14756 else
14758 /* This can happen for a variable used in a
14759 late-specified return type of a local lambda, or for a
14760 local static or constant. Building a new VAR_DECL
14761 should be OK in all those cases. */
14762 r = tsubst_decl (t, args, complain);
14763 if (local_specializations)
14764 /* Avoid infinite recursion (79640). */
14765 register_local_specialization (r, t);
14766 if (decl_maybe_constant_var_p (r))
14768 /* We can't call cp_finish_decl, so handle the
14769 initializer by hand. */
14770 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14771 complain, in_decl);
14772 if (!processing_template_decl)
14773 init = maybe_constant_init (init);
14774 if (processing_template_decl
14775 ? potential_constant_expression (init)
14776 : reduced_constant_expression_p (init))
14777 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14778 = TREE_CONSTANT (r) = true;
14779 DECL_INITIAL (r) = init;
14780 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
14781 TREE_TYPE (r)
14782 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
14783 complain, adc_variable_type);
14785 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14786 || decl_constant_var_p (r)
14787 || errorcount || sorrycount);
14788 if (!processing_template_decl
14789 && !TREE_STATIC (r))
14790 r = process_outer_var_ref (r, complain);
14792 /* Remember this for subsequent uses. */
14793 if (local_specializations)
14794 register_local_specialization (r, t);
14797 else
14798 r = t;
14799 if (!mark_used (r, complain))
14800 return error_mark_node;
14801 return r;
14803 case NAMESPACE_DECL:
14804 return t;
14806 case OVERLOAD:
14807 /* An OVERLOAD will always be a non-dependent overload set; an
14808 overload set from function scope will just be represented with an
14809 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14810 gcc_assert (!uses_template_parms (t));
14811 /* We must have marked any lookups as persistent. */
14812 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14813 return t;
14815 case BASELINK:
14816 return tsubst_baselink (t, current_nonlambda_class_type (),
14817 args, complain, in_decl);
14819 case TEMPLATE_DECL:
14820 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14821 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14822 args, complain, in_decl);
14823 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14824 return tsubst (t, args, complain, in_decl);
14825 else if (DECL_CLASS_SCOPE_P (t)
14826 && uses_template_parms (DECL_CONTEXT (t)))
14828 /* Template template argument like the following example need
14829 special treatment:
14831 template <template <class> class TT> struct C {};
14832 template <class T> struct D {
14833 template <class U> struct E {};
14834 C<E> c; // #1
14836 D<int> d; // #2
14838 We are processing the template argument `E' in #1 for
14839 the template instantiation #2. Originally, `E' is a
14840 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14841 have to substitute this with one having context `D<int>'. */
14843 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14844 if (dependent_scope_p (context))
14846 /* When rewriting a constructor into a deduction guide, a
14847 non-dependent name can become dependent, so memtmpl<args>
14848 becomes context::template memtmpl<args>. */
14849 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14850 return build_qualified_name (type, context, DECL_NAME (t),
14851 /*template*/true);
14853 return lookup_field (context, DECL_NAME(t), 0, false);
14855 else
14856 /* Ordinary template template argument. */
14857 return t;
14859 case CAST_EXPR:
14860 case REINTERPRET_CAST_EXPR:
14861 case CONST_CAST_EXPR:
14862 case STATIC_CAST_EXPR:
14863 case DYNAMIC_CAST_EXPR:
14864 case IMPLICIT_CONV_EXPR:
14865 case CONVERT_EXPR:
14866 case NOP_EXPR:
14868 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14869 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14870 return build1 (code, type, op0);
14873 case SIZEOF_EXPR:
14874 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14875 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14877 tree expanded, op = TREE_OPERAND (t, 0);
14878 int len = 0;
14880 if (SIZEOF_EXPR_TYPE_P (t))
14881 op = TREE_TYPE (op);
14883 ++cp_unevaluated_operand;
14884 ++c_inhibit_evaluation_warnings;
14885 /* We only want to compute the number of arguments. */
14886 if (PACK_EXPANSION_P (op))
14887 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14888 else
14889 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14890 args, complain, in_decl);
14891 --cp_unevaluated_operand;
14892 --c_inhibit_evaluation_warnings;
14894 if (TREE_CODE (expanded) == TREE_VEC)
14896 len = TREE_VEC_LENGTH (expanded);
14897 /* Set TREE_USED for the benefit of -Wunused. */
14898 for (int i = 0; i < len; i++)
14899 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14900 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14903 if (expanded == error_mark_node)
14904 return error_mark_node;
14905 else if (PACK_EXPANSION_P (expanded)
14906 || (TREE_CODE (expanded) == TREE_VEC
14907 && pack_expansion_args_count (expanded)))
14910 if (PACK_EXPANSION_P (expanded))
14911 /* OK. */;
14912 else if (TREE_VEC_LENGTH (expanded) == 1)
14913 expanded = TREE_VEC_ELT (expanded, 0);
14914 else
14915 expanded = make_argument_pack (expanded);
14917 if (TYPE_P (expanded))
14918 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14919 complain & tf_error);
14920 else
14921 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14922 complain & tf_error);
14924 else
14925 return build_int_cst (size_type_node, len);
14927 if (SIZEOF_EXPR_TYPE_P (t))
14929 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14930 args, complain, in_decl);
14931 r = build1 (NOP_EXPR, r, error_mark_node);
14932 r = build1 (SIZEOF_EXPR,
14933 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14934 SIZEOF_EXPR_TYPE_P (r) = 1;
14935 return r;
14937 /* Fall through */
14939 case INDIRECT_REF:
14940 case NEGATE_EXPR:
14941 case TRUTH_NOT_EXPR:
14942 case BIT_NOT_EXPR:
14943 case ADDR_EXPR:
14944 case UNARY_PLUS_EXPR: /* Unary + */
14945 case ALIGNOF_EXPR:
14946 case AT_ENCODE_EXPR:
14947 case ARROW_EXPR:
14948 case THROW_EXPR:
14949 case TYPEID_EXPR:
14950 case REALPART_EXPR:
14951 case IMAGPART_EXPR:
14952 case PAREN_EXPR:
14954 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14955 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14956 return build1 (code, type, op0);
14959 case COMPONENT_REF:
14961 tree object;
14962 tree name;
14964 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14965 name = TREE_OPERAND (t, 1);
14966 if (TREE_CODE (name) == BIT_NOT_EXPR)
14968 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14969 complain, in_decl);
14970 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14972 else if (TREE_CODE (name) == SCOPE_REF
14973 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14975 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14976 complain, in_decl);
14977 name = TREE_OPERAND (name, 1);
14978 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14979 complain, in_decl);
14980 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14981 name = build_qualified_name (/*type=*/NULL_TREE,
14982 base, name,
14983 /*template_p=*/false);
14985 else if (BASELINK_P (name))
14986 name = tsubst_baselink (name,
14987 non_reference (TREE_TYPE (object)),
14988 args, complain,
14989 in_decl);
14990 else
14991 name = tsubst_copy (name, args, complain, in_decl);
14992 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14995 case PLUS_EXPR:
14996 case MINUS_EXPR:
14997 case MULT_EXPR:
14998 case TRUNC_DIV_EXPR:
14999 case CEIL_DIV_EXPR:
15000 case FLOOR_DIV_EXPR:
15001 case ROUND_DIV_EXPR:
15002 case EXACT_DIV_EXPR:
15003 case BIT_AND_EXPR:
15004 case BIT_IOR_EXPR:
15005 case BIT_XOR_EXPR:
15006 case TRUNC_MOD_EXPR:
15007 case FLOOR_MOD_EXPR:
15008 case TRUTH_ANDIF_EXPR:
15009 case TRUTH_ORIF_EXPR:
15010 case TRUTH_AND_EXPR:
15011 case TRUTH_OR_EXPR:
15012 case RSHIFT_EXPR:
15013 case LSHIFT_EXPR:
15014 case RROTATE_EXPR:
15015 case LROTATE_EXPR:
15016 case EQ_EXPR:
15017 case NE_EXPR:
15018 case MAX_EXPR:
15019 case MIN_EXPR:
15020 case LE_EXPR:
15021 case GE_EXPR:
15022 case LT_EXPR:
15023 case GT_EXPR:
15024 case COMPOUND_EXPR:
15025 case DOTSTAR_EXPR:
15026 case MEMBER_REF:
15027 case PREDECREMENT_EXPR:
15028 case PREINCREMENT_EXPR:
15029 case POSTDECREMENT_EXPR:
15030 case POSTINCREMENT_EXPR:
15032 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15033 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15034 return build_nt (code, op0, op1);
15037 case SCOPE_REF:
15039 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15040 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15041 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15042 QUALIFIED_NAME_IS_TEMPLATE (t));
15045 case ARRAY_REF:
15047 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15048 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15049 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15052 case CALL_EXPR:
15054 int n = VL_EXP_OPERAND_LENGTH (t);
15055 tree result = build_vl_exp (CALL_EXPR, n);
15056 int i;
15057 for (i = 0; i < n; i++)
15058 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15059 complain, in_decl);
15060 return result;
15063 case COND_EXPR:
15064 case MODOP_EXPR:
15065 case PSEUDO_DTOR_EXPR:
15066 case VEC_PERM_EXPR:
15068 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15069 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15070 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15071 r = build_nt (code, op0, op1, op2);
15072 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15073 return r;
15076 case NEW_EXPR:
15078 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15079 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15080 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15081 r = build_nt (code, op0, op1, op2);
15082 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15083 return r;
15086 case DELETE_EXPR:
15088 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15089 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15090 r = build_nt (code, op0, op1);
15091 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15092 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15093 return r;
15096 case TEMPLATE_ID_EXPR:
15098 /* Substituted template arguments */
15099 tree fn = TREE_OPERAND (t, 0);
15100 tree targs = TREE_OPERAND (t, 1);
15102 fn = tsubst_copy (fn, args, complain, in_decl);
15103 if (targs)
15104 targs = tsubst_template_args (targs, args, complain, in_decl);
15106 return lookup_template_function (fn, targs);
15109 case TREE_LIST:
15111 tree purpose, value, chain;
15113 if (t == void_list_node)
15114 return t;
15116 purpose = TREE_PURPOSE (t);
15117 if (purpose)
15118 purpose = tsubst_copy (purpose, args, complain, in_decl);
15119 value = TREE_VALUE (t);
15120 if (value)
15121 value = tsubst_copy (value, args, complain, in_decl);
15122 chain = TREE_CHAIN (t);
15123 if (chain && chain != void_type_node)
15124 chain = tsubst_copy (chain, args, complain, in_decl);
15125 if (purpose == TREE_PURPOSE (t)
15126 && value == TREE_VALUE (t)
15127 && chain == TREE_CHAIN (t))
15128 return t;
15129 return tree_cons (purpose, value, chain);
15132 case RECORD_TYPE:
15133 case UNION_TYPE:
15134 case ENUMERAL_TYPE:
15135 case INTEGER_TYPE:
15136 case TEMPLATE_TYPE_PARM:
15137 case TEMPLATE_TEMPLATE_PARM:
15138 case BOUND_TEMPLATE_TEMPLATE_PARM:
15139 case TEMPLATE_PARM_INDEX:
15140 case POINTER_TYPE:
15141 case REFERENCE_TYPE:
15142 case OFFSET_TYPE:
15143 case FUNCTION_TYPE:
15144 case METHOD_TYPE:
15145 case ARRAY_TYPE:
15146 case TYPENAME_TYPE:
15147 case UNBOUND_CLASS_TEMPLATE:
15148 case TYPEOF_TYPE:
15149 case DECLTYPE_TYPE:
15150 case TYPE_DECL:
15151 return tsubst (t, args, complain, in_decl);
15153 case USING_DECL:
15154 t = DECL_NAME (t);
15155 /* Fall through. */
15156 case IDENTIFIER_NODE:
15157 if (IDENTIFIER_CONV_OP_P (t))
15159 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15160 return make_conv_op_name (new_type);
15162 else
15163 return t;
15165 case CONSTRUCTOR:
15166 /* This is handled by tsubst_copy_and_build. */
15167 gcc_unreachable ();
15169 case VA_ARG_EXPR:
15171 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15172 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15173 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15176 case CLEANUP_POINT_EXPR:
15177 /* We shouldn't have built any of these during initial template
15178 generation. Instead, they should be built during instantiation
15179 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15180 gcc_unreachable ();
15182 case OFFSET_REF:
15184 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15185 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15186 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15187 r = build2 (code, type, op0, op1);
15188 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15189 if (!mark_used (TREE_OPERAND (r, 1), complain)
15190 && !(complain & tf_error))
15191 return error_mark_node;
15192 return r;
15195 case EXPR_PACK_EXPANSION:
15196 error ("invalid use of pack expansion expression");
15197 return error_mark_node;
15199 case NONTYPE_ARGUMENT_PACK:
15200 error ("use %<...%> to expand argument pack");
15201 return error_mark_node;
15203 case VOID_CST:
15204 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15205 return t;
15207 case INTEGER_CST:
15208 case REAL_CST:
15209 case STRING_CST:
15210 case COMPLEX_CST:
15212 /* Instantiate any typedefs in the type. */
15213 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15214 r = fold_convert (type, t);
15215 gcc_assert (TREE_CODE (r) == code);
15216 return r;
15219 case PTRMEM_CST:
15220 /* These can sometimes show up in a partial instantiation, but never
15221 involve template parms. */
15222 gcc_assert (!uses_template_parms (t));
15223 return t;
15225 case UNARY_LEFT_FOLD_EXPR:
15226 return tsubst_unary_left_fold (t, args, complain, in_decl);
15227 case UNARY_RIGHT_FOLD_EXPR:
15228 return tsubst_unary_right_fold (t, args, complain, in_decl);
15229 case BINARY_LEFT_FOLD_EXPR:
15230 return tsubst_binary_left_fold (t, args, complain, in_decl);
15231 case BINARY_RIGHT_FOLD_EXPR:
15232 return tsubst_binary_right_fold (t, args, complain, in_decl);
15233 case PREDICT_EXPR:
15234 return t;
15236 default:
15237 /* We shouldn't get here, but keep going if !flag_checking. */
15238 if (flag_checking)
15239 gcc_unreachable ();
15240 return t;
15244 /* Helper function for tsubst_omp_clauses, used for instantiation of
15245 OMP_CLAUSE_DECL of clauses. */
15247 static tree
15248 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15249 tree in_decl)
15251 if (decl == NULL_TREE)
15252 return NULL_TREE;
15254 /* Handle an OpenMP array section represented as a TREE_LIST (or
15255 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15256 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15257 TREE_LIST. We can handle it exactly the same as an array section
15258 (purpose, value, and a chain), even though the nomenclature
15259 (low_bound, length, etc) is different. */
15260 if (TREE_CODE (decl) == TREE_LIST)
15262 tree low_bound
15263 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15264 /*integral_constant_expression_p=*/false);
15265 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15266 /*integral_constant_expression_p=*/false);
15267 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15268 in_decl);
15269 if (TREE_PURPOSE (decl) == low_bound
15270 && TREE_VALUE (decl) == length
15271 && TREE_CHAIN (decl) == chain)
15272 return decl;
15273 tree ret = tree_cons (low_bound, length, chain);
15274 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15275 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15276 return ret;
15278 tree ret = tsubst_expr (decl, args, complain, in_decl,
15279 /*integral_constant_expression_p=*/false);
15280 /* Undo convert_from_reference tsubst_expr could have called. */
15281 if (decl
15282 && REFERENCE_REF_P (ret)
15283 && !REFERENCE_REF_P (decl))
15284 ret = TREE_OPERAND (ret, 0);
15285 return ret;
15288 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15290 static tree
15291 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15292 tree args, tsubst_flags_t complain, tree in_decl)
15294 tree new_clauses = NULL_TREE, nc, oc;
15295 tree linear_no_step = NULL_TREE;
15297 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15299 nc = copy_node (oc);
15300 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15301 new_clauses = nc;
15303 switch (OMP_CLAUSE_CODE (nc))
15305 case OMP_CLAUSE_LASTPRIVATE:
15306 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15308 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15309 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15310 in_decl, /*integral_constant_expression_p=*/false);
15311 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15312 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15314 /* FALLTHRU */
15315 case OMP_CLAUSE_PRIVATE:
15316 case OMP_CLAUSE_SHARED:
15317 case OMP_CLAUSE_FIRSTPRIVATE:
15318 case OMP_CLAUSE_COPYIN:
15319 case OMP_CLAUSE_COPYPRIVATE:
15320 case OMP_CLAUSE_UNIFORM:
15321 case OMP_CLAUSE_DEPEND:
15322 case OMP_CLAUSE_FROM:
15323 case OMP_CLAUSE_TO:
15324 case OMP_CLAUSE_MAP:
15325 case OMP_CLAUSE_USE_DEVICE_PTR:
15326 case OMP_CLAUSE_IS_DEVICE_PTR:
15327 OMP_CLAUSE_DECL (nc)
15328 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15329 in_decl);
15330 break;
15331 case OMP_CLAUSE_TILE:
15332 case OMP_CLAUSE_IF:
15333 case OMP_CLAUSE_NUM_THREADS:
15334 case OMP_CLAUSE_SCHEDULE:
15335 case OMP_CLAUSE_COLLAPSE:
15336 case OMP_CLAUSE_FINAL:
15337 case OMP_CLAUSE_DEVICE:
15338 case OMP_CLAUSE_DIST_SCHEDULE:
15339 case OMP_CLAUSE_NUM_TEAMS:
15340 case OMP_CLAUSE_THREAD_LIMIT:
15341 case OMP_CLAUSE_SAFELEN:
15342 case OMP_CLAUSE_SIMDLEN:
15343 case OMP_CLAUSE_NUM_TASKS:
15344 case OMP_CLAUSE_GRAINSIZE:
15345 case OMP_CLAUSE_PRIORITY:
15346 case OMP_CLAUSE_ORDERED:
15347 case OMP_CLAUSE_HINT:
15348 case OMP_CLAUSE_NUM_GANGS:
15349 case OMP_CLAUSE_NUM_WORKERS:
15350 case OMP_CLAUSE_VECTOR_LENGTH:
15351 case OMP_CLAUSE_WORKER:
15352 case OMP_CLAUSE_VECTOR:
15353 case OMP_CLAUSE_ASYNC:
15354 case OMP_CLAUSE_WAIT:
15355 OMP_CLAUSE_OPERAND (nc, 0)
15356 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15357 in_decl, /*integral_constant_expression_p=*/false);
15358 break;
15359 case OMP_CLAUSE_REDUCTION:
15360 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15362 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15363 if (TREE_CODE (placeholder) == SCOPE_REF)
15365 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15366 complain, in_decl);
15367 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15368 = build_qualified_name (NULL_TREE, scope,
15369 TREE_OPERAND (placeholder, 1),
15370 false);
15372 else
15373 gcc_assert (identifier_p (placeholder));
15375 OMP_CLAUSE_DECL (nc)
15376 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15377 in_decl);
15378 break;
15379 case OMP_CLAUSE_GANG:
15380 case OMP_CLAUSE_ALIGNED:
15381 OMP_CLAUSE_DECL (nc)
15382 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15383 in_decl);
15384 OMP_CLAUSE_OPERAND (nc, 1)
15385 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15386 in_decl, /*integral_constant_expression_p=*/false);
15387 break;
15388 case OMP_CLAUSE_LINEAR:
15389 OMP_CLAUSE_DECL (nc)
15390 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15391 in_decl);
15392 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15394 gcc_assert (!linear_no_step);
15395 linear_no_step = nc;
15397 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15398 OMP_CLAUSE_LINEAR_STEP (nc)
15399 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15400 complain, in_decl);
15401 else
15402 OMP_CLAUSE_LINEAR_STEP (nc)
15403 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15404 in_decl,
15405 /*integral_constant_expression_p=*/false);
15406 break;
15407 case OMP_CLAUSE_NOWAIT:
15408 case OMP_CLAUSE_DEFAULT:
15409 case OMP_CLAUSE_UNTIED:
15410 case OMP_CLAUSE_MERGEABLE:
15411 case OMP_CLAUSE_INBRANCH:
15412 case OMP_CLAUSE_NOTINBRANCH:
15413 case OMP_CLAUSE_PROC_BIND:
15414 case OMP_CLAUSE_FOR:
15415 case OMP_CLAUSE_PARALLEL:
15416 case OMP_CLAUSE_SECTIONS:
15417 case OMP_CLAUSE_TASKGROUP:
15418 case OMP_CLAUSE_NOGROUP:
15419 case OMP_CLAUSE_THREADS:
15420 case OMP_CLAUSE_SIMD:
15421 case OMP_CLAUSE_DEFAULTMAP:
15422 case OMP_CLAUSE_INDEPENDENT:
15423 case OMP_CLAUSE_AUTO:
15424 case OMP_CLAUSE_SEQ:
15425 break;
15426 default:
15427 gcc_unreachable ();
15429 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15430 switch (OMP_CLAUSE_CODE (nc))
15432 case OMP_CLAUSE_SHARED:
15433 case OMP_CLAUSE_PRIVATE:
15434 case OMP_CLAUSE_FIRSTPRIVATE:
15435 case OMP_CLAUSE_LASTPRIVATE:
15436 case OMP_CLAUSE_COPYPRIVATE:
15437 case OMP_CLAUSE_LINEAR:
15438 case OMP_CLAUSE_REDUCTION:
15439 case OMP_CLAUSE_USE_DEVICE_PTR:
15440 case OMP_CLAUSE_IS_DEVICE_PTR:
15441 /* tsubst_expr on SCOPE_REF results in returning
15442 finish_non_static_data_member result. Undo that here. */
15443 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15444 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15445 == IDENTIFIER_NODE))
15447 tree t = OMP_CLAUSE_DECL (nc);
15448 tree v = t;
15449 while (v)
15450 switch (TREE_CODE (v))
15452 case COMPONENT_REF:
15453 case MEM_REF:
15454 case INDIRECT_REF:
15455 CASE_CONVERT:
15456 case POINTER_PLUS_EXPR:
15457 v = TREE_OPERAND (v, 0);
15458 continue;
15459 case PARM_DECL:
15460 if (DECL_CONTEXT (v) == current_function_decl
15461 && DECL_ARTIFICIAL (v)
15462 && DECL_NAME (v) == this_identifier)
15463 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15464 /* FALLTHRU */
15465 default:
15466 v = NULL_TREE;
15467 break;
15470 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15471 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15472 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15473 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15474 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15476 tree decl = OMP_CLAUSE_DECL (nc);
15477 if (VAR_P (decl))
15479 retrofit_lang_decl (decl);
15480 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15483 break;
15484 default:
15485 break;
15489 new_clauses = nreverse (new_clauses);
15490 if (ort != C_ORT_OMP_DECLARE_SIMD)
15492 new_clauses = finish_omp_clauses (new_clauses, ort);
15493 if (linear_no_step)
15494 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15495 if (nc == linear_no_step)
15497 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15498 break;
15501 return new_clauses;
15504 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15506 static tree
15507 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15508 tree in_decl)
15510 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15512 tree purpose, value, chain;
15514 if (t == NULL)
15515 return t;
15517 if (TREE_CODE (t) != TREE_LIST)
15518 return tsubst_copy_and_build (t, args, complain, in_decl,
15519 /*function_p=*/false,
15520 /*integral_constant_expression_p=*/false);
15522 if (t == void_list_node)
15523 return t;
15525 purpose = TREE_PURPOSE (t);
15526 if (purpose)
15527 purpose = RECUR (purpose);
15528 value = TREE_VALUE (t);
15529 if (value)
15531 if (TREE_CODE (value) != LABEL_DECL)
15532 value = RECUR (value);
15533 else
15535 value = lookup_label (DECL_NAME (value));
15536 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15537 TREE_USED (value) = 1;
15540 chain = TREE_CHAIN (t);
15541 if (chain && chain != void_type_node)
15542 chain = RECUR (chain);
15543 return tree_cons (purpose, value, chain);
15544 #undef RECUR
15547 /* Used to temporarily communicate the list of #pragma omp parallel
15548 clauses to #pragma omp for instantiation if they are combined
15549 together. */
15551 static tree *omp_parallel_combined_clauses;
15553 /* Substitute one OMP_FOR iterator. */
15555 static void
15556 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15557 tree initv, tree condv, tree incrv, tree *clauses,
15558 tree args, tsubst_flags_t complain, tree in_decl,
15559 bool integral_constant_expression_p)
15561 #define RECUR(NODE) \
15562 tsubst_expr ((NODE), args, complain, in_decl, \
15563 integral_constant_expression_p)
15564 tree decl, init, cond, incr;
15566 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15567 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15569 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15571 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15572 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15575 decl = TREE_OPERAND (init, 0);
15576 init = TREE_OPERAND (init, 1);
15577 tree decl_expr = NULL_TREE;
15578 if (init && TREE_CODE (init) == DECL_EXPR)
15580 /* We need to jump through some hoops to handle declarations in the
15581 init-statement, since we might need to handle auto deduction,
15582 but we need to keep control of initialization. */
15583 decl_expr = init;
15584 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15585 decl = tsubst_decl (decl, args, complain);
15587 else
15589 if (TREE_CODE (decl) == SCOPE_REF)
15591 decl = RECUR (decl);
15592 if (TREE_CODE (decl) == COMPONENT_REF)
15594 tree v = decl;
15595 while (v)
15596 switch (TREE_CODE (v))
15598 case COMPONENT_REF:
15599 case MEM_REF:
15600 case INDIRECT_REF:
15601 CASE_CONVERT:
15602 case POINTER_PLUS_EXPR:
15603 v = TREE_OPERAND (v, 0);
15604 continue;
15605 case PARM_DECL:
15606 if (DECL_CONTEXT (v) == current_function_decl
15607 && DECL_ARTIFICIAL (v)
15608 && DECL_NAME (v) == this_identifier)
15610 decl = TREE_OPERAND (decl, 1);
15611 decl = omp_privatize_field (decl, false);
15613 /* FALLTHRU */
15614 default:
15615 v = NULL_TREE;
15616 break;
15620 else
15621 decl = RECUR (decl);
15623 init = RECUR (init);
15625 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15626 if (auto_node && init)
15627 TREE_TYPE (decl)
15628 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15630 gcc_assert (!type_dependent_expression_p (decl));
15632 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15634 if (decl_expr)
15636 /* Declare the variable, but don't let that initialize it. */
15637 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15638 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15639 RECUR (decl_expr);
15640 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15643 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15644 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15645 if (TREE_CODE (incr) == MODIFY_EXPR)
15647 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15648 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15649 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15650 NOP_EXPR, rhs, complain);
15652 else
15653 incr = RECUR (incr);
15654 TREE_VEC_ELT (declv, i) = decl;
15655 TREE_VEC_ELT (initv, i) = init;
15656 TREE_VEC_ELT (condv, i) = cond;
15657 TREE_VEC_ELT (incrv, i) = incr;
15658 return;
15661 if (decl_expr)
15663 /* Declare and initialize the variable. */
15664 RECUR (decl_expr);
15665 init = NULL_TREE;
15667 else if (init)
15669 tree *pc;
15670 int j;
15671 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15673 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15675 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15676 && OMP_CLAUSE_DECL (*pc) == decl)
15677 break;
15678 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15679 && OMP_CLAUSE_DECL (*pc) == decl)
15681 if (j)
15682 break;
15683 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15684 tree c = *pc;
15685 *pc = OMP_CLAUSE_CHAIN (c);
15686 OMP_CLAUSE_CHAIN (c) = *clauses;
15687 *clauses = c;
15689 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15690 && OMP_CLAUSE_DECL (*pc) == decl)
15692 error ("iteration variable %qD should not be firstprivate",
15693 decl);
15694 *pc = OMP_CLAUSE_CHAIN (*pc);
15696 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15697 && OMP_CLAUSE_DECL (*pc) == decl)
15699 error ("iteration variable %qD should not be reduction",
15700 decl);
15701 *pc = OMP_CLAUSE_CHAIN (*pc);
15703 else
15704 pc = &OMP_CLAUSE_CHAIN (*pc);
15706 if (*pc)
15707 break;
15709 if (*pc == NULL_TREE)
15711 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15712 OMP_CLAUSE_DECL (c) = decl;
15713 c = finish_omp_clauses (c, C_ORT_OMP);
15714 if (c)
15716 OMP_CLAUSE_CHAIN (c) = *clauses;
15717 *clauses = c;
15721 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15722 if (COMPARISON_CLASS_P (cond))
15724 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15725 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15726 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15728 else
15729 cond = RECUR (cond);
15730 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15731 switch (TREE_CODE (incr))
15733 case PREINCREMENT_EXPR:
15734 case PREDECREMENT_EXPR:
15735 case POSTINCREMENT_EXPR:
15736 case POSTDECREMENT_EXPR:
15737 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15738 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15739 break;
15740 case MODIFY_EXPR:
15741 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15742 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15744 tree rhs = TREE_OPERAND (incr, 1);
15745 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15746 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15747 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15748 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15749 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15750 rhs0, rhs1));
15752 else
15753 incr = RECUR (incr);
15754 break;
15755 case MODOP_EXPR:
15756 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15757 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15759 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15760 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15761 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15762 TREE_TYPE (decl), lhs,
15763 RECUR (TREE_OPERAND (incr, 2))));
15765 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15766 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15767 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15769 tree rhs = TREE_OPERAND (incr, 2);
15770 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15771 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15772 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15773 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15774 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15775 rhs0, rhs1));
15777 else
15778 incr = RECUR (incr);
15779 break;
15780 default:
15781 incr = RECUR (incr);
15782 break;
15785 TREE_VEC_ELT (declv, i) = decl;
15786 TREE_VEC_ELT (initv, i) = init;
15787 TREE_VEC_ELT (condv, i) = cond;
15788 TREE_VEC_ELT (incrv, i) = incr;
15789 #undef RECUR
15792 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15793 of OMP_TARGET's body. */
15795 static tree
15796 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15798 *walk_subtrees = 0;
15799 switch (TREE_CODE (*tp))
15801 case OMP_TEAMS:
15802 return *tp;
15803 case BIND_EXPR:
15804 case STATEMENT_LIST:
15805 *walk_subtrees = 1;
15806 break;
15807 default:
15808 break;
15810 return NULL_TREE;
15813 /* Helper function for tsubst_expr. For decomposition declaration
15814 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15815 also the corresponding decls representing the identifiers
15816 of the decomposition declaration. Return DECL if successful
15817 or error_mark_node otherwise, set *FIRST to the first decl
15818 in the list chained through DECL_CHAIN and *CNT to the number
15819 of such decls. */
15821 static tree
15822 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15823 tsubst_flags_t complain, tree in_decl, tree *first,
15824 unsigned int *cnt)
15826 tree decl2, decl3, prev = decl;
15827 *cnt = 0;
15828 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15829 for (decl2 = DECL_CHAIN (pattern_decl);
15830 decl2
15831 && VAR_P (decl2)
15832 && DECL_DECOMPOSITION_P (decl2)
15833 && DECL_NAME (decl2);
15834 decl2 = DECL_CHAIN (decl2))
15836 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15838 gcc_assert (errorcount);
15839 return error_mark_node;
15841 (*cnt)++;
15842 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15843 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15844 tree v = DECL_VALUE_EXPR (decl2);
15845 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15846 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15847 decl3 = tsubst (decl2, args, complain, in_decl);
15848 SET_DECL_VALUE_EXPR (decl2, v);
15849 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15850 if (VAR_P (decl3))
15851 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15852 maybe_push_decl (decl3);
15853 if (error_operand_p (decl3))
15854 decl = error_mark_node;
15855 else if (decl != error_mark_node
15856 && DECL_CHAIN (decl3) != prev)
15858 gcc_assert (errorcount);
15859 decl = error_mark_node;
15861 else
15862 prev = decl3;
15864 *first = prev;
15865 return decl;
15868 /* Like tsubst_copy for expressions, etc. but also does semantic
15869 processing. */
15871 tree
15872 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15873 bool integral_constant_expression_p)
15875 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15876 #define RECUR(NODE) \
15877 tsubst_expr ((NODE), args, complain, in_decl, \
15878 integral_constant_expression_p)
15880 tree stmt, tmp;
15881 tree r;
15882 location_t loc;
15884 if (t == NULL_TREE || t == error_mark_node)
15885 return t;
15887 loc = input_location;
15888 if (EXPR_HAS_LOCATION (t))
15889 input_location = EXPR_LOCATION (t);
15890 if (STATEMENT_CODE_P (TREE_CODE (t)))
15891 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15893 switch (TREE_CODE (t))
15895 case STATEMENT_LIST:
15897 tree_stmt_iterator i;
15898 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15899 RECUR (tsi_stmt (i));
15900 break;
15903 case CTOR_INITIALIZER:
15904 finish_mem_initializers (tsubst_initializer_list
15905 (TREE_OPERAND (t, 0), args));
15906 break;
15908 case RETURN_EXPR:
15909 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15910 break;
15912 case EXPR_STMT:
15913 tmp = RECUR (EXPR_STMT_EXPR (t));
15914 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15915 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15916 else
15917 finish_expr_stmt (tmp);
15918 break;
15920 case USING_STMT:
15921 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15922 /*attribs=*/NULL_TREE);
15923 break;
15925 case DECL_EXPR:
15927 tree decl, pattern_decl;
15928 tree init;
15930 pattern_decl = decl = DECL_EXPR_DECL (t);
15931 if (TREE_CODE (decl) == LABEL_DECL)
15932 finish_label_decl (DECL_NAME (decl));
15933 else if (TREE_CODE (decl) == USING_DECL)
15935 tree scope = USING_DECL_SCOPE (decl);
15936 tree name = DECL_NAME (decl);
15938 scope = tsubst (scope, args, complain, in_decl);
15939 decl = lookup_qualified_name (scope, name,
15940 /*is_type_p=*/false,
15941 /*complain=*/false);
15942 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15943 qualified_name_lookup_error (scope, name, decl, input_location);
15944 else
15945 finish_local_using_decl (decl, scope, name);
15947 else if (DECL_PACK_P (decl))
15949 /* Don't build up decls for a variadic capture proxy, we'll
15950 instantiate the elements directly as needed. */
15951 break;
15953 else if (is_capture_proxy (decl)
15954 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
15956 /* We're in tsubst_lambda_expr, we've already inserted new capture
15957 proxies, and uses will find them with lookup_name. */
15958 break;
15960 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
15961 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
15962 /* Don't copy the old closure; we'll create a new one in
15963 tsubst_lambda_expr. */
15964 break;
15965 else
15967 init = DECL_INITIAL (decl);
15968 decl = tsubst (decl, args, complain, in_decl);
15969 if (decl != error_mark_node)
15971 /* By marking the declaration as instantiated, we avoid
15972 trying to instantiate it. Since instantiate_decl can't
15973 handle local variables, and since we've already done
15974 all that needs to be done, that's the right thing to
15975 do. */
15976 if (VAR_P (decl))
15977 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15978 if (VAR_P (decl)
15979 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15980 /* Anonymous aggregates are a special case. */
15981 finish_anon_union (decl);
15982 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15984 DECL_CONTEXT (decl) = current_function_decl;
15985 if (DECL_NAME (decl) == this_identifier)
15987 tree lam = DECL_CONTEXT (current_function_decl);
15988 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15989 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15991 insert_capture_proxy (decl);
15993 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15994 /* We already did a pushtag. */;
15995 else if (TREE_CODE (decl) == FUNCTION_DECL
15996 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15997 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15999 DECL_CONTEXT (decl) = NULL_TREE;
16000 pushdecl (decl);
16001 DECL_CONTEXT (decl) = current_function_decl;
16002 cp_check_omp_declare_reduction (decl);
16004 else
16006 int const_init = false;
16007 maybe_push_decl (decl);
16008 if (VAR_P (decl)
16009 && DECL_PRETTY_FUNCTION_P (decl))
16011 /* For __PRETTY_FUNCTION__ we have to adjust the
16012 initializer. */
16013 const char *const name
16014 = cxx_printable_name (current_function_decl, 2);
16015 init = cp_fname_init (name, &TREE_TYPE (decl));
16017 else
16018 init = tsubst_init (init, decl, args, complain, in_decl);
16020 if (VAR_P (decl))
16021 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16022 (pattern_decl));
16023 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16024 if (VAR_P (decl)
16025 && DECL_DECOMPOSITION_P (decl)
16026 && TREE_TYPE (pattern_decl) != error_mark_node)
16028 unsigned int cnt;
16029 tree first;
16030 decl = tsubst_decomp_names (decl, pattern_decl, args,
16031 complain, in_decl, &first,
16032 &cnt);
16033 if (decl != error_mark_node)
16034 cp_finish_decomp (decl, first, cnt);
16040 break;
16043 case FOR_STMT:
16044 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16045 RECUR (FOR_INIT_STMT (t));
16046 finish_init_stmt (stmt);
16047 tmp = RECUR (FOR_COND (t));
16048 finish_for_cond (tmp, stmt, false);
16049 tmp = RECUR (FOR_EXPR (t));
16050 finish_for_expr (tmp, stmt);
16051 RECUR (FOR_BODY (t));
16052 finish_for_stmt (stmt);
16053 break;
16055 case RANGE_FOR_STMT:
16057 tree decl, expr;
16058 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16059 decl = RANGE_FOR_DECL (t);
16060 decl = tsubst (decl, args, complain, in_decl);
16061 maybe_push_decl (decl);
16062 expr = RECUR (RANGE_FOR_EXPR (t));
16063 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16065 unsigned int cnt;
16066 tree first;
16067 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16068 complain, in_decl, &first, &cnt);
16069 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
16070 RANGE_FOR_IVDEP (t));
16072 else
16073 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
16074 RANGE_FOR_IVDEP (t));
16075 RECUR (RANGE_FOR_BODY (t));
16076 finish_for_stmt (stmt);
16078 break;
16080 case WHILE_STMT:
16081 stmt = begin_while_stmt ();
16082 tmp = RECUR (WHILE_COND (t));
16083 finish_while_stmt_cond (tmp, stmt, false);
16084 RECUR (WHILE_BODY (t));
16085 finish_while_stmt (stmt);
16086 break;
16088 case DO_STMT:
16089 stmt = begin_do_stmt ();
16090 RECUR (DO_BODY (t));
16091 finish_do_body (stmt);
16092 tmp = RECUR (DO_COND (t));
16093 finish_do_stmt (tmp, stmt, false);
16094 break;
16096 case IF_STMT:
16097 stmt = begin_if_stmt ();
16098 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16099 tmp = RECUR (IF_COND (t));
16100 tmp = finish_if_stmt_cond (tmp, stmt);
16101 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16102 /* Don't instantiate the THEN_CLAUSE. */;
16103 else
16105 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16106 if (inhibit)
16107 ++c_inhibit_evaluation_warnings;
16108 RECUR (THEN_CLAUSE (t));
16109 if (inhibit)
16110 --c_inhibit_evaluation_warnings;
16112 finish_then_clause (stmt);
16114 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16115 /* Don't instantiate the ELSE_CLAUSE. */;
16116 else if (ELSE_CLAUSE (t))
16118 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16119 begin_else_clause (stmt);
16120 if (inhibit)
16121 ++c_inhibit_evaluation_warnings;
16122 RECUR (ELSE_CLAUSE (t));
16123 if (inhibit)
16124 --c_inhibit_evaluation_warnings;
16125 finish_else_clause (stmt);
16128 finish_if_stmt (stmt);
16129 break;
16131 case BIND_EXPR:
16132 if (BIND_EXPR_BODY_BLOCK (t))
16133 stmt = begin_function_body ();
16134 else
16135 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16136 ? BCS_TRY_BLOCK : 0);
16138 RECUR (BIND_EXPR_BODY (t));
16140 if (BIND_EXPR_BODY_BLOCK (t))
16141 finish_function_body (stmt);
16142 else
16143 finish_compound_stmt (stmt);
16144 break;
16146 case BREAK_STMT:
16147 finish_break_stmt ();
16148 break;
16150 case CONTINUE_STMT:
16151 finish_continue_stmt ();
16152 break;
16154 case SWITCH_STMT:
16155 stmt = begin_switch_stmt ();
16156 tmp = RECUR (SWITCH_STMT_COND (t));
16157 finish_switch_cond (tmp, stmt);
16158 RECUR (SWITCH_STMT_BODY (t));
16159 finish_switch_stmt (stmt);
16160 break;
16162 case CASE_LABEL_EXPR:
16164 tree low = RECUR (CASE_LOW (t));
16165 tree high = RECUR (CASE_HIGH (t));
16166 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16167 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16168 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16169 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16171 break;
16173 case LABEL_EXPR:
16175 tree decl = LABEL_EXPR_LABEL (t);
16176 tree label;
16178 label = finish_label_stmt (DECL_NAME (decl));
16179 if (TREE_CODE (label) == LABEL_DECL)
16180 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16181 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16182 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16184 break;
16186 case GOTO_EXPR:
16187 tmp = GOTO_DESTINATION (t);
16188 if (TREE_CODE (tmp) != LABEL_DECL)
16189 /* Computed goto's must be tsubst'd into. On the other hand,
16190 non-computed gotos must not be; the identifier in question
16191 will have no binding. */
16192 tmp = RECUR (tmp);
16193 else
16194 tmp = DECL_NAME (tmp);
16195 finish_goto_stmt (tmp);
16196 break;
16198 case ASM_EXPR:
16200 tree string = RECUR (ASM_STRING (t));
16201 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16202 complain, in_decl);
16203 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16204 complain, in_decl);
16205 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16206 complain, in_decl);
16207 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16208 complain, in_decl);
16209 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16210 clobbers, labels);
16211 tree asm_expr = tmp;
16212 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16213 asm_expr = TREE_OPERAND (asm_expr, 0);
16214 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16216 break;
16218 case TRY_BLOCK:
16219 if (CLEANUP_P (t))
16221 stmt = begin_try_block ();
16222 RECUR (TRY_STMTS (t));
16223 finish_cleanup_try_block (stmt);
16224 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16226 else
16228 tree compound_stmt = NULL_TREE;
16230 if (FN_TRY_BLOCK_P (t))
16231 stmt = begin_function_try_block (&compound_stmt);
16232 else
16233 stmt = begin_try_block ();
16235 RECUR (TRY_STMTS (t));
16237 if (FN_TRY_BLOCK_P (t))
16238 finish_function_try_block (stmt);
16239 else
16240 finish_try_block (stmt);
16242 RECUR (TRY_HANDLERS (t));
16243 if (FN_TRY_BLOCK_P (t))
16244 finish_function_handler_sequence (stmt, compound_stmt);
16245 else
16246 finish_handler_sequence (stmt);
16248 break;
16250 case HANDLER:
16252 tree decl = HANDLER_PARMS (t);
16254 if (decl)
16256 decl = tsubst (decl, args, complain, in_decl);
16257 /* Prevent instantiate_decl from trying to instantiate
16258 this variable. We've already done all that needs to be
16259 done. */
16260 if (decl != error_mark_node)
16261 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16263 stmt = begin_handler ();
16264 finish_handler_parms (decl, stmt);
16265 RECUR (HANDLER_BODY (t));
16266 finish_handler (stmt);
16268 break;
16270 case TAG_DEFN:
16271 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16272 if (CLASS_TYPE_P (tmp))
16274 /* Local classes are not independent templates; they are
16275 instantiated along with their containing function. And this
16276 way we don't have to deal with pushing out of one local class
16277 to instantiate a member of another local class. */
16278 /* Closures are handled by the LAMBDA_EXPR. */
16279 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16280 complete_type (tmp);
16281 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16282 if ((VAR_P (fld)
16283 || (TREE_CODE (fld) == FUNCTION_DECL
16284 && !DECL_ARTIFICIAL (fld)))
16285 && DECL_TEMPLATE_INSTANTIATION (fld))
16286 instantiate_decl (fld, /*defer_ok=*/false,
16287 /*expl_inst_class=*/false);
16289 break;
16291 case STATIC_ASSERT:
16293 tree condition;
16295 ++c_inhibit_evaluation_warnings;
16296 condition =
16297 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16298 args,
16299 complain, in_decl,
16300 /*integral_constant_expression_p=*/true);
16301 --c_inhibit_evaluation_warnings;
16303 finish_static_assert (condition,
16304 STATIC_ASSERT_MESSAGE (t),
16305 STATIC_ASSERT_SOURCE_LOCATION (t),
16306 /*member_p=*/false);
16308 break;
16310 case OACC_KERNELS:
16311 case OACC_PARALLEL:
16312 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16313 in_decl);
16314 stmt = begin_omp_parallel ();
16315 RECUR (OMP_BODY (t));
16316 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16317 break;
16319 case OMP_PARALLEL:
16320 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16321 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16322 complain, in_decl);
16323 if (OMP_PARALLEL_COMBINED (t))
16324 omp_parallel_combined_clauses = &tmp;
16325 stmt = begin_omp_parallel ();
16326 RECUR (OMP_PARALLEL_BODY (t));
16327 gcc_assert (omp_parallel_combined_clauses == NULL);
16328 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16329 = OMP_PARALLEL_COMBINED (t);
16330 pop_omp_privatization_clauses (r);
16331 break;
16333 case OMP_TASK:
16334 r = push_omp_privatization_clauses (false);
16335 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16336 complain, in_decl);
16337 stmt = begin_omp_task ();
16338 RECUR (OMP_TASK_BODY (t));
16339 finish_omp_task (tmp, stmt);
16340 pop_omp_privatization_clauses (r);
16341 break;
16343 case OMP_FOR:
16344 case OMP_SIMD:
16345 case CILK_SIMD:
16346 case CILK_FOR:
16347 case OMP_DISTRIBUTE:
16348 case OMP_TASKLOOP:
16349 case OACC_LOOP:
16351 tree clauses, body, pre_body;
16352 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16353 tree orig_declv = NULL_TREE;
16354 tree incrv = NULL_TREE;
16355 enum c_omp_region_type ort = C_ORT_OMP;
16356 int i;
16358 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16359 ort = C_ORT_CILK;
16360 else if (TREE_CODE (t) == OACC_LOOP)
16361 ort = C_ORT_ACC;
16363 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16364 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16365 in_decl);
16366 if (OMP_FOR_INIT (t) != NULL_TREE)
16368 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16369 if (OMP_FOR_ORIG_DECLS (t))
16370 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16371 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16372 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16373 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16376 stmt = begin_omp_structured_block ();
16378 pre_body = push_stmt_list ();
16379 RECUR (OMP_FOR_PRE_BODY (t));
16380 pre_body = pop_stmt_list (pre_body);
16382 if (OMP_FOR_INIT (t) != NULL_TREE)
16383 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16384 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16385 incrv, &clauses, args, complain, in_decl,
16386 integral_constant_expression_p);
16387 omp_parallel_combined_clauses = NULL;
16389 body = push_stmt_list ();
16390 RECUR (OMP_FOR_BODY (t));
16391 body = pop_stmt_list (body);
16393 if (OMP_FOR_INIT (t) != NULL_TREE)
16394 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16395 orig_declv, initv, condv, incrv, body, pre_body,
16396 NULL, clauses);
16397 else
16399 t = make_node (TREE_CODE (t));
16400 TREE_TYPE (t) = void_type_node;
16401 OMP_FOR_BODY (t) = body;
16402 OMP_FOR_PRE_BODY (t) = pre_body;
16403 OMP_FOR_CLAUSES (t) = clauses;
16404 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16405 add_stmt (t);
16408 add_stmt (finish_omp_structured_block (stmt));
16409 pop_omp_privatization_clauses (r);
16411 break;
16413 case OMP_SECTIONS:
16414 omp_parallel_combined_clauses = NULL;
16415 /* FALLTHRU */
16416 case OMP_SINGLE:
16417 case OMP_TEAMS:
16418 case OMP_CRITICAL:
16419 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16420 && OMP_TEAMS_COMBINED (t));
16421 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16422 in_decl);
16423 stmt = push_stmt_list ();
16424 RECUR (OMP_BODY (t));
16425 stmt = pop_stmt_list (stmt);
16427 t = copy_node (t);
16428 OMP_BODY (t) = stmt;
16429 OMP_CLAUSES (t) = tmp;
16430 add_stmt (t);
16431 pop_omp_privatization_clauses (r);
16432 break;
16434 case OACC_DATA:
16435 case OMP_TARGET_DATA:
16436 case OMP_TARGET:
16437 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16438 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16439 in_decl);
16440 keep_next_level (true);
16441 stmt = begin_omp_structured_block ();
16443 RECUR (OMP_BODY (t));
16444 stmt = finish_omp_structured_block (stmt);
16446 t = copy_node (t);
16447 OMP_BODY (t) = stmt;
16448 OMP_CLAUSES (t) = tmp;
16449 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16451 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16452 if (teams)
16454 /* For combined target teams, ensure the num_teams and
16455 thread_limit clause expressions are evaluated on the host,
16456 before entering the target construct. */
16457 tree c;
16458 for (c = OMP_TEAMS_CLAUSES (teams);
16459 c; c = OMP_CLAUSE_CHAIN (c))
16460 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16461 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16462 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16464 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16465 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16466 if (expr == error_mark_node)
16467 continue;
16468 tmp = TARGET_EXPR_SLOT (expr);
16469 add_stmt (expr);
16470 OMP_CLAUSE_OPERAND (c, 0) = expr;
16471 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16472 OMP_CLAUSE_FIRSTPRIVATE);
16473 OMP_CLAUSE_DECL (tc) = tmp;
16474 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16475 OMP_TARGET_CLAUSES (t) = tc;
16479 add_stmt (t);
16480 break;
16482 case OACC_DECLARE:
16483 t = copy_node (t);
16484 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16485 complain, in_decl);
16486 OACC_DECLARE_CLAUSES (t) = tmp;
16487 add_stmt (t);
16488 break;
16490 case OMP_TARGET_UPDATE:
16491 case OMP_TARGET_ENTER_DATA:
16492 case OMP_TARGET_EXIT_DATA:
16493 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16494 complain, in_decl);
16495 t = copy_node (t);
16496 OMP_STANDALONE_CLAUSES (t) = tmp;
16497 add_stmt (t);
16498 break;
16500 case OACC_ENTER_DATA:
16501 case OACC_EXIT_DATA:
16502 case OACC_UPDATE:
16503 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16504 complain, in_decl);
16505 t = copy_node (t);
16506 OMP_STANDALONE_CLAUSES (t) = tmp;
16507 add_stmt (t);
16508 break;
16510 case OMP_ORDERED:
16511 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16512 complain, in_decl);
16513 stmt = push_stmt_list ();
16514 RECUR (OMP_BODY (t));
16515 stmt = pop_stmt_list (stmt);
16517 t = copy_node (t);
16518 OMP_BODY (t) = stmt;
16519 OMP_ORDERED_CLAUSES (t) = tmp;
16520 add_stmt (t);
16521 break;
16523 case OMP_SECTION:
16524 case OMP_MASTER:
16525 case OMP_TASKGROUP:
16526 stmt = push_stmt_list ();
16527 RECUR (OMP_BODY (t));
16528 stmt = pop_stmt_list (stmt);
16530 t = copy_node (t);
16531 OMP_BODY (t) = stmt;
16532 add_stmt (t);
16533 break;
16535 case OMP_ATOMIC:
16536 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16537 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16539 tree op1 = TREE_OPERAND (t, 1);
16540 tree rhs1 = NULL_TREE;
16541 tree lhs, rhs;
16542 if (TREE_CODE (op1) == COMPOUND_EXPR)
16544 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16545 op1 = TREE_OPERAND (op1, 1);
16547 lhs = RECUR (TREE_OPERAND (op1, 0));
16548 rhs = RECUR (TREE_OPERAND (op1, 1));
16549 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16550 NULL_TREE, NULL_TREE, rhs1,
16551 OMP_ATOMIC_SEQ_CST (t));
16553 else
16555 tree op1 = TREE_OPERAND (t, 1);
16556 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16557 tree rhs1 = NULL_TREE;
16558 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16559 enum tree_code opcode = NOP_EXPR;
16560 if (code == OMP_ATOMIC_READ)
16562 v = RECUR (TREE_OPERAND (op1, 0));
16563 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16565 else if (code == OMP_ATOMIC_CAPTURE_OLD
16566 || code == OMP_ATOMIC_CAPTURE_NEW)
16568 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16569 v = RECUR (TREE_OPERAND (op1, 0));
16570 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16571 if (TREE_CODE (op11) == COMPOUND_EXPR)
16573 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16574 op11 = TREE_OPERAND (op11, 1);
16576 lhs = RECUR (TREE_OPERAND (op11, 0));
16577 rhs = RECUR (TREE_OPERAND (op11, 1));
16578 opcode = TREE_CODE (op11);
16579 if (opcode == MODIFY_EXPR)
16580 opcode = NOP_EXPR;
16582 else
16584 code = OMP_ATOMIC;
16585 lhs = RECUR (TREE_OPERAND (op1, 0));
16586 rhs = RECUR (TREE_OPERAND (op1, 1));
16588 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16589 OMP_ATOMIC_SEQ_CST (t));
16591 break;
16593 case TRANSACTION_EXPR:
16595 int flags = 0;
16596 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16597 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16599 if (TRANSACTION_EXPR_IS_STMT (t))
16601 tree body = TRANSACTION_EXPR_BODY (t);
16602 tree noex = NULL_TREE;
16603 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16605 noex = MUST_NOT_THROW_COND (body);
16606 if (noex == NULL_TREE)
16607 noex = boolean_true_node;
16608 body = TREE_OPERAND (body, 0);
16610 stmt = begin_transaction_stmt (input_location, NULL, flags);
16611 RECUR (body);
16612 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16614 else
16616 stmt = build_transaction_expr (EXPR_LOCATION (t),
16617 RECUR (TRANSACTION_EXPR_BODY (t)),
16618 flags, NULL_TREE);
16619 RETURN (stmt);
16622 break;
16624 case MUST_NOT_THROW_EXPR:
16626 tree op0 = RECUR (TREE_OPERAND (t, 0));
16627 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16628 RETURN (build_must_not_throw_expr (op0, cond));
16631 case EXPR_PACK_EXPANSION:
16632 error ("invalid use of pack expansion expression");
16633 RETURN (error_mark_node);
16635 case NONTYPE_ARGUMENT_PACK:
16636 error ("use %<...%> to expand argument pack");
16637 RETURN (error_mark_node);
16639 case CILK_SPAWN_STMT:
16640 cfun->calls_cilk_spawn = 1;
16641 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16643 case CILK_SYNC_STMT:
16644 RETURN (build_cilk_sync ());
16646 case COMPOUND_EXPR:
16647 tmp = RECUR (TREE_OPERAND (t, 0));
16648 if (tmp == NULL_TREE)
16649 /* If the first operand was a statement, we're done with it. */
16650 RETURN (RECUR (TREE_OPERAND (t, 1)));
16651 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16652 RECUR (TREE_OPERAND (t, 1)),
16653 complain));
16655 case ANNOTATE_EXPR:
16656 tmp = RECUR (TREE_OPERAND (t, 0));
16657 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16658 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16660 default:
16661 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16663 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16664 /*function_p=*/false,
16665 integral_constant_expression_p));
16668 RETURN (NULL_TREE);
16669 out:
16670 input_location = loc;
16671 return r;
16672 #undef RECUR
16673 #undef RETURN
16676 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16677 function. For description of the body see comment above
16678 cp_parser_omp_declare_reduction_exprs. */
16680 static void
16681 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16683 if (t == NULL_TREE || t == error_mark_node)
16684 return;
16686 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16688 tree_stmt_iterator tsi;
16689 int i;
16690 tree stmts[7];
16691 memset (stmts, 0, sizeof stmts);
16692 for (i = 0, tsi = tsi_start (t);
16693 i < 7 && !tsi_end_p (tsi);
16694 i++, tsi_next (&tsi))
16695 stmts[i] = tsi_stmt (tsi);
16696 gcc_assert (tsi_end_p (tsi));
16698 if (i >= 3)
16700 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16701 && TREE_CODE (stmts[1]) == DECL_EXPR);
16702 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16703 args, complain, in_decl);
16704 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16705 args, complain, in_decl);
16706 DECL_CONTEXT (omp_out) = current_function_decl;
16707 DECL_CONTEXT (omp_in) = current_function_decl;
16708 keep_next_level (true);
16709 tree block = begin_omp_structured_block ();
16710 tsubst_expr (stmts[2], args, complain, in_decl, false);
16711 block = finish_omp_structured_block (block);
16712 block = maybe_cleanup_point_expr_void (block);
16713 add_decl_expr (omp_out);
16714 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16715 TREE_NO_WARNING (omp_out) = 1;
16716 add_decl_expr (omp_in);
16717 finish_expr_stmt (block);
16719 if (i >= 6)
16721 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16722 && TREE_CODE (stmts[4]) == DECL_EXPR);
16723 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16724 args, complain, in_decl);
16725 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16726 args, complain, in_decl);
16727 DECL_CONTEXT (omp_priv) = current_function_decl;
16728 DECL_CONTEXT (omp_orig) = current_function_decl;
16729 keep_next_level (true);
16730 tree block = begin_omp_structured_block ();
16731 tsubst_expr (stmts[5], args, complain, in_decl, false);
16732 block = finish_omp_structured_block (block);
16733 block = maybe_cleanup_point_expr_void (block);
16734 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16735 add_decl_expr (omp_priv);
16736 add_decl_expr (omp_orig);
16737 finish_expr_stmt (block);
16738 if (i == 7)
16739 add_decl_expr (omp_orig);
16743 /* T is a postfix-expression that is not being used in a function
16744 call. Return the substituted version of T. */
16746 static tree
16747 tsubst_non_call_postfix_expression (tree t, tree args,
16748 tsubst_flags_t complain,
16749 tree in_decl)
16751 if (TREE_CODE (t) == SCOPE_REF)
16752 t = tsubst_qualified_id (t, args, complain, in_decl,
16753 /*done=*/false, /*address_p=*/false);
16754 else
16755 t = tsubst_copy_and_build (t, args, complain, in_decl,
16756 /*function_p=*/false,
16757 /*integral_constant_expression_p=*/false);
16759 return t;
16762 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
16763 instantiation context. Instantiating a pack expansion containing a lambda
16764 might result in multiple lambdas all based on the same lambda in the
16765 template. */
16767 tree
16768 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16770 tree oldfn = lambda_function (t);
16771 in_decl = oldfn;
16773 tree r = build_lambda_expr ();
16775 LAMBDA_EXPR_LOCATION (r)
16776 = LAMBDA_EXPR_LOCATION (t);
16777 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
16778 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
16779 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
16781 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
16782 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
16783 else
16784 record_lambda_scope (r);
16786 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
16787 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
16789 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
16790 cap = TREE_CHAIN (cap))
16792 tree field = TREE_PURPOSE (cap);
16793 if (PACK_EXPANSION_P (field))
16794 field = PACK_EXPANSION_PATTERN (field);
16795 field = tsubst_decl (field, args, complain);
16797 if (field == error_mark_node)
16798 return error_mark_node;
16800 tree init = TREE_VALUE (cap);
16801 if (PACK_EXPANSION_P (init))
16802 init = tsubst_pack_expansion (init, args, complain, in_decl);
16803 else
16804 init = tsubst_copy_and_build (init, args, complain, in_decl,
16805 /*fn*/false, /*constexpr*/false);
16807 if (TREE_CODE (field) == TREE_VEC)
16809 int len = TREE_VEC_LENGTH (field);
16810 gcc_assert (TREE_CODE (init) == TREE_VEC
16811 && TREE_VEC_LENGTH (init) == len);
16812 for (int i = 0; i < len; ++i)
16813 LAMBDA_EXPR_CAPTURE_LIST (r)
16814 = tree_cons (TREE_VEC_ELT (field, i),
16815 TREE_VEC_ELT (init, i),
16816 LAMBDA_EXPR_CAPTURE_LIST (r));
16818 else
16820 LAMBDA_EXPR_CAPTURE_LIST (r)
16821 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
16823 if (id_equal (DECL_NAME (field), "__this"))
16824 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
16828 tree type = begin_lambda_type (r);
16830 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
16831 determine_visibility (TYPE_NAME (type));
16833 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
16835 tree oldtmpl = (generic_lambda_fn_p (oldfn)
16836 ? DECL_TI_TEMPLATE (oldfn)
16837 : NULL_TREE);
16839 tree fntype = static_fn_type (oldfn);
16840 if (oldtmpl)
16841 ++processing_template_decl;
16842 fntype = tsubst (fntype, args, complain, in_decl);
16843 if (oldtmpl)
16844 --processing_template_decl;
16846 if (fntype == error_mark_node)
16847 r = error_mark_node;
16848 else
16850 /* Fix the type of 'this'. */
16851 fntype = build_memfn_type (fntype, type,
16852 type_memfn_quals (fntype),
16853 type_memfn_rqual (fntype));
16854 tree fn, tmpl;
16855 if (oldtmpl)
16857 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
16858 fn = DECL_TEMPLATE_RESULT (tmpl);
16859 finish_member_declaration (tmpl);
16861 else
16863 tmpl = NULL_TREE;
16864 fn = tsubst_function_decl (oldfn, args, complain, fntype);
16865 finish_member_declaration (fn);
16868 /* Let finish_function set this. */
16869 DECL_DECLARED_CONSTEXPR_P (fn) = false;
16871 bool nested = cfun;
16872 if (nested)
16873 push_function_context ();
16875 tree body = start_lambda_function (fn, r);
16877 local_specialization_stack s (lss_copy);
16879 register_parameter_specializations (oldfn, fn);
16881 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
16882 /*constexpr*/false);
16884 finish_lambda_function (body);
16886 if (nested)
16887 pop_function_context ();
16889 /* The capture list was built up in reverse order; fix that now. */
16890 LAMBDA_EXPR_CAPTURE_LIST (r)
16891 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
16893 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
16895 maybe_add_lambda_conv_op (type);
16898 finish_struct (type, /*attr*/NULL_TREE);
16900 insert_pending_capture_proxies ();
16902 return r;
16905 /* Like tsubst but deals with expressions and performs semantic
16906 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16908 tree
16909 tsubst_copy_and_build (tree t,
16910 tree args,
16911 tsubst_flags_t complain,
16912 tree in_decl,
16913 bool function_p,
16914 bool integral_constant_expression_p)
16916 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16917 #define RECUR(NODE) \
16918 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16919 /*function_p=*/false, \
16920 integral_constant_expression_p)
16922 tree retval, op1;
16923 location_t loc;
16925 if (t == NULL_TREE || t == error_mark_node)
16926 return t;
16928 loc = input_location;
16929 if (EXPR_HAS_LOCATION (t))
16930 input_location = EXPR_LOCATION (t);
16932 /* N3276 decltype magic only applies to calls at the top level or on the
16933 right side of a comma. */
16934 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16935 complain &= ~tf_decltype;
16937 switch (TREE_CODE (t))
16939 case USING_DECL:
16940 t = DECL_NAME (t);
16941 /* Fall through. */
16942 case IDENTIFIER_NODE:
16944 tree decl;
16945 cp_id_kind idk;
16946 bool non_integral_constant_expression_p;
16947 const char *error_msg;
16949 if (IDENTIFIER_CONV_OP_P (t))
16951 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16952 t = make_conv_op_name (new_type);
16955 /* Look up the name. */
16956 decl = lookup_name (t);
16958 /* By convention, expressions use ERROR_MARK_NODE to indicate
16959 failure, not NULL_TREE. */
16960 if (decl == NULL_TREE)
16961 decl = error_mark_node;
16963 decl = finish_id_expression (t, decl, NULL_TREE,
16964 &idk,
16965 integral_constant_expression_p,
16966 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16967 &non_integral_constant_expression_p,
16968 /*template_p=*/false,
16969 /*done=*/true,
16970 /*address_p=*/false,
16971 /*template_arg_p=*/false,
16972 &error_msg,
16973 input_location);
16974 if (error_msg)
16975 error (error_msg);
16976 if (!function_p && identifier_p (decl))
16978 if (complain & tf_error)
16979 unqualified_name_lookup_error (decl);
16980 decl = error_mark_node;
16982 RETURN (decl);
16985 case TEMPLATE_ID_EXPR:
16987 tree object;
16988 tree templ = RECUR (TREE_OPERAND (t, 0));
16989 tree targs = TREE_OPERAND (t, 1);
16991 if (targs)
16992 targs = tsubst_template_args (targs, args, complain, in_decl);
16993 if (targs == error_mark_node)
16994 return error_mark_node;
16996 if (TREE_CODE (templ) == SCOPE_REF)
16998 tree name = TREE_OPERAND (templ, 1);
16999 tree tid = lookup_template_function (name, targs);
17000 TREE_OPERAND (templ, 1) = tid;
17001 return templ;
17004 if (variable_template_p (templ))
17005 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17007 if (TREE_CODE (templ) == COMPONENT_REF)
17009 object = TREE_OPERAND (templ, 0);
17010 templ = TREE_OPERAND (templ, 1);
17012 else
17013 object = NULL_TREE;
17014 templ = lookup_template_function (templ, targs);
17016 if (object)
17017 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17018 object, templ, NULL_TREE));
17019 else
17020 RETURN (baselink_for_fns (templ));
17023 case INDIRECT_REF:
17025 tree r = RECUR (TREE_OPERAND (t, 0));
17027 if (REFERENCE_REF_P (t))
17029 /* A type conversion to reference type will be enclosed in
17030 such an indirect ref, but the substitution of the cast
17031 will have also added such an indirect ref. */
17032 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
17033 r = convert_from_reference (r);
17035 else
17036 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17037 complain|decltype_flag);
17039 if (TREE_CODE (r) == INDIRECT_REF)
17040 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17042 RETURN (r);
17045 case NOP_EXPR:
17047 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17048 tree op0 = RECUR (TREE_OPERAND (t, 0));
17049 RETURN (build_nop (type, op0));
17052 case IMPLICIT_CONV_EXPR:
17054 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17055 tree expr = RECUR (TREE_OPERAND (t, 0));
17056 int flags = LOOKUP_IMPLICIT;
17057 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17058 flags = LOOKUP_NORMAL;
17059 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17060 flags));
17063 case CONVERT_EXPR:
17065 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17066 tree op0 = RECUR (TREE_OPERAND (t, 0));
17067 RETURN (build1 (CONVERT_EXPR, type, op0));
17070 case CAST_EXPR:
17071 case REINTERPRET_CAST_EXPR:
17072 case CONST_CAST_EXPR:
17073 case DYNAMIC_CAST_EXPR:
17074 case STATIC_CAST_EXPR:
17076 tree type;
17077 tree op, r = NULL_TREE;
17079 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17080 if (integral_constant_expression_p
17081 && !cast_valid_in_integral_constant_expression_p (type))
17083 if (complain & tf_error)
17084 error ("a cast to a type other than an integral or "
17085 "enumeration type cannot appear in a constant-expression");
17086 RETURN (error_mark_node);
17089 op = RECUR (TREE_OPERAND (t, 0));
17091 warning_sentinel s(warn_useless_cast);
17092 switch (TREE_CODE (t))
17094 case CAST_EXPR:
17095 r = build_functional_cast (type, op, complain);
17096 break;
17097 case REINTERPRET_CAST_EXPR:
17098 r = build_reinterpret_cast (type, op, complain);
17099 break;
17100 case CONST_CAST_EXPR:
17101 r = build_const_cast (type, op, complain);
17102 break;
17103 case DYNAMIC_CAST_EXPR:
17104 r = build_dynamic_cast (type, op, complain);
17105 break;
17106 case STATIC_CAST_EXPR:
17107 r = build_static_cast (type, op, complain);
17108 break;
17109 default:
17110 gcc_unreachable ();
17113 RETURN (r);
17116 case POSTDECREMENT_EXPR:
17117 case POSTINCREMENT_EXPR:
17118 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17119 args, complain, in_decl);
17120 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17121 complain|decltype_flag));
17123 case PREDECREMENT_EXPR:
17124 case PREINCREMENT_EXPR:
17125 case NEGATE_EXPR:
17126 case BIT_NOT_EXPR:
17127 case ABS_EXPR:
17128 case TRUTH_NOT_EXPR:
17129 case UNARY_PLUS_EXPR: /* Unary + */
17130 case REALPART_EXPR:
17131 case IMAGPART_EXPR:
17132 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17133 RECUR (TREE_OPERAND (t, 0)),
17134 complain|decltype_flag));
17136 case FIX_TRUNC_EXPR:
17137 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17138 false, complain));
17140 case ADDR_EXPR:
17141 op1 = TREE_OPERAND (t, 0);
17142 if (TREE_CODE (op1) == LABEL_DECL)
17143 RETURN (finish_label_address_expr (DECL_NAME (op1),
17144 EXPR_LOCATION (op1)));
17145 if (TREE_CODE (op1) == SCOPE_REF)
17146 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17147 /*done=*/true, /*address_p=*/true);
17148 else
17149 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17150 in_decl);
17151 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17152 complain|decltype_flag));
17154 case PLUS_EXPR:
17155 case MINUS_EXPR:
17156 case MULT_EXPR:
17157 case TRUNC_DIV_EXPR:
17158 case CEIL_DIV_EXPR:
17159 case FLOOR_DIV_EXPR:
17160 case ROUND_DIV_EXPR:
17161 case EXACT_DIV_EXPR:
17162 case BIT_AND_EXPR:
17163 case BIT_IOR_EXPR:
17164 case BIT_XOR_EXPR:
17165 case TRUNC_MOD_EXPR:
17166 case FLOOR_MOD_EXPR:
17167 case TRUTH_ANDIF_EXPR:
17168 case TRUTH_ORIF_EXPR:
17169 case TRUTH_AND_EXPR:
17170 case TRUTH_OR_EXPR:
17171 case RSHIFT_EXPR:
17172 case LSHIFT_EXPR:
17173 case RROTATE_EXPR:
17174 case LROTATE_EXPR:
17175 case EQ_EXPR:
17176 case NE_EXPR:
17177 case MAX_EXPR:
17178 case MIN_EXPR:
17179 case LE_EXPR:
17180 case GE_EXPR:
17181 case LT_EXPR:
17182 case GT_EXPR:
17183 case MEMBER_REF:
17184 case DOTSTAR_EXPR:
17186 warning_sentinel s1(warn_type_limits);
17187 warning_sentinel s2(warn_div_by_zero);
17188 warning_sentinel s3(warn_logical_op);
17189 warning_sentinel s4(warn_tautological_compare);
17190 tree op0 = RECUR (TREE_OPERAND (t, 0));
17191 tree op1 = RECUR (TREE_OPERAND (t, 1));
17192 tree r = build_x_binary_op
17193 (input_location, TREE_CODE (t),
17194 op0,
17195 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17196 ? ERROR_MARK
17197 : TREE_CODE (TREE_OPERAND (t, 0))),
17198 op1,
17199 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17200 ? ERROR_MARK
17201 : TREE_CODE (TREE_OPERAND (t, 1))),
17202 /*overload=*/NULL,
17203 complain|decltype_flag);
17204 if (EXPR_P (r) && TREE_NO_WARNING (t))
17205 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17207 RETURN (r);
17210 case POINTER_PLUS_EXPR:
17212 tree op0 = RECUR (TREE_OPERAND (t, 0));
17213 tree op1 = RECUR (TREE_OPERAND (t, 1));
17214 return fold_build_pointer_plus (op0, op1);
17217 case SCOPE_REF:
17218 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17219 /*address_p=*/false));
17220 case ARRAY_REF:
17221 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17222 args, complain, in_decl);
17223 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17224 RECUR (TREE_OPERAND (t, 1)),
17225 complain|decltype_flag));
17227 case ARRAY_NOTATION_REF:
17229 tree start_index, length, stride;
17230 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
17231 args, complain, in_decl);
17232 start_index = RECUR (ARRAY_NOTATION_START (t));
17233 length = RECUR (ARRAY_NOTATION_LENGTH (t));
17234 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
17235 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17236 length, stride, TREE_TYPE (op1)));
17238 case SIZEOF_EXPR:
17239 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17240 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17241 RETURN (tsubst_copy (t, args, complain, in_decl));
17242 /* Fall through */
17244 case ALIGNOF_EXPR:
17246 tree r;
17248 op1 = TREE_OPERAND (t, 0);
17249 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17250 op1 = TREE_TYPE (op1);
17251 if (!args)
17253 /* When there are no ARGS, we are trying to evaluate a
17254 non-dependent expression from the parser. Trying to do
17255 the substitutions may not work. */
17256 if (!TYPE_P (op1))
17257 op1 = TREE_TYPE (op1);
17259 else
17261 ++cp_unevaluated_operand;
17262 ++c_inhibit_evaluation_warnings;
17263 if (TYPE_P (op1))
17264 op1 = tsubst (op1, args, complain, in_decl);
17265 else
17266 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17267 /*function_p=*/false,
17268 /*integral_constant_expression_p=*/
17269 false);
17270 --cp_unevaluated_operand;
17271 --c_inhibit_evaluation_warnings;
17273 if (TYPE_P (op1))
17274 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17275 complain & tf_error);
17276 else
17277 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17278 complain & tf_error);
17279 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17281 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17283 if (!processing_template_decl && TYPE_P (op1))
17285 r = build_min (SIZEOF_EXPR, size_type_node,
17286 build1 (NOP_EXPR, op1, error_mark_node));
17287 SIZEOF_EXPR_TYPE_P (r) = 1;
17289 else
17290 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17291 TREE_SIDE_EFFECTS (r) = 0;
17292 TREE_READONLY (r) = 1;
17294 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17296 RETURN (r);
17299 case AT_ENCODE_EXPR:
17301 op1 = TREE_OPERAND (t, 0);
17302 ++cp_unevaluated_operand;
17303 ++c_inhibit_evaluation_warnings;
17304 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17305 /*function_p=*/false,
17306 /*integral_constant_expression_p=*/false);
17307 --cp_unevaluated_operand;
17308 --c_inhibit_evaluation_warnings;
17309 RETURN (objc_build_encode_expr (op1));
17312 case NOEXCEPT_EXPR:
17313 op1 = TREE_OPERAND (t, 0);
17314 ++cp_unevaluated_operand;
17315 ++c_inhibit_evaluation_warnings;
17316 ++cp_noexcept_operand;
17317 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17318 /*function_p=*/false,
17319 /*integral_constant_expression_p=*/false);
17320 --cp_unevaluated_operand;
17321 --c_inhibit_evaluation_warnings;
17322 --cp_noexcept_operand;
17323 RETURN (finish_noexcept_expr (op1, complain));
17325 case MODOP_EXPR:
17327 warning_sentinel s(warn_div_by_zero);
17328 tree lhs = RECUR (TREE_OPERAND (t, 0));
17329 tree rhs = RECUR (TREE_OPERAND (t, 2));
17330 tree r = build_x_modify_expr
17331 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17332 complain|decltype_flag);
17333 /* TREE_NO_WARNING must be set if either the expression was
17334 parenthesized or it uses an operator such as >>= rather
17335 than plain assignment. In the former case, it was already
17336 set and must be copied. In the latter case,
17337 build_x_modify_expr sets it and it must not be reset
17338 here. */
17339 if (TREE_NO_WARNING (t))
17340 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17342 RETURN (r);
17345 case ARROW_EXPR:
17346 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17347 args, complain, in_decl);
17348 /* Remember that there was a reference to this entity. */
17349 if (DECL_P (op1)
17350 && !mark_used (op1, complain) && !(complain & tf_error))
17351 RETURN (error_mark_node);
17352 RETURN (build_x_arrow (input_location, op1, complain));
17354 case NEW_EXPR:
17356 tree placement = RECUR (TREE_OPERAND (t, 0));
17357 tree init = RECUR (TREE_OPERAND (t, 3));
17358 vec<tree, va_gc> *placement_vec;
17359 vec<tree, va_gc> *init_vec;
17360 tree ret;
17362 if (placement == NULL_TREE)
17363 placement_vec = NULL;
17364 else
17366 placement_vec = make_tree_vector ();
17367 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17368 vec_safe_push (placement_vec, TREE_VALUE (placement));
17371 /* If there was an initializer in the original tree, but it
17372 instantiated to an empty list, then we should pass a
17373 non-NULL empty vector to tell build_new that it was an
17374 empty initializer() rather than no initializer. This can
17375 only happen when the initializer is a pack expansion whose
17376 parameter packs are of length zero. */
17377 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17378 init_vec = NULL;
17379 else
17381 init_vec = make_tree_vector ();
17382 if (init == void_node)
17383 gcc_assert (init_vec != NULL);
17384 else
17386 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17387 vec_safe_push (init_vec, TREE_VALUE (init));
17391 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17392 tree op2 = RECUR (TREE_OPERAND (t, 2));
17393 ret = build_new (&placement_vec, op1, op2, &init_vec,
17394 NEW_EXPR_USE_GLOBAL (t),
17395 complain);
17397 if (placement_vec != NULL)
17398 release_tree_vector (placement_vec);
17399 if (init_vec != NULL)
17400 release_tree_vector (init_vec);
17402 RETURN (ret);
17405 case DELETE_EXPR:
17407 tree op0 = RECUR (TREE_OPERAND (t, 0));
17408 tree op1 = RECUR (TREE_OPERAND (t, 1));
17409 RETURN (delete_sanity (op0, op1,
17410 DELETE_EXPR_USE_VEC (t),
17411 DELETE_EXPR_USE_GLOBAL (t),
17412 complain));
17415 case COMPOUND_EXPR:
17417 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17418 complain & ~tf_decltype, in_decl,
17419 /*function_p=*/false,
17420 integral_constant_expression_p);
17421 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17422 op0,
17423 RECUR (TREE_OPERAND (t, 1)),
17424 complain|decltype_flag));
17427 case CALL_EXPR:
17429 tree function;
17430 vec<tree, va_gc> *call_args;
17431 unsigned int nargs, i;
17432 bool qualified_p;
17433 bool koenig_p;
17434 tree ret;
17436 function = CALL_EXPR_FN (t);
17437 /* Internal function with no arguments. */
17438 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17439 RETURN (t);
17441 /* When we parsed the expression, we determined whether or
17442 not Koenig lookup should be performed. */
17443 koenig_p = KOENIG_LOOKUP_P (t);
17444 if (function == NULL_TREE)
17446 koenig_p = false;
17447 qualified_p = false;
17449 else if (TREE_CODE (function) == SCOPE_REF)
17451 qualified_p = true;
17452 function = tsubst_qualified_id (function, args, complain, in_decl,
17453 /*done=*/false,
17454 /*address_p=*/false);
17456 else if (koenig_p && identifier_p (function))
17458 /* Do nothing; calling tsubst_copy_and_build on an identifier
17459 would incorrectly perform unqualified lookup again.
17461 Note that we can also have an IDENTIFIER_NODE if the earlier
17462 unqualified lookup found a member function; in that case
17463 koenig_p will be false and we do want to do the lookup
17464 again to find the instantiated member function.
17466 FIXME but doing that causes c++/15272, so we need to stop
17467 using IDENTIFIER_NODE in that situation. */
17468 qualified_p = false;
17470 else
17472 if (TREE_CODE (function) == COMPONENT_REF)
17474 tree op = TREE_OPERAND (function, 1);
17476 qualified_p = (TREE_CODE (op) == SCOPE_REF
17477 || (BASELINK_P (op)
17478 && BASELINK_QUALIFIED_P (op)));
17480 else
17481 qualified_p = false;
17483 if (TREE_CODE (function) == ADDR_EXPR
17484 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17485 /* Avoid error about taking the address of a constructor. */
17486 function = TREE_OPERAND (function, 0);
17488 function = tsubst_copy_and_build (function, args, complain,
17489 in_decl,
17490 !qualified_p,
17491 integral_constant_expression_p);
17493 if (BASELINK_P (function))
17494 qualified_p = true;
17497 nargs = call_expr_nargs (t);
17498 call_args = make_tree_vector ();
17499 for (i = 0; i < nargs; ++i)
17501 tree arg = CALL_EXPR_ARG (t, i);
17503 if (!PACK_EXPANSION_P (arg))
17504 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17505 else
17507 /* Expand the pack expansion and push each entry onto
17508 CALL_ARGS. */
17509 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17510 if (TREE_CODE (arg) == TREE_VEC)
17512 unsigned int len, j;
17514 len = TREE_VEC_LENGTH (arg);
17515 for (j = 0; j < len; ++j)
17517 tree value = TREE_VEC_ELT (arg, j);
17518 if (value != NULL_TREE)
17519 value = convert_from_reference (value);
17520 vec_safe_push (call_args, value);
17523 else
17525 /* A partial substitution. Add one entry. */
17526 vec_safe_push (call_args, arg);
17531 /* We do not perform argument-dependent lookup if normal
17532 lookup finds a non-function, in accordance with the
17533 expected resolution of DR 218. */
17534 if (koenig_p
17535 && ((is_overloaded_fn (function)
17536 /* If lookup found a member function, the Koenig lookup is
17537 not appropriate, even if an unqualified-name was used
17538 to denote the function. */
17539 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17540 || identifier_p (function))
17541 /* Only do this when substitution turns a dependent call
17542 into a non-dependent call. */
17543 && type_dependent_expression_p_push (t)
17544 && !any_type_dependent_arguments_p (call_args))
17545 function = perform_koenig_lookup (function, call_args, tf_none);
17547 if (function != NULL_TREE
17548 && identifier_p (function)
17549 && !any_type_dependent_arguments_p (call_args))
17551 if (koenig_p && (complain & tf_warning_or_error))
17553 /* For backwards compatibility and good diagnostics, try
17554 the unqualified lookup again if we aren't in SFINAE
17555 context. */
17556 tree unq = (tsubst_copy_and_build
17557 (function, args, complain, in_decl, true,
17558 integral_constant_expression_p));
17559 if (unq == error_mark_node)
17561 release_tree_vector (call_args);
17562 RETURN (error_mark_node);
17565 if (unq != function)
17567 /* In a lambda fn, we have to be careful to not
17568 introduce new this captures. Legacy code can't
17569 be using lambdas anyway, so it's ok to be
17570 stricter. */
17571 bool in_lambda = (current_class_type
17572 && LAMBDA_TYPE_P (current_class_type));
17573 char const *const msg
17574 = G_("%qD was not declared in this scope, "
17575 "and no declarations were found by "
17576 "argument-dependent lookup at the point "
17577 "of instantiation");
17579 bool diag = true;
17580 if (in_lambda)
17581 error_at (EXPR_LOC_OR_LOC (t, input_location),
17582 msg, function);
17583 else
17584 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17585 msg, function);
17586 if (diag)
17588 tree fn = unq;
17590 if (INDIRECT_REF_P (fn))
17591 fn = TREE_OPERAND (fn, 0);
17592 if (is_overloaded_fn (fn))
17593 fn = get_first_fn (fn);
17595 if (!DECL_P (fn))
17596 /* Can't say anything more. */;
17597 else if (DECL_CLASS_SCOPE_P (fn))
17599 location_t loc = EXPR_LOC_OR_LOC (t,
17600 input_location);
17601 inform (loc,
17602 "declarations in dependent base %qT are "
17603 "not found by unqualified lookup",
17604 DECL_CLASS_CONTEXT (fn));
17605 if (current_class_ptr)
17606 inform (loc,
17607 "use %<this->%D%> instead", function);
17608 else
17609 inform (loc,
17610 "use %<%T::%D%> instead",
17611 current_class_name, function);
17613 else
17614 inform (DECL_SOURCE_LOCATION (fn),
17615 "%qD declared here, later in the "
17616 "translation unit", fn);
17617 if (in_lambda)
17619 release_tree_vector (call_args);
17620 RETURN (error_mark_node);
17624 function = unq;
17627 if (identifier_p (function))
17629 if (complain & tf_error)
17630 unqualified_name_lookup_error (function);
17631 release_tree_vector (call_args);
17632 RETURN (error_mark_node);
17636 /* Remember that there was a reference to this entity. */
17637 if (function != NULL_TREE
17638 && DECL_P (function)
17639 && !mark_used (function, complain) && !(complain & tf_error))
17641 release_tree_vector (call_args);
17642 RETURN (error_mark_node);
17645 /* Put back tf_decltype for the actual call. */
17646 complain |= decltype_flag;
17648 if (function == NULL_TREE)
17649 switch (CALL_EXPR_IFN (t))
17651 case IFN_LAUNDER:
17652 gcc_assert (nargs == 1);
17653 if (vec_safe_length (call_args) != 1)
17655 error_at (EXPR_LOC_OR_LOC (t, input_location),
17656 "wrong number of arguments to "
17657 "%<__builtin_launder%>");
17658 ret = error_mark_node;
17660 else
17661 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17662 input_location),
17663 (*call_args)[0], complain);
17664 break;
17666 default:
17667 /* Unsupported internal function with arguments. */
17668 gcc_unreachable ();
17670 else if (TREE_CODE (function) == OFFSET_REF)
17671 ret = build_offset_ref_call_from_tree (function, &call_args,
17672 complain);
17673 else if (TREE_CODE (function) == COMPONENT_REF)
17675 tree instance = TREE_OPERAND (function, 0);
17676 tree fn = TREE_OPERAND (function, 1);
17678 if (processing_template_decl
17679 && (type_dependent_expression_p (instance)
17680 || (!BASELINK_P (fn)
17681 && TREE_CODE (fn) != FIELD_DECL)
17682 || type_dependent_expression_p (fn)
17683 || any_type_dependent_arguments_p (call_args)))
17684 ret = build_min_nt_call_vec (function, call_args);
17685 else if (!BASELINK_P (fn))
17686 ret = finish_call_expr (function, &call_args,
17687 /*disallow_virtual=*/false,
17688 /*koenig_p=*/false,
17689 complain);
17690 else
17691 ret = (build_new_method_call
17692 (instance, fn,
17693 &call_args, NULL_TREE,
17694 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17695 /*fn_p=*/NULL,
17696 complain));
17698 else
17699 ret = finish_call_expr (function, &call_args,
17700 /*disallow_virtual=*/qualified_p,
17701 koenig_p,
17702 complain);
17704 release_tree_vector (call_args);
17706 if (ret != error_mark_node)
17708 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17709 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17710 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17711 bool thk = CALL_FROM_THUNK_P (t);
17712 if (op || ord || rev || thk)
17714 function = extract_call_expr (ret);
17715 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17716 CALL_EXPR_ORDERED_ARGS (function) = ord;
17717 CALL_EXPR_REVERSE_ARGS (function) = rev;
17718 if (thk)
17720 CALL_FROM_THUNK_P (function) = true;
17721 /* The thunk location is not interesting. */
17722 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17727 RETURN (ret);
17730 case COND_EXPR:
17732 tree cond = RECUR (TREE_OPERAND (t, 0));
17733 tree folded_cond = fold_non_dependent_expr (cond);
17734 tree exp1, exp2;
17736 if (TREE_CODE (folded_cond) == INTEGER_CST)
17738 if (integer_zerop (folded_cond))
17740 ++c_inhibit_evaluation_warnings;
17741 exp1 = RECUR (TREE_OPERAND (t, 1));
17742 --c_inhibit_evaluation_warnings;
17743 exp2 = RECUR (TREE_OPERAND (t, 2));
17745 else
17747 exp1 = RECUR (TREE_OPERAND (t, 1));
17748 ++c_inhibit_evaluation_warnings;
17749 exp2 = RECUR (TREE_OPERAND (t, 2));
17750 --c_inhibit_evaluation_warnings;
17752 cond = folded_cond;
17754 else
17756 exp1 = RECUR (TREE_OPERAND (t, 1));
17757 exp2 = RECUR (TREE_OPERAND (t, 2));
17760 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17761 cond, exp1, exp2, complain));
17764 case PSEUDO_DTOR_EXPR:
17766 tree op0 = RECUR (TREE_OPERAND (t, 0));
17767 tree op1 = RECUR (TREE_OPERAND (t, 1));
17768 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17769 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17770 input_location));
17773 case TREE_LIST:
17775 tree purpose, value, chain;
17777 if (t == void_list_node)
17778 RETURN (t);
17780 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17781 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17783 /* We have pack expansions, so expand those and
17784 create a new list out of it. */
17785 tree purposevec = NULL_TREE;
17786 tree valuevec = NULL_TREE;
17787 tree chain;
17788 int i, len = -1;
17790 /* Expand the argument expressions. */
17791 if (TREE_PURPOSE (t))
17792 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17793 complain, in_decl);
17794 if (TREE_VALUE (t))
17795 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17796 complain, in_decl);
17798 /* Build the rest of the list. */
17799 chain = TREE_CHAIN (t);
17800 if (chain && chain != void_type_node)
17801 chain = RECUR (chain);
17803 /* Determine the number of arguments. */
17804 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17806 len = TREE_VEC_LENGTH (purposevec);
17807 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17809 else if (TREE_CODE (valuevec) == TREE_VEC)
17810 len = TREE_VEC_LENGTH (valuevec);
17811 else
17813 /* Since we only performed a partial substitution into
17814 the argument pack, we only RETURN (a single list
17815 node. */
17816 if (purposevec == TREE_PURPOSE (t)
17817 && valuevec == TREE_VALUE (t)
17818 && chain == TREE_CHAIN (t))
17819 RETURN (t);
17821 RETURN (tree_cons (purposevec, valuevec, chain));
17824 /* Convert the argument vectors into a TREE_LIST */
17825 i = len;
17826 while (i > 0)
17828 /* Grab the Ith values. */
17829 i--;
17830 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17831 : NULL_TREE;
17832 value
17833 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17834 : NULL_TREE;
17836 /* Build the list (backwards). */
17837 chain = tree_cons (purpose, value, chain);
17840 RETURN (chain);
17843 purpose = TREE_PURPOSE (t);
17844 if (purpose)
17845 purpose = RECUR (purpose);
17846 value = TREE_VALUE (t);
17847 if (value)
17848 value = RECUR (value);
17849 chain = TREE_CHAIN (t);
17850 if (chain && chain != void_type_node)
17851 chain = RECUR (chain);
17852 if (purpose == TREE_PURPOSE (t)
17853 && value == TREE_VALUE (t)
17854 && chain == TREE_CHAIN (t))
17855 RETURN (t);
17856 RETURN (tree_cons (purpose, value, chain));
17859 case COMPONENT_REF:
17861 tree object;
17862 tree object_type;
17863 tree member;
17864 tree r;
17866 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17867 args, complain, in_decl);
17868 /* Remember that there was a reference to this entity. */
17869 if (DECL_P (object)
17870 && !mark_used (object, complain) && !(complain & tf_error))
17871 RETURN (error_mark_node);
17872 object_type = TREE_TYPE (object);
17874 member = TREE_OPERAND (t, 1);
17875 if (BASELINK_P (member))
17876 member = tsubst_baselink (member,
17877 non_reference (TREE_TYPE (object)),
17878 args, complain, in_decl);
17879 else
17880 member = tsubst_copy (member, args, complain, in_decl);
17881 if (member == error_mark_node)
17882 RETURN (error_mark_node);
17884 if (TREE_CODE (member) == FIELD_DECL)
17886 r = finish_non_static_data_member (member, object, NULL_TREE);
17887 if (TREE_CODE (r) == COMPONENT_REF)
17888 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17889 RETURN (r);
17891 else if (type_dependent_expression_p (object))
17892 /* We can't do much here. */;
17893 else if (!CLASS_TYPE_P (object_type))
17895 if (scalarish_type_p (object_type))
17897 tree s = NULL_TREE;
17898 tree dtor = member;
17900 if (TREE_CODE (dtor) == SCOPE_REF)
17902 s = TREE_OPERAND (dtor, 0);
17903 dtor = TREE_OPERAND (dtor, 1);
17905 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17907 dtor = TREE_OPERAND (dtor, 0);
17908 if (TYPE_P (dtor))
17909 RETURN (finish_pseudo_destructor_expr
17910 (object, s, dtor, input_location));
17914 else if (TREE_CODE (member) == SCOPE_REF
17915 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17917 /* Lookup the template functions now that we know what the
17918 scope is. */
17919 tree scope = TREE_OPERAND (member, 0);
17920 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17921 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17922 member = lookup_qualified_name (scope, tmpl,
17923 /*is_type_p=*/false,
17924 /*complain=*/false);
17925 if (BASELINK_P (member))
17927 BASELINK_FUNCTIONS (member)
17928 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17929 args);
17930 member = (adjust_result_of_qualified_name_lookup
17931 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17932 object_type));
17934 else
17936 qualified_name_lookup_error (scope, tmpl, member,
17937 input_location);
17938 RETURN (error_mark_node);
17941 else if (TREE_CODE (member) == SCOPE_REF
17942 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17943 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17945 if (complain & tf_error)
17947 if (TYPE_P (TREE_OPERAND (member, 0)))
17948 error ("%qT is not a class or namespace",
17949 TREE_OPERAND (member, 0));
17950 else
17951 error ("%qD is not a class or namespace",
17952 TREE_OPERAND (member, 0));
17954 RETURN (error_mark_node);
17957 r = finish_class_member_access_expr (object, member,
17958 /*template_p=*/false,
17959 complain);
17960 if (TREE_CODE (r) == COMPONENT_REF)
17961 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17962 RETURN (r);
17965 case THROW_EXPR:
17966 RETURN (build_throw
17967 (RECUR (TREE_OPERAND (t, 0))));
17969 case CONSTRUCTOR:
17971 vec<constructor_elt, va_gc> *n;
17972 constructor_elt *ce;
17973 unsigned HOST_WIDE_INT idx;
17974 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17975 bool process_index_p;
17976 int newlen;
17977 bool need_copy_p = false;
17978 tree r;
17980 if (type == error_mark_node)
17981 RETURN (error_mark_node);
17983 /* digest_init will do the wrong thing if we let it. */
17984 if (type && TYPE_PTRMEMFUNC_P (type))
17985 RETURN (t);
17987 /* We do not want to process the index of aggregate
17988 initializers as they are identifier nodes which will be
17989 looked up by digest_init. */
17990 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17992 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17993 newlen = vec_safe_length (n);
17994 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17996 if (ce->index && process_index_p
17997 /* An identifier index is looked up in the type
17998 being initialized, not the current scope. */
17999 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18000 ce->index = RECUR (ce->index);
18002 if (PACK_EXPANSION_P (ce->value))
18004 /* Substitute into the pack expansion. */
18005 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18006 in_decl);
18008 if (ce->value == error_mark_node
18009 || PACK_EXPANSION_P (ce->value))
18011 else if (TREE_VEC_LENGTH (ce->value) == 1)
18012 /* Just move the argument into place. */
18013 ce->value = TREE_VEC_ELT (ce->value, 0);
18014 else
18016 /* Update the length of the final CONSTRUCTOR
18017 arguments vector, and note that we will need to
18018 copy.*/
18019 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18020 need_copy_p = true;
18023 else
18024 ce->value = RECUR (ce->value);
18027 if (need_copy_p)
18029 vec<constructor_elt, va_gc> *old_n = n;
18031 vec_alloc (n, newlen);
18032 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18034 if (TREE_CODE (ce->value) == TREE_VEC)
18036 int i, len = TREE_VEC_LENGTH (ce->value);
18037 for (i = 0; i < len; ++i)
18038 CONSTRUCTOR_APPEND_ELT (n, 0,
18039 TREE_VEC_ELT (ce->value, i));
18041 else
18042 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18046 r = build_constructor (init_list_type_node, n);
18047 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18049 if (TREE_HAS_CONSTRUCTOR (t))
18051 fcl_t cl = fcl_functional;
18052 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18053 cl = fcl_c99;
18054 RETURN (finish_compound_literal (type, r, complain, cl));
18057 TREE_TYPE (r) = type;
18058 RETURN (r);
18061 case TYPEID_EXPR:
18063 tree operand_0 = TREE_OPERAND (t, 0);
18064 if (TYPE_P (operand_0))
18066 operand_0 = tsubst (operand_0, args, complain, in_decl);
18067 RETURN (get_typeid (operand_0, complain));
18069 else
18071 operand_0 = RECUR (operand_0);
18072 RETURN (build_typeid (operand_0, complain));
18076 case VAR_DECL:
18077 if (!args)
18078 RETURN (t);
18079 else if (DECL_PACK_P (t))
18081 /* We don't build decls for an instantiation of a
18082 variadic capture proxy, we instantiate the elements
18083 when needed. */
18084 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
18085 return RECUR (DECL_VALUE_EXPR (t));
18087 /* Fall through */
18089 case PARM_DECL:
18091 tree r = tsubst_copy (t, args, complain, in_decl);
18092 /* ??? We're doing a subset of finish_id_expression here. */
18093 if (VAR_P (r)
18094 && !processing_template_decl
18095 && !cp_unevaluated_operand
18096 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18097 && CP_DECL_THREAD_LOCAL_P (r))
18099 if (tree wrap = get_tls_wrapper_fn (r))
18100 /* Replace an evaluated use of the thread_local variable with
18101 a call to its wrapper. */
18102 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18104 else if (outer_automatic_var_p (r))
18106 r = process_outer_var_ref (r, complain);
18107 if (is_capture_proxy (r) && !DECL_PACK_P (t))
18108 register_local_specialization (r, t);
18111 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18112 /* If the original type was a reference, we'll be wrapped in
18113 the appropriate INDIRECT_REF. */
18114 r = convert_from_reference (r);
18115 RETURN (r);
18118 case VA_ARG_EXPR:
18120 tree op0 = RECUR (TREE_OPERAND (t, 0));
18121 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18122 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18125 case OFFSETOF_EXPR:
18127 tree object_ptr
18128 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18129 in_decl, /*function_p=*/false,
18130 /*integral_constant_expression_p=*/false);
18131 RETURN (finish_offsetof (object_ptr,
18132 RECUR (TREE_OPERAND (t, 0)),
18133 EXPR_LOCATION (t)));
18136 case ADDRESSOF_EXPR:
18137 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18138 RECUR (TREE_OPERAND (t, 0)), complain));
18140 case TRAIT_EXPR:
18142 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18143 complain, in_decl);
18145 tree type2 = TRAIT_EXPR_TYPE2 (t);
18146 if (type2 && TREE_CODE (type2) == TREE_LIST)
18147 type2 = RECUR (type2);
18148 else if (type2)
18149 type2 = tsubst (type2, args, complain, in_decl);
18151 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18154 case STMT_EXPR:
18156 tree old_stmt_expr = cur_stmt_expr;
18157 tree stmt_expr = begin_stmt_expr ();
18159 cur_stmt_expr = stmt_expr;
18160 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18161 integral_constant_expression_p);
18162 stmt_expr = finish_stmt_expr (stmt_expr, false);
18163 cur_stmt_expr = old_stmt_expr;
18165 /* If the resulting list of expression statement is empty,
18166 fold it further into void_node. */
18167 if (empty_expr_stmt_p (stmt_expr))
18168 stmt_expr = void_node;
18170 RETURN (stmt_expr);
18173 case LAMBDA_EXPR:
18175 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18177 RETURN (build_lambda_object (r));
18180 case TARGET_EXPR:
18181 /* We can get here for a constant initializer of non-dependent type.
18182 FIXME stop folding in cp_parser_initializer_clause. */
18184 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18185 complain);
18186 RETURN (r);
18189 case TRANSACTION_EXPR:
18190 RETURN (tsubst_expr(t, args, complain, in_decl,
18191 integral_constant_expression_p));
18193 case PAREN_EXPR:
18194 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18196 case VEC_PERM_EXPR:
18198 tree op0 = RECUR (TREE_OPERAND (t, 0));
18199 tree op1 = RECUR (TREE_OPERAND (t, 1));
18200 tree op2 = RECUR (TREE_OPERAND (t, 2));
18201 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18202 complain));
18205 case REQUIRES_EXPR:
18206 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18208 default:
18209 /* Handle Objective-C++ constructs, if appropriate. */
18211 tree subst
18212 = objcp_tsubst_copy_and_build (t, args, complain,
18213 in_decl, /*function_p=*/false);
18214 if (subst)
18215 RETURN (subst);
18217 RETURN (tsubst_copy (t, args, complain, in_decl));
18220 #undef RECUR
18221 #undef RETURN
18222 out:
18223 input_location = loc;
18224 return retval;
18227 /* Verify that the instantiated ARGS are valid. For type arguments,
18228 make sure that the type's linkage is ok. For non-type arguments,
18229 make sure they are constants if they are integral or enumerations.
18230 Emit an error under control of COMPLAIN, and return TRUE on error. */
18232 static bool
18233 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18235 if (dependent_template_arg_p (t))
18236 return false;
18237 if (ARGUMENT_PACK_P (t))
18239 tree vec = ARGUMENT_PACK_ARGS (t);
18240 int len = TREE_VEC_LENGTH (vec);
18241 bool result = false;
18242 int i;
18244 for (i = 0; i < len; ++i)
18245 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18246 result = true;
18247 return result;
18249 else if (TYPE_P (t))
18251 /* [basic.link]: A name with no linkage (notably, the name
18252 of a class or enumeration declared in a local scope)
18253 shall not be used to declare an entity with linkage.
18254 This implies that names with no linkage cannot be used as
18255 template arguments
18257 DR 757 relaxes this restriction for C++0x. */
18258 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18259 : no_linkage_check (t, /*relaxed_p=*/false));
18261 if (nt)
18263 /* DR 488 makes use of a type with no linkage cause
18264 type deduction to fail. */
18265 if (complain & tf_error)
18267 if (TYPE_UNNAMED_P (nt))
18268 error ("%qT is/uses unnamed type", t);
18269 else
18270 error ("template argument for %qD uses local type %qT",
18271 tmpl, t);
18273 return true;
18275 /* In order to avoid all sorts of complications, we do not
18276 allow variably-modified types as template arguments. */
18277 else if (variably_modified_type_p (t, NULL_TREE))
18279 if (complain & tf_error)
18280 error ("%qT is a variably modified type", t);
18281 return true;
18284 /* Class template and alias template arguments should be OK. */
18285 else if (DECL_TYPE_TEMPLATE_P (t))
18287 /* A non-type argument of integral or enumerated type must be a
18288 constant. */
18289 else if (TREE_TYPE (t)
18290 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18291 && !REFERENCE_REF_P (t)
18292 && !TREE_CONSTANT (t))
18294 if (complain & tf_error)
18295 error ("integral expression %qE is not constant", t);
18296 return true;
18298 return false;
18301 static bool
18302 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18304 int ix, len = DECL_NTPARMS (tmpl);
18305 bool result = false;
18307 for (ix = 0; ix != len; ix++)
18309 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18310 result = true;
18312 if (result && (complain & tf_error))
18313 error (" trying to instantiate %qD", tmpl);
18314 return result;
18317 /* We're out of SFINAE context now, so generate diagnostics for the access
18318 errors we saw earlier when instantiating D from TMPL and ARGS. */
18320 static void
18321 recheck_decl_substitution (tree d, tree tmpl, tree args)
18323 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18324 tree type = TREE_TYPE (pattern);
18325 location_t loc = input_location;
18327 push_access_scope (d);
18328 push_deferring_access_checks (dk_no_deferred);
18329 input_location = DECL_SOURCE_LOCATION (pattern);
18330 tsubst (type, args, tf_warning_or_error, d);
18331 input_location = loc;
18332 pop_deferring_access_checks ();
18333 pop_access_scope (d);
18336 /* Instantiate the indicated variable, function, or alias template TMPL with
18337 the template arguments in TARG_PTR. */
18339 static tree
18340 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18342 tree targ_ptr = orig_args;
18343 tree fndecl;
18344 tree gen_tmpl;
18345 tree spec;
18346 bool access_ok = true;
18348 if (tmpl == error_mark_node)
18349 return error_mark_node;
18351 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18353 /* If this function is a clone, handle it specially. */
18354 if (DECL_CLONED_FUNCTION_P (tmpl))
18356 tree spec;
18357 tree clone;
18359 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18360 DECL_CLONED_FUNCTION. */
18361 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18362 targ_ptr, complain);
18363 if (spec == error_mark_node)
18364 return error_mark_node;
18366 /* Look for the clone. */
18367 FOR_EACH_CLONE (clone, spec)
18368 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18369 return clone;
18370 /* We should always have found the clone by now. */
18371 gcc_unreachable ();
18372 return NULL_TREE;
18375 if (targ_ptr == error_mark_node)
18376 return error_mark_node;
18378 /* Check to see if we already have this specialization. */
18379 gen_tmpl = most_general_template (tmpl);
18380 if (TMPL_ARGS_DEPTH (targ_ptr)
18381 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18382 /* targ_ptr only has the innermost template args, so add the outer ones
18383 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18384 the case of a non-dependent call within a template definition). */
18385 targ_ptr = (add_outermost_template_args
18386 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18387 targ_ptr));
18389 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18390 but it doesn't seem to be on the hot path. */
18391 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18393 gcc_assert (tmpl == gen_tmpl
18394 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18395 == spec)
18396 || fndecl == NULL_TREE);
18398 if (spec != NULL_TREE)
18400 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18402 if (complain & tf_error)
18403 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18404 return error_mark_node;
18406 return spec;
18409 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18410 complain))
18411 return error_mark_node;
18413 /* We are building a FUNCTION_DECL, during which the access of its
18414 parameters and return types have to be checked. However this
18415 FUNCTION_DECL which is the desired context for access checking
18416 is not built yet. We solve this chicken-and-egg problem by
18417 deferring all checks until we have the FUNCTION_DECL. */
18418 push_deferring_access_checks (dk_deferred);
18420 /* Instantiation of the function happens in the context of the function
18421 template, not the context of the overload resolution we're doing. */
18422 push_to_top_level ();
18423 /* If there are dependent arguments, e.g. because we're doing partial
18424 ordering, make sure processing_template_decl stays set. */
18425 if (uses_template_parms (targ_ptr))
18426 ++processing_template_decl;
18427 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18429 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18430 complain, gen_tmpl, true);
18431 push_nested_class (ctx);
18434 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18436 fndecl = NULL_TREE;
18437 if (VAR_P (pattern))
18439 /* We need to determine if we're using a partial or explicit
18440 specialization now, because the type of the variable could be
18441 different. */
18442 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18443 tree elt = most_specialized_partial_spec (tid, complain);
18444 if (elt == error_mark_node)
18445 pattern = error_mark_node;
18446 else if (elt)
18448 tree partial_tmpl = TREE_VALUE (elt);
18449 tree partial_args = TREE_PURPOSE (elt);
18450 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18451 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18455 /* Substitute template parameters to obtain the specialization. */
18456 if (fndecl == NULL_TREE)
18457 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18458 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18459 pop_nested_class ();
18460 pop_from_top_level ();
18462 if (fndecl == error_mark_node)
18464 pop_deferring_access_checks ();
18465 return error_mark_node;
18468 /* The DECL_TI_TEMPLATE should always be the immediate parent
18469 template, not the most general template. */
18470 DECL_TI_TEMPLATE (fndecl) = tmpl;
18471 DECL_TI_ARGS (fndecl) = targ_ptr;
18473 /* Now we know the specialization, compute access previously
18474 deferred. Do no access control for inheriting constructors,
18475 as we already checked access for the inherited constructor. */
18476 if (!(flag_new_inheriting_ctors
18477 && DECL_INHERITED_CTOR (fndecl)))
18479 push_access_scope (fndecl);
18480 if (!perform_deferred_access_checks (complain))
18481 access_ok = false;
18482 pop_access_scope (fndecl);
18484 pop_deferring_access_checks ();
18486 /* If we've just instantiated the main entry point for a function,
18487 instantiate all the alternate entry points as well. We do this
18488 by cloning the instantiation of the main entry point, not by
18489 instantiating the template clones. */
18490 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18491 clone_function_decl (fndecl, /*update_methods=*/false);
18493 if (!access_ok)
18495 if (!(complain & tf_error))
18497 /* Remember to reinstantiate when we're out of SFINAE so the user
18498 can see the errors. */
18499 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18501 return error_mark_node;
18503 return fndecl;
18506 /* Wrapper for instantiate_template_1. */
18508 tree
18509 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18511 tree ret;
18512 timevar_push (TV_TEMPLATE_INST);
18513 ret = instantiate_template_1 (tmpl, orig_args, complain);
18514 timevar_pop (TV_TEMPLATE_INST);
18515 return ret;
18518 /* Instantiate the alias template TMPL with ARGS. Also push a template
18519 instantiation level, which instantiate_template doesn't do because
18520 functions and variables have sufficient context established by the
18521 callers. */
18523 static tree
18524 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18526 struct pending_template *old_last_pend = last_pending_template;
18527 struct tinst_level *old_error_tinst = last_error_tinst_level;
18528 if (tmpl == error_mark_node || args == error_mark_node)
18529 return error_mark_node;
18530 tree tinst = build_tree_list (tmpl, args);
18531 if (!push_tinst_level (tinst))
18533 ggc_free (tinst);
18534 return error_mark_node;
18537 args =
18538 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18539 args, tmpl, complain,
18540 /*require_all_args=*/true,
18541 /*use_default_args=*/true);
18543 tree r = instantiate_template (tmpl, args, complain);
18544 pop_tinst_level ();
18545 /* We can't free this if a pending_template entry or last_error_tinst_level
18546 is pointing at it. */
18547 if (last_pending_template == old_last_pend
18548 && last_error_tinst_level == old_error_tinst)
18549 ggc_free (tinst);
18551 return r;
18554 /* PARM is a template parameter pack for FN. Returns true iff
18555 PARM is used in a deducible way in the argument list of FN. */
18557 static bool
18558 pack_deducible_p (tree parm, tree fn)
18560 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18561 for (; t; t = TREE_CHAIN (t))
18563 tree type = TREE_VALUE (t);
18564 tree packs;
18565 if (!PACK_EXPANSION_P (type))
18566 continue;
18567 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18568 packs; packs = TREE_CHAIN (packs))
18569 if (template_args_equal (TREE_VALUE (packs), parm))
18571 /* The template parameter pack is used in a function parameter
18572 pack. If this is the end of the parameter list, the
18573 template parameter pack is deducible. */
18574 if (TREE_CHAIN (t) == void_list_node)
18575 return true;
18576 else
18577 /* Otherwise, not. Well, it could be deduced from
18578 a non-pack parameter, but doing so would end up with
18579 a deduction mismatch, so don't bother. */
18580 return false;
18583 /* The template parameter pack isn't used in any function parameter
18584 packs, but it might be used deeper, e.g. tuple<Args...>. */
18585 return true;
18588 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18589 NARGS elements of the arguments that are being used when calling
18590 it. TARGS is a vector into which the deduced template arguments
18591 are placed.
18593 Returns either a FUNCTION_DECL for the matching specialization of FN or
18594 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18595 true, diagnostics will be printed to explain why it failed.
18597 If FN is a conversion operator, or we are trying to produce a specific
18598 specialization, RETURN_TYPE is the return type desired.
18600 The EXPLICIT_TARGS are explicit template arguments provided via a
18601 template-id.
18603 The parameter STRICT is one of:
18605 DEDUCE_CALL:
18606 We are deducing arguments for a function call, as in
18607 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18608 deducing arguments for a call to the result of a conversion
18609 function template, as in [over.call.object].
18611 DEDUCE_CONV:
18612 We are deducing arguments for a conversion function, as in
18613 [temp.deduct.conv].
18615 DEDUCE_EXACT:
18616 We are deducing arguments when doing an explicit instantiation
18617 as in [temp.explicit], when determining an explicit specialization
18618 as in [temp.expl.spec], or when taking the address of a function
18619 template, as in [temp.deduct.funcaddr]. */
18621 tree
18622 fn_type_unification (tree fn,
18623 tree explicit_targs,
18624 tree targs,
18625 const tree *args,
18626 unsigned int nargs,
18627 tree return_type,
18628 unification_kind_t strict,
18629 int flags,
18630 bool explain_p,
18631 bool decltype_p)
18633 tree parms;
18634 tree fntype;
18635 tree decl = NULL_TREE;
18636 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18637 bool ok;
18638 static int deduction_depth;
18639 struct pending_template *old_last_pend = last_pending_template;
18640 struct tinst_level *old_error_tinst = last_error_tinst_level;
18642 tree orig_fn = fn;
18643 if (flag_new_inheriting_ctors)
18644 fn = strip_inheriting_ctors (fn);
18646 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18647 tree tinst;
18648 tree r = error_mark_node;
18650 tree full_targs = targs;
18651 if (TMPL_ARGS_DEPTH (targs)
18652 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18653 full_targs = (add_outermost_template_args
18654 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18655 targs));
18657 if (decltype_p)
18658 complain |= tf_decltype;
18660 /* In C++0x, it's possible to have a function template whose type depends
18661 on itself recursively. This is most obvious with decltype, but can also
18662 occur with enumeration scope (c++/48969). So we need to catch infinite
18663 recursion and reject the substitution at deduction time; this function
18664 will return error_mark_node for any repeated substitution.
18666 This also catches excessive recursion such as when f<N> depends on
18667 f<N-1> across all integers, and returns error_mark_node for all the
18668 substitutions back up to the initial one.
18670 This is, of course, not reentrant. */
18671 if (excessive_deduction_depth)
18672 return error_mark_node;
18673 tinst = build_tree_list (fn, NULL_TREE);
18674 ++deduction_depth;
18676 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18678 fntype = TREE_TYPE (fn);
18679 if (explicit_targs)
18681 /* [temp.deduct]
18683 The specified template arguments must match the template
18684 parameters in kind (i.e., type, nontype, template), and there
18685 must not be more arguments than there are parameters;
18686 otherwise type deduction fails.
18688 Nontype arguments must match the types of the corresponding
18689 nontype template parameters, or must be convertible to the
18690 types of the corresponding nontype parameters as specified in
18691 _temp.arg.nontype_, otherwise type deduction fails.
18693 All references in the function type of the function template
18694 to the corresponding template parameters are replaced by the
18695 specified template argument values. If a substitution in a
18696 template parameter or in the function type of the function
18697 template results in an invalid type, type deduction fails. */
18698 int i, len = TREE_VEC_LENGTH (tparms);
18699 location_t loc = input_location;
18700 bool incomplete = false;
18702 if (explicit_targs == error_mark_node)
18703 goto fail;
18705 if (TMPL_ARGS_DEPTH (explicit_targs)
18706 < TMPL_ARGS_DEPTH (full_targs))
18707 explicit_targs = add_outermost_template_args (full_targs,
18708 explicit_targs);
18710 /* Adjust any explicit template arguments before entering the
18711 substitution context. */
18712 explicit_targs
18713 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18714 complain,
18715 /*require_all_args=*/false,
18716 /*use_default_args=*/false));
18717 if (explicit_targs == error_mark_node)
18718 goto fail;
18720 /* Substitute the explicit args into the function type. This is
18721 necessary so that, for instance, explicitly declared function
18722 arguments can match null pointed constants. If we were given
18723 an incomplete set of explicit args, we must not do semantic
18724 processing during substitution as we could create partial
18725 instantiations. */
18726 for (i = 0; i < len; i++)
18728 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18729 bool parameter_pack = false;
18730 tree targ = TREE_VEC_ELT (explicit_targs, i);
18732 /* Dig out the actual parm. */
18733 if (TREE_CODE (parm) == TYPE_DECL
18734 || TREE_CODE (parm) == TEMPLATE_DECL)
18736 parm = TREE_TYPE (parm);
18737 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18739 else if (TREE_CODE (parm) == PARM_DECL)
18741 parm = DECL_INITIAL (parm);
18742 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18745 if (!parameter_pack && targ == NULL_TREE)
18746 /* No explicit argument for this template parameter. */
18747 incomplete = true;
18749 if (parameter_pack && pack_deducible_p (parm, fn))
18751 /* Mark the argument pack as "incomplete". We could
18752 still deduce more arguments during unification.
18753 We remove this mark in type_unification_real. */
18754 if (targ)
18756 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18757 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18758 = ARGUMENT_PACK_ARGS (targ);
18761 /* We have some incomplete argument packs. */
18762 incomplete = true;
18766 TREE_VALUE (tinst) = explicit_targs;
18767 if (!push_tinst_level (tinst))
18769 excessive_deduction_depth = true;
18770 goto fail;
18772 processing_template_decl += incomplete;
18773 input_location = DECL_SOURCE_LOCATION (fn);
18774 /* Ignore any access checks; we'll see them again in
18775 instantiate_template and they might have the wrong
18776 access path at this point. */
18777 push_deferring_access_checks (dk_deferred);
18778 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18779 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18780 pop_deferring_access_checks ();
18781 input_location = loc;
18782 processing_template_decl -= incomplete;
18783 pop_tinst_level ();
18785 if (fntype == error_mark_node)
18786 goto fail;
18788 /* Place the explicitly specified arguments in TARGS. */
18789 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18790 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18791 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18794 /* Never do unification on the 'this' parameter. */
18795 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18797 if (return_type && strict == DEDUCE_CALL)
18799 /* We're deducing for a call to the result of a template conversion
18800 function. The parms we really want are in return_type. */
18801 if (POINTER_TYPE_P (return_type))
18802 return_type = TREE_TYPE (return_type);
18803 parms = TYPE_ARG_TYPES (return_type);
18805 else if (return_type)
18807 tree *new_args;
18809 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18810 new_args = XALLOCAVEC (tree, nargs + 1);
18811 new_args[0] = return_type;
18812 memcpy (new_args + 1, args, nargs * sizeof (tree));
18813 args = new_args;
18814 ++nargs;
18817 /* We allow incomplete unification without an error message here
18818 because the standard doesn't seem to explicitly prohibit it. Our
18819 callers must be ready to deal with unification failures in any
18820 event. */
18822 TREE_VALUE (tinst) = targs;
18823 /* If we aren't explaining yet, push tinst context so we can see where
18824 any errors (e.g. from class instantiations triggered by instantiation
18825 of default template arguments) come from. If we are explaining, this
18826 context is redundant. */
18827 if (!explain_p && !push_tinst_level (tinst))
18829 excessive_deduction_depth = true;
18830 goto fail;
18833 /* type_unification_real will pass back any access checks from default
18834 template argument substitution. */
18835 vec<deferred_access_check, va_gc> *checks;
18836 checks = NULL;
18838 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18839 full_targs, parms, args, nargs, /*subr=*/0,
18840 strict, flags, &checks, explain_p);
18841 if (!explain_p)
18842 pop_tinst_level ();
18843 if (!ok)
18844 goto fail;
18846 /* Now that we have bindings for all of the template arguments,
18847 ensure that the arguments deduced for the template template
18848 parameters have compatible template parameter lists. We cannot
18849 check this property before we have deduced all template
18850 arguments, because the template parameter types of a template
18851 template parameter might depend on prior template parameters
18852 deduced after the template template parameter. The following
18853 ill-formed example illustrates this issue:
18855 template<typename T, template<T> class C> void f(C<5>, T);
18857 template<int N> struct X {};
18859 void g() {
18860 f(X<5>(), 5l); // error: template argument deduction fails
18863 The template parameter list of 'C' depends on the template type
18864 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18865 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18866 time that we deduce 'C'. */
18867 if (!template_template_parm_bindings_ok_p
18868 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18870 unify_inconsistent_template_template_parameters (explain_p);
18871 goto fail;
18874 /* All is well so far. Now, check:
18876 [temp.deduct]
18878 When all template arguments have been deduced, all uses of
18879 template parameters in nondeduced contexts are replaced with
18880 the corresponding deduced argument values. If the
18881 substitution results in an invalid type, as described above,
18882 type deduction fails. */
18883 TREE_VALUE (tinst) = targs;
18884 if (!push_tinst_level (tinst))
18886 excessive_deduction_depth = true;
18887 goto fail;
18890 /* Also collect access checks from the instantiation. */
18891 reopen_deferring_access_checks (checks);
18893 decl = instantiate_template (fn, targs, complain);
18895 checks = get_deferred_access_checks ();
18896 pop_deferring_access_checks ();
18898 pop_tinst_level ();
18900 if (decl == error_mark_node)
18901 goto fail;
18903 /* Now perform any access checks encountered during substitution. */
18904 push_access_scope (decl);
18905 ok = perform_access_checks (checks, complain);
18906 pop_access_scope (decl);
18907 if (!ok)
18908 goto fail;
18910 /* If we're looking for an exact match, check that what we got
18911 is indeed an exact match. It might not be if some template
18912 parameters are used in non-deduced contexts. But don't check
18913 for an exact match if we have dependent template arguments;
18914 in that case we're doing partial ordering, and we already know
18915 that we have two candidates that will provide the actual type. */
18916 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18918 tree substed = TREE_TYPE (decl);
18919 unsigned int i;
18921 tree sarg
18922 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18923 if (return_type)
18924 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18925 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18926 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18928 unify_type_mismatch (explain_p, args[i],
18929 TREE_VALUE (sarg));
18930 goto fail;
18934 /* After doing deduction with the inherited constructor, actually return an
18935 instantiation of the inheriting constructor. */
18936 if (orig_fn != fn)
18937 decl = instantiate_template (orig_fn, targs, complain);
18939 r = decl;
18941 fail:
18942 --deduction_depth;
18943 if (excessive_deduction_depth)
18945 if (deduction_depth == 0)
18946 /* Reset once we're all the way out. */
18947 excessive_deduction_depth = false;
18950 /* We can't free this if a pending_template entry or last_error_tinst_level
18951 is pointing at it. */
18952 if (last_pending_template == old_last_pend
18953 && last_error_tinst_level == old_error_tinst)
18954 ggc_free (tinst);
18956 return r;
18959 /* Adjust types before performing type deduction, as described in
18960 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18961 sections are symmetric. PARM is the type of a function parameter
18962 or the return type of the conversion function. ARG is the type of
18963 the argument passed to the call, or the type of the value
18964 initialized with the result of the conversion function.
18965 ARG_EXPR is the original argument expression, which may be null. */
18967 static int
18968 maybe_adjust_types_for_deduction (unification_kind_t strict,
18969 tree* parm,
18970 tree* arg,
18971 tree arg_expr)
18973 int result = 0;
18975 switch (strict)
18977 case DEDUCE_CALL:
18978 break;
18980 case DEDUCE_CONV:
18981 /* Swap PARM and ARG throughout the remainder of this
18982 function; the handling is precisely symmetric since PARM
18983 will initialize ARG rather than vice versa. */
18984 std::swap (parm, arg);
18985 break;
18987 case DEDUCE_EXACT:
18988 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18989 too, but here handle it by stripping the reference from PARM
18990 rather than by adding it to ARG. */
18991 if (TREE_CODE (*parm) == REFERENCE_TYPE
18992 && TYPE_REF_IS_RVALUE (*parm)
18993 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18994 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18995 && TREE_CODE (*arg) == REFERENCE_TYPE
18996 && !TYPE_REF_IS_RVALUE (*arg))
18997 *parm = TREE_TYPE (*parm);
18998 /* Nothing else to do in this case. */
18999 return 0;
19001 default:
19002 gcc_unreachable ();
19005 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19007 /* [temp.deduct.call]
19009 If P is not a reference type:
19011 --If A is an array type, the pointer type produced by the
19012 array-to-pointer standard conversion (_conv.array_) is
19013 used in place of A for type deduction; otherwise,
19015 --If A is a function type, the pointer type produced by
19016 the function-to-pointer standard conversion
19017 (_conv.func_) is used in place of A for type deduction;
19018 otherwise,
19020 --If A is a cv-qualified type, the top level
19021 cv-qualifiers of A's type are ignored for type
19022 deduction. */
19023 if (TREE_CODE (*arg) == ARRAY_TYPE)
19024 *arg = build_pointer_type (TREE_TYPE (*arg));
19025 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19026 *arg = build_pointer_type (*arg);
19027 else
19028 *arg = TYPE_MAIN_VARIANT (*arg);
19031 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19032 reference to a cv-unqualified template parameter that does not represent a
19033 template parameter of a class template (during class template argument
19034 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19035 an lvalue, the type "lvalue reference to A" is used in place of A for type
19036 deduction. */
19037 if (TREE_CODE (*parm) == REFERENCE_TYPE
19038 && TYPE_REF_IS_RVALUE (*parm)
19039 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19040 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19041 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19042 && (arg_expr ? lvalue_p (arg_expr)
19043 /* try_one_overload doesn't provide an arg_expr, but
19044 functions are always lvalues. */
19045 : TREE_CODE (*arg) == FUNCTION_TYPE))
19046 *arg = build_reference_type (*arg);
19048 /* [temp.deduct.call]
19050 If P is a cv-qualified type, the top level cv-qualifiers
19051 of P's type are ignored for type deduction. If P is a
19052 reference type, the type referred to by P is used for
19053 type deduction. */
19054 *parm = TYPE_MAIN_VARIANT (*parm);
19055 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19057 *parm = TREE_TYPE (*parm);
19058 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19061 /* DR 322. For conversion deduction, remove a reference type on parm
19062 too (which has been swapped into ARG). */
19063 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19064 *arg = TREE_TYPE (*arg);
19066 return result;
19069 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19070 template which does contain any deducible template parameters; check if
19071 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19072 unify_one_argument. */
19074 static int
19075 check_non_deducible_conversion (tree parm, tree arg, int strict,
19076 int flags, bool explain_p)
19078 tree type;
19080 if (!TYPE_P (arg))
19081 type = TREE_TYPE (arg);
19082 else
19083 type = arg;
19085 if (same_type_p (parm, type))
19086 return unify_success (explain_p);
19088 if (strict == DEDUCE_CONV)
19090 if (can_convert_arg (type, parm, NULL_TREE, flags,
19091 explain_p ? tf_warning_or_error : tf_none))
19092 return unify_success (explain_p);
19094 else if (strict != DEDUCE_EXACT)
19096 if (can_convert_arg (parm, type,
19097 TYPE_P (arg) ? NULL_TREE : arg,
19098 flags, explain_p ? tf_warning_or_error : tf_none))
19099 return unify_success (explain_p);
19102 if (strict == DEDUCE_EXACT)
19103 return unify_type_mismatch (explain_p, parm, arg);
19104 else
19105 return unify_arg_conversion (explain_p, parm, type, arg);
19108 static bool uses_deducible_template_parms (tree type);
19110 /* Returns true iff the expression EXPR is one from which a template
19111 argument can be deduced. In other words, if it's an undecorated
19112 use of a template non-type parameter. */
19114 static bool
19115 deducible_expression (tree expr)
19117 /* Strip implicit conversions. */
19118 while (CONVERT_EXPR_P (expr))
19119 expr = TREE_OPERAND (expr, 0);
19120 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19123 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19124 deducible way; that is, if it has a max value of <PARM> - 1. */
19126 static bool
19127 deducible_array_bound (tree domain)
19129 if (domain == NULL_TREE)
19130 return false;
19132 tree max = TYPE_MAX_VALUE (domain);
19133 if (TREE_CODE (max) != MINUS_EXPR)
19134 return false;
19136 return deducible_expression (TREE_OPERAND (max, 0));
19139 /* Returns true iff the template arguments ARGS use a template parameter
19140 in a deducible way. */
19142 static bool
19143 deducible_template_args (tree args)
19145 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19147 bool deducible;
19148 tree elt = TREE_VEC_ELT (args, i);
19149 if (ARGUMENT_PACK_P (elt))
19150 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19151 else
19153 if (PACK_EXPANSION_P (elt))
19154 elt = PACK_EXPANSION_PATTERN (elt);
19155 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19156 deducible = true;
19157 else if (TYPE_P (elt))
19158 deducible = uses_deducible_template_parms (elt);
19159 else
19160 deducible = deducible_expression (elt);
19162 if (deducible)
19163 return true;
19165 return false;
19168 /* Returns true iff TYPE contains any deducible references to template
19169 parameters, as per 14.8.2.5. */
19171 static bool
19172 uses_deducible_template_parms (tree type)
19174 if (PACK_EXPANSION_P (type))
19175 type = PACK_EXPANSION_PATTERN (type);
19177 /* T
19178 cv-list T
19179 TT<T>
19180 TT<i>
19181 TT<> */
19182 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19183 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19184 return true;
19186 /* T*
19188 T&& */
19189 if (POINTER_TYPE_P (type))
19190 return uses_deducible_template_parms (TREE_TYPE (type));
19192 /* T[integer-constant ]
19193 type [i] */
19194 if (TREE_CODE (type) == ARRAY_TYPE)
19195 return (uses_deducible_template_parms (TREE_TYPE (type))
19196 || deducible_array_bound (TYPE_DOMAIN (type)));
19198 /* T type ::*
19199 type T::*
19200 T T::*
19201 T (type ::*)()
19202 type (T::*)()
19203 type (type ::*)(T)
19204 type (T::*)(T)
19205 T (type ::*)(T)
19206 T (T::*)()
19207 T (T::*)(T) */
19208 if (TYPE_PTRMEM_P (type))
19209 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19210 || (uses_deducible_template_parms
19211 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19213 /* template-name <T> (where template-name refers to a class template)
19214 template-name <i> (where template-name refers to a class template) */
19215 if (CLASS_TYPE_P (type)
19216 && CLASSTYPE_TEMPLATE_INFO (type)
19217 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19218 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19219 (CLASSTYPE_TI_ARGS (type)));
19221 /* type (T)
19223 T(T) */
19224 if (TREE_CODE (type) == FUNCTION_TYPE
19225 || TREE_CODE (type) == METHOD_TYPE)
19227 if (uses_deducible_template_parms (TREE_TYPE (type)))
19228 return true;
19229 tree parm = TYPE_ARG_TYPES (type);
19230 if (TREE_CODE (type) == METHOD_TYPE)
19231 parm = TREE_CHAIN (parm);
19232 for (; parm; parm = TREE_CHAIN (parm))
19233 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19234 return true;
19237 return false;
19240 /* Subroutine of type_unification_real and unify_pack_expansion to
19241 handle unification of a single P/A pair. Parameters are as
19242 for those functions. */
19244 static int
19245 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19246 int subr, unification_kind_t strict,
19247 bool explain_p)
19249 tree arg_expr = NULL_TREE;
19250 int arg_strict;
19252 if (arg == error_mark_node || parm == error_mark_node)
19253 return unify_invalid (explain_p);
19254 if (arg == unknown_type_node)
19255 /* We can't deduce anything from this, but we might get all the
19256 template args from other function args. */
19257 return unify_success (explain_p);
19259 /* Implicit conversions (Clause 4) will be performed on a function
19260 argument to convert it to the type of the corresponding function
19261 parameter if the parameter type contains no template-parameters that
19262 participate in template argument deduction. */
19263 if (strict != DEDUCE_EXACT
19264 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19265 /* For function parameters with no deducible template parameters,
19266 just return. We'll check non-dependent conversions later. */
19267 return unify_success (explain_p);
19269 switch (strict)
19271 case DEDUCE_CALL:
19272 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19273 | UNIFY_ALLOW_MORE_CV_QUAL
19274 | UNIFY_ALLOW_DERIVED);
19275 break;
19277 case DEDUCE_CONV:
19278 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19279 break;
19281 case DEDUCE_EXACT:
19282 arg_strict = UNIFY_ALLOW_NONE;
19283 break;
19285 default:
19286 gcc_unreachable ();
19289 /* We only do these transformations if this is the top-level
19290 parameter_type_list in a call or declaration matching; in other
19291 situations (nested function declarators, template argument lists) we
19292 won't be comparing a type to an expression, and we don't do any type
19293 adjustments. */
19294 if (!subr)
19296 if (!TYPE_P (arg))
19298 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19299 if (type_unknown_p (arg))
19301 /* [temp.deduct.type] A template-argument can be
19302 deduced from a pointer to function or pointer
19303 to member function argument if the set of
19304 overloaded functions does not contain function
19305 templates and at most one of a set of
19306 overloaded functions provides a unique
19307 match. */
19309 if (resolve_overloaded_unification
19310 (tparms, targs, parm, arg, strict,
19311 arg_strict, explain_p))
19312 return unify_success (explain_p);
19313 return unify_overload_resolution_failure (explain_p, arg);
19316 arg_expr = arg;
19317 arg = unlowered_expr_type (arg);
19318 if (arg == error_mark_node)
19319 return unify_invalid (explain_p);
19322 arg_strict |=
19323 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19325 else
19326 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19327 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19328 return unify_template_argument_mismatch (explain_p, parm, arg);
19330 /* For deduction from an init-list we need the actual list. */
19331 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19332 arg = arg_expr;
19333 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19336 /* for_each_template_parm callback that always returns 0. */
19338 static int
19339 zero_r (tree, void *)
19341 return 0;
19344 /* for_each_template_parm any_fn callback to handle deduction of a template
19345 type argument from the type of an array bound. */
19347 static int
19348 array_deduction_r (tree t, void *data)
19350 tree_pair_p d = (tree_pair_p)data;
19351 tree &tparms = d->purpose;
19352 tree &targs = d->value;
19354 if (TREE_CODE (t) == ARRAY_TYPE)
19355 if (tree dom = TYPE_DOMAIN (t))
19356 if (tree max = TYPE_MAX_VALUE (dom))
19358 if (TREE_CODE (max) == MINUS_EXPR)
19359 max = TREE_OPERAND (max, 0);
19360 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19361 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19362 UNIFY_ALLOW_NONE, /*explain*/false);
19365 /* Keep walking. */
19366 return 0;
19369 /* Try to deduce any not-yet-deduced template type arguments from the type of
19370 an array bound. This is handled separately from unify because 14.8.2.5 says
19371 "The type of a type parameter is only deduced from an array bound if it is
19372 not otherwise deduced." */
19374 static void
19375 try_array_deduction (tree tparms, tree targs, tree parm)
19377 tree_pair_s data = { tparms, targs };
19378 hash_set<tree> visited;
19379 for_each_template_parm (parm, zero_r, &data, &visited,
19380 /*nondeduced*/false, array_deduction_r);
19383 /* Most parms like fn_type_unification.
19385 If SUBR is 1, we're being called recursively (to unify the
19386 arguments of a function or method parameter of a function
19387 template).
19389 CHECKS is a pointer to a vector of access checks encountered while
19390 substituting default template arguments. */
19392 static int
19393 type_unification_real (tree tparms,
19394 tree full_targs,
19395 tree xparms,
19396 const tree *xargs,
19397 unsigned int xnargs,
19398 int subr,
19399 unification_kind_t strict,
19400 int flags,
19401 vec<deferred_access_check, va_gc> **checks,
19402 bool explain_p)
19404 tree parm, arg;
19405 int i;
19406 int ntparms = TREE_VEC_LENGTH (tparms);
19407 int saw_undeduced = 0;
19408 tree parms;
19409 const tree *args;
19410 unsigned int nargs;
19411 unsigned int ia;
19413 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19414 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19415 gcc_assert (ntparms > 0);
19417 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19419 /* Reset the number of non-defaulted template arguments contained
19420 in TARGS. */
19421 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19423 again:
19424 parms = xparms;
19425 args = xargs;
19426 nargs = xnargs;
19428 ia = 0;
19429 while (parms && parms != void_list_node
19430 && ia < nargs)
19432 parm = TREE_VALUE (parms);
19434 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19435 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19436 /* For a function parameter pack that occurs at the end of the
19437 parameter-declaration-list, the type A of each remaining
19438 argument of the call is compared with the type P of the
19439 declarator-id of the function parameter pack. */
19440 break;
19442 parms = TREE_CHAIN (parms);
19444 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19445 /* For a function parameter pack that does not occur at the
19446 end of the parameter-declaration-list, the type of the
19447 parameter pack is a non-deduced context. */
19448 continue;
19450 arg = args[ia];
19451 ++ia;
19453 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19454 explain_p))
19455 return 1;
19458 if (parms
19459 && parms != void_list_node
19460 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19462 /* Unify the remaining arguments with the pack expansion type. */
19463 tree argvec;
19464 tree parmvec = make_tree_vec (1);
19466 /* Allocate a TREE_VEC and copy in all of the arguments */
19467 argvec = make_tree_vec (nargs - ia);
19468 for (i = 0; ia < nargs; ++ia, ++i)
19469 TREE_VEC_ELT (argvec, i) = args[ia];
19471 /* Copy the parameter into parmvec. */
19472 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19473 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19474 /*subr=*/subr, explain_p))
19475 return 1;
19477 /* Advance to the end of the list of parameters. */
19478 parms = TREE_CHAIN (parms);
19481 /* Fail if we've reached the end of the parm list, and more args
19482 are present, and the parm list isn't variadic. */
19483 if (ia < nargs && parms == void_list_node)
19484 return unify_too_many_arguments (explain_p, nargs, ia);
19485 /* Fail if parms are left and they don't have default values and
19486 they aren't all deduced as empty packs (c++/57397). This is
19487 consistent with sufficient_parms_p. */
19488 if (parms && parms != void_list_node
19489 && TREE_PURPOSE (parms) == NULL_TREE)
19491 unsigned int count = nargs;
19492 tree p = parms;
19493 bool type_pack_p;
19496 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19497 if (!type_pack_p)
19498 count++;
19499 p = TREE_CHAIN (p);
19501 while (p && p != void_list_node);
19502 if (count != nargs)
19503 return unify_too_few_arguments (explain_p, ia, count,
19504 type_pack_p);
19507 if (!subr)
19509 tsubst_flags_t complain = (explain_p
19510 ? tf_warning_or_error
19511 : tf_none);
19512 bool tried_array_deduction = (cxx_dialect < cxx1z);
19514 for (i = 0; i < ntparms; i++)
19516 tree targ = TREE_VEC_ELT (targs, i);
19517 tree tparm = TREE_VEC_ELT (tparms, i);
19519 /* Clear the "incomplete" flags on all argument packs now so that
19520 substituting them into later default arguments works. */
19521 if (targ && ARGUMENT_PACK_P (targ))
19523 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19524 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19527 if (targ || tparm == error_mark_node)
19528 continue;
19529 tparm = TREE_VALUE (tparm);
19531 if (TREE_CODE (tparm) == TYPE_DECL
19532 && !tried_array_deduction)
19534 try_array_deduction (tparms, targs, xparms);
19535 tried_array_deduction = true;
19536 if (TREE_VEC_ELT (targs, i))
19537 continue;
19540 /* If this is an undeduced nontype parameter that depends on
19541 a type parameter, try another pass; its type may have been
19542 deduced from a later argument than the one from which
19543 this parameter can be deduced. */
19544 if (TREE_CODE (tparm) == PARM_DECL
19545 && uses_template_parms (TREE_TYPE (tparm))
19546 && saw_undeduced < 2)
19548 saw_undeduced = 1;
19549 continue;
19552 /* Core issue #226 (C++0x) [temp.deduct]:
19554 If a template argument has not been deduced, its
19555 default template argument, if any, is used.
19557 When we are in C++98 mode, TREE_PURPOSE will either
19558 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19559 to explicitly check cxx_dialect here. */
19560 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19561 /* OK, there is a default argument. Wait until after the
19562 conversion check to do substitution. */
19563 continue;
19565 /* If the type parameter is a parameter pack, then it will
19566 be deduced to an empty parameter pack. */
19567 if (template_parameter_pack_p (tparm))
19569 tree arg;
19571 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19573 arg = make_node (NONTYPE_ARGUMENT_PACK);
19574 TREE_CONSTANT (arg) = 1;
19576 else
19577 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19579 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19581 TREE_VEC_ELT (targs, i) = arg;
19582 continue;
19585 return unify_parameter_deduction_failure (explain_p, tparm);
19588 /* DR 1391: All parameters have args, now check non-dependent parms for
19589 convertibility. */
19590 if (saw_undeduced < 2)
19591 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19592 parms && parms != void_list_node && ia < nargs; )
19594 parm = TREE_VALUE (parms);
19596 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19597 && (!TREE_CHAIN (parms)
19598 || TREE_CHAIN (parms) == void_list_node))
19599 /* For a function parameter pack that occurs at the end of the
19600 parameter-declaration-list, the type A of each remaining
19601 argument of the call is compared with the type P of the
19602 declarator-id of the function parameter pack. */
19603 break;
19605 parms = TREE_CHAIN (parms);
19607 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19608 /* For a function parameter pack that does not occur at the
19609 end of the parameter-declaration-list, the type of the
19610 parameter pack is a non-deduced context. */
19611 continue;
19613 arg = args[ia];
19614 ++ia;
19616 if (uses_template_parms (parm))
19617 continue;
19618 if (check_non_deducible_conversion (parm, arg, strict, flags,
19619 explain_p))
19620 return 1;
19623 /* Now substitute into the default template arguments. */
19624 for (i = 0; i < ntparms; i++)
19626 tree targ = TREE_VEC_ELT (targs, i);
19627 tree tparm = TREE_VEC_ELT (tparms, i);
19629 if (targ || tparm == error_mark_node)
19630 continue;
19631 tree parm = TREE_VALUE (tparm);
19633 if (TREE_CODE (parm) == PARM_DECL
19634 && uses_template_parms (TREE_TYPE (parm))
19635 && saw_undeduced < 2)
19636 continue;
19638 tree arg = TREE_PURPOSE (tparm);
19639 reopen_deferring_access_checks (*checks);
19640 location_t save_loc = input_location;
19641 if (DECL_P (parm))
19642 input_location = DECL_SOURCE_LOCATION (parm);
19643 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19644 if (!uses_template_parms (arg))
19645 arg = convert_template_argument (parm, arg, full_targs, complain,
19646 i, NULL_TREE);
19647 else if (saw_undeduced < 2)
19648 arg = NULL_TREE;
19649 else
19650 arg = error_mark_node;
19651 input_location = save_loc;
19652 *checks = get_deferred_access_checks ();
19653 pop_deferring_access_checks ();
19654 if (arg == error_mark_node)
19655 return 1;
19656 else if (arg)
19658 TREE_VEC_ELT (targs, i) = arg;
19659 /* The position of the first default template argument,
19660 is also the number of non-defaulted arguments in TARGS.
19661 Record that. */
19662 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19663 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19667 if (saw_undeduced++ == 1)
19668 goto again;
19671 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19672 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19674 return unify_success (explain_p);
19677 /* Subroutine of type_unification_real. Args are like the variables
19678 at the call site. ARG is an overloaded function (or template-id);
19679 we try deducing template args from each of the overloads, and if
19680 only one succeeds, we go with that. Modifies TARGS and returns
19681 true on success. */
19683 static bool
19684 resolve_overloaded_unification (tree tparms,
19685 tree targs,
19686 tree parm,
19687 tree arg,
19688 unification_kind_t strict,
19689 int sub_strict,
19690 bool explain_p)
19692 tree tempargs = copy_node (targs);
19693 int good = 0;
19694 tree goodfn = NULL_TREE;
19695 bool addr_p;
19697 if (TREE_CODE (arg) == ADDR_EXPR)
19699 arg = TREE_OPERAND (arg, 0);
19700 addr_p = true;
19702 else
19703 addr_p = false;
19705 if (TREE_CODE (arg) == COMPONENT_REF)
19706 /* Handle `&x' where `x' is some static or non-static member
19707 function name. */
19708 arg = TREE_OPERAND (arg, 1);
19710 if (TREE_CODE (arg) == OFFSET_REF)
19711 arg = TREE_OPERAND (arg, 1);
19713 /* Strip baselink information. */
19714 if (BASELINK_P (arg))
19715 arg = BASELINK_FUNCTIONS (arg);
19717 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19719 /* If we got some explicit template args, we need to plug them into
19720 the affected templates before we try to unify, in case the
19721 explicit args will completely resolve the templates in question. */
19723 int ok = 0;
19724 tree expl_subargs = TREE_OPERAND (arg, 1);
19725 arg = TREE_OPERAND (arg, 0);
19727 for (lkp_iterator iter (arg); iter; ++iter)
19729 tree fn = *iter;
19730 tree subargs, elem;
19732 if (TREE_CODE (fn) != TEMPLATE_DECL)
19733 continue;
19735 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19736 expl_subargs, NULL_TREE, tf_none,
19737 /*require_all_args=*/true,
19738 /*use_default_args=*/true);
19739 if (subargs != error_mark_node
19740 && !any_dependent_template_arguments_p (subargs))
19742 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19743 if (try_one_overload (tparms, targs, tempargs, parm,
19744 elem, strict, sub_strict, addr_p, explain_p)
19745 && (!goodfn || !same_type_p (goodfn, elem)))
19747 goodfn = elem;
19748 ++good;
19751 else if (subargs)
19752 ++ok;
19754 /* If no templates (or more than one) are fully resolved by the
19755 explicit arguments, this template-id is a non-deduced context; it
19756 could still be OK if we deduce all template arguments for the
19757 enclosing call through other arguments. */
19758 if (good != 1)
19759 good = ok;
19761 else if (TREE_CODE (arg) != OVERLOAD
19762 && TREE_CODE (arg) != FUNCTION_DECL)
19763 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19764 -- but the deduction does not succeed because the expression is
19765 not just the function on its own. */
19766 return false;
19767 else
19768 for (lkp_iterator iter (arg); iter; ++iter)
19770 tree fn = *iter;
19771 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19772 strict, sub_strict, addr_p, explain_p)
19773 && (!goodfn || !decls_match (goodfn, fn)))
19775 goodfn = fn;
19776 ++good;
19780 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19781 to function or pointer to member function argument if the set of
19782 overloaded functions does not contain function templates and at most
19783 one of a set of overloaded functions provides a unique match.
19785 So if we found multiple possibilities, we return success but don't
19786 deduce anything. */
19788 if (good == 1)
19790 int i = TREE_VEC_LENGTH (targs);
19791 for (; i--; )
19792 if (TREE_VEC_ELT (tempargs, i))
19794 tree old = TREE_VEC_ELT (targs, i);
19795 tree new_ = TREE_VEC_ELT (tempargs, i);
19796 if (new_ && old && ARGUMENT_PACK_P (old)
19797 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19798 /* Don't forget explicit template arguments in a pack. */
19799 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19800 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19801 TREE_VEC_ELT (targs, i) = new_;
19804 if (good)
19805 return true;
19807 return false;
19810 /* Core DR 115: In contexts where deduction is done and fails, or in
19811 contexts where deduction is not done, if a template argument list is
19812 specified and it, along with any default template arguments, identifies
19813 a single function template specialization, then the template-id is an
19814 lvalue for the function template specialization. */
19816 tree
19817 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19819 tree expr, offset, baselink;
19820 bool addr;
19822 if (!type_unknown_p (orig_expr))
19823 return orig_expr;
19825 expr = orig_expr;
19826 addr = false;
19827 offset = NULL_TREE;
19828 baselink = NULL_TREE;
19830 if (TREE_CODE (expr) == ADDR_EXPR)
19832 expr = TREE_OPERAND (expr, 0);
19833 addr = true;
19835 if (TREE_CODE (expr) == OFFSET_REF)
19837 offset = expr;
19838 expr = TREE_OPERAND (expr, 1);
19840 if (BASELINK_P (expr))
19842 baselink = expr;
19843 expr = BASELINK_FUNCTIONS (expr);
19846 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19848 int good = 0;
19849 tree goodfn = NULL_TREE;
19851 /* If we got some explicit template args, we need to plug them into
19852 the affected templates before we try to unify, in case the
19853 explicit args will completely resolve the templates in question. */
19855 tree expl_subargs = TREE_OPERAND (expr, 1);
19856 tree arg = TREE_OPERAND (expr, 0);
19857 tree badfn = NULL_TREE;
19858 tree badargs = NULL_TREE;
19860 for (lkp_iterator iter (arg); iter; ++iter)
19862 tree fn = *iter;
19863 tree subargs, elem;
19865 if (TREE_CODE (fn) != TEMPLATE_DECL)
19866 continue;
19868 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19869 expl_subargs, NULL_TREE, tf_none,
19870 /*require_all_args=*/true,
19871 /*use_default_args=*/true);
19872 if (subargs != error_mark_node
19873 && !any_dependent_template_arguments_p (subargs))
19875 elem = instantiate_template (fn, subargs, tf_none);
19876 if (elem == error_mark_node)
19878 badfn = fn;
19879 badargs = subargs;
19881 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19883 goodfn = elem;
19884 ++good;
19888 if (good == 1)
19890 mark_used (goodfn);
19891 expr = goodfn;
19892 if (baselink)
19893 expr = build_baselink (BASELINK_BINFO (baselink),
19894 BASELINK_ACCESS_BINFO (baselink),
19895 expr, BASELINK_OPTYPE (baselink));
19896 if (offset)
19898 tree base
19899 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19900 expr = build_offset_ref (base, expr, addr, complain);
19902 if (addr)
19903 expr = cp_build_addr_expr (expr, complain);
19904 return expr;
19906 else if (good == 0 && badargs && (complain & tf_error))
19907 /* There were no good options and at least one bad one, so let the
19908 user know what the problem is. */
19909 instantiate_template (badfn, badargs, complain);
19911 return orig_expr;
19914 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19915 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19916 different overloads deduce different arguments for a given parm.
19917 ADDR_P is true if the expression for which deduction is being
19918 performed was of the form "& fn" rather than simply "fn".
19920 Returns 1 on success. */
19922 static int
19923 try_one_overload (tree tparms,
19924 tree orig_targs,
19925 tree targs,
19926 tree parm,
19927 tree arg,
19928 unification_kind_t strict,
19929 int sub_strict,
19930 bool addr_p,
19931 bool explain_p)
19933 int nargs;
19934 tree tempargs;
19935 int i;
19937 if (arg == error_mark_node)
19938 return 0;
19940 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19941 to function or pointer to member function argument if the set of
19942 overloaded functions does not contain function templates and at most
19943 one of a set of overloaded functions provides a unique match.
19945 So if this is a template, just return success. */
19947 if (uses_template_parms (arg))
19948 return 1;
19950 if (TREE_CODE (arg) == METHOD_TYPE)
19951 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19952 else if (addr_p)
19953 arg = build_pointer_type (arg);
19955 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19957 /* We don't copy orig_targs for this because if we have already deduced
19958 some template args from previous args, unify would complain when we
19959 try to deduce a template parameter for the same argument, even though
19960 there isn't really a conflict. */
19961 nargs = TREE_VEC_LENGTH (targs);
19962 tempargs = make_tree_vec (nargs);
19964 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19965 return 0;
19967 /* First make sure we didn't deduce anything that conflicts with
19968 explicitly specified args. */
19969 for (i = nargs; i--; )
19971 tree elt = TREE_VEC_ELT (tempargs, i);
19972 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19974 if (!elt)
19975 /*NOP*/;
19976 else if (uses_template_parms (elt))
19977 /* Since we're unifying against ourselves, we will fill in
19978 template args used in the function parm list with our own
19979 template parms. Discard them. */
19980 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19981 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19983 /* Check that the argument at each index of the deduced argument pack
19984 is equivalent to the corresponding explicitly specified argument.
19985 We may have deduced more arguments than were explicitly specified,
19986 and that's OK. */
19988 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19989 that's wrong if we deduce the same argument pack from multiple
19990 function arguments: it's only incomplete the first time. */
19992 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19993 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19995 if (TREE_VEC_LENGTH (deduced_pack)
19996 < TREE_VEC_LENGTH (explicit_pack))
19997 return 0;
19999 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20000 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20001 TREE_VEC_ELT (deduced_pack, j)))
20002 return 0;
20004 else if (oldelt && !template_args_equal (oldelt, elt))
20005 return 0;
20008 for (i = nargs; i--; )
20010 tree elt = TREE_VEC_ELT (tempargs, i);
20012 if (elt)
20013 TREE_VEC_ELT (targs, i) = elt;
20016 return 1;
20019 /* PARM is a template class (perhaps with unbound template
20020 parameters). ARG is a fully instantiated type. If ARG can be
20021 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20022 TARGS are as for unify. */
20024 static tree
20025 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20026 bool explain_p)
20028 tree copy_of_targs;
20030 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20031 return NULL_TREE;
20032 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20033 /* Matches anything. */;
20034 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20035 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20036 return NULL_TREE;
20038 /* We need to make a new template argument vector for the call to
20039 unify. If we used TARGS, we'd clutter it up with the result of
20040 the attempted unification, even if this class didn't work out.
20041 We also don't want to commit ourselves to all the unifications
20042 we've already done, since unification is supposed to be done on
20043 an argument-by-argument basis. In other words, consider the
20044 following pathological case:
20046 template <int I, int J, int K>
20047 struct S {};
20049 template <int I, int J>
20050 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20052 template <int I, int J, int K>
20053 void f(S<I, J, K>, S<I, I, I>);
20055 void g() {
20056 S<0, 0, 0> s0;
20057 S<0, 1, 2> s2;
20059 f(s0, s2);
20062 Now, by the time we consider the unification involving `s2', we
20063 already know that we must have `f<0, 0, 0>'. But, even though
20064 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20065 because there are two ways to unify base classes of S<0, 1, 2>
20066 with S<I, I, I>. If we kept the already deduced knowledge, we
20067 would reject the possibility I=1. */
20068 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20070 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20072 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20073 return NULL_TREE;
20074 return arg;
20077 /* If unification failed, we're done. */
20078 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20079 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20080 return NULL_TREE;
20082 return arg;
20085 /* Given a template type PARM and a class type ARG, find the unique
20086 base type in ARG that is an instance of PARM. We do not examine
20087 ARG itself; only its base-classes. If there is not exactly one
20088 appropriate base class, return NULL_TREE. PARM may be the type of
20089 a partial specialization, as well as a plain template type. Used
20090 by unify. */
20092 static enum template_base_result
20093 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20094 bool explain_p, tree *result)
20096 tree rval = NULL_TREE;
20097 tree binfo;
20099 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20101 binfo = TYPE_BINFO (complete_type (arg));
20102 if (!binfo)
20104 /* The type could not be completed. */
20105 *result = NULL_TREE;
20106 return tbr_incomplete_type;
20109 /* Walk in inheritance graph order. The search order is not
20110 important, and this avoids multiple walks of virtual bases. */
20111 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20113 tree r = try_class_unification (tparms, targs, parm,
20114 BINFO_TYPE (binfo), explain_p);
20116 if (r)
20118 /* If there is more than one satisfactory baseclass, then:
20120 [temp.deduct.call]
20122 If they yield more than one possible deduced A, the type
20123 deduction fails.
20125 applies. */
20126 if (rval && !same_type_p (r, rval))
20128 *result = NULL_TREE;
20129 return tbr_ambiguous_baseclass;
20132 rval = r;
20136 *result = rval;
20137 return tbr_success;
20140 /* Returns the level of DECL, which declares a template parameter. */
20142 static int
20143 template_decl_level (tree decl)
20145 switch (TREE_CODE (decl))
20147 case TYPE_DECL:
20148 case TEMPLATE_DECL:
20149 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20151 case PARM_DECL:
20152 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20154 default:
20155 gcc_unreachable ();
20157 return 0;
20160 /* Decide whether ARG can be unified with PARM, considering only the
20161 cv-qualifiers of each type, given STRICT as documented for unify.
20162 Returns nonzero iff the unification is OK on that basis. */
20164 static int
20165 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20167 int arg_quals = cp_type_quals (arg);
20168 int parm_quals = cp_type_quals (parm);
20170 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20171 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20173 /* Although a CVR qualifier is ignored when being applied to a
20174 substituted template parameter ([8.3.2]/1 for example), that
20175 does not allow us to unify "const T" with "int&" because both
20176 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20177 It is ok when we're allowing additional CV qualifiers
20178 at the outer level [14.8.2.1]/3,1st bullet. */
20179 if ((TREE_CODE (arg) == REFERENCE_TYPE
20180 || TREE_CODE (arg) == FUNCTION_TYPE
20181 || TREE_CODE (arg) == METHOD_TYPE)
20182 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20183 return 0;
20185 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20186 && (parm_quals & TYPE_QUAL_RESTRICT))
20187 return 0;
20190 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20191 && (arg_quals & parm_quals) != parm_quals)
20192 return 0;
20194 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20195 && (parm_quals & arg_quals) != arg_quals)
20196 return 0;
20198 return 1;
20201 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20202 void
20203 template_parm_level_and_index (tree parm, int* level, int* index)
20205 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20206 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20207 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20209 *index = TEMPLATE_TYPE_IDX (parm);
20210 *level = TEMPLATE_TYPE_LEVEL (parm);
20212 else
20214 *index = TEMPLATE_PARM_IDX (parm);
20215 *level = TEMPLATE_PARM_LEVEL (parm);
20219 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20220 do { \
20221 if (unify (TP, TA, P, A, S, EP)) \
20222 return 1; \
20223 } while (0)
20225 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20226 expansion at the end of PACKED_PARMS. Returns 0 if the type
20227 deduction succeeds, 1 otherwise. STRICT is the same as in
20228 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20229 function call argument list. We'll need to adjust the arguments to make them
20230 types. SUBR tells us if this is from a recursive call to
20231 type_unification_real, or for comparing two template argument
20232 lists. */
20234 static int
20235 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20236 tree packed_args, unification_kind_t strict,
20237 bool subr, bool explain_p)
20239 tree parm
20240 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20241 tree pattern = PACK_EXPANSION_PATTERN (parm);
20242 tree pack, packs = NULL_TREE;
20243 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20245 /* Add in any args remembered from an earlier partial instantiation. */
20246 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20248 packed_args = expand_template_argument_pack (packed_args);
20250 int len = TREE_VEC_LENGTH (packed_args);
20252 /* Determine the parameter packs we will be deducing from the
20253 pattern, and record their current deductions. */
20254 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20255 pack; pack = TREE_CHAIN (pack))
20257 tree parm_pack = TREE_VALUE (pack);
20258 int idx, level;
20260 /* Determine the index and level of this parameter pack. */
20261 template_parm_level_and_index (parm_pack, &level, &idx);
20263 /* Keep track of the parameter packs and their corresponding
20264 argument packs. */
20265 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20266 TREE_TYPE (packs) = make_tree_vec (len - start);
20269 /* Loop through all of the arguments that have not yet been
20270 unified and unify each with the pattern. */
20271 for (i = start; i < len; i++)
20273 tree parm;
20274 bool any_explicit = false;
20275 tree arg = TREE_VEC_ELT (packed_args, i);
20277 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20278 or the element of its argument pack at the current index if
20279 this argument was explicitly specified. */
20280 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20282 int idx, level;
20283 tree arg, pargs;
20284 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20286 arg = NULL_TREE;
20287 if (TREE_VALUE (pack)
20288 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20289 && (i - start < TREE_VEC_LENGTH (pargs)))
20291 any_explicit = true;
20292 arg = TREE_VEC_ELT (pargs, i - start);
20294 TMPL_ARG (targs, level, idx) = arg;
20297 /* If we had explicit template arguments, substitute them into the
20298 pattern before deduction. */
20299 if (any_explicit)
20301 /* Some arguments might still be unspecified or dependent. */
20302 bool dependent;
20303 ++processing_template_decl;
20304 dependent = any_dependent_template_arguments_p (targs);
20305 if (!dependent)
20306 --processing_template_decl;
20307 parm = tsubst (pattern, targs,
20308 explain_p ? tf_warning_or_error : tf_none,
20309 NULL_TREE);
20310 if (dependent)
20311 --processing_template_decl;
20312 if (parm == error_mark_node)
20313 return 1;
20315 else
20316 parm = pattern;
20318 /* Unify the pattern with the current argument. */
20319 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20320 explain_p))
20321 return 1;
20323 /* For each parameter pack, collect the deduced value. */
20324 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20326 int idx, level;
20327 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20329 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20330 TMPL_ARG (targs, level, idx);
20334 /* Verify that the results of unification with the parameter packs
20335 produce results consistent with what we've seen before, and make
20336 the deduced argument packs available. */
20337 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20339 tree old_pack = TREE_VALUE (pack);
20340 tree new_args = TREE_TYPE (pack);
20341 int i, len = TREE_VEC_LENGTH (new_args);
20342 int idx, level;
20343 bool nondeduced_p = false;
20345 /* By default keep the original deduced argument pack.
20346 If necessary, more specific code is going to update the
20347 resulting deduced argument later down in this function. */
20348 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20349 TMPL_ARG (targs, level, idx) = old_pack;
20351 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20352 actually deduce anything. */
20353 for (i = 0; i < len && !nondeduced_p; ++i)
20354 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20355 nondeduced_p = true;
20356 if (nondeduced_p)
20357 continue;
20359 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20361 /* If we had fewer function args than explicit template args,
20362 just use the explicits. */
20363 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20364 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20365 if (len < explicit_len)
20366 new_args = explicit_args;
20369 if (!old_pack)
20371 tree result;
20372 /* Build the deduced *_ARGUMENT_PACK. */
20373 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20375 result = make_node (NONTYPE_ARGUMENT_PACK);
20376 TREE_CONSTANT (result) = 1;
20378 else
20379 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20381 SET_ARGUMENT_PACK_ARGS (result, new_args);
20383 /* Note the deduced argument packs for this parameter
20384 pack. */
20385 TMPL_ARG (targs, level, idx) = result;
20387 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20388 && (ARGUMENT_PACK_ARGS (old_pack)
20389 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20391 /* We only had the explicitly-provided arguments before, but
20392 now we have a complete set of arguments. */
20393 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20395 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20396 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20397 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20399 else
20401 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20402 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20404 if (!comp_template_args (old_args, new_args,
20405 &bad_old_arg, &bad_new_arg))
20406 /* Inconsistent unification of this parameter pack. */
20407 return unify_parameter_pack_inconsistent (explain_p,
20408 bad_old_arg,
20409 bad_new_arg);
20413 return unify_success (explain_p);
20416 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20417 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20418 parameters and return value are as for unify. */
20420 static int
20421 unify_array_domain (tree tparms, tree targs,
20422 tree parm_dom, tree arg_dom,
20423 bool explain_p)
20425 tree parm_max;
20426 tree arg_max;
20427 bool parm_cst;
20428 bool arg_cst;
20430 /* Our representation of array types uses "N - 1" as the
20431 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20432 not an integer constant. We cannot unify arbitrarily
20433 complex expressions, so we eliminate the MINUS_EXPRs
20434 here. */
20435 parm_max = TYPE_MAX_VALUE (parm_dom);
20436 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20437 if (!parm_cst)
20439 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20440 parm_max = TREE_OPERAND (parm_max, 0);
20442 arg_max = TYPE_MAX_VALUE (arg_dom);
20443 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20444 if (!arg_cst)
20446 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20447 trying to unify the type of a variable with the type
20448 of a template parameter. For example:
20450 template <unsigned int N>
20451 void f (char (&) [N]);
20452 int g();
20453 void h(int i) {
20454 char a[g(i)];
20455 f(a);
20458 Here, the type of the ARG will be "int [g(i)]", and
20459 may be a SAVE_EXPR, etc. */
20460 if (TREE_CODE (arg_max) != MINUS_EXPR)
20461 return unify_vla_arg (explain_p, arg_dom);
20462 arg_max = TREE_OPERAND (arg_max, 0);
20465 /* If only one of the bounds used a MINUS_EXPR, compensate
20466 by adding one to the other bound. */
20467 if (parm_cst && !arg_cst)
20468 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20469 integer_type_node,
20470 parm_max,
20471 integer_one_node);
20472 else if (arg_cst && !parm_cst)
20473 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20474 integer_type_node,
20475 arg_max,
20476 integer_one_node);
20478 return unify (tparms, targs, parm_max, arg_max,
20479 UNIFY_ALLOW_INTEGER, explain_p);
20482 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20484 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20486 static pa_kind_t
20487 pa_kind (tree t)
20489 if (PACK_EXPANSION_P (t))
20490 t = PACK_EXPANSION_PATTERN (t);
20491 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20492 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20493 || DECL_TYPE_TEMPLATE_P (t))
20494 return pa_tmpl;
20495 else if (TYPE_P (t))
20496 return pa_type;
20497 else
20498 return pa_expr;
20501 /* Deduce the value of template parameters. TPARMS is the (innermost)
20502 set of template parameters to a template. TARGS is the bindings
20503 for those template parameters, as determined thus far; TARGS may
20504 include template arguments for outer levels of template parameters
20505 as well. PARM is a parameter to a template function, or a
20506 subcomponent of that parameter; ARG is the corresponding argument.
20507 This function attempts to match PARM with ARG in a manner
20508 consistent with the existing assignments in TARGS. If more values
20509 are deduced, then TARGS is updated.
20511 Returns 0 if the type deduction succeeds, 1 otherwise. The
20512 parameter STRICT is a bitwise or of the following flags:
20514 UNIFY_ALLOW_NONE:
20515 Require an exact match between PARM and ARG.
20516 UNIFY_ALLOW_MORE_CV_QUAL:
20517 Allow the deduced ARG to be more cv-qualified (by qualification
20518 conversion) than ARG.
20519 UNIFY_ALLOW_LESS_CV_QUAL:
20520 Allow the deduced ARG to be less cv-qualified than ARG.
20521 UNIFY_ALLOW_DERIVED:
20522 Allow the deduced ARG to be a template base class of ARG,
20523 or a pointer to a template base class of the type pointed to by
20524 ARG.
20525 UNIFY_ALLOW_INTEGER:
20526 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20527 case for more information.
20528 UNIFY_ALLOW_OUTER_LEVEL:
20529 This is the outermost level of a deduction. Used to determine validity
20530 of qualification conversions. A valid qualification conversion must
20531 have const qualified pointers leading up to the inner type which
20532 requires additional CV quals, except at the outer level, where const
20533 is not required [conv.qual]. It would be normal to set this flag in
20534 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20535 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20536 This is the outermost level of a deduction, and PARM can be more CV
20537 qualified at this point.
20538 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20539 This is the outermost level of a deduction, and PARM can be less CV
20540 qualified at this point. */
20542 static int
20543 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20544 bool explain_p)
20546 int idx;
20547 tree targ;
20548 tree tparm;
20549 int strict_in = strict;
20550 tsubst_flags_t complain = (explain_p
20551 ? tf_warning_or_error
20552 : tf_none);
20554 /* I don't think this will do the right thing with respect to types.
20555 But the only case I've seen it in so far has been array bounds, where
20556 signedness is the only information lost, and I think that will be
20557 okay. */
20558 while (CONVERT_EXPR_P (parm))
20559 parm = TREE_OPERAND (parm, 0);
20561 if (arg == error_mark_node)
20562 return unify_invalid (explain_p);
20563 if (arg == unknown_type_node
20564 || arg == init_list_type_node)
20565 /* We can't deduce anything from this, but we might get all the
20566 template args from other function args. */
20567 return unify_success (explain_p);
20569 if (parm == any_targ_node || arg == any_targ_node)
20570 return unify_success (explain_p);
20572 /* If PARM uses template parameters, then we can't bail out here,
20573 even if ARG == PARM, since we won't record unifications for the
20574 template parameters. We might need them if we're trying to
20575 figure out which of two things is more specialized. */
20576 if (arg == parm && !uses_template_parms (parm))
20577 return unify_success (explain_p);
20579 /* Handle init lists early, so the rest of the function can assume
20580 we're dealing with a type. */
20581 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20583 tree elt, elttype;
20584 unsigned i;
20585 tree orig_parm = parm;
20587 /* Replace T with std::initializer_list<T> for deduction. */
20588 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20589 && flag_deduce_init_list)
20590 parm = listify (parm);
20592 if (!is_std_init_list (parm)
20593 && TREE_CODE (parm) != ARRAY_TYPE)
20594 /* We can only deduce from an initializer list argument if the
20595 parameter is std::initializer_list or an array; otherwise this
20596 is a non-deduced context. */
20597 return unify_success (explain_p);
20599 if (TREE_CODE (parm) == ARRAY_TYPE)
20600 elttype = TREE_TYPE (parm);
20601 else
20603 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20604 /* Deduction is defined in terms of a single type, so just punt
20605 on the (bizarre) std::initializer_list<T...>. */
20606 if (PACK_EXPANSION_P (elttype))
20607 return unify_success (explain_p);
20610 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20612 int elt_strict = strict;
20614 if (elt == error_mark_node)
20615 return unify_invalid (explain_p);
20617 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20619 tree type = TREE_TYPE (elt);
20620 if (type == error_mark_node)
20621 return unify_invalid (explain_p);
20622 /* It should only be possible to get here for a call. */
20623 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20624 elt_strict |= maybe_adjust_types_for_deduction
20625 (DEDUCE_CALL, &elttype, &type, elt);
20626 elt = type;
20629 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20630 explain_p);
20633 if (TREE_CODE (parm) == ARRAY_TYPE
20634 && deducible_array_bound (TYPE_DOMAIN (parm)))
20636 /* Also deduce from the length of the initializer list. */
20637 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20638 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20639 if (idx == error_mark_node)
20640 return unify_invalid (explain_p);
20641 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20642 idx, explain_p);
20645 /* If the std::initializer_list<T> deduction worked, replace the
20646 deduced A with std::initializer_list<A>. */
20647 if (orig_parm != parm)
20649 idx = TEMPLATE_TYPE_IDX (orig_parm);
20650 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20651 targ = listify (targ);
20652 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20654 return unify_success (explain_p);
20657 /* If parm and arg aren't the same kind of thing (template, type, or
20658 expression), fail early. */
20659 if (pa_kind (parm) != pa_kind (arg))
20660 return unify_invalid (explain_p);
20662 /* Immediately reject some pairs that won't unify because of
20663 cv-qualification mismatches. */
20664 if (TREE_CODE (arg) == TREE_CODE (parm)
20665 && TYPE_P (arg)
20666 /* It is the elements of the array which hold the cv quals of an array
20667 type, and the elements might be template type parms. We'll check
20668 when we recurse. */
20669 && TREE_CODE (arg) != ARRAY_TYPE
20670 /* We check the cv-qualifiers when unifying with template type
20671 parameters below. We want to allow ARG `const T' to unify with
20672 PARM `T' for example, when computing which of two templates
20673 is more specialized, for example. */
20674 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20675 && !check_cv_quals_for_unify (strict_in, arg, parm))
20676 return unify_cv_qual_mismatch (explain_p, parm, arg);
20678 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20679 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20680 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20681 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20682 strict &= ~UNIFY_ALLOW_DERIVED;
20683 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20684 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20686 switch (TREE_CODE (parm))
20688 case TYPENAME_TYPE:
20689 case SCOPE_REF:
20690 case UNBOUND_CLASS_TEMPLATE:
20691 /* In a type which contains a nested-name-specifier, template
20692 argument values cannot be deduced for template parameters used
20693 within the nested-name-specifier. */
20694 return unify_success (explain_p);
20696 case TEMPLATE_TYPE_PARM:
20697 case TEMPLATE_TEMPLATE_PARM:
20698 case BOUND_TEMPLATE_TEMPLATE_PARM:
20699 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20700 if (error_operand_p (tparm))
20701 return unify_invalid (explain_p);
20703 if (TEMPLATE_TYPE_LEVEL (parm)
20704 != template_decl_level (tparm))
20705 /* The PARM is not one we're trying to unify. Just check
20706 to see if it matches ARG. */
20708 if (TREE_CODE (arg) == TREE_CODE (parm)
20709 && (is_auto (parm) ? is_auto (arg)
20710 : same_type_p (parm, arg)))
20711 return unify_success (explain_p);
20712 else
20713 return unify_type_mismatch (explain_p, parm, arg);
20715 idx = TEMPLATE_TYPE_IDX (parm);
20716 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20717 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20718 if (error_operand_p (tparm))
20719 return unify_invalid (explain_p);
20721 /* Check for mixed types and values. */
20722 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20723 && TREE_CODE (tparm) != TYPE_DECL)
20724 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20725 && TREE_CODE (tparm) != TEMPLATE_DECL))
20726 gcc_unreachable ();
20728 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20730 if ((strict_in & UNIFY_ALLOW_DERIVED)
20731 && CLASS_TYPE_P (arg))
20733 /* First try to match ARG directly. */
20734 tree t = try_class_unification (tparms, targs, parm, arg,
20735 explain_p);
20736 if (!t)
20738 /* Otherwise, look for a suitable base of ARG, as below. */
20739 enum template_base_result r;
20740 r = get_template_base (tparms, targs, parm, arg,
20741 explain_p, &t);
20742 if (!t)
20743 return unify_no_common_base (explain_p, r, parm, arg);
20744 arg = t;
20747 /* ARG must be constructed from a template class or a template
20748 template parameter. */
20749 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20750 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20751 return unify_template_deduction_failure (explain_p, parm, arg);
20753 /* Deduce arguments T, i from TT<T> or TT<i>. */
20754 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20755 return 1;
20757 arg = TYPE_TI_TEMPLATE (arg);
20759 /* Fall through to deduce template name. */
20762 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20763 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20765 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20767 /* Simple cases: Value already set, does match or doesn't. */
20768 if (targ != NULL_TREE && template_args_equal (targ, arg))
20769 return unify_success (explain_p);
20770 else if (targ)
20771 return unify_inconsistency (explain_p, parm, targ, arg);
20773 else
20775 /* If PARM is `const T' and ARG is only `int', we don't have
20776 a match unless we are allowing additional qualification.
20777 If ARG is `const int' and PARM is just `T' that's OK;
20778 that binds `const int' to `T'. */
20779 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20780 arg, parm))
20781 return unify_cv_qual_mismatch (explain_p, parm, arg);
20783 /* Consider the case where ARG is `const volatile int' and
20784 PARM is `const T'. Then, T should be `volatile int'. */
20785 arg = cp_build_qualified_type_real
20786 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20787 if (arg == error_mark_node)
20788 return unify_invalid (explain_p);
20790 /* Simple cases: Value already set, does match or doesn't. */
20791 if (targ != NULL_TREE && same_type_p (targ, arg))
20792 return unify_success (explain_p);
20793 else if (targ)
20794 return unify_inconsistency (explain_p, parm, targ, arg);
20796 /* Make sure that ARG is not a variable-sized array. (Note
20797 that were talking about variable-sized arrays (like
20798 `int[n]'), rather than arrays of unknown size (like
20799 `int[]').) We'll get very confused by such a type since
20800 the bound of the array is not constant, and therefore
20801 not mangleable. Besides, such types are not allowed in
20802 ISO C++, so we can do as we please here. We do allow
20803 them for 'auto' deduction, since that isn't ABI-exposed. */
20804 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20805 return unify_vla_arg (explain_p, arg);
20807 /* Strip typedefs as in convert_template_argument. */
20808 arg = canonicalize_type_argument (arg, tf_none);
20811 /* If ARG is a parameter pack or an expansion, we cannot unify
20812 against it unless PARM is also a parameter pack. */
20813 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20814 && !template_parameter_pack_p (parm))
20815 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20817 /* If the argument deduction results is a METHOD_TYPE,
20818 then there is a problem.
20819 METHOD_TYPE doesn't map to any real C++ type the result of
20820 the deduction can not be of that type. */
20821 if (TREE_CODE (arg) == METHOD_TYPE)
20822 return unify_method_type_error (explain_p, arg);
20824 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20825 return unify_success (explain_p);
20827 case TEMPLATE_PARM_INDEX:
20828 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20829 if (error_operand_p (tparm))
20830 return unify_invalid (explain_p);
20832 if (TEMPLATE_PARM_LEVEL (parm)
20833 != template_decl_level (tparm))
20835 /* The PARM is not one we're trying to unify. Just check
20836 to see if it matches ARG. */
20837 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20838 && cp_tree_equal (parm, arg));
20839 if (result)
20840 unify_expression_unequal (explain_p, parm, arg);
20841 return result;
20844 idx = TEMPLATE_PARM_IDX (parm);
20845 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20847 if (targ)
20849 int x = !cp_tree_equal (targ, arg);
20850 if (x)
20851 unify_inconsistency (explain_p, parm, targ, arg);
20852 return x;
20855 /* [temp.deduct.type] If, in the declaration of a function template
20856 with a non-type template-parameter, the non-type
20857 template-parameter is used in an expression in the function
20858 parameter-list and, if the corresponding template-argument is
20859 deduced, the template-argument type shall match the type of the
20860 template-parameter exactly, except that a template-argument
20861 deduced from an array bound may be of any integral type.
20862 The non-type parameter might use already deduced type parameters. */
20863 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20864 if (tree a = type_uses_auto (tparm))
20866 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20867 if (tparm == error_mark_node)
20868 return 1;
20871 if (!TREE_TYPE (arg))
20872 /* Template-parameter dependent expression. Just accept it for now.
20873 It will later be processed in convert_template_argument. */
20875 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20876 non_reference (tparm)))
20877 /* OK */;
20878 else if ((strict & UNIFY_ALLOW_INTEGER)
20879 && CP_INTEGRAL_TYPE_P (tparm))
20880 /* Convert the ARG to the type of PARM; the deduced non-type
20881 template argument must exactly match the types of the
20882 corresponding parameter. */
20883 arg = fold (build_nop (tparm, arg));
20884 else if (uses_template_parms (tparm))
20886 /* We haven't deduced the type of this parameter yet. */
20887 if (cxx_dialect >= cxx1z
20888 /* We deduce from array bounds in try_array_deduction. */
20889 && !(strict & UNIFY_ALLOW_INTEGER))
20891 /* Deduce it from the non-type argument. */
20892 tree atype = TREE_TYPE (arg);
20893 RECUR_AND_CHECK_FAILURE (tparms, targs,
20894 tparm, atype,
20895 UNIFY_ALLOW_NONE, explain_p);
20897 else
20898 /* Try again later. */
20899 return unify_success (explain_p);
20901 else
20902 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20904 /* If ARG is a parameter pack or an expansion, we cannot unify
20905 against it unless PARM is also a parameter pack. */
20906 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20907 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20908 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20911 bool removed_attr = false;
20912 arg = strip_typedefs_expr (arg, &removed_attr);
20914 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20915 return unify_success (explain_p);
20917 case PTRMEM_CST:
20919 /* A pointer-to-member constant can be unified only with
20920 another constant. */
20921 if (TREE_CODE (arg) != PTRMEM_CST)
20922 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20924 /* Just unify the class member. It would be useless (and possibly
20925 wrong, depending on the strict flags) to unify also
20926 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20927 arg refer to the same variable, even if through different
20928 classes. For instance:
20930 struct A { int x; };
20931 struct B : A { };
20933 Unification of &A::x and &B::x must succeed. */
20934 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20935 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20938 case POINTER_TYPE:
20940 if (!TYPE_PTR_P (arg))
20941 return unify_type_mismatch (explain_p, parm, arg);
20943 /* [temp.deduct.call]
20945 A can be another pointer or pointer to member type that can
20946 be converted to the deduced A via a qualification
20947 conversion (_conv.qual_).
20949 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20950 This will allow for additional cv-qualification of the
20951 pointed-to types if appropriate. */
20953 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20954 /* The derived-to-base conversion only persists through one
20955 level of pointers. */
20956 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20958 return unify (tparms, targs, TREE_TYPE (parm),
20959 TREE_TYPE (arg), strict, explain_p);
20962 case REFERENCE_TYPE:
20963 if (TREE_CODE (arg) != REFERENCE_TYPE)
20964 return unify_type_mismatch (explain_p, parm, arg);
20965 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20966 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20968 case ARRAY_TYPE:
20969 if (TREE_CODE (arg) != ARRAY_TYPE)
20970 return unify_type_mismatch (explain_p, parm, arg);
20971 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20972 != (TYPE_DOMAIN (arg) == NULL_TREE))
20973 return unify_type_mismatch (explain_p, parm, arg);
20974 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20975 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20976 if (TYPE_DOMAIN (parm) != NULL_TREE)
20977 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20978 TYPE_DOMAIN (arg), explain_p);
20979 return unify_success (explain_p);
20981 case REAL_TYPE:
20982 case COMPLEX_TYPE:
20983 case VECTOR_TYPE:
20984 case INTEGER_TYPE:
20985 case BOOLEAN_TYPE:
20986 case ENUMERAL_TYPE:
20987 case VOID_TYPE:
20988 case NULLPTR_TYPE:
20989 if (TREE_CODE (arg) != TREE_CODE (parm))
20990 return unify_type_mismatch (explain_p, parm, arg);
20992 /* We have already checked cv-qualification at the top of the
20993 function. */
20994 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20995 return unify_type_mismatch (explain_p, parm, arg);
20997 /* As far as unification is concerned, this wins. Later checks
20998 will invalidate it if necessary. */
20999 return unify_success (explain_p);
21001 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21002 /* Type INTEGER_CST can come from ordinary constant template args. */
21003 case INTEGER_CST:
21004 while (CONVERT_EXPR_P (arg))
21005 arg = TREE_OPERAND (arg, 0);
21007 if (TREE_CODE (arg) != INTEGER_CST)
21008 return unify_template_argument_mismatch (explain_p, parm, arg);
21009 return (tree_int_cst_equal (parm, arg)
21010 ? unify_success (explain_p)
21011 : unify_template_argument_mismatch (explain_p, parm, arg));
21013 case TREE_VEC:
21015 int i, len, argslen;
21016 int parm_variadic_p = 0;
21018 if (TREE_CODE (arg) != TREE_VEC)
21019 return unify_template_argument_mismatch (explain_p, parm, arg);
21021 len = TREE_VEC_LENGTH (parm);
21022 argslen = TREE_VEC_LENGTH (arg);
21024 /* Check for pack expansions in the parameters. */
21025 for (i = 0; i < len; ++i)
21027 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21029 if (i == len - 1)
21030 /* We can unify against something with a trailing
21031 parameter pack. */
21032 parm_variadic_p = 1;
21033 else
21034 /* [temp.deduct.type]/9: If the template argument list of
21035 P contains a pack expansion that is not the last
21036 template argument, the entire template argument list
21037 is a non-deduced context. */
21038 return unify_success (explain_p);
21042 /* If we don't have enough arguments to satisfy the parameters
21043 (not counting the pack expression at the end), or we have
21044 too many arguments for a parameter list that doesn't end in
21045 a pack expression, we can't unify. */
21046 if (parm_variadic_p
21047 ? argslen < len - parm_variadic_p
21048 : argslen != len)
21049 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21051 /* Unify all of the parameters that precede the (optional)
21052 pack expression. */
21053 for (i = 0; i < len - parm_variadic_p; ++i)
21055 RECUR_AND_CHECK_FAILURE (tparms, targs,
21056 TREE_VEC_ELT (parm, i),
21057 TREE_VEC_ELT (arg, i),
21058 UNIFY_ALLOW_NONE, explain_p);
21060 if (parm_variadic_p)
21061 return unify_pack_expansion (tparms, targs, parm, arg,
21062 DEDUCE_EXACT,
21063 /*subr=*/true, explain_p);
21064 return unify_success (explain_p);
21067 case RECORD_TYPE:
21068 case UNION_TYPE:
21069 if (TREE_CODE (arg) != TREE_CODE (parm))
21070 return unify_type_mismatch (explain_p, parm, arg);
21072 if (TYPE_PTRMEMFUNC_P (parm))
21074 if (!TYPE_PTRMEMFUNC_P (arg))
21075 return unify_type_mismatch (explain_p, parm, arg);
21077 return unify (tparms, targs,
21078 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21079 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21080 strict, explain_p);
21082 else if (TYPE_PTRMEMFUNC_P (arg))
21083 return unify_type_mismatch (explain_p, parm, arg);
21085 if (CLASSTYPE_TEMPLATE_INFO (parm))
21087 tree t = NULL_TREE;
21089 if (strict_in & UNIFY_ALLOW_DERIVED)
21091 /* First, we try to unify the PARM and ARG directly. */
21092 t = try_class_unification (tparms, targs,
21093 parm, arg, explain_p);
21095 if (!t)
21097 /* Fallback to the special case allowed in
21098 [temp.deduct.call]:
21100 If P is a class, and P has the form
21101 template-id, then A can be a derived class of
21102 the deduced A. Likewise, if P is a pointer to
21103 a class of the form template-id, A can be a
21104 pointer to a derived class pointed to by the
21105 deduced A. */
21106 enum template_base_result r;
21107 r = get_template_base (tparms, targs, parm, arg,
21108 explain_p, &t);
21110 if (!t)
21112 /* Don't give the derived diagnostic if we're
21113 already dealing with the same template. */
21114 bool same_template
21115 = (CLASSTYPE_TEMPLATE_INFO (arg)
21116 && (CLASSTYPE_TI_TEMPLATE (parm)
21117 == CLASSTYPE_TI_TEMPLATE (arg)));
21118 return unify_no_common_base (explain_p && !same_template,
21119 r, parm, arg);
21123 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21124 && (CLASSTYPE_TI_TEMPLATE (parm)
21125 == CLASSTYPE_TI_TEMPLATE (arg)))
21126 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21127 Then, we should unify `int' and `U'. */
21128 t = arg;
21129 else
21130 /* There's no chance of unification succeeding. */
21131 return unify_type_mismatch (explain_p, parm, arg);
21133 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21134 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21136 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21137 return unify_type_mismatch (explain_p, parm, arg);
21138 return unify_success (explain_p);
21140 case METHOD_TYPE:
21141 case FUNCTION_TYPE:
21143 unsigned int nargs;
21144 tree *args;
21145 tree a;
21146 unsigned int i;
21148 if (TREE_CODE (arg) != TREE_CODE (parm))
21149 return unify_type_mismatch (explain_p, parm, arg);
21151 /* CV qualifications for methods can never be deduced, they must
21152 match exactly. We need to check them explicitly here,
21153 because type_unification_real treats them as any other
21154 cv-qualified parameter. */
21155 if (TREE_CODE (parm) == METHOD_TYPE
21156 && (!check_cv_quals_for_unify
21157 (UNIFY_ALLOW_NONE,
21158 class_of_this_parm (arg),
21159 class_of_this_parm (parm))))
21160 return unify_cv_qual_mismatch (explain_p, parm, arg);
21161 if (TREE_CODE (arg) == FUNCTION_TYPE
21162 && type_memfn_quals (parm) != type_memfn_quals (arg))
21163 return unify_cv_qual_mismatch (explain_p, parm, arg);
21164 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21165 return unify_type_mismatch (explain_p, parm, arg);
21167 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21168 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21170 nargs = list_length (TYPE_ARG_TYPES (arg));
21171 args = XALLOCAVEC (tree, nargs);
21172 for (a = TYPE_ARG_TYPES (arg), i = 0;
21173 a != NULL_TREE && a != void_list_node;
21174 a = TREE_CHAIN (a), ++i)
21175 args[i] = TREE_VALUE (a);
21176 nargs = i;
21178 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21179 args, nargs, 1, DEDUCE_EXACT,
21180 LOOKUP_NORMAL, NULL, explain_p))
21181 return 1;
21183 if (flag_noexcept_type)
21185 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21186 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21187 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21188 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21189 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21190 && uses_template_parms (TREE_PURPOSE (pspec)))
21191 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21192 TREE_PURPOSE (aspec),
21193 UNIFY_ALLOW_NONE, explain_p);
21194 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21195 return unify_type_mismatch (explain_p, parm, arg);
21198 return 0;
21201 case OFFSET_TYPE:
21202 /* Unify a pointer to member with a pointer to member function, which
21203 deduces the type of the member as a function type. */
21204 if (TYPE_PTRMEMFUNC_P (arg))
21206 /* Check top-level cv qualifiers */
21207 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21208 return unify_cv_qual_mismatch (explain_p, parm, arg);
21210 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21211 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21212 UNIFY_ALLOW_NONE, explain_p);
21214 /* Determine the type of the function we are unifying against. */
21215 tree fntype = static_fn_type (arg);
21217 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21220 if (TREE_CODE (arg) != OFFSET_TYPE)
21221 return unify_type_mismatch (explain_p, parm, arg);
21222 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21223 TYPE_OFFSET_BASETYPE (arg),
21224 UNIFY_ALLOW_NONE, explain_p);
21225 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21226 strict, explain_p);
21228 case CONST_DECL:
21229 if (DECL_TEMPLATE_PARM_P (parm))
21230 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21231 if (arg != scalar_constant_value (parm))
21232 return unify_template_argument_mismatch (explain_p, parm, arg);
21233 return unify_success (explain_p);
21235 case FIELD_DECL:
21236 case TEMPLATE_DECL:
21237 /* Matched cases are handled by the ARG == PARM test above. */
21238 return unify_template_argument_mismatch (explain_p, parm, arg);
21240 case VAR_DECL:
21241 /* We might get a variable as a non-type template argument in parm if the
21242 corresponding parameter is type-dependent. Make any necessary
21243 adjustments based on whether arg is a reference. */
21244 if (CONSTANT_CLASS_P (arg))
21245 parm = fold_non_dependent_expr (parm);
21246 else if (REFERENCE_REF_P (arg))
21248 tree sub = TREE_OPERAND (arg, 0);
21249 STRIP_NOPS (sub);
21250 if (TREE_CODE (sub) == ADDR_EXPR)
21251 arg = TREE_OPERAND (sub, 0);
21253 /* Now use the normal expression code to check whether they match. */
21254 goto expr;
21256 case TYPE_ARGUMENT_PACK:
21257 case NONTYPE_ARGUMENT_PACK:
21258 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21259 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21261 case TYPEOF_TYPE:
21262 case DECLTYPE_TYPE:
21263 case UNDERLYING_TYPE:
21264 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21265 or UNDERLYING_TYPE nodes. */
21266 return unify_success (explain_p);
21268 case ERROR_MARK:
21269 /* Unification fails if we hit an error node. */
21270 return unify_invalid (explain_p);
21272 case INDIRECT_REF:
21273 if (REFERENCE_REF_P (parm))
21275 bool pexp = PACK_EXPANSION_P (arg);
21276 if (pexp)
21277 arg = PACK_EXPANSION_PATTERN (arg);
21278 if (REFERENCE_REF_P (arg))
21279 arg = TREE_OPERAND (arg, 0);
21280 if (pexp)
21281 arg = make_pack_expansion (arg);
21282 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21283 strict, explain_p);
21285 /* FALLTHRU */
21287 default:
21288 /* An unresolved overload is a nondeduced context. */
21289 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21290 return unify_success (explain_p);
21291 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21292 expr:
21293 /* We must be looking at an expression. This can happen with
21294 something like:
21296 template <int I>
21297 void foo(S<I>, S<I + 2>);
21299 This is a "nondeduced context":
21301 [deduct.type]
21303 The nondeduced contexts are:
21305 --A type that is a template-id in which one or more of
21306 the template-arguments is an expression that references
21307 a template-parameter.
21309 In these cases, we assume deduction succeeded, but don't
21310 actually infer any unifications. */
21312 if (!uses_template_parms (parm)
21313 && !template_args_equal (parm, arg))
21314 return unify_expression_unequal (explain_p, parm, arg);
21315 else
21316 return unify_success (explain_p);
21319 #undef RECUR_AND_CHECK_FAILURE
21321 /* Note that DECL can be defined in this translation unit, if
21322 required. */
21324 static void
21325 mark_definable (tree decl)
21327 tree clone;
21328 DECL_NOT_REALLY_EXTERN (decl) = 1;
21329 FOR_EACH_CLONE (clone, decl)
21330 DECL_NOT_REALLY_EXTERN (clone) = 1;
21333 /* Called if RESULT is explicitly instantiated, or is a member of an
21334 explicitly instantiated class. */
21336 void
21337 mark_decl_instantiated (tree result, int extern_p)
21339 SET_DECL_EXPLICIT_INSTANTIATION (result);
21341 /* If this entity has already been written out, it's too late to
21342 make any modifications. */
21343 if (TREE_ASM_WRITTEN (result))
21344 return;
21346 /* For anonymous namespace we don't need to do anything. */
21347 if (decl_anon_ns_mem_p (result))
21349 gcc_assert (!TREE_PUBLIC (result));
21350 return;
21353 if (TREE_CODE (result) != FUNCTION_DECL)
21354 /* The TREE_PUBLIC flag for function declarations will have been
21355 set correctly by tsubst. */
21356 TREE_PUBLIC (result) = 1;
21358 /* This might have been set by an earlier implicit instantiation. */
21359 DECL_COMDAT (result) = 0;
21361 if (extern_p)
21362 DECL_NOT_REALLY_EXTERN (result) = 0;
21363 else
21365 mark_definable (result);
21366 mark_needed (result);
21367 /* Always make artificials weak. */
21368 if (DECL_ARTIFICIAL (result) && flag_weak)
21369 comdat_linkage (result);
21370 /* For WIN32 we also want to put explicit instantiations in
21371 linkonce sections. */
21372 else if (TREE_PUBLIC (result))
21373 maybe_make_one_only (result);
21376 /* If EXTERN_P, then this function will not be emitted -- unless
21377 followed by an explicit instantiation, at which point its linkage
21378 will be adjusted. If !EXTERN_P, then this function will be
21379 emitted here. In neither circumstance do we want
21380 import_export_decl to adjust the linkage. */
21381 DECL_INTERFACE_KNOWN (result) = 1;
21384 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21385 important template arguments. If any are missing, we check whether
21386 they're important by using error_mark_node for substituting into any
21387 args that were used for partial ordering (the ones between ARGS and END)
21388 and seeing if it bubbles up. */
21390 static bool
21391 check_undeduced_parms (tree targs, tree args, tree end)
21393 bool found = false;
21394 int i;
21395 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21396 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21398 found = true;
21399 TREE_VEC_ELT (targs, i) = error_mark_node;
21401 if (found)
21403 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21404 if (substed == error_mark_node)
21405 return true;
21407 return false;
21410 /* Given two function templates PAT1 and PAT2, return:
21412 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21413 -1 if PAT2 is more specialized than PAT1.
21414 0 if neither is more specialized.
21416 LEN indicates the number of parameters we should consider
21417 (defaulted parameters should not be considered).
21419 The 1998 std underspecified function template partial ordering, and
21420 DR214 addresses the issue. We take pairs of arguments, one from
21421 each of the templates, and deduce them against each other. One of
21422 the templates will be more specialized if all the *other*
21423 template's arguments deduce against its arguments and at least one
21424 of its arguments *does* *not* deduce against the other template's
21425 corresponding argument. Deduction is done as for class templates.
21426 The arguments used in deduction have reference and top level cv
21427 qualifiers removed. Iff both arguments were originally reference
21428 types *and* deduction succeeds in both directions, an lvalue reference
21429 wins against an rvalue reference and otherwise the template
21430 with the more cv-qualified argument wins for that pairing (if
21431 neither is more cv-qualified, they both are equal). Unlike regular
21432 deduction, after all the arguments have been deduced in this way,
21433 we do *not* verify the deduced template argument values can be
21434 substituted into non-deduced contexts.
21436 The logic can be a bit confusing here, because we look at deduce1 and
21437 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21438 can find template arguments for pat1 to make arg1 look like arg2, that
21439 means that arg2 is at least as specialized as arg1. */
21442 more_specialized_fn (tree pat1, tree pat2, int len)
21444 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21445 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21446 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21447 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21448 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21449 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21450 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21451 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21452 tree origs1, origs2;
21453 bool lose1 = false;
21454 bool lose2 = false;
21456 /* Remove the this parameter from non-static member functions. If
21457 one is a non-static member function and the other is not a static
21458 member function, remove the first parameter from that function
21459 also. This situation occurs for operator functions where we
21460 locate both a member function (with this pointer) and non-member
21461 operator (with explicit first operand). */
21462 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21464 len--; /* LEN is the number of significant arguments for DECL1 */
21465 args1 = TREE_CHAIN (args1);
21466 if (!DECL_STATIC_FUNCTION_P (decl2))
21467 args2 = TREE_CHAIN (args2);
21469 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21471 args2 = TREE_CHAIN (args2);
21472 if (!DECL_STATIC_FUNCTION_P (decl1))
21474 len--;
21475 args1 = TREE_CHAIN (args1);
21479 /* If only one is a conversion operator, they are unordered. */
21480 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21481 return 0;
21483 /* Consider the return type for a conversion function */
21484 if (DECL_CONV_FN_P (decl1))
21486 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21487 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21488 len++;
21491 processing_template_decl++;
21493 origs1 = args1;
21494 origs2 = args2;
21496 while (len--
21497 /* Stop when an ellipsis is seen. */
21498 && args1 != NULL_TREE && args2 != NULL_TREE)
21500 tree arg1 = TREE_VALUE (args1);
21501 tree arg2 = TREE_VALUE (args2);
21502 int deduce1, deduce2;
21503 int quals1 = -1;
21504 int quals2 = -1;
21505 int ref1 = 0;
21506 int ref2 = 0;
21508 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21509 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21511 /* When both arguments are pack expansions, we need only
21512 unify the patterns themselves. */
21513 arg1 = PACK_EXPANSION_PATTERN (arg1);
21514 arg2 = PACK_EXPANSION_PATTERN (arg2);
21516 /* This is the last comparison we need to do. */
21517 len = 0;
21520 /* DR 1847: If a particular P contains no template-parameters that
21521 participate in template argument deduction, that P is not used to
21522 determine the ordering. */
21523 if (!uses_deducible_template_parms (arg1)
21524 && !uses_deducible_template_parms (arg2))
21525 goto next;
21527 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21529 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21530 arg1 = TREE_TYPE (arg1);
21531 quals1 = cp_type_quals (arg1);
21534 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21536 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21537 arg2 = TREE_TYPE (arg2);
21538 quals2 = cp_type_quals (arg2);
21541 arg1 = TYPE_MAIN_VARIANT (arg1);
21542 arg2 = TYPE_MAIN_VARIANT (arg2);
21544 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21546 int i, len2 = remaining_arguments (args2);
21547 tree parmvec = make_tree_vec (1);
21548 tree argvec = make_tree_vec (len2);
21549 tree ta = args2;
21551 /* Setup the parameter vector, which contains only ARG1. */
21552 TREE_VEC_ELT (parmvec, 0) = arg1;
21554 /* Setup the argument vector, which contains the remaining
21555 arguments. */
21556 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21557 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21559 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21560 argvec, DEDUCE_EXACT,
21561 /*subr=*/true, /*explain_p=*/false)
21562 == 0);
21564 /* We cannot deduce in the other direction, because ARG1 is
21565 a pack expansion but ARG2 is not. */
21566 deduce2 = 0;
21568 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21570 int i, len1 = remaining_arguments (args1);
21571 tree parmvec = make_tree_vec (1);
21572 tree argvec = make_tree_vec (len1);
21573 tree ta = args1;
21575 /* Setup the parameter vector, which contains only ARG1. */
21576 TREE_VEC_ELT (parmvec, 0) = arg2;
21578 /* Setup the argument vector, which contains the remaining
21579 arguments. */
21580 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21581 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21583 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21584 argvec, DEDUCE_EXACT,
21585 /*subr=*/true, /*explain_p=*/false)
21586 == 0);
21588 /* We cannot deduce in the other direction, because ARG2 is
21589 a pack expansion but ARG1 is not.*/
21590 deduce1 = 0;
21593 else
21595 /* The normal case, where neither argument is a pack
21596 expansion. */
21597 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21598 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21599 == 0);
21600 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21601 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21602 == 0);
21605 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21606 arg2, then arg2 is not as specialized as arg1. */
21607 if (!deduce1)
21608 lose2 = true;
21609 if (!deduce2)
21610 lose1 = true;
21612 /* "If, for a given type, deduction succeeds in both directions
21613 (i.e., the types are identical after the transformations above)
21614 and both P and A were reference types (before being replaced with
21615 the type referred to above):
21616 - if the type from the argument template was an lvalue reference and
21617 the type from the parameter template was not, the argument type is
21618 considered to be more specialized than the other; otherwise,
21619 - if the type from the argument template is more cv-qualified
21620 than the type from the parameter template (as described above),
21621 the argument type is considered to be more specialized than the other;
21622 otherwise,
21623 - neither type is more specialized than the other." */
21625 if (deduce1 && deduce2)
21627 if (ref1 && ref2 && ref1 != ref2)
21629 if (ref1 > ref2)
21630 lose1 = true;
21631 else
21632 lose2 = true;
21634 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21636 if ((quals1 & quals2) == quals2)
21637 lose2 = true;
21638 if ((quals1 & quals2) == quals1)
21639 lose1 = true;
21643 if (lose1 && lose2)
21644 /* We've failed to deduce something in either direction.
21645 These must be unordered. */
21646 break;
21648 next:
21650 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21651 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21652 /* We have already processed all of the arguments in our
21653 handing of the pack expansion type. */
21654 len = 0;
21656 args1 = TREE_CHAIN (args1);
21657 args2 = TREE_CHAIN (args2);
21660 /* "In most cases, all template parameters must have values in order for
21661 deduction to succeed, but for partial ordering purposes a template
21662 parameter may remain without a value provided it is not used in the
21663 types being used for partial ordering."
21665 Thus, if we are missing any of the targs1 we need to substitute into
21666 origs1, then pat2 is not as specialized as pat1. This can happen when
21667 there is a nondeduced context. */
21668 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21669 lose2 = true;
21670 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21671 lose1 = true;
21673 processing_template_decl--;
21675 /* If both deductions succeed, the partial ordering selects the more
21676 constrained template. */
21677 if (!lose1 && !lose2)
21679 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21680 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21681 lose1 = !subsumes_constraints (c1, c2);
21682 lose2 = !subsumes_constraints (c2, c1);
21685 /* All things being equal, if the next argument is a pack expansion
21686 for one function but not for the other, prefer the
21687 non-variadic function. FIXME this is bogus; see c++/41958. */
21688 if (lose1 == lose2
21689 && args1 && TREE_VALUE (args1)
21690 && args2 && TREE_VALUE (args2))
21692 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21693 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21696 if (lose1 == lose2)
21697 return 0;
21698 else if (!lose1)
21699 return 1;
21700 else
21701 return -1;
21704 /* Determine which of two partial specializations of TMPL is more
21705 specialized.
21707 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21708 to the first partial specialization. The TREE_PURPOSE is the
21709 innermost set of template parameters for the partial
21710 specialization. PAT2 is similar, but for the second template.
21712 Return 1 if the first partial specialization is more specialized;
21713 -1 if the second is more specialized; 0 if neither is more
21714 specialized.
21716 See [temp.class.order] for information about determining which of
21717 two templates is more specialized. */
21719 static int
21720 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21722 tree targs;
21723 int winner = 0;
21724 bool any_deductions = false;
21726 tree tmpl1 = TREE_VALUE (pat1);
21727 tree tmpl2 = TREE_VALUE (pat2);
21728 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21729 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21731 /* Just like what happens for functions, if we are ordering between
21732 different template specializations, we may encounter dependent
21733 types in the arguments, and we need our dependency check functions
21734 to behave correctly. */
21735 ++processing_template_decl;
21736 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21737 if (targs)
21739 --winner;
21740 any_deductions = true;
21743 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21744 if (targs)
21746 ++winner;
21747 any_deductions = true;
21749 --processing_template_decl;
21751 /* If both deductions succeed, the partial ordering selects the more
21752 constrained template. */
21753 if (!winner && any_deductions)
21754 return more_constrained (tmpl1, tmpl2);
21756 /* In the case of a tie where at least one of the templates
21757 has a parameter pack at the end, the template with the most
21758 non-packed parameters wins. */
21759 if (winner == 0
21760 && any_deductions
21761 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21762 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21764 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21765 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21766 int len1 = TREE_VEC_LENGTH (args1);
21767 int len2 = TREE_VEC_LENGTH (args2);
21769 /* We don't count the pack expansion at the end. */
21770 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21771 --len1;
21772 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21773 --len2;
21775 if (len1 > len2)
21776 return 1;
21777 else if (len1 < len2)
21778 return -1;
21781 return winner;
21784 /* Return the template arguments that will produce the function signature
21785 DECL from the function template FN, with the explicit template
21786 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21787 also match. Return NULL_TREE if no satisfactory arguments could be
21788 found. */
21790 static tree
21791 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21793 int ntparms = DECL_NTPARMS (fn);
21794 tree targs = make_tree_vec (ntparms);
21795 tree decl_type = TREE_TYPE (decl);
21796 tree decl_arg_types;
21797 tree *args;
21798 unsigned int nargs, ix;
21799 tree arg;
21801 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21803 /* Never do unification on the 'this' parameter. */
21804 decl_arg_types = skip_artificial_parms_for (decl,
21805 TYPE_ARG_TYPES (decl_type));
21807 nargs = list_length (decl_arg_types);
21808 args = XALLOCAVEC (tree, nargs);
21809 for (arg = decl_arg_types, ix = 0;
21810 arg != NULL_TREE && arg != void_list_node;
21811 arg = TREE_CHAIN (arg), ++ix)
21812 args[ix] = TREE_VALUE (arg);
21814 if (fn_type_unification (fn, explicit_args, targs,
21815 args, ix,
21816 (check_rettype || DECL_CONV_FN_P (fn)
21817 ? TREE_TYPE (decl_type) : NULL_TREE),
21818 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21819 /*decltype*/false)
21820 == error_mark_node)
21821 return NULL_TREE;
21823 return targs;
21826 /* Return the innermost template arguments that, when applied to a partial
21827 specialization SPEC_TMPL of TMPL, yield the ARGS.
21829 For example, suppose we have:
21831 template <class T, class U> struct S {};
21832 template <class T> struct S<T*, int> {};
21834 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21835 partial specialization and the ARGS will be {double*, int}. The resulting
21836 vector will be {double}, indicating that `T' is bound to `double'. */
21838 static tree
21839 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21841 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21842 tree spec_args
21843 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21844 int i, ntparms = TREE_VEC_LENGTH (tparms);
21845 tree deduced_args;
21846 tree innermost_deduced_args;
21848 innermost_deduced_args = make_tree_vec (ntparms);
21849 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21851 deduced_args = copy_node (args);
21852 SET_TMPL_ARGS_LEVEL (deduced_args,
21853 TMPL_ARGS_DEPTH (deduced_args),
21854 innermost_deduced_args);
21856 else
21857 deduced_args = innermost_deduced_args;
21859 bool tried_array_deduction = (cxx_dialect < cxx1z);
21860 again:
21861 if (unify (tparms, deduced_args,
21862 INNERMOST_TEMPLATE_ARGS (spec_args),
21863 INNERMOST_TEMPLATE_ARGS (args),
21864 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21865 return NULL_TREE;
21867 for (i = 0; i < ntparms; ++i)
21868 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21870 if (!tried_array_deduction)
21872 try_array_deduction (tparms, innermost_deduced_args,
21873 INNERMOST_TEMPLATE_ARGS (spec_args));
21874 tried_array_deduction = true;
21875 if (TREE_VEC_ELT (innermost_deduced_args, i))
21876 goto again;
21878 return NULL_TREE;
21881 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21882 if (!push_tinst_level (tinst))
21884 excessive_deduction_depth = true;
21885 return NULL_TREE;
21888 /* Verify that nondeduced template arguments agree with the type
21889 obtained from argument deduction.
21891 For example:
21893 struct A { typedef int X; };
21894 template <class T, class U> struct C {};
21895 template <class T> struct C<T, typename T::X> {};
21897 Then with the instantiation `C<A, int>', we can deduce that
21898 `T' is `A' but unify () does not check whether `typename T::X'
21899 is `int'. */
21900 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21902 if (spec_args != error_mark_node)
21903 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21904 INNERMOST_TEMPLATE_ARGS (spec_args),
21905 tmpl, tf_none, false, false);
21907 pop_tinst_level ();
21909 if (spec_args == error_mark_node
21910 /* We only need to check the innermost arguments; the other
21911 arguments will always agree. */
21912 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21913 INNERMOST_TEMPLATE_ARGS (args)))
21914 return NULL_TREE;
21916 /* Now that we have bindings for all of the template arguments,
21917 ensure that the arguments deduced for the template template
21918 parameters have compatible template parameter lists. See the use
21919 of template_template_parm_bindings_ok_p in fn_type_unification
21920 for more information. */
21921 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21922 return NULL_TREE;
21924 return deduced_args;
21927 // Compare two function templates T1 and T2 by deducing bindings
21928 // from one against the other. If both deductions succeed, compare
21929 // constraints to see which is more constrained.
21930 static int
21931 more_specialized_inst (tree t1, tree t2)
21933 int fate = 0;
21934 int count = 0;
21936 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21938 --fate;
21939 ++count;
21942 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21944 ++fate;
21945 ++count;
21948 // If both deductions succeed, then one may be more constrained.
21949 if (count == 2 && fate == 0)
21950 fate = more_constrained (t1, t2);
21952 return fate;
21955 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21956 Return the TREE_LIST node with the most specialized template, if
21957 any. If there is no most specialized template, the error_mark_node
21958 is returned.
21960 Note that this function does not look at, or modify, the
21961 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21962 returned is one of the elements of INSTANTIATIONS, callers may
21963 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21964 and retrieve it from the value returned. */
21966 tree
21967 most_specialized_instantiation (tree templates)
21969 tree fn, champ;
21971 ++processing_template_decl;
21973 champ = templates;
21974 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21976 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
21977 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21978 if (fate == -1)
21979 champ = fn;
21980 else if (!fate)
21982 /* Equally specialized, move to next function. If there
21983 is no next function, nothing's most specialized. */
21984 fn = TREE_CHAIN (fn);
21985 champ = fn;
21986 if (!fn)
21987 break;
21991 if (champ)
21992 /* Now verify that champ is better than everything earlier in the
21993 instantiation list. */
21994 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21995 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21997 champ = NULL_TREE;
21998 break;
22002 processing_template_decl--;
22004 if (!champ)
22005 return error_mark_node;
22007 return champ;
22010 /* If DECL is a specialization of some template, return the most
22011 general such template. Otherwise, returns NULL_TREE.
22013 For example, given:
22015 template <class T> struct S { template <class U> void f(U); };
22017 if TMPL is `template <class U> void S<int>::f(U)' this will return
22018 the full template. This function will not trace past partial
22019 specializations, however. For example, given in addition:
22021 template <class T> struct S<T*> { template <class U> void f(U); };
22023 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22024 `template <class T> template <class U> S<T*>::f(U)'. */
22026 tree
22027 most_general_template (tree decl)
22029 if (TREE_CODE (decl) != TEMPLATE_DECL)
22031 if (tree tinfo = get_template_info (decl))
22032 decl = TI_TEMPLATE (tinfo);
22033 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22034 template friend, or a FIELD_DECL for a capture pack. */
22035 if (TREE_CODE (decl) != TEMPLATE_DECL)
22036 return NULL_TREE;
22039 /* Look for more and more general templates. */
22040 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22042 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22043 (See cp-tree.h for details.) */
22044 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22045 break;
22047 if (CLASS_TYPE_P (TREE_TYPE (decl))
22048 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22049 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22050 break;
22052 /* Stop if we run into an explicitly specialized class template. */
22053 if (!DECL_NAMESPACE_SCOPE_P (decl)
22054 && DECL_CONTEXT (decl)
22055 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22056 break;
22058 decl = DECL_TI_TEMPLATE (decl);
22061 return decl;
22064 /* Return the most specialized of the template partial specializations
22065 which can produce TARGET, a specialization of some class or variable
22066 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22067 a TEMPLATE_DECL node corresponding to the partial specialization, while
22068 the TREE_PURPOSE is the set of template arguments that must be
22069 substituted into the template pattern in order to generate TARGET.
22071 If the choice of partial specialization is ambiguous, a diagnostic
22072 is issued, and the error_mark_node is returned. If there are no
22073 partial specializations matching TARGET, then NULL_TREE is
22074 returned, indicating that the primary template should be used. */
22076 static tree
22077 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22079 tree list = NULL_TREE;
22080 tree t;
22081 tree champ;
22082 int fate;
22083 bool ambiguous_p;
22084 tree outer_args = NULL_TREE;
22085 tree tmpl, args;
22087 if (TYPE_P (target))
22089 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22090 tmpl = TI_TEMPLATE (tinfo);
22091 args = TI_ARGS (tinfo);
22093 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22095 tmpl = TREE_OPERAND (target, 0);
22096 args = TREE_OPERAND (target, 1);
22098 else if (VAR_P (target))
22100 tree tinfo = DECL_TEMPLATE_INFO (target);
22101 tmpl = TI_TEMPLATE (tinfo);
22102 args = TI_ARGS (tinfo);
22104 else
22105 gcc_unreachable ();
22107 tree main_tmpl = most_general_template (tmpl);
22109 /* For determining which partial specialization to use, only the
22110 innermost args are interesting. */
22111 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22113 outer_args = strip_innermost_template_args (args, 1);
22114 args = INNERMOST_TEMPLATE_ARGS (args);
22117 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22119 tree spec_args;
22120 tree spec_tmpl = TREE_VALUE (t);
22122 if (outer_args)
22124 /* Substitute in the template args from the enclosing class. */
22125 ++processing_template_decl;
22126 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22127 --processing_template_decl;
22130 if (spec_tmpl == error_mark_node)
22131 return error_mark_node;
22133 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22134 if (spec_args)
22136 if (outer_args)
22137 spec_args = add_to_template_args (outer_args, spec_args);
22139 /* Keep the candidate only if the constraints are satisfied,
22140 or if we're not compiling with concepts. */
22141 if (!flag_concepts
22142 || constraints_satisfied_p (spec_tmpl, spec_args))
22144 list = tree_cons (spec_args, TREE_VALUE (t), list);
22145 TREE_TYPE (list) = TREE_TYPE (t);
22150 if (! list)
22151 return NULL_TREE;
22153 ambiguous_p = false;
22154 t = list;
22155 champ = t;
22156 t = TREE_CHAIN (t);
22157 for (; t; t = TREE_CHAIN (t))
22159 fate = more_specialized_partial_spec (tmpl, champ, t);
22160 if (fate == 1)
22162 else
22164 if (fate == 0)
22166 t = TREE_CHAIN (t);
22167 if (! t)
22169 ambiguous_p = true;
22170 break;
22173 champ = t;
22177 if (!ambiguous_p)
22178 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22180 fate = more_specialized_partial_spec (tmpl, champ, t);
22181 if (fate != 1)
22183 ambiguous_p = true;
22184 break;
22188 if (ambiguous_p)
22190 const char *str;
22191 char *spaces = NULL;
22192 if (!(complain & tf_error))
22193 return error_mark_node;
22194 if (TYPE_P (target))
22195 error ("ambiguous template instantiation for %q#T", target);
22196 else
22197 error ("ambiguous template instantiation for %q#D", target);
22198 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22199 for (t = list; t; t = TREE_CHAIN (t))
22201 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22202 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22203 "%s %#qS", spaces ? spaces : str, subst);
22204 spaces = spaces ? spaces : get_spaces (str);
22206 free (spaces);
22207 return error_mark_node;
22210 return champ;
22213 /* Explicitly instantiate DECL. */
22215 void
22216 do_decl_instantiation (tree decl, tree storage)
22218 tree result = NULL_TREE;
22219 int extern_p = 0;
22221 if (!decl || decl == error_mark_node)
22222 /* An error occurred, for which grokdeclarator has already issued
22223 an appropriate message. */
22224 return;
22225 else if (! DECL_LANG_SPECIFIC (decl))
22227 error ("explicit instantiation of non-template %q#D", decl);
22228 return;
22231 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22232 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22234 if (VAR_P (decl) && !var_templ)
22236 /* There is an asymmetry here in the way VAR_DECLs and
22237 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22238 the latter, the DECL we get back will be marked as a
22239 template instantiation, and the appropriate
22240 DECL_TEMPLATE_INFO will be set up. This does not happen for
22241 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22242 should handle VAR_DECLs as it currently handles
22243 FUNCTION_DECLs. */
22244 if (!DECL_CLASS_SCOPE_P (decl))
22246 error ("%qD is not a static data member of a class template", decl);
22247 return;
22249 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22250 if (!result || !VAR_P (result))
22252 error ("no matching template for %qD found", decl);
22253 return;
22255 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22257 error ("type %qT for explicit instantiation %qD does not match "
22258 "declared type %qT", TREE_TYPE (result), decl,
22259 TREE_TYPE (decl));
22260 return;
22263 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22265 error ("explicit instantiation of %q#D", decl);
22266 return;
22268 else
22269 result = decl;
22271 /* Check for various error cases. Note that if the explicit
22272 instantiation is valid the RESULT will currently be marked as an
22273 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22274 until we get here. */
22276 if (DECL_TEMPLATE_SPECIALIZATION (result))
22278 /* DR 259 [temp.spec].
22280 Both an explicit instantiation and a declaration of an explicit
22281 specialization shall not appear in a program unless the explicit
22282 instantiation follows a declaration of the explicit specialization.
22284 For a given set of template parameters, if an explicit
22285 instantiation of a template appears after a declaration of an
22286 explicit specialization for that template, the explicit
22287 instantiation has no effect. */
22288 return;
22290 else if (DECL_EXPLICIT_INSTANTIATION (result))
22292 /* [temp.spec]
22294 No program shall explicitly instantiate any template more
22295 than once.
22297 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22298 the first instantiation was `extern' and the second is not,
22299 and EXTERN_P for the opposite case. */
22300 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22301 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22302 /* If an "extern" explicit instantiation follows an ordinary
22303 explicit instantiation, the template is instantiated. */
22304 if (extern_p)
22305 return;
22307 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22309 error ("no matching template for %qD found", result);
22310 return;
22312 else if (!DECL_TEMPLATE_INFO (result))
22314 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22315 return;
22318 if (storage == NULL_TREE)
22320 else if (storage == ridpointers[(int) RID_EXTERN])
22322 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22323 pedwarn (input_location, OPT_Wpedantic,
22324 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22325 "instantiations");
22326 extern_p = 1;
22328 else
22329 error ("storage class %qD applied to template instantiation", storage);
22331 check_explicit_instantiation_namespace (result);
22332 mark_decl_instantiated (result, extern_p);
22333 if (! extern_p)
22334 instantiate_decl (result, /*defer_ok=*/true,
22335 /*expl_inst_class_mem_p=*/false);
22338 static void
22339 mark_class_instantiated (tree t, int extern_p)
22341 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22342 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22343 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22344 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22345 if (! extern_p)
22347 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22348 rest_of_type_compilation (t, 1);
22352 /* Called from do_type_instantiation through binding_table_foreach to
22353 do recursive instantiation for the type bound in ENTRY. */
22354 static void
22355 bt_instantiate_type_proc (binding_entry entry, void *data)
22357 tree storage = *(tree *) data;
22359 if (MAYBE_CLASS_TYPE_P (entry->type)
22360 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22361 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22364 /* Perform an explicit instantiation of template class T. STORAGE, if
22365 non-null, is the RID for extern, inline or static. COMPLAIN is
22366 nonzero if this is called from the parser, zero if called recursively,
22367 since the standard is unclear (as detailed below). */
22369 void
22370 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22372 int extern_p = 0;
22373 int nomem_p = 0;
22374 int static_p = 0;
22375 int previous_instantiation_extern_p = 0;
22377 if (TREE_CODE (t) == TYPE_DECL)
22378 t = TREE_TYPE (t);
22380 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22382 tree tmpl =
22383 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22384 if (tmpl)
22385 error ("explicit instantiation of non-class template %qD", tmpl);
22386 else
22387 error ("explicit instantiation of non-template type %qT", t);
22388 return;
22391 complete_type (t);
22393 if (!COMPLETE_TYPE_P (t))
22395 if (complain & tf_error)
22396 error ("explicit instantiation of %q#T before definition of template",
22398 return;
22401 if (storage != NULL_TREE)
22403 if (!in_system_header_at (input_location))
22405 if (storage == ridpointers[(int) RID_EXTERN])
22407 if (cxx_dialect == cxx98)
22408 pedwarn (input_location, OPT_Wpedantic,
22409 "ISO C++ 1998 forbids the use of %<extern%> on "
22410 "explicit instantiations");
22412 else
22413 pedwarn (input_location, OPT_Wpedantic,
22414 "ISO C++ forbids the use of %qE"
22415 " on explicit instantiations", storage);
22418 if (storage == ridpointers[(int) RID_INLINE])
22419 nomem_p = 1;
22420 else if (storage == ridpointers[(int) RID_EXTERN])
22421 extern_p = 1;
22422 else if (storage == ridpointers[(int) RID_STATIC])
22423 static_p = 1;
22424 else
22426 error ("storage class %qD applied to template instantiation",
22427 storage);
22428 extern_p = 0;
22432 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22434 /* DR 259 [temp.spec].
22436 Both an explicit instantiation and a declaration of an explicit
22437 specialization shall not appear in a program unless the explicit
22438 instantiation follows a declaration of the explicit specialization.
22440 For a given set of template parameters, if an explicit
22441 instantiation of a template appears after a declaration of an
22442 explicit specialization for that template, the explicit
22443 instantiation has no effect. */
22444 return;
22446 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22448 /* [temp.spec]
22450 No program shall explicitly instantiate any template more
22451 than once.
22453 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22454 instantiation was `extern'. If EXTERN_P then the second is.
22455 These cases are OK. */
22456 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22458 if (!previous_instantiation_extern_p && !extern_p
22459 && (complain & tf_error))
22460 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22462 /* If we've already instantiated the template, just return now. */
22463 if (!CLASSTYPE_INTERFACE_ONLY (t))
22464 return;
22467 check_explicit_instantiation_namespace (TYPE_NAME (t));
22468 mark_class_instantiated (t, extern_p);
22470 if (nomem_p)
22471 return;
22473 /* In contrast to implicit instantiation, where only the
22474 declarations, and not the definitions, of members are
22475 instantiated, we have here:
22477 [temp.explicit]
22479 The explicit instantiation of a class template specialization
22480 implies the instantiation of all of its members not
22481 previously explicitly specialized in the translation unit
22482 containing the explicit instantiation.
22484 Of course, we can't instantiate member template classes, since we
22485 don't have any arguments for them. Note that the standard is
22486 unclear on whether the instantiation of the members are
22487 *explicit* instantiations or not. However, the most natural
22488 interpretation is that it should be an explicit
22489 instantiation. */
22490 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22491 if ((VAR_P (fld)
22492 || (TREE_CODE (fld) == FUNCTION_DECL
22493 && !static_p
22494 && user_provided_p (fld)))
22495 && DECL_TEMPLATE_INSTANTIATION (fld))
22497 mark_decl_instantiated (fld, extern_p);
22498 if (! extern_p)
22499 instantiate_decl (fld, /*defer_ok=*/true,
22500 /*expl_inst_class_mem_p=*/true);
22503 if (CLASSTYPE_NESTED_UTDS (t))
22504 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22505 bt_instantiate_type_proc, &storage);
22508 /* Given a function DECL, which is a specialization of TMPL, modify
22509 DECL to be a re-instantiation of TMPL with the same template
22510 arguments. TMPL should be the template into which tsubst'ing
22511 should occur for DECL, not the most general template.
22513 One reason for doing this is a scenario like this:
22515 template <class T>
22516 void f(const T&, int i);
22518 void g() { f(3, 7); }
22520 template <class T>
22521 void f(const T& t, const int i) { }
22523 Note that when the template is first instantiated, with
22524 instantiate_template, the resulting DECL will have no name for the
22525 first parameter, and the wrong type for the second. So, when we go
22526 to instantiate the DECL, we regenerate it. */
22528 static void
22529 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22531 /* The arguments used to instantiate DECL, from the most general
22532 template. */
22533 tree code_pattern;
22535 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22537 /* Make sure that we can see identifiers, and compute access
22538 correctly. */
22539 push_access_scope (decl);
22541 if (TREE_CODE (decl) == FUNCTION_DECL)
22543 tree decl_parm;
22544 tree pattern_parm;
22545 tree specs;
22546 int args_depth;
22547 int parms_depth;
22549 args_depth = TMPL_ARGS_DEPTH (args);
22550 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22551 if (args_depth > parms_depth)
22552 args = get_innermost_template_args (args, parms_depth);
22554 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22555 args, tf_error, NULL_TREE,
22556 /*defer_ok*/false);
22557 if (specs && specs != error_mark_node)
22558 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22559 specs);
22561 /* Merge parameter declarations. */
22562 decl_parm = skip_artificial_parms_for (decl,
22563 DECL_ARGUMENTS (decl));
22564 pattern_parm
22565 = skip_artificial_parms_for (code_pattern,
22566 DECL_ARGUMENTS (code_pattern));
22567 while (decl_parm && !DECL_PACK_P (pattern_parm))
22569 tree parm_type;
22570 tree attributes;
22572 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22573 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22574 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22575 NULL_TREE);
22576 parm_type = type_decays_to (parm_type);
22577 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22578 TREE_TYPE (decl_parm) = parm_type;
22579 attributes = DECL_ATTRIBUTES (pattern_parm);
22580 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22582 DECL_ATTRIBUTES (decl_parm) = attributes;
22583 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22585 decl_parm = DECL_CHAIN (decl_parm);
22586 pattern_parm = DECL_CHAIN (pattern_parm);
22588 /* Merge any parameters that match with the function parameter
22589 pack. */
22590 if (pattern_parm && DECL_PACK_P (pattern_parm))
22592 int i, len;
22593 tree expanded_types;
22594 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22595 the parameters in this function parameter pack. */
22596 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22597 args, tf_error, NULL_TREE);
22598 len = TREE_VEC_LENGTH (expanded_types);
22599 for (i = 0; i < len; i++)
22601 tree parm_type;
22602 tree attributes;
22604 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22605 /* Rename the parameter to include the index. */
22606 DECL_NAME (decl_parm) =
22607 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22608 parm_type = TREE_VEC_ELT (expanded_types, i);
22609 parm_type = type_decays_to (parm_type);
22610 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22611 TREE_TYPE (decl_parm) = parm_type;
22612 attributes = DECL_ATTRIBUTES (pattern_parm);
22613 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22615 DECL_ATTRIBUTES (decl_parm) = attributes;
22616 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22618 decl_parm = DECL_CHAIN (decl_parm);
22621 /* Merge additional specifiers from the CODE_PATTERN. */
22622 if (DECL_DECLARED_INLINE_P (code_pattern)
22623 && !DECL_DECLARED_INLINE_P (decl))
22624 DECL_DECLARED_INLINE_P (decl) = 1;
22626 else if (VAR_P (decl))
22628 start_lambda_scope (decl);
22629 DECL_INITIAL (decl) =
22630 tsubst_expr (DECL_INITIAL (code_pattern), args,
22631 tf_error, DECL_TI_TEMPLATE (decl),
22632 /*integral_constant_expression_p=*/false);
22633 finish_lambda_scope ();
22634 if (VAR_HAD_UNKNOWN_BOUND (decl))
22635 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22636 tf_error, DECL_TI_TEMPLATE (decl));
22638 else
22639 gcc_unreachable ();
22641 pop_access_scope (decl);
22644 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22645 substituted to get DECL. */
22647 tree
22648 template_for_substitution (tree decl)
22650 tree tmpl = DECL_TI_TEMPLATE (decl);
22652 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22653 for the instantiation. This is not always the most general
22654 template. Consider, for example:
22656 template <class T>
22657 struct S { template <class U> void f();
22658 template <> void f<int>(); };
22660 and an instantiation of S<double>::f<int>. We want TD to be the
22661 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22662 while (/* An instantiation cannot have a definition, so we need a
22663 more general template. */
22664 DECL_TEMPLATE_INSTANTIATION (tmpl)
22665 /* We must also deal with friend templates. Given:
22667 template <class T> struct S {
22668 template <class U> friend void f() {};
22671 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22672 so far as the language is concerned, but that's still
22673 where we get the pattern for the instantiation from. On
22674 other hand, if the definition comes outside the class, say:
22676 template <class T> struct S {
22677 template <class U> friend void f();
22679 template <class U> friend void f() {}
22681 we don't need to look any further. That's what the check for
22682 DECL_INITIAL is for. */
22683 || (TREE_CODE (decl) == FUNCTION_DECL
22684 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22685 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22687 /* The present template, TD, should not be a definition. If it
22688 were a definition, we should be using it! Note that we
22689 cannot restructure the loop to just keep going until we find
22690 a template with a definition, since that might go too far if
22691 a specialization was declared, but not defined. */
22693 /* Fetch the more general template. */
22694 tmpl = DECL_TI_TEMPLATE (tmpl);
22697 return tmpl;
22700 /* Returns true if we need to instantiate this template instance even if we
22701 know we aren't going to emit it. */
22703 bool
22704 always_instantiate_p (tree decl)
22706 /* We always instantiate inline functions so that we can inline them. An
22707 explicit instantiation declaration prohibits implicit instantiation of
22708 non-inline functions. With high levels of optimization, we would
22709 normally inline non-inline functions -- but we're not allowed to do
22710 that for "extern template" functions. Therefore, we check
22711 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22712 return ((TREE_CODE (decl) == FUNCTION_DECL
22713 && (DECL_DECLARED_INLINE_P (decl)
22714 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22715 /* And we need to instantiate static data members so that
22716 their initializers are available in integral constant
22717 expressions. */
22718 || (VAR_P (decl)
22719 && decl_maybe_constant_var_p (decl)));
22722 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22723 instantiate it now, modifying TREE_TYPE (fn). Returns false on
22724 error, true otherwise. */
22726 bool
22727 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
22729 tree fntype, spec, noex, clone;
22731 /* Don't instantiate a noexcept-specification from template context. */
22732 if (processing_template_decl)
22733 return true;
22735 if (DECL_CLONED_FUNCTION_P (fn))
22736 fn = DECL_CLONED_FUNCTION (fn);
22737 fntype = TREE_TYPE (fn);
22738 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22740 if (!spec || !TREE_PURPOSE (spec))
22741 return true;
22743 noex = TREE_PURPOSE (spec);
22745 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22747 static hash_set<tree>* fns = new hash_set<tree>;
22748 bool added = false;
22749 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22750 spec = get_defaulted_eh_spec (fn, complain);
22751 else if (!(added = !fns->add (fn)))
22753 /* If hash_set::add returns true, the element was already there. */
22754 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
22755 DECL_SOURCE_LOCATION (fn));
22756 error_at (loc,
22757 "exception specification of %qD depends on itself",
22758 fn);
22759 spec = noexcept_false_spec;
22761 else if (push_tinst_level (fn))
22763 push_access_scope (fn);
22764 push_deferring_access_checks (dk_no_deferred);
22765 input_location = DECL_SOURCE_LOCATION (fn);
22766 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22767 DEFERRED_NOEXCEPT_ARGS (noex),
22768 tf_warning_or_error, fn,
22769 /*function_p=*/false,
22770 /*integral_constant_expression_p=*/true);
22771 pop_deferring_access_checks ();
22772 pop_access_scope (fn);
22773 pop_tinst_level ();
22774 spec = build_noexcept_spec (noex, tf_warning_or_error);
22775 if (spec == error_mark_node)
22776 spec = noexcept_false_spec;
22778 else
22779 spec = noexcept_false_spec;
22781 if (added)
22782 fns->remove (fn);
22784 if (spec == error_mark_node)
22785 return false;
22787 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22790 FOR_EACH_CLONE (clone, fn)
22792 if (TREE_TYPE (clone) == fntype)
22793 TREE_TYPE (clone) = TREE_TYPE (fn);
22794 else
22795 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22798 return true;
22801 /* We're starting to process the function INST, an instantiation of PATTERN;
22802 add their parameters to local_specializations. */
22804 static void
22805 register_parameter_specializations (tree pattern, tree inst)
22807 tree tmpl_parm = DECL_ARGUMENTS (pattern);
22808 tree spec_parm = DECL_ARGUMENTS (inst);
22809 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
22811 register_local_specialization (spec_parm, tmpl_parm);
22812 spec_parm = skip_artificial_parms_for (inst, spec_parm);
22813 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
22815 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22817 if (!DECL_PACK_P (tmpl_parm))
22819 register_local_specialization (spec_parm, tmpl_parm);
22820 spec_parm = DECL_CHAIN (spec_parm);
22822 else
22824 /* Register the (value) argument pack as a specialization of
22825 TMPL_PARM, then move on. */
22826 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22827 register_local_specialization (argpack, tmpl_parm);
22830 gcc_assert (!spec_parm);
22833 /* Produce the definition of D, a _DECL generated from a template. If
22834 DEFER_OK is true, then we don't have to actually do the
22835 instantiation now; we just have to do it sometime. Normally it is
22836 an error if this is an explicit instantiation but D is undefined.
22837 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22838 instantiated class template. */
22840 tree
22841 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22843 tree tmpl = DECL_TI_TEMPLATE (d);
22844 tree gen_args;
22845 tree args;
22846 tree td;
22847 tree code_pattern;
22848 tree spec;
22849 tree gen_tmpl;
22850 bool pattern_defined;
22851 location_t saved_loc = input_location;
22852 int saved_unevaluated_operand = cp_unevaluated_operand;
22853 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22854 bool external_p;
22855 bool deleted_p;
22857 /* This function should only be used to instantiate templates for
22858 functions and static member variables. */
22859 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22861 /* A concept is never instantiated. */
22862 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22864 /* Variables are never deferred; if instantiation is required, they
22865 are instantiated right away. That allows for better code in the
22866 case that an expression refers to the value of the variable --
22867 if the variable has a constant value the referring expression can
22868 take advantage of that fact. */
22869 if (VAR_P (d))
22870 defer_ok = false;
22872 /* Don't instantiate cloned functions. Instead, instantiate the
22873 functions they cloned. */
22874 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22875 d = DECL_CLONED_FUNCTION (d);
22877 if (DECL_TEMPLATE_INSTANTIATED (d)
22878 || (TREE_CODE (d) == FUNCTION_DECL
22879 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22880 || DECL_TEMPLATE_SPECIALIZATION (d))
22881 /* D has already been instantiated or explicitly specialized, so
22882 there's nothing for us to do here.
22884 It might seem reasonable to check whether or not D is an explicit
22885 instantiation, and, if so, stop here. But when an explicit
22886 instantiation is deferred until the end of the compilation,
22887 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22888 the instantiation. */
22889 return d;
22891 /* Check to see whether we know that this template will be
22892 instantiated in some other file, as with "extern template"
22893 extension. */
22894 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22896 /* In general, we do not instantiate such templates. */
22897 if (external_p && !always_instantiate_p (d))
22898 return d;
22900 gen_tmpl = most_general_template (tmpl);
22901 gen_args = DECL_TI_ARGS (d);
22903 if (tmpl != gen_tmpl)
22904 /* We should already have the extra args. */
22905 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22906 == TMPL_ARGS_DEPTH (gen_args));
22907 /* And what's in the hash table should match D. */
22908 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22909 || spec == NULL_TREE);
22911 /* This needs to happen before any tsubsting. */
22912 if (! push_tinst_level (d))
22913 return d;
22915 timevar_push (TV_TEMPLATE_INST);
22917 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22918 for the instantiation. */
22919 td = template_for_substitution (d);
22920 args = gen_args;
22922 if (VAR_P (d))
22924 /* Look up an explicit specialization, if any. */
22925 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22926 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22927 if (elt && elt != error_mark_node)
22929 td = TREE_VALUE (elt);
22930 args = TREE_PURPOSE (elt);
22934 code_pattern = DECL_TEMPLATE_RESULT (td);
22936 /* We should never be trying to instantiate a member of a class
22937 template or partial specialization. */
22938 gcc_assert (d != code_pattern);
22940 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22941 || DECL_TEMPLATE_SPECIALIZATION (td))
22942 /* In the case of a friend template whose definition is provided
22943 outside the class, we may have too many arguments. Drop the
22944 ones we don't need. The same is true for specializations. */
22945 args = get_innermost_template_args
22946 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22948 if (TREE_CODE (d) == FUNCTION_DECL)
22950 deleted_p = DECL_DELETED_FN (code_pattern);
22951 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22952 && DECL_INITIAL (code_pattern) != error_mark_node)
22953 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22954 || deleted_p);
22956 else
22958 deleted_p = false;
22959 if (DECL_CLASS_SCOPE_P (code_pattern))
22960 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22961 || DECL_INLINE_VAR_P (code_pattern));
22962 else
22963 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22966 /* We may be in the middle of deferred access check. Disable it now. */
22967 push_deferring_access_checks (dk_no_deferred);
22969 /* Unless an explicit instantiation directive has already determined
22970 the linkage of D, remember that a definition is available for
22971 this entity. */
22972 if (pattern_defined
22973 && !DECL_INTERFACE_KNOWN (d)
22974 && !DECL_NOT_REALLY_EXTERN (d))
22975 mark_definable (d);
22977 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22978 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22979 input_location = DECL_SOURCE_LOCATION (d);
22981 /* If D is a member of an explicitly instantiated class template,
22982 and no definition is available, treat it like an implicit
22983 instantiation. */
22984 if (!pattern_defined && expl_inst_class_mem_p
22985 && DECL_EXPLICIT_INSTANTIATION (d))
22987 /* Leave linkage flags alone on instantiations with anonymous
22988 visibility. */
22989 if (TREE_PUBLIC (d))
22991 DECL_NOT_REALLY_EXTERN (d) = 0;
22992 DECL_INTERFACE_KNOWN (d) = 0;
22994 SET_DECL_IMPLICIT_INSTANTIATION (d);
22997 /* Defer all other templates, unless we have been explicitly
22998 forbidden from doing so. */
22999 if (/* If there is no definition, we cannot instantiate the
23000 template. */
23001 ! pattern_defined
23002 /* If it's OK to postpone instantiation, do so. */
23003 || defer_ok
23004 /* If this is a static data member that will be defined
23005 elsewhere, we don't want to instantiate the entire data
23006 member, but we do want to instantiate the initializer so that
23007 we can substitute that elsewhere. */
23008 || (external_p && VAR_P (d))
23009 /* Handle here a deleted function too, avoid generating
23010 its body (c++/61080). */
23011 || deleted_p)
23013 /* The definition of the static data member is now required so
23014 we must substitute the initializer. */
23015 if (VAR_P (d)
23016 && !DECL_INITIAL (d)
23017 && DECL_INITIAL (code_pattern))
23019 tree ns;
23020 tree init;
23021 bool const_init = false;
23022 bool enter_context = DECL_CLASS_SCOPE_P (d);
23024 ns = decl_namespace_context (d);
23025 push_nested_namespace (ns);
23026 if (enter_context)
23027 push_nested_class (DECL_CONTEXT (d));
23028 init = tsubst_expr (DECL_INITIAL (code_pattern),
23029 args,
23030 tf_warning_or_error, NULL_TREE,
23031 /*integral_constant_expression_p=*/false);
23032 /* If instantiating the initializer involved instantiating this
23033 again, don't call cp_finish_decl twice. */
23034 if (!DECL_INITIAL (d))
23036 /* Make sure the initializer is still constant, in case of
23037 circular dependency (template/instantiate6.C). */
23038 const_init
23039 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23040 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23041 /*asmspec_tree=*/NULL_TREE,
23042 LOOKUP_ONLYCONVERTING);
23044 if (enter_context)
23045 pop_nested_class ();
23046 pop_nested_namespace (ns);
23049 /* We restore the source position here because it's used by
23050 add_pending_template. */
23051 input_location = saved_loc;
23053 if (at_eof && !pattern_defined
23054 && DECL_EXPLICIT_INSTANTIATION (d)
23055 && DECL_NOT_REALLY_EXTERN (d))
23056 /* [temp.explicit]
23058 The definition of a non-exported function template, a
23059 non-exported member function template, or a non-exported
23060 member function or static data member of a class template
23061 shall be present in every translation unit in which it is
23062 explicitly instantiated. */
23063 permerror (input_location, "explicit instantiation of %qD "
23064 "but no definition available", d);
23066 /* If we're in unevaluated context, we just wanted to get the
23067 constant value; this isn't an odr use, so don't queue
23068 a full instantiation. */
23069 if (cp_unevaluated_operand != 0)
23070 goto out;
23071 /* ??? Historically, we have instantiated inline functions, even
23072 when marked as "extern template". */
23073 if (!(external_p && VAR_P (d)))
23074 add_pending_template (d);
23075 goto out;
23077 /* Tell the repository that D is available in this translation unit
23078 -- and see if it is supposed to be instantiated here. */
23079 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23081 /* In a PCH file, despite the fact that the repository hasn't
23082 requested instantiation in the PCH it is still possible that
23083 an instantiation will be required in a file that includes the
23084 PCH. */
23085 if (pch_file)
23086 add_pending_template (d);
23087 /* Instantiate inline functions so that the inliner can do its
23088 job, even though we'll not be emitting a copy of this
23089 function. */
23090 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23091 goto out;
23094 bool push_to_top, nested;
23095 tree fn_context;
23096 fn_context = decl_function_context (d);
23097 nested = current_function_decl != NULL_TREE;
23098 push_to_top = !(nested && fn_context == current_function_decl);
23100 vec<tree> omp_privatization_save;
23101 if (nested)
23102 save_omp_privatization_clauses (omp_privatization_save);
23104 if (push_to_top)
23105 push_to_top_level ();
23106 else
23108 push_function_context ();
23109 cp_unevaluated_operand = 0;
23110 c_inhibit_evaluation_warnings = 0;
23113 /* Mark D as instantiated so that recursive calls to
23114 instantiate_decl do not try to instantiate it again. */
23115 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23117 /* Regenerate the declaration in case the template has been modified
23118 by a subsequent redeclaration. */
23119 regenerate_decl_from_template (d, td, args);
23121 /* We already set the file and line above. Reset them now in case
23122 they changed as a result of calling regenerate_decl_from_template. */
23123 input_location = DECL_SOURCE_LOCATION (d);
23125 if (VAR_P (d))
23127 tree init;
23128 bool const_init = false;
23130 /* Clear out DECL_RTL; whatever was there before may not be right
23131 since we've reset the type of the declaration. */
23132 SET_DECL_RTL (d, NULL);
23133 DECL_IN_AGGR_P (d) = 0;
23135 /* The initializer is placed in DECL_INITIAL by
23136 regenerate_decl_from_template so we don't need to
23137 push/pop_access_scope again here. Pull it out so that
23138 cp_finish_decl can process it. */
23139 init = DECL_INITIAL (d);
23140 DECL_INITIAL (d) = NULL_TREE;
23141 DECL_INITIALIZED_P (d) = 0;
23143 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23144 initializer. That function will defer actual emission until
23145 we have a chance to determine linkage. */
23146 DECL_EXTERNAL (d) = 0;
23148 /* Enter the scope of D so that access-checking works correctly. */
23149 bool enter_context = DECL_CLASS_SCOPE_P (d);
23150 if (enter_context)
23151 push_nested_class (DECL_CONTEXT (d));
23153 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23154 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23156 if (enter_context)
23157 pop_nested_class ();
23159 if (variable_template_p (gen_tmpl))
23160 note_variable_template_instantiation (d);
23162 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23163 synthesize_method (d);
23164 else if (TREE_CODE (d) == FUNCTION_DECL)
23166 hash_map<tree, tree> *saved_local_specializations;
23167 tree block = NULL_TREE;
23169 /* Save away the current list, in case we are instantiating one
23170 template from within the body of another. */
23171 saved_local_specializations = local_specializations;
23173 /* Set up the list of local specializations. */
23174 local_specializations = new hash_map<tree, tree>;
23176 /* Set up context. */
23177 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23178 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23179 block = push_stmt_list ();
23180 else
23181 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23183 /* Some typedefs referenced from within the template code need to be
23184 access checked at template instantiation time, i.e now. These
23185 types were added to the template at parsing time. Let's get those
23186 and perform the access checks then. */
23187 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23188 args);
23190 /* Create substitution entries for the parameters. */
23191 register_parameter_specializations (code_pattern, d);
23193 /* Substitute into the body of the function. */
23194 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23195 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23196 tf_warning_or_error, tmpl);
23197 else
23199 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23200 tf_warning_or_error, tmpl,
23201 /*integral_constant_expression_p=*/false);
23203 /* Set the current input_location to the end of the function
23204 so that finish_function knows where we are. */
23205 input_location
23206 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23208 /* Remember if we saw an infinite loop in the template. */
23209 current_function_infinite_loop
23210 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23213 /* We don't need the local specializations any more. */
23214 delete local_specializations;
23215 local_specializations = saved_local_specializations;
23217 /* Finish the function. */
23218 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23219 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23220 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23221 else
23223 d = finish_function (0);
23224 expand_or_defer_fn (d);
23227 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23228 cp_check_omp_declare_reduction (d);
23231 /* We're not deferring instantiation any more. */
23232 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23234 if (push_to_top)
23235 pop_from_top_level ();
23236 else
23237 pop_function_context ();
23239 if (nested)
23240 restore_omp_privatization_clauses (omp_privatization_save);
23242 out:
23243 pop_deferring_access_checks ();
23244 timevar_pop (TV_TEMPLATE_INST);
23245 pop_tinst_level ();
23246 input_location = saved_loc;
23247 cp_unevaluated_operand = saved_unevaluated_operand;
23248 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23250 return d;
23253 /* Run through the list of templates that we wish we could
23254 instantiate, and instantiate any we can. RETRIES is the
23255 number of times we retry pending template instantiation. */
23257 void
23258 instantiate_pending_templates (int retries)
23260 int reconsider;
23261 location_t saved_loc = input_location;
23263 /* Instantiating templates may trigger vtable generation. This in turn
23264 may require further template instantiations. We place a limit here
23265 to avoid infinite loop. */
23266 if (pending_templates && retries >= max_tinst_depth)
23268 tree decl = pending_templates->tinst->decl;
23270 fatal_error (input_location,
23271 "template instantiation depth exceeds maximum of %d"
23272 " instantiating %q+D, possibly from virtual table generation"
23273 " (use -ftemplate-depth= to increase the maximum)",
23274 max_tinst_depth, decl);
23275 if (TREE_CODE (decl) == FUNCTION_DECL)
23276 /* Pretend that we defined it. */
23277 DECL_INITIAL (decl) = error_mark_node;
23278 return;
23283 struct pending_template **t = &pending_templates;
23284 struct pending_template *last = NULL;
23285 reconsider = 0;
23286 while (*t)
23288 tree instantiation = reopen_tinst_level ((*t)->tinst);
23289 bool complete = false;
23291 if (TYPE_P (instantiation))
23293 if (!COMPLETE_TYPE_P (instantiation))
23295 instantiate_class_template (instantiation);
23296 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23297 for (tree fld = TYPE_FIELDS (instantiation);
23298 fld; fld = TREE_CHAIN (fld))
23299 if ((VAR_P (fld)
23300 || (TREE_CODE (fld) == FUNCTION_DECL
23301 && !DECL_ARTIFICIAL (fld)))
23302 && DECL_TEMPLATE_INSTANTIATION (fld))
23303 instantiate_decl (fld,
23304 /*defer_ok=*/false,
23305 /*expl_inst_class_mem_p=*/false);
23307 if (COMPLETE_TYPE_P (instantiation))
23308 reconsider = 1;
23311 complete = COMPLETE_TYPE_P (instantiation);
23313 else
23315 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23316 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23318 instantiation
23319 = instantiate_decl (instantiation,
23320 /*defer_ok=*/false,
23321 /*expl_inst_class_mem_p=*/false);
23322 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23323 reconsider = 1;
23326 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23327 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23330 if (complete)
23331 /* If INSTANTIATION has been instantiated, then we don't
23332 need to consider it again in the future. */
23333 *t = (*t)->next;
23334 else
23336 last = *t;
23337 t = &(*t)->next;
23339 tinst_depth = 0;
23340 current_tinst_level = NULL;
23342 last_pending_template = last;
23344 while (reconsider);
23346 input_location = saved_loc;
23349 /* Substitute ARGVEC into T, which is a list of initializers for
23350 either base class or a non-static data member. The TREE_PURPOSEs
23351 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23352 instantiate_decl. */
23354 static tree
23355 tsubst_initializer_list (tree t, tree argvec)
23357 tree inits = NULL_TREE;
23359 for (; t; t = TREE_CHAIN (t))
23361 tree decl;
23362 tree init;
23363 tree expanded_bases = NULL_TREE;
23364 tree expanded_arguments = NULL_TREE;
23365 int i, len = 1;
23367 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23369 tree expr;
23370 tree arg;
23372 /* Expand the base class expansion type into separate base
23373 classes. */
23374 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23375 tf_warning_or_error,
23376 NULL_TREE);
23377 if (expanded_bases == error_mark_node)
23378 continue;
23380 /* We'll be building separate TREE_LISTs of arguments for
23381 each base. */
23382 len = TREE_VEC_LENGTH (expanded_bases);
23383 expanded_arguments = make_tree_vec (len);
23384 for (i = 0; i < len; i++)
23385 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23387 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23388 expand each argument in the TREE_VALUE of t. */
23389 expr = make_node (EXPR_PACK_EXPANSION);
23390 PACK_EXPANSION_LOCAL_P (expr) = true;
23391 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23392 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23394 if (TREE_VALUE (t) == void_type_node)
23395 /* VOID_TYPE_NODE is used to indicate
23396 value-initialization. */
23398 for (i = 0; i < len; i++)
23399 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23401 else
23403 /* Substitute parameter packs into each argument in the
23404 TREE_LIST. */
23405 in_base_initializer = 1;
23406 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23408 tree expanded_exprs;
23410 /* Expand the argument. */
23411 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23412 expanded_exprs
23413 = tsubst_pack_expansion (expr, argvec,
23414 tf_warning_or_error,
23415 NULL_TREE);
23416 if (expanded_exprs == error_mark_node)
23417 continue;
23419 /* Prepend each of the expanded expressions to the
23420 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23421 for (i = 0; i < len; i++)
23423 TREE_VEC_ELT (expanded_arguments, i) =
23424 tree_cons (NULL_TREE,
23425 TREE_VEC_ELT (expanded_exprs, i),
23426 TREE_VEC_ELT (expanded_arguments, i));
23429 in_base_initializer = 0;
23431 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23432 since we built them backwards. */
23433 for (i = 0; i < len; i++)
23435 TREE_VEC_ELT (expanded_arguments, i) =
23436 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23441 for (i = 0; i < len; ++i)
23443 if (expanded_bases)
23445 decl = TREE_VEC_ELT (expanded_bases, i);
23446 decl = expand_member_init (decl);
23447 init = TREE_VEC_ELT (expanded_arguments, i);
23449 else
23451 tree tmp;
23452 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23453 tf_warning_or_error, NULL_TREE);
23455 decl = expand_member_init (decl);
23456 if (decl && !DECL_P (decl))
23457 in_base_initializer = 1;
23459 init = TREE_VALUE (t);
23460 tmp = init;
23461 if (init != void_type_node)
23462 init = tsubst_expr (init, argvec,
23463 tf_warning_or_error, NULL_TREE,
23464 /*integral_constant_expression_p=*/false);
23465 if (init == NULL_TREE && tmp != NULL_TREE)
23466 /* If we had an initializer but it instantiated to nothing,
23467 value-initialize the object. This will only occur when
23468 the initializer was a pack expansion where the parameter
23469 packs used in that expansion were of length zero. */
23470 init = void_type_node;
23471 in_base_initializer = 0;
23474 if (decl)
23476 init = build_tree_list (decl, init);
23477 TREE_CHAIN (init) = inits;
23478 inits = init;
23482 return inits;
23485 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23487 static void
23488 set_current_access_from_decl (tree decl)
23490 if (TREE_PRIVATE (decl))
23491 current_access_specifier = access_private_node;
23492 else if (TREE_PROTECTED (decl))
23493 current_access_specifier = access_protected_node;
23494 else
23495 current_access_specifier = access_public_node;
23498 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23499 is the instantiation (which should have been created with
23500 start_enum) and ARGS are the template arguments to use. */
23502 static void
23503 tsubst_enum (tree tag, tree newtag, tree args)
23505 tree e;
23507 if (SCOPED_ENUM_P (newtag))
23508 begin_scope (sk_scoped_enum, newtag);
23510 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23512 tree value;
23513 tree decl;
23515 decl = TREE_VALUE (e);
23516 /* Note that in a template enum, the TREE_VALUE is the
23517 CONST_DECL, not the corresponding INTEGER_CST. */
23518 value = tsubst_expr (DECL_INITIAL (decl),
23519 args, tf_warning_or_error, NULL_TREE,
23520 /*integral_constant_expression_p=*/true);
23522 /* Give this enumeration constant the correct access. */
23523 set_current_access_from_decl (decl);
23525 /* Actually build the enumerator itself. Here we're assuming that
23526 enumerators can't have dependent attributes. */
23527 build_enumerator (DECL_NAME (decl), value, newtag,
23528 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23531 if (SCOPED_ENUM_P (newtag))
23532 finish_scope ();
23534 finish_enum_value_list (newtag);
23535 finish_enum (newtag);
23537 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23538 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23541 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23542 its type -- but without substituting the innermost set of template
23543 arguments. So, innermost set of template parameters will appear in
23544 the type. */
23546 tree
23547 get_mostly_instantiated_function_type (tree decl)
23549 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23550 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23553 /* Return truthvalue if we're processing a template different from
23554 the last one involved in diagnostics. */
23555 bool
23556 problematic_instantiation_changed (void)
23558 return current_tinst_level != last_error_tinst_level;
23561 /* Remember current template involved in diagnostics. */
23562 void
23563 record_last_problematic_instantiation (void)
23565 last_error_tinst_level = current_tinst_level;
23568 struct tinst_level *
23569 current_instantiation (void)
23571 return current_tinst_level;
23574 /* Return TRUE if current_function_decl is being instantiated, false
23575 otherwise. */
23577 bool
23578 instantiating_current_function_p (void)
23580 return (current_instantiation ()
23581 && current_instantiation ()->decl == current_function_decl);
23584 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23585 type. Return zero for ok, nonzero for disallowed. Issue error and
23586 warning messages under control of COMPLAIN. */
23588 static int
23589 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23591 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23592 return 0;
23593 else if (POINTER_TYPE_P (type))
23594 return 0;
23595 else if (TYPE_PTRMEM_P (type))
23596 return 0;
23597 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23598 return 0;
23599 else if (TREE_CODE (type) == TYPENAME_TYPE)
23600 return 0;
23601 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23602 return 0;
23603 else if (TREE_CODE (type) == NULLPTR_TYPE)
23604 return 0;
23605 /* A bound template template parm could later be instantiated to have a valid
23606 nontype parm type via an alias template. */
23607 else if (cxx_dialect >= cxx11
23608 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23609 return 0;
23611 if (complain & tf_error)
23613 if (type == error_mark_node)
23614 inform (input_location, "invalid template non-type parameter");
23615 else
23616 error ("%q#T is not a valid type for a template non-type parameter",
23617 type);
23619 return 1;
23622 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23623 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23625 static bool
23626 dependent_type_p_r (tree type)
23628 tree scope;
23630 /* [temp.dep.type]
23632 A type is dependent if it is:
23634 -- a template parameter. Template template parameters are types
23635 for us (since TYPE_P holds true for them) so we handle
23636 them here. */
23637 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23638 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23639 return true;
23640 /* -- a qualified-id with a nested-name-specifier which contains a
23641 class-name that names a dependent type or whose unqualified-id
23642 names a dependent type. */
23643 if (TREE_CODE (type) == TYPENAME_TYPE)
23644 return true;
23646 /* An alias template specialization can be dependent even if the
23647 resulting type is not. */
23648 if (dependent_alias_template_spec_p (type))
23649 return true;
23651 /* -- a cv-qualified type where the cv-unqualified type is
23652 dependent.
23653 No code is necessary for this bullet; the code below handles
23654 cv-qualified types, and we don't want to strip aliases with
23655 TYPE_MAIN_VARIANT because of DR 1558. */
23656 /* -- a compound type constructed from any dependent type. */
23657 if (TYPE_PTRMEM_P (type))
23658 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23659 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23660 (type)));
23661 else if (TYPE_PTR_P (type)
23662 || TREE_CODE (type) == REFERENCE_TYPE)
23663 return dependent_type_p (TREE_TYPE (type));
23664 else if (TREE_CODE (type) == FUNCTION_TYPE
23665 || TREE_CODE (type) == METHOD_TYPE)
23667 tree arg_type;
23669 if (dependent_type_p (TREE_TYPE (type)))
23670 return true;
23671 for (arg_type = TYPE_ARG_TYPES (type);
23672 arg_type;
23673 arg_type = TREE_CHAIN (arg_type))
23674 if (dependent_type_p (TREE_VALUE (arg_type)))
23675 return true;
23676 if (cxx_dialect >= cxx1z)
23678 /* A value-dependent noexcept-specifier makes the type dependent. */
23679 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23680 if (spec && TREE_PURPOSE (spec)
23681 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23682 return true;
23684 return false;
23686 /* -- an array type constructed from any dependent type or whose
23687 size is specified by a constant expression that is
23688 value-dependent.
23690 We checked for type- and value-dependence of the bounds in
23691 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23692 if (TREE_CODE (type) == ARRAY_TYPE)
23694 if (TYPE_DOMAIN (type)
23695 && dependent_type_p (TYPE_DOMAIN (type)))
23696 return true;
23697 return dependent_type_p (TREE_TYPE (type));
23700 /* -- a template-id in which either the template name is a template
23701 parameter ... */
23702 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23703 return true;
23704 /* ... or any of the template arguments is a dependent type or
23705 an expression that is type-dependent or value-dependent. */
23706 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23707 && (any_dependent_template_arguments_p
23708 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23709 return true;
23711 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23712 dependent; if the argument of the `typeof' expression is not
23713 type-dependent, then it should already been have resolved. */
23714 if (TREE_CODE (type) == TYPEOF_TYPE
23715 || TREE_CODE (type) == DECLTYPE_TYPE
23716 || TREE_CODE (type) == UNDERLYING_TYPE)
23717 return true;
23719 /* A template argument pack is dependent if any of its packed
23720 arguments are. */
23721 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23723 tree args = ARGUMENT_PACK_ARGS (type);
23724 int i, len = TREE_VEC_LENGTH (args);
23725 for (i = 0; i < len; ++i)
23726 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23727 return true;
23730 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23731 be template parameters. */
23732 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23733 return true;
23735 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23736 return true;
23738 /* The standard does not specifically mention types that are local
23739 to template functions or local classes, but they should be
23740 considered dependent too. For example:
23742 template <int I> void f() {
23743 enum E { a = I };
23744 S<sizeof (E)> s;
23747 The size of `E' cannot be known until the value of `I' has been
23748 determined. Therefore, `E' must be considered dependent. */
23749 scope = TYPE_CONTEXT (type);
23750 if (scope && TYPE_P (scope))
23751 return dependent_type_p (scope);
23752 /* Don't use type_dependent_expression_p here, as it can lead
23753 to infinite recursion trying to determine whether a lambda
23754 nested in a lambda is dependent (c++/47687). */
23755 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23756 && DECL_LANG_SPECIFIC (scope)
23757 && DECL_TEMPLATE_INFO (scope)
23758 && (any_dependent_template_arguments_p
23759 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23760 return true;
23762 /* Other types are non-dependent. */
23763 return false;
23766 /* Returns TRUE if TYPE is dependent, in the sense of
23767 [temp.dep.type]. Note that a NULL type is considered dependent. */
23769 bool
23770 dependent_type_p (tree type)
23772 /* If there are no template parameters in scope, then there can't be
23773 any dependent types. */
23774 if (!processing_template_decl)
23776 /* If we are not processing a template, then nobody should be
23777 providing us with a dependent type. */
23778 gcc_assert (type);
23779 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23780 return false;
23783 /* If the type is NULL, we have not computed a type for the entity
23784 in question; in that case, the type is dependent. */
23785 if (!type)
23786 return true;
23788 /* Erroneous types can be considered non-dependent. */
23789 if (type == error_mark_node)
23790 return false;
23792 /* Getting here with global_type_node means we improperly called this
23793 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23794 gcc_checking_assert (type != global_type_node);
23796 /* If we have not already computed the appropriate value for TYPE,
23797 do so now. */
23798 if (!TYPE_DEPENDENT_P_VALID (type))
23800 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23801 TYPE_DEPENDENT_P_VALID (type) = 1;
23804 return TYPE_DEPENDENT_P (type);
23807 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23808 lookup. In other words, a dependent type that is not the current
23809 instantiation. */
23811 bool
23812 dependent_scope_p (tree scope)
23814 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23815 && !currently_open_class (scope));
23818 /* T is a SCOPE_REF; return whether we need to consider it
23819 instantiation-dependent so that we can check access at instantiation
23820 time even though we know which member it resolves to. */
23822 static bool
23823 instantiation_dependent_scope_ref_p (tree t)
23825 if (DECL_P (TREE_OPERAND (t, 1))
23826 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23827 && accessible_in_template_p (TREE_OPERAND (t, 0),
23828 TREE_OPERAND (t, 1)))
23829 return false;
23830 else
23831 return true;
23834 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23835 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23836 expression. */
23838 /* Note that this predicate is not appropriate for general expressions;
23839 only constant expressions (that satisfy potential_constant_expression)
23840 can be tested for value dependence. */
23842 bool
23843 value_dependent_expression_p (tree expression)
23845 if (!processing_template_decl || expression == NULL_TREE)
23846 return false;
23848 /* A name declared with a dependent type. */
23849 if (DECL_P (expression) && type_dependent_expression_p (expression))
23850 return true;
23852 switch (TREE_CODE (expression))
23854 case BASELINK:
23855 /* A dependent member function of the current instantiation. */
23856 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23858 case FUNCTION_DECL:
23859 /* A dependent member function of the current instantiation. */
23860 if (DECL_CLASS_SCOPE_P (expression)
23861 && dependent_type_p (DECL_CONTEXT (expression)))
23862 return true;
23863 break;
23865 case IDENTIFIER_NODE:
23866 /* A name that has not been looked up -- must be dependent. */
23867 return true;
23869 case TEMPLATE_PARM_INDEX:
23870 /* A non-type template parm. */
23871 return true;
23873 case CONST_DECL:
23874 /* A non-type template parm. */
23875 if (DECL_TEMPLATE_PARM_P (expression))
23876 return true;
23877 return value_dependent_expression_p (DECL_INITIAL (expression));
23879 case VAR_DECL:
23880 /* A constant with literal type and is initialized
23881 with an expression that is value-dependent.
23883 Note that a non-dependent parenthesized initializer will have
23884 already been replaced with its constant value, so if we see
23885 a TREE_LIST it must be dependent. */
23886 if (DECL_INITIAL (expression)
23887 && decl_constant_var_p (expression)
23888 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23889 /* cp_finish_decl doesn't fold reference initializers. */
23890 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23891 || type_dependent_expression_p (DECL_INITIAL (expression))
23892 || value_dependent_expression_p (DECL_INITIAL (expression))))
23893 return true;
23894 if (DECL_HAS_VALUE_EXPR_P (expression))
23896 tree value_expr = DECL_VALUE_EXPR (expression);
23897 if (type_dependent_expression_p (value_expr))
23898 return true;
23900 return false;
23902 case DYNAMIC_CAST_EXPR:
23903 case STATIC_CAST_EXPR:
23904 case CONST_CAST_EXPR:
23905 case REINTERPRET_CAST_EXPR:
23906 case CAST_EXPR:
23907 /* These expressions are value-dependent if the type to which
23908 the cast occurs is dependent or the expression being casted
23909 is value-dependent. */
23911 tree type = TREE_TYPE (expression);
23913 if (dependent_type_p (type))
23914 return true;
23916 /* A functional cast has a list of operands. */
23917 expression = TREE_OPERAND (expression, 0);
23918 if (!expression)
23920 /* If there are no operands, it must be an expression such
23921 as "int()". This should not happen for aggregate types
23922 because it would form non-constant expressions. */
23923 gcc_assert (cxx_dialect >= cxx11
23924 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23926 return false;
23929 if (TREE_CODE (expression) == TREE_LIST)
23930 return any_value_dependent_elements_p (expression);
23932 return value_dependent_expression_p (expression);
23935 case SIZEOF_EXPR:
23936 if (SIZEOF_EXPR_TYPE_P (expression))
23937 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23938 /* FALLTHRU */
23939 case ALIGNOF_EXPR:
23940 case TYPEID_EXPR:
23941 /* A `sizeof' expression is value-dependent if the operand is
23942 type-dependent or is a pack expansion. */
23943 expression = TREE_OPERAND (expression, 0);
23944 if (PACK_EXPANSION_P (expression))
23945 return true;
23946 else if (TYPE_P (expression))
23947 return dependent_type_p (expression);
23948 return instantiation_dependent_uneval_expression_p (expression);
23950 case AT_ENCODE_EXPR:
23951 /* An 'encode' expression is value-dependent if the operand is
23952 type-dependent. */
23953 expression = TREE_OPERAND (expression, 0);
23954 return dependent_type_p (expression);
23956 case NOEXCEPT_EXPR:
23957 expression = TREE_OPERAND (expression, 0);
23958 return instantiation_dependent_uneval_expression_p (expression);
23960 case SCOPE_REF:
23961 /* All instantiation-dependent expressions should also be considered
23962 value-dependent. */
23963 return instantiation_dependent_scope_ref_p (expression);
23965 case COMPONENT_REF:
23966 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23967 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23969 case NONTYPE_ARGUMENT_PACK:
23970 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23971 is value-dependent. */
23973 tree values = ARGUMENT_PACK_ARGS (expression);
23974 int i, len = TREE_VEC_LENGTH (values);
23976 for (i = 0; i < len; ++i)
23977 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23978 return true;
23980 return false;
23983 case TRAIT_EXPR:
23985 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23986 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23987 || (type2 ? dependent_type_p (type2) : false));
23990 case MODOP_EXPR:
23991 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23992 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23994 case ARRAY_REF:
23995 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23996 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23998 case ADDR_EXPR:
24000 tree op = TREE_OPERAND (expression, 0);
24001 return (value_dependent_expression_p (op)
24002 || has_value_dependent_address (op));
24005 case REQUIRES_EXPR:
24006 /* Treat all requires-expressions as value-dependent so
24007 we don't try to fold them. */
24008 return true;
24010 case TYPE_REQ:
24011 return dependent_type_p (TREE_OPERAND (expression, 0));
24013 case CALL_EXPR:
24015 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24016 return true;
24017 tree fn = get_callee_fndecl (expression);
24018 int i, nargs;
24019 nargs = call_expr_nargs (expression);
24020 for (i = 0; i < nargs; ++i)
24022 tree op = CALL_EXPR_ARG (expression, i);
24023 /* In a call to a constexpr member function, look through the
24024 implicit ADDR_EXPR on the object argument so that it doesn't
24025 cause the call to be considered value-dependent. We also
24026 look through it in potential_constant_expression. */
24027 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24028 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24029 && TREE_CODE (op) == ADDR_EXPR)
24030 op = TREE_OPERAND (op, 0);
24031 if (value_dependent_expression_p (op))
24032 return true;
24034 return false;
24037 case TEMPLATE_ID_EXPR:
24038 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
24039 type-dependent. */
24040 return type_dependent_expression_p (expression)
24041 || variable_concept_p (TREE_OPERAND (expression, 0));
24043 case CONSTRUCTOR:
24045 unsigned ix;
24046 tree val;
24047 if (dependent_type_p (TREE_TYPE (expression)))
24048 return true;
24049 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24050 if (value_dependent_expression_p (val))
24051 return true;
24052 return false;
24055 case STMT_EXPR:
24056 /* Treat a GNU statement expression as dependent to avoid crashing
24057 under instantiate_non_dependent_expr; it can't be constant. */
24058 return true;
24060 default:
24061 /* A constant expression is value-dependent if any subexpression is
24062 value-dependent. */
24063 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24065 case tcc_reference:
24066 case tcc_unary:
24067 case tcc_comparison:
24068 case tcc_binary:
24069 case tcc_expression:
24070 case tcc_vl_exp:
24072 int i, len = cp_tree_operand_length (expression);
24074 for (i = 0; i < len; i++)
24076 tree t = TREE_OPERAND (expression, i);
24078 /* In some cases, some of the operands may be missing.
24079 (For example, in the case of PREDECREMENT_EXPR, the
24080 amount to increment by may be missing.) That doesn't
24081 make the expression dependent. */
24082 if (t && value_dependent_expression_p (t))
24083 return true;
24086 break;
24087 default:
24088 break;
24090 break;
24093 /* The expression is not value-dependent. */
24094 return false;
24097 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24098 [temp.dep.expr]. Note that an expression with no type is
24099 considered dependent. Other parts of the compiler arrange for an
24100 expression with type-dependent subexpressions to have no type, so
24101 this function doesn't have to be fully recursive. */
24103 bool
24104 type_dependent_expression_p (tree expression)
24106 if (!processing_template_decl)
24107 return false;
24109 if (expression == NULL_TREE || expression == error_mark_node)
24110 return false;
24112 /* An unresolved name is always dependent. */
24113 if (identifier_p (expression)
24114 || TREE_CODE (expression) == USING_DECL
24115 || TREE_CODE (expression) == WILDCARD_DECL)
24116 return true;
24118 /* A fold expression is type-dependent. */
24119 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24120 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24121 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24122 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24123 return true;
24125 /* Some expression forms are never type-dependent. */
24126 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24127 || TREE_CODE (expression) == SIZEOF_EXPR
24128 || TREE_CODE (expression) == ALIGNOF_EXPR
24129 || TREE_CODE (expression) == AT_ENCODE_EXPR
24130 || TREE_CODE (expression) == NOEXCEPT_EXPR
24131 || TREE_CODE (expression) == TRAIT_EXPR
24132 || TREE_CODE (expression) == TYPEID_EXPR
24133 || TREE_CODE (expression) == DELETE_EXPR
24134 || TREE_CODE (expression) == VEC_DELETE_EXPR
24135 || TREE_CODE (expression) == THROW_EXPR
24136 || TREE_CODE (expression) == REQUIRES_EXPR)
24137 return false;
24139 /* The types of these expressions depends only on the type to which
24140 the cast occurs. */
24141 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24142 || TREE_CODE (expression) == STATIC_CAST_EXPR
24143 || TREE_CODE (expression) == CONST_CAST_EXPR
24144 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24145 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24146 || TREE_CODE (expression) == CAST_EXPR)
24147 return dependent_type_p (TREE_TYPE (expression));
24149 /* The types of these expressions depends only on the type created
24150 by the expression. */
24151 if (TREE_CODE (expression) == NEW_EXPR
24152 || TREE_CODE (expression) == VEC_NEW_EXPR)
24154 /* For NEW_EXPR tree nodes created inside a template, either
24155 the object type itself or a TREE_LIST may appear as the
24156 operand 1. */
24157 tree type = TREE_OPERAND (expression, 1);
24158 if (TREE_CODE (type) == TREE_LIST)
24159 /* This is an array type. We need to check array dimensions
24160 as well. */
24161 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24162 || value_dependent_expression_p
24163 (TREE_OPERAND (TREE_VALUE (type), 1));
24164 else
24165 return dependent_type_p (type);
24168 if (TREE_CODE (expression) == SCOPE_REF)
24170 tree scope = TREE_OPERAND (expression, 0);
24171 tree name = TREE_OPERAND (expression, 1);
24173 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24174 contains an identifier associated by name lookup with one or more
24175 declarations declared with a dependent type, or...a
24176 nested-name-specifier or qualified-id that names a member of an
24177 unknown specialization. */
24178 return (type_dependent_expression_p (name)
24179 || dependent_scope_p (scope));
24182 if (TREE_CODE (expression) == TEMPLATE_DECL
24183 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24184 return uses_outer_template_parms (expression);
24186 if (TREE_CODE (expression) == STMT_EXPR)
24187 expression = stmt_expr_value_expr (expression);
24189 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24191 tree elt;
24192 unsigned i;
24194 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24196 if (type_dependent_expression_p (elt))
24197 return true;
24199 return false;
24202 /* A static data member of the current instantiation with incomplete
24203 array type is type-dependent, as the definition and specializations
24204 can have different bounds. */
24205 if (VAR_P (expression)
24206 && DECL_CLASS_SCOPE_P (expression)
24207 && dependent_type_p (DECL_CONTEXT (expression))
24208 && VAR_HAD_UNKNOWN_BOUND (expression))
24209 return true;
24211 /* An array of unknown bound depending on a variadic parameter, eg:
24213 template<typename... Args>
24214 void foo (Args... args)
24216 int arr[] = { args... };
24219 template<int... vals>
24220 void bar ()
24222 int arr[] = { vals... };
24225 If the array has no length and has an initializer, it must be that
24226 we couldn't determine its length in cp_complete_array_type because
24227 it is dependent. */
24228 if (VAR_P (expression)
24229 && TREE_TYPE (expression) != NULL_TREE
24230 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24231 && !TYPE_DOMAIN (TREE_TYPE (expression))
24232 && DECL_INITIAL (expression))
24233 return true;
24235 /* A function or variable template-id is type-dependent if it has any
24236 dependent template arguments. */
24237 if (VAR_OR_FUNCTION_DECL_P (expression)
24238 && DECL_LANG_SPECIFIC (expression)
24239 && DECL_TEMPLATE_INFO (expression))
24241 /* Consider the innermost template arguments, since those are the ones
24242 that come from the template-id; the template arguments for the
24243 enclosing class do not make it type-dependent unless they are used in
24244 the type of the decl. */
24245 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24246 && (any_dependent_template_arguments_p
24247 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24248 return true;
24250 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24251 type-dependent. Checking this is important for functions with auto
24252 return type, which looks like a dependent type. */
24253 if (TREE_CODE (expression) == FUNCTION_DECL
24254 && (!DECL_CLASS_SCOPE_P (expression)
24255 || !dependent_type_p (DECL_CONTEXT (expression)))
24256 && (!DECL_FRIEND_CONTEXT (expression)
24257 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression)))
24258 && !DECL_LOCAL_FUNCTION_P (expression))
24260 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24261 || undeduced_auto_decl (expression));
24262 return false;
24266 /* Always dependent, on the number of arguments if nothing else. */
24267 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24268 return true;
24270 if (TREE_TYPE (expression) == unknown_type_node)
24272 if (TREE_CODE (expression) == ADDR_EXPR)
24273 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24274 if (TREE_CODE (expression) == COMPONENT_REF
24275 || TREE_CODE (expression) == OFFSET_REF)
24277 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24278 return true;
24279 expression = TREE_OPERAND (expression, 1);
24280 if (identifier_p (expression))
24281 return false;
24283 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24284 if (TREE_CODE (expression) == SCOPE_REF)
24285 return false;
24287 if (BASELINK_P (expression))
24289 if (BASELINK_OPTYPE (expression)
24290 && dependent_type_p (BASELINK_OPTYPE (expression)))
24291 return true;
24292 expression = BASELINK_FUNCTIONS (expression);
24295 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24297 if (any_dependent_template_arguments_p
24298 (TREE_OPERAND (expression, 1)))
24299 return true;
24300 expression = TREE_OPERAND (expression, 0);
24301 if (identifier_p (expression))
24302 return true;
24305 gcc_assert (TREE_CODE (expression) == OVERLOAD
24306 || TREE_CODE (expression) == FUNCTION_DECL);
24308 for (lkp_iterator iter (expression); iter; ++iter)
24309 if (type_dependent_expression_p (*iter))
24310 return true;
24312 return false;
24315 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24317 /* Dependent type attributes might not have made it from the decl to
24318 the type yet. */
24319 if (DECL_P (expression)
24320 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24321 return true;
24323 return (dependent_type_p (TREE_TYPE (expression)));
24326 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24327 type-dependent if the expression refers to a member of the current
24328 instantiation and the type of the referenced member is dependent, or the
24329 class member access expression refers to a member of an unknown
24330 specialization.
24332 This function returns true if the OBJECT in such a class member access
24333 expression is of an unknown specialization. */
24335 bool
24336 type_dependent_object_expression_p (tree object)
24338 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24339 dependent. */
24340 if (TREE_CODE (object) == IDENTIFIER_NODE)
24341 return true;
24342 tree scope = TREE_TYPE (object);
24343 return (!scope || dependent_scope_p (scope));
24346 /* walk_tree callback function for instantiation_dependent_expression_p,
24347 below. Returns non-zero if a dependent subexpression is found. */
24349 static tree
24350 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24351 void * /*data*/)
24353 if (TYPE_P (*tp))
24355 /* We don't have to worry about decltype currently because decltype
24356 of an instantiation-dependent expr is a dependent type. This
24357 might change depending on the resolution of DR 1172. */
24358 *walk_subtrees = false;
24359 return NULL_TREE;
24361 enum tree_code code = TREE_CODE (*tp);
24362 switch (code)
24364 /* Don't treat an argument list as dependent just because it has no
24365 TREE_TYPE. */
24366 case TREE_LIST:
24367 case TREE_VEC:
24368 return NULL_TREE;
24370 case TEMPLATE_PARM_INDEX:
24371 return *tp;
24373 /* Handle expressions with type operands. */
24374 case SIZEOF_EXPR:
24375 case ALIGNOF_EXPR:
24376 case TYPEID_EXPR:
24377 case AT_ENCODE_EXPR:
24379 tree op = TREE_OPERAND (*tp, 0);
24380 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24381 op = TREE_TYPE (op);
24382 if (TYPE_P (op))
24384 if (dependent_type_p (op))
24385 return *tp;
24386 else
24388 *walk_subtrees = false;
24389 return NULL_TREE;
24392 break;
24395 case COMPONENT_REF:
24396 if (identifier_p (TREE_OPERAND (*tp, 1)))
24397 /* In a template, finish_class_member_access_expr creates a
24398 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24399 type-dependent, so that we can check access control at
24400 instantiation time (PR 42277). See also Core issue 1273. */
24401 return *tp;
24402 break;
24404 case SCOPE_REF:
24405 if (instantiation_dependent_scope_ref_p (*tp))
24406 return *tp;
24407 else
24408 break;
24410 /* Treat statement-expressions as dependent. */
24411 case BIND_EXPR:
24412 return *tp;
24414 /* Treat requires-expressions as dependent. */
24415 case REQUIRES_EXPR:
24416 return *tp;
24418 case CALL_EXPR:
24419 /* Treat calls to function concepts as dependent. */
24420 if (function_concept_check_p (*tp))
24421 return *tp;
24422 break;
24424 case TEMPLATE_ID_EXPR:
24425 /* And variable concepts. */
24426 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24427 return *tp;
24428 break;
24430 default:
24431 break;
24434 if (type_dependent_expression_p (*tp))
24435 return *tp;
24436 else
24437 return NULL_TREE;
24440 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24441 sense defined by the ABI:
24443 "An expression is instantiation-dependent if it is type-dependent
24444 or value-dependent, or it has a subexpression that is type-dependent
24445 or value-dependent."
24447 Except don't actually check value-dependence for unevaluated expressions,
24448 because in sizeof(i) we don't care about the value of i. Checking
24449 type-dependence will in turn check value-dependence of array bounds/template
24450 arguments as needed. */
24452 bool
24453 instantiation_dependent_uneval_expression_p (tree expression)
24455 tree result;
24457 if (!processing_template_decl)
24458 return false;
24460 if (expression == error_mark_node)
24461 return false;
24463 result = cp_walk_tree_without_duplicates (&expression,
24464 instantiation_dependent_r, NULL);
24465 return result != NULL_TREE;
24468 /* As above, but also check value-dependence of the expression as a whole. */
24470 bool
24471 instantiation_dependent_expression_p (tree expression)
24473 return (instantiation_dependent_uneval_expression_p (expression)
24474 || value_dependent_expression_p (expression));
24477 /* Like type_dependent_expression_p, but it also works while not processing
24478 a template definition, i.e. during substitution or mangling. */
24480 bool
24481 type_dependent_expression_p_push (tree expr)
24483 bool b;
24484 ++processing_template_decl;
24485 b = type_dependent_expression_p (expr);
24486 --processing_template_decl;
24487 return b;
24490 /* Returns TRUE if ARGS contains a type-dependent expression. */
24492 bool
24493 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24495 unsigned int i;
24496 tree arg;
24498 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24500 if (type_dependent_expression_p (arg))
24501 return true;
24503 return false;
24506 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24507 expressions) contains any type-dependent expressions. */
24509 bool
24510 any_type_dependent_elements_p (const_tree list)
24512 for (; list; list = TREE_CHAIN (list))
24513 if (type_dependent_expression_p (TREE_VALUE (list)))
24514 return true;
24516 return false;
24519 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24520 expressions) contains any value-dependent expressions. */
24522 bool
24523 any_value_dependent_elements_p (const_tree list)
24525 for (; list; list = TREE_CHAIN (list))
24526 if (value_dependent_expression_p (TREE_VALUE (list)))
24527 return true;
24529 return false;
24532 /* Returns TRUE if the ARG (a template argument) is dependent. */
24534 bool
24535 dependent_template_arg_p (tree arg)
24537 if (!processing_template_decl)
24538 return false;
24540 /* Assume a template argument that was wrongly written by the user
24541 is dependent. This is consistent with what
24542 any_dependent_template_arguments_p [that calls this function]
24543 does. */
24544 if (!arg || arg == error_mark_node)
24545 return true;
24547 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24548 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24550 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24551 return true;
24552 if (TREE_CODE (arg) == TEMPLATE_DECL)
24554 if (DECL_TEMPLATE_PARM_P (arg))
24555 return true;
24556 /* A member template of a dependent class is not necessarily
24557 type-dependent, but it is a dependent template argument because it
24558 will be a member of an unknown specialization to that template. */
24559 tree scope = CP_DECL_CONTEXT (arg);
24560 return TYPE_P (scope) && dependent_type_p (scope);
24562 else if (ARGUMENT_PACK_P (arg))
24564 tree args = ARGUMENT_PACK_ARGS (arg);
24565 int i, len = TREE_VEC_LENGTH (args);
24566 for (i = 0; i < len; ++i)
24568 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24569 return true;
24572 return false;
24574 else if (TYPE_P (arg))
24575 return dependent_type_p (arg);
24576 else
24577 return (type_dependent_expression_p (arg)
24578 || value_dependent_expression_p (arg));
24581 /* Returns true if ARGS (a collection of template arguments) contains
24582 any types that require structural equality testing. */
24584 bool
24585 any_template_arguments_need_structural_equality_p (tree args)
24587 int i;
24588 int j;
24590 if (!args)
24591 return false;
24592 if (args == error_mark_node)
24593 return true;
24595 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24597 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24598 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24600 tree arg = TREE_VEC_ELT (level, j);
24601 tree packed_args = NULL_TREE;
24602 int k, len = 1;
24604 if (ARGUMENT_PACK_P (arg))
24606 /* Look inside the argument pack. */
24607 packed_args = ARGUMENT_PACK_ARGS (arg);
24608 len = TREE_VEC_LENGTH (packed_args);
24611 for (k = 0; k < len; ++k)
24613 if (packed_args)
24614 arg = TREE_VEC_ELT (packed_args, k);
24616 if (error_operand_p (arg))
24617 return true;
24618 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24619 continue;
24620 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24621 return true;
24622 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24623 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24624 return true;
24629 return false;
24632 /* Returns true if ARGS (a collection of template arguments) contains
24633 any dependent arguments. */
24635 bool
24636 any_dependent_template_arguments_p (const_tree args)
24638 int i;
24639 int j;
24641 if (!args)
24642 return false;
24643 if (args == error_mark_node)
24644 return true;
24646 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24648 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24649 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24650 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24651 return true;
24654 return false;
24657 /* Returns TRUE if the template TMPL is type-dependent. */
24659 bool
24660 dependent_template_p (tree tmpl)
24662 if (TREE_CODE (tmpl) == OVERLOAD)
24664 for (lkp_iterator iter (tmpl); iter; ++iter)
24665 if (dependent_template_p (*iter))
24666 return true;
24667 return false;
24670 /* Template template parameters are dependent. */
24671 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24672 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24673 return true;
24674 /* So are names that have not been looked up. */
24675 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24676 return true;
24677 return false;
24680 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24682 bool
24683 dependent_template_id_p (tree tmpl, tree args)
24685 return (dependent_template_p (tmpl)
24686 || any_dependent_template_arguments_p (args));
24689 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24690 are dependent. */
24692 bool
24693 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24695 int i;
24697 if (!processing_template_decl)
24698 return false;
24700 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24702 tree decl = TREE_VEC_ELT (declv, i);
24703 tree init = TREE_VEC_ELT (initv, i);
24704 tree cond = TREE_VEC_ELT (condv, i);
24705 tree incr = TREE_VEC_ELT (incrv, i);
24707 if (type_dependent_expression_p (decl)
24708 || TREE_CODE (decl) == SCOPE_REF)
24709 return true;
24711 if (init && type_dependent_expression_p (init))
24712 return true;
24714 if (type_dependent_expression_p (cond))
24715 return true;
24717 if (COMPARISON_CLASS_P (cond)
24718 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24719 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24720 return true;
24722 if (TREE_CODE (incr) == MODOP_EXPR)
24724 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24725 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24726 return true;
24728 else if (type_dependent_expression_p (incr))
24729 return true;
24730 else if (TREE_CODE (incr) == MODIFY_EXPR)
24732 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24733 return true;
24734 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24736 tree t = TREE_OPERAND (incr, 1);
24737 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24738 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24739 return true;
24744 return false;
24747 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24748 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24749 no such TYPE can be found. Note that this function peers inside
24750 uninstantiated templates and therefore should be used only in
24751 extremely limited situations. ONLY_CURRENT_P restricts this
24752 peering to the currently open classes hierarchy (which is required
24753 when comparing types). */
24755 tree
24756 resolve_typename_type (tree type, bool only_current_p)
24758 tree scope;
24759 tree name;
24760 tree decl;
24761 int quals;
24762 tree pushed_scope;
24763 tree result;
24765 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24767 scope = TYPE_CONTEXT (type);
24768 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24769 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24770 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24771 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24772 identifier of the TYPENAME_TYPE anymore.
24773 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24774 TYPENAME_TYPE instead, we avoid messing up with a possible
24775 typedef variant case. */
24776 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24778 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24779 it first before we can figure out what NAME refers to. */
24780 if (TREE_CODE (scope) == TYPENAME_TYPE)
24782 if (TYPENAME_IS_RESOLVING_P (scope))
24783 /* Given a class template A with a dependent base with nested type C,
24784 typedef typename A::C::C C will land us here, as trying to resolve
24785 the initial A::C leads to the local C typedef, which leads back to
24786 A::C::C. So we break the recursion now. */
24787 return type;
24788 else
24789 scope = resolve_typename_type (scope, only_current_p);
24791 /* If we don't know what SCOPE refers to, then we cannot resolve the
24792 TYPENAME_TYPE. */
24793 if (!CLASS_TYPE_P (scope))
24794 return type;
24795 /* If this is a typedef, we don't want to look inside (c++/11987). */
24796 if (typedef_variant_p (type))
24797 return type;
24798 /* If SCOPE isn't the template itself, it will not have a valid
24799 TYPE_FIELDS list. */
24800 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24801 /* scope is either the template itself or a compatible instantiation
24802 like X<T>, so look up the name in the original template. */
24803 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24804 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24805 gcc_checking_assert (uses_template_parms (scope));
24806 /* If scope has no fields, it can't be a current instantiation. Check this
24807 before currently_open_class to avoid infinite recursion (71515). */
24808 if (!TYPE_FIELDS (scope))
24809 return type;
24810 /* If the SCOPE is not the current instantiation, there's no reason
24811 to look inside it. */
24812 if (only_current_p && !currently_open_class (scope))
24813 return type;
24814 /* Enter the SCOPE so that name lookup will be resolved as if we
24815 were in the class definition. In particular, SCOPE will no
24816 longer be considered a dependent type. */
24817 pushed_scope = push_scope (scope);
24818 /* Look up the declaration. */
24819 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24820 tf_warning_or_error);
24822 result = NULL_TREE;
24824 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24825 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24826 tree fullname = TYPENAME_TYPE_FULLNAME (type);
24827 if (!decl)
24828 /*nop*/;
24829 else if (identifier_p (fullname)
24830 && TREE_CODE (decl) == TYPE_DECL)
24832 result = TREE_TYPE (decl);
24833 if (result == error_mark_node)
24834 result = NULL_TREE;
24836 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
24837 && DECL_CLASS_TEMPLATE_P (decl))
24839 /* Obtain the template and the arguments. */
24840 tree tmpl = TREE_OPERAND (fullname, 0);
24841 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
24843 /* We get here with a plain identifier because a previous tentative
24844 parse of the nested-name-specifier as part of a ptr-operator saw
24845 ::template X<A>. The use of ::template is necessary in a
24846 ptr-operator, but wrong in a declarator-id.
24848 [temp.names]: In a qualified-id of a declarator-id, the keyword
24849 template shall not appear at the top level. */
24850 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
24851 "keyword %<template%> not allowed in declarator-id");
24852 tmpl = decl;
24854 tree args = TREE_OPERAND (fullname, 1);
24855 /* Instantiate the template. */
24856 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24857 /*entering_scope=*/true,
24858 tf_error | tf_user);
24859 if (result == error_mark_node)
24860 result = NULL_TREE;
24863 /* Leave the SCOPE. */
24864 if (pushed_scope)
24865 pop_scope (pushed_scope);
24867 /* If we failed to resolve it, return the original typename. */
24868 if (!result)
24869 return type;
24871 /* If lookup found a typename type, resolve that too. */
24872 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24874 /* Ill-formed programs can cause infinite recursion here, so we
24875 must catch that. */
24876 TYPENAME_IS_RESOLVING_P (result) = 1;
24877 result = resolve_typename_type (result, only_current_p);
24878 TYPENAME_IS_RESOLVING_P (result) = 0;
24881 /* Qualify the resulting type. */
24882 quals = cp_type_quals (type);
24883 if (quals)
24884 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24886 return result;
24889 /* EXPR is an expression which is not type-dependent. Return a proxy
24890 for EXPR that can be used to compute the types of larger
24891 expressions containing EXPR. */
24893 tree
24894 build_non_dependent_expr (tree expr)
24896 tree inner_expr;
24898 /* When checking, try to get a constant value for all non-dependent
24899 expressions in order to expose bugs in *_dependent_expression_p
24900 and constexpr. This can affect code generation, see PR70704, so
24901 only do this for -fchecking=2. */
24902 if (flag_checking > 1
24903 && cxx_dialect >= cxx11
24904 /* Don't do this during nsdmi parsing as it can lead to
24905 unexpected recursive instantiations. */
24906 && !parsing_nsdmi ()
24907 /* Don't do this during concept expansion either and for
24908 the same reason. */
24909 && !expanding_concept ())
24910 fold_non_dependent_expr (expr);
24912 /* Preserve OVERLOADs; the functions must be available to resolve
24913 types. */
24914 inner_expr = expr;
24915 if (TREE_CODE (inner_expr) == STMT_EXPR)
24916 inner_expr = stmt_expr_value_expr (inner_expr);
24917 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24918 inner_expr = TREE_OPERAND (inner_expr, 0);
24919 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24920 inner_expr = TREE_OPERAND (inner_expr, 1);
24921 if (is_overloaded_fn (inner_expr)
24922 || TREE_CODE (inner_expr) == OFFSET_REF)
24923 return expr;
24924 /* There is no need to return a proxy for a variable. */
24925 if (VAR_P (expr))
24926 return expr;
24927 /* Preserve string constants; conversions from string constants to
24928 "char *" are allowed, even though normally a "const char *"
24929 cannot be used to initialize a "char *". */
24930 if (TREE_CODE (expr) == STRING_CST)
24931 return expr;
24932 /* Preserve void and arithmetic constants, as an optimization -- there is no
24933 reason to create a new node. */
24934 if (TREE_CODE (expr) == VOID_CST
24935 || TREE_CODE (expr) == INTEGER_CST
24936 || TREE_CODE (expr) == REAL_CST)
24937 return expr;
24938 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24939 There is at least one place where we want to know that a
24940 particular expression is a throw-expression: when checking a ?:
24941 expression, there are special rules if the second or third
24942 argument is a throw-expression. */
24943 if (TREE_CODE (expr) == THROW_EXPR)
24944 return expr;
24946 /* Don't wrap an initializer list, we need to be able to look inside. */
24947 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24948 return expr;
24950 /* Don't wrap a dummy object, we need to be able to test for it. */
24951 if (is_dummy_object (expr))
24952 return expr;
24954 if (TREE_CODE (expr) == COND_EXPR)
24955 return build3 (COND_EXPR,
24956 TREE_TYPE (expr),
24957 TREE_OPERAND (expr, 0),
24958 (TREE_OPERAND (expr, 1)
24959 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24960 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24961 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24962 if (TREE_CODE (expr) == COMPOUND_EXPR
24963 && !COMPOUND_EXPR_OVERLOADED (expr))
24964 return build2 (COMPOUND_EXPR,
24965 TREE_TYPE (expr),
24966 TREE_OPERAND (expr, 0),
24967 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24969 /* If the type is unknown, it can't really be non-dependent */
24970 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24972 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24973 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24976 /* ARGS is a vector of expressions as arguments to a function call.
24977 Replace the arguments with equivalent non-dependent expressions.
24978 This modifies ARGS in place. */
24980 void
24981 make_args_non_dependent (vec<tree, va_gc> *args)
24983 unsigned int ix;
24984 tree arg;
24986 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24988 tree newarg = build_non_dependent_expr (arg);
24989 if (newarg != arg)
24990 (*args)[ix] = newarg;
24994 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24995 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24996 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24998 static tree
24999 make_auto_1 (tree name, bool set_canonical)
25001 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25002 TYPE_NAME (au) = build_decl (input_location,
25003 TYPE_DECL, name, au);
25004 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25005 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25006 (0, processing_template_decl + 1, processing_template_decl + 1,
25007 TYPE_NAME (au), NULL_TREE);
25008 if (set_canonical)
25009 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25010 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25011 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25013 return au;
25016 tree
25017 make_decltype_auto (void)
25019 return make_auto_1 (decltype_auto_identifier, true);
25022 tree
25023 make_auto (void)
25025 return make_auto_1 (auto_identifier, true);
25028 /* Return a C++17 deduction placeholder for class template TMPL. */
25030 tree
25031 make_template_placeholder (tree tmpl)
25033 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25034 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25035 return t;
25038 /* True iff T is a C++17 class template deduction placeholder. */
25040 bool
25041 template_placeholder_p (tree t)
25043 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25046 /* Make a "constrained auto" type-specifier. This is an
25047 auto type with constraints that must be associated after
25048 deduction. The constraint is formed from the given
25049 CONC and its optional sequence of arguments, which are
25050 non-null if written as partial-concept-id. */
25052 tree
25053 make_constrained_auto (tree con, tree args)
25055 tree type = make_auto_1 (auto_identifier, false);
25057 /* Build the constraint. */
25058 tree tmpl = DECL_TI_TEMPLATE (con);
25059 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25060 expr = build_concept_check (expr, type, args);
25062 tree constr = normalize_expression (expr);
25063 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25065 /* Our canonical type depends on the constraint. */
25066 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25068 /* Attach the constraint to the type declaration. */
25069 tree decl = TYPE_NAME (type);
25070 return decl;
25073 /* Given type ARG, return std::initializer_list<ARG>. */
25075 static tree
25076 listify (tree arg)
25078 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25080 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25082 gcc_rich_location richloc (input_location);
25083 maybe_add_include_fixit (&richloc, "<initializer_list>");
25084 error_at_rich_loc (&richloc,
25085 "deducing from brace-enclosed initializer list"
25086 " requires #include <initializer_list>");
25088 return error_mark_node;
25090 tree argvec = make_tree_vec (1);
25091 TREE_VEC_ELT (argvec, 0) = arg;
25093 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25094 NULL_TREE, 0, tf_warning_or_error);
25097 /* Replace auto in TYPE with std::initializer_list<auto>. */
25099 static tree
25100 listify_autos (tree type, tree auto_node)
25102 tree init_auto = listify (auto_node);
25103 tree argvec = make_tree_vec (1);
25104 TREE_VEC_ELT (argvec, 0) = init_auto;
25105 if (processing_template_decl)
25106 argvec = add_to_template_args (current_template_args (), argvec);
25107 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25110 /* Hash traits for hashing possibly constrained 'auto'
25111 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25113 struct auto_hash : default_hash_traits<tree>
25115 static inline hashval_t hash (tree);
25116 static inline bool equal (tree, tree);
25119 /* Hash the 'auto' T. */
25121 inline hashval_t
25122 auto_hash::hash (tree t)
25124 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25125 /* Matching constrained-type-specifiers denote the same template
25126 parameter, so hash the constraint. */
25127 return hash_placeholder_constraint (c);
25128 else
25129 /* But unconstrained autos are all separate, so just hash the pointer. */
25130 return iterative_hash_object (t, 0);
25133 /* Compare two 'auto's. */
25135 inline bool
25136 auto_hash::equal (tree t1, tree t2)
25138 if (t1 == t2)
25139 return true;
25141 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25142 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25144 /* Two unconstrained autos are distinct. */
25145 if (!c1 || !c2)
25146 return false;
25148 return equivalent_placeholder_constraints (c1, c2);
25151 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25152 constrained) auto, add it to the vector. */
25154 static int
25155 extract_autos_r (tree t, void *data)
25157 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25158 if (is_auto (t))
25160 /* All the autos were built with index 0; fix that up now. */
25161 tree *p = hash.find_slot (t, INSERT);
25162 unsigned idx;
25163 if (*p)
25164 /* If this is a repeated constrained-type-specifier, use the index we
25165 chose before. */
25166 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25167 else
25169 /* Otherwise this is new, so use the current count. */
25170 *p = t;
25171 idx = hash.elements () - 1;
25173 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25176 /* Always keep walking. */
25177 return 0;
25180 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25181 says they can appear anywhere in the type. */
25183 static tree
25184 extract_autos (tree type)
25186 hash_set<tree> visited;
25187 hash_table<auto_hash> hash (2);
25189 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25191 tree tree_vec = make_tree_vec (hash.elements());
25192 for (hash_table<auto_hash>::iterator iter = hash.begin();
25193 iter != hash.end(); ++iter)
25195 tree elt = *iter;
25196 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25197 TREE_VEC_ELT (tree_vec, i)
25198 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25201 return tree_vec;
25204 /* The stem for deduction guide names. */
25205 const char *const dguide_base = "__dguide_";
25207 /* Return the name for a deduction guide for class template TMPL. */
25209 tree
25210 dguide_name (tree tmpl)
25212 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25213 tree tname = TYPE_IDENTIFIER (type);
25214 char *buf = (char *) alloca (1 + strlen (dguide_base)
25215 + IDENTIFIER_LENGTH (tname));
25216 memcpy (buf, dguide_base, strlen (dguide_base));
25217 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25218 IDENTIFIER_LENGTH (tname) + 1);
25219 tree dname = get_identifier (buf);
25220 TREE_TYPE (dname) = type;
25221 return dname;
25224 /* True if NAME is the name of a deduction guide. */
25226 bool
25227 dguide_name_p (tree name)
25229 return (TREE_TYPE (name)
25230 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25231 strlen (dguide_base)));
25234 /* True if FN is a deduction guide. */
25236 bool
25237 deduction_guide_p (const_tree fn)
25239 if (DECL_P (fn))
25240 if (tree name = DECL_NAME (fn))
25241 return dguide_name_p (name);
25242 return false;
25245 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25247 bool
25248 copy_guide_p (const_tree fn)
25250 gcc_assert (deduction_guide_p (fn));
25251 if (!DECL_ARTIFICIAL (fn))
25252 return false;
25253 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25254 return (TREE_CHAIN (parms) == void_list_node
25255 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25258 /* True if FN is a guide generated from a constructor template. */
25260 bool
25261 template_guide_p (const_tree fn)
25263 gcc_assert (deduction_guide_p (fn));
25264 if (!DECL_ARTIFICIAL (fn))
25265 return false;
25266 tree tmpl = DECL_TI_TEMPLATE (fn);
25267 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25268 return PRIMARY_TEMPLATE_P (org);
25269 return false;
25272 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25273 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25274 template parameter types. Note that the handling of template template
25275 parameters relies on current_template_parms being set appropriately for the
25276 new template. */
25278 static tree
25279 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25280 tree tsubst_args, tsubst_flags_t complain)
25282 tree oldidx = get_template_parm_index (olddecl);
25284 tree newtype;
25285 if (TREE_CODE (olddecl) == TYPE_DECL
25286 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25288 tree oldtype = TREE_TYPE (olddecl);
25289 newtype = cxx_make_type (TREE_CODE (oldtype));
25290 TYPE_MAIN_VARIANT (newtype) = newtype;
25291 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25292 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25293 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25295 else
25296 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25297 complain, NULL_TREE);
25299 tree newdecl
25300 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25301 DECL_NAME (olddecl), newtype);
25302 SET_DECL_TEMPLATE_PARM_P (newdecl);
25304 tree newidx;
25305 if (TREE_CODE (olddecl) == TYPE_DECL
25306 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25308 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25309 = build_template_parm_index (index, level, level,
25310 newdecl, newtype);
25311 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25312 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25313 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25314 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25316 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25318 DECL_TEMPLATE_RESULT (newdecl)
25319 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25320 DECL_NAME (olddecl), newtype);
25321 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25322 // First create a copy (ttargs) of tsubst_args with an
25323 // additional level for the template template parameter's own
25324 // template parameters (ttparms).
25325 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25326 (DECL_TEMPLATE_PARMS (olddecl)));
25327 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25328 tree ttargs = make_tree_vec (depth + 1);
25329 for (int i = 0; i < depth; ++i)
25330 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25331 TREE_VEC_ELT (ttargs, depth)
25332 = template_parms_level_to_args (ttparms);
25333 // Substitute ttargs into ttparms to fix references to
25334 // other template parameters.
25335 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25336 complain);
25337 // Now substitute again with args based on tparms, to reduce
25338 // the level of the ttparms.
25339 ttargs = current_template_args ();
25340 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25341 complain);
25342 // Finally, tack the adjusted parms onto tparms.
25343 ttparms = tree_cons (size_int (depth), ttparms,
25344 current_template_parms);
25345 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25348 else
25350 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25351 tree newconst
25352 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25353 TREE_CODE (oldconst),
25354 DECL_NAME (oldconst), newtype);
25355 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25356 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25357 SET_DECL_TEMPLATE_PARM_P (newconst);
25358 newidx = build_template_parm_index (index, level, level,
25359 newconst, newtype);
25360 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25361 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25362 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25365 return newdecl;
25368 /* Returns a C++17 class deduction guide template based on the constructor
25369 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25370 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25372 static tree
25373 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25375 tree type, tparms, targs, fparms, fargs, ci;
25376 bool memtmpl = false;
25377 bool explicit_p;
25378 location_t loc;
25379 tree fn_tmpl = NULL_TREE;
25381 if (TYPE_P (ctor))
25383 type = ctor;
25384 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25385 if (copy_p)
25387 type = TREE_TYPE (type);
25388 fparms = tree_cons (NULL_TREE, type, void_list_node);
25390 else
25391 fparms = void_list_node;
25393 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25394 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25395 targs = CLASSTYPE_TI_ARGS (type);
25396 ci = NULL_TREE;
25397 fargs = NULL_TREE;
25398 loc = DECL_SOURCE_LOCATION (ctmpl);
25399 explicit_p = false;
25401 else
25403 ++processing_template_decl;
25405 fn_tmpl
25406 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25407 : DECL_TI_TEMPLATE (ctor));
25408 if (outer_args)
25409 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25410 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25412 type = DECL_CONTEXT (ctor);
25414 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25415 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25416 fully specialized args for the enclosing class. Strip those off, as
25417 the deduction guide won't have those template parameters. */
25418 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25419 TMPL_PARMS_DEPTH (tparms));
25420 /* Discard the 'this' parameter. */
25421 fparms = FUNCTION_ARG_CHAIN (ctor);
25422 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25423 ci = get_constraints (ctor);
25424 loc = DECL_SOURCE_LOCATION (ctor);
25425 explicit_p = DECL_NONCONVERTING_P (ctor);
25427 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25429 memtmpl = true;
25431 /* For a member template constructor, we need to flatten the two
25432 template parameter lists into one, and then adjust the function
25433 signature accordingly. This gets...complicated. */
25434 tree save_parms = current_template_parms;
25436 /* For a member template we should have two levels of parms/args, one
25437 for the class and one for the constructor. We stripped
25438 specialized args for further enclosing classes above. */
25439 const int depth = 2;
25440 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25442 /* Template args for translating references to the two-level template
25443 parameters into references to the one-level template parameters we
25444 are creating. */
25445 tree tsubst_args = copy_node (targs);
25446 TMPL_ARGS_LEVEL (tsubst_args, depth)
25447 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25449 /* Template parms for the constructor template. */
25450 tree ftparms = TREE_VALUE (tparms);
25451 unsigned flen = TREE_VEC_LENGTH (ftparms);
25452 /* Template parms for the class template. */
25453 tparms = TREE_CHAIN (tparms);
25454 tree ctparms = TREE_VALUE (tparms);
25455 unsigned clen = TREE_VEC_LENGTH (ctparms);
25456 /* Template parms for the deduction guide start as a copy of the
25457 template parms for the class. We set current_template_parms for
25458 lookup_template_class_1. */
25459 current_template_parms = tparms = copy_node (tparms);
25460 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25461 for (unsigned i = 0; i < clen; ++i)
25462 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25464 /* Now we need to rewrite the constructor parms to append them to the
25465 class parms. */
25466 for (unsigned i = 0; i < flen; ++i)
25468 unsigned index = i + clen;
25469 unsigned level = 1;
25470 tree oldelt = TREE_VEC_ELT (ftparms, i);
25471 tree olddecl = TREE_VALUE (oldelt);
25472 tree newdecl = rewrite_template_parm (olddecl, index, level,
25473 tsubst_args, complain);
25474 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25475 tsubst_args, complain, ctor);
25476 tree list = build_tree_list (newdef, newdecl);
25477 TEMPLATE_PARM_CONSTRAINTS (list)
25478 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25479 tsubst_args, complain, ctor);
25480 TREE_VEC_ELT (new_vec, index) = list;
25481 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25484 /* Now we have a final set of template parms to substitute into the
25485 function signature. */
25486 targs = template_parms_to_args (tparms);
25487 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25488 complain, ctor);
25489 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25490 if (ci)
25491 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25493 current_template_parms = save_parms;
25495 --processing_template_decl;
25498 if (!memtmpl)
25500 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25501 tparms = copy_node (tparms);
25502 INNERMOST_TEMPLATE_PARMS (tparms)
25503 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25506 tree fntype = build_function_type (type, fparms);
25507 tree ded_fn = build_lang_decl_loc (loc,
25508 FUNCTION_DECL,
25509 dguide_name (type), fntype);
25510 DECL_ARGUMENTS (ded_fn) = fargs;
25511 DECL_ARTIFICIAL (ded_fn) = true;
25512 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25513 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25514 DECL_ARTIFICIAL (ded_tmpl) = true;
25515 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25516 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25517 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25518 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25519 if (DECL_P (ctor))
25520 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25521 if (ci)
25522 set_constraints (ded_tmpl, ci);
25524 return ded_tmpl;
25527 /* Deduce template arguments for the class template placeholder PTYPE for
25528 template TMPL based on the initializer INIT, and return the resulting
25529 type. */
25531 static tree
25532 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25533 tsubst_flags_t complain)
25535 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25537 /* We should have handled this in the caller. */
25538 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25539 return ptype;
25540 if (complain & tf_error)
25541 error ("non-class template %qT used without template arguments", tmpl);
25542 return error_mark_node;
25545 tree type = TREE_TYPE (tmpl);
25547 bool try_list_ctor = false;
25549 vec<tree,va_gc> *args;
25550 if (init == NULL_TREE
25551 || TREE_CODE (init) == TREE_LIST)
25552 args = make_tree_vector_from_list (init);
25553 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25555 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25556 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25558 /* As an exception, the first phase in 16.3.1.7 (considering the
25559 initializer list as a single argument) is omitted if the
25560 initializer list consists of a single expression of type cv U,
25561 where U is a specialization of C or a class derived from a
25562 specialization of C. */
25563 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25564 tree etype = TREE_TYPE (elt);
25566 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25567 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25568 int err = unify (tparms, targs, type, etype,
25569 UNIFY_ALLOW_DERIVED, /*explain*/false);
25570 if (err == 0)
25571 try_list_ctor = false;
25572 ggc_free (targs);
25574 if (try_list_ctor || is_std_init_list (type))
25575 args = make_tree_vector_single (init);
25576 else
25577 args = make_tree_vector_from_ctor (init);
25579 else
25580 args = make_tree_vector_single (init);
25582 tree dname = dguide_name (tmpl);
25583 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25584 /*type*/false, /*complain*/false,
25585 /*hidden*/false);
25586 bool elided = false;
25587 if (cands == error_mark_node)
25588 cands = NULL_TREE;
25590 /* Prune explicit deduction guides in copy-initialization context. */
25591 if (flags & LOOKUP_ONLYCONVERTING)
25593 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25594 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25595 elided = true;
25597 if (elided)
25599 /* Found a nonconverting guide, prune the candidates. */
25600 tree pruned = NULL_TREE;
25601 for (lkp_iterator iter (cands); iter; ++iter)
25602 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25603 pruned = lookup_add (*iter, pruned);
25605 cands = pruned;
25609 tree outer_args = NULL_TREE;
25610 if (DECL_CLASS_SCOPE_P (tmpl)
25611 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25613 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25614 type = TREE_TYPE (most_general_template (tmpl));
25617 bool saw_ctor = false;
25618 if (CLASSTYPE_METHOD_VEC (type))
25619 // FIXME cache artificial deduction guides
25620 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25621 iter; ++iter)
25623 tree guide = build_deduction_guide (*iter, outer_args, complain);
25624 if ((flags & LOOKUP_ONLYCONVERTING)
25625 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25626 elided = true;
25627 else
25628 cands = lookup_add (guide, cands);
25630 saw_ctor = true;
25633 tree call = error_mark_node;
25635 /* If this is list-initialization and the class has a list constructor, first
25636 try deducing from the list as a single argument, as [over.match.list]. */
25637 tree list_cands = NULL_TREE;
25638 if (try_list_ctor && cands)
25639 for (lkp_iterator iter (cands); iter; ++iter)
25641 tree dg = *iter;
25642 if (is_list_ctor (dg))
25643 list_cands = lookup_add (dg, list_cands);
25645 if (list_cands)
25647 ++cp_unevaluated_operand;
25648 call = build_new_function_call (list_cands, &args, tf_decltype);
25649 --cp_unevaluated_operand;
25651 if (call == error_mark_node)
25653 /* That didn't work, now try treating the list as a sequence of
25654 arguments. */
25655 release_tree_vector (args);
25656 args = make_tree_vector_from_ctor (init);
25660 /* Maybe generate an implicit deduction guide. */
25661 if (call == error_mark_node && args->length () < 2)
25663 tree gtype = NULL_TREE;
25665 if (args->length () == 1)
25666 /* Generate a copy guide. */
25667 gtype = build_reference_type (type);
25668 else if (!saw_ctor)
25669 /* Generate a default guide. */
25670 gtype = type;
25672 if (gtype)
25674 tree guide = build_deduction_guide (gtype, outer_args, complain);
25675 cands = lookup_add (guide, cands);
25679 if (elided && !cands)
25681 error ("cannot deduce template arguments for copy-initialization"
25682 " of %qT, as it has no non-explicit deduction guides or "
25683 "user-declared constructors", type);
25684 return error_mark_node;
25686 else if (!cands && call == error_mark_node)
25688 error ("cannot deduce template arguments of %qT, as it has no viable "
25689 "deduction guides", type);
25690 return error_mark_node;
25693 if (call == error_mark_node)
25695 ++cp_unevaluated_operand;
25696 call = build_new_function_call (cands, &args, tf_decltype);
25697 --cp_unevaluated_operand;
25700 if (call == error_mark_node && (complain & tf_warning_or_error))
25702 error ("class template argument deduction failed:");
25704 ++cp_unevaluated_operand;
25705 call = build_new_function_call (cands, &args, complain | tf_decltype);
25706 --cp_unevaluated_operand;
25708 if (elided)
25709 inform (input_location, "explicit deduction guides not considered "
25710 "for copy-initialization");
25713 release_tree_vector (args);
25715 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
25718 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25719 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25721 tree
25722 do_auto_deduction (tree type, tree init, tree auto_node)
25724 return do_auto_deduction (type, init, auto_node,
25725 tf_warning_or_error,
25726 adc_unspecified);
25729 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25730 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25731 The CONTEXT determines the context in which auto deduction is performed
25732 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25733 OUTER_TARGS are used during template argument deduction
25734 (context == adc_unify) to properly substitute the result, and is ignored
25735 in other contexts.
25737 For partial-concept-ids, extra args may be appended to the list of deduced
25738 template arguments prior to determining constraint satisfaction. */
25740 tree
25741 do_auto_deduction (tree type, tree init, tree auto_node,
25742 tsubst_flags_t complain, auto_deduction_context context,
25743 tree outer_targs, int flags)
25745 tree targs;
25747 if (init == error_mark_node)
25748 return error_mark_node;
25750 if (init && type_dependent_expression_p (init)
25751 && context != adc_unify)
25752 /* Defining a subset of type-dependent expressions that we can deduce
25753 from ahead of time isn't worth the trouble. */
25754 return type;
25756 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25757 /* C++17 class template argument deduction. */
25758 return do_class_deduction (type, tmpl, init, flags, complain);
25760 if (TREE_TYPE (init) == NULL_TREE)
25761 /* Nothing we can do with this, even in deduction context. */
25762 return type;
25764 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25765 with either a new invented type template parameter U or, if the
25766 initializer is a braced-init-list (8.5.4), with
25767 std::initializer_list<U>. */
25768 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25770 if (!DIRECT_LIST_INIT_P (init))
25771 type = listify_autos (type, auto_node);
25772 else if (CONSTRUCTOR_NELTS (init) == 1)
25773 init = CONSTRUCTOR_ELT (init, 0)->value;
25774 else
25776 if (complain & tf_warning_or_error)
25778 if (permerror (input_location, "direct-list-initialization of "
25779 "%<auto%> requires exactly one element"))
25780 inform (input_location,
25781 "for deduction to %<std::initializer_list%>, use copy-"
25782 "list-initialization (i.e. add %<=%> before the %<{%>)");
25784 type = listify_autos (type, auto_node);
25788 if (type == error_mark_node)
25789 return error_mark_node;
25791 init = resolve_nondeduced_context (init, complain);
25793 if (context == adc_decomp_type
25794 && auto_node == type
25795 && init != error_mark_node
25796 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25797 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25798 and initializer has array type, deduce cv-qualified array type. */
25799 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25800 complain);
25801 else if (AUTO_IS_DECLTYPE (auto_node))
25803 bool id = (DECL_P (init)
25804 || ((TREE_CODE (init) == COMPONENT_REF
25805 || TREE_CODE (init) == SCOPE_REF)
25806 && !REF_PARENTHESIZED_P (init)));
25807 targs = make_tree_vec (1);
25808 TREE_VEC_ELT (targs, 0)
25809 = finish_decltype_type (init, id, tf_warning_or_error);
25810 if (type != auto_node)
25812 if (complain & tf_error)
25813 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25814 return error_mark_node;
25817 else
25819 tree parms = build_tree_list (NULL_TREE, type);
25820 tree tparms;
25822 if (flag_concepts)
25823 tparms = extract_autos (type);
25824 else
25826 tparms = make_tree_vec (1);
25827 TREE_VEC_ELT (tparms, 0)
25828 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25831 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25832 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25833 DEDUCE_CALL, LOOKUP_NORMAL,
25834 NULL, /*explain_p=*/false);
25835 if (val > 0)
25837 if (processing_template_decl)
25838 /* Try again at instantiation time. */
25839 return type;
25840 if (type && type != error_mark_node
25841 && (complain & tf_error))
25842 /* If type is error_mark_node a diagnostic must have been
25843 emitted by now. Also, having a mention to '<type error>'
25844 in the diagnostic is not really useful to the user. */
25846 if (cfun && auto_node == current_function_auto_return_pattern
25847 && LAMBDA_FUNCTION_P (current_function_decl))
25848 error ("unable to deduce lambda return type from %qE", init);
25849 else
25850 error ("unable to deduce %qT from %qE", type, init);
25851 type_unification_real (tparms, targs, parms, &init, 1, 0,
25852 DEDUCE_CALL, LOOKUP_NORMAL,
25853 NULL, /*explain_p=*/true);
25855 return error_mark_node;
25859 /* Check any placeholder constraints against the deduced type. */
25860 if (flag_concepts && !processing_template_decl)
25861 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25863 /* Use the deduced type to check the associated constraints. If we
25864 have a partial-concept-id, rebuild the argument list so that
25865 we check using the extra arguments. */
25866 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25867 tree cargs = CHECK_CONSTR_ARGS (constr);
25868 if (TREE_VEC_LENGTH (cargs) > 1)
25870 cargs = copy_node (cargs);
25871 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25873 else
25874 cargs = targs;
25875 if (!constraints_satisfied_p (constr, cargs))
25877 if (complain & tf_warning_or_error)
25879 switch (context)
25881 case adc_unspecified:
25882 case adc_unify:
25883 error("placeholder constraints not satisfied");
25884 break;
25885 case adc_variable_type:
25886 case adc_decomp_type:
25887 error ("deduced initializer does not satisfy "
25888 "placeholder constraints");
25889 break;
25890 case adc_return_type:
25891 error ("deduced return type does not satisfy "
25892 "placeholder constraints");
25893 break;
25894 case adc_requirement:
25895 error ("deduced expression type does not satisfy "
25896 "placeholder constraints");
25897 break;
25899 diagnose_constraints (input_location, constr, targs);
25901 return error_mark_node;
25905 if (processing_template_decl && context != adc_unify)
25906 outer_targs = current_template_args ();
25907 targs = add_to_template_args (outer_targs, targs);
25908 return tsubst (type, targs, complain, NULL_TREE);
25911 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25912 result. */
25914 tree
25915 splice_late_return_type (tree type, tree late_return_type)
25917 if (is_auto (type))
25919 if (late_return_type)
25920 return late_return_type;
25922 tree idx = get_template_parm_index (type);
25923 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25924 /* In an abbreviated function template we didn't know we were dealing
25925 with a function template when we saw the auto return type, so update
25926 it to have the correct level. */
25927 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25929 return type;
25932 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25933 'decltype(auto)' or a deduced class template. */
25935 bool
25936 is_auto (const_tree type)
25938 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25939 && (TYPE_IDENTIFIER (type) == auto_identifier
25940 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25941 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25942 return true;
25943 else
25944 return false;
25947 /* for_each_template_parm callback for type_uses_auto. */
25950 is_auto_r (tree tp, void */*data*/)
25952 return is_auto (tp);
25955 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25956 a use of `auto'. Returns NULL_TREE otherwise. */
25958 tree
25959 type_uses_auto (tree type)
25961 if (type == NULL_TREE)
25962 return NULL_TREE;
25963 else if (flag_concepts)
25965 /* The Concepts TS allows multiple autos in one type-specifier; just
25966 return the first one we find, do_auto_deduction will collect all of
25967 them. */
25968 if (uses_template_parms (type))
25969 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25970 /*visited*/NULL, /*nondeduced*/true);
25971 else
25972 return NULL_TREE;
25974 else
25975 return find_type_usage (type, is_auto);
25978 /* For a given template T, return the vector of typedefs referenced
25979 in T for which access check is needed at T instantiation time.
25980 T is either a FUNCTION_DECL or a RECORD_TYPE.
25981 Those typedefs were added to T by the function
25982 append_type_to_template_for_access_check. */
25984 vec<qualified_typedef_usage_t, va_gc> *
25985 get_types_needing_access_check (tree t)
25987 tree ti;
25988 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25990 if (!t || t == error_mark_node)
25991 return NULL;
25993 if (!(ti = get_template_info (t)))
25994 return NULL;
25996 if (CLASS_TYPE_P (t)
25997 || TREE_CODE (t) == FUNCTION_DECL)
25999 if (!TI_TEMPLATE (ti))
26000 return NULL;
26002 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26005 return result;
26008 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26009 tied to T. That list of typedefs will be access checked at
26010 T instantiation time.
26011 T is either a FUNCTION_DECL or a RECORD_TYPE.
26012 TYPE_DECL is a TYPE_DECL node representing a typedef.
26013 SCOPE is the scope through which TYPE_DECL is accessed.
26014 LOCATION is the location of the usage point of TYPE_DECL.
26016 This function is a subroutine of
26017 append_type_to_template_for_access_check. */
26019 static void
26020 append_type_to_template_for_access_check_1 (tree t,
26021 tree type_decl,
26022 tree scope,
26023 location_t location)
26025 qualified_typedef_usage_t typedef_usage;
26026 tree ti;
26028 if (!t || t == error_mark_node)
26029 return;
26031 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26032 || CLASS_TYPE_P (t))
26033 && type_decl
26034 && TREE_CODE (type_decl) == TYPE_DECL
26035 && scope);
26037 if (!(ti = get_template_info (t)))
26038 return;
26040 gcc_assert (TI_TEMPLATE (ti));
26042 typedef_usage.typedef_decl = type_decl;
26043 typedef_usage.context = scope;
26044 typedef_usage.locus = location;
26046 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26049 /* Append TYPE_DECL to the template TEMPL.
26050 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26051 At TEMPL instanciation time, TYPE_DECL will be checked to see
26052 if it can be accessed through SCOPE.
26053 LOCATION is the location of the usage point of TYPE_DECL.
26055 e.g. consider the following code snippet:
26057 class C
26059 typedef int myint;
26062 template<class U> struct S
26064 C::myint mi; // <-- usage point of the typedef C::myint
26067 S<char> s;
26069 At S<char> instantiation time, we need to check the access of C::myint
26070 In other words, we need to check the access of the myint typedef through
26071 the C scope. For that purpose, this function will add the myint typedef
26072 and the scope C through which its being accessed to a list of typedefs
26073 tied to the template S. That list will be walked at template instantiation
26074 time and access check performed on each typedefs it contains.
26075 Note that this particular code snippet should yield an error because
26076 myint is private to C. */
26078 void
26079 append_type_to_template_for_access_check (tree templ,
26080 tree type_decl,
26081 tree scope,
26082 location_t location)
26084 qualified_typedef_usage_t *iter;
26085 unsigned i;
26087 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26089 /* Make sure we don't append the type to the template twice. */
26090 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26091 if (iter->typedef_decl == type_decl && scope == iter->context)
26092 return;
26094 append_type_to_template_for_access_check_1 (templ, type_decl,
26095 scope, location);
26098 /* Convert the generic type parameters in PARM that match the types given in the
26099 range [START_IDX, END_IDX) from the current_template_parms into generic type
26100 packs. */
26102 tree
26103 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26105 tree current = current_template_parms;
26106 int depth = TMPL_PARMS_DEPTH (current);
26107 current = INNERMOST_TEMPLATE_PARMS (current);
26108 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26110 for (int i = 0; i < start_idx; ++i)
26111 TREE_VEC_ELT (replacement, i)
26112 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26114 for (int i = start_idx; i < end_idx; ++i)
26116 /* Create a distinct parameter pack type from the current parm and add it
26117 to the replacement args to tsubst below into the generic function
26118 parameter. */
26120 tree o = TREE_TYPE (TREE_VALUE
26121 (TREE_VEC_ELT (current, i)));
26122 tree t = copy_type (o);
26123 TEMPLATE_TYPE_PARM_INDEX (t)
26124 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26125 o, 0, 0, tf_none);
26126 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26127 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26128 TYPE_MAIN_VARIANT (t) = t;
26129 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26130 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26131 TREE_VEC_ELT (replacement, i) = t;
26132 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26135 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26136 TREE_VEC_ELT (replacement, i)
26137 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26139 /* If there are more levels then build up the replacement with the outer
26140 template parms. */
26141 if (depth > 1)
26142 replacement = add_to_template_args (template_parms_to_args
26143 (TREE_CHAIN (current_template_parms)),
26144 replacement);
26146 return tsubst (parm, replacement, tf_none, NULL_TREE);
26149 /* Entries in the decl_constraint hash table. */
26150 struct GTY((for_user)) constr_entry
26152 tree decl;
26153 tree ci;
26156 /* Hashing function and equality for constraint entries. */
26157 struct constr_hasher : ggc_ptr_hash<constr_entry>
26159 static hashval_t hash (constr_entry *e)
26161 return (hashval_t)DECL_UID (e->decl);
26164 static bool equal (constr_entry *e1, constr_entry *e2)
26166 return e1->decl == e2->decl;
26170 /* A mapping from declarations to constraint information. Note that
26171 both templates and their underlying declarations are mapped to the
26172 same constraint information.
26174 FIXME: This is defined in pt.c because garbage collection
26175 code is not being generated for constraint.cc. */
26177 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26179 /* Returns the template constraints of declaration T. If T is not
26180 constrained, return NULL_TREE. Note that T must be non-null. */
26182 tree
26183 get_constraints (tree t)
26185 if (!flag_concepts)
26186 return NULL_TREE;
26188 gcc_assert (DECL_P (t));
26189 if (TREE_CODE (t) == TEMPLATE_DECL)
26190 t = DECL_TEMPLATE_RESULT (t);
26191 constr_entry elt = { t, NULL_TREE };
26192 constr_entry* found = decl_constraints->find (&elt);
26193 if (found)
26194 return found->ci;
26195 else
26196 return NULL_TREE;
26199 /* Associate the given constraint information CI with the declaration
26200 T. If T is a template, then the constraints are associated with
26201 its underlying declaration. Don't build associations if CI is
26202 NULL_TREE. */
26204 void
26205 set_constraints (tree t, tree ci)
26207 if (!ci)
26208 return;
26209 gcc_assert (t && flag_concepts);
26210 if (TREE_CODE (t) == TEMPLATE_DECL)
26211 t = DECL_TEMPLATE_RESULT (t);
26212 gcc_assert (!get_constraints (t));
26213 constr_entry elt = {t, ci};
26214 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26215 constr_entry* entry = ggc_alloc<constr_entry> ();
26216 *entry = elt;
26217 *slot = entry;
26220 /* Remove the associated constraints of the declaration T. */
26222 void
26223 remove_constraints (tree t)
26225 gcc_assert (DECL_P (t));
26226 if (TREE_CODE (t) == TEMPLATE_DECL)
26227 t = DECL_TEMPLATE_RESULT (t);
26229 constr_entry elt = {t, NULL_TREE};
26230 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26231 if (slot)
26232 decl_constraints->clear_slot (slot);
26235 /* Memoized satisfaction results for declarations. This
26236 maps the pair (constraint_info, arguments) to the result computed
26237 by constraints_satisfied_p. */
26239 struct GTY((for_user)) constraint_sat_entry
26241 tree ci;
26242 tree args;
26243 tree result;
26246 /* Hashing function and equality for constraint entries. */
26248 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26250 static hashval_t hash (constraint_sat_entry *e)
26252 hashval_t val = iterative_hash_object(e->ci, 0);
26253 return iterative_hash_template_arg (e->args, val);
26256 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26258 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26262 /* Memoized satisfaction results for concept checks. */
26264 struct GTY((for_user)) concept_spec_entry
26266 tree tmpl;
26267 tree args;
26268 tree result;
26271 /* Hashing function and equality for constraint entries. */
26273 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26275 static hashval_t hash (concept_spec_entry *e)
26277 return hash_tmpl_and_args (e->tmpl, e->args);
26280 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26282 ++comparing_specializations;
26283 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26284 --comparing_specializations;
26285 return eq;
26289 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26290 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26292 /* Search for a memoized satisfaction result. Returns one of the
26293 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26295 tree
26296 lookup_constraint_satisfaction (tree ci, tree args)
26298 constraint_sat_entry elt = { ci, args, NULL_TREE };
26299 constraint_sat_entry* found = constraint_memos->find (&elt);
26300 if (found)
26301 return found->result;
26302 else
26303 return NULL_TREE;
26306 /* Memoize the result of a satisfication test. Returns the saved result. */
26308 tree
26309 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26311 constraint_sat_entry elt = {ci, args, result};
26312 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26313 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26314 *entry = elt;
26315 *slot = entry;
26316 return result;
26319 /* Search for a memoized satisfaction result for a concept. */
26321 tree
26322 lookup_concept_satisfaction (tree tmpl, tree args)
26324 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26325 concept_spec_entry* found = concept_memos->find (&elt);
26326 if (found)
26327 return found->result;
26328 else
26329 return NULL_TREE;
26332 /* Memoize the result of a concept check. Returns the saved result. */
26334 tree
26335 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26337 concept_spec_entry elt = {tmpl, args, result};
26338 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26339 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26340 *entry = elt;
26341 *slot = entry;
26342 return result;
26345 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26347 /* Returns a prior concept specialization. This returns the substituted
26348 and normalized constraints defined by the concept. */
26350 tree
26351 get_concept_expansion (tree tmpl, tree args)
26353 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26354 concept_spec_entry* found = concept_expansions->find (&elt);
26355 if (found)
26356 return found->result;
26357 else
26358 return NULL_TREE;
26361 /* Save a concept expansion for later. */
26363 tree
26364 save_concept_expansion (tree tmpl, tree args, tree def)
26366 concept_spec_entry elt = {tmpl, args, def};
26367 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26368 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26369 *entry = elt;
26370 *slot = entry;
26371 return def;
26374 static hashval_t
26375 hash_subsumption_args (tree t1, tree t2)
26377 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26378 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26379 int val = 0;
26380 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26381 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26382 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26383 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26384 return val;
26387 /* Compare the constraints of two subsumption entries. The LEFT1 and
26388 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26389 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26391 static bool
26392 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26394 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26395 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26396 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26397 CHECK_CONSTR_ARGS (right1)))
26398 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26399 CHECK_CONSTR_ARGS (right2));
26400 return false;
26403 /* Key/value pair for learning and memoizing subsumption results. This
26404 associates a pair of check constraints (including arguments) with
26405 a boolean value indicating the result. */
26407 struct GTY((for_user)) subsumption_entry
26409 tree t1;
26410 tree t2;
26411 bool result;
26414 /* Hashing function and equality for constraint entries. */
26416 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26418 static hashval_t hash (subsumption_entry *e)
26420 return hash_subsumption_args (e->t1, e->t2);
26423 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26425 ++comparing_specializations;
26426 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26427 --comparing_specializations;
26428 return eq;
26432 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26434 /* Search for a previously cached subsumption result. */
26436 bool*
26437 lookup_subsumption_result (tree t1, tree t2)
26439 subsumption_entry elt = { t1, t2, false };
26440 subsumption_entry* found = subsumption_table->find (&elt);
26441 if (found)
26442 return &found->result;
26443 else
26444 return 0;
26447 /* Save a subsumption result. */
26449 bool
26450 save_subsumption_result (tree t1, tree t2, bool result)
26452 subsumption_entry elt = {t1, t2, result};
26453 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26454 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26455 *entry = elt;
26456 *slot = entry;
26457 return result;
26460 /* Set up the hash table for constraint association. */
26462 void
26463 init_constraint_processing (void)
26465 if (!flag_concepts)
26466 return;
26468 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26469 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26470 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26471 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26472 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26475 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26476 0..N-1. */
26478 void
26479 declare_integer_pack (void)
26481 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26482 build_function_type_list (integer_type_node,
26483 integer_type_node,
26484 NULL_TREE),
26485 NULL_TREE, ECF_CONST);
26486 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26487 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26490 /* Set up the hash tables for template instantiations. */
26492 void
26493 init_template_processing (void)
26495 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26496 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26498 if (cxx_dialect >= cxx11)
26499 declare_integer_pack ();
26502 /* Print stats about the template hash tables for -fstats. */
26504 void
26505 print_template_statistics (void)
26507 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26508 "%f collisions\n", (long) decl_specializations->size (),
26509 (long) decl_specializations->elements (),
26510 decl_specializations->collisions ());
26511 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26512 "%f collisions\n", (long) type_specializations->size (),
26513 (long) type_specializations->elements (),
26514 type_specializations->collisions ());
26517 #include "gt-cp-pt.h"