+lookupThName th_name@(TH.Name occ TH.NameS)
+ = do { let rdr_name = mkRdrUnqual (OccName.mkOccFS ns occ_fs)
+ ; rdr_env <- getLocalRdrEnv
+ ; case lookupLocalRdrEnv rdr_env rdr_name of
+ Just name -> return name
+ Nothing -> do
+ { mb_name <- lookupSrcOcc_maybe rdr_name
+ ; case mb_name of
+ Just name -> return name ;
+ Nothing -> failWithTc (notInScope th_name)
+ }}
+ where
+ ns | isLexCon occ_fs = OccName.dataName
+ | otherwise = OccName.varName
+ occ_fs = mkFastString (TH.occString occ)
+
+lookupThName (TH.Name occ (TH.NameU uniq))
+ = return (mkInternalName (mk_uniq uniq) (OccName.mkOccFS bogus_ns occ_fs) noSrcLoc)
+ where
+ occ_fs = mkFastString (TH.occString occ)
+ bogus_ns = OccName.varName -- Not yet recorded in the TH name
+ -- but only the unique matters
+
+tcLookupTh :: Name -> TcM TcTyThing
+-- This is a specialised version of TcEnv.tcLookup; specialised mainly in that
+-- it gives a reify-related error message on failure, whereas in the normal
+-- tcLookup, failure is a bug.
+tcLookupTh name
+ = do { (gbl_env, lcl_env) <- getEnvs
+ ; case lookupNameEnv (tcl_env lcl_env) name of
+ Just thing -> returnM thing
+ Nothing -> do
+ { if nameIsLocalOrFrom (tcg_mod gbl_env) name
+ then -- It's defined in this module
+ case lookupNameEnv (tcg_type_env gbl_env) name of
+ Just thing -> return (AGlobal thing)
+ Nothing -> failWithTc (notInEnv name)
+
+ else do -- It's imported
+ { (eps,hpt) <- getEpsAndHpt
+ ; case lookupType hpt (eps_PTE eps) name of
+ Just thing -> return (AGlobal thing)
+ Nothing -> do { traceIf (text "tcLookupGlobal" <+> ppr name)
+ ; thing <- initIfaceTcRn (tcImportDecl name)
+ ; return (AGlobal thing) }
+ -- Imported names should always be findable;
+ -- if not, we fail hard in tcImportDecl
+ }}}
+
+mk_uniq :: Int# -> Unique
+mk_uniq u = mkUniqueGrimily (I# u)
+
+notInScope :: TH.Name -> SDoc
+notInScope th_name = quotes (text (TH.pprint th_name)) <+>
+ ptext SLIT("is not in scope at a reify")
+ -- Ugh! Rather an indirect way to display the name
+
+notInEnv :: Name -> SDoc
+notInEnv name = quotes (ppr name) <+>
+ ptext SLIT("is not in the type environment at a reify")
+