Spicy
plugin.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <algorithm>
6 #include <iostream>
7 #include <memory>
8 #include <optional>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 #include <hilti/rt/3rdparty/ArticleEnumClass-v2/EnumClass.h>
14 
15 #include <hilti/ast/ctor.h>
16 #include <hilti/ast/node.h>
17 #include <hilti/ast/type.h>
18 #include <hilti/base/logger.h>
19 #include <hilti/base/result.h>
20 #include <hilti/base/util.h>
21 #include <hilti/compiler/coercion.h>
22 #include <hilti/compiler/context.h>
23 
24 namespace hilti {
25 
26 class Unit;
27 class Context;
28 
29 namespace printer {
30 class Stream;
31 } // namespace printer
32 
51 struct Plugin {
53  template<typename Result, typename... Args>
54  using Hook = std::optional<std::function<Result(Args...)>>;
55 
57  std::string component;
58 
60  hilti::rt::filesystem::path extension;
61 
66  std::vector<hilti::rt::filesystem::path> cxx_includes;
67 
72  int order = 0;
73 
82 
91  Hook<Result<Node>, std::istream&, hilti::rt::filesystem::path> parse;
92 
105  Hook<std::optional<Ctor>, Ctor, const Type&, bitmask<CoercionStyle>> coerce_ctor;
106 
122  Hook<std::optional<Type>, Type, const Type&, bitmask<CoercionStyle>> coerce_type;
123 
132  Hook<void, std::shared_ptr<hilti::Context>, const std::vector<std::pair<ID, NodeRef>>&, Unit*> build_scopes;
133 
143 
153 
164 
176 
186 
198 
210 
220 };
221 
222 class PluginRegistry;
223 
224 namespace plugin {
226 PluginRegistry& registry();
227 } // namespace plugin
228 
234 public:
235  PluginRegistry();
236 
241  const std::vector<Plugin>& plugins() const { return _plugins; }
242 
250  Result<Plugin> pluginForExtension(hilti::rt::filesystem::path ext) const;
251 
258  template<typename PluginMember>
259  bool hasHookFor(PluginMember hook) {
260  for ( const auto& p : plugin::registry().plugins() ) {
261  if ( p.*hook )
262  return true;
263  }
264 
265  return false;
266  }
267 
274  bool supportsExtension(hilti::rt::filesystem::path ext) const {
275  return pluginForExtension(std::move(ext)).hasValue();
276  }
277 
279  auto supportedExtensions() const {
280  return util::transform(_plugins, [](auto& p) { return p.extension; });
281  }
282 
291  void register_(const Plugin& p);
292 
293 private:
294  std::vector<Plugin> _plugins;
295 };
296 
297 namespace plugin {
302 class Register {
303 public:
309  Register(const Plugin& p) { registry().register_(p); }
310 };
311 
312 } // namespace plugin
313 
314 } // namespace hilti
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > resolve_ids
Definition: plugin.h:142
auto supportedExtensions() const
Definition: plugin.h:279
bool hasHookFor(PluginMember hook)
Definition: plugin.h:259
Hook< Result< Node >, std::istream &, hilti::rt::filesystem::path > parse
Definition: plugin.h:91
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > resolve_operators
Definition: plugin.h:152
Hook< bool, std::shared_ptr< hilti::Context >, Node *, bool, Unit * > transform
Definition: plugin.h:209
bool supportsExtension(hilti::rt::filesystem::path ext) const
Definition: plugin.h:274
std::vector< hilti::rt::filesystem::path > cxx_includes
Definition: plugin.h:66
Definition: plugin.h:51
const std::vector< Plugin > & plugins() const
Definition: plugin.h:241
bool hasValue() const
Definition: result.h:148
std::string component
Definition: plugin.h:57
Definition: plugin.h:302
Hook< void, std::shared_ptr< hilti::Context >, std::vector< Node > *, Unit * > preserved_validate
Definition: plugin.h:197
Hook< bool, const Node &, hilti::printer::Stream & > print_ast
Definition: plugin.h:219
Hook< std::optional< Type >, Type, const Type &, bitmask< CoercionStyle > > coerce_type
Definition: plugin.h:122
Definition: plugin.h:233
Register(const Plugin &p)
Definition: plugin.h:309
Hook< void, std::shared_ptr< hilti::Context >, const std::vector< std::pair< ID, NodeRef > > &, Unit * > build_scopes
Definition: plugin.h:132
Hook< std::vector< hilti::rt::filesystem::path >, std::shared_ptr< hilti::Context > > library_paths
Definition: plugin.h:81
std::optional< std::function< Result(Args...)> > Hook
Definition: plugin.h:54
Definition: unit.h:43
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > apply_coercions
Definition: plugin.h:163
Definition: node.h:97
hilti::rt::filesystem::path extension
Definition: plugin.h:60
auto transform(const std::vector< X > &x, F f)
Definition: util.h:86
Hook< void, std::shared_ptr< hilti::Context >, Node *, Unit * > post_validate
Definition: plugin.h:185
Hook< void, std::shared_ptr< hilti::Context >, Node *, Unit *, bool * > pre_validate
Definition: plugin.h:175
Definition: result.h:67
Hook< std::optional< Ctor >, Ctor, const Type &, bitmask< CoercionStyle > > coerce_ctor
Definition: plugin.h:105