Skip to content

Commit

Permalink
Implement GenTests defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
TravisCardwell committed Jan 9, 2025
1 parent fe6fbe6 commit 66375e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 56 deletions.
31 changes: 7 additions & 24 deletions hs-bindgen/src/HsBindgen/GenTests/C.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module HsBindgen.GenTests.C (
import Data.Char qualified as Char
import Data.List qualified as List
import Data.Text qualified as T
import Data.Typeable (typeOf)
import Data.Vec.Lazy qualified as Vec
import System.FilePath qualified as FilePath

Expand Down Expand Up @@ -62,10 +63,6 @@ genTestsC cTestHeaderPath cTestSourcePath lineLength cHeaderPath decls = do

getTestHeaderDecls :: CFunPrefix -> Hs.Decl -> [CTestDecl]
getTestHeaderDecls cFunPrefix = \case
Hs.DeclData{} -> []
Hs.DeclEmpty{} -> []
Hs.DeclNewtype{} -> []
Hs.DeclPatSyn{} -> []
Hs.DeclInstance instanceDecl -> case instanceDecl of
Hs.InstanceStorable Hs.Struct{..} _storableInstance ->
case getStructCTypeSpelling structOrigin of
Expand All @@ -77,17 +74,11 @@ getTestHeaderDecls cFunPrefix = \case
, CTestPreturbDecl cFunPrefix structName cts
]
Nothing -> []
Hs.InstanceHasFLAM {} -> []
Hs.DeclNewtypeInstance{} -> []
Hs.DeclForeignImport{} -> []
Hs.DeclVar{} -> []
_otherwise -> []
_otherwise -> []

getTestSourceDefns :: CFunPrefix -> Hs.Decl -> [CTestDefn]
getTestSourceDefns cFunPrefix = \case
Hs.DeclData{} -> []
Hs.DeclEmpty{} -> []
Hs.DeclNewtype{} -> []
Hs.DeclPatSyn{} -> []
Hs.DeclInstance instanceDecl -> case instanceDecl of
Hs.InstanceStorable Hs.Struct{..} _storableInstance ->
case getStructCTypeSpelling structOrigin of
Expand All @@ -100,10 +91,8 @@ getTestSourceDefns cFunPrefix = \case
, CTestPreturbDefn cFunPrefix structName cts fieldPs
]
Nothing -> []
Hs.InstanceHasFLAM {} -> []
Hs.DeclNewtypeInstance{} -> []
Hs.DeclForeignImport{} -> []
Hs.DeclVar{} -> []
_otherwise -> []
_otherwise -> []

getStructCTypeSpelling :: Hs.StructOrigin -> Maybe CTypeSpelling
getStructCTypeSpelling = \case
Expand All @@ -125,9 +114,7 @@ getFieldP Hs.Field{..} = (cName, hsTypeName)

hsTypeName :: HsTypeName
hsTypeName = case fieldType of
Hs.HsType{} -> error "not supported: HsType"
Hs.HsPrimType hsPrimType -> case hsPrimType of
HsT.HsPrimVoid -> error "not supported: HsPrimVoid"
HsT.HsPrimCChar -> "CChar"
HsT.HsPrimCSChar -> "CSChar"
HsT.HsPrimCUChar -> "CUChar"
Expand All @@ -142,12 +129,8 @@ getFieldP Hs.Field{..} = (cName, hsTypeName)
HsT.HsPrimCBool -> "CBool"
HsT.HsPrimCFloat -> "CFloat"
HsT.HsPrimCDouble -> "CDouble"
Hs.HsTypRef{} -> error "not supported: HsTypRef"
Hs.HsConstArray{} -> error "not supported: HsConstArray"
Hs.HsPtr{} -> error "not supported: HsPtr"
Hs.HsFunPtr{} -> error "not supported: HsFunPtr"
Hs.HsIO{} -> error "not supported: HsIO"
Hs.HsFun{} -> error "not supported: HsFun"
x -> error $ "not supported: " ++ show (typeOf x)
x -> error $ "not supported: " ++ show (typeOf x)

{-------------------------------------------------------------------------------
AST
Expand Down
40 changes: 8 additions & 32 deletions hs-bindgen/src/HsBindgen/GenTests/Hs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ genTestsHs

getFfiFunctions :: IncludeFile -> CFunPrefix -> Hs.Decl -> [FfiFunction]
getFfiFunctions includeFile cFunPrefix = \case
Hs.DeclData{} -> []
Hs.DeclEmpty{} -> []
Hs.DeclNewtype{} -> []
Hs.DeclPatSyn{} -> []
Hs.DeclInstance instanceDecl -> case instanceDecl of
Hs.InstanceStorable Hs.Struct{..} _storableInstance ->
[ FfiSizeof includeFile cFunPrefix structName
Expand All @@ -104,17 +100,11 @@ getFfiFunctions includeFile cFunPrefix = \case
, FfiGenSeqC includeFile cFunPrefix structName
, FfiPreturb includeFile cFunPrefix structName
]
Hs.InstanceHasFLAM {} -> []
Hs.DeclNewtypeInstance{} -> []
Hs.DeclForeignImport{} -> []
Hs.DeclVar{} -> []
_otherwise -> []
_otherwise -> []

getOrphanInstances :: Hs.Decl -> [OrphanInstance]
getOrphanInstances = \case
Hs.DeclData{} -> []
Hs.DeclEmpty{} -> []
Hs.DeclNewtype{} -> []
Hs.DeclPatSyn{} -> []
Hs.DeclInstance instanceDecl -> case instanceDecl of
Hs.InstanceStorable Hs.Struct{..} _storableInstance ->
let fieldNames = Hs.fieldName <$> Vec.toList structFields
Expand All @@ -123,37 +113,23 @@ getOrphanInstances = \case
, PreturbInstance structName structConstr fieldNames
, SameSemanticsInstance structName fieldNames
]
Hs.InstanceHasFLAM {} -> []
Hs.DeclNewtypeInstance{} -> []
Hs.DeclForeignImport{} -> []
Hs.DeclVar{} -> []
_otherwise -> []
_otherwise -> []

getTypeTests :: Hs.Decl -> [TypeTest]
getTypeTests = \case
Hs.DeclData{} -> []
Hs.DeclEmpty{} -> []
Hs.DeclNewtype{} -> []
Hs.DeclPatSyn{} -> []
Hs.DeclInstance instanceDecl -> case instanceDecl of
Hs.InstanceStorable Hs.Struct{..} _storableInstance ->
[TypeTest structName]
Hs.InstanceHasFLAM {} -> []
Hs.DeclNewtypeInstance{} -> []
Hs.DeclForeignImport{} -> []
Hs.DeclVar{} -> []
_otherwise -> []
_otherwise -> []

getTestsFunNames :: Hs.Decl -> [HsName NsTypeConstr]
getTestsFunNames = \case
Hs.DeclData{} -> []
Hs.DeclEmpty{} -> []
Hs.DeclNewtype{} -> []
Hs.DeclPatSyn{} -> []
Hs.DeclInstance instanceDecl -> case instanceDecl of
Hs.InstanceStorable Hs.Struct{..} _storableInstance -> [structName]
Hs.InstanceHasFLAM {} -> []
Hs.DeclNewtypeInstance{} -> []
Hs.DeclForeignImport{} -> []
Hs.DeclVar{} -> []
_otherwise -> []
_otherwise -> []

{-------------------------------------------------------------------------------
AST
Expand Down

0 comments on commit 66375e5

Please sign in to comment.