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
61 Data type declarations
69 (ValueEnv Id) -- Globals
70 (ValueEnv (TcIdBndr s)) -- Locals
71 (MutableVar s (TcTyVarSet s)) -- Free type variables of locals
72 -- ...why mutable? see notes with tcGetGlobalTyVars
74 type TyVarEnv s = UniqFM (TcKind s, TyVar)
75 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
76 type ClassEnv s = UniqFM (TcKind s, Class)
77 type ValueEnv id = UniqFM id
79 initEnv :: MutableVar s (TcTyVarSet s) -> TcEnv s
80 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
82 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
83 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
84 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
90 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
91 tcExtendTyVarEnv names kinds_w_types scope
92 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
94 tve' = addListToUFM tve (zipEqual "tcTyVarScope" names kinds_w_types)
96 tcSetEnv (TcEnv tve' tce ce gve lve gtvs) scope
99 The Kind, TyVar, Class and TyCon envs
100 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
102 Extending the environments. Notice the uses of @zipLazy@, which makes sure
103 that the knot-tied TyVars, TyCons and Classes aren't looked at too early.
106 tcExtendTyConEnv :: [(Name,Maybe Arity)] -> [TyCon] -> TcM s r -> TcM s r
108 tcExtendTyConEnv names_w_arities tycons scope
109 = newKindVars (length names_w_arities) `thenNF_Tc` \ kinds ->
110 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
112 tce' = addListToUFM tce [ (name, (kind, arity, tycon))
113 | ((name,arity), (kind,tycon))
114 <- zipEqual "tcExtendTyConEnv" names_w_arities (kinds `zipLazy` tycons)
117 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope `thenTc` \ result ->
118 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
122 tcExtendClassEnv :: [Name] -> [Class] -> TcM s r -> TcM s r
123 tcExtendClassEnv names classes scope
124 = newKindVars (length names) `thenNF_Tc` \ kinds ->
125 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
127 ce' = addListToUFM ce (zipEqual "tcExtendClassEnv" names (kinds `zipLazy` classes))
129 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope `thenTc` \ result ->
130 mapNF_Tc tcDefaultKind kinds `thenNF_Tc_`
135 Looking up in the environments.
139 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
140 returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr PprShowAll name)) name)
144 = -- Try for a wired-in tycon
145 case maybeWiredInTyConName name of {
146 Just tc | isSynTyCon tc -> returnTc (kind, Just (tyConArity tc), tc)
147 | otherwise -> returnTc (kind, Nothing, tc)
149 kind = kindToTcKind (tyConKind tc)
154 -- Try in the environment
155 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
156 case lookupUFM tce name of {
157 Just stuff -> returnTc stuff;
161 -- Could be that he's using a class name as a type constructor
162 case lookupUFM ce name of
163 Just _ -> failTc (classAsTyConErr name)
164 Nothing -> pprPanic "tcLookupTyCon:" (ppr PprDebug name)
167 tcLookupTyConByKey uniq
168 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
170 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
171 (pprPanic "tcLookupTyConByKey:" (pprUnique10 uniq))
177 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
178 -- pprTrace "tcLookupClass:" (hsep [text "Uniq:", pprUnique10 (uniqueOf name), text "; avail:", hsep (map (pprUnique10 . fst) (ufmToList ce))]) $
179 -- pprTrace "tcLookupClass:" (hsep [text "Uniq:", pprUnique (uniqueOf name), text "; avail:", hsep (map (pprUnique . fst) (ufmToList ce))]) $
180 case lookupUFM ce name of
181 Just stuff -> returnTc stuff
182 Nothing -> -- Could be that he's using a type constructor as a class
183 case lookupUFM tce name of
184 Just _ -> failTc (tyConAsClassErr name)
185 Nothing -> pprPanic "tcLookupClass:" (ppr PprShowAll name)
187 tcLookupClassByKey uniq
188 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
190 (kind, clas) = lookupWithDefaultUFM_Directly ce
191 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
196 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
197 tcGetTyConsAndClasses
198 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
199 returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
200 [c | (_, c) <- eltsUFM ce])
205 Extending and consulting the value environment
206 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
208 tcExtendGlobalValEnv ids scope
209 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
211 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
213 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
215 tcExtendLocalValEnv names ids scope
216 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
217 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
219 lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
220 extra_global_tyvars = tyVarsOfTypes (map idType ids)
221 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
223 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
225 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
228 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
229 To improve subsequent calls to the same function it writes the zonked set back into
233 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
235 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
236 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
237 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
238 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
239 returnNF_Tc global_tvs'
241 tcExtendGlobalTyVars extra_global_tvs scope
242 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
243 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
245 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tvs
247 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
248 tcSetEnv (TcEnv tve tce ce gve lve gtvs') scope
252 tcLookupLocalValue :: Name -> NF_TcM s (Maybe (TcIdBndr s))
253 tcLookupLocalValue name
254 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
255 returnNF_Tc (lookupUFM lve name)
257 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
258 tcLookupLocalValueByKey uniq
259 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
260 returnNF_Tc (lookupUFM_Directly lve uniq)
262 tcLookupLocalValueOK :: String -> Name -> NF_TcM s (TcIdBndr s)
263 tcLookupLocalValueOK err name
264 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
265 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
268 tcLookupGlobalValue :: Name -> NF_TcM s Id
269 tcLookupGlobalValue name
270 = case maybeWiredInIdName name of
271 Just id -> returnNF_Tc id
272 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
273 returnNF_Tc (lookupWithDefaultUFM gve def name)
275 def = pprPanic "tcLookupGlobalValue:" (ppr PprDebug name)
277 tcLookupGlobalValueMaybe :: Name -> NF_TcM s (Maybe Id)
278 tcLookupGlobalValueMaybe name
279 = case maybeWiredInIdName name of
280 Just id -> returnNF_Tc (Just id)
281 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
282 returnNF_Tc (lookupUFM gve name)
285 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
286 tcLookupGlobalValueByKey uniq
287 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
288 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
291 def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
293 def = panic "tcLookupGlobalValueByKey"
296 tcLookupGlobalValueByKeyMaybe :: Unique -> NF_TcM s (Maybe Id)
297 tcLookupGlobalValueByKeyMaybe uniq
298 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
299 returnNF_Tc (lookupUFM_Directly gve uniq)
302 -- Non-monadic version, environment given explicitly
303 tcExplicitLookupGlobal :: TcEnv s -> Name -> Maybe Id
304 tcExplicitLookupGlobal (TcEnv tve tce ce gve lve gtvs) name
305 = case maybeWiredInIdName name of
307 Nothing -> lookupUFM gve name
309 -- Extract the IdInfo from an IfaceSig imported from an interface file
310 tcAddImportedIdInfo :: TcEnv s -> Id -> Id
311 tcAddImportedIdInfo unf_env id
312 | isLocallyDefined id -- Don't look up locally defined Ids, because they
313 -- have explicit local definitions, so we get a black hole!
316 = id `replaceIdInfo` new_info
317 -- The Id must be returned without a data dependency on maybe_id
319 new_info = -- pprTrace "tcAdd" (ppr PprDebug id) $
320 case tcExplicitLookupGlobal unf_env (getName id) of
322 Just imported_id -> getIdInfo imported_id
323 -- ToDo: could check that types are the same
331 -- Uses the Name as the Name of the Id
332 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
334 newMonoIds names kind m
335 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
337 new_ids = zipWithEqual "newMonoIds" mk_id names tys
338 mk_id name ty = mkUserId name ty NoPragmaInfo
340 tcExtendLocalValEnv names new_ids (m new_ids)
342 no_of_names = length names
344 newLocalId :: OccName -> TcType s -> NF_TcM s (TcIdBndr s)
346 = tcGetSrcLoc `thenNF_Tc` \ loc ->
347 tcGetUnique `thenNF_Tc` \ uniq ->
348 returnNF_Tc (mkUserLocal name uniq ty loc)
350 newLocalIds :: [OccName] -> [TcType s] -> NF_TcM s [TcIdBndr s]
351 newLocalIds names tys
352 = tcGetSrcLoc `thenNF_Tc` \ loc ->
353 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
355 new_ids = zipWith3Equal "newLocalIds" mk_id names uniqs tys
356 mk_id name uniq ty = mkUserLocal name uniq ty loc
362 classAsTyConErr name sty
363 = hcat [ptext SLIT("Class used as a type constructor: "), ppr sty name]
365 tyConAsClassErr name sty
366 = hcat [ptext SLIT("Type constructor used as a class: "), ppr sty name]