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 OccName ( OccName )
22 import DriverState ( v_Build_tag )
23 import CmdLineOpts ( opt_HiVersion )
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 IfaceKind where
574 put_ bh IfaceLiftedTypeKind = putByte bh 0
575 put_ bh IfaceUnliftedTypeKind = putByte bh 1
576 put_ bh IfaceOpenTypeKind = putByte bh 2
577 put_ bh (IfaceFunKind k1 k2) = do
584 0 -> return IfaceLiftedTypeKind
585 1 -> return IfaceUnliftedTypeKind
586 2 -> return IfaceOpenTypeKind
589 return (IfaceFunKind k1 k2)
591 instance Binary IfaceType where
592 put_ bh (IfaceForAllTy aa ab) = do
596 put_ bh (IfaceTyVar ad) = do
599 put_ bh (IfaceAppTy ae af) = do
603 put_ bh (IfaceFunTy ag ah) = do
607 put_ bh (IfacePredTy aq) = do
611 -- Simple compression for common cases of TyConApp
612 put_ bh (IfaceTyConApp IfaceIntTc []) = putByte bh 6
613 put_ bh (IfaceTyConApp IfaceCharTc []) = putByte bh 7
614 put_ bh (IfaceTyConApp IfaceBoolTc []) = putByte bh 8
615 put_ bh (IfaceTyConApp IfaceListTc [ty]) = do { putByte bh 9; put_ bh ty }
616 -- Unit tuple and pairs
617 put_ bh (IfaceTyConApp (IfaceTupTc Boxed 0) []) = putByte bh 10
618 put_ bh (IfaceTyConApp (IfaceTupTc Boxed 2) [t1,t2]) = do { putByte bh 11; put_ bh t1; put_ bh t2 }
620 put_ bh (IfaceTyConApp (IfaceTc tc) tys) = do { putByte bh 12; put_ bh tc; put_ bh tys }
621 put_ bh (IfaceTyConApp tc tys) = do { putByte bh 13; put_ bh tc; put_ bh tys }
628 return (IfaceForAllTy aa ab)
630 return (IfaceTyVar ad)
633 return (IfaceAppTy ae af)
636 return (IfaceFunTy ag ah)
638 return (IfacePredTy ap)
640 -- Now the special cases for TyConApp
641 6 -> return (IfaceTyConApp IfaceIntTc [])
642 7 -> return (IfaceTyConApp IfaceCharTc [])
643 8 -> return (IfaceTyConApp IfaceBoolTc [])
644 9 -> do { ty <- get bh; return (IfaceTyConApp IfaceListTc [ty]) }
645 10 -> return (IfaceTyConApp (IfaceTupTc Boxed 0) [])
646 11 -> do { t1 <- get bh; t2 <- get bh; return (IfaceTyConApp (IfaceTupTc Boxed 2) [t1,t2]) }
647 12 -> do { tc <- get bh; tys <- get bh; return (IfaceTyConApp (IfaceTc tc) tys) }
648 _ -> do { tc <- get bh; tys <- get bh; return (IfaceTyConApp tc tys) }
650 instance Binary IfaceTyCon where
651 -- Int,Char,Bool can't show up here because they can't not be saturated
652 put_ bh IfaceListTc = putByte bh 1
653 put_ bh IfacePArrTc = putByte bh 2
654 put_ bh (IfaceTupTc bx ar) = do { putByte bh 3; put_ bh bx; put_ bh ar }
655 put_ bh tc = pprPanic "BinIface.put:" (ppr tc) -- Dealt with by the IfaceType instance
660 1 -> return IfaceListTc
661 2 -> return IfacePArrTc
662 _ -> do { bx <- get bh; ar <- get bh; return (IfaceTupTc bx ar) }
664 instance Binary IfacePredType where
665 put_ bh (IfaceClassP aa ab) = do
669 put_ bh (IfaceIParam ac ad) = do
678 return (IfaceClassP aa ab)
681 return (IfaceIParam ac ad)
683 -------------------------------------------------------------------------
684 -- IfaceExpr and friends
685 -------------------------------------------------------------------------
687 instance Binary IfaceExpr where
688 put_ bh (IfaceLcl aa) = do
691 put_ bh (IfaceType ab) = do
694 put_ bh (IfaceTuple ac ad) = do
698 put_ bh (IfaceLam ae af) = do
702 put_ bh (IfaceApp ag ah) = do
706 put_ bh (IfaceCase ai aj ak) = do
711 put_ bh (IfaceLet al am) = do
715 put_ bh (IfaceNote an ao) = do
719 put_ bh (IfaceLit ap) = do
722 put_ bh (IfaceFCall as at) = do
726 put_ bh (IfaceExt aa) = do
735 return (IfaceType ab)
738 return (IfaceTuple ac ad)
741 return (IfaceLam ae af)
744 return (IfaceApp ag ah)
748 return (IfaceCase ai aj ak)
751 return (IfaceLet al am)
754 return (IfaceNote an ao)
759 return (IfaceFCall as at)
763 instance Binary IfaceConAlt where
764 put_ bh IfaceDefault = do
766 put_ bh (IfaceDataAlt aa) = do
769 put_ bh (IfaceTupleAlt ab) = do
772 put_ bh (IfaceLitAlt ac) = do
778 0 -> do return IfaceDefault
780 return (IfaceDataAlt aa)
782 return (IfaceTupleAlt ab)
784 return (IfaceLitAlt ac)
786 instance Binary IfaceBinding where
787 put_ bh (IfaceNonRec aa ab) = do
791 put_ bh (IfaceRec ac) = do
799 return (IfaceNonRec aa ab)
803 instance Binary IfaceIdInfo where
804 put_ bh NoInfo = putByte bh 0
805 put_ bh (HasInfo i) = do
808 put_ bh DiscardedInfo = panic "BinIface:DiscardedInfo"
814 _ -> do info <- lazyGet bh
815 return (HasInfo info)
817 instance Binary IfaceInfoItem where
818 put_ bh (HsArity aa) = do
821 put_ bh (HsStrictness ab) = do
824 put_ bh (HsUnfold ac ad) = do
828 put_ bh HsNoCafRefs = do
830 put_ bh (HsWorker ae af) = do
840 return (HsStrictness ab)
843 return (HsUnfold ac ad)
844 3 -> do return HsNoCafRefs
847 return (HsWorker ae af)
849 instance Binary IfaceNote where
850 put_ bh (IfaceSCC aa) = do
853 put_ bh (IfaceCoerce ab) = do
856 put_ bh IfaceInlineCall = do
858 put_ bh IfaceInlineMe = do
860 put_ bh (IfaceCoreNote s) = do
869 return (IfaceCoerce ab)
870 2 -> do return IfaceInlineCall
871 3 -> do return IfaceInlineMe
873 return (IfaceCoreNote ac)
876 -------------------------------------------------------------------------
877 -- IfaceDecl and friends
878 -------------------------------------------------------------------------
880 instance Binary IfaceDecl where
881 put_ bh (IfaceId name ty idinfo) = do
886 put_ bh (IfaceForeign ae af) =
887 error "Binary.put_(IfaceDecl): IfaceForeign"
888 put_ bh (IfaceData a1 a2 a3 a4 a5 a6 a7 a8) = do
899 put_ bh (IfaceSyn aq ar as at) = do
905 put_ bh (IfaceClass a1 a2 a3 a4 a5 a6 a7) = do
917 0 -> do name <- get bh
920 return (IfaceId name ty idinfo)
921 1 -> error "Binary.get(TyClDecl): ForeignType"
931 return (IfaceData a1 a2 a3 a4 a5 a6 a7 a8)
937 return (IfaceSyn aq ar as at)
946 return (IfaceClass a1 a2 a3 a4 a5 a6 a7)
948 instance Binary IfaceInst where
949 put_ bh (IfaceInst ty dfun) = do
952 get bh = do ty <- get bh
954 return (IfaceInst ty dfun)
956 instance Binary IfaceConDecl where
957 put_ bh (IfaceConDecl a1 a2 a3 a4 a5 a6) = do
971 return (IfaceConDecl a1 a2 a3 a4 a5 a6)
973 instance Binary IfaceClassOp where
974 put_ bh (IfaceClassOp n def ty) = do
982 return (IfaceClassOp n def ty)
984 instance Binary IfaceRule where
985 -- IfaceBuiltinRule should not happen here
986 put_ bh (IfaceRule a1 a2 a3 a4 a5 a6) = do
1000 return (IfaceRule a1 a2 a3 a4 a5 a6)
1002 instance (Binary datacon) => Binary (DataConDetails datacon) where
1003 put_ bh (DataCons aa) = do
1006 put_ bh Unknown = do
1011 0 -> do aa <- get bh
1012 return (DataCons aa)
1013 _ -> do return Unknown