PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/compiler/basicTypes/Module.hs

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