2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
4 \section[Rename-three]{Third of the renaming passes}
6 The business of this pass is to:
8 \item find all the things declared at top level,
9 \item assign uniques to them
10 \item return an association list mapping their @ProtoName@s to
11 freshly-minted @Names@ for them.
14 No attempt is made to discover whether the same thing is declared
15 twice: that is up to the caller to sort out.
18 #include "HsVersions.h"
22 initRn3, Rn3M(..), -- re-exported from monad
25 Module, Bag, ProtoNamePat(..), InPat, Maybe, Name,
26 ExportFlag, PprStyle, Pretty(..), PrettyRep, ProtoName,
27 PreludeNameFun(..), PreludeNameFuns(..), SplitUniqSupply
31 import Bag -- lots of stuff
32 import Errors ( dupNamesErr, dupPreludeNameErr,
33 badExportNameErr, badImportNameErr,
36 import HsCore -- ****** NEED TO SEE CONSTRUCTORS ******
37 import HsPragmas -- ****** NEED TO SEE CONSTRUCTORS ******
39 import Maybes ( Maybe(..) )
40 import Name ( Name(..) )
41 import NameTypes ( fromPrelude, FullName )
43 import RenameAuxFuns ( mkGlobalNameFun,
44 GlobalNameFuns(..), GlobalNameFun(..),
45 PreludeNameFuns(..), PreludeNameFun(..)
48 import SrcLoc ( mkUnknownSrcLoc, SrcLoc )
52 *********************************************************
54 \subsection{Type declarations}
56 *********************************************************
59 type BagAssoc = Bag (ProtoName, Name) -- Bag version
60 type NameSpaceAssoc = [(ProtoName, Name)] -- List version
64 *********************************************************
66 \subsection{Main function: @rnModule3@}
68 *********************************************************
71 rnModule3 :: PreludeNameFuns
72 -> [FAST_STRING] -- list of imported module names
74 -> Rn3M ( NameSpaceAssoc, NameSpaceAssoc,
75 GlobalNameFun, GlobalNameFun,
78 rnModule3 pnfs@(val_pnf, tc_pnf) imported_mod_names
79 (Module mod_name exports imports _ ty_decls _ class_decls
80 inst_decls _ _ binds sigs _)
82 = putInfoDownM3 {- ???pnfs -} mod_name exports (
84 doTyDecls3 ty_decls `thenRn3` \ (constrs, tycons) ->
85 doClassDecls3 class_decls `thenRn3` \ (ops, classes) ->
86 doBinds3 binds `thenRn3` \ val_binds ->
87 doIntSigs3 sigs `thenRn3` \ val_sigs ->
89 let val_namespace = constrs `unionBags` ops `unionBags` val_binds
91 tc_namespace = tycons `unionBags` classes
93 (var_alist, var_dup_errs) = deal_with_dups "variable" val_pnf (bagToList val_namespace)
94 (tc_alist, tc_dup_errs) = deal_with_dups "type or class" tc_pnf (bagToList tc_namespace)
95 v_gnf = mkGlobalNameFun mod_name val_pnf var_alist
96 tc_gnf = mkGlobalNameFun mod_name tc_pnf tc_alist
99 verifyExports v_gnf tc_gnf (mod_name : imported_mod_names) exports
100 `thenRn3` \ export_errs ->
101 verifyImports v_gnf tc_gnf imports `thenRn3` \ import_errs ->
103 returnRn3 ( var_alist, tc_alist,
105 var_dup_errs `unionBags` tc_dup_errs `unionBags`
106 export_errs `unionBags` import_errs
109 deal_with_dups :: String -> PreludeNameFun -> NameSpaceAssoc
110 -> (NameSpaceAssoc, Bag Error)
112 deal_with_dups kind_str pnf alist
114 listToBag (map mk_dup_err dup_lists) `unionBags`
115 listToBag (map mk_prel_dup_err prel_dups)
118 goodies :: [(ProtoName,Name)] --NameSpaceAssoc
119 dup_lists :: [[(ProtoName, Name)]]
121 -- Find all the names which are defined twice.
122 -- By "name" here, we mean "string"; that is, we are looking
123 -- for places where two strings are bound to different Names
124 -- in the top-level scope of this module.
126 (singles, dup_lists) = removeDups cmp alist
127 -- We want to compare their *local* names; the removeDups thing
128 -- is checking for whether two objects have the same local name.
129 cmp (a, _) (b, _) = cmpByLocalName a b
131 -- Anything in alist with a Unk name is defined right here in
132 -- this module; hence, it should not be a prelude name. We
133 -- need to check this separately, because the prelude is
134 -- imported only implicitly, via the PrelNameFuns argument
136 (goodies, prel_dups) = if fromPrelude mod_name then
137 (singles, []) -- Compiling the prelude, so ignore this check
139 partition local_def_of_prelude_thing singles
141 local_def_of_prelude_thing (Unk s, _)
143 Just _ -> False -- Eek! It's a prelude name
144 Nothing -> True -- It isn't; all is ok
145 local_def_of_prelude_thing other = True
147 mk_dup_err :: [(ProtoName, Name)] -> Error
148 mk_dup_err dups_of_name
150 dup_pnames_w_src_loc = [ (pn, getSrcLoc name) | (pn,name) <- dups_of_name ]
152 dupNamesErr kind_str dup_pnames_w_src_loc
154 -- This module defines a prelude thing
155 mk_prel_dup_err :: (ProtoName, Name) -> Error
156 mk_prel_dup_err (pn, name)
157 = dupPreludeNameErr kind_str (pn, getSrcLoc name)
160 *********************************************************
162 \subsection{Type and class declarations}
164 *********************************************************
167 doTyDecls3 :: [ProtoNameTyDecl] -> Rn3M (BagAssoc, BagAssoc)
169 doTyDecls3 [] = returnRn3 (emptyBag, emptyBag)
171 doTyDecls3 (tyd:tyds)
172 = andRn3 combiner (do_decl tyd) (doTyDecls3 tyds)
174 combiner (cons1, tycons1) (cons2, tycons2)
175 = (cons1 `unionBags` cons2, tycons1 `unionBags` tycons2)
177 do_decl (TyData context tycon tyvars condecls deriv pragmas src_loc)
178 = newFullNameM3 tycon src_loc True{-tycon-ish-} Nothing
179 `thenRn3` \ (uniq, tycon_name) ->
181 exp_flag = getExportFlag tycon_name
182 -- we want to force all data cons to have the very
183 -- same export flag as their type constructor
185 doConDecls3 False{-not invisibles-} exp_flag condecls `thenRn3` \ data_cons ->
186 do_data_pragmas exp_flag pragmas `thenRn3` \ pragma_data_cons ->
187 returnRn3 (data_cons `unionBags` pragma_data_cons,
188 unitBag (tycon, OtherTyCon uniq tycon_name (length tyvars)
189 True -- indicates @data@ tycon
190 [ c | (_,c) <- bagToList data_cons ]))
193 do_decl (TySynonym tycon tyvars monoty pragmas src_loc)
194 = newFullNameM3 tycon src_loc True{-tycon-ish-} Nothing
195 `thenRn3` \ (uniq, tycon_name) ->
197 unitBag (tycon, OtherTyCon uniq tycon_name (length tyvars) False bottom))
198 -- False indicates @type@ tycon
200 bottom = panic "do_decl: data cons on synonym?"
202 do_data_pragmas exp_flag (DataPragmas con_decls specs)
203 = doConDecls3 True{-invisibles-} exp_flag con_decls
207 doConDecls3 :: Bool -- True <=> mk invisible FullNames
208 -> ExportFlag -- Export flag of the TyCon; we want
210 -> [ProtoNameConDecl]
213 doConDecls3 _ _ [] = returnRn3 emptyBag
215 doConDecls3 want_invisibles exp_flag (cd:cds)
216 = andRn3 unionBags (do_decl cd) (doConDecls3 want_invisibles exp_flag cds)
218 mk_name = if want_invisibles then newInvisibleNameM3 else newFullNameM3
220 do_decl (ConDecl con tys src_loc)
221 = mk_name con src_loc True{-tycon-ish-} (Just exp_flag) `thenRn3` \ (uniq, con_name) ->
222 returnRn3 (unitBag (con, OtherTopId uniq con_name))
226 @doClassDecls3@ uses the `name function' to map local class names into
227 original names, calling @doClassOps3@ to do the same for the
228 class operations. @doClassDecls3@ is used to process module
232 doClassDecls3 :: [ProtoNameClassDecl] -> Rn3M (BagAssoc, BagAssoc)
234 doClassDecls3 [] = returnRn3 (emptyBag, emptyBag)
236 doClassDecls3 (cd:cds)
237 = andRn3 combiner (do_decl cd) (doClassDecls3 cds)
239 combiner (ops1, classes1) (ops2, classes2)
240 = (ops1 `unionBags` ops2, classes1 `unionBags` classes2)
242 do_decl (ClassDecl context cname@(Prel c) tyvar sigs defaults pragmas src_loc)
243 = doClassOps3 c 1 sigs `thenRn3` \ (_, ops) ->
244 returnRn3 (ops, unitBag (cname, c))
246 do_decl (ClassDecl context cname tyvar sigs defaults pragmas src_loc)
247 = newFullNameM3 cname src_loc True{-tycon-ish-} Nothing
248 `thenRn3` \ (uniq, class_name) ->
249 fixRn3 ( \ ~(clas_ops,_) ->
251 class_Name = OtherClass uniq class_name
252 [ o | (_,o) <- bagToList clas_ops ]
254 doClassOps3 class_Name 1 sigs `thenRn3` \ (_, ops) ->
255 returnRn3 (ops, class_Name)
256 ) `thenRn3` \ (ops, class_Name) ->
258 returnRn3 (ops, unitBag (cname, class_Name))
261 We stitch on a class-op tag to each class operation. They are guaranteed
262 to be done in left-to-right order.
265 doClassOps3 :: Name{-class-} -> Int -> [ProtoNameSig] -> Rn3M (Int, BagAssoc)
267 doClassOps3 clas tag [] = returnRn3 (tag, emptyBag)
269 doClassOps3 clas tag (sig:rest)
270 = do_op sig `thenRn3` \ (tag1, bag1) ->
271 doClassOps3 clas tag1 rest `thenRn3` \ (tagr, bagr) ->
272 returnRn3 (tagr, bag1 `unionBags` bagr)
274 do_op (ClassOpSig op ty pragma src_loc)
275 = newFullNameM3 op src_loc False{-not tyconish-} Nothing `thenRn3` \ (uniq, _) ->
277 op_name = ClassOpName uniq clas (get_str op) tag
279 returnRn3 (tag+1, unitBag (op, op_name))
281 -- A rather yukky function to get the original name out of a class operation.
282 get_str :: ProtoName -> FAST_STRING
284 get_str (Imp _ d _ _) = d
287 Remember, interface signatures don't have user-pragmas, etc., in them.
289 doIntSigs3 :: [ProtoNameSig] -> Rn3M BagAssoc
291 doIntSigs3 [] = returnRn3 emptyBag
294 = andRn3 unionBags (do_sig s) (doIntSigs3 ss)
296 do_sig (Sig v ty pragma src_loc)
297 = newFullNameM3 v src_loc False{-distinctly untycon-ish-} Nothing
298 `thenRn3` \ (uniq, v_fname) ->
299 returnRn3 (unitBag (v, OtherTopId uniq v_fname))
302 *********************************************************
304 \subsection{Bindings}
306 *********************************************************
309 doBinds3 :: ProtoNameBinds -> Rn3M BagAssoc
311 doBinds3 EmptyBinds = returnRn3 emptyBag
313 doBinds3 (ThenBinds binds1 binds2)
314 = andRn3 unionBags (doBinds3 binds1) (doBinds3 binds2)
316 doBinds3 (SingleBind bind) = doBind3 bind
318 doBinds3 (BindWith bind sigs) = doBind3 bind
322 doBind3 :: ProtoNameBind -> Rn3M BagAssoc
323 doBind3 EmptyBind = returnRn3 emptyBag
324 doBind3 (NonRecBind mbind) = doMBinds3 mbind
325 doBind3 (RecBind mbind) = doMBinds3 mbind
327 doMBinds3 :: ProtoNameMonoBinds -> Rn3M BagAssoc
329 doMBinds3 EmptyMonoBinds = returnRn3 emptyBag
330 doMBinds3 (PatMonoBind pat grhss_and_binds locn) = doPat3 locn pat
331 doMBinds3 (FunMonoBind p_name _ locn) = doTopLevName locn p_name
333 doMBinds3 (AndMonoBinds mbinds1 mbinds2)
334 = andRn3 unionBags (doMBinds3 mbinds1) (doMBinds3 mbinds2)
337 Fold over a list of patterns:
339 doPats3 locn [] = returnRn3 emptyBag
340 doPats3 locn (pat:pats)
341 = andRn3 unionBags (doPat3 locn pat) (doPats3 locn pats)
345 doPat3 :: SrcLoc -> ProtoNamePat -> Rn3M BagAssoc
347 doPat3 locn WildPatIn = returnRn3 emptyBag
348 doPat3 locn (LitPatIn _) = returnRn3 emptyBag
349 doPat3 locn (LazyPatIn pat) = doPat3 locn pat
350 doPat3 locn (VarPatIn n) = doTopLevName locn n
351 doPat3 locn (ListPatIn pats) = doPats3 locn pats
352 doPat3 locn (TuplePatIn pats) = doPats3 locn pats
353 doPat3 locn (NPlusKPatIn n _) = doTopLevName locn n
355 doPat3 locn (AsPatIn p_name pat)
356 = andRn3 unionBags (doTopLevName locn p_name) (doPat3 locn pat)
358 doPat3 locn (ConPatIn name pats) = doPats3 locn pats
360 doPat3 locn (ConOpPatIn pat1 name pat2)
361 = andRn3 unionBags (doPat3 locn pat1) (doPat3 locn pat2)
364 doPat3 locn (ProcessorPatIn pats pat)
365 = andRn3 unionBags (doPats3 locn pats) (doPat3 locn pat)
366 #endif {- Data Parallel Haskell -}
370 doTopLevName :: SrcLoc -> ProtoName -> Rn3M BagAssoc
373 = newFullNameM3 pn locn False{-un-tycon-ish-} Nothing `thenRn3` \ (uniq, name) ->
374 returnRn3 (unitBag (pn, OtherTopId uniq name))
377 Have to check that export/imports lists aren't too drug-crazed.
380 verifyExports :: GlobalNameFun -> GlobalNameFun
381 -> [FAST_STRING] -- module names that might appear
382 -- in an export list; includes the
383 -- name of this module
384 -> [IE] -- export list
387 verifyExports v_gnf tc_gnf imported_mod_names exports
388 = mapRn3 verify exports `thenRn3` \ errs ->
389 chk_exp_dups exports `thenRn3` \ dup_errs ->
390 returnRn3 (unionManyBags (errs ++ dup_errs))
392 present nf str = nf (Unk str)
394 ok = returnRn3 emptyBag
395 naughty nm msg = returnRn3 (unitBag (badExportNameErr (_UNPK_ nm) msg))
396 undef_name nm = naughty nm "is not defined."
397 dup_name (nm:_)= naughty nm "occurs more than once."
402 export_strs = [ nm | (nm, _) <- fst (getRawIEStrings exports) ]
403 (_, dup_lists) = removeDups _CMP_STRING_ export_strs
405 mapRn3 dup_name dup_lists
407 ---------------- the more serious checking
409 = case (present v_gnf v) of { Nothing -> undef_name v; _ -> ok }
411 verify (IEModuleContents mod)
412 = if not (mod `is_elem` imported_mod_names) then undef_name mod else ok
414 is_elem = isIn "verifyExports"
416 verify (IEThingAbs tc)
417 = case (present tc_gnf tc) of
418 Nothing -> undef_name tc
419 Just nm -> case nm of
420 PreludeTyCon _ _ _ False{-syn-}
421 -> naughty tc "must be exported with a `(..)' -- it's a Prelude synonym."
422 OtherTyCon _ _ _ False{-syn-} _
423 -> naughty tc "must be exported with a `(..)' -- it's a synonym."
426 -> naughty tc "cannot be exported \"abstractly\" (it's a Prelude class)."
428 -> naughty tc "cannot be exported \"abstractly\" (it's a class)."
431 verify (IEThingAll tc)
432 = case (present tc_gnf tc) of
433 Nothing -> undef_name tc
434 Just nm -> case nm of
435 OtherTyCon _ _ _ True{-data-} [{-no cons-}]
436 -> naughty tc "can't be exported with a `(..)' -- it was imported abstractly."
439 verify (IEConWithCons tc cs)
440 = case (present tc_gnf tc) of
441 Nothing -> undef_name tc
442 Just nm -> mapRn3 verify (map IEVar cs) `thenRn3` \ errs ->
443 returnRn3 (unionManyBags errs)
444 -- ToDo: turgid checking which we don't care about (WDP 94/10)
446 verify (IEClsWithOps c ms)
447 = case (present tc_gnf c) of
448 Nothing -> undef_name c
449 Just _ -> mapRn3 verify (map IEVar ms) `thenRn3` \ errs ->
450 returnRn3 (unionManyBags errs)
451 -- ToDo: turgid checking which we don't care about (WDP 94/10)
454 Note: we're not too particular about whether something mentioned in an
455 import list is in {\em that} interface... (ToDo? Probably not.)
458 verifyImports :: GlobalNameFun -> GlobalNameFun
459 -> [ProtoNameImportedInterface]
462 verifyImports v_gnf tc_gnf imports
463 = mapRn3 chk_one (map collect imports) `thenRn3` \ errs ->
464 returnRn3 (unionManyBags errs)
466 -- collect: name/locn, import list, renamings list
468 collect (ImportAll iff renamings)
469 = (iface iff, [], [], renamings)
470 collect (ImportSome iff imp_list renamings)
471 = (iface iff, imp_list, [], renamings)
472 collect (ImportButHide iff hide_list renamings)
473 = (iface iff, [], hide_list, renamings)
476 iface (MkInterface name _ _ _ _ _ _ locn) = (name, locn)
479 chk_one :: ((FAST_STRING, SrcLoc), [IE], [IE], [Renaming])
482 chk_one ((mod_name, locn), import_list, hide_list, renamings)
483 = mapRn3 verify import_list `thenRn3` \ errs1 ->
484 chk_imp_dups import_list `thenRn3` \ dup_errs ->
485 -- ToDo: we could check the hiding list more carefully
486 chk_imp_dups hide_list `thenRn3` \ dup_errs2 ->
487 mapRn3 chk_rn renamings `thenRn3` \ errs2 ->
488 returnRn3 (unionManyBags (errs1 ++ dup_errs ++ dup_errs2 ++ errs2))
490 present nf str = nf (Unk (rename_it str))
493 = case [ too | (MkRenaming from too) <- renamings, str == from ] of
497 ok = returnRn3 emptyBag
498 naughty nm msg = returnRn3 (unitBag (badImportNameErr (_UNPK_ mod_name) (_UNPK_ nm) msg locn))
499 undef_name nm = naughty nm "is not defined."
500 undef_rn_name n r = naughty n ("is not defined (renamed to `"++ _UNPK_ r ++"').")
501 dup_name (nm:_) = naughty nm "occurs more than once."
506 import_strs = [ nm | (nm, _) <- fst (getRawIEStrings imports) ]
507 (_, dup_lists) = removeDups _CMP_STRING_ import_strs
509 mapRn3 dup_name dup_lists
512 chk_rn (MkRenaming from too) -- Note: "present" will rename
513 = case (present v_gnf from) of -- the "from" to the "too"...
515 Nothing -> case (present tc_gnf from) of
517 Nothing -> undef_rn_name from too
521 = case (present v_gnf v) of { Nothing -> undef_name v; _ -> ok }
523 verify (IEThingAbs tc)
524 = case (present tc_gnf tc) of
525 Nothing -> undef_name tc
526 Just nm -> case nm of
527 PreludeTyCon _ _ _ False{-syn-}
528 -> naughty tc "must be imported with a `(..)' -- it's a Prelude synonym."
529 OtherTyCon _ _ _ False{-syn-} _
530 -> naughty tc "must be imported with a `(..)' -- it's a synonym."
532 -> naughty tc "cannot be imported \"abstractly\" (it's a Prelude class)."
534 -> naughty tc "cannot be imported \"abstractly\" (it's a class)."
537 verify (IEThingAll tc)
538 = case (present tc_gnf tc) of
539 Nothing -> undef_name tc
540 Just nm -> case nm of
541 OtherTyCon _ _ _ True{-data-} [{-no cons-}]
542 -> naughty tc "can't be imported with a `(..)' -- the interface says it's abstract."
545 verify (IEConWithCons tc cs)
546 = case (present tc_gnf tc) of
547 Nothing -> undef_name tc
548 Just nm -> mapRn3 verify (map IEVar cs) `thenRn3` \ errs ->
549 returnRn3 (unionManyBags errs)
550 -- One could add a great wad of tedious checking
551 -- here, but I am too lazy to do so. WDP 94/10
553 verify (IEClsWithOps c ms)
554 = case (present tc_gnf c) of
555 Nothing -> undef_name c
556 Just _ -> mapRn3 verify (map IEVar ms) `thenRn3` \ errs ->
557 returnRn3 (unionManyBags errs)
558 -- Ditto about tedious checking. WDP 94/10