[project @ 2000-12-08 12:56:00 by simonpj]
[ghc-hetmet.git] / ghc / compiler / typecheck / TcEnv.lhs
index 88d0159..0dbc636 100644 (file)
@@ -11,12 +11,12 @@ module TcEnv(
        -- Instance environment, and InstInfo type
        tcGetInstEnv, tcSetInstEnv, 
        InstInfo(..), pprInstInfo,
-       simpleInstInfoTy, simpleInstInfoTyCon, isLocalInst,
+       simpleInstInfoTy, simpleInstInfoTyCon, 
 
        -- Global environment
        tcExtendGlobalEnv, tcExtendGlobalValEnv, 
        tcLookupTyCon, tcLookupClass, tcLookupGlobalId, tcLookupDataCon,
-       tcLookupGlobal_maybe, tcLookupGlobal,
+       tcLookupGlobal_maybe, tcLookupGlobal, 
 
        -- Local environment
        tcExtendKindEnv, 
@@ -27,14 +27,14 @@ module TcEnv(
        tcGetGlobalTyVars, tcExtendGlobalTyVars,
 
        -- Random useful things
-       tcAddImportedIdInfo, tcInstId,
+       RecTcEnv, tcAddImportedIdInfo, tcLookupRecId, tcLookupRecId_maybe, tcInstId,
 
        -- New Ids
        newLocalId, newSpecPragmaId,
-       newDefaultMethodName, newDFunName,
+       newDFunName,
 
        -- Misc
-       isLocalThing, tcSetEnv, explicitLookupId
+       isLocalThing, tcSetEnv
   ) where
 
 #include "HsVersions.h"
@@ -44,27 +44,26 @@ import TcMonad
 import TcType          ( TcKind,  TcType, TcTyVar, TcTyVarSet, TcThetaType,
                          tcInstTyVars, zonkTcTyVars,
                        )
-import Id              ( mkUserLocal, isDataConWrapId_maybe )
-import IdInfo          ( vanillaIdInfo )
+import Id              ( idName, mkUserLocal, isDataConWrapId_maybe )
+import IdInfo          ( constantIdInfo )
 import MkId            ( mkSpecPragmaId )
 import Var             ( TyVar, Id, idType, lazySetIdInfo, idInfo )
 import VarSet
-import Type            ( Type, ThetaType,
-                         tyVarsOfTypes,
+import Type            ( Type,
+                         tyVarsOfTypes, splitDFunTy,
                          splitForAllTys, splitRhoTy,
-                         getDFunTyKey, splitTyConApp_maybe
+                         getDFunTyKey, tyConAppTyCon
                        )
 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
+                         nameOccName, getSrcLoc, mkLocalName,
+                         isLocalName, nameModule_maybe
                        )
-import OccName         ( mkDFunOcc, mkDefaultMethodOcc, occNameString )
+import Name            ( NameEnv, lookupNameEnv, nameEnvElts, extendNameEnvList, emptyNameEnv )
+import OccName         ( mkDFunOcc, occNameString )
 import HscTypes                ( DFunId, TypeEnv, HomeSymbolTable, PackageTypeEnv )
 import Module          ( Module )
 import InstEnv         ( InstEnv, emptyInstEnv )
@@ -151,7 +150,8 @@ initTcEnv hst pte
                         tcTyVars = gtv_var
         })}
   where
-    lookup name = lookupType hst pte name
+    lookup name | isLocalName name = Nothing
+               | otherwise        = lookupType hst pte name
 
 
 tcEnvClasses env = [cl | AClass cl <- nameEnvElts (tcGEnv env)]
@@ -165,7 +165,7 @@ 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] [Class]
+                   | DataTyDetails ClassContext [DataCon] [Id]
                    | ClassDetails ClassContext [Id] [ClassOpItem] DataCon
 \end{code}
 
@@ -192,13 +192,34 @@ lookup_local env name
        Nothing    -> case lookup_global env name of
                        Just thing -> Just (AGlobal thing)
                        Nothing    -> Nothing
-
-explicitLookupId :: TcEnv -> Name -> Maybe Id
-explicitLookupId env name = case lookup_global env name of
-                               Just (AnId id) -> Just id
-                               other          -> Nothing
 \end{code}
 
+\begin{code}
+type RecTcEnv = TcEnv
+-- This environment is used for getting the 'right' IdInfo 
+-- on imported things and for looking up Ids in unfoldings
+-- The environment doesn't have any local Ids in it
+
+tcAddImportedIdInfo :: RecTcEnv -> Id -> Id
+tcAddImportedIdInfo env id
+  = id `lazySetIdInfo` new_info
+       -- 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
+                 Just imported_id -> idInfo imported_id
+               -- ToDo: could check that types are the same
+
+tcLookupRecId_maybe :: RecTcEnv -> Name -> Maybe Id
+tcLookupRecId_maybe env name = case lookup_global env name of
+                                  Just (AnId id) -> Just id
+                                  other          -> Nothing
+
+tcLookupRecId ::  RecTcEnv -> Name -> Id
+tcLookupRecId env name = case lookup_global env name of
+                               Just (AnId id) -> id
+                               Nothing        -> pprPanic "tcLookupRecId" (ppr name)
+\end{code}
 
 %************************************************************************
 %*                                                                     *
@@ -224,20 +245,6 @@ tcInstId id
        (theta', tau') = splitRhoTy rho' 
     in
     returnNF_Tc (tyvars', theta', tau')
-
-tcAddImportedIdInfo :: TcEnv -> Id -> Id
-tcAddImportedIdInfo unf_env id
-  | isLocallyDefined id                -- Don't look up locally defined Ids, because they
-                               -- have explicit local definitions, so we get a black hole!
-  = id
-  | otherwise
-  = id `lazySetIdInfo` new_info
-       -- The Id must be returned without a data dependency on maybe_id
-  where
-    new_info = case explicitLookupId unf_env (getName id) of
-                    Nothing          -> vanillaIdInfo
-                    Just imported_id -> idInfo imported_id
-               -- ToDo: could check that types are the same
 \end{code}
 
 
@@ -261,26 +268,20 @@ newSpecPragmaId name ty
     returnNF_Tc (mkSpecPragmaId (nameOccName name) uniq ty (getSrcLoc name))
 \end{code}
 
-Make a name for the dict fun for an instance decl
+Make a name for the dict fun for an instance decl.
+It's a *local* name for the moment.  The CoreTidy pass
+will globalise it.
 
 \begin{code}
-newDFunName :: Module -> Class -> [Type] -> SrcLoc -> NF_TcM Name
-newDFunName mod clas (ty:_) loc
-  = tcGetDFunUniq dfun_string  `thenNF_Tc` \ inst_uniq ->
-    tcGetUnique                        `thenNF_Tc` \ uniq ->
-    returnNF_Tc (mkGlobalName uniq mod
-                             (mkDFunOcc dfun_string inst_uniq) 
-                             loc)
+newDFunName :: Class -> [Type] -> SrcLoc -> NF_TcM Name
+newDFunName clas (ty:_) loc
+  = tcGetUnique                        `thenNF_Tc` \ uniq ->
+    returnNF_Tc (mkLocalName uniq (mkDFunOcc dfun_string) loc)
   where
        -- Any string that is somewhat unique will do
     dfun_string = occNameString (getOccName clas) ++ occNameString (getDFunTyKey ty)
 
-newDefaultMethodName :: Name -> SrcLoc -> NF_TcM Name
-newDefaultMethodName op_name loc
-  = tcGetUnique                        `thenNF_Tc` \ uniq ->
-    returnNF_Tc (mkGlobalName uniq (nameModule op_name)
-                             (mkDefaultMethodOcc (getOccName op_name))
-                             loc)
+newDFunName clas [] loc = pprPanic "newDFunName" (ppr clas <+> ppr loc)
 \end{code}
 
 \begin{code}
@@ -299,17 +300,21 @@ isLocalThing mod thing = case nameModule_maybe (getName thing) of
 %************************************************************************
 
 \begin{code}
-tcExtendGlobalEnv :: [(Name, TyThing)] -> TcM r -> TcM r
-tcExtendGlobalEnv bindings thing_inside
+tcExtendGlobalEnv :: [TyThing] -> TcM r -> TcM r
+tcExtendGlobalEnv things thing_inside
   = tcGetEnv                           `thenNF_Tc` \ env ->
     let
-       ge' = extendNameEnvList (tcGEnv env) bindings
+       ge' = extendNameEnvList (tcGEnv env) [(getName thing, thing) | thing <- things]
     in
     tcSetEnv (env {tcGEnv = ge'}) thing_inside
 
 tcExtendGlobalValEnv :: [Id] -> TcM a -> TcM a
 tcExtendGlobalValEnv ids thing_inside
-  = tcExtendGlobalEnv [(getName id, AnId id) | id <- ids] thing_inside
+  = tcGetEnv                           `thenNF_Tc` \ env ->
+    let
+       ge' = extendNameEnvList (tcGEnv env) [(getName id, AnId id) | id <- ids]
+    in
+    tcSetEnv (env {tcGEnv = ge'}) thing_inside
 \end{code}
 
 
@@ -508,16 +513,9 @@ The InstInfo type summarises the information in an instance declaration
 \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
+      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
     }
 
@@ -525,17 +523,13 @@ pprInstInfo info = vcat [ptext SLIT("InstInfo:") <+> ppr (idType (iDFunId info))
                         nest 4 (ppr (iBinds info))]
 
 simpleInstInfoTy :: InstInfo -> Type
-simpleInstInfoTy (InstInfo {iTys = [ty]}) = ty
+simpleInstInfoTy info = case splitDFunTy (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
-   = case splitTyConApp_maybe (simpleInstInfoTy inst) of 
-       Just (tycon, _) -> tycon
-
-isLocalInst :: Module -> InstInfo -> Bool
-isLocalInst mod info = isLocalThing mod (iDFunId info)
+simpleInstInfoTyCon inst = tyConAppTyCon (simpleInstInfoTy inst)
 \end{code}