PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/compiler/iface/BinIface.hs

https://bitbucket.org/carter/ghc
Haskell | 1587 lines | 1280 code | 158 blank | 149 comment | 10 complexity | 6272603e87a894ccbfdfc3e46087e8af MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. --
  2. -- (c) The University of Glasgow 2002-2006
  3. --
  4. {-# OPTIONS_GHC -fno-warn-orphans #-}
  5. {-# OPTIONS_GHC -O #-}
  6. -- We always optimise this, otherwise performance of a non-optimised
  7. -- compiler is severely affected
  8. -- | Binary interface file support.
  9. module BinIface (
  10. writeBinIface,
  11. readBinIface,
  12. getSymtabName,
  13. getDictFastString,
  14. CheckHiWay(..),
  15. TraceBinIFaceReading(..)
  16. ) where
  17. #include "HsVersions.h"
  18. import TcRnMonad
  19. import TyCon
  20. import DataCon (dataConName, dataConWorkId, dataConTyCon)
  21. import PrelInfo (wiredInThings, basicKnownKeyNames)
  22. import Id (idName, isDataConWorkId_maybe)
  23. import CoreSyn (DFunArg(..))
  24. import Coercion (LeftOrRight(..))
  25. import TysWiredIn
  26. import IfaceEnv
  27. import HscTypes
  28. import BasicTypes
  29. import Demand
  30. import Annotations
  31. import IfaceSyn
  32. import Module
  33. import Name
  34. import Avail
  35. import VarEnv
  36. import DynFlags
  37. import UniqFM
  38. import UniqSupply
  39. import CostCentre
  40. import Panic
  41. import Binary
  42. import SrcLoc
  43. import ErrUtils
  44. import FastMutInt
  45. import Unique
  46. import Outputable
  47. import Platform
  48. import FastString
  49. import Constants
  50. import Util
  51. import Data.Bits
  52. import Data.Char
  53. import Data.List
  54. import Data.Word
  55. import Data.Array
  56. import Data.IORef
  57. import Control.Monad
  58. -- ---------------------------------------------------------------------------
  59. -- Reading and writing binary interface files
  60. --
  61. data CheckHiWay = CheckHiWay | IgnoreHiWay
  62. deriving Eq
  63. data TraceBinIFaceReading = TraceBinIFaceReading | QuietBinIFaceReading
  64. deriving Eq
  65. -- | Read an interface file
  66. readBinIface :: CheckHiWay -> TraceBinIFaceReading -> FilePath
  67. -> TcRnIf a b ModIface
  68. readBinIface checkHiWay traceBinIFaceReading hi_path = do
  69. ncu <- mkNameCacheUpdater
  70. dflags <- getDynFlags
  71. liftIO $ readBinIface_ dflags checkHiWay traceBinIFaceReading hi_path ncu
  72. readBinIface_ :: DynFlags -> CheckHiWay -> TraceBinIFaceReading -> FilePath
  73. -> NameCacheUpdater
  74. -> IO ModIface
  75. readBinIface_ dflags checkHiWay traceBinIFaceReading hi_path ncu = do
  76. let printer :: SDoc -> IO ()
  77. printer = case traceBinIFaceReading of
  78. TraceBinIFaceReading -> \sd -> log_action dflags dflags SevOutput noSrcSpan defaultDumpStyle sd
  79. QuietBinIFaceReading -> \_ -> return ()
  80. wantedGot :: Outputable a => String -> a -> a -> IO ()
  81. wantedGot what wanted got =
  82. printer (text what <> text ": " <>
  83. vcat [text "Wanted " <> ppr wanted <> text ",",
  84. text "got " <> ppr got])
  85. errorOnMismatch :: (Eq a, Show a) => String -> a -> a -> IO ()
  86. errorOnMismatch what wanted got =
  87. -- This will be caught by readIface which will emit an error
  88. -- msg containing the iface module name.
  89. when (wanted /= got) $ ghcError $ ProgramError
  90. (what ++ " (wanted " ++ show wanted
  91. ++ ", got " ++ show got ++ ")")
  92. bh <- Binary.readBinMem hi_path
  93. -- Read the magic number to check that this really is a GHC .hi file
  94. -- (This magic number does not change when we change
  95. -- GHC interface file format)
  96. magic <- get bh
  97. wantedGot "Magic" (binaryInterfaceMagic dflags) magic
  98. errorOnMismatch "magic number mismatch: old/corrupt interface file?"
  99. (binaryInterfaceMagic dflags) magic
  100. -- Note [dummy iface field]
  101. -- read a dummy 32/64 bit value. This field used to hold the
  102. -- dictionary pointer in old interface file formats, but now
  103. -- the dictionary pointer is after the version (where it
  104. -- should be). Also, the serialisation of value of type "Bin
  105. -- a" used to depend on the word size of the machine, now they
  106. -- are always 32 bits.
  107. if wORD_SIZE dflags == 4
  108. then do _ <- Binary.get bh :: IO Word32; return ()
  109. else do _ <- Binary.get bh :: IO Word64; return ()
  110. -- Check the interface file version and ways.
  111. check_ver <- get bh
  112. let our_ver = show hiVersion
  113. wantedGot "Version" our_ver check_ver
  114. errorOnMismatch "mismatched interface file versions" our_ver check_ver
  115. check_way <- get bh
  116. let way_descr = getWayDescr dflags
  117. wantedGot "Way" way_descr check_way
  118. when (checkHiWay == CheckHiWay) $
  119. errorOnMismatch "mismatched interface file ways" way_descr check_way
  120. -- Read the dictionary
  121. -- The next word in the file is a pointer to where the dictionary is
  122. -- (probably at the end of the file)
  123. dict_p <- Binary.get bh
  124. data_p <- tellBin bh -- Remember where we are now
  125. seekBin bh dict_p
  126. dict <- getDictionary bh
  127. seekBin bh data_p -- Back to where we were before
  128. -- Initialise the user-data field of bh
  129. bh <- do
  130. bh <- return $ setUserData bh $ newReadState (error "getSymtabName")
  131. (getDictFastString dict)
  132. symtab_p <- Binary.get bh -- Get the symtab ptr
  133. data_p <- tellBin bh -- Remember where we are now
  134. seekBin bh symtab_p
  135. symtab <- getSymbolTable bh ncu
  136. seekBin bh data_p -- Back to where we were before
  137. -- It is only now that we know how to get a Name
  138. return $ setUserData bh $ newReadState (getSymtabName ncu dict symtab)
  139. (getDictFastString dict)
  140. -- Read the interface file
  141. get bh
  142. -- | Write an interface file
  143. writeBinIface :: DynFlags -> FilePath -> ModIface -> IO ()
  144. writeBinIface dflags hi_path mod_iface = do
  145. bh <- openBinMem initBinMemSize
  146. put_ bh (binaryInterfaceMagic dflags)
  147. -- dummy 32/64-bit field before the version/way for
  148. -- compatibility with older interface file formats.
  149. -- See Note [dummy iface field] above.
  150. if wORD_SIZE dflags == 4
  151. then Binary.put_ bh (0 :: Word32)
  152. else Binary.put_ bh (0 :: Word64)
  153. -- The version and way descriptor go next
  154. put_ bh (show hiVersion)
  155. let way_descr = getWayDescr dflags
  156. put_ bh way_descr
  157. -- Remember where the dictionary pointer will go
  158. dict_p_p <- tellBin bh
  159. -- Placeholder for ptr to dictionary
  160. put_ bh dict_p_p
  161. -- Remember where the symbol table pointer will go
  162. symtab_p_p <- tellBin bh
  163. put_ bh symtab_p_p
  164. -- Make some intial state
  165. symtab_next <- newFastMutInt
  166. writeFastMutInt symtab_next 0
  167. symtab_map <- newIORef emptyUFM
  168. let bin_symtab = BinSymbolTable {
  169. bin_symtab_next = symtab_next,
  170. bin_symtab_map = symtab_map }
  171. dict_next_ref <- newFastMutInt
  172. writeFastMutInt dict_next_ref 0
  173. dict_map_ref <- newIORef emptyUFM
  174. let bin_dict = BinDictionary {
  175. bin_dict_next = dict_next_ref,
  176. bin_dict_map = dict_map_ref }
  177. -- Put the main thing,
  178. bh <- return $ setUserData bh $ newWriteState (putName bin_dict bin_symtab)
  179. (putFastString bin_dict)
  180. put_ bh mod_iface
  181. -- Write the symtab pointer at the fornt of the file
  182. symtab_p <- tellBin bh -- This is where the symtab will start
  183. putAt bh symtab_p_p symtab_p -- Fill in the placeholder
  184. seekBin bh symtab_p -- Seek back to the end of the file
  185. -- Write the symbol table itself
  186. symtab_next <- readFastMutInt symtab_next
  187. symtab_map <- readIORef symtab_map
  188. putSymbolTable bh symtab_next symtab_map
  189. debugTraceMsg dflags 3 (text "writeBinIface:" <+> int symtab_next
  190. <+> text "Names")
  191. -- NB. write the dictionary after the symbol table, because
  192. -- 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
  197. -- Write the dictionary itself
  198. dict_next <- readFastMutInt dict_next_ref
  199. dict_map <- readIORef dict_map_ref
  200. putDictionary bh dict_next dict_map
  201. debugTraceMsg dflags 3 (text "writeBinIface:" <+> int dict_next
  202. <+> text "dict entries")
  203. -- And send the result to the file
  204. writeBinMem bh hi_path
  205. -- | Initial ram buffer to allocate for writing interface files
  206. initBinMemSize :: Int
  207. initBinMemSize = 1024 * 1024
  208. binaryInterfaceMagic :: DynFlags -> Word32
  209. binaryInterfaceMagic dflags
  210. | target32Bit (targetPlatform dflags) = 0x1face
  211. | otherwise = 0x1face64
  212. -- -----------------------------------------------------------------------------
  213. -- The symbol table
  214. --
  215. putSymbolTable :: BinHandle -> Int -> UniqFM (Int,Name) -> IO ()
  216. putSymbolTable bh next_off symtab = do
  217. put_ bh next_off
  218. let names = elems (array (0,next_off-1) (eltsUFM symtab))
  219. mapM_ (\n -> serialiseName bh n symtab) names
  220. getSymbolTable :: BinHandle -> NameCacheUpdater -> IO SymbolTable
  221. getSymbolTable bh ncu = do
  222. sz <- get bh
  223. od_names <- sequence (replicate sz (get bh))
  224. updateNameCache ncu $ \namecache ->
  225. let arr = listArray (0,sz-1) names
  226. (namecache', names) =
  227. mapAccumR (fromOnDiskName arr) namecache od_names
  228. in (namecache', arr)
  229. type OnDiskName = (PackageId, ModuleName, OccName)
  230. fromOnDiskName :: Array Int Name -> NameCache -> OnDiskName -> (NameCache, Name)
  231. fromOnDiskName _ nc (pid, mod_name, occ) =
  232. let mod = mkModule pid mod_name
  233. cache = nsNames nc
  234. in case lookupOrigNameCache cache mod occ of
  235. Just name -> (nc, name)
  236. Nothing ->
  237. let (uniq, us) = takeUniqFromSupply (nsUniqs nc)
  238. name = mkExternalName uniq mod occ noSrcSpan
  239. new_cache = extendNameCache cache mod occ name
  240. in ( nc{ nsUniqs = us, nsNames = new_cache }, name )
  241. serialiseName :: BinHandle -> Name -> UniqFM (Int,Name) -> IO ()
  242. serialiseName bh name _ = do
  243. let mod = ASSERT2( isExternalName name, ppr name ) nameModule name
  244. put_ bh (modulePackageId mod, moduleName mod, nameOccName name)
  245. -- Note [Symbol table representation of names]
  246. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  247. --
  248. -- An occurrence of a name in an interface file is serialized as a single 32-bit word.
  249. -- The format of this word is:
  250. -- 00xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  251. -- A normal name. x is an index into the symbol table
  252. -- 01xxxxxxxxyyyyyyyyyyyyyyyyyyyyyyyy
  253. -- A known-key name. x is the Unique's Char, y is the int part
  254. -- 10xxyyzzzzzzzzzzzzzzzzzzzzzzzzzzzz
  255. -- A tuple name:
  256. -- x is the tuple sort (00b ==> boxed, 01b ==> unboxed, 10b ==> constraint)
  257. -- y is the thing (00b ==> tycon, 01b ==> datacon, 10b ==> datacon worker)
  258. -- z is the arity
  259. -- 11xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  260. -- An implicit parameter TyCon name. x is an index into the FastString *dictionary*
  261. --
  262. -- Note that we have to have special representation for tuples and IP TyCons because they
  263. -- form an "infinite" family and hence are not recorded explicitly in wiredInTyThings or
  264. -- basicKnownKeyNames.
  265. knownKeyNamesMap :: UniqFM Name
  266. knownKeyNamesMap = listToUFM_Directly [(nameUnique n, n) | n <- knownKeyNames]
  267. where
  268. knownKeyNames :: [Name]
  269. knownKeyNames = map getName wiredInThings ++ basicKnownKeyNames
  270. -- See Note [Symbol table representation of names]
  271. putName :: BinDictionary -> BinSymbolTable -> BinHandle -> Name -> IO ()
  272. putName _dict BinSymbolTable{
  273. bin_symtab_map = symtab_map_ref,
  274. bin_symtab_next = symtab_next } bh name
  275. | name `elemUFM` knownKeyNamesMap
  276. , let (c, u) = unpkUnique (nameUnique name) -- INVARIANT: (ord c) fits in 8 bits
  277. = -- ASSERT(u < 2^(22 :: Int))
  278. put_ bh (0x40000000 .|. (fromIntegral (ord c) `shiftL` 22) .|. (fromIntegral u :: Word32))
  279. | otherwise
  280. = case wiredInNameTyThing_maybe name of
  281. Just (ATyCon tc)
  282. | isTupleTyCon tc -> putTupleName_ bh tc 0
  283. Just (ADataCon dc)
  284. | let tc = dataConTyCon dc, isTupleTyCon tc -> putTupleName_ bh tc 1
  285. Just (AnId x)
  286. | Just dc <- isDataConWorkId_maybe x, let tc = dataConTyCon dc, isTupleTyCon tc -> putTupleName_ bh tc 2
  287. _ -> do
  288. symtab_map <- readIORef symtab_map_ref
  289. case lookupUFM symtab_map name of
  290. Just (off,_) -> put_ bh (fromIntegral off :: Word32)
  291. Nothing -> do
  292. off <- readFastMutInt symtab_next
  293. -- MASSERT(off < 2^(30 :: Int))
  294. writeFastMutInt symtab_next (off+1)
  295. writeIORef symtab_map_ref
  296. $! addToUFM symtab_map name (off,name)
  297. put_ bh (fromIntegral off :: Word32)
  298. putTupleName_ :: BinHandle -> TyCon -> Word32 -> IO ()
  299. putTupleName_ bh tc thing_tag
  300. = -- ASSERT(arity < 2^(30 :: Int))
  301. put_ bh (0x80000000 .|. (sort_tag `shiftL` 28) .|. (thing_tag `shiftL` 26) .|. arity)
  302. where
  303. arity = fromIntegral (tupleTyConArity tc)
  304. sort_tag = case tupleTyConSort tc of
  305. BoxedTuple -> 0
  306. UnboxedTuple -> 1
  307. ConstraintTuple -> 2
  308. -- See Note [Symbol table representation of names]
  309. getSymtabName :: NameCacheUpdater
  310. -> Dictionary -> SymbolTable
  311. -> BinHandle -> IO Name
  312. getSymtabName _ncu _dict symtab bh = do
  313. i <- get bh
  314. case i .&. 0xC0000000 of
  315. 0x00000000 -> return $! symtab ! fromIntegral (i ::  Word32)
  316. 0x40000000 -> return $! case lookupUFM_Directly knownKeyNamesMap (mkUnique tag ix) of
  317. Nothing -> pprPanic "getSymtabName:unknown known-key unique" (ppr i)
  318. Just n -> n
  319. where tag = chr (fromIntegral ((i .&. 0x3FC00000) `shiftR` 22))
  320. ix = fromIntegral i .&. 0x003FFFFF
  321. 0x80000000 -> return $! case thing_tag of
  322. 0 -> tyConName (tupleTyCon sort arity)
  323. 1 -> dataConName dc
  324. 2 -> idName (dataConWorkId dc)
  325. _ -> pprPanic "getSymtabName:unknown tuple thing" (ppr i)
  326. where
  327. dc = tupleCon sort arity
  328. sort = case (i .&. 0x30000000) `shiftR` 28 of
  329. 0 -> BoxedTuple
  330. 1 -> UnboxedTuple
  331. 2 -> ConstraintTuple
  332. _ -> pprPanic "getSymtabName:unknown tuple sort" (ppr i)
  333. thing_tag = (i .&. 0x0CFFFFFF) `shiftR` 26
  334. arity = fromIntegral (i .&. 0x03FFFFFF)
  335. _ -> pprPanic "getSymtabName:unknown name tag" (ppr i)
  336. data BinSymbolTable = BinSymbolTable {
  337. bin_symtab_next :: !FastMutInt, -- The next index to use
  338. bin_symtab_map :: !(IORef (UniqFM (Int,Name)))
  339. -- indexed by Name
  340. }
  341. putFastString :: BinDictionary -> BinHandle -> FastString -> IO ()
  342. putFastString dict bh fs = allocateFastString dict fs >>= put_ bh
  343. allocateFastString :: BinDictionary -> FastString -> IO Word32
  344. allocateFastString BinDictionary { bin_dict_next = j_r,
  345. bin_dict_map = out_r} f = do
  346. out <- readIORef out_r
  347. let uniq = getUnique f
  348. case lookupUFM out uniq of
  349. Just (j, _) -> return (fromIntegral j :: Word32)
  350. Nothing -> do
  351. j <- readFastMutInt j_r
  352. writeFastMutInt j_r (j + 1)
  353. writeIORef out_r $! addToUFM out uniq (j, f)
  354. return (fromIntegral j :: Word32)
  355. getDictFastString :: Dictionary -> BinHandle -> IO FastString
  356. getDictFastString dict bh = do
  357. j <- get bh
  358. return $! (dict ! fromIntegral (j :: Word32))
  359. data BinDictionary = BinDictionary {
  360. bin_dict_next :: !FastMutInt, -- The next index to use
  361. bin_dict_map :: !(IORef (UniqFM (Int,FastString)))
  362. -- indexed by FastString
  363. }
  364. -- -----------------------------------------------------------------------------
  365. -- All the binary instances
  366. -- BasicTypes
  367. {-! for Fixity derive: Binary !-}
  368. {-! for FixityDirection derive: Binary !-}
  369. {-! for Boxity derive: Binary !-}
  370. {-! for StrictnessMark derive: Binary !-}
  371. {-! for Activation derive: Binary !-}
  372. -- Demand
  373. {-! for Demand derive: Binary !-}
  374. {-! for Demands derive: Binary !-}
  375. {-! for DmdResult derive: Binary !-}
  376. {-! for StrictSig derive: Binary !-}
  377. -- Class
  378. {-! for DefMeth derive: Binary !-}
  379. -- HsTypes
  380. {-! for HsPred derive: Binary !-}
  381. {-! for HsType derive: Binary !-}
  382. {-! for TupCon derive: Binary !-}
  383. {-! for HsTyVarBndr derive: Binary !-}
  384. -- HsCore
  385. {-! for UfExpr derive: Binary !-}
  386. {-! for UfConAlt derive: Binary !-}
  387. {-! for UfBinding derive: Binary !-}
  388. {-! for UfBinder derive: Binary !-}
  389. {-! for HsIdInfo derive: Binary !-}
  390. {-! for UfNote derive: Binary !-}
  391. -- HsDecls
  392. {-! for ConDetails derive: Binary !-}
  393. {-! for BangType derive: Binary !-}
  394. -- CostCentre
  395. {-! for IsCafCC derive: Binary !-}
  396. {-! for CostCentre derive: Binary !-}
  397. -- ---------------------------------------------------------------------------
  398. -- Reading a binary interface into ParsedIface
  399. instance Binary ModIface where
  400. put_ bh (ModIface {
  401. mi_module = mod,
  402. mi_boot = is_boot,
  403. mi_iface_hash= iface_hash,
  404. mi_mod_hash = mod_hash,
  405. mi_flag_hash = flag_hash,
  406. mi_orphan = orphan,
  407. mi_finsts = hasFamInsts,
  408. mi_deps = deps,
  409. mi_usages = usages,
  410. mi_exports = exports,
  411. mi_exp_hash = exp_hash,
  412. mi_used_th = used_th,
  413. mi_fixities = fixities,
  414. mi_warns = warns,
  415. mi_anns = anns,
  416. mi_decls = decls,
  417. mi_insts = insts,
  418. mi_fam_insts = fam_insts,
  419. mi_rules = rules,
  420. mi_orphan_hash = orphan_hash,
  421. mi_vect_info = vect_info,
  422. mi_hpc = hpc_info,
  423. mi_trust = trust,
  424. mi_trust_pkg = trust_pkg }) = do
  425. put_ bh mod
  426. put_ bh is_boot
  427. put_ bh iface_hash
  428. put_ bh mod_hash
  429. put_ bh flag_hash
  430. put_ bh orphan
  431. put_ bh hasFamInsts
  432. lazyPut bh deps
  433. lazyPut bh usages
  434. put_ bh exports
  435. put_ bh exp_hash
  436. put_ bh used_th
  437. put_ bh fixities
  438. lazyPut bh warns
  439. lazyPut bh anns
  440. put_ bh decls
  441. put_ bh insts
  442. put_ bh fam_insts
  443. lazyPut bh rules
  444. put_ bh orphan_hash
  445. put_ bh vect_info
  446. put_ bh hpc_info
  447. put_ bh trust
  448. put_ bh trust_pkg
  449. get bh = do
  450. mod_name <- get bh
  451. is_boot <- get bh
  452. iface_hash <- get bh
  453. mod_hash <- get bh
  454. flag_hash <- get bh
  455. orphan <- get bh
  456. hasFamInsts <- get bh
  457. deps <- lazyGet bh
  458. usages <- {-# SCC "bin_usages" #-} lazyGet bh
  459. exports <- {-# SCC "bin_exports" #-} get bh
  460. exp_hash <- get bh
  461. used_th <- get bh
  462. fixities <- {-# SCC "bin_fixities" #-} get bh
  463. warns <- {-# SCC "bin_warns" #-} lazyGet bh
  464. anns <- {-# SCC "bin_anns" #-} lazyGet bh
  465. decls <- {-# SCC "bin_tycldecls" #-} get bh
  466. insts <- {-# SCC "bin_insts" #-} get bh
  467. fam_insts <- {-# SCC "bin_fam_insts" #-} get bh
  468. rules <- {-# SCC "bin_rules" #-} lazyGet bh
  469. orphan_hash <- get bh
  470. vect_info <- get bh
  471. hpc_info <- get bh
  472. trust <- get bh
  473. trust_pkg <- get bh
  474. return (ModIface {
  475. mi_module = mod_name,
  476. mi_boot = is_boot,
  477. mi_iface_hash = iface_hash,
  478. mi_mod_hash = mod_hash,
  479. mi_flag_hash = flag_hash,
  480. mi_orphan = orphan,
  481. mi_finsts = hasFamInsts,
  482. mi_deps = deps,
  483. mi_usages = usages,
  484. mi_exports = exports,
  485. mi_exp_hash = exp_hash,
  486. mi_used_th = used_th,
  487. mi_anns = anns,
  488. mi_fixities = fixities,
  489. mi_warns = warns,
  490. mi_decls = decls,
  491. mi_globals = Nothing,
  492. mi_insts = insts,
  493. mi_fam_insts = fam_insts,
  494. mi_rules = rules,
  495. mi_orphan_hash = orphan_hash,
  496. mi_vect_info = vect_info,
  497. mi_hpc = hpc_info,
  498. mi_trust = trust,
  499. mi_trust_pkg = trust_pkg,
  500. -- And build the cached values
  501. mi_warn_fn = mkIfaceWarnCache warns,
  502. mi_fix_fn = mkIfaceFixCache fixities,
  503. mi_hash_fn = mkIfaceHashCache decls })
  504. getWayDescr :: DynFlags -> String
  505. getWayDescr dflags
  506. | platformUnregisterised (targetPlatform dflags) = 'u':tag
  507. | otherwise = tag
  508. where tag = buildTag dflags
  509. -- if this is an unregisterised build, make sure our interfaces
  510. -- can't be used by a registerised build.
  511. -------------------------------------------------------------------------
  512. -- Types from: HscTypes
  513. -------------------------------------------------------------------------
  514. instance Binary Dependencies where
  515. put_ bh deps = do put_ bh (dep_mods deps)
  516. put_ bh (dep_pkgs deps)
  517. put_ bh (dep_orphs deps)
  518. put_ bh (dep_finsts deps)
  519. get bh = do ms <- get bh
  520. ps <- get bh
  521. os <- get bh
  522. fis <- get bh
  523. return (Deps { dep_mods = ms, dep_pkgs = ps, dep_orphs = os,
  524. dep_finsts = fis })
  525. instance Binary AvailInfo where
  526. put_ bh (Avail aa) = do
  527. putByte bh 0
  528. put_ bh aa
  529. put_ bh (AvailTC ab ac) = do
  530. putByte bh 1
  531. put_ bh ab
  532. put_ bh ac
  533. get bh = do
  534. h <- getByte bh
  535. case h of
  536. 0 -> do aa <- get bh
  537. return (Avail aa)
  538. _ -> do ab <- get bh
  539. ac <- get bh
  540. return (AvailTC ab ac)
  541. instance Binary Usage where
  542. put_ bh usg@UsagePackageModule{} = do
  543. putByte bh 0
  544. put_ bh (usg_mod usg)
  545. put_ bh (usg_mod_hash usg)
  546. put_ bh (usg_safe usg)
  547. put_ bh usg@UsageHomeModule{} = do
  548. putByte bh 1
  549. put_ bh (usg_mod_name usg)
  550. put_ bh (usg_mod_hash usg)
  551. put_ bh (usg_exports usg)
  552. put_ bh (usg_entities usg)
  553. put_ bh (usg_safe usg)
  554. put_ bh usg@UsageFile{} = do
  555. putByte bh 2
  556. put_ bh (usg_file_path usg)
  557. put_ bh (usg_mtime usg)
  558. get bh = do
  559. h <- getByte bh
  560. case h of
  561. 0 -> do
  562. nm <- get bh
  563. mod <- get bh
  564. safe <- get bh
  565. return UsagePackageModule { usg_mod = nm, usg_mod_hash = mod, usg_safe = safe }
  566. 1 -> do
  567. nm <- get bh
  568. mod <- get bh
  569. exps <- get bh
  570. ents <- get bh
  571. safe <- get bh
  572. return UsageHomeModule { usg_mod_name = nm, usg_mod_hash = mod,
  573. usg_exports = exps, usg_entities = ents, usg_safe = safe }
  574. 2 -> do
  575. fp <- get bh
  576. mtime <- get bh
  577. return UsageFile { usg_file_path = fp, usg_mtime = mtime }
  578. i -> error ("Binary.get(Usage): " ++ show i)
  579. instance Binary Warnings where
  580. put_ bh NoWarnings = putByte bh 0
  581. put_ bh (WarnAll t) = do
  582. putByte bh 1
  583. put_ bh t
  584. put_ bh (WarnSome ts) = do
  585. putByte bh 2
  586. put_ bh ts
  587. get bh = do
  588. h <- getByte bh
  589. case h of
  590. 0 -> return NoWarnings
  591. 1 -> do aa <- get bh
  592. return (WarnAll aa)
  593. _ -> do aa <- get bh
  594. return (WarnSome aa)
  595. instance Binary WarningTxt where
  596. put_ bh (WarningTxt w) = do
  597. putByte bh 0
  598. put_ bh w
  599. put_ bh (DeprecatedTxt d) = do
  600. putByte bh 1
  601. put_ bh d
  602. get bh = do
  603. h <- getByte bh
  604. case h of
  605. 0 -> do w <- get bh
  606. return (WarningTxt w)
  607. _ -> do d <- get bh
  608. return (DeprecatedTxt d)
  609. -------------------------------------------------------------------------
  610. -- Types from: BasicTypes
  611. -------------------------------------------------------------------------
  612. instance Binary Activation where
  613. put_ bh NeverActive = do
  614. putByte bh 0
  615. put_ bh AlwaysActive = do
  616. putByte bh 1
  617. put_ bh (ActiveBefore aa) = do
  618. putByte bh 2
  619. put_ bh aa
  620. put_ bh (ActiveAfter ab) = do
  621. putByte bh 3
  622. put_ bh ab
  623. get bh = do
  624. h <- getByte bh
  625. case h of
  626. 0 -> do return NeverActive
  627. 1 -> do return AlwaysActive
  628. 2 -> do aa <- get bh
  629. return (ActiveBefore aa)
  630. _ -> do ab <- get bh
  631. return (ActiveAfter ab)
  632. instance Binary RuleMatchInfo where
  633. put_ bh FunLike = putByte bh 0
  634. put_ bh ConLike = putByte bh 1
  635. get bh = do
  636. h <- getByte bh
  637. if h == 1 then return ConLike
  638. else return FunLike
  639. instance Binary InlinePragma where
  640. put_ bh (InlinePragma a b c d) = do
  641. put_ bh a
  642. put_ bh b
  643. put_ bh c
  644. put_ bh d
  645. get bh = do
  646. a <- get bh
  647. b <- get bh
  648. c <- get bh
  649. d <- get bh
  650. return (InlinePragma a b c d)
  651. instance Binary InlineSpec where
  652. put_ bh EmptyInlineSpec = putByte bh 0
  653. put_ bh Inline = putByte bh 1
  654. put_ bh Inlinable = putByte bh 2
  655. put_ bh NoInline = putByte bh 3
  656. get bh = do h <- getByte bh
  657. case h of
  658. 0 -> return EmptyInlineSpec
  659. 1 -> return Inline
  660. 2 -> return Inlinable
  661. _ -> return NoInline
  662. instance Binary HsBang where
  663. put_ bh HsNoBang = putByte bh 0
  664. put_ bh HsStrict = putByte bh 1
  665. put_ bh HsUnpack = putByte bh 2
  666. put_ bh HsUnpackFailed = putByte bh 3
  667. put_ bh HsNoUnpack = putByte bh 4
  668. get bh = do
  669. h <- getByte bh
  670. case h of
  671. 0 -> do return HsNoBang
  672. 1 -> do return HsStrict
  673. 2 -> do return HsUnpack
  674. 3 -> do return HsUnpackFailed
  675. _ -> do return HsNoUnpack
  676. instance Binary TupleSort where
  677. put_ bh BoxedTuple = putByte bh 0
  678. put_ bh UnboxedTuple = putByte bh 1
  679. put_ bh ConstraintTuple = putByte bh 2
  680. get bh = do
  681. h <- getByte bh
  682. case h of
  683. 0 -> do return BoxedTuple
  684. 1 -> do return UnboxedTuple
  685. _ -> do return ConstraintTuple
  686. instance Binary RecFlag where
  687. put_ bh Recursive = do
  688. putByte bh 0
  689. put_ bh NonRecursive = do
  690. putByte bh 1
  691. get bh = do
  692. h <- getByte bh
  693. case h of
  694. 0 -> do return Recursive
  695. _ -> do return NonRecursive
  696. instance Binary DefMethSpec where
  697. put_ bh NoDM = putByte bh 0
  698. put_ bh VanillaDM = putByte bh 1
  699. put_ bh GenericDM = putByte bh 2
  700. get bh = do
  701. h <- getByte bh
  702. case h of
  703. 0 -> return NoDM
  704. 1 -> return VanillaDM
  705. _ -> return GenericDM
  706. instance Binary FixityDirection where
  707. put_ bh InfixL = do
  708. putByte bh 0
  709. put_ bh InfixR = do
  710. putByte bh 1
  711. put_ bh InfixN = do
  712. putByte bh 2
  713. get bh = do
  714. h <- getByte bh
  715. case h of
  716. 0 -> do return InfixL
  717. 1 -> do return InfixR
  718. _ -> do return InfixN
  719. instance Binary Fixity where
  720. put_ bh (Fixity aa ab) = do
  721. put_ bh aa
  722. put_ bh ab
  723. get bh = do
  724. aa <- get bh
  725. ab <- get bh
  726. return (Fixity aa ab)
  727. -------------------------------------------------------------------------
  728. -- Types from: Demand
  729. -------------------------------------------------------------------------
  730. instance Binary DmdType where
  731. -- Ignore DmdEnv when spitting out the DmdType
  732. put bh (DmdType _ ds dr) = do p <- put bh ds; put_ bh dr; return (castBin p)
  733. get bh = do ds <- get bh; dr <- get bh; return (DmdType emptyVarEnv ds dr)
  734. instance Binary Demand where
  735. put_ bh Top = do
  736. putByte bh 0
  737. put_ bh Abs = do
  738. putByte bh 1
  739. put_ bh (Call aa) = do
  740. putByte bh 2
  741. put_ bh aa
  742. put_ bh (Eval ab) = do
  743. putByte bh 3
  744. put_ bh ab
  745. put_ bh (Defer ac) = do
  746. putByte bh 4
  747. put_ bh ac
  748. put_ bh (Box ad) = do
  749. putByte bh 5
  750. put_ bh ad
  751. put_ bh Bot = do
  752. putByte bh 6
  753. get bh = do
  754. h <- getByte bh
  755. case h of
  756. 0 -> do return Top
  757. 1 -> do return Abs
  758. 2 -> do aa <- get bh
  759. return (Call aa)
  760. 3 -> do ab <- get bh
  761. return (Eval ab)
  762. 4 -> do ac <- get bh
  763. return (Defer ac)
  764. 5 -> do ad <- get bh
  765. return (Box ad)
  766. _ -> do return Bot
  767. instance Binary Demands where
  768. put_ bh (Poly aa) = do
  769. putByte bh 0
  770. put_ bh aa
  771. put_ bh (Prod ab) = do
  772. putByte bh 1
  773. put_ bh ab
  774. get bh = do
  775. h <- getByte bh
  776. case h of
  777. 0 -> do aa <- get bh
  778. return (Poly aa)
  779. _ -> do ab <- get bh
  780. return (Prod ab)
  781. instance Binary DmdResult where
  782. put_ bh TopRes = do
  783. putByte bh 0
  784. put_ bh RetCPR = do
  785. putByte bh 1
  786. put_ bh BotRes = do
  787. putByte bh 2
  788. get bh = do
  789. h <- getByte bh
  790. case h of
  791. 0 -> do return TopRes
  792. 1 -> do return RetCPR -- Really use RetCPR even if -fcpr-off
  793. -- The wrapper was generated for CPR in
  794. -- the imported module!
  795. _ -> do return BotRes
  796. instance Binary StrictSig where
  797. put_ bh (StrictSig aa) = do
  798. put_ bh aa
  799. get bh = do
  800. aa <- get bh
  801. return (StrictSig aa)
  802. -------------------------------------------------------------------------
  803. -- Types from: CostCentre
  804. -------------------------------------------------------------------------
  805. instance Binary IsCafCC where
  806. put_ bh CafCC = do
  807. putByte bh 0
  808. put_ bh NotCafCC = do
  809. putByte bh 1
  810. get bh = do
  811. h <- getByte bh
  812. case h of
  813. 0 -> do return CafCC
  814. _ -> do return NotCafCC
  815. instance Binary CostCentre where
  816. put_ bh (NormalCC aa ab ac _ad ae) = do
  817. putByte bh 0
  818. put_ bh aa
  819. put_ bh ab
  820. put_ bh ac
  821. put_ bh ae
  822. put_ bh (AllCafsCC ae _af) = do
  823. putByte bh 1
  824. put_ bh ae
  825. get bh = do
  826. h <- getByte bh
  827. case h of
  828. 0 -> do aa <- get bh
  829. ab <- get bh
  830. ac <- get bh
  831. ae <- get bh
  832. return (NormalCC aa ab ac noSrcSpan ae)
  833. _ -> do ae <- get bh
  834. return (AllCafsCC ae noSrcSpan)
  835. -- We ignore the SrcSpans in CostCentres when we serialise them,
  836. -- and set the SrcSpans to noSrcSpan when deserialising. This is
  837. -- ok, because we only need the SrcSpan when declaring the
  838. -- CostCentre in the original module, it is not used by importing
  839. -- modules.
  840. -------------------------------------------------------------------------
  841. -- IfaceTypes and friends
  842. -------------------------------------------------------------------------
  843. instance Binary IfaceBndr where
  844. put_ bh (IfaceIdBndr aa) = do
  845. putByte bh 0
  846. put_ bh aa
  847. put_ bh (IfaceTvBndr ab) = do
  848. putByte bh 1
  849. put_ bh ab
  850. get bh = do
  851. h <- getByte bh
  852. case h of
  853. 0 -> do aa <- get bh
  854. return (IfaceIdBndr aa)
  855. _ -> do ab <- get bh
  856. return (IfaceTvBndr ab)
  857. instance Binary IfaceLetBndr where
  858. put_ bh (IfLetBndr a b c) = do
  859. put_ bh a
  860. put_ bh b
  861. put_ bh c
  862. get bh = do a <- get bh
  863. b <- get bh
  864. c <- get bh
  865. return (IfLetBndr a b c)
  866. instance Binary IfaceType where
  867. put_ bh (IfaceForAllTy aa ab) = do
  868. putByte bh 0
  869. put_ bh aa
  870. put_ bh ab
  871. put_ bh (IfaceTyVar ad) = do
  872. putByte bh 1
  873. put_ bh ad
  874. put_ bh (IfaceAppTy ae af) = do
  875. putByte bh 2
  876. put_ bh ae
  877. put_ bh af
  878. put_ bh (IfaceFunTy ag ah) = do
  879. putByte bh 3
  880. put_ bh ag
  881. put_ bh ah
  882. put_ bh (IfaceCoConApp cc tys)
  883. = do { putByte bh 4; put_ bh cc; put_ bh tys }
  884. put_ bh (IfaceTyConApp tc tys)
  885. = do { putByte bh 5; put_ bh tc; put_ bh tys }
  886. put_ bh (IfaceLitTy n)
  887. = do { putByte bh 30; put_ bh n }
  888. get bh = do
  889. h <- getByte bh
  890. case h of
  891. 0 -> do aa <- get bh
  892. ab <- get bh
  893. return (IfaceForAllTy aa ab)
  894. 1 -> do ad <- get bh
  895. return (IfaceTyVar ad)
  896. 2 -> do ae <- get bh
  897. af <- get bh
  898. return (IfaceAppTy ae af)
  899. 3 -> do ag <- get bh
  900. ah <- get bh
  901. return (IfaceFunTy ag ah)
  902. 4 -> do { cc <- get bh; tys <- get bh
  903. ; return (IfaceCoConApp cc tys) }
  904. 5 -> do { tc <- get bh; tys <- get bh
  905. ; return (IfaceTyConApp tc tys) }
  906. 30 -> do n <- get bh
  907. return (IfaceLitTy n)
  908. _ -> panic ("get IfaceType " ++ show h)
  909. instance Binary IfaceTyLit where
  910. put_ bh (IfaceNumTyLit n) = putByte bh 1 >> put_ bh n
  911. put_ bh (IfaceStrTyLit n) = putByte bh 2 >> put_ bh n
  912. get bh =
  913. do tag <- getByte bh
  914. case tag of
  915. 1 -> do { n <- get bh
  916. ; return (IfaceNumTyLit n) }
  917. 2 -> do { n <- get bh
  918. ; return (IfaceStrTyLit n) }
  919. _ -> panic ("get IfaceTyLit " ++ show tag)
  920. instance Binary IfaceTyCon where
  921. put_ bh (IfaceTc ext) = put_ bh ext
  922. get bh = liftM IfaceTc (get bh)
  923. instance Binary LeftOrRight where
  924. put_ bh CLeft = putByte bh 0
  925. put_ bh CRight = putByte bh 1
  926. get bh = do { h <- getByte bh
  927. ; case h of
  928. 0 -> return CLeft
  929. _ -> return CRight }
  930. instance Binary IfaceCoCon where
  931. put_ bh (IfaceCoAx n) = do { putByte bh 0; put_ bh n }
  932. put_ bh IfaceReflCo = putByte bh 1
  933. put_ bh IfaceUnsafeCo = putByte bh 2
  934. put_ bh IfaceSymCo = putByte bh 3
  935. put_ bh IfaceTransCo = putByte bh 4
  936. put_ bh IfaceInstCo = putByte bh 5
  937. put_ bh (IfaceNthCo d) = do { putByte bh 6; put_ bh d }
  938. put_ bh (IfaceLRCo lr) = do { putByte bh 7; put_ bh lr }
  939. get bh = do
  940. h <- getByte bh
  941. case h of
  942. 0 -> do { n <- get bh; return (IfaceCoAx n) }
  943. 1 -> return IfaceReflCo
  944. 2 -> return IfaceUnsafeCo
  945. 3 -> return IfaceSymCo
  946. 4 -> return IfaceTransCo
  947. 5 -> return IfaceInstCo
  948. 6 -> do { d <- get bh; return (IfaceNthCo d) }
  949. 7 -> do { lr <- get bh; return (IfaceLRCo lr) }
  950. _ -> panic ("get IfaceCoCon " ++ show h)
  951. -------------------------------------------------------------------------
  952. -- IfaceExpr and friends
  953. -------------------------------------------------------------------------
  954. instance Binary IfaceExpr where
  955. put_ bh (IfaceLcl aa) = do
  956. putByte bh 0
  957. put_ bh aa
  958. put_ bh (IfaceType ab) = do
  959. putByte bh 1
  960. put_ bh ab
  961. put_ bh (IfaceCo ab) = do
  962. putByte bh 2
  963. put_ bh ab
  964. put_ bh (IfaceTuple ac ad) = do
  965. putByte bh 3
  966. put_ bh ac
  967. put_ bh ad
  968. put_ bh (IfaceLam ae af) = do
  969. putByte bh 4
  970. put_ bh ae
  971. put_ bh af
  972. put_ bh (IfaceApp ag ah) = do
  973. putByte bh 5
  974. put_ bh ag
  975. put_ bh ah
  976. put_ bh (IfaceCase ai aj ak) = do
  977. putByte bh 6
  978. put_ bh ai
  979. put_ bh aj
  980. put_ bh ak
  981. put_ bh (IfaceLet al am) = do
  982. putByte bh 7
  983. put_ bh al
  984. put_ bh am
  985. put_ bh (IfaceTick an ao) = do
  986. putByte bh 8
  987. put_ bh an
  988. put_ bh ao
  989. put_ bh (IfaceLit ap) = do
  990. putByte bh 9
  991. put_ bh ap
  992. put_ bh (IfaceFCall as at) = do
  993. putByte bh 10
  994. put_ bh as
  995. put_ bh at
  996. put_ bh (IfaceExt aa) = do
  997. putByte bh 11
  998. put_ bh aa
  999. put_ bh (IfaceCast ie ico) = do
  1000. putByte bh 12
  1001. put_ bh ie
  1002. put_ bh ico
  1003. put_ bh (IfaceECase a b) = do
  1004. putByte bh 13
  1005. put_ bh a
  1006. put_ bh b
  1007. get bh = do
  1008. h <- getByte bh
  1009. case h of
  1010. 0 -> do aa <- get bh
  1011. return (IfaceLcl aa)
  1012. 1 -> do ab <- get bh
  1013. return (IfaceType ab)
  1014. 2 -> do ab <- get bh
  1015. return (IfaceCo ab)
  1016. 3 -> do ac <- get bh
  1017. ad <- get bh
  1018. return (IfaceTuple ac ad)
  1019. 4 -> do ae <- get bh
  1020. af <- get bh
  1021. return (IfaceLam ae af)
  1022. 5 -> do ag <- get bh
  1023. ah <- get bh
  1024. return (IfaceApp ag ah)
  1025. 6 -> do ai <- get bh
  1026. aj <- get bh
  1027. ak <- get bh
  1028. return (IfaceCase ai aj ak)
  1029. 7 -> do al <- get bh
  1030. am <- get bh
  1031. return (IfaceLet al am)
  1032. 8 -> do an <- get bh
  1033. ao <- get bh
  1034. return (IfaceTick an ao)
  1035. 9 -> do ap <- get bh
  1036. return (IfaceLit ap)
  1037. 10 -> do as <- get bh
  1038. at <- get bh
  1039. return (IfaceFCall as at)
  1040. 11 -> do aa <- get bh
  1041. return (IfaceExt aa)
  1042. 12 -> do ie <- get bh
  1043. ico <- get bh
  1044. return (IfaceCast ie ico)
  1045. 13 -> do a <- get bh
  1046. b <- get bh
  1047. return (IfaceECase a b)
  1048. _ -> panic ("get IfaceExpr " ++ show h)
  1049. instance Binary IfaceConAlt where
  1050. put_ bh IfaceDefault = putByte bh 0
  1051. put_ bh (IfaceDataAlt aa) = putByte bh 1 >> put_ bh aa
  1052. put_ bh (IfaceLitAlt ac) = putByte bh 2 >> put_ bh ac
  1053. get bh = do
  1054. h <- getByte bh
  1055. case h of
  1056. 0 -> return IfaceDefault
  1057. 1 -> get bh >>= (return . IfaceDataAlt)
  1058. _ -> get bh >>= (return . IfaceLitAlt)
  1059. instance Binary IfaceBinding where
  1060. put_ bh (IfaceNonRec aa ab) = putByte bh 0 >> put_ bh aa >> put_ bh ab
  1061. put_ bh (IfaceRec ac) = putByte bh 1 >> put_ bh ac
  1062. get bh = do
  1063. h <- getByte bh
  1064. case h of
  1065. 0 -> do { aa <- get bh; ab <- get bh; return (IfaceNonRec aa ab) }
  1066. _ -> do { ac <- get bh; return (IfaceRec ac) }
  1067. instance Binary IfaceIdDetails where
  1068. put_ bh IfVanillaId = putByte bh 0
  1069. put_ bh (IfRecSelId a b) = putByte bh 1 >> put_ bh a >> put_ bh b
  1070. put_ bh (IfDFunId n) = do { putByte bh 2; put_ bh n }
  1071. get bh = do
  1072. h <- getByte bh
  1073. case h of
  1074. 0 -> return IfVanillaId
  1075. 1 -> do { a <- get bh; b <- get bh; return (IfRecSelId a b) }
  1076. _ -> do { n <- get bh; return (IfDFunId n) }
  1077. instance Binary (DFunArg IfaceExpr) where
  1078. put_ bh (DFunPolyArg e) = putByte bh 0 >> put_ bh e
  1079. put_ bh (DFunLamArg i) = putByte bh 1 >> put_ bh i
  1080. get bh = do { h <- getByte bh
  1081. ; case h of
  1082. 0 -> do { a <- get bh; return (DFunPolyArg a) }
  1083. _ -> do { a <- get bh; return (DFunLamArg a) } }
  1084. instance Binary IfaceIdInfo where
  1085. put_ bh NoInfo = putByte bh 0
  1086. put_ bh (HasInfo i) = putByte bh 1 >> lazyPut bh i -- NB lazyPut
  1087. get bh = do
  1088. h <- getByte bh
  1089. case h of
  1090. 0 -> return NoInfo
  1091. _ -> lazyGet bh >>= (return . HasInfo) -- NB lazyGet
  1092. instance Binary IfaceInfoItem where
  1093. put_ bh (HsArity aa) = putByte bh 0 >> put_ bh aa
  1094. put_ bh (HsStrictness ab) = putByte bh 1 >> put_ bh ab
  1095. put_ bh (HsUnfold lb ad) = putByte bh 2 >> put_ bh lb >> put_ bh ad
  1096. put_ bh (HsInline ad) = putByte bh 3 >> put_ bh ad
  1097. put_ bh HsNoCafRefs = putByte bh 4
  1098. get bh = do
  1099. h <- getByte bh
  1100. case h of
  1101. 0 -> get bh >>= (return . HsArity)
  1102. 1 -> get bh >>= (return . HsStrictness)
  1103. 2 -> do lb <- get bh
  1104. ad <- get bh
  1105. return (HsUnfold lb ad)
  1106. 3 -> get bh >>= (return . HsInline)
  1107. _ -> return HsNoCafRefs
  1108. instance Binary IfaceUnfolding where
  1109. put_ bh (IfCoreUnfold s e) = do
  1110. putByte bh 0
  1111. put_ bh s
  1112. put_ bh e
  1113. put_ bh (IfInlineRule a b c d) = do
  1114. putByte bh 1
  1115. put_ bh a
  1116. put_ bh b
  1117. put_ bh c
  1118. put_ bh d
  1119. put_ bh (IfLclWrapper a n) = do
  1120. putByte bh 2
  1121. put_ bh a
  1122. put_ bh n
  1123. put_ bh (IfExtWrapper a n) = do
  1124. putByte bh 3
  1125. put_ bh a
  1126. put_ bh n
  1127. put_ bh (IfDFunUnfold as) = do
  1128. putByte bh 4
  1129. put_ bh as
  1130. put_ bh (IfCompulsory e) = do
  1131. putByte bh 5
  1132. put_ bh e
  1133. get bh = do
  1134. h <- getByte bh
  1135. case h of
  1136. 0 -> do s <- get bh
  1137. e <- get bh
  1138. return (IfCoreUnfold s e)
  1139. 1 -> do a <- get bh
  1140. b <- get bh
  1141. c <- get bh
  1142. d <- get bh
  1143. return (IfInlineRule a b c d)
  1144. 2 -> do a <- get bh
  1145. n <- get bh
  1146. return (IfLclWrapper a n)
  1147. 3 -> do a <- get bh
  1148. n <- get bh
  1149. return (IfExtWrapper a n)
  1150. 4 -> do as <- get bh
  1151. return (IfDFunUnfold as)
  1152. _ -> do e <- get bh
  1153. return (IfCompulsory e)
  1154. instance Binary IfaceTickish where
  1155. put_ bh (IfaceHpcTick m ix) = do
  1156. putByte bh 0
  1157. put_ bh m
  1158. put_ bh ix
  1159. put_ bh (IfaceSCC cc tick push) = do
  1160. putByte bh 1
  1161. put_ bh cc
  1162. put_ bh tick
  1163. put_ bh push
  1164. get bh = do
  1165. h <- getByte bh
  1166. case h of
  1167. 0 -> do m <- get bh
  1168. ix <- get bh
  1169. return (IfaceHpcTick m ix)
  1170. 1 -> do cc <- get bh
  1171. tick <- get bh
  1172. push <- get bh
  1173. return (IfaceSCC cc tick push)
  1174. _ -> panic ("get IfaceTickish " ++ show h)
  1175. -------------------------------------------------------------------------
  1176. -- IfaceDecl and friends
  1177. -------------------------------------------------------------------------
  1178. -- A bit of magic going on here: there's no need to store the OccName
  1179. -- for a decl on the disk, since we can infer the namespace from the
  1180. -- context; however it is useful to have the OccName in the IfaceDecl
  1181. -- to avoid re-building it in various places. So we build the OccName
  1182. -- when de-serialising.
  1183. instance Binary IfaceDecl where
  1184. put_ bh (IfaceId name ty details idinfo) = do
  1185. putByte bh 0
  1186. put_ bh (occNameFS name)
  1187. put_ bh ty
  1188. put_ bh details
  1189. put_ bh idinfo
  1190. put_ _ (IfaceForeign _ _) =
  1191. error "Binary.put_(IfaceDecl): IfaceForeign"
  1192. put_ bh (IfaceData a1 a2 a3 a4 a5 a6 a7 a8) = do
  1193. putByte bh 2
  1194. put_ bh (occNameFS a1)
  1195. put_ bh a2
  1196. put_ bh a3
  1197. put_ bh a4
  1198. put_ bh a5
  1199. put_ bh a6
  1200. put_ bh a7
  1201. put_ bh a8
  1202. put_ bh (IfaceSyn a1 a2 a3 a4) = do
  1203. putByte bh 3
  1204. put_ bh (occNameFS a1)
  1205. put_ bh a2
  1206. put_ bh a3
  1207. put_ bh a4
  1208. put_ bh (IfaceClass a1 a2 a3 a4 a5 a6 a7) = do
  1209. putByte bh 4
  1210. put_ bh a1
  1211. put_ bh (occNameFS a2)
  1212. put_ bh a3
  1213. put_ bh a4
  1214. put_ bh a5
  1215. put_ bh a6
  1216. put_ bh a7
  1217. put_ bh (IfaceAxiom a1 a2 a3 a4) = do
  1218. putByte bh 5
  1219. put_ bh (occNameFS a1)
  1220. put_ bh a2
  1221. put_ bh a3
  1222. put_ bh a4
  1223. get bh = do
  1224. h <- getByte bh
  1225. case h of
  1226. 0 -> do name <- get bh
  1227. ty <- get bh
  1228. details <- get bh
  1229. idinfo <- get bh
  1230. occ <- return $! mkOccNameFS varName name
  1231. return (IfaceId occ ty details idinfo)
  1232. 1 -> error "Binary.get(TyClDecl): ForeignType"
  1233. 2 -> do a1 <- get bh
  1234. a2 <- get bh
  1235. a3 <- get bh
  1236. a4 <- get bh
  1237. a5 <- get bh
  1238. a6 <- get bh
  1239. a7 <- get bh
  1240. a8 <- get bh
  1241. occ <- return $! mkOccNameFS tcName a1
  1242. return (IfaceData occ a2 a3 a4 a5 a6 a7 a8)
  1243. 3 -> do a1 <- get bh
  1244. a2 <- get bh
  1245. a3 <- get bh
  1246. a4 <- get bh
  1247. occ <- return $! mkOccNameFS tcName a1
  1248. return (IfaceSyn occ a2 a3 a4)
  1249. 4 -> do a1 <- get bh
  1250. a2 <- get bh
  1251. a3 <- get bh
  1252. a4 <- get bh
  1253. a5 <- get bh
  1254. a6 <- get bh
  1255. a7 <- get bh
  1256. occ <- return $! mkOccNameFS clsName a2
  1257. return (IfaceClass a1 occ a3 a4 a5 a6 a7)
  1258. _ -> do a1 <- get bh
  1259. a2 <- get bh
  1260. a3 <- get bh
  1261. a4 <- get bh
  1262. occ <- return $! mkOccNameFS tcName a1
  1263. return (IfaceAxiom occ a2 a3 a4)
  1264. instance Binary ty => Binary (SynTyConRhs ty) where
  1265. put_ bh (SynFamilyTyCon a b) = putByte bh 0 >> put_ bh a >> put_ bh b
  1266. put_ bh (SynonymTyCon ty) = putByte bh 1 >> put_ bh ty
  1267. get bh = do { h <- getByte bh
  1268. ; case h of
  1269. 0 -> do { a <- get bh
  1270. ; b <- get bh
  1271. ; return (SynFamilyTyCon a b) }
  1272. _ -> do { ty <- get bh
  1273. ; return (SynonymTyCon ty) } }
  1274. instance Binary IfaceClsInst where
  1275. put_ bh (IfaceClsInst cls tys dfun flag orph) = do
  1276. put_ bh cls
  1277. put_ bh tys
  1278. put_ bh dfun
  1279. put_ bh flag
  1280. put_ bh orph
  1281. get bh = do
  1282. cls <- get bh
  1283. tys <- get bh
  1284. dfun <- get bh
  1285. flag <- get bh
  1286. orph <- get bh
  1287. return (IfaceClsInst cls tys dfun flag orph)
  1288. instance Binary IfaceFamInst where
  1289. put_ bh (IfaceFamInst fam tys name orph) = do
  1290. put_ bh fam
  1291. put_ bh tys
  1292. put_ bh name
  1293. put_ bh orph
  1294. get bh = do
  1295. fam <- get bh
  1296. tys <- get bh
  1297. name <- get bh
  1298. orph <- get bh
  1299. return (IfaceFamInst fam tys name orph)
  1300. instance Binary OverlapFlag where
  1301. put_ bh (NoOverlap b) = putByte bh 0 >> put_ bh b
  1302. put_ bh (OverlapOk b) = putByte bh 1 >> put_ bh b
  1303. put_ bh (Incoherent b) = putByte bh 2 >> put_ bh b
  1304. get bh = do
  1305. h <- getByte bh
  1306. b <- get bh
  1307. case h of
  1308. 0 -> return $ NoOverlap b
  1309. 1 -> return $ OverlapOk b
  1310. 2 -> return $ Incoherent b
  1311. _ -> panic ("get OverlapFlag " ++ show h)
  1312. instance Binary IfaceConDecls where
  1313. put_ bh (IfAbstractTyCon d) = putByte bh 0 >> put_ bh d
  1314. put_ bh IfDataFamTyCon = putByte bh 1
  1315. put_ bh (IfDataTyCon cs) = putByte bh 2 >> put_ bh cs
  1316. put_ bh (IfNewTyCon c) = putByte bh 3 >> put_ bh c
  1317. get bh = do
  1318. h <- getByte bh
  1319. case h of
  1320. 0 -> get bh >>= (return . IfAbstractTyCon)
  1321. 1 -> return IfDataFamTyCon
  1322. 2 -> get bh >>= (return . IfDataTyCon)
  1323. _ -> get bh >>= (return . IfNewTyCon)
  1324. instance Binary IfaceConDecl where
  1325. put_ bh (IfCon a1 a2 a3 a4 a5 a6 a7 a8 a9 a10) = do
  1326. put_ bh a1
  1327. put_ bh a2
  1328. put_ bh a3
  1329. put_ bh a4
  1330. put_ bh a5
  1331. put_ bh a6
  1332. put_ bh a7
  1333. put_ bh a8
  1334. put_ bh a9
  1335. put_ bh a10
  1336. get bh = do
  1337. a1 <- get bh
  1338. a2 <- get bh
  1339. a3 <- get bh
  1340. a4 <- get bh
  1341. a5 <- get bh
  1342. a6 <- get bh
  1343. a7 <- get bh
  1344. a8 <- get bh
  1345. a9 <- get bh
  1346. a10 <- get bh
  1347. return (IfCon a1 a2 a3 a4 a5 a6 a7 a8 a9 a10)
  1348. instance Binary IfaceAT where
  1349. put_ bh (IfaceAT dec defs) = do
  1350. put_ bh dec
  1351. put_ bh defs
  1352. get bh = do
  1353. dec <- get bh

Large files files are truncated, but you can click here to view the full file