[project @ 1996-04-20 10:37:06 by partain]
[ghc-hetmet.git] / ghc / compiler / rename / RnNames.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[RnNames]{Extracting imported and top-level names in scope}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module RnNames (
10         getGlobalNames,
11         GlobalNameInfo(..)
12     ) where
13
14 import PreludeGlaST     ( MutableVar(..) )
15
16 import Ubiq
17
18 import HsSyn
19 import RdrHsSyn
20 import RnHsSyn
21
22 import RnMonad
23 import RnIfaces         ( IfaceCache(..), cachedIface, cachedDecl )
24 import RnUtils          ( RnEnv(..), emptyRnEnv, extendGlobalRnEnv,
25                           lubExportFlag, qualNameErr, dupNamesErr )
26 import ParseUtils       ( ParsedIface(..), RdrIfaceDecl(..), RdrIfaceInst )
27
28
29 import Bag              ( emptyBag, unitBag, consBag, unionBags, unionManyBags,
30                           mapBag, listToBag, bagToList )
31 import CmdLineOpts      ( opt_NoImplicitPrelude )
32 import ErrUtils         ( Error(..), Warning(..), addShortErrLocLine )
33 import FiniteMap        ( emptyFM, addListToFM, lookupFM, fmToList, eltsFM, delListFromFM )
34 import Id               ( GenId )
35 import Maybes           ( maybeToBool, catMaybes, MaybeErr(..) )
36 import Name             ( RdrName(..), Name, isQual, mkTopLevName,
37                           mkImportedName, nameExportFlag, nameImportFlag,
38                           getLocalName, getSrcLoc, pprNonSym, moduleNamePair,
39                           isLexCon, isRdrLexCon, ExportFlag(..)
40                         )
41 import PrelInfo         ( BuiltinNames(..), BuiltinKeys(..) )
42 import PrelMods         ( fromPrelude, pRELUDE )
43 import Pretty
44 import SrcLoc           ( SrcLoc, mkIfaceSrcLoc )
45 import TyCon            ( tyConDataCons )
46 import UniqFM           ( emptyUFM, addListToUFM_C, lookupUFM )
47 import UniqSupply       ( splitUniqSupply )
48 import Util             ( isIn, cmpPString, sortLt, removeDups, equivClasses, panic, assertPanic )
49 \end{code}
50
51
52 \begin{code}
53 type GlobalNameInfo = (BuiltinNames,
54                        BuiltinKeys,
55                        Name -> ExportFlag,      -- export flag
56                        Name -> [RdrName])       -- occurence names
57
58 type RnM_Info s r = RnMonad GlobalNameInfo s r
59
60 getGlobalNames ::
61            IfaceCache           
62         -> GlobalNameInfo       
63         -> UniqSupply
64         -> RdrNameHsModule
65         -> IO (RnEnv,
66                [Module],                -- directly imported modules
67                Bag (Module,RnName),     -- unqualified imports from module
68                Bag RenamedFixityDecl,   -- imported fixity decls
69                Bag Error,
70                Bag Warning)
71
72 getGlobalNames iface_cache info us
73                (HsModule mod _ _ imports _ ty_decls _ cls_decls _ _ _ binds _ _)
74   = case initRn True mod emptyRnEnv us1 
75                 (setExtraRn info $
76                  getSourceNames ty_decls cls_decls binds)
77     of { ((src_vals, src_tcs), src_errs, src_warns) ->
78
79     doImportDecls iface_cache info us2 imports  >>=
80         \ (imp_vals, imp_tcs, imp_mods, unqual_imps, imp_fixes, imp_errs, imp_warns) ->
81
82     let
83         unqual_vals = mapBag (\rn -> (Unqual (getLocalName rn), rn)) src_vals
84         unqual_tcs  = mapBag (\rn -> (Unqual (getLocalName rn), rn)) src_tcs
85
86         all_vals = bagToList (unqual_vals `unionBags` imp_vals)
87         all_tcs  = bagToList (unqual_tcs  `unionBags` imp_tcs)
88
89         (all_env, dups) = extendGlobalRnEnv emptyRnEnv all_vals all_tcs
90
91         dup_errs = map dup_err (equivClasses cmp_rdr (bagToList dups))
92         cmp_rdr (rdr1,_,_) (rdr2,_,_) = cmp rdr1 rdr2
93         dup_err ((rdr,rn,rn'):rest) = globalDupNamesErr rdr (rn:rn': [rn|(_,_,rn)<-rest])
94
95         all_errs  = src_errs  `unionBags` imp_errs `unionBags` listToBag dup_errs
96         all_warns = src_warns `unionBags` imp_warns
97     in
98     return (all_env, imp_mods, unqual_imps, imp_fixes, all_errs, all_warns)
99     }
100   where
101     (us1, us2) = splitUniqSupply us
102 \end{code}
103
104 *********************************************************
105 *                                                       *
106 \subsection{Top-level source names}
107 *                                                       *
108 *********************************************************
109
110 \begin{code}
111 getSourceNames ::
112            [RdrNameTyDecl]
113         -> [RdrNameClassDecl]
114         -> RdrNameHsBinds
115         -> RnM_Info s (Bag RnName,      -- values
116                        Bag RnName)      -- tycons/classes
117
118 getSourceNames ty_decls cls_decls binds
119   = mapAndUnzipRn getTyDeclNames ty_decls   `thenRn` \ (tycon_s, constrs_s) ->
120     mapAndUnzipRn getClassNames cls_decls  `thenRn` \ (cls_s, cls_ops_s) ->
121     getTopBindsNames binds                         `thenRn` \ bind_names ->
122     returnRn (unionManyBags constrs_s `unionBags`
123               unionManyBags cls_ops_s `unionBags` bind_names,
124               listToBag tycon_s `unionBags` listToBag cls_s)
125
126
127 getTyDeclNames :: RdrNameTyDecl
128                -> RnM_Info s (RnName, Bag RnName)       -- tycon and constrs
129
130 getTyDeclNames (TyData _ tycon _ condecls _ _ src_loc)
131   = newGlobalName src_loc Nothing tycon `thenRn` \ tycon_name ->
132     mapRn (getConDeclName (Just (nameExportFlag tycon_name)))
133                                condecls `thenRn` \ con_names ->
134     returnRn (RnData tycon_name con_names,
135               listToBag (map (\ n -> RnConstr n tycon_name) con_names))
136
137 getTyDeclNames (TyNew _ tycon _ condecls _ _ src_loc)
138   = newGlobalName src_loc Nothing tycon `thenRn` \ tycon_name ->
139     mapRn (getConDeclName (Just (nameExportFlag tycon_name)))
140                                condecls `thenRn` \ con_names ->
141     returnRn (RnData tycon_name con_names,
142               listToBag (map (\ n -> RnConstr n tycon_name) con_names))
143
144 getTyDeclNames (TySynonym tycon _ _ src_loc)
145   = newGlobalName src_loc Nothing tycon `thenRn` \ tycon_name ->
146     returnRn (RnSyn tycon_name, emptyBag)
147
148 getConDeclName exp (ConDecl con _ src_loc)
149   = newGlobalName src_loc exp con
150 getConDeclName exp (ConOpDecl _ op _ src_loc)
151   = newGlobalName src_loc exp op
152 getConDeclName exp (NewConDecl con _ src_loc)
153   = newGlobalName src_loc exp con
154 getConDeclName exp (RecConDecl con fields src_loc)
155   = panic "getConDeclName:RecConDecl"
156     newGlobalName src_loc exp con
157
158
159 getClassNames :: RdrNameClassDecl
160               -> RnM_Info s (RnName, Bag RnName)        -- class and class ops
161
162 getClassNames (ClassDecl _ cname _ sigs _ _ src_loc)
163   = newGlobalName src_loc Nothing cname `thenRn` \ class_name ->
164     getClassOpNames (Just (nameExportFlag class_name))
165                                   sigs  `thenRn` \ op_names ->
166     returnRn (RnClass class_name op_names,
167               listToBag (map (\ n -> RnClassOp n class_name) op_names))
168
169 getClassOpNames exp []
170   = returnRn []
171 getClassOpNames exp (ClassOpSig op _ _ src_loc : sigs)
172   = newGlobalName src_loc exp op `thenRn` \ op_name ->
173     getClassOpNames exp sigs     `thenRn` \ op_names ->
174     returnRn (op_name : op_names)
175 getClassOpNames exp (_ : sigs)
176   = getClassOpNames exp sigs
177 \end{code}
178
179 *********************************************************
180 *                                                       *
181 \subsection{Bindings}
182 *                                                       *
183 *********************************************************
184
185 \begin{code}
186 getTopBindsNames :: RdrNameHsBinds
187                  -> RnM_Info s (Bag RnName)
188
189 getTopBindsNames binds = doBinds binds
190
191 doBinds EmptyBinds           = returnRn emptyBag
192 doBinds (SingleBind bind)    = doBind bind
193 doBinds (BindWith bind sigs) = doBind bind
194 doBinds (ThenBinds binds1 binds2)
195   = andRn unionBags (doBinds binds1) (doBinds binds2)
196
197 doBind EmptyBind          = returnRn emptyBag
198 doBind (NonRecBind mbind) = doMBinds mbind
199 doBind (RecBind mbind)    = doMBinds mbind
200
201 doMBinds EmptyMonoBinds                         = returnRn emptyBag
202 doMBinds (PatMonoBind pat grhss_and_binds locn) = doPat locn pat
203 doMBinds (FunMonoBind p_name _ _ locn)          = doName locn p_name
204 doMBinds (AndMonoBinds mbinds1 mbinds2)
205   = andRn unionBags (doMBinds mbinds1) (doMBinds mbinds2)
206
207 doPats locn pats
208   = mapRn (doPat locn) pats     `thenRn` \ pats_s ->
209     returnRn (unionManyBags pats_s)
210
211 doPat locn WildPatIn             = returnRn emptyBag
212 doPat locn (LitPatIn _)          = returnRn emptyBag
213 doPat locn (LazyPatIn pat)       = doPat locn pat
214 doPat locn (VarPatIn var)        = doName locn var
215 doPat locn (NegPatIn pat)        = doPat locn pat
216 doPat locn (ParPatIn pat)        = doPat locn pat
217 doPat locn (ListPatIn pats)      = doPats locn pats
218 doPat locn (TuplePatIn pats)     = doPats locn pats
219 doPat locn (ConPatIn name pats)  = doPats locn pats
220 doPat locn (ConOpPatIn p1 op p2)
221   = andRn unionBags (doPat locn p1) (doPat locn p2)
222 doPat locn (AsPatIn as_name pat)
223   = andRn unionBags (doName locn as_name) (doPat locn pat)
224 doPat locn (RecPatIn name fields)
225   = mapRn (doField locn) fields `thenRn` \ fields_s ->
226     returnRn (unionManyBags fields_s)
227
228 doField locn (_, pat, _) = doPat locn pat
229
230 doName locn rdr
231   = newGlobalName locn Nothing rdr `thenRn` \ name ->
232     returnRn (unitBag (RnName name))
233 \end{code}
234
235 *********************************************************
236 *                                                       *
237 \subsection{Creating a new global name}
238 *                                                       *
239 *********************************************************
240
241 \begin{code}
242 newGlobalName :: SrcLoc -> Maybe ExportFlag
243               -> RdrName -> RnM_Info s Name
244
245 newGlobalName locn maybe_exp rdr
246   = getExtraRn                  `thenRn` \ (_,b_keys,exp_fn,occ_fn) ->
247     getModuleRn                 `thenRn` \ mod ->
248     rnGetUnique                 `thenRn` \ u ->
249     let
250         (uniq, unqual)
251           = case rdr of
252               Qual m n -> (u, n)
253               Unqual n -> case (lookupFM b_keys n) of
254                             Nothing      -> (u,   n)
255                             Just (key,_) -> (key, n)
256
257         orig   = if fromPrelude mod
258                  then (Unqual unqual)
259                  else (Qual mod unqual)
260
261         exp = case maybe_exp of
262                Just exp -> exp
263                Nothing  -> exp_fn n
264
265         n = mkTopLevName uniq orig locn exp (occ_fn n)
266     in
267     addErrIfRn (isQual rdr) (qualNameErr "name in definition" (rdr, locn)) `thenRn_`
268     returnRn n    
269 \end{code}
270
271 *********************************************************
272 *                                                       *
273 \subsection{Imported names}
274 *                                                       *
275 *********************************************************
276
277 \begin{code}
278 type ImportNameInfo = (GlobalNameInfo,
279                        FiniteMap (Module,FAST_STRING) RnName,   -- values imported so far
280                        FiniteMap (Module,FAST_STRING) RnName,   -- tycons/classes imported so far
281                        Name -> ExportFlag)                      -- import flag
282                 
283 type RnM_IInfo s r = RnMonad ImportNameInfo s r
284
285 doImportDecls ::
286            IfaceCache
287         -> GlobalNameInfo                       -- builtin and knot name info
288         -> UniqSupply
289         -> [RdrNameImportDecl]                  -- import declarations
290         -> IO (Bag (RdrName,RnName),            -- imported values in scope
291                Bag (RdrName,RnName),            -- imported tycons/classes in scope
292                [Module],                        -- directly imported modules
293                Bag (Module,RnName),             -- unqualified import from module
294                Bag RenamedFixityDecl,           -- fixity info for imported names
295                Bag Error,
296                Bag Warning)
297
298 doImportDecls iface_cache g_info us src_imps
299   = fixIO ( \ ~(_, _, _, _, _, _, rec_imp_flags) ->
300         let
301             rec_imp_fm = addListToUFM_C lubExportFlag emptyUFM (bagToList rec_imp_flags)
302
303             rec_imp_fn :: Name -> ExportFlag
304             rec_imp_fn n = case lookupUFM rec_imp_fm n of
305                              Nothing   -> panic "RnNames:rec_imp_fn"
306                              Just flag -> flag
307
308             i_info = (g_info, emptyFM, emptyFM, rec_imp_fn)
309         in
310         doImports iface_cache i_info us (qprel_imp ++ prel_imp ++ src_imps)
311     ) >>= \ (vals, tcs, unquals, fixes, errs, warns, _) ->
312     let
313         imp_mods      = [ mod | ImportDecl mod _ _ _ _ <- src_imps ]
314         imp_warns     = listToBag (map dupImportWarn imp_dups)
315         prel_warns    = listToBag (map qualPreludeImportWarn qual_prels)
316
317         (_, imp_dups) = removeDups cmp_mod src_imps
318         cmp_mod (ImportDecl m1 _ _ _ _) (ImportDecl m2 _ _ _ _) = cmpPString m1 m2
319         qual_prels = [imp | imp@(ImportDecl mod qual _ _ _) <- src_imps,
320                             fromPrelude mod && qual]
321     in
322     return (vals, tcs, imp_mods, unquals, fixes, errs,
323             prel_warns `unionBags` imp_warns `unionBags` warns)
324   where
325     explicit_prelude_import
326       = null [() | (ImportDecl mod qual _ _ _) <- src_imps,
327                    fromPrelude mod && not qual]
328
329     qprel_imp = if opt_NoImplicitPrelude
330                 then [{-the flag really means it: *NO* implicit "import Prelude" -}]
331                 else [ImportDecl pRELUDE True Nothing Nothing mkIfaceSrcLoc]
332
333     prel_imp  = if not explicit_prelude_import || opt_NoImplicitPrelude
334                 then
335                    [ {-prelude imported explicitly => no import Prelude-} ]
336                 else
337                    [ImportDecl pRELUDE False Nothing Nothing mkIfaceSrcLoc]
338
339 doImports iface_cache i_info us []
340   = return (emptyBag, emptyBag, emptyBag, emptyBag, emptyBag, emptyBag, emptyBag)
341 doImports iface_cache i_info@(g_info,done_vals,done_tcs,imp_fn) us (imp:imps)
342   = doImport iface_cache i_info us1 imp
343         >>= \ (vals1, tcs1, unquals1, fixes1, errs1, warns1, imps1) ->
344     let
345         new_vals = [ (moduleNamePair rn, rn) | (_,rn) <- bagToList vals1,
346                         not (maybeToBool (lookupFM done_vals (moduleNamePair rn))) ]
347                         -- moduleNamePair computed twice
348         ext_vals = addListToFM done_vals new_vals
349
350         new_tcs  = [ (moduleNamePair rn, rn) | (_,rn) <- bagToList tcs1,
351                         not (maybeToBool (lookupFM done_tcs (moduleNamePair rn))) ]
352         ext_tcs  = addListToFM done_tcs new_tcs
353     in
354     doImports iface_cache (g_info,ext_vals,ext_tcs,imp_fn) us2 imps
355         >>= \ (vals2, tcs2, unquals2, fixes2, errs2, warns2, imps2) ->
356     return (vals1    `unionBags` vals2,
357             tcs1     `unionBags` tcs2,
358             unquals1 `unionBags` unquals2,
359             fixes1   `unionBags` fixes2,
360             errs1    `unionBags` errs2,
361             warns1   `unionBags` warns2,
362             imps1    `unionBags` imps2)
363   where
364     (us1, us2) = splitUniqSupply us
365
366
367 doImport :: IfaceCache
368          -> ImportNameInfo
369          -> UniqSupply
370          -> RdrNameImportDecl
371          -> IO (Bag (RdrName,RnName),           -- values
372                 Bag (RdrName,RnName),           -- tycons/classes
373                 Bag (Module,RnName),            -- unqual imports
374                 Bag RenamedFixityDecl,
375                 Bag Error,
376                 Bag Warning,
377                 Bag (RnName,ExportFlag))        -- import flags
378
379 doImport iface_cache info us (ImportDecl mod qual maybe_as maybe_spec src_loc)
380   = cachedIface iface_cache mod         >>= \ maybe_iface ->
381     case maybe_iface of
382       Failed err ->
383         return (emptyBag, emptyBag, emptyBag, emptyBag,
384                 unitBag err, emptyBag, emptyBag)
385       Succeeded iface -> 
386         let
387             (b_vals, b_tcs, maybe_spec') = getBuiltins info mod maybe_spec 
388             (ies, chk_ies, get_errs)     = getOrigIEs iface maybe_spec'
389         in
390         doOrigIEs iface_cache info mod src_loc us ies 
391                 >>= \ (ie_vals, ie_tcs, imp_flags, errs, warns) ->
392         accumulate (map (checkOrigIE iface_cache) chk_ies)
393                 >>= \ chk_errs_warns ->
394         accumulate (map (getFixityDecl iface_cache) (bagToList ie_vals))
395                 >>= \ fix_maybes_errs ->
396         let
397             (chk_errs, chk_warns)  = unzip chk_errs_warns
398             (fix_maybes, fix_errs) = unzip fix_maybes_errs
399
400             final_vals = mapBag fst_occ b_vals `unionBags` mapBag pair_occ ie_vals
401             final_tcs  = mapBag fst_occ b_tcs  `unionBags` mapBag pair_occ ie_tcs
402
403             unquals    = if qual then emptyBag
404                          else mapBag pair_as (ie_vals `unionBags` ie_tcs)
405
406             final_fixes = listToBag (catMaybes fix_maybes)
407
408             final_errs  = mapBag (\ err -> err mod src_loc) (unionManyBags (get_errs:chk_errs))
409                           `unionBags` errs `unionBags` unionManyBags fix_errs
410             final_warns = mapBag (\ warn -> warn mod src_loc) (unionManyBags chk_warns)
411                           `unionBags` warns
412         in
413         return (final_vals, final_tcs, unquals, final_fixes,
414                 final_errs, final_warns, imp_flags)
415   where
416     as_mod = case maybe_as of {Nothing -> mod; Just as_this -> as_this}
417     mk_occ str = if qual then Qual as_mod str else Unqual str
418
419     fst_occ (str, rn) = (mk_occ str, rn)
420     pair_occ rn       = (mk_occ (getLocalName rn), rn)
421     pair_as  rn       = (as_mod, rn)
422
423
424 getBuiltins info mod maybe_spec
425   | not (fromPrelude mod)
426   = (emptyBag, emptyBag, maybe_spec)
427
428 getBuiltins (((b_val_names,b_tc_names),_,_,_),_,_,_) mod maybe_spec
429   = case maybe_spec of 
430       Nothing           -> (all_vals, all_tcs, Nothing)
431
432       Just (True, ies)  -> -- hiding does not work for builtin names
433                            (all_vals, all_tcs, maybe_spec)
434
435       Just (False, ies) -> let 
436                               (vals,tcs,ies_left) = do_builtin ies
437                            in
438                            (vals, tcs, Just (False, ies_left))
439   where
440     all_vals = do_all_builtin (fmToList b_val_names)
441     all_tcs  = do_all_builtin (fmToList b_tc_names)
442
443     do_all_builtin [] = emptyBag
444     do_all_builtin ((str,rn):rest)
445       = (str, rn) `consBag` do_all_builtin rest
446
447     do_builtin [] = (emptyBag,emptyBag,[]) 
448     do_builtin (ie:ies)
449       = let str = unqual_str (ie_name ie)
450         in
451         case (lookupFM b_tc_names str) of -- NB: we favour the tycon/class FM...
452           Just rn -> case (ie,rn) of
453              (IEThingAbs _, WiredInTyCon tc)
454                 -> (vals, (str, rn) `consBag` tcs, ies_left)
455              (IEThingAll _, WiredInTyCon tc)
456                 -> (listToBag (map (\ id -> (getLocalName id, WiredInId id)) 
457                                    (tyConDataCons tc))
458                     `unionBags` vals,
459                     (str,rn) `consBag` tcs, ies_left)
460              _ -> panic "importing builtin names (1)"
461
462           Nothing ->
463             case (lookupFM b_val_names str) of
464               Nothing -> (vals, tcs, ie:ies_left)
465               Just rn -> case (ie,rn) of
466                  (IEVar _, WiredInId _)        
467                     -> ((str, rn) `consBag` vals, tcs, ies_left)
468                  _ -> panic "importing builtin names (2)"
469       where
470         (vals, tcs, ies_left) = do_builtin ies
471
472
473 getOrigIEs (ParsedIface _ _ _ _ exps _ _ _ _ _ _) Nothing               -- import all
474   = (map mkAllIE (eltsFM exps), [], emptyBag)
475
476 getOrigIEs (ParsedIface _ _ _ _ exps _ _ _ _ _ _) (Just (True, ies))    -- import hiding
477   = (map mkAllIE (eltsFM exps_left), found_ies, errs)
478   where
479     (found_ies, errs) = lookupIEs exps ies
480     exps_left = delListFromFM exps (map (getLocalName.ie_name.fst) found_ies)
481
482 getOrigNames (ParsedIface _ _ _ _ exps _ _ _ _ _ _) (Just (False, ies))
483   = (map fst found_ies, found_ies, errs)
484   where
485     (found_ies, errs) = lookupIEs exps ies
486
487
488 mkAllIE (orig,ExportAbs)
489   = ASSERT(isLexCon (getLocalName orig))
490     IEThingAbs orig
491 mkAllIE (orig, ExportAll)
492   | isLexCon (getLocalName orig)
493   = IEThingAll orig
494   | otherwise
495   = IEVar orig
496
497
498 lookupIEs exps [] 
499   = ([], emptyBag)
500 lookupIEs exps (ie:ies)
501   = case lookupFM exps (unqual_str (ie_name ie)) of 
502       Nothing ->
503         (orig_ies, unknownImpSpecErr ie `consBag` errs)
504       Just (orig,flag) ->
505         (orig_ie orig flag ie ++ orig_ies,
506          adderr_if (seen_ie orig orig_ies) (duplicateImpSpecErr ie) errs)
507   where
508     (orig_ies, errs) = lookupIEs exps ies
509
510     orig_ie orig flag (IEVar n)          = [(IEVar orig, flag)]
511     orig_ie orig flag (IEThingAbs n)     = [(IEThingAbs orig, flag)]
512     orig_ie orig flag (IEThingAll n)     = [(IEThingAll orig, flag)]
513     orig_ie orig flag (IEThingWith n ns) = [(IEThingWith orig ns, flag)]
514
515     seen_ie orig seen_ies = any (\ (ie,_) -> orig == ie_name ie) seen_ies
516
517
518 doOrigIEs iface_cache info mod src_loc us []
519   = return (emptyBag,emptyBag,emptyBag,emptyBag,emptyBag)
520
521 doOrigIEs iface_cache info mod src_loc us (ie:ies)
522   = doOrigIE iface_cache info mod src_loc us1 ie 
523         >>= \ (vals1, tcs1, errs1, warns1, imps1) ->
524     doOrigIEs iface_cache info mod src_loc us2 ies
525         >>= \ (vals2, tcs2, errs2, warns2, imps2) ->
526     return (vals1    `unionBags` vals2,
527             tcs1     `unionBags` tcs2,
528             errs1    `unionBags` errs2,
529             warns1   `unionBags` warns2,
530             imps1    `unionBags` imps2)
531   where
532     (us1, us2) = splitUniqSupply us
533
534 doOrigIE iface_cache info mod src_loc us ie
535   = with_decl iface_cache (ie_name ie)
536         (\ err  -> (emptyBag, emptyBag, emptyBag, unitBag err, emptyBag))
537         (\ decl -> case initRn True mod emptyRnEnv us
538                                (setExtraRn info $
539                                 pushSrcLocRn src_loc $
540                                 getIfaceDeclNames ie decl)
541                    of
542                    ((vals, tcs, imps), errs, warns) -> (vals, tcs, imps, errs, warns))
543
544 checkOrigIE iface_cache (IEThingAll n, ExportAbs)
545   = with_decl iface_cache n
546         (\ err  -> (unitBag (\ mod locn -> err), emptyBag))
547         (\ decl -> case decl of
548                 TypeSig _ _ _ -> (emptyBag, unitBag (allWhenSynImpSpecWarn n))
549                 other         -> (unitBag (allWhenAbsImpSpecErr n), emptyBag))
550
551 checkOrigIE iface_cache (IEThingWith n ns, ExportAbs)
552   = return (unitBag (withWhenAbsImpSpecErr n), emptyBag)
553
554 checkOrigIE iface_cache (IEThingWith n ns, ExportAll)
555   = with_decl iface_cache n
556         (\ err  -> (unitBag (\ mod locn -> err), emptyBag))
557         (\ decl -> case decl of
558                 NewTypeSig _ con _ _  -> (check_with "constructrs" [con] ns, emptyBag)
559                 DataSig    _ cons _ _ -> (check_with "constructrs" cons  ns, emptyBag)
560                 ClassSig   _ ops _ _  -> (check_with "class ops"   ops   ns, emptyBag))
561   where
562     check_with str has rdrs
563       | sortLt (<) (map getLocalName has) == sortLt (<) (map unqual_str rdrs)
564       = emptyBag
565       | otherwise
566       = unitBag (withImpSpecErr str n has rdrs)
567
568 checkOrigIE iface_cache other
569   = return (emptyBag, emptyBag)
570
571
572 with_decl iface_cache n do_err do_decl
573   = cachedDecl iface_cache (isRdrLexCon n) n   >>= \ maybe_decl ->
574     case maybe_decl of
575       Failed err     -> return (do_err err)
576       Succeeded decl -> return (do_decl decl)
577
578
579 getFixityDecl iface_cache rn
580   = let
581         (mod, str) = moduleNamePair rn
582     in
583     cachedIface iface_cache mod >>= \ maybe_iface ->
584     case maybe_iface of
585       Failed err ->
586         return (Nothing, unitBag err)
587       Succeeded (ParsedIface _ _ _ _ _ _ fixes _ _ _ _) ->
588         case lookupFM fixes str of
589           Nothing           -> return (Nothing, emptyBag)
590           Just (InfixL _ i) -> return (Just (InfixL rn i), emptyBag)
591           Just (InfixR _ i) -> return (Just (InfixR rn i), emptyBag)
592           Just (InfixN _ i) -> return (Just (InfixN rn i), emptyBag)
593
594 ie_name (IEVar n)         = n
595 ie_name (IEThingAbs n)    = n
596 ie_name (IEThingAll n)    = n
597 ie_name (IEThingWith n _) = n
598
599 unqual_str (Unqual str) = str
600 unqual_str q@(Qual _ _) = panic "unqual_str"
601
602 adderr_if True err errs  = err `consBag` errs
603 adderr_if False err errs = errs
604 \end{code}
605
606 *********************************************************
607 *                                                       *
608 \subsection{Actually creating the imported names}
609 *                                                       *
610 *********************************************************
611
612 \begin{code}
613 getIfaceDeclNames :: RdrNameIE -> RdrIfaceDecl
614                   -> RnM_IInfo s (Bag RnName,                   -- values
615                                   Bag RnName,                   -- tycons/classes
616                                   Bag (RnName,ExportFlag))      -- import flags
617
618 getIfaceDeclNames ie (ValSig val src_loc _)
619   = newImportedName False src_loc Nothing Nothing val `thenRn` \ val_name ->
620     returnRn (unitBag (RnName val_name),
621               emptyBag,
622               unitBag (RnName val_name, ExportAll))
623
624 getIfaceDeclNames ie (TypeSig tycon src_loc _)
625   = newImportedName True src_loc Nothing Nothing tycon `thenRn` \ tycon_name ->
626     returnRn (emptyBag,
627               unitBag (RnSyn tycon_name),
628               unitBag (RnSyn tycon_name, ExportAll))
629
630 getIfaceDeclNames ie (NewTypeSig tycon con src_loc _)
631   = newImportedName True src_loc Nothing Nothing tycon `thenRn` \ tycon_name ->
632     mapRn (newImportedName False src_loc (Just (nameExportFlag tycon_name))
633                                          (Just (nameImportFlag tycon_name)))
634                                             [con] `thenRn` \ con_names ->
635     returnRn (if imp_all (imp_flag ie) then
636                   listToBag (map (\ n -> RnConstr n tycon_name) con_names)
637               else
638                   emptyBag,
639               unitBag (RnData tycon_name con_names),
640               unitBag (RnData tycon_name con_names, imp_flag ie))
641
642 getIfaceDeclNames ie (DataSig tycon cons src_loc _)
643   = newImportedName True src_loc Nothing Nothing tycon `thenRn` \ tycon_name ->
644     mapRn (newImportedName False src_loc (Just (nameExportFlag tycon_name))
645                                          (Just (nameImportFlag tycon_name)))
646                                              cons `thenRn` \ con_names ->
647     returnRn (if imp_all (imp_flag ie) then
648                   listToBag (map (\ n -> RnConstr n tycon_name) con_names)
649               else
650                   emptyBag,
651               unitBag (RnData tycon_name con_names),
652               unitBag (RnData tycon_name con_names, imp_flag ie))
653
654 getIfaceDeclNames ie (ClassSig cls ops src_loc _)
655   = newImportedName True src_loc Nothing Nothing cls `thenRn` \ cls_name ->
656     mapRn (newImportedName False src_loc (Just (nameExportFlag cls_name))
657                                          (Just (nameImportFlag cls_name)))
658                                             ops `thenRn` \ op_names ->
659     returnRn (if imp_all (imp_flag ie) then
660                   listToBag (map (\ n -> RnClassOp n cls_name) op_names)
661               else
662                   emptyBag,
663               unitBag (RnClass cls_name op_names),
664               unitBag (RnClass cls_name op_names, imp_flag ie))
665
666
667 imp_all ExportAll = True
668 imp_all _         = False
669
670 imp_flag (IEThingAbs _)    = ExportAbs
671 imp_flag (IEThingAll _)    = ExportAll
672 imp_flag (IEThingWith _ _) = ExportAll
673 \end{code}
674
675 *********************************************************
676 *                                                       *
677 \subsection{Creating a new imported name}
678 *                                                       *
679 *********************************************************
680
681 \begin{code}
682 newImportedName :: Bool                 -- True => tycon or class
683                 -> SrcLoc
684                 -> Maybe ExportFlag     -- maybe export flag
685                 -> Maybe ExportFlag     -- maybe import flag
686                 -> RdrName              -- orig name
687                 -> RnM_IInfo s Name
688
689 newImportedName tycon_or_class locn maybe_exp maybe_imp rdr
690   = getExtraRn `thenRn` \ ((_,b_keys,exp_fn,occ_fn),done_vals,done_tcs,imp_fn) ->
691     case if tycon_or_class
692          then lookupFM done_tcs  (moduleNamePair rdr)
693          else lookupFM done_vals (moduleNamePair rdr)
694     of
695     Just rn -> returnRn (getName rn)
696     Nothing -> 
697         rnGetUnique     `thenRn` \ u ->
698         let 
699             uniq = case rdr of
700                      Qual m n -> u
701                      Unqual n -> case lookupFM b_keys n of
702                                    Nothing      -> u
703                                    Just (key,_) -> key
704
705             exp  = case maybe_exp of
706                      Just exp -> exp
707                      Nothing  -> exp_fn n
708
709             imp  = case maybe_imp of
710                      Just imp -> imp
711                      Nothing  -> imp_fn n
712
713             n = mkImportedName uniq rdr imp locn exp (occ_fn n)
714         in
715         returnRn n
716 \end{code}
717
718 \begin{code}
719 globalDupNamesErr rdr rns sty
720   = ppHang (ppBesides [pprNonSym sty rdr, ppStr " multiply defined:"])
721          4 (ppAboves (map pp_def rns))
722   where
723     pp_def rn = addShortErrLocLine (getSrcLoc rn) (\ sty -> ppr sty rn) sty
724
725 dupImportWarn dup_imps sty
726   = ppStr "dupImportWarn"
727
728 qualPreludeImportWarn imp sty
729   = ppStr "qualPreludeImportWarn"
730
731 unknownImpSpecErr ie imp_mod locn sty
732   = ppStr "unknownImpSpecErr"
733
734 duplicateImpSpecErr ie imp_mod locn sty
735   = ppStr "duplicateImpSpecErr"
736
737 allWhenSynImpSpecWarn n imp_mod locn sty 
738   = ppStr "allWhenSynImpSpecWarn"
739
740 allWhenAbsImpSpecErr n imp_mod locn sty 
741   = ppStr "allWhenAbsImpSpecErr"
742
743 withWhenAbsImpSpecErr n imp_mod locn sty 
744   = ppStr "withWhenAbsImpSpecErr"
745
746 withImpSpecErr str n has ns imp_mod locn sty 
747   = ppStr "withImpSpecErr"
748 \end{code}