Skip to content

Commit

Permalink
Add a Module data type.
Browse files Browse the repository at this point in the history
Signed-off-by: Edward Z. Yang <[email protected]>
  • Loading branch information
ezyang committed Mar 31, 2016
1 parent e65fe1f commit db9ccb5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
22 changes: 2 additions & 20 deletions Cabal/Distribution/InstalledPackageInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module Distribution.InstalledPackageInfo (
InstalledPackageInfo(..),
installedComponentId,
installedPackageId,
OriginalModule(..), ExposedModule(..),
ExposedModule(..),
ParseResult(..), PError(..), PWarning,
emptyInstalledPackageInfo,
parseInstalledPackageInfo,
Expand Down Expand Up @@ -162,29 +162,13 @@ emptyInstalledPackageInfo
-- -----------------------------------------------------------------------------
-- Exposed modules

data OriginalModule
= OriginalModule {
originalPackageId :: UnitId,
originalModuleName :: ModuleName
}
deriving (Generic, Eq, Read, Show)

data ExposedModule
= ExposedModule {
exposedName :: ModuleName,
exposedReexport :: Maybe OriginalModule
exposedReexport :: Maybe Module
}
deriving (Eq, Generic, Read, Show)

instance Text OriginalModule where
disp (OriginalModule ipi m) =
disp ipi <> Disp.char ':' <> disp m
parse = do
ipi <- parse
_ <- Parse.char ':'
m <- parse
return (OriginalModule ipi m)

instance Text ExposedModule where
disp (ExposedModule m reexport) =
Disp.sep [ disp m
Expand All @@ -202,8 +186,6 @@ instance Text ExposedModule where
return (ExposedModule m reexport)


instance Binary OriginalModule

instance Binary ExposedModule

-- To maintain backwards-compatibility, we accept both comma/non-comma
Expand Down
33 changes: 30 additions & 3 deletions Cabal/Distribution/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ module Distribution.Package (
getHSLibraryName,
InstalledPackageId, -- backwards compat

-- * Modules
Module(..),

-- * ABI hash
AbiHash(..),

Expand All @@ -55,6 +58,7 @@ import qualified Text.PrettyPrint as Disp
import Distribution.Compat.ReadP
import Distribution.Compat.Binary
import Distribution.Text
import Distribution.ModuleName

import Control.DeepSeq (NFData(..))
import qualified Data.Char as Char
Expand Down Expand Up @@ -111,9 +115,32 @@ instance Text PackageIdentifier where
instance NFData PackageIdentifier where
rnf (PackageIdentifier name version) = rnf name `seq` rnf version

-- ------------------------------------------------------------
-- * Component Source Hash
-- ------------------------------------------------------------
-- | A module identity uniquely identifies a Haskell module by
-- qualifying a 'ModuleName' with the 'UnitId' which defined
-- it. This type distinguishes between two packages
-- which provide a module with the same name, or a module
-- from the same package compiled with different dependencies.
-- There are a few cases where Cabal needs to know about
-- module identities, e.g., when writing out reexported modules in
-- the 'InstalledPackageInfo'.
data Module =
Module { moduleUnitId :: UnitId,
moduleName :: ModuleName }
deriving (Generic, Read, Show, Eq, Ord, Typeable, Data)

instance Binary Module

instance Text Module where
disp (Module uid mod_name) =
disp uid <> Disp.text ":" <> disp mod_name
parse = do
uid <- parse
_ <- Parse.char ':'
mod_name <- parse
return (Module uid mod_name)

instance NFData Module where
rnf (Module uid mod_name) = rnf uid `seq` rnf mod_name

-- | A 'ComponentId' uniquely identifies the transitive source
-- code closure of a component. For non-Backpack components, it also
Expand Down
6 changes: 3 additions & 3 deletions Cabal/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1740,8 +1740,8 @@ resolveModuleReexports installedPackages srcpkgid key externalPkgDeps lib =
, let exportingPackageName = packageName srcpkgid
definingModuleName = visibleModuleName
definingPackageId = key
originalModule = Installed.OriginalModule definingPackageId
definingModuleName
originalModule = Module definingPackageId
definingModuleName
exposedModule = Installed.ExposedModule visibleModuleName
(Just originalModule)
]
Expand All @@ -1758,7 +1758,7 @@ resolveModuleReexports installedPackages srcpkgid key externalPkgDeps lib =
-- In this case the reexport will point to this package.
Nothing -> return exposedModule {
Installed.exposedReexport =
Just (Installed.OriginalModule
Just (Module
(Installed.installedUnitId pkg)
(Installed.exposedName exposedModule)) }
-- On the other hand, a visible module might actually be itself
Expand Down
4 changes: 2 additions & 2 deletions Cabal/Distribution/Simple/PackageIndex.hs
Original file line number Diff line number Diff line change
Expand Up @@ -614,8 +614,8 @@ moduleNameIndex index =
IPI.ExposedModule m reexport <- IPI.exposedModules pkg
case reexport of
Nothing -> return (m, [pkg])
Just (IPI.OriginalModule _ m') | m == m' -> []
| otherwise -> return (m', [pkg])
Just (Module _ m') | m == m' -> []
| otherwise -> return (m', [pkg])
-- The heuristic is this: we want to prefer the original package
-- which originally exported a module. However, if a reexport
-- also *renamed* the module (m /= m'), then we have to use the
Expand Down

0 comments on commit db9ccb5

Please sign in to comment.