[project @ 1996-06-26 10:26:00 by partain]
[ghc-hetmet.git] / ghc / compiler / rename / Rename.lhs
index 780017a..8e9c81d 100644 (file)
@@ -8,13 +8,13 @@
 
 module Rename ( renameModule ) where
 
-import PreludeGlaST    ( thenPrimIO, newVar, MutableVar(..) )
+import PreludeGlaST    ( thenPrimIO )
 
-import Ubiq
+IMP_Ubiq()
 
 import HsSyn
 import RdrHsSyn                ( RdrNameHsModule(..), RdrNameImportDecl(..) )
-import RnHsSyn         ( RnName, RenamedHsModule(..), isRnTyConOrClass, isRnWired )
+import RnHsSyn         ( RnName(..){-.. is for Ix hack only-}, RenamedHsModule(..), isRnTyConOrClass, isRnWired )
 
 --ToDo:rm: all for debugging only
 import Maybes
@@ -32,17 +32,17 @@ import ParseUtils   ( ParsedIface(..), RdrIfaceDecl(..), RdrIfaceInst(..),
 import RnMonad
 import RnNames         ( getGlobalNames, GlobalNameInfo(..) )
 import RnSource                ( rnSource )
-import RnIfaces                ( findHiFiles, rnIfaces )
-import RnUtils         ( RnEnv(..), extendGlobalRnEnv, emptyRnEnv, multipleOccWarn )
+import RnIfaces                ( rnIfaces, initIfaceCache, IfaceCache )
+import RnUtils         ( SYN_IE(RnEnv), extendGlobalRnEnv, emptyRnEnv )
 
 import Bag             ( isEmptyBag, unionBags, unionManyBags, bagToList, listToBag )
-import CmdLineOpts     ( opt_HiDirList, opt_SysHiDirList )
-import ErrUtils                ( Error(..), Warning(..) )
+import CmdLineOpts     ( opt_HiMap, opt_NoImplicitPrelude )
+import ErrUtils                ( SYN_IE(Error), SYN_IE(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 Name            ( isLocallyDefined, mkWiredInName, Name, RdrName(..), ExportFlag(..) )
+import PrelInfo                ( builtinNameInfo, SYN_IE(BuiltinNames), SYN_IE(BuiltinKeys) )
+import Unique          ( ixClassKey )
 import UniqFM          ( emptyUFM, lookupUFM, addListToUFM_C, eltsUFM )
 import UniqSupply      ( splitUniqSupply )
 import Util            ( panic, assertPanic )
@@ -56,6 +56,7 @@ renameModule :: UniqSupply
                    RnEnv,              -- final env (for renaming derivings)
                    [Module],           -- imported modules; for profiling
 
+                   Name -> ExportFlag, -- export info
                    (UsagesMap,
                    VersionsMap,        -- version info; for usage
                    [Module]),          -- instance modules; for iface
@@ -65,23 +66,25 @@ renameModule :: UniqSupply
 \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 us input@(HsModule mod _ _ imports _ _ _ _ _ _ _ _ _ _)
+renameModule us input@(HsModule modname _ _ imports _ _ _ _ _ _ _ _ _ _)
 
   = let
        (b_names, b_keys, _) = builtinNameInfo
+       pp_pair (n,m) = ppBesides [ppPStr m,ppChar '.',ppPStr n]
     in
-    --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))
-    --                              ]}) $
-
-    findHiFiles opt_HiDirList opt_SysHiDirList     >>=          \ hi_files ->
-    newVar (emptyFM,emptyFM,hi_files){-init iface cache-}  `thenPrimIO` \ iface_cache ->
+    {-
+    pprTrace "builtins:\n" (case b_names of { (builtin_ids, builtin_tcs) ->
+                           ppAboves [ ppCat (map pp_pair (keysFM builtin_ids))
+                                    , ppCat (map pp_pair (keysFM builtin_tcs))
+                                    , ppCat (map pp_pair (keysFM b_keys))
+                                    ]}) $
+    -}
+    makeHiMap opt_HiMap            >>=          \ hi_files ->
+--  pprTrace "HiMap:\n" (ppAboves [ ppCat [ppPStr m, ppStr p] | (m,p) <- fmToList hi_files])
+    initIfaceCache modname hi_files  >>= \ iface_cache ->
 
     fixIO ( \ ~(_, _, _, _, rec_occ_fm, rec_export_fn) ->
     let
@@ -100,7 +103,7 @@ renameModule us input@(HsModule mod _ _ imports _ _ _ _ _ _ _ _ _ _)
     else
 
     -- No top-level name errors so rename source ...
-    case initRn True mod occ_env us2
+    case initRn True modname occ_env us2
                (rnSource imp_mods unqual_imps imp_fixes input) of {
        ((rn_module, export_fn, src_occs), src_errs, src_warns) ->
 
@@ -128,10 +131,10 @@ renameModule us input@(HsModule mod _ _ imports _ _ _ _ _ _ _ _ _ _)
            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, _) ->
+    }) >>= \ (rn_module, imp_mods, errs_so_far, warns_so_far, occ_fm, export_fn) ->
 
     if not (isEmptyBag errs_so_far) then
-       return (rn_panic, rn_panic, rn_panic, rn_panic, errs_so_far, warns_so_far)
+       return (rn_panic, rn_panic, rn_panic, rn_panic, rn_panic, errs_so_far, warns_so_far)
     else
 
     -- No errors renaming source so rename the interfaces ...
@@ -155,23 +158,37 @@ renameModule us input@(HsModule mod _ _ imports _ _ _ _ _ _ _ _ _ _)
                -- 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)
+         = extendGlobalRnEnv emptyRnEnv (map pairify_rn def_vals)
+                                        (map pairify_rn 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.
+         = extendGlobalRnEnv emptyRnEnv (map pairify_rn occ_vals)
+                                        (map pairify_rn occ_tcs)
+
+       -- This stuff is pretty dodgy right now: I think original
+       -- names and occurrence names may be getting entangled
+       -- when they shouldn't be... WDP 96/06
+
+        pairify_rn rn -- ToDo: move to Name?
+         = let
+               name = getName rn
+           in
+           (if isLocalName name
+            then Unqual (getLocalName name)
+            else case (origName "pairify_rn" name) of { OrigName m n ->
+                 Qual m n }
+            , rn)
 
        must_haves
-         = [ name_fn (mkBuiltinName u pRELUDE str) 
-           | (str, (u, name_fn)) <- fmToList b_keys,
+         | opt_NoImplicitPrelude
+         = [{-no Prelude.hi, no point looking-}]
+         | otherwise
+         = [ name_fn (mkWiredInName u orig ExportAll)
+           | (orig@(OrigName mod str), (u, name_fn)) <- fmToList b_keys,
              str `notElem` [ SLIT("main"), SLIT("mainPrimIO")] ]
     in
-    ASSERT (isEmptyBag orig_occ_dups)
+--  ASSERT (isEmptyBag orig_occ_dups)
+    (if (isEmptyBag orig_occ_dups) then \x->x
+     else pprTrace "orig_occ_dups:" (ppAboves [ ppCat [ppr PprDebug m, ppr PprDebug n, ppr PprDebug o] | (m,n,o) <- bagToList orig_occ_dups])) $
     ASSERT (isEmptyBag orig_def_dups)
 
     rnIfaces iface_cache imp_mods us3 orig_def_env orig_occ_env
@@ -184,6 +201,7 @@ renameModule us input@(HsModule mod _ _ imports _ _ _ _ _ _ _ _ _ _)
     return (rn_module_with_imports,
            final_env,
            imp_mods,
+           export_fn,
            usage_stuff,
            errs_so_far  `unionBags` iface_errs,
            warns_so_far `unionBags` iface_warns)
@@ -195,66 +213,29 @@ renameModule us input@(HsModule mod _ _ imports _ _ _ _ _ _ _ _ _ _)
 \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])
+makeHiMap :: Maybe String -> IO (FiniteMap Module FilePath)
 
-    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]
+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.
 
-pprRdrIfaceDecl (ClassSig c ops _ decl)
-  = ppBesides [ppStr "class=", ppr PprDebug c, ppStr "; ops=", ppr PprDebug ops,
-              ppStr "; ", ppr PprDebug decl]
+    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)
 
-pprRdrIfaceDecl (ValSig f _ ty)
-  = ppBesides [ppr PprDebug f, ppStr " :: ", ppr PprDebug ty]
+    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}
 
-pprRdrInstDecl (InstSig c t _ decl)
-  = ppBesides [ppStr "class=", ppr PprDebug c, ppStr " type=", ppr PprDebug t, ppStr "; ",
-               ppr PprDebug decl]
--}
+Warning message used herein:
+\begin{code}
+multipleOccWarn (name, occs) sty
+  = ppBesides [ppStr "warning:multiple names used to refer to `", ppr sty name, ppStr "': ",
+              ppInterleave ppComma (map (ppr sty) occs)]
 \end{code}