Spicy
union.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 #include <vector>
7 
8 #include <hilti/ast/attribute.h>
9 #include <hilti/ast/expression.h>
10 #include <hilti/ast/function.h>
11 #include <hilti/ast/id.h>
12 #include <hilti/ast/type.h>
13 #include <hilti/ast/types/function.h>
14 #include <hilti/ast/types/unknown.h>
15 
16 namespace hilti {
17 namespace type {
18 
19 namespace union_ {
21 class Field : public NodeBase {
22 public:
23  Field() : NodeBase({ID("<no id>"), type::unknown, node::none}, Meta()) {}
24  Field(ID id, Type t, std::optional<AttributeSet> attrs = {}, Meta m = Meta())
25  : NodeBase(nodes(std::move(id), std::move(t), std::move(attrs)), std::move(m)) {}
26 
27  const auto& id() const { return child<ID>(0); }
28  auto type() const { return type::effectiveType(child<Type>(1)); }
29  auto attributes() const { return childs()[2].tryReferenceAs<AttributeSet>(); }
30 
32  auto properties() const { return node::Properties{}; }
33 
34  bool operator==(const Field& other) const {
35  return id() == other.id() && type() == other.type() && attributes() == other.attributes();
36  }
37 
45  static Field setAttributes(const Field& f, const AttributeSet& attrs) {
46  auto x = Field(f);
47  x.childs()[2] = attrs;
48  return x;
49  }
50 };
51 
52 inline Node to_node(Field f) { return Node(std::move(f)); }
53 
54 } // namespace union_
55 
58 public:
59  Union(std::vector<union_::Field> fields, Meta m = Meta())
60  : TypeBase(nodes(node::none, std::move(fields)), std::move(m)) {}
61  Union(Wildcard /*unused*/, Meta m = Meta()) : TypeBase(nodes(node::none), std::move(m)), _wildcard(true) {}
62 
63  auto fields() const { return childsOfType<union_::Field>(); }
64 
65  auto types() const {
66  std::vector<Type> types;
67  for ( auto c = ++childs().begin(); c != childs().end(); c++ )
68  types.push_back(c->as<union_::Field>().type());
69 
70  return types;
71  }
72 
73  auto ids() const {
74  std::vector<ID> ids;
75  for ( auto c = ++childs().begin(); c != childs().end(); c++ )
76  ids.push_back(c->as<union_::Field>().id());
77 
78  return ids;
79  }
80 
81  std::optional<union_::Field> field(const ID& id) const {
82  for ( auto f : fields() ) {
83  if ( f.id() == id )
84  return f;
85  }
86 
87  return {};
88  }
89 
90  unsigned int index(const ID& id) const {
91  for ( const auto&& [i, f] : util::enumerate(fields()) ) {
92  if ( f.id() == id )
93  return i + 1;
94  }
95 
96  return 0;
97  }
98 
99  auto fields(const ID& id) const {
100  std::vector<union_::Field> x;
101 
102  for ( const auto& f : fields() ) {
103  if ( f.id() == id )
104  x.push_back(f);
105  }
106 
107  return x;
108  }
109 
110  bool operator==(const Union& other) const {
111  if ( typeID() && other.typeID() )
112  return *typeID() == *other.typeID();
113 
114  return fields() == other.fields();
115  }
116 
118  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
120  auto typeParameters() const {
121  std::vector<Node> params;
122  for ( auto c = ++childs().begin(); c != childs().end(); c++ )
123  params.emplace_back(c->as<union_::Field>().type());
124  return params;
125  }
127  auto isWildcard() const { return _wildcard; }
128 
130  auto properties() const { return node::Properties{}; }
131 
139  static Union addField(const Union& s, union_::Field f) {
140  auto x = Type(s)._clone().as<Union>();
141  x.addChild(std::move(f));
142  return x;
143  }
144 
145 private:
146  bool _wildcard = false;
147 };
148 
149 } // namespace type
150 } // namespace hilti
static Union addField(const Union &s, union_::Field f)
Definition: union.h:139
auto & childs() const
Definition: node.h:445
static Field setAttributes(const Field &f, const AttributeSet &attrs)
Definition: union.h:45
Definition: union.h:21
const Node none
Definition: node.cc:12
constexpr auto enumerate(T &&iterable)
Definition: util.h:153
void addChild(Node n)
Definition: node.h:434
Definition: type.h:27
Definition: union.h:57
Definition: meta.h:18
Definition: attribute.h:159
auto properties() const
Definition: union.h:130
auto typeParameters() const
Definition: union.h:120
auto properties() const
Definition: union.h:32
auto isWildcard() const
Definition: union.h:127
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
std::optional< ID > typeID() const
Definition: type.h:163
Definition: type.h:152
Definition: node.h:97
Definition: type.h:23
Definition: type.h:249
auto isEqual(const Type &other) const
Definition: union.h:118
Definition: id.h:18
Definition: node.h:318