Make the Ord Module independent of Unique order
[ghc.git] / compiler / basicTypes / Module.hs
1 {-
2 (c) The University of Glasgow, 2004-2006
3
4
5 Module
6 ~~~~~~~~~~
7 Simply the name of a module, represented as a FastString.
8 These are Uniquable, hence we can build Maps with Modules as
9 the keys.
10 -}
11
12 {-# LANGUAGE RecordWildCards #-}
13 {-# LANGUAGE MultiParamTypeClasses #-}
14
15 module Module
16 (
17 -- * The ModuleName type
18 ModuleName,
19 pprModuleName,
20 moduleNameFS,
21 moduleNameString,
22 moduleNameSlashes, moduleNameColons,
23 moduleStableString,
24 mkModuleName,
25 mkModuleNameFS,
26 stableModuleNameCmp,
27
28 -- * The UnitId type
29 UnitId,
30 fsToUnitId,
31 unitIdFS,
32 stringToUnitId,
33 unitIdString,
34 stableUnitIdCmp,
35
36 -- * Wired-in UnitIds
37 -- $wired_in_packages
38 primUnitId,
39 integerUnitId,
40 baseUnitId,
41 rtsUnitId,
42 thUnitId,
43 dphSeqUnitId,
44 dphParUnitId,
45 mainUnitId,
46 thisGhcUnitId,
47 holeUnitId, isHoleModule,
48 interactiveUnitId, isInteractiveModule,
49 wiredInUnitIds,
50
51 -- * The Module type
52 Module(Module),
53 moduleUnitId, moduleName,
54 pprModule,
55 mkModule,
56 stableModuleCmp,
57 HasModule(..),
58 ContainsModule(..),
59
60 -- * The ModuleLocation type
61 ModLocation(..),
62 addBootSuffix, addBootSuffix_maybe, addBootSuffixLocn,
63
64 -- * Module mappings
65 ModuleEnv,
66 elemModuleEnv, extendModuleEnv, extendModuleEnvList,
67 extendModuleEnvList_C, plusModuleEnv_C,
68 delModuleEnvList, delModuleEnv, plusModuleEnv, lookupModuleEnv,
69 lookupWithDefaultModuleEnv, mapModuleEnv, mkModuleEnv, emptyModuleEnv,
70 moduleEnvKeys, moduleEnvElts, moduleEnvToList,
71 unitModuleEnv, isEmptyModuleEnv,
72 extendModuleEnvWith, filterModuleEnv,
73
74 -- * ModuleName mappings
75 ModuleNameEnv, DModuleNameEnv,
76
77 -- * Sets of Modules
78 ModuleSet,
79 emptyModuleSet, mkModuleSet, moduleSetElts, extendModuleSet, elemModuleSet
80 ) where
81
82 import Config
83 import Outputable
84 import Unique
85 import UniqFM
86 import UniqDFM
87 import FastString
88 import Binary
89 import Util
90 import Data.List
91 import Data.Ord
92 import {-# SOURCE #-} Packages
93 import GHC.PackageDb (BinaryStringRep(..), DbModuleRep(..), DbModule(..))
94
95 import Data.Data
96 import Data.Map (Map)
97 import Data.Set (Set)
98 import qualified Data.Map as Map
99 import qualified Data.Set as Set
100 import qualified FiniteMap as Map
101 import System.FilePath
102
103 -- Note [The identifier lexicon]
104 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105 -- Package keys, installed package IDs, ABI hashes, package names,
106 -- versions, there are a *lot* of different identifiers for closely
107 -- related things. What do they all mean? Here's what. (See also
108 -- https://ghc.haskell.org/trac/ghc/wiki/Commentary/Packages/Concepts )
109 --
110 -- THE IMPORTANT ONES
111 --
112 -- ComponentId: An opaque identifier provided by Cabal, which should
113 -- uniquely identify such things as the package name, the package
114 -- version, the name of the component, the hash of the source code
115 -- tarball, the selected Cabal flags, GHC flags, direct dependencies of
116 -- the component. These are very similar to InstalledPackageId, but
117 -- an 'InstalledPackageId' implies that it identifies a package, while
118 -- a package may install multiple components with different
119 -- 'ComponentId's.
120 -- - Same as Distribution.Package.ComponentId
121 --
122 -- UnitId: A ComponentId + a mapping from hole names (ModuleName) to
123 -- Modules. This is how the compiler identifies instantatiated
124 -- components, and also is the main identifier by which GHC identifies
125 -- things.
126 -- - When Backpack is not being used, UnitId = ComponentId.
127 -- this means a useful fiction for end-users is that there are
128 -- only ever ComponentIds, and some ComponentIds happen to have
129 -- more information (UnitIds).
130 -- - Same as Language.Haskell.TH.Syntax:PkgName, see
131 -- https://ghc.haskell.org/trac/ghc/ticket/10279
132 -- - The same as PackageKey in GHC 7.10 (we renamed it because
133 -- they don't necessarily identify packages anymore.)
134 -- - Same as -this-package-key/-package-name flags
135 --
136 -- Module: A UnitId + ModuleName. This is how the compiler identifies
137 -- modules (e.g. a Name is a Module + OccName)
138 -- - Same as Language.Haskell.TH.Syntax:Module
139 --
140 -- THE LESS IMPORTANT ONES
141 --
142 -- PackageName: The "name" field in a Cabal file, something like "lens".
143 -- - Same as Distribution.Package.PackageName
144 -- - DIFFERENT FROM Language.Haskell.TH.Syntax:PkgName, see
145 -- https://ghc.haskell.org/trac/ghc/ticket/10279
146 -- - DIFFERENT FROM -package-name flag
147 -- - DIFFERENT FROM the 'name' field in an installed package
148 -- information. This field could more accurately be described
149 -- as a munged package name: when it's for the main library
150 -- it is the same as the package name, but if it's an internal
151 -- library it's a munged combination of the package name and
152 -- the component name.
153 --
154 -- LEGACY ONES
155 --
156 -- InstalledPackageId: This is what we used to call ComponentId.
157 -- It's a still pretty useful concept for packages that have only
158 -- one library; in that case the logical InstalledPackageId =
159 -- ComponentId. Also, the Cabal nix-local-build continues to
160 -- compute an InstalledPackageId which is then forcibly used
161 -- for all components in a package. This means that if a dependency
162 -- from one component in a package changes, the InstalledPackageId
163 -- changes: you don't get as fine-grained dependency tracking,
164 -- but it means your builds are hermetic. Eventually, Cabal will
165 -- deal completely in components and we can get rid of this.
166 --
167 -- PackageKey: This is what we used to call UnitId. We ditched
168 -- "Package" from the name when we realized that you might want to
169 -- assign different "PackageKeys" to components from the same package.
170 -- (For a brief, non-released period of time, we also called these
171 -- UnitKeys).
172
173 {-
174 ************************************************************************
175 * *
176 \subsection{Module locations}
177 * *
178 ************************************************************************
179 -}
180
181 -- | Where a module lives on the file system: the actual locations
182 -- of the .hs, .hi and .o files, if we have them
183 data ModLocation
184 = ModLocation {
185 ml_hs_file :: Maybe FilePath,
186 -- The source file, if we have one. Package modules
187 -- probably don't have source files.
188
189 ml_hi_file :: FilePath,
190 -- Where the .hi file is, whether or not it exists
191 -- yet. Always of form foo.hi, even if there is an
192 -- hi-boot file (we add the -boot suffix later)
193
194 ml_obj_file :: FilePath
195 -- Where the .o file is, whether or not it exists yet.
196 -- (might not exist either because the module hasn't
197 -- been compiled yet, or because it is part of a
198 -- package with a .a file)
199 } deriving Show
200
201 instance Outputable ModLocation where
202 ppr = text . show
203
204 {-
205 For a module in another package, the hs_file and obj_file
206 components of ModLocation are undefined.
207
208 The locations specified by a ModLocation may or may not
209 correspond to actual files yet: for example, even if the object
210 file doesn't exist, the ModLocation still contains the path to
211 where the object file will reside if/when it is created.
212 -}
213
214 addBootSuffix :: FilePath -> FilePath
215 -- ^ Add the @-boot@ suffix to .hs, .hi and .o files
216 addBootSuffix path = path ++ "-boot"
217
218 addBootSuffix_maybe :: Bool -> FilePath -> FilePath
219 -- ^ Add the @-boot@ suffix if the @Bool@ argument is @True@
220 addBootSuffix_maybe is_boot path
221 | is_boot = addBootSuffix path
222 | otherwise = path
223
224 addBootSuffixLocn :: ModLocation -> ModLocation
225 -- ^ Add the @-boot@ suffix to all file paths associated with the module
226 addBootSuffixLocn locn
227 = locn { ml_hs_file = fmap addBootSuffix (ml_hs_file locn)
228 , ml_hi_file = addBootSuffix (ml_hi_file locn)
229 , ml_obj_file = addBootSuffix (ml_obj_file locn) }
230
231 {-
232 ************************************************************************
233 * *
234 \subsection{The name of a module}
235 * *
236 ************************************************************************
237 -}
238
239 -- | A ModuleName is essentially a simple string, e.g. @Data.List@.
240 newtype ModuleName = ModuleName FastString
241
242 instance Uniquable ModuleName where
243 getUnique (ModuleName nm) = getUnique nm
244
245 instance Eq ModuleName where
246 nm1 == nm2 = getUnique nm1 == getUnique nm2
247
248 instance Ord ModuleName where
249 nm1 `compare` nm2 = stableModuleNameCmp nm1 nm2
250
251 instance Outputable ModuleName where
252 ppr = pprModuleName
253
254 instance Binary ModuleName where
255 put_ bh (ModuleName fs) = put_ bh fs
256 get bh = do fs <- get bh; return (ModuleName fs)
257
258 instance BinaryStringRep ModuleName where
259 fromStringRep = mkModuleNameFS . mkFastStringByteString
260 toStringRep = fastStringToByteString . moduleNameFS
261
262 instance Data ModuleName where
263 -- don't traverse?
264 toConstr _ = abstractConstr "ModuleName"
265 gunfold _ _ = error "gunfold"
266 dataTypeOf _ = mkNoRepType "ModuleName"
267
268 stableModuleNameCmp :: ModuleName -> ModuleName -> Ordering
269 -- ^ Compares module names lexically, rather than by their 'Unique's
270 stableModuleNameCmp n1 n2 = moduleNameFS n1 `compare` moduleNameFS n2
271
272 pprModuleName :: ModuleName -> SDoc
273 pprModuleName (ModuleName nm) =
274 getPprStyle $ \ sty ->
275 if codeStyle sty
276 then ztext (zEncodeFS nm)
277 else ftext nm
278
279 moduleNameFS :: ModuleName -> FastString
280 moduleNameFS (ModuleName mod) = mod
281
282 moduleNameString :: ModuleName -> String
283 moduleNameString (ModuleName mod) = unpackFS mod
284
285 -- | Get a string representation of a 'Module' that's unique and stable
286 -- across recompilations.
287 -- eg. "$aeson_70dylHtv1FFGeai1IoxcQr$Data.Aeson.Types.Internal"
288 moduleStableString :: Module -> String
289 moduleStableString Module{..} =
290 "$" ++ unitIdString moduleUnitId ++ "$" ++ moduleNameString moduleName
291
292 mkModuleName :: String -> ModuleName
293 mkModuleName s = ModuleName (mkFastString s)
294
295 mkModuleNameFS :: FastString -> ModuleName
296 mkModuleNameFS s = ModuleName s
297
298 -- |Returns the string version of the module name, with dots replaced by slashes.
299 --
300 moduleNameSlashes :: ModuleName -> String
301 moduleNameSlashes = dots_to_slashes . moduleNameString
302 where dots_to_slashes = map (\c -> if c == '.' then pathSeparator else c)
303
304 -- |Returns the string version of the module name, with dots replaced by underscores.
305 --
306 moduleNameColons :: ModuleName -> String
307 moduleNameColons = dots_to_colons . moduleNameString
308 where dots_to_colons = map (\c -> if c == '.' then ':' else c)
309
310 {-
311 ************************************************************************
312 * *
313 \subsection{A fully qualified module}
314 * *
315 ************************************************************************
316 -}
317
318 -- | A Module is a pair of a 'UnitId' and a 'ModuleName'.
319 data Module = Module {
320 moduleUnitId :: !UnitId, -- pkg-1.0
321 moduleName :: !ModuleName -- A.B.C
322 }
323 deriving (Eq, Ord)
324
325 instance Uniquable Module where
326 getUnique (Module p n) = getUnique (unitIdFS p `appendFS` moduleNameFS n)
327
328 instance Outputable Module where
329 ppr = pprModule
330
331 instance Binary Module where
332 put_ bh (Module p n) = put_ bh p >> put_ bh n
333 get bh = do p <- get bh; n <- get bh; return (Module p n)
334
335 instance Data Module where
336 -- don't traverse?
337 toConstr _ = abstractConstr "Module"
338 gunfold _ _ = error "gunfold"
339 dataTypeOf _ = mkNoRepType "Module"
340
341 -- | This gives a stable ordering, as opposed to the Ord instance which
342 -- gives an ordering based on the 'Unique's of the components, which may
343 -- not be stable from run to run of the compiler.
344 stableModuleCmp :: Module -> Module -> Ordering
345 stableModuleCmp (Module p1 n1) (Module p2 n2)
346 = (p1 `stableUnitIdCmp` p2) `thenCmp`
347 (n1 `stableModuleNameCmp` n2)
348
349 mkModule :: UnitId -> ModuleName -> Module
350 mkModule = Module
351
352 pprModule :: Module -> SDoc
353 pprModule mod@(Module p n) =
354 pprPackagePrefix p mod <> pprModuleName n
355
356 pprPackagePrefix :: UnitId -> Module -> SDoc
357 pprPackagePrefix p mod = getPprStyle doc
358 where
359 doc sty
360 | codeStyle sty =
361 if p == mainUnitId
362 then empty -- never qualify the main package in code
363 else ztext (zEncodeFS (unitIdFS p)) <> char '_'
364 | qualModule sty mod = ppr (moduleUnitId mod) <> char ':'
365 -- the PrintUnqualified tells us which modules have to
366 -- be qualified with package names
367 | otherwise = empty
368
369 class ContainsModule t where
370 extractModule :: t -> Module
371
372 class HasModule m where
373 getModule :: m Module
374
375 instance DbModuleRep UnitId ModuleName Module where
376 fromDbModule (DbModule uid mod_name) = mkModule uid mod_name
377 toDbModule mod = DbModule (moduleUnitId mod) (moduleName mod)
378
379 {-
380 ************************************************************************
381 * *
382 \subsection{UnitId}
383 * *
384 ************************************************************************
385 -}
386
387 -- | A string which uniquely identifies a package. For wired-in packages,
388 -- it is just the package name, but for user compiled packages, it is a hash.
389 -- ToDo: when the key is a hash, we can do more clever things than store
390 -- the hex representation and hash-cons those strings.
391 newtype UnitId = PId FastString deriving Eq
392 -- here to avoid module loops with PackageConfig
393
394 instance Uniquable UnitId where
395 getUnique pid = getUnique (unitIdFS pid)
396
397 instance Ord UnitId where
398 nm1 `compare` nm2 = stableUnitIdCmp nm1 nm2
399
400 instance Data UnitId where
401 -- don't traverse?
402 toConstr _ = abstractConstr "UnitId"
403 gunfold _ _ = error "gunfold"
404 dataTypeOf _ = mkNoRepType "UnitId"
405
406 stableUnitIdCmp :: UnitId -> UnitId -> Ordering
407 -- ^ Compares package ids lexically, rather than by their 'Unique's
408 stableUnitIdCmp p1 p2 = unitIdFS p1 `compare` unitIdFS p2
409
410 instance Outputable UnitId where
411 ppr pk = getPprStyle $ \sty -> sdocWithDynFlags $ \dflags ->
412 case unitIdPackageIdString dflags pk of
413 Nothing -> ftext (unitIdFS pk)
414 Just pkg -> text pkg
415 -- Don't bother qualifying if it's wired in!
416 <> (if qualPackage sty pk && not (pk `elem` wiredInUnitIds)
417 then char '@' <> ftext (unitIdFS pk)
418 else empty)
419
420 instance Binary UnitId where
421 put_ bh pid = put_ bh (unitIdFS pid)
422 get bh = do { fs <- get bh; return (fsToUnitId fs) }
423
424 instance BinaryStringRep UnitId where
425 fromStringRep = fsToUnitId . mkFastStringByteString
426 toStringRep = fastStringToByteString . unitIdFS
427
428 fsToUnitId :: FastString -> UnitId
429 fsToUnitId = PId
430
431 unitIdFS :: UnitId -> FastString
432 unitIdFS (PId fs) = fs
433
434 stringToUnitId :: String -> UnitId
435 stringToUnitId = fsToUnitId . mkFastString
436
437 unitIdString :: UnitId -> String
438 unitIdString = unpackFS . unitIdFS
439
440
441 -- -----------------------------------------------------------------------------
442 -- $wired_in_packages
443 -- Certain packages are known to the compiler, in that we know about certain
444 -- entities that reside in these packages, and the compiler needs to
445 -- declare static Modules and Names that refer to these packages. Hence
446 -- the wired-in packages can't include version numbers, since we don't want
447 -- to bake the version numbers of these packages into GHC.
448 --
449 -- So here's the plan. Wired-in packages are still versioned as
450 -- normal in the packages database, and you can still have multiple
451 -- versions of them installed. However, for each invocation of GHC,
452 -- only a single instance of each wired-in package will be recognised
453 -- (the desired one is selected via @-package@\/@-hide-package@), and GHC
454 -- will use the unversioned 'UnitId' below when referring to it,
455 -- including in .hi files and object file symbols. Unselected
456 -- versions of wired-in packages will be ignored, as will any other
457 -- package that depends directly or indirectly on it (much as if you
458 -- had used @-ignore-package@).
459
460 -- Make sure you change 'Packages.findWiredInPackages' if you add an entry here
461
462 integerUnitId, primUnitId,
463 baseUnitId, rtsUnitId,
464 thUnitId, dphSeqUnitId, dphParUnitId,
465 mainUnitId, thisGhcUnitId, interactiveUnitId :: UnitId
466 primUnitId = fsToUnitId (fsLit "ghc-prim")
467 integerUnitId = fsToUnitId (fsLit n)
468 where
469 n = case cIntegerLibraryType of
470 IntegerGMP -> "integer-gmp"
471 IntegerSimple -> "integer-simple"
472 baseUnitId = fsToUnitId (fsLit "base")
473 rtsUnitId = fsToUnitId (fsLit "rts")
474 thUnitId = fsToUnitId (fsLit "template-haskell")
475 dphSeqUnitId = fsToUnitId (fsLit "dph-seq")
476 dphParUnitId = fsToUnitId (fsLit "dph-par")
477 thisGhcUnitId = fsToUnitId (fsLit "ghc")
478 interactiveUnitId = fsToUnitId (fsLit "interactive")
479
480 -- | This is the package Id for the current program. It is the default
481 -- package Id if you don't specify a package name. We don't add this prefix
482 -- to symbol names, since there can be only one main package per program.
483 mainUnitId = fsToUnitId (fsLit "main")
484
485 -- | This is a fake package id used to provide identities to any un-implemented
486 -- signatures. The set of hole identities is global over an entire compilation.
487 holeUnitId :: UnitId
488 holeUnitId = fsToUnitId (fsLit "hole")
489
490 isInteractiveModule :: Module -> Bool
491 isInteractiveModule mod = moduleUnitId mod == interactiveUnitId
492
493 isHoleModule :: Module -> Bool
494 isHoleModule mod = moduleUnitId mod == holeUnitId
495
496 wiredInUnitIds :: [UnitId]
497 wiredInUnitIds = [ primUnitId,
498 integerUnitId,
499 baseUnitId,
500 rtsUnitId,
501 thUnitId,
502 thisGhcUnitId,
503 dphSeqUnitId,
504 dphParUnitId ]
505
506 {-
507 ************************************************************************
508 * *
509 \subsection{@ModuleEnv@s}
510 * *
511 ************************************************************************
512 -}
513
514 -- | A map keyed off of 'Module's
515 newtype ModuleEnv elt = ModuleEnv (Map NDModule elt)
516 {-
517 Note [ModuleEnv performance and determinism]
518 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
519 To prevent accidental reintroduction of nondeterminism the Ord instance
520 for Module was changed to not depend on Unique ordering and to use the
521 lexicographic order. This is potentially expensive, but when measured
522 there was no difference in performance.
523
524 To be on the safe side and not pessimize ModuleEnv uses nondeterministic
525 ordering on Module and normalizes by doing the lexicographic sort when
526 turning the env to a list.
527 See Note [Unique Determinism] for more information about the source of
528 nondeterminismand and Note [Deterministic UniqFM] for explanation of why
529 it matters for maps.
530 -}
531
532 newtype NDModule = NDModule { unNDModule :: Module }
533 deriving Eq
534 -- A wrapper for Module with faster nondeterministic Ord.
535 -- Don't export, See [ModuleEnv performance and determinism]
536
537 instance Ord NDModule where
538 compare (NDModule (Module p1 n1)) (NDModule (Module p2 n2)) =
539 (getUnique p1 `compare` getUnique p2) `thenCmp`
540 (getUnique n1 `compare` getUnique n2)
541
542 filterModuleEnv :: (Module -> a -> Bool) -> ModuleEnv a -> ModuleEnv a
543 filterModuleEnv f (ModuleEnv e) =
544 ModuleEnv (Map.filterWithKey (f . unNDModule) e)
545
546 elemModuleEnv :: Module -> ModuleEnv a -> Bool
547 elemModuleEnv m (ModuleEnv e) = Map.member (NDModule m) e
548
549 extendModuleEnv :: ModuleEnv a -> Module -> a -> ModuleEnv a
550 extendModuleEnv (ModuleEnv e) m x = ModuleEnv (Map.insert (NDModule m) x e)
551
552 extendModuleEnvWith :: (a -> a -> a) -> ModuleEnv a -> Module -> a
553 -> ModuleEnv a
554 extendModuleEnvWith f (ModuleEnv e) m x =
555 ModuleEnv (Map.insertWith f (NDModule m) x e)
556
557 extendModuleEnvList :: ModuleEnv a -> [(Module, a)] -> ModuleEnv a
558 extendModuleEnvList (ModuleEnv e) xs =
559 ModuleEnv (Map.insertList [(NDModule k, v) | (k,v) <- xs] e)
560
561 extendModuleEnvList_C :: (a -> a -> a) -> ModuleEnv a -> [(Module, a)]
562 -> ModuleEnv a
563 extendModuleEnvList_C f (ModuleEnv e) xs =
564 ModuleEnv (Map.insertListWith f [(NDModule k, v) | (k,v) <- xs] e)
565
566 plusModuleEnv_C :: (a -> a -> a) -> ModuleEnv a -> ModuleEnv a -> ModuleEnv a
567 plusModuleEnv_C f (ModuleEnv e1) (ModuleEnv e2) =
568 ModuleEnv (Map.unionWith f e1 e2)
569
570 delModuleEnvList :: ModuleEnv a -> [Module] -> ModuleEnv a
571 delModuleEnvList (ModuleEnv e) ms =
572 ModuleEnv (Map.deleteList (map NDModule ms) e)
573
574 delModuleEnv :: ModuleEnv a -> Module -> ModuleEnv a
575 delModuleEnv (ModuleEnv e) m = ModuleEnv (Map.delete (NDModule m) e)
576
577 plusModuleEnv :: ModuleEnv a -> ModuleEnv a -> ModuleEnv a
578 plusModuleEnv (ModuleEnv e1) (ModuleEnv e2) = ModuleEnv (Map.union e1 e2)
579
580 lookupModuleEnv :: ModuleEnv a -> Module -> Maybe a
581 lookupModuleEnv (ModuleEnv e) m = Map.lookup (NDModule m) e
582
583 lookupWithDefaultModuleEnv :: ModuleEnv a -> a -> Module -> a
584 lookupWithDefaultModuleEnv (ModuleEnv e) x m =
585 Map.findWithDefault x (NDModule m) e
586
587 mapModuleEnv :: (a -> b) -> ModuleEnv a -> ModuleEnv b
588 mapModuleEnv f (ModuleEnv e) = ModuleEnv (Map.mapWithKey (\_ v -> f v) e)
589
590 mkModuleEnv :: [(Module, a)] -> ModuleEnv a
591 mkModuleEnv xs = ModuleEnv (Map.fromList [(NDModule k, v) | (k,v) <- xs])
592
593 emptyModuleEnv :: ModuleEnv a
594 emptyModuleEnv = ModuleEnv Map.empty
595
596 moduleEnvKeys :: ModuleEnv a -> [Module]
597 moduleEnvKeys (ModuleEnv e) = sort $ map unNDModule $ Map.keys e
598 -- See Note [ModuleEnv performance and determinism]
599
600 moduleEnvElts :: ModuleEnv a -> [a]
601 moduleEnvElts e = map snd $ moduleEnvToList e
602 -- See Note [ModuleEnv performance and determinism]
603
604 moduleEnvToList :: ModuleEnv a -> [(Module, a)]
605 moduleEnvToList (ModuleEnv e) =
606 sortBy (comparing fst) [(m, v) | (NDModule m, v) <- Map.toList e]
607 -- See Note [ModuleEnv performance and determinism]
608
609 unitModuleEnv :: Module -> a -> ModuleEnv a
610 unitModuleEnv m x = ModuleEnv (Map.singleton (NDModule m) x)
611
612 isEmptyModuleEnv :: ModuleEnv a -> Bool
613 isEmptyModuleEnv (ModuleEnv e) = Map.null e
614
615 -- | A set of 'Module's
616 type ModuleSet = Set Module
617
618 mkModuleSet :: [Module] -> ModuleSet
619 extendModuleSet :: ModuleSet -> Module -> ModuleSet
620 emptyModuleSet :: ModuleSet
621 moduleSetElts :: ModuleSet -> [Module]
622 elemModuleSet :: Module -> ModuleSet -> Bool
623
624 emptyModuleSet = Set.empty
625 mkModuleSet = Set.fromList
626 extendModuleSet s m = Set.insert m s
627 moduleSetElts = Set.toList
628 elemModuleSet = Set.member
629
630 {-
631 A ModuleName has a Unique, so we can build mappings of these using
632 UniqFM.
633 -}
634
635 -- | A map keyed off of 'ModuleName's (actually, their 'Unique's)
636 type ModuleNameEnv elt = UniqFM elt
637
638
639 -- | A map keyed off of 'ModuleName's (actually, their 'Unique's)
640 -- Has deterministic folds and can be deterministically converted to a list
641 type DModuleNameEnv elt = UniqDFM elt