compiler: Write .o files atomically. See #14533
[ghc.git] / compiler / main / PprTyThing.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Pretty-printing TyThings
4 --
5 -- (c) The GHC Team 2005
6 --
7 -----------------------------------------------------------------------------
8
9 {-# LANGUAGE CPP #-}
10 module PprTyThing (
11 pprTyThing,
12 pprTyThingInContext,
13 pprTyThingLoc,
14 pprTyThingInContextLoc,
15 pprTyThingHdr,
16 pprTypeForUser,
17 pprFamInst
18 ) where
19
20 #include "HsVersions.h"
21
22 import GhcPrelude
23
24 import Type ( ArgFlag(..), TyThing(..), mkTyVarBinders, pprUserForAll )
25 import IfaceSyn ( ShowSub(..), ShowHowMuch(..), AltPpr(..)
26 , showToHeader, pprIfaceDecl )
27 import CoAxiom ( coAxiomTyCon )
28 import HscTypes( tyThingParent_maybe )
29 import MkIface ( tyThingToIfaceDecl )
30 import Type ( tidyOpenType )
31 import FamInstEnv( FamInst(..), FamFlavor(..) )
32 import Type( Type, pprTypeApp, pprSigmaType )
33 import Name
34 import VarEnv( emptyTidyEnv )
35 import Outputable
36
37 -- -----------------------------------------------------------------------------
38 -- Pretty-printing entities that we get from the GHC API
39
40 {- Note [Pretty printing via IfaceSyn]
41 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42 Our general plan for prett-printing
43 - Types
44 - TyCons
45 - Classes
46 - Pattern synonyms
47 ...etc...
48
49 is to convert them to IfaceSyn, and pretty-print that. For example
50 - pprType converts a Type to an IfaceType, and pretty prints that.
51 - pprTyThing converts the TyThing to an IfaceDecl,
52 and pretty prints that.
53
54 So IfaceSyn play a dual role:
55 - it's the internal version of an interface files
56 - it's used for pretty-printing
57
58 Why do this?
59
60 * A significant reason is that we need to be able
61 to pretty-print IfaceSyn (to display Foo.hi), and it was a
62 pain to duplicate masses of pretty-printing goop, esp for
63 Type and IfaceType.
64
65 * When pretty-printing (a type, say), we want to tidy (with
66 tidyType) to avoids having (forall a a. blah) where the two
67 a's have different uniques.
68
69 Alas, for type constructors, TyCon, tidying does not work well,
70 because a TyCon includes DataCons which include Types, which mention
71 TyCons. And tidying can't tidy a mutually recursive data structure
72 graph, only trees.
73
74 * Interface files contains fast-strings, not uniques, so the very same
75 tidying must take place when we convert to IfaceDecl. E.g.
76 MkIface.tyThingToIfaceDecl which converts a TyThing (i.e. TyCon,
77 Class etc) to an IfaceDecl.
78
79 Bottom line: IfaceDecls are already 'tidy', so it's straightforward
80 to print them.
81
82 * An alternative I once explored was to ensure that TyCons get type
83 variables with distinct print-names. That's ok for type variables
84 but less easy for kind variables. Processing data type declarations
85 is already so complicated that I don't think it's sensible to add
86 the extra requirement that it generates only "pretty" types and
87 kinds.
88
89 Consequences:
90
91 - IfaceSyn (and IfaceType) must contain enough information to
92 print nicely. Hence, for example, the IfaceAppArgs type, which
93 allows us to suppress invisible kind arguments in types
94 (see Note [Suppressing invisible arguments] in IfaceType)
95
96 - In a few places we have info that is used only for pretty-printing,
97 and is totally ignored when turning IfaceSyn back into TyCons
98 etc (in TcIface). For example, IfaceClosedSynFamilyTyCon
99 stores a [IfaceAxBranch] that is used only for pretty-printing.
100
101 - See Note [Free tyvars in IfaceType] in IfaceType
102
103 See #7730, #8776 for details -}
104
105 --------------------
106 -- | Pretty-prints a 'FamInst' (type/data family instance) with its defining location.
107 pprFamInst :: FamInst -> SDoc
108 -- * For data instances we go via pprTyThing of the representational TyCon,
109 -- because there is already much cleverness associated with printing
110 -- data type declarations that I don't want to duplicate
111 -- * For type instances we print directly here; there is no TyCon
112 -- to give to pprTyThing
113 --
114 -- FamInstEnv.pprFamInst does a more quick-and-dirty job for internal purposes
115
116 pprFamInst (FamInst { fi_flavor = DataFamilyInst rep_tc })
117 = pprTyThingInContextLoc (ATyCon rep_tc)
118
119 pprFamInst (FamInst { fi_flavor = SynFamilyInst, fi_axiom = axiom
120 , fi_tvs = tvs, fi_tys = lhs_tys, fi_rhs = rhs })
121 = showWithLoc (pprDefinedAt (getName axiom)) $
122 hang (text "type instance"
123 <+> pprUserForAll (mkTyVarBinders Specified tvs)
124 -- See Note [Printing foralls in type family instances]
125 -- in IfaceType
126 <+> pprTypeApp (coAxiomTyCon axiom) lhs_tys)
127 2 (equals <+> ppr rhs)
128
129 ----------------------------
130 -- | Pretty-prints a 'TyThing' with its defining location.
131 pprTyThingLoc :: TyThing -> SDoc
132 pprTyThingLoc tyThing
133 = showWithLoc (pprDefinedAt (getName tyThing))
134 (pprTyThing showToHeader tyThing)
135
136 -- | Pretty-prints the 'TyThing' header. For functions and data constructors
137 -- the function is equivalent to 'pprTyThing' but for type constructors
138 -- and classes it prints only the header part of the declaration.
139 pprTyThingHdr :: TyThing -> SDoc
140 pprTyThingHdr = pprTyThing showToHeader
141
142 -- | Pretty-prints a 'TyThing' in context: that is, if the entity
143 -- is a data constructor, record selector, or class method, then
144 -- the entity's parent declaration is pretty-printed with irrelevant
145 -- parts omitted.
146 pprTyThingInContext :: ShowSub -> TyThing -> SDoc
147 pprTyThingInContext show_sub thing
148 = go [] thing
149 where
150 go ss thing
151 = case tyThingParent_maybe thing of
152 Just parent ->
153 go (getOccName thing : ss) parent
154 Nothing ->
155 pprTyThing
156 (show_sub { ss_how_much = ShowSome ss (AltPpr Nothing) })
157 thing
158
159 -- | Like 'pprTyThingInContext', but adds the defining location.
160 pprTyThingInContextLoc :: TyThing -> SDoc
161 pprTyThingInContextLoc tyThing
162 = showWithLoc (pprDefinedAt (getName tyThing))
163 (pprTyThingInContext showToHeader tyThing)
164
165 -- | Pretty-prints a 'TyThing'.
166 pprTyThing :: ShowSub -> TyThing -> SDoc
167 -- We pretty-print 'TyThing' via 'IfaceDecl'
168 -- See Note [Pretty-printing TyThings]
169 pprTyThing ss ty_thing
170 = pprIfaceDecl ss' (tyThingToIfaceDecl ty_thing)
171 where
172 ss' = case ss_how_much ss of
173 ShowHeader (AltPpr Nothing) -> ss { ss_how_much = ShowHeader ppr' }
174 ShowSome xs (AltPpr Nothing) -> ss { ss_how_much = ShowSome xs ppr' }
175 _ -> ss
176
177 ppr' = AltPpr $ ppr_bndr $ getName ty_thing
178
179 ppr_bndr :: Name -> Maybe (OccName -> SDoc)
180 ppr_bndr name
181 | isBuiltInSyntax name
182 = Nothing
183 | otherwise
184 = case nameModule_maybe name of
185 Just mod -> Just $ \occ -> getPprStyle $ \sty ->
186 pprModulePrefix sty mod occ <> ppr occ
187 Nothing -> WARN( True, ppr name ) Nothing
188 -- Nothing is unexpected here; TyThings have External names
189
190 pprTypeForUser :: Type -> SDoc
191 -- The type is tidied
192 pprTypeForUser ty
193 = pprSigmaType tidy_ty
194 where
195 (_, tidy_ty) = tidyOpenType emptyTidyEnv ty
196 -- Often the types/kinds we print in ghci are fully generalised
197 -- and have no free variables, but it turns out that we sometimes
198 -- print un-generalised kinds (eg when doing :k T), so it's
199 -- better to use tidyOpenType here
200
201 showWithLoc :: SDoc -> SDoc -> SDoc
202 showWithLoc loc doc
203 = hang doc 2 (char '\t' <> comment <+> loc)
204 -- The tab tries to make them line up a bit
205 where
206 comment = text "--"