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_IgnoreHiVersion ) where
10 #include "HsVersions.h"
19 import HsPat ( HsConDetails(..) )
24 import RdrName ( mkRdrUnqual, mkRdrQual )
25 import Name ( Name, nameOccName, nameModule_maybe )
26 import NameEnv ( NameEnv, lookupNameEnv, nameEnvElts )
27 import Module ( moduleName )
28 import OccName ( OccName )
30 import DriverState ( v_Build_tag )
31 import CmdLineOpts ( opt_HiVersion )
38 import EXCEPTION ( throwDyn )
41 #include "HsVersions.h"
43 -- ---------------------------------------------------------------------------
44 -- We write out a ModIface, but read it in as a ParsedIface.
45 -- There are some big differences, and some subtle ones. We do most
46 -- of the conversion on the way out, so there is minimal fuss when we
47 -- read it back in again (see RnMonad.lhs)
49 -- The main difference is that all Names in a ModIface are RdrNames in
50 -- a ParsedIface, so when writing out a Name in binary we make sure it
51 -- is binary-compatible with a RdrName.
53 -- Other subtle differences:
54 -- - pi_mod is a ModuleName, but mi_mod is a Module. Hence we put
55 -- Modules as ModuleNames.
56 -- - pi_exports and pi_usages, Names have
57 -- to be converted to OccNames.
58 -- - pi_fixity is a NameEnv in ModIface,
59 -- but a list of (Name,Fixity) pairs in ParsedIface.
60 -- - versioning is totally different.
61 -- - deprecations are different.
63 writeBinIface :: FilePath -> ModIface -> IO ()
64 writeBinIface hi_path mod_iface
65 = putBinFileWithDict hi_path (mi_module mod_iface) mod_iface
67 readBinIface :: FilePath -> IO ParsedIface
68 readBinIface hi_path = getBinFileWithDict hi_path
71 -- %*********************************************************
73 -- All the Binary instances
75 -- %*********************************************************
78 {-! for IPName derive: Binary !-}
79 {-! for Fixity derive: Binary !-}
80 {-! for FixityDirection derive: Binary !-}
81 {-! for NewOrData derive: Binary !-}
82 {-! for Boxity derive: Binary !-}
83 {-! for StrictnessMark derive: Binary !-}
84 {-! for Activation derive: Binary !-}
86 instance Binary Name where
87 -- we must print these as RdrNames, because that's how they will be read in
89 = case nameModule_maybe name of
91 | this_mod == mod -> put_ bh (mkRdrUnqual occ)
92 | otherwise -> put_ bh (mkRdrQual (moduleName mod) occ)
93 _ -> put_ bh (mkRdrUnqual occ)
95 occ = nameOccName name
96 (this_mod,_,_,_) = getUserData bh
98 get bh = error "can't Binary.get a Name"
101 {-! for Demand derive: Binary !-}
102 {-! for Demands derive: Binary !-}
103 {-! for DmdResult derive: Binary !-}
104 {-! for StrictSig derive: Binary !-}
106 instance Binary DmdType where
107 -- ignore DmdEnv when spitting out the DmdType
108 put bh (DmdType _ ds dr) = do p <- put bh ds; put bh dr; return (castBin p)
109 get bh = do ds <- get bh; dr <- get bh; return (DmdType emptyVarEnv ds dr)
112 {-! for DataConDetails derive: Binary !-}
115 {-! for DefMeth derive: Binary !-}
118 {-! for HsPred derive: Binary !-}
119 {-! for HsType derive: Binary !-}
120 {-! for HsTupCon derive: Binary !-}
121 {-! for HsTyVarBndr derive: Binary !-}
124 {-! for UfExpr derive: Binary !-}
125 {-! for UfConAlt derive: Binary !-}
126 {-! for UfBinding derive: Binary !-}
127 {-! for UfBinder derive: Binary !-}
128 {-! for HsIdInfo derive: Binary !-}
129 {-! for UfNote derive: Binary !-}
132 {-! for ConDetails derive: Binary !-}
133 {-! for BangType derive: Binary !-}
135 instance (Binary name) => Binary (TyClDecl name) where
136 put_ bh (IfaceSig name ty idinfo _) = do
141 put_ bh (ForeignType ae af ag ah) =
142 error "Binary.put_(TyClDecl): ForeignType"
143 put_ bh (TyData ai aj ak al am _ (Just generics) _) = do
151 put_ bh generics -- Record whether generics needed or not
152 put_ bh (TySynonym aq ar as _) = do
157 put_ bh c@(ClassDecl ctxt nm tyvars fds sigs _ _) = do
164 -- ignore methods (there should be none)
173 return (IfaceSig name ty idinfo noSrcLoc)
174 1 -> error "Binary.get(TyClDecl): ForeignType"
182 return (TyData n_or_d ctx nm tyvars cons
183 Nothing (Just generics) noSrcLoc)
188 return (TySynonym aq ar as noSrcLoc)
195 return (ClassDecl ctxt nm tyvars fds sigs
198 instance (Binary name) => Binary (ConDecl name) where
199 put_ bh (ConDecl aa ac ad ae _) = do
210 return (ConDecl aa ac ad ae noSrcLoc)
212 instance (Binary name) => Binary (InstDecl name) where
213 put_ bh (InstDecl aa _ _ ad _) = do
222 return (InstDecl aa EmptyMonoBinds [{-no sigs-}] ad noSrcLoc)
224 instance (Binary name) => Binary (RuleDecl name) where
225 put_ bh (IfaceRule ag ah ai aj ak al _) = do
233 get bh = do ag <- get bh
239 return (IfaceRule ag ah ai aj ak al noSrcLoc)
241 instance (Binary name) => Binary (DeprecDecl name) where
242 put_ bh (Deprecation aa ab _) = do
249 return (Deprecation aa ab noSrcLoc)
252 instance Binary name => Binary (Sig name) where
253 put_ bh (ClassOpSig n def ty _) = do put_ bh n; put_ bh def; put_ bh ty
258 return (ClassOpSig n def ty noSrcLoc)
261 {-! for IsCafCC derive: Binary !-}
262 {-! for IsDupdCC derive: Binary !-}
263 {-! for CostCentre derive: Binary !-}
267 instance Binary ModIface where
269 build_tag <- readIORef v_Build_tag
270 put_ bh (show opt_HiVersion ++ build_tag)
271 p <- put_ bh (moduleName (mi_module iface))
272 put_ bh (mi_package iface)
273 put_ bh (vers_module (mi_version iface))
274 put_ bh (mi_orphan iface)
276 lazyPut bh (mi_deps iface)
277 lazyPut bh (map usageToOccName (mi_usages iface))
278 put_ bh (vers_exports (mi_version iface),
279 map exportItemToRdrExportItem (mi_exports iface))
280 put_ bh (declsToVersionedDecls (dcl_tycl (mi_decls iface))
281 (vers_decls (mi_version iface)))
283 put_ bh (collectFixities (mi_fixities iface)
284 (dcl_tycl (mi_decls iface)))
285 put_ bh (dcl_insts (mi_decls iface))
286 lazyPut bh (vers_rules (mi_version iface), dcl_rules (mi_decls iface))
287 lazyPut bh (deprecsToIfaceDeprecs (mi_deprecs iface))
289 -- Read in as a ParsedIface, not a ModIface. See above.
290 get bh = error "Binary.get: ModIface"
292 declsToVersionedDecls :: [RenamedTyClDecl] -> NameEnv Version
293 -> [(Version, RenamedTyClDecl)]
294 declsToVersionedDecls decls env
297 case lookupNameEnv env (tyClDeclName d) of
298 Nothing -> (initialVersion, d)
302 --NOT REALLY: deprecsToIfaceDeprecs :: Deprecations -> IfaceDeprecs
303 deprecsToIfaceDeprecs NoDeprecs = Nothing
304 deprecsToIfaceDeprecs (DeprecAll txt) = Just (Left txt)
305 deprecsToIfaceDeprecs (DeprecSome env) = Just (Right (nameEnvElts env))
308 {-! for GenAvailInfo derive: Binary !-}
309 {-! for WhatsImported derive: Binary !-}
311 -- For binary interfaces we need to convert the ImportVersion Names to OccNames
312 usageToOccName :: Usage Name -> Usage OccName
314 = usg { usg_entities = [ (nameOccName n, v) | (n,v) <- usg_entities usg ] }
316 exportItemToRdrExportItem (mn, avails)
317 = (mn, map availInfoToRdrAvailInfo avails)
319 availInfoToRdrAvailInfo :: AvailInfo -> RdrAvailInfo
320 availInfoToRdrAvailInfo (Avail n)
321 = Avail (nameOccName n)
322 availInfoToRdrAvailInfo (AvailTC n ns)
323 = AvailTC (nameOccName n) (map nameOccName ns)
325 -- ---------------------------------------------------------------------------
326 -- Reading a binary interface into ParsedIface
328 instance Binary ParsedIface where
330 pi_mod = module_name,
332 pi_vers = module_ver,
335 pi_exports = exports,
336 pi_decls = tycl_decls,
337 pi_fixity = fixities,
340 pi_deprecs = deprecs } = do
341 build_tag <- readIORef v_Build_tag
342 put_ bh (show opt_HiVersion ++ build_tag)
356 ignore_ver <- readIORef v_IgnoreHiVersion
357 build_tag <- readIORef v_Build_tag
358 let our_ver = show opt_HiVersion ++ build_tag
359 when (check_ver /= our_ver && not ignore_ver) $
360 -- use userError because this will be caught by readIface
361 -- which will emit an error msg containing the iface module name.
362 throwDyn (ProgramError (
363 "mismatched interface file versions: expected "
364 ++ our_ver ++ ", found " ++ check_ver))
365 module_name <- get bh -- same rep. as Module, so that's ok
370 usages <- {-# SCC "bin_usages" #-} lazyGet bh
371 exports <- {-# SCC "bin_exports" #-} get bh
372 tycl_decls <- {-# SCC "bin_tycldecls" #-} get bh
373 fixities <- {-# SCC "bin_fixities" #-} get bh
374 insts <- {-# SCC "bin_insts" #-} get bh
375 rules <- {-# SCC "bin_rules" #-} lazyGet bh
376 deprecs <- {-# SCC "bin_deprecs" #-} lazyGet bh
377 return (ParsedIface {
378 pi_mod = module_name,
380 pi_vers = module_ver,
384 pi_exports = exports,
385 pi_decls = tycl_decls,
386 pi_fixity = fixities,
387 pi_insts = reverse insts,
389 pi_deprecs = deprecs })
391 GLOBAL_VAR(v_IgnoreHiVersion, False, Bool)
393 -- ----------------------------------------------------------------------------
394 {-* Generated by DrIFT-v1.0 : Look, but Don't Touch. *-}
396 -- Imported from other files :-
398 instance Binary Dependencies where
399 put_ bh deps = do put_ bh (dep_mods deps)
400 put_ bh (dep_pkgs deps)
401 put_ bh (dep_orphs deps)
403 get bh = do ms <- get bh
406 return (Deps { dep_mods = ms, dep_pkgs = ps, dep_orphs = os })
408 instance (Binary name) => Binary (GenAvailInfo name) where
409 put_ bh (Avail aa) = do
412 put_ bh (AvailTC ab ac) = do
423 return (AvailTC ab ac)
425 instance (Binary name) => Binary (Usage name) where
427 put_ bh (usg_name usg)
428 put_ bh (usg_mod usg)
429 put_ bh (usg_exports usg)
430 put_ bh (usg_entities usg)
431 put_ bh (usg_rules usg)
439 return (Usage { usg_name = nm, usg_mod = mod,
440 usg_exports = exps, usg_entities = ents,
443 instance Binary Activation where
444 put_ bh NeverActive = do
446 put_ bh AlwaysActive = do
448 put_ bh (ActiveBefore aa) = do
451 put_ bh (ActiveAfter ab) = do
457 0 -> do return NeverActive
458 1 -> do return AlwaysActive
460 return (ActiveBefore aa)
462 return (ActiveAfter ab)
464 instance Binary StrictnessMark where
465 put_ bh MarkedUserStrict = do
467 put_ bh MarkedStrict = do
469 put_ bh MarkedUnboxed = do
471 put_ bh NotMarkedStrict = do
476 0 -> do return MarkedUserStrict
477 1 -> do return MarkedStrict
478 2 -> do return MarkedUnboxed
479 _ -> do return NotMarkedStrict
481 instance Binary Boxity where
490 _ -> do return Unboxed
492 instance Binary NewOrData where
495 put_ bh DataType = do
500 0 -> do return NewType
501 _ -> do return DataType
503 instance Binary FixityDirection where
513 0 -> do return InfixL
514 1 -> do return InfixR
515 _ -> do return InfixN
517 instance Binary Fixity where
518 put_ bh (Fixity aa ab) = do
524 return (Fixity aa ab)
526 instance (Binary name) => Binary (FixitySig name) where
527 put_ bh (FixitySig aa ab _) = do
533 return (FixitySig aa ab noSrcLoc)
535 instance (Binary name) => Binary (IPName name) where
536 put_ bh (Dupable aa) = do
539 put_ bh (Linear ab) = do
550 instance Binary Demand where
555 put_ bh (Call aa) = do
558 put_ bh (Eval ab) = do
561 put_ bh (Defer ac) = do
564 put_ bh (Box ad) = do
584 instance Binary Demands where
585 put_ bh (Poly aa) = do
588 put_ bh (Prod ab) = do
599 instance Binary DmdResult where
609 0 -> do return TopRes
610 1 -> do return RetCPR -- Really use RetCPR even if -fcpr-off
611 -- The wrapper was generated for CPR in
612 -- the imported module!
613 _ -> do return BotRes
615 instance Binary StrictSig where
616 put_ bh (StrictSig aa) = do
620 return (StrictSig aa)
622 instance (Binary name) => Binary (HsTyVarBndr name) where
623 put_ bh (UserTyVar aa) = do
626 put_ bh (IfaceTyVar ab ac) = do
634 return (UserTyVar aa)
637 return (IfaceTyVar ab ac)
639 instance Binary HsTupCon where
640 put_ bh (HsTupCon ab ac) = do
646 return (HsTupCon ab ac)
648 instance (Binary name) => Binary (HsTyOp name) where
649 put_ bh HsArrow = putByte bh 0
650 put_ bh (HsTyOp n) = do putByte bh 1
653 get bh = do h <- getByte bh
659 instance (Binary name) => Binary (HsType name) where
660 put_ bh (HsForAllTy aa ab ac) = do
665 put_ bh (HsTyVar ad) = do
668 put_ bh (HsAppTy ae af) = do
672 put_ bh (HsFunTy ag ah) = do
676 put_ bh (HsListTy ai) = do
679 put_ bh (HsPArrTy aj) = do
682 put_ bh (HsTupleTy ak al) = do
686 put_ bh (HsOpTy am an ao) = do
691 put_ bh (HsNumTy ap) = do
694 put_ bh (HsPredTy aq) = do
697 put_ bh (HsKindSig ar as) = do
707 return (HsForAllTy aa ab ac)
712 return (HsAppTy ae af)
715 return (HsFunTy ag ah)
722 return (HsTupleTy ak al)
726 return (HsOpTy am an ao)
733 return (HsKindSig ar as)
735 instance (Binary name) => Binary (HsPred name) where
736 put_ bh (HsClassP aa ab) = do
740 put_ bh (HsIParam ac ad) = do
749 return (HsClassP aa ab)
752 return (HsIParam ac ad)
754 instance (Binary name) => Binary (UfExpr name) where
755 put_ bh (UfVar aa) = do
758 put_ bh (UfType ab) = do
761 put_ bh (UfTuple ac ad) = do
765 put_ bh (UfLam ae af) = do
769 put_ bh (UfApp ag ah) = do
773 put_ bh (UfCase ai aj ak) = do
778 put_ bh (UfLet al am) = do
782 put_ bh (UfNote an ao) = do
786 put_ bh (UfLit ap) = do
789 put_ bh (UfFCall as at) = do
802 return (UfTuple ac ad)
812 return (UfCase ai aj ak)
818 return (UfNote an ao)
823 return (UfFCall as at)
825 instance (Binary name) => Binary (UfConAlt name) where
826 put_ bh UfDefault = do
828 put_ bh (UfDataAlt aa) = do
831 put_ bh (UfTupleAlt ab) = do
834 put_ bh (UfLitAlt ac) = do
840 0 -> do return UfDefault
842 return (UfDataAlt aa)
844 return (UfTupleAlt ab)
848 instance (Binary name) => Binary (UfBinding name) where
849 put_ bh (UfNonRec aa ab) = do
853 put_ bh (UfRec ac) = do
861 return (UfNonRec aa ab)
865 instance (Binary name) => Binary (UfBinder name) where
866 put_ bh (UfValBinder aa ab) = do
870 put_ bh (UfTyBinder ac ad) = do
879 return (UfValBinder aa ab)
882 return (UfTyBinder ac ad)
884 instance (Binary name) => Binary (HsIdInfo name) where
885 put_ bh (HsArity aa) = do
888 put_ bh (HsStrictness ab) = do
891 put_ bh (HsUnfold ac ad) = do
895 put_ bh HsNoCafRefs = do
897 put_ bh (HsWorker ae af) = do
907 return (HsStrictness ab)
910 return (HsUnfold ac ad)
911 3 -> do return HsNoCafRefs
914 return (HsWorker ae af)
916 instance (Binary name) => Binary (UfNote name) where
917 put_ bh (UfSCC aa) = do
920 put_ bh (UfCoerce ab) = do
923 put_ bh UfInlineCall = do
925 put_ bh UfInlineMe = do
927 put_ bh (UfCoreNote s) = do
937 2 -> do return UfInlineCall
938 3 -> do return UfInlineMe
940 return (UfCoreNote ac)
942 instance (Binary name) => Binary (BangType name) where
943 put_ bh (BangType aa ab) = do
949 return (BangType aa ab)
951 instance (Binary name, Binary arg) => Binary (HsConDetails name arg) where
952 put_ bh (PrefixCon aa) = do
955 put_ bh (InfixCon ab ac) = do
959 put_ bh (RecCon ad) = do
966 return (PrefixCon aa)
969 return (InfixCon ab ac)
973 instance (Binary datacon) => Binary (DataConDetails datacon) where
974 put_ bh (DataCons aa) = do
979 put_ bh (HasCons ab) = do
987 1 -> do return Unknown
991 instance (Binary id) => Binary (DefMeth id) where
992 put_ bh NoDefMeth = do
994 put_ bh (DefMeth aa) = do
997 put_ bh GenDefMeth = do
1002 0 -> do return NoDefMeth
1003 1 -> do aa <- get bh
1005 _ -> do return GenDefMeth
1007 instance Binary IsCafCC where
1010 put_ bh NotCafCC = do
1015 0 -> do return CafCC
1016 _ -> do return NotCafCC
1018 instance Binary IsDupdCC where
1019 put_ bh OriginalCC = do
1026 0 -> do return OriginalCC
1027 _ -> do return DupdCC
1029 instance Binary CostCentre where
1030 put_ bh NoCostCentre = do
1032 put_ bh (NormalCC aa ab ac ad) = do
1038 put_ bh (AllCafsCC ae) = do
1044 0 -> do return NoCostCentre
1045 1 -> do aa <- get bh
1049 return (NormalCC aa ab ac ad)
1050 _ -> do ae <- get bh
1051 return (AllCafsCC ae)