74#ifndef PYTHON_DICT_HANDLER_H
75#define PYTHON_DICT_HANDLER_H
83#include <nlohmann/json.hpp>
87#include <unordered_map>
162 const nlohmann::json &element,
163 const nlohmann::json &generator,
187 const nlohmann::json &element,
188 const exprt &target_symbol);
197 const nlohmann::json &element,
198 const exprt &target_symbol);
232 const exprt &dict_expr,
233 const nlohmann::json &slice_node,
235 bool check_type =
false);
261 const exprt &dict_expr,
262 const exprt &key_expr,
265 const nlohmann::json &node,
266 codet &target_block);
284 const exprt &key_expr,
285 const exprt &dict_expr,
304 const exprt &dict_expr,
305 const nlohmann::json &slice_node,
306 codet &target_block);
352 const nlohmann::json &left,
353 const nlohmann::json &right,
422 const exprt &dict_expr,
423 const nlohmann::json &slice_node,
445 const nlohmann::json &ast_node,
446 const nlohmann::json &target,
447 codet &target_block);
459 const nlohmann::json &ast_node,
473 const nlohmann::json &ast_node,
474 const nlohmann::json &target,
504 const exprt &dict_expr,
505 const nlohmann::json &call_node);
545 static const std::string &
548 static const std::string empty;
549 auto &m = keys ? dict_keys_list_id_ : dict_vals_list_id_;
550 auto it = m.find(dict_sym_id);
551 return it != m.end() ? it->second : empty;
572 const exprt &dict_expr,
574 const nlohmann::json &call_node);
581 const nlohmann::json &element);
681 const nlohmann::json &lhs,
682 const nlohmann::json &rhs)
const;
697 static std::unordered_map<std::string, std::string> dict_keys_list_id_;
700 static std::unordered_map<std::string, std::string> dict_vals_list_id_;
709 get_dict_key_type_from_annotation(
const nlohmann::json &annotation_node);
717 exprt get_key_expr(
const nlohmann::json &slice_node);
727 static size_t generate_nested_dict_type_hash(
const typet &dict_type);
739 exprt safe_cast_to_dict_pointer(
740 const nlohmann::json &node,
741 const exprt &obj_value,
742 const typet &target_ptr_type,
752 void store_nested_dict_value(
753 const nlohmann::json &element,
755 const exprt &value_expr,
770 void store_dict_value_by_pointer(
771 const nlohmann::json &node,
773 const exprt &values_member,
788 exprt retrieve_nested_dict_value(
789 const nlohmann::json &slice_node,
790 const exprt &obj_value,
791 const typet &expected_type,
804 std::string generate_unique_dict_name(
805 const nlohmann::json &element,
A `switch' instruction */ class code_switcht : public codet { public: inline code_switcht() : codet("...
Definition std_code.h:574
std::vector< entry > entries
Definition element_type_registry.h:54
Main converter for transforming Python AST into ESBMC's intermediate representation.
Definition python_converter.h:76
exprt handle_dict_fromkeys(const nlohmann::json &call_node)
Handles dict.fromkeys() class method calls.
Definition dict_methods.cpp:1141
bool holds_dict_lists(const typet &type) const
True when a dict value of type holds a dict's list pointers, which a byte copy into the values list d...
Definition dict_core.cpp:78
bool is_retypable_subscript(const nlohmann::json &node)
A subscript whose base can be converted again to retype the read.
Definition dict_type_resolution.cpp:8
static const std::string & get_internal_list_id(const std::string &dict_sym_id, bool keys)
Return the keys-list or values-list symbol id for dict_sym_id.
Definition python_dict_handler.h:546
void check_value_type(bool enabled, const exprt &obj, const typet &type, const locationt &location)
When enabled, assert that the PyObject obj holds a value of type.
Definition dict_access.cpp:10
exprt bind_dict_source(const exprt &source, const nlohmann::json &node)
A call returning a dict is a statement; binds its result so its members can be read.
Definition dict_methods.cpp:1422
exprt handle_dict_popitem(const exprt &dict_expr, const nlohmann::json &call_node)
Handles dict.popitem() method calls.
Definition dict_methods.cpp:940
void copy_dict_entries(const exprt &dict_expr, const exprt &source, const nlohmann::json &call_node)
Copies every entry of source into dict_expr, overwriting keys already present (dict....
Definition dict_methods.cpp:1441
exprt handle_dict_constructor(const nlohmann::json &call_node)
Handles dict(iterable) constructor calls.
Definition dict_methods.cpp:1227
struct_typet get_dict_struct_type()
Gets or creates the dictionary struct type.
Definition dict_core.cpp:88
exprt handle_dict_setdefault(const exprt &dict_expr, const nlohmann::json &call_node)
Handles dict.setdefault() method calls.
Definition dict_methods.cpp:267
bool is_dict_literal(const nlohmann::json &element) const
Checks if a JSON AST node represents a dictionary literal.
Definition dict_core.cpp:63
static bool is_value_returning_method(const std::string &method_name)
Returns true for dict methods that return a value and emit IR.
Definition dict_methods.cpp:644
bool handle_subscript_assignment_check(python_converter &converter, const nlohmann::json &ast_node, const nlohmann::json &target, codet &target_block)
Check and handle dictionary subscript assignment.
Definition dict_assignment.cpp:5
typet get_popitem_tuple_type(const exprt &dict_expr)
Returns the tuple type (key_type, value_type) for dict.popitem().
Definition dict_methods.cpp:880
typet get_dict_value_type_from_annotation(const nlohmann::json &annotation_node)
Extract value type from dict type annotation.
Definition dict_type_resolution.cpp:89
exprt handle_dict_subscript(const exprt &dict_expr, const nlohmann::json &slice_node, const typet &expected_type=typet(), bool check_type=false)
Handles dictionary subscript access (e.g., dict['key']).
Definition dict_access.cpp:99
exprt handle_dict_membership(const exprt &key_expr, const exprt &dict_expr, bool negated)
Handles dictionary membership testing (in / not in operators).
Definition dict_access.cpp:717
exprt handle_dict_update(const exprt &dict_expr, const nlohmann::json &call_node)
Handles dict.update() method calls.
Definition dict_methods.cpp:1336
typet recorded_tuple_value_type(const exprt &dict_expr) const
Stored tuple struct type of a literal dict's values, recorded at construction; empty unless every val...
Definition dict_type_resolution.cpp:546
bool handle_literal_assignment_check(python_converter &converter, const nlohmann::json &ast_node, const exprt &lhs)
Check and handle dictionary literal assignment.
Definition dict_assignment.cpp:49
typet uniform_list_dict_value_type(const std::string &list_id) const
The scalar or str value type shared by every dict literal recorded as an element of list list_id; emp...
Definition dict_type_resolution.cpp:565
typet resolve_expected_type_for_dict_subscript(const exprt &dict_expr)
Resolve expected type for dict subscript using variable annotation.
Definition dict_type_resolution.cpp:112
bool handle_unannotated_literal_check(python_converter &converter, const nlohmann::json &ast_node, const nlohmann::json &target, const symbol_id &sid)
Check and handle unannotated dictionary literal.
Definition dict_assignment.cpp:83
exprt handle_dict_clear(const exprt &dict_expr, const nlohmann::json &call_node)
Handles dict.clear() method calls.
Definition dict_methods.cpp:611
exprt build_range_dict_comprehension(const nlohmann::json &element, const nlohmann::json &generator, symbolt &dict_sym)
Definition dict_construction.cpp:347
void create_empty_dict(const nlohmann::json &element, const exprt &target_symbol)
Initialises target_symbol as an empty dict located at element.
Definition dict_construction.cpp:746
exprt create_dict_from_literal(const nlohmann::json &element, const exprt &target_symbol)
Creates and initializes a dictionary from a literal expression.
Definition dict_construction.cpp:510
void resolve_dict_subscript_types(const nlohmann::json &left, const nlohmann::json &right, exprt &lhs, exprt &rhs)
Resolve dictionary subscript types for comparisons.
Definition dict_type_resolution.cpp:14
char annotated_value_kind(const exprt &dict_expr)
's' or 'n' when dict_expr is written dict[K, V] with a str or number V, else '\0'.
Definition dict_access.cpp:50
exprt handle_dict_pop(const exprt &dict_expr, const nlohmann::json &call_node)
Handles dict.pop() method calls.
Definition dict_methods.cpp:675
exprt handle_dict_union(const exprt &lhs, const exprt &rhs, const nlohmann::json &element)
Builds lhs | rhs (PEP 584) as a new dict holding lhs's entries updated with rhs's.
Definition dict_methods.cpp:1530
void handle_dict_delete(const exprt &dict_expr, const nlohmann::json &slice_node, codet &target_block)
Handles dictionary key deletion (e.g., del dict['key']).
Definition dict_access.cpp:741
typet literal_value_type(const exprt &target)
The Optional<T> value type a literal assigned to target stores, from the target's annotation or,...
Definition dict_construction.cpp:503
std::optional< bool > try_constant_fold_eq(const nlohmann::json &lhs, const nlohmann::json &rhs) const
Constant-fold a dict-vs-dict equality when both AST operands are literal int-keyed,...
Definition dict_compare.cpp:168
exprt handle_dict_copy(const exprt &dict_expr, const nlohmann::json &call_node)
Handles dict.copy() method calls.
Definition dict_methods.cpp:566
exprt compare(const exprt &lhs, const exprt &rhs, const std::string &op)
Compares two dictionaries for equality or inequality.
Definition dict_compare.cpp:5
std::string materialize_tuple_key(exprt &key_expr, const nlohmann::json &element)
Gives a tuple key an identity so value-reading folds can resolve it.
Definition dict_construction.cpp:481
bool reads_value_as_tagged(const exprt &dict_expr, const nlohmann::json &slice_node, const element_type_registry::entries &entries)
Whether a read of dict_expr yields the stored item as a tagged scalar.
Definition dict_access.cpp:74
python_dict_handler(python_converter &converter, contextt &symbol_table, type_handler &type_handler)
Constructs a new dictionary handler.
Definition dict_core.cpp:10
exprt handle_dict_get(const exprt &dict_expr, const nlohmann::json &call_node)
Handles dict.get() method calls.
Definition dict_methods.cpp:20
exprt create_dict_with_unpacking(const nlohmann::json &element, const exprt &target_symbol)
Builds a dict display holding a ** unpack ({**a, "k": v, **b}) by applying its entries in order to an...
Definition dict_construction.cpp:711
exprt read_struct_value(const exprt &obj_value, const typet &type)
Read a struct value of type type from a dict item's value pointer obj_value.
Definition dict_access.cpp:501
void handle_dict_subscript_assign(const exprt &dict_expr, const exprt &key_expr, const exprt &value, const locationt &location, const nlohmann::json &node, codet &target_block)
Handles dictionary subscript assignment (e.g., dict['key'] = value).
Definition dict_access.cpp:520
typet literal_value_type_hint
The value type a dict literal not bound to an annotated name takes, such as one passed straight to an...
Definition python_dict_handler.h:410
exprt get_dict_literal(const nlohmann::json &element)
Creates a dictionary literal expression.
Definition dict_construction.cpp:18
exprt get_dict_comprehension(const nlohmann::json &element)
Definition dict_construction.cpp:47
bool is_dict_type(const typet &type) const
Checks if a type represents a Python dictionary.
Definition dict_core.cpp:68
Definition std_types.h:232
Definition type_handler.h:113