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 {
19 namespace type {
20 
23 public:
24  Union(std::vector<Declaration> fields, Meta m = Meta())
25  : TypeBase(nodes(node::none, std::move(fields)), std::move(m)) {}
26  Union(Wildcard /*unused*/, Meta m = Meta()) : TypeBase(nodes(node::none), std::move(m)), _wildcard(true) {}
27 
28  auto fields() const { return childsOfType<declaration::Field>(); }
29 
30  hilti::optional_ref<const declaration::Field> field(const ID& id) const {
31  for ( const auto& f : fields() ) {
32  if ( f.id() == id )
33  return f;
34  }
35 
36  return {};
37  }
38 
39  unsigned int index(const ID& id) const {
40  for ( const auto&& [i, f] : util::enumerate(fields()) ) {
41  if ( f.id() == id )
42  return i + 1;
43  }
44 
45  return 0;
46  }
47 
48  bool operator==(const Union& other) const { return fields() == other.fields(); }
49 
51  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
52 
54  auto _isResolved(ResolvedState* rstate) const {
55  for ( auto c = ++childs().begin(); c != childs().end(); c++ ) {
56  if ( ! c->as<declaration::Field>().isResolved(rstate) )
57  return false;
58  }
59 
60  return true;
61  }
62 
64  auto typeParameters() const {
65  std::vector<Node> params;
66  for ( auto c = ++childs().begin(); c != childs().end(); c++ )
67  params.emplace_back(c->as<declaration::Field>().type());
68  return params;
69  }
71  auto isWildcard() const { return _wildcard; }
72 
74  auto properties() const { return node::Properties{}; }
75 
83  static Union addField(const Union& s, declaration::Field f) {
84  auto x = Type(s)._clone().as<Union>();
85  x.addChild(std::move(f));
86  return x;
87  }
88 
89 private:
90  bool _wildcard = false;
91 };
92 
93 } // namespace type
94 } // namespace hilti
const Node none
Definition: node.cc:14
constexpr auto enumerate(T &&iterable)
Definition: util.h:111
void addChild(Node n)
Definition: node.h:459
Definition: type.h:37
Definition: union.h:22
Definition: meta.h:18
auto properties() const
Definition: union.h:74
auto typeParameters() const
Definition: union.h:64
Definition: type.h:159
Definition: optional-ref.h:22
auto _isResolved(ResolvedState *rstate) const
Definition: union.h:54
auto isWildcard() const
Definition: union.h:71
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
static Union addField(const Union &s, declaration::Field f)
Definition: union.h:83
Definition: type.h:198
Definition: type.h:33
Definition: type.h:269
const auto & childs() const
Definition: node.h:470
auto isEqual(const Type &other) const
Definition: union.h:51
Definition: field.h:21
Definition: id.h:18