Spicy
parameter.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <utility>
7 
8 #include <hilti/ast/declaration.h>
9 #include <hilti/ast/expression.h>
10 #include <hilti/ast/id.h>
11 #include <hilti/ast/type.h>
12 #include <hilti/ast/types/auto.h>
13 
14 namespace hilti {
15 namespace declaration {
16 
17 namespace parameter {
18 
20 enum class Kind {
21  Unknown,
22  Copy,
23  In,
24  InOut
25 };
26 
27 namespace detail {
28 constexpr util::enum_::Value<Kind> kinds[] = {
29  {Kind::Unknown, "unknown"},
30  {Kind::Copy, "copy"},
31  {Kind::In, "in"},
32  {Kind::InOut, "inout"},
33 };
34 } // namespace detail
35 
36 constexpr auto to_string(Kind k) { return util::enum_::to_string(k, detail::kinds); }
37 
38 namespace kind {
39 constexpr auto from_string(const std::string_view& s) { return util::enum_::from_string<Kind>(s, detail::kinds); }
40 } // namespace kind
41 
42 } // namespace parameter
43 
46 public:
47  Parameter(ID id, hilti::Type type, parameter::Kind kind, std::optional<hilti::Expression> default_, Meta m = Meta())
48  : NodeBase(nodes(std::move(id), std::move(type), std::move(default_)), std::move(m)), _kind(kind) {}
49 
50  Parameter(ID id, hilti::Type type, parameter::Kind kind, std::optional<hilti::Expression> default_,
51  bool is_struct_param, Meta m = Meta())
52  : NodeBase(nodes(std::move(id), std::move(type), std::move(default_)), std::move(m)),
53  _kind(kind),
54  _is_struct_param(is_struct_param) {}
55 
56  Parameter() : NodeBase({node::none, node::none, node::none}, Meta()) {}
57 
58  auto type() const { return type::effectiveType(child<hilti::Type>(1)); }
59 
60  auto default_() const { return childs()[2].tryReferenceAs<hilti::Expression>(); }
61  auto kind() const { return _kind; }
62  auto isStructParameter() const { return _is_struct_param; }
63 
64  bool operator==(const Parameter& other) const {
65  return id() == other.id() && type() == other.type() && kind() == other.kind() && default_() == other.default_();
66  }
67 
69  bool isConstant() const { return _kind == parameter::Kind::In; }
71  const ID& id() const { return child<ID>(0); }
73  Linkage linkage() const { return Linkage::Private; }
75  std::string displayName() const { return "parameter"; };
77  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
78 
80  auto properties() const {
81  return node::Properties{{"kind", to_string(_kind)}, {"is_struct_param", _is_struct_param}};
82  }
83 
91  static Declaration setType(const Parameter& d, std::optional<hilti::Type> t) {
92  auto x = Declaration(d)._clone().as<Parameter>();
93  if ( t )
94  x.childs()[1] = *t;
95  else
96  x.childs()[1] = node::none;
97 
98  return x;
99  }
100 
108  static Declaration setDefault(const Parameter& d, const hilti::Expression& e) {
109  auto x = Declaration(d)._clone().as<Parameter>();
110  x.childs()[2] = e;
111  return x;
112  }
113 
119  static Declaration setIsStructParameter(const Parameter& d) {
120  auto x = Declaration(d)._clone().as<Parameter>();
121  x._is_struct_param = true;
122  return x;
123  }
124 
125 private:
126  parameter::Kind _kind = parameter::Kind::Unknown;
127  bool _is_struct_param = false;
128 };
129 
131 inline bool areEquivalent(const Parameter& p1, const Parameter& p2) {
132  if ( p1.kind() != p2.kind() || p1.default_() != p2.default_() )
133  return false;
134 
135  auto auto1 = p1.type().tryAs<type::Auto>();
136  auto auto2 = p2.type().tryAs<type::Auto>();
137 
138  if ( auto1 || auto2 )
139  return true;
140 
141  return p1.type() == p2.type();
142 }
143 
144 } // namespace declaration
145 } // namespace hilti
static Declaration setDefault(const Parameter &d, const hilti::Expression &e)
Definition: parameter.h:108
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
static Declaration setIsStructParameter(const Parameter &d)
Definition: parameter.h:119
const Node none
Definition: node.cc:12
bool isConstant() const
Definition: parameter.h:69
Definition: meta.h:18
Definition: parameter.h:45
auto isEqual(const Declaration &other) const
Definition: parameter.h:77
const ID & id() const
Definition: parameter.h:71
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
auto properties() const
Definition: parameter.h:80
static Declaration setType(const Parameter &d, std::optional< hilti::Type > t)
Definition: parameter.h:91
Definition: auto.h:16
Definition: declaration.h:15
Linkage linkage() const
Definition: parameter.h:73
Definition: id.h:18
Definition: node.h:318
std::string displayName() const
Definition: parameter.h:75