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 BasicTypes ( Fixity, NewOrData(..) )
28 import Name ( getOccName, OccName, NamedThing(..) )
29 import Outputable ( interppSP, interpp'SP,
30 PprStyle(..), Outputable(..){-instance * []-}
33 import SrcLoc ( SrcLoc )
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)
56 hsDeclName :: (NamedThing name, Outputable name, Outputable pat,
57 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
58 => HsDecl tyvar uvar name pat -> name
60 hsDeclName (TyD (TyData _ _ name _ _ _ _ _)) = name
61 hsDeclName (TyD (TySynonym name _ _ _)) = name
62 hsDeclName (ClD (ClassDecl _ name _ _ _ _ _)) = name
63 hsDeclName (SigD (IfaceSig name _ _ _)) = name
64 hsDeclName (InstD (InstDecl _ _ _ (Just name) _)) = name
65 -- Others don't make sense
67 hsDeclName x = pprPanic "HsDecls.hsDeclName" (ppr PprDebug x)
72 instance (NamedThing name, Outputable name, Outputable pat,
73 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
74 => Outputable (HsDecl tyvar uvar name pat) where
76 ppr sty (TyD td) = ppr sty td
77 ppr sty (ClD cd) = ppr sty cd
78 ppr sty (SigD sig) = ppr sty sig
79 ppr sty (ValD binds) = ppr sty binds
80 ppr sty (DefD def) = ppr sty def
81 ppr sty (InstD inst) = ppr sty inst
84 instance (Ord3 name, Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar,
85 NamedThing name, Outputable name, Outputable pat) =>
86 Ord3 (HsDecl tyvar uvar name pat) where
88 instance (Ord3 name) => Ord3 (HsDecl tyvar uvar name pat) where
90 d1 `cmp` d2 = hsDeclName d1 `cmp` hsDeclName d2
94 %************************************************************************
96 \subsection[FixityDecl]{A fixity declaration}
98 %************************************************************************
101 data FixityDecl name = FixityDecl name Fixity SrcLoc
103 instance Outputable name => Outputable (FixityDecl name) where
104 ppr sty (FixityDecl name fixity loc) = sep [ppr sty fixity, ppr sty name]
108 %************************************************************************
110 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
112 %************************************************************************
117 (Context name) -- context
118 name -- type constructor
119 [HsTyVar name] -- type variables
120 [ConDecl name] -- data constructors (empty if abstract)
121 (Maybe [name]) -- derivings; Nothing => not specified
122 -- (i.e., derive default); Just [] => derive
123 -- *nothing*; Just <list> => as you would
128 | TySynonym name -- type constructor
129 [HsTyVar name] -- type variables
130 (HsType name) -- synonym expansion
136 instance (NamedThing name, Outputable name)
137 => Outputable (TyDecl name) where
139 ppr sty (TySynonym tycon tyvars mono_ty src_loc)
140 = hang (pp_decl_head sty SLIT("type") empty tycon tyvars)
143 ppr sty (TyData new_or_data context tycon tyvars condecls derivings pragmas src_loc)
145 (pp_decl_head sty keyword (pp_context_and_arrow sty context) tycon tyvars)
146 (pp_condecls sty condecls)
149 keyword = case new_or_data of
150 NewType -> SLIT("newtype")
151 DataType -> SLIT("data")
153 pp_decl_head sty str pp_context tycon tyvars
154 = hsep [ptext str, pp_context, ppr sty tycon,
155 interppSP sty tyvars, ptext SLIT("=")]
157 pp_condecls sty [] = empty -- Curious!
158 pp_condecls sty (c:cs)
159 = sep (ppr sty c : map (\ c -> (<>) (ptext SLIT("| ")) (ppr sty c)) cs)
161 pp_tydecl sty pp_head pp_decl_rhs derivings
162 = hang pp_head 4 (sep [
164 case (derivings, sty) of
166 (_,PprInterface) -> empty -- No derivings in interfaces
167 (Just ds,_) -> hsep [ptext SLIT("deriving"), parens (interpp'SP sty ds)]
170 pp_context_and_arrow :: Outputable name => PprStyle -> Context name -> Doc
171 pp_context_and_arrow sty [] = empty
172 pp_context_and_arrow sty theta = hsep [pprContext sty theta, ptext SLIT("=>")]
175 A type for recording what types a datatype should be specialised to.
176 It's called a ``Sig'' because it's sort of like a ``type signature''
177 for an datatype declaration.
180 data SpecDataSig name
181 = SpecDataSig name -- tycon to specialise
185 instance (NamedThing name, Outputable name)
186 => Outputable (SpecDataSig name) where
188 ppr sty (SpecDataSig tycon ty _)
189 = hsep [text "{-# SPECIALIZE data", ppr sty ty, text "#-}"]
192 %************************************************************************
194 \subsection[ConDecl]{A data-constructor declaration}
196 %************************************************************************
200 = ConDecl name -- Constructor name
201 (Context name) -- Existential context for this constructor
206 = VanillaCon -- prefix-style con decl
209 | InfixCon -- infix-style con decl
213 | RecCon -- record-style con decl
214 [([name], BangType name)] -- list of "fields"
216 | NewCon -- newtype con decl
220 = Banged (HsType name) -- HsType: to allow Haskell extensions
221 | Unbanged (HsType name) -- (MonoType only needed for straight Haskell)
225 instance (NamedThing name, Outputable name) => Outputable (ConDecl name) where
226 ppr sty (ConDecl con cxt con_details loc)
227 = pp_context_and_arrow sty cxt <+> ppr_con_details sty con con_details
229 ppr_con_details sty con (InfixCon ty1 ty2)
230 = hsep [ppr_bang sty ty1, ppr sty con, ppr_bang sty ty2]
232 ppr_con_details sty con (VanillaCon tys)
233 = ppr sty con <+> hsep (map (ppr_bang sty) tys)
235 ppr_con_details sty con (NewCon ty)
236 = ppr sty con <+> pprParendHsType sty ty
238 ppr_con_details sty con (RecCon fields)
239 = ppr sty con <+> braces (hsep (punctuate comma (map ppr_field fields)))
241 ppr_field (ns, ty) = hsep (map (ppr sty) ns) <+>
245 ppr_bang sty (Banged ty) = ptext SLIT("!") <> pprParendHsType sty ty
246 ppr_bang sty (Unbanged ty) = pprParendHsType sty ty
249 %************************************************************************
251 \subsection[ClassDecl]{A class declaration}
253 %************************************************************************
256 data ClassDecl tyvar uvar name pat
257 = ClassDecl (Context name) -- context...
258 name -- name of the class
259 (HsTyVar name) -- the class type variable
260 [Sig name] -- methods' signatures
261 (MonoBinds tyvar uvar name pat) -- default methods
267 instance (NamedThing name, Outputable name, Outputable pat,
268 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
269 => Outputable (ClassDecl tyvar uvar name pat) where
271 ppr sty (ClassDecl context clas tyvar sigs methods pragmas src_loc)
272 | null sigs -- No "where" part
275 | otherwise -- Laid out
276 = sep [hsep [top_matter, ptext SLIT("where {")],
277 nest 4 (vcat [sep (map ppr_sig sigs),
281 top_matter = hsep [ptext SLIT("class"), pp_context_and_arrow sty context,
282 ppr sty clas, ppr sty tyvar]
283 ppr_sig sig = ppr sty sig <> semi
286 %************************************************************************
288 \subsection[InstDecl]{An instance declaration (also, @SpecInstSig@)}
290 %************************************************************************
293 data InstDecl tyvar uvar name pat
294 = InstDecl (HsType name) -- Context => Class Instance-type
295 -- Using a polytype means that the renamer conveniently
296 -- figures out the quantified type variables for us.
298 (MonoBinds tyvar uvar name pat)
300 [Sig name] -- User-supplied pragmatic info
302 (Maybe name) -- Name for the dictionary function
308 instance (NamedThing name, Outputable name, Outputable pat,
309 Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
310 => Outputable (InstDecl tyvar uvar name pat) where
312 ppr sty (InstDecl inst_ty binds uprags dfun_name src_loc)
313 | case sty of { PprInterface -> True; other -> False} ||
314 nullMonoBinds binds && null uprags
315 = hsep [ptext SLIT("instance"), ppr sty inst_ty]
318 = vcat [hsep [ptext SLIT("instance"), ppr sty inst_ty, ptext SLIT("where")],
319 nest 4 (ppr sty uprags),
320 nest 4 (ppr sty binds) ]
323 A type for recording what instances the user wants to specialise;
324 called a ``Sig'' because it's sort of like a ``type signature'' for an
327 data SpecInstSig name
328 = SpecInstSig name -- class
329 (HsType name) -- type to specialise to
332 instance (NamedThing name, Outputable name)
333 => Outputable (SpecInstSig name) where
335 ppr sty (SpecInstSig clas ty _)
336 = hsep [text "{-# SPECIALIZE instance", ppr sty clas, ppr sty ty, text "#-}"]
339 %************************************************************************
341 \subsection[DefaultDecl]{A @default@ declaration}
343 %************************************************************************
345 There can only be one default declaration per module, but it is hard
346 for the parser to check that; we pass them all through in the abstract
347 syntax, and that restriction must be checked in the front end.
350 data DefaultDecl name
351 = DefaultDecl [HsType name]
354 instance (NamedThing name, Outputable name)
355 => Outputable (DefaultDecl name) where
357 ppr sty (DefaultDecl tys src_loc)
358 = (<>) (ptext SLIT("default ")) (parens (interpp'SP sty tys))
361 %************************************************************************
363 \subsection{Signatures in interface files}
365 %************************************************************************
374 instance (NamedThing name, Outputable name) => Outputable (IfaceSig name) where
375 ppr sty (IfaceSig var ty _ _)
376 = hang (hsep [ppr sty var, ptext SLIT("::")])
381 | HsStrictness (HsStrictnessInfo name)
382 | HsUnfold Bool (UfExpr name) -- True <=> INLINE pragma
383 | HsUpdate UpdateInfo
384 | HsArgUsage ArgUsageInfo
385 | HsFBType FBTypeInfo
386 -- ToDo: specialisations
388 data HsStrictnessInfo name
389 = HsStrictnessInfo [Demand]
390 (Maybe (name, [name])) -- Worker, if any
391 -- and needed constructors