2019-11-06 Jason Merrill <jason@redhat.com>
+ Implement D1907R1 "structural type".
+ * tree.c (structural_type_p): New.
+ * pt.c (invalid_nontype_parm_type_p): Use it.
+ * class.c (build_base_field_1): Take binfo. Copy TREE_PRIVATE.
+ (build_base_field): Pass binfo.
+
PR c++/92150 - partial specialization with class NTTP.
* pt.c (unify): Handle VIEW_CONVERT_EXPR.
fields at NEXT_FIELD, and return it. */
static tree
-build_base_field_1 (tree t, tree basetype, tree *&next_field)
+build_base_field_1 (tree t, tree binfo, tree *&next_field)
{
/* Create the FIELD_DECL. */
+ tree basetype = BINFO_TYPE (binfo);
gcc_assert (CLASSTYPE_AS_BASE (basetype));
tree decl = build_decl (input_location,
FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
DECL_ARTIFICIAL (decl) = 1;
DECL_IGNORED_P (decl) = 1;
DECL_FIELD_CONTEXT (decl) = t;
+ TREE_PRIVATE (decl) = TREE_PRIVATE (binfo);
+ TREE_PROTECTED (decl) = TREE_PROTECTED (binfo);
if (is_empty_class (basetype))
/* CLASSTYPE_SIZE is one byte, but the field needs to have size zero. */
DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = size_zero_node;
CLASSTYPE_EMPTY_P (t) = 0;
/* Create the FIELD_DECL. */
- decl = build_base_field_1 (t, basetype, next_field);
+ decl = build_base_field_1 (t, binfo, next_field);
/* Try to place the field. It may take more than one try if we
have a hard time placing the field without putting two
aggregate bases. */
if (cxx_dialect >= cxx17 && !BINFO_VIRTUAL_P (binfo))
{
- tree decl = build_base_field_1 (t, basetype, next_field);
+ tree decl = build_base_field_1 (t, binfo, next_field);
DECL_FIELD_OFFSET (decl) = BINFO_OFFSET (binfo);
DECL_FIELD_BIT_OFFSET (decl) = bitsize_zero_node;
SET_DECL_OFFSET_ALIGN (decl, BITS_PER_UNIT);
extern bool trivially_copyable_p (const_tree);
extern bool type_has_unique_obj_representations (const_tree);
extern bool scalarish_type_p (const_tree);
+extern bool structural_type_p (tree, bool = false);
extern bool type_has_nontrivial_default_init (const_tree);
extern bool type_has_nontrivial_copy_init (const_tree);
extern void maybe_warn_parm_abi (tree, location_t);
return false;
if (!complete_type_or_else (type, NULL_TREE))
return true;
- if (!literal_type_p (type))
+ if (!structural_type_p (type))
{
- error ("%qT is not a valid type for a template non-type parameter "
- "because it is not literal", type);
- explain_non_literal_class (type);
- return true;
- }
- if (cp_has_mutable_p (type))
- {
- error ("%qT is not a valid type for a template non-type parameter "
- "because it has a mutable member", type);
+ auto_diagnostic_group d;
+ if (complain & tf_error)
+ error ("%qT is not a valid type for a template non-type parameter "
+ "because it is not structural", type);
+ structural_type_p (type, true);
return true;
}
- /* FIXME check op<=> and strong structural equality once spaceship is
- implemented. */
return false;
}
return 1;
}
+/* True IFF T is a C++20 structural type (P1907R1) that can be used as a
+ non-type template parameter. If EXPLAIN, explain why not. */
+
+bool
+structural_type_p (tree t, bool explain)
+{
+ t = strip_array_types (t);
+ if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
+ return true;
+ if (NULLPTR_TYPE_P (t))
+ return true;
+ if (TYPE_PTR_P (t) || TYPE_PTRMEM_P (t))
+ return true;
+ if (TYPE_REF_P (t) && !TYPE_REF_IS_RVALUE (t))
+ return true;
+ if (!CLASS_TYPE_P (t))
+ return false;
+ if (TREE_CODE (t) == UNION_TYPE)
+ {
+ if (explain)
+ inform (location_of (t), "%qT is a union", t);
+ return false;
+ }
+ if (!literal_type_p (t))
+ {
+ if (explain)
+ explain_non_literal_class (t);
+ return false;
+ }
+ if (CLASSTYPE_HAS_MUTABLE (t))
+ {
+ if (explain)
+ inform (location_of (t), "%qT has a mutable member", t);
+ return false;
+ }
+ for (tree m = next_initializable_field (TYPE_FIELDS (t)); m;
+ m = next_initializable_field (DECL_CHAIN (m)))
+ {
+ if (TREE_PRIVATE (m) || TREE_PROTECTED (m))
+ {
+ if (explain)
+ inform (location_of (m), "%qD is not public", m);
+ return false;
+ }
+ if (!structural_type_p (TREE_TYPE (m)))
+ {
+ if (explain)
+ {
+ inform (location_of (m), "%qD has a non-structural type", m);
+ structural_type_p (TREE_TYPE (m), true);
+ }
+ return false;
+ }
+ }
+ return true;
+}
+
/* Handle the C++17 [[nodiscard]] attribute, which is similar to the GNU
warn_unused_result attribute. */
// auto operator<=> (const non_literal_fixed_string&) = default;
};
-template <non_literal_class> // { dg-error "11:is not a valid type for a template non-type parameter because it is not literal" }
+template <non_literal_class> // { dg-error "11:is not a valid type for a template non-type parameter because it is not structural" }
int operator"" _udl(); // { dg-error "5:literal operator template .int operator\"\"_udl\\(\\). has invalid parameter list" }