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 import PreludeGlaST ( thenPrimIO, newVar, MutableVar(..) )
16 import RdrHsSyn ( RdrNameHsModule(..), RdrNameImportDecl(..) )
17 import RnHsSyn ( RnName, RenamedHsModule(..), isRnTyConOrClass, isRnWired )
19 --ToDo:rm: all for debugging only
27 import Util (pprPanic, pprTrace)
29 import ParseUtils ( ParsedIface(..), RdrIfaceDecl(..), RdrIfaceInst(..) )
31 import RnNames ( getGlobalNames, GlobalNameInfo(..) )
32 import RnSource ( rnSource )
33 import RnIfaces ( findHiFiles, rnIfaces, finalIfaceInfo, VersionInfo(..) )
34 import RnUtils ( RnEnv(..), extendGlobalRnEnv, emptyRnEnv, multipleOccWarn )
37 import Bag ( isEmptyBag, unionBags, unionManyBags, bagToList, listToBag )
38 import CmdLineOpts ( opt_HiDirList, opt_SysHiDirList )
39 import ErrUtils ( Error(..), Warning(..) )
40 import FiniteMap ( emptyFM, eltsFM, fmToList, lookupFM{-ToDo:rm-} )
41 import Maybes ( catMaybes )
42 import Name ( isLocallyDefined, mkBuiltinName, Name, RdrName(..) )
43 import PrelInfo ( BuiltinNames(..), BuiltinKeys(..) )
44 import PrelMods ( pRELUDE )
45 import UniqFM ( emptyUFM, lookupUFM, addListToUFM_C, eltsUFM )
46 import UniqSupply ( splitUniqSupply )
47 import Util ( panic, assertPanic )
51 renameModule :: BuiltinNames
56 -> IO (RenamedHsModule, -- output, after renaming
57 RnEnv, -- final env (for renaming derivings)
58 [Module], -- imported modules; for profiling
60 VersionInfo, -- version info; for usage
61 [Module], -- instance modules; for iface
67 ToDo: May want to arrange to return old interface for this module!
68 ToDo: Builtin names which must be read.
69 ToDo: Deal with instances (instance version, this module on instance list ???)
72 renameModule b_names b_keys us
73 input@(HsModule mod _ _ imports _ _ _ _ _ _ _ _ _ _)
75 = pprTrace "builtins:\n" (case b_names of { (builtin_ids, builtin_tcs) ->
76 ppAboves [ ppCat (map ppPStr (keysFM builtin_ids))
77 , ppCat (map ppPStr (keysFM builtin_tcs))
78 , ppCat (map ppPStr (keysFM b_keys))
81 findHiFiles opt_HiDirList opt_SysHiDirList >>= \ hi_files ->
82 newVar (emptyFM, hi_files){-init iface cache-} `thenPrimIO` \ iface_cache ->
84 fixIO ( \ ~(_, _, _, _, rec_occ_fm, rec_export_fn) ->
86 rec_occ_fn :: Name -> [RdrName]
87 rec_occ_fn n = case lookupUFM rec_occ_fm n of
89 Just (rn,occs) -> occs
91 global_name_info = (b_names, b_keys, rec_export_fn, rec_occ_fn)
93 getGlobalNames iface_cache global_name_info us1 input >>=
94 \ (occ_env, imp_mods, unqual_imps, imp_fixes, top_errs, top_warns) ->
96 if not (isEmptyBag top_errs) then
97 return (rn_panic, rn_panic, top_errs, top_warns, emptyUFM, rn_panic)
100 -- No top-level name errors so rename source ...
101 case initRn True mod occ_env us2
102 (rnSource imp_mods unqual_imps imp_fixes input) of {
103 ((rn_module, export_fn, src_occs), src_errs, src_warns) ->
105 --pprTrace "renameModule:" (ppCat (map (ppr PprDebug . fst) (bagToList src_occs))) $
108 occ_fm :: UniqFM (RnName, [RdrName])
110 occ_list = [ (rn,(rn,[occ])) | (rn,occ) <- bagToList src_occs]
111 occ_fm = addListToUFM_C insert_occ emptyUFM occ_list
113 insert_occ (rn,olds) (rn',[new]) = (rn, insert new olds)
115 insert new [] = [new]
116 insert new xxs@(x:xs) = case cmp new x of LT_ -> new : xxs
118 GT__ -> x : insert new xs
120 occ_warns = map multipleOccWarn (filter multiple_occs (eltsUFM occ_fm))
121 multiple_occs (rn, (o1:o2:_)) = True
122 multiple_occs _ = False
124 return (rn_module, imp_mods,
125 top_errs `unionBags` src_errs,
126 top_warns `unionBags` src_warns `unionBags` listToBag occ_warns,
129 }) >>= \ (rn_module, imp_mods, errs_so_far, warns_so_far, occ_fm, _) ->
131 if not (isEmptyBag errs_so_far) then
132 return (rn_panic, rn_panic, rn_panic, rn_panic, rn_panic, errs_so_far, warns_so_far)
135 -- No errors renaming source so rename the interfaces ...
137 -- split up all names that occurred in the source; between
138 -- those that are defined therein and those merely mentioned.
139 -- We also divide by tycon/class and value names (as usual).
141 occ_rns = [ rn | (rn,_) <- eltsUFM occ_fm ]
142 -- all occurrence names, from this module and imported
144 (defined_here, defined_elsewhere)
145 = partition isLocallyDefined occ_rns
148 = partition isRnWired defined_elsewhere
150 (def_tcs, def_vals) = partition isRnTyConOrClass defined_here
151 (occ_tcs, occ_vals) = partition isRnTyConOrClass occ_rns
152 -- the occ stuff includes *all* occurrences,
153 -- including those for which we have definitions
155 (orig_def_env, orig_def_dups)
156 = extendGlobalRnEnv emptyRnEnv (map pair_orig def_vals)
157 (map pair_orig def_tcs)
158 (orig_occ_env, orig_occ_dups)
159 = extendGlobalRnEnv emptyRnEnv (map pair_orig occ_vals)
160 (map pair_orig occ_tcs)
162 pair_orig rn = (origName rn, rn)
164 -- we must ensure that the definitions of things in the BuiltinKey
165 -- table which may be *required* by the typechecker etc are read.
168 = [ name_fn (mkBuiltinName u pRELUDE str)
169 | (str, (u, name_fn)) <- fmToList b_keys,
170 str `notElem` [ SLIT("main"), SLIT("mainPrimIO")] ]
172 ASSERT (isEmptyBag orig_occ_dups)
173 ASSERT (isEmptyBag orig_def_dups)
175 rnIfaces iface_cache imp_mods us3 orig_def_env orig_occ_env
176 rn_module (must_haves ++ imports_used) >>=
177 \ (rn_module_with_imports, final_env,
178 (implicit_val_fm, implicit_tc_fm),
179 (iface_errs, iface_warns)) ->
181 all_imports_used = imports_used ++ eltsFM implicit_tc_fm
182 ++ eltsFM implicit_val_fm
184 finalIfaceInfo iface_cache all_imports_used imp_mods >>=
185 \ (version_info, instance_mods) ->
187 return (rn_module_with_imports,
192 errs_so_far `unionBags` iface_errs,
193 warns_so_far `unionBags` iface_warns)
195 rn_panic = panic "renameModule: aborted with errors"
197 (us1, us') = splitUniqSupply us
198 (us2, us3) = splitUniqSupply us'
202 pprPIface (ParsedIface m v mv lcm exm ims lfx ltdm lvdm lids ldp)
204 ppCat [ppPStr SLIT("interface"), ppPStr m, ppInt v,
205 case mv of { Nothing -> ppNil; Just n -> ppInt n }],
207 ppPStr SLIT("__versions__"),
208 ppAboves [ ppCat[ppPStr n, ppInt v] | (n,v) <- fmToList lcm ],
210 ppPStr SLIT("__exports__"),
211 ppAboves [ ppBesides[ppPStr n, ppSP, ppr PprDebug rn,
212 case ex of {ExportAll -> ppStr "(..)"; _ -> ppNil}]
213 | (n,(rn,ex)) <- fmToList exm ],
215 pp_ims (bagToList ims),
218 pp_insts (bagToList lids),
223 pp_ims ms = ppAbove (ppPStr SLIT("__instance_modules__"))
224 (ppCat (map ppPStr ms))
227 | isEmptyFM fx = ppNil
228 | otherwise = ppAboves (ppPStr SLIT("__fixities__")
229 : [ ppr PprDebug fix | (n, fix) <- fmToList fx])
231 pp_decls tds vds = ppAboves (ppPStr SLIT("__declarations__")
232 : [ pprRdrIfaceDecl d | (n, d) <- fmToList tds ++ fmToList vds])
235 pp_insts is = ppAboves (ppPStr SLIT("__instances__")
236 : [ pprRdrInstDecl i | i <- is])
238 pp_pragmas ps | isEmptyFM ps = ppNil
239 | otherwise = panic "Rename.pp_pragmas"
241 pprRdrIfaceDecl (TypeSig tc _ decl)
242 = ppBesides [ppStr "tycon=", ppr PprDebug tc, ppStr "; ", ppr PprDebug decl]
244 pprRdrIfaceDecl (NewTypeSig tc dc _ decl)
245 = ppBesides [ppStr "tycon=", ppr PprDebug tc, ppStr "; datacon=", ppr PprDebug dc,
246 ppStr "; ", ppr PprDebug decl]
248 pprRdrIfaceDecl (DataSig tc dcs dfs _ decl)
249 = ppBesides [ppStr "tycon=", ppr PprDebug tc, ppStr "; datacons=", ppr PprDebug dcs,
250 ppStr "; fields=", ppr PprDebug dfs, ppStr "; ", ppr PprDebug decl]
252 pprRdrIfaceDecl (ClassSig c ops _ decl)
253 = ppBesides [ppStr "class=", ppr PprDebug c, ppStr "; ops=", ppr PprDebug ops,
254 ppStr "; ", ppr PprDebug decl]
256 pprRdrIfaceDecl (ValSig f _ ty)
257 = ppBesides [ppr PprDebug f, ppStr " :: ", ppr PprDebug ty]
259 pprRdrInstDecl (InstSig c t _ decl)
260 = ppBesides [ppStr "class=", ppr PprDebug c, ppStr " type=", ppr PprDebug t, ppStr "; ",