Spicy
map.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <algorithm>
6 #include <utility>
7 #include <vector>
8 
9 #include <hilti/ast/builder/type.h>
10 #include <hilti/ast/ctor.h>
11 #include <hilti/ast/expression.h>
12 #include <hilti/ast/types/auto.h>
13 #include <hilti/ast/types/bool.h>
14 #include <hilti/ast/types/map.h>
15 
16 namespace hilti {
17 namespace ctor {
18 
19 namespace map {
21 class Element : public NodeBase {
22 public:
23  Element(Expression k, Expression v, Meta m = Meta()) : NodeBase(nodes(std::move(k), std::move(v)), std::move(m)) {}
24  Element(Meta m = Meta()) : NodeBase(nodes(node::none, node::none), std::move(m)) {}
25 
26  const auto& key() const { return child<Expression>(0); }
27  const auto& value() const { return child<Expression>(1); }
28 
30  auto properties() const { return node::Properties{}; }
31 
32  bool operator==(const Element& other) const { return key() == other.key() && value() == other.value(); }
33 };
34 
35 inline Node to_node(Element f) { return Node(std::move(f)); }
36 } // namespace map
37 
39 class Map : public NodeBase, public hilti::trait::isCtor {
40 public:
41  Map(const std::vector<map::Element>& e, const Meta& m = Meta())
42  : NodeBase(nodes(e.size() ? Type(type::auto_) : Type(type::Bool()), std::move(e)), m) {}
43  Map(Type key, Type value, const std::vector<map::Element>& e, Meta m = Meta())
44  : NodeBase(nodes(type::Map(key, value, m), std::move(e)), m) {}
45 
46  const auto& keyType() const {
47  if ( auto t = children()[0].tryAs<type::Map>() )
48  return t->keyType();
49  else
50  return children()[0].as<Type>();
51  }
52 
53  const auto& valueType() const {
54  if ( auto t = children()[0].tryAs<type::Map>() )
55  return t->valueType();
56  else
57  return children()[0].as<Type>();
58  }
59 
60  auto value() const { return children<const map::Element>(1, -1); }
61 
62  void setElementType(Type k, Type v) { children()[0] = type::Map(std::move(k), std::move(v), meta()); }
63 
64  void setValue(std::vector<map::Element> elems) {
65  children().erase(children().begin() + 1, children().end());
66  for ( auto&& e : elems )
67  children().push_back(e);
68  }
69 
70  bool operator==(const Map& other) const {
71  return keyType() == other.keyType() && valueType() == other.valueType() && value() == other.value();
72  }
73 
75  const auto& type() const { return children()[0].as<Type>(); }
77  bool isConstant() const { return false; }
79  auto isLhs() const { return false; }
81  auto isTemporary() const { return true; }
83  auto isEqual(const Ctor& other) const { return node::isEqual(this, other); }
85  auto properties() const { return node::Properties{}; }
86 };
87 
88 } // namespace ctor
89 } // namespace hilti
auto properties() const
Definition: map.h:85
const auto & type() const
Definition: map.h:75
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:470
bool isConstant() const
Definition: map.h:77
Definition: meta.h:18
Definition: type.h:159
auto isTemporary() const
Definition: map.h:81
auto isEqual(const Ctor &other) const
Definition: map.h:83
auto properties() const
Definition: map.h:30
Definition: map.h:39
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
Definition: map.h:72
Definition: bool.h:13
Definition: node.h:113
Definition: ctor.h:15
Definition: map.h:21
auto & meta() const
Definition: node.h:474
auto isLhs() const
Definition: map.h:79
Definition: node.h:358