3 TcId, TcIdSet, tcInstId,
6 TcEnv, ValueEnv, TcTyThing(..),
8 initEnv, getEnvTyCons, getEnvClasses, getAllEnvTyCons,
10 tcExtendTyVarEnv, tcExtendTyVarEnvForMeths, tcExtendTypeEnv, tcGetInScopeTyVars,
13 tcLookupTyCon, tcLookupTyConByKey,
14 tcLookupClass, tcLookupClassByKey,
16 tcExtendGlobalValEnv, tcExtendLocalValEnv,
17 tcGetValueEnv, tcSetValueEnv,
20 tcLookupValue, tcLookupValueMaybe,
21 tcLookupValueByKey, tcLookupValueByKeyMaybe,
22 explicitLookupValueByKey, explicitLookupValue,
24 newLocalId, newSpecPragmaId,
25 tcGetGlobalTyVars, tcExtendGlobalTyVars,
30 #include "HsVersions.h"
32 import HsTypes ( HsTyVar, getTyVarName )
33 import Id ( mkUserLocal, isDataConId_maybe )
34 import MkId ( mkSpecPragmaId )
35 import Var ( TyVar, Id, setVarName,
36 idType, setIdInfo, idInfo, tyVarKind
38 import TcType ( TcType, TcTyVar, TcTyVarSet, TcThetaType,
39 tcInstTyVars, zonkTcTyVars,
44 import Type ( Kind, superKind,
45 tyVarsOfType, tyVarsOfTypes, mkTyVarTy, substTy,
46 splitForAllTys, splitRhoTy, splitFunTys, substTopTy,
47 splitAlgTyConApp_maybe, getTyVar
49 import UsageSPUtils ( unannotTy )
50 import DataCon ( DataCon )
51 import TyCon ( TyCon, tyConKind, tyConArity, isSynTyCon )
52 import Class ( Class, classTyCon )
56 import BasicTypes ( Arity )
57 import IdInfo ( noIdInfo )
58 import Name ( Name, OccName, nameOccName, getSrcLoc,
59 maybeWiredInTyConName, maybeWiredInIdName, isLocallyDefined,
62 import Unique ( pprUnique10, Unique, Uniquable(..) )
63 import FiniteMap ( lookupFM, addToFM )
65 import Unique ( Uniquable(..) )
66 import Util ( zipEqual, zipWith3Equal, mapAccumL )
67 import Bag ( bagToList )
68 import Maybes ( maybeToBool, catMaybes )
69 import SrcLoc ( SrcLoc )
70 import FastString ( FastString )
74 %************************************************************************
78 %************************************************************************
82 type TcId = Id -- Type may be a TcType
85 tcLookupDataCon :: Name -> TcM s (DataCon, [TcType], TcType)
86 tcLookupDataCon con_name
87 = tcLookupValue con_name `thenNF_Tc` \ con_id ->
88 case isDataConId_maybe con_id of {
89 Nothing -> failWithTc (badCon con_id);
92 tcInstId con_id `thenNF_Tc` \ (_, _, con_tau) ->
93 -- Ignore the con_theta; overloaded constructors only
94 -- behave differently when called, not when used for
97 (arg_tys, result_ty) = splitFunTys con_tau
99 ASSERT( maybeToBool (splitAlgTyConApp_maybe result_ty) )
100 returnTc (data_con, arg_tys, result_ty) }
102 -- A useful function that takes an occurrence of a global thing
103 -- and instantiates its type with fresh type variables
105 -> NF_TcM s ([TcTyVar], -- It's instantiated type
110 (tyvars, rho) = splitForAllTys (unannotTy (idType id))
112 tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', arg_tys, tenv) ->
114 rho' = substTopTy tenv rho
115 (theta', tau') = splitRhoTy rho'
117 returnNF_Tc (tyvars', theta', tau')
120 Between the renamer and the first invocation of the UsageSP inference,
121 identifiers read from interface files will have usage information in
122 their types, whereas other identifiers will not. The unannotTy here
123 in @tcInstId@ prevents this information from pointlessly propagating
124 further prior to the first usage inference.
127 %************************************************************************
131 %************************************************************************
133 Data type declarations
134 ~~~~~~~~~~~~~~~~~~~~~
140 (TcTyVarSet, -- The in-scope TyVars
141 TcRef TcTyVarSet) -- Free type variables of the value env
142 -- ...why mutable? see notes with tcGetGlobalTyVars
143 -- Includes the in-scope tyvars
145 type NameEnv val = UniqFM val -- Keyed by Names
147 type TypeEnv = NameEnv (TcKind, Maybe Arity, TcTyThing)
148 type ValueEnv = NameEnv Id
150 data TcTyThing = ATyVar TcTyVar -- Mutable only so that the kind can be mutable
151 -- if the kind is mutable, the tyvar must be so that
157 initEnv :: TcRef TcTyVarSet -> TcEnv
158 initEnv mut = TcEnv emptyUFM emptyUFM (emptyVarSet, mut)
160 getEnvTyCons (TcEnv te _ _) = [tc | (_, _, ATyCon tc) <- eltsUFM te]
161 getEnvClasses (TcEnv te _ _) = [cl | (_, _, AClass cl) <- eltsUFM te]
162 getAllEnvTyCons (TcEnv te _ _) = catMaybes (map gettc (eltsUFM te))
164 gettc (_,_, ATyCon tc) = Just tc
165 gettc (_,_, AClass cl) = Just (classTyCon cl)
172 Extending the type environment.
175 tcExtendTyVarEnv :: [TyVar] -> TcM s r -> TcM s r
176 tcExtendTyVarEnv tyvars scope
177 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve (in_scope_tvs, gtvs)) ->
179 extend_list = [ (getName tv, (kindToTcKind (tyVarKind tv), Nothing, ATyVar tv))
182 te' = addListToUFM te extend_list
183 new_tv_set = mkVarSet tyvars
184 in_scope_tvs' = in_scope_tvs `unionVarSet` new_tv_set
186 -- It's important to add the in-scope tyvars to the global tyvar set
188 -- f (x::r) = let g y = y::r in ...
189 -- Here, g mustn't be generalised. This is also important during
190 -- class and instance decls, when we mustn't generalise the class tyvars
191 -- when typechecking the methods.
192 tc_extend_gtvs gtvs new_tv_set `thenNF_Tc` \ gtvs' ->
193 tcSetEnv (TcEnv te' ve (in_scope_tvs', gtvs')) scope
195 -- This variant, tcExtendTyVarEnvForMeths, takes *two* bunches of tyvars:
196 -- the signature tyvars contain the original names
197 -- the instance tyvars are what those names should be mapped to
198 -- It's needed when typechecking the method bindings of class and instance decls
199 -- It does *not* extend the global tyvars; tcMethodBind does that for itself
201 tcExtendTyVarEnvForMeths :: [TyVar] -> [TcTyVar] -> TcM s r -> TcM s r
202 tcExtendTyVarEnvForMeths sig_tyvars inst_tyvars thing_inside
203 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
205 te' = addListToUFM te stuff
207 tcSetEnv (TcEnv te' ve gtvs) thing_inside
209 stuff = [ (getName sig_tv, (kindToTcKind (tyVarKind inst_tv), Nothing, ATyVar inst_tv))
210 | (sig_tv, inst_tv) <- zipEqual "tcMeth" sig_tyvars inst_tyvars
213 tcExtendGlobalTyVars extra_global_tvs scope
214 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve (in_scope,gtvs)) ->
215 tc_extend_gtvs gtvs extra_global_tvs `thenNF_Tc` \ gtvs' ->
216 tcSetEnv (TcEnv te ve (in_scope,gtvs')) scope
218 tc_extend_gtvs gtvs extra_global_tvs
219 = tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
221 new_global_tyvars = global_tvs `unionVarSet` extra_global_tvs
223 tcNewMutVar new_global_tyvars
226 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
227 To improve subsequent calls to the same function it writes the zonked set back into
231 tcGetGlobalTyVars :: NF_TcM s TcTyVarSet
233 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve (_,gtvs)) ->
234 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
235 zonkTcTyVars (varSetElems global_tvs) `thenNF_Tc` \ global_tys' ->
237 global_tvs' = (tyVarsOfTypes global_tys')
239 tcWriteMutVar gtvs global_tvs' `thenNF_Tc_`
240 returnNF_Tc global_tvs'
242 tcGetInScopeTyVars :: NF_TcM s [TcTyVar]
244 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve (in_scope_tvs, gtvs)) ->
245 returnNF_Tc (varSetElems in_scope_tvs)
249 Type constructors and classes
252 tcExtendTypeEnv :: [(Name, (TcKind, Maybe Arity, TcTyThing))] -> TcM s r -> TcM s r
253 tcExtendTypeEnv bindings scope
254 = ASSERT( null [tv | (_, (_,_,ATyVar tv)) <- bindings] )
255 -- Not for tyvars; use tcExtendTyVarEnv
256 tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
258 te' = addListToUFM te bindings
260 tcSetEnv (TcEnv te' ve gtvs) scope
264 Looking up in the environments.
267 tcLookupTy :: Name -> NF_TcM s (TcKind, Maybe Arity, TcTyThing)
269 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
270 case lookupUFM te name of {
271 Just thing -> returnNF_Tc thing ;
274 case maybeWiredInTyConName name of
275 Just tc -> returnNF_Tc (kindToTcKind (tyConKind tc), maybe_arity, ATyCon tc)
277 maybe_arity | isSynTyCon tc = Just (tyConArity tc)
278 | otherwise = Nothing
280 Nothing -> -- This can happen if an interface-file
281 -- unfolding is screwed up
282 failWithTc (tyNameOutOfScope name)
285 tcLookupClass :: Name -> NF_TcM s Class
287 = tcLookupTy name `thenNF_Tc` \ (_, _, AClass clas) ->
290 tcLookupTyCon :: Name -> NF_TcM s TyCon
292 = tcLookupTy name `thenNF_Tc` \ (_, _, ATyCon tycon) ->
295 tcLookupClassByKey :: Unique -> NF_TcM s Class
296 tcLookupClassByKey key
297 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
298 case lookupUFM_Directly te key of
299 Just (_, _, AClass cl) -> returnNF_Tc cl
300 other -> pprPanic "tcLookupClassByKey:" (pprUnique10 key)
302 tcLookupTyConByKey :: Unique -> NF_TcM s TyCon
303 tcLookupTyConByKey key
304 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
305 case lookupUFM_Directly te key of
306 Just (_, _, ATyCon tc) -> returnNF_Tc tc
307 other -> pprPanic "tcLookupTyConByKey:" (pprUnique10 key)
313 %************************************************************************
315 \subsection{The value environment}
317 %************************************************************************
320 tcExtendGlobalValEnv :: [Id] -> TcM s a -> TcM s a
321 tcExtendGlobalValEnv ids scope
322 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
324 ve' = addListToUFM_Directly ve [(getUnique id, id) | id <- ids]
326 tcSetEnv (TcEnv te ve' gtvs) scope
328 tcExtendLocalValEnv :: [(Name,TcId)] -> TcM s a -> TcM s a
329 tcExtendLocalValEnv names_w_ids scope
330 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve (in_scope_tvs,gtvs)) ->
331 tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
333 ve' = addListToUFM ve names_w_ids
334 extra_global_tyvars = tyVarsOfTypes (map (idType . snd) names_w_ids)
336 tc_extend_gtvs gtvs extra_global_tyvars `thenNF_Tc` \ gtvs' ->
337 tcSetEnv (TcEnv te ve' (in_scope_tvs,gtvs')) scope
342 tcLookupValue :: Name -> NF_TcM s Id -- Panics if not found
344 = case maybeWiredInIdName name of
345 Just id -> returnNF_Tc id
346 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
347 returnNF_Tc (lookupWithDefaultUFM ve def name)
349 def = pprPanic "tcLookupValue:" (ppr name)
351 tcLookupValueMaybe :: Name -> NF_TcM s (Maybe Id)
352 tcLookupValueMaybe name
353 = case maybeWiredInIdName name of
354 Just id -> returnNF_Tc (Just id)
355 Nothing -> tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
356 returnNF_Tc (lookupUFM ve name)
358 tcLookupValueByKey :: Unique -> NF_TcM s Id -- Panics if not found
359 tcLookupValueByKey key
360 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
361 returnNF_Tc (explicitLookupValueByKey ve key)
363 tcLookupValueByKeyMaybe :: Unique -> NF_TcM s (Maybe Id)
364 tcLookupValueByKeyMaybe key
365 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
366 returnNF_Tc (lookupUFM_Directly ve key)
368 tcGetValueEnv :: NF_TcM s ValueEnv
370 = tcGetEnv `thenNF_Tc` \ (TcEnv te ve gtvs) ->
373 tcSetValueEnv :: ValueEnv -> TcM s a -> TcM s a
374 tcSetValueEnv ve scope
375 = tcGetEnv `thenNF_Tc` \ (TcEnv te _ gtvs) ->
376 tcSetEnv (TcEnv te ve gtvs) scope
378 -- Non-monadic version, environment given explicitly
379 explicitLookupValueByKey :: ValueEnv -> Unique -> Id
380 explicitLookupValueByKey ve key
381 = lookupWithDefaultUFM_Directly ve def key
383 def = pprPanic "lookupValueByKey:" (pprUnique10 key)
385 explicitLookupValue :: ValueEnv -> Name -> Maybe Id
386 explicitLookupValue ve name
387 = case maybeWiredInIdName name of
389 Nothing -> lookupUFM ve name
391 -- Extract the IdInfo from an IfaceSig imported from an interface file
392 tcAddImportedIdInfo :: ValueEnv -> Id -> Id
393 tcAddImportedIdInfo unf_env id
394 | isLocallyDefined id -- Don't look up locally defined Ids, because they
395 -- have explicit local definitions, so we get a black hole!
398 = id `setIdInfo` new_info
399 -- The Id must be returned without a data dependency on maybe_id
401 new_info = -- pprTrace "tcAdd" (ppr id) $
402 case explicitLookupValue unf_env (getName id) of
404 Just imported_id -> idInfo imported_id
405 -- ToDo: could check that types are the same
409 %************************************************************************
411 \subsection{Constructing new Ids}
413 %************************************************************************
416 newLocalId :: OccName -> TcType -> SrcLoc -> NF_TcM s TcId
417 newLocalId name ty loc
418 = tcGetUnique `thenNF_Tc` \ uniq ->
419 returnNF_Tc (mkUserLocal name uniq ty loc)
421 newSpecPragmaId :: Name -> TcType -> NF_TcM s TcId
422 newSpecPragmaId name ty
423 = tcGetUnique `thenNF_Tc` \ uniq ->
424 returnNF_Tc (mkSpecPragmaId (nameOccName name) uniq ty (getSrcLoc name))
428 %************************************************************************
432 %************************************************************************
436 = quotes (ppr con_id) <+> ptext SLIT("is not a data constructor")
438 = quotes (ppr op) <+> ptext SLIT("is not a primop")
440 tyNameOutOfScope name
441 = quotes (ppr name) <+> ptext SLIT("is not in scope")