5 | import public Core.Binary.Prims
6 | import Core.Context.Log
9 | import Core.UnifyState
12 | import Data.SortedSet
16 | import Libraries.Data.NameMap
17 | import Libraries.Data.NatSet
19 | import public Libraries.Utils.Binary
28 | ttcVersion = 2025_08_16_00
31 | checkTTCVersion : String -> Int -> Int -> Core ()
32 | checkTTCVersion file ver exp
33 | = when (ver /= exp) (throw $
TTCError $
Format file ver exp)
35 | record TTCFile extra where
36 | constructor MkTTCFile
39 | sourceHash : Maybe String
41 | importHashes : List (Namespace, Int)
42 | incData : List (CG, String, List String)
43 | context : List (Name, Binary)
44 | userHoles : List Name
45 | autoHints : List (Name, Bool)
46 | typeHints : List (Name, Name, Bool)
47 | imported : List (ModuleIdent, Bool, Namespace)
49 | currentNS : Namespace
50 | nestedNS : List Namespace
51 | pairnames : Maybe PairNames
52 | rewritenames : Maybe RewriteNames
53 | primnames : PrimNames
54 | foreignImpl : SortedSet (Name, String)
55 | namedirectives : List (Name, List String)
56 | cgdirectives : SortedSet (CG, String)
57 | transforms : List (Name, Transform)
58 | foreignExports : List (Name, (List (String, String)))
61 | HasNames a => HasNames (List a) where
62 | full c ns = full_aux c [] ns
63 | where full_aux : Context -> List a -> List a -> Core (List a)
64 | full_aux c res [] = pure (reverse res)
65 | full_aux c res (n :: ns) = full_aux c (!(full c n):: res) ns
68 | resolved c ns = resolved_aux c [] ns
69 | where resolved_aux : Context -> List a -> List a -> Core (List a)
70 | resolved_aux c res [] = pure (reverse res)
71 | resolved_aux c res (n :: ns) = resolved_aux c (!(resolved c n) :: res) ns
72 | HasNames (Int, FC, Name) where
73 | full c (i, fc, n) = pure (i, fc, !(full c n))
74 | resolved c (i, fc, n) = pure (i, fc, !(resolved c n))
76 | HasNames (Name, Bool) where
77 | full c (n, b) = pure (!(full c n), b)
78 | resolved c (n, b) = pure (!(resolved c n), b)
80 | HasNames (Name, List a) where
81 | full c (n, b) = pure (!(full c n), b)
82 | resolved c (n, b) = pure (!(resolved c n), b)
84 | HasNames (Name, Transform) where
85 | full c (n, b) = pure (!(full c n), !(full c b))
86 | resolved c (n, b) = pure (!(resolved c n), !(resolved c b))
88 | HasNames (Name, Name, Bool) where
89 | full c (n1, n2, b) = pure (!(full c n1), !(full c n2), b)
90 | resolved c (n1, n2, b) = pure (!(resolved c n1), !(resolved c n2), b)
92 | HasNames e => HasNames (TTCFile e) where
93 | full gam (MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
96 | imported nextVar currentNS nestedNS
97 | pairnames rewritenames primnames foreignImpl
98 | namedirectives cgdirectives trans
100 | = pure $
MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
102 | !(traverse (full gam) autoHints)
103 | !(traverse (full gam) typeHints)
104 | imported nextVar currentNS nestedNS
105 | !(fullPair gam pairnames)
106 | !(fullRW gam rewritenames)
107 | !(fullPrim gam primnames)
108 | (SortedSet.fromList !(traverse fullForeign $
Prelude.toList foreignImpl))
109 | !(full gam namedirectives)
115 | fullPair : Context -> Maybe PairNames -> Core (Maybe PairNames)
116 | fullPair gam Nothing = pure Nothing
117 | fullPair gam (Just (MkPairNs t f s))
118 | = pure $
Just $
MkPairNs !(full gam t) !(full gam f) !(full gam s)
120 | fullRW : Context -> Maybe RewriteNames -> Core (Maybe RewriteNames)
121 | fullRW gam Nothing = pure Nothing
122 | fullRW gam (Just (MkRewriteNs e r))
123 | = pure $
Just $
MkRewriteNs !(full gam e) !(full gam r)
125 | fullPrim : Context -> PrimNames -> Core PrimNames
126 | fullPrim gam (MkPrimNs mi ms mc md mt mn mdl)
127 | = [| MkPrimNs (full gam mi)
135 | fullForeign : (Name, String) -> Core (Name, String)
136 | fullForeign (n,s) = pure $
(!(full gam n), s)
141 | resolved gam (MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
143 | autoHints typeHints
144 | imported nextVar currentNS nestedNS
145 | pairnames rewritenames primnames foreignImpl
146 | namedirectives cgdirectives trans
148 | = pure $
MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
150 | !(traverse (resolved gam) autoHints)
151 | !(traverse (resolved gam) typeHints)
152 | imported nextVar currentNS nestedNS
153 | !(resolvedPair gam pairnames)
154 | !(resolvedRW gam rewritenames)
155 | !(resolvedPrim gam primnames)
156 | (SortedSet.fromList !(traverse resolvedForeign $
Prelude.toList foreignImpl))
157 | !(resolved gam namedirectives)
159 | !(resolved gam trans)
160 | !(resolved gam fexp)
161 | !(resolved gam extra)
163 | resolvedPair : Context -> Maybe PairNames -> Core (Maybe PairNames)
164 | resolvedPair gam Nothing = pure Nothing
165 | resolvedPair gam (Just (MkPairNs t f s))
166 | = pure $
Just $
MkPairNs !(resolved gam t) !(resolved gam f) !(resolved gam s)
168 | resolvedRW : Context -> Maybe RewriteNames -> Core (Maybe RewriteNames)
169 | resolvedRW gam Nothing = pure Nothing
170 | resolvedRW gam (Just (MkRewriteNs e r))
171 | = pure $
Just $
MkRewriteNs !(resolved gam e) !(resolved gam r)
173 | resolvedPrim : Context -> PrimNames -> Core PrimNames
174 | resolvedPrim gam (MkPrimNs mi ms mc md mt mn mdl)
175 | = pure $
MkPrimNs !(resolved gam mi)
181 | !(resolved gam mdl)
183 | resolvedForeign : (Name, String) -> Core (Name, String)
184 | resolvedForeign (n,s) = pure $
(!(resolved gam n), s)
188 | writeTTCFile : (HasNames extra, TTC extra) =>
189 | {auto c : Ref Ctxt Defs} ->
190 | Ref Bin Binary -> TTCFile extra -> Core ()
191 | writeTTCFile b file_in
192 | = do file <- toFullNames file_in
194 | toBuf @{Wasteful} (version file)
195 | toBuf (totalReq file)
196 | toBuf (sourceHash file)
197 | toBuf (ifaceHash file)
198 | toBuf (importHashes file)
199 | toBuf (incData file)
200 | toBuf (imported file)
201 | toBuf (extraData file)
202 | toBuf (context file)
203 | toBuf (userHoles file)
204 | toBuf (autoHints file)
205 | toBuf (typeHints file)
206 | toBuf (nextVar file)
207 | toBuf (currentNS file)
208 | toBuf (nestedNS file)
209 | toBuf (pairnames file)
210 | toBuf (rewritenames file)
211 | toBuf (primnames file)
212 | toBuf (Prelude.toList $
foreignImpl file)
213 | toBuf (namedirectives file)
214 | toBuf (Prelude.toList $
cgdirectives file)
215 | toBuf (transforms file)
216 | toBuf (foreignExports file)
218 | readTTCFile : TTC extra =>
219 | {auto c : Ref Ctxt Defs} ->
220 | Bool -> String -> Maybe (Namespace) ->
221 | Ref Bin Binary -> Core (TTCFile extra)
222 | readTTCFile readall file as b
223 | = do hdr <- fromBuf
224 | when (hdr /= "TT2") $
225 | corrupt ("TTC header in " ++ file ++ " " ++ show hdr)
226 | ver <- fromBuf @{Wasteful}
227 | checkTTCVersion file ver ttcVersion
228 | totalReq <- fromBuf
229 | sourceFileHash <- fromBuf
230 | ifaceHash <- fromBuf
231 | importHashes <- fromBuf
236 | then pure (MkTTCFile ver totalReq
237 | sourceFileHash ifaceHash importHashes
238 | incData [] [] [] [] []
239 | 0 (mkNamespace "") [] Nothing
241 | (MkPrimNs Nothing Nothing Nothing Nothing Nothing Nothing Nothing)
242 | empty [] empty [] [] ex)
254 | let foreignImpl = SortedSet.fromList !(fromBuf)
256 | let cgds = SortedSet.fromList (!fromBuf)
259 | pure (MkTTCFile ver totalReq
260 | sourceFileHash ifaceHash importHashes incData
261 | (map (replaceNS cns) defs) uholes
262 | autohs typehs imp nextv cns nns
263 | pns rws prims foreignImpl nds cgds trans fexp ex)
267 | replaceNS : Namespace -> (Name, a) -> (Name, a)
268 | replaceNS ns n@(NS _ _, d) = n
269 | replaceNS ns (n, d) = (NS ns n, d)
272 | getSaveDefs : Namespace -> List Name -> List (Name, Binary) -> Defs ->
273 | Core (List (Name, Binary))
274 | getSaveDefs modns [] acc _ = pure acc
275 | getSaveDefs modns (n :: ns) acc defs
276 | = do Just gdef <- lookupCtxtExact n (gamma defs)
277 | | Nothing => getSaveDefs modns ns acc defs
279 | case definition gdef of
280 | Builtin _ => getSaveDefs modns ns acc defs
281 | _ => do bin <- initBinaryS 16384
282 | toBuf (trimNS modns !(full (gamma defs) gdef))
284 | getSaveDefs modns ns ((trimName (fullname gdef), b) :: acc) defs
286 | trimName : Name -> Name
287 | trimName n@(NS defns d) = if defns == modns then d else n
293 | writeToTTC : (HasNames extra, TTC extra) =>
294 | {auto c : Ref Ctxt Defs} ->
295 | {auto u : Ref UST UState} ->
296 | extra -> (sourceFileName : String) ->
297 | (ttcFileName : String) -> Core ()
298 | writeToTTC extradata sourceFileName ttcFileName
299 | = do bin <- initBinary
302 | gdefs <- getSaveDefs (currentNS defs) (keys (toSave defs)) [] defs
303 | sourceHash <- hashFileWith defs.options.hashFn sourceFileName
304 | totalReq <- getDefaultTotalityOption
305 | log "ttc.write" 5 $
unwords
306 | [ "Writing", ttcFileName
307 | , "with source hash", show sourceHash
308 | , "and interface hash", show (ifaceHash defs)
311 | (MkTTCFile ttcVersion totalReq
313 | (ifaceHash defs) (importHashes defs)
316 | (keys (userHoles defs))
317 | (saveAutoHints defs)
318 | (saveTypeHints defs)
323 | (pairnames (options defs))
324 | (rewritenames (options defs))
325 | (primnames (options defs))
326 | (foreignImpl (options defs))
327 | (NameMap.toList (namedirectives defs))
328 | (cgdirectives defs)
329 | (saveTransforms defs)
330 | (NameMap.toList (foreignExports defs))
333 | Right ok <- coreLift $
writeToFile ttcFileName !(get Bin)
334 | | Left err => throw (InternalError (ttcFileName ++ ": " ++ show err))
337 | addGlobalDef : {auto c : Ref Ctxt Defs} ->
338 | (modns : ModuleIdent) -> Namespace ->
339 | (importAs : Maybe Namespace) ->
340 | (Name, Binary) -> Core ()
341 | addGlobalDef modns filens asm (n, def)
342 | = do defs <- get Ctxt
343 | codedentry <- lookupContextEntry n (gamma defs)
346 | entry <- maybe (pure Nothing)
347 | (\ p => do x <- decode (gamma defs) (fst p) False (snd p)
350 | unless (completeDef entry) $
351 | ignore $
addContextEntry filens n def
353 | whenJust asm $
\ as => addContextAlias (asName modns as n) n
359 | completeDef : Maybe GlobalDef -> Bool
360 | completeDef Nothing = False
361 | completeDef (Just def)
362 | = case definition def of
367 | addTypeHint : {auto c : Ref Ctxt Defs} ->
368 | FC -> (Name, Name, Bool) -> Core ()
369 | addTypeHint fc (tyn, hintn, d)
370 | = do logC "ttc.read" 10 (pure (show !(getFullName hintn) ++ " for " ++
371 | show !(getFullName tyn)))
372 | addHintFor fc tyn hintn d True
374 | addAutoHint : {auto c : Ref Ctxt Defs} -> (Name, Bool) -> Core ()
375 | addAutoHint (hintn_in, d)
376 | = do hintn <- toResolvedNames hintn_in
377 | update Ctxt { autoHints $= insert hintn d }
380 | updatePair : {auto c : Ref Ctxt Defs} -> Maybe PairNames -> Core ()
381 | updatePair p = update Ctxt { options->pairnames $= (p <+>) }
384 | updateRewrite : {auto c : Ref Ctxt Defs} -> Maybe RewriteNames -> Core ()
385 | updateRewrite r = update Ctxt { options->rewritenames $= (r <+>) }
388 | updatePrimNames : PrimNames -> PrimNames -> PrimNames
390 | = { fromIntegerName $= (p.fromIntegerName <+>),
391 | fromStringName $= (p.fromStringName <+>),
392 | fromCharName $= (p.fromCharName <+>),
393 | fromDoubleName $= (p.fromDoubleName <+>),
394 | fromTTImpName $= (p.fromTTImpName <+>),
395 | fromNameName $= (p.fromNameName <+>),
396 | fromDeclsName $= (p.fromDeclsName <+>)
400 | updatePrims : {auto c : Ref Ctxt Defs} -> PrimNames -> Core ()
401 | updatePrims p = update Ctxt { options->primnames $= updatePrimNames p }
404 | updateForeignImpl : {auto c : Ref Ctxt Defs} -> SortedSet (Name, String) -> Core ()
405 | updateForeignImpl xs =
406 | update Ctxt { options->foreignImpl $= union xs }
409 | updateNameDirectives : {auto c : Ref Ctxt Defs} ->
410 | List (Name, List String) -> Core ()
411 | updateNameDirectives [] = pure ()
412 | updateNameDirectives ((t, ns) :: nds)
413 | = do update Ctxt { namedirectives $= insert t ns }
414 | updateNameDirectives nds
417 | updateCGDirectives : {auto c : Ref Ctxt Defs} ->
418 | SortedSet (CG, String) -> Core ()
419 | updateCGDirectives cgs = update Ctxt { cgdirectives $= union cgs }
422 | updateTransforms : {auto c : Ref Ctxt Defs} ->
423 | List (Name, Transform) -> Core ()
424 | updateTransforms ts = update Ctxt { pendingTransforms $= (++ ts) }
427 | updateFExports : {auto c : Ref Ctxt Defs} ->
428 | List (Name, (List (String, String))) -> Core ()
429 | updateFExports [] = pure ()
430 | updateFExports ((n, conv) :: ns)
431 | = do defs <- get Ctxt
432 | put Ctxt ({ foreignExports $= insert n conv } defs)
435 | getNSas : (String, (ModuleIdent, Bool, Namespace)) ->
436 | (ModuleIdent, Namespace)
437 | getNSas (a, (b, c, d)) = (b, d)
445 | readFromTTC : TTC extra =>
446 | {auto c : Ref Ctxt Defs} ->
447 | {auto u : Ref UST UState} ->
451 | (fname : String) ->
452 | (modNS : ModuleIdent) ->
453 | (importAs : Namespace) ->
454 | Core (Maybe (extra, Int,
455 | List (ModuleIdent, Bool, Namespace)))
456 | readFromTTC nestedns loc reexp fname modNS importAs
457 | = do defs <- get Ctxt
461 | let False = (modNS, reexp, importAs) `elem` map snd (allImported defs)
462 | | True => pure Nothing
463 | put Ctxt ({ allImported $= ((fname, (modNS, reexp, importAs)) :: ) } defs)
465 | Right buffer <- coreLift $
readFromFile fname
466 | | Left err => throw (InternalError (fname ++ ": " ++ show err))
467 | bin <- newRef Bin buffer
468 | let as = if importAs == miAsNamespace modNS
475 | if alreadyDone modNS importAs (allImported defs)
476 | then do ttc <- readTTCFile False fname as bin
477 | let ex = extraData ttc
478 | pure (Just (ex, ifaceHash ttc, imported ttc))
480 | ttc <- readTTCFile True fname as bin
481 | let ex = extraData ttc
482 | traverse_ (addGlobalDef modNS (currentNS ttc) as) (context ttc)
483 | traverse_ (addUserHole True) (userHoles ttc)
484 | setNS (currentNS ttc)
485 | when nestedns $
setNestedNS (nestedNS ttc)
488 | unless (modNS `elem` map (fst . getNSas) (allImported defs)) $
490 | do traverse_ (addTypeHint loc) (typeHints ttc)
491 | traverse_ addAutoHint (autoHints ttc)
492 | addImportedInc modNS (incData ttc)
494 | updatePair (pairnames ttc)
495 | updateRewrite (rewritenames ttc)
496 | updatePrims (primnames ttc)
497 | updateForeignImpl (foreignImpl ttc)
498 | updateNameDirectives (reverse (namedirectives ttc))
499 | updateCGDirectives (cgdirectives ttc)
500 | updateTransforms (transforms ttc)
501 | updateFExports (foreignExports ttc)
503 | when (not reexp) clearSavedHints
508 | update UST { nextName := nextVar ttc }
509 | pure (Just (ex, ifaceHash ttc, imported ttc))
511 | alreadyDone : ModuleIdent -> Namespace ->
512 | List (String, (ModuleIdent, Bool, Namespace)) ->
514 | alreadyDone modns importAs [] = False
518 | alreadyDone modns importAs ((_, (m, _, a)) :: rest)
519 | = (modns == m && importAs == a)
520 | || (modns == m && miAsNamespace modns == importAs)
521 | || alreadyDone modns importAs rest
526 | getTotalReq : String -> Ref Bin Binary -> Core TotalReq
528 | = do hdr <- fromBuf {a = String}
529 | when (hdr /= "TT2") $
530 | corrupt ("TTC header in " ++ file ++ " " ++ show hdr)
531 | ver <- fromBuf @{Wasteful}
532 | checkTTCVersion file ver ttcVersion
538 | getHashes : String -> Ref Bin Binary -> Core (Maybe String, Int)
540 | = do ignore $
getTotalReq file b
541 | sourceFileHash <- fromBuf
542 | interfaceHash <- fromBuf
543 | pure (sourceFileHash, interfaceHash)
547 | getImportHashes : String -> Ref Bin Binary ->
548 | Core (List (Namespace, Int))
549 | getImportHashes file b
550 | = do ignore $
getHashes file b
555 | getIncData : String -> Ref Bin Binary ->
556 | Core (List (CG, String, List String))
558 | = do ignore $
getImportHashes file b
562 | readTotalReq : (fileName : String) ->
563 | Core (Maybe TotalReq)
564 | readTotalReq fileName
565 | = do Right buffer <- coreLift $
readFromFile fileName
566 | | Left err => pure Nothing
567 | b <- newRef Bin buffer
568 | catch (Just <$> getTotalReq fileName b)
569 | (\err => pure Nothing)
572 | readHashes : (fileName : String) ->
573 | Core (Maybe String, Int)
574 | readHashes fileName
575 | = do Right buffer <- coreLift $
readFromFile fileName
576 | | Left err => pure (Nothing, 0)
577 | b <- newRef Bin buffer
578 | catch (getHashes fileName b)
579 | (\err => pure (Nothing, 0))
582 | readImportHashes : (fname : String) ->
583 | Core (List (Namespace, Int))
584 | readImportHashes fname
585 | = do Right buffer <- coreLift $
readFromFile fname
586 | | Left err => pure []
587 | b <- newRef Bin buffer
588 | catch (do res <- getImportHashes fname b
593 | readIncData : (fname : String) ->
594 | Core (List (CG, String, List String))
596 | = do Right buffer <- coreLift $
readFromFile fname
597 | | Left err => pure []
598 | b <- newRef Bin buffer
599 | catch (do res <- getIncData fname b