[project @ 2002-12-11 11:59:59 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnEnv.lhs
index f5f3eab..e11b771 100644 (file)
@@ -31,7 +31,7 @@ import HscTypes               ( Provenance(..), pprNameProvenance, hasBetterProv,
 import TcRnMonad
 import Name            ( Name, getName, getSrcLoc, nameIsLocalOrFrom, isWiredInName,
                          mkInternalName, mkExternalName, mkIPName, nameSrcLoc,
-                         nameOccName, setNameModuleAndLoc, nameModule  )
+                         nameOccName, setNameSrcLoc, nameModule        )
 import NameSet
 import OccName         ( OccName, tcName, isDataOcc, occNameUserString, occNameFlavour )
 import Module          ( Module, ModuleName, moduleName, mkHomeModule,
@@ -66,19 +66,15 @@ import FastString   ( FastString )
 
 \begin{code}
 newTopBinder :: Module -> RdrName -> SrcLoc -> TcRn m 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
   | Just name <- isExact_maybe rdr_name
   = returnM name
 
   | otherwise
-  = newGlobalName mod (rdrNameOcc rdr_name) loc
+  = ASSERT( not (isOrig rdr_name) || rdrNameModule rdr_name == moduleName mod )
+       -- When reading External Core we get Orig names as binders, 
+       -- but they should agree with the module gotten from the monad
+    newGlobalName mod (rdrNameOcc rdr_name) loc
 
 newGlobalName :: Module -> OccName -> SrcLoc -> TcRn m Name
 newGlobalName mod occ loc
@@ -88,15 +84,21 @@ newGlobalName mod occ loc
 
        -- A hit in the cache!  We are at the binding site of the name.
        -- This is the moment when we know the defining SrcLoc
-       -- of the Name.  However, since we must have encountered an 
-       -- occurrence before the binding site, this must be an 
-       -- implicitly-imported name and we can't give a useful SrcLoc to
-       -- it.  So we just leave it alone.
+       -- of the Name, so we set the SrcLoc of the name we return.
+       --
+       -- Main reason: then (bogus) multiple bindings of the same Name
+       --              get different SrcLocs can can be reported as such.
        --
-       -- IMPORTANT: don't mess with wired-in names.  
-       -- Their wired-in-ness is in the SrcLoc
+       -- Possible other reason: it might be in the cache because we
+       --      encountered an occurrence before the binding site for an
+       --      implicitly-imported Name.  Perhaps the current SrcLoc is
+       --      better... but not really: it'll still just say 'imported'
+       --
+       -- IMPORTANT: Don't mess with wired-in names.  
+       --            Their wired-in-ness is in the SrcLoc
 
-       Just name -> returnM name
+       Just name | isWiredInName name -> returnM name
+                 | otherwise          -> returnM (setNameSrcLoc name loc)
                     
        -- Miss in the cache!
        -- Build a completely new Name, and put it in the cache
@@ -234,19 +236,12 @@ lookupTopBndrRn rdr_name
 -- A separate function (importsFromLocalDecls) reports duplicate top level
 -- decls, so here it's safe just to choose an arbitrary one.
 
-       -- There should never be a qualified name in a binding position 
-       -- The parser could check this, but doesn't (yet)
-  | isQual rdr_name
-  = getSrcLocM                                                 `thenM` \ loc ->
-    qualNameErr (text "In its declaration") (rdr_name,loc)     `thenM_`
-    returnM (mkUnboundName rdr_name)
+-- There should never be a qualified name in a binding position in Haskell,
+-- but there can be if we have read in an external-Core file.
+-- The Haskell parser checks for the illegal qualified name in Haskell 
+-- source files, so we don't need to do so here.
 
-  | otherwise
-  = ASSERT( not (isOrig rdr_name) )
-       -- Original names are used only for occurrences, 
-       -- not binding sites
-
-    getModeRn                  `thenM` \ mode ->
+  = getModeRn                  `thenM` \ mode ->
     case mode of
        InterfaceMode mod -> 
            getSrcLocM          `thenM` \ loc ->
@@ -339,15 +334,12 @@ lookupInstDeclBndr cls_name rdr_name
 
        other               -> pprPanic "lookupInstDeclBndr" (ppr cls_name)
 
-  | isQual rdr_name    -- Should never have a qualified name in a binding position
-  = getSrcLocM                                                 `thenM` \ loc ->
-    qualNameErr (text "In an instance method") (rdr_name,loc)  `thenM_`
-    returnM (mkUnboundName rdr_name)
-       
+
   | otherwise          -- Occurs in derived instances, where we just
                        -- refer directly to the right method, and avail_env
                        -- isn't available
   = ASSERT2( not (isQual rdr_name), ppr rdr_name )
+         -- NB: qualified names are rejected by the parser
     lookupOrigName rdr_name
 
   where
@@ -582,19 +574,32 @@ implicitModuleFVs source_fvs
     namesNeededForFlattening           `plusFV`
     ubiquitousNames
 
+
+thProxyName :: NameSet
+mkTemplateHaskellFVs :: NameSet -> NameSet
        -- This is a bit of a hack.  When we see the Template-Haskell construct
        --      [| expr |]
        -- we are going to need lots of the ``smart constructors'' defined in
        -- the main Template Haskell data type module.  Rather than treat them
        -- all as free vars at every occurrence site, we just make the Q type
        -- consructor a free var.... and then use that here to haul in the others
-mkTemplateHaskellFVs source_fvs
+
 #ifdef GHCI
-       -- Only if Template Haskell is enabled
+---------------        Template Haskell enabled --------------
+thProxyName = unitFV qTyConName
+
+mkTemplateHaskellFVs source_fvs
   | qTyConName `elemNameSet` source_fvs = templateHaskellNames
-#endif
   | otherwise                          = emptyFVs
 
+#else
+---------------        Template Haskell disabled --------------
+
+thProxyName                    = emptyFVs
+mkTemplateHaskellFVs source_fvs = emptyFVs
+#endif
+--------------------------------------------------------
+
 -- ubiquitous_names are loaded regardless, because 
 -- they are needed in virtually every program
 ubiquitousNames 
@@ -820,8 +825,11 @@ checkDupOrQualNames, checkDupNames :: SDoc
        -- Works in any variant of the renamer monad
 
 checkDupOrQualNames doc_str rdr_names_w_loc
-  =    -- Check for use of qualified names
-    mappM_ (qualNameErr doc_str) quals         `thenM_`
+  =    -- Qualified names in patterns are now rejected by the parser
+       -- but I'm not 100% certain that it finds all cases, so I've left
+       -- this check in for now.  Should go eventually.
+       --      Hmm.  Sooner rather than later.. data type decls
+--     mappM_ (qualNameErr doc_str) quals      `thenM_`
     checkDupNames doc_str rdr_names_w_loc
   where
     quals = filter (isQual . fst) rdr_names_w_loc