X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Frename%2FRename.lhs;h=1a969990e33a1d3ff51d088e2d32fc3face95357;hb=5cf27e8f1731c52fe63a5b9615f927484164c61b;hp=a2900c7671d8a6971e124e282c6943d4607cc1ea;hpb=e7d21ee4f8ac907665a7e170c71d59e13a01da09;p=ghc-hetmet.git diff --git a/ghc/compiler/rename/Rename.lhs b/ghc/compiler/rename/Rename.lhs index a2900c7..1a96999 100644 --- a/ghc/compiler/rename/Rename.lhs +++ b/ghc/compiler/rename/Rename.lhs @@ -1,145 +1,282 @@ % -% (c) The GRASP Project, Glasgow University, 1992-1994 +% (c) The GRASP Project, Glasgow University, 1992-1996 % \section[Rename]{Renaming and dependency analysis passes} \begin{code} #include "HsVersions.h" -module Rename ( - renameModule, - - -- for completeness - Module, Bag, InPat, ProtoNamePat(..), RenamedPat(..), Name, - ProtoName, SplitUniqSupply, PreludeNameFun(..), - PreludeNameFuns(..), Maybe, Error(..), Pretty(..), PprStyle, - PrettyRep, GlobalNameFuns(..), GlobalNameFun(..), - GlobalSwitch - ) where - -import AbsSyn -import Bag ( isEmptyBag, unionBags, Bag ) -import CmdLineOpts ( GlobalSwitch(..) ) -import RenameMonad12 -import Rename1 -import Rename2 -import Rename3 -import Rename4 -import RenameAuxFuns ( PreludeNameFuns(..), GlobalNameFuns(..) ) ---import Pretty -- ToDo: rm debugging -import SplitUniq ( splitUniqSupply, SplitUniqSupply ) -import Util +module Rename ( renameModule ) where + +import PreludeGlaST ( thenPrimIO, newVar, MutableVar(..) ) + +import Ubiq + +import HsSyn +import RdrHsSyn ( RdrNameHsModule(..), RdrNameImportDecl(..) ) +import RnHsSyn ( RnName, RenamedHsModule(..), isRnTyConOrClass, isRnWired ) + +--ToDo:rm: all for debugging only +import Maybes +import Name +import Outputable +import RnIfaces +import PprStyle +import Pretty +import FiniteMap +import Util (pprPanic, pprTrace) + +import ParseUtils ( ParsedIface(..), RdrIfaceDecl(..), RdrIfaceInst(..), + UsagesMap(..), VersionsMap(..) + ) +import RnMonad +import RnNames ( getGlobalNames, GlobalNameInfo(..) ) +import RnSource ( rnSource ) +import RnIfaces ( rnIfaces ) +import RnUtils ( RnEnv(..), extendGlobalRnEnv, emptyRnEnv, multipleOccWarn ) + +import Bag ( isEmptyBag, unionBags, unionManyBags, bagToList, listToBag ) +import CmdLineOpts ( opt_HiMap ) +import ErrUtils ( Error(..), Warning(..) ) +import FiniteMap ( emptyFM, eltsFM, fmToList, lookupFM{-ToDo:rm-} ) +import Maybes ( catMaybes ) +import Name ( isLocallyDefined, mkBuiltinName, Name, RdrName(..) ) +import PrelInfo ( builtinNameInfo, BuiltinNames(..), BuiltinKeys(..) ) +import PrelMods ( pRELUDE ) +import UniqFM ( emptyUFM, lookupUFM, addListToUFM_C, eltsUFM ) +import UniqSupply ( splitUniqSupply ) +import Util ( panic, assertPanic ) \end{code} -Here's what the renamer does, basically: -\begin{description} -\item[@Rename1@:] -Flattens out the declarations from the interfaces which this module -imports. The result is a new module with no imports, but with more -declarations. (Obviously, the imported declarations have ``funny -names'' [@ProtoNames@] to indicate their origin.) Handles selective -import, renaming, \& such. - -%-------------------------------------------------------------------- -\item[@Rename2@:] -Removes duplicate declarations. Duplicates can arise when two -imported interface have a signature (or whatever) for the same -thing. We check that the two are consistent and then drop one. -Considerable huff and puff to pick the one with the ``better'' -pragmatic information. - -%-------------------------------------------------------------------- -\item[@Rename3@:] -Find all the top-level-ish (i.e., global) entities, assign them -@Uniques@, and make a \tr{ProtoName -> Name} mapping for them, -in preparation for... - -%-------------------------------------------------------------------- -\item[@Rename4@:] -Actually prepare the ``renamed'' module. In sticking @Names@ on -everything, it will catch out-of-scope errors (and a couple of similar -type-variable-use errors). We also our initial dependency analysis of -the program (required before typechecking). -\end{description} +\begin{code} +renameModule :: UniqSupply + -> RdrNameHsModule + + -> IO (RenamedHsModule, -- output, after renaming + RnEnv, -- final env (for renaming derivings) + [Module], -- imported modules; for profiling + + (UsagesMap, + VersionsMap, -- version info; for usage + [Module]), -- instance modules; for iface + + Bag Error, + Bag Warning) +\end{code} + +ToDo: May want to arrange to return old interface for this module! +ToDo: Builtin names which must be read. +ToDo: Deal with instances (instance version, this module on instance list ???) \begin{code} -renameModule :: (GlobalSwitch -> Bool) -- to check cmd-line opts - -> PreludeNameFuns -- lookup funs for deeply wired-in names - -> ProtoNameModule -- input - -> SplitUniqSupply - -> (RenamedModule, -- output, after renaming - [FAST_STRING], -- Names of the imported modules - -- (profiling needs to know this) - GlobalNameFuns, -- final name funs; used later - -- to rename generated `deriving' - -- bindings. - Bag Error -- Errors, from passes 1-4 - ) - --- Very space-leak sensitive - -renameModule sw_chkr gnfs@(val_pnf, tc_pnf) - input@(Module mod_name _ _ _ _ _ _ _ _ _ _ _ _) - uniqs +renameModule us input@(HsModule mod _ _ imports _ _ _ _ _ _ _ _ _ _) + = let - use_mentioned_vars = sw_chkr UseGetMentionedVars + (b_names, b_keys, _) = builtinNameInfo in - BIND ( - BSCC("Rename1") - initRn12 mod_name (rnModule1 gnfs use_mentioned_vars input) - ESCC - ) _TO_ ((mod1, imported_module_names), errs1) -> + --pprTrace "builtins:\n" (case b_names of { (builtin_ids, builtin_tcs) -> + -- ppAboves [ ppCat (map ppPStr (keysFM builtin_ids)) + -- , ppCat (map ppPStr (keysFM builtin_tcs)) + -- , ppCat (map ppPStr (keysFM b_keys)) + -- ]}) $ - BIND ( - BSCC("Rename2") - initRn12 mod_name (rnModule2 mod1) - ESCC - ) _TO_ (mod2, errs2) -> + makeHiMap opt_HiMap >>= \ hi_files -> +-- pprTrace "HiMap:\n" (ppAboves [ ppCat [ppPStr m, ppStr p] | (m,p) <- fmToList hi_files]) + newVar (emptyFM,emptyFM,hi_files){-init iface cache-} `thenPrimIO` \ iface_cache -> --- pprTrace "rename2:" (ppr PprDebug mod2) ( + fixIO ( \ ~(_, _, _, _, rec_occ_fm, rec_export_fn) -> + let + rec_occ_fn :: Name -> [RdrName] + rec_occ_fn n = case lookupUFM rec_occ_fm n of + Nothing -> [] + Just (rn,occs) -> occs - BIND (splitUniqSupply uniqs) _TO_ (us1, us2) -> + global_name_info = (b_names, b_keys, rec_export_fn, rec_occ_fn) + in + getGlobalNames iface_cache global_name_info us1 input >>= + \ (occ_env, imp_mods, unqual_imps, imp_fixes, top_errs, top_warns) -> - BIND ( - BSCC("Rename3") - initRn3 (rnModule3 gnfs imported_module_names mod2) us1 - ESCC - ) _TO_ (val_space, tc_space, v_gnf, tc_gnf, errs3) -> + if not (isEmptyBag top_errs) then + return (rn_panic, rn_panic, top_errs, top_warns, emptyUFM, rn_panic) + else + + -- No top-level name errors so rename source ... + case initRn True mod occ_env us2 + (rnSource imp_mods unqual_imps imp_fixes input) of { + ((rn_module, export_fn, src_occs), src_errs, src_warns) -> + + --pprTrace "renameModule:" (ppCat (map (ppr PprDebug . fst) (bagToList src_occs))) $ let - final_name_funs = (v_gnf, tc_gnf) + occ_fm :: UniqFM (RnName, [RdrName]) + + occ_list = [ (rn,(rn,[occ])) | (rn,occ) <- bagToList src_occs] + occ_fm = addListToUFM_C insert_occ emptyUFM occ_list + + insert_occ (rn,olds) (rn',[new]) = (rn, insert new olds) - errs_so_far = errs1 `unionBags` errs2 `unionBags` errs3 - -- see note below about why we consult errs at this pt + insert new [] = [new] + insert new xxs@(x:xs) = case cmp new x of LT_ -> new : xxs + EQ_ -> xxs + GT__ -> x : insert new xs + + occ_warns = map multipleOccWarn (filter multiple_occs (eltsUFM occ_fm)) + multiple_occs (rn, (o1:o2:_)) = True + multiple_occs _ = False in - if not (isEmptyBag errs_so_far) then -- give up now - ( panic "rename", imported_module_names, final_name_funs, errs_so_far ) + return (rn_module, imp_mods, + top_errs `unionBags` src_errs, + top_warns `unionBags` src_warns `unionBags` listToBag occ_warns, + occ_fm, export_fn) + + }) >>= \ (rn_module, imp_mods, errs_so_far, warns_so_far, occ_fm, _) -> + + if not (isEmptyBag errs_so_far) then + return (rn_panic, rn_panic, rn_panic, rn_panic, errs_so_far, warns_so_far) else - BIND ( - BSCC("Rename4") - initRn4 sw_chkr final_name_funs (rnModule4 mod2) us2 - ESCC - ) _TO_ (mod4, errs4) -> - - ( mod4, imported_module_names, final_name_funs, errs4 ) - BEND - BEND --- ) - BEND - BEND - BEND + + -- No errors renaming source so rename the interfaces ... + let + -- split up all names that occurred in the source; between + -- those that are defined therein and those merely mentioned. + -- We also divide by tycon/class and value names (as usual). + + occ_rns = [ rn | (rn,_) <- eltsUFM occ_fm ] + -- all occurrence names, from this module and imported + + (defined_here, defined_elsewhere) + = partition isLocallyDefined occ_rns + + (_, imports_used) + = partition isRnWired defined_elsewhere + + (def_tcs, def_vals) = partition isRnTyConOrClass defined_here + (occ_tcs, occ_vals) = partition isRnTyConOrClass occ_rns + -- the occ stuff includes *all* occurrences, + -- including those for which we have definitions + + (orig_def_env, orig_def_dups) + = extendGlobalRnEnv emptyRnEnv (map pair_orig def_vals) + (map pair_orig def_tcs) + (orig_occ_env, orig_occ_dups) + = extendGlobalRnEnv emptyRnEnv (map pair_orig occ_vals) + (map pair_orig occ_tcs) + + pair_orig rn = (origName rn, rn) + + -- we must ensure that the definitions of things in the BuiltinKey + -- table which may be *required* by the typechecker etc are read. + + must_haves + = [ name_fn (mkBuiltinName u pRELUDE str) + | (str, (u, name_fn)) <- fmToList b_keys, + str `notElem` [ SLIT("main"), SLIT("mainPrimIO")] ] + in + ASSERT (isEmptyBag orig_occ_dups) + ASSERT (isEmptyBag orig_def_dups) + + rnIfaces iface_cache imp_mods us3 orig_def_env orig_occ_env + rn_module (must_haves ++ imports_used) >>= + \ (rn_module_with_imports, final_env, + (implicit_val_fm, implicit_tc_fm), + usage_stuff, + (iface_errs, iface_warns)) -> + + return (rn_module_with_imports, + final_env, + imp_mods, + usage_stuff, + errs_so_far `unionBags` iface_errs, + warns_so_far `unionBags` iface_warns) + where + rn_panic = panic "renameModule: aborted with errors" + + (us1, us') = splitUniqSupply us + (us2, us3) = splitUniqSupply us' \end{code} -Why stop if errors in the first three passes: Suppose you're compiling -a module with a top-level definition named \tr{scaleFloat}. Sadly, -this is also a Prelude class-method name. \tr{rnModule3} will have -detected this error, but: it will also have picked (arbitrarily) one -of the two definitions for its final ``value'' name-function. If, by -chance, it should have picked the class-method... when it comes to pin -a Unique on the top-level (bogus) \tr{scaleFloat}, it will ask for the -class-method's Unique (!); it doesn't have one, and you will get a -panic. - -Another way to handle this would be for the duplicate detector to -clobber duplicates with some ``safe'' value. Then things would be -fine in \tr{rnModule4}. Maybe some other time... +\begin{code} +makeHiMap :: Maybe String -> IO (FiniteMap Module FilePath) + +makeHiMap Nothing = error "Rename.makeHiMap:no .hi map given by the GHC driver (?)" +makeHiMap (Just f) + = readFile f >>= \ cts -> + return (snag_mod emptyFM cts []) + where + -- we alternate between "snag"ging mod(ule names) and path(names), + -- accumulating names (reversed) and the final resulting map + -- as we move along. + + snag_mod map [] [] = map + snag_mod map (' ':cs) rmod = snag_path map (_PK_ (reverse rmod)) cs [] + snag_mod map (c:cs) rmod = snag_mod map cs (c:rmod) + + snag_path map mod [] rpath = addToFM map mod (reverse rpath) + snag_path map mod ('\n':cs) rpath = snag_mod (addToFM map mod (reverse rpath)) cs [] + snag_path map mod (c:cs) rpath = snag_path map mod cs (c:rpath) +\end{code} + +\begin{code} +{- TESTING: +pprPIface (ParsedIface m ?? v mv usgs lcm exm ims lfx ltdm lvdm lids ldp) + = ppAboves [ + ppCat [ppPStr SLIT("interface"), ppPStr m, ppInt v, + case mv of { Nothing -> ppNil; Just n -> ppInt n }], + + ppPStr SLIT("__versions__"), + ppAboves [ ppCat[ppPStr n, ppInt v] | (n,v) <- fmToList lcm ], + + ppPStr SLIT("__exports__"), + ppAboves [ ppBesides[ppPStr n, ppSP, ppr PprDebug rn, + case ex of {ExportAll -> ppStr "(..)"; _ -> ppNil}] + | (n,(rn,ex)) <- fmToList exm ], + + pp_ims (bagToList ims), + pp_fixities lfx, + pp_decls ltdm lvdm, + pp_insts (bagToList lids), + pp_pragmas ldp + ] + where + pp_ims [] = ppNil + pp_ims ms = ppAbove (ppPStr SLIT("__instance_modules__")) + (ppCat (map ppPStr ms)) + + pp_fixities fx + | isEmptyFM fx = ppNil + | otherwise = ppAboves (ppPStr SLIT("__fixities__") + : [ ppr PprDebug fix | (n, fix) <- fmToList fx]) + + pp_decls tds vds = ppAboves (ppPStr SLIT("__declarations__") + : [ pprRdrIfaceDecl d | (n, d) <- fmToList tds ++ fmToList vds]) + + pp_insts [] = ppNil + pp_insts is = ppAboves (ppPStr SLIT("__instances__") + : [ pprRdrInstDecl i | i <- is]) + + pp_pragmas ps | isEmptyFM ps = ppNil + | otherwise = panic "Rename.pp_pragmas" + +pprRdrIfaceDecl (TypeSig tc _ decl) + = ppBesides [ppStr "tycon=", ppr PprDebug tc, ppStr "; ", ppr PprDebug decl] + +pprRdrIfaceDecl (NewTypeSig tc dc _ decl) + = ppBesides [ppStr "tycon=", ppr PprDebug tc, ppStr "; datacon=", ppr PprDebug dc, + ppStr "; ", ppr PprDebug decl] + +pprRdrIfaceDecl (DataSig tc dcs dfs _ decl) + = ppBesides [ppStr "tycon=", ppr PprDebug tc, ppStr "; datacons=", ppr PprDebug dcs, + ppStr "; fields=", ppr PprDebug dfs, ppStr "; ", ppr PprDebug decl] + +pprRdrIfaceDecl (ClassSig c ops _ decl) + = ppBesides [ppStr "class=", ppr PprDebug c, ppStr "; ops=", ppr PprDebug ops, + ppStr "; ", ppr PprDebug decl] + +pprRdrIfaceDecl (ValSig f _ ty) + = ppBesides [ppr PprDebug f, ppStr " :: ", ppr PprDebug ty] + +pprRdrInstDecl (InstSig c t _ decl) + = ppBesides [ppStr "class=", ppr PprDebug c, ppStr " type=", ppr PprDebug t, ppStr "; ", + ppr PprDebug decl] +-} +\end{code}