vectorise: Put it out of its misery
[ghc.git] / compiler / backpack / RnModIface.hs
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE LambdaCase #-}
3
4 -- | This module implements interface renaming, which is
5 -- used to rewrite interface files on the fly when we
6 -- are doing indefinite typechecking and need instantiations
7 -- of modules which do not necessarily exist yet.
8
9 module RnModIface(
10 rnModIface,
11 rnModExports,
12 tcRnModIface,
13 tcRnModExports,
14 ) where
15
16 #include "HsVersions.h"
17
18 import GhcPrelude
19
20 import SrcLoc
21 import Outputable
22 import HscTypes
23 import Module
24 import UniqFM
25 import Avail
26 import IfaceSyn
27 import FieldLabel
28 import Var
29 import ErrUtils
30
31 import Name
32 import TcRnMonad
33 import Util
34 import Fingerprint
35 import BasicTypes
36
37 -- a bit vexing
38 import {-# SOURCE #-} LoadIface
39 import DynFlags
40
41 import qualified Data.Traversable as T
42
43 import Bag
44 import Data.IORef
45 import NameShape
46 import IfaceEnv
47
48 tcRnMsgMaybe :: IO (Either ErrorMessages a) -> TcM a
49 tcRnMsgMaybe do_this = do
50 r <- liftIO $ do_this
51 case r of
52 Left errs -> do
53 addMessages (emptyBag, errs)
54 failM
55 Right x -> return x
56
57 tcRnModIface :: [(ModuleName, Module)] -> Maybe NameShape -> ModIface -> TcM ModIface
58 tcRnModIface x y z = do
59 hsc_env <- getTopEnv
60 tcRnMsgMaybe $ rnModIface hsc_env x y z
61
62 tcRnModExports :: [(ModuleName, Module)] -> ModIface -> TcM [AvailInfo]
63 tcRnModExports x y = do
64 hsc_env <- getTopEnv
65 tcRnMsgMaybe $ rnModExports hsc_env x y
66
67 failWithRn :: SDoc -> ShIfM a
68 failWithRn doc = do
69 errs_var <- fmap sh_if_errs getGblEnv
70 dflags <- getDynFlags
71 errs <- readTcRef errs_var
72 -- TODO: maybe associate this with a source location?
73 writeTcRef errs_var (errs `snocBag` mkPlainErrMsg dflags noSrcSpan doc)
74 failM
75
76 -- | What we have is a generalized ModIface, which corresponds to
77 -- a module that looks like p[A=<A>]:B. We need a *specific* ModIface, e.g.
78 -- p[A=q():A]:B (or maybe even p[A=<B>]:B) which we load
79 -- up (either to merge it, or to just use during typechecking).
80 --
81 -- Suppose we have:
82 --
83 -- p[A=<A>]:M ==> p[A=q():A]:M
84 --
85 -- Substitute all occurrences of <A> with q():A (renameHoleModule).
86 -- Then, for any Name of form {A.T}, replace the Name with
87 -- the Name according to the exports of the implementing module.
88 -- This works even for p[A=<B>]:M, since we just read in the
89 -- exports of B.hi, which is assumed to be ready now.
90 --
91 -- This function takes an optional 'NameShape', which can be used
92 -- to further refine the identities in this interface: suppose
93 -- we read a declaration for {H.T} but we actually know that this
94 -- should be Foo.T; then we'll also rename this (this is used
95 -- when loading an interface to merge it into a requirement.)
96 rnModIface :: HscEnv -> [(ModuleName, Module)] -> Maybe NameShape
97 -> ModIface -> IO (Either ErrorMessages ModIface)
98 rnModIface hsc_env insts nsubst iface = do
99 initRnIface hsc_env iface insts nsubst $ do
100 mod <- rnModule (mi_module iface)
101 sig_of <- case mi_sig_of iface of
102 Nothing -> return Nothing
103 Just x -> fmap Just (rnModule x)
104 exports <- mapM rnAvailInfo (mi_exports iface)
105 decls <- mapM rnIfaceDecl' (mi_decls iface)
106 insts <- mapM rnIfaceClsInst (mi_insts iface)
107 fams <- mapM rnIfaceFamInst (mi_fam_insts iface)
108 deps <- rnDependencies (mi_deps iface)
109 -- TODO:
110 -- mi_rules
111 return iface { mi_module = mod
112 , mi_sig_of = sig_of
113 , mi_insts = insts
114 , mi_fam_insts = fams
115 , mi_exports = exports
116 , mi_decls = decls
117 , mi_deps = deps }
118
119 -- | Rename just the exports of a 'ModIface'. Useful when we're doing
120 -- shaping prior to signature merging.
121 rnModExports :: HscEnv -> [(ModuleName, Module)] -> ModIface -> IO (Either ErrorMessages [AvailInfo])
122 rnModExports hsc_env insts iface
123 = initRnIface hsc_env iface insts Nothing
124 $ mapM rnAvailInfo (mi_exports iface)
125
126 rnDependencies :: Rename Dependencies
127 rnDependencies deps = do
128 orphs <- rnDepModules dep_orphs deps
129 finsts <- rnDepModules dep_finsts deps
130 return deps { dep_orphs = orphs, dep_finsts = finsts }
131
132 rnDepModules :: (Dependencies -> [Module]) -> Dependencies -> ShIfM [Module]
133 rnDepModules sel deps = do
134 hsc_env <- getTopEnv
135 hmap <- getHoleSubst
136 -- NB: It's not necessary to test if we're doing signature renaming,
137 -- because ModIface will never contain module reference for itself
138 -- in these dependencies.
139 fmap (nubSort . concat) . T.forM (sel deps) $ \mod -> do
140 dflags <- getDynFlags
141 let mod' = renameHoleModule dflags hmap mod
142 iface <- liftIO . initIfaceCheck (text "rnDepModule") hsc_env
143 $ loadSysInterface (text "rnDepModule") mod'
144 return (mod' : sel (mi_deps iface))
145
146 {-
147 ************************************************************************
148 * *
149 ModIface substitution
150 * *
151 ************************************************************************
152 -}
153
154 -- | Run a computation in the 'ShIfM' monad.
155 initRnIface :: HscEnv -> ModIface -> [(ModuleName, Module)] -> Maybe NameShape
156 -> ShIfM a -> IO (Either ErrorMessages a)
157 initRnIface hsc_env iface insts nsubst do_this = do
158 errs_var <- newIORef emptyBag
159 let dflags = hsc_dflags hsc_env
160 hsubst = listToUFM insts
161 rn_mod = renameHoleModule dflags hsubst
162 env = ShIfEnv {
163 sh_if_module = rn_mod (mi_module iface),
164 sh_if_semantic_module = rn_mod (mi_semantic_module iface),
165 sh_if_hole_subst = listToUFM insts,
166 sh_if_shape = nsubst,
167 sh_if_errs = errs_var
168 }
169 -- Modeled off of 'initTc'
170 res <- initTcRnIf 'c' hsc_env env () $ tryM do_this
171 msgs <- readIORef errs_var
172 case res of
173 Left _ -> return (Left msgs)
174 Right r | not (isEmptyBag msgs) -> return (Left msgs)
175 | otherwise -> return (Right r)
176
177 -- | Environment for 'ShIfM' monads.
178 data ShIfEnv = ShIfEnv {
179 -- What we are renaming the ModIface to. It assumed that
180 -- the original mi_module of the ModIface is
181 -- @generalizeModule (mi_module iface)@.
182 sh_if_module :: Module,
183 -- The semantic module that we are renaming to
184 sh_if_semantic_module :: Module,
185 -- Cached hole substitution, e.g.
186 -- @sh_if_hole_subst == listToUFM . unitIdInsts . moduleUnitId . sh_if_module@
187 sh_if_hole_subst :: ShHoleSubst,
188 -- An optional name substitution to be applied when renaming
189 -- the names in the interface. If this is 'Nothing', then
190 -- we just load the target interface and look at the export
191 -- list to determine the renaming.
192 sh_if_shape :: Maybe NameShape,
193 -- Mutable reference to keep track of errors (similar to 'tcl_errs')
194 sh_if_errs :: IORef ErrorMessages
195 }
196
197 getHoleSubst :: ShIfM ShHoleSubst
198 getHoleSubst = fmap sh_if_hole_subst getGblEnv
199
200 type ShIfM = TcRnIf ShIfEnv ()
201 type Rename a = a -> ShIfM a
202
203
204 rnModule :: Rename Module
205 rnModule mod = do
206 hmap <- getHoleSubst
207 dflags <- getDynFlags
208 return (renameHoleModule dflags hmap mod)
209
210 rnAvailInfo :: Rename AvailInfo
211 rnAvailInfo (Avail n) = Avail <$> rnIfaceGlobal n
212 rnAvailInfo (AvailTC n ns fs) = do
213 -- Why don't we rnIfaceGlobal the availName itself? It may not
214 -- actually be exported by the module it putatively is from, in
215 -- which case we won't be able to tell what the name actually
216 -- is. But for the availNames they MUST be exported, so they
217 -- will rename fine.
218 ns' <- mapM rnIfaceGlobal ns
219 fs' <- mapM rnFieldLabel fs
220 case ns' ++ map flSelector fs' of
221 [] -> panic "rnAvailInfoEmpty AvailInfo"
222 (rep:rest) -> ASSERT2( all ((== nameModule rep) . nameModule) rest, ppr rep $$ hcat (map ppr rest) ) do
223 n' <- setNameModule (Just (nameModule rep)) n
224 return (AvailTC n' ns' fs')
225
226 rnFieldLabel :: Rename FieldLabel
227 rnFieldLabel (FieldLabel l b sel) = do
228 sel' <- rnIfaceGlobal sel
229 return (FieldLabel l b sel')
230
231
232
233
234 -- | The key function. This gets called on every Name embedded
235 -- inside a ModIface. Our job is to take a Name from some
236 -- generalized unit ID p[A=<A>, B=<B>], and change
237 -- it to the correct name for a (partially) instantiated unit
238 -- ID, e.g. p[A=q[]:A, B=<B>].
239 --
240 -- There are two important things to do:
241 --
242 -- If a hole is substituted with a real module implementation,
243 -- we need to look at that actual implementation to determine what
244 -- the true identity of this name should be. We'll do this by
245 -- loading that module's interface and looking at the mi_exports.
246 --
247 -- However, there is one special exception: when we are loading
248 -- the interface of a requirement. In this case, we may not have
249 -- the "implementing" interface, because we are reading this
250 -- interface precisely to "merge it in".
251 --
252 -- External case:
253 -- p[A=<B>]:A (and thisUnitId is something else)
254 -- We are loading this in order to determine B.hi! So
255 -- don't load B.hi to find the exports.
256 --
257 -- Local case:
258 -- p[A=<A>]:A (and thisUnitId is p[A=<A>])
259 -- This should not happen, because the rename is not necessary
260 -- in this case, but if it does we shouldn't load A.hi!
261 --
262 -- Compare me with 'tcIfaceGlobal'!
263
264 -- In effect, this function needs compute the name substitution on the
265 -- fly. What it has is the name that we would like to substitute.
266 -- If the name is not a hole name {M.x} (e.g. isHoleModule) then
267 -- no renaming can take place (although the inner hole structure must
268 -- be updated to account for the hole module renaming.)
269 rnIfaceGlobal :: Name -> ShIfM Name
270 rnIfaceGlobal n = do
271 hsc_env <- getTopEnv
272 let dflags = hsc_dflags hsc_env
273 iface_semantic_mod <- fmap sh_if_semantic_module getGblEnv
274 mb_nsubst <- fmap sh_if_shape getGblEnv
275 hmap <- getHoleSubst
276 let m = nameModule n
277 m' = renameHoleModule dflags hmap m
278 case () of
279 -- Did we encounter {A.T} while renaming p[A=<B>]:A? If so,
280 -- do NOT assume B.hi is available.
281 -- In this case, rename {A.T} to {B.T} but don't look up exports.
282 _ | m' == iface_semantic_mod
283 , isHoleModule m'
284 -- NB: this could be Nothing for computeExports, we have
285 -- nothing to say.
286 -> do n' <- setNameModule (Just m') n
287 case mb_nsubst of
288 Nothing -> return n'
289 Just nsubst ->
290 case maybeSubstNameShape nsubst n' of
291 -- TODO: would love to have context
292 -- TODO: This will give an unpleasant message if n'
293 -- is a constructor; then we'll suggest adding T
294 -- but it won't work.
295 Nothing -> failWithRn $ vcat [
296 text "The identifier" <+> ppr (occName n') <+>
297 text "does not exist in the local signature.",
298 parens (text "Try adding it to the export list of the hsig file.")
299 ]
300 Just n'' -> return n''
301 -- Fastpath: we are renaming p[H=<H>]:A.T, in which case the
302 -- export list is irrelevant.
303 | not (isHoleModule m)
304 -> setNameModule (Just m') n
305 -- The substitution was from <A> to p[]:A.
306 -- But this does not mean {A.T} goes to p[]:A.T:
307 -- p[]:A may reexport T from somewhere else. Do the name
308 -- substitution. Furthermore, we need
309 -- to make sure we pick the accurate name NOW,
310 -- or we might accidentally reject a merge.
311 | otherwise
312 -> do -- Make sure we look up the local interface if substitution
313 -- went from <A> to <B>.
314 let m'' = if isHoleModule m'
315 -- Pull out the local guy!!
316 then mkModule (thisPackage dflags) (moduleName m')
317 else m'
318 iface <- liftIO . initIfaceCheck (text "rnIfaceGlobal") hsc_env
319 $ loadSysInterface (text "rnIfaceGlobal") m''
320 let nsubst = mkNameShape (moduleName m) (mi_exports iface)
321 case maybeSubstNameShape nsubst n of
322 Nothing -> failWithRn $ vcat [
323 text "The identifier" <+> ppr (occName n) <+>
324 -- NB: report m' because it's more user-friendly
325 text "does not exist in the signature for" <+> ppr m',
326 parens (text "Try adding it to the export list in that hsig file.")
327 ]
328 Just n' -> return n'
329
330 -- | Rename an implicit name, e.g., a DFun or coercion axiom.
331 -- Here is where we ensure that DFuns have the correct module as described in
332 -- Note [rnIfaceNeverExported].
333 rnIfaceNeverExported :: Name -> ShIfM Name
334 rnIfaceNeverExported name = do
335 hmap <- getHoleSubst
336 dflags <- getDynFlags
337 iface_semantic_mod <- fmap sh_if_semantic_module getGblEnv
338 let m = renameHoleModule dflags hmap $ nameModule name
339 -- Doublecheck that this DFun/coercion axiom was, indeed, locally defined.
340 MASSERT2( iface_semantic_mod == m, ppr iface_semantic_mod <+> ppr m )
341 setNameModule (Just m) name
342
343 -- Note [rnIfaceNeverExported]
344 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
345 -- For the high-level overview, see
346 -- Note [Handling never-exported TyThings under Backpack]
347 --
348 -- When we see a reference to an entity that was defined in a signature,
349 -- 'rnIfaceGlobal' relies on the identifier in question being part of the
350 -- exports of the implementing 'ModIface', so that we can use the exports to
351 -- decide how to rename the identifier. Unfortunately, references to 'DFun's
352 -- and 'CoAxiom's will run into trouble under this strategy, because they are
353 -- never exported.
354 --
355 -- Let us consider first what should happen in the absence of promotion. In
356 -- this setting, a reference to a 'DFun' or a 'CoAxiom' can only occur inside
357 -- the signature *that is defining it* (as there are no Core terms in
358 -- typechecked-only interface files, there's no way for a reference to occur
359 -- besides from the defining 'ClsInst' or closed type family). Thus,
360 -- it doesn't really matter what names we give the DFun/CoAxiom, as long
361 -- as it's consistent between the declaration site and the use site.
362 --
363 -- We have to make sure that these bogus names don't get propagated,
364 -- but it is fine: see Note [Signature merging DFuns] for the fixups
365 -- to the names we do before writing out the merged interface.
366 -- (It's even easier for instantiation, since the DFuns all get
367 -- dropped entirely; the instances are reexported implicitly.)
368 --
369 -- Unfortunately, this strategy is not enough in the presence of promotion
370 -- (see bug #13149), where modules which import the signature may make
371 -- reference to their coercions. It's not altogether clear how to
372 -- fix this case, but it is definitely a bug!
373
374 -- PILES AND PILES OF BOILERPLATE
375
376 -- | Rename an 'IfaceClsInst', with special handling for an associated
377 -- dictionary function.
378 rnIfaceClsInst :: Rename IfaceClsInst
379 rnIfaceClsInst cls_inst = do
380 n <- rnIfaceGlobal (ifInstCls cls_inst)
381 tys <- mapM rnMaybeIfaceTyCon (ifInstTys cls_inst)
382
383 dfun <- rnIfaceNeverExported (ifDFun cls_inst)
384 return cls_inst { ifInstCls = n
385 , ifInstTys = tys
386 , ifDFun = dfun
387 }
388
389 rnMaybeIfaceTyCon :: Rename (Maybe IfaceTyCon)
390 rnMaybeIfaceTyCon Nothing = return Nothing
391 rnMaybeIfaceTyCon (Just tc) = Just <$> rnIfaceTyCon tc
392
393 rnIfaceFamInst :: Rename IfaceFamInst
394 rnIfaceFamInst d = do
395 fam <- rnIfaceGlobal (ifFamInstFam d)
396 tys <- mapM rnMaybeIfaceTyCon (ifFamInstTys d)
397 axiom <- rnIfaceGlobal (ifFamInstAxiom d)
398 return d { ifFamInstFam = fam, ifFamInstTys = tys, ifFamInstAxiom = axiom }
399
400 rnIfaceDecl' :: Rename (Fingerprint, IfaceDecl)
401 rnIfaceDecl' (fp, decl) = (,) fp <$> rnIfaceDecl decl
402
403 rnIfaceDecl :: Rename IfaceDecl
404 rnIfaceDecl d@IfaceId{} = do
405 name <- case ifIdDetails d of
406 IfDFunId -> rnIfaceNeverExported (ifName d)
407 _ | isDefaultMethodOcc (occName (ifName d))
408 -> rnIfaceNeverExported (ifName d)
409 -- Typeable bindings. See Note [Grand plan for Typeable].
410 _ | isTypeableBindOcc (occName (ifName d))
411 -> rnIfaceNeverExported (ifName d)
412 | otherwise -> rnIfaceGlobal (ifName d)
413 ty <- rnIfaceType (ifType d)
414 details <- rnIfaceIdDetails (ifIdDetails d)
415 info <- rnIfaceIdInfo (ifIdInfo d)
416 return d { ifName = name
417 , ifType = ty
418 , ifIdDetails = details
419 , ifIdInfo = info
420 }
421 rnIfaceDecl d@IfaceData{} = do
422 name <- rnIfaceGlobal (ifName d)
423 binders <- mapM rnIfaceTyConBinder (ifBinders d)
424 ctxt <- mapM rnIfaceType (ifCtxt d)
425 cons <- rnIfaceConDecls (ifCons d)
426 res_kind <- rnIfaceType (ifResKind d)
427 parent <- rnIfaceTyConParent (ifParent d)
428 return d { ifName = name
429 , ifBinders = binders
430 , ifCtxt = ctxt
431 , ifCons = cons
432 , ifResKind = res_kind
433 , ifParent = parent
434 }
435 rnIfaceDecl d@IfaceSynonym{} = do
436 name <- rnIfaceGlobal (ifName d)
437 binders <- mapM rnIfaceTyConBinder (ifBinders d)
438 syn_kind <- rnIfaceType (ifResKind d)
439 syn_rhs <- rnIfaceType (ifSynRhs d)
440 return d { ifName = name
441 , ifBinders = binders
442 , ifResKind = syn_kind
443 , ifSynRhs = syn_rhs
444 }
445 rnIfaceDecl d@IfaceFamily{} = do
446 name <- rnIfaceGlobal (ifName d)
447 binders <- mapM rnIfaceTyConBinder (ifBinders d)
448 fam_kind <- rnIfaceType (ifResKind d)
449 fam_flav <- rnIfaceFamTyConFlav (ifFamFlav d)
450 return d { ifName = name
451 , ifBinders = binders
452 , ifResKind = fam_kind
453 , ifFamFlav = fam_flav
454 }
455 rnIfaceDecl d@IfaceClass{} = do
456 name <- rnIfaceGlobal (ifName d)
457 binders <- mapM rnIfaceTyConBinder (ifBinders d)
458 body <- rnIfaceClassBody (ifBody d)
459 return d { ifName = name
460 , ifBinders = binders
461 , ifBody = body
462 }
463 rnIfaceDecl d@IfaceAxiom{} = do
464 name <- rnIfaceNeverExported (ifName d)
465 tycon <- rnIfaceTyCon (ifTyCon d)
466 ax_branches <- mapM rnIfaceAxBranch (ifAxBranches d)
467 return d { ifName = name
468 , ifTyCon = tycon
469 , ifAxBranches = ax_branches
470 }
471 rnIfaceDecl d@IfacePatSyn{} = do
472 name <- rnIfaceGlobal (ifName d)
473 let rnPat (n, b) = (,) <$> rnIfaceGlobal n <*> pure b
474 pat_matcher <- rnPat (ifPatMatcher d)
475 pat_builder <- T.traverse rnPat (ifPatBuilder d)
476 pat_univ_bndrs <- mapM rnIfaceForAllBndr (ifPatUnivBndrs d)
477 pat_ex_bndrs <- mapM rnIfaceForAllBndr (ifPatExBndrs d)
478 pat_prov_ctxt <- mapM rnIfaceType (ifPatProvCtxt d)
479 pat_req_ctxt <- mapM rnIfaceType (ifPatReqCtxt d)
480 pat_args <- mapM rnIfaceType (ifPatArgs d)
481 pat_ty <- rnIfaceType (ifPatTy d)
482 return d { ifName = name
483 , ifPatMatcher = pat_matcher
484 , ifPatBuilder = pat_builder
485 , ifPatUnivBndrs = pat_univ_bndrs
486 , ifPatExBndrs = pat_ex_bndrs
487 , ifPatProvCtxt = pat_prov_ctxt
488 , ifPatReqCtxt = pat_req_ctxt
489 , ifPatArgs = pat_args
490 , ifPatTy = pat_ty
491 }
492
493 rnIfaceClassBody :: Rename IfaceClassBody
494 rnIfaceClassBody IfAbstractClass = return IfAbstractClass
495 rnIfaceClassBody d@IfConcreteClass{} = do
496 ctxt <- mapM rnIfaceType (ifClassCtxt d)
497 ats <- mapM rnIfaceAT (ifATs d)
498 sigs <- mapM rnIfaceClassOp (ifSigs d)
499 return d { ifClassCtxt = ctxt, ifATs = ats, ifSigs = sigs }
500
501 rnIfaceFamTyConFlav :: Rename IfaceFamTyConFlav
502 rnIfaceFamTyConFlav (IfaceClosedSynFamilyTyCon (Just (n, axs)))
503 = IfaceClosedSynFamilyTyCon . Just <$> ((,) <$> rnIfaceNeverExported n
504 <*> mapM rnIfaceAxBranch axs)
505 rnIfaceFamTyConFlav flav = pure flav
506
507 rnIfaceAT :: Rename IfaceAT
508 rnIfaceAT (IfaceAT decl mb_ty)
509 = IfaceAT <$> rnIfaceDecl decl <*> T.traverse rnIfaceType mb_ty
510
511 rnIfaceTyConParent :: Rename IfaceTyConParent
512 rnIfaceTyConParent (IfDataInstance n tc args)
513 = IfDataInstance <$> rnIfaceGlobal n
514 <*> rnIfaceTyCon tc
515 <*> rnIfaceTcArgs args
516 rnIfaceTyConParent IfNoParent = pure IfNoParent
517
518 rnIfaceConDecls :: Rename IfaceConDecls
519 rnIfaceConDecls (IfDataTyCon ds)
520 = IfDataTyCon <$> mapM rnIfaceConDecl ds
521 rnIfaceConDecls (IfNewTyCon d) = IfNewTyCon <$> rnIfaceConDecl d
522 rnIfaceConDecls IfAbstractTyCon = pure IfAbstractTyCon
523
524 rnIfaceConDecl :: Rename IfaceConDecl
525 rnIfaceConDecl d = do
526 con_name <- rnIfaceGlobal (ifConName d)
527 con_ex_tvs <- mapM rnIfaceTvBndr (ifConExTvs d)
528 con_user_tvbs <- mapM rnIfaceForAllBndr (ifConUserTvBinders d)
529 let rnIfConEqSpec (n,t) = (,) n <$> rnIfaceType t
530 con_eq_spec <- mapM rnIfConEqSpec (ifConEqSpec d)
531 con_ctxt <- mapM rnIfaceType (ifConCtxt d)
532 con_arg_tys <- mapM rnIfaceType (ifConArgTys d)
533 con_fields <- mapM rnFieldLabel (ifConFields d)
534 let rnIfaceBang (IfUnpackCo co) = IfUnpackCo <$> rnIfaceCo co
535 rnIfaceBang bang = pure bang
536 con_stricts <- mapM rnIfaceBang (ifConStricts d)
537 return d { ifConName = con_name
538 , ifConExTvs = con_ex_tvs
539 , ifConUserTvBinders = con_user_tvbs
540 , ifConEqSpec = con_eq_spec
541 , ifConCtxt = con_ctxt
542 , ifConArgTys = con_arg_tys
543 , ifConFields = con_fields
544 , ifConStricts = con_stricts
545 }
546
547 rnIfaceClassOp :: Rename IfaceClassOp
548 rnIfaceClassOp (IfaceClassOp n ty dm) =
549 IfaceClassOp <$> rnIfaceGlobal n
550 <*> rnIfaceType ty
551 <*> rnMaybeDefMethSpec dm
552
553 rnMaybeDefMethSpec :: Rename (Maybe (DefMethSpec IfaceType))
554 rnMaybeDefMethSpec (Just (GenericDM ty)) = Just . GenericDM <$> rnIfaceType ty
555 rnMaybeDefMethSpec mb = return mb
556
557 rnIfaceAxBranch :: Rename IfaceAxBranch
558 rnIfaceAxBranch d = do
559 ty_vars <- mapM rnIfaceTvBndr (ifaxbTyVars d)
560 lhs <- rnIfaceTcArgs (ifaxbLHS d)
561 rhs <- rnIfaceType (ifaxbRHS d)
562 return d { ifaxbTyVars = ty_vars
563 , ifaxbLHS = lhs
564 , ifaxbRHS = rhs }
565
566 rnIfaceIdInfo :: Rename IfaceIdInfo
567 rnIfaceIdInfo NoInfo = pure NoInfo
568 rnIfaceIdInfo (HasInfo is) = HasInfo <$> mapM rnIfaceInfoItem is
569
570 rnIfaceInfoItem :: Rename IfaceInfoItem
571 rnIfaceInfoItem (HsUnfold lb if_unf)
572 = HsUnfold lb <$> rnIfaceUnfolding if_unf
573 rnIfaceInfoItem i
574 = pure i
575
576 rnIfaceUnfolding :: Rename IfaceUnfolding
577 rnIfaceUnfolding (IfCoreUnfold stable if_expr)
578 = IfCoreUnfold stable <$> rnIfaceExpr if_expr
579 rnIfaceUnfolding (IfCompulsory if_expr)
580 = IfCompulsory <$> rnIfaceExpr if_expr
581 rnIfaceUnfolding (IfInlineRule arity unsat_ok boring_ok if_expr)
582 = IfInlineRule arity unsat_ok boring_ok <$> rnIfaceExpr if_expr
583 rnIfaceUnfolding (IfDFunUnfold bs ops)
584 = IfDFunUnfold <$> rnIfaceBndrs bs <*> mapM rnIfaceExpr ops
585
586 rnIfaceExpr :: Rename IfaceExpr
587 rnIfaceExpr (IfaceLcl name) = pure (IfaceLcl name)
588 rnIfaceExpr (IfaceExt gbl) = IfaceExt <$> rnIfaceGlobal gbl
589 rnIfaceExpr (IfaceType ty) = IfaceType <$> rnIfaceType ty
590 rnIfaceExpr (IfaceCo co) = IfaceCo <$> rnIfaceCo co
591 rnIfaceExpr (IfaceTuple sort args) = IfaceTuple sort <$> rnIfaceExprs args
592 rnIfaceExpr (IfaceLam lam_bndr expr)
593 = IfaceLam <$> rnIfaceLamBndr lam_bndr <*> rnIfaceExpr expr
594 rnIfaceExpr (IfaceApp fun arg)
595 = IfaceApp <$> rnIfaceExpr fun <*> rnIfaceExpr arg
596 rnIfaceExpr (IfaceCase scrut case_bndr alts)
597 = IfaceCase <$> rnIfaceExpr scrut
598 <*> pure case_bndr
599 <*> mapM rnIfaceAlt alts
600 rnIfaceExpr (IfaceECase scrut ty)
601 = IfaceECase <$> rnIfaceExpr scrut <*> rnIfaceType ty
602 rnIfaceExpr (IfaceLet (IfaceNonRec bndr rhs) body)
603 = IfaceLet <$> (IfaceNonRec <$> rnIfaceLetBndr bndr <*> rnIfaceExpr rhs)
604 <*> rnIfaceExpr body
605 rnIfaceExpr (IfaceLet (IfaceRec pairs) body)
606 = IfaceLet <$> (IfaceRec <$> mapM (\(bndr, rhs) ->
607 (,) <$> rnIfaceLetBndr bndr
608 <*> rnIfaceExpr rhs) pairs)
609 <*> rnIfaceExpr body
610 rnIfaceExpr (IfaceCast expr co)
611 = IfaceCast <$> rnIfaceExpr expr <*> rnIfaceCo co
612 rnIfaceExpr (IfaceLit lit) = pure (IfaceLit lit)
613 rnIfaceExpr (IfaceFCall cc ty) = IfaceFCall cc <$> rnIfaceType ty
614 rnIfaceExpr (IfaceTick tickish expr) = IfaceTick tickish <$> rnIfaceExpr expr
615
616 rnIfaceBndrs :: Rename [IfaceBndr]
617 rnIfaceBndrs = mapM rnIfaceBndr
618
619 rnIfaceBndr :: Rename IfaceBndr
620 rnIfaceBndr (IfaceIdBndr (fs, ty)) = IfaceIdBndr <$> ((,) fs <$> rnIfaceType ty)
621 rnIfaceBndr (IfaceTvBndr tv_bndr) = IfaceTvBndr <$> rnIfaceTvBndr tv_bndr
622
623 rnIfaceTvBndr :: Rename IfaceTvBndr
624 rnIfaceTvBndr (fs, kind) = (,) fs <$> rnIfaceType kind
625
626 rnIfaceTyConBinder :: Rename IfaceTyConBinder
627 rnIfaceTyConBinder (TvBndr tv vis) = TvBndr <$> rnIfaceTvBndr tv <*> pure vis
628
629 rnIfaceAlt :: Rename IfaceAlt
630 rnIfaceAlt (conalt, names, rhs)
631 = (,,) <$> rnIfaceConAlt conalt <*> pure names <*> rnIfaceExpr rhs
632
633 rnIfaceConAlt :: Rename IfaceConAlt
634 rnIfaceConAlt (IfaceDataAlt data_occ) = IfaceDataAlt <$> rnIfaceGlobal data_occ
635 rnIfaceConAlt alt = pure alt
636
637 rnIfaceLetBndr :: Rename IfaceLetBndr
638 rnIfaceLetBndr (IfLetBndr fs ty info jpi)
639 = IfLetBndr fs <$> rnIfaceType ty <*> rnIfaceIdInfo info <*> pure jpi
640
641 rnIfaceLamBndr :: Rename IfaceLamBndr
642 rnIfaceLamBndr (bndr, oneshot) = (,) <$> rnIfaceBndr bndr <*> pure oneshot
643
644 rnIfaceCo :: Rename IfaceCoercion
645 rnIfaceCo (IfaceReflCo role ty) = IfaceReflCo role <$> rnIfaceType ty
646 rnIfaceCo (IfaceFunCo role co1 co2)
647 = IfaceFunCo role <$> rnIfaceCo co1 <*> rnIfaceCo co2
648 rnIfaceCo (IfaceTyConAppCo role tc cos)
649 = IfaceTyConAppCo role <$> rnIfaceTyCon tc <*> mapM rnIfaceCo cos
650 rnIfaceCo (IfaceAppCo co1 co2)
651 = IfaceAppCo <$> rnIfaceCo co1 <*> rnIfaceCo co2
652 rnIfaceCo (IfaceForAllCo bndr co1 co2)
653 = IfaceForAllCo <$> rnIfaceTvBndr bndr <*> rnIfaceCo co1 <*> rnIfaceCo co2
654 rnIfaceCo (IfaceFreeCoVar c) = pure (IfaceFreeCoVar c)
655 rnIfaceCo (IfaceCoVarCo lcl) = IfaceCoVarCo <$> pure lcl
656 rnIfaceCo (IfaceHoleCo lcl) = IfaceHoleCo <$> pure lcl
657 rnIfaceCo (IfaceAxiomInstCo n i cs)
658 = IfaceAxiomInstCo <$> rnIfaceGlobal n <*> pure i <*> mapM rnIfaceCo cs
659 rnIfaceCo (IfaceUnivCo s r t1 t2)
660 = IfaceUnivCo s r <$> rnIfaceType t1 <*> rnIfaceType t2
661 rnIfaceCo (IfaceSymCo c)
662 = IfaceSymCo <$> rnIfaceCo c
663 rnIfaceCo (IfaceTransCo c1 c2)
664 = IfaceTransCo <$> rnIfaceCo c1 <*> rnIfaceCo c2
665 rnIfaceCo (IfaceInstCo c1 c2)
666 = IfaceInstCo <$> rnIfaceCo c1 <*> rnIfaceCo c2
667 rnIfaceCo (IfaceNthCo d c) = IfaceNthCo d <$> rnIfaceCo c
668 rnIfaceCo (IfaceLRCo lr c) = IfaceLRCo lr <$> rnIfaceCo c
669 rnIfaceCo (IfaceSubCo c) = IfaceSubCo <$> rnIfaceCo c
670 rnIfaceCo (IfaceAxiomRuleCo ax cos)
671 = IfaceAxiomRuleCo ax <$> mapM rnIfaceCo cos
672 rnIfaceCo (IfaceKindCo c) = IfaceKindCo <$> rnIfaceCo c
673 rnIfaceCo (IfaceCoherenceCo c1 c2) = IfaceCoherenceCo <$> rnIfaceCo c1 <*> rnIfaceCo c2
674
675 rnIfaceTyCon :: Rename IfaceTyCon
676 rnIfaceTyCon (IfaceTyCon n info)
677 = IfaceTyCon <$> rnIfaceGlobal n <*> pure info
678
679 rnIfaceExprs :: Rename [IfaceExpr]
680 rnIfaceExprs = mapM rnIfaceExpr
681
682 rnIfaceIdDetails :: Rename IfaceIdDetails
683 rnIfaceIdDetails (IfRecSelId (Left tc) b) = IfRecSelId <$> fmap Left (rnIfaceTyCon tc) <*> pure b
684 rnIfaceIdDetails (IfRecSelId (Right decl) b) = IfRecSelId <$> fmap Right (rnIfaceDecl decl) <*> pure b
685 rnIfaceIdDetails details = pure details
686
687 rnIfaceType :: Rename IfaceType
688 rnIfaceType (IfaceFreeTyVar n) = pure (IfaceFreeTyVar n)
689 rnIfaceType (IfaceTyVar n) = pure (IfaceTyVar n)
690 rnIfaceType (IfaceAppTy t1 t2)
691 = IfaceAppTy <$> rnIfaceType t1 <*> rnIfaceType t2
692 rnIfaceType (IfaceLitTy l) = return (IfaceLitTy l)
693 rnIfaceType (IfaceFunTy t1 t2)
694 = IfaceFunTy <$> rnIfaceType t1 <*> rnIfaceType t2
695 rnIfaceType (IfaceDFunTy t1 t2)
696 = IfaceDFunTy <$> rnIfaceType t1 <*> rnIfaceType t2
697 rnIfaceType (IfaceTupleTy s i tks)
698 = IfaceTupleTy s i <$> rnIfaceTcArgs tks
699 rnIfaceType (IfaceTyConApp tc tks)
700 = IfaceTyConApp <$> rnIfaceTyCon tc <*> rnIfaceTcArgs tks
701 rnIfaceType (IfaceForAllTy tv t)
702 = IfaceForAllTy <$> rnIfaceForAllBndr tv <*> rnIfaceType t
703 rnIfaceType (IfaceCoercionTy co)
704 = IfaceCoercionTy <$> rnIfaceCo co
705 rnIfaceType (IfaceCastTy ty co)
706 = IfaceCastTy <$> rnIfaceType ty <*> rnIfaceCo co
707
708 rnIfaceForAllBndr :: Rename IfaceForAllBndr
709 rnIfaceForAllBndr (TvBndr tv vis) = TvBndr <$> rnIfaceTvBndr tv <*> pure vis
710
711 rnIfaceTcArgs :: Rename IfaceTcArgs
712 rnIfaceTcArgs (ITC_Invis t ts) = ITC_Invis <$> rnIfaceType t <*> rnIfaceTcArgs ts
713 rnIfaceTcArgs (ITC_Vis t ts) = ITC_Vis <$> rnIfaceType t <*> rnIfaceTcArgs ts
714 rnIfaceTcArgs ITC_Nil = pure ITC_Nil