7 #include <hilti/ast/operators/common.h> 8 #include <hilti/ast/types/bool.h> 9 #include <hilti/ast/types/integer.h> 10 #include <hilti/ast/types/map.h> 11 #include <hilti/ast/types/void.h> 12 #include <hilti/base/util.h> 18 static inline auto constantKeyType(
unsigned int op,
const char* doc =
"<type of key>") {
21 if ( resolved_ops.empty() )
22 return type::DocOnly(doc);
24 if ( op >= resolved_ops.size() ) {
25 logger().internalError(
26 util::fmt(
"keyType(): index %d out of range, only %" PRIu64
" ops available", op, resolved_ops.size()));
29 return type::constant(resolved_ops[op].type().as<type::Map>().keyType());
35 STANDARD_OPERATOR_1(map::iterator, Deref, operator_::dereferencedType(0),
36 type::constant(type::map::Iterator(type::Wildcard())),
37 "Returns the map element that the iterator refers to.");
38 STANDARD_OPERATOR_1(map::iterator, IncrPostfix, operator_::sameTypeAs(0,
"iterator<map<*>>"),
39 type::map::Iterator(type::Wildcard()),
40 "Advances the iterator by one map element, returning the previous position.");
41 STANDARD_OPERATOR_1(map::iterator, IncrPrefix, operator_::sameTypeAs(0,
"iterator<map<*>>"),
42 type::map::Iterator(type::Wildcard()),
43 "Advances the iterator by one map element, returning the new position.");
44 STANDARD_OPERATOR_2(map::iterator, Equal, type::Bool(), type::constant(type::map::Iterator(type::Wildcard())),
45 operator_::sameTypeAs(0,
"iterator<map<*>>"),
46 "Returns true if two map iterators refer to the same location.");
47 STANDARD_OPERATOR_2(map::iterator, Unequal, type::Bool(), type::constant(type::map::Iterator(type::Wildcard())),
48 operator_::sameTypeAs(0,
"iterator<map<*>>"),
49 "Returns true if two map iterators refer to different locations.");
51 STANDARD_OPERATOR_1(map, Size, type::UnsignedInteger(64), type::constant(type::Map(type::Wildcard())),
52 "Returns the number of elements a map contains.");
53 STANDARD_OPERATOR_2(map, Equal, type::Bool(), type::constant(type::Map(type::Wildcard())),
54 operator_::sameTypeAs(0,
"map<*>"),
"Compares two maps element-wise.");
55 STANDARD_OPERATOR_2(map, Unequal, type::Bool(), type::constant(type::Map(type::Wildcard())),
56 operator_::sameTypeAs(0,
"map<*>"),
"Compares two maps element-wise.");
57 STANDARD_OPERATOR_2(map, In, type::Bool(), type::Any(), type::constant(type::Map(type::Wildcard())),
58 "Returns true if an element is part of the map.");
59 STANDARD_OPERATOR_2(map, Delete, type::void_, type::Map(type::Wildcard()), detail::constantKeyType(0,
"key"),
60 "Removes an element from the map.")
62 STANDARD_OPERATOR_2x_low_prio(
63 map, IndexConst, Index, operator_::constantElementType(0), type::constant(type::Map(type::Wildcard())),
64 detail::constantKeyType(0, "key"),
65 "Returns the map's element for the given key. The key must exist, otherwise the operation "
66 "will throw a runtime error.");
67 STANDARD_OPERATOR_2x_lhs(map, IndexNonConst, Index, operator_::elementType(0), type::Map(type::Wildcard()),
68 detail::constantKeyType(0, "key"),
69 "Returns the map's element for the given key. The key must exist, otherwise the operation "
70 "will throw a runtime error.");
72 STANDARD_OPERATOR_3(map, IndexAssign, type::void_, type::Map(type::Wildcard()), detail::constantKeyType(0, "key"),
74 "Updates the map value for a given key. If the key does not exist a new element is inserted.");
76 BEGIN_METHOD(map, Get)
77 const auto& signature()
const {
78 static auto _signature = Signature{.self = type::Map(type::Wildcard()),
79 .result = operator_::elementType(0),
81 .args = {{
"key", type::Any()}, {
"default", type::Any(),
true}},
83 Returns the map's element for the given key. If the key does not exist, returns 84 the default value if provided; otherwise throws a runtime error. 90 BEGIN_METHOD(map, Clear)
91 const auto& signature()
const {
92 static auto _signature =
93 Signature{.self = type::Map(type::Wildcard()), .result = type::void_, .id =
"clear", .args = {}, .doc = R
"( 94 Removes all elements from the map.
Definition: operator-registry.h:15