{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE KindSignatures #-}
module FFICXX.Runtime.CodeGen.Cxx where
import Data.Functor.Identity (Identity)
import Data.Kind (Type)
import Data.List (intercalate)
import Data.String (IsString (..))
newtype = HdrName {HeaderName -> String
unHdrName :: String}
deriving (Int -> HeaderName -> ShowS
[HeaderName] -> ShowS
HeaderName -> String
(Int -> HeaderName -> ShowS)
-> (HeaderName -> String)
-> ([HeaderName] -> ShowS)
-> Show HeaderName
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HeaderName -> ShowS
showsPrec :: Int -> HeaderName -> ShowS
$cshow :: HeaderName -> String
show :: HeaderName -> String
$cshowList :: [HeaderName] -> ShowS
showList :: [HeaderName] -> ShowS
Show, HeaderName -> HeaderName -> Bool
(HeaderName -> HeaderName -> Bool)
-> (HeaderName -> HeaderName -> Bool) -> Eq HeaderName
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HeaderName -> HeaderName -> Bool
== :: HeaderName -> HeaderName -> Bool
$c/= :: HeaderName -> HeaderName -> Bool
/= :: HeaderName -> HeaderName -> Bool
Eq, Eq HeaderName
Eq HeaderName
-> (HeaderName -> HeaderName -> Ordering)
-> (HeaderName -> HeaderName -> Bool)
-> (HeaderName -> HeaderName -> Bool)
-> (HeaderName -> HeaderName -> Bool)
-> (HeaderName -> HeaderName -> Bool)
-> (HeaderName -> HeaderName -> HeaderName)
-> (HeaderName -> HeaderName -> HeaderName)
-> Ord HeaderName
HeaderName -> HeaderName -> Bool
HeaderName -> HeaderName -> Ordering
HeaderName -> HeaderName -> HeaderName
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: HeaderName -> HeaderName -> Ordering
compare :: HeaderName -> HeaderName -> Ordering
$c< :: HeaderName -> HeaderName -> Bool
< :: HeaderName -> HeaderName -> Bool
$c<= :: HeaderName -> HeaderName -> Bool
<= :: HeaderName -> HeaderName -> Bool
$c> :: HeaderName -> HeaderName -> Bool
> :: HeaderName -> HeaderName -> Bool
$c>= :: HeaderName -> HeaderName -> Bool
>= :: HeaderName -> HeaderName -> Bool
$cmax :: HeaderName -> HeaderName -> HeaderName
max :: HeaderName -> HeaderName -> HeaderName
$cmin :: HeaderName -> HeaderName -> HeaderName
min :: HeaderName -> HeaderName -> HeaderName
Ord)
instance IsString HeaderName where
fromString :: String -> HeaderName
fromString = String -> HeaderName
HdrName
newtype Namespace = NS {Namespace -> String
unNamespace :: String}
deriving (Int -> Namespace -> ShowS
[Namespace] -> ShowS
Namespace -> String
(Int -> Namespace -> ShowS)
-> (Namespace -> String)
-> ([Namespace] -> ShowS)
-> Show Namespace
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Namespace -> ShowS
showsPrec :: Int -> Namespace -> ShowS
$cshow :: Namespace -> String
show :: Namespace -> String
$cshowList :: [Namespace] -> ShowS
showList :: [Namespace] -> ShowS
Show, Namespace -> Namespace -> Bool
(Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Bool) -> Eq Namespace
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Namespace -> Namespace -> Bool
== :: Namespace -> Namespace -> Bool
$c/= :: Namespace -> Namespace -> Bool
/= :: Namespace -> Namespace -> Bool
Eq, Eq Namespace
Eq Namespace
-> (Namespace -> Namespace -> Ordering)
-> (Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Bool)
-> (Namespace -> Namespace -> Namespace)
-> (Namespace -> Namespace -> Namespace)
-> Ord Namespace
Namespace -> Namespace -> Bool
Namespace -> Namespace -> Ordering
Namespace -> Namespace -> Namespace
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Namespace -> Namespace -> Ordering
compare :: Namespace -> Namespace -> Ordering
$c< :: Namespace -> Namespace -> Bool
< :: Namespace -> Namespace -> Bool
$c<= :: Namespace -> Namespace -> Bool
<= :: Namespace -> Namespace -> Bool
$c> :: Namespace -> Namespace -> Bool
> :: Namespace -> Namespace -> Bool
$c>= :: Namespace -> Namespace -> Bool
>= :: Namespace -> Namespace -> Bool
$cmax :: Namespace -> Namespace -> Namespace
max :: Namespace -> Namespace -> Namespace
$cmin :: Namespace -> Namespace -> Namespace
min :: Namespace -> Namespace -> Namespace
Ord)
instance IsString Namespace where
fromString :: String -> Namespace
fromString = String -> Namespace
NS
data PragmaParam = Once
newtype NamePart (f :: Type -> Type) = NamePart String
newtype CName (f :: Type -> Type) = CName [NamePart f]
sname :: String -> CName Identity
sname :: String -> CName Identity
sname String
s = [NamePart Identity] -> CName Identity
forall (f :: * -> *). [NamePart f] -> CName f
CName [String -> NamePart Identity
forall (f :: * -> *). String -> NamePart f
NamePart String
s]
renderCName :: CName Identity -> String
renderCName :: CName Identity -> String
renderCName (CName [NamePart Identity]
ps) = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"##" ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ (NamePart Identity -> String) -> [NamePart Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (\(NamePart String
p) -> String
p) [NamePart Identity]
ps
data CType (f :: Type -> Type)
= CTVoid
| CTSimple (CName f)
| CTStar (CType f)
| CTAuto
| CTTApp
(CName f)
[CType f]
| CTConst (CType f)
| CTScoped (CType f) (CType f)
| CTVerbatim String
data COp = CArrow | CAssign
renderCOp :: COp -> String
renderCOp :: COp -> String
renderCOp COp
CArrow = String
"->"
renderCOp COp
CAssign = String
"="
data CExp (f :: Type -> Type)
=
CVar (CName f)
|
CApp (CExp f) [CExp f]
|
CTApp (CName f) [CType f] [CExp f]
|
CBinOp COp (CExp f) (CExp f)
|
CCast (CType f) (CExp f)
|
CAddr (CExp f)
|
CStar (CExp f)
|
CNew (CName f) [CExp f]
|
CTNew (CName f) [CType f] [CExp f]
|
CTNewI (CName f) (CName f) [CType f] [CExp f]
|
CEMacroApp (CName f) [CName f]
|
CEVerbatim String
|
CNull
data CFunDecl (f :: Type -> Type)
=
CFunDecl (CType f) (CName f) [(CType f, CName f)]
data CVarDecl (f :: Type -> Type)
= CVarDecl
(CType f)
(CName f)
data CQual = Inline
data CStatement (f :: Type -> Type)
=
UsingNamespace Namespace
|
TypeDef (CType f) (CName f)
|
CExpSA (CExp f)
|
CDeclaration (CFunDecl f)
|
CDefinition (Maybe CQual) (CFunDecl f) [CStatement f]
|
CInit (CVarDecl f) (CExp f)
|
CReturn (CExp f)
|
CDelete (CExp f)
|
CMacroApp (CName f) [CName f]
|
CExtern [CStatement f]
|
String
|
CEmptyLine
|
CVerbatim String
data CMacro (f :: Type -> Type)
=
CRegular (CStatement f)
|
Include HeaderName
|
Pragma PragmaParam
|
Undef (CName f)
|
Define (CName f) [CName f] [CStatement f]
|
EmptyLine
|
Verbatim String
data CBlock (f :: Type -> Type) = ExternC [CMacro f]
renderPragmaParam :: PragmaParam -> String
renderPragmaParam :: PragmaParam -> String
renderPragmaParam PragmaParam
Once = String
"once"
renderCType :: CType Identity -> String
renderCType :: CType Identity -> String
renderCType CType Identity
CTVoid = String
"void"
renderCType (CTSimple CName Identity
n) = CName Identity -> String
renderCName CName Identity
n
renderCType (CTStar CType Identity
t) = CType Identity -> String
renderCType CType Identity
t String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"*"
renderCType CType Identity
CTAuto = String
"auto"
renderCType (CTTApp CName Identity
n [CType Identity]
ts) = CName Identity -> String
renderCName CName Identity
n String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"<" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CType Identity -> String) -> [CType Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CType Identity -> String
renderCType [CType Identity]
ts) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
">"
renderCType (CTConst CType Identity
t) = String
"const " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CType Identity -> String
renderCType CType Identity
t
renderCType (CTScoped CType Identity
t CType Identity
i) = CType Identity -> String
renderCType CType Identity
t String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"::" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CType Identity -> String
renderCType CType Identity
i
renderCType (CTVerbatim String
t) = String
t
renderCExp :: CExp Identity -> String
renderCExp :: CExp Identity -> String
renderCExp (CVar CName Identity
n) = CName Identity -> String
renderCName CName Identity
n
renderCExp (CApp CExp Identity
f [CExp Identity]
es) =
( case CExp Identity
f of
CVar CName Identity
_ -> CExp Identity -> String
renderCExp CExp Identity
f
CExp Identity
_ -> String
"(" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CExp Identity -> String
renderCExp CExp Identity
f String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"("
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CExp Identity -> String) -> [CExp Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CExp Identity -> String
renderCExp [CExp Identity]
es)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
renderCExp (CTApp CName Identity
f [CType Identity]
ts [CExp Identity]
es) =
CName Identity -> String
renderCName CName Identity
f
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"<"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CType Identity -> String) -> [CType Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CType Identity -> String
renderCType [CType Identity]
ts)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
">"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"("
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CExp Identity -> String) -> [CExp Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CExp Identity -> String
renderCExp [CExp Identity]
es)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
renderCExp (CBinOp COp
o CExp Identity
x CExp Identity
y) =
( case CExp Identity
x of
CVar CName Identity
_ -> CExp Identity -> String
renderCExp CExp Identity
x
CExp Identity
_ -> String
"(" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CExp Identity -> String
renderCExp CExp Identity
x String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> COp -> String
renderCOp COp
o
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CExp Identity -> String
renderCExp CExp Identity
y
renderCExp (CCast CType Identity
t CExp Identity
e) = String
"(" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CType Identity -> String
renderCType CType Identity
t String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CExp Identity -> String
renderCExp CExp Identity
e
renderCExp (CAddr CExp Identity
e) = String
"&(" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CExp Identity -> String
renderCExp CExp Identity
e String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
renderCExp (CStar CExp Identity
e) = String
"*(" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CExp Identity -> String
renderCExp CExp Identity
e String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
renderCExp (CNew CName Identity
n [CExp Identity]
es) =
String
"new "
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
n
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"("
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CExp Identity -> String) -> [CExp Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CExp Identity -> String
renderCExp [CExp Identity]
es)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
renderCExp (CTNew CName Identity
n [CType Identity]
ts [CExp Identity]
es) =
String
"new "
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
n
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"<"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CType Identity -> String) -> [CType Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CType Identity -> String
renderCType [CType Identity]
ts)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
">"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"("
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CExp Identity -> String) -> [CExp Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CExp Identity -> String
renderCExp [CExp Identity]
es)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
renderCExp (CTNewI CName Identity
n CName Identity
i [CType Identity]
ts [CExp Identity]
es) =
String
"new "
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
n
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"<"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CType Identity -> String) -> [CType Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CType Identity -> String
renderCType [CType Identity]
ts)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
">::"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
i
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"("
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CExp Identity -> String) -> [CExp Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CExp Identity -> String
renderCExp [CExp Identity]
es)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
renderCExp (CEMacroApp CName Identity
n [CName Identity]
as) =
CName Identity -> String
renderCName CName Identity
n
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"("
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CName Identity -> String) -> [CName Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CName Identity -> String
renderCName [CName Identity]
as)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
renderCExp (CEVerbatim String
e) = String
e
renderCExp CExp Identity
CNull = String
""
renderCQual :: CQual -> String
renderCQual :: CQual -> String
renderCQual CQual
Inline = String
"inline"
renderCFDecl :: CFunDecl Identity -> String
renderCFDecl :: CFunDecl Identity -> String
renderCFDecl (CFunDecl CType Identity
typ CName Identity
fname [(CType Identity, CName Identity)]
args) =
CType Identity -> String
renderCType CType Identity
typ String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
fname String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" ( " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " (((CType Identity, CName Identity) -> String)
-> [(CType Identity, CName Identity)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (CType Identity, CName Identity) -> String
mkArgStr [(CType Identity, CName Identity)]
args) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" )"
where
mkArgStr :: (CType Identity, CName Identity) -> String
mkArgStr (CType Identity
t, CName Identity
a) = CType Identity -> String
renderCType CType Identity
t String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
a
renderCVDecl :: CVarDecl Identity -> String
renderCVDecl :: CVarDecl Identity -> String
renderCVDecl (CVarDecl CType Identity
typ CName Identity
vname) = CType Identity -> String
renderCType CType Identity
typ String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
vname
renderCStmt :: CStatement Identity -> String
renderCStmt :: CStatement Identity -> String
renderCStmt (UsingNamespace (NS String
ns)) = String
"using namespace " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
ns String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
";"
renderCStmt (TypeDef CType Identity
typ CName Identity
n) = String
"typedef " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CType Identity -> String
renderCType CType Identity
typ String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
n String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
";"
renderCStmt (CExpSA CExp Identity
e) = CExp Identity -> String
renderCExp CExp Identity
e String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
";"
renderCStmt (CDeclaration CFunDecl Identity
e) = CFunDecl Identity -> String
renderCFDecl CFunDecl Identity
e String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
";"
renderCStmt (CDefinition Maybe CQual
mq CFunDecl Identity
d [CStatement Identity]
body) =
String -> (CQual -> String) -> Maybe CQual -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"" (\CQual
q -> CQual -> String
renderCQual CQual
q String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" ") Maybe CQual
mq
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CFunDecl Identity -> String
renderCFDecl CFunDecl Identity
d
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" {\n"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (CStatement Identity -> String) -> [CStatement Identity] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CStatement Identity -> String
renderCStmt [CStatement Identity]
body
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n}\n"
renderCStmt (CInit CVarDecl Identity
d CExp Identity
e) = CVarDecl Identity -> String
renderCVDecl CVarDecl Identity
d String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"=" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CExp Identity -> String
renderCExp CExp Identity
e String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
";"
renderCStmt (CReturn CExp Identity
e) = String
"return " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CExp Identity -> String
renderCExp CExp Identity
e String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
";"
renderCStmt (CDelete CExp Identity
e) = String
"delete " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CExp Identity -> String
renderCExp CExp Identity
e String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
";"
renderCStmt (CMacroApp CName Identity
n [CName Identity]
as) =
CName Identity -> String
renderCName CName Identity
n
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"("
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CName Identity -> String) -> [CName Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CName Identity -> String
renderCName [CName Identity]
as)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
")"
renderCStmt (CExtern [CStatement Identity]
body) =
String
"extern \"C\" {\n"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> (CStatement Identity -> String) -> [CStatement Identity] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CStatement Identity -> String
renderCStmt [CStatement Identity]
body
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"}\n"
renderCStmt (Comment String
str) = String
"// " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
str String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n"
renderCStmt CStatement Identity
CEmptyLine = String
"\n"
renderCStmt (CVerbatim String
str) = String
str
renderCStmtInMacro :: CStatement Identity -> [String]
renderCStmtInMacro :: CStatement Identity -> [String]
renderCStmtInMacro (Comment String
_str) = [String
""]
renderCStmtInMacro CStatement Identity
CEmptyLine = [String
""]
renderCStmtInMacro (CVerbatim String
str) = String -> [String]
lines String
str
renderCStmtInMacro CStatement Identity
s = String -> [String]
lines (CStatement Identity -> String
renderCStmt CStatement Identity
s)
renderCMacro :: CMacro Identity -> String
renderCMacro :: CMacro Identity -> String
renderCMacro (CRegular CStatement Identity
stmt) = CStatement Identity -> String
renderCStmt CStatement Identity
stmt
renderCMacro (Include (HdrName String
hdr)) = String
"\n#include \"" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
hdr String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\"\n"
renderCMacro (Pragma PragmaParam
param) = String
"\n#pragma " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> PragmaParam -> String
renderPragmaParam PragmaParam
param String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n"
renderCMacro (Undef CName Identity
n) = String
"\n#undef " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
n String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n"
renderCMacro (Define CName Identity
m [CName Identity]
ts [CStatement Identity]
stmts) =
String
"\n#define " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> CName Identity -> String
renderCName CName Identity
m
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> case [CName Identity]
ts of
[] -> String
" "
[CName Identity]
_ -> String
"(" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
", " ((CName Identity -> String) -> [CName Identity] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map CName Identity -> String
renderCName [CName Identity]
ts) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
") \\\n"
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"\\\n" ((CStatement Identity -> [String])
-> [CStatement Identity] -> [String]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CStatement Identity -> [String]
renderCStmtInMacro [CStatement Identity]
stmts)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"\n"
renderCMacro CMacro Identity
EmptyLine = String
"\n"
renderCMacro (Verbatim String
str) = String
str
renderBlock :: CBlock Identity -> String
renderBlock :: CBlock Identity -> String
renderBlock (ExternC [CMacro Identity]
ms) =
String
"\n#ifdef __cplusplus\n\
\extern \"C\" {\n\
\#endif\n"
String -> ShowS
forall a. [a] -> [a] -> [a]
++ (CMacro Identity -> String) -> [CMacro Identity] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CMacro Identity -> String
renderCMacro [CMacro Identity]
ms
String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"\n#ifdef __cplusplus\n\
\}\n\
\#endif\n"