module GenBind (expandHooks)
where
import Data.Char (toUpper, toLower, isSpace)
import Data.List (deleteBy, intersperse, isPrefixOf, find, nubBy)
import Data.Maybe (isNothing, isJust, fromJust, fromMaybe)
import Control.Monad (when, unless, liftM, mapAndUnzipM)
import Data.Bits ((.&.), (.|.), xor, complement)
import Position (Position, Pos(posOf), nopos, builtinPos)
import Errors (interr, todo)
import Idents (Ident, identToLexeme, onlyPosIdent)
import Attributes (newAttrsOnlyPos)
import C2HSConfig (dlsuffix)
import C2HSState (CST, nop, errorsPresent, showErrors, fatal,
SwitchBoard(..), Traces(..), putTraceStr, getSwitch,
printCIO)
import C (AttrC, CObj(..), CTag(..), lookupDefObjC, lookupDefTagC,
CHeader(..), CExtDecl, CDecl(..), CDeclSpec(..),
CStorageSpec(..), CTypeSpec(..), CTypeQual(..),
CStructUnion(..), CStructTag(..), CEnum(..), CDeclr(..),
CInit(..), CExpr(..), CAssignOp(..), CBinaryOp(..),
CUnaryOp(..), CConst (..),
CT, readCT, transCT, getCHeaderCT, runCT, ifCTExc,
raiseErrorCTExc, findValueObj, findFunObj, findTag,
findTypeObj, applyPrefixToNameSpaces, isTypedef,
simplifyDecl, declrFromDecl, declrNamed, structMembers,
structName, tagName, declaredName , structFromDecl,
funResultAndArgs, chaseDecl, findAndChaseDecl,
findObjShadow,
checkForAlias, checkForOneAliasName, lookupEnum,
lookupStructUnion, lookupDeclOrTag, isPtrDeclr,
isArrDeclr, dropPtrDeclr, isPtrDecl, getDeclOf, isFunDeclr,
refersToNewDef, CDef(..))
import CHS (CHSModule(..), CHSFrag(..), CHSHook(..), CHSTrans(..),
CHSParm(..), CHSArg(..), CHSAccess(..), CHSAPath(..),
CHSPtrType(..), showCHSParm)
import CInfo (CPrimType(..), size, alignment, bitfieldIntSigned,
bitfieldAlignment)
import GBMonad (TransFun, transTabToTransFun, HsObject(..), GB, HsPtrRep,
initialGBState, setContext, getPrefix, getLock,
delayCode, getDelayedCode, ptrMapsTo, queryPtr, objIs,
queryObj, queryClass, queryPointer, mergeMaps, dumpMaps)
lookupDftMarshIn :: String -> [ExtType] -> GB (Maybe (Ident, CHSArg))
lookupDftMarshIn :: String -> [ExtType] -> GB (Maybe (Ident, CHSArg))
lookupDftMarshIn String
"Bool" [PrimET CPrimType
pt] | CPrimType -> Bool
isIntegralCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
cFromBoolIde, CHSArg
CHSValArg)
lookupDftMarshIn String
hsTy [PrimET CPrimType
pt] | String -> Bool
isIntegralHsType String
hsTy
Bool -> Bool -> Bool
&&CPrimType -> Bool
isIntegralCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
cIntConvIde, CHSArg
CHSValArg)
lookupDftMarshIn String
hsTy [PrimET CPrimType
pt] | String -> Bool
isFloatHsType String
hsTy
Bool -> Bool -> Bool
&&CPrimType -> Bool
isFloatCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
cFloatConvIde, CHSArg
CHSValArg)
lookupDftMarshIn String
"String" [PtrET (PrimET CPrimType
CCharPT)] =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
withCStringIde, CHSArg
CHSIOArg)
lookupDftMarshIn String
"String" [PtrET (PrimET CPrimType
CCharPT), PrimET CPrimType
pt]
| CPrimType -> Bool
isIntegralCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
withCStringLenIde, CHSArg
CHSIOArg)
lookupDftMarshIn String
hsTy [PtrET ExtType
ty] | ExtType -> String
showExtType ExtType
ty String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
hsTy =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
withIde, CHSArg
CHSIOArg)
lookupDftMarshIn String
hsTy [PtrET (PrimET CPrimType
pt)]
| String -> Bool
isIntegralHsType String
hsTy Bool -> Bool -> Bool
&& CPrimType -> Bool
isIntegralCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
withIntConvIde, CHSArg
CHSIOArg)
lookupDftMarshIn String
hsTy [PtrET (PrimET CPrimType
pt)]
| String -> Bool
isFloatHsType String
hsTy Bool -> Bool -> Bool
&& CPrimType -> Bool
isFloatCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
withFloatConvIde, CHSArg
CHSIOArg)
lookupDftMarshIn String
"Bool" [PtrET (PrimET CPrimType
pt)]
| CPrimType -> Bool
isIntegralCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
withFromBoolIde, CHSArg
CHSIOArg)
lookupDftMarshIn String
_ [ExtType]
_ =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Ident, CHSArg)
forall a. Maybe a
Nothing
lookupDftMarshOut :: String -> [ExtType] -> GB (Maybe (Ident, CHSArg))
lookupDftMarshOut :: String -> [ExtType] -> GB (Maybe (Ident, CHSArg))
lookupDftMarshOut String
"()" [ExtType]
_ =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
voidIde, CHSArg
CHSVoidArg)
lookupDftMarshOut String
"Bool" [PrimET CPrimType
pt] | CPrimType -> Bool
isIntegralCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
cToBoolIde, CHSArg
CHSValArg)
lookupDftMarshOut String
hsTy [PrimET CPrimType
pt] | String -> Bool
isIntegralHsType String
hsTy
Bool -> Bool -> Bool
&&CPrimType -> Bool
isIntegralCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
cIntConvIde, CHSArg
CHSValArg)
lookupDftMarshOut String
hsTy [PrimET CPrimType
pt] | String -> Bool
isFloatHsType String
hsTy
Bool -> Bool -> Bool
&&CPrimType -> Bool
isFloatCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
cFloatConvIde, CHSArg
CHSValArg)
lookupDftMarshOut String
"String" [PtrET (PrimET CPrimType
CCharPT)] =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
peekCStringIde, CHSArg
CHSIOArg)
lookupDftMarshOut String
"String" [PtrET (PrimET CPrimType
CCharPT), PrimET CPrimType
pt]
| CPrimType -> Bool
isIntegralCPrimType CPrimType
pt =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
peekCStringLenIde, CHSArg
CHSIOArg)
lookupDftMarshOut String
hsTy [PtrET ExtType
ty] | ExtType -> String
showExtType ExtType
ty String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
hsTy =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg)))
-> Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a b. (a -> b) -> a -> b
$ (Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (Ident
peekIde, CHSArg
CHSIOArg)
lookupDftMarshOut String
_ [ExtType]
_ =
Maybe (Ident, CHSArg) -> GB (Maybe (Ident, CHSArg))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Ident, CHSArg)
forall a. Maybe a
Nothing
isIntegralHsType :: String -> Bool
isIntegralHsType :: String -> Bool
isIntegralHsType String
"Int" = Bool
True
isIntegralHsType String
"Int8" = Bool
True
isIntegralHsType String
"Int16" = Bool
True
isIntegralHsType String
"Int32" = Bool
True
isIntegralHsType String
"Int64" = Bool
True
isIntegralHsType String
"Word8" = Bool
True
isIntegralHsType String
"Word16" = Bool
True
isIntegralHsType String
"Word32" = Bool
True
isIntegralHsType String
"Word64" = Bool
True
isIntegralHsType String
_ = Bool
False
isFloatHsType :: String -> Bool
isFloatHsType :: String -> Bool
isFloatHsType String
"Float" = Bool
True
isFloatHsType String
"Double" = Bool
True
isFloatHsType String
_ = Bool
False
isIntegralCPrimType :: CPrimType -> Bool
isIntegralCPrimType :: CPrimType -> Bool
isIntegralCPrimType = (CPrimType -> [CPrimType] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [CPrimType
CCharPT, CPrimType
CSCharPT, CPrimType
CIntPT, CPrimType
CShortPT, CPrimType
CLongPT,
CPrimType
CLLongPT, CPrimType
CUIntPT, CPrimType
CUCharPT, CPrimType
CUShortPT,
CPrimType
CULongPT, CPrimType
CULLongPT])
isFloatCPrimType :: CPrimType -> Bool
isFloatCPrimType :: CPrimType -> Bool
isFloatCPrimType = (CPrimType -> [CPrimType] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [CPrimType
CFloatPT, CPrimType
CDoublePT, CPrimType
CLDoublePT])
voidIde :: Ident
voidIde = String -> Ident
noPosIdent String
"void"
cFromBoolIde :: Ident
cFromBoolIde = String -> Ident
noPosIdent String
"cFromBool"
cToBoolIde :: Ident
cToBoolIde = String -> Ident
noPosIdent String
"cToBool"
cIntConvIde :: Ident
cIntConvIde = String -> Ident
noPosIdent String
"cIntConv"
cFloatConvIde :: Ident
cFloatConvIde = String -> Ident
noPosIdent String
"cFloatConv"
withIde :: Ident
withIde = String -> Ident
noPosIdent String
"with"
withCStringIde :: Ident
withCStringIde = String -> Ident
noPosIdent String
"withCString"
withCStringLenIde :: Ident
withCStringLenIde = String -> Ident
noPosIdent String
"withCStringLenIntConv"
withIntConvIde :: Ident
withIntConvIde = String -> Ident
noPosIdent String
"withIntConv"
withFloatConvIde :: Ident
withFloatConvIde = String -> Ident
noPosIdent String
"withFloatConv"
withFromBoolIde :: Ident
withFromBoolIde = String -> Ident
noPosIdent String
"withFromBoolConv"
peekIde :: Ident
peekIde = String -> Ident
noPosIdent String
"peek"
peekCStringIde :: Ident
peekCStringIde = String -> Ident
noPosIdent String
"peekCString"
peekCStringLenIde :: Ident
peekCStringLenIde = String -> Ident
noPosIdent String
"peekCStringLenIntConv"
expandHooks :: AttrC -> CHSModule -> CST s (CHSModule, String, String)
expandHooks :: forall s. AttrC -> CHSModule -> CST s (CHSModule, String, String)
expandHooks AttrC
ac CHSModule
mod = do
mLock <- (SwitchBoard -> Maybe String) -> CST s (Maybe String)
forall a s. (SwitchBoard -> a) -> CST s a
getSwitch SwitchBoard -> Maybe String
lockFunSB
(_, res) <- runCT (expandModule mod) ac (initialGBState mLock)
return res
expandModule :: CHSModule -> GB (CHSModule, String, String)
expandModule :: CHSModule -> CT GBState (CHSModule, String, String)
expandModule (CHSModule [CHSFrag]
frags) =
do
CST (CState GBState) ()
forall {s}. CST s ()
traceInfoExpand
frags' <- [CHSFrag] -> GB [CHSFrag]
expandFrags [CHSFrag]
frags
delayedFrags <- getDelayedCode
chi <- dumpMaps
errs <- errorsPresent
if errs
then do
traceInfoErr
errmsgs <- showErrors
fatal ("Errors during expansion of binding hooks:\n\n"
++ errmsgs)
else do
traceInfoOK
warnmsgs <- showErrors
return (CHSModule (frags' ++ delayedFrags), chi, warnmsgs)
where
traceInfoExpand :: CST s ()
traceInfoExpand = (Traces -> Bool) -> String -> CST s ()
forall s. (Traces -> Bool) -> String -> CST s ()
putTraceStr Traces -> Bool
tracePhasesSW
(String
"...expanding binding hooks...\n")
traceInfoErr :: CST s ()
traceInfoErr = (Traces -> Bool) -> String -> CST s ()
forall s. (Traces -> Bool) -> String -> CST s ()
putTraceStr Traces -> Bool
tracePhasesSW
(String
"...error(s) detected.\n")
traceInfoOK :: CST s ()
traceInfoOK = (Traces -> Bool) -> String -> CST s ()
forall s. (Traces -> Bool) -> String -> CST s ()
putTraceStr Traces -> Bool
tracePhasesSW
(String
"...successfully completed.\n")
expandFrags :: [CHSFrag] -> GB [CHSFrag]
expandFrags :: [CHSFrag] -> GB [CHSFrag]
expandFrags = ([[CHSFrag]] -> [CHSFrag])
-> PreCST SwitchBoard (CState GBState) [[CHSFrag]] -> GB [CHSFrag]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [[CHSFrag]] -> [CHSFrag]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (PreCST SwitchBoard (CState GBState) [[CHSFrag]] -> GB [CHSFrag])
-> ([CHSFrag] -> PreCST SwitchBoard (CState GBState) [[CHSFrag]])
-> [CHSFrag]
-> GB [CHSFrag]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CHSFrag -> GB [CHSFrag])
-> [CHSFrag] -> PreCST SwitchBoard (CState GBState) [[CHSFrag]]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM CHSFrag -> GB [CHSFrag]
expandFrag
expandFrag :: CHSFrag -> GB [CHSFrag]
expandFrag :: CHSFrag -> GB [CHSFrag]
expandFrag verb :: CHSFrag
verb@(CHSVerb String
_ Position
_ ) = [CHSFrag] -> GB [CHSFrag]
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return [CHSFrag
verb]
expandFrag line :: CHSFrag
line@(CHSLine Position
_ ) = [CHSFrag] -> GB [CHSFrag]
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return [CHSFrag
line]
expandFrag prag :: CHSFrag
prag@(CHSLang [String]
_ Position
_ ) = [CHSFrag] -> GB [CHSFrag]
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return [CHSFrag
prag]
expandFrag (CHSHook CHSHook
h ) =
do
code <- CHSHook -> GB String
expandHook CHSHook
h
return [CHSVerb code builtinPos]
GB [CHSFrag] -> GB [CHSFrag] -> GB [CHSFrag]
forall s a. CT s a -> CT s a -> CT s a
`ifCTExc` [CHSFrag] -> GB [CHSFrag]
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return [String -> Position -> CHSFrag
CHSVerb String
"** ERROR **" Position
builtinPos]
expandFrag (CHSCPP String
s Position
_ ) =
String -> GB [CHSFrag]
forall a. String -> a
interr (String -> GB [CHSFrag]) -> String -> GB [CHSFrag]
forall a b. (a -> b) -> a -> b
$ String
"GenBind.expandFrag: Left over CHSCPP!\n---\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n---"
expandFrag (CHSC String
s Position
_ ) =
String -> GB [CHSFrag]
forall a. String -> a
interr (String -> GB [CHSFrag]) -> String -> GB [CHSFrag]
forall a b. (a -> b) -> a -> b
$ String
"GenBind.expandFrag: Left over CHSC!\n---\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n---"
expandFrag (CHSCond [(Ident, [CHSFrag])]
alts Maybe [CHSFrag]
dft) =
do
CST (CState GBState) ()
traceInfoCond
[(Ident, [CHSFrag])] -> GB [CHSFrag]
select [(Ident, [CHSFrag])]
alts
where
select :: [(Ident, [CHSFrag])] -> GB [CHSFrag]
select [] = do
Maybe [CHSFrag] -> CST (CState GBState) ()
forall {a}. Maybe a -> CST (CState GBState) ()
traceInfoDft Maybe [CHSFrag]
dft
[CHSFrag] -> GB [CHSFrag]
expandFrags ([CHSFrag]
-> ([CHSFrag] -> [CHSFrag]) -> Maybe [CHSFrag] -> [CHSFrag]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] [CHSFrag] -> [CHSFrag]
forall a. a -> a
id Maybe [CHSFrag]
dft)
select ((Ident
ide, [CHSFrag]
frags):[(Ident, [CHSFrag])]
alts) = do
oobj <- Ident -> CT GBState (Maybe CTag)
forall s. Ident -> CT s (Maybe CTag)
findTag Ident
ide
traceInfoVal ide oobj
if isNothing oobj
then
select alts
else
expandFrags frags
traceInfoCond :: CST (CState GBState) ()
traceInfoCond = String -> CST (CState GBState) ()
traceGenBind String
"** CPP conditional:\n"
traceInfoVal :: Ident -> Maybe a -> CST (CState GBState) ()
traceInfoVal Ident
ide Maybe a
oobj = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$ Ident -> String
identToLexeme Ident
ide String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" is " String -> String -> String
forall a. [a] -> [a] -> [a]
++
(if Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing Maybe a
oobj then String
"not " else String
"") String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"defined.\n"
traceInfoDft :: Maybe a -> CST (CState GBState) ()
traceInfoDft Maybe a
dft = if Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing Maybe a
dft
then
() -> CST (CState GBState) ()
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else
String -> CST (CState GBState) ()
traceGenBind String
"Choosing else branch.\n"
expandHook :: CHSHook -> GB String
expandHook :: CHSHook -> GB String
expandHook (CHSImport Bool
qual Ident
ide String
chi Position
_) =
do
String -> CST (CState GBState) ()
mergeMaps String
chi
String -> GB String
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> GB String) -> String -> GB String
forall a b. (a -> b) -> a -> b
$
String
"import " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (if Bool
qual then String
"qualified " else String
"") String -> String -> String
forall a. [a] -> [a] -> [a]
++ Ident -> String
identToLexeme Ident
ide
expandHook (CHSContext Maybe String
olib Maybe String
oprefix Maybe String
olock Position
_) =
do
Maybe String
-> Maybe String -> Maybe String -> CST (CState GBState) ()
setContext Maybe String
olib Maybe String
oprefix Maybe String
olock
(String -> CST (CState GBState) ())
-> Maybe String -> CST (CState GBState) ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> Maybe a -> m ()
mapMaybeM_ String -> CST (CState GBState) ()
forall s. String -> CT s ()
applyPrefixToNameSpaces Maybe String
oprefix
String -> GB String
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return String
""
expandHook (CHSType Ident
ide Position
pos) =
do
CST (CState GBState) ()
traceInfoType
decl <- Ident -> Bool -> Bool -> CT GBState CDecl
forall s. Ident -> Bool -> Bool -> CT s CDecl
findAndChaseDecl Ident
ide Bool
False Bool
True
ty <- extractSimpleType pos decl
traceInfoDump decl ty
return $ "(" ++ showExtType ty ++ ")"
where
traceInfoType :: CST (CState GBState) ()
traceInfoType = String -> CST (CState GBState) ()
traceGenBind String
"** Type hook:\n"
traceInfoDump :: a -> ExtType -> CST (CState GBState) ()
traceInfoDump a
decl ExtType
ty = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"Declaration\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
decl String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\ntranslates to\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExtType -> String
showExtType ExtType
ty String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
expandHook (CHSSizeof Ident
ide Position
pos) =
do
CST (CState GBState) ()
traceInfoSizeof
decl <- Ident -> Bool -> Bool -> CT GBState CDecl
forall s. Ident -> Bool -> Bool -> CT s CDecl
findAndChaseDecl Ident
ide Bool
False Bool
True
(size, _) <- sizeAlignOf decl
traceInfoDump decl size
return $ show (fromIntegral . padBits $ size)
where
traceInfoSizeof :: CST (CState GBState) ()
traceInfoSizeof = String -> CST (CState GBState) ()
traceGenBind String
"** Sizeof hook:\n"
traceInfoDump :: a -> BitSize -> CST (CState GBState) ()
traceInfoDump a
decl BitSize
size = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"Size of declaration\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
decl String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\nis "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer) -> (BitSize -> Int) -> BitSize -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BitSize -> Int
padBits (BitSize -> Integer) -> BitSize -> Integer
forall a b. (a -> b) -> a -> b
$ BitSize
size) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
expandHook (CHSEnum Ident
cide Maybe Ident
oalias CHSTrans
chsTrans Maybe String
oprefix [Ident]
derive Position
_) =
do
enum <- Ident -> Bool -> CT GBState CEnum
forall s. Ident -> Bool -> CT s CEnum
lookupEnum Ident
cide Bool
True
gprefix <- getPrefix
let prefix = String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
gprefix Maybe String
oprefix
trans = String -> CHSTrans -> Ident -> String
transTabToTransFun String
prefix CHSTrans
chsTrans
hide = Ident -> String
identToLexeme (Ident -> String)
-> (Maybe Ident -> Ident) -> Maybe Ident -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ident -> Maybe Ident -> Ident
forall a. a -> Maybe a -> a
fromMaybe Ident
cide (Maybe Ident -> String) -> Maybe Ident -> String
forall a b. (a -> b) -> a -> b
$ Maybe Ident
oalias
enumDef enum hide trans (map identToLexeme derive)
expandHook hook :: CHSHook
hook@(CHSCall Bool
isPure Bool
isUns Bool
isNol Ident
ide Maybe Ident
oalias Position
pos) =
do
CST (CState GBState) ()
traceEnter
(ObjCO cdecl, ide) <- Ident -> Bool -> CT GBState (CObj, Ident)
forall s. Ident -> Bool -> CT s (CObj, Ident)
findFunObj Ident
ide Bool
True
mLock <- if isNol then return Nothing else getLock
let ideLexeme = Ident -> String
identToLexeme Ident
ide
hsLexeme = String
ideLexeme String -> (Ident -> String) -> Maybe Ident -> String
forall b a. b -> (a -> b) -> Maybe a -> b
`maybe` Ident -> String
identToLexeme (Maybe Ident -> String) -> Maybe Ident -> String
forall a b. (a -> b) -> a -> b
$ Maybe Ident
oalias
cdecl' = Ident
ide Ident -> CDecl -> CDecl
`simplifyDecl` CDecl
cdecl
callImport hook isPure isUns mLock ideLexeme hsLexeme cdecl' pos
where
traceEnter :: CST (CState GBState) ()
traceEnter = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"** Call hook for `" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Ident -> String
identToLexeme Ident
ide String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"':\n"
expandHook hook :: CHSHook
hook@(CHSFun Bool
isPure Bool
isUns Bool
isNol Ident
ide Maybe Ident
oalias Maybe String
ctxt [CHSParm]
parms CHSParm
parm Position
pos) =
do
CST (CState GBState) ()
traceEnter
(ObjCO cdecl, cide) <- Ident -> Bool -> CT GBState (CObj, Ident)
forall s. Ident -> Bool -> CT s (CObj, Ident)
findFunObj Ident
ide Bool
True
mLock <- if isNol then return Nothing else getLock
let ideLexeme = Ident -> String
identToLexeme Ident
ide
hsLexeme = String
ideLexeme String -> (Ident -> String) -> Maybe Ident -> String
forall b a. b -> (a -> b) -> Maybe a -> b
`maybe` Ident -> String
identToLexeme (Maybe Ident -> String) -> Maybe Ident -> String
forall a b. (a -> b) -> a -> b
$ Maybe Ident
oalias
fiLexeme = String
hsLexeme String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'_"
fiIde = Position -> String -> Ident
onlyPosIdent Position
nopos String
fiLexeme
cdecl' = Ident
cide Ident -> CDecl -> CDecl
`simplifyDecl` CDecl
cdecl
callHook = Bool -> Bool -> Bool -> Ident -> Maybe Ident -> Position -> CHSHook
CHSCall Bool
isPure Bool
isUns Bool
isNol Ident
cide (Ident -> Maybe Ident
forall a. a -> Maybe a
Just Ident
fiIde) Position
pos
callImport callHook isPure isUns mLock (identToLexeme cide) fiLexeme cdecl' pos
funDef isPure hsLexeme fiLexeme cdecl' ctxt mLock parms parm pos
where
traceEnter :: CST (CState GBState) ()
traceEnter = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"** Fun hook for `" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Ident -> String
identToLexeme Ident
ide String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"':\n"
expandHook (CHSField CHSAccess
access CHSAPath
path Position
pos) =
do
CST (CState GBState) ()
traceInfoField
(decl, offsets) <- CHSAPath -> GB (CDecl, [BitSize])
accessPath CHSAPath
path
traceDepth offsets
ty <- extractSimpleType pos decl
traceValueType ty
setGet pos access offsets ty
where
accessString :: String
accessString = case CHSAccess
access of
CHSAccess
CHSGet -> String
"Get"
CHSAccess
CHSSet -> String
"Set"
traceInfoField :: CST (CState GBState) ()
traceInfoField = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$ String
"** " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
accessString String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" hook:\n"
traceDepth :: t a -> CST (CState GBState) ()
traceDepth t a
offsets = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$ String
"Depth of access path: "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (t a -> Int
forall a. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
offsets) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
traceValueType :: ExtType -> CST (CState GBState) ()
traceValueType ExtType
et = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"Type of accessed value: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExtType -> String
showExtType ExtType
et String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
expandHook (CHSPointer Bool
isStar Ident
cName Maybe Ident
oalias CHSPtrType
ptrKind Bool
isNewtype Maybe Ident
oRefType Position
pos) =
do
CST (CState GBState) ()
traceInfoPointer
let hsIde :: Ident
hsIde = Ident -> Maybe Ident -> Ident
forall a. a -> Maybe a -> a
fromMaybe Ident
cName Maybe Ident
oalias
hsName :: String
hsName = Ident -> String
identToLexeme Ident
hsIde
Ident
hsIde Ident -> HsObject -> CST (CState GBState) ()
`objIs` CHSPtrType -> Bool -> HsObject
Pointer CHSPtrType
ptrKind Bool
isNewtype
declOrTag <- Ident -> Bool -> CT GBState (Either CDecl CTag)
forall s. Ident -> Bool -> CT s (Either CDecl CTag)
lookupDeclOrTag Ident
cName Bool
True
case declOrTag of
Left CDecl
cdecl -> do
cNameFull <- case CDecl -> Maybe Ident
declaredName CDecl
cdecl of
Just Ident
ide -> Ident -> PreCST SwitchBoard (CState GBState) Ident
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return Ident
ide
Maybe Ident
Nothing -> String -> PreCST SwitchBoard (CState GBState) Ident
forall a. String -> a
interr
String
"GenBind.expandHook: Where is the name?"
cNameFull `refersToNewDef` ObjCD (TypeCO cdecl)
traceInfoCName "declaration" cNameFull
unless (isStar || isPtrDecl cdecl) $
ptrExpectedErr (posOf cName)
(hsType, isFun) <-
case oRefType of
Maybe Ident
Nothing -> do
cDecl <- Ident -> Bool -> CT GBState CDecl
forall s. Ident -> Bool -> CT s CDecl
chaseDecl Ident
cNameFull (Bool -> Bool
not Bool
isStar)
et <- extractPtrType cDecl
let et' = Bool -> ExtType -> ExtType
adjustPtr Bool
isStar ExtType
et
return (showExtType et', isFunExtType et')
Just Ident
hsType -> (String, Bool)
-> PreCST SwitchBoard (CState GBState) (String, Bool)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Ident -> String
identToLexeme Ident
hsType, Bool
False)
traceInfoHsType hsName hsType
realCName <- liftM (maybe cName snd) $ findObjShadow cName
pointerDef isStar realCName hsName ptrKind isNewtype hsType isFun
Right CTag
tag -> do
let cNameFull :: Ident
cNameFull = CTag -> Ident
tagName CTag
tag
String -> Ident -> CST (CState GBState) ()
traceInfoCName String
"tag definition" Ident
cNameFull
Bool -> CST (CState GBState) () -> CST (CState GBState) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
isStar (CST (CState GBState) () -> CST (CState GBState) ())
-> CST (CState GBState) () -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
Position -> CST (CState GBState) ()
forall a. Position -> GB a
ptrExpectedErr (Ident -> Position
forall a. Pos a => a -> Position
posOf Ident
cName)
let hsType :: String
hsType = case Maybe Ident
oRefType of
Maybe Ident
Nothing -> String
"()"
Just Ident
hsType -> Ident -> String
identToLexeme Ident
hsType
String -> String -> CST (CState GBState) ()
traceInfoHsType String
hsName String
hsType
Bool
-> Ident
-> String
-> CHSPtrType
-> Bool
-> String
-> Bool
-> GB String
pointerDef Bool
isStar Ident
cNameFull String
hsName CHSPtrType
ptrKind Bool
isNewtype String
hsType Bool
False
where
adjustPtr :: Bool -> ExtType -> ExtType
adjustPtr Bool
True ExtType
et = ExtType
et
adjustPtr Bool
False (PtrET ExtType
et) = ExtType
et
adjustPtr Bool
_ ExtType
_ = String -> ExtType
forall a. String -> a
interr String
"GenBind.adjustPtr: Where is the Ptr?"
traceInfoPointer :: CST (CState GBState) ()
traceInfoPointer = String -> CST (CState GBState) ()
traceGenBind String
"** Pointer hook:\n"
traceInfoCName :: String -> Ident -> CST (CState GBState) ()
traceInfoCName String
kind Ident
ide = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"found C " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
kind String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" for `" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Ident -> String
identToLexeme Ident
ide String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'\n"
traceInfoHsType :: String -> String -> CST (CState GBState) ()
traceInfoHsType String
name String
ty = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"associated with Haskell entity `" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
name String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'\nhaving type " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ty
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
expandHook (CHSClass Maybe Ident
oclassIde Ident
classIde Ident
typeIde Position
pos) =
do
CST (CState GBState) ()
traceInfoClass
Ident
classIde Ident -> HsObject -> CST (CState GBState) ()
`objIs` Maybe Ident -> Ident -> HsObject
Class Maybe Ident
oclassIde Ident
typeIde
superClasses <- Maybe Ident -> GB [(String, String, HsObject)]
collectClasses Maybe Ident
oclassIde
Pointer ptrType isNewtype <- queryPointer typeIde
when (ptrType == CHSStablePtr) $
illegalStablePtrErr pos
classDef pos (identToLexeme classIde) (identToLexeme typeIde)
ptrType isNewtype superClasses
where
collectClasses :: Maybe Ident -> GB [(String, String, HsObject)]
collectClasses :: Maybe Ident -> GB [(String, String, HsObject)]
collectClasses Maybe Ident
Nothing = [(String, String, HsObject)] -> GB [(String, String, HsObject)]
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return []
collectClasses (Just Ident
ide) =
do
Class oclassIde typeIde <- Ident -> GB HsObject
queryClass Ident
ide
ptr <- queryPointer typeIde
classes <- collectClasses oclassIde
return $ (identToLexeme ide, identToLexeme typeIde, ptr) : classes
traceInfoClass :: CST (CState GBState) ()
traceInfoClass = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$ String
"** Class hook:\n"
enumDef :: CEnum -> String -> TransFun -> [String] -> GB String
enumDef :: CEnum -> String -> (Ident -> String) -> [String] -> GB String
enumDef cenum :: CEnum
cenum@(CEnum Maybe Ident
_ [(Ident, Maybe CExpr)]
list Attrs
_) String
hident Ident -> String
trans [String]
userDerive =
do
(list', enumAuto) <- [(Ident, Maybe CExpr)]
-> PreCST
SwitchBoard (CState GBState) ([(Ident, Maybe CExpr)], Bool)
forall {a}.
[(a, Maybe CExpr)]
-> PreCST SwitchBoard (CState GBState) ([(a, Maybe CExpr)], Bool)
evalTagVals [(Ident, Maybe CExpr)]
list
let enumVals = [(Ident -> String
trans Ident
ide, Maybe CExpr
cexpr) | (Ident
ide, Maybe CExpr
cexpr) <- [(Ident, Maybe CExpr)]
list']
defHead = String -> String
enumHead String
hident
defBody = Int -> [(String, Maybe CExpr)] -> String
enumBody (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
defHead Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2) [(String, Maybe CExpr)]
enumVals
inst = [String] -> String
makeDerives
(if Bool
enumAuto then String
"Enum" String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
userDerive else [String]
userDerive) String -> String -> String
forall a. [a] -> [a] -> [a]
++
if Bool
enumAuto then String
"\n" else String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> [(String, Maybe CExpr)] -> String
enumInst String
hident [(String, Maybe CExpr)]
enumVals
return $ defHead ++ defBody ++ inst
where
cpos :: Position
cpos = CEnum -> Position
forall a. Pos a => a -> Position
posOf CEnum
cenum
evalTagVals :: [(a, Maybe CExpr)]
-> PreCST SwitchBoard (CState GBState) ([(a, Maybe CExpr)], Bool)
evalTagVals [] = ([(a, Maybe CExpr)], Bool)
-> PreCST SwitchBoard (CState GBState) ([(a, Maybe CExpr)], Bool)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([], Bool
True)
evalTagVals ((a
ide, Maybe CExpr
Nothing ):[(a, Maybe CExpr)]
list) =
do
(list', derived) <- [(a, Maybe CExpr)]
-> PreCST SwitchBoard (CState GBState) ([(a, Maybe CExpr)], Bool)
evalTagVals [(a, Maybe CExpr)]
list
return ((ide, Nothing):list', derived)
evalTagVals ((a
ide, Just CExpr
exp):[(a, Maybe CExpr)]
list) =
do
(list', derived) <- [(a, Maybe CExpr)]
-> PreCST SwitchBoard (CState GBState) ([(a, Maybe CExpr)], Bool)
evalTagVals [(a, Maybe CExpr)]
list
val <- evalConstCExpr exp
case val of
IntResult Integer
val' ->
([(a, Maybe CExpr)], Bool)
-> PreCST SwitchBoard (CState GBState) ([(a, Maybe CExpr)], Bool)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((a
ide, CExpr -> Maybe CExpr
forall a. a -> Maybe a
Just (CExpr -> Maybe CExpr) -> CExpr -> Maybe CExpr
forall a b. (a -> b) -> a -> b
$ CConst -> Attrs -> CExpr
CConst (Integer -> Attrs -> CConst
CIntConst Integer
val' Attrs
at1) Attrs
at2)(a, Maybe CExpr) -> [(a, Maybe CExpr)] -> [(a, Maybe CExpr)]
forall a. a -> [a] -> [a]
:[(a, Maybe CExpr)]
list',
Bool
False)
FloatResult Float
_ ->
Position
-> String
-> PreCST SwitchBoard (CState GBState) ([(a, Maybe CExpr)], Bool)
forall a. Position -> String -> GB a
illegalConstExprErr (CExpr -> Position
forall a. Pos a => a -> Position
posOf CExpr
exp) String
"a float result"
where
at1 :: Attrs
at1 = Position -> Attrs
newAttrsOnlyPos Position
nopos
at2 :: Attrs
at2 = Position -> Attrs
newAttrsOnlyPos Position
nopos
makeDerives :: [String] -> String
makeDerives [] = String
""
makeDerives [String]
dList = String
"deriving (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (String -> [String] -> [String]
forall a. a -> [a] -> [a]
intersperse String
"," [String]
dList) String -> String -> String
forall a. [a] -> [a] -> [a]
++String
")"
enumHead :: String -> String
enumHead :: String -> String
enumHead String
ident = String
"data " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ident String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = "
enumBody :: Int -> [(String, Maybe CExpr)] -> String
enumBody :: Int -> [(String, Maybe CExpr)] -> String
enumBody Int
indent [] = String
""
enumBody Int
indent ((String
ide, Maybe CExpr
_):[(String, Maybe CExpr)]
list) =
String
ide String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
indent Char
' '
String -> String -> String
forall a. [a] -> [a] -> [a]
++ (if [(String, Maybe CExpr)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(String, Maybe CExpr)]
list then String
"" else String
"| " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> [(String, Maybe CExpr)] -> String
enumBody Int
indent [(String, Maybe CExpr)]
list)
enumInst :: String -> [(String, Maybe CExpr)] -> String
enumInst :: String -> [(String, Maybe CExpr)] -> String
enumInst String
ident [(String, Maybe CExpr)]
list =
String
"instance Enum " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ident String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" where\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ [(String, Integer)] -> String
forall {a}. (Ord a, Num a, Show a) => [(String, a)] -> String
fromDef [(String, Integer)]
flatList String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [(String, Integer)] -> String
forall {a}. (Ord a, Num a, Show a) => [(String, a)] -> String
toDef [(String, Integer)]
flatList String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
succDef [String]
names String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
predDef [String]
names String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
enumFromToDef [String]
names
where
names :: [String]
names = ((String, Maybe CExpr) -> String)
-> [(String, Maybe CExpr)] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String, Maybe CExpr) -> String
forall a b. (a, b) -> a
fst [(String, Maybe CExpr)]
list
flatList :: [(String, Integer)]
flatList = [(String, Maybe CExpr)] -> Integer -> [(String, Integer)]
forall {a}. [(a, Maybe CExpr)] -> Integer -> [(a, Integer)]
flatten [(String, Maybe CExpr)]
list Integer
0
flatten :: [(a, Maybe CExpr)] -> Integer -> [(a, Integer)]
flatten [] Integer
n = []
flatten ((a
ide, Maybe CExpr
exp):[(a, Maybe CExpr)]
list) Integer
n = (a
ide, Integer
val) (a, Integer) -> [(a, Integer)] -> [(a, Integer)]
forall a. a -> [a] -> [a]
: [(a, Maybe CExpr)] -> Integer -> [(a, Integer)]
flatten [(a, Maybe CExpr)]
list (Integer
val Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
1)
where
val :: Integer
val = case Maybe CExpr
exp of
Maybe CExpr
Nothing -> Integer
n
Just (CConst (CIntConst Integer
m Attrs
_) Attrs
_) -> Integer
m
Just CExpr
_ -> String -> Integer
forall a. String -> a
interr String
"GenBind.enumInst: Integer constant expected!"
show' :: a -> String
show' a
x = if a
x a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< a
0 then String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")" else a -> String
forall a. Show a => a -> String
show a
x
fromDef :: [(String, a)] -> String
fromDef [(String, a)]
list = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ String
" fromEnum " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ide String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall {a}. (Ord a, Num a, Show a) => a -> String
show' a
val String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
| (String
ide, a
val) <- [(String, a)]
list
]
toDef :: [(String, a)] -> String
toDef [(String, a)]
list = [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ String
" toEnum " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall {a}. (Ord a, Num a, Show a) => a -> String
show' a
val String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ide String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
| (String
ide, a
val) <- ((String, a) -> (String, a) -> Bool)
-> [(String, a)] -> [(String, a)]
forall a. (a -> a -> Bool) -> [a] -> [a]
nubBy (\(String, a)
x (String, a)
y -> (String, a) -> a
forall a b. (a, b) -> b
snd (String, a)
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== (String, a) -> a
forall a b. (a, b) -> b
snd (String, a)
y) [(String, a)]
list
]
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" toEnum unmatched = error (\"" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ident
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
".toEnum: Cannot match \" ++ show unmatched)\n"
succDef :: [String] -> String
succDef [] = String
" succ _ = undefined\n"
succDef [String
x] = String
" succ _ = undefined\n"
succDef (String
x:String
x':[String]
xs) =
String
" succ " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
x' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
succDef (String
x'String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
xs)
predDef :: [String] -> String
predDef [] = String
" pred _ = undefined\n"
predDef [String
x] = String
" pred _ = undefined\n"
predDef (String
x:String
x':[String]
xs) =
String
" pred " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
x' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
x String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
predDef (String
x'String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
xs)
enumFromToDef :: [String] -> String
enumFromToDef [] = String
""
enumFromToDef [String]
names =
String
" enumFromTo x y | fromEnum x == fromEnum y = [ y ]\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" | otherwise = x : enumFromTo (succ x) y\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" enumFrom x = enumFromTo x " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall a. HasCallStack => [a] -> a
last [String]
names String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" enumFromThen _ _ = "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" error \"Enum "String -> String -> String
forall a. [a] -> [a] -> [a]
++String
identString -> String -> String
forall a. [a] -> [a] -> [a]
++String
": enumFromThen not implemented\"\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" enumFromThenTo _ _ _ = "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" error \"Enum "String -> String -> String
forall a. [a] -> [a] -> [a]
++String
identString -> String -> String
forall a. [a] -> [a] -> [a]
++String
": enumFromThenTo not implemented\"\n"
callImport :: CHSHook -> Bool -> Bool -> Maybe String -> String -> String
-> CDecl -> Position -> GB String
callImport :: CHSHook
-> Bool
-> Bool
-> Maybe String
-> String
-> String
-> CDecl
-> Position
-> GB String
callImport CHSHook
hook Bool
isPure Bool
isUns Maybe String
mLock String
ideLexeme String
hsLexeme CDecl
cdecl Position
pos =
do
(mHsPtrRep, extType) <- Position -> CDecl -> Bool -> GB ([Maybe HsPtrRep], ExtType)
extractFunType Position
pos CDecl
cdecl Bool
isPure
header <- getSwitch headerSB
delayCode hook (foreignImport header ideLexeme hsLexeme isUns extType)
traceFunType extType
if any isJust mHsPtrRep
then createLambdaExpr mHsPtrRep
else return funStr
where
createLambdaExpr :: [Maybe HsPtrRep] -> GB String
createLambdaExpr :: [Maybe HsPtrRep] -> GB String
createLambdaExpr [Maybe HsPtrRep]
foreignVec = String -> GB String
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> GB String) -> String -> GB String
forall a b. (a -> b) -> a -> b
$
String
"(\\" String -> String -> String
forall a. [a] -> [a] -> [a]
++
[String] -> String
unwords ((Maybe HsPtrRep -> Integer -> String)
-> [Maybe HsPtrRep] -> [Integer] -> [String]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Maybe HsPtrRep -> Integer -> String
forall {a} {a} {b} {d}.
Show a =>
Maybe (a, b, Maybe String, d) -> a -> String
wrPattern [Maybe HsPtrRep]
foreignVec [Integer
1..])String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" -> "String -> String -> String
forall a. [a] -> [a] -> [a]
++
[String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ((Maybe HsPtrRep -> Integer -> String)
-> [Maybe HsPtrRep] -> [Integer] -> [String]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Maybe HsPtrRep -> Integer -> String
forall {a} {a} {c} {d}.
Show a =>
Maybe (a, CHSPtrType, c, d) -> a -> String
wrForPtr [Maybe HsPtrRep]
foreignVec [Integer
1..])String -> String -> String
forall a. [a] -> [a] -> [a]
++String
funStrString -> String -> String
forall a. [a] -> [a] -> [a]
++String
" "String -> String -> String
forall a. [a] -> [a] -> [a]
++
[String] -> String
unwords ((Maybe HsPtrRep -> Integer -> String)
-> [Maybe HsPtrRep] -> [Integer] -> [String]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Maybe HsPtrRep -> Integer -> String
forall {a} {a} {c} {d}.
Show a =>
Maybe (a, CHSPtrType, c, d) -> a -> String
wrArg [Maybe HsPtrRep]
foreignVec [Integer
1..])String -> String -> String
forall a. [a] -> [a] -> [a]
++String
")"
wrPattern :: Maybe (a, b, Maybe String, d) -> a -> String
wrPattern (Just (a
_,b
_,Just String
con,d
_)) a
n = String
"("String -> String -> String
forall a. [a] -> [a] -> [a]
++String
conString -> String -> String
forall a. [a] -> [a] -> [a]
++String
" arg"String -> String -> String
forall a. [a] -> [a] -> [a]
++a -> String
forall a. Show a => a -> String
show a
nString -> String -> String
forall a. [a] -> [a] -> [a]
++String
")"
wrPattern Maybe (a, b, Maybe String, d)
_ a
n = String
"arg"String -> String -> String
forall a. [a] -> [a] -> [a]
++a -> String
forall a. Show a => a -> String
show a
n
wrForPtr :: Maybe (a, CHSPtrType, c, d) -> a -> String
wrForPtr (Just (a
_,CHSPtrType
CHSForeignPtr,c
_,d
_)) a
n
= String
"withForeignPtr arg"String -> String -> String
forall a. [a] -> [a] -> [a]
++a -> String
forall a. Show a => a -> String
show a
nString -> String -> String
forall a. [a] -> [a] -> [a]
++String
" $ \\argPtr"String -> String -> String
forall a. [a] -> [a] -> [a]
++a -> String
forall a. Show a => a -> String
show a
nString -> String -> String
forall a. [a] -> [a] -> [a]
++String
" ->"
wrForPtr Maybe (a, CHSPtrType, c, d)
_ a
n = String
""
wrArg :: Maybe (a, CHSPtrType, c, d) -> a -> String
wrArg (Just (a
_,CHSPtrType
CHSForeignPtr,c
_,d
_)) a
n = String
"argPtr"String -> String -> String
forall a. [a] -> [a] -> [a]
++a -> String
forall a. Show a => a -> String
show a
n
wrArg (Just (a
_,CHSPtrType
CHSStablePtr,c
_,d
_)) a
n =
String
"(castStablePtrToPtr arg"String -> String -> String
forall a. [a] -> [a] -> [a]
++a -> String
forall a. Show a => a -> String
show a
nString -> String -> String
forall a. [a] -> [a] -> [a]
++String
")"
wrArg Maybe (a, CHSPtrType, c, d)
_ a
n = String
"arg"String -> String -> String
forall a. [a] -> [a] -> [a]
++a -> String
forall a. Show a => a -> String
show a
n
funStr :: String
funStr = case Maybe String
mLock of Maybe String
Nothing -> String
hsLexeme
Just String
lockFun -> String
lockFun String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" $ " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
hsLexeme
traceFunType :: ExtType -> CST (CState GBState) ()
traceFunType ExtType
et = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"Imported function type: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExtType -> String
showExtType ExtType
et String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
foreignImport :: String -> String -> String -> Bool -> ExtType -> String
foreignImport :: String -> String -> String -> Bool -> ExtType -> String
foreignImport String
header String
ident String
hsIdent Bool
isUnsafe ExtType
ty =
String
"foreign import ccall " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
safety String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
forall a. Show a => a -> String
show String
entity String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\n " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
hsIdent String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" :: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExtType -> String
showExtType ExtType
ty String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
where
safety :: String
safety = if Bool
isUnsafe then String
"unsafe" else String
"safe"
entity :: String
entity | String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
header = String
ident
| Bool
otherwise = String
header String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ident
funDef :: Bool
-> String
-> String
-> CDecl
-> Maybe String
-> Maybe String
-> [CHSParm]
-> CHSParm
-> Position
-> GB String
funDef :: Bool
-> String
-> String
-> CDecl
-> Maybe String
-> Maybe String
-> [CHSParm]
-> CHSParm
-> Position
-> GB String
funDef Bool
isPure String
hsLexeme String
fiLexeme CDecl
cdecl Maybe String
octxt Maybe String
mLock [CHSParm]
parms CHSParm
parm Position
pos =
do
(parms', parm', isImpure) <- Position
-> [CHSParm] -> CHSParm -> CDecl -> GB ([CHSParm], CHSParm, Bool)
addDftMarshaller Position
pos [CHSParm]
parms CHSParm
parm CDecl
cdecl
traceMarsh parms' parm' isImpure
let
sig = String
hsLexeme String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" :: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [CHSParm] -> CHSParm -> String
funTy [CHSParm]
parms' CHSParm
parm' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
marshs = [Integer -> CHSParm -> (String, String, String, String, String)
forall {p}.
Show p =>
p -> CHSParm -> (String, String, String, String, String)
marshArg Integer
i CHSParm
parm | (Integer
i, CHSParm
parm) <- [Integer] -> [CHSParm] -> [(Integer, CHSParm)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Integer
1..] [CHSParm]
parms']
funArgs = [String
funArg | (String
funArg, String
_, String
_, String
_, String
_) <- [(String, String, String, String, String)]
marshs, String
funArg String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= String
""]
marshIns = [String
marshIn | (String
_, String
marshIn, String
_, String
_, String
_) <- [(String, String, String, String, String)]
marshs]
callArgs = [String
callArg | (String
_, String
_, String
callArg, String
_, String
_) <- [(String, String, String, String, String)]
marshs]
marshOuts = [String
marshOut | (String
_, String
_, String
_, String
marshOut, String
_) <- [(String, String, String, String, String)]
marshs, String
marshOut String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= String
""]
retArgs = [String
retArg | (String
_, String
_, String
_, String
_, String
retArg) <- [(String, String, String, String, String)]
marshs, String
retArg String -> String -> Bool
forall a. Eq a => a -> a -> Bool
/= String
""]
funHead = String
hsLexeme String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
join [String]
funArgs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" =\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++
if Bool
isPure Bool -> Bool -> Bool
&& Bool
isImpure then String
" unsafePerformIO $\n" else String
""
lock = case Maybe String
mLock of Maybe String
Nothing -> String
""
Just String
lock -> String
lock String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" $"
call = if Bool
isPure
then String
" let {res = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fiLexeme String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
join [String]
callArgs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"} in\n"
else String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
lock String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fiLexeme String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
join [String]
callArgs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" >>= \\res ->\n"
marshRes = case CHSParm
parm' of
CHSParm Maybe (Ident, CHSArg)
_ String
_ Bool
twoCVal (Just (Ident
_ , CHSArg
CHSVoidArg)) Position
_ -> String
""
CHSParm Maybe (Ident, CHSArg)
_ String
_ Bool
twoCVal (Just (Ident
omIde, CHSArg
CHSIOArg )) Position
_ ->
String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Ident -> String
identToLexeme Ident
omIde String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" res >>= \\res' ->\n"
CHSParm Maybe (Ident, CHSArg)
_ String
_ Bool
twoCVal (Just (Ident
omIde, CHSArg
CHSValArg )) Position
_ ->
String
" let {res' = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Ident -> String
identToLexeme Ident
omIde String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" res} in\n"
CHSParm Maybe (Ident, CHSArg)
_ String
_ Bool
_ Maybe (Ident, CHSArg)
Nothing Position
_ ->
String -> String
forall a. String -> a
interr String
"GenBind.funDef: marshRes: no default?"
retArgs' = case CHSParm
parm' of
CHSParm Maybe (Ident, CHSArg)
_ String
_ Bool
_ (Just (Ident
_, CHSArg
CHSVoidArg)) Position
_ -> [String]
retArgs
CHSParm
_ -> String
"res'"String -> [String] -> [String]
forall a. a -> [a] -> [a]
:[String]
retArgs
ret = String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (String -> [String] -> [String]
forall a. a -> [a] -> [a]
intersperse String
", " [String]
retArgs') String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
funBody = [String] -> String
joinLines [String]
marshIns String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
call String -> String -> String
forall a. [a] -> [a] -> [a]
++
[String] -> String
joinLines [String]
marshOuts String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
marshRes String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++
(if Bool
isImpure Bool -> Bool -> Bool
|| Bool -> Bool
not Bool
isPure then String
"return " else String
"") String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ret
return $ sig ++ funHead ++ funBody
where
join :: [String] -> String
join = (String -> String) -> [String] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Char
' 'Char -> String -> String
forall a. a -> [a] -> [a]
:)
joinLines :: [String] -> String
joinLines = (String -> String) -> [String] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (\String
s -> String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
s String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n")
funTy :: [CHSParm] -> CHSParm -> String
funTy [CHSParm]
parms CHSParm
parm =
let
ctxt :: String
ctxt = case Maybe String
octxt of
Maybe String
Nothing -> String
""
Just String
ctxtStr -> String
ctxtStr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" => "
argTys :: [String]
argTys = [String
ty | CHSParm Maybe (Ident, CHSArg)
im String
ty Bool
_ Maybe (Ident, CHSArg)
_ Position
_ <- [CHSParm]
parms , Maybe (Ident, CHSArg) -> Bool
forall {a}. Maybe (a, CHSArg) -> Bool
notVoid Maybe (Ident, CHSArg)
im]
resTys :: [String]
resTys = [String
ty | CHSParm Maybe (Ident, CHSArg)
_ String
ty Bool
_ Maybe (Ident, CHSArg)
om Position
_ <- CHSParm
parmCHSParm -> [CHSParm] -> [CHSParm]
forall a. a -> [a] -> [a]
:[CHSParm]
parms, Maybe (Ident, CHSArg) -> Bool
forall {a}. Maybe (a, CHSArg) -> Bool
notVoid Maybe (Ident, CHSArg)
om]
resTup :: String
resTup = let
(String
lp, String
rp) = if Bool
isPure Bool -> Bool -> Bool
&& [String] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
resTys Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1
then (String
"", String
"")
else (String
"(", String
")")
io :: String
io = if Bool
isPure then String
"" else String
"IO "
in
String
io String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
lp String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (String -> [String] -> [String]
forall a. a -> [a] -> [a]
intersperse String
", " [String]
resTys) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
rp
in
String
ctxt String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (String -> [String] -> [String]
forall a. a -> [a] -> [a]
intersperse String
" -> " ([String]
argTys [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String
resTup]))
where
notVoid :: Maybe (a, CHSArg) -> Bool
notVoid Maybe (a, CHSArg)
Nothing = String -> Bool
forall a. String -> a
interr String
"GenBind.funDef: \
\No default marshaller?"
notVoid (Just (a
_, CHSArg
kind)) = CHSArg
kind CHSArg -> CHSArg -> Bool
forall a. Eq a => a -> a -> Bool
/= CHSArg
CHSVoidArg
marshArg :: p -> CHSParm -> (String, String, String, String, String)
marshArg p
i (CHSParm (Just (Ident
imIde, CHSArg
imArgKind)) String
_ Bool
twoCVal
(Just (Ident
omIde, CHSArg
omArgKind)) Position
_ ) =
let
a :: String
a = String
"a" String -> String -> String
forall a. [a] -> [a] -> [a]
++ p -> String
forall a. Show a => a -> String
show p
i
imStr :: String
imStr = Ident -> String
identToLexeme Ident
imIde
imApp :: String
imApp = String
imStr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
a
funArg :: String
funArg = if CHSArg
imArgKind CHSArg -> CHSArg -> Bool
forall a. Eq a => a -> a -> Bool
== CHSArg
CHSVoidArg then String
"" else String
a
inBndr :: String
inBndr = if Bool
twoCVal
then String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'1, " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'2)"
else String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'"
marshIn :: String
marshIn = case CHSArg
imArgKind of
CHSArg
CHSVoidArg -> String
imStr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" $ \\" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
inBndr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" -> "
CHSArg
CHSIOArg -> String
imApp String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" $ \\" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
inBndr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" -> "
CHSArg
CHSValArg -> String
"let {" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
inBndr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = " String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
imApp String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"} in "
callArg :: String
callArg = if Bool
twoCVal
then String
"" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'1 " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'2"
else String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'"
omApp :: String
omApp = Ident -> String
identToLexeme Ident
omIde String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
callArg
outBndr :: String
outBndr = String
a String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"''"
marshOut :: String
marshOut = case CHSArg
omArgKind of
CHSArg
CHSVoidArg -> String
""
CHSArg
CHSIOArg -> String
omApp String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
">>= \\" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
outBndr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" -> "
CHSArg
CHSValArg -> String
"let {" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
outBndr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = " String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
omApp String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"} in "
retArg :: String
retArg = if CHSArg
omArgKind CHSArg -> CHSArg -> Bool
forall a. Eq a => a -> a -> Bool
== CHSArg
CHSVoidArg then String
"" else String
outBndr
in
(String
funArg, String
marshIn, String
callArg, String
marshOut, String
retArg)
marshArg p
_ CHSParm
_ = String -> (String, String, String, String, String)
forall a. String -> a
interr String
"GenBind.funDef: Missing default?"
traceMarsh :: [CHSParm] -> CHSParm -> Bool -> CST (CState GBState) ()
traceMarsh [CHSParm]
parms CHSParm
parm Bool
isImpure = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"Marshalling specification including defaults: \n" String -> String -> String
forall a. [a] -> [a] -> [a]
++
[CHSParm] -> String -> String
showParms ([CHSParm]
parms [CHSParm] -> [CHSParm] -> [CHSParm]
forall a. [a] -> [a] -> [a]
++ [CHSParm
parm]) String
"" String -> String -> String
forall a. [a] -> [a] -> [a]
++
String
" The marshalling is " String -> String -> String
forall a. [a] -> [a] -> [a]
++ if Bool
isImpure then String
"impure.\n" else String
"pure.\n"
where
showParms :: [CHSParm] -> String -> String
showParms [] = String -> String
forall a. a -> a
id
showParms (CHSParm
parm:[CHSParm]
parms) = String -> String -> String
showString String
" "
(String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CHSParm -> String -> String
showCHSParm CHSParm
parm
(String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> String -> String
showChar Char
'\n'
(String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CHSParm] -> String -> String
showParms [CHSParm]
parms
addDftMarshaller :: Position -> [CHSParm] -> CHSParm -> CDecl
-> GB ([CHSParm], CHSParm, Bool)
addDftMarshaller :: Position
-> [CHSParm] -> CHSParm -> CDecl -> GB ([CHSParm], CHSParm, Bool)
addDftMarshaller Position
pos [CHSParm]
parms CHSParm
parm CDecl
cdecl = do
(_, fType) <- Position -> CDecl -> Bool -> GB ([Maybe HsPtrRep], ExtType)
extractFunType Position
pos CDecl
cdecl Bool
True
let (resTy, argTys) = splitFunTy fType
(parm' , isImpure1) <- checkResMarsh parm resTy
(parms', isImpure2) <- addDft parms argTys
return (parms', parm', isImpure1 || isImpure2)
where
checkResMarsh :: CHSParm -> ExtType -> GB (CHSParm, Bool)
checkResMarsh (CHSParm (Just (Ident, CHSArg)
_) String
_ Bool
_ Maybe (Ident, CHSArg)
_ Position
pos) ExtType
_ =
Position -> GB (CHSParm, Bool)
forall a. Position -> GB a
resMarshIllegalInErr Position
pos
checkResMarsh (CHSParm Maybe (Ident, CHSArg)
_ String
_ Bool
True Maybe (Ident, CHSArg)
_ Position
pos) ExtType
_ =
Position -> GB (CHSParm, Bool)
forall a. Position -> GB a
resMarshIllegalTwoCValErr Position
pos
checkResMarsh (CHSParm Maybe (Ident, CHSArg)
_ String
ty Bool
_ Maybe (Ident, CHSArg)
omMarsh Position
pos) ExtType
cTy = do
(imMarsh', _ ) <- Maybe (Ident, CHSArg)
-> PreCST
SwitchBoard (CState GBState) (Maybe (Ident, CHSArg), Bool)
forall {m :: * -> *}.
Monad m =>
Maybe (Ident, CHSArg) -> m (Maybe (Ident, CHSArg), Bool)
addDftVoid Maybe (Ident, CHSArg)
forall a. Maybe a
Nothing
(omMarsh', isImpure) <- addDftOut pos omMarsh ty [cTy]
return (CHSParm imMarsh' ty False omMarsh' pos, isImpure)
splitFunTy :: ExtType -> (ExtType, [ExtType])
splitFunTy (FunET ExtType
UnitET ExtType
ty ) = ExtType -> (ExtType, [ExtType])
splitFunTy ExtType
ty
splitFunTy (FunET ExtType
ty1 ExtType
ty2) = let
(ExtType
resTy, [ExtType]
argTys) = ExtType -> (ExtType, [ExtType])
splitFunTy ExtType
ty2
in
(ExtType
resTy, ExtType
ty1ExtType -> [ExtType] -> [ExtType]
forall a. a -> [a] -> [a]
:[ExtType]
argTys)
splitFunTy ExtType
resTy = (ExtType
resTy, [])
addDft :: [CHSParm]
-> [ExtType]
-> PreCST SwitchBoard (CState GBState) ([CHSParm], Bool)
addDft ((CHSParm Maybe (Ident, CHSArg)
imMarsh String
hsTy Bool
False Maybe (Ident, CHSArg)
omMarsh Position
p):[CHSParm]
parms) (ExtType
cTy :[ExtType]
cTys) = do
(imMarsh', isImpureIn ) <- Position
-> Maybe (Ident, CHSArg)
-> String
-> [ExtType]
-> PreCST
SwitchBoard (CState GBState) (Maybe (Ident, CHSArg), Bool)
addDftIn Position
p Maybe (Ident, CHSArg)
imMarsh String
hsTy [ExtType
cTy]
(omMarsh', isImpureOut) <- addDftVoid omMarsh
(parms' , isImpure ) <- addDft parms cTys
return (CHSParm imMarsh' hsTy False omMarsh' p : parms',
isImpure || isImpureIn || isImpureOut)
addDft ((CHSParm Maybe (Ident, CHSArg)
imMarsh String
hsTy Bool
True Maybe (Ident, CHSArg)
omMarsh Position
p):[CHSParm]
parms) (ExtType
cTy1:ExtType
cTy2:[ExtType]
cTys) = do
(imMarsh', isImpureIn ) <- Position
-> Maybe (Ident, CHSArg)
-> String
-> [ExtType]
-> PreCST
SwitchBoard (CState GBState) (Maybe (Ident, CHSArg), Bool)
addDftIn Position
p Maybe (Ident, CHSArg)
imMarsh String
hsTy [ExtType
cTy1, ExtType
cTy2]
(omMarsh', isImpureOut) <- addDftVoid omMarsh
(parms' , isImpure ) <- addDft parms cTys
return (CHSParm imMarsh' hsTy True omMarsh' p : parms',
isImpure || isImpureIn || isImpureOut)
addDft [] [] =
([CHSParm], Bool)
-> PreCST SwitchBoard (CState GBState) ([CHSParm], Bool)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return ([], Bool
False)
addDft ((CHSParm Maybe (Ident, CHSArg)
_ String
_ Bool
_ Maybe (Ident, CHSArg)
_ Position
pos):[CHSParm]
parms) [] =
Position
-> String -> PreCST SwitchBoard (CState GBState) ([CHSParm], Bool)
forall a. Position -> String -> GB a
marshArgMismatchErr Position
pos String
"This parameter is in excess of the C arguments."
addDft [] (ExtType
_:[ExtType]
_) =
Position
-> String -> PreCST SwitchBoard (CState GBState) ([CHSParm], Bool)
forall a. Position -> String -> GB a
marshArgMismatchErr Position
pos String
"Parameter marshallers are missing."
addDftIn :: Position
-> Maybe (Ident, CHSArg)
-> String
-> [ExtType]
-> PreCST
SwitchBoard (CState GBState) (Maybe (Ident, CHSArg), Bool)
addDftIn Position
_ imMarsh :: Maybe (Ident, CHSArg)
imMarsh@(Just (Ident
_, CHSArg
kind)) String
_ [ExtType]
_ = (Maybe (Ident, CHSArg), Bool)
-> PreCST
SwitchBoard (CState GBState) (Maybe (Ident, CHSArg), Bool)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg)
imMarsh,
CHSArg
kind CHSArg -> CHSArg -> Bool
forall a. Eq a => a -> a -> Bool
== CHSArg
CHSIOArg)
addDftIn Position
pos imMarsh :: Maybe (Ident, CHSArg)
imMarsh@Maybe (Ident, CHSArg)
Nothing String
hsTy [ExtType]
cTys = do
marsh <- String -> [ExtType] -> GB (Maybe (Ident, CHSArg))
lookupDftMarshIn String
hsTy [ExtType]
cTys
when (isNothing marsh) $
noDftMarshErr pos "\"in\"" hsTy cTys
return (marsh, case marsh of {Just (Ident
_, CHSArg
kind) -> CHSArg
kind CHSArg -> CHSArg -> Bool
forall a. Eq a => a -> a -> Bool
== CHSArg
CHSIOArg})
addDftOut :: Position
-> Maybe (Ident, CHSArg)
-> String
-> [ExtType]
-> PreCST
SwitchBoard (CState GBState) (Maybe (Ident, CHSArg), Bool)
addDftOut Position
_ omMarsh :: Maybe (Ident, CHSArg)
omMarsh@(Just (Ident
_, CHSArg
kind)) String
_ [ExtType]
_ = (Maybe (Ident, CHSArg), Bool)
-> PreCST
SwitchBoard (CState GBState) (Maybe (Ident, CHSArg), Bool)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg)
omMarsh,
CHSArg
kind CHSArg -> CHSArg -> Bool
forall a. Eq a => a -> a -> Bool
== CHSArg
CHSIOArg)
addDftOut Position
pos omMarsh :: Maybe (Ident, CHSArg)
omMarsh@Maybe (Ident, CHSArg)
Nothing String
hsTy [ExtType]
cTys = do
marsh <- String -> [ExtType] -> GB (Maybe (Ident, CHSArg))
lookupDftMarshOut String
hsTy [ExtType]
cTys
when (isNothing marsh) $
noDftMarshErr pos "\"out\"" hsTy cTys
return (marsh, case marsh of {Just (Ident
_, CHSArg
kind) -> CHSArg
kind CHSArg -> CHSArg -> Bool
forall a. Eq a => a -> a -> Bool
== CHSArg
CHSIOArg})
addDftVoid :: Maybe (Ident, CHSArg) -> m (Maybe (Ident, CHSArg), Bool)
addDftVoid marsh :: Maybe (Ident, CHSArg)
marsh@(Just (Ident
_, CHSArg
kind)) = (Maybe (Ident, CHSArg), Bool) -> m (Maybe (Ident, CHSArg), Bool)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Ident, CHSArg)
marsh, CHSArg
kind CHSArg -> CHSArg -> Bool
forall a. Eq a => a -> a -> Bool
== CHSArg
CHSIOArg)
addDftVoid Maybe (Ident, CHSArg)
Nothing = do
(Maybe (Ident, CHSArg), Bool) -> m (Maybe (Ident, CHSArg), Bool)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Ident, CHSArg) -> Maybe (Ident, CHSArg)
forall a. a -> Maybe a
Just (String -> Ident
noPosIdent String
"void", CHSArg
CHSVoidArg), Bool
False)
accessPath :: CHSAPath -> GB (CDecl, [BitSize])
accessPath :: CHSAPath -> GB (CDecl, [BitSize])
accessPath (CHSRoot Ident
ide) =
do
decl <- Ident -> Bool -> Bool -> CT GBState CDecl
forall s. Ident -> Bool -> Bool -> CT s CDecl
findAndChaseDecl Ident
ide Bool
False Bool
True
return (ide `simplifyDecl` decl, [BitSize 0 0])
accessPath (CHSDeref (CHSRoot Ident
ide) Position
_) =
do
decl <- Ident -> Bool -> Bool -> CT GBState CDecl
forall s. Ident -> Bool -> Bool -> CT s CDecl
findAndChaseDecl Ident
ide Bool
True Bool
True
return (ide `simplifyDecl` decl, [BitSize 0 0])
accessPath (CHSRef root :: CHSAPath
root@(CHSRoot Ident
ide1) Ident
ide2) =
do
su <- Ident -> Bool -> Bool -> CT GBState CStructUnion
forall s. Ident -> Bool -> Bool -> CT s CStructUnion
lookupStructUnion Ident
ide1 Bool
False Bool
True
(offset, decl') <- refStruct su ide2
adecl <- replaceByAlias decl'
return (adecl, [offset])
accessPath (CHSRef (CHSDeref (CHSRoot Ident
ide1) Position
_) Ident
ide2) =
do
su <- Ident -> Bool -> Bool -> CT GBState CStructUnion
forall s. Ident -> Bool -> Bool -> CT s CStructUnion
lookupStructUnion Ident
ide1 Bool
True Bool
True
(offset, decl') <- refStruct su ide2
adecl <- replaceByAlias decl'
return (adecl, [offset])
accessPath (CHSRef CHSAPath
path Ident
ide) =
do
(decl, offset:offsets) <- CHSAPath -> GB (CDecl, [BitSize])
accessPath CHSAPath
path
assertPrimDeclr ide decl
su <- structFromDecl (posOf ide) decl
(addOffset, decl') <- refStruct su ide
adecl <- replaceByAlias decl'
return (adecl, offset `addBitSize` addOffset : offsets)
where
assertPrimDeclr :: Ident -> CDecl -> CST (CState GBState) ()
assertPrimDeclr Ident
ide (CDecl [CDeclSpec]
_ [(Maybe CDeclr, Maybe CInit, Maybe CExpr)
declr] Attrs
_) =
case (Maybe CDeclr, Maybe CInit, Maybe CExpr)
declr of
(Just (CVarDeclr Maybe Ident
_ Attrs
_), Maybe CInit
_, Maybe CExpr
_) -> CST (CState GBState) ()
forall e s. PreCST e s ()
nop
(Maybe CDeclr, Maybe CInit, Maybe CExpr)
_ -> Ident -> CST (CState GBState) ()
forall a. Ident -> GB a
structExpectedErr Ident
ide
accessPath (CHSDeref CHSAPath
path Position
pos) =
do
(decl, offsets) <- CHSAPath -> GB (CDecl, [BitSize])
accessPath CHSAPath
path
decl' <- derefOrErr decl
adecl <- replaceByAlias decl'
return (adecl, BitSize 0 0 : offsets)
where
derefOrErr :: CDecl -> CT GBState CDecl
derefOrErr (CDecl [CDeclSpec]
specs [(Maybe CDeclr, Maybe CInit, Maybe CExpr)
declr] Attrs
at) =
case (Maybe CDeclr, Maybe CInit, Maybe CExpr)
declr of
(Just (CPtrDeclr [CTypeQual
_] CDeclr
declr Attrs
at), Maybe CInit
oinit, Maybe CExpr
oexpr) ->
CDecl -> CT GBState CDecl
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CDecl -> CT GBState CDecl) -> CDecl -> CT GBState CDecl
forall a b. (a -> b) -> a -> b
$ [CDeclSpec]
-> [(Maybe CDeclr, Maybe CInit, Maybe CExpr)] -> Attrs -> CDecl
CDecl [CDeclSpec]
specs [(CDeclr -> Maybe CDeclr
forall a. a -> Maybe a
Just CDeclr
declr, Maybe CInit
oinit, Maybe CExpr
oexpr)] Attrs
at
(Just (CPtrDeclr (CTypeQual
_:[CTypeQual]
quals) CDeclr
declr Attrs
at), Maybe CInit
oinit, Maybe CExpr
oexpr) ->
CDecl -> CT GBState CDecl
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CDecl -> CT GBState CDecl) -> CDecl -> CT GBState CDecl
forall a b. (a -> b) -> a -> b
$
[CDeclSpec]
-> [(Maybe CDeclr, Maybe CInit, Maybe CExpr)] -> Attrs -> CDecl
CDecl [CDeclSpec]
specs [(CDeclr -> Maybe CDeclr
forall a. a -> Maybe a
Just ([CTypeQual] -> CDeclr -> Attrs -> CDeclr
CPtrDeclr [CTypeQual]
quals CDeclr
declr Attrs
at), Maybe CInit
oinit, Maybe CExpr
oexpr)] Attrs
at
(Maybe CDeclr, Maybe CInit, Maybe CExpr)
_ ->
Position -> CT GBState CDecl
forall a. Position -> GB a
ptrExpectedErr Position
pos
replaceByAlias :: CDecl -> GB CDecl
replaceByAlias :: CDecl -> CT GBState CDecl
replaceByAlias cdecl :: CDecl
cdecl@(CDecl [CDeclSpec]
_ [(Maybe CDeclr
_, Maybe CInit
_, Maybe CExpr
size)] Attrs
at) =
do
ocdecl <- CDecl -> CT GBState (Maybe CDecl)
forall s. CDecl -> CT s (Maybe CDecl)
checkForAlias CDecl
cdecl
case ocdecl of
Maybe CDecl
Nothing -> CDecl -> CT GBState CDecl
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return CDecl
cdecl
Just (CDecl [CDeclSpec]
specs [(Maybe CDeclr
declr, Maybe CInit
init, Maybe CExpr
_)] Attrs
at) ->
CDecl -> CT GBState CDecl
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CDecl -> CT GBState CDecl) -> CDecl -> CT GBState CDecl
forall a b. (a -> b) -> a -> b
$ [CDeclSpec]
-> [(Maybe CDeclr, Maybe CInit, Maybe CExpr)] -> Attrs -> CDecl
CDecl [CDeclSpec]
specs [(Maybe CDeclr
declr, Maybe CInit
init, Maybe CExpr
size)] Attrs
at
refStruct :: CStructUnion -> Ident -> GB (BitSize, CDecl)
refStruct :: CStructUnion -> Ident -> GB (BitSize, CDecl)
refStruct CStructUnion
su Ident
ide =
do
let ([CDecl]
fields, CStructTag
tag) = CStructUnion -> ([CDecl], CStructTag)
structMembers CStructUnion
su
([CDecl]
pre, [CDecl]
post) = (CDecl -> Bool) -> [CDecl] -> ([CDecl], [CDecl])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Bool -> Bool
not (Bool -> Bool) -> (CDecl -> Bool) -> CDecl -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CDecl -> Ident -> Bool) -> Ident -> CDecl -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip CDecl -> Ident -> Bool
declNamed Ident
ide) [CDecl]
fields
Bool -> CST (CState GBState) () -> CST (CState GBState) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([CDecl] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [CDecl]
post) (CST (CState GBState) () -> CST (CState GBState) ())
-> CST (CState GBState) () -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
Position -> Ident -> CST (CState GBState) ()
forall a. Position -> Ident -> GB a
unknownFieldErr (CStructUnion -> Position
forall a. Pos a => a -> Position
posOf CStructUnion
su) Ident
ide
let decl :: CDecl
decl = [CDecl] -> CDecl
forall a. HasCallStack => [a] -> a
head [CDecl]
post
offset <- case CStructTag
tag of
CStructTag
CStructTag -> [CDecl]
-> CDecl
-> CStructTag
-> PreCST SwitchBoard (CState GBState) BitSize
offsetInStruct [CDecl]
pre CDecl
decl CStructTag
tag
CStructTag
CUnionTag -> BitSize -> PreCST SwitchBoard (CState GBState) BitSize
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (BitSize -> PreCST SwitchBoard (CState GBState) BitSize)
-> BitSize -> PreCST SwitchBoard (CState GBState) BitSize
forall a b. (a -> b) -> a -> b
$ Int -> Int -> BitSize
BitSize Int
0 Int
0
return (offset, decl)
declNamed :: CDecl -> Ident -> Bool
(CDecl [CDeclSpec]
_ [(Maybe CDeclr
Nothing , Maybe CInit
_, Maybe CExpr
_)] Attrs
_) declNamed :: CDecl -> Ident -> Bool
`declNamed` Ident
ide = Bool
False
(CDecl [CDeclSpec]
_ [(Just CDeclr
declr, Maybe CInit
_, Maybe CExpr
_)] Attrs
_) `declNamed` Ident
ide = CDeclr
declr CDeclr -> Ident -> Bool
`declrNamed` Ident
ide
(CDecl [CDeclSpec]
_ [] Attrs
_) `declNamed` Ident
_ =
String -> Bool
forall a. String -> a
interr String
"GenBind.declNamed: Abstract declarator in structure!"
CDecl
_ `declNamed` Ident
_ =
String -> Bool
forall a. String -> a
interr String
"GenBind.declNamed: More than one declarator!"
setGet :: Position -> CHSAccess -> [BitSize] -> ExtType -> GB String
setGet :: Position -> CHSAccess -> [BitSize] -> ExtType -> GB String
setGet Position
pos CHSAccess
access [BitSize]
offsets ExtType
ty =
do
let pre :: String
pre = case CHSAccess
access of
CHSAccess
CHSSet -> String
"(\\ptr val -> do {"
CHSAccess
CHSGet -> String
"(\\ptr -> do {"
body <- [BitSize] -> GB String
setGetBody ([BitSize] -> [BitSize]
forall a. [a] -> [a]
reverse [BitSize]
offsets)
return $ pre ++ body ++ "})"
where
setGetBody :: [BitSize] -> GB String
setGetBody [BitSize Int
offset Int
bitOffset] =
do
let ty' :: ExtType
ty' = case ExtType
ty of
t :: ExtType
t@(DefinedET CDecl
_ HsPtrRep
_) -> ExtType -> ExtType
PtrET ExtType
t
ExtType
t -> ExtType
t
let tyTag :: String
tyTag = ExtType -> String
showExtType ExtType
ty'
bf <- ExtType -> GB (Maybe (Bool, Int))
checkType ExtType
ty'
case bf of
Maybe (Bool, Int)
Nothing -> String -> GB String
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> GB String) -> String -> GB String
forall a b. (a -> b) -> a -> b
$ case CHSAccess
access of
CHSAccess
CHSGet -> Int -> String -> String
forall {a}. Show a => a -> String -> String
peekOp Int
offset String
tyTag
CHSAccess
CHSSet -> Int -> String -> String -> String
forall {a}. Show a => a -> String -> String -> String
pokeOp Int
offset String
tyTag String
"val"
Just (Bool
_, Int
bs) -> String -> GB String
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> GB String) -> String -> GB String
forall a b. (a -> b) -> a -> b
$ case CHSAccess
access of
CHSAccess
CHSGet -> String
"val <- " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String -> String
forall {a}. Show a => a -> String -> String
peekOp Int
offset String
tyTag
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
extractBitfield
CHSAccess
CHSSet -> String
"org <- " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String -> String
forall {a}. Show a => a -> String -> String
peekOp Int
offset String
tyTag
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
insertBitfield
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String -> String -> String
forall {a}. Show a => a -> String -> String -> String
pokeOp Int
offset String
tyTag String
"val'"
where
extractBitfield :: String
extractBitfield = String
"; return $ (val `shiftL` ("
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
bitsPerField String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" - "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Int
bs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bitOffset) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")) `shiftR` ("
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
bitsPerField String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" - " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
bs
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
bitsPerField :: String
bitsPerField = Int -> String
forall a. Show a => a -> String
show (Int -> String) -> Int -> String
forall a b. (a -> b) -> a -> b
$ CPrimType -> Int
size CPrimType
CIntPT Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8
insertBitfield :: String
insertBitfield = String
"; let {val' = (org .&. " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
middleMask
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
") .|. (val `shiftL` "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
bitOffset String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")}; "
middleMask :: String
middleMask = String
"fromIntegral (((maxBound::CUInt) `shiftL` "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
bs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
") `rotateL` "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
bitOffset String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
setGetBody (BitSize Int
offset Int
0 : [BitSize]
offsets) =
do
code <- [BitSize] -> GB String
setGetBody [BitSize]
offsets
return $ "ptr <- peekByteOff ptr " ++ show offset ++ "; " ++ code
setGetBody (BitSize Int
_ Int
_ : [BitSize]
_ ) =
Position -> GB String
forall a. Position -> GB a
derefBitfieldErr Position
pos
checkType :: ExtType -> GB (Maybe (Bool, Int))
checkType (IOET ExtType
_ ) = String -> GB (Maybe (Bool, Int))
forall a. String -> a
interr String
"GenBind.setGet: Illegal \
\type!"
checkType (ExtType
UnitET ) = Position -> GB (Maybe (Bool, Int))
forall a. Position -> GB a
voidFieldErr Position
pos
checkType (PrimET (CUFieldPT Int
bs)) = Maybe (Bool, Int) -> GB (Maybe (Bool, Int))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Bool, Int) -> GB (Maybe (Bool, Int)))
-> Maybe (Bool, Int) -> GB (Maybe (Bool, Int))
forall a b. (a -> b) -> a -> b
$ (Bool, Int) -> Maybe (Bool, Int)
forall a. a -> Maybe a
Just (Bool
False, Int
bs)
checkType (PrimET (CSFieldPT Int
bs)) = Maybe (Bool, Int) -> GB (Maybe (Bool, Int))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Bool, Int) -> GB (Maybe (Bool, Int)))
-> Maybe (Bool, Int) -> GB (Maybe (Bool, Int))
forall a b. (a -> b) -> a -> b
$ (Bool, Int) -> Maybe (Bool, Int)
forall a. a -> Maybe a
Just (Bool
True , Int
bs)
checkType ExtType
_ = Maybe (Bool, Int) -> GB (Maybe (Bool, Int))
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (Bool, Int)
forall a. Maybe a
Nothing
peekOp :: a -> String -> String
peekOp a
off String
tyTag = String
"peekByteOff ptr " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
off String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" ::IO " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
tyTag
pokeOp :: a -> String -> String -> String
pokeOp a
off String
tyTag String
var = String
"pokeByteOff ptr " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
off String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
var
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"::" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
tyTag String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
pointerDef :: Bool
-> Ident
-> String
-> CHSPtrType
-> Bool
-> String
-> Bool
-> GB String
pointerDef :: Bool
-> Ident
-> String
-> CHSPtrType
-> Bool
-> String
-> Bool
-> GB String
pointerDef Bool
isStar Ident
cNameFull String
hsName CHSPtrType
ptrKind Bool
isNewtype String
hsType Bool
isFun =
do
keepOld <- (SwitchBoard -> Bool) -> PreCST SwitchBoard (CState GBState) Bool
forall a s. (SwitchBoard -> a) -> CST s a
getSwitch SwitchBoard -> Bool
oldFFI
let ptrArg = if Bool
keepOld
then String
"()"
else if Bool
isNewtype
then String
hsName
else String
hsType
ptrCon = case CHSPtrType
ptrKind of
CHSPtrType
CHSPtr | Bool
isFun -> String
"FunPtr"
CHSPtrType
_ -> CHSPtrType -> String
forall a. Show a => a -> String
show CHSPtrType
ptrKind
ptrType = String
ptrCon String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ptrArg String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
thePtr = (Bool
isStar, Ident
cNameFull)
thePtr `ptrMapsTo` (isFun,
ptrKind,
if isNewtype then Just hsName else Nothing,
ptrArg)
return $
if isNewtype
then "newtype " ++ hsName ++ " = " ++ hsName ++ " (" ++ ptrType ++ ")"
else "type " ++ hsName ++ " = " ++ ptrType
classDef :: Position
-> String
-> String
-> CHSPtrType
-> Bool
-> [(String, String, HsObject)]
-> GB String
classDef :: Position
-> String
-> String
-> CHSPtrType
-> Bool
-> [(String, String, HsObject)]
-> GB String
classDef Position
pos String
className String
typeName CHSPtrType
ptrType Bool
isNewtype [(String, String, HsObject)]
superClasses =
do
let
toMethodName :: String
toMethodName = case String
typeName of
String
"" -> String -> String
forall a. String -> a
interr String
"GenBind.classDef: \
\Illegal identifier!"
Char
c:String
cs -> Char -> Char
toLower Char
c Char -> String -> String
forall a. a -> [a] -> [a]
: String
cs
fromMethodName :: String
fromMethodName = String
"from" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
typeName
classDefContext :: String
classDefContext = case [(String, String, HsObject)]
superClasses of
[] -> String
""
(String
superName, String
_, HsObject
_):[(String, String, HsObject)]
_ -> String
superName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" p => "
classDef :: String
classDef =
String
"class " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
classDefContext String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
className String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" p where\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
toMethodName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" :: p -> " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
typeName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fromMethodName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" :: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
typeName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" -> p\n"
instDef :: String
instDef =
String
"instance " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
className String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
typeName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" where\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
toMethodName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = id\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fromMethodName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" = id\n"
instDefs <- [(String, String, HsObject)] -> GB String
castInstDefs [(String, String, HsObject)]
superClasses
return $ classDef ++ instDefs ++ instDef
where
castInstDefs :: [(String, String, HsObject)] -> GB String
castInstDefs [] = String -> GB String
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return String
""
castInstDefs ((String
superName, String
ptrName, Pointer CHSPtrType
ptrType' Bool
isNewtype'):[(String, String, HsObject)]
classes) =
do
Bool -> CST (CState GBState) () -> CST (CState GBState) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (CHSPtrType
ptrType CHSPtrType -> CHSPtrType -> Bool
forall a. Eq a => a -> a -> Bool
== CHSPtrType
ptrType') (CST (CState GBState) () -> CST (CState GBState) ())
-> CST (CState GBState) () -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
Position -> String -> String -> CST (CState GBState) ()
forall a. Position -> String -> String -> GB a
pointerTypeMismatchErr Position
pos String
className String
superName
let toMethodName :: String
toMethodName = case String
ptrName of
String
"" -> String -> String
forall a. String -> a
interr String
"GenBind.classDef: \
\Illegal identifier - 2!"
Char
c:String
cs -> Char -> Char
toLower Char
c Char -> String -> String
forall a. a -> [a] -> [a]
: String
cs
fromMethodName :: String
fromMethodName = String
"from" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ptrName
castFun :: String
castFun = String
"cast" String -> String -> String
forall a. [a] -> [a] -> [a]
++ CHSPtrType -> String
forall a. Show a => a -> String
show CHSPtrType
ptrType
typeConstr :: String
typeConstr = if Bool
isNewtype then String
typeName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " else String
""
superConstr :: String
superConstr = if Bool
isNewtype' then String
ptrName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " else String
""
instDef :: String
instDef =
String
"instance " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
superName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
typeName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" where\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
toMethodName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
typeConstr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"p) = "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
superConstr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
castFun String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" p)\n"
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
fromMethodName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" (" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
superConstr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"p) = "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
typeConstr String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
castFun String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" p)\n"
instDefs <- [(String, String, HsObject)] -> GB String
castInstDefs [(String, String, HsObject)]
classes
return $ instDef ++ instDefs
data ConstResult = IntResult Integer
| FloatResult Float
data ExtType = FunET ExtType ExtType
| IOET ExtType
| PtrET ExtType
| DefinedET CDecl HsPtrRep
| PrimET CPrimType
| UnitET
instance Eq ExtType where
(FunET ExtType
t1 ExtType
t2 ) == :: ExtType -> ExtType -> Bool
== (FunET ExtType
t1' ExtType
t2' ) = ExtType
t1 ExtType -> ExtType -> Bool
forall a. Eq a => a -> a -> Bool
== ExtType
t1' Bool -> Bool -> Bool
&& ExtType
t2 ExtType -> ExtType -> Bool
forall a. Eq a => a -> a -> Bool
== ExtType
t2'
(IOET ExtType
t ) == (IOET ExtType
t' ) = ExtType
t ExtType -> ExtType -> Bool
forall a. Eq a => a -> a -> Bool
== ExtType
t'
(PtrET ExtType
t ) == (PtrET ExtType
t' ) = ExtType
t ExtType -> ExtType -> Bool
forall a. Eq a => a -> a -> Bool
== ExtType
t'
(DefinedET CDecl
_ HsPtrRep
rep ) == (DefinedET CDecl
_ HsPtrRep
rep' ) = HsPtrRep
rep HsPtrRep -> HsPtrRep -> Bool
forall a. Eq a => a -> a -> Bool
== HsPtrRep
rep'
(PrimET CPrimType
t ) == (PrimET CPrimType
t' ) = CPrimType
t CPrimType -> CPrimType -> Bool
forall a. Eq a => a -> a -> Bool
== CPrimType
t'
ExtType
UnitET == ExtType
UnitET = Bool
True
data CompType = ExtType ExtType
| SUType CStructUnion
isFunExtType :: ExtType -> Bool
isFunExtType :: ExtType -> Bool
isFunExtType (FunET ExtType
_ ExtType
_) = Bool
True
isFunExtType (IOET ExtType
_ ) = Bool
True
isFunExtType (DefinedET CDecl
_ (Bool
isFun,CHSPtrType
_,Maybe String
_,String
_)) = Bool
isFun
isFunExtType ExtType
_ = Bool
False
showExtType :: ExtType -> String
showExtType :: ExtType -> String
showExtType (FunET ExtType
UnitET ExtType
res) = ExtType -> String
showExtType ExtType
res
showExtType (FunET ExtType
arg ExtType
res) = String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExtType -> String
showExtType ExtType
arg String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" -> "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExtType -> String
showExtType ExtType
res String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showExtType (IOET ExtType
t) = String
"(IO " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExtType -> String
showExtType ExtType
t String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showExtType (PtrET ExtType
t) = let ptrCon :: String
ptrCon = if ExtType -> Bool
isFunExtType ExtType
t
then String
"FunPtr" else String
"Ptr"
in
String
"(" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
ptrCon String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExtType -> String
showExtType ExtType
t
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showExtType (DefinedET CDecl
_ (Bool
_,CHSPtrType
_,Maybe String
_,String
str)) = String
str
showExtType (PrimET CPrimType
CPtrPT) = String
"(Ptr ())"
showExtType (PrimET CPrimType
CFunPtrPT) = String
"(FunPtr ())"
showExtType (PrimET CPrimType
CCharPT) = String
"CChar"
showExtType (PrimET CPrimType
CUCharPT) = String
"CUChar"
showExtType (PrimET CPrimType
CSCharPT) = String
"CSChar"
showExtType (PrimET CPrimType
CIntPT) = String
"CInt"
showExtType (PrimET CPrimType
CShortPT) = String
"CShort"
showExtType (PrimET CPrimType
CLongPT) = String
"CLong"
showExtType (PrimET CPrimType
CLLongPT) = String
"CLLong"
showExtType (PrimET CPrimType
CUIntPT) = String
"CUInt"
showExtType (PrimET CPrimType
CUShortPT) = String
"CUShort"
showExtType (PrimET CPrimType
CULongPT) = String
"CULong"
showExtType (PrimET CPrimType
CULLongPT) = String
"CULLong"
showExtType (PrimET CPrimType
CFloatPT) = String
"CFloat"
showExtType (PrimET CPrimType
CDoublePT) = String
"CDouble"
showExtType (PrimET CPrimType
CLDoublePT) = String
"CLDouble"
showExtType (PrimET (CSFieldPT Int
bs)) = String
"CInt{-:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
bs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"-}"
showExtType (PrimET (CUFieldPT Int
bs)) = String
"CUInt{-:" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
bs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"-}"
showExtType ExtType
UnitET = String
"()"
extractFunType :: Position -> CDecl -> Bool ->
GB ([Maybe HsPtrRep], ExtType)
Position
pos CDecl
cdecl Bool
isPure =
do
let ([CDecl]
args, CDecl
resultDecl, Bool
variadic) = CDecl -> ([CDecl], CDecl, Bool)
funResultAndArgs CDecl
cdecl
Bool -> CST (CState GBState) () -> CST (CState GBState) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
variadic (CST (CState GBState) () -> CST (CState GBState) ())
-> CST (CState GBState) () -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
Position -> Position -> CST (CState GBState) ()
forall a. Position -> Position -> GB a
variadicErr Position
pos Position
cpos
preResultType <- (ExtType -> ExtType) -> GB ExtType -> GB ExtType
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ((Maybe HsPtrRep, ExtType) -> ExtType
forall a b. (a, b) -> b
snd ((Maybe HsPtrRep, ExtType) -> ExtType)
-> (ExtType -> (Maybe HsPtrRep, ExtType)) -> ExtType -> ExtType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExtType -> (Maybe HsPtrRep, ExtType)
expandSpecialPtrs) (GB ExtType -> GB ExtType) -> GB ExtType -> GB ExtType
forall a b. (a -> b) -> a -> b
$
Position -> CDecl -> GB ExtType
extractSimpleType Position
pos CDecl
resultDecl
let resultType = if Bool
isPure
then ExtType
preResultType
else ExtType -> ExtType
IOET ExtType
preResultType
(foreignSyn, argTypes) <- liftM (unzip . map expandSpecialPtrs) $
mapM (extractSimpleType pos) args
return (foreignSyn, foldr FunET resultType argTypes)
where
cpos :: Position
cpos = CDecl -> Position
forall a. Pos a => a -> Position
posOf CDecl
cdecl
expandSpecialPtrs :: ExtType -> (Maybe HsPtrRep, ExtType)
expandSpecialPtrs :: ExtType -> (Maybe HsPtrRep, ExtType)
expandSpecialPtrs all :: ExtType
all@(DefinedET CDecl
cdecl (Bool
_, CHSPtrType
CHSPtr, Maybe String
Nothing, String
_)) =
(Maybe HsPtrRep
forall a. Maybe a
Nothing, ExtType -> ExtType
PtrET ExtType
all)
expandSpecialPtrs all :: ExtType
all@(DefinedET CDecl
cdecl HsPtrRep
hsPtrRep) =
(HsPtrRep -> Maybe HsPtrRep
forall a. a -> Maybe a
Just HsPtrRep
hsPtrRep, ExtType -> ExtType
PtrET ExtType
all)
expandSpecialPtrs ExtType
all = (Maybe HsPtrRep
forall a. Maybe a
Nothing, ExtType
all)
extractSimpleType :: Position -> CDecl -> GB ExtType
Position
pos CDecl
cdecl =
do
CST (CState GBState) ()
traceEnter
ct <- CDecl -> GB CompType
extractCompType CDecl
cdecl
case ct of
ExtType ExtType
et -> ExtType -> GB ExtType
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return ExtType
et
SUType CStructUnion
_ -> Position -> Position -> GB ExtType
forall a. Position -> Position -> GB a
illegalStructUnionErr (CDecl -> Position
forall a. Pos a => a -> Position
posOf CDecl
cdecl) Position
pos
where
traceEnter :: CST (CState GBState) ()
traceEnter = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"Entering `extractSimpleType'...\n"
extractPtrType :: CDecl -> GB ExtType
CDecl
cdecl = do
ct <- CDecl -> GB CompType
extractCompType CDecl
cdecl
case ct of
ExtType ExtType
et -> ExtType -> GB ExtType
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return ExtType
et
SUType CStructUnion
_ -> ExtType -> GB ExtType
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return ExtType
UnitET
extractCompType :: CDecl -> GB CompType
cdecl :: CDecl
cdecl@(CDecl [CDeclSpec]
specs [(Maybe CDeclr, Maybe CInit, Maybe CExpr)]
declrs Attrs
ats) =
if [(Maybe CDeclr, Maybe CInit, Maybe CExpr)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Maybe CDeclr, Maybe CInit, Maybe CExpr)]
declrs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1
then String -> GB CompType
forall a. String -> a
interr String
"GenBind.extractCompType: Too many declarators!"
else case [(Maybe CDeclr, Maybe CInit, Maybe CExpr)]
declrs of
[(Just CDeclr
declr, Maybe CInit
_, Maybe CExpr
size)] | CDeclr -> Bool
isPtrDeclr CDeclr
declr -> CDeclr -> GB CompType
ptrType CDeclr
declr
| CDeclr -> Bool
isFunDeclr CDeclr
declr -> GB CompType
funType
| Bool
otherwise -> Maybe CExpr -> GB CompType
aliasOrSpecType Maybe CExpr
size
[] -> Maybe CExpr -> GB CompType
aliasOrSpecType Maybe CExpr
forall a. Maybe a
Nothing
where
ptrType :: CDeclr -> GB CompType
ptrType CDeclr
declr = do
CST (CState GBState) ()
tracePtrType
let declrs' :: CDeclr
declrs' = CDeclr -> CDeclr
dropPtrDeclr CDeclr
declr
cdecl' :: CDecl
cdecl' = [CDeclSpec]
-> [(Maybe CDeclr, Maybe CInit, Maybe CExpr)] -> Attrs -> CDecl
CDecl [CDeclSpec]
specs [(CDeclr -> Maybe CDeclr
forall a. a -> Maybe a
Just CDeclr
declrs', Maybe CInit
forall a. Maybe a
Nothing, Maybe CExpr
forall a. Maybe a
Nothing)] Attrs
ats
oalias :: Maybe Ident
oalias = CDecl -> Maybe Ident
checkForOneAliasName CDecl
cdecl'
oHsRepr <- case Maybe Ident
oalias of
Maybe Ident
Nothing -> Maybe HsPtrRep
-> PreCST SwitchBoard (CState GBState) (Maybe HsPtrRep)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe HsPtrRep
-> PreCST SwitchBoard (CState GBState) (Maybe HsPtrRep))
-> Maybe HsPtrRep
-> PreCST SwitchBoard (CState GBState) (Maybe HsPtrRep)
forall a b. (a -> b) -> a -> b
$ Maybe HsPtrRep
forall a. Maybe a
Nothing
Just Ident
ide -> (Bool, Ident)
-> PreCST SwitchBoard (CState GBState) (Maybe HsPtrRep)
queryPtr (Bool
True, Ident
ide)
case oHsRepr of
Just HsPtrRep
repr -> HsPtrRep -> GB CompType
forall {s}. HsPtrRep -> PreCST SwitchBoard s CompType
ptrAlias HsPtrRep
repr
Maybe HsPtrRep
Nothing -> do
ct <- CDecl -> GB CompType
extractCompType CDecl
cdecl'
returnX $ case ct of
ExtType ExtType
et -> ExtType -> ExtType
PtrET ExtType
et
SUType CStructUnion
_ -> ExtType -> ExtType
PtrET ExtType
UnitET
funType :: GB CompType
funType = do
CST (CState GBState) ()
traceFunType
(_, et) <- Position -> CDecl -> Bool -> GB ([Maybe HsPtrRep], ExtType)
extractFunType (CDecl -> Position
forall a. Pos a => a -> Position
posOf CDecl
cdecl) CDecl
cdecl Bool
False
returnX et
aliasOrSpecType :: Maybe CExpr -> GB CompType
aliasOrSpecType :: Maybe CExpr -> GB CompType
aliasOrSpecType Maybe CExpr
size = do
Maybe CExpr -> CST (CState GBState) ()
forall {a}. Maybe a -> CST (CState GBState) ()
traceAliasOrSpecType Maybe CExpr
size
case CDecl -> Maybe Ident
checkForOneAliasName CDecl
cdecl of
Maybe Ident
Nothing -> Position -> [CDeclSpec] -> Maybe CExpr -> GB CompType
specType (CDecl -> Position
forall a. Pos a => a -> Position
posOf CDecl
cdecl) [CDeclSpec]
specs Maybe CExpr
size
Just Ident
ide -> do
Ident -> CST (CState GBState) ()
traceAlias Ident
ide
oHsRepr <- (Bool, Ident)
-> PreCST SwitchBoard (CState GBState) (Maybe HsPtrRep)
queryPtr (Bool
False, Ident
ide)
case oHsRepr of
Maybe HsPtrRep
Nothing -> do
cdecl' <- Ident -> CT GBState CDecl
forall s. Ident -> CT s CDecl
getDeclOf Ident
ide
let CDecl specs [(declr, init, _)] at =
ide `simplifyDecl` cdecl'
sdecl = [CDeclSpec]
-> [(Maybe CDeclr, Maybe CInit, Maybe CExpr)] -> Attrs -> CDecl
CDecl [CDeclSpec]
specs [(Maybe CDeclr
declr, Maybe CInit
init, Maybe CExpr
size)] Attrs
at
extractCompType sdecl
Just HsPtrRep
repr -> HsPtrRep -> GB CompType
forall {s}. HsPtrRep -> PreCST SwitchBoard s CompType
ptrAlias HsPtrRep
repr
ptrAlias :: HsPtrRep -> PreCST SwitchBoard s CompType
ptrAlias (Bool
isFun, CHSPtrType
ptrTy, Maybe String
wrapped, String
tyArg) =
ExtType -> PreCST SwitchBoard s CompType
forall {s}. ExtType -> PreCST SwitchBoard s CompType
returnX (ExtType -> PreCST SwitchBoard s CompType)
-> ExtType -> PreCST SwitchBoard s CompType
forall a b. (a -> b) -> a -> b
$ CDecl -> HsPtrRep -> ExtType
DefinedET CDecl
cdecl (Bool
isFun, CHSPtrType
ptrTy, Maybe String
wrapped, String
tyArg)
returnX :: ExtType -> PreCST SwitchBoard s CompType
returnX retval :: ExtType
retval@(PtrET ExtType
et) = do
keepOld <- (SwitchBoard -> Bool) -> CST s Bool
forall a s. (SwitchBoard -> a) -> CST s a
getSwitch SwitchBoard -> Bool
oldFFI
if keepOld
then return $ ExtType (PrimET CPtrPT)
else return $ ExtType retval
returnX ExtType
retval = CompType -> PreCST SwitchBoard s CompType
forall a. a -> PreCST SwitchBoard s a
forall (m :: * -> *) a. Monad m => a -> m a
return (CompType -> PreCST SwitchBoard s CompType)
-> CompType -> PreCST SwitchBoard s CompType
forall a b. (a -> b) -> a -> b
$ ExtType -> CompType
ExtType ExtType
retval
tracePtrType :: CST (CState GBState) ()
tracePtrType = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$ String
"extractCompType: explicit pointer type\n"
traceFunType :: CST (CState GBState) ()
traceFunType = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$ String
"extractCompType: explicit function type\n"
traceAliasOrSpecType :: Maybe a -> CST (CState GBState) ()
traceAliasOrSpecType Maybe a
Nothing = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"extractCompType: checking for alias\n"
traceAliasOrSpecType (Just a
_) = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"extractCompType: checking for alias of bitfield\n"
traceAlias :: Ident -> CST (CState GBState) ()
traceAlias Ident
ide = String -> CST (CState GBState) ()
traceGenBind (String -> CST (CState GBState) ())
-> String -> CST (CState GBState) ()
forall a b. (a -> b) -> a -> b
$
String
"extractCompType: found an alias called `" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Ident -> String
identToLexeme Ident
ide String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'\n"
typeMap :: [([CTypeSpec], ExtType)]
typeMap :: [([CTypeSpec], ExtType)]
typeMap = [([CTypeSpec
void] , ExtType
UnitET ),
([CTypeSpec
char] , CPrimType -> ExtType
PrimET CPrimType
CCharPT ),
([CTypeSpec
unsigned, CTypeSpec
char] , CPrimType -> ExtType
PrimET CPrimType
CUCharPT ),
([CTypeSpec
signed, CTypeSpec
char] , CPrimType -> ExtType
PrimET CPrimType
CSCharPT ),
([CTypeSpec
signed] , CPrimType -> ExtType
PrimET CPrimType
CIntPT ),
([CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CIntPT ),
([CTypeSpec
signed, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CIntPT ),
([CTypeSpec
short] , CPrimType -> ExtType
PrimET CPrimType
CShortPT ),
([CTypeSpec
short, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CShortPT ),
([CTypeSpec
signed, CTypeSpec
short] , CPrimType -> ExtType
PrimET CPrimType
CShortPT ),
([CTypeSpec
signed, CTypeSpec
short, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CShortPT ),
([CTypeSpec
long] , CPrimType -> ExtType
PrimET CPrimType
CLongPT ),
([CTypeSpec
long, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CLongPT ),
([CTypeSpec
signed, CTypeSpec
long] , CPrimType -> ExtType
PrimET CPrimType
CLongPT ),
([CTypeSpec
signed, CTypeSpec
long, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CLongPT ),
([CTypeSpec
long, CTypeSpec
long] , CPrimType -> ExtType
PrimET CPrimType
CLLongPT ),
([CTypeSpec
long, CTypeSpec
long, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CLLongPT ),
([CTypeSpec
signed, CTypeSpec
long, CTypeSpec
long] , CPrimType -> ExtType
PrimET CPrimType
CLLongPT ),
([CTypeSpec
signed, CTypeSpec
long, CTypeSpec
long, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CLLongPT ),
([CTypeSpec
unsigned] , CPrimType -> ExtType
PrimET CPrimType
CUIntPT ),
([CTypeSpec
unsigned, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CUIntPT ),
([CTypeSpec
unsigned, CTypeSpec
short] , CPrimType -> ExtType
PrimET CPrimType
CUShortPT ),
([CTypeSpec
unsigned, CTypeSpec
short, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CUShortPT ),
([CTypeSpec
unsigned, CTypeSpec
long] , CPrimType -> ExtType
PrimET CPrimType
CULongPT ),
([CTypeSpec
unsigned, CTypeSpec
long, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CULongPT ),
([CTypeSpec
unsigned, CTypeSpec
long, CTypeSpec
long] , CPrimType -> ExtType
PrimET CPrimType
CULLongPT ),
([CTypeSpec
unsigned, CTypeSpec
long, CTypeSpec
long, CTypeSpec
int] , CPrimType -> ExtType
PrimET CPrimType
CULLongPT ),
([CTypeSpec
float] , CPrimType -> ExtType
PrimET CPrimType
CFloatPT ),
([CTypeSpec
double] , CPrimType -> ExtType
PrimET CPrimType
CDoublePT ),
([CTypeSpec
long, CTypeSpec
double] , CPrimType -> ExtType
PrimET CPrimType
CLDoublePT),
([CTypeSpec
enum] , CPrimType -> ExtType
PrimET CPrimType
CIntPT )]
where
void :: CTypeSpec
void = Attrs -> CTypeSpec
CVoidType Attrs
forall a. HasCallStack => a
undefined
char :: CTypeSpec
char = Attrs -> CTypeSpec
CCharType Attrs
forall a. HasCallStack => a
undefined
short :: CTypeSpec
short = Attrs -> CTypeSpec
CShortType Attrs
forall a. HasCallStack => a
undefined
int :: CTypeSpec
int = Attrs -> CTypeSpec
CIntType Attrs
forall a. HasCallStack => a
undefined
long :: CTypeSpec
long = Attrs -> CTypeSpec
CLongType Attrs
forall a. HasCallStack => a
undefined
float :: CTypeSpec
float = Attrs -> CTypeSpec
CFloatType Attrs
forall a. HasCallStack => a
undefined
double :: CTypeSpec
double = Attrs -> CTypeSpec
CDoubleType Attrs
forall a. HasCallStack => a
undefined
signed :: CTypeSpec
signed = Attrs -> CTypeSpec
CSignedType Attrs
forall a. HasCallStack => a
undefined
unsigned :: CTypeSpec
unsigned = Attrs -> CTypeSpec
CUnsigType Attrs
forall a. HasCallStack => a
undefined
enum :: CTypeSpec
enum = CEnum -> Attrs -> CTypeSpec
CEnumType CEnum
forall a. HasCallStack => a
undefined Attrs
forall a. HasCallStack => a
undefined
specType :: Position -> [CDeclSpec] -> Maybe CExpr -> GB CompType
specType :: Position -> [CDeclSpec] -> Maybe CExpr -> GB CompType
specType Position
cpos [CDeclSpec]
specs Maybe CExpr
osize =
let tspecs :: [CTypeSpec]
tspecs = [CTypeSpec
ts | CTypeSpec CTypeSpec
ts <- [CDeclSpec]
specs]
in case [CTypeSpec] -> [([CTypeSpec], ExtType)] -> Maybe ExtType
forall {b}. [CTypeSpec] -> [([CTypeSpec], b)] -> Maybe b
lookupTSpec [CTypeSpec]
tspecs [([CTypeSpec], ExtType)]
typeMap of
Just ExtType
et | ExtType -> Bool
isUnsupportedType ExtType
et -> Position -> GB CompType
forall a. Position -> GB a
unsupportedTypeSpecErr Position
cpos
| Maybe CExpr -> Bool
forall a. Maybe a -> Bool
isNothing Maybe CExpr
osize -> CompType -> GB CompType
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CompType -> GB CompType) -> CompType -> GB CompType
forall a b. (a -> b) -> a -> b
$ ExtType -> CompType
ExtType ExtType
et
| Bool
otherwise -> [CTypeSpec] -> ExtType -> Maybe CExpr -> GB CompType
bitfieldSpec [CTypeSpec]
tspecs ExtType
et Maybe CExpr
osize
Maybe ExtType
Nothing ->
case [CTypeSpec]
tspecs of
[CSUType CStructUnion
cu Attrs
_] -> CompType -> GB CompType
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CompType -> GB CompType) -> CompType -> GB CompType
forall a b. (a -> b) -> a -> b
$ CStructUnion -> CompType
SUType CStructUnion
cu
[CEnumType CEnum
_ Attrs
_] -> CompType -> GB CompType
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CompType -> GB CompType) -> CompType -> GB CompType
forall a b. (a -> b) -> a -> b
$ ExtType -> CompType
ExtType (CPrimType -> ExtType
PrimET CPrimType
CIntPT)
[CTypeDef Ident
_ Attrs
_] -> String -> GB CompType
forall a. String -> a
interr String
"GenBind.specType: Illegal typedef alias!"
[CTypeSpec]
_ -> Position -> GB CompType
forall a. Position -> GB a
illegalTypeSpecErr Position
cpos
where
lookupTSpec :: [CTypeSpec] -> [([CTypeSpec], b)] -> Maybe b
lookupTSpec = ([CTypeSpec] -> [CTypeSpec] -> Bool)
-> [CTypeSpec] -> [([CTypeSpec], b)] -> Maybe b
forall a b. (a -> a -> Bool) -> a -> [(a, b)] -> Maybe b
lookupBy [CTypeSpec] -> [CTypeSpec] -> Bool
matches
isUnsupportedType :: ExtType -> Bool
isUnsupportedType (PrimET CPrimType
et) = CPrimType -> Int
size CPrimType
et Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
isUnsupportedType ExtType
_ = Bool
False
matches :: [CTypeSpec] -> [CTypeSpec] -> Bool
[] matches :: [CTypeSpec] -> [CTypeSpec] -> Bool
`matches` [] = Bool
True
[] `matches` (CTypeSpec
_:[CTypeSpec]
_) = Bool
False
(CTypeSpec
spec:[CTypeSpec]
specs) `matches` [CTypeSpec]
specs'
| (CTypeSpec -> Bool) -> [CTypeSpec] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (CTypeSpec -> CTypeSpec -> Bool
eqSpec CTypeSpec
spec) [CTypeSpec]
specs' = [CTypeSpec]
specs [CTypeSpec] -> [CTypeSpec] -> Bool
`matches` (CTypeSpec -> CTypeSpec -> Bool)
-> CTypeSpec -> [CTypeSpec] -> [CTypeSpec]
forall a. (a -> a -> Bool) -> a -> [a] -> [a]
deleteBy CTypeSpec -> CTypeSpec -> Bool
eqSpec CTypeSpec
spec [CTypeSpec]
specs'
| Bool
otherwise = Bool
False
eqSpec :: CTypeSpec -> CTypeSpec -> Bool
eqSpec (CVoidType Attrs
_) (CVoidType Attrs
_) = Bool
True
eqSpec (CCharType Attrs
_) (CCharType Attrs
_) = Bool
True
eqSpec (CShortType Attrs
_) (CShortType Attrs
_) = Bool
True
eqSpec (CIntType Attrs
_) (CIntType Attrs
_) = Bool
True
eqSpec (CLongType Attrs
_) (CLongType Attrs
_) = Bool
True
eqSpec (CFloatType Attrs
_) (CFloatType Attrs
_) = Bool
True
eqSpec (CDoubleType Attrs
_) (CDoubleType Attrs
_) = Bool
True
eqSpec (CSignedType Attrs
_) (CSignedType Attrs
_) = Bool
True
eqSpec (CUnsigType Attrs
_) (CUnsigType Attrs
_) = Bool
True
eqSpec (CSUType CStructUnion
_ Attrs
_) (CSUType CStructUnion
_ Attrs
_) = Bool
True
eqSpec (CEnumType CEnum
_ Attrs
_) (CEnumType CEnum
_ Attrs
_) = Bool
True
eqSpec (CTypeDef Ident
_ Attrs
_) (CTypeDef Ident
_ Attrs
_) = Bool
True
eqSpec CTypeSpec
_ CTypeSpec
_ = Bool
False
bitfieldSpec :: [CTypeSpec] -> ExtType -> Maybe CExpr -> GB CompType
bitfieldSpec :: [CTypeSpec] -> ExtType -> Maybe CExpr -> GB CompType
bitfieldSpec [CTypeSpec]
tspecs ExtType
et (Just CExpr
sizeExpr) =
do
let pos :: Position
pos = CExpr -> Position
forall a. Pos a => a -> Position
posOf CExpr
sizeExpr
sizeResult <- CExpr -> GB ConstResult
evalConstCExpr CExpr
sizeExpr
case sizeResult of
FloatResult Float
_ -> Position -> String -> GB CompType
forall a. Position -> String -> GB a
illegalConstExprErr Position
pos String
"a float result"
IntResult Integer
size' -> do
let size :: Int
size = Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
size'
case ExtType
et of
PrimET CPrimType
CUIntPT -> CPrimType -> GB CompType
returnCT (CPrimType -> GB CompType) -> CPrimType -> GB CompType
forall a b. (a -> b) -> a -> b
$ Int -> CPrimType
CUFieldPT Int
size
PrimET CPrimType
CIntPT
| [CTypeSpec
signed] [CTypeSpec] -> [CTypeSpec] -> Bool
`matches` [CTypeSpec]
tspecs
Bool -> Bool -> Bool
|| [CTypeSpec
signed, CTypeSpec
int] [CTypeSpec] -> [CTypeSpec] -> Bool
`matches` [CTypeSpec]
tspecs -> CPrimType -> GB CompType
returnCT (CPrimType -> GB CompType) -> CPrimType -> GB CompType
forall a b. (a -> b) -> a -> b
$ Int -> CPrimType
CSFieldPT Int
size
| [CTypeSpec
int] [CTypeSpec] -> [CTypeSpec] -> Bool
`matches` [CTypeSpec]
tspecs ->
CPrimType -> GB CompType
returnCT (CPrimType -> GB CompType) -> CPrimType -> GB CompType
forall a b. (a -> b) -> a -> b
$ if Bool
bitfieldIntSigned then Int -> CPrimType
CSFieldPT Int
size
else Int -> CPrimType
CUFieldPT Int
size
ExtType
_ -> Position -> GB CompType
forall a. Position -> GB a
illegalFieldSizeErr Position
pos
where
returnCT :: CPrimType -> GB CompType
returnCT = CompType -> GB CompType
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CompType -> GB CompType)
-> (CPrimType -> CompType) -> CPrimType -> GB CompType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExtType -> CompType
ExtType (ExtType -> CompType)
-> (CPrimType -> ExtType) -> CPrimType -> CompType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CPrimType -> ExtType
PrimET
int :: CTypeSpec
int = Attrs -> CTypeSpec
CIntType Attrs
forall a. HasCallStack => a
undefined
signed :: CTypeSpec
signed = Attrs -> CTypeSpec
CSignedType Attrs
forall a. HasCallStack => a
undefined
data BitSize = BitSize Int Int
deriving (BitSize -> BitSize -> Bool
(BitSize -> BitSize -> Bool)
-> (BitSize -> BitSize -> Bool) -> Eq BitSize
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BitSize -> BitSize -> Bool
== :: BitSize -> BitSize -> Bool
$c/= :: BitSize -> BitSize -> Bool
/= :: BitSize -> BitSize -> Bool
Eq, Int -> BitSize -> String -> String
[BitSize] -> String -> String
BitSize -> String
(Int -> BitSize -> String -> String)
-> (BitSize -> String)
-> ([BitSize] -> String -> String)
-> Show BitSize
forall a.
(Int -> a -> String -> String)
-> (a -> String) -> ([a] -> String -> String) -> Show a
$cshowsPrec :: Int -> BitSize -> String -> String
showsPrec :: Int -> BitSize -> String -> String
$cshow :: BitSize -> String
show :: BitSize -> String
$cshowList :: [BitSize] -> String -> String
showList :: [BitSize] -> String -> String
Show)
instance Ord BitSize where
bs1 :: BitSize
bs1@(BitSize Int
o1 Int
b1) < :: BitSize -> BitSize -> Bool
< bs2 :: BitSize
bs2@(BitSize Int
o2 Int
b2) =
BitSize -> Int
padBits BitSize
bs1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< BitSize -> Int
padBits BitSize
bs2 Bool -> Bool -> Bool
|| (Int
o1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
o2 Bool -> Bool -> Bool
&& Int
b1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
b2)
BitSize
bs1 <= :: BitSize -> BitSize -> Bool
<= BitSize
bs2 = BitSize
bs1 BitSize -> BitSize -> Bool
forall a. Ord a => a -> a -> Bool
< BitSize
bs2 Bool -> Bool -> Bool
|| BitSize
bs1 BitSize -> BitSize -> Bool
forall a. Eq a => a -> a -> Bool
== BitSize
bs2
addBitSize :: BitSize -> BitSize -> BitSize
addBitSize :: BitSize -> BitSize -> BitSize
addBitSize (BitSize Int
o1 Int
b1) (BitSize Int
o2 Int
b2) = Int -> Int -> BitSize
BitSize (Int
o1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
o2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
overflow) Int
rest
where
bitsPerBitfield :: Int
bitsPerBitfield = CPrimType -> Int
size CPrimType
CIntPT Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8
(Int
overflow, Int
rest) = (Int
b1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
b2) Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int
bitsPerBitfield
padBits :: BitSize -> Int
padBits :: BitSize -> Int
padBits (BitSize Int
o Int
0) = Int
o
padBits (BitSize Int
o Int
_) = Int
o Int -> Int -> Int
forall a. Num a => a -> a -> a
+ CPrimType -> Int
size CPrimType
CIntPT
offsetInStruct :: [CDecl] -> CDecl -> CStructTag -> GB BitSize
offsetInStruct :: [CDecl]
-> CDecl
-> CStructTag
-> PreCST SwitchBoard (CState GBState) BitSize
offsetInStruct [] CDecl
_ CStructTag
_ = BitSize -> PreCST SwitchBoard (CState GBState) BitSize
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (BitSize -> PreCST SwitchBoard (CState GBState) BitSize)
-> BitSize -> PreCST SwitchBoard (CState GBState) BitSize
forall a b. (a -> b) -> a -> b
$ Int -> Int -> BitSize
BitSize Int
0 Int
0
offsetInStruct [CDecl]
decls CDecl
decl CStructTag
tag =
do
(offset, _) <- [CDecl] -> CStructTag -> GB (BitSize, Int)
sizeAlignOfStruct [CDecl]
decls CStructTag
tag
(_, align) <- sizeAlignOf decl
return $ alignOffset offset align
sizeAlignOfStruct :: [CDecl] -> CStructTag -> GB (BitSize, Int)
sizeAlignOfStruct :: [CDecl] -> CStructTag -> GB (BitSize, Int)
sizeAlignOfStruct [] CStructTag
_ = (BitSize, Int) -> GB (BitSize, Int)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Int -> BitSize
BitSize Int
0 Int
0, Int
1)
sizeAlignOfStruct [CDecl]
decls CStructTag
CStructTag =
do
(offset, preAlign) <- [CDecl] -> CStructTag -> GB (BitSize, Int)
sizeAlignOfStruct ([CDecl] -> [CDecl]
forall a. HasCallStack => [a] -> [a]
init [CDecl]
decls) CStructTag
CStructTag
(size, align) <- sizeAlignOf (last decls)
let sizeOfStruct = BitSize -> Int -> BitSize
alignOffset BitSize
offset Int
align BitSize -> BitSize -> BitSize
`addBitSize` BitSize
size
align' = if Int
align Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int
align else Int
bitfieldAlignment
alignOfStruct = Int
preAlign Int -> Int -> Int
forall a. Ord a => a -> a -> a
`max` Int
align'
return (sizeOfStruct, alignOfStruct)
sizeAlignOfStruct [CDecl]
decls CStructTag
CUnionTag =
do
(sizes, aligns) <- (CDecl -> GB (BitSize, Int))
-> [CDecl]
-> PreCST SwitchBoard (CState GBState) ([BitSize], [Int])
forall (m :: * -> *) a b c.
Applicative m =>
(a -> m (b, c)) -> [a] -> m ([b], [c])
mapAndUnzipM CDecl -> GB (BitSize, Int)
sizeAlignOf [CDecl]
decls
let aligns' = [if Int
align Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int
align else Int
bitfieldAlignment
| Int
align <- [Int]
aligns]
return (maximum sizes, maximum aligns')
sizeAlignOfStructPad :: [CDecl] -> CStructTag -> GB (BitSize, Int)
sizeAlignOfStructPad :: [CDecl] -> CStructTag -> GB (BitSize, Int)
sizeAlignOfStructPad [CDecl]
decls CStructTag
tag =
do
(size, align) <- [CDecl] -> CStructTag -> GB (BitSize, Int)
sizeAlignOfStruct [CDecl]
decls CStructTag
tag
return (alignOffset size align, align)
sizeAlignOf :: CDecl -> GB (BitSize, Int)
sizeAlignOf :: CDecl -> GB (BitSize, Int)
sizeAlignOf (CDecl [CDeclSpec]
specs [(Just CDeclr
declr, Maybe CInit
_, Maybe CExpr
size)] Attrs
ats) | CDeclr -> Bool
isArrDeclr CDeclr
declr =
String -> GB (BitSize, Int)
forall a. String -> a
interr (String -> GB (BitSize, Int)) -> String -> GB (BitSize, Int)
forall a b. (a -> b) -> a -> b
$ String
"sizeAlignOf: calculating size of constant array not supported."
sizeAlignOf CDecl
cdecl =
do
ct <- CDecl -> GB CompType
extractCompType CDecl
cdecl
case ct of
ExtType (FunET ExtType
_ ExtType
_ ) -> (BitSize, Int) -> GB (BitSize, Int)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CPrimType -> BitSize
bitSize CPrimType
CFunPtrPT,
CPrimType -> Int
alignment CPrimType
CFunPtrPT)
ExtType (IOET ExtType
_ ) -> String -> GB (BitSize, Int)
forall a. String -> a
interr String
"GenBind.sizeof: Illegal IO type!"
ExtType (PtrET ExtType
t )
| ExtType -> Bool
isFunExtType ExtType
t -> (BitSize, Int) -> GB (BitSize, Int)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CPrimType -> BitSize
bitSize CPrimType
CFunPtrPT,
CPrimType -> Int
alignment CPrimType
CFunPtrPT)
| Bool
otherwise -> (BitSize, Int) -> GB (BitSize, Int)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CPrimType -> BitSize
bitSize CPrimType
CPtrPT, CPrimType -> Int
alignment CPrimType
CPtrPT)
ExtType (DefinedET CDecl
_ HsPtrRep
_ ) -> (BitSize, Int) -> GB (BitSize, Int)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CPrimType -> BitSize
bitSize CPrimType
CPtrPT, CPrimType -> Int
alignment CPrimType
CPtrPT)
ExtType (PrimET CPrimType
pt ) -> (BitSize, Int) -> GB (BitSize, Int)
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (CPrimType -> BitSize
bitSize CPrimType
pt, CPrimType -> Int
alignment CPrimType
pt)
ExtType ExtType
UnitET -> Position -> GB (BitSize, Int)
forall a. Position -> GB a
voidFieldErr (CDecl -> Position
forall a. Pos a => a -> Position
posOf CDecl
cdecl)
SUType CStructUnion
su ->
do
let ([CDecl]
fields, CStructTag
tag) = CStructUnion -> ([CDecl], CStructTag)
structMembers CStructUnion
su
fields' <- let ide :: Maybe Ident
ide = CStructUnion -> Maybe Ident
structName CStructUnion
su
in
if (Bool -> Bool
not (Bool -> Bool) -> ([CDecl] -> Bool) -> [CDecl] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CDecl] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([CDecl] -> Bool) -> [CDecl] -> Bool
forall a b. (a -> b) -> a -> b
$ [CDecl]
fields) Bool -> Bool -> Bool
|| Maybe Ident -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Ident
ide
then [CDecl] -> PreCST SwitchBoard (CState GBState) [CDecl]
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return [CDecl]
fields
else do
tag <- Ident -> CT GBState (Maybe CTag)
forall s. Ident -> CT s (Maybe CTag)
findTag (Maybe Ident -> Ident
forall a. HasCallStack => Maybe a -> a
fromJust Maybe Ident
ide)
case tag of
Just (StructUnionCT CStructUnion
su) -> [CDecl] -> PreCST SwitchBoard (CState GBState) [CDecl]
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return
(([CDecl], CStructTag) -> [CDecl]
forall a b. (a, b) -> a
fst (([CDecl], CStructTag) -> [CDecl])
-> (CStructUnion -> ([CDecl], CStructTag))
-> CStructUnion
-> [CDecl]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CStructUnion -> ([CDecl], CStructTag)
structMembers (CStructUnion -> [CDecl]) -> CStructUnion -> [CDecl]
forall a b. (a -> b) -> a -> b
$ CStructUnion
su)
Maybe CTag
_ -> [CDecl] -> PreCST SwitchBoard (CState GBState) [CDecl]
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return [CDecl]
fields
sizeAlignOfStructPad fields' tag
where
bitSize :: CPrimType -> BitSize
bitSize CPrimType
et | Int
sz Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Int -> Int -> BitSize
BitSize Int
0 (-Int
sz)
| Bool
otherwise = Int -> Int -> BitSize
BitSize Int
sz Int
0
where
sz :: Int
sz = CPrimType -> Int
size CPrimType
et
alignOffset :: BitSize -> Int -> BitSize
alignOffset :: BitSize -> Int -> BitSize
alignOffset offset :: BitSize
offset@(BitSize Int
octetOffset Int
bitOffset) Int
align
| Int
align Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
bitOffset Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 =
BitSize -> Int -> BitSize
alignOffset (Int -> Int -> BitSize
BitSize (Int
octetOffset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
bitOffset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
7) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8) Int
0) Int
align
| Int
align Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& Int
bitOffset Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 =
Int -> Int -> BitSize
BitSize (((Int
octetOffset Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
align Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
align) Int
0
| Int
bitOffset Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
Bool -> Bool -> Bool
|| Bool
overflowingBitfield =
BitSize -> Int -> BitSize
alignOffset BitSize
offset Int
bitfieldAlignment
| Bool
otherwise =
BitSize
offset
where
bitsPerBitfield :: Int
bitsPerBitfield = CPrimType -> Int
size CPrimType
CIntPT Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8
overflowingBitfield :: Bool
overflowingBitfield = Int
bitOffset Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
align Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
bitsPerBitfield
evalConstCExpr :: CExpr -> GB ConstResult
evalConstCExpr :: CExpr -> GB ConstResult
evalConstCExpr (CComma [CExpr]
_ Attrs
at) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr (Attrs -> Position
forall a. Pos a => a -> Position
posOf Attrs
at) String
"a comma expression"
evalConstCExpr (CAssign CAssignOp
_ CExpr
_ CExpr
_ Attrs
at) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr (Attrs -> Position
forall a. Pos a => a -> Position
posOf Attrs
at) String
"an assignment"
evalConstCExpr (CCond CExpr
b (Just CExpr
t) CExpr
e Attrs
_) =
do
bv <- CExpr -> GB ConstResult
evalConstCExpr CExpr
b
case bv of
IntResult Integer
bvi -> if Integer
bvi Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
/= Integer
0 then CExpr -> GB ConstResult
evalConstCExpr CExpr
t else CExpr -> GB ConstResult
evalConstCExpr CExpr
e
FloatResult Float
_ -> Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr (CExpr -> Position
forall a. Pos a => a -> Position
posOf CExpr
b) String
"a float result"
evalConstCExpr (CBinary CBinaryOp
op CExpr
lhs CExpr
rhs Attrs
at) =
do
lhsVal <- CExpr -> GB ConstResult
evalConstCExpr CExpr
lhs
rhsVal <- evalConstCExpr rhs
let (lhsVal', rhsVal') = usualArithConv lhsVal rhsVal
applyBin (posOf at) op lhsVal' rhsVal'
evalConstCExpr (CCast CDecl
_ CExpr
_ Attrs
_) =
String -> GB ConstResult
forall a. String -> a
todo String
"GenBind.evalConstCExpr: Casts are not implemented yet."
evalConstCExpr (CUnary CUnaryOp
op CExpr
arg Attrs
at) =
do
argVal <- CExpr -> GB ConstResult
evalConstCExpr CExpr
arg
applyUnary (posOf at) op argVal
evalConstCExpr (CSizeofExpr CExpr
_ Attrs
_) =
String -> GB ConstResult
forall a. String -> a
todo String
"GenBind.evalConstCExpr: sizeof not implemented yet."
evalConstCExpr (CSizeofType CDecl
decl Attrs
_) =
do
(size, _) <- CDecl -> GB (BitSize, Int)
sizeAlignOf CDecl
decl
return $ IntResult (fromIntegral . padBits $ size)
evalConstCExpr (CAlignofExpr CExpr
_ Attrs
_) =
String -> GB ConstResult
forall a. String -> a
todo String
"GenBind.evalConstCExpr: alignof (GNU C extension) not implemented yet."
evalConstCExpr (CAlignofType CDecl
decl Attrs
_) =
do
(_, align) <- CDecl -> GB (BitSize, Int)
sizeAlignOf CDecl
decl
return $ IntResult (fromIntegral align)
evalConstCExpr (CIndex CExpr
_ CExpr
_ Attrs
at) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr (Attrs -> Position
forall a. Pos a => a -> Position
posOf Attrs
at) String
"array indexing"
evalConstCExpr (CCall CExpr
_ [CExpr]
_ Attrs
at) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr (Attrs -> Position
forall a. Pos a => a -> Position
posOf Attrs
at) String
"function call"
evalConstCExpr (CMember CExpr
_ Ident
_ Bool
_ Attrs
at) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr (Attrs -> Position
forall a. Pos a => a -> Position
posOf Attrs
at) String
"a . or -> operator"
evalConstCExpr (CVar Ident
ide Attrs
at) =
do
(cobj, _) <- Ident -> Bool -> CT GBState (CObj, Ident)
forall s. Ident -> Bool -> CT s (CObj, Ident)
findValueObj Ident
ide Bool
False
case cobj of
EnumCO Ident
ide (CEnum Maybe Ident
_ [(Ident, Maybe CExpr)]
enumrs Attrs
_) -> (Integer -> ConstResult)
-> PreCST SwitchBoard (CState GBState) Integer -> GB ConstResult
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Integer -> ConstResult
IntResult (PreCST SwitchBoard (CState GBState) Integer -> GB ConstResult)
-> PreCST SwitchBoard (CState GBState) Integer -> GB ConstResult
forall a b. (a -> b) -> a -> b
$
Ident
-> [(Ident, Maybe CExpr)]
-> Integer
-> PreCST SwitchBoard (CState GBState) Integer
forall {t}.
Eq t =>
t
-> [(t, Maybe CExpr)]
-> Integer
-> PreCST SwitchBoard (CState GBState) Integer
enumTagValue Ident
ide [(Ident, Maybe CExpr)]
enumrs Integer
0
CObj
_ ->
String -> GB ConstResult
forall a. String -> a
todo (String -> GB ConstResult) -> String -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ String
"GenBind.evalConstCExpr: variable names not implemented yet " String -> String -> String
forall a. [a] -> [a] -> [a]
++
Position -> String
forall a. Show a => a -> String
show (Attrs -> Position
forall a. Pos a => a -> Position
posOf Attrs
at)
where
enumTagValue :: t
-> [(t, Maybe CExpr)]
-> Integer
-> PreCST SwitchBoard (CState GBState) Integer
enumTagValue t
_ [] Integer
_ =
String -> PreCST SwitchBoard (CState GBState) Integer
forall a. String -> a
interr String
"GenBind.enumTagValue: enumerator not in declaration"
enumTagValue t
ide ((t
ide', Maybe CExpr
oexpr):[(t, Maybe CExpr)]
enumrs) Integer
val =
do
val' <- case Maybe CExpr
oexpr of
Maybe CExpr
Nothing -> Integer -> PreCST SwitchBoard (CState GBState) Integer
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return Integer
val
Just CExpr
exp ->
do
val' <- CExpr -> GB ConstResult
evalConstCExpr CExpr
exp
case val' of
IntResult Integer
val' -> Integer -> PreCST SwitchBoard (CState GBState) Integer
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return Integer
val'
FloatResult Float
_ ->
Position -> String -> PreCST SwitchBoard (CState GBState) Integer
forall a. Position -> String -> GB a
illegalConstExprErr (CExpr -> Position
forall a. Pos a => a -> Position
posOf CExpr
exp) String
"a float result"
if ide == ide'
then
return val'
else
enumTagValue ide enumrs (val' + 1)
evalConstCExpr (CConst CConst
c Attrs
_) =
CConst -> GB ConstResult
evalCConst CConst
c
evalCConst :: CConst -> GB ConstResult
evalCConst :: CConst -> GB ConstResult
evalCConst (CIntConst Integer
i Attrs
_ ) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult Integer
i
evalCConst (CCharConst Char
c Attrs
_ ) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
c))
evalCConst (CFloatConst String
s Attrs
_ ) =
String -> GB ConstResult
forall a. String -> a
todo String
"GenBind.evalCConst: Float conversion from literal misses."
evalCConst (CStrConst String
s Attrs
at) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr (Attrs -> Position
forall a. Pos a => a -> Position
posOf Attrs
at) String
"a string constant"
usualArithConv :: ConstResult -> ConstResult -> (ConstResult, ConstResult)
usualArithConv :: ConstResult -> ConstResult -> (ConstResult, ConstResult)
usualArithConv lhs :: ConstResult
lhs@(FloatResult Float
_) ConstResult
rhs = (ConstResult
lhs, ConstResult -> ConstResult
toFloat ConstResult
rhs)
usualArithConv ConstResult
lhs rhs :: ConstResult
rhs@(FloatResult Float
_) = (ConstResult -> ConstResult
toFloat ConstResult
lhs, ConstResult
rhs)
usualArithConv ConstResult
lhs ConstResult
rhs = (ConstResult
lhs, ConstResult
rhs)
toFloat :: ConstResult -> ConstResult
toFloat :: ConstResult -> ConstResult
toFloat x :: ConstResult
x@(FloatResult Float
_) = ConstResult
x
toFloat (IntResult Integer
i) = Float -> ConstResult
FloatResult (Float -> ConstResult)
-> (Integer -> Float) -> Integer -> ConstResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> ConstResult) -> Integer -> ConstResult
forall a b. (a -> b) -> a -> b
$ Integer
i
applyBin :: Position
-> CBinaryOp
-> ConstResult
-> ConstResult
-> GB ConstResult
applyBin :: Position
-> CBinaryOp -> ConstResult -> ConstResult -> GB ConstResult
applyBin Position
cpos CBinaryOp
CMulOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
y)
applyBin Position
cpos CBinaryOp
CMulOp (FloatResult Float
x)
(FloatResult Float
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Float -> ConstResult
FloatResult (Float
x Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float
y)
applyBin Position
cpos CBinaryOp
CDivOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
y)
applyBin Position
cpos CBinaryOp
CDivOp (FloatResult Float
x)
(FloatResult Float
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Float -> ConstResult
FloatResult (Float
x Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Float
y)
applyBin Position
cpos CBinaryOp
CRmdOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return(ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
y)
applyBin Position
cpos CBinaryOp
CRmdOp (FloatResult Float
x)
(FloatResult Float
y) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"a % operator applied to a float"
applyBin Position
cpos CBinaryOp
CAddOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
y)
applyBin Position
cpos CBinaryOp
CAddOp (FloatResult Float
x)
(FloatResult Float
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Float -> ConstResult
FloatResult (Float
x Float -> Float -> Float
forall a. Num a => a -> a -> a
+ Float
y)
applyBin Position
cpos CBinaryOp
CSubOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
y)
applyBin Position
cpos CBinaryOp
CSubOp (FloatResult Float
x)
(FloatResult Float
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Float -> ConstResult
FloatResult (Float
x Float -> Float -> Float
forall a. Num a => a -> a -> a
- Float
y)
applyBin Position
cpos CBinaryOp
CShlOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
2Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
y)
applyBin Position
cpos CBinaryOp
CShlOp (FloatResult Float
x)
(FloatResult Float
y) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"a << operator applied to a float"
applyBin Position
cpos CBinaryOp
CShrOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Integer
2Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^Integer
y)
applyBin Position
cpos CBinaryOp
CShrOp (FloatResult Float
x)
(FloatResult Float
y) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"a >> operator applied to a float"
applyBin Position
cpos CBinaryOp
CAndOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.&. Integer
y)
applyBin Position
cpos CBinaryOp
COrOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
.|. Integer
y)
applyBin Position
cpos CBinaryOp
CXorOp (IntResult Integer
x)
(IntResult Integer
y) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (ConstResult -> GB ConstResult) -> ConstResult -> GB ConstResult
forall a b. (a -> b) -> a -> b
$ Integer -> ConstResult
IntResult (Integer
x Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
`xor` Integer
y)
applyBin Position
cpos CBinaryOp
_ (IntResult Integer
x)
(IntResult Integer
y) =
String -> GB ConstResult
forall a. String -> a
todo String
"GenBind.applyBin: Not yet implemented operator in constant expression."
applyBin Position
cpos CBinaryOp
_ (FloatResult Float
x)
(FloatResult Float
y) =
String -> GB ConstResult
forall a. String -> a
todo String
"GenBind.applyBin: Not yet implemented operator in constant expression."
applyBin Position
_ CBinaryOp
_ ConstResult
_ ConstResult
_ =
String -> GB ConstResult
forall a. String -> a
interr String
"GenBind.applyBinOp: Illegal combination!"
applyUnary :: Position -> CUnaryOp -> ConstResult -> GB ConstResult
applyUnary :: Position -> CUnaryOp -> ConstResult -> GB ConstResult
applyUnary Position
cpos CUnaryOp
CPreIncOp ConstResult
_ =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"a ++ operator"
applyUnary Position
cpos CUnaryOp
CPreDecOp ConstResult
_ =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"a -- operator"
applyUnary Position
cpos CUnaryOp
CPostIncOp ConstResult
_ =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"a ++ operator"
applyUnary Position
cpos CUnaryOp
CPostDecOp ConstResult
_ =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"a -- operator"
applyUnary Position
cpos CUnaryOp
CAdrOp ConstResult
_ =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"a & operator"
applyUnary Position
cpos CUnaryOp
CIndOp ConstResult
_ =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"a * operator"
applyUnary Position
cpos CUnaryOp
CPlusOp ConstResult
arg = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return ConstResult
arg
applyUnary Position
cpos CUnaryOp
CMinOp (IntResult Integer
x) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> ConstResult
IntResult (-Integer
x))
applyUnary Position
cpos CUnaryOp
CMinOp (FloatResult Float
x) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Float -> ConstResult
FloatResult (-Float
x))
applyUnary Position
cpos CUnaryOp
CCompOp (IntResult Integer
x) = ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> ConstResult
IntResult (Integer -> Integer
forall a. Bits a => a -> a
complement Integer
x))
applyUnary Position
cpos CUnaryOp
CNegOp (IntResult Integer
x) =
let r :: Integer
r = Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> (Bool -> Int) -> Bool -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Int
forall a. Enum a => a -> Int
fromEnum (Bool -> Integer) -> Bool -> Integer
forall a b. (a -> b) -> a -> b
$ (Integer
x Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0)
in ConstResult -> GB ConstResult
forall a. a -> PreCST SwitchBoard (CState GBState) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> ConstResult
IntResult Integer
r)
applyUnary Position
cpos CUnaryOp
CNegOp (FloatResult Float
_) =
Position -> String -> GB ConstResult
forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
"! applied to a float"
noPosIdent :: String -> Ident
noPosIdent :: String -> Ident
noPosIdent = Position -> String -> Ident
onlyPosIdent Position
nopos
traceGenBind :: String -> GB ()
traceGenBind :: String -> CST (CState GBState) ()
traceGenBind = (Traces -> Bool) -> String -> CST (CState GBState) ()
forall s. (Traces -> Bool) -> String -> CST s ()
putTraceStr Traces -> Bool
traceGenBindSW
lookupBy :: (a -> a -> Bool) -> a -> [(a, b)] -> Maybe b
lookupBy :: forall a b. (a -> a -> Bool) -> a -> [(a, b)] -> Maybe b
lookupBy a -> a -> Bool
eq a
x = ((a, b) -> b) -> Maybe (a, b) -> Maybe b
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, b) -> b
forall a b. (a, b) -> b
snd (Maybe (a, b) -> Maybe b)
-> ([(a, b)] -> Maybe (a, b)) -> [(a, b)] -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a, b) -> Bool) -> [(a, b)] -> Maybe (a, b)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (a -> a -> Bool
eq a
x (a -> Bool) -> ((a, b) -> a) -> (a, b) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, b) -> a
forall a b. (a, b) -> a
fst)
mapMaybeM_ :: Monad m => (a -> m b) -> Maybe a -> m ()
mapMaybeM_ :: forall (m :: * -> *) a b. Monad m => (a -> m b) -> Maybe a -> m ()
mapMaybeM_ a -> m b
m Maybe a
Nothing = () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
mapMaybeM_ a -> m b
m (Just a
a) = a -> m b
m a
a m b -> m () -> m ()
forall a b. m a -> m b -> m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
unknownFieldErr :: Position -> Ident -> GB a
unknownFieldErr :: forall a. Position -> Ident -> GB a
unknownFieldErr Position
cpos Ident
ide =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc (Ident -> Position
forall a. Pos a => a -> Position
posOf Ident
ide)
[String
"Unknown member name!",
String
"The structure has no member called `" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Ident -> String
identToLexeme Ident
ide
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'. The structure is defined at",
Position -> String
forall a. Show a => a -> String
show Position
cpos String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"."]
illegalStructUnionErr :: Position -> Position -> GB a
illegalStructUnionErr :: forall a. Position -> Position -> GB a
illegalStructUnionErr Position
cpos Position
pos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Illegal structure or union type!",
String
"There is not automatic support for marshaling of structures and",
String
"unions; the offending type is declared at "
String -> String -> String
forall a. [a] -> [a] -> [a]
++ Position -> String
forall a. Show a => a -> String
show Position
cpos String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"."]
illegalTypeSpecErr :: Position -> GB a
illegalTypeSpecErr :: forall a. Position -> GB a
illegalTypeSpecErr Position
cpos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
cpos
[String
"Illegal type!",
String
"The type specifiers of this declaration do not form a legal ANSI C(89) \
\type."
]
unsupportedTypeSpecErr :: Position -> GB a
unsupportedTypeSpecErr :: forall a. Position -> GB a
unsupportedTypeSpecErr Position
cpos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
cpos
[String
"Unsupported type!",
String
"The type specifier of this declaration is not supported by your C \
\compiler."
]
variadicErr :: Position -> Position -> GB a
variadicErr :: forall a. Position -> Position -> GB a
variadicErr Position
pos Position
cpos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Variadic function!",
String
"Calling variadic functions is not supported by the FFI; the function",
String
"is defined at " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Position -> String
forall a. Show a => a -> String
show Position
cpos String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"."]
illegalConstExprErr :: Position -> String -> GB a
illegalConstExprErr :: forall a. Position -> String -> GB a
illegalConstExprErr Position
cpos String
hint =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
cpos [String
"Illegal constant expression!",
String
"Encountered " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
hint String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" in a constant expression,",
String
"which ANSI C89 does not permit."]
voidFieldErr :: Position -> GB a
voidFieldErr :: forall a. Position -> GB a
voidFieldErr Position
cpos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
cpos [String
"Void field in struct!",
String
"Attempt to access a structure field of type void."]
structExpectedErr :: Ident -> GB a
structExpectedErr :: forall a. Ident -> GB a
structExpectedErr Ident
ide =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc (Ident -> Position
forall a. Pos a => a -> Position
posOf Ident
ide)
[String
"Expected a structure or union!",
String
"Attempt to access member `" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Ident -> String
identToLexeme Ident
ide String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"' in something not",
String
"a structure or union."]
ptrExpectedErr :: Position -> GB a
ptrExpectedErr :: forall a. Position -> GB a
ptrExpectedErr Position
pos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Expected a pointer object!",
String
"Attempt to dereference a non-pointer object or to use it in a `pointer' \
\hook."]
illegalStablePtrErr :: Position -> GB a
illegalStablePtrErr :: forall a. Position -> GB a
illegalStablePtrErr Position
pos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Illegal use of a stable pointer!",
String
"Class hooks cannot be used for stable pointers."]
pointerTypeMismatchErr :: Position -> String -> String -> GB a
pointerTypeMismatchErr :: forall a. Position -> String -> String -> GB a
pointerTypeMismatchErr Position
pos String
className String
superName =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Pointer type mismatch!",
String
"The pointer of the class hook for `" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
className
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"' is of a different kind",
String
"than that of the class hook for `" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
superName String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"'; this is illegal",
String
"as the latter is defined to be an (indirect) superclass of the former."]
illegalFieldSizeErr :: Position -> GB a
illegalFieldSizeErr :: forall a. Position -> GB a
illegalFieldSizeErr Position
cpos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
cpos
[String
"Illegal field size!",
String
"Only signed and unsigned `int' types may have a size annotation."]
derefBitfieldErr :: Position -> GB a
derefBitfieldErr :: forall a. Position -> GB a
derefBitfieldErr Position
pos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Illegal dereferencing of a bit field!",
String
"Bit fields cannot be dereferenced."]
resMarshIllegalInErr :: Position -> GB a
resMarshIllegalInErr :: forall a. Position -> GB a
resMarshIllegalInErr Position
pos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Malformed result marshalling!",
String
"There may not be an \"in\" marshaller for the result."]
resMarshIllegalTwoCValErr :: Position -> GB a
resMarshIllegalTwoCValErr :: forall a. Position -> GB a
resMarshIllegalTwoCValErr Position
pos =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Malformed result marshalling!",
String
"Two C values (i.e., the `&' symbol) are not allowed for the result."]
marshArgMismatchErr :: Position -> String -> GB a
marshArgMismatchErr :: forall a. Position -> String -> GB a
marshArgMismatchErr Position
pos String
reason =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Function arity mismatch!",
String
reason]
noDftMarshErr :: Position -> String -> String -> [ExtType] -> GB a
noDftMarshErr :: forall a. Position -> String -> String -> [ExtType] -> GB a
noDftMarshErr Position
pos String
inOut String
hsTy [ExtType]
cTys =
Position -> [String] -> CT GBState a
forall s a. Position -> [String] -> CT s a
raiseErrorCTExc Position
pos
[String
"Missing " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
inOut String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" marshaller!",
String
"There is no default marshaller for this combination of Haskell and \
\C type:",
String
"Haskell type: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
hsTy,
String
"C type : " String -> String -> String
forall a. [a] -> [a] -> [a]
++ [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (String -> [String] -> [String]
forall a. a -> [a] -> [a]
intersperse String
" " ((ExtType -> String) -> [ExtType] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map ExtType -> String
showExtType [ExtType]
cTys))]