c2hsc 0.3.0 → 0.3.1
raw patch · 3 files changed
+57/−41 lines, 3 files
Files
- Main.hs +51/−35
- README.md +5/−5
- c2hsc.cabal +1/−1
Main.hs view
@@ -11,7 +11,6 @@ import qualified Data.Map as M import Data.Maybe import Data.Traversable---import Debug.Trace import Language.C.Data.Ident import Language.C.Data.InputStream import Language.C.Data.Node@@ -76,6 +75,9 @@ main :: IO () main = getArgs >>= runArgs +smokeTest :: IO ()+smokeTest = runArgs ["--prefix=Test", "--stdout", "test/smoke.h"]+ runArgs :: [String] -> IO() runArgs mainArgs = do opts <- withArgs (if null mainArgs then ["--help"] else mainArgs)@@ -96,7 +98,7 @@ parseFile gccPath opts = for_ (files opts) $ \fileName -> do result <- runPreprocessor (newGCC gccPath)- (rawCppArgs [(cppopts opts)] fileName)+ (rawCppArgs [cppopts opts] fileName) case result of Left err -> error $ "Failed to run cpp: " ++ show err Right stream -> do@@ -154,8 +156,14 @@ unless (useStdout opts) $ hClose handlec putStrLn $ "Wrote " ++ targetc - where capitalize [] = []- capitalize (x:xs) = toTitle x : xs+capitalize :: String -> String+capitalize [] = []+capitalize (x:xs) = toTitle x : camelCase xs++camelCase :: String -> String+camelCase [] = []+camelCase ('_':xs) = capitalize xs+camelCase (x:xs) = x : camelCase xs ------------------------------- PURE FUNCTIONS ------------------------------- @@ -230,37 +238,41 @@ appendNode :: FilePath -> CExtDecl -> Output () -appendNode fp dx@(CDeclExt (CDecl declSpecs items _)) =- for_ items $ \(declrtr, _, _) ->- for_ (splitDecl declrtr) $ \(d, ddrs, nm) ->- case ddrs of- CFunDeclr (Right (_, _)) _ _ : _ ->- when (declInFile fp dx) $- appendFunc "#ccall" declSpecs d+appendNode fp dx@(CDeclExt (CDecl declSpecs items _)) = do+ case items of+ [] -> do+ appendHsc $ "{- " ++ P.render (pretty dx) ++ " -}"+ appendType declSpecs "" - _ -> do- when (declInFile fp dx) $ do- appendHsc $ "{- " ++ P.render (pretty dx) ++ " -}"- appendType declSpecs nm+ _ ->+ for_ items $ \(declrtr, _, _) ->+ for_ (splitDecl declrtr) $ \(d, ddrs, nm) ->+ case ddrs of+ CFunDeclr (Right (_, _)) _ _ : _ ->+ when (declInFile fp dx) $+ appendFunc "#ccall" declSpecs d - -- If the type is a typedef, record the equivalence so we can look- -- it up later- case head declSpecs of- CStorageSpec (CTypedef _) -> do- -- jww (2012-09-04): Types which are typedefs of functions- -- pointers are not working, since declSpecTypeName only gives- -- the function return type, not the function type- dname <- declSpecTypeName declSpecs- case dname of- "" -> return ()- _ -> defineType nm dname- _ -> return ()+ _ -> do+ when (declInFile fp dx) $ do+ appendHsc $ "{- " ++ P.render (pretty dx) ++ " -}"+ appendType declSpecs nm - where splitDecl declrtr = do- -- Take advantage of the Maybe monad to save us some effort+ -- If the type is a typedef, record the equivalence so we can+ -- look it up later+ case head declSpecs of+ CStorageSpec (CTypedef _) -> do+ -- jww (2012-09-04): Types which are typedefs of functions+ -- pointers are not working, since declSpecTypeName only gives+ -- the function return type, not the function type+ dname <- declSpecTypeName declSpecs+ case dname of+ "" -> return ()+ _ -> defineType nm dname+ _ -> return ()++ where splitDecl declrtr = do -- in the Maybe Monad d@(CDeclr ident ddrs _ _ _) <- declrtr- (Ident nm _ _) <- ident- return (d, ddrs, nm)+ return (d, ddrs, case ident of Just (Ident nm _ _) -> nm; _ -> "") appendNode fp dx@(CFDefExt (CFunDef declSpecs declrtr _ _ _)) = -- Assume functions defined in headers are inline functions@@ -325,8 +337,14 @@ appendHsc $ "#starttype " ++ name' for_ xs $ \x -> for_ (cdeclName x) $ \declName -> do- tname <- cdeclTypeName x- appendHsc $ "#field " ++ declName ++ " , " ++ tname+ let (CDecl declSpecs' ((Just y, _, _):_) _) = x+ case y of+ (CDeclr _ (CArrDeclr {}:zs) _ _ _) -> do+ tname <- derDeclrTypeName declSpecs' zs+ appendHsc $ "#array_field " ++ declName ++ " , " ++ tname+ _ -> do+ tname <- cdeclTypeName x+ appendHsc $ "#field " ++ declName ++ " , " ++ tname appendHsc "#stoptype" appendType' (CTypeSpec (CEnumType (CEnum ident defs _ _) _)) = do@@ -529,7 +547,5 @@ cTypeName (CTypeOfType _ _) _ = return "" cTypeName _ _ = return ""---- runArgs ["--prefix=C2Hsc.Smoke", "--cppopts=-U__BLOCKS__", "test/smoke.h"] -- c2hsc.hs
README.md view
@@ -11,13 +11,13 @@ Known issues: + - Pointers to "struct foo" are being rendered as Ptr () [void *]+ - Function pointers of void return type are rendered incorrectlyb+ - `const` is being dropped from BC_INLINE macros+ - Handle type synonyms (output them as type a = b)+ - Global variables are not being emitted - Varargs functions do not translate- - Arrays are not handled at all (use #array_field $name , $type)- - Handle type synonyms- - Unnamed enums are not being emitted - Inline helper generator outputs the wrong headers- - Files named foo_bar should become FooBar- - Global variables are not being emitted Also, please note that this tool will never be 100% accurate. It cannot translate macros, or anything related to the preprocessor, for example. It
c2hsc.cabal view
@@ -1,6 +1,6 @@ Name: c2hsc -Version: 0.3.0+Version: 0.3.1 Synopsis: Convert C API header files to .hsc and .hsc.helper.c files Description: Convert C API header files to .hsc and .hsc.helper.c files