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::ctor {
17 
18 namespace map {
20 class Element : public NodeBase {
21 public:
22  Element(Expression k, Expression v, Meta m = Meta()) : NodeBase(nodes(std::move(k), std::move(v)), std::move(m)) {}
23  Element(Meta m = Meta()) : NodeBase(nodes(node::none, node::none), std::move(m)) {}
24 
25  const auto& key() const { return child<Expression>(0); }
26  const auto& value() const { return child<Expression>(1); }
27 
29  auto properties() const { return node::Properties{}; }
30 
31  bool operator==(const Element& other) const { return key() == other.key() && value() == other.value(); }
32 };
33 
34 inline Node to_node(Element f) { return Node(std::move(f)); }
35 } // namespace map
36 
38 class Map : public NodeBase, public hilti::trait::isCtor {
39 public:
40  Map(const std::vector<map::Element>& e, const Meta& m = Meta())
41  : NodeBase(nodes(e.size() ? Type(type::auto_) : Type(type::Bool()), e), m) {}
42  Map(const Type& key, const Type& value, const std::vector<map::Element>& e, const Meta& m = Meta())
43  : NodeBase(nodes(type::Map(key, value, m), e), m) {}
44 
45  const auto& keyType() const {
46  if ( auto t = children()[0].tryAs<type::Map>() )
47  return t->keyType();
48  else
49  return children()[0].as<Type>();
50  }
51 
52  const auto& valueType() const {
53  if ( auto t = children()[0].tryAs<type::Map>() )
54  return t->valueType();
55  else
56  return children()[0].as<Type>();
57  }
58 
59  auto value() const { return children<const map::Element>(1, -1); }
60 
61  void setElementType(const Type& k, const Type& v) { children()[0] = type::Map(k, v, meta()); }
62 
63  void setValue(const std::vector<map::Element>& elems) {
64  children().erase(children().begin() + 1, children().end());
65  for ( auto&& e : elems )
66  children().emplace_back(e);
67  }
68 
69  bool operator==(const Map& other) const {
70  return keyType() == other.keyType() && valueType() == other.valueType() && value() == other.value();
71  }
72 
74  const auto& type() const { return children()[0].as<Type>(); }
76  bool isConstant() const { return false; }
78  auto isLhs() const { return false; }
80  auto isTemporary() const { return true; }
82  auto isEqual(const Ctor& other) const { return node::isEqual(this, other); }
84  auto properties() const { return node::Properties{}; }
85 };
86 
87 } // namespace hilti::ctor
auto properties() const
Definition: map.h:84
const auto & type() const
Definition: map.h:74
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:471
bool isConstant() const
Definition: map.h:76
Definition: meta.h:19
Definition: type.h:160
auto isTemporary() const
Definition: map.h:80
auto isEqual(const Ctor &other) const
Definition: map.h:82
auto properties() const
Definition: map.h:29
Definition: map.h:38
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:97
Definition: map.h:71
Definition: bool.h:12
Definition: node.h:111
Definition: ctor.h:15
Definition: map.h:20
auto & meta() const
Definition: node.h:475
auto isLhs() const
Definition: map.h:78
Definition: node.h:359