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@.
12 #include "HsVersions.h"
15 import HsBinds ( HsBinds, MonoBinds, Sig, nullMonoBinds )
16 import HsPragmas ( DataPragmas, ClassPragmas,
17 InstancePragmas, ClassOpPragmas
20 import HsCore ( UfExpr )
21 import BasicTypes ( Fixity, NewOrData(..) )
22 import IdInfo ( ArgUsageInfo, FBTypeInfo, ArityInfo, UpdateInfo )
23 import Demand ( Demand )
26 import Name ( getOccName, OccName, NamedThing(..) )
28 import SrcLoc ( SrcLoc )
33 %************************************************************************
35 \subsection[HsDecl]{Declarations}
37 %************************************************************************
40 data HsDecl flexi name pat
42 | ClD (ClassDecl flexi name pat)
43 | InstD (InstDecl flexi name pat)
44 | DefD (DefaultDecl name)
45 | ValD (HsBinds flexi name pat)
46 | SigD (IfaceSig name)
51 hsDeclName :: (NamedThing name, Outputable name, Outputable pat)
52 => HsDecl flexi name pat -> name
54 hsDeclName (TyD (TyData _ _ name _ _ _ _ _)) = name
55 hsDeclName (TyD (TySynonym name _ _ _)) = name
56 hsDeclName (ClD (ClassDecl _ name _ _ _ _ _ _ _)) = name
57 hsDeclName (SigD (IfaceSig name _ _ _)) = name
58 hsDeclName (InstD (InstDecl _ _ _ (Just name) _)) = name
59 -- Others don't make sense
61 hsDeclName x = pprPanic "HsDecls.hsDeclName" (ppr x)
66 instance (NamedThing name, Outputable name, Outputable pat)
67 => Outputable (HsDecl flexi name pat) where
71 ppr (SigD sig) = ppr sig
72 ppr (ValD binds) = ppr binds
73 ppr (DefD def) = ppr def
74 ppr (InstD inst) = ppr inst
77 -- hsDeclName needs more context when DEBUG is on
78 instance (NamedThing name, Outputable name, Outputable pat, Eq name)
79 => Eq (HsDecl flex name pat) where
80 d1 == d2 = hsDeclName d1 == hsDeclName d2
82 instance (NamedThing name, Outputable name, Outputable pat, Ord name)
83 => Ord (HsDecl flex name pat) where
84 d1 `compare` d2 = hsDeclName d1 `compare` hsDeclName d2
86 instance (Eq name) => Eq (HsDecl flex name pat) where
87 d1 == d2 = hsDeclName d1 == hsDeclName d2
89 instance (Ord name) => Ord (HsDecl flexi name pat) where
90 d1 `compare` d2 = hsDeclName d1 `compare` hsDeclName d2
95 %************************************************************************
97 \subsection[FixityDecl]{A fixity declaration}
99 %************************************************************************
102 data FixityDecl name = FixityDecl name Fixity SrcLoc
104 instance Outputable name => Outputable (FixityDecl name) where
105 ppr (FixityDecl name fixity loc) = sep [ppr fixity, ppr name]
109 %************************************************************************
111 \subsection[TyDecl]{@data@, @newtype@ or @type@ (synonym) type declaration}
113 %************************************************************************
118 (Context name) -- context
119 name -- type constructor
120 [HsTyVar name] -- type variables
121 [ConDecl name] -- data constructors (empty if abstract)
122 (Maybe [name]) -- derivings; Nothing => not specified
123 -- (i.e., derive default); Just [] => derive
124 -- *nothing*; Just <list> => as you would
129 | TySynonym name -- type constructor
130 [HsTyVar name] -- type variables
131 (HsType name) -- synonym expansion
137 instance (NamedThing name, Outputable name)
138 => Outputable (TyDecl name) where
140 ppr (TySynonym tycon tyvars mono_ty src_loc)
141 = hang (pp_decl_head SLIT("type") empty tycon tyvars)
144 ppr (TyData new_or_data context tycon tyvars condecls derivings pragmas src_loc)
146 (pp_decl_head keyword (pp_context_and_arrow context) tycon tyvars)
147 (pp_condecls condecls)
150 keyword = case new_or_data of
151 NewType -> SLIT("newtype")
152 DataType -> SLIT("data")
154 pp_decl_head str pp_context tycon tyvars
155 = hsep [ptext str, pp_context, ppr tycon,
156 interppSP tyvars, ptext SLIT("=")]
158 pp_condecls [] = empty -- Curious!
160 = sep (ppr c : map (\ c -> (<>) (ptext SLIT("| ")) (ppr c)) cs)
162 pp_tydecl pp_head pp_decl_rhs derivings
163 = hang pp_head 4 (sep [
167 Just ds -> hsep [ptext SLIT("deriving"), parens (interpp'SP ds)]
170 pp_context_and_arrow :: Outputable name => Context name -> SDoc
171 pp_context_and_arrow [] = empty
172 pp_context_and_arrow theta = hsep [pprContext 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 (SpecDataSig tycon ty _)
189 = hsep [text "{-# SPECIALIZE data", ppr 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 (ConDecl con cxt con_details loc)
227 = pp_context_and_arrow cxt <+> ppr_con_details con con_details
229 ppr_con_details con (InfixCon ty1 ty2)
230 = hsep [ppr_bang ty1, ppr con, ppr_bang ty2]
232 ppr_con_details con (VanillaCon tys)
233 = ppr con <+> hsep (map (ppr_bang) tys)
235 ppr_con_details con (NewCon ty)
236 = ppr con <+> pprParendHsType ty
238 ppr_con_details con (RecCon fields)
239 = ppr con <+> braces (hsep (punctuate comma (map ppr_field fields)))
241 ppr_field (ns, ty) = hsep (map (ppr) ns) <+>
245 ppr_bang (Banged ty) = ptext SLIT("!") <> pprParendHsType ty
246 ppr_bang (Unbanged ty) = pprParendHsType ty
249 %************************************************************************
251 \subsection[ClassDecl]{A class declaration}
253 %************************************************************************
256 data ClassDecl flexi name pat
257 = ClassDecl (Context name) -- context...
258 name -- name of the class
259 [HsTyVar name] -- the class type variables
260 [Sig name] -- methods' signatures
261 (MonoBinds flexi name pat) -- default methods
263 name name -- The names of the tycon and datacon for this class
264 -- These are filled in by the renamer
269 instance (NamedThing name, Outputable name, Outputable pat)
270 => Outputable (ClassDecl flexi name pat) where
272 ppr (ClassDecl context clas tyvars sigs methods pragmas _ _ src_loc)
273 | null sigs -- No "where" part
276 | otherwise -- Laid out
277 = sep [hsep [top_matter, ptext SLIT("where {")],
278 nest 4 (vcat [sep (map ppr_sig sigs),
282 top_matter = hsep [ptext SLIT("class"), pp_context_and_arrow context,
283 ppr clas, hsep (map (ppr) tyvars)]
284 ppr_sig sig = ppr sig <> semi
287 %************************************************************************
289 \subsection[InstDecl]{An instance declaration (also, @SpecInstSig@)}
291 %************************************************************************
294 data InstDecl flexi name pat
295 = InstDecl (HsType name) -- Context => Class Instance-type
296 -- Using a polytype means that the renamer conveniently
297 -- figures out the quantified type variables for us.
299 (MonoBinds flexi name pat)
301 [Sig name] -- User-supplied pragmatic info
303 (Maybe name) -- Name for the dictionary function
309 instance (NamedThing name, Outputable name, Outputable pat)
310 => Outputable (InstDecl flexi name pat) where
312 ppr (InstDecl inst_ty binds uprags dfun_name src_loc)
313 = getPprStyle $ \ sty ->
314 if ifaceStyle sty || (nullMonoBinds binds && null uprags) then
315 hsep [ptext SLIT("instance"), ppr inst_ty]
317 vcat [hsep [ptext SLIT("instance"), ppr inst_ty, ptext SLIT("where")],
322 A type for recording what instances the user wants to specialise;
323 called a ``Sig'' because it's sort of like a ``type signature'' for an
326 data SpecInstSig name
327 = SpecInstSig name -- class
328 (HsType name) -- type to specialise to
331 instance (NamedThing name, Outputable name)
332 => Outputable (SpecInstSig name) where
334 ppr (SpecInstSig clas ty _)
335 = hsep [text "{-# SPECIALIZE instance", ppr clas, ppr ty, text "#-}"]
338 %************************************************************************
340 \subsection[DefaultDecl]{A @default@ declaration}
342 %************************************************************************
344 There can only be one default declaration per module, but it is hard
345 for the parser to check that; we pass them all through in the abstract
346 syntax, and that restriction must be checked in the front end.
349 data DefaultDecl name
350 = DefaultDecl [HsType name]
353 instance (NamedThing name, Outputable name)
354 => Outputable (DefaultDecl name) where
356 ppr (DefaultDecl tys src_loc)
357 = ptext SLIT("default") <+> parens (interpp'SP tys)
360 %************************************************************************
362 \subsection{Signatures in interface files}
364 %************************************************************************
373 instance (NamedThing name, Outputable name) => Outputable (IfaceSig name) where
374 ppr (IfaceSig var ty _ _)
375 = hang (hsep [ppr var, ptext SLIT("::")])
380 | HsStrictness (HsStrictnessInfo name)
381 | HsUnfold Bool (UfExpr name) -- True <=> INLINE pragma
382 | HsUpdate UpdateInfo
383 | HsArgUsage ArgUsageInfo
384 | HsFBType FBTypeInfo
385 -- ToDo: specialisations
387 data HsStrictnessInfo name
388 = HsStrictnessInfo [Demand]
389 (Maybe (name, [name])) -- Worker, if any
390 -- and needed constructors