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,
19 tcLookupGlobalValueByKeyMaybe,
21 newMonoIds, newLocalIds, newLocalId,
22 tcGetGlobalTyVars, tcExtendGlobalTyVars
27 IMPORT_DELOOPER(TcMLoop) -- for paranoia checking
29 import HsTypes ( HsTyVar(..) )
30 import Id ( SYN_IE(Id), GenId, idType, mkUserLocal, mkUserId, replaceIdInfo, getIdInfo )
31 import PragmaInfo ( PragmaInfo(..) )
32 import TcHsSyn ( SYN_IE(TcIdBndr), TcIdOcc(..) )
33 import TcKind ( TcKind, newKindVars, newKindVar, tcDefaultKind, kindToTcKind )
34 import TcType ( SYN_IE(TcType), TcMaybe, SYN_IE(TcTyVar), SYN_IE(TcTyVarSet),
35 newTyVarTys, tcInstTyVars, zonkTcTyVars
37 import TyVar ( unionTyVarSets, emptyTyVarSet )
38 import Type ( tyVarsOfTypes, splitForAllTy )
39 import TyCon ( TyCon, tyConKind, synTyConArity )
40 import Class ( SYN_IE(Class), GenClass, classSig )
44 import IdInfo ( noIdInfo )
45 import Name ( Name, OccName(..), getSrcLoc, occNameString,
46 maybeWiredInTyConName, maybeWiredInIdName, pprSym
50 import Unique ( pprUnique10{-, pprUnique ToDo:rm-} )
52 import Util ( zipEqual, zipWithEqual, zipWith3Equal, zipLazy,
53 panic, pprPanic{-, pprTrace ToDo:rm-}
57 Data type declarations
65 (ValueEnv Id) -- Globals
66 (ValueEnv (TcIdBndr s)) -- Locals
67 (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
68 -- ...why mutable? see notes with tcGetGlobalTyVars
70 type TyVarEnv s = UniqFM (TcKind s, TyVar)
71 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
72 type ClassEnv s = UniqFM (TcKind s, Class)
73 type ValueEnv id = UniqFM id
75 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
76 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
78 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
79 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
80 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
86 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
87 tcExtendTyVarEnv names kinds_w_types scope
88 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
90 tve' = addListToUFM tve (zipEqual "tcTyVarScope" names kinds_w_types)
92 tcSetEnv (TcEnv tve' tce ce gve lve gtvs) scope
95 The Kind, TyVar, Class and TyCon envs
96 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98 Extending the environments. Notice the uses of @zipLazy@, which makes sure
99 that the knot-tied TyVars, TyCons and Classes aren't looked at too early.
102 tcExtendTyConEnv :: [(Name,Maybe Arity)] -> [TyCon] -> TcM s r -> TcM s r
104 tcExtendTyConEnv names_w_arities tycons scope
105 = newKindVars (length names_w_arities) `thenNF_Tc` \ kinds ->
106 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
108 tce' = addListToUFM tce [ (name, (kind, arity, tycon))
109 | ((name,arity), (kind,tycon))
110 <- zipEqual "tcExtendTyConEnv" names_w_arities (kinds `zipLazy` tycons)
113 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope `thenTc` \ result ->
114 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
118 tcExtendClassEnv :: [Name] -> [Class] -> TcM s r -> TcM s r
119 tcExtendClassEnv names classes scope
120 = newKindVars (length names) `thenNF_Tc` \ kinds ->
121 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
123 ce' = addListToUFM ce (zipEqual "tcExtendClassEnv" names (kinds `zipLazy` classes))
125 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope `thenTc` \ result ->
126 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
131 Looking up in the environments.
135 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
136 returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr PprShowAll name)) name)
140 = case maybeWiredInTyConName name of
141 Just tc -> returnTc (kindToTcKind (tyConKind tc), synTyConArity tc, tc)
142 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
143 case lookupUFM tce name of
144 Just stuff -> returnTc stuff
145 Nothing -> -- Could be that he's using a class name as a type constructor
146 case lookupUFM ce name of
147 Just _ -> failTc (classAsTyConErr name)
148 Nothing -> pprPanic "tcLookupTyCon:" (ppr PprDebug name)
150 tcLookupTyConByKey uniq
151 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
153 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
154 (pprPanic "tcLookupTyCon:" (pprUnique10 uniq))
160 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
161 -- pprTrace "tcLookupClass:" (ppCat [ppStr "Uniq:", pprUnique10 (uniqueOf name), ppStr "; avail:", ppCat (map (pprUnique10 . fst) (ufmToList ce))]) $
162 -- pprTrace "tcLookupClass:" (ppCat [ppStr "Uniq:", pprUnique (uniqueOf name), ppStr "; avail:", ppCat (map (pprUnique . fst) (ufmToList ce))]) $
163 case lookupUFM ce name of
164 Just stuff -> returnTc stuff
165 Nothing -> -- Could be that he's using a type constructor as a class
166 case lookupUFM tce name of
167 Just _ -> failTc (tyConAsClassErr name)
168 Nothing -> pprPanic "tcLookupClass:" (ppr PprShowAll name)
170 tcLookupClassByKey uniq
171 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
173 (kind, clas) = lookupWithDefaultUFM_Directly ce
174 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
179 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
180 tcGetTyConsAndClasses
181 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
182 returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
183 [c | (_, c) <- eltsUFM ce])
188 Extending and consulting the value environment
189 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
191 tcExtendGlobalValEnv ids scope
192 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
194 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
196 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
198 tcExtendLocalValEnv names ids scope
199 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
200 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
202 lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
203 extra_global_tyvars = tyVarsOfTypes (map idType ids)
204 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
206 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
208 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
211 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
212 To improve subsequent calls to the same function it writes the zonked set back into
216 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
218 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
219 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
220 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
221 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
222 returnNF_Tc global_tvs'
224 tcExtendGlobalTyVars extra_global_tvs scope
225 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
226 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
228 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tvs
230 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
231 tcSetEnv (TcEnv tve tce ce gve lve gtvs') scope
235 tcLookupLocalValue :: Name -> NF_TcM s (Maybe (TcIdBndr s))
236 tcLookupLocalValue name
237 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
238 returnNF_Tc (lookupUFM lve name)
240 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
241 tcLookupLocalValueByKey uniq
242 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
243 returnNF_Tc (lookupUFM_Directly lve uniq)
245 tcLookupLocalValueOK :: String -> Name -> NF_TcM s (TcIdBndr s)
246 tcLookupLocalValueOK err name
247 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
248 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
251 tcLookupGlobalValue :: Name -> NF_TcM s Id
253 tcLookupGlobalValue name
254 = case maybeWiredInIdName name of
255 Just id -> returnNF_Tc id
256 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
257 returnNF_Tc (lookupWithDefaultUFM gve def name)
259 def = pprPanic "tcLookupGlobalValue:" (ppr PprDebug name)
261 tcLookupGlobalValueMaybe :: Name -> NF_TcM s (Maybe Id)
263 tcLookupGlobalValueMaybe name
264 = case maybeWiredInIdName name of
265 Just id -> returnNF_Tc (Just id)
266 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
267 returnNF_Tc (lookupUFM gve name)
270 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
271 tcLookupGlobalValueByKey uniq
272 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
273 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
276 def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
278 def = panic "tcLookupGlobalValueByKey"
281 tcLookupGlobalValueByKeyMaybe :: Unique -> NF_TcM s (Maybe Id)
282 tcLookupGlobalValueByKeyMaybe uniq
283 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
284 returnNF_Tc (lookupUFM_Directly gve uniq)
286 -- Extract the IdInfo from an IfaceSig imported from an interface file
287 tcAddImportedIdInfo :: Id -> NF_TcM s Id
288 tcAddImportedIdInfo id
289 = tcLookupGlobalValueMaybe (getName id) `thenNF_Tc` \ maybe_id ->
291 new_info = case maybe_id of
293 Just imported_id -> getIdInfo imported_id
294 -- ToDo: could check that types are the same
296 returnNF_Tc (id `replaceIdInfo` new_info)
297 -- The Id must be returned without a data dependency on maybe_id
305 -- Uses the Name as the Name of the Id
306 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
308 newMonoIds names kind m
309 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
311 new_ids = zipWithEqual "newMonoIds" mk_id names tys
312 mk_id name ty = mkUserId name ty NoPragmaInfo
314 tcExtendLocalValEnv names new_ids (m new_ids)
316 no_of_names = length names
318 newLocalId :: OccName -> TcType s -> NF_TcM s (TcIdBndr s)
320 = tcGetSrcLoc `thenNF_Tc` \ loc ->
321 tcGetUnique `thenNF_Tc` \ uniq ->
322 returnNF_Tc (mkUserLocal name uniq ty loc)
324 newLocalIds :: [OccName] -> [TcType s] -> NF_TcM s [TcIdBndr s]
325 newLocalIds names tys
326 = tcGetSrcLoc `thenNF_Tc` \ loc ->
327 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
329 new_ids = zipWith3Equal "newLocalIds" mk_id names uniqs tys
330 mk_id name uniq ty = mkUserLocal name uniq ty loc
336 classAsTyConErr name sty
337 = ppBesides [ppPStr SLIT("Class used as a type constructor: "), pprSym sty name]
339 tyConAsClassErr name sty
340 = ppBesides [ppPStr SLIT("Type constructor used as a class: "), pprSym sty name]