[project @ 2003-03-27 08:18:21 by simonpj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Module.lhs
index 913add3..4b59757 100644 (file)
-%\r
-% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998\r
-%\r
-\section[Module]{The @Module@ module.}\r
-\r
-Representing modules and their flavours.\r
-\r
-\begin{code}\r
-module Module \r
-    (\r
-      Module               -- abstract, instance of Eq, Ord, Outputable\r
-    , ModuleName\r
-\r
-    , moduleNameString         -- :: ModuleName -> EncodedString\r
-    , moduleNameUserString     -- :: ModuleName -> UserString\r
-\r
-    , moduleString          -- :: Module -> EncodedString\r
-    , moduleUserString      -- :: Module -> UserString\r
-    , moduleName           -- :: Module -> ModuleName\r
-\r
-    , mkVanillaModule      -- :: ModuleName -> Module\r
-    , mkThisModule         -- :: ModuleName -> Module\r
-    , mkPrelModule          -- :: UserString -> Module\r
-\r
-    , isDynamicModule       -- :: Module -> Bool\r
-    , isLibModule\r
-\r
-    , mkSrcModule\r
-\r
-    , mkSrcModuleFS         -- :: UserFS    -> ModuleName\r
-    , mkSysModuleFS         -- :: EncodedFS -> ModuleName\r
-\r
-    , pprModule, pprModuleName\r
\r
-       -- DllFlavour\r
-    , DllFlavour, dll, notDll\r
-\r
-       -- ModFlavour\r
-    , ModFlavour, libMod, userMod\r
-\r
-       -- Where to find a .hi file\r
-    , WhereFrom(..), SearchPath, mkSearchPath\r
-    , ModuleHiMap, mkModuleHiMaps\r
-\r
-    ) where\r
-\r
-#include "HsVersions.h"\r
-import OccName\r
-import Outputable\r
-import FiniteMap\r
-import CmdLineOpts     ( opt_Static, opt_CompilingPrelude, opt_WarnHiShadows )\r
-import Constants       ( interfaceFileFormatVersion )\r
-import Maybes          ( seqMaybe )\r
-import Maybe           ( fromMaybe )\r
-import Directory       ( doesFileExist )\r
-import DirUtils                ( getDirectoryContents )\r
-import List            ( intersperse )\r
-import Monad           ( foldM )\r
-import IO              ( hPutStrLn, stderr, isDoesNotExistError )\r
-\end{code}\r
-\r
-\r
-%************************************************************************\r
-%*                                                                     *\r
-\subsection{Interface file flavour}\r
-%*                                                                     *\r
-%************************************************************************\r
-\r
-A further twist to the tale is the support for dynamically linked libraries under\r
-Win32. Here, dealing with the use of global variables that's residing in a DLL\r
-requires special handling at the point of use (there's an extra level of indirection,\r
-i.e., (**v) to get at v's value, rather than just (*v) .) When slurping in an\r
-interface file we then record whether it's coming from a .hi corresponding to a\r
-module that's packaged up in a DLL or not, so that we later can emit the\r
-appropriate code.\r
-\r
-The logic for how an interface file is marked as corresponding to a module that's\r
-hiding in a DLL is explained elsewhere (ToDo: give renamer href here.)\r
-\r
-\begin{code}\r
-data DllFlavour = NotDll       -- Ordinary module\r
-               | Dll           -- The module's object code lives in a DLL.\r
-               deriving( Eq )\r
-\r
-dll    = Dll\r
-notDll = NotDll\r
-\r
-instance Text DllFlavour where -- Just used in debug prints of lex tokens\r
-  showsPrec n NotDll s = s\r
-  showsPrec n Dll    s = "dll " ++ s\r
-\end{code}\r
-\r
-\r
-%************************************************************************\r
-%*                                                                     *\r
-\subsection{System/user module}\r
-%*                                                                     *\r
-%************************************************************************\r
-\r
-We also track whether an imported module is from a 'system-ish' place.  In this case\r
-we don't record the fact that this module depends on it, nor usages of things\r
-inside it.  \r
-\r
-\begin{code}\r
-data ModFlavour = LibMod       -- A library-ish module\r
-               | UserMod       -- Not library-ish\r
-\r
-libMod  = LibMod\r
-userMod = UserMod\r
-\end{code}\r
-\r
-\r
-%************************************************************************\r
-%*                                                                     *\r
-\subsection{Where from}\r
-%*                                                                     *\r
-%************************************************************************\r
-\r
-The @WhereFrom@ type controls where the renamer looks for an interface file\r
-\r
-\begin{code}\r
-data WhereFrom = ImportByUser          -- Ordinary user import: look for M.hi\r
-              | ImportByUserSource     -- User {- SOURCE -}: look for M.hi-boot\r
-              | ImportBySystem         -- Non user import.  Look for M.hi if M is in\r
-                                       -- the module this module depends on, or is a system-ish module; \r
-                                       -- M.hi-boot otherwise\r
-\r
-instance Outputable WhereFrom where\r
-  ppr ImportByUser       = empty\r
-  ppr ImportByUserSource = ptext SLIT("{- SOURCE -}")\r
-  ppr ImportBySystem     = ptext SLIT("{- SYSTEM IMPORT -}")\r
-\end{code}\r
-\r
-\r
-%************************************************************************\r
-%*                                                                     *\r
-\subsection{The name of a module}\r
-%*                                                                     *\r
-%************************************************************************\r
-\r
-\begin{code}\r
-type ModuleName = EncodedFS\r
-       -- Haskell module names can include the quote character ',\r
-       -- so the module names have the z-encoding applied to them\r
-\r
-\r
-pprModuleName :: ModuleName -> SDoc\r
-pprModuleName nm = pprEncodedFS nm\r
-\r
-moduleNameString :: ModuleName -> EncodedString\r
-moduleNameString mod = _UNPK_ mod\r
-\r
-moduleNameUserString :: ModuleName -> UserString\r
-moduleNameUserString mod = decode (_UNPK_ mod)\r
-\r
-mkSrcModule :: UserString -> ModuleName\r
-mkSrcModule s = _PK_ (encode s)\r
-\r
-mkSrcModuleFS :: UserFS -> ModuleName\r
-mkSrcModuleFS s = encodeFS s\r
-\r
-mkSysModuleFS :: EncodedFS -> ModuleName\r
-mkSysModuleFS s = s \r
-\end{code}\r
-\r
-\begin{code}\r
-data Module = Module\r
-               ModuleName\r
-               ModFlavour\r
-               DllFlavour\r
-\end{code}\r
-\r
-\begin{code}\r
-instance Outputable Module where\r
-  ppr = pprModule\r
-\r
-instance Eq Module where\r
-  (Module m1 _  _) == (Module m2 _ _) = m1 == m2\r
-\r
-instance Ord Module where\r
-  (Module m1 _ _) `compare` (Module m2 _ _) = m1 `compare` m2\r
-\end{code}\r
-\r
-\r
-\begin{code}\r
-pprModule :: Module -> SDoc\r
-pprModule (Module mod _ _) = pprEncodedFS mod\r
-\end{code}\r
-\r
-\r
-\begin{code}\r
-mkModule = Module\r
-\r
-mkVanillaModule :: ModuleName -> Module\r
-mkVanillaModule name = Module name UserMod NotDll\r
-\r
-mkThisModule :: ModuleName -> Module   -- The module being comiled\r
-mkThisModule name = Module name UserMod NotDll -- ToDo: correct Dll flag?\r
-\r
-mkPrelModule :: ModuleName -> Module\r
-mkPrelModule name = Module name sys dll\r
- where \r
-  sys | opt_CompilingPrelude = UserMod\r
-      | otherwise           = LibMod\r
-\r
-  dll | opt_Static || opt_CompilingPrelude = NotDll\r
-      | otherwise                         = Dll\r
-\r
-moduleString :: Module -> EncodedString\r
-moduleString (Module mod _ _) = _UNPK_ mod\r
-\r
-moduleName :: Module -> ModuleName\r
-moduleName (Module mod _ _) = mod\r
-\r
-moduleUserString :: Module -> UserString\r
-moduleUserString (Module mod _ _) = moduleNameUserString mod\r
-\end{code}\r
-\r
-\begin{code}\r
-isDynamicModule :: Module -> Bool\r
-isDynamicModule (Module _ _ Dll)  = True\r
-isDynamicModule _                = False\r
-\r
-isLibModule :: Module -> Bool\r
-isLibModule (Module _ LibMod _) = True\r
-isLibModule _                  = False\r
-\end{code}\r
-\r
-\r
-%************************************************************************\r
-%*                                                                     *\r
-\subsection{Finding modules in the file system\r
-%*                                                                     *\r
-%************************************************************************\r
-\r
-\begin{code}\r
-type ModuleHiMap = FiniteMap ModuleName (String, Module)\r
-  -- Mapping from module name to \r
-  --   * the file path of its corresponding interface file, \r
-  --   * the Module, decorated with it's properties\r
-\end{code}\r
-\r
-(We allege that) it is quicker to build up a mapping from module names\r
-to the paths to their corresponding interface files once, than to search\r
-along the import part every time we slurp in a new module (which we \r
-do quite a lot of.)\r
-\r
-\begin{code}\r
-type SearchPath = [(String,String)]    -- List of (directory,suffix) pairs to search \r
-                                        -- for interface files.\r
-\r
-mkModuleHiMaps :: SearchPath -> IO (ModuleHiMap, ModuleHiMap)\r
-mkModuleHiMaps dirs = foldM (getAllFilesMatching dirs) (env,env) dirs\r
- where\r
-  env = emptyFM\r
-\r
-{- A pseudo file, currently "dLL_ifs.hi",\r
-   signals that the interface files\r
-   contained in a particular directory have got their\r
-   corresponding object codes stashed away in a DLL\r
-   \r
-   This stuff is only needed to deal with Win32 DLLs,\r
-   and conceivably we conditionally compile in support\r
-   for handling it. (ToDo?)\r
--}\r
-dir_contain_dll_his = "dLL_ifs.hi"\r
-\r
-getAllFilesMatching :: SearchPath\r
-                   -> (ModuleHiMap, ModuleHiMap)\r
-                   -> (FilePath, String) \r
-                   -> IO (ModuleHiMap, ModuleHiMap)\r
-getAllFilesMatching dirs hims (dir_path, suffix) = ( do\r
-    -- fpaths entries do not have dir_path prepended\r
-  fpaths  <- getDirectoryContents dir_path\r
-  is_dll <- catch\r
-               (if opt_Static || dir_path == "." then\r
-                    return NotDll\r
-                else\r
-                    do  exists <- doesFileExist (dir_path ++ '/': dir_contain_dll_his)\r
-                        return (if exists then Dll else NotDll)\r
-               )\r
-               (\ _ {-don't care-} -> return NotDll)\r
-  return (foldl (addModules is_dll) hims fpaths)\r
-  )  -- soft failure\r
-      `catch` \r
-        (\ err -> do\r
-             hPutStrLn stderr\r
-                    ("Import path element `" ++ dir_path ++ \r
-                     if (isDoesNotExistError err) then\r
-                        "' does not exist, ignoring."\r
-                     else\r
-                       "' couldn't read, ignoring.")\r
-              \r
-              return hims\r
-       )\r
- where\r
-  \r
-       -- Dreadfully crude.  We want a better way to distinguish\r
-       -- "library-ish" modules.\r
-   is_sys | head dir_path == '/' = LibMod\r
-         | otherwise            = UserMod\r
-\r
-   xiffus       = reverse dotted_suffix \r
-   dotted_suffix = case suffix of\r
-                     []       -> []\r
-                     ('.':xs) -> suffix\r
-                     ls       -> '.':ls\r
-\r
-   hi_boot_version_xiffus = \r
-      reverse (show interfaceFileFormatVersion) ++ '-':hi_boot_xiffus\r
-   hi_boot_xiffus = "toob-ih." -- .hi-boot reversed!\r
-\r
-   addModules is_dll his@(hi_env, hib_env) filename = fromMaybe his $ \r
-        FMAP add_hi   (go xiffus                rev_fname)     `seqMaybe`\r
-\r
-        FMAP add_vhib (go hi_boot_version_xiffus rev_fname)    `seqMaybe`\r
-               -- If there's a Foo.hi-boot-N file then override any Foo.hi-boot\r
-\r
-       FMAP add_hib  (go hi_boot_xiffus         rev_fname)\r
-     where\r
-       rev_fname = reverse filename\r
-       path      = dir_path ++ '/':filename\r
-\r
-         -- In these functions file_nm is the base of the filename,\r
-         -- with the path and suffix both stripped off.  The filename\r
-         -- is the *unencoded* module name (else 'make' gets confused).\r
-         -- But the domain of the HiMaps is ModuleName which is encoded.\r
-       add_hi    file_nm = (add_to_map addNewOne hi_env file_nm,   hib_env)\r
-       add_vhib  file_nm = (hi_env, add_to_map overrideNew hib_env file_nm)\r
-       add_hib   file_nm = (hi_env, add_to_map addNewOne   hib_env file_nm)\r
-\r
-       add_to_map combiner env file_nm \r
-         = addToFM_C combiner env mod_nm (path, mkModule mod_nm is_sys is_dll)\r
-         where\r
-           mod_nm = mkSrcModuleFS file_nm\r
-\r
-   -- go prefix (prefix ++ stuff) == Just (reverse stuff)\r
-   go [] xs                    = Just (_PK_ (reverse xs))\r
-   go _  []                    = Nothing\r
-   go (x:xs) (y:ys) | x == y    = go xs ys \r
-                   | otherwise = Nothing\r
-\r
-   addNewOne | opt_WarnHiShadows = conflict\r
-            | otherwise         = stickWithOld\r
-\r
-   stickWithOld old new = old\r
-   overrideNew  old new = new\r
-\r
-   conflict (old_path,mod) (new_path,_)\r
-    | old_path /= new_path = \r
-        pprTrace "Warning: " (text "Identically named interface files present on the import path, " $$\r
-                             text (show old_path) <+> text "shadows" $$\r
-                             text (show new_path) $$\r
-                             text "on the import path: " <+> \r
-                             text (concat (intersperse ":" (map fst dirs))))\r
-        (old_path,mod)\r
-    | otherwise = (old_path,mod)  -- don't warn about innocous shadowings.\r
-\end{code}\r
-\r
-\r
-%*********************************************************\r
-%*                                                      *\r
-\subsection{Making a search path}\r
-%*                                                      *\r
-%*********************************************************\r
-\r
-@mkSearchPath@ takes a string consisting of a colon-separated list\r
-of directories and corresponding suffixes, and turns it into a list\r
-of (directory, suffix) pairs.  For example:\r
-\r
-\begin{verbatim}\r
- mkSearchPath "foo%.hi:.%.p_hi:baz%.mc_hi"\r
-   = [("foo",".hi"),( ".", ".p_hi"), ("baz",".mc_hi")]\r
-\begin{verbatim}\r
-\r
-\begin{code}\r
-mkSearchPath :: Maybe String -> SearchPath\r
-mkSearchPath Nothing = [(".",".hi")]  -- ToDo: default should be to look in\r
-                                     -- the directory the module we're compiling\r
-                                     -- lives.\r
-mkSearchPath (Just s) = go s\r
-  where\r
-    go "" = []\r
-    go s  = \r
-      case span (/= '%') s of\r
-       (dir,'%':rs) ->\r
-         case span (/= ':') rs of\r
-          (hisuf,_:rest) -> (dir,hisuf):go rest\r
-          (hisuf,[])     -> [(dir,hisuf)]\r
-\end{code}\r
-\r
+%
+% (c) The GRASP/AQUA Project, Glasgow University, 1992-2002
+%
+
+ModuleName
+~~~~~~~~~~
+Simply the name of a module, represented as a Z-encoded FastString.
+These are Uniquable, hence we can build FiniteMaps with ModuleNames as
+the keys.
+
+Module
+~~~~~~
+
+A ModuleName with some additional information, namely whether the
+module resides in the Home package or in a different package.  We need
+to know this for two reasons: 
+  
+  * generating cross-DLL calls is different from intra-DLL calls 
+    (see below).
+  * we don't record version information in interface files for entities
+    in a different package.
+
+The unique of a Module is identical to the unique of a ModuleName, so
+it is safe to look up in a Module map using a ModuleName and vice
+versa.
+
+Notes on DLLs
+~~~~~~~~~~~~~
+When compiling module A, which imports module B, we need to 
+know whether B will be in the same DLL as A.  
+       If it's in the same DLL, we refer to B_f_closure
+       If it isn't, we refer to _imp__B_f_closure
+When compiling A, we record in B's Module value whether it's
+in a different DLL, by setting the DLL flag.
+
+
+
+
+\begin{code}
+module Module 
+    (
+      Module,                  -- Abstract, instance of Eq, Ord, Outputable
+
+    , ModLocation(..),
+    , showModMsg
+
+    , ModuleName
+    , pprModuleName            -- :: ModuleName -> SDoc
+    , printModulePrefix
+
+    , moduleName               -- :: Module -> ModuleName 
+    , moduleNameString         -- :: ModuleName -> EncodedString
+    , moduleNameUserString     -- :: ModuleName -> UserString
+    , moduleNameFS             -- :: ModuleName -> EncodedFS
+
+    , moduleString             -- :: Module -> EncodedString
+    , moduleUserString         -- :: Module -> UserString
+
+    , mkBasePkgModule          -- :: UserString -> Module
+    , mkThPkgModule            -- :: UserString -> Module
+    , mkHomeModule             -- :: ModuleName -> Module
+    , isHomeModule             -- :: Module -> Bool
+    , mkPackageModule          -- :: ModuleName -> Module
+
+    , mkModuleName             -- :: UserString -> ModuleName
+    , mkModuleNameFS           -- :: UserFS    -> ModuleName
+    , mkSysModuleNameFS                -- :: EncodedFS -> ModuleName
+
+    , pprModule,
+    , ModuleEnv,
+    , elemModuleEnv, extendModuleEnv, extendModuleEnvList, plusModuleEnv_C
+    , delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv
+    , lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv
+    , moduleEnvElts, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv
+    , extendModuleEnv_C
+    , lookupModuleEnvByName, extendModuleEnvByName, unitModuleEnvByName
+
+    , ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
+
+    ) where
+
+#include "HsVersions.h"
+import OccName
+import Outputable
+import Packages                ( PackageName, basePackage, thPackage )
+import CmdLineOpts     ( opt_InPackage )
+import FastString      ( FastString )
+import Unique          ( Uniquable(..) )
+import Maybes          ( expectJust )
+import UniqFM
+import UniqSet
+import Binary
+import FastString
+\end{code}
+
+
+%************************************************************************
+%*                                                                     *
+\subsection{Interface file flavour}
+%*                                                                     *
+%************************************************************************
+
+A further twist to the tale is the support for dynamically linked
+libraries under Win32. Here, dealing with the use of global variables
+that's residing in a DLL requires special handling at the point of use
+(there's an extra level of indirection, i.e., (**v) to get at v's
+value, rather than just (*v) .) When slurping in an interface file we
+then record whether it's coming from a .hi corresponding to a module
+that's packaged up in a DLL or not, so that we later can emit the
+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.)
+
+\begin{code}
+data Module = Module ModuleName !PackageInfo
+
+data PackageInfo
+  = ThisPackage                                -- A module from the same package 
+                                       -- as the one being compiled
+  | AnotherPackage                     -- A module from a different package
+
+packageInfoPackage :: PackageInfo -> PackageName
+packageInfoPackage ThisPackage        = opt_InPackage
+packageInfoPackage AnotherPackage     = FSLIT("<pkg>")
+
+instance Outputable PackageInfo where
+       -- Just used in debug prints of lex tokens and in debug modde
+   ppr pkg_info = ppr (packageInfoPackage pkg_info)
+\end{code}
+
+
+%************************************************************************
+%*                                                                     *
+\subsection{Module locations}
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+data ModLocation
+   = ModLocation {
+        ml_hs_file   :: Maybe FilePath,
+
+        ml_hspp_file :: Maybe FilePath, -- Path of preprocessed source
+
+        ml_hi_file   :: FilePath,      -- Where the .hi file is, whether or not it exists
+                                       -- Always of form foo.hi, even if there is an hi-boot
+                                       -- file (we add the -boot suffix later)
+
+        ml_obj_file  :: FilePath       -- Where the .o file is, whether or not it exists
+                                       -- (might not exist either because the module
+                                       --  hasn't been compiled yet, or because
+                                       --  it is part of a package with a .a file)
+     }
+     deriving Show
+
+instance Outputable ModLocation where
+   ppr = text . show
+
+-- Rather a gruesome function to have in Module
+
+showModMsg :: Bool -> Module -> ModLocation -> String
+showModMsg use_object mod location =
+    mod_str ++ replicate (max 0 (16 - length mod_str)) ' '
+    ++" ( " ++ expectJust "showModMsg" (ml_hs_file location) ++ ", "
+    ++ (if use_object
+         then ml_obj_file location
+         else "interpreted")
+    ++ " )"
+ where mod_str = moduleUserString mod
+\end{code}
+
+For a module in another package, the hs_file and obj_file
+components of ModLocation are undefined.  
+
+The locations specified by a ModLocation may or may not
+correspond to actual files yet: for example, even if the object
+file doesn't exist, the ModLocation still contains the path to
+where the object file will reside if/when it is created.
+
+
+%************************************************************************
+%*                                                                     *
+\subsection{The name of a module}
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+newtype ModuleName = ModuleName EncodedFS
+       -- Haskell module names can include the quote character ',
+       -- so the module names have the z-encoding applied to them
+
+instance Binary ModuleName where
+   put_ bh (ModuleName m) = put_ bh m
+   get bh = do m <- get bh; return (ModuleName m)
+
+instance Uniquable ModuleName where
+  getUnique (ModuleName nm) = getUnique 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 (ModuleName nm) = pprEncodedFS nm
+
+moduleNameFS :: ModuleName -> EncodedFS
+moduleNameFS (ModuleName mod) = mod
+
+moduleNameString :: ModuleName -> EncodedString
+moduleNameString (ModuleName mod) = unpackFS mod
+
+moduleNameUserString :: ModuleName -> UserString
+moduleNameUserString (ModuleName mod) = decode (unpackFS mod)
+
+-- used to be called mkSrcModule
+mkModuleName :: UserString -> ModuleName
+mkModuleName s = ModuleName (mkFastString (encode s))
+
+-- used to be called mkSrcModuleFS
+mkModuleNameFS :: UserFS -> ModuleName
+mkModuleNameFS s = ModuleName (encodeFS s)
+
+-- 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
+  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
+  m1 `compare` m2 = getUnique m1 `compare` getUnique m2
+\end{code}
+
+
+\begin{code}
+pprModule :: Module -> SDoc
+pprModule (Module mod p) = getPprStyle $ \ sty ->
+                          if debugStyle sty then
+                               -- Print the package too
+                               -- Don't use '.' because it gets confused
+                               --      with module names
+                               brackets (ppr p) <> pprModuleName mod
+                          else
+                               pprModuleName mod
+\end{code}
+
+
+\begin{code}
+mkBasePkgModule :: ModuleName -> Module
+mkBasePkgModule mod_nm
+  = Module mod_nm pack_info
+  where
+    pack_info
+      | opt_InPackage == basePackage = ThisPackage
+      | otherwise                   = AnotherPackage
+
+mkThPkgModule :: ModuleName -> Module
+mkThPkgModule mod_nm
+  = Module mod_nm pack_info
+  where
+    pack_info
+      | opt_InPackage == thPackage = ThisPackage
+      | otherwise                 = AnotherPackage
+
+mkHomeModule :: ModuleName -> Module
+mkHomeModule mod_nm = Module mod_nm ThisPackage
+
+isHomeModule :: Module -> Bool
+isHomeModule (Module nm ThisPackage) = True
+isHomeModule _                       = False
+
+mkPackageModule :: ModuleName -> Module
+mkPackageModule mod_nm = Module mod_nm AnotherPackage
+
+moduleString :: Module -> EncodedString
+moduleString (Module (ModuleName fs) _) = unpackFS fs
+
+moduleName :: Module -> ModuleName
+moduleName (Module mod pkg_info) = mod
+
+moduleUserString :: Module -> UserString
+moduleUserString (Module mod _) = moduleNameUserString mod
+
+printModulePrefix :: Module -> Bool
+  -- When printing, say M.x
+printModulePrefix (Module nm ThisPackage) = False
+printModulePrefix _                       = True
+\end{code}
+
+
+%************************************************************************
+%*                                                                      *
+\subsection{@ModuleEnv@s}
+%*                                                                      *
+%************************************************************************
+
+\begin{code}
+type ModuleEnv elt = UniqFM elt
+-- A ModuleName and Module have the same Unique,
+-- so both will work as keys.  
+-- The 'ByName' variants work on ModuleNames
+
+emptyModuleEnv       :: ModuleEnv a
+mkModuleEnv          :: [(Module, a)] -> ModuleEnv a
+unitModuleEnv        :: Module -> a -> ModuleEnv a
+extendModuleEnv      :: ModuleEnv a -> Module -> a -> ModuleEnv a
+extendModuleEnv_C    :: (a->a->a) -> 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
+moduleEnvElts        :: 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
+
+-- The ByName variants
+lookupModuleEnvByName :: ModuleEnv a -> ModuleName -> Maybe a
+unitModuleEnvByName   :: ModuleName -> a -> ModuleEnv a
+extendModuleEnvByName :: ModuleEnv a -> ModuleName -> a -> ModuleEnv a
+
+elemModuleEnv       = elemUFM
+extendModuleEnv     = addToUFM
+extendModuleEnvByName = addToUFM
+extendModuleEnv_C   = addToUFM_C
+extendModuleEnvList = addListToUFM
+plusModuleEnv_C     = plusUFM_C
+delModuleEnvList    = delListFromUFM
+delModuleEnv        = delFromUFM
+plusModuleEnv       = plusUFM
+lookupModuleEnv     = lookupUFM
+lookupModuleEnvByName = lookupUFM
+lookupWithDefaultModuleEnv = lookupWithDefaultUFM
+mapModuleEnv        = mapUFM
+mkModuleEnv         = listToUFM
+emptyModuleEnv      = emptyUFM
+moduleEnvElts       = eltsUFM
+unitModuleEnv       = unitUFM
+unitModuleEnvByName = unitUFM
+isEmptyModuleEnv    = isNullUFM
+foldModuleEnv       = foldUFM
+\end{code}
+
+\begin{code}
+
+type ModuleSet = UniqSet Module
+mkModuleSet    :: [Module] -> ModuleSet
+extendModuleSet :: ModuleSet -> Module -> ModuleSet
+emptyModuleSet  :: ModuleSet
+moduleSetElts   :: ModuleSet -> [Module]
+elemModuleSet   :: Module -> ModuleSet -> Bool
+
+emptyModuleSet  = emptyUniqSet
+mkModuleSet     = mkUniqSet
+extendModuleSet = addOneToUniqSet
+moduleSetElts   = uniqSetToList
+elemModuleSet   = elementOfUniqSet
+\end{code}