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 <functional>
7 #include <iostream>
8 #include <memory>
9 #include <optional>
10 #include <string>
11 #include <utility>
12 #include <vector>
13 
14 #include <hilti/rt/3rdparty/ArticleEnumClass-v2/EnumClass.h>
15 
16 #include <hilti/ast/ctor.h>
17 #include <hilti/ast/node.h>
18 #include <hilti/ast/type.h>
19 #include <hilti/base/logger.h>
20 #include <hilti/base/result.h>
21 #include <hilti/base/util.h>
22 #include <hilti/compiler/coercion.h>
23 #include <hilti/compiler/context.h>
24 
25 namespace hilti {
26 
27 class Unit;
28 class Context;
29 
30 namespace printer {
31 class Stream;
32 } // namespace printer
33 
52 struct Plugin {
54  template<typename Result, typename... Args>
55  using Hook = std::optional<std::function<Result(Args...)>>;
56 
58  std::string component;
59 
64  int order = 0;
65 
67  hilti::rt::filesystem::path extension;
68 
73  std::vector<hilti::rt::filesystem::path> cxx_includes;
74 
83 
92  Hook<Result<Node>, std::istream&, hilti::rt::filesystem::path> parse;
93 
106  Hook<std::optional<Ctor>, Ctor, const Type&, bitmask<CoercionStyle>> coerce_ctor;
107 
123  Hook<std::optional<Type>, Type, const Type&, bitmask<CoercionStyle>> coerce_type;
124 
134 
144 
154 
164 
175 
185 
196 
207 };
208 
209 class PluginRegistry;
210 
211 namespace plugin {
213 PluginRegistry& registry();
214 } // namespace plugin
215 
221 public:
222  PluginRegistry();
223 
228  const std::vector<Plugin>& plugins() const { return _plugins; }
229 
237  Result<std::reference_wrapper<const Plugin>> pluginForExtension(hilti::rt::filesystem::path ext) const;
238 
243  const Plugin& hiltiPlugin() const;
244 
251  template<typename PluginMember>
252  bool hasHookFor(PluginMember hook) {
253  for ( const auto& p : plugin::registry().plugins() ) {
254  if ( p.*hook )
255  return true;
256  }
257 
258  return false;
259  }
260 
267  bool supportsExtension(hilti::rt::filesystem::path ext) const {
268  return pluginForExtension(std::move(ext)).hasValue();
269  }
270 
272  auto supportedExtensions() const {
273  return util::transform(_plugins, [](auto& p) { return p.extension; });
274  }
275 
284  void register_(const Plugin& p);
285 
286 private:
287  std::vector<Plugin> _plugins;
288 };
289 
290 namespace detail {
291 
292 Plugin create_hilti_plugin();
293 
294 } // namespace detail
295 
296 } // namespace hilti
auto supportedExtensions() const
Definition: plugin.h:272
bool hasHookFor(PluginMember hook)
Definition: plugin.h:252
Hook< Result< Node >, std::istream &, hilti::rt::filesystem::path > parse
Definition: plugin.h:92
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_transform
Definition: plugin.h:206
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_normalize
Definition: plugin.h:143
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_validate_pre
Definition: plugin.h:174
bool supportsExtension(hilti::rt::filesystem::path ext) const
Definition: plugin.h:267
std::vector< hilti::rt::filesystem::path > cxx_includes
Definition: plugin.h:73
Definition: plugin.h:52
const std::vector< Plugin > & plugins() const
Definition: plugin.h:228
bool hasValue() const
Definition: result.h:152
std::string component
Definition: plugin.h:58
Definition: type.h:160
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_coerce
Definition: plugin.h:153
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_build_scopes
Definition: plugin.h:133
Hook< std::optional< Type >, Type, const Type &, bitmask< CoercionStyle > > coerce_type
Definition: plugin.h:123
Definition: plugin.h:220
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_resolve
Definition: plugin.h:163
Hook< std::vector< hilti::rt::filesystem::path >, std::shared_ptr< hilti::Context > > library_paths
Definition: plugin.h:82
std::optional< std::function< Result(Args...)> > Hook
Definition: plugin.h:55
Definition: unit.h:49
Definition: node.h:112
hilti::rt::filesystem::path extension
Definition: plugin.h:67
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_validate_post
Definition: plugin.h:184
Hook< bool, const Node &, hilti::printer::Stream & > ast_print
Definition: plugin.h:195
Definition: result.h:67
Hook< std::optional< Ctor >, Ctor, const Type &, bitmask< CoercionStyle > > coerce_ctor
Definition: plugin.h:106