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(..), BangType(..),
15 IfaceSig(..), SpecDataSig(..),
16 DeprecDecl(..), DeprecTxt,
17 hsDeclName, tyClDeclName, isClassDecl, isSynDecl, isDataDecl, countTyClDecls, toHsRule
20 #include "HsVersions.h"
23 import HsBinds ( HsBinds, MonoBinds, Sig(..), FixitySig(..), nullMonoBinds )
24 import HsExpr ( HsExpr )
25 import HsPragmas ( DataPragmas, ClassPragmas )
26 import HsImpExp ( IE(..) )
28 import PprCore ( pprCoreRule )
29 import HsCore ( UfExpr(UfVar), UfBinder, IfaceSig(..), eq_ufBinders, eq_ufExpr, pprUfExpr, toUfExpr, toUfBndr )
30 import CoreSyn ( CoreRule(..) )
31 import BasicTypes ( Fixity, NewOrData(..) )
32 import CallConv ( CallConv, pprCallConv )
33 import Var ( TyVar, Id )
34 import Name ( toRdrName )
38 import FunDeps ( pprFundeps )
39 import Class ( FunDep )
40 import CStrings ( CLabelString, pprCLabelString )
42 import SrcLoc ( SrcLoc, noSrcLoc )
47 %************************************************************************
49 \subsection[HsDecl]{Declarations}
51 %************************************************************************
55 = TyClD (TyClDecl name pat)
56 | InstD (InstDecl name pat)
57 | DefD (DefaultDecl name)
58 | ValD (HsBinds name pat)
59 | ForD (ForeignDecl name)
60 | SigD (IfaceSig name)
61 | FixD (FixitySig name)
62 | DeprecD (DeprecDecl name)
63 | RuleD (RuleDecl name pat)
65 -- NB: all top-level fixity decls are contained EITHER
67 -- OR in the ClassDecls in TyClDs
70 -- a) data constructors
71 -- b) class methods (but they can be also done in the
72 -- signatures of class decls)
73 -- c) imported functions (that have an IfacSig)
76 -- The latter is for class methods only
81 hsDeclName :: (Outputable name, Outputable pat)
82 => HsDecl name pat -> name
84 hsDeclName (TyClD decl) = tyClDeclName decl
85 hsDeclName (SigD (IfaceSig name _ _ _)) = name
86 hsDeclName (InstD (InstDecl _ _ _ name _)) = name
87 hsDeclName (ForD (ForeignDecl name _ _ _ _ _)) = name
88 hsDeclName (FixD (FixitySig name _ _)) = name
89 -- Others don't make sense
91 hsDeclName x = pprPanic "HsDecls.hsDeclName" (ppr x)
94 tyClDeclName :: TyClDecl name pat -> name
95 tyClDeclName (TyData _ _ name _ _ _ _ _ _) = name
96 tyClDeclName (TySynonym name _ _ _) = name
97 tyClDeclName (ClassDecl _ name _ _ _ _ _ _ _ _ _ _) = name
101 instance (Outputable name, Outputable pat)
102 => Outputable (HsDecl name pat) where
104 ppr (TyClD dcl) = ppr dcl
105 ppr (SigD sig) = ppr sig
106 ppr (ValD binds) = ppr binds
107 ppr (DefD def) = ppr def
108 ppr (InstD inst) = ppr inst
109 ppr (ForD fd) = ppr fd
110 ppr (FixD fd) = ppr fd
111 ppr (RuleD rd) = ppr rd
112 ppr (DeprecD dd) = ppr dd
116 instance Ord name => Eq (HsDecl name pat) where
117 -- Used only when comparing interfaces,
118 -- at which time only signature and type/class decls
119 (SigD s1) == (SigD s2) = s1 == s2
120 (TyClD d1) == (TyClD d2) = d1 == d2
125 %************************************************************************
127 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
129 %************************************************************************
132 data TyClDecl name pat
134 (HsContext name) -- context
135 name -- type constructor
136 [HsTyVarBndr name] -- type variables
137 [ConDecl name] -- data constructors (empty if abstract)
138 Int -- Number of data constructors (valid even if type is abstract)
139 (Maybe [name]) -- derivings; Nothing => not specified
140 -- (i.e., derive default); Just [] => derive
141 -- *nothing*; Just <list> => as you would
146 | TySynonym name -- type constructor
147 [HsTyVarBndr name] -- type variables
148 (HsType name) -- synonym expansion
151 | ClassDecl (HsContext name) -- context...
152 name -- name of the class
153 [HsTyVarBndr name] -- the class type variables
154 [FunDep name] -- functional dependencies
155 [Sig name] -- methods' signatures
156 (MonoBinds name pat) -- default methods
158 name name name [name] -- The names of the tycon, datacon wrapper, datacon worker,
159 -- and superclass selectors for this class.
160 -- These are filled in as the ClassDecl is made.
163 instance Ord name => Eq (TyClDecl name pat) where
164 -- Used only when building interface files
165 (==) (TyData nd1 cxt1 n1 tvs1 cons1 _ _ _ _)
166 (TyData nd2 cxt2 n2 tvs2 cons2 _ _ _ _)
169 eqWithHsTyVars tvs1 tvs2 (\ env ->
170 eq_hsContext env cxt1 cxt2 &&
171 eqListBy (eq_ConDecl env) cons1 cons2
174 (==) (TySynonym n1 tvs1 ty1 _)
175 (TySynonym n2 tvs2 ty2 _)
177 eqWithHsTyVars tvs1 tvs2 (\ env -> eq_hsType env ty1 ty2)
179 (==) (ClassDecl cxt1 n1 tvs1 fds1 sigs1 _ _ _ _ _ _ _)
180 (ClassDecl cxt2 n2 tvs2 fds2 sigs2 _ _ _ _ _ _ _)
182 eqWithHsTyVars tvs1 tvs2 (\ env ->
183 eq_hsContext env cxt1 cxt2 &&
184 eqListBy (eq_hsFD env) fds1 fds2 &&
185 eqListBy (eq_cls_sig env) sigs1 sigs2
188 (==) _ _ = False -- default case
191 eq_hsFD env (ns1,ms1) (ns2,ms2)
192 = eqListBy (eq_hsVar env) ns1 ns2 && eqListBy (eq_hsVar env) ms1 ms2
194 eq_cls_sig env (ClassOpSig n1 _ b1 ty1 _) (ClassOpSig n2 _ b2 ty2 _)
195 = n1==n2 && b1==b2 && eq_hsType env ty1 ty2
199 countTyClDecls :: [TyClDecl name pat] -> (Int, Int, Int, Int)
200 -- class, data, newtype, synonym decls
202 = (length [() | ClassDecl _ _ _ _ _ _ _ _ _ _ _ _ <- decls],
203 length [() | TyData DataType _ _ _ _ _ _ _ _ <- decls],
204 length [() | TyData NewType _ _ _ _ _ _ _ _ <- decls],
205 length [() | TySynonym _ _ _ _ <- decls])
207 isDataDecl, isSynDecl, isClassDecl :: TyClDecl name pat -> Bool
209 isSynDecl (TySynonym _ _ _ _) = True
210 isSynDecl other = False
212 isDataDecl (TyData _ _ _ _ _ _ _ _ _) = True
213 isDataDecl other = False
215 isClassDecl (ClassDecl _ _ _ _ _ _ _ _ _ _ _ _) = True
216 isClassDecl other = False
220 instance (Outputable name, Outputable pat)
221 => Outputable (TyClDecl name pat) where
223 ppr (TySynonym tycon tyvars mono_ty src_loc)
224 = hang (ptext SLIT("type") <+> pp_decl_head [] tycon tyvars <+> equals)
227 ppr (TyData new_or_data context tycon tyvars condecls ncons derivings pragmas src_loc)
229 (ptext keyword <+> pp_decl_head context tycon tyvars <+> equals)
230 (pp_condecls condecls ncons)
233 keyword = case new_or_data of
234 NewType -> SLIT("newtype")
235 DataType -> SLIT("data")
237 ppr (ClassDecl context clas tyvars fds sigs methods pragmas _ _ _ _ src_loc)
238 | null sigs -- No "where" part
241 | otherwise -- Laid out
242 = sep [hsep [top_matter, ptext SLIT("where {")],
243 nest 4 (sep [sep (map ppr_sig sigs), pp_methods, char '}'])]
245 top_matter = ptext SLIT("class") <+> pp_decl_head context clas tyvars <+> pprFundeps fds
246 ppr_sig sig = ppr sig <> semi
247 pp_methods = getPprStyle $ \ sty ->
248 if ifaceStyle sty then empty else ppr methods
251 pp_decl_head :: Outputable name => HsContext name -> name -> [HsTyVarBndr name] -> SDoc
252 pp_decl_head context thing tyvars = hsep [pprHsContext context, ppr thing, interppSP tyvars]
254 pp_condecls [] ncons = ptext SLIT("{- abstract with") <+> int ncons <+> ptext SLIT("constructors -}")
255 pp_condecls (c:cs) ncons = sep (ppr c : map (\ c -> ptext SLIT("|") <+> ppr c) cs)
257 pp_tydecl pp_head pp_decl_rhs derivings
258 = hang pp_head 4 (sep [
262 Just ds -> hsep [ptext SLIT("deriving"), parens (interpp'SP ds)]
266 A type for recording what types a datatype should be specialised to.
267 It's called a ``Sig'' because it's sort of like a ``type signature''
268 for an datatype declaration.
271 data SpecDataSig name
272 = SpecDataSig name -- tycon to specialise
276 instance (Outputable name)
277 => Outputable (SpecDataSig name) where
279 ppr (SpecDataSig tycon ty _)
280 = hsep [text "{-# SPECIALIZE data", ppr ty, text "#-}"]
283 %************************************************************************
285 \subsection[ConDecl]{A data-constructor declaration}
287 %************************************************************************
291 = ConDecl name -- Constructor name; this is used for the
292 -- DataCon itself, and for the user-callable wrapper Id
294 name -- Name of the constructor's 'worker Id'
295 -- Filled in as the ConDecl is built
297 [HsTyVarBndr name] -- Existentially quantified type variables
298 (HsContext name) -- ...and context
299 -- If both are empty then there are no existentials
305 = VanillaCon -- prefix-style con decl
308 | InfixCon -- infix-style con decl
312 | RecCon -- record-style con decl
313 [([name], BangType name)] -- list of "fields"
315 | NewCon -- newtype con decl, possibly with a labelled field.
317 (Maybe name) -- Just x => labelled field 'x'
320 = Banged (HsType name) -- HsType: to allow Haskell extensions
321 | Unbanged (HsType name) -- (MonoType only needed for straight Haskell)
322 | Unpacked (HsType name) -- Field is strict and to be unpacked if poss.
325 eq_ConDecl env (ConDecl n1 _ tvs1 cxt1 cds1 _)
326 (ConDecl n2 _ tvs2 cxt2 cds2 _)
328 (eqWithHsTyVars tvs1 tvs2 $ \ env ->
329 eq_hsContext env cxt1 cxt2 &&
330 eq_ConDetails env cds1 cds2)
332 eq_ConDetails env (VanillaCon bts1) (VanillaCon bts2)
333 = eqListBy (eq_btype env) bts1 bts2
334 eq_ConDetails env (InfixCon bta1 btb1) (InfixCon bta2 btb2)
335 = eq_btype env bta1 bta2 && eq_btype env btb1 btb2
336 eq_ConDetails env (RecCon fs1) (RecCon fs2)
337 = eqListBy (eq_fld env) fs1 fs2
338 eq_ConDetails env (NewCon t1 mn1) (NewCon t2 mn2)
339 = eq_hsType env t1 t2 && mn1 == mn2
340 eq_ConDetails env _ _ = False
342 eq_fld env (ns1,bt1) (ns2, bt2) = ns1==ns2 && eq_btype env bt1 bt2
344 eq_btype env (Banged t1) (Banged t2) = eq_hsType env t1 t2
345 eq_btype env (Unbanged t1) (Unbanged t2) = eq_hsType env t1 t2
346 eq_btype env (Unpacked t1) (Unpacked t2) = eq_hsType env t1 t2
347 eq_btype env _ _ = False
351 instance (Outputable name) => Outputable (ConDecl name) where
352 ppr (ConDecl con _ tvs cxt con_details loc)
353 = sep [pprHsForAll tvs cxt, ppr_con_details con con_details]
355 ppr_con_details con (InfixCon ty1 ty2)
356 = hsep [ppr_bang ty1, ppr con, ppr_bang ty2]
358 ppr_con_details con (VanillaCon tys)
359 = ppr con <+> hsep (map (ppr_bang) tys)
361 ppr_con_details con (NewCon ty Nothing)
362 = ppr con <+> pprParendHsType ty
364 ppr_con_details con (NewCon ty (Just x))
365 = ppr con <+> braces pp_field
367 pp_field = ppr x <+> dcolon <+> pprParendHsType ty
369 ppr_con_details con (RecCon fields)
370 = ppr con <+> braces (hsep (punctuate comma (map ppr_field fields)))
372 ppr_field (ns, ty) = hsep (map (ppr) ns) <+>
376 instance Outputable name => Outputable (BangType name) where
379 ppr_bang (Banged ty) = ptext SLIT("!") <> pprParendHsType ty
380 ppr_bang (Unbanged ty) = pprParendHsType ty
381 ppr_bang (Unpacked ty) = ptext SLIT("! !") <> pprParendHsType ty
385 %************************************************************************
387 \subsection[InstDecl]{An instance declaration
389 %************************************************************************
392 data InstDecl name pat
393 = InstDecl (HsType name) -- Context => Class Instance-type
394 -- Using a polytype means that the renamer conveniently
395 -- figures out the quantified type variables for us.
399 [Sig name] -- User-supplied pragmatic info
401 name -- Name for the dictionary function
407 instance (Outputable name, Outputable pat)
408 => Outputable (InstDecl name pat) where
410 ppr (InstDecl inst_ty binds uprags dfun_name src_loc)
411 = getPprStyle $ \ sty ->
412 if ifaceStyle sty then
413 hsep [ptext SLIT("instance"), ppr inst_ty, equals, ppr dfun_name]
415 vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
421 instance Ord name => Eq (InstDecl name pat) where
422 -- Used for interface comparison only, so don't compare bindings
423 (==) (InstDecl inst_ty1 _ _ dfun1 _) (InstDecl inst_ty2 _ _ dfun2 _)
424 = inst_ty1 == inst_ty2 && dfun1 == dfun2
428 %************************************************************************
430 \subsection[DefaultDecl]{A @default@ declaration}
432 %************************************************************************
434 There can only be one default declaration per module, but it is hard
435 for the parser to check that; we pass them all through in the abstract
436 syntax, and that restriction must be checked in the front end.
439 data DefaultDecl name
440 = DefaultDecl [HsType name]
443 instance (Outputable name)
444 => Outputable (DefaultDecl name) where
446 ppr (DefaultDecl tys src_loc)
447 = ptext SLIT("default") <+> parens (interpp'SP tys)
450 %************************************************************************
452 \subsection{Foreign function interface declaration}
454 %************************************************************************
457 data ForeignDecl name =
466 instance (Outputable name)
467 => Outputable (ForeignDecl name) where
469 ppr (ForeignDecl nm imp_exp ty ext_name cconv src_loc)
470 = ptext SLIT("foreign") <+> ppr_imp_exp <+> pprCallConv cconv <+>
471 ppr ext_name <+> ppr_unsafe <+> ppr nm <+> dcolon <+> ppr ty
473 (ppr_imp_exp, ppr_unsafe) =
475 FoLabel -> (ptext SLIT("label"), empty)
476 FoExport -> (ptext SLIT("export"), empty)
478 | us -> (ptext SLIT("import"), ptext SLIT("unsafe"))
479 | otherwise -> (ptext SLIT("import"), empty)
484 | FoImport Bool -- True => unsafe call.
488 | ExtName CLabelString -- The external name of the foreign thing,
489 (Maybe CLabelString) -- and optionally its DLL or module name
490 -- Both of these are completely unencoded;
491 -- we just print them as they are
493 isDynamicExtName :: ExtName -> Bool
494 isDynamicExtName Dynamic = True
495 isDynamicExtName _ = False
497 extNameStatic :: ExtName -> CLabelString
498 extNameStatic (ExtName f _) = f
499 extNameStatic Dynamic = panic "staticExtName: Dynamic - shouldn't ever happen."
501 instance Outputable ExtName where
502 ppr Dynamic = ptext SLIT("dynamic")
503 ppr (ExtName nm mb_mod) =
504 case mb_mod of { Nothing -> empty; Just m -> doubleQuotes (ptext m) } <+>
505 doubleQuotes (pprCLabelString nm)
508 %************************************************************************
510 \subsection{Transformation rules}
512 %************************************************************************
515 data RuleDecl name pat
516 = HsRule -- Source rule
517 FAST_STRING -- Rule name
518 [name] -- Forall'd tyvars, filled in by the renamer with
519 -- tyvars mentioned in sigs; then filled out by typechecker
520 [RuleBndr name] -- Forall'd term vars
521 (HsExpr name pat) -- LHS
522 (HsExpr name pat) -- RHS
525 | IfaceRule -- One that's come in from an interface file; pre-typecheck
527 [UfBinder name] -- Tyvars and term vars
529 [UfExpr name] -- Args of LHS
530 (UfExpr name) -- Pre typecheck
533 | IfaceRuleOut -- Post typecheck
540 | RuleBndrSig name (HsType name)
542 instance Ord name => Eq (RuleDecl name pat) where
543 -- Works for IfaceRules only; used when comparing interface file versions
544 (IfaceRule n1 bs1 f1 es1 rhs1 _) == (IfaceRule n2 bs2 f2 es2 rhs2 _)
545 = n1==n2 && f1 == f2 &&
546 eq_ufBinders emptyEqHsEnv bs1 bs2 (\env ->
547 eqListBy (eq_ufExpr env) (rhs1:es1) (rhs2:es2))
549 instance (Outputable name, Outputable pat)
550 => Outputable (RuleDecl name pat) where
551 ppr (HsRule name tvs ns lhs rhs loc)
552 = sep [text "{-# RULES" <+> doubleQuotes (ptext name),
553 pp_forall, ppr lhs, equals <+> ppr rhs,
556 pp_forall | null tvs && null ns = empty
557 | otherwise = text "forall" <+>
558 fsep (map ppr tvs ++ map ppr ns)
561 ppr (IfaceRule name tpl_vars fn tpl_args rhs loc)
562 = hsep [ doubleQuotes (ptext name),
563 ptext SLIT("__forall") <+> braces (interppSP tpl_vars),
564 ppr fn <+> sep (map (pprUfExpr parens) tpl_args),
565 ptext SLIT("=") <+> ppr rhs
568 ppr (IfaceRuleOut fn rule) = pprCoreRule (ppr fn) rule
570 instance Outputable name => Outputable (RuleBndr name) where
571 ppr (RuleBndr name) = ppr name
572 ppr (RuleBndrSig name ty) = ppr name <> dcolon <> ppr ty
574 toHsRule id (BuiltinRule _)
575 = pprTrace "toHsRule: builtin" (ppr id) (bogusIfaceRule id)
577 toHsRule id (Rule name bndrs args rhs)
578 = IfaceRule name (map toUfBndr bndrs) (toRdrName id)
579 (map toUfExpr args) (toUfExpr rhs) noSrcLoc
582 = IfaceRule SLIT("bogus") [] (toRdrName id) [] (UfVar (toRdrName id)) noSrcLoc
586 %************************************************************************
588 \subsection[DeprecDecl]{Deprecations}
590 %************************************************************************
592 We use exported entities for things to deprecate. Cunning trick (hack?):
593 `IEModuleContents undefined' is used for module deprecation.
596 data DeprecDecl name = Deprecation (IE name) DeprecTxt SrcLoc
598 type DeprecTxt = FAST_STRING -- reason/explanation for deprecation
600 instance Outputable name => Outputable (DeprecDecl name) where
601 ppr (Deprecation (IEModuleContents _) txt _)
602 = hsep [text "{-# DEPRECATED", doubleQuotes (ppr txt), text "#-}"]
603 ppr (Deprecation thing txt _)
604 = hsep [text "{-# DEPRECATED", ppr thing, doubleQuotes (ppr txt), text "#-}"]