+ 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 -> lookupIfaceName 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 ->
+ lookupSrcGlobalOcc (qualifyRdrName (moduleName mod) 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
+ | isOrig rdr_name -- Can occur in source code too
+ = lookupOrigName rdr_name
+
+ | otherwise
+ = getModeRn `thenRn` \ mode ->
+ case mode of
+ SourceMode -> lookupSrcGlobalOcc rdr_name
+ InterfaceMode -> lookupIfaceUnqual rdr_name
+
+lookupSrcGlobalOcc rdr_name
+ -- Lookup a source-code rdr-name; may be qualified or not
+ = 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 -> failWithRn (mkUnboundName rdr_name)
+ (unknownNameErr rdr_name)
+
+lookupOrigName :: RdrName -> RnM d Name
+lookupOrigName rdr_name
+ = ASSERT( isOrig rdr_name )
+ newGlobalName (rdrNameModule rdr_name) (rdrNameOcc rdr_name)
+
+lookupIfaceUnqual :: RdrName -> RnM d Name
+lookupIfaceUnqual rdr_name
+ = ASSERT( isUnqual rdr_name )
+ -- An Unqual is allowed; interface files contain
+ -- unqualified names for locally-defined things, such as
+ -- constructors of a data type.
+ getModuleRn `thenRn ` \ mod ->
+ newGlobalName (moduleName mod) (rdrNameOcc rdr_name)
+
+lookupIfaceName :: RdrName -> RnM d Name
+lookupIfaceName rdr_name
+ | isUnqual rdr_name = lookupIfaceUnqual rdr_name
+ | otherwise = lookupOrigName 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}
+
+@lookupOrigName@ takes an RdrName representing an {\em original}
+name, and adds it to the occurrence pool so that it'll be loaded
+later. This is used when language constructs (such as monad
+comprehensions, overloaded literals, or deriving clauses) require some
+stuff to be loaded that isn't explicitly mentioned in the code.