{-# LANGUAGE DeriveFunctor #-}
module GHC.Driver.Env.KnotVars( KnotVars(..)
, emptyKnotVars
, knotVarsFromModuleEnv
, knotVarElems
, lookupKnotVars
, knotVarsWithout
) where
import GHC.Prelude
import GHC.Unit.Types ( Module )
import GHC.Unit.Module.Env
import Data.Maybe
import GHC.Utils.Outputable
data KnotVars a = KnotVars { forall a. KnotVars a -> [Module]
kv_domain :: [Module]
, forall a. KnotVars a -> Module -> Maybe a
kv_lookup :: Module -> Maybe a
}
| NoKnotVars
deriving (forall a b. (a -> b) -> KnotVars a -> KnotVars b)
-> (forall a b. a -> KnotVars b -> KnotVars a) -> Functor KnotVars
forall a b. a -> KnotVars b -> KnotVars a
forall a b. (a -> b) -> KnotVars a -> KnotVars b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> KnotVars a -> KnotVars b
fmap :: forall a b. (a -> b) -> KnotVars a -> KnotVars b
$c<$ :: forall a b. a -> KnotVars b -> KnotVars a
<$ :: forall a b. a -> KnotVars b -> KnotVars a
Functor
instance Outputable (KnotVars a) where
ppr :: KnotVars a -> SDoc
ppr KnotVars a
NoKnotVars = String -> SDoc
text String
"NoKnot"
ppr (KnotVars [Module]
dom Module -> Maybe a
_lookup) = String -> SDoc
text String
"Knotty:" SDoc -> SDoc -> SDoc
<+> [Module] -> SDoc
forall a. Outputable a => a -> SDoc
ppr [Module]
dom
emptyKnotVars :: KnotVars a
emptyKnotVars :: forall a. KnotVars a
emptyKnotVars = KnotVars a
forall a. KnotVars a
NoKnotVars
knotVarsFromModuleEnv :: ModuleEnv a -> KnotVars a
knotVarsFromModuleEnv :: forall a. ModuleEnv a -> KnotVars a
knotVarsFromModuleEnv ModuleEnv a
me | ModuleEnv a -> Bool
forall a. ModuleEnv a -> Bool
isEmptyModuleEnv ModuleEnv a
me = KnotVars a
forall a. KnotVars a
NoKnotVars
knotVarsFromModuleEnv ModuleEnv a
me = [Module] -> (Module -> Maybe a) -> KnotVars a
forall a. [Module] -> (Module -> Maybe a) -> KnotVars a
KnotVars (ModuleEnv a -> [Module]
forall a. ModuleEnv a -> [Module]
moduleEnvKeys ModuleEnv a
me) (ModuleEnv a -> Module -> Maybe a
forall a. ModuleEnv a -> Module -> Maybe a
lookupModuleEnv ModuleEnv a
me)
knotVarElems :: KnotVars a -> [a]
knotVarElems :: forall a. KnotVars a -> [a]
knotVarElems (KnotVars [Module]
keys Module -> Maybe a
lookup) = (Module -> Maybe a) -> [Module] -> [a]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Module -> Maybe a
lookup [Module]
keys
knotVarElems KnotVars a
NoKnotVars = []
lookupKnotVars :: KnotVars a -> Module -> Maybe a
lookupKnotVars :: forall a. KnotVars a -> Module -> Maybe a
lookupKnotVars (KnotVars [Module]
_ Module -> Maybe a
lookup) Module
x = Module -> Maybe a
lookup Module
x
lookupKnotVars KnotVars a
NoKnotVars Module
_ = Maybe a
forall a. Maybe a
Nothing
knotVarsWithout :: Module -> KnotVars a -> KnotVars a
knotVarsWithout :: forall a. Module -> KnotVars a -> KnotVars a
knotVarsWithout Module
this_mod (KnotVars [Module]
loop_mods Module -> Maybe a
lkup) = [Module] -> (Module -> Maybe a) -> KnotVars a
forall a. [Module] -> (Module -> Maybe a) -> KnotVars a
KnotVars
((Module -> Bool) -> [Module] -> [Module]
forall a. (a -> Bool) -> [a] -> [a]
filter (Module -> Module -> Bool
forall a. Eq a => a -> a -> Bool
/= Module
this_mod) [Module]
loop_mods)
(\Module
that_mod -> if Module
that_mod Module -> Module -> Bool
forall a. Eq a => a -> a -> Bool
== Module
this_mod then Maybe a
forall a. Maybe a
Nothing else Module -> Maybe a
lkup Module
that_mod)
knotVarsWithout Module
_ KnotVars a
NoKnotVars = KnotVars a
forall a. KnotVars a
NoKnotVars