|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: JSON symbol deserialization |
| 4 | +
|
| 5 | +Author: Chris Smowton, [email protected] |
| 6 | +
|
| 7 | +\*******************************************************************/ |
| 8 | + |
| 9 | +#include "json_symbol.h" |
| 10 | + |
| 11 | +#include <util/exception_utils.h> |
| 12 | +#include <util/expr.h> |
| 13 | +#include <util/json_irep.h> |
| 14 | +#include <util/source_location.h> |
| 15 | +#include <util/type.h> |
| 16 | + |
| 17 | +/// Return string value for a given key if present in the json object. |
| 18 | +/// \param in: The json object that is getting fetched as a string. |
| 19 | +/// \param key: The key for the json value to be fetched. |
| 20 | +/// \return: A string value for the corresponding key. |
| 21 | +static const std::string & |
| 22 | +try_get_string(const jsont &in, const std::string &key) |
| 23 | +{ |
| 24 | + if(!in.is_string()) |
| 25 | + throw deserialization_exceptiont( |
| 26 | + "symbol_from_json: expected string for key '" + key + "'"); |
| 27 | + return in.value; |
| 28 | +} |
| 29 | + |
| 30 | +/// Return boolean value for a given key if present in the json object. |
| 31 | +/// \param in: The json object that is getting fetched as a boolean. |
| 32 | +/// \param key: The key for the json value to be fetched. |
| 33 | +/// \return: A boolean value for the corresponding key. |
| 34 | +static bool try_get_bool(const jsont &in, const std::string &key) |
| 35 | +{ |
| 36 | + if(!(in.is_true() || in.is_false())) |
| 37 | + throw deserialization_exceptiont( |
| 38 | + "symbol_from_json: expected bool for key '" + key + "'"); |
| 39 | + return in.is_true(); |
| 40 | +} |
| 41 | + |
| 42 | +/// Deserialise a json object to a symbolt. |
| 43 | +/// \param in: The json object that is getting fetched as an object. |
| 44 | +/// \return: A symbolt representing the json object. |
| 45 | +symbolt symbol_from_json(const jsont &in) |
| 46 | +{ |
| 47 | + if(!in.is_object()) |
| 48 | + throw deserialization_exceptiont("symbol_from_json takes an object"); |
| 49 | + symbolt result; |
| 50 | + json_irept json2irep(true); |
| 51 | + for(const auto &kv : in.object) |
| 52 | + { |
| 53 | + if(kv.first == "type") |
| 54 | + { |
| 55 | + irept irep = json2irep.convert_from_json(kv.second); |
| 56 | + result.type = static_cast<typet &>(irep); |
| 57 | + } |
| 58 | + else if(kv.first == "value") |
| 59 | + { |
| 60 | + irept irep = json2irep.convert_from_json(kv.second); |
| 61 | + result.value = static_cast<exprt &>(irep); |
| 62 | + } |
| 63 | + else if(kv.first == "location") |
| 64 | + { |
| 65 | + irept irep = json2irep.convert_from_json(kv.second); |
| 66 | + result.location = static_cast<source_locationt &>(irep); |
| 67 | + } |
| 68 | + else if(kv.first == "name") |
| 69 | + result.name = try_get_string(kv.second, "name"); |
| 70 | + else if(kv.first == "module") |
| 71 | + result.module = try_get_string(kv.second, "module"); |
| 72 | + else if(kv.first == "base_name") |
| 73 | + result.base_name = try_get_string(kv.second, "base_name"); |
| 74 | + else if(kv.first == "mode") |
| 75 | + result.mode = try_get_string(kv.second, "mode"); |
| 76 | + else if(kv.first == "pretty_name") |
| 77 | + result.pretty_name = try_get_string(kv.second, "pretty_name"); |
| 78 | + else if(kv.first == "is_type") |
| 79 | + result.is_type = try_get_bool(kv.second, "is_type"); |
| 80 | + else if(kv.first == "is_macro") |
| 81 | + result.is_macro = try_get_bool(kv.second, "is_macro"); |
| 82 | + else if(kv.first == "is_exported") |
| 83 | + result.is_exported = try_get_bool(kv.second, "is_exported"); |
| 84 | + else if(kv.first == "is_input") |
| 85 | + result.is_input = try_get_bool(kv.second, "is_input"); |
| 86 | + else if(kv.first == "is_output") |
| 87 | + result.is_output = try_get_bool(kv.second, "is_output"); |
| 88 | + else if(kv.first == "is_state_var") |
| 89 | + result.is_state_var = try_get_bool(kv.second, "is_state_var"); |
| 90 | + else if(kv.first == "is_property") |
| 91 | + result.is_property = try_get_bool(kv.second, "is_property"); |
| 92 | + else if(kv.first == "is_static_lifetime") |
| 93 | + result.is_static_lifetime = try_get_bool(kv.second, "is_static_lifetime"); |
| 94 | + else if(kv.first == "is_thread_local") |
| 95 | + result.is_thread_local = try_get_bool(kv.second, "is_thread_local"); |
| 96 | + else if(kv.first == "is_lvalue") |
| 97 | + result.is_lvalue = try_get_bool(kv.second, "is_lvalue"); |
| 98 | + else if(kv.first == "is_file_local") |
| 99 | + result.is_file_local = try_get_bool(kv.second, "is_file_local"); |
| 100 | + else if(kv.first == "is_extern") |
| 101 | + result.is_extern = try_get_bool(kv.second, "is_extern"); |
| 102 | + else if(kv.first == "is_volatile") |
| 103 | + result.is_volatile = try_get_bool(kv.second, "is_volatile"); |
| 104 | + else if(kv.first == "is_parameter") |
| 105 | + result.is_parameter = try_get_bool(kv.second, "is_parameter"); |
| 106 | + else if(kv.first == "is_auxiliary") |
| 107 | + result.is_auxiliary = try_get_bool(kv.second, "is_auxiliary"); |
| 108 | + else if(kv.first == "is_weak") |
| 109 | + result.is_weak = try_get_bool(kv.second, "is_weak"); |
| 110 | + else |
| 111 | + throw deserialization_exceptiont( |
| 112 | + "symbol_from_json: unexpected key '" + kv.first + "'"); |
| 113 | + } |
| 114 | + |
| 115 | + return result; |
| 116 | +} |
0 commit comments