|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Byte flattening |
| 4 | +
|
| 5 | +Author: Diffblue Ltd. |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#ifndef CPROVER_SOLVERS_FLATTENING_FLATTEN_BYTE_EXTRACT_EXCEPTIONS_H |
| 10 | +#define CPROVER_SOLVERS_FLATTENING_FLATTEN_BYTE_EXTRACT_EXCEPTIONS_H |
| 11 | + |
| 12 | +#include <sstream> |
| 13 | +#include <stdexcept> |
| 14 | +#include <string> |
| 15 | + |
| 16 | +#include <util/std_expr.h> |
| 17 | +#include <util/std_types.h> |
| 18 | + |
| 19 | +class flatten_byte_extract_exceptiont : public std::runtime_error |
| 20 | +{ |
| 21 | +public: |
| 22 | + explicit flatten_byte_extract_exceptiont(const std::string &exception_message) |
| 23 | + : runtime_error(exception_message) |
| 24 | + { |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +class non_const_array_sizet : public flatten_byte_extract_exceptiont |
| 29 | +{ |
| 30 | +public: |
| 31 | + non_const_array_sizet(const array_typet &array_type, const exprt &max_bytes) |
| 32 | + : flatten_byte_extract_exceptiont("cannot unpack array of non-const size"), |
| 33 | + max_bytes(max_bytes), |
| 34 | + array_type(array_type) |
| 35 | + { |
| 36 | + std::ostringstream error_message; |
| 37 | + error_message << runtime_error::what() << "\n"; |
| 38 | + error_message << "array_type: " << array_type.pretty(); |
| 39 | + error_message << "\nmax_bytes: " << max_bytes.pretty(); |
| 40 | + computed_error_message = error_message.str(); |
| 41 | + } |
| 42 | + |
| 43 | + const char *what() const noexcept override |
| 44 | + { |
| 45 | + return computed_error_message.c_str(); |
| 46 | + } |
| 47 | + |
| 48 | +private: |
| 49 | + exprt max_bytes; |
| 50 | + array_typet array_type; |
| 51 | + |
| 52 | + std::string computed_error_message; |
| 53 | +}; |
| 54 | + |
| 55 | +class non_byte_alignedt : public flatten_byte_extract_exceptiont |
| 56 | +{ |
| 57 | +public: |
| 58 | + non_byte_alignedt( |
| 59 | + const struct_typet &struct_type, |
| 60 | + const struct_union_typet::componentt &component, |
| 61 | + const mp_integer &byte_width) |
| 62 | + : flatten_byte_extract_exceptiont( |
| 63 | + "cannot unpack struct with non-byte aligned components"), |
| 64 | + struct_type(struct_type), |
| 65 | + component(component), |
| 66 | + byte_width(byte_width) |
| 67 | + { |
| 68 | + std::ostringstream error_message; |
| 69 | + error_message << runtime_error::what() << "\n"; |
| 70 | + error_message << "width: " << byte_width << "\n"; |
| 71 | + error_message << "component:" << component.get_name() << "\n"; |
| 72 | + error_message << "struct_type: " << struct_type.pretty(); |
| 73 | + computed_error_message = error_message.str(); |
| 74 | + } |
| 75 | + |
| 76 | + const char *what() const noexcept override |
| 77 | + { |
| 78 | + return computed_error_message.c_str(); |
| 79 | + } |
| 80 | + |
| 81 | +private: |
| 82 | + const struct_typet struct_type; |
| 83 | + const struct_union_typet::componentt component; |
| 84 | + const mp_integer byte_width; |
| 85 | + |
| 86 | + std::string computed_error_message; |
| 87 | +}; |
| 88 | + |
| 89 | +class non_constant_widtht : public flatten_byte_extract_exceptiont |
| 90 | +{ |
| 91 | +public: |
| 92 | +public: |
| 93 | + non_constant_widtht(const exprt &src, const exprt &max_bytes) |
| 94 | + : flatten_byte_extract_exceptiont( |
| 95 | + "cannot unpack object of non-constant width"), |
| 96 | + src(src), |
| 97 | + max_bytes(max_bytes) |
| 98 | + { |
| 99 | + std::ostringstream error_message; |
| 100 | + error_message << runtime_error::what() << "\n"; |
| 101 | + error_message << "array_type: " << src.pretty(); |
| 102 | + error_message << "\nmax_bytes: " << max_bytes.pretty(); |
| 103 | + computed_error_message = error_message.str(); |
| 104 | + } |
| 105 | + |
| 106 | + const char *what() const noexcept override |
| 107 | + { |
| 108 | + return computed_error_message.c_str(); |
| 109 | + } |
| 110 | + |
| 111 | +private: |
| 112 | + exprt src; |
| 113 | + exprt max_bytes; |
| 114 | + |
| 115 | + std::string computed_error_message; |
| 116 | +}; |
| 117 | + |
| 118 | +class non_const_byte_extraction_sizet : public flatten_byte_extract_exceptiont |
| 119 | +{ |
| 120 | +public: |
| 121 | + explicit non_const_byte_extraction_sizet( |
| 122 | + const byte_extract_exprt &unpack_expr) |
| 123 | + : flatten_byte_extract_exceptiont( |
| 124 | + "byte_extract flatting with non-constant size"), |
| 125 | + unpack_expr(unpack_expr) |
| 126 | + { |
| 127 | + std::ostringstream error_message; |
| 128 | + error_message << runtime_error::what() << "\n"; |
| 129 | + error_message << "unpack_expr: " << unpack_expr.pretty(); |
| 130 | + computed_error_message = error_message.str(); |
| 131 | + } |
| 132 | + |
| 133 | + const char *what() const noexcept override |
| 134 | + { |
| 135 | + return computed_error_message.c_str(); |
| 136 | + } |
| 137 | + |
| 138 | +private: |
| 139 | + const byte_extract_exprt unpack_expr; |
| 140 | + |
| 141 | + std::string computed_error_message; |
| 142 | +}; |
| 143 | + |
| 144 | +#endif // CPROVER_SOLVERS_FLATTENING_FLATTEN_BYTE_EXTRACT_EXCEPTIONS_H |
0 commit comments