Spicy
map.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <vector>
6 
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>
13 
14 namespace hilti::operator_ {
15 
16 namespace detail {
17 
18 static inline auto constantKeyType(unsigned int op, const char* doc = "<type of key>") {
19  return [=](const hilti::node::Range<Expression>& /* orig_ops */,
20  const hilti::node::Range<Expression>& resolved_ops) -> std::optional<Type> {
21  if ( resolved_ops.empty() )
22  return type::DocOnly(doc);
23 
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()));
27  }
28 
29  return type::constant(resolved_ops[op].type().as<type::Map>().keyType());
30  };
31 }
32 
33 } // namespace detail
34 
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.");
50 
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.")
61 
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.");
71 
72 STANDARD_OPERATOR_3(map, IndexAssign, type::void_, type::Map(type::Wildcard()), detail::constantKeyType(0, "key"),
73  type::Any(),
74  "Updates the map value for a given key. If the key does not exist a new element is inserted.");
75 
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),
80  .id = "get",
81  .args = {{"key", type::Any()}, {"default", type::Any(), true}},
82  .doc = R"(
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.
85 )"};
86  return _signature;
87  }
88 END_METHOD
89 
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.
95 )"};
96  return _signature;
97  }
98 END_METHOD
99 
100 
101 } // namespace hilti::operator_
Definition: node.h:38
Definition: operator-registry.h:15