2 #include "HsVersions.h"
7 initEnv, getEnv_LocalIds, getEnv_TyCons, getEnv_Classes,
9 tcTyVarScope, tcTyVarScopeGivenKinds, tcLookupTyVar,
11 tcExtendTyConEnv, tcLookupTyCon, tcLookupTyConByKey,
12 tcExtendClassEnv, tcLookupClass, tcLookupClassByKey,
14 tcExtendGlobalValEnv, tcExtendLocalValEnv,
15 tcLookupLocalValue, tcLookupLocalValueOK, tcLookupLocalValueByKey,
16 tcLookupGlobalValue, tcLookupGlobalValueByKey,
18 newMonoIds, newLocalIds, newLocalId,
24 import TcMLoop -- for paranoia checking
26 import Id ( Id(..), GenId, idType, mkUserLocal )
27 import TcHsSyn ( TcIdBndr(..), TcIdOcc(..) )
28 import TcKind ( TcKind, newKindVars, tcDefaultKind, kindToTcKind )
29 import TcType ( TcType(..), TcMaybe, TcTyVar(..), TcTyVarSet(..),
30 newTyVarTys, tcInstTyVars, tcInstType, zonkTcTyVars
32 import TyVar ( mkTyVar, getTyVarKind, unionTyVarSets, emptyTyVarSet )
33 import Type ( tyVarsOfTypes )
34 import TyCon ( TyCon, Arity(..), tyConKind, synTyConArity )
35 import Class ( Class(..), GenClass, getClassSig )
39 import Name ( getOccName, getSrcLoc, Name{-instance NamedThing-} )
42 import RnHsSyn ( RnName(..) )
43 import Type ( splitForAllTy )
44 import Unique ( Unique )
46 import Util ( zipWithEqual, zipWith3Equal, zipLazy, panic, pprPanic )
49 Data type declarations
57 (ValueEnv Id) -- Globals
58 (ValueEnv (TcIdBndr s)) -- Locals
59 (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
60 -- ...why mutable? see notes with tcGetGlobalTyVars
62 type TyVarEnv s = UniqFM (TcKind s, TyVar)
63 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
64 type ClassEnv s = UniqFM (TcKind s, Class)
65 type ValueEnv id = UniqFM id
67 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
68 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
70 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
71 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
72 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
75 Making new TcTyVars, with knot tying!
76 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78 tcTyVarScopeGivenKinds
79 :: [Name] -- Names of some type variables
81 -> ([TyVar] -> TcM s a) -- Thing to type check in their scope
84 tcTyVarScopeGivenKinds names kinds thing_inside
85 = fixTc (\ ~(rec_tyvars, _) ->
86 -- Ok to look at names, kinds, but not tyvars!
88 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
90 tve' = addListToUFM tve (names `zip` (kinds `zipLazy` rec_tyvars))
92 tcSetEnv (TcEnv tve' tce ce gve lve gtvs)
93 (thing_inside rec_tyvars) `thenTc` \ result ->
95 -- Get the tyvar's Kinds from their TcKinds
96 mapNF_Tc tcDefaultKind kinds `thenNF_Tc` \ kinds' ->
98 -- Construct the real TyVars
100 tyvars = zipWithEqual mk_tyvar names kinds'
101 mk_tyvar name kind = mkTyVar name (uniqueOf name) kind
103 returnTc (tyvars, result)
104 ) `thenTc` \ (_,result) ->
107 tcTyVarScope names thing_inside
108 = newKindVars (length names) `thenNF_Tc` \ kinds ->
109 tcTyVarScopeGivenKinds names kinds thing_inside
113 The Kind, TyVar, Class and TyCon envs
114 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
116 Extending the environments. Notice the uses of @zipLazy@, which makes sure
117 that the knot-tied TyVars, TyCons and Classes aren't looked at too early.
120 tcExtendTyConEnv :: [(RnName,Maybe Arity)] -> [TyCon] -> TcM s r -> TcM s r
122 tcExtendTyConEnv names_w_arities tycons scope
123 = newKindVars (length names_w_arities) `thenNF_Tc` \ kinds ->
124 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
126 tce' = addListToUFM tce [ (name, (kind, arity, tycon))
127 | ((name,arity), (kind,tycon)) <- names_w_arities `zip`
128 (kinds `zipLazy` tycons)
131 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope `thenTc` \ result ->
132 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
136 tcExtendClassEnv :: [RnName] -> [Class] -> TcM s r -> TcM s r
137 tcExtendClassEnv names classes scope
138 = newKindVars (length names) `thenNF_Tc` \ kinds ->
139 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
141 ce' = addListToUFM ce (names `zip` (kinds `zipLazy` classes))
143 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope `thenTc` \ result ->
144 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
149 Looking up in the environments.
153 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
154 returnNF_Tc (lookupWithDefaultUFM tve (panic "tcLookupTyVar") name)
157 tcLookupTyCon (WiredInTyCon tc) -- wired in tycons
158 = returnNF_Tc (kindToTcKind (tyConKind tc), synTyConArity tc, tc)
161 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
162 returnNF_Tc (lookupWithDefaultUFM tce (panic "tcLookupTyCon") name)
164 tcLookupTyConByKey uniq
165 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
167 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
168 (pprPanic "tcLookupTyCon:" (ppr PprDebug uniq))
174 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
175 returnNF_Tc (lookupWithDefaultUFM ce (panic "tcLookupClass") name)
177 tcLookupClassByKey uniq
178 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
180 (kind, clas) = lookupWithDefaultUFM_Directly ce
181 (pprPanic "tcLookupClas:" (ppr PprDebug uniq))
189 Extending and consulting the value environment
190 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
192 tcExtendGlobalValEnv ids scope
193 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
195 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
197 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
199 tcExtendLocalValEnv names ids scope
200 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
201 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
203 lve' = addListToUFM lve (names `zip` ids)
204 extra_global_tyvars = tyVarsOfTypes (map idType ids)
205 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
207 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
209 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
212 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
213 To improve subsequent calls to the same function it writes the zonked set back into
217 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
219 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
220 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
221 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
222 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
223 returnNF_Tc global_tvs'
227 tcLookupLocalValue :: RnName -> NF_TcM s (Maybe (TcIdBndr s))
228 tcLookupLocalValue name
229 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
230 returnNF_Tc (lookupUFM lve name)
232 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
233 tcLookupLocalValueByKey uniq
234 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
235 returnNF_Tc (lookupUFM_Directly lve uniq)
237 tcLookupLocalValueOK :: String -> RnName -> NF_TcM s (TcIdBndr s)
238 tcLookupLocalValueOK err name
239 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
240 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
243 tcLookupGlobalValue :: RnName -> NF_TcM s Id
245 tcLookupGlobalValue (WiredInId id) -- wired in ids
248 tcLookupGlobalValue name
249 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
250 returnNF_Tc (lookupWithDefaultUFM gve def name)
253 def = pprPanic "tcLookupGlobalValue:" (ppr PprDebug name)
255 def = panic "tcLookupGlobalValue"
258 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
259 tcLookupGlobalValueByKey uniq
260 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
261 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
264 def = pprPanic "tcLookupGlobalValueByKey:" (ppr PprDebug uniq)
266 def = panic "tcLookupGlobalValueByKey"
276 newMonoIds :: [RnName] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
278 newMonoIds names kind m
279 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
280 tcGetUniques no_of_names `thenNF_Tc` \ uniqs ->
282 new_ids = zipWith3Equal mk_id names uniqs tys
286 name_str = case (getOccName name) of { Unqual n -> n }
288 mkUserLocal name_str uniq ty (getSrcLoc name)
290 tcExtendLocalValEnv names new_ids (m new_ids)
292 no_of_names = length names
294 newLocalId :: FAST_STRING -> TcType s -> NF_TcM s (TcIdOcc s)
296 = tcGetSrcLoc `thenNF_Tc` \ loc ->
297 tcGetUnique `thenNF_Tc` \ uniq ->
298 returnNF_Tc (TcId (mkUserLocal name uniq ty loc))
300 newLocalIds :: [FAST_STRING] -> [TcType s] -> NF_TcM s [TcIdOcc s]
301 newLocalIds names tys
302 = tcGetSrcLoc `thenNF_Tc` \ loc ->
303 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
305 new_ids = zipWith3Equal mk_id names uniqs tys
306 mk_id name uniq ty = TcId (mkUserLocal name uniq ty loc)