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 <string>
8 #include <tuple>
9 #include <unordered_map>
10 #include <utility>
11 #include <vector>
12 
13 #include <hilti/rt/any.h>
14 #include <hilti/rt/filesystem.h>
15 
16 #include <hilti/ast/id.h>
17 #include <hilti/autogen/config.h>
18 #include <hilti/base/logger.h>
19 #include <hilti/base/result.h>
20 #include <hilti/base/util.h>
21 
22 namespace hilti {
23 
24 class PluginRegistry;
25 class Unit;
26 
33 struct Options {
34  bool debug = false;
35  bool debug_trace =
36  false;
37  bool debug_flow = false;
39  bool track_location = true;
40  bool skip_validation = false;
42  bool optimize = false;
43  std::vector<hilti::rt::filesystem::path> library_paths;
44  std::string cxx_namespace_extern =
45  "hlt";
46  std::string cxx_namespace_intern = "__hlt";
47  std::vector<hilti::rt::filesystem::path>
49  bool keep_tmps = false;
61  template<typename T>
62  T getAuxOption(const std::string& key, T default_) const {
63  auto i = _aux_options.find(key);
64  if ( i != _aux_options.end() )
65  return hilti::rt::any_cast<T>(i->second);
66  else
67  return default_;
68  }
69 
78  template<typename T>
79  void setAuxOption(const std::string& key, T value) {
80  _aux_options[key] = value;
81  }
82 
90  Result<Nothing> parseDebugAddl(const std::string& flags);
91 
93  void print(std::ostream& out) const;
94 
95 private:
96  std::map<std::string, hilti::rt::any> _aux_options;
97 };
98 
99 namespace context {
100 
107 struct CacheIndex {
108  ID id;
109  hilti::rt::filesystem::path path;
111  CacheIndex() = default;
112  CacheIndex(ID id, const hilti::rt::filesystem::path& path) : id(std::move(id)), path(util::normalizePath(path)) {}
113  bool operator<(const CacheIndex& other) const { return id < other.id; }
114 };
115 
122 struct CacheEntry {
123  std::shared_ptr<Unit> unit;
125  CacheEntry() = default;
126  CacheEntry(std::shared_ptr<Unit> unit) : unit(std::move(unit)) {}
127 };
128 
129 } // namespace context
130 
132 class Context {
133 public:
137  explicit Context(Options options);
138 
140  ~Context();
141 
143  const Options& options() const { return _options; }
144 
153  void cacheUnit(const std::shared_ptr<Unit>& unit);
154 
164  std::optional<context::CacheEntry> lookupUnit(const ID& id, const hilti::rt::filesystem::path& extension);
165 
173  std::optional<context::CacheEntry> lookupUnit(const hilti::rt::filesystem::path& path,
174  std::optional<hilti::rt::filesystem::path> ast_extension = {});
175 
180  std::vector<std::weak_ptr<Unit>> lookupDependenciesForUnit(const ID& id,
181  const hilti::rt::filesystem::path& extension);
182 
188  void dumpUnitCache(const hilti::logging::DebugStream& stream);
189 
190 private:
191  Options _options;
192 
193  std::unordered_map<ID, std::shared_ptr<context::CacheEntry>> _unit_cache_by_id;
194  std::unordered_map<std::string, std::shared_ptr<context::CacheEntry>> _unit_cache_by_path;
195 };
196 
197 } // namespace hilti
Definition: context.h:107
bool track_location
Definition: context.h:39
bool keep_tmps
Definition: context.h:49
Definition: context.h:33
ID id
Definition: context.h:108
bool optimize
Definition: context.h:42
std::string cxx_namespace_intern
Definition: context.h:46
Definition: optional.h:79
bool debug_flow
Definition: context.h:37
void setAuxOption(const std::string &key, T value)
Definition: context.h:79
Result< Nothing > parseDebugAddl(const std::string &flags)
Definition: context.cc:16
std::vector< hilti::rt::filesystem::path > cxx_include_paths
Definition: context.h:48
std::string cxx_namespace_extern
Definition: context.h:44
std::vector< hilti::rt::filesystem::path > library_paths
Definition: context.h:43
void print(std::ostream &out) const
Definition: context.cc:34
std::shared_ptr< Unit > unit
Definition: context.h:123
Definition: context.h:122
bool debug_trace
Definition: context.h:35
bool debug
Definition: context.h:34
T getAuxOption(const std::string &key, T default_) const
Definition: context.h:62
hilti::rt::filesystem::path path
Definition: context.h:109
const Options & options() const
Definition: context.h:143
Definition: elements.cc:17
bool skip_validation
Definition: context.h:40
Definition: id.h:18
Definition: result.h:67
Definition: context.h:132
Definition: logger.h:28