Spicy
function.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <string_view>
7 #include <utility>
8 
9 #include <hilti/ast/attribute.h>
10 #include <hilti/ast/forward.h>
11 #include <hilti/ast/node.h>
12 #include <hilti/ast/statement.h>
13 #include <hilti/ast/type.h>
14 #include <hilti/ast/types/function.h>
15 
16 namespace hilti {
17 
18 namespace function {
19 
21 enum class CallingConvention {
22  Extern,
23  ExternNoSuspend,
24  Standard
25 };
26 
27 namespace detail {
28 constexpr util::enum_::Value<CallingConvention> Conventions[] = {
29  {CallingConvention::Extern, "extern"},
30  {CallingConvention::ExternNoSuspend, "extern-no-suspend"},
31  {CallingConvention::Standard, "<standard>"},
32 };
33 } // namespace detail
34 
35 constexpr auto to_string(CallingConvention cc) { return util::enum_::to_string(cc, detail::Conventions); }
36 
37 namespace calling_convention {
38 constexpr auto from_string(std::string_view s) {
39  return util::enum_::from_string<CallingConvention>(s, detail::Conventions);
40 }
41 } // namespace calling_convention
42 
43 } // namespace function
45 class Function : public Node {
46 public:
47  const auto& id() const { return _id; }
48  auto type() const { return child<QualifiedType>(0); }
49  auto ftype() const { return child<QualifiedType>(0)->type()->as<type::Function>(); }
50  auto body() const { return child<Statement>(1); }
51  auto attributes() const { return child<AttributeSet>(2); }
52  auto callingConvention() const { return _cc; }
53  auto isStatic() const { return attributes()->find(hilti::attribute::kind::Static) != nullptr; }
54 
55  void setBody(ASTContext* ctx, Statement* b) { setChild(ctx, 1, b); }
56  void setID(ID id) { _id = std::move(id); }
57  void setResultType(ASTContext* ctx, QualifiedType* t) { ftype()->setResultType(ctx, t); }
58 
59  node::Properties properties() const override {
60  auto p = node::Properties{{"id", _id}, {"cc", to_string(_cc)}};
61  return Node::properties() + std::move(p);
62  }
63 
64  static auto create(ASTContext* ctx, const ID& id, type::Function* ftype, Statement* body,
65  function::CallingConvention cc = function::CallingConvention::Standard,
66  AttributeSet* attrs = nullptr, const Meta& meta = {}) {
67  if ( ! attrs )
68  attrs = AttributeSet::create(ctx);
69 
70  return ctx->make<Function>(ctx, {QualifiedType::create(ctx, ftype, Constness::Const, meta), body, attrs}, id,
71  cc, meta);
72  }
73 
74 protected:
75  Function(ASTContext* ctx, Nodes children, ID id, function::CallingConvention cc, Meta meta = {})
76  : Node(ctx, NodeTags, std::move(children), std::move(meta)), _id(std::move(id)), _cc(cc) {}
77 
78  std::string _dump() const override;
79 
80  HILTI_NODE_0(Function, final);
81 
82 private:
83  ID _id;
84  function::CallingConvention _cc;
85 };
86 
87 } // namespace hilti
Definition: ast-context.h:121
T * make(Args &&... args)
Definition: ast-context.h:366
Definition: attribute.h:198
Definition: function.h:45
node::Properties properties() const override
Definition: function.h:59
std::string _dump() const override
Definition: function.cc:8
Definition: id.h:15
Definition: meta.h:30
Definition: node.h:239
void setChild(ASTContext *ctx, size_t idx, Node *n)
Definition: node.h:577
const auto & children() const
Definition: node.h:363
const auto & meta() const
Definition: node.h:305
Node(ASTContext *ctx, node::Tags node_tags, Nodes children, Meta meta)
Definition: node.h:897
virtual node::Properties properties() const
Definition: node.h:866
Definition: type.h:356
static auto create(ASTContext *ctx, UnqualifiedType *t, Constness const_, Meta m=Meta())
Definition: type.h:421
Definition: statement.h:15
Definition: function.h:49