module GHC.Types.Fixity.Env
   ( FixityEnv
   , FixItem (..)
   , emptyFixityEnv
   , lookupFixity
   , mkIfaceFixCache
   , emptyIfaceFixCache
   )
where

import GHC.Prelude

import GHC.Types.Fixity
import GHC.Types.Name
import GHC.Types.Name.Env

import GHC.Utils.Outputable

-- | Fixity environment mapping names to their fixities
type FixityEnv = NameEnv FixItem

-- | Fixity information for an 'Name'. We keep the OccName in the range
-- so that we can generate an interface from it
data FixItem = FixItem OccName Fixity

instance Outputable FixItem where
  ppr :: FixItem -> SDoc
ppr (FixItem OccName
occ Fixity
fix) = forall a. Outputable a => a -> SDoc
ppr Fixity
fix SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr OccName
occ

emptyFixityEnv :: FixityEnv
emptyFixityEnv :: FixityEnv
emptyFixityEnv = forall a. NameEnv a
emptyNameEnv

lookupFixity :: FixityEnv -> Name -> Fixity
lookupFixity :: FixityEnv -> Name -> Fixity
lookupFixity FixityEnv
env Name
n = case forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv FixityEnv
env Name
n of
                        Just (FixItem OccName
_ Fixity
fix) -> Fixity
fix
                        Maybe FixItem
Nothing         -> Fixity
defaultFixity

-- | Creates cached lookup for the 'mi_fix_fn' field of 'ModIface'
mkIfaceFixCache :: [(OccName, Fixity)] -> OccName -> Maybe Fixity
mkIfaceFixCache :: [(OccName, Fixity)] -> OccName -> Maybe Fixity
mkIfaceFixCache [(OccName, Fixity)]
pairs
  = \OccName
n -> forall a. OccEnv a -> OccName -> Maybe a
lookupOccEnv OccEnv Fixity
env OccName
n
  where
   env :: OccEnv Fixity
env = forall a. [(OccName, a)] -> OccEnv a
mkOccEnv [(OccName, Fixity)]
pairs

emptyIfaceFixCache :: OccName -> Maybe Fixity
emptyIfaceFixCache :: OccName -> Maybe Fixity
emptyIfaceFixCache OccName
_ = forall a. Maybe a
Nothing