This patch implements built-in trait for std::add_pointer.
gcc/cp/ChangeLog:
* cp-trait.def: Define __add_pointer.
* semantics.cc (finish_trait_type): Handle CPTK_ADD_POINTER.
(object_type_p): New function.
(referenceable_type_p): Likewise.
(trait_expr_value): Use object_type_p.
gcc/testsuite/ChangeLog:
* g++.dg/ext/has-builtin-1.C: Test existence of __add_pointer.
* g++.dg/ext/add_pointer.C: New test.
Signed-off-by: Ken Matsui <kmatsui@gcc.gnu.org>
Reviewed-by: Jason Merrill <jason@redhat.com>
#define DEFTRAIT_TYPE_DEFAULTED
#endif
+DEFTRAIT_TYPE (ADD_POINTER, "__add_pointer", 1)
DEFTRAIT_EXPR (HAS_NOTHROW_ASSIGN, "__has_nothrow_assign", 1)
DEFTRAIT_EXPR (HAS_NOTHROW_CONSTRUCTOR, "__has_nothrow_constructor", 1)
DEFTRAIT_EXPR (HAS_NOTHROW_COPY, "__has_nothrow_copy", 1)
fold_convert (TREE_TYPE (arg1), arg2)));
}
+/* [basic.types] 8. True iff TYPE is an object type. */
+
+static bool
+object_type_p (const_tree type)
+{
+ return (TREE_CODE (type) != FUNCTION_TYPE
+ && !TYPE_REF_P (type)
+ && !VOID_TYPE_P (type));
+}
+
/* Actually evaluates the trait. */
static bool
return is_nothrow_convertible (type1, type2);
case CPTK_IS_OBJECT:
- return (type_code1 != FUNCTION_TYPE
- && type_code1 != REFERENCE_TYPE
- && type_code1 != VOID_TYPE);
+ return object_type_p (type1);
case CPTK_IS_POINTER_INTERCONVERTIBLE_BASE_OF:
return pointer_interconvertible_base_of_p (type1, type2);
(non_reference (to), non_reference (from))));
}
+/* [defns.referenceable] True iff TYPE is a referenceable type. */
+
+static bool
+referenceable_type_p (const_tree type)
+{
+ return (TYPE_REF_P (type)
+ || object_type_p (type)
+ || (FUNC_OR_METHOD_TYPE_P (type)
+ && (type_memfn_quals (type) == TYPE_UNQUALIFIED
+ && type_memfn_rqual (type) == REF_QUAL_NONE)));
+}
+
/* Process a trait expression. */
tree
switch (kind)
{
+ case CPTK_ADD_POINTER:
+ /* [meta.trans.ptr]. */
+ if (VOID_TYPE_P (type1) || referenceable_type_p (type1))
+ {
+ if (TYPE_REF_P (type1))
+ type1 = TREE_TYPE (type1);
+ return build_pointer_type (type1);
+ }
+ return type1;
+
case CPTK_REMOVE_CV:
return cv_unqualified (type1);
--- /dev/null
+// { dg-do compile { target c++11 } }
+
+#define SA(X) static_assert((X),#X)
+
+class ClassType { };
+
+SA(__is_same(__add_pointer(int), int*));
+SA(__is_same(__add_pointer(int*), int**));
+SA(__is_same(__add_pointer(const int), const int*));
+SA(__is_same(__add_pointer(int&), int*));
+SA(__is_same(__add_pointer(ClassType*), ClassType**));
+SA(__is_same(__add_pointer(ClassType), ClassType*));
+SA(__is_same(__add_pointer(void), void*));
+SA(__is_same(__add_pointer(const void), const void*));
+SA(__is_same(__add_pointer(volatile void), volatile void*));
+SA(__is_same(__add_pointer(const volatile void), const volatile void*));
+
+void f1();
+using f1_type = decltype(f1);
+using pf1_type = decltype(&f1);
+SA(__is_same(__add_pointer(f1_type), pf1_type));
+
+void f2() noexcept; // PR libstdc++/78361
+using f2_type = decltype(f2);
+using pf2_type = decltype(&f2);
+SA(__is_same(__add_pointer(f2_type), pf2_type));
+
+using fn_type = void();
+using pfn_type = void(*)();
+SA(__is_same(__add_pointer(fn_type), pfn_type));
+
+SA(__is_same(__add_pointer(void() &), void() &));
+SA(__is_same(__add_pointer(void() & noexcept), void() & noexcept));
+SA(__is_same(__add_pointer(void() const), void() const));
+SA(__is_same(__add_pointer(void(...) &), void(...) &));
+SA(__is_same(__add_pointer(void(...) & noexcept), void(...) & noexcept));
+SA(__is_same(__add_pointer(void(...) const), void(...) const));
+
+SA(__is_same(__add_pointer(void() __restrict), void() __restrict));
// { dg-do compile }
// Verify that __has_builtin gives the correct answer for C++ built-ins.
+#if !__has_builtin (__add_pointer)
+# error "__has_builtin (__add_pointer) failed"
+#endif
#if !__has_builtin (__builtin_addressof)
# error "__has_builtin (__builtin_addressof) failed"
#endif