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/attribute.h>
9 #include <hilti/ast/declaration.h>
10 #include <hilti/ast/expression.h>
11 #include <hilti/ast/id.h>
12 #include <hilti/ast/type.h>
13 #include <hilti/ast/types/auto.h>
14 #include <hilti/ast/types/unknown.h>
15 
16 namespace hilti {
17 namespace declaration {
18 
19 namespace parameter {
20 
22 enum class Kind {
23  Unknown,
24  Copy,
25  In,
26  InOut
27 };
28 
29 namespace detail {
30 constexpr util::enum_::Value<Kind> kinds[] = {
31  {Kind::Unknown, "unknown"},
32  {Kind::Copy, "copy"},
33  {Kind::In, "in"},
34  {Kind::InOut, "inout"},
35 };
36 } // namespace detail
37 
38 constexpr auto to_string(Kind k) { return util::enum_::to_string(k, detail::kinds); }
39 
40 namespace kind {
41 constexpr auto from_string(const std::string_view& s) { return util::enum_::from_string<Kind>(s, detail::kinds); }
42 } // namespace kind
43 
44 } // namespace parameter
45 
47 class Parameter : public DeclarationBase {
48 public:
49  Parameter(ID id, hilti::Type type, parameter::Kind kind, std::optional<hilti::Expression> default_,
50  std::optional<AttributeSet> attrs, Meta m = Meta())
51  : DeclarationBase(nodes(std::move(id), type::nonConstant(std::move(type)), std::move(default_),
52  std::move(attrs)),
53  std::move(m)),
54  _kind(kind) {}
55 
56  Parameter(ID id, hilti::Type type, parameter::Kind kind, std::optional<hilti::Expression> default_,
57  bool is_type_param, std::optional<AttributeSet> attrs, Meta m = Meta())
58  : DeclarationBase(nodes(std::move(id), type::nonConstant(std::move(type)), std::move(default_),
59  std::move(attrs)),
60  std::move(m)),
61  _kind(kind),
62  _is_type_param(is_type_param) {}
63 
64  Parameter() : DeclarationBase({node::none, type::unknown, node::none, node::none}, Meta()) {}
65 
66  auto attributes() const { return childs()[3].tryAs<AttributeSet>(); }
67  auto default_() const { return childs()[2].tryAs<hilti::Expression>(); }
68  auto kind() const { return _kind; }
69  const auto& type() const { return child<hilti::Type>(1); }
70  auto isTypeParameter() const { return _is_type_param; }
71  auto isResolved(type::ResolvedState* rstate) const { return type::detail::isResolved(type(), rstate); }
72 
73  void setDefault(hilti::Expression e) { childs()[2] = std::move(e); }
74  void setIsTypeParameter() { _is_type_param = true; }
75  void setType(hilti::Type t) { childs()[1] = std::move(t); }
76 
77  bool operator==(const Parameter& other) const {
78  return id() == other.id() && type() == other.type() && kind() == other.kind() && default_() == other.default_();
79  }
80 
82  bool isConstant() const { return _kind == parameter::Kind::In; }
84  const ID& id() const { return child<ID>(0); }
86  Linkage linkage() const { return Linkage::Private; }
88  std::string displayName() const { return "parameter"; };
90  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
91 
93  auto properties() const { return node::Properties{{"kind", to_string(_kind)}, {"is_type_param", _is_type_param}}; }
94 
95 private:
96  parameter::Kind _kind = parameter::Kind::Unknown;
97  bool _is_type_param = false;
98 };
99 
101 inline bool areEquivalent(const Parameter& p1, const Parameter& p2) {
102  if ( p1.kind() != p2.kind() || p1.default_() != p2.default_() )
103  return false;
104 
105  auto auto1 = p1.type().tryAs<type::Auto>();
106  auto auto2 = p2.type().tryAs<type::Auto>();
107 
108  if ( auto1 || auto2 )
109  return true;
110 
111  return p1.type() == p2.type();
112 }
113 
114 } // namespace declaration
115 } // namespace hilti
Definition: declaration.h:53
const Node none
Definition: node.cc:14
bool isConstant() const
Definition: parameter.h:82
Definition: meta.h:18
Definition: attribute.h:145
Definition: type.h:159
Definition: declaration.h:87
Definition: parameter.h:47
auto isEqual(const Declaration &other) const
Definition: parameter.h:90
const ID & id() const
Definition: parameter.h:84
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
auto properties() const
Definition: parameter.h:93
Definition: auto.h:13
Linkage linkage() const
Definition: parameter.h:86
Definition: id.h:18
std::string displayName() const
Definition: parameter.h:88