{-# LANGUAGE ScopedTypeVariables, PatternGuards, TupleSections #-}
module Language.Fortran.Analysis.Renaming
( analyseRenames, analyseRenamesWithModuleMap, rename, unrename, ModuleMap )
where
import Language.Fortran.AST hiding (fromList)
import Language.Fortran.Intrinsics
import Language.Fortran.Analysis
import Language.Fortran.ParserMonad (FortranVersion(..))
import Prelude hiding (lookup)
import Data.Maybe (mapMaybe, fromMaybe)
import qualified Data.List as L
import Data.Map (insert, empty, lookup, Map)
import qualified Data.Map.Strict as M
import Control.Monad.State.Strict
import Data.Generics.Uniplate.Data
import Data.Data
import Data.Functor.Identity (Identity)
type ModuleMap = Map ProgramUnitName ModEnv
type Renamer a = State RenameState a
data RenameState = RenameState { RenameState -> FortranVersion
langVersion :: FortranVersion
, RenameState -> IntrinsicsTable
intrinsics :: IntrinsicsTable
, RenameState -> [String]
scopeStack :: [String]
, RenameState -> [Int]
uniqNums :: [Int]
, RenameState -> [ModEnv]
environ :: [ModEnv]
, RenameState -> ModuleMap
moduleMap :: ModuleMap }
deriving (Int -> RenameState -> ShowS
[RenameState] -> ShowS
RenameState -> String
(Int -> RenameState -> ShowS)
-> (RenameState -> String)
-> ([RenameState] -> ShowS)
-> Show RenameState
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RenameState] -> ShowS
$cshowList :: [RenameState] -> ShowS
show :: RenameState -> String
$cshow :: RenameState -> String
showsPrec :: Int -> RenameState -> ShowS
$cshowsPrec :: Int -> RenameState -> ShowS
Show, RenameState -> RenameState -> Bool
(RenameState -> RenameState -> Bool)
-> (RenameState -> RenameState -> Bool) -> Eq RenameState
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RenameState -> RenameState -> Bool
$c/= :: RenameState -> RenameState -> Bool
== :: RenameState -> RenameState -> Bool
$c== :: RenameState -> RenameState -> Bool
Eq)
type RenamerFunc t = t -> Renamer t
analyseRenames :: Data a => ProgramFile (Analysis a) -> ProgramFile (Analysis a)
analyseRenames :: ProgramFile (Analysis a) -> ProgramFile (Analysis a)
analyseRenames (ProgramFile MetaInfo
mi [ProgramUnit (Analysis a)]
pus) = ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall a.
Data a =>
ProgramFile (Analysis a) -> ProgramFile (Analysis a)
cleanupUseRenames (ProgramFile (Analysis a) -> ProgramFile (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall a b. (a -> b) -> a -> b
$ MetaInfo -> [ProgramUnit (Analysis a)] -> ProgramFile (Analysis a)
forall a. MetaInfo -> [ProgramUnit a] -> ProgramFile a
ProgramFile MetaInfo
mi [ProgramUnit (Analysis a)]
pus'
where
(Just [ProgramUnit (Analysis a)]
pus', RenameState
_) = State RenameState (Maybe [ProgramUnit (Analysis a)])
-> RenameState -> (Maybe [ProgramUnit (Analysis a)], RenameState)
forall a b. State a b -> a -> (b, a)
runRenamer (RenamerFunc (Maybe [ProgramUnit (Analysis a)])
forall a. Data a => RenamerFunc (Maybe [ProgramUnit (Analysis a)])
renameSubPUs ([ProgramUnit (Analysis a)] -> Maybe [ProgramUnit (Analysis a)]
forall a. a -> Maybe a
Just [ProgramUnit (Analysis a)]
pus)) (FortranVersion -> RenameState
renameState0 (MetaInfo -> FortranVersion
miVersion MetaInfo
mi))
analyseRenamesWithModuleMap :: Data a => ModuleMap -> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
analyseRenamesWithModuleMap :: ModuleMap -> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
analyseRenamesWithModuleMap ModuleMap
mmap (ProgramFile MetaInfo
mi [ProgramUnit (Analysis a)]
pus) = ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall a.
Data a =>
ProgramFile (Analysis a) -> ProgramFile (Analysis a)
cleanupUseRenames (ProgramFile (Analysis a) -> ProgramFile (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall a b. (a -> b) -> a -> b
$ MetaInfo -> [ProgramUnit (Analysis a)] -> ProgramFile (Analysis a)
forall a. MetaInfo -> [ProgramUnit a] -> ProgramFile a
ProgramFile MetaInfo
mi [ProgramUnit (Analysis a)]
pus'
where
(Just [ProgramUnit (Analysis a)]
pus', RenameState
_) = State RenameState (Maybe [ProgramUnit (Analysis a)])
-> RenameState -> (Maybe [ProgramUnit (Analysis a)], RenameState)
forall a b. State a b -> a -> (b, a)
runRenamer (RenamerFunc (Maybe [ProgramUnit (Analysis a)])
forall a. Data a => RenamerFunc (Maybe [ProgramUnit (Analysis a)])
renameSubPUs ([ProgramUnit (Analysis a)] -> Maybe [ProgramUnit (Analysis a)]
forall a. a -> Maybe a
Just [ProgramUnit (Analysis a)]
pus)) (FortranVersion -> RenameState
renameState0 (MetaInfo -> FortranVersion
miVersion MetaInfo
mi)) { moduleMap :: ModuleMap
moduleMap = ModuleMap
mmap }
rename :: Data a => ProgramFile (Analysis a) -> ProgramFile (Analysis a)
rename :: ProgramFile (Analysis a) -> ProgramFile (Analysis a)
rename ProgramFile (Analysis a)
pf = (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall a.
Data a =>
(ProgramUnit a -> ProgramUnit a) -> ProgramFile a -> ProgramFile a
trPU ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
forall a.
Data a =>
ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
fPU ((Expression (Analysis a) -> Expression (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall a.
Data a =>
(Expression a -> Expression a) -> ProgramFile a -> ProgramFile a
trE Expression (Analysis a) -> Expression (Analysis a)
forall a.
Data a =>
Expression (Analysis a) -> Expression (Analysis a)
fE ProgramFile (Analysis a)
pf)
where
trE :: Data a => (Expression a -> Expression a) -> ProgramFile a -> ProgramFile a
trE :: (Expression a -> Expression a) -> ProgramFile a -> ProgramFile a
trE = (Expression a -> Expression a) -> ProgramFile a -> ProgramFile a
forall from to. Biplate from to => (to -> to) -> from -> from
transformBi
fE :: Data a => Expression (Analysis a) -> Expression (Analysis a)
fE :: Expression (Analysis a) -> Expression (Analysis a)
fE (ExpValue Analysis a
a SrcSpan
s (ValVariable String
v)) = Analysis a
-> SrcSpan -> Value (Analysis a) -> Expression (Analysis a)
forall a. a -> SrcSpan -> Value a -> Expression a
ExpValue Analysis a
a SrcSpan
s (Value (Analysis a) -> Expression (Analysis a))
-> (String -> Value (Analysis a))
-> String
-> Expression (Analysis a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Value (Analysis a)
forall a. String -> Value a
ValVariable (String -> Expression (Analysis a))
-> String -> Expression (Analysis a)
forall a b. (a -> b) -> a -> b
$ String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
v (Analysis a -> Maybe String
forall a. Analysis a -> Maybe String
uniqueName Analysis a
a)
fE (ExpValue Analysis a
a SrcSpan
s (ValIntrinsic String
v)) = Analysis a
-> SrcSpan -> Value (Analysis a) -> Expression (Analysis a)
forall a. a -> SrcSpan -> Value a -> Expression a
ExpValue Analysis a
a SrcSpan
s (Value (Analysis a) -> Expression (Analysis a))
-> (String -> Value (Analysis a))
-> String
-> Expression (Analysis a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Value (Analysis a)
forall a. String -> Value a
ValIntrinsic (String -> Expression (Analysis a))
-> String -> Expression (Analysis a)
forall a b. (a -> b) -> a -> b
$ String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
v (Analysis a -> Maybe String
forall a. Analysis a -> Maybe String
uniqueName Analysis a
a)
fE Expression (Analysis a)
x = Expression (Analysis a)
x
trPU :: Data a => (ProgramUnit a -> ProgramUnit a) -> ProgramFile a -> ProgramFile a
trPU :: (ProgramUnit a -> ProgramUnit a) -> ProgramFile a -> ProgramFile a
trPU = (ProgramUnit a -> ProgramUnit a) -> ProgramFile a -> ProgramFile a
forall from to. Biplate from to => (to -> to) -> from -> from
transformBi
fPU :: Data a => ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
fPU :: ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
fPU (PUFunction Analysis a
a SrcSpan
s Maybe (TypeSpec (Analysis a))
ty PrefixSuffix (Analysis a)
r String
n Maybe (AList Expression (Analysis a))
args Maybe (Expression (Analysis a))
res [Block (Analysis a)]
b Maybe [ProgramUnit (Analysis a)]
subs) =
Analysis a
-> SrcSpan
-> Maybe (TypeSpec (Analysis a))
-> PrefixSuffix (Analysis a)
-> String
-> Maybe (AList Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> [Block (Analysis a)]
-> Maybe [ProgramUnit (Analysis a)]
-> ProgramUnit (Analysis a)
forall a.
a
-> SrcSpan
-> Maybe (TypeSpec a)
-> PrefixSuffix a
-> String
-> Maybe (AList Expression a)
-> Maybe (Expression a)
-> [Block a]
-> Maybe [ProgramUnit a]
-> ProgramUnit a
PUFunction Analysis a
a SrcSpan
s Maybe (TypeSpec (Analysis a))
ty PrefixSuffix (Analysis a)
r (String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
n (Analysis a -> Maybe String
forall a. Analysis a -> Maybe String
uniqueName Analysis a
a)) Maybe (AList Expression (Analysis a))
args Maybe (Expression (Analysis a))
res [Block (Analysis a)]
b Maybe [ProgramUnit (Analysis a)]
subs
fPU (PUSubroutine Analysis a
a SrcSpan
s PrefixSuffix (Analysis a)
r String
n Maybe (AList Expression (Analysis a))
args [Block (Analysis a)]
b Maybe [ProgramUnit (Analysis a)]
subs) =
Analysis a
-> SrcSpan
-> PrefixSuffix (Analysis a)
-> String
-> Maybe (AList Expression (Analysis a))
-> [Block (Analysis a)]
-> Maybe [ProgramUnit (Analysis a)]
-> ProgramUnit (Analysis a)
forall a.
a
-> SrcSpan
-> PrefixSuffix a
-> String
-> Maybe (AList Expression a)
-> [Block a]
-> Maybe [ProgramUnit a]
-> ProgramUnit a
PUSubroutine Analysis a
a SrcSpan
s PrefixSuffix (Analysis a)
r (String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
n (Analysis a -> Maybe String
forall a. Analysis a -> Maybe String
uniqueName Analysis a
a)) Maybe (AList Expression (Analysis a))
args [Block (Analysis a)]
b Maybe [ProgramUnit (Analysis a)]
subs
fPU ProgramUnit (Analysis a)
x = ProgramUnit (Analysis a)
x
unrename :: Data a => ProgramFile (Analysis a) -> ProgramFile (Analysis a)
unrename :: ProgramFile (Analysis a) -> ProgramFile (Analysis a)
unrename = (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall a.
Data a =>
(ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
trPU ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
forall a.
Data a =>
ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
fPU (ProgramFile (Analysis a) -> ProgramFile (Analysis a))
-> (ProgramFile (Analysis a) -> ProgramFile (Analysis a))
-> ProgramFile (Analysis a)
-> ProgramFile (Analysis a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Expression (Analysis a) -> Expression (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall a.
Data a =>
(Expression (Analysis a) -> Expression (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
trE Expression (Analysis a) -> Expression (Analysis a)
forall a.
Data a =>
Expression (Analysis a) -> Expression (Analysis a)
fE
where
trE :: Data a => (Expression (Analysis a) -> Expression (Analysis a)) -> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
trE :: (Expression (Analysis a) -> Expression (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
trE = (Expression (Analysis a) -> Expression (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall from to. Biplate from to => (to -> to) -> from -> from
transformBi
fE :: Data a => Expression (Analysis a) -> Expression (Analysis a)
fE :: Expression (Analysis a) -> Expression (Analysis a)
fE e :: Expression (Analysis a)
e@(ExpValue Analysis a
a SrcSpan
s (ValVariable String
_)) = Analysis a
-> SrcSpan -> Value (Analysis a) -> Expression (Analysis a)
forall a. a -> SrcSpan -> Value a -> Expression a
ExpValue Analysis a
a SrcSpan
s (String -> Value (Analysis a)
forall a. String -> Value a
ValVariable (Expression (Analysis a) -> String
forall a. Expression (Analysis a) -> String
srcName Expression (Analysis a)
e))
fE e :: Expression (Analysis a)
e@(ExpValue Analysis a
a SrcSpan
s (ValIntrinsic String
_)) = Analysis a
-> SrcSpan -> Value (Analysis a) -> Expression (Analysis a)
forall a. a -> SrcSpan -> Value a -> Expression a
ExpValue Analysis a
a SrcSpan
s (String -> Value (Analysis a)
forall a. String -> Value a
ValIntrinsic (Expression (Analysis a) -> String
forall a. Expression (Analysis a) -> String
srcName Expression (Analysis a)
e))
fE Expression (Analysis a)
e = Expression (Analysis a)
e
trPU :: Data a => (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)) -> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
trPU :: (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
trPU = (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall from to. Biplate from to => (to -> to) -> from -> from
transformBi
fPU :: Data a => ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
fPU :: ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
fPU (PUFunction Analysis a
a SrcSpan
s Maybe (TypeSpec (Analysis a))
ty PrefixSuffix (Analysis a)
r String
_ Maybe (AList Expression (Analysis a))
args Maybe (Expression (Analysis a))
res [Block (Analysis a)]
b Maybe [ProgramUnit (Analysis a)]
subs)
| Just String
srcN <- Analysis a -> Maybe String
forall a. Analysis a -> Maybe String
sourceName Analysis a
a = Analysis a
-> SrcSpan
-> Maybe (TypeSpec (Analysis a))
-> PrefixSuffix (Analysis a)
-> String
-> Maybe (AList Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> [Block (Analysis a)]
-> Maybe [ProgramUnit (Analysis a)]
-> ProgramUnit (Analysis a)
forall a.
a
-> SrcSpan
-> Maybe (TypeSpec a)
-> PrefixSuffix a
-> String
-> Maybe (AList Expression a)
-> Maybe (Expression a)
-> [Block a]
-> Maybe [ProgramUnit a]
-> ProgramUnit a
PUFunction Analysis a
a SrcSpan
s Maybe (TypeSpec (Analysis a))
ty PrefixSuffix (Analysis a)
r String
srcN Maybe (AList Expression (Analysis a))
args Maybe (Expression (Analysis a))
res [Block (Analysis a)]
b Maybe [ProgramUnit (Analysis a)]
subs
fPU (PUSubroutine Analysis a
a SrcSpan
s PrefixSuffix (Analysis a)
r String
_ Maybe (AList Expression (Analysis a))
args [Block (Analysis a)]
b Maybe [ProgramUnit (Analysis a)]
subs)
| Just String
srcN <- Analysis a -> Maybe String
forall a. Analysis a -> Maybe String
sourceName Analysis a
a = Analysis a
-> SrcSpan
-> PrefixSuffix (Analysis a)
-> String
-> Maybe (AList Expression (Analysis a))
-> [Block (Analysis a)]
-> Maybe [ProgramUnit (Analysis a)]
-> ProgramUnit (Analysis a)
forall a.
a
-> SrcSpan
-> PrefixSuffix a
-> String
-> Maybe (AList Expression a)
-> [Block a]
-> Maybe [ProgramUnit a]
-> ProgramUnit a
PUSubroutine Analysis a
a SrcSpan
s PrefixSuffix (Analysis a)
r String
srcN Maybe (AList Expression (Analysis a))
args [Block (Analysis a)]
b Maybe [ProgramUnit (Analysis a)]
subs
fPU ProgramUnit (Analysis a)
pu = ProgramUnit (Analysis a)
pu
programUnit :: Data a => RenamerFunc (ProgramUnit (Analysis a))
programUnit :: RenamerFunc (ProgramUnit (Analysis a))
programUnit (PUModule Analysis a
a SrcSpan
s String
name [Block (Analysis a)]
blocks Maybe [ProgramUnit (Analysis a)]
m_contains) = do
ModEnv
env0 <- [Block (Analysis a)] -> Renamer ModEnv
forall a. Data a => [Block (Analysis a)] -> Renamer ModEnv
initialEnv [Block (Analysis a)]
blocks
String -> ModEnv -> Renamer ()
pushScope String
name ModEnv
env0
[Block (Analysis a)]
blocks1 <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (f (Analysis a))
renameModDecls [Block (Analysis a)]
blocks
[Block (Analysis a)]
blocks2 <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a. Data a => RenamerFunc (Block (Analysis a))
renameUseSt [Block (Analysis a)]
blocks1
Maybe [ProgramUnit (Analysis a)]
m_contains' <- RenamerFunc (Maybe [ProgramUnit (Analysis a)])
forall a. Data a => RenamerFunc (Maybe [ProgramUnit (Analysis a)])
renameSubPUs Maybe [ProgramUnit (Analysis a)]
m_contains
[Block (Analysis a)]
blocks3 <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a. Data a => RenamerFunc (Block (Analysis a))
renameBlock [Block (Analysis a)]
blocks2
ModEnv
env <- Renamer ModEnv
getEnv
String -> ModEnv -> Renamer ()
addModEnv String
name ModEnv
env
let a' :: Analysis a
a' = Analysis a
a { moduleEnv :: Maybe ModEnv
moduleEnv = ModEnv -> Maybe ModEnv
forall a. a -> Maybe a
Just ModEnv
env }
Renamer ()
popScope
RenamerFunc (ProgramUnit (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Analysis a
-> SrcSpan
-> String
-> [Block (Analysis a)]
-> Maybe [ProgramUnit (Analysis a)]
-> ProgramUnit (Analysis a)
forall a.
a
-> SrcSpan
-> String
-> [Block a]
-> Maybe [ProgramUnit a]
-> ProgramUnit a
PUModule Analysis a
a' SrcSpan
s String
name [Block (Analysis a)]
blocks3 Maybe [ProgramUnit (Analysis a)]
m_contains')
programUnit (PUFunction Analysis a
a SrcSpan
s Maybe (TypeSpec (Analysis a))
ty PrefixSuffix (Analysis a)
rec String
name Maybe (AList Expression (Analysis a))
args Maybe (Expression (Analysis a))
res [Block (Analysis a)]
blocks Maybe [ProgramUnit (Analysis a)]
m_contains) = do
~(Just String
name') <- String -> Renamer (Maybe String)
getFromEnv String
name
([Block (Analysis a)]
blocks1, ModEnv
_) <- [Block (Analysis a)]
-> String
-> StateT RenameState Identity ([Block (Analysis a)], ModEnv)
forall a.
Data a =>
[Block (Analysis a)]
-> String
-> StateT RenameState Identity ([Block (Analysis a)], ModEnv)
returnBlocksEnv [Block (Analysis a)]
blocks String
name
[Block (Analysis a)]
blocks2 <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a. Data a => RenamerFunc (Block (Analysis a))
renameEntryPointResultDecl [Block (Analysis a)]
blocks1
Maybe (Expression (Analysis a))
res' <- (Expression (Analysis a)
-> StateT RenameState Identity (Expression (Analysis a)))
-> Maybe (Expression (Analysis a))
-> StateT RenameState Identity (Maybe (Expression (Analysis a)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Expression (Analysis a)
-> StateT RenameState Identity (Expression (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (f (Analysis a))
renameGenericDecls Maybe (Expression (Analysis a))
res
Maybe (AList Expression (Analysis a))
args' <- (AList Expression (Analysis a)
-> StateT RenameState Identity (AList Expression (Analysis a)))
-> Maybe (AList Expression (Analysis a))
-> StateT
RenameState Identity (Maybe (AList Expression (Analysis a)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM AList Expression (Analysis a)
-> StateT RenameState Identity (AList Expression (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (f (Analysis a))
renameGenericDecls Maybe (AList Expression (Analysis a))
args
[Block (Analysis a)]
blocks3 <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (f (Analysis a))
renameDeclDecls [Block (Analysis a)]
blocks2
Maybe [ProgramUnit (Analysis a)]
m_contains' <- RenamerFunc (Maybe [ProgramUnit (Analysis a)])
forall a. Data a => RenamerFunc (Maybe [ProgramUnit (Analysis a)])
renameSubPUs Maybe [ProgramUnit (Analysis a)]
m_contains
[Block (Analysis a)]
blocks4 <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a. Data a => RenamerFunc (Block (Analysis a))
renameBlock [Block (Analysis a)]
blocks3
let env :: ModEnv
env = String -> (String, NameType) -> ModEnv
forall k a. k -> a -> Map k a
M.singleton String
name (String
name', NameType
NTSubprogram)
let a' :: Analysis a
a' = Analysis a
a { moduleEnv :: Maybe ModEnv
moduleEnv = ModEnv -> Maybe ModEnv
forall a. a -> Maybe a
Just ModEnv
env }
Renamer ()
popScope
let pu' :: ProgramUnit (Analysis a)
pu' = Analysis a
-> SrcSpan
-> Maybe (TypeSpec (Analysis a))
-> PrefixSuffix (Analysis a)
-> String
-> Maybe (AList Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> [Block (Analysis a)]
-> Maybe [ProgramUnit (Analysis a)]
-> ProgramUnit (Analysis a)
forall a.
a
-> SrcSpan
-> Maybe (TypeSpec a)
-> PrefixSuffix a
-> String
-> Maybe (AList Expression a)
-> Maybe (Expression a)
-> [Block a]
-> Maybe [ProgramUnit a]
-> ProgramUnit a
PUFunction Analysis a
a' SrcSpan
s Maybe (TypeSpec (Analysis a))
ty PrefixSuffix (Analysis a)
rec String
name Maybe (AList Expression (Analysis a))
args' Maybe (Expression (Analysis a))
res' [Block (Analysis a)]
blocks4 Maybe [ProgramUnit (Analysis a)]
m_contains'
RenamerFunc (ProgramUnit (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return RenamerFunc (ProgramUnit (Analysis a))
-> (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> RenamerFunc (ProgramUnit (Analysis a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setSourceName String
name (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> ProgramUnit (Analysis a)
-> ProgramUnit (Analysis a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setUniqueName String
name' RenamerFunc (ProgramUnit (Analysis a))
-> RenamerFunc (ProgramUnit (Analysis a))
forall a b. (a -> b) -> a -> b
$ ProgramUnit (Analysis a)
pu'
programUnit (PUSubroutine Analysis a
a SrcSpan
s PrefixSuffix (Analysis a)
rec String
name Maybe (AList Expression (Analysis a))
args [Block (Analysis a)]
blocks Maybe [ProgramUnit (Analysis a)]
m_contains) = do
~(Just String
name') <- String -> Renamer (Maybe String)
getFromEnv String
name
([Block (Analysis a)]
blocks1, ModEnv
_) <- [Block (Analysis a)]
-> String
-> StateT RenameState Identity ([Block (Analysis a)], ModEnv)
forall a.
Data a =>
[Block (Analysis a)]
-> String
-> StateT RenameState Identity ([Block (Analysis a)], ModEnv)
returnBlocksEnv [Block (Analysis a)]
blocks String
name
Maybe (AList Expression (Analysis a))
args' <- (AList Expression (Analysis a)
-> StateT RenameState Identity (AList Expression (Analysis a)))
-> Maybe (AList Expression (Analysis a))
-> StateT
RenameState Identity (Maybe (AList Expression (Analysis a)))
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM AList Expression (Analysis a)
-> StateT RenameState Identity (AList Expression (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (f (Analysis a))
renameGenericDecls Maybe (AList Expression (Analysis a))
args
[Block (Analysis a)]
blocks2 <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (f (Analysis a))
renameDeclDecls [Block (Analysis a)]
blocks1
Maybe [ProgramUnit (Analysis a)]
m_contains' <- RenamerFunc (Maybe [ProgramUnit (Analysis a)])
forall a. Data a => RenamerFunc (Maybe [ProgramUnit (Analysis a)])
renameSubPUs Maybe [ProgramUnit (Analysis a)]
m_contains
[Block (Analysis a)]
blocks3 <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a. Data a => RenamerFunc (Block (Analysis a))
renameBlock [Block (Analysis a)]
blocks2
let env :: ModEnv
env = String -> (String, NameType) -> ModEnv
forall k a. k -> a -> Map k a
M.singleton String
name (String
name', NameType
NTSubprogram)
let a' :: Analysis a
a' = Analysis a
a { moduleEnv :: Maybe ModEnv
moduleEnv = ModEnv -> Maybe ModEnv
forall a. a -> Maybe a
Just ModEnv
env }
Renamer ()
popScope
let pu' :: ProgramUnit (Analysis a)
pu' = Analysis a
-> SrcSpan
-> PrefixSuffix (Analysis a)
-> String
-> Maybe (AList Expression (Analysis a))
-> [Block (Analysis a)]
-> Maybe [ProgramUnit (Analysis a)]
-> ProgramUnit (Analysis a)
forall a.
a
-> SrcSpan
-> PrefixSuffix a
-> String
-> Maybe (AList Expression a)
-> [Block a]
-> Maybe [ProgramUnit a]
-> ProgramUnit a
PUSubroutine Analysis a
a' SrcSpan
s PrefixSuffix (Analysis a)
rec String
name Maybe (AList Expression (Analysis a))
args' [Block (Analysis a)]
blocks3 Maybe [ProgramUnit (Analysis a)]
m_contains'
RenamerFunc (ProgramUnit (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return RenamerFunc (ProgramUnit (Analysis a))
-> (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> RenamerFunc (ProgramUnit (Analysis a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setSourceName String
name (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> (ProgramUnit (Analysis a) -> ProgramUnit (Analysis a))
-> ProgramUnit (Analysis a)
-> ProgramUnit (Analysis a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ProgramUnit (Analysis a) -> ProgramUnit (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setUniqueName String
name' RenamerFunc (ProgramUnit (Analysis a))
-> RenamerFunc (ProgramUnit (Analysis a))
forall a b. (a -> b) -> a -> b
$ ProgramUnit (Analysis a)
pu'
programUnit (PUMain Analysis a
a SrcSpan
s Maybe String
n [Block (Analysis a)]
blocks Maybe [ProgramUnit (Analysis a)]
m_contains) = do
ModEnv
env0 <- [Block (Analysis a)] -> Renamer ModEnv
forall a. Data a => [Block (Analysis a)] -> Renamer ModEnv
initialEnv [Block (Analysis a)]
blocks
String -> ModEnv -> Renamer ()
pushScope (String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"_main" Maybe String
n) ModEnv
env0
[Block (Analysis a)]
blocks' <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (f (Analysis a))
renameDeclDecls [Block (Analysis a)]
blocks
Maybe [ProgramUnit (Analysis a)]
m_contains' <- RenamerFunc (Maybe [ProgramUnit (Analysis a)])
forall a. Data a => RenamerFunc (Maybe [ProgramUnit (Analysis a)])
renameSubPUs Maybe [ProgramUnit (Analysis a)]
m_contains
[Block (Analysis a)]
blocks'' <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a. Data a => RenamerFunc (Block (Analysis a))
renameBlock [Block (Analysis a)]
blocks'
Renamer ()
popScope
RenamerFunc (ProgramUnit (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Analysis a
-> SrcSpan
-> Maybe String
-> [Block (Analysis a)]
-> Maybe [ProgramUnit (Analysis a)]
-> ProgramUnit (Analysis a)
forall a.
a
-> SrcSpan
-> Maybe String
-> [Block a]
-> Maybe [ProgramUnit a]
-> ProgramUnit a
PUMain Analysis a
a SrcSpan
s Maybe String
n [Block (Analysis a)]
blocks'' Maybe [ProgramUnit (Analysis a)]
m_contains')
programUnit ProgramUnit (Analysis a)
pu = RenamerFunc (ProgramUnit (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return ProgramUnit (Analysis a)
pu
returnBlocksEnv :: Data a => [Block (Analysis a)]
-> String
-> StateT RenameState Identity ([Block (Analysis a)], ModEnv)
returnBlocksEnv :: [Block (Analysis a)]
-> String
-> StateT RenameState Identity ([Block (Analysis a)], ModEnv)
returnBlocksEnv [Block (Analysis a)]
bs String
n = do
[Block (Analysis a)]
bs1 <- (Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a)))
-> [Block (Analysis a)]
-> StateT RenameState Identity [Block (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Block (Analysis a)
-> StateT RenameState Identity (Block (Analysis a))
forall a. Data a => RenamerFunc (Block (Analysis a))
renameEntryPointDecl [Block (Analysis a)]
bs
ModEnv
e0 <- [Block (Analysis a)] -> Renamer ModEnv
forall a. Data a => [Block (Analysis a)] -> Renamer ModEnv
initialEnv [Block (Analysis a)]
bs1
String -> ModEnv -> Renamer ()
pushScope String
n ModEnv
e0
([Block (Analysis a)], ModEnv)
-> StateT RenameState Identity ([Block (Analysis a)], ModEnv)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Block (Analysis a)]
bs1, ModEnv
e0)
declarator :: forall a. Data a => RenamerFunc (Declarator (Analysis a))
declarator :: RenamerFunc (Declarator (Analysis a))
declarator (DeclVariable Analysis a
a SrcSpan
s Expression (Analysis a)
e1 Maybe (Expression (Analysis a))
me2 Maybe (Expression (Analysis a))
me3) = do
Expression (Analysis a)
e1' <- RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExpDecl Expression (Analysis a)
e1
Maybe (Expression (Analysis a))
me2' <- RenamerFunc (Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> StateT RenameState Identity (Maybe (Expression (Analysis a)))
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM (RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExp :: RenamerFunc (Expression (Analysis a))) Maybe (Expression (Analysis a))
me2
Maybe (Expression (Analysis a))
me3' <- RenamerFunc (Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> StateT RenameState Identity (Maybe (Expression (Analysis a)))
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM (RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExp :: RenamerFunc (Expression (Analysis a))) Maybe (Expression (Analysis a))
me3
RenamerFunc (Declarator (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return RenamerFunc (Declarator (Analysis a))
-> RenamerFunc (Declarator (Analysis a))
forall a b. (a -> b) -> a -> b
$ Analysis a
-> SrcSpan
-> Expression (Analysis a)
-> Maybe (Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> Declarator (Analysis a)
forall a.
a
-> SrcSpan
-> Expression a
-> Maybe (Expression a)
-> Maybe (Expression a)
-> Declarator a
DeclVariable Analysis a
a SrcSpan
s Expression (Analysis a)
e1' Maybe (Expression (Analysis a))
me2' Maybe (Expression (Analysis a))
me3'
declarator (DeclArray Analysis a
a SrcSpan
s Expression (Analysis a)
e1 AList DimensionDeclarator (Analysis a)
ddAList Maybe (Expression (Analysis a))
me2 Maybe (Expression (Analysis a))
me3) = do
Expression (Analysis a)
e1' <- RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExpDecl Expression (Analysis a)
e1
AList DimensionDeclarator (Analysis a)
ddAList' <- RenamerFunc (Expression (Analysis a))
-> AList DimensionDeclarator (Analysis a)
-> StateT
RenameState Identity (AList DimensionDeclarator (Analysis a))
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM (RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExp :: RenamerFunc (Expression (Analysis a))) AList DimensionDeclarator (Analysis a)
ddAList
Maybe (Expression (Analysis a))
me2' <- RenamerFunc (Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> StateT RenameState Identity (Maybe (Expression (Analysis a)))
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM (RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExp :: RenamerFunc (Expression (Analysis a))) Maybe (Expression (Analysis a))
me2
Maybe (Expression (Analysis a))
me3' <- RenamerFunc (Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> StateT RenameState Identity (Maybe (Expression (Analysis a)))
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM (RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExp :: RenamerFunc (Expression (Analysis a))) Maybe (Expression (Analysis a))
me3
RenamerFunc (Declarator (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return RenamerFunc (Declarator (Analysis a))
-> RenamerFunc (Declarator (Analysis a))
forall a b. (a -> b) -> a -> b
$ Analysis a
-> SrcSpan
-> Expression (Analysis a)
-> AList DimensionDeclarator (Analysis a)
-> Maybe (Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> Declarator (Analysis a)
forall a.
a
-> SrcSpan
-> Expression a
-> AList DimensionDeclarator a
-> Maybe (Expression a)
-> Maybe (Expression a)
-> Declarator a
DeclArray Analysis a
a SrcSpan
s Expression (Analysis a)
e1' AList DimensionDeclarator (Analysis a)
ddAList' Maybe (Expression (Analysis a))
me2' Maybe (Expression (Analysis a))
me3'
expression :: Data a => RenamerFunc (Expression (Analysis a))
expression :: RenamerFunc (Expression (Analysis a))
expression = RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExp
renameState0 :: FortranVersion -> RenameState
renameState0 :: FortranVersion -> RenameState
renameState0 FortranVersion
v = RenameState :: FortranVersion
-> IntrinsicsTable
-> [String]
-> [Int]
-> [ModEnv]
-> ModuleMap
-> RenameState
RenameState { langVersion :: FortranVersion
langVersion = FortranVersion
v
, intrinsics :: IntrinsicsTable
intrinsics = FortranVersion -> IntrinsicsTable
getVersionIntrinsics FortranVersion
v
, scopeStack :: [String]
scopeStack = []
, uniqNums :: [Int]
uniqNums = [Int
1..]
, environ :: [ModEnv]
environ = [ModEnv
forall k a. Map k a
empty]
, moduleMap :: ModuleMap
moduleMap = ModuleMap
forall k a. Map k a
empty }
runRenamer :: State a b -> a -> (b, a)
runRenamer :: State a b -> a -> (b, a)
runRenamer = State a b -> a -> (b, a)
forall a b. State a b -> a -> (b, a)
runState
getUniqNum :: Renamer Int
getUniqNum :: Renamer Int
getUniqNum = do
Int
uniqNum <- (RenameState -> Int) -> Renamer Int
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ([Int] -> Int
forall a. [a] -> a
head ([Int] -> Int) -> (RenameState -> [Int]) -> RenameState -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenameState -> [Int]
uniqNums)
(RenameState -> RenameState) -> Renamer ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((RenameState -> RenameState) -> Renamer ())
-> (RenameState -> RenameState) -> Renamer ()
forall a b. (a -> b) -> a -> b
$ \ RenameState
s -> RenameState
s { uniqNums :: [Int]
uniqNums = Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
drop Int
1 (RenameState -> [Int]
uniqNums RenameState
s) }
Int -> Renamer Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
uniqNum
uniquify :: String -> String -> Renamer String
uniquify :: String -> String -> Renamer String
uniquify String
scope String
var = do
Int
n <- Renamer Int
getUniqNum
String -> Renamer String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> Renamer String) -> String -> Renamer String
forall a b. (a -> b) -> a -> b
$ String
scope String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"_" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
var String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n
isUseStatement :: Block a -> Bool
isUseStatement :: Block a -> Bool
isUseStatement (BlStatement a
_ SrcSpan
_ Maybe (Expression a)
_ (StUse a
_ SrcSpan
_ (ExpValue a
_ SrcSpan
_ (ValVariable String
_)) Maybe ModuleNature
_ Only
_ Maybe (AList Use a)
_)) = Bool
True
isUseStatement Block a
_ = Bool
False
initialEnv :: forall a. Data a => [Block (Analysis a)] -> Renamer ModEnv
initialEnv :: [Block (Analysis a)] -> Renamer ModEnv
initialEnv [Block (Analysis a)]
blocks = do
let uses :: [Block (Analysis a)]
uses = (Block (Analysis a) -> Bool)
-> [Block (Analysis a)] -> [Block (Analysis a)]
forall a. (a -> Bool) -> [a] -> [a]
filter Block (Analysis a) -> Bool
forall a. Block a -> Bool
isUseStatement [Block (Analysis a)]
blocks
ModuleMap
mMap <- (RenameState -> ModuleMap) -> StateT RenameState Identity ModuleMap
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets RenameState -> ModuleMap
moduleMap
ModEnv
modEnv <- ([ModEnv] -> ModEnv)
-> StateT RenameState Identity [ModEnv] -> Renamer ModEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [ModEnv] -> ModEnv
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
f (Map k a) -> Map k a
M.unions (StateT RenameState Identity [ModEnv] -> Renamer ModEnv)
-> ((Block (Analysis a) -> Renamer ModEnv)
-> StateT RenameState Identity [ModEnv])
-> (Block (Analysis a) -> Renamer ModEnv)
-> Renamer ModEnv
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Block (Analysis a)]
-> (Block (Analysis a) -> Renamer ModEnv)
-> StateT RenameState Identity [ModEnv]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [Block (Analysis a)]
uses ((Block (Analysis a) -> Renamer ModEnv) -> Renamer ModEnv)
-> (Block (Analysis a) -> Renamer ModEnv) -> Renamer ModEnv
forall a b. (a -> b) -> a -> b
$ \ Block (Analysis a)
use -> case Block (Analysis a)
use of
(BlStatement Analysis a
_ SrcSpan
_ Maybe (Expression (Analysis a))
_ (StUse Analysis a
_ SrcSpan
_ (ExpValue Analysis a
_ SrcSpan
_ (ValVariable String
m)) Maybe ModuleNature
_ Only
_ Maybe (AList Use (Analysis a))
Nothing)) ->
ModEnv -> Renamer ModEnv
forall (m :: * -> *) a. Monad m => a -> m a
return (ModEnv -> Renamer ModEnv) -> ModEnv -> Renamer ModEnv
forall a b. (a -> b) -> a -> b
$ ModEnv -> Maybe ModEnv -> ModEnv
forall a. a -> Maybe a -> a
fromMaybe ModEnv
forall k a. Map k a
empty (String -> ProgramUnitName
Named String
m ProgramUnitName -> ModuleMap -> Maybe ModEnv
forall k a. Ord k => k -> Map k a -> Maybe a
`lookup` ModuleMap
mMap)
(BlStatement Analysis a
_ SrcSpan
_ Maybe (Expression (Analysis a))
_ (StUse Analysis a
_ SrcSpan
_ (ExpValue Analysis a
_ SrcSpan
_ (ValVariable String
m)) Maybe ModuleNature
_ Only
_ (Just AList Use (Analysis a)
onlyAList)))
| [Use (Analysis a)]
only <- AList Use (Analysis a) -> [Use (Analysis a)]
forall (t :: * -> *) a. AList t a -> [t a]
aStrip AList Use (Analysis a)
onlyAList -> do
let env :: ModEnv
env = ModEnv -> Maybe ModEnv -> ModEnv
forall a. a -> Maybe a -> a
fromMaybe ModEnv
forall k a. Map k a
empty (String -> ProgramUnitName
Named String
m ProgramUnitName -> ModuleMap -> Maybe ModEnv
forall k a. Ord k => k -> Map k a -> Maybe a
`lookup` ModuleMap
mMap)
let localNamePairs :: [(String, String)]
localNamePairs = ((Use (Analysis a) -> Maybe (String, String))
-> [Use (Analysis a)] -> [(String, String)])
-> [Use (Analysis a)]
-> (Use (Analysis a) -> Maybe (String, String))
-> [(String, String)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Use (Analysis a) -> Maybe (String, String))
-> [Use (Analysis a)] -> [(String, String)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe [Use (Analysis a)]
only ((Use (Analysis a) -> Maybe (String, String))
-> [(String, String)])
-> (Use (Analysis a) -> Maybe (String, String))
-> [(String, String)]
forall a b. (a -> b) -> a -> b
$ \ Use (Analysis a)
r -> case Use (Analysis a)
r of
UseID Analysis a
_ SrcSpan
_ v :: Expression (Analysis a)
v@(ExpValue Analysis a
_ SrcSpan
_ ValVariable{}) -> (String, String) -> Maybe (String, String)
forall a. a -> Maybe a
Just (Expression (Analysis a) -> String
forall a. Expression (Analysis a) -> String
varName Expression (Analysis a)
v, Expression (Analysis a) -> String
forall a. Expression (Analysis a) -> String
varName Expression (Analysis a)
v)
UseRename Analysis a
_ SrcSpan
_ Expression (Analysis a)
u Expression (Analysis a)
v -> (String, String) -> Maybe (String, String)
forall a. a -> Maybe a
Just (Expression (Analysis a) -> String
forall a. Expression (Analysis a) -> String
varName Expression (Analysis a)
u, Expression (Analysis a) -> String
forall a. Expression (Analysis a) -> String
varName Expression (Analysis a)
v)
Use (Analysis a)
_ -> Maybe (String, String)
forall a. Maybe a
Nothing
let re :: ModEnv
re = [(String, (String, NameType))] -> ModEnv
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [ (String
local, (String, NameType)
info) | (String
local, String
orig) <- [(String, String)]
localNamePairs
, Just (String, NameType)
info <- [String -> ModEnv -> Maybe (String, NameType)
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
orig ModEnv
env] ]
ModEnv -> Renamer ModEnv
forall (m :: * -> *) a. Monad m => a -> m a
return ModEnv
re
Block (Analysis a)
_ -> ModEnv -> Renamer ModEnv
forall (m :: * -> *) a. Monad m => a -> m a
return ModEnv
forall k a. Map k a
empty
let global :: ModEnv
global = ModEnv -> Maybe ModEnv -> ModEnv
forall a. a -> Maybe a -> a
fromMaybe ModEnv
forall k a. Map k a
M.empty (Maybe ModEnv -> ModEnv) -> Maybe ModEnv -> ModEnv
forall a b. (a -> b) -> a -> b
$ ProgramUnitName -> ModuleMap -> Maybe ModEnv
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup ProgramUnitName
NamelessMain ModuleMap
mMap
let common :: ModEnv
common = [(String, (String, NameType))] -> ModEnv
forall k a. Ord k => [(k, a)] -> Map k a
M.fromList [ (String
v, (String
v', NameType
NTVariable))
| CommonGroup Analysis a
_ SrcSpan
_ Maybe (Expression (Analysis a))
me1 AList Expression (Analysis a)
alist <- [Block (Analysis a)] -> [CommonGroup (Analysis a)]
forall from to. Biplate from to => from -> [to]
universeBi [Block (Analysis a)]
blocks :: [CommonGroup (Analysis a)]
, let prefix :: String
prefix = case Maybe (Expression (Analysis a))
me1 of Just Expression (Analysis a)
e1 -> Expression (Analysis a) -> String
forall a. Expression (Analysis a) -> String
srcName Expression (Analysis a)
e1; Maybe (Expression (Analysis a))
_ -> String
""
, e :: Expression (Analysis a)
e@(ExpValue Analysis a
_ SrcSpan
_ ValVariable{}) <- [Expression (Analysis a)] -> [Expression (Analysis a)]
forall from to. Biplate from to => from -> [to]
universeBi (AList Expression (Analysis a) -> [Expression (Analysis a)]
forall (t :: * -> *) a. AList t a -> [t a]
aStrip AList Expression (Analysis a)
alist) :: [Expression (Analysis a)]
, let v :: String
v = Expression (Analysis a) -> String
forall a. Expression (Analysis a) -> String
srcName Expression (Analysis a)
e
, let v' :: String
v' = String
prefix String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"_" String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
v String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"_common" ]
ModEnv -> Renamer ModEnv
forall (m :: * -> *) a. Monad m => a -> m a
return (ModEnv -> Renamer ModEnv) -> ModEnv -> Renamer ModEnv
forall a b. (a -> b) -> a -> b
$ [ModEnv] -> ModEnv
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
f (Map k a) -> Map k a
M.unions [ModEnv
modEnv, ModEnv
global, ModEnv
common]
getScopes :: Renamer String
getScopes :: Renamer String
getScopes = (RenameState -> String) -> Renamer String
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets (String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
L.intercalate String
"_" ([String] -> String)
-> (RenameState -> [String]) -> RenameState -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [String] -> [String]
forall a. [a] -> [a]
reverse ([String] -> [String])
-> (RenameState -> [String]) -> RenameState -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenameState -> [String]
scopeStack)
pushScope :: String -> ModEnv -> Renamer ()
pushScope :: String -> ModEnv -> Renamer ()
pushScope String
name ModEnv
env0 = (RenameState -> RenameState) -> Renamer ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((RenameState -> RenameState) -> Renamer ())
-> (RenameState -> RenameState) -> Renamer ()
forall a b. (a -> b) -> a -> b
$ \ RenameState
s -> RenameState
s { scopeStack :: [String]
scopeStack = String
name String -> [String] -> [String]
forall a. a -> [a] -> [a]
: RenameState -> [String]
scopeStack RenameState
s
, environ :: [ModEnv]
environ = ModEnv
env0 ModEnv -> [ModEnv] -> [ModEnv]
forall a. a -> [a] -> [a]
: RenameState -> [ModEnv]
environ RenameState
s }
popScope :: Renamer ()
popScope :: Renamer ()
popScope = (RenameState -> RenameState) -> Renamer ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((RenameState -> RenameState) -> Renamer ())
-> (RenameState -> RenameState) -> Renamer ()
forall a b. (a -> b) -> a -> b
$ \ RenameState
s -> RenameState
s { scopeStack :: [String]
scopeStack = Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop Int
1 ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ RenameState -> [String]
scopeStack RenameState
s
, environ :: [ModEnv]
environ = Int -> [ModEnv] -> [ModEnv]
forall a. Int -> [a] -> [a]
drop Int
1 ([ModEnv] -> [ModEnv]) -> [ModEnv] -> [ModEnv]
forall a b. (a -> b) -> a -> b
$ RenameState -> [ModEnv]
environ RenameState
s }
addModEnv :: String -> ModEnv -> Renamer ()
addModEnv :: String -> ModEnv -> Renamer ()
addModEnv String
name ModEnv
env = (RenameState -> RenameState) -> Renamer ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((RenameState -> RenameState) -> Renamer ())
-> (RenameState -> RenameState) -> Renamer ()
forall a b. (a -> b) -> a -> b
$ \ RenameState
s -> RenameState
s { moduleMap :: ModuleMap
moduleMap = ProgramUnitName -> ModEnv -> ModuleMap -> ModuleMap
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert (String -> ProgramUnitName
Named String
name) ModEnv
env (RenameState -> ModuleMap
moduleMap RenameState
s) }
getEnv :: Renamer ModEnv
getEnv :: Renamer ModEnv
getEnv = (RenameState -> ModEnv) -> Renamer ModEnv
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets ([ModEnv] -> ModEnv
forall a. [a] -> a
head ([ModEnv] -> ModEnv)
-> (RenameState -> [ModEnv]) -> RenameState -> ModEnv
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenameState -> [ModEnv]
environ)
getEnvs :: Renamer ModEnv
getEnvs :: Renamer ModEnv
getEnvs = ((String, NameType) -> (String, NameType) -> (String, NameType))
-> [ModEnv] -> ModEnv
forall (f :: * -> *) k a.
(Foldable f, Ord k) =>
(a -> a -> a) -> f (Map k a) -> Map k a
M.unionsWith ((((String, NameType), (String, NameType)) -> (String, NameType))
-> (String, NameType) -> (String, NameType) -> (String, NameType)
forall a b c. ((a, b) -> c) -> a -> b -> c
curry ((String, NameType), (String, NameType)) -> (String, NameType)
forall a b. (a, b) -> a
fst) ([ModEnv] -> ModEnv)
-> StateT RenameState Identity [ModEnv] -> Renamer ModEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (RenameState -> [ModEnv]) -> StateT RenameState Identity [ModEnv]
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets RenameState -> [ModEnv]
environ
getFromEnv :: String -> Renamer (Maybe String)
getFromEnv :: String -> Renamer (Maybe String)
getFromEnv String
v = (((String, NameType) -> String
forall a b. (a, b) -> a
fst ((String, NameType) -> String)
-> Maybe (String, NameType) -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap`) (Maybe (String, NameType) -> Maybe String)
-> (ModEnv -> Maybe (String, NameType)) -> ModEnv -> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ModEnv -> Maybe (String, NameType)
forall k a. Ord k => k -> Map k a -> Maybe a
lookup String
v) (ModEnv -> Maybe String)
-> Renamer ModEnv -> Renamer (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Renamer ModEnv
getEnv
getFromEnvs :: String -> Renamer (Maybe String)
getFromEnvs :: String -> Renamer (Maybe String)
getFromEnvs = (Maybe (String, NameType) -> Maybe String)
-> StateT RenameState Identity (Maybe (String, NameType))
-> Renamer (Maybe String)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((String, NameType) -> String)
-> Maybe (String, NameType) -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String, NameType) -> String
forall a b. (a, b) -> a
fst) (StateT RenameState Identity (Maybe (String, NameType))
-> Renamer (Maybe String))
-> (String
-> StateT RenameState Identity (Maybe (String, NameType)))
-> String
-> Renamer (Maybe String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> StateT RenameState Identity (Maybe (String, NameType))
getFromEnvsWithType
getFromEnvsWithType :: String -> Renamer (Maybe (String, NameType))
getFromEnvsWithType :: String -> StateT RenameState Identity (Maybe (String, NameType))
getFromEnvsWithType String
v = do
ModEnv
envs <- Renamer ModEnv
getEnvs
case String -> ModEnv -> Maybe (String, NameType)
forall k a. Ord k => k -> Map k a -> Maybe a
lookup String
v ModEnv
envs of
Just (String
v', NameType
nt) -> Maybe (String, NameType)
-> StateT RenameState Identity (Maybe (String, NameType))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (String, NameType)
-> StateT RenameState Identity (Maybe (String, NameType)))
-> Maybe (String, NameType)
-> StateT RenameState Identity (Maybe (String, NameType))
forall a b. (a -> b) -> a -> b
$ (String, NameType) -> Maybe (String, NameType)
forall a. a -> Maybe a
Just (String
v', NameType
nt)
Maybe (String, NameType)
Nothing -> do
IntrinsicsTable
itab <- (RenameState -> IntrinsicsTable)
-> StateT RenameState Identity IntrinsicsTable
forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets RenameState -> IntrinsicsTable
intrinsics
case String -> IntrinsicsTable -> Maybe IntrinsicType
getIntrinsicReturnType String
v IntrinsicsTable
itab of
Maybe IntrinsicType
Nothing -> Maybe (String, NameType)
-> StateT RenameState Identity (Maybe (String, NameType))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (String, NameType)
forall a. Maybe a
Nothing
Just IntrinsicType
_ -> ((String, NameType) -> Maybe (String, NameType)
forall a. a -> Maybe a
Just ((String, NameType) -> Maybe (String, NameType))
-> (String -> (String, NameType))
-> String
-> Maybe (String, NameType)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (,NameType
NTIntrinsic)) (String -> Maybe (String, NameType))
-> Renamer String
-> StateT RenameState Identity (Maybe (String, NameType))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> NameType -> Renamer String
addUnique String
v NameType
NTIntrinsic
getFromEnvsIfSubprogram :: String -> Renamer (Maybe String)
getFromEnvsIfSubprogram :: String -> Renamer (Maybe String)
getFromEnvsIfSubprogram String
v = do
Maybe (String, NameType)
mEntry <- String -> StateT RenameState Identity (Maybe (String, NameType))
getFromEnvsWithType String
v
case Maybe (String, NameType)
mEntry of
Just (String
v', NameType
NTSubprogram) -> Maybe String -> Renamer (Maybe String)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe String -> Renamer (Maybe String))
-> Maybe String -> Renamer (Maybe String)
forall a b. (a -> b) -> a -> b
$ String -> Maybe String
forall a. a -> Maybe a
Just String
v'
Just (String
_, NameType
NTVariable) -> String -> Renamer (Maybe String)
getFromEnv String
v
Maybe (String, NameType)
_ -> Maybe String -> Renamer (Maybe String)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe String
forall a. Maybe a
Nothing
addToEnv :: String -> String -> NameType -> Renamer ()
addToEnv :: String -> String -> NameType -> Renamer ()
addToEnv String
v String
v' NameType
nt = (RenameState -> RenameState) -> Renamer ()
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify ((RenameState -> RenameState) -> Renamer ())
-> (RenameState -> RenameState) -> Renamer ()
forall a b. (a -> b) -> a -> b
$ \ RenameState
s -> RenameState
s { environ :: [ModEnv]
environ = String -> (String, NameType) -> ModEnv -> ModEnv
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert String
v (String
v', NameType
nt) ([ModEnv] -> ModEnv
forall a. [a] -> a
head (RenameState -> [ModEnv]
environ RenameState
s)) ModEnv -> [ModEnv] -> [ModEnv]
forall a. a -> [a] -> [a]
: Int -> [ModEnv] -> [ModEnv]
forall a. Int -> [a] -> [a]
drop Int
1 (RenameState -> [ModEnv]
environ RenameState
s) }
addUnique :: String -> NameType -> Renamer String
addUnique :: String -> NameType -> Renamer String
addUnique String
v NameType
nt = do
String
v' <- (String -> String -> Renamer String)
-> String -> String -> Renamer String
forall a b c. (a -> b -> c) -> b -> a -> c
flip String -> String -> Renamer String
uniquify String
v (String -> Renamer String) -> Renamer String -> Renamer String
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Renamer String
getScopes
String -> String -> NameType -> Renamer ()
addToEnv String
v String
v' NameType
nt
String -> Renamer String
forall (m :: * -> *) a. Monad m => a -> m a
return String
v'
addUnique_ :: String -> NameType -> Renamer ()
addUnique_ :: String -> NameType -> Renamer ()
addUnique_ String
v NameType
nt = Renamer String -> Renamer ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (String -> NameType -> Renamer String
addUnique String
v NameType
nt)
maybeAddUnique :: String -> NameType -> Renamer String
maybeAddUnique :: String -> NameType -> Renamer String
maybeAddUnique String
v NameType
nt = Renamer String
-> (String -> Renamer String) -> Maybe String -> Renamer String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> NameType -> Renamer String
addUnique String
v NameType
nt) String -> Renamer String
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe String -> Renamer String)
-> Renamer (Maybe String) -> Renamer String
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> Renamer (Maybe String)
getFromEnvsIfSubprogram String
v
setUniqueName, setSourceName :: (Annotated f, Data a) => String -> f (Analysis a) -> f (Analysis a)
setUniqueName :: String -> f (Analysis a) -> f (Analysis a)
setUniqueName String
un f (Analysis a)
x
| a :: Analysis a
a@Analysis { uniqueName :: forall a. Analysis a -> Maybe String
uniqueName = Maybe String
Nothing } <- f (Analysis a) -> Analysis a
forall (f :: * -> *) a. Annotated f => f a -> a
getAnnotation f (Analysis a)
x = Analysis a -> f (Analysis a) -> f (Analysis a)
forall (f :: * -> *) a. Annotated f => a -> f a -> f a
setAnnotation (Analysis a
a { uniqueName :: Maybe String
uniqueName = String -> Maybe String
forall a. a -> Maybe a
Just String
un }) f (Analysis a)
x
| Bool
otherwise = f (Analysis a)
x
setSourceName :: String -> f (Analysis a) -> f (Analysis a)
setSourceName String
sn f (Analysis a)
x
| a :: Analysis a
a@Analysis { sourceName :: forall a. Analysis a -> Maybe String
sourceName = Maybe String
Nothing } <- f (Analysis a) -> Analysis a
forall (f :: * -> *) a. Annotated f => f a -> a
getAnnotation f (Analysis a)
x = Analysis a -> f (Analysis a) -> f (Analysis a)
forall (f :: * -> *) a. Annotated f => a -> f a -> f a
setAnnotation (Analysis a
a { sourceName :: Maybe String
sourceName = String -> Maybe String
forall a. a -> Maybe a
Just String
sn }) f (Analysis a)
x
| Bool
otherwise = f (Analysis a)
x
renameSubPUs :: Data a => RenamerFunc (Maybe [ProgramUnit (Analysis a)])
renameSubPUs :: RenamerFunc (Maybe [ProgramUnit (Analysis a)])
renameSubPUs Maybe [ProgramUnit (Analysis a)]
Nothing = RenamerFunc (Maybe [ProgramUnit (Analysis a)])
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe [ProgramUnit (Analysis a)]
forall a. Maybe a
Nothing
renameSubPUs (Just [ProgramUnit (Analysis a)]
pus) = [ProgramUnit (Analysis a)] -> Renamer ()
forall a. Data a => [ProgramUnit (Analysis a)] -> Renamer ()
skimProgramUnits [ProgramUnit (Analysis a)]
pus Renamer ()
-> StateT RenameState Identity (Maybe [ProgramUnit (Analysis a)])
-> StateT RenameState Identity (Maybe [ProgramUnit (Analysis a)])
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [ProgramUnit (Analysis a)] -> Maybe [ProgramUnit (Analysis a)]
forall a. a -> Maybe a
Just ([ProgramUnit (Analysis a)] -> Maybe [ProgramUnit (Analysis a)])
-> StateT RenameState Identity [ProgramUnit (Analysis a)]
-> StateT RenameState Identity (Maybe [ProgramUnit (Analysis a)])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (ProgramUnit (Analysis a)
-> StateT RenameState Identity (ProgramUnit (Analysis a)))
-> [ProgramUnit (Analysis a)]
-> StateT RenameState Identity [ProgramUnit (Analysis a)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ProgramUnit (Analysis a)
-> StateT RenameState Identity (ProgramUnit (Analysis a))
forall a. Data a => RenamerFunc (ProgramUnit (Analysis a))
programUnit [ProgramUnit (Analysis a)]
pus
skimProgramUnits :: Data a => [ProgramUnit (Analysis a)] -> Renamer ()
skimProgramUnits :: [ProgramUnit (Analysis a)] -> Renamer ()
skimProgramUnits [ProgramUnit (Analysis a)]
pus = [ProgramUnit (Analysis a)]
-> (ProgramUnit (Analysis a) -> Renamer ()) -> Renamer ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [ProgramUnit (Analysis a)]
pus ((ProgramUnit (Analysis a) -> Renamer ()) -> Renamer ())
-> (ProgramUnit (Analysis a) -> Renamer ()) -> Renamer ()
forall a b. (a -> b) -> a -> b
$ \ ProgramUnit (Analysis a)
pu -> case ProgramUnit (Analysis a)
pu of
PUModule Analysis a
_ SrcSpan
_ String
name [Block (Analysis a)]
_ Maybe [ProgramUnit (Analysis a)]
_ -> String -> String -> NameType -> Renamer ()
addToEnv String
name String
name NameType
NTSubprogram
PUFunction Analysis a
_ SrcSpan
_ Maybe (TypeSpec (Analysis a))
_ PrefixSuffix (Analysis a)
_ String
name Maybe (AList Expression (Analysis a))
_ Maybe (Expression (Analysis a))
_ [Block (Analysis a)]
_ Maybe [ProgramUnit (Analysis a)]
_ -> String -> NameType -> Renamer ()
addUnique_ String
name NameType
NTSubprogram
PUSubroutine Analysis a
_ SrcSpan
_ PrefixSuffix (Analysis a)
_ String
name Maybe (AList Expression (Analysis a))
_ [Block (Analysis a)]
_ Maybe [ProgramUnit (Analysis a)]
_ -> String -> NameType -> Renamer ()
addUnique_ String
name NameType
NTSubprogram
PUMain Analysis a
_ SrcSpan
_ (Just String
name) [Block (Analysis a)]
_ Maybe [ProgramUnit (Analysis a)]
_ -> String -> String -> NameType -> Renamer ()
addToEnv String
name String
name NameType
NTSubprogram
ProgramUnit (Analysis a)
_ -> () -> Renamer ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
renameGenericDecls :: (Data a, Data (f (Analysis a))) => RenamerFunc (f (Analysis a))
renameGenericDecls :: RenamerFunc (f (Analysis a))
renameGenericDecls = RenamerFunc (Expression (Analysis a))
-> RenamerFunc (f (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (Expression (Analysis a))
-> RenamerFunc (f (Analysis a))
trans RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExpDecl
where
trans :: (Data a, Data (f (Analysis a))) => RenamerFunc (Expression (Analysis a)) -> RenamerFunc (f (Analysis a))
trans :: RenamerFunc (Expression (Analysis a))
-> RenamerFunc (f (Analysis a))
trans = RenamerFunc (Expression (Analysis a))
-> RenamerFunc (f (Analysis a))
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM
renameExpDecl :: Data a => RenamerFunc (Expression (Analysis a))
renameExpDecl :: RenamerFunc (Expression (Analysis a))
renameExpDecl e :: Expression (Analysis a)
e@(ExpValue Analysis a
_ SrcSpan
_ (ValVariable String
v)) = (String -> Expression (Analysis a) -> Expression (Analysis a))
-> Expression (Analysis a) -> String -> Expression (Analysis a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setUniqueName (String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setSourceName String
v Expression (Analysis a)
e) (String -> Expression (Analysis a))
-> Renamer String
-> StateT RenameState Identity (Expression (Analysis a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> NameType -> Renamer String
maybeAddUnique String
v NameType
NTVariable
renameExpDecl e :: Expression (Analysis a)
e@(ExpValue Analysis a
_ SrcSpan
_ (ValIntrinsic String
v)) = (String -> Expression (Analysis a) -> Expression (Analysis a))
-> Expression (Analysis a) -> String -> Expression (Analysis a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setUniqueName (String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setSourceName String
v Expression (Analysis a)
e) (String -> Expression (Analysis a))
-> Renamer String
-> StateT RenameState Identity (Expression (Analysis a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> NameType -> Renamer String
addUnique String
v NameType
NTIntrinsic
renameExpDecl Expression (Analysis a)
e = RenamerFunc (Expression (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return Expression (Analysis a)
e
renameInterfaces :: (Data a, Data (f (Analysis a))) => RenamerFunc (f (Analysis a))
renameInterfaces :: RenamerFunc (f (Analysis a))
renameInterfaces = RenamerFunc (Block (Analysis a)) -> RenamerFunc (f (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (Block (Analysis a)) -> RenamerFunc (f (Analysis a))
trans RenamerFunc (Block (Analysis a))
forall a. Data a => RenamerFunc (Block (Analysis a))
interface
where
trans :: (Data a, Data (f (Analysis a))) => RenamerFunc (Block (Analysis a)) -> RenamerFunc (f (Analysis a))
trans :: RenamerFunc (Block (Analysis a)) -> RenamerFunc (f (Analysis a))
trans = RenamerFunc (Block (Analysis a)) -> RenamerFunc (f (Analysis a))
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM
interface :: Data a => RenamerFunc (Block (Analysis a))
interface :: RenamerFunc (Block (Analysis a))
interface (BlInterface Analysis a
a SrcSpan
s (Just e :: Expression (Analysis a)
e@(ExpValue Analysis a
_ SrcSpan
_ (ValVariable String
v))) Bool
abst [ProgramUnit (Analysis a)]
pus [Block (Analysis a)]
bs) = do
Expression (Analysis a)
e' <- (String -> Expression (Analysis a) -> Expression (Analysis a))
-> Expression (Analysis a) -> String -> Expression (Analysis a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setUniqueName (String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setSourceName String
v Expression (Analysis a)
e) (String -> Expression (Analysis a))
-> Renamer String
-> StateT RenameState Identity (Expression (Analysis a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> NameType -> Renamer String
maybeAddUnique String
v NameType
NTSubprogram
RenamerFunc (Block (Analysis a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure RenamerFunc (Block (Analysis a))
-> RenamerFunc (Block (Analysis a))
forall a b. (a -> b) -> a -> b
$ Analysis a
-> SrcSpan
-> Maybe (Expression (Analysis a))
-> Bool
-> [ProgramUnit (Analysis a)]
-> [Block (Analysis a)]
-> Block (Analysis a)
forall a.
a
-> SrcSpan
-> Maybe (Expression a)
-> Bool
-> [ProgramUnit a]
-> [Block a]
-> Block a
BlInterface Analysis a
a SrcSpan
s (Expression (Analysis a) -> Maybe (Expression (Analysis a))
forall a. a -> Maybe a
Just Expression (Analysis a)
e') Bool
abst [ProgramUnit (Analysis a)]
pus [Block (Analysis a)]
bs
interface Block (Analysis a)
b = RenamerFunc (Block (Analysis a))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Block (Analysis a)
b
renameModDecls :: (Data a, Data (f (Analysis a))) => RenamerFunc (f (Analysis a))
renameModDecls :: RenamerFunc (f (Analysis a))
renameModDecls = RenamerFunc (f (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (f (Analysis a))
renameDeclDecls RenamerFunc (f (Analysis a))
-> RenamerFunc (f (Analysis a)) -> RenamerFunc (f (Analysis a))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< RenamerFunc (f (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (f (Analysis a))
renameInterfaces
renameDeclDecls :: (Data a, Data (f (Analysis a))) => RenamerFunc (f (Analysis a))
renameDeclDecls :: RenamerFunc (f (Analysis a))
renameDeclDecls = RenamerFunc (Declarator (Analysis a))
-> RenamerFunc (f (Analysis a))
forall a (f :: * -> *).
(Data a, Data (f (Analysis a))) =>
RenamerFunc (Declarator (Analysis a))
-> RenamerFunc (f (Analysis a))
trans RenamerFunc (Declarator (Analysis a))
forall a. Data a => RenamerFunc (Declarator (Analysis a))
declarator
where
trans :: (Data a, Data (f (Analysis a))) => RenamerFunc (Declarator (Analysis a)) -> RenamerFunc (f (Analysis a))
trans :: RenamerFunc (Declarator (Analysis a))
-> RenamerFunc (f (Analysis a))
trans = RenamerFunc (Declarator (Analysis a))
-> RenamerFunc (f (Analysis a))
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM
renameEntryPointDecl :: Data a => RenamerFunc (Block (Analysis a))
renameEntryPointDecl :: RenamerFunc (Block (Analysis a))
renameEntryPointDecl (BlStatement Analysis a
a SrcSpan
s Maybe (Expression (Analysis a))
l (StEntry Analysis a
a' SrcSpan
s' Expression (Analysis a)
v Maybe (AList Expression (Analysis a))
mArgs Maybe (Expression (Analysis a))
mRes)) = do
Expression (Analysis a)
v' <- RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExpDecl Expression (Analysis a)
v
RenamerFunc (Block (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Analysis a
-> SrcSpan
-> Maybe (Expression (Analysis a))
-> Statement (Analysis a)
-> Block (Analysis a)
forall a.
a -> SrcSpan -> Maybe (Expression a) -> Statement a -> Block a
BlStatement Analysis a
a SrcSpan
s Maybe (Expression (Analysis a))
l (Analysis a
-> SrcSpan
-> Expression (Analysis a)
-> Maybe (AList Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> Statement (Analysis a)
forall a.
a
-> SrcSpan
-> Expression a
-> Maybe (AList Expression a)
-> Maybe (Expression a)
-> Statement a
StEntry Analysis a
a' SrcSpan
s' Expression (Analysis a)
v' Maybe (AList Expression (Analysis a))
mArgs Maybe (Expression (Analysis a))
mRes))
renameEntryPointDecl Block (Analysis a)
b = RenamerFunc (Block (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return Block (Analysis a)
b
renameEntryPointResultDecl :: Data a => RenamerFunc (Block (Analysis a))
renameEntryPointResultDecl :: RenamerFunc (Block (Analysis a))
renameEntryPointResultDecl (BlStatement Analysis a
a SrcSpan
s Maybe (Expression (Analysis a))
l (StEntry Analysis a
a' SrcSpan
s' Expression (Analysis a)
v Maybe (AList Expression (Analysis a))
mArgs (Just Expression (Analysis a)
res))) = do
Expression (Analysis a)
res' <- RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
renameExpDecl Expression (Analysis a)
res
RenamerFunc (Block (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return (Analysis a
-> SrcSpan
-> Maybe (Expression (Analysis a))
-> Statement (Analysis a)
-> Block (Analysis a)
forall a.
a -> SrcSpan -> Maybe (Expression a) -> Statement a -> Block a
BlStatement Analysis a
a SrcSpan
s Maybe (Expression (Analysis a))
l (Analysis a
-> SrcSpan
-> Expression (Analysis a)
-> Maybe (AList Expression (Analysis a))
-> Maybe (Expression (Analysis a))
-> Statement (Analysis a)
forall a.
a
-> SrcSpan
-> Expression a
-> Maybe (AList Expression a)
-> Maybe (Expression a)
-> Statement a
StEntry Analysis a
a' SrcSpan
s' Expression (Analysis a)
v Maybe (AList Expression (Analysis a))
mArgs (Expression (Analysis a) -> Maybe (Expression (Analysis a))
forall a. a -> Maybe a
Just Expression (Analysis a)
res')))
renameEntryPointResultDecl Block (Analysis a)
b = RenamerFunc (Block (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return Block (Analysis a)
b
renameExp :: Data a => RenamerFunc (Expression (Analysis a))
renameExp :: RenamerFunc (Expression (Analysis a))
renameExp e :: Expression (Analysis a)
e@(ExpValue Analysis a
_ SrcSpan
_ (ValVariable String
v)) = Expression (Analysis a)
-> (String -> Expression (Analysis a))
-> Maybe String
-> Expression (Analysis a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Expression (Analysis a)
e (String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
`setUniqueName` String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setSourceName String
v Expression (Analysis a)
e) (Maybe String -> Expression (Analysis a))
-> Renamer (Maybe String)
-> StateT RenameState Identity (Expression (Analysis a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> Renamer (Maybe String)
getFromEnvs String
v
renameExp e :: Expression (Analysis a)
e@(ExpValue Analysis a
_ SrcSpan
_ (ValIntrinsic String
v)) = (String -> Expression (Analysis a) -> Expression (Analysis a))
-> Expression (Analysis a) -> String -> Expression (Analysis a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setUniqueName (String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setSourceName String
v Expression (Analysis a)
e) (String -> Expression (Analysis a))
-> Renamer String
-> StateT RenameState Identity (Expression (Analysis a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` String -> NameType -> Renamer String
addUnique String
v NameType
NTIntrinsic
renameExp Expression (Analysis a)
e = RenamerFunc (Expression (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return Expression (Analysis a)
e
renameBlock :: Data a => RenamerFunc (Block (Analysis a))
renameBlock :: RenamerFunc (Block (Analysis a))
renameBlock = RenamerFunc (Expression (Analysis a))
-> RenamerFunc (Block (Analysis a))
forall a.
Data a =>
RenamerFunc (Expression a) -> RenamerFunc (Block a)
trans RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
expression
where
trans :: Data a => RenamerFunc (Expression a) -> RenamerFunc (Block a)
trans :: RenamerFunc (Expression a) -> RenamerFunc (Block a)
trans = RenamerFunc (Expression a) -> RenamerFunc (Block a)
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM
renameUseSt :: Data a => RenamerFunc (Block (Analysis a))
renameUseSt :: RenamerFunc (Block (Analysis a))
renameUseSt (BlStatement Analysis a
a SrcSpan
s Maybe (Expression (Analysis a))
l st :: Statement (Analysis a)
st@StUse{}) = Analysis a
-> SrcSpan
-> Maybe (Expression (Analysis a))
-> Statement (Analysis a)
-> Block (Analysis a)
forall a.
a -> SrcSpan -> Maybe (Expression a) -> Statement a -> Block a
BlStatement Analysis a
a SrcSpan
s Maybe (Expression (Analysis a))
l (Statement (Analysis a) -> Block (Analysis a))
-> StateT RenameState Identity (Statement (Analysis a))
-> StateT RenameState Identity (Block (Analysis a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RenamerFunc (Expression (Analysis a))
-> RenamerFunc (Statement (Analysis a))
forall a.
Data a =>
RenamerFunc (Expression a) -> RenamerFunc (Statement a)
trans RenamerFunc (Expression (Analysis a))
forall a. Data a => RenamerFunc (Expression (Analysis a))
expression Statement (Analysis a)
st
where
trans :: Data a => RenamerFunc (Expression a) -> RenamerFunc (Statement a)
trans :: RenamerFunc (Expression a) -> RenamerFunc (Statement a)
trans = RenamerFunc (Expression a) -> RenamerFunc (Statement a)
forall (m :: * -> *) from to.
(Monad m, Applicative m, Biplate from to) =>
(to -> m to) -> from -> m from
transformBiM
renameUseSt Block (Analysis a)
b = RenamerFunc (Block (Analysis a))
forall (m :: * -> *) a. Monad m => a -> m a
return Block (Analysis a)
b
cleanupUseRenames :: forall a. Data a => ProgramFile (Analysis a) -> ProgramFile (Analysis a)
cleanupUseRenames :: ProgramFile (Analysis a) -> ProgramFile (Analysis a)
cleanupUseRenames = (Use (Analysis a) -> Use (Analysis a))
-> ProgramFile (Analysis a) -> ProgramFile (Analysis a)
forall from to. Biplate from to => (to -> to) -> from -> from
transformBi (\ Use (Analysis a)
u -> case Use (Analysis a)
u :: Use (Analysis a) of
UseRename Analysis a
a SrcSpan
s Expression (Analysis a)
e1 e2 :: Expression (Analysis a)
e2@(ExpValue Analysis a
_ SrcSpan
_ (ValVariable String
v)) -> Analysis a
-> SrcSpan
-> Expression (Analysis a)
-> Expression (Analysis a)
-> Use (Analysis a)
forall a. a -> SrcSpan -> Expression a -> Expression a -> Use a
UseRename Analysis a
a SrcSpan
s Expression (Analysis a)
e1 (Expression (Analysis a) -> Use (Analysis a))
-> Expression (Analysis a) -> Use (Analysis a)
forall a b. (a -> b) -> a -> b
$ String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setUniqueName (Expression (Analysis a) -> String
forall a. Expression (Analysis a) -> String
varName Expression (Analysis a)
e1) (String -> Expression (Analysis a) -> Expression (Analysis a)
forall (f :: * -> *) a.
(Annotated f, Data a) =>
String -> f (Analysis a) -> f (Analysis a)
setSourceName String
v Expression (Analysis a)
e2)
Use (Analysis a)
_ -> Use (Analysis a)
u)