[project @ 2004-12-02 17:18:15 by simonpj]
[ghc-hetmet.git] / ghc / compiler / iface / IfaceEnv.lhs
1 (c) The University of Glasgow 2002
2
3 \begin{code}
4 module IfaceEnv (
5         newGlobalBinder, newIPName, newImplicitBinder, 
6         lookupIfaceTop, lookupIfaceExt,
7         lookupOrig, lookupIfaceTc,
8         newIfaceName, newIfaceNames,
9         extendIfaceIdEnv, extendIfaceTyVarEnv,
10         tcIfaceLclId,     tcIfaceTyVar, 
11
12         -- Name-cache stuff
13         allocateGlobalBinder, initNameCache, 
14    ) where
15
16 #include "HsVersions.h"
17
18 import TcRnMonad
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
31 import OccName          ( OccName, isTupleOcc_maybe, tcName, dataName,
32                           lookupOccEnv, unitOccEnv, extendOccEnv, extendOccEnvList )
33 import PrelNames        ( gHC_PRIM, pREL_TUP )
34 import Module           ( Module, emptyModuleEnv, 
35                           lookupModuleEnv, extendModuleEnv_C )
36 import UniqSupply       ( UniqSupply, splitUniqSupply, uniqFromSupply, uniqsFromSupply )
37 import FiniteMap        ( emptyFM, lookupFM, addToFM )
38 import BasicTypes       ( IPName(..), mapIPName )
39 import SrcLoc           ( SrcLoc, noSrcLoc )
40 import Maybes           ( orElse )
41
42 import Outputable
43 \end{code}
44
45
46 %*********************************************************
47 %*                                                      *
48         Allocating new Names in the Name Cache
49 %*                                                      *
50 %*********************************************************
51
52 \begin{code}
53 newGlobalBinder :: Module -> OccName -> Maybe Name -> SrcLoc -> TcRnIf a b Name
54 -- Used for source code and interface files, to make the
55 -- Name for a thing, given its Module and OccName
56 --
57 -- The cache may already already have a binding for this thing,
58 -- because we may have seen an occurrence before, but now is the
59 -- moment when we know its Module and SrcLoc in their full glory
60
61 newGlobalBinder mod occ mb_parent loc
62   = do  { mod `seq` occ `seq` return () -- See notes with lookupOrig_help
63         ; name_supply <- getNameCache
64         ; let (name_supply', name) = allocateGlobalBinder 
65                                         name_supply mod occ
66                                         mb_parent loc
67         ; setNameCache name_supply'
68         ; return name }
69
70 allocateGlobalBinder
71   :: NameCache 
72   -> Module -> OccName -> Maybe Name -> SrcLoc 
73   -> (NameCache, Name)
74 allocateGlobalBinder name_supply mod occ mb_parent loc
75   = case lookupOrigNameCache (nsNames name_supply) mod occ of
76         -- A hit in the cache!  We are at the binding site of the name.
77         -- This is the moment when we know the defining Module and SrcLoc
78         -- of the Name, so we set these fields in the Name we return.
79         --
80         -- This is essential, to get the right Module in a Name.
81         -- Also: then (bogus) multiple bindings of the same Name
82         --              get different SrcLocs can can be reported as such.
83         --
84         -- Possible other reason: it might be in the cache because we
85         --      encountered an occurrence before the binding site for an
86         --      implicitly-imported Name.  Perhaps the current SrcLoc is
87         --      better... but not really: it'll still just say 'imported'
88         --
89         -- IMPORTANT: Don't mess with wired-in names.  
90         --            Their wired-in-ness is in their NameSort
91         --            and their Module is correct.
92
93         Just name | isWiredInName name -> (name_supply, name)
94                   | otherwise -> (new_name_supply, name')
95                   where
96                     uniq      = nameUnique name
97                     name'     = mkExternalName uniq mod occ mb_parent loc
98                     new_cache = extend_name_cache (nsNames name_supply) mod occ name'
99                     new_name_supply = name_supply {nsNames = new_cache}              
100
101         -- Miss in the cache!
102         -- Build a completely new Name, and put it in the cache
103         Nothing -> (new_name_supply, name)
104                 where
105                   (us', us1)      = splitUniqSupply (nsUniqs name_supply)
106                   uniq            = uniqFromSupply us1
107                   name            = mkExternalName uniq mod occ mb_parent loc
108                   new_cache       = extend_name_cache (nsNames name_supply) mod occ name
109                   new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
110
111
112 newImplicitBinder :: Name                       -- Base name
113                   -> (OccName -> OccName)       -- Occurrence name modifier
114                   -> TcRnIf m n Name            -- Implicit name
115 -- Called in BuildTyCl to allocate the implicit binders of type/class decls
116 -- For source type/class decls, this is the first occurrence
117 -- For iface ones, the LoadIface has alrady allocated a suitable name in the cache
118 --
119 -- An *implicit* name has the base-name as parent
120 newImplicitBinder base_name mk_sys_occ
121   = newGlobalBinder (nameModule base_name)
122                     (mk_sys_occ (nameOccName base_name))
123                     (Just parent_name)
124                     (nameSrcLoc base_name)    
125   where
126     parent_name = case nameParent_maybe base_name of
127                     Just parent_name  -> parent_name
128                     Nothing           -> base_name
129
130 lookupOrig :: Module -> OccName -> TcRnIf a b Name
131 -- Even if we get a miss in the original-name cache, we 
132 -- make a new External Name. 
133 -- We fake up 
134 --      Module to AnotherPackage
135 --      SrcLoc to noSrcLoc
136 --      Parent no Nothing
137 -- They'll be overwritten, in due course, by LoadIface.loadDecl.
138
139 lookupOrig mod occ 
140   = do  {       -- First ensure that mod and occ are evaluated
141                 -- If not, chaos can ensue:
142                 --      we read the name-cache
143                 --      then pull on mod (say)
144                 --      which does some stuff that modifies the name cache
145                 -- This did happen, with tycon_mod in TcIface.tcIfaceAlt (DataAlt..)
146           mod `seq` occ `seq` return () 
147     
148         ; name_supply <- getNameCache
149         ; case lookupOrigNameCache (nsNames name_supply) mod occ of {
150               Just name -> returnM name ;
151               Nothing   -> do 
152
153         { let { (us', us1)      = splitUniqSupply (nsUniqs name_supply)
154               ; uniq            = uniqFromSupply us1
155               ; name            = mkExternalName uniq mod occ Nothing noSrcLoc
156               ; new_cache       = extend_name_cache (nsNames name_supply) mod occ name
157               ; new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
158           }
159         ; setNameCache new_name_supply
160         ; return name }
161     }}
162
163 newIPName :: IPName OccName -> TcRnIf m n (IPName Name)
164 newIPName occ_name_ip
165   = getNameCache                `thenM` \ name_supply ->
166     let
167         ipcache = nsIPs name_supply
168     in
169     case lookupFM ipcache key of
170         Just name_ip -> returnM name_ip
171         Nothing      -> setNameCache new_ns     `thenM_`
172                         returnM name_ip
173                   where
174                      (us', us1)  = splitUniqSupply (nsUniqs name_supply)
175                      uniq        = uniqFromSupply us1
176                      name_ip     = mapIPName (mkIPName uniq) occ_name_ip
177                      new_ipcache = addToFM ipcache key name_ip
178                      new_ns      = name_supply {nsUniqs = us', nsIPs = new_ipcache}
179     where 
180         key = occ_name_ip       -- Ensures that ?x and %x get distinct Names
181 \end{code}
182
183         Local helper functions (not exported)
184
185 \begin{code}
186 lookupOrigNameCache :: OrigNameCache -> Module -> OccName -> Maybe Name
187 lookupOrigNameCache nc mod occ
188   | mod == pREL_TUP || mod == gHC_PRIM,         -- Boxed tuples from one, 
189     Just tup_info <- isTupleOcc_maybe occ       -- unboxed from the other
190   =     -- Special case for tuples; there are too many
191         -- of them to pre-populate the original-name cache
192     Just (mk_tup_name tup_info)
193   where
194     mk_tup_name (ns, boxity, arity)
195         | ns == tcName   = tyConName (tupleTyCon boxity arity)
196         | ns == dataName = dataConName (tupleCon boxity arity)
197         | otherwise      = varName (dataConWorkId (tupleCon boxity arity))
198
199 lookupOrigNameCache nc mod occ  -- The normal case
200   = case lookupModuleEnv nc mod of
201         Nothing      -> Nothing
202         Just occ_env -> lookupOccEnv occ_env occ
203
204 extendOrigNameCache :: OrigNameCache -> Name -> OrigNameCache
205 extendOrigNameCache nc name 
206   = extend_name_cache nc (nameModule name) (nameOccName name) name
207
208 extend_name_cache :: OrigNameCache -> Module -> OccName -> Name -> OrigNameCache
209 extend_name_cache nc mod occ name
210   = extendModuleEnv_C combine nc mod (unitOccEnv occ name)
211   where
212     combine occ_env _ = extendOccEnv occ_env occ name
213
214 getNameCache :: TcRnIf a b NameCache
215 getNameCache = do { HscEnv { hsc_NC = nc_var } <- getTopEnv; 
216                     readMutVar nc_var }
217
218 setNameCache :: NameCache -> TcRnIf a b ()
219 setNameCache nc = do { HscEnv { hsc_NC = nc_var } <- getTopEnv; 
220                        writeMutVar nc_var nc }
221 \end{code}
222
223
224 \begin{code}
225 initNameCache :: UniqSupply -> [Name] -> NameCache
226 initNameCache us names
227   = NameCache { nsUniqs = us,
228                 nsNames = initOrigNames names,
229                 nsIPs   = emptyFM }
230
231 initOrigNames :: [Name] -> OrigNameCache
232 initOrigNames names = foldl extendOrigNameCache emptyModuleEnv names
233 \end{code}
234
235
236
237 %************************************************************************
238 %*                                                                      *
239                 Type variables and local Ids
240 %*                                                                      *
241 %************************************************************************
242
243 \begin{code}
244 tcIfaceLclId :: OccName -> IfL Id
245 tcIfaceLclId occ
246   = do  { lcl <- getLclEnv
247         ; return (lookupOccEnv (if_id_env lcl) occ
248                   `orElse` 
249                   pprPanic "tcIfaceLclId" (ppr occ)) }
250
251 extendIfaceIdEnv :: [Id] -> IfL a -> IfL a
252 extendIfaceIdEnv ids thing_inside
253   = do  { env <- getLclEnv
254         ; let { id_env' = extendOccEnvList (if_id_env env) pairs
255               ; pairs   = [(getOccName id, id) | id <- ids] }
256         ; setLclEnv (env { if_id_env = id_env' }) thing_inside }
257
258
259 tcIfaceTyVar :: OccName -> IfL TyVar
260 tcIfaceTyVar occ
261   = do  { lcl <- getLclEnv
262         ; return (lookupOccEnv (if_tv_env lcl) occ
263                   `orElse`
264                   pprPanic "tcIfaceTyVar" (ppr occ)) }
265
266 extendIfaceTyVarEnv :: [TyVar] -> IfL a -> IfL a
267 extendIfaceTyVarEnv tyvars thing_inside
268   = do  { env <- getLclEnv
269         ; let { tv_env' = extendOccEnvList (if_tv_env env) pairs
270               ; pairs   = [(getOccName tv, tv) | tv <- tyvars] }
271         ; setLclEnv (env { if_tv_env = tv_env' }) thing_inside }
272 \end{code}
273
274
275 %************************************************************************
276 %*                                                                      *
277                 Getting from RdrNames to Names
278 %*                                                                      *
279 %************************************************************************
280
281 \begin{code}
282 lookupIfaceTc :: IfaceTyCon -> IfL Name
283 lookupIfaceTc (IfaceTc ext) = lookupIfaceExt ext
284 lookupIfaceTc other_tc      = return (ifaceTyConName other_tc)
285
286 lookupIfaceExt :: IfaceExtName -> IfL Name
287 lookupIfaceExt (ExtPkg  mod occ)   = lookupOrig mod occ
288 lookupIfaceExt (HomePkg mod occ _) = lookupOrig mod occ
289 lookupIfaceExt (LocalTop occ)      = lookupIfaceTop occ
290 lookupIfaceExt (LocalTopSub occ _) = lookupIfaceTop occ
291
292 lookupIfaceTop :: OccName -> IfL Name
293 -- Look up a top-level name from the current Iface module
294 lookupIfaceTop occ
295   = do  { env <- getLclEnv; lookupOrig (if_mod env) occ }
296
297 newIfaceName :: OccName -> IfL Name
298 newIfaceName occ
299   = do  { uniq <- newUnique
300         ; return (mkInternalName uniq occ noSrcLoc) }
301
302 newIfaceNames :: [OccName] -> IfL [Name]
303 newIfaceNames occs
304   = do  { uniqs <- newUniqueSupply
305         ; return [ mkInternalName uniq occ noSrcLoc
306                  | (occ,uniq) <- occs `zip` uniqsFromSupply uniqs] }
307 \end{code}