3 -- (c) The University of Glasgow 2002-2006
5 -- Binary interface file support.
7 module BinIface ( writeBinIface, readBinIface,
8 CheckHiWay(..), TraceBinIFaceReading(..) ) where
10 #include "HsVersions.h"
43 import Control.Exception
46 data CheckHiWay = CheckHiWay | IgnoreHiWay
49 data TraceBinIFaceReading = TraceBinIFaceReading | QuietBinIFaceReading
52 -- ---------------------------------------------------------------------------
53 -- Reading and writing binary interface files
55 readBinIface :: CheckHiWay -> TraceBinIFaceReading -> FilePath
56 -> TcRnIf a b ModIface
57 readBinIface checkHiWay traceBinIFaceReading hi_path = do
59 (new_nc, iface) <- liftIO $
60 readBinIface_ checkHiWay traceBinIFaceReading hi_path nc
64 readBinIface_ :: CheckHiWay -> TraceBinIFaceReading -> FilePath -> NameCache
65 -> IO (NameCache, ModIface)
66 readBinIface_ checkHiWay traceBinIFaceReading hi_path nc = do
67 let printer :: SDoc -> IO ()
68 printer = case traceBinIFaceReading of
69 TraceBinIFaceReading -> \sd -> printSDoc sd defaultDumpStyle
70 QuietBinIFaceReading -> \_ -> return ()
71 wantedGot :: Outputable a => String -> a -> a -> IO ()
72 wantedGot what wanted got
73 = printer (text what <> text ": " <>
74 vcat [text "Wanted " <> ppr wanted <> text ",",
75 text "got " <> ppr got])
77 errorOnMismatch' :: (Eq a, Show a) => String -> a -> a -> IO () -> IO ()
78 errorOnMismatch' what wanted got io
79 = do when (wanted /= got) $ io
80 errorOnMismatch what wanted got
82 errorOnMismatch :: (Eq a, Show a) => String -> a -> a -> IO ()
83 errorOnMismatch what wanted got
84 -- This will be caught by readIface which will emit an error
85 -- msg containing the iface module name.
86 = when (wanted /= got) $ throwDyn $ ProgramError
87 (what ++ " (wanted " ++ show wanted
88 ++ ", got " ++ show got ++ ")")
89 bh <- Binary.readBinMem hi_path
91 -- Read the magic number to check that this really is a GHC .hi file
92 -- (This magic number does not change when we change
93 -- GHC interface file format)
95 wantedGot "Magic" binaryInterfaceMagic magic
96 errorOnMismatch "magic number mismatch: old/corrupt interface file?"
97 binaryInterfaceMagic magic
99 -- Get the dictionary pointer. We won't attempt to actually
100 -- read the dictionary until we've done the version checks below,
101 -- just in case this isn't a valid interface. In retrospect the
102 -- version should have come before the dictionary pointer, but this
103 -- is the way it was done originally, and we can't change it now.
104 dict_p <- Binary.get bh -- Get the dictionary ptr
106 -- Check the interface file version and ways.
108 let our_ver = show opt_HiVersion
109 wantedGot "Version" our_ver check_ver
110 errorOnMismatch "mismatched interface file versions" our_ver check_ver
113 way_descr <- getWayDescr
114 wantedGot "Way" way_descr check_way
115 when (checkHiWay == CheckHiWay) $
116 errorOnMismatch "mismatched interface file ways" way_descr check_way
118 -- Read the dictionary
119 -- The next word in the file is a pointer to where the dictionary is
120 -- (probably at the end of the file)
121 data_p <- tellBin bh -- Remember where we are now
123 dict <- getDictionary bh
124 seekBin bh data_p -- Back to where we were before
126 -- Initialise the user-data field of bh
127 ud <- newReadState dict
128 bh <- return (setUserData bh ud)
130 symtab_p <- Binary.get bh -- Get the symtab ptr
131 data_p <- tellBin bh -- Remember where we are now
133 (nc', symtab) <- getSymbolTable bh nc
134 seekBin bh data_p -- Back to where we were before
135 let ud = getUserData bh
136 bh <- return $! setUserData bh ud{ud_symtab = symtab}
141 writeBinIface :: DynFlags -> FilePath -> ModIface -> IO ()
142 writeBinIface dflags hi_path mod_iface = do
143 bh <- openBinMem initBinMemSize
144 put_ bh binaryInterfaceMagic
146 -- Remember where the dictionary pointer will go
147 dict_p_p <- tellBin bh
148 put_ bh dict_p_p -- Placeholder for ptr to dictionary
150 -- The version and way descriptor go next
151 put_ bh (show opt_HiVersion)
152 way_descr <- getWayDescr
155 -- Remember where the symbol table pointer will go
156 symtab_p_p <- tellBin bh
159 -- Make some intial state
160 symtab_next <- newFastMutInt
161 writeFastMutInt symtab_next 0
162 symtab_map <- newIORef emptyUFM
163 let bin_symtab = BinSymbolTable {
164 bin_symtab_next = symtab_next,
165 bin_symtab_map = symtab_map }
166 dict_next_ref <- newFastMutInt
167 writeFastMutInt dict_next_ref 0
168 dict_map_ref <- newIORef emptyUFM
169 let bin_dict = BinDictionary {
170 bin_dict_next = dict_next_ref,
171 bin_dict_map = dict_map_ref }
172 ud <- newWriteState (putName bin_symtab) (putFastString bin_dict)
174 -- Put the main thing,
175 bh <- return $ setUserData bh ud
178 -- Write the symtab pointer at the fornt of the file
179 symtab_p <- tellBin bh -- This is where the symtab will start
180 putAt bh symtab_p_p symtab_p -- Fill in the placeholder
181 seekBin bh symtab_p -- Seek back to the end of the file
183 -- Write the symbol table itself
184 symtab_next <- readFastMutInt symtab_next
185 symtab_map <- readIORef symtab_map
186 putSymbolTable bh symtab_next symtab_map
187 debugTraceMsg dflags 3 (text "writeBinIface:" <+> int symtab_next
190 -- NB. write the dictionary after the symbol table, because
191 -- writing the symbol table may create more dictionary entries.
193 -- Write the dictionary pointer at the fornt of the file
194 dict_p <- tellBin bh -- This is where the dictionary will start
195 putAt bh dict_p_p dict_p -- Fill in the placeholder
196 seekBin bh dict_p -- Seek back to the end of the file
198 -- Write the dictionary itself
199 dict_next <- readFastMutInt dict_next_ref
200 dict_map <- readIORef dict_map_ref
201 putDictionary bh dict_next dict_map
202 debugTraceMsg dflags 3 (text "writeBinIface:" <+> int dict_next
203 <+> text "dict entries")
205 -- And send the result to the file
206 writeBinMem bh hi_path
208 initBinMemSize :: Int
209 initBinMemSize = 1024 * 1024
211 -- The *host* architecture version:
212 #include "MachDeps.h"
214 binaryInterfaceMagic :: Word32
215 #if WORD_SIZE_IN_BITS == 32
216 binaryInterfaceMagic = 0x1face
217 #elif WORD_SIZE_IN_BITS == 64
218 binaryInterfaceMagic = 0x1face64
221 -- -----------------------------------------------------------------------------
224 putSymbolTable :: BinHandle -> Int -> UniqFM (Int,Name) -> IO ()
225 putSymbolTable bh next_off symtab = do
227 let names = elems (array (0,next_off-1) (eltsUFM symtab))
228 mapM_ (\n -> serialiseName bh n symtab) names
230 getSymbolTable :: BinHandle -> NameCache -> IO (NameCache, Array Int Name)
231 getSymbolTable bh namecache = do
233 od_names <- sequence (replicate sz (get bh))
235 arr = listArray (0,sz-1) names
236 (namecache', names) =
237 mapAccumR (fromOnDiskName arr) namecache od_names
239 return (namecache', arr)
241 type OnDiskName = (PackageId, ModuleName, OccName)
248 fromOnDiskName _ nc (pid, mod_name, occ) =
250 mod = mkModule pid mod_name
253 case lookupOrigNameCache cache mod occ of
254 Just name -> (nc, name)
258 uniq = uniqFromSupply us
259 name = mkExternalName uniq mod occ noSrcSpan
260 new_cache = extendNameCache cache mod occ name
262 case splitUniqSupply us of { (us',_) ->
263 ( nc{ nsUniqs = us', nsNames = new_cache }, name )
266 serialiseName :: BinHandle -> Name -> UniqFM (Int,Name) -> IO ()
267 serialiseName bh name _ = do
268 let mod = nameModule name
269 put_ bh (modulePackageId mod, moduleName mod, nameOccName name)
272 putName :: BinSymbolTable -> BinHandle -> Name -> IO ()
273 putName BinSymbolTable{
274 bin_symtab_map = symtab_map_ref,
275 bin_symtab_next = symtab_next } bh name
277 symtab_map <- readIORef symtab_map_ref
278 case lookupUFM symtab_map name of
279 Just (off,_) -> put_ bh off
281 off <- readFastMutInt symtab_next
282 writeFastMutInt symtab_next (off+1)
283 writeIORef symtab_map_ref
284 $! addToUFM symtab_map name (off,name)
288 data BinSymbolTable = BinSymbolTable {
289 bin_symtab_next :: !FastMutInt, -- The next index to use
290 bin_symtab_map :: !(IORef (UniqFM (Int,Name)))
295 putFastString :: BinDictionary -> BinHandle -> FastString -> IO ()
296 putFastString BinDictionary { bin_dict_next = j_r,
297 bin_dict_map = out_r} bh f
299 out <- readIORef out_r
300 let uniq = getUnique f
301 case lookupUFM out uniq of
302 Just (j, _) -> put_ bh j
304 j <- readFastMutInt j_r
306 writeFastMutInt j_r (j + 1)
307 writeIORef out_r $! addToUFM out uniq (j, f)
310 data BinDictionary = BinDictionary {
311 bin_dict_next :: !FastMutInt, -- The next index to use
312 bin_dict_map :: !(IORef (UniqFM (Int,FastString)))
313 -- indexed by FastString
316 -- -----------------------------------------------------------------------------
317 -- All the binary instances
320 {-! for IPName derive: Binary !-}
321 {-! for Fixity derive: Binary !-}
322 {-! for FixityDirection derive: Binary !-}
323 {-! for Boxity derive: Binary !-}
324 {-! for StrictnessMark derive: Binary !-}
325 {-! for Activation derive: Binary !-}
328 {-! for Demand derive: Binary !-}
329 {-! for Demands derive: Binary !-}
330 {-! for DmdResult derive: Binary !-}
331 {-! for StrictSig derive: Binary !-}
334 {-! for DefMeth derive: Binary !-}
337 {-! for HsPred derive: Binary !-}
338 {-! for HsType derive: Binary !-}
339 {-! for TupCon derive: Binary !-}
340 {-! for HsTyVarBndr derive: Binary !-}
343 {-! for UfExpr derive: Binary !-}
344 {-! for UfConAlt derive: Binary !-}
345 {-! for UfBinding derive: Binary !-}
346 {-! for UfBinder derive: Binary !-}
347 {-! for HsIdInfo derive: Binary !-}
348 {-! for UfNote derive: Binary !-}
351 {-! for ConDetails derive: Binary !-}
352 {-! for BangType derive: Binary !-}
355 {-! for IsCafCC derive: Binary !-}
356 {-! for IsDupdCC derive: Binary !-}
357 {-! for CostCentre derive: Binary !-}
361 -- ---------------------------------------------------------------------------
362 -- Reading a binary interface into ParsedIface
364 instance Binary ModIface where
368 mi_iface_hash= iface_hash,
369 mi_mod_hash = mod_hash,
371 mi_finsts = hasFamInsts,
374 mi_exports = exports,
375 mi_exp_hash = exp_hash,
376 mi_fixities = fixities,
377 mi_deprecs = deprecs,
380 mi_fam_insts = fam_insts,
382 mi_orphan_hash = orphan_hash,
383 mi_vect_info = vect_info,
384 mi_hpc = hpc_info }) = do
411 hasFamInsts <- get bh
413 usages <- {-# SCC "bin_usages" #-} lazyGet bh
414 exports <- {-# SCC "bin_exports" #-} get bh
416 fixities <- {-# SCC "bin_fixities" #-} get bh
417 deprecs <- {-# SCC "bin_deprecs" #-} lazyGet bh
418 decls <- {-# SCC "bin_tycldecls" #-} get bh
419 insts <- {-# SCC "bin_insts" #-} get bh
420 fam_insts <- {-# SCC "bin_fam_insts" #-} get bh
421 rules <- {-# SCC "bin_rules" #-} lazyGet bh
422 orphan_hash <- get bh
426 mi_module = mod_name,
428 mi_iface_hash = iface_hash,
429 mi_mod_hash = mod_hash,
431 mi_finsts = hasFamInsts,
434 mi_exports = exports,
435 mi_exp_hash = exp_hash,
436 mi_fixities = fixities,
437 mi_deprecs = deprecs,
439 mi_globals = Nothing,
441 mi_fam_insts = fam_insts,
443 mi_orphan_hash = orphan_hash,
444 mi_vect_info = vect_info,
446 -- And build the cached values
447 mi_dep_fn = mkIfaceDepCache deprecs,
448 mi_fix_fn = mkIfaceFixCache fixities,
449 mi_hash_fn = mkIfaceHashCache decls })
451 getWayDescr :: IO String
453 tag <- readIORef v_Build_tag
454 if cGhcUnregisterised == "YES" then return ('u':tag) else return tag
455 -- if this is an unregisterised build, make sure our interfaces
456 -- can't be used by a registerised build.
458 -------------------------------------------------------------------------
459 -- Types from: HscTypes
460 -------------------------------------------------------------------------
462 instance Binary Dependencies where
463 put_ bh deps = do put_ bh (dep_mods deps)
464 put_ bh (dep_pkgs deps)
465 put_ bh (dep_orphs deps)
466 put_ bh (dep_finsts deps)
468 get bh = do ms <- get bh
472 return (Deps { dep_mods = ms, dep_pkgs = ps, dep_orphs = os,
475 instance (Binary name) => Binary (GenAvailInfo name) where
476 put_ bh (Avail aa) = do
479 put_ bh (AvailTC ab ac) = do
490 return (AvailTC ab ac)
492 instance Binary Usage where
493 put_ bh usg@UsagePackageModule{} = do
495 put_ bh (usg_mod usg)
496 put_ bh (usg_mod_hash usg)
497 put_ bh usg@UsageHomeModule{} = do
499 put_ bh (usg_mod_name usg)
500 put_ bh (usg_mod_hash usg)
501 put_ bh (usg_exports usg)
502 put_ bh (usg_entities usg)
510 return UsagePackageModule { usg_mod = nm, usg_mod_hash = mod }
516 return UsageHomeModule { usg_mod_name = nm, usg_mod_hash = mod,
517 usg_exports = exps, usg_entities = ents }
519 instance Binary Deprecations where
520 put_ bh NoDeprecs = putByte bh 0
521 put_ bh (DeprecAll t) = do
524 put_ bh (DeprecSome ts) = do
531 0 -> return NoDeprecs
533 return (DeprecAll aa)
535 return (DeprecSome aa)
537 -------------------------------------------------------------------------
538 -- Types from: BasicTypes
539 -------------------------------------------------------------------------
541 instance Binary Activation where
542 put_ bh NeverActive = do
544 put_ bh AlwaysActive = do
546 put_ bh (ActiveBefore aa) = do
549 put_ bh (ActiveAfter ab) = do
555 0 -> do return NeverActive
556 1 -> do return AlwaysActive
558 return (ActiveBefore aa)
560 return (ActiveAfter ab)
562 instance Binary StrictnessMark where
563 put_ bh MarkedStrict = do
565 put_ bh MarkedUnboxed = do
567 put_ bh NotMarkedStrict = do
572 0 -> do return MarkedStrict
573 1 -> do return MarkedUnboxed
574 _ -> do return NotMarkedStrict
576 instance Binary Boxity where
585 _ -> do return Unboxed
587 instance Binary TupCon where
588 put_ bh (TupCon ab ac) = do
594 return (TupCon ab ac)
596 instance Binary RecFlag where
597 put_ bh Recursive = do
599 put_ bh NonRecursive = do
604 0 -> do return Recursive
605 _ -> do return NonRecursive
607 instance Binary DefMeth where
608 put_ bh NoDefMeth = putByte bh 0
609 put_ bh DefMeth = putByte bh 1
610 put_ bh GenDefMeth = putByte bh 2
614 0 -> return NoDefMeth
616 _ -> return GenDefMeth
618 instance Binary FixityDirection where
628 0 -> do return InfixL
629 1 -> do return InfixR
630 _ -> do return InfixN
632 instance Binary Fixity where
633 put_ bh (Fixity aa ab) = do
639 return (Fixity aa ab)
641 instance (Binary name) => Binary (IPName name) where
642 put_ bh (IPName aa) = put_ bh aa
643 get bh = do aa <- get bh
646 -------------------------------------------------------------------------
647 -- Types from: Demand
648 -------------------------------------------------------------------------
650 instance Binary DmdType where
651 -- Ignore DmdEnv when spitting out the DmdType
652 put bh (DmdType _ ds dr) = do p <- put bh ds; put bh dr; return (castBin p)
653 get bh = do ds <- get bh; dr <- get bh; return (DmdType emptyVarEnv ds dr)
655 instance Binary Demand where
660 put_ bh (Call aa) = do
663 put_ bh (Eval ab) = do
666 put_ bh (Defer ac) = do
669 put_ bh (Box ad) = do
689 instance Binary Demands where
690 put_ bh (Poly aa) = do
693 put_ bh (Prod ab) = do
704 instance Binary DmdResult where
714 0 -> do return TopRes
715 1 -> do return RetCPR -- Really use RetCPR even if -fcpr-off
716 -- The wrapper was generated for CPR in
717 -- the imported module!
718 _ -> do return BotRes
720 instance Binary StrictSig where
721 put_ bh (StrictSig aa) = do
725 return (StrictSig aa)
728 -------------------------------------------------------------------------
729 -- Types from: CostCentre
730 -------------------------------------------------------------------------
732 instance Binary IsCafCC where
735 put_ bh NotCafCC = do
741 _ -> do return NotCafCC
743 instance Binary IsDupdCC where
744 put_ bh OriginalCC = do
751 0 -> do return OriginalCC
752 _ -> do return DupdCC
754 instance Binary CostCentre where
755 put_ bh NoCostCentre = do
757 put_ bh (NormalCC aa ab ac ad) = do
763 put_ bh (AllCafsCC ae) = do
769 0 -> do return NoCostCentre
774 return (NormalCC aa ab ac ad)
776 return (AllCafsCC ae)
778 -------------------------------------------------------------------------
779 -- IfaceTypes and friends
780 -------------------------------------------------------------------------
782 instance Binary IfaceBndr where
783 put_ bh (IfaceIdBndr aa) = do
786 put_ bh (IfaceTvBndr ab) = do
793 return (IfaceIdBndr aa)
795 return (IfaceTvBndr ab)
797 instance Binary IfaceLetBndr where
798 put_ bh (IfLetBndr a b c) = do
802 get bh = do a <- get bh
805 return (IfLetBndr a b c)
807 instance Binary IfaceType where
808 put_ bh (IfaceForAllTy aa ab) = do
812 put_ bh (IfaceTyVar ad) = do
815 put_ bh (IfaceAppTy ae af) = do
819 put_ bh (IfaceFunTy ag ah) = do
823 put_ bh (IfacePredTy aq) = do
827 -- Simple compression for common cases of TyConApp
828 put_ bh (IfaceTyConApp IfaceIntTc []) = putByte bh 6
829 put_ bh (IfaceTyConApp IfaceCharTc []) = putByte bh 7
830 put_ bh (IfaceTyConApp IfaceBoolTc []) = putByte bh 8
831 put_ bh (IfaceTyConApp IfaceListTc [ty]) = do { putByte bh 9; put_ bh ty }
832 -- Unit tuple and pairs
833 put_ bh (IfaceTyConApp (IfaceTupTc Boxed 0) []) = putByte bh 10
834 put_ bh (IfaceTyConApp (IfaceTupTc Boxed 2) [t1,t2]) = do { putByte bh 11; put_ bh t1; put_ bh t2 }
836 put_ bh (IfaceTyConApp IfaceLiftedTypeKindTc []) = putByte bh 12
837 put_ bh (IfaceTyConApp IfaceOpenTypeKindTc []) = putByte bh 13
838 put_ bh (IfaceTyConApp IfaceUnliftedTypeKindTc []) = putByte bh 14
839 put_ bh (IfaceTyConApp IfaceUbxTupleKindTc []) = putByte bh 15
840 put_ bh (IfaceTyConApp IfaceArgTypeKindTc []) = putByte bh 16
844 put_ bh (IfaceTyConApp (IfaceTc tc) tys) = do { putByte bh 18; put_ bh tc; put_ bh tys }
845 put_ bh (IfaceTyConApp tc tys) = do { putByte bh 19; put_ bh tc; put_ bh tys }
852 return (IfaceForAllTy aa ab)
854 return (IfaceTyVar ad)
857 return (IfaceAppTy ae af)
860 return (IfaceFunTy ag ah)
862 return (IfacePredTy ap)
864 -- Now the special cases for TyConApp
865 6 -> return (IfaceTyConApp IfaceIntTc [])
866 7 -> return (IfaceTyConApp IfaceCharTc [])
867 8 -> return (IfaceTyConApp IfaceBoolTc [])
868 9 -> do { ty <- get bh; return (IfaceTyConApp IfaceListTc [ty]) }
869 10 -> return (IfaceTyConApp (IfaceTupTc Boxed 0) [])
870 11 -> do { t1 <- get bh; t2 <- get bh; return (IfaceTyConApp (IfaceTupTc Boxed 2) [t1,t2]) }
871 12 -> return (IfaceTyConApp IfaceLiftedTypeKindTc [])
872 13 -> return (IfaceTyConApp IfaceOpenTypeKindTc [])
873 14 -> return (IfaceTyConApp IfaceUnliftedTypeKindTc [])
874 15 -> return (IfaceTyConApp IfaceUbxTupleKindTc [])
875 16 -> return (IfaceTyConApp IfaceArgTypeKindTc [])
877 18 -> do { tc <- get bh; tys <- get bh; return (IfaceTyConApp (IfaceTc tc) tys) }
878 _ -> do { tc <- get bh; tys <- get bh; return (IfaceTyConApp tc tys) }
880 instance Binary IfaceTyCon where
881 -- Int,Char,Bool can't show up here because they can't not be saturated
883 put_ bh IfaceIntTc = putByte bh 1
884 put_ bh IfaceBoolTc = putByte bh 2
885 put_ bh IfaceCharTc = putByte bh 3
886 put_ bh IfaceListTc = putByte bh 4
887 put_ bh IfacePArrTc = putByte bh 5
888 put_ bh IfaceLiftedTypeKindTc = putByte bh 6
889 put_ bh IfaceOpenTypeKindTc = putByte bh 7
890 put_ bh IfaceUnliftedTypeKindTc = putByte bh 8
891 put_ bh IfaceUbxTupleKindTc = putByte bh 9
892 put_ bh IfaceArgTypeKindTc = putByte bh 10
893 put_ bh (IfaceTupTc bx ar) = do { putByte bh 11; put_ bh bx; put_ bh ar }
894 put_ bh (IfaceTc ext) = do { putByte bh 12; put_ bh ext }
899 1 -> return IfaceIntTc
900 2 -> return IfaceBoolTc
901 3 -> return IfaceCharTc
902 4 -> return IfaceListTc
903 5 -> return IfacePArrTc
904 6 -> return IfaceLiftedTypeKindTc
905 7 -> return IfaceOpenTypeKindTc
906 8 -> return IfaceUnliftedTypeKindTc
907 9 -> return IfaceUbxTupleKindTc
908 10 -> return IfaceArgTypeKindTc
909 11 -> do { bx <- get bh; ar <- get bh; return (IfaceTupTc bx ar) }
910 _ -> do { ext <- get bh; return (IfaceTc ext) }
912 instance Binary IfacePredType where
913 put_ bh (IfaceClassP aa ab) = do
917 put_ bh (IfaceIParam ac ad) = do
921 put_ bh (IfaceEqPred ac ad) = do
930 return (IfaceClassP aa ab)
933 return (IfaceIParam ac ad)
936 return (IfaceEqPred ac ad)
937 _ -> panic ("get IfacePredType " ++ show h)
939 -------------------------------------------------------------------------
940 -- IfaceExpr and friends
941 -------------------------------------------------------------------------
943 instance Binary IfaceExpr where
944 put_ bh (IfaceLcl aa) = do
947 put_ bh (IfaceType ab) = do
950 put_ bh (IfaceTuple ac ad) = do
954 put_ bh (IfaceLam ae af) = do
958 put_ bh (IfaceApp ag ah) = do
963 put_ bh (IfaceCase ai aj al ak) = do
970 put_ bh (IfaceLet al am) = do
974 put_ bh (IfaceNote an ao) = do
978 put_ bh (IfaceLit ap) = do
981 put_ bh (IfaceFCall as at) = do
985 put_ bh (IfaceExt aa) = do
988 put_ bh (IfaceCast ie ico) = do
992 put_ bh (IfaceTick m ix) = do
1000 return (IfaceLcl aa)
1001 1 -> do ab <- get bh
1002 return (IfaceType ab)
1003 2 -> do ac <- get bh
1005 return (IfaceTuple ac ad)
1006 3 -> do ae <- get bh
1008 return (IfaceLam ae af)
1009 4 -> do ag <- get bh
1011 return (IfaceApp ag ah)
1012 5 -> do ai <- get bh
1018 return (IfaceCase ai aj al ak)
1019 6 -> do al <- get bh
1021 return (IfaceLet al am)
1022 7 -> do an <- get bh
1024 return (IfaceNote an ao)
1025 8 -> do ap <- get bh
1026 return (IfaceLit ap)
1027 9 -> do as <- get bh
1029 return (IfaceFCall as at)
1030 10 -> do aa <- get bh
1031 return (IfaceExt aa)
1032 11 -> do ie <- get bh
1034 return (IfaceCast ie ico)
1035 12 -> do m <- get bh
1037 return (IfaceTick m ix)
1038 _ -> panic ("get IfaceExpr " ++ show h)
1040 instance Binary IfaceConAlt where
1041 put_ bh IfaceDefault = do
1043 put_ bh (IfaceDataAlt aa) = do
1046 put_ bh (IfaceTupleAlt ab) = do
1049 put_ bh (IfaceLitAlt ac) = do
1055 0 -> do return IfaceDefault
1056 1 -> do aa <- get bh
1057 return (IfaceDataAlt aa)
1058 2 -> do ab <- get bh
1059 return (IfaceTupleAlt ab)
1060 _ -> do ac <- get bh
1061 return (IfaceLitAlt ac)
1063 instance Binary IfaceBinding where
1064 put_ bh (IfaceNonRec aa ab) = do
1068 put_ bh (IfaceRec ac) = do
1074 0 -> do aa <- get bh
1076 return (IfaceNonRec aa ab)
1077 _ -> do ac <- get bh
1078 return (IfaceRec ac)
1080 instance Binary IfaceIdInfo where
1081 put_ bh NoInfo = putByte bh 0
1082 put_ bh (HasInfo i) = do
1084 lazyPut bh i -- NB lazyPut
1090 _ -> do info <- lazyGet bh -- NB lazyGet
1091 return (HasInfo info)
1093 instance Binary IfaceInfoItem where
1094 put_ bh (HsArity aa) = do
1097 put_ bh (HsStrictness ab) = do
1100 put_ bh (HsUnfold ad) = do
1103 put_ bh (HsInline ad) = do
1106 put_ bh HsNoCafRefs = do
1108 put_ bh (HsWorker ae af) = do
1115 0 -> do aa <- get bh
1117 1 -> do ab <- get bh
1118 return (HsStrictness ab)
1119 2 -> do ad <- get bh
1120 return (HsUnfold ad)
1121 3 -> do ad <- get bh
1122 return (HsInline ad)
1123 4 -> do return HsNoCafRefs
1124 _ -> do ae <- get bh
1126 return (HsWorker ae af)
1128 instance Binary IfaceNote where
1129 put_ bh (IfaceSCC aa) = do
1132 put_ bh IfaceInlineMe = do
1134 put_ bh (IfaceCoreNote s) = do
1140 0 -> do aa <- get bh
1141 return (IfaceSCC aa)
1142 3 -> do return IfaceInlineMe
1143 4 -> do ac <- get bh
1144 return (IfaceCoreNote ac)
1145 _ -> panic ("get IfaceNote " ++ show h)
1147 -------------------------------------------------------------------------
1148 -- IfaceDecl and friends
1149 -------------------------------------------------------------------------
1151 -- A bit of magic going on here: there's no need to store the OccName
1152 -- for a decl on the disk, since we can infer the namespace from the
1153 -- context; however it is useful to have the OccName in the IfaceDecl
1154 -- to avoid re-building it in various places. So we build the OccName
1155 -- when de-serialising.
1157 instance Binary IfaceDecl where
1158 put_ bh (IfaceId name ty idinfo) = do
1160 put_ bh (occNameFS name)
1163 put_ _ (IfaceForeign _ _) =
1164 error "Binary.put_(IfaceDecl): IfaceForeign"
1165 put_ bh (IfaceData a1 a2 a3 a4 a5 a6 a7 a8) = do
1167 put_ bh (occNameFS a1)
1175 put_ bh (IfaceSyn a1 a2 a3 a4 a5) = do
1177 put_ bh (occNameFS a1)
1182 put_ bh (IfaceClass a1 a2 a3 a4 a5 a6 a7) = do
1185 put_ bh (occNameFS a2)
1194 0 -> do name <- get bh
1197 occ <- return $! mkOccNameFS varName name
1198 return (IfaceId occ ty idinfo)
1199 1 -> error "Binary.get(TyClDecl): ForeignType"
1209 occ <- return $! mkOccNameFS tcName a1
1210 return (IfaceData occ a2 a3 a4 a5 a6 a7 a8)
1217 occ <- return $! mkOccNameFS tcName a1
1218 return (IfaceSyn occ a2 a3 a4 a5)
1227 occ <- return $! mkOccNameFS clsName a2
1228 return (IfaceClass a1 occ a3 a4 a5 a6 a7)
1230 instance Binary IfaceInst where
1231 put_ bh (IfaceInst cls tys dfun flag orph) = do
1237 get bh = do cls <- get bh
1242 return (IfaceInst cls tys dfun flag orph)
1244 instance Binary IfaceFamInst where
1245 put_ bh (IfaceFamInst fam tys tycon) = do
1249 get bh = do fam <- get bh
1252 return (IfaceFamInst fam tys tycon)
1254 instance Binary OverlapFlag where
1255 put_ bh NoOverlap = putByte bh 0
1256 put_ bh OverlapOk = putByte bh 1
1257 put_ bh Incoherent = putByte bh 2
1258 get bh = do h <- getByte bh
1260 0 -> return NoOverlap
1261 1 -> return OverlapOk
1262 2 -> return Incoherent
1263 _ -> panic ("get OverlapFlag " ++ show h)
1265 instance Binary IfaceConDecls where
1266 put_ bh IfAbstractTyCon = putByte bh 0
1267 put_ bh IfOpenDataTyCon = putByte bh 1
1268 put_ bh (IfDataTyCon cs) = do { putByte bh 2
1270 put_ bh (IfNewTyCon c) = do { putByte bh 3
1275 0 -> return IfAbstractTyCon
1276 1 -> return IfOpenDataTyCon
1277 2 -> do cs <- get bh
1278 return (IfDataTyCon cs)
1279 _ -> do aa <- get bh
1280 return (IfNewTyCon aa)
1282 instance Binary IfaceConDecl where
1283 put_ bh (IfCon a1 a2 a3 a4 a5 a6 a7 a8 a9) = do
1293 get bh = do a1 <- get bh
1302 return (IfCon a1 a2 a3 a4 a5 a6 a7 a8 a9)
1304 instance Binary IfaceClassOp where
1305 put_ bh (IfaceClassOp n def ty) = do
1306 put_ bh (occNameFS n)
1313 occ <- return $! mkOccNameFS varName n
1314 return (IfaceClassOp occ def ty)
1316 instance Binary IfaceRule where
1317 put_ bh (IfaceRule a1 a2 a3 a4 a5 a6 a7) = do
1333 return (IfaceRule a1 a2 a3 a4 a5 a6 a7)
1335 instance Binary IfaceVectInfo where
1336 put_ bh (IfaceVectInfo a1 a2 a3) = do
1344 return (IfaceVectInfo a1 a2 a3)