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, Kind )
34 import TcType ( SYN_IE(TcType), TcMaybe, SYN_IE(TcTyVar), SYN_IE(TcTyVarSet),
35 newTyVarTys, tcInstTyVars, zonkTcTyVars
37 import TyVar ( unionTyVarSets, emptyTyVarSet, tyVarSetToList, SYN_IE(TyVar) )
38 import PprType ( GenTyVar )
39 import Type ( tyVarsOfTypes, splitForAllTy )
40 import TyCon ( TyCon, tyConKind, synTyConArity, SYN_IE(Arity) )
41 import Class ( SYN_IE(Class), GenClass, classSig )
45 import IdInfo ( noIdInfo )
46 import Name ( Name, OccName(..), getSrcLoc, occNameString,
47 maybeWiredInTyConName, maybeWiredInIdName,
52 import Unique ( pprUnique10{-, pprUnique ToDo:rm-}, Unique )
54 import Util ( zipEqual, zipWithEqual, zipWith3Equal, zipLazy,
55 panic, pprPanic, pprTrace
60 Data type declarations
68 (ValueEnv Id) -- Globals
69 (ValueEnv (TcIdBndr s)) -- Locals
70 (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
71 -- ...why mutable? see notes with tcGetGlobalTyVars
73 type TyVarEnv s = UniqFM (TcKind s, TyVar)
74 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
75 type ClassEnv s = UniqFM (TcKind s, Class)
76 type ValueEnv id = UniqFM id
78 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
79 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
81 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
82 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
83 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
89 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
90 tcExtendTyVarEnv names kinds_w_types scope
91 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
93 tve' = addListToUFM tve (zipEqual "tcTyVarScope" names kinds_w_types)
95 tcSetEnv (TcEnv tve' tce ce gve lve gtvs) scope
98 The Kind, TyVar, Class and TyCon envs
99 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101 Extending the environments. Notice the uses of @zipLazy@, which makes sure
102 that the knot-tied TyVars, TyCons and Classes aren't looked at too early.
105 tcExtendTyConEnv :: [(Name,Maybe Arity)] -> [TyCon] -> TcM s r -> TcM s r
107 tcExtendTyConEnv names_w_arities tycons scope
108 = newKindVars (length names_w_arities) `thenNF_Tc` \ kinds ->
109 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
111 tce' = addListToUFM tce [ (name, (kind, arity, tycon))
112 | ((name,arity), (kind,tycon))
113 <- zipEqual "tcExtendTyConEnv" names_w_arities (kinds `zipLazy` tycons)
116 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope `thenTc` \ result ->
117 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
121 tcExtendClassEnv :: [Name] -> [Class] -> TcM s r -> TcM s r
122 tcExtendClassEnv names classes scope
123 = newKindVars (length names) `thenNF_Tc` \ kinds ->
124 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
126 ce' = addListToUFM ce (zipEqual "tcExtendClassEnv" names (kinds `zipLazy` classes))
128 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope `thenTc` \ result ->
129 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
134 Looking up in the environments.
138 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
139 returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr PprShowAll name)) name)
143 = case maybeWiredInTyConName name of
144 Just tc -> returnTc (kindToTcKind (tyConKind tc), synTyConArity tc, tc)
145 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
146 case lookupUFM tce name of
147 Just stuff -> returnTc stuff
148 Nothing -> -- Could be that he's using a class name as a type constructor
149 case lookupUFM ce name of
150 Just _ -> failTc (classAsTyConErr name)
151 Nothing -> pprPanic "tcLookupTyCon:" (ppr PprDebug name)
153 tcLookupTyConByKey uniq
154 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
156 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
157 (pprPanic "tcLookupTyCon:" (pprUnique10 uniq))
163 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
164 -- pprTrace "tcLookupClass:" (hsep [text "Uniq:", pprUnique10 (uniqueOf name), text "; avail:", hsep (map (pprUnique10 . fst) (ufmToList ce))]) $
165 -- pprTrace "tcLookupClass:" (hsep [text "Uniq:", pprUnique (uniqueOf name), text "; avail:", hsep (map (pprUnique . fst) (ufmToList ce))]) $
166 case lookupUFM ce name of
167 Just stuff -> returnTc stuff
168 Nothing -> -- Could be that he's using a type constructor as a class
169 case lookupUFM tce name of
170 Just _ -> failTc (tyConAsClassErr name)
171 Nothing -> pprPanic "tcLookupClass:" (ppr PprShowAll name)
173 tcLookupClassByKey uniq
174 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
176 (kind, clas) = lookupWithDefaultUFM_Directly ce
177 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
182 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
183 tcGetTyConsAndClasses
184 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
185 returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
186 [c | (_, c) <- eltsUFM ce])
191 Extending and consulting the value environment
192 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194 tcExtendGlobalValEnv ids scope
195 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
197 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
199 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
201 tcExtendLocalValEnv names ids scope
202 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
203 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
205 lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
206 extra_global_tyvars = tyVarsOfTypes (map idType ids)
207 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
209 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
211 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
214 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
215 To improve subsequent calls to the same function it writes the zonked set back into
219 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
221 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
222 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
223 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
224 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
225 returnNF_Tc global_tvs'
227 tcExtendGlobalTyVars extra_global_tvs scope
228 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
229 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
231 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tvs
233 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
234 tcSetEnv (TcEnv tve tce ce gve lve gtvs') scope
238 tcLookupLocalValue :: Name -> 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 -> Name -> 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 :: Name -> NF_TcM s Id
256 tcLookupGlobalValue name
257 = case maybeWiredInIdName name of
258 Just id -> returnNF_Tc id
259 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
260 returnNF_Tc (lookupWithDefaultUFM gve def name)
262 def = pprPanic "tcLookupGlobalValue:" (ppr PprDebug name)
264 tcLookupGlobalValueMaybe :: Name -> NF_TcM s (Maybe Id)
266 tcLookupGlobalValueMaybe name
267 = case maybeWiredInIdName name of
268 Just id -> returnNF_Tc (Just id)
269 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
270 returnNF_Tc (lookupUFM gve name)
273 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
274 tcLookupGlobalValueByKey uniq
275 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
276 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
279 def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
281 def = panic "tcLookupGlobalValueByKey"
284 tcLookupGlobalValueByKeyMaybe :: Unique -> NF_TcM s (Maybe Id)
285 tcLookupGlobalValueByKeyMaybe uniq
286 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
287 returnNF_Tc (lookupUFM_Directly gve uniq)
289 -- Extract the IdInfo from an IfaceSig imported from an interface file
290 tcAddImportedIdInfo :: Id -> NF_TcM s Id
291 tcAddImportedIdInfo id
292 = tcLookupGlobalValueMaybe (getName id) `thenNF_Tc` \ maybe_id ->
294 new_info = case maybe_id of
296 Just imported_id -> getIdInfo imported_id
297 -- ToDo: could check that types are the same
299 returnNF_Tc (id `replaceIdInfo` new_info)
300 -- The Id must be returned without a data dependency on maybe_id
308 -- Uses the Name as the Name of the Id
309 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
311 newMonoIds names kind m
312 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
314 new_ids = zipWithEqual "newMonoIds" mk_id names tys
315 mk_id name ty = mkUserId name ty NoPragmaInfo
317 tcExtendLocalValEnv names new_ids (m new_ids)
319 no_of_names = length names
321 newLocalId :: OccName -> TcType s -> NF_TcM s (TcIdBndr s)
323 = tcGetSrcLoc `thenNF_Tc` \ loc ->
324 tcGetUnique `thenNF_Tc` \ uniq ->
325 returnNF_Tc (mkUserLocal name uniq ty loc)
327 newLocalIds :: [OccName] -> [TcType s] -> NF_TcM s [TcIdBndr s]
328 newLocalIds names tys
329 = tcGetSrcLoc `thenNF_Tc` \ loc ->
330 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
332 new_ids = zipWith3Equal "newLocalIds" mk_id names uniqs tys
333 mk_id name uniq ty = mkUserLocal name uniq ty loc
339 classAsTyConErr name sty
340 = hcat [ptext SLIT("Class used as a type constructor: "), ppr sty name]
342 tyConAsClassErr name sty
343 = hcat [ptext SLIT("Type constructor used as a class: "), ppr sty name]