[project @ 2000-05-09 13:15:37 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnEnv.lhs
index 561fbdb..118267f 100644 (file)
@@ -61,7 +61,7 @@ newLocalTopBinder :: Module -> OccName
               -> (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.
        --
@@ -72,12 +72,13 @@ newLocalTopBinder mod occ rec_exp_fn loc
 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 
@@ -85,10 +86,9 @@ newTopBinder mod occ mk_prov
     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
@@ -99,8 +99,7 @@ newTopBinder mod occ mk_prov
        -- So for them we just set the provenance
 
        Just name -> let 
-                       new_name  | isWiredInName name = setNameProvenance name (mk_prov name)
-                                 | otherwise          = mkGlobalName (nameUnique name) mod occ (mk_prov name)
+                       new_name  = set_prov (setNameModule name mod)
                        new_cache = addToFM cache key new_name
                     in
                     setNameSupplyRn (us, inst_ns, new_cache, ipcache)  `thenRn_`
@@ -111,7 +110,7 @@ newTopBinder mod occ mk_prov
        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_`
@@ -150,15 +149,33 @@ mkImportedGlobalName mod_name occ
                     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