+ | not (isSrcRdrName rdr_name)
+ = lookupImportedName rdr_name
+
+ | otherwise
+ = -- First look up the name in the normal environment.
+ lookupGreRn rdr_name `thenM` \ mb_gre ->
+ case mb_gre of {
+ Just gre -> returnM (gre_name gre) ;
+ Nothing ->
+
+ -- We allow qualified names on the command line to refer to
+ -- *any* name exported by any module in scope, just as if
+ -- there was an "import qualified M" declaration for every
+ -- module.
+ getModule `thenM` \ mod ->
+ if isQual rdr_name && mod == iNTERACTIVE then
+ -- This test is not expensive,
+ lookupQualifiedName rdr_name -- and only happens for failed lookups
+ else
+ unboundName rdr_name }
+
+lookupImportedName :: RdrName -> TcRnIf m n Name
+-- Lookup the occurrence of an imported name
+-- The RdrName is *always* qualified or Exact
+-- Treat it as an original name, and conjure up the Name
+-- Usually it's Exact or Orig, but it can be Qual if it
+-- comes from an hi-boot file. (This minor infelicity is
+-- just to reduce duplication in the parser.)
+lookupImportedName rdr_name
+ | Just n <- isExact_maybe rdr_name
+ -- This happens in derived code
+ = returnM n
+
+ | otherwise -- Always Orig, even when reading a .hi-boot file
+ = ASSERT( not (isUnqual rdr_name) )
+ lookupOrig (rdrNameModule rdr_name) (rdrNameOcc rdr_name)
+
+unboundName :: RdrName -> RnM Name
+unboundName rdr_name
+ = do { addErr (unknownNameErr rdr_name)
+ ; env <- getGlobalRdrEnv;
+ ; traceRn (vcat [unknownNameErr rdr_name,
+ ptext SLIT("Global envt is:"),
+ nest 3 (pprGlobalRdrEnv env)])
+ ; returnM (mkUnboundName rdr_name) }
+
+--------------------------------------------------
+-- Lookup in the Global RdrEnv of the module
+--------------------------------------------------
+
+lookupSrcOcc_maybe :: RdrName -> RnM (Maybe Name)
+-- No filter function; does not report an error on failure
+lookupSrcOcc_maybe rdr_name
+ = do { mb_gre <- lookupGreRn rdr_name
+ ; case mb_gre of
+ Nothing -> returnM Nothing
+ Just gre -> returnM (Just (gre_name gre)) }
+
+-------------------------
+lookupGreRn :: RdrName -> RnM (Maybe GlobalRdrElt)
+-- Just look up the RdrName in the GlobalRdrEnv
+lookupGreRn rdr_name
+ = lookupGreRn_help rdr_name (lookupGRE_RdrName rdr_name)
+
+lookupGreLocalRn :: RdrName -> RnM (Maybe GlobalRdrElt)
+-- Similar, but restricted to locally-defined things
+lookupGreLocalRn rdr_name
+ = lookupGreRn_help rdr_name lookup_fn
+ where
+ lookup_fn env = filter isLocalGRE (lookupGRE_RdrName rdr_name env)
+
+lookupGreRn_help :: RdrName -- Only used in error message
+ -> (GlobalRdrEnv -> [GlobalRdrElt]) -- Lookup function
+ -> RnM (Maybe GlobalRdrElt)
+-- Checks for exactly one match; reports deprecations
+-- Returns Nothing, without error, if too few
+lookupGreRn_help rdr_name lookup
+ = do { env <- getGlobalRdrEnv
+ ; case lookup env of
+ [] -> returnM Nothing
+ [gre] -> returnM (Just gre)
+ gres -> do { addNameClashErrRn rdr_name gres
+ ; returnM (Just (head gres)) } }
+
+------------------------------
+-- GHCi support
+------------------------------
+
+-- A qualified name on the command line can refer to any module at all: we
+-- try to load the interface if we don't already have it.
+lookupQualifiedName :: RdrName -> RnM Name
+lookupQualifiedName rdr_name
+ = let
+ mod = rdrNameModule rdr_name
+ occ = rdrNameOcc rdr_name
+ in
+ loadSrcInterface doc mod False `thenM` \ iface ->
+
+ case [ (mod,occ) |
+ (mod,avails) <- mi_exports iface,
+ avail <- avails,
+ name <- availNames avail,
+ name == occ ] of
+ ((mod,occ):ns) -> ASSERT (null ns)
+ lookupOrig mod occ
+ _ -> unboundName rdr_name
+ where
+ doc = ptext SLIT("Need to find") <+> ppr rdr_name