module Camfort.Specification.Stencils
(InferMode, infer, check, synth, findVarFlowCycles) where
import Control.Monad.State.Lazy
import Control.Monad.Writer hiding (Product)
import qualified Camfort.Specification.Stencils.Grammar as Gram
import Camfort.Specification.Stencils.CheckFrontend hiding (LogLine)
import Camfort.Specification.Stencils.InferenceFrontend
import Camfort.Specification.Stencils.Syntax
import Camfort.Specification.Stencils.Synthesis
import Camfort.Analysis.CommentAnnotator
import Camfort.Analysis.Annotations
import Camfort.Helpers hiding (lineCol, spanLineCol)
import qualified Language.Fortran.AST as F
import qualified Language.Fortran.Analysis as FA
import qualified Language.Fortran.Analysis.Types as FAT
import qualified Language.Fortran.Analysis.Renaming as FAR
import qualified Language.Fortran.Analysis.BBlocks as FAB
import qualified Language.Fortran.Analysis.DataFlow as FAD
import qualified Language.Fortran.Util.Position as FU
import Data.Generics.Uniplate.Operations
import Data.Data
import qualified Data.Map as M
import Data.Maybe
import Data.List
infer :: InferMode -> Filename
-> F.ProgramFile Annotation
-> (String, F.ProgramFile Annotation)
infer mode filename pf =
if null output
then ("", fmap FA.prevAnnotation pf'')
else ("\n" ++ filename ++ "\n" ++ output, fmap FA.prevAnnotation pf'')
where
output = (intercalate "\n")
. (filter (not . white))
. map (formatSpec Nothing nameMap) $ results
white = all (\x -> (x == ' ') || (x == '\t'))
(pf'', results) = (stencilInference nameMap mode) . FAB.analyseBBlocks $ pf'
nameMap = FAR.extractNameMap pf'
pf' = FAR.analyseRenames . FA.initAnalysis $ pf
synth :: InferMode
-> [(Filename, F.ProgramFile A)]
-> (String, [(Filename, F.ProgramFile Annotation)])
synth mode ps = foldr buildOutput ("", []) ps
where
buildOutput (f, pf) (r, pfs) = (r ++ r', (f, pf') : pfs)
where (r', pf') = synthPF mode f pf
synthPF :: InferMode -> Filename
-> F.ProgramFile Annotation
-> (String, F.ProgramFile Annotation)
synthPF mode filename pf =
("", fmap FA.prevAnnotation pf'')
where
(pf'', _) = (stencilInference nameMap Synth) . FAB.analyseBBlocks $ pf'
nameMap = FAR.extractNameMap pf'
pf' = FAR.analyseRenames . FA.initAnalysis $ pf
check :: Filename -> F.ProgramFile Annotation -> String
check filename pf =
if null output then "" else "\n" ++ filename ++ "\n" ++ output
where
output = intercalate "\n" results
results = (stencilChecking nameMap) . FAB.analyseBBlocks $ pf'
nameMap = FAR.extractNameMap pf'
pf' = FAR.analyseRenames . FA.initAnalysis $ pf