Skip to content

Allow expr_dynamic_cast to be used with std_code types #1441

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 7 commits into from
Oct 6, 2017
Merged
Changes from 1 commit
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
136 changes: 111 additions & 25 deletions src/util/expr_cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ Author: Nathan Phillips <[email protected]>
/// \return true if \a base is of type \a T
template<typename T> bool can_cast_expr(const exprt &base);

/// Called after casting. Provides a point to assert on the structure of the
/// Called after casting. Provides a point to assert on the structure of the
/// expr. By default, this is a no-op, but you can provide an overload to
/// validate particular types.
/// validate particular types. Should always succeed unless the program has
/// entered an invalid state. We validate objects at cast time as that is when
/// these checks have been used historically, but it would be reasonable to
/// validate objects in this way at any time.
inline void validate_expr(const exprt &) {}

namespace detail // NOLINT
{

// We hide this in a namespace so that only functions that it only
// participates in overload resolution when explicitly requested.
// We hide these functions in a namespace so that only functions that they only
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

English. Perhaps "so that functions only"?

// participate in overload resolution when explicitly requested.

/// \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 TUnderlying An exprt-derived class type
/// \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
Expand All @@ -54,22 +56,39 @@ template <typename T, typename TExpr>
optionalt<std::reference_wrapper<typename std::remove_reference<T>::type>>
expr_try_dynamic_cast(TExpr &base)
{
typedef typename std::decay<T>::type TUnderlying;
typedef typename std::remove_reference<T>::type TConst;
typedef typename std::decay<T>::type TUnderlyingt;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally wouldn't start typedefs with a capital T, perhaps underlying_typet?

typedef typename std::remove_reference<T>::type TConstt;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth creating a typedef rather than just using it inline?

static_assert(
std::is_same<typename std::remove_const<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, TUnderlying>::value,
std::is_base_of<exprt, TUnderlyingt>::value,
"The template argument T must be derived from exprt.");
if(!can_cast_expr<TUnderlying>(base))
if(!can_cast_expr<TUnderlyingt>(base))
return {};
T value=static_cast<T>(base);
validate_expr(value);
return std::reference_wrapper<TConst>(value);
T ret=static_cast<T>(base);
validate_expr(ret);
return std::reference_wrapper<TConstt>(ret);
}

/// \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
template <typename T, typename TExpr>
optionalt<std::reference_wrapper<typename std::remove_reference<T>::type>>
expr_try_checked_cast(TExpr &base)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This operation doesn't make sense. If you assert your check will pass then why would you force yourself to write code to handle the case that you're saying can't happen?

{
typedef typename std::decay<T>::type TUnderlyingt;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth creating a typedef rather than just using it inline?

PRECONDITION(can_cast_expr<TUnderlyingt>(base));
return expr_try_checked_cast<T>(base);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this infinite recursion?

}

} // namespace detail
Expand Down Expand Up @@ -100,40 +119,79 @@ expr_try_dynamic_cast(exprt &base)
return detail::expr_try_dynamic_cast<T>(base);
}

/// \brief Try to cast a constant reference to a generic exprt to a specific
/// derived class. Also assert that the expr 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 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_checked_cast(const exprt &base)
{
return detail::expr_try_checked_cast<T>(base);
}

/// \brief Try to cast a reference to a generic exprt to a specific derived
/// class. Also assert that the expr 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 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_checked_cast(exprt &base)
{
return detail::expr_try_checked_cast<T>(base);
}

namespace detail // NOLINT
{

// We hide this in a namespace so that only functions that it only
// participates in overload resolution when explicitly requested.
// We hide these functions in a namespace so that only functions that they only
// participate in overload resolution when explicitly requested.

/// \brief Cast a reference to a generic exprt to a specific derived class
/// \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
/// \tparam TUnderlying An exprt-derived class type
/// \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 T
/// \throw std::bad_cast If \a base is not an instance of \a TUnderlying
/// \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_dynamic_cast(TExpr &base)
{
typedef typename std::decay<T>::type TUnderlying;
typedef typename std::decay<T>::type TUnderlyingt;
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, TUnderlying>::value,
std::is_base_of<exprt, TUnderlyingt>::value,
"The template argument T must be derived from exprt.");
PRECONDITION(can_cast_expr<TUnderlying>(base));
if(!can_cast_expr<TUnderlying>(base))
if(!can_cast_expr<TUnderlyingt>(base))
throw std::bad_cast();
T value=static_cast<T>(base);
validate_expr(value);
return value;
T ret=static_cast<T>(base);
validate_expr(ret);
return ret;
}

/// \brief Cast a reference to a generic exprt to a specific derived class.
/// Also assert that the expression has the expected type.
/// \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 T
/// \throw std::bad_cast If \a base is not an instance of \a TUnderlying
/// \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)
{
typedef typename std::decay<T>::type TUnderlyingt;
PRECONDITION(can_cast_expr<TUnderlyingt>(base));
return expr_dynamic_cast<T>(base);
}

} // namespace detail
Expand Down Expand Up @@ -165,6 +223,34 @@ 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