/* Handle parameterized types (templates) for GNU -*- C++ -*-.
Copyright (C) 1992-2020 Free Software Foundation, Inc.
Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
Rewritten by Jason Merrill (jason@cygnus.com).
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
/* Known bugs or deficiencies include:
all methods must be provided in header files; can't use a source
file that contains only the method templates and "just win". */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "cp-tree.h"
#include "timevar.h"
#include "stringpool.h"
#include "varasm.h"
#include "attribs.h"
#include "stor-layout.h"
#include "intl.h"
#include "c-family/c-objc.h"
#include "cp-objcp-common.h"
#include "toplev.h"
#include "tree-iterator.h"
#include "type-utils.h"
#include "gimplify.h"
#include "gcc-rich-location.h"
#include "selftest.h"
#include "target.h"
/* The type of functions taking a tree, and some additional data, and
returning an int. */
typedef int (*tree_fn_t) (tree, void*);
/* The PENDING_TEMPLATES is a TREE_LIST of templates whose
instantiations have been deferred, either because their definitions
were not yet available, or because we were putting off doing the work. */
struct GTY ((chain_next ("%h.next"))) pending_template
{
struct pending_template *next;
struct tinst_level *tinst;
};
static GTY(()) struct pending_template *pending_templates;
static GTY(()) struct pending_template *last_pending_template;
int processing_template_parmlist;
static int template_header_count;
static vec<int> inline_parm_levels;
static GTY(()) struct tinst_level *current_tinst_level;
static GTY(()) vec<tree, va_gc> *saved_access_scope;
/* Live only within one (recursive) call to tsubst_expr. We use
this to pass the statement expression node from the STMT_EXPR
to the EXPR_STMT that is its result. */
static tree cur_stmt_expr;
// -------------------------------------------------------------------------- //
// Local Specialization Stack
//
// Implementation of the RAII helper for creating new local
// specializations.
local_specialization_stack::local_specialization_stack (lss_policy policy)
: saved (local_specializations)
{
if (policy == lss_nop)
;
else if (policy == lss_blank || !saved)
local_specializations = new hash_map<tree, tree>;
else
local_specializations = new hash_map<tree, tree>(*saved);
}
local_specialization_stack::~local_specialization_stack ()
{
if (local_specializations != saved)
{
delete local_specializations;
local_specializations = saved;
}
}
/* True if we've recursed into fn_type_unification too many times. */
static bool excessive_deduction_depth;
struct GTY((for_user)) spec_entry
{
tree tmpl;
tree args;
tree spec;
};
struct spec_hasher : ggc_ptr_hash<spec_entry>
{
static hashval_t hash (spec_entry *);
static bool equal (spec_entry *, spec_entry *);
};
static GTY (()) hash_table<spec_hasher> *decl_specializations;
static GTY (()) hash_table<spec_hasher> *type_specializations;
/* Contains canonical template parameter types. The vector is indexed by
the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
TREE_LIST, whose TREE_VALUEs contain the canonical template
parameters of various types and levels. */
static GTY(()) vec<tree, va_gc> *canonical_template_parms;
#define UNIFY_ALLOW_NONE 0
#define UNIFY_ALLOW_MORE_CV_QUAL 1
#define UNIFY_ALLOW_LESS_CV_QUAL 2
#define UNIFY_ALLOW_DERIVED 4
#define UNIFY_ALLOW_INTEGER 8
#define UNIFY_ALLOW_OUTER_LEVEL 16
#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
enum template_base_result {
tbr_incomplete_type,
tbr_ambiguous_baseclass,
tbr_success
};
static bool resolve_overloaded_unification (tree, tree, tree, tree,
unification_kind_t, int,
bool);
static int try_one_overload (tree, tree, tree, tree, tree,
unification_kind_t, int, bool, bool);
static int unify (tree, tree, tree, tree, int, bool);
static void add_pending_template (tree);
static tree reopen_tinst_level (struct tinst_level *);
static tree tsubst_initializer_list (tree, tree);
static tree get_partial_spec_bindings (tree, tree, tree);
static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
bool, bool);
static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
bool, bool);
static void tsubst_enum (tree, tree, tree);
static tree add_to_template_args (tree, tree);
static bool check_instantiated_args (tree, tree, tsubst_flags_t);
static int check_non_deducible_conversion (tree, tree, int, int,
struct conversion **, bool);
static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
tree);
static int type_unification_real (tree, tree, tree, const tree *,
unsigned int, int, unification_kind_t,
vec<deferred_access_check, va_gc> **,
bool);
static void note_template_header (int);
static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
static tree convert_template_argument (tree, tree, tree,
tsubst_flags_t, int, tree);
static tree for_each_template_parm (tree, tree_fn_t, void*,
hash_set<tree> *, bool, tree_fn_t = NULL);
static tree expand_template_argument_pack (tree);
static tree build_template_parm_index (int, int, int, tree, tree);
static bool inline_needs_template_parms (tree, bool);
static void push_inline_template_parms_recursive (tree, int);
static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
static int mark_template_parm (tree, void *);
static int template_parm_this_level_p (tree, void *);
static tree tsubst_friend_function (tree, tree);
static tree tsubst_friend_class (tree, tree);
static int can_complete_type_without_circularity (tree);
static tree get_bindings (tree, tree, tree, bool);
static int template_decl_level (tree);
static int check_cv_quals_for_unify (int, tree, tree);
static int unify_pack_expansion (tree, tree, tree,
tree, unification_kind_t, bool, bool);
static tree copy_template_args (tree);
static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
tree most_specialized_partial_spec (tree, tsubst_flags_t);
static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
static bool check_specialization_scope (void);
static tree process_partial_specialization (tree);
static void set_current_access_from_decl (tree);
static enum template_base_result get_template_base (tree, tree, tree, tree,
bool , tree *);
static tree try_class_unification (tree, tree, tree, tree, bool);
static bool class_nttp_const_wrapper_p (tree t);
static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
tree, tree);
static bool template_template_parm_bindings_ok_p (tree, tree);
static void tsubst_default_arguments (tree, tsubst_flags_t);
static tree for_each_template_parm_r (tree *, int *, void *);
static tree copy_default_args_to_explicit_spec_1 (tree, tree);
static void copy_default_args_to_explicit_spec (tree);
static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
static bool dependent_template_arg_p (tree);
static bool any_template_arguments_need_structural_equality_p (tree);
static bool dependent_type_p_r (tree);
static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
static tree tsubst_decl (tree, tree, tsubst_flags_t);
static void perform_typedefs_access_check (tree tmpl, tree targs);
static void append_type_to_template_for_access_check_1 (tree, tree, tree,
location_t);
static tree listify (tree);
static tree listify_autos (tree, tree);
static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
static bool complex_alias_template_p (const_tree tmpl);
static tree get_underlying_template (tree);
static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
static tree canonicalize_expr_argument (tree, tsubst_flags_t);
static tree make_argument_pack (tree);
static void register_parameter_specializations (tree, tree);
static tree enclosing_instantiation_of (tree tctx);
/* Make the current scope suitable for access checking when we are
processing T. T can be FUNCTION_DECL for instantiated function
template, VAR_DECL for static member variable, or TYPE_DECL for
alias template (needed by instantiate_decl). */
void
push_access_scope (tree t)
{
gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
|| TREE_CODE (t) == TYPE_DECL);
if (DECL_FRIEND_CONTEXT (t))
push_nested_class (DECL_FRIEND_CONTEXT (t));
else if (DECL_CLASS_SCOPE_P (t))
push_nested_class (DECL_CONTEXT (t));
else
push_to_top_level ();
if (TREE_CODE (t) == FUNCTION_DECL)
{
vec_safe_push (saved_access_scope, current_function_decl);
current_function_decl = t;
}
}
/* Restore the scope set up by push_access_scope. T is the node we
are processing. */
void
pop_access_scope (tree t)
{
if (TREE_CODE (t) == FUNCTION_DECL)
current_function_decl = saved_access_scope->pop();
if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
pop_nested_class ();
else
pop_from_top_level ();
}
/* Do any processing required when DECL (a member template
declaration) is finished. Returns the TEMPLATE_DECL corresponding
to DECL, unless it is a specialization, in which case the DECL
itself is returned. */
tree
finish_member_template_decl (tree decl)
{
if (decl == error_mark_node)
return error_mark_node;
gcc_assert (DECL_P (decl));
if (TREE_CODE (decl) == TYPE_DECL)
{
tree type;
type = TREE_TYPE (decl);
if (type == error_mark_node)
return error_mark_node;
if (MAYBE_CLASS_TYPE_P (type)
&& CLASSTYPE_TEMPLATE_INFO (type)
&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
{
tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
check_member_template (tmpl);
return tmpl;
}
return NULL_TREE;
}
else if (TREE_CODE (decl) == FIELD_DECL)
error_at (DECL_SOURCE_LOCATION (decl),
"data member %qD cannot be a member template", decl);
else if (DECL_TEMPLATE_INFO (decl))
{
if (!DECL_TEMPLATE_SPECIALIZATION (decl))
{
check_member_template (DECL_TI_TEMPLATE (decl));
return DECL_TI_TEMPLATE (decl);
}
else
return decl;
}
else
error_at (DECL_SOURCE_LOCATION (decl),
"invalid member template declaration %qD", decl);
return error_mark_node;
}
/* Create a template info node. */
tree
build_template_info (tree template_decl, tree template_args)
{
tree result = make_node (TEMPLATE_INFO);
TI_TEMPLATE (result) = template_decl;
TI_ARGS (result) = template_args;
return result;
}
/* Return the template info node corresponding to T, whatever T is. */
tree
get_template_info (const_tree t)
{
tree tinfo = NULL_TREE;
if (!t || t == error_mark_node)
return NULL;
if (TREE_CODE (t) == NAMESPACE_DECL
|| TREE_CODE (t) == PARM_DECL)
return NULL;
if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
tinfo = DECL_TEMPLATE_INFO (t);
if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
t = TREE_TYPE (t);
if (OVERLOAD_TYPE_P (t))
tinfo = TYPE_TEMPLATE_INFO (t);
else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
return tinfo;
}
/* Returns the template nesting level of the indicated class TYPE.
For example, in:
template <class T>
struct A
{
template <class U>
struct B {};
};
A<T>::B<U> has depth two, while A<T> has depth one.
Both A<T>::B<int> and A<int>::B<U> have depth one, if
they are instantiations, not specializations.
This function is guaranteed to return 0 if passed NULL_TREE so
that, for example, `template_class_depth (current_class_type)' is
always safe. */
int
template_class_depth (tree type)
{
int depth;
for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
{
tree tinfo = get_template_info (type);
if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
&& uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
++depth;
if (DECL_P (type))
{
if (tree fctx = DECL_FRIEND_CONTEXT (type))
type = fctx;
else
type = CP_DECL_CONTEXT (type);
}
else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
type = LAMBDA_TYPE_EXTRA_SCOPE (type);
else
type = CP_TYPE_CONTEXT (type);
}
return depth;
}
/* Return TRUE if NODE instantiates a template that has arguments of
its own, be it directly a primary template or indirectly through a
partial specializations. */
static bool
instantiates_primary_template_p (tree node)
{
tree tinfo = get_template_info (node);
if (!tinfo)
return false;
tree tmpl = TI_TEMPLATE (tinfo);
if (PRIMARY_TEMPLATE_P (tmpl))
return true;
if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
return false;
/* So now we know we have a specialization, but it could be a full
or a partial specialization. To tell which, compare the depth of
its template arguments with those of its context. */
tree ctxt = DECL_CONTEXT (tmpl);
tree ctinfo = get_template_info (ctxt);
if (!ctinfo)
return true;
return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
> TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
}
/* Subroutine of maybe_begin_member_template_processing.
Returns true if processing DECL needs us to push template parms. */
static bool
inline_needs_template_parms (tree decl, bool nsdmi)
{
if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
return false;
return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
> (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
}
/* Subroutine of maybe_begin_member_template_processing.
Push the template parms in PARMS, starting from LEVELS steps into the
chain, and ending at the beginning, since template parms are listed
innermost first. */
static void
push_inline_template_parms_recursive (tree parmlist, int levels)
{
tree parms = TREE_VALUE (parmlist);
int i;
if (levels > 1)
push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
++processing_template_decl;
current_template_parms
= tree_cons (size_int (processing_template_decl),
parms, current_template_parms);
TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
NULL);
for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
{
tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
if (error_operand_p (parm))
continue;
gcc_assert (DECL_P (parm));
switch (TREE_CODE (parm))
{
case TYPE_DECL:
case TEMPLATE_DECL:
pushdecl (parm);
break;
case PARM_DECL:
/* Push the CONST_DECL. */
pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
break;
default:
gcc_unreachable ();
}
}
}
/* Restore the template parameter context for a member template, a
friend template defined in a class definition, or a non-template
member of template class. */
void
maybe_begin_member_template_processing (tree decl)
{
tree parms;
int levels = 0;
bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
if (nsdmi)
{
tree ctx = DECL_CONTEXT (decl);
decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
/* Disregard full specializations (c++/60999). */
&& uses_template_parms (ctx)
? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
}
if (inline_needs_template_parms (decl, nsdmi))
{
parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
if (DECL_TEMPLATE_SPECIALIZATION (decl))
{
--levels;
parms = TREE_CHAIN (parms);
}
push_inline_template_parms_recursive (parms, levels);
}
/* Remember how many levels of template parameters we pushed so that
we can pop them later. */
inline_parm_levels.safe_push (levels);
}
/* Undo the effects of maybe_begin_member_template_processing. */
void
maybe_end_member_template_processing (void)
{
int i;
int last;
if (inline_parm_levels.length () == 0)
return;
last = inline_parm_levels.pop ();
for (i = 0; i < last; ++i)
{
--processing_template_decl;
current_template_parms = TREE_CHAIN (current_template_parms);
poplevel (0, 0, 0);
}
}
/* Return a new template argument vector which contains all of ARGS,
but has as its innermost set of arguments the EXTRA_ARGS. */
static tree
add_to_template_args (tree args, tree extra_args)
{
tree new_args;
int extra_depth;
int i;
int j;
if (args == NULL_TREE || extra_args == error_mark_node)
return extra_args;
extra_depth = TMPL_ARGS_DEPTH (extra_args);
new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
for (j = 1; j <= extra_depth; ++j, ++i)
SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
return new_args;
}
/* Like add_to_template_args, but only the outermost ARGS are added to
the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
(EXTRA_ARGS) levels are added. This function is used to combine
the template arguments from a partial instantiation with the
template arguments used to attain the full instantiation from the
partial instantiation. */
tree
add_outermost_template_args (tree args, tree extra_args)
{
tree new_args;
/* If there are more levels of EXTRA_ARGS than there are ARGS,
something very fishy is going on. */
gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
/* If *all* the new arguments will be the EXTRA_ARGS, just return
them. */
if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
return extra_args;
/* For the moment, we make ARGS look like it contains fewer levels. */
TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
new_args = add_to_template_args (args, extra_args);
/* Now, we restore ARGS to its full dimensions. */
TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
return new_args;
}
/* Return the N levels of innermost template arguments from the ARGS. */
tree
get_innermost_template_args (tree args, int n)
{
tree new_args;
int extra_levels;
int i;
gcc_assert (n >= 0);
/* If N is 1, just return the innermost set of template arguments. */
if (n == 1)
return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
/* If we're not removing anything, just return the arguments we were
given. */
extra_levels = TMPL_ARGS_DEPTH (args) - n;
gcc_assert (extra_levels >= 0);
if (extra_levels == 0)
return args;
/* Make a new set of arguments, not containing the outer arguments. */
new_args = make_tree_vec (n);
for (i = 1; i <= n; ++i)
SET_TMPL_ARGS_LEVEL (new_args, i,
TMPL_ARGS_LEVEL (args, i + extra_levels));
return new_args;
}
/* The inverse of get_innermost_template_args: Return all but the innermost
EXTRA_LEVELS levels of template arguments from the ARGS. */
static tree
strip_innermost_template_args (tree args, int extra_levels)
{
tree new_args;
int n = TMPL_ARGS_DEPTH (args) - extra_levels;
int i;
gcc_assert (n >= 0);
/* If N is 1, just return the outermost set of template arguments. */
if (n == 1)
return TMPL_ARGS_LEVEL (args, 1);
/* If we're not removing anything, just return the arguments we were
given. */
gcc_assert (extra_levels >= 0);
if (extra_levels == 0)
return args;
/* Make a new set of arguments, not containing the inner arguments. */
new_args = make_tree_vec (n);
for (i = 1; i <= n; ++i)
SET_TMPL_ARGS_LEVEL (new_args, i,
TMPL_ARGS_LEVEL (args, i));
return new_args;
}
/* We've got a template header coming up; push to a new level for storing
the parms. */
void
begin_template_parm_list (void)
{
/* We use a non-tag-transparent scope here, which causes pushtag to
put tags in this scope, rather than in the enclosing class or
namespace scope. This is the right thing, since we want
TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
global template class, push_template_decl handles putting the
TEMPLATE_DECL into top-level scope. For a nested template class,
e.g.:
template <class T> struct S1 {
template <class T> struct S2 {};
};
pushtag contains special code to insert the TEMPLATE_DECL for S2
at the right scope. */
begin_scope (sk_template_parms, NULL);
++processing_template_decl;
++processing_template_parmlist;
note_template_header (0);
/* Add a dummy parameter level while we process the parameter list. */
current_template_parms
= tree_cons (size_int (processing_template_decl),
make_tree_vec (0),
current_template_parms);
}
/* This routine is called when a specialization is declared. If it is
invalid to declare a specialization here, an error is reported and
false is returned, otherwise this routine will return true. */
static bool
check_specialization_scope (void)
{
tree scope = current_scope ();
/* [temp.expl.spec]
An explicit specialization shall be declared in the namespace of
which the template is a member, or, for member templates, in the
namespace of which the enclosing class or enclosing class
template is a member. An explicit specialization of a member
function, member class or static data member of a class template
shall be declared in the namespace of which the class template
is a member. */
if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
{
error ("explicit specialization in non-namespace scope %qD", scope);
return false;
}
/* [temp.expl.spec]
In an explicit specialization declaration for a member of a class
template or a member template that appears in namespace scope,
the member template and some of its enclosing class templates may
remain unspecialized, except that the declaration shall not
explicitly specialize a class member template if its enclosing
class templates are not explicitly specialized as well. */
if (current_template_parms)
{
error ("enclosing class templates are not explicitly specialized");
return false;
}
return true;
}
/* We've just seen template <>. */
bool
begin_specialization (void)
{
begin_scope (sk_template_spec, NULL);
note_template_header (1);
return check_specialization_scope ();
}
/* Called at then end of processing a declaration preceded by
template<>. */
void
end_specialization (void)
{
finish_scope ();
reset_specialization ();
}
/* Any template <>'s that we have seen thus far are not referring to a
function specialization. */
void
reset_specialization (void)
{
processing_specialization = 0;
template_header_count = 0;
}
/* We've just seen a template header. If SPECIALIZATION is nonzero,
it was of the form template <>. */
static void
note_template_header (int specialization)
{
processing_specialization = specialization;
template_header_count++;
}
/* We're beginning an explicit instantiation. */
void
begin_explicit_instantiation (void)
{
gcc_assert (!processing_explicit_instantiation);
processing_explicit_instantiation = true;
}
void
end_explicit_instantiation (void)
{
gcc_assert (processing_explicit_instantiation);
processing_explicit_instantiation = false;
}
/* An explicit specialization or partial specialization of TMPL is being
declared. Check that the namespace in which the specialization is
occurring is permissible. Returns false iff it is invalid to
specialize TMPL in the current namespace. */
static bool
check_specialization_namespace (tree tmpl)
{
tree tpl_ns = decl_namespace_context (tmpl);
/* [tmpl.expl.spec]
An explicit specialization shall be declared in a namespace enclosing the
specialized template. An explicit specialization whose declarator-id is
not qualified shall be declared in the nearest enclosing namespace of the
template, or, if the namespace is inline (7.3.1), any namespace from its
enclosing namespace set. */
if (current_scope() != DECL_CONTEXT (tmpl)
&& !at_namespace_scope_p ())
{
error ("specialization of %qD must appear at namespace scope", tmpl);
return false;
}
if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
/* Same or enclosing namespace. */
return true;
else
{
auto_diagnostic_group d;
if (permerror (input_location,
"specialization of %qD in different namespace", tmpl))
inform (DECL_SOURCE_LOCATION (tmpl),
" from definition of %q#D", tmpl);
return false;
}
}
/* SPEC is an explicit instantiation. Check that it is valid to
perform this explicit instantiation in the current namespace. */
static void
check_explicit_instantiation_namespace (tree spec)
{
tree ns;
/* DR 275: An explicit instantiation shall appear in an enclosing
namespace of its template. */
ns = decl_namespace_context (spec);
if (!is_nested_namespace (current_namespace, ns))
permerror (input_location, "explicit instantiation of %qD in namespace %qD "
"(which does not enclose namespace %qD)",
spec, current_namespace, ns);
}
/* Returns the type of a template specialization only if that
specialization needs to be defined. Otherwise (e.g., if the type has
already been defined), the function returns NULL_TREE. */
static tree
maybe_new_partial_specialization (tree type)
{
/* An implicit instantiation of an incomplete type implies
the definition of a new class template.
template<typename T>
struct S;
template<typename T>
struct S<T*>;
Here, S<T*> is an implicit instantiation of S whose type
is incomplete. */
if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
return type;
/* It can also be the case that TYPE is a completed specialization.
Continuing the previous example, suppose we also declare:
template<typename T>
requires Integral<T>
struct S<T*>;
Here, S<T*> refers to the specialization S<T*> defined
above. However, we need to differentiate definitions because
we intend to define a new partial specialization. In this case,
we rely on the fact that the constraints are different for
this declaration than that above.
Note that we also get here for injected class names and
late-parsed template definitions. We must ensure that we
do not create new type declarations for those cases. */
if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
{
tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
tree args = CLASSTYPE_TI_ARGS (type);
/* If there are no template parameters, this cannot be a new
partial template specialization? */
if (!current_template_parms)
return NULL_TREE;
/* The injected-class-name is not a new partial specialization. */
if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
return NULL_TREE;
/* If the constraints are not the same as those of the primary
then, we can probably create a new specialization. */
tree type_constr = current_template_constraints ();
if (type == TREE_TYPE (tmpl))
{
tree main_constr = get_constraints (tmpl);
if (equivalent_constraints (type_constr, main_constr))
return NULL_TREE;
}
/* Also, if there's a pre-existing specialization with matching
constraints, then this also isn't new. */
tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
while (specs)
{
tree spec_tmpl = TREE_VALUE (specs);
tree spec_args = TREE_PURPOSE (specs);
tree spec_constr = get_constraints (spec_tmpl);
if (comp_template_args (args, spec_args)
&& equivalent_constraints (type_constr, spec_constr))
return NULL_TREE;
specs = TREE_CHAIN (specs);
}
/* Create a new type node (and corresponding type decl)
for the newly declared specialization. */
tree t = make_class_type (TREE_CODE (type));
CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
/* We only need a separate type node for storing the definition of this
partial specialization; uses of S<T*> are unconstrained, so all are
equivalent. So keep TYPE_CANONICAL the same. */
TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
/* Build the corresponding type decl. */
tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
DECL_CONTEXT (d) = TYPE_CONTEXT (t);
DECL_SOURCE_LOCATION (d) = input_location;
TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
return t;
}
return NULL_TREE;
}
/* The TYPE is being declared. If it is a template type, that means it
is a partial specialization. Do appropriate error-checking. */
tree
maybe_process_partial_specialization (tree type)
{
tree context;
if (type == error_mark_node)
return error_mark_node;
/* A lambda that appears in specialization context is not itself a
specialization. */
if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
return type;
/* An injected-class-name is not a specialization. */
if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
return type;
if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
{
error ("name of class shadows template template parameter %qD",
TYPE_NAME (type));
return error_mark_node;
}
context = TYPE_CONTEXT (type);
if (TYPE_ALIAS_P (type))
{
tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
error ("specialization of alias template %qD",
TI_TEMPLATE (tinfo));
else
error ("explicit specialization of non-template %qT", type);
return error_mark_node;
}
else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
{
/* This is for ordinary explicit specialization and partial
specialization of a template class such as:
template <> class C<int>;
or:
template <class T> class C<T*>;
Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
if (tree t = maybe_new_partial_specialization (type))
{
if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
&& !at_namespace_scope_p ())
return error_mark_node;
SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
if (processing_template_decl)
{
tree decl = push_template_decl (TYPE_MAIN_DECL (t));
if (decl == error_mark_node)
return error_mark_node;
return TREE_TYPE (decl);
}
}
else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
error ("specialization of %qT after instantiation", type);
else if (errorcount && !processing_specialization
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
&& !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
/* Trying to define a specialization either without a template<> header
or in an inappropriate place. We've already given an error, so just
bail now so we don't actually define the specialization. */
return error_mark_node;
}
else if (CLASS_TYPE_P (type)
&& !CLASSTYPE_USE_TEMPLATE (type)
&& CLASSTYPE_TEMPLATE_INFO (type)
&& context && CLASS_TYPE_P (context)
&& CLASSTYPE_TEMPLATE_INFO (context))
{
/* This is for an explicit specialization of member class
template according to [temp.expl.spec/18]:
template <> template <class U> class C<int>::D;
The context `C<int>' must be an implicit instantiation.
Otherwise this is just a member class template declared
earlier like:
template <> class C<int> { template <class U> class D; };
template <> template <class U> class C<int>::D;
In the first case, `C<int>::D' is a specialization of `C<T>::D'
while in the second case, `C<int>::D' is a primary template
and `C<T>::D' may not exist. */
if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
&& !COMPLETE_TYPE_P (type))
{
tree t;
tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
if (current_namespace
!= decl_namespace_context (tmpl))
{
if (permerror (input_location,
"specialization of %qD in different namespace",
type))
inform (DECL_SOURCE_LOCATION (tmpl),
"from definition of %q#D", tmpl);
}
/* Check for invalid specialization after instantiation:
template <> template <> class C<int>::D<int>;
template <> template <class U> class C<int>::D; */
for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
t; t = TREE_CHAIN (t))
{
tree inst = TREE_VALUE (t);
if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
|| !COMPLETE_OR_OPEN_TYPE_P (inst))
{
/* We already have a full specialization of this partial
instantiation, or a full specialization has been
looked up but not instantiated. Reassign it to the
new member specialization template. */
spec_entry elt;
spec_entry *entry;
elt.tmpl = most_general_template (tmpl);
elt.args = CLASSTYPE_TI_ARGS (inst);
elt.spec = inst;
type_specializations->remove_elt (&elt);
elt.tmpl = tmpl;
CLASSTYPE_TI_ARGS (inst)
= elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
spec_entry **slot
= type_specializations->find_slot (&elt, INSERT);
entry = ggc_alloc<spec_entry> ();
*entry = elt;
*slot = entry;
}
else
/* But if we've had an implicit instantiation, that's a
problem ([temp.expl.spec]/6). */
error ("specialization %qT after instantiation %qT",
type, inst);
}
/* Mark TYPE as a specialization. And as a result, we only
have one level of template argument for the innermost
class template. */
SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
CLASSTYPE_TI_ARGS (type)
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
}
}
else if (processing_specialization)
{
/* Someday C++0x may allow for enum template specialization. */
if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
&& CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
pedwarn (input_location, OPT_Wpedantic, "template specialization "
"of %qD not allowed by ISO C++", type);
else
{
error ("explicit specialization of non-template %qT", type);
return error_mark_node;
}
}
return type;
}
/* Returns nonzero if we can optimize the retrieval of specializations
for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
static inline bool
optimize_specialization_lookup_p (tree tmpl)
{
return (DECL_FUNCTION_TEMPLATE_P (tmpl)
&& DECL_CLASS_SCOPE_P (tmpl)
/* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
parameter. */
&& CLASS_TYPE_P (DECL_CONTEXT (tmpl))
/* The optimized lookup depends on the fact that the
template arguments for the member function template apply
purely to the containing class, which is not true if the
containing class is an explicit or partial
specialization. */
&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
&& !DECL_MEMBER_TEMPLATE_P (tmpl)
&& !DECL_CONV_FN_P (tmpl)
/* It is possible to have a template that is not a member
template and is not a member of a template class:
template <typename T>
struct S { friend A::f(); };
Here, the friend function is a template, but the context does
not have template information. The optimized lookup relies
on having ARGS be the template arguments for both the class
and the function template. */
&& !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
}
/* Make sure ARGS doesn't use any inappropriate typedefs; we should have
gone through coerce_template_parms by now. */
static void
verify_unstripped_args_1 (tree inner)
{
for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
{
tree arg = TREE_VEC_ELT (inner, i);
if (TREE_CODE (arg) == TEMPLATE_DECL)
/* OK */;
else if (TYPE_P (arg))
gcc_assert (strip_typedefs (arg, NULL) == arg);
else if (ARGUMENT_PACK_P (arg))
verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
/* Allow typedefs on the type of a non-type argument, since a
parameter can have them. */;
else
gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
}
}
static void
verify_unstripped_args (tree args)
{
++processing_template_decl;
if (!any_dependent_template_arguments_p (args))
verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
--processing_template_decl;
}
/* Retrieve the specialization (in the sense of [temp.spec] - a
specialization is either an instantiation or an explicit
specialization) of TMPL for the given template ARGS. If there is
no such specialization, return NULL_TREE. The ARGS are a vector of
arguments, or a vector of vectors of arguments, in the case of
templates with more than one level of parameters.
If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
then we search for a partial specialization matching ARGS. This
parameter is ignored if TMPL is not a class template.
We can also look up a FIELD_DECL, if it is a lambda capture pack; the
result is a NONTYPE_ARGUMENT_PACK. */
static tree
retrieve_specialization (tree tmpl, tree args, hashval_t hash)
{
if (tmpl == NULL_TREE)
return NULL_TREE;
if (args == error_mark_node)
return NULL_TREE;
gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
|| TREE_CODE (tmpl) == FIELD_DECL);
/* There should be as many levels of arguments as there are
levels of parameters. */
gcc_assert (TMPL_ARGS_DEPTH (args)
== (TREE_CODE (tmpl) == TEMPLATE_DECL
? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
: template_class_depth (DECL_CONTEXT (tmpl))));
if (flag_checking)
verify_unstripped_args (args);
/* Lambda functions in templates aren't instantiated normally, but through
tsubst_lambda_expr. */
if (lambda_fn_in_template_p (tmpl))
return NULL_TREE;
if (optimize_specialization_lookup_p (tmpl))
{
/* The template arguments actually apply to the containing
class. Find the class specialization with those
arguments. */
tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
tree class_specialization
= retrieve_specialization (class_template, args, 0);
if (!class_specialization)
return NULL_TREE;
/* Find the instance of TMPL. */
tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
for (ovl_iterator iter (fns); iter; ++iter)
{
tree fn = *iter;
if (tree ti = get_template_info (fn))
if (TI_TEMPLATE (ti) == tmpl
/* using-declarations can bring in a different
instantiation of tmpl as a member of a different
instantiation of tmpl's class. We don't want those
here. */
&& DECL_CONTEXT (fn) == class_specialization)
return fn;
}
return NULL_TREE;
}
else
{
spec_entry *found;
spec_entry elt;
hash_table<spec_hasher> *specializations;
elt.tmpl = tmpl;
elt.args = args;
elt.spec = NULL_TREE;
if (DECL_CLASS_TEMPLATE_P (tmpl))
specializations = type_specializations;
else
specializations = decl_specializations;
if (hash == 0)
hash = spec_hasher::hash (&elt);
found = specializations->find_with_hash (&elt, hash);
if (found)
return found->spec;
}
return NULL_TREE;
}
/* Like retrieve_specialization, but for local declarations. */
tree
retrieve_local_specialization (tree tmpl)
{
if (local_specializations == NULL)
return NULL_TREE;
tree *slot = local_specializations->get (tmpl);
return slot ? *slot : NULL_TREE;
}
/* Returns nonzero iff DECL is a specialization of TMPL. */
int
is_specialization_of (tree decl, tree tmpl)
{
tree t;
if (TREE_CODE (decl) == FUNCTION_DECL)
{
for (t = decl;
t != NULL_TREE;
t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
if (t == tmpl)
return 1;
}
else
{
gcc_assert (TREE_CODE (decl) == TYPE_DECL);
for (t = TREE_TYPE (decl);
t != NULL_TREE;
t = CLASSTYPE_USE_TEMPLATE (t)
? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
return 1;
}
return 0;
}
/* Returns nonzero iff DECL is a specialization of friend declaration
FRIEND_DECL according to [temp.friend]. */
bool
is_specialization_of_friend (tree decl, tree friend_decl)
{
bool need_template = true;
int template_depth;
gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
|| TREE_CODE (decl) == TYPE_DECL);
/* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
of a template class, we want to check if DECL is a specialization
if this. */
if (TREE_CODE (friend_decl) == FUNCTION_DECL
&& DECL_TEMPLATE_INFO (friend_decl)
&& !DECL_USE_TEMPLATE (friend_decl))
{
/* We want a TEMPLATE_DECL for `is_specialization_of'. */
friend_decl = DECL_TI_TEMPLATE (friend_decl);
need_template = false;
}
else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
&& !PRIMARY_TEMPLATE_P (friend_decl))
need_template = false;
/* There is nothing to do if this is not a template friend. */
if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
return false;
if (is_specialization_of (decl, friend_decl))
return true;
/* [temp.friend/6]
A member of a class template may be declared to be a friend of a
non-template class. In this case, the corresponding member of
every specialization of the class template is a friend of the
class granting friendship.
For example, given a template friend declaration
template <class T> friend void A<T>::f();
the member function below is considered a friend
template <> struct A<int> {
void f();
};
For this type of template friend, TEMPLATE_DEPTH below will be
nonzero. To determine if DECL is a friend of FRIEND, we first
check if the enclosing class is a specialization of another. */
template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
if (template_depth
&& DECL_CLASS_SCOPE_P (decl)
&& is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
{
/* Next, we check the members themselves. In order to handle
a few tricky cases, such as when FRIEND_DECL's are
template <class T> friend void A<T>::g(T t);
template <class T> template <T t> friend void A<T>::h();
and DECL's are
void A<int>::g(int);
template <int> void A<int>::h();
we need to figure out ARGS, the template arguments from
the context of DECL. This is required for template substitution
of `T' in the function parameter of `g' and template parameter
of `h' in the above examples. Here ARGS corresponds to `int'. */
tree context = DECL_CONTEXT (decl);
tree args = NULL_TREE;
int current_depth = 0;
while (current_depth < template_depth)
{
if (CLASSTYPE_TEMPLATE_INFO (context))
{
if (current_depth == 0)
args = TYPE_TI_ARGS (context);
else
args = add_to_template_args (TYPE_TI_ARGS (context), args);
current_depth++;
}
context = TYPE_CONTEXT (context);
}
if (TREE_CODE (decl) == FUNCTION_DECL)
{
bool is_template;
tree friend_type;
tree decl_type;
tree friend_args_type;
tree decl_args_type;
/* Make sure that both DECL and FRIEND_DECL are templates or
non-templates. */
is_template = DECL_TEMPLATE_INFO (decl)
&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
if (need_template ^ is_template)
return false;
else if (is_template)
{
/* If both are templates, check template parameter list. */
tree friend_parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
args, tf_none);
if (!comp_template_parms
(DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
friend_parms))
return false;
decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
}
else
decl_type = TREE_TYPE (decl);
friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
tf_none, NULL_TREE);
if (friend_type == error_mark_node)
return false;
/* Check if return types match. */
if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
return false;
/* Check if function parameter types match, ignoring the
`this' parameter. */
friend_args_type = TYPE_ARG_TYPES (friend_type);
decl_args_type = TYPE_ARG_TYPES (decl_type);
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
friend_args_type = TREE_CHAIN (friend_args_type);
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
decl_args_type = TREE_CHAIN (decl_args_type);
return compparms (decl_args_type, friend_args_type);
}
else
{
/* DECL is a TYPE_DECL */
bool is_template;
tree decl_type = TREE_TYPE (decl);
/* Make sure that both DECL and FRIEND_DECL are templates or
non-templates. */
is_template
= CLASSTYPE_TEMPLATE_INFO (decl_type)
&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
if (need_template ^ is_template)
return false;
else if (is_template)
{
tree friend_parms;
/* If both are templates, check the name of the two
TEMPLATE_DECL's first because is_friend didn't. */
if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
!= DECL_NAME (friend_decl))
return false;
/* Now check template parameter list. */
friend_parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
args, tf_none);
return comp_template_parms
(DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
friend_parms);
}
else
return (DECL_NAME (decl)
== DECL_NAME (friend_decl));
}
}
return false;
}
/* Register the specialization SPEC as a specialization of TMPL with
the indicated ARGS. IS_FRIEND indicates whether the specialization
is actually just a friend declaration. ATTRLIST is the list of
attributes that the specialization is declared with or NULL when
it isn't. Returns SPEC, or an equivalent prior declaration, if
available.
We also store instantiations of field packs in the hash table, even
though they are not themselves templates, to make lookup easier. */
static tree
register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
hashval_t hash)
{
tree fn;
spec_entry **slot = NULL;
spec_entry elt;
gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
|| (TREE_CODE (tmpl) == FIELD_DECL
&& TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
if (TREE_CODE (spec) == FUNCTION_DECL
&& uses_template_parms (DECL_TI_ARGS (spec)))
/* This is the FUNCTION_DECL for a partial instantiation. Don't
register it; we want the corresponding TEMPLATE_DECL instead.
We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
the more obvious `uses_template_parms (spec)' to avoid problems
with default function arguments. In particular, given
something like this:
template <class T> void f(T t1, T t = T())
the default argument expression is not substituted for in an
instantiation unless and until it is actually needed. */
return spec;
if (optimize_specialization_lookup_p (tmpl))
/* We don't put these specializations in the hash table, but we might
want to give an error about a mismatch. */
fn = retrieve_specialization (tmpl, args, 0);
else
{
elt.tmpl = tmpl;
elt.args = args;
elt.spec = spec;
if (hash == 0)
hash = spec_hasher::hash (&elt);
slot =
decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
if (*slot)
fn = ((spec_entry *) *slot)->spec;
else
fn = NULL_TREE;
}
/* We can sometimes try to re-register a specialization that we've
already got. In particular, regenerate_decl_from_template calls
duplicate_decls which will update the specialization list. But,
we'll still get called again here anyhow. It's more convenient
to simply allow this than to try to prevent it. */
if (fn == spec)
return spec;
else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
{
if (DECL_TEMPLATE_INSTANTIATION (fn))
{
if (DECL_ODR_USED (fn)
|| DECL_EXPLICIT_INSTANTIATION (fn))
{
error ("specialization of %qD after instantiation",
fn);
return error_mark_node;
}
else
{
tree clone;
/* This situation should occur only if the first
specialization is an implicit instantiation, the
second is an explicit specialization, and the
implicit instantiation has not yet been used. That
situation can occur if we have implicitly
instantiated a member function and then specialized
it later.
We can also wind up here if a friend declaration that
looked like an instantiation turns out to be a
specialization:
template <class T> void foo(T);
class S { friend void foo<>(int) };
template <> void foo(int);
We transform the existing DECL in place so that any
pointers to it become pointers to the updated
declaration.
If there was a definition for the template, but not
for the specialization, we want this to look as if
there were no definition, and vice versa. */
DECL_INITIAL (fn) = NULL_TREE;
duplicate_decls (spec, fn, is_friend);
/* The call to duplicate_decls will have applied
[temp.expl.spec]:
An explicit specialization of a function template
is inline only if it is explicitly declared to be,
and independently of whether its function template
is.
to the primary function; now copy the inline bits to
the various clones. */
FOR_EACH_CLONE (clone, fn)
{
DECL_DECLARED_INLINE_P (clone)
= DECL_DECLARED_INLINE_P (fn);
DECL_SOURCE_LOCATION (clone)
= DECL_SOURCE_LOCATION (fn);
DECL_DELETED_FN (clone)
= DECL_DELETED_FN (fn);
}
check_specialization_namespace (tmpl);
return fn;
}
}
else if (DECL_TEMPLATE_SPECIALIZATION (fn))
{
tree dd = duplicate_decls (spec, fn, is_friend);
if (dd == error_mark_node)
/* We've already complained in duplicate_decls. */
return error_mark_node;
if (dd == NULL_TREE && DECL_INITIAL (spec))
/* Dup decl failed, but this is a new definition. Set the
line number so any errors match this new
definition. */
DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
return fn;
}
}
else if (fn)
return duplicate_decls (spec, fn, is_friend);
/* A specialization must be declared in the same namespace as the
template it is specializing. */
if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
&& !check_specialization_namespace (tmpl))
DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
{
spec_entry *entry = ggc_alloc<spec_entry> ();
gcc_assert (tmpl && args && spec);
*entry = elt;
*slot = entry;
if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
&& PRIMARY_TEMPLATE_P (tmpl)
&& DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
|| variable_template_p (tmpl))
/* If TMPL is a forward declaration of a template function, keep a list
of all specializations in case we need to reassign them to a friend
template later in tsubst_friend_function.
Also keep a list of all variable template instantiations so that
process_partial_specialization can check whether a later partial
specialization would have used it. */
DECL_TEMPLATE_INSTANTIATIONS (tmpl)
= tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
}
return spec;
}
/* Returns true iff two spec_entry nodes are equivalent. */
int comparing_specializations;
bool
spec_hasher::equal (spec_entry *e1, spec_entry *e2)
{
int equal;
++comparing_specializations;
equal = (e1->tmpl == e2->tmpl
&& comp_template_args (e1->args, e2->args));
if (equal && flag_concepts
/* tmpl could be a FIELD_DECL for a capture pack. */
&& TREE_CODE (e1->tmpl) == TEMPLATE_DECL
&& VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
&& uses_template_parms (e1->args))
{
/* Partial specializations of a variable template can be distinguished by
constraints. */
tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
equal = equivalent_constraints (c1, c2);
}
--comparing_specializations;
return equal;
}
/* Returns a hash for a template TMPL and template arguments ARGS. */
static hashval_t
hash_tmpl_and_args (tree tmpl, tree args)
{
hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
return iterative_hash_template_arg (args, val);
}
/* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
ignoring SPEC. */
hashval_t
spec_hasher::hash (spec_entry *e)
{
return hash_tmpl_and_args (e->tmpl, e->args);
}
/* Recursively calculate a hash value for a template argument ARG, for use
in the hash tables of template specializations. We must be
careful to (at least) skip the same entities template_args_equal
does. */
hashval_t
iterative_hash_template_arg (tree arg, hashval_t val)
{
if (arg == NULL_TREE)
return iterative_hash_object (arg, val);
if (!TYPE_P (arg))
/* Strip nop-like things, but not the same as STRIP_NOPS. */
while (CONVERT_EXPR_P (arg)
|| TREE_CODE (arg) == NON_LVALUE_EXPR
|| class_nttp_const_wrapper_p (arg))
arg = TREE_OPERAND (arg, 0);
enum tree_code code = TREE_CODE (arg);
val = iterative_hash_object (code, val);
switch (code)
{
case ARGUMENT_PACK_SELECT:
gcc_unreachable ();
case ERROR_MARK:
return val;
case IDENTIFIER_NODE:
return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
case TREE_VEC:
for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
return val;
case TYPE_PACK_EXPANSION:
case EXPR_PACK_EXPANSION:
val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
case TYPE_ARGUMENT_PACK:
case NONTYPE_ARGUMENT_PACK:
return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
case TREE_LIST:
for (; arg; arg = TREE_CHAIN (arg))
val = iterative_hash_template_arg (TREE_VALUE (arg), val);
return val;
case OVERLOAD:
for (lkp_iterator iter (arg); iter; ++iter)
val = iterative_hash_template_arg (*iter, val);
return val;
case CONSTRUCTOR:
{
tree field, value;
unsigned i;
iterative_hash_template_arg (TREE_TYPE (arg), val);
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
{
val = iterative_hash_template_arg (field, val);
val = iterative_hash_template_arg (value, val);
}
return val;
}
case PARM_DECL:
if (!DECL_ARTIFICIAL (arg))
{
val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
}
return iterative_hash_template_arg (TREE_TYPE (arg), val);
case TARGET_EXPR:
return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
case PTRMEM_CST:
val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
case TEMPLATE_PARM_INDEX:
val = iterative_hash_template_arg
(TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
case TRAIT_EXPR:
val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
case BASELINK:
val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
val);
return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
val);
case MODOP_EXPR:
val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
code = TREE_CODE (TREE_OPERAND (arg, 1));
val = iterative_hash_object (code, val);
return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
case LAMBDA_EXPR:
/* [temp.over.link] Two lambda-expressions are never considered
equivalent.
So just hash the closure type. */
return iterative_hash_template_arg (TREE_TYPE (arg), val);
case CAST_EXPR:
case IMPLICIT_CONV_EXPR:
case STATIC_CAST_EXPR:
case REINTERPRET_CAST_EXPR:
case CONST_CAST_EXPR:
case DYNAMIC_CAST_EXPR:
case NEW_EXPR:
val = iterative_hash_template_arg (TREE_TYPE (arg), val);
/* Now hash operands as usual. */
break;
case CALL_EXPR:
{
tree fn = CALL_EXPR_FN (arg);
if (tree name = dependent_name (fn))
{
if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
fn = name;
}
val = iterative_hash_template_arg (fn, val);
call_expr_arg_iterator ai;
for (tree x = first_call_expr_arg (arg, &ai); x;
x = next_call_expr_arg (&ai))
val = iterative_hash_template_arg (x, val);
return val;
}
default:
break;
}
char tclass = TREE_CODE_CLASS (code);
switch (tclass)
{
case tcc_type:
if (tree ats = alias_template_specialization_p (arg, nt_transparent))
{
// We want an alias specialization that survived strip_typedefs
// to hash differently from its TYPE_CANONICAL, to avoid hash
// collisions that compare as different in template_args_equal.
// These could be dependent specializations that strip_typedefs
// left alone, or untouched specializations because
// coerce_template_parms returns the unconverted template
// arguments if it sees incomplete argument packs.
tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
}
switch (TREE_CODE (arg))
{
case TEMPLATE_TEMPLATE_PARM:
{
tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
/* Do not recurse with TPI directly, as that is unbounded
recursion. */
val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
}
break;
case DECLTYPE_TYPE:
val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
break;
default:
if (tree canonical = TYPE_CANONICAL (arg))
val = iterative_hash_object (TYPE_HASH (canonical), val);
break;
}
return val;
case tcc_declaration:
case tcc_constant:
return iterative_hash_expr (arg, val);
default:
gcc_assert (IS_EXPR_CODE_CLASS (tclass));
for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
return val;
}
gcc_unreachable ();
return 0;
}
/* Unregister the specialization SPEC as a specialization of TMPL.
Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
if the SPEC was listed as a specialization of TMPL.
Note that SPEC has been ggc_freed, so we can't look inside it. */
bool
reregister_specialization (tree spec, tree tinfo, tree new_spec)
{
spec_entry *entry;
spec_entry elt;
elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
elt.args = TI_ARGS (tinfo);
elt.spec = NULL_TREE;
entry = decl_specializations->find (&elt);
if (entry != NULL)
{
gcc_assert (entry->spec == spec || entry->spec == new_spec);
gcc_assert (new_spec != NULL_TREE);
entry->spec = new_spec;
return 1;
}
return 0;
}
/* Like register_specialization, but for local declarations. We are
registering SPEC, an instantiation of TMPL. */
void
register_local_specialization (tree spec, tree tmpl)
{
gcc_assert (tmpl != spec);
local_specializations->put (tmpl, spec);
}
/* TYPE is a class type. Returns true if TYPE is an explicitly
specialized class. */
bool
explicit_class_specialization_p (tree type)
{
if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
return false;
return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
}
/* Print the list of functions at FNS, going through all the overloads
for each element of the list. Alternatively, FNS cannot be a
TREE_LIST, in which case it will be printed together with all the
overloads.
MORE and *STR should respectively be FALSE and NULL when the function
is called from the outside. They are used internally on recursive
calls. print_candidates manages the two parameters and leaves NULL
in *STR when it ends. */
static void
print_candidates_1 (tree fns, char **str, bool more = false)
{
if (TREE_CODE (fns) == TREE_LIST)
for (; fns; fns = TREE_CHAIN (fns))
print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
else
for (lkp_iterator iter (fns); iter;)
{
tree cand = *iter;
++iter;
const char *pfx = *str;
if (!pfx)
{
if (more || iter)
pfx = _("candidates are:");
else
pfx = _("candidate is:");
*str = get_spaces (pfx);
}
inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
}
}
/* Print the list of candidate FNS in an error message. FNS can also
be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
void
print_candidates (tree fns)
{
char *str = NULL;
print_candidates_1 (fns, &str);
free (str);
}
/* Get a (possibly) constrained template declaration for the
purpose of ordering candidates. */
static tree
get_template_for_ordering (tree list)
{
gcc_assert (TREE_CODE (list) == TREE_LIST);
tree f = TREE_VALUE (list);
if (tree ti = DECL_TEMPLATE_INFO (f))
return TI_TEMPLATE (ti);
return f;
}
/* Among candidates having the same signature, return the
most constrained or NULL_TREE if there is no best candidate.
If the signatures of candidates vary (e.g., template
specialization vs. member function), then there can be no
most constrained.
Note that we don't compare constraints on the functions
themselves, but rather those of their templates. */
static tree
most_constrained_function (tree candidates)
{
// Try to find the best candidate in a first pass.
tree champ = candidates;
for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
{
int winner = more_constrained (get_template_for_ordering (champ),
get_template_for_ordering (c));
if (winner == -1)
champ = c; // The candidate is more constrained
else if (winner == 0)
return NULL_TREE; // Neither is more constrained
}
// Verify that the champ is better than previous candidates.
for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
if (!more_constrained (get_template_for_ordering (champ),
get_template_for_ordering (c)))
return NULL_TREE;
}
return champ;
}
/* Returns the template (one of the functions given by TEMPLATE_ID)
which can be specialized to match the indicated DECL with the
explicit template args given in TEMPLATE_ID. The DECL may be
NULL_TREE if none is available. In that case, the functions in
TEMPLATE_ID are non-members.
If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
specialization of a member template.
The TEMPLATE_COUNT is the number of references to qualifying
template classes that appeared in the name of the function. See
check_explicit_specialization for a more accurate description.
TSK indicates what kind of template declaration (if any) is being
declared. TSK_TEMPLATE indicates that the declaration given by
DECL, though a FUNCTION_DECL, has template parameters, and is
therefore a template function.
The template args (those explicitly specified and those deduced)
are output in a newly created vector *TARGS_OUT.
If it is impossible to determine the result, an error message is
issued. The error_mark_node is returned to indicate failure. */
static tree
determine_specialization (tree template_id,
tree decl,
tree* targs_out,
int need_member_template,
int template_count,
tmpl_spec_kind tsk)
{
tree fns;
tree targs;
tree explicit_targs;
tree candidates = NULL_TREE;
/* A TREE_LIST of templates of which DECL may be a specialization.
The TREE_VALUE of each node is a TEMPLATE_DECL. The
corresponding TREE_PURPOSE is the set of template arguments that,
when used to instantiate the template, would produce a function
with the signature of DECL. */
tree templates = NULL_TREE;
int header_count;
cp_binding_level *b;
*targs_out = NULL_TREE;
if (template_id == error_mark_node || decl == error_mark_node)
return error_mark_node;
/* We shouldn't be specializing a member template of an
unspecialized class template; we already gave an error in
check_specialization_scope, now avoid crashing. */
if (!VAR_P (decl)
&& template_count && DECL_CLASS_SCOPE_P (decl)
&& template_class_depth (DECL_CONTEXT (decl)) > 0)
{
gcc_assert (errorcount);
return error_mark_node;
}
fns = TREE_OPERAND (template_id, 0);
explicit_targs = TREE_OPERAND (template_id, 1);
if (fns == error_mark_node)
return error_mark_node;
/* Check for baselinks. */
if (BASELINK_P (fns))
fns = BASELINK_FUNCTIONS (fns);
if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
{
error_at (DECL_SOURCE_LOCATION (decl),
"%qD is not a function template", fns);
return error_mark_node;
}
else if (VAR_P (decl) && !variable_template_p (fns))
{
error ("%qD is not a variable template", fns);
return error_mark_node;
}
/* Count the number of template headers specified for this
specialization. */
header_count = 0;
for (b = current_binding_level;
b->kind == sk_template_parms;
b = b->level_chain)
++header_count;
tree orig_fns = fns;
if (variable_template_p (fns))
{
tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
targs = coerce_template_parms (parms, explicit_targs, fns,
tf_warning_or_error,
/*req_all*/true, /*use_defarg*/true);
if (targs != error_mark_node)
templates = tree_cons (targs, fns, templates);
}
else for (lkp_iterator iter (fns); iter; ++iter)
{
tree fn = *iter;
if (TREE_CODE (fn) == TEMPLATE_DECL)
{
tree decl_arg_types;
tree fn_arg_types;
tree insttype;
/* In case of explicit specialization, we need to check if
the number of template headers appearing in the specialization
is correct. This is usually done in check_explicit_specialization,
but the check done there cannot be exhaustive when specializing
member functions. Consider the following code:
template <> void A<int>::f(int);
template <> template <> void A<int>::f(int);
Assuming that A<int> is not itself an explicit specialization
already, the first line specializes "f" which is a non-template
member function, whilst the second line specializes "f" which
is a template member function. So both lines are syntactically
correct, and check_explicit_specialization does not reject
them.
Here, we can do better, as we are matching the specialization
against the declarations. We count the number of template
headers, and we check if they match TEMPLATE_COUNT + 1
(TEMPLATE_COUNT is the number of qualifying template classes,
plus there must be another header for the member template
itself).
Notice that if header_count is zero, this is not a
specialization but rather a template instantiation, so there
is no check we can perform here. */
if (header_count && header_count != template_count + 1)
continue;
/* Check that the number of template arguments at the
innermost level for DECL is the same as for FN. */
if (current_binding_level->kind == sk_template_parms
&& !current_binding_level->explicit_spec_p
&& (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
!= TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
(current_template_parms))))
continue;
/* DECL might be a specialization of FN. */
decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
/* For a non-static member function, we need to make sure
that the const qualification is the same. Since
get_bindings does not try to merge the "this" parameter,
we must do the comparison explicitly. */
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
{
if (!same_type_p (TREE_VALUE (fn_arg_types),
TREE_VALUE (decl_arg_types)))
continue;
/* And the ref-qualification. */
if (type_memfn_rqual (TREE_TYPE (decl))
!= type_memfn_rqual (TREE_TYPE (fn)))
continue;
}
/* Skip the "this" parameter and, for constructors of
classes with virtual bases, the VTT parameter. A
full specialization of a constructor will have a VTT
parameter, but a template never will. */
decl_arg_types
= skip_artificial_parms_for (decl, decl_arg_types);
fn_arg_types
= skip_artificial_parms_for (fn, fn_arg_types);
/* Function templates cannot be specializations; there are
no partial specializations of functions. Therefore, if
the type of DECL does not match FN, there is no
match.
Note that it should never be the case that we have both
candidates added here, and for regular member functions
below. */
if (tsk == tsk_template)
{
if (compparms (fn_arg_types, decl_arg_types))
candidates = tree_cons (NULL_TREE, fn, candidates);
continue;
}
/* See whether this function might be a specialization of this
template. Suppress access control because we might be trying
to make this specialization a friend, and we have already done
access control for the declaration of the specialization. */
push_deferring_access_checks (dk_no_check);
targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
pop_deferring_access_checks ();
if (!targs)
/* We cannot deduce template arguments that when used to
specialize TMPL will produce DECL. */
continue;
if (uses_template_parms (targs))
/* We deduced something involving 'auto', which isn't a valid
template argument. */
continue;
/* Remove, from the set of candidates, all those functions
whose constraints are not satisfied. */
if (flag_concepts && !constraints_satisfied_p (fn, targs))
continue;
// Then, try to form the new function type.
insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
if (insttype == error_mark_node)
continue;
fn_arg_types
= skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
if (!compparms (fn_arg_types, decl_arg_types))
continue;
/* Save this template, and the arguments deduced. */
templates = tree_cons (targs, fn, templates);
}
else if (need_member_template)
/* FN is an ordinary member function, and we need a
specialization of a member template. */
;
else if (TREE_CODE (fn) != FUNCTION_DECL)
/* We can get IDENTIFIER_NODEs here in certain erroneous
cases. */
;
else if (!DECL_FUNCTION_MEMBER_P (fn))
/* This is just an ordinary non-member function. Nothing can
be a specialization of that. */
;
else if (DECL_ARTIFICIAL (fn))
/* Cannot specialize functions that are created implicitly. */
;
else
{
tree decl_arg_types;
/* This is an ordinary member function. However, since
we're here, we can assume its enclosing class is a
template class. For example,
template <typename T> struct S { void f(); };
template <> void S<int>::f() {}
Here, S<int>::f is a non-template, but S<int> is a
template class. If FN has the same type as DECL, we
might be in business. */
if (!DECL_TEMPLATE_INFO (fn))
/* Its enclosing class is an explicit specialization
of a template class. This is not a candidate. */
continue;
if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
TREE_TYPE (TREE_TYPE (fn))))
/* The return types differ. */
continue;
/* Adjust the type of DECL in case FN is a static member. */
decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
if (DECL_STATIC_FUNCTION_P (fn)
&& DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
decl_arg_types = TREE_CHAIN (decl_arg_types);
if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
decl_arg_types))
continue;
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
&& (type_memfn_rqual (TREE_TYPE (decl))
!= type_memfn_rqual (TREE_TYPE (fn))))
continue;
// If the deduced arguments do not satisfy the constraints,
// this is not a candidate.
if (flag_concepts && !constraints_satisfied_p (fn))
continue;
// Add the candidate.
candidates = tree_cons (NULL_TREE, fn, candidates);
}
}
if (templates && TREE_CHAIN (templates))
{
/* We have:
[temp.expl.spec]
It is possible for a specialization with a given function
signature to be instantiated from more than one function
template. In such cases, explicit specification of the
template arguments must be used to uniquely identify the
function template specialization being specialized.
Note that here, there's no suggestion that we're supposed to
determine which of the candidate templates is most
specialized. However, we, also have:
[temp.func.order]
Partial ordering of overloaded function template
declarations is used in the following contexts to select
the function template to which a function template
specialization refers:
-- when an explicit specialization refers to a function
template.
So, we do use the partial ordering rules, at least for now.
This extension can only serve to make invalid programs valid,
so it's safe. And, there is strong anecdotal evidence that
the committee intended the partial ordering rules to apply;
the EDG front end has that behavior, and John Spicer claims
that the committee simply forgot to delete the wording in
[temp.expl.spec]. */
tree tmpl = most_specialized_instantiation (templates);
if (tmpl != error_mark_node)
{
templates = tmpl;
TREE_CHAIN (templates) = NULL_TREE;
}
}
// Concepts allows multiple declarations of member functions
// with the same signature. Like above, we need to rely on
// on the partial ordering of those candidates to determine which
// is the best.
if (flag_concepts && candidates && TREE_CHAIN (candidates))
{
if (tree cand = most_constrained_function (candidates))
{
candidates = cand;
TREE_CHAIN (cand) = NULL_TREE;
}
}
if (templates == NULL_TREE && candidates == NULL_TREE)
{
error ("template-id %qD for %q+D does not match any template "
"declaration", template_id, decl);
if (header_count && header_count != template_count + 1)
inform (DECL_SOURCE_LOCATION (decl),
"saw %d %<template<>%>, need %d for "
"specializing a member function template",
header_count, template_count + 1);
else
print_candidates (orig_fns);
return error_mark_node;
}
else if ((templates && TREE_CHAIN (templates))
|| (candidates && TREE_CHAIN (candidates))
|| (templates && candidates))
{
error ("ambiguous template specialization %qD for %q+D",
template_id, decl);
candidates = chainon (candidates, templates);
print_candidates (candidates);
return error_mark_node;
}
/* We have one, and exactly one, match. */
if (candidates)
{
tree fn = TREE_VALUE (candidates);
*targs_out = copy_node (DECL_TI_ARGS (fn));
/* Propagate the candidate's constraints to the declaration. */
set_constraints (decl, get_constraints (fn));
/* DECL is a re-declaration or partial instantiation of a template
function. */
if (TREE_CODE (fn) == TEMPLATE_DECL)
return fn;
/* It was a specialization of an ordinary member function in a
template class. */
return DECL_TI_TEMPLATE (fn);
}
/* It was a specialization of a template. */
targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
{
*targs_out = copy_node (targs);
SET_TMPL_ARGS_LEVEL (*targs_out,
TMPL_ARGS_DEPTH (*targs_out),
TREE_PURPOSE (templates));
}
else
*targs_out = TREE_PURPOSE (templates);
return TREE_VALUE (templates);
}
/* Returns a chain of parameter types, exactly like the SPEC_TYPES,
but with the default argument values filled in from those in the
TMPL_TYPES. */
static tree
copy_default_args_to_explicit_spec_1 (tree spec_types,
tree tmpl_types)
{
tree new_spec_types;
if (!spec_types)
return NULL_TREE;
if (spec_types == void_list_node)
return void_list_node;
/* Substitute into the rest of the list. */
new_spec_types =
copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
TREE_CHAIN (tmpl_types));
/* Add the default argument for this parameter. */
return hash_tree_cons (TREE_PURPOSE (tmpl_types),
TREE_VALUE (spec_types),
new_spec_types);
}
/* DECL is an explicit specialization. Replicate default arguments
from the template it specializes. (That way, code like:
template <class T> void f(T = 3);
template <> void f(double);
void g () { f (); }
works, as required.) An alternative approach would be to look up
the correct default arguments at the call-site, but this approach
is consistent with how implicit instantiations are handled. */
static void
copy_default_args_to_explicit_spec (tree decl)
{
tree tmpl;
tree spec_types;
tree tmpl_types;
tree new_spec_types;
tree old_type;
tree new_type;
tree t;
tree object_type = NULL_TREE;
tree in_charge = NULL_TREE;
tree vtt = NULL_TREE;
/* See if there's anything we need to do. */
tmpl = DECL_TI_TEMPLATE (decl);
tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
for (t = tmpl_types; t; t = TREE_CHAIN (t))
if (TREE_PURPOSE (t))
break;
if (!t)
return;
old_type = TREE_TYPE (decl);
spec_types = TYPE_ARG_TYPES (old_type);
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
{
/* Remove the this pointer, but remember the object's type for
CV quals. */
object_type = TREE_TYPE (TREE_VALUE (spec_types));
spec_types = TREE_CHAIN (spec_types);
tmpl_types = TREE_CHAIN (tmpl_types);
if (DECL_HAS_IN_CHARGE_PARM_P (decl))
{
/* DECL may contain more parameters than TMPL due to the extra
in-charge parameter in constructors and destructors. */
in_charge = spec_types;
spec_types = TREE_CHAIN (spec_types);
}
if (DECL_HAS_VTT_PARM_P (decl))
{
vtt = spec_types;
spec_types = TREE_CHAIN (spec_types);
}
}
/* Compute the merged default arguments. */
new_spec_types =
copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
/* Compute the new FUNCTION_TYPE. */
if (object_type)
{
if (vtt)
new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
TREE_VALUE (vtt),
new_spec_types);
if (in_charge)
/* Put the in-charge parameter back. */
new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
TREE_VALUE (in_charge),
new_spec_types);
new_type = build_method_type_directly (object_type,
TREE_TYPE (old_type),
new_spec_types);
}
else
new_type = build_function_type (TREE_TYPE (old_type),
new_spec_types);
new_type = cp_build_type_attribute_variant (new_type,
TYPE_ATTRIBUTES (old_type));
new_type = cxx_copy_lang_qualifiers (new_type, old_type);
TREE_TYPE (decl) = new_type;
}
/* Return the number of template headers we expect to see for a definition
or specialization of CTYPE or one of its non-template members. */
int
num_template_headers_for_class (tree ctype)
{
int num_templates = 0;
while (ctype && CLASS_TYPE_P (ctype))
{
/* You're supposed to have one `template <...>' for every
template class, but you don't need one for a full
specialization. For example:
template <class T> struct S{};
template <> struct S<int> { void f(); };
void S<int>::f () {}
is correct; there shouldn't be a `template <>' for the
definition of `S<int>::f'. */
if (!CLASSTYPE_TEMPLATE_INFO (ctype))
/* If CTYPE does not have template information of any
kind, then it is not a template, nor is it nested
within a template. */
break;
if (explicit_class_specialization_p (ctype))
break;
if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
++num_templates;
ctype = TYPE_CONTEXT (ctype);
}
return num_templates;
}
/* Do a simple sanity check on the template headers that precede the
variable declaration DECL. */
void
check_template_variable (tree decl)
{
tree ctx = CP_DECL_CONTEXT (decl);
int wanted = num_template_headers_for_class (ctx);
if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
{
if (cxx_dialect < cxx14)
pedwarn (DECL_SOURCE_LOCATION (decl), 0,
"variable templates only available with "
"%<-std=c++14%> or %<-std=gnu++14%>");
// Namespace-scope variable templates should have a template header.
++wanted;
}
if (template_header_count > wanted)
{
auto_diagnostic_group d;
bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
"too many template headers for %qD "
"(should be %d)",
decl, wanted);
if (warned && CLASS_TYPE_P (ctx)
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
inform (DECL_SOURCE_LOCATION (decl),
"members of an explicitly specialized class are defined "
"without a template header");
}
}
/* An explicit specialization whose declarator-id or class-head-name is not
qualified shall be declared in the nearest enclosing namespace of the
template, or, if the namespace is inline (7.3.1), any namespace from its
enclosing namespace set.
If the name declared in the explicit instantiation is an unqualified name,
the explicit instantiation shall appear in the namespace where its template
is declared or, if that namespace is inline (7.3.1), any namespace from its
enclosing namespace set. */
void
check_unqualified_spec_or_inst (tree t, location_t loc)
{
tree tmpl = most_general_template (t);
if (DECL_NAMESPACE_SCOPE_P (tmpl)
&& !is_nested_namespace (current_namespace,
CP_DECL_CONTEXT (tmpl), true))
{
if (processing_specialization)
permerror (loc, "explicit specialization of %qD outside its "
"namespace must use a nested-name-specifier", tmpl);
else if (processing_explicit_instantiation
&& cxx_dialect >= cxx11)
/* This was allowed in C++98, so only pedwarn. */
pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
"outside its namespace must use a nested-name-"
"specifier", tmpl);
}
}
/* Warn for a template specialization SPEC that is missing some of a set
of function or type attributes that the template TEMPL is declared with.
ATTRLIST is a list of additional attributes that SPEC should be taken
to ultimately be declared with. */
static void
warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
{
if (DECL_FUNCTION_TEMPLATE_P (tmpl))
tmpl = DECL_TEMPLATE_RESULT (tmpl);
/* Avoid warning if the difference between the primary and
the specialization is not in one of the attributes below. */
const char* const blacklist[] = {
"alloc_align", "alloc_size", "assume_aligned", "format",
"format_arg", "malloc", "nonnull", NULL
};
/* Put together a list of the black listed attributes that the primary
template is declared with that the specialization is not, in case
it's not apparent from the most recent declaration of the primary. */
pretty_printer str;
unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
blacklist, &str);
if (!nattrs)
return;
auto_diagnostic_group d;
if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
"explicit specialization %q#D may be missing attributes",
spec))
inform (DECL_SOURCE_LOCATION (tmpl),
nattrs > 1
? G_("missing primary template attributes %s")
: G_("missing primary template attribute %s"),
pp_formatted_text (&str));
}
/* Check to see if the function just declared, as indicated in
DECLARATOR, and in DECL, is a specialization of a function
template. We may also discover that the declaration is an explicit
instantiation at this point.
Returns DECL, or an equivalent declaration that should be used
instead if all goes well. Issues an error message if something is
amiss. Returns error_mark_node if the error is not easily
recoverable.
FLAGS is a bitmask consisting of the following flags:
2: The function has a definition.
4: The function is a friend.
The TEMPLATE_COUNT is the number of references to qualifying
template classes that appeared in the name of the function. For
example, in
template <class T> struct S { void f(); };
void S<int>::f();
the TEMPLATE_COUNT would be 1. However, explicitly specialized
classes are not counted in the TEMPLATE_COUNT, so that in
template <class T> struct S {};
template <> struct S<int> { void f(); }
template <> void S<int>::f();
the TEMPLATE_COUNT would be 0. (Note that this declaration is
invalid; there should be no template <>.)
If the function is a specialization, it is marked as such via
DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
is set up correctly, and it is added to the list of specializations
for that template. */
tree
check_explicit_specialization (tree declarator,
tree decl,
int template_count,
int flags,
tree attrlist)
{
int have_def = flags & 2;
int is_friend = flags & 4;
bool is_concept = flags & 8;
int specialization = 0;
int explicit_instantiation = 0;
int member_specialization = 0;
tree ctype = DECL_CLASS_CONTEXT (decl);
tree dname = DECL_NAME (decl);
tmpl_spec_kind tsk;
if (is_friend)
{
if (!processing_specialization)
tsk = tsk_none;
else
tsk = tsk_excessive_parms;
}
else
tsk = current_tmpl_spec_kind (template_count);
switch (tsk)
{
case tsk_none:
if (processing_specialization && !VAR_P (decl))
{
specialization = 1;
SET_DECL_TEMPLATE_SPECIALIZATION (decl);
}
else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
{
if (is_friend)
/* This could be something like:
template <class T> void f(T);
class S { friend void f<>(int); } */
specialization = 1;
else
{
/* This case handles bogus declarations like template <>
template <class T> void f<int>(); */
error_at (cp_expr_loc_or_input_loc (declarator),
"template-id %qE in declaration of primary template",
declarator);
return decl;
}
}
break;
case tsk_invalid_member_spec:
/* The error has already been reported in
check_specialization_scope. */
return error_mark_node;
case tsk_invalid_expl_inst:
error ("template parameter list used in explicit instantiation");
/* Fall through. */
case tsk_expl_inst:
if (have_def)
error ("definition provided for explicit instantiation");
explicit_instantiation = 1;
break;
case tsk_excessive_parms:
case tsk_insufficient_parms:
if (tsk == tsk_excessive_parms)
error ("too many template parameter lists in declaration of %qD",
decl);
else if (template_header_count)
error("too few template parameter lists in declaration of %qD", decl);
else
error("explicit specialization of %qD must be introduced by "
"%<template <>%>", decl);
/* Fall through. */
case tsk_expl_spec:
if (is_concept)
error ("explicit specialization declared %<concept%>");
if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
/* In cases like template<> constexpr bool v = true;
We'll give an error in check_template_variable. */
break;
SET_DECL_TEMPLATE_SPECIALIZATION (decl);
if (ctype)
member_specialization = 1;
else
specialization = 1;
break;
case tsk_template:
if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
{
/* This case handles bogus declarations like template <>
template <class T> void f<int>(); */
if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
error_at (cp_expr_loc_or_input_loc (declarator),
"template-id %qE in declaration of primary template",
declarator);
else if (variable_template_p (TREE_OPERAND (declarator, 0)))
{
/* Partial specialization of variable template. */
SET_DECL_TEMPLATE_SPECIALIZATION (decl);
specialization = 1;
goto ok;
}
else if (cxx_dialect < cxx14)
error_at (cp_expr_loc_or_input_loc (declarator),
"non-type partial specialization %qE "
"is not allowed", declarator);
else
error_at (cp_expr_loc_or_input_loc (declarator),
"non-class, non-variable partial specialization %qE "
"is not allowed", declarator);
return decl;
ok:;
}
if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
/* This is a specialization of a member template, without
specialization the containing class. Something like:
template <class T> struct S {
template <class U> void f (U);
};
template <> template <class U> void S<int>::f(U) {}
That's a specialization -- but of the entire template. */
specialization = 1;
break;
default:
gcc_unreachable ();
}
if ((specialization || member_specialization)
/* This doesn't apply to variable templates. */
&& FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
{
tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
for (; t; t = TREE_CHAIN (t))
if (TREE_PURPOSE (t))
{
permerror (input_location,
"default argument specified in explicit specialization");
break;
}
}
if (specialization || member_specialization || explicit_instantiation)
{
tree tmpl = NULL_TREE;
tree targs = NULL_TREE;
bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
/* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
if (!was_template_id)
{
tree fns;
gcc_assert (identifier_p (declarator));
if (ctype)
fns = dname;
else
{
/* If there is no class context, the explicit instantiation
must be at namespace scope. */
gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
/* Find the namespace binding, using the declaration
context. */
fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
false, true);
if (fns == error_mark_node)
/* If lookup fails, look for a friend declaration so we can
give a better diagnostic. */
fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
/*type*/false, /*complain*/true,
/*hidden*/true);
if (fns == error_mark_node || !is_overloaded_fn (fns))
{
error ("%qD is not a template function", dname);
fns = error_mark_node;
}
}
declarator = lookup_template_function (fns, NULL_TREE);
}
if (declarator == error_mark_node)
return error_mark_node;
if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
{
if (!explicit_instantiation)
/* A specialization in class scope. This is invalid,
but the error will already have been flagged by
check_specialization_scope. */
return error_mark_node;
else
{
/* It's not valid to write an explicit instantiation in
class scope, e.g.:
class C { template void f(); }
This case is caught by the parser. However, on
something like:
template class C { void f(); };
(which is invalid) we can get here. The error will be
issued later. */
;
}
return decl;
}
else if (ctype != NULL_TREE
&& (identifier_p (TREE_OPERAND (declarator, 0))))
{
// We'll match variable templates in start_decl.
if (VAR_P (decl))
return decl;
/* Find the list of functions in ctype that have the same
name as the declared function. */
tree name = TREE_OPERAND (declarator, 0);
if (constructor_name_p (name, ctype))
{
if (DECL_CONSTRUCTOR_P (decl)
? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
: !CLASSTYPE_DESTRUCTOR (ctype))
{
/* From [temp.expl.spec]:
If such an explicit specialization for the member
of a class template names an implicitly-declared
special member function (clause _special_), the
program is ill-formed.
Similar language is found in [temp.explicit]. */
error ("specialization of implicitly-declared special member function");
return error_mark_node;
}
name = DECL_NAME (decl);
}
/* For a type-conversion operator, We might be looking for
`operator int' which will be a specialization of
`operator T'. Grab all the conversion operators, and
then select from them. */
tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
? conv_op_identifier : name);
if (fns == NULL_TREE)
{
error ("no member function %qD declared in %qT", name, ctype);
return error_mark_node;
}
else
TREE_OPERAND (declarator, 0) = fns;
}
/* Figure out what exactly is being specialized at this point.
Note that for an explicit instantiation, even one for a
member function, we cannot tell a priori whether the
instantiation is for a member template, or just a member
function of a template class. Even if a member template is
being instantiated, the member template arguments may be
elided if they can be deduced from the rest of the
declaration. */
tmpl = determine_specialization (declarator, decl,
&targs,
member_specialization,
template_count,
tsk);
if (!tmpl || tmpl == error_mark_node)
/* We couldn't figure out what this declaration was
specializing. */
return error_mark_node;
else
{
if (TREE_CODE (decl) == FUNCTION_DECL
&& DECL_HIDDEN_FRIEND_P (tmpl))
{
auto_diagnostic_group d;
if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
"friend declaration %qD is not visible to "
"explicit specialization", tmpl))
inform (DECL_SOURCE_LOCATION (tmpl),
"friend declaration here");
}
else if (!ctype && !is_friend
&& CP_DECL_CONTEXT (decl) == current_namespace)
check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
tree gen_tmpl = most_general_template (tmpl);
if (explicit_instantiation)
{
/* We don't set DECL_EXPLICIT_INSTANTIATION here; that
is done by do_decl_instantiation later. */
int arg_depth = TMPL_ARGS_DEPTH (targs);
int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
if (arg_depth > parm_depth)
{
/* If TMPL is not the most general template (for
example, if TMPL is a friend template that is
injected into namespace scope), then there will
be too many levels of TARGS. Remove some of them
here. */
int i;
tree new_targs;
new_targs = make_tree_vec (parm_depth);
for (i = arg_depth - parm_depth; i < arg_depth; ++i)
TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
= TREE_VEC_ELT (targs, i);
targs = new_targs;
}
return instantiate_template (tmpl, targs, tf_error);
}
/* If we thought that the DECL was a member function, but it
turns out to be specializing a static member function,
make DECL a static member function as well. */
if (DECL_FUNCTION_TEMPLATE_P (tmpl)
&& DECL_STATIC_FUNCTION_P (tmpl)
&& DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
revert_static_member_fn (decl);
/* If this is a specialization of a member template of a
template class, we want to return the TEMPLATE_DECL, not
the specialization of it. */
if (tsk == tsk_template && !was_template_id)
{
tree result = DECL_TEMPLATE_RESULT (tmpl);
SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
DECL_INITIAL (result) = NULL_TREE;
if (have_def)
{
tree parm;
DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
DECL_SOURCE_LOCATION (result)
= DECL_SOURCE_LOCATION (decl);
/* We want to use the argument list specified in the
definition, not in the original declaration. */
DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
for (parm = DECL_ARGUMENTS (result); parm;
parm = DECL_CHAIN (parm))
DECL_CONTEXT (parm) = result;
}
return register_specialization (tmpl, gen_tmpl, targs,
is_friend, 0);
}
/* Set up the DECL_TEMPLATE_INFO for DECL. */
DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
if (was_template_id)
TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
/* Inherit default function arguments from the template
DECL is specializing. */
if (DECL_FUNCTION_TEMPLATE_P (tmpl))
copy_default_args_to_explicit_spec (decl);
/* This specialization has the same protection as the
template it specializes. */
TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
/* 7.1.1-1 [dcl.stc]
A storage-class-specifier shall not be specified in an
explicit specialization...
The parser rejects these, so unless action is taken here,
explicit function specializations will always appear with
global linkage.
The action recommended by the C++ CWG in response to C++
defect report 605 is to make the storage class and linkage
of the explicit specialization match the templated function:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
*/
if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
{
tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
/* A concept cannot be specialized. */
if (DECL_DECLARED_CONCEPT_P (tmpl_func))
{
error ("explicit specialization of function concept %qD",
gen_tmpl);
return error_mark_node;
}
/* This specialization has the same linkage and visibility as
the function template it specializes. */
TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
if (! TREE_PUBLIC (decl))
{
DECL_INTERFACE_KNOWN (decl) = 1;
DECL_NOT_REALLY_EXTERN (decl) = 1;
}
DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
{
DECL_VISIBILITY_SPECIFIED (decl) = 1;
DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
}
}
/* If DECL is a friend declaration, declared using an
unqualified name, the namespace associated with DECL may
have been set incorrectly. For example, in:
template <typename T> void f(T);
namespace N {
struct S { friend void f<int>(int); }
}
we will have set the DECL_CONTEXT for the friend
declaration to N, rather than to the global namespace. */
if (DECL_NAMESPACE_SCOPE_P (decl))
DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
if (is_friend && !have_def)
/* This is not really a declaration of a specialization.
It's just the name of an instantiation. But, it's not
a request for an instantiation, either. */
SET_DECL_IMPLICIT_INSTANTIATION (decl);
else if (TREE_CODE (decl) == FUNCTION_DECL)
/* A specialization is not necessarily COMDAT. */
DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
&& DECL_DECLARED_INLINE_P (decl));
else if (VAR_P (decl))
DECL_COMDAT (decl) = false;
/* If this is a full specialization, register it so that we can find
it again. Partial specializations will be registered in
process_partial_specialization. */
if (!processing_template_decl)
{
warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
decl = register_specialization (decl, gen_tmpl, targs,
is_friend, 0);
}
/* A 'structor should already have clones. */
gcc_assert (decl == error_mark_node
|| variable_template_p (tmpl)
|| !(DECL_CONSTRUCTOR_P (decl)
|| DECL_DESTRUCTOR_P (decl))
|| DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
}
}
return decl;
}
/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
parameters. These are represented in the same format used for
DECL_TEMPLATE_PARMS. */
int
comp_template_parms (const_tree parms1, const_tree parms2)
{
const_tree p1;
const_tree p2;
if (parms1 == parms2)
return 1;
for (p1 = parms1, p2 = parms2;
p1 != NULL_TREE && p2 != NULL_TREE;
p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
{
tree t1 = TREE_VALUE (p1);
tree t2 = TREE_VALUE (p2);
int i;
gcc_assert (TREE_CODE (t1) == TREE_VEC);
gcc_assert (TREE_CODE (t2) == TREE_VEC);
if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
return 0;
for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
{
tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
/* If either of the template parameters are invalid, assume
they match for the sake of error recovery. */
if (error_operand_p (parm1) || error_operand_p (parm2))
return 1;
if (TREE_CODE (parm1) != TREE_CODE (parm2))
return 0;
if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
&& (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
== TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
continue;
else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
return 0;
}
}
if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
/* One set of parameters has more parameters lists than the
other. */
return 0;
return 1;
}
/* Returns true if two template parameters are declared with
equivalent constraints. */
static bool
template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
{
tree req1 = TREE_TYPE (parm1);
tree req2 = TREE_TYPE (parm2);
if (!req1 != !req2)
return false;
if (req1)
return cp_tree_equal (req1, req2);
return true;
}
/* Returns true when two template parameters are equivalent. */
static bool
template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
{
tree decl1 = TREE_VALUE (parm1);
tree decl2 = TREE_VALUE (parm2);
/* If either of the template parameters are invalid, assume
they match for the sake of error recovery. */
if (error_operand_p (decl1) || error_operand_p (decl2))
return true;
/* ... they declare parameters of the same kind. */
if (TREE_CODE (decl1) != TREE_CODE (decl2))
return false;
/* ... one parameter was introduced by a parameter declaration, then
both are. This case arises as a result of eagerly rewriting declarations
during parsing. */
if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
return false;
/* ... if either declares a pack, they both do. */
if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
return false;
if (TREE_CODE (decl1) == PARM_DECL)
{
/* ... if they declare non-type parameters, the types are equivalent. */
if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
return false;
}
else if (TREE_CODE (decl2) == TEMPLATE_DECL)
{
/* ... if they declare template template parameters, their template
parameter lists are equivalent. */
if (!template_heads_equivalent_p (decl1, decl2))
return false;
}
/* ... if they are declared with a qualified-concept name, they both
are, and those names are equivalent. */
return template_parameter_constraints_equivalent_p (parm1, parm2);
}
/* Returns true if two template parameters lists are equivalent.
Two template parameter lists are equivalent if they have the
same length and their corresponding parameters are equivalent.
PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
data structure returned by DECL_TEMPLATE_PARMS.
This is generally the same implementation as comp_template_parms
except that it also the concept names and arguments used to
introduce parameters. */
static bool
template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
{
if (parms1 == parms2)
return true;
const_tree p1 = parms1;
const_tree p2 = parms2;
while (p1 != NULL_TREE && p2 != NULL_TREE)
{
tree list1 = TREE_VALUE (p1);
tree list2 = TREE_VALUE (p2);
if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
return 0;
for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
{
tree parm1 = TREE_VEC_ELT (list1, i);
tree parm2 = TREE_VEC_ELT (list2, i);
if (!template_parameters_equivalent_p (parm1, parm2))
return false;
}
p1 = TREE_CHAIN (p1);
p2 = TREE_CHAIN (p2);
}
if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
return false;
return true;
}
/* Return true if the requires-clause of the template parameter lists are
equivalent and false otherwise. */
static bool
template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
{
tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
return false;
if (!cp_tree_equal (req1, req2))
return false;
return true;
}
/* Returns true if two template heads are equivalent. 17.6.6.1p6:
Two template heads are equivalent if their template parameter
lists are equivalent and their requires clauses are equivalent.
In pre-C++20, this is equivalent to calling comp_template_parms
for the template parameters of TMPL1 and TMPL2. */
bool
template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
{
tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
/* Don't change the matching rules for pre-C++20. */
if (cxx_dialect < cxx2a)
return comp_template_parms (parms1, parms2);
/* ... have the same number of template parameters, and their
corresponding parameters are equivalent. */
if (!template_parameter_lists_equivalent_p (parms1, parms2))
return false;
/* ... if either has a requires-clause, they both do and their
corresponding constraint-expressions are equivalent. */
return template_requirements_equivalent_p (parms1, parms2);
}
/* Determine whether PARM is a parameter pack. */
bool
template_parameter_pack_p (const_tree parm)
{
/* Determine if we have a non-type template parameter pack. */
if (TREE_CODE (parm) == PARM_DECL)
return (DECL_TEMPLATE_PARM_P (parm)
&& TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
return TEMPLATE_PARM_PARAMETER_PACK (parm);
/* If this is a list of template parameters, we could get a
TYPE_DECL or a TEMPLATE_DECL. */
if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
parm = TREE_TYPE (parm);
/* Otherwise it must be a type template parameter. */
return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
|| TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
&& TEMPLATE_TYPE_PARAMETER_PACK (parm));
}
/* Determine if T is a function parameter pack. */
bool
function_parameter_pack_p (const_tree t)
{
if (t && TREE_CODE (t) == PARM_DECL)
return DECL_PACK_P (t);
return false;
}
/* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
tree
get_function_template_decl (const_tree primary_func_tmpl_inst)
{
if (! primary_func_tmpl_inst
|| TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
|| ! primary_template_specialization_p (primary_func_tmpl_inst))
return NULL;
return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
}
/* Return true iff the function parameter PARAM_DECL was expanded
from the function parameter pack PACK. */
bool
function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
{
if (DECL_ARTIFICIAL (param_decl)
|| !function_parameter_pack_p (pack))
return false;
/* The parameter pack and its pack arguments have the same
DECL_PARM_INDEX. */
return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
}
/* Determine whether ARGS describes a variadic template args list,
i.e., one that is terminated by a template argument pack. */
static bool
template_args_variadic_p (tree args)
{
int nargs;
tree last_parm;
if (args == NULL_TREE)
return false;
args = INNERMOST_TEMPLATE_ARGS (args);
nargs = TREE_VEC_LENGTH (args);
if (nargs == 0)
return false;
last_parm = TREE_VEC_ELT (args, nargs - 1);
return ARGUMENT_PACK_P (last_parm);
}
/* Generate a new name for the parameter pack name NAME (an
IDENTIFIER_NODE) that incorporates its */
static tree
make_ith_pack_parameter_name (tree name, int i)
{
/* Munge the name to include the parameter index. */
#define NUMBUF_LEN 128
char numbuf[NUMBUF_LEN];
char* newname;
int newname_len;
if (name == NULL_TREE)
return name;
snprintf (numbuf, NUMBUF_LEN, "%i", i);
newname_len = IDENTIFIER_LENGTH (name)
+ strlen (numbuf) + 2;
newname = (char*)alloca (newname_len);
snprintf (newname, newname_len,
"%s#%i", IDENTIFIER_POINTER (name), i);
return get_identifier (newname);
}
/* Return true if T is a primary function, class or alias template
specialization, not including the template pattern. */
bool
primary_template_specialization_p (const_tree t)
{
if (!t)
return false;
if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
return (DECL_LANG_SPECIFIC (t)
&& DECL_USE_TEMPLATE (t)
&& DECL_TEMPLATE_INFO (t)
&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
return (CLASSTYPE_TEMPLATE_INFO (t)
&& CLASSTYPE_USE_TEMPLATE (t)
&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
else if (alias_template_specialization_p (t, nt_transparent))
return true;
return false;
}
/* Return true if PARM is a template template parameter. */
bool
template_template_parameter_p (const_tree parm)
{
return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
}
/* Return true iff PARM is a DECL representing a type template
parameter. */
bool
template_type_parameter_p (const_tree parm)
{
return (parm
&& (TREE_CODE (parm) == TYPE_DECL
|| TREE_CODE (parm) == TEMPLATE_DECL)
&& DECL_TEMPLATE_PARM_P (parm));
}
/* Return the template parameters of T if T is a
primary template instantiation, NULL otherwise. */
tree
get_primary_template_innermost_parameters (const_tree t)
{
tree parms = NULL, template_info = NULL;
if ((template_info = get_template_info (t))
&& primary_template_specialization_p (t))
parms = INNERMOST_TEMPLATE_PARMS
(DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
return parms;
}
/* Return the template parameters of the LEVELth level from the full list
of template parameters PARMS. */
tree
get_template_parms_at_level (tree parms, int level)
{
tree p;
if (!parms
|| TREE_CODE (parms) != TREE_LIST
|| level > TMPL_PARMS_DEPTH (parms))
return NULL_TREE;
for (p = parms; p; p = TREE_CHAIN (p))
if (TMPL_PARMS_DEPTH (p) == level)
return p;
return NULL_TREE;
}
/* Returns the template arguments of T if T is a template instantiation,
NULL otherwise. */
tree
get_template_innermost_arguments (const_tree t)
{
tree args = NULL, template_info = NULL;
if ((template_info = get_template_info (t))
&& TI_ARGS (template_info))
args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
return args;
}
/* Return the argument pack elements of T if T is a template argument pack,
NULL otherwise. */
tree
get_template_argument_pack_elems (const_tree t)
{
if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
&& TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
return NULL;
return ARGUMENT_PACK_ARGS (t);
}
/* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
ARGUMENT_PACK_SELECT represents. */
static tree
argument_pack_select_arg (tree t)
{
tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
/* If the selected argument is an expansion E, that most likely means we were
called from gen_elem_of_pack_expansion_instantiation during the
substituting of an argument pack (of which the Ith element is a pack
expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
In this case, the Ith element resulting from this substituting is going to
be a pack expansion, which pattern is the pattern of E. Let's return the
pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
resulting pack expansion from it. */
if (PACK_EXPANSION_P (arg))
{
/* Make sure we aren't throwing away arg info. */
gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
arg = PACK_EXPANSION_PATTERN (arg);
}
return arg;
}
/* True iff FN is a function representing a built-in variadic parameter
pack. */
bool
builtin_pack_fn_p (tree fn)
{
if (!fn
|| TREE_CODE (fn) != FUNCTION_DECL
|| !DECL_IS_BUILTIN (fn))
return false;
if (id_equal (DECL_NAME (fn), "__integer_pack"))
return true;
return false;
}
/* True iff CALL is a call to a function representing a built-in variadic
parameter pack. */
static bool
builtin_pack_call_p (tree call)
{
if (TREE_CODE (call) != CALL_EXPR)
return false;
return builtin_pack_fn_p (CALL_EXPR_FN (call));
}
/* Return a TREE_VEC for the expansion of __integer_pack(HI). */
static tree
expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
tree in_decl)
{
tree ohi = CALL_EXPR_ARG (call, 0);
tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
false/*fn*/, true/*int_cst*/);
if (value_dependent_expression_p (hi))
{
if (hi != ohi)
{
call = copy_node (call);
CALL_EXPR_ARG (call, 0) = hi;
}
tree ex = make_pack_expansion (call, complain);
tree vec = make_tree_vec (1);
TREE_VEC_ELT (vec, 0) = ex;
return vec;
}
else
{
hi = cxx_constant_value (hi);
int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
/* Calculate the largest value of len that won't make the size of the vec
overflow an int. The compiler will exceed resource limits long before
this, but it seems a decent place to diagnose. */
int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
if (len < 0 || len > max)
{
if ((complain & tf_error)
&& hi != error_mark_node)
error ("argument to %<__integer_pack%> must be between 0 and %d",
max);
return error_mark_node;
}
tree vec = make_tree_vec (len);
for (int i = 0; i < len; ++i)
TREE_VEC_ELT (vec, i) = size_int (i);
return vec;
}
}
/* Return a TREE_VEC for the expansion of built-in template parameter pack
CALL. */
static tree
expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
tree in_decl)
{
if (!builtin_pack_call_p (call))
return NULL_TREE;
tree fn = CALL_EXPR_FN (call);
if (id_equal (DECL_NAME (fn), "__integer_pack"))
return expand_integer_pack (call, args, complain, in_decl);
return NULL_TREE;
}
/* Structure used to track the progress of find_parameter_packs_r. */
struct find_parameter_pack_data
{
/* TREE_LIST that will contain all of the parameter packs found by
the traversal. */
tree* parameter_packs;
/* Set of AST nodes that have been visited by the traversal. */
hash_set<tree> *visited;
/* True iff we're making a type pack expansion. */
bool type_pack_expansion_p;
};
/* Identifies all of the argument packs that occur in a template
argument and appends them to the TREE_LIST inside DATA, which is a
find_parameter_pack_data structure. This is a subroutine of
make_pack_expansion and uses_parameter_packs. */
static tree
find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
{
tree t = *tp;
struct find_parameter_pack_data* ppd =
(struct find_parameter_pack_data*)data;
bool parameter_pack_p = false;
/* Don't look through typedefs; we are interested in whether a
parameter pack is actually written in the expression/type we're
looking at, not the target type. */
if (TYPE_P (t) && typedef_variant_p (t))
{
/* But do look at arguments for an alias template. */
if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
cp_walk_tree (&TI_ARGS (tinfo),
&find_parameter_packs_r,
ppd, ppd->visited);
*walk_subtrees = 0;
return NULL_TREE;
}
/* Identify whether this is a parameter pack or not. */
switch (TREE_CODE (t))
{
case TEMPLATE_PARM_INDEX:
if (TEMPLATE_PARM_PARAMETER_PACK (t))
parameter_pack_p = true;
break;
case TEMPLATE_TYPE_PARM:
t = TYPE_MAIN_VARIANT (t);
/* FALLTHRU */
case TEMPLATE_TEMPLATE_PARM:
/* If the placeholder appears in the decl-specifier-seq of a function
parameter pack (14.6.3), or the type-specifier-seq of a type-id that
is a pack expansion, the invented template parameter is a template
parameter pack. */
if (ppd->type_pack_expansion_p && is_auto (t))
TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
if (TEMPLATE_TYPE_PARAMETER_PACK (t))
parameter_pack_p = true;
break;
case FIELD_DECL:
case PARM_DECL:
if (DECL_PACK_P (t))
{
/* We don't want to walk into the type of a PARM_DECL,
because we don't want to see the type parameter pack. */
*walk_subtrees = 0;
parameter_pack_p = true;
}
break;
case VAR_DECL:
if (DECL_PACK_P (t))
{
/* We don't want to walk into the type of a variadic capture proxy,
because we don't want to see the type parameter pack. */
*walk_subtrees = 0;
parameter_pack_p = true;
}
else if (variable_template_specialization_p (t))
{
cp_walk_tree (&DECL_TI_ARGS (t),
find_parameter_packs_r,
ppd, ppd->visited);
*walk_subtrees = 0;
}
break;
case CALL_EXPR:
if (builtin_pack_call_p (t))
parameter_pack_p = true;
break;
case BASES:
parameter_pack_p = true;
break;
default:
/* Not a parameter pack. */
break;
}
if (parameter_pack_p)
{
/* Add this parameter pack to the list. */
*ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
}
if (TYPE_P (t))
cp_walk_tree (&TYPE_CONTEXT (t),
&find_parameter_packs_r, ppd, ppd->visited);
/* This switch statement will return immediately if we don't find a
parameter pack. ??? Should some of these be in cp_walk_subtrees? */
switch (TREE_CODE (t))
{
case BOUND_TEMPLATE_TEMPLATE_PARM:
/* Check the template itself. */
cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
&find_parameter_packs_r, ppd, ppd->visited);
return NULL_TREE;
case DECL_EXPR:
{
tree decl = DECL_EXPR_DECL (t);
/* Ignore the declaration of a capture proxy for a parameter pack. */
if (is_capture_proxy (decl))
*walk_subtrees = 0;
if (is_typedef_decl (decl))
/* Since we stop at typedefs above, we need to look through them at
the point of the DECL_EXPR. */
cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
&find_parameter_packs_r, ppd, ppd->visited);
return NULL_TREE;
}
case TEMPLATE_DECL:
if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
return NULL_TREE;
cp_walk_tree (&TREE_TYPE (t),
&find_parameter_packs_r, ppd, ppd->visited);
return NULL_TREE;
case TYPENAME_TYPE:
cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
ppd, ppd->visited);
*walk_subtrees = 0;
return NULL_TREE;
case TYPE_PACK_EXPANSION:
case EXPR_PACK_EXPANSION:
*walk_subtrees = 0;
return NULL_TREE;
case INTEGER_TYPE:
cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
ppd, ppd->visited);
*walk_subtrees = 0;
return NULL_TREE;
case IDENTIFIER_NODE:
cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
ppd->visited);
*walk_subtrees = 0;
return NULL_TREE;
case LAMBDA_EXPR:
{
/* Look at explicit captures. */
for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
cap; cap = TREE_CHAIN (cap))
cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
ppd->visited);
/* Since we defer implicit capture, look in the parms and body. */
tree fn = lambda_function (t);
cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
ppd->visited);
cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
ppd->visited);
*walk_subtrees = 0;
return NULL_TREE;
}
case DECLTYPE_TYPE:
{
/* When traversing a DECLTYPE_TYPE_EXPR, we need to set
type_pack_expansion_p to false so that any placeholders
within the expression don't get marked as parameter packs. */
bool type_pack_expansion_p = ppd->type_pack_expansion_p;
ppd->type_pack_expansion_p = false;
cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
ppd, ppd->visited);
ppd->type_pack_expansion_p = type_pack_expansion_p;
*walk_subtrees = 0;
return NULL_TREE;
}
case IF_STMT:
cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
ppd, ppd->visited);
cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
ppd, ppd->visited);
cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
ppd, ppd->visited);
/* Don't walk into IF_STMT_EXTRA_ARGS. */
*walk_subtrees = 0;
return NULL_TREE;
default:
return NULL_TREE;
}
return NULL_TREE;
}
/* Determines if the expression or type T uses any parameter packs. */
tree
uses_parameter_packs (tree t)
{
tree parameter_packs = NULL_TREE;
struct find_parameter_pack_data ppd;
ppd.parameter_packs = ¶meter_packs;
ppd.visited = new hash_set<tree>;
ppd.type_pack_expansion_p = false;
cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
delete ppd.visited;
return parameter_packs;
}
/* Turn ARG, which may be an expression, type, or a TREE_LIST
representation a base-class initializer into a parameter pack
expansion. If all goes well, the resulting node will be an
EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
respectively. */
tree
make_pack_expansion (tree arg, tsubst_flags_t complain)
{
tree result;
tree parameter_packs = NULL_TREE;
bool for_types = false;
struct find_parameter_pack_data ppd;
if (!arg || arg == error_mark_node)
return arg;
if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
{
/* A TREE_LIST with a non-null TREE_PURPOSE is for a base
class initializer. In this case, the TREE_PURPOSE will be a
_TYPE node (representing the base class expansion we're
initializing) and the TREE_VALUE will be a TREE_LIST
containing the initialization arguments.
The resulting expansion looks somewhat different from most
expansions. Rather than returning just one _EXPANSION, we
return a TREE_LIST whose TREE_PURPOSE is a
TYPE_PACK_EXPANSION containing the bases that will be
initialized. The TREE_VALUE will be identical to the
original TREE_VALUE, which is a list of arguments that will
be passed to each base. We do not introduce any new pack
expansion nodes into the TREE_VALUE (although it is possible
that some already exist), because the TREE_PURPOSE and
TREE_VALUE all need to be expanded together with the same
_EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
resulting TREE_PURPOSE will mention the parameter packs in
both the bases and the arguments to the bases. */
tree purpose;
tree value;
tree parameter_packs = NULL_TREE;
/* Determine which parameter packs will be used by the base
class expansion. */
ppd.visited = new hash_set<tree>;
ppd.parameter_packs = ¶meter_packs;
ppd.type_pack_expansion_p = false;
gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
&ppd, ppd.visited);
if (parameter_packs == NULL_TREE)
{
if (complain & tf_error)
error ("base initializer expansion %qT contains no parameter packs",
arg);
delete ppd.visited;
return error_mark_node;
}
if (TREE_VALUE (arg) != void_type_node)
{
/* Collect the sets of parameter packs used in each of the
initialization arguments. */
for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
{
/* Determine which parameter packs will be expanded in this
argument. */
cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
&ppd, ppd.visited);
}
}
delete ppd.visited;
/* Create the pack expansion type for the base type. */
purpose = cxx_make_type (TYPE_PACK_EXPANSION);
SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
/* Just use structural equality for these TYPE_PACK_EXPANSIONS;
they will rarely be compared to anything. */
SET_TYPE_STRUCTURAL_EQUALITY (purpose);
return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
}
if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
for_types = true;
/* Build the PACK_EXPANSION_* node. */
result = for_types
? cxx_make_type (TYPE_PACK_EXPANSION)
: make_node (EXPR_PACK_EXPANSION);
SET_PACK_EXPANSION_PATTERN (result, arg);
if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
{
/* Propagate type and const-expression information. */
TREE_TYPE (result) = TREE_TYPE (arg);
TREE_CONSTANT (result) = TREE_CONSTANT (arg);
/* Mark this read now, since the expansion might be length 0. */
mark_exp_read (arg);
}
else
/* Just use structural equality for these TYPE_PACK_EXPANSIONS;
they will rarely be compared to anything. */
SET_TYPE_STRUCTURAL_EQUALITY (result);
/* Determine which parameter packs will be expanded. */
ppd.parameter_packs = ¶meter_packs;
ppd.visited = new hash_set<tree>;
ppd.type_pack_expansion_p = TYPE_P (arg);
cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
delete ppd.visited;
/* Make sure we found some parameter packs. */
if (parameter_packs == NULL_TREE)
{
if (complain & tf_error)
{
if (TYPE_P (arg))
error ("expansion pattern %qT contains no parameter packs", arg);
else
error ("expansion pattern %qE contains no parameter packs", arg);
}
return error_mark_node;
}
PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
return result;
}
/* Checks T for any "bare" parameter packs, which have not yet been
expanded, and issues an error if any are found. This operation can
only be done on full expressions or types (e.g., an expression
statement, "if" condition, etc.), because we could have expressions like:
foo(f(g(h(args)))...)
where "args" is a parameter pack. check_for_bare_parameter_packs
should not be called for the subexpressions args, h(args),
g(h(args)), or f(g(h(args))), because we would produce erroneous
error messages.
Returns TRUE and emits an error if there were bare parameter packs,
returns FALSE otherwise. */
bool
check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
{
tree parameter_packs = NULL_TREE;
struct find_parameter_pack_data ppd;
if (!processing_template_decl || !t || t == error_mark_node)
return false;
/* A lambda might use a parameter pack from the containing context. */
if (current_class_type && LAMBDA_TYPE_P (current_class_type)
&& CLASSTYPE_TEMPLATE_INFO (current_class_type))
return false;
if (TREE_CODE (t) == TYPE_DECL)
t = TREE_TYPE (t);
ppd.parameter_packs = ¶meter_packs;
ppd.visited = new hash_set<tree>;
ppd.type_pack_expansion_p = false;
cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
delete ppd.visited;
if (parameter_packs)
{
if (loc == UNKNOWN_LOCATION)
loc = cp_expr_loc_or_input_loc (t);
error_at (loc, "parameter packs not expanded with %<...%>:");
while (parameter_packs)
{
tree pack = TREE_VALUE (parameter_packs);
tree name = NULL_TREE;
if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
|| TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
name = TYPE_NAME (pack);
else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
else if (TREE_CODE (pack) == CALL_EXPR)
name = DECL_NAME (CALL_EXPR_FN (pack));
else
name = DECL_NAME (pack);
if (name)
inform (loc, " %qD", name);
else
inform (loc, " %s", "<anonymous>");
parameter_packs = TREE_CHAIN (parameter_packs);
}
return true;
}
return false;
}
/* Expand any parameter packs that occur in the template arguments in
ARGS. */
tree
expand_template_argument_pack (tree args)
{
if (args == error_mark_node)
return error_mark_node;
tree result_args = NULL_TREE;
int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
int num_result_args = -1;
int non_default_args_count = -1;
/* First, determine if we need to expand anything, and the number of
slots we'll need. */
for (in_arg = 0; in_arg < nargs; ++in_arg)
{
tree arg = TREE_VEC_ELT (args, in_arg);
if (arg == NULL_TREE)
return args;
if (ARGUMENT_PACK_P (arg))
{
int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
if (num_result_args < 0)
num_result_args = in_arg + num_packed;
else
num_result_args += num_packed;
}
else
{
if (num_result_args >= 0)
num_result_args++;
}
}
/* If no expansion is necessary, we're done. */
if (num_result_args < 0)
return args;
/* Expand arguments. */
result_args = make_tree_vec (num_result_args);
if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
non_default_args_count =
GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
for (in_arg = 0; in_arg < nargs; ++in_arg)
{
tree arg = TREE_VEC_ELT (args, in_arg);
if (ARGUMENT_PACK_P (arg))
{
tree packed = ARGUMENT_PACK_ARGS (arg);
int i, num_packed = TREE_VEC_LENGTH (packed);
for (i = 0; i < num_packed; ++i, ++out_arg)
TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
if (non_default_args_count > 0)
non_default_args_count += num_packed - 1;
}
else
{
TREE_VEC_ELT (result_args, out_arg) = arg;
++out_arg;
}
}
if (non_default_args_count >= 0)
SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
return result_args;
}
/* Checks if DECL shadows a template parameter.
[temp.local]: A template-parameter shall not be redeclared within its
scope (including nested scopes).
Emits an error and returns TRUE if the DECL shadows a parameter,
returns FALSE otherwise. */
bool
check_template_shadow (tree decl)
{
tree olddecl;
/* If we're not in a template, we can't possibly shadow a template
parameter. */
if (!current_template_parms)
return true;
/* Figure out what we're shadowing. */
decl = OVL_FIRST (decl);
olddecl = innermost_non_namespace_value (DECL_NAME (decl));
/* If there's no previous binding for this name, we're not shadowing
anything, let alone a template parameter. */
if (!olddecl)
return true;
/* If we're not shadowing a template parameter, we're done. Note
that OLDDECL might be an OVERLOAD (or perhaps even an
ERROR_MARK), so we can't just blithely assume it to be a _DECL
node. */
if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
return true;
/* We check for decl != olddecl to avoid bogus errors for using a
name inside a class. We check TPFI to avoid duplicate errors for
inline member templates. */
if (decl == olddecl
|| (DECL_TEMPLATE_PARM_P (decl)
&& TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
return true;
/* Don't complain about the injected class name, as we've already
complained about the class itself. */
if (DECL_SELF_REFERENCE_P (decl))
return false;
if (DECL_TEMPLATE_PARM_P (decl))
error ("declaration of template parameter %q+D shadows "
"template parameter", decl);
else
error ("declaration of %q+#D shadows template parameter", decl);
inform (DECL_SOURCE_LOCATION (olddecl),
"template parameter %qD declared here", olddecl);
return false;
}
/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
ORIG_LEVEL, DECL, and TYPE. */
static tree
build_template_parm_index (int index,
int level,
int orig_level,
tree decl,
tree type)
{
tree t = make_node (TEMPLATE_PARM_INDEX);
TEMPLATE_PARM_IDX (t) = index;
TEMPLATE_PARM_LEVEL (t) = level;
TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
TEMPLATE_PARM_DECL (t) = decl;
TREE_TYPE (t) = type;
TREE_CONSTANT (t) = TREE_CONSTANT (decl);
TREE_READONLY (t) = TREE_READONLY (decl);
return t;
}
/* Find the canonical type parameter for the given template type
parameter. Returns the canonical type parameter, which may be TYPE
if no such parameter existed. */
static tree
canonical_type_parameter (tree type)
{
tree list;
int idx = TEMPLATE_TYPE_IDX (type);
gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
if (!canonical_template_parms)
vec_alloc (canonical_template_parms, idx + 1);
if (canonical_template_parms->length () <= (unsigned) idx)
vec_safe_grow_cleared (canonical_template_parms, idx + 1);
list = (*canonical_template_parms)[idx];
while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
list = TREE_CHAIN (list);
if (list)
return TREE_VALUE (list);
else
{
(*canonical_template_parms)[idx]
= tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
return type;
}
}
/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
new one is created. */
static tree
reduce_template_parm_level (tree index, tree type, int levels, tree args,
tsubst_flags_t complain)
{
if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
|| (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
!= TEMPLATE_PARM_LEVEL (index) - levels)
|| !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
{
tree orig_decl = TEMPLATE_PARM_DECL (index);
tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
TREE_CODE (orig_decl), DECL_NAME (orig_decl),
type);
TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
TREE_READONLY (decl) = TREE_READONLY (orig_decl);
DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
DECL_ARTIFICIAL (decl) = 1;
SET_DECL_TEMPLATE_PARM_P (decl);
tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
TEMPLATE_PARM_LEVEL (index) - levels,
TEMPLATE_PARM_ORIG_LEVEL (index),
decl, type);
TEMPLATE_PARM_DESCENDANTS (index) = tpi;
TEMPLATE_PARM_PARAMETER_PACK (tpi)
= TEMPLATE_PARM_PARAMETER_PACK (index);
/* Template template parameters need this. */
tree inner = decl;
if (TREE_CODE (decl) == TEMPLATE_DECL)
{
inner = build_decl (DECL_SOURCE_LOCATION (decl),
TYPE_DECL, DECL_NAME (decl), type);
DECL_TEMPLATE_RESULT (decl) = inner;
DECL_ARTIFICIAL (inner) = true;
DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
(DECL_TEMPLATE_PARMS (orig_decl), args, complain);
}
/* Attach the TPI to the decl. */
if (TREE_CODE (inner) == TYPE_DECL)
TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
else
DECL_INITIAL (decl) = tpi;
}
return TEMPLATE_PARM_DESCENDANTS (index);
}
/* Process information from new template parameter PARM and append it
to the LIST being built. This new parameter is a non-type
parameter iff IS_NON_TYPE is true. This new parameter is a
parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
is in PARM_LOC. */
tree
process_template_parm (tree list, location_t parm_loc, tree parm,
bool is_non_type, bool is_parameter_pack)
{
tree decl = 0;
int idx = 0;
gcc_assert (TREE_CODE (parm) == TREE_LIST);
tree defval = TREE_PURPOSE (parm);
tree constr = TREE_TYPE (parm);
if (list)
{
tree p = tree_last (list);
if (p && TREE_VALUE (p) != error_mark_node)
{
p = TREE_VALUE (p);
if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
else
idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
}
++idx;
}
if (is_non_type)
{
parm = TREE_VALUE (parm);
SET_DECL_TEMPLATE_PARM_P (parm);
if (TREE_TYPE (parm) != error_mark_node)
{
/* [temp.param]
The top-level cv-qualifiers on the template-parameter are
ignored when determining its type. */
TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
TREE_TYPE (parm) = error_mark_node;
else if (uses_parameter_packs (TREE_TYPE (parm))
&& !is_parameter_pack
/* If we're in a nested template parameter list, the template
template parameter could be a parameter pack. */
&& processing_template_parmlist == 1)
{
/* This template parameter is not a parameter pack, but it
should be. Complain about "bare" parameter packs. */
check_for_bare_parameter_packs (TREE_TYPE (parm));
/* Recover by calling this a parameter pack. */
is_parameter_pack = true;
}
}
/* A template parameter is not modifiable. */
TREE_CONSTANT (parm) = 1;
TREE_READONLY (parm) = 1;
decl = build_decl (parm_loc,
CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
TREE_CONSTANT (decl) = 1;
TREE_READONLY (decl) = 1;
DECL_INITIAL (parm) = DECL_INITIAL (decl)
= build_template_parm_index (idx, processing_template_decl,
processing_template_decl,
decl, TREE_TYPE (parm));
TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
= is_parameter_pack;
}
else
{
tree t;
parm = TREE_VALUE (TREE_VALUE (parm));
if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
{
t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
/* This is for distinguishing between real templates and template
template parameters */
TREE_TYPE (parm) = t;
/* any_template_parm_r expects to be able to get the targs of a
DECL_TEMPLATE_RESULT. */
tree result = DECL_TEMPLATE_RESULT (parm);
TREE_TYPE (result) = t;
tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
tree tinfo = build_template_info (parm, args);
retrofit_lang_decl (result);
DECL_TEMPLATE_INFO (result) = tinfo;
decl = parm;
}
else
{
t = cxx_make_type (TEMPLATE_TYPE_PARM);
/* parm is either IDENTIFIER_NODE or NULL_TREE. */
decl = build_decl (parm_loc,
TYPE_DECL, parm, t);
}
TYPE_NAME (t) = decl;
TYPE_STUB_DECL (t) = decl;
parm = decl;
TEMPLATE_TYPE_PARM_INDEX (t)
= build_template_parm_index (idx, processing_template_decl,
processing_template_decl,
decl, TREE_TYPE (parm));
TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
SET_TYPE_STRUCTURAL_EQUALITY (t);
else
TYPE_CANONICAL (t) = canonical_type_parameter (t);
}
DECL_ARTIFICIAL (decl) = 1;
SET_DECL_TEMPLATE_PARM_P (decl);
/* Build requirements for the type/template parameter.
This must be done after SET_DECL_TEMPLATE_PARM_P or
process_template_parm could fail. */
tree reqs = finish_shorthand_constraint (parm, constr);
decl = pushdecl (decl);
if (!is_non_type)
parm = decl;
/* Build the parameter node linking the parameter declaration,
its default argument (if any), and its constraints (if any). */
parm = build_tree_list (defval, parm);
TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
return chainon (list, parm);
}
/* The end of a template parameter list has been reached. Process the
tree list into a parameter vector, converting each parameter into a more
useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
as PARM_DECLs. */
tree
end_template_parm_list (tree parms)
{
int nparms;
tree parm, next;
tree saved_parmlist = make_tree_vec (list_length (parms));
/* Pop the dummy parameter level and add the real one. */
current_template_parms = TREE_CHAIN (current_template_parms);
current_template_parms
= tree_cons (size_int (processing_template_decl),
saved_parmlist, current_template_parms);
for (parm = parms, nparms = 0; parm; parm = next, nparms++)
{
next = TREE_CHAIN (parm);
TREE_VEC_ELT (saved_parmlist, nparms) = parm;
TREE_CHAIN (parm) = NULL_TREE;
}
--processing_template_parmlist;
return saved_parmlist;
}
// Explicitly indicate the end of the template parameter list. We assume
// that the current template parameters have been constructed and/or
// managed explicitly, as when creating new template template parameters
// from a shorthand constraint.
void
end_template_parm_list ()
{
--processing_template_parmlist;
}
/* end_template_decl is called after a template declaration is seen. */
void
end_template_decl (void)
{
reset_specialization ();
if (! processing_template_decl)
return;
/* This matches the pushlevel in begin_template_parm_list. */
finish_scope ();
--processing_template_decl;
current_template_parms = TREE_CHAIN (current_template_parms);
}
/* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
thereof, and converts it into an argument suitable to be passed to
the type substitution functions. Note that if the TREE_LIST contains
an error_mark node, the returned argument is error_mark_node. */
tree
template_parm_to_arg (tree t)
{
if (!t)
return NULL_TREE;
if (TREE_CODE (t) == TREE_LIST)
t = TREE_VALUE (t);
if (error_operand_p (t))
return error_mark_node;
if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
{
if (TREE_CODE (t) == TYPE_DECL
|| TREE_CODE (t) == TEMPLATE_DECL)
t = TREE_TYPE (t);
else
t = DECL_INITIAL (t);
}
gcc_assert (TEMPLATE_PARM_P (t));
if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
|| TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
{
if (TEMPLATE_TYPE_PARAMETER_PACK (t))
{
/* Turn this argument into a TYPE_ARGUMENT_PACK
with a single element, which expands T. */
tree vec = make_tree_vec (1);
if (CHECKING_P)
SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
t = cxx_make_type (TYPE_ARGUMENT_PACK);
SET_ARGUMENT_PACK_ARGS (t, vec);
}
}
else
{
if (TEMPLATE_PARM_PARAMETER_PACK (t))
{
/* Turn this argument into a NONTYPE_ARGUMENT_PACK
with a single element, which expands T. */
tree vec = make_tree_vec (1);
if (CHECKING_P)
SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
t = convert_from_reference (t);
TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
t = make_node (NONTYPE_ARGUMENT_PACK);
SET_ARGUMENT_PACK_ARGS (t, vec);
}
else
t = convert_from_reference (t);
}
return t;
}
/* Given a single level of template parameters (a TREE_VEC), return it
as a set of template arguments. */
tree
template_parms_level_to_args (tree parms)
{
tree a = copy_node (parms);
TREE_TYPE (a) = NULL_TREE;
for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
if (CHECKING_P)
SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
return a;
}
/* Given a set of template parameters, return them as a set of template
arguments. The template parameters are represented as a TREE_VEC, in
the form documented in cp-tree.h for template arguments. */
tree
template_parms_to_args (tree parms)
{
tree header;
tree args = NULL_TREE;
int length = TMPL_PARMS_DEPTH (parms);
int l = length;
/* If there is only one level of template parameters, we do not
create a TREE_VEC of TREE_VECs. Instead, we return a single
TREE_VEC containing the arguments. */
if (length > 1)
args = make_tree_vec (length);
for (header = parms; header; header = TREE_CHAIN (header))
{
tree a = template_parms_level_to_args (TREE_VALUE (header));
if (length > 1)
TREE_VEC_ELT (args, --l) = a;
else
args = a;
}
return args;
}
/* Within the declaration of a template, return the currently active
template parameters as an argument TREE_VEC. */
static tree
current_template_args (void)
{
return template_parms_to_args (current_template_parms);
}
/* Return the fully generic arguments for of TMPL, i.e. what
current_template_args would be while parsing it. */
tree
generic_targs_for (tree tmpl)
{
if (tmpl == NULL_TREE)
return NULL_TREE;
if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
|| DECL_TEMPLATE_SPECIALIZATION (tmpl))
/* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
template parameter, it has no TEMPLATE_INFO; for a partial
specialization, it has the arguments for the primary template, and we
want the arguments for the partial specialization. */;
else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
if (tree ti = get_template_info (result))
return TI_ARGS (ti);
return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
}
/* Update the declared TYPE by doing any lookups which were thought to be
dependent, but are not now that we know the SCOPE of the declarator. */
tree
maybe_update_decl_type (tree orig_type, tree scope)
{
tree type = orig_type;
if (type == NULL_TREE)
return type;
if (TREE_CODE (orig_type) == TYPE_DECL)
type = TREE_TYPE (type);
if (scope && TYPE_P (scope) && dependent_type_p (scope)
&& dependent_type_p (type)
/* Don't bother building up the args in this case. */
&& TREE_CODE (type) != TEMPLATE_TYPE_PARM)
{
/* tsubst in the args corresponding to the template parameters,
including auto if present. Most things will be unchanged, but
make_typename_type and tsubst_qualified_id will resolve
TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
tree args = current_template_args ();
tree auto_node = type_uses_auto (type);
tree pushed;
if (auto_node)
{
tree auto_vec = make_tree_vec (1);
TREE_VEC_ELT (auto_vec, 0) = auto_node;
args = add_to_template_args (args, auto_vec);
}
pushed = push_scope (scope);
type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
if (pushed)
pop_scope (scope);
}
if (type == error_mark_node)
return orig_type;
if (TREE_CODE (orig_type) == TYPE_DECL)
{
if (same_type_p (type, TREE_TYPE (orig_type)))
type = orig_type;
else
type = TYPE_NAME (type);
}
return type;
}
/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
the new template is a member template. */
static tree
build_template_decl (tree decl, tree parms, bool member_template_p)
{
tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
DECL_TEMPLATE_PARMS (tmpl) = parms;
DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
return tmpl;
}
struct template_parm_data
{
/* The level of the template parameters we are currently
processing. */
int level;
/* The index of the specialization argument we are currently
processing. */
int current_arg;
/* An array whose size is the number of template parameters. The
elements are nonzero if the parameter has been used in any one
of the arguments processed so far. */
int* parms;
/* An array whose size is the number of template arguments. The
elements are nonzero if the argument makes use of template
parameters of this level. */
int* arg_uses_template_parms;
};
/* Subroutine of push_template_decl used to see if each template
parameter in a partial specialization is used in the explicit
argument list. If T is of the LEVEL given in DATA (which is
treated as a template_parm_data*), then DATA->PARMS is marked
appropriately. */
static int
mark_template_parm (tree t, void* data)
{
int level;
int idx;
struct template_parm_data* tpd = (struct template_parm_data*) data;
template_parm_level_and_index (t, &level, &idx);
if (level == tpd->level)
{
tpd->parms[idx] = 1;
tpd->arg_uses_template_parms[tpd->current_arg] = 1;
}
/* In C++17 the type of a non-type argument is a deduced context. */
if (cxx_dialect >= cxx17
&& TREE_CODE (t) == TEMPLATE_PARM_INDEX)
for_each_template_parm (TREE_TYPE (t),
&mark_template_parm,
data,
NULL,
/*include_nondeduced_p=*/false);
/* Return zero so that for_each_template_parm will continue the
traversal of the tree; we want to mark *every* template parm. */
return 0;
}
/* Process the partial specialization DECL. */
static tree
process_partial_specialization (tree decl)
{
tree type = TREE_TYPE (decl);
tree tinfo = get_template_info (decl);
tree maintmpl = TI_TEMPLATE (tinfo);
tree specargs = TI_ARGS (tinfo);
tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
tree inner_parms;
tree inst;
int nargs = TREE_VEC_LENGTH (inner_args);
int ntparms;
int i;
bool did_error_intro = false;
struct template_parm_data tpd;
struct template_parm_data tpd2;
gcc_assert (current_template_parms);
/* A concept cannot be specialized. */
if (flag_concepts && variable_concept_p (maintmpl))
{
error ("specialization of variable concept %q#D", maintmpl);
return error_mark_node;
}
inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
ntparms = TREE_VEC_LENGTH (inner_parms);
/* We check that each of the template parameters given in the
partial specialization is used in the argument list to the
specialization. For example:
template <class T> struct S;
template <class T> struct S<T*>;
The second declaration is OK because `T*' uses the template
parameter T, whereas
template <class T> struct S<int>;
is no good. Even trickier is:
template <class T>
struct S1
{
template <class U>
struct S2;
template <class U>
struct S2<T>;
};
The S2<T> declaration is actually invalid; it is a
full-specialization. Of course,
template <class U>
struct S2<T (*)(U)>;
or some such would have been OK. */
tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
tpd.parms = XALLOCAVEC (int, ntparms);
memset (tpd.parms, 0, sizeof (int) * ntparms);
tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
for (i = 0; i < nargs; ++i)
{
tpd.current_arg = i;
for_each_template_parm (TREE_VEC_ELT (inner_args, i),
&mark_template_parm,
&tpd,
NULL,
/*include_nondeduced_p=*/false);
}
for (i = 0; i < ntparms; ++i)
if (tpd.parms[i] == 0)
{
/* One of the template parms was not used in a deduced context in the
specialization. */
if (!did_error_intro)
{
error ("template parameters not deducible in "
"partial specialization:");
did_error_intro = true;
}
inform (input_location, " %qD",
TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
}
if (did_error_intro)
return error_mark_node;
/* [temp.class.spec]
The argument list of the specialization shall not be identical to
the implicit argument list of the primary template. */
tree main_args
= TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
&& (!flag_concepts
|| !strictly_subsumes (current_template_constraints (),
main_args, maintmpl)))
{
if (!flag_concepts)
error ("partial specialization %q+D does not specialize "
"any template arguments; to define the primary template, "
"remove the template argument list", decl);
else
error ("partial specialization %q+D does not specialize any "
"template arguments and is not more constrained than "
"the primary template; to define the primary template, "
"remove the template argument list", decl);
inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
}
/* A partial specialization that replaces multiple parameters of the
primary template with a pack expansion is less specialized for those
parameters. */
if (nargs < DECL_NTPARMS (maintmpl))
{
error ("partial specialization is not more specialized than the "
"primary template because it replaces multiple parameters "
"with a pack expansion");
inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
/* Avoid crash in process_partial_specialization. */
return decl;
}
else if (nargs > DECL_NTPARMS (maintmpl))
{
error ("too many arguments for partial specialization %qT", type);
inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
/* Avoid crash below. */
return decl;
}
/* If we aren't in a dependent class, we can actually try deduction. */
else if (tpd.level == 1
/* FIXME we should be able to handle a partial specialization of a
partial instantiation, but currently we can't (c++/41727). */
&& TMPL_ARGS_DEPTH (specargs) == 1
&& !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
{
auto_diagnostic_group d;
if (permerror (input_location, "partial specialization %qD is not "
"more specialized than", decl))
inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
maintmpl);
}
/* [temp.class.spec]
A partially specialized non-type argument expression shall not
involve template parameters of the partial specialization except
when the argument expression is a simple identifier.
The type of a template parameter corresponding to a specialized
non-type argument shall not be dependent on a parameter of the
specialization.
Also, we verify that pack expansions only occur at the
end of the argument list. */
tpd2.parms = 0;
for (i = 0; i < nargs; ++i)
{
tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
tree arg = TREE_VEC_ELT (inner_args, i);
tree packed_args = NULL_TREE;
int j, len = 1;
if (ARGUMENT_PACK_P (arg))
{
/* Extract the arguments from the argument pack. We'll be
iterating over these in the following loop. */
packed_args = ARGUMENT_PACK_ARGS (arg);
len = TREE_VEC_LENGTH (packed_args);
}
for (j = 0; j < len; j++)
{
if (packed_args)
/* Get the Jth argument in the parameter pack. */
arg = TREE_VEC_ELT (packed_args, j);
if (PACK_EXPANSION_P (arg))
{
/* Pack expansions must come at the end of the
argument list. */
if ((packed_args && j < len - 1)
|| (!packed_args && i < nargs - 1))
{
if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
error ("parameter pack argument %qE must be at the "
"end of the template argument list", arg);
else
error ("parameter pack argument %qT must be at the "
"end of the template argument list", arg);
}
}
if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
/* We only care about the pattern. */
arg = PACK_EXPANSION_PATTERN (arg);
if (/* These first two lines are the `non-type' bit. */
!TYPE_P (arg)
&& TREE_CODE (arg) != TEMPLATE_DECL
/* This next two lines are the `argument expression is not just a
simple identifier' condition and also the `specialized
non-type argument' bit. */
&& TREE_CODE (arg) != TEMPLATE_PARM_INDEX
&& !((REFERENCE_REF_P (arg)
|| TREE_CODE (arg) == VIEW_CONVERT_EXPR)
&& TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
{
if ((!packed_args && tpd.arg_uses_template_parms[i])
|| (packed_args && uses_template_parms (arg)))
error_at (cp_expr_loc_or_input_loc (arg),
"template argument %qE involves template "
"parameter(s)", arg);
else
{
/* Look at the corresponding template parameter,
marking which template parameters its type depends
upon. */
tree type = TREE_TYPE (parm);
if (!tpd2.parms)
{
/* We haven't yet initialized TPD2. Do so now. */
tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
/* The number of parameters here is the number in the
main template, which, as checked in the assertion
above, is NARGS. */
tpd2.parms = XALLOCAVEC (int, nargs);
tpd2.level =
TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
}
/* Mark the template parameters. But this time, we're
looking for the template parameters of the main
template, not in the specialization. */
tpd2.current_arg = i;
tpd2.arg_uses_template_parms[i] = 0;
memset (tpd2.parms, 0, sizeof (int) * nargs);
for_each_template_parm (type,
&mark_template_parm,
&tpd2,
NULL,
/*include_nondeduced_p=*/false);
if (tpd2.arg_uses_template_parms [i])
{
/* The type depended on some template parameters.
If they are fully specialized in the
specialization, that's OK. */
int j;
int count = 0;
for (j = 0; j < nargs; ++j)
if (tpd2.parms[j] != 0
&& tpd.arg_uses_template_parms [j])
++count;
if (count != 0)
error_n (input_location, count,
"type %qT of template argument %qE depends "
"on a template parameter",
"type %qT of template argument %qE depends "
"on template parameters",
type,
arg);
}
}
}
}
}
/* We should only get here once. */
if (TREE_CODE (decl) == TYPE_DECL)
gcc_assert (!COMPLETE_TYPE_P (type));
// Build the template decl.
tree tmpl = build_template_decl (decl, current_template_parms,
DECL_MEMBER_TEMPLATE_P (maintmpl));
TREE_TYPE (tmpl) = type;
DECL_TEMPLATE_RESULT (tmpl) = decl;
SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
/* Give template template parms a DECL_CONTEXT of the template
for which they are a parameter. */
for (i = 0; i < ntparms; ++i)
{
tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
if (TREE_CODE (parm) == TEMPLATE_DECL)
DECL_CONTEXT (parm) = tmpl;
}
if (VAR_P (decl))
/* We didn't register this in check_explicit_specialization so we could
wait until the constraints were set. */
decl = register_specialization (decl, maintmpl, specargs, false, 0);
else
associate_classtype_constraints (type);
DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
= tree_cons (specargs, tmpl,
DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
inst = TREE_CHAIN (inst))
{
tree instance = TREE_VALUE (inst);
if (TYPE_P (instance)
? (COMPLETE_TYPE_P (instance)
&& CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
: DECL_TEMPLATE_INSTANTIATION (instance))
{
tree spec = most_specialized_partial_spec (instance, tf_none);
tree inst_decl = (DECL_P (instance)
? instance : TYPE_NAME (instance));
if (!spec)
/* OK */;
else if (spec == error_mark_node)
permerror (input_location,
"declaration of %qD ambiguates earlier template "
"instantiation for %qD", decl, inst_decl);
else if (TREE_VALUE (spec) == tmpl)
permerror (input_location,
"partial specialization of %qD after instantiation "
"of %qD", decl, inst_decl);
}
}
return decl;
}
/* PARM is a template parameter of some form; return the corresponding
TEMPLATE_PARM_INDEX. */
static tree
get_template_parm_index (tree parm)
{
if (TREE_CODE (parm) == PARM_DECL
|| TREE_CODE (parm) == CONST_DECL)
parm = DECL_INITIAL (parm);
else if (TREE_CODE (parm) == TYPE_DECL
|| TREE_CODE (parm) == TEMPLATE_DECL)
parm = TREE_TYPE (parm);
if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
|| TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
|| TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
parm = TEMPLATE_TYPE_PARM_INDEX (parm);
gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
return parm;
}
/* Subroutine of fixed_parameter_pack_p below. Look for any template
parameter packs used by the template parameter PARM. */
static void
fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
{
/* A type parm can't refer to another parm. */
if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
return;
else if (TREE_CODE (parm) == PARM_DECL)
{
cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
ppd, ppd->visited);
return;
}
gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
{
tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
if (template_parameter_pack_p (p))
/* Any packs in the type are expanded by this parameter. */;
else
fixed_parameter_pack_p_1 (p, ppd);
}
}
/* PARM is a template parameter pack. Return any parameter packs used in
its type or the type of any of its template parameters. If there are
any such packs, it will be instantiated into a fixed template parameter
list by partial instantiation rather than be fully deduced. */
tree
fixed_parameter_pack_p (tree parm)
{
/* This can only be true in a member template. */
if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
return NULL_TREE;
/* This can only be true for a parameter pack. */
if (!template_parameter_pack_p (parm))
return NULL_TREE;
/* A type parm can't refer to another parm. */
if (TREE_CODE (parm) == TYPE_DECL)
return NULL_TREE;
tree parameter_packs = NULL_TREE;
struct find_parameter_pack_data ppd;
ppd.parameter_packs = ¶meter_packs;
ppd.visited = new hash_set<tree>;
ppd.type_pack_expansion_p = false;
fixed_parameter_pack_p_1 (parm, &ppd);
delete ppd.visited;
return parameter_packs;
}
/* Check that a template declaration's use of default arguments and
parameter packs is not invalid. Here, PARMS are the template
parameters. IS_PRIMARY is true if DECL is the thing declared by
a primary template. IS_PARTIAL is true if DECL is a partial
specialization.
IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
function template declaration or a friend class template
declaration. In the function case, 1 indicates a declaration, 2
indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
emitted for extraneous default arguments.
Returns TRUE if there were no errors found, FALSE otherwise. */
bool
check_default_tmpl_args (tree decl, tree parms, bool is_primary,
bool is_partial, int is_friend_decl)
{
const char *msg;
int last_level_to_check;
tree parm_level;
bool no_errors = true;
/* [temp.param]
A default template-argument shall not be specified in a
function template declaration or a function template definition, nor
in the template-parameter-list of the definition of a member of a
class template. */
if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
|| (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
/* You can't have a function template declaration in a local
scope, nor you can you define a member of a class template in a
local scope. */
return true;
if ((TREE_CODE (decl) == TYPE_DECL
&& TREE_TYPE (decl)
&& LAMBDA_TYPE_P (TREE_TYPE (decl)))
|| (TREE_CODE (decl) == FUNCTION_DECL
&& LAMBDA_FUNCTION_P (decl)))
/* A lambda doesn't have an explicit declaration; don't complain
about the parms of the enclosing class. */
return true;
if (current_class_type
&& !TYPE_BEING_DEFINED (current_class_type)
&& DECL_LANG_SPECIFIC (decl)
&& DECL_DECLARES_FUNCTION_P (decl)
/* If this is either a friend defined in the scope of the class
or a member function. */
&& (DECL_FUNCTION_MEMBER_P (decl)
? same_type_p (DECL_CONTEXT (decl), current_class_type)
: DECL_FRIEND_CONTEXT (decl)
? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
: false)
/* And, if it was a member function, it really was defined in
the scope of the class. */
&& (!DECL_FUNCTION_MEMBER_P (decl)
|| DECL_INITIALIZED_IN_CLASS_P (decl)))
/* We already checked these parameters when the template was
declared, so there's no need to do it again now. This function
was defined in class scope, but we're processing its body now
that the class is complete. */
return true;
/* Core issue 226 (C++0x only): the following only applies to class
templates. */
if (is_primary
&& ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
{
/* [temp.param]
If a template-parameter has a default template-argument, all
subsequent template-parameters shall have a default
template-argument supplied. */
for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
{
tree inner_parms = TREE_VALUE (parm_level);
int ntparms = TREE_VEC_LENGTH (inner_parms);
int seen_def_arg_p = 0;
int i;
for (i = 0; i < ntparms; ++i)
{
tree parm = TREE_VEC_ELT (inner_parms, i);
if (parm == error_mark_node)
continue;
if (TREE_PURPOSE (parm))
seen_def_arg_p = 1;
else if (seen_def_arg_p
&& !template_parameter_pack_p (TREE_VALUE (parm)))
{
error ("no default argument for %qD", TREE_VALUE (parm));
/* For better subsequent error-recovery, we indicate that
there should have been a default argument. */
TREE_PURPOSE (parm) = error_mark_node;
no_errors = false;
}
else if (!is_partial
&& !is_friend_decl
/* Don't complain about an enclosing partial
specialization. */
&& parm_level == parms
&& TREE_CODE (decl) == TYPE_DECL
&& i < ntparms - 1
&& template_parameter_pack_p (TREE_VALUE (parm))
/* A fixed parameter pack will be partially
instantiated into a fixed length list. */
&& !fixed_parameter_pack_p (TREE_VALUE (parm)))
{
/* A primary class template can only have one
parameter pack, at the end of the template
parameter list. */
error ("parameter pack %q+D must be at the end of the"
" template parameter list", TREE_VALUE (parm));
TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
= error_mark_node;
no_errors = false;
}
}
}
}
if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
|| is_partial
|| !is_primary
|| is_friend_decl)
/* For an ordinary class template, default template arguments are
allowed at the innermost level, e.g.:
template <class T = int>
struct S {};
but, in a partial specialization, they're not allowed even
there, as we have in [temp.class.spec]:
The template parameter list of a specialization shall not
contain default template argument values.
So, for a partial specialization, or for a function template
(in C++98/C++03), we look at all of them. */
;
else
/* But, for a primary class template that is not a partial
specialization we look at all template parameters except the
innermost ones. */
parms = TREE_CHAIN (parms);
/* Figure out what error message to issue. */
if (is_friend_decl == 2)
msg = G_("default template arguments may not be used in function template "
"friend re-declaration");
else if (is_friend_decl)
msg = G_("default template arguments may not be used in template "
"friend declarations");
else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
msg = G_("default template arguments may not be used in function templates "
"without %<-std=c++11%> or %<-std=gnu++11%>");
else if (is_partial)
msg = G_("default template arguments may not be used in "
"partial specializations");
else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
msg = G_("default argument for template parameter for class enclosing %qD");
else
/* Per [temp.param]/9, "A default template-argument shall not be
specified in the template-parameter-lists of the definition of
a member of a class template that appears outside of the member's
class.", thus if we aren't handling a member of a class template
there is no need to examine the parameters. */
return true;
if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
/* If we're inside a class definition, there's no need to
examine the parameters to the class itself. On the one
hand, they will be checked when the class is defined, and,
on the other, default arguments are valid in things like:
template <class T = double>
struct S { template <class U> void f(U); };
Here the default argument for `S' has no bearing on the
declaration of `f'. */
last_level_to_check = template_class_depth (current_class_type) + 1;
else
/* Check everything. */
last_level_to_check = 0;
for (parm_level = parms;
parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
parm_level = TREE_CHAIN (parm_level))
{
tree inner_parms = TREE_VALUE (parm_level);
int i;
int ntparms;
ntparms = TREE_VEC_LENGTH (inner_parms);
for (i = 0; i < ntparms; ++i)
{
if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
continue;
if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
{
if (msg)
{
no_errors = false;
if (is_friend_decl == 2)
return no_errors;
error (msg, decl);
msg = 0;
}
/* Clear out the default argument so that we are not
confused later. */
TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
}
}
/* At this point, if we're still interested in issuing messages,
they must apply to classes surrounding the object declared. */
if (msg)
msg = G_("default argument for template parameter for class "
"enclosing %qD");
}
return no_errors;
}
/* Worker for push_template_decl_real, called via
for_each_template_parm. DATA is really an int, indicating the
level of the parameters we are interested in. If T is a template
parameter of that level, return nonzero. */
static int
template_parm_this_level_p (tree t, void* data)
{
int this_level = *(int *)data;
int level;
if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
level = TEMPLATE_PARM_LEVEL (t);
else
level = TEMPLATE_TYPE_LEVEL (t);
return level == this_level;
}
/* Worker for uses_outer_template_parms, called via for_each_template_parm.
DATA is really an int, indicating the innermost outer level of parameters.
If T is a template parameter of that level or further out, return
nonzero. */
static int
template_parm_outer_level (tree t, void *data)
{
int this_level = *(int *)data;
int level;
if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
level = TEMPLATE_PARM_LEVEL (t);
else
level = TEMPLATE_TYPE_LEVEL (t);
|