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>") {
20 return [=](
const std::vector<Expression>& ,
21 const std::vector<Expression>& resolved_ops) -> std::optional<Type> {
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,
"element"),
61 "Removes an element from the map.")
63 STANDARD_OPERATOR_2x(map, IndexConst, Index, operator_::constantElementType(0),
64 type::constant(type::Map(type::Wildcard())), type::Any(),
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()), type::Any(),
68 "Returns the map's element for the given key. The key must exist, otherwise the operation "
69 "will throw a runtime error.");
71 STANDARD_OPERATOR_3(map, IndexAssign, type::Void(), type::Map(type::Wildcard()), type::Any(), type::Any(),
72 "Updates the map value for a given key. If the key does not exist a new element is inserted.");
74 BEGIN_METHOD(map, Get)
75 auto signature()
const {
76 return Signature{.self = type::Map(type::Wildcard()),
77 .result = operator_::elementType(0),
79 .args = {{.id =
"key", .type = type::Any()},
80 {.id =
"default", .type = type::Any(), .optional =
true}},
82 Returns the map's element for the given key. If the key does not exist, returns 83 the default value if provided; otherwise throws a runtime error. 88 BEGIN_METHOD(map, Clear) 89 auto signature()
const {
90 return Signature{.self = type::Map(type::Wildcard()),
91 .result = type::Void(),
95 Removes all elements from the map.
std::string fmt(const char *fmt, const Args &... args)
Definition: util.h:80