2 % (c) The GRASP Project, Glasgow University, 1992-1996
4 \section[Rename]{Renaming and dependency analysis passes}
7 #include "HsVersions.h"
9 module Rename ( renameModule ) where
11 #if __GLASGOW_HASKELL__ <= 201
12 import PreludeGlaST ( thenPrimIO )
19 IMPORT_1_3(List(partition))
22 import RdrHsSyn ( RdrName(..), SYN_IE(RdrNameHsModule), SYN_IE(RdrNameImportDecl) )
23 import RnHsSyn ( SYN_IE(RenamedHsModule), SYN_IE(RenamedHsDecl), extractHsTyNames )
25 import CmdLineOpts ( opt_HiMap, opt_WarnNameShadowing, opt_D_show_rn_trace,
26 opt_D_dump_rn, opt_D_show_rn_stats,
27 opt_D_show_unused_imports, opt_PprUserLength
30 import RnNames ( getGlobalNames )
31 import RnSource ( rnDecl )
32 import RnIfaces ( getImportedInstDecls, importDecl, getImportVersions, getSpecialInstModules,
34 mkSearchPath, getSlurpedNames, getRnStats
36 import RnEnv ( availsToNameSet, addAvailToNameSet,
37 addImplicitOccsRn, lookupImplicitOccRn )
38 import Id ( GenId {- instance NamedThing -} )
39 import Name ( Name, Provenance, ExportFlag(..), isLocallyDefined,
40 NameSet(..), elemNameSet, mkNameSet, unionNameSets,
41 nameSetToList, minusNameSet, NamedThing(..),
42 nameModule, pprModule, pprOccName, nameOccName
44 import TysWiredIn ( unitTyCon, intTyCon, doubleTyCon )
45 import TyCon ( TyCon )
46 import PrelMods ( mAIN, gHC_MAIN )
47 import ErrUtils ( SYN_IE(Error), SYN_IE(Warning), pprBagOfErrors,
48 doIfSet, dumpIfSet, ghcExit
50 import FiniteMap ( emptyFM, eltsFM, fmToList, addToFM, FiniteMap )
52 import Outputable ( Outputable(..), PprStyle(..),
53 pprErrorsStyle, pprDumpStyle, printErrs
55 import Bag ( isEmptyBag )
56 import Util ( cmpPString, equivClasses, panic, assertPanic, pprTrace )
57 #if __GLASGOW_HASKELL__ >= 202
65 renameModule :: UniqSupply
67 -> IO (Maybe (RenamedHsModule, -- Output, after renaming
68 InterfaceDetails, -- Interface; for interface file generatino
69 RnNameSupply, -- Final env; for renaming derivings
70 [Module])) -- Imported modules; for profiling
72 renameModule us this_mod@(HsModule mod_name vers exports imports fixities local_decls loc)
73 = -- Initialise the renamer monad
74 initRn mod_name us (mkSearchPath opt_HiMap) loc
76 \ (maybe_rn_stuff, rn_errs_bag, rn_warns_bag) ->
79 doIfSet (not (isEmptyBag rn_warns_bag))
80 (print_errs rn_warns_bag) >>
82 -- Check for errors; exit if so
83 doIfSet (not (isEmptyBag rn_errs_bag))
84 (print_errs rn_errs_bag >>
88 -- Dump output, if any
89 (case maybe_rn_stuff of
91 Just results@(rn_mod, _, _, _)
92 -> dumpIfSet opt_D_dump_rn "Renamer:"
93 (ppr pprDumpStyle rn_mod)
100 print_errs errs = printErrs (pprBagOfErrors pprErrorsStyle errs)
105 rename this_mod@(HsModule mod_name vers exports imports fixities local_decls loc)
106 = -- FIND THE GLOBAL NAME ENVIRONMENT
107 getGlobalNames this_mod `thenRn` \ global_name_info ->
109 case global_name_info of {
110 Nothing -> -- Everything is up to date; no need to recompile further
114 -- Otherwise, just carry on
115 Just (export_env, rn_env, explicit_names) ->
118 initRnMS rn_env mod_name SourceMode (
119 addImplicits mod_name `thenRn_`
120 mapRn rnDecl local_decls
121 ) `thenRn` \ rn_local_decls ->
123 -- SLURP IN ALL THE NEEDED DECLARATIONS
124 slurpDecls rn_local_decls `thenRn` \ rn_all_decls ->
127 -- GENERATE THE VERSION/USAGE INFO
128 getImportVersions mod_name exports `thenRn` \ import_versions ->
129 getNameSupplyRn `thenRn` \ name_supply ->
131 -- REPORT UNUSED NAMES
132 reportUnusedNames explicit_names `thenRn_`
134 -- GENERATE THE SPECIAL-INSTANCE MODULE LIST
135 -- The "special instance" modules are those modules that contain instance
136 -- declarations that contain no type constructor or class that was declared
138 getSpecialInstModules `thenRn` \ imported_special_inst_mods ->
140 special_inst_decls = [d | InstD d@(InstDecl inst_ty _ _ _ _) <- rn_local_decls,
141 all (not.isLocallyDefined) (nameSetToList (extractHsTyNames inst_ty))
143 special_inst_mods | null special_inst_decls = imported_special_inst_mods
144 | otherwise = mod_name : imported_special_inst_mods
148 -- RETURN THE RENAMED MODULE
150 import_mods = [mod | ImportDecl mod _ _ _ _ _ <- imports]
152 renamed_module = HsModule mod_name vers
153 trashed_exports trashed_imports trashed_fixities
157 rnStats rn_all_decls `thenRn_`
158 returnRn (Just (renamed_module,
159 (import_versions, export_env, special_inst_mods),
164 trashed_exports = {-trace "rnSource:trashed_exports"-} Nothing
165 trashed_imports = {-trace "rnSource:trashed_imports"-} []
166 trashed_fixities = []
169 @addImplicits@ forces the renamer to slurp in some things which aren't
170 mentioned explicitly, but which might be needed by the type checker.
173 addImplicits mod_name
174 = addImplicitOccsRn default_tys
176 -- Add occurrences for Int, Double, and (), because they
177 -- are the types to which ambigious type variables may be defaulted by
178 -- the type checker; so they won't every appear explicitly.
179 -- [The () one is a GHC extension for defaulting CCall results.]
180 default_tys = [getName intTyCon, getName doubleTyCon, getName unitTyCon ]
186 = -- First of all, get all the compulsory decls
187 slurp_compulsories decls `thenRn` \ decls1 ->
189 -- Next get the optional ones
190 closeDecls Optional decls1 `thenRn` \ decls2 ->
192 -- Finally get those deferred data type declarations
193 getDeferredDataDecls `thenRn` \ data_decls ->
194 mapRn rn_data_decl data_decls `thenRn` \ rn_data_decls ->
197 returnRn (rn_data_decls ++ decls2)
200 -- The "slurp_compulsories" function is a loop that alternates
201 -- between slurping compulsory decls and slurping the instance
202 -- decls thus made relavant.
203 -- We *must* loop again here. Why? Two reasons:
204 -- (a) an instance decl will give rise to an unresolved dfun, whose
205 -- decl we must slurp to get its version number; that's the version
206 -- number for the whole instance decl. (And its unfolding might mention new
207 -- unresolved names.)
208 -- (b) an instance decl might give rise to a new unresolved class,
209 -- whose decl we must slurp, which might let in some new instance decls,
210 -- and so on. Example: instance Foo a => Baz [a] where ...
211 slurp_compulsories decls
212 = closeDecls Compulsory decls `thenRn` \ decls1 ->
214 -- Instance decls still pending?
215 getImportedInstDecls `thenRn` \ inst_decls ->
216 if null inst_decls then
220 -- Yes, there are some, so rename them and loop
221 traceRn (sep [ptext SLIT("Slurped"), int (length inst_decls), ptext SLIT("instance decls")])
223 mapRn rn_inst_decl inst_decls `thenRn` \ new_inst_decls ->
224 slurp_compulsories (new_inst_decls ++ decls1)
228 closeDecls :: Necessity
229 -> [RenamedHsDecl] -- Declarations got so far
230 -> RnMG [RenamedHsDecl] -- input + extra decls slurped
231 -- The monad includes a list of possibly-unresolved Names
232 -- This list is empty when closeDecls returns
234 closeDecls necessity decls
235 = popOccurrenceName necessity `thenRn` \ maybe_unresolved ->
236 case maybe_unresolved of
238 -- No more unresolved names
239 Nothing -> returnRn decls
241 -- An unresolved name
243 -> -- Slurp its declaration, if any
244 -- traceRn (sep [ptext SLIT("Considering"), ppr PprDebug name]) `thenRn_`
245 importDecl name necessity `thenRn` \ maybe_decl ->
248 -- No declaration... (wired in thing or optional)
249 Nothing -> closeDecls necessity decls
251 -- Found a declaration... rename it
252 Just decl -> rn_iface_decl mod_name necessity decl `thenRn` \ new_decl ->
253 closeDecls necessity (new_decl : decls)
255 mod_name = nameModule name
258 rn_iface_decl mod_name necessity decl -- Notice that the rnEnv starts empty
259 = initRnMS emptyRnEnv mod_name (InterfaceMode necessity) (rnDecl decl)
261 rn_inst_decl (mod_name,decl) = rn_iface_decl mod_name Compulsory (InstD decl)
263 rn_data_decl (tycon_name,ty_decl) = rn_iface_decl mod_name Compulsory (TyD ty_decl)
265 mod_name = nameModule tycon_name
269 reportUnusedNames explicit_avail_names
270 | not opt_D_show_unused_imports
274 = getSlurpedNames `thenRn` \ slurped_names ->
276 unused = explicit_avail_names `minusNameSet` slurped_names
277 (local_unused, imported_unused) = partition isLocallyDefined (nameSetToList unused)
278 imports_by_module = equivClasses cmp imported_unused
279 name1 `cmp` name2 = nameModule name1 `_CMP_STRING_` nameModule name2
281 pp_imp sty = sep [text "For information: the following unqualified imports are unused:",
282 nest 4 (vcat (map (pp_group sty) imports_by_module))]
283 pp_group sty (n:ns) = sep [hcat [text "Module ", pprModule (PprForUser opt_PprUserLength) (nameModule n), char ':'],
284 nest 4 (sep (map (pprOccName sty . nameOccName) (n:ns)))]
286 pp_local sty = sep [text "For information: the following local top-level definitions are unused:",
287 nest 4 (sep (map (pprOccName sty . nameOccName) local_unused))]
289 (if null imported_unused
291 else addWarnRn pp_imp) `thenRn_`
293 (if null local_unused
295 else addWarnRn pp_local)
297 rnStats :: [RenamedHsDecl] -> RnMG ()
299 | opt_D_show_rn_trace ||
300 opt_D_show_rn_stats ||
302 = getRnStats all_decls `thenRn` \ msg ->
303 ioToRnMG (printErrs msg) `thenRn_`
306 | otherwise = returnRn ()