9 #ifndef CPROVER_UTIL_EXPR_CAST_H
10 #define CPROVER_UTIL_EXPR_CAST_H
16 #include <type_traits>
54 template<
typename Ret,
typename T>
58 !std::is_reference<Ret>::value,
59 "Ret must not be a reference, i.e. expr_try_dynamic_cast<const thingt> "
60 "rather than expr_try_dynamic_cast<const thing &>");
63 typename std::conditional<
64 std::is_const<T>::value,
65 typename std::add_const<Ret>::type,
80 template <
typename T,
typename TExpr>
88 std::is_base_of<
exprt,
typename std::decay<TExpr>::type>::value,
89 "Tried to expr_try_dynamic_cast from something that wasn't an exprt");
91 std::is_base_of<exprt, T>::value,
92 "The template argument T must be derived from exprt.");
95 const auto ret=
static_cast<returnt
>(&base);
106 template <
typename T,
typename TExpr>
111 "This template overload must only match where base is an rvalue.");
113 std::is_base_of<
exprt,
typename std::decay<TExpr>::type>::value,
114 "Tried to expr_try_dynamic_cast from something that wasn't an exprt.");
116 std::is_base_of<exprt, T>::value,
117 "The template argument T must be derived from exprt.");
118 static_assert(!std::is_const<TExpr>::value,
"Attempted to move from const.");
119 if(!can_cast_expr<T>(base))
121 std::optional<T> ret{
static_cast<T &&
>(base)};
134 template <
typename T,
typename TType>
141 std::is_base_of<
typet,
typename std::decay<TType>::type>::value,
142 "Tried to type_try_dynamic_cast from something that wasn't an typet");
144 std::is_base_of<typet, T>::value,
145 "The template argument T must be derived from typet.");
146 if(!
can_cast_type<
typename std::remove_const<T>::type>(base))
149 return static_cast<returnt
>(&base);
158 template <
typename T,
typename TType>
163 "This template overload must only match where base is an rvalue.");
165 std::is_base_of<
typet,
typename std::decay<TType>::type>::value,
166 "Tried to type_try_dynamic_cast from something that wasn't an typet.");
168 std::is_base_of<typet, T>::value,
169 "The template argument T must be derived from typet.");
170 static_assert(!std::is_const<TType>::value,
"Attempted to move from const.");
171 if(!can_cast_type<T>(base))
174 std::optional<T> ret{
static_cast<T &&
>(base)};
181 template<
typename Ret,
typename T>
185 !std::is_reference<Ret>::value,
186 "Ret must not be a reference, i.e. expr_dynamic_cast<const thingt> rather "
187 "than expr_dynamic_cast<const thing &>");
190 typename std::conditional<
191 std::is_const<T>::value,
192 typename std::add_const<Ret>::type,
206 template<
typename T,
typename TExpr>
210 const auto ret=expr_try_dynamic_cast<T>(base);
212 throw std::bad_cast();
226 template<
typename T,
typename TExpr>
231 return expr_dynamic_cast<T>(base);
241 template <
typename T,
typename TType>
245 auto result = type_try_dynamic_cast<T>(base);
254 bool allow_more=
false)
Base class for all expressions.
The type of an expression, extends irept.
auto expr_checked_cast(TExpr &base) -> typename detail::expr_dynamic_cast_return_typet< T, TExpr >::type
Cast a reference to a generic exprt to a specific derived class.
bool can_cast_expr(const exprt &base)
Check whether a reference to a generic exprt is of a specific derived class.
auto type_checked_cast(TType &base) -> typename detail::expr_dynamic_cast_return_typet< T, TType >::type
Cast a reference to a generic typet to a specific derived class and checks that the type could be con...
auto type_try_dynamic_cast(TType &base) -> typename detail::expr_try_dynamic_cast_return_typet< T, TType >::type
Try to cast a reference to a generic typet to a specific derived class.
void validate_operands(const exprt &value, exprt::operandst::size_type number, const char *message, bool allow_more=false)
void validate_expr(const exprt &)
Called after casting.
bool can_cast_type(const typet &base)
Check whether a reference to a generic typet is of a specific derived class.
auto expr_try_dynamic_cast(TExpr &base) -> typename detail::expr_try_dynamic_cast_return_typet< T, TExpr >::type
Try to cast a reference to a generic exprt to a specific derived class.
auto expr_dynamic_cast(TExpr &base) -> typename detail::expr_dynamic_cast_return_typet< T, TExpr >::type
Cast a reference to a generic exprt to a specific derived class.
bool is_rvalue_reference(const typet &type)
Returns if the type is an R value reference.
#define CHECK_RETURN(CONDITION)
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
#define PRECONDITION(CONDITION)
std::conditional< std::is_const< T >::value, typename std::add_const< Ret >::type, Ret >::type & type
std::conditional< std::is_const< T >::value, typename std::add_const< Ret >::type, Ret >::type * type