remove Haddock-lexing/parsing/renaming from GHC
[ghc-hetmet.git] / compiler / hsSyn / HsTypes.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 HsTypes: Abstract syntax: user-defined types
7
8 \begin{code}
9 module HsTypes (
10         HsType(..), LHsType, 
11         HsTyVarBndr(..), LHsTyVarBndr,
12         HsExplicitForAll(..),
13         HsContext, LHsContext,
14         HsPred(..), LHsPred,
15
16         LBangType, BangType, HsBang(..), 
17         getBangType, getBangStrictness, 
18
19         ConDeclField(..), pprConDeclFields,
20         
21         mkExplicitHsForAllTy, mkImplicitHsForAllTy, hsExplicitTvs,
22         hsTyVarName, hsTyVarNames, replaceTyVarName,
23         hsLTyVarName, hsLTyVarNames, hsLTyVarLocName, hsLTyVarLocNames,
24         splitHsInstDeclTy, splitHsFunType,
25         
26         -- Type place holder
27         PostTcType, placeHolderType,
28
29         -- Printing
30         pprParendHsType, pprHsForAll, pprHsContext, ppr_hs_context, pprHsTyVarBndr
31     ) where
32
33 import {-# SOURCE #-} HsExpr ( HsSplice, pprSplice )
34
35 import Type
36 import HsDoc
37 import BasicTypes
38 import SrcLoc
39 import StaticFlags
40 import Outputable
41 import FastString
42 \end{code}
43
44
45 %************************************************************************
46 %*                                                                      *
47 \subsection{Annotating the syntax}
48 %*                                                                      *
49 %************************************************************************
50
51 \begin{code}
52 type PostTcType = Type          -- Used for slots in the abstract syntax
53                                 -- where we want to keep slot for a type
54                                 -- to be added by the type checker...but
55                                 -- before typechecking it's just bogus
56
57 placeHolderType :: PostTcType   -- Used before typechecking
58 placeHolderType  = panic "Evaluated the place holder for a PostTcType"
59 \end{code}
60
61 %************************************************************************
62 %*                                                                      *
63 \subsection{Bang annotations}
64 %*                                                                      *
65 %************************************************************************
66
67 \begin{code}
68 type LBangType name = Located (BangType name)
69 type BangType name  = HsType name       -- Bangs are in the HsType data type
70
71 data HsBang = HsNoBang  -- Only used as a return value for getBangStrictness,
72                         -- never appears on a HsBangTy
73             | HsStrict  -- ! 
74             | HsUnbox   -- {-# UNPACK #-} ! (GHC extension, meaning "unbox")
75
76 instance Outputable HsBang where
77     ppr (HsNoBang) = empty
78     ppr (HsStrict) = char '!'
79     ppr (HsUnbox)  = ptext (sLit "!!")
80
81 getBangType :: LHsType a -> LHsType a
82 getBangType (L _ (HsBangTy _ ty)) = ty
83 getBangType ty                    = ty
84
85 getBangStrictness :: LHsType a -> HsBang
86 getBangStrictness (L _ (HsBangTy s _)) = s
87 getBangStrictness _                    = HsNoBang
88 \end{code}
89
90
91 %************************************************************************
92 %*                                                                      *
93 \subsection{Data types}
94 %*                                                                      *
95 %************************************************************************
96
97 This is the syntax for types as seen in type signatures.
98
99 \begin{code}
100 type LHsContext name = Located (HsContext name)
101
102 type HsContext name = [LHsPred name]
103
104 type LHsPred name = Located (HsPred name)
105
106 data HsPred name = HsClassP name [LHsType name]          -- class constraint
107                  | HsEqualP (LHsType name) (LHsType name)-- equality constraint
108                  | HsIParam (IPName name) (LHsType name)
109
110 type LHsType name = Located (HsType name)
111
112 data HsType name
113   = HsForAllTy  HsExplicitForAll        -- Renamer leaves this flag unchanged, to record the way
114                                         -- the user wrote it originally, so that the printer can
115                                         -- print it as the user wrote it
116                 [LHsTyVarBndr name]     -- With ImplicitForAll, this is the empty list
117                                         -- until the renamer fills in the variables
118                 (LHsContext name)
119                 (LHsType name)
120
121   | HsTyVar             name            -- Type variable or type constructor
122
123   | HsAppTy             (LHsType name)
124                         (LHsType name)
125
126   | HsFunTy             (LHsType name)   -- function type
127                         (LHsType name)
128
129   | HsListTy            (LHsType name)  -- Element type
130
131   | HsPArrTy            (LHsType name)  -- Elem. type of parallel array: [:t:]
132
133   | HsTupleTy           Boxity
134                         [LHsType name]  -- Element types (length gives arity)
135
136   | HsOpTy              (LHsType name) (Located name) (LHsType name)
137
138   | HsParTy             (LHsType name)   
139         -- Parenthesis preserved for the precedence re-arrangement in RnTypes
140         -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
141         -- 
142         -- However, NB that toHsType doesn't add HsParTys (in an effort to keep
143         -- interface files smaller), so when printing a HsType we may need to
144         -- add parens.  
145
146   | HsNumTy             Integer         -- Generics only
147
148   | HsPredTy            (HsPred name)   -- Only used in the type of an instance
149                                         -- declaration, eg.  Eq [a] -> Eq a
150                                         --                             ^^^^
151                                         --                            HsPredTy
152                                         -- Note no need for location info on the
153                                         -- enclosed HsPred; the one on the type will do
154
155   | HsKindSig           (LHsType name)  -- (ty :: kind)
156                         Kind            -- A type with a kind signature
157
158   | HsSpliceTy          (HsSplice name)
159
160   | HsDocTy             (LHsType name) LHsDocString -- A documented type
161
162   | HsBangTy    HsBang (LHsType name)   -- Bang-style type annotations 
163   | HsRecTy [ConDeclField name]         -- Only in data type declarations
164
165 data HsExplicitForAll = Explicit | Implicit
166
167
168
169 data ConDeclField name  -- Record fields have Haddoc docs on them
170   = ConDeclField { cd_fld_name :: Located name,
171                    cd_fld_type :: LBangType name, 
172                    cd_fld_doc  :: Maybe LHsDocString }
173
174
175 -----------------------
176 -- Combine adjacent for-alls. 
177 -- The following awkward situation can happen otherwise:
178 --      f :: forall a. ((Num a) => Int)
179 -- might generate HsForAll (Just [a]) [] (HsForAll Nothing [Num a] t)
180 -- Then a isn't discovered as ambiguous, and we abstract the AbsBinds wrt []
181 -- but the export list abstracts f wrt [a].  Disaster.
182 --
183 -- A valid type must have one for-all at the top of the type, or of the fn arg types
184
185 mkImplicitHsForAllTy ::                        LHsContext name -> LHsType name -> HsType name
186 mkExplicitHsForAllTy :: [LHsTyVarBndr name] -> LHsContext name -> LHsType name -> HsType name
187 mkImplicitHsForAllTy     ctxt ty = mkHsForAllTy Implicit [] ctxt ty
188 mkExplicitHsForAllTy tvs ctxt ty = mkHsForAllTy Explicit tvs ctxt ty
189
190 mkHsForAllTy :: HsExplicitForAll -> [LHsTyVarBndr name] -> LHsContext name -> LHsType name -> HsType name
191 -- Smart constructor for HsForAllTy
192 mkHsForAllTy exp tvs (L _ []) ty = mk_forall_ty exp tvs ty
193 mkHsForAllTy exp tvs ctxt ty = HsForAllTy exp tvs ctxt ty
194
195 -- mk_forall_ty makes a pure for-all type (no context)
196 mk_forall_ty :: HsExplicitForAll -> [LHsTyVarBndr name] -> LHsType name -> HsType name
197 mk_forall_ty exp  tvs  (L _ (HsParTy ty))                   = mk_forall_ty exp tvs ty
198 mk_forall_ty exp1 tvs1 (L _ (HsForAllTy exp2 tvs2 ctxt ty)) = mkHsForAllTy (exp1 `plus` exp2) (tvs1 ++ tvs2) ctxt ty
199 mk_forall_ty exp  tvs  ty                                   = HsForAllTy exp tvs (L noSrcSpan []) ty
200         -- Even if tvs is empty, we still make a HsForAll!
201         -- In the Implicit case, this signals the place to do implicit quantification
202         -- In the Explicit case, it prevents implicit quantification    
203         --      (see the sigtype production in Parser.y.pp)
204         --      so that (forall. ty) isn't implicitly quantified
205
206 plus :: HsExplicitForAll -> HsExplicitForAll -> HsExplicitForAll
207 Implicit `plus` Implicit = Implicit
208 _        `plus` _        = Explicit
209
210 hsExplicitTvs :: LHsType name -> [name]
211 -- The explicitly-given forall'd type variables of a HsType
212 hsExplicitTvs (L _ (HsForAllTy Explicit tvs _ _)) = hsLTyVarNames tvs
213 hsExplicitTvs _                                   = []
214
215 ---------------------
216 type LHsTyVarBndr name = Located (HsTyVarBndr name)
217
218 data HsTyVarBndr name
219   = UserTyVar name
220   | KindedTyVar name Kind
221         --  *** NOTA BENE *** A "monotype" in a pragma can have
222         -- for-alls in it, (mostly to do with dictionaries).  These
223         -- must be explicitly Kinded.
224
225 hsTyVarName :: HsTyVarBndr name -> name
226 hsTyVarName (UserTyVar n)     = n
227 hsTyVarName (KindedTyVar n _) = n
228
229 hsLTyVarName :: LHsTyVarBndr name -> name
230 hsLTyVarName = hsTyVarName . unLoc
231
232 hsTyVarNames :: [HsTyVarBndr name] -> [name]
233 hsTyVarNames tvs = map hsTyVarName tvs
234
235 hsLTyVarNames :: [LHsTyVarBndr name] -> [name]
236 hsLTyVarNames = map hsLTyVarName
237
238 hsLTyVarLocName :: LHsTyVarBndr name -> Located name
239 hsLTyVarLocName = fmap hsTyVarName
240
241 hsLTyVarLocNames :: [LHsTyVarBndr name] -> [Located name]
242 hsLTyVarLocNames = map hsLTyVarLocName
243
244 replaceTyVarName :: HsTyVarBndr name1 -> name2 -> HsTyVarBndr name2
245 replaceTyVarName (UserTyVar _)     n' = UserTyVar n'
246 replaceTyVarName (KindedTyVar _ k) n' = KindedTyVar n' k
247 \end{code}
248
249
250 \begin{code}
251 splitHsInstDeclTy 
252     :: OutputableBndr name
253     => HsType name 
254     -> ([LHsTyVarBndr name], HsContext name, name, [LHsType name])
255         -- Split up an instance decl type, returning the pieces
256
257 splitHsInstDeclTy inst_ty
258   = case inst_ty of
259         HsParTy (L _ ty)              -> splitHsInstDeclTy ty
260         HsForAllTy _ tvs cxt (L _ ty) -> split_tau tvs (unLoc cxt) ty
261         other                         -> split_tau []  []          other
262     -- The type vars should have been computed by now, even if they were implicit
263   where
264     split_tau tvs cxt (HsPredTy (HsClassP cls tys)) = (tvs, cxt, cls, tys)
265     split_tau tvs cxt (HsParTy (L _ ty))            = split_tau tvs cxt ty
266     split_tau _ _ _ = pprPanic "splitHsInstDeclTy" (ppr inst_ty)
267
268 -- Splits HsType into the (init, last) parts
269 -- Breaks up any parens in the result type: 
270 --      splitHsFunType (a -> (b -> c)) = ([a,b], c)
271 splitHsFunType :: LHsType name -> ([LHsType name], LHsType name)
272 splitHsFunType (L _ (HsFunTy x y)) = (x:args, res)
273   where
274   (args, res) = splitHsFunType y
275 splitHsFunType (L _ (HsParTy ty))  = splitHsFunType ty
276 splitHsFunType other               = ([], other)
277 \end{code}
278
279
280 %************************************************************************
281 %*                                                                      *
282 \subsection{Pretty printing}
283 %*                                                                      *
284 %************************************************************************
285
286 \begin{code}
287 instance (OutputableBndr name) => Outputable (HsType name) where
288     ppr ty = pprHsType ty
289
290 instance (Outputable name) => Outputable (HsTyVarBndr name) where
291     ppr (UserTyVar name)        = ppr name
292     ppr (KindedTyVar name kind) = pprHsTyVarBndr name kind
293
294 instance OutputableBndr name => Outputable (HsPred name) where
295     ppr (HsClassP clas tys) = ppr clas <+> hsep (map pprLHsType tys)
296     ppr (HsEqualP t1 t2)    = hsep [pprLHsType t1, ptext (sLit "~"), 
297                                     pprLHsType t2]
298     ppr (HsIParam n ty)     = hsep [ppr n, dcolon, ppr ty]
299
300 pprLHsType :: OutputableBndr name => LHsType name -> SDoc
301 pprLHsType = pprParendHsType . unLoc
302
303 pprHsTyVarBndr :: Outputable name => name -> Kind -> SDoc
304 pprHsTyVarBndr name kind | isLiftedTypeKind kind = ppr name
305                          | otherwise             = hsep [ppr name, dcolon, pprParendKind kind]
306
307 pprHsForAll :: OutputableBndr name => HsExplicitForAll -> [LHsTyVarBndr name] ->  LHsContext name -> SDoc
308 pprHsForAll exp tvs cxt 
309   | show_forall = forall_part <+> pprHsContext (unLoc cxt)
310   | otherwise   = pprHsContext (unLoc cxt)
311   where
312     show_forall =  opt_PprStyle_Debug
313                 || (not (null tvs) && is_explicit)
314     is_explicit = case exp of {Explicit -> True; Implicit -> False}
315     forall_part = ptext (sLit "forall") <+> interppSP tvs <> dot
316
317 pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc
318 pprHsContext []  = empty
319 pprHsContext cxt = ppr_hs_context cxt <+> ptext (sLit "=>")
320
321 ppr_hs_context :: (OutputableBndr name) => HsContext name -> SDoc
322 ppr_hs_context []  = empty
323 ppr_hs_context cxt = parens (interpp'SP cxt)
324
325 pprConDeclFields :: OutputableBndr name => [ConDeclField name] -> SDoc
326 pprConDeclFields fields = braces (sep (punctuate comma (map ppr_fld fields)))
327   where
328     ppr_fld (ConDeclField { cd_fld_name = n, cd_fld_type = ty, 
329                             cd_fld_doc = doc })
330         = ppr n <+> dcolon <+> ppr ty <+> ppr_mbDoc doc
331 \end{code}
332
333 \begin{code}
334 pREC_TOP, pREC_FUN, pREC_OP, pREC_CON :: Int
335 pREC_TOP = 0  -- type   in ParseIface.y
336 pREC_FUN = 1  -- btype  in ParseIface.y
337               -- Used for LH arg of (->)
338 pREC_OP  = 2  -- Used for arg of any infix operator
339               -- (we don't keep their fixities around)
340 pREC_CON = 3  -- Used for arg of type applicn:
341               -- always parenthesise unless atomic
342
343 maybeParen :: Int       -- Precedence of context
344            -> Int       -- Precedence of top-level operator
345            -> SDoc -> SDoc      -- Wrap in parens if (ctxt >= op)
346 maybeParen ctxt_prec op_prec p | ctxt_prec >= op_prec = parens p
347                                | otherwise            = p
348         
349 -- printing works more-or-less as for Types
350
351 pprHsType, pprParendHsType :: (OutputableBndr name) => HsType name -> SDoc
352
353 pprHsType ty       = getPprStyle $ \sty -> ppr_mono_ty pREC_TOP (prepare sty ty)
354 pprParendHsType ty = ppr_mono_ty pREC_CON ty
355
356 -- Before printing a type
357 -- (a) Remove outermost HsParTy parens
358 -- (b) Drop top-level for-all type variables in user style
359 --     since they are implicit in Haskell
360 prepare :: PprStyle -> HsType name -> HsType name
361 prepare sty (HsParTy ty)          = prepare sty (unLoc ty)
362 prepare _   ty                    = ty
363
364 ppr_mono_lty :: (OutputableBndr name) => Int -> LHsType name -> SDoc
365 ppr_mono_lty ctxt_prec ty = ppr_mono_ty ctxt_prec (unLoc ty)
366
367 ppr_mono_ty :: (OutputableBndr name) => Int -> HsType name -> SDoc
368 ppr_mono_ty ctxt_prec (HsForAllTy exp tvs ctxt ty)
369   = maybeParen ctxt_prec pREC_FUN $
370     sep [pprHsForAll exp tvs ctxt, ppr_mono_lty pREC_TOP ty]
371
372 ppr_mono_ty _         (HsBangTy b ty)     = ppr b <> ppr ty
373 ppr_mono_ty _         (HsRecTy flds)      = pprConDeclFields flds
374 ppr_mono_ty _         (HsTyVar name)      = ppr name
375 ppr_mono_ty ctxt_prec (HsFunTy ty1 ty2)   = ppr_fun_ty ctxt_prec ty1 ty2
376 ppr_mono_ty _         (HsTupleTy con tys) = tupleParens con (interpp'SP tys)
377 ppr_mono_ty _         (HsKindSig ty kind) = parens (ppr_mono_lty pREC_TOP ty <+> dcolon <+> pprKind kind)
378 ppr_mono_ty _         (HsListTy ty)       = brackets (ppr_mono_lty pREC_TOP ty)
379 ppr_mono_ty _         (HsPArrTy ty)       = pabrackets (ppr_mono_lty pREC_TOP ty)
380 ppr_mono_ty _         (HsPredTy pred)     = ppr pred
381 ppr_mono_ty _         (HsNumTy n)         = integer n  -- generics only
382 ppr_mono_ty _         (HsSpliceTy s)      = pprSplice s
383
384 ppr_mono_ty ctxt_prec (HsAppTy fun_ty arg_ty)
385   = maybeParen ctxt_prec pREC_CON $
386     hsep [ppr_mono_lty pREC_FUN fun_ty, ppr_mono_lty pREC_CON arg_ty]
387
388 ppr_mono_ty ctxt_prec (HsOpTy ty1 op ty2)  
389   = maybeParen ctxt_prec pREC_OP $
390     ppr_mono_lty pREC_OP ty1 <+> ppr op <+> ppr_mono_lty pREC_OP ty2
391
392 ppr_mono_ty _         (HsParTy ty)
393   = parens (ppr_mono_lty pREC_TOP ty)
394   -- Put the parens in where the user did
395   -- But we still use the precedence stuff to add parens because
396   --    toHsType doesn't put in any HsParTys, so we may still need them
397
398 ppr_mono_ty ctxt_prec (HsDocTy ty doc) 
399   = maybeParen ctxt_prec pREC_OP $
400     ppr_mono_lty pREC_OP ty <+> ppr (unLoc doc)
401   -- we pretty print Haddock comments on types as if they were
402   -- postfix operators
403
404 --------------------------
405 ppr_fun_ty :: (OutputableBndr name) => Int -> LHsType name -> LHsType name -> SDoc
406 ppr_fun_ty ctxt_prec ty1 ty2
407   = let p1 = ppr_mono_lty pREC_FUN ty1
408         p2 = ppr_mono_lty pREC_TOP ty2
409     in
410     maybeParen ctxt_prec pREC_FUN $
411     sep [p1, ptext (sLit "->") <+> p2]
412
413 --------------------------
414 pabrackets :: SDoc -> SDoc
415 pabrackets p = ptext (sLit "[:") <> p <> ptext (sLit ":]")
416 \end{code}
417
418