Skip to content

Commit 89bffe9

Browse files
gchateletZijunZhaoCCK
authored andcommitted
[libc] Add is_object (llvm#65749)
Add the is_object type traits. Implementation comes from https://en.cppreference.com/w/cpp/types/is_object
1 parent 09c9d98 commit 89bffe9

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ add_header_library(
118118
type_traits/is_lvalue_reference.h
119119
type_traits/is_member_pointer.h
120120
type_traits/is_null_pointer.h
121+
type_traits/is_object.h
121122
type_traits/is_pointer.h
122123
type_traits/is_reference.h
123124
type_traits/is_rvalue_reference.h

libc/src/__support/CPP/type_traits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "src/__support/CPP/type_traits/is_lvalue_reference.h"
3333
#include "src/__support/CPP/type_traits/is_member_pointer.h"
3434
#include "src/__support/CPP/type_traits/is_null_pointer.h"
35+
#include "src/__support/CPP/type_traits/is_object.h"
3536
#include "src/__support/CPP/type_traits/is_pointer.h"
3637
#include "src/__support/CPP/type_traits/is_reference.h"
3738
#include "src/__support/CPP/type_traits/is_rvalue_reference.h"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- is_object type_traits -----------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#ifndef LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_IS_OBJECT_H
9+
#define LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_IS_OBJECT_H
10+
11+
#include "src/__support/CPP/type_traits/bool_constant.h"
12+
#include "src/__support/CPP/type_traits/is_array.h"
13+
#include "src/__support/CPP/type_traits/is_class.h"
14+
#include "src/__support/CPP/type_traits/is_scalar.h"
15+
#include "src/__support/CPP/type_traits/is_union.h"
16+
#include "src/__support/macros/attributes.h"
17+
18+
namespace __llvm_libc::cpp {
19+
20+
// is_object
21+
template <class T>
22+
struct is_object
23+
: cpp::bool_constant<cpp::is_scalar_v<T> || cpp::is_array_v<T> ||
24+
cpp::is_union_v<T> || cpp::is_class_v<T>> {};
25+
template <class T>
26+
LIBC_INLINE_VAR constexpr bool is_object_v = is_object<T>::value;
27+
28+
} // namespace __llvm_libc::cpp
29+
30+
#endif // LLVM_LIBC_SRC_SUPPORT_CPP_TYPE_TRAITS_IS_OBJECT_H

libc/test/src/__support/CPP/type_traits_test.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,28 @@ TEST(LlvmLibcTypeTraitsTest, is_enum_enum) {
253253

254254
// TODO is_null_pointer
255255

256+
TEST(LlvmLibcTypeTraitsTest, is_object) {
257+
EXPECT_TRUE((is_object_v<int>)); // scalar
258+
EXPECT_TRUE((is_object_v<Struct[]>)); // array
259+
EXPECT_TRUE((is_object_v<Union>)); // union
260+
EXPECT_TRUE((is_object_v<Class>)); // class
261+
262+
// pointers are still objects
263+
EXPECT_TRUE((is_object_v<int *>)); // scalar
264+
EXPECT_TRUE((is_object_v<Struct(*)[]>)); // array
265+
EXPECT_TRUE((is_object_v<Union *>)); // union
266+
EXPECT_TRUE((is_object_v<Class *>)); // class
267+
268+
// reference are not objects
269+
EXPECT_FALSE((is_object_v<int &>)); // scalar
270+
EXPECT_FALSE((is_object_v<Struct(&)[]>)); // array
271+
EXPECT_FALSE((is_object_v<Union &>)); // union
272+
EXPECT_FALSE((is_object_v<Class &>)); // class
273+
274+
// not an object
275+
EXPECT_FALSE((is_object_v<void>));
276+
}
277+
256278
// TODO is_pointer
257279

258280
// TODO is_reference

utils/bazel/llvm-project-overlay/libc/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ libc_support_library(
309309
"src/__support/CPP/type_traits/is_lvalue_reference.h",
310310
"src/__support/CPP/type_traits/is_member_pointer.h",
311311
"src/__support/CPP/type_traits/is_null_pointer.h",
312+
"src/__support/CPP/type_traits/is_object.h",
312313
"src/__support/CPP/type_traits/is_pointer.h",
313314
"src/__support/CPP/type_traits/is_reference.h",
314315
"src/__support/CPP/type_traits/is_rvalue_reference.h",

0 commit comments

Comments
 (0)