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> 19 static inline auto constantKeyType(
unsigned int op,
const char* doc =
"<type of key>") {
22 if ( resolved_ops.empty() )
23 return type::DocOnly(doc);
25 if ( op >= resolved_ops.size() ) {
26 logger().internalError(
27 util::fmt(
"keyType(): index %d out of range, only %" PRIu64
" ops available", op, resolved_ops.size()));
30 return type::constant(resolved_ops[op].type().as<type::Map>().keyType());
36 STANDARD_OPERATOR_1(map::iterator, Deref, operator_::dereferencedType(0),
37 type::constant(type::map::Iterator(type::Wildcard())),
38 "Returns the map element that the iterator refers to.");
39 STANDARD_OPERATOR_1(map::iterator, IncrPostfix, operator_::sameTypeAs(0,
"iterator<map<*>>"),
40 type::map::Iterator(type::Wildcard()),
41 "Advances the iterator by one map element, returning the previous position.");
42 STANDARD_OPERATOR_1(map::iterator, IncrPrefix, operator_::sameTypeAs(0,
"iterator<map<*>>"),
43 type::map::Iterator(type::Wildcard()),
44 "Advances the iterator by one map element, returning the new position.");
45 STANDARD_OPERATOR_2(map::iterator, Equal, type::Bool(), type::constant(type::map::Iterator(type::Wildcard())),
46 operator_::sameTypeAs(0,
"iterator<map<*>>"),
47 "Returns true if two map iterators refer to the same location.");
48 STANDARD_OPERATOR_2(map::iterator, Unequal, type::Bool(), type::constant(type::map::Iterator(type::Wildcard())),
49 operator_::sameTypeAs(0,
"iterator<map<*>>"),
50 "Returns true if two map iterators refer to different locations.");
52 STANDARD_OPERATOR_1(map, Size, type::UnsignedInteger(64), type::constant(type::Map(type::Wildcard())),
53 "Returns the number of elements a map contains.");
54 STANDARD_OPERATOR_2(map, Equal, type::Bool(), type::constant(type::Map(type::Wildcard())),
55 operator_::sameTypeAs(0,
"map<*>"),
"Compares two maps element-wise.");
56 STANDARD_OPERATOR_2(map, Unequal, type::Bool(), type::constant(type::Map(type::Wildcard())),
57 operator_::sameTypeAs(0,
"map<*>"),
"Compares two maps element-wise.");
58 STANDARD_OPERATOR_2(map, In, type::Bool(), type::Any(), type::constant(type::Map(type::Wildcard())),
59 "Returns true if an element is part of the map.");
60 STANDARD_OPERATOR_2(map, Delete, type::void_, type::Map(type::Wildcard()), detail::constantKeyType(0,
"key"),
61 "Removes an element from the map.")
63 STANDARD_OPERATOR_2x_low_prio(
64 map, IndexConst, Index, operator_::constantElementType(0), type::constant(type::Map(type::Wildcard())),
65 detail::constantKeyType(0, "key"),
66 "Returns the map's element for the given key. The key must exist, otherwise the operation "
67 "will throw a runtime error.");
68 STANDARD_OPERATOR_2x_lhs(map, IndexNonConst, Index, operator_::elementType(0), type::Map(type::Wildcard()),
69 detail::constantKeyType(0, "key"),
70 "Returns the map's element for the given key. The key must exist, otherwise the operation "
71 "will throw a runtime error.");
73 STANDARD_OPERATOR_3(map, IndexAssign, type::void_, type::Map(type::Wildcard()), detail::constantKeyType(0, "key"),
75 "Updates the map value for a given key. If the key does not exist a new element is inserted.");
77 BEGIN_METHOD(map, Get)
78 auto signature()
const {
79 return Signature{.self = type::Map(type::Wildcard()),
80 .result = operator_::elementType(0),
82 .args = {{.id =
"key", .type = type::Any()},
83 {.id =
"default", .type = type::Any(), .optional =
true}},
85 Returns the map's element for the given key. If the key does not exist, returns 86 the default value if provided; otherwise throws a runtime error. 91 BEGIN_METHOD(map, Clear) 92 auto signature()
const {
93 return Signature{.self = type::Map(type::Wildcard()),
94 .result = type::void_,
98 Removes all elements from the map.
std::string fmt(const char *fmt, const Args &... args)
Definition: util.h:80