+ case lookupFM cache key of
+ Just name -> traceRn (text "newGlobalName: hit" <+> ppr name) `thenRn_`
+ returnRn name
+
+ Nothing -> setNameSupplyRn (us', new_cache, ipcache) `thenRn_`
+ traceRn (text "newGlobalName: new" <+> ppr name) `thenRn_`
+ returnRn name
+ where
+ (us', us1) = splitUniqSupply us
+ uniq = uniqFromSupply us1
+ mod = mkVanillaModule mod_name
+ name = mkGlobalName uniq mod occ noSrcLoc
+ new_cache = addToFM cache key name
+
+newIPName rdr_name
+ = getNameSupplyRn `thenRn` \ (us, cache, ipcache) ->
+ case lookupFM ipcache key of
+ Just name -> returnRn name
+ Nothing -> setNameSupplyRn (us', cache, new_ipcache) `thenRn_`
+ returnRn name
+ where
+ (us', us1) = splitUniqSupply us
+ uniq = uniqFromSupply us1
+ name = mkIPName uniq key
+ new_ipcache = addToFM ipcache key name
+ where key = (rdrNameOcc rdr_name)
+\end{code}
+
+%*********************************************************
+%* *
+\subsection{Looking up names}
+%* *
+%*********************************************************
+
+Looking up a name in the RnEnv.
+
+\begin{code}
+lookupBndrRn rdr_name
+ = getLocalNameEnv `thenRn` \ local_env ->
+ case lookupRdrEnv local_env rdr_name of
+ Just name -> returnRn name
+ Nothing -> lookupTopBndrRn rdr_name
+
+lookupTopBndrRn rdr_name
+ = getModeRn `thenRn` \ mode ->
+ case mode of
+ InterfaceMode -> -- Look in the global name cache
+ lookupOrigName rdr_name
+
+ 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 ->
+ getGlobalNameEnv `thenRn` \ global_env ->
+ case lookupRdrEnv global_env (qualifyRdrName (moduleName mod) rdr_name) of
+ Just ((name,_):rest) -> ASSERT( null rest )
+ returnRn name
+ Nothing -> -- Almost always this case is a compiler bug.
+ -- But consider a type signature that doesn't have
+ -- a corresponding binder:
+ -- module M where { f :: Int->Int }
+ -- We use lookupSigOccRn, which uses lookupBndrRn (for good reasons)
+ -- and we don't want to panic. So we report an out-of-scope error
+ failWithRn (mkUnboundName rdr_name)
+ (unknownNameErr rdr_name)
+
+-- 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 -> RnMS Name
+lookupSigOccRn = lookupBndrRn
+
+-- lookupOccRn looks up an occurrence of a RdrName
+lookupOccRn :: RdrName -> RnMS Name
+lookupOccRn rdr_name
+ = getLocalNameEnv `thenRn` \ local_env ->
+ case lookupRdrEnv local_env rdr_name of
+ Just name -> returnRn 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 `thenRn` \ mode ->
+ case mode of {
+ -- When processing interface files, the global env
+ -- is always empty, so go straight to the name cache
+ InterfaceMode -> lookupOrigName rdr_name ;
+
+ SourceMode ->
+
+ getGlobalNameEnv `thenRn` \ global_env ->
+ case lookupRdrEnv global_env rdr_name of
+ Just [(name,_)] -> returnRn name
+ Just stuff@((name,_):_)
+ -> addNameClashErrRn rdr_name stuff `thenRn_`
+ returnRn name
+ Nothing -> -- Not found when processing source code; so fail
+ failWithRn (mkUnboundName rdr_name)
+ (unknownNameErr rdr_name)
+ }
+\end{code}
+%