inline-c-0.5.5.3: Write Haskell source files including C code inline. No FFI required.

Language.C.Inline.Internal

Synopsis

# Context handling

Sets the Context for the current module. This function, if called, must be called before any of the other TH functions in this module. Fails if that's not the case.

Gets the current Context. Also makes sure that the current module is initialised.

# Emitting and invoking C code

The functions in this section let us access more the C file associated with the current module. They can be used to build additional features on top of the basic machinery. All of inline-c is based upon the functions defined here.

## Emitting C code

Simply appends some string to the module's C file. Use with care.

## Inlining C code

We use the Code data structure to represent some C code that we want to emit to the module's C file and immediately generate a foreign call to. For this reason, Code includes both some C definition, and enough information to be able to generate a foreign call -- specifically the name of the function to call and the Haskell type.

All the quasi-quoters work by constructing a Code and calling inlineCode.

data Code Source #

Data type representing a list of C definitions with a typed and named entry function.

We use it as a basis to inline and call C code.

Constructors

 Code FieldscodeCallSafety :: SafetySafety of the foreign call.codeType :: TypeQType of the foreign call.codeFunName :: StringName of the function to call in the code below.codeDefs :: StringThe C code.

Inlines a piece of code inline. The resulting Exp will have the type specified in the codeType.

In practice, this function outputs the C code to the module's C file, and then inserts a foreign call of type codeType calling the provided codeFunName.

Example:

c_add :: Int -> Int -> Int
c_add = $(inlineCode$ Code
TH.Unsafe                   -- Call safety
[t| Int -> Int -> Int |]    -- Call type
-- C Code
"int francescos_add(int x, int y) { int z = x + y; return z; }")


Arguments

 :: Safety Safety of the foreign call -> TypeQ Type of the foreign call -> Type CIdentifier Return type of the C expr -> [(CIdentifier, Type CIdentifier)] Parameters of the C expr -> String The C expression -> ExpQ

Same as inlineCItems, but with a single expression.

c_cos :: Double -> Double
c_cos = $(inlineExp TH.Unsafe [t| Double -> Double |] (quickCParser_ "double" parseType) [("x", quickCParser_ "double") parseType] "cos(x)")  Arguments  :: Safety Safety of the foreign call -> TypeQ Type of the foreign call -> Type CIdentifier Return type of the C expr -> [(CIdentifier, Type CIdentifier)] Parameters of the C expr -> String The C items -> ExpQ Same as inlineCode, but accepts a string containing a list of C statements instead instead than a full-blown Code. A function containing the provided statement will be automatically generated. c_cos :: Double -> Double c_cos =$(inlineItems
TH.Unsafe
[t| Double -> Double |]
(quickCParser_ "double" parseType)
[("x", quickCParser_ "double" parseType)]
"return cos(x);")


# Parsing

These functions are used to parse the anti-quotations. They're exposed for testing purposes, you really should not use them.

data SomeEq Source #

Instances

 Source # Methods(==) :: SomeEq -> SomeEq -> Bool #(/=) :: SomeEq -> SomeEq -> Bool # Source # MethodsshowsPrec :: Int -> SomeEq -> ShowS #showList :: [SomeEq] -> ShowS #

toSomeEq :: (Eq a, Typeable a) => a -> SomeEq Source #

Constructors

Instances

 Source # Methods Source # MethodsshowList :: [ParameterType] -> ShowS #

Constructors

 ParseTypedC Fields

Arguments

 :: CParser HaskellIdentifier m => AntiQuoters -> m ParseTypedC Returns the return type, the captured variables, and the body.

runParserInQ :: String -> TypeNames -> (forall m. CParser HaskellIdentifier m => m a) -> Q a Source #

# Utility functions for writing quasiquoters

Arguments

 :: Purity -> (TypeQ -> Type CIdentifier -> [(CIdentifier, Type CIdentifier)] -> String -> ExpQ) Function building an Haskell expression, see inlineExp for guidance on the other args. -> QuasiQuoter