2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[HsDecls]{Abstract syntax: global declarations}
6 Definitions for: @TyDecl@ and @ConDecl@, @ClassDecl@,
7 @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
11 HsDecl(..), TyClDecl(..), InstDecl(..), RuleDecl(..), RuleBndr(..),
12 DefaultDecl(..), ForeignDecl(..), ForKind(..),
13 ExtName(..), isDynamicExtName, extNameStatic,
14 ConDecl(..), ConDetails(..),
15 BangType(..), getBangType,
16 IfaceSig(..), SpecDataSig(..),
17 DeprecDecl(..), DeprecTxt,
18 hsDeclName, tyClDeclName, isClassDecl, isSynDecl, isDataDecl, countTyClDecls, toHsRule
21 #include "HsVersions.h"
24 import HsBinds ( HsBinds, MonoBinds, Sig(..), FixitySig(..), nullMonoBinds )
25 import HsExpr ( HsExpr )
26 import HsPragmas ( DataPragmas, ClassPragmas )
27 import HsImpExp ( IE(..) )
29 import PprCore ( pprCoreRule )
30 import HsCore ( UfExpr(UfVar), UfBinder, IfaceSig(..), eq_ufBinders, eq_ufExpr, pprUfExpr, toUfExpr, toUfBndr )
31 import CoreSyn ( CoreRule(..) )
32 import BasicTypes ( Fixity, NewOrData(..) )
33 import CallConv ( CallConv, pprCallConv )
34 import Name ( toRdrName )
37 import FunDeps ( pprFundeps )
38 import Class ( FunDep )
39 import CStrings ( CLabelString, pprCLabelString )
41 import SrcLoc ( SrcLoc, noSrcLoc )
45 %************************************************************************
47 \subsection[HsDecl]{Declarations}
49 %************************************************************************
53 = TyClD (TyClDecl name pat)
54 | InstD (InstDecl name pat)
55 | DefD (DefaultDecl name)
56 | ValD (HsBinds name pat)
57 | ForD (ForeignDecl name)
58 | SigD (IfaceSig name)
59 | FixD (FixitySig name)
60 | DeprecD (DeprecDecl name)
61 | RuleD (RuleDecl name pat)
63 -- NB: all top-level fixity decls are contained EITHER
65 -- OR in the ClassDecls in TyClDs
68 -- a) data constructors
69 -- b) class methods (but they can be also done in the
70 -- signatures of class decls)
71 -- c) imported functions (that have an IfacSig)
74 -- The latter is for class methods only
79 hsDeclName :: (Outputable name, Outputable pat)
80 => HsDecl name pat -> name
82 hsDeclName (TyClD decl) = tyClDeclName decl
83 hsDeclName (SigD (IfaceSig name _ _ _)) = name
84 hsDeclName (InstD (InstDecl _ _ _ name _)) = name
85 hsDeclName (ForD (ForeignDecl name _ _ _ _ _)) = name
86 hsDeclName (FixD (FixitySig name _ _)) = name
87 -- Others don't make sense
89 hsDeclName x = pprPanic "HsDecls.hsDeclName" (ppr x)
92 tyClDeclName :: TyClDecl name pat -> name
93 tyClDeclName (TyData _ _ name _ _ _ _ _ _) = name
94 tyClDeclName (TySynonym name _ _ _) = name
95 tyClDeclName (ClassDecl _ name _ _ _ _ _ _ _ _ _ _) = name
99 instance (Outputable name, Outputable pat)
100 => Outputable (HsDecl name pat) where
102 ppr (TyClD dcl) = ppr dcl
103 ppr (SigD sig) = ppr sig
104 ppr (ValD binds) = ppr binds
105 ppr (DefD def) = ppr def
106 ppr (InstD inst) = ppr inst
107 ppr (ForD fd) = ppr fd
108 ppr (FixD fd) = ppr fd
109 ppr (RuleD rd) = ppr rd
110 ppr (DeprecD dd) = ppr dd
114 instance Ord name => Eq (HsDecl name pat) where
115 -- Used only when comparing interfaces,
116 -- at which time only signature and type/class decls
117 (SigD s1) == (SigD s2) = s1 == s2
118 (TyClD d1) == (TyClD d2) = d1 == d2
123 %************************************************************************
125 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
127 %************************************************************************
130 data TyClDecl name pat
132 (HsContext name) -- context
133 name -- type constructor
134 [HsTyVarBndr name] -- type variables
135 [ConDecl name] -- data constructors (empty if abstract)
136 Int -- Number of data constructors (valid even if type is abstract)
137 (Maybe [name]) -- derivings; Nothing => not specified
138 -- (i.e., derive default); Just [] => derive
139 -- *nothing*; Just <list> => as you would
144 | TySynonym name -- type constructor
145 [HsTyVarBndr name] -- type variables
146 (HsType name) -- synonym expansion
149 | ClassDecl (HsContext name) -- context...
150 name -- name of the class
151 [HsTyVarBndr name] -- the class type variables
152 [FunDep name] -- functional dependencies
153 [Sig name] -- methods' signatures
154 (MonoBinds name pat) -- default methods
156 name name name [name] -- The names of the tycon, datacon wrapper, datacon worker,
157 -- and superclass selectors for this class.
158 -- These are filled in as the ClassDecl is made.
161 instance Ord name => Eq (TyClDecl name pat) where
162 -- Used only when building interface files
163 (==) (TyData nd1 cxt1 n1 tvs1 cons1 _ _ _ _)
164 (TyData nd2 cxt2 n2 tvs2 cons2 _ _ _ _)
167 eqWithHsTyVars tvs1 tvs2 (\ env ->
168 eq_hsContext env cxt1 cxt2 &&
169 eqListBy (eq_ConDecl env) cons1 cons2
172 (==) (TySynonym n1 tvs1 ty1 _)
173 (TySynonym n2 tvs2 ty2 _)
175 eqWithHsTyVars tvs1 tvs2 (\ env -> eq_hsType env ty1 ty2)
177 (==) (ClassDecl cxt1 n1 tvs1 fds1 sigs1 _ _ _ _ _ _ _)
178 (ClassDecl cxt2 n2 tvs2 fds2 sigs2 _ _ _ _ _ _ _)
180 eqWithHsTyVars tvs1 tvs2 (\ env ->
181 eq_hsContext env cxt1 cxt2 &&
182 eqListBy (eq_hsFD env) fds1 fds2 &&
183 eqListBy (eq_cls_sig env) sigs1 sigs2
186 (==) _ _ = False -- default case
189 eq_hsFD env (ns1,ms1) (ns2,ms2)
190 = eqListBy (eq_hsVar env) ns1 ns2 && eqListBy (eq_hsVar env) ms1 ms2
192 eq_cls_sig env (ClassOpSig n1 _ b1 ty1 _) (ClassOpSig n2 _ b2 ty2 _)
193 = n1==n2 && b1==b2 && eq_hsType env ty1 ty2
197 countTyClDecls :: [TyClDecl name pat] -> (Int, Int, Int, Int)
198 -- class, data, newtype, synonym decls
200 = (length [() | ClassDecl _ _ _ _ _ _ _ _ _ _ _ _ <- decls],
201 length [() | TyData DataType _ _ _ _ _ _ _ _ <- decls],
202 length [() | TyData NewType _ _ _ _ _ _ _ _ <- decls],
203 length [() | TySynonym _ _ _ _ <- decls])
205 isDataDecl, isSynDecl, isClassDecl :: TyClDecl name pat -> Bool
207 isSynDecl (TySynonym _ _ _ _) = True
208 isSynDecl other = False
210 isDataDecl (TyData _ _ _ _ _ _ _ _ _) = True
211 isDataDecl other = False
213 isClassDecl (ClassDecl _ _ _ _ _ _ _ _ _ _ _ _) = True
214 isClassDecl other = False
218 instance (Outputable name, Outputable pat)
219 => Outputable (TyClDecl name pat) where
221 ppr (TySynonym tycon tyvars mono_ty src_loc)
222 = hang (ptext SLIT("type") <+> pp_decl_head [] tycon tyvars <+> equals)
225 ppr (TyData new_or_data context tycon tyvars condecls ncons derivings pragmas src_loc)
227 (ptext keyword <+> pp_decl_head context tycon tyvars <+> equals)
228 (pp_condecls condecls ncons)
231 keyword = case new_or_data of
232 NewType -> SLIT("newtype")
233 DataType -> SLIT("data")
235 ppr (ClassDecl context clas tyvars fds sigs methods pragmas _ _ _ _ src_loc)
236 | null sigs -- No "where" part
239 | otherwise -- Laid out
240 = sep [hsep [top_matter, ptext SLIT("where {")],
241 nest 4 (sep [sep (map ppr_sig sigs), pp_methods, char '}'])]
243 top_matter = ptext SLIT("class") <+> pp_decl_head context clas tyvars <+> pprFundeps fds
244 ppr_sig sig = ppr sig <> semi
245 pp_methods = getPprStyle $ \ sty ->
246 if ifaceStyle sty then empty else ppr methods
249 pp_decl_head :: Outputable name => HsContext name -> name -> [HsTyVarBndr name] -> SDoc
250 pp_decl_head context thing tyvars = hsep [pprHsContext context, ppr thing, interppSP tyvars]
252 pp_condecls [] ncons = ptext SLIT("{- abstract with") <+> int ncons <+> ptext SLIT("constructors -}")
253 pp_condecls (c:cs) ncons = sep (ppr c : map (\ c -> ptext SLIT("|") <+> ppr c) cs)
255 pp_tydecl pp_head pp_decl_rhs derivings
256 = hang pp_head 4 (sep [
260 Just ds -> hsep [ptext SLIT("deriving"), parens (interpp'SP ds)]
264 A type for recording what types a datatype should be specialised to.
265 It's called a ``Sig'' because it's sort of like a ``type signature''
266 for an datatype declaration.
269 data SpecDataSig name
270 = SpecDataSig name -- tycon to specialise
274 instance (Outputable name)
275 => Outputable (SpecDataSig name) where
277 ppr (SpecDataSig tycon ty _)
278 = hsep [text "{-# SPECIALIZE data", ppr ty, text "#-}"]
281 %************************************************************************
283 \subsection[ConDecl]{A data-constructor declaration}
285 %************************************************************************
289 = ConDecl name -- Constructor name; this is used for the
290 -- DataCon itself, and for the user-callable wrapper Id
292 name -- Name of the constructor's 'worker Id'
293 -- Filled in as the ConDecl is built
295 [HsTyVarBndr name] -- Existentially quantified type variables
296 (HsContext name) -- ...and context
297 -- If both are empty then there are no existentials
303 = VanillaCon -- prefix-style con decl
306 | InfixCon -- infix-style con decl
310 | RecCon -- record-style con decl
311 [([name], BangType name)] -- list of "fields"
313 | NewCon -- newtype con decl, possibly with a labelled field.
315 (Maybe name) -- Just x => labelled field 'x'
317 eq_ConDecl env (ConDecl n1 _ tvs1 cxt1 cds1 _)
318 (ConDecl n2 _ tvs2 cxt2 cds2 _)
320 (eqWithHsTyVars tvs1 tvs2 $ \ env ->
321 eq_hsContext env cxt1 cxt2 &&
322 eq_ConDetails env cds1 cds2)
324 eq_ConDetails env (VanillaCon bts1) (VanillaCon bts2)
325 = eqListBy (eq_btype env) bts1 bts2
326 eq_ConDetails env (InfixCon bta1 btb1) (InfixCon bta2 btb2)
327 = eq_btype env bta1 bta2 && eq_btype env btb1 btb2
328 eq_ConDetails env (RecCon fs1) (RecCon fs2)
329 = eqListBy (eq_fld env) fs1 fs2
330 eq_ConDetails env (NewCon t1 mn1) (NewCon t2 mn2)
331 = eq_hsType env t1 t2 && mn1 == mn2
332 eq_ConDetails env _ _ = False
334 eq_fld env (ns1,bt1) (ns2, bt2) = ns1==ns2 && eq_btype env bt1 bt2
338 = Banged (HsType name) -- HsType: to allow Haskell extensions
339 | Unbanged (HsType name) -- (MonoType only needed for straight Haskell)
340 | Unpacked (HsType name) -- Field is strict and to be unpacked if poss.
342 getBangType (Banged ty) = ty
343 getBangType (Unbanged ty) = ty
344 getBangType (Unpacked ty) = ty
346 eq_btype env (Banged t1) (Banged t2) = eq_hsType env t1 t2
347 eq_btype env (Unbanged t1) (Unbanged t2) = eq_hsType env t1 t2
348 eq_btype env (Unpacked t1) (Unpacked t2) = eq_hsType env t1 t2
349 eq_btype env _ _ = False
353 instance (Outputable name) => Outputable (ConDecl name) where
354 ppr (ConDecl con _ tvs cxt con_details loc)
355 = sep [pprHsForAll tvs cxt, ppr_con_details con con_details]
357 ppr_con_details con (InfixCon ty1 ty2)
358 = hsep [ppr_bang ty1, ppr con, ppr_bang ty2]
360 ppr_con_details con (VanillaCon tys)
361 = ppr con <+> hsep (map (ppr_bang) tys)
363 ppr_con_details con (NewCon ty Nothing)
364 = ppr con <+> pprParendHsType ty
366 ppr_con_details con (NewCon ty (Just x))
367 = ppr con <+> braces pp_field
369 pp_field = ppr x <+> dcolon <+> pprParendHsType ty
371 ppr_con_details con (RecCon fields)
372 = ppr con <+> braces (hsep (punctuate comma (map ppr_field fields)))
374 ppr_field (ns, ty) = hsep (map (ppr) ns) <+>
378 instance Outputable name => Outputable (BangType name) where
381 ppr_bang (Banged ty) = ptext SLIT("!") <> pprParendHsType ty
382 ppr_bang (Unbanged ty) = pprParendHsType ty
383 ppr_bang (Unpacked ty) = ptext SLIT("! !") <> pprParendHsType ty
387 %************************************************************************
389 \subsection[InstDecl]{An instance declaration
391 %************************************************************************
394 data InstDecl name pat
395 = InstDecl (HsType name) -- Context => Class Instance-type
396 -- Using a polytype means that the renamer conveniently
397 -- figures out the quantified type variables for us.
401 [Sig name] -- User-supplied pragmatic info
403 name -- Name for the dictionary function
409 instance (Outputable name, Outputable pat)
410 => Outputable (InstDecl name pat) where
412 ppr (InstDecl inst_ty binds uprags dfun_name src_loc)
413 = getPprStyle $ \ sty ->
414 if ifaceStyle sty then
415 hsep [ptext SLIT("instance"), ppr inst_ty, equals, ppr dfun_name]
417 vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
423 instance Ord name => Eq (InstDecl name pat) where
424 -- Used for interface comparison only, so don't compare bindings
425 (==) (InstDecl inst_ty1 _ _ dfun1 _) (InstDecl inst_ty2 _ _ dfun2 _)
426 = inst_ty1 == inst_ty2 && dfun1 == dfun2
430 %************************************************************************
432 \subsection[DefaultDecl]{A @default@ declaration}
434 %************************************************************************
436 There can only be one default declaration per module, but it is hard
437 for the parser to check that; we pass them all through in the abstract
438 syntax, and that restriction must be checked in the front end.
441 data DefaultDecl name
442 = DefaultDecl [HsType name]
445 instance (Outputable name)
446 => Outputable (DefaultDecl name) where
448 ppr (DefaultDecl tys src_loc)
449 = ptext SLIT("default") <+> parens (interpp'SP tys)
452 %************************************************************************
454 \subsection{Foreign function interface declaration}
456 %************************************************************************
459 data ForeignDecl name =
468 instance (Outputable name)
469 => Outputable (ForeignDecl name) where
471 ppr (ForeignDecl nm imp_exp ty ext_name cconv src_loc)
472 = ptext SLIT("foreign") <+> ppr_imp_exp <+> pprCallConv cconv <+>
473 ppr ext_name <+> ppr_unsafe <+> ppr nm <+> dcolon <+> ppr ty
475 (ppr_imp_exp, ppr_unsafe) =
477 FoLabel -> (ptext SLIT("label"), empty)
478 FoExport -> (ptext SLIT("export"), empty)
480 | us -> (ptext SLIT("import"), ptext SLIT("unsafe"))
481 | otherwise -> (ptext SLIT("import"), empty)
486 | FoImport Bool -- True => unsafe call.
490 | ExtName CLabelString -- The external name of the foreign thing,
491 (Maybe CLabelString) -- and optionally its DLL or module name
492 -- Both of these are completely unencoded;
493 -- we just print them as they are
495 isDynamicExtName :: ExtName -> Bool
496 isDynamicExtName Dynamic = True
497 isDynamicExtName _ = False
499 extNameStatic :: ExtName -> CLabelString
500 extNameStatic (ExtName f _) = f
501 extNameStatic Dynamic = panic "staticExtName: Dynamic - shouldn't ever happen."
503 instance Outputable ExtName where
504 ppr Dynamic = ptext SLIT("dynamic")
505 ppr (ExtName nm mb_mod) =
506 case mb_mod of { Nothing -> empty; Just m -> doubleQuotes (ptext m) } <+>
507 doubleQuotes (pprCLabelString nm)
510 %************************************************************************
512 \subsection{Transformation rules}
514 %************************************************************************
517 data RuleDecl name pat
518 = HsRule -- Source rule
519 FAST_STRING -- Rule name
520 [name] -- Forall'd tyvars, filled in by the renamer with
521 -- tyvars mentioned in sigs; then filled out by typechecker
522 [RuleBndr name] -- Forall'd term vars
523 (HsExpr name pat) -- LHS
524 (HsExpr name pat) -- RHS
527 | IfaceRule -- One that's come in from an interface file; pre-typecheck
529 [UfBinder name] -- Tyvars and term vars
531 [UfExpr name] -- Args of LHS
532 (UfExpr name) -- Pre typecheck
535 | IfaceRuleOut -- Post typecheck
542 | RuleBndrSig name (HsType name)
544 instance Ord name => Eq (RuleDecl name pat) where
545 -- Works for IfaceRules only; used when comparing interface file versions
546 (IfaceRule n1 bs1 f1 es1 rhs1 _) == (IfaceRule n2 bs2 f2 es2 rhs2 _)
547 = n1==n2 && f1 == f2 &&
548 eq_ufBinders emptyEqHsEnv bs1 bs2 (\env ->
549 eqListBy (eq_ufExpr env) (rhs1:es1) (rhs2:es2))
551 instance (Outputable name, Outputable pat)
552 => Outputable (RuleDecl name pat) where
553 ppr (HsRule name tvs ns lhs rhs loc)
554 = sep [text "{-# RULES" <+> doubleQuotes (ptext name),
555 pp_forall, ppr lhs, equals <+> ppr rhs,
558 pp_forall | null tvs && null ns = empty
559 | otherwise = text "forall" <+>
560 fsep (map ppr tvs ++ map ppr ns)
563 ppr (IfaceRule name tpl_vars fn tpl_args rhs loc)
564 = hsep [ doubleQuotes (ptext name),
565 ptext SLIT("__forall") <+> braces (interppSP tpl_vars),
566 ppr fn <+> sep (map (pprUfExpr parens) tpl_args),
567 ptext SLIT("=") <+> ppr rhs
570 ppr (IfaceRuleOut fn rule) = pprCoreRule (ppr fn) rule
572 instance Outputable name => Outputable (RuleBndr name) where
573 ppr (RuleBndr name) = ppr name
574 ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty
576 toHsRule id (BuiltinRule _)
577 = pprTrace "toHsRule: builtin" (ppr id) (bogusIfaceRule id)
579 toHsRule id (Rule name bndrs args rhs)
580 = IfaceRule name (map toUfBndr bndrs) (toRdrName id)
581 (map toUfExpr args) (toUfExpr rhs) noSrcLoc
584 = IfaceRule SLIT("bogus") [] (toRdrName id) [] (UfVar (toRdrName id)) noSrcLoc
588 %************************************************************************
590 \subsection[DeprecDecl]{Deprecations}
592 %************************************************************************
594 We use exported entities for things to deprecate. Cunning trick (hack?):
595 `IEModuleContents undefined' is used for module deprecation.
598 data DeprecDecl name = Deprecation (IE name) DeprecTxt SrcLoc
600 type DeprecTxt = FAST_STRING -- reason/explanation for deprecation
602 instance Outputable name => Outputable (DeprecDecl name) where
603 ppr (Deprecation (IEModuleContents _) txt _)
604 = hsep [text "{-# DEPRECATED", doubleQuotes (ppr txt), text "#-}"]
605 ppr (Deprecation thing txt _)
606 = hsep [text "{-# DEPRECATED", ppr thing, doubleQuotes (ppr txt), text "#-}"]