+ 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)
+ }
+
+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.
+
+This doesn't apply in interface mode, where everything is explicit,
+but we don't check for this case: it does no harm to record an
+``extra'' occurrence and @lookupOrigNames@ isn't used much in
+interface mode (it's only the @Nothing@ clause of @rnDerivs@ that
+calls it at all I think).
+
+ \fbox{{\em Jan 98: this comment is wrong: @rnHsType@ uses it quite a bit.}}
+
+For List and Tuple types it's important to get the correct
+@isLocallyDefined@ flag, which is used in turn when deciding
+whether there are any instance decls in this module are ``special''.
+The name cache should have the correct provenance, though.
+
+\begin{code}
+lookupOrigName :: RdrName -> RnM d Name
+lookupOrigName rdr_name
+ | isQual rdr_name
+ = newGlobalName (rdrNameModule rdr_name) (rdrNameOcc rdr_name)
+
+ | otherwise
+ = -- 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)
+
+lookupOrigNames :: [RdrName] -> RnM d NameSet
+lookupOrigNames rdr_names
+ = mapRn lookupOrigName rdr_names `thenRn` \ names ->
+ returnRn (mkNameSet names)
+\end{code}
+
+lookupSysBinder is used for the "system binders" of a type, class, or instance decl.
+It ensures that the module is set correctly in the name cache, and sets the provenance
+on the returned name too. The returned name will end up actually in the type, class,
+or instance.
+
+\begin{code}
+lookupSysBinder rdr_name
+ = ASSERT( isUnqual rdr_name )
+ getModuleRn `thenRn` \ mod ->
+ getSrcLocRn `thenRn` \ loc ->
+ newTopBinder mod rdr_name loc
+\end{code}