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 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_CASTS_H_ |
| 18 | #define ART_RUNTIME_BASE_CASTS_H_ |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 19 | |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 20 | #include <assert.h> |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 21 | #include <limits> |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 22 | #include <stdint.h> |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 23 | #include <string.h> |
Andreas Gampe | eafdb96 | 2014-10-23 11:24:08 -0700 | [diff] [blame] | 24 | #include <type_traits> |
| 25 | |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 26 | #include "base/logging.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 27 | #include "base/macros.h" |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 28 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 29 | namespace art { |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 30 | |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 31 | // Use implicit_cast as a safe version of static_cast or const_cast |
| 32 | // for upcasting in the type hierarchy (i.e. casting a pointer to Foo |
| 33 | // to a pointer to SuperclassOfFoo or casting a pointer to Foo to |
| 34 | // a const pointer to Foo). |
| 35 | // When you use implicit_cast, the compiler checks that the cast is safe. |
| 36 | // Such explicit implicit_casts are necessary in surprisingly many |
| 37 | // situations where C++ demands an exact type match instead of an |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 38 | // argument type convertible to a target type. |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 39 | // |
| 40 | // The From type can be inferred, so the preferred syntax for using |
| 41 | // implicit_cast is the same as for static_cast etc.: |
| 42 | // |
| 43 | // implicit_cast<ToType>(expr) |
| 44 | // |
| 45 | // implicit_cast would have been part of the C++ standard library, |
| 46 | // but the proposal was submitted too late. It will probably make |
| 47 | // its way into the language in the future. |
| 48 | template<typename To, typename From> |
| 49 | inline To implicit_cast(From const &f) { |
| 50 | return f; |
| 51 | } |
| 52 | |
| 53 | // When you upcast (that is, cast a pointer from type Foo to type |
| 54 | // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts |
| 55 | // always succeed. When you downcast (that is, cast a pointer from |
| 56 | // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because |
| 57 | // how do you know the pointer is really of type SubclassOfFoo? It |
| 58 | // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, |
| 59 | // when you downcast, you should use this macro. In debug mode, we |
| 60 | // use dynamic_cast<> to double-check the downcast is legal (we die |
| 61 | // if it's not). In normal mode, we do the efficient static_cast<> |
| 62 | // instead. Thus, it's important to test in debug mode to make sure |
| 63 | // the cast is legal! |
| 64 | // This is the only place in the code we should use dynamic_cast<>. |
| 65 | // In particular, you SHOULDN'T be using dynamic_cast<> in order to |
| 66 | // do RTTI (eg code like this: |
| 67 | // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo); |
| 68 | // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo); |
| 69 | // You should design the code some other way not to need this. |
| 70 | |
| 71 | template<typename To, typename From> // use like this: down_cast<T*>(foo); |
| 72 | inline To down_cast(From* f) { // so we only accept pointers |
Andreas Gampe | eafdb96 | 2014-10-23 11:24:08 -0700 | [diff] [blame] | 73 | static_assert(std::is_base_of<From, typename std::remove_pointer<To>::type>::value, |
| 74 | "down_cast unsafe as To is not a subtype of From"); |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 75 | |
Carl Shapiro | 6d860c1 | 2011-07-20 16:40:16 -0700 | [diff] [blame] | 76 | return static_cast<To>(f); |
| 77 | } |
| 78 | |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 79 | template <class Dest, class Source> |
| 80 | inline Dest bit_cast(const Source& source) { |
| 81 | // Compile time assertion: sizeof(Dest) == sizeof(Source) |
| 82 | // A compile error here means your Dest and Source have different sizes. |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 83 | static_assert(sizeof(Dest) == sizeof(Source), "sizes should be equal"); |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 84 | Dest dest; |
| 85 | memcpy(&dest, &source, sizeof(dest)); |
| 86 | return dest; |
| 87 | } |
| 88 | |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 89 | // A version of static_cast that DCHECKs that the value can be precisely represented |
| 90 | // when converting to Dest. |
| 91 | template <typename Dest, typename Source> |
| 92 | inline Dest dchecked_integral_cast(const Source source) { |
| 93 | DCHECK( |
| 94 | // Check that the value is within the lower limit of Dest. |
| 95 | (static_cast<intmax_t>(std::numeric_limits<Dest>::min()) <= |
| 96 | static_cast<intmax_t>(std::numeric_limits<Source>::min()) || |
| 97 | source >= static_cast<Source>(std::numeric_limits<Dest>::min())) && |
| 98 | // Check that the value is within the upper limit of Dest. |
| 99 | (static_cast<uintmax_t>(std::numeric_limits<Dest>::max()) >= |
| 100 | static_cast<uintmax_t>(std::numeric_limits<Source>::max()) || |
Andreas Gampe | 8cf00fa | 2017-04-28 20:50:19 -0700 | [diff] [blame^] | 101 | source <= static_cast<Source>(std::numeric_limits<Dest>::max()))) |
| 102 | << "dchecked_integral_cast failed for " << source |
| 103 | << " (would be " << static_cast<Dest>(source) << ")"; |
Andreas Gampe | e34a42c | 2015-04-25 14:44:29 -0700 | [diff] [blame] | 104 | |
| 105 | return static_cast<Dest>(source); |
| 106 | } |
| 107 | |
Vladimir Marko | db8e62d | 2016-03-30 16:30:21 +0100 | [diff] [blame] | 108 | // A version of reinterpret_cast<>() between pointers and int64_t/uint64_t |
| 109 | // that goes through uintptr_t to avoid treating the pointer as "signed." |
| 110 | |
| 111 | template <typename Dest, typename Source> |
| 112 | inline Dest reinterpret_cast64(Source source) { |
| 113 | // This is the overload for casting from int64_t/uint64_t to a pointer. |
| 114 | static_assert(std::is_same<Source, int64_t>::value || std::is_same<Source, uint64_t>::value, |
| 115 | "Source must be int64_t or uint64_t."); |
| 116 | static_assert(std::is_pointer<Dest>::value, "Dest must be a pointer."); |
| 117 | // Check that we don't lose any non-0 bits here. |
| 118 | DCHECK_EQ(static_cast<Source>(static_cast<uintptr_t>(source)), source); |
| 119 | return reinterpret_cast<Dest>(static_cast<uintptr_t>(source)); |
| 120 | } |
| 121 | |
| 122 | template <typename Dest, typename Source> |
| 123 | inline Dest reinterpret_cast64(Source* ptr) { |
| 124 | // This is the overload for casting from a pointer to int64_t/uint64_t. |
| 125 | static_assert(std::is_same<Dest, int64_t>::value || std::is_same<Dest, uint64_t>::value, |
| 126 | "Dest must be int64_t or uint64_t."); |
| 127 | static_assert(sizeof(uintptr_t) <= sizeof(Dest), "Expecting at most 64-bit pointers."); |
| 128 | return static_cast<Dest>(reinterpret_cast<uintptr_t>(ptr)); |
| 129 | } |
| 130 | |
Carl Shapiro | 6b6b5f0 | 2011-06-21 15:05:09 -0700 | [diff] [blame] | 131 | } // namespace art |
Carl Shapiro | a5d5cfd | 2011-06-21 12:46:59 -0700 | [diff] [blame] | 132 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 133 | #endif // ART_RUNTIME_BASE_CASTS_H_ |