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  const auto& type() const { return child<Type>(1).as<Type>(); }
55  NodeRef typeRef() { return NodeRef(childs()[1]); }
56  const auto& ftype() const { return child<Type>(1).as<type::Function>(); }
57  auto body() const { return childs()[2].tryAs<Statement>(); }
58  auto attributes() const { return childs()[3].tryAs<AttributeSet>(); }
59  auto callingConvention() const { return _cc; }
60  bool isStatic() const { return AttributeSet::find(attributes(), "&static").has_value(); }
61 
62  bool operator==(const Function& other) const {
63  return id() == other.id() && type() == other.type() && body() == other.body() &&
64  attributes() == other.attributes() && callingConvention() == other.callingConvention();
65  }
66 
67  void setBody(Statement b) { childs()[2] = std::move(b); }
68  void setID(ID id) { childs()[0] = std::move(id); }
69  void setResultType(Type t) { childs()[1].as<type::Function>().setResultType(std::move(t)); }
70 
72  Node& _typeNode() { return childs()[1]; }
73 
75  auto properties() const { return node::Properties{{"cc", to_string(_cc)}}; }
76 
77 private:
78  function::CallingConvention _cc = function::CallingConvention::Standard;
79 };
80 
82 inline Node to_node(Function f) { return Node(std::move(f)); }
83 
84 } // namespace hilti
Definition: function.h:71
const Node none
Definition: node.cc:14
Definition: function.h:44
Definition: meta.h:18
Definition: attribute.h:145
Definition: type.h:159
Node & _typeNode()
Definition: function.h:72
hilti::optional_ref< const Attribute > find(std::string_view tag) const
Definition: attribute.h:171
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
Definition: node.h:113
Definition: node-ref.h:44
Definition: id.h:18
Definition: node.h:358
auto properties() const
Definition: function.h:75