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/auto.h>
13 #include <hilti/ast/types/struct.h>
14 
15 namespace hilti {
16 namespace ctor {
17 
18 namespace struct_ {
20 class Field : public NodeBase {
21 public:
22  Field(ID id, Expression e, Meta m = Meta()) : NodeBase(nodes(std::move(id), std::move(e)), std::move(m)) {}
23  Field(Meta m = Meta()) : NodeBase(nodes(node::none, node::none), std::move(m)) {}
24 
25  const auto& id() const { return child<ID>(0); }
26  const auto& expression() const { return child<Expression>(1); }
27 
29  auto properties() const { return node::Properties{}; }
30 
31  bool operator==(const Field& other) const { return id() == other.id() && expression() == other.expression(); }
32 };
33 
34 inline Node to_node(Field f) { return Node(std::move(f)); }
35 } // namespace struct_
36 
38 class Struct : public NodeBase, public hilti::trait::isCtor {
39 public:
40  Struct(std::vector<struct_::Field> f, Meta m = Meta()) : NodeBase(nodes(type::auto_, std::move(f)), std::move(m)) {}
41  Struct(std::vector<struct_::Field> f, Type t, Meta m = Meta())
42  : NodeBase(nodes(std::move(t), std::move(f)), std::move(m)) {}
43 
45  auto fields() const { return children<struct_::Field>(1, -1); }
46 
47  auto stype() const { return child<type::Struct>(0); }
48 
51  for ( const auto& f : fields() ) {
52  if ( f.id() == id )
53  return f;
54  }
55 
56  return {};
57  }
58 
59  void setType(type::Struct x) { children()[0] = std::move(x); }
60 
61  bool operator==(const Struct& other) const { return fields() == other.fields(); }
62 
64  const auto& type() const { return child<Type>(0); }
65 
67  bool isConstant() const { return true; }
69  auto isLhs() const { return false; }
71  auto isTemporary() const { return true; }
73  auto isEqual(const Ctor& other) const { return node::isEqual(this, other); }
74 
76  auto properties() const { return node::Properties{}; }
77 };
78 
79 } // namespace ctor
80 } // namespace hilti
auto isEqual(const Ctor &other) const
Definition: struct.h:73
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:470
Definition: struct.h:33
hilti::optional_ref< const struct_::Field > field(const ID &id) const
Definition: struct.h:50
auto fields() const
Definition: struct.h:45
Definition: meta.h:18
Definition: type.h:159
auto properties() const
Definition: struct.h:29
Definition: optional-ref.h:22
auto isLhs() const
Definition: struct.h:69
Definition: struct.h:38
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
auto isTemporary() const
Definition: struct.h:71
Definition: node.h:113
Definition: ctor.h:15
Definition: struct.h:20
auto properties() const
Definition: struct.h:76
bool isConstant() const
Definition: struct.h:67
const auto & type() const
Definition: struct.h:64
Definition: id.h:18
Definition: node.h:358