PageRenderTime 79ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 1ms

/compiler/iface/IfaceEnv.lhs

https://bitbucket.org/carter/ghc
Haskell | 310 lines | 204 code | 46 blank | 60 comment | 5 complexity | 0bf2ed038263f7b2ad09dc6217e8a651 MD5 | raw file
  1. (c) The University of Glasgow 2002-2006
  2. \begin{code}
  3. {-# OPTIONS -fno-warn-tabs #-}
  4. -- The above warning supression flag is a temporary kludge.
  5. -- While working on this module you are encouraged to remove it and
  6. -- detab the module (please do the detabbing in a separate patch). See
  7. -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
  8. -- for details
  9. module IfaceEnv (
  10. newGlobalBinder, newImplicitBinder,
  11. lookupIfaceTop,
  12. lookupOrig, lookupOrigNameCache, extendNameCache,
  13. newIfaceName, newIfaceNames,
  14. extendIfaceIdEnv, extendIfaceTyVarEnv,
  15. tcIfaceLclId, tcIfaceTyVar, lookupIfaceTyVar,
  16. ifaceExportNames,
  17. -- Name-cache stuff
  18. allocateGlobalBinder, initNameCache, updNameCache,
  19. getNameCache, mkNameCacheUpdater, NameCacheUpdater(..)
  20. ) where
  21. #include "HsVersions.h"
  22. import TcRnMonad
  23. import TysWiredIn
  24. import HscTypes
  25. import TyCon
  26. import Type
  27. import DataCon
  28. import Var
  29. import Name
  30. import Avail
  31. import PrelNames
  32. import Module
  33. import UniqFM
  34. import FastString
  35. import UniqSupply
  36. import SrcLoc
  37. import Util
  38. import Outputable
  39. import Exception ( evaluate )
  40. import Data.IORef ( atomicModifyIORef, readIORef )
  41. \end{code}
  42. %*********************************************************
  43. %* *
  44. Allocating new Names in the Name Cache
  45. %* *
  46. %*********************************************************
  47. \begin{code}
  48. newGlobalBinder :: Module -> OccName -> SrcSpan -> TcRnIf a b Name
  49. -- Used for source code and interface files, to make the
  50. -- Name for a thing, given its Module and OccName
  51. --
  52. -- The cache may already already have a binding for this thing,
  53. -- because we may have seen an occurrence before, but now is the
  54. -- moment when we know its Module and SrcLoc in their full glory
  55. newGlobalBinder mod occ loc
  56. = do mod `seq` occ `seq` return () -- See notes with lookupOrig
  57. -- traceIf (text "newGlobalBinder" <+> ppr mod <+> ppr occ <+> ppr loc)
  58. updNameCache $ \name_cache ->
  59. allocateGlobalBinder name_cache mod occ loc
  60. allocateGlobalBinder
  61. :: NameCache
  62. -> Module -> OccName -> SrcSpan
  63. -> (NameCache, Name)
  64. allocateGlobalBinder name_supply mod occ loc
  65. = case lookupOrigNameCache (nsNames name_supply) mod occ of
  66. -- A hit in the cache! We are at the binding site of the name.
  67. -- This is the moment when we know the SrcLoc
  68. -- of the Name, so we set this field in the Name we return.
  69. --
  70. -- Then (bogus) multiple bindings of the same Name
  71. -- get different SrcLocs can can be reported as such.
  72. --
  73. -- Possible other reason: it might be in the cache because we
  74. -- encountered an occurrence before the binding site for an
  75. -- implicitly-imported Name. Perhaps the current SrcLoc is
  76. -- better... but not really: it'll still just say 'imported'
  77. --
  78. -- IMPORTANT: Don't mess with wired-in names.
  79. -- Their wired-in-ness is in their NameSort
  80. -- and their Module is correct.
  81. Just name | isWiredInName name -> (name_supply, name)
  82. | mod /= iNTERACTIVE -> (new_name_supply, name')
  83. -- Note [interactive name cache]
  84. where
  85. uniq = nameUnique name
  86. name' = mkExternalName uniq mod occ loc
  87. new_cache = extendNameCache (nsNames name_supply) mod occ name'
  88. new_name_supply = name_supply {nsNames = new_cache}
  89. -- Miss in the cache!
  90. -- Build a completely new Name, and put it in the cache
  91. _ -> (new_name_supply, name)
  92. where
  93. (uniq, us') = takeUniqFromSupply (nsUniqs name_supply)
  94. name = mkExternalName uniq mod occ loc
  95. new_cache = extendNameCache (nsNames name_supply) mod occ name
  96. new_name_supply = name_supply {nsUniqs = us', nsNames = new_cache}
  97. {- Note [interactive name cache]
  98. In GHCi we always create Names with the same Module, ":Interactive".
  99. However, we want to be able to shadow older declarations with newer
  100. ones, and we don't want the Name cache giving us back the same Unique
  101. for the new Name as for the old, hence this special case.
  102. See also Note [Outputable Orig RdrName] in HscTypes.
  103. -}
  104. newImplicitBinder :: Name -- Base name
  105. -> (OccName -> OccName) -- Occurrence name modifier
  106. -> TcRnIf m n Name -- Implicit name
  107. -- Called in BuildTyCl to allocate the implicit binders of type/class decls
  108. -- For source type/class decls, this is the first occurrence
  109. -- For iface ones, the LoadIface has alrady allocated a suitable name in the cache
  110. newImplicitBinder base_name mk_sys_occ
  111. | Just mod <- nameModule_maybe base_name
  112. = newGlobalBinder mod occ loc
  113. | otherwise -- When typechecking a [d| decl bracket |],
  114. -- TH generates types, classes etc with Internal names,
  115. -- so we follow suit for the implicit binders
  116. = do { uniq <- newUnique
  117. ; return (mkInternalName uniq occ loc) }
  118. where
  119. occ = mk_sys_occ (nameOccName base_name)
  120. loc = nameSrcSpan base_name
  121. ifaceExportNames :: [IfaceExport] -> TcRnIf gbl lcl [AvailInfo]
  122. ifaceExportNames exports = return exports
  123. lookupOrig :: Module -> OccName -> TcRnIf a b Name
  124. lookupOrig mod occ
  125. = do { -- First ensure that mod and occ are evaluated
  126. -- If not, chaos can ensue:
  127. -- we read the name-cache
  128. -- then pull on mod (say)
  129. -- which does some stuff that modifies the name cache
  130. -- This did happen, with tycon_mod in TcIface.tcIfaceAlt (DataAlt..)
  131. mod `seq` occ `seq` return ()
  132. -- ; traceIf (text "lookup_orig" <+> ppr mod <+> ppr occ)
  133. ; updNameCache $ \name_cache ->
  134. case lookupOrigNameCache (nsNames name_cache) mod occ of {
  135. Just name -> (name_cache, name);
  136. Nothing ->
  137. case takeUniqFromSupply (nsUniqs name_cache) of {
  138. (uniq, us) ->
  139. let
  140. name = mkExternalName uniq mod occ noSrcSpan
  141. new_cache = extendNameCache (nsNames name_cache) mod occ name
  142. in (name_cache{ nsUniqs = us, nsNames = new_cache }, name)
  143. }}}
  144. \end{code}
  145. %************************************************************************
  146. %* *
  147. Name cache access
  148. %* *
  149. %************************************************************************
  150. \begin{code}
  151. lookupOrigNameCache :: OrigNameCache -> Module -> OccName -> Maybe Name
  152. lookupOrigNameCache _ mod occ
  153. -- Don't need to mention gHC_UNIT here because it is explicitly
  154. -- included in TysWiredIn.wiredInTyCons
  155. | mod == gHC_TUPLE || mod == gHC_PRIM, -- Boxed tuples from one,
  156. Just tup_info <- isTupleOcc_maybe occ -- unboxed from the other
  157. = -- Special case for tuples; there are too many
  158. -- of them to pre-populate the original-name cache
  159. Just (mk_tup_name tup_info)
  160. where
  161. mk_tup_name (ns, sort, arity)
  162. | ns == tcName = tyConName (tupleTyCon sort arity)
  163. | ns == dataName = dataConName (tupleCon sort arity)
  164. | otherwise = Var.varName (dataConWorkId (tupleCon sort arity))
  165. lookupOrigNameCache nc mod occ -- The normal case
  166. = case lookupModuleEnv nc mod of
  167. Nothing -> Nothing
  168. Just occ_env -> lookupOccEnv occ_env occ
  169. extendOrigNameCache :: OrigNameCache -> Name -> OrigNameCache
  170. extendOrigNameCache nc name
  171. = ASSERT2( isExternalName name, ppr name )
  172. extendNameCache nc (nameModule name) (nameOccName name) name
  173. extendNameCache :: OrigNameCache -> Module -> OccName -> Name -> OrigNameCache
  174. extendNameCache nc mod occ name
  175. = extendModuleEnvWith combine nc mod (unitOccEnv occ name)
  176. where
  177. combine _ occ_env = extendOccEnv occ_env occ name
  178. getNameCache :: TcRnIf a b NameCache
  179. getNameCache = do { HscEnv { hsc_NC = nc_var } <- getTopEnv;
  180. readMutVar nc_var }
  181. updNameCache :: (NameCache -> (NameCache, c)) -> TcRnIf a b c
  182. updNameCache upd_fn = do
  183. HscEnv { hsc_NC = nc_var } <- getTopEnv
  184. atomicUpdMutVar' nc_var upd_fn
  185. -- | A function that atomically updates the name cache given a modifier
  186. -- function. The second result of the modifier function will be the result
  187. -- of the IO action.
  188. data NameCacheUpdater = NCU { updateNameCache :: forall c. (NameCache -> (NameCache, c)) -> IO c }
  189. -- | Return a function to atomically update the name cache.
  190. mkNameCacheUpdater :: TcRnIf a b NameCacheUpdater
  191. mkNameCacheUpdater = do
  192. nc_var <- hsc_NC `fmap` getTopEnv
  193. let update_nc f = do r <- atomicModifyIORef nc_var f
  194. _ <- evaluate =<< readIORef nc_var
  195. return r
  196. return (NCU update_nc)
  197. \end{code}
  198. \begin{code}
  199. initNameCache :: UniqSupply -> [Name] -> NameCache
  200. initNameCache us names
  201. = NameCache { nsUniqs = us,
  202. nsNames = initOrigNames names }
  203. initOrigNames :: [Name] -> OrigNameCache
  204. initOrigNames names = foldl extendOrigNameCache emptyModuleEnv names
  205. \end{code}
  206. %************************************************************************
  207. %* *
  208. Type variables and local Ids
  209. %* *
  210. %************************************************************************
  211. \begin{code}
  212. tcIfaceLclId :: FastString -> IfL Id
  213. tcIfaceLclId occ
  214. = do { lcl <- getLclEnv
  215. ; case (lookupUFM (if_id_env lcl) occ) of
  216. Just ty_var -> return ty_var
  217. Nothing -> failIfM (text "Iface id out of scope: " <+> ppr occ)
  218. }
  219. extendIfaceIdEnv :: [Id] -> IfL a -> IfL a
  220. extendIfaceIdEnv ids thing_inside
  221. = do { env <- getLclEnv
  222. ; let { id_env' = addListToUFM (if_id_env env) pairs
  223. ; pairs = [(occNameFS (getOccName id), id) | id <- ids] }
  224. ; setLclEnv (env { if_id_env = id_env' }) thing_inside }
  225. tcIfaceTyVar :: FastString -> IfL TyVar
  226. tcIfaceTyVar occ
  227. = do { lcl <- getLclEnv
  228. ; case (lookupUFM (if_tv_env lcl) occ) of
  229. Just ty_var -> return ty_var
  230. Nothing -> failIfM (text "Iface type variable out of scope: " <+> ppr occ)
  231. }
  232. lookupIfaceTyVar :: FastString -> IfL (Maybe TyVar)
  233. lookupIfaceTyVar occ
  234. = do { lcl <- getLclEnv
  235. ; return (lookupUFM (if_tv_env lcl) occ) }
  236. extendIfaceTyVarEnv :: [TyVar] -> IfL a -> IfL a
  237. extendIfaceTyVarEnv tyvars thing_inside
  238. = do { env <- getLclEnv
  239. ; let { tv_env' = addListToUFM (if_tv_env env) pairs
  240. ; pairs = [(occNameFS (getOccName tv), tv) | tv <- tyvars] }
  241. ; setLclEnv (env { if_tv_env = tv_env' }) thing_inside }
  242. \end{code}
  243. %************************************************************************
  244. %* *
  245. Getting from RdrNames to Names
  246. %* *
  247. %************************************************************************
  248. \begin{code}
  249. lookupIfaceTop :: OccName -> IfL Name
  250. -- Look up a top-level name from the current Iface module
  251. lookupIfaceTop occ
  252. = do { env <- getLclEnv; lookupOrig (if_mod env) occ }
  253. newIfaceName :: OccName -> IfL Name
  254. newIfaceName occ
  255. = do { uniq <- newUnique
  256. ; return $! mkInternalName uniq occ noSrcSpan }
  257. newIfaceNames :: [OccName] -> IfL [Name]
  258. newIfaceNames occs
  259. = do { uniqs <- newUniqueSupply
  260. ; return [ mkInternalName uniq occ noSrcSpan
  261. | (occ,uniq) <- occs `zip` uniqsFromSupply uniqs] }
  262. \end{code}