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::type {
22 
23 namespace bitfield {
24 
26 class Bits : public hilti::NodeBase {
27 public:
28  Bits() : NodeBase({ID("<no id>"), hilti::node::none}, Meta()) {}
29  Bits(ID id, int lower, int upper, int field_width, std::optional<AttributeSet> attrs = {}, Meta m = Meta())
30  : hilti::NodeBase(nodes(std::move(id),
32  hilti::type::UnsignedInteger(field_width)),
33  hilti::type::auto_, std::move(attrs)),
34  std::move(m)),
35  _lower(lower),
36  _upper(upper),
37  _field_width(field_width) {}
38 
39  const auto& id() const { return child<ID>(0); }
40  auto lower() const { return _lower; }
41  auto upper() const { return _upper; }
42  auto fieldWidth() const { return _field_width; }
43  auto attributes() const { return children()[3].tryAs<AttributeSet>(); }
44  const Type& ddType() const { return children()[1].as<hilti::declaration::Expression>().expression().type(); }
45  NodeRef ddRef() const { return NodeRef(children()[1]); }
46  const auto& itemType() const { return child<Type>(2); }
47 
49  auto properties() const {
50  return node::Properties{
51  {"lower", _lower},
52  {"upper", _upper},
53  {"field_width", _field_width},
54  };
55  }
56 
57  void setAttributes(const AttributeSet& attrs) { children()[3] = attrs; }
58  void setItemType(const Type& t) { children()[2] = t; }
59 
60  bool operator==(const Bits& other) const {
61  return id() == other.id() && _lower == other._lower && _upper == other._upper &&
62  _field_width == other._field_width && itemType() == other.itemType() &&
63  attributes() == other.attributes();
64  }
65 
66 private:
67  int _lower = 0;
68  int _upper = 0;
69  int _field_width = 0;
70 };
71 
72 inline hilti::Node to_node(Bits f) { return hilti::Node(std::move(f)); }
73 
74 } // namespace bitfield
75 
77 class Bitfield : public hilti::TypeBase,
81 public:
82  Bitfield(int width, std::vector<bitfield::Bits> bits, const Meta& m = Meta())
83  : TypeBase(nodes(type::UnsignedInteger(width, m), hilti::type::auto_, std::move(bits)), m), _width(width) {}
84  Bitfield(Wildcard /*unused*/, Meta m = Meta())
85  : TypeBase({hilti::type::unknown, hilti::type::unknown}, std::move(m)), _wildcard(true) {}
86 
87  int width() const { return _width; }
88  auto bits() const { return children<bitfield::Bits>(2, -1); }
89  hilti::optional_ref<const bitfield::Bits> bits(const ID& id) const;
90  std::optional<int> bitsIndex(const ID& id) const;
91  const Type& parseType() const { return child<Type>(0); }
92  const Type& type() const { return child<Type>(1); }
93 
94  void addField(bitfield::Bits f) { addChild(std::move(f)); }
95  void setType(const Type& t) { children()[1] = t; }
96 
97  bool operator==(const Bitfield& other) const { return width() == other.width() && bits() == other.bits(); }
98 
100  auto isEqual(const Type& other) const { return node::isEqual(this, other); }
102  auto _isResolved(ResolvedState* rstate) const { return true; }
104  auto typeParameters() const { return hilti::util::slice(children(), 1); }
106  auto isWildcard() const { return _wildcard; }
108  auto properties() const { return node::Properties{}; }
109 
110 private:
111  int _width = 0;
112  bool _wildcard = false;
113 };
114 
115 } // namespace spicy::type
auto isEqual(const Type &other) const
Definition: bitfield.h:100
NodeBase(Meta meta)
Definition: node.h:367
auto typeParameters() const
Definition: bitfield.h:104
Definition: bitfield.h:26
auto properties() const
Definition: bitfield.h:49
const Node none
Definition: node.cc:14
Definition: bitfield.h:77
const auto & children() const
Definition: node.h:472
void addChild(Node n)
Definition: node.h:461
Definition: aliases.h:52
Definition: type.h:38
static Declaration createDollarDollarDeclaration(Type t)
Definition: keyword.h:69
Definition: meta.h:19
Definition: attribute.h:174
Definition: integer.h:53
Definition: type.h:160
Definition: optional-ref.h:22
Definition: expression.h:16
auto _isResolved(ResolvedState *rstate) const
Definition: bitfield.h:102
Definition: type.h:206
auto isWildcard() const
Definition: bitfield.h:106
Definition: node.h:112
auto properties() const
Definition: bitfield.h:108
Definition: node-ref.h:45
Definition: type.h:33
Definition: type.h:277
Definition: id.h:18
Definition: node.h:360