module GHC.HsToCore.GuardedRHSs ( dsGuarded, dsGRHSs, isTrueLHsExpr ) where
import GHC.Prelude
import {-# SOURCE #-} GHC.HsToCore.Expr ( dsLExpr, dsLocalBinds )
import {-# SOURCE #-} GHC.HsToCore.Match ( matchSinglePatVar )
import GHC.Hs
import GHC.Core.Make
import GHC.Core
import GHC.Core.Utils (bindNonRec)
import GHC.HsToCore.Monad
import GHC.HsToCore.Utils
import GHC.HsToCore.Pmc.Types ( Nablas )
import GHC.Core.Type ( Type )
import GHC.Utils.Misc
import GHC.Types.SrcLoc
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Core.Multiplicity
import Control.Monad ( zipWithM )
import Data.List.NonEmpty ( NonEmpty, toList )
dsGuarded :: GRHSs GhcTc (LHsExpr GhcTc) -> Type -> NonEmpty Nablas -> DsM CoreExpr
dsGuarded :: GRHSs GhcTc (LHsExpr GhcTc)
-> Type -> NonEmpty Nablas -> DsM CoreExpr
dsGuarded GRHSs GhcTc (LHsExpr GhcTc)
grhss Type
rhs_ty NonEmpty Nablas
rhss_nablas = do
match_result <- HsMatchContextRn
-> GRHSs GhcTc (LHsExpr GhcTc)
-> Type
-> NonEmpty Nablas
-> DsM (MatchResult CoreExpr)
dsGRHSs HsMatchContextRn
HsMatchContext (GenLocated SrcSpanAnnN Name)
forall fn. HsMatchContext fn
PatBindRhs GRHSs GhcTc (LHsExpr GhcTc)
grhss Type
rhs_ty NonEmpty Nablas
rhss_nablas
error_expr <- mkErrorAppDs nON_EXHAUSTIVE_GUARDS_ERROR_ID rhs_ty
(text "pattern binding")
extractMatchResult match_result error_expr
dsGRHSs :: HsMatchContextRn
-> GRHSs GhcTc (LHsExpr GhcTc)
-> Type
-> NonEmpty Nablas
-> DsM (MatchResult CoreExpr)
dsGRHSs :: HsMatchContextRn
-> GRHSs GhcTc (LHsExpr GhcTc)
-> Type
-> NonEmpty Nablas
-> DsM (MatchResult CoreExpr)
dsGRHSs HsMatchContextRn
hs_ctx (GRHSs XCGRHSs GhcTc (LHsExpr GhcTc)
_ [LGRHS GhcTc (LHsExpr GhcTc)]
grhss HsLocalBinds GhcTc
binds) Type
rhs_ty NonEmpty Nablas
rhss_nablas
= Bool -> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a. HasCallStack => Bool -> a -> a
assert ([GenLocated
EpAnnCO (GRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> Bool
forall (f :: * -> *) a. Foldable f => f a -> Bool
notNull [LGRHS GhcTc (LHsExpr GhcTc)]
[GenLocated
EpAnnCO (GRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
grhss) (DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr))
-> DsM (MatchResult CoreExpr) -> DsM (MatchResult CoreExpr)
forall a b. (a -> b) -> a -> b
$
do { match_results <- Bool
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr]
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr]
forall a. HasCallStack => Bool -> a -> a
assert ([GenLocated
EpAnnCO (GRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LGRHS GhcTc (LHsExpr GhcTc)]
[GenLocated
EpAnnCO (GRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
grhss Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== NonEmpty Nablas -> Int
forall a. NonEmpty a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length NonEmpty Nablas
rhss_nablas) (IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr]
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr])
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr]
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr]
forall a b. (a -> b) -> a -> b
$
(Nablas
-> GenLocated
EpAnnCO (GRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> DsM (MatchResult CoreExpr))
-> [Nablas]
-> [GenLocated
EpAnnCO (GRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult CoreExpr]
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM (HsMatchContextRn
-> Type
-> Nablas
-> LGRHS GhcTc (LHsExpr GhcTc)
-> DsM (MatchResult CoreExpr)
dsGRHS HsMatchContextRn
hs_ctx Type
rhs_ty) (NonEmpty Nablas -> [Nablas]
forall a. NonEmpty a -> [a]
toList NonEmpty Nablas
rhss_nablas) [LGRHS GhcTc (LHsExpr GhcTc)]
[GenLocated
EpAnnCO (GRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
grhss
; nablas <- getPmNablas
; let ds_binds = Nablas -> DsM CoreExpr -> DsM CoreExpr
forall a. Nablas -> DsM a -> DsM a
updPmNablas Nablas
nablas (DsM CoreExpr -> DsM CoreExpr)
-> (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsLocalBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds HsLocalBinds GhcTc
binds
match_result1 = (MatchResult CoreExpr
-> MatchResult CoreExpr -> MatchResult CoreExpr)
-> [MatchResult CoreExpr] -> MatchResult CoreExpr
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 MatchResult CoreExpr
-> MatchResult CoreExpr -> MatchResult CoreExpr
combineMatchResults [MatchResult CoreExpr]
match_results
match_result2 = (CoreExpr -> DsM CoreExpr)
-> MatchResult CoreExpr -> MatchResult CoreExpr
forall a b. (a -> DsM b) -> MatchResult a -> MatchResult b
adjustMatchResultDs CoreExpr -> DsM CoreExpr
ds_binds MatchResult CoreExpr
match_result1
; return match_result2 }
dsGRHS :: HsMatchContextRn -> Type -> Nablas -> LGRHS GhcTc (LHsExpr GhcTc)
-> DsM (MatchResult CoreExpr)
dsGRHS :: HsMatchContextRn
-> Type
-> Nablas
-> LGRHS GhcTc (LHsExpr GhcTc)
-> DsM (MatchResult CoreExpr)
dsGRHS HsMatchContextRn
hs_ctx Type
rhs_ty Nablas
rhs_nablas (L EpAnnCO
_ (GRHS XCGRHS GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
_ [GuardLStmt GhcTc]
guards GenLocated SrcSpanAnnA (HsExpr GhcTc)
rhs))
= [GuardStmt GhcTc]
-> HsMatchContextRn
-> Nablas
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards ((GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> [GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
-> [Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))]
forall a b. (a -> b) -> [a] -> [b]
map GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))
-> Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc))
forall l e. GenLocated l e -> e
unLoc [GuardLStmt GhcTc]
[GenLocated
SrcSpanAnnA (Stmt GhcTc (GenLocated SrcSpanAnnA (HsExpr GhcTc)))]
guards) HsMatchContextRn
hs_ctx Nablas
rhs_nablas LHsExpr GhcTc
GenLocated SrcSpanAnnA (HsExpr GhcTc)
rhs Type
rhs_ty
matchGuards :: [GuardStmt GhcTc]
-> HsMatchContextRn
-> Nablas
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards :: [GuardStmt GhcTc]
-> HsMatchContextRn
-> Nablas
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards [] HsMatchContextRn
_ Nablas
nablas LHsExpr GhcTc
rhs Type
_
= do { core_rhs <- Nablas -> DsM CoreExpr -> DsM CoreExpr
forall a. Nablas -> DsM a -> DsM a
updPmNablas Nablas
nablas (LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
rhs)
; return (cantFailMatchResult core_rhs) }
matchGuards (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
e SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_ : [GuardStmt GhcTc]
stmts) HsMatchContextRn
ctx Nablas
nablas LHsExpr GhcTc
rhs Type
rhs_ty
| Just CoreExpr -> DsM CoreExpr
addTicks <- LHsExpr GhcTc -> Maybe (CoreExpr -> DsM CoreExpr)
isTrueLHsExpr LHsExpr GhcTc
e = do
match_result <- [GuardStmt GhcTc]
-> HsMatchContextRn
-> Nablas
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards [GuardStmt GhcTc]
stmts HsMatchContextRn
ctx Nablas
nablas LHsExpr GhcTc
rhs Type
rhs_ty
return (adjustMatchResultDs addTicks match_result)
matchGuards (BodyStmt XBodyStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LHsExpr GhcTc
expr SyntaxExpr GhcTc
_ SyntaxExpr GhcTc
_ : [GuardStmt GhcTc]
stmts) HsMatchContextRn
ctx Nablas
nablas LHsExpr GhcTc
rhs Type
rhs_ty = do
match_result <- [GuardStmt GhcTc]
-> HsMatchContextRn
-> Nablas
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards [GuardStmt GhcTc]
stmts HsMatchContextRn
ctx Nablas
nablas LHsExpr GhcTc
rhs Type
rhs_ty
pred_expr <- dsLExpr expr
return (mkGuardedMatchResult pred_expr match_result)
matchGuards (LetStmt XLetStmt GhcTc GhcTc (LHsExpr GhcTc)
_ HsLocalBinds GhcTc
binds : [GuardStmt GhcTc]
stmts) HsMatchContextRn
ctx Nablas
nablas LHsExpr GhcTc
rhs Type
rhs_ty = do
match_result <- [GuardStmt GhcTc]
-> HsMatchContextRn
-> Nablas
-> LHsExpr GhcTc
-> Type
-> DsM (MatchResult CoreExpr)
matchGuards [GuardStmt GhcTc]
stmts HsMatchContextRn
ctx Nablas
nablas LHsExpr GhcTc
rhs Type
rhs_ty
return (adjustMatchResultDs (dsLocalBinds binds) match_result)
matchGuards (BindStmt XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
_ LPat GhcTc
pat LHsExpr GhcTc
bind_rhs : [GuardStmt GhcTc]
stmts) HsMatchContextRn
ctx Nablas
nablas LHsExpr GhcTc
rhs Type
rhs_ty = do
let upat :: Pat GhcTc
upat = GenLocated SrcSpanAnnA (Pat GhcTc) -> Pat GhcTc
forall l e. GenLocated l e -> e
unLoc LPat GhcTc
GenLocated SrcSpanAnnA (Pat GhcTc)
pat
match_var <- Type -> Pat GhcTc -> DsM Id
selectMatchVar Type
ManyTy Pat GhcTc
upat
match_result <- matchGuards stmts ctx nablas rhs rhs_ty
core_rhs <- dsLExpr bind_rhs
match_result' <-
matchSinglePatVar match_var (Just core_rhs) (StmtCtxt $ PatGuard ctx)
pat rhs_ty match_result
return $ bindNonRec match_var core_rhs <$> match_result'
matchGuards (LastStmt {} : [GuardStmt GhcTc]
_) HsMatchContextRn
_ Nablas
_ LHsExpr GhcTc
_ Type
_ = String -> DsM (MatchResult CoreExpr)
forall a. HasCallStack => String -> a
panic String
"matchGuards LastStmt"
matchGuards (ParStmt {} : [GuardStmt GhcTc]
_) HsMatchContextRn
_ Nablas
_ LHsExpr GhcTc
_ Type
_ = String -> DsM (MatchResult CoreExpr)
forall a. HasCallStack => String -> a
panic String
"matchGuards ParStmt"
matchGuards (TransStmt {} : [GuardStmt GhcTc]
_) HsMatchContextRn
_ Nablas
_ LHsExpr GhcTc
_ Type
_ = String -> DsM (MatchResult CoreExpr)
forall a. HasCallStack => String -> a
panic String
"matchGuards TransStmt"
matchGuards (RecStmt {} : [GuardStmt GhcTc]
_) HsMatchContextRn
_ Nablas
_ LHsExpr GhcTc
_ Type
_ = String -> DsM (MatchResult CoreExpr)
forall a. HasCallStack => String -> a
panic String
"matchGuards RecStmt"
matchGuards (ApplicativeStmt {} : [GuardStmt GhcTc]
_) HsMatchContextRn
_ Nablas
_ LHsExpr GhcTc
_ Type
_ =
String -> DsM (MatchResult CoreExpr)
forall a. HasCallStack => String -> a
panic String
"matchGuards ApplicativeLastStmt"