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 tcAddImportedIdInfo, tcExplicitLookupGlobal,
19 tcLookupGlobalValueByKeyMaybe,
21 newMonoIds, newLocalIds, newLocalId,
22 tcGetGlobalTyVars, tcExtendGlobalTyVars
27 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
28 IMPORT_DELOOPER(TcMLoop) -- for paranoia checking
31 import HsTypes ( HsTyVar(..) )
32 import Id ( SYN_IE(Id), GenId, idType, mkUserLocal, mkUserId, replaceIdInfo, getIdInfo )
33 import PragmaInfo ( PragmaInfo(..) )
34 import TcKind ( TcKind, newKindVars, newKindVar, tcDefaultKind, kindToTcKind, Kind )
35 import TcType ( SYN_IE(TcIdBndr), TcIdOcc(..),
36 SYN_IE(TcType), TcMaybe, SYN_IE(TcTyVar), SYN_IE(TcTyVarSet),
37 newTyVarTys, tcInstTyVars, zonkTcTyVars
39 import TyVar ( unionTyVarSets, emptyTyVarSet, tyVarSetToList, SYN_IE(TyVar) )
40 import PprType ( GenTyVar )
41 import Type ( tyVarsOfTypes, splitForAllTy )
42 import TyCon ( TyCon, tyConKind, tyConArity, isSynTyCon, SYN_IE(Arity) )
43 import Class ( SYN_IE(Class), GenClass )
47 import IdInfo ( noIdInfo )
48 import Name ( Name, OccName(..), getSrcLoc, occNameString,
49 maybeWiredInTyConName, maybeWiredInIdName, isLocallyDefined,
53 import Unique ( pprUnique10{-, pprUnique ToDo:rm-}, Unique, Uniquable(..) )
55 import Util ( zipEqual, zipWithEqual, zipWith3Equal, zipLazy,
56 panic, pprPanic, pprTrace
58 import Maybes ( maybeToBool )
62 Data type declarations
70 (ValueEnv Id) -- Globals
71 (ValueEnv (TcIdBndr s)) -- Locals
72 (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
73 -- ...why mutable? see notes with tcGetGlobalTyVars
75 type TyVarEnv s = UniqFM (TcKind s, TyVar)
76 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
77 type ClassEnv s = UniqFM (TcKind s, Class)
78 type ValueEnv id = UniqFM id
80 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
81 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
83 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
84 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
85 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
91 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
92 tcExtendTyVarEnv names kinds_w_types scope
93 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
95 tve' = addListToUFM tve (zipEqual "tcTyVarScope" names kinds_w_types)
97 tcSetEnv (TcEnv tve' tce ce gve lve gtvs) scope
100 The Kind, TyVar, Class and TyCon envs
101 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
103 Extending the environments. Notice the uses of @zipLazy@, which makes sure
104 that the knot-tied TyVars, TyCons and Classes aren't looked at too early.
107 tcExtendTyConEnv :: [(Name,Maybe Arity)] -> [TyCon] -> TcM s r -> TcM s r
109 tcExtendTyConEnv names_w_arities tycons scope
110 = newKindVars (length names_w_arities) `thenNF_Tc` \ kinds ->
111 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
113 tce' = addListToUFM tce [ (name, (kind, arity, tycon))
114 | ((name,arity), (kind,tycon))
115 <- zipEqual "tcExtendTyConEnv" names_w_arities (kinds `zipLazy` tycons)
118 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope `thenTc` \ result ->
119 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
123 tcExtendClassEnv :: [Name] -> [Class] -> TcM s r -> TcM s r
124 tcExtendClassEnv names classes scope
125 = newKindVars (length names) `thenNF_Tc` \ kinds ->
126 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
128 ce' = addListToUFM ce (zipEqual "tcExtendClassEnv" names (kinds `zipLazy` classes))
130 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope `thenTc` \ result ->
131 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
136 Looking up in the environments.
140 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
141 returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr PprShowAll name)) name)
145 = -- Try for a wired-in tycon
146 case maybeWiredInTyConName name of {
147 Just tc | isSynTyCon tc -> returnTc (kind, Just (tyConArity tc), tc)
148 | otherwise -> returnTc (kind, Nothing, tc)
150 kind = kindToTcKind (tyConKind tc)
155 -- Try in the environment
156 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
157 case lookupUFM tce name of {
158 Just stuff -> returnTc stuff;
162 -- Could be that he's using a class name as a type constructor
163 case lookupUFM ce name of
164 Just _ -> failTc (classAsTyConErr name)
165 Nothing -> pprPanic "tcLookupTyCon:" (ppr PprDebug name)
168 tcLookupTyConByKey uniq
169 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
171 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
172 (pprPanic "tcLookupTyConByKey:" (pprUnique10 uniq))
178 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
179 case lookupUFM ce name of
180 Just stuff -- Common case: it's ok
183 Nothing -- Could be that he's using a type constructor as a class
184 | maybeToBool (maybeWiredInTyConName name)
185 || maybeToBool (lookupUFM tce name)
186 -> failTc (tyConAsClassErr name)
188 | otherwise -- Wierd! Renamer shouldn't let this happen
189 -> pprPanic "tcLookupClass:" (ppr PprShowAll name)
191 tcLookupClassByKey uniq
192 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
194 (kind, clas) = lookupWithDefaultUFM_Directly ce
195 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
200 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
201 tcGetTyConsAndClasses
202 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
203 returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
204 [c | (_, c) <- eltsUFM ce])
209 Extending and consulting the value environment
210 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212 tcExtendGlobalValEnv ids scope
213 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
215 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
217 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
219 tcExtendLocalValEnv names ids scope
220 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
221 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
223 lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
224 extra_global_tyvars = tyVarsOfTypes (map idType ids)
225 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
227 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
229 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
232 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
233 To improve subsequent calls to the same function it writes the zonked set back into
237 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
239 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
240 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
241 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
242 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
243 returnNF_Tc global_tvs'
245 tcExtendGlobalTyVars extra_global_tvs scope
246 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
247 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
249 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tvs
251 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
252 tcSetEnv (TcEnv tve tce ce gve lve gtvs') scope
256 tcLookupLocalValue :: Name -> NF_TcM s (Maybe (TcIdBndr s))
257 tcLookupLocalValue name
258 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
259 returnNF_Tc (lookupUFM lve name)
261 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
262 tcLookupLocalValueByKey uniq
263 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
264 returnNF_Tc (lookupUFM_Directly lve uniq)
266 tcLookupLocalValueOK :: String -> Name -> NF_TcM s (TcIdBndr s)
267 tcLookupLocalValueOK err name
268 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
269 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
272 tcLookupGlobalValue :: Name -> NF_TcM s Id
273 tcLookupGlobalValue name
274 = case maybeWiredInIdName name of
275 Just id -> returnNF_Tc id
276 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
277 returnNF_Tc (lookupWithDefaultUFM gve def name)
279 def = pprPanic "tcLookupGlobalValue:" (ppr PprDebug name)
281 tcLookupGlobalValueMaybe :: Name -> NF_TcM s (Maybe Id)
282 tcLookupGlobalValueMaybe name
283 = case maybeWiredInIdName name of
284 Just id -> returnNF_Tc (Just id)
285 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
286 returnNF_Tc (lookupUFM gve name)
289 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
290 tcLookupGlobalValueByKey uniq
291 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
292 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
295 def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
297 def = panic "tcLookupGlobalValueByKey"
300 tcLookupGlobalValueByKeyMaybe :: Unique -> NF_TcM s (Maybe Id)
301 tcLookupGlobalValueByKeyMaybe uniq
302 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
303 returnNF_Tc (lookupUFM_Directly gve uniq)
306 -- Non-monadic version, environment given explicitly
307 tcExplicitLookupGlobal :: TcEnv s -> Name -> Maybe Id
308 tcExplicitLookupGlobal (TcEnv tve tce ce gve lve gtvs) name
309 = case maybeWiredInIdName name of
311 Nothing -> lookupUFM gve name
313 -- Extract the IdInfo from an IfaceSig imported from an interface file
314 tcAddImportedIdInfo :: TcEnv s -> Id -> Id
315 tcAddImportedIdInfo unf_env id
316 | isLocallyDefined id -- Don't look up locally defined Ids, because they
317 -- have explicit local definitions, so we get a black hole!
320 = id `replaceIdInfo` new_info
321 -- The Id must be returned without a data dependency on maybe_id
323 new_info = -- pprTrace "tcAdd" (ppr PprDebug id) $
324 case tcExplicitLookupGlobal unf_env (getName id) of
326 Just imported_id -> getIdInfo imported_id
327 -- ToDo: could check that types are the same
335 -- Uses the Name as the Name of the Id
336 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
338 newMonoIds names kind m
339 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
341 new_ids = zipWithEqual "newMonoIds" mk_id names tys
342 mk_id name ty = mkUserId name ty NoPragmaInfo
344 tcExtendLocalValEnv names new_ids (m new_ids)
346 no_of_names = length names
348 newLocalId :: OccName -> TcType s -> NF_TcM s (TcIdBndr s)
350 = tcGetSrcLoc `thenNF_Tc` \ loc ->
351 tcGetUnique `thenNF_Tc` \ uniq ->
352 returnNF_Tc (mkUserLocal name uniq ty loc)
354 newLocalIds :: [OccName] -> [TcType s] -> NF_TcM s [TcIdBndr s]
355 newLocalIds names tys
356 = tcGetSrcLoc `thenNF_Tc` \ loc ->
357 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
359 new_ids = zipWith3Equal "newLocalIds" mk_id names uniqs tys
360 mk_id name uniq ty = mkUserLocal name uniq ty loc
366 classAsTyConErr name sty
367 = hcat [ptext SLIT("Class used as a type constructor: "), ppr sty name]
369 tyConAsClassErr name sty
370 = hcat [ptext SLIT("Type constructor used as a class: "), ppr sty name]