1 (c) The University of Glasgow 2002
5 newGlobalBinder, newIPName, newImplicitBinder,
6 lookupIfaceTop, lookupIfaceExt,
7 lookupOrig, lookupIfaceTc,
8 newIfaceName, newIfaceNames,
9 extendIfaceIdEnv, extendIfaceTyVarEnv, refineIfaceIdEnv,
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 Unify ( TypeRefinement )
28 import DataCon ( dataConWorkId, dataConName )
29 import Var ( TyVar, Id, varName, setIdType, idType )
30 import Name ( Name, nameUnique, nameModule,
31 nameOccName, nameSrcLoc,
32 getOccName, nameParent_maybe,
33 isWiredInName, mkIPName,
34 mkExternalName, mkInternalName )
35 import NameSet ( NameSet, emptyNameSet, addListToNameSet )
36 import OccName ( OccName, isTupleOcc_maybe, tcName, dataName, mapOccEnv, occNameFS,
37 lookupOccEnv, unitOccEnv, extendOccEnv, extendOccEnvList )
38 import PrelNames ( gHC_PRIM, pREL_TUP )
39 import Module ( Module, emptyModuleEnv,
40 lookupModuleEnv, extendModuleEnv_C )
41 import UniqFM ( lookupUFM, addListToUFM )
42 import FastString ( FastString )
43 import UniqSupply ( UniqSupply, splitUniqSupply, uniqFromSupply, uniqsFromSupply )
44 import FiniteMap ( emptyFM, lookupFM, addToFM )
45 import BasicTypes ( IPName(..), mapIPName )
46 import SrcLoc ( SrcLoc, noSrcLoc )
47 import Maybes ( orElse )
53 %*********************************************************
55 Allocating new Names in the Name Cache
57 %*********************************************************
60 newGlobalBinder :: Module -> OccName -> Maybe Name -> SrcLoc -> TcRnIf a b Name
61 -- Used for source code and interface files, to make the
62 -- Name for a thing, given its Module and OccName
64 -- The cache may already already have a binding for this thing,
65 -- because we may have seen an occurrence before, but now is the
66 -- moment when we know its Module and SrcLoc in their full glory
68 newGlobalBinder mod occ mb_parent loc
69 = do { mod `seq` occ `seq` return () -- See notes with lookupOrig_help
70 -- ; traceIf (text "newGlobalBinder" <+> ppr mod <+> ppr occ <+> ppr loc)
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) 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 parent and SrcLoc
86 -- of the Name, so we set these fields in the Name we return.
88 -- Then (bogus) multiple bindings of the same Name
89 -- get different SrcLocs can can be reported as such.
91 -- Possible other reason: it might be in the cache because we
92 -- encountered an occurrence before the binding site for an
93 -- implicitly-imported Name. Perhaps the current SrcLoc is
94 -- better... but not really: it'll still just say 'imported'
96 -- IMPORTANT: Don't mess with wired-in names.
97 -- Their wired-in-ness is in their NameSort
98 -- and their Module is correct.
100 Just name | isWiredInName name -> (name_supply, name)
101 | otherwise -> (new_name_supply, name')
103 uniq = nameUnique name
104 name' = mkExternalName uniq mod occ mb_parent loc
105 new_cache = extend_name_cache (nsNames name_supply) mod occ name'
106 new_name_supply = name_supply {nsNames = new_cache}
108 -- Miss in the cache!
109 -- Build a completely new Name, and put it in the cache
110 Nothing -> (new_name_supply, name)
112 (us', us1) = splitUniqSupply (nsUniqs name_supply)
113 uniq = uniqFromSupply us1
114 name = mkExternalName uniq mod occ mb_parent loc
115 new_cache = extend_name_cache (nsNames name_supply) mod occ name
116 new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
119 newImplicitBinder :: Name -- Base name
120 -> (OccName -> OccName) -- Occurrence name modifier
121 -> TcRnIf m n Name -- Implicit name
122 -- Called in BuildTyCl to allocate the implicit binders of type/class decls
123 -- For source type/class decls, this is the first occurrence
124 -- For iface ones, the LoadIface has alrady allocated a suitable name in the cache
126 -- An *implicit* name has the base-name as parent
127 newImplicitBinder base_name mk_sys_occ
128 = newGlobalBinder (nameModule base_name)
129 (mk_sys_occ (nameOccName base_name))
131 (nameSrcLoc base_name)
133 parent_name = case nameParent_maybe base_name of
134 Just parent_name -> parent_name
137 ifaceExportNames :: [IfaceExport] -> TcRnIf gbl lcl NameSet
138 ifaceExportNames exports
139 = foldlM do_one emptyNameSet exports
141 do_one acc (mod, exports) = foldlM (do_avail mod) acc exports
142 do_avail mod acc avail = do { ns <- lookupAvail mod avail
143 ; return (addListToNameSet acc ns) }
145 lookupAvail :: Module -> GenAvailInfo OccName -> TcRnIf a b [Name]
146 -- Find all the names arising from an import
147 -- Make sure the parent info is correct, even though we may not
148 -- yet have read the interface for this module
149 lookupAvail mod (Avail n) = do { n' <- lookupOrig mod n;
151 lookupAvail mod (AvailTC p_occ occs)
152 = do { p_name <- lookupOrig mod p_occ
153 ; let lookup_sub occ | occ == p_occ = return p_name
154 | otherwise = lookup_orig mod occ (Just p_name)
155 ; mappM lookup_sub occs }
156 -- Remember that 'occs' is all the exported things, including
157 -- the parent. It's possible to export just class ops without
158 -- the class, via C( op ). If the class was exported too we'd
161 -- The use of lookupOrigSub here (rather than lookupOrig)
162 -- ensures that the subordinate names record their parent;
163 -- and that in turn ensures that the GlobalRdrEnv
164 -- has the correct parent for all the names in its range.
165 -- For imported things, we may only suck in the interface later, if ever.
166 -- Reason for all this:
167 -- Suppose module M exports type A.T, and constructor A.MkT
168 -- Then, we know that A.MkT is a subordinate name of A.T,
169 -- even though we aren't at the binding site of A.T
170 -- And it's important, because we may simply re-export A.T
171 -- without ever sucking in the declaration itself.
174 lookupOrig :: Module -> OccName -> TcRnIf a b Name
175 -- Even if we get a miss in the original-name cache, we
176 -- make a new External Name.
178 -- SrcLoc to noSrcLoc
180 -- They'll be overwritten, in due course, by LoadIface.loadDecl.
181 lookupOrig mod occ = lookup_orig mod occ Nothing
183 lookup_orig :: Module -> OccName -> Maybe Name -> TcRnIf a b Name
184 -- Used when we know the parent of the thing we are looking up
185 lookup_orig mod occ mb_parent
186 = do { -- First ensure that mod and occ are evaluated
187 -- If not, chaos can ensue:
188 -- we read the name-cache
189 -- then pull on mod (say)
190 -- which does some stuff that modifies the name cache
191 -- This did happen, with tycon_mod in TcIface.tcIfaceAlt (DataAlt..)
192 mod `seq` occ `seq` return ()
194 ; name_supply <- getNameCache
195 ; case lookupOrigNameCache (nsNames name_supply) mod occ of {
196 Just name -> returnM name ;
199 { let { (us', us1) = splitUniqSupply (nsUniqs name_supply)
200 ; uniq = uniqFromSupply us1
201 ; name = mkExternalName uniq mod occ mb_parent noSrcLoc
202 ; new_cache = extend_name_cache (nsNames name_supply) mod occ name
203 ; new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
205 ; setNameCache new_name_supply
209 newIPName :: IPName OccName -> TcRnIf m n (IPName Name)
210 newIPName occ_name_ip
211 = getNameCache `thenM` \ name_supply ->
213 ipcache = nsIPs name_supply
215 case lookupFM ipcache key of
216 Just name_ip -> returnM name_ip
217 Nothing -> setNameCache new_ns `thenM_`
220 (us', us1) = splitUniqSupply (nsUniqs name_supply)
221 uniq = uniqFromSupply us1
222 name_ip = mapIPName (mkIPName uniq) occ_name_ip
223 new_ipcache = addToFM ipcache key name_ip
224 new_ns = name_supply {nsUniqs = us', nsIPs = new_ipcache}
226 key = occ_name_ip -- Ensures that ?x and %x get distinct Names
229 Local helper functions (not exported)
232 lookupOrigNameCache :: OrigNameCache -> Module -> OccName -> Maybe Name
233 lookupOrigNameCache nc mod occ
234 | mod == pREL_TUP || mod == gHC_PRIM, -- Boxed tuples from one,
235 Just tup_info <- isTupleOcc_maybe occ -- unboxed from the other
236 = -- Special case for tuples; there are too many
237 -- of them to pre-populate the original-name cache
238 Just (mk_tup_name tup_info)
240 mk_tup_name (ns, boxity, arity)
241 | ns == tcName = tyConName (tupleTyCon boxity arity)
242 | ns == dataName = dataConName (tupleCon boxity arity)
243 | otherwise = varName (dataConWorkId (tupleCon boxity arity))
245 lookupOrigNameCache nc mod occ -- The normal case
246 = case lookupModuleEnv nc mod of
248 Just occ_env -> lookupOccEnv occ_env occ
250 extendOrigNameCache :: OrigNameCache -> Name -> OrigNameCache
251 extendOrigNameCache nc name
252 = extend_name_cache nc (nameModule name) (nameOccName name) name
254 extend_name_cache :: OrigNameCache -> Module -> OccName -> Name -> OrigNameCache
255 extend_name_cache nc mod occ name
256 = extendModuleEnv_C combine nc mod (unitOccEnv occ name)
258 combine occ_env _ = extendOccEnv occ_env occ name
260 getNameCache :: TcRnIf a b NameCache
261 getNameCache = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
264 setNameCache :: NameCache -> TcRnIf a b ()
265 setNameCache nc = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
266 writeMutVar nc_var nc }
271 initNameCache :: UniqSupply -> [Name] -> NameCache
272 initNameCache us names
273 = NameCache { nsUniqs = us,
274 nsNames = initOrigNames names,
277 initOrigNames :: [Name] -> OrigNameCache
278 initOrigNames names = foldl extendOrigNameCache emptyModuleEnv names
283 %************************************************************************
285 Type variables and local Ids
287 %************************************************************************
290 tcIfaceLclId :: FastString -> IfL Id
292 = do { lcl <- getLclEnv
293 ; case (lookupUFM (if_id_env lcl) occ) of
294 Just ty_var -> return ty_var
295 Nothing -> failIfM (text "Iface id out of scope: " <+> ppr occ)
298 refineIfaceIdEnv :: TypeRefinement -> IfL a -> IfL a
299 refineIfaceIdEnv (tv_subst, _) thing_inside
300 = do { env <- getLclEnv
301 ; let { id_env' = mapOccEnv refine_id (if_id_env env)
302 ; refine_id id = setIdType id (substTy subst (idType id))
303 ; subst = mkOpenTvSubst tv_subst }
304 ; setLclEnv (env { if_id_env = id_env' }) thing_inside }
306 extendIfaceIdEnv :: [Id] -> IfL a -> IfL a
307 extendIfaceIdEnv ids thing_inside
308 = do { env <- getLclEnv
309 ; let { id_env' = addListToUFM (if_id_env env) pairs
310 ; pairs = [(occNameFS (getOccName id), id) | id <- ids] }
311 ; setLclEnv (env { if_id_env = id_env' }) thing_inside }
314 tcIfaceTyVar :: FastString -> IfL TyVar
316 = do { lcl <- getLclEnv
317 ; case (lookupUFM (if_tv_env lcl) occ) of
318 Just ty_var -> return ty_var
319 Nothing -> failIfM (text "Iface type variable out of scope: " <+> ppr occ)
322 extendIfaceTyVarEnv :: [TyVar] -> IfL a -> IfL a
323 extendIfaceTyVarEnv tyvars thing_inside
324 = do { env <- getLclEnv
325 ; let { tv_env' = addListToUFM (if_tv_env env) pairs
326 ; pairs = [(occNameFS (getOccName tv), tv) | tv <- tyvars] }
327 ; setLclEnv (env { if_tv_env = tv_env' }) thing_inside }
331 %************************************************************************
333 Getting from RdrNames to Names
335 %************************************************************************
338 lookupIfaceTc :: IfaceTyCon -> IfL Name
339 lookupIfaceTc (IfaceTc ext) = lookupIfaceExt ext
340 lookupIfaceTc other_tc = return (ifaceTyConName other_tc)
342 lookupIfaceExt :: IfaceExtName -> IfL Name
343 lookupIfaceExt (ExtPkg mod occ) = lookupOrig mod occ
344 lookupIfaceExt (HomePkg mod occ _) = lookupOrig mod occ
345 lookupIfaceExt (LocalTop occ) = lookupIfaceTop occ
346 lookupIfaceExt (LocalTopSub occ _) = lookupIfaceTop occ
348 lookupIfaceTop :: OccName -> IfL Name
349 -- Look up a top-level name from the current Iface module
351 = do { env <- getLclEnv; lookupOrig (if_mod env) occ }
353 newIfaceName :: OccName -> IfL Name
355 = do { uniq <- newUnique
356 ; return (mkInternalName uniq occ noSrcLoc) }
358 newIfaceNames :: [OccName] -> IfL [Name]
360 = do { uniqs <- newUniqueSupply
361 ; return [ mkInternalName uniq occ noSrcLoc
362 | (occ,uniq) <- occs `zip` uniqsFromSupply uniqs] }