8 #ifndef INCLUDE_UTIL_CASTING_H_
9 #define INCLUDE_UTIL_CASTING_H_
28 #include <type_traits>
34 #ifndef LLVM_HAS_CPP_ATTRIBUTE
35 #if defined(__cplusplus) && defined(__has_cpp_attribute)
36 # define LLVM_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
38 # define LLVM_HAS_CPP_ATTRIBUTE(x) 0
45 #if defined(__cplusplus) && __cplusplus > 201402L && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
46 #define LLVM_NODISCARD [[nodiscard]]
47 #elif LLVM_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
48 #define LLVM_NODISCARD [[clang::warn_unused_result]]
53 #elif defined(__GNUC__) && LLVM_HAS_CPP_ATTRIBUTE(nodiscard)
54 #define LLVM_NODISCARD [[nodiscard]]
56 #define LLVM_NODISCARD
69 template<
typename T,
typename Enable =
void>
78 using type =
const std::remove_pointer_t<T> *;
82 template<
typename T,
typename Enable =
void>
90 T, std::enable_if_t<std::is_pointer<T>::value>>
131 template <
typename To,
typename From,
typename Enabler =
void>
134 static inline bool doit(
const From &Val)
136 return To::classof(&Val);
141 template <
typename To,
typename From>
142 struct isa_impl<To, From, std::enable_if_t<std::is_base_of<To, From>::value>>
144 static inline bool doit(
const From &)
152 static inline bool doit(
const From &Val)
158 template <
typename To,
typename From>
struct isa_impl_cl<To, const From>
160 static inline bool doit(
const From &Val)
166 template <
typename To,
typename From>
169 static inline bool doit(
const std::unique_ptr<From> &Val)
171 assert(Val &&
"SVFUtil::isa<> used on a null pointer");
176 template <
typename To,
typename From>
struct isa_impl_cl<To, From*>
178 static inline bool doit(
const From *Val)
180 assert(Val &&
"SVFUtil::isa<> used on a null pointer");
185 template <
typename To,
typename From>
struct isa_impl_cl<To, From*
const>
187 static inline bool doit(
const From *Val)
189 assert(Val &&
"SVFUtil::isa<> used on a null pointer");
194 template <
typename To,
typename From>
struct isa_impl_cl<To, const From*>
196 static inline bool doit(
const From *Val)
198 assert(Val &&
"SVFUtil::isa<> used on a null pointer");
203 template <
typename To,
typename From>
struct isa_impl_cl<To, const From*
const>
205 static inline bool doit(
const From *Val)
207 assert(Val &&
"SVFUtil::isa<> used on a null pointer");
212 template<
typename To,
typename From,
typename SimpleFrom>
217 static bool doit(
const From &Val)
225 template<
typename To,
typename FromTy>
229 static bool doit(
const FromTy &Val)
247 template <
typename First,
typename Second,
typename... Rest,
typename Y>
250 return SVFUtil::isa<First>(Val) ||
SVFUtil::isa<Second, Rest...>(Val);
257 template<
class To,
class From>
struct cast_retty;
285 template <
class To,
class From>
296 template<
class To,
class From,
class SimpleFrom>
305 template<
class To,
class FromTy>
312 template<
class To,
class From>
347 std::is_same<X, typename simplify_type<X>::SimpleType>
::value;
357 template <
class X,
class Y>
358 inline std::enable_if_t<!is_simple_type<Y>::value,
362 assert(SVFUtil::isa<X>(Val) &&
"cast<Ty>() argument of incompatible type!");
367 template <
class X,
class Y>
370 assert(SVFUtil::isa<X>(Val) &&
"cast<Ty>() argument of incompatible type!");
375 template <
class X,
class Y>
378 assert(SVFUtil::isa<X>(Val) &&
"cast<Ty>() argument of incompatible type!");
383 template <
class X,
class Y>
384 inline typename cast_retty<X, std::unique_ptr<Y>>::ret_type
387 assert(SVFUtil::isa<X>(Val.get()) &&
"cast<Ty>() argument of incompatible type!");
402 template <
class X,
class Y>
407 return SVFUtil::isa<X>(Val) ? SVFUtil::cast<X>(Val) :
nullptr;
410 template <
class X,
class Y>
413 return SVFUtil::isa<X>(Val) ? SVFUtil::cast<X>(Val) :
nullptr;
416 template <
class X,
class Y>
419 return SVFUtil::isa<X>(Val) ? SVFUtil::cast<X>(Val) :
nullptr;
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > cast(const Y &Val)
LLVM_NODISCARD bool isa(const Y &Val)
LLVM_NODISCARD std::enable_if_t<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type > dyn_cast(const Y &Val)
const std::remove_pointer_t< T > * type
If T is a pointer, just return it. If it is not, return T&.
static cast_retty< To, FromTy >::ret_type doit(const FromTy &Val)
static cast_retty< To, From >::ret_type doit(From &Val)
typename std::remove_pointer< PointerType >::type ResultType
std::unique_ptr< ResultType > ret_type
typename cast_retty_impl< To, From * >::ret_type PointerType
typename cast_retty_impl< To, FromTy >::ret_type ret_type
typename cast_retty< To, SimpleFrom >::ret_type ret_type
typename cast_retty_wrap< To, From, typename simplify_type< From >::SimpleType >::ret_type ret_type
static bool doit(const From &)
static bool doit(const From *Val)
static bool doit(const From *Val)
static bool doit(const From &Val)
static bool doit(const From *Val)
static bool doit(const From *Val)
static bool doit(const std::unique_ptr< From > &Val)
static bool doit(const From &Val)
static bool doit(const FromTy &Val)
static bool doit(const From &Val)
static bool doit(const From &Val)
typename SVFUtil::add_const_past_pointer< NonConstSimpleType >::type SimpleType
typename simplify_type< From >::SimpleType NonConstSimpleType
typename SVFUtil::add_lvalue_reference_if_not_pointer< SimpleType >::type RetType
static RetType getSimplifiedValue(const From &Val)
static SimpleType & getSimplifiedValue(From &Val)