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  Init,
20  PreInit,
21  Struct,
22  Private,
23  Public,
24 };
25 
26 namespace detail {
27 constexpr util::enum_::Value<Linkage> Linkages[] = {{.value = Linkage::Struct, .name = "struct"},
28  {.value = Linkage::Public, .name = "public"},
29  {.value = Linkage::Private, .name = "private"},
30  {.value = Linkage::Init, .name = "init"},
31  {.value = Linkage::PreInit, .name = "preinit"}};
32 } // namespace detail
33 
35 constexpr auto to_string(Linkage f) { return util::enum_::to_string(f, detail::Linkages); }
36 
37 namespace linkage {
43 constexpr auto from_string(std::string_view s) { return util::enum_::from_string<Linkage>(s, detail::Linkages); }
44 } // namespace linkage
45 } // namespace declaration
46 
48 class Declaration : public Node, public node::WithDocString {
49 public:
50  ~Declaration() override;
51 
53  const auto& id() const { return _id; }
54 
56  auto linkage() const { return _linkage; }
57 
63  const auto& fullyQualifiedID() const { return _fqid; }
64 
72  const auto& canonicalID() const { return _canonical_id; }
73 
78  auto declarationIndex() const { return _declaration_index; }
79 
84  void setID(const ID& id) {
85  _id = id;
86  _fqid = {}, _canonical_id = {};
87  }
88 
90  void setLinkage(declaration::Linkage linkage) { _linkage = linkage; }
91 
98  void setFullyQualifiedID(ID id) { _fqid = std::move(id); }
99 
106  void setCanonicalID(ID id) { _canonical_id = std::move(id); }
107 
113  virtual std::string_view displayName() const = 0;
114 
116  node::Properties properties() const override {
117  auto p = node::Properties{{"id", _id},
118  {"linkage", declaration::to_string(_linkage)},
119  {"declaration", to_string(_declaration_index)},
120  {"fqid", _fqid},
121  {"canonical-id", _canonical_id}};
122 
123  return Node::properties() + std::move(p);
124  }
125 
126  Declaration(const Declaration& other) : Node(other), node::WithDocString(other) {
127  _id = other._id;
128  _linkage = other._linkage;
129  // Do not copy computed state, we'll want to recompute that eventually.
130  }
131 
132  Declaration(Declaration&& other) = default;
133 
134  Declaration& operator=(const Declaration& other) = delete;
135  Declaration& operator=(Declaration&& other) = delete;
136 
137 protected:
138  friend class ASTContext;
139 
140  Declaration(ASTContext* ctx, node::Tags node_tags, Nodes children, ID id, declaration::Linkage linkage,
141  Meta meta = {})
142  : Node(ctx, node_tags, std::move(children), std::move(meta)), _id(std::move(id)), _linkage(linkage) {}
143 
144  // For the AST context to set the declaration index.
145  void setDeclarationIndex(ast::DeclarationIndex index) {
146  assert(index);
147  _declaration_index = index;
148  }
149 
150  std::string _dump() const override;
151 
152  HILTI_NODE_0(Declaration, override);
153 
154 private:
155  ID _id;
156  declaration::Linkage _linkage;
157 
158  ast::DeclarationIndex _declaration_index; // index registered by the context
159  ID _fqid; // computed during AST processing
160  ID _canonical_id; // computed during AST processing
161 };
162 
163 } // namespace hilti
Definition: declaration.h:48
void setLinkage(declaration::Linkage linkage)
Definition: declaration.h:90
const auto & canonicalID() const
Definition: declaration.h:72
void setCanonicalID(ID id)
Definition: declaration.h:106
auto declarationIndex() const
Definition: declaration.h:78
node::Properties properties() const override
Definition: declaration.h:116
void setID(const ID &id)
Definition: declaration.h:84
void setFullyQualifiedID(ID id)
Definition: declaration.h:98
const auto & fullyQualifiedID() const
Definition: declaration.h:63
const auto & id() const
Definition: declaration.h:53
std::string _dump() const override
Definition: declaration.cc:10
virtual std::string_view displayName() const =0
auto linkage() const
Definition: declaration.h:56
Definition: id.h:15
Definition: node.h:240
const auto & children() const
Definition: node.h:364
const auto & meta() const
Definition: node.h:306
Node(ASTContext *ctx, node::Tags node_tags, Nodes children, Meta meta)
Definition: node.h:922
virtual node::Properties properties() const
Definition: node.h:891
Definition: node.h:1101