[project @ 1998-02-02 14:52:08 by simonm]
[ghc-hetmet.git] / ghc / compiler / rename / Rename.lhs
index cd531b8..0cb23f0 100644 (file)
@@ -4,39 +4,47 @@
 \section[Rename]{Renaming and dependency analysis passes}
 
 \begin{code}
-#include "HsVersions.h"
-
 module Rename ( renameModule ) where
 
-import PreludeGlaST    ( thenPrimIO )
-
-IMP_Ubiq()
-IMPORT_1_3(List(partition))
+#include "HsVersions.h"
 
 import HsSyn
-import RdrHsSyn                ( RdrName, SYN_IE(RdrNameHsModule), SYN_IE(RdrNameImportDecl) )
-import RnHsSyn         ( SYN_IE(RenamedHsModule), SYN_IE(RenamedHsDecl), extractHsTyNames )
+import RdrHsSyn                ( RdrName(..), RdrNameHsModule, RdrNameImportDecl )
+import RnHsSyn         ( RenamedHsModule, RenamedHsDecl, extractHsTyNames )
 
-import CmdLineOpts     ( opt_HiMap )
+import CmdLineOpts     ( opt_HiMap, opt_WarnNameShadowing, 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, getDecl, getImportVersions, getSpecialInstModules,
-                         mkSearchPath, getWiredInDecl
+import RnIfaces                ( getImportedInstDecls, importDecl, getImportVersions, getSpecialInstModules,
+                         getDeferredDataDecls,
+                         mkSearchPath, getSlurpedNames, getRnStats
                        )
-import RnEnv           ( availsToNameSet, addAvailToNameSet, addImplicitOccsRn )
-import Id              ( GenId {- instance NamedThing -} )
-import Name            ( Name, Provenance, ExportFlag(..), isLocallyDefined,
-                         NameSet(..), elemNameSet, mkNameSet, unionNameSets, nameSetToList,
-                         isWiredInName, modAndOcc
+import RnEnv           ( availsToNameSet, addAvailToNameSet,
+                         addImplicitOccsRn, lookupImplicitOccRn )
+import Name            ( Name, PrintUnqualified, Provenance, ExportFlag(..), 
+                         isLocallyDefined,
+                         NameSet(..), elemNameSet, mkNameSet, unionNameSets, 
+                         nameSetToList, minusNameSet, NamedThing(..),
+                         nameModule, pprModule, pprOccName, nameOccName
                        )
 import TysWiredIn      ( unitTyCon, intTyCon, doubleTyCon )
 import TyCon           ( TyCon )
-import ErrUtils                ( SYN_IE(Error), SYN_IE(Warning) )
+import PrelMods                ( mAIN, pREL_MAIN )
+import PrelInfo                ( ioTyCon_NAME )
+import ErrUtils                ( pprBagOfErrors, pprBagOfWarnings,
+                         doIfSet, dumpIfSet, ghcExit
+                       )
 import FiniteMap       ( emptyFM, eltsFM, fmToList, addToFM, FiniteMap )
-import Pretty
-import PprStyle                ( PprStyle(..) )
-import Util            ( panic, assertPanic, pprTrace )
+import Bag             ( isEmptyBag )
+import UniqSupply      ( UniqSupply )
+import Util            ( equivClasses )
+import Maybes          ( maybeToBool )
+import List            ( partition )
+import Outputable
 \end{code}
 
 
@@ -44,69 +52,78 @@ import Util         ( panic, assertPanic, pprTrace )
 \begin{code}
 renameModule :: UniqSupply
             -> RdrNameHsModule
-            -> IO (Maybe                       -- Nothing <=> everything up to date;
-                                               -- no ned to recompile any further
-                         (RenamedHsModule,     -- Output, after renaming
+            -> IO (Maybe (RenamedHsModule,     -- Output, after renaming
                           InterfaceDetails,    -- Interface; for interface file generatino
                           RnNameSupply,        -- Final env; for renaming derivings
-                          [Module]),           -- Imported modules; for profiling
-                   Bag Error, 
-                   Bag Warning
-                  )
-\end{code} 
-
+                          [Module]))           -- Imported modules; for profiling
 
-\begin{code}
 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 $
-
-       -- FIND THE GLOBAL NAME ENVIRONMENT
-    getGlobalNames this_mod                    `thenRn` \ global_name_info ->
+  =    -- 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}
 
-    case global_name_info of {
-       Nothing ->      -- Everything is up to date; no need to recompile further
-                       returnRn Nothing ;
 
-                       -- Otherwise, just carry on
-       Just (export_env, rn_env, local_avails) ->
+\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_names, print_unqual) = maybe_stuff
+    in
 
        -- RENAME THE SOURCE
-       -- We also 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.]
-    initRnMS rn_env mod_name SourceMode (mapRn rnDecl local_decls)     `thenRn` \ rn_local_decls ->
-    addImplicitOccsRn [getName intTyCon, 
-                      getName doubleTyCon, 
-                      getName unitTyCon]               `thenRn_` 
+    initRnMS rn_env mod_name SourceMode (
+       addImplicits mod_name                           `thenRn_`
+       mapRn rnDecl local_decls
+    )                                                  `thenRn` \ rn_local_decls ->
 
        -- SLURP IN ALL THE NEEDED DECLARATIONS
-       -- Notice that the rnEnv starts empty
-    closeDecls rn_local_decls (availsToNameSet local_avails) []
-                                                       `thenRn` \ (rn_all_decls, imported_avails) ->
-
-       -- SLURP IN ALL NEEDED INSTANCE DECLARATIONS
-       -- We keep the ones that only mention things (type constructors, classes) that are
-       -- already imported.  Ones which don't can't possibly be useful to us.
-    getImportedInstDecls                               `thenRn` \ imported_insts ->
-    let
-       all_big_names = mkNameSet [name | Avail name _ <- local_avails]    `unionNameSets` 
-                       mkNameSet [name | Avail name _ <- imported_avails]
-
-       rn_needed_insts = [ initRnMS emptyRnEnv mod_name InterfaceMode (rnDecl (InstD inst_decl))
-                         | (inst_names, mod_name, inst_decl) <- imported_insts,
-                           all (`elemNameSet` all_big_names) inst_names
-                         ]
-    in
-    sequenceRn rn_needed_insts                         `thenRn` \ inst_decls ->
-       -- Maybe we need to do another close-decls?
+    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
 
        -- GENERATE THE VERSION/USAGE INFO
-    getImportVersions imported_avails                  `thenRn` \ import_versions ->
+    getImportVersions mod_name exports                 `thenRn` \ import_versions ->
     getNameSupplyRn                                    `thenRn` \ name_supply ->
 
+       -- REPORT UNUSED NAMES
+    reportUnusedNames explicit_names                   `thenRn_`
 
        -- GENERATE THE SPECIAL-INSTANCE MODULE LIST
        -- The "special instance" modules are those modules that contain instance
@@ -122,79 +139,165 @@ renameModule us this_mod@(HsModule mod_name vers exports imports fixities local_
     in
                  
     
-
        -- RETURN THE RENAMED MODULE
     let
-       import_mods = [mod | ImportDecl mod _ _ _ _ <- imports]
+       import_mods = [mod | ImportDecl mod _ _ _ _ _ <- imports]
 
        renamed_module = HsModule mod_name vers 
                                  trashed_exports trashed_imports trashed_fixities
-                                 (inst_decls ++ rn_all_decls)
+                                 rn_all_decls
                                  loc
     in
+    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}
-closeDecls :: [RenamedHsDecl]                  -- Declarations got so far
-          -> NameSet                           -- Names bound by those declarations
-          -> [AvailInfo]                       -- Available stuff generated by closeDecls so far
-          -> RnMG ([RenamedHsDecl],            -- The closed set
-                   [AvailInfo])                -- Available stuff generated by closeDecls
-       -- The monad includes a list of possibly-unresolved Names
-       -- This list is empty when closeDecls returns
+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 ]
 
-closeDecls decls decl_names import_avails
-  = popOccurrenceName          `thenRn` \ maybe_unresolved ->
+       -- Add occurrences for IO or PrimIO
+    implicit_main |  mod_name == mAIN
+                 || mod_name == pREL_MAIN = [ioTyCon_NAME]
+                 |  otherwise            = []
+\end{code}
 
-    case maybe_unresolved of
 
-       -- No more unresolved names; we're done
-       Nothing ->      returnRn (decls, import_avails)
+\begin{code}
+slurpDecls print_unqual decls
+  =    -- First of all, get all the compulsory decls
+    slurp_compulsories decls   `thenRn` \ decls1 ->
 
-       -- An "unresolved" name that we've already dealt with
-       Just (name,_) | name `elemNameSet` decl_names 
-         -> closeDecls decls decl_names import_avails
+       -- 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 ->
        
-       -- An unresolved name that's wired in.  In this case there's no 
-       -- declaration to get, but we still want to record it as now available,
-       -- so that we remember to look for instance declarations involving it.
-       Just (name,_) | isWiredInName name
-         -> getWiredInDecl name        `thenRn` \ decl_avail ->
-                    closeDecls decls 
-                               (addAvailToNameSet decl_names decl_avail)
-                               (decl_avail : import_avails)
-
-       -- Genuinely unresolved name
-       Just (name,necessity) | otherwise
-         -> getDecl name               `thenRn` \ (decl_avail,new_decl) ->
-            case decl_avail of
-
-               -- Can't find the declaration; check that it was optional
-               NotAvailable -> checkRn (case necessity of { Optional -> True; other -> False})
-                                       (getDeclErr name)       `thenRn_` 
-                               closeDecls decls decl_names import_avails
-
-               -- Found it
-               other -> initRnMS emptyRnEnv mod_name InterfaceMode (
-                                    rnDecl new_decl
-                        )                              `thenRn` \ rn_decl ->
-                        closeDecls (rn_decl : decls)
-                                   (addAvailToNameSet decl_names decl_avail)
-                                   (decl_avail : import_avails)
-                    where
-                        (mod_name,_) = modAndOcc name
-
-getDeclErr name sty
-  = ppSep [ppStr "Failed to find interface decl for", ppr sty name]
+               -- 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 explicit_avail_names
+  = getSlurpedNames                    `thenRn` \ slurped_names ->
+    let
+       unused        = explicit_avail_names `minusNameSet` slurped_names
+       (local_unused, imported_unused) = partition isLocallyDefined (nameSetToList unused)
+       imports_by_module = equivClasses cmp imported_unused
+       name1 `cmp` name2 = nameModule name1 `compare` nameModule name2 
+
+       pp_imp = sep [text "Warning: the following unqualified imports are unused:",
+                         nest 4 (vcat (map pp_group imports_by_module))]
+       pp_group (n:ns) = sep [hcat [text "Module ", pprModule (nameModule n), char ':'],
+                                  nest 4 (sep (map (pprOccName . nameOccName) (n:ns)))]
+
+       pp_local = sep [text "Warning: the following local top-level definitions are unused:",
+                           nest 4 (sep (map (pprOccName . nameOccName) local_unused))]
+    in
+    (if not opt_WarnUnusedImports || null imported_unused
+     then returnRn ()
+     else addWarnRn pp_imp)    `thenRn_`
+
+    (if not opt_WarnUnusedBinds || null local_unused
+     then returnRn ()
+     else addWarnRn pp_local)
+
+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 ()
+
+       | otherwise = returnRn ()
+\end{code}