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 std::vector<Expression>& /* orig_ops */,
21  const std::vector<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, "element"),
61  "Removes an element from the map.")
62 
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.");
70 
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.");
73 
74 BEGIN_METHOD(map, Get)
75  auto signature() const {
76  return Signature{.self = type::Map(type::Wildcard()),
77  .result = operator_::elementType(0),
78  .id = "get",
79  .args = {{.id = "key", .type = type::Any()},
80  {.id = "default", .type = type::Any(), .optional = true}},
81  .doc = R"(
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.
84 )"};
85  }
86 END_METHOD
87 
88 BEGIN_METHOD(map, Clear)
89  auto signature() const {
90  return Signature{.self = type::Map(type::Wildcard()),
91  .result = type::Void(),
92  .id = "clear",
93  .args = {},
94  .doc = R"(
95 Removes all elements from the map.
96 )"};
97  }
98 END_METHOD
99 
100 
101 } // namespace operator_
102 
103 } // namespace hilti
std::string fmt(const char *fmt, const Args &... args)
Definition: util.h:80