Spicy
attribute.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <algorithm>
6 #include <optional>
7 #include <string>
8 #include <string_view>
9 #include <utility>
10 
11 #include <hilti/ast/expression.h>
12 #include <hilti/ast/forward.h>
13 #include <hilti/ast/node.h>
14 #include <hilti/base/util.h>
15 
16 namespace hilti {
17 
18 namespace attribute {
19 
30 class Kind {
31 public:
32  explicit Kind(std::string name) : _name(std::move(name)) { _register(); }
33 
34  Kind() = delete;
35  Kind(const Kind& other) = default;
36  Kind(Kind&& other) = default;
37 
38  ~Kind() = default; // not unregistering here, so that attribute remains known
39 
40  operator std::string() const { return _name; }
41 
42  Kind& operator=(const Kind& other) = default;
43  Kind& operator=(Kind&& other) = default;
44 
45  bool operator==(const Kind& other) const { return _name == other._name; }
46  bool operator!=(const Kind& other) const { return ! operator==(other); }
47 
48  // Returns a corresponding attribute kind iff the string matches to one of
49  // the known attributes created so far. Otherwise throws an `out_of_range`
50  // exception.
51  static Kind fromString(const std::string_view& s) {
52  if ( _known_attributes && _known_attributes->contains(std::string(s)) )
53  return Kind(std::string(s));
54  else
55  throw std::out_of_range("unknown attribute kind: " + std::string(s));
56  }
57 
58 private:
59  void _register();
60 
61  std::string _name = "<unset attribute>";
62  static std::set<std::string>* _known_attributes;
63 };
64 
66 inline bool isOneOf(const Kind& kind, std::initializer_list<Kind> kinds) {
67  return std::ranges::find(kinds, kind) != kinds.end();
68 }
69 
70 inline auto to_string(const Kind& kind) { return std::string(kind); }
71 
72 inline std::ostream& operator<<(std::ostream& out, const Kind& x) {
73  out << to_string(x);
74  return out;
75 }
76 
77 namespace kind {
78 inline auto from_string(const std::string_view& s) { return Kind::fromString(s); }
79 
80 // In the following, we predefine all attributes that are part of the HILTI language.
81 
82 const Kind AlwaysEmit("&always-emit");
83 const Kind Anchor("&anchor");
84 const Kind Anonymous("&anonymous");
85 const Kind Convert("&convert");
86 const Kind Cxxname("&cxxname");
87 const Kind CxxAnyAsPtr("&cxx-any-as-ptr");
88 const Kind Debug("&debug");
89 const Kind Default("&default");
90 const Kind HavePrototype("&have_prototype");
91 const Kind Internal("&internal");
92 const Kind NeededByFeature("&needed-by-feature");
93 const Kind NoEmit("&no-emit");
94 const Kind Nosub("&nosub");
95 const Kind OnHeap("&on-heap");
96 const Kind Optional("&optional");
97 const Kind Priority("&priority");
98 const Kind Public("&public");
99 const Kind RequiresTypeFeature("&requires-type-feature");
100 const Kind Static("&static");
101 
102 } // namespace kind
103 } // namespace attribute
104 
106 class Attribute : public Node {
107 public:
109  const auto& kind() const { return _kind; }
110 
112  auto hasValue() const { return child(0) != nullptr; }
113 
119  Node* value() const { return child(0); }
120 
128 
137 
148 
159 
160  node::Properties properties() const final {
161  auto p = node::Properties{{"tag", to_string(kind())}};
162  return Node::properties() + std::move(p);
163  }
164 
173  static auto create(ASTContext* ctx, const attribute::Kind& kind, Expression* v, const Meta& m = Meta()) {
174  return ctx->make<Attribute>(ctx, {v}, kind, m);
175  }
176 
183  static auto create(ASTContext* ctx, const attribute::Kind& kind, const Meta& m = Meta()) {
184  return create(ctx, kind, nullptr, m);
185  }
186 
187 protected:
189  : Node(ctx, NodeTags, std::move(children), std::move(m)), _kind(std::move(kind)) {}
190 
191  std::string _dump() const override;
192 
193  HILTI_NODE_0(Attribute, final);
194 
195 private:
196  attribute::Kind _kind;
197 };
198 
200 class AttributeSet : public Node {
201 public:
203  auto attributes() const { return children<Attribute>(0, {}); }
204 
211  Attribute* find(const attribute::Kind& kind) const;
212 
218  hilti::node::Set<Attribute> findAll(const attribute::Kind& kind) const;
219 
221  void add(ASTContext* ctx, Attribute* a) {
222  addChild(ctx, a);
223  // Combine this location with the attribute's location so this spans the range
224  setMeta(meta().mergeLocation(a->location()));
225  }
226 
228  void remove(const attribute::Kind& kind);
229 
231  operator bool() const { return ! attributes().empty(); }
232 
233  static auto create(ASTContext* ctx, const Attributes& attrs = {}, Meta m = Meta()) {
234  return ctx->make<AttributeSet>(ctx, attrs, std::move(m));
235  }
236 
237 protected:
245  : Node(ctx, NodeTags, std::move(children), std::move(m)) {}
246 
252  AttributeSet(ASTContext* ctx, Meta m = Meta()) : Node(ctx, {node::tag::AttributeSet}, {}, std::move(m)) {}
253 
254  std::string _dump() const override;
255 
256  HILTI_NODE_0(AttributeSet, final);
257 };
258 
259 } // namespace hilti
260 
261 namespace std {
262 template<>
263 struct hash<hilti::attribute::Kind> {
264  size_t operator()(const hilti::attribute::Kind& x) const { return std::hash<std::string>()(x); }
265 };
266 } // namespace std
Definition: ast-context.h:121
T * make(Args &&... args)
Definition: ast-context.h:366
Definition: attribute.h:106
static auto create(ASTContext *ctx, const attribute::Kind &kind, Expression *v, const Meta &m=Meta())
Definition: attribute.h:173
std::string _dump() const override
Definition: attribute.cc:80
Result< Expression * > valueAsExpression() const
Definition: attribute.cc:24
Node * value() const
Definition: attribute.h:119
Result< int64_t > valueAsInteger() const
Definition: attribute.cc:45
Result< bool > coerceValueTo(Builder *builder, QualifiedType *dst)
Definition: attribute.cc:60
static auto create(ASTContext *ctx, const attribute::Kind &kind, const Meta &m=Meta())
Definition: attribute.h:183
const auto & kind() const
Definition: attribute.h:109
node::Properties properties() const final
Definition: attribute.h:160
Result< std::string > valueAsString() const
Definition: attribute.cc:34
auto hasValue() const
Definition: attribute.h:112
Definition: attribute.h:200
std::string _dump() const override
Definition: attribute.cc:82
hilti::node::Set< Attribute > findAll(const attribute::Kind &kind) const
Definition: attribute.cc:92
void add(ASTContext *ctx, Attribute *a)
Definition: attribute.h:221
auto attributes() const
Definition: attribute.h:203
void remove(const attribute::Kind &kind)
Definition: attribute.cc:102
Attribute * find(const attribute::Kind &kind) const
Definition: attribute.cc:84
AttributeSet(ASTContext *ctx, Meta m=Meta())
Definition: attribute.h:252
AttributeSet(ASTContext *ctx, Nodes children, Meta m=Meta())
Definition: attribute.h:244
Definition: builder.h:36
Definition: expression.h:15
Definition: meta.h:30
Definition: node.h:240
void setMeta(Meta m)
Definition: node.h:312
void addChild(ASTContext *ctx, Node *n)
Definition: node.h:522
const auto & location() const
Definition: node.h:309
const auto & children() const
Definition: node.h:364
const auto & meta() const
Definition: node.h:306
T * child(unsigned int i) const
Definition: node.h:374
virtual node::Properties properties() const
Definition: node.h:891
Definition: forward.h:758
Definition: type.h:362
Definition: attribute.h:30
Definition: result.h:71