/
Utils.hs
257 lines (240 loc) · 8.13 KB
/
Utils.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{- | "GHC.Syb.Utils" provides common utilities for the Ghc Api,
either based on Data\/Typeable or for use with Data.Generics
over Ghc Api types.
example output of 'showData' on 'parsedSource', 'renamedSource', and
'typecheckedSource' for a trivial @HelloWorld@ module, compared with
'ppr' output:
@
------------------------- pretty-printed parsedSource
module HelloWorld where
main = putStrLn "Hello, World!"
------------------------- pretty-printed renamedSource
Just (HelloWorld.main = System.IO.putStrLn "Hello, World!",
[import Prelude],
Nothing,
Nothing,
(HaddockModInfo
(Nothing)
(Nothing)
(Nothing)
(Nothing)))
------------------------- pretty-printed typecheckedSource
Just <AbsBinds [] [] [HelloWorld.main <= [] main]
HelloWorld.main :: GHC.IOBase.IO ()
[]
{ main = System.IO.putStrLn "Hello, World!" }>
------------------------- shown parsedSource
(L {HelloWorld.hs:1:0}
(HsModule
(Just
(L {HelloWorld.hs:1:7-16} {ModuleName: HelloWorld}))
(Nothing)
[]
[
(L {HelloWorld.hs:2:0-30}
(ValD
(FunBind
(L {HelloWorld.hs:2:0-3}
(Unqual {OccName: main}))
(False)
(MatchGroup
[
(L {HelloWorld.hs:2:0-30}
(Match
[]
(Nothing)
(GRHSs
[
(L {HelloWorld.hs:2:7-30}
(GRHS
[]
(L {HelloWorld.hs:2:7-30}
(HsApp
(L {HelloWorld.hs:2:7-14}
(HsVar
(Unqual {OccName: putStrLn})))
(L {HelloWorld.hs:2:16-30}
(HsLit
(HsString {FastString: "Hello, World!"})))))))]
(EmptyLocalBinds))))] {!type placeholder here?!})
(WpHole) {!NameSet placeholder here!}
(Nothing))))]
(Nothing)
(HaddockModInfo
(Nothing)
(Nothing)
(Nothing)
(Nothing))
(Nothing)))
------------------------- shown renamedSource
((,,,,)
(HsGroup
(ValBindsOut
[
((,)
(NonRecursive) {Bag(Located (HsBind Name)):
[
(L {HelloWorld.hs:2:0-30}
(FunBind
(L {HelloWorld.hs:2:0-3} {Name: HelloWorld.main})
(False)
(MatchGroup
[
(L {HelloWorld.hs:2:0-30}
(Match
[]
(Nothing)
(GRHSs
[
(L {HelloWorld.hs:2:7-30}
(GRHS
[]
(L {HelloWorld.hs:2:7-30}
(HsApp
(L {HelloWorld.hs:2:7-14}
(HsVar {Name: System.IO.putStrLn}))
(L {HelloWorld.hs:2:16-30}
(HsLit
(HsString {FastString: "Hello, World!"})))))))]
(EmptyLocalBinds))))] {!type placeholder here?!})
(WpHole) {NameSet:
[{Name: System.IO.putStrLn}]}
(Nothing)))]})]
[])
[]
[]
[]
[]
[]
[]
[]
[]
[])
[
(L {Implicit import declaration}
(ImportDecl
(L {Implicit import declaration} {ModuleName: Prelude})
(False)
(False)
(Nothing)
(Nothing)))]
(Nothing)
(Nothing)
(HaddockModInfo
(Nothing)
(Nothing)
(Nothing)
(Nothing)))
------------------------- shown typecheckedSource
{Bag(Located (HsBind Var)):
[
(L {HelloWorld.hs:2:0-30}
(AbsBinds
[]
[]
[
((,,,)
[] {Var: HelloWorld.main} {Var: main}
[])] {Bag(Located (HsBind Var)):
[
(L {HelloWorld.hs:2:0-30}
(FunBind
(L {HelloWorld.hs:2:0-3} {Var: main})
(False)
(MatchGroup
[
(L {HelloWorld.hs:2:0-30}
(Match
[]
(Nothing)
(GRHSs
[
(L {HelloWorld.hs:2:7-30}
(GRHS
[]
(L {HelloWorld.hs:2:7-30}
(HsApp
(L {HelloWorld.hs:2:7-14}
(HsVar {Var: System.IO.putStrLn}))
(L {HelloWorld.hs:2:16-30}
(HsLit
(HsString {FastString: "Hello, World!"})))))))]
(EmptyLocalBinds))))] GHC.IOBase.IO ())
(WpHole) {!NameSet placeholder here!}
(Nothing)))]}))]}
@
-}
module GHC.SYB.Utils where
import Data.Generics
-- import qualified GHC.Paths
import PprTyThing
import DynFlags
import GHC
import Outputable
import SrcLoc
import qualified OccName(occNameString)
import Bag(Bag,bagToList)
import Var(Var)
import FastString(FastString)
import NameSet(NameSet,nameSetToList)
import GHC.SYB.Instances
import Data.List
-- | Ghc Ast types tend to have undefined holes, to be filled
-- by later compiler phases. We tag Asts with their source,
-- so that we can avoid such holes based on who generated the Asts.
data Stage = Parser | Renamer | TypeChecker deriving (Eq,Ord,Show)
-- | Generic Data-based show, with special cases for GHC Ast types,
-- and simplistic indentation-based layout (the 'Int' parameter);
-- showing abstract types abstractly and avoiding known potholes
-- (based on the 'Stage' that generated the Ast)
showData :: Data a => Stage -> Int -> a -> String
showData stage n =
generic `ext1Q` list `extQ` string `extQ` fastString `extQ` srcSpan
`extQ` name `extQ` occName `extQ` moduleName `extQ` var `extQ` dataCon
`extQ` bagName `extQ` bagRdrName `extQ` bagVar `extQ` nameSet
`extQ` postTcType `extQ` fixity
where generic :: Data a => a -> String
generic t = indent n ++ "(" ++ showConstr (toConstr t)
++ space (concat (intersperse " " (gmapQ (showData stage (n+1)) t))) ++ ")"
space "" = ""
space s = ' ':s
indent n = "\n" ++ replicate n ' '
string = show :: String -> String
fastString = ("{FastString: "++) . (++"}") . show :: FastString -> String
list l = indent n ++ "["
++ concat (intersperse "," (map (showData stage (n+1)) l)) ++ "]"
name = ("{Name: "++) . (++"}") . showSDoc . ppr :: Name -> String
occName = ("{OccName: "++) . (++"}") . OccName.occNameString
moduleName = ("{ModuleName: "++) . (++"}") . showSDoc . ppr :: ModuleName -> String
srcSpan = ("{"++) . (++"}") . showSDoc . ppr :: SrcSpan -> String
var = ("{Var: "++) . (++"}") . showSDoc . ppr :: Var -> String
dataCon = ("{DataCon: "++) . (++"}") . showSDoc . ppr :: DataCon -> String
bagRdrName:: Bag (Located (HsBind RdrName)) -> String
bagRdrName = ("{Bag(Located (HsBind RdrName)): "++) . (++"}") . list . bagToList
bagName :: Bag (Located (HsBind Name)) -> String
bagName = ("{Bag(Located (HsBind Name)): "++) . (++"}") . list . bagToList
bagVar :: Bag (Located (HsBind Var)) -> String
bagVar = ("{Bag(Located (HsBind Var)): "++) . (++"}") . list . bagToList
nameSet | stage `elem` [Parser,TypeChecker]
= const ("{!NameSet placeholder here!}") :: NameSet -> String
| otherwise
= ("{NameSet: "++) . (++"}") . list . nameSetToList
postTcType | stage<TypeChecker = const "{!type placeholder here?!}" :: PostTcType -> String
| otherwise = showSDoc . ppr :: Type -> String
fixity | stage<Renamer = const "{!fixity placeholder here?!}" :: GHC.Fixity -> String
| otherwise = ("{Fixity: "++) . (++"}") . showSDoc . ppr :: GHC.Fixity -> String
-- | Like 'everything', but avoid known potholes, based on the 'Stage' that
-- generated the Ast.
everythingStaged :: Stage -> (r -> r -> r) -> r -> GenericQ r -> GenericQ r
everythingStaged stage k z f x
| (const False `extQ` postTcType `extQ` fixity `extQ` nameSet) x = z
| otherwise = foldl k (f x) (gmapQ (everythingStaged stage k z f) x)
where nameSet = const (stage `elem` [Parser,TypeChecker]) :: NameSet -> Bool
postTcType = const (stage<TypeChecker) :: PostTcType -> Bool
fixity = const (stage<Renamer) :: GHC.Fixity -> Bool
-- | A variation of 'everything', using a 'GenericQ Bool' to skip
-- parts of the input 'Data'.
everythingBut :: GenericQ Bool -> (r -> r -> r) -> r -> GenericQ r -> GenericQ r
everythingBut q k z f x
| q x = z
| otherwise = foldl k (f x) (gmapQ (everythingBut q k z f) x)