2 % (c) The GRASP Project, Glasgow University, 1992-1996
4 \section[Rename]{Renaming and dependency analysis passes}
7 module Rename ( renameModule ) where
9 #include "HsVersions.h"
12 import RdrHsSyn ( RdrName(..), RdrNameHsModule )
13 import RnHsSyn ( RenamedHsModule, RenamedHsDecl, extractHsTyNames )
15 import CmdLineOpts ( opt_HiMap, opt_D_show_rn_trace,
16 opt_D_dump_rn, opt_D_show_rn_stats,
17 opt_WarnUnusedBinds, opt_WarnUnusedImports
20 import RnNames ( getGlobalNames )
21 import RnSource ( rnDecl )
22 import RnIfaces ( getImportedInstDecls, importDecl, getImportVersions, getSpecialInstModules,
24 mkSearchPath, getSlurpedNames, getRnStats
26 import RnEnv ( addImplicitOccsRn )
27 import Name ( Name, PrintUnqualified, Provenance, isLocallyDefined,
29 nameSetToList, minusNameSet, NamedThing(..),
30 nameModule, pprModule, pprOccName, nameOccName
32 import TysWiredIn ( unitTyCon, intTyCon, doubleTyCon )
33 import TyCon ( TyCon )
34 import PrelMods ( mAIN, pREL_MAIN )
35 import PrelInfo ( ioTyCon_NAME )
36 import ErrUtils ( pprBagOfErrors, pprBagOfWarnings,
37 doIfSet, dumpIfSet, ghcExit
39 import Bag ( isEmptyBag )
40 import UniqSupply ( UniqSupply )
41 import Util ( equivClasses )
42 import Maybes ( maybeToBool )
43 import List ( partition )
50 renameModule :: UniqSupply
52 -> IO (Maybe (RenamedHsModule, -- Output, after renaming
53 InterfaceDetails, -- Interface; for interface file generatino
54 RnNameSupply, -- Final env; for renaming derivings
55 [Module])) -- Imported modules; for profiling
57 renameModule us this_mod@(HsModule mod_name vers exports imports fixities local_decls loc)
58 = -- Initialise the renamer monad
59 initRn mod_name us (mkSearchPath opt_HiMap) loc
61 \ (maybe_rn_stuff, rn_errs_bag, rn_warns_bag) ->
64 doIfSet (not (isEmptyBag rn_warns_bag))
65 (printErrs (pprBagOfWarnings rn_warns_bag)) >>
67 -- Check for errors; exit if so
68 doIfSet (not (isEmptyBag rn_errs_bag))
69 (printErrs (pprBagOfErrors rn_errs_bag) >>
73 -- Dump output, if any
74 (case maybe_rn_stuff of
76 Just results@(rn_mod, _, _, _)
77 -> dumpIfSet opt_D_dump_rn "Renamer:"
87 rename this_mod@(HsModule mod_name vers exports imports fixities local_decls loc)
88 = -- FIND THE GLOBAL NAME ENVIRONMENT
89 getGlobalNames this_mod `thenRn` \ maybe_stuff ->
91 -- CHECK FOR EARLY EXIT
92 if not (maybeToBool maybe_stuff) then
93 -- Everything is up to date; no need to recompile further
98 Just (export_env, rn_env, explicit_names, print_unqual) = maybe_stuff
102 initRnMS rn_env mod_name SourceMode (
103 addImplicits mod_name `thenRn_`
104 mapRn rnDecl local_decls
105 ) `thenRn` \ rn_local_decls ->
107 -- SLURP IN ALL THE NEEDED DECLARATIONS
108 slurpDecls print_unqual rn_local_decls `thenRn` \ rn_all_decls ->
110 -- EXIT IF ERRORS FOUND
111 checkErrsRn `thenRn` \ no_errs_so_far ->
112 if not no_errs_so_far then
113 -- Found errors already, so exit now
118 -- GENERATE THE VERSION/USAGE INFO
119 getImportVersions mod_name exports `thenRn` \ import_versions ->
120 getNameSupplyRn `thenRn` \ name_supply ->
122 -- REPORT UNUSED NAMES
123 reportUnusedNames explicit_names `thenRn_`
125 -- GENERATE THE SPECIAL-INSTANCE MODULE LIST
126 -- The "special instance" modules are those modules that contain instance
127 -- declarations that contain no type constructor or class that was declared
129 getSpecialInstModules `thenRn` \ imported_special_inst_mods ->
131 special_inst_decls = [d | InstD d@(InstDecl inst_ty _ _ _ _) <- rn_local_decls,
132 all (not.isLocallyDefined) (nameSetToList (extractHsTyNames inst_ty))
134 special_inst_mods | null special_inst_decls = imported_special_inst_mods
135 | otherwise = mod_name : imported_special_inst_mods
139 -- RETURN THE RENAMED MODULE
141 import_mods = [mod | ImportDecl mod _ _ _ _ _ <- imports]
143 renamed_module = HsModule mod_name vers
144 trashed_exports trashed_imports trashed_fixities
148 rnStats rn_all_decls `thenRn_`
149 returnRn (Just (renamed_module,
150 (import_versions, export_env, special_inst_mods),
154 trashed_exports = {-trace "rnSource:trashed_exports"-} Nothing
155 trashed_imports = {-trace "rnSource:trashed_imports"-} []
156 trashed_fixities = []
159 @addImplicits@ forces the renamer to slurp in some things which aren't
160 mentioned explicitly, but which might be needed by the type checker.
163 addImplicits mod_name
164 = addImplicitOccsRn (implicit_main ++ default_tys)
166 -- Add occurrences for Int, Double, and (), because they
167 -- are the types to which ambigious type variables may be defaulted by
168 -- the type checker; so they won't every appear explicitly.
169 -- [The () one is a GHC extension for defaulting CCall results.]
170 default_tys = [getName intTyCon, getName doubleTyCon, getName unitTyCon ]
172 -- Add occurrences for IO or PrimIO
173 implicit_main | mod_name == mAIN
174 || mod_name == pREL_MAIN = [ioTyCon_NAME]
180 slurpDecls print_unqual decls
181 = -- First of all, get all the compulsory decls
182 slurp_compulsories decls `thenRn` \ decls1 ->
184 -- Next get the optional ones
185 closeDecls optional_mode decls1 `thenRn` \ decls2 ->
187 -- Finally get those deferred data type declarations
188 getDeferredDataDecls `thenRn` \ data_decls ->
189 mapRn (rn_data_decl compulsory_mode) data_decls `thenRn` \ rn_data_decls ->
192 returnRn (rn_data_decls ++ decls2)
195 compulsory_mode = InterfaceMode Compulsory print_unqual
196 optional_mode = InterfaceMode Optional print_unqual
198 -- The "slurp_compulsories" function is a loop that alternates
199 -- between slurping compulsory decls and slurping the instance
200 -- decls thus made relavant.
201 -- We *must* loop again here. Why? Two reasons:
202 -- (a) an instance decl will give rise to an unresolved dfun, whose
203 -- decl we must slurp to get its version number; that's the version
204 -- number for the whole instance decl. (And its unfolding might mention new
205 -- unresolved names.)
206 -- (b) an instance decl might give rise to a new unresolved class,
207 -- whose decl we must slurp, which might let in some new instance decls,
208 -- and so on. Example: instance Foo a => Baz [a] where ...
209 slurp_compulsories decls
210 = closeDecls compulsory_mode decls `thenRn` \ decls1 ->
212 -- Instance decls still pending?
213 getImportedInstDecls `thenRn` \ inst_decls ->
214 if null inst_decls then
218 -- Yes, there are some, so rename them and loop
219 traceRn (sep [ptext SLIT("Slurped"), int (length inst_decls), ptext SLIT("instance decls")])
221 mapRn (rn_inst_decl compulsory_mode) inst_decls `thenRn` \ new_inst_decls ->
222 slurp_compulsories (new_inst_decls ++ decls1)
226 closeDecls :: RnSMode
227 -> [RenamedHsDecl] -- Declarations got so far
228 -> RnMG [RenamedHsDecl] -- input + extra decls slurped
229 -- The monad includes a list of possibly-unresolved Names
230 -- This list is empty when closeDecls returns
232 closeDecls mode decls
233 = popOccurrenceName mode `thenRn` \ maybe_unresolved ->
234 case maybe_unresolved of
236 -- No more unresolved names
237 Nothing -> returnRn decls
239 -- An unresolved name
241 -> -- Slurp its declaration, if any
242 -- traceRn (sep [ptext SLIT("Considering"), ppr name_w_loc]) `thenRn_`
243 importDecl name_w_loc mode `thenRn` \ maybe_decl ->
246 -- No declaration... (wired in thing or optional)
247 Nothing -> closeDecls mode decls
249 -- Found a declaration... rename it
250 Just decl -> rn_iface_decl mod_name mode decl `thenRn` \ new_decl ->
251 closeDecls mode (new_decl : decls)
253 mod_name = nameModule (fst name_w_loc)
255 rn_iface_decl mod_name mode decl
256 = initRnMS emptyRnEnv mod_name mode (rnDecl decl)
258 rn_inst_decl mode (mod_name,decl) = rn_iface_decl mod_name mode (InstD decl)
259 rn_data_decl mode (tycon_name,ty_decl) = rn_iface_decl mod_name mode (TyD ty_decl)
261 mod_name = nameModule tycon_name
265 reportUnusedNames explicit_avail_names
266 = getSlurpedNames `thenRn` \ slurped_names ->
268 unused = explicit_avail_names `minusNameSet` slurped_names
269 (local_unused, imported_unused) = partition isLocallyDefined (nameSetToList unused)
270 imports_by_module = equivClasses cmp imported_unused
271 name1 `cmp` name2 = nameModule name1 `compare` nameModule name2
273 pp_imp = sep [text "Warning: the following unqualified imports are unused:",
274 nest 4 (vcat (map pp_group imports_by_module))]
275 pp_group (n:ns) = sep [hcat [text "Module ", pprModule (nameModule n), char ':'],
276 nest 4 (sep (map (pprOccName . nameOccName) (n:ns)))]
278 pp_local = sep [text "Warning: the following local top-level definitions are unused:",
279 nest 4 (sep (map (pprOccName . nameOccName) local_unused))]
281 (if not opt_WarnUnusedImports || null imported_unused
283 else addWarnRn pp_imp) `thenRn_`
285 (if not opt_WarnUnusedBinds || null local_unused
287 else addWarnRn pp_local)
289 rnStats :: [RenamedHsDecl] -> RnMG ()
291 | opt_D_show_rn_trace ||
292 opt_D_show_rn_stats ||
294 = getRnStats all_decls `thenRn` \ msg ->
295 ioToRnMG (printErrs msg) `thenRn_`
298 | otherwise = returnRn ()