{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
module Rattus.Plugin.Strictify
  (strictifyExpr, SCxt (..)) where
import Prelude hiding ((<>))
import Rattus.Plugin.Utils

#if __GLASGOW_HASKELL__ >= 900
import GHC.Plugins
#else
import GhcPlugins
#endif

#if __GLASGOW_HASKELL__ >= 902
import GHC.Types.Tickish
#endif

data SCxt = SCxt {SCxt -> SrcSpan
srcSpan :: SrcSpan, SCxt -> Bool
checkStrictData :: Bool}

-- | Transforms all functions into strict functions. If the
-- 'checkStrictData' field of the 'SCxt' argument is set to @True@,
-- then this function also checks for use of non-strict data types and
-- produces warnings if it finds any.
strictifyExpr :: SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr :: SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss (Let (NonRec CoreBndr
b CoreExpr
e1) CoreExpr
e2) = do
  CoreExpr
e1' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e1
  CoreExpr
e2' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e2
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case CoreExpr
e1' CoreBndr
b (CoreExpr -> Type
exprType CoreExpr
e2) [forall {b}. AltCon -> [b] -> Expr b -> Alt b
mkAlt AltCon
DEFAULT [] CoreExpr
e2' ])
strictifyExpr SCxt
ss (Case CoreExpr
e CoreBndr
b Type
t [Alt CoreBndr]
alts) = do
  CoreExpr
e' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e
  [Alt CoreBndr]
alts' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((\(AltCon
c,[CoreBndr]
args,CoreExpr
e) -> forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\CoreExpr
e' -> forall {b}. AltCon -> [b] -> Expr b -> Alt b
mkAlt AltCon
c [CoreBndr]
args CoreExpr
e' ) (SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {b}. Alt b -> (AltCon, [b], Expr b)
getAlt) [Alt CoreBndr]
alts
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case CoreExpr
e' CoreBndr
b Type
t [Alt CoreBndr]
alts')
strictifyExpr SCxt
ss (Let (Rec [(CoreBndr, CoreExpr)]
es) CoreExpr
e) = do
  [(CoreBndr, CoreExpr)]
es' <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\ (CoreBndr
b,CoreExpr
e) -> SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \CoreExpr
e'-> forall (m :: * -> *) a. Monad m => a -> m a
return (CoreBndr
b,CoreExpr
e')) [(CoreBndr, CoreExpr)]
es
  CoreExpr
e' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Bind b -> Expr b -> Expr b
Let (forall b. [(b, Expr b)] -> Bind b
Rec [(CoreBndr, CoreExpr)]
es') CoreExpr
e')
strictifyExpr SCxt
ss (Lam CoreBndr
b CoreExpr
e)
   | Bool -> Bool
not (CoreBndr -> Bool
isCoVar CoreBndr
b) Bool -> Bool -> Bool
&& Bool -> Bool
not (CoreBndr -> Bool
isTyVar CoreBndr
b) Bool -> Bool -> Bool
&& Type -> Bool
tcIsLiftedTypeKind(HasDebugCallStack => Type -> Type
typeKind (CoreBndr -> Type
varType CoreBndr
b))
    = do
       CoreExpr
e' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e
       CoreBndr
b' <- forall (m :: * -> *).
MonadUnique m =>
FastString -> CoreBndr -> m CoreBndr
mkSysLocalFromVar (String -> FastString
fsLit String
"strict") CoreBndr
b
       forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. b -> Expr b -> Expr b
Lam CoreBndr
b' (forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case (forall b. CoreBndr -> Expr b
varToCoreExpr CoreBndr
b') CoreBndr
b (CoreExpr -> Type
exprType CoreExpr
e) [forall {b}. AltCon -> [b] -> Expr b -> Alt b
mkAlt AltCon
DEFAULT [] CoreExpr
e' ]))
   | Bool
otherwise = do
       CoreExpr
e' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e
       forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. b -> Expr b -> Expr b
Lam CoreBndr
b CoreExpr
e')
strictifyExpr SCxt
ss (Cast CoreExpr
e CoercionR
c) = do
  CoreExpr
e' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Expr b -> CoercionR -> Expr b
Cast CoreExpr
e' CoercionR
c)
strictifyExpr SCxt
ss (Tick t :: CoreTickish
t@(SourceNote RealSrcSpan
span String
_) CoreExpr
e) = do
  CoreExpr
e' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr (SCxt
ss{srcSpan :: SrcSpan
srcSpan = RealSrcSpan -> SrcSpan
fromRealSrcSpan RealSrcSpan
span}) CoreExpr
e
  forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. CoreTickish -> Expr b -> Expr b
Tick CoreTickish
t CoreExpr
e')
strictifyExpr SCxt
ss (App CoreExpr
e1 CoreExpr
e2)
  | (SCxt -> Bool
checkStrictData SCxt
ss Bool -> Bool -> Bool
&& Bool -> Bool
not (forall {b}. Expr b -> Bool
isType CoreExpr
e2) Bool -> Bool -> Bool
&& Type -> Bool
tcIsLiftedTypeKind(HasDebugCallStack => Type -> Type
typeKind (CoreExpr -> Type
exprType CoreExpr
e2))
        Bool -> Bool -> Bool
&& Bool -> Bool
not (Type -> Bool
isStrict (CoreExpr -> Type
exprType CoreExpr
e2))) = do
      (forall (m :: * -> *).
(HasDynFlags m, MonadIO m, HasLogger m) =>
Severity -> SrcSpan -> SDoc -> m ()
printMessage Severity
SevWarning (SCxt -> SrcSpan
srcSpan SCxt
ss)
         (String -> SDoc
text String
"The use of lazy type " SDoc -> SDoc -> SDoc
<> forall a. Outputable a => a -> SDoc
ppr (CoreExpr -> Type
exprType CoreExpr
e2) SDoc -> SDoc -> SDoc
<> SDoc
" may lead to memory leaks"))
      CoreExpr
e1' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss{checkStrictData :: Bool
checkStrictData = Bool
False} CoreExpr
e1
      CoreExpr
e2' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss{checkStrictData :: Bool
checkStrictData = Bool
False} CoreExpr
e2
      forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Expr b -> Expr b -> Expr b
App CoreExpr
e1' CoreExpr
e2')
  | Bool
otherwise = do
      CoreExpr
e1' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e1
      CoreExpr
e2' <- SCxt -> CoreExpr -> CoreM CoreExpr
strictifyExpr SCxt
ss CoreExpr
e2
      forall (m :: * -> *) a. Monad m => a -> m a
return (forall b. Expr b -> Expr b -> Expr b
App CoreExpr
e1' CoreExpr
e2')
strictifyExpr SCxt
_ss CoreExpr
e = forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
e