{-# LANGUAGE CPP #-}
module NameShape(
NameShape(..),
emptyNameShape,
mkNameShape,
extendNameShape,
nameShapeExports,
substNameShape,
maybeSubstNameShape,
) where
#include "HsVersions.h"
import GhcPrelude
import Outputable
import HscTypes
import Module
import UniqFM
import Avail
import FieldLabel
import Name
import NameEnv
import TcRnMonad
import Util
import IfaceEnv
import Control.Monad
emptyNameShape :: ModuleName -> NameShape
emptyNameShape :: ModuleName -> NameShape
emptyNameShape mod_name :: ModuleName
mod_name = ModuleName -> [AvailInfo] -> OccEnv Name -> NameShape
NameShape ModuleName
mod_name [] OccEnv Name
forall a. OccEnv a
emptyOccEnv
mkNameShape :: ModuleName -> [AvailInfo] -> NameShape
mkNameShape :: ModuleName -> [AvailInfo] -> NameShape
mkNameShape mod_name :: ModuleName
mod_name as :: [AvailInfo]
as =
ModuleName -> [AvailInfo] -> OccEnv Name -> NameShape
NameShape ModuleName
mod_name [AvailInfo]
as (OccEnv Name -> NameShape) -> OccEnv Name -> NameShape
forall a b. (a -> b) -> a -> b
$ [(OccName, Name)] -> OccEnv Name
forall a. [(OccName, a)] -> OccEnv a
mkOccEnv ([(OccName, Name)] -> OccEnv Name)
-> [(OccName, Name)] -> OccEnv Name
forall a b. (a -> b) -> a -> b
$ do
AvailInfo
a <- [AvailInfo]
as
Name
n <- AvailInfo -> Name
availName AvailInfo
a Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: AvailInfo -> [Name]
availNamesWithSelectors AvailInfo
a
(OccName, Name) -> [(OccName, Name)]
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> OccName
forall name. HasOccName name => name -> OccName
occName Name
n, Name
n)
extendNameShape :: HscEnv -> NameShape -> [AvailInfo] -> IO (Either SDoc NameShape)
extendNameShape :: HscEnv -> NameShape -> [AvailInfo] -> IO (Either SDoc NameShape)
extendNameShape hsc_env :: HscEnv
hsc_env ns :: NameShape
ns as :: [AvailInfo]
as =
case ModuleName -> [AvailInfo] -> [AvailInfo] -> Either SDoc ShNameSubst
uAvailInfos (NameShape -> ModuleName
ns_mod_name NameShape
ns) (NameShape -> [AvailInfo]
ns_exports NameShape
ns) [AvailInfo]
as of
Left err :: SDoc
err -> Either SDoc NameShape -> IO (Either SDoc NameShape)
forall (m :: * -> *) a. Monad m => a -> m a
return (SDoc -> Either SDoc NameShape
forall a b. a -> Either a b
Left SDoc
err)
Right nsubst :: ShNameSubst
nsubst -> do
[AvailInfo]
as1 <- (AvailInfo -> IO AvailInfo) -> [AvailInfo] -> IO [AvailInfo]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (IO AvailInfo -> IO AvailInfo
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO AvailInfo -> IO AvailInfo)
-> (AvailInfo -> IO AvailInfo) -> AvailInfo -> IO AvailInfo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HscEnv -> ShNameSubst -> AvailInfo -> IO AvailInfo
substNameAvailInfo HscEnv
hsc_env ShNameSubst
nsubst) (NameShape -> [AvailInfo]
ns_exports NameShape
ns)
[AvailInfo]
as2 <- (AvailInfo -> IO AvailInfo) -> [AvailInfo] -> IO [AvailInfo]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (IO AvailInfo -> IO AvailInfo
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO AvailInfo -> IO AvailInfo)
-> (AvailInfo -> IO AvailInfo) -> AvailInfo -> IO AvailInfo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HscEnv -> ShNameSubst -> AvailInfo -> IO AvailInfo
substNameAvailInfo HscEnv
hsc_env ShNameSubst
nsubst) [AvailInfo]
as
let new_avails :: [AvailInfo]
new_avails = [AvailInfo] -> [AvailInfo] -> [AvailInfo]
mergeAvails [AvailInfo]
as1 [AvailInfo]
as2
Either SDoc NameShape -> IO (Either SDoc NameShape)
forall (m :: * -> *) a. Monad m => a -> m a
return (Either SDoc NameShape -> IO (Either SDoc NameShape))
-> (NameShape -> Either SDoc NameShape)
-> NameShape
-> IO (Either SDoc NameShape)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameShape -> Either SDoc NameShape
forall a b. b -> Either a b
Right (NameShape -> IO (Either SDoc NameShape))
-> NameShape -> IO (Either SDoc NameShape)
forall a b. (a -> b) -> a -> b
$ NameShape
ns {
ns_exports :: [AvailInfo]
ns_exports = [AvailInfo]
new_avails,
ns_map :: OccEnv Name
ns_map = [(OccName, Name)] -> OccEnv Name
forall a. [(OccName, a)] -> OccEnv a
mkOccEnv ([(OccName, Name)] -> OccEnv Name)
-> [(OccName, Name)] -> OccEnv Name
forall a b. (a -> b) -> a -> b
$ do
AvailInfo
a <- [AvailInfo]
new_avails
Name
n <- AvailInfo -> Name
availName AvailInfo
a Name -> [Name] -> [Name]
forall a. a -> [a] -> [a]
: AvailInfo -> [Name]
availNames AvailInfo
a
(OccName, Name) -> [(OccName, Name)]
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> OccName
forall name. HasOccName name => name -> OccName
occName Name
n, Name
n)
}
nameShapeExports :: NameShape -> [AvailInfo]
nameShapeExports :: NameShape -> [AvailInfo]
nameShapeExports = NameShape -> [AvailInfo]
ns_exports
substNameShape :: NameShape -> Name -> Name
substNameShape :: NameShape -> Name -> Name
substNameShape ns :: NameShape
ns n :: Name
n | HasDebugCallStack => Name -> Module
Name -> Module
nameModule Name
n Module -> Module -> Bool
forall a. Eq a => a -> a -> Bool
== NameShape -> Module
ns_module NameShape
ns
, Just n' :: Name
n' <- OccEnv Name -> OccName -> Maybe Name
forall a. OccEnv a -> OccName -> Maybe a
lookupOccEnv (NameShape -> OccEnv Name
ns_map NameShape
ns) (Name -> OccName
forall name. HasOccName name => name -> OccName
occName Name
n)
= Name
n'
| Bool
otherwise
= Name
n
maybeSubstNameShape :: NameShape -> Name -> Maybe Name
maybeSubstNameShape :: NameShape -> Name -> Maybe Name
maybeSubstNameShape ns :: NameShape
ns n :: Name
n
| HasDebugCallStack => Name -> Module
Name -> Module
nameModule Name
n Module -> Module -> Bool
forall a. Eq a => a -> a -> Bool
== NameShape -> Module
ns_module NameShape
ns
= OccEnv Name -> OccName -> Maybe Name
forall a. OccEnv a -> OccName -> Maybe a
lookupOccEnv (NameShape -> OccEnv Name
ns_map NameShape
ns) (Name -> OccName
forall name. HasOccName name => name -> OccName
occName Name
n)
| Bool
otherwise
= Maybe Name
forall a. Maybe a
Nothing
ns_module :: NameShape -> Module
ns_module :: NameShape -> Module
ns_module = ModuleName -> Module
mkHoleModule (ModuleName -> Module)
-> (NameShape -> ModuleName) -> NameShape -> Module
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NameShape -> ModuleName
ns_mod_name
type ShNameSubst = NameEnv Name
substName :: ShNameSubst -> Name -> Name
substName :: ShNameSubst -> Name -> Name
substName env :: ShNameSubst
env n :: Name
n | Just n' :: Name
n' <- ShNameSubst -> Name -> Maybe Name
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv ShNameSubst
env Name
n = Name
n'
| Bool
otherwise = Name
n
substNameAvailInfo :: HscEnv -> ShNameSubst -> AvailInfo -> IO AvailInfo
substNameAvailInfo :: HscEnv -> ShNameSubst -> AvailInfo -> IO AvailInfo
substNameAvailInfo _ env :: ShNameSubst
env (Avail n :: Name
n) = AvailInfo -> IO AvailInfo
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> AvailInfo
Avail (ShNameSubst -> Name -> Name
substName ShNameSubst
env Name
n))
substNameAvailInfo hsc_env :: HscEnv
hsc_env env :: ShNameSubst
env (AvailTC n :: Name
n ns :: [Name]
ns fs :: [FieldLabel]
fs) =
let mb_mod :: Maybe Module
mb_mod = (Name -> Module) -> Maybe Name -> Maybe Module
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap HasDebugCallStack => Name -> Module
Name -> Module
nameModule (ShNameSubst -> Name -> Maybe Name
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv ShNameSubst
env Name
n)
in Name -> [Name] -> [FieldLabel] -> AvailInfo
AvailTC (ShNameSubst -> Name -> Name
substName ShNameSubst
env Name
n)
([Name] -> [FieldLabel] -> AvailInfo)
-> IO [Name] -> IO ([FieldLabel] -> AvailInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Name -> IO Name) -> [Name] -> IO [Name]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (HscEnv -> IfG Name -> IO Name
forall a. HscEnv -> IfG a -> IO a
initIfaceLoad HscEnv
hsc_env (IfG Name -> IO Name) -> (Name -> IfG Name) -> Name -> IO Name
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe Module -> Name -> IfG Name
forall m n. Maybe Module -> Name -> TcRnIf m n Name
setNameModule Maybe Module
mb_mod) [Name]
ns
IO ([FieldLabel] -> AvailInfo) -> IO [FieldLabel] -> IO AvailInfo
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (FieldLabel -> IO FieldLabel) -> [FieldLabel] -> IO [FieldLabel]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (HscEnv -> Maybe Module -> FieldLabel -> IO FieldLabel
setNameFieldSelector HscEnv
hsc_env Maybe Module
mb_mod) [FieldLabel]
fs
setNameFieldSelector :: HscEnv -> Maybe Module -> FieldLabel -> IO FieldLabel
setNameFieldSelector :: HscEnv -> Maybe Module -> FieldLabel -> IO FieldLabel
setNameFieldSelector _ Nothing f :: FieldLabel
f = FieldLabel -> IO FieldLabel
forall (m :: * -> *) a. Monad m => a -> m a
return FieldLabel
f
setNameFieldSelector hsc_env :: HscEnv
hsc_env mb_mod :: Maybe Module
mb_mod (FieldLabel l :: FieldLabelString
l b :: Bool
b sel :: Name
sel) = do
Name
sel' <- HscEnv -> IfG Name -> IO Name
forall a. HscEnv -> IfG a -> IO a
initIfaceLoad HscEnv
hsc_env (IfG Name -> IO Name) -> IfG Name -> IO Name
forall a b. (a -> b) -> a -> b
$ Maybe Module -> Name -> IfG Name
forall m n. Maybe Module -> Name -> TcRnIf m n Name
setNameModule Maybe Module
mb_mod Name
sel
FieldLabel -> IO FieldLabel
forall (m :: * -> *) a. Monad m => a -> m a
return (FieldLabelString -> Bool -> Name -> FieldLabel
forall a. FieldLabelString -> Bool -> a -> FieldLbl a
FieldLabel FieldLabelString
l Bool
b Name
sel')
mergeAvails :: [AvailInfo] -> [AvailInfo] -> [AvailInfo]
mergeAvails :: [AvailInfo] -> [AvailInfo] -> [AvailInfo]
mergeAvails as1 :: [AvailInfo]
as1 as2 :: [AvailInfo]
as2 =
let mkNE :: [AvailInfo] -> NameEnv AvailInfo
mkNE as :: [AvailInfo]
as = [(Name, AvailInfo)] -> NameEnv AvailInfo
forall a. [(Name, a)] -> NameEnv a
mkNameEnv [(AvailInfo -> Name
availName AvailInfo
a, AvailInfo
a) | AvailInfo
a <- [AvailInfo]
as]
in NameEnv AvailInfo -> [AvailInfo]
forall a. NameEnv a -> [a]
nameEnvElts ((AvailInfo -> AvailInfo -> AvailInfo)
-> NameEnv AvailInfo -> NameEnv AvailInfo -> NameEnv AvailInfo
forall a. (a -> a -> a) -> NameEnv a -> NameEnv a -> NameEnv a
plusNameEnv_C AvailInfo -> AvailInfo -> AvailInfo
plusAvail ([AvailInfo] -> NameEnv AvailInfo
mkNE [AvailInfo]
as1) ([AvailInfo] -> NameEnv AvailInfo
mkNE [AvailInfo]
as2))
uAvailInfos :: ModuleName -> [AvailInfo] -> [AvailInfo] -> Either SDoc ShNameSubst
uAvailInfos :: ModuleName -> [AvailInfo] -> [AvailInfo] -> Either SDoc ShNameSubst
uAvailInfos flexi :: ModuleName
flexi as1 :: [AvailInfo]
as1 as2 :: [AvailInfo]
as2 =
let mkOE :: [AvailInfo] -> NameEnv AvailInfo
mkOE as :: [AvailInfo]
as = [(OccName, AvailInfo)] -> NameEnv AvailInfo
forall key elt. Uniquable key => [(key, elt)] -> UniqFM elt
listToUFM ([(OccName, AvailInfo)] -> NameEnv AvailInfo)
-> [(OccName, AvailInfo)] -> NameEnv AvailInfo
forall a b. (a -> b) -> a -> b
$ do AvailInfo
a <- [AvailInfo]
as
Name
n <- AvailInfo -> [Name]
availNames AvailInfo
a
(OccName, AvailInfo) -> [(OccName, AvailInfo)]
forall (m :: * -> *) a. Monad m => a -> m a
return (Name -> OccName
nameOccName Name
n, AvailInfo
a)
in (ShNameSubst -> (AvailInfo, AvailInfo) -> Either SDoc ShNameSubst)
-> ShNameSubst
-> [(AvailInfo, AvailInfo)]
-> Either SDoc ShNameSubst
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM (\subst :: ShNameSubst
subst (a1 :: AvailInfo
a1, a2 :: AvailInfo
a2) -> ModuleName
-> ShNameSubst -> AvailInfo -> AvailInfo -> Either SDoc ShNameSubst
uAvailInfo ModuleName
flexi ShNameSubst
subst AvailInfo
a1 AvailInfo
a2) ShNameSubst
forall a. NameEnv a
emptyNameEnv
(UniqFM (AvailInfo, AvailInfo) -> [(AvailInfo, AvailInfo)]
forall a. NameEnv a -> [a]
eltsUFM ((AvailInfo -> AvailInfo -> (AvailInfo, AvailInfo))
-> NameEnv AvailInfo
-> NameEnv AvailInfo
-> UniqFM (AvailInfo, AvailInfo)
forall elt1 elt2 elt3.
(elt1 -> elt2 -> elt3) -> UniqFM elt1 -> UniqFM elt2 -> UniqFM elt3
intersectUFM_C (,) ([AvailInfo] -> NameEnv AvailInfo
mkOE [AvailInfo]
as1) ([AvailInfo] -> NameEnv AvailInfo
mkOE [AvailInfo]
as2)))
uAvailInfo :: ModuleName -> ShNameSubst -> AvailInfo -> AvailInfo
-> Either SDoc ShNameSubst
uAvailInfo :: ModuleName
-> ShNameSubst -> AvailInfo -> AvailInfo -> Either SDoc ShNameSubst
uAvailInfo flexi :: ModuleName
flexi subst :: ShNameSubst
subst (Avail n1 :: Name
n1) (Avail n2 :: Name
n2) = ModuleName
-> ShNameSubst -> Name -> Name -> Either SDoc ShNameSubst
uName ModuleName
flexi ShNameSubst
subst Name
n1 Name
n2
uAvailInfo flexi :: ModuleName
flexi subst :: ShNameSubst
subst (AvailTC n1 :: Name
n1 _ _) (AvailTC n2 :: Name
n2 _ _) = ModuleName
-> ShNameSubst -> Name -> Name -> Either SDoc ShNameSubst
uName ModuleName
flexi ShNameSubst
subst Name
n1 Name
n2
uAvailInfo _ _ a1 :: AvailInfo
a1 a2 :: AvailInfo
a2 = SDoc -> Either SDoc ShNameSubst
forall a b. a -> Either a b
Left (SDoc -> Either SDoc ShNameSubst)
-> SDoc -> Either SDoc ShNameSubst
forall a b. (a -> b) -> a -> b
$ String -> SDoc
text "While merging export lists, could not combine"
SDoc -> SDoc -> SDoc
<+> AvailInfo -> SDoc
forall a. Outputable a => a -> SDoc
ppr AvailInfo
a1 SDoc -> SDoc -> SDoc
<+> String -> SDoc
text "with" SDoc -> SDoc -> SDoc
<+> AvailInfo -> SDoc
forall a. Outputable a => a -> SDoc
ppr AvailInfo
a2
SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
parens (String -> SDoc
text "one is a type, the other is a plain identifier")
uName :: ModuleName -> ShNameSubst -> Name -> Name -> Either SDoc ShNameSubst
uName :: ModuleName
-> ShNameSubst -> Name -> Name -> Either SDoc ShNameSubst
uName flexi :: ModuleName
flexi subst :: ShNameSubst
subst n1 :: Name
n1 n2 :: Name
n2
| Name
n1 Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
n2 = ShNameSubst -> Either SDoc ShNameSubst
forall a b. b -> Either a b
Right ShNameSubst
subst
| Name -> Bool
isFlexi Name
n1 = ModuleName
-> ShNameSubst -> Name -> Name -> Either SDoc ShNameSubst
uHoleName ModuleName
flexi ShNameSubst
subst Name
n1 Name
n2
| Name -> Bool
isFlexi Name
n2 = ModuleName
-> ShNameSubst -> Name -> Name -> Either SDoc ShNameSubst
uHoleName ModuleName
flexi ShNameSubst
subst Name
n2 Name
n1
| Bool
otherwise = SDoc -> Either SDoc ShNameSubst
forall a b. a -> Either a b
Left (String -> SDoc
text "While merging export lists, could not unify"
SDoc -> SDoc -> SDoc
<+> Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
n1 SDoc -> SDoc -> SDoc
<+> String -> SDoc
text "with" SDoc -> SDoc -> SDoc
<+> Name -> SDoc
forall a. Outputable a => a -> SDoc
ppr Name
n2 SDoc -> SDoc -> SDoc
$$ SDoc
extra)
where
isFlexi :: Name -> Bool
isFlexi n :: Name
n = Name -> Bool
isHoleName Name
n Bool -> Bool -> Bool
&& Module -> ModuleName
moduleName (HasDebugCallStack => Name -> Module
Name -> Module
nameModule Name
n) ModuleName -> ModuleName -> Bool
forall a. Eq a => a -> a -> Bool
== ModuleName
flexi
extra :: SDoc
extra | Name -> Bool
isHoleName Name
n1 Bool -> Bool -> Bool
|| Name -> Bool
isHoleName Name
n2
= String -> SDoc
text "Neither name variable originates from the current signature."
| Bool
otherwise
= SDoc
empty
uHoleName :: ModuleName -> ShNameSubst -> Name -> Name
-> Either SDoc ShNameSubst
uHoleName :: ModuleName
-> ShNameSubst -> Name -> Name -> Either SDoc ShNameSubst
uHoleName flexi :: ModuleName
flexi subst :: ShNameSubst
subst h :: Name
h n :: Name
n =
ASSERT( isHoleName h )
case ShNameSubst -> Name -> Maybe Name
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv ShNameSubst
subst Name
h of
Just n' :: Name
n' -> ModuleName
-> ShNameSubst -> Name -> Name -> Either SDoc ShNameSubst
uName ModuleName
flexi ShNameSubst
subst Name
n' Name
n
Nothing | Just n' :: Name
n' <- ShNameSubst -> Name -> Maybe Name
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv ShNameSubst
subst Name
n ->
ASSERT( isHoleName n ) uName flexi subst h n'
| Bool
otherwise ->
ShNameSubst -> Either SDoc ShNameSubst
forall a b. b -> Either a b
Right (ShNameSubst -> Name -> Name -> ShNameSubst
forall a. NameEnv a -> Name -> a -> NameEnv a
extendNameEnv ShNameSubst
subst Name
h Name
n)