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