module RnEnv where -- Export everything
+IMPORT_1_3(List (nub))
IMP_Ubiq()
-import CmdLineOpts ( opt_WarnNameShadowing, opt_IgnoreIfacePragmas )
+import CmdLineOpts ( opt_WarnNameShadowing )
import HsSyn
import RdrHsSyn ( RdrName(..), SYN_IE(RdrNameIE),
- rdrNameOcc, isQual, qual
+ 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,
- isLocalName, isWiredInName, nameOccName, setNameProvenance,
- pprProvenance, pprOccName, pprModule, pprNonSymOcc, pprNameProvenance
+ mkLocalName, mkGlobalName, modAndOcc, isLocallyDefinedName,
+ 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}
newGlobalName mod occ
= -- First check the cache
getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
- case lookupFM cache (mod,occ) of
+ let key = (mod,occ) in
+ case lookupFM cache key of
-- A hit in the cache! Return it, but change the src loc
-- of the thing we've found if this is a second definition site
(us', us1) = splitUniqSupply us
uniq = getUnique us1
name = mkGlobalName uniq mod occ VanillaDefn Implicit
- cache' = addToFM cache (mod,occ) name
+ cache' = addToFM cache key name
in
setNameSupplyRn (us', inst_ns, cache') `thenRn_`
returnRn name
-- 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
uniq = getUnique us1
- new_name = case lookupFM cache (mod,occ) of
- Just name -> setNameProvenance name provenance
- Nothing -> mkGlobalName uniq mod occ VanillaDefn provenance
- cache' = addToFM cache (mod,occ) new_name
+ key = (mod,occ)
+ new_name = case lookupFM cache key of
+ Just name -> setNameProvenance name provenance
+ other -> mkGlobalName uniq mod occ VanillaDefn provenance
+ new_cache = addToFM cache key new_name
in
- setNameSupplyRn (us', inst_ns, cache') `thenRn_`
+ setNameSupplyRn (us', inst_ns, new_cache) `thenRn_`
returnRn new_name
--- newDfunName is used to allocate a name for the dictionary function for
--- a local instance declaration. No need to put it in the cache (I think!).
-newDfunName :: SrcLoc -> RnMS s Name
-newDfunName src_loc
- = getNameSupplyRn `thenRn` \ (us, inst_ns, cache) ->
- getModuleRn `thenRn` \ mod_name ->
+-- newSysName is used to create the names for
+-- a) default methods
+-- These are never mentioned explicitly in source code (hence no point in looking
+-- them up in the NameEnv), but when reading an interface file
+-- we may want to slurp in their pragma info. In the source file itself we
+-- need to create these names too so that we export them into the inferface file for this module.
+
+newSysName :: OccName -> ExportFlag -> SrcLoc -> RnMS s Name
+newSysName occ export_flag loc
+ = getModeRn `thenRn` \ mode ->
+ getModuleRn `thenRn` \ mod_name ->
+ case mode of
+ SourceMode -> newLocallyDefinedGlobalName
+ mod_name occ
+ (\_ -> export_flag)
+ loc
+ 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
+-- over original names), but we still want to make the Dfun locally-defined.
+-- So we can't use whether or not we're in source mode to decide the locally-defined question.
+newDfunName :: Maybe RdrName -> SrcLoc -> RnMS s Name
+newDfunName Nothing src_loc -- Local instance decls have a "Nothing"
+ = getModuleRn `thenRn` \ mod_name ->
+ newInstUniq `thenRn` \ inst_uniq ->
let
- (us', us1) = splitUniqSupply us
- uniq = getUnique us1
- dfun_name = mkGlobalName uniq mod_name (VarOcc (_PK_ ("df" ++ show inst_ns)))
- VanillaDefn (LocalDef Exported src_loc)
- in
- setNameSupplyRn (us', inst_ns+1, cache) `thenRn_`
- returnRn dfun_name
+ dfun_occ = VarOcc (_PK_ ("$d" ++ show inst_uniq))
+ in
+ newLocallyDefinedGlobalName mod_name dfun_occ
+ (\_ -> Exported) src_loc
+
+newDfunName (Just n) src_loc -- Imported ones have "Just n"
+ = getModuleRn `thenRn` \ mod_name ->
+ newGlobalName mod_name (rdrNameOcc n)
newLocalNames :: [(RdrName,SrcLoc)] -> RnM s d [Name]
\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}
Looking up a name in the RnEnv.
\begin{code}
-lookupRn :: RdrName -> RnMS s Name
-lookupRn rdr_name
- = getNameEnv `thenRn` \ name_env ->
- case lookupFM name_env rdr_name of
+lookupRn :: NameEnv -> RdrName -> RnMS s Name
+lookupRn name_env rdr_name
+ = case lookupFM name_env rdr_name of
-- Found it!
Just name -> returnRn name
-- 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
newGlobalName mod_name occ
+lookupBndrRn rdr_name
+ = getNameEnv `thenRn` \ name_env ->
+ lookupRn name_env rdr_name
+
-- Just like lookupRn except that we record the occurrence too
-- Perhaps surprisingly, even wired-in names are recorded.
-- Why? So that we know which wired-in names are referred to when
-- deciding which instance declarations to import.
lookupOccRn :: RdrName -> RnMS s Name
lookupOccRn rdr_name
- = lookupRn rdr_name `thenRn` \ name ->
- if isLocalName name then
- returnRn name
- else
- addOccurrenceName Compulsory name `thenRn_`
- returnRn 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
- = lookupRn rdr_name `thenRn` \ name ->
- if opt_IgnoreIfacePragmas || isLocalName name then
- -- Never look for optional things if we're
- -- ignoring optional input interface information
- returnRn name
- else
- addOccurrenceName Optional name `thenRn_`
- returnRn name
+ = getNameEnv `thenRn` \ name_env ->
+ lookupRn name_env rdr_name `thenRn` \ name ->
+ addOccurrenceName name
+
+-- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
+-- environment. It's used for record field names only.
+lookupGlobalOccRn :: RdrName -> RnMS s Name
+lookupGlobalOccRn rdr_name
+ = getGlobalNameEnv `thenRn` \ name_env ->
+ lookupRn name_env rdr_name `thenRn` \ name ->
+ addOccurrenceName name
+
+
-- lookupImplicitOccRn takes an RdrName representing an *original* name, and
-- adds it to the occurrence pool so that it'll be loaded later. This is
-- This doesn't apply in interface mode, where everything is explicit, but
-- we don't check for this case: it does no harm to record an "extra" occurrence
-- and lookupImplicitOccRn isn't used much in interface mode (it's only the
--- Nothing clause of rnDerivs that calls it at all I think.
+-- Nothing clause of rnDerivs that calls it at all I think).
--
-- For List and Tuple types it's important to get the correct
-- isLocallyDefined flag, which is used in turn when deciding
lookupImplicitOccRn :: RdrName -> RnMS s Name
lookupImplicitOccRn (Qual mod occ)
= newGlobalName mod occ `thenRn` \ name ->
- addOccurrenceName Compulsory name `thenRn_`
- returnRn name
+ addOccurrenceName name
-addImplicitOccRn :: Name -> RnM s d ()
-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]
-
-emptyModuleAvails = emptyFM
-plusModuleAvails = plusFM_C (++)
-lookupModuleAvails = lookupFM
+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]
+
+plusExportAvails :: ExportAvails -> ExportAvails -> ExportAvails
+plusExportAvails (m1, e1) (m2, e2)
+ = (plusFM_C (++) m1 m2, plusUFM_C plusAvail e1 e2)
\end{code}
=============== AvailInfo ================
\begin{code}
-plusAvail (Avail n1 ns1) (Avail n2 ns2) = Avail n1 (nub (ns1 ++ ns2))
+plusAvail (Avail n1) (Avail n2) = Avail n1
+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 NotAvailable = names
-addAvailToNameSet names (Avail n ns) = addListToNameSet names (n:ns)
+addAvailToNameSet names avail = addListToNameSet names (availNames avail)
availsToNameSet :: [AvailInfo] -> NameSet
availsToNameSet avails = foldl addAvailToNameSet emptyNameSet avails
+availName :: AvailInfo -> Name
+availName (Avail n) = n
+availName (AvailTC n _) = n
+
availNames :: AvailInfo -> [Name]
-availNames NotAvailable = []
-availNames (Avail n ns) = n:ns
-
-filterAvail :: RdrNameIE -> AvailInfo -> AvailInfo
-filterAvail (IEThingWith _ wanted) NotAvailable = NotAvailable
-filterAvail (IEThingWith _ wanted) (Avail n ns)
- | sub_names_ok = Avail n (filter is_wanted ns)
- | otherwise = NotAvailable
+availNames NotAvailable = []
+availNames (Avail n) = [n]
+availNames (AvailTC n ns) = ns
+
+-- availEntityNames is used to extract the names that can appear on their own in
+-- an export or import list. For class decls, class methods can appear on their
+-- own, thus import A( op )
+-- but constructors cannot; thus
+-- import B( T )
+-- means import type T from B, not constructor T.
+
+availEntityNames :: AvailInfo -> [Name]
+availEntityNames NotAvailable = []
+availEntityNames (Avail n) = [n]
+availEntityNames (AvailTC n ns) = n : filter (isVarOcc . nameOccName) ns
+
+filterAvail :: RdrNameIE -- Wanted
+ -> AvailInfo -- Available
+ -> AvailInfo -- Resulting available;
+ -- NotAvailable if wanted stuff isn't there
+
+filterAvail ie@(IEThingWith want wants) avail@(AvailTC n ns)
+ | sub_names_ok = AvailTC n (filter is_wanted ns)
+ | otherwise = pprTrace "filterAvail" (hsep [ppr PprDebug ie, pprAvail PprDebug avail]) $
+ NotAvailable
where
is_wanted name = nameOccName name `elem` wanted_occs
sub_names_ok = all (`elem` avail_occs) wanted_occs
- wanted_occs = map rdrNameOcc wanted
avail_occs = map nameOccName ns
+ wanted_occs = map rdrNameOcc (want:wants)
+
+filterAvail (IEThingAbs _) (AvailTC n ns)
+ | n `elem` ns = AvailTC n [n]
+
+filterAvail (IEThingAbs _) avail@(Avail n) = avail -- Type synonyms
+
+filterAvail (IEVar _) avail@(Avail n) = avail
+filterAvail (IEVar v) avail@(AvailTC n ns) = AvailTC n (filter wanted ns)
+ where
+ wanted n = nameOccName n == occ
+ occ = rdrNameOcc v
+ -- The second equation happens if we import a class op, thus
+ -- import A( op )
+ -- where op is a class operation
+
+filterAvail (IEThingAll _) avail@(AvailTC _ _) = avail
+
+filterAvail ie avail = NotAvailable
-filterAvail (IEThingAll _) avail = avail
-filterAvail ie (Avail n ns) = Avail n [] -- IEThingAbs and IEVar
+-- 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
--- pprAvail gets given the OccName of the "host" thing
-pprAvail sty NotAvailable = ppStr "NotAvailable"
-pprAvail sty (Avail n ns) = ppCat [pprOccName sty (nameOccName n),
- ppStr "(",
- ppInterleave ppComma (map (pprOccName sty.nameOccName) ns),
- ppStr ")"]
+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 [ppStr "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 [ppStr "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 [ppStr "More than one value with the same name (shadowing): ", ppr sty shadow]
+ = hcat [ptext SLIT("This binding for"),
+ ppr sty shadow,
+ ptext SLIT("shadows an existing binding")]
unknownNameErr name sty
- = ppSep [ppStr flavour, ppStr "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 [ppStr "invalid use of qualified ",
- ppStr descriptor, ppStr ": ",
- 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 [ppStr "duplicate bindings of `",
- ppr sty name, ppStr "' in ",
- ppStr descriptor])
+ addErrRn (\sty -> hsep [ptext SLIT("Conflicting definitions for"),
+ ppr sty name,
+ ptext SLIT("in"), descriptor sty])
\end{code}