0 | ||| Reading and writing 'Defs' from/to a binary file. In order to be saved, a
  1 | ||| name must have been flagged using 'toSave'. (Otherwise we'd save out
  2 | ||| everything, not just the things in the current file).
  3 | module Core.Binary
  4 |
  5 | import public Core.Binary.Prims
  6 | import Core.Context.Log
  7 | import Core.Options
  8 | import Core.TTC
  9 | import Core.UnifyState
 10 |
 11 | import Data.String
 12 | import Data.SortedSet
 13 |
 14 | import System.File
 15 |
 16 | import Libraries.Data.NameMap
 17 | import Libraries.Data.NatSet
 18 |
 19 | import public Libraries.Utils.Binary
 20 |
 21 | %default covering
 22 |
 23 | ||| TTC files can only be compatible if the version number is the same
 24 | ||| Update with the current date in YYYY_MM_DD_00 format, or bump the auxiliary
 25 | ||| version number if you're changing the version more than once in the same day.
 26 | export
 27 | ttcVersion : Int
 28 | ttcVersion = 2025_08_16_00
 29 |
 30 | export
 31 | checkTTCVersion : String -> Int -> Int -> Core ()
 32 | checkTTCVersion file ver exp
 33 |   = when (ver /= exp) (throw $ TTCError $ Format file ver exp)
 34 |
 35 | record TTCFile extra where
 36 |   constructor MkTTCFile
 37 |   version : Int
 38 |   totalReq : TotalReq
 39 |   sourceHash : Maybe String
 40 |   ifaceHash : Int
 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)
 48 |   nextVar : Int
 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)))
 59 |   extraData : extra
 60 |
 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
 66 |
 67 |
 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))
 75 |
 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)
 79 |
 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)
 83 |
 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))
 87 |
 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)
 91 |
 92 | HasNames e => HasNames (TTCFile e) where
 93 |   full gam (MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
 94 |                       context userHoles
 95 |                       autoHints typeHints
 96 |                       imported nextVar currentNS nestedNS
 97 |                       pairnames rewritenames primnames foreignImpl
 98 |                       namedirectives cgdirectives trans
 99 |                       fexp extra)
100 |       = pure $ MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
101 |                          context userHoles
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)
110 |                          cgdirectives
111 |                          !(full gam trans)
112 |                          !(full gam fexp)
113 |                          !(full gam extra)
114 |     where
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)
119 |
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)
124 |
125 |       fullPrim : Context -> PrimNames -> Core PrimNames
126 |       fullPrim gam (MkPrimNs mi ms mc md mt mn mdl)
127 |           = [| MkPrimNs (full gam mi)
128 |                         (full gam ms)
129 |                         (full gam mc)
130 |                         (full gam md)
131 |                         (full gam mt)
132 |                         (full gam mn)
133 |                         (full gam mdl) |]
134 |
135 |       fullForeign : (Name, String) -> Core (Name, String)
136 |       fullForeign (n,s) = pure $ (!(full gam n), s)
137 |
138 |   -- I don't think we ever actually want to call this, because after we read
139 |   -- from the file we're going to add them to learn what the resolved names
140 |   -- are supposed to be! But for completeness, let's do it right.
141 |   resolved gam (MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
142 |                       context userHoles
143 |                       autoHints typeHints
144 |                       imported nextVar currentNS nestedNS
145 |                       pairnames rewritenames primnames foreignImpl
146 |                       namedirectives cgdirectives trans
147 |                       fexp extra)
148 |       = pure $ MkTTCFile version totalReq sourceHash ifaceHash iHashes incData
149 |                          context userHoles
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)
158 |                          cgdirectives
159 |                          !(resolved gam trans)
160 |                          !(resolved gam fexp)
161 |                          !(resolved gam extra)
162 |     where
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)
167 |
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)
172 |
173 |       resolvedPrim : Context -> PrimNames -> Core PrimNames
174 |       resolvedPrim gam (MkPrimNs mi ms mc md mt mn mdl)
175 |           = pure $ MkPrimNs !(resolved gam mi)
176 |                             !(resolved gam ms)
177 |                             !(resolved gam mc)
178 |                             !(resolved gam md)
179 |                             !(resolved gam mt)
180 |                             !(resolved gam mn)
181 |                             !(resolved gam mdl)
182 |
183 |       resolvedForeign : (Name, String) -> Core (Name, String)
184 |       resolvedForeign (n,s) = pure $ (!(resolved gam n), s)
185 |
186 | -- NOTE: TTC files are only compatible if the version number is the same,
187 | -- *and* the 'annot/extra' type are the same, or there are no holes/constraints
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
193 |            toBuf "TT2"
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)
217 |
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
232 |            incData <- fromBuf
233 |            imp <- fromBuf
234 |            ex <- fromBuf
235 |            if not readall
236 |               then pure (MkTTCFile ver totalReq
237 |                                    sourceFileHash ifaceHash importHashes
238 |                                    incData [] [] [] [] []
239 |                                    0 (mkNamespace "") [] Nothing
240 |                                    Nothing
241 |                                    (MkPrimNs Nothing Nothing Nothing Nothing Nothing Nothing Nothing)
242 |                                    empty [] empty [] [] ex)
243 |               else do
244 |                  defs <- fromBuf
245 |                  uholes <- fromBuf
246 |                  autohs <- fromBuf
247 |                  typehs <- fromBuf
248 |                  nextv <- fromBuf
249 |                  cns <- fromBuf
250 |                  nns <- fromBuf
251 |                  pns <- fromBuf
252 |                  rws <- fromBuf
253 |                  prims <- fromBuf
254 |                  let foreignImpl = SortedSet.fromList !(fromBuf)
255 |                  nds <- fromBuf
256 |                  let cgds = SortedSet.fromList (!fromBuf)
257 |                  trans <- fromBuf
258 |                  fexp <- 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)
264 |   where
265 |     -- We don't store full names in 'defs' - we remove the namespace if it's
266 |     -- the same as the current namespace. So, this puts it back.
267 |     replaceNS : Namespace -> (Name, a) -> (Name, a)
268 |     replaceNS ns n@(NS _ _, d) = n
269 |     replaceNS ns (n, d) = (NS ns n, d)
270 |
271 | -- Pull out the list of GlobalDefs that we want to save
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 -- 'n' really should exist though!
278 |          -- No need to save builtins
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))
283 |                       b <- get Bin
284 |                       getSaveDefs modns ns ((trimName (fullname gdef), b) :: acc) defs
285 |   where
286 |     trimName : Name -> Name
287 |     trimName n@(NS defns d) = if defns == modns then d else n
288 |     trimName n = n
289 |
290 | -- Write out the things in the context which have been defined in the
291 | -- current source file
292 | export
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
300 |          defs <- get Ctxt
301 |          ust <- get UST
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)
309 |            ]
310 |          writeTTCFile bin
311 |                    (MkTTCFile ttcVersion totalReq
312 |                               sourceHash
313 |                               (ifaceHash defs) (importHashes defs)
314 |                               (incData defs)
315 |                               gdefs
316 |                               (keys (userHoles defs))
317 |                               (saveAutoHints defs)
318 |                               (saveTypeHints defs)
319 |                               (imported defs)
320 |                               (nextName ust)
321 |                               (currentNS defs)
322 |                               (nestedNS 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))
331 |                               extradata)
332 |
333 |          Right ok <- coreLift $ writeToFile ttcFileName !(get Bin)
334 |                | Left err => throw (InternalError (ttcFileName ++ ": " ++ show err))
335 |          pure ()
336 |
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)
344 |          -- Don't update the coded entry because some names might not be
345 |          -- resolved yet
346 |          entry <- maybe (pure Nothing)
347 |                         (\ p => do x <- decode (gamma defs) (fst p) False (snd p)
348 |                                    pure (Just x))
349 |                         codedentry
350 |          unless (completeDef entry) $
351 |            ignore $ addContextEntry filens n def
352 |
353 |          whenJust asm $ \ as => addContextAlias (asName modns as n) n
354 |
355 |   where
356 |     -- If the definition already exists, don't overwrite it with an empty
357 |     -- definition or hole. This might happen if a function is declared in one
358 |     -- module and defined in another.
359 |     completeDef : Maybe GlobalDef -> Bool
360 |     completeDef Nothing = False
361 |     completeDef (Just def)
362 |         = case definition def of
363 |                None => False
364 |                Hole {} => False
365 |                _ => True
366 |
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
373 |
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 }
378 |
379 | export
380 | updatePair : {auto c : Ref Ctxt Defs} -> Maybe PairNames -> Core ()
381 | updatePair p = update Ctxt { options->pairnames $= (p <+>) }
382 |
383 | export
384 | updateRewrite : {auto c : Ref Ctxt Defs} -> Maybe RewriteNames -> Core ()
385 | updateRewrite r = update Ctxt { options->rewritenames $= (r <+>) }
386 |
387 | export
388 | updatePrimNames : PrimNames -> PrimNames -> PrimNames
389 | updatePrimNames p
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   <+>)
397 |       }
398 |
399 | export
400 | updatePrims : {auto c : Ref Ctxt Defs} -> PrimNames -> Core ()
401 | updatePrims p = update Ctxt { options->primnames $= updatePrimNames p }
402 |
403 | export
404 | updateForeignImpl : {auto c : Ref Ctxt Defs} -> SortedSet (Name, String) -> Core ()
405 | updateForeignImpl xs =
406 |     update Ctxt { options->foreignImpl $= union xs }
407 |
408 | export
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
415 |
416 | export
417 | updateCGDirectives : {auto c : Ref Ctxt Defs} ->
418 |                      SortedSet (CG, String) -> Core ()
419 | updateCGDirectives cgs = update Ctxt { cgdirectives $= union cgs }
420 |
421 | export
422 | updateTransforms : {auto c : Ref Ctxt Defs} ->
423 |                    List (Name, Transform) -> Core ()
424 | updateTransforms ts = update Ctxt { pendingTransforms $= (++ ts) }
425 |
426 | export
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)
433 |          updateFExports ns
434 |
435 | getNSas : (String, (ModuleIdent, Bool, Namespace)) ->
436 |           (ModuleIdent, Namespace)
437 | getNSas (a, (b, c, d)) = (b, d)
438 |
439 | -- Add definitions from a binary file to the current context
440 | -- Returns the "extra" section of the file (user defined data), the interface
441 | -- hash and the list of additional TTCs that need importing
442 | -- (we need to return these, rather than do it here, because after loading
443 | -- the data that's when we process the extra data...)
444 | export
445 | readFromTTC : TTC extra =>
446 |               {auto c : Ref Ctxt Defs} ->
447 |               {auto u : Ref UST UState} ->
448 |               Bool -> -- set nested namespaces (for records, to use at the REPL)
449 |               FC ->
450 |               Bool -> -- importing as public
451 |               (fname : String) -> -- file containing the module
452 |               (modNS : ModuleIdent) -> -- module namespace
453 |               (importAs : Namespace) -> -- namespace to import as
454 |               Core (Maybe (extra, Int,
455 |                            List (ModuleIdent, Bool, Namespace)))
456 | readFromTTC nestedns loc reexp fname modNS importAs
457 |     = do defs <- get Ctxt
458 |          -- If it's already in the context, with the same visibility flag,
459 |          -- don't load it again (we do need to load it again if it's visible
460 |          -- this time, because we need to reexport the dependencies.)
461 |          let False = (modNS, reexp, importAs) `elem` map snd (allImported defs)
462 |               | True => pure Nothing
463 |          put Ctxt ({ allImported $= ((fname, (modNS, reexp, importAs)) :: ) } defs)
464 |
465 |          Right buffer <- coreLift $ readFromFile fname
466 |                | Left err => throw (InternalError (fname ++ ": " ++ show err))
467 |          bin <- newRef Bin buffer -- for reading the file into
468 |          let as = if importAs == miAsNamespace modNS
469 |                      then Nothing
470 |                      else Just importAs
471 |
472 |          -- If it's already imported, but without reexporting, then all we're
473 |          -- interested in is returning which other modules to load.
474 |          -- Otherwise, add the data
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))
479 |             else do
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)
486 |                -- Only do the next batch if the module hasn't been loaded
487 |                -- in any form
488 |                unless (modNS `elem` map (fst . getNSas) (allImported defs)) $
489 |                -- Set up typeHints and autoHints based on the loaded data
490 |                  do traverse_ (addTypeHint loc) (typeHints ttc)
491 |                     traverse_ addAutoHint (autoHints ttc)
492 |                     addImportedInc modNS (incData ttc)
493 |                     -- Set up pair/rewrite etc names
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)
502 |
503 |                when (not reexp) clearSavedHints
504 |                resetFirstEntry
505 |
506 |                -- Finally, update the unification state with the holes from the
507 |                -- ttc
508 |                update UST { nextName := nextVar ttc }
509 |                pure (Just (ex, ifaceHash ttc, imported ttc))
510 |   where
511 |     alreadyDone : ModuleIdent -> Namespace ->
512 |                   List (String, (ModuleIdent, Bool, Namespace)) ->
513 |                   Bool
514 |     alreadyDone modns importAs [] = False
515 |       -- If we've already imported 'modns' as 'importAs', or we're importing
516 |       -- 'modns' as itself and it's already imported as anything, then no
517 |       -- need to load again.
518 |     alreadyDone modns importAs ((_, (m, _, a)) :: rest)
519 |         = (modns == m && importAs == a)
520 |           || (modns == m && miAsNamespace modns == importAs)
521 |           || alreadyDone modns importAs rest
522 |
523 | -- Implements a portion of @readTTCFile@. The fields must be read in order.
524 | -- This reads everything up to and including `totalReq`.
525 | export
526 | getTotalReq : String -> Ref Bin Binary -> Core TotalReq
527 | getTotalReq file b
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
533 |          fromBuf -- `totalReq`
534 |
535 | -- Implements a portion of @readTTCFile@. The fields must be read in order.
536 | -- This reads everything up to and including `interfaceHash`.
537 | export
538 | getHashes : String -> Ref Bin Binary -> Core (Maybe String, Int)
539 | getHashes file b
540 |     = do ignore $ getTotalReq file b
541 |          sourceFileHash <- fromBuf
542 |          interfaceHash <- fromBuf
543 |          pure (sourceFileHash, interfaceHash)
544 |
545 | -- Implements a portion of @readTTCFile@. The fields must be read in order.
546 | -- This reads everything up to and including `importHashes`.
547 | getImportHashes : String -> Ref Bin Binary ->
548 |                   Core (List (Namespace, Int))
549 | getImportHashes file b
550 |     = do ignore $ getHashes file b
551 |          fromBuf -- `importHashes`
552 |
553 | -- Implements a portion of @readTTCFile@. The fields must be read in order.
554 | -- This reads everything up to and including `incData`.
555 | getIncData : String -> Ref Bin Binary ->
556 |              Core (List (CG, String, List String))
557 | getIncData file b
558 |     = do ignore $ getImportHashes file b
559 |          fromBuf -- `incData`
560 |
561 | export
562 | readTotalReq : (fileName : String) -> -- file containing the module
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)
570 |
571 | export
572 | readHashes : (fileName : String) -> -- file containing the module
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))
580 |
581 | export
582 | readImportHashes : (fname : String) -> -- file containing the module
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
589 |                    pure res)
590 |                (\err => pure [])
591 |
592 | export
593 | readIncData : (fname : String) -> -- file containing the module
594 |               Core (List (CG, String, List String))
595 | readIncData fname
596 |     = do Right buffer <- coreLift $ readFromFile fname
597 |             | Left err => pure []
598 |          b <- newRef Bin buffer
599 |          catch (do res <- getIncData fname b
600 |                    pure res)
601 |                (\err => pure [])
602 |