3 TcIdOcc(..), TcIdBndr, tcIdType, tcIdTyVars, tcInstId,
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
25 #include "HsVersions.h"
27 import HsTypes ( HsTyVar(..) )
28 import Id ( Id, GenId, idType, mkUserLocal, mkUserId, replaceIdInfo, getIdInfo )
29 import PragmaInfo ( PragmaInfo(..) )
30 import TcKind ( TcKind, newKindVars, newKindVar, tcDefaultKind, kindToTcKind, Kind )
31 import TcType ( TcType, TcMaybe, TcTyVar, TcTyVarSet, TcThetaType,
32 newTyVarTys, tcInstTyVars, zonkTcTyVars, tcInstType
34 import TyVar ( mkTyVarSet, unionTyVarSets, emptyTyVarSet, tyVarSetToList, TyVar )
35 import PprType ( GenTyVar )
36 import Type ( tyVarsOfType, tyVarsOfTypes, splitForAllTys, splitRhoTy )
37 import TyCon ( TyCon, tyConKind, tyConArity, isSynTyCon, Arity )
38 import Class ( Class )
42 import IdInfo ( noIdInfo )
43 import Name ( Name, OccName(..), getSrcLoc, occNameString,
44 maybeWiredInTyConName, maybeWiredInIdName, isLocallyDefined,
47 import Unique ( pprUnique10{-, pprUnique ToDo:rm-}, Unique, Uniquable(..) )
49 import Util ( zipEqual, zipWithEqual, zipWith3Equal, zipLazy
51 import Maybes ( maybeToBool )
55 %************************************************************************
57 \subsection{TcId, TcIdOcc}
59 %************************************************************************
63 type TcIdBndr s = GenId (TcType s) -- Binders are all TcTypes
64 data TcIdOcc s = TcId (TcIdBndr s) -- Bindees may be either
67 instance Eq (TcIdOcc s) where
68 (TcId id1) == (TcId id2) = id1 == id2
69 (RealId id1) == (RealId id2) = id1 == id2
72 instance Ord (TcIdOcc s) where
73 (TcId id1) `compare` (TcId id2) = id1 `compare` id2
74 (RealId id1) `compare` (RealId id2) = id1 `compare` id2
75 (TcId _) `compare` (RealId _) = LT
76 (RealId _) `compare` (TcId _) = GT
78 instance Outputable (TcIdOcc s) where
79 ppr (TcId id) = ppr id
80 ppr (RealId id) = ppr id
82 instance NamedThing (TcIdOcc s) where
83 getName (TcId id) = getName id
84 getName (RealId id) = getName id
87 tcIdType :: TcIdOcc s -> TcType s
88 tcIdType (TcId id) = idType id
89 tcIdType (RealId id) = pprPanic "tcIdType:" (ppr id)
91 tcIdTyVars (TcId id) = tyVarsOfType (idType id)
92 tcIdTyVars (RealId _) = emptyTyVarSet -- Top level Ids have no free type variables
95 -- A useful function that takes an occurrence of a global thing
96 -- and instantiates its type with fresh type variables
98 -> NF_TcM s ([TcTyVar s], -- It's instantiated type
104 (tyvars, rho) = splitForAllTys (idType id)
106 tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', arg_tys, tenv) ->
107 tcInstType tenv rho `thenNF_Tc` \ rho' ->
109 (theta', tau') = splitRhoTy rho'
111 returnNF_Tc (tyvars', theta', tau')
115 %************************************************************************
119 %************************************************************************
121 Data type declarations
122 ~~~~~~~~~~~~~~~~~~~~~
129 (ValueEnv Id) -- Globals
130 (ValueEnv (TcIdBndr s)) -- Locals
131 (TcRef s (TcTyVarSet s)) -- Free type variables of locals
132 -- ...why mutable? see notes with tcGetGlobalTyVars
134 type TyVarEnv s = UniqFM (TcKind s, TyVar)
135 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
136 type ClassEnv s = UniqFM ([TcKind s], Class) -- The kinds are the kinds of the args
138 type ValueEnv id = UniqFM id
140 initEnv :: TcRef s (TcTyVarSet s) -> TcEnv s
141 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
143 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
144 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
145 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
151 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
152 tcExtendTyVarEnv names kinds_w_types scope
153 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
155 tve' = addListToUFM tve (zipEqual "tcTyVarScope" names kinds_w_types)
157 tcSetEnv (TcEnv tve' tce ce gve lve gtvs) scope
160 The Kind, TyVar, Class and TyCon envs
161 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
163 Extending the environments.
166 tcExtendTyConEnv :: [(Name, (TcKind s, Maybe Arity, TyCon))] -> TcM s r -> TcM s r
168 tcExtendTyConEnv bindings scope
169 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
171 tce' = addListToUFM tce bindings
173 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope
176 tcExtendClassEnv :: [(Name, ([TcKind s], Class))] -> TcM s r -> TcM s r
177 tcExtendClassEnv bindings scope
178 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
180 ce' = addListToUFM ce bindings
182 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope
186 Looking up in the environments.
190 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
191 returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr name)) name)
195 = -- Try for a wired-in tycon
196 case maybeWiredInTyConName name of {
197 Just tc | isSynTyCon tc -> returnTc (kind, Just (tyConArity tc), tc)
198 | otherwise -> returnTc (kind, Nothing, tc)
200 kind = kindToTcKind (tyConKind tc)
205 -- Try in the environment
206 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
207 case lookupUFM tce name of {
208 Just stuff -> returnTc stuff;
212 -- Could be that he's using a class name as a type constructor
213 case lookupUFM ce name of
214 Just _ -> failWithTc (classAsTyConErr name)
215 Nothing -> pprPanic "tcLookupTyCon:" (ppr name)
218 tcLookupTyConByKey uniq
219 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
221 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
222 (pprPanic "tcLookupTyConByKey:" (pprUnique10 uniq))
228 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
229 case lookupUFM ce name of
230 Just stuff -- Common case: it's ok
233 Nothing -- Could be that he's using a type constructor as a class
234 | maybeToBool (maybeWiredInTyConName name)
235 || maybeToBool (lookupUFM tce name)
236 -> failWithTc (tyConAsClassErr name)
238 | otherwise -- Wierd! Renamer shouldn't let this happen
239 -> pprPanic "tcLookupClass" (ppr name)
241 tcLookupClassByKey uniq
242 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
244 (kind, clas) = lookupWithDefaultUFM_Directly ce
245 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
250 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
251 tcGetTyConsAndClasses
252 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
253 returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
254 [c | (_, c) <- eltsUFM ce])
259 Extending and consulting the value environment
260 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
262 tcExtendGlobalValEnv ids scope
263 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
265 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
267 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
269 tcExtendLocalValEnv names ids scope
270 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
271 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
273 lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
274 extra_global_tyvars = tyVarsOfTypes (map idType ids)
275 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
277 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
279 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
282 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
283 To improve subsequent calls to the same function it writes the zonked set back into
287 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
289 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
290 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
291 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
292 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
293 returnNF_Tc global_tvs'
295 tcExtendGlobalTyVars extra_global_tvs scope
296 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
297 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
299 new_global_tyvars = global_tvs `unionTyVarSets` mkTyVarSet extra_global_tvs
301 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
302 tcSetEnv (TcEnv tve tce ce gve lve gtvs') scope
306 tcLookupLocalValue :: Name -> NF_TcM s (Maybe (TcIdBndr s))
307 tcLookupLocalValue name
308 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
309 returnNF_Tc (lookupUFM lve name)
311 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
312 tcLookupLocalValueByKey uniq
313 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
314 returnNF_Tc (lookupUFM_Directly lve uniq)
316 tcLookupLocalValueOK :: String -> Name -> NF_TcM s (TcIdBndr s)
317 tcLookupLocalValueOK err name
318 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
319 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
322 tcLookupGlobalValue :: Name -> NF_TcM s Id
323 tcLookupGlobalValue name
324 = case maybeWiredInIdName name of
325 Just id -> returnNF_Tc id
326 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
327 returnNF_Tc (lookupWithDefaultUFM gve def name)
329 def = pprPanic "tcLookupGlobalValue:" (ppr name)
331 tcLookupGlobalValueMaybe :: Name -> NF_TcM s (Maybe Id)
332 tcLookupGlobalValueMaybe name
333 = case maybeWiredInIdName name of
334 Just id -> returnNF_Tc (Just id)
335 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
336 returnNF_Tc (lookupUFM gve name)
339 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
340 tcLookupGlobalValueByKey uniq
341 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
342 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
345 def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
347 def = panic "tcLookupGlobalValueByKey"
350 tcLookupGlobalValueByKeyMaybe :: Unique -> NF_TcM s (Maybe Id)
351 tcLookupGlobalValueByKeyMaybe uniq
352 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
353 returnNF_Tc (lookupUFM_Directly gve uniq)
356 -- Non-monadic version, environment given explicitly
357 tcExplicitLookupGlobal :: TcEnv s -> Name -> Maybe Id
358 tcExplicitLookupGlobal (TcEnv tve tce ce gve lve gtvs) name
359 = case maybeWiredInIdName name of
361 Nothing -> lookupUFM gve name
363 -- Extract the IdInfo from an IfaceSig imported from an interface file
364 tcAddImportedIdInfo :: TcEnv s -> Id -> Id
365 tcAddImportedIdInfo unf_env id
366 | isLocallyDefined id -- Don't look up locally defined Ids, because they
367 -- have explicit local definitions, so we get a black hole!
370 = id `replaceIdInfo` new_info
371 -- The Id must be returned without a data dependency on maybe_id
373 new_info = -- pprTrace "tcAdd" (ppr id) $
374 case tcExplicitLookupGlobal unf_env (getName id) of
376 Just imported_id -> getIdInfo imported_id
377 -- ToDo: could check that types are the same
385 -- Uses the Name as the Name of the Id
386 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
388 newMonoIds names kind m
389 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
391 new_ids = zipWithEqual "newMonoIds" mk_id names tys
392 mk_id name ty = mkUserId name ty NoPragmaInfo
394 tcExtendLocalValEnv names new_ids (m new_ids)
396 no_of_names = length names
398 newLocalId :: OccName -> TcType s -> NF_TcM s (TcIdBndr s)
400 = tcGetSrcLoc `thenNF_Tc` \ loc ->
401 tcGetUnique `thenNF_Tc` \ uniq ->
402 returnNF_Tc (mkUserLocal name uniq ty loc)
404 newLocalIds :: [OccName] -> [TcType s] -> NF_TcM s [TcIdBndr s]
405 newLocalIds names tys
406 = tcGetSrcLoc `thenNF_Tc` \ loc ->
407 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
409 new_ids = zipWith3Equal "newLocalIds" mk_id names uniqs tys
410 mk_id name uniq ty = mkUserLocal name uniq ty loc
418 = ptext SLIT("Class used as a type constructor:") <+> ppr name
421 = ptext SLIT("Type constructor used as a class:") <+> ppr name