2 #include "HsVersions.h"
7 initEnv, getEnv_LocalIds, getEnv_TyCons, getEnv_Classes,
9 tcExtendKindEnv, tcExtendTyVarEnv, tcExtendTyConEnv, tcExtendClassEnv,
10 tcLookupTyVar, tcLookupTyCon, tcLookupClass, tcLookupClassByKey,
12 tcExtendGlobalValEnv, tcExtendLocalValEnv,
13 tcLookupLocalValue, tcLookupLocalValueOK,
14 tcLookupGlobalValue, tcLookupGlobalValueByKey,
16 tcTyVarScope, newMonoIds, newLocalIds,
22 import TcMLoop -- for paranoia checking
24 import Id ( Id(..), GenId, idType, mkUserLocal )
25 import TcHsSyn ( TcIdBndr(..) )
26 import TcKind ( TcKind, newKindVars, tcKindToKind, kindToTcKind )
27 import TcType ( TcType(..), TcMaybe, TcTyVar(..), TcTyVarSet(..), newTyVarTys, zonkTcTyVars )
28 import TyVar ( mkTyVar, getTyVarKind, unionTyVarSets, emptyTyVarSet )
29 import Type ( tyVarsOfTypes )
30 import TyCon ( TyCon, getTyConKind )
31 import Class ( Class(..), GenClass, getClassSig )
35 import Name ( Name(..), getNameShortName )
38 import Unique ( Unique )
40 import Util ( zipWithEqual, zipWith3Equal, zipLazy, panic )
43 Data type declarations
49 (ValueEnv Id) -- Globals
50 (ValueEnv (TcIdBndr s)) -- Locals
51 (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
52 -- ...why mutable? see notes with tcGetGlobalTyVars
53 (KindEnv s) -- Gives TcKinds of TyCons and Classes
57 type TyVarEnv s = UniqFM (TcKind s, TyVar)
58 type TyConEnv = UniqFM TyCon
59 type KindEnv s = UniqFM (TcKind s)
60 type ClassEnv = UniqFM Class
61 type ValueEnv id = UniqFM id
63 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
64 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM mut emptyUFM emptyUFM emptyUFM
66 getEnv_LocalIds (TcEnv _ _ ls _ _ _ _) = ls
67 getEnv_TyCons (TcEnv _ _ _ _ _ ts _) = ts
68 getEnv_Classes (TcEnv _ _ _ _ _ _ cs) = cs
71 Making new TcTyVars, with knot tying!
72 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74 tcTyVarScope :: [Name] -- Names of some type variables
75 -> ([TyVar] -> TcM s a) -- Thing to type check in their scope
78 tcTyVarScope tyvar_names thing_inside
79 = newKindVars (length tyvar_names) `thenNF_Tc` \ tyvar_kinds ->
81 fixTc (\ ~(tyvars, _) ->
82 -- Ok to look at kinds, but not tyvars!
83 tcExtendTyVarEnv tyvar_names (tyvar_kinds `zipLazy` tyvars) (
85 -- Do the thing inside
86 thing_inside tyvars `thenTc` \ result ->
88 -- Get the tyvar's Kinds from their TcKinds
89 mapNF_Tc tcKindToKind tyvar_kinds `thenNF_Tc` \ tyvar_kinds' ->
91 -- Construct the real TyVars
93 tyvars = zipWithEqual mk_tyvar tyvar_names tyvar_kinds'
94 mk_tyvar name kind = mkTyVar (getNameShortName name) (getItsUnique name) kind
96 returnTc (tyvars, result)
97 )) `thenTc` \ (_,result) ->
102 The Kind, TyVar, Class and TyCon envs
103 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
105 Extending the environments
108 tcExtendKindEnv :: [Name] -> [TcKind s] -> TcM s r -> TcM s r
109 tcExtendKindEnv names kinds scope
110 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
112 ke' = addListToUFM ke (names `zip` kinds)
114 tcSetEnv (TcEnv tve gve lve gtvs ke' tce ce) scope
116 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
117 tcExtendTyVarEnv tyvar_names kinds_w_tyvars scope
118 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
120 tve' = addListToUFM tve (tyvar_names `zip` kinds_w_tyvars)
122 tcSetEnv (TcEnv tve' gve lve gtvs ke tce ce) scope
124 tcExtendTyConEnv tycons scope
125 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
127 tce' = addListToUFM_Directly tce [(getItsUnique tycon, tycon) | tycon <- tycons]
129 tcSetEnv (TcEnv tve gve lve gtvs ke tce' ce) scope
131 tcExtendClassEnv classes scope
132 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
134 ce' = addListToUFM_Directly ce [(getItsUnique clas, clas) | clas <- classes]
136 tcSetEnv (TcEnv tve gve lve gtvs ke tce ce') scope
140 Looking up in the environments
144 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
145 returnNF_Tc (lookupWithDefaultUFM tve (panic "tcLookupTyVar") name)
148 tcLookupTyCon (WiredInTyCon tc) -- wired in tycons
149 = returnNF_Tc (kindToTcKind (getTyConKind tc), tc)
152 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
154 tycon = lookupWithDefaultUFM tce (panic "tcLookupTyCon") name
155 kind = lookupWithDefaultUFM ke (kindToTcKind (getTyConKind tycon)) name
156 -- The KE will bind tycon in the current mutually-recursive set.
157 -- If the KE doesn't, then the tycon is already defined, and we
158 -- can safely grab the kind from the TyCon itself
160 returnNF_Tc (kind,tycon)
164 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
166 clas = lookupWithDefaultUFM ce (panic "tcLookupClass") name
167 (tyvar, _, _) = getClassSig clas
168 kind = lookupWithDefaultUFM ke (kindToTcKind (getTyVarKind tyvar)) name
170 returnNF_Tc (kind,clas)
172 tcLookupClassByKey uniq
173 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
175 clas = lookupWithDefaultUFM_Directly ce (panic "tcLookupClas") uniq
182 Extending and consulting the value environment
183 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185 tcExtendGlobalValEnv ids scope
186 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
188 gve' = addListToUFM_Directly gve [(getItsUnique id, id) | id <- ids]
190 tcSetEnv (TcEnv tve gve' lve gtvs ke tce ce) scope
192 tcExtendLocalValEnv names ids scope
193 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
194 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
196 lve' = addListToUFM lve (names `zip` ids)
197 extra_global_tyvars = tyVarsOfTypes (map idType ids)
198 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
200 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
202 tcSetEnv (TcEnv tve gve lve' gtvs' ke tce ce) scope
205 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
206 To improve subsequent calls to the same function it writes the zonked set back into
210 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
212 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
213 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
214 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
215 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
216 returnNF_Tc global_tvs'
220 tcLookupLocalValue :: Name -> NF_TcM s (Maybe (TcIdBndr s))
221 tcLookupLocalValue name
222 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
223 returnNF_Tc (lookupUFM lve name)
225 tcLookupLocalValueOK :: String -> Name -> NF_TcM s (TcIdBndr s)
226 tcLookupLocalValueOK err name
227 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
228 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
231 tcLookupGlobalValue :: Name -> NF_TcM s Id
233 tcLookupGlobalValue (WiredInVal id) -- wired in ids
236 tcLookupGlobalValue name
237 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
238 returnNF_Tc (lookupWithDefaultUFM gve def name)
241 def = panic ("tcLookupGlobalValue:" ++ ppShow 1000 (ppr PprDebug name))
243 def = panic "tcLookupGlobalValue"
247 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
248 tcLookupGlobalValueByKey uniq
249 = tcGetEnv `thenNF_Tc` \ (TcEnv tve gve lve gtvs ke tce ce) ->
250 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
253 def = panic ("tcLookupGlobalValueByKey:" ++ ppShow 1000 (ppr PprDebug uniq))
255 def = panic "tcLookupGlobalValueByKey"
265 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
266 newMonoIds names kind m
267 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
268 tcGetUniques no_of_names `thenNF_Tc` \ uniqs ->
270 new_ids = zipWith3Equal mk_id names uniqs tys
271 mk_id name uniq ty = mkUserLocal (getOccurrenceName name) uniq ty
274 tcExtendLocalValEnv names new_ids (m new_ids)
276 no_of_names = length names
278 newLocalIds :: [FAST_STRING] -> [TcType s] -> NF_TcM s [TcIdBndr s]
279 newLocalIds names tys
280 = tcGetSrcLoc `thenNF_Tc` \ loc ->
281 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
283 new_ids = zipWith3Equal mk_id names uniqs tys
284 mk_id name uniq ty = mkUserLocal name uniq ty loc