1 (c) The University of Glasgow 2002
5 newGlobalBinder, newIPName, newImplicitBinder,
6 lookupIfaceTop, lookupIfaceExt,
7 lookupOrig, lookupIfaceTc,
8 newIfaceName, newIfaceNames,
9 extendIfaceIdEnv, extendIfaceTyVarEnv,
10 tcIfaceLclId, tcIfaceTyVar,
12 lookupAvail, ifaceExportNames,
15 allocateGlobalBinder, initNameCache,
18 #include "HsVersions.h"
21 import IfaceType ( IfaceExtName(..), IfaceTyCon(..), ifaceTyConName )
22 import TysWiredIn ( tupleTyCon, tupleCon )
23 import HscTypes ( NameCache(..), HscEnv(..), GenAvailInfo(..),
24 IfaceExport, OrigNameCache )
25 import Type ( mkOpenTvSubst, substTy )
26 import TyCon ( TyCon, tyConName )
27 import DataCon ( dataConWorkId, dataConName )
28 import Var ( TyVar, Id, varName, setIdType, idType )
29 import Name ( Name, nameUnique, nameModule,
30 nameOccName, nameSrcLoc,
31 getOccName, nameParent_maybe,
32 isWiredInName, mkIPName,
33 mkExternalName, mkInternalName )
34 import NameSet ( NameSet, emptyNameSet, addListToNameSet )
35 import OccName ( OccName, isTupleOcc_maybe, tcName, dataName, mapOccEnv, occNameFS,
36 lookupOccEnv, unitOccEnv, extendOccEnv, extendOccEnvList )
37 import PrelNames ( gHC_PRIM, dATA_TUP )
38 import Module ( Module, emptyModuleEnv, ModuleName, modulePackageId,
39 lookupModuleEnv, extendModuleEnv_C, mkModule )
40 import UniqFM ( lookupUFM, addListToUFM )
41 import FastString ( FastString )
42 import UniqSupply ( UniqSupply, splitUniqSupply, uniqFromSupply, uniqsFromSupply )
43 import FiniteMap ( emptyFM, lookupFM, addToFM )
44 import BasicTypes ( IPName(..), mapIPName )
45 import SrcLoc ( SrcLoc, noSrcLoc )
51 %*********************************************************
53 Allocating new Names in the Name Cache
55 %*********************************************************
58 newGlobalBinder :: Module -> OccName -> Maybe Name -> SrcLoc -> TcRnIf a b Name
59 -- Used for source code and interface files, to make the
60 -- Name for a thing, given its Module and OccName
62 -- The cache may already already have a binding for this thing,
63 -- because we may have seen an occurrence before, but now is the
64 -- moment when we know its Module and SrcLoc in their full glory
66 newGlobalBinder mod occ mb_parent loc
67 = do { mod `seq` occ `seq` return () -- See notes with lookupOrig_help
68 -- ; traceIf (text "newGlobalBinder" <+> ppr mod <+> ppr occ <+> ppr loc)
69 ; name_supply <- getNameCache
70 ; let (name_supply', name) = allocateGlobalBinder
73 ; setNameCache name_supply'
78 -> Module -> OccName -> Maybe Name -> SrcLoc
80 allocateGlobalBinder name_supply mod occ mb_parent loc
81 = case lookupOrigNameCache (nsNames name_supply) mod occ of
82 -- A hit in the cache! We are at the binding site of the name.
83 -- This is the moment when we know the defining parent and SrcLoc
84 -- of the Name, so we set these fields in the Name we return.
86 -- Then (bogus) multiple bindings of the same Name
87 -- get different SrcLocs can can be reported as such.
89 -- Possible other reason: it might be in the cache because we
90 -- encountered an occurrence before the binding site for an
91 -- implicitly-imported Name. Perhaps the current SrcLoc is
92 -- better... but not really: it'll still just say 'imported'
94 -- IMPORTANT: Don't mess with wired-in names.
95 -- Their wired-in-ness is in their NameSort
96 -- and their Module is correct.
98 Just name | isWiredInName name -> (name_supply, name)
99 | otherwise -> (new_name_supply, name')
101 uniq = nameUnique name
102 name' = mkExternalName uniq mod occ mb_parent loc
103 new_cache = extend_name_cache (nsNames name_supply) mod occ name'
104 new_name_supply = name_supply {nsNames = new_cache}
106 -- Miss in the cache!
107 -- Build a completely new Name, and put it in the cache
108 Nothing -> (new_name_supply, name)
110 (us', us1) = splitUniqSupply (nsUniqs name_supply)
111 uniq = uniqFromSupply us1
112 name = mkExternalName uniq mod occ mb_parent loc
113 new_cache = extend_name_cache (nsNames name_supply) mod occ name
114 new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
117 newImplicitBinder :: Name -- Base name
118 -> (OccName -> OccName) -- Occurrence name modifier
119 -> TcRnIf m n Name -- Implicit name
120 -- Called in BuildTyCl to allocate the implicit binders of type/class decls
121 -- For source type/class decls, this is the first occurrence
122 -- For iface ones, the LoadIface has alrady allocated a suitable name in the cache
124 -- An *implicit* name has the base-name as parent
125 newImplicitBinder base_name mk_sys_occ
126 = newGlobalBinder (nameModule base_name)
127 (mk_sys_occ (nameOccName base_name))
129 (nameSrcLoc base_name)
131 parent_name = case nameParent_maybe base_name of
132 Just parent_name -> parent_name
135 ifaceExportNames :: [IfaceExport] -> TcRnIf gbl lcl NameSet
136 ifaceExportNames exports
137 = foldlM do_one emptyNameSet exports
139 do_one acc (mod, exports) = foldlM (do_avail mod) acc exports
140 do_avail mod acc avail = do { ns <- lookupAvail mod avail
141 ; return (addListToNameSet acc ns) }
143 lookupAvail :: Module -> GenAvailInfo OccName -> TcRnIf a b [Name]
144 -- Find all the names arising from an import
145 -- Make sure the parent info is correct, even though we may not
146 -- yet have read the interface for this module
147 lookupAvail mod (Avail n) = do { n' <- lookupOrig mod n;
149 lookupAvail mod (AvailTC p_occ occs)
150 = do { p_name <- lookupOrig mod p_occ
151 ; let lookup_sub occ | occ == p_occ = return p_name
152 | otherwise = lookup_orig mod occ (Just p_name)
153 ; mappM lookup_sub occs }
154 -- Remember that 'occs' is all the exported things, including
155 -- the parent. It's possible to export just class ops without
156 -- the class, via C( op ). If the class was exported too we'd
159 -- The use of lookupOrigSub here (rather than lookupOrig)
160 -- ensures that the subordinate names record their parent;
161 -- and that in turn ensures that the GlobalRdrEnv
162 -- has the correct parent for all the names in its range.
163 -- For imported things, we may only suck in the interface later, if ever.
164 -- Reason for all this:
165 -- Suppose module M exports type A.T, and constructor A.MkT
166 -- Then, we know that A.MkT is a subordinate name of A.T,
167 -- even though we aren't at the binding site of A.T
168 -- And it's important, because we may simply re-export A.T
169 -- without ever sucking in the declaration itself.
172 lookupOrig :: Module -> OccName -> TcRnIf a b Name
173 -- Even if we get a miss in the original-name cache, we
174 -- make a new External Name.
176 -- SrcLoc to noSrcLoc
178 -- They'll be overwritten, in due course, by LoadIface.loadDecl.
179 lookupOrig mod occ = lookup_orig mod occ Nothing
181 lookup_orig :: Module -> OccName -> Maybe Name -> TcRnIf a b Name
182 -- Used when we know the parent of the thing we are looking up
183 lookup_orig mod occ mb_parent
184 = do { -- First ensure that mod and occ are evaluated
185 -- If not, chaos can ensue:
186 -- we read the name-cache
187 -- then pull on mod (say)
188 -- which does some stuff that modifies the name cache
189 -- This did happen, with tycon_mod in TcIface.tcIfaceAlt (DataAlt..)
190 mod `seq` occ `seq` return ()
192 ; name_supply <- getNameCache
193 ; case lookupOrigNameCache (nsNames name_supply) mod occ of {
194 Just name -> returnM name ;
197 { let { (us', us1) = splitUniqSupply (nsUniqs name_supply)
198 ; uniq = uniqFromSupply us1
199 ; name = mkExternalName uniq mod occ mb_parent noSrcLoc
200 ; new_cache = extend_name_cache (nsNames name_supply) mod occ name
201 ; new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
203 ; setNameCache new_name_supply
207 newIPName :: IPName OccName -> TcRnIf m n (IPName Name)
208 newIPName occ_name_ip
209 = getNameCache `thenM` \ name_supply ->
211 ipcache = nsIPs name_supply
213 case lookupFM ipcache key of
214 Just name_ip -> returnM name_ip
215 Nothing -> setNameCache new_ns `thenM_`
218 (us', us1) = splitUniqSupply (nsUniqs name_supply)
219 uniq = uniqFromSupply us1
220 name_ip = mapIPName (mkIPName uniq) occ_name_ip
221 new_ipcache = addToFM ipcache key name_ip
222 new_ns = name_supply {nsUniqs = us', nsIPs = new_ipcache}
224 key = occ_name_ip -- Ensures that ?x and %x get distinct Names
227 Local helper functions (not exported)
230 lookupOrigNameCache :: OrigNameCache -> Module -> OccName -> Maybe Name
231 lookupOrigNameCache nc mod occ
232 | mod == dATA_TUP || mod == gHC_PRIM, -- Boxed tuples from one,
233 Just tup_info <- isTupleOcc_maybe occ -- unboxed from the other
234 = -- Special case for tuples; there are too many
235 -- of them to pre-populate the original-name cache
236 Just (mk_tup_name tup_info)
238 mk_tup_name (ns, boxity, arity)
239 | ns == tcName = tyConName (tupleTyCon boxity arity)
240 | ns == dataName = dataConName (tupleCon boxity arity)
241 | otherwise = varName (dataConWorkId (tupleCon boxity arity))
243 lookupOrigNameCache nc mod occ -- The normal case
244 = case lookupModuleEnv nc mod of
246 Just occ_env -> lookupOccEnv occ_env occ
248 extendOrigNameCache :: OrigNameCache -> Name -> OrigNameCache
249 extendOrigNameCache nc name
250 = extend_name_cache nc (nameModule name) (nameOccName name) name
252 extend_name_cache :: OrigNameCache -> Module -> OccName -> Name -> OrigNameCache
253 extend_name_cache nc mod occ name
254 = extendModuleEnv_C combine nc mod (unitOccEnv occ name)
256 combine occ_env _ = extendOccEnv occ_env occ name
258 getNameCache :: TcRnIf a b NameCache
259 getNameCache = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
262 setNameCache :: NameCache -> TcRnIf a b ()
263 setNameCache nc = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
264 writeMutVar nc_var nc }
269 initNameCache :: UniqSupply -> [Name] -> NameCache
270 initNameCache us names
271 = NameCache { nsUniqs = us,
272 nsNames = initOrigNames names,
275 initOrigNames :: [Name] -> OrigNameCache
276 initOrigNames names = foldl extendOrigNameCache emptyModuleEnv names
281 %************************************************************************
283 Type variables and local Ids
285 %************************************************************************
288 tcIfaceLclId :: FastString -> IfL Id
290 = do { lcl <- getLclEnv
291 ; case (lookupUFM (if_id_env lcl) occ) of
292 Just ty_var -> return ty_var
293 Nothing -> failIfM (text "Iface id out of scope: " <+> ppr occ)
296 extendIfaceIdEnv :: [Id] -> IfL a -> IfL a
297 extendIfaceIdEnv ids thing_inside
298 = do { env <- getLclEnv
299 ; let { id_env' = addListToUFM (if_id_env env) pairs
300 ; pairs = [(occNameFS (getOccName id), id) | id <- ids] }
301 ; setLclEnv (env { if_id_env = id_env' }) thing_inside }
304 tcIfaceTyVar :: FastString -> IfL TyVar
306 = do { lcl <- getLclEnv
307 ; case (lookupUFM (if_tv_env lcl) occ) of
308 Just ty_var -> return ty_var
309 Nothing -> failIfM (text "Iface type variable out of scope: " <+> ppr occ)
312 extendIfaceTyVarEnv :: [TyVar] -> IfL a -> IfL a
313 extendIfaceTyVarEnv tyvars thing_inside
314 = do { env <- getLclEnv
315 ; let { tv_env' = addListToUFM (if_tv_env env) pairs
316 ; pairs = [(occNameFS (getOccName tv), tv) | tv <- tyvars] }
317 ; setLclEnv (env { if_tv_env = tv_env' }) thing_inside }
321 %************************************************************************
323 Getting from RdrNames to Names
325 %************************************************************************
328 lookupIfaceTc :: IfaceTyCon -> IfL Name
329 lookupIfaceTc (IfaceTc ext) = lookupIfaceExt ext
330 lookupIfaceTc other_tc = return (ifaceTyConName other_tc)
332 lookupIfaceExt :: IfaceExtName -> IfL Name
333 lookupIfaceExt (ExtPkg mod occ) = lookupOrig mod occ
334 lookupIfaceExt (HomePkg mod occ _) = lookupHomePackage mod occ
335 lookupIfaceExt (LocalTop occ) = lookupIfaceTop occ
336 lookupIfaceExt (LocalTopSub occ _) = lookupIfaceTop occ
338 lookupIfaceTop :: OccName -> IfL Name
339 -- Look up a top-level name from the current Iface module
341 = do { env <- getLclEnv; lookupOrig (if_mod env) occ }
343 lookupHomePackage :: ModuleName -> OccName -> IfL Name
344 lookupHomePackage mod_name occ
345 = do { env <- getLclEnv;
346 ; let this_pkg = modulePackageId (if_mod env)
347 ; lookupOrig (mkModule this_pkg mod_name) occ }
349 newIfaceName :: OccName -> IfL Name
351 = do { uniq <- newUnique
352 ; return $! mkInternalName uniq occ noSrcLoc }
354 newIfaceNames :: [OccName] -> IfL [Name]
356 = do { uniqs <- newUniqueSupply
357 ; return [ mkInternalName uniq occ noSrcLoc
358 | (occ,uniq) <- occs `zip` uniqsFromSupply uniqs] }