2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[HsDecls]{Abstract syntax: global declarations}
6 Definitions for: @FixityDecl@, @TyDecl@ and @ConDecl@, @ClassDecl@,
7 @InstDecl@, @DefaultDecl@.
10 #include "HsVersions.h"
17 import HsBinds ( HsBinds, MonoBinds, Sig, nullMonoBinds )
18 import HsPragmas ( DataPragmas, ClassPragmas,
19 InstancePragmas, ClassOpPragmas
23 import SpecEnv ( SpecEnv )
24 import HsCore ( UfExpr )
25 import HsBasic ( Fixity )
28 import Name ( pprSym, pprNonSym, getOccName, OccName )
29 import Outputable ( interppSP, interpp'SP,
30 Outputable(..){-instance * []-}
33 import SrcLoc ( SrcLoc )
34 import PprStyle ( PprStyle(..), ifaceStyle )
38 %************************************************************************
40 \subsection[HsDecl]{Declarations}
42 %************************************************************************
45 data HsDecl tyvar uvar name pat
47 | ClD (ClassDecl tyvar uvar name pat)
48 | InstD (InstDecl tyvar uvar name pat)
49 | DefD (DefaultDecl name)
50 | ValD (HsBinds tyvar uvar name pat)
51 | SigD (IfaceSig name)
55 hsDeclName (TyD (TyData _ name _ _ _ _ _)) = name
56 hsDeclName (TyD (TyNew _ name _ _ _ _ _)) = name
57 hsDeclName (TyD (TySynonym name _ _ _)) = name
58 hsDeclName (ClD (ClassDecl _ name _ _ _ _ _)) = name
59 hsDeclName (SigD (IfaceSig name _ _ _)) = name
60 -- Others don't make sense
64 instance (NamedThing name, Outputable name, Outputable pat,
65 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
66 => Outputable (HsDecl tyvar uvar name pat) where
68 ppr sty (TyD td) = ppr sty td
69 ppr sty (ClD cd) = ppr sty cd
70 ppr sty (SigD sig) = ppr sty sig
71 ppr sty (ValD binds) = ppr sty binds
72 ppr sty (DefD def) = ppr sty def
73 ppr sty (InstD inst) = ppr sty inst
75 -- In interfaces, top-level binders are printed without their "Module." prefix
76 ppr_top_binder sty bndr | ifaceStyle sty = ppr sty (getOccName bndr)
77 | otherwise = ppr sty bndr
81 %************************************************************************
83 \subsection[FixityDecl]{A fixity declaration}
85 %************************************************************************
88 data FixityDecl name = FixityDecl name Fixity SrcLoc
90 instance Outputable name => Outputable (FixityDecl name) where
91 ppr sty (FixityDecl name fixity loc) = ppSep [ppr sty fixity, ppr sty name]
95 %************************************************************************
97 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
99 %************************************************************************
103 = TyData (Context name) -- context
104 name -- type constructor
105 [HsTyVar name] -- type variables
106 [ConDecl name] -- data constructors (empty if abstract)
107 (Maybe [name]) -- derivings; Nothing => not specified
108 -- (i.e., derive default); Just [] => derive
109 -- *nothing*; Just <list> => as you would
114 | TyNew (Context name) -- context
115 name -- type constructor
116 [HsTyVar name] -- type variables
117 (ConDecl name) -- data constructor
118 (Maybe [name]) -- derivings; as above
122 | TySynonym name -- type constructor
123 [HsTyVar name] -- type variables
124 (HsType name) -- synonym expansion
130 instance (NamedThing name, Outputable name)
131 => Outputable (TyDecl name) where
133 ppr sty (TySynonym tycon tyvars mono_ty src_loc)
134 = ppHang (pp_decl_head sty SLIT("type") ppNil tycon tyvars)
137 ppr sty (TyData context tycon tyvars condecls derivings pragmas src_loc)
139 (pp_decl_head sty SLIT("data") (pp_context_and_arrow sty context) tycon tyvars)
140 (pp_condecls sty condecls)
143 ppr sty (TyNew context tycon tyvars condecl derivings pragmas src_loc)
145 (pp_decl_head sty SLIT("newtype") (pp_context_and_arrow sty context) tycon tyvars)
149 pp_decl_head sty str pp_context tycon tyvars
150 = ppCat [ppPStr str, pp_context, ppr_top_binder sty tycon,
151 interppSP sty tyvars, ppPStr SLIT("=")]
153 pp_condecls sty [] = ppNil -- Curious!
154 pp_condecls sty (c:cs)
155 = ppSep (ppr sty c : map (\ c -> ppBeside (ppPStr SLIT("| ")) (ppr sty c)) cs)
157 pp_tydecl sty pp_head pp_decl_rhs derivings
158 = ppHang pp_head 4 (ppSep [
160 case (derivings, sty) of
162 (_,PprInterface) -> ppNil -- No derivings in interfaces
163 (Just ds,_) -> ppCat [ppPStr SLIT("deriving"), ppParens (interpp'SP sty ds)]
166 pp_context_and_arrow :: Outputable name => PprStyle -> Context name -> Pretty
167 pp_context_and_arrow sty [] = ppNil
168 pp_context_and_arrow sty theta = ppCat [pprContext sty theta, ppPStr SLIT("=>")]
171 A type for recording what types a datatype should be specialised to.
172 It's called a ``Sig'' because it's sort of like a ``type signature''
173 for an datatype declaration.
176 data SpecDataSig name
177 = SpecDataSig name -- tycon to specialise
181 instance (NamedThing name, Outputable name)
182 => Outputable (SpecDataSig name) where
184 ppr sty (SpecDataSig tycon ty _)
185 = ppCat [ppStr "{-# SPECIALIZE data", ppr sty ty, ppStr "#-}"]
188 %************************************************************************
190 \subsection[ConDecl]{A data-constructor declaration}
192 %************************************************************************
196 = ConDecl name -- prefix-style con decl
200 | ConOpDecl (BangType name) -- infix-style con decl
206 [([name], BangType name)] -- list of "fields"
209 | NewConDecl name -- newtype con decl
214 = Banged (HsType name) -- HsType: to allow Haskell extensions
215 | Unbanged (HsType name) -- (MonoType only needed for straight Haskell)
219 instance (NamedThing name, Outputable name) => Outputable (ConDecl name) where
221 ppr sty (ConDecl con tys _)
222 = ppCat [ppr_top_binder sty con, ppInterleave ppNil (map (ppr_bang sty) tys)]
224 -- We print ConOpDecls in prefix form in interface files
225 ppr sty (ConOpDecl ty1 op ty2 _)
227 = ppCat [ppr_top_binder sty op, ppr_bang sty ty1, ppr_bang sty ty2]
229 = ppCat [ppr_bang sty ty1, ppr_top_binder sty op, ppr_bang sty ty2]
231 ppr sty (NewConDecl con ty _)
232 = ppCat [ppr_top_binder sty con, pprParendHsType sty ty]
233 ppr sty (RecConDecl con fields _)
234 = ppCat [ppr_top_binder sty con,
235 ppCurlies (ppInterleave pp'SP (map pp_field fields))
238 pp_field (ns, ty) = ppCat [ppCat (map (ppr_top_binder sty) ns),
239 ppPStr SLIT("::"), ppr_bang sty ty]
241 ppr_bang sty (Banged ty) = ppBeside (ppPStr SLIT("! ")) (pprParendHsType sty ty)
242 -- The extra space helps the lexical analyser that lexes
243 -- interface files; it doesn't make the rigid operator/identifier
244 -- distinction, so "!a" is a valid identifier so far as it is concerned
245 ppr_bang sty (Unbanged ty) = pprParendHsType sty ty
248 %************************************************************************
250 \subsection[ClassDecl]{A class declaration}
252 %************************************************************************
255 data ClassDecl tyvar uvar name pat
256 = ClassDecl (Context name) -- context...
257 name -- name of the class
258 (HsTyVar name) -- the class type variable
259 [Sig name] -- methods' signatures
260 (MonoBinds tyvar uvar name pat) -- default methods
266 instance (NamedThing name, Outputable name, Outputable pat,
267 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
268 => Outputable (ClassDecl tyvar uvar name pat) where
270 ppr sty (ClassDecl context clas tyvar sigs methods pragmas src_loc)
271 | null sigs -- No "where" part
274 | iface_style -- All on one line (for now at least)
275 = ppCat [top_matter, ppPStr SLIT("where"),
276 ppCurlies (ppInterleave (ppPStr SLIT("; ")) pp_sigs)]
278 | otherwise -- Laid out
279 = ppSep [ppCat [top_matter, ppPStr SLIT("where {")],
280 ppNest 4 ((ppIntersperse ppSemi pp_sigs `ppAbove` pp_methods)
281 `ppBeside` ppChar '}')]
283 top_matter = ppCat [ppPStr SLIT("class"), pp_context_and_arrow sty context,
284 ppr_top_binder sty clas, ppr sty tyvar]
285 pp_sigs = map (ppr sty) sigs
286 pp_methods = ppr sty methods
287 iface_style = case sty of {PprInterface -> True; other -> False}
290 %************************************************************************
292 \subsection[InstDecl]{An instance declaration (also, @SpecInstSig@)}
294 %************************************************************************
297 data InstDecl tyvar uvar name pat
298 = InstDecl (HsType name) -- Context => Class Instance-type
299 -- Using a polytype means that the renamer conveniently
300 -- figures out the quantified type variables for us.
302 (MonoBinds tyvar uvar name pat)
304 [Sig name] -- User-supplied pragmatic info
306 (Maybe name) -- Name for the dictionary function
312 instance (NamedThing name, Outputable name, Outputable pat,
313 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
314 => Outputable (InstDecl tyvar uvar name pat) where
316 ppr sty (InstDecl inst_ty binds uprags dfun_name src_loc)
317 | case sty of { PprInterface -> True; other -> False} ||
318 nullMonoBinds binds && null uprags
319 = ppCat [ppPStr SLIT("instance"), ppr sty inst_ty]
322 = ppAboves [ppCat [ppPStr SLIT("instance"), ppr sty inst_ty, ppPStr SLIT("where")],
323 ppNest 4 (ppr sty uprags),
324 ppNest 4 (ppr sty binds) ]
327 A type for recording what instances the user wants to specialise;
328 called a ``Sig'' because it's sort of like a ``type signature'' for an
331 data SpecInstSig name
332 = SpecInstSig name -- class
333 (HsType name) -- type to specialise to
336 instance (NamedThing name, Outputable name)
337 => Outputable (SpecInstSig name) where
339 ppr sty (SpecInstSig clas ty _)
340 = ppCat [ppStr "{-# SPECIALIZE instance", ppr sty clas, ppr sty ty, ppStr "#-}"]
343 %************************************************************************
345 \subsection[DefaultDecl]{A @default@ declaration}
347 %************************************************************************
349 There can only be one default declaration per module, but it is hard
350 for the parser to check that; we pass them all through in the abstract
351 syntax, and that restriction must be checked in the front end.
354 data DefaultDecl name
355 = DefaultDecl [HsType name]
358 instance (NamedThing name, Outputable name)
359 => Outputable (DefaultDecl name) where
361 ppr sty (DefaultDecl tys src_loc)
362 = ppBeside (ppPStr SLIT("default ")) (ppParens (interpp'SP sty tys))
365 %************************************************************************
367 \subsection{Signatures in interface files}
369 %************************************************************************
378 instance (NamedThing name, Outputable name) => Outputable (IfaceSig name) where
379 ppr sty (IfaceSig var ty _ _)
380 = ppHang (ppCat [ppr_top_binder sty var, ppPStr SLIT("::")])
385 | HsStrictness (StrictnessInfo name)
386 | HsUnfold (UfExpr name)
387 | HsUpdate UpdateInfo
388 | HsDeforest DeforestInfo
389 | HsArgUsage ArgUsageInfo
390 | HsFBType FBTypeInfo
391 -- ToDo: specialisations