+renameModule dflags hit hst pcs this_module rdr_module
+ = renameSource dflags hit hst pcs this_module $
+ rename this_module rdr_module
+\end{code}
+
+
+\begin{code}
+renameStmt :: DynFlags
+ -> HomeIfaceTable -> HomeSymbolTable
+ -> PersistentCompilerState
+ -> Module -- current context (scope to compile in)
+ -> Module -- current module
+ -> LocalRdrEnv -- current context (temp bindings)
+ -> RdrNameStmt -- parsed stmt
+ -> IO ( PersistentCompilerState,
+ PrintUnqualified,
+ Maybe ([Name], (SyntaxMap, RenamedStmt, [RenamedHsDecl]))
+ )
+
+renameStmt dflags hit hst pcs scope_module this_module local_env stmt
+ = renameSource dflags hit hst pcs this_module $
+
+ -- Load the interface for the context module, so
+ -- that we can get its top-level lexical environment
+ -- Bale out if we fail to do this
+ loadInterface doc (moduleName scope_module) ImportByUser `thenRn` \ iface ->
+ let rdr_env = mi_globals iface
+ print_unqual = unQualInScope rdr_env
+ in
+ checkErrsRn `thenRn` \ no_errs_so_far ->
+ if not no_errs_so_far then
+ returnRn (print_unqual, Nothing)
+ else
+
+ -- Rename it
+ initRnMS rdr_env local_env emptyLocalFixityEnv CmdLineMode (
+ rnStmt stmt $ \ stmt' ->
+ returnRn (([], stmt'), emptyFVs)
+ ) `thenRn` \ ((binders, stmt), fvs) ->
+
+ -- Bale out if we fail
+ checkErrsRn `thenRn` \ no_errs_so_far ->
+ if not no_errs_so_far then
+ doDump [] stmt [] `thenRn_` returnRn (print_unqual, Nothing)
+ else
+
+ let filtered_fvs = fvs `delListFromNameSet` rdrEnvElts local_env in
+
+ -- Add implicit free vars, and close decls
+ addImplicitFVs rdr_env Nothing filtered_fvs
+ `thenRn` \ (slurp_fvs, syntax_map) ->
+ slurpImpDecls slurp_fvs `thenRn` \ decls ->
+
+ doDump binders stmt decls `thenRn_`
+ returnRn (print_unqual, Just (binders, (syntax_map, stmt, decls)))
+
+ where
+ doc = text "context for compiling expression"
+
+ doDump :: [Name] -> RenamedStmt -> [RenamedHsDecl] -> RnMG (Either IOError ())
+ doDump bndrs stmt decls
+ = getDOptsRn `thenRn` \ dflags ->
+ ioToRnM (dumpIfSet_dyn dflags Opt_D_dump_rn "Renamer:"
+ (vcat [text "Binders:" <+> ppr bndrs,
+ ppr stmt, text "",
+ vcat (map ppr decls)]))
+\end{code}
+
+
+%*********************************************************
+%* *
+\subsection{The main function: rename}
+%* *
+%*********************************************************
+
+\begin{code}
+renameSource :: DynFlags
+ -> HomeIfaceTable -> HomeSymbolTable
+ -> PersistentCompilerState
+ -> Module
+ -> RnMG (PrintUnqualified, Maybe r)
+ -> IO (PersistentCompilerState, PrintUnqualified, Maybe r)
+ -- Nothing => some error occurred in the renamer
+
+renameSource dflags hit hst old_pcs this_module thing_inside
+ = do { showPass dflags "Renamer"
+
+ -- Initialise the renamer monad
+ ; (new_pcs, msgs, (print_unqual, maybe_rn_stuff))
+ <- initRn dflags hit hst old_pcs this_module thing_inside
+
+ -- Print errors from renaming
+ ; printErrorsAndWarnings print_unqual msgs ;
+
+ -- Return results. No harm in updating the PCS
+ ; if errorsFound msgs then
+ return (new_pcs, print_unqual, Nothing)
+ else
+ return (new_pcs, print_unqual, maybe_rn_stuff)