|
12 | 12 | /// \file ansi-c/c_expr.h |
13 | 13 | /// API to expression classes that are internal to the C frontend |
14 | 14 |
|
| 15 | +#include <util/byte_operators.h> |
15 | 16 | #include <util/std_code.h> |
16 | 17 |
|
17 | 18 | /// \brief Shuffle elements of one or two vectors, modelled after Clang's |
@@ -370,4 +371,51 @@ inline enum_is_in_range_exprt &to_enum_is_in_range_expr(exprt &expr) |
370 | 371 | return ret; |
371 | 372 | } |
372 | 373 |
|
| 374 | +/// \brief Reinterpret the bits of an expression of type `S` as an expression of |
| 375 | +/// type `T` where `S` and `T` have the same size. |
| 376 | +class bit_cast_exprt : public unary_exprt |
| 377 | +{ |
| 378 | +public: |
| 379 | + bit_cast_exprt(exprt expr, typet type) |
| 380 | + : unary_exprt(ID_bit_cast, std::move(expr), std::move(type)) |
| 381 | + { |
| 382 | + } |
| 383 | + |
| 384 | + byte_extract_exprt lower() const; |
| 385 | +}; |
| 386 | + |
| 387 | +template <> |
| 388 | +inline bool can_cast_expr<bit_cast_exprt>(const exprt &base) |
| 389 | +{ |
| 390 | + return base.id() == ID_bit_cast; |
| 391 | +} |
| 392 | + |
| 393 | +inline void validate_expr(const bit_cast_exprt &value) |
| 394 | +{ |
| 395 | + validate_operands(value, 1, "bit_cast must have one operand"); |
| 396 | +} |
| 397 | + |
| 398 | +/// \brief Cast an exprt to a \ref bit_cast_exprt |
| 399 | +/// |
| 400 | +/// \a expr must be known to be \ref bit_cast_exprt. |
| 401 | +/// |
| 402 | +/// \param expr: Source expression |
| 403 | +/// \return Object of type \ref bit_cast_exprt |
| 404 | +inline const bit_cast_exprt &to_bit_cast_expr(const exprt &expr) |
| 405 | +{ |
| 406 | + PRECONDITION(expr.id() == ID_bit_cast); |
| 407 | + const bit_cast_exprt &ret = static_cast<const bit_cast_exprt &>(expr); |
| 408 | + validate_expr(ret); |
| 409 | + return ret; |
| 410 | +} |
| 411 | + |
| 412 | +/// \copydoc to_bit_cast_expr(const exprt &) |
| 413 | +inline bit_cast_exprt &to_bit_cast_expr(exprt &expr) |
| 414 | +{ |
| 415 | + PRECONDITION(expr.id() == ID_bit_cast); |
| 416 | + bit_cast_exprt &ret = static_cast<bit_cast_exprt &>(expr); |
| 417 | + validate_expr(ret); |
| 418 | + return ret; |
| 419 | +} |
| 420 | + |
373 | 421 | #endif // CPROVER_ANSI_C_C_EXPR_H |
0 commit comments