[project @ 1997-06-18 23:52:36 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnEnv.lhs
index 9f81643..d926583 100644 (file)
@@ -8,6 +8,7 @@
 
 module RnEnv where             -- Export everything
 
+IMPORT_1_3(List (nub))
 IMP_Ubiq()
 
 import CmdLineOpts     ( opt_WarnNameShadowing )
@@ -16,29 +17,28 @@ import RdrHsSyn             ( RdrName(..), SYN_IE(RdrNameIE),
                          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, getNameProvenance,
-                         pprProvenance, pprOccName, pprModule, pprNonSymOcc, pprNameProvenance
+                         pprProvenance, pprOccName, pprModule, pprNameProvenance
                        )
 import TyCon           ( TyCon )
 import TysWiredIn      ( tupleTyCon, listTyCon, charTyCon, intTyCon )
 import FiniteMap
 import Outputable
 import Unique          ( Unique, unboundKey )
-import UniqFM           ( Uniquable(..) )
+import UniqFM           ( Uniquable(..), listToUFM, plusUFM_C )
 import Maybes          ( maybeToBool )
 import UniqSupply
 import SrcLoc          ( SrcLoc, noSrcLoc )
 import Pretty
-import PprStyle                ( PprStyle(..) )
+import Outputable      ( PprStyle(..) )
 import Util            --( panic, removeDups, pprTrace, assertPanic )
-#if __GLASGOW_HASKELL__ >= 202
-import List (nub)
-#endif
+
 \end{code}
 
 
@@ -88,26 +88,29 @@ 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 VanillaDefn provenance
        new_cache  = addToFM cache key new_name
     in
     setNameSupplyRn (us', inst_ns, new_cache)          `thenRn_`
@@ -129,7 +132,7 @@ newSysName occ export_flag loc
                                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
@@ -261,7 +264,7 @@ lookupRn name_env rdr_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
@@ -285,7 +288,7 @@ lookupOccRn :: RdrName -> RnMS s Name
 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.
@@ -293,15 +296,7 @@ lookupGlobalOccRn :: RdrName -> RnMS s Name
 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
 
    
 
@@ -324,13 +319,13 @@ lookupOptionalOccRn rdr_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))
@@ -366,12 +361,27 @@ 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
+
+       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
@@ -404,13 +414,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}
 
 
@@ -485,39 +502,6 @@ filterAvail (IEThingAll _) avail@(AvailTC _ _)  = avail
 
 filterAvail ie avail = NotAvailable 
 
-{-     OLD     to be deleted
-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)
--}
 
 -- In interfaces, pprAvail gets given the OccName of the "host" thing
 pprAvail PprInterface avail = ppr_avail (pprOccName PprInterface . nameOccName) avail
@@ -573,14 +557,14 @@ conflictFM bad fm key elt
 
 \begin{code}
 nameClashErr (rdr_name, (name1,name2)) sty
-  = hang (hsep [ptext SLIT("Conflicting definitions for: "), ppr sty rdr_name])
+  = 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])
+  = 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"), 
@@ -594,14 +578,14 @@ unknownNameErr name sty
 
 qualNameErr descriptor (name,loc)
   = pushSrcLocRn loc $
-    addErrRn (\sty -> hsep [ ptext SLIT("invalid use of qualified 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 -> hsep [ptext SLIT("duplicate bindings of"), 
+    addErrRn (\sty -> hsep [ptext SLIT("Conflicting definitions for"), 
                            ppr sty name, 
                            ptext SLIT("in"), descriptor sty])
 \end{code}