[project @ 2000-10-16 08:24:18 by simonpj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Module.lhs
index 92877df..9266c58 100644 (file)
@@ -21,52 +21,54 @@ in a different DLL, by setting the DLL flag.
 \begin{code}
 module Module 
     (
-      Module               -- abstract, instance of Eq, Ord, Outputable
+      Module, moduleName, mod_kind
+                           -- abstract, instance of Eq, Ord, Outputable
     , ModuleName
+    , ModuleKind(..)
+    , isLocalModuleKind
 
     , moduleNameString         -- :: ModuleName -> EncodedString
     , moduleNameUserString     -- :: ModuleName -> UserString
+    , moduleNameFS             -- :: ModuleName -> EncodedFS
 
-    , moduleString          -- :: Module -> EncodedString
-    , moduleUserString      -- :: Module -> UserString
-    , moduleName           -- :: Module -> ModuleName
+    , moduleString             -- :: Module -> EncodedString
+    , moduleUserString         -- :: Module -> UserString
+    , moduleName               -- :: Module -> ModuleName
 
     , mkVanillaModule      -- :: ModuleName -> Module
-    , mkThisModule         -- :: ModuleName -> Module
-    , mkPrelModule          -- :: UserString -> Module
-    , mkModule             -- :: ModuleName -> PackageName -> Module
-    
-    , isLocalModule       -- :: Module -> Bool
+--    , mkThisModule       -- :: ModuleName -> Module
+    , mkPrelModule             -- :: UserString -> Module
+    , mkModule                 -- :: ModuleName -> ModuleKind -> Module
+    , isLocalModule            -- :: Module -> Bool
 
-    , mkSrcModule
+--    , mkSrcModule
 
-    , mkSrcModuleFS         -- :: UserFS    -> ModuleName
-    , mkSysModuleFS         -- :: EncodedFS -> ModuleName
+    , mkModuleName             -- :: UserString -> ModuleName
+    , mkModuleNameFS           -- :: UserFS    -> ModuleName
+    , mkSysModuleNameFS                -- :: EncodedFS -> ModuleName
 
-    , pprModule, pprModuleName
+    , pprModule,
  
     , PackageName
 
        -- Where to find a .hi file
-    , WhereFrom(..), SearchPath, mkSearchPath
-    , ModuleHiMap, mkModuleHiMaps
+    , WhereFrom(..)
+
+    , ModuleEnv,
+    , elemModuleEnv, extendModuleEnv, extendModuleEnvList, plusModuleEnv_C
+    , delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv
+    , lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv
+    , rngModuleEnv, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv
 
     ) where
 
 #include "HsVersions.h"
 import OccName
 import Outputable
-import FiniteMap
-import CmdLineOpts     ( opt_Static, opt_InPackage, opt_WarnHiShadows, opt_HiMapSep )
-import Constants       ( interfaceFileFormatVersion )
-import Maybes          ( seqMaybe )
-import Maybe           ( fromMaybe )
-import Directory       ( doesFileExist )
-import DirUtils                ( getDirectoryContents )
-import List            ( intersperse )
-import Monad           ( foldM )
-import IO              ( hPutStrLn, stderr, isDoesNotExistError )
-import FastString      ( FastString )
+import CmdLineOpts     ( opt_InPackage )
+import FastString      ( FastString, uniqueOfFS )
+import Unique          ( Uniquable(..), mkUniqueGrimily )
+import UniqFM
 \end{code}
 
 
@@ -87,20 +89,36 @@ appropriate code.
 The logic for how an interface file is marked as corresponding to a module that's
 hiding in a DLL is explained elsewhere (ToDo: give renamer href here.)
 
+@SourceOnly@ and @ObjectCode@ indicate a module from the same package
+as the one being compiled, i.e. a home module.  @InPackage@ means one
+from a different package.
+
 \begin{code}
-data PackageInfo = ThisPackage                         -- A module from the same package 
-                                               -- as the one being compiled
-                | AnotherPackage PackageName   -- A module from a different package
+data Module = Module ModuleName ModuleKind
+
+data ModuleKind
+   = SourceOnly FilePath            -- .hs
+   | ObjectCode FilePath FilePath   -- .o, .hi
+   | InPackage  PackageName
+
+moduleName (Module m _) = m
+moduleKind (Module _ k) = k
+
+isLocalModuleKind (InPackage _) = False
+isLocalModuleKind _             = True
 
 type PackageName = FastString          -- No encoding at all
 
-preludePackage :: PackageName
-preludePackage = SLIT("std")
+preludePackage :: ModuleKind
+preludePackage = InPackage SLIT("std")
 
-instance Show PackageInfo where        -- Just used in debug prints of lex tokens
-                               -- and in debug modde
-  showsPrec n ThisPackage        s = "<THIS>"   ++ s
-  showsPrec n (AnotherPackage p) s = (_UNPK_ p) ++ s
+instance Outputable ModuleKind where
+   ppr (SourceOnly path_hs) 
+      = text "SourceOnly" <+> text (show path_hs)
+   ppr (ObjectCode path_o path_hi)
+      = text "ObjectCode" <+> text (show path_o) <+> text (show path_hi)
+   ppr (InPackage pkgname)
+      = text "InPackage" <+> text (show pkgname)
 \end{code}
 
 
@@ -133,42 +151,67 @@ instance Outputable WhereFrom where
 %************************************************************************
 
 \begin{code}
-type ModuleName = EncodedFS
+newtype ModuleName = ModuleName EncodedFS
        -- Haskell module names can include the quote character ',
        -- so the module names have the z-encoding applied to them
 
+instance Uniquable ModuleName where
+  getUnique (ModuleName nm) = mkUniqueGrimily (uniqueOfFS nm)
+
+instance Eq ModuleName where
+  nm1 == nm2 = getUnique nm1 == getUnique nm2
+
+-- Warning: gives an ordering relation based on the uniques of the
+-- FastStrings which are the (encoded) module names.  This is _not_
+-- a lexicographical ordering.
+instance Ord ModuleName where
+  nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
+
+instance Outputable ModuleName where
+  ppr = pprModuleName
+
+
 pprModuleName :: ModuleName -> SDoc
-pprModuleName nm = pprEncodedFS nm
+pprModuleName (ModuleName nm) = pprEncodedFS nm
+
+moduleNameFS :: ModuleName -> EncodedFS
+moduleNameFS (ModuleName mod) = mod
 
 moduleNameString :: ModuleName -> EncodedString
-moduleNameString mod = _UNPK_ mod
+moduleNameString (ModuleName mod) = _UNPK_ mod
 
 moduleNameUserString :: ModuleName -> UserString
-moduleNameUserString mod = decode (_UNPK_ mod)
+moduleNameUserString (ModuleName mod) = decode (_UNPK_ mod)
 
-mkSrcModule :: UserString -> ModuleName
-mkSrcModule s = _PK_ (encode s)
+-- used to be called mkSrcModule
+mkModuleName :: UserString -> ModuleName
+mkModuleName s = ModuleName (_PK_ (encode s))
 
-mkSrcModuleFS :: UserFS -> ModuleName
-mkSrcModuleFS s = encodeFS s
-
-mkSysModuleFS :: EncodedFS -> ModuleName
-mkSysModuleFS s = s 
-\end{code}
+-- used to be called mkSrcModuleFS
+mkModuleNameFS :: UserFS -> ModuleName
+mkModuleNameFS s = ModuleName (encodeFS s)
 
-\begin{code}
-data Module = Module ModuleName PackageInfo
+-- used to be called mkSysModuleFS
+mkSysModuleNameFS :: EncodedFS -> ModuleName
+mkSysModuleNameFS s = ModuleName s 
 \end{code}
 
 \begin{code}
 instance Outputable Module where
   ppr = pprModule
 
+instance Uniquable Module where
+  getUnique (Module nm _) = getUnique nm
+
+-- Same if they have the same name.
 instance Eq Module where
-  (Module m1 _) == (Module m2 _) = m1 == m2
+  m1 == m2 = getUnique m1 == getUnique m2
 
+-- Warning: gives an ordering relation based on the uniques of the
+-- FastStrings which are the (encoded) module names.  This is _not_
+-- a lexicographical ordering.
 instance Ord Module where
-  (Module m1 _) `compare` (Module m2 _) = m1 `compare` m2
+  m1 `compare` m2 = getUnique m1 `compare` getUnique m2
 \end{code}
 
 
@@ -177,187 +220,94 @@ pprModule :: Module -> SDoc
 pprModule (Module mod p) = getPprStyle $ \ sty ->
                           if debugStyle sty then
                                -- Print the package too
-                               text (show p) <> dot <> pprModuleName mod
+                               ppr p <> dot <> pprModuleName mod
                           else
                                pprModuleName mod
 \end{code}
 
 
 \begin{code}
-mkModule :: ModuleName -- Name of the module
-        -> PackageName
-        -> Module
-mkModule mod_nm pack_name
-  = Module mod_nm pack_info
-  where
-    pack_info | pack_name == opt_InPackage = ThisPackage
-             | otherwise                  = AnotherPackage pack_name
-
-
+mkModule :: ModuleName -> ModuleKind -> Module
+mkModule = Module
+-- I don't think anybody except the Finder should ever try to create a
+-- Module now, so this lot commented out pro tem (JRS)
+--mkModule :: ModuleName       -- Name of the module
+--      -> PackageName
+--      -> Module
+--mkModule mod_nm pack_name
+--  = Module mod_nm pack_info
+--  where
+--    pack_info | pack_name == opt_InPackage = ThisPackage
+--           | otherwise                  = AnotherPackage pack_name
+
+
+-- Used temporarily when we first come across Foo.x in an interface
+-- file, but before we've opened Foo.hi.
+-- (Until we've opened Foo.hi we don't know what the PackageInfo is.)
 mkVanillaModule :: ModuleName -> Module
-mkVanillaModule name = Module name ThisPackage
-       -- Used temporarily when we first come across Foo.x in an interface
-       -- file, but before we've opened Foo.hi.
-       -- (Until we've opened Foo.hi we don't know what the PackageInfo is.)
+mkVanillaModule name = Module name (panic "mkVanillaModule:unknown mod_kind field")
 
-mkThisModule :: ModuleName -> Module   -- The module being compiled
-mkThisModule name = Module name ThisPackage
+--mkThisModule :: ModuleName -> Module -- The module being compiled
+--mkThisModule name = Module name ThisPackage
 
 mkPrelModule :: ModuleName -> Module
-mkPrelModule name = mkModule name preludePackage
+mkPrelModule name = Module name preludePackage
 
 moduleString :: Module -> EncodedString
-moduleString (Module mod _) = _UNPK_ mod
+moduleString (Module (ModuleName fs) _) = _UNPK_ fs
 
 moduleName :: Module -> ModuleName
 moduleName (Module mod _) = mod
 
 moduleUserString :: Module -> UserString
 moduleUserString (Module mod _) = moduleNameUserString mod
-\end{code}
 
-\begin{code}
 isLocalModule :: Module -> Bool
-isLocalModule (Module _ ThisPackage) = True
-isLocalModule _                             = False
+isLocalModule (Module nm kind) = isLocalModuleKind kind
 \end{code}
 
-
 %************************************************************************
-%*                                                                     *
-\subsection{Finding modules in the file system
-%*                                                                     *
+%*                                                                      *
+\subsection{@ModuleEnv@s}
+%*                                                                      *
 %************************************************************************
 
 \begin{code}
-type ModuleHiMap = FiniteMap ModuleName String
-  -- Mapping from module name to 
-  --   * the file path of its corresponding interface file, 
-  --   * the ModuleName
-\end{code}
-
-(We allege that) it is quicker to build up a mapping from module names
-to the paths to their corresponding interface files once, than to search
-along the import part every time we slurp in a new module (which we 
-do quite a lot of.)
-
-\begin{code}
-type SearchPath = [(String,String)]    -- List of (directory,suffix) pairs to search 
-                                        -- for interface files.
-
-mkModuleHiMaps :: SearchPath -> IO (SearchPath, ModuleHiMap, ModuleHiMap)
-mkModuleHiMaps dirs = do (hi,hi_boot) <- foldM (getAllFilesMatching dirs) (env,env) dirs
-                        return (dirs, hi, hi_boot)
- where
-  env = emptyFM
-
-getAllFilesMatching :: SearchPath
-                   -> (ModuleHiMap, ModuleHiMap)
-                   -> (FilePath, String) 
-                   -> IO (ModuleHiMap, ModuleHiMap)
-getAllFilesMatching dirs hims (dir_path, suffix) = ( do
-    -- fpaths entries do not have dir_path prepended
-  fpaths  <- getDirectoryContents dir_path
-  return (foldl addModules hims fpaths))
-  -- soft failure
-      `catch` 
-        (\ err -> do
-             hPutStrLn stderr
-                    ("Import path element `" ++ dir_path ++ 
-                     if (isDoesNotExistError err) then
-                        "' does not exist, ignoring."
-                     else
-                       "' couldn't read, ignoring.")
-              
-              return hims
-       )
- where
-   xiffus       = reverse dotted_suffix 
-   dotted_suffix = case suffix of
-                     []       -> []
-                     ('.':xs) -> suffix
-                     ls       -> '.':ls
-
-   hi_boot_version_xiffus = 
-      reverse (show interfaceFileFormatVersion) ++ '-':hi_boot_xiffus
-   hi_boot_xiffus = "toob-ih." -- .hi-boot reversed!
-
-   addModules his@(hi_env, hib_env) filename = fromMaybe his $ 
-        FMAP add_hi   (go xiffus                rev_fname)     `seqMaybe`
-
-        FMAP add_vhib (go hi_boot_version_xiffus rev_fname)    `seqMaybe`
-               -- If there's a Foo.hi-boot-N file then override any Foo.hi-boot
-
-       FMAP add_hib  (go hi_boot_xiffus         rev_fname)
-     where
-       rev_fname = reverse filename
-       path      = dir_path ++ '/':filename
-
-         -- In these functions file_nm is the base of the filename,
-         -- with the path and suffix both stripped off.  The filename
-         -- is the *unencoded* module name (else 'make' gets confused).
-         -- But the domain of the HiMaps is ModuleName which is encoded.
-       add_hi    file_nm = (add_to_map addNewOne hi_env file_nm,   hib_env)
-       add_vhib  file_nm = (hi_env, add_to_map overrideNew hib_env file_nm)
-       add_hib   file_nm = (hi_env, add_to_map addNewOne   hib_env file_nm)
-
-       add_to_map combiner env file_nm 
-         = addToFM_C combiner env mod_nm path
-         where
-           mod_nm = mkSrcModuleFS file_nm
-
-   -- go prefix (prefix ++ stuff) == Just (reverse stuff)
-   go [] xs                    = Just (_PK_ (reverse xs))
-   go _  []                    = Nothing
-   go (x:xs) (y:ys) | x == y    = go xs ys 
-                   | otherwise = Nothing
-
-   addNewOne | opt_WarnHiShadows = conflict
-            | otherwise         = stickWithOld
-
-   stickWithOld old new = old
-   overrideNew  old new = new
-
-   conflict old_path new_path
-    | old_path /= new_path = 
-        pprTrace "Warning: " (text "Identically named interface files present on the import path, " $$
-                             text (show old_path) <+> text "shadows" $$
-                             text (show new_path) $$
-                             text "on the import path: " <+> 
-                             text (concat (intersperse ":" (map fst dirs))))
-        old_path
-    | otherwise = old_path     -- don't warn about innocous shadowings.
+type ModuleEnv elt = UniqFM elt
+
+emptyModuleEnv       :: ModuleEnv a
+mkModuleEnv          :: [(Module, a)] -> ModuleEnv a
+unitModuleEnv        :: Module -> a -> ModuleEnv a
+extendModuleEnv      :: ModuleEnv a -> Module -> a -> ModuleEnv a
+plusModuleEnv        :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
+extendModuleEnvList  :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
+                  
+delModuleEnvList     :: ModuleEnv a -> [Module] -> ModuleEnv a
+delModuleEnv         :: ModuleEnv a -> Module -> ModuleEnv a
+plusModuleEnv_C      :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
+mapModuleEnv         :: (a -> b) -> ModuleEnv a -> ModuleEnv b
+rngModuleEnv         :: ModuleEnv a -> [a]
+                  
+isEmptyModuleEnv     :: ModuleEnv a -> Bool
+lookupModuleEnv      :: ModuleEnv a -> Module -> Maybe a
+lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
+elemModuleEnv        :: Module -> ModuleEnv a -> Bool
+foldModuleEnv        :: (a -> b -> b) -> b -> ModuleEnv a -> b
+
+elemModuleEnv       = elemUFM
+extendModuleEnv     = addToUFM
+extendModuleEnvList = addListToUFM
+plusModuleEnv_C     = plusUFM_C
+delModuleEnvList    = delListFromUFM
+delModuleEnv        = delFromUFM
+plusModuleEnv       = plusUFM
+lookupModuleEnv     = lookupUFM
+lookupWithDefaultModuleEnv = lookupWithDefaultUFM
+mapModuleEnv        = mapUFM
+mkModuleEnv         = listToUFM
+emptyModuleEnv      = emptyUFM
+rngModuleEnv        = eltsUFM
+unitModuleEnv       = unitUFM
+isEmptyModuleEnv    = isNullUFM
+foldModuleEnv       = foldUFM
 \end{code}
-
-
-%*********************************************************
-%*                                                      *
-\subsection{Making a search path}
-%*                                                      *
-%*********************************************************
-
-@mkSearchPath@ takes a string consisting of a colon-separated list
-of directories and corresponding suffixes, and turns it into a list
-of (directory, suffix) pairs.  For example:
-
-\begin{verbatim}
- mkSearchPath "foo%.hi:.%.p_hi:baz%.mc_hi"
-   = [("foo",".hi"),( ".", ".p_hi"), ("baz",".mc_hi")]
-\begin{verbatim}
-
-\begin{code}
-mkSearchPath :: Maybe String -> SearchPath
-mkSearchPath Nothing = [(".",".hi")]  -- ToDo: default should be to look in
-                                     -- the directory the module we're compiling
-                                     -- lives.
-mkSearchPath (Just s) = go s
-  where
-    go "" = []
-    go s  = 
-      case span (/= '%') s of
-       (dir,'%':rs) ->
-         case span (/= opt_HiMapSep) rs of
-          (hisuf,_:rest) -> (dir,hisuf):go rest
-          (hisuf,[])     -> [(dir,hisuf)]
-\end{code}
-