ESBMC
Efficient SMT-based Context-Bounded Model Checker
Loading...
Searching...
No Matches
python_dict_internal.h
Go to the documentation of this file.
1#pragma once
2
3// Shared include bundle for the python-dict module translation units. This is
4// the exact set of headers the original monolithic python_dict_handler.cpp
5// pulled in, so every split unit sees the same declarations it did before the
6// refactor.
15#include <irep2/irep2_utils.h>
17#include <util/lang/c_types.h>
18#include <util/config/config.h>
19#include <util/symtab/context.h>
20#include <util/irep/migrate.h>
22#include <util/irep/std_code.h>
23
24#include <algorithm>
25#include <functional>
26#include <sstream>
27
30inline const nlohmann::json *
31constant_bound_to(const nlohmann::json &node, const std::string &name)
32{
33 const std::string type = node.value("_type", "");
34 const nlohmann::json *target = type == "Assign" && node["targets"].size() == 1
35 ? &node["targets"][0]
36 : type == "AnnAssign" ? &node["target"]
37 : nullptr;
38 if (
39 !target || target->value("_type", "") != "Name" ||
40 target->value("id", "") != name || !node["value"].is_object() ||
41 node["value"].value("_type", "") != "Constant")
42 return nullptr;
43 return &node["value"];
44}
45
52 const nlohmann::json &node,
53 const std::string &name,
54 unsigned &count,
55 const nlohmann::json *&value)
56{
57 if (node.is_string())
58 count += node.get<std::string>() == name;
59 if (node.is_array())
60 for (const auto &item : node)
61 count_name_bindings(item, name, count, value);
62 if (!node.is_object())
63 return;
64 const std::string type = node.value("_type", "");
65 if (type == "Name" || type == "Constant")
66 {
67 const bool load =
68 node.contains("ctx") && node["ctx"].value("_type", "") == "Load";
69 count += type == "Name" && node.value("id", "") == name && !load;
70 return;
71 }
72 if (const nlohmann::json *constant = constant_bound_to(node, name))
73 value = constant;
74 for (const auto &item : node.items())
75 if (item.key() != "_type")
76 count_name_bindings(item.value(), name, count, value);
77}
78
84inline std::string
85dict_key_id(const nlohmann::json &key, const nlohmann::json &ast)
86{
87 if (!key.is_object())
88 return std::string();
89 const std::string type = key.value("_type", "");
90 if (type == "Constant")
91 return key["value"].dump();
92 if (type != "Name")
93 return std::string();
94 unsigned count = 0;
95 const nlohmann::json *value = nullptr;
96 count_name_bindings(ast, key.value("id", ""), count, value);
97 return count == 1 && value ? (*value)["value"].dump() : std::string();
98}
Definition toml.hpp:1531
int count
Definition cuda.h:51
Handler for Python dictionary operations in ESBMC's Python frontend.
std::string dict_key_id(const nlohmann::json &key, const nlohmann::json &ast)
The source literal of a constant dict key, e.g.
Definition python_dict_internal.h:85
const nlohmann::json * constant_bound_to(const nlohmann::json &node, const std::string &name)
The Constant that statement node binds to name alone, as in name = c or name: T = c,...
Definition python_dict_internal.h:31
void count_name_bindings(const nlohmann::json &node, const std::string &name, unsigned &count, const nlohmann::json *&value)
Counts every binding of name in node (a Name it stores or deletes, a parameter, def,...
Definition python_dict_internal.h:51
uint256_t key
Definition solidity_mapping.c:1