[project @ 2000-10-30 17:18:26 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcEnv.lhs
index c782a2a..88d0159 100644 (file)
@@ -6,19 +6,22 @@ module TcEnv(
        -- Getting stuff from the environment
        TcEnv, initTcEnv, 
        tcEnvTyCons, tcEnvClasses, tcEnvIds, tcEnvTcIds, tcEnvTyVars,
+       getTcGEnv,
        
-       -- Instance environment
+       -- Instance environment, and InstInfo type
        tcGetInstEnv, tcSetInstEnv, 
+       InstInfo(..), pprInstInfo,
+       simpleInstInfoTy, simpleInstInfoTyCon, isLocalInst,
 
        -- Global environment
        tcExtendGlobalEnv, tcExtendGlobalValEnv, 
        tcLookupTyCon, tcLookupClass, tcLookupGlobalId, tcLookupDataCon,
-       tcLookupGlobal_maybe,
+       tcLookupGlobal_maybe, tcLookupGlobal,
 
        -- Local environment
        tcExtendKindEnv, 
        tcExtendTyVarEnv, tcExtendTyVarEnvForMeths, 
-       tcExtendLocalValEnv,
+       tcExtendLocalValEnv, tcLookup,
 
        -- Global type variables
        tcGetGlobalTyVars, tcExtendGlobalTyVars,
@@ -28,52 +31,49 @@ module TcEnv(
 
        -- New Ids
        newLocalId, newSpecPragmaId,
-       newDefaultMethodName, newDFunName
+       newDefaultMethodName, newDFunName,
+
+       -- Misc
+       isLocalThing, tcSetEnv, explicitLookupId
   ) where
 
 #include "HsVersions.h"
 
+import RnHsSyn         ( RenamedMonoBinds, RenamedSig )
 import TcMonad
-import TcType  ( TcKind,  TcType, TcTyVar, TcTyVarSet, TcThetaType,
-                 tcInstTyVars, zonkTcTyVars,
-               )
-import Id      ( mkUserLocal, isDataConWrapId_maybe )
-import IdInfo  ( vanillaIdInfo )
-import MkId    ( mkSpecPragmaId )
-import Var     ( TyVar, Id, setVarName,
-                 idType, lazySetIdInfo, idInfo, tyVarKind, UVar,
-               )
+import TcType          ( TcKind,  TcType, TcTyVar, TcTyVarSet, TcThetaType,
+                         tcInstTyVars, zonkTcTyVars,
+                       )
+import Id              ( mkUserLocal, isDataConWrapId_maybe )
+import IdInfo          ( vanillaIdInfo )
+import MkId            ( mkSpecPragmaId )
+import Var             ( TyVar, Id, idType, lazySetIdInfo, idInfo )
 import VarSet
-import VarEnv  ( TyVarSubstEnv )
-import Type    ( Kind, Type, superKind,
-                 tyVarsOfType, tyVarsOfTypes,
-                 splitForAllTys, splitRhoTy, splitFunTys,
-                 splitAlgTyConApp_maybe, getTyVar, getDFunTyKey
-               )
-import DataCon ( DataCon )
-import TyCon   ( TyCon, tyConKind, tyConArity, isSynTyCon )
-import Class   ( Class, ClassOpItem, ClassContext, classTyCon )
-import Subst   ( substTy )
-import Name    ( Name, OccName, NamedThing(..), 
-                 nameOccName, nameModule, getSrcLoc, mkGlobalName,
-                 isLocallyDefined,
-                 NameEnv, emptyNameEnv, lookupNameEnv, nameEnvElts, 
-                 extendNameEnv, extendNameEnvList
-               )
-import OccName ( mkDFunOcc, mkDefaultMethodOcc, occNameString )
-import Module  ( Module )
-import Unify   ( unifyTyListsX, matchTys )
-import HscTypes        ( ModDetails(..), InstEnv, lookupTypeEnv, TyThing(..),
-                 GlobalSymbolTable, Provenance(..) )
-import Unique  ( pprUnique10, Unique, Uniquable(..) )
-import UniqFM
-import Unique  ( Uniquable(..) )
-import Util    ( zipEqual, zipWith3Equal, mapAccumL )
-import SrcLoc  ( SrcLoc )
-import FastString      ( FastString )
-import Maybes
+import Type            ( Type, ThetaType,
+                         tyVarsOfTypes,
+                         splitForAllTys, splitRhoTy,
+                         getDFunTyKey, splitTyConApp_maybe
+                       )
+import DataCon         ( DataCon )
+import TyCon           ( TyCon )
+import Class           ( Class, ClassOpItem, ClassContext )
+import Subst           ( substTy )
+import Name            ( Name, OccName, NamedThing(..), 
+                         nameOccName, nameModule, getSrcLoc, mkGlobalName,
+                         isLocallyDefined, nameModule_maybe,
+                         NameEnv, lookupNameEnv, nameEnvElts, 
+                         extendNameEnvList, emptyNameEnv
+                       )
+import OccName         ( mkDFunOcc, mkDefaultMethodOcc, occNameString )
+import HscTypes                ( DFunId, TypeEnv, HomeSymbolTable, PackageTypeEnv )
+import Module          ( Module )
+import InstEnv         ( InstEnv, emptyInstEnv )
+import HscTypes                ( lookupType, TyThing(..) )
+import Util            ( zipEqual )
+import SrcLoc          ( SrcLoc )
 import Outputable
-import IOExts  ( newIORef )
+
+import IOExts          ( newIORef )
 \end{code}
 
 %************************************************************************
@@ -88,12 +88,12 @@ type TcIdSet = IdSet
 
 data TcEnv
   = TcEnv {
-       tcGST    :: GlobalSymbolTable,  -- The symbol table at the moment we began this compilation
+       tcGST    :: Name -> Maybe TyThing,      -- The type environment at the moment we began this compilation
 
        tcInsts  :: InstEnv,            -- All instances (both imported and in this module)
 
-       tcGEnv   :: NameEnv TyThing,    -- The global type environment we've accumulated while
-                                       -- compiling this module:
+       tcGEnv   :: TypeEnv,            -- The global type environment we've accumulated while
+                {- NameEnv TyThing-}   -- compiling this module:
                                        --      types and classes (both imported and local)
                                        --      imported Ids
                                        -- (Ids defined in this module are in the local envt)
@@ -141,15 +141,18 @@ data TcTyThing
 --     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 :: GlobalSymbolTable -> InstEnv -> IO TcEnv
-initTcEnv gst inst_env
+initTcEnv :: HomeSymbolTable -> PackageTypeEnv -> IO TcEnv
+initTcEnv hst pte 
   = do { gtv_var <- newIORef emptyVarSet ;
-        return (TcEnv { tcGST    = gst,
+        return (TcEnv { tcGST    = lookup,
                         tcGEnv   = emptyNameEnv,
-                        tcInsts  = inst_env,
+                        tcInsts  = emptyInstEnv,
                         tcLEnv   = emptyNameEnv,
                         tcTyVars = gtv_var
         })}
+  where
+    lookup name = lookupType hst pte name
+
 
 tcEnvClasses env = [cl | AClass cl <- nameEnvElts (tcGEnv env)]
 tcEnvTyCons  env = [tc | ATyCon tc <- nameEnvElts (tcGEnv env)] 
@@ -157,6 +160,8 @@ tcEnvIds     env = [id | AnId   id <- nameEnvElts (tcGEnv env)]
 tcEnvTyVars  env = [tv | ATyVar tv <- nameEnvElts (tcLEnv env)]
 tcEnvTcIds   env = [id | ATcId  id <- 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
@@ -177,7 +182,7 @@ lookup_global :: TcEnv -> Name -> Maybe TyThing
 lookup_global env name 
   = case lookupNameEnv (tcGEnv env) name of
        Just thing -> Just thing
-       Nothing    -> lookupTypeEnv (tcGST env) name
+       Nothing    -> tcGST env name
 
 lookup_local :: TcEnv -> Name -> Maybe TcTyThing
        -- Try the local envt and then try the global
@@ -278,6 +283,14 @@ newDefaultMethodName op_name loc
                              loc)
 \end{code}
 
+\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
+\end{code}
 
 %************************************************************************
 %*                                                                     *
@@ -315,14 +328,14 @@ tcLookupGlobal name
   = tcLookupGlobal_maybe name  `thenNF_Tc` \ maybe_thing ->
     case maybe_thing of
        Just thing -> returnNF_Tc thing
-       other      -> notFound "tcLookupGlobal:" name
+       other      -> notFound "tcLookupGlobal" name
 
 tcLookupGlobalId :: Name -> NF_TcM Id
 tcLookupGlobalId name
   = tcLookupGlobal_maybe name  `thenNF_Tc` \ maybe_id ->
     case maybe_id of
        Just (AnId clas) -> returnNF_Tc clas
-       other            -> notFound "tcLookupGlobalId:" name
+       other            -> notFound "tcLookupGlobalId" name
        
 tcLookupDataCon :: Name -> TcM DataCon
 tcLookupDataCon con_name
@@ -337,14 +350,14 @@ tcLookupClass name
   = tcLookupGlobal_maybe name  `thenNF_Tc` \ maybe_clas ->
     case maybe_clas of
        Just (AClass clas) -> returnNF_Tc clas
-       other              -> notFound "tcLookupClass:" name
+       other              -> notFound "tcLookupClass" name
        
 tcLookupTyCon :: Name -> NF_TcM TyCon
 tcLookupTyCon name
   = tcLookupGlobal_maybe name  `thenNF_Tc` \ maybe_tc ->
     case maybe_tc of
        Just (ATyCon tc) -> returnNF_Tc tc
-       other            -> notFound "tcLookupTyCon:" name
+       other            -> notFound "tcLookupTyCon" name
 \end{code}
 
 
@@ -365,7 +378,7 @@ tcLookup name
   = tcLookup_maybe name                `thenNF_Tc` \ maybe_thing ->
     case maybe_thing of
        Just thing -> returnNF_Tc thing
-       other      -> notFound "tcLookup:" name
+       other      -> notFound "tcLookup" name
        -- Extract the IdInfo from an IfaceSig imported from an interface file
 \end{code}
 
@@ -484,6 +497,50 @@ tcSetInstEnv ie thing_inside
 
 %************************************************************************
 %*                                                                     *
+\subsection{The InstInfo type}
+%*                                                                     *
+%************************************************************************
+
+The InstInfo type summarises the information in an instance declaration
+
+    instance c => k (t tvs) where b
+
+\begin{code}
+data InstInfo
+  = InstInfo {
+      iClass :: Class,         -- Class, k
+      iTyVars :: [TyVar],      -- Type variables, tvs
+      iTys    :: [Type],       -- The types at which the class is being instantiated
+      iTheta  :: ThetaType,    -- inst_decl_theta: the original context, c, from the
+                               --   instance declaration.  It constrains (some of)
+                               --   the TyVars above
+      iLocal  :: Bool,         -- True <=> it's defined in this module
+      iDFunId :: DFunId,               -- The dfun id
+      iBinds  :: RenamedMonoBinds,     -- Bindings, b
+      iLoc    :: SrcLoc,               -- Source location assoc'd with this instance's defn
+      iPrags  :: [RenamedSig]          -- User pragmas recorded for generating specialised instances
+    }
+
+pprInstInfo info = vcat [ptext SLIT("InstInfo:") <+> ppr (idType (iDFunId info)),
+                        nest 4 (ppr (iBinds info))]
+
+simpleInstInfoTy :: InstInfo -> Type
+simpleInstInfoTy (InstInfo {iTys = [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
+   = case splitTyConApp_maybe (simpleInstInfoTy inst) of 
+       Just (tycon, _) -> tycon
+
+isLocalInst :: Module -> InstInfo -> Bool
+isLocalInst mod info = isLocalThing mod (iDFunId info)
+\end{code}
+
+
+%************************************************************************
+%*                                                                     *
 \subsection{Errors}
 %*                                                                     *
 %************************************************************************