Spicy
context.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <map>
6 #include <memory>
7 #include <set>
8 #include <string>
9 #include <tuple>
10 #include <unordered_map>
11 #include <utility>
12 #include <vector>
13 
14 #include <hilti/rt/any.h>
15 #include <hilti/rt/filesystem.h>
16 
17 #include <hilti/ast/id.h>
18 #include <hilti/autogen/config.h>
19 #include <hilti/base/result.h>
20 #include <hilti/base/util.h>
21 
22 namespace hilti {
23 
24 class PluginRegistry;
25 
32 struct Options {
33  bool debug = false;
34  bool debug_trace =
35  false;
36  bool debug_flow = false;
38  bool track_location = true;
39  bool skip_validation = false;
41  bool optimize = false;
42  std::vector<hilti::rt::filesystem::path> library_paths;
43  std::string cxx_namespace_extern =
44  "hlt";
45  std::string cxx_namespace_intern = "__hlt";
46  std::vector<hilti::rt::filesystem::path>
59  template<typename T>
60  T getAuxOption(const std::string& key, T default_) const {
61  auto i = _aux_options.find(key);
62  if ( i != _aux_options.end() )
63  return hilti::rt::any_cast<T>(i->second);
64  else
65  return default_;
66  }
67 
76  template<typename T>
77  void setAuxOption(const std::string& key, T value) {
78  _aux_options[key] = value;
79  }
80 
88  Result<Nothing> parseDebugAddl(const std::string& flags);
89 
91  void print(std::ostream& out) const;
92 
93 private:
94  std::map<std::string, hilti::rt::any> _aux_options;
95 };
96 
97 namespace context {
98 
105 struct ModuleIndex {
106  ID id;
107  hilti::rt::filesystem::path path;
109  ModuleIndex() = default;
110  ModuleIndex(ID id, const hilti::rt::filesystem::path& path) : id(std::move(id)), path(util::normalizePath(path)) {}
111  bool operator<(const ModuleIndex& other) const { return id < other.id; }
112 };
113 
120 struct CachedModule {
123  bool requires_compilation =
124  false;
126  std::optional<std::set<ModuleIndex>> dependencies;
128  bool final = false;
130  CachedModule() = default;
131  CachedModule(ModuleIndex index, NodeRef node) : index(std::move(index)), node(std::move(node)) {}
132 };
133 
134 } // namespace context
135 
137 class Context {
138 public:
142  explicit Context(Options options);
143 
145  ~Context();
146 
148  const Options& options() const { return _options; }
149 
161  const context::CachedModule& registerModule(const context::ModuleIndex& idx, Node&& module,
162  bool requires_compilation);
163 
169  void updateModule(const context::CachedModule& module);
170 
178  std::optional<context::CachedModule> lookupModule(const ID& id);
179 
187  std::optional<context::CachedModule> lookupModule(const hilti::rt::filesystem::path& path);
188 
196  std::vector<context::CachedModule> lookupDependenciesForModule(const ID& id);
197 
198 private:
199  Options _options;
200 
201  std::vector<std::pair<std::unique_ptr<Node>, std::shared_ptr<context::CachedModule>>> _modules;
202  std::unordered_map<ID, std::shared_ptr<context::CachedModule>> _module_cache_by_id;
203  std::unordered_map<std::string, std::shared_ptr<context::CachedModule>> _module_cache_by_path;
204 };
205 
206 } // namespace hilti
bool track_location
Definition: context.h:38
Definition: context.h:120
Definition: context.h:32
ModuleIndex index
Definition: context.h:121
bool optimize
Definition: context.h:41
std::string cxx_namespace_intern
Definition: context.h:45
Definition: optional.h:79
bool debug_flow
Definition: context.h:36
void setAuxOption(const std::string &key, T value)
Definition: context.h:77
ID id
Definition: context.h:106
hilti::rt::filesystem::path path
Definition: context.h:107
Result< Nothing > parseDebugAddl(const std::string &flags)
Definition: context.cc:14
std::vector< hilti::rt::filesystem::path > cxx_include_paths
Definition: context.h:47
NodeRef node
Definition: context.h:122
std::string cxx_namespace_extern
Definition: context.h:43
std::optional< std::set< ModuleIndex > > dependencies
Definition: context.h:126
std::vector< hilti::rt::filesystem::path > library_paths
Definition: context.h:42
void print(std::ostream &out) const
Definition: context.cc:32
bool debug_trace
Definition: context.h:34
Definition: context.h:105
bool debug
Definition: context.h:33
Definition: node.h:97
T getAuxOption(const std::string &key, T default_) const
Definition: context.h:60
Definition: node_ref.h:44
const Options & options() const
Definition: context.h:148
Definition: elements.cc:17
bool skip_validation
Definition: context.h:39
Definition: id.h:18
Definition: result.h:67
Definition: context.h:137