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 {
15 namespace operator_ {
16 
17 namespace detail {
18 
19 static inline auto constantKeyType(unsigned int op, const char* doc = "<type of key>") {
20  return [=](const hilti::node::Range<Expression>& /* orig_ops */,
21  const hilti::node::Range<Expression>& resolved_ops) -> std::optional<Type> {
22  if ( resolved_ops.empty() )
23  return type::DocOnly(doc);
24 
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()));
28  }
29 
30  return type::constant(resolved_ops[op].type().as<type::Map>().keyType());
31  };
32 }
33 
34 } // namespace detail
35 
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.");
51 
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.")
62 
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.");
72 
73 STANDARD_OPERATOR_3(map, IndexAssign, type::void_, type::Map(type::Wildcard()), detail::constantKeyType(0, "key"),
74  type::Any(),
75  "Updates the map value for a given key. If the key does not exist a new element is inserted.");
76 
77 BEGIN_METHOD(map, Get)
78  auto signature() const {
79  return Signature{.self = type::Map(type::Wildcard()),
80  .result = operator_::elementType(0),
81  .id = "get",
82  .args = {{.id = "key", .type = type::Any()},
83  {.id = "default", .type = type::Any(), .optional = true}},
84  .doc = R"(
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.
87 )"};
88  }
89 END_METHOD
90 
91 BEGIN_METHOD(map, Clear)
92  auto signature() const {
93  return Signature{.self = type::Map(type::Wildcard()),
94  .result = type::void_,
95  .id = "clear",
96  .args = {},
97  .doc = R"(
98 Removes all elements from the map.
99 )"};
100  }
101 END_METHOD
102 
103 
104 } // namespace operator_
105 
106 } // namespace hilti
Definition: node.h:39
std::string fmt(const char *fmt, const Args &... args)
Definition: util.h:80