2 #include "HsVersions.h"
7 initEnv, getEnv_LocalIds, getEnv_TyCons, getEnv_Classes,
9 tcExtendTyVarEnv, tcLookupTyVar,
11 tcExtendTyConEnv, tcLookupTyCon, tcLookupTyConByKey,
12 tcExtendClassEnv, tcLookupClass, tcLookupClassByKey,
13 tcGetTyConsAndClasses,
15 tcExtendGlobalValEnv, tcExtendLocalValEnv,
16 tcLookupLocalValue, tcLookupLocalValueOK, tcLookupLocalValueByKey,
17 tcLookupGlobalValue, tcLookupGlobalValueByKey, tcLookupGlobalValueMaybe,
18 tcLookupGlobalValueByKeyMaybe,
20 newMonoIds, newLocalIds, newLocalId,
21 tcGetGlobalTyVars, tcExtendGlobalTyVars
26 IMPORT_DELOOPER(TcMLoop) -- for paranoia checking
28 import HsTypes ( HsTyVar(..) )
29 import Id ( SYN_IE(Id), GenId, idType, mkUserLocal, mkUserId )
30 import PragmaInfo ( PragmaInfo(..) )
31 import TcHsSyn ( SYN_IE(TcIdBndr), TcIdOcc(..) )
32 import TcKind ( TcKind, newKindVars, newKindVar, tcDefaultKind, kindToTcKind )
33 import TcType ( SYN_IE(TcType), TcMaybe, SYN_IE(TcTyVar), SYN_IE(TcTyVarSet),
34 newTyVarTys, tcInstTyVars, zonkTcTyVars
36 import TyVar ( unionTyVarSets, emptyTyVarSet )
37 import Type ( tyVarsOfTypes, splitForAllTy )
38 import TyCon ( TyCon, tyConKind, synTyConArity )
39 import Class ( SYN_IE(Class), GenClass, classSig )
43 import Name ( Name, OccName(..), getSrcLoc, occNameString,
44 maybeWiredInTyConName, maybeWiredInIdName, pprSym
48 import Unique ( pprUnique10{-, pprUnique ToDo:rm-} )
50 import Util ( zipEqual, zipWithEqual, zipWith3Equal, zipLazy,
51 panic, pprPanic{-, pprTrace ToDo:rm-}
55 Data type declarations
63 (ValueEnv Id) -- Globals
64 (ValueEnv (TcIdBndr s)) -- Locals
65 (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
66 -- ...why mutable? see notes with tcGetGlobalTyVars
68 type TyVarEnv s = UniqFM (TcKind s, TyVar)
69 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
70 type ClassEnv s = UniqFM (TcKind s, Class)
71 type ValueEnv id = UniqFM id
73 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
74 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
76 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
77 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
78 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
84 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
85 tcExtendTyVarEnv names kinds_w_types scope
86 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
88 tve' = addListToUFM tve (zipEqual "tcTyVarScope" names kinds_w_types)
90 tcSetEnv (TcEnv tve' tce ce gve lve gtvs) scope
93 The Kind, TyVar, Class and TyCon envs
94 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96 Extending the environments. Notice the uses of @zipLazy@, which makes sure
97 that the knot-tied TyVars, TyCons and Classes aren't looked at too early.
100 tcExtendTyConEnv :: [(Name,Maybe Arity)] -> [TyCon] -> TcM s r -> TcM s r
102 tcExtendTyConEnv names_w_arities tycons scope
103 = newKindVars (length names_w_arities) `thenNF_Tc` \ kinds ->
104 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
106 tce' = addListToUFM tce [ (name, (kind, arity, tycon))
107 | ((name,arity), (kind,tycon))
108 <- zipEqual "tcExtendTyConEnv" names_w_arities (kinds `zipLazy` tycons)
111 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope `thenTc` \ result ->
112 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
116 tcExtendClassEnv :: [Name] -> [Class] -> TcM s r -> TcM s r
117 tcExtendClassEnv names classes scope
118 = newKindVars (length names) `thenNF_Tc` \ kinds ->
119 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
121 ce' = addListToUFM ce (zipEqual "tcExtendClassEnv" names (kinds `zipLazy` classes))
123 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope `thenTc` \ result ->
124 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
129 Looking up in the environments.
133 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
134 returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr PprShowAll name)) name)
138 = case maybeWiredInTyConName name of
139 Just tc -> returnTc (kindToTcKind (tyConKind tc), synTyConArity tc, tc)
140 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
141 case lookupUFM tce name of
142 Just stuff -> returnTc stuff
143 Nothing -> -- Could be that he's using a class name as a type constructor
144 case lookupUFM ce name of
145 Just _ -> failTc (classAsTyConErr name)
146 Nothing -> pprPanic "tcLookupTyCon:" (ppr PprDebug name)
148 tcLookupTyConByKey uniq
149 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
151 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
152 (pprPanic "tcLookupTyCon:" (pprUnique10 uniq))
158 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
159 -- pprTrace "tcLookupClass:" (ppCat [ppStr "Uniq:", pprUnique10 (uniqueOf name), ppStr "; avail:", ppCat (map (pprUnique10 . fst) (ufmToList ce))]) $
160 -- pprTrace "tcLookupClass:" (ppCat [ppStr "Uniq:", pprUnique (uniqueOf name), ppStr "; avail:", ppCat (map (pprUnique . fst) (ufmToList ce))]) $
161 case lookupUFM ce name of
162 Just stuff -> returnTc stuff
163 Nothing -> -- Could be that he's using a type constructor as a class
164 case lookupUFM tce name of
165 Just _ -> failTc (tyConAsClassErr name)
166 Nothing -> pprPanic "tcLookupClass:" (ppr PprShowAll name)
168 tcLookupClassByKey uniq
169 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
171 (kind, clas) = lookupWithDefaultUFM_Directly ce
172 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
177 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
178 tcGetTyConsAndClasses
179 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
180 returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
181 [c | (_, c) <- eltsUFM ce])
186 Extending and consulting the value environment
187 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
189 tcExtendGlobalValEnv ids scope
190 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
192 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
194 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
196 tcExtendLocalValEnv names ids scope
197 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
198 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
200 lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
201 extra_global_tyvars = tyVarsOfTypes (map idType ids)
202 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
204 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
206 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
209 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
210 To improve subsequent calls to the same function it writes the zonked set back into
214 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
216 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
217 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
218 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
219 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
220 returnNF_Tc global_tvs'
222 tcExtendGlobalTyVars extra_global_tvs scope
223 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
224 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
226 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tvs
228 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
229 tcSetEnv (TcEnv tve tce ce gve lve gtvs') scope
233 tcLookupLocalValue :: Name -> NF_TcM s (Maybe (TcIdBndr s))
234 tcLookupLocalValue name
235 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
236 returnNF_Tc (lookupUFM lve name)
238 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
239 tcLookupLocalValueByKey uniq
240 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
241 returnNF_Tc (lookupUFM_Directly lve uniq)
243 tcLookupLocalValueOK :: String -> Name -> NF_TcM s (TcIdBndr s)
244 tcLookupLocalValueOK err name
245 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
246 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
249 tcLookupGlobalValue :: Name -> NF_TcM s Id
251 tcLookupGlobalValue name
252 = case maybeWiredInIdName name of
253 Just id -> returnNF_Tc id
254 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
255 returnNF_Tc (lookupWithDefaultUFM gve def name)
257 def = pprPanic "tcLookupGlobalValue:" (ppr PprDebug name)
259 tcLookupGlobalValueMaybe :: Name -> NF_TcM s (Maybe Id)
261 tcLookupGlobalValueMaybe name
262 = case maybeWiredInIdName name of
263 Just id -> returnNF_Tc (Just id)
264 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
265 returnNF_Tc (lookupUFM gve name)
268 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
269 tcLookupGlobalValueByKey uniq
270 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
271 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
274 def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
276 def = panic "tcLookupGlobalValueByKey"
279 tcLookupGlobalValueByKeyMaybe :: Unique -> NF_TcM s (Maybe Id)
280 tcLookupGlobalValueByKeyMaybe uniq
281 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
282 returnNF_Tc (lookupUFM_Directly gve uniq)
290 -- Uses the Name as the Name of the Id
291 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
293 newMonoIds names kind m
294 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
296 new_ids = zipWithEqual "newMonoIds" mk_id names tys
297 mk_id name ty = mkUserId name ty NoPragmaInfo
299 tcExtendLocalValEnv names new_ids (m new_ids)
301 no_of_names = length names
303 newLocalId :: OccName -> TcType s -> NF_TcM s (TcIdBndr s)
305 = tcGetSrcLoc `thenNF_Tc` \ loc ->
306 tcGetUnique `thenNF_Tc` \ uniq ->
307 returnNF_Tc (mkUserLocal name uniq ty loc)
309 newLocalIds :: [OccName] -> [TcType s] -> NF_TcM s [TcIdBndr s]
310 newLocalIds names tys
311 = tcGetSrcLoc `thenNF_Tc` \ loc ->
312 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
314 new_ids = zipWith3Equal "newLocalIds" mk_id names uniqs tys
315 mk_id name uniq ty = mkUserLocal name uniq ty loc
321 classAsTyConErr name sty
322 = ppBesides [ppStr "Class used as a type constructor: ", pprSym sty name]
324 tyConAsClassErr name sty
325 = ppBesides [ppStr "Type constructor used as a class: ", pprSym sty name]