-import Panic
-import Bag ( emptyBag, listToBag )
-
-import Distribution.Compiler
-
-import EXCEPTION ( throwDyn )
-import IO
-import List
-
-#if __GLASGOW_HASKELL__ >= 601
-import System.IO ( openBinaryFile )
-#else
-import IOExts ( openFileEx, IOModeEx(..) )
-#endif
-
-#if __GLASGOW_HASKELL__ < 601
-openBinaryFile fp mode = openFileEx fp (BinaryMode mode)
-#endif
-
--- getImportsFromFile is careful to close the file afterwards, otherwise
--- we can end up with a large number of open handles before the garbage
--- collector gets around to closing them.
-getImportsFromFile :: DynFlags -> FilePath
- -> IO ([Located ModuleName], [Located ModuleName], Located ModuleName)
-getImportsFromFile dflags filename = do
- buf <- hGetStringBuffer filename
- getImports dflags buf filename
-
-getImports :: DynFlags -> StringBuffer -> FilePath
- -> IO ([Located ModuleName], [Located ModuleName], Located ModuleName)
-getImports dflags buf filename = do
+import Maybes
+import Bag ( emptyBag, listToBag, unitBag )
+
+import MonadUtils ( MonadIO )
+import Exception
+import Control.Monad
+import System.IO
+import Data.List
+
+------------------------------------------------------------------------------
+
+-- | Parse the imports of a source file.
+--
+-- Throws a 'SourceError' if parsing fails.
+getImports :: GhcMonad m =>
+ DynFlags
+ -> StringBuffer -- ^ Parse this.
+ -> FilePath -- ^ Filename the buffer came from. Used for
+ -- reporting parse error locations.
+ -> FilePath -- ^ The original source filename (used for locations
+ -- in the function result)
+ -> m ([Located (ImportDecl RdrName)], [Located (ImportDecl RdrName)], Located ModuleName)
+ -- ^ The source imports, normal imports, and the module name.
+getImports dflags buf filename source_filename = do