Skip to content

Return pointer from expr_try_dynamic_cast #1466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 52 additions & 120 deletions src/util/expr_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Author: Nathan Phillips <[email protected]>

#include "invariant.h"
#include "expr.h"
#include "optional.h"

/// \brief Check whether a reference to a generic \ref exprt is of a specific
/// derived class.
Expand All @@ -41,71 +40,70 @@ inline void validate_expr(const exprt &) {}
namespace detail // NOLINT
{

// We hide these functions in a namespace so that they only participate in
// overload resolution when explicitly requested.
template<typename Ret, typename T>
struct expr_try_dynamic_cast_return_typet final
{
static_assert(
!std::is_reference<Ret>::value,
"Ret must be non-qualified");

typedef
typename std::conditional<
std::is_const<T>::value,
typename std::add_const<Ret>::type,
Ret>::type *
type;
};

} // namespace detail

/// \brief Try to cast a reference to a generic exprt to a specific derived
/// class
/// \tparam T The reference or const reference type to \a TUnderlying to cast
/// to
/// \tparam TExpr The original type to cast from, either exprt or const exprt
/// \param base Reference to a generic \ref exprt
/// \return Reference to object of type \a TUnderlying
/// or valueless optional if \a base is not an instance of \a TUnderlying
/// \return Ptr to object of type \a TUnderlying
/// or nullptr if \a base is not an instance of \a TUnderlying
template <typename T, typename TExpr>
optionalt<std::reference_wrapper<typename std::remove_reference<T>::type>>
expr_try_dynamic_cast(TExpr &base)
auto expr_try_dynamic_cast(TExpr &base)
-> typename detail::expr_try_dynamic_cast_return_typet<T, TExpr>::type
{
typedef typename std::decay<T>::type decayt;
typedef
typename detail::expr_try_dynamic_cast_return_typet<T, TExpr>::type
returnt;
static_assert(
std::is_same<typename std::remove_const<TExpr>::type, exprt>::value,
std::is_same<typename std::decay<TExpr>::type, exprt>::value,
"Tried to expr_try_dynamic_cast from something that wasn't an exprt");
static_assert(
std::is_reference<T>::value,
"Tried to convert exprt & to non-reference type");
static_assert(
std::is_base_of<exprt, decayt>::value,
std::is_base_of<exprt, T>::value,
"The template argument T must be derived from exprt.");
if(!can_cast_expr<decayt>(base))
return {};
T ret=static_cast<T>(base);
validate_expr(ret);
return std::reference_wrapper<typename std::remove_reference<T>::type>(ret);
if(!can_cast_expr<T>(base))
return nullptr;
const auto ret=static_cast<returnt>(&base);
validate_expr(*ret);
return ret;
}

} // namespace detail

/// \brief Try to cast a constant reference to a generic exprt to a specific
/// derived class
/// \tparam T The exprt-derived class to cast to
/// \param base Reference to a generic \ref exprt
/// \return Reference to object of type \a T or valueless optional if \a base
/// is not an instance of \a T
template<typename T>
optionalt<std::reference_wrapper<typename std::remove_reference<T>::type>>
expr_try_dynamic_cast(const exprt &base)
namespace detail // NOLINT
{
return detail::expr_try_dynamic_cast<T>(base);
}

/// \brief Try to cast a reference to a generic exprt to a specific derived
/// class
/// \tparam T The exprt-derived class to cast to
/// \param base Reference to a generic \ref exprt
/// \return Reference to object of type \a T or valueless optional if \a base is
/// not an instance of \a T
template<typename T>
optionalt<std::reference_wrapper<typename std::remove_reference<T>::type>>
expr_try_dynamic_cast(exprt &base)
template<typename Ret, typename T>
struct expr_dynamic_cast_return_typet final
{
return detail::expr_try_dynamic_cast<T>(base);
}
static_assert(
!std::is_reference<Ret>::value,
"Ret must be non-qualified");

namespace detail // NOLINT
{
typedef
typename std::conditional<
std::is_const<T>::value,
typename std::add_const<Ret>::type,
Ret>::type &
type;
};

// We hide these functions in a namespace so that they only participate in
// overload resolution when explicitly requested.
} // namespace detail

/// \brief Cast a reference to a generic exprt to a specific derived class.
/// \tparam T The reference or const reference type to \a TUnderlying to cast to
Expand All @@ -114,23 +112,13 @@ namespace detail // NOLINT
/// \return Reference to object of type \a T
/// \throw std::bad_cast If \a base is not an instance of \a TUnderlying
template<typename T, typename TExpr>
T expr_dynamic_cast(TExpr &base)
auto expr_dynamic_cast(TExpr &base)
-> typename detail::expr_dynamic_cast_return_typet<T, TExpr>::type
{
typedef typename std::decay<T>::type decayt;
static_assert(
std::is_same<typename std::remove_const<TExpr>::type, exprt>::value,
"Tried to expr_dynamic_cast from something that wasn't an exprt");
static_assert(
std::is_reference<T>::value,
"Tried to convert exprt & to non-reference type");
static_assert(
std::is_base_of<exprt, decayt>::value,
"The template argument T must be derived from exprt.");
if(!can_cast_expr<decayt>(base))
const auto ret=expr_try_dynamic_cast<T>(base);
if(ret==nullptr)
throw std::bad_cast();
T ret=static_cast<T>(base);
validate_expr(ret);
return ret;
return *ret;
}

/// \brief Cast a reference to a generic exprt to a specific derived class.
Expand All @@ -143,69 +131,13 @@ T expr_dynamic_cast(TExpr &base)
/// \remark If CBMC assertions (PRECONDITION) are set to abort then this will
/// abort rather than throw if \a base is not an instance of \a TUnderlying
template<typename T, typename TExpr>
T expr_checked_cast(TExpr &base)
auto expr_checked_cast(TExpr &base)
-> typename detail::expr_dynamic_cast_return_typet<T, TExpr>::type
{
PRECONDITION(can_cast_expr<typename std::decay<T>::type>(base));
PRECONDITION(can_cast_expr<T>(base));
return expr_dynamic_cast<T>(base);
}

} // namespace detail

/// \brief Cast a constant reference to a generic exprt to a specific derived
/// class
/// \tparam T The exprt-derived class to cast to
/// \param base Reference to a generic \ref exprt
/// \return Reference to object of type \a T
/// \throw std::bad_cast If \a base is not an instance of \a T
/// \remark If CBMC assertions (PRECONDITION) are set to abort then this will
/// abort rather than throw if \a base is not an instance of \a T
template<typename T>
T expr_dynamic_cast(const exprt &base)
{
return detail::expr_dynamic_cast<T>(base);
}

/// \brief Cast a reference to a generic exprt to a specific derived class
/// \tparam T The exprt-derived class to cast to
/// \param base Reference to a generic \ref exprt
/// \return Reference to object of type \a T
/// \throw std::bad_cast If \a base is not an instance of \a T
/// \remark If CBMC assertions (PRECONDITION) are set to abort then this will
/// abort rather than throw if \a base is not an instance of \a T
template<typename T>
T expr_dynamic_cast(exprt &base)
{
return detail::expr_dynamic_cast<T>(base);
}

/// \brief Cast a constant reference to a generic exprt to a specific derived
/// class. Also assert that the exprt invariants are not violated.
/// \tparam T The exprt-derived class to cast to
/// \param base Reference to a generic \ref exprt
/// \return Reference to object of type \a T
/// \throw std::bad_cast If \a base is not an instance of \a T
/// \remark If CBMC assertions (PRECONDITION) are set to abort then this will
/// abort rather than throw if \a base is not an instance of \a T
template<typename T>
T expr_checked_cast(const exprt &base)
{
return detail::expr_checked_cast<T>(base);
}

/// \brief Cast a reference to a generic exprt to a specific derived class.
/// Also assert that the exprt invariants are not violated.
/// \tparam T The exprt-derived class to cast to
/// \param base Reference to a generic \ref exprt
/// \return Reference to object of type \a T
/// \throw std::bad_cast If \a base is not an instance of \a T
/// \remark If CBMC assertions (PRECONDITION) are set to abort then this will
/// abort rather than throw if \a base is not an instance of \a T
template<typename T>
T expr_checked_cast(exprt &base)
{
return detail::expr_checked_cast<T>(base);
}

inline void validate_operands(
const exprt &value,
exprt::operandst::size_type number,
Expand Down
5 changes: 2 additions & 3 deletions src/util/std_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ inline bool can_cast_code_impl(const exprt &expr, const Tag &tag)
{
try
{
return expr_dynamic_cast<const codet &>(expr).get_statement()==tag;
return expr_dynamic_cast<codet>(expr).get_statement()==tag;
}
catch(const std::bad_cast &)
{
Expand Down Expand Up @@ -1246,8 +1246,7 @@ inline bool can_cast_side_effect_expr_impl(const exprt &expr, const Tag &tag)
{
try
{
return
expr_dynamic_cast<const side_effect_exprt &>(expr).get_statement()==tag;
return expr_dynamic_cast<side_effect_exprt>(expr).get_statement()==tag;
}
catch(const std::bad_cast &)
{
Expand Down
18 changes: 9 additions & 9 deletions unit/util/expr_cast/expr_cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ SCENARIO("expr_dynamic_cast",
THEN("Try-casting from exprt reference to symbol_exprt pointer "
"returns a value")
{
REQUIRE(expr_try_dynamic_cast<const symbol_exprt &>(expr).has_value());
REQUIRE(expr_try_dynamic_cast<symbol_exprt>(expr)!=nullptr);
}

THEN("Casting from exprt pointer to transt pointer doesn't return a value")
{
REQUIRE(!expr_try_dynamic_cast<const transt &>(expr).has_value());
REQUIRE(expr_try_dynamic_cast<transt>(expr)==nullptr);
}
}
GIVEN("A exprt reference to a symbolt")
Expand All @@ -38,13 +38,13 @@ SCENARIO("expr_dynamic_cast",
THEN("Casting from exprt reference to symbol_exprt reference "
"returns a value")
{
REQUIRE(expr_try_dynamic_cast<symbol_exprt &>(expr).has_value());
REQUIRE(expr_try_dynamic_cast<symbol_exprt>(expr)!=nullptr);
}

THEN("Casting from exprt reference to transt reference "
"doesn't return a value")
{
REQUIRE(!expr_try_dynamic_cast<transt &>(expr).has_value());
REQUIRE(expr_try_dynamic_cast<transt>(expr)==nullptr);
}
}
GIVEN("A const exprt reference to a symbolt")
Expand All @@ -54,15 +54,15 @@ SCENARIO("expr_dynamic_cast",
THEN(
"Casting from exprt reference to symbol_exprt reference should not throw")
{
REQUIRE_NOTHROW(expr_dynamic_cast<const symbol_exprt &>(expr_ref));
REQUIRE_NOTHROW(expr_dynamic_cast<symbol_exprt>(expr_ref));
}

THEN("Casting from exprt reference to transt reference should throw")
{
// This no longer throws exceptions when our custom asserts are set to
// abort the program
// REQUIRE_THROWS_AS(
// expr_dynamic_cast<const transt &>(expr_ref),
// expr_dynamic_cast<transt>(expr_ref),
// std::bad_cast);
}
}
Expand All @@ -73,23 +73,23 @@ SCENARIO("expr_dynamic_cast",
THEN(
"Casting from exprt reference to symbol_exprt reference should not throw")
{
REQUIRE_NOTHROW(expr_dynamic_cast<symbol_exprt &>(expr_ref));
REQUIRE_NOTHROW(expr_dynamic_cast<symbol_exprt>(expr_ref));
}

THEN("Casting from exprt reference to transt reference should throw")
{
// This no longer throws exceptions when our custom asserts are set to
// abort the program
// REQUIRE_THROWS_AS(
// expr_dynamic_cast<transt &>(expr_ref),
// expr_dynamic_cast<transt>(expr_ref),
// std::bad_cast);
}

THEN(
"Casting from non-const exprt reference to const symbol_exprt reference "
"should be fine")
{
REQUIRE_NOTHROW(expr_dynamic_cast<const symbol_exprt &>(expr_ref));
REQUIRE_NOTHROW(expr_dynamic_cast<symbol_exprt>(expr_ref));
}
}
}