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(..), LHsDecl, TyClDecl(..), LTyClDecl,
12 InstDecl(..), LInstDecl, NewOrData(..),
13 RuleDecl(..), LRuleDecl, RuleBndr(..),
14 DefaultDecl(..), LDefaultDecl, SpliceDecl(..),
15 ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..),
16 CImportSpec(..), FoType(..),
17 ConDecl(..), ResType(..), LConDecl,
18 DeprecDecl(..), LDeprecDecl,
19 HsGroup(..), emptyRdrGroup, emptyRnGroup, appendGroups,
20 tcdName, tyClDeclNames, tyClDeclTyVars,
21 isClassDecl, isTFunDecl, isSynDecl, isTEqnDecl, isDataDecl,
25 collectRuleBndrSigTys,
28 #include "HsVersions.h"
31 import {-# SOURCE #-} HsExpr( HsExpr, pprExpr )
32 -- Because Expr imports Decls via HsBracket
34 import HsBinds ( HsValBinds(..), HsBind, LHsBinds, plusHsValBinds,
35 Sig(..), LSig, LFixitySig, pprLHsBinds,
36 emptyValBindsIn, emptyValBindsOut )
37 import HsPat ( HsConDetails(..), hsConArgs )
38 import HsImpExp ( pprHsVar )
40 import NameSet ( NameSet )
41 import HscTypes ( DeprecTxt )
42 import CoreSyn ( RuleName )
43 import Kind ( Kind, pprKind )
44 import BasicTypes ( Activation(..) )
45 import ForeignCall ( CCallTarget(..), DNCallSpec, CCallConv, Safety,
46 CExportSpec(..), CLabelString )
49 import FunDeps ( pprFundeps )
50 import Class ( FunDep )
53 import SrcLoc ( Located(..), unLoc, noLoc )
58 %************************************************************************
60 \subsection[HsDecl]{Declarations}
62 %************************************************************************
65 type LHsDecl id = Located (HsDecl id)
72 | DefD (DefaultDecl id)
73 | ForD (ForeignDecl id)
74 | DeprecD (DeprecDecl id)
76 | SpliceD (SpliceDecl id)
78 -- NB: all top-level fixity decls are contained EITHER
80 -- OR in the ClassDecls in TyClDs
83 -- a) data constructors
84 -- b) class methods (but they can be also done in the
85 -- signatures of class decls)
86 -- c) imported functions (that have an IfacSig)
89 -- The latter is for class methods only
91 -- A [HsDecl] is categorised into a HsGroup before being
92 -- fed to the renamer.
95 hs_valds :: HsValBinds id,
96 hs_tyclds :: [LTyClDecl id],
97 hs_instds :: [LInstDecl id],
99 hs_fixds :: [LFixitySig id],
100 -- Snaffled out of both top-level fixity signatures,
101 -- and those in class declarations
103 hs_defds :: [LDefaultDecl id],
104 hs_fords :: [LForeignDecl id],
105 hs_depds :: [LDeprecDecl id],
106 hs_ruleds :: [LRuleDecl id]
109 emptyGroup, emptyRdrGroup, emptyRnGroup :: HsGroup a
110 emptyRdrGroup = emptyGroup { hs_valds = emptyValBindsIn }
111 emptyRnGroup = emptyGroup { hs_valds = emptyValBindsOut }
113 emptyGroup = HsGroup { hs_tyclds = [], hs_instds = [],
114 hs_fixds = [], hs_defds = [], hs_fords = [],
115 hs_depds = [], hs_ruleds = [],
116 hs_valds = error "emptyGroup hs_valds: Can't happen" }
118 appendGroups :: HsGroup a -> HsGroup a -> HsGroup a
121 hs_valds = val_groups1,
130 hs_valds = val_groups2,
140 hs_valds = val_groups1 `plusHsValBinds` val_groups2,
141 hs_tyclds = tyclds1 ++ tyclds2,
142 hs_instds = instds1 ++ instds2,
143 hs_fixds = fixds1 ++ fixds2,
144 hs_defds = defds1 ++ defds2,
145 hs_fords = fords1 ++ fords2,
146 hs_depds = depds1 ++ depds2,
147 hs_ruleds = rulds1 ++ rulds2 }
151 instance OutputableBndr name => Outputable (HsDecl name) where
152 ppr (TyClD dcl) = ppr dcl
153 ppr (ValD binds) = ppr binds
154 ppr (DefD def) = ppr def
155 ppr (InstD inst) = ppr inst
156 ppr (ForD fd) = ppr fd
157 ppr (SigD sd) = ppr sd
158 ppr (RuleD rd) = ppr rd
159 ppr (DeprecD dd) = ppr dd
160 ppr (SpliceD dd) = ppr dd
162 instance OutputableBndr name => Outputable (HsGroup name) where
163 ppr (HsGroup { hs_valds = val_decls,
164 hs_tyclds = tycl_decls,
165 hs_instds = inst_decls,
166 hs_fixds = fix_decls,
167 hs_depds = deprec_decls,
168 hs_fords = foreign_decls,
169 hs_defds = default_decls,
170 hs_ruleds = rule_decls })
171 = vcat [ppr_ds fix_decls, ppr_ds default_decls,
172 ppr_ds deprec_decls, ppr_ds rule_decls,
174 ppr_ds tycl_decls, ppr_ds inst_decls,
175 ppr_ds foreign_decls]
178 ppr_ds ds = text "" $$ vcat (map ppr ds)
180 data SpliceDecl id = SpliceDecl (Located (HsExpr id)) -- Top level splice
182 instance OutputableBndr name => Outputable (SpliceDecl name) where
183 ppr (SpliceDecl e) = ptext SLIT("$") <> parens (pprExpr (unLoc e))
187 %************************************************************************
189 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
191 %************************************************************************
193 --------------------------------
195 --------------------------------
197 Here is the story about the implicit names that go with type, class,
198 and instance decls. It's a bit tricky, so pay attention!
200 "Implicit" (or "system") binders
201 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
202 Each data type decl defines
203 a worker name for each constructor
204 to-T and from-T convertors
205 Each class decl defines
206 a tycon for the class
207 a data constructor for that tycon
208 the worker for that constructor
209 a selector for each superclass
211 All have occurrence names that are derived uniquely from their parent
214 None of these get separate definitions in an interface file; they are
215 fully defined by the data or class decl. But they may *occur* in
216 interface files, of course. Any such occurrence must haul in the
217 relevant type or class decl.
220 - Ensure they "point to" the parent data/class decl
221 when loading that decl from an interface file
222 (See RnHiFiles.getSysBinders)
224 - When typechecking the decl, we build the implicit TyCons and Ids.
225 When doing so we look them up in the name cache (RnEnv.lookupSysName),
226 to ensure correct module and provenance is set
228 These are the two places that we have to conjure up the magic derived
229 names. (The actual magic is in OccName.mkWorkerOcc, etc.)
233 - Occurrence name is derived uniquely from the method name
236 - If there is a default method name at all, it's recorded in
237 the ClassOpSig (in HsBinds), in the DefMeth field.
238 (DefMeth is defined in Class.lhs)
240 Source-code class decls and interface-code class decls are treated subtly
241 differently, which has given me a great deal of confusion over the years.
242 Here's the deal. (We distinguish the two cases because source-code decls
243 have (Just binds) in the tcdMeths field, whereas interface decls have Nothing.
245 In *source-code* class declarations:
247 - When parsing, every ClassOpSig gets a DefMeth with a suitable RdrName
248 This is done by RdrHsSyn.mkClassOpSigDM
250 - The renamer renames it to a Name
252 - During typechecking, we generate a binding for each $dm for
253 which there's a programmer-supplied default method:
258 We generate a binding for $dmop1 but not for $dmop2.
259 The Class for Foo has a NoDefMeth for op2 and a DefMeth for op1.
260 The Name for $dmop2 is simply discarded.
262 In *interface-file* class declarations:
263 - When parsing, we see if there's an explicit programmer-supplied default method
264 because there's an '=' sign to indicate it:
266 op1 = :: <type> -- NB the '='
268 We use this info to generate a DefMeth with a suitable RdrName for op1,
269 and a NoDefMeth for op2
270 - The interface file has a separate definition for $dmop1, with unfolding etc.
271 - The renamer renames it to a Name.
272 - The renamer treats $dmop1 as a free variable of the declaration, so that
273 the binding for $dmop1 will be sucked in. (See RnHsSyn.tyClDeclFVs)
274 This doesn't happen for source code class decls, because they *bind* the default method.
278 Each instance declaration gives rise to one dictionary function binding.
280 The type checker makes up new source-code instance declarations
281 (e.g. from 'deriving' or generic default methods --- see
282 TcInstDcls.tcInstDecls1). So we can't generate the names for
283 dictionary functions in advance (we don't know how many we need).
285 On the other hand for interface-file instance declarations, the decl
286 specifies the name of the dictionary function, and it has a binding elsewhere
287 in the interface file:
288 instance {Eq Int} = dEqInt
289 dEqInt :: {Eq Int} <pragma info>
291 So again we treat source code and interface file code slightly differently.
294 - Source code instance decls have a Nothing in the (Maybe name) field
295 (see data InstDecl below)
297 - The typechecker makes up a Local name for the dict fun for any source-code
298 instance decl, whether it comes from a source-code instance decl, or whether
299 the instance decl is derived from some other construct (e.g. 'deriving').
301 - The occurrence name it chooses is derived from the instance decl (just for
302 documentation really) --- e.g. dNumInt. Two dict funs may share a common
303 occurrence name, but will have different uniques. E.g.
304 instance Foo [Int] where ...
305 instance Foo [Bool] where ...
306 These might both be dFooList
308 - The CoreTidy phase externalises the name, and ensures the occurrence name is
309 unique (this isn't special to dict funs). So we'd get dFooList and dFooList1.
311 - We can take this relaxed approach (changing the occurrence name later)
312 because dict fun Ids are not captured in a TyCon or Class (unlike default
313 methods, say). Instead, they are kept separately in the InstEnv. This
314 makes it easy to adjust them after compiling a module. (Once we've finished
315 compiling that module, they don't change any more.)
319 - The instance decl gives the dict fun name, so the InstDecl has a (Just name)
320 in the (Maybe name) field.
322 - RnHsSyn.instDeclFVs treats the dict fun name as free in the decl, so that we
323 suck in the dfun binding
327 -- TyClDecls are precisely the kind of declarations that can
328 -- appear in interface files; or (internally) in GHC's interface
329 -- for a module. That's why (despite the misnomer) IfaceSig and ForeignType
330 -- are both in TyClDecl
332 -- Representation of type functions and associated data types & synonyms
333 -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
334 -- 'TyData' and 'TySynonym' have a field 'tcdPats::Maybe [LHsType name]', with
335 -- the following meaning:
337 -- * If it is 'Nothing', we have a *vanilla* data type declaration or type
338 -- synonym declaration and 'tcdVars' contains the type parameters of the
341 -- * If it is 'Just pats', we have the definition of an associated data type
342 -- or a type function equations (toplevel or nested in an instance
343 -- declarations). Then, 'pats' are type patterns for the type-indexes of
344 -- the type constructor and 'tcdVars' are the variables in those
345 -- patterns. Hence, the arity of the type constructor is 'length tcdPats'
346 -- and *not* 'length tcdVars'.
348 -- In both cases, 'tcdVars' collects all variables we need to quantify over.
350 type LTyClDecl name = Located (TyClDecl name)
354 tcdLName :: Located name,
355 tcdExtName :: Maybe FastString,
359 | TyData { tcdND :: NewOrData,
360 tcdCtxt :: LHsContext name, -- Context
361 tcdLName :: Located name, -- Type constructor
362 tcdTyVars :: [LHsTyVarBndr name], -- Type variables
363 tcdTyPats :: Maybe [LHsType name], -- Type patterns
364 tcdKindSig:: Maybe Kind, -- Optional kind sig;
367 -- indexed type sigs)
369 tcdCons :: [LConDecl name], -- Data constructors
370 -- For data T a = T1 | T2 a the LConDecls all have ResTyH98
371 -- For data T a where { T1 :: T a } the LConDecls all have ResTyGADT
373 tcdDerivs :: Maybe [LHsType name]
374 -- Derivings; Nothing => not specified
375 -- Just [] => derive exactly what is asked
376 -- These "types" must be of form
377 -- forall ab. C ty1 ty2
378 -- Typically the foralls and ty args are empty, but they
379 -- are non-empty for the newtype-deriving case
382 | TyFunction {tcdLName :: Located name, -- type constructor
383 tcdTyVars :: [LHsTyVarBndr name], -- type variables
384 tcdIso :: Bool, -- injective type?
385 tcdKind :: Kind -- result kind
388 | TySynonym { tcdLName :: Located name, -- type constructor
389 tcdTyVars :: [LHsTyVarBndr name], -- type variables
390 tcdTyPats :: Maybe [LHsType name], -- Type patterns
391 -- 'Nothing' => vanilla
393 tcdSynRhs :: LHsType name -- synonym expansion
396 | ClassDecl { tcdCtxt :: LHsContext name, -- Context...
397 tcdLName :: Located name, -- Name of the class
398 tcdTyVars :: [LHsTyVarBndr name], -- Class type variables
399 tcdFDs :: [Located (FunDep name)], -- Functional deps
400 tcdSigs :: [LSig name], -- Methods' signatures
401 tcdMeths :: LHsBinds name, -- Default methods
402 tcdATs :: [LTyClDecl name] -- Associated types; ie
409 = NewType -- "newtype Blah ..."
410 | DataType -- "data Blah ..."
411 deriving( Eq ) -- Needed because Demand derives Eq
417 isTFunDecl, isDataDecl, isSynDecl, isTEqnDecl, isClassDecl ::
418 TyClDecl name -> Bool
420 -- type function kind signature
421 isTFunDecl (TyFunction {}) = True
422 isTFunDecl other = False
424 -- vanilla Haskell type synonym
425 isSynDecl (TySynonym {tcdTyPats = Nothing}) = True
426 isSynDecl other = False
428 -- type equation (of a type function)
429 isTEqnDecl (TySynonym {tcdTyPats = Just _}) = True
430 isTEqnDecl other = False
432 isDataDecl (TyData {}) = True
433 isDataDecl other = False
435 isClassDecl (ClassDecl {}) = True
436 isClassDecl other = False
442 tcdName :: TyClDecl name -> name
443 tcdName decl = unLoc (tcdLName decl)
445 tyClDeclNames :: Eq name => TyClDecl name -> [Located name]
446 -- Returns all the *binding* names of the decl, along with their SrcLocs
447 -- The first one is guaranteed to be the name of the decl
448 -- For record fields, the first one counts as the SrcLoc
449 -- We use the equality to filter out duplicate field names
451 tyClDeclNames (TyFunction {tcdLName = name}) = [name]
452 tyClDeclNames (TySynonym {tcdLName = name,
453 tcdTyPats= Nothing}) = [name]
454 tyClDeclNames (TySynonym {} ) = [] -- type equation
455 tyClDeclNames (ForeignType {tcdLName = name}) = [name]
457 tyClDeclNames (ClassDecl {tcdLName = cls_name, tcdSigs = sigs, tcdATs = ats})
459 concatMap (tyClDeclNames . unLoc) ats ++ [n | L _ (TypeSig n _) <- sigs]
461 tyClDeclNames (TyData {tcdLName = tc_name, tcdCons = cons})
462 = tc_name : conDeclsNames (map unLoc cons)
464 tyClDeclTyVars (TyFunction {tcdTyVars = tvs}) = tvs
465 tyClDeclTyVars (TySynonym {tcdTyVars = tvs}) = tvs
466 tyClDeclTyVars (TyData {tcdTyVars = tvs}) = tvs
467 tyClDeclTyVars (ClassDecl {tcdTyVars = tvs}) = tvs
468 tyClDeclTyVars (ForeignType {}) = []
472 countTyClDecls :: [TyClDecl name] -> (Int, Int, Int, Int, Int, Int)
473 -- class, synonym decls, type function signatures,
474 -- type function equations, data, newtype
476 = (count isClassDecl decls,
477 count isSynDecl decls,
478 count isTFunDecl decls,
479 count isTEqnDecl decls,
480 count isDataTy decls,
483 isDataTy TyData{tcdND=DataType} = True
486 isNewTy TyData{tcdND=NewType} = True
491 instance OutputableBndr name
492 => Outputable (TyClDecl name) where
494 ppr (ForeignType {tcdLName = ltycon})
495 = hsep [ptext SLIT("foreign import type dotnet"), ppr ltycon]
497 ppr (TyFunction {tcdLName = ltycon, tcdTyVars = tyvars, tcdIso = iso,
499 = typeMaybeIso <+> pp_decl_head [] ltycon tyvars Nothing <+>
500 dcolon <+> pprKind kind
502 typeMaybeIso = if iso
503 then ptext SLIT("type iso")
504 else ptext SLIT("type")
506 ppr (TySynonym {tcdLName = ltycon, tcdTyVars = tyvars, tcdTyPats = typats,
507 tcdSynRhs = mono_ty})
508 = hang (ptext SLIT("type") <+> pp_decl_head [] ltycon tyvars typats <+>
512 ppr (TyData {tcdND = new_or_data, tcdCtxt = context, tcdLName = ltycon,
513 tcdTyVars = tyvars, tcdTyPats = typats, tcdKindSig = mb_sig,
514 tcdCons = condecls, tcdDerivs = derivings})
515 = pp_tydecl (ppr new_or_data <+>
516 pp_decl_head (unLoc context) ltycon tyvars typats <+>
518 (pp_condecls condecls)
521 ppr_sig Nothing = empty
522 ppr_sig (Just kind) = dcolon <+> pprKind kind
524 ppr (ClassDecl {tcdCtxt = context, tcdLName = lclas, tcdTyVars = tyvars,
526 tcdSigs = sigs, tcdMeths = methods, tcdATs = ats})
527 | null sigs && null ats -- No "where" part
530 | otherwise -- Laid out
531 = sep [hsep [top_matter, ptext SLIT("where {")],
532 nest 4 (sep [ sep (map ppr_semi ats)
533 , sep (map ppr_semi sigs)
534 , pprLHsBinds methods
537 top_matter = ptext SLIT("class")
538 <+> pp_decl_head (unLoc context) lclas tyvars Nothing
539 <+> pprFundeps (map unLoc fds)
540 ppr_semi decl = ppr decl <> semi
542 pp_decl_head :: OutputableBndr name
545 -> [LHsTyVarBndr name]
546 -> Maybe [LHsType name]
548 pp_decl_head context thing tyvars Nothing -- no explicit type patterns
549 = hsep [pprHsContext context, ppr thing, interppSP tyvars]
550 pp_decl_head context thing _ (Just typats) -- explicit type patterns
551 = hsep [ pprHsContext context, ppr thing
552 , hsep (map (pprParendHsType.unLoc) typats)]
554 pp_condecls cs@(L _ ConDecl{ con_res = ResTyGADT _ } : _) -- In GADT syntax
555 = hang (ptext SLIT("where")) 2 (vcat (map ppr cs))
556 pp_condecls cs -- In H98 syntax
557 = equals <+> sep (punctuate (ptext SLIT(" |")) (map ppr cs))
559 pp_tydecl pp_head pp_decl_rhs derivings
560 = hang pp_head 4 (sep [
564 Just ds -> hsep [ptext SLIT("deriving"), parens (interpp'SP ds)]
567 instance Outputable NewOrData where
568 ppr NewType = ptext SLIT("newtype")
569 ppr DataType = ptext SLIT("data")
573 %************************************************************************
575 \subsection[ConDecl]{A data-constructor declaration}
577 %************************************************************************
580 type LConDecl name = Located (ConDecl name)
582 -- data T b = forall a. Eq a => MkT a b
583 -- MkT :: forall b a. Eq a => MkT a b
586 -- MkT1 :: Int -> T Int
588 -- data T = Int `MkT` Int
592 -- Int `MkT` Int :: T Int
596 { con_name :: Located name -- Constructor name; this is used for the
597 -- DataCon itself, and for the user-callable wrapper Id
599 , con_explicit :: HsExplicitForAll -- Is there an user-written forall? (cf. HStypes.HsForAllTy)
601 , con_qvars :: [LHsTyVarBndr name] -- ResTyH98: the constructor's existential type variables
602 -- ResTyGADT: all the constructor's quantified type variables
604 , con_cxt :: LHsContext name -- The context. This *does not* include the
605 -- "stupid theta" which lives only in the TyData decl
607 , con_details :: HsConDetails name (LBangType name) -- The main payload
609 , con_res :: ResType name -- Result type of the constructor
613 = ResTyH98 -- Constructor was declared using Haskell 98 syntax
614 | ResTyGADT (LHsType name) -- Constructor was declared using GADT-style syntax,
615 -- and here is its result type
619 conDeclsNames :: Eq name => [ConDecl name] -> [Located name]
620 -- See tyClDeclNames for what this does
621 -- The function is boringly complicated because of the records
622 -- And since we only have equality, we have to be a little careful
624 = snd (foldl do_one ([], []) cons)
626 do_one (flds_seen, acc) (ConDecl { con_name = lname, con_details = RecCon flds })
627 = (map unLoc new_flds ++ flds_seen, lname : [f | f <- new_flds] ++ acc)
629 new_flds = [ f | (f,_) <- flds, not (unLoc f `elem` flds_seen) ]
631 do_one (flds_seen, acc) c
632 = (flds_seen, (con_name c):acc)
634 conDetailsTys details = map getBangType (hsConArgs details)
639 instance (OutputableBndr name) => Outputable (ConDecl name) where
642 pprConDecl (ConDecl con expl tvs cxt details ResTyH98)
643 = sep [pprHsForAll expl tvs cxt, ppr_details con details]
645 ppr_details con (InfixCon t1 t2) = hsep [ppr t1, pprHsVar con, ppr t2]
646 ppr_details con (PrefixCon tys) = hsep (pprHsVar con : map ppr tys)
647 ppr_details con (RecCon fields) = ppr con <+> ppr_fields fields
649 pprConDecl (ConDecl con expl tvs cxt (PrefixCon arg_tys) (ResTyGADT res_ty))
650 = ppr con <+> dcolon <+>
651 sep [pprHsForAll expl tvs cxt, ppr (foldr mk_fun_ty res_ty arg_tys)]
653 mk_fun_ty a b = noLoc (HsFunTy a b)
654 pprConDecl (ConDecl con expl tvs cxt (RecCon fields) (ResTyGADT res_ty))
655 = sep [pprHsForAll expl tvs cxt, ppr con <+> ppr fields <+> dcolon <+> ppr res_ty]
657 ppr_fields fields = braces (sep (punctuate comma (map ppr_field fields)))
658 ppr_field (n, ty) = ppr n <+> dcolon <+> ppr ty
661 %************************************************************************
663 \subsection[InstDecl]{An instance declaration
665 %************************************************************************
668 type LInstDecl name = Located (InstDecl name)
671 = InstDecl (LHsType name) -- Context => Class Instance-type
672 -- Using a polytype means that the renamer conveniently
673 -- figures out the quantified type variables for us.
675 [LSig name] -- User-supplied pragmatic info
676 [LTyClDecl name]-- Associated types (ie, 'TyData' and
679 instance (OutputableBndr name) => Outputable (InstDecl name) where
681 ppr (InstDecl inst_ty binds uprags ats)
682 = vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
685 nest 4 (pprLHsBinds binds) ]
687 -- Extract the declarations of associated types from an instance
689 instDeclATs :: InstDecl name -> [LTyClDecl name]
690 instDeclATs (InstDecl _ _ _ ats) = ats
693 %************************************************************************
695 \subsection[DefaultDecl]{A @default@ declaration}
697 %************************************************************************
699 There can only be one default declaration per module, but it is hard
700 for the parser to check that; we pass them all through in the abstract
701 syntax, and that restriction must be checked in the front end.
704 type LDefaultDecl name = Located (DefaultDecl name)
706 data DefaultDecl name
707 = DefaultDecl [LHsType name]
709 instance (OutputableBndr name)
710 => Outputable (DefaultDecl name) where
712 ppr (DefaultDecl tys)
713 = ptext SLIT("default") <+> parens (interpp'SP tys)
716 %************************************************************************
718 \subsection{Foreign function interface declaration}
720 %************************************************************************
724 -- foreign declarations are distinguished as to whether they define or use a
727 -- * the Boolean value indicates whether the pre-standard deprecated syntax
730 type LForeignDecl name = Located (ForeignDecl name)
732 data ForeignDecl name
733 = ForeignImport (Located name) (LHsType name) ForeignImport -- defines name
734 | ForeignExport (Located name) (LHsType name) ForeignExport -- uses name
736 -- Specification Of an imported external entity in dependence on the calling
739 data ForeignImport = -- import of a C entity
741 -- * the two strings specifying a header file or library
742 -- may be empty, which indicates the absence of a
743 -- header or object specification (both are not used
744 -- in the case of `CWrapper' and when `CFunction'
745 -- has a dynamic target)
747 -- * the calling convention is irrelevant for code
748 -- generation in the case of `CLabel', but is needed
749 -- for pretty printing
751 -- * `Safety' is irrelevant for `CLabel' and `CWrapper'
753 CImport CCallConv -- ccall or stdcall
754 Safety -- safe or unsafe
755 FastString -- name of C header
756 FastString -- name of library object
757 CImportSpec -- details of the C entity
759 -- import of a .NET function
761 | DNImport DNCallSpec
763 -- details of an external C entity
765 data CImportSpec = CLabel CLabelString -- import address of a C label
766 | CFunction CCallTarget -- static or dynamic function
767 | CWrapper -- wrapper to expose closures
770 -- specification of an externally exported entity in dependence on the calling
773 data ForeignExport = CExport CExportSpec -- contains the calling convention
774 | DNExport -- presently unused
776 -- abstract type imported from .NET
778 data FoType = DNType -- In due course we'll add subtype stuff
779 deriving (Eq) -- Used for equality instance for TyClDecl
782 -- pretty printing of foreign declarations
785 instance OutputableBndr name => Outputable (ForeignDecl name) where
786 ppr (ForeignImport n ty fimport) =
787 ptext SLIT("foreign import") <+> ppr fimport <+>
788 ppr n <+> dcolon <+> ppr ty
789 ppr (ForeignExport n ty fexport) =
790 ptext SLIT("foreign export") <+> ppr fexport <+>
791 ppr n <+> dcolon <+> ppr ty
793 instance Outputable ForeignImport where
794 ppr (DNImport spec) =
795 ptext SLIT("dotnet") <+> ppr spec
796 ppr (CImport cconv safety header lib spec) =
797 ppr cconv <+> ppr safety <+>
798 char '"' <> pprCEntity header lib spec <> char '"'
800 pprCEntity header lib (CLabel lbl) =
801 ptext SLIT("static") <+> ftext header <+> char '&' <>
802 pprLib lib <> ppr lbl
803 pprCEntity header lib (CFunction (StaticTarget lbl)) =
804 ptext SLIT("static") <+> ftext header <+> char '&' <>
805 pprLib lib <> ppr lbl
806 pprCEntity header lib (CFunction (DynamicTarget)) =
807 ptext SLIT("dynamic")
808 pprCEntity _ _ (CWrapper) = ptext SLIT("wrapper")
810 pprLib lib | nullFS lib = empty
811 | otherwise = char '[' <> ppr lib <> char ']'
813 instance Outputable ForeignExport where
814 ppr (CExport (CExportStatic lbl cconv)) =
815 ppr cconv <+> char '"' <> ppr lbl <> char '"'
817 ptext SLIT("dotnet") <+> ptext SLIT("\"<unused>\"")
819 instance Outputable FoType where
820 ppr DNType = ptext SLIT("type dotnet")
824 %************************************************************************
826 \subsection{Transformation rules}
828 %************************************************************************
831 type LRuleDecl name = Located (RuleDecl name)
834 = HsRule -- Source rule
835 RuleName -- Rule name
837 [RuleBndr name] -- Forall'd vars; after typechecking this includes tyvars
838 (Located (HsExpr name)) -- LHS
839 NameSet -- Free-vars from the LHS
840 (Located (HsExpr name)) -- RHS
841 NameSet -- Free-vars from the RHS
844 = RuleBndr (Located name)
845 | RuleBndrSig (Located name) (LHsType name)
847 collectRuleBndrSigTys :: [RuleBndr name] -> [LHsType name]
848 collectRuleBndrSigTys bndrs = [ty | RuleBndrSig _ ty <- bndrs]
850 instance OutputableBndr name => Outputable (RuleDecl name) where
851 ppr (HsRule name act ns lhs fv_lhs rhs fv_rhs)
852 = sep [text "{-# RULES" <+> doubleQuotes (ftext name) <+> ppr act,
853 nest 4 (pp_forall <+> pprExpr (unLoc lhs)),
854 nest 4 (equals <+> pprExpr (unLoc rhs) <+> text "#-}") ]
856 pp_forall | null ns = empty
857 | otherwise = text "forall" <+> fsep (map ppr ns) <> dot
859 instance OutputableBndr name => Outputable (RuleBndr name) where
860 ppr (RuleBndr name) = ppr name
861 ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty
865 %************************************************************************
867 \subsection[DeprecDecl]{Deprecations}
869 %************************************************************************
871 We use exported entities for things to deprecate.
874 type LDeprecDecl name = Located (DeprecDecl name)
876 data DeprecDecl name = Deprecation name DeprecTxt
878 instance OutputableBndr name => Outputable (DeprecDecl name) where
879 ppr (Deprecation thing txt)
880 = hsep [text "{-# DEPRECATED", ppr thing, doubleQuotes (ppr txt), text "#-}"]