[project @ 1997-07-31 00:05:10 by sof]
[ghc-hetmet.git] / ghc / compiler / rename / RnEnv.lhs
index b734653..a2534f3 100644 (file)
@@ -8,6 +8,7 @@
 
 module RnEnv where             -- Export everything
 
+IMPORT_1_3(List (nub))
 IMP_Ubiq()
 
 import CmdLineOpts     ( opt_WarnNameShadowing )
@@ -16,9 +17,9 @@ import RdrHsSyn               ( RdrName(..), SYN_IE(RdrNameIE),
                          rdrNameOcc, ieOcc, isQual, qual
                        )
 import HsTypes         ( getTyVarName, replaceTyVarName )
-import BasicTypes      ( Fixity(..), FixityDirection(..) )
+import BasicTypes      ( Fixity(..), FixityDirection(..), IfaceFlavour(..), pprModule )
 import RnMonad
-import Name            ( Name, OccName(..), Provenance(..), DefnInfo(..), ExportFlag(..), NamedThing(..),
+import Name            ( Name, OccName(..), Provenance(..), ExportFlag(..), NamedThing(..),
                          occNameString, occNameFlavour,
                          SYN_IE(NameSet), emptyNameSet, addListToNameSet,
                          mkLocalName, mkGlobalName, modAndOcc, isLocallyDefinedName,
@@ -28,18 +29,15 @@ import Name         ( Name, OccName(..), Provenance(..), DefnInfo(..), ExportFlag(..),
 import TyCon           ( TyCon )
 import TysWiredIn      ( tupleTyCon, listTyCon, charTyCon, intTyCon )
 import FiniteMap
-import Outputable
-import Unique          ( Unique, unboundKey )
-import UniqFM           ( Uniquable(..) )
+import Unique          ( Unique, Uniquable(..), unboundKey )
+import UniqFM           ( listToUFM, plusUFM_C )
 import Maybes          ( maybeToBool )
 import UniqSupply
 import SrcLoc          ( SrcLoc, noSrcLoc )
 import Pretty
-import Outputable      ( PprStyle(..) )
-import Util            --( panic, removeDups, pprTrace, assertPanic )
-#if __GLASGOW_HASKELL__ >= 202
-import List (nub)
-#endif
+import Outputable      ( Outputable(..), PprStyle(..) )
+import Util            ( Ord3(..), panic, removeDups, pprTrace, assertPanic )
+
 \end{code}
 
 
@@ -51,8 +49,8 @@ import List (nub)
 %*********************************************************
 
 \begin{code}
-newGlobalName :: Module -> OccName -> RnM s d Name
-newGlobalName mod occ
+newGlobalName :: Module -> OccName -> IfaceFlavour -> RnM s d Name
+newGlobalName mod occ iface_flavour
   =    -- First check the cache
     getNameSupplyRn            `thenRn` \ (us, inst_ns, cache) ->
     let key = (mod,occ)         in
@@ -64,12 +62,12 @@ newGlobalName mod occ
        Just name ->  returnRn name
 
        -- Miss in the cache, so build a new original name,
-       -- and put it in the cache
+       -- And put it in the cache
        Nothing        -> 
            let
                (us', us1) = splitUniqSupply us
                uniq       = getUnique us1
-               name       = mkGlobalName uniq mod occ VanillaDefn Implicit
+               name       = mkGlobalName uniq mod occ (Implicit iface_flavour)
                cache'     = addToFM cache key name
            in
            setNameSupplyRn (us', inst_ns, cache')              `thenRn_`
@@ -89,49 +87,34 @@ newLocallyDefinedGlobalName mod occ rec_exp_fn loc
        -- 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!
        --
-       -- 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
+       -- 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
         key        = (mod,occ)
        new_name   = case lookupFM cache key of
-                        Just name | is_implicit_prov
-                                  -> setNameProvenance name provenance
-                                  where
-                                     is_implicit_prov = case getNameProvenance name of
-                                                           Implicit -> True
-                                                           other    -> False
-                        other   -> mkGlobalName uniq mod occ VanillaDefn provenance
-
+                        Just name -> setNameProvenance name provenance
+                        other     -> mkGlobalName uniq mod occ provenance
        new_cache  = addToFM cache key new_name
     in
     setNameSupplyRn (us', inst_ns, new_cache)          `thenRn_`
     returnRn new_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.
@@ -148,7 +131,7 @@ newDfunName Nothing src_loc                 -- Local instance decls have a "Nothing"
 
 newDfunName (Just n) src_loc                   -- Imported ones have "Just n"
   = getModuleRn                `thenRn` \ mod_name ->
-    newGlobalName mod_name (rdrNameOcc n)
+    newGlobalName mod_name (rdrNameOcc n) HiFile {- Correct? -} 
 
 
 newLocalNames :: [(RdrName,SrcLoc)] -> RnM s d [Name]
@@ -234,6 +217,13 @@ checkDupNames doc_str rdr_names_w_loc
     returnRn ()
   where
     (_, dups) = removeDups (\(n1,l1) (n2,l2) -> n1 `cmp` n2) rdr_names_w_loc
+
+
+-- Yuk!
+ifaceFlavour name = case getNameProvenance name of
+                       Imported _ _ hif -> hif
+                       Implicit hif     -> hif
+                       other            -> HiFile      -- Shouldn't happen
 \end{code}
 
 
@@ -265,13 +255,13 @@ lookupRn name_env rdr_name
                        InterfaceMode _ -> 
                            case rdr_name of
 
-                               Qual mod_name occ -> newGlobalName mod_name occ
+                               Qual mod_name occ hif -> newGlobalName mod_name occ hif
 
                                -- An Unqual is allowed; interface files contain 
                                -- unqualified names for locally-defined things, such as
                                -- constructors of a data type.
                                Unqual occ -> getModuleRn       `thenRn ` \ mod_name ->
-                                             newGlobalName mod_name occ
+                                             newGlobalName mod_name occ HiFile
 
 
 lookupBndrRn rdr_name
@@ -315,8 +305,8 @@ lookupGlobalOccRn rdr_name
 -- The name cache should have the correct provenance, though.
 
 lookupImplicitOccRn :: RdrName -> RnMS s Name 
-lookupImplicitOccRn (Qual mod occ)
- = newGlobalName mod occ               `thenRn` \ name ->
+lookupImplicitOccRn (Qual mod occ hif)
+ = newGlobalName mod occ hif           `thenRn` \ name ->
    addOccurrenceName name
 
 addImplicitOccRn :: Name -> RnMS s Name
@@ -359,12 +349,28 @@ plusRnEnv (RnEnv n1 f1) (RnEnv n2 f2)
 ===============  NameEnv  ================
 \begin{code}
 plusNameEnvRn :: NameEnv -> NameEnv -> RnM s d NameEnv
-plusNameEnvRn n1 n2
-  = mapRn (addErrRn.nameClashErr) (conflictsFM (/=) n1 n2)             `thenRn_`
-    returnRn (n1 `plusFM` n2)
-
-addOneToNameEnv :: NameEnv -> RdrName -> Name -> NameEnv
-addOneToNameEnv env rdr_name name = 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
+
+       other      -> 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
@@ -397,13 +403,20 @@ pprFixityProvenance sty (fixity, prov) = pprProvenance sty prov
 
 ===============  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}
 
 
@@ -535,12 +548,12 @@ conflictFM bad fm key elt
 nameClashErr (rdr_name, (name1,name2)) sty
   = hang (hsep [ptext SLIT("Conflicting definitions for:"), ppr sty rdr_name])
        4 (vcat [pprNameProvenance sty name1,
-                    pprNameProvenance sty name2])
+                pprNameProvenance sty name2])
 
 fixityClashErr (rdr_name, (fp1,fp2)) sty
   = hang (hsep [ptext SLIT("Conflicting fixities for:"), ppr sty rdr_name])
        4 (vcat [pprFixityProvenance sty fp1,
-                    pprFixityProvenance sty fp2])
+                pprFixityProvenance sty fp2])
 
 shadowedNameWarn shadow sty
   = hcat [ptext SLIT("This binding for"),