{-# OPTIONS -fno-warn-name-shadowing #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE NoMonoLocalBinds #-}
{-# LANGUAGE TypeFamilies #-}
module Language.Haskell.Names.Exports
( processExports
) where
import Fay.Compiler.Prelude
import Fay.Compiler.ModuleT
import Language.Haskell.Names.GlobalSymbolTable as Global
import Language.Haskell.Names.ModuleSymbols
import Language.Haskell.Names.ScopeUtils
import Language.Haskell.Names.SyntaxUtils
import Language.Haskell.Names.Types (Error (..), GName (..), ModuleNameS, NameInfo (..),
Scoped (..), Symbols (..), mkTy, mkVal, st_origName)
import Control.Monad.Writer (WriterT (WriterT), runWriterT)
import qualified Data.Map as Map
import qualified Data.Set as Set
import Language.Haskell.Exts
processExports
:: (MonadModule m, ModuleInfo m ~ Symbols, Data l, Eq l)
=> Global.Table
-> Module l
-> m (Maybe (ExportSpecList (Scoped l)), Symbols)
processExports :: Table -> Module l -> m (Maybe (ExportSpecList (Scoped l)), Symbols)
processExports Table
tbl Module l
m =
case Module l -> Maybe (ExportSpecList l)
forall l. Module l -> Maybe (ExportSpecList l)
getExportSpecList Module l
m of
Maybe (ExportSpecList l)
Nothing ->
(Maybe (ExportSpecList (Scoped l)), Symbols)
-> m (Maybe (ExportSpecList (Scoped l)), Symbols)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (ExportSpecList (Scoped l))
forall a. Maybe a
Nothing, Table -> Module l -> Symbols
forall l. (Eq l, Data l) => Table -> Module l -> Symbols
moduleSymbols Table
tbl Module l
m)
Just ExportSpecList l
exp ->
((ExportSpecList (Scoped l), Symbols)
-> (Maybe (ExportSpecList (Scoped l)), Symbols))
-> m (ExportSpecList (Scoped l), Symbols)
-> m (Maybe (ExportSpecList (Scoped l)), Symbols)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ((ExportSpecList (Scoped l) -> Maybe (ExportSpecList (Scoped l)))
-> (ExportSpecList (Scoped l), Symbols)
-> (Maybe (ExportSpecList (Scoped l)), Symbols)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first ExportSpecList (Scoped l) -> Maybe (ExportSpecList (Scoped l))
forall a. a -> Maybe a
Just) (m (ExportSpecList (Scoped l), Symbols)
-> m (Maybe (ExportSpecList (Scoped l)), Symbols))
-> m (ExportSpecList (Scoped l), Symbols)
-> m (Maybe (ExportSpecList (Scoped l)), Symbols)
forall a b. (a -> b) -> a -> b
$ Table -> ExportSpecList l -> m (ExportSpecList (Scoped l), Symbols)
forall (m :: * -> *) l.
(MonadModule m, ModuleInfo m ~ Symbols) =>
Table -> ExportSpecList l -> m (ExportSpecList (Scoped l), Symbols)
resolveExportSpecList Table
tbl ExportSpecList l
exp
resolveExportSpecList
:: (MonadModule m, ModuleInfo m ~ Symbols)
=> Global.Table
-> ExportSpecList l
-> m (ExportSpecList (Scoped l), Symbols)
resolveExportSpecList :: Table -> ExportSpecList l -> m (ExportSpecList (Scoped l), Symbols)
resolveExportSpecList Table
tbl (ExportSpecList l
l [ExportSpec l]
specs) =
(([ExportSpec (Scoped l)], Symbols)
-> (ExportSpecList (Scoped l), Symbols))
-> m ([ExportSpec (Scoped l)], Symbols)
-> m (ExportSpecList (Scoped l), Symbols)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (([ExportSpec (Scoped l)] -> ExportSpecList (Scoped l))
-> ([ExportSpec (Scoped l)], Symbols)
-> (ExportSpecList (Scoped l), Symbols)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first (([ExportSpec (Scoped l)] -> ExportSpecList (Scoped l))
-> ([ExportSpec (Scoped l)], Symbols)
-> (ExportSpecList (Scoped l), Symbols))
-> ([ExportSpec (Scoped l)] -> ExportSpecList (Scoped l))
-> ([ExportSpec (Scoped l)], Symbols)
-> (ExportSpecList (Scoped l), Symbols)
forall a b. (a -> b) -> a -> b
$ Scoped l -> [ExportSpec (Scoped l)] -> ExportSpecList (Scoped l)
forall l. l -> [ExportSpec l] -> ExportSpecList l
ExportSpecList (Scoped l -> [ExportSpec (Scoped l)] -> ExportSpecList (Scoped l))
-> Scoped l -> [ExportSpec (Scoped l)] -> ExportSpecList (Scoped l)
forall a b. (a -> b) -> a -> b
$ l -> Scoped l
forall l. l -> Scoped l
none l
l) (m ([ExportSpec (Scoped l)], Symbols)
-> m (ExportSpecList (Scoped l), Symbols))
-> m ([ExportSpec (Scoped l)], Symbols)
-> m (ExportSpecList (Scoped l), Symbols)
forall a b. (a -> b) -> a -> b
$
WriterT Symbols m [ExportSpec (Scoped l)]
-> m ([ExportSpec (Scoped l)], Symbols)
forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT (WriterT Symbols m [ExportSpec (Scoped l)]
-> m ([ExportSpec (Scoped l)], Symbols))
-> WriterT Symbols m [ExportSpec (Scoped l)]
-> m ([ExportSpec (Scoped l)], Symbols)
forall a b. (a -> b) -> a -> b
$
(ExportSpec l -> WriterT Symbols m (ExportSpec (Scoped l)))
-> [ExportSpec l] -> WriterT Symbols m [ExportSpec (Scoped l)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (m (ExportSpec (Scoped l), Symbols)
-> WriterT Symbols m (ExportSpec (Scoped l))
forall w (m :: * -> *) a. m (a, w) -> WriterT w m a
WriterT (m (ExportSpec (Scoped l), Symbols)
-> WriterT Symbols m (ExportSpec (Scoped l)))
-> (ExportSpec l -> m (ExportSpec (Scoped l), Symbols))
-> ExportSpec l
-> WriterT Symbols m (ExportSpec (Scoped l))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Table -> ExportSpec l -> m (ExportSpec (Scoped l), Symbols)
forall (m :: * -> *) l.
(MonadModule m, ModuleInfo m ~ Symbols) =>
Table -> ExportSpec l -> m (ExportSpec (Scoped l), Symbols)
resolveExportSpec Table
tbl) [ExportSpec l]
specs
resolveExportSpec
:: (MonadModule m, ModuleInfo m ~ Symbols)
=> Global.Table
-> ExportSpec l
-> m (ExportSpec (Scoped l), Symbols)
resolveExportSpec :: Table -> ExportSpec l -> m (ExportSpec (Scoped l), Symbols)
resolveExportSpec Table
tbl ExportSpec l
exp =
case ExportSpec l
exp of
EVar l
l QName l
qn -> (ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols)
forall (m :: * -> *) a. Monad m => a -> m a
return ((ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols))
-> (ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols)
forall a b. (a -> b) -> a -> b
$
case QName l -> Table -> Result l (SymValueInfo OrigName)
forall l. QName l -> Table -> Result l (SymValueInfo OrigName)
Global.lookupValue QName l
qn Table
tbl of
Global.Error Error l
err ->
(Error l -> ExportSpec l -> ExportSpec (Scoped l)
forall (f :: * -> *) l. Functor f => Error l -> f l -> f (Scoped l)
scopeError Error l
err ExportSpec l
exp, Symbols
forall a. Monoid a => a
mempty)
Global.Result SymValueInfo OrigName
i ->
let s :: Symbols
s = SymValueInfo OrigName -> Symbols
mkVal SymValueInfo OrigName
i
in
(Scoped l -> QName (Scoped l) -> ExportSpec (Scoped l)
forall l. l -> QName l -> ExportSpec l
EVar (NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (Symbols -> NameInfo l
forall l. Symbols -> NameInfo l
Export Symbols
s) l
l)
(NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (SymValueInfo OrigName -> NameInfo l
forall l. SymValueInfo OrigName -> NameInfo l
GlobalValue SymValueInfo OrigName
i) (l -> Scoped l) -> QName l -> QName (Scoped l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName l
qn), Symbols
s)
Global.Special {} -> [Char] -> (ExportSpec (Scoped l), Symbols)
forall a. HasCallStack => [Char] -> a
error [Char]
"Global.Special in export list?"
EAbs l
l Namespace l
ns QName l
qn -> (ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols)
forall (m :: * -> *) a. Monad m => a -> m a
return ((ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols))
-> (ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols)
forall a b. (a -> b) -> a -> b
$
case QName l -> Table -> Result l (SymTypeInfo OrigName)
forall l. QName l -> Table -> Result l (SymTypeInfo OrigName)
Global.lookupType QName l
qn Table
tbl of
Global.Error Error l
err ->
(Error l -> ExportSpec l -> ExportSpec (Scoped l)
forall (f :: * -> *) l. Functor f => Error l -> f l -> f (Scoped l)
scopeError Error l
err ExportSpec l
exp, Symbols
forall a. Monoid a => a
mempty)
Global.Result SymTypeInfo OrigName
i ->
let s :: Symbols
s = SymTypeInfo OrigName -> Symbols
mkTy SymTypeInfo OrigName
i
in
(Scoped l
-> Namespace (Scoped l)
-> QName (Scoped l)
-> ExportSpec (Scoped l)
forall l. l -> Namespace l -> QName l -> ExportSpec l
EAbs (NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (Symbols -> NameInfo l
forall l. Symbols -> NameInfo l
Export Symbols
s) l
l) (Namespace l -> Namespace (Scoped l)
forall (a :: * -> *) l. Annotated a => a l -> a (Scoped l)
noScope Namespace l
ns)
(NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (SymTypeInfo OrigName -> NameInfo l
forall l. SymTypeInfo OrigName -> NameInfo l
GlobalType SymTypeInfo OrigName
i) (l -> Scoped l) -> QName l -> QName (Scoped l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName l
qn), Symbols
s)
Global.Special {} -> [Char] -> (ExportSpec (Scoped l), Symbols)
forall a. HasCallStack => [Char] -> a
error [Char]
"Global.Special in export list?"
EThingWith l
l (EWildcard l
wcl Int
wcn) QName l
qn [] -> (ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols)
forall (m :: * -> *) a. Monad m => a -> m a
return ((ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols))
-> (ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols)
forall a b. (a -> b) -> a -> b
$
case QName l -> Table -> Result l (SymTypeInfo OrigName)
forall l. QName l -> Table -> Result l (SymTypeInfo OrigName)
Global.lookupType QName l
qn Table
tbl of
Global.Error Error l
err ->
(Error l -> ExportSpec l -> ExportSpec (Scoped l)
forall (f :: * -> *) l. Functor f => Error l -> f l -> f (Scoped l)
scopeError Error l
err ExportSpec l
exp, Symbols
forall a. Monoid a => a
mempty)
Global.Result SymTypeInfo OrigName
i ->
let
subs :: Symbols
subs = [Symbols] -> Symbols
forall a. Monoid a => [a] -> a
mconcat
[ SymValueInfo OrigName -> Symbols
mkVal SymValueInfo OrigName
info
| SymValueInfo OrigName
info <- [SymValueInfo OrigName]
allValueInfos
, Just OrigName
n' <- Maybe OrigName -> [Maybe OrigName]
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe OrigName -> [Maybe OrigName])
-> Maybe OrigName -> [Maybe OrigName]
forall a b. (a -> b) -> a -> b
$ SymValueInfo OrigName -> Maybe OrigName
forall n. SymValueInfo n -> Maybe n
sv_parent SymValueInfo OrigName
info
, OrigName
n' OrigName -> OrigName -> Bool
forall a. Eq a => a -> a -> Bool
== SymTypeInfo OrigName -> OrigName
forall name. SymTypeInfo name -> name
st_origName SymTypeInfo OrigName
i ]
s :: Symbols
s = SymTypeInfo OrigName -> Symbols
mkTy SymTypeInfo OrigName
i Symbols -> Symbols -> Symbols
forall a. Semigroup a => a -> a -> a
<> Symbols
subs
in
( Scoped l
-> EWildcard (Scoped l)
-> QName (Scoped l)
-> [CName (Scoped l)]
-> ExportSpec (Scoped l)
forall l. l -> EWildcard l -> QName l -> [CName l] -> ExportSpec l
EThingWith (NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (Symbols -> NameInfo l
forall l. Symbols -> NameInfo l
Export Symbols
s) l
l)
(Scoped l -> Int -> EWildcard (Scoped l)
forall l. l -> Int -> EWildcard l
EWildcard (NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (Symbols -> NameInfo l
forall l. Symbols -> NameInfo l
Export Symbols
s) l
wcl) Int
wcn)
(NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (SymTypeInfo OrigName -> NameInfo l
forall l. SymTypeInfo OrigName -> NameInfo l
GlobalType SymTypeInfo OrigName
i) (l -> Scoped l) -> QName l -> QName (Scoped l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName l
qn)
[]
, Symbols
s
)
Global.Special {} -> [Char] -> (ExportSpec (Scoped l), Symbols)
forall a. HasCallStack => [Char] -> a
error [Char]
"Global.Special in export list?"
EThingWith l
_ (EWildcard l
_ Int
_) QName l
_qn [CName l]
_cns -> [Char] -> m (ExportSpec (Scoped l), Symbols)
forall a. HasCallStack => [Char] -> a
error [Char]
"Name resolution: CNames are not supported in wildcard exports"
EThingWith l
l (NoWildcard l
wcl) QName l
qn [CName l]
cns -> (ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols)
forall (m :: * -> *) a. Monad m => a -> m a
return ((ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols))
-> (ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols)
forall a b. (a -> b) -> a -> b
$
case QName l -> Table -> Result l (SymTypeInfo OrigName)
forall l. QName l -> Table -> Result l (SymTypeInfo OrigName)
Global.lookupType QName l
qn Table
tbl of
Global.Error Error l
err ->
(Error l -> ExportSpec l -> ExportSpec (Scoped l)
forall (f :: * -> *) l. Functor f => Error l -> f l -> f (Scoped l)
scopeError Error l
err ExportSpec l
exp, Symbols
forall a. Monoid a => a
mempty)
Global.Result SymTypeInfo OrigName
i ->
let
([CName (Scoped l)]
cns', Symbols
subs) =
Symbols
-> OrigName
-> (CName l -> Error l)
-> [CName l]
-> ([CName (Scoped l)], Symbols)
forall l.
Symbols
-> OrigName
-> (CName l -> Error l)
-> [CName l]
-> ([CName (Scoped l)], Symbols)
resolveCNames
(Table -> Symbols
Global.toSymbols Table
tbl)
(SymTypeInfo OrigName -> OrigName
forall name. SymTypeInfo name -> name
st_origName SymTypeInfo OrigName
i)
(\CName l
cn -> QName l -> Error l
forall l. QName l -> Error l
ENotInScope (l -> Name l -> QName l
forall l. l -> Name l -> QName l
UnQual (CName l -> l
forall (ast :: * -> *) l. Annotated ast => ast l -> l
ann CName l
cn) (CName l -> Name l
forall l. CName l -> Name l
unCName CName l
cn)))
[CName l]
cns
s :: Symbols
s = SymTypeInfo OrigName -> Symbols
mkTy SymTypeInfo OrigName
i Symbols -> Symbols -> Symbols
forall a. Semigroup a => a -> a -> a
<> Symbols
subs
in
( Scoped l
-> EWildcard (Scoped l)
-> QName (Scoped l)
-> [CName (Scoped l)]
-> ExportSpec (Scoped l)
forall l. l -> EWildcard l -> QName l -> [CName l] -> ExportSpec l
EThingWith (NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (Symbols -> NameInfo l
forall l. Symbols -> NameInfo l
Export Symbols
s) l
l)
(Scoped l -> EWildcard (Scoped l)
forall l. l -> EWildcard l
NoWildcard (NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (Symbols -> NameInfo l
forall l. Symbols -> NameInfo l
Export Symbols
s) l
wcl))
(NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (SymTypeInfo OrigName -> NameInfo l
forall l. SymTypeInfo OrigName -> NameInfo l
GlobalType SymTypeInfo OrigName
i) (l -> Scoped l) -> QName l -> QName (Scoped l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> QName l
qn)
[CName (Scoped l)]
cns'
, Symbols
s
)
Global.Special {} -> [Char] -> (ExportSpec (Scoped l), Symbols)
forall a. HasCallStack => [Char] -> a
error [Char]
"Global.Special in export list?"
EModuleContents l
_ (ModuleName l
_ [Char]
mod) ->
let
filterByPrefix
:: Ord i
=> ModuleNameS
-> Map.Map GName (Set.Set i)
-> Set.Set i
filterByPrefix :: [Char] -> Map GName (Set i) -> Set i
filterByPrefix [Char]
prefix Map GName (Set i)
m =
[Set i] -> Set i
forall (f :: * -> *) a. (Foldable f, Ord a) => f (Set a) -> Set a
Set.unions
[ Set i
i | (GName { gModule :: GName -> [Char]
gModule = [Char]
prefix' }, Set i
i) <- Map GName (Set i) -> [(GName, Set i)]
forall k a. Map k a -> [(k, a)]
Map.toList Map GName (Set i)
m, [Char]
prefix' [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
prefix ]
filterEntities
:: Ord i
=> Map.Map GName (Set.Set i)
-> Set.Set i
filterEntities :: Map GName (Set i) -> Set i
filterEntities Map GName (Set i)
ents =
Set i -> Set i -> Set i
forall a. Ord a => Set a -> Set a -> Set a
Set.intersection
([Char] -> Map GName (Set i) -> Set i
forall i. Ord i => [Char] -> Map GName (Set i) -> Set i
filterByPrefix [Char]
mod Map GName (Set i)
ents)
([Char] -> Map GName (Set i) -> Set i
forall i. Ord i => [Char] -> Map GName (Set i) -> Set i
filterByPrefix [Char]
"" Map GName (Set i)
ents)
eVals :: Set (SymValueInfo OrigName)
eVals = Map GName (Set (SymValueInfo OrigName))
-> Set (SymValueInfo OrigName)
forall i. Ord i => Map GName (Set i) -> Set i
filterEntities (Map GName (Set (SymValueInfo OrigName))
-> Set (SymValueInfo OrigName))
-> Map GName (Set (SymValueInfo OrigName))
-> Set (SymValueInfo OrigName)
forall a b. (a -> b) -> a -> b
$ Table -> Map GName (Set (SymValueInfo OrigName))
Global.values Table
tbl
eTyps :: Set (SymTypeInfo OrigName)
eTyps = Map GName (Set (SymTypeInfo OrigName))
-> Set (SymTypeInfo OrigName)
forall i. Ord i => Map GName (Set i) -> Set i
filterEntities (Map GName (Set (SymTypeInfo OrigName))
-> Set (SymTypeInfo OrigName))
-> Map GName (Set (SymTypeInfo OrigName))
-> Set (SymTypeInfo OrigName)
forall a b. (a -> b) -> a -> b
$ Table -> Map GName (Set (SymTypeInfo OrigName))
Global.types Table
tbl
s :: Symbols
s = Set (SymValueInfo OrigName)
-> Set (SymTypeInfo OrigName) -> Symbols
Symbols Set (SymValueInfo OrigName)
eVals Set (SymTypeInfo OrigName)
eTyps
in
(ExportSpec (Scoped l), Symbols)
-> m (ExportSpec (Scoped l), Symbols)
forall (m :: * -> *) a. Monad m => a -> m a
return (NameInfo l -> l -> Scoped l
forall l. NameInfo l -> l -> Scoped l
Scoped (Symbols -> NameInfo l
forall l. Symbols -> NameInfo l
Export Symbols
s) (l -> Scoped l) -> ExportSpec l -> ExportSpec (Scoped l)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ExportSpec l
exp, Symbols
s)
where
allValueInfos :: [SymValueInfo OrigName]
allValueInfos =
Set (SymValueInfo OrigName) -> [SymValueInfo OrigName]
forall a. Set a -> [a]
Set.toList (Set (SymValueInfo OrigName) -> [SymValueInfo OrigName])
-> Set (SymValueInfo OrigName) -> [SymValueInfo OrigName]
forall a b. (a -> b) -> a -> b
$ (Set (SymValueInfo OrigName)
-> Set (SymValueInfo OrigName) -> Set (SymValueInfo OrigName))
-> Set (SymValueInfo OrigName)
-> Map GName (Set (SymValueInfo OrigName))
-> Set (SymValueInfo OrigName)
forall a b k. (a -> b -> a) -> a -> Map k b -> a
Map.foldl' Set (SymValueInfo OrigName)
-> Set (SymValueInfo OrigName) -> Set (SymValueInfo OrigName)
forall a. Ord a => Set a -> Set a -> Set a
Set.union Set (SymValueInfo OrigName)
forall a. Set a
Set.empty (Map GName (Set (SymValueInfo OrigName))
-> Set (SymValueInfo OrigName))
-> Map GName (Set (SymValueInfo OrigName))
-> Set (SymValueInfo OrigName)
forall a b. (a -> b) -> a -> b
$ Table -> Map GName (Set (SymValueInfo OrigName))
Global.values Table
tbl