\section[Rename]{Renaming and dependency analysis passes}
\begin{code}
-module Rename ( renameModule, renameStmt, closeIfaceDecls, checkOldIface ) where
+module Rename (
+ renameModule, renameStmt, renameRdrName,
+ closeIfaceDecls, checkOldIface
+ ) where
#include "HsVersions.h"
RdrNameStmt
)
import RnHsSyn ( RenamedHsDecl, RenamedTyClDecl, RenamedRuleDecl, RenamedInstDecl,
- extractHsTyNames, RenamedStmt,
+ RenamedStmt,
instDeclFVs, tyClDeclFVs, ruleDeclFVs
)
closeDecls,
RecompileRequired, outOfDate, recompileRequired
)
-import RnHiFiles ( readIface, removeContext, loadInterface,
+import RnHiFiles ( readIface, loadInterface,
loadExports, loadFixDecls, loadDeprecs,
)
-import MkIface ( pprUsage )
import RnEnv ( availsToNameSet, mkIfaceGlobalRdrEnv,
emptyAvailEnv, unitAvailEnv, availEnvElts,
plusAvailEnv, groupAvails, warnUnusedImports,
warnUnusedLocalBinds, warnUnusedModules,
- lookupSrcName, addImplicitFVs,
- newGlobalName, unQualInScope,, ubiquitousNames
+ lookupSrcName, getImplicitStmtFVs,
+ getImplicitModuleFVs, newGlobalName, unQualInScope,
+ ubiquitousNames, lookupOccRn
)
import Module ( Module, ModuleName, WhereFrom(..),
moduleNameUserString, moduleName,
moduleEnvElts
)
-import Name ( Name, nameIsLocalOrFrom, nameModule )
-import Name ( mkNameEnv, nameEnvElts, extendNameEnv )
-import RdrName ( foldRdrEnv, isQual )
+import Name ( Name, nameModule )
+import NameEnv
import NameSet
-import PrelNames ( SyntaxMap, pRELUDE_Name )
+import RdrName ( foldRdrEnv, isQual )
+import PrelNames ( pRELUDE_Name )
import ErrUtils ( dumpIfSet, dumpIfSet_dyn, showPass,
printErrorsAndWarnings, errorsFound )
import Bag ( bagToList )
import Maybes ( maybeToBool, catMaybes )
import Outputable
import IO ( openFile, IOMode(..) )
-import HscTypes ( PersistentCompilerState, HomeIfaceTable, HomeSymbolTable,
- ModIface(..), WhatsImported(..),
- VersionInfo(..), ImportVersion, IsExported,
- IfaceDecls, mkIfaceDecls, dcl_tycl, dcl_rules, dcl_insts,
- GlobalRdrEnv, GlobalRdrElt(..), pprGlobalRdrEnv,
- AvailEnv, GenAvailInfo(..), AvailInfo, Avails,
- Provenance(..), ImportReason(..), initialVersionInfo,
- Deprecations(..),
- LocalRdrEnv
- )
-import CmStaticInfo ( GhciMode(..) )
+import HscTypes -- lots of it
import List ( partition, nub )
\end{code}
%*********************************************************
%* *
-\subsection{The two main wrappers}
+\subsection{The main wrappers}
%* *
%*********************************************************
-> PersistentCompilerState
-> Module -> RdrNameHsModule
-> IO (PersistentCompilerState, PrintUnqualified,
- Maybe (IsExported, ModIface, (SyntaxMap, [RenamedHsDecl])))
+ Maybe (IsExported, ModIface, [RenamedHsDecl]))
-- Nothing => some error occurred in the renamer
renameModule dflags hit hst pcs this_module rdr_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)
+ -> InteractiveContext
-> RdrNameStmt -- parsed stmt
-> IO ( PersistentCompilerState,
PrintUnqualified,
- Maybe ([Name], (SyntaxMap, RenamedStmt, [RenamedHsDecl]))
+ Maybe ([Name], (RenamedStmt, [RenamedHsDecl]))
)
-renameStmt dflags hit hst pcs scope_module this_module local_env stmt
+renameStmt dflags hit hst pcs this_module ic stmt
= renameSource dflags hit hst pcs this_module $
+ extendTypeEnvRn (ic_type_env ic) $
- -- 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
+ -- load the context module
+ loadContextModule (ic_module ic) $ \ (rdr_env, print_unqual) ->
- -- Rename it
- initRnMS rdr_env local_env emptyLocalFixityEnv CmdLineMode (
+ -- Rename the stmt
+ initRnMS rdr_env (ic_rn_env ic) emptyLocalFixityEnv CmdLineMode (
rnStmt stmt $ \ stmt' ->
returnRn (([], stmt'), emptyFVs)
- ) `thenRn` \ ((binders, stmt), fvs) ->
+ ) `thenRn` \ ((binders, stmt), fvs) ->
-- Bale out if we fail
- checkErrsRn `thenRn` \ no_errs_so_far ->
+ checkErrsRn `thenRn` \ no_errs_so_far ->
if not no_errs_so_far then
- doDump [] stmt [] `thenRn_` returnRn (print_unqual, Nothing)
+ doDump dflags [] 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)))
+ getImplicitStmtFVs `thenRn` \ implicit_fvs ->
+ slurpImpDecls (fvs `plusFV` implicit_fvs) `thenRn` \ decls ->
+ -- NB: an earlier version deleted (rdrEnvElts local_env) from
+ -- the fvs. But (a) that isn't necessary, because previously
+ -- bound things in the local_env will be in the TypeEnv, and
+ -- the renamer doesn't re-slurp such things, and
+ -- (b) it's WRONG to delete them. Consider in GHCi:
+ -- Mod> let x = e :: T
+ -- Mod> let y = x + 3
+ -- We need to pass 'x' among the fvs to slurpImpDecls, so that
+ -- the latter can see that T is a gate, and hence import the Num T
+ -- instance decl. (See the InTypEnv case in RnIfaces.slurpSourceRefs.)
+
+ doDump dflags binders stmt decls `thenRn_`
+ returnRn (print_unqual, Just (binders, (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:"
+ doDump :: DynFlags -> [Name] -> RenamedStmt -> [RenamedHsDecl]
+ -> RnMG (Either IOError ())
+ doDump dflags bndrs stmt decls
+ = ioToRnM (dumpIfSet_dyn dflags Opt_D_dump_rn "Renamer:"
(vcat [text "Binders:" <+> ppr bndrs,
ppr stmt, text "",
vcat (map ppr decls)]))
-\end{code}
+renameRdrName
+ :: DynFlags
+ -> HomeIfaceTable -> HomeSymbolTable
+ -> PersistentCompilerState
+ -> Module -- current module
+ -> InteractiveContext
+ -> [RdrName] -- name to rename
+ -> IO ( PersistentCompilerState,
+ PrintUnqualified,
+ Maybe ([Name], [RenamedHsDecl])
+ )
+
+renameRdrName dflags hit hst pcs this_module ic rdr_names =
+ renameSource dflags hit hst pcs this_module $
+ extendTypeEnvRn (ic_type_env ic) $
+ loadContextModule (ic_module ic) $ \ (rdr_env, print_unqual) ->
+
+ -- rename the rdr_name
+ initRnMS rdr_env (ic_rn_env ic) emptyLocalFixityEnv CmdLineMode
+ (mapRn (tryRn.lookupOccRn) rdr_names) `thenRn` \ maybe_names ->
+ let
+ ok_names = [ a | Right a <- maybe_names ]
+ in
+ if null ok_names
+ then let errs = head [ e | Left e <- maybe_names ]
+ in setErrsRn errs `thenRn_`
+ doDump dflags ok_names [] `thenRn_`
+ returnRn (print_unqual, Nothing)
+ else
+
+ slurpImpDecls (mkNameSet ok_names) `thenRn` \ decls ->
+
+ doDump dflags ok_names decls `thenRn_`
+ returnRn (print_unqual, Just (ok_names, decls))
+ where
+ doDump :: DynFlags -> [Name] -> [RenamedHsDecl] -> RnMG (Either IOError ())
+ doDump dflags names decls
+ = ioToRnM (dumpIfSet_dyn dflags Opt_D_dump_rn "Renamer:"
+ (vcat [ppr names, text "",
+ vcat (map ppr decls)]))
+
+
+-- 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
+loadContextModule scope_module thing_inside
+ = let doc = text "context for compiling expression"
+ in
+ 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
+ thing_inside (rdr_env, print_unqual)
+\end{code}
+
%*********************************************************
%* *
\subsection{The main function: rename}
\begin{code}
rename :: Module -> RdrNameHsModule
- -> RnMG (PrintUnqualified, Maybe (IsExported, ModIface, (SyntaxMap, [RenamedHsDecl])))
+ -> RnMG (PrintUnqualified, Maybe (IsExported, ModIface, [RenamedHsDecl]))
rename this_module contents@(HsModule _ _ exports imports local_decls mod_deprec loc)
= pushSrcLocRn loc $
else
-- SLURP IN ALL THE NEEDED DECLARATIONS
- addImplicitFVs gbl_env (Just (mod_name, rn_local_decls))
- source_fvs `thenRn` \ (slurp_fvs, sugar_map) ->
- traceRn (text "Source FVs:" <+> fsep (map ppr (nameSetToList slurp_fvs))) `thenRn_`
- slurpImpDecls slurp_fvs `thenRn` \ rn_imp_decls ->
+ -- Find out what re-bindable names to use for desugaring
+ getImplicitModuleFVs mod_name rn_local_decls `thenRn` \ implicit_fvs ->
+ let
+ export_fvs = availsToNameSet export_avails
+ source_fvs2 = source_fvs `plusFV` export_fvs
+ -- The export_fvs make the exported names look just as if they
+ -- occurred in the source program. For the reasoning, see the
+ -- comments with RnIfaces.mkImportInfo
+ -- It also helps reportUnusedNames, which of course must not complain
+ -- that 'f' isn't mentioned if it is mentioned in the export list
+
+ source_fvs3 = implicit_fvs `plusFV` source_fvs2
+ -- It's important to do the "plus" this way round, so that
+ -- when compiling the prelude, locally-defined (), Bool, etc
+ -- override the implicit ones.
+ in
+ traceRn (text "Source FVs:" <+> fsep (map ppr (nameSetToList source_fvs3))) `thenRn_`
+ slurpImpDecls source_fvs3 `thenRn` \ rn_imp_decls ->
rnDump rn_imp_decls rn_local_decls `thenRn_`
-- GENERATE THE VERSION/USAGE INFO
- mkImportInfo mod_name imports `thenRn` \ my_usages ->
+ mkImportInfo mod_name imports `thenRn` \ my_usages ->
-- BUILD THE MODULE INTERFACE
let
my_exports = groupAvails this_module export_avails
final_decls = rn_local_decls ++ rn_imp_decls
- is_orphan = any (isOrphanDecl this_module) rn_local_decls
mod_iface = ModIface { mi_module = this_module,
mi_version = initialVersionInfo,
mi_usages = my_usages,
mi_boot = False,
- mi_orphan = is_orphan,
+ mi_orphan = panic "is_orphan",
mi_exports = my_exports,
mi_globals = gbl_env,
mi_fixities = fixities,
-- REPORT UNUSED NAMES, AND DEBUG DUMP
reportUnusedNames mod_iface print_unqualified
imports global_avail_env
- source_fvs export_avails rn_imp_decls `thenRn_`
+ source_fvs2 rn_imp_decls `thenRn_`
+ -- NB: source_fvs2: include exports (else we get bogus
+ -- warnings of unused things) but not implicit FVs.
- returnRn (print_unqualified, Just (is_exported, mod_iface, (sugar_map, final_decls)))
+ returnRn (print_unqualified, Just (is_exported, mod_iface, final_decls))
where
mod_name = moduleName this_module
\end{code}
-\begin{code}
-isOrphanDecl this_mod (InstD (InstDecl inst_ty _ _ _ _))
- = not (foldNameSet ((||) . nameIsLocalOrFrom this_mod) False
- (extractHsTyNames (removeContext inst_ty)))
- -- The 'removeContext' is because of
- -- instance Foo a => Baz T where ...
- -- The decl is an orphan if Baz and T are both not locally defined,
- -- even if Foo *is* locally defined
-
-isOrphanDecl this_mod (RuleD (HsRule _ _ _ lhs _ _))
- = check lhs
- where
- -- At the moment we just check for common LHS forms
- -- Expand as necessary. Getting it wrong just means
- -- more orphans than necessary
- check (HsVar v) = not (nameIsLocalOrFrom this_mod v)
- check (HsApp f a) = check f && check a
- check (HsLit _) = False
- check (HsOverLit _) = False
- check (OpApp l o _ r) = check l && check o && check r
- check (NegApp e) = check e
- check (HsPar e) = check e
- check (SectionL e o) = check e && check o
- check (SectionR o e) = check e && check o
-
- check other = True -- Safe fall through
-
-isOrphanDecl _ _ = False
-\end{code}
%*********************************************************
-> readIface iface_path `thenRn` \ read_result ->
case read_result of
Left err -> -- Old interface file not found, or garbled; give up
- traceRn (text "Bad old interface file" $$ nest 4 err) `thenRn_`
+ traceHiDiffsRn (
+ text "Cannot read old interface file:"
+ $$ nest 4 err) `thenRn_`
returnRn (outOfDate, Nothing)
Right parsed_iface
-> [RdrNameImportDecl]
-> AvailEnv
-> NameSet -- Used in this module
- -> Avails -- Exported by this module
-> [RenamedHsDecl]
-> RnMG ()
reportUnusedNames my_mod_iface unqual imports avail_env
- source_fvs export_avails imported_decls
+ used_names imported_decls
= warnUnusedModules unused_imp_mods `thenRn_`
warnUnusedLocalBinds bad_locals `thenRn_`
warnUnusedImports bad_imp_names `thenRn_`
this_mod = mi_module my_mod_iface
gbl_env = mi_globals my_mod_iface
- -- The export_fvs make the exported names look just as if they
- -- occurred in the source program.
- export_fvs = availsToNameSet export_avails
- used_names = source_fvs `plusFV` export_fvs
-
-- Now, a use of C implies a use of T,
-- if C was brought into scope by T(..) or T(C)
really_used_names = used_names `unionNameSets`
-> FiniteMap ModuleName AvailEnv -- Minimal imports
-> RnMG ()
printMinimalImports this_mod unqual imps
- = doptRn Opt_D_dump_minimal_imports `thenRn` \ dump_minimal ->
- if not dump_minimal then returnRn () else
+ = ifOptRn Opt_D_dump_minimal_imports $
mapRn to_ies (fmToList imps) `thenRn` \ mod_ies ->
ioToRnM (do { h <- openFile filename WriteMode ;