Spicy
hook.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 
7 #include <hilti/ast/expressions/keyword.h>
8 #include <hilti/ast/function.h>
9 #include <hilti/ast/node.h>
10 #include <hilti/ast/types/function.h>
11 #include <hilti/ast/types/void.h>
12 
13 #include <spicy/ast/attribute.h>
14 #include <spicy/ast/forward.h>
15 #include <spicy/ast/node.h>
16 
17 namespace spicy {
18 
19 namespace type {
20 
21 class Unit;
22 
23 namespace unit::item {
24 class Field;
25 }
26 
27 } // namespace type
28 
29 namespace declaration {
30 
31 namespace hook {
32 
34 enum class Type {
39  Standard,
40 
42  ForEach,
43 
45  Error,
46 };
47 
48 namespace detail {
49 constexpr hilti::util::enum_::Value<Type> Types[] = {
50  {.value = Type::Standard, .name = "standard"},
51  {.value = Type::ForEach, .name = "foreach"},
52  {.value = Type::Error, .name = "error"},
53 };
54 
55 } // namespace detail
56 
57 constexpr auto to_string(Type cc) { return hilti::util::enum_::to_string(cc, detail::Types); }
58 } // namespace hook
59 
61 class Hook : public Declaration {
62 public:
63  ~Hook() override;
64 
65  auto function() const { return child<Function>(0); }
66  auto attributes() const { return function()->attributes(); }
67  auto dd() const { return child<Declaration>(1); }
68 
69  auto body() const { return function()->body(); }
70  auto ftype() const { return function()->ftype(); }
71  auto type() const { return function()->type(); }
72 
73  auto unitTypeIndex() { return _unit_type_index; }
74  auto unitFieldIndex() { return _unit_field_index; }
75 
76  hilti::Expression* priority() const {
77  if ( auto* attr = attributes()->find(attribute::kind::Priority) )
78  return *attr->valueAsExpression();
79  else
80  return nullptr;
81  }
82 
83  hook::Type hookType() const {
84  if ( attributes()->find(attribute::kind::Foreach) )
85  return hook::Type::ForEach;
86  else if ( attributes()->find(attribute::kind::Error) )
87  return hook::Type::Error;
88  else
89  return hook::Type::Standard;
90  }
91 
92  auto isDebug() const { return attributes()->find(attribute::kind::Debug); }
93 
94  void setUnitTypeIndex(hilti::ast::TypeIndex index) {
95  assert(index);
96  _unit_type_index = index;
97  }
98 
99  void setUnitFieldIndex(hilti::ast::DeclarationIndex index) {
100  assert(index);
101  _unit_field_index = index;
102  }
103 
104  void setDDType(ASTContext* ctx, QualifiedType* t) {
106  }
107 
108  void setParameters(ASTContext* ctx, const hilti::declaration::Parameters& params) {
109  ftype()->setParameters(ctx, params);
110  }
111  void setResult(ASTContext* ctx, QualifiedType* t) { function()->setResultType(ctx, t); }
112 
113  std::string_view displayName() const override { return "Spicy hook"; }
114  node::Properties properties() const final;
115 
116  static auto create(ASTContext* ctx, const hilti::declaration::Parameters& parameters, Statement* body,
117  AttributeSet* attrs, const Meta& m = Meta()) {
118  if ( ! attrs )
119  attrs = AttributeSet::create(ctx);
120 
121  auto* ftype = hilti::type::Function::create(ctx,
122  QualifiedType::create(ctx, hilti::type::Void::create(ctx, m),
123  hilti::Constness::Const),
124  parameters, hilti::type::function::Flavor::Hook,
125  hilti::type::function::CallingConvention::Standard, m);
126  auto* func = hilti::Function::create(ctx, hilti::ID(), ftype, body, attrs, m);
127  return ctx->make<Hook>(ctx, {func, nullptr}, m);
128  }
129 
130 protected:
131  Hook(ASTContext* ctx, Nodes children, Meta m = Meta())
132  : Declaration(ctx, NodeTags, std::move(children), hilti::ID(), hilti::declaration::Linkage::Private,
133  std::move(m)) {}
134 
135  SPICY_NODE_1(declaration::Hook, Declaration, final);
136 
137 private:
138  hilti::ast::TypeIndex _unit_type_index;
139  hilti::ast::DeclarationIndex _unit_field_index;
140 };
141 
142 using Hooks = NodeVector<Hook>;
143 
144 } // namespace declaration
145 } // namespace spicy
Definition: ast-context.h:121
Definition: attribute.h:200
Definition: declaration.h:48
Definition: expression.h:15
Definition: id.h:15
Definition: meta.h:30
void setChild(ASTContext *ctx, size_t idx, Node *n)
Definition: node.h:602
const auto & children() const
Definition: node.h:364
Definition: type.h:362
static auto create(ASTContext *ctx, UnqualifiedType *t, Constness const_, Meta m=Meta())
Definition: type.h:427
Definition: statement.h:15
Definition: ast-context.h:54
static auto createDollarDollarDeclaration(ASTContext *ctx, QualifiedType *type)
Definition: keyword.h:61
Definition: hook.h:61
node::Properties properties() const final
Definition: hook.cc:11
std::string_view displayName() const override
Definition: hook.h:113
Definition: util.h:558