Spicy
bitfield.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/declarations/expression.h>
10 #include <hilti/ast/expression.h>
11 #include <hilti/ast/expressions/keyword.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/integer.h>
17 #include <hilti/ast/types/unknown.h>
18 
19 #include <spicy/ast/aliases.h>
20 
21 namespace spicy {
22 namespace type {
23 
24 namespace bitfield {
25 
27 class Bits : public hilti::NodeBase {
28 public:
29  Bits() : NodeBase({ID("<no id>"), hilti::node::none}, Meta()) {}
30  Bits(ID id, int lower, int upper, int field_width, std::optional<AttributeSet> attrs = {}, Meta m = Meta())
31  : hilti::NodeBase(nodes(std::move(id),
33  hilti::type::UnsignedInteger(field_width)),
34  hilti::type::auto_, std::move(attrs)),
35  std::move(m)),
36  _lower(lower),
37  _upper(upper),
38  _field_width(field_width) {}
39 
40  const auto& id() const { return child<ID>(0); }
41  auto lower() const { return _lower; }
42  auto upper() const { return _upper; }
43  auto fieldWidth() const { return _field_width; }
44  auto attributes() const { return childs()[3].tryAs<AttributeSet>(); }
45  const Type& ddType() const { return childs()[1].as<hilti::declaration::Expression>().expression().type(); }
46  NodeRef ddRef() const { return NodeRef(childs()[1]); }
47  const auto& itemType() const { return child<Type>(2); }
48 
50  auto properties() const {
51  return node::Properties{
52  {"lower", _lower},
53  {"upper", _upper},
54  {"field_width", _field_width},
55  };
56  }
57 
58  void setAttributes(AttributeSet attrs) { childs()[3] = std::move(attrs); }
59  void setItemType(Type t) { childs()[2] = std::move(t); }
60 
61  bool operator==(const Bits& other) const {
62  return id() == other.id() && _lower == other._lower && _upper == other._upper &&
63  _field_width == other._field_width && itemType() == other.itemType() &&
64  attributes() == other.attributes();
65  }
66 
67 private:
68  int _lower = 0;
69  int _upper = 0;
70  int _field_width = 0;
71 };
72 
73 inline hilti::Node to_node(Bits f) { return hilti::Node(std::move(f)); }
74 
75 } // namespace bitfield
76 
78 class Bitfield : public hilti::TypeBase,
82 public:
83  Bitfield(int width, std::vector<bitfield::Bits> bits, Meta m = Meta())
84  : TypeBase(nodes(type::UnsignedInteger(width, m), hilti::type::auto_, std::move(bits)), m), _width(width) {}
85  Bitfield(Wildcard /*unused*/, Meta m = Meta())
86  : TypeBase({hilti::type::unknown, hilti::type::unknown}, std::move(m)), _wildcard(true) {}
87 
88  int width() const { return _width; }
89  auto bits() const { return childs<bitfield::Bits>(2, -1); }
90  hilti::optional_ref<const bitfield::Bits> bits(const ID& id) const;
91  std::optional<int> bitsIndex(const ID& id) const;
92  const Type& parseType() const { return child<Type>(0); }
93  const Type& type() const { return child<Type>(1); }
94 
95  void addField(bitfield::Bits f) { addChild(std::move(f)); }
96  void setType(Type t) { childs()[1] = std::move(t); }
97 
98  bool operator==(const Bitfield& other) const { return width() == other.width() && bits() == other.bits(); }
99 
101  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
103  auto _isResolved(ResolvedState* rstate) const { return true; }
105  auto typeParameters() const { return hilti::util::slice(childs(), 1); }
107  auto isWildcard() const { return _wildcard; }
109  auto properties() const { return node::Properties{}; }
110 
111 private:
112  int _width = 0;
113  bool _wildcard = false;
114 };
115 
116 } // namespace type
117 } // namespace spicy
auto isEqual(const Type &other) const
Definition: bitfield.h:101
NodeBase(Meta meta)
Definition: node.h:365
auto typeParameters() const
Definition: bitfield.h:105
Definition: bitfield.h:27
auto properties() const
Definition: bitfield.h:50
const Node none
Definition: node.cc:14
Definition: bitfield.h:78
void addChild(Node n)
Definition: node.h:459
Definition: type.h:37
static Declaration createDollarDollarDeclaration(Type t)
Definition: keyword.h:64
Definition: meta.h:18
Definition: attribute.h:145
Definition: integer.h:54
Definition: type.h:159
Definition: optional-ref.h:22
Definition: expression.h:17
auto _isResolved(ResolvedState *rstate) const
Definition: bitfield.h:103
Definition: type.h:198
auto isWildcard() const
Definition: bitfield.h:107
Definition: node.h:113
auto properties() const
Definition: bitfield.h:109
Definition: node-ref.h:44
Definition: type.h:33
Definition: type.h:269
const auto & childs() const
Definition: node.h:470
std::vector< T > slice(const std::vector< T > &v, int begin, int end=-1)
Definition: util.h:154
Definition: id.h:18
Definition: node.h:358