2 % (c) The AQUA Project, Glasgow University, 1996
4 \section[PprType]{Printing Types, TyVars, Classes, ClassOps, TyCons}
7 #include "HsVersions.h"
10 GenTyVar, pprGenTyVar, pprTyVarBndr,
11 TyCon, pprTyCon, showTyCon,
13 pprGenType, pprParendGenType,
14 pprType, pprParendType,
20 GenClassOp, pprGenClassOp,
22 addTyVar{-ToDo:don't export-}, nmbrTyVar,
24 nmbrType, nmbrTyCon, nmbrClass
28 IMPORT_DELOOPER(IdLoop)
29 --IMPORT_DELOOPER(TyLoop) -- for paranoia checking
32 -- (PprType can see all the representations it's trying to print)
33 import Type ( GenType(..), maybeAppTyCon,
34 splitForAllTy, splitSigmaTy, splitRhoTy, splitAppTy )
35 import TyVar ( GenTyVar(..) )
36 import TyCon ( TyCon(..), NewOrData )
37 import Class ( SYN_IE(Class), GenClass(..),
38 SYN_IE(ClassOp), GenClassOp(..) )
39 import Kind ( Kind(..), isBoxedTypeKind, pprParendKind )
40 import Usage ( pprUVar, GenUsage(..), SYN_IE(Usage), SYN_IE(UVar) )
43 import CStrings ( identToC )
44 import CmdLineOpts ( opt_OmitInterfacePragmas )
45 import Maybes ( maybeToBool )
46 import Name ( nameString, Name{-instance Outputable-},
47 OccName, pprOccName, getOccString, pprNonSymOcc
49 import Outputable ( ifPprShowAll, interpp'SP )
51 import PprStyle ( PprStyle(..), codeStyle, showUserishTypes )
53 import UniqFM ( addToUFM_Directly, lookupUFM_Directly{-, ufmToList ToDo:rm-} )
54 import Unique ( pprUnique10, pprUnique, incrUnique, listTyConKey )
59 instance (Eq tyvar, Outputable tyvar,
60 Eq uvar, Outputable uvar ) => Outputable (GenType tyvar uvar) where
61 ppr sty ty = pprGenType sty ty
63 instance Outputable TyCon where
64 ppr sty tycon = pprTyCon sty tycon
66 instance Outputable (GenClass tyvar uvar) where
67 -- we use pprIfaceClass for printing in interfaces
68 ppr sty (Class u n _ _ _ _ _ _ _ _) = ppr sty n
70 instance Outputable ty => Outputable (GenClassOp ty) where
71 ppr sty clsop = pprGenClassOp sty clsop
73 instance Outputable (GenTyVar flexi) where
74 ppr sty tv = pprGenTyVar sty tv
76 -- and two SPECIALIZEd ones:
77 instance Outputable {-Type, i.e.:-}(GenType TyVar UVar) where
78 ppr sty ty = pprGenType sty ty
80 instance Outputable {-TyVar, i.e.:-}(GenTyVar Usage) where
81 ppr sty ty = pprGenTyVar sty ty
84 %************************************************************************
86 \subsection[Type]{@Type@}
88 %************************************************************************
90 @pprGenType@ is the std @Type@ printer; the overloaded @ppr@ function is
91 defined to use this. @pprParendGenType@ is the same, except it puts
92 parens around the type, except for the atomic cases. @pprParendGenType@
93 works just by setting the initial context precedence very high.
96 pprGenType, pprParendGenType :: (Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
97 => PprStyle -> GenType tyvar uvar -> Pretty
99 pprGenType sty ty = ppr_ty (init_ppr_env sty) tOP_PREC ty
100 pprParendGenType sty ty = ppr_ty (init_ppr_env sty) tYCON_PREC ty
102 pprType, pprParendType :: PprStyle -> Type -> Pretty
103 pprType sty ty = ppr_ty (init_ppr_env_type sty) tOP_PREC ty
104 pprParendType sty ty = ppr_ty (init_ppr_env_type sty) tYCON_PREC ty
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 :: PprEnv tyvar uvar bndr occ -> Int
114 -> GenType tyvar uvar
117 ppr_ty env ctxt_prec (TyVarTy tyvar)
120 ppr_ty env ctxt_prec (TyConTy tycon usage)
121 = ppr_tycon env tycon
123 ppr_ty env ctxt_prec ty@(ForAllTy _ _)
124 | show_forall = ppSep [ ppPStr SLIT("_forall_"), pp_tyvars,
125 pp_theta, ppPStr SLIT("=>"), pp_body
127 | null theta = pp_body
128 | otherwise = ppSep [pp_theta, ppPStr SLIT("=>"), pp_body]
130 (tyvars, rho_ty) = splitForAllTy ty
131 (theta, body_ty) | show_context = splitRhoTy rho_ty
132 | otherwise = ([], rho_ty)
134 pp_tyvars = ppBracket (ppIntersperse ppSP (map (pTyVarB env) tyvars))
135 pp_theta | null theta = ppNil
136 | otherwise = ppCurlies (ppInterleave ppComma (map (ppr_dict env tOP_PREC) theta))
137 pp_body = ppr_ty env ctxt_prec body_ty
140 show_forall = case sty of
144 show_context = case sty of
149 ppr_ty env ctxt_prec (ForAllUsageTy uv uvs ty)
150 = panic "ppr_ty:ForAllUsageTy"
152 ppr_ty env ctxt_prec (FunTy ty1 ty2 usage)
153 -- We fiddle the precedences passed to left/right branches,
154 -- so that right associativity comes out nicely...
155 = maybeParen ctxt_prec fUN_PREC
156 (ppCat [ppr_ty env fUN_PREC ty1,
158 ppr_ty env tOP_PREC ty2])
160 ppr_ty env ctxt_prec ty@(AppTy _ _)
161 = ppr_corner env ctxt_prec fun_ty arg_tys
163 (fun_ty, arg_tys) = splitAppTy ty
165 ppr_ty env ctxt_prec (SynTy tycon tys expansion)
166 | codeStyle (pStyle env)
167 -- always expand types that squeak into C-variable names
168 = ppr_ty env ctxt_prec expansion
172 (ppr_app env ctxt_prec (ppr_tycon env tycon) tys)
173 (ifPprShowAll (pStyle env) (ppCat [ppStr " {- expansion:",
174 ppr_ty env tOP_PREC expansion,
177 ppr_ty env ctxt_prec (DictTy clas ty usage)
178 = ppCurlies (ppr_dict env tOP_PREC (clas, ty))
179 -- Curlies are temporary
182 -- Some help functions
183 ppr_corner env ctxt_prec (TyConTy FunTyCon usage) arg_tys
184 | length arg_tys == 2
185 = ppr_ty env ctxt_prec (FunTy ty1 ty2 usage)
187 (ty1:ty2:_) = arg_tys
189 ppr_corner env ctxt_prec (TyConTy (TupleTyCon _ _ a) usage) arg_tys
190 | not (codeStyle (pStyle env)) -- no magic in that case
191 = --ASSERT(length arg_tys == a)
192 --(if (length arg_tys /= a) then pprTrace "ppr_corner:" (ppCat [ppInt a, ppInterleave ppComma (map (pprGenType PprDebug) arg_tys)]) else id) $
193 ppBesides [ppLparen, arg_tys_w_commas, ppRparen]
195 arg_tys_w_commas = ppIntersperse pp'SP (map (ppr_ty env tOP_PREC) arg_tys)
197 ppr_corner env ctxt_prec (TyConTy tycon usage) arg_tys
198 | not (codeStyle (pStyle env)) && uniqueOf tycon == listTyConKey
199 = ASSERT(length arg_tys == 1)
200 ppBesides [ppLbrack, ppr_ty env tOP_PREC ty1, ppRbrack]
204 ppr_corner env ctxt_prec (TyConTy tycon usage) arg_tys
205 = ppr_app env ctxt_prec (ppr_tycon env tycon) arg_tys
207 ppr_corner env ctxt_prec (TyVarTy tyvar) arg_tys
208 = ppr_app env ctxt_prec (pTyVarO env tyvar) arg_tys
211 ppr_app env ctxt_prec pp_fun []
213 ppr_app env ctxt_prec pp_fun arg_tys
214 = maybeParen ctxt_prec tYCON_PREC (ppCat [pp_fun, arg_tys_w_spaces])
216 arg_tys_w_spaces = ppIntersperse ppSP (map (ppr_ty env tYCON_PREC) arg_tys)
219 ppr_dict env ctxt_prec (clas, ty)
220 = maybeParen ctxt_prec tYCON_PREC
221 (ppCat [ppr_class env clas, ppr_ty env tYCON_PREC ty])
225 -- This one uses only "ppr"
227 = initPprEnv sty b b b b (Just (ppr sty)) (Just (ppr sty)) (Just (ppr sty)) b b b b b
229 b = panic "PprType:init_ppr_env"
231 -- This one uses pprTyVarBndr, and thus is specific to GenTyVar's types
232 init_ppr_env_type sty
233 = initPprEnv sty b b b b (Just (pprTyVarBndr sty)) (Just (ppr sty)) (Just (ppr sty)) b b b b b
235 b = panic "PprType:init_ppr_env"
237 ppr_tycon env tycon = ppr (pStyle env) tycon
238 ppr_class env clas = ppr (pStyle env) clas
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 -> pprOccName sty (getOccName n)
278 Nothing -> case kind of
279 TypeKind -> ppChar 'o'
280 BoxedTypeKind -> ppChar 't'
281 UnboxedTypeKind -> ppChar 'u'
282 ArrowKind _ _ -> ppChar 'a'
285 We print type-variable binders with their kinds in interface files.
288 pprTyVarBndr sty@PprInterface tyvar@(TyVar uniq kind name usage)
289 | not (isBoxedTypeKind kind)
290 = ppBesides [pprGenTyVar sty tyvar, ppStr "::", pprParendKind kind]
292 pprTyVarBndr sty tyvar = pprGenTyVar sty tyvar
295 %************************************************************************
297 \subsection[TyCon]{@TyCon@}
299 %************************************************************************
301 ToDo; all this is suspiciously like getOccName!
304 showTyCon :: PprStyle -> TyCon -> String
305 showTyCon sty tycon = ppShow 80 (pprTyCon sty tycon)
309 then ppBesides (ppPStr SLIT("Prelude_") : map mangle x)
312 -- ToDo: really should be in CStrings
313 mangle '(' = ppPStr SLIT("Z40") -- decimal ascii #s
314 mangle ')' = ppPStr SLIT("Z41")
315 mangle '[' = ppPStr SLIT("Z91")
316 mangle ']' = ppPStr SLIT("Z93")
317 mangle ',' = ppPStr SLIT("Z44")
318 mangle '-' = ppPStr SLIT("Zm")
319 mangle '>' = ppPStr SLIT("Zg")
321 pprTyCon :: PprStyle -> TyCon -> Pretty
322 pprTyCon sty tycon = ppr sty (getName tycon)
324 {- This old code looks suspicious to me.
325 Just printing the name should do the job; apart from the extra junk
328 Let's try and live without all this...
329 Delete in due course. SLPJ Nov 96
331 pprTyCon sty (PrimTyCon _ name _ _) = ppr sty name
333 pprTyCon sty FunTyCon = maybe_code sty "->"
334 pprTyCon sty (TupleTyCon _ _ arity) = case arity of
335 0 -> maybe_code sty "()"
336 2 -> maybe_code sty "(,)"
337 3 -> maybe_code sty "(,,)"
338 4 -> maybe_code sty "(,,,)"
339 5 -> maybe_code sty "(,,,,)"
340 n -> maybe_code sty ( "(" ++ nOfThem (n-1) ',' ++ ")" )
342 pprTyCon sty tycon@(DataTyCon uniq name kind tyvars ctxt cons derivings nd)
345 pprTyCon sty (SpecTyCon tc ty_maybes)
346 = ppBeside (pprTyCon sty tc)
347 ((if (codeStyle sty) then identToC else ppPStr) tys_stuff)
349 tys_stuff = specMaybeTysSuffix ty_maybes
351 pprTyCon sty (SynTyCon uniq name kind arity tyvars expansion)
352 = ppBeside (ppr sty name)
354 (ppCat [ ppStr " {-",
356 interpp'SP sty tyvars,
357 pprParendGenType sty expansion,
363 %************************************************************************
365 \subsection[Class]{@Class@}
367 %************************************************************************
370 pprGenClassOp :: Outputable ty => PprStyle -> GenClassOp ty -> Pretty
372 pprGenClassOp sty op = ppr_class_op sty [] op
374 ppr_class_op sty tyvars (ClassOp op_name i ty)
377 PprForAsm _ _ -> pp_C
378 PprInterface -> pp_sigd
379 PprShowAll -> pp_sigd
382 pp_C = ppr sty op_name
383 pp_user = pprNonSymOcc sty op_name
384 pp_sigd = ppCat [pp_user, ppPStr SLIT("::"), ppr sty ty]
388 %************************************************************************
390 \subsection{Mumbo jumbo}
392 %************************************************************************
395 -- Shallowly magical; converts a type into something
396 -- vaguely close to what can be used in C identifier.
397 -- Produces things like what we have in mkCompoundName,
398 -- which can be "dot"ted together...
400 getTypeString :: Type -> FAST_STRING
403 = case (splitAppTy ty) of { (tc, args) ->
404 _CONCAT_ (do_tc tc : map do_arg_ty args) }
406 do_tc (TyConTy tc _) = nameString (getName tc)
407 do_tc (SynTy _ _ ty) = do_tc ty
408 do_tc other = --pprTrace "getTypeString:do_tc:" (pprType PprDebug other) $
409 (_PK_ (ppShow 1000 (pprType PprForC other)))
411 do_arg_ty (TyConTy tc _) = nameString (getName tc)
412 do_arg_ty (TyVarTy tv) = _PK_ (ppShow 80 (ppr PprForC tv))
413 do_arg_ty (SynTy _ _ ty) = do_arg_ty ty
414 do_arg_ty other = --pprTrace "getTypeString:do_arg_ty:" (pprType PprDebug other) $
415 _PK_ (ppShow 1000 (pprType PprForC other))
417 -- PprForC expands type synonyms as it goes;
418 -- it also forces consistent naming of tycons
419 -- (e.g., can't have both "(,) a b" and "(a,b)":
420 -- must be consistent!
422 specMaybeTysSuffix :: [Maybe Type] -> FAST_STRING
423 specMaybeTysSuffix ty_maybes
424 = panic "PprType.specMaybeTysSuffix"
427 ty_strs = concat (map typeMaybeString ty_maybes)
428 dotted_tys = [ _CONS_ '.' str | str <- ty_strs ]
434 Grab a name for the type. This is used to determine the type
435 description for profiling.
437 getTyDescription :: Type -> String
440 = case (splitSigmaTy ty) of { (_, _, tau_ty) ->
443 AppTy fun _ -> getTyDescription fun
444 FunTy _ res _ -> '-' : '>' : fun_result res
445 TyConTy tycon _ -> getOccString tycon
446 SynTy tycon _ _ -> getOccString tycon
447 DictTy _ _ _ -> "dict"
448 ForAllTy _ ty -> getTyDescription ty
449 _ -> pprPanic "getTyDescription: other" (pprType PprDebug tau_ty)
452 fun_result (FunTy _ res _) = '>' : fun_result res
453 fun_result other = getTyDescription other
458 nmbrType :: Type -> NmbrM Type
460 nmbrType (TyVarTy tv)
461 = nmbrTyVar tv `thenNmbr` \ new_tv ->
462 returnNmbr (TyVarTy new_tv)
464 nmbrType (AppTy t1 t2)
465 = nmbrType t1 `thenNmbr` \ new_t1 ->
466 nmbrType t2 `thenNmbr` \ new_t2 ->
467 returnNmbr (AppTy new_t1 new_t2)
469 nmbrType (TyConTy tc use)
470 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
471 nmbrUsage use `thenNmbr` \ new_use ->
472 returnNmbr (TyConTy tc new_use)
474 nmbrType (SynTy tc args expand)
475 = --nmbrTyCon tc `thenNmbr` \ new_tc ->
476 mapNmbr nmbrType args `thenNmbr` \ new_args ->
477 nmbrType expand `thenNmbr` \ new_expand ->
478 returnNmbr (SynTy tc new_args new_expand)
480 nmbrType (ForAllTy tv ty)
481 = addTyVar tv `thenNmbr` \ new_tv ->
482 nmbrType ty `thenNmbr` \ new_ty ->
483 returnNmbr (ForAllTy new_tv new_ty)
485 nmbrType (ForAllUsageTy u us ty)
486 = addUVar u `thenNmbr` \ new_u ->
487 mapNmbr nmbrUVar us `thenNmbr` \ new_us ->
488 nmbrType ty `thenNmbr` \ new_ty ->
489 returnNmbr (ForAllUsageTy new_u new_us new_ty)
491 nmbrType (FunTy t1 t2 use)
492 = nmbrType t1 `thenNmbr` \ new_t1 ->
493 nmbrType t2 `thenNmbr` \ new_t2 ->
494 nmbrUsage use `thenNmbr` \ new_use ->
495 returnNmbr (FunTy new_t1 new_t2 new_use)
497 nmbrType (DictTy c ty use)
498 = --nmbrClass c `thenNmbr` \ new_c ->
499 nmbrType ty `thenNmbr` \ new_ty ->
500 nmbrUsage use `thenNmbr` \ new_use ->
501 returnNmbr (DictTy c new_ty new_use)
505 addTyVar, nmbrTyVar :: TyVar -> NmbrM TyVar
507 addTyVar tv@(TyVar u k maybe_name use) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
508 = --pprTrace "addTyVar:" (ppCat [pprUnique u, pprUnique ut]) $
509 case (lookupUFM_Directly tvenv u) of
510 Just xx -> -- pprTrace "addTyVar: already in map!" (ppr PprDebug tv) $
511 -- (It gets triggered when we do a datatype: first we
512 -- "addTyVar" the tyvars for the datatype as a whole;
513 -- we will subsequently "addId" the data cons, including
514 -- the type for each of them -- each of which includes
515 -- _forall_ ...tvs..., which we will addTyVar.
516 -- Harmless, if that's all that happens....
520 nenv_plus_tv = NmbrEnv ui (incrUnique ut) uu
522 (addToUFM_Directly tvenv u new_tv)
525 (nenv2, new_use) = nmbrUsage use nenv_plus_tv
527 new_tv = TyVar ut k maybe_name new_use
531 nmbrTyVar tv@(TyVar u _ _ _) nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
532 = case (lookupUFM_Directly tvenv u) of
533 Just xx -> (nenv, xx)
535 --pprTrace "nmbrTyVar: lookup failed:" (ppCat (ppr PprDebug u : [ppCat [ppr PprDebug x, ppStr "=>", ppr PprDebug tv] | (x,tv) <- ufmToList tvenv])) $
539 nmbrTyCon : only called from ``top-level'', if you know what I mean.
541 nmbrTyCon tc@FunTyCon = returnNmbr tc
542 nmbrTyCon tc@(TupleTyCon _ _ _) = returnNmbr tc
543 nmbrTyCon tc@(PrimTyCon _ _ _ _) = returnNmbr tc
545 nmbrTyCon (DataTyCon u n k tvs theta cons clss nod)
546 = --pprTrace "nmbrDataTyCon:" (ppCat (map (ppr PprDebug) tvs)) $
547 mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
548 mapNmbr nmbr_theta theta `thenNmbr` \ new_theta ->
549 mapNmbr nmbrId cons `thenNmbr` \ new_cons ->
550 returnNmbr (DataTyCon u n k new_tvs new_theta new_cons clss nod)
553 = --nmbrClass c `thenNmbr` \ new_c ->
554 nmbrType t `thenNmbr` \ new_t ->
555 returnNmbr (c, new_t)
557 nmbrTyCon (SynTyCon u n k a tvs expand)
558 = mapNmbr addTyVar tvs `thenNmbr` \ new_tvs ->
559 nmbrType expand `thenNmbr` \ new_expand ->
560 returnNmbr (SynTyCon u n k a new_tvs new_expand)
562 nmbrTyCon (SpecTyCon tc specs)
563 = mapNmbr nmbrMaybeTy specs `thenNmbr` \ new_specs ->
564 returnNmbr (SpecTyCon tc new_specs)
567 nmbrMaybeTy Nothing = returnNmbr Nothing
568 nmbrMaybeTy (Just t) = nmbrType t `thenNmbr` \ new_t ->
569 returnNmbr (Just new_t)
573 nmbrClass (Class u n tv supers ssels ops osels odefms instenv isupers)
574 = addTyVar tv `thenNmbr` \ new_tv ->
575 mapNmbr nmbr_op ops `thenNmbr` \ new_ops ->
576 returnNmbr (Class u n new_tv supers ssels new_ops osels odefms instenv isupers)
578 nmbr_op (ClassOp n tag ty)
579 = nmbrType ty `thenNmbr` \ new_ty ->
580 returnNmbr (ClassOp n tag new_ty)
584 nmbrUsage :: Usage -> NmbrM Usage
586 nmbrUsage u = returnNmbr u
588 nmbrUsage u@UsageOne = returnNmbr u
589 nmbrUsage u@UsageOmega = returnNmbr u
590 nmbrUsage (UsageVar u)
591 = nmbrUVar u `thenNmbr` \ new_u ->
592 returnNmbr (UsageVar new_u)
597 addUVar, nmbrUVar :: UVar -> NmbrM UVar
599 addUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
600 = case (lookupUFM_Directly uvenv u) of
601 Just xx -> trace "addUVar: already in map!" $
605 nenv_plus_uv = NmbrEnv ui ut (incrUnique uu)
608 (addToUFM_Directly uvenv u new_uv)
611 (nenv_plus_uv, new_uv)
613 nmbrUVar u nenv@(NmbrEnv ui ut uu idenv tvenv uvenv)
614 = case (lookupUFM_Directly uvenv u) of
615 Just xx -> (nenv, xx)
617 trace "nmbrUVar: lookup failed" $