[project @ 2000-03-23 17:45:17 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnBinds.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[RnBinds]{Renaming and dependency analysis of bindings}
5
6 This module does renaming and dependency analysis on value bindings in
7 the abstract syntax.  It does {\em not} do cycle-checks on class or
8 type-synonym declarations; those cannot be done at this stage because
9 they may be affected by renaming (which isn't fully worked out yet).
10
11 \begin{code}
12 module RnBinds (
13         rnTopBinds, rnTopMonoBinds,
14         rnMethodBinds, renameSigs,
15         rnBinds,
16         unknownSigErr
17    ) where
18
19 #include "HsVersions.h"
20
21 import {-# SOURCE #-} RnSource ( rnHsSigType )
22
23 import HsSyn
24 import HsBinds          ( sigsForMe )
25 import RdrHsSyn
26 import RnHsSyn
27 import RnMonad
28 import RnExpr           ( rnMatch, rnGRHSs, rnPat, checkPrecMatch )
29 import RnEnv            ( bindLocatedLocalsRn, lookupBndrRn, lookupGlobalOccRn,
30                           warnUnusedLocalBinds, mapFvRn, 
31                           FreeVars, emptyFVs, plusFV, plusFVs, unitFV, addOneFV,
32                           unknownNameErr
33                         )
34 import CmdLineOpts      ( opt_WarnMissingSigs )
35 import Digraph          ( stronglyConnComp, SCC(..) )
36 import Name             ( OccName, Name, nameOccName )
37 import NameSet
38 import RdrName          ( RdrName, rdrNameOcc  )
39 import BasicTypes       ( RecFlag(..), TopLevelFlag(..) )
40 import Util             ( thenCmp, removeDups )
41 import List             ( partition )
42 import ListSetOps       ( minusList )
43 import Bag              ( bagToList )
44 import FiniteMap        ( lookupFM, listToFM )
45 import Maybe            ( isJust )
46 import Outputable
47 \end{code}
48
49 -- ToDo: Put the annotations into the monad, so that they arrive in the proper
50 -- place and can be used when complaining.
51
52 The code tree received by the function @rnBinds@ contains definitions
53 in where-clauses which are all apparently mutually recursive, but which may
54 not really depend upon each other. For example, in the top level program
55 \begin{verbatim}
56 f x = y where a = x
57               y = x
58 \end{verbatim}
59 the definitions of @a@ and @y@ do not depend on each other at all.
60 Unfortunately, the typechecker cannot always check such definitions.
61 \footnote{Mycroft, A. 1984. Polymorphic type schemes and recursive
62 definitions. In Proceedings of the International Symposium on Programming,
63 Toulouse, pp. 217-39. LNCS 167. Springer Verlag.}
64 However, the typechecker usually can check definitions in which only the
65 strongly connected components have been collected into recursive bindings.
66 This is precisely what the function @rnBinds@ does.
67
68 ToDo: deal with case where a single monobinds binds the same variable
69 twice.
70
71 The vertag tag is a unique @Int@; the tags only need to be unique
72 within one @MonoBinds@, so that unique-Int plumbing is done explicitly
73 (heavy monad machinery not needed).
74
75 \begin{code}
76 type VertexTag  = Int
77 type Cycle      = [VertexTag]
78 type Edge       = (VertexTag, VertexTag)
79 \end{code}
80
81 %************************************************************************
82 %*                                                                      *
83 %* naming conventions                                                   *
84 %*                                                                      *
85 %************************************************************************
86
87 \subsection[name-conventions]{Name conventions}
88
89 The basic algorithm involves walking over the tree and returning a tuple
90 containing the new tree plus its free variables. Some functions, such
91 as those walking polymorphic bindings (HsBinds) and qualifier lists in
92 list comprehensions (@Quals@), return the variables bound in local
93 environments. These are then used to calculate the free variables of the
94 expression evaluated in these environments.
95
96 Conventions for variable names are as follows:
97 \begin{itemize}
98 \item
99 new code is given a prime to distinguish it from the old.
100
101 \item
102 a set of variables defined in @Exp@ is written @dvExp@
103
104 \item
105 a set of variables free in @Exp@ is written @fvExp@
106 \end{itemize}
107
108 %************************************************************************
109 %*                                                                      *
110 %* analysing polymorphic bindings (HsBinds, Bind, MonoBinds)            *
111 %*                                                                      *
112 %************************************************************************
113
114 \subsubsection[dep-HsBinds]{Polymorphic bindings}
115
116 Non-recursive expressions are reconstructed without any changes at top
117 level, although their component expressions may have to be altered.
118 However, non-recursive expressions are currently not expected as
119 \Haskell{} programs, and this code should not be executed.
120
121 Monomorphic bindings contain information that is returned in a tuple
122 (a @FlatMonoBindsInfo@) containing:
123
124 \begin{enumerate}
125 \item
126 a unique @Int@ that serves as the ``vertex tag'' for this binding.
127
128 \item
129 the name of a function or the names in a pattern. These are a set
130 referred to as @dvLhs@, the defined variables of the left hand side.
131
132 \item
133 the free variables of the body. These are referred to as @fvBody@.
134
135 \item
136 the definition's actual code. This is referred to as just @code@.
137 \end{enumerate}
138
139 The function @nonRecDvFv@ returns two sets of variables. The first is
140 the set of variables defined in the set of monomorphic bindings, while the
141 second is the set of free variables in those bindings.
142
143 The set of variables defined in a non-recursive binding is just the
144 union of all of them, as @union@ removes duplicates. However, the
145 free variables in each successive set of cumulative bindings is the
146 union of those in the previous set plus those of the newest binding after
147 the defined variables of the previous set have been removed.
148
149 @rnMethodBinds@ deals only with the declarations in class and
150 instance declarations.  It expects only to see @FunMonoBind@s, and
151 it expects the global environment to contain bindings for the binders
152 (which are all class operations).
153
154 %************************************************************************
155 %*                                                                      *
156 \subsubsection{ Top-level bindings}
157 %*                                                                      *
158 %************************************************************************
159
160 @rnTopBinds@ assumes that the environment already
161 contains bindings for the binders of this particular binding.
162
163 \begin{code}
164 rnTopBinds    :: RdrNameHsBinds -> RnMS (RenamedHsBinds, FreeVars)
165
166 rnTopBinds EmptyBinds                     = returnRn (EmptyBinds, emptyFVs)
167 rnTopBinds (MonoBind bind sigs _)         = rnTopMonoBinds bind sigs
168   -- The parser doesn't produce other forms
169
170
171 rnTopMonoBinds EmptyMonoBinds sigs 
172   = returnRn (EmptyBinds, emptyFVs)
173
174 rnTopMonoBinds mbinds sigs
175  =  mapRn lookupBndrRn binder_rdr_names `thenRn` \ binder_names ->
176     let
177         binder_set    = mkNameSet binder_names
178         binder_occ_fm = listToFM [(nameOccName x,x) | x <- binder_names]
179     in
180     renameSigs opt_WarnMissingSigs binder_set
181                (lookupSigOccRn binder_occ_fm) sigs `thenRn` \ (siglist, sig_fvs) ->
182     rn_mono_binds siglist mbinds                   `thenRn` \ (final_binds, bind_fvs) ->
183     returnRn (final_binds, bind_fvs `plusFV` sig_fvs)
184   where
185     binder_rdr_names = map fst (bagToList (collectMonoBinders mbinds))
186
187 -- the names appearing in the sigs have to be bound by 
188 -- this group's binders.
189 lookupSigOccRn binder_occ_fm rdr_name
190   = case lookupFM binder_occ_fm (rdrNameOcc rdr_name) of
191         Nothing -> failWithRn (mkUnboundName rdr_name)
192                               (unknownNameErr rdr_name)
193         Just x  -> returnRn x
194 \end{code}
195
196 %************************************************************************
197 %*                                                                      *
198 %*              Nested binds
199 %*                                                                      *
200 %************************************************************************
201
202 \subsubsection{Nested binds}
203
204 @rnMonoBinds@
205 \begin{itemize}
206 \item collects up the binders for this declaration group,
207 \item checks that they form a set
208 \item extends the environment to bind them to new local names
209 \item calls @rnMonoBinds@ to do the real work
210 \end{itemize}
211 %
212 \begin{code}
213 rnBinds       :: RdrNameHsBinds 
214               -> (RenamedHsBinds -> RnMS (result, FreeVars))
215               -> RnMS (result, FreeVars)
216
217 rnBinds EmptyBinds             thing_inside = thing_inside EmptyBinds
218 rnBinds (MonoBind bind sigs _) thing_inside = rnMonoBinds bind sigs thing_inside
219   -- the parser doesn't produce other forms
220
221
222 rnMonoBinds :: RdrNameMonoBinds 
223             -> [RdrNameSig]
224             -> (RenamedHsBinds -> RnMS (result, FreeVars))
225             -> RnMS (result, FreeVars)
226
227 rnMonoBinds EmptyMonoBinds sigs thing_inside = thing_inside EmptyBinds
228
229 rnMonoBinds mbinds sigs thing_inside -- Non-empty monobinds
230   =     -- Extract all the binders in this group,
231         -- and extend current scope, inventing new names for the new binders
232         -- This also checks that the names form a set
233     bindLocatedLocalsRn (text "a binding group") mbinders_w_srclocs
234     $ \ new_mbinders ->
235     let
236         binder_set    = mkNameSet new_mbinders
237         binder_occ_fm = listToFM [(nameOccName x,x) | x <- new_mbinders]
238
239            -- Weed out the fixity declarations that do not
240            -- apply to any of the binders in this group.
241         (sigs_for_me, fixes_not_for_me) = partition forLocalBind sigs
242
243         forLocalBind (FixSig sig@(FixitySig name _ _ )) =
244             isJust (lookupFM binder_occ_fm (rdrNameOcc name))
245         forLocalBind _ = True
246     in
247         -- Rename the signatures
248     renameSigs False binder_set
249                (lookupSigOccRn binder_occ_fm) sigs_for_me   `thenRn` \ (siglist, sig_fvs) ->
250
251         -- Report the fixity declarations in this group that 
252         -- don't refer to any of the group's binders.
253         -- Then install the fixity declarations that do apply here
254         -- Notice that they scope over thing_inside too
255     mapRn_ (unknownSigErr) fixes_not_for_me     `thenRn_`
256     let
257         fixity_sigs = [(name,sig) | FixSig sig@(FixitySig name _ _) <- siglist ]
258     in
259     extendFixityEnv fixity_sigs $
260
261     rn_mono_binds siglist mbinds           `thenRn` \ (binds, bind_fvs) ->
262
263     -- Now do the "thing inside", and deal with the free-variable calculations
264     thing_inside binds                     `thenRn` \ (result,result_fvs) ->
265     let
266         all_fvs        = result_fvs `plusFV` bind_fvs `plusFV` sig_fvs
267         unused_binders = nameSetToList (binder_set `minusNameSet` all_fvs)
268     in
269     warnUnusedLocalBinds unused_binders `thenRn_`
270     returnRn (result, delListFromNameSet all_fvs new_mbinders)
271   where
272     mbinders_w_srclocs = bagToList (collectMonoBinders mbinds)
273 \end{code}
274
275
276 %************************************************************************
277 %*                                                                      *
278 \subsubsection{         MonoBinds -- the main work is done here}
279 %*                                                                      *
280 %************************************************************************
281
282 @rn_mono_binds@ is used by {\em both} top-level and nested bindings.
283 It assumes that all variables bound in this group are already in scope.
284 This is done {\em either} by pass 3 (for the top-level bindings),
285 {\em or} by @rnMonoBinds@ (for the nested ones).
286
287 \begin{code}
288 rn_mono_binds :: [RenamedSig]           -- Signatures attached to this group
289               -> RdrNameMonoBinds       
290               -> RnMS (RenamedHsBinds,  -- 
291                          FreeVars)      -- Free variables
292
293 rn_mono_binds siglist mbinds
294   =
295          -- Rename the bindings, returning a MonoBindsInfo
296          -- which is a list of indivisible vertices so far as
297          -- the strongly-connected-components (SCC) analysis is concerned
298     flattenMonoBinds siglist mbinds             `thenRn` \ mbinds_info ->
299
300          -- Do the SCC analysis
301     let 
302         edges       = mkEdges (mbinds_info `zip` [(0::Int)..])
303         scc_result  = stronglyConnComp edges
304         final_binds = foldr1 ThenBinds (map reconstructCycle scc_result)
305
306          -- Deal with bound and free-var calculation
307         rhs_fvs = plusFVs [fvs | (_,fvs,_,_) <- mbinds_info]
308     in
309     returnRn (final_binds, rhs_fvs)
310 \end{code}
311
312 @flattenMonoBinds@ is ever-so-slightly magical in that it sticks
313 unique ``vertex tags'' on its output; minor plumbing required.
314
315 Sigh --- need to pass along the signatures for the group of bindings,
316 in case any of them \fbox{\ ???\ } 
317
318 \begin{code}
319 flattenMonoBinds :: [RenamedSig]                -- Signatures
320                  -> RdrNameMonoBinds
321                  -> RnMS [FlatMonoBindsInfo]
322
323 flattenMonoBinds sigs EmptyMonoBinds = returnRn []
324
325 flattenMonoBinds sigs (AndMonoBinds bs1 bs2)
326   = flattenMonoBinds sigs bs1   `thenRn` \ flat1 ->
327     flattenMonoBinds sigs bs2   `thenRn` \ flat2 ->
328     returnRn (flat1 ++ flat2)
329
330 flattenMonoBinds sigs (PatMonoBind pat grhss locn)
331   = pushSrcLocRn locn                   $
332     rnPat pat                           `thenRn` \ (pat', pat_fvs) ->
333
334          -- Find which things are bound in this group
335     let
336         names_bound_here = mkNameSet (collectPatBinders pat')
337         sigs_for_me      = sigsForMe (`elemNameSet` names_bound_here) sigs
338     in
339     rnGRHSs grhss                       `thenRn` \ (grhss', fvs) ->
340     returnRn 
341         [(names_bound_here,
342           fvs `plusFV` pat_fvs,
343           PatMonoBind pat' grhss' locn,
344           sigs_for_me
345          )]
346
347 flattenMonoBinds sigs (FunMonoBind name inf matches locn)
348   = pushSrcLocRn locn                                   $
349     lookupBndrRn name                                   `thenRn` \ new_name ->
350     let
351         sigs_for_me = sigsForMe (new_name ==) sigs
352     in
353     mapFvRn rnMatch matches                             `thenRn` \ (new_matches, fvs) ->
354     mapRn_ (checkPrecMatch inf new_name) new_matches    `thenRn_`
355     returnRn
356       [(unitNameSet new_name,
357         fvs,
358         FunMonoBind new_name inf new_matches locn,
359         sigs_for_me
360         )]
361 \end{code}
362
363
364 @rnMethodBinds@ is used for the method bindings of a class and an instance
365 declaration.   Like @rnMonoBinds@ but without dependency analysis.
366
367 NOTA BENE: we record each {\em binder} of a method-bind group as a free variable.
368 That's crucial when dealing with an instance decl:
369 \begin{verbatim}
370         instance Foo (T a) where
371            op x = ...
372 \end{verbatim}
373 This might be the {\em sole} occurrence of @op@ for an imported class @Foo@,
374 and unless @op@ occurs we won't treat the type signature of @op@ in the class
375 decl for @Foo@ as a source of instance-decl gates.  But we should!  Indeed,
376 in many ways the @op@ in an instance decl is just like an occurrence, not
377 a binder.
378
379 \begin{code}
380 rnMethodBinds :: RdrNameMonoBinds -> RnMS (RenamedMonoBinds, FreeVars)
381
382 rnMethodBinds EmptyMonoBinds = returnRn (EmptyMonoBinds, emptyFVs)
383
384 rnMethodBinds (AndMonoBinds mb1 mb2)
385   = rnMethodBinds mb1   `thenRn` \ (mb1', fvs1) ->
386     rnMethodBinds mb2   `thenRn` \ (mb2', fvs2) ->
387     returnRn (mb1' `AndMonoBinds` mb2', fvs1 `plusFV` fvs2)
388
389 rnMethodBinds (FunMonoBind name inf matches locn)
390   = pushSrcLocRn locn                                   $
391
392     lookupGlobalOccRn name                              `thenRn` \ sel_name -> 
393         -- We use the selector name as the binder
394
395     mapFvRn rnMatch matches                             `thenRn` \ (new_matches, fvs) ->
396     mapRn_ (checkPrecMatch inf sel_name) new_matches    `thenRn_`
397     returnRn (FunMonoBind sel_name inf new_matches locn, fvs `addOneFV` sel_name)
398
399 rnMethodBinds (PatMonoBind (VarPatIn name) grhss locn)
400   = pushSrcLocRn locn                   $
401     lookupGlobalOccRn name              `thenRn` \ sel_name -> 
402     rnGRHSs grhss                       `thenRn` \ (grhss', fvs) ->
403     returnRn (PatMonoBind (VarPatIn sel_name) grhss' locn, fvs `addOneFV` sel_name)
404
405 -- Can't handle method pattern-bindings which bind multiple methods.
406 rnMethodBinds mbind@(PatMonoBind other_pat _ locn)
407   = pushSrcLocRn locn   $
408     failWithRn (EmptyMonoBinds, emptyFVs) (methodBindErr mbind)
409 \end{code}
410
411
412 %************************************************************************
413 %*                                                                      *
414 \subsection[reconstruct-deps]{Reconstructing dependencies}
415 %*                                                                      *
416 %************************************************************************
417
418 This @MonoBinds@- and @ClassDecls@-specific code is segregated here,
419 as the two cases are similar.
420
421 \begin{code}
422 reconstructCycle :: SCC FlatMonoBindsInfo
423                  -> RenamedHsBinds
424
425 reconstructCycle (AcyclicSCC (_, _, binds, sigs))
426   = MonoBind binds sigs NonRecursive
427
428 reconstructCycle (CyclicSCC cycle)
429   = MonoBind this_gp_binds this_gp_sigs Recursive
430   where
431     this_gp_binds      = foldr1 AndMonoBinds [binds | (_, _, binds, _) <- cycle]
432     this_gp_sigs       = foldr1 (++)         [sigs  | (_, _, _, sigs) <- cycle]
433 \end{code}
434
435 %************************************************************************
436 %*                                                                      *
437 \subsubsection{ Manipulating FlatMonoBindInfo}
438 %*                                                                      *
439 %************************************************************************
440
441 During analysis a @MonoBinds@ is flattened to a @FlatMonoBindsInfo@.
442 The @RenamedMonoBinds@ is always an empty bind, a pattern binding or
443 a function binding, and has itself been dependency-analysed and
444 renamed.
445
446 \begin{code}
447 type FlatMonoBindsInfo
448   = (NameSet,                   -- Set of names defined in this vertex
449      NameSet,                   -- Set of names used in this vertex
450      RenamedMonoBinds,
451      [RenamedSig])              -- Signatures, if any, for this vertex
452
453 mkEdges :: [(FlatMonoBindsInfo, VertexTag)] -> [(FlatMonoBindsInfo, VertexTag, [VertexTag])]
454
455 mkEdges flat_info
456   = [ (info, tag, dest_vertices (nameSetToList names_used))
457     | (info@(names_defined, names_used, mbind, sigs), tag) <- flat_info
458     ]
459   where
460          -- An edge (v,v') indicates that v depends on v'
461     dest_vertices src_mentions = [ target_vertex
462                                  | ((names_defined, _, _, _), target_vertex) <- flat_info,
463                                    mentioned_name <- src_mentions,
464                                    mentioned_name `elemNameSet` names_defined
465                                  ]
466 \end{code}
467
468
469 %************************************************************************
470 %*                                                                      *
471 \subsubsection[dep-Sigs]{Signatures (and user-pragmas for values)}
472 %*                                                                      *
473 %************************************************************************
474
475 @renameSigs@ checks for:
476 \begin{enumerate}
477 \item more than one sig for one thing;
478 \item signatures given for things not bound here;
479 \item with suitably flaggery, that all top-level things have type signatures.
480 \end{enumerate}
481 %
482 At the moment we don't gather free-var info from the types in
483 signatures.  We'd only need this if we wanted to report unused tyvars.
484
485 \begin{code}
486 renameSigs ::  Bool             -- True => warn if (required) type signatures are missing.
487             -> NameSet          -- Set of names bound in this group
488             -> (RdrName -> RnMS Name)
489             -> [RdrNameSig]
490             -> RnMS ([RenamedSig], FreeVars)     -- List of Sig constructors
491
492 renameSigs sigs_required binders lookup_occ_nm sigs
493   =      -- Rename the signatures
494     mapFvRn (renameSig lookup_occ_nm) sigs      `thenRn` \ (sigs', fvs) ->
495
496         -- Check for (a) duplicate signatures
497         --           (b) signatures for things not in this group
498         --           (c) optionally, bindings with no signature
499     let
500         (goodies, dups) = removeDups cmp_sig (sigsForMe (not . isUnboundName) sigs')
501         not_this_group  = sigsForMe (not . (`elemNameSet` binders)) goodies
502         type_sig_vars   = [n | Sig n _ _     <- goodies]
503         un_sigd_binders | sigs_required = nameSetToList binders `minusList` type_sig_vars
504                         | otherwise     = []
505     in
506     mapRn_ dupSigDeclErr dups                           `thenRn_`
507     mapRn_ unknownSigErr not_this_group                 `thenRn_`
508     mapRn_ (addWarnRn.missingSigWarn) un_sigd_binders   `thenRn_`
509     returnRn (sigs', fvs)       
510                 -- bad ones and all:
511                 -- we need bindings of *some* sort for every name
512
513 -- We use lookupOccRn in the signatures, which is a little bit unsatisfactory
514 -- because this won't work for:
515 --      instance Foo T where
516 --        {-# INLINE op #-}
517 --        Baz.op = ...
518 -- We'll just rename the INLINE prag to refer to whatever other 'op'
519 -- is in scope.  (I'm assuming that Baz.op isn't in scope unqualified.)
520 -- Doesn't seem worth much trouble to sort this.
521
522 renameSig :: (RdrName -> RnMS Name) -> Sig RdrName -> RnMS (Sig Name, FreeVars)
523
524 renameSig lookup_occ_nm (Sig v ty src_loc)
525   = pushSrcLocRn src_loc $
526     lookup_occ_nm v                             `thenRn` \ new_v ->
527     rnHsSigType (quotes (ppr v)) ty             `thenRn` \ (new_ty,fvs) ->
528     returnRn (Sig new_v new_ty src_loc, fvs `addOneFV` new_v)
529
530 renameSig _ (SpecInstSig ty src_loc)
531   = pushSrcLocRn src_loc $
532     rnHsSigType (text "A SPECIALISE instance pragma") ty `thenRn` \ (new_ty, fvs) ->
533     returnRn (SpecInstSig new_ty src_loc, fvs)
534
535 renameSig lookup_occ_nm (SpecSig v ty src_loc)
536   = pushSrcLocRn src_loc $
537     lookup_occ_nm v                     `thenRn` \ new_v ->
538     rnHsSigType (quotes (ppr v)) ty     `thenRn` \ (new_ty,fvs) ->
539     returnRn (SpecSig new_v new_ty src_loc, fvs `addOneFV` new_v)
540
541 renameSig lookup_occ_nm (FixSig (FixitySig v fix src_loc))
542   = pushSrcLocRn src_loc $
543     lookup_occ_nm v             `thenRn` \ new_v ->
544     returnRn (FixSig (FixitySig new_v fix src_loc), unitFV new_v)
545
546 renameSig lookup_occ_nm (DeprecSig (Deprecation ie txt) src_loc)
547   = pushSrcLocRn src_loc $
548     renameIE lookup_occ_nm ie   `thenRn` \ (new_ie, fvs) ->
549     returnRn (DeprecSig (Deprecation new_ie txt) src_loc, fvs)
550
551 renameSig lookup_occ_nm (InlineSig v p src_loc)
552   = pushSrcLocRn src_loc $
553     lookup_occ_nm v             `thenRn` \ new_v ->
554     returnRn (InlineSig new_v p src_loc, unitFV new_v)
555
556 renameSig lookup_occ_nm (NoInlineSig v p src_loc)
557   = pushSrcLocRn src_loc $
558     lookup_occ_nm v             `thenRn` \ new_v ->
559     returnRn (NoInlineSig new_v p src_loc, unitFV new_v)
560 \end{code}
561
562 \begin{code}
563 renameIE :: (RdrName -> RnMS Name) -> IE RdrName -> RnMS (IE Name, FreeVars)
564 renameIE lookup_occ_nm (IEVar v)
565   = lookup_occ_nm v             `thenRn` \ new_v ->
566     returnRn (IEVar new_v, unitFV new_v)
567
568 renameIE lookup_occ_nm (IEThingAbs v)
569   = lookup_occ_nm v             `thenRn` \ new_v ->
570     returnRn (IEThingAbs new_v, unitFV new_v)
571
572 renameIE lookup_occ_nm (IEThingAll v)
573   = lookup_occ_nm v             `thenRn` \ new_v ->
574     returnRn (IEThingAll new_v, unitFV new_v)
575
576 renameIE lookup_occ_nm (IEThingWith v vs)
577   = lookup_occ_nm v             `thenRn` \ new_v ->
578     mapRn lookup_occ_nm vs      `thenRn` \ new_vs ->
579     returnRn (IEThingWith new_v new_vs, plusFVs [ unitFV x | x <- new_v:new_vs ])
580
581 renameIE lookup_occ_nm (IEModuleContents m)
582   = returnRn (IEModuleContents m, emptyFVs)
583 \end{code}
584
585 Checking for distinct signatures; oh, so boring
586
587
588 \begin{code}
589 cmp_sig :: RenamedSig -> RenamedSig -> Ordering
590 cmp_sig (Sig n1 _ _)         (Sig n2 _ _)         = n1 `compare` n2
591 cmp_sig (DeprecSig (Deprecation ie1 _) _)
592         (DeprecSig (Deprecation ie2 _) _)         = cmp_ie ie1 ie2
593 cmp_sig (InlineSig n1 _ _)   (InlineSig n2 _ _)   = n1 `compare` n2
594 cmp_sig (NoInlineSig n1 _ _) (NoInlineSig n2 _ _) = n1 `compare` n2
595 cmp_sig (SpecInstSig ty1 _)  (SpecInstSig ty2 _)  = cmpHsType compare ty1 ty2
596 cmp_sig (SpecSig n1 ty1 _)   (SpecSig n2 ty2 _) 
597   = -- may have many specialisations for one value;
598     -- but not ones that are exactly the same...
599         thenCmp (n1 `compare` n2) (cmpHsType compare ty1 ty2)
600
601 cmp_sig other_1 other_2                                 -- Tags *must* be different
602   | (sig_tag other_1) _LT_ (sig_tag other_2) = LT 
603   | otherwise                                = GT
604
605 cmp_ie :: IE Name -> IE Name -> Ordering
606 cmp_ie (IEVar            n1  ) (IEVar            n2  ) = n1 `compare` n2
607 cmp_ie (IEThingAbs       n1  ) (IEThingAbs       n2  ) = n1 `compare` n2
608 cmp_ie (IEThingAll       n1  ) (IEThingAll       n2  ) = n1 `compare` n2
609 -- Hmmm...
610 cmp_ie (IEThingWith      n1 _) (IEThingWith      n2 _) = n1 `compare` n2
611 cmp_ie (IEModuleContents _   ) (IEModuleContents _   ) = EQ
612
613 sig_tag (Sig n1 _ _)               = (ILIT(1) :: FAST_INT)
614 sig_tag (SpecSig n1 _ _)           = ILIT(2)
615 sig_tag (InlineSig n1 _ _)         = ILIT(3)
616 sig_tag (NoInlineSig n1 _ _)       = ILIT(4)
617 sig_tag (SpecInstSig _ _)          = ILIT(5)
618 sig_tag (FixSig _)                 = ILIT(6)
619 sig_tag (DeprecSig _ _)            = ILIT(7)
620 sig_tag _                          = panic# "tag(RnBinds)"
621 \end{code}
622
623 %************************************************************************
624 %*                                                                      *
625 \subsection{Error messages}
626 %*                                                                      *
627 %************************************************************************
628
629 \begin{code}
630 dupSigDeclErr (sig:sigs)
631   = pushSrcLocRn loc $
632     addErrRn (sep [ptext SLIT("Duplicate") <+> ptext what_it_is <> colon,
633                    ppr sig])
634   where
635     (what_it_is, loc) = sig_doc sig
636
637 unknownSigErr sig
638   = pushSrcLocRn loc $
639     addErrRn (sep [ptext SLIT("Misplaced"),
640                    ptext what_it_is <> colon,
641                    ppr sig])
642   where
643     (what_it_is, loc) = sig_doc sig
644
645 sig_doc (Sig        _ _ loc)         = (SLIT("type signature"),loc)
646 sig_doc (ClassOpSig _ _ _ _ loc)     = (SLIT("class-method type signature"), loc)
647 sig_doc (SpecSig    _ _ loc)         = (SLIT("SPECIALISE pragma"),loc)
648 sig_doc (InlineSig  _ _    loc)      = (SLIT("INLINE pragma"),loc)
649 sig_doc (NoInlineSig  _ _  loc)      = (SLIT("NOINLINE pragma"),loc)
650 sig_doc (SpecInstSig _ loc)          = (SLIT("SPECIALISE instance pragma"),loc)
651 sig_doc (FixSig (FixitySig _ _ loc)) = (SLIT("fixity declaration"), loc)
652 sig_doc (DeprecSig _ loc)            = (SLIT("DEPRECATED pragma"), loc)
653
654 missingSigWarn var
655   = sep [ptext SLIT("definition but no type signature for"), quotes (ppr var)]
656
657 methodBindErr mbind
658  =  hang (ptext SLIT("Can't handle multiple methods defined by one pattern binding"))
659        4 (ppr mbind)
660 \end{code}