2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-2002
7 Simply the name of a module, represented as a Z-encoded FastString.
8 These are Uniquable, hence we can build FiniteMaps with ModuleNames as
14 A ModuleName with some additional information, namely whether the
15 module resides in the Home package or in a different package. We need
16 to know this for two reasons:
18 * generating cross-DLL calls is different from intra-DLL calls
20 * we don't record version information in interface files for entities
21 in a different package.
23 The unique of a Module is identical to the unique of a ModuleName, so
24 it is safe to look up in a Module map using a ModuleName and vice
29 When compiling module A, which imports module B, we need to
30 know whether B will be in the same DLL as A.
31 If it's in the same DLL, we refer to B_f_closure
32 If it isn't, we refer to _imp__B_f_closure
33 When compiling A, we record in B's Module value whether it's
34 in a different DLL, by setting the DLL flag.
42 Module, -- Abstract, instance of Eq, Ord, Outputable
48 , pprModuleName -- :: ModuleName -> SDoc
51 , moduleName -- :: Module -> ModuleName
52 , moduleNameString -- :: ModuleName -> EncodedString
53 , moduleNameUserString -- :: ModuleName -> UserString
54 , moduleNameFS -- :: ModuleName -> EncodedFS
56 , moduleString -- :: Module -> EncodedString
57 , moduleUserString -- :: Module -> UserString
60 , mkBasePkgModule -- :: UserString -> Module
61 , mkHomeModule -- :: ModuleName -> Module
62 , isHomeModule -- :: Module -> Bool
63 , mkPackageModule -- :: ModuleName -> Module
65 , mkModuleName -- :: UserString -> ModuleName
66 , mkModuleNameFS -- :: UserFS -> ModuleName
67 , mkSysModuleNameFS -- :: EncodedFS -> ModuleName
72 , elemModuleEnv, extendModuleEnv, extendModuleEnvList, plusModuleEnv_C
73 , delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv
74 , lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv
75 , moduleEnvElts, unitModuleEnv, isEmptyModuleEnv, foldModuleEnv
77 , lookupModuleEnvByName, extendModuleEnvByName, unitModuleEnvByName
79 , ModuleSet, emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
83 #include "HsVersions.h"
86 import Packages ( PackageName, basePackage )
87 import CmdLineOpts ( opt_InPackage )
88 import Unique ( Uniquable(..) )
89 import Maybes ( expectJust )
97 %************************************************************************
99 \subsection{Interface file flavour}
101 %************************************************************************
103 A further twist to the tale is the support for dynamically linked
104 libraries under Win32. Here, dealing with the use of global variables
105 that's residing in a DLL requires special handling at the point of use
106 (there's an extra level of indirection, i.e., (**v) to get at v's
107 value, rather than just (*v) .) When slurping in an interface file we
108 then record whether it's coming from a .hi corresponding to a module
109 that's packaged up in a DLL or not, so that we later can emit the
112 The logic for how an interface file is marked as corresponding to a
113 module that's hiding in a DLL is explained elsewhere (ToDo: give
117 data Module = Module ModuleName !PackageInfo
120 = ThisPackage -- A module from the same package
121 -- as the one being compiled
122 | AnotherPackage -- A module from a different package
124 packageInfoPackage :: PackageInfo -> PackageName
125 packageInfoPackage ThisPackage = opt_InPackage
126 packageInfoPackage AnotherPackage = FSLIT("<pkg>")
128 instance Outputable PackageInfo where
129 -- Just used in debug prints of lex tokens and in debug modde
130 ppr pkg_info = ppr (packageInfoPackage pkg_info)
134 %************************************************************************
136 \subsection{Module locations}
138 %************************************************************************
143 ml_hs_file :: Maybe FilePath,
145 ml_hspp_file :: Maybe FilePath, -- Path of preprocessed source
147 ml_hi_file :: FilePath, -- Where the .hi file is, whether or not it exists
148 -- Always of form foo.hi, even if there is an hi-boot
149 -- file (we add the -boot suffix later)
151 ml_obj_file :: FilePath -- Where the .o file is, whether or not it exists
152 -- (might not exist either because the module
153 -- hasn't been compiled yet, or because
154 -- it is part of a package with a .a file)
158 instance Outputable ModLocation where
161 -- Rather a gruesome function to have in Module
163 showModMsg :: Bool -> Module -> ModLocation -> String
164 showModMsg use_object mod location =
165 mod_str ++ replicate (max 0 (16 - length mod_str)) ' '
166 ++" ( " ++ expectJust "showModMsg" (ml_hs_file location) ++ ", "
168 then ml_obj_file location
171 where mod_str = moduleUserString mod
174 For a module in another package, the hs_file and obj_file
175 components of ModLocation are undefined.
177 The locations specified by a ModLocation may or may not
178 correspond to actual files yet: for example, even if the object
179 file doesn't exist, the ModLocation still contains the path to
180 where the object file will reside if/when it is created.
183 %************************************************************************
185 \subsection{The name of a module}
187 %************************************************************************
190 newtype ModuleName = ModuleName EncodedFS
191 -- Haskell module names can include the quote character ',
192 -- so the module names have the z-encoding applied to them
194 instance Binary ModuleName where
195 put_ bh (ModuleName m) = put_ bh m
196 get bh = do m <- get bh; return (ModuleName m)
198 instance Uniquable ModuleName where
199 getUnique (ModuleName nm) = getUnique nm
201 instance Eq ModuleName where
202 nm1 == nm2 = getUnique nm1 == getUnique nm2
204 -- Warning: gives an ordering relation based on the uniques of the
205 -- FastStrings which are the (encoded) module names. This is _not_
206 -- a lexicographical ordering.
207 instance Ord ModuleName where
208 nm1 `compare` nm2 = getUnique nm1 `compare` getUnique nm2
210 instance Outputable ModuleName where
214 pprModuleName :: ModuleName -> SDoc
215 pprModuleName (ModuleName nm) = pprEncodedFS nm
217 moduleNameFS :: ModuleName -> EncodedFS
218 moduleNameFS (ModuleName mod) = mod
220 moduleNameString :: ModuleName -> EncodedString
221 moduleNameString (ModuleName mod) = unpackFS mod
223 moduleNameUserString :: ModuleName -> UserString
224 moduleNameUserString (ModuleName mod) = decode (unpackFS mod)
226 -- used to be called mkSrcModule
227 mkModuleName :: UserString -> ModuleName
228 mkModuleName s = ModuleName (mkFastString (encode s))
230 -- used to be called mkSrcModuleFS
231 mkModuleNameFS :: UserFS -> ModuleName
232 mkModuleNameFS s = ModuleName (encodeFS s)
234 -- used to be called mkSysModuleFS
235 mkSysModuleNameFS :: EncodedFS -> ModuleName
236 mkSysModuleNameFS s = ModuleName s
240 instance Outputable Module where
243 instance Uniquable Module where
244 getUnique (Module nm _) = getUnique nm
246 -- Same if they have the same name.
247 instance Eq Module where
248 m1 == m2 = getUnique m1 == getUnique m2
250 -- Warning: gives an ordering relation based on the uniques of the
251 -- FastStrings which are the (encoded) module names. This is _not_
252 -- a lexicographical ordering.
253 instance Ord Module where
254 m1 `compare` m2 = getUnique m1 `compare` getUnique m2
259 pprModule :: Module -> SDoc
260 pprModule (Module mod p) = getPprStyle $ \ sty ->
261 if debugStyle sty then
262 -- Print the package too
263 -- Don't use '.' because it gets confused
265 brackets (ppr p) <> pprModuleName mod
272 mkModule :: PackageName -> ModuleName -> Module
273 mkModule pkg_name mod_name
274 = Module mod_name pkg_info
277 | opt_InPackage == pkg_name = ThisPackage
278 | otherwise = AnotherPackage
280 mkBasePkgModule :: ModuleName -> Module
281 mkBasePkgModule mod_nm = mkModule basePackage mod_nm
283 mkHomeModule :: ModuleName -> Module
284 mkHomeModule mod_nm = Module mod_nm ThisPackage
286 isHomeModule :: Module -> Bool
287 isHomeModule (Module nm ThisPackage) = True
288 isHomeModule _ = False
290 mkPackageModule :: ModuleName -> Module
291 mkPackageModule mod_nm = Module mod_nm AnotherPackage
293 moduleString :: Module -> EncodedString
294 moduleString (Module (ModuleName fs) _) = unpackFS fs
296 moduleName :: Module -> ModuleName
297 moduleName (Module mod pkg_info) = mod
299 moduleUserString :: Module -> UserString
300 moduleUserString (Module mod _) = moduleNameUserString mod
302 printModulePrefix :: Module -> Bool
303 -- When printing, say M.x
304 printModulePrefix (Module nm ThisPackage) = False
305 printModulePrefix _ = True
309 %************************************************************************
311 \subsection{@ModuleEnv@s}
313 %************************************************************************
316 type ModuleEnv elt = UniqFM elt
317 -- A ModuleName and Module have the same Unique,
318 -- so both will work as keys.
319 -- The 'ByName' variants work on ModuleNames
321 emptyModuleEnv :: ModuleEnv a
322 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
323 unitModuleEnv :: Module -> a -> ModuleEnv a
324 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
325 extendModuleEnv_C :: (a->a->a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
326 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
327 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
329 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
330 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
331 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
332 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
333 moduleEnvElts :: ModuleEnv a -> [a]
335 isEmptyModuleEnv :: ModuleEnv a -> Bool
336 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
337 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
338 elemModuleEnv :: Module -> ModuleEnv a -> Bool
339 foldModuleEnv :: (a -> b -> b) -> b -> ModuleEnv a -> b
341 -- The ByName variants
342 lookupModuleEnvByName :: ModuleEnv a -> ModuleName -> Maybe a
343 unitModuleEnvByName :: ModuleName -> a -> ModuleEnv a
344 extendModuleEnvByName :: ModuleEnv a -> ModuleName -> a -> ModuleEnv a
346 elemModuleEnv = elemUFM
347 extendModuleEnv = addToUFM
348 extendModuleEnvByName = addToUFM
349 extendModuleEnv_C = addToUFM_C
350 extendModuleEnvList = addListToUFM
351 plusModuleEnv_C = plusUFM_C
352 delModuleEnvList = delListFromUFM
353 delModuleEnv = delFromUFM
354 plusModuleEnv = plusUFM
355 lookupModuleEnv = lookupUFM
356 lookupModuleEnvByName = lookupUFM
357 lookupWithDefaultModuleEnv = lookupWithDefaultUFM
358 mapModuleEnv = mapUFM
359 mkModuleEnv = listToUFM
360 emptyModuleEnv = emptyUFM
361 moduleEnvElts = eltsUFM
362 unitModuleEnv = unitUFM
363 unitModuleEnvByName = unitUFM
364 isEmptyModuleEnv = isNullUFM
365 foldModuleEnv = foldUFM
370 type ModuleSet = UniqSet Module
371 mkModuleSet :: [Module] -> ModuleSet
372 extendModuleSet :: ModuleSet -> Module -> ModuleSet
373 emptyModuleSet :: ModuleSet
374 moduleSetElts :: ModuleSet -> [Module]
375 elemModuleSet :: Module -> ModuleSet -> Bool
377 emptyModuleSet = emptyUniqSet
378 mkModuleSet = mkUniqSet
379 extendModuleSet = addOneToUniqSet
380 moduleSetElts = uniqSetToList
381 elemModuleSet = elementOfUniqSet