2 % (c) The AQUA Project, Glasgow University, 1996
4 \section[PprType]{Printing Types, TyVars, Classes, ClassOps, TyCons}
7 #include "HsVersions.h"
10 GenTyVar, pprGenTyVar,
11 TyCon, pprTyCon, showTyCon,
13 pprGenType, pprParendGenType,
14 pprType, pprParendType,
21 GenClassOp, pprGenClassOp,
23 addTyVar{-ToDo:don't export-}, nmbrTyVar,
25 nmbrType, nmbrTyCon, nmbrClass
29 IMPORT_DELOOPER(IdLoop)
30 --IMPORT_DELOOPER(TyLoop) -- for paranoia checking
33 -- (PprType can see all the representations it's trying to print)
34 import Type ( GenType(..), maybeAppTyCon,
35 splitForAllTy, splitSigmaTy, splitRhoTy, splitAppTy )
36 import TyVar ( GenTyVar(..) )
37 import TyCon ( TyCon(..), NewOrData )
38 import Class ( SYN_IE(Class), GenClass(..),
39 SYN_IE(ClassOp), GenClassOp(..) )
40 import Kind ( Kind(..) )
41 import Usage ( pprUVar, GenUsage(..), SYN_IE(Usage), SYN_IE(UVar) )
44 import CStrings ( identToC )
45 import CmdLineOpts ( opt_OmitInterfacePragmas )
46 import Maybes ( maybeToBool )
47 import Name ( isLexVarSym, isLexSpecialSym, origName, moduleOf,
48 getLocalName, Name{-instance Outputable-}
50 import Outputable ( ifPprShowAll, interpp'SP )
52 import PprStyle ( PprStyle(..), codeStyle, showUserishTypes )
54 import TysWiredIn ( listTyCon )
55 import UniqFM ( addToUFM_Directly, lookupUFM_Directly, ufmToList{-ToDo:rm-} )
56 import Unique ( pprUnique10, pprUnique, incrUnique, listTyConKey )
61 instance (Eq tyvar, Outputable tyvar,
62 Eq uvar, Outputable uvar ) => Outputable (GenType tyvar uvar) where
63 ppr sty ty = pprGenType sty ty
65 instance Outputable TyCon where
66 ppr sty tycon = pprTyCon sty tycon
68 instance Outputable (GenClass tyvar uvar) where
69 -- we use pprIfaceClass for printing in interfaces
70 ppr sty (Class u n _ _ _ _ _ _ _ _) = ppr sty n
72 instance Outputable ty => Outputable (GenClassOp ty) where
73 ppr sty clsop = pprGenClassOp sty clsop
75 instance Outputable (GenTyVar flexi) where
76 ppr sty tv = pprGenTyVar sty tv
78 -- and two SPECIALIZEd ones:
79 instance Outputable {-Type, i.e.:-}(GenType TyVar UVar) where
80 ppr sty ty = pprGenType sty ty
82 instance Outputable {-TyVar, i.e.:-}(GenTyVar Usage) where
83 ppr sty ty = pprGenTyVar sty ty
86 %************************************************************************
88 \subsection[Type]{@Type@}
90 %************************************************************************
92 @pprGenType@ is the std @Type@ printer; the overloaded @ppr@ function is
93 defined to use this. @pprParendGenType@ is the same, except it puts
94 parens around the type, except for the atomic cases. @pprParendGenType@
95 works just by setting the initial context precedence very high.
98 pprGenType, pprParendGenType :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
99 => PprStyle -> GenType tyvar uvar -> Pretty
101 pprGenType sty ty = ppr_ty sty (init_ppr_env sty) tOP_PREC ty
102 pprParendGenType sty ty = ppr_ty sty (init_ppr_env sty) tYCON_PREC ty
104 pprType sty ty = ppr_ty sty (init_ppr_env sty) tOP_PREC (ty :: Type)
105 pprParendType sty ty = ppr_ty sty (init_ppr_env sty) tYCON_PREC (ty :: Type)
107 pprMaybeTy :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
108 => PprStyle -> Maybe (GenType tyvar uvar) -> Pretty
109 pprMaybeTy sty Nothing = ppChar '*'
110 pprMaybeTy sty (Just ty) = pprParendGenType sty ty
114 ppr_ty :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
115 => PprStyle -> PprEnv tyvar uvar bndr occ -> Int
116 -> GenType tyvar uvar
119 ppr_ty sty env ctxt_prec (TyVarTy tyvar)
120 = ppr_tyvar env tyvar
122 ppr_ty sty env ctxt_prec (TyConTy tycon usage)
125 ppr_ty sty env ctxt_prec ty@(ForAllTy _ _)
126 | showUserishTypes sty = ppr_ty sty env' ctxt_prec body_ty
128 | otherwise = ppSep [ ppPStr SLIT("_forall_"),
129 ppIntersperse pp'SP pp_tyvars,
131 ppr_ty sty env' ctxt_prec body_ty
134 (tyvars, body_ty) = splitForAllTy ty
135 env' = foldl add_tyvar env tyvars
136 pp_tyvars = map (ppr_tyvar env') tyvars
138 ppr_ty sty env ctxt_prec (ForAllUsageTy uv uvs ty)
139 = panic "ppr_ty:ForAllUsageTy"
141 ppr_ty sty env ctxt_prec ty@(FunTy (DictTy _ _ _) _ _)
142 | showUserishTypes sty
143 -- Print a nice looking context (Eq a, Text b) => ...
144 = ppSep [ppBeside (ppr_theta theta) (ppPStr SLIT(" =>")),
145 ppr_ty sty env ctxt_prec body_ty
148 (theta, body_ty) = splitRhoTy ty
150 ppr_theta = case sty of { PprInterface -> ppr_theta_2 ; _ -> ppr_theta_1 }
152 ppr_theta_1 [ct] = ppr_dict sty env tOP_PREC ct
153 ppr_theta_1 cts = ppParens (ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts))
155 ppr_theta_2 cts = ppBesides [ppStr "{{", ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts), ppStr "}}"]
157 ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
158 -- We fiddle the precedences passed to left/right branches,
159 -- so that right associativity comes out nicely...
160 = maybeParen ctxt_prec fUN_PREC
161 (ppCat [ppr_ty sty env fUN_PREC ty1,
163 ppr_ty sty env tOP_PREC ty2])
165 ppr_ty sty env ctxt_prec ty@(AppTy _ _)
166 = ppr_corner sty env ctxt_prec fun_ty arg_tys
168 (fun_ty, arg_tys) = splitAppTy ty
170 ppr_ty sty env ctxt_prec (SynTy tycon tys expansion)
172 -- always expand types that squeak into C-variable names
173 = ppr_ty sty env ctxt_prec expansion
177 (ppr_app sty env ctxt_prec (ppr sty tycon) tys)
178 (ifPprShowAll sty (ppCat [ppStr " {- expansion:",
179 ppr_ty sty env tOP_PREC expansion,
182 ppr_ty sty env ctxt_prec (DictTy clas ty usage)
183 = ppr_dict sty env ctxt_prec (clas, ty)
185 -- Some help functions
186 ppr_corner sty env ctxt_prec (TyConTy FunTyCon usage) arg_tys
187 | length arg_tys == 2
188 = ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
190 (ty1:ty2:_) = arg_tys
192 ppr_corner sty env ctxt_prec (TyConTy (TupleTyCon _ _ a) usage) arg_tys
193 | not (codeStyle sty) -- no magic in that case
194 = --ASSERT(length arg_tys == a)
195 (if (length arg_tys /= a) then pprTrace "ppr_corner:" (ppCat [ppInt a, ppInterleave ppComma (map (pprGenType PprDebug) arg_tys)]) else id) $
196 ppBesides [ppLparen, arg_tys_w_commas, ppRparen]
198 arg_tys_w_commas = ppIntersperse pp'SP (map (ppr_ty sty env tOP_PREC) arg_tys)
200 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
201 | not (codeStyle sty) && tycon == listTyCon
202 = ASSERT(length arg_tys == 1)
203 ppBesides [ppLbrack, ppr_ty sty env tOP_PREC ty1, ppRbrack]
207 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
208 = ppr_app sty env ctxt_prec (ppr sty tycon) arg_tys
210 ppr_corner sty env ctxt_prec (TyVarTy tyvar) arg_tys
211 = ppr_app sty env ctxt_prec (ppr_tyvar env tyvar) arg_tys
214 ppr_app sty env ctxt_prec pp_fun []
216 ppr_app sty env ctxt_prec pp_fun arg_tys
217 = maybeParen ctxt_prec tYCON_PREC (ppCat [pp_fun, arg_tys_w_spaces])
219 arg_tys_w_spaces = ppIntersperse ppSP (map (ppr_ty sty env tYCON_PREC) arg_tys)
222 ppr_dict sty env ctxt_prec (clas, ty)
223 = maybeParen ctxt_prec tYCON_PREC
224 (ppCat [ppr sty clas, ppr_ty sty env tYCON_PREC ty])
227 This stuff is effectively stubbed out for the time being
231 = initPprEnv sty b b b b b b b b b b b
233 b = panic "PprType:init_ppr_env"
235 ppr_tyvar env tyvar = ppr (pStyle env) tyvar
236 ppr_uvar env uvar = ppr (pStyle env) uvar
238 add_tyvar env tyvar = env
239 add_uvar env uvar = env
242 @ppr_ty@ takes an @Int@ that is the precedence of the context.
243 The precedence levels are:
245 \item[0:] What we start with.
246 \item[1:] Function application (@FunTys@).
247 \item[2:] Type constructors.
252 tOP_PREC = (0 :: Int)
253 fUN_PREC = (1 :: Int)
254 tYCON_PREC = (2 :: Int)
256 maybeParen ctxt_prec inner_prec pretty
257 | ctxt_prec < inner_prec = pretty
258 | otherwise = ppParens pretty
261 %************************************************************************
263 \subsection[TyVar]{@TyVar@}
265 %************************************************************************
268 pprGenTyVar sty (TyVar uniq kind name usage)
274 _ -> ppBesides [pp_name, ppStr "{-", pp_u, ppStr "-}"]
276 pp_u = pprUnique uniq
277 pp_name = case name of
278 Just n -> ppPStr (getLocalName n)
279 Nothing -> case kind of
280 TypeKind -> ppChar 'o'
281 BoxedTypeKind -> ppChar 't'
282 UnboxedTypeKind -> ppChar 'u'
283 ArrowKind _ _ -> ppChar 'a'
286 %************************************************************************
288 \subsection[TyCon]{@TyCon@}
290 %************************************************************************
292 ToDo; all this is suspiciously like getOccName!
295 showTyCon :: PprStyle -> TyCon -> String
296 showTyCon sty tycon = ppShow 80 (pprTyCon sty tycon)
300 then ppBesides (ppPStr SLIT("Prelude_") : map mangle x)
303 -- ToDo: really should be in CStrings
304 mangle '(' = ppPStr SLIT("Z40") -- decimal ascii #s
305 mangle ')' = ppPStr SLIT("Z41")
306 mangle '[' = ppPStr SLIT("Z91")
307 mangle ']' = ppPStr SLIT("Z93")
308 mangle ',' = ppPStr SLIT("Z44")
309 mangle '-' = ppPStr SLIT("Zm")
310 mangle '>' = ppPStr SLIT("Zg")
312 pprTyCon :: PprStyle -> TyCon -> Pretty
314 pprTyCon sty (PrimTyCon _ name _ _) = ppr sty name
316 pprTyCon sty FunTyCon = maybe_code sty "->"
317 pprTyCon sty (TupleTyCon _ _ arity) = case arity of
318 0 -> maybe_code sty "()"
319 2 -> maybe_code sty "(,)"
320 3 -> maybe_code sty "(,,)"
321 4 -> maybe_code sty "(,,,)"
322 5 -> maybe_code sty "(,,,,)"
323 n -> maybe_code sty ( "(" ++ nOfThem (n-1) ',' ++ ")" )
325 pprTyCon sty tycon@(DataTyCon uniq name kind tyvars ctxt cons derivings nd)
326 | uniq == listTyConKey
327 = maybe_code sty "[]"
331 pprTyCon sty (SpecTyCon tc ty_maybes)
332 = ppBeside (pprTyCon sty tc)
333 ((if (codeStyle sty) then identToC else ppPStr) tys_stuff)
335 tys_stuff = specMaybeTysSuffix ty_maybes
337 pprTyCon sty (SynTyCon uniq name kind arity tyvars expansion)
338 = ppBeside (ppr sty name)
340 (ppCat [ ppStr " {-",
342 interpp'SP sty tyvars,
343 pprParendGenType sty expansion,
348 %************************************************************************
350 \subsection[Class]{@Class@}
352 %************************************************************************
355 pprGenClassOp :: Outputable ty => PprStyle -> GenClassOp ty -> Pretty
357 pprGenClassOp sty op = ppr_class_op sty [] op
359 ppr_class_op sty tyvars (ClassOp op_name i ty)
362 PprForAsm _ _ -> pp_C
363 PprInterface -> pp_sigd
364 PprShowAll -> pp_sigd
367 pp_C = ppPStr op_name
368 pp_user = if isLexVarSym op_name && not (isLexSpecialSym op_name)
371 pp_sigd = ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
375 %************************************************************************
377 \subsection{Mumbo jumbo}
379 %************************************************************************
382 -- Shallowly magical; converts a type into something
383 -- vaguely close to what can be used in C identifier.
384 -- Produces things like what we have in mkCompoundName,
385 -- which can be "dot"ted together...
387 getTypeString :: Type -> [Either OrigName FAST_STRING]
390 = case (splitAppTy ty) of { (tc, args) ->
391 do_tc tc : map do_arg_ty args }
393 do_tc (TyConTy tc _) = Left (origName "do_tc" tc)
394 do_tc (SynTy _ _ ty) = do_tc ty
395 do_tc other = --pprTrace "getTypeString:do_tc:" (pprType PprDebug other) $
396 Right (_PK_ (ppShow 1000 (pprType PprForC other)))
398 do_arg_ty (TyConTy tc _) = Left (origName "do_arg_ty" tc)
399 do_arg_ty (TyVarTy tv) = Right (_PK_ (ppShow 80 (ppr PprForC tv)))
400 do_arg_ty (SynTy _ _ ty) = do_arg_ty ty
401 do_arg_ty other = --pprTrace "getTypeString:do_arg_ty:" (pprType PprDebug other) $
402 Right (_PK_ (ppShow 1000 (pprType PprForC other)))
404 -- PprForC expands type synonyms as it goes;
405 -- it also forces consistent naming of tycons
406 -- (e.g., can't have both "(,) a b" and "(a,b)":
407 -- must be consistent!
409 --------------------------------------------------
416 '-' : '>' : xs -> '-' : '>' : tidy (no_leading_sps xs)
417 other -> ' ' : tidy more
419 tidy (',' : more) = ',' : tidy (no_leading_sps more)
421 tidy (x : xs) = x : tidy xs -- catch all
423 no_leading_sps [] = []
424 no_leading_sps (' ':xs) = no_leading_sps xs
425 no_leading_sps other = other
427 typeMaybeString :: Maybe Type -> [Either OrigName FAST_STRING]
428 typeMaybeString Nothing = [Right SLIT("!")]
429 typeMaybeString (Just t) = getTypeString t
431 specMaybeTysSuffix :: [Maybe Type] -> FAST_STRING
432 specMaybeTysSuffix ty_maybes
433 = panic "PprType.specMaybeTysSuffix"
436 ty_strs = concat (map typeMaybeString ty_maybes)
437 dotted_tys = [ _CONS_ '.' str | str <- ty_strs ]
443 Grab a name for the type. This is used to determine the type
444 description for profiling.
446 getTyDescription :: Type -> String
449 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
452 AppTy fun _ -> getTyDescription fun
453 FunTy _ res _ -> '-' : '>' : fun_result res
454 TyConTy tycon _ -> _UNPK_ (getLocalName tycon)
455 SynTy tycon _ _ -> _UNPK_ (getLocalName tycon)
456 DictTy _ _ _ -> "dict"
457 _ -> pprPanic "getTyDescription: other" (pprType PprDebug tau_ty)
460 fun_result (FunTy _ res _) = '>' : fun_result res
461 fun_result other = getTyDescription other
466 nmbrType :: Type -> NmbrM Type
468 nmbrType (TyVarTy tv)
469 = nmbrTyVar tv `thenNmbr` \ new_tv ->
470 returnNmbr (TyVarTy new_tv)
472 nmbrType (AppTy t1 t2)
473 = nmbrType t1 `thenNmbr` \ new_t1 ->
474 nmbrType t2 `thenNmbr` \ new_t2 ->
475 returnNmbr (AppTy new_t1 new_t2)
477 nmbrType (TyConTy tc use)
478 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
479 nmbrUsage use `thenNmbr` \ new_use ->
480 returnNmbr (TyConTy tc new_use)
482 nmbrType (SynTy tc args expand)
483 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
484 mapNmbr nmbrType args `thenNmbr` \ new_args ->
485 nmbrType expand `thenNmbr` \ new_expand ->
486 returnNmbr (SynTy tc new_args new_expand)
488 nmbrType (ForAllTy tv ty)
489 = addTyVar tv `thenNmbr` \ new_tv ->
490 nmbrType ty `thenNmbr` \ new_ty ->
491 returnNmbr (ForAllTy new_tv new_ty)
493 nmbrType (ForAllUsageTy u us ty)
494 = addUVar u `thenNmbr` \ new_u ->
495 mapNmbr nmbrUVar us `thenNmbr` \ new_us ->
496 nmbrType ty `thenNmbr` \ new_ty ->
497 returnNmbr (ForAllUsageTy new_u new_us new_ty)
499 nmbrType (FunTy t1 t2 use)
500 = nmbrType t1 `thenNmbr` \ new_t1 ->
501 nmbrType t2 `thenNmbr` \ new_t2 ->
502 nmbrUsage use `thenNmbr` \ new_use ->
503 returnNmbr (FunTy new_t1 new_t2 new_use)
505 nmbrType (DictTy c ty use)
506 = --nmbrClass c `thenNmbr` \ new_c ->
507 nmbrType ty `thenNmbr` \ new_ty ->
508 nmbrUsage use `thenNmbr` \ new_use ->
509 returnNmbr (DictTy c new_ty new_use)
513 addTyVar, nmbrTyVar :: TyVar -> NmbrM TyVar
515 addTyVar tv@(TyVar u k maybe_name use) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
516 = --pprTrace "addTyVar:" (ppCat [pprUnique u, pprUnique ut]) $
517 case (lookupUFM_Directly tvenv u) of
518 Just xx -> -- pprTrace "addTyVar: already in map!" (ppr PprDebug tv) $
519 -- (It gets triggered when we do a datatype: first we
520 -- "addTyVar" the tyvars for the datatype as a whole;
521 -- we will subsequently "addId" the data cons, including
522 -- the type for each of them -- each of which includes
523 -- _forall_ ...tvs..., which we will addTyVar.
524 -- Harmless, if that's all that happens....
528 nenv_plus_tv = NmbrEnv ui (incrUnique ut) uu
530 (addToUFM_Directly tvenv u new_tv)
533 (nenv2, new_use) = nmbrUsage use nenv_plus_tv
535 new_tv = TyVar ut k maybe_name new_use
539 nmbrTyVar tv@(TyVar u _ _ _) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
540 = case (lookupUFM_Directly tvenv u) of
541 Just xx -> (nenv, xx)
543 pprTrace "nmbrTyVar: lookup failed:" (ppCat (ppr PprDebug u : [ppCat [ppr PprDebug x, ppStr "=>", ppr PprDebug tv] | (x,tv) <- ufmToList tvenv])) $
547 nmbrTyCon : only called from ``top-level'', if you know what I mean.
549 nmbrTyCon tc@FunTyCon = returnNmbr tc
550 nmbrTyCon tc@(TupleTyCon _ _ _) = returnNmbr tc
551 nmbrTyCon tc@(PrimTyCon _ _ _ _) = returnNmbr tc
553 nmbrTyCon (DataTyCon u n k tvs theta cons clss nod)
554 = --pprTrace "nmbrDataTyCon:" (ppCat (map (ppr PprDebug) tvs)) $
555 mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
556 mapNmbr nmbr_theta theta `thenNmbr` \ new_theta ->
557 mapNmbr nmbrId cons `thenNmbr` \ new_cons ->
558 returnNmbr (DataTyCon u n k new_tvs new_theta new_cons clss nod)
561 = --nmbrClass c `thenNmbr` \ new_c ->
562 nmbrType t `thenNmbr` \ new_t ->
563 returnNmbr (c, new_t)
565 nmbrTyCon (SynTyCon u n k a tvs expand)
566 = mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
567 nmbrType expand `thenNmbr` \ new_expand ->
568 returnNmbr (SynTyCon u n k a new_tvs new_expand)
570 nmbrTyCon (SpecTyCon tc specs)
571 = mapNmbr nmbrMaybeTy specs `thenNmbr` \ new_specs ->
572 returnNmbr (SpecTyCon tc new_specs)
575 nmbrMaybeTy Nothing = returnNmbr Nothing
576 nmbrMaybeTy (Just t) = nmbrType t `thenNmbr` \ new_t ->
577 returnNmbr (Just new_t)
581 nmbrClass (Class u n tv supers ssels ops osels odefms instenv isupers)
582 = addTyVar tv `thenNmbr` \ new_tv ->
583 mapNmbr nmbr_op ops `thenNmbr` \ new_ops ->
584 returnNmbr (Class u n new_tv supers ssels new_ops osels odefms instenv isupers)
586 nmbr_op (ClassOp n tag ty)
587 = nmbrType ty `thenNmbr` \ new_ty ->
588 returnNmbr (ClassOp n tag new_ty)
592 nmbrUsage :: Usage -> NmbrM Usage
594 nmbrUsage u = returnNmbr u
596 nmbrUsage u@UsageOne = returnNmbr u
597 nmbrUsage u@UsageOmega = returnNmbr u
598 nmbrUsage (UsageVar u)
599 = nmbrUVar u `thenNmbr` \ new_u ->
600 returnNmbr (UsageVar new_u)
605 addUVar, nmbrUVar :: UVar -> NmbrM UVar
607 addUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
608 = case (lookupUFM_Directly uvenv u) of
609 Just xx -> trace "addUVar: already in map!" $
613 nenv_plus_uv = NmbrEnv ui ut (incrUnique uu)
616 (addToUFM_Directly uvenv u new_uv)
619 (nenv_plus_uv, new_uv)
621 nmbrUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
622 = case (lookupUFM_Directly uvenv u) of
623 Just xx -> (nenv, xx)
625 trace "nmbrUVar: lookup failed" $