2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[RnNames]{Extracting imported and top-level names in scope}
7 #include "HsVersions.h"
14 import PreludeGlaST ( MutableVar(..) )
23 import RnIfaces ( IfaceCache(..), cachedIface, cachedDecl )
24 import RnUtils ( RnEnv(..), emptyRnEnv, extendGlobalRnEnv,
25 lubExportFlag, qualNameErr, dupNamesErr, negateNameWarn
27 import ParseUtils ( ParsedIface(..), RdrIfaceDecl(..), RdrIfaceInst )
30 import Bag ( emptyBag, unitBag, consBag, snocBag, unionBags,
31 unionManyBags, mapBag, filterBag, listToBag, bagToList )
32 import CmdLineOpts ( opt_NoImplicitPrelude )
33 import ErrUtils ( Error(..), Warning(..), addErrLoc, addShortErrLocLine, addShortWarnLocLine )
34 import FiniteMap ( emptyFM, addListToFM, lookupFM, fmToList, eltsFM, delListFromFM )
36 import Maybes ( maybeToBool, catMaybes, MaybeErr(..) )
37 import Name ( RdrName(..), Name, isQual, mkTopLevName, origName,
38 mkImportedName, nameExportFlag, nameImportFlag,
39 getLocalName, getSrcLoc, getImpLocs, moduleNamePair,
40 pprNonSym, isLexCon, isRdrLexCon, ExportFlag(..)
42 import PrelInfo ( BuiltinNames(..), BuiltinKeys(..) )
43 import PrelMods ( fromPrelude, pRELUDE )
45 import SrcLoc ( SrcLoc, mkBuiltinSrcLoc )
46 import TyCon ( tyConDataCons )
47 import UniqFM ( emptyUFM, addListToUFM_C, lookupUFM )
48 import UniqSupply ( splitUniqSupply )
49 import Util ( isIn, assoc, cmpPString, sortLt, removeDups,
50 equivClasses, panic, assertPanic )
55 type GlobalNameInfo = (BuiltinNames,
57 Name -> ExportFlag, -- export flag
58 Name -> [RdrName]) -- occurence names
60 type RnM_Info s r = RnMonad GlobalNameInfo s r
68 [Module], -- directly imported modules
69 Bag (Module,RnName), -- unqualified imports from module
70 Bag RenamedFixityDecl, -- imported fixity decls
74 getGlobalNames iface_cache info us
75 (HsModule mod _ _ imports _ ty_decls _ cls_decls _ _ _ binds _ _)
76 = case initRn True mod emptyRnEnv us1
78 getSourceNames ty_decls cls_decls binds)
79 of { ((src_vals, src_tcs), src_errs, src_warns) ->
81 doImportDecls iface_cache info us2 imports >>=
82 \ (imp_vals, imp_tcs, imp_mods, unqual_imps, imp_fixes, imp_errs, imp_warns) ->
85 unqual_vals = map (\rn -> (Unqual (getLocalName rn), rn)) (bagToList src_vals)
86 unqual_tcs = map (\rn -> (Unqual (getLocalName rn), rn)) (bagToList src_tcs)
88 (src_env, src_dups) = extendGlobalRnEnv emptyRnEnv unqual_vals unqual_tcs
89 (all_env, imp_dups) = extendGlobalRnEnv src_env (bagToList imp_vals) (bagToList imp_tcs)
91 -- remove dups of the same imported thing
92 diff_imp_dups = filterBag diff_orig imp_dups
93 diff_orig (_,rn1,rn2) = origName rn1 /= origName rn2
95 all_dups = bagToList (src_dups `unionBags` diff_imp_dups)
96 dup_errs = map dup_err (equivClasses cmp_rdr all_dups)
97 cmp_rdr (rdr1,_,_) (rdr2,_,_) = cmp rdr1 rdr2
98 dup_err ((rdr,rn1,rn2):rest) = globalDupNamesErr rdr (rn1:rn2: [rn|(_,_,rn)<-rest])
100 all_errs = src_errs `unionBags` imp_errs `unionBags` listToBag dup_errs
101 all_warns = src_warns `unionBags` imp_warns
103 return (all_env, imp_mods, unqual_imps, imp_fixes, all_errs, all_warns)
106 (us1, us2) = splitUniqSupply us
109 *********************************************************
111 \subsection{Top-level source names}
113 *********************************************************
118 -> [RdrNameClassDecl]
120 -> RnM_Info s (Bag RnName, -- values
121 Bag RnName) -- tycons/classes
123 getSourceNames ty_decls cls_decls binds
124 = mapAndUnzip3Rn getTyDeclNames ty_decls `thenRn` \ (tycon_s, constrs_s, fields_s) ->
125 mapAndUnzipRn getClassNames cls_decls `thenRn` \ (cls_s, cls_ops_s) ->
126 getTopBindsNames binds `thenRn` \ bind_names ->
127 returnRn (unionManyBags constrs_s `unionBags`
128 unionManyBags fields_s `unionBags`
129 unionManyBags cls_ops_s `unionBags` bind_names,
130 listToBag tycon_s `unionBags` listToBag cls_s)
133 getTyDeclNames :: RdrNameTyDecl
134 -> RnM_Info s (RnName, Bag RnName, Bag RnName) -- tycon, constrs and fields
136 getTyDeclNames (TyData _ tycon _ condecls _ _ src_loc)
137 = newGlobalName src_loc Nothing tycon `thenRn` \ tycon_name ->
138 getConFieldNames (Just (nameExportFlag tycon_name)) emptyBag emptyBag emptyFM
139 condecls `thenRn` \ (con_names, field_names) ->
141 rn_tycon = RnData tycon_name con_names field_names
142 rn_constrs = [ RnConstr name tycon_name | name <- con_names]
143 rn_fields = [ RnField name tycon_name | name <- field_names]
145 returnRn (rn_tycon, listToBag rn_constrs, listToBag rn_fields)
147 getTyDeclNames (TyNew _ tycon _ [NewConDecl con _ con_loc] _ _ src_loc)
148 = newGlobalName src_loc Nothing tycon `thenRn` \ tycon_name ->
149 newGlobalName con_loc (Just (nameExportFlag tycon_name)) con
150 `thenRn` \ con_name ->
151 returnRn (RnData tycon_name [con_name] [],
152 unitBag (RnConstr con_name tycon_name),
155 getTyDeclNames (TySynonym tycon _ _ src_loc)
156 = newGlobalName src_loc Nothing tycon `thenRn` \ tycon_name ->
157 returnRn (RnSyn tycon_name, emptyBag, emptyBag)
160 getConFieldNames exp constrs fields have []
161 = returnRn (bagToList constrs, bagToList fields)
163 getConFieldNames exp constrs fields have (ConDecl con _ src_loc : rest)
164 = newGlobalName src_loc exp con `thenRn` \ con_name ->
165 getConFieldNames exp (constrs `snocBag` con_name) fields have rest
167 getConFieldNames exp constrs fields have (ConOpDecl _ con _ src_loc : rest)
168 = newGlobalName src_loc exp con `thenRn` \ con_name ->
169 getConFieldNames exp (constrs `snocBag` con_name) fields have rest
171 getConFieldNames exp constrs fields have (RecConDecl con fielddecls src_loc : rest)
172 = mapRn (addErrRn . dupFieldErr con src_loc) dups `thenRn_`
173 newGlobalName src_loc exp con `thenRn` \ con_name ->
174 mapRn (newGlobalName src_loc exp) new_fields `thenRn` \ field_names ->
176 all_constrs = constrs `snocBag` con_name
177 all_fields = fields `unionBags` listToBag field_names
179 getConFieldNames exp all_constrs all_fields new_have rest
181 (uniq_fields, dups) = removeDups cmp (concat (map fst fielddecls))
182 new_fields = filter (not . maybeToBool . lookupFM have) uniq_fields
183 new_have = addListToFM have (zip new_fields (repeat ()))
185 getClassNames :: RdrNameClassDecl
186 -> RnM_Info s (RnName, Bag RnName) -- class and class ops
188 getClassNames (ClassDecl _ cname _ sigs _ _ src_loc)
189 = newGlobalName src_loc Nothing cname `thenRn` \ class_name ->
190 getClassOpNames (Just (nameExportFlag class_name))
191 sigs `thenRn` \ op_names ->
192 returnRn (RnClass class_name op_names,
193 listToBag (map (\ n -> RnClassOp n class_name) op_names))
195 getClassOpNames exp []
197 getClassOpNames exp (ClassOpSig op _ _ src_loc : sigs)
198 = newGlobalName src_loc exp op `thenRn` \ op_name ->
199 getClassOpNames exp sigs `thenRn` \ op_names ->
200 returnRn (op_name : op_names)
201 getClassOpNames exp (_ : sigs)
202 = getClassOpNames exp sigs
205 *********************************************************
207 \subsection{Bindings}
209 *********************************************************
212 getTopBindsNames :: RdrNameHsBinds
213 -> RnM_Info s (Bag RnName)
215 getTopBindsNames binds = doBinds binds
217 doBinds EmptyBinds = returnRn emptyBag
218 doBinds (SingleBind bind) = doBind bind
219 doBinds (BindWith bind sigs) = doBind bind
220 doBinds (ThenBinds binds1 binds2)
221 = andRn unionBags (doBinds binds1) (doBinds binds2)
223 doBind EmptyBind = returnRn emptyBag
224 doBind (NonRecBind mbind) = doMBinds mbind
225 doBind (RecBind mbind) = doMBinds mbind
227 doMBinds EmptyMonoBinds = returnRn emptyBag
228 doMBinds (PatMonoBind pat grhss_and_binds locn) = doPat locn pat
229 doMBinds (FunMonoBind p_name _ _ locn) = doName locn p_name
230 doMBinds (AndMonoBinds mbinds1 mbinds2)
231 = andRn unionBags (doMBinds mbinds1) (doMBinds mbinds2)
234 = mapRn (doPat locn) pats `thenRn` \ pats_s ->
235 returnRn (unionManyBags pats_s)
237 doPat locn WildPatIn = returnRn emptyBag
238 doPat locn (LitPatIn _) = returnRn emptyBag
239 doPat locn (LazyPatIn pat) = doPat locn pat
240 doPat locn (VarPatIn var) = doName locn var
241 doPat locn (NegPatIn pat) = doPat locn pat
242 doPat locn (ParPatIn pat) = doPat locn pat
243 doPat locn (ListPatIn pats) = doPats locn pats
244 doPat locn (TuplePatIn pats) = doPats locn pats
245 doPat locn (ConPatIn name pats) = doPats locn pats
246 doPat locn (ConOpPatIn p1 op p2)
247 = andRn unionBags (doPat locn p1) (doPat locn p2)
248 doPat locn (AsPatIn as_name pat)
249 = andRn unionBags (doName locn as_name) (doPat locn pat)
250 doPat locn (RecPatIn name fields)
251 = mapRn (doField locn) fields `thenRn` \ fields_s ->
252 returnRn (unionManyBags fields_s)
254 doField locn (_, pat, _) = doPat locn pat
257 = newGlobalName locn Nothing rdr `thenRn` \ name ->
258 returnRn (unitBag (RnName name))
261 *********************************************************
263 \subsection{Creating a new global name}
265 *********************************************************
268 newGlobalName :: SrcLoc -> Maybe ExportFlag
269 -> RdrName -> RnM_Info s Name
271 -- ToDo: b_names and b_keys being defined in this module !!!
273 newGlobalName locn maybe_exp rdr
274 = getExtraRn `thenRn` \ (_,b_keys,exp_fn,occ_fn) ->
275 getModuleRn `thenRn` \ mod ->
276 rnGetUnique `thenRn` \ u ->
281 Unqual n -> case (lookupFM b_keys n) of
283 Just (key,_) -> (key, n)
285 orig = if fromPrelude mod
287 else (Qual mod unqual)
289 exp = case maybe_exp of
293 n = mkTopLevName uniq orig locn exp (occ_fn n) -- NB: two "n"s
295 addWarnIfRn (rdr == Unqual SLIT("negate")) (negateNameWarn (rdr, locn)) `thenRn_`
296 addErrIfRn (isQual rdr) (qualNameErr "name in definition" (rdr, locn)) `thenRn_`
300 *********************************************************
302 \subsection{Imported names}
304 *********************************************************
307 type ImportNameInfo = (GlobalNameInfo,
308 FiniteMap (Module,FAST_STRING) RnName, -- values imported so far
309 FiniteMap (Module,FAST_STRING) RnName, -- tycons/classes imported so far
310 Name -> (ExportFlag, [SrcLoc])) -- import flag and src locns
312 type RnM_IInfo s r = RnMonad ImportNameInfo s r
316 -> GlobalNameInfo -- builtin and knot name info
318 -> [RdrNameImportDecl] -- import declarations
319 -> IO (Bag (RdrName,RnName), -- imported values in scope
320 Bag (RdrName,RnName), -- imported tycons/classes in scope
321 [Module], -- directly imported modules
322 Bag (Module,RnName), -- unqualified import from module
323 Bag RenamedFixityDecl, -- fixity info for imported names
327 doImportDecls iface_cache g_info us src_imps
328 = fixIO ( \ ~(_, _, _, _, _, _, rec_imp_stuff) ->
330 rec_imp_fm = addListToUFM_C add_stuff emptyUFM (bagToList rec_imp_stuff)
331 add_stuff (imp1,locns1) (imp2,locns2) = (lubExportFlag imp1 imp2, locns1 `unionBags` locns2)
333 rec_imp_fn :: Name -> (ExportFlag, [SrcLoc])
334 rec_imp_fn n = case lookupUFM rec_imp_fm n of
335 Nothing -> panic "RnNames:rec_imp_fn"
336 Just (flag, locns) -> (flag, bagToList locns)
338 i_info = (g_info, emptyFM, emptyFM, rec_imp_fn)
340 -- cache the imported modules
341 -- this ensures that all directly imported modules
342 -- will have their original name iface in scope
343 accumulate (map (cachedIface False iface_cache) imp_mods) >>
345 -- process the imports
346 doImports iface_cache i_info us all_imps
348 ) >>= \ (vals, tcs, unquals, fixes, errs, warns, _) ->
350 return (vals, tcs, imp_mods, unquals, fixes,
351 errs, imp_warns `unionBags` warns)
353 the_imps = implicit_prel ++ src_imps
354 all_imps = implicit_qprel ++ the_imps
356 implicit_qprel = if opt_NoImplicitPrelude
357 then [{- no "import qualified Prelude" -}]
358 else [ImportDecl pRELUDE True Nothing Nothing prel_loc]
360 explicit_prelude_imp = not (null [ () | (ImportDecl mod qual _ _ _) <- src_imps,
363 implicit_prel = if explicit_prelude_imp || opt_NoImplicitPrelude
364 then [{- no "import Prelude" -}]
365 else [ImportDecl pRELUDE False Nothing Nothing prel_loc]
367 prel_imps -- WDP: Just guessing on this defn... ToDo
368 = [ imp | imp@(ImportDecl mod _ _ _ _) <- the_imps, fromPrelude mod ]
370 prel_loc = mkBuiltinSrcLoc
372 (uniq_imps, imp_dups) = removeDups cmp_mod the_imps
373 cmp_mod (ImportDecl m1 _ _ _ _) (ImportDecl m2 _ _ _ _) = cmpPString m1 m2
375 qprel_imps = [ imp | imp@(ImportDecl mod True Nothing _ _) <- prel_imps ]
377 imp_mods = [ mod | ImportDecl mod _ _ _ _ <- uniq_imps ]
378 imp_warns = listToBag (map dupImportWarn imp_dups)
380 listToBag (map qualPreludeImportWarn qprel_imps)
383 doImports iface_cache i_info us []
384 = return (emptyBag, emptyBag, emptyBag, emptyBag, emptyBag, emptyBag, emptyBag)
385 doImports iface_cache i_info@(g_info,done_vals,done_tcs,imp_fn) us (imp:imps)
386 = doImport iface_cache i_info us1 imp
387 >>= \ (vals1, tcs1, unquals1, fixes1, errs1, warns1, imps1) ->
389 new_vals = [ (moduleNamePair rn, rn) | (_,rn) <- bagToList vals1,
390 not (maybeToBool (lookupFM done_vals (moduleNamePair rn))) ]
391 -- moduleNamePair computed twice
392 ext_vals = addListToFM done_vals new_vals
394 new_tcs = [ (moduleNamePair rn, rn) | (_,rn) <- bagToList tcs1,
395 not (maybeToBool (lookupFM done_tcs (moduleNamePair rn))) ]
396 ext_tcs = addListToFM done_tcs new_tcs
398 doImports iface_cache (g_info,ext_vals,ext_tcs,imp_fn) us2 imps
399 >>= \ (vals2, tcs2, unquals2, fixes2, errs2, warns2, imps2) ->
400 return (vals1 `unionBags` vals2,
401 tcs1 `unionBags` tcs2,
402 unquals1 `unionBags` unquals2,
403 fixes1 `unionBags` fixes2,
404 errs1 `unionBags` errs2,
405 warns1 `unionBags` warns2,
406 imps1 `unionBags` imps2)
408 (us1, us2) = splitUniqSupply us
411 doImport :: IfaceCache
415 -> IO (Bag (RdrName,RnName), -- values
416 Bag (RdrName,RnName), -- tycons/classes
417 Bag (Module,RnName), -- unqual imports
418 Bag RenamedFixityDecl,
421 Bag (RnName,(ExportFlag,Bag SrcLoc))) -- import flags and src locs
423 doImport iface_cache info us (ImportDecl mod qual maybe_as maybe_spec src_loc)
424 = cachedIface False iface_cache mod >>= \ maybe_iface ->
427 return (emptyBag, emptyBag, emptyBag, emptyBag,
428 unitBag err, emptyBag, emptyBag)
431 (b_vals, b_tcs, maybe_spec') = getBuiltins info mod maybe_spec
432 (ies, chk_ies, get_errs) = getOrigIEs iface maybe_spec'
434 doOrigIEs iface_cache info mod src_loc us ies
435 >>= \ (ie_vals, ie_tcs, imp_flags, errs, warns) ->
436 accumulate (map (checkOrigIE iface_cache) chk_ies)
437 >>= \ chk_errs_warns ->
439 final_vals = mapBag fst_occ b_vals `unionBags` mapBag pair_occ ie_vals
440 final_tcs = mapBag fst_occ b_tcs `unionBags` mapBag pair_occ ie_tcs
442 accumulate (map (getFixityDecl iface_cache) (bagToList final_vals))
443 >>= \ fix_maybes_errs ->
445 (chk_errs, chk_warns) = unzip chk_errs_warns
446 (fix_maybes, fix_errs) = unzip fix_maybes_errs
448 unquals = if qual then emptyBag
449 else mapBag pair_as (ie_vals `unionBags` ie_tcs)
451 final_fixes = listToBag (catMaybes fix_maybes)
453 final_errs = mapBag (\ err -> err mod src_loc) (unionManyBags (get_errs:chk_errs))
454 `unionBags` errs `unionBags` unionManyBags fix_errs
455 final_warns = mapBag (\ warn -> warn mod src_loc) (unionManyBags chk_warns)
457 imp_stuff = mapBag (\ (n,imp) -> (n,(imp,unitBag src_loc))) imp_flags
459 return (final_vals, final_tcs, unquals, final_fixes,
460 final_errs, final_warns, imp_stuff)
462 as_mod = case maybe_as of {Nothing -> mod; Just as_this -> as_this}
463 mk_occ str = if qual then Qual as_mod str else Unqual str
465 fst_occ (str, rn) = (mk_occ str, rn)
466 pair_occ rn = (mk_occ (getLocalName rn), rn)
467 pair_as rn = (as_mod, rn)
470 getBuiltins _ mod maybe_spec
471 | not (fromPrelude mod)
472 = (emptyBag, emptyBag, maybe_spec)
474 getBuiltins (((b_val_names,b_tc_names),_,_,_),_,_,_) mod maybe_spec
476 Nothing -> (all_vals, all_tcs, Nothing)
478 Just (True, ies) -> -- hiding does not work for builtin names
479 (all_vals, all_tcs, maybe_spec)
481 Just (False, ies) -> let
482 (vals,tcs,ies_left) = do_builtin ies
484 (vals, tcs, Just (False, ies_left))
486 all_vals = do_all_builtin (fmToList b_val_names)
487 all_tcs = do_all_builtin (fmToList b_tc_names)
489 do_all_builtin [] = emptyBag
490 do_all_builtin ((str,rn):rest)
491 = (str, rn) `consBag` do_all_builtin rest
493 do_builtin [] = (emptyBag,emptyBag,[])
495 = let str = unqual_str (ie_name ie)
497 case (lookupFM b_tc_names str) of -- NB: we favour the tycon/class FM...
498 Just rn -> case (ie,rn) of
499 (IEThingAbs _, WiredInTyCon tc)
500 -> (vals, (str, rn) `consBag` tcs, ies_left)
501 (IEThingAll _, WiredInTyCon tc)
502 -> (listToBag (map (\ id -> (getLocalName id, WiredInId id))
505 (str,rn) `consBag` tcs, ies_left)
506 _ -> panic "importing builtin names (1)"
509 case (lookupFM b_val_names str) of
510 Nothing -> (vals, tcs, ie:ies_left)
511 Just rn -> case (ie,rn) of
512 (IEVar _, WiredInId _)
513 -> ((str, rn) `consBag` vals, tcs, ies_left)
514 _ -> panic "importing builtin names (2)"
516 (vals, tcs, ies_left) = do_builtin ies
519 getOrigIEs (ParsedIface _ _ _ _ _ _ exps _ _ _ _ _ _) Nothing -- import all
520 = (map mkAllIE (eltsFM exps), [], emptyBag)
522 getOrigIEs (ParsedIface _ _ _ _ _ _ exps _ _ _ _ _ _) (Just (True, ies)) -- import hiding
523 = (map mkAllIE (eltsFM exps_left), found_ies, errs)
525 (found_ies, errs) = lookupIEs exps ies
526 exps_left = delListFromFM exps (map (getLocalName.ie_name.fst) found_ies)
528 getOrigIEs (ParsedIface _ _ _ _ _ _ exps _ _ _ _ _ _) (Just (False, ies)) -- import these
529 = (map fst found_ies, found_ies, errs)
531 (found_ies, errs) = lookupIEs exps ies
534 mkAllIE (orig,ExportAbs)
535 = ASSERT(isLexCon (getLocalName orig))
537 mkAllIE (orig, ExportAll)
538 | isLexCon (getLocalName orig)
546 lookupIEs exps (ie:ies)
547 = case lookupFM exps (unqual_str (ie_name ie)) of
549 (orig_ies, unknownImpSpecErr ie `consBag` errs)
551 (orig_ie orig flag ie ++ orig_ies,
552 adderr_if (seen_ie orig orig_ies) (duplicateImpSpecErr ie) errs)
554 (orig_ies, errs) = lookupIEs exps ies
556 orig_ie orig flag (IEVar n) = [(IEVar orig, flag)]
557 orig_ie orig flag (IEThingAbs n) = [(IEThingAbs orig, flag)]
558 orig_ie orig flag (IEThingAll n) = [(IEThingAll orig, flag)]
559 orig_ie orig flag (IEThingWith n ns) = [(IEThingWith orig ns, flag)]
561 seen_ie orig seen_ies = any (\ (ie,_) -> orig == ie_name ie) seen_ies
564 doOrigIEs iface_cache info mod src_loc us []
565 = return (emptyBag,emptyBag,emptyBag,emptyBag,emptyBag)
567 doOrigIEs iface_cache info mod src_loc us (ie:ies)
568 = doOrigIE iface_cache info mod src_loc us1 ie
569 >>= \ (vals1, tcs1, imps1, errs1, warns1) ->
570 doOrigIEs iface_cache info mod src_loc us2 ies
571 >>= \ (vals2, tcs2, imps2, errs2, warns2) ->
572 return (vals1 `unionBags` vals2,
573 tcs1 `unionBags` tcs2,
574 imps1 `unionBags` imps2,
575 errs1 `unionBags` errs2,
576 warns1 `unionBags` warns2)
578 (us1, us2) = splitUniqSupply us
580 doOrigIE iface_cache info mod src_loc us ie
581 = with_decl iface_cache (ie_name ie)
582 (\ err -> (emptyBag, emptyBag, emptyBag, unitBag err, emptyBag))
583 (\ decl -> case initRn True mod emptyRnEnv us
585 pushSrcLocRn src_loc $
586 getIfaceDeclNames ie decl)
588 ((vals, tcs, imps), errs, warns) -> (vals, tcs, imps, errs, warns))
590 checkOrigIE iface_cache (IEThingAll n, ExportAbs)
591 = with_decl iface_cache n
592 (\ err -> (unitBag (\ mod locn -> err), emptyBag))
593 (\ decl -> case decl of
594 TypeSig _ _ _ -> (emptyBag, unitBag (allWhenSynImpSpecWarn n))
595 other -> (unitBag (allWhenAbsImpSpecErr n), emptyBag))
597 checkOrigIE iface_cache (IEThingWith n ns, ExportAbs)
598 = return (unitBag (withWhenAbsImpSpecErr n), emptyBag)
600 checkOrigIE iface_cache (IEThingWith n ns, ExportAll)
601 = with_decl iface_cache n
602 (\ err -> (unitBag (\ mod locn -> err), emptyBag))
603 (\ decl -> case decl of
604 NewTypeSig _ con _ _ -> (check_with "constructrs" [con] ns, emptyBag)
605 DataSig _ cons fields _ _ -> (check_with "constructrs (and fields)" (cons++fields) ns, emptyBag)
606 ClassSig _ ops _ _ -> (check_with "class ops" ops ns, emptyBag))
608 check_with str has rdrs
609 | sortLt (<) (map getLocalName has) == sortLt (<) (map unqual_str rdrs)
612 = unitBag (withImpSpecErr str n has rdrs)
614 checkOrigIE iface_cache other
615 = return (emptyBag, emptyBag)
618 with_decl iface_cache n do_err do_decl
619 = cachedDecl iface_cache (isRdrLexCon n) n >>= \ maybe_decl ->
621 Failed err -> return (do_err err)
622 Succeeded decl -> return (do_decl decl)
625 getFixityDecl iface_cache (_,rn)
627 (mod, str) = moduleNamePair rn
629 cachedIface True iface_cache mod >>= \ maybe_iface ->
632 return (Nothing, unitBag err)
633 Succeeded (ParsedIface _ _ _ _ _ _ _ _ fixes _ _ _ _) ->
634 case lookupFM fixes str of
635 Nothing -> return (Nothing, emptyBag)
636 Just (InfixL _ i) -> return (Just (InfixL rn i), emptyBag)
637 Just (InfixR _ i) -> return (Just (InfixR rn i), emptyBag)
638 Just (InfixN _ i) -> return (Just (InfixN rn i), emptyBag)
640 ie_name (IEVar n) = n
641 ie_name (IEThingAbs n) = n
642 ie_name (IEThingAll n) = n
643 ie_name (IEThingWith n _) = n
645 unqual_str (Unqual str) = str
646 unqual_str q@(Qual _ _) = panic "unqual_str"
648 adderr_if True err errs = err `consBag` errs
649 adderr_if False err errs = errs
652 *********************************************************
654 \subsection{Actually creating the imported names}
656 *********************************************************
659 getIfaceDeclNames :: RdrNameIE -> RdrIfaceDecl
660 -> RnM_IInfo s (Bag RnName, -- values
661 Bag RnName, -- tycons/classes
662 Bag (RnName,ExportFlag)) -- import flags
664 getIfaceDeclNames ie (ValSig val src_loc _)
665 = newImportedName False src_loc Nothing Nothing val `thenRn` \ val_name ->
666 returnRn (unitBag (RnName val_name),
668 unitBag (RnName val_name, ExportAll))
670 getIfaceDeclNames ie (TypeSig tycon src_loc _)
671 = newImportedName True src_loc Nothing Nothing tycon `thenRn` \ tycon_name ->
673 unitBag (RnSyn tycon_name),
674 unitBag (RnSyn tycon_name, ExportAll))
676 getIfaceDeclNames ie (NewTypeSig tycon con src_loc _)
677 = newImportedName True src_loc Nothing Nothing tycon `thenRn` \ tycon_name ->
678 newImportedName False src_loc (Just (nameExportFlag tycon_name))
679 (Just (nameImportFlag tycon_name))
680 con `thenRn` \ con_name ->
681 returnRn (if imp_all (imp_flag ie) then
682 unitBag (RnConstr con_name tycon_name)
685 unitBag (RnData tycon_name [con_name] []),
686 unitBag (RnData tycon_name [con_name] [], imp_flag ie))
688 getIfaceDeclNames ie (DataSig tycon cons fields src_loc _)
689 = newImportedName True src_loc Nothing Nothing tycon `thenRn` \ tycon_name ->
690 mapRn (newImportedName False src_loc (Just (nameExportFlag tycon_name))
691 (Just (nameImportFlag tycon_name)))
692 cons `thenRn` \ con_names ->
693 mapRn (newImportedName False src_loc (Just (nameExportFlag tycon_name))
694 (Just (nameImportFlag tycon_name)))
695 fields `thenRn` \ field_names ->
697 rn_tycon = RnData tycon_name con_names field_names
698 rn_constrs = [ RnConstr name tycon_name | name <- con_names ]
699 rn_fields = [ RnField name tycon_name | name <- field_names ]
701 returnRn (if imp_all (imp_flag ie) then
702 listToBag rn_constrs `unionBags` listToBag rn_fields
706 unitBag (rn_tycon, imp_flag ie))
708 getIfaceDeclNames ie (ClassSig cls ops src_loc _)
709 = newImportedName True src_loc Nothing Nothing cls `thenRn` \ cls_name ->
710 mapRn (newImportedName False src_loc (Just (nameExportFlag cls_name))
711 (Just (nameImportFlag cls_name)))
712 ops `thenRn` \ op_names ->
713 returnRn (if imp_all (imp_flag ie) then
714 listToBag (map (\ n -> RnClassOp n cls_name) op_names)
717 unitBag (RnClass cls_name op_names),
718 unitBag (RnClass cls_name op_names, imp_flag ie))
721 imp_all ExportAll = True
724 imp_flag (IEThingAbs _) = ExportAbs
725 imp_flag (IEThingAll _) = ExportAll
726 imp_flag (IEThingWith _ _) = ExportAll
729 *********************************************************
731 \subsection{Creating a new imported name}
733 *********************************************************
736 newImportedName :: Bool -- True => tycon or class
738 -> Maybe ExportFlag -- maybe export flag
739 -> Maybe ExportFlag -- maybe import flag
740 -> RdrName -- orig name
743 newImportedName tycon_or_class locn maybe_exp maybe_imp rdr
744 = getExtraRn `thenRn` \ ((_,b_keys,exp_fn,occ_fn),done_vals,done_tcs,imp_fn) ->
745 case if tycon_or_class
746 then lookupFM done_tcs (moduleNamePair rdr)
747 else lookupFM done_vals (moduleNamePair rdr)
749 Just rn -> returnRn (getName rn)
751 rnGetUnique `thenRn` \ u ->
755 Unqual n -> case lookupFM b_keys n of
759 exp = case maybe_exp of
763 imp = case maybe_imp of
767 (imp_flag, imp_locs) = imp_fn n
769 n = mkImportedName uniq rdr imp locn imp_locs exp (occ_fn n) -- NB: two "n"s
775 globalDupNamesErr rdr rns sty
776 = ppAboves (message : map pp_dup rns)
778 message = ppBesides [ppStr "multiple declarations of `", pprNonSym sty rdr, ppStr "'"]
780 pp_dup rn = addShortErrLocLine (get_loc rn) (\ sty ->
781 ppCat [pp_descrip rn, pprNonSym sty rn]) sty
783 get_loc rn = case getImpLocs rn of
787 pp_descrip (RnName _) = ppStr "as a value:"
788 pp_descrip (RnSyn _) = ppStr "as a type synonym:"
789 pp_descrip (RnData _ _ _) = ppStr "as a data type:"
790 pp_descrip (RnConstr _ _) = ppStr "as a data constructor:"
791 pp_descrip (RnField _ _) = ppStr "as a record field:"
792 pp_descrip (RnClass _ _) = ppStr "as a class:"
793 pp_descrip (RnClassOp _ _) = ppStr "as a class method:"
796 dupImportWarn (ImportDecl m1 _ _ _ locn1 : dup_imps) sty
797 = ppAboves (item1 : map dup_item dup_imps)
799 item1 = addShortWarnLocLine locn1 (\ sty ->
800 ppCat [ppStr "multiple imports from module", ppPStr m1]) sty
802 dup_item (ImportDecl m _ _ _ locn)
803 = addShortWarnLocLine locn (\ sty ->
804 ppCat [ppStr "here was another import from module", ppPStr m]) sty
806 qualPreludeImportWarn (ImportDecl m _ _ _ locn)
807 = addShortWarnLocLine locn (\ sty ->
808 ppCat [ppStr "qualified import of prelude module", ppPStr m])
810 unknownImpSpecErr ie imp_mod locn
811 = addShortErrLocLine locn (\ sty ->
812 ppBesides [ppStr "module ", ppPStr imp_mod, ppStr " does not export `", ppr sty (ie_name ie), ppStr "'"])
814 duplicateImpSpecErr ie imp_mod locn
815 = addShortErrLocLine locn (\ sty ->
816 ppBesides [ppStr "`", ppr sty (ie_name ie), ppStr "' already seen in import list"])
818 allWhenSynImpSpecWarn n imp_mod locn
819 = addShortWarnLocLine locn (\ sty ->
820 ppBesides [ppStr "type synonym `", ppr sty n, ppStr "' should not be imported with (..)"])
822 allWhenAbsImpSpecErr n imp_mod locn
823 = addShortErrLocLine locn (\ sty ->
824 ppBesides [ppStr "module ", ppPStr imp_mod, ppStr " only exports `", ppr sty n, ppStr "' abstractly"])
826 withWhenAbsImpSpecErr n imp_mod locn
827 = addShortErrLocLine locn (\ sty ->
828 ppBesides [ppStr "module ", ppPStr imp_mod, ppStr " only exports `", ppr sty n, ppStr "' abstractly"])
830 withImpSpecErr str n has ns imp_mod locn
831 = addErrLoc locn "" (\ sty ->
832 ppAboves [ ppBesides [ppStr "inconsistent list of", ppStr str, ppStr "in import list for `", ppr sty n, ppStr "'"],
833 ppCat [ppStr " expected:", ppInterleave ppComma (map (ppr sty) has)],
834 ppCat [ppStr " found: ", ppInterleave ppComma (map (ppr sty) ns)] ])
836 dupFieldErr con locn (dup:rest)
837 = addShortErrLocLine locn (\ sty ->
838 ppBesides [ppStr "record field `", ppr sty dup, ppStr "declared multiple times in `", ppr sty con, ppStr "'"])