module LLVM.Internal.ObjectFile where
import Control.Exception (bracket)
import Control.Monad.IO.Class
import Control.Monad.AnyCont
import Foreign.Ptr
import LLVM.Prelude
import LLVM.Internal.Coding
import LLVM.Internal.MemoryBuffer
import qualified LLVM.Internal.FFI.LLVMCTypes as FFI
import qualified LLVM.Internal.FFI.ObjectFile as FFI
newtype ObjectFile = ObjectFile (Ptr FFI.ObjectFile)
disposeObjectFile :: ObjectFile -> IO ()
disposeObjectFile (ObjectFile obj) = FFI.disposeObjectFile obj
createObjectFile :: FilePath -> IO ObjectFile
createObjectFile path = flip runAnyContT return $ do
FFI.OwnerTransfered buf <- encodeM (File path)
obj <- liftIO $ FFI.createObjectFile buf
when (obj == nullPtr) $ error "LLVMCreateObjectFile returned a null pointer."
return (ObjectFile obj)
withObjectFile :: FilePath -> (ObjectFile -> IO a) -> IO a
withObjectFile f = bracket (createObjectFile f) disposeObjectFile