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, newSpecPragmaId,
22 tcGetGlobalTyVars, tcExtendGlobalTyVars
25 #include "HsVersions.h"
27 import MkId ( mkUserLocal, mkUserId, mkSpecPragmaId )
28 import Id ( Id, GenId, idType, replaceIdInfo, idInfo )
29 import TcKind ( TcKind, kindToTcKind, Kind )
30 import TcType ( TcType, TcMaybe, TcTyVar, TcTyVarSet, TcThetaType,
31 newTyVarTys, tcInstTyVars, zonkTcTyVars, tcInstType
33 import TyVar ( mkTyVarSet, unionTyVarSets, emptyTyVarSet, TyVar )
34 import Type ( tyVarsOfType, tyVarsOfTypes, splitForAllTys, splitRhoTy )
35 import TyCon ( TyCon, tyConKind, tyConArity, isSynTyCon, Arity )
36 import Class ( Class )
40 import IdInfo ( noIdInfo )
41 import Name ( Name, OccName(..), nameOccName,
42 maybeWiredInTyConName, maybeWiredInIdName, isLocallyDefined,
45 import Unique ( pprUnique10{-, pprUnique ToDo:rm-}, Unique, Uniquable(..) )
47 import Util ( zipEqual, zipWithEqual, zipWith3Equal )
48 import Maybes ( maybeToBool )
52 %************************************************************************
54 \subsection{TcId, TcIdOcc}
56 %************************************************************************
60 type TcIdBndr s = GenId (TcType s) -- Binders are all TcTypes
61 data TcIdOcc s = TcId (TcIdBndr s) -- Bindees may be either
64 instance Eq (TcIdOcc s) where
65 (TcId id1) == (TcId id2) = id1 == id2
66 (RealId id1) == (RealId id2) = id1 == id2
69 instance Ord (TcIdOcc s) where
70 (TcId id1) `compare` (TcId id2) = id1 `compare` id2
71 (RealId id1) `compare` (RealId id2) = id1 `compare` id2
72 (TcId _) `compare` (RealId _) = LT
73 (RealId _) `compare` (TcId _) = GT
75 instance Outputable (TcIdOcc s) where
76 ppr (TcId id) = ppr id
77 ppr (RealId id) = ppr id
79 instance NamedThing (TcIdOcc s) where
80 getName (TcId id) = getName id
81 getName (RealId id) = getName id
84 tcIdType :: TcIdOcc s -> TcType s
85 tcIdType (TcId id) = idType id
86 tcIdType (RealId id) = pprPanic "tcIdType:" (ppr id)
88 tcIdTyVars (TcId id) = tyVarsOfType (idType id)
89 tcIdTyVars (RealId _) = emptyTyVarSet -- Top level Ids have no free type variables
92 -- A useful function that takes an occurrence of a global thing
93 -- and instantiates its type with fresh type variables
95 -> NF_TcM s ([TcTyVar s], -- It's instantiated type
101 (tyvars, rho) = splitForAllTys (idType id)
103 tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', arg_tys, tenv) ->
104 tcInstType tenv rho `thenNF_Tc` \ rho' ->
106 (theta', tau') = splitRhoTy rho'
108 returnNF_Tc (tyvars', theta', tau')
112 %************************************************************************
116 %************************************************************************
118 Data type declarations
119 ~~~~~~~~~~~~~~~~~~~~~
126 (ValueEnv Id) -- Globals
127 (ValueEnv (TcIdBndr s)) -- Locals
128 (TcRef s (TcTyVarSet s)) -- Free type variables of locals
129 -- ...why mutable? see notes with tcGetGlobalTyVars
131 type TyVarEnv s = UniqFM (TcKind s, TyVar)
132 type TyConEnv s = UniqFM (TcKind s, Maybe Arity, TyCon) -- Arity present for Synonyms only
133 type ClassEnv s = UniqFM ([TcKind s], Class) -- The kinds are the kinds of the args
135 type ValueEnv id = UniqFM id
137 initEnv :: TcRef s (TcTyVarSet s) -> TcEnv s
138 initEnv mut = TcEnv emptyUFM emptyUFM emptyUFM emptyUFM emptyUFM mut
140 getEnv_LocalIds (TcEnv _ _ _ _ ls _) = eltsUFM ls
141 getEnv_TyCons (TcEnv _ ts _ _ _ _) = [tycon | (_, _, tycon) <- eltsUFM ts]
142 getEnv_Classes (TcEnv _ _ cs _ _ _) = [clas | (_, clas) <- eltsUFM cs]
148 tcExtendTyVarEnv :: [Name] -> [(TcKind s, TyVar)] -> TcM s r -> TcM s r
149 tcExtendTyVarEnv names kinds_w_types scope
150 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
152 tve' = addListToUFM tve (zipEqual "tcTyVarScope" names kinds_w_types)
154 tcSetEnv (TcEnv tve' tce ce gve lve gtvs) scope
157 The Kind, TyVar, Class and TyCon envs
158 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
160 Extending the environments.
163 tcExtendTyConEnv :: [(Name, (TcKind s, Maybe Arity, TyCon))] -> TcM s r -> TcM s r
165 tcExtendTyConEnv bindings scope
166 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
168 tce' = addListToUFM tce bindings
170 tcSetEnv (TcEnv tve tce' ce gve lve gtvs) scope
173 tcExtendClassEnv :: [(Name, ([TcKind s], Class))] -> TcM s r -> TcM s r
174 tcExtendClassEnv bindings scope
175 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
177 ce' = addListToUFM ce bindings
179 tcSetEnv (TcEnv tve tce ce' gve lve gtvs) scope
183 Looking up in the environments.
187 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
188 returnNF_Tc (lookupWithDefaultUFM tve (pprPanic "tcLookupTyVar:" (ppr name)) name)
192 = -- Try for a wired-in tycon
193 case maybeWiredInTyConName name of {
194 Just tc | isSynTyCon tc -> returnTc (kind, Just (tyConArity tc), tc)
195 | otherwise -> returnTc (kind, Nothing, tc)
197 kind = kindToTcKind (tyConKind tc)
202 -- Try in the environment
203 tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
204 case lookupUFM tce name of {
205 Just stuff -> returnTc stuff;
209 -- Could be that he's using a class name as a type constructor
210 case lookupUFM ce name of
211 Just _ -> failWithTc (classAsTyConErr name)
212 Nothing -> pprPanic "tcLookupTyCon:" (ppr name)
215 tcLookupTyConByKey uniq
216 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
218 (kind, arity, tycon) = lookupWithDefaultUFM_Directly tce
219 (pprPanic "tcLookupTyConByKey:" (pprUnique10 uniq))
225 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
226 case lookupUFM ce name of
227 Just stuff -- Common case: it's ok
230 Nothing -- Could be that he's using a type constructor as a class
231 | maybeToBool (maybeWiredInTyConName name)
232 || maybeToBool (lookupUFM tce name)
233 -> failWithTc (tyConAsClassErr name)
235 | otherwise -- Wierd! Renamer shouldn't let this happen
236 -> pprPanic "tcLookupClass" (ppr name)
238 tcLookupClassByKey uniq
239 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
241 (kind, clas) = lookupWithDefaultUFM_Directly ce
242 (pprPanic "tcLookupClassByKey:" (pprUnique10 uniq))
247 tcGetTyConsAndClasses :: NF_TcM s ([TyCon], [Class])
248 tcGetTyConsAndClasses
249 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
250 returnNF_Tc ([tc | (_, _, tc) <- eltsUFM tce],
251 [c | (_, c) <- eltsUFM ce])
256 Extending and consulting the value environment
257 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
259 tcExtendGlobalValEnv ids scope
260 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
262 gve' = addListToUFM_Directly gve [(uniqueOf id, id) | id <- ids]
264 tcSetEnv (TcEnv tve tce ce gve' lve gtvs) scope
266 tcExtendLocalValEnv names ids scope
267 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
268 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
270 lve' = addListToUFM lve (zipEqual "tcExtendLocalValEnv" names ids)
271 extra_global_tyvars = tyVarsOfTypes (map idType ids)
272 new_global_tyvars = global_tvs `unionTyVarSets` extra_global_tyvars
274 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
276 tcSetEnv (TcEnv tve tce ce gve lve' gtvs') scope
279 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
280 To improve subsequent calls to the same function it writes the zonked set back into
284 tcGetGlobalTyVars :: NF_TcM s (TcTyVarSet s)
286 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
287 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
288 zonkTcTyVars global_tvs `thenNF_Tc` \ global_tvs' ->
289 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
290 returnNF_Tc global_tvs'
292 tcExtendGlobalTyVars extra_global_tvs scope
293 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
294 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
296 new_global_tyvars = global_tvs `unionTyVarSets` mkTyVarSet extra_global_tvs
298 tcNewMutVar new_global_tyvars `thenNF_Tc` \ gtvs' ->
299 tcSetEnv (TcEnv tve tce ce gve lve gtvs') scope
303 tcLookupLocalValue :: Name -> NF_TcM s (Maybe (TcIdBndr s))
304 tcLookupLocalValue name
305 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
306 returnNF_Tc (lookupUFM lve name)
308 tcLookupLocalValueByKey :: Unique -> NF_TcM s (Maybe (TcIdBndr s))
309 tcLookupLocalValueByKey uniq
310 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
311 returnNF_Tc (lookupUFM_Directly lve uniq)
313 tcLookupLocalValueOK :: String -> Name -> NF_TcM s (TcIdBndr s)
314 tcLookupLocalValueOK err name
315 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
316 returnNF_Tc (lookupWithDefaultUFM lve (panic err) name)
319 tcLookupGlobalValue :: Name -> NF_TcM s Id
320 tcLookupGlobalValue name
321 = case maybeWiredInIdName name of
322 Just id -> returnNF_Tc id
323 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
324 returnNF_Tc (lookupWithDefaultUFM gve def name)
326 def = pprPanic "tcLookupGlobalValue:" (ppr name)
328 tcLookupGlobalValueMaybe :: Name -> NF_TcM s (Maybe Id)
329 tcLookupGlobalValueMaybe name
330 = case maybeWiredInIdName name of
331 Just id -> returnNF_Tc (Just id)
332 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
333 returnNF_Tc (lookupUFM gve name)
336 tcLookupGlobalValueByKey :: Unique -> NF_TcM s Id
337 tcLookupGlobalValueByKey uniq
338 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
339 returnNF_Tc (lookupWithDefaultUFM_Directly gve def uniq)
342 def = pprPanic "tcLookupGlobalValueByKey:" (pprUnique10 uniq)
344 def = panic "tcLookupGlobalValueByKey"
347 tcLookupGlobalValueByKeyMaybe :: Unique -> NF_TcM s (Maybe Id)
348 tcLookupGlobalValueByKeyMaybe uniq
349 = tcGetEnv `thenNF_Tc` \ (TcEnv tve tce ce gve lve gtvs) ->
350 returnNF_Tc (lookupUFM_Directly gve uniq)
353 -- Non-monadic version, environment given explicitly
354 tcExplicitLookupGlobal :: TcEnv s -> Name -> Maybe Id
355 tcExplicitLookupGlobal (TcEnv tve tce ce gve lve gtvs) name
356 = case maybeWiredInIdName name of
358 Nothing -> lookupUFM gve name
360 -- Extract the IdInfo from an IfaceSig imported from an interface file
361 tcAddImportedIdInfo :: TcEnv s -> Id -> Id
362 tcAddImportedIdInfo unf_env id
363 | isLocallyDefined id -- Don't look up locally defined Ids, because they
364 -- have explicit local definitions, so we get a black hole!
367 = id `replaceIdInfo` new_info
368 -- The Id must be returned without a data dependency on maybe_id
370 new_info = -- pprTrace "tcAdd" (ppr id) $
371 case tcExplicitLookupGlobal unf_env (getName id) of
373 Just imported_id -> idInfo imported_id
374 -- ToDo: could check that types are the same
382 -- Uses the Name as the Name of the Id
383 newMonoIds :: [Name] -> Kind -> ([TcIdBndr s] -> TcM s a) -> TcM s a
385 newMonoIds names kind m
386 = newTyVarTys no_of_names kind `thenNF_Tc` \ tys ->
388 new_ids = zipWithEqual "newMonoIds" mk_id names tys
389 mk_id name ty = mkUserId name ty
391 tcExtendLocalValEnv names new_ids (m new_ids)
393 no_of_names = length names
395 newLocalId :: OccName -> TcType s -> NF_TcM s (TcIdBndr s)
397 = tcGetSrcLoc `thenNF_Tc` \ loc ->
398 tcGetUnique `thenNF_Tc` \ uniq ->
399 returnNF_Tc (mkUserLocal name uniq ty loc)
401 newLocalIds :: [OccName] -> [TcType s] -> NF_TcM s [TcIdBndr s]
402 newLocalIds names tys
403 = tcGetSrcLoc `thenNF_Tc` \ loc ->
404 tcGetUniques (length names) `thenNF_Tc` \ uniqs ->
406 new_ids = zipWith3Equal "newLocalIds" mk_id names uniqs tys
407 mk_id name uniq ty = mkUserLocal name uniq ty loc
411 newSpecPragmaId :: Name -> TcType s -> NF_TcM s (TcIdBndr s)
412 newSpecPragmaId name ty
413 = tcGetSrcLoc `thenNF_Tc` \ loc ->
414 tcGetUnique `thenNF_Tc` \ uniq ->
415 returnNF_Tc (mkSpecPragmaId (nameOccName name) uniq ty loc)
421 = ptext SLIT("Class used as a type constructor:") <+> ppr name
424 = ptext SLIT("Type constructor used as a class:") <+> ppr name