Spicy
function.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <set>
6 #include <utility>
7 
8 #include <hilti/ast/attribute.h>
9 #include <hilti/ast/node.h>
10 #include <hilti/ast/statement.h>
11 #include <hilti/ast/type.h>
12 #include <hilti/ast/types/function.h>
13 
14 namespace hilti {
15 
16 namespace function {
17 
19 enum class CallingConvention {
20  Extern,
21  ExternNoSuspend,
22  Standard
23 };
24 
25 namespace detail {
26 constexpr util::enum_::Value<CallingConvention> conventions[] = {
27  {CallingConvention::Extern, "extern"},
28  {CallingConvention::ExternNoSuspend, "extern-no-suspend"},
29  {CallingConvention::Standard, "<standard>"},
30 };
31 } // namespace detail
32 
33 constexpr auto to_string(CallingConvention cc) { return util::enum_::to_string(cc, detail::conventions); }
34 
35 namespace calling_convention {
36 constexpr inline auto from_string(const std::string_view& s) {
37  return util::enum_::from_string<CallingConvention>(s, detail::conventions);
38 }
39 } // namespace calling_convention
40 
41 } // namespace function
42 
44 class Function : public NodeBase {
45 public:
46  Function(ID id, Type type, std::optional<Statement> body,
47  function::CallingConvention cc = function::CallingConvention::Standard,
48  std::optional<AttributeSet> attrs = {}, Meta m = Meta())
49  : NodeBase(nodes(std::move(id), std::move(type), std::move(body), std::move(attrs)), std::move(m)), _cc(cc) {}
50 
52 
53  const auto& id() const { return child<ID>(0); }
54  auto type() const { return type::effectiveType(child<Type>(1)).as<type::Function>(); }
55  auto body() const { return childs()[2].tryReferenceAs<Statement>(); }
56  auto attributes() const { return childs()[3].tryReferenceAs<AttributeSet>(); }
57  auto callingConvention() const { return _cc; }
58  bool isStatic() const { return AttributeSet::find(attributes(), "&static").has_value(); }
59 
60  bool operator==(const Function& other) const {
61  return id() == other.id() && type() == other.type() && body() == other.body() &&
62  attributes() == other.attributes() && callingConvention() == other.callingConvention();
63  }
64 
66  auto properties() const { return node::Properties{{"cc", to_string(_cc)}}; }
67 
75  static Function setBody(const Function& d, const Statement& b) {
76  auto x = Function(d);
77  x.childs()[2] = b;
78  return x;
79  }
80 
81 private:
82  function::CallingConvention _cc = function::CallingConvention::Standard;
83 };
84 
86 inline Node to_node(Function f) { return Node(std::move(f)); }
87 
88 } // namespace hilti
Definition: function.h:69
static Function setBody(const Function &d, const Statement &b)
Definition: function.h:75
const Node none
Definition: node.cc:12
Definition: function.h:44
Definition: meta.h:18
Definition: attribute.h:159
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
std::optional< Attribute > find(std::string_view tag) const
Definition: attribute.h:190
Definition: node.h:97
Definition: id.h:18
Definition: node.h:318
auto properties() const
Definition: function.h:66