[project @ 2000-12-19 17:32:44 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnHiFiles.lhs
index ad4f8c1..d7ccd6e 100644 (file)
@@ -27,9 +27,8 @@ import HscTypes               ( ModuleLocation(..),
                          AvailInfo, GenAvailInfo(..), Avails, Deprecations(..)
                         )
 import HsSyn           ( TyClDecl(..), InstDecl(..),
-                         HsType(..), ConDecl(..), 
-                         FixitySig(..), RuleDecl(..),
-                         tyClDeclNames
+                         HsType(..), FixitySig(..), RuleDecl(..),
+                         tyClDeclNames, tyClDeclSysNames
                        )
 import RdrHsSyn                ( RdrNameTyClDecl, RdrNameInstDecl, RdrNameRuleDecl,
                          extractHsTyRdrNames 
@@ -45,7 +44,7 @@ import Name           ( Name {-instance NamedThing-}, nameOccName,
                         )
 import Name            ( mkNameEnv, extendNameEnv )
 import Module          ( Module, 
-                         moduleName, isModuleInThisPackage,
+                         moduleName, isHomeModule,
                          ModuleName, WhereFrom(..),
                          extendModuleEnv, mkVanillaModule
                        )
@@ -60,6 +59,9 @@ import Lex
 import FiniteMap
 import Outputable
 import Bag
+import Config
+
+import Directory
 \end{code}
 
 
@@ -104,8 +106,9 @@ tryLoadInterface :: SDoc -> ModuleName -> WhereFrom -> RnM d (ModIface, Maybe Me
   -- (If the load fails, we plug in a vanilla placeholder)
 tryLoadInterface doc_str mod_name from
  = getHomeIfaceTableRn         `thenRn` \ hit ->
+   getModuleRn                 `thenRn` \ this_mod ->
    getIfacesRn                         `thenRn` \ ifaces@(Ifaces { iPIT = pit }) ->
-       
+
        -- CHECK WHETHER WE HAVE IT ALREADY
    case lookupIfaceByModName hit pit mod_name of {
        Just iface |  case from of
@@ -146,6 +149,12 @@ tryLoadInterface doc_str mod_name from
    warnCheckRn (not redundant_source_import)
                (warnRedundantSourceImport mod_name)    `thenRn_`
 
+       -- Check that we aren't importing ourselves. 
+       -- That only happens in Rename.checkOldIface, 
+       -- which doesn't call tryLoadInterface
+   warnCheckRn (moduleName this_mod /= mod_name)
+               (warnSelfImport this_mod)               `thenRn_`
+
        -- READ THE MODULE IN
    findAndReadIface doc_str mod_name hi_boot_file   `thenRn` \ read_result ->
    case read_result of {
@@ -174,7 +183,7 @@ tryLoadInterface doc_str mod_name from
        -- about, it should be from a different package to this one
     WARN( not (maybeToBool mod_info) && 
          case from of { ImportBySystem -> True; other -> False } &&
-         isModuleInThisPackage mod,
+         isHomeModule mod,
          ppr mod )
 
     loadDecls mod              (iDecls ifaces)   (pi_decls iface)      `thenRn` \ (decls_vers, new_decls) ->
@@ -196,7 +205,14 @@ tryLoadInterface doc_str mod_name from
                        ImportByUser -> addModDeps mod is_loaded (pi_usages iface) mod_map
                        other        -> mod_map
        mod_map2 = delFromFM mod_map1 mod_name
-       is_loaded m = maybeToBool (lookupIfaceByModName hit pit m)
+
+       this_mod_name = moduleName this_mod
+       is_loaded m   =  m == this_mod_name 
+                     || maybeToBool (lookupIfaceByModName hit pit m)
+               -- We treat the currently-being-compiled module as 'loaded' because
+               -- even though it isn't yet in the HIT or PIT; otherwise it gets
+               -- put into iImpModInfo, and then spat out into its own interface
+               -- file as a dependency
 
        -- Now add info about this module to the PIT
        has_orphans = pi_orphan iface
@@ -207,7 +223,7 @@ tryLoadInterface doc_str mod_name from
                               mi_fixities = fix_env, mi_deprecs = deprec_env,
                               mi_usages  = [], -- Will be filled in later
                               mi_decls   = panic "No mi_decls in PIT",
-                              mi_globals = panic "No mi_globals in PIT"
+                              mi_globals = mkIfaceGlobalRdrEnv avails
                    }
 
        new_ifaces = ifaces { iPIT        = new_pit,
@@ -239,7 +255,7 @@ addModDeps mod is_loaded new_deps mod_deps
        -- and in that case, forget about the boot indicator
     filtered_new_deps :: [(ModuleName, (WhetherHasOrphans, IsBootInterface))]
     filtered_new_deps
-       | isModuleInThisPackage mod 
+       | isHomeModule mod 
                            = [ (imp_mod, (has_orphans, is_boot))
                              | (imp_mod, has_orphans, is_boot, _) <- new_deps,
                                not (is_loaded imp_mod)
@@ -260,13 +276,12 @@ addModDeps mod is_loaded new_deps mod_deps
 
 loadExports :: (Version, [ExportItem]) -> RnM d (Version, [(ModuleName,Avails)])
 loadExports (vers, items)
-  = getModuleRn                                `thenRn` \ this_mod ->
-    mapRn (loadExport this_mod) items          `thenRn` \ avails_s ->
+  = mapRn loadExport items     `thenRn` \ avails_s ->
     returnRn (vers, avails_s)
 
 
-loadExport :: Module -> ExportItem -> RnM d (ModuleName, Avails)
-loadExport this_mod (mod, entities)
+loadExport :: ExportItem -> RnM d (ModuleName, Avails)
+loadExport (mod, entities)
   = mapRn (load_entity mod) entities   `thenRn` \ avails ->
     returnRn (mod, avails)
   where
@@ -292,8 +307,11 @@ loadDecls mod (decls_map, n_slurped) decls
     returnRn (vers, (decls_map', n_slurped))
 
 loadDecl mod (version_map, decls_map) (version, decl)
-  = getIfaceDeclBinders mod decl       `thenRn` \ full_avail ->
+  = getTyClDeclBinders mod decl        `thenRn` \ (avail, sys_names) ->
     let
+       full_avail    = case avail of
+                         Avail n -> avail
+                         AvailTC n ns -> AvailTC n (sys_names ++ ns)
        main_name     = availName full_avail
        new_decls_map = extendNameEnvList decls_map stuff
        stuff         = [ (name, (full_avail, name==main_name, (mod, decl))) 
@@ -415,50 +433,27 @@ It doesn't deal with source-code specific things: @ValD@, @DefD@.  They
 are handled by the sourc-code specific stuff in @RnNames@.
 
 \begin{code}
-getIfaceDeclBinders, getTyClDeclBinders
+getTyClDeclBinders
        :: Module
        -> RdrNameTyClDecl
-       -> RnM d AvailInfo
+       -> RnM d (AvailInfo, [Name])    -- The [Name] are the system names
 
-getIfaceDeclBinders mod tycl_decl
-  = getTyClDeclBinders    mod tycl_decl        `thenRn` \ avail ->
-    getSysTyClDeclBinders mod tycl_decl        `thenRn` \ extras ->
-    returnRn (addSysAvails avail extras)
-               -- Add the sys-binders to avail.  When we import the decl,
-               -- it's full_avail that will get added to the 'already-slurped' set (iSlurp)
-               -- If we miss out sys-binders, we'll read the decl multiple times!
-
-getTyClDeclBinders mod (IfaceSig var ty prags src_loc)
+-----------------
+getTyClDeclBinders mod (IfaceSig {tcdName = var, tcdLoc = src_loc})
   = newTopBinder mod var src_loc                       `thenRn` \ var_name ->
-    returnRn (Avail var_name)
+    returnRn (Avail var_name, [])
 
 getTyClDeclBinders mod tycl_decl
-  = mapRn do_one (tyClDeclNames tycl_decl)     `thenRn` \ (main_name:sub_names) ->
-    returnRn (AvailTC main_name (main_name : sub_names))
-  where
-    do_one (name,loc) = newTopBinder mod name loc
-\end{code}
+  = new_top_bndrs mod (tyClDeclNames tycl_decl)                `thenRn` \ names@(main_name:_) ->
+    new_top_bndrs mod (tyClDeclSysNames tycl_decl)     `thenRn` \ sys_names ->
+    returnRn (AvailTC main_name names, sys_names)
 
-@getDeclSysBinders@ gets the implicit binders introduced by a decl.
-A the moment that's just the tycon and datacon that come with a class decl.
-They aren't returned by @getDeclBinders@ because they aren't in scope;
-but they {\em should} be put into the @DeclsMap@ of this module.
-
-Note that this excludes the default-method names of a class decl,
-and the dict fun of an instance decl, because both of these have 
-bindings of their own elsewhere.
-
-\begin{code}
-getSysTyClDeclBinders mod (ClassDecl _ cname _ _ sigs _ names src_loc)
-  = sequenceRn [newTopBinder mod n src_loc | n <- names]
-
-getSysTyClDeclBinders mod (TyData _ _ _ _ cons _ _ _ _ _)
-  = sequenceRn [newTopBinder mod wkr_name src_loc | ConDecl _ wkr_name _ _ _ src_loc <- cons]
-
-getSysTyClDeclBinders mod other_decl
-  = returnRn []
+-----------------
+new_top_bndrs mod names_w_locs
+  = sequenceRn [newTopBinder mod name loc | (name,loc) <- names_w_locs]
 \end{code}
 
+
 %*********************************************************
 %*                                                     *
 \subsection{Reading an interface file}
@@ -480,15 +475,16 @@ findAndReadIface doc_str mod_name hi_boot_file
     case maybe_found of
 
       Right (Just (wanted_mod,locn))
-        -> readIface (mkHiPath hi_boot_file (ml_hi_file locn)) `thenRn` \ read_result ->
+        -> mkHiPath hi_boot_file locn `thenRn` \ file -> 
+          readIface file `thenRn` \ read_result ->
           case read_result of
-              Left bad -> returnRn (Left bad)
-              Right iface 
-                 -> let read_mod = pi_mod iface
-                   in warnCheckRn (wanted_mod == read_mod)
-                                  (hiModuleNameMismatchWarn wanted_mod read_mod) 
-                                       `thenRn_`
-                      returnRn (Right (wanted_mod, iface))
+                Left bad -> returnRn (Left bad)
+                Right iface 
+                   -> let read_mod = pi_mod iface
+                     in warnCheckRn (wanted_mod == read_mod)
+                                    (hiModuleNameMismatchWarn wanted_mod 
+                                       read_mod) `thenRn_`
+                        returnRn (Right (wanted_mod, iface))
        -- Can't find it
       other   -> traceRn (ptext SLIT("...not found"))  `thenRn_`
                 returnRn (Left (noIfaceErr mod_name hi_boot_file))
@@ -500,9 +496,15 @@ findAndReadIface doc_str mod_name hi_boot_file
                           ppr mod_name <> semi],
                     nest 4 (ptext SLIT("reason:") <+> doc_str)]
 
-mkHiPath hi_boot_file (Just path)
-  | hi_boot_file = path ++ "-boot-5"
-  | otherwise    = path
+mkHiPath hi_boot_file locn
+  | hi_boot_file = 
+       ioToRnM_no_fail (doesFileExist hi_boot_ver_path) `thenRn` \ b ->
+       if b then returnRn hi_boot_ver_path
+            else returnRn hi_boot_path
+  | otherwise    = returnRn hi_path
+       where (Just hi_path)   = ml_hi_file locn
+             hi_boot_path     = hi_path ++ "-boot"
+             hi_boot_ver_path = hi_path ++ "-boot-" ++ cHscIfaceFileVersion
 \end{code}
 
 @readIface@ tries just the one file.
@@ -512,7 +514,8 @@ readIface :: String -> RnM d (Either Message ParsedIface)
        -- Nothing <=> file not found, or unreadable, or illegible
        -- Just x  <=> successfully found and parsed 
 readIface file_path
-  = traceRn (ptext SLIT("readIFace") <+> text file_path)       `thenRn_` 
+  = --ioToRnM (putStrLn ("reading iface " ++ file_path)) `thenRn_`
+    traceRn (ptext SLIT("readIFace") <+> text file_path)       `thenRn_` 
 
     ioToRnM (hGetStringBuffer False file_path)                 `thenRn` \ read_result ->
     case read_result of {
@@ -612,5 +615,8 @@ hiModuleNameMismatchWarn requested_mod read_mod =
 warnRedundantSourceImport mod_name
   = ptext SLIT("Unnecessary {- SOURCE -} in the import of module")
           <+> quotes (ppr mod_name)
+
+warnSelfImport mod
+  = ptext SLIT("Importing my own interface: module") <+> ppr mod
 \end{code}