Spicy
Public Types | Public Attributes | List of all members
hilti::Plugin Struct Reference

#include <plugin.h>

Public Types

template<typename Result , typename... Args>
using Hook = std::optional< std::function< Result(Args...)> >
 

Public Attributes

std::string component
 
int order = 0
 
hilti::rt::filesystem::path extension
 
std::vector< hilti::rt::filesystem::path > cxx_includes
 
Hook< std::vector< hilti::rt::filesystem::path >, std::shared_ptr< hilti::Context > > library_paths
 
Hook< Result< Node >, std::istream &, hilti::rt::filesystem::path > parse
 
Hook< std::optional< Ctor >, Ctor, const Type &, bitmask< CoercionStyle > > coerce_ctor
 
Hook< std::optional< Type >, Type, const Type &, bitmask< CoercionStyle > > coerce_type
 
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_build_scopes
 
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_normalize
 
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_coerce
 
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_resolve
 
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_validate_pre
 
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_validate_post
 
Hook< bool, const Node &, hilti::printer::Stream & > ast_print
 
Hook< bool, std::shared_ptr< hilti::Context >, Node *, Unit * > ast_transform
 

Detailed Description

Compiler plugin that implements AST-to-AST translation through a set of passes.

The HILTI compiler itself is the one plugin that's always available. On top of that, further plugins may implement passes as needed to preprocess an AST before it gets to the HILTI plugin. That way, an external plugin can implement support for new language using HILTI as its codegen backend by (1) reading its representation into an AST using its own set of nodes (which may include reusing existing HILTI AST nodes where convinient), (2) implementing the resolution passes to fully resolve that AST (reusing HILTI passes internally where convenient), and (3) finally transforming that AST into a pure HILTI AST consisting only of the HILT nodes.

A plugin implements a set of hook methods that get called by the compilation process at the appropriate times. All hooks should be stateless, apart from changing the AST as appropriate.

Member Typedef Documentation

◆ Hook

template<typename Result , typename... Args>
using hilti::Plugin::Hook = std::optional<std::function<Result(Args...)> >

Helper template to define the type of hook methods.

Member Data Documentation

◆ ast_build_scopes

Hook<bool, std::shared_ptr<hilti::Context>, Node*, Unit*> hilti::Plugin::ast_build_scopes

Hook called to build the scopes in a module's AST.

Parameters
arg1compiler context that's in use
arg2root node of AST; the hook may modify the AST
arg3current unit being compiled
Returns
true if the hook modified the AST in a substantial way

◆ ast_coerce

Hook<bool, std::shared_ptr<hilti::Context>, Node*, Unit*> hilti::Plugin::ast_coerce

Hook called to apply type coersions to the AST.

Parameters
arg1compiler context that's in use
arg2root node of AST; the hook may modify the AST
arg3current unit being compiled
Returns
true if the hook modified the AST in a substantial way

◆ ast_normalize

Hook<bool, std::shared_ptr<hilti::Context>, Node*, Unit*> hilti::Plugin::ast_normalize

Hook called to prepare an AST before any further stages execute.

Parameters
arg1compiler context that's in use
arg2root node of AST; the hook may modify the AST
arg3current unit being compiled
Returns
true if the hook modified the AST in a substantial way

◆ ast_print

Hook<bool, const Node&, hilti::printer::Stream&> hilti::Plugin::ast_print

Hook called to print an AST back as source code. The hook gets to choose if it wants to print the node itself, or fall back to the default printer.

Parameters
arg1root of AST to print
arg2stream to print to
Returns
true if the hook printed the AST, false to fall back to default

◆ ast_resolve

Hook<bool, std::shared_ptr<hilti::Context>, Node*, Unit*> hilti::Plugin::ast_resolve

Hook called to resolve unknown types and other entities.

Parameters
arg1compiler context that's in use
arg2root node of AST; the hook may modify the AST
arg3current unit being compiled
Returns
true if the hook modified the AST in a substantial way

◆ ast_transform

Hook<bool, std::shared_ptr<hilti::Context>, Node*, Unit*> hilti::Plugin::ast_transform

Hook called to replace AST nodes of one language (plugin) with nodes of another coming further down in the pipeline.

Parameters
arg1compiler context that's in use
arg2root node of AST; the hook may modify the AST
arg3current unit being compiled
Returns
true if the hook modified the AST in a substantial way

◆ ast_validate_post

Hook<bool, std::shared_ptr<hilti::Context>, Node*, Unit*> hilti::Plugin::ast_validate_post

Hook called to validate correctness of an AST once fully resolved. Any errors must be reported by setting the nodes' error information.

Parameters
arg1compiler context that's in use
arg2root node of AST; the hook may not modify the AST
arg3current unit being compiled

◆ ast_validate_pre

Hook<bool, std::shared_ptr<hilti::Context>, Node*, Unit*> hilti::Plugin::ast_validate_pre

Hook called to validate correctness of an AST before resolving starts (to the degree it can at that time). Any errors must be reported by setting the nodes' error information.

Parameters
arg1compiler context that's in use
arg2root node of AST; the hook may not modify the AST
arg3current unit being compiled

◆ coerce_ctor

Hook<std::optional<Ctor>, Ctor, const Type&, bitmask<CoercionStyle> > hilti::Plugin::coerce_ctor

Hook called to perform coercion of a Ctor into another of a given target type.

If the plugin knows how to handle the coercion, the hook returns a new Ctor that's now of the target type.

Parameters
arg1compiler context that's in use
arg2ctor that needs coercion
arg3target type for ctor
arg4coercion style to use
Returns
new ctor if plugin performed coercion

◆ coerce_type

Hook<std::optional<Type>, Type, const Type&, bitmask<CoercionStyle> > hilti::Plugin::coerce_type

Hook called to approved coercion of an expression into a different type.

If the plugin knows it can handle the coercion, it returns the resulting coerced Type. If so, it must then also provide an apply_coercions hook that will later be called to perform the actual coercion during code generation.

Parameters
arg1compiler context that's in use
arg2type that needs coercion
arg3target type for coercion
arg4coercion style to use
Returns
new type if plugin can handle this coercion

◆ component

std::string hilti::Plugin::component

Name of the plugin.

◆ cxx_includes

std::vector<hilti::rt::filesystem::path> hilti::Plugin::cxx_includes

Additional C++ include files that the plugin needs to have added to generated C++ code.

◆ extension

hilti::rt::filesystem::path hilti::Plugin::extension

Extension for source files that the plugin handles. Must include the leading ..

◆ library_paths

Hook<std::vector<hilti::rt::filesystem::path>, std::shared_ptr<hilti::Context> > hilti::Plugin::library_paths

Hook called to retrieve paths to search when importing modules that this plugin handles.

Parameters
arg1compiler context that's in use
Returns
directories to search

◆ order

int hilti::Plugin::order = 0

Plugins will be executed in numerical order, with lower order numbers executing first.

◆ parse

Hook<Result<Node>, std::istream&, hilti::rt::filesystem::path> hilti::Plugin::parse

Hook called to parse input file that this plugin handles.

Parameters
arg1compiler context that's in use #param arg2 input stream to parse
arg3file associated with the input stream
Returns
directories to search

The documentation for this struct was generated from the following file: