2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsDecls]{Abstract syntax: global declarations}
6 Definitions for: @TyDecl@ and @oCnDecl@, @ClassDecl@,
7 @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
11 HsDecl(..), TyClDecl(..), InstDecl(..), RuleDecl(..), RuleBndr(..),
13 ForeignDecl(..), ForeignImport(..), ForeignExport(..),
14 CImportSpec(..), FoType(..),
15 ConDecl(..), ConDetails(..),
16 BangType(..), getBangType, getBangStrictness, unbangedType,
17 DeprecDecl(..), DeprecTxt,
18 hsDeclName, instDeclName,
19 tyClDeclName, tyClDeclNames, tyClDeclSysNames, tyClDeclTyVars,
20 isClassDecl, isSynDecl, isDataDecl, isIfaceSigDecl, isCoreDecl,
22 mkClassDeclSysNames, isSourceInstDecl, ifaceRuleDeclName,
23 getClassDeclSysNames, conDetailsTys,
27 #include "HsVersions.h"
30 import HsBinds ( HsBinds, MonoBinds, Sig(..), FixitySig(..) )
31 import HsExpr ( HsExpr )
32 import HsImpExp ( ppr_var )
34 import PprCore ( pprCoreRule )
35 import HsCore ( UfExpr, UfBinder, HsIdInfo, pprHsIdInfo,
36 eq_ufBinders, eq_ufExpr, pprUfExpr
38 import CoreSyn ( CoreRule(..), RuleName )
39 import BasicTypes ( NewOrData(..), StrictnessMark(..), Activation(..) )
40 import ForeignCall ( CCallTarget(..), DNCallSpec, CCallConv, Safety,
44 import Name ( NamedThing )
45 import FunDeps ( pprFundeps )
46 import TyCon ( DataConDetails(..), visibleDataCons )
47 import Class ( FunDep, DefMeth(..) )
48 import CStrings ( CLabelString )
50 import Util ( eqListBy, count )
51 import SrcLoc ( SrcLoc )
54 import Maybe ( isNothing, fromJust )
58 %************************************************************************
60 \subsection[HsDecl]{Declarations}
62 %************************************************************************
66 = TyClD (TyClDecl name pat)
67 | InstD (InstDecl name pat)
68 | DefD (DefaultDecl name)
69 | ValD (HsBinds name pat)
70 | ForD (ForeignDecl name)
71 | FixD (FixitySig name)
72 | DeprecD (DeprecDecl name)
73 | RuleD (RuleDecl name pat)
75 -- NB: all top-level fixity decls are contained EITHER
77 -- OR in the ClassDecls in TyClDs
80 -- a) data constructors
81 -- b) class methods (but they can be also done in the
82 -- signatures of class decls)
83 -- c) imported functions (that have an IfacSig)
86 -- The latter is for class methods only
91 hsDeclName :: (NamedThing name, Outputable name, Outputable pat)
92 => HsDecl name pat -> name
94 hsDeclName (TyClD decl) = tyClDeclName decl
95 hsDeclName (InstD decl) = instDeclName decl
96 hsDeclName (ForD decl) = foreignDeclName decl
97 hsDeclName (FixD (FixitySig name _ _)) = name
98 -- Others don't make sense
100 hsDeclName x = pprPanic "HsDecls.hsDeclName" (ppr x)
104 instDeclName :: InstDecl name pat -> name
105 instDeclName (InstDecl _ _ _ (Just name) _) = name
110 instance (NamedThing name, Outputable name, Outputable pat)
111 => Outputable (HsDecl name pat) where
113 ppr (TyClD dcl) = ppr dcl
114 ppr (ValD binds) = ppr binds
115 ppr (DefD def) = ppr def
116 ppr (InstD inst) = ppr inst
117 ppr (ForD fd) = ppr fd
118 ppr (FixD fd) = ppr fd
119 ppr (RuleD rd) = ppr rd
120 ppr (DeprecD dd) = ppr dd
124 %************************************************************************
126 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
128 %************************************************************************
130 --------------------------------
132 --------------------------------
134 Here is the story about the implicit names that go with type, class, and instance
135 decls. It's a bit tricky, so pay attention!
137 "Implicit" (or "system") binders
138 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139 Each data type decl defines
140 a worker name for each constructor
141 to-T and from-T convertors
142 Each class decl defines
143 a tycon for the class
144 a data constructor for that tycon
145 the worker for that constructor
146 a selector for each superclass
148 All have occurrence names that are derived uniquely from their parent declaration.
150 None of these get separate definitions in an interface file; they are
151 fully defined by the data or class decl. But they may *occur* in
152 interface files, of course. Any such occurrence must haul in the
153 relevant type or class decl.
156 - Make up their occurrence names immediately
157 This is done in RdrHsSyn.mkClassDecl, mkTyDecl, mkConDecl
159 - Ensure they "point to" the parent data/class decl
160 when loading that decl from an interface file
161 (See RnHiFiles.getTyClDeclSysNames)
163 - When renaming the decl look them up in the name cache,
164 ensure correct module and provenance is set
168 - Occurrence name is derived uniquely from the method name
171 - If there is a default method name at all, it's recorded in
172 the ClassOpSig (in HsBinds), in the DefMeth field.
173 (DefMeth is defined in Class.lhs)
175 Source-code class decls and interface-code class decls are treated subtly
176 differently, which has given me a great deal of confusion over the years.
177 Here's the deal. (We distinguish the two cases because source-code decls
178 have (Just binds) in the tcdMeths field, whereas interface decls have Nothing.
180 In *source-code* class declarations:
181 - When parsing, every ClassOpSig gets a DefMeth with a suitable RdrName
182 This is done by RdrHsSyn.mkClassOpSigDM
184 - The renamer renames it to a Name
186 - During typechecking, we generate a binding for each $dm for
187 which there's a programmer-supplied default method:
192 We generate a binding for $dmop1 but not for $dmop2.
193 The Class for Foo has a NoDefMeth for op2 and a DefMeth for op1.
194 The Name for $dmop2 is simply discarded.
196 In *interface-file* class declarations:
197 - When parsing, we see if there's an explicit programmer-supplied default method
198 because there's an '=' sign to indicate it:
200 op1 = :: <type> -- NB the '='
202 We use this info to generate a DefMeth with a suitable RdrName for op1,
203 and a NoDefMeth for op2
204 - The interface file has a separate definition for $dmop1, with unfolding etc.
205 - The renamer renames it to a Name.
206 - The renamer treats $dmop1 as a free variable of the declaration, so that
207 the binding for $dmop1 will be sucked in. (See RnHsSyn.tyClDeclFVs)
208 This doesn't happen for source code class decls, because they *bind* the default method.
212 Each instance declaration gives rise to one dictionary function binding.
214 The type checker makes up new source-code instance declarations
215 (e.g. from 'deriving' or generic default methods --- see
216 TcInstDcls.tcInstDecls1). So we can't generate the names for
217 dictionary functions in advance (we don't know how many we need).
219 On the other hand for interface-file instance declarations, the decl
220 specifies the name of the dictionary function, and it has a binding elsewhere
221 in the interface file:
222 instance {Eq Int} = dEqInt
223 dEqInt :: {Eq Int} <pragma info>
225 So again we treat source code and interface file code slightly differently.
228 - Source code instance decls have a Nothing in the (Maybe name) field
229 (see data InstDecl below)
231 - The typechecker makes up a Local name for the dict fun for any source-code
232 instance decl, whether it comes from a source-code instance decl, or whether
233 the instance decl is derived from some other construct (e.g. 'deriving').
235 - The occurrence name it chooses is derived from the instance decl (just for
236 documentation really) --- e.g. dNumInt. Two dict funs may share a common
237 occurrence name, but will have different uniques. E.g.
238 instance Foo [Int] where ...
239 instance Foo [Bool] where ...
240 These might both be dFooList
242 - The CoreTidy phase externalises the name, and ensures the occurrence name is
243 unique (this isn't special to dict funs). So we'd get dFooList and dFooList1.
245 - We can take this relaxed approach (changing the occurrence name later)
246 because dict fun Ids are not captured in a TyCon or Class (unlike default
247 methods, say). Instead, they are kept separately in the InstEnv. This
248 makes it easy to adjust them after compiling a module. (Once we've finished
249 compiling that module, they don't change any more.)
253 - The instance decl gives the dict fun name, so the InstDecl has a (Just name)
254 in the (Maybe name) field.
256 - RnHsSyn.instDeclFVs treats the dict fun name as free in the decl, so that we
257 suck in the dfun binding
261 -- TyClDecls are precisely the kind of declarations that can
262 -- appear in interface files; or (internally) in GHC's interface
263 -- for a module. That's why (despite the misnomer) IfaceSig and ForeignType
264 -- are both in TyClDecl
266 data TyClDecl name pat
267 = IfaceSig { tcdName :: name, -- It may seem odd to classify an interface-file signature
268 tcdType :: HsType name, -- as a 'TyClDecl', but it's very convenient.
269 tcdIdInfo :: [HsIdInfo name],
273 | ForeignType { tcdName :: name, -- See remarks about IfaceSig above
274 tcdExtName :: Maybe FastString,
278 | TyData { tcdND :: NewOrData,
279 tcdCtxt :: HsContext name, -- context
280 tcdName :: name, -- type constructor
281 tcdTyVars :: [HsTyVarBndr name], -- type variables
282 tcdCons :: DataConDetails (ConDecl name), -- data constructors (empty if abstract)
283 tcdDerivs :: Maybe (HsContext name), -- derivings; Nothing => not specified
284 -- Just [] => derive exactly what is asked
285 tcdSysNames :: DataSysNames name, -- Generic converter functions
289 | TySynonym { tcdName :: name, -- type constructor
290 tcdTyVars :: [HsTyVarBndr name], -- type variables
291 tcdSynRhs :: HsType name, -- synonym expansion
295 | ClassDecl { tcdCtxt :: HsContext name, -- Context...
296 tcdName :: name, -- Name of the class
297 tcdTyVars :: [HsTyVarBndr name], -- The class type variables
298 tcdFDs :: [FunDep name], -- Functional dependencies
299 tcdSigs :: [Sig name], -- Methods' signatures
300 tcdMeths :: Maybe (MonoBinds name pat), -- Default methods
301 -- Nothing for imported class decls
302 -- Just bs for source class decls
303 tcdSysNames :: ClassSysNames name,
306 -- a Core value binding (coming from 'external Core' input.)
307 | CoreDecl { tcdName :: name,
308 tcdType :: HsType name,
309 tcdRhs :: UfExpr name,
318 isIfaceSigDecl, isCoreDecl, isDataDecl, isSynDecl, isClassDecl :: TyClDecl name pat -> Bool
320 isIfaceSigDecl (IfaceSig {}) = True
321 isIfaceSigDecl other = False
323 isSynDecl (TySynonym {}) = True
324 isSynDecl other = False
326 isDataDecl (TyData {}) = True
327 isDataDecl other = False
329 isClassDecl (ClassDecl {}) = True
330 isClassDecl other = False
332 isCoreDecl (CoreDecl {}) = True
333 isCoreDecl other = False
340 --------------------------------
341 tyClDeclName :: TyClDecl name pat -> name
342 tyClDeclName tycl_decl = tcdName tycl_decl
344 --------------------------------
345 tyClDeclNames :: Eq name => TyClDecl name pat -> [(name, SrcLoc)]
346 -- Returns all the *binding* names of the decl, along with their SrcLocs
347 -- The first one is guaranteed to be the name of the decl
348 -- For record fields, the first one counts as the SrcLoc
349 -- We use the equality to filter out duplicate field names
351 tyClDeclNames (TySynonym {tcdName = name, tcdLoc = loc}) = [(name,loc)]
352 tyClDeclNames (IfaceSig {tcdName = name, tcdLoc = loc}) = [(name,loc)]
353 tyClDeclNames (CoreDecl {tcdName = name, tcdLoc = loc}) = [(name,loc)]
354 tyClDeclNames (ForeignType {tcdName = name, tcdLoc = loc}) = [(name,loc)]
356 tyClDeclNames (ClassDecl {tcdName = cls_name, tcdSigs = sigs, tcdLoc = loc})
357 = (cls_name,loc) : [(n,loc) | ClassOpSig n _ _ loc <- sigs]
359 tyClDeclNames (TyData {tcdName = tc_name, tcdCons = cons, tcdLoc = loc})
360 = (tc_name,loc) : conDeclsNames cons
363 tyClDeclTyVars (TySynonym {tcdTyVars = tvs}) = tvs
364 tyClDeclTyVars (TyData {tcdTyVars = tvs}) = tvs
365 tyClDeclTyVars (ClassDecl {tcdTyVars = tvs}) = tvs
366 tyClDeclTyVars (ForeignType {}) = []
367 tyClDeclTyVars (IfaceSig {}) = []
368 tyClDeclTyVars (CoreDecl {}) = []
371 --------------------------------
372 -- The "system names" are extra implicit names *bound* by the decl.
373 -- They are kept in a list rather than a tuple
374 -- to make the renamer easier.
376 type ClassSysNames name = [name]
377 -- For class decls they are:
378 -- [tycon, datacon wrapper, datacon worker,
379 -- superclass selector 1, ..., superclass selector n]
381 type DataSysNames name = [name]
382 -- For data decls they are
384 -- where from :: T -> Tring
387 tyClDeclSysNames :: TyClDecl name pat -> [(name, SrcLoc)]
388 -- Similar to tyClDeclNames, but returns the "implicit"
389 -- or "system" names of the declaration
391 tyClDeclSysNames (ClassDecl {tcdSysNames = names, tcdLoc = loc})
392 = [(n,loc) | n <- names]
393 tyClDeclSysNames (TyData {tcdCons = DataCons cons, tcdSysNames = names, tcdLoc = loc})
394 = [(n,loc) | n <- names] ++
395 [(wkr_name,loc) | ConDecl _ wkr_name _ _ _ loc <- cons]
396 tyClDeclSysNames decl = []
399 mkClassDeclSysNames :: (name, name, name, [name]) -> [name]
400 getClassDeclSysNames :: [name] -> (name, name, name, [name])
401 mkClassDeclSysNames (a,b,c,ds) = a:b:c:ds
402 getClassDeclSysNames (a:b:c:ds) = (a,b,c,ds)
406 instance (NamedThing name, Ord name) => Eq (TyClDecl name pat) where
407 -- Used only when building interface files
408 (==) d1@(IfaceSig {}) d2@(IfaceSig {})
409 = tcdName d1 == tcdName d2 &&
410 tcdType d1 == tcdType d2 &&
411 tcdIdInfo d1 == tcdIdInfo d2
413 (==) d1@(CoreDecl {}) d2@(CoreDecl {})
414 = tcdName d1 == tcdName d2 &&
415 tcdType d1 == tcdType d2 &&
416 tcdRhs d1 == tcdRhs d2
418 (==) d1@(ForeignType {}) d2@(ForeignType {})
419 = tcdName d1 == tcdName d2 &&
420 tcdFoType d1 == tcdFoType d2
422 (==) d1@(TyData {}) d2@(TyData {})
423 = tcdName d1 == tcdName d2 &&
424 tcdND d1 == tcdND d2 &&
425 eqWithHsTyVars (tcdTyVars d1) (tcdTyVars d2) (\ env ->
426 eq_hsContext env (tcdCtxt d1) (tcdCtxt d2) &&
427 eq_hsCD env (tcdCons d1) (tcdCons d2)
430 (==) d1@(TySynonym {}) d2@(TySynonym {})
431 = tcdName d1 == tcdName d2 &&
432 eqWithHsTyVars (tcdTyVars d1) (tcdTyVars d2) (\ env ->
433 eq_hsType env (tcdSynRhs d1) (tcdSynRhs d2)
436 (==) d1@(ClassDecl {}) d2@(ClassDecl {})
437 = tcdName d1 == tcdName d2 &&
438 eqWithHsTyVars (tcdTyVars d1) (tcdTyVars d2) (\ env ->
439 eq_hsContext env (tcdCtxt d1) (tcdCtxt d2) &&
440 eqListBy (eq_hsFD env) (tcdFDs d1) (tcdFDs d2) &&
441 eqListBy (eq_cls_sig env) (tcdSigs d1) (tcdSigs d2)
444 (==) _ _ = False -- default case
446 eq_hsCD env (DataCons c1) (DataCons c2) = eqListBy (eq_ConDecl env) c1 c2
447 eq_hsCD env Unknown Unknown = True
448 eq_hsCD env (HasCons n1) (HasCons n2) = n1 == n2
449 eq_hsCD env d1 d2 = False
451 eq_hsFD env (ns1,ms1) (ns2,ms2)
452 = eqListBy (eq_hsVar env) ns1 ns2 && eqListBy (eq_hsVar env) ms1 ms2
454 eq_cls_sig env (ClassOpSig n1 dm1 ty1 _) (ClassOpSig n2 dm2 ty2 _)
455 = n1==n2 && dm1 `eq_dm` dm2 && eq_hsType env ty1 ty2
457 -- Ignore the name of the default method for (DefMeth id)
458 -- This is used for comparing declarations before putting
459 -- them into interface files, and the name of the default
460 -- method isn't relevant
461 NoDefMeth `eq_dm` NoDefMeth = True
462 GenDefMeth `eq_dm` GenDefMeth = True
463 DefMeth _ `eq_dm` DefMeth _ = True
464 dm1 `eq_dm` dm2 = False
470 countTyClDecls :: [TyClDecl name pat] -> (Int, Int, Int, Int, Int)
471 -- class, data, newtype, synonym decls
473 = (count isClassDecl decls,
474 count isSynDecl decls,
475 count (\ x -> isIfaceSigDecl x || isCoreDecl x) decls,
476 count isDataTy decls,
479 isDataTy TyData{tcdND=DataType} = True
482 isNewTy TyData{tcdND=NewType} = True
487 instance (NamedThing name, Outputable name, Outputable pat)
488 => Outputable (TyClDecl name pat) where
490 ppr (IfaceSig {tcdName = var, tcdType = ty, tcdIdInfo = info})
491 = getPprStyle $ \ sty ->
492 hsep [ ppr_var var, dcolon, ppr ty, pprHsIdInfo info ]
494 ppr (ForeignType {tcdName = tycon})
495 = hsep [ptext SLIT("foreign import type dotnet"), ppr tycon]
497 ppr (TySynonym {tcdName = tycon, tcdTyVars = tyvars, tcdSynRhs = mono_ty})
498 = hang (ptext SLIT("type") <+> pp_decl_head [] tycon tyvars <+> equals)
501 ppr (TyData {tcdND = new_or_data, tcdCtxt = context, tcdName = tycon,
502 tcdTyVars = tyvars, tcdCons = condecls,
503 tcdDerivs = derivings})
504 = pp_tydecl (ptext keyword <+> pp_decl_head context tycon tyvars)
505 (pp_condecls condecls)
508 keyword = case new_or_data of
509 NewType -> SLIT("newtype")
510 DataType -> SLIT("data")
512 ppr (ClassDecl {tcdCtxt = context, tcdName = clas, tcdTyVars = tyvars, tcdFDs = fds,
513 tcdSigs = sigs, tcdMeths = methods})
514 | null sigs -- No "where" part
517 | otherwise -- Laid out
518 = sep [hsep [top_matter, ptext SLIT("where {")],
519 nest 4 (sep [sep (map ppr_sig sigs), pp_methods, char '}'])]
521 top_matter = ptext SLIT("class") <+> pp_decl_head context clas tyvars <+> pprFundeps fds
522 ppr_sig sig = ppr sig <> semi
524 pp_methods = if isNothing methods
526 else ppr (fromJust methods)
528 ppr (CoreDecl {tcdName = var, tcdType = ty, tcdRhs = rhs})
529 = getPprStyle $ \ sty ->
530 hsep [ ppr_var var, dcolon, ppr ty, ppr rhs ]
532 pp_decl_head :: Outputable name => HsContext name -> name -> [HsTyVarBndr name] -> SDoc
533 pp_decl_head context thing tyvars = hsep [pprHsContext context, ppr thing, interppSP tyvars]
535 pp_condecls Unknown = ptext SLIT("{- abstract -}")
536 pp_condecls (HasCons n) = ptext SLIT("{- abstract with") <+> int n <+> ptext SLIT("constructors -}")
537 pp_condecls (DataCons cs) = equals <+> sep (punctuate (ptext SLIT(" |")) (map ppr cs))
539 pp_tydecl pp_head pp_decl_rhs derivings
540 = hang pp_head 4 (sep [
544 Just ds -> hsep [ptext SLIT("deriving"), ppr_hs_context ds]
549 %************************************************************************
551 \subsection[ConDecl]{A data-constructor declaration}
553 %************************************************************************
557 = ConDecl name -- Constructor name; this is used for the
558 -- DataCon itself, and for the user-callable wrapper Id
560 name -- Name of the constructor's 'worker Id'
561 -- Filled in as the ConDecl is built
563 [HsTyVarBndr name] -- Existentially quantified type variables
564 (HsContext name) -- ...and context
565 -- If both are empty then there are no existentials
571 = VanillaCon -- prefix-style con decl
574 | InfixCon -- infix-style con decl
578 | RecCon -- record-style con decl
579 [([name], BangType name)] -- list of "fields"
583 conDeclsNames :: Eq name => DataConDetails (ConDecl name) -> [(name,SrcLoc)]
584 -- See tyClDeclNames for what this does
585 -- The function is boringly complicated because of the records
586 -- And since we only have equality, we have to be a little careful
588 = snd (foldl do_one ([], []) (visibleDataCons cons))
590 do_one (flds_seen, acc) (ConDecl name _ _ _ details loc)
591 = do_details ((name,loc):acc) details
593 do_details acc (RecCon flds) = foldl do_fld (flds_seen, acc) flds
594 do_details acc other = (flds_seen, acc)
596 do_fld acc (flds, _) = foldl do_fld1 acc flds
598 do_fld1 (flds_seen, acc) fld
599 | fld `elem` flds_seen = (flds_seen,acc)
600 | otherwise = (fld:flds_seen, (fld,loc):acc)
604 conDetailsTys :: ConDetails name -> [HsType name]
605 conDetailsTys (VanillaCon btys) = map getBangType btys
606 conDetailsTys (InfixCon bty1 bty2) = [getBangType bty1, getBangType bty2]
607 conDetailsTys (RecCon fields) = [getBangType bty | (_, bty) <- fields]
610 eq_ConDecl env (ConDecl n1 _ tvs1 cxt1 cds1 _)
611 (ConDecl n2 _ tvs2 cxt2 cds2 _)
613 (eq_hsTyVars env tvs1 tvs2 $ \ env ->
614 eq_hsContext env cxt1 cxt2 &&
615 eq_ConDetails env cds1 cds2)
617 eq_ConDetails env (VanillaCon bts1) (VanillaCon bts2)
618 = eqListBy (eq_btype env) bts1 bts2
619 eq_ConDetails env (InfixCon bta1 btb1) (InfixCon bta2 btb2)
620 = eq_btype env bta1 bta2 && eq_btype env btb1 btb2
621 eq_ConDetails env (RecCon fs1) (RecCon fs2)
622 = eqListBy (eq_fld env) fs1 fs2
623 eq_ConDetails env _ _ = False
625 eq_fld env (ns1,bt1) (ns2, bt2) = ns1==ns2 && eq_btype env bt1 bt2
629 data BangType name = BangType StrictnessMark (HsType name)
631 getBangType (BangType _ ty) = ty
632 getBangStrictness (BangType s _) = s
634 unbangedType ty = BangType NotMarkedStrict ty
636 eq_btype env (BangType s1 t1) (BangType s2 t2) = s1==s2 && eq_hsType env t1 t2
640 instance (Outputable name) => Outputable (ConDecl name) where
641 ppr (ConDecl con _ tvs cxt con_details loc)
642 = sep [pprHsForAll tvs cxt, ppr_con_details con con_details]
644 ppr_con_details con (InfixCon ty1 ty2)
645 = hsep [ppr_bang ty1, ppr con, ppr_bang ty2]
647 -- ConDecls generated by MkIface.ifaceTyThing always have a VanillaCon, even
648 -- if the constructor is an infix one. This is because in an interface file
649 -- we don't distinguish between the two. Hence when printing these for the
650 -- user, we need to parenthesise infix constructor names.
651 ppr_con_details con (VanillaCon tys)
652 = hsep (ppr_var con : map (ppr_bang) tys)
654 ppr_con_details con (RecCon fields)
655 = ppr con <+> braces (sep (punctuate comma (map ppr_field fields)))
657 ppr_field (ns, ty) = hsep (map (ppr) ns) <+>
661 instance Outputable name => Outputable (BangType name) where
664 ppr_bang (BangType s ty) = ppr s <> pprParendHsType ty
668 %************************************************************************
670 \subsection[InstDecl]{An instance declaration
672 %************************************************************************
675 data InstDecl name pat
676 = InstDecl (HsType name) -- Context => Class Instance-type
677 -- Using a polytype means that the renamer conveniently
678 -- figures out the quantified type variables for us.
682 [Sig name] -- User-supplied pragmatic info
684 (Maybe name) -- Name for the dictionary function
685 -- Nothing for source-file instance decls
689 isSourceInstDecl :: InstDecl name pat -> Bool
690 isSourceInstDecl (InstDecl _ _ _ maybe_dfun _) = isNothing maybe_dfun
694 instance (Outputable name, Outputable pat)
695 => Outputable (InstDecl name pat) where
697 ppr (InstDecl inst_ty binds uprags maybe_dfun_name src_loc)
698 = vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
702 pp_dfun = case maybe_dfun_name of
708 instance Ord name => Eq (InstDecl name pat) where
709 -- Used for interface comparison only, so don't compare bindings
710 (==) (InstDecl inst_ty1 _ _ dfun1 _) (InstDecl inst_ty2 _ _ dfun2 _)
711 = inst_ty1 == inst_ty2 && dfun1 == dfun2
715 %************************************************************************
717 \subsection[DefaultDecl]{A @default@ declaration}
719 %************************************************************************
721 There can only be one default declaration per module, but it is hard
722 for the parser to check that; we pass them all through in the abstract
723 syntax, and that restriction must be checked in the front end.
726 data DefaultDecl name
727 = DefaultDecl [HsType name]
730 instance (Outputable name)
731 => Outputable (DefaultDecl name) where
733 ppr (DefaultDecl tys src_loc)
734 = ptext SLIT("default") <+> parens (interpp'SP tys)
737 %************************************************************************
739 \subsection{Foreign function interface declaration}
741 %************************************************************************
745 -- foreign declarations are distinguished as to whether they define or use a
748 -- * the Boolean value indicates whether the pre-standard deprecated syntax
751 data ForeignDecl name
752 = ForeignImport name (HsType name) ForeignImport Bool SrcLoc -- defines name
753 | ForeignExport name (HsType name) ForeignExport Bool SrcLoc -- uses name
755 -- yield the Haskell name defined or used in a foreign declaration
757 foreignDeclName :: ForeignDecl name -> name
758 foreignDeclName (ForeignImport n _ _ _ _) = n
759 foreignDeclName (ForeignExport n _ _ _ _) = n
761 -- specification of an imported external entity in dependence on the calling
764 data ForeignImport = -- import of a C entity
766 -- * the two strings specifying a header file or library
767 -- may be empty, which indicates the absence of a
768 -- header or object specification (both are not used
769 -- in the case of `CWrapper' and when `CFunction'
770 -- has a dynamic target)
772 -- * the calling convention is irrelevant for code
773 -- generation in the case of `CLabel', but is needed
774 -- for pretty printing
776 -- * `Safety' is irrelevant for `CLabel' and `CWrapper'
778 CImport CCallConv -- ccall or stdcall
779 Safety -- safe or unsafe
780 FastString -- name of C header
781 FastString -- name of library object
782 CImportSpec -- details of the C entity
784 -- import of a .NET function
786 | DNImport DNCallSpec
788 -- details of an external C entity
790 data CImportSpec = CLabel CLabelString -- import address of a C label
791 | CFunction CCallTarget -- static or dynamic function
792 | CWrapper -- wrapper to expose closures
795 -- specification of an externally exported entity in dependence on the calling
798 data ForeignExport = CExport CExportSpec -- contains the calling convention
799 | DNExport -- presently unused
801 -- abstract type imported from .NET
803 data FoType = DNType -- In due course we'll add subtype stuff
804 deriving (Eq) -- Used for equality instance for TyClDecl
807 -- pretty printing of foreign declarations
810 instance Outputable name => Outputable (ForeignDecl name) where
811 ppr (ForeignImport n ty fimport _ _) =
812 ptext SLIT("foreign import") <+> ppr fimport <+>
813 ppr n <+> dcolon <+> ppr ty
814 ppr (ForeignExport n ty fexport _ _) =
815 ptext SLIT("foreign export") <+> ppr fexport <+>
816 ppr n <+> dcolon <+> ppr ty
818 instance Outputable ForeignImport where
819 ppr (DNImport spec) =
820 ptext SLIT("dotnet") <+> ppr spec
821 ppr (CImport cconv safety header lib spec) =
822 ppr cconv <+> ppr safety <+>
823 char '"' <> pprCEntity header lib spec <> char '"'
825 pprCEntity header lib (CLabel lbl) =
826 ptext SLIT("static") <+> ptext header <+> char '&' <>
827 pprLib lib <> ppr lbl
828 pprCEntity header lib (CFunction (StaticTarget lbl)) =
829 ptext SLIT("static") <+> ptext header <+> char '&' <>
830 pprLib lib <> ppr lbl
831 pprCEntity header lib (CFunction (DynamicTarget)) =
832 ptext SLIT("dynamic")
833 pprCEntity header lib (CFunction (CasmTarget _)) =
834 panic "HsDecls.pprCEntity: malformed C function target"
835 pprCEntity _ _ (CWrapper) = ptext SLIT("wrapper")
837 pprLib lib | nullFastString lib = empty
838 | otherwise = char '[' <> ppr lib <> char ']'
840 instance Outputable ForeignExport where
841 ppr (CExport (CExportStatic lbl cconv)) =
842 ppr cconv <+> char '"' <> ppr lbl <> char '"'
844 ptext SLIT("dotnet") <+> ptext SLIT("\"<unused>\"")
846 instance Outputable FoType where
847 ppr DNType = ptext SLIT("type dotnet")
851 %************************************************************************
853 \subsection{Transformation rules}
855 %************************************************************************
858 data RuleDecl name pat
859 = HsRule -- Source rule
860 RuleName -- Rule name
862 [RuleBndr name] -- Forall'd vars; after typechecking this includes tyvars
863 (HsExpr name pat) -- LHS
864 (HsExpr name pat) -- RHS
867 | IfaceRule -- One that's come in from an interface file; pre-typecheck
870 [UfBinder name] -- Tyvars and term vars
872 [UfExpr name] -- Args of LHS
873 (UfExpr name) -- Pre typecheck
876 | IfaceRuleOut -- Post typecheck
880 ifaceRuleDeclName :: RuleDecl name pat -> name
881 ifaceRuleDeclName (IfaceRule _ _ _ n _ _ _) = n
882 ifaceRuleDeclName (IfaceRuleOut n r) = n
883 ifaceRuleDeclName (HsRule fs _ _ _ _ _) = pprPanic "ifaceRuleDeclName" (ppr fs)
887 | RuleBndrSig name (HsType name)
889 collectRuleBndrSigTys :: [RuleBndr name] -> [HsType name]
890 collectRuleBndrSigTys bndrs = [ty | RuleBndrSig _ ty <- bndrs]
892 instance (NamedThing name, Ord name) => Eq (RuleDecl name pat) where
893 -- Works for IfaceRules only; used when comparing interface file versions
894 (IfaceRule n1 a1 bs1 f1 es1 rhs1 _) == (IfaceRule n2 a2 bs2 f2 es2 rhs2 _)
895 = n1==n2 && f1 == f2 && a1==a2 &&
896 eq_ufBinders emptyEqHsEnv bs1 bs2 (\env ->
897 eqListBy (eq_ufExpr env) (rhs1:es1) (rhs2:es2))
899 instance (NamedThing name, Outputable name, Outputable pat)
900 => Outputable (RuleDecl name pat) where
901 ppr (HsRule name act ns lhs rhs loc)
902 = sep [text "{-# RULES" <+> doubleQuotes (ptext name) <+> ppr act,
903 pp_forall, ppr lhs, equals <+> ppr rhs,
906 pp_forall | null ns = empty
907 | otherwise = text "forall" <+> fsep (map ppr ns) <> dot
909 ppr (IfaceRule name act tpl_vars fn tpl_args rhs loc)
910 = hsep [ doubleQuotes (ptext name), ppr act,
911 ptext SLIT("__forall") <+> braces (interppSP tpl_vars),
912 ppr fn <+> sep (map (pprUfExpr parens) tpl_args),
913 ptext SLIT("=") <+> ppr rhs
916 ppr (IfaceRuleOut fn rule) = pprCoreRule (ppr fn) rule
918 instance Outputable name => Outputable (RuleBndr name) where
919 ppr (RuleBndr name) = ppr name
920 ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty
924 %************************************************************************
926 \subsection[DeprecDecl]{Deprecations}
928 %************************************************************************
930 We use exported entities for things to deprecate.
933 data DeprecDecl name = Deprecation name DeprecTxt SrcLoc
935 type DeprecTxt = FAST_STRING -- reason/explanation for deprecation
937 instance Outputable name => Outputable (DeprecDecl name) where
938 ppr (Deprecation thing txt _)
939 = hsep [text "{-# DEPRECATED", ppr thing, doubleQuotes (ppr txt), text "#-}"]