2 #include "HsVersions.h"
7 initEnv, getEnv_LocalIds, getEnv_TyCons, getEnv_Classes,
9 tcTyVarScope, tcTyVarScopeGivenKinds, tcLookupTyVar,
11 tcExtendTyConEnv, tcLookupTyCon, tcLookupTyConByKey,
12 tcExtendClassEnv, tcLookupClass, tcLookupClassByKey,
13 tcGetTyConsAndClasses,
15 tcExtendGlobalValEnv, tcExtendLocalValEnv,
16 tcLookupLocalValue, tcLookupLocalValueOK, tcLookupLocalValueByKey,
17 tcLookupGlobalValue, tcLookupGlobalValueByKey,
19 newMonoIds, newLocalIds, newLocalId,
25 import TcMLoop -- for paranoia checking
27 import Id ( Id(..), GenId, idType, mkUserLocal )
28 import TcHsSyn ( TcIdBndr(..), TcIdOcc(..) )
29 import TcKind ( TcKind, newKindVars, tcDefaultKind, kindToTcKind )
30 import TcType ( TcType(..), TcMaybe, TcTyVar(..), TcTyVarSet(..),
31 newTyVarTys, tcInstTyVars, tcInstType, zonkTcTyVars
33 import TyVar ( mkTyVar, tyVarKind, unionTyVarSets, emptyTyVarSet )
34 import Type ( tyVarsOfTypes )
35 import TyCon ( TyCon, Arity(..), tyConKind, synTyConArity )
36 import Class ( Class(..), GenClass, classSig )
38 import TcMonad hiding ( rnMtoTcM )
40 import Name ( getOccName, getSrcLoc, Name{-instance NamedThing-} )
43 import RnHsSyn ( RnName(..) )
44 import Type ( splitForAllTy )
45 import Unique ( pprUnique10, pprUnique{-ToDo:rm-} )
47 import Util ( zipEqual, zipWithEqual, zipWith3Equal, zipLazy,
48 panic, pprPanic, pprTrace{-ToDo:rm-}
52 Data type declarations
60 (ValueEnv Id) -- Globals
61 (ValueEnv (TcIdBndr s)) -- Locals
62 (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
63 -- ...why mutable? see notes with tcGetGlobalTyVars
65 type TyVarEnv s = UniqFM (TcKind s, TyVar)
66 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
67 type ClassEnv s = UniqFM (TcKind s, Class)
68 type ValueEnv id = UniqFM id
70 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
71 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
73 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
74 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
75 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
78 Making new TcTyVars, with knot tying!
79 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
81 tcTyVarScopeGivenKinds
82 :: [Name] -- Names of some type variables
84 -> ([TyVar] -> TcM s a) -- Thing to type check in their scope
87 tcTyVarScopeGivenKinds names kinds thing_inside
88 = fixTc (\ ~(rec_tyvars, _) ->
89 -- Ok to look at names, kinds, but not tyvars!
91 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
93 tve' = addListToUFM tve (zipEqual "tcTyVarScopeGivenKinds" names (kinds `zipLazy` rec_tyvars))
95 tcSetEnv (TcEnv tve' tce ce gve lve gtvs)
96 (thing_inside rec_tyvars) `thenTc` \ result ->
98 -- Get the tyvar's Kinds from their TcKinds
99 mapNF_Tc tcDefaultKind kinds `thenNF_Tc` \ kinds' ->
101 -- Construct the real TyVars
103 tyvars = zipWithEqual "tcTyVarScopeGivenKinds" mk_tyvar names kinds'
104 mk_tyvar name kind = mkTyVar name (uniqueOf name) kind
106 returnTc (tyvars, result)
107 ) `thenTc` \ (_,result) ->
110 tcTyVarScope names thing_inside
111 = newKindVars (length names) `thenNF_Tc` \ kinds ->
112 tcTyVarScopeGivenKinds names kinds thing_inside
116 The Kind, TyVar, Class and TyCon envs
117 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119 Extending the environments. Notice the uses of @zipLazy@, which makes sure
120 that the knot-tied TyVars, TyCons and Classes aren't looked at too early.
123 tcExtendTyConEnv :: [(RnName,Maybe Arity)] -> [TyCon] -> TcM s r -> TcM s r
125 tcExtendTyConEnv names_w_arities tycons scope
126 = newKindVars (length names_w_arities) `thenNF_Tc` \ kinds ->
127 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
129 tce' = addListToUFM tce [ (name, (kind, arity, tycon))
130 | ((name,arity), (kind,tycon))
131 <- zipEqual "tcExtendTyConEnv" names_w_arities (kinds `zipLazy` tycons)
134 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope `thenTc` \ result ->
135 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
139 tcExtendClassEnv :: [RnName] -> [Class] -> TcM s r -> TcM s r
140 tcExtendClassEnv names classes scope
141 = newKindVars (length names) `thenNF_Tc` \ kinds ->
142 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
144 ce' = addListToUFM ce (zipEqual "tcExtendClassEnv" names (kinds `zipLazy` classes))
146 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope `thenTc` \ result ->
147 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
152 Looking up in the environments.
156 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
157 returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr PprShowAll name)) name)
160 tcLookupTyCon (WiredInTyCon tc) -- wired in tycons
161 = returnNF_Tc (kindToTcKind (tyConKind tc), synTyConArity tc, tc)
164 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
165 returnNF_Tc (lookupWithDefaultUFM tce (pprPanic "tcLookupTyCon:" (ppr PprShowAll name)) name)
167 tcLookupTyConByKey uniq
168 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
170 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
171 (pprPanic "tcLookupTyCon:" (pprUnique10 uniq))
177 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
178 -- pprTrace "tcLookupClass:" (ppCat [ppStr "Uniq:", pprUnique10 (uniqueOf name), ppStr "; avail:", ppCat (map (pprUnique10 . fst) (ufmToList ce))]) $
179 -- pprTrace "tcLookupClass:" (ppCat [ppStr "Uniq:", pprUnique (uniqueOf name), ppStr "; avail:", ppCat (map (pprUnique . fst) (ufmToList ce))]) $
180 returnNF_Tc (lookupWithDefaultUFM ce (pprPanic "tcLookupClass:" (ppr PprShowAll name)) name)
182 tcLookupClassByKey uniq
183 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
185 (kind, clas) = lookupWithDefaultUFM_Directly ce
186 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
191 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
192 tcGetTyConsAndClasses
193 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
194 returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
195 [c | (_, c) <- eltsUFM ce])
200 Extending and consulting the value environment
201 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203 tcExtendGlobalValEnv ids scope
204 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
206 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
208 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
210 tcExtendLocalValEnv names ids scope
211 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
212 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
214 lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
215 extra_global_tyvars = tyVarsOfTypes (map idType ids)
216 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
218 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
220 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
223 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
224 To improve subsequent calls to the same function it writes the zonked set back into
228 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
230 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
231 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
232 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
233 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
234 returnNF_Tc global_tvs'
238 tcLookupLocalValue :: RnName -> NF_TcM s (Maybe (TcIdBndr s))
239 tcLookupLocalValue name
240 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
241 returnNF_Tc (lookupUFM lve name)
243 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
244 tcLookupLocalValueByKey uniq
245 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
246 returnNF_Tc (lookupUFM_Directly lve uniq)
248 tcLookupLocalValueOK :: String -> RnName -> NF_TcM s (TcIdBndr s)
249 tcLookupLocalValueOK err name
250 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
251 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
254 tcLookupGlobalValue :: RnName -> NF_TcM s Id
256 tcLookupGlobalValue (WiredInId id) -- wired in ids
259 tcLookupGlobalValue name
260 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
261 returnNF_Tc (lookupWithDefaultUFM gve def name)
264 def = pprPanic "tcLookupGlobalValue:" (ppr PprDebug name)
266 def = panic "tcLookupGlobalValue"
269 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
270 tcLookupGlobalValueByKey uniq
271 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
272 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
275 def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
277 def = panic "tcLookupGlobalValueByKey"
287 newMonoIds :: [RnName] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
289 newMonoIds names kind m
290 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
291 tcGetUniques no_of_names `thenNF_Tc` \ uniqs ->
293 new_ids = zipWith3Equal "newMonoIds" mk_id names uniqs tys
297 name_str = case (getOccName name) of { Unqual n -> n }
299 mkUserLocal name_str uniq ty (getSrcLoc name)
301 tcExtendLocalValEnv names new_ids (m new_ids)
303 no_of_names = length names
305 newLocalId :: FAST_STRING -> TcType s -> NF_TcM s (TcIdOcc s)
307 = tcGetSrcLoc `thenNF_Tc` \ loc ->
308 tcGetUnique `thenNF_Tc` \ uniq ->
309 returnNF_Tc (TcId (mkUserLocal name uniq ty loc))
311 newLocalIds :: [FAST_STRING] -> [TcType s] -> NF_TcM s [TcIdOcc s]
312 newLocalIds names tys
313 = tcGetSrcLoc `thenNF_Tc` \ loc ->
314 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
316 new_ids = zipWith3Equal "newLocalIds" mk_id names uniqs tys
317 mk_id name uniq ty = TcId (mkUserLocal name uniq ty loc)