1 {-% DrIFT (Automatic class derivations for Haskell) v1.1 %-}
2 {-% DrIFT (Automatic class derivations for Haskell) v1.1 %-}
4 -- (c) The University of Glasgow 2002
6 -- Binary interface file support.
8 module BinIface ( writeBinIface, readBinIface, v_IgnoreHiWay ) where
10 #include "HsVersions.h"
17 import Packages ( PackageIdH(..) )
18 import Class ( DefMeth(..) )
20 import DriverState ( v_Build_tag )
21 import CmdLineOpts ( opt_HiVersion )
22 import Kind ( Kind(..) )
28 import EXCEPTION ( throwDyn )
32 #include "HsVersions.h"
34 -- ---------------------------------------------------------------------------
35 writeBinIface :: FilePath -> ModIface -> IO ()
36 writeBinIface hi_path mod_iface
37 = putBinFileWithDict hi_path mod_iface
39 readBinIface :: FilePath -> IO ModIface
40 readBinIface hi_path = getBinFileWithDict hi_path
43 -- %*********************************************************
45 -- All the Binary instances
47 -- %*********************************************************
50 {-! for IPName derive: Binary !-}
51 {-! for Fixity derive: Binary !-}
52 {-! for FixityDirection derive: Binary !-}
53 {-! for Boxity derive: Binary !-}
54 {-! for StrictnessMark derive: Binary !-}
55 {-! for Activation derive: Binary !-}
58 {-! for Demand derive: Binary !-}
59 {-! for Demands derive: Binary !-}
60 {-! for DmdResult derive: Binary !-}
61 {-! for StrictSig derive: Binary !-}
64 {-! for DefMeth derive: Binary !-}
67 {-! for HsPred derive: Binary !-}
68 {-! for HsType derive: Binary !-}
69 {-! for TupCon derive: Binary !-}
70 {-! for HsTyVarBndr derive: Binary !-}
73 {-! for UfExpr derive: Binary !-}
74 {-! for UfConAlt derive: Binary !-}
75 {-! for UfBinding derive: Binary !-}
76 {-! for UfBinder derive: Binary !-}
77 {-! for HsIdInfo derive: Binary !-}
78 {-! for UfNote derive: Binary !-}
81 {-! for ConDetails derive: Binary !-}
82 {-! for BangType derive: Binary !-}
85 {-! for IsCafCC derive: Binary !-}
86 {-! for IsDupdCC derive: Binary !-}
87 {-! for CostCentre derive: Binary !-}
91 -- ---------------------------------------------------------------------------
92 -- Reading a binary interface into ParsedIface
94 instance Binary ModIface where
98 mi_mod_vers = mod_vers,
99 mi_package = _, -- we ignore the package on output
103 mi_exports = exports,
104 mi_exp_vers = exp_vers,
105 mi_fixities = fixities,
106 mi_deprecs = deprecs,
110 mi_rule_vers = rule_vers }) = do
111 put_ bh (show opt_HiVersion)
112 build_tag <- readIORef v_Build_tag
131 let our_ver = show opt_HiVersion
132 when (check_ver /= our_ver) $
133 -- use userError because this will be caught by readIface
134 -- which will emit an error msg containing the iface module name.
135 throwDyn (ProgramError (
136 "mismatched interface file versions: expected "
137 ++ our_ver ++ ", found " ++ check_ver))
140 ignore_way <- readIORef v_IgnoreHiWay
141 build_tag <- readIORef v_Build_tag
142 when (not ignore_way && check_way /= build_tag) $
143 -- use userError because this will be caught by readIface
144 -- which will emit an error msg containing the iface module name.
145 throwDyn (ProgramError (
146 "mismatched interface file ways: expected "
147 ++ build_tag ++ ", found " ++ check_way))
154 usages <- {-# SCC "bin_usages" #-} lazyGet bh
155 exports <- {-# SCC "bin_exports" #-} get bh
157 fixities <- {-# SCC "bin_fixities" #-} get bh
158 deprecs <- {-# SCC "bin_deprecs" #-} lazyGet bh
159 decls <- {-# SCC "bin_tycldecls" #-} get bh
160 insts <- {-# SCC "bin_insts" #-} get bh
161 rules <- {-# SCC "bin_rules" #-} lazyGet bh
164 mi_package = HomePackage, -- to be filled in properly later
165 mi_module = mod_name,
167 mi_mod_vers = mod_vers,
171 mi_exports = exports,
172 mi_exp_vers = exp_vers,
173 mi_fixities = fixities,
174 mi_deprecs = deprecs,
178 mi_rule_vers = rule_vers,
179 -- And build the cached values
180 mi_dep_fn = mkIfaceDepCache deprecs,
181 mi_fix_fn = mkIfaceFixCache fixities,
182 mi_ver_fn = mkIfaceVerCache decls })
184 GLOBAL_VAR(v_IgnoreHiWay, False, Bool)
186 -------------------------------------------------------------------------
187 -- Types from: HscTypes
188 -------------------------------------------------------------------------
190 instance Binary Dependencies where
191 put_ bh deps = do put_ bh (dep_mods deps)
192 put_ bh (dep_pkgs deps)
193 put_ bh (dep_orphs deps)
195 get bh = do ms <- get bh
198 return (Deps { dep_mods = ms, dep_pkgs = ps, dep_orphs = os })
200 instance (Binary name) => Binary (GenAvailInfo name) where
201 put_ bh (Avail aa) = do
204 put_ bh (AvailTC ab ac) = do
215 return (AvailTC ab ac)
217 instance Binary Usage where
219 put_ bh (usg_name usg)
220 put_ bh (usg_mod usg)
221 put_ bh (usg_exports usg)
222 put_ bh (usg_entities usg)
223 put_ bh (usg_rules usg)
231 return (Usage { usg_name = nm, usg_mod = mod,
232 usg_exports = exps, usg_entities = ents,
235 instance Binary a => Binary (Deprecs a) where
236 put_ bh NoDeprecs = putByte bh 0
237 put_ bh (DeprecAll t) = do
240 put_ bh (DeprecSome ts) = do
247 0 -> return NoDeprecs
249 return (DeprecAll aa)
251 return (DeprecSome aa)
253 -------------------------------------------------------------------------
254 -- Types from: BasicTypes
255 -------------------------------------------------------------------------
257 instance Binary Activation where
258 put_ bh NeverActive = do
260 put_ bh AlwaysActive = do
262 put_ bh (ActiveBefore aa) = do
265 put_ bh (ActiveAfter ab) = do
271 0 -> do return NeverActive
272 1 -> do return AlwaysActive
274 return (ActiveBefore aa)
276 return (ActiveAfter ab)
278 instance Binary StrictnessMark where
279 put_ bh MarkedStrict = do
281 put_ bh MarkedUnboxed = do
283 put_ bh NotMarkedStrict = do
288 0 -> do return MarkedStrict
289 1 -> do return MarkedUnboxed
290 _ -> do return NotMarkedStrict
292 instance Binary Boxity where
301 _ -> do return Unboxed
303 instance Binary TupCon where
304 put_ bh (TupCon ab ac) = do
310 return (TupCon ab ac)
312 instance Binary RecFlag where
313 put_ bh Recursive = do
315 put_ bh NonRecursive = do
320 0 -> do return Recursive
321 _ -> do return NonRecursive
323 instance Binary DefMeth where
324 put_ bh NoDefMeth = putByte bh 0
325 put_ bh DefMeth = putByte bh 1
326 put_ bh GenDefMeth = putByte bh 2
330 0 -> return NoDefMeth
332 _ -> return GenDefMeth
334 instance Binary FixityDirection where
344 0 -> do return InfixL
345 1 -> do return InfixR
346 _ -> do return InfixN
348 instance Binary Fixity where
349 put_ bh (Fixity aa ab) = do
355 return (Fixity aa ab)
357 instance (Binary name) => Binary (IPName name) where
358 put_ bh (Dupable aa) = do
361 put_ bh (Linear ab) = do
372 -------------------------------------------------------------------------
373 -- Types from: Demand
374 -------------------------------------------------------------------------
376 instance Binary DmdType where
377 -- Ignore DmdEnv when spitting out the DmdType
378 put bh (DmdType _ ds dr) = do p <- put bh ds; put bh dr; return (castBin p)
379 get bh = do ds <- get bh; dr <- get bh; return (DmdType emptyVarEnv ds dr)
381 instance Binary Demand where
386 put_ bh (Call aa) = do
389 put_ bh (Eval ab) = do
392 put_ bh (Defer ac) = do
395 put_ bh (Box ad) = do
415 instance Binary Demands where
416 put_ bh (Poly aa) = do
419 put_ bh (Prod ab) = do
430 instance Binary DmdResult where
440 0 -> do return TopRes
441 1 -> do return RetCPR -- Really use RetCPR even if -fcpr-off
442 -- The wrapper was generated for CPR in
443 -- the imported module!
444 _ -> do return BotRes
446 instance Binary StrictSig where
447 put_ bh (StrictSig aa) = do
451 return (StrictSig aa)
454 -------------------------------------------------------------------------
455 -- Types from: CostCentre
456 -------------------------------------------------------------------------
458 instance Binary IsCafCC where
461 put_ bh NotCafCC = do
467 _ -> do return NotCafCC
469 instance Binary IsDupdCC where
470 put_ bh OriginalCC = do
477 0 -> do return OriginalCC
478 _ -> do return DupdCC
480 instance Binary CostCentre where
481 put_ bh NoCostCentre = do
483 put_ bh (NormalCC aa ab ac ad) = do
489 put_ bh (AllCafsCC ae) = do
495 0 -> do return NoCostCentre
500 return (NormalCC aa ab ac ad)
502 return (AllCafsCC ae)
504 -------------------------------------------------------------------------
505 -- IfaceTypes and friends
506 -------------------------------------------------------------------------
508 instance Binary IfaceExtName where
509 put_ bh (ExtPkg mod occ) = do
513 put_ bh (HomePkg mod occ vers) = do
518 put_ bh (LocalTop occ) = do
521 put_ bh (LocalTopSub occ _) = do -- Write LocalTopSub just like LocalTop
528 0 -> do mod <- get bh
530 return (ExtPkg mod occ)
531 1 -> do mod <- get bh
534 return (HomePkg mod occ vers)
535 _ -> do occ <- get bh
536 return (LocalTop occ)
538 instance Binary IfaceBndr where
539 put_ bh (IfaceIdBndr aa) = do
542 put_ bh (IfaceTvBndr ab) = do
549 return (IfaceIdBndr aa)
551 return (IfaceTvBndr ab)
553 instance Binary Kind where
554 put_ bh LiftedTypeKind = putByte bh 0
555 put_ bh UnliftedTypeKind = putByte bh 1
556 put_ bh OpenTypeKind = putByte bh 2
557 put_ bh ArgTypeKind = putByte bh 3
558 put_ bh UbxTupleKind = putByte bh 4
559 put_ bh (FunKind k1 k2) = do
563 put_ bh (KindVar kv) = pprPanic "BinIface.put_: kind var" (ppr kv)
568 0 -> return LiftedTypeKind
569 1 -> return UnliftedTypeKind
570 2 -> return OpenTypeKind
571 3 -> return ArgTypeKind
572 4 -> return UbxTupleKind
575 return (FunKind k1 k2)
577 instance Binary IfaceType where
578 put_ bh (IfaceForAllTy aa ab) = do
582 put_ bh (IfaceTyVar ad) = do
585 put_ bh (IfaceAppTy ae af) = do
589 put_ bh (IfaceFunTy ag ah) = do
593 put_ bh (IfacePredTy aq) = do
597 -- Simple compression for common cases of TyConApp
598 put_ bh (IfaceTyConApp IfaceIntTc []) = putByte bh 6
599 put_ bh (IfaceTyConApp IfaceCharTc []) = putByte bh 7
600 put_ bh (IfaceTyConApp IfaceBoolTc []) = putByte bh 8
601 put_ bh (IfaceTyConApp IfaceListTc [ty]) = do { putByte bh 9; put_ bh ty }
602 -- Unit tuple and pairs
603 put_ bh (IfaceTyConApp (IfaceTupTc Boxed 0) []) = putByte bh 10
604 put_ bh (IfaceTyConApp (IfaceTupTc Boxed 2) [t1,t2]) = do { putByte bh 11; put_ bh t1; put_ bh t2 }
606 put_ bh (IfaceTyConApp (IfaceTc tc) tys) = do { putByte bh 12; put_ bh tc; put_ bh tys }
607 put_ bh (IfaceTyConApp tc tys) = do { putByte bh 13; put_ bh tc; put_ bh tys }
614 return (IfaceForAllTy aa ab)
616 return (IfaceTyVar ad)
619 return (IfaceAppTy ae af)
622 return (IfaceFunTy ag ah)
624 return (IfacePredTy ap)
626 -- Now the special cases for TyConApp
627 6 -> return (IfaceTyConApp IfaceIntTc [])
628 7 -> return (IfaceTyConApp IfaceCharTc [])
629 8 -> return (IfaceTyConApp IfaceBoolTc [])
630 9 -> do { ty <- get bh; return (IfaceTyConApp IfaceListTc [ty]) }
631 10 -> return (IfaceTyConApp (IfaceTupTc Boxed 0) [])
632 11 -> do { t1 <- get bh; t2 <- get bh; return (IfaceTyConApp (IfaceTupTc Boxed 2) [t1,t2]) }
633 12 -> do { tc <- get bh; tys <- get bh; return (IfaceTyConApp (IfaceTc tc) tys) }
634 _ -> do { tc <- get bh; tys <- get bh; return (IfaceTyConApp tc tys) }
636 instance Binary IfaceTyCon where
637 -- Int,Char,Bool can't show up here because they can't not be saturated
638 put_ bh IfaceListTc = putByte bh 1
639 put_ bh IfacePArrTc = putByte bh 2
640 put_ bh (IfaceTupTc bx ar) = do { putByte bh 3; put_ bh bx; put_ bh ar }
641 put_ bh tc = pprPanic "BinIface.put:" (ppr tc) -- Dealt with by the IfaceType instance
646 1 -> return IfaceListTc
647 2 -> return IfacePArrTc
648 _ -> do { bx <- get bh; ar <- get bh; return (IfaceTupTc bx ar) }
650 instance Binary IfacePredType where
651 put_ bh (IfaceClassP aa ab) = do
655 put_ bh (IfaceIParam ac ad) = do
664 return (IfaceClassP aa ab)
667 return (IfaceIParam ac ad)
669 -------------------------------------------------------------------------
670 -- IfaceExpr and friends
671 -------------------------------------------------------------------------
673 instance Binary IfaceExpr where
674 put_ bh (IfaceLcl aa) = do
677 put_ bh (IfaceType ab) = do
680 put_ bh (IfaceTuple ac ad) = do
684 put_ bh (IfaceLam ae af) = do
688 put_ bh (IfaceApp ag ah) = do
693 put_ bh (IfaceCase ai aj al ak) = do
700 put_ bh (IfaceLet al am) = do
704 put_ bh (IfaceNote an ao) = do
708 put_ bh (IfaceLit ap) = do
711 put_ bh (IfaceFCall as at) = do
715 put_ bh (IfaceExt aa) = do
724 return (IfaceType ab)
727 return (IfaceTuple ac ad)
730 return (IfaceLam ae af)
733 return (IfaceApp ag ah)
740 return (IfaceCase ai aj al ak)
743 return (IfaceLet al am)
746 return (IfaceNote an ao)
751 return (IfaceFCall as at)
755 instance Binary IfaceConAlt where
756 put_ bh IfaceDefault = do
758 put_ bh (IfaceDataAlt aa) = do
761 put_ bh (IfaceTupleAlt ab) = do
764 put_ bh (IfaceLitAlt ac) = do
770 0 -> do return IfaceDefault
772 return (IfaceDataAlt aa)
774 return (IfaceTupleAlt ab)
776 return (IfaceLitAlt ac)
778 instance Binary IfaceBinding where
779 put_ bh (IfaceNonRec aa ab) = do
783 put_ bh (IfaceRec ac) = do
791 return (IfaceNonRec aa ab)
795 instance Binary IfaceIdInfo where
796 put_ bh NoInfo = putByte bh 0
797 put_ bh (HasInfo i) = do
805 _ -> do info <- lazyGet bh
806 return (HasInfo info)
808 instance Binary IfaceInfoItem where
809 put_ bh (HsArity aa) = do
812 put_ bh (HsStrictness ab) = do
815 put_ bh (HsUnfold ac ad) = do
819 put_ bh HsNoCafRefs = do
821 put_ bh (HsWorker ae af) = do
831 return (HsStrictness ab)
834 return (HsUnfold ac ad)
835 3 -> do return HsNoCafRefs
838 return (HsWorker ae af)
840 instance Binary IfaceNote where
841 put_ bh (IfaceSCC aa) = do
844 put_ bh (IfaceCoerce ab) = do
847 put_ bh IfaceInlineCall = do
849 put_ bh IfaceInlineMe = do
851 put_ bh (IfaceCoreNote s) = do
860 return (IfaceCoerce ab)
861 2 -> do return IfaceInlineCall
862 3 -> do return IfaceInlineMe
864 return (IfaceCoreNote ac)
867 -------------------------------------------------------------------------
868 -- IfaceDecl and friends
869 -------------------------------------------------------------------------
871 instance Binary IfaceDecl where
872 put_ bh (IfaceId name ty idinfo) = do
877 put_ bh (IfaceForeign ae af) =
878 error "Binary.put_(IfaceDecl): IfaceForeign"
879 put_ bh (IfaceData a1 a2 a3 a4 a5 a6) = do
888 put_ bh (IfaceSyn aq ar as at) = do
894 put_ bh (IfaceClass a1 a2 a3 a4 a5 a6 a7) = do
906 0 -> do name <- get bh
909 return (IfaceId name ty idinfo)
910 1 -> error "Binary.get(TyClDecl): ForeignType"
918 return (IfaceData a1 a2 a3 a4 a5 a6)
924 return (IfaceSyn aq ar as at)
933 return (IfaceClass a1 a2 a3 a4 a5 a6 a7)
935 instance Binary IfaceInst where
936 put_ bh (IfaceInst ty dfun) = do
939 get bh = do ty <- get bh
941 return (IfaceInst ty dfun)
943 instance Binary IfaceConDecls where
944 put_ bh IfAbstractTyCon = putByte bh 0
945 put_ bh (IfDataTyCon st cs) = do { putByte bh 1
948 put_ bh (IfNewTyCon c) = do { putByte bh 2
953 0 -> return IfAbstractTyCon
956 return (IfDataTyCon st cs)
958 return (IfNewTyCon aa)
960 instance Binary IfaceConDecl where
961 put_ bh (IfVanillaCon a1 a2 a3 a4 a5) = do
968 put_ bh (IfGadtCon a1 a2 a3 a4 a5 a6) = do
984 return (IfVanillaCon a1 a2 a3 a4 a5)
991 return (IfGadtCon a1 a2 a3 a4 a5 a6)
993 instance Binary IfaceClassOp where
994 put_ bh (IfaceClassOp n def ty) = do
1002 return (IfaceClassOp n def ty)
1004 instance Binary IfaceRule where
1005 -- IfaceBuiltinRule should not happen here
1006 put_ bh (IfaceRule a1 a2 a3 a4 a5 a6) = do
1020 return (IfaceRule a1 a2 a3 a4 a5 a6)