Spicy
hook.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/function.h>
10 
11 #include <spicy/ast/aliases.h>
12 #include <spicy/ast/engine.h>
13 
14 namespace spicy {
15 
17 class Hook : public Function {
18 public:
19  Hook(const std::vector<type::function::Parameter>& params, std::optional<Statement> body, Engine engine,
20  std::optional<AttributeSet> attrs = {}, Meta m = Meta())
21  : Function(ID("<hook>"),
22  type::Function(type::function::Result(type::Void(), m), params, type::function::Flavor::Hook, m),
23  std::move(body), hilti::function::CallingConvention::Standard, std::move(attrs), std::move(m)),
24  _engine(engine) {}
25 
26  Hook() = default;
27 
28  Engine engine() const { return _engine; }
29  bool isForEach() const { return AttributeSet::find(attributes(), "foreach").has_value(); }
30  bool isDebug() const { return AttributeSet::find(attributes(), "%debug").has_value(); }
31 
32  std::optional<Expression> priority() const {
33  if ( auto p = AttributeSet::find(attributes(), "priority") )
34  return *p->valueAs<Expression>();
35 
36  return {};
37  }
38 
39  bool operator==(const Hook& other) const {
40  return static_cast<Function>(*this) == static_cast<Function>(other) && // NOLINT (cppcoreguidelines-slicing)
41  _engine == other._engine;
42  }
43 
44  auto properties() const { return Function::properties() + node::Properties{{"engine", to_string(_engine)}}; }
45 
46 private:
47  Engine _engine = {};
48 };
49 
51 inline Node to_node(Hook f) { return Node(std::move(f)); }
52 
53 } // namespace spicy
Definition: function.h:69
Definition: void.h:13
Definition: function.h:44
Definition: hook.h:17
Definition: meta.h:18
std::optional< Attribute > find(std::string_view tag) const
Definition: attribute.h:190
Definition: node.h:97
Definition: id.h:18
auto properties() const
Definition: function.h:66