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,
117  const hilti::declaration::Parameters& parameters,
118  hilti::statement::Block* body,
119  AttributeSet* attrs,
120  const Meta& m = Meta()) {
121  if ( ! attrs )
122  attrs = AttributeSet::create(ctx);
123 
124  auto* ftype = hilti::type::Function::create(ctx,
126  hilti::type::Void::create(ctx, m),
127  hilti::Constness::Const),
128  parameters,
129  hilti::type::function::Flavor::Hook,
130  hilti::type::function::CallingConvention::Standard,
131  m);
132  auto* func = hilti::Function::create(ctx, hilti::ID(), ftype, body, attrs, m);
133  return ctx->make<Hook>(ctx, {func, nullptr}, m);
134  }
135 
136 protected:
137  Hook(ASTContext* ctx, Nodes children, Meta m = Meta())
138  : Declaration(ctx,
139  NodeTags,
140  std::move(children),
141  hilti::ID(),
142  hilti::declaration::Linkage::Private,
143  std::move(m)) {}
144 
145  SPICY_NODE_1(declaration::Hook, Declaration, final);
146 
147 private:
148  hilti::ast::TypeIndex _unit_type_index;
149  hilti::ast::DeclarationIndex _unit_field_index;
150 };
151 
152 using Hooks = NodeVector<Hook>;
153 
154 } // namespace declaration
155 } // namespace spicy
Definition: ast-context.h:128
Definition: attribute.h:200
Definition: declaration.h:53
Definition: expression.h:15
Definition: id.h:15
Definition: meta.h:30
void setChild(ASTContext *ctx, size_t idx, Node *n)
Definition: node.h:631
const auto & children() const
Definition: node.h:382
Definition: type.h:365
static auto create(ASTContext *ctx, UnqualifiedType *t, Constness const_, Meta m=Meta())
Definition: type.h:436
Definition: ast-context.h:58
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:468