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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
--
-- Haddock - A Haskell Documentation Tool
--
-- (c) Simon Marlow 2003
--
module Haddock.Interface.Rename (renameInterface) where
import Haddock.Types
import Haddock.GHC.Utils
import GHC hiding (NoLink)
import Name
import BasicTypes
import SrcLoc
import Bag (emptyBag)
import Outputable
import Util (thenCmp)
import Data.List
import Data.Map (Map)
import qualified Data.Map as Map hiding ( Map )
import Prelude hiding (mapM)
import Data.Traversable (mapM)
import Control.Arrow
import Control.Monad hiding (mapM)
import Control.Monad.Writer hiding (mapM)
renameInterface :: LinkEnv -> Interface -> ErrMsgM Interface
renameInterface renamingEnv mod =
-- first create the local env, where every name exported by this module
-- is mapped to itself, and everything else comes from the global renaming
-- env
let localEnv = foldl fn renamingEnv (ifaceVisibleExports mod)
where fn env name = Map.insert name (nameSetMod name (ifaceMod mod)) env
docs = Map.toList (ifaceDocMap mod)
renameMapElem (k,d) = do d' <- renameDoc d; return (k, d')
-- rename names in the exported declarations to point to things that
-- are closer to, or maybe even exported by, the current module.
(renamedExportItems, missingNames1)
= runRnFM localEnv (renameExportItems (ifaceExportItems mod))
(rnDocMap, missingNames2)
= runRnFM localEnv (liftM Map.fromList (mapM renameMapElem docs))
(finalModuleDoc, missingNames3)
= runRnFM localEnv (renameMaybeDoc (ifaceDoc mod))
-- combine the missing names and filter out the built-ins, which would
-- otherwise allways be missing.
missingNames = nub $ filter isExternalName
(missingNames1 ++ missingNames2 ++ missingNames3)
-- filter out certain built in type constructors using their string
-- representation. TODO: use the Name constants from the GHC API.
strings = filter (`notElem` ["()", "[]", "(->)"])
(map pretty missingNames)
in do
-- report things that we couldn't link to. Only do this for non-hidden
-- modules.
when (OptHide `notElem` ifaceOptions mod && not (null strings)) $
tell ["Warning: " ++ show (ppr (ifaceMod mod) defaultUserStyle) ++
": could not find link destinations for:\n"++
" " ++ concat (map (' ':) strings) ]
return $ mod { ifaceRnDoc = finalModuleDoc,
ifaceRnDocMap = rnDocMap,
ifaceRnExportItems = renamedExportItems }
--------------------------------------------------------------------------------
-- Monad for renaming
--
-- The monad does two things for us: it passes around the environment for
-- renaming, and it returns a list of names which couldn't be found in
-- the environment.
--------------------------------------------------------------------------------
newtype GenRnM n a =
RnM { unRn :: (n -> (Bool, DocName)) -- name lookup function
-> (a,[n])
}
type RnM a = GenRnM Name a
instance Monad (GenRnM n) where
(>>=) = thenRn
return = returnRn
returnRn :: a -> GenRnM n a
returnRn a = RnM (\_ -> (a,[]))
thenRn :: GenRnM n a -> (a -> GenRnM n b) -> GenRnM n b
m `thenRn` k = RnM (\lkp -> case unRn m lkp of
(a,out1) -> case unRn (k a) lkp of
(b,out2) -> (b,out1++out2))
getLookupRn :: RnM (Name -> (Bool, DocName))
getLookupRn = RnM (\lkp -> (lkp,[]))
outRn :: Name -> RnM ()
outRn name = RnM (\_ -> ((),[name]))
lookupRn :: (DocName -> a) -> Name -> RnM a
lookupRn and_then name = do
lkp <- getLookupRn
case lkp name of
(False,maps_to) -> do outRn name; return (and_then maps_to)
(True, maps_to) -> return (and_then maps_to)
newtype OrdName = MkOrdName Name
instance Eq OrdName where
(MkOrdName a) == (MkOrdName b) = compare a b == EQ
instance Ord OrdName where
(MkOrdName a) `compare` (MkOrdName b) =
case (nameModule_maybe a, nameModule_maybe b) of
(Just modA, Just modB) ->
(modA `compare` modB) `thenCmp` (getOccName a `compare` getOccName b)
(Nothing, Nothing) -> getOccName a `compare` getOccName b
_ -> LT
instance Outputable OrdName where
ppr (MkOrdName x) = ppr (nameOccName x)
runRnFM :: LinkEnv -> RnM a -> (a,[Name])
runRnFM env rn = unRn rn lkp
where
lkp n = case Map.lookup (MkOrdName n) ordEnv of
Nothing -> (False, NoLink n)
Just q -> (True, Link q)
ordEnv = Map.fromList . map (MkOrdName *** id) . Map.toList $ env
--------------------------------------------------------------------------------
-- Renaming
--------------------------------------------------------------------------------
keep n = NoLink n
keepL (L loc n) = L loc (NoLink n)
rename = lookupRn id
renameL (L loc name) = return . L loc =<< rename name
renameExportItems :: [ExportItem Name] -> RnM [ExportItem DocName]
renameExportItems items = mapM renameExportItem items
renameMaybeDoc :: Maybe (HsDoc Name) -> RnM (Maybe (HsDoc DocName))
renameMaybeDoc mbDoc = mapM renameDoc mbDoc
renameLDoc (L loc doc) = return . L loc =<< renameDoc doc
renameDoc :: HsDoc Name -> RnM (HsDoc DocName)
renameDoc doc = case doc of
DocEmpty -> return DocEmpty
DocAppend a b -> do
a' <- renameDoc a
b' <- renameDoc b
return (DocAppend a' b')
DocString str -> return (DocString str)
DocParagraph doc -> do
doc' <- renameDoc doc
return (DocParagraph doc')
DocIdentifier ids -> do
lkp <- getLookupRn
case [ n | (True, n) <- map lkp ids ] of
ids'@(_:_) -> return (DocIdentifier ids')
[] -> return (DocIdentifier (map NoLink ids))
DocModule str -> return (DocModule str)
DocEmphasis doc -> do
doc' <- renameDoc doc
return (DocEmphasis doc')
DocMonospaced doc -> do
doc' <- renameDoc doc
return (DocMonospaced doc')
DocUnorderedList docs -> do
docs' <- mapM renameDoc docs
return (DocUnorderedList docs')
DocOrderedList docs -> do
docs' <- mapM renameDoc docs
return (DocOrderedList docs')
DocDefList docs -> do
docs' <- mapM (\(a,b) -> do
a' <- renameDoc a
b' <- renameDoc b
return (a',b')) docs
return (DocDefList docs')
DocCodeBlock doc -> do
doc' <- renameDoc doc
return (DocCodeBlock doc')
DocURL str -> return (DocURL str)
DocAName str -> return (DocAName str)
renameLPred (L loc p) = return . L loc =<< renamePred p
renamePred :: HsPred Name -> RnM (HsPred DocName)
renamePred (HsClassP name types) = do
name' <- rename name
types' <- mapM renameLType types
return (HsClassP name' types')
renamePred (HsIParam (IPName name) t) = do
name' <- rename name
t' <- renameLType t
return (HsIParam (IPName name') t')
renameLType (L loc t) = return . L loc =<< renameType t
renameType t = case t of
HsForAllTy expl tyvars lcontext ltype -> do
tyvars' <- mapM renameLTyVarBndr tyvars
lcontext' <- renameLContext lcontext
ltype' <- renameLType ltype
return (HsForAllTy expl tyvars' lcontext' ltype')
HsTyVar n -> return . HsTyVar =<< rename n
HsBangTy b ltype -> return . HsBangTy b =<< renameLType ltype
HsAppTy a b -> do
a' <- renameLType a
b' <- renameLType b
return (HsAppTy a' b')
HsFunTy a b -> do
a' <- renameLType a
b' <- renameLType b
return (HsFunTy a' b')
HsListTy t -> return . HsListTy =<< renameLType t
HsPArrTy t -> return . HsPArrTy =<< renameLType t
HsTupleTy b ts -> return . HsTupleTy b =<< mapM renameLType ts
HsOpTy a (L loc op) b -> do
op' <- rename op
a' <- renameLType a
b' <- renameLType b
return (HsOpTy a' (L loc op') b')
HsParTy t -> return . HsParTy =<< renameLType t
HsNumTy n -> return (HsNumTy n)
HsPredTy p -> return . HsPredTy =<< renamePred p
HsKindSig t k -> do
t' <- renameLType t
return (HsKindSig t' k)
HsDocTy t doc -> do
t' <- renameLType t
doc' <- renameLDoc doc
return (HsDocTy t' doc')
_ -> error "renameType"
renameLTyVarBndr (L loc tv) = do
name' <- rename (hsTyVarName tv)
return $ L loc (replaceTyVarName tv name')
renameLContext (L loc context) = do
context' <- mapM renameLPred context
return (L loc context')
renameInstHead :: InstHead Name -> RnM (InstHead DocName)
renameInstHead (preds, className, types) = do
preds' <- mapM renamePred preds
className' <- rename className
types' <- mapM renameType types
return (preds', className', types')
renameLDecl (L loc d) = return . L loc =<< renameDecl d
renameDecl d = case d of
TyClD d -> do
d' <- renameTyClD d
return (TyClD d')
SigD s -> do
s' <- renameSig s
return (SigD s')
ForD d -> do
d' <- renameForD d
return (ForD d')
_ -> error "renameDecl"
renameTyClD d = case d of
ForeignType _ _ _ -> error "renameTyClD" -- I'm guessing these can't be exported
-- ForeignType name a b -> do
-- name' <- renameL name
-- return (ForeignType name' a b)
TyData x lcontext lname ltyvars _ k cons _ -> do
lcontext' <- renameLContext lcontext
ltyvars' <- mapM renameLTyVarBndr ltyvars
cons' <- mapM renameLCon cons
-- I don't think we need the derivings, so we return Nothing
-- We skip the type patterns too. TODO: find out what they are :-)
return (TyData x lcontext' (keepL lname) ltyvars' Nothing k cons' Nothing)
TySynonym lname ltyvars typat ltype -> do
ltyvars' <- mapM renameLTyVarBndr ltyvars
ltype' <- renameLType ltype
-- We skip type patterns here as well.
return (TySynonym (keepL lname) ltyvars' Nothing ltype')
ClassDecl lcontext lname ltyvars lfundeps lsigs _ _ _ -> do
lcontext' <- renameLContext lcontext
ltyvars' <- mapM renameLTyVarBndr ltyvars
lfundeps' <- mapM renameLFunDep lfundeps
lsigs' <- mapM renameLSig lsigs
-- we don't need the default methods or the already collected doc entities
-- we skip the ATs for now.
return (ClassDecl lcontext' (keepL lname) ltyvars' lfundeps' lsigs' emptyBag [] [])
where
renameLCon (L loc con) = return . L loc =<< renameCon con
renameCon (ConDecl lname expl ltyvars lcontext details restype mbldoc) = do
ltyvars' <- mapM renameLTyVarBndr ltyvars
lcontext' <- renameLContext lcontext
details' <- renameDetails details
restype' <- renameResType restype
mbldoc' <- mapM renameLDoc mbldoc
return (ConDecl (keepL lname) expl ltyvars' lcontext' details' restype' mbldoc')
renameDetails (RecCon fields) = return . RecCon =<< mapM renameField fields
renameDetails (PrefixCon ps) = return . PrefixCon =<< mapM renameLType ps
renameDetails (InfixCon a b) = do
a' <- renameLType a
b' <- renameLType b
return (InfixCon a' b')
renameField (ConDeclField name t doc) = do
t' <- renameLType t
doc' <- mapM renameLDoc doc
return (ConDeclField (keepL name) t' doc')
renameResType (ResTyH98) = return ResTyH98
renameResType (ResTyGADT t) = return . ResTyGADT =<< renameLType t
renameLFunDep (L loc (xs, ys)) = return (L loc (map keep xs, map keep ys))
renameLSig (L loc sig) = return . L loc =<< renameSig sig
renameSig sig = case sig of
TypeSig (L loc name) ltype -> do
ltype' <- renameLType ltype
return (TypeSig (L loc (keep name)) ltype')
{- SpecSig lname ltype x -> do
lname' <- renameL lname
ltype' <- renameLType ltype
return (SpecSig lname' ltype' x)
InlineSig lname x -> do
lname' <- renameL lname
return (InlineSig lname' x)
SpecInstSig t -> return . SpecInstSig =<< renameLType t
FixSig fsig -> return . FixSig =<< renameFixitySig fsig
where
renameFixitySig (FixitySig lname x) = do
lname' <- renameL lname
return (FixitySig lname' x)
-}
renameForD (ForeignImport lname ltype x) = do
ltype' <- renameLType ltype
return (ForeignImport (keepL lname) ltype' x)
renameForD (ForeignExport lname ltype x) = do
ltype' <- renameLType ltype
return (ForeignExport (keepL lname) ltype' x)
renameExportItem :: ExportItem Name -> RnM (ExportItem DocName)
renameExportItem item = case item of
ExportModule mod -> return (ExportModule mod)
ExportGroup lev id doc -> do
doc' <- renameDoc doc
return (ExportGroup lev id doc')
ExportDecl x decl doc instances -> do
decl' <- renameLDecl decl
doc' <- mapM renameDoc doc
instances' <- mapM renameInstHead instances
return (ExportDecl x decl' doc' instances')
ExportNoDecl x y subs -> do
y' <- lookupRn id y
subs' <- mapM (lookupRn id) subs
return (ExportNoDecl x y' subs')
ExportDoc doc -> do
doc' <- renameDoc doc
return (ExportDoc doc')
|