+ = getGlobalRdrEnv `thenM` \ global_env ->
+ case lookupRdrEnv global_env rdr_name of
+ Nothing -> returnM Nothing
+ Just gres -> case [gre_name gre | gre <- gres, isLocalGRE gre] of
+ [] -> returnM Nothing
+ (n:ns) -> returnM (Just n)
+
+
+-- lookupSigOccRn is used for type signatures and pragmas
+-- Is this valid?
+-- module A
+-- import M( f )
+-- f :: Int -> Int
+-- f x = x
+-- It's clear that the 'f' in the signature must refer to A.f
+-- The Haskell98 report does not stipulate this, but it will!
+-- So we must treat the 'f' in the signature in the same way
+-- as the binding occurrence of 'f', using lookupBndrRn
+lookupSigOccRn :: RdrName -> RnM Name
+lookupSigOccRn = lookupBndrRn
+
+-- lookupInstDeclBndr is used for the binders in an
+-- instance declaration. Here we use the class name to
+-- disambiguate.
+
+lookupInstDeclBndr :: Name -> RdrName -> RnM Name
+ -- We use the selector name as the binder
+lookupInstDeclBndr cls_name rdr_name
+ | isUnqual rdr_name
+ = -- Find all the things the class op name maps to
+ -- and pick the one with the right parent name
+ getGblEnv `thenM` \ gbl_env ->
+ let
+ avail_env = imp_env (tcg_imports gbl_env)
+ in
+ case lookupAvailEnv avail_env cls_name of
+ Nothing ->
+ -- If the class itself isn't in scope, then cls_name will
+ -- be unboundName, and there'll already be an error for
+ -- that in the error list. Example:
+ -- e.g. import Prelude hiding( Ord )
+ -- instance Ord T where ...
+ -- The program is wrong, but that should not cause a crash.
+ returnM (mkUnboundName rdr_name)
+
+ Just (AvailTC _ ns) -> case [n | n <- ns, nameOccName n == occ] of
+ (n:ns)-> ASSERT( null ns ) returnM n
+ [] -> unboundName 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 )
+ lookupOrigName rdr_name
+
+ where
+ occ = rdrNameOcc rdr_name
+
+lookupSysBndr :: RdrName -> RnM Name
+-- Used for the 'system binders' in a data type or class declaration
+-- Do *not* look up in the RdrEnv; these system binders are never in scope
+-- Instead, get the module from the monad... but remember that
+-- where the module is depends on whether we are renaming source or
+-- interface file stuff
+lookupSysBndr rdr_name
+ = getSrcLocM `thenM` \ loc ->
+ getModeRn `thenM` \ mode ->
+ case mode of
+ InterfaceMode mod -> newTopBinder mod rdr_name loc
+ other -> getModule `thenM` \ mod ->
+ newTopBinder mod rdr_name loc
+
+-- lookupOccRn looks up an occurrence of a RdrName
+lookupOccRn :: RdrName -> RnM Name
+lookupOccRn rdr_name
+ = getLocalRdrEnv `thenM` \ local_env ->
+ case lookupRdrEnv local_env rdr_name of
+ Just name -> returnM name
+ Nothing -> lookupGlobalOccRn rdr_name
+
+-- lookupGlobalOccRn is like lookupOccRn, except that it looks in the global
+-- environment. It's used only for
+-- record field names
+-- class op names in class and instance decls
+
+lookupGlobalOccRn rdr_name
+ = getModeRn `thenM` \ mode ->
+ case mode of
+ InterfaceMode mod -> lookupIfaceName mod rdr_name
+ SourceMode -> lookupSrcName rdr_name
+
+ CmdLineMode
+ | not (isQual rdr_name) ->
+ lookupSrcName rdr_name
+
+ -- We allow qualified names on the command line to refer to
+ -- *any* name exported by any module in scope, just as if
+ -- there was an "import qualified M" declaration for every
+ -- module.
+ --
+ -- First look up the name in the normal environment. If
+ -- it isn't there, we manufacture a new occurrence of an
+ -- original name.
+ | otherwise ->
+ lookupSrcName_maybe rdr_name `thenM` \ mb_name ->
+ case mb_name of
+ Just name -> returnM name
+ Nothing -> lookupQualifiedName rdr_name
+
+-- A qualified name on the command line can refer to any module at all: we
+-- try to load the interface if we don't already have it.
+lookupQualifiedName :: RdrName -> TcRn m Name
+lookupQualifiedName rdr_name
+ = let
+ mod = rdrNameModule rdr_name
+ occ = rdrNameOcc rdr_name
+ in
+ loadInterface (ppr rdr_name) mod (ImportByUser False) `thenM` \ iface ->
+ case [ name | (_,avails) <- mi_exports iface,
+ avail <- avails,
+ name <- availNames avail,
+ nameOccName name == occ ] of
+ (n:ns) -> ASSERT (null ns) returnM n
+ _ -> unboundName rdr_name
+
+lookupSrcName :: RdrName -> TcRn m Name
+lookupSrcName rdr_name
+ = lookupSrcName_maybe rdr_name `thenM` \ mb_name ->
+ case mb_name of
+ Nothing -> unboundName rdr_name
+ Just name -> returnM name
+
+lookupSrcName_maybe :: RdrName -> TcRn m (Maybe Name)
+lookupSrcName_maybe rdr_name
+ | Just name <- isExact_maybe rdr_name -- Can occur in source code too
+ = returnM (Just name)
+
+ | isOrig rdr_name -- An original name
+ = newGlobalNameFromRdrName rdr_name `thenM` \ name ->
+ returnM (Just name)
+
+ | otherwise
+ = lookupGRE rdr_name `thenM` \ mb_gre ->
+ case mb_gre of
+ Nothing -> returnM Nothing
+ Just gre -> returnM (Just (gre_name gre))
+
+lookupGRE :: RdrName -> TcRn m (Maybe GlobalRdrElt)
+lookupGRE rdr_name
+ = getGlobalRdrEnv `thenM` \ global_env ->
+ case lookupRdrEnv global_env rdr_name of
+ Just [gre] -> case gre_deprec gre of
+ Nothing -> returnM (Just gre)
+ Just _ -> warnDeprec gre `thenM_`
+ returnM (Just gre)
+ Just stuff@(gre : _) -> addNameClashErrRn rdr_name stuff `thenM_`
+ returnM (Just gre)
+ Nothing -> return Nothing
+
+lookupIfaceName :: Module -> RdrName -> TcRn m Name
+ -- An Unqual is allowed; interface files contain