1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
module Analysis.Files where
import Curry.Compiler.Distribution ( installDir )
import Control.Monad ( when, unless )
import Data.List ( isPrefixOf, isSuffixOf )
import Data.Time ( ClockTime )
import FlatCurry.Files
import FlatCurry.Goodies ( progImports )
import FlatCurry.Types ( Prog, QName )
import RW.Base ( ReadWrite )
import System.CurryPath ( currySubdir, lookupModuleSourceInLoadPath
, stripCurrySuffix )
import System.Directory
import System.FilePath
import Analysis.Logging ( DLevel, debugMessage )
import Analysis.ProgInfo
getAnalysisBaseFile :: String -> String -> IO String
getAnalysisBaseFile moduleName anaName = do
analysisDirectory <- getAnalysisDirectory
currentDir <- getCurrentDirectory >>= return . dropDrive
let modAnaName = moduleName <.> anaName
(fileDir,_) <- findModuleSourceInLoadPath moduleName
if isAbsolute fileDir
then return (analysisDirectory </> dropDrive fileDir </> modAnaName)
else return (analysisDirectory </> currentDir </> fileDir </> modAnaName)
getAnalysisPublicFile :: String -> String -> IO String
getAnalysisPublicFile modname ananame = do
getAnalysisBaseFile modname ananame >>= return . (<.> "pub")
getAnalysisDirectory :: IO String
getAnalysisDirectory = do
homedir <- getHomeDirectory
hashomedir <- doesDirectoryExist homedir
let cassStoreDir = if hashomedir then homedir else installDir
return $ cassStoreDir </> ".curry_analysis_cache" </>
joinPath (tail (splitDirectories currySubdir))
getInterfaceInfos :: (Read a, ReadWrite a) => DLevel -> String -> [String]
-> IO (ProgInfo a)
getInterfaceInfos _ _ [] = return emptyProgInfo
getInterfaceInfos dl anaName (mod:mods) =
do modInfo <- loadPublicAnalysis dl anaName mod
modsInfo <- getInterfaceInfos dl anaName mods
return (combineProgInfo modInfo modsInfo)
loadDefaultAnalysisValues :: Read a => DLevel -> String -> String
-> IO [(QName,a)]
loadDefaultAnalysisValues dl anaName moduleName = do
(_,fileName) <- findModuleSourceInLoadPath moduleName
let defaultFileName = stripCurrySuffix fileName ++ ".defaults." ++ anaName
fileExists <- doesFileExist defaultFileName
if fileExists
then do debugMessage dl 3 ("Load default values from " ++ defaultFileName)
defaultValues <- readFile defaultFileName >>= return . read
return (map (\ (f,a) -> ((moduleName,f),a)) defaultValues)
else return []
loadCompleteAnalysis :: (Read a, ReadWrite a) => DLevel -> String -> String
-> IO (ProgInfo a)
loadCompleteAnalysis dl ananame mainModule =
getAnalysisBaseFile mainModule ananame >>= readAnalysisFiles dl
loadPublicAnalysis :: (Read a, ReadWrite a) => DLevel -> String -> String
-> IO (ProgInfo a)
loadPublicAnalysis dl anaName moduleName = do
getAnalysisPublicFile moduleName anaName >>= readAnalysisPublicFile dl
storeImportModuleList :: DLevel -> String -> [String] -> IO ()
storeImportModuleList dl modname modlist = do
importListFile <- getAnalysisBaseFile modname "IMPORTLIST"
createDirectoryR dl (dropFileName importListFile)
writeFile importListFile (show modlist)
getImportModuleListFile :: String -> IO (Maybe String)
getImportModuleListFile modname = do
importListFile <- getAnalysisBaseFile modname "IMPORTLIST"
iflExists <- doesFileExist importListFile
return $ if iflExists then Just importListFile else Nothing
storeAnalysisResult :: (Show a, ReadWrite a) => DLevel -> String -> String
-> ProgInfo a -> IO ()
storeAnalysisResult dl ananame moduleName result = do
baseFileName <- getAnalysisBaseFile moduleName ananame
createDirectoryR dl (dropFileName baseFileName)
debugMessage dl 4 ("Analysis result: " ++ showProgInfo result)
writeAnalysisFiles dl baseFileName result
createDirectoryR :: DLevel -> String -> IO ()
createDirectoryR dl maindir =
let (drv,dir) = splitDrive maindir
in createDirectories drv (splitDirectories dir)
where
createDirectories _ [] = return ()
createDirectories dirname (dir:dirs) = do
let createdDir = dirname </> dir
dirExists <- doesDirectoryExist createdDir
unless dirExists $ do
debugMessage dl 3 ("Creating directory '" ++ createdDir ++ "'...")
createDirectory createdDir
createDirectories createdDir dirs
deleteAllAnalysisFiles :: String -> IO ()
deleteAllAnalysisFiles ananame = do
analysisDir <- getAnalysisDirectory
deleteAllInDir analysisDir
where
deleteAllInDir dir = do
dircont <- getDirectoryContents dir
mapM_ processDirElem (filter (not . isPrefixOf ".") dircont)
where
processDirElem f = do
let fullname = dir </> f
when (isAnaFile f) $ do
putStrLn ("DELETE: " ++ fullname)
removeFile fullname
isdir <- doesDirectoryExist fullname
when isdir $ deleteAllInDir fullname
isAnaFile f = any hasAnaSuffix [".pub",".priv",".pub.rw",".priv.rw"]
where
hasAnaSuffix suf =
suf `isSuffixOf` f &&
('.':ananame) `isSuffixOf` take (length f - length suf) f
findModuleSourceInLoadPath :: String -> IO (String,String)
findModuleSourceInLoadPath modname =
lookupModuleSourceInLoadPath modname >>=
maybe (error $ "Source file for module '"++modname++"' not found!")
return
getImports :: DLevel -> String -> IO [String]
getImports dl moduleName = do
debugMessage dl 3 $ "Reading interface of module " ++ moduleName
readNewestFlatCurryInt moduleName >>= return . progImports
getSourceFileTime :: String -> IO (String,ClockTime)
getSourceFileTime moduleName = do
(_,fileName) <- findModuleSourceInLoadPath moduleName
time <- getModificationTime fileName
return (moduleName,time)
getFlatCurryFileTime :: String -> IO (String,Maybe ClockTime)
getFlatCurryFileTime modname =
lookupFlatCurryFileInLoadPath modname >>=
maybe (return (modname, Nothing))
(\fcyFileName -> do
ftime <- getModificationTime fcyFileName
return (modname, Just ftime))
flatCurryFileNewer :: String -> IO (Maybe String)
flatCurryFileNewer modname = do
(_,sourceFileName) <- findModuleSourceInLoadPath modname
stime <- getModificationTime sourceFileName
lookupFlatCurryFileInLoadPath modname >>=
maybe (return Nothing)
(\fcyFileName -> do
itime <- getModificationTime fcyFileName
return (if itime >= stime then Just fcyFileName else Nothing))
readNewestFlatCurry :: String -> IO Prog
readNewestFlatCurry modname =
flatCurryFileNewer modname >>=
maybe (readFlatCurry modname) readFlatCurryFile
readNewestFlatCurryInt :: String -> IO Prog
readNewestFlatCurryInt modname =
flatCurryFileNewer modname >>=
maybe (readFlatCurryInt modname) (readFlatCurryFile . flat2intName)
flat2intName :: String -> String
flat2intName fn = reverse ("tnif" ++ drop 3 (reverse fn))
|