[project @ 2006-01-06 16:30:17 by simonmar]
[ghc-hetmet.git] / ghc / compiler / basicTypes / Module.lhs
index 0cc8d16..f4e413d 100644 (file)
 %
-% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
+% (c) The University of Glasgow, 2004
 %
-\section[Module]{The @Module@ module.}
-
-Representing modules and their flavours.
-
-
-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.
-
-
 
+Module
+~~~~~~~~~~
+Simply the name of a module, represented as a FastString.
+These are Uniquable, hence we can build FiniteMaps with ModuleNames as
+the keys.
 
 \begin{code}
 module Module 
     (
-      Module               -- abstract, instance of Eq, Ord, Outputable
-    , ModuleName
-
-    , moduleNameString         -- :: ModuleName -> EncodedString
-    , moduleNameUserString     -- :: ModuleName -> UserString
+      Module                   -- Abstract, instance of Eq, Ord, Outputable
+    , pprModule                        -- :: ModuleName -> SDoc
 
-    , moduleString          -- :: Module -> EncodedString
-    , moduleUserString      -- :: Module -> UserString
-    , moduleName           -- :: Module -> ModuleName
+    , ModLocation(..)
+    , addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn
 
-    , mkVanillaModule      -- :: ModuleName -> Module
-    , mkThisModule         -- :: ModuleName -> Module
-    , mkPrelModule          -- :: UserString -> Module
-    , mkModule             -- :: ModuleName -> PackageName -> Module
-    
-    , isLocalModule       -- :: Module -> Bool
+    , moduleString             -- :: ModuleName -> String
+    , moduleFS                 -- :: ModuleName -> FastString
 
-    , mkSrcModule
-
-    , mkSrcModuleFS         -- :: UserFS    -> ModuleName
-    , mkSysModuleFS         -- :: EncodedFS -> ModuleName
-
-    , pprModule, pprModuleName
+    , mkModule                 -- :: String -> ModuleName
+    , mkModuleFS               -- :: FastString -> ModuleName
  
-    , PackageName
+    , ModuleEnv
+    , elemModuleEnv, extendModuleEnv, extendModuleEnvList, plusModuleEnv_C
+    , delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv
+    , lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv
+    , moduleEnvElts, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv
+    , extendModuleEnv_C, filterModuleEnv
 
-       -- Where to find a .hi file
-    , WhereFrom(..)
+    , ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
 
     ) where
 
 #include "HsVersions.h"
 import OccName
 import Outputable
-import CmdLineOpts     ( opt_InPackage )
-import FastString      ( FastString )
+import Unique          ( Uniquable(..) )
+import UniqFM
+import UniqSet
+import Binary
+import FastString
 \end{code}
 
-
 %************************************************************************
 %*                                                                     *
-\subsection{Interface file flavour}
+\subsection{Module locations}
 %*                                                                     *
 %************************************************************************
 
-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 PackageInfo = ThisPackage                         -- A module from the same package 
-                                               -- as the one being compiled
-                | AnotherPackage PackageName   -- A module from a different package
-
-type PackageName = FastString          -- No encoding at all
-
-preludePackage :: PackageName
-preludePackage = 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
+data ModLocation
+   = ModLocation {
+        ml_hs_file   :: Maybe FilePath,
+               -- The source file, if we have one.  Package modules
+               -- probably don't have source files.
+
+        ml_hi_file   :: FilePath,
+               -- Where the .hi file is, whether or not it exists
+               -- yet.  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 yet.
+               -- (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
 \end{code}
 
+For a module in another package, the hs_file and obj_file
+components of ModLocation are undefined.  
 
-%************************************************************************
-%*                                                                     *
-\subsection{Where from}
-%*                                                                     *
-%************************************************************************
-
-The @WhereFrom@ type controls where the renamer looks for an interface file
+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.
 
 \begin{code}
-data WhereFrom = ImportByUser          -- Ordinary user import: look for M.hi
-              | ImportByUserSource     -- User {- SOURCE -}: look for M.hi-boot
-              | ImportBySystem         -- Non user import.  Look for M.hi if M is in
-                                       -- the module this module depends on, or is a system-ish module; 
-                                       -- M.hi-boot otherwise
-
-instance Outputable WhereFrom where
-  ppr ImportByUser       = empty
-  ppr ImportByUserSource = ptext SLIT("{- SOURCE -}")
-  ppr ImportBySystem     = ptext SLIT("{- SYSTEM IMPORT -}")
+addBootSuffix :: FilePath -> FilePath
+-- Add the "-boot" suffix to .hs, .hi and .o files
+addBootSuffix path = path ++ "-boot"
+
+addBootSuffix_maybe :: Bool -> FilePath -> FilePath
+addBootSuffix_maybe is_boot path
+ | is_boot   = addBootSuffix path
+ | otherwise = path
+
+addBootSuffixLocn :: ModLocation -> ModLocation
+addBootSuffixLocn locn
+  = locn { ml_hs_file  = fmap addBootSuffix (ml_hs_file locn)
+        , ml_hi_file  = addBootSuffix (ml_hi_file locn)
+        , ml_obj_file = addBootSuffix (ml_obj_file locn) }
 \end{code}
 
 
@@ -123,91 +106,112 @@ instance Outputable WhereFrom where
 %************************************************************************
 
 \begin{code}
-type ModuleName = EncodedFS
+newtype Module = Module FastString
        -- Haskell module names can include the quote character ',
        -- so the module names have the z-encoding applied to them
 
-pprModuleName :: ModuleName -> SDoc
-pprModuleName nm = pprEncodedFS nm
-
-moduleNameString :: ModuleName -> EncodedString
-moduleNameString mod = _UNPK_ mod
-
-moduleNameUserString :: ModuleName -> UserString
-moduleNameUserString mod = decode (_UNPK_ mod)
-
-mkSrcModule :: UserString -> ModuleName
-mkSrcModule s = _PK_ (encode s)
+instance Binary Module where
+   put_ bh (Module m) = put_ bh m
+   get bh = do m <- get bh; return (Module m)
 
-mkSrcModuleFS :: UserFS -> ModuleName
-mkSrcModuleFS s = encodeFS s
-
-mkSysModuleFS :: EncodedFS -> ModuleName
-mkSysModuleFS s = s 
-\end{code}
-
-\begin{code}
-data Module = Module ModuleName PackageInfo
-\end{code}
-
-\begin{code}
-instance Outputable Module where
-  ppr = pprModule
+instance Uniquable Module where
+  getUnique (Module nm) = getUnique nm
 
 instance Eq Module where
-  (Module m1 _) == (Module m2 _) = m1 == m2
+  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 Module where
-  (Module m1 _) `compare` (Module m2 _) = m1 `compare` m2
-\end{code}
+  nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
 
+instance Outputable Module where
+  ppr = pprModule
 
-\begin{code}
 pprModule :: Module -> SDoc
-pprModule (Module mod p) = getPprStyle $ \ sty ->
-                          if debugStyle sty then
-                               -- Print the package too
-                               text (show 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
+pprModule (Module nm) = 
+    getPprStyle $ \ sty ->
+    if codeStyle sty 
+       then ftext (zEncodeFS nm)
+       else ftext nm
 
+moduleFS :: Module -> FastString
+moduleFS (Module mod) = mod
 
-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.)
+moduleString :: Module -> String
+moduleString (Module mod) = unpackFS mod
 
-mkThisModule :: ModuleName -> Module   -- The module being compiled
-mkThisModule name = Module name ThisPackage
+-- used to be called mkSrcModule
+mkModule :: String -> Module
+mkModule s = Module (mkFastString s)
 
-mkPrelModule :: ModuleName -> Module
-mkPrelModule name = mkModule name preludePackage
-
-moduleString :: Module -> EncodedString
-moduleString (Module mod _) = _UNPK_ mod
+-- used to be called mkSrcModuleFS
+mkModuleFS :: FastString -> Module
+mkModuleFS s = Module s
+\end{code}
 
-moduleName :: Module -> ModuleName
-moduleName (Module mod _) = mod
+%************************************************************************
+%*                                                                      *
+\subsection{@ModuleEnv@s}
+%*                                                                      *
+%************************************************************************
 
-moduleUserString :: Module -> UserString
-moduleUserString (Module mod _) = moduleNameUserString mod
+\begin{code}
+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
+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
+filterModuleEnv      :: (a -> Bool) -> ModuleEnv a -> ModuleEnv a
+
+filterModuleEnv            = filterUFM
+elemModuleEnv       = elemUFM
+extendModuleEnv     = addToUFM
+extendModuleEnv_C   = addToUFM_C
+extendModuleEnvList = addListToUFM
+plusModuleEnv_C     = plusUFM_C
+delModuleEnvList    = delListFromUFM
+delModuleEnv        = delFromUFM
+plusModuleEnv       = plusUFM
+lookupModuleEnv     = lookupUFM
+lookupWithDefaultModuleEnv = lookupWithDefaultUFM
+mapModuleEnv        = mapUFM
+mkModuleEnv         = listToUFM
+emptyModuleEnv      = emptyUFM
+moduleEnvElts       = eltsUFM
+unitModuleEnv       = unitUFM
+isEmptyModuleEnv    = isNullUFM
+foldModuleEnv       = foldUFM
 \end{code}
 
 \begin{code}
-isLocalModule :: Module -> Bool
-isLocalModule (Module _ ThisPackage) = True
-isLocalModule _                             = False
+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}