[project @ 1996-04-07 15:41:24 by partain]
[ghc-hetmet.git] / ghc / compiler / rename / Rename.lhs
index 3b7cdf2..386dcbe 100644 (file)
 
 module Rename ( renameModule ) where
 
-import Ubiq{-uitous-}
+import PreludeGlaST    ( thenPrimIO, returnPrimIO, fixPrimIO, newVar, MutableVar(..) )
+
+import Ubiq
 
 import HsSyn
-import RdrHsSyn                ( ProtoNameHsModule(..) )
-import RnHsSyn         ( RenamedHsModule(..) )
-
-import Bag             ( isEmptyBag, unionBags )
-import CmdLineOpts     ( opt_UseGetMentionedVars )
-import ErrUtils                ( Error(..) )
-import Pretty          ( Pretty(..){-ToDo:rm?-} )
-import RnMonad12       ( initRn12 )
-import RnMonad4                ( initRn4 )
-import RnPass1
-import RnPass2
-import RnPass3
-import RnPass4
-import RnUtils         ( PreludeNameMappers(..), GlobalNameMappers(..) )
+import RdrHsSyn                ( RdrNameHsModule(..), RdrNameImportDecl(..) )
+import RnHsSyn         ( RnName, RenamedHsModule(..), isRnTyCon, isRnClass )
+
+import RnMonad
+import RnNames         ( getGlobalNames, GlobalNameInfo(..) )
+import RnSource                ( rnSource )
+import RnIfaces                ( rnInterfaces, finalIfaceInfo, VersionInfo(..), ParsedIface )
+import RnUtils         ( extendGlobalRnEnv, emptyRnEnv, multipleOccWarn )
+import MainMonad
+
+import Bag             ( isEmptyBag, unionBags, bagToList, listToBag )
+import ErrUtils                ( Error(..), Warning(..) )
+import FiniteMap       ( emptyFM, eltsFM )
+import Name            ( Name, RdrName(..) )
+import Outputable      ( getOrigNameRdr, isLocallyDefined )
+import PrelInfo                ( BuiltinNames(..), BuiltinKeys(..) )
+import UniqFM          ( emptyUFM, lookupUFM, addListToUFM_C, eltsUFM )
 import UniqSupply      ( splitUniqSupply )
-import Util            ( panic )
-\end{code}
+import Util            ( panic, assertPanic )
 
-Here's what the renamer does, basically:
-\begin{description}
-\item[@RnPass1@:]
-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[@RnPass2@:]
-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[@RnPass3@:]
-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[@RnPass4@:]
-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}
+findHiFiles :: PrimIO (FiniteMap Module FAST_STRING)
+findHiFiles = returnPrimIO emptyFM
+\end{code}
 
 \begin{code}
-renameModule :: PreludeNameMappers     -- lookup funs for deeply wired-in names
-            -> ProtoNameHsModule       -- input
+renameModule :: BuiltinNames
+            -> BuiltinKeys
             -> UniqSupply
-            -> (RenamedHsModule,       -- output, after renaming
-                Bag FAST_STRING,       -- Names of the imported modules
-                                       -- (profiling needs to know this)
-                GlobalNameMappers,     -- final name funs; used later
-                                       -- to rename generated `deriving'
-                                       -- bindings.
-                Bag Error              -- Errors, from passes 1-4
+            -> RdrNameHsModule
+
+            -> MainIO
+               (
+                RenamedHsModule,  -- output, after renaming
+                [Module],         -- imported modules; for profiling
+
+                VersionInfo,      -- version info; for usage
+                [Module],         -- instance modules; for iface
+
+                Bag Error,
+                Bag Warning
                )
+\end{code}
 
--- Very space-leak sensitive
+ToDo: May want to arrange to return old interface for this module!
+ToDo: Return OrigName RnEnv to rename derivings etc with.
+ToDo: Builtin names which must be read.
+ToDo: Deal with instances (instance version, this module on instance list ???)
 
-renameModule gnfs@(val_pnf, tc_pnf)
-            input@(HsModule mod_name _ _ _ _ _ _ _ _ _ _ _ _)
-            uniqs
-  = let
-       use_mentioned_vars = opt_UseGetMentionedVars
-    in
-    case (initRn12 mod_name (rnModule1 gnfs use_mentioned_vars input))
-      of { ((mod1, imported_module_names), errs1) ->
+\begin{code}
+renameModule b_names b_keys us
+            input@(HsModule mod _ _ imports _ _ _ _ _ _ _ _ _ _)
+  = findHiFiles                        `thenPrimIO` \ hi_files ->
+    newVar (emptyFM, hi_files) `thenPrimIO` \ iface_var ->
 
-    case (initRn12 mod_name (rnModule2 mod1)) of { (mod2, errs2) ->
+    fixPrimIO ( \ (_, _, _, _, 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
 
-    case (splitUniqSupply uniqs) of { (us1, us2) ->
+       global_name_info = (b_names, b_keys, rec_export_fn, rec_occ_fn)
+    in
+    getGlobalNames iface_var global_name_info us1 input
+               `thenPrimIO` \ (occ_env, imp_mods, imp_fixes, top_errs, top_warns) ->
 
-    case (initRn3 (rnModule3 gnfs imported_module_names mod2) us1)
-      of { (val_space, tc_space, v_gnf, tc_gnf, errs3) ->
+    if not (isEmptyBag top_errs) then
+       returnPrimIO (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 imp_fixes input) of {
+       ((rn_module, export_fn, src_occs), src_errs, src_warns) ->
 
     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
 
-       errs_so_far = errs1 `unionBags` errs2 `unionBags` errs3
-               -- see note below about why we consult errs at this pt
+       insert_occ (rn,olds) (rn',[new]) = (rn, insert new olds)
+
+        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 )
+    returnPrimIO (rn_module, imp_mods,
+                 top_errs  `unionBags` src_errs,
+                 top_warns `unionBags` src_warns `unionBags` listToBag occ_warns,
+                 occ_fm, export_fn)
+
+    }) `thenPrimIO` \ (rn_module, imp_mods, errs_so_far, warns_so_far, occ_fm, _) ->
+
+    if not (isEmptyBag errs_so_far) then
+       returnMn (rn_panic, rn_panic, rn_panic, rn_panic,
+                 errs_so_far, warns_so_far)
     else
-       case (initRn4 final_name_funs (rnModule mod2) us2)
-         of { (mod4, errs4) ->
 
-       ( mod4, imported_module_names, final_name_funs, errs4 ) }
-    }}}}
-\end{code}
+    -- No errors renaming source so rename the interfaces ...
+    let
+        imports_used = [ rn | (rn,_) <- eltsUFM occ_fm, not (isLocallyDefined rn) ]
+       (import_tcs, import_vals) = partition (\ rn -> isRnTyCon rn || isRnClass rn) imports_used
+
+       (orig_env, orig_dups) = extendGlobalRnEnv emptyRnEnv (map pair_orig import_vals)
+                                                            (map pair_orig import_tcs)
+        pair_orig rn = (getOrigNameRdr rn, rn)
 
-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{rnModule}.  Maybe some other time...
+       -- ToDo: Do we need top-level names from this module in orig_env ???
+    in
+    ASSERT (isEmptyBag orig_dups)
+    rnInterfaces iface_var orig_env us3 rn_module imports_used
+               `thenPrimIO` \ (rn_module_with_imports,
+                               (implicit_val_fm, implicit_tc_fm),
+                               iface_errs, iface_warns) ->
+    let
+        all_imports_used = imports_used ++ eltsFM implicit_tc_fm ++ eltsFM implicit_val_fm
+    in
+    finalIfaceInfo iface_var all_imports_used imp_mods
+               `thenPrimIO` \ (version_info, instance_mods) ->
+
+    returnMn (rn_module_with_imports, imp_mods, 
+             version_info, instance_mods, 
+             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}