X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FbasicTypes%2FModule.lhs;h=d67b8a5c951d07ac85290674e8d398987fdca98c;hb=8e67f5502e2e316245806ee3735a2f41a844b611;hp=4320bc3982bf5cd42ff847293199cf4bd3bf40fd;hpb=506fa77d392191e46c12b2c19387ff5b0888f6a2;p=ghc-hetmet.git diff --git a/ghc/compiler/basicTypes/Module.lhs b/ghc/compiler/basicTypes/Module.lhs index 4320bc3..d67b8a5 100644 --- a/ghc/compiler/basicTypes/Module.lhs +++ b/ghc/compiler/basicTypes/Module.lhs @@ -1,134 +1,103 @@ % -% (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. +Module +~~~~~~~~~~ +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. \begin{code} module Module ( - Module -- abstract, instance of Eq, Ord, Outputable - , ModuleName + Module, -- Abstract, instance of Eq, Ord, Outputable + , pprModule -- :: ModuleName -> SDoc - , moduleNameString -- :: ModuleName -> EncodedString - , moduleNameUserString -- :: ModuleName -> UserString + , ModLocation(..), + , addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn, - , moduleString -- :: Module -> EncodedString - , moduleUserString -- :: Module -> UserString - , moduleName -- :: Module -> ModuleName + , moduleString -- :: ModuleName -> EncodedString + , moduleUserString -- :: ModuleName -> UserString + , moduleFS -- :: ModuleName -> EncodedFS - , mkVanillaModule -- :: ModuleName -> Module - , mkThisModule -- :: ModuleName -> Module - , mkPrelModule -- :: UserString -> Module - - , isDynamicModule -- :: Module -> Bool - , isLibModule - - , mkSrcModule - - , mkSrcModuleFS -- :: UserFS -> ModuleName - , mkSysModuleFS -- :: EncodedFS -> ModuleName - - , pprModule, pprModuleName + , mkModule -- :: UserString -> ModuleName + , mkModuleFS -- :: UserFS -> ModuleName + , mkSysModuleFS -- :: EncodedFS -> ModuleName - -- DllFlavour - , DllFlavour, dll, notDll - - -- ModFlavour - , ModFlavour, libMod, userMod + , 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(..), SearchPath, mkSearchPath - , ModuleHiMap, mkModuleHiMaps + , ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet ) where #include "HsVersions.h" import OccName import Outputable -import FiniteMap -import CmdLineOpts ( opt_Static, opt_CompilingPrelude, opt_WarnHiShadows ) -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 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 DllFlavour = NotDll -- Ordinary module - | Dll -- The module's object code lives in a DLL. - deriving( Eq ) - -dll = Dll -notDll = NotDll - -instance Text DllFlavour where -- Just used in debug prints of lex tokens - showsPrec n NotDll s = s - showsPrec n Dll s = "dll " ++ 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{System/user module} -%* * -%************************************************************************ - -We also track whether an imported module is from a 'system-ish' place. In this case -we don't record the fact that this module depends on it, nor usages of things -inside it. +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 ModFlavour = LibMod -- A library-ish module - | UserMod -- Not library-ish - -libMod = LibMod -userMod = UserMod -\end{code} - - -%************************************************************************ -%* * -\subsection{Where from} -%* * -%************************************************************************ - -The @WhereFrom@ type controls where the renamer looks for an interface file - -\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} @@ -139,247 +108,116 @@ instance Outputable WhereFrom where %************************************************************************ \begin{code} -type ModuleName = EncodedFS +newtype Module = Module EncodedFS -- Haskell module names can include the quote character ', -- so the module names have the z-encoding applied to them -type ModuleNameSet = FiniteMap ModuleName -elemModuleNameSet s x = elemFM s x -moduleNameSetElems s = eltsFM s - - -pprModuleName :: ModuleName -> SDoc -pprModuleName nm = pprEncodedFS nm - -moduleNameString :: ModuleName -> EncodedString -moduleNameString mod = _UNPK_ mod +instance Binary Module where + put_ bh (Module m) = put_ bh m + get bh = do m <- get bh; return (Module m) -moduleNameUserString :: ModuleName -> UserString -moduleNameUserString mod = decode (_UNPK_ mod) +instance Uniquable Module where + getUnique (Module nm) = getUnique nm -mkSrcModule :: UserString -> ModuleName -mkSrcModule s = _PK_ (encode s) - -mkSrcModuleFS :: UserFS -> ModuleName -mkSrcModuleFS s = encodeFS s - -mkSysModuleFS :: EncodedFS -> ModuleName -mkSysModuleFS s = s -\end{code} +instance Eq Module where + nm1 == nm2 = getUnique nm1 == getUnique nm2 -\begin{code} -data Module = Module - ModuleName - ModFlavour - DllFlavour -\end{code} +-- 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 + nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2 -\begin{code} instance Outputable Module where ppr = pprModule -instance Eq Module where - (Module m1 _ _) == (Module m2 _ _) = m1 == m2 - -instance Ord Module where - (Module m1 _ _) `compare` (Module m2 _ _) = m1 `compare` m2 -\end{code} - -\begin{code} pprModule :: Module -> SDoc -pprModule (Module mod _ _) = pprEncodedFS mod -\end{code} - - -\begin{code} -mkModule = Module - -mkVanillaModule :: ModuleName -> Module -mkVanillaModule name = Module name UserMod NotDll - -mkThisModule :: ModuleName -> Module -- The module being comiled -mkThisModule name = Module name UserMod NotDll -- ToDo: correct Dll flag? - -mkPrelModule :: ModuleName -> Module -mkPrelModule name = Module name sys dll - where - sys | opt_CompilingPrelude = UserMod - | otherwise = LibMod +pprModule (Module nm) = pprEncodedFS nm - dll | opt_Static || opt_CompilingPrelude = NotDll - | otherwise = Dll +moduleFS :: Module -> EncodedFS +moduleFS (Module mod) = mod moduleString :: Module -> EncodedString -moduleString (Module mod _ _) = _UNPK_ mod - -moduleName :: Module -> ModuleName -moduleName (Module mod _ _) = mod +moduleString (Module mod) = unpackFS mod moduleUserString :: Module -> UserString -moduleUserString (Module mod _ _) = moduleNameUserString mod -\end{code} +moduleUserString (Module mod) = decode (unpackFS mod) -\begin{code} -isDynamicModule :: Module -> Bool -isDynamicModule (Module _ _ Dll) = True -isDynamicModule _ = False +-- used to be called mkSrcModule +mkModule :: UserString -> Module +mkModule s = Module (mkFastString (encode s)) -isLibModule :: Module -> Bool -isLibModule (Module _ LibMod _) = True -isLibModule _ = False -\end{code} +-- used to be called mkSrcModuleFS +mkModuleFS :: UserFS -> Module +mkModuleFS s = Module (encodeFS s) +-- used to be called mkSysModuleFS +mkSysModuleFS :: EncodedFS -> Module +mkSysModuleFS s = Module s +\end{code} %************************************************************************ -%* * -\subsection{Finding modules in the file system -%* * +%* * +\subsection{@ModuleEnv@s} +%* * %************************************************************************ \begin{code} -type ModuleHiMap = FiniteMap ModuleName (String, Module) - -- Mapping from module name to - -- * the file path of its corresponding interface file, - -- * the Module, decorated with it's properties -\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 (ModuleHiMap, ModuleHiMap) -mkModuleHiMaps dirs = foldM (getAllFilesMatching dirs) (env,env) dirs - where - env = emptyFM - -{- A pseudo file, currently "dLL_ifs.hi", - signals that the interface files - contained in a particular directory have got their - corresponding object codes stashed away in a DLL - - This stuff is only needed to deal with Win32 DLLs, - and conceivably we conditionally compile in support - for handling it. (ToDo?) --} -dir_contain_dll_his = "dLL_ifs.hi" - -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 - is_dll <- catch - (if opt_Static || dir_path == "." then - return NotDll - else - do exists <- doesFileExist (dir_path ++ '/': dir_contain_dll_his) - return (if exists then Dll else NotDll) - ) - (\ _ {-don't care-} -> return NotDll) - return (foldl (addModules is_dll) 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 - - -- Dreadfully crude. We want a better way to distinguish - -- "library-ish" modules. - is_sys | head dir_path == '/' = LibMod - | otherwise = UserMod - - 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 is_dll 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` - FMAP add_hib (go hi_boot_xiffus rev_fname) - where - rev_fname = reverse filename - path = dir_path ++ '/':filename - - mk_module mod_nm = Module mod_nm is_sys is_dll - add_hi mod_nm = (addToFM_C addNewOne hi_env mod_nm (path, mk_module mod_nm), hib_env) - add_vhib mod_nm = (hi_env, addToFM_C overrideNew hib_env mod_nm (path, mk_module mod_nm)) - add_hib mod_nm = (hi_env, addToFM_C addNewOne hib_env mod_nm (path, mk_module mod_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,mod) (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,mod) - | otherwise = (old_path,mod) -- 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 +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} - -%********************************************************* -%* * -\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 (/= ':') rs of - (hisuf,_:rest) -> (dir,hisuf):go rest - (hisuf,[]) -> [(dir,hisuf)] +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} -