- = lookupGlobalNameRn rdr_name `thenRn` \ maybe_name ->
- checkUnboundRn rdr_name maybe_name `thenRn` \ name ->
- let
- name' = mungePrintUnqual rdr_name name
- in
- addOccurrenceName name'
-
-
--- mungePrintUnqual is used to make *imported* *occurrences* print unqualified
--- if they were mentioned unqualified in the source code.
--- This improves error messages from the type checker.
--- NB: the binding site is treated differently; see lookupBndrRn
--- After the type checker all occurrences are replaced by the one
--- at the binding site.
-mungePrintUnqual (Qual _ _ _) name = name
-mungePrintUnqual (Unqual _) name
- = case getNameProvenance name of
- NonLocalDef imp hif False -> setNameProvenance name (NonLocalDef imp hif True)
- other -> name
-
--- lookupImplicitOccRn takes an RdrName representing an *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 lookupImplicitOccRn isn't used much in interface mode (it's only the
--- Nothing clause of rnDerivs that calls it at all I think).
--- [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.
-
-lookupImplicitOccRn :: RdrName -> RnMS s Name
-lookupImplicitOccRn (Qual mod occ hif)
- = newImportedGlobalName mod occ hif `thenRn` \ name ->
- addOccurrenceName name
-
-addImplicitOccRn :: Name -> RnMS s Name
-addImplicitOccRn name = addOccurrenceName name
-
-addImplicitOccsRn :: [Name] -> RnMS s ()
-addImplicitOccsRn names = addOccurrenceNames names
+ = 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
+ Nothing -> getModeRn `thenRn` \ mode ->
+ case mode of
+ -- Not found when processing source code; so fail
+ SourceMode -> failWithRn (mkUnboundName rdr_name)
+ (unknownNameErr rdr_name)
+
+ -- Not found when processing an imported declaration,
+ -- so we create a new name for the purpose
+ InterfaceMode -> mkImportedGlobalFromRdrName rdr_name