{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
module GHC.Types.Name.Cache
( lookupOrigNameCache
, extendOrigNameCache
, extendNameCache
, initNameCache
, NameCache(..), OrigNameCache
) where
import GHC.Prelude
import GHC.Unit.Module
import GHC.Types.Name
import GHC.Types.Unique.Supply
import GHC.Builtin.Types
import GHC.Builtin.Names
import GHC.Utils.Misc
import GHC.Utils.Outputable
import GHC.Utils.Panic
#include "GhclibHsVersions.h"
type OrigNameCache = ModuleEnv (OccEnv Name)
lookupOrigNameCache :: OrigNameCache -> Module -> OccName -> Maybe Name
lookupOrigNameCache :: OrigNameCache -> Module -> OccName -> Maybe Name
lookupOrigNameCache OrigNameCache
nc Module
mod OccName
occ
| Module
mod forall a. Eq a => a -> a -> Bool
== Module
gHC_TYPES Bool -> Bool -> Bool
|| Module
mod forall a. Eq a => a -> a -> Bool
== Module
gHC_PRIM Bool -> Bool -> Bool
|| Module
mod forall a. Eq a => a -> a -> Bool
== Module
gHC_TUPLE
, Just Name
name <- OccName -> Maybe Name
isBuiltInOcc_maybe OccName
occ
=
forall a. a -> Maybe a
Just Name
name
| Bool
otherwise
= case forall a. ModuleEnv a -> Module -> Maybe a
lookupModuleEnv OrigNameCache
nc Module
mod of
Maybe (OccEnv Name)
Nothing -> forall a. Maybe a
Nothing
Just OccEnv Name
occ_env -> forall a. OccEnv a -> OccName -> Maybe a
lookupOccEnv OccEnv Name
occ_env OccName
occ
extendOrigNameCache :: OrigNameCache -> Name -> OrigNameCache
extendOrigNameCache :: OrigNameCache -> Name -> OrigNameCache
extendOrigNameCache OrigNameCache
nc Name
name
= ASSERT2( isExternalName name, ppr name )
OrigNameCache -> Module -> OccName -> Name -> OrigNameCache
extendNameCache OrigNameCache
nc (HasDebugCallStack => Name -> Module
nameModule Name
name) (Name -> OccName
nameOccName Name
name) Name
name
extendNameCache :: OrigNameCache -> Module -> OccName -> Name -> OrigNameCache
extendNameCache :: OrigNameCache -> Module -> OccName -> Name -> OrigNameCache
extendNameCache OrigNameCache
nc Module
mod OccName
occ Name
name
= forall a.
(a -> a -> a) -> ModuleEnv a -> Module -> a -> ModuleEnv a
extendModuleEnvWith OccEnv Name -> OccEnv Name -> OccEnv Name
combine OrigNameCache
nc Module
mod (forall a. OccName -> a -> OccEnv a
unitOccEnv OccName
occ Name
name)
where
combine :: OccEnv Name -> OccEnv Name -> OccEnv Name
combine OccEnv Name
_ OccEnv Name
occ_env = forall a. OccEnv a -> OccName -> a -> OccEnv a
extendOccEnv OccEnv Name
occ_env OccName
occ Name
name
data NameCache
= NameCache { NameCache -> UniqSupply
nsUniqs :: !UniqSupply,
NameCache -> OrigNameCache
nsNames :: !OrigNameCache
}
initNameCache :: UniqSupply -> [Name] -> NameCache
initNameCache :: UniqSupply -> [Name] -> NameCache
initNameCache UniqSupply
us [Name]
names
= NameCache { nsUniqs :: UniqSupply
nsUniqs = UniqSupply
us,
nsNames :: OrigNameCache
nsNames = [Name] -> OrigNameCache
initOrigNames [Name]
names }
initOrigNames :: [Name] -> OrigNameCache
initOrigNames :: [Name] -> OrigNameCache
initOrigNames [Name]
names = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' OrigNameCache -> Name -> OrigNameCache
extendOrigNameCache forall a. ModuleEnv a
emptyModuleEnv [Name]
names