[project @ 2000-11-21 09:30:16 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnIfaces.lhs
index 91ce759..e62b780 100644 (file)
@@ -44,7 +44,7 @@ import Name           ( Name {-instance NamedThing-}, nameOccName,
                         )
 import Name            ( elemNameEnv, delFromNameEnv )
 import Module          ( Module, ModuleEnv, 
-                         moduleName, isModuleInThisPackage,
+                         moduleName, isHomeModule,
                          ModuleName, WhereFrom(..),
                          emptyModuleEnv, 
                          extendModuleEnv_C, foldModuleEnv, lookupModuleEnv,
@@ -85,53 +85,14 @@ getInterfaceExports mod_name from
 %*                                                     *
 %*********************************************************
 
-getImportVersions figures out what the ``usage information'' for this
+mkImportInof figures out what the ``usage information'' for this
 moudule is; that is, what it must record in its interface file as the
-things it uses.  It records:
-
-\begin{itemize}
-\item  (a) anything reachable from its body code
-\item  (b) any module exported with a @module Foo@
-\item   (c) anything reachable from an exported item
-\end{itemize}
-
-Why (b)?  Because if @Foo@ changes then this module's export list
-will change, so we must recompile this module at least as far as
-making a new interface file --- but in practice that means complete
-recompilation.
-
-Why (c)?  Consider this:
-\begin{verbatim}
-       module A( f, g ) where  |       module B( f ) where
-         import B( f )         |         f = h 3
-         g = ...               |         h = ...
-\end{verbatim}
-
-Here, @B.f@ isn't used in A.  Should we nevertheless record @B.f@ in
-@A@'s usages?  Our idea is that we aren't going to touch A.hi if it is
-*identical* to what it was before.  If anything about @B.f@ changes
-than anyone who imports @A@ should be recompiled in case they use
-@B.f@ (they'll get an early exit if they don't).  So, if anything
-about @B.f@ changes we'd better make sure that something in A.hi
-changes, and the convenient way to do that is to record the version
-number @B.f@ in A.hi in the usage list.  If B.f changes that'll force a
-complete recompiation of A, which is overkill but it's the only way to 
-write a new, slightly different, A.hi.
-
-But the example is tricker.  Even if @B.f@ doesn't change at all,
-@B.h@ may do so, and this change may not be reflected in @f@'s version
-number.  But with -O, a module that imports A must be recompiled if
-@B.h@ changes!  So A must record a dependency on @B.h@.  So we treat
-the occurrence of @B.f@ in the export list *just as if* it were in the
-code of A, and thereby haul in all the stuff reachable from it.
-
-[NB: If B was compiled with -O, but A isn't, we should really *still*
-haul in all the unfoldings for B, in case the module that imports A *is*
-compiled with -O.  I think this is the case.]
-
-Even if B is used at all we get a usage line for B
-       import B <n> :: ... ;
-in A.hi, to record the fact that A does import B.  This is used to decide
+things it uses.  
+
+We produce a line for every module B below the module, A, currently being
+compiled:
+       import B <n> ;
+to record the fact that A does import B indireclty.  This is used to decide
 to look to look for B.hi rather than B.hi-boot when compiling a module that
 imports A.  This line says that A imports B, but uses nothing in it.
 So we'll get an early bale-out when compiling A if B's version changes.
@@ -217,7 +178,7 @@ mkImportInfo this_mod imports
 
            mod             = mi_module iface
            mod_name        = moduleName mod
-           is_home_pkg_mod = isModuleInThisPackage mod
+           is_home_pkg_mod = isHomeModule mod
            version_info    = mi_version iface
            version_env     = vers_decls   version_info
            mod_vers        = vers_module  version_info
@@ -252,22 +213,20 @@ slurpImpDecls source_fvs
   = traceRn (text "slurpImp" <+> fsep (map ppr (nameSetToList source_fvs))) `thenRn_`
 
        -- The current slurped-set records all local things
-    getSlurped                                 `thenRn` \ source_binders ->
-    slurpSourceRefs source_binders source_fvs  `thenRn` \ (decls, needed) ->
+    slurpSourceRefs source_fvs `thenRn` \ (decls, needed) ->
 
        -- Then get everything else
     closeDecls decls needed
 
 
 -------------------------------------------------------
-slurpSourceRefs :: NameSet                     -- Variables defined in source
-               -> FreeVars                     -- Variables referenced in source
+slurpSourceRefs :: FreeVars                    -- Variables referenced in source
                -> RnMG ([RenamedHsDecl],
                         FreeVars)              -- Un-satisfied needs
 -- The declaration (and hence home module) of each gate has
 -- already been loaded
 
-slurpSourceRefs source_binders source_fvs
+slurpSourceRefs source_fvs
   = go_outer []                        -- Accumulating decls
             emptyFVs                   -- Unsatisfied needs
             emptyFVs                   -- Accumulating gates
@@ -283,8 +242,8 @@ slurpSourceRefs source_binders source_fvs
 
     go_outer decls fvs all_gates refs  -- refs are not necessarily slurped yet
        = traceRn (text "go_outer" <+> ppr refs)                `thenRn_`
-         getImportedInstDecls all_gates                        `thenRn` \ inst_decls ->
          foldlRn go_inner (decls, fvs, emptyFVs) refs          `thenRn` \ (decls1, fvs1, gates1) ->
+         getImportedInstDecls (all_gates `plusFV` gates1)      `thenRn` \ inst_decls ->
          rnIfaceInstDecls decls1 fvs1 gates1 inst_decls        `thenRn` \ (decls2, fvs2, gates2) ->
          go_outer decls2 fvs2 (all_gates `plusFV` gates2)
                               (nameSetToList (gates2 `minusNameSet` all_gates))
@@ -317,8 +276,12 @@ closeDecls decls needed
     case rule_decls of
        []    -> returnRn decls -- No new rules, so we are done
        other -> rnIfaceDecls rnIfaceRuleDecl rule_decls        `thenRn` \ rule_decls' ->
-                closeDecls (map RuleD rule_decls' ++ decls)
-                           (plusFVs (map ruleDeclFVs rule_decls'))
+                let
+                       rule_fvs = plusFVs (map ruleDeclFVs rule_decls')
+                in
+                traceRn (text "closeRules" <+> ppr rule_decls' $$ fsep (map ppr (nameSetToList rule_fvs)))     `thenRn_`
+                closeDecls (map RuleD rule_decls' ++ decls) rule_fvs
+
                 
 
 -------------------------------------------------------
@@ -376,15 +339,12 @@ recordSlurp ifaces@(Ifaces { iDecls = (decls_map, n_slurped),
     main_name  = availName avail
     mod               = nameModule main_name
     new_slurped_names = addAvailToNameSet slurped_names avail
-    new_vslurp | isModuleInThisPackage mod = (imp_mods, addOneToNameSet imp_names main_name)
-              | otherwise                 = (extendModuleSet imp_mods mod, imp_names)
+    new_vslurp | isHomeModule mod = (imp_mods, addOneToNameSet imp_names main_name)
+              | otherwise        = (extendModuleSet imp_mods mod, imp_names)
 
-recordLocalSlurps local_avails
+recordLocalSlurps new_names
   = getIfacesRn        `thenRn` \ ifaces ->
-    let
-       new_slurped_names = foldl addAvailToNameSet (iSlurp ifaces) local_avails
-    in
-    setIfacesRn (ifaces { iSlurp  = new_slurped_names })
+    setIfacesRn (ifaces { iSlurp  = iSlurp ifaces `unionNameSets` new_names })
 \end{code}
 
 
@@ -638,10 +598,10 @@ data ImportDeclResult
   | HereItIs (Module, RdrNameTyClDecl)
 
 importDecl name
-  =    -- STEP 1: Check if it was loaded before beginning this module
-    if isLocalName name then
-       traceRn (text "Already (local)" <+> ppr name) `thenRn_`
-       returnRn AlreadySlurped
+  =    -- STEP 1: Check if we've slurped it in while compiling this module
+    getIfacesRn                                `thenRn` \ ifaces ->
+    if name `elemNameSet` iSlurp ifaces then   
+       returnRn AlreadySlurped 
     else
 
        -- STEP 2: Check if it's already in the type environment
@@ -658,19 +618,13 @@ importDecl name
 
        Nothing -> 
 
-       -- STEP 3: Check if we've slurped it in while compiling this module
-    getIfacesRn                                `thenRn` \ ifaces ->
-    if name `elemNameSet` iSlurp ifaces then   
-       returnRn AlreadySlurped 
-    else
-
-       -- STEP 4: OK, we have to slurp it in from an interface file
+       -- STEP 3: OK, we have to slurp it in from an interface file
        --         First load the interface file
     traceRn nd_doc                     `thenRn_`
     loadHomeInterface nd_doc name      `thenRn_`
     getIfacesRn                                `thenRn` \ ifaces ->
 
-       -- STEP 5: Get the declaration out
+       -- STEP 4: Get the declaration out
     let
        (decls_map, _) = iDecls ifaces
     in
@@ -711,11 +665,11 @@ recompileRequired :: FilePath             -- Only needed for debug msgs
                  -> ModIface           -- Old interface
                  -> RnMG RecompileRequired
 recompileRequired iface_path source_unchanged iface
-  = traceRn (text "Considering whether compilation is required for" <+> text iface_path <> colon)      `thenRn_`
+  = traceHiDiffsRn (text "Considering whether compilation is required for" <+> text iface_path <> colon)       `thenRn_`
 
        -- CHECK WHETHER THE SOURCE HAS CHANGED
     if not source_unchanged then
-       traceRn (nest 4 (text "Source file changed or recompilation check turned off")) `thenRn_` 
+       traceHiDiffsRn (nest 4 (text "Source file changed or recompilation check turned off"))  `thenRn_` 
        returnRn outOfDate
     else
 
@@ -745,8 +699,21 @@ checkModUsage (mod_name, _, _, NothingAtAll)
        -- In this case we don't even want to open Foo's interface.
   = up_to_date (ptext SLIT("Nothing used from:") <+> ppr mod_name)
 
-checkModUsage (mod_name, _, _, whats_imported)
-  = tryLoadInterface doc_str mod_name ImportBySystem   `thenRn` \ (iface, maybe_err) ->
+checkModUsage (mod_name, _, is_boot, whats_imported)
+  =    -- Load the imported interface is possible
+       -- We use tryLoadInterface, because failure is not an error
+       -- (might just be that the old .hi file for this module is out of date)
+       -- We use ImportByUser/ImportByUserSource as the 'from' flag, 
+       --      a) because we need to know whether to load the .hi-boot file
+       --      b) because loadInterface things matters are amiss if we 
+       --         ImportBySystem an interface it knows nothing about
+    let
+       doc_str = sep [ptext SLIT("need version info for"), ppr mod_name]
+       from    | is_boot   = ImportByUserSource
+               | otherwise = ImportByUser
+    in
+    tryLoadInterface doc_str mod_name from     `thenRn` \ (iface, maybe_err) ->
+
     case maybe_err of {
        Just err -> out_of_date (sep [ptext SLIT("Can't find version number for module"), 
                                      ppr mod_name]) ;
@@ -793,8 +760,6 @@ checkModUsage (mod_name, _, _, whats_imported)
        up_to_date (ptext SLIT("...but the bits I use haven't."))
 
     }}
-  where
-    doc_str = sep [ptext SLIT("need version info for"), ppr mod_name]
 
 ------------------------
 checkModuleVersion old_mod_vers new_vers
@@ -819,8 +784,8 @@ checkEntityUsage new_vers (name,old_vers)
          | new_vers == old_vers -> returnRn upToDate
          | otherwise            -> out_of_date (sep [ptext SLIT("Out of date:"), ppr name])
 
-up_to_date  msg = traceRn msg `thenRn_` returnRn upToDate
-out_of_date msg = traceRn msg `thenRn_` returnRn outOfDate
+up_to_date  msg = traceHiDiffsRn msg `thenRn_` returnRn upToDate
+out_of_date msg = traceHiDiffsRn msg `thenRn_` returnRn outOfDate
 \end{code}