Spicy
declaration.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <utility>
7 
8 #include <hilti/ast/ast-context.h>
9 #include <hilti/ast/forward.h>
10 #include <hilti/ast/id.h>
11 #include <hilti/ast/node.h>
12 
13 namespace hilti {
14 
15 namespace declaration {
16 
18 enum class Linkage {
19  Export,
20  Init,
21  PreInit,
22  Private,
23  Public,
25  Struct,
26 };
27 
28 namespace detail {
29 constexpr util::enum_::Value<Linkage> Linkages[] = {
30  {.value = Linkage::Export, .name = "exported"},
31  {.value = Linkage::Init, .name = "init"},
32  {.value = Linkage::PreInit, .name = "preinit"},
33  {.value = Linkage::Private, .name = "private"},
34  {.value = Linkage::Public, .name = "public"},
35  {.value = Linkage::Struct, .name = "struct"},
36 };
37 } // namespace detail
38 
40 constexpr auto to_string(Linkage f) { return util::enum_::to_string(f, detail::Linkages); }
41 
42 namespace linkage {
48 constexpr auto from_string(std::string_view s) { return util::enum_::from_string<Linkage>(s, detail::Linkages); }
49 } // namespace linkage
50 } // namespace declaration
51 
53 class Declaration : public Node, public node::WithDocString {
54 public:
55  ~Declaration() override;
56 
58  const auto& id() const { return _id; }
59 
61  auto linkage() const { return _linkage; }
62 
64  auto isPublic() const {
65  return _linkage == declaration::Linkage::Public || _linkage == declaration::Linkage::Export;
66  }
67 
73  const auto& fullyQualifiedID() const { return _fqid; }
74 
82  const auto& canonicalID() const { return _canonical_id; }
83 
88  auto declarationIndex() const { return _declaration_index; }
89 
94  void setID(const ID& id) {
95  _id = id;
96  _fqid = {}, _canonical_id = {};
97  }
98 
100  void setLinkage(declaration::Linkage linkage) { _linkage = linkage; }
101 
108  void setFullyQualifiedID(ID id) { _fqid = std::move(id); }
109 
116  void setCanonicalID(ID id) { _canonical_id = std::move(id); }
117 
123  virtual std::string_view displayName() const = 0;
124 
126  node::Properties properties() const override {
127  auto p = node::Properties{{"id", _id},
128  {"linkage", declaration::to_string(_linkage)},
129  {"declaration", to_string(_declaration_index)},
130  {"fqid", _fqid},
131  {"canonical-id", _canonical_id}};
132 
133  return Node::properties() + std::move(p);
134  }
135 
136  Declaration(const Declaration& other) : Node(other), node::WithDocString(other) {
137  _id = other._id;
138  _linkage = other._linkage;
139  // Do not copy computed state, we'll want to recompute that eventually.
140  }
141 
142  Declaration(Declaration&& other) = default;
143 
144  Declaration& operator=(const Declaration& other) = delete;
145  Declaration& operator=(Declaration&& other) = delete;
146 
147 protected:
148  friend class ASTContext;
149 
150  Declaration(ASTContext* ctx,
151  node::Tags node_tags,
152  Nodes children,
153  ID id,
154  declaration::Linkage linkage,
155  Meta meta = {})
156  : Node(ctx, node_tags, std::move(children), std::move(meta)), _id(std::move(id)), _linkage(linkage) {}
157 
158  // For the AST context to set the declaration index.
159  void setDeclarationIndex(ast::DeclarationIndex index) {
160  assert(index);
161  _declaration_index = index;
162  }
163 
164  std::string _dump() const override;
165 
166  HILTI_NODE_0(Declaration, override);
167 
168 private:
169  ID _id;
170  declaration::Linkage _linkage;
171 
172  ast::DeclarationIndex _declaration_index; // index registered by the context
173  ID _fqid; // computed during AST processing
174  ID _canonical_id; // computed during AST processing
175 };
176 
177 } // namespace hilti
Definition: declaration.h:53
void setLinkage(declaration::Linkage linkage)
Definition: declaration.h:100
auto isPublic() const
Definition: declaration.h:64
const auto & canonicalID() const
Definition: declaration.h:82
void setCanonicalID(ID id)
Definition: declaration.h:116
auto declarationIndex() const
Definition: declaration.h:88
node::Properties properties() const override
Definition: declaration.h:126
void setID(const ID &id)
Definition: declaration.h:94
void setFullyQualifiedID(ID id)
Definition: declaration.h:108
const auto & fullyQualifiedID() const
Definition: declaration.h:73
const auto & id() const
Definition: declaration.h:58
std::string _dump() const override
Definition: declaration.cc:10
virtual std::string_view displayName() const =0
auto linkage() const
Definition: declaration.h:61
Definition: id.h:15
Definition: node.h:243
const auto & children() const
Definition: node.h:382
const auto & meta() const
Definition: node.h:324
Node(ASTContext *ctx, node::Tags node_tags, Nodes children, Meta meta)
Definition: node.h:984
virtual node::Properties properties() const
Definition: node.h:953
Definition: node.h:1170