[project @ 1998-11-26 09:17:22 by sof]
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreLint.lhs
index d4dffad..7dada83 100644 (file)
@@ -15,39 +15,44 @@ import IO   ( hPutStr, stderr )
 
 import CmdLineOpts      ( opt_D_show_passes, opt_DoCoreLinting )
 import CoreSyn
+import CoreUtils       ( idSpecVars )
 
 import Bag
-import Kind            ( hasMoreBoxityInfo, Kind{-instance-}, 
-                         isTypeKind, isBoxedTypeKind {- TEMP --SOF -} )
+import Kind            ( hasMoreBoxityInfo, Kind{-instance-} )
 import Literal         ( literalType, Literal{-instance-} )
-import Id              ( idType, isBottomingId, dataConRepType, isDataCon, isNewCon, isAlgCon,
+import Id              ( idType, isBottomingId, dataConRepType, isDataCon, isAlgCon,
                          dataConArgTys, GenId{-instances-},
-                         emptyIdSet, mkIdSet, intersectIdSets,
+                         emptyIdSet, mkIdSet, 
                          unionIdSets, elementOfIdSet, IdSet,
                          Id
                        )
 import Maybes          ( catMaybes )
 import Name            ( isLocallyDefined, getSrcLoc, Name{-instance NamedThing-},
-                         NamedThing(..) )
+                         NamedThing(..)
+                       )
 import PprCore
 import ErrUtils                ( doIfSet, ghcExit )
-import PprType         ( GenType, GenTyVar, TyCon )
-import PrimOp          ( primOpType, PrimOp(..) )
+import PrimOp          ( primOpType )
 import PrimRep         ( PrimRep(..) )
 import SrcLoc          ( SrcLoc )
 import Type            ( mkFunTy, splitFunTy_maybe, mkForAllTy,
-                         splitForAllTy_maybe,
-                         isUnpointedType, typeKind, instantiateTy, splitSigmaTy,
+                         splitForAllTy_maybe, tyVarsOfType,
+                         isUnpointedType, typeKind, instantiateTy,
                          splitAlgTyConApp_maybe, Type
                        )
-import TyCon           ( isPrimTyCon, isDataTyCon )
-import TyVar           ( TyVar, tyVarKind, mkTyVarEnv )
+import TyCon           ( TyCon, isPrimTyCon, isDataTyCon )
+import TyVar           ( TyVar, tyVarKind, mkTyVarEnv, 
+                         TyVarSet,
+                           emptyTyVarSet, mkTyVarSet, isEmptyTyVarSet, 
+                           minusTyVarSet, elementOfTyVarSet, tyVarSetToList,
+                           unionTyVarSets, intersectTyVarSets
+                       )
 import ErrUtils                ( ErrMsg )
 import Unique          ( Unique )
 import Util            ( zipEqual )
 import Outputable
 
-infixr 9 `thenL`, `seqL`, `thenMaybeL`, `seqMaybeL`
+infixr 9 `thenL`, `seqL`, `thenMaybeL`
 \end{code}
 
 %************************************************************************
@@ -175,11 +180,17 @@ lintSingleBinding (binder,rhs)
        `seqL`
        -- Check (not isUnpointedType)
        checkIfSpecDoneL (not (isUnpointedType (idType binder)))
-         (mkRhsPrimMsg binder rhs)
+         (mkRhsPrimMsg binder rhs)  `seqL`
 
+        -- Check whether binder's specialisations contain any out-of-scope variables
+        ifSpecDoneL (mapL (checkSpecIdInScope binder) spec_vars `seqL` returnL ())
+         
        -- We should check the unfolding, if any, but this is tricky because
        -- the unfolding is a SimplifiableCoreExpr. Give up for now.
     )
+    where
+     spec_vars = idSpecVars binder
+
 \end{code}
 
 %************************************************************************
@@ -203,13 +214,19 @@ lintCoreExpr (Var var)
        -- The hack here simply doesn't check for out-of-scope-ness for
        -- data constructors (at least, in a function position).
 
-  | otherwise    = checkInScope var `seqL` returnL (Just (idType var))
+  | otherwise    = checkIdInScope var `seqL` returnL (Just (idType var))
 
 lintCoreExpr (Lit lit) = returnL (Just (literalType lit))
-lintCoreExpr (SCC _ expr) = lintCoreExpr expr
-lintCoreExpr e@(Coerce coercion ty expr)
-  = lintCoercion e coercion    `seqL`
-    lintCoreExpr expr `seqL` returnL (Just ty)
+
+lintCoreExpr (Note (Coerce to_ty from_ty) expr)
+  = lintCoreExpr expr  `thenMaybeL` \ expr_ty ->
+    lintTy to_ty       `seqL`
+    lintTy from_ty     `seqL`
+    checkTys from_ty expr_ty (mkCoerceErr from_ty expr_ty)     `seqL`
+    returnL (Just to_ty)
+
+lintCoreExpr (Note other_note expr)
+  = lintCoreExpr expr
 
 lintCoreExpr (Let binds body)
   = lintCoreBinding binds `thenL` \binders ->
@@ -236,16 +253,17 @@ lintCoreExpr e@(App fun arg)
   = lintCoreExpr fun `thenMaybeL` \ty -> lintCoreArg {-True-} e ty arg
     -- Note: we do check for primitive types in this argument
 
-lintCoreExpr (Lam (ValBinder var) expr)
-  = addLoc (LambdaBodyOf var)
+lintCoreExpr (Lam vb@(ValBinder var) expr)
+  = addLoc (LambdaBodyOf vb)
       (addInScopeVars [var]
        (lintCoreExpr expr `thenMaybeL` \ty ->
         returnL (Just (mkFunTy (idType var) ty))))
 
-lintCoreExpr (Lam (TyBinder tyvar) expr)
-  = lintCoreExpr expr `thenMaybeL` \ty ->
-    returnL (Just(mkForAllTy tyvar ty))
-    -- ToDo: Should add in-scope type variable at this point
+lintCoreExpr (Lam tb@(TyBinder tyvar) expr)
+  = addLoc (LambdaBodyOf tb)  $
+     addInScopeTyVars [tyvar] $
+       lintCoreExpr expr                          `thenMaybeL` \ ty ->
+       returnL (Just(mkForAllTy tyvar ty))
 
 lintCoreExpr e@(Case scrut alts)
  = lintCoreExpr scrut `thenMaybeL` \ty ->
@@ -289,7 +307,7 @@ lintCoreArg e ty (LitArg lit)
 
 lintCoreArg e ty (VarArg v)
   = -- Make sure variable is bound
-    checkInScope v `seqL`
+    checkIdInScope v `seqL`
     -- Make sure function type matches argument
     case (splitFunTy_maybe ty) of
       Just (arg,res) | (var_ty == arg) -> returnL(Just res)
@@ -298,7 +316,8 @@ lintCoreArg e ty (VarArg v)
     var_ty = idType v
 
 lintCoreArg e ty a@(TyArg arg_ty)
-  = -- ToDo: Check that ty is well-kinded and has no unbound tyvars
+  = lintTy arg_ty                           `seqL`
+    checkTyVarsInScope (tyVarsOfType arg_ty) `seqL`
     case (splitForAllTy_maybe ty) of
       Nothing -> addErrL (mkTyAppMsg SLIT("Illegal") ty arg_ty e) `seqL` returnL Nothing
 
@@ -407,19 +426,17 @@ lintDeflt deflt@(BindDefault binder rhs) ty
 
 %************************************************************************
 %*                                                                     *
-\subsection[lint-coercion]{Coercion}
+\subsection[lint-types]{Types}
 %*                                                                     *
 %************************************************************************
 
 \begin{code}
-lintCoercion e (CoerceIn  con) = check_con e con
-lintCoercion e (CoerceOut con) = check_con e con
-
-check_con e con = checkL (isNewCon con)
-                        (mkCoerceErrMsg e)
+lintTy :: Type -> LintM ()
+lintTy ty = returnL ()
+-- ToDo: Check that ty is well-kinded and has no unbound tyvars
 \end{code}
 
-
+    
 %************************************************************************
 %*                                                                     *
 \subsection[lint-monad]{The Lint monad}
@@ -430,24 +447,29 @@ check_con e con = checkL (isNewCon con)
 type LintM a = Bool            -- True <=> specialisation has been done
            -> [LintLocInfo]    -- Locations
            -> IdSet            -- Local vars in scope
+           -> TyVarSet         -- Local tyvars in scope
            -> Bag ErrMsg       -- Error messages so far
            -> (a, Bag ErrMsg)  -- Result and error messages (if any)
 
 data LintLocInfo
-  = RhsOf Id           -- The variable bound
-  | LambdaBodyOf Id    -- The lambda-binder
-  | BodyOfLetRec [Id]  -- One of the binders
-  | ImportedUnfolding SrcLoc -- Some imported unfolding (ToDo: say which)
+  = RhsOf Id                   -- The variable bound
+  | LambdaBodyOf CoreBinder    -- The lambda-binder
+  | BodyOfLetRec [Id]          -- One of the binders
+  | ImportedUnfolding SrcLoc    -- Some imported unfolding (ToDo: say which)
 
 instance Outputable LintLocInfo where
     ppr (RhsOf v)
       = ppr (getSrcLoc v) <> colon <+> 
        brackets (ptext SLIT("RHS of") <+> pp_binders [v])
 
-    ppr (LambdaBodyOf b)
+    ppr (LambdaBodyOf (ValBinder b))
       = ppr (getSrcLoc b) <> colon <+>
        brackets (ptext SLIT("in body of lambda with binder") <+> pp_binder b)
 
+    ppr (LambdaBodyOf (TyBinder b))
+      = ppr (getSrcLoc b) <> colon <+>
+       brackets (ptext SLIT("in body of lambda with type binder") <+> ppr b)
+
     ppr (BodyOfLetRec bs)
       = ppr (getSrcLoc (head bs)) <> colon <+>
        brackets (ptext SLIT("in body of letrec with binders") <+> pp_binders bs)
@@ -466,7 +488,7 @@ pp_binder b = hsep [ppr b, text "::", ppr (idType b)]
 \begin{code}
 initL :: LintM a -> Bool -> Maybe ErrMsg
 initL m spec_done
-  = case (m spec_done [] emptyIdSet emptyBag) of { (_, errs) ->
+  = case (m spec_done [] emptyIdSet emptyTyVarSet emptyBag) of { (_, errs) ->
     if isEmptyBag errs then
        Nothing
     else
@@ -474,29 +496,23 @@ initL m spec_done
     }
 
 returnL :: a -> LintM a
-returnL r spec loc scope errs = (r, errs)
+returnL r spec loc scope tyscope errs = (r, errs)
 
 thenL :: LintM a -> (a -> LintM b) -> LintM b
-thenL m k spec loc scope errs
-  = case m spec loc scope errs of
-      (r, errs') -> k r spec loc scope errs'
+thenL m k spec loc scope tyscope errs
+  = case m spec loc scope tyscope errs of
+      (r, errs') -> k r spec loc scope tyscope errs'
 
 seqL :: LintM a -> LintM b -> LintM b
-seqL m k spec loc scope errs
-  = case m spec loc scope errs of
-      (_, errs') -> k spec loc scope errs'
+seqL m k spec loc scope tyscope errs
+  = case m spec loc scope tyscope errs of
+      (_, errs') -> k spec loc scope tyscope errs'
 
 thenMaybeL :: LintM (Maybe a) -> (a -> LintM (Maybe b)) -> LintM (Maybe b)
-thenMaybeL m k spec loc scope errs
-  = case m spec loc scope errs of
-      (Nothing, errs2) -> (Nothing, errs2)
-      (Just r,  errs2) -> k r spec loc scope errs2
-
-seqMaybeL :: LintM (Maybe a) -> LintM (Maybe b) -> LintM (Maybe b)
-seqMaybeL m k spec loc scope errs
-  = case m spec loc scope errs of
+thenMaybeL m k spec loc scope tyscope errs
+  = case m spec loc scope tyscope errs of
       (Nothing, errs2) -> (Nothing, errs2)
-      (Just _,  errs2) -> k spec loc scope errs2
+      (Just r,  errs2) -> k r spec loc scope tyscope errs2
 
 mapL :: (a -> LintM b) -> [a] -> LintM [b]
 mapL f [] = returnL []
@@ -516,19 +532,20 @@ mapMaybeL f (x:xs)
 
 \begin{code}
 checkL :: Bool -> ErrMsg -> LintM ()
-checkL True  msg spec loc scope errs = ((), errs)
-checkL False msg spec loc scope errs = ((), addErr errs msg loc)
+checkL True  msg spec loc scope tyscope errs = ((), errs)
+checkL False msg spec loc scope tyscope errs = ((), addErr errs msg loc)
 
 checkIfSpecDoneL :: Bool -> ErrMsg -> LintM ()
-checkIfSpecDoneL True  msg spec  loc scope errs = ((), errs)
-checkIfSpecDoneL False msg True  loc scope errs = ((), addErr errs msg loc)
-checkIfSpecDoneL False msg False loc scope errs = ((), errs)
+checkIfSpecDoneL True  msg spec  loc scope tyscope errs = ((), errs)
+checkIfSpecDoneL False msg True  loc scope tyscope errs = ((), addErr errs msg loc)
+checkIfSpecDoneL False msg False loc scope tyscope errs = ((), errs)
 
-addErrIfL pred spec
-  = if pred then addErrL spec else returnL ()
+ifSpecDoneL :: LintM () -> LintM ()
+ifSpecDoneL m False loc scope tyscope errs = ((), errs)
+ifSpecDoneL m True  loc scope tyscope errs = m True loc scope tyscope errs
 
 addErrL :: ErrMsg -> LintM ()
-addErrL msg spec loc scope errs = ((), addErr errs msg loc)
+addErrL msg spec loc scope tyscope errs = ((), addErr errs msg loc)
 
 addErr :: Bag ErrMsg -> ErrMsg -> [LintLocInfo] -> Bag ErrMsg
 
@@ -537,11 +554,11 @@ addErr errs_so_far msg locs
     errs_so_far `snocBag` (hang (ppr (head locs)) 4 msg)
 
 addLoc :: LintLocInfo -> LintM a -> LintM a
-addLoc extra_loc m spec loc scope errs
-  = m spec (extra_loc:loc) scope errs
+addLoc extra_loc m spec loc scope tyscope errs
+  = m spec (extra_loc:loc) scope tyscope errs
 
 addInScopeVars :: [Id] -> LintM a -> LintM a
-addInScopeVars ids m spec loc scope errs
+addInScopeVars ids m spec loc scope tyscope errs
   = -- We check if these "new" ids are already
     -- in scope, i.e., we have *shadowing* going on.
     -- For now, it's just a "trace"; we may make
@@ -556,23 +573,52 @@ addInScopeVars ids m spec loc scope errs
 --  (if isEmptyUniqSet shadowed
 --  then id
 --  else pprTrace "Shadowed vars:" (ppr (uniqSetToList shadowed))) (
-    m spec loc (scope `unionIdSets` new_set) errs
+    m spec loc (scope `unionIdSets` new_set) tyscope errs
 --  )
+
+addInScopeTyVars :: [TyVar] -> LintM a -> LintM a
+addInScopeTyVars tyvars m spec loc scope tyscope errs
+  = m spec loc scope (tyscope `unionTyVarSets` new_set) errs
+    where
+     new_set   = mkTyVarSet tyvars
+    
 \end{code}
 
 \begin{code}
-checkInScope :: Id -> LintM ()
-checkInScope id spec loc scope errs
+checkIdInScope :: Id -> LintM ()
+checkIdInScope id 
+  = checkInScope (ptext SLIT("is out of scope")) id
+
+checkSpecIdInScope :: Id -> Id -> LintM ()
+checkSpecIdInScope binder id 
+  = checkInScope msg id
+    where
+     msg = ptext SLIT("is out of scope inside specialisation info for") <+> 
+          ppr binder
+
+checkInScope :: SDoc -> Id -> LintM ()
+checkInScope loc_msg id spec loc scope tyscope errs
   = let
        id_name = getName id
     in
     if isLocallyDefined id_name && not (id `elementOfIdSet` scope) then
-      ((), addErr errs (hsep [ppr id, ptext SLIT("is out of scope")]) loc)
+      ((), addErr errs (hsep [ppr id, loc_msg]) loc)
     else
       ((),errs)
 
+checkTyVarsInScope :: TyVarSet -> LintM ()
+checkTyVarsInScope tyvars spec loc scope tyscope errs
+-- | not (isEmptyTyVarSet out_of_scope) = ((), errs')
+ | otherwise                   = ((), errs)
+   where
+    out_of_scope = tyvars `minusTyVarSet` tyscope
+    errs'        = 
+       foldr (\ tv errs -> addErr errs (hsep [ppr tv, ptext SLIT("is out of scope")]) loc)
+            errs
+            (tyVarSetToList out_of_scope)
+
 checkTys :: Type -> Type -> ErrMsg -> LintM ()
-checkTys ty1 ty2 msg spec loc scope errs
+checkTys ty1 ty2 msg spec loc scope tyscope errs
   = if ty1 == ty2 then ((), errs) else ((), addErr errs msg loc)
 \end{code}
 
@@ -581,31 +627,12 @@ mkConErrMsg e
   = ($$) (ptext SLIT("Application of newtype constructor:"))
            (ppr e)
 
-mkCoerceErrMsg e
-  = ($$) (ptext SLIT("Coercion using a datatype constructor:"))
-        (ppr e)
-
 
 mkCaseAltMsg :: CoreCaseAlts -> ErrMsg
 mkCaseAltMsg alts
   = ($$) (ptext SLIT("Type of case alternatives not the same:"))
            (ppr alts)
 
-mkCaseDataConMsg :: CoreExpr -> ErrMsg
-mkCaseDataConMsg expr
-  = ($$) (ptext SLIT("A case scrutinee not of data constructor type:"))
-           (pprCoreExpr expr)
-
-mkCaseNotPrimMsg :: TyCon -> ErrMsg
-mkCaseNotPrimMsg tycon
-  = ($$) (ptext SLIT("A primitive case on a non-primitive type:"))
-           (ppr tycon)
-
-mkCasePrimMsg :: TyCon -> ErrMsg
-mkCasePrimMsg tycon
-  = ($$) (ptext SLIT("An algebraic case on a primitive type:"))
-           (ppr tycon)
-
 mkCaseAbstractMsg :: TyCon -> ErrMsg
 mkCaseAbstractMsg tycon
   = ($$) (ptext SLIT("An algebraic case on some weird type:"))
@@ -691,9 +718,9 @@ mkRhsPrimMsg binder rhs
              hsep [ptext SLIT("Binder's type:"), ppr (idType binder)]
             ]
 
-mkSpecTyAppMsg :: CoreArg -> ErrMsg
-mkSpecTyAppMsg arg
-  = ($$)
-      (ptext SLIT("Unboxed types in a type application (after specialisation):"))
-      (ppr arg)
+mkCoerceErr from_ty expr_ty
+  = vcat [ptext SLIT("From-type of Coerce differs from type of enclosed expression"),
+         ptext SLIT("From-type:") <+> ppr from_ty,
+         ptext SLIT("Type of enclosed expr:") <+> ppr expr_ty
+    ]
 \end{code}