module Cryptol.ModuleSystem.Renamer.ImplicitImports
( addImplicitNestedImports
) where
import Data.List(partition)
import Cryptol.Parser.Position(Range)
import Cryptol.Utils.Ident(packModName)
import Cryptol.Parser.AST
addImplicitNestedImports :: [TopDecl PName] -> [TopDecl PName]
addImplicitNestedImports :: [TopDecl PName] -> [TopDecl PName]
addImplicitNestedImports = forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TopDecl PName] -> ([[Ident]], [TopDecl PName])
addImplicitNestedImports'
addImplicitNestedImports' ::
[TopDecl PName] -> ([[Ident]], [TopDecl PName])
addImplicitNestedImports' :: [TopDecl PName] -> ([[Ident]], [TopDecl PName])
addImplicitNestedImports' [TopDecl PName]
decls =
(forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[[Ident]]]
exportedMods, forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[TopDecl PName]]
newDecls forall a. [a] -> [a] -> [a]
++ [TopDecl PName]
other)
where
([TopDecl PName]
mods,[TopDecl PName]
other) = forall a. (a -> Bool) -> [a] -> ([a], [a])
partition forall name. TopDecl name -> Bool
isNestedMod [TopDecl PName]
decls
([[TopDecl PName]]
newDecls,[[[Ident]]]
exportedMods) = forall a b. [(a, b)] -> ([a], [b])
unzip (forall a b. (a -> b) -> [a] -> [b]
map TopDecl PName -> ([TopDecl PName], [[Ident]])
processModule [TopDecl PName]
mods)
processModule :: TopDecl PName -> ([TopDecl PName], [[Ident]])
processModule :: TopDecl PName -> ([TopDecl PName], [[Ident]])
processModule ~dcl :: TopDecl PName
dcl@(DModule TopLevel (NestedModule PName)
m) =
let NestedModule ModuleG PName PName
m1 = forall a. TopLevel a -> a
tlValue TopLevel (NestedModule PName)
m
in
case forall mname name. ModuleG mname name -> ModuleDefinition name
mDef ModuleG PName PName
m1 of
NormalModule [TopDecl PName]
ds ->
let ([[Ident]]
childExs, [TopDecl PName]
ds1) = [TopDecl PName] -> ([[Ident]], [TopDecl PName])
addImplicitNestedImports' [TopDecl PName]
ds
mname :: Ident
mname = PName -> Ident
getIdent (forall a. Located a -> a
thing (forall mname name. ModuleG mname name -> Located mname
mName ModuleG PName PName
m1))
imps :: [[Ident]]
imps = forall a b. (a -> b) -> [a] -> [b]
map (Ident
mname forall a. a -> [a] -> [a]
:) ([] forall a. a -> [a] -> [a]
: [[Ident]]
childExs)
loc :: Range
loc = forall a. Located a -> Range
srcRange (forall mname name. ModuleG mname name -> Located mname
mName ModuleG PName PName
m1)
in ( forall name. TopLevel (NestedModule name) -> TopDecl name
DModule TopLevel (NestedModule PName)
m { tlValue :: NestedModule PName
tlValue = forall name. ModuleG name name -> NestedModule name
NestedModule ModuleG PName PName
m1 { mDef :: ModuleDefinition PName
mDef = forall name. [TopDecl name] -> ModuleDefinition name
NormalModule [TopDecl PName]
ds1 } }
forall a. a -> [a] -> [a]
: forall a b. (a -> b) -> [a] -> [b]
map (Range -> [Ident] -> TopDecl PName
mkImp Range
loc) [[Ident]]
imps
, case forall a. TopLevel a -> ExportType
tlExport TopLevel (NestedModule PName)
m of
ExportType
Public -> [[Ident]]
imps
ExportType
Private -> []
)
FunctorInstance {} -> ([TopDecl PName
dcl], [])
InterfaceModule {} -> ([TopDecl PName
dcl], [])
isNestedMod :: TopDecl name -> Bool
isNestedMod :: forall name. TopDecl name -> Bool
isNestedMod TopDecl name
d =
case TopDecl name
d of
DModule TopLevel (NestedModule name)
tl -> case forall a. TopLevel a -> a
tlValue TopLevel (NestedModule name)
tl of
NestedModule ModuleG name name
m -> Bool -> Bool
not (forall mname nmae. ModuleG mname nmae -> Bool
mIsFunctor ModuleG name name
m)
TopDecl name
_ -> Bool
False
isToQual :: [Ident] -> ModName
isToQual :: [Ident] -> ModName
isToQual [Ident]
is = [Text] -> ModName
packModName (forall a b. (a -> b) -> [a] -> [b]
map Ident -> Text
identText [Ident]
is)
isToName :: [Ident] -> PName
isToName :: [Ident] -> PName
isToName [Ident]
is = case [Ident]
is of
[Ident
i] -> Ident -> PName
mkUnqual Ident
i
[Ident]
_ -> ModName -> Ident -> PName
mkQual ([Ident] -> ModName
isToQual (forall a. [a] -> [a]
init [Ident]
is)) (forall a. [a] -> a
last [Ident]
is)
mkImp :: Range -> [Ident] -> TopDecl PName
mkImp :: Range -> [Ident] -> TopDecl PName
mkImp Range
loc [Ident]
xs =
forall name. Located (ImportG (ImpName name)) -> TopDecl name
DImport
Located
{ srcRange :: Range
srcRange = Range
loc
, thing :: ImportG (ImpName PName)
thing = Import
{ iModule :: ImpName PName
iModule = forall name. name -> ImpName name
ImpNested ([Ident] -> PName
isToName [Ident]
xs)
, iAs :: Maybe ModName
iAs = forall a. a -> Maybe a
Just ([Ident] -> ModName
isToQual [Ident]
xs)
, iSpec :: Maybe ImportSpec
iSpec = forall a. Maybe a
Nothing
, iInst :: Maybe (ModuleInstanceArgs PName)
iInst = forall a. Maybe a
Nothing
}
}