import ErrUtils ( addShortErrLocLine, addShortWarnLocLine,
pprBagOfErrors, ErrMsg, WarnMsg, Message
)
-import Name ( Module, Name, OccName, NamedThing(..), IfaceFlavour,
- isLocallyDefinedName, nameModule, nameOccName
+import Name ( Name, OccName, NamedThing(..),
+ isLocallyDefinedName, nameModule, nameOccName,
+ decode
+ )
+import Module ( Module, IfaceFlavour, setModuleFlavour, mkSysModuleFS,
+ bootFlavour, moduleString, moduleIfaceFlavour, mkDynFlavour
)
import NameSet
import RdrName ( RdrName )
-import CmdLineOpts ( opt_D_show_rn_trace, opt_IgnoreIfacePragmas, opt_WarnHiShadows )
+import CmdLineOpts ( opt_D_show_rn_trace, opt_IgnoreIfacePragmas,
+ opt_WarnHiShadows, opt_Static
+ )
import PrelInfo ( builtinNames )
import TysWiredIn ( boolTyCon )
import SrcLoc ( SrcLoc, mkGeneratedSrcLoc )
import Util
import Outputable
import DirUtils ( getDirectoryContents )
+import Directory ( doesFileExist )
import IO ( hPutStrLn, stderr, isDoesNotExistError )
import Monad ( foldM )
import Maybe ( fromMaybe )
rn_ns :: SSTRef s RnNameSupply,
rn_errs :: SSTRef s (Bag WarnMsg, Bag ErrMsg),
rn_occs :: SSTRef s ([Occurrence],[Occurrence]), -- Occurrences: compulsory and optional resp
- rn_mod :: Module
+ rn_hi_map :: ModuleHiMap, -- for .hi files
+ rn_hiboot_map :: ModuleHiMap, -- for .hi-boot files
+ rn_mod :: Module
}
type Occurrence = (Name, SrcLoc) -- The srcloc is the occurrence site
-- For getting global names
data GDown = GDown {
- rn_hi_map :: ModuleHiMap, -- for .hi files
- rn_hiboot_map :: ModuleHiMap, -- for .hi-boot files
rn_ifaces :: SSTRWRef Ifaces
}
type SearchPath = [(String,String)] -- List of (directory,suffix) pairs to search
-- for interface files.
-type ModuleHiMap = FiniteMap String String
+type ModuleHiMap = FiniteMap String (String, Bool)
-- mapping from module name to the file path of its corresponding
-- interface file.
\end{code}
data ParsedIface
= ParsedIface
- Module -- Module name
Version -- Module version number
[ImportVersion OccName] -- Usages
[ExportItem] -- Exports
ExportEnv, -- What this module exports
[Module]) -- Instance modules
+
+-- needed by Main to fish out the fixities assoc list.
+getIfaceFixities :: InterfaceDetails -> Fixities
+getIfaceFixities (_, ExportEnv _ fs, _) = fs
+
+
type RdrNamePragma = () -- Fudge for now
-------------------
-> IO (r, Bag ErrMsg, Bag WarnMsg)
initRn mod us dirs loc do_rn = do
+ (himap, hibmap) <- mkModuleHiMaps dirs
names_var <- sstToIO (newMutVarSST (us, emptyFM, builtins))
errs_var <- sstToIO (newMutVarSST (emptyBag,emptyBag))
iface_var <- sstToIO (newMutVarSST (emptyIfaces mod))
occs_var <- sstToIO (newMutVarSST initOccs)
- (himap, hibmap) <- mkModuleHiMaps dirs
let
rn_down = RnDown { rn_loc = loc, rn_omit = \n -> False, rn_ns = names_var,
rn_errs = errs_var, rn_occs = occs_var,
+ rn_hi_map = himap, rn_hiboot_map = hibmap,
rn_mod = mod }
- g_down = GDown { rn_hi_map = himap, rn_hiboot_map = hibmap, rn_ifaces = iface_var }
+ g_down = GDown {rn_ifaces = iface_var }
-- do the business
res <- sstToIO (do_rn rn_down g_down)
iDefData = emptyNameEnv,
iInstMods = []
}
-
builtins :: FiniteMap (Module,OccName) Name
-builtins = bagToFM $
- mapBag (\ name -> ((nameModule name, nameOccName name), name))
- builtinNames
+builtins =
+ bagToFM (
+ mapBag (\ name -> ((nameModule name, nameOccName name), name))
+ builtinNames)
-- Initial value for the occurrence pool.
initOccs :: ([Occurrence],[Occurrence]) -- Compulsory and optional respectively
-- to do as much as possible explicitly.
\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}
mkModuleHiMaps :: SearchPath -> IO (ModuleHiMap, ModuleHiMap)
mkModuleHiMaps dirs = foldM (getAllFilesMatching dirs) (env,env) dirs
where
env = emptyFM
+{- a pseudo file which 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
- return (foldl addModules hims fpaths)
+ fpaths <- getDirectoryContents dir_path
+ is_dyns <- catch
+ (if opt_Static || dir_path == "." then
+ return False
+ else
+ doesFileExist (dir_path ++ '/': dir_contain_dll_his))
+ (\ _ {-don't care-} -> return False)
+ return (foldl (addModules is_dyns) hims fpaths)
) -- soft failure
`catch`
(\ err -> do
- hPutStrLn stderr
+ hPutStrLn stderr
("Import path element `" ++ dir_path ++
if (isDoesNotExistError err) then
"' does not exist, ignoring."
reverse (show interfaceFileFormatVersion) ++ '-':hi_boot_xiffus
hi_boot_xiffus = "toob-ih." -- .hi-boot reversed.
- addModules his@(hi_env, hib_env) nm = fromMaybe his $
- FMAP (\ (mod_nm,v) -> (addToFM_C addNewOne hi_env mod_nm v, hib_env))
+ addModules is_dll his@(hi_env, hib_env) nm = fromMaybe his $
+ FMAP (\ (mod_nm,v) -> (addToFM_C addNewOne hi_env mod_nm (v, is_dll), hib_env))
(go xiffus rev_nm) `seqMaybe`
- FMAP (\ (mod_nm,v) -> (hi_env, addToFM_C overrideNew hib_env mod_nm v))
+ FMAP (\ (mod_nm,v) -> (hi_env, addToFM_C overrideNew hib_env mod_nm (v,is_dll)))
(go hi_boot_version_xiffus rev_nm) `seqMaybe`
- FMAP (\ (mod_nm,v) -> (hi_env, addToFM_C addNewOne hib_env mod_nm v))
+ FMAP (\ (mod_nm,v) -> (hi_env, addToFM_C addNewOne hib_env mod_nm (v,is_dll)))
(go hi_boot_xiffus rev_nm)
where
rev_nm = reverse nm
conflict old_path new_path
| old_path /= new_path =
- pprTrace "Warning: " (text "Identically named interface files present on import 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: " <+>
thenRn_ :: RnM s d a -> RnM s d b -> RnM s d b
andRn :: (a -> a -> a) -> RnM s d a -> RnM s d a -> RnM s d a
mapRn :: (a -> RnM s d b) -> [a] -> RnM s d [b]
+mapRn_ :: (a -> RnM s d b) -> [a] -> RnM s d ()
mapMaybeRn :: (a -> RnM s d (Maybe b)) -> [a] -> RnM s d [b]
sequenceRn :: [RnM s d a] -> RnM s d [a]
foldlRn :: (b -> a -> RnM s d b) -> b -> [a] -> RnM s d b
mapRn f xs `thenRn` \ rs ->
returnRn (r:rs)
+mapRn_ f [] = returnRn ()
+mapRn_ f (x:xs) =
+ f x `thenRn_`
+ mapRn_ f xs
+
foldlRn k z [] = returnRn z
foldlRn k z (x:xs) = k z x `thenRn` \ z' ->
foldlRn k z' xs
in
writeMutVarSST names_var (us, mapInst', cache) `thenSST_`
returnSST uniq
+
+getUniqRn :: RnM s d Unique
+getUniqRn (RnDown {rn_ns = names_var}) l_down
+ = readMutVarSST names_var `thenSST` \ (us, mapInst, cache) ->
+ let
+ (us1,us') = splitUniqSupply us
+ in
+ writeMutVarSST names_var (us', mapInst, cache) `thenSST_`
+ returnSST (uniqFromSupply us1)
\end{code}
================ Occurrences =====================
setIfacesRn ifaces rn_down (GDown {rn_ifaces = iface_var})
= writeMutVarSST iface_var ifaces
-getModuleHiMap :: Bool -> RnMG ModuleHiMap
-getModuleHiMap want_hi_boot rn_down (GDown {rn_hi_map = himap, rn_hiboot_map = hibmap})
+getModuleHiMap :: Bool -> RnM s d ModuleHiMap
+getModuleHiMap want_hi_boot (RnDown {rn_hi_map = himap, rn_hiboot_map = hibmap}) _
| want_hi_boot = returnSST hibmap
| otherwise = returnSST himap
+\end{code}
+
+The interface file format is capable of distinguishing
+between normal imports/exports of names from other modules
+and 'hi-boot' mentions of names, with the flavour in the
+being encoded inside a @Module@.
+
+@setModuleFlavourRn@ fixes up @Module@ values containing
+normal flavours, returning a @Module@ value containing
+the attributes of the module that's in scope. The only
+attribute at the moment is the DLLness of a module, i.e.,
+whether the object code for that module resides in a
+Win32 DLL or not.
+
+\begin{code}
+setModuleFlavourRn :: Module -> RnM s d Module
+setModuleFlavourRn mod
+ | bootFlavour hif = returnRn mod
+ | otherwise =
+ getModuleHiMap (bootFlavour hif) `thenRn` \ himap ->
+ case (lookupFM himap mod_pstr) of
+ Nothing -> returnRn mod
+ Just (_, is_in_a_dll) ->
+ returnRn (setModuleFlavour (mkDynFlavour is_in_a_dll hif) mod)
+ where
+ mod_pstr = moduleString mod
+ hif = moduleIfaceFlavour mod
\end{code}