From e7ab5252136f986033bb60993dd859b5c2d91ae2 Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Thu, 19 Nov 2020 16:05:21 +0300 Subject: [PATCH] Add CppSupport with backported utilities (#4525) --- .../runtime/src/main/cpp/CppSupport.hpp | 51 ++++++++++++++++++ .../runtime/src/main/cpp/MemorySharedRefs.hpp | 10 ++-- .../runtime/src/main/cpp/Natives.cpp | 3 +- .../main/cpp/TestSupportCompilerGenerated.hpp | 4 +- .../runtime/src/main/cpp/UtilsTest.cpp | 52 +++++++------------ 5 files changed, 79 insertions(+), 41 deletions(-) create mode 100644 kotlin-native/runtime/src/main/cpp/CppSupport.hpp diff --git a/kotlin-native/runtime/src/main/cpp/CppSupport.hpp b/kotlin-native/runtime/src/main/cpp/CppSupport.hpp new file mode 100644 index 00000000000..9f04f544c41 --- /dev/null +++ b/kotlin-native/runtime/src/main/cpp/CppSupport.hpp @@ -0,0 +1,51 @@ +/* + * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#ifndef RUNTIME_CPP_SUPPORT_H +#define RUNTIME_CPP_SUPPORT_H + +#include +#include + +// A collection of backported utilities from future C++ versions. + +namespace kotlin { +namespace std_support { + +////////////////////////// C++14 ////////////////////////// + +template +std::unique_ptr make_unique(Args&&... args) { + return std::unique_ptr(new T(std::forward(args)...)); +} + +template +using make_unsigned_t = typename std::make_unsigned::type; + +////////////////////////// C++17 ////////////////////////// + +template +constexpr bool is_trivially_destructible_v = std::is_trivially_destructible::value; +template +constexpr bool is_nothrow_default_constructible_v = std::is_nothrow_default_constructible::value; +template +constexpr bool is_nothrow_destructible_v = std::is_nothrow_destructible::value; +template +constexpr bool is_copy_constructible_v = std::is_copy_constructible::value; +template +constexpr bool is_copy_assignable_v = std::is_copy_assignable::value; +template +constexpr bool is_move_constructible_v = std::is_move_constructible::value; +template +constexpr bool is_move_assignable_v = std::is_move_assignable::value; +template +constexpr bool is_nothrow_move_constructible_v = std::is_nothrow_move_constructible::value; +template +constexpr bool is_nothrow_move_assignable_v = std::is_nothrow_move_assignable::value; + +} // namespace std_support +} // namespace kotlin + +#endif // RUNTIME_CPP_SUPPORT_H diff --git a/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.hpp b/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.hpp index c6d8fc8c82c..7e2e45b7113 100644 --- a/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.hpp +++ b/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.hpp @@ -8,6 +8,7 @@ #include +#include "CppSupport.hpp" #include "Memory.h" // TODO: Generalize for uses outside this file. @@ -37,8 +38,8 @@ class KRefSharedHolder { ForeignRefContext context_; }; -static_assert(std::is_trivially_destructible::value, - "KRefSharedHolder destructor is not guaranteed to be called."); +static_assert( + kotlin::std_support::is_trivially_destructible_v, "KRefSharedHolder destructor is not guaranteed to be called."); class BackRefFromAssociatedObject { public: @@ -66,7 +67,8 @@ class BackRefFromAssociatedObject { volatile int refCount; }; -static_assert(std::is_trivially_destructible::value, - "BackRefFromAssociatedObject destructor is not guaranteed to be called."); +static_assert( + kotlin::std_support::is_trivially_destructible_v, + "BackRefFromAssociatedObject destructor is not guaranteed to be called."); #endif // RUNTIME_MEMORYSHAREDREFS_HPP diff --git a/kotlin-native/runtime/src/main/cpp/Natives.cpp b/kotlin-native/runtime/src/main/cpp/Natives.cpp index c0e339c5b66..565963aac16 100644 --- a/kotlin-native/runtime/src/main/cpp/Natives.cpp +++ b/kotlin-native/runtime/src/main/cpp/Natives.cpp @@ -21,6 +21,7 @@ #include #include +#include "CppSupport.hpp" #include "KAssert.h" #include "Exceptions.h" #include "Memory.h" @@ -50,7 +51,7 @@ OBJ_GETTER0(Kotlin_native_internal_undefined) { } void* Kotlin_interop_malloc(KLong size, KInt align) { - if (size < 0 || static_cast::type>(size) > std::numeric_limits::max()) { + if (size < 0 || static_cast>(size) > std::numeric_limits::max()) { return nullptr; } RuntimeAssert(align > 0, "Unsupported alignment"); diff --git a/kotlin-native/runtime/src/main/cpp/TestSupportCompilerGenerated.hpp b/kotlin-native/runtime/src/main/cpp/TestSupportCompilerGenerated.hpp index cf0bb7d1f7c..26fccdb0838 100644 --- a/kotlin-native/runtime/src/main/cpp/TestSupportCompilerGenerated.hpp +++ b/kotlin-native/runtime/src/main/cpp/TestSupportCompilerGenerated.hpp @@ -9,6 +9,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "CppSupport.hpp" #include "Types.h" #include "Utils.hpp" @@ -20,8 +21,7 @@ public: explicit ScopedStrictMockFunction(Mock** globalMockLocation) : globalMockLocation_(globalMockLocation) { RuntimeCheck(globalMockLocation != nullptr, "ScopedStrictMockFunction needs non-null global mock location"); RuntimeCheck(*globalMockLocation == nullptr, "ScopedStrictMockFunction needs null global mock"); - // TODO: Use make_unique when sysroots on Linux get updated. - mock_ = std::unique_ptr(new Mock()); + mock_ = kotlin::std_support::make_unique(); *globalMockLocation_ = mock_.get(); } diff --git a/kotlin-native/runtime/src/main/cpp/UtilsTest.cpp b/kotlin-native/runtime/src/main/cpp/UtilsTest.cpp index b6403b45766..5ba0aea917c 100644 --- a/kotlin-native/runtime/src/main/cpp/UtilsTest.cpp +++ b/kotlin-native/runtime/src/main/cpp/UtilsTest.cpp @@ -5,40 +5,24 @@ #include "Utils.hpp" -#include - #include "gtest/gtest.h" -namespace { +#include "CppSupport.hpp" -// TODO: use std variants when we move to C++17 -template -constexpr bool is_nothrow_default_constructible_v = std::is_nothrow_default_constructible::value; -template -constexpr bool is_nothrow_destructible_v = std::is_nothrow_destructible::value; -template -constexpr bool is_copy_constructible_v = std::is_copy_constructible::value; -template -constexpr bool is_copy_assignable_v = std::is_copy_assignable::value; -template -constexpr bool is_move_constructible_v = std::is_move_constructible::value; -template -constexpr bool is_move_assignable_v = std::is_move_assignable::value; -template -constexpr bool is_nothrow_move_constructible_v = std::is_nothrow_move_constructible::value; -template -constexpr bool is_nothrow_move_assignable_v = std::is_nothrow_move_assignable::value; +using namespace kotlin; + +namespace { struct A { int field; }; -class MoveOnlyImpl : private kotlin::MoveOnly { +class MoveOnlyImpl : private MoveOnly { public: A a; }; -class PinnedImpl : private kotlin::Pinned { +class PinnedImpl : private Pinned { public: A a; }; @@ -46,21 +30,21 @@ public: } // namespace TEST(UtilsTest, MoveOnlyImpl) { - static_assert(is_nothrow_default_constructible_v, "Must be nothrow default constructible"); - static_assert(is_nothrow_destructible_v, "Must be nothrow destructible"); - static_assert(!is_copy_constructible_v, "Must not be copy constructible"); - static_assert(!is_copy_assignable_v, "Must not be copy assignable"); - static_assert(is_nothrow_move_constructible_v, "Must be nothrow move constructible"); - static_assert(is_nothrow_move_assignable_v, "Must be nothrow move assignable"); + static_assert(std_support::is_nothrow_default_constructible_v, "Must be nothrow default constructible"); + static_assert(std_support::is_nothrow_destructible_v, "Must be nothrow destructible"); + static_assert(!std_support::is_copy_constructible_v, "Must not be copy constructible"); + static_assert(!std_support::is_copy_assignable_v, "Must not be copy assignable"); + static_assert(std_support::is_nothrow_move_constructible_v, "Must be nothrow move constructible"); + static_assert(std_support::is_nothrow_move_assignable_v, "Must be nothrow move assignable"); static_assert(sizeof(MoveOnlyImpl) == sizeof(A), "Must not increase size"); } TEST(UtilsTest, PinnedImpl) { - static_assert(is_nothrow_default_constructible_v, "Must be nothrow default constructible"); - static_assert(is_nothrow_destructible_v, "Must be nothrow destructible"); - static_assert(!is_copy_constructible_v, "Must not be copy constructible"); - static_assert(!is_copy_assignable_v, "Must not be copy assignable"); - static_assert(!is_move_constructible_v, "Must not be move constructible"); - static_assert(!is_move_assignable_v, "Must not be move assignable"); + static_assert(std_support::is_nothrow_default_constructible_v, "Must be nothrow default constructible"); + static_assert(std_support::is_nothrow_destructible_v, "Must be nothrow destructible"); + static_assert(!std_support::is_copy_constructible_v, "Must not be copy constructible"); + static_assert(!std_support::is_copy_assignable_v, "Must not be copy assignable"); + static_assert(!std_support::is_move_constructible_v, "Must not be move constructible"); + static_assert(!std_support::is_move_assignable_v, "Must not be move assignable"); static_assert(sizeof(PinnedImpl) == sizeof(A), "Must not increase size"); }