1
+ ||| Reading and writing 'Defs' from/to a binary file. In order to be saved, a
2
+ ||| name must have been flagged using 'toSave'. (Otherwise we'd save out
3
+ ||| everything, not just the things in the current file).
1
4
module Core.Binary
2
5
3
6
import Core.CaseTree
@@ -12,28 +15,23 @@ import Core.TT
12
15
import Core.TTC
13
16
import Core.UnifyState
14
17
15
- import Libraries. Data.IntMap
18
+ import Data.Buffer
16
19
import Data.List
17
- import Libraries.Data.NameMap
18
20
19
21
import System.File
20
22
21
- -- Reading and writing 'Defs' from/to a binary file
22
- -- In order to be saved, a name must have been flagged using 'toSave'.
23
- -- (Otherwise we'd save out everything, not just the things in the current
24
- -- file).
23
+ import Libraries.Data.IntMap
24
+ import Libraries.Data.NameMap
25
25
26
26
import public Libraries . Utils . Binary
27
27
28
- import Data.Buffer
29
-
30
28
%default covering
31
29
32
- -- increment this when changing anything in the data format
33
- -- TTC files can only be compatible if the version number is the same
30
+ ||| TTC files can only be compatible if the version number is the same
31
+ ||| (Increment this when changing anything in the data format)
34
32
export
35
33
ttcVersion : Int
36
- ttcVersion = 55
34
+ ttcVersion = 56
37
35
38
36
export
39
37
checkTTCVersion : String -> Int -> Int -> Core ()
@@ -43,6 +41,7 @@ checkTTCVersion file ver exp
43
41
record TTCFile extra where
44
42
constructor MkTTCFile
45
43
version : Int
44
+ sourceHash : String
46
45
ifaceHash : Int
47
46
importHashes : List (Namespace, Int)
48
47
context : List (Name, Binary)
@@ -93,14 +92,14 @@ HasNames (Name, Name, Bool) where
93
92
resolved c (n1, n2, b) = pure (! (resolved c n1), ! (resolved c n2), b)
94
93
95
94
HasNames e => HasNames (TTCFile e) where
96
- full gam (MkTTCFile version ifaceHash iHashes
95
+ full gam (MkTTCFile version sourceHash ifaceHash iHashes
97
96
context userHoles
98
97
autoHints typeHints
99
98
imported nextVar currentNS nestedNS
100
99
pairnames rewritenames primnames
101
100
namedirectives cgdirectives trans
102
101
extra)
103
- = pure $ MkTTCFile version ifaceHash iHashes
102
+ = pure $ MkTTCFile version sourceHash ifaceHash iHashes
104
103
context userHoles
105
104
! (traverse (full gam) autoHints)
106
105
! (traverse (full gam) typeHints)
@@ -131,14 +130,14 @@ HasNames e => HasNames (TTCFile e) where
131
130
-- I don't think we ever actually want to call this, because after we read
132
131
-- from the file we're going to add them to learn what the resolved names
133
132
-- are supposed to be! But for completeness, let's do it right.
134
- resolved gam (MkTTCFile version ifaceHash iHashes
133
+ resolved gam (MkTTCFile version sourceHash ifaceHash iHashes
135
134
context userHoles
136
135
autoHints typeHints
137
136
imported nextVar currentNS nestedNS
138
137
pairnames rewritenames primnames
139
138
namedirectives cgdirectives trans
140
139
extra)
141
- = pure $ MkTTCFile version ifaceHash iHashes
140
+ = pure $ MkTTCFile version sourceHash ifaceHash iHashes
142
141
context userHoles
143
142
! (traverse (resolved gam) autoHints)
144
143
! (traverse (resolved gam) typeHints)
@@ -177,6 +176,7 @@ writeTTCFile b file_in
177
176
= do file <- toFullNames file_in
178
177
toBuf b " TT2"
179
178
toBuf @{Wasteful } b (version file)
179
+ toBuf b (sourceHash file)
180
180
toBuf b (ifaceHash file)
181
181
toBuf b (importHashes file)
182
182
toBuf b (imported file)
@@ -205,12 +205,13 @@ readTTCFile readall file as b
205
205
when (hdr /= " TT2" ) $ corrupt (" TTC header in " ++ file ++ " " ++ show hdr)
206
206
ver <- fromBuf @{Wasteful } b
207
207
checkTTCVersion file ver ttcVersion
208
+ sourceFileHash <- fromBuf b
208
209
ifaceHash <- fromBuf b
209
210
importHashes <- fromBuf b
210
211
imp <- fromBuf b
211
212
ex <- fromBuf b
212
213
if not readall
213
- then pure (MkTTCFile ver ifaceHash importHashes [] [] [] [] []
214
+ then pure (MkTTCFile ver sourceFileHash ifaceHash importHashes [] [] [] [] []
214
215
0 (mkNamespace " " ) [] Nothing
215
216
Nothing
216
217
(MkPrimNs Nothing Nothing Nothing Nothing )
@@ -229,7 +230,7 @@ readTTCFile readall file as b
229
230
nds <- fromBuf b
230
231
cgds <- fromBuf b
231
232
trans <- fromBuf b
232
- pure (MkTTCFile ver ifaceHash importHashes
233
+ pure (MkTTCFile ver sourceFileHash ifaceHash importHashes
233
234
(map (replaceNS cns) defs) uholes
234
235
autohs typehs imp nextv cns nns
235
236
pns rws prims nds cgds trans ex)
@@ -265,15 +266,17 @@ export
265
266
writeToTTC : (HasNames extra , TTC extra ) =>
266
267
{auto c : Ref Ctxt Defs} ->
267
268
{auto u : Ref UST UState} ->
268
- extra -> (fname : String) -> Core ()
269
- writeToTTC extradata fname
269
+ extra -> (sourceFileName : String) ->
270
+ (ttcFileName : String) -> Core ()
271
+ writeToTTC extradata sourceFileName ttcFileName
270
272
= do bin <- initBinary
271
273
defs <- get Ctxt
272
274
ust <- get UST
273
275
gdefs <- getSaveDefs (currentNS defs) (keys (toSave defs)) [] defs
274
- log " ttc.write" 5 $ " Writing " ++ fname ++ " with hash " ++ show (ifaceHash defs)
276
+ sourceHash <- hashFile sourceFileName
277
+ log " ttc.write" 5 $ " Writing " ++ ttcFileName ++ " with source hash " ++ sourceHash ++ " and interface hash " ++ show (ifaceHash defs)
275
278
writeTTCFile bin
276
- (MkTTCFile ttcVersion (ifaceHash defs) (importHashes defs)
279
+ (MkTTCFile ttcVersion (sourceHash) ( ifaceHash defs) (importHashes defs)
277
280
gdefs
278
281
(keys (userHoles defs))
279
282
(saveAutoHints defs)
@@ -290,8 +293,8 @@ writeToTTC extradata fname
290
293
(saveTransforms defs)
291
294
extradata)
292
295
293
- Right ok <- coreLift $ writeToFile fname ! (get Bin )
294
- | Left err => throw (InternalError (fname ++ " : " ++ show err))
296
+ Right ok <- coreLift $ writeToFile ttcFileName ! (get Bin )
297
+ | Left err => throw (InternalError (ttcFileName ++ " : " ++ show err))
295
298
pure ()
296
299
297
300
addGlobalDef : {auto c : Ref Ctxt Defs} ->
@@ -497,27 +500,31 @@ getImportHashes file b
497
500
when (hdr /= " TT2" ) $ corrupt (" TTC header in " ++ file ++ " " ++ show hdr)
498
501
ver <- fromBuf @{Wasteful } b
499
502
checkTTCVersion file ver ttcVersion
500
- ifaceHash <- fromBuf {a = Int } b
503
+ sourceFileHash <- fromBuf {a = String } b
504
+ interfaceHash <- fromBuf {a = Int } b
501
505
fromBuf b
502
506
503
- getHash : String -> Ref Bin Binary -> Core Int
504
- getHash file b
507
+ export
508
+ getHashes : String -> Ref Bin Binary -> Core (String, Int)
509
+ getHashes file b
505
510
= do hdr <- fromBuf {a = String } b
506
511
when (hdr /= " TT2" ) $ corrupt (" TTC header in " ++ file ++ " " ++ show hdr)
507
512
ver <- fromBuf @{Wasteful } b
508
513
checkTTCVersion file ver ttcVersion
509
- fromBuf b
514
+ sourceFileHash <- fromBuf b
515
+ interfaceHash <- fromBuf b
516
+ pure (sourceFileHash, interfaceHash)
510
517
511
518
export
512
- readIFaceHash : (fname : String) -> -- file containing the module
513
- Core Int
514
- readIFaceHash fname
515
- = do Right buffer <- coreLift $ readFromFile fname
516
- | Left err => pure 0
519
+ readHashes : (fileName : String) -> -- file containing the module
520
+ Core ( String , Int )
521
+ readHashes fileName
522
+ = do Right buffer <- coreLift $ readFromFile fileName
523
+ | Left err => pure ( " " , 0 )
517
524
b <- newRef Bin buffer
518
- catch (do res <- getHash fname b
519
- pure res )
520
- (\ err => pure 0 )
525
+ catch (do hashes <- getHashes fileName b
526
+ pure hashes )
527
+ (\ err => pure ( " " , 0 ) )
521
528
522
529
export
523
530
readImportHashes : (fname : String) -> -- file containing the module
0 commit comments