import Name ( Name, Provenance(..), ExportFlag(..), NamedThing(..),
ImportReason(..), getSrcLoc,
mkLocalName, mkImportedLocalName, mkGlobalName, mkUnboundName,
- mkIPName, isSystemName,
+ mkIPName, isSystemName, isWiredInName,
nameOccName, setNameModule, nameModule,
pprOccName, isLocallyDefined, nameUnique, nameOccName,
occNameUserString,
-> (Name -> ExportFlag) -> SrcLoc
-> RnM d Name
newLocalTopBinder mod occ rec_exp_fn loc
- = newTopBinder mod occ (\name -> LocalDef loc (rec_exp_fn name))
+ = newTopBinder mod occ (\name -> setNameProvenance name (LocalDef loc (rec_exp_fn name)))
-- We must set the provenance of the thing in the cache
-- correctly, particularly whether or not it is locally defined.
--
newImportedBinder :: Module -> RdrName -> RnM d Name
newImportedBinder mod rdr_name
= ASSERT2( isUnqual rdr_name, ppr rdr_name )
- newTopBinder mod (rdrNameOcc rdr_name) (\name -> implicitImportProvenance)
+ newTopBinder mod (rdrNameOcc rdr_name) (\name -> name)
+ -- Provenance is already implicitImportProvenance
implicitImportProvenance = NonLocalDef ImplicitImport False
-newTopBinder :: Module -> OccName -> (Name -> Provenance) -> RnM d Name
-newTopBinder mod occ mk_prov
+newTopBinder :: Module -> OccName -> (Name -> Name) -> RnM d Name
+newTopBinder mod occ set_prov
= -- First check the cache
getNameSupplyRn `thenRn` \ (us, inst_ns, cache, ipcache) ->
let
in
case lookupFM cache key of
- -- A hit in the cache! Re-use the unique (which may be widely known)
- -- But otherwise build a new name, thereby
- -- overwriting whatever module details and provenance is in the cache already;
- -- This updates WiredIn things and known-key things, which are there from the start.
+ -- A hit in the cache!
+ -- Set the Module of the thing, and set its provenance (hack pending
+ -- spj update)
--
-- It also means that if there are two defns for the same thing
-- in a module, then each gets a separate SrcLoc
+ --
+ -- There's a complication for wired-in names. We don't want to
+ -- forget that they are wired in even when compiling that module
+ -- (else we spit out redundant defns into the interface file)
+ -- So for them we just set the provenance
Just name -> let
- new_name = mkGlobalName (nameUnique name) mod occ (mk_prov new_name)
+ new_name = set_prov (setNameModule name mod)
new_cache = addToFM cache key new_name
in
setNameSupplyRn (us, inst_ns, new_cache, ipcache) `thenRn_`
Nothing -> let
(us', us1) = splitUniqSupply us
uniq = uniqFromSupply us1
- new_name = mkGlobalName uniq mod occ (mk_prov new_name)
+ new_name = set_prov (mkGlobalName uniq mod occ implicitImportProvenance)
new_cache = addToFM cache key new_name
in
setNameSupplyRn (us', inst_ns, new_cache, ipcache) `thenRn_`
new_cache = addToFM cache key name
updateProvenances :: [Name] -> RnM d ()
+-- Update the provenances of everything that is in scope.
+-- We must be careful not to disturb the Module package info
+-- already in the cache. Why not? Consider
+-- module A module M( f )
+-- import M( f ) import N( f)
+-- import N
+-- So f is defined in N, and M re-exports it.
+-- When processing module A:
+-- 1. We read M.hi first, and make a vanilla name N.f
+-- (without reading N.hi). The package info says <THIS>
+-- for lack of anything better.
+-- 2. Now we read N, which update the cache to record
+-- the correct package for N.f.
+-- 3. Finally we update provenances (once we've read all imports).
+-- Step 3 must not destroy package info recorded in Step 2.
+
updateProvenances names
= getNameSupplyRn `thenRn` \ (us, inst_ns, cache, ipcache) ->
- setNameSupplyRn (us, inst_ns, update cache names, ipcache)
+ setNameSupplyRn (us, inst_ns, foldr update cache names, ipcache)
where
- update cache [] = cache
- update cache (name:names) = WARN( not (key `elemFM` cache), ppr name )
- update (addToFM cache key name) names
- where
- key = (moduleName (nameModule name), nameOccName name)
+ update name cache = addToFM_C update_prov cache key name
+ where
+ key = (moduleName (nameModule name), nameOccName name)
+
+ update_prov name_in_cache name_with_prov
+ = setNameProvenance name_in_cache (getNameProvenance name_with_prov)
+
mkImportedGlobalFromRdrName :: RdrName -> RnM d Name