+lookupBndrRn rdr_name
+ = traceRn (text "lookupBndrRn" <+> ppr rdr_name) `thenRn_`
+ getNameEnvs `thenRn` \ (global_env, local_env) ->
+
+ -- Try local env
+ case lookupRdrEnv local_env rdr_name of {
+ Just name -> returnRn name ;
+ Nothing ->
+
+ getModeRn `thenRn` \ mode ->
+ case mode of
+ InterfaceMode -> -- Look in the global name cache
+ mkImportedGlobalFromRdrName rdr_name `thenRn` \ n ->
+ traceRn (text "lookupBndrRn result:" <+> ppr n) `thenRn_`
+ returnRn n
+
+ SourceMode -> -- Source mode, so look up a *qualified* version
+ -- of the name, so that we get the right one even
+ -- if there are many with the same occ name
+ -- There must *be* a binding
+ getModuleRn `thenRn` \ mod ->
+ case lookupRdrEnv global_env (qualifyRdrName mod rdr_name) of
+ Just (name:rest) -> ASSERT( null rest )
+ returnRn name
+ Nothing -> pprPanic "lookupBndrRn" (ppr mod <+> ppr rdr_name)
+ }
+
+-- lookupOccRn looks up an occurrence of a RdrName
+lookupOccRn :: RdrName -> RnMS Name
+lookupOccRn rdr_name
+ = getNameEnvs `thenRn` \ (global_env, local_env) ->
+ lookup_occ global_env local_env 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 :: RdrName -> RnMS Name
+lookupGlobalOccRn rdr_name
+ = getNameEnvs `thenRn` \ (global_env, local_env) ->
+ lookup_global_occ global_env rdr_name
+
+-- lookupSigOccRn is used for type signatures and pragmas
+-- Is this valid?
+-- module A
+-- import M( f )
+-- f :: Int -> Int
+-- f x = x
+-- In a sense, it's clear that the 'f' in the signature must refer
+-- to A.f, but the Haskell98 report does not stipulate this, so
+-- I treat the 'f' in the signature as a reference to an unqualified
+-- 'f' and hence fail with an ambiguous reference.
+lookupSigOccRn :: RdrName -> RnMS Name
+lookupSigOccRn = lookupOccRn
+
+{- OLD VERSION
+-- This code tries to be cleverer than the above.
+-- The variable in a signature must refer to a locally-defined thing,
+-- even if there's an imported thing of the same name.
+--
+-- But this doesn't work for instance decls:
+-- instance Enum Int where
+-- {-# INLINE enumFrom #-}
+-- ...
+-- Here the enumFrom is an imported reference!
+lookupSigOccRn rdr_name
+ = getNameEnvs `thenRn` \ (global_env, local_env) ->
+ case (lookupRdrEnv local_env rdr_name, lookupRdrEnv global_env rdr_name) of
+ (Just name, _) -> returnRn name
+
+ (Nothing, Just names) -> case filter isLocallyDefined names of
+ [n] -> returnRn n
+ ns -> pprPanic "lookupSigOccRn" (ppr rdr_name <+> ppr names <+> ppr ns)
+ -- There can't be a local top-level name-clash
+ -- (That's dealt with elsewhere.)
+
+ (Nothing, Nothing) -> failWithRn (mkUnboundName rdr_name)
+ (unknownNameErr rdr_name)
+-}
+
+
+-- Look in both local and global env
+lookup_occ global_env local_env rdr_name
+ = case lookupRdrEnv local_env rdr_name of
+ Just name -> returnRn name
+ Nothing -> lookup_global_occ global_env rdr_name
+
+-- Look in global env only
+lookup_global_occ global_env rdr_name
+ = case lookupRdrEnv global_env rdr_name of
+ Just [name] -> returnRn name
+ Just stuff@(name:_) -> addNameClashErrRn rdr_name stuff `thenRn_`
+ returnRn name