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