|
1 |
| -/* |
2 |
| - Author: Nathan Phillips <[email protected]> |
3 |
| -*/ |
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: |
| 4 | +
|
| 5 | +Author: Nathan Phillips <[email protected]> |
| 6 | +
|
| 7 | +\*******************************************************************/ |
4 | 8 |
|
5 | 9 | #ifndef CPROVER_UTIL_EXPR_CAST_H
|
6 | 10 | #define CPROVER_UTIL_EXPR_CAST_H
|
|
10 | 14 |
|
11 | 15 | #include <typeinfo>
|
12 | 16 | #include <type_traits>
|
| 17 | +#include <functional> |
| 18 | + |
13 | 19 | #include "invariant.h"
|
14 | 20 | #include "expr.h"
|
15 |
| - |
| 21 | +#include "optional.h" |
16 | 22 |
|
17 | 23 | /// \brief Check whether a reference to a generic \ref exprt is of a specific
|
18 | 24 | /// derived class.
|
|
25 | 31 | template<typename T> bool can_cast_expr(const exprt &base);
|
26 | 32 |
|
27 | 33 |
|
28 |
| -/// \brief Cast a constant pointer to a generic exprt to a specific derived |
29 |
| -/// class |
| 34 | +/// \brief Try to cast a constant reference to a generic exprt to a specific |
| 35 | +/// derived class |
30 | 36 | /// \tparam T The exprt-derived class to cast to
|
31 |
| -/// \param base Pointer to a generic \ref exprt |
32 |
| -/// \return Pointer to object of type \a T or null if \a base is not an |
33 |
| -/// instance of \a T |
| 37 | +/// \param base Reference to a generic \ref exprt |
| 38 | +/// \return Reference to object of type \a T or valueless optional if \a base |
| 39 | +/// is not an instance of \a T |
34 | 40 | template<typename T>
|
35 |
| -T expr_dynamic_cast(const exprt *base) |
| 41 | +optionalt<std::reference_wrapper<typename std::remove_reference<T>::type>> |
| 42 | +expr_try_dynamic_cast(const exprt &base) |
36 | 43 | {
|
37 |
| - return expr_dynamic_cast< |
| 44 | + return expr_try_dynamic_cast< |
38 | 45 | T,
|
39 |
| - typename std::remove_const<typename std::remove_pointer<T>::type>::type, |
| 46 | + typename std::remove_reference<T>::type, |
| 47 | + typename std::remove_const<typename std::remove_reference<T>::type>::type, |
40 | 48 | const exprt>(base);
|
41 | 49 | }
|
42 | 50 |
|
43 |
| -/// \brief Cast a pointer to a generic exprt to a specific derived class |
| 51 | +/// \brief Try to cast a reference to a generic exprt to a specific derived |
| 52 | +/// class |
44 | 53 | /// \tparam T The exprt-derived class to cast to
|
45 |
| -/// \param base Pointer to a generic \ref exprt |
46 |
| -/// \return Pointer to object of type \a T or null if \a base is not an |
47 |
| -/// instance of \a T |
| 54 | +/// \param base Reference to a generic \ref exprt |
| 55 | +/// \return Reference to object of type \a T or valueless optional if \a base is |
| 56 | +/// not an instance of \a T |
48 | 57 | template<typename T>
|
49 |
| -T expr_dynamic_cast(exprt *base) |
| 58 | +optionalt<std::reference_wrapper<typename std::remove_reference<T>::type>> |
| 59 | +expr_try_dynamic_cast(exprt &base) |
50 | 60 | {
|
51 |
| - return expr_dynamic_cast< |
| 61 | + return expr_try_dynamic_cast< |
52 | 62 | T,
|
53 |
| - typename std::remove_const<typename std::remove_pointer<T>::type>::type, |
| 63 | + typename std::remove_reference<T>::type, |
| 64 | + typename std::remove_const<typename std::remove_reference<T>::type>::type, |
54 | 65 | exprt>(base);
|
55 | 66 | }
|
56 | 67 |
|
57 |
| -/// \brief Cast a pointer to a generic exprt to a specific derived class |
58 |
| -/// \tparam T The pointer or const pointer type to \a TUnderlying to cast to |
| 68 | +/// \brief Try to cast a reference to a generic exprt to a specific derived |
| 69 | +/// class |
| 70 | +/// \tparam T The reference or const reference type to \a TUnderlying to cast |
| 71 | +/// to |
59 | 72 | /// \tparam TUnderlying An exprt-derived class type
|
60 | 73 | /// \tparam TExpr The original type to cast from, either exprt or const exprt
|
61 |
| -/// \param base Pointer to a generic \ref exprt |
62 |
| -/// \return Pointer to object of type \a TUnderlying |
63 |
| -/// or null if \a base is not an instance of \a TUnderlying |
64 |
| -template<typename T, typename TUnderlying, typename TExpr> |
65 |
| -T expr_dynamic_cast(TExpr *base) |
| 74 | +/// \param base Reference to a generic \ref exprt |
| 75 | +/// \return Reference to object of type \a TUnderlying |
| 76 | +/// or valueless optional if \a base is not an instance of \a TUnderlying |
| 77 | +template<typename T, typename TConst, typename TUnderlying, typename TExpr> |
| 78 | +optionalt<std::reference_wrapper<TConst>> expr_try_dynamic_cast(TExpr &base) |
66 | 79 | {
|
67 | 80 | static_assert(
|
68 | 81 | std::is_same<typename std::remove_const<TExpr>::type, exprt>::value,
|
69 |
| - "Tried to expr_dynamic_cast from something that wasn't an exprt"); |
| 82 | + "Tried to expr_try_dynamic_cast from something that wasn't an exprt"); |
70 | 83 | static_assert(
|
71 |
| - std::is_pointer<T>::value, |
72 |
| - "Tried to convert exprt * to non-pointer type"); |
| 84 | + std::is_reference<T>::value, |
| 85 | + "Tried to convert exprt & to non-reference type"); |
73 | 86 | static_assert(
|
74 | 87 | std::is_base_of<exprt, TUnderlying>::value,
|
75 | 88 | "The template argument T must be derived from exprt.");
|
76 |
| - if(base == nullptr) |
77 |
| - return nullptr; |
78 |
| - if(!can_cast_expr<TUnderlying>(*base)) |
79 |
| - return nullptr; |
| 89 | + if(!can_cast_expr<TUnderlying>(base)) |
| 90 | + return optionalt<std::reference_wrapper<TConst>>(); |
80 | 91 | T value=static_cast<T>(base);
|
81 |
| - validate_expr(*value); |
82 |
| - return value; |
| 92 | + validate_expr(value); |
| 93 | + return optionalt<std::reference_wrapper<TConst>>(value); |
83 | 94 | }
|
84 | 95 |
|
85 | 96 | /// \brief Cast a constant reference to a generic exprt to a specific derived
|
|
0 commit comments