Spicy
module.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 #include <vector>
7 
8 #include <hilti/ast/declaration.h>
9 #include <hilti/ast/declarations/property.h>
10 #include <hilti/ast/id.h>
11 #include <hilti/ast/node.h>
12 #include <hilti/ast/statement.h>
13 #include <hilti/ast/statements/block.h>
14 #include <hilti/ast/statements/expression.h>
15 #include <hilti/base/util.h>
16 
17 namespace hilti {
18 
20 class Module : public NodeBase {
21 public:
22  Module(ID id = {}, Meta m = Meta()) : NodeBase({std::move(id), statement::Block({}, m)}, std::move(m)) {}
23 
24  Module(ID id, std::vector<Declaration> decls, Meta m = Meta())
25  : NodeBase(nodes(std::move(id), statement::Block({}, m), std::move(decls)), std::move(m)) {}
26 
27  Module(ID id, std::vector<Declaration> decls, std::vector<Statement> stmts, const Meta& m = Meta())
28  : NodeBase(nodes(std::move(id), statement::Block(std::move(stmts), m), std::move(decls)), m) {}
29 
30  const auto& id() const { return child<ID>(0); }
31  const auto& statements() const { return child<statement::Block>(1); }
32  const auto& declarations() const {
33  if ( _cache.declarations.empty() )
34  _cache.declarations = childsOfType<Declaration>();
35 
36  return _cache.declarations;
37  }
38 
39  const auto& preserved() const { return _preserved; }
40  auto& preserved() { return _preserved; }
41 
42  bool isEmpty() const {
43  // We always have an ID and a block as childs.
44  return childs().size() <= 2 && statements().statements().empty();
45  }
46 
51  void clear();
52 
61 
68  std::vector<declaration::Property> moduleProperties(const ID& id) const;
69 
78  void add(Declaration n) { addChild(std::move(n)); }
79 
90  void add(Statement s) { childs()[1].as<statement::Block>()._add(std::move(s)); }
91 
102  void add(Expression e) { add(statement::Expression(std::move(e))); }
103 
113  NodeRef preserve(Node n);
114 
116  auto properties() const { return node::Properties{}; }
117 
118  void clearCache() { _cache.declarations.clear(); }
119 
120 private:
121  std::vector<Node> _preserved;
122 
123  mutable struct { std::vector<Declaration> declarations; } _cache;
124 };
125 
127 inline Node to_node(Module i) { return Node(std::move(i)); }
128 
129 } // namespace hilti
auto & childs() const
Definition: node.h:445
Definition: expression.h:14
void add(Expression e)
Definition: module.h:102
std::vector< declaration::Property > moduleProperties(const ID &id) const
Definition: module.cc:37
void addChild(Node n)
Definition: node.h:434
void clear()
Definition: module.cc:9
auto properties() const
Definition: module.h:116
Definition: meta.h:18
void add(Statement s)
Definition: module.h:90
void add(Declaration n)
Definition: module.h:78
Definition: block.h:15
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
Definition: module.h:20
Definition: node.h:97
Definition: node_ref.h:44
NodeRef preserve(Node n)
Definition: module.cc:22
Result< declaration::Property > moduleProperty(const ID &id) const
Definition: module.cc:28
Definition: id.h:18
Definition: result.h:67
Definition: node.h:318