Spicy
enum.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/id.h>
10 #include <hilti/ast/type.h>
11 
12 namespace hilti {
13 namespace type {
14 
15 namespace enum_ {
18 public:
19  Label() : NodeBase({ID("<no id>")}, Meta()) {}
20  Label(ID id, Meta m = Meta()) : NodeBase({std::move(id)}, std::move(m)) {}
21  Label(ID id, int v, Meta m = Meta()) : NodeBase({std::move(id)}, std::move(m)), _value(v) {}
22 
23  const auto& id() const { return child<ID>(0); }
24  auto value() const { return _value; }
25 
26  bool operator==(const Label& other) const { return id() == other.id() && value() == other.value(); }
27 
29  auto properties() const { return node::Properties{{"value", _value}}; }
30 
31 private:
32  int _value = -1;
33 };
34 
35 inline Node to_node(Label l) { return Node(std::move(l)); }
36 
37 } // namespace enum_
38 
41 public:
42  Enum(std::vector<enum_::Label> l, Meta m = Meta())
43  : TypeBase(nodes(_normalizeLabels(std::move(l))), std::move(m)) {}
44  Enum(Wildcard /*unused*/, Meta m = Meta()) : TypeBase(std::move(m)), _wildcard(true) {}
45 
46  std::vector<enum_::Label> labels() const { return childs<enum_::Label>(0, -1); }
47 
52  std::vector<enum_::Label> uniqueLabels() const {
53  auto pred_gt = [](const enum_::Label& e1, const enum_::Label& e2) { return e1.value() > e2.value(); };
54  auto pred_eq = [](const enum_::Label& e1, const enum_::Label& e2) { return e1.value() == e2.value(); };
55  std::vector<enum_::Label> x = labels();
56  std::sort(x.begin(), x.end(), pred_gt);
57  x.erase(std::unique(x.begin(), x.end(), pred_eq), x.end());
58  return x;
59  }
60 
61  std::optional<enum_::Label> label(const ID& id) const {
62  for ( auto l : labels() ) {
63  if ( l.id() == id )
64  return l;
65  }
66 
67  return {};
68  }
69 
70  bool operator==(const Enum& other) const {
71  if ( typeID() && other.typeID() )
72  return *typeID() == *other.typeID();
73 
74  return labels() == other.labels();
75  }
76 
78  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
80  auto typeParameters() const {
81  std::vector<Node> params;
82  for ( auto&& c : uniqueLabels() )
83  params.emplace_back(std::move(c));
84  return params;
85  }
87  auto isWildcard() const { return _wildcard; }
88 
90  auto properties() const { return node::Properties{}; }
91 
92 private:
93  static std::vector<enum_::Label> _normalizeLabels(std::vector<enum_::Label> /*labels*/);
94 
95  bool _wildcard = false;
96 };
97 
98 } // namespace type
99 } // namespace hilti
std::vector< enum_::Label > uniqueLabels() const
Definition: enum.h:52
auto isEqual(const Type &other) const
Definition: enum.h:78
Definition: meta.h:18
auto properties() const
Definition: enum.h:90
auto typeParameters() const
Definition: enum.h:80
auto properties() const
Definition: enum.h:29
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: enum.h:40
Definition: node.h:97
Definition: type.h:23
Definition: type.h:249
auto isWildcard() const
Definition: enum.h:87
Definition: enum.h:17
Definition: id.h:18
Definition: node.h:318