Skip to content

Commit

Permalink
Fix handling of void parameters passed to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lehmacdj committed Feb 5, 2025
1 parent 763fd98 commit 24316d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions inline-c/src/Language/C/Inline/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,17 @@ convertType purity cTypes = runMaybeT . go
-- We cannot convert standalone prototypes
mzero

buildArr :: [TH.Type] -> TH.Type -> TH.Q TH.Type
buildArr [] hsRetType =
case purity of
Pure -> [t| $(return hsRetType) |]
IO -> [t| IO $(return hsRetType) |]
buildArr [TH.TupleT 0] hsRetType =
case purity of
Pure -> [t| $(return hsRetType) |]
IO -> [t| IO $(return hsRetType) |]
buildArr (TH.TupleT 0 : hsPars) hsRetType =
fail "C function can only have void parameter as the only parameter"
buildArr (hsPar : hsPars) hsRetType =
[t| $(return hsPar) -> $(buildArr hsPars hsRetType) |]

Expand Down
4 changes: 4 additions & 0 deletions inline-c/test/Language/C/Inline/ContextSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ spec = do
shouldBeType
(cty "int (*f)(unsigned char, float)")
[t| FunPtr (CUChar -> CFloat -> IO CInt) |]
Hspec.it "converts void parameter function pointer" $ do
shouldBeType
(cty "void (*f)(void)")
[t| FunPtr (IO ()) |]
Hspec.it "converts complicated function pointers (1)" $ do
-- pointer to function returning pointer to function returning int
shouldBeType
Expand Down

0 comments on commit 24316d3

Please sign in to comment.