Spicy
keyword.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <ostream>
6 #include <utility>
7 
8 #include <hilti/ast/declaration.h>
9 #include <hilti/ast/declarations/expression.h>
10 #include <hilti/ast/expression.h>
11 #include <hilti/ast/types/auto.h>
12 #include <hilti/ast/types/unknown.h>
13 #include <hilti/base/util.h>
14 
15 namespace hilti::expression {
16 
17 namespace keyword {
18 // Type of a reserved keyword
19 enum class Kind {
20  Self,
21  DollarDollar,
22  Captures,
23  Scope
24 };
25 
26 namespace detail {
27 constexpr util::enum_::Value<Kind> kinds[] = {{Kind::Self, "self"},
28  {Kind::DollarDollar, "$$"},
29  {Kind::Captures, "$@"},
30  {Kind::Scope, "$scope"}};
31 } // namespace detail
32 
33 namespace kind {
34 constexpr auto from_string(const std::string_view& s) { return util::enum_::from_string<Kind>(s, detail::kinds); }
35 } // namespace kind
36 
37 constexpr auto to_string(Kind m) { return util::enum_::to_string(m, detail::kinds); }
38 
39 } // namespace keyword
40 
43 public:
44  Keyword(keyword::Kind kind, Meta m = Meta()) : NodeBase(nodes(type::auto_), std::move(m)), _kind(kind) {}
45  Keyword(keyword::Kind kind, Type t, Meta m = Meta()) : NodeBase(nodes(std::move(t)), std::move(m)), _kind(kind) {}
46 
47  keyword::Kind kind() const { return _kind; }
48 
49  bool operator==(const Keyword& other) const { return _kind == other._kind && type() == other.type(); }
50 
51  void setType(const Type& t) { children()[0] = t; }
52 
54  bool isLhs() const { return true; }
56  bool isTemporary() const { return false; }
58  const Type& type() const { return children()[0].as<Type>(); }
59 
61  auto isConstant() const { return false; }
63  auto isEqual(const Expression& other) const { return node::isEqual(this, other); }
64 
66  auto properties() const { return node::Properties{{"kind", to_string(_kind)}}; }
67 
70  Expression kw = hilti::expression::Keyword(hilti::expression::keyword::Kind::DollarDollar,
71  hilti::type::pruneWalk(std::move(t)));
72  return hilti::declaration::Expression("__dd", std::move(kw), hilti::declaration::Linkage::Private);
73  }
74 
75 private:
76  keyword::Kind _kind;
77 };
78 
79 inline std::ostream& operator<<(std::ostream& stream, const Keyword& keyword) {
80  switch ( keyword.kind() ) {
81  case keyword::Kind::Self: return stream << "<self>";
82  case keyword::Kind::DollarDollar: return stream << "<$$>";
83  case keyword::Kind::Captures: return stream << "<captures>";
84  case keyword::Kind::Scope: return stream << "<scope>";
85  }
86 
87  return stream;
88 }
89 
90 } // namespace hilti::expression
auto isConstant() const
Definition: keyword.h:61
Definition: declaration.h:54
Definition: keyword.h:42
bool isLhs() const
Definition: keyword.h:54
Definition: expression.h:18
static Declaration createDollarDollarDeclaration(Type t)
Definition: keyword.h:69
Definition: meta.h:19
auto isEqual(const Expression &other) const
Definition: keyword.h:63
Definition: type.h:160
Definition: expression.h:16
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:98
bool isTemporary() const
Definition: keyword.h:56
const Type & type() const
Definition: keyword.h:58
auto properties() const
Definition: keyword.h:66
Definition: node.h:360
Definition: expression.h:21