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 newMonoIds, newLocalIds, newLocalId,
20 tcGetGlobalTyVars, tcExtendGlobalTyVars
25 IMPORT_DELOOPER(TcMLoop) -- for paranoia checking
27 import HsTypes ( HsTyVar(..) )
28 import Id ( SYN_IE(Id), GenId, idType, mkUserLocal, mkUserId )
29 import PragmaInfo ( PragmaInfo(..) )
30 import TcHsSyn ( SYN_IE(TcIdBndr), TcIdOcc(..) )
31 import TcKind ( TcKind, newKindVars, newKindVar, tcDefaultKind, kindToTcKind )
32 import TcType ( SYN_IE(TcType), TcMaybe, SYN_IE(TcTyVar), SYN_IE(TcTyVarSet),
33 newTyVarTys, tcInstTyVars, zonkTcTyVars
35 import TyVar ( unionTyVarSets, emptyTyVarSet )
36 import Type ( tyVarsOfTypes, splitForAllTy )
37 import TyCon ( TyCon, tyConKind, synTyConArity )
38 import Class ( SYN_IE(Class), GenClass, classSig )
42 import Name ( Name, OccName(..), getSrcLoc, occNameString,
43 maybeWiredInTyConName, maybeWiredInIdName, pprSym
47 import Unique ( pprUnique10{-, pprUnique ToDo:rm-} )
49 import Util ( zipEqual, zipWithEqual, zipWith3Equal, zipLazy,
50 panic, pprPanic{-, pprTrace ToDo:rm-}
54 Data type declarations
62 (ValueEnv Id) -- Globals
63 (ValueEnv (TcIdBndr s)) -- Locals
64 (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
65 -- ...why mutable? see notes with tcGetGlobalTyVars
67 type TyVarEnv s = UniqFM (TcKind s, TyVar)
68 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
69 type ClassEnv s = UniqFM (TcKind s, Class)
70 type ValueEnv id = UniqFM id
72 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
73 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
75 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
76 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
77 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
83 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
84 tcExtendTyVarEnv names kinds_w_types scope
85 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
87 tve' = addListToUFM tve (zipEqual "tcTyVarScope" names kinds_w_types)
89 tcSetEnv (TcEnv tve' tce ce gve lve gtvs) scope
92 The Kind, TyVar, Class and TyCon envs
93 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95 Extending the environments. Notice the uses of @zipLazy@, which makes sure
96 that the knot-tied TyVars, TyCons and Classes aren't looked at too early.
99 tcExtendTyConEnv :: [(Name,Maybe Arity)] -> [TyCon] -> TcM s r -> TcM s r
101 tcExtendTyConEnv names_w_arities tycons scope
102 = newKindVars (length names_w_arities) `thenNF_Tc` \ kinds ->
103 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
105 tce' = addListToUFM tce [ (name, (kind, arity, tycon))
106 | ((name,arity), (kind,tycon))
107 <- zipEqual "tcExtendTyConEnv" names_w_arities (kinds `zipLazy` tycons)
110 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope `thenTc` \ result ->
111 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
115 tcExtendClassEnv :: [Name] -> [Class] -> TcM s r -> TcM s r
116 tcExtendClassEnv names classes scope
117 = newKindVars (length names) `thenNF_Tc` \ kinds ->
118 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
120 ce' = addListToUFM ce (zipEqual "tcExtendClassEnv" names (kinds `zipLazy` classes))
122 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope `thenTc` \ result ->
123 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
128 Looking up in the environments.
132 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
133 returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr PprShowAll name)) name)
137 = case maybeWiredInTyConName name of
138 Just tc -> returnTc (kindToTcKind (tyConKind tc), synTyConArity tc, tc)
139 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
140 case lookupUFM tce name of
141 Just stuff -> returnTc stuff
142 Nothing -> -- Could be that he's using a class name as a type constructor
143 case lookupUFM ce name of
144 Just _ -> failTc (classAsTyConErr name)
145 Nothing -> pprPanic "tcLookupTyCon:" (ppr PprDebug name)
147 tcLookupTyConByKey uniq
148 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
150 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
151 (pprPanic "tcLookupTyCon:" (pprUnique10 uniq))
157 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
158 -- pprTrace "tcLookupClass:" (ppCat [ppStr "Uniq:", pprUnique10 (uniqueOf name), ppStr "; avail:", ppCat (map (pprUnique10 . fst) (ufmToList ce))]) $
159 -- pprTrace "tcLookupClass:" (ppCat [ppStr "Uniq:", pprUnique (uniqueOf name), ppStr "; avail:", ppCat (map (pprUnique . fst) (ufmToList ce))]) $
160 case lookupUFM ce name of
161 Just stuff -> returnTc stuff
162 Nothing -> -- Could be that he's using a type constructor as a class
163 case lookupUFM tce name of
164 Just _ -> failTc (tyConAsClassErr name)
165 Nothing -> pprPanic "tcLookupClass:" (ppr PprShowAll name)
167 tcLookupClassByKey uniq
168 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
170 (kind, clas) = lookupWithDefaultUFM_Directly ce
171 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
176 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
177 tcGetTyConsAndClasses
178 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
179 returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
180 [c | (_, c) <- eltsUFM ce])
185 Extending and consulting the value environment
186 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
188 tcExtendGlobalValEnv ids scope
189 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
191 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
193 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
195 tcExtendLocalValEnv names ids scope
196 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
197 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
199 lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
200 extra_global_tyvars = tyVarsOfTypes (map idType ids)
201 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
203 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
205 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
208 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
209 To improve subsequent calls to the same function it writes the zonked set back into
213 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
215 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
216 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
217 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
218 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
219 returnNF_Tc global_tvs'
221 tcExtendGlobalTyVars extra_global_tvs scope
222 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
223 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
225 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tvs
227 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
228 tcSetEnv (TcEnv tve tce ce gve lve gtvs') scope
232 tcLookupLocalValue :: Name -> NF_TcM s (Maybe (TcIdBndr s))
233 tcLookupLocalValue name
234 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
235 returnNF_Tc (lookupUFM lve name)
237 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
238 tcLookupLocalValueByKey uniq
239 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
240 returnNF_Tc (lookupUFM_Directly lve uniq)
242 tcLookupLocalValueOK :: String -> Name -> NF_TcM s (TcIdBndr s)
243 tcLookupLocalValueOK err name
244 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
245 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
248 tcLookupGlobalValue :: Name -> NF_TcM s Id
250 tcLookupGlobalValue name
251 = case maybeWiredInIdName name of
252 Just id -> returnNF_Tc id
253 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
254 returnNF_Tc (lookupWithDefaultUFM gve def name)
256 def = pprPanic "tcLookupGlobalValue:" (ppr PprDebug name)
258 tcLookupGlobalValueMaybe :: Name -> NF_TcM s (Maybe Id)
260 tcLookupGlobalValueMaybe name
261 = case maybeWiredInIdName name of
262 Just id -> returnNF_Tc (Just id)
263 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
264 returnNF_Tc (lookupUFM gve name)
267 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
268 tcLookupGlobalValueByKey uniq
269 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
270 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
273 def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
275 def = panic "tcLookupGlobalValueByKey"
285 -- Uses the Name as the Name of the Id
286 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
288 newMonoIds names kind m
289 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
291 new_ids = zipWithEqual "newMonoIds" mk_id names tys
292 mk_id name ty = mkUserId name ty NoPragmaInfo
294 tcExtendLocalValEnv names new_ids (m new_ids)
296 no_of_names = length names
298 newLocalId :: OccName -> TcType s -> NF_TcM s (TcIdBndr s)
300 = tcGetSrcLoc `thenNF_Tc` \ loc ->
301 tcGetUnique `thenNF_Tc` \ uniq ->
302 returnNF_Tc (mkUserLocal name uniq ty loc)
304 newLocalIds :: [OccName] -> [TcType s] -> NF_TcM s [TcIdBndr s]
305 newLocalIds names tys
306 = tcGetSrcLoc `thenNF_Tc` \ loc ->
307 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
309 new_ids = zipWith3Equal "newLocalIds" mk_id names uniqs tys
310 mk_id name uniq ty = mkUserLocal name uniq ty loc
316 classAsTyConErr name sty
317 = ppBesides [ppStr "Class used as a type constructor: ", pprSym sty name]
319 tyConAsClassErr name sty
320 = ppBesides [ppStr "Type constructor used as a class: ", pprSym sty name]