Spicy
keyword.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 
7 #include <hilti/ast/expression.h>
8 #include <hilti/ast/types/unknown.h>
9 #include <hilti/base/util.h>
10 
11 namespace hilti {
12 namespace expression {
13 
14 namespace keyword {
15 // Type of a reserved keyword
16 enum class Kind {
17  Self,
18  DollarDollar,
19  Captures
20 };
21 
22 namespace detail {
23 constexpr util::enum_::Value<Kind> kinds[] = {{Kind::Self, "self"}, {Kind::DollarDollar, "$$"}, {Kind::Captures, "$@"}};
24 } // namespace detail
25 
26 namespace kind {
27 constexpr auto from_string(const std::string_view& s) { return util::enum_::from_string<Kind>(s, detail::kinds); }
28 } // namespace kind
29 
30 constexpr auto to_string(Kind m) { return util::enum_::to_string(m, detail::kinds); }
31 
32 } // namespace keyword
33 
36 public:
37  Keyword(keyword::Kind kind, Meta m = Meta()) : NodeBase({type::unknown}, std::move(m)), _kind(kind) {}
38  Keyword(keyword::Kind kind, Type t, Meta m = Meta()) : NodeBase({std::move(t)}, std::move(m)), _kind(kind) {}
39  Keyword(keyword::Kind kind, NodeRef d, Meta m = Meta())
40  : NodeBase({node::none}, std::move(m)), _kind(kind), _decl(d) {}
41 
42  keyword::Kind kind() const { return _kind; }
43  bool hasType() const { return _decl.has_value() || ! childs()[0].isA<type::Unknown>(); }
44 
46  bool isSet() const { return (! childs()[0].isA<type::Unknown>()) || _decl.has_value(); }
47 
48  bool operator==(const Keyword& other) const { return _kind == other._kind && type() == other.type(); }
49 
51  bool isLhs() const { return true; }
53  bool isTemporary() const { return false; }
55  Type type() const {
56  auto t = type::effectiveType(_decl ? (**_decl).as<declaration::Type>().type() : childs()[0].as<Type>());
57 
58  if ( _kind == keyword::Kind::Self )
59  t = type::removeFlags(t, type::Flag::Constant);
60 
61  return t;
62  }
63 
65  auto isConstant() const { return false; }
66 
68  auto isEqual(const Expression& other) const { return node::isEqual(this, other); }
69 
71  auto properties() const { return node::Properties{{"kind", to_string(_kind)}}; }
72 
80  static Expression setType(const Keyword& e, const Type& t) {
81  auto x = Expression(e)._clone().as<Keyword>();
82  x.childs()[0] = t;
83  x._decl = {};
84  return x;
85  }
86 
87 private:
88  keyword::Kind _kind;
89  std::optional<NodeRef> _decl;
90 };
91 
92 } // namespace expression
93 } // namespace hilti
auto isConstant() const
Definition: keyword.h:65
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
Definition: type.h:16
Type type() const
Definition: keyword.h:55
const Node none
Definition: node.cc:12
Definition: keyword.h:35
bool isLhs() const
Definition: keyword.h:51
Definition: expression.h:16
Definition: meta.h:18
static Expression setType(const Keyword &e, const Type &t)
Definition: keyword.h:80
auto isEqual(const Expression &other) const
Definition: keyword.h:68
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
bool isTemporary() const
Definition: keyword.h:53
Definition: node_ref.h:44
Definition: unknown.h:13
auto properties() const
Definition: keyword.h:71
Definition: node.h:318
bool isSet() const
Definition: keyword.h:46