{-# OPTIONS -fno-warn-tabs #-}-- The above warning supression flag is a temporary kludge.-- While working on this module you are encouraged to remove it and-- detab the module (please do the detabbing in a separate patch). See-- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces-- for detailsmoduleIfaceEnv(newGlobalBinder,newImplicitBinder,lookupIfaceTop,lookupOrig,lookupOrigNameCache,extendNameCache,newIfaceName,newIfaceNames,extendIfaceIdEnv,extendIfaceTyVarEnv,tcIfaceLclId,tcIfaceTyVar,lookupIfaceTyVar,ifaceExportNames,-- Name-cache stuffallocateGlobalBinder,initNameCache,updNameCache,getNameCache,mkNameCacheUpdater,NameCacheUpdater(..))where#include "HsVersions.h"importTcRnMonadimportTysWiredInimportHscTypesimportTyConimportTypeimportDataConimportVarimportNameimportAvailimportPrelNamesimportModuleimportUniqFMimportFastStringimportUniqSupplyimportSrcLocimportUtilimportOutputableimportException(evaluate)importData.IORef(atomicModifyIORef,readIORef)

newGlobalBinder::Module->OccName->SrcSpan->TcRnIfabName-- Used for source code and interface files, to make the-- Name for a thing, given its Module and OccName---- The cache may already already have a binding for this thing,-- because we may have seen an occurrence before, but now is the-- moment when we know its Module and SrcLoc in their full glorynewGlobalBindermodoccloc=domod`seq`occ`seq`return()-- See notes with lookupOrig-- traceIf (text "newGlobalBinder" <+> ppr mod <+> ppr occ <+> ppr loc)updNameCache$\name_cache->allocateGlobalBindername_cachemodocclocallocateGlobalBinder::NameCache->Module->OccName->SrcSpan->(NameCache,Name)allocateGlobalBindername_supplymodoccloc=caselookupOrigNameCache(nsNamesname_supply)modoccof-- A hit in the cache! We are at the binding site of the name.-- This is the moment when we know the SrcLoc-- of the Name, so we set this field in the Name we return.---- Then (bogus) multiple bindings of the same Name-- get different SrcLocs can can be reported as such.---- Possible other reason: it might be in the cache because we-- encountered an occurrence before the binding site for an-- implicitly-imported Name. Perhaps the current SrcLoc is-- better... but not really: it'll still just say 'imported'---- IMPORTANT: Don't mess with wired-in names.-- Their wired-in-ness is in their NameSort-- and their Module is correct.Justname|isWiredInNamename->(name_supply,name)|mod/=iNTERACTIVE->(new_name_supply,name')-- Note [interactive name cache]whereuniq=nameUniquenamename'=mkExternalNameuniqmodocclocnew_cache=extendNameCache(nsNamesname_supply)modoccname'new_name_supply=name_supply{nsNames=new_cache}-- Miss in the cache!-- Build a completely new Name, and put it in the cache_->(new_name_supply,name)where(uniq,us')=takeUniqFromSupply(nsUniqsname_supply)name=mkExternalNameuniqmodocclocnew_cache=extendNameCache(nsNamesname_supply)modoccnamenew_name_supply=name_supply{nsUniqs=us',nsNames=new_cache}{- Note [interactive name cache]
In GHCi we always create Names with the same Module, ":Interactive".
However, we want to be able to shadow older declarations with newer
ones, and we don't want the Name cache giving us back the same Unique
for the new Name as for the old, hence this special case.
See also Note [Outputable Orig RdrName] in HscTypes.
-}newImplicitBinder::Name-- Base name->(OccName->OccName)-- Occurrence name modifier->TcRnIfmnName-- Implicit name-- Called in BuildTyCl to allocate the implicit binders of type/class decls-- For source type/class decls, this is the first occurrence-- For iface ones, the LoadIface has alrady allocated a suitable name in the cachenewImplicitBinderbase_namemk_sys_occ|Justmod<-nameModule_maybebase_name=newGlobalBindermodoccloc|otherwise-- When typechecking a [d| decl bracket |], -- TH generates types, classes etc with Internal names,-- so we follow suit for the implicit binders=do{uniq<-newUnique;return(mkInternalNameuniqoccloc)}whereocc=mk_sys_occ(nameOccNamebase_name)loc=nameSrcSpanbase_nameifaceExportNames::[IfaceExport]->TcRnIfgbllcl[AvailInfo]ifaceExportNamesexports=returnexportslookupOrig::Module->OccName->TcRnIfabNamelookupOrigmodocc=do{-- First ensure that mod and occ are evaluated-- If not, chaos can ensue:-- we read the name-cache-- then pull on mod (say)-- which does some stuff that modifies the name cache-- This did happen, with tycon_mod in TcIface.tcIfaceAlt (DataAlt..)mod`seq`occ`seq`return()-- ; traceIf (text "lookup_orig" <+> ppr mod <+> ppr occ);updNameCache$\name_cache->caselookupOrigNameCache(nsNamesname_cache)modoccof{Justname->(name_cache,name);Nothing->casetakeUniqFromSupply(nsUniqsname_cache)of{(uniq,us)->letname=mkExternalNameuniqmodoccnoSrcSpannew_cache=extendNameCache(nsNamesname_cache)modoccnamein(name_cache{nsUniqs=us,nsNames=new_cache},name)}}}

lookupOrigNameCache::OrigNameCache->Module->OccName->MaybeNamelookupOrigNameCache_modocc-- Don't need to mention gHC_UNIT here because it is explicitly-- included in TysWiredIn.wiredInTyCons|mod==gHC_TUPLE||mod==gHC_PRIM,-- Boxed tuples from one, Justtup_info<-isTupleOcc_maybeocc-- unboxed from the other=-- Special case for tuples; there are too many-- of them to pre-populate the original-name cacheJust(mk_tup_nametup_info)wheremk_tup_name(ns,sort,arity)|ns==tcName=tyConName(tupleTyConsortarity)|ns==dataName=dataConName(tupleConsortarity)|otherwise=Var.varName(dataConWorkId(tupleConsortarity))lookupOrigNameCachencmodocc-- The normal case=caselookupModuleEnvncmodofNothing->NothingJustocc_env->lookupOccEnvocc_envoccextendOrigNameCache::OrigNameCache->Name->OrigNameCacheextendOrigNameCachencname=ASSERT2(isExternalNamename,pprname)extendNameCachenc(nameModulename)(nameOccNamename)nameextendNameCache::OrigNameCache->Module->OccName->Name->OrigNameCacheextendNameCachencmodoccname=extendModuleEnvWithcombinencmod(unitOccEnvoccname)wherecombine_occ_env=extendOccEnvocc_envoccnamegetNameCache::TcRnIfabNameCachegetNameCache=do{HscEnv{hsc_NC=nc_var}<-getTopEnv;readMutVarnc_var}updNameCache::(NameCache->(NameCache,c))->TcRnIfabcupdNameCacheupd_fn=doHscEnv{hsc_NC=nc_var}<-getTopEnvatomicUpdMutVar'nc_varupd_fn-- | A function that atomically updates the name cache given a modifier-- function. The second result of the modifier function will be the result-- of the IO action.dataNameCacheUpdater=NCU{updateNameCache::forallc.(NameCache->(NameCache,c))->IOc}-- | Return a function to atomically update the name cache.mkNameCacheUpdater::TcRnIfabNameCacheUpdatermkNameCacheUpdater=donc_var<-hsc_NC`fmap`getTopEnvletupdate_ncf=dor<-atomicModifyIORefnc_varf_<-evaluate=<<readIORefnc_varreturnrreturn(NCUupdate_nc)

tcIfaceLclId::FastString->IfLIdtcIfaceLclIdocc=do{lcl<-getLclEnv;case(lookupUFM(if_id_envlcl)occ)ofJustty_var->returnty_varNothing->failIfM(text"Iface id out of scope: "<+>pprocc)}extendIfaceIdEnv::[Id]->IfLa->IfLaextendIfaceIdEnvidsthing_inside=do{env<-getLclEnv;let{id_env'=addListToUFM(if_id_envenv)pairs;pairs=[(occNameFS(getOccNameid),id)|id<-ids]};setLclEnv(env{if_id_env=id_env'})thing_inside}tcIfaceTyVar::FastString->IfLTyVartcIfaceTyVarocc=do{lcl<-getLclEnv;case(lookupUFM(if_tv_envlcl)occ)ofJustty_var->returnty_varNothing->failIfM(text"Iface type variable out of scope: "<+>pprocc)}lookupIfaceTyVar::FastString->IfL(MaybeTyVar)lookupIfaceTyVarocc=do{lcl<-getLclEnv;return(lookupUFM(if_tv_envlcl)occ)}extendIfaceTyVarEnv::[TyVar]->IfLa->IfLaextendIfaceTyVarEnvtyvarsthing_inside=do{env<-getLclEnv;let{tv_env'=addListToUFM(if_tv_envenv)pairs;pairs=[(occNameFS(getOccNametv),tv)|tv<-tyvars]};setLclEnv(env{if_tv_env=tv_env'})thing_inside}

lookupIfaceTop::OccName->IfLName-- Look up a top-level name from the current Iface modulelookupIfaceTopocc=do{env<-getLclEnv;lookupOrig(if_modenv)occ}newIfaceName::OccName->IfLNamenewIfaceNameocc=do{uniq<-newUnique;return$!mkInternalNameuniqoccnoSrcSpan}newIfaceNames::[OccName]->IfL[Name]newIfaceNamesoccs=do{uniqs<-newUniqueSupply;return[mkInternalNameuniqoccnoSrcSpan|(occ,uniq)<-occs`zip`uniqsFromSupplyuniqs]}