+\begin{code}
+-- | parse a file, returning the abstract syntax
+hscParse :: GhcMonad m =>
+ ModSummary
+ -> m (Located (HsModule RdrName))
+hscParse mod_summary = do
+ hsc_env <- getSession
+ let dflags = hsc_dflags hsc_env
+ src_filename = ms_hspp_file mod_summary
+ maybe_src_buf = ms_hspp_buf mod_summary
+ -------------------------- Parser ----------------
+ liftIO $ showPass dflags "Parser"
+ {-# SCC "Parser" #-} do
+
+ -- sometimes we already have the buffer in memory, perhaps
+ -- because we needed to parse the imports out of it, or get the
+ -- module name.
+ buf <- case maybe_src_buf of
+ Just b -> return b
+ Nothing -> liftIO $ hGetStringBuffer src_filename
+
+ let loc = mkSrcLoc (mkFastString src_filename) 1 1
+
+ case unP parseModule (mkPState buf loc dflags) of
+ PFailed span err ->
+ throwOneError (mkPlainErrMsg span err)
+
+ POk pst rdr_module -> do
+ let ms@(warns,errs) = getMessages pst
+ logWarnings warns
+ if errorsFound dflags ms then
+ liftIO $ throwIO $ mkSrcErr errs
+ else liftIO $ do
+ dumpIfSet_dyn dflags Opt_D_dump_parsed "Parser" (ppr rdr_module) ;
+ dumpIfSet_dyn dflags Opt_D_source_stats "Source Statistics"
+ (ppSourceStats False rdr_module) ;
+ return rdr_module
+ -- ToDo: free the string buffer later.
+
+-- | Rename and typecheck a module
+hscTypecheck :: GhcMonad m =>
+ ModSummary -> Located (HsModule RdrName)
+ -> m TcGblEnv
+hscTypecheck mod_summary rdr_module = do
+ hsc_env <- getSession
+ r <- {-# SCC "Typecheck-Rename" #-}
+ ioMsgMaybe $ tcRnModule hsc_env (ms_hsc_src mod_summary) False rdr_module
+ return r
+
+-- XXX: should this really be a Maybe X? Check under which circumstances this
+-- can become a Nothing and decide whether this should instead throw an
+-- exception/signal an error.
+type RenamedStuff =
+ (Maybe (HsGroup Name, [LImportDecl Name], Maybe [LIE Name],
+ Maybe LHsDocString))
+
+-- | Rename and typecheck a module, additionally returning the renamed syntax
+hscTypecheckRename ::
+ GhcMonad m =>
+ ModSummary -> Located (HsModule RdrName)
+ -> m (TcGblEnv, RenamedStuff)
+hscTypecheckRename mod_summary rdr_module = do
+ hsc_env <- getSession
+ tc_result
+ <- {-# SCC "Typecheck-Rename" #-}
+ ioMsgMaybe $ tcRnModule hsc_env (ms_hsc_src mod_summary) True rdr_module
+
+ let -- This 'do' is in the Maybe monad!
+ rn_info = do { decl <- tcg_rn_decls tc_result
+ ; let imports = tcg_rn_imports tc_result
+ exports = tcg_rn_exports tc_result
+ doc_hdr = tcg_doc_hdr tc_result
+ ; return (decl,imports,exports,doc_hdr) }
+
+ return (tc_result, rn_info)
+
+-- | Convert a typechecked module to Core
+hscDesugar :: GhcMonad m => ModSummary -> TcGblEnv -> m ModGuts
+hscDesugar mod_summary tc_result =
+ withSession $ \hsc_env ->
+ ioMsgMaybe $ deSugar hsc_env (ms_location mod_summary) tc_result
+
+-- | Make a 'ModIface' from the results of typechecking. Used when
+-- not optimising, and the interface doesn't need to contain any
+-- unfoldings or other cross-module optimisation info.
+-- ToDo: the old interface is only needed to get the version numbers,
+-- we should use fingerprint versions instead.
+makeSimpleIface :: GhcMonad m =>
+ Maybe ModIface -> TcGblEnv -> ModDetails
+ -> m (ModIface,Bool)
+makeSimpleIface maybe_old_iface tc_result details =
+ withSession $ \hsc_env ->
+ ioMsgMaybe $ mkIfaceTc hsc_env (fmap mi_iface_hash maybe_old_iface) details tc_result
+
+-- | Make a 'ModDetails' from the results of typechecking. Used when
+-- typechecking only, as opposed to full compilation.
+makeSimpleDetails :: GhcMonad m => TcGblEnv -> m ModDetails
+makeSimpleDetails tc_result =
+ withSession $ \hsc_env -> liftIO $ mkBootModDetailsTc hsc_env tc_result
+\end{code}
+