Spicy
union.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <functional>
6 #include <utility>
7 #include <vector>
8 
9 #include <hilti/ast/attribute.h>
10 #include <hilti/ast/declarations/field.h>
11 #include <hilti/ast/expression.h>
12 #include <hilti/ast/function.h>
13 #include <hilti/ast/id.h>
14 #include <hilti/ast/type.h>
15 #include <hilti/ast/types/function.h>
16 #include <hilti/ast/types/unknown.h>
17 
18 namespace hilti::type {
19 
22 public:
23  Union(std::vector<Declaration> fields, Meta m = Meta())
24  : TypeBase(nodes(node::none, std::move(fields)), std::move(m)) {}
25  Union(Wildcard /*unused*/, Meta m = Meta()) : TypeBase(nodes(node::none), std::move(m)), _wildcard(true) {}
26 
27  auto fields() const { return childrenOfType<declaration::Field>(); }
28 
29  hilti::optional_ref<const declaration::Field> field(const ID& id) const {
30  for ( const auto& f : fields() ) {
31  if ( f.id() == id )
32  return f;
33  }
34 
35  return {};
36  }
37 
38  unsigned int index(const ID& id) const {
39  for ( const auto&& [i, f] : util::enumerate(fields()) ) {
40  if ( f.id() == id )
41  return i + 1;
42  }
43 
44  return 0;
45  }
46 
47  bool operator==(const Union& other) const { return fields() == other.fields(); }
48 
50  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
51 
53  auto _isResolved(ResolvedState* rstate) const {
54  for ( auto c = ++children().begin(); c != children().end(); c++ ) {
55  if ( ! c->as<declaration::Field>().isResolved(rstate) )
56  return false;
57  }
58 
59  return true;
60  }
61 
63  auto typeParameters() const {
64  std::vector<Node> params;
65  for ( auto c = ++children().begin(); c != children().end(); c++ )
66  params.emplace_back(c->as<declaration::Field>().type());
67  return params;
68  }
70  auto isWildcard() const { return _wildcard; }
71 
73  auto properties() const { return node::Properties{}; }
74 
82  static Union addField(const Union& s, declaration::Field f) {
83  auto x = Type(s)._clone().as<Union>();
84  x.addChild(std::move(f));
85  return x;
86  }
87 
88 private:
89  bool _wildcard = false;
90 };
91 
92 } // namespace hilti::type
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:471
void addChild(Node n)
Definition: node.h:460
Definition: type.h:36
Definition: union.h:21
Definition: meta.h:19
auto properties() const
Definition: union.h:73
auto typeParameters() const
Definition: union.h:63
Definition: type.h:158
Definition: optional-ref.h:22
auto _isResolved(ResolvedState *rstate) const
Definition: union.h:53
auto isWildcard() const
Definition: union.h:70
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:97
static Union addField(const Union &s, declaration::Field f)
Definition: union.h:82
Definition: type.h:197
Definition: type.h:32
Definition: type.h:268
auto isEqual(const Type &other) const
Definition: union.h:50
Definition: field.h:20
Definition: type.h:25
Definition: id.h:18