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 UniqFM ( addToUFM_Directly, lookupUFM_Directly{-, ufmToList ToDo:rm-} )
55 import Unique ( pprUnique10, pprUnique, incrUnique, listTyConKey )
60 instance (Eq tyvar, Outputable tyvar,
61 Eq uvar, Outputable uvar ) => Outputable (GenType tyvar uvar) where
62 ppr sty ty = pprGenType sty ty
64 instance Outputable TyCon where
65 ppr sty tycon = pprTyCon sty tycon
67 instance Outputable (GenClass tyvar uvar) where
68 -- we use pprIfaceClass for printing in interfaces
69 ppr sty (Class u n _ _ _ _ _ _ _ _) = ppr sty n
71 instance Outputable ty => Outputable (GenClassOp ty) where
72 ppr sty clsop = pprGenClassOp sty clsop
74 instance Outputable (GenTyVar flexi) where
75 ppr sty tv = pprGenTyVar sty tv
77 -- and two SPECIALIZEd ones:
78 instance Outputable {-Type, i.e.:-}(GenType TyVar UVar) where
79 ppr sty ty = pprGenType sty ty
81 instance Outputable {-TyVar, i.e.:-}(GenTyVar Usage) where
82 ppr sty ty = pprGenTyVar sty ty
85 %************************************************************************
87 \subsection[Type]{@Type@}
89 %************************************************************************
91 @pprGenType@ is the std @Type@ printer; the overloaded @ppr@ function is
92 defined to use this. @pprParendGenType@ is the same, except it puts
93 parens around the type, except for the atomic cases. @pprParendGenType@
94 works just by setting the initial context precedence very high.
97 pprGenType, pprParendGenType :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
98 => PprStyle -> GenType tyvar uvar -> Pretty
100 pprGenType sty ty = ppr_ty sty (init_ppr_env sty) tOP_PREC ty
101 pprParendGenType sty ty = ppr_ty sty (init_ppr_env sty) tYCON_PREC ty
103 pprType sty ty = ppr_ty sty (init_ppr_env sty) tOP_PREC (ty :: Type)
104 pprParendType sty ty = ppr_ty sty (init_ppr_env sty) tYCON_PREC (ty :: Type)
106 pprMaybeTy :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
107 => PprStyle -> Maybe (GenType tyvar uvar) -> Pretty
108 pprMaybeTy sty Nothing = ppChar '*'
109 pprMaybeTy sty (Just ty) = pprParendGenType sty ty
113 ppr_ty :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
114 => PprStyle -> PprEnv tyvar uvar bndr occ -> Int
115 -> GenType tyvar uvar
118 ppr_ty sty env ctxt_prec (TyVarTy tyvar)
119 = ppr_tyvar env tyvar
121 ppr_ty sty env ctxt_prec (TyConTy tycon usage)
124 ppr_ty sty env ctxt_prec ty@(ForAllTy _ _)
125 | showUserishTypes sty = ppr_ty sty env' ctxt_prec body_ty
127 | otherwise = ppSep [ ppPStr SLIT("_forall_"),
128 ppIntersperse pp'SP pp_tyvars,
130 ppr_ty sty env' ctxt_prec body_ty
133 (tyvars, body_ty) = splitForAllTy ty
134 env' = foldl add_tyvar env tyvars
135 pp_tyvars = map (ppr_tyvar env') tyvars
137 ppr_ty sty env ctxt_prec (ForAllUsageTy uv uvs ty)
138 = panic "ppr_ty:ForAllUsageTy"
140 ppr_ty sty env ctxt_prec ty@(FunTy (DictTy _ _ _) _ _)
141 | showUserishTypes sty
142 -- Print a nice looking context (Eq a, Text b) => ...
143 = ppSep [ppBeside (ppr_theta theta) (ppPStr SLIT(" =>")),
144 ppr_ty sty env ctxt_prec body_ty
147 (theta, body_ty) = splitRhoTy ty
149 ppr_theta = case sty of { PprInterface -> ppr_theta_2 ; _ -> ppr_theta_1 }
151 ppr_theta_1 [ct] = ppr_dict sty env tOP_PREC ct
152 ppr_theta_1 cts = ppParens (ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts))
154 ppr_theta_2 cts = ppBesides [ppStr "{{", ppInterleave ppComma (map (ppr_dict sty env tOP_PREC) cts), ppStr "}}"]
156 ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
157 -- We fiddle the precedences passed to left/right branches,
158 -- so that right associativity comes out nicely...
159 = maybeParen ctxt_prec fUN_PREC
160 (ppCat [ppr_ty sty env fUN_PREC ty1,
162 ppr_ty sty env tOP_PREC ty2])
164 ppr_ty sty env ctxt_prec ty@(AppTy _ _)
165 = ppr_corner sty env ctxt_prec fun_ty arg_tys
167 (fun_ty, arg_tys) = splitAppTy ty
169 ppr_ty sty env ctxt_prec (SynTy tycon tys expansion)
171 -- always expand types that squeak into C-variable names
172 = ppr_ty sty env ctxt_prec expansion
176 (ppr_app sty env ctxt_prec (ppr sty tycon) tys)
177 (ifPprShowAll sty (ppCat [ppStr " {- expansion:",
178 ppr_ty sty env tOP_PREC expansion,
181 ppr_ty sty env ctxt_prec (DictTy clas ty usage)
182 = ppr_dict sty env ctxt_prec (clas, ty)
184 -- Some help functions
185 ppr_corner sty env ctxt_prec (TyConTy FunTyCon usage) arg_tys
186 | length arg_tys == 2
187 = ppr_ty sty env ctxt_prec (FunTy ty1 ty2 usage)
189 (ty1:ty2:_) = arg_tys
191 ppr_corner sty env ctxt_prec (TyConTy (TupleTyCon _ _ a) usage) arg_tys
192 | not (codeStyle sty) -- no magic in that case
193 = --ASSERT(length arg_tys == a)
194 (if (length arg_tys /= a) then pprTrace "ppr_corner:" (ppCat [ppInt a, ppInterleave ppComma (map (pprGenType PprDebug) arg_tys)]) else id) $
195 ppBesides [ppLparen, arg_tys_w_commas, ppRparen]
197 arg_tys_w_commas = ppIntersperse pp'SP (map (ppr_ty sty env tOP_PREC) arg_tys)
199 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
200 | not (codeStyle sty) && uniqueOf tycon == listTyConKey
201 = ASSERT(length arg_tys == 1)
202 ppBesides [ppLbrack, ppr_ty sty env tOP_PREC ty1, ppRbrack]
206 ppr_corner sty env ctxt_prec (TyConTy tycon usage) arg_tys
207 = ppr_app sty env ctxt_prec (ppr sty tycon) arg_tys
209 ppr_corner sty env ctxt_prec (TyVarTy tyvar) arg_tys
210 = ppr_app sty env ctxt_prec (ppr_tyvar env tyvar) arg_tys
213 ppr_app sty env ctxt_prec pp_fun []
215 ppr_app sty env ctxt_prec pp_fun arg_tys
216 = maybeParen ctxt_prec tYCON_PREC (ppCat [pp_fun, arg_tys_w_spaces])
218 arg_tys_w_spaces = ppIntersperse ppSP (map (ppr_ty sty env tYCON_PREC) arg_tys)
221 ppr_dict sty env ctxt_prec (clas, ty)
222 = maybeParen ctxt_prec tYCON_PREC
223 (ppCat [ppr sty clas, ppr_ty sty env tYCON_PREC ty])
226 This stuff is effectively stubbed out for the time being
230 = initPprEnv sty b b b b b b b b b b b
232 b = panic "PprType:init_ppr_env"
234 ppr_tyvar env tyvar = ppr (pStyle env) tyvar
235 ppr_uvar env uvar = ppr (pStyle env) uvar
237 add_tyvar env tyvar = env
238 add_uvar env uvar = env
241 @ppr_ty@ takes an @Int@ that is the precedence of the context.
242 The precedence levels are:
244 \item[0:] What we start with.
245 \item[1:] Function application (@FunTys@).
246 \item[2:] Type constructors.
251 tOP_PREC = (0 :: Int)
252 fUN_PREC = (1 :: Int)
253 tYCON_PREC = (2 :: Int)
255 maybeParen ctxt_prec inner_prec pretty
256 | ctxt_prec < inner_prec = pretty
257 | otherwise = ppParens pretty
260 %************************************************************************
262 \subsection[TyVar]{@TyVar@}
264 %************************************************************************
267 pprGenTyVar sty (TyVar uniq kind name usage)
273 _ -> ppBesides [pp_name, ppStr "{-", pp_u, ppStr "-}"]
275 pp_u = pprUnique uniq
276 pp_name = case name of
277 Just n -> ppPStr (getLocalName n)
278 Nothing -> case kind of
279 TypeKind -> ppChar 'o'
280 BoxedTypeKind -> ppChar 't'
281 UnboxedTypeKind -> ppChar 'u'
282 ArrowKind _ _ -> ppChar 'a'
285 %************************************************************************
287 \subsection[TyCon]{@TyCon@}
289 %************************************************************************
291 ToDo; all this is suspiciously like getOccName!
294 showTyCon :: PprStyle -> TyCon -> String
295 showTyCon sty tycon = ppShow 80 (pprTyCon sty tycon)
299 then ppBesides (ppPStr SLIT("Prelude_") : map mangle x)
302 -- ToDo: really should be in CStrings
303 mangle '(' = ppPStr SLIT("Z40") -- decimal ascii #s
304 mangle ')' = ppPStr SLIT("Z41")
305 mangle '[' = ppPStr SLIT("Z91")
306 mangle ']' = ppPStr SLIT("Z93")
307 mangle ',' = ppPStr SLIT("Z44")
308 mangle '-' = ppPStr SLIT("Zm")
309 mangle '>' = ppPStr SLIT("Zg")
311 pprTyCon :: PprStyle -> TyCon -> Pretty
313 pprTyCon sty (PrimTyCon _ name _ _) = ppr sty name
315 pprTyCon sty FunTyCon = maybe_code sty "->"
316 pprTyCon sty (TupleTyCon _ _ arity) = case arity of
317 0 -> maybe_code sty "()"
318 2 -> maybe_code sty "(,)"
319 3 -> maybe_code sty "(,,)"
320 4 -> maybe_code sty "(,,,)"
321 5 -> maybe_code sty "(,,,,)"
322 n -> maybe_code sty ( "(" ++ nOfThem (n-1) ',' ++ ")" )
324 pprTyCon sty tycon@(DataTyCon uniq name kind tyvars ctxt cons derivings nd)
325 | uniq == listTyConKey
326 = maybe_code sty "[]"
330 pprTyCon sty (SpecTyCon tc ty_maybes)
331 = ppBeside (pprTyCon sty tc)
332 ((if (codeStyle sty) then identToC else ppPStr) tys_stuff)
334 tys_stuff = specMaybeTysSuffix ty_maybes
336 pprTyCon sty (SynTyCon uniq name kind arity tyvars expansion)
337 = ppBeside (ppr sty name)
339 (ppCat [ ppStr " {-",
341 interpp'SP sty tyvars,
342 pprParendGenType sty expansion,
347 %************************************************************************
349 \subsection[Class]{@Class@}
351 %************************************************************************
354 pprGenClassOp :: Outputable ty => PprStyle -> GenClassOp ty -> Pretty
356 pprGenClassOp sty op = ppr_class_op sty [] op
358 ppr_class_op sty tyvars (ClassOp op_name i ty)
361 PprForAsm _ _ -> pp_C
362 PprInterface -> pp_sigd
363 PprShowAll -> pp_sigd
366 pp_C = ppPStr op_name
367 pp_user = if isLexVarSym op_name && not (isLexSpecialSym op_name)
370 pp_sigd = ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
374 %************************************************************************
376 \subsection{Mumbo jumbo}
378 %************************************************************************
381 -- Shallowly magical; converts a type into something
382 -- vaguely close to what can be used in C identifier.
383 -- Produces things like what we have in mkCompoundName,
384 -- which can be "dot"ted together...
386 getTypeString :: Type -> [Either OrigName FAST_STRING]
389 = case (splitAppTy ty) of { (tc, args) ->
390 do_tc tc : map do_arg_ty args }
392 do_tc (TyConTy tc _) = Left (origName "do_tc" tc)
393 do_tc (SynTy _ _ ty) = do_tc ty
394 do_tc other = --pprTrace "getTypeString:do_tc:" (pprType PprDebug other) $
395 Right (_PK_ (ppShow 1000 (pprType PprForC other)))
397 do_arg_ty (TyConTy tc _) = Left (origName "do_arg_ty" tc)
398 do_arg_ty (TyVarTy tv) = Right (_PK_ (ppShow 80 (ppr PprForC tv)))
399 do_arg_ty (SynTy _ _ ty) = do_arg_ty ty
400 do_arg_ty other = --pprTrace "getTypeString:do_arg_ty:" (pprType PprDebug other) $
401 Right (_PK_ (ppShow 1000 (pprType PprForC other)))
403 -- PprForC expands type synonyms as it goes;
404 -- it also forces consistent naming of tycons
405 -- (e.g., can't have both "(,) a b" and "(a,b)":
406 -- must be consistent!
408 --------------------------------------------------
415 '-' : '>' : xs -> '-' : '>' : tidy (no_leading_sps xs)
416 other -> ' ' : tidy more
418 tidy (',' : more) = ',' : tidy (no_leading_sps more)
420 tidy (x : xs) = x : tidy xs -- catch all
422 no_leading_sps [] = []
423 no_leading_sps (' ':xs) = no_leading_sps xs
424 no_leading_sps other = other
426 typeMaybeString :: Maybe Type -> [Either OrigName FAST_STRING]
427 typeMaybeString Nothing = [Right SLIT("!")]
428 typeMaybeString (Just t) = getTypeString t
430 specMaybeTysSuffix :: [Maybe Type] -> FAST_STRING
431 specMaybeTysSuffix ty_maybes
432 = panic "PprType.specMaybeTysSuffix"
435 ty_strs = concat (map typeMaybeString ty_maybes)
436 dotted_tys = [ _CONS_ '.' str | str <- ty_strs ]
442 Grab a name for the type. This is used to determine the type
443 description for profiling.
445 getTyDescription :: Type -> String
448 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
451 AppTy fun _ -> getTyDescription fun
452 FunTy _ res _ -> '-' : '>' : fun_result res
453 TyConTy tycon _ -> _UNPK_ (getLocalName tycon)
454 SynTy tycon _ _ -> _UNPK_ (getLocalName tycon)
455 DictTy _ _ _ -> "dict"
456 _ -> pprPanic "getTyDescription: other" (pprType PprDebug tau_ty)
459 fun_result (FunTy _ res _) = '>' : fun_result res
460 fun_result other = getTyDescription other
465 nmbrType :: Type -> NmbrM Type
467 nmbrType (TyVarTy tv)
468 = nmbrTyVar tv `thenNmbr` \ new_tv ->
469 returnNmbr (TyVarTy new_tv)
471 nmbrType (AppTy t1 t2)
472 = nmbrType t1 `thenNmbr` \ new_t1 ->
473 nmbrType t2 `thenNmbr` \ new_t2 ->
474 returnNmbr (AppTy new_t1 new_t2)
476 nmbrType (TyConTy tc use)
477 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
478 nmbrUsage use `thenNmbr` \ new_use ->
479 returnNmbr (TyConTy tc new_use)
481 nmbrType (SynTy tc args expand)
482 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
483 mapNmbr nmbrType args `thenNmbr` \ new_args ->
484 nmbrType expand `thenNmbr` \ new_expand ->
485 returnNmbr (SynTy tc new_args new_expand)
487 nmbrType (ForAllTy tv ty)
488 = addTyVar tv `thenNmbr` \ new_tv ->
489 nmbrType ty `thenNmbr` \ new_ty ->
490 returnNmbr (ForAllTy new_tv new_ty)
492 nmbrType (ForAllUsageTy u us ty)
493 = addUVar u `thenNmbr` \ new_u ->
494 mapNmbr nmbrUVar us `thenNmbr` \ new_us ->
495 nmbrType ty `thenNmbr` \ new_ty ->
496 returnNmbr (ForAllUsageTy new_u new_us new_ty)
498 nmbrType (FunTy t1 t2 use)
499 = nmbrType t1 `thenNmbr` \ new_t1 ->
500 nmbrType t2 `thenNmbr` \ new_t2 ->
501 nmbrUsage use `thenNmbr` \ new_use ->
502 returnNmbr (FunTy new_t1 new_t2 new_use)
504 nmbrType (DictTy c ty use)
505 = --nmbrClass c `thenNmbr` \ new_c ->
506 nmbrType ty `thenNmbr` \ new_ty ->
507 nmbrUsage use `thenNmbr` \ new_use ->
508 returnNmbr (DictTy c new_ty new_use)
512 addTyVar, nmbrTyVar :: TyVar -> NmbrM TyVar
514 addTyVar tv@(TyVar u k maybe_name use) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
515 = --pprTrace "addTyVar:" (ppCat [pprUnique u, pprUnique ut]) $
516 case (lookupUFM_Directly tvenv u) of
517 Just xx -> -- pprTrace "addTyVar: already in map!" (ppr PprDebug tv) $
518 -- (It gets triggered when we do a datatype: first we
519 -- "addTyVar" the tyvars for the datatype as a whole;
520 -- we will subsequently "addId" the data cons, including
521 -- the type for each of them -- each of which includes
522 -- _forall_ ...tvs..., which we will addTyVar.
523 -- Harmless, if that's all that happens....
527 nenv_plus_tv = NmbrEnv ui (incrUnique ut) uu
529 (addToUFM_Directly tvenv u new_tv)
532 (nenv2, new_use) = nmbrUsage use nenv_plus_tv
534 new_tv = TyVar ut k maybe_name new_use
538 nmbrTyVar tv@(TyVar u _ _ _) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
539 = case (lookupUFM_Directly tvenv u) of
540 Just xx -> (nenv, xx)
542 --pprTrace "nmbrTyVar: lookup failed:" (ppCat (ppr PprDebug u : [ppCat [ppr PprDebug x, ppStr "=>", ppr PprDebug tv] | (x,tv) <- ufmToList tvenv])) $
546 nmbrTyCon : only called from ``top-level'', if you know what I mean.
548 nmbrTyCon tc@FunTyCon = returnNmbr tc
549 nmbrTyCon tc@(TupleTyCon _ _ _) = returnNmbr tc
550 nmbrTyCon tc@(PrimTyCon _ _ _ _) = returnNmbr tc
552 nmbrTyCon (DataTyCon u n k tvs theta cons clss nod)
553 = --pprTrace "nmbrDataTyCon:" (ppCat (map (ppr PprDebug) tvs)) $
554 mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
555 mapNmbr nmbr_theta theta `thenNmbr` \ new_theta ->
556 mapNmbr nmbrId cons `thenNmbr` \ new_cons ->
557 returnNmbr (DataTyCon u n k new_tvs new_theta new_cons clss nod)
560 = --nmbrClass c `thenNmbr` \ new_c ->
561 nmbrType t `thenNmbr` \ new_t ->
562 returnNmbr (c, new_t)
564 nmbrTyCon (SynTyCon u n k a tvs expand)
565 = mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
566 nmbrType expand `thenNmbr` \ new_expand ->
567 returnNmbr (SynTyCon u n k a new_tvs new_expand)
569 nmbrTyCon (SpecTyCon tc specs)
570 = mapNmbr nmbrMaybeTy specs `thenNmbr` \ new_specs ->
571 returnNmbr (SpecTyCon tc new_specs)
574 nmbrMaybeTy Nothing = returnNmbr Nothing
575 nmbrMaybeTy (Just t) = nmbrType t `thenNmbr` \ new_t ->
576 returnNmbr (Just new_t)
580 nmbrClass (Class u n tv supers ssels ops osels odefms instenv isupers)
581 = addTyVar tv `thenNmbr` \ new_tv ->
582 mapNmbr nmbr_op ops `thenNmbr` \ new_ops ->
583 returnNmbr (Class u n new_tv supers ssels new_ops osels odefms instenv isupers)
585 nmbr_op (ClassOp n tag ty)
586 = nmbrType ty `thenNmbr` \ new_ty ->
587 returnNmbr (ClassOp n tag new_ty)
591 nmbrUsage :: Usage -> NmbrM Usage
593 nmbrUsage u = returnNmbr u
595 nmbrUsage u@UsageOne = returnNmbr u
596 nmbrUsage u@UsageOmega = returnNmbr u
597 nmbrUsage (UsageVar u)
598 = nmbrUVar u `thenNmbr` \ new_u ->
599 returnNmbr (UsageVar new_u)
604 addUVar, nmbrUVar :: UVar -> NmbrM UVar
606 addUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
607 = case (lookupUFM_Directly uvenv u) of
608 Just xx -> trace "addUVar: already in map!" $
612 nenv_plus_uv = NmbrEnv ui ut (incrUnique uu)
615 (addToUFM_Directly uvenv u new_uv)
618 (nenv_plus_uv, new_uv)
620 nmbrUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
621 = case (lookupUFM_Directly uvenv u) of
622 Just xx -> (nenv, xx)
624 trace "nmbrUVar: lookup failed" $