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_DELOOPER(HsLoop) ( nullMonoBinds, MonoBinds, Sig )
18 import HsPragmas ( DataPragmas, ClassPragmas,
19 InstancePragmas, ClassOpPragmas
24 import Name ( pprSym, pprNonSym )
25 import Outputable ( interppSP, interpp'SP,
26 Outputable(..){-instance * []-}
29 import SrcLoc ( SrcLoc )
30 import Util ( panic#{-ToDo:rm eventually-} )
33 %************************************************************************
35 \subsection[FixityDecl]{A fixity declaration}
37 %************************************************************************
47 instance (NamedThing name, Outputable name)
48 => Outputable (FixityDecl name) where
49 ppr sty (InfixL var prec) = print_it sty "l" prec var
50 ppr sty (InfixR var prec) = print_it sty "r" prec var
51 ppr sty (InfixN var prec) = print_it sty "" prec var
53 print_it sty suff prec var
54 = ppBesides [ppStr "infix", ppStr suff, ppSP, ppInt prec, ppSP, pprSym sty var]
57 %************************************************************************
59 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
61 %************************************************************************
65 = TyData (Context name) -- context
66 name -- type constructor
67 [name] -- type variables
68 [ConDecl name] -- data constructors (empty if abstract)
69 (Maybe [name]) -- derivings; Nothing => not specified
70 -- (i.e., derive default); Just [] => derive
71 -- *nothing*; Just <list> => as you would
76 | TyNew (Context name) -- context
77 name -- type constructor
78 [name] -- type variables
79 [ConDecl name] -- data constructor (empty if abstract)
80 (Maybe [name]) -- derivings; as above
84 | TySynonym name -- type constructor
85 [name] -- type variables
86 (MonoType name) -- synonym expansion
92 instance (NamedThing name, Outputable name)
93 => Outputable (TyDecl name) where
95 ppr sty (TySynonym tycon tyvars mono_ty src_loc)
96 = ppHang (pp_decl_head sty SLIT("type") ppNil tycon tyvars)
97 4 (ppCat [ppEquals, ppr sty mono_ty])
99 ppr sty (TyData context tycon tyvars condecls derivings pragmas src_loc)
101 (pp_decl_head sty SLIT("data") (pprContext sty context) tycon tyvars)
102 (pp_condecls sty condecls)
105 ppr sty (TyNew context tycon tyvars condecl derivings pragmas src_loc)
107 (pp_decl_head sty SLIT("newtype") (pprContext sty context) tycon tyvars)
108 (pp_condecls sty condecl)
111 pp_decl_head sty str pp_context tycon tyvars
112 = ppCat [ppPStr str, pp_context, ppr sty tycon, interppSP sty tyvars]
114 pp_condecls sty [] = ppNil -- abstract datatype
115 pp_condecls sty (c:cs)
116 = ppSep (ppBeside (ppStr "= ") (ppr sty c)
117 : map (\ x -> ppBeside (ppStr "| ") (ppr sty x)) cs)
119 pp_tydecl sty pp_head pp_decl_rhs derivings
120 = ppHang pp_head 4 (ppSep [
124 Just ds -> ppBeside (ppPStr SLIT("deriving "))
125 (ppParens (ppInterleave ppComma (map (ppr sty) ds)))])
128 A type for recording what types a datatype should be specialised to.
129 It's called a ``Sig'' because it's sort of like a ``type signature''
130 for an datatype declaration.
133 data SpecDataSig name
134 = SpecDataSig name -- tycon to specialise
138 instance (NamedThing name, Outputable name)
139 => Outputable (SpecDataSig name) where
141 ppr sty (SpecDataSig tycon ty _)
142 = ppCat [ppStr "{-# SPECIALIZE data", ppr sty ty, ppStr "#-}"]
145 %************************************************************************
147 \subsection[ConDecl]{A data-constructor declaration}
149 %************************************************************************
153 = ConDecl name -- prefix-style con decl
157 | ConOpDecl (BangType name) -- infix-style con decl
163 [([name], BangType name)] -- list of "fields"
166 | NewConDecl name -- newtype con decl
171 = Banged (PolyType name) -- PolyType: to allow Haskell extensions
172 | Unbanged (PolyType name) -- (MonoType only needed for straight Haskell)
176 instance (NamedThing name, Outputable name) => Outputable (ConDecl name) where
178 ppr sty (ConDecl con tys _)
179 = ppCat [pprNonSym sty con, ppInterleave ppNil (map (ppr_bang sty) tys)]
180 ppr sty (ConOpDecl ty1 op ty2 _)
181 = ppCat [ppr_bang sty ty1, pprSym sty op, ppr_bang sty ty2]
182 ppr sty (NewConDecl con ty _)
183 = ppCat [pprNonSym sty con, pprParendMonoType sty ty]
184 ppr sty (RecConDecl con fields _)
185 = ppCat [pprNonSym sty con, ppChar '{',
186 ppInterleave pp'SP (map pp_field fields), ppChar '}']
188 pp_field (n, ty) = ppCat [ppr sty n, ppPStr SLIT("::"), ppr_bang sty ty]
190 ppr_bang sty (Banged ty) = ppBeside (ppChar '!') (pprParendPolyType sty ty)
191 ppr_bang sty (Unbanged ty) = pprParendPolyType sty ty
194 %************************************************************************
196 \subsection[ClassDecl]{A class declaration}
198 %************************************************************************
201 data ClassDecl tyvar uvar name pat
202 = ClassDecl (Context name) -- context...
203 name -- name of the class
204 name -- the class type variable
205 [Sig name] -- methods' signatures
206 (MonoBinds tyvar uvar name pat) -- default methods
212 instance (NamedThing name, Outputable name, Outputable pat,
213 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
214 => Outputable (ClassDecl tyvar uvar name pat) where
216 ppr sty (ClassDecl context clas tyvar sigs methods pragmas src_loc)
218 top_matter = ppCat [ppStr "class", pprContext sty context,
219 ppr sty clas, ppr sty tyvar]
221 if null sigs && nullMonoBinds methods then
222 ppAbove top_matter (ppNest 4 (ppr sty pragmas))
224 ppAboves [ppCat [top_matter, ppStr "where"],
225 ppNest 4 (ppAboves (map (ppr sty) sigs)),
226 ppNest 4 (ppr sty methods),
227 ppNest 4 (ppr sty pragmas) ]
230 %************************************************************************
232 \subsection[InstDecl]{An instance declaration (also, @SpecInstSig@)}
234 %************************************************************************
237 data InstDecl tyvar uvar name pat
238 = InstDecl name -- Class
240 (PolyType name) -- Context => Instance-type
241 -- Using a polytype means that the renamer conveniently
242 -- figures out the quantified type variables for us.
244 (MonoBinds tyvar uvar name pat)
246 Bool -- True <=> This instance decl is from the
247 -- module being compiled; False <=> It is from
248 -- an imported interface.
250 Module -- The name of the module where the instance decl
251 -- originally came from
253 [Sig name] -- actually user-supplied pragmatic info
254 (InstancePragmas name) -- interface-supplied pragmatic info
259 instance (NamedThing name, Outputable name, Outputable pat,
260 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
261 => Outputable (InstDecl tyvar uvar name pat) where
263 ppr sty (InstDecl clas ty binds from_here modname uprags pragmas src_loc)
267 HsPreForAllTy c t -> (c, t)
268 HsForAllTy _ c t -> (c, t)
270 top_matter = ppCat [ppStr "instance", pprContext sty context,
271 ppr sty clas, pprParendMonoType sty inst_ty]
273 if nullMonoBinds binds && null uprags then
274 ppAbove top_matter (ppNest 4 (ppr sty pragmas))
276 ppAboves [ppCat [top_matter, ppStr "where"],
277 if null uprags then ppNil else ppNest 4 (ppr sty uprags),
278 ppNest 4 (ppr sty binds),
279 ppNest 4 (ppr sty pragmas) ]
282 A type for recording what instances the user wants to specialise;
283 called a ``Sig'' because it's sort of like a ``type signature'' for an
286 data SpecInstSig name
287 = SpecInstSig name -- class
288 (MonoType name) -- type to specialise to
291 instance (NamedThing name, Outputable name)
292 => Outputable (SpecInstSig name) where
294 ppr sty (SpecInstSig clas ty _)
295 = ppCat [ppStr "{-# SPECIALIZE instance", ppr sty clas, ppr sty ty, ppStr "#-}"]
298 %************************************************************************
300 \subsection[DefaultDecl]{A @default@ declaration}
302 %************************************************************************
304 There can only be one default declaration per module, but it is hard
305 for the parser to check that; we pass them all through in the abstract
306 syntax, and that restriction must be checked in the front end.
309 data DefaultDecl name
310 = DefaultDecl [MonoType name]
313 instance (NamedThing name, Outputable name)
314 => Outputable (DefaultDecl name) where
316 ppr sty (DefaultDecl tys src_loc)
317 = ppBeside (ppPStr SLIT("default ")) (ppParens (interpp'SP sty tys))