2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 HsDecls: Abstract syntax: global declarations
8 Definitions for: @TyDecl@ and @oCnDecl@, @ClassDecl@,
9 @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
13 HsDecl(..), LHsDecl, TyClDecl(..), LTyClDecl,
14 InstDecl(..), LInstDecl, DerivDecl(..), LDerivDecl, NewOrData(..),
15 RuleDecl(..), LRuleDecl, RuleBndr(..),
16 DefaultDecl(..), LDefaultDecl, SpliceDecl(..),
17 ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..),
18 CImportSpec(..), FoType(..),
19 ConDecl(..), ResType(..), LConDecl,
20 DocDecl(..), LDocDecl, docDeclDoc, DocEntity(..),
21 DeprecDecl(..), LDeprecDecl,
22 HsGroup(..), emptyRdrGroup, emptyRnGroup, appendGroups,
23 tcdName, tyClDeclNames, tyClDeclTyVars,
24 isClassDecl, isTFunDecl, isSynDecl, isDataDecl, isKindSigDecl,
29 collectRuleBndrSigTys,
32 #include "HsVersions.h"
35 import {-# SOURCE #-} HsExpr( HsExpr, pprExpr )
36 -- Because Expr imports Decls via HsBracket
45 import {- Kind parts of -} Type
56 import Data.Maybe ( isJust )
59 %************************************************************************
61 \subsection[HsDecl]{Declarations}
63 %************************************************************************
66 type LHsDecl id = Located (HsDecl id)
71 | DerivD (DerivDecl id)
74 | DefD (DefaultDecl id)
75 | ForD (ForeignDecl id)
76 | DeprecD (DeprecDecl id)
78 | SpliceD (SpliceDecl id)
82 -- NB: all top-level fixity decls are contained EITHER
84 -- OR in the ClassDecls in TyClDs
87 -- a) data constructors
88 -- b) class methods (but they can be also done in the
89 -- signatures of class decls)
90 -- c) imported functions (that have an IfacSig)
93 -- The latter is for class methods only
95 -- A [HsDecl] is categorised into a HsGroup before being
96 -- fed to the renamer.
99 hs_valds :: HsValBinds id,
100 hs_tyclds :: [LTyClDecl id],
101 hs_instds :: [LInstDecl id],
102 hs_derivds :: [LDerivDecl id],
104 hs_fixds :: [LFixitySig id],
105 -- Snaffled out of both top-level fixity signatures,
106 -- and those in class declarations
108 hs_defds :: [LDefaultDecl id],
109 hs_fords :: [LForeignDecl id],
110 hs_depds :: [LDeprecDecl id],
111 hs_ruleds :: [LRuleDecl id],
113 hs_docs :: [DocEntity id]
114 -- Used to remember the module structure,
115 -- which is needed to produce Haddock documentation
118 emptyGroup, emptyRdrGroup, emptyRnGroup :: HsGroup a
119 emptyRdrGroup = emptyGroup { hs_valds = emptyValBindsIn }
120 emptyRnGroup = emptyGroup { hs_valds = emptyValBindsOut }
122 emptyGroup = HsGroup { hs_tyclds = [], hs_instds = [], hs_derivds = [],
123 hs_fixds = [], hs_defds = [], hs_fords = [],
124 hs_depds = [], hs_ruleds = [],
125 hs_valds = error "emptyGroup hs_valds: Can't happen",
128 appendGroups :: HsGroup a -> HsGroup a -> HsGroup a
131 hs_valds = val_groups1,
134 hs_derivds = derivds1,
142 hs_valds = val_groups2,
145 hs_derivds = derivds2,
154 hs_valds = val_groups1 `plusHsValBinds` val_groups2,
155 hs_tyclds = tyclds1 ++ tyclds2,
156 hs_instds = instds1 ++ instds2,
157 hs_derivds = derivds1 ++ derivds2,
158 hs_fixds = fixds1 ++ fixds2,
159 hs_defds = defds1 ++ defds2,
160 hs_fords = fords1 ++ fords2,
161 hs_depds = depds1 ++ depds2,
162 hs_ruleds = rulds1 ++ rulds2,
163 hs_docs = docs1 ++ docs2 }
167 instance OutputableBndr name => Outputable (HsDecl name) where
168 ppr (TyClD dcl) = ppr dcl
169 ppr (ValD binds) = ppr binds
170 ppr (DefD def) = ppr def
171 ppr (InstD inst) = ppr inst
172 ppr (DerivD deriv) = ppr deriv
173 ppr (ForD fd) = ppr fd
174 ppr (SigD sd) = ppr sd
175 ppr (RuleD rd) = ppr rd
176 ppr (DeprecD dd) = ppr dd
177 ppr (SpliceD dd) = ppr dd
178 ppr (DocD doc) = ppr doc
180 instance OutputableBndr name => Outputable (HsGroup name) where
181 ppr (HsGroup { hs_valds = val_decls,
182 hs_tyclds = tycl_decls,
183 hs_instds = inst_decls,
184 hs_derivds = deriv_decls,
185 hs_fixds = fix_decls,
186 hs_depds = deprec_decls,
187 hs_fords = foreign_decls,
188 hs_defds = default_decls,
189 hs_ruleds = rule_decls })
190 = vcat [ppr_ds fix_decls, ppr_ds default_decls,
191 ppr_ds deprec_decls, ppr_ds rule_decls,
193 ppr_ds tycl_decls, ppr_ds inst_decls,
195 ppr_ds foreign_decls]
198 ppr_ds ds = text "" $$ vcat (map ppr ds)
200 data SpliceDecl id = SpliceDecl (Located (HsExpr id)) -- Top level splice
202 instance OutputableBndr name => Outputable (SpliceDecl name) where
203 ppr (SpliceDecl e) = ptext SLIT("$") <> parens (pprExpr (unLoc e))
207 %************************************************************************
209 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
211 %************************************************************************
213 --------------------------------
215 --------------------------------
217 Here is the story about the implicit names that go with type, class,
218 and instance decls. It's a bit tricky, so pay attention!
220 "Implicit" (or "system") binders
221 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222 Each data type decl defines
223 a worker name for each constructor
224 to-T and from-T convertors
225 Each class decl defines
226 a tycon for the class
227 a data constructor for that tycon
228 the worker for that constructor
229 a selector for each superclass
231 All have occurrence names that are derived uniquely from their parent
234 None of these get separate definitions in an interface file; they are
235 fully defined by the data or class decl. But they may *occur* in
236 interface files, of course. Any such occurrence must haul in the
237 relevant type or class decl.
240 - Ensure they "point to" the parent data/class decl
241 when loading that decl from an interface file
242 (See RnHiFiles.getSysBinders)
244 - When typechecking the decl, we build the implicit TyCons and Ids.
245 When doing so we look them up in the name cache (RnEnv.lookupSysName),
246 to ensure correct module and provenance is set
248 These are the two places that we have to conjure up the magic derived
249 names. (The actual magic is in OccName.mkWorkerOcc, etc.)
253 - Occurrence name is derived uniquely from the method name
256 - If there is a default method name at all, it's recorded in
257 the ClassOpSig (in HsBinds), in the DefMeth field.
258 (DefMeth is defined in Class.lhs)
260 Source-code class decls and interface-code class decls are treated subtly
261 differently, which has given me a great deal of confusion over the years.
262 Here's the deal. (We distinguish the two cases because source-code decls
263 have (Just binds) in the tcdMeths field, whereas interface decls have Nothing.
265 In *source-code* class declarations:
267 - When parsing, every ClassOpSig gets a DefMeth with a suitable RdrName
268 This is done by RdrHsSyn.mkClassOpSigDM
270 - The renamer renames it to a Name
272 - During typechecking, we generate a binding for each $dm for
273 which there's a programmer-supplied default method:
278 We generate a binding for $dmop1 but not for $dmop2.
279 The Class for Foo has a NoDefMeth for op2 and a DefMeth for op1.
280 The Name for $dmop2 is simply discarded.
282 In *interface-file* class declarations:
283 - When parsing, we see if there's an explicit programmer-supplied default method
284 because there's an '=' sign to indicate it:
286 op1 = :: <type> -- NB the '='
288 We use this info to generate a DefMeth with a suitable RdrName for op1,
289 and a NoDefMeth for op2
290 - The interface file has a separate definition for $dmop1, with unfolding etc.
291 - The renamer renames it to a Name.
292 - The renamer treats $dmop1 as a free variable of the declaration, so that
293 the binding for $dmop1 will be sucked in. (See RnHsSyn.tyClDeclFVs)
294 This doesn't happen for source code class decls, because they *bind* the default method.
298 Each instance declaration gives rise to one dictionary function binding.
300 The type checker makes up new source-code instance declarations
301 (e.g. from 'deriving' or generic default methods --- see
302 TcInstDcls.tcInstDecls1). So we can't generate the names for
303 dictionary functions in advance (we don't know how many we need).
305 On the other hand for interface-file instance declarations, the decl
306 specifies the name of the dictionary function, and it has a binding elsewhere
307 in the interface file:
308 instance {Eq Int} = dEqInt
309 dEqInt :: {Eq Int} <pragma info>
311 So again we treat source code and interface file code slightly differently.
314 - Source code instance decls have a Nothing in the (Maybe name) field
315 (see data InstDecl below)
317 - The typechecker makes up a Local name for the dict fun for any source-code
318 instance decl, whether it comes from a source-code instance decl, or whether
319 the instance decl is derived from some other construct (e.g. 'deriving').
321 - The occurrence name it chooses is derived from the instance decl (just for
322 documentation really) --- e.g. dNumInt. Two dict funs may share a common
323 occurrence name, but will have different uniques. E.g.
324 instance Foo [Int] where ...
325 instance Foo [Bool] where ...
326 These might both be dFooList
328 - The CoreTidy phase externalises the name, and ensures the occurrence name is
329 unique (this isn't special to dict funs). So we'd get dFooList and dFooList1.
331 - We can take this relaxed approach (changing the occurrence name later)
332 because dict fun Ids are not captured in a TyCon or Class (unlike default
333 methods, say). Instead, they are kept separately in the InstEnv. This
334 makes it easy to adjust them after compiling a module. (Once we've finished
335 compiling that module, they don't change any more.)
339 - The instance decl gives the dict fun name, so the InstDecl has a (Just name)
340 in the (Maybe name) field.
342 - RnHsSyn.instDeclFVs treats the dict fun name as free in the decl, so that we
343 suck in the dfun binding
347 -- TyClDecls are precisely the kind of declarations that can
348 -- appear in interface files; or (internally) in GHC's interface
349 -- for a module. That's why (despite the misnomer) IfaceSig and ForeignType
350 -- are both in TyClDecl
352 -- Representation of indexed types
353 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
354 -- Kind signatures of indexed types come in two flavours:
356 -- * kind signatures for type functions: variant `TyFunction' and
358 -- * kind signatures for indexed data types and newtypes : variant `TyData'
359 -- iff a kind is present in `tcdKindSig' and there are no constructors in
362 -- Indexed types are represented by 'TyData' and 'TySynonym' using the field
363 -- 'tcdTyPats::Maybe [LHsType name]', with the following meaning:
365 -- * If it is 'Nothing', we have a *vanilla* data type declaration or type
366 -- synonym declaration and 'tcdVars' contains the type parameters of the
369 -- * If it is 'Just pats', we have the definition of an indexed type Then,
370 -- 'pats' are type patterns for the type-indexes of the type constructor
371 -- and 'tcdVars' are the variables in those patterns. Hence, the arity of
372 -- the indexed type (ie, the number of indexes) is 'length tcdTyPats' and
373 -- *not* 'length tcdVars'.
375 -- In both cases, 'tcdVars' collects all variables we need to quantify over.
377 type LTyClDecl name = Located (TyClDecl name)
381 tcdLName :: Located name,
382 tcdExtName :: Maybe FastString,
386 | TyData { tcdND :: NewOrData,
387 tcdCtxt :: LHsContext name, -- Context
388 tcdLName :: Located name, -- Type constructor
389 tcdTyVars :: [LHsTyVarBndr name], -- Type variables
390 tcdTyPats :: Maybe [LHsType name], -- Type patterns
391 tcdKindSig:: Maybe Kind, -- Optional kind sig;
394 -- indexed type sigs)
396 tcdCons :: [LConDecl name], -- Data constructors
397 -- For data T a = T1 | T2 a the LConDecls all have ResTyH98
398 -- For data T a where { T1 :: T a } the LConDecls all have ResTyGADT
400 tcdDerivs :: Maybe [LHsType name]
401 -- Derivings; Nothing => not specified
402 -- Just [] => derive exactly what is asked
403 -- These "types" must be of form
404 -- forall ab. C ty1 ty2
405 -- Typically the foralls and ty args are empty, but they
406 -- are non-empty for the newtype-deriving case
409 | TyFunction {tcdLName :: Located name, -- type constructor
410 tcdTyVars :: [LHsTyVarBndr name], -- type variables
411 tcdIso :: Bool, -- injective type?
412 tcdKind :: Kind -- result kind
415 | TySynonym { tcdLName :: Located name, -- type constructor
416 tcdTyVars :: [LHsTyVarBndr name], -- type variables
417 tcdTyPats :: Maybe [LHsType name], -- Type patterns
418 -- 'Nothing' => vanilla
420 tcdSynRhs :: LHsType name -- synonym expansion
423 | ClassDecl { tcdCtxt :: LHsContext name, -- Context...
424 tcdLName :: Located name, -- Name of the class
425 tcdTyVars :: [LHsTyVarBndr name], -- Class type variables
426 tcdFDs :: [Located (FunDep name)], -- Functional deps
427 tcdSigs :: [LSig name], -- Methods' signatures
428 tcdMeths :: LHsBinds name, -- Default methods
429 tcdATs :: [LTyClDecl name], -- Associated types; ie
433 tcdDocs :: [DocEntity name] -- Haddock docs
437 = NewType -- "newtype Blah ..."
438 | DataType -- "data Blah ..."
439 deriving( Eq ) -- Needed because Demand derives Eq
445 isTFunDecl, isDataDecl, isSynDecl, isClassDecl, isKindSigDecl, isIdxTyDecl ::
446 TyClDecl name -> Bool
448 -- type function kind signature
449 isTFunDecl (TyFunction {}) = True
450 isTFunDecl other = False
452 -- vanilla Haskell type synonym
453 isSynDecl (TySynonym {tcdTyPats = Nothing}) = True
454 isSynDecl other = False
456 -- type equation (of a type function)
457 isTEqnDecl (TySynonym {tcdTyPats = Just _}) = True
458 isTEqnDecl other = False
460 isDataDecl (TyData {}) = True
461 isDataDecl other = False
463 isClassDecl (ClassDecl {}) = True
464 isClassDecl other = False
466 -- kind signature (for an indexed type)
467 isKindSigDecl (TyFunction {} ) = True
468 isKindSigDecl (TyData {tcdKindSig = Just _,
469 tcdCons = [] }) = True
470 isKindSigDecl other = False
472 -- definition of an instance of an indexed type
474 | isTEqnDecl tydecl = True
475 | isDataDecl tydecl = isJust (tcdTyPats tydecl)
482 tcdName :: TyClDecl name -> name
483 tcdName decl = unLoc (tcdLName decl)
485 tyClDeclNames :: Eq name => TyClDecl name -> [Located name]
486 -- Returns all the *binding* names of the decl, along with their SrcLocs
487 -- The first one is guaranteed to be the name of the decl
488 -- For record fields, the first one counts as the SrcLoc
489 -- We use the equality to filter out duplicate field names
491 tyClDeclNames (TyFunction {tcdLName = name}) = [name]
492 tyClDeclNames (TySynonym {tcdLName = name}) = [name]
493 tyClDeclNames (ForeignType {tcdLName = name}) = [name]
495 tyClDeclNames (ClassDecl {tcdLName = cls_name, tcdSigs = sigs, tcdATs = ats})
497 concatMap (tyClDeclNames . unLoc) ats ++ [n | L _ (TypeSig n _) <- sigs]
499 tyClDeclNames (TyData {tcdLName = tc_name, tcdCons = cons})
500 = tc_name : conDeclsNames (map unLoc cons)
502 tyClDeclTyVars (TyFunction {tcdTyVars = tvs}) = tvs
503 tyClDeclTyVars (TySynonym {tcdTyVars = tvs}) = tvs
504 tyClDeclTyVars (TyData {tcdTyVars = tvs}) = tvs
505 tyClDeclTyVars (ClassDecl {tcdTyVars = tvs}) = tvs
506 tyClDeclTyVars (ForeignType {}) = []
510 countTyClDecls :: [TyClDecl name] -> (Int, Int, Int, Int, Int, Int)
511 -- class, synonym decls, type function signatures,
512 -- type function equations, data, newtype
514 = (count isClassDecl decls,
515 count isSynDecl decls,
516 count isTFunDecl decls,
517 count isTEqnDecl decls,
518 count isDataTy decls,
521 isDataTy TyData{tcdND=DataType} = True
524 isNewTy TyData{tcdND=NewType} = True
529 instance OutputableBndr name
530 => Outputable (TyClDecl name) where
532 ppr (ForeignType {tcdLName = ltycon})
533 = hsep [ptext SLIT("foreign import type dotnet"), ppr ltycon]
535 ppr (TyFunction {tcdLName = ltycon, tcdTyVars = tyvars, tcdIso = iso,
537 = typeMaybeIso <+> pp_decl_head [] ltycon tyvars Nothing <+>
538 dcolon <+> pprKind kind
540 typeMaybeIso = if iso
541 then ptext SLIT("type family iso")
542 else ptext SLIT("type family")
544 ppr (TySynonym {tcdLName = ltycon, tcdTyVars = tyvars, tcdTyPats = typats,
545 tcdSynRhs = mono_ty})
546 = hang (ptext SLIT("type") <+>
547 (if isJust typats then ptext SLIT("instance") else empty) <+>
548 pp_decl_head [] ltycon tyvars typats <+>
552 ppr (TyData {tcdND = new_or_data, tcdCtxt = context, tcdLName = ltycon,
553 tcdTyVars = tyvars, tcdTyPats = typats, tcdKindSig = mb_sig,
554 tcdCons = condecls, tcdDerivs = derivings})
555 = pp_tydecl (null condecls && isJust mb_sig)
557 (if isJust typats then ptext SLIT("instance") else empty) <+>
558 pp_decl_head (unLoc context) ltycon tyvars typats <+>
560 (pp_condecls condecls)
563 ppr_sig Nothing = empty
564 ppr_sig (Just kind) = dcolon <+> pprKind kind
566 ppr (ClassDecl {tcdCtxt = context, tcdLName = lclas, tcdTyVars = tyvars,
568 tcdSigs = sigs, tcdMeths = methods, tcdATs = ats})
569 | null sigs && null ats -- No "where" part
572 | otherwise -- Laid out
573 = sep [hsep [top_matter, ptext SLIT("where {")],
574 nest 4 (sep [ sep (map ppr_semi ats)
575 , sep (map ppr_semi sigs)
576 , pprLHsBinds methods
579 top_matter = ptext SLIT("class")
580 <+> pp_decl_head (unLoc context) lclas tyvars Nothing
581 <+> pprFundeps (map unLoc fds)
582 ppr_semi decl = ppr decl <> semi
584 pp_decl_head :: OutputableBndr name
587 -> [LHsTyVarBndr name]
588 -> Maybe [LHsType name]
590 pp_decl_head context thing tyvars Nothing -- no explicit type patterns
591 = hsep [pprHsContext context, ppr thing, interppSP tyvars]
592 pp_decl_head context thing _ (Just typats) -- explicit type patterns
593 = hsep [ pprHsContext context, ppr thing
594 , hsep (map (pprParendHsType.unLoc) typats)]
596 pp_condecls cs@(L _ ConDecl{ con_res = ResTyGADT _ } : _) -- In GADT syntax
597 = hang (ptext SLIT("where")) 2 (vcat (map ppr cs))
598 pp_condecls cs -- In H98 syntax
599 = equals <+> sep (punctuate (ptext SLIT(" |")) (map ppr cs))
601 pp_tydecl True pp_head pp_decl_rhs derivings
603 pp_tydecl False pp_head pp_decl_rhs derivings
604 = hang pp_head 4 (sep [
608 Just ds -> hsep [ptext SLIT("deriving"), parens (interpp'SP ds)]
611 instance Outputable NewOrData where
612 ppr NewType = ptext SLIT("newtype")
613 ppr DataType = ptext SLIT("data")
617 %************************************************************************
619 \subsection[ConDecl]{A data-constructor declaration}
621 %************************************************************************
624 type LConDecl name = Located (ConDecl name)
626 -- data T b = forall a. Eq a => MkT a b
627 -- MkT :: forall b a. Eq a => MkT a b
630 -- MkT1 :: Int -> T Int
632 -- data T = Int `MkT` Int
636 -- Int `MkT` Int :: T Int
640 { con_name :: Located name -- Constructor name; this is used for the
641 -- DataCon itself, and for the user-callable wrapper Id
643 , con_explicit :: HsExplicitForAll -- Is there an user-written forall? (cf. HStypes.HsForAllTy)
645 , con_qvars :: [LHsTyVarBndr name] -- ResTyH98: the constructor's existential type variables
646 -- ResTyGADT: all the constructor's quantified type variables
648 , con_cxt :: LHsContext name -- The context. This *does not* include the
649 -- "stupid theta" which lives only in the TyData decl
651 , con_details :: HsConDetails name (LBangType name) -- The main payload
653 , con_res :: ResType name -- Result type of the constructor
655 , con_doc :: Maybe (LHsDoc name) -- A possible Haddock comment
659 = ResTyH98 -- Constructor was declared using Haskell 98 syntax
660 | ResTyGADT (LHsType name) -- Constructor was declared using GADT-style syntax,
661 -- and here is its result type
665 conDeclsNames :: Eq name => [ConDecl name] -> [Located name]
666 -- See tyClDeclNames for what this does
667 -- The function is boringly complicated because of the records
668 -- And since we only have equality, we have to be a little careful
670 = snd (foldl do_one ([], []) cons)
672 do_one (flds_seen, acc) (ConDecl { con_name = lname, con_details = RecCon flds })
673 = (map unLoc new_flds ++ flds_seen, lname : [f | f <- new_flds] ++ acc)
675 new_flds = [ f | (HsRecField f _ _) <- flds, not (unLoc f `elem` flds_seen) ]
677 do_one (flds_seen, acc) c
678 = (flds_seen, (con_name c):acc)
680 conDetailsTys details = map getBangType (hsConArgs details)
685 instance (OutputableBndr name) => Outputable (ConDecl name) where
688 pprConDecl (ConDecl con expl tvs cxt details ResTyH98 doc)
689 = sep [ppr_mbDoc doc, pprHsForAll expl tvs cxt, ppr_details con details]
691 ppr_details con (InfixCon t1 t2) = hsep [ppr t1, pprHsVar con, ppr t2]
692 ppr_details con (PrefixCon tys) = hsep (pprHsVar con : map ppr tys)
693 ppr_details con (RecCon fields) = ppr con <+> ppr_fields fields
695 pprConDecl (ConDecl con expl tvs cxt (PrefixCon arg_tys) (ResTyGADT res_ty) _)
696 = ppr con <+> dcolon <+>
697 sep [pprHsForAll expl tvs cxt, ppr (foldr mk_fun_ty res_ty arg_tys)]
699 mk_fun_ty a b = noLoc (HsFunTy a b)
701 pprConDecl (ConDecl con expl tvs cxt (RecCon fields) (ResTyGADT res_ty) _)
702 = sep [pprHsForAll expl tvs cxt, ppr con <+> ppr_fields fields <+> dcolon <+> ppr res_ty]
704 ppr_fields fields = braces (sep (punctuate comma (map ppr fields)))
707 %************************************************************************
709 \subsection[InstDecl]{An instance declaration
711 %************************************************************************
714 type LInstDecl name = Located (InstDecl name)
717 = InstDecl (LHsType name) -- Context => Class Instance-type
718 -- Using a polytype means that the renamer conveniently
719 -- figures out the quantified type variables for us.
721 [LSig name] -- User-supplied pragmatic info
722 [LTyClDecl name]-- Associated types (ie, 'TyData' and
725 instance (OutputableBndr name) => Outputable (InstDecl name) where
727 ppr (InstDecl inst_ty binds uprags ats)
728 = vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
731 nest 4 (pprLHsBinds binds) ]
733 -- Extract the declarations of associated types from an instance
735 instDeclATs :: InstDecl name -> [LTyClDecl name]
736 instDeclATs (InstDecl _ _ _ ats) = ats
739 %************************************************************************
741 \subsection[DerivDecl]{A stand-alone instance deriving declaration
743 %************************************************************************
746 type LDerivDecl name = Located (DerivDecl name)
749 = DerivDecl (LHsType name) (Located name)
751 instance (OutputableBndr name) => Outputable (DerivDecl name) where
753 = hsep [ptext SLIT("deriving"), ppr ty, ptext SLIT("for"), ppr n]
756 %************************************************************************
758 \subsection[DefaultDecl]{A @default@ declaration}
760 %************************************************************************
762 There can only be one default declaration per module, but it is hard
763 for the parser to check that; we pass them all through in the abstract
764 syntax, and that restriction must be checked in the front end.
767 type LDefaultDecl name = Located (DefaultDecl name)
769 data DefaultDecl name
770 = DefaultDecl [LHsType name]
772 instance (OutputableBndr name)
773 => Outputable (DefaultDecl name) where
775 ppr (DefaultDecl tys)
776 = ptext SLIT("default") <+> parens (interpp'SP tys)
779 %************************************************************************
781 \subsection{Foreign function interface declaration}
783 %************************************************************************
787 -- foreign declarations are distinguished as to whether they define or use a
790 -- * the Boolean value indicates whether the pre-standard deprecated syntax
793 type LForeignDecl name = Located (ForeignDecl name)
795 data ForeignDecl name
796 = ForeignImport (Located name) (LHsType name) ForeignImport -- defines name
797 | ForeignExport (Located name) (LHsType name) ForeignExport -- uses name
799 -- Specification Of an imported external entity in dependence on the calling
802 data ForeignImport = -- import of a C entity
804 -- * the two strings specifying a header file or library
805 -- may be empty, which indicates the absence of a
806 -- header or object specification (both are not used
807 -- in the case of `CWrapper' and when `CFunction'
808 -- has a dynamic target)
810 -- * the calling convention is irrelevant for code
811 -- generation in the case of `CLabel', but is needed
812 -- for pretty printing
814 -- * `Safety' is irrelevant for `CLabel' and `CWrapper'
816 CImport CCallConv -- ccall or stdcall
817 Safety -- safe or unsafe
818 FastString -- name of C header
819 FastString -- name of library object
820 CImportSpec -- details of the C entity
822 -- import of a .NET function
824 | DNImport DNCallSpec
826 -- details of an external C entity
828 data CImportSpec = CLabel CLabelString -- import address of a C label
829 | CFunction CCallTarget -- static or dynamic function
830 | CWrapper -- wrapper to expose closures
833 -- specification of an externally exported entity in dependence on the calling
836 data ForeignExport = CExport CExportSpec -- contains the calling convention
837 | DNExport -- presently unused
839 -- abstract type imported from .NET
841 data FoType = DNType -- In due course we'll add subtype stuff
842 deriving (Eq) -- Used for equality instance for TyClDecl
845 -- pretty printing of foreign declarations
848 instance OutputableBndr name => Outputable (ForeignDecl name) where
849 ppr (ForeignImport n ty fimport) =
850 ptext SLIT("foreign import") <+> ppr fimport <+>
851 ppr n <+> dcolon <+> ppr ty
852 ppr (ForeignExport n ty fexport) =
853 ptext SLIT("foreign export") <+> ppr fexport <+>
854 ppr n <+> dcolon <+> ppr ty
856 instance Outputable ForeignImport where
857 ppr (DNImport spec) =
858 ptext SLIT("dotnet") <+> ppr spec
859 ppr (CImport cconv safety header lib spec) =
860 ppr cconv <+> ppr safety <+>
861 char '"' <> pprCEntity header lib spec <> char '"'
863 pprCEntity header lib (CLabel lbl) =
864 ptext SLIT("static") <+> ftext header <+> char '&' <>
865 pprLib lib <> ppr lbl
866 pprCEntity header lib (CFunction (StaticTarget lbl)) =
867 ptext SLIT("static") <+> ftext header <+> char '&' <>
868 pprLib lib <> ppr lbl
869 pprCEntity header lib (CFunction (DynamicTarget)) =
870 ptext SLIT("dynamic")
871 pprCEntity _ _ (CWrapper) = ptext SLIT("wrapper")
873 pprLib lib | nullFS lib = empty
874 | otherwise = char '[' <> ppr lib <> char ']'
876 instance Outputable ForeignExport where
877 ppr (CExport (CExportStatic lbl cconv)) =
878 ppr cconv <+> char '"' <> ppr lbl <> char '"'
880 ptext SLIT("dotnet") <+> ptext SLIT("\"<unused>\"")
882 instance Outputable FoType where
883 ppr DNType = ptext SLIT("type dotnet")
887 %************************************************************************
889 \subsection{Transformation rules}
891 %************************************************************************
894 type LRuleDecl name = Located (RuleDecl name)
897 = HsRule -- Source rule
898 RuleName -- Rule name
900 [RuleBndr name] -- Forall'd vars; after typechecking this includes tyvars
901 (Located (HsExpr name)) -- LHS
902 NameSet -- Free-vars from the LHS
903 (Located (HsExpr name)) -- RHS
904 NameSet -- Free-vars from the RHS
907 = RuleBndr (Located name)
908 | RuleBndrSig (Located name) (LHsType name)
910 collectRuleBndrSigTys :: [RuleBndr name] -> [LHsType name]
911 collectRuleBndrSigTys bndrs = [ty | RuleBndrSig _ ty <- bndrs]
913 instance OutputableBndr name => Outputable (RuleDecl name) where
914 ppr (HsRule name act ns lhs fv_lhs rhs fv_rhs)
915 = sep [text "{-# RULES" <+> doubleQuotes (ftext name) <+> ppr act,
916 nest 4 (pp_forall <+> pprExpr (unLoc lhs)),
917 nest 4 (equals <+> pprExpr (unLoc rhs) <+> text "#-}") ]
919 pp_forall | null ns = empty
920 | otherwise = text "forall" <+> fsep (map ppr ns) <> dot
922 instance OutputableBndr name => Outputable (RuleBndr name) where
923 ppr (RuleBndr name) = ppr name
924 ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty
927 %************************************************************************
929 \subsection[DocDecl]{Document comments}
931 %************************************************************************
935 -- source code entities, for representing the module structure
938 | DocEntity (DocDecl name)
940 type LDocDecl name = Located (DocDecl name)
943 = DocCommentNext (HsDoc name)
944 | DocCommentPrev (HsDoc name)
945 | DocCommentNamed String (HsDoc name)
946 | DocGroup Int (HsDoc name)
948 -- Okay, I need to reconstruct the document comments, but for now:
949 instance Outputable (DocDecl name) where
950 ppr _ = text "<document comment>"
952 docDeclDoc (DocCommentNext d) = d
953 docDeclDoc (DocCommentPrev d) = d
954 docDeclDoc (DocCommentNamed _ d) = d
955 docDeclDoc (DocGroup _ d) = d
959 %************************************************************************
961 \subsection[DeprecDecl]{Deprecations}
963 %************************************************************************
965 We use exported entities for things to deprecate.
968 type LDeprecDecl name = Located (DeprecDecl name)
970 data DeprecDecl name = Deprecation name DeprecTxt
972 instance OutputableBndr name => Outputable (DeprecDecl name) where
973 ppr (Deprecation thing txt)
974 = hsep [text "{-# DEPRECATED", ppr thing, doubleQuotes (ppr txt), text "#-}"]