[project @ 1998-04-07 16:40:08 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / Rename.lhs
index 3b7cdf2..5a98a5b 100644 (file)
 \section[Rename]{Renaming and dependency analysis passes}
 
 \begin{code}
-#include "HsVersions.h"
-
 module Rename ( renameModule ) where
 
-import Ubiq{-uitous-}
+#include "HsVersions.h"
 
 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 UniqSupply      ( splitUniqSupply )
-import Util            ( panic )
+import RdrHsSyn                ( RdrName(..), RdrNameHsModule )
+import RnHsSyn         ( RenamedHsModule, RenamedHsDecl, extractHsTyNames )
+
+import CmdLineOpts     ( opt_HiMap, opt_D_show_rn_trace,
+                         opt_D_dump_rn, opt_D_show_rn_stats,
+                         opt_WarnUnusedBinds, opt_WarnUnusedImports
+                       )
+import RnMonad
+import RnNames         ( getGlobalNames )
+import RnSource                ( rnDecl )
+import RnIfaces                ( getImportedInstDecls, importDecl, getImportVersions, getSpecialInstModules,
+                         getDeferredDataDecls,
+                         mkSearchPath, getSlurpedNames, getRnStats
+                       )
+import RnEnv           ( addImplicitOccsRn, availNames )
+import Name            ( Name, PrintUnqualified, Provenance, isLocallyDefined,
+                         NameSet(..),
+                         nameSetToList, minusNameSet, NamedThing(..),
+                         nameModule, pprModule, pprOccName, nameOccName
+                       )
+import TysWiredIn      ( unitTyCon, intTyCon, doubleTyCon )
+import TyCon           ( TyCon )
+import PrelMods                ( mAIN, pREL_MAIN )
+import PrelInfo                ( ioTyCon_NAME )
+import ErrUtils                ( pprBagOfErrors, pprBagOfWarnings,
+                         doIfSet, dumpIfSet, ghcExit
+                       )
+import Bag             ( isEmptyBag )
+import FiniteMap       ( fmToList, delListFromFM )
+import UniqSupply      ( UniqSupply )
+import Util            ( equivClasses )
+import Maybes          ( maybeToBool )
+import List            ( partition )
+import Outputable
 \end{code}
 
-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}
+
 
 \begin{code}
-renameModule :: PreludeNameMappers     -- lookup funs for deeply wired-in names
-            -> ProtoNameHsModule       -- input
-            -> 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
-               )
-
--- Very space-leak sensitive
-
-renameModule gnfs@(val_pnf, tc_pnf)
-            input@(HsModule mod_name _ _ _ _ _ _ _ _ _ _ _ _)
-            uniqs
-  = let
-       use_mentioned_vars = opt_UseGetMentionedVars
+renameModule :: UniqSupply
+            -> RdrNameHsModule
+            -> IO (Maybe 
+                     ( RenamedHsModule   -- Output, after renaming
+                     , InterfaceDetails  -- Interface; for interface file generatino
+                     , RnNameSupply      -- Final env; for renaming derivings
+                     , [Module]          -- Imported modules; for profiling
+                     ))
+
+renameModule us this_mod@(HsModule mod_name vers exports imports fixities local_decls loc)
+  =    -- Initialise the renamer monad
+    initRn mod_name us (mkSearchPath opt_HiMap) loc
+          (rename this_mod)                            >>=
+       \ (maybe_rn_stuff, rn_errs_bag, rn_warns_bag) ->
+
+       -- Check for warnings
+    doIfSet (not (isEmptyBag rn_warns_bag))
+           (printErrs (pprBagOfWarnings rn_warns_bag)) >>
+
+       -- Check for errors; exit if so
+    doIfSet (not (isEmptyBag rn_errs_bag))
+           (printErrs (pprBagOfErrors rn_errs_bag)      >>
+            ghcExit 1
+           )                                            >>
+
+       -- Dump output, if any
+    (case maybe_rn_stuff of
+       Nothing  -> return ()
+       Just results@(rn_mod, _, _, _)
+                -> dumpIfSet opt_D_dump_rn "Renamer:"
+                             (ppr rn_mod)
+    )                                                  >>
+
+       -- Return results
+    return maybe_rn_stuff
+\end{code}
+
+
+\begin{code}
+rename this_mod@(HsModule mod_name vers exports imports fixities local_decls loc)
+  =    -- FIND THE GLOBAL NAME ENVIRONMENT
+    getGlobalNames this_mod                    `thenRn` \ maybe_stuff ->
+
+       -- CHECK FOR EARLY EXIT
+    if not (maybeToBool maybe_stuff) then
+       -- Everything is up to date; no need to recompile further
+       rnStats []              `thenRn_`
+       returnRn Nothing
+    else
+    let
+       Just (export_env, rn_env, explicit_info, print_unqual) = maybe_stuff
     in
-    case (initRn12 mod_name (rnModule1 gnfs use_mentioned_vars input))
-      of { ((mod1, imported_module_names), errs1) ->
 
-    case (initRn12 mod_name (rnModule2 mod1)) of { (mod2, errs2) ->
+       -- RENAME THE SOURCE
+    initRnMS rn_env mod_name SourceMode (
+       addImplicits mod_name                           `thenRn_`
+       mapRn rnDecl local_decls
+    )                                                  `thenRn` \ rn_local_decls ->
 
-    case (splitUniqSupply uniqs) of { (us1, us2) ->
+       -- SLURP IN ALL THE NEEDED DECLARATIONS
+    slurpDecls print_unqual rn_local_decls             `thenRn` \ rn_all_decls ->
+
+       -- EXIT IF ERRORS FOUND
+    checkErrsRn                                `thenRn` \ no_errs_so_far ->
+    if not no_errs_so_far then
+       -- Found errors already, so exit now
+       rnStats []              `thenRn_`
+       returnRn Nothing
+    else
 
-    case (initRn3 (rnModule3 gnfs imported_module_names mod2) us1)
-      of { (val_space, tc_space, v_gnf, tc_gnf, errs3) ->
+       -- GENERATE THE VERSION/USAGE INFO
+    getImportVersions mod_name exports                 `thenRn` \ import_versions ->
+    getNameSupplyRn                                    `thenRn` \ name_supply ->
 
+       -- REPORT UNUSED NAMES
+    reportUnusedNames export_env explicit_info         `thenRn_`
+
+       -- GENERATE THE SPECIAL-INSTANCE MODULE LIST
+       -- The "special instance" modules are those modules that contain instance
+       -- declarations that contain no type constructor or class that was declared
+       -- in that module.
+    getSpecialInstModules                              `thenRn` \ imported_special_inst_mods ->
+    let
+       special_inst_decls = [d | InstD d@(InstDecl inst_ty _ _ _ _) <- rn_local_decls,
+                                 all (not.isLocallyDefined) (nameSetToList (extractHsTyNames inst_ty))
+                            ]
+       special_inst_mods | null special_inst_decls = imported_special_inst_mods
+                         | otherwise               = mod_name : imported_special_inst_mods
+    in
+                 
+    
+       -- RETURN THE RENAMED MODULE
     let
-       final_name_funs = (v_gnf, tc_gnf)
+       import_mods = [mod | ImportDecl mod _ _ _ _ _ <- imports]
 
-       errs_so_far = errs1 `unionBags` errs2 `unionBags` errs3
-               -- see note below about why we consult errs at this pt
+       renamed_module = HsModule mod_name vers 
+                                 trashed_exports trashed_imports trashed_fixities
+                                 rn_all_decls
+                                 loc
     in
-    if not (isEmptyBag errs_so_far) then -- give up now
-       ( panic "rename", imported_module_names, final_name_funs, errs_so_far )
-    else
-       case (initRn4 final_name_funs (rnModule mod2) us2)
-         of { (mod4, errs4) ->
+    rnStats rn_all_decls       `thenRn_`
+    returnRn (Just (renamed_module, 
+                   (import_versions, export_env, special_inst_mods),
+                    name_supply,
+                    import_mods))
+  where
+    trashed_exports  = {-trace "rnSource:trashed_exports"-} Nothing
+    trashed_imports  = {-trace "rnSource:trashed_imports"-} []
+    trashed_fixities = []
+\end{code}
+
+@addImplicits@ forces the renamer to slurp in some things which aren't
+mentioned explicitly, but which might be needed by the type checker.
+
+\begin{code}
+addImplicits mod_name
+  = addImplicitOccsRn (implicit_main ++ default_tys)
+  where
+       -- Add occurrences for Int, Double, and (), because they
+       -- are the types to which ambigious type variables may be defaulted by
+       -- the type checker; so they won't every appear explicitly.
+       -- [The () one is a GHC extension for defaulting CCall results.]
+    default_tys = [getName intTyCon, getName doubleTyCon, getName unitTyCon ]
+
+       -- Add occurrences for IO or PrimIO
+    implicit_main |  mod_name == mAIN
+                 || mod_name == pREL_MAIN = [ioTyCon_NAME]
+                 |  otherwise            = []
+\end{code}
+
+
+\begin{code}
+slurpDecls print_unqual decls
+  =    -- First of all, get all the compulsory decls
+    slurp_compulsories decls   `thenRn` \ decls1 ->
+
+       -- Next get the optional ones
+    closeDecls optional_mode decls1    `thenRn` \ decls2 ->
+
+       -- Finally get those deferred data type declarations
+    getDeferredDataDecls                               `thenRn` \ data_decls ->
+    mapRn (rn_data_decl compulsory_mode) data_decls    `thenRn` \ rn_data_decls ->
+
+       -- Done
+    returnRn (rn_data_decls ++ decls2)
+
+  where
+    compulsory_mode = InterfaceMode Compulsory print_unqual
+    optional_mode   = InterfaceMode Optional   print_unqual
+
+       -- The "slurp_compulsories" function is a loop that alternates
+       -- between slurping compulsory decls and slurping the instance
+       -- decls thus made relavant.
+        -- We *must* loop again here.  Why?  Two reasons:
+       -- (a) an instance decl will give rise to an unresolved dfun, whose
+       --      decl we must slurp to get its version number; that's the version
+       --      number for the whole instance decl.  (And its unfolding might mention new
+       --  unresolved names.)
+       -- (b) an instance decl might give rise to a new unresolved class,
+       --      whose decl we must slurp, which might let in some new instance decls,
+       --      and so on.  Example:  instance Foo a => Baz [a] where ...
+    slurp_compulsories decls
+      = closeDecls compulsory_mode decls       `thenRn` \ decls1 ->
+       
+               -- Instance decls still pending?
+        getImportedInstDecls                   `thenRn` \ inst_decls ->
+       if null inst_decls then 
+               -- No, none
+           returnRn decls1
+       else
+               -- Yes, there are some, so rename them and loop
+            traceRn (sep [ptext SLIT("Slurped"), int (length inst_decls), ptext SLIT("instance decls")])
+                                                               `thenRn_`
+            mapRn (rn_inst_decl compulsory_mode) inst_decls    `thenRn` \ new_inst_decls ->
+            slurp_compulsories (new_inst_decls ++ decls1)
+\end{code}
+
+\begin{code}
+closeDecls :: RnSMode
+          -> [RenamedHsDecl]                   -- Declarations got so far
+          -> RnMG [RenamedHsDecl]              -- input + extra decls slurped
+       -- The monad includes a list of possibly-unresolved Names
+       -- This list is empty when closeDecls returns
+
+closeDecls mode decls 
+  = popOccurrenceName mode             `thenRn` \ maybe_unresolved ->
+    case maybe_unresolved of
+
+       -- No more unresolved names
+       Nothing -> returnRn decls
+                       
+       -- An unresolved name
+       Just name_w_loc
+         ->    -- Slurp its declaration, if any
+--          traceRn (sep [ptext SLIT("Considering"), ppr name_w_loc])  `thenRn_`
+            importDecl name_w_loc mode         `thenRn` \ maybe_decl ->
+            case maybe_decl of
+
+               -- No declaration... (wired in thing or optional)
+               Nothing   -> closeDecls mode decls
+
+               -- Found a declaration... rename it
+               Just decl -> rn_iface_decl mod_name mode decl   `thenRn` \ new_decl ->
+                            closeDecls mode (new_decl : decls)
+                        where
+                          mod_name = nameModule (fst name_w_loc)
+
+rn_iface_decl mod_name mode decl
+  = initRnMS emptyRnEnv mod_name mode (rnDecl decl)
+                                       
+rn_inst_decl mode (mod_name,decl)      = rn_iface_decl mod_name mode (InstD decl)
+rn_data_decl mode (tycon_name,ty_decl) = rn_iface_decl mod_name mode (TyD ty_decl)
+                                      where
+                                        mod_name = nameModule tycon_name
+\end{code}
+
+\begin{code}
+reportUnusedNames (ExportEnv export_avails _) explicit_info
+  | not (opt_WarnUnusedBinds || opt_WarnUnusedImports)
+  = returnRn ()
+
+  | otherwise
+  = getSlurpedNames                    `thenRn` \ slurped_names ->
+    let
+       unused_info :: FiniteMap Name HowInScope
+       unused_info = foldl delListFromFM
+                           (delListFromFM explicit_info (nameSetToList slurped_names))
+                           (map availNames export_avails)
+       unused_list = fmToList unused_info
+
+       groups = filter wanted (equivClasses cmp unused_list)
+              where
+                (name1, his1) `cmp` (name2, his2) = his1 `cmph` his2
+                
+                (FromLocalDefn _)     `cmph` (FromImportDecl _ _)  = LT
+                (FromLocalDefn _)     `cmph` (FromLocalDefn _)     = EQ
+                (FromImportDecl m1 _) `cmph` (FromImportDecl m2 _) = m1 `compare` m2
+                h1                    `cmph` h2                    = GT
+
+       wanted ((_,FromImportDecl _ _) : _) = opt_WarnUnusedImports
+       wanted ((_,FromLocalDefn _)    : _) = opt_WarnUnusedImports
+
+       pp_imp = sep [text "Warning: the following are unused:",
+                     nest 4 (vcat (map pp_group groups))]
+
+       pp_group group = sep [msg <> char ':',
+                             nest 4 (sep (map (pprOccName . nameOccName . fst) group))]
+                      where
+                        his = case group of
+                                 ((_,his) : _) -> his
+
+                        msg = case his of
+                                 FromImportDecl m _ -> text "Imported from" <+> pprModule m
+                                 FromLocalDefn _    -> text "Locally defined"   
+
+    in
+    if null groups
+    then returnRn ()
+    else addWarnRn pp_imp
+
+rnStats :: [RenamedHsDecl] -> RnMG ()
+rnStats all_decls
+        | opt_D_show_rn_trace || 
+         opt_D_show_rn_stats ||
+         opt_D_dump_rn 
+       = getRnStats all_decls          `thenRn` \ msg ->
+         ioToRnMG (printErrs msg)      `thenRn_`
+         returnRn ()
 
-       ( mod4, imported_module_names, final_name_funs, errs4 ) }
-    }}}}
+       | otherwise = returnRn ()
 \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{rnModule}.  Maybe some other time...