[project @ 2002-06-07 09:40:05 by matthewc]
[ghc-hetmet.git] / ghc / compiler / rename / RnHiFiles.lhs
index 45fb805..3bd71f9 100644 (file)
@@ -9,45 +9,44 @@ module RnHiFiles (
        tryLoadInterface, loadOrphanModules,
        loadExports, loadFixDecls, loadDeprecs,
 
-       lookupFixityRn, 
-
        getTyClDeclBinders
    ) where
 
 #include "HsVersions.h"
 
-import DriverState     ( GhcMode(..), v_GhcMode )
+import DriverState     ( v_GhcMode, isCompManagerMode )
 import DriverUtil      ( splitFilename )
 import CmdLineOpts     ( opt_IgnoreIfacePragmas )
+import Parser          ( parseIface )
 import HscTypes                ( ModuleLocation(..),
                          ModIface(..), emptyModIface,
                          VersionInfo(..), ImportedModuleInfo,
-                         lookupIfaceByModName, 
+                         lookupIfaceByModName, RdrExportItem,
                          ImportVersion, WhetherHasOrphans, IsBootInterface,
                          DeclsMap, GatedDecl, IfaceInsts, IfaceRules,
                          AvailInfo, GenAvailInfo(..), Avails, Deprecations(..)
                         )
-import HsSyn           ( TyClDecl(..), InstDecl(..),
-                         HsType(..), HsPred(..), FixitySig(..), RuleDecl(..),
-                         tyClDeclNames, tyClDeclSysNames, hsTyVarNames, getHsInstHead,
+import HsSyn           ( TyClDecl(..), InstDecl(..), RuleDecl(..),
+                         tyClDeclNames, tyClDeclSysNames, hsTyVarNames, 
+                         getHsInstHead,
                        )
 import RdrHsSyn                ( RdrNameTyClDecl, RdrNameInstDecl, RdrNameRuleDecl )
 import RnHsSyn         ( extractHsTyNames_s )
-import BasicTypes      ( Version, defaultFixity )
+import BasicTypes      ( Version )
 import RnTypes         ( rnHsType )
 import RnEnv
 import RnMonad
-import ParseIface      ( parseIface )
 
+import PrelNames       ( gHC_PRIM_Name, gHC_PRIM )
 import Name            ( Name {-instance NamedThing-}, 
-                         nameModule, isLocalName, nameIsLocalOrFrom
+                         nameModule, isInternalName, nameIsLocalOrFrom
                         )
 import NameEnv
 import NameSet
 import Module
 import RdrName         ( rdrNameOcc )
 import SrcLoc          ( mkSrcLoc )
-import Maybes          ( maybeToBool, orElse )
+import Maybes          ( maybeToBool )
 import StringBuffer     ( hGetStringBuffer )
 import FastString      ( mkFastString )
 import ErrUtils         ( Message )
@@ -57,10 +56,16 @@ import FiniteMap
 import ListSetOps      ( minusList )
 import Outputable
 import Bag
+import BinIface                ( {- just instances -} )
+import qualified Binary
+import Panic
 import Config
 
 import IOExts
+import Exception
+import Dynamic         ( fromDynamic )
 import Directory
+import List            ( isSuffixOf )
 \end{code}
 
 
@@ -73,7 +78,7 @@ import Directory
 \begin{code}
 loadHomeInterface :: SDoc -> Name -> RnM d ModIface
 loadHomeInterface doc_str name
-  = ASSERT2( not (isLocalName name), ppr name <+> parens doc_str )
+  = ASSERT2( not (isInternalName name), ppr name <+> parens doc_str )
     loadInterface doc_str (moduleName (nameModule name)) ImportBySystem
 
 loadOrphanModules :: [ModuleName] -> RnM d ()
@@ -221,7 +226,8 @@ tryLoadInterface doc_str mod_name from
        -- Now add info about this module to the PIT
        has_orphans = pi_orphan iface
        new_pit   = extendModuleEnv pit mod mod_iface
-       mod_iface = ModIface { mi_module = mod, mi_version = version,
+       mod_iface = ModIface { mi_module = mod, mi_package = pi_pkg iface,
+                              mi_version = version,
                               mi_orphan = has_orphans, mi_boot = hi_boot_file,
                               mi_exports = avails, 
                               mi_fixities = fix_env, mi_deprecs = deprec_env,
@@ -277,13 +283,13 @@ addModDeps mod is_loaded new_deps mod_deps
 --     Loading the export list
 -----------------------------------------------------
 
-loadExports :: (Version, [ExportItem]) -> RnM d (Version, [(ModuleName,Avails)])
+loadExports :: (Version, [RdrExportItem]) -> RnM d (Version, [(ModuleName,Avails)])
 loadExports (vers, items)
   = mapRn loadExport items     `thenRn` \ avails_s ->
     returnRn (vers, avails_s)
 
 
-loadExport :: ExportItem -> RnM d (ModuleName, Avails)
+loadExport :: RdrExportItem -> RnM d (ModuleName, Avails)
 loadExport (mod, entities)
   = mapRn (load_entity mod) entities   `thenRn` \ avails ->
     returnRn (mod, avails)
@@ -335,7 +341,7 @@ loadFixDecls mod decls
   where
     mod_name = moduleName mod
 
-loadFixDecl mod_name sig@(FixitySig rdr_name fixity loc)
+loadFixDecl mod_name (rdr_name, fixity)
   = newGlobalName mod_name (rdrNameOcc rdr_name)       `thenRn` \ name ->
     returnRn (name, fixity)
 
@@ -463,6 +469,10 @@ getTyClDeclBinders mod (IfaceSig {tcdName = var, tcdLoc = src_loc})
   = newTopBinder mod var src_loc                       `thenRn` \ var_name ->
     returnRn (Avail var_name, [])
 
+getTyClDeclBinders mod (CoreDecl {tcdName = var, tcdLoc = src_loc})
+  = newTopBinder mod var src_loc                       `thenRn` \ var_name ->
+    returnRn (Avail var_name, [])
+
 getTyClDeclBinders mod tycl_decl
   = new_top_bndrs mod (tyClDeclNames tycl_decl)                `thenRn` \ names@(main_name:_) ->
     new_top_bndrs mod (tyClDeclSysNames tycl_decl)     `thenRn` \ sys_names ->
@@ -491,14 +501,18 @@ findAndReadIface :: SDoc -> ModuleName
 findAndReadIface doc_str mod_name hi_boot_file
   = traceRn trace_msg                  `thenRn_`
 
+    -- Check for GHC.Prim, and return its static interface
+    if mod_name == gHC_PRIM_Name
+       then returnRn (Right (gHC_PRIM, ghcPrimIface))
+       else
+
     -- In interactive or --make mode, we are *not allowed* to demand-load
     -- a home package .hi file.  So don't even look for them.
     -- This helps in the case where you are sitting in eg. ghc/lib/std
     -- and start up GHCi - it won't complain that all the modules it tries
     -- to load are found in the home location.
     ioToRnM_no_fail (readIORef v_GhcMode) `thenRn` \ mode ->
-    let home_allowed = hi_boot_file ||
-                      mode `notElem` [ DoInteractive, DoMake ]
+    let home_allowed = hi_boot_file || not (isCompManagerMode mode)
     in
 
     ioToRnM (if home_allowed 
@@ -512,18 +526,14 @@ findAndReadIface doc_str mod_name hi_boot_file
           readIface file `thenRn` \ read_result ->
           case read_result of
                 Left bad -> returnRn (Left bad)
-                Right iface 
-                   -> let read_mod = pi_mod iface
-                     in -- check that the module names agree
-                        checkRn
-                          (wanted_mod == read_mod)
-                          (hiModuleNameMismatchWarn wanted_mod read_mod)
+                Right iface ->  -- check that the module names agree
+                     let read_mod_name = pi_mod iface
+                         wanted_mod_name = moduleName wanted_mod
+                     in
+                     checkRn
+                         (wanted_mod_name == read_mod_name)
+                         (hiModuleNameMismatchWarn wanted_mod_name read_mod_name)
                                        `thenRn_`
-                        -- check that the package names agree
-                        warnCheckRn 
-                          (modulePackage wanted_mod == modulePackage read_mod)
-                          (packageNameMismatchWarn wanted_mod read_mod)
-                                        `thenRn_`
                         returnRn (Right (wanted_mod, iface))
        -- Can't find it
       other   -> traceRn (ptext SLIT("...not found"))  `thenRn_`
@@ -558,76 +568,47 @@ readIface file_path
   = --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 {
-       Left io_error  -> bale_out (text (show io_error)) ;
+  let hi_boot_ver = "hi-boot-" ++ cHscIfaceFileVersion in
+  if ".hi-boot" `isSuffixOf` file_path
+     || hi_boot_ver `isSuffixOf` file_path then
+
+      ioToRnM (hGetStringBuffer file_path) `thenRn` \ read_result ->
+      case read_result of {
+       Left io_error  -> bale_out (text (show io_error));
        Right contents -> 
 
-    case parseIface contents init_parser_state of
-       POk _ iface          -> returnRn (Right iface)
+      case parseIface contents (mkPState loc exts) of {
+       POk _ iface          -> returnRn (Right iface);
        PFailed err          -> bale_out err
-    }
-  where
-    init_parser_state = PState{ bol = 0#, atbol = 1#,
-                               context = [],
-                               glasgow_exts = 1#,
-                               loc = mkSrcLoc (mkFastString file_path) 1 }
+     }}
 
-    bale_out err = returnRn (Left (badIfaceFile file_path err))
-\end{code}
+  else
+      ioToRnM_no_fail (myTry (Binary.getBinFileWithDict file_path)) 
+         `thenRn` \ either_iface ->
 
-%*********************************************************
-%*                                                     *
-\subsection{Looking up fixities}
-%*                                                     *
-%*********************************************************
-
-@lookupFixityRn@ has to be in RnIfaces (or RnHiFiles), instead of
-its obvious home in RnEnv,  because it calls @loadHomeInterface@.
+      case either_iface of
+        Right iface -> returnRn (Right iface)
+       Left (DynException d) | Just e <- fromDynamic d
+               -> bale_out (text (show (e :: GhcException)))
 
-lookupFixity is a bit strange.  
+        Left err -> bale_out (text (show err))
 
-* Nested local fixity decls are put in the local fixity env, which we
-  find with getFixtyEnv
-
-* Imported fixities are found in the HIT or PIT
+  where
+    exts = ExtFlags {glasgowExtsEF = True,
+                    ffiEF         = True,
+                    withEF        = True,
+                    parrEF        = True}
+    loc  = mkSrcLoc (mkFastString file_path) 1
 
-* Top-level fixity decls in this module may be for Names that are
-    either  Global        (constructors, class operations)
-    or             Local/Exported (everything else)
-  (See notes with RnNames.getLocalDeclBinders for why we have this split.)
-  We put them all in the local fixity environment
+    bale_out err = returnRn (Left (badIfaceFile file_path err))
 
-\begin{code}
-lookupFixityRn :: Name -> RnMS Fixity
-lookupFixityRn name
-  = getModuleRn                                `thenRn` \ this_mod ->
-    if nameIsLocalOrFrom this_mod name
-    then       -- It's defined in this module
-       getFixityEnv                    `thenRn` \ local_fix_env ->
-       returnRn (lookupLocalFixity local_fix_env name)
-
-    else       -- It's imported
-      -- For imported names, we have to get their fixities by doing a
-      -- loadHomeInterface, and consulting the Ifaces that comes back
-      -- from that, because the interface file for the Name might not
-      -- have been loaded yet.  Why not?  Suppose you import module A,
-      -- which exports a function 'f', thus;
-      --        module CurrentModule where
-      --         import A( f )
-      --       module A( f ) where
-      --         import B( f )
-      -- Then B isn't loaded right away (after all, it's possible that
-      -- nothing from B will be used).  When we come across a use of
-      -- 'f', we need to know its fixity, and it's then, and only
-      -- then, that we load B.hi.  That is what's happening here.
-       loadHomeInterface doc name              `thenRn` \ iface ->
-       returnRn (lookupNameEnv (mi_fixities iface) name `orElse` defaultFixity)
-  where
-    doc      = ptext SLIT("Checking fixity for") <+> ppr name
+#if __GLASGOW_HASKELL__ < 501
+myTry = Exception.tryAllIO
+#else
+myTry = Exception.try
+#endif
 \end{code}
 
-
 %*********************************************************
 %*                                                      *
 \subsection{Errors}
@@ -645,23 +626,14 @@ badIfaceFile file err
   = vcat [ptext SLIT("Bad interface file:") <+> text file, 
          nest 4 err]
 
-hiModuleNameMismatchWarn :: Module -> Module  -> Message
+hiModuleNameMismatchWarn :: ModuleName -> ModuleName -> Message
 hiModuleNameMismatchWarn requested_mod read_mod = 
     hsep [ ptext SLIT("Something is amiss; requested module name")
-        , ppr (moduleName requested_mod)
+        , ppr requested_mod
         , ptext SLIT("differs from name found in the interface file")
         , ppr read_mod
         ]
 
-packageNameMismatchWarn :: Module -> Module  -> Message
-packageNameMismatchWarn requested_mod read_mod = 
-    fsep [ ptext SLIT("Module"), quotes (ppr requested_mod), 
-         ptext SLIT("is located in package"), 
-         quotes (ptext (modulePackage requested_mod)),
-         ptext SLIT("but its interface file claims it is part of package"),
-         quotes (ptext (modulePackage read_mod))
-       ]
-
 warnRedundantSourceImport mod_name
   = ptext SLIT("Unnecessary {- SOURCE -} in the import of module")
           <+> quotes (ppr mod_name)