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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
%
\section[RnMonad4]{The monad used by the fourth renamer pass}
\begin{code}
#include "HsVersions.h"
module RnMonad4 (
Rn4M(..),
initRn4, thenRn4, thenRn4_, andRn4, returnRn4, mapRn4, mapAndUnzipRn4,
addErrRn4, failButContinueRn4, recoverQuietlyRn4,
pushSrcLocRn4,
getSrcLocRn4,
lookupValue, lookupValueEvenIfInvisible,
lookupClassOp, lookupFixityOp,
lookupTyCon, lookupTyConEvenIfInvisible,
lookupClass,
extendSS2, extendSS,
namesFromProtoNames,
TyVarNamesEnv(..), mkTyVarNamesEnv, domTyVarNamesEnv,
lookupTyVarName, nullTyVarNamesEnv, catTyVarNamesEnvs
-- for completeness
) where
import Ubiq{-uitous-}
import Bag ( emptyBag, isEmptyBag, unionBags, snocBag, Bag )
import CmdLineOpts ( opt_ShowPragmaNameErrs, opt_NameShadowingNotOK )
import ErrUtils
import FiniteMap ( emptyFM, addListToFM, addToFM, lookupFM )
import Name ( invisibleName, isTyConName, isClassName,
isClassOpName, isUnboundName, Name(..)
)
import NameTypes ( mkShortName, ShortName{-instances-} )
import Outputable ( pprNonOp )
import Pretty
import ProtoName ( eqProtoName, cmpByLocalName, ProtoName(..) )
import RnUtils ( dupNamesErr, GlobalNameMappers(..) )
import SrcLoc ( mkUnknownSrcLoc, SrcLoc{-instance-} )
import UniqSet ( mkUniqSet, minusUniqSet, UniqSet(..) )
import UniqSupply ( getUniques, splitUniqSupply )
import Util ( assoc, removeDups, zipWithEqual, panic )
infixr 9 `thenRn4`, `thenRn4_`
\end{code}
%************************************************************************
%* *
\subsection[RnMonad4]{Plain @Rename@ monadery for pass~4}
%* *
%************************************************************************
\begin{code}
type ScopeStack = FiniteMap FAST_STRING Name
type Rn4M result
= GlobalNameMappers
-> ScopeStack
-> Bag Error
-> UniqSupply
-> SrcLoc
-> (result, Bag Error)
{-# INLINE andRn4 #-}
{-# INLINE thenRn4 #-}
{-# INLINE thenLazilyRn4 #-}
{-# INLINE thenRn4_ #-}
{-# INLINE returnRn4 #-}
initRn4 :: GlobalNameMappers
-> Rn4M result
-> UniqSupply
-> (result, Bag Error)
initRn4 gnfs renamer init_us
= renamer gnfs emptyFM emptyBag init_us mkUnknownSrcLoc
thenRn4, thenLazilyRn4
:: Rn4M a -> (a -> Rn4M b) -> Rn4M b
thenRn4_ :: Rn4M a -> Rn4M b -> Rn4M b
andRn4 :: (a -> a -> a) -> Rn4M a -> Rn4M a -> Rn4M a
thenRn4 expr cont gnfs ss errs uniqs locn
= case (splitUniqSupply uniqs) of { (s1, s2) ->
case (expr gnfs ss errs s1 locn) of { (res1, errs1) ->
case (cont res1 gnfs ss errs1 s2 locn) of { (res2, errs2) ->
(res2, errs2) }}}
thenLazilyRn4 expr cont gnfs ss errs uniqs locn
= let
(s1, s2) = splitUniqSupply uniqs
(res1, errs1) = expr gnfs ss errs s1 locn
(res2, errs2) = cont res1 gnfs ss errs1 s2 locn
in
(res2, errs2)
thenRn4_ expr cont gnfs ss errs uniqs locn
= case (splitUniqSupply uniqs) of { (s1, s2) ->
case (expr gnfs ss errs s1 locn) of { (_, errs1) ->
case (cont gnfs ss errs1 s2 locn) of { (res2, errs2) ->
(res2, errs2) }}}
andRn4 combiner m1 m2 gnfs ss errs us locn
= case (splitUniqSupply us) of { (s1, s2) ->
case (m1 gnfs ss errs s1 locn) of { (res1, errs1) ->
case (m2 gnfs ss errs1 s2 locn) of { (res2, errs2) ->
(combiner res1 res2, errs2) }}}
returnRn4 :: a -> Rn4M a
returnRn4 result gnfs ss errs_so_far uniqs locn
= (result, errs_so_far)
failButContinueRn4 :: a -> Error -> Rn4M a
failButContinueRn4 res err gnfs ss errs_so_far uniqs locn
= (res, errs_so_far `snocBag` err)
addErrRn4 :: Error -> Rn4M ()
addErrRn4 err gnfs ss errs_so_far uniqs locn
= ((), errs_so_far `snocBag` err)
\end{code}
When we're looking at interface pragmas, we want to be able to recover
back to a ``I don't know anything pragmatic'' state if we encounter
some problem. @recoverQuietlyRn4@ is given a ``use-this-instead'' value,
as well as the action to perform. This code is intentionally very lazy,
returning a triple immediately, no matter what.
\begin{code}
recoverQuietlyRn4 :: a -> Rn4M a -> Rn4M a
recoverQuietlyRn4 use_this_if_err action gnfs ss errs_so_far uniqs locn
= let
(result, errs_out)
= case (action gnfs ss emptyBag{-leav out errs-} uniqs locn) of
(result1, errs1) ->
if isEmptyBag errs1 then -- all's well! (but retain incoming errs)
(result1, errs_so_far)
else -- give up; return *incoming* UniqueSupply...
(use_this_if_err,
if opt_ShowPragmaNameErrs
then errs_so_far `unionBags` errs1
else errs_so_far) -- toss errs, otherwise
in
(result, errs_out)
\end{code}
\begin{code}
mapRn4 :: (a -> Rn4M b) -> [a] -> Rn4M [b]
mapRn4 f [] = returnRn4 []
mapRn4 f (x:xs)
= f x `thenRn4` \ r ->
mapRn4 f xs `thenRn4` \ rs ->
returnRn4 (r:rs)
mapAndUnzipRn4 :: (a -> Rn4M (b,c)) -> [a] -> Rn4M ([b],[c])
mapAndUnzipRn4 f [] = returnRn4 ([],[])
mapAndUnzipRn4 f (x:xs)
= f x `thenRn4` \ (r1, r2) ->
mapAndUnzipRn4 f xs `thenRn4` \ (rs1, rs2) ->
returnRn4 (r1:rs1, r2:rs2)
\end{code}
\begin{code}
pushSrcLocRn4 :: SrcLoc -> Rn4M a -> Rn4M a
pushSrcLocRn4 locn exp gnfs ss errs_so_far uniq_supply old_locn
= exp gnfs ss errs_so_far uniq_supply locn
getSrcLocRn4 :: Rn4M SrcLoc
getSrcLocRn4 gnfs ss errs_so_far uniq_supply locn
= returnRn4 locn gnfs ss errs_so_far uniq_supply locn
\end{code}
\begin{code}
getNextUniquesFromRn4 :: Int -> Rn4M [Unique]
getNextUniquesFromRn4 n gnfs ss errs_so_far us locn
= case (getUniques n us) of { next_uniques ->
(next_uniques, errs_so_far) }
\end{code}
*********************************************************
* *
\subsection{Making new names}
* *
*********************************************************
@namesFromProtoNames@ takes a bunch of protonames, which are defined
together in a group (eg a pattern or set of bindings), checks they
are distinct, and creates new full names for them.
\begin{code}
namesFromProtoNames :: String -- Documentation string
-> [(ProtoName, SrcLoc)]
-> Rn4M [Name]
namesFromProtoNames kind pnames_w_src_loc gnfs ss errs_so_far us locn
= (mapRn4 (addErrRn4 . dupNamesErr kind) dups `thenRn4_`
mkNewNames goodies
) {-Rn4-} gnfs ss errs_so_far us locn
where
(goodies, dups) = removeDups cmp pnames_w_src_loc
-- We want to compare their local names rather than their
-- full protonames. It probably doesn't matter here, but it
-- does in RnPass3.lhs!
cmp (a, _) (b, _) = cmpByLocalName a b
\end{code}
@mkNewNames@ assumes the names are unique.
\begin{code}
mkNewNames :: [(ProtoName, SrcLoc)] -> Rn4M [Name]
mkNewNames pnames_w_locs
= getNextUniquesFromRn4 (length pnames_w_locs) `thenRn4` \ uniqs ->
returnRn4 (zipWithEqual new_short_name uniqs pnames_w_locs)
where
new_short_name uniq (Unk str, srcloc) -- gotta be an Unk...
= Short uniq (mkShortName str srcloc)
\end{code}
*********************************************************
* *
\subsection{Local scope extension and lookup}
* *
*********************************************************
If the input name is an @Imp@, @lookupValue@ looks it up in the GNF.
If it is an @Unk@, it looks it up first in the local environment
(scope stack), and if it isn't found there, then in the value GNF. If
it isn't found at all, @lookupValue@ adds an error message, and
returns an @Unbound@ name.
\begin{code}
unboundName :: ProtoName -> Name
unboundName pn
= Unbound (grab_string pn)
where
grab_string (Unk s) = s
grab_string (Qunk _ s) = s
grab_string (Imp _ _ _ s) = s
\end{code}
@lookupValue@ looks up a non-invisible value;
@lookupValueEvenIfInvisible@ gives a successful lookup even if the
value is not visible to the user (e.g., came out of a pragma).
@lookup_val@ is the help function to do the work.
\begin{code}
lookupValue v {-Rn4-} gnfs ss errs_so_far us locn
= (lookup_val v `thenLazilyRn4` \ name ->
if invisibleName name
then failButContinueRn4 (unboundName v) (unknownNameErr "value" v mkUnknownSrcLoc)
else returnRn4 name
) {-Rn4-} gnfs ss errs_so_far us locn
lookupValueEvenIfInvisible v = lookup_val v
lookup_val :: ProtoName -> Rn4M Name
lookup_val pname@(Unk v) gnfs@(v_gnf, tc_gnf) ss a b locn
= case (lookupFM ss v) of
Just name -> returnRn4 name gnfs ss a b locn
Nothing -> case (v_gnf pname) of
Just name -> returnRn4 name gnfs ss a b locn
Nothing -> failButContinueRn4 (unboundName pname)
(unknownNameErr "value" pname locn)
gnfs ss a b locn
lookup_val (Qunk _ _) _ _ _ _ _ = panic "RnMonad4:lookup_val:Qunk"
-- If it ain't an Unk it must be in the global name fun; that includes
-- prelude things.
lookup_val pname gnfs@(v_gnf, tc_gnf) ss a b locn
= case (v_gnf pname) of
Just name -> returnRn4 name gnfs ss a b locn
Nothing -> failButContinueRn4 (unboundName pname)
(unknownNameErr "value" pname locn)
gnfs ss a b locn
\end{code}
Looking up the operators in a fixity decl is done differently. We
want to simply drop any fixity decls which refer to operators which
aren't in scope. Unfortunately, such fixity decls {\em will} appear
because the parser collects *all* the fixity decls from {\em all} the
imported interfaces (regardless of selective import), and dumps them
together as the module fixity decls. This is really a bug. In
particular:
\begin{itemize}
\item
We won't complain about fixity decls for operators which aren't
declared.
\item
We won't attach the right fixity to something which has been renamed.
\end{itemize}
We're not going to export Prelude-related fixities (ToDo: correctly),
so we nuke those, too.
\begin{code}
lookupFixityOp (Prel _) gnfs@(v_gnf, tc_gnf) = returnRn4 Nothing gnfs
lookupFixityOp pname gnfs@(v_gnf, tc_gnf) = returnRn4 (v_gnf pname) gnfs
\end{code}
\begin{code}
lookupTyCon, lookupTyConEvenIfInvisible :: ProtoName -> Rn4M Name
-- The global name funs handle Prel things
lookupTyCon tc {-Rn4-} gnfs ss errs_so_far us locn
= (lookup_tycon tc `thenLazilyRn4` \ name ->
if invisibleName name
then failButContinueRn4 (unboundName tc) (unknownNameErr "type constructor" tc mkUnknownSrcLoc)
else returnRn4 name
) {-Rn4-} gnfs ss errs_so_far us locn
lookupTyConEvenIfInvisible tc = lookup_tycon tc
lookup_tycon (Prel name) gnfs ss a b locn = returnRn4 name gnfs ss a b locn
lookup_tycon pname gnfs@(v_gnf, tc_gnf) ss a b locn
= case (tc_gnf pname) of
Just name | isTyConName name -> returnRn4 name gnfs ss a b locn
_ -> failButContinueRn4 (unboundName pname)
(unknownNameErr "type constructor" pname locn)
gnfs ss a b locn
\end{code}
\begin{code}
lookupClass :: ProtoName -> Rn4M Name
lookupClass pname gnfs@(v_gnf, tc_gnf) ss a b locn
= case (tc_gnf pname) of
Just name | isClassName name -> returnRn4 name gnfs ss a b locn
_ -> failButContinueRn4 (unboundName pname)
(unknownNameErr "class" pname locn)
gnfs ss a b locn
\end{code}
@lookupClassOp@ is used when looking up the lhs identifiers in a class
or instance decl. It checks that the name it finds really is a class
op, and that its class matches that of the class or instance decl
being looked at.
\begin{code}
lookupClassOp :: Name -> ProtoName -> Rn4M Name
lookupClassOp class_name pname gnfs@(v_gnf, tc_gnf) ss a b locn
= case v_gnf pname of
Just op_name | isClassOpName class_name op_name
|| isUnboundName class_name -- avoid spurious errors
-> returnRn4 op_name gnfs ss a b locn
other -> failButContinueRn4 (unboundName pname)
(badClassOpErr class_name pname locn)
gnfs ss a b locn
\end{code}
@extendSS@ extends the scope; @extendSS2@ also removes the newly bound
free vars from the result.
\begin{code}
extendSS :: [Name] -- Newly bound names
-> Rn4M a
-> Rn4M a
extendSS binders expr gnfs ss errs us locn
= case (extend binders ss gnfs ss errs us locn) of { (new_ss, new_errs) ->
expr gnfs new_ss new_errs us locn }
where
extend :: [Name] -> ScopeStack -> Rn4M ScopeStack
extend names ss
= if opt_NameShadowingNotOK then
hard_way names ss
else -- ignore shadowing; blast 'em in
returnRn4 (
addListToFM ss [ (getOccurrenceName x, n) | n@(Short _ x) <- names]
)
hard_way [] ss = returnRn4 ss
hard_way (name@(Short _ sname):names) ss
= let
str = getOccurrenceName sname
in
(case (lookupFM ss str) of
Nothing -> returnRn4 (addToFM ss str name)
Just _ -> failButContinueRn4 ss (shadowedNameErr name locn)
) `thenRn4` \ new_ss ->
hard_way names new_ss
extendSS2 :: [Name] -- Newly bound names
-> Rn4M (a, UniqSet Name)
-> Rn4M (a, UniqSet Name)
extendSS2 binders expr gnfs ss errs_so_far us locn
= case (extendSS binders expr gnfs ss errs_so_far us locn) of
((e2, freevars), errs)
-> ((e2, freevars `minusUniqSet` (mkUniqSet binders)),
errs)
\end{code}
The free var set returned by @(extendSS binders m)@ is that returned
by @m@, {\em minus} binders.
*********************************************************
* *
\subsection{mkTyVarNamesEnv}
* *
*********************************************************
\begin{code}
type TyVarNamesEnv = [(ProtoName, Name)]
nullTyVarNamesEnv :: TyVarNamesEnv
nullTyVarNamesEnv = []
catTyVarNamesEnvs :: TyVarNamesEnv -> TyVarNamesEnv -> TyVarNamesEnv
catTyVarNamesEnvs e1 e2 = e1 ++ e2
domTyVarNamesEnv :: TyVarNamesEnv -> [ProtoName]
domTyVarNamesEnv env = map fst env
\end{code}
@mkTyVarNamesEnv@ checks for duplicates, and complains if so.
\begin{code}
mkTyVarNamesEnv
:: SrcLoc
-> [ProtoName] -- The type variables
-> Rn4M (TyVarNamesEnv,[Name]) -- Environment and renamed tyvars
mkTyVarNamesEnv src_loc tyvars {-Rn4-} gnfs ss errs_so_far us locn
= (namesFromProtoNames "type variable"
(tyvars `zip` repeat src_loc) `thenRn4` \ tyvars2 ->
-- tyvars2 may not be in the same order as tyvars, so we need some
-- jiggery pokery to build the right tyvar env, and return the
-- renamed tyvars in the original order.
let tv_string_name_pairs = extend tyvars2 []
tv_env = map (lookup tv_string_name_pairs) tyvars
tyvars2_in_orig_order = map snd tv_env
in
returnRn4 (tv_env, tyvars2_in_orig_order)
) {-Rn4-} gnfs ss errs_so_far us locn
where
extend :: [Name] -> [(FAST_STRING, Name)] -> [(FAST_STRING, Name)]
extend [] ss = ss
extend (name@(Short _ sname):names) ss
= (getOccurrenceName sname, name) : extend names ss
lookup :: [(FAST_STRING, Name)] -> ProtoName -> (ProtoName, Name)
lookup pairs tyvar_pn
= (tyvar_pn, assoc "mkTyVarNamesEnv" pairs (getOccurrenceName tyvar_pn))
\end{code}
\begin{code}
lookupTyVarName :: TyVarNamesEnv -> ProtoName -> Rn4M Name
lookupTyVarName env pname {-Rn4-} gnfs ss errs_so_far us locn
= (case (assoc_maybe env pname) of
Just name -> returnRn4 name
Nothing -> getSrcLocRn4 `thenRn4` \ loc ->
failButContinueRn4 (unboundName pname)
(unknownNameErr "type variable" pname loc)
) {-Rn4-} gnfs ss errs_so_far us locn
where
assoc_maybe [] _ = Nothing
assoc_maybe ((tv,xxx) : tvs) key
= if tv `eqProtoName` key then Just xxx else assoc_maybe tvs key
\end{code}
%************************************************************************
%* *
\subsection{Error messages}
%* *
%************************************************************************
\begin{code}
badClassOpErr clas op locn
= addErrLoc locn "" ( \ sty ->
ppBesides [ppChar '`', pprNonOp sty op, ppStr "' is not an operation of class `",
ppr sty clas, ppStr "'."] )
----------------------------
-- dupNamesErr: from RnUtils
---------------------------
shadowedNameErr shadow locn
= addShortErrLocLine locn ( \ sty ->
ppBesides [ppStr "more than one value with the same name (shadowing): ",
ppr sty shadow] )
------------------------------------------
unknownNameErr descriptor undef_thing locn
= addShortErrLocLine locn ( \ sty ->
ppBesides [ppStr "undefined ", ppStr descriptor, ppStr ": ",
pprNonOp sty undef_thing] )
\end{code}
|