diff options
author | Francesco Mazzoli <f@mazzo.li> | 2017-03-07 23:39:51 -0500 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2017-03-08 19:15:54 -0500 |
commit | 0fac488cca04a07224926e35be9c45ee2d0e1631 (patch) | |
tree | 48c5317fa66d9e09ff9bd829daf26539a971abc8 /libraries/template-haskell | |
parent | de62f587463f6377df1e69e11504578833dfe653 (diff) | |
download | haskell-0fac488cca04a07224926e35be9c45ee2d0e1631.tar.gz |
Allow compilation of C/C++/ObjC/ObjC++ files with module from TH
The main goal is to easily allow the inline-c project (and
similar projects such as inline-java) to emit C/C++ files to
be compiled and linked with the current module.
Moreover, `addCStub` is removed, since it's quite fragile. Most
notably, the C stubs end up in the file generated by
`CodeOutput.outputForeignStubs`, which is tuned towards
generating a file for stubs coming from `capi` and Haskell-to-C
exports.
Reviewers: simonmar, austin, goldfire, facundominguez, dfeuer, bgamari
Reviewed By: dfeuer, bgamari
Subscribers: snowleopard, rwbarton, dfeuer, thomie, duncan, mboes
Differential Revision: https://phabricator.haskell.org/D3280
Diffstat (limited to 'libraries/template-haskell')
-rw-r--r-- | libraries/template-haskell/Language/Haskell/TH/Syntax.hs | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs index c531eeffd7..466834a9a4 100644 --- a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs +++ b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs @@ -27,6 +27,7 @@ module Language.Haskell.TH.Syntax ( module Language.Haskell.TH.Syntax -- * Language extensions , module Language.Haskell.TH.LanguageExtensions + , ForeignSrcLang(..) ) where import Data.Data hiding (Fixity(..)) @@ -40,6 +41,7 @@ import Data.Word import Data.Ratio import GHC.Generics ( Generic ) import GHC.Lexeme ( startsVarSym, startsVarId ) +import GHC.ForeignSrcLang.Type import Language.Haskell.TH.LanguageExtensions import Numeric.Natural @@ -92,7 +94,7 @@ class Monad m => Quasi m where qAddTopDecls :: [Dec] -> m () - qAddCStub :: String -> m () + qAddForeignFile :: ForeignSrcLang -> String -> m () qAddModFinalizer :: Q () -> m () @@ -133,7 +135,7 @@ instance Quasi IO where qRecover _ _ = badIO "recover" -- Maybe we could fix this? qAddDependentFile _ = badIO "addDependentFile" qAddTopDecls _ = badIO "addTopDecls" - qAddCStub _ = badIO "addCStub" + qAddForeignFile _ _ = badIO "addForeignFile" qAddModFinalizer _ = badIO "addModFinalizer" qGetQ = badIO "getQ" qPutQ _ = badIO "putQ" @@ -459,24 +461,25 @@ addDependentFile fp = Q (qAddDependentFile fp) addTopDecls :: [Dec] -> Q () addTopDecls ds = Q (qAddTopDecls ds) --- | Add an additional C stub. The added stub will be built and included in the --- object file of the current module. +-- | Emit a foreign file which will be compiled and linked to the object for +-- the current module. Currently only languages that can be compiled with +-- the C compiler are supported, and the flags passed as part of -optc will +-- be also applied to the C compiler invocation that will compile them. -- --- Compilation errors in the given string are reported next to the line of the --- enclosing splice. +-- Note that for non-C languages (for example C++) @extern "C"@ directives +-- must be used to get symbols that we can access from Haskell. -- --- The accuracy of the error location can be improved by adding --- #line pragmas in the argument. e.g. +-- To get better errors, it is reccomended to use #line pragmas when +-- emitting C files, e.g. -- -- > {-# LANGUAGE CPP #-} -- > ... --- > addCStub $ unlines +-- > addForeignFile LangC $ unlines -- > [ "#line " ++ show (__LINE__ + 1) ++ " " ++ show __FILE__ -- > , ... -- > ] --- -addCStub :: String -> Q () -addCStub str = Q (qAddCStub str) +addForeignFile :: ForeignSrcLang -> String -> Q () +addForeignFile lang str = Q (qAddForeignFile lang str) -- | Add a finalizer that will run in the Q monad after the current module has -- been type checked. This only makes sense when run within a top-level splice. @@ -521,7 +524,7 @@ instance Quasi Q where qRunIO = runIO qAddDependentFile = addDependentFile qAddTopDecls = addTopDecls - qAddCStub = addCStub + qAddForeignFile = addForeignFile qAddModFinalizer = addModFinalizer qGetQ = getQ qPutQ = putQ |