|
| 1 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 2 | + |
| 3 | +-- | Generate a module use-graph. |
| 4 | +module Language.Fortran.Analysis.ModGraph |
| 5 | + (genModGraph, ModGraph(..), ModOrigin(..), modGraphToDOT, takeNextMods, delModNodes) |
| 6 | +where |
| 7 | + |
| 8 | +import Language.Fortran.AST hiding (setName) |
| 9 | +import qualified Language.Fortran.Parser as Parser |
| 10 | +import Language.Fortran.Version |
| 11 | +import Language.Fortran.Util.ModFile |
| 12 | +import Language.Fortran.Util.Files |
| 13 | + |
| 14 | +import Prelude hiding (mod) |
| 15 | +import Control.Monad |
| 16 | +import Control.Monad.State.Strict |
| 17 | +import Data.Data |
| 18 | +import Data.Generics.Uniplate.Data |
| 19 | +import Data.Graph.Inductive hiding (version) |
| 20 | +import Data.Maybe |
| 21 | +import Data.Either.Combinators ( fromRight' ) |
| 22 | +import qualified Data.ByteString.Lazy.Char8 as LB |
| 23 | +import qualified Data.Map as M |
| 24 | +import System.IO |
| 25 | +import System.FilePath |
| 26 | + |
| 27 | +-------------------------------------------------- |
| 28 | + |
| 29 | +data ModOrigin = MOFile FilePath | MOFSMod FilePath |
| 30 | + deriving (Eq, Data, Show) |
| 31 | + |
| 32 | +instance Ord ModOrigin where |
| 33 | + MOFSMod _ <= MOFSMod _ = True |
| 34 | + a <= b = a == b |
| 35 | + |
| 36 | +data ModGraph = ModGraph { mgModNodeMap :: M.Map String (Node, Maybe ModOrigin) |
| 37 | + , mgGraph :: Gr String () |
| 38 | + , mgNumNodes :: Int } |
| 39 | + deriving (Eq, Data) |
| 40 | + |
| 41 | +modGraph0 :: ModGraph |
| 42 | +modGraph0 = ModGraph M.empty empty 0 |
| 43 | + |
| 44 | +type ModGrapher a = StateT ModGraph IO a |
| 45 | + |
| 46 | +maybeAddModName :: String -> Maybe ModOrigin -> ModGrapher Node |
| 47 | +maybeAddModName modName org = do |
| 48 | + mg@ModGraph { mgModNodeMap = mnmap, mgGraph = gr, mgNumNodes = numNodes } <- get |
| 49 | + case M.lookup modName mnmap of |
| 50 | + Just (i, org') | org <= org' -> pure i |
| 51 | + | otherwise -> do |
| 52 | + let mnmap' = M.insert modName (i, org) mnmap |
| 53 | + put $ mg { mgModNodeMap = mnmap' } |
| 54 | + pure i |
| 55 | + Nothing -> do |
| 56 | + let i = numNodes + 1 |
| 57 | + let mnmap' = M.insert modName (i, org) mnmap |
| 58 | + let gr' = insNode (i, modName) gr |
| 59 | + put $ mg { mgModNodeMap = mnmap', mgGraph = gr', mgNumNodes = i } |
| 60 | + pure i |
| 61 | + |
| 62 | +addModDep :: String -> String -> ModGrapher () |
| 63 | +addModDep modName depName = do |
| 64 | + i <- maybeAddModName modName Nothing |
| 65 | + j <- maybeAddModName depName Nothing |
| 66 | + mg@ModGraph { mgGraph = gr } <- get |
| 67 | + put $ mg { mgGraph = insEdge (i, j, ()) gr } |
| 68 | + |
| 69 | +genModGraph :: Maybe FortranVersion -> [FilePath] -> [FilePath] -> IO ModGraph |
| 70 | +genModGraph mversion includeDirs paths = do |
| 71 | + let perModule path pu@(PUModule _ _ modName _ _) = do |
| 72 | + _ <- maybeAddModName modName (Just $ MOFile path) |
| 73 | + let uses = [ usedName | StUse _ _ (ExpValue _ _ (ValVariable usedName)) _ _ _ <- |
| 74 | + universeBi pu :: [Statement ()] ] |
| 75 | + forM_ uses $ \ usedName -> do |
| 76 | + _ <- maybeAddModName usedName Nothing |
| 77 | + addModDep modName usedName |
| 78 | + perModule path pu | Named puName <- getName pu = do |
| 79 | + _ <- maybeAddModName puName (Just $ MOFile path) |
| 80 | + let uses = [ usedName | StUse _ _ (ExpValue _ _ (ValVariable usedName)) _ _ _ <- |
| 81 | + universeBi pu :: [Statement ()] ] |
| 82 | + forM_ uses $ \ usedName -> do |
| 83 | + _ <- maybeAddModName usedName Nothing |
| 84 | + addModDep puName usedName |
| 85 | + perModule _ _ = pure () |
| 86 | + let iter :: FilePath -> ModGrapher () |
| 87 | + iter path = do |
| 88 | + contents <- liftIO $ flexReadFile path |
| 89 | + fileMods <- liftIO $ decodeModFiles includeDirs |
| 90 | + let version = fromMaybe (deduceFortranVersion path) mversion |
| 91 | + mods = map snd fileMods |
| 92 | + parserF0 = Parser.byVerWithMods mods version |
| 93 | + parserF fn bs = fromRight' $ parserF0 fn bs |
| 94 | + forM_ fileMods $ \ (fileName, mod) -> do |
| 95 | + forM_ [ name | Named name <- M.keys (combinedModuleMap [mod]) ] $ \ name -> do |
| 96 | + _ <- maybeAddModName name . Just $ MOFSMod fileName |
| 97 | + pure () |
| 98 | + let pf = parserF path contents |
| 99 | + mapM_ (perModule path) (childrenBi pf :: [ProgramUnit ()]) |
| 100 | + pure () |
| 101 | + execStateT (mapM_ iter paths) modGraph0 |
| 102 | + |
| 103 | +modGraphToDOT :: ModGraph -> String |
| 104 | +modGraphToDOT ModGraph { mgGraph = gr } = unlines dot |
| 105 | + where |
| 106 | + dot = [ "strict digraph {\n" |
| 107 | + , "node [shape=box,fontname=\"Courier New\"]\n" ] ++ |
| 108 | + concatMap (\ (i, name) -> |
| 109 | + [ "n" ++ show i ++ "[label=\"" ++ name ++ "\"]\n" |
| 110 | + , "n" ++ show i ++ " -> {" ] ++ |
| 111 | + [ " n" ++ show j | j <- suc gr i ] ++ |
| 112 | + ["}\n"]) |
| 113 | + (labNodes gr) ++ |
| 114 | + [ "}\n" ] |
| 115 | + |
| 116 | +takeNextMods :: ModGraph -> [(Node, Maybe ModOrigin)] |
| 117 | +takeNextMods ModGraph { mgModNodeMap = mnmap, mgGraph = gr } = noDepFiles |
| 118 | + where |
| 119 | + noDeps = [ (i, modName) | (i, modName) <- labNodes gr, null (suc gr i) ] |
| 120 | + noDepFiles = [ (i, mo) | (i, modName) <- noDeps |
| 121 | + , (_, mo) <- maybeToList (M.lookup modName mnmap) ] |
| 122 | + |
| 123 | +delModNodes :: [Node] -> ModGraph -> ModGraph |
| 124 | +delModNodes ns mg@ModGraph { mgGraph = gr } = mg' |
| 125 | + where |
| 126 | + mg' = mg { mgGraph = delNodes ns gr } |
| 127 | + |
| 128 | +-------------------------------------------------- |
| 129 | + |
| 130 | +decodeModFiles :: [FilePath] -> IO [(FilePath, ModFile)] |
| 131 | +decodeModFiles = foldM (\ modFiles d -> do |
| 132 | + -- Figure out the camfort mod files and parse them. |
| 133 | + modFileNames <- filter isModFile `fmap` getDirContents d |
| 134 | + addedModFiles <- fmap concat . forM modFileNames $ \ modFileName -> do |
| 135 | + contents <- LB.readFile (d </> modFileName) |
| 136 | + case decodeModFile contents of |
| 137 | + Left msg -> do |
| 138 | + hPutStrLn stderr $ modFileName ++ ": Error: " ++ msg |
| 139 | + return [(modFileName, emptyModFile)] |
| 140 | + Right mods -> do |
| 141 | + hPutStrLn stderr $ modFileName ++ ": successfully parsed precompiled file." |
| 142 | + return $ map (modFileName,) mods |
| 143 | + return $ addedModFiles ++ modFiles |
| 144 | + ) [] -- can't use emptyModFiles |
| 145 | + |
| 146 | +isModFile :: FilePath -> Bool |
| 147 | +isModFile = (== modFileSuffix) . takeExtension |
0 commit comments