2 % (c) The GRASP Project, Glasgow University, 1992-1998
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, availNames )
27 import Name ( Name, isLocallyDefined,
29 nameModule, pprModule, pprOccName, nameOccName
32 import TyCon ( TyCon )
33 import PrelMods ( mAIN, pREL_MAIN )
34 import TysWiredIn ( unitTyCon, intTyCon, doubleTyCon )
35 import PrelInfo ( ioTyCon_NAME, thinAirIdNames )
36 import ErrUtils ( pprBagOfErrors, pprBagOfWarnings,
37 doIfSet, dumpIfSet, ghcExit
39 import Bag ( isEmptyBag )
40 import FiniteMap ( fmToList, delListFromFM )
41 import UniqSupply ( UniqSupply )
42 import Util ( equivClasses )
43 import Maybes ( maybeToBool )
50 renameModule :: UniqSupply
53 ( RenamedHsModule -- Output, after renaming
54 , InterfaceDetails -- Interface; for interface file generatino
55 , RnNameSupply -- Final env; for renaming derivings
56 , [Module] -- Imported modules; for profiling
59 renameModule us this_mod@(HsModule mod_name vers exports imports fixities local_decls loc)
60 = -- Initialise the renamer monad
61 initRn mod_name us (mkSearchPath opt_HiMap) loc
63 \ (maybe_rn_stuff, rn_errs_bag, rn_warns_bag) ->
66 doIfSet (not (isEmptyBag rn_warns_bag))
67 (printErrs (pprBagOfWarnings rn_warns_bag)) >>
69 -- Check for errors; exit if so
70 doIfSet (not (isEmptyBag rn_errs_bag))
71 (printErrs (pprBagOfErrors rn_errs_bag) >>
75 -- Dump output, if any
76 (case maybe_rn_stuff of
78 Just results@(rn_mod, _, _, _)
79 -> dumpIfSet opt_D_dump_rn "Renamer:"
89 rename this_mod@(HsModule mod_name vers exports imports fixities local_decls loc)
90 = -- FIND THE GLOBAL NAME ENVIRONMENT
91 getGlobalNames this_mod `thenRn` \ maybe_stuff ->
93 -- CHECK FOR EARLY EXIT
94 if not (maybeToBool maybe_stuff) then
95 -- Everything is up to date; no need to recompile further
100 Just (export_env, rn_env, explicit_info, print_unqual) = maybe_stuff
104 initRnMS rn_env mod_name SourceMode (
105 addImplicits mod_name `thenRn_`
106 mapRn rnDecl local_decls
107 ) `thenRn` \ rn_local_decls ->
109 -- SLURP IN ALL THE NEEDED DECLARATIONS
110 slurpDecls print_unqual rn_local_decls `thenRn` \ rn_all_decls ->
112 -- EXIT IF ERRORS FOUND
113 checkErrsRn `thenRn` \ no_errs_so_far ->
114 if not no_errs_so_far then
115 -- Found errors already, so exit now
120 -- GENERATE THE VERSION/USAGE INFO
121 getImportVersions mod_name exports `thenRn` \ import_versions ->
122 getNameSupplyRn `thenRn` \ name_supply ->
124 -- REPORT UNUSED NAMES
125 reportUnusedNames export_env explicit_info `thenRn_`
127 -- GENERATE THE SPECIAL-INSTANCE MODULE LIST
128 -- The "special instance" modules are those modules that contain instance
129 -- declarations that contain no type constructor or class that was declared
131 getSpecialInstModules `thenRn` \ imported_special_inst_mods ->
133 special_inst_decls = [d | InstD d@(InstDecl inst_ty _ _ _ _) <- rn_local_decls,
134 all (not.isLocallyDefined) (nameSetToList (extractHsTyNames inst_ty))
136 special_inst_mods | null special_inst_decls = imported_special_inst_mods
137 | otherwise = mod_name : imported_special_inst_mods
141 -- RETURN THE RENAMED MODULE
143 import_mods = [mod | ImportDecl mod _ _ _ _ _ <- imports]
145 renamed_module = HsModule mod_name vers
146 trashed_exports trashed_imports trashed_fixities
150 rnStats rn_all_decls `thenRn_`
151 returnRn (Just (renamed_module,
152 (import_versions, export_env, special_inst_mods),
156 trashed_exports = {-trace "rnSource:trashed_exports"-} Nothing
157 trashed_imports = {-trace "rnSource:trashed_imports"-} []
158 trashed_fixities = []
161 @addImplicits@ forces the renamer to slurp in some things which aren't
162 mentioned explicitly, but which might be needed by the type checker.
165 addImplicits mod_name
166 = addImplicitOccsRn (implicit_main ++ default_tys ++ thinAirIdNames)
168 -- Add occurrences for Int, Double, and (), because they
169 -- are the types to which ambigious type variables may be defaulted by
170 -- the type checker; so they won't always appear explicitly.
171 -- [The () one is a GHC extension for defaulting CCall results.]
172 default_tys = [getName intTyCon, getName doubleTyCon, getName unitTyCon ]
174 -- Add occurrences for IO or PrimIO
175 implicit_main | mod_name == mAIN
176 || mod_name == pREL_MAIN = [ioTyCon_NAME]
182 slurpDecls print_unqual decls
183 = -- First of all, get all the compulsory decls
184 slurp_compulsories decls `thenRn` \ decls1 ->
186 -- Next get the optional ones
187 closeDecls optional_mode decls1 `thenRn` \ decls2 ->
189 -- Finally get those deferred data type declarations
190 getDeferredDataDecls `thenRn` \ data_decls ->
191 mapRn (rn_data_decl compulsory_mode) data_decls `thenRn` \ rn_data_decls ->
194 returnRn (rn_data_decls ++ decls2)
197 compulsory_mode = InterfaceMode Compulsory print_unqual
198 optional_mode = InterfaceMode Optional print_unqual
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_mode 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 compulsory_mode) inst_decls `thenRn` \ new_inst_decls ->
224 slurp_compulsories (new_inst_decls ++ decls1)
228 closeDecls :: RnSMode
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 mode decls
235 = popOccurrenceName mode `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 name_w_loc]) `thenRn_`
245 importDecl name_w_loc mode `thenRn` \ maybe_decl ->
248 -- No declaration... (wired in thing or optional)
249 Nothing -> closeDecls mode decls
251 -- Found a declaration... rename it
252 Just decl -> rn_iface_decl mod_name mode decl `thenRn` \ new_decl ->
253 closeDecls mode (new_decl : decls)
255 mod_name = nameModule (fst name_w_loc)
257 rn_iface_decl mod_name mode decl
258 = initRnMS emptyRnEnv mod_name mode (rnDecl decl)
260 rn_inst_decl mode (mod_name,decl) = rn_iface_decl mod_name mode (InstD decl)
261 rn_data_decl mode (tycon_name,ty_decl) = rn_iface_decl mod_name mode (TyD ty_decl)
263 mod_name = nameModule tycon_name
267 reportUnusedNames (ExportEnv export_avails _) explicit_info
268 | not (opt_WarnUnusedBinds || opt_WarnUnusedImports)
272 = getSlurpedNames `thenRn` \ slurped_names ->
274 unused_info :: FiniteMap Name HowInScope
275 unused_info = foldl delListFromFM
276 (delListFromFM explicit_info (nameSetToList slurped_names))
277 (map availNames export_avails)
278 unused_list = fmToList unused_info
280 groups = filter wanted (equivClasses cmp unused_list)
282 (name1, his1) `cmp` (name2, his2) = his1 `cmph` his2
284 (FromLocalDefn _) `cmph` (FromImportDecl _ _) = LT
285 (FromLocalDefn _) `cmph` (FromLocalDefn _) = EQ
286 (FromImportDecl m1 _) `cmph` (FromImportDecl m2 _) = m1 `compare` m2
289 wanted ((_,FromImportDecl _ _) : _) = opt_WarnUnusedImports
290 wanted ((_,FromLocalDefn _) : _) = opt_WarnUnusedImports
292 pp_imp = sep [text "Warning: the following are unused:",
293 nest 4 (vcat (map pp_group groups))]
295 pp_group group = sep [msg <> char ':',
296 nest 4 (sep (map (pprOccName . nameOccName . fst) group))]
302 FromImportDecl m _ -> text "Imported from" <+> pprModule m
303 FromLocalDefn _ -> text "Locally defined"
308 else addWarnRn pp_imp
310 rnStats :: [RenamedHsDecl] -> RnMG ()
312 | opt_D_show_rn_trace ||
313 opt_D_show_rn_stats ||
315 = getRnStats all_decls `thenRn` \ msg ->
316 ioToRnMG (printErrs msg) `thenRn_`
319 | otherwise = returnRn ()