module RnEnv where -- Export everything
+IMPORT_1_3(List (nub))
IMP_Ubiq()
import CmdLineOpts ( opt_WarnNameShadowing )
rdrNameOcc, ieOcc, isQual, qual
)
import HsTypes ( getTyVarName, replaceTyVarName )
+import BasicTypes ( Fixity(..), FixityDirection(..) )
import RnMonad
-import Name ( Name, OccName(..), Provenance(..), DefnInfo(..), ExportFlag(..),
+import Name ( Name, OccName(..), Provenance(..), DefnInfo(..), ExportFlag(..), NamedThing(..),
occNameString, occNameFlavour,
SYN_IE(NameSet), emptyNameSet, addListToNameSet,
mkLocalName, mkGlobalName, modAndOcc, isLocallyDefinedName,
- isWiredInName, nameOccName, setNameProvenance, isVarOcc,
- pprProvenance, pprOccName, pprModule, pprNonSymOcc, pprNameProvenance
+ isWiredInName, nameOccName, setNameProvenance, isVarOcc, getNameProvenance,
+ pprProvenance, pprOccName, pprModule, pprNameProvenance
)
import TyCon ( TyCon )
import TysWiredIn ( tupleTyCon, listTyCon, charTyCon, intTyCon )
import FiniteMap
+import Outputable
import Unique ( Unique, unboundKey )
+import UniqFM ( Uniquable(..), listToUFM, plusUFM_C )
import Maybes ( maybeToBool )
import UniqSupply
import SrcLoc ( SrcLoc, noSrcLoc )
import Pretty
-import PprStyle ( PprStyle(..) )
-import Util ( panic, removeDups, pprTrace, assertPanic )
+import Outputable ( PprStyle(..) )
+import Util --( panic, removeDups, pprTrace, assertPanic )
+
\end{code}
-- If it's not in the cache we put it there with the correct provenance.
-- The idea is that, after all this, the cache
-- will contain a Name with the correct Provenance (i.e. Local)
+
+ -- OLD (now wrong) COMMENT:
+ -- "Actually, there's a catch. If this is the *second* binding for something
+ -- we want to allocate a *fresh* unique, rather than using the same Name as before.
+ -- Otherwise we don't detect conflicting definitions of the same top-level name!
+ -- So the only time we re-use a Name already in the cache is when it's one of
+ -- the Implicit magic-unique ones mentioned in the previous para"
+
+ -- This (incorrect) patch doesn't work for record decls, when we have
+ -- the same field declared in multiple constructors. With the above patch,
+ -- each occurrence got a new Name --- aargh!
+ --
+ -- So I reverted to the simple caching method (no "second-binding" thing)
+ -- The multiple-local-binding case is now handled by improving the conflict
+ -- detection in plusNameEnv.
let
provenance = LocalDef (rec_exp_fn new_name) loc
(us', us1) = splitUniqSupply us
key = (mod,occ)
new_name = case lookupFM cache key of
Just name -> setNameProvenance name provenance
- Nothing -> mkGlobalName uniq mod occ VanillaDefn provenance
+ other -> mkGlobalName uniq mod occ VanillaDefn provenance
new_cache = addToFM cache key new_name
in
setNameSupplyRn (us', inst_ns, new_cache) `thenRn_`
mod_name occ
(\_ -> export_flag)
loc
- InterfaceMode -> newGlobalName mod_name occ
+ InterfaceMode _ -> newGlobalName mod_name occ
-- newDfunName is a variant, specially for dfuns.
-- When renaming derived definitions we are in *interface* mode (because we can trip
\end{code}
\begin{code}
-bindLocatedLocalsRn :: String -- Documentation string for error message
+bindLocatedLocalsRn :: (PprStyle -> Doc) -- Documentation string for error message
-> [(RdrName,SrcLoc)]
-> ([Name] -> RnMS s a)
-> RnMS s a
bindLocatedLocalsRn doc_str rdr_names_w_loc enclosed_scope
- = -- Check for use of qualified names
- mapRn (qualNameErr doc_str) quals `thenRn_`
- -- Check for dupicated names in a binding group
- mapRn (dupNamesErr doc_str) dups `thenRn_`
+ = checkDupOrQualNames doc_str rdr_names_w_loc `thenRn_`
getNameEnv `thenRn` \ name_env ->
(if opt_WarnNameShadowing
in
setNameEnv new_name_env (enclosed_scope names)
where
- quals = filter (isQual.fst) rdr_names_w_loc
- (these, dups) = removeDups (\(n1,l1) (n2,l2) -> n1 `cmp` n2) rdr_names_w_loc
check_shadow name_env (rdr_name,loc)
= case lookupFM name_env rdr_name of
Nothing -> returnRn ()
bindLocalsRn doc_str rdr_names enclosed_scope
= getSrcLocRn `thenRn` \ loc ->
- bindLocatedLocalsRn doc_str (rdr_names `zip` repeat loc) enclosed_scope
+ bindLocatedLocalsRn (\_ -> text doc_str)
+ (rdr_names `zip` repeat loc)
+ enclosed_scope
bindTyVarsRn doc_str tyvar_names enclosed_scope
= getSrcLocRn `thenRn` \ loc ->
in
bindLocatedLocalsRn doc_str located_tyvars $ \ names ->
enclosed_scope (zipWith replaceTyVarName tyvar_names names)
+
+ -- Works in any variant of the renamer monad
+checkDupOrQualNames, checkDupNames :: (PprStyle -> Doc)
+ -> [(RdrName, SrcLoc)]
+ -> RnM s d ()
+
+checkDupOrQualNames doc_str rdr_names_w_loc
+ = -- Check for use of qualified names
+ mapRn (qualNameErr doc_str) quals `thenRn_`
+ checkDupNames doc_str rdr_names_w_loc
+ where
+ quals = filter (isQual.fst) rdr_names_w_loc
+
+checkDupNames doc_str rdr_names_w_loc
+ = -- Check for dupicated names in a binding group
+ mapRn (dupNamesErr doc_str) dups `thenRn_`
+ returnRn ()
+ where
+ (_, dups) = removeDups (\(n1,l1) (n2,l2) -> n1 `cmp` n2) rdr_names_w_loc
\end{code}
-- Not found when processing an imported declaration,
-- so we create a new name for the purpose
- InterfaceMode ->
+ InterfaceMode _ ->
case rdr_name of
Qual mod_name occ -> newGlobalName mod_name occ
lookupOccRn rdr_name
= getNameEnv `thenRn` \ name_env ->
lookupRn name_env rdr_name `thenRn` \ name ->
- addOccurrenceName Compulsory name
+ addOccurrenceName name
-- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
-- environment. It's used for record field names only.
lookupGlobalOccRn rdr_name
= getGlobalNameEnv `thenRn` \ name_env ->
lookupRn name_env rdr_name `thenRn` \ name ->
- addOccurrenceName Compulsory name
-
--- lookupOptionalOccRn is similar, but it's used in places where
--- we don't *have* to find a definition for the thing.
-lookupOptionalOccRn :: RdrName -> RnMS s Name
-lookupOptionalOccRn rdr_name
- = getNameEnv `thenRn` \ name_env ->
- lookupRn name_env rdr_name `thenRn` \ name ->
- addOccurrenceName Optional name
+ addOccurrenceName name
lookupImplicitOccRn :: RdrName -> RnMS s Name
lookupImplicitOccRn (Qual mod occ)
= newGlobalName mod occ `thenRn` \ name ->
- addOccurrenceName Compulsory name
+ addOccurrenceName name
-addImplicitOccRn :: Name -> RnM s d Name
-addImplicitOccRn name = addOccurrenceName Compulsory name
+addImplicitOccRn :: Name -> RnMS s Name
+addImplicitOccRn name = addOccurrenceName name
-addImplicitOccsRn :: [Name] -> RnM s d ()
-addImplicitOccsRn names = addOccurrenceNames Compulsory names
+addImplicitOccsRn :: [Name] -> RnMS s ()
+addImplicitOccsRn names = addOccurrenceNames names
listType_RDR = qual (modAndOcc listType_name)
tupleType_RDR n = qual (modAndOcc (tupleType_name n))
=============== NameEnv ================
\begin{code}
plusNameEnvRn :: NameEnv -> NameEnv -> RnM s d NameEnv
-plusNameEnvRn n1 n2
- = mapRn (addErrRn.nameClashErr) (conflictsFM (/=) n1 n2) `thenRn_`
- returnRn (n1 `plusFM` n2)
-
-addOneToNameEnvRn :: NameEnv -> RdrName -> Name -> RnM s d NameEnv
-addOneToNameEnvRn env rdr_name name
- = mapRn (addErrRn.nameClashErr) (conflictFM (/=) env rdr_name name) `thenRn_`
- returnRn (addToFM env rdr_name name)
+plusNameEnvRn env1 env2
+ = mapRn (addErrRn.nameClashErr) (conflictsFM conflicting_name env1 env2) `thenRn_`
+ returnRn (env1 `plusFM` env2)
+
+addOneToNameEnv :: NameEnv -> RdrName -> Name -> RnM s d NameEnv
+addOneToNameEnv env rdr_name name
+ = case lookupFM env rdr_name of
+ Just name2 | conflicting_name name name2
+ -> addErrRn (nameClashErr (rdr_name, (name, name2))) `thenRn_`
+ returnRn env
+
+ Nothing -> returnRn (addToFM env rdr_name name)
+
+conflicting_name n1 n2 = (n1 /= n2) || (isLocallyDefinedName n1 && isLocallyDefinedName n2)
+ -- We complain of a conflict if one RdrName maps to two different Names,
+ -- OR if one RdrName maps to the same *locally-defined* Name. The latter
+ -- case is to catch two separate, local definitions of the same thing.
+ --
+ -- If a module imports itself then there might be a local defn and an imported
+ -- defn of the same name; in this case the names will compare as equal, but
+ -- will still have different provenances.
lookupNameEnv :: NameEnv -> RdrName -> Maybe Name
lookupNameEnv = lookupFM
+
+delOneFromNameEnv :: NameEnv -> RdrName -> NameEnv
+delOneFromNameEnv env rdr_name = delFromFM env rdr_name
\end{code}
=============== FixityEnv ================
= mapRn (addErrRn.fixityClashErr) (conflictsFM bad_fix f1 f2) `thenRn_`
returnRn (f1 `plusFM` f2)
-addOneToFixityEnvRn env rdr_name fixity
- = mapRn (addErrRn.fixityClashErr) (conflictFM bad_fix env rdr_name fixity) `thenRn_`
- returnRn (addToFM env rdr_name fixity)
+addOneToFixityEnv env rdr_name fixity = addToFM env rdr_name fixity
lookupFixityEnv env rdr_name
= case lookupFM env rdr_name of
bad_fix :: (Fixity, Provenance) -> (Fixity, Provenance) -> Bool
bad_fix (f1,_) (f2,_) = f1 /= f2
-pprFixityProvenance :: PprStyle -> (Fixity,Provenance) -> Pretty
+pprFixityProvenance :: PprStyle -> (Fixity,Provenance) -> Doc
pprFixityProvenance sty (fixity, prov) = pprProvenance sty prov
\end{code}
=============== Avails ================
\begin{code}
-emptyModuleAvails :: ModuleAvails
-plusModuleAvails :: ModuleAvails -> ModuleAvails -> ModuleAvails
-lookupModuleAvails :: ModuleAvails -> Module -> Maybe [AvailInfo]
+mkExportAvails :: Bool -> Module -> [AvailInfo] -> ExportAvails
+mkExportAvails unqualified_import mod_name avails
+ = (mod_avail_env, entity_avail_env)
+ where
+ -- The "module M" syntax only applies to *unqualified* imports (1.4 Report, Section 5.1.1)
+ mod_avail_env | unqualified_import = unitFM mod_name avails
+ | otherwise = emptyFM
+
+ entity_avail_env = listToUFM [ (name,avail) | avail <- avails,
+ name <- availEntityNames avail]
-emptyModuleAvails = emptyFM
-plusModuleAvails = plusFM_C (++)
-lookupModuleAvails = lookupFM
+plusExportAvails :: ExportAvails -> ExportAvails -> ExportAvails
+plusExportAvails (m1, e1) (m2, e2)
+ = (plusFM_C (++) m1 m2, plusUFM_C plusAvail e1 e2)
\end{code}
plusAvail (AvailTC n1 ns1) (AvailTC n2 ns2) = AvailTC n1 (nub (ns1 ++ ns2))
plusAvail a NotAvailable = a
plusAvail NotAvailable a = a
+-- Added SOF 4/97
+#ifdef DEBUG
+plusAvail a1 a2 = panic ("RnEnv.plusAvail " ++ (show (hsep [pprAvail PprDebug a1,pprAvail PprDebug a2])))
+#endif
addAvailToNameSet :: NameSet -> AvailInfo -> NameSet
addAvailToNameSet names avail = addListToNameSet names (availNames avail)
filterAvail ie@(IEThingWith want wants) avail@(AvailTC n ns)
| sub_names_ok = AvailTC n (filter is_wanted ns)
- | otherwise = pprTrace "filterAvail" (ppCat [ppr PprDebug ie, pprAvail PprDebug avail]) $
+ | otherwise = pprTrace "filterAvail" (hsep [ppr PprDebug ie, pprAvail PprDebug avail]) $
NotAvailable
where
is_wanted name = nameOccName name `elem` wanted_occs
filterAvail ie avail = NotAvailable
-hideAvail :: RdrNameIE -- Hide this
- -> AvailInfo -- Available
- -> AvailInfo -- Resulting available;
--- Don't complain about hiding non-existent things; that's done elsewhere
-
-hideAvail ie NotAvailable
- = NotAvailable
-
-hideAvail ie (Avail n)
- | not (ieOcc ie == nameOccName n) = Avail n -- No match
- | otherwise = NotAvailable -- Names match
-
-hideAvail ie (AvailTC n ns)
- | not (ieOcc ie == nameOccName n) -- No match
- = case ie of -- But in case we are faced with ...hiding( (+) )
- -- we filter the "ns" anyhow
- IEVar op -> AvailTC n (filter keep ns)
- where
- op_occ = rdrNameOcc op
- keep n = nameOccName n /= op_occ
-
- other -> AvailTC n ns
-
- | otherwise -- Names match
- = case ie of
- IEThingAbs _ -> AvailTC n (filter (/= n) ns)
- IEThingAll _ -> NotAvailable
- IEThingWith hide hides -> AvailTC n (filter keep ns)
- where
- keep n = nameOccName n `notElem` hide_occs
- hide_occs = map rdrNameOcc (hide : hides)
-
-
--- pprAvail gets given the OccName of the "host" thing
-pprAvail sty NotAvailable = ppPStr SLIT("NotAvailable")
-pprAvail sty (AvailTC n ns) = ppCat [pprOccName sty (nameOccName n),
- ppChar '(',
- ppInterleave ppComma (map (pprOccName sty.nameOccName) ns),
- ppChar ')']
-pprAvail sty (Avail n) = pprOccName sty (nameOccName n)
+-- In interfaces, pprAvail gets given the OccName of the "host" thing
+pprAvail PprInterface avail = ppr_avail (pprOccName PprInterface . nameOccName) avail
+pprAvail sty avail = ppr_avail (ppr sty) avail
+
+ppr_avail pp_name NotAvailable = ptext SLIT("NotAvailable")
+ppr_avail pp_name (AvailTC n ns) = hsep [
+ pp_name n,
+ parens $ hsep $ punctuate comma $
+ map pp_name ns
+ ]
+ppr_avail pp_name (Avail n) = pp_name n
\end{code}
\begin{code}
nameClashErr (rdr_name, (name1,name2)) sty
- = ppHang (ppCat [ppPStr SLIT("Conflicting definitions for: "), ppr sty rdr_name])
- 4 (ppAboves [pprNameProvenance sty name1,
- pprNameProvenance sty name2])
+ = hang (hsep [ptext SLIT("Conflicting definitions for:"), ppr sty rdr_name])
+ 4 (vcat [pprNameProvenance sty name1,
+ pprNameProvenance sty name2])
fixityClashErr (rdr_name, (fp1,fp2)) sty
- = ppHang (ppCat [ppPStr SLIT("Conflicting fixities for: "), ppr sty rdr_name])
- 4 (ppAboves [pprFixityProvenance sty fp1,
- pprFixityProvenance sty fp2])
+ = hang (hsep [ptext SLIT("Conflicting fixities for:"), ppr sty rdr_name])
+ 4 (vcat [pprFixityProvenance sty fp1,
+ pprFixityProvenance sty fp2])
shadowedNameWarn shadow sty
- = ppBesides [ppPStr SLIT("This binding for"),
- ppQuote (ppr sty shadow),
- ppPStr SLIT("shadows an existing binding")]
+ = hcat [ptext SLIT("This binding for"),
+ ppr sty shadow,
+ ptext SLIT("shadows an existing binding")]
unknownNameErr name sty
- = ppSep [ppStr flavour, ppPStr SLIT("not in scope:"), ppr sty name]
+ = sep [text flavour, ptext SLIT("not in scope:"), ppr sty name]
where
flavour = occNameFlavour (rdrNameOcc name)
qualNameErr descriptor (name,loc)
= pushSrcLocRn loc $
- addErrRn (\sty -> ppBesides [ppPStr SLIT("invalid use of qualified "),
- ppStr descriptor, ppPStr SLIT(": "),
- pprNonSymOcc sty (rdrNameOcc name) ])
+ addErrRn (\sty -> hsep [ ptext SLIT("Invalid use of qualified name"),
+ ppr sty name,
+ ptext SLIT("in"),
+ descriptor sty])
dupNamesErr descriptor ((name,loc) : dup_things)
= pushSrcLocRn loc $
- addErrRn (\sty -> ppBesides [ppPStr SLIT("duplicate bindings of `"),
- ppr sty name, ppPStr SLIT("' in "),
- ppStr descriptor])
+ addErrRn (\sty -> hsep [ptext SLIT("Conflicting definitions for"),
+ ppr sty name,
+ ptext SLIT("in"), descriptor sty])
\end{code}