Safe Haskell | None |
---|---|
Language | Haskell2010 |
Shared portion of the C++ code generator. Usable by binding definitions.
Synopsis
- type Generator = ReaderT Env (WriterT [Chunk] (Either ErrorMsg))
- data Env
- execGenerator :: Interface -> ComputedInterfaceData -> Module -> Maybe String -> Generator a -> Either ErrorMsg String
- addIncludes :: MonadWriter [Chunk] m => [Include] -> m ()
- addInclude :: MonadWriter [Chunk] m => Include -> m ()
- addReqsM :: MonadWriter [Chunk] m => Reqs -> m ()
- askInterface :: MonadReader Env m => m Interface
- askComputedInterfaceData :: Generator ComputedInterfaceData
- askModule :: MonadReader Env m => m Module
- abort :: ErrorMsg -> Generator a
- makeCppName :: [String] -> String
- externalNameToCpp :: ExtName -> String
- toArgName :: Int -> String
- toArgNameAlt :: Int -> String
- exceptionIdArgName :: String
- exceptionPtrArgName :: String
- exceptionVarName :: String
- exceptionRethrowFnName :: String
- data Chunk = Chunk {
- chunkContents :: !String
- chunkIncludes :: !(Set Include)
- codeChunk :: String -> Chunk
- includesChunk :: Set Include -> Chunk
- runChunkWriter :: Writer [Chunk] a -> (a, Chunk)
- evalChunkWriter :: Writer [Chunk] a -> a
- execChunkWriter :: Writer [Chunk] a -> Chunk
- runChunkWriterT :: Monad m => WriterT [Chunk] m a -> m (a, Chunk)
- evalChunkWriterT :: Monad m => WriterT [Chunk] m a -> m a
- execChunkWriterT :: Monad m => WriterT [Chunk] m a -> m Chunk
- data SayExportMode
- say :: MonadWriter [Chunk] m => String -> m ()
- says :: MonadWriter [Chunk] m => [String] -> m ()
- sayIdentifier :: MonadWriter [Chunk] m => Identifier -> m ()
- renderIdentifier :: Identifier -> String
- sayVar :: MonadWriter [Chunk] m => String -> Maybe [String] -> Type -> m ()
- sayType :: MonadWriter [Chunk] m => Maybe [String] -> Type -> m ()
- sayFunction :: String -> [String] -> Type -> Maybe (Generator ()) -> Generator ()
- typeToCType :: Type -> Generator (Maybe Type)
- typeReqs :: Type -> Generator Reqs
- findExportModule :: ExtName -> Generator Module
- getEffectiveExceptionHandlers :: ExceptionHandlers -> Generator ExceptionHandlers
Code generation monad
type Generator = ReaderT Env (WriterT [Chunk] (Either ErrorMsg)) Source #
A generator monad for C++ code.
TODO This should not simply be a type synonym.
execGenerator :: Interface -> ComputedInterfaceData -> Module -> Maybe String -> Generator a -> Either ErrorMsg String Source #
Runs a generator action and returns its output, or an error message if unsuccessful.
addIncludes :: MonadWriter [Chunk] m => [Include] -> m () Source #
Adds #include
statements to the includes block generated at the top of
the currently generating file.
addInclude :: MonadWriter [Chunk] m => Include -> m () Source #
Adds an #include
statement to the includes block generated at the top of
the currently generating file.
askInterface :: MonadReader Env m => m Interface Source #
Returns the currently generating interface.
askComputedInterfaceData :: Generator ComputedInterfaceData Source #
Returns the computed data for the currently generating interface.
Names
makeCppName :: [String] -> String Source #
Constructs a C++ identifier by combining a list of strings with __
.
externalNameToCpp :: ExtName -> String Source #
Returns the C++ binding function name for an external name.
toArgName :: Int -> String Source #
Returns a distinct argument variable name for each nonnegative number.
toArgNameAlt :: Int -> String Source #
Same as toArgName
, but with distinct names, with with similarity between
toArgName n
and toArgNameAlt n
.
exceptionIdArgName :: String Source #
The C++ variable name to use for the exception ID argument in a gateway function.
exceptionPtrArgName :: String Source #
The C++ variable name to use for the exception pointer argument in a gateway function.
exceptionVarName :: String Source #
The C++ variable name to use in a catch
statement in a gateway function.
exceptionRethrowFnName :: String Source #
The name of the C++ function that receives an exception from a foreign language and throws it in C++.
Token rendering
A chunk is a string that contains an arbitrary portion of C++ code, together with a set of includes. The only requirement is that chunk's code boundaries are also C++ token boundaries, because the generator monad automates the process of inserting whitespace between chunk boundaries where necessary.
Chunk | |
|
runChunkWriter :: Writer [Chunk] a -> (a, Chunk) Source #
Runs a Chunk
writer, combining them with combineChunks
to form a single
string.
execChunkWriter :: Writer [Chunk] a -> Chunk Source #
Runs a Chunk
writer and returns the written log.
runChunkWriterT :: Monad m => WriterT [Chunk] m a -> m (a, Chunk) Source #
Runs a Chunk
writer transformer, combining them with combineChunks
to
form a single string.
evalChunkWriterT :: Monad m => WriterT [Chunk] m a -> m a Source #
Runs a Chunk
writer transformer and returns the monad's value.
execChunkWriterT :: Monad m => WriterT [Chunk] m a -> m Chunk Source #
Runs a Chunk
writer transformer and returns the written log.
High-level code generation
data SayExportMode Source #
The section of code that Hoppy is generating, for an export.
SaySource | Hoppy is generating the C++ source file for a module. The generator should emit C++ definitions that will be imported over foreign language's FFIs. This is the main place for code generation in C++ bindings. |
SayHeader | Hoppy is generating the C++ header file for a module. The generator
should emit C++ declarations that can be |
sayIdentifier :: MonadWriter [Chunk] m => Identifier -> m () Source #
Emits an Identifier
.
renderIdentifier :: Identifier -> String Source #
Renders an Identifier
to a string.
sayVar :: MonadWriter [Chunk] m => String -> Maybe [String] -> Type -> m () Source #
sayVar name maybeParamNames t
speaks a variable declaration of the form
<type> <name>
, where <name>
is the given name, and <type>
is
rendered by giving maybeParamNames
and t
to sayType
.
This function is useful for generating variable declarations, declarations with assignments, and function prototypes and definitions.
sayType :: MonadWriter [Chunk] m => Maybe [String] -> Type -> m () Source #
sayType maybeParamNames t
renders t
in C++ syntax. If t
is a
fnT
, then maybeParamNames
will provide variable names for parameters, if
present.
:: String | Function name. |
-> [String] | Parameter names. |
-> Type | |
-> Maybe (Generator ()) | If present, then the function is defined and the action here is used to render its body. If absent, then the function is only declared (no function body). |
-> Generator () |
Renders a C++ function.
Auxiliary functions
typeToCType :: Type -> Generator (Maybe Type) Source #
Returns a Type
iff there is a C type distinct from the given C++ type
that should be used for conversion.
This returns Nothing
for Internal_TManual
. TManual needs special
handling.
typeReqs :: Type -> Generator Reqs Source #
Returns the requirements to refer to a type from C++ code. This is a monadic function so that it has access to the environment, but it does not emit any code.
findExportModule :: ExtName -> Generator Module Source #
Looks up the module exporting the given external name in the current
interface. abort
is called if the external name is not found.
getEffectiveExceptionHandlers :: ExceptionHandlers -> Generator ExceptionHandlers Source #
Combines the given exception handlers (from a particular exported entity) with the handlers from the current module and interface. The given handlers have highest precedence, followed by module handlers, followed by interface handlers.