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::ctor {
16 
17 namespace struct_ {
19 class Field : public NodeBase {
20 public:
21  Field(ID id, Expression e, Meta m = Meta()) : NodeBase(nodes(std::move(id), std::move(e)), std::move(m)) {}
22  Field(Meta m = Meta()) : NodeBase(nodes(node::none, node::none), std::move(m)) {}
23 
24  const auto& id() const { return child<ID>(0); }
25  const auto& expression() const { return child<Expression>(1); }
26 
28  auto properties() const { return node::Properties{}; }
29 
30  bool operator==(const Field& other) const { return id() == other.id() && expression() == other.expression(); }
31 };
32 
33 inline Node to_node(Field f) { return Node(std::move(f)); }
34 } // namespace struct_
35 
37 class Struct : public NodeBase, public hilti::trait::isCtor {
38 public:
39  Struct(std::vector<struct_::Field> f, Meta m = Meta()) : NodeBase(nodes(type::auto_, std::move(f)), std::move(m)) {}
40  Struct(std::vector<struct_::Field> f, Type t, Meta m = Meta())
41  : NodeBase(nodes(std::move(t), std::move(f)), std::move(m)) {}
42 
44  auto fields() const { return children<struct_::Field>(1, -1); }
45 
46  auto stype() const { return child<type::Struct>(0); }
47 
50  for ( const auto& f : fields() ) {
51  if ( f.id() == id )
52  return f;
53  }
54 
55  return {};
56  }
57 
58  void setType(const Type& t) { children()[0] = t; }
59 
60  bool operator==(const Struct& other) const { return fields() == other.fields(); }
61 
63  const auto& type() const { return child<Type>(0); }
64 
66  bool isConstant() const { return true; }
68  auto isLhs() const { return false; }
70  auto isTemporary() const { return true; }
72  auto isEqual(const Ctor& other) const { return node::isEqual(this, other); }
73 
75  auto properties() const { return node::Properties{}; }
76 };
77 
78 } // namespace hilti::ctor
auto isEqual(const Ctor &other) const
Definition: struct.h:72
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:471
hilti::optional_ref< const struct_::Field > field(const ID &id) const
Definition: struct.h:49
auto fields() const
Definition: struct.h:44
Definition: meta.h:19
Definition: type.h:160
auto properties() const
Definition: struct.h:28
Definition: optional-ref.h:22
auto isLhs() const
Definition: struct.h:68
Definition: struct.h:37
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:97
auto isTemporary() const
Definition: struct.h:70
Definition: node.h:111
Definition: ctor.h:15
Definition: struct.h:19
auto properties() const
Definition: struct.h:75
bool isConstant() const
Definition: struct.h:66
const auto & type() const
Definition: struct.h:63
Definition: id.h:18
Definition: node.h:359