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/map.h>
13 #include <hilti/ast/types/unknown.h>
14 
15 namespace hilti {
16 namespace ctor {
17 
19 class Map : public NodeBase, public hilti::trait::isCtor {
20 public:
21  using Element = std::pair<Expression, Expression>;
22  Map(const std::vector<Element>& e, const Meta& m = Meta())
23  : NodeBase(nodes(node::none, node::none, _flatten(e)), m) {}
24  Map(Type key, Type value, const std::vector<Element>& e, Meta m = Meta())
25  : NodeBase(nodes(std::move(key), std::move(value), _flatten(e)), std::move(m)) {}
26 
27  auto keyType() const {
28  if ( auto t = childs()[0].tryAs<Type>() )
29  return type::effectiveType(*t);
30  else {
31  if ( childs().size() < 2 )
32  return type::unknown;
33 
34  return childs()[2].as<Expression>().type();
35  }
36  }
37 
38  auto elementType() const {
39  if ( auto t = childs()[1].tryAs<Type>() )
40  return type::effectiveType(*t);
41  else {
42  if ( childs().size() < 3 )
43  return type::unknown;
44 
45  return childs()[3].as<Expression>().type();
46  }
47  }
48 
49  auto value() const {
50  auto exprs = childs<Expression>(2, -1);
51  std::vector<Element> elems;
52  for ( auto&& i = exprs.begin(); i != exprs.end(); i += 2 )
53  elems.emplace_back(std::make_pair(std::move(*i), std::move(*(i + 1))));
54  return elems;
55  }
56 
57  bool operator==(const Map& other) const {
58  return keyType() == other.keyType() && elementType() == other.elementType() && value() == other.value();
59  }
60 
62  auto type() const { return type::Map(keyType(), elementType(), meta()); }
64  bool isConstant() const { return false; }
66  auto isLhs() const { return false; }
68  auto isTemporary() const { return true; }
70  auto isEqual(const Ctor& other) const { return node::isEqual(this, other); }
72  auto properties() const { return node::Properties{}; }
73 
74 private:
75  std::vector<Expression> _flatten(const std::vector<Element>& elems) {
76  std::vector<Expression> exprs;
77  for ( auto&& e : elems ) {
78  exprs.emplace_back(e.first);
79  exprs.emplace_back(e.second);
80  }
81 
82  return exprs;
83  }
84 };
85 
86 } // namespace ctor
87 } // namespace hilti
auto properties() const
Definition: map.h:72
auto & childs() const
Definition: node.h:445
auto type() const
Definition: map.h:62
const Node none
Definition: node.cc:12
bool isConstant() const
Definition: map.h:64
Definition: meta.h:18
auto isTemporary() const
Definition: map.h:68
auto isEqual(const Ctor &other) const
Definition: map.h:70
Definition: map.h:19
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
Definition: map.h:55
Definition: ctor.h:15
auto & meta() const
Definition: node.h:449
auto isLhs() const
Definition: map.h:66
Definition: node.h:318