2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 %************************************************************************
6 \section[HsCore]{Core-syntax unfoldings in Haskell interface files}
8 %************************************************************************
10 We could either use this, or parameterise @GenCoreExpr@ on @Types@ and
11 @TyVars@ as well. Currently trying the former... MEGA SIGH.
15 module IfaceType, -- Re-export all this
17 IfaceDecl(..), IfaceClassOp(..), IfaceConDecl(..), IfaceConDecls(..),
18 IfaceExpr(..), IfaceAlt, IfaceNote(..),
19 IfaceBinding(..), IfaceConAlt(..), IfaceIdInfo(..),
20 IfaceInfoItem(..), IfaceRule(..), IfaceInst(..),
25 -- Converting things to IfaceSyn
26 tyThingToIfaceDecl, dfunToIfaceInst, coreRuleToIfaceRule,
29 IfaceEq(..), (&&&), bool, eqListBy, eqMaybeBy,
30 eqIfDecl, eqIfInst, eqIfRule,
33 pprIfaceExpr, pprIfaceDecl
36 #include "HsVersions.h"
41 import FunDeps ( pprFundeps )
42 import NewDemand ( StrictSig, pprIfaceStrictSig )
43 import TcType ( deNoteType, mkSigmaTy, tcSplitDFunTy, mkClassPred )
44 import Type ( TyThing(..), mkForAllTys, mkFunTys, splitForAllTys, funResultTy,
45 mkTyVarTys, mkTyConApp, mkTyVarTys, mkPredTy, tidyTopType )
46 import InstEnv ( DFunId )
47 import Id ( Id, idName, idType, idInfo, idArity, isDataConWorkId_maybe, isFCallId_maybe )
48 import NewDemand ( isTopSig )
49 import IdInfo ( IdInfo, CafInfo(..), WorkerInfo(..),
50 arityInfo, cafInfo, newStrictnessInfo,
51 workerInfo, unfoldingInfo, inlinePragInfo )
52 import TyCon ( TyCon, ArgVrcs, AlgTyConRhs(..), isRecursiveTyCon, isForeignTyCon,
53 isSynTyCon, isNewTyCon, isAlgTyCon, isPrimTyCon, isFunTyCon,
54 isTupleTyCon, tupleTyConBoxity,
55 tyConHasGenerics, tyConArgVrcs, tyConTheta, getSynTyConDefn,
56 tyConArity, tyConTyVars, algTyConRhs, tyConExtName )
57 import DataCon ( dataConName, dataConSig, dataConFieldLabels, dataConStrictMarks,
59 import Class ( FunDep, DefMeth, classExtraBigSig, classTyCon )
60 import OccName ( OccName, OccEnv, lookupOccEnv, emptyOccEnv,
61 lookupOccEnv, extendOccEnv, emptyOccEnv,
62 OccSet, unionOccSets, unitOccSet )
63 import Name ( Name, NamedThing(..), getOccName, nameOccName, nameModuleName, isExternalName )
64 import NameSet ( NameSet, elemNameSet )
65 import Module ( ModuleName )
66 import CostCentre ( CostCentre, pprCostCentreCore )
67 import Literal ( Literal )
68 import ForeignCall ( ForeignCall )
69 import TysPrim ( alphaTyVars )
70 import BasicTypes ( Arity, Activation(..), StrictnessMark,
71 RecFlag(..), boolToRecFlag, Boxity(..),
75 import Maybes ( catMaybes )
76 import Util ( lengthIs )
79 infix 4 `eqIfExt`, `eqIfIdInfo`, `eqIfType`
83 %************************************************************************
85 Data type declarations
87 %************************************************************************
91 = IfaceId { ifName :: OccName,
93 ifIdInfo :: IfaceIdInfo }
95 | IfaceData { ifCtxt :: IfaceContext, -- Context
96 ifName :: OccName, -- Type constructor
97 ifTyVars :: [IfaceTvBndr], -- Type variables
98 ifCons :: IfaceConDecls, -- Includes new/data info
99 ifRec :: RecFlag, -- Recursive or not?
101 ifGeneric :: Bool -- True <=> generic converter functions available
102 } -- We need this for imported data decls, since the
103 -- imported modules may have been compiled with
104 -- different flags to the current compilation unit
106 | IfaceSyn { ifName :: OccName, -- Type constructor
107 ifTyVars :: [IfaceTvBndr], -- Type variables
109 ifSynRhs :: IfaceType -- synonym expansion
112 | IfaceClass { ifCtxt :: IfaceContext, -- Context...
113 ifName :: OccName, -- Name of the class
114 ifTyVars :: [IfaceTvBndr], -- Type variables
115 ifFDs :: [FunDep OccName], -- Functional dependencies
116 ifSigs :: [IfaceClassOp], -- Method signatures
117 ifRec :: RecFlag, -- Is newtype/datatype associated with the class recursive?
118 ifVrcs :: ArgVrcs -- ... and what are its argument variances ...
121 | IfaceForeign { ifName :: OccName, -- Needs expanding when we move beyond .NET
122 ifExtName :: Maybe FastString }
124 data IfaceClassOp = IfaceClassOp OccName DefMeth IfaceType
125 -- Nothing => no default method
126 -- Just False => ordinary polymorphic default method
127 -- Just True => generic default method
130 = IfAbstractTyCon -- No info
131 | IfDataTyCon [IfaceConDecl] -- data type decls
132 | IfNewTyCon IfaceConDecl -- newtype decls
134 visibleIfConDecls :: IfaceConDecls -> [IfaceConDecl]
135 visibleIfConDecls IfAbstractTyCon = []
136 visibleIfConDecls (IfDataTyCon cs) = cs
137 visibleIfConDecls (IfNewTyCon c) = [c]
140 = IfaceConDecl OccName -- Constructor name
141 [IfaceTvBndr] -- Existental tyvars
142 IfaceContext -- Existential context
143 [IfaceType] -- Arg types
144 [StrictnessMark] -- Empty (meaning all lazy), or 1-1 corresp with arg types
145 [OccName] -- ...ditto... (field labels)
147 data IfaceInst = IfaceInst { ifInstHead :: IfaceType, -- Just the instance head type, quantified
148 -- so that it'll compare alpha-wise
149 ifDFun :: OccName } -- And the dfun
150 -- There's always a separate IfaceDecl for the DFun, which gives
151 -- its IdInfo with its full type and version number.
152 -- The instance declarations taken together have a version number,
153 -- and we don't want that to wobble gratuitously
154 -- If this instance decl is *used*, we'll record a usage on the dfun;
155 -- and if the head does not change it won't be used if it wasn't before
159 ifRuleName :: RuleName,
160 ifActivation :: Activation,
161 ifRuleBndrs :: [IfaceBndr], -- Tyvars and term vars
162 ifRuleHead :: IfaceExtName, -- Head of lhs
163 ifRuleArgs :: [IfaceExpr], -- Args of LHS
164 ifRuleRhs :: IfaceExpr
166 | IfaceBuiltinRule IfaceExtName CoreRule -- So that built-in rules can
167 -- wait in the RulePol
170 = NoInfo -- When writing interface file without -O
171 | HasInfo [IfaceInfoItem] -- Has info, and here it is
173 -- Here's a tricky case:
174 -- * Compile with -O module A, and B which imports A.f
175 -- * Change function f in A, and recompile without -O
176 -- * When we read in old A.hi we read in its IdInfo (as a thunk)
177 -- (In earlier GHCs we used to drop IdInfo immediately on reading,
178 -- but we do not do that now. Instead it's discarded when the
179 -- ModIface is read into the various decl pools.)
180 -- * The version comparsion sees that new (=NoInfo) differs from old (=HasInfo *)
181 -- and so gives a new version.
185 | HsStrictness StrictSig
186 | HsUnfold Activation IfaceExpr
188 | HsWorker OccName Arity -- Worker, if any see IdInfo.WorkerInfo
189 -- for why we want arity here.
190 -- NB: Specialisations and rules come in separately and are
191 -- only later attached to the Id. Partial reason: some are orphans.
193 --------------------------------
196 | IfaceExt IfaceExtName
197 | IfaceType IfaceType
198 | IfaceTuple Boxity [IfaceExpr] -- Saturated; type arguments omitted
199 | IfaceLam IfaceBndr IfaceExpr
200 | IfaceApp IfaceExpr IfaceExpr
201 | IfaceCase IfaceExpr OccName [IfaceAlt]
202 | IfaceLet IfaceBinding IfaceExpr
203 | IfaceNote IfaceNote IfaceExpr
205 | IfaceFCall ForeignCall IfaceType
207 data IfaceNote = IfaceSCC CostCentre
208 | IfaceCoerce IfaceType
211 | IfaceCoreNote String
213 type IfaceAlt = (IfaceConAlt, [OccName], IfaceExpr)
214 -- Note: OccName, not IfaceBndr (and same with the case binder)
215 -- We reconstruct the kind/type of the thing from the context
216 -- thus saving bulk in interface files
218 data IfaceConAlt = IfaceDefault
219 | IfaceDataAlt OccName
220 | IfaceTupleAlt Boxity
221 | IfaceLitAlt Literal
224 = IfaceNonRec IfaceIdBndr IfaceExpr
225 | IfaceRec [(IfaceIdBndr, IfaceExpr)]
229 %************************************************************************
231 \subsection[HsCore-print]{Printing Core unfoldings}
233 %************************************************************************
235 ----------------------------- Printing IfaceDecl ------------------------------------
238 instance Outputable IfaceDecl where
241 pprIfaceDecl (IfaceId {ifName = var, ifType = ty, ifIdInfo = info})
242 = sep [ ppr var <+> dcolon <+> ppr ty,
245 pprIfaceDecl (IfaceForeign {ifName = tycon})
246 = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
248 pprIfaceDecl (IfaceSyn {ifName = tycon, ifTyVars = tyvars, ifSynRhs = mono_ty, ifVrcs = vrcs})
249 = hang (ptext SLIT("type") <+> pp_decl_head [] tycon tyvars)
250 4 (vcat [equals <+> ppr mono_ty,
253 pprIfaceDecl (IfaceData {ifCtxt = context, ifName = tycon, ifGeneric = gen,
254 ifTyVars = tyvars, ifCons = condecls, ifRec = isrec, ifVrcs = vrcs})
255 = hang (pp_nd <+> pp_decl_head context tycon tyvars)
256 4 (vcat [pprVrcs vrcs, pprRec isrec, pprGen gen, pp_condecls condecls])
258 pp_nd = case condecls of
259 IfAbstractTyCon -> ptext SLIT("data")
260 IfDataTyCon _ -> ptext SLIT("data")
261 IfNewTyCon _ -> ptext SLIT("newtype")
263 pprIfaceDecl (IfaceClass {ifCtxt = context, ifName = clas, ifTyVars = tyvars,
264 ifFDs = fds, ifSigs = sigs, ifVrcs = vrcs, ifRec = isrec})
265 = hang (ptext SLIT("class") <+> pp_decl_head context clas tyvars <+> pprFundeps fds)
266 4 (vcat [pprVrcs vrcs,
270 pprVrcs vrcs = ptext SLIT("Variances") <+> ppr vrcs
271 pprRec isrec = ptext SLIT("RecFlag") <+> ppr isrec
272 pprGen True = ptext SLIT("Generics: yes")
273 pprGen False = ptext SLIT("Generics: no")
275 instance Outputable IfaceClassOp where
276 ppr (IfaceClassOp n dm ty) = ppr n <+> ppr dm <+> dcolon <+> ppr ty
278 pp_decl_head :: IfaceContext -> OccName -> [IfaceTvBndr] -> SDoc
279 pp_decl_head context thing tyvars
280 = hsep [pprIfaceContext context, ppr thing, pprIfaceTvBndrs tyvars]
282 pp_condecls IfAbstractTyCon = ptext SLIT("{- abstract -}")
283 pp_condecls (IfDataTyCon cs) = equals <+> sep (punctuate (ptext SLIT(" |")) (map ppr cs))
284 pp_condecls (IfNewTyCon c) = equals <+> ppr c
286 instance Outputable IfaceConDecl where
287 ppr (IfaceConDecl name ex_tvs ex_ctxt arg_tys strs fields)
288 = pprIfaceForAllPart ex_tvs ex_ctxt $
289 sep [ppr name <+> sep (map pprParendIfaceType arg_tys),
290 if null strs then empty
291 else nest 4 (ptext SLIT("Stricts:") <+> hsep (map ppr strs)),
292 if null fields then empty
293 else nest 4 (ptext SLIT("Fields:") <+> hsep (map ppr fields))]
295 instance Outputable IfaceRule where
296 ppr (IfaceRule name act bndrs fn args rhs)
297 = sep [hsep [doubleQuotes (ftext name), ppr act,
298 ptext SLIT("forall") <+> pprIfaceBndrs bndrs],
299 nest 2 (sep [ppr fn <+> sep (map (pprIfaceExpr parens) args),
300 ptext SLIT("=") <+> ppr rhs])
302 ppr (IfaceBuiltinRule name rule)
303 = ptext SLIT("Built-in rule for") <+> ppr name
305 instance Outputable IfaceInst where
306 ppr (IfaceInst {ifDFun = dfun_id, ifInstHead = ty})
307 = hang (ptext SLIT("instance") <+> ppr ty)
308 2 (equals <+> ppr dfun_id)
312 ----------------------------- Printing IfaceExpr ------------------------------------
315 instance Outputable IfaceExpr where
316 ppr e = pprIfaceExpr noParens e
318 pprIfaceExpr :: (SDoc -> SDoc) -> IfaceExpr -> SDoc
319 -- The function adds parens in context that need
320 -- an atomic value (e.g. function args)
322 pprIfaceExpr add_par (IfaceLcl v) = ppr v
323 pprIfaceExpr add_par (IfaceExt v) = ppr v
324 pprIfaceExpr add_par (IfaceLit l) = ppr l
325 pprIfaceExpr add_par (IfaceFCall cc ty) = braces (ppr cc <+> ppr ty)
326 pprIfaceExpr add_par (IfaceType ty) = char '@' <+> pprParendIfaceType ty
328 pprIfaceExpr add_par app@(IfaceApp _ _) = add_par (pprIfaceApp app [])
329 pprIfaceExpr add_par (IfaceTuple c as) = tupleParens c (interpp'SP as)
331 pprIfaceExpr add_par e@(IfaceLam _ _)
332 = add_par (sep [char '\\' <+> sep (map ppr bndrs) <+> arrow,
333 pprIfaceExpr noParens body])
335 (bndrs,body) = collect [] e
336 collect bs (IfaceLam b e) = collect (b:bs) e
337 collect bs e = (reverse bs, e)
339 pprIfaceExpr add_par (IfaceCase scrut bndr [(con, bs, rhs)])
340 = add_par (sep [ptext SLIT("case") <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
341 <+> ppr bndr <+> char '{' <+> ppr_con_bs con bs <+> arrow,
342 pprIfaceExpr noParens rhs <+> char '}'])
344 pprIfaceExpr add_par (IfaceCase scrut bndr alts)
345 = add_par (sep [ptext SLIT("case") <+> pprIfaceExpr noParens scrut <+> ptext SLIT("of")
346 <+> ppr bndr <+> char '{',
347 nest 2 (sep (map ppr_alt alts)) <+> char '}'])
349 pprIfaceExpr add_par (IfaceLet (IfaceNonRec b rhs) body)
350 = add_par (sep [ptext SLIT("let {"),
351 nest 2 (ppr_bind (b, rhs)),
353 pprIfaceExpr noParens body])
355 pprIfaceExpr add_par (IfaceLet (IfaceRec pairs) body)
356 = add_par (sep [ptext SLIT("letrec {"),
357 nest 2 (sep (map ppr_bind pairs)),
359 pprIfaceExpr noParens body])
361 pprIfaceExpr add_par (IfaceNote note body) = add_par (ppr note <+> pprIfaceExpr parens body)
363 ppr_alt (con, bs, rhs) = sep [ppr_con_bs con bs,
364 arrow <+> pprIfaceExpr noParens rhs]
366 ppr_con_bs (IfaceTupleAlt tup_con) bs = tupleParens tup_con (interpp'SP bs)
367 ppr_con_bs con bs = ppr con <+> hsep (map ppr bs)
369 ppr_bind ((b,ty),rhs) = sep [ppr b <+> dcolon <+> ppr ty,
370 equals <+> pprIfaceExpr noParens rhs]
373 pprIfaceApp (IfaceApp fun arg) args = pprIfaceApp fun (nest 2 (pprIfaceExpr parens arg) : args)
374 pprIfaceApp fun args = sep (pprIfaceExpr parens fun : args)
377 instance Outputable IfaceNote where
378 ppr (IfaceSCC cc) = pprCostCentreCore cc
379 ppr (IfaceCoerce ty) = ptext SLIT("__coerce") <+> pprParendIfaceType ty
380 ppr IfaceInlineCall = ptext SLIT("__inline_call")
381 ppr IfaceInlineMe = ptext SLIT("__inline_me")
382 ppr (IfaceCoreNote s) = ptext SLIT("__core_note") <+> pprHsString (mkFastString s)
384 instance Outputable IfaceConAlt where
385 ppr IfaceDefault = text "DEFAULT"
386 ppr (IfaceLitAlt l) = ppr l
387 ppr (IfaceDataAlt d) = ppr d
388 -- IfaceTupleAlt is handled by the case-alternative printer
391 instance Outputable IfaceIdInfo where
393 ppr (HasInfo is) = ptext SLIT("{-") <+> fsep (map ppr_hs_info is) <+> ptext SLIT("-}")
395 ppr_hs_info (HsUnfold prag unf) = sep [ptext SLIT("Unfolding: ") <> ppr prag,
396 parens (pprIfaceExpr noParens unf)]
397 ppr_hs_info (HsArity arity) = ptext SLIT("Arity:") <+> int arity
398 ppr_hs_info (HsStrictness str) = ptext SLIT("Strictness:") <+> pprIfaceStrictSig str
399 ppr_hs_info HsNoCafRefs = ptext SLIT("HasNoCafRefs")
400 ppr_hs_info (HsWorker w a) = ptext SLIT("Worker:") <+> ppr w <+> int a
404 %************************************************************************
406 Converting things to their Iface equivalents
408 %************************************************************************
412 tyThingToIfaceDecl :: Bool
413 -> NameSet -- Tycons and classes to export abstractly
414 -> (Name -> IfaceExtName) -> TyThing -> IfaceDecl
415 tyThingToIfaceDecl discard_id_info _ ext (AnId id)
416 = IfaceId { ifName = getOccName id,
417 ifType = toIfaceType ext (idType id),
420 info | discard_id_info = NoInfo
421 | otherwise = HasInfo (toIfaceIdInfo ext (idInfo id))
423 tyThingToIfaceDecl _ _ ext (AClass clas)
424 = IfaceClass { ifCtxt = toIfaceContext ext sc_theta,
425 ifName = getOccName clas,
426 ifTyVars = toIfaceTvBndrs clas_tyvars,
427 ifFDs = map toIfaceFD clas_fds,
428 ifSigs = map toIfaceClassOp op_stuff,
429 ifRec = boolToRecFlag (isRecursiveTyCon tycon),
430 ifVrcs = tyConArgVrcs tycon }
432 (clas_tyvars, clas_fds, sc_theta, _, op_stuff) = classExtraBigSig clas
433 tycon = classTyCon clas
435 toIfaceClassOp (sel_id, def_meth)
436 = ASSERT(sel_tyvars == clas_tyvars)
437 IfaceClassOp (getOccName sel_id) def_meth (toIfaceType ext op_ty)
439 -- Be careful when splitting the type, because of things
440 -- like class Foo a where
441 -- op :: (?x :: String) => a -> a
442 -- and class Baz a where
443 -- op :: (Ord a) => a -> a
444 (sel_tyvars, rho_ty) = splitForAllTys (idType sel_id)
445 op_ty = funResultTy rho_ty
447 toIfaceFD (tvs1, tvs2) = (map getOccName tvs1, map getOccName tvs2)
449 tyThingToIfaceDecl _ abstract_tcs ext (ATyCon tycon)
451 = IfaceSyn { ifName = getOccName tycon,
452 ifTyVars = toIfaceTvBndrs tyvars,
453 ifVrcs = tyConArgVrcs tycon,
454 ifSynRhs = toIfaceType ext syn_ty }
457 = IfaceData { ifCtxt = toIfaceContext ext (tyConTheta tycon),
458 ifName = getOccName tycon,
459 ifTyVars = toIfaceTvBndrs tyvars,
460 ifCons = ifaceConDecls (algTyConRhs tycon),
461 ifRec = boolToRecFlag (isRecursiveTyCon tycon),
462 ifVrcs = tyConArgVrcs tycon,
463 ifGeneric = tyConHasGenerics tycon }
465 | isForeignTyCon tycon
466 = IfaceForeign { ifName = getOccName tycon,
467 ifExtName = tyConExtName tycon }
469 | isPrimTyCon tycon || isFunTyCon tycon
470 -- Needed in GHCi for ':info Int#', for example
471 = IfaceData { ifCtxt = [],
472 ifName = getOccName tycon,
473 ifTyVars = toIfaceTvBndrs (take (tyConArity tycon) alphaTyVars),
474 ifCons = IfAbstractTyCon,
476 ifRec = NonRecursive,
477 ifVrcs = tyConArgVrcs tycon }
479 | otherwise = pprPanic "toIfaceDecl" (ppr tycon)
481 tyvars = tyConTyVars tycon
482 (_, syn_ty) = getSynTyConDefn tycon
483 abstract = getName tycon `elemNameSet` abstract_tcs
485 ifaceConDecls _ | abstract = IfAbstractTyCon
486 ifaceConDecls (NewTyCon con _ _) = IfNewTyCon (ifaceConDecl con)
487 ifaceConDecls (DataTyCon cons _) = IfDataTyCon (map ifaceConDecl cons)
488 ifaceConDecls AbstractTyCon = pprPanic "ifaceConDecls" (ppr tycon)
489 -- We're exporting this thing, so it's locally defined and should not be abstract
491 ifaceConDecl data_con
492 = IfaceConDecl (getOccName (dataConName data_con))
493 (toIfaceTvBndrs ex_tyvars)
494 (toIfaceContext ext ex_theta)
495 (map (toIfaceType ext) arg_tys)
497 (map getOccName field_labels)
499 (_, _, ex_tyvars, ex_theta, arg_tys, _) = dataConSig data_con
500 field_labels = dataConFieldLabels data_con
501 strict_marks = dataConStrictMarks data_con
503 -- This case only happens in the call to ifaceThing in InteractiveUI
504 -- Otherwise DataCons are filtered out in ifaceThing_acc
505 tyThingToIfaceDecl _ _ ext (ADataCon dc)
506 = IfaceId { ifName = getOccName dc,
507 ifType = toIfaceType ext full_ty,
510 (tvs, stupid_theta, ex_tvs, ex_theta, arg_tys, tycon) = dataConSig dc
512 -- The "stupid context" isn't part of the wrapper-Id type
513 -- (for better or worse -- see note in DataCon.lhs), so we
514 -- have to make it up here
515 full_ty = mkSigmaTy (tvs ++ ex_tvs) (stupid_theta ++ ex_theta)
516 (mkFunTys arg_tys (mkTyConApp tycon (mkTyVarTys tvs)))
518 --------------------------
519 dfunToIfaceInst :: ModuleName -> DFunId -> IfaceInst
520 dfunToIfaceInst mod dfun_id
521 = IfaceInst { ifDFun = getOccName dfun_id,
522 ifInstHead = toIfaceType (mkLhsNameFn mod) tidy_ty }
524 (tvs, _, cls, tys) = tcSplitDFunTy (idType dfun_id)
525 head_ty = mkForAllTys tvs (mkPredTy (mkClassPred cls tys))
526 -- No need to record the instance context;
527 -- it's in the dfun anyway
529 tidy_ty = tidyTopType (deNoteType head_ty)
530 -- The deNoteType is very important. It removes all type
531 -- synonyms from the instance type in interface files.
532 -- That in turn makes sure that when reading in instance decls
533 -- from interface files that the 'gating' mechanism works properly.
534 -- Otherwise you could have
535 -- type Tibble = T Int
536 -- instance Foo Tibble where ...
537 -- and this instance decl wouldn't get imported into a module
538 -- that mentioned T but not Tibble.
541 --------------------------
542 toIfaceIdInfo :: (Name -> IfaceExtName) -> IdInfo -> [IfaceInfoItem]
543 toIfaceIdInfo ext id_info
544 = catMaybes [arity_hsinfo, caf_hsinfo, strict_hsinfo,
545 wrkr_hsinfo, unfold_hsinfo]
547 ------------ Arity --------------
548 arity_info = arityInfo id_info
549 arity_hsinfo | arity_info == 0 = Nothing
550 | otherwise = Just (HsArity arity_info)
552 ------------ Caf Info --------------
553 caf_info = cafInfo id_info
554 caf_hsinfo = case caf_info of
555 NoCafRefs -> Just HsNoCafRefs
558 ------------ Strictness --------------
559 -- No point in explicitly exporting TopSig
560 strict_hsinfo = case newStrictnessInfo id_info of
561 Just sig | not (isTopSig sig) -> Just (HsStrictness sig)
564 ------------ Worker --------------
565 work_info = workerInfo id_info
566 has_worker = case work_info of { HasWorker _ _ -> True; other -> False }
567 wrkr_hsinfo = case work_info of
568 HasWorker work_id wrap_arity ->
569 Just (HsWorker (getOccName work_id) wrap_arity)
572 ------------ Unfolding --------------
573 -- The unfolding is redundant if there is a worker
574 unfold_info = unfoldingInfo id_info
575 inline_prag = inlinePragInfo id_info
576 rhs = unfoldingTemplate unfold_info
577 unfold_hsinfo | neverUnfold unfold_info
578 || has_worker = Nothing
579 | otherwise = Just (HsUnfold inline_prag (toIfaceExpr ext rhs))
581 --------------------------
582 coreRuleToIfaceRule :: ModuleName -> (Name -> IfaceExtName) -> IdCoreRule -> IfaceRule
583 coreRuleToIfaceRule mod ext (id, BuiltinRule _ _)
584 = pprTrace "toHsRule: builtin" (ppr id) (bogusIfaceRule (mkIfaceExtName (getName id)))
586 coreRuleToIfaceRule mod ext (id, Rule name act bndrs args rhs)
587 = IfaceRule { ifRuleName = name, ifActivation = act,
588 ifRuleBndrs = map (toIfaceBndr ext) bndrs,
589 ifRuleHead = ext (getName id),
590 ifRuleArgs = map (toIfaceExpr (mkLhsNameFn mod)) args,
591 -- Use LHS name-fn for the args
592 ifRuleRhs = toIfaceExpr ext rhs }
594 bogusIfaceRule :: IfaceExtName -> IfaceRule
595 bogusIfaceRule id_name
596 = IfaceRule FSLIT("bogus") NeverActive [] id_name [] (IfaceExt id_name)
598 ---------------------
599 toIfaceExpr :: (Name -> IfaceExtName) -> CoreExpr -> IfaceExpr
600 toIfaceExpr ext (Var v) = toIfaceVar ext v
601 toIfaceExpr ext (Lit l) = IfaceLit l
602 toIfaceExpr ext (Type ty) = IfaceType (toIfaceType ext ty)
603 toIfaceExpr ext (Lam x b) = IfaceLam (toIfaceBndr ext x) (toIfaceExpr ext b)
604 toIfaceExpr ext (App f a) = toIfaceApp ext f [a]
605 toIfaceExpr ext (Case s x as) = IfaceCase (toIfaceExpr ext s) (getOccName x) (map (toIfaceAlt ext) as)
606 toIfaceExpr ext (Let b e) = IfaceLet (toIfaceBind ext b) (toIfaceExpr ext e)
607 toIfaceExpr ext (Note n e) = IfaceNote (toIfaceNote ext n) (toIfaceExpr ext e)
609 ---------------------
610 toIfaceNote ext (SCC cc) = IfaceSCC cc
611 toIfaceNote ext (Coerce t1 _) = IfaceCoerce (toIfaceType ext t1)
612 toIfaceNote ext InlineCall = IfaceInlineCall
613 toIfaceNote ext InlineMe = IfaceInlineMe
614 toIfaceNote ext (CoreNote s) = IfaceCoreNote s
616 ---------------------
617 toIfaceBind ext (NonRec b r) = IfaceNonRec (toIfaceIdBndr ext b) (toIfaceExpr ext r)
618 toIfaceBind ext (Rec prs) = IfaceRec [(toIfaceIdBndr ext b, toIfaceExpr ext r) | (b,r) <- prs]
620 ---------------------
621 toIfaceAlt ext (c,bs,r) = (toIfaceCon c, map getOccName bs, toIfaceExpr ext r)
623 ---------------------
624 toIfaceCon (DataAlt dc) | isTupleTyCon tc = IfaceTupleAlt (tupleTyConBoxity tc)
625 | otherwise = IfaceDataAlt (getOccName dc)
629 toIfaceCon (LitAlt l) = IfaceLitAlt l
630 toIfaceCon DEFAULT = IfaceDefault
632 ---------------------
633 toIfaceApp ext (App f a) as = toIfaceApp ext f (a:as)
634 toIfaceApp ext (Var v) as
635 = case isDataConWorkId_maybe v of
636 -- We convert the *worker* for tuples into IfaceTuples
637 Just dc | isTupleTyCon tc && saturated
638 -> IfaceTuple (tupleTyConBoxity tc) tup_args
640 val_args = dropWhile isTypeArg as
641 saturated = val_args `lengthIs` idArity v
642 tup_args = map (toIfaceExpr ext) val_args
645 other -> mkIfaceApps ext (toIfaceVar ext v) as
647 toIfaceApp ext e as = mkIfaceApps ext (toIfaceExpr ext e) as
649 mkIfaceApps ext f as = foldl (\f a -> IfaceApp f (toIfaceExpr ext a)) f as
651 ---------------------
652 toIfaceVar :: (Name -> IfaceExtName) -> Id -> IfaceExpr
654 | Just fcall <- isFCallId_maybe v = IfaceFCall fcall (toIfaceType ext (idType v))
655 -- Foreign calls have special syntax
656 | isExternalName name = IfaceExt (ext name)
657 | otherwise = IfaceLcl (nameOccName name)
661 ---------------------
662 -- mkLhsNameFn ignores versioning info altogether
663 -- Used for the LHS of instance decls and rules, where we
664 -- there's no point in recording version info
665 mkLhsNameFn :: ModuleName -> Name -> IfaceExtName
666 mkLhsNameFn this_mod name
667 | mod == this_mod = LocalTop occ
668 | otherwise = ExtPkg mod occ
670 mod = nameModuleName name
671 occ = nameOccName name
675 %************************************************************************
677 Equality, for interface file version generaion only
679 %************************************************************************
681 Equality over IfaceSyn returns an IfaceEq, not a Bool. The new constructor is
682 EqBut, which gives the set of *locally-defined* things whose version must be equal
683 for the whole thing to be equal. So the key function is eqIfExt, which compares
686 Of course, equality is also done modulo alpha conversion.
690 = Equal -- Definitely exactly the same
691 | NotEqual -- Definitely different
692 | EqBut OccSet -- The same provided these local things have not changed
694 bool :: Bool -> IfaceEq
696 bool False = NotEqual
698 zapEq :: IfaceEq -> IfaceEq -- Used to forget EqBut information
699 zapEq (EqBut _) = Equal
702 (&&&) :: IfaceEq -> IfaceEq -> IfaceEq
704 NotEqual &&& x = NotEqual
705 EqBut occs &&& Equal = EqBut occs
706 EqBut occs &&& NotEqual = NotEqual
707 EqBut occs1 &&& EqBut occs2 = EqBut (occs1 `unionOccSets` occs2)
709 ---------------------
710 eqIfExt :: IfaceExtName -> IfaceExtName -> IfaceEq
711 -- This function is the core of the EqBut stuff
712 eqIfExt (ExtPkg mod1 occ1) (ExtPkg mod2 occ2) = bool (mod1==mod2 && occ1==occ2)
713 eqIfExt (HomePkg mod1 occ1 v1) (HomePkg mod2 occ2 v2) = bool (mod1==mod2 && occ1==occ2 && v1==v2)
714 eqIfExt (LocalTop occ1) (LocalTop occ2) | occ1 == occ2 = EqBut (unitOccSet occ1)
715 eqIfExt (LocalTopSub occ1 p1) (LocalTop occ2) | occ1 == occ2 = EqBut (unitOccSet p1)
716 eqIfExt (LocalTopSub occ1 p1) (LocalTopSub occ2 _) | occ1 == occ2 = EqBut (unitOccSet p1)
717 eqIfExt n1 n2 = NotEqual
722 ---------------------
723 eqIfDecl :: IfaceDecl -> IfaceDecl -> IfaceEq
724 eqIfDecl (IfaceId s1 t1 i1) (IfaceId s2 t2 i2)
725 = bool (s1 == s2) &&& (t1 `eqIfType` t2) &&& (i1 `eqIfIdInfo` i2)
727 eqIfDecl d1@(IfaceForeign {}) d2@(IfaceForeign {})
728 = bool (ifName d1 == ifName d2 && ifExtName d1 == ifExtName d2)
730 eqIfDecl d1@(IfaceData {}) d2@(IfaceData {})
731 = bool (ifName d1 == ifName d2 &&
732 ifRec d1 == ifRec d2 &&
733 ifVrcs d1 == ifVrcs d2 &&
734 ifGeneric d1 == ifGeneric d2) &&&
735 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
736 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
737 eq_hsCD env (ifCons d1) (ifCons d2)
740 eqIfDecl d1@(IfaceSyn {}) d2@(IfaceSyn {})
741 = bool (ifName d1 == ifName d2) &&&
742 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
743 eq_ifType env (ifSynRhs d1) (ifSynRhs d2)
746 eqIfDecl d1@(IfaceClass {}) d2@(IfaceClass {})
747 = bool (ifName d1 == ifName d2 &&
748 ifRec d1 == ifRec d2 &&
749 ifVrcs d1 == ifVrcs d2) &&&
750 eqWith (ifTyVars d1) (ifTyVars d2) (\ env ->
751 eq_ifContext env (ifCtxt d1) (ifCtxt d2) &&&
752 eqListBy (eq_hsFD env) (ifFDs d1) (ifFDs d2) &&&
753 eqListBy (eq_cls_sig env) (ifSigs d1) (ifSigs d2)
756 eqIfDecl _ _ = NotEqual -- default case
759 eqWith :: [IfaceTvBndr] -> [IfaceTvBndr] -> (EqEnv -> IfaceEq) -> IfaceEq
760 eqWith = eq_ifTvBndrs emptyEqEnv
762 -----------------------
763 eqIfInst d1 d2 = bool (ifDFun d1 == ifDFun d2) &&&
764 zapEq (ifInstHead d1 `eqIfType` ifInstHead d2)
765 -- zapEq: for instances, ignore the EqBut part
767 eqIfRule (IfaceRule n1 a1 bs1 f1 es1 rhs1)
768 (IfaceRule n2 a2 bs2 f2 es2 rhs2)
769 = bool (n1==n2 && a1==a2) &&&
771 eq_ifBndrs emptyEqEnv bs1 bs2 (\env ->
772 zapEq (eqListBy (eq_ifaceExpr env) es1 es2) &&&
773 -- zapEq: for the LHSs, ignore the EqBut part
774 eq_ifaceExpr env rhs1 rhs2)
775 eqIfRule _ _ = NotEqual
777 eq_hsCD env (IfDataTyCon c1) (IfDataTyCon c2) = eqListBy (eq_ConDecl env) c1 c2
778 eq_hsCD env (IfNewTyCon c1) (IfNewTyCon c2) = eq_ConDecl env c1 c2
779 eq_hsCD env IfAbstractTyCon IfAbstractTyCon = Equal
780 eq_hsCD env d1 d2 = NotEqual
782 eq_ConDecl env (IfaceConDecl n1 tvs1 cxt1 args1 ss1 lbls1)
783 (IfaceConDecl n2 tvs2 cxt2 args2 ss2 lbls2)
784 = bool (n1 == n2 && ss1 == ss2 && lbls1 == lbls2) &&&
785 eq_ifTvBndrs env tvs1 tvs2 (\ env ->
786 eq_ifContext env cxt1 cxt2 &&&
787 eq_ifTypes env args1 args2)
789 eq_hsFD env (ns1,ms1) (ns2,ms2)
790 = eqListBy (eqIfOcc env) ns1 ns2 &&& eqListBy (eqIfOcc env) ms1 ms2
792 eq_cls_sig env (IfaceClassOp n1 dm1 ty1) (IfaceClassOp n2 dm2 ty2)
793 = bool (n1==n2 && dm1 == dm2) &&& eq_ifType env ty1 ty2
799 eqIfIdInfo NoInfo NoInfo = Equal
800 eqIfIdInfo (HasInfo is1) (HasInfo is2) = eqListBy eq_item is1 is2
801 eqIfIdInfo i1 i2 = NotEqual
803 eq_item (HsArity a1) (HsArity a2) = bool (a1 == a2)
804 eq_item (HsStrictness s1) (HsStrictness s2) = bool (s1 == s2)
805 eq_item (HsUnfold a1 u1) (HsUnfold a2 u2) = bool (a1 == a2) &&& eq_ifaceExpr emptyEqEnv u1 u2
806 eq_item HsNoCafRefs HsNoCafRefs = Equal
807 eq_item (HsWorker occ1 a1) (HsWorker occ2 a2) = bool (a1==a2 && occ1==occ2)
808 eq_item _ _ = NotEqual
811 eq_ifaceExpr :: EqEnv -> IfaceExpr -> IfaceExpr -> IfaceEq
812 eq_ifaceExpr env (IfaceLcl v1) (IfaceLcl v2) = eqIfOcc env v1 v2
813 eq_ifaceExpr env (IfaceExt v1) (IfaceExt v2) = eqIfExt v1 v2
814 eq_ifaceExpr env (IfaceLit l1) (IfaceLit l2) = bool (l1 == l2)
815 eq_ifaceExpr env (IfaceFCall c1 ty1) (IfaceFCall c2 ty2) = bool (c1==c2) &&& eq_ifType env ty1 ty2
816 eq_ifaceExpr env (IfaceType ty1) (IfaceType ty2) = eq_ifType env ty1 ty2
817 eq_ifaceExpr env (IfaceTuple n1 as1) (IfaceTuple n2 as2) = bool (n1==n2) &&& eqListBy (eq_ifaceExpr env) as1 as2
818 eq_ifaceExpr env (IfaceLam b1 body1) (IfaceLam b2 body2) = eq_ifBndr env b1 b2 (\env -> eq_ifaceExpr env body1 body2)
819 eq_ifaceExpr env (IfaceApp f1 a1) (IfaceApp f2 a2) = eq_ifaceExpr env f1 f2 &&& eq_ifaceExpr env a1 a2
820 eq_ifaceExpr env (IfaceNote n1 r1) (IfaceNote n2 r2) = eq_ifaceNote env n1 n2 &&& eq_ifaceExpr env r1 r2
822 eq_ifaceExpr env (IfaceCase s1 b1 as1) (IfaceCase s2 b2 as2)
823 = eq_ifaceExpr env s1 s2 &&&
824 eq_ifNakedBndr env b1 b2 (\env -> eqListBy (eq_ifaceAlt env) as1 as2)
826 eq_ifaceAlt env (c1,bs1,r1) (c2,bs2,r2)
827 = bool (eq_ifaceConAlt c1 c2) &&&
828 eq_ifNakedBndrs env bs1 bs2 (\env -> eq_ifaceExpr env r1 r2)
830 eq_ifaceExpr env (IfaceLet (IfaceNonRec b1 r1) x1) (IfaceLet (IfaceNonRec b2 r2) x2)
831 = eq_ifaceExpr env r1 r2 &&& eq_ifIdBndr env b1 b2 (\env -> eq_ifaceExpr env x1 x2)
833 eq_ifaceExpr env (IfaceLet (IfaceRec as1) x1) (IfaceLet (IfaceRec as2) x2)
834 = eq_ifIdBndrs env bs1 bs2 (\env -> eqListBy (eq_ifaceExpr env) rs1 rs2 &&& eq_ifaceExpr env x1 x2)
836 (bs1,rs1) = unzip as1
837 (bs2,rs2) = unzip as2
840 eq_ifaceExpr env _ _ = NotEqual
843 eq_ifaceConAlt :: IfaceConAlt -> IfaceConAlt -> Bool
844 eq_ifaceConAlt IfaceDefault IfaceDefault = True
845 eq_ifaceConAlt (IfaceDataAlt n1) (IfaceDataAlt n2) = n1==n2
846 eq_ifaceConAlt (IfaceTupleAlt c1) (IfaceTupleAlt c2) = c1==c2
847 eq_ifaceConAlt (IfaceLitAlt l1) (IfaceLitAlt l2) = l1==l2
848 eq_ifaceConAlt _ _ = False
851 eq_ifaceNote :: EqEnv -> IfaceNote -> IfaceNote -> IfaceEq
852 eq_ifaceNote env (IfaceSCC c1) (IfaceSCC c2) = bool (c1==c2)
853 eq_ifaceNote env (IfaceCoerce t1) (IfaceCoerce t2) = eq_ifType env t1 t2
854 eq_ifaceNote env IfaceInlineCall IfaceInlineCall = Equal
855 eq_ifaceNote env IfaceInlineMe IfaceInlineMe = Equal
856 eq_ifaceNote env (IfaceCoreNote s1) (IfaceCoreNote s2) = bool (s1==s2)
857 eq_ifaceNote env _ _ = NotEqual
861 ---------------------
862 eqIfType t1 t2 = eq_ifType emptyEqEnv t1 t2
865 eq_ifType env (IfaceTyVar n1) (IfaceTyVar n2) = eqIfOcc env n1 n2
866 eq_ifType env (IfaceAppTy s1 t1) (IfaceAppTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
867 eq_ifType env (IfacePredTy st1) (IfacePredTy st2) = eq_ifPredType env st1 st2
868 eq_ifType env (IfaceTyConApp tc1 ts1) (IfaceTyConApp tc2 ts2) = tc1 `eqIfTc` tc2 &&& eq_ifTypes env ts1 ts2
869 eq_ifType env (IfaceForAllTy tv1 t1) (IfaceForAllTy tv2 t2) = eq_ifTvBndr env tv1 tv2 (\env -> eq_ifType env t1 t2)
870 eq_ifType env (IfaceFunTy s1 t1) (IfaceFunTy s2 t2) = eq_ifType env s1 s2 &&& eq_ifType env t1 t2
871 eq_ifType env _ _ = NotEqual
874 eq_ifTypes env = eqListBy (eq_ifType env)
877 eq_ifContext env a b = eqListBy (eq_ifPredType env) a b
880 eq_ifPredType env (IfaceClassP c1 tys1) (IfaceClassP c2 tys2) = c1 `eqIfExt` c2 &&& eq_ifTypes env tys1 tys2
881 eq_ifPredType env (IfaceIParam n1 ty1) (IfaceIParam n2 ty2) = bool (n1 == n2) &&& eq_ifType env ty1 ty2
882 eq_ifPredType env _ _ = NotEqual
885 eqIfTc (IfaceTc tc1) (IfaceTc tc2) = tc1 `eqIfExt` tc2
886 eqIfTc IfaceIntTc IfaceIntTc = Equal
887 eqIfTc IfaceCharTc IfaceCharTc = Equal
888 eqIfTc IfaceBoolTc IfaceBoolTc = Equal
889 eqIfTc IfaceListTc IfaceListTc = Equal
890 eqIfTc IfacePArrTc IfacePArrTc = Equal
891 eqIfTc (IfaceTupTc bx1 ar1) (IfaceTupTc bx2 ar2) = bool (bx1==bx2 && ar1==ar2)
892 eqIfTc _ _ = NotEqual
895 -----------------------------------------------------------
896 Support code for equality checking
897 -----------------------------------------------------------
900 ------------------------------------
901 type EqEnv = OccEnv OccName -- Tracks the mapping from L-variables to R-variables
903 eqIfOcc :: EqEnv -> OccName -> OccName -> IfaceEq
904 eqIfOcc env n1 n2 = case lookupOccEnv env n1 of
905 Just n1 -> bool (n1 == n2)
906 Nothing -> bool (n1 == n2)
908 extendEqEnv :: EqEnv -> OccName -> OccName -> EqEnv
909 extendEqEnv env n1 n2 | n1 == n2 = env
910 | otherwise = extendOccEnv env n1 n2
913 emptyEqEnv = emptyOccEnv
915 ------------------------------------
916 type ExtEnv bndr = EqEnv -> bndr -> bndr -> (EqEnv -> IfaceEq) -> IfaceEq
918 eq_ifNakedBndr :: ExtEnv OccName
919 eq_ifBndr :: ExtEnv IfaceBndr
920 eq_ifTvBndr :: ExtEnv IfaceTvBndr
921 eq_ifIdBndr :: ExtEnv IfaceIdBndr
923 eq_ifNakedBndr env n1 n2 k = k (extendEqEnv env n1 n2)
925 eq_ifBndr env (IfaceIdBndr b1) (IfaceIdBndr b2) k = eq_ifIdBndr env b1 b2 k
926 eq_ifBndr env (IfaceTvBndr b1) (IfaceTvBndr b2) k = eq_ifTvBndr env b1 b2 k
927 eq_ifBndr _ _ _ _ = NotEqual
929 eq_ifTvBndr env (v1, k1) (v2, k2) k = bool (k1 == k2) &&& k (extendEqEnv env v1 v2)
930 eq_ifIdBndr env (v1, t1) (v2, t2) k = eq_ifType env t1 t2 &&& k (extendEqEnv env v1 v2)
932 eq_ifBndrs :: ExtEnv [IfaceBndr]
933 eq_ifIdBndrs :: ExtEnv [IfaceIdBndr]
934 eq_ifTvBndrs :: ExtEnv [IfaceTvBndr]
935 eq_ifNakedBndrs :: ExtEnv [OccName]
936 eq_ifBndrs = eq_bndrs_with eq_ifBndr
937 eq_ifIdBndrs = eq_bndrs_with eq_ifIdBndr
938 eq_ifTvBndrs = eq_bndrs_with eq_ifTvBndr
939 eq_ifNakedBndrs = eq_bndrs_with eq_ifNakedBndr
941 eq_bndrs_with eq env [] [] k = k env
942 eq_bndrs_with eq env (b1:bs1) (b2:bs2) k = eq env b1 b2 (\env -> eq_bndrs_with eq env bs1 bs2 k)
943 eq_bndrs_with eq env _ _ _ = NotEqual
947 eqListBy :: (a->a->IfaceEq) -> [a] -> [a] -> IfaceEq
948 eqListBy eq [] [] = Equal
949 eqListBy eq (x:xs) (y:ys) = eq x y &&& eqListBy eq xs ys
950 eqListBy eq xs ys = NotEqual
952 eqMaybeBy :: (a->a->IfaceEq) -> Maybe a -> Maybe a -> IfaceEq
953 eqMaybeBy eq Nothing Nothing = Equal
954 eqMaybeBy eq (Just x) (Just y) = eq x y
955 eqMaybeBy eq x y = NotEqual