Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 16 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBARTBASE_BASE_CASTS_H_ |
| 18 | #define ART_LIBARTBASE_BASE_CASTS_H_ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 20 | #include <stdint.h> |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 21 | #include <string.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 22 | |
| 23 | #include <limits> |
Andreas Gampe | eafdb96 | 2014-10-23 11:24:08 -0700 | [diff] [blame] | 24 | #include <type_traits> |
| 25 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 27 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 28 | #include "stl_util_identity.h" |
| 29 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 30 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 31 | |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 32 | // Use implicit_cast as a safe version of static_cast or const_cast |
| 33 | // for upcasting in the type hierarchy (i.e. casting a pointer to Foo |
| 34 | // to a pointer to SuperclassOfFoo or casting a pointer to Foo to |
| 35 | // a const pointer to Foo). |
| 36 | // When you use implicit_cast, the compiler checks that the cast is safe. |
| 37 | // Such explicit implicit_casts are necessary in surprisingly many |
| 38 | // situations where C++ demands an exact type match instead of an |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 39 | // argument type convertible to a target type. |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 40 | // |
| 41 | // The From type can be inferred, so the preferred syntax for using |
| 42 | // implicit_cast is the same as for static_cast etc.: |
| 43 | // |
| 44 | // implicit_cast<ToType>(expr) |
| 45 | // |
| 46 | // implicit_cast would have been part of the C++ standard library, |
| 47 | // but the proposal was submitted too late. It will probably make |
| 48 | // its way into the language in the future. |
| 49 | template<typename To, typename From> |
| 50 | inline To implicit_cast(From const &f) { |
| 51 | return f; |
| 52 | } |
| 53 | |
| 54 | // When you upcast (that is, cast a pointer from type Foo to type |
| 55 | // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts |
| 56 | // always succeed. When you downcast (that is, cast a pointer from |
| 57 | // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because |
| 58 | // how do you know the pointer is really of type SubclassOfFoo? It |
| 59 | // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, |
| 60 | // when you downcast, you should use this macro. In debug mode, we |
| 61 | // use dynamic_cast<> to double-check the downcast is legal (we die |
| 62 | // if it's not). In normal mode, we do the efficient static_cast<> |
| 63 | // instead. Thus, it's important to test in debug mode to make sure |
| 64 | // the cast is legal! |
| 65 | // This is the only place in the code we should use dynamic_cast<>. |
| 66 | // In particular, you SHOULDN'T be using dynamic_cast<> in order to |
| 67 | // do RTTI (eg code like this: |
| 68 | // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo); |
| 69 | // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo); |
| 70 | // You should design the code some other way not to need this. |
| 71 | |
| 72 | template<typename To, typename From> // use like this: down_cast<T*>(foo); |
| 73 | inline To down_cast(From* f) { // so we only accept pointers |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 74 | static_assert(std::is_base_of_v<From, std::remove_pointer_t<To>>, |
Andreas Gampe | eafdb96 | 2014-10-23 11:24:08 -0700 | [diff] [blame] | 75 | "down_cast unsafe as To is not a subtype of From"); |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 76 | |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 77 | return static_cast<To>(f); |
| 78 | } |
| 79 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 80 | template<typename To, typename From> // use like this: down_cast<T&>(foo); |
| 81 | inline To down_cast(From& f) { // so we only accept references |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 82 | static_assert(std::is_base_of_v<From, std::remove_reference_t<To>>, |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 83 | "down_cast unsafe as To is not a subtype of From"); |
| 84 | |
| 85 | return static_cast<To>(f); |
| 86 | } |
| 87 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 88 | template <class Dest, class Source> |
| 89 | inline Dest bit_cast(const Source& source) { |
| 90 | // Compile time assertion: sizeof(Dest) == sizeof(Source) |
| 91 | // A compile error here means your Dest and Source have different sizes. |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 92 | static_assert(sizeof(Dest) == sizeof(Source), "sizes should be equal"); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 93 | Dest dest; |
| 94 | memcpy(&dest, &source, sizeof(dest)); |
| 95 | return dest; |
| 96 | } |
| 97 | |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 98 | // A version of static_cast that DCHECKs that the value can be precisely represented |
| 99 | // when converting to Dest. |
| 100 | template <typename Dest, typename Source> |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 101 | constexpr Dest dchecked_integral_cast(Source source) { |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 102 | DCHECK( |
| 103 | // Check that the value is within the lower limit of Dest. |
| 104 | (static_cast<intmax_t>(std::numeric_limits<Dest>::min()) <= |
| 105 | static_cast<intmax_t>(std::numeric_limits<Source>::min()) || |
| 106 | source >= static_cast<Source>(std::numeric_limits<Dest>::min())) && |
| 107 | // Check that the value is within the upper limit of Dest. |
| 108 | (static_cast<uintmax_t>(std::numeric_limits<Dest>::max()) >= |
| 109 | static_cast<uintmax_t>(std::numeric_limits<Source>::max()) || |
Andreas Gampe | 8cf00fa | 2017-04-28 20:50:19 -0700 | [diff] [blame] | 110 | source <= static_cast<Source>(std::numeric_limits<Dest>::max()))) |
| 111 | << "dchecked_integral_cast failed for " << source |
| 112 | << " (would be " << static_cast<Dest>(source) << ")"; |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 113 | |
| 114 | return static_cast<Dest>(source); |
| 115 | } |
| 116 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 117 | // A version of dchecked_integral_cast casting between an integral type and an enum type. |
| 118 | // When casting to an enum type, the cast does not check if the value corresponds to an enumerator. |
| 119 | // When casting from an enum type, the target type can be omitted and the enum's underlying type |
| 120 | // shall be used. |
| 121 | |
| 122 | template <typename Dest, typename Source> |
| 123 | constexpr |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 124 | std::enable_if_t<!std::is_enum_v<Source>, Dest> enum_cast(Source value) { |
| 125 | return static_cast<Dest>(dchecked_integral_cast<std::underlying_type_t<Dest>>(value)); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | template <typename Dest = void, typename Source> |
| 129 | constexpr |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 130 | typename std::enable_if_t<std::is_enum_v<Source>, |
| 131 | std::conditional_t<std::is_same_v<Dest, void>, |
| 132 | std::underlying_type<Source>, |
| 133 | Identity<Dest>>>::type |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 134 | enum_cast(Source value) { |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 135 | using return_type = typename std::conditional_t<std::is_same_v<Dest, void>, |
| 136 | std::underlying_type<Source>, |
| 137 | Identity<Dest>>::type; |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 138 | return dchecked_integral_cast<return_type>( |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 139 | static_cast<std::underlying_type_t<Source>>(value)); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 142 | // A version of reinterpret_cast<>() between pointers and int64_t/uint64_t |
| 143 | // that goes through uintptr_t to avoid treating the pointer as "signed." |
| 144 | |
| 145 | template <typename Dest, typename Source> |
| 146 | inline Dest reinterpret_cast64(Source source) { |
| 147 | // This is the overload for casting from int64_t/uint64_t to a pointer. |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 148 | static_assert(std::is_same_v<Source, int64_t> || std::is_same_v<Source, uint64_t>, |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 149 | "Source must be int64_t or uint64_t."); |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 150 | static_assert(std::is_pointer_v<Dest>, "Dest must be a pointer."); |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 151 | // Check that we don't lose any non-0 bits here. |
| 152 | DCHECK_EQ(static_cast<Source>(static_cast<uintptr_t>(source)), source); |
| 153 | return reinterpret_cast<Dest>(static_cast<uintptr_t>(source)); |
| 154 | } |
| 155 | |
| 156 | template <typename Dest, typename Source> |
| 157 | inline Dest reinterpret_cast64(Source* ptr) { |
| 158 | // This is the overload for casting from a pointer to int64_t/uint64_t. |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 159 | static_assert(std::is_same_v<Dest, int64_t> || std::is_same_v<Dest, uint64_t>, |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 160 | "Dest must be int64_t or uint64_t."); |
| 161 | static_assert(sizeof(uintptr_t) <= sizeof(Dest), "Expecting at most 64-bit pointers."); |
| 162 | return static_cast<Dest>(reinterpret_cast<uintptr_t>(ptr)); |
| 163 | } |
| 164 | |
Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 165 | // A version of reinterpret_cast<>() between pointers and int32_t/uint32_t that enforces |
| 166 | // zero-extension and checks that the values are converted without loss of precision. |
| 167 | |
| 168 | template <typename Dest, typename Source> |
| 169 | inline Dest reinterpret_cast32(Source source) { |
| 170 | // This is the overload for casting from int32_t/uint32_t to a pointer. |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 171 | static_assert(std::is_same_v<Source, int32_t> || std::is_same_v<Source, uint32_t>, |
Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 172 | "Source must be int32_t or uint32_t."); |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 173 | static_assert(std::is_pointer_v<Dest>, "Dest must be a pointer."); |
Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 174 | // Check that we don't lose any non-0 bits here. |
| 175 | static_assert(sizeof(uintptr_t) >= sizeof(Source), "Expecting at least 32-bit pointers."); |
| 176 | return reinterpret_cast<Dest>(static_cast<uintptr_t>(static_cast<uint32_t>(source))); |
| 177 | } |
| 178 | |
| 179 | template <typename Dest, typename Source> |
| 180 | inline Dest reinterpret_cast32(Source* ptr) { |
| 181 | // This is the overload for casting from a pointer to int32_t/uint32_t. |
Vladimir Marko | 495311c | 2022-04-14 10:59:53 +0000 | [diff] [blame] | 182 | static_assert(std::is_same_v<Dest, int32_t> || std::is_same_v<Dest, uint32_t>, |
Vladimir Marko | 8e524ad | 2018-07-13 10:27:43 +0100 | [diff] [blame] | 183 | "Dest must be int32_t or uint32_t."); |
| 184 | static_assert(sizeof(uintptr_t) >= sizeof(Dest), "Expecting at least 32-bit pointers."); |
| 185 | return static_cast<Dest>(dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(ptr))); |
| 186 | } |
| 187 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 188 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 189 | |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 190 | #endif // ART_LIBARTBASE_BASE_CASTS_H_ |