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,
13 allocateGlobalBinder, initNameCache
16 #include "HsVersions.h"
19 import IfaceType ( IfaceExtName(..), IfaceTyCon(..), ifaceTyConName )
20 import TysWiredIn ( tupleTyCon, tupleCon )
21 import HscTypes ( NameCache(..), HscEnv(..), OrigNameCache )
22 import TyCon ( TyCon, tyConName )
23 import DataCon ( dataConWorkId, dataConName )
24 import Var ( TyVar, Id, varName )
25 import Name ( Name, nameUnique, nameModule,
26 nameOccName, nameSrcLoc,
27 getOccName, nameParent_maybe,
28 isWiredInName, mkIPName,
29 mkExternalName, mkInternalName )
30 import OccName ( OccName, isTupleOcc_maybe, tcName, dataName,
31 lookupOccEnv, unitOccEnv, extendOccEnv, extendOccEnvList )
32 import PrelNames ( gHC_PRIM_Name, pREL_TUP_Name )
33 import Module ( Module, ModuleName, moduleName, mkPackageModule,
34 emptyModuleEnv, lookupModuleEnvByName, extendModuleEnv_C )
35 import UniqSupply ( UniqSupply, splitUniqSupply, uniqFromSupply, uniqsFromSupply )
36 import FiniteMap ( emptyFM, lookupFM, addToFM )
37 import BasicTypes ( IPName(..), mapIPName )
38 import SrcLoc ( SrcLoc, noSrcLoc )
39 import Maybes ( orElse )
45 %*********************************************************
47 Allocating new Names in the Name Cache
49 %*********************************************************
52 newGlobalBinder :: Module -> OccName -> Maybe Name -> SrcLoc -> TcRnIf a b Name
53 -- Used for source code and interface files, to make the
54 -- Name for a thing, given its Module and OccName
56 -- The cache may already already have a binding for this thing,
57 -- because we may have seen an occurrence before, but now is the
58 -- moment when we know its Module and SrcLoc in their full glory
60 newGlobalBinder mod occ mb_parent loc
61 = do { mod `seq` occ `seq` return () -- See notes with lookupOrig_help
62 ; name_supply <- getNameCache
63 ; let (name_supply', name) = allocateGlobalBinder
66 ; setNameCache name_supply'
71 -> Module -> OccName -> Maybe Name -> SrcLoc
73 allocateGlobalBinder name_supply mod occ mb_parent loc
74 = case lookupOrigNameCache (nsNames name_supply) (moduleName mod) occ of
75 -- A hit in the cache! We are at the binding site of the name.
76 -- This is the moment when we know the defining Module and SrcLoc
77 -- of the Name, so we set these fields in the Name we return.
79 -- This is essential, to get the right Module in a Name.
80 -- Also: then (bogus) multiple bindings of the same Name
81 -- get different SrcLocs can can be reported as such.
83 -- Possible other reason: it might be in the cache because we
84 -- encountered an occurrence before the binding site for an
85 -- implicitly-imported Name. Perhaps the current SrcLoc is
86 -- better... but not really: it'll still just say 'imported'
88 -- IMPORTANT: Don't mess with wired-in names.
89 -- Their wired-in-ness is in their NameSort
90 -- and their Module is correct.
92 Just name | isWiredInName name -> (name_supply, name)
93 | otherwise -> (new_name_supply, name')
95 uniq = nameUnique name
96 name' = mkExternalName uniq mod occ mb_parent loc
97 new_cache = extend_name_cache (nsNames name_supply) mod occ name'
98 new_name_supply = name_supply {nsNames = new_cache}
100 -- Miss in the cache!
101 -- Build a completely new Name, and put it in the cache
102 Nothing -> (new_name_supply, name)
104 (us', us1) = splitUniqSupply (nsUniqs name_supply)
105 uniq = uniqFromSupply us1
106 name = mkExternalName uniq mod occ mb_parent loc
107 new_cache = extend_name_cache (nsNames name_supply) mod occ name
108 new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
111 newImplicitBinder :: Name -- Base name
112 -> (OccName -> OccName) -- Occurrence name modifier
113 -> TcRnIf m n Name -- Implicit name
114 -- Called in BuildTyCl to allocate the implicit binders of type/class decls
115 -- For source type/class decls, this is the first occurrence
116 -- For iface ones, the LoadIface has alrady allocated a suitable name in the cache
118 -- An *implicit* name has the base-name as parent
119 newImplicitBinder base_name mk_sys_occ
120 = newGlobalBinder (nameModule base_name)
121 (mk_sys_occ (nameOccName base_name))
123 (nameSrcLoc base_name)
125 parent_name = case nameParent_maybe base_name of
126 Just parent_name -> parent_name
129 lookupOrig :: ModuleName -> OccName -> TcRnIf a b Name
130 -- This one starts with a ModuleName, not a Module, because
131 -- we may be simply looking at an occurrence M.x in an interface file.
132 -- We may enounter this well before finding the binding site for M.x
134 -- So, even if we get a miss in the original-name cache, we
135 -- make a new External Name.
137 -- Module to AnotherPackage
138 -- SrcLoc to noSrcLoc
140 -- They'll be overwritten, in due course, by LoadIface.loadDecl.
142 lookupOrig mod_name occ
143 = do { -- First ensure that mod_name and occ are evaluated
144 -- If not, chaos can ensue:
145 -- we read the name-cache
146 -- then pull on mod (say)
147 -- which does some stuff that modifies the name cache
148 -- This did happen, with tycon_mod in TcIface.tcIfaceAlt (DataAlt..)
149 mod `seq` occ `seq` return ()
151 ; name_supply <- getNameCache
152 ; case lookupOrigNameCache (nsNames name_supply) mod_name occ of {
153 Just name -> returnM name ;
156 { let { (us', us1) = splitUniqSupply (nsUniqs name_supply)
157 ; uniq = uniqFromSupply us1
158 ; name = mkExternalName uniq tmp_mod occ Nothing noSrcLoc
159 ; new_cache = extend_name_cache (nsNames name_supply) tmp_mod occ name
160 ; new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
161 ; tmp_mod = mkPackageModule mod_name
162 -- Guess at the package-ness for now, becuase we don't know whether
163 -- this imported module is from the home package or not.
164 -- If we ever need it, we'll open its interface, and update the cache
165 -- with a better name (newGlobalBinder)
167 ; setNameCache new_name_supply
171 newIPName :: IPName OccName -> TcRnIf m n (IPName Name)
172 newIPName occ_name_ip
173 = getNameCache `thenM` \ name_supply ->
175 ipcache = nsIPs name_supply
177 case lookupFM ipcache key of
178 Just name_ip -> returnM name_ip
179 Nothing -> setNameCache new_ns `thenM_`
182 (us', us1) = splitUniqSupply (nsUniqs name_supply)
183 uniq = uniqFromSupply us1
184 name_ip = mapIPName (mkIPName uniq) occ_name_ip
185 new_ipcache = addToFM ipcache key name_ip
186 new_ns = name_supply {nsUniqs = us', nsIPs = new_ipcache}
188 key = occ_name_ip -- Ensures that ?x and %x get distinct Names
191 Local helper functions (not exported)
194 lookupOrigNameCache :: OrigNameCache -> ModuleName -> OccName -> Maybe Name
195 lookupOrigNameCache nc mod_name occ
196 | mod_name == pREL_TUP_Name || mod_name == gHC_PRIM_Name, -- Boxed tuples from one,
197 Just tup_info <- isTupleOcc_maybe occ -- unboxed from the other
198 = -- Special case for tuples; there are too many
199 -- of them to pre-populate the original-name cache
200 Just (mk_tup_name tup_info)
202 mk_tup_name (ns, boxity, arity)
203 | ns == tcName = tyConName (tupleTyCon boxity arity)
204 | ns == dataName = dataConName (tupleCon boxity arity)
205 | otherwise = varName (dataConWorkId (tupleCon boxity arity))
207 lookupOrigNameCache nc mod_name occ -- The normal case
208 = case lookupModuleEnvByName nc mod_name of
210 Just occ_env -> lookupOccEnv occ_env occ
212 extendOrigNameCache :: OrigNameCache -> Name -> OrigNameCache
213 extendOrigNameCache nc name
214 = extend_name_cache nc (nameModule name) (nameOccName name) name
216 extend_name_cache :: OrigNameCache -> Module -> OccName -> Name -> OrigNameCache
217 extend_name_cache nc mod occ name
218 = extendModuleEnv_C combine nc mod (unitOccEnv occ name)
220 combine occ_env _ = extendOccEnv occ_env occ name
222 getNameCache :: TcRnIf a b NameCache
223 getNameCache = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
226 setNameCache :: NameCache -> TcRnIf a b ()
227 setNameCache nc = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
228 writeMutVar nc_var nc }
233 initNameCache :: UniqSupply -> [Name] -> NameCache
234 initNameCache us names
235 = NameCache { nsUniqs = us,
236 nsNames = initOrigNames names,
239 initOrigNames :: [Name] -> OrigNameCache
240 initOrigNames names = foldl extendOrigNameCache emptyModuleEnv names
245 %************************************************************************
247 Type variables and local Ids
249 %************************************************************************
252 tcIfaceLclId :: OccName -> IfL Id
254 = do { lcl <- getLclEnv
255 ; return (lookupOccEnv (if_id_env lcl) occ
257 pprPanic "tcIfaceLclId" (ppr occ)) }
259 extendIfaceIdEnv :: [Id] -> IfL a -> IfL a
260 extendIfaceIdEnv ids thing_inside
261 = do { env <- getLclEnv
262 ; let { id_env' = extendOccEnvList (if_id_env env) pairs
263 ; pairs = [(getOccName id, id) | id <- ids] }
264 ; setLclEnv (env { if_id_env = id_env' }) thing_inside }
267 tcIfaceTyVar :: OccName -> IfL TyVar
269 = do { lcl <- getLclEnv
270 ; return (lookupOccEnv (if_tv_env lcl) occ
272 pprPanic "tcIfaceTyVar" (ppr occ)) }
274 extendIfaceTyVarEnv :: [TyVar] -> IfL a -> IfL a
275 extendIfaceTyVarEnv tyvars thing_inside
276 = do { env <- getLclEnv
277 ; let { tv_env' = extendOccEnvList (if_tv_env env) pairs
278 ; pairs = [(getOccName tv, tv) | tv <- tyvars] }
279 ; setLclEnv (env { if_tv_env = tv_env' }) thing_inside }
283 %************************************************************************
285 Getting from RdrNames to Names
287 %************************************************************************
290 lookupIfaceTc :: IfaceTyCon -> IfL Name
291 lookupIfaceTc (IfaceTc ext) = lookupIfaceExt ext
292 lookupIfaceTc other_tc = return (ifaceTyConName other_tc)
294 lookupIfaceExt :: IfaceExtName -> IfL Name
295 lookupIfaceExt (ExtPkg mod occ) = lookupOrig mod occ
296 lookupIfaceExt (HomePkg mod occ _) = lookupOrig mod occ
297 lookupIfaceExt (LocalTop occ) = lookupIfaceTop occ
298 lookupIfaceExt (LocalTopSub occ _) = lookupIfaceTop occ
300 lookupIfaceTop :: OccName -> IfL Name
301 -- Look up a top-level name from the current Iface module
303 = do { env <- getLclEnv; lookupOrig (if_mod env) occ }
305 newIfaceName :: OccName -> IfL Name
307 = do { uniq <- newUnique
308 ; return (mkInternalName uniq occ noSrcLoc) }
310 newIfaceNames :: [OccName] -> IfL [Name]
312 = do { uniqs <- newUniqueSupply
313 ; return [ mkInternalName uniq occ noSrcLoc
314 | (occ,uniq) <- occs `zip` uniqsFromSupply uniqs] }