-
Notifications
You must be signed in to change notification settings - Fork 26
/
Weeder.hs
390 lines (298 loc) · 11.3 KB
/
Weeder.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
{-# language ApplicativeDo #-}
{-# language BlockArguments #-}
{-# language DeriveGeneric #-}
{-# language FlexibleContexts #-}
{-# language LambdaCase #-}
{-# language NamedFieldPuns #-}
{-# language NoImplicitPrelude #-}
{-# language OverloadedLabels #-}
{-# language OverloadedStrings #-}
module Weeder
( -- * Analysis
Analysis(..)
, analyseHieFile
, emptyAnalysis
, allDeclarations
-- ** Reachability
, Root(..)
, reachable
-- * Declarations
, Declaration(..)
)
where
-- algebraic-graphs
import Algebra.Graph ( Graph, edge, empty, overlay, vertex, vertexList )
import Algebra.Graph.ToGraph ( dfs )
-- base
import Control.Applicative ( Alternative )
import Control.Monad ( guard, msum, when )
import Data.Foldable ( for_, traverse_ )
import Data.List ( intercalate )
import Data.Monoid ( First( First ) )
import GHC.Generics ( Generic )
import Prelude hiding ( span )
-- containers
import Data.Map.Strict ( Map )
import qualified Data.Map.Strict as Map
import Data.Sequence ( Seq )
import Data.Set ( Set )
import qualified Data.Set as Set
-- generic-lens
import Data.Generics.Labels ()
-- ghc
import Avail ( AvailInfo( Avail, AvailTC ) )
import FieldLabel ( FieldLbl( FieldLabel, flSelector ) )
import HieTypes
( BindType( RegularBind )
, ContextInfo( Decl, ValBind, PatternBind, Use, TyDecl, ClassTyDecl )
, DeclType( DataDec, ClassDec, ConDec )
, HieAST( Node, nodeInfo, nodeChildren, nodeSpan )
, HieASTs( HieASTs )
, HieFile( HieFile, hie_asts, hie_exports, hie_module, hie_hs_file )
, IdentifierDetails( IdentifierDetails, identInfo )
, NodeInfo( NodeInfo, nodeIdentifiers, nodeAnnotations )
, Scope( ModuleScope )
)
import Module ( Module, moduleStableString )
import Name ( Name, nameModule_maybe, nameOccName )
import OccName
( OccName
, isDataOcc
, isDataSymOcc
, isTcOcc
, isTvOcc
, isVarOcc
, occNameString
)
import SrcLoc ( RealSrcSpan, realSrcSpanEnd, realSrcSpanStart )
-- lens
import Control.Lens ( (%=) )
-- mtl
import Control.Monad.State.Class ( MonadState )
-- transformers
import Control.Monad.Trans.Maybe ( runMaybeT )
data Declaration =
Declaration
{ declModule :: Module
-- ^ The module this declaration occurs in.
, declOccName :: OccName
-- ^ The symbol name of a declaration.
}
deriving
( Eq, Ord )
instance Show Declaration where
show =
declarationStableName
declarationStableName :: Declaration -> String
declarationStableName Declaration { declModule, declOccName } =
let
namespace
| isVarOcc declOccName = "var"
| isTvOcc declOccName = "tv"
| isTcOcc declOccName = "tc"
| isDataOcc declOccName = "data"
| isDataSymOcc declOccName = "dataSym"
| otherwise = "unknown"
in
intercalate "$" [ namespace, moduleStableString declModule, "$", occNameString declOccName ]
-- | All information maintained by 'analyseHieFile'.
data Analysis =
Analysis
{ dependencyGraph :: Graph Declaration
-- ^ A graph between declarations, capturing dependencies.
, declarationSites :: Map Declaration ( Set RealSrcSpan )
-- ^ A partial mapping between declarations and their definition site.
-- This Map is partial as we don't always know where a Declaration was
-- defined (e.g., it may come from a package without source code).
-- We capture a set of spans, because a declaration may be defined in
-- multiple locations, e.g., a type signature for a function separate
-- from its definition.
, implicitRoots :: Set Declaration
-- ^ The Set of all Declarations that are always reachable. This is used
-- to capture knowledge not yet modelled in weeder, such as instance
-- declarations depending on top-level functions.
, exports :: Map Module ( Set Declaration )
-- ^ All exports for a given module.
, modulePaths :: Map Module FilePath
-- ^ A map from modules to the file path to the .hs file defining them.
}
deriving
( Generic )
-- | The empty analysis - the result of analysing zero @.hie@ files.
emptyAnalysis :: Analysis
emptyAnalysis = Analysis empty mempty mempty mempty mempty
-- | A root for reachability analysis.
data Root
= -- | A given declaration is a root.
DeclarationRoot Declaration
| -- | All exported declarations in a module are roots.
ModuleRoot Module
deriving
( Eq, Ord )
-- | Determine the set of all declaration reachable from a set of roots.
reachable :: Analysis -> Set Root -> Set Declaration
reachable Analysis{ dependencyGraph, exports } roots =
Set.fromList ( dfs ( foldMap rootDeclarations roots ) dependencyGraph )
where
rootDeclarations = \case
DeclarationRoot d -> [ d ]
ModuleRoot m -> foldMap Set.toList ( Map.lookup m exports )
-- | The set of all known declarations, including usages.
allDeclarations :: Analysis -> Set Declaration
allDeclarations Analysis{ dependencyGraph } =
Set.fromList ( vertexList dependencyGraph )
-- | Incrementally update 'Analysis' with information in a 'HieFile'.
analyseHieFile :: MonadState Analysis m => HieFile -> m ()
analyseHieFile HieFile{ hie_asts = HieASTs hieASTs, hie_exports, hie_module, hie_hs_file } = do
#modulePaths %= Map.insert hie_module hie_hs_file
for_ hieASTs \ast -> do
addAllDeclarations ast
topLevelAnalysis ast
for_ hie_exports ( analyseExport hie_module )
analyseExport :: MonadState Analysis m => Module -> AvailInfo -> m ()
analyseExport m = \case
Avail name ->
for_ ( nameToDeclaration name ) addExport
AvailTC name pieces fields -> do
for_ ( nameToDeclaration name ) addExport
for_ pieces ( traverse_ addExport . nameToDeclaration )
for_ fields \FieldLabel{ flSelector } -> for_ ( nameToDeclaration flSelector ) addExport
where
addExport :: MonadState Analysis m => Declaration -> m ()
addExport d = #exports %= Map.insertWith (<>) m ( Set.singleton d )
-- | @addDependency x y@ adds the information that @x@ depends on @y@.
addDependency :: MonadState Analysis m => Declaration -> Declaration -> m ()
addDependency x y =
#dependencyGraph %= overlay ( edge x y )
addImplicitRoot :: MonadState Analysis m => Declaration -> m ()
addImplicitRoot x =
#implicitRoots %= Set.insert x
define :: MonadState Analysis m => Declaration -> RealSrcSpan -> m ()
define decl span =
when ( realSrcSpanStart span /= realSrcSpanEnd span ) do
#declarationSites %= Map.insertWith Set.union decl ( Set.singleton span )
#dependencyGraph %= overlay ( vertex decl )
addDeclaration :: MonadState Analysis m => Declaration -> m ()
addDeclaration decl =
#dependencyGraph %= overlay ( vertex decl )
-- | Try and add vertices for all declarations in an AST - both
-- those declared here, and those referred to from here.
addAllDeclarations :: ( MonadState Analysis m ) => HieAST a -> m ()
addAllDeclarations n@Node{ nodeChildren } = do
for_ ( findIdentifiers ( const True ) n ) addDeclaration
for_ nodeChildren addAllDeclarations
topLevelAnalysis :: MonadState Analysis m => HieAST a -> m ()
topLevelAnalysis n@Node{ nodeChildren } = do
analysed <-
runMaybeT
( msum
[
-- analyseStandaloneDeriving n
-- ,
analyseInstanceDeclaration n
, analyseBinding n
, analyseRewriteRule n
, analyseClassDeclaration n
, analyseDataDeclaration n
]
)
case analysed of
Nothing ->
-- We didn't find a top level declaration here, check all this nodes
-- children.
traverse_ topLevelAnalysis nodeChildren
Just () ->
-- Top level analysis succeeded, there's nothing more to do for this node.
return ()
analyseBinding :: ( Alternative m, MonadState Analysis m ) => HieAST a -> m ()
analyseBinding n@Node{ nodeSpan, nodeInfo = NodeInfo{ nodeAnnotations } } = do
guard $ ( "FunBind", "HsBindLR" ) `Set.member` nodeAnnotations
for_ ( findDeclarations n ) \d -> do
define d nodeSpan
for_ ( uses n ) $ addDependency d
analyseRewriteRule :: ( Alternative m, MonadState Analysis m ) => HieAST a -> m ()
analyseRewriteRule n@Node{ nodeInfo = NodeInfo{ nodeAnnotations } } = do
guard ( ( "HsRule", "RuleDecl" ) `Set.member` nodeAnnotations )
for_ ( uses n ) addImplicitRoot
analyseInstanceDeclaration :: ( Alternative m, MonadState Analysis m ) => HieAST a -> m ()
analyseInstanceDeclaration n@Node{ nodeInfo = NodeInfo{ nodeAnnotations } } = do
guard ( ( "ClsInstD", "InstDecl" ) `Set.member` nodeAnnotations )
traverse_ addImplicitRoot ( uses n )
analyseClassDeclaration :: ( Alternative m, MonadState Analysis m ) => HieAST a -> m ()
analyseClassDeclaration n@Node{ nodeInfo = NodeInfo{ nodeAnnotations } } = do
guard ( ( "ClassDecl", "TyClDecl" ) `Set.member` nodeAnnotations )
for_ ( findIdentifiers isClassDeclaration n ) $
for_ ( findIdentifiers ( const True ) n ) . addDependency
where
isClassDeclaration =
not . Set.null . Set.filter \case
Decl ClassDec _ ->
True
_ ->
False
analyseDataDeclaration :: ( Alternative m, MonadState Analysis m ) => HieAST a -> m ()
analyseDataDeclaration n@Node { nodeInfo = NodeInfo{ nodeAnnotations } } = do
guard ( ( "DataDecl", "TyClDecl" ) `Set.member` nodeAnnotations )
for_
( foldMap
( First . Just )
( findIdentifiers ( any isDataDec ) n )
)
\dataTypeName ->
for_ ( constructors n ) \constructor ->
for_ ( foldMap ( First . Just ) ( findIdentifiers ( any isConDec ) constructor ) ) \conDec -> do
addDependency conDec dataTypeName
for_ ( uses constructor ) ( addDependency conDec )
where
isDataDec = \case
Decl DataDec _ -> True
_ -> False
isConDec = \case
Decl ConDec _ -> True
_ -> False
constructors :: HieAST a -> Seq ( HieAST a )
constructors n@Node { nodeChildren, nodeInfo = NodeInfo{ nodeAnnotations } } =
if any ( \( _, t ) -> t == "ConDecl" ) nodeAnnotations then
pure n
else
foldMap constructors nodeChildren
findDeclarations :: HieAST a -> Seq Declaration
findDeclarations =
findIdentifiers
( not
. Set.null
. Set.filter \case
-- Things that count as declarations
ValBind RegularBind ModuleScope _ -> True
PatternBind ModuleScope _ _ -> True
Decl _ _ -> True
TyDecl -> True
ClassTyDecl{} -> True
-- Anything else is not a declaration
_ -> False
)
findIdentifiers
:: ( Set ContextInfo -> Bool )
-> HieAST a
-> Seq Declaration
findIdentifiers f Node{ nodeInfo = NodeInfo{ nodeIdentifiers }, nodeChildren } =
foldMap
( \case
( Left _, _ ) ->
mempty
( Right name, IdentifierDetails{ identInfo } ) ->
if f identInfo then
foldMap pure ( nameToDeclaration name )
else
mempty
)
( Map.toList nodeIdentifiers )
<> foldMap ( findIdentifiers f ) nodeChildren
uses :: HieAST a -> Set Declaration
uses =
foldMap Set.singleton
. findIdentifiers \identInfo -> Use `Set.member` identInfo
nameToDeclaration :: Name -> Maybe Declaration
nameToDeclaration name = do
m <- nameModule_maybe name
return Declaration { declModule = m, declOccName = nameOccName name }