X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2Ftypecheck%2FTcEnv.lhs;h=f2e0d1da32ed6ae0c00423099e514ebd0d63092d;hb=5e3f005d3012472e422d4ffd7dca5c21a80fca80;hp=ac92dc3c245025c0ea61d8988af4d921aaf8780a;hpb=be2c67eb19565e8d0d9ca5a53f526ce49acf1d92;p=ghc-hetmet.git diff --git a/ghc/compiler/typecheck/TcEnv.lhs b/ghc/compiler/typecheck/TcEnv.lhs index ac92dc3..f2e0d1d 100644 --- a/ghc/compiler/typecheck/TcEnv.lhs +++ b/ghc/compiler/typecheck/TcEnv.lhs @@ -5,7 +5,7 @@ module TcEnv( -- Getting stuff from the environment TcEnv, initTcEnv, - tcEnvTyCons, tcEnvClasses, tcEnvIds, tcEnvTcIds, tcEnvTyVars, + tcEnvTyCons, tcEnvClasses, tcEnvIds, tcLEnvElts, getTcGEnv, -- Instance environment, and InstInfo type @@ -14,14 +14,14 @@ module TcEnv( simpleInstInfoTy, simpleInstInfoTyCon, -- Global environment - tcExtendGlobalEnv, tcExtendGlobalValEnv, + tcExtendGlobalEnv, tcExtendGlobalValEnv, tcExtendGlobalTypeEnv, tcLookupTyCon, tcLookupClass, tcLookupGlobalId, tcLookupDataCon, - tcLookupGlobal_maybe, tcLookupGlobal, tcLookupSyntaxId, tcLookupSyntaxName, + tcLookupGlobal_maybe, tcLookupGlobal, -- Local environment - tcExtendKindEnv, tcLookupLocalIds, + tcExtendKindEnv, tcLookupLocalIds, tcInLocalScope, tcExtendTyVarEnv, tcExtendTyVarEnvForMeths, - tcExtendLocalValEnv, tcLookup, tcLookup_maybe, + tcExtendLocalValEnv, tcLookup, tcLookup_maybe, tcLookupId, -- Global type variables tcGetGlobalTyVars, tcExtendGlobalTyVars, @@ -30,8 +30,7 @@ module TcEnv( RecTcEnv, tcAddImportedIdInfo, tcLookupRecId, tcLookupRecId_maybe, -- New Ids - newLocalId, newSpecPragmaId, - newDFunName, + newLocalName, newDFunName, -- Misc isLocalThing, tcSetEnv @@ -41,34 +40,35 @@ module TcEnv( import RnHsSyn ( RenamedMonoBinds, RenamedSig ) import TcMonad -import TcType ( TcKind, TcType, TcTyVar, TcTyVarSet, - zonkTcTyVarsAndFV +import TcMType ( zonkTcTyVarsAndFV ) +import TcType ( Type, ThetaType, TcKind, TcTyVar, TcTyVarSet, + tyVarsOfTypes, tcSplitDFunTy, + getDFunTyKey, tcTyConAppTyCon ) -import Id ( idName, mkUserLocal, isDataConWrapId_maybe ) -import IdInfo ( constantIdInfo ) -import MkId ( mkSpecPragmaId ) +import Id ( idName, isDataConWrapId_maybe ) +import IdInfo ( vanillaIdInfo ) import Var ( TyVar, Id, idType, lazySetIdInfo, idInfo ) import VarSet -import Type ( Type, - tyVarsOfTypes, splitDFunTy, - getDFunTyKey, tyConAppTyCon - ) import DataCon ( DataCon ) import TyCon ( TyCon ) -import Class ( Class, ClassOpItem, ClassContext ) -import Name ( Name, OccName, NamedThing(..), - nameOccName, getSrcLoc, mkLocalName, - isLocalName, nameModule_maybe +import Class ( Class, ClassOpItem ) +import Name ( Name, NamedThing(..), + getSrcLoc, mkLocalName, isLocalName, nameIsLocalOrFrom ) -import Name ( NameEnv, lookupNameEnv, nameEnvElts, extendNameEnvList, emptyNameEnv ) +import NameEnv ( NameEnv, lookupNameEnv, nameEnvElts, elemNameEnv, + extendNameEnvList, emptyNameEnv, plusNameEnv ) import OccName ( mkDFunOcc, occNameString ) -import HscTypes ( DFunId, TypeEnv, HomeSymbolTable, PackageTypeEnv ) +import HscTypes ( DFunId, + PackageTypeEnv, TypeEnv, + extendTypeEnvList, extendTypeEnvWithIds, + typeEnvTyCons, typeEnvClasses, typeEnvIds, + HomeSymbolTable + ) import Module ( Module ) import InstEnv ( InstEnv, emptyInstEnv ) import HscTypes ( lookupType, TyThing(..) ) import Util ( zipEqual ) import SrcLoc ( SrcLoc ) -import qualified PrelNames import Outputable import IOExts ( newIORef ) @@ -86,8 +86,6 @@ type TcIdSet = IdSet data TcEnv = TcEnv { - tcSyntaxMap :: PrelNames.SyntaxMap, -- The syntax map (usually the identity) - tcGST :: Name -> Maybe TyThing, -- The type environment at the moment we began this compilation tcInsts :: InstEnv, -- All instances (both imported and in this module) @@ -96,7 +94,8 @@ data TcEnv {- NameEnv TyThing-} -- compiling this module: -- types and classes (both imported and local) -- imported Ids - -- (Ids defined in this module are in the local envt) + -- (Ids defined in this module start in the local envt, + -- though they move to the global envt during zonking) tcLEnv :: NameEnv TcTyThing, -- The local type environment: Ids and TyVars -- defined in this module @@ -129,23 +128,10 @@ used thus: \begin{code} -data TcTyThing - = AGlobal TyThing -- Used only in the return type of a lookup - | ATcId TcId -- Ids defined in this module - | ATyVar TyVar -- Type variables - | AThing TcKind -- Used temporarily, during kind checking --- Here's an example of how the AThing guy is used --- Suppose we are checking (forall a. T a Int): --- 1. We first bind (a -> AThink kv), where kv is a kind variable. --- 2. Then we kind-check the (T a Int) part. --- 3. Then we zonk the kind variable. --- 4. Now we know the kind for 'a', and we add (a -> ATyVar a::K) to the environment - -initTcEnv :: PrelNames.SyntaxMap -> HomeSymbolTable -> PackageTypeEnv -> IO TcEnv -initTcEnv syntax_map hst pte +initTcEnv :: HomeSymbolTable -> PackageTypeEnv -> IO TcEnv +initTcEnv hst pte = do { gtv_var <- newIORef emptyVarSet ; - return (TcEnv { tcSyntaxMap = syntax_map, - tcGST = lookup, + return (TcEnv { tcGST = lookup, tcGEnv = emptyNameEnv, tcInsts = emptyInstEnv, tcLEnv = emptyNameEnv, @@ -156,21 +142,41 @@ initTcEnv syntax_map hst pte | otherwise = lookupType hst pte name -tcEnvClasses env = [cl | AClass cl <- nameEnvElts (tcGEnv env)] -tcEnvTyCons env = [tc | ATyCon tc <- nameEnvElts (tcGEnv env)] -tcEnvIds env = [id | AnId id <- nameEnvElts (tcGEnv env)] -tcEnvTyVars env = [tv | ATyVar tv <- nameEnvElts (tcLEnv env)] -tcEnvTcIds env = [id | ATcId id <- nameEnvElts (tcLEnv env)] +tcEnvClasses env = typeEnvClasses (tcGEnv env) +tcEnvTyCons env = typeEnvTyCons (tcGEnv env) +tcEnvIds env = typeEnvIds (tcGEnv env) +tcLEnvElts env = nameEnvElts (tcLEnv env) getTcGEnv (TcEnv { tcGEnv = genv }) = genv --- This data type is used to help tie the knot --- when type checking type and class declarations -data TyThingDetails = SynTyDetails Type - | DataTyDetails ClassContext [DataCon] [Id] - | ClassDetails ClassContext [Id] [ClassOpItem] DataCon +tcInLocalScope :: TcEnv -> Name -> Bool +tcInLocalScope env v = v `elemNameEnv` (tcLEnv env) +\end{code} + +\begin{code} +data TcTyThing + = AGlobal TyThing -- Used only in the return type of a lookup + | ATcId TcId -- Ids defined in this module + | ATyVar TyVar -- Type variables + | AThing TcKind -- Used temporarily, during kind checking +-- Here's an example of how the AThing guy is used +-- Suppose we are checking (forall a. T a Int): +-- 1. We first bind (a -> AThink kv), where kv is a kind variable. +-- 2. Then we kind-check the (T a Int) part. +-- 3. Then we zonk the kind variable. +-- 4. Now we know the kind for 'a', and we add (a -> ATyVar a::K) to the environment + \end{code} +This data type is used to help tie the knot + when type checking type and class declarations + +\begin{code} +data TyThingDetails = SynTyDetails Type + | DataTyDetails ThetaType [DataCon] [Id] + | ClassDetails ThetaType [Id] [ClassOpItem] DataCon + | ForeignTyDetails -- Nothing yet +\end{code} %************************************************************************ %* * @@ -208,7 +214,7 @@ tcAddImportedIdInfo env id -- The Id must be returned without a data dependency on maybe_id where new_info = case tcLookupRecId_maybe env (idName id) of - Nothing -> pprTrace "tcAddIdInfo" (ppr id) constantIdInfo + Nothing -> pprTrace "tcAddIdInfo" (ppr id) vanillaIdInfo Just imported_id -> idInfo imported_id -- ToDo: could check that types are the same @@ -232,15 +238,10 @@ tcLookupRecId env name = case lookup_global env name of Constructing new Ids \begin{code} -newLocalId :: OccName -> TcType -> SrcLoc -> NF_TcM TcId -newLocalId name ty loc +newLocalName :: Name -> NF_TcM Name +newLocalName name -- Make a clone = tcGetUnique `thenNF_Tc` \ uniq -> - returnNF_Tc (mkUserLocal name uniq ty loc) - -newSpecPragmaId :: Name -> TcType -> NF_TcM TcId -newSpecPragmaId name ty - = tcGetUnique `thenNF_Tc` \ uniq -> - returnNF_Tc (mkSpecPragmaId (nameOccName name) uniq ty (getSrcLoc name)) + returnNF_Tc (mkLocalName uniq (getOccName name) (getSrcLoc name)) \end{code} Make a name for the dict fun for an instance decl. @@ -261,11 +262,7 @@ newDFunName clas [] loc = pprPanic "newDFunName" (ppr clas <+> ppr loc) \begin{code} isLocalThing :: NamedThing a => Module -> a -> Bool - -- True if the thing has a Local name, - -- or a Global name from the specified module -isLocalThing mod thing = case nameModule_maybe (getName thing) of - Nothing -> True -- A local name - Just m -> m == mod -- A global thing +isLocalThing mod thing = nameIsLocalOrFrom mod (getName thing) \end{code} %************************************************************************ @@ -279,7 +276,16 @@ tcExtendGlobalEnv :: [TyThing] -> TcM r -> TcM r tcExtendGlobalEnv things thing_inside = tcGetEnv `thenNF_Tc` \ env -> let - ge' = extendNameEnvList (tcGEnv env) [(getName thing, thing) | thing <- things] + ge' = extendTypeEnvList (tcGEnv env) things + in + tcSetEnv (env {tcGEnv = ge'}) thing_inside + + +tcExtendGlobalTypeEnv :: TypeEnv -> TcM r -> TcM r +tcExtendGlobalTypeEnv extra_env thing_inside + = tcGetEnv `thenNF_Tc` \ env -> + let + ge' = tcGEnv env `plusNameEnv` extra_env in tcSetEnv (env {tcGEnv = ge'}) thing_inside @@ -287,7 +293,7 @@ tcExtendGlobalValEnv :: [Id] -> TcM a -> TcM a tcExtendGlobalValEnv ids thing_inside = tcGetEnv `thenNF_Tc` \ env -> let - ge' = extendNameEnvList (tcGEnv env) [(getName id, AnId id) | id <- ids] + ge' = extendTypeEnvWithIds (tcGEnv env) ids in tcSetEnv (env {tcGEnv = ge'}) thing_inside \end{code} @@ -339,6 +345,14 @@ tcLookupTyCon name Just (ATyCon tc) -> returnNF_Tc tc other -> notFound "tcLookupTyCon" name +tcLookupId :: Name -> NF_TcM Id +tcLookupId name + = tcLookup name `thenNF_Tc` \ thing -> + case thing of + ATcId tc_id -> returnNF_Tc tc_id + AGlobal (AnId id) -> returnNF_Tc id + other -> pprPanic "tcLookupId" (ppr name) + tcLookupLocalIds :: [Name] -> NF_TcM [TcId] tcLookupLocalIds ns = tcGetEnv `thenNF_Tc` \ env -> @@ -347,21 +361,6 @@ tcLookupLocalIds ns lookup lenv name = case lookupNameEnv lenv name of Just (ATcId id) -> id other -> pprPanic "tcLookupLocalIds" (ppr name) - -tcLookupSyntaxId :: Name -> NF_TcM Id --- Lookup a name like PrelNum.fromInt, and return the corresponding Id, --- after mapping through the SyntaxMap. This may give us the Id for --- (say) MyPrelude.fromInteger -tcLookupSyntaxId name - = tcGetEnv `thenNF_Tc` \ env -> - returnNF_Tc (case lookup_global env (tcSyntaxMap env name) of - Just (AnId id) -> id - other -> pprPanic "tcLookupSyntaxId" (ppr name)) - -tcLookupSyntaxName :: Name -> NF_TcM Name -tcLookupSyntaxName name - = tcGetEnv `thenNF_Tc` \ env -> - returnNF_Tc (tcSyntaxMap env name) \end{code} @@ -509,7 +508,6 @@ The InstInfo type summarises the information in an instance declaration \begin{code} data InstInfo = InstInfo { - iLocal :: Bool, -- True <=> it's defined in this module iDFunId :: DFunId, -- The dfun id iBinds :: RenamedMonoBinds, -- Bindings, b iPrags :: [RenamedSig] -- User pragmas recorded for generating specialised instances @@ -519,13 +517,13 @@ pprInstInfo info = vcat [ptext SLIT("InstInfo:") <+> ppr (idType (iDFunId info)) nest 4 (ppr (iBinds info))] simpleInstInfoTy :: InstInfo -> Type -simpleInstInfoTy info = case splitDFunTy (idType (iDFunId info)) of +simpleInstInfoTy info = case tcSplitDFunTy (idType (iDFunId info)) of (_, _, _, [ty]) -> ty simpleInstInfoTyCon :: InstInfo -> TyCon -- Gets the type constructor for a simple instance declaration, -- i.e. one of the form instance (...) => C (T a b c) where ... -simpleInstInfoTyCon inst = tyConAppTyCon (simpleInstInfoTy inst) +simpleInstInfoTyCon inst = tcTyConAppTyCon (simpleInstInfoTy inst) \end{code}