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 <functional>
7 #include <optional>
8 #include <set>
9 #include <utility>
10 #include <vector>
11 
12 #include <hilti/ast/attribute.h>
13 #include <hilti/ast/declaration.h>
14 #include <hilti/ast/declarations/expression.h>
15 #include <hilti/ast/declarations/field.h>
16 #include <hilti/ast/expression.h>
17 #include <hilti/ast/expressions/grouping.h>
18 #include <hilti/ast/expressions/keyword.h>
19 #include <hilti/ast/expressions/unresolved-operator.h>
20 #include <hilti/ast/function.h>
21 #include <hilti/ast/id.h>
22 #include <hilti/ast/operators/reference.h>
23 #include <hilti/ast/scope.h>
24 #include <hilti/ast/type.h>
25 #include <hilti/ast/types/function.h>
26 #include <hilti/ast/types/reference.h>
27 #include <hilti/ast/types/unknown.h>
28 
29 namespace hilti {
30 namespace type {
31 
34 public:
35  Struct(std::vector<Declaration> fields, Meta m = Meta()) : TypeBase(nodes(node::none, std::move(fields)), m) {}
36 
37  Struct(std::vector<type::function::Parameter> params, std::vector<Declaration> fields, Meta m = Meta())
38  : TypeBase(nodes(node::none, std::move(fields),
39  util::transform(params,
40  [](auto p) {
41  p.setIsTypeParameter();
42  return Declaration(p);
43  })),
44  std::move(m)) {}
45 
46  Struct(Wildcard /*unused*/, Meta m = Meta()) : TypeBase(nodes(node::none), m), _wildcard(true) {}
47 
48  NodeRef selfRef() const {
49  if ( children()[0].isA<Declaration>() )
50  return NodeRef(children()[0]);
51  else
52  return {};
53  }
54 
55  auto hasFinalizer() const { return field("~finally").has_value(); }
56  auto parameters() const { return childrenOfType<type::function::Parameter>(); }
57  auto parameterRefs() const { return childRefsOfType<type::function::Parameter>(); }
58 
59  auto fields() const { return childrenOfType<declaration::Field>(); }
60 
61  hilti::optional_ref<const declaration::Field> field(const ID& id) const {
62  for ( const auto& f : fields() ) {
63  if ( f.id() == id )
64  return f;
65  }
66 
67  return {};
68  }
69 
70  hilti::node::Set<const declaration::Field> fields(const ID& id) const {
72  for ( const auto& f : fields() ) {
73  if ( f.id() == id )
74  x.insert(f);
75  }
76 
77  return x;
78  }
79 
80  void addField(Declaration f) {
81  assert(f.isA<declaration::Field>());
82  addChild(std::move(f));
83  }
84 
85  bool operator==(const Struct& other) const { return fields() == other.fields(); }
86 
88  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
90  auto _isResolved(ResolvedState* rstate) const {
91  for ( const auto& c : children() ) {
92  if ( auto f = c.tryAs<declaration::Field>() ) {
93  if ( ! f->isResolved(rstate) )
94  return false;
95  }
96  else if ( auto p = c.tryAs<type::function::Parameter>() )
97  if ( ! p->isResolved(rstate) )
98  return false;
99  }
100 
101  return true;
102  }
103 
105  auto typeParameters() const {
106  std::vector<Node> params;
107  for ( const auto& f : fields() )
108  params.emplace_back(f.type());
109  return params;
110  }
112  auto isWildcard() const { return _wildcard; }
113 
115  auto properties() const { return node::Properties{}; }
116 
123  static void setSelf(Node* n) {
124  assert(n->isA<type::Struct>());
125  Expression self =
126  expression::Keyword(expression::keyword::Kind::Self, type::ValueReference(NodeRef(*n)), n->meta());
127  Declaration d = declaration::Expression("self", std::move(self), declaration::Linkage::Private, n->meta());
128  n->children()[0] = std::move(d);
129  }
130 
131 private:
132  bool _wildcard = false;
133 };
134 
135 } // namespace type
136 } // namespace hilti
Definition: declaration.h:53
const Node none
Definition: node.cc:14
Definition: keyword.h:39
const auto & children() const
Definition: node.h:470
void addChild(Node n)
Definition: node.h:459
Definition: struct.h:33
auto isWildcard() const
Definition: struct.h:112
auto properties() const
Definition: struct.h:115
auto isEqual(const Type &other) const
Definition: struct.h:88
Definition: type.h:37
Definition: meta.h:18
Definition: reference.h:83
auto typeParameters() const
Definition: struct.h:105
Definition: type.h:159
auto _isResolved(ResolvedState *rstate) const
Definition: struct.h:90
Definition: expression.h:17
Definition: optional-ref.h:22
Definition: node.h:42
Definition: parameter.h:47
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
Definition: type.h:198
Definition: node.h:113
Definition: node-ref.h:44
Definition: type.h:33
Definition: type.h:269
Definition: field.h:21
static void setSelf(Node *n)
Definition: struct.h:123
Definition: id.h:18