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