Spicy
struct.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/ctor.h>
10 #include <hilti/ast/expression.h>
11 #include <hilti/ast/id.h>
12 #include <hilti/ast/types/struct.h>
13 
14 namespace hilti {
15 namespace ctor {
16 
17 namespace struct_ {
19 using Field = std::pair<ID, Expression>;
20 } // namespace struct_
21 
23 class Struct : public NodeBase, public hilti::trait::isCtor {
24 public:
25  Struct(std::vector<struct_::Field> f, Meta m = Meta())
26  : NodeBase(nodes(type::unknown, std::move(f)), std::move(m)) {}
27  Struct(std::vector<struct_::Field> f, Type t, Meta m = Meta())
28  : NodeBase(nodes(std::move(t), std::move(f)), std::move(m)) {}
29 
31  auto ids() const { return childsOfType<ID>(); }
32 
34  auto values() const { return childsOfType<Expression>(); }
35 
37  auto fields() const { return util::zip2(ids(), values()); }
38 
40  std::optional<struct_::Field> field(const ID& id) const {
41  for ( auto f : fields() ) {
42  if ( f.first == id )
43  return f;
44  }
45 
46  return {};
47  }
48 
49  bool operator==(const Struct& other) const { return ids() == other.ids() && values() == other.values(); }
50 
52  Type type() const {
53  if ( auto t = childs()[0].as<Type>(); ! t.isA<type::Unknown>() )
54  return type::effectiveType(t);
55 
56  auto f = util::transform(fields(), [](auto& x) {
57  return type::struct_::Field(x.first, x.second.type(), {}, x.first.meta());
58  });
59  return type::Struct(f, meta());
60  }
61 
63  bool isConstant() const { return true; }
65  auto isLhs() const { return false; }
67  auto isTemporary() const { return true; }
69  auto isEqual(const Ctor& other) const { return node::isEqual(this, other); }
70 
72  auto properties() const { return node::Properties{}; }
73 };
74 
75 } // namespace ctor
76 } // namespace hilti
Type type() const
Definition: struct.h:52
auto isEqual(const Ctor &other) const
Definition: struct.h:69
Definition: struct.h:23
auto values() const
Definition: struct.h:34
Definition: struct.h:118
auto fields() const
Definition: struct.h:37
std::optional< struct_::Field > field(const ID &id) const
Definition: struct.h:40
std::list< std::pair< A, B > > zip2(const std::list< A > &lhs, const std::list< B > &rhs)
Definition: util.h:475
Definition: meta.h:18
auto isLhs() const
Definition: struct.h:65
auto ids() const
Definition: struct.h:31
Definition: struct.h:23
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
auto isTemporary() const
Definition: struct.h:67
Definition: ctor.h:15
auto properties() const
Definition: struct.h:72
auto transform(const std::vector< X > &x, F f)
Definition: util.h:86
bool isConstant() const
Definition: struct.h:63
Definition: unknown.h:13
Definition: id.h:18
Definition: node.h:318