module Language.C.Inline.Error (
reportErrorWithLang, reportErrorAndFail,
tryWithPlaceholder,
prettyQC
) where
import Language.Haskell.TH as TH
import Language.Haskell.TH.Syntax as TH
import Language.C.Quote as QC
import Language.C.Quote.ObjC as QC
import Text.PrettyPrint.Mainland as QC
reportErrorWithLang :: QC.Extensions -> String -> Q ()
reportErrorWithLang lang msg
= do
{ loc <- location
; TH.reportError $ "Inline " ++ showLang lang ++ ": " ++ msg
}
reportErrorAndFail :: QC.Extensions -> String -> Q a
reportErrorAndFail lang msg
= reportErrorAndFail' $ "Inline " ++ showLang lang ++ ": " ++ msg
reportErrorAndFail' :: String -> Q a
reportErrorAndFail' msg
= do
{ TH.reportError msg
; fail "Fatal error due to inline code"
}
showLang :: QC.Extensions -> String
showLang QC.Antiquotation = "C"
showLang QC.Gcc = "GCC C"
showLang QC.CUDA = "CUDA C"
showLang QC.OpenCL = "OpenCL"
showLang QC.ObjC = "Objective-C"
tryWithPlaceholder :: Q TH.Exp -> Q TH.Exp
tryWithPlaceholder = ([| error "language-c-quote: internal error: tryWithPlaceholder" |] `recover`)
prettyQC :: QC.Pretty a => a -> String
prettyQC = QC.pretty 80 . QC.ppr