[project @ 2000-10-31 08:08:38 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnEnv.lhs
index d4ff303..97f505e 100644 (file)
@@ -11,7 +11,7 @@ module RnEnv where            -- Export everything
 import HsSyn
 import RdrHsSyn                ( RdrNameIE )
 import RdrName         ( RdrName, rdrNameModule, rdrNameOcc, isQual, isUnqual,
-                         mkRdrUnqual, qualifyRdrName
+                         mkRdrUnqual, qualifyRdrName, lookupRdrEnv
                        )
 import HsTypes         ( hsTyVarName, replaceTyVarName )
 import HscTypes                ( Provenance(..), pprNameProvenance, hasBetterProv,
@@ -21,13 +21,13 @@ import RnMonad
 import Name            ( Name, NamedThing(..),
                          getSrcLoc, 
                          mkLocalName, mkImportedLocalName, mkGlobalName,
-                         mkIPName, nameOccName, nameModule,
+                         mkIPName, nameOccName, nameModule_maybe,
                          extendNameEnv_C, plusNameEnv_C, nameEnvElts,
                          setNameModuleAndLoc
                        )
 import NameSet
 import OccName         ( OccName, occNameUserString, occNameFlavour )
-import Module          ( ModuleName, moduleName, mkVanillaModule )
+import Module          ( ModuleName, moduleName, mkVanillaModule, mkSysModuleNameFS, moduleNameFS )
 import FiniteMap
 import Unique          ( Unique )
 import UniqSupply
@@ -38,6 +38,7 @@ import Util           ( sortLt )
 import List            ( nub )
 import PrelNames       ( mkUnboundName )
 import CmdLineOpts
+import FastString      ( FastString )
 \end{code}
 
 %*********************************************************
@@ -48,10 +49,25 @@ import CmdLineOpts
 
 \begin{code}
 newTopBinder :: Module -> RdrName -> SrcLoc -> RnM d Name
+       -- newTopBinder puts into the cache the binder with the
+       -- module information set correctly.  When the decl is later renamed,
+       -- the binding site will thereby get the correct module.
+       -- There maybe occurrences that don't have the correct Module, but
+       -- by the typechecker will propagate the binding definition to all 
+       -- the occurrences, so that doesn't matter
+
 newTopBinder mod rdr_name loc
   =    -- First check the cache
     traceRn (text "newTopBinder" <+> ppr mod <+> ppr loc) `thenRn_`
 
+       -- There should never be a qualified name in a binding position (except in instance decls)
+       -- The parser doesn't check this because the same parser parses instance decls
+    (if isQual rdr_name then
+       qualNameErr (text "its declaration") (rdr_name,loc)
+     else
+       returnRn ()
+    )                          `thenRn_`
+
     getNameSupplyRn            `thenRn` \ (us, cache, ipcache) ->
     let 
        occ = rdrNameOcc rdr_name
@@ -223,6 +239,15 @@ lookupGlobalOccRn rdr_name
                        failWithRn (mkUnboundName rdr_name)
                                   (unknownNameErr rdr_name)
     }
+
+lookupGlobalRn :: GlobalRdrEnv -> RdrName -> RnM d (Maybe Name)
+  -- Checks that there is exactly one
+lookupGlobalRn global_env rdr_name
+  = case lookupRdrEnv global_env rdr_name of
+       Just [(name,_)]         -> returnRn (Just name)
+       Just stuff@((name,_):_) -> addNameClashErrRn rdr_name stuff     `thenRn_`
+                                  returnRn (Just name)
+       Nothing                 -> returnRn Nothing
 \end{code}
 %
 
@@ -342,15 +367,14 @@ bindLocatedLocalsRn doc_str rdr_names_w_loc enclosed_scope
                Just name -> pushSrcLocRn loc $
                             addWarnRn (shadowedNameWarn rdr_name)
 
-bindCoreLocalFVRn :: RdrName -> (Name -> RnMS (a, FreeVars))
-                 -> RnMS (a, FreeVars)
+bindCoreLocalRn :: RdrName -> (Name -> RnMS a) -> RnMS a
   -- A specialised variant when renaming stuff from interface
   -- files (of which there is a lot)
   --   * one at a time
   --   * no checks for shadowing
   --   * always imported
   --   * deal with free vars
-bindCoreLocalFVRn rdr_name enclosed_scope
+bindCoreLocalRn rdr_name enclosed_scope
   = getSrcLocRn                `thenRn` \ loc ->
     getLocalNameEnv            `thenRn` \ name_env ->
     getNameSupplyRn            `thenRn` \ (us, cache, ipcache) ->
@@ -363,13 +387,12 @@ bindCoreLocalFVRn rdr_name enclosed_scope
     let
        new_name_env = extendRdrEnv name_env rdr_name name
     in
-    setLocalNameEnv new_name_env (enclosed_scope name) `thenRn` \ (result, fvs) ->
-    returnRn (result, delFromNameSet fvs name)
+    setLocalNameEnv new_name_env (enclosed_scope name)
 
-bindCoreLocalsFVRn []     thing_inside = thing_inside []
-bindCoreLocalsFVRn (b:bs) thing_inside = bindCoreLocalFVRn b   $ \ name' ->
-                                        bindCoreLocalsFVRn bs  $ \ names' ->
-                                        thing_inside (name':names')
+bindCoreLocalsRn []     thing_inside = thing_inside []
+bindCoreLocalsRn (b:bs) thing_inside = bindCoreLocalRn b       $ \ name' ->
+                                      bindCoreLocalsRn bs      $ \ names' ->
+                                      thing_inside (name':names')
 
 bindLocalNames names enclosed_scope
   = getLocalNameEnv            `thenRn` \ name_env ->
@@ -399,8 +422,8 @@ bindLocalsFVRn doc rdr_names enclosed_scope
     returnRn (thing, delListFromNameSet fvs names)
 
 -------------------------------------
-bindUVarRn :: SDoc -> RdrName -> (Name -> RnMS (a, FreeVars)) -> RnMS (a, FreeVars)
-bindUVarRn = bindLocalRn
+bindUVarRn :: RdrName -> (Name -> RnMS a) -> RnMS a
+bindUVarRn = bindCoreLocalRn
 
 -------------------------------------
 extendTyVarEnvFVRn :: [Name] -> RnMS (a, FreeVars) -> RnMS (a, FreeVars)
@@ -631,18 +654,31 @@ filterAvail (IEThingAll _) avail@(AvailTC _ _)   = Just avail
 filterAvail ie avail = Nothing
 
 -------------------------------------
-sortAvails :: Avails -> Avails
-sortAvails avails = sortLt lt avails
+groupAvails :: Module -> Avails -> [(ModuleName, Avails)]
+  -- Group by module and sort by occurrence
+  -- This keeps the list in canonical order
+groupAvails this_mod avails 
+  = [ (mkSysModuleNameFS fs, sortLt lt avails)
+    | (fs,avails) <- fmToList groupFM
+    ]
   where
-    a1 `lt` a2 = mod1 < mod2 ||
-                (mod1 == mod2 && occ1 < occ2)
+    groupFM :: FiniteMap FastString Avails
+       -- Deliberately use the FastString so we
+       -- get a canonical ordering
+    groupFM = foldl add emptyFM avails
+
+    add env avail = addToFM_C combine env mod_fs [avail]
+                 where
+                   mod_fs = moduleNameFS (moduleName avail_mod)
+                   avail_mod = case nameModule_maybe (availName avail) of
+                                         Just m  -> m
+                                         Nothing -> this_mod
+                   combine old _ = avail:old
+
+    a1 `lt` a2 = occ1 < occ2
               where
-                name1 = availName a1
-                name2 = availName a2
-                mod1  = nameModule name1
-                mod2  = nameModule name2
-                occ1  = nameOccName name1
-                occ2  = nameOccName name2
+                occ1  = nameOccName (availName a1)
+                occ2  = nameOccName (availName a2)
                                
 -------------------------------------
 pprAvail :: AvailInfo -> SDoc
@@ -661,25 +697,6 @@ pprAvail (Avail n) = ppr n
 %************************************************************************
 
 \begin{code}
-type FreeVars  = NameSet
-
-plusFV   :: FreeVars -> FreeVars -> FreeVars
-addOneFV :: FreeVars -> Name -> FreeVars
-unitFV   :: Name -> FreeVars
-emptyFVs :: FreeVars
-plusFVs  :: [FreeVars] -> FreeVars
-mkFVs   :: [Name] -> FreeVars
-
-isEmptyFVs = isEmptyNameSet
-emptyFVs   = emptyNameSet
-plusFVs    = unionManyNameSets
-plusFV     = unionNameSets
-mkFVs     = mkNameSet
-
--- No point in adding implicitly imported names to the free-var set
-addOneFV s n = addOneToNameSet s n
-unitFV     n = unitNameSet n
-
 -- A useful utility
 mapFvRn f xs = mapRn f xs      `thenRn` \ stuff ->
               let