1 (c) The University of Glasgow 2002
5 newGlobalBinder, newIPName, newImplicitBinder,
6 lookupIfaceTop, lookupIfaceExt,
7 lookupOrig, lookupIfaceTc,
8 newIfaceName, newIfaceNames,
9 extendIfaceIdEnv, extendIfaceTyVarEnv,
10 tcIfaceGlobal, tcIfaceTyCon, tcIfaceClass, tcIfaceExtId,
11 tcIfaceTyVar, tcIfaceDataCon, tcIfaceLclId,
14 allocateGlobalBinder, initNameCache
17 #include "HsVersions.h"
19 import {-# SOURCE #-} TcIface( tcImportDecl )
22 import IfaceType ( IfaceExtName(..), IfaceTyCon(..), ifaceTyConName )
23 import HscTypes ( NameCache(..), HscEnv(..),
24 TyThing, tyThingClass, tyThingTyCon,
25 ExternalPackageState(..), OrigNameCache, lookupType )
26 import TyCon ( TyCon, tyConName )
27 import Class ( Class )
28 import DataCon ( DataCon, dataConWorkId, dataConName )
29 import Var ( TyVar, Id, varName )
30 import Name ( Name, nameUnique, nameModule,
31 nameOccName, nameSrcLoc,
32 getOccName, nameParent_maybe,
33 isWiredInName, nameIsLocalOrFrom, mkIPName,
34 mkExternalName, mkInternalName )
36 import OccName ( OccName, isTupleOcc_maybe, tcName, dataName,
37 lookupOccEnv, unitOccEnv, extendOccEnv, extendOccEnvList )
38 import PrelNames ( gHC_PRIM_Name, pREL_TUP_Name )
39 import TysWiredIn ( intTyCon, boolTyCon, charTyCon, listTyCon, parrTyCon,
40 tupleTyCon, tupleCon )
41 import HscTypes ( ExternalPackageState, NameCache, TyThing(..) )
42 import Module ( Module, ModuleName, moduleName, mkPackageModule,
43 emptyModuleEnv, lookupModuleEnvByName, extendModuleEnv_C )
44 import UniqSupply ( UniqSupply, splitUniqSupply, uniqFromSupply, uniqsFromSupply )
45 import FiniteMap ( emptyFM, lookupFM, addToFM )
46 import BasicTypes ( IPName(..), mapIPName )
47 import SrcLoc ( SrcLoc, noSrcLoc )
48 import Maybes ( orElse )
54 %*********************************************************
56 Allocating new Names in the Name Cache
58 %*********************************************************
61 newGlobalBinder :: Module -> OccName -> Maybe Name -> SrcLoc -> TcRnIf a b Name
62 -- Used for source code and interface files, to make the
63 -- Name for a thing, given its Module and OccName
65 -- The cache may already already have a binding for this thing,
66 -- because we may have seen an occurrence before, but now is the
67 -- moment when we know its Module and SrcLoc in their full glory
69 newGlobalBinder mod occ mb_parent loc
70 = do { mod `seq` occ `seq` return () -- See notes with lookupOrig_help
71 ; name_supply <- getNameCache
72 ; let (name_supply', name) = allocateGlobalBinder
75 ; setNameCache name_supply'
80 -> Module -> OccName -> Maybe Name -> SrcLoc
82 allocateGlobalBinder name_supply mod occ mb_parent loc
83 = case lookupOrigNameCache (nsNames name_supply) (moduleName mod) occ of
84 -- A hit in the cache! We are at the binding site of the name.
85 -- This is the moment when we know the defining Module and SrcLoc
86 -- of the Name, so we set these fields in the Name we return.
88 -- This is essential, to get the right Module in a Name.
89 -- Also: then (bogus) multiple bindings of the same Name
90 -- get different SrcLocs can can be reported as such.
92 -- Possible other reason: it might be in the cache because we
93 -- encountered an occurrence before the binding site for an
94 -- implicitly-imported Name. Perhaps the current SrcLoc is
95 -- better... but not really: it'll still just say 'imported'
97 -- IMPORTANT: Don't mess with wired-in names.
98 -- Their wired-in-ness is in their NameSort
99 -- and their Module is correct.
101 Just name | isWiredInName name -> (name_supply, name)
102 | otherwise -> (new_name_supply, name')
104 uniq = nameUnique name
105 name' = mkExternalName uniq mod occ mb_parent loc
106 new_cache = extend_name_cache (nsNames name_supply) mod occ name'
107 new_name_supply = name_supply {nsNames = new_cache}
109 -- Miss in the cache!
110 -- Build a completely new Name, and put it in the cache
111 Nothing -> (new_name_supply, name)
113 (us', us1) = splitUniqSupply (nsUniqs name_supply)
114 uniq = uniqFromSupply us1
115 name = mkExternalName uniq mod occ mb_parent loc
116 new_cache = extend_name_cache (nsNames name_supply) mod occ name
117 new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
120 newImplicitBinder :: Name -- Base name
121 -> (OccName -> OccName) -- Occurrence name modifier
122 -> TcRnIf m n Name -- Implicit name
123 -- Called in BuildTyCl to allocate the implicit binders of type/class decls
124 -- For source type/class decls, this is the first occurrence
125 -- For iface ones, the LoadIface has alrady allocated a suitable name in the cache
127 -- An *implicit* name has the base-name as parent
128 newImplicitBinder base_name mk_sys_occ
129 = newGlobalBinder (nameModule base_name)
130 (mk_sys_occ (nameOccName base_name))
132 (nameSrcLoc base_name)
134 parent_name = case nameParent_maybe base_name of
135 Just parent_name -> parent_name
138 lookupOrig :: ModuleName -> OccName -> TcRnIf a b Name
139 -- This one starts with a ModuleName, not a Module, because
140 -- we may be simply looking at an occurrence M.x in an interface file.
141 -- We may enounter this well before finding the binding site for M.x
143 -- So, even if we get a miss in the original-name cache, we
144 -- make a new External Name.
146 -- Module to AnotherPackage
147 -- SrcLoc to noSrcLoc
149 -- They'll be overwritten, in due course, by LoadIface.loadDecl.
151 lookupOrig mod_name occ
152 = do { -- First ensure that mod_name and occ are evaluated
153 -- If not, chaos can ensue:
154 -- we read the name-cache
155 -- then pull on mod (say)
156 -- which does some stuff that modifies the name cache
157 -- This did happen, with tycon_mod in TcIface.tcIfaceAlt (DataAlt..)
158 mod `seq` occ `seq` return ()
160 ; name_supply <- getNameCache
161 ; case lookupOrigNameCache (nsNames name_supply) mod_name occ of {
162 Just name -> returnM name ;
165 { let { (us', us1) = splitUniqSupply (nsUniqs name_supply)
166 ; uniq = uniqFromSupply us1
167 ; name = mkExternalName uniq tmp_mod occ Nothing noSrcLoc
168 ; new_cache = extend_name_cache (nsNames name_supply) tmp_mod occ name
169 ; new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
170 ; tmp_mod = mkPackageModule mod_name
171 -- Guess at the package-ness for now, becuase we don't know whether
172 -- this imported module is from the home package or not.
173 -- If we ever need it, we'll open its interface, and update the cache
174 -- with a better name (newGlobalBinder)
176 ; setNameCache new_name_supply
180 newIPName :: IPName OccName -> TcRnIf m n (IPName Name)
181 newIPName occ_name_ip
182 = getNameCache `thenM` \ name_supply ->
184 ipcache = nsIPs name_supply
186 case lookupFM ipcache key of
187 Just name_ip -> returnM name_ip
188 Nothing -> setNameCache new_ns `thenM_`
191 (us', us1) = splitUniqSupply (nsUniqs name_supply)
192 uniq = uniqFromSupply us1
193 name_ip = mapIPName (mkIPName uniq) occ_name_ip
194 new_ipcache = addToFM ipcache key name_ip
195 new_ns = name_supply {nsUniqs = us', nsIPs = new_ipcache}
197 key = occ_name_ip -- Ensures that ?x and %x get distinct Names
200 Local helper functions (not exported)
203 lookupOrigNameCache :: OrigNameCache -> ModuleName -> OccName -> Maybe Name
204 lookupOrigNameCache nc mod_name occ
205 | mod_name == pREL_TUP_Name || mod_name == gHC_PRIM_Name, -- Boxed tuples from one,
206 Just tup_info <- isTupleOcc_maybe occ -- unboxed from the other
207 = -- Special case for tuples; there are too many
208 -- of them to pre-populate the original-name cache
209 Just (mk_tup_name tup_info)
211 mk_tup_name (ns, boxity, arity)
212 | ns == tcName = tyConName (tupleTyCon boxity arity)
213 | ns == dataName = dataConName (tupleCon boxity arity)
214 | otherwise = varName (dataConWorkId (tupleCon boxity arity))
216 lookupOrigNameCache nc mod_name occ -- The normal case
217 = case lookupModuleEnvByName nc mod_name of
219 Just occ_env -> lookupOccEnv occ_env occ
221 extendOrigNameCache :: OrigNameCache -> Name -> OrigNameCache
222 extendOrigNameCache nc name
223 = extend_name_cache nc (nameModule name) (nameOccName name) name
225 extend_name_cache :: OrigNameCache -> Module -> OccName -> Name -> OrigNameCache
226 extend_name_cache nc mod occ name
227 = extendModuleEnv_C combine nc mod (unitOccEnv occ name)
229 combine occ_env _ = extendOccEnv occ_env occ name
231 getNameCache :: TcRnIf a b NameCache
232 getNameCache = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
235 setNameCache :: NameCache -> TcRnIf a b ()
236 setNameCache nc = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
237 writeMutVar nc_var nc }
242 initNameCache :: UniqSupply -> [Name] -> NameCache
243 initNameCache us names
244 = NameCache { nsUniqs = us,
245 nsNames = initOrigNames names,
248 initOrigNames :: [Name] -> OrigNameCache
249 initOrigNames names = foldl extendOrigNameCache emptyModuleEnv names
253 %************************************************************************
255 Getting from Names to TyThings
257 %************************************************************************
260 tcIfaceGlobal :: Name -> IfM a TyThing
262 = do { (eps,hpt) <- getEpsAndHpt
263 ; case lookupType hpt (eps_PTE eps) name of {
264 Just thing -> return thing ;
267 setLclEnv () $ do -- This gets us back to IfG, mainly to
268 -- pacify get_type_env; rather untidy
270 ; case if_rec_types env of
271 Just (mod, get_type_env)
272 | nameIsLocalOrFrom mod name
273 -> do -- It's defined in the module being compiled
274 { type_env <- get_type_env
275 ; case lookupNameEnv type_env name of
276 Just thing -> return thing
277 Nothing -> pprPanic "tcIfaceGlobal (local): not found:"
278 (ppr name $$ ppr type_env) }
280 other -> tcImportDecl name -- It's imported; go get it
283 tcIfaceTyCon :: IfaceTyCon -> IfL TyCon
284 tcIfaceTyCon IfaceIntTc = return intTyCon
285 tcIfaceTyCon IfaceBoolTc = return boolTyCon
286 tcIfaceTyCon IfaceCharTc = return charTyCon
287 tcIfaceTyCon IfaceListTc = return listTyCon
288 tcIfaceTyCon IfacePArrTc = return parrTyCon
289 tcIfaceTyCon (IfaceTupTc bx ar) = return (tupleTyCon bx ar)
290 tcIfaceTyCon (IfaceTc ext_nm) = do { name <- lookupIfaceExt ext_nm
291 ; thing <- tcIfaceGlobal name
292 ; return (tyThingTyCon thing) }
294 tcIfaceClass :: IfaceExtName -> IfL Class
295 tcIfaceClass rdr_name = do { name <- lookupIfaceExt rdr_name
296 ; thing <- tcIfaceGlobal name
297 ; return (tyThingClass thing) }
299 tcIfaceDataCon :: IfaceExtName -> IfL DataCon
300 tcIfaceDataCon gbl = do { name <- lookupIfaceExt gbl
301 ; thing <- tcIfaceGlobal name
303 ADataCon dc -> return dc
304 other -> pprPanic "tcIfaceExtDC" (ppr gbl $$ ppr name$$ ppr thing) }
306 tcIfaceExtId :: IfaceExtName -> IfL Id
307 tcIfaceExtId gbl = do { name <- lookupIfaceExt gbl
308 ; thing <- tcIfaceGlobal name
311 other -> pprPanic "tcIfaceExtId" (ppr gbl $$ ppr name$$ ppr thing) }
313 ------------------------------------------
314 tcIfaceLclId :: OccName -> IfL Id
316 = do { lcl <- getLclEnv
317 ; return (lookupOccEnv (if_id_env lcl) occ
319 pprPanic "tcIfaceLclId" (ppr occ)) }
321 tcIfaceTyVar :: OccName -> IfL TyVar
323 = do { lcl <- getLclEnv
324 ; return (lookupOccEnv (if_tv_env lcl) occ
326 pprPanic "tcIfaceTyVar" (ppr occ)) }
328 extendIfaceIdEnv :: [Id] -> IfL a -> IfL a
329 extendIfaceIdEnv ids thing_inside
330 = do { env <- getLclEnv
331 ; let { id_env' = extendOccEnvList (if_id_env env) pairs
332 ; pairs = [(getOccName id, id) | id <- ids] }
333 ; setLclEnv (env { if_id_env = id_env' }) thing_inside }
335 extendIfaceTyVarEnv :: [TyVar] -> IfL a -> IfL a
336 extendIfaceTyVarEnv tyvars thing_inside
337 = do { env <- getLclEnv
338 ; let { tv_env' = extendOccEnvList (if_tv_env env) pairs
339 ; pairs = [(getOccName tv, tv) | tv <- tyvars] }
340 ; setLclEnv (env { if_tv_env = tv_env' }) thing_inside }
344 %************************************************************************
346 Getting from RdrNames to Names
348 %************************************************************************
351 lookupIfaceTc :: IfaceTyCon -> IfL Name
352 lookupIfaceTc (IfaceTc ext) = lookupIfaceExt ext
353 lookupIfaceTc other_tc = return (ifaceTyConName other_tc)
355 lookupIfaceExt :: IfaceExtName -> IfL Name
356 lookupIfaceExt (ExtPkg mod occ) = lookupOrig mod occ
357 lookupIfaceExt (HomePkg mod occ _) = lookupOrig mod occ
358 lookupIfaceExt (LocalTop occ) = lookupIfaceTop occ
359 lookupIfaceExt (LocalTopSub occ _) = lookupIfaceTop occ
361 lookupIfaceTop :: OccName -> IfL Name
362 -- Look up a top-level name from the current Iface module
364 = do { env <- getLclEnv; lookupOrig (if_mod env) occ }
366 newIfaceName :: OccName -> IfL Name
368 = do { uniq <- newUnique
369 ; return (mkInternalName uniq occ noSrcLoc) }
371 newIfaceNames :: [OccName] -> IfL [Name]
373 = do { uniqs <- newUniqueSupply
374 ; return [ mkInternalName uniq occ noSrcLoc
375 | (occ,uniq) <- occs `zip` uniqsFromSupply uniqs] }