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 TyCon ( DataConDetails(..) )
18 import Class ( DefMeth(..) )
20 import Module ( moduleName, mkModule )
21 import DriverState ( v_Build_tag )
22 import CmdLineOpts ( opt_HiVersion )
23 import Kind ( Kind(..) )
29 import EXCEPTION ( throwDyn )
33 #include "HsVersions.h"
35 -- ---------------------------------------------------------------------------
36 writeBinIface :: FilePath -> ModIface -> IO ()
37 writeBinIface hi_path mod_iface
38 = putBinFileWithDict hi_path mod_iface
40 readBinIface :: FilePath -> IO ModIface
41 readBinIface hi_path = getBinFileWithDict hi_path
44 -- %*********************************************************
46 -- All the Binary instances
48 -- %*********************************************************
51 {-! for IPName derive: Binary !-}
52 {-! for Fixity derive: Binary !-}
53 {-! for FixityDirection derive: Binary !-}
54 {-! for NewOrData derive: Binary !-}
55 {-! for Boxity derive: Binary !-}
56 {-! for StrictnessMark derive: Binary !-}
57 {-! for Activation derive: Binary !-}
60 {-! for Demand derive: Binary !-}
61 {-! for Demands derive: Binary !-}
62 {-! for DmdResult derive: Binary !-}
63 {-! for StrictSig derive: Binary !-}
66 {-! for DataConDetails derive: Binary !-}
69 {-! for DefMeth derive: Binary !-}
72 {-! for HsPred derive: Binary !-}
73 {-! for HsType derive: Binary !-}
74 {-! for TupCon derive: Binary !-}
75 {-! for HsTyVarBndr derive: Binary !-}
78 {-! for UfExpr derive: Binary !-}
79 {-! for UfConAlt derive: Binary !-}
80 {-! for UfBinding derive: Binary !-}
81 {-! for UfBinder derive: Binary !-}
82 {-! for HsIdInfo derive: Binary !-}
83 {-! for UfNote derive: Binary !-}
86 {-! for ConDetails derive: Binary !-}
87 {-! for BangType derive: Binary !-}
90 {-! for IsCafCC derive: Binary !-}
91 {-! for IsDupdCC derive: Binary !-}
92 {-! for CostCentre derive: Binary !-}
96 -- ---------------------------------------------------------------------------
97 -- Reading a binary interface into ParsedIface
99 instance Binary ModIface where
102 mi_mod_vers = mod_vers,
103 mi_package = pkg_name,
107 mi_exports = exports,
108 mi_exp_vers = exp_vers,
109 mi_fixities = fixities,
110 mi_deprecs = deprecs,
114 mi_rule_vers = rule_vers }) = do
115 put_ bh (show opt_HiVersion)
116 build_tag <- readIORef v_Build_tag
119 put_ bh (moduleName mod)
135 let our_ver = show opt_HiVersion
136 when (check_ver /= our_ver) $
137 -- use userError because this will be caught by readIface
138 -- which will emit an error msg containing the iface module name.
139 throwDyn (ProgramError (
140 "mismatched interface file versions: expected "
141 ++ our_ver ++ ", found " ++ check_ver))
144 ignore_way <- readIORef v_IgnoreHiWay
145 build_tag <- readIORef v_Build_tag
146 when (not ignore_way && check_way /= build_tag) $
147 -- use userError because this will be caught by readIface
148 -- which will emit an error msg containing the iface module name.
149 throwDyn (ProgramError (
150 "mismatched interface file ways: expected "
151 ++ build_tag ++ ", found " ++ check_way))
159 usages <- {-# SCC "bin_usages" #-} lazyGet bh
160 exports <- {-# SCC "bin_exports" #-} get bh
162 fixities <- {-# SCC "bin_fixities" #-} get bh
163 deprecs <- {-# SCC "bin_deprecs" #-} lazyGet bh
164 decls <- {-# SCC "bin_tycldecls" #-} get bh
165 insts <- {-# SCC "bin_insts" #-} get bh
166 rules <- {-# SCC "bin_rules" #-} lazyGet bh
169 mi_package = pkg_name,
170 mi_module = mkModule pkg_name mod_name,
171 -- We write the module as a ModuleName, becuase whether
172 -- or not it's a home-package module depends on the importer
173 -- mkModule reconstructs the Module, by comparing the static
174 -- opt_InPackage flag with the package name in the interface file
175 mi_mod_vers = mod_vers,
176 mi_boot = False, -- Binary interfaces are never .hi-boot files!
180 mi_exports = exports,
181 mi_exp_vers = exp_vers,
182 mi_fixities = fixities,
183 mi_deprecs = deprecs,
187 mi_rule_vers = rule_vers,
188 -- And build the cached values
189 mi_dep_fn = mkIfaceDepCache deprecs,
190 mi_fix_fn = mkIfaceFixCache fixities,
191 mi_ver_fn = mkIfaceVerCache decls })
193 GLOBAL_VAR(v_IgnoreHiWay, False, Bool)
195 -------------------------------------------------------------------------
196 -- Types from: HscTypes
197 -------------------------------------------------------------------------
199 instance Binary Dependencies where
200 put_ bh deps = do put_ bh (dep_mods deps)
201 put_ bh (dep_pkgs deps)
202 put_ bh (dep_orphs deps)
204 get bh = do ms <- get bh
207 return (Deps { dep_mods = ms, dep_pkgs = ps, dep_orphs = os })
209 instance (Binary name) => Binary (GenAvailInfo name) where
210 put_ bh (Avail aa) = do
213 put_ bh (AvailTC ab ac) = do
224 return (AvailTC ab ac)
226 instance Binary Usage where
228 put_ bh (usg_name usg)
229 put_ bh (usg_mod usg)
230 put_ bh (usg_exports usg)
231 put_ bh (usg_entities usg)
232 put_ bh (usg_rules usg)
240 return (Usage { usg_name = nm, usg_mod = mod,
241 usg_exports = exps, usg_entities = ents,
244 instance Binary a => Binary (Deprecs a) where
245 put_ bh NoDeprecs = putByte bh 0
246 put_ bh (DeprecAll t) = do
249 put_ bh (DeprecSome ts) = do
256 0 -> return NoDeprecs
258 return (DeprecAll aa)
260 return (DeprecSome aa)
262 -------------------------------------------------------------------------
263 -- Types from: BasicTypes
264 -------------------------------------------------------------------------
266 instance Binary Activation where
267 put_ bh NeverActive = do
269 put_ bh AlwaysActive = do
271 put_ bh (ActiveBefore aa) = do
274 put_ bh (ActiveAfter ab) = do
280 0 -> do return NeverActive
281 1 -> do return AlwaysActive
283 return (ActiveBefore aa)
285 return (ActiveAfter ab)
287 instance Binary StrictnessMark where
288 put_ bh MarkedStrict = do
290 put_ bh MarkedUnboxed = do
292 put_ bh NotMarkedStrict = do
297 0 -> do return MarkedStrict
298 1 -> do return MarkedUnboxed
299 _ -> do return NotMarkedStrict
301 instance Binary Boxity where
310 _ -> do return Unboxed
312 instance Binary TupCon where
313 put_ bh (TupCon ab ac) = do
319 return (TupCon ab ac)
321 instance Binary NewOrData where
324 put_ bh DataType = do
329 0 -> do return NewType
330 _ -> do return DataType
332 instance Binary RecFlag where
333 put_ bh Recursive = do
335 put_ bh NonRecursive = do
340 0 -> do return Recursive
341 _ -> do return NonRecursive
343 instance Binary DefMeth where
344 put_ bh NoDefMeth = putByte bh 0
345 put_ bh DefMeth = putByte bh 1
346 put_ bh GenDefMeth = putByte bh 2
350 0 -> return NoDefMeth
352 _ -> return GenDefMeth
354 instance Binary FixityDirection where
364 0 -> do return InfixL
365 1 -> do return InfixR
366 _ -> do return InfixN
368 instance Binary Fixity where
369 put_ bh (Fixity aa ab) = do
375 return (Fixity aa ab)
377 instance (Binary name) => Binary (IPName name) where
378 put_ bh (Dupable aa) = do
381 put_ bh (Linear ab) = do
392 -------------------------------------------------------------------------
393 -- Types from: Demand
394 -------------------------------------------------------------------------
396 instance Binary DmdType where
397 -- Ignore DmdEnv when spitting out the DmdType
398 put bh (DmdType _ ds dr) = do p <- put bh ds; put bh dr; return (castBin p)
399 get bh = do ds <- get bh; dr <- get bh; return (DmdType emptyVarEnv ds dr)
401 instance Binary Demand where
406 put_ bh (Call aa) = do
409 put_ bh (Eval ab) = do
412 put_ bh (Defer ac) = do
415 put_ bh (Box ad) = do
435 instance Binary Demands where
436 put_ bh (Poly aa) = do
439 put_ bh (Prod ab) = do
450 instance Binary DmdResult where
460 0 -> do return TopRes
461 1 -> do return RetCPR -- Really use RetCPR even if -fcpr-off
462 -- The wrapper was generated for CPR in
463 -- the imported module!
464 _ -> do return BotRes
466 instance Binary StrictSig where
467 put_ bh (StrictSig aa) = do
471 return (StrictSig aa)
474 -------------------------------------------------------------------------
475 -- Types from: CostCentre
476 -------------------------------------------------------------------------
478 instance Binary IsCafCC where
481 put_ bh NotCafCC = do
487 _ -> do return NotCafCC
489 instance Binary IsDupdCC where
490 put_ bh OriginalCC = do
497 0 -> do return OriginalCC
498 _ -> do return DupdCC
500 instance Binary CostCentre where
501 put_ bh NoCostCentre = do
503 put_ bh (NormalCC aa ab ac ad) = do
509 put_ bh (AllCafsCC ae) = do
515 0 -> do return NoCostCentre
520 return (NormalCC aa ab ac ad)
522 return (AllCafsCC ae)
524 -------------------------------------------------------------------------
525 -- IfaceTypes and friends
526 -------------------------------------------------------------------------
528 instance Binary IfaceExtName where
529 put_ bh (ExtPkg mod occ) = do
533 put_ bh (HomePkg mod occ vers) = do
538 put_ bh (LocalTop occ) = do
541 put_ bh (LocalTopSub occ _) = do -- Write LocalTopSub just like LocalTop
548 0 -> do mod <- get bh
550 return (ExtPkg mod occ)
551 1 -> do mod <- get bh
554 return (HomePkg mod occ vers)
555 _ -> do occ <- get bh
556 return (LocalTop occ)
558 instance Binary IfaceBndr where
559 put_ bh (IfaceIdBndr aa) = do
562 put_ bh (IfaceTvBndr ab) = do
569 return (IfaceIdBndr aa)
571 return (IfaceTvBndr ab)
573 instance Binary Kind where
574 put_ bh LiftedTypeKind = putByte bh 0
575 put_ bh UnliftedTypeKind = putByte bh 1
576 put_ bh OpenTypeKind = putByte bh 2
577 put_ bh ArgTypeKind = putByte bh 3
578 put_ bh UbxTupleKind = putByte bh 4
579 put_ bh (FunKind k1 k2) = do
583 put_ bh (KindVar kv) = pprPanic "BinIface.put_: kind var" (ppr kv)
588 0 -> return LiftedTypeKind
589 1 -> return UnliftedTypeKind
590 2 -> return OpenTypeKind
591 3 -> return ArgTypeKind
592 4 -> return UbxTupleKind
595 return (FunKind k1 k2)
597 instance Binary IfaceType where
598 put_ bh (IfaceForAllTy aa ab) = do
602 put_ bh (IfaceTyVar ad) = do
605 put_ bh (IfaceAppTy ae af) = do
609 put_ bh (IfaceFunTy ag ah) = do
613 put_ bh (IfacePredTy aq) = do
617 -- Simple compression for common cases of TyConApp
618 put_ bh (IfaceTyConApp IfaceIntTc []) = putByte bh 6
619 put_ bh (IfaceTyConApp IfaceCharTc []) = putByte bh 7
620 put_ bh (IfaceTyConApp IfaceBoolTc []) = putByte bh 8
621 put_ bh (IfaceTyConApp IfaceListTc [ty]) = do { putByte bh 9; put_ bh ty }
622 -- Unit tuple and pairs
623 put_ bh (IfaceTyConApp (IfaceTupTc Boxed 0) []) = putByte bh 10
624 put_ bh (IfaceTyConApp (IfaceTupTc Boxed 2) [t1,t2]) = do { putByte bh 11; put_ bh t1; put_ bh t2 }
626 put_ bh (IfaceTyConApp (IfaceTc tc) tys) = do { putByte bh 12; put_ bh tc; put_ bh tys }
627 put_ bh (IfaceTyConApp tc tys) = do { putByte bh 13; put_ bh tc; put_ bh tys }
634 return (IfaceForAllTy aa ab)
636 return (IfaceTyVar ad)
639 return (IfaceAppTy ae af)
642 return (IfaceFunTy ag ah)
644 return (IfacePredTy ap)
646 -- Now the special cases for TyConApp
647 6 -> return (IfaceTyConApp IfaceIntTc [])
648 7 -> return (IfaceTyConApp IfaceCharTc [])
649 8 -> return (IfaceTyConApp IfaceBoolTc [])
650 9 -> do { ty <- get bh; return (IfaceTyConApp IfaceListTc [ty]) }
651 10 -> return (IfaceTyConApp (IfaceTupTc Boxed 0) [])
652 11 -> do { t1 <- get bh; t2 <- get bh; return (IfaceTyConApp (IfaceTupTc Boxed 2) [t1,t2]) }
653 12 -> do { tc <- get bh; tys <- get bh; return (IfaceTyConApp (IfaceTc tc) tys) }
654 _ -> do { tc <- get bh; tys <- get bh; return (IfaceTyConApp tc tys) }
656 instance Binary IfaceTyCon where
657 -- Int,Char,Bool can't show up here because they can't not be saturated
658 put_ bh IfaceListTc = putByte bh 1
659 put_ bh IfacePArrTc = putByte bh 2
660 put_ bh (IfaceTupTc bx ar) = do { putByte bh 3; put_ bh bx; put_ bh ar }
661 put_ bh tc = pprPanic "BinIface.put:" (ppr tc) -- Dealt with by the IfaceType instance
666 1 -> return IfaceListTc
667 2 -> return IfacePArrTc
668 _ -> do { bx <- get bh; ar <- get bh; return (IfaceTupTc bx ar) }
670 instance Binary IfacePredType where
671 put_ bh (IfaceClassP aa ab) = do
675 put_ bh (IfaceIParam ac ad) = do
684 return (IfaceClassP aa ab)
687 return (IfaceIParam ac ad)
689 -------------------------------------------------------------------------
690 -- IfaceExpr and friends
691 -------------------------------------------------------------------------
693 instance Binary IfaceExpr where
694 put_ bh (IfaceLcl aa) = do
697 put_ bh (IfaceType ab) = do
700 put_ bh (IfaceTuple ac ad) = do
704 put_ bh (IfaceLam ae af) = do
708 put_ bh (IfaceApp ag ah) = do
712 put_ bh (IfaceCase ai aj ak) = do
717 put_ bh (IfaceLet al am) = do
721 put_ bh (IfaceNote an ao) = do
725 put_ bh (IfaceLit ap) = do
728 put_ bh (IfaceFCall as at) = do
732 put_ bh (IfaceExt aa) = do
741 return (IfaceType ab)
744 return (IfaceTuple ac ad)
747 return (IfaceLam ae af)
750 return (IfaceApp ag ah)
754 return (IfaceCase ai aj ak)
757 return (IfaceLet al am)
760 return (IfaceNote an ao)
765 return (IfaceFCall as at)
769 instance Binary IfaceConAlt where
770 put_ bh IfaceDefault = do
772 put_ bh (IfaceDataAlt aa) = do
775 put_ bh (IfaceTupleAlt ab) = do
778 put_ bh (IfaceLitAlt ac) = do
784 0 -> do return IfaceDefault
786 return (IfaceDataAlt aa)
788 return (IfaceTupleAlt ab)
790 return (IfaceLitAlt ac)
792 instance Binary IfaceBinding where
793 put_ bh (IfaceNonRec aa ab) = do
797 put_ bh (IfaceRec ac) = do
805 return (IfaceNonRec aa ab)
809 instance Binary IfaceIdInfo where
810 put_ bh NoInfo = putByte bh 0
811 put_ bh (HasInfo i) = do
814 put_ bh DiscardedInfo = panic "BinIface:DiscardedInfo"
820 _ -> do info <- lazyGet bh
821 return (HasInfo info)
823 instance Binary IfaceInfoItem where
824 put_ bh (HsArity aa) = do
827 put_ bh (HsStrictness ab) = do
830 put_ bh (HsUnfold ac ad) = do
834 put_ bh HsNoCafRefs = do
836 put_ bh (HsWorker ae af) = do
846 return (HsStrictness ab)
849 return (HsUnfold ac ad)
850 3 -> do return HsNoCafRefs
853 return (HsWorker ae af)
855 instance Binary IfaceNote where
856 put_ bh (IfaceSCC aa) = do
859 put_ bh (IfaceCoerce ab) = do
862 put_ bh IfaceInlineCall = do
864 put_ bh IfaceInlineMe = do
866 put_ bh (IfaceCoreNote s) = do
875 return (IfaceCoerce ab)
876 2 -> do return IfaceInlineCall
877 3 -> do return IfaceInlineMe
879 return (IfaceCoreNote ac)
882 -------------------------------------------------------------------------
883 -- IfaceDecl and friends
884 -------------------------------------------------------------------------
886 instance Binary IfaceDecl where
887 put_ bh (IfaceId name ty idinfo) = do
892 put_ bh (IfaceForeign ae af) =
893 error "Binary.put_(IfaceDecl): IfaceForeign"
894 put_ bh (IfaceData a1 a2 a3 a4 a5 a6 a7 a8) = do
905 put_ bh (IfaceSyn aq ar as at) = do
911 put_ bh (IfaceClass a1 a2 a3 a4 a5 a6 a7) = do
923 0 -> do name <- get bh
926 return (IfaceId name ty idinfo)
927 1 -> error "Binary.get(TyClDecl): ForeignType"
937 return (IfaceData a1 a2 a3 a4 a5 a6 a7 a8)
943 return (IfaceSyn aq ar as at)
952 return (IfaceClass a1 a2 a3 a4 a5 a6 a7)
954 instance Binary IfaceInst where
955 put_ bh (IfaceInst ty dfun) = do
958 get bh = do ty <- get bh
960 return (IfaceInst ty dfun)
962 instance Binary IfaceConDecl where
963 put_ bh (IfaceConDecl a1 a2 a3 a4 a5 a6) = do
977 return (IfaceConDecl a1 a2 a3 a4 a5 a6)
979 instance Binary IfaceClassOp where
980 put_ bh (IfaceClassOp n def ty) = do
988 return (IfaceClassOp n def ty)
990 instance Binary IfaceRule where
991 -- IfaceBuiltinRule should not happen here
992 put_ bh (IfaceRule a1 a2 a3 a4 a5 a6) = do
1006 return (IfaceRule a1 a2 a3 a4 a5 a6)
1008 instance (Binary datacon) => Binary (DataConDetails datacon) where
1009 put_ bh (DataCons aa) = do
1012 put_ bh Unknown = do
1017 0 -> do aa <- get bh
1018 return (DataCons aa)
1019 _ -> do return Unknown