{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Capnp.Fields
( HasField(..)
, Field(..)
, FieldLoc(..)
, DataFieldLoc(..)
, FieldKind(..)
, HasUnion(..)
, Variant(..)
, HasVariant(..)
) where
import Capnp.Bits
import Data.Word
import GHC.OverloadedLabels (IsLabel(..))
import GHC.TypeLits (Symbol)
import qualified Capnp.Classes as C
import qualified Capnp.Message as M
import qualified Capnp.New.Classes as NC
import qualified Capnp.Repr as R
import qualified Capnp.Untyped as U
data FieldKind
= Slot
| Group
deriving(Int -> FieldKind -> ShowS
[FieldKind] -> ShowS
FieldKind -> String
(Int -> FieldKind -> ShowS)
-> (FieldKind -> String)
-> ([FieldKind] -> ShowS)
-> Show FieldKind
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FieldKind] -> ShowS
$cshowList :: [FieldKind] -> ShowS
show :: FieldKind -> String
$cshow :: FieldKind -> String
showsPrec :: Int -> FieldKind -> ShowS
$cshowsPrec :: Int -> FieldKind -> ShowS
Show, ReadPrec [FieldKind]
ReadPrec FieldKind
Int -> ReadS FieldKind
ReadS [FieldKind]
(Int -> ReadS FieldKind)
-> ReadS [FieldKind]
-> ReadPrec FieldKind
-> ReadPrec [FieldKind]
-> Read FieldKind
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [FieldKind]
$creadListPrec :: ReadPrec [FieldKind]
readPrec :: ReadPrec FieldKind
$creadPrec :: ReadPrec FieldKind
readList :: ReadS [FieldKind]
$creadList :: ReadS [FieldKind]
readsPrec :: Int -> ReadS FieldKind
$creadsPrec :: Int -> ReadS FieldKind
Read, FieldKind -> FieldKind -> Bool
(FieldKind -> FieldKind -> Bool)
-> (FieldKind -> FieldKind -> Bool) -> Eq FieldKind
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FieldKind -> FieldKind -> Bool
$c/= :: FieldKind -> FieldKind -> Bool
== :: FieldKind -> FieldKind -> Bool
$c== :: FieldKind -> FieldKind -> Bool
Eq)
newtype Field (k :: FieldKind) a b = Field (FieldLoc k (R.ReprFor b))
data FieldLoc (k :: FieldKind) (r :: R.Repr) where
GroupField :: FieldLoc 'Group ('R.Ptr ('Just 'R.Struct))
PtrField :: R.IsPtrRepr a => Word16 -> FieldLoc 'Slot ('R.Ptr a)
DataField :: C.IsWord (R.UntypedData a) => DataFieldLoc a -> FieldLoc 'Slot ('R.Data a)
VoidField :: FieldLoc 'Slot ('R.Data 'R.Sz0)
data DataFieldLoc (sz :: R.DataSz) = DataFieldLoc
{ DataFieldLoc sz -> BitCount
shift :: !BitCount
, DataFieldLoc sz -> Word16
index :: !Word16
, DataFieldLoc sz -> Word64
mask :: !Word64
, DataFieldLoc sz -> Word64
defaultValue :: !Word64
}
class R.IsStruct a => HasUnion a where
unionField :: Field 'Slot a Word16
data Which a
data RawWhich (mut :: M.Mutability) a
internalWhich :: U.ReadCtx m mut => Word16 -> R.Raw mut a -> m (RawWhich mut a)
type instance R.ReprFor (Which a) = 'R.Ptr ('Just 'R.Struct)
instance (NC.Allocate a, HasUnion a, R.IsStruct (Which a)) => NC.Allocate (Which a) where
type AllocHint (Which a) = NC.AllocHint a
new :: AllocHint (Which a)
-> Message ('Mut s) -> m (Raw ('Mut s) (Which a))
new AllocHint (Which a)
hint Message ('Mut s)
msg = do
R.Raw Untyped ('Mut s) (ReprFor a)
struct <- AllocHint a -> Message ('Mut s) -> m (Raw ('Mut s) a)
forall a (m :: * -> *) s.
(Allocate a, RWCtx m s) =>
AllocHint a -> Message ('Mut s) -> m (Raw ('Mut s) a)
NC.new @a AllocHint a
AllocHint (Which a)
hint Message ('Mut s)
msg
Raw ('Mut s) (Which a) -> m (Raw ('Mut s) (Which a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Untyped ('Mut s) (ReprFor (Which a)) -> Raw ('Mut s) (Which a)
forall (mut :: Mutability) a. Untyped mut (ReprFor a) -> Raw mut a
R.Raw Untyped ('Mut s) (ReprFor a)
Untyped ('Mut s) (ReprFor (Which a))
struct)
instance
( NC.Allocate (Which a)
, NC.AllocHint (Which a) ~ ()
, NC.Parse (Which a) p
) => NC.EstimateAlloc (Which a) p
data Variant (k :: FieldKind) a b = Variant
{ Variant k a b -> Field k a b
field :: !(Field k a b)
, Variant k a b -> Word16
tagValue :: !Word16
}
class R.IsStruct a => HasField (name :: Symbol) k a b | a name -> k b where
fieldByLabel :: Field k a b
instance HasField name k a b => IsLabel name (Field k a b) where
fromLabel :: Field k a b
fromLabel = HasField name k a b => Field k a b
forall (name :: Symbol) (k :: FieldKind) a b.
HasField name k a b =>
Field k a b
fieldByLabel @name @k @a @b
class HasUnion a => HasVariant (name :: Symbol) k a b | a name -> k b where
variantByLabel :: Variant k a b
instance HasVariant name k a b => IsLabel name (Variant k a b) where
fromLabel :: Variant k a b
fromLabel = HasVariant name k a b => Variant k a b
forall (name :: Symbol) (k :: FieldKind) a b.
HasVariant name k a b =>
Variant k a b
variantByLabel @name @k @a @b