{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE DeriveFoldable #-}
module Distribution.Backpack.ModuleScope (
ModuleScope(..),
ModuleProvides,
ModuleRequires,
ModuleSource(..),
dispModuleSource,
WithSource(..),
unWithSource,
getSource,
ModuleWithSource,
emptyModuleScope,
) where
import Prelude ()
import Distribution.Compat.Prelude
import Distribution.ModuleName
import Distribution.Types.IncludeRenaming
import Distribution.Types.PackageName
import Distribution.Types.ComponentName
import Distribution.Types.LibraryName
import Distribution.Pretty
import Distribution.Backpack
import Distribution.Backpack.ModSubst
import qualified Data.Map as Map
import Text.PrettyPrint
data ModuleScope = ModuleScope {
modScopeProvides :: ModuleProvides,
modScopeRequires :: ModuleRequires
}
emptyModuleScope :: ModuleScope
emptyModuleScope = ModuleScope Map.empty Map.empty
type ModuleProvides = Map ModuleName [ModuleWithSource]
type ModuleRequires = Map ModuleName [ModuleWithSource]
data ModuleSource
= FromMixins PackageName ComponentName IncludeRenaming
| FromBuildDepends PackageName ComponentName
| FromExposedModules ModuleName
| FromOtherModules ModuleName
| FromSignatures ModuleName
dispModuleSource :: ModuleSource -> Doc
dispModuleSource (FromMixins pn cn incls)
= text "mixins:" <+> dispComponent pn cn <+> pretty incls
dispModuleSource (FromBuildDepends pn cn)
= text "build-depends:" <+> dispComponent pn cn
dispModuleSource (FromExposedModules m)
= text "exposed-modules:" <+> pretty m
dispModuleSource (FromOtherModules m)
= text "other-modules:" <+> pretty m
dispModuleSource (FromSignatures m)
= text "signatures:" <+> pretty m
dispComponent :: PackageName -> ComponentName -> Doc
dispComponent pn cn =
case cn of
CLibName LMainLibName -> pretty pn
CLibName (LSubLibName ucn) -> pretty pn <<>> colon <<>> pretty ucn
_ -> pretty pn <+> parens (pretty cn)
data WithSource a = WithSource ModuleSource a
deriving (Functor, Foldable, Traversable)
unWithSource :: WithSource a -> a
unWithSource (WithSource _ x) = x
getSource :: WithSource a -> ModuleSource
getSource (WithSource s _) = s
type ModuleWithSource = WithSource OpenModule
instance ModSubst a => ModSubst (WithSource a) where
modSubst subst (WithSource s m) = WithSource s (modSubst subst m)