static void add_pending_template (tree);
static tree reopen_tinst_level (struct tinst_level *);
static tree tsubst_initializer_list (tree, tree);
-static tree get_class_bindings (tree, tree, tree, tree);
+static tree get_partial_spec_bindings (tree, 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,
static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
static void regenerate_decl_from_template (tree, tree);
-static tree most_specialized_class (tree, tsubst_flags_t);
+static 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);
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)
- /* TMPL is a forward declaration of a template function; keep a list
+ 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. */
+ 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));
}
/* This case handles bogus declarations like template <>
template <class T> void f<int>(); */
- if (uses_template_parms (declarator))
+ if (!uses_template_parms (declarator))
+ error ("template-id %qD 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 ("non-type partial specialization %qD "
"is not allowed", declarator);
else
- error ("template-id %qD in declaration of primary template",
- declarator);
+ error ("non-class, non-variable partial specialization %qD "
+ "is not allowed", declarator);
return decl;
+ ok:;
}
if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
{
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 (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
+ if (!was_template_id)
{
tree fns;
else if (ctype != NULL_TREE
&& (identifier_p (TREE_OPERAND (declarator, 0))))
{
- // Ignore variable templates.
+ // We'll match variable templates in start_decl.
if (VAR_P (decl))
return 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)
+ if (tsk == tsk_template && !was_template_id)
{
tree result = DECL_TEMPLATE_RESULT (tmpl);
SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
/* 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))
process_partial_specialization (tree decl)
{
tree type = TREE_TYPE (decl);
- tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
- tree specargs = CLASSTYPE_TI_ARGS (type);
+ 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;
The argument list of the specialization shall not be identical to
the implicit argument list of the primary template. */
- if (comp_template_args
- (inner_args,
- INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
- (maintmpl)))))
- error ("partial specialization %qT does not specialize any template arguments", type);
+ tree main_args
+ = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
+ if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args)))
+ error ("partial specialization %qD does not specialize "
+ "any template arguments", decl);
/* A partial specialization that replaces multiple parameters of the
primary template with a pack expansion is less specialized for those
}
/* We should only get here once. */
- gcc_assert (!COMPLETE_TYPE_P (type));
+ if (TREE_CODE (decl) == TYPE_DECL)
+ gcc_assert (!COMPLETE_TYPE_P (type));
tree tmpl = build_template_decl (decl, current_template_parms,
DECL_MEMBER_TEMPLATE_P (maintmpl));
for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
inst = TREE_CHAIN (inst))
{
- tree inst_type = TREE_VALUE (inst);
- if (COMPLETE_TYPE_P (inst_type)
- && CLASSTYPE_IMPLICIT_INSTANTIATION (inst_type))
+ 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_class (inst_type, tf_none);
- if (spec && TREE_TYPE (spec) == type)
- permerror (input_location,
- "partial specialization of %qT after instantiation "
- "of %qT", type, inst_type);
+ tree spec = most_specialized_partial_spec (instance, tf_none);
+ if (spec && TREE_VALUE (spec) == tmpl)
+ {
+ tree inst_decl = (DECL_P (instance)
+ ? instance : TYPE_NAME (instance));
+ permerror (input_location,
+ "partial specialization of %qD after instantiation "
+ "of %qD", decl, inst_decl);
+ }
}
}
return error_mark_node;
/* See if this is a partial specialization. */
- is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
- && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
- && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
+ is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
+ && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
+ && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
+ || (TREE_CODE (decl) == VAR_DECL
+ && DECL_LANG_SPECIFIC (decl)
+ && DECL_TEMPLATE_SPECIALIZATION (decl)
+ && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
is_friend = true;
/* Determine what specialization of the original template to
instantiate. */
- t = most_specialized_class (type, tf_warning_or_error);
+ t = most_specialized_partial_spec (type, tf_warning_or_error);
if (t == error_mark_node)
{
TYPE_BEING_DEFINED (type) = 1;
if (new_type == error_mark_node)
RETURN (error_mark_node);
/* If we get a real template back, return it. This can happen in
- the context of most_specialized_class. */
+ the context of most_specialized_partial_spec. */
if (TREE_CODE (new_type) == TEMPLATE_DECL)
return new_type;
complain, gen_tmpl, true);
push_nested_class (ctx);
}
+
+ tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
+
+ if (VAR_P (pattern))
+ {
+ /* We need to determine if we're using a partial or explicit
+ specialization now, because the type of the variable could be
+ different. */
+ tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
+ tree elt = most_specialized_partial_spec (tid, complain);
+ if (elt == error_mark_node)
+ pattern = error_mark_node;
+ else if (elt)
+ {
+ tmpl = TREE_VALUE (elt);
+ pattern = DECL_TEMPLATE_RESULT (tmpl);
+ targ_ptr = TREE_PURPOSE (elt);
+ }
+ }
+
/* Substitute template parameters to obtain the specialization. */
- fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
- targ_ptr, complain, gen_tmpl);
+ fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
if (DECL_CLASS_SCOPE_P (gen_tmpl))
pop_nested_class ();
pop_from_top_level ();
/* Determine which of two partial specializations of TMPL is more
specialized.
- PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
- to the first partial specialization. The TREE_VALUE is the
+ PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
+ to the first partial specialization. The TREE_PURPOSE is the
innermost set of template parameters for the partial
specialization. PAT2 is similar, but for the second template.
two templates is more specialized. */
static int
-more_specialized_class (tree tmpl, tree pat1, tree pat2)
+more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
{
tree targs;
- tree tmpl1, tmpl2;
int winner = 0;
bool any_deductions = false;
- tmpl1 = TREE_TYPE (pat1);
- tmpl2 = TREE_TYPE (pat2);
+ tree tmpl1 = TREE_VALUE (pat1);
+ tree tmpl2 = TREE_VALUE (pat2);
+ tree parms1 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl1);
+ tree parms2 = DECL_INNERMOST_TEMPLATE_PARMS (tmpl2);
+ tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
+ tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
/* Just like what happens for functions, if we are ordering between
- different class template specializations, we may encounter dependent
+ different template specializations, we may encounter dependent
types in the arguments, and we need our dependency check functions
to behave correctly. */
++processing_template_decl;
- targs = get_class_bindings (tmpl, TREE_VALUE (pat1),
- CLASSTYPE_TI_ARGS (tmpl1),
- CLASSTYPE_TI_ARGS (tmpl2));
+ targs = get_partial_spec_bindings (tmpl, parms1, specargs1, specargs2);
if (targs)
{
--winner;
any_deductions = true;
}
- targs = get_class_bindings (tmpl, TREE_VALUE (pat2),
- CLASSTYPE_TI_ARGS (tmpl2),
- CLASSTYPE_TI_ARGS (tmpl1));
+ targs = get_partial_spec_bindings (tmpl, parms2, specargs2, specargs1);
if (targs)
{
++winner;
}
--processing_template_decl;
- /* In the case of a tie where at least one of the class templates
+ /* In the case of a tie where at least one of the templates
has a parameter pack at the end, the template with the most
non-packed parameters wins. */
if (winner == 0
is bound to `double'. */
static tree
-get_class_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
+get_partial_spec_bindings (tree tmpl, tree tparms, tree spec_args, tree args)
{
int i, ntparms = TREE_VEC_LENGTH (tparms);
tree deduced_args;
return decl;
}
-/* Return the most specialized of the class template partial
- specializations which can produce TYPE, a specialization of some class
- template. The value returned is actually a TREE_LIST; the TREE_TYPE is
- a _TYPE node corresponding to the partial specialization, while the
- TREE_PURPOSE is the set of template arguments that must be
- substituted into the TREE_TYPE in order to generate TYPE.
+/* Return the most specialized of the template partial specializations
+ which can produce TARGET, a specialization of some class or variable
+ template. The value returned is actually a TREE_LIST; the TREE_VALUE is
+ a TEMPLATE_DECL node corresponding to the partial specialization, while
+ the TREE_PURPOSE is the set of template arguments that must be
+ substituted into the template pattern in order to generate TARGET.
If the choice of partial specialization is ambiguous, a diagnostic
is issued, and the error_mark_node is returned. If there are no
- partial specializations matching TYPE, then NULL_TREE is
+ partial specializations matching TARGET, then NULL_TREE is
returned, indicating that the primary template should be used. */
static tree
-most_specialized_class (tree type, tsubst_flags_t complain)
+most_specialized_partial_spec (tree target, tsubst_flags_t complain)
{
tree list = NULL_TREE;
tree t;
int fate;
bool ambiguous_p;
tree outer_args = NULL_TREE;
+ tree tmpl, args;
+
+ if (TYPE_P (target))
+ {
+ tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
+ tmpl = TI_TEMPLATE (tinfo);
+ args = TI_ARGS (tinfo);
+ }
+ else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
+ {
+ tmpl = TREE_OPERAND (target, 0);
+ args = TREE_OPERAND (target, 1);
+ }
+ else if (VAR_P (target))
+ {
+ tree tinfo = DECL_TEMPLATE_INFO (target);
+ tmpl = TI_TEMPLATE (tinfo);
+ args = TI_ARGS (tinfo);
+ }
+ else
+ gcc_unreachable ();
- tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
tree main_tmpl = most_general_template (tmpl);
- tree args = CLASSTYPE_TI_ARGS (type);
/* For determining which partial specialization to use, only the
innermost args are interesting. */
tree partial_spec_args;
tree spec_args;
tree spec_tmpl = TREE_VALUE (t);
- tree orig_parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
- partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
+ partial_spec_args = TREE_PURPOSE (t);
++processing_template_decl;
return error_mark_node;
tree parms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
- spec_args = get_class_bindings (tmpl, parms,
+ spec_args = get_partial_spec_bindings (tmpl, parms,
partial_spec_args,
args);
if (spec_args)
{
if (outer_args)
spec_args = add_to_template_args (outer_args, spec_args);
- list = tree_cons (spec_args, orig_parms, list);
+ list = tree_cons (spec_args, TREE_VALUE (t), list);
TREE_TYPE (list) = TREE_TYPE (t);
}
}
t = TREE_CHAIN (t);
for (; t; t = TREE_CHAIN (t))
{
- fate = more_specialized_class (tmpl, champ, t);
+ fate = more_specialized_partial_spec (tmpl, champ, t);
if (fate == 1)
;
else
if (!ambiguous_p)
for (t = list; t && t != champ; t = TREE_CHAIN (t))
{
- fate = more_specialized_class (tmpl, champ, t);
+ fate = more_specialized_partial_spec (tmpl, champ, t);
if (fate != 1)
{
ambiguous_p = true;
char *spaces = NULL;
if (!(complain & tf_error))
return error_mark_node;
- error ("ambiguous class template instantiation for %q#T", type);
+ if (TYPE_P (target))
+ error ("ambiguous template instantiation for %q#T", target);
+ else
+ error ("ambiguous template instantiation for %q#D", target);
str = ngettext ("candidate is:", "candidates are:", list_length (list));
for (t = list; t; t = TREE_CHAIN (t))
{
- error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
+ tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
+ inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
+ "%s %#S", spaces ? spaces : str, subst);
spaces = spaces ? spaces : get_spaces (str);
}
free (spaces);