{-# LINE 1 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
module Data.GI.CodeGen.StructLayout
( Aggregate(..)
, fixupStructLayouts
) where
{-# LINE 36 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
import Control.Monad (forM, unless, when)
import qualified Data.Map.Lazy as ML
import Data.Maybe (isJust)
import Data.Text (Text)
import qualified Data.Text as T
import Foreign.C.Types (CTime)
import Foreign.Storable (alignment, sizeOf)
import qualified System.Info
import Data.GI.GIR.BasicTypes (BasicType(..), Name(..), Type(..))
import Data.GI.GIR.Field (Field(..), AnonymousMember(..))
import Data.GI.GIR.Struct (Struct(..))
import Data.GI.GIR.Union (Union(..))
data Aggregate = AggStruct Struct
| AggUnion Union
| AggScalar Int
| AggPointer
data Mode = TypelibMode
| ABIMode
deriving (Mode -> Mode -> Bool
(Mode -> Mode -> Bool) -> (Mode -> Mode -> Bool) -> Eq Mode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Mode -> Mode -> Bool
== :: Mode -> Mode -> Bool
$c/= :: Mode -> Mode -> Bool
/= :: Mode -> Mode -> Bool
Eq)
data FieldLayout = FieldLayout {
FieldLayout -> Int
flOffset :: Int
, FieldLayout -> Maybe Int
flBitShift :: Maybe Int
}
data Layout = Layout {
Layout -> Int
lSize :: Int
, Layout -> Int
lAlign :: Int
, Layout -> [FieldLayout]
lFields :: [FieldLayout]
}
type SizeAlign = (Int, Int)
fixupStructLayouts :: ML.Map Name Aggregate -> ML.Map Name Aggregate
fixupStructLayouts :: Map Name Aggregate -> Map Name Aggregate
fixupStructLayouts Map Name Aggregate
aggs = (Name -> Aggregate -> Maybe Aggregate)
-> Map Name Aggregate -> Map Name Aggregate
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
ML.mapMaybeWithKey Name -> Aggregate -> Maybe Aggregate
fix Map Name Aggregate
aggs
where
fix :: Name -> Aggregate -> Maybe Aggregate
fix :: Name -> Aggregate -> Maybe Aggregate
fix Name
n (AggStruct Struct
s) | [Field] -> Bool
needsFixing (Struct -> [Field]
structFields Struct
s) = do
layout <- (Text -> Maybe Layout)
-> (Layout -> Maybe Layout) -> Either Text Layout -> Maybe Layout
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Layout -> Text -> Maybe Layout
forall a b. a -> b -> a
const Maybe Layout
forall a. Maybe a
Nothing) Layout -> Maybe Layout
forall a. a -> Maybe a
Just (Name -> Either Text Layout
abiLayout Name
n)
return $ AggStruct (s { structSize = lSize layout
, structFields = applyLayout layout
(structFields s) })
fix Name
n (AggUnion Union
u) | [Field] -> Bool
needsFixing (Union -> [Field]
unionFields Union
u) = do
layout <- (Text -> Maybe Layout)
-> (Layout -> Maybe Layout) -> Either Text Layout -> Maybe Layout
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe Layout -> Text -> Maybe Layout
forall a b. a -> b -> a
const Maybe Layout
forall a. Maybe a
Nothing) Layout -> Maybe Layout
forall a. a -> Maybe a
Just (Name -> Either Text Layout
abiLayout Name
n)
return $ AggUnion (u { unionSize = lSize layout
, unionFields = applyLayout layout
(unionFields u) })
fix Name
_ Aggregate
_ = Maybe Aggregate
forall a. Maybe a
Nothing
needsFixing :: [Field] -> Bool
needsFixing :: [Field] -> Bool
needsFixing = (Field -> Bool) -> [Field] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\Field
f -> Maybe Int -> Bool
forall a. Maybe a -> Bool
isJust (Field -> Maybe Int
fieldBits Field
f) Bool -> Bool -> Bool
|| Field -> Bool
embedsAggregate Field
f)
embedsAggregate :: Field -> Bool
embedsAggregate :: Field -> Bool
embedsAggregate Field
f
| Maybe AnonymousMember -> Bool
forall a. Maybe a -> Bool
isJust (Field -> Maybe AnonymousMember
fieldAnonymous Field
f) = Bool
True
| Field -> Bool
isPointerField Field
f = Bool
False
| Bool
otherwise = case Type -> Type
elementType (Field -> Type
fieldType Field
f) of
TInterface Name
n -> case Name -> Map Name Aggregate -> Maybe Aggregate
forall k a. Ord k => k -> Map k a -> Maybe a
ML.lookup Name
n Map Name Aggregate
aggs of
Just (AggStruct Struct
_) -> Bool
True
Just (AggUnion Union
_) -> Bool
True
Maybe Aggregate
_ -> Bool
False
Type
TGValue -> Bool
True
TGClosure Maybe Type
_ -> Bool
True
Type
_ -> Bool
False
elementType :: Type -> Type
elementType :: Type -> Type
elementType (TCArray Bool
_ Int
fixed Int
_ Type
t) | Int
fixed Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = Type -> Type
elementType Type
t
elementType Type
t = Type
t
applyLayout :: Layout -> [Field] -> [Field]
applyLayout :: Layout -> [Field] -> [Field]
applyLayout Layout
layout =
(FieldLayout -> Field -> Field)
-> [FieldLayout] -> [Field] -> [Field]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\FieldLayout
fl Field
f -> Field
f { fieldOffset = flOffset fl
, fieldBitShift = flBitShift fl })
(Layout -> [FieldLayout]
lFields Layout
layout)
typelibLayouts :: ML.Map Name (Either Text Layout)
typelibLayouts :: Map Name (Either Text Layout)
typelibLayouts = (Aggregate -> Maybe (Either Text Layout))
-> Map Name Aggregate -> Map Name (Either Text Layout)
forall a b k. (a -> Maybe b) -> Map k a -> Map k b
ML.mapMaybe Aggregate -> Maybe (Either Text Layout)
verified Map Name Aggregate
aggs
where verified :: Aggregate -> Maybe (Either Text Layout)
verified (AggStruct Struct
s) =
Either Text Layout -> Maybe (Either Text Layout)
forall a. a -> Maybe a
Just (Int -> [Field] -> Layout -> Either Text Layout
verify (Struct -> Int
structSize Struct
s) (Struct -> [Field]
structFields Struct
s)
(Layout -> Either Text Layout)
-> Either Text Layout -> Either Text Layout
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Mode -> [Field] -> Either Text Layout
structLayout Mode
TypelibMode (Struct -> [Field]
structFields Struct
s))
verified (AggUnion Union
u) =
Either Text Layout -> Maybe (Either Text Layout)
forall a. a -> Maybe a
Just (Int -> [Field] -> Layout -> Either Text Layout
verify (Union -> Int
unionSize Union
u) (Union -> [Field]
unionFields Union
u)
(Layout -> Either Text Layout)
-> Either Text Layout -> Either Text Layout
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Mode -> [Field] -> Either Text Layout
unionLayout Mode
TypelibMode (Union -> [Field]
unionFields Union
u))
verified Aggregate
_ = Maybe (Either Text Layout)
forall a. Maybe a
Nothing
verify :: Int -> [Field] -> Layout -> Either Text Layout
verify :: Int -> [Field] -> Layout -> Either Text Layout
verify Int
size [Field]
fields Layout
layout = do
Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Layout -> Int
lSize Layout
layout Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
size) (Either Text () -> Either Text ())
-> Either Text () -> Either Text ()
forall a b. (a -> b) -> a -> b
$
Text -> Either Text ()
forall a b. a -> Either a b
Left (Text -> Either Text ()) -> Text -> Either Text ()
forall a b. (a -> b) -> a -> b
$ Text
"Computed size " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow (Layout -> Int
lSize Layout
layout)
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" does not match typelib size " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
tshow Int
size
let offsets :: [(Int, Int)]
offsets = [(FieldLayout -> Int
flOffset FieldLayout
fl, Field -> Int
fieldOffset Field
f)
| (FieldLayout
fl, Field
f) <- [FieldLayout] -> [Field] -> [(FieldLayout, Field)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Layout -> [FieldLayout]
lFields Layout
layout) [Field]
fields
, Bool -> Bool
not (Maybe AnonymousMember -> Bool
forall a. Maybe a -> Bool
isJust (Field -> Maybe AnonymousMember
fieldAnonymous Field
f))]
Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (((Int, Int) -> Bool) -> [(Int, Int)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all ((Int -> Int -> Bool) -> (Int, Int) -> Bool
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
(==)) [(Int, Int)]
offsets) (Either Text () -> Either Text ())
-> Either Text () -> Either Text ()
forall a b. (a -> b) -> a -> b
$
Text -> Either Text ()
forall a b. a -> Either a b
Left Text
"Computed offsets do not match the typelib offsets"
Layout -> Either Text Layout
forall a. a -> Either Text a
forall (m :: * -> *) a. Monad m => a -> m a
return Layout
layout
abiLayouts :: ML.Map Name (Either Text Layout)
abiLayouts :: Map Name (Either Text Layout)
abiLayouts = (Name -> Aggregate -> Maybe (Either Text Layout))
-> Map Name Aggregate -> Map Name (Either Text Layout)
forall k a b. (k -> a -> Maybe b) -> Map k a -> Map k b
ML.mapMaybeWithKey Name -> Aggregate -> Maybe (Either Text Layout)
abi Map Name Aggregate
aggs
where abi :: Name -> Aggregate -> Maybe (Either Text Layout)
abi Name
n (AggStruct Struct
s) =
Either Text Layout -> Maybe (Either Text Layout)
forall a. a -> Maybe a
Just (Name -> Either Text Layout
typelibLayout Name
n Either Text Layout -> Either Text Layout -> Either Text Layout
forall a b. Either Text a -> Either Text b -> Either Text b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Mode -> [Field] -> Either Text Layout
structLayout Mode
ABIMode (Struct -> [Field]
structFields Struct
s))
abi Name
n (AggUnion Union
u) =
Either Text Layout -> Maybe (Either Text Layout)
forall a. a -> Maybe a
Just (Name -> Either Text Layout
typelibLayout Name
n Either Text Layout -> Either Text Layout -> Either Text Layout
forall a b. Either Text a -> Either Text b -> Either Text b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Mode -> [Field] -> Either Text Layout
unionLayout Mode
ABIMode (Union -> [Field]
unionFields Union
u))
abi Name
_ Aggregate
_ = Maybe (Either Text Layout)
forall a. Maybe a
Nothing
typelibLayout :: Name -> Either Text Layout
typelibLayout :: Name -> Either Text Layout
typelibLayout Name
n = case Name -> Map Name (Either Text Layout) -> Maybe (Either Text Layout)
forall k a. Ord k => k -> Map k a -> Maybe a
ML.lookup Name
n Map Name (Either Text Layout)
typelibLayouts of
Just Either Text Layout
l -> Either Text Layout
l
Maybe (Either Text Layout)
Nothing -> Text -> Either Text Layout
forall a b. a -> Either a b
Left (Text
"No layout for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name -> Text
forall a. Show a => a -> Text
tshow Name
n)
abiLayout :: Name -> Either Text Layout
abiLayout :: Name -> Either Text Layout
abiLayout Name
n = case Name -> Map Name (Either Text Layout) -> Maybe (Either Text Layout)
forall k a. Ord k => k -> Map k a -> Maybe a
ML.lookup Name
n Map Name (Either Text Layout)
abiLayouts of
Just Either Text Layout
l -> Either Text Layout
l
Maybe (Either Text Layout)
Nothing -> Text -> Either Text Layout
forall a b. a -> Either a b
Left (Text
"No layout for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name -> Text
forall a. Show a => a -> Text
tshow Name
n)
embeddedSizeAlign :: Mode -> Name -> Either Text SizeAlign
embeddedSizeAlign :: Mode -> Name -> Either Text (Int, Int)
embeddedSizeAlign Mode
mode Name
n = do
layout <- case Mode
mode of
Mode
TypelibMode -> Name -> Either Text Layout
typelibLayout Name
n
Mode
ABIMode -> Name -> Either Text Layout
abiLayout Name
n
return (lSize layout, lAlign layout)
structLayout :: Mode -> [Field] -> Either Text Layout
structLayout :: Mode -> [Field] -> Either Text Layout
structLayout Mode
mode [Field]
fields = do
sizes <- [Field]
-> (Field -> Either Text (Int, Int)) -> Either Text [(Int, Int)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Field]
fields (Mode -> Field -> Either Text (Int, Int)
fieldSizeAlign Mode
mode)
let bitfields = case Mode
mode of
Mode
TypelibMode -> (Field -> Maybe Int) -> [Field] -> [Maybe Int]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe Int -> Field -> Maybe Int
forall a b. a -> b -> a
const Maybe Int
forall a. Maybe a
Nothing) [Field]
fields
Mode
ABIMode -> (Field -> Maybe Int) -> [Field] -> [Maybe Int]
forall a b. (a -> b) -> [a] -> [b]
map Field -> Maybe Int
fieldBits [Field]
fields
placed = [Field]
-> [(Int, Int)] -> [Maybe Int] -> [(Field, (Int, Int), Maybe Int)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Field]
fields [(Int, Int)]
sizes [Maybe Int]
bitfields
if msBitfields
then msStructLayout placed
else sysvStructLayout placed
unionLayout :: Mode -> [Field] -> Either Text Layout
unionLayout :: Mode -> [Field] -> Either Text Layout
unionLayout Mode
mode [Field]
fields = do
sizes <- [Field]
-> (Field -> Either Text (Int, Int)) -> Either Text [(Int, Int)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Field]
fields (Mode -> Field -> Either Text (Int, Int)
fieldSizeAlign Mode
mode)
let align = [Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (Int
1 Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: ((Int, Int) -> Int) -> [(Int, Int)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Int) -> Int
forall a b. (a, b) -> b
snd [(Int, Int)]
sizes)
size = Int -> Int -> Int
roundUp ([Int] -> Int
forall a. Ord a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (Int
0 Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: ((Int, Int) -> Int) -> [(Int, Int)] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map (Int, Int) -> Int
forall a b. (a, b) -> a
fst [(Int, Int)]
sizes)) Int
align
fl (Field
f, (Int
s, b
_)) = case (Mode
mode, Field -> Maybe Int
fieldBits Field
f) of
(Mode
ABIMode, Just Int
w) -> Int -> Maybe Int -> FieldLayout
FieldLayout Int
0 (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Int -> Int -> Int
bitShift Int
s Int
0 Int
w))
(Mode, Maybe Int)
_ -> Int -> Maybe Int -> FieldLayout
FieldLayout Int
0 Maybe Int
forall a. Maybe a
Nothing
return $ Layout size align (map fl (zip fields sizes))
fieldSizeAlign :: Mode -> Field -> Either Text SizeAlign
fieldSizeAlign :: Mode -> Field -> Either Text (Int, Int)
fieldSizeAlign Mode
mode Field
f = case Field -> Maybe AnonymousMember
fieldAnonymous Field
f of
Just AnonymousMember
_ | Mode
mode Mode -> Mode -> Bool
forall a. Eq a => a -> a -> Bool
== Mode
TypelibMode -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right (Int
0, Int
1)
Just (AnonymousUnion [Field]
members) -> do
layout <- Mode -> [Field] -> Either Text Layout
unionLayout Mode
mode [Field]
members
return (lSize layout, lAlign layout)
Just (AnonymousStruct [Field]
members) -> do
layout <- Mode -> [Field] -> Either Text Layout
structLayout Mode
mode [Field]
members
return (lSize layout, lAlign layout)
Maybe AnonymousMember
Nothing
| Field -> Bool
isPointerField Field
f -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right (Int, Int)
pointerSizeAlign
| TCArray Bool
_ Int
fixed Int
_ Type
_ <- Field -> Type
fieldType Field
f, Int
fixed Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0, Field -> Bool
fieldArrayOfPointers Field
f ->
let (Int
size, Int
align) = (Int, Int)
pointerSizeAlign
in (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right (Int
fixed Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
size, Int
align)
| Bool
otherwise -> case Mode -> Type -> Either Text (Int, Int)
typeSizeAlign Mode
mode (Field -> Type
fieldType Field
f) of
Left Text
err -> Text -> Either Text (Int, Int)
forall a b. a -> Either a b
Left (Text
"Field “" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Field -> Text
fieldName Field
f Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"”: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
err)
Either Text (Int, Int)
r -> Either Text (Int, Int)
r
typeSizeAlign :: Mode -> Type -> Either Text SizeAlign
typeSizeAlign :: Mode -> Type -> Either Text (Int, Int)
typeSizeAlign Mode
mode Type
t = case Type
t of
TBasicType BasicType
b -> BasicType -> Either Text (Int, Int)
basicSizeAlign BasicType
b
TInterface Name
n -> case Name -> Map Name Aggregate -> Maybe Aggregate
forall k a. Ord k => k -> Map k a -> Maybe a
ML.lookup Name
n Map Name Aggregate
aggs of
Just (AggStruct Struct
_) -> Mode -> Name -> Either Text (Int, Int)
embeddedSizeAlign Mode
mode Name
n
Just (AggUnion Union
_) -> Mode -> Name -> Either Text (Int, Int)
embeddedSizeAlign Mode
mode Name
n
Just (AggScalar Int
size) -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right (Int
size, Int
size)
Just Aggregate
AggPointer -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right (Int, Int)
pointerSizeAlign
Maybe Aggregate
Nothing -> Text -> Either Text (Int, Int)
forall a b. a -> Either a b
Left (Text
"Unknown type " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name -> Text
forall a. Show a => a -> Text
tshow Name
n)
Type
TGValue -> Mode -> Name -> Either Text (Int, Int)
embeddedSizeAlign Mode
mode (Text -> Text -> Name
Name Text
"GObject" Text
"Value")
TGClosure Maybe Type
_ -> Mode -> Name -> Either Text (Int, Int)
embeddedSizeAlign Mode
mode (Text -> Text -> Name
Name Text
"GObject" Text
"Closure")
TCArray Bool
_ Int
fixed Int
_ Type
elemType
| Int
fixed Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 -> do
(size, align) <- Mode -> Type -> Either Text (Int, Int)
typeSizeAlign Mode
mode Type
elemType
return (fixed * size, align)
| Bool
otherwise -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right (Int, Int)
pointerSizeAlign
Type
_ -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right (Int, Int)
pointerSizeAlign
isPointerField :: Field -> Bool
isPointerField :: Field -> Bool
isPointerField Field
f = Maybe Callback -> Bool
forall a. Maybe a -> Bool
isJust (Field -> Maybe Callback
fieldCallback Field
f) Bool -> Bool -> Bool
|| Field -> Maybe Bool
fieldIsPointer Field
f Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True
sysvStructLayout :: [(Field, SizeAlign, Maybe Int)] -> Either Text Layout
sysvStructLayout :: [(Field, (Int, Int), Maybe Int)] -> Either Text Layout
sysvStructLayout = Int
-> Int
-> [FieldLayout]
-> [(Field, (Int, Int), Maybe Int)]
-> Either Text Layout
go Int
0 Int
1 []
where
go :: Int -> Int -> [FieldLayout] -> [(Field, SizeAlign, Maybe Int)]
-> Either Text Layout
go :: Int
-> Int
-> [FieldLayout]
-> [(Field, (Int, Int), Maybe Int)]
-> Either Text Layout
go Int
pos Int
align [FieldLayout]
acc [] =
Layout -> Either Text Layout
forall a b. b -> Either a b
Right (Layout -> Either Text Layout) -> Layout -> Either Text Layout
forall a b. (a -> b) -> a -> b
$ Int -> Int -> [FieldLayout] -> Layout
Layout (Int -> Int -> Int
roundUp (Int -> Int -> Int
roundUp Int
pos Int
8 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8) Int
align) Int
align ([FieldLayout] -> [FieldLayout]
forall a. [a] -> [a]
reverse [FieldLayout]
acc)
go Int
pos Int
align [FieldLayout]
acc ((Field
f, (Int
size, Int
falign), Maybe Int
bits) : [(Field, (Int, Int), Maybe Int)]
rest) = case Maybe Int
bits of
Maybe Int
Nothing ->
let offset :: Int
offset = Int -> Int -> Int
roundUp (Int -> Int -> Int
roundUp Int
pos Int
8 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
8) Int
falign
in Int
-> Int
-> [FieldLayout]
-> [(Field, (Int, Int), Maybe Int)]
-> Either Text Layout
go ((Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
size) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
align Int
falign)
(Int -> Maybe Int -> FieldLayout
FieldLayout Int
offset Maybe Int
forall a. Maybe a
Nothing FieldLayout -> [FieldLayout] -> [FieldLayout]
forall a. a -> [a] -> [a]
: [FieldLayout]
acc) [(Field, (Int, Int), Maybe Int)]
rest
Just Int
w -> do
let unitBits :: Int
unitBits = Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8
Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
unitBits) (Either Text () -> Either Text ())
-> Either Text () -> Either Text ()
forall a b. (a -> b) -> a -> b
$
Text -> Either Text ()
forall a b. a -> Either a b
Left (Text
"Bitfield “" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Field -> Text
fieldName Field
f Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"” is wider than its type")
let start :: Int
start = if Int
pos Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
unitBits Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
unitBits
then Int -> Int -> Int
roundUp Int
pos Int
unitBits
else Int
pos
unit :: Int
unit = (Int
start Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
unitBits) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
size
Int
-> Int
-> [FieldLayout]
-> [(Field, (Int, Int), Maybe Int)]
-> Either Text Layout
go (Int
start Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
w) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
align Int
falign)
(Int -> Maybe Int -> FieldLayout
FieldLayout Int
unit (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Int -> Int -> Int
bitShift Int
size (Int
start Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
unit Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8) Int
w)) FieldLayout -> [FieldLayout] -> [FieldLayout]
forall a. a -> [a] -> [a]
: [FieldLayout]
acc)
[(Field, (Int, Int), Maybe Int)]
rest
msStructLayout :: [(Field, SizeAlign, Maybe Int)] -> Either Text Layout
msStructLayout :: [(Field, (Int, Int), Maybe Int)] -> Either Text Layout
msStructLayout = Int
-> Maybe (Int, Int, Int)
-> Int
-> [FieldLayout]
-> [(Field, (Int, Int), Maybe Int)]
-> Either Text Layout
go Int
0 Maybe (Int, Int, Int)
forall a. Maybe a
Nothing Int
1 []
where
go :: Int -> Maybe (Int, Int, Int) -> Int -> [FieldLayout]
-> [(Field, SizeAlign, Maybe Int)] -> Either Text Layout
go :: Int
-> Maybe (Int, Int, Int)
-> Int
-> [FieldLayout]
-> [(Field, (Int, Int), Maybe Int)]
-> Either Text Layout
go Int
end Maybe (Int, Int, Int)
_ Int
align [FieldLayout]
acc [] =
Layout -> Either Text Layout
forall a b. b -> Either a b
Right (Layout -> Either Text Layout) -> Layout -> Either Text Layout
forall a b. (a -> b) -> a -> b
$ Int -> Int -> [FieldLayout] -> Layout
Layout (Int -> Int -> Int
roundUp Int
end Int
align) Int
align ([FieldLayout] -> [FieldLayout]
forall a. [a] -> [a]
reverse [FieldLayout]
acc)
go Int
end Maybe (Int, Int, Int)
unit Int
align [FieldLayout]
acc ((Field
f, (Int
size, Int
falign), Maybe Int
bits) : [(Field, (Int, Int), Maybe Int)]
rest) = case Maybe Int
bits of
Maybe Int
Nothing ->
let offset :: Int
offset = Int -> Int -> Int
roundUp Int
end Int
falign
in Int
-> Maybe (Int, Int, Int)
-> Int
-> [FieldLayout]
-> [(Field, (Int, Int), Maybe Int)]
-> Either Text Layout
go (Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
size) Maybe (Int, Int, Int)
forall a. Maybe a
Nothing (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
align Int
falign)
(Int -> Maybe Int -> FieldLayout
FieldLayout Int
offset Maybe Int
forall a. Maybe a
Nothing FieldLayout -> [FieldLayout] -> [FieldLayout]
forall a. a -> [a] -> [a]
: [FieldLayout]
acc) [(Field, (Int, Int), Maybe Int)]
rest
Just Int
w -> do
Bool -> Either Text () -> Either Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8) (Either Text () -> Either Text ())
-> Either Text () -> Either Text ()
forall a b. (a -> b) -> a -> b
$
Text -> Either Text ()
forall a b. a -> Either a b
Left (Text
"Bitfield “" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Field -> Text
fieldName Field
f Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"” is wider than its type")
case Maybe (Int, Int, Int)
unit of
Just (Int
uOffset, Int
uSize, Int
used) | Int
uSize Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
size Bool -> Bool -> Bool
&& Int
used Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8 ->
Int
-> Maybe (Int, Int, Int)
-> Int
-> [FieldLayout]
-> [(Field, (Int, Int), Maybe Int)]
-> Either Text Layout
go Int
end ((Int, Int, Int) -> Maybe (Int, Int, Int)
forall a. a -> Maybe a
Just (Int
uOffset, Int
uSize, Int
used Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
w)) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
align Int
falign)
(Int -> Maybe Int -> FieldLayout
FieldLayout Int
uOffset (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Int -> Int -> Int
bitShift Int
size Int
used Int
w)) FieldLayout -> [FieldLayout] -> [FieldLayout]
forall a. a -> [a] -> [a]
: [FieldLayout]
acc) [(Field, (Int, Int), Maybe Int)]
rest
Maybe (Int, Int, Int)
_ -> do
let offset :: Int
offset = Int -> Int -> Int
roundUp Int
end Int
falign
Int
-> Maybe (Int, Int, Int)
-> Int
-> [FieldLayout]
-> [(Field, (Int, Int), Maybe Int)]
-> Either Text Layout
go (Int
offset Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
size) ((Int, Int, Int) -> Maybe (Int, Int, Int)
forall a. a -> Maybe a
Just (Int
offset, Int
size, Int
w)) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
align Int
falign)
(Int -> Maybe Int -> FieldLayout
FieldLayout Int
offset (Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Int -> Int -> Int
bitShift Int
size Int
0 Int
w)) FieldLayout -> [FieldLayout] -> [FieldLayout]
forall a. a -> [a] -> [a]
: [FieldLayout]
acc) [(Field, (Int, Int), Maybe Int)]
rest
bitShift :: Int -> Int -> Int -> Int
bitShift :: Int -> Int -> Int -> Int
bitShift Int
unitSize Int
pos Int
width
| Bool
bigEndian = Int
unitSize Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
width
| Bool
otherwise = Int
pos
bigEndian :: Bool
bigEndian :: Bool
bigEndian = (Int
1234 :: Int) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
4321
{-# LINE 321 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
msBitfields :: Bool
msBitfields :: Bool
msBitfields = [Char]
System.Info.os [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"mingw32"
pointerSizeAlign :: SizeAlign
pointerSizeAlign :: (Int, Int)
pointerSizeAlign = ((Int
8), Int
8)
{-# LINE 328 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
basicSizeAlign :: BasicType -> Either Text SizeAlign
basicSizeAlign :: BasicType -> Either Text (Int, Int)
basicSizeAlign BasicType
t = case BasicType
t of
BasicType
TBoolean -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right ((Int
4), Int
4)
{-# LINE 333 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TInt -> Right ((4), 4)
{-# LINE 334 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TUInt -> Right ((4), 4)
{-# LINE 335 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TLong -> Right ((8), 8)
{-# LINE 336 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TULong -> Right ((8), 8)
{-# LINE 337 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TInt8 -> Right ((1), 1)
{-# LINE 338 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TUInt8 -> Right ((1), 1)
{-# LINE 339 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TInt16 -> Right ((2), 2)
{-# LINE 340 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TUInt16 -> Right ((2), 2)
{-# LINE 341 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TInt32 -> Right ((4), 4)
{-# LINE 342 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TUInt32 -> Right ((4), 4)
{-# LINE 343 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TInt64 -> Right ((8), 8)
{-# LINE 344 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TUInt64 -> Right ((8), 8)
{-# LINE 345 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TFloat -> Right ((4), 4)
{-# LINE 346 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TDouble -> Right ((8), 8)
{-# LINE 347 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TUniChar -> Right ((4), 4)
{-# LINE 348 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TGType -> Right ((8), 8)
{-# LINE 349 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TUTF8 -> Right pointerSizeAlign
BasicType
TFileName -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right (Int, Int)
pointerSizeAlign
BasicType
TPtr -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right (Int, Int)
pointerSizeAlign
BasicType
TIntPtr -> (Int, Int) -> Either Text (Int, Int)
forall a b. b -> Either a b
Right ((Int
8), Int
8)
{-# LINE 353 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TUIntPtr -> Right ((8), 8)
{-# LINE 354 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TShort -> Right ((2), 2)
{-# LINE 355 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TUShort -> Right ((2), 2)
{-# LINE 356 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TSize -> Right ((8), 8)
{-# LINE 357 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
TSSize -> Right ((8), 8)
{-# LINE 358 "lib/Data/GI/CodeGen/StructLayout.hsc" #-}
Ttime_t -> Right (sizeOf (0 :: CTime), alignment (0 :: CTime))
BasicType
_ -> Text -> Either Text (Int, Int)
forall a b. a -> Either a b
Left (Text
"Unsupported basic type " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> BasicType -> Text
forall a. Show a => a -> Text
tshow BasicType
t)
roundUp :: Int -> Int -> Int
roundUp :: Int -> Int -> Int
roundUp Int
n Int
a = ((Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
a Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
a) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
a
tshow :: Show a => a -> Text
tshow :: forall a. Show a => a -> Text
tshow = [Char] -> Text
T.pack ([Char] -> Text) -> (a -> [Char]) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Char]
forall a. Show a => a -> [Char]
show