4 TyThing(..), TyThingDetails(..), TcTyThing(..),
6 -- Getting stuff from the environment
8 tcEnvTyCons, tcEnvClasses, tcEnvIds, tcEnvTcIds, tcEnvTyVars,
11 -- Instance environment, and InstInfo type
12 tcGetInstEnv, tcSetInstEnv,
13 InstInfo(..), pprInstInfo,
14 simpleInstInfoTy, simpleInstInfoTyCon,
17 tcExtendGlobalEnv, tcExtendGlobalValEnv,
18 tcLookupTyCon, tcLookupClass, tcLookupGlobalId, tcLookupDataCon,
19 tcLookupGlobal_maybe, tcLookupGlobal,
23 tcExtendTyVarEnv, tcExtendTyVarEnvForMeths,
24 tcExtendLocalValEnv, tcLookup,
26 -- Global type variables
27 tcGetGlobalTyVars, tcExtendGlobalTyVars,
29 -- Random useful things
30 RecTcEnv, tcAddImportedIdInfo, tcLookupRecId, tcInstId,
33 newLocalId, newSpecPragmaId,
34 newDefaultMethodName, newDFunName,
37 isLocalThing, tcSetEnv
40 #include "HsVersions.h"
42 import RnHsSyn ( RenamedMonoBinds, RenamedSig )
44 import TcType ( TcKind, TcType, TcTyVar, TcTyVarSet, TcThetaType,
45 tcInstTyVars, zonkTcTyVars,
47 import Id ( idName, mkUserLocal, isDataConWrapId_maybe )
48 import IdInfo ( constantIdInfo )
49 import MkId ( mkSpecPragmaId )
50 import Var ( TyVar, Id, idType, lazySetIdInfo, idInfo )
53 tyVarsOfTypes, splitDFunTy,
54 splitForAllTys, splitRhoTy,
55 getDFunTyKey, splitTyConApp_maybe
57 import DataCon ( DataCon )
58 import TyCon ( TyCon )
59 import Class ( Class, ClassOpItem, ClassContext )
60 import Subst ( substTy )
61 import Name ( Name, OccName, NamedThing(..),
62 nameOccName, nameModule, getSrcLoc, mkGlobalName,
63 isLocalName, nameModule_maybe
65 import Name ( NameEnv, lookupNameEnv, nameEnvElts, extendNameEnvList, emptyNameEnv )
66 import OccName ( mkDFunOcc, mkDefaultMethodOcc, occNameString )
67 import HscTypes ( DFunId, TypeEnv, HomeSymbolTable, PackageTypeEnv )
68 import Module ( Module )
69 import InstEnv ( InstEnv, emptyInstEnv )
70 import HscTypes ( lookupType, TyThing(..) )
71 import Util ( zipEqual )
72 import SrcLoc ( SrcLoc )
75 import IOExts ( newIORef )
78 %************************************************************************
82 %************************************************************************
85 type TcId = Id -- Type may be a TcType
90 tcGST :: Name -> Maybe TyThing, -- The type environment at the moment we began this compilation
92 tcInsts :: InstEnv, -- All instances (both imported and in this module)
94 tcGEnv :: TypeEnv, -- The global type environment we've accumulated while
95 {- NameEnv TyThing-} -- compiling this module:
96 -- types and classes (both imported and local)
98 -- (Ids defined in this module are in the local envt)
100 tcLEnv :: NameEnv TcTyThing, -- The local type environment: Ids and TyVars
101 -- defined in this module
103 tcTyVars :: TcRef TcTyVarSet -- The "global tyvars"
104 -- Namely, the in-scope TyVars bound in tcLEnv, plus the tyvars
105 -- mentioned in the types of Ids bound in tcLEnv
106 -- Why mutable? see notes with tcGetGlobalTyVars
111 The Global-Env/Local-Env story
112 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113 During type checking, we keep in the GlobalEnv
114 * All types and classes
115 * All Ids derived from types and classes (constructors, selectors)
118 At the end of type checking, we zonk the local bindings,
119 and as we do so we add to the GlobalEnv
120 * Locally defined top-level Ids
122 Why? Because they are now Ids not TcIds. This final GlobalEnv is
124 a) fed back (via the knot) to typechecking the
125 unfoldings of interface signatures
127 b) used to augment the GlobalSymbolTable
132 = AGlobal TyThing -- Used only in the return type of a lookup
133 | ATcId TcId -- Ids defined in this module
134 | ATyVar TyVar -- Type variables
135 | AThing TcKind -- Used temporarily, during kind checking
136 -- Here's an example of how the AThing guy is used
137 -- Suppose we are checking (forall a. T a Int):
138 -- 1. We first bind (a -> AThink kv), where kv is a kind variable.
139 -- 2. Then we kind-check the (T a Int) part.
140 -- 3. Then we zonk the kind variable.
141 -- 4. Now we know the kind for 'a', and we add (a -> ATyVar a::K) to the environment
143 initTcEnv :: HomeSymbolTable -> PackageTypeEnv -> IO TcEnv
145 = do { gtv_var <- newIORef emptyVarSet ;
146 return (TcEnv { tcGST = lookup,
147 tcGEnv = emptyNameEnv,
148 tcInsts = emptyInstEnv,
149 tcLEnv = emptyNameEnv,
153 lookup name | isLocalName name = Nothing
154 | otherwise = lookupType hst pte name
157 tcEnvClasses env = [cl | AClass cl <- nameEnvElts (tcGEnv env)]
158 tcEnvTyCons env = [tc | ATyCon tc <- nameEnvElts (tcGEnv env)]
159 tcEnvIds env = [id | AnId id <- nameEnvElts (tcGEnv env)]
160 tcEnvTyVars env = [tv | ATyVar tv <- nameEnvElts (tcLEnv env)]
161 tcEnvTcIds env = [id | ATcId id <- nameEnvElts (tcLEnv env)]
163 getTcGEnv (TcEnv { tcGEnv = genv }) = genv
165 -- This data type is used to help tie the knot
166 -- when type checking type and class declarations
167 data TyThingDetails = SynTyDetails Type
168 | DataTyDetails ClassContext [DataCon]
169 | ClassDetails ClassContext [Id] [ClassOpItem] DataCon
173 %************************************************************************
175 \subsection{Basic lookups}
177 %************************************************************************
180 lookup_global :: TcEnv -> Name -> Maybe TyThing
181 -- Try the global envt and then the global symbol table
182 lookup_global env name
183 = case lookupNameEnv (tcGEnv env) name of
184 Just thing -> Just thing
185 Nothing -> tcGST env name
187 lookup_local :: TcEnv -> Name -> Maybe TcTyThing
188 -- Try the local envt and then try the global
189 lookup_local env name
190 = case lookupNameEnv (tcLEnv env) name of
191 Just thing -> Just thing
192 Nothing -> case lookup_global env name of
193 Just thing -> Just (AGlobal thing)
198 type RecTcEnv = TcEnv
199 -- This environment is used for getting the 'right' IdInfo
200 -- on imported things and for looking up Ids in unfoldings
201 -- The environment doesn't have any local Ids in it
203 tcAddImportedIdInfo :: RecTcEnv -> Id -> Id
204 tcAddImportedIdInfo env id
205 = id `lazySetIdInfo` new_info
206 -- The Id must be returned without a data dependency on maybe_id
208 new_info = case tcLookupRecId env (idName id) of
209 Nothing -> constantIdInfo
210 Just imported_id -> idInfo imported_id
211 -- ToDo: could check that types are the same
213 tcLookupRecId :: RecTcEnv -> Name -> Maybe Id
214 tcLookupRecId env name = case lookup_global env name of
215 Just (AnId id) -> Just id
220 %************************************************************************
222 \subsection{Random useful functions}
224 %************************************************************************
228 -- A useful function that takes an occurrence of a global thing
229 -- and instantiates its type with fresh type variables
231 -> NF_TcM ([TcTyVar], -- It's instantiated type
236 (tyvars, rho) = splitForAllTys (idType id)
238 tcInstTyVars tyvars `thenNF_Tc` \ (tyvars', arg_tys, tenv) ->
240 rho' = substTy tenv rho
241 (theta', tau') = splitRhoTy rho'
243 returnNF_Tc (tyvars', theta', tau')
247 %************************************************************************
249 \subsection{Making new Ids}
251 %************************************************************************
256 newLocalId :: OccName -> TcType -> SrcLoc -> NF_TcM TcId
257 newLocalId name ty loc
258 = tcGetUnique `thenNF_Tc` \ uniq ->
259 returnNF_Tc (mkUserLocal name uniq ty loc)
261 newSpecPragmaId :: Name -> TcType -> NF_TcM TcId
262 newSpecPragmaId name ty
263 = tcGetUnique `thenNF_Tc` \ uniq ->
264 returnNF_Tc (mkSpecPragmaId (nameOccName name) uniq ty (getSrcLoc name))
267 Make a name for the dict fun for an instance decl
270 newDFunName :: Module -> Class -> [Type] -> SrcLoc -> NF_TcM Name
271 newDFunName mod clas (ty:_) loc
272 = tcGetDFunUniq dfun_string `thenNF_Tc` \ inst_uniq ->
273 tcGetUnique `thenNF_Tc` \ uniq ->
274 returnNF_Tc (mkGlobalName uniq mod
275 (mkDFunOcc dfun_string inst_uniq)
278 -- Any string that is somewhat unique will do
279 dfun_string = occNameString (getOccName clas) ++ occNameString (getDFunTyKey ty)
281 newDFunName mod clas [] loc = pprPanic "newDFunName" (ppr mod <+> ppr clas <+> ppr loc)
283 newDefaultMethodName :: Name -> SrcLoc -> NF_TcM Name
284 newDefaultMethodName op_name loc
285 = tcGetUnique `thenNF_Tc` \ uniq ->
286 returnNF_Tc (mkGlobalName uniq (nameModule op_name)
287 (mkDefaultMethodOcc (getOccName op_name))
292 isLocalThing :: NamedThing a => Module -> a -> Bool
293 -- True if the thing has a Local name,
294 -- or a Global name from the specified module
295 isLocalThing mod thing = case nameModule_maybe (getName thing) of
296 Nothing -> True -- A local name
297 Just m -> m == mod -- A global thing
300 %************************************************************************
302 \subsection{The global environment}
304 %************************************************************************
307 tcExtendGlobalEnv :: [(Name, TyThing)] -> TcM r -> TcM r
308 tcExtendGlobalEnv bindings thing_inside
309 = tcGetEnv `thenNF_Tc` \ env ->
311 ge' = extendNameEnvList (tcGEnv env) bindings
313 tcSetEnv (env {tcGEnv = ge'}) thing_inside
315 tcExtendGlobalValEnv :: [Id] -> TcM a -> TcM a
316 tcExtendGlobalValEnv ids thing_inside
317 = tcExtendGlobalEnv [(getName id, AnId id) | id <- ids] thing_inside
322 tcLookupGlobal_maybe :: Name -> NF_TcM (Maybe TyThing)
323 tcLookupGlobal_maybe name
324 = tcGetEnv `thenNF_Tc` \ env ->
325 returnNF_Tc (lookup_global env name)
328 A variety of global lookups, when we know what we are looking for.
331 tcLookupGlobal :: Name -> NF_TcM TyThing
333 = tcLookupGlobal_maybe name `thenNF_Tc` \ maybe_thing ->
335 Just thing -> returnNF_Tc thing
336 other -> notFound "tcLookupGlobal" name
338 tcLookupGlobalId :: Name -> NF_TcM Id
339 tcLookupGlobalId name
340 = tcLookupGlobal_maybe name `thenNF_Tc` \ maybe_id ->
342 Just (AnId clas) -> returnNF_Tc clas
343 other -> notFound "tcLookupGlobalId" name
345 tcLookupDataCon :: Name -> TcM DataCon
346 tcLookupDataCon con_name
347 = tcLookupGlobalId con_name `thenNF_Tc` \ con_id ->
348 case isDataConWrapId_maybe con_id of
349 Just data_con -> returnTc data_con
350 Nothing -> failWithTc (badCon con_id)
353 tcLookupClass :: Name -> NF_TcM Class
355 = tcLookupGlobal_maybe name `thenNF_Tc` \ maybe_clas ->
357 Just (AClass clas) -> returnNF_Tc clas
358 other -> notFound "tcLookupClass" name
360 tcLookupTyCon :: Name -> NF_TcM TyCon
362 = tcLookupGlobal_maybe name `thenNF_Tc` \ maybe_tc ->
364 Just (ATyCon tc) -> returnNF_Tc tc
365 other -> notFound "tcLookupTyCon" name
369 %************************************************************************
371 \subsection{The local environment}
373 %************************************************************************
376 tcLookup_maybe :: Name -> NF_TcM (Maybe TcTyThing)
378 = tcGetEnv `thenNF_Tc` \ env ->
379 returnNF_Tc (lookup_local env name)
381 tcLookup :: Name -> NF_TcM TcTyThing
383 = tcLookup_maybe name `thenNF_Tc` \ maybe_thing ->
385 Just thing -> returnNF_Tc thing
386 other -> notFound "tcLookup" name
387 -- Extract the IdInfo from an IfaceSig imported from an interface file
392 tcExtendKindEnv :: [(Name,TcKind)] -> TcM r -> TcM r
393 tcExtendKindEnv pairs thing_inside
394 = tcGetEnv `thenNF_Tc` \ env ->
396 le' = extendNameEnvList (tcLEnv env) [(n, AThing k) | (n,k) <- pairs]
397 -- No need to extend global tyvars for kind checking
399 tcSetEnv (env {tcLEnv = le'}) thing_inside
401 tcExtendTyVarEnv :: [TyVar] -> TcM r -> TcM r
402 tcExtendTyVarEnv tyvars thing_inside
403 = tcGetEnv `thenNF_Tc` \ env@(TcEnv {tcLEnv = le, tcTyVars = gtvs}) ->
405 le' = extendNameEnvList le [ (getName tv, ATyVar tv) | tv <- tyvars]
406 new_tv_set = mkVarSet tyvars
408 -- It's important to add the in-scope tyvars to the global tyvar set
410 -- f (x::r) = let g y = y::r in ...
411 -- Here, g mustn't be generalised. This is also important during
412 -- class and instance decls, when we mustn't generalise the class tyvars
413 -- when typechecking the methods.
414 tc_extend_gtvs gtvs new_tv_set `thenNF_Tc` \ gtvs' ->
415 tcSetEnv (env {tcLEnv = le', tcTyVars = gtvs'}) thing_inside
417 -- This variant, tcExtendTyVarEnvForMeths, takes *two* bunches of tyvars:
418 -- the signature tyvars contain the original names
419 -- the instance tyvars are what those names should be mapped to
420 -- It's needed when typechecking the method bindings of class and instance decls
421 -- It does *not* extend the global tyvars; tcMethodBind does that for itself
423 tcExtendTyVarEnvForMeths :: [TyVar] -> [TcTyVar] -> TcM r -> TcM r
424 tcExtendTyVarEnvForMeths sig_tyvars inst_tyvars thing_inside
425 = tcGetEnv `thenNF_Tc` \ env ->
427 le' = extendNameEnvList (tcLEnv env) stuff
428 stuff = [ (getName sig_tv, ATyVar inst_tv)
429 | (sig_tv, inst_tv) <- zipEqual "tcMeth" sig_tyvars inst_tyvars
432 tcSetEnv (env {tcLEnv = le'}) thing_inside
437 tcExtendLocalValEnv :: [(Name,TcId)] -> TcM a -> TcM a
438 tcExtendLocalValEnv names_w_ids thing_inside
439 = tcGetEnv `thenNF_Tc` \ env ->
441 extra_global_tyvars = tyVarsOfTypes [idType id | (name,id) <- names_w_ids]
442 extra_env = [(name, ATcId id) | (name,id) <- names_w_ids]
443 le' = extendNameEnvList (tcLEnv env) extra_env
445 tc_extend_gtvs (tcTyVars env) extra_global_tyvars `thenNF_Tc` \ gtvs' ->
446 tcSetEnv (env {tcLEnv = le', tcTyVars = gtvs'}) thing_inside
450 %************************************************************************
452 \subsection{The global tyvars}
454 %************************************************************************
457 tcExtendGlobalTyVars extra_global_tvs thing_inside
458 = tcGetEnv `thenNF_Tc` \ env ->
459 tc_extend_gtvs (tcTyVars env) extra_global_tvs `thenNF_Tc` \ gtvs' ->
460 tcSetEnv (env {tcTyVars = gtvs'}) thing_inside
462 tc_extend_gtvs gtvs extra_global_tvs
463 = tcReadMutVar gtvs `thenNF_Tc` \ global_tvs ->
464 tcNewMutVar (global_tvs `unionVarSet` extra_global_tvs)
467 @tcGetGlobalTyVars@ returns a fully-zonked set of tyvars free in the environment.
468 To improve subsequent calls to the same function it writes the zonked set back into
472 tcGetGlobalTyVars :: NF_TcM TcTyVarSet
474 = tcGetEnv `thenNF_Tc` \ (TcEnv {tcTyVars = gtv_var}) ->
475 tcReadMutVar gtv_var `thenNF_Tc` \ global_tvs ->
476 zonkTcTyVars (varSetElems global_tvs) `thenNF_Tc` \ global_tys' ->
478 global_tvs' = (tyVarsOfTypes global_tys')
480 tcWriteMutVar gtv_var global_tvs' `thenNF_Tc_`
481 returnNF_Tc global_tvs'
485 %************************************************************************
487 \subsection{The instance environment}
489 %************************************************************************
492 tcGetInstEnv :: NF_TcM InstEnv
493 tcGetInstEnv = tcGetEnv `thenNF_Tc` \ env ->
494 returnNF_Tc (tcInsts env)
496 tcSetInstEnv :: InstEnv -> TcM a -> TcM a
497 tcSetInstEnv ie thing_inside
498 = tcGetEnv `thenNF_Tc` \ env ->
499 tcSetEnv (env {tcInsts = ie}) thing_inside
503 %************************************************************************
505 \subsection{The InstInfo type}
507 %************************************************************************
509 The InstInfo type summarises the information in an instance declaration
511 instance c => k (t tvs) where b
516 iLocal :: Bool, -- True <=> it's defined in this module
517 iDFunId :: DFunId, -- The dfun id
518 iBinds :: RenamedMonoBinds, -- Bindings, b
519 iPrags :: [RenamedSig] -- User pragmas recorded for generating specialised instances
522 pprInstInfo info = vcat [ptext SLIT("InstInfo:") <+> ppr (idType (iDFunId info)),
523 nest 4 (ppr (iBinds info))]
525 simpleInstInfoTy :: InstInfo -> Type
526 simpleInstInfoTy info = case splitDFunTy (idType (iDFunId info)) of
527 (_, _, _, [ty]) -> ty
529 simpleInstInfoTyCon :: InstInfo -> TyCon
530 -- Gets the type constructor for a simple instance declaration,
531 -- i.e. one of the form instance (...) => C (T a b c) where ...
532 simpleInstInfoTyCon inst
533 = case splitTyConApp_maybe (simpleInstInfoTy inst) of
534 Just (tycon, _) -> tycon
538 %************************************************************************
542 %************************************************************************
545 badCon con_id = quotes (ppr con_id) <+> ptext SLIT("is not a data constructor")
547 notFound wheRe name = failWithTc (text wheRe <> colon <+> quotes (ppr name) <+>
548 ptext SLIT("is not in scope"))