{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998


Desugaring expressions.
-}

{-# LANGUAGE CPP, MultiWayIf #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE ViewPatterns #-}

module DsExpr ( dsExpr, dsLExpr, dsLExprNoLP, dsLocalBinds
              , dsValBinds, dsLit, dsSyntaxExpr ) where

#include "HsVersions.h"

import GhcPrelude

import Match
import MatchLit
import DsBinds
import DsGRHSs
import DsListComp
import DsUtils
import DsArrows
import DsMonad
import Check ( checkGuardMatches )
import Name
import NameEnv
import FamInstEnv( topNormaliseType )
import DsMeta
import HsSyn

-- NB: The desugarer, which straddles the source and Core worlds, sometimes
--     needs to see source types
import TcType
import TcEvidence
import TcRnMonad
import TcHsSyn
import Type
import CoreSyn
import CoreUtils
import MkCore

import DynFlags
import CostCentre
import Id
import MkId
import Module
import ConLike
import DataCon
import TysWiredIn
import PrelNames
import BasicTypes
import Maybes
import VarEnv
import SrcLoc
import Util
import Bag
import Outputable
import PatSyn

import Control.Monad

{-
************************************************************************
*                                                                      *
                dsLocalBinds, dsValBinds
*                                                                      *
************************************************************************
-}

dsLocalBinds :: LHsLocalBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds :: LHsLocalBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds (LHsLocalBinds GhcTc -> Located (SrcSpanLess (LHsLocalBinds GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _   (EmptyLocalBinds _))  body :: CoreExpr
body = CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
body
dsLocalBinds (LHsLocalBinds GhcTc -> Located (SrcSpanLess (LHsLocalBinds GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc (HsValBinds _ binds)) body :: CoreExpr
body = SrcSpan -> DsM CoreExpr -> DsM CoreExpr
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
                                                   HsValBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsValBinds HsValBinds GhcTc
binds CoreExpr
body
dsLocalBinds (LHsLocalBinds GhcTc -> Located (SrcSpanLess (LHsLocalBinds GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (HsIPBinds _ binds))    body :: CoreExpr
body = HsIPBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsIPBinds  HsIPBinds GhcTc
binds CoreExpr
body
dsLocalBinds _                                _    = String -> DsM CoreExpr
forall a. String -> a
panic "dsLocalBinds"

-------------------------
-- caller sets location
dsValBinds :: HsValBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsValBinds :: HsValBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsValBinds (XValBindsLR (NValBinds binds _)) body :: CoreExpr
body
  = ((RecFlag, LHsBinds GhcTc) -> CoreExpr -> DsM CoreExpr)
-> CoreExpr -> [(RecFlag, LHsBinds GhcTc)] -> DsM CoreExpr
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m a) -> a -> [b] -> m a
foldrM (RecFlag, LHsBinds GhcTc) -> CoreExpr -> DsM CoreExpr
ds_val_bind CoreExpr
body [(RecFlag, LHsBinds GhcTc)]
binds
dsValBinds (ValBinds {})       _    = String -> DsM CoreExpr
forall a. String -> a
panic "dsValBinds ValBindsIn"

-------------------------
dsIPBinds :: HsIPBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsIPBinds :: HsIPBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsIPBinds (IPBinds ev_binds :: XIPBinds GhcTc
ev_binds ip_binds :: [LIPBind GhcTc]
ip_binds) body :: CoreExpr
body
  = do  { [CoreBind]
ds_binds <- TcEvBinds -> DsM [CoreBind]
dsTcEvBinds XIPBinds GhcTc
TcEvBinds
ev_binds
        ; let inner :: CoreExpr
inner = [CoreBind] -> CoreExpr -> CoreExpr
mkCoreLets [CoreBind]
ds_binds CoreExpr
body
                -- The dict bindings may not be in
                -- dependency order; hence Rec
        ; (LIPBind GhcTc -> CoreExpr -> DsM CoreExpr)
-> CoreExpr -> [LIPBind GhcTc] -> DsM CoreExpr
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m a) -> a -> [b] -> m a
foldrM LIPBind GhcTc -> CoreExpr -> DsM CoreExpr
forall a.
(HasSrcSpan a, SrcSpanLess a ~ IPBind GhcTc) =>
a -> CoreExpr -> DsM CoreExpr
ds_ip_bind CoreExpr
inner [LIPBind GhcTc]
ip_binds }
  where
    ds_ip_bind :: a -> CoreExpr -> DsM CoreExpr
ds_ip_bind (a -> Located (SrcSpanLess a)
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (IPBind _ ~(Right n) e)) body :: CoreExpr
body
      = do CoreExpr
e' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
e
           CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreBind -> CoreExpr -> CoreExpr
forall b. Bind b -> Expr b -> Expr b
Let (CoreBndr -> CoreExpr -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec CoreBndr
IdP GhcTc
n CoreExpr
e') CoreExpr
body)
    ds_ip_bind _ _ = String -> DsM CoreExpr
forall a. String -> a
panic "dsIPBinds"
dsIPBinds (XHsIPBinds _) _ = String -> DsM CoreExpr
forall a. String -> a
panic "dsIPBinds"

-------------------------
-- caller sets location
ds_val_bind :: (RecFlag, LHsBinds GhcTc) -> CoreExpr -> DsM CoreExpr
-- Special case for bindings which bind unlifted variables
-- We need to do a case right away, rather than building
-- a tuple and doing selections.
-- Silently ignore INLINE and SPECIALISE pragmas...
ds_val_bind :: (RecFlag, LHsBinds GhcTc) -> CoreExpr -> DsM CoreExpr
ds_val_bind (NonRecursive, hsbinds :: LHsBinds GhcTc
hsbinds) body :: CoreExpr
body
  | [LHsBindLR GhcTc GhcTc
-> Located (SrcSpanLess (LHsBindLR GhcTc GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc bind :: SrcSpanLess (LHsBindLR GhcTc GhcTc)
bind] <- LHsBinds GhcTc -> [LHsBindLR GhcTc GhcTc]
forall a. Bag a -> [a]
bagToList LHsBinds GhcTc
hsbinds
        -- Non-recursive, non-overloaded bindings only come in ones
        -- ToDo: in some bizarre case it's conceivable that there
        --       could be dict binds in the 'binds'.  (See the notes
        --       below.  Then pattern-match would fail.  Urk.)
  , HsBind GhcTc -> Bool
isUnliftedHsBind SrcSpanLess (LHsBindLR GhcTc GhcTc)
HsBind GhcTc
bind
  = SrcSpan -> DsM CoreExpr -> DsM CoreExpr
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
     -- see Note [Strict binds checks] in DsBinds
    if HsBind GhcTc -> Bool
forall idL idR. HsBindLR idL idR -> Bool
is_polymorphic SrcSpanLess (LHsBindLR GhcTc GhcTc)
HsBind GhcTc
bind
    then SDoc -> DsM CoreExpr
errDsCoreExpr (HsBind GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
poly_bind_err SrcSpanLess (LHsBindLR GhcTc GhcTc)
HsBind GhcTc
bind)
            -- data Ptr a = Ptr Addr#
            -- f x = let p@(Ptr y) = ... in ...
            -- Here the binding for 'p' is polymorphic, but does
            -- not mix with an unlifted binding for 'y'.  You should
            -- use a bang pattern.  Trac #6078.

    else do { Bool
-> IOEnv (Env DsGblEnv DsLclEnv) ()
-> IOEnv (Env DsGblEnv DsLclEnv) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (HsBind GhcTc -> Bool
forall (p :: Pass). HsBind (GhcPass p) -> Bool
looksLazyPatBind SrcSpanLess (LHsBindLR GhcTc GhcTc)
HsBind GhcTc
bind) (IOEnv (Env DsGblEnv DsLclEnv) ()
 -> IOEnv (Env DsGblEnv DsLclEnv) ())
-> IOEnv (Env DsGblEnv DsLclEnv) ()
-> IOEnv (Env DsGblEnv DsLclEnv) ()
forall a b. (a -> b) -> a -> b
$
              WarningFlag -> SDoc -> IOEnv (Env DsGblEnv DsLclEnv) ()
warnIfSetDs WarningFlag
Opt_WarnUnbangedStrictPatterns (HsBind GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
unlifted_must_be_bang SrcSpanLess (LHsBindLR GhcTc GhcTc)
HsBind GhcTc
bind)
        -- Complain about a binding that looks lazy
        --    e.g.    let I# y = x in ...
        -- Remember, in checkStrictBinds we are going to do strict
        -- matching, so (for software engineering reasons) we insist
        -- that the strictness is manifest on each binding
        -- However, lone (unboxed) variables are ok


            ; HsBind GhcTc -> CoreExpr -> DsM CoreExpr
dsUnliftedBind SrcSpanLess (LHsBindLR GhcTc GhcTc)
HsBind GhcTc
bind CoreExpr
body }
  where
    is_polymorphic :: HsBindLR idL idR -> Bool
is_polymorphic (AbsBinds { abs_tvs :: forall idL idR. HsBindLR idL idR -> [CoreBndr]
abs_tvs = [CoreBndr]
tvs, abs_ev_vars :: forall idL idR. HsBindLR idL idR -> [CoreBndr]
abs_ev_vars = [CoreBndr]
evs })
                     = Bool -> Bool
not ([CoreBndr] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [CoreBndr]
tvs Bool -> Bool -> Bool
&& [CoreBndr] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [CoreBndr]
evs)
    is_polymorphic _ = Bool
False

    unlifted_must_be_bang :: a -> SDoc
unlifted_must_be_bang bind :: a
bind
      = SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "Pattern bindings containing unlifted types should use" SDoc -> SDoc -> SDoc
$$
              String -> SDoc
text "an outermost bang pattern:")
           2 (a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
bind)

    poly_bind_err :: a -> SDoc
poly_bind_err bind :: a
bind
      = SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "You can't mix polymorphic and unlifted bindings:")
           2 (a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
bind) SDoc -> SDoc -> SDoc
$$
        String -> SDoc
text "Probable fix: add a type signature"

ds_val_bind (is_rec :: RecFlag
is_rec, binds :: LHsBinds GhcTc
binds) _body :: CoreExpr
_body
  | (LHsBindLR GhcTc GhcTc -> Bool) -> LHsBinds GhcTc -> Bool
forall a. (a -> Bool) -> Bag a -> Bool
anyBag (HsBind GhcTc -> Bool
isUnliftedHsBind (HsBind GhcTc -> Bool)
-> (LHsBindLR GhcTc GhcTc -> HsBind GhcTc)
-> LHsBindLR GhcTc GhcTc
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LHsBindLR GhcTc GhcTc -> HsBind GhcTc
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc) LHsBinds GhcTc
binds  -- see Note [Strict binds checks] in DsBinds
  = ASSERT( isRec is_rec )
    SDoc -> DsM CoreExpr
errDsCoreExpr (SDoc -> DsM CoreExpr) -> SDoc -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
    SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "Recursive bindings for unlifted types aren't allowed:")
       2 ([SDoc] -> SDoc
vcat ((LHsBindLR GhcTc GhcTc -> SDoc)
-> [LHsBindLR GhcTc GhcTc] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map LHsBindLR GhcTc GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr (LHsBinds GhcTc -> [LHsBindLR GhcTc GhcTc]
forall a. Bag a -> [a]
bagToList LHsBinds GhcTc
binds)))

-- Ordinary case for bindings; none should be unlifted
ds_val_bind (is_rec :: RecFlag
is_rec, binds :: LHsBinds GhcTc
binds) body :: CoreExpr
body
  = do  { MASSERT( isRec is_rec || isSingletonBag binds )
               -- we should never produce a non-recursive list of multiple binds

        ; (force_vars :: [CoreBndr]
force_vars,prs :: [(CoreBndr, CoreExpr)]
prs) <- LHsBinds GhcTc -> DsM ([CoreBndr], [(CoreBndr, CoreExpr)])
dsLHsBinds LHsBinds GhcTc
binds
        ; let body' :: CoreExpr
body' = (CoreBndr -> CoreExpr -> CoreExpr)
-> CoreExpr -> [CoreBndr] -> CoreExpr
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr CoreBndr -> CoreExpr -> CoreExpr
seqVar CoreExpr
body [CoreBndr]
force_vars
        ; ASSERT2( not (any (isUnliftedType . idType . fst) prs), ppr is_rec $$ ppr binds )
          case [(CoreBndr, CoreExpr)]
prs of
            [] -> CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
body
            _  -> CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreBind -> CoreExpr -> CoreExpr
forall b. Bind b -> Expr b -> Expr b
Let ([(CoreBndr, CoreExpr)] -> CoreBind
forall b. [(b, Expr b)] -> Bind b
Rec [(CoreBndr, CoreExpr)]
prs) CoreExpr
body') }
        -- Use a Rec regardless of is_rec.
        -- Why? Because it allows the binds to be all
        -- mixed up, which is what happens in one rare case
        -- Namely, for an AbsBind with no tyvars and no dicts,
        --         but which does have dictionary bindings.
        -- See notes with TcSimplify.inferLoop [NO TYVARS]
        -- It turned out that wrapping a Rec here was the easiest solution
        --
        -- NB The previous case dealt with unlifted bindings, so we
        --    only have to deal with lifted ones now; so Rec is ok

------------------
dsUnliftedBind :: HsBind GhcTc -> CoreExpr -> DsM CoreExpr
dsUnliftedBind :: HsBind GhcTc -> CoreExpr -> DsM CoreExpr
dsUnliftedBind (AbsBinds { abs_tvs :: forall idL idR. HsBindLR idL idR -> [CoreBndr]
abs_tvs = [], abs_ev_vars :: forall idL idR. HsBindLR idL idR -> [CoreBndr]
abs_ev_vars = []
               , abs_exports :: forall idL idR. HsBindLR idL idR -> [ABExport idL]
abs_exports = [ABExport GhcTc]
exports
               , abs_ev_binds :: forall idL idR. HsBindLR idL idR -> [TcEvBinds]
abs_ev_binds = [TcEvBinds]
ev_binds
               , abs_binds :: forall idL idR. HsBindLR idL idR -> LHsBinds idL
abs_binds = LHsBinds GhcTc
lbinds }) body :: CoreExpr
body
  = do { let body1 :: CoreExpr
body1 = (ABExport GhcTc -> CoreExpr -> CoreExpr)
-> CoreExpr -> [ABExport GhcTc] -> CoreExpr
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ABExport GhcTc -> CoreExpr -> CoreExpr
forall p. (IdP p ~ CoreBndr) => ABExport p -> CoreExpr -> CoreExpr
bind_export CoreExpr
body [ABExport GhcTc]
exports
             bind_export :: ABExport p -> CoreExpr -> CoreExpr
bind_export export :: ABExport p
export b :: CoreExpr
b = CoreBndr -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec (ABExport p -> IdP p
forall p. ABExport p -> IdP p
abe_poly ABExport p
export) (CoreBndr -> CoreExpr
forall b. CoreBndr -> Expr b
Var (ABExport p -> IdP p
forall p. ABExport p -> IdP p
abe_mono ABExport p
export)) CoreExpr
b
       ; CoreExpr
body2 <- (CoreExpr -> LHsBindLR GhcTc GhcTc -> DsM CoreExpr)
-> CoreExpr -> LHsBinds GhcTc -> DsM CoreExpr
forall (m :: * -> *) b a.
Monad m =>
(b -> a -> m b) -> b -> Bag a -> m b
foldlBagM (\body :: CoreExpr
body lbind :: LHsBindLR GhcTc GhcTc
lbind -> HsBind GhcTc -> CoreExpr -> DsM CoreExpr
dsUnliftedBind (LHsBindLR GhcTc GhcTc -> SrcSpanLess (LHsBindLR GhcTc GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc LHsBindLR GhcTc GhcTc
lbind) CoreExpr
body)
                            CoreExpr
body1 LHsBinds GhcTc
lbinds
       ; [CoreBind]
ds_binds <- [TcEvBinds] -> DsM [CoreBind]
dsTcEvBinds_s [TcEvBinds]
ev_binds
       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return ([CoreBind] -> CoreExpr -> CoreExpr
mkCoreLets [CoreBind]
ds_binds CoreExpr
body2) }

dsUnliftedBind (FunBind { fun_id :: forall idL idR. HsBindLR idL idR -> Located (IdP idL)
fun_id = (Located (IdP GhcTc) -> Located (SrcSpanLess (Located CoreBndr))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L l :: SrcSpan
l fun :: SrcSpanLess (Located CoreBndr)
fun)
                        , fun_matches :: forall idL idR. HsBindLR idL idR -> MatchGroup idR (LHsExpr idR)
fun_matches = MatchGroup GhcTc (LHsExpr GhcTc)
matches
                        , fun_co_fn :: forall idL idR. HsBindLR idL idR -> HsWrapper
fun_co_fn = HsWrapper
co_fn
                        , fun_tick :: forall idL idR. HsBindLR idL idR -> [Tickish CoreBndr]
fun_tick = [Tickish CoreBndr]
tick }) body :: CoreExpr
body
               -- Can't be a bang pattern (that looks like a PatBind)
               -- so must be simply unboxed
  = do { (args :: [CoreBndr]
args, rhs :: CoreExpr
rhs) <- HsMatchContext Name
-> Maybe (LHsExpr GhcTc)
-> MatchGroup GhcTc (LHsExpr GhcTc)
-> DsM ([CoreBndr], CoreExpr)
matchWrapper (Located Name -> HsMatchContext Name
forall id. Located id -> HsMatchContext id
mkPrefixFunRhs (SrcSpan -> SrcSpanLess (Located Name) -> Located Name
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
l (SrcSpanLess (Located Name) -> Located Name)
-> SrcSpanLess (Located Name) -> Located Name
forall a b. (a -> b) -> a -> b
$ CoreBndr -> Name
idName SrcSpanLess (Located CoreBndr)
CoreBndr
fun))
                                     Maybe (LHsExpr GhcTc)
forall a. Maybe a
Nothing MatchGroup GhcTc (LHsExpr GhcTc)
matches
       ; MASSERT( null args ) -- Functions aren't lifted
       ; MASSERT( isIdHsWrapper co_fn )
       ; let rhs' :: CoreExpr
rhs' = [Tickish CoreBndr] -> CoreExpr -> CoreExpr
mkOptTickBox [Tickish CoreBndr]
tick CoreExpr
rhs
       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreBndr -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec SrcSpanLess (Located CoreBndr)
CoreBndr
fun CoreExpr
rhs' CoreExpr
body) }

dsUnliftedBind (PatBind {pat_lhs :: forall idL idR. HsBindLR idL idR -> LPat idL
pat_lhs = LPat GhcTc
pat, pat_rhs :: forall idL idR. HsBindLR idL idR -> GRHSs idR (LHsExpr idR)
pat_rhs = GRHSs GhcTc (LHsExpr GhcTc)
grhss
                        , pat_ext :: forall idL idR. HsBindLR idL idR -> XPatBind idL idR
pat_ext = NPatBindTc _ ty }) body :: CoreExpr
body
  =     -- let C x# y# = rhs in body
        -- ==> case rhs of C x# y# -> body
    do { CoreExpr
rhs <- GRHSs GhcTc (LHsExpr GhcTc) -> Type -> DsM CoreExpr
dsGuarded GRHSs GhcTc (LHsExpr GhcTc)
grhss Type
ty
       ; HsMatchContext Name
-> GRHSs GhcTc (LHsExpr GhcTc) -> IOEnv (Env DsGblEnv DsLclEnv) ()
checkGuardMatches HsMatchContext Name
forall id. HsMatchContext id
PatBindGuards GRHSs GhcTc (LHsExpr GhcTc)
grhss
       ; let upat :: SrcSpanLess (LPat GhcTc)
upat = LPat GhcTc -> SrcSpanLess (LPat GhcTc)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc LPat GhcTc
pat
             eqn :: EquationInfo
eqn = EqnInfo :: [LPat GhcTc] -> Origin -> MatchResult -> EquationInfo
EqnInfo { eqn_pats :: [LPat GhcTc]
eqn_pats = [SrcSpanLess (LPat GhcTc)
LPat GhcTc
upat],
                             eqn_orig :: Origin
eqn_orig = Origin
FromSource,
                             eqn_rhs :: MatchResult
eqn_rhs = CoreExpr -> MatchResult
cantFailMatchResult CoreExpr
body }
       ; CoreBndr
var    <- LPat GhcTc -> DsM CoreBndr
selectMatchVar SrcSpanLess (LPat GhcTc)
LPat GhcTc
upat
       ; CoreExpr
result <- HsMatchContext Name
-> [CoreBndr] -> [EquationInfo] -> Type -> DsM CoreExpr
matchEquations HsMatchContext Name
forall id. HsMatchContext id
PatBindRhs [CoreBndr
var] [EquationInfo
eqn] (CoreExpr -> Type
exprType CoreExpr
body)
       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreBndr -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec CoreBndr
var CoreExpr
rhs CoreExpr
result) }

dsUnliftedBind bind :: HsBind GhcTc
bind body :: CoreExpr
body = String -> SDoc -> DsM CoreExpr
forall a. HasCallStack => String -> SDoc -> a
pprPanic "dsLet: unlifted" (HsBind GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsBind GhcTc
bind SDoc -> SDoc -> SDoc
$$ CoreExpr -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoreExpr
body)

{-
************************************************************************
*                                                                      *
\subsection[DsExpr-vars-and-cons]{Variables, constructors, literals}
*                                                                      *
************************************************************************
-}

dsLExpr :: LHsExpr GhcTc -> DsM CoreExpr

dsLExpr :: LHsExpr GhcTc -> DsM CoreExpr
dsLExpr (LHsExpr GhcTc -> Located (SrcSpanLess (LHsExpr GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc e :: SrcSpanLess (LHsExpr GhcTc)
e)
  = SrcSpan -> DsM CoreExpr -> DsM CoreExpr
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
    do { CoreExpr
core_expr <- HsExpr GhcTc -> DsM CoreExpr
dsExpr SrcSpanLess (LHsExpr GhcTc)
HsExpr GhcTc
e
   -- uncomment this check to test the hsExprType function in TcHsSyn
   --    ; MASSERT2( exprType core_expr `eqType` hsExprType e
   --              , ppr e <+> dcolon <+> ppr (hsExprType e) $$
   --                ppr core_expr <+> dcolon <+> ppr (exprType core_expr) )
       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
core_expr }

-- | Variant of 'dsLExpr' that ensures that the result is not levity
-- polymorphic. This should be used when the resulting expression will
-- be an argument to some other function.
-- See Note [Levity polymorphism checking] in DsMonad
-- See Note [Levity polymorphism invariants] in CoreSyn
dsLExprNoLP :: LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP :: LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP (LHsExpr GhcTc -> Located (SrcSpanLess (LHsExpr GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc e :: SrcSpanLess (LHsExpr GhcTc)
e)
  = SrcSpan -> DsM CoreExpr -> DsM CoreExpr
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
    do { CoreExpr
e' <- HsExpr GhcTc -> DsM CoreExpr
dsExpr SrcSpanLess (LHsExpr GhcTc)
HsExpr GhcTc
e
       ; CoreExpr -> SDoc -> IOEnv (Env DsGblEnv DsLclEnv) ()
dsNoLevPolyExpr CoreExpr
e' (String -> SDoc
text "In the type of expression:" SDoc -> SDoc -> SDoc
<+> HsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr SrcSpanLess (LHsExpr GhcTc)
HsExpr GhcTc
e)
       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
e' }

dsExpr :: HsExpr GhcTc -> DsM CoreExpr
dsExpr :: HsExpr GhcTc -> DsM CoreExpr
dsExpr = Bool -> HsExpr GhcTc -> DsM CoreExpr
ds_expr Bool
False

ds_expr :: Bool   -- are we directly inside an HsWrap?
                  -- See Wrinkle in Note [Detecting forced eta expansion]
        -> HsExpr GhcTc -> DsM CoreExpr
ds_expr :: Bool -> HsExpr GhcTc -> DsM CoreExpr
ds_expr _ (HsPar _ e :: LHsExpr GhcTc
e)            = LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
e
ds_expr _ (ExprWithTySig _ e :: LHsExpr GhcTc
e _)  = LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
e
ds_expr w :: Bool
w (HsVar _ (Located (IdP GhcTc) -> Located (SrcSpanLess (Located CoreBndr))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ var :: SrcSpanLess (Located CoreBndr)
var)) = Bool -> CoreBndr -> DsM CoreExpr
dsHsVar Bool
w SrcSpanLess (Located CoreBndr)
CoreBndr
var
ds_expr _ (HsUnboundVar {})      = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr: HsUnboundVar" -- Typechecker eliminates them
ds_expr w :: Bool
w (HsConLikeOut _ con :: ConLike
con)   = Bool -> ConLike -> DsM CoreExpr
dsConLike Bool
w ConLike
con
ds_expr _ (HsIPVar {})           = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr: HsIPVar"
ds_expr _ (HsOverLabel{})        = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr: HsOverLabel"

ds_expr _ (HsLit _ lit :: HsLit GhcTc
lit)
  = do { HsLit GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) ()
warnAboutOverflowedLit HsLit GhcTc
lit
       ; HsLit GhcRn -> DsM CoreExpr
dsLit (HsLit GhcTc -> HsLit GhcRn
forall a b. ConvertIdX a b => HsLit a -> HsLit b
convertLit HsLit GhcTc
lit) }

ds_expr _ (HsOverLit _ lit :: HsOverLit GhcTc
lit)
  = do { HsOverLit GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) ()
warnAboutOverflowedOverLit HsOverLit GhcTc
lit
       ; HsOverLit GhcTc -> DsM CoreExpr
dsOverLit HsOverLit GhcTc
lit }

ds_expr _ (HsWrap _ co_fn :: HsWrapper
co_fn e :: HsExpr GhcTc
e)
  = do { CoreExpr
e' <- Bool -> HsExpr GhcTc -> DsM CoreExpr
ds_expr Bool
True HsExpr GhcTc
e    -- This is the one place where we recurse to
                                 -- ds_expr (passing True), rather than dsExpr
       ; CoreExpr -> CoreExpr
wrap' <- HsWrapper -> DsM (CoreExpr -> CoreExpr)
dsHsWrapper HsWrapper
co_fn
       ; DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       ; let wrapped_e :: CoreExpr
wrapped_e = CoreExpr -> CoreExpr
wrap' CoreExpr
e'
             wrapped_ty :: Type
wrapped_ty = CoreExpr -> Type
exprType CoreExpr
wrapped_e
       ; HsExpr GhcTc -> Type -> IOEnv (Env DsGblEnv DsLclEnv) ()
checkForcedEtaExpansion HsExpr GhcTc
e Type
wrapped_ty -- See Note [Detecting forced eta expansion]
       ; DynFlags -> CoreExpr -> Type -> IOEnv (Env DsGblEnv DsLclEnv) ()
warnAboutIdentities DynFlags
dflags CoreExpr
e' Type
wrapped_ty
       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
wrapped_e }

ds_expr _ (NegApp _ (LHsExpr GhcTc -> Located (SrcSpanLess (LHsExpr GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc
                      (HsOverLit _ lit@(OverLit { ol_val = HsIntegral i})))
                  neg_expr :: SyntaxExpr GhcTc
neg_expr)
  = do { CoreExpr
expr' <- SrcSpan -> DsM CoreExpr -> DsM CoreExpr
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ do
          { HsOverLit GhcTc -> IOEnv (Env DsGblEnv DsLclEnv) ()
warnAboutOverflowedOverLit
              (HsOverLit GhcTc
lit { ol_val :: OverLitVal
ol_val = IntegralLit -> OverLitVal
HsIntegral (IntegralLit -> IntegralLit
negateIntegralLit IntegralLit
i) })
          ; HsOverLit GhcTc -> DsM CoreExpr
dsOverLit HsOverLit GhcTc
lit }
       ; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
neg_expr [CoreExpr
expr'] }

ds_expr _ (NegApp _ expr :: LHsExpr GhcTc
expr neg_expr :: SyntaxExpr GhcTc
neg_expr)
  = do { CoreExpr
expr' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
expr
       ; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
neg_expr [CoreExpr
expr'] }

ds_expr _ (HsLam _ a_Match :: MatchGroup GhcTc (LHsExpr GhcTc)
a_Match)
  = ([CoreBndr] -> CoreExpr -> CoreExpr)
-> ([CoreBndr], CoreExpr) -> CoreExpr
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [CoreBndr] -> CoreExpr -> CoreExpr
forall b. [b] -> Expr b -> Expr b
mkLams (([CoreBndr], CoreExpr) -> CoreExpr)
-> DsM ([CoreBndr], CoreExpr) -> DsM CoreExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HsMatchContext Name
-> Maybe (LHsExpr GhcTc)
-> MatchGroup GhcTc (LHsExpr GhcTc)
-> DsM ([CoreBndr], CoreExpr)
matchWrapper HsMatchContext Name
forall id. HsMatchContext id
LambdaExpr Maybe (LHsExpr GhcTc)
forall a. Maybe a
Nothing MatchGroup GhcTc (LHsExpr GhcTc)
a_Match

ds_expr _ (HsLamCase _ matches :: MatchGroup GhcTc (LHsExpr GhcTc)
matches)
  = do { ([discrim_var :: CoreBndr
discrim_var], matching_code :: CoreExpr
matching_code) <- HsMatchContext Name
-> Maybe (LHsExpr GhcTc)
-> MatchGroup GhcTc (LHsExpr GhcTc)
-> DsM ([CoreBndr], CoreExpr)
matchWrapper HsMatchContext Name
forall id. HsMatchContext id
CaseAlt Maybe (LHsExpr GhcTc)
forall a. Maybe a
Nothing MatchGroup GhcTc (LHsExpr GhcTc)
matches
       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ CoreBndr -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam CoreBndr
discrim_var CoreExpr
matching_code }

ds_expr _ e :: HsExpr GhcTc
e@(HsApp _ fun :: LHsExpr GhcTc
fun arg :: LHsExpr GhcTc
arg)
  = do { CoreExpr
fun' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
fun
       ; DsM CoreExpr -> (CoreExpr -> CoreExpr) -> DsM CoreExpr
forall a. DsM a -> (a -> CoreExpr) -> DsM CoreExpr
dsWhenNoErrs (LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
arg)
                      (\arg' :: CoreExpr
arg' -> SDoc -> CoreExpr -> CoreExpr -> CoreExpr
mkCoreAppDs (String -> SDoc
text "HsApp" SDoc -> SDoc -> SDoc
<+> HsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcTc
e) CoreExpr
fun' CoreExpr
arg') }

ds_expr _ (HsAppType _ e :: LHsExpr GhcTc
e _)
    -- ignore type arguments here; they're in the wrappers instead at this point
  = LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
e

{-
Note [Desugaring vars]
~~~~~~~~~~~~~~~~~~~~~~
In one situation we can get a *coercion* variable in a HsVar, namely
the support method for an equality superclass:
   class (a~b) => C a b where ...
   instance (blah) => C (T a) (T b) where ..
Then we get
   $dfCT :: forall ab. blah => C (T a) (T b)
   $dfCT ab blah = MkC ($c$p1C a blah) ($cop a blah)

   $c$p1C :: forall ab. blah => (T a ~ T b)
   $c$p1C ab blah = let ...; g :: T a ~ T b = ... } in g

That 'g' in the 'in' part is an evidence variable, and when
converting to core it must become a CO.

Operator sections.  At first it looks as if we can convert
\begin{verbatim}
        (expr op)
\end{verbatim}
to
\begin{verbatim}
        \x -> op expr x
\end{verbatim}

But no!  expr might be a redex, and we can lose laziness badly this
way.  Consider
\begin{verbatim}
        map (expr op) xs
\end{verbatim}
for example.  So we convert instead to
\begin{verbatim}
        let y = expr in \x -> op y x
\end{verbatim}
If \tr{expr} is actually just a variable, say, then the simplifier
will sort it out.
-}

ds_expr _ e :: HsExpr GhcTc
e@(OpApp _ e1 :: LHsExpr GhcTc
e1 op :: LHsExpr GhcTc
op e2 :: LHsExpr GhcTc
e2)
  = -- for the type of y, we need the type of op's 2nd argument
    do { CoreExpr
op' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
op
       ; DsM [CoreExpr] -> ([CoreExpr] -> CoreExpr) -> DsM CoreExpr
forall a. DsM a -> (a -> CoreExpr) -> DsM CoreExpr
dsWhenNoErrs ((LHsExpr GhcTc -> DsM CoreExpr)
-> [LHsExpr GhcTc] -> DsM [CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP [LHsExpr GhcTc
e1, LHsExpr GhcTc
e2])
                      (\exprs' :: [CoreExpr]
exprs' -> SDoc -> CoreExpr -> [CoreExpr] -> CoreExpr
mkCoreAppsDs (String -> SDoc
text "opapp" SDoc -> SDoc -> SDoc
<+> HsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcTc
e) CoreExpr
op' [CoreExpr]
exprs') }

ds_expr _ (SectionL _ expr :: LHsExpr GhcTc
expr op :: LHsExpr GhcTc
op)       -- Desugar (e !) to ((!) e)
  = do { CoreExpr
op' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
op
       ; DsM CoreExpr -> (CoreExpr -> CoreExpr) -> DsM CoreExpr
forall a. DsM a -> (a -> CoreExpr) -> DsM CoreExpr
dsWhenNoErrs (LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
expr)
                      (\expr' :: CoreExpr
expr' -> SDoc -> CoreExpr -> CoreExpr -> CoreExpr
mkCoreAppDs (String -> SDoc
text "sectionl" SDoc -> SDoc -> SDoc
<+> LHsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcTc
expr) CoreExpr
op' CoreExpr
expr') }

-- dsLExpr (SectionR op expr)   -- \ x -> op x expr
ds_expr _ e :: HsExpr GhcTc
e@(SectionR _ op :: LHsExpr GhcTc
op expr :: LHsExpr GhcTc
expr) = do
    CoreExpr
core_op <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
op
    -- for the type of x, we need the type of op's 2nd argument
    let (x_ty :: Type
x_ty:y_ty :: Type
y_ty:_, _) = Type -> ([Type], Type)
splitFunTys (CoreExpr -> Type
exprType CoreExpr
core_op)
        -- See comment with SectionL
    CoreExpr
y_core <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
expr
    DsM [CoreBndr] -> ([CoreBndr] -> CoreExpr) -> DsM CoreExpr
forall a. DsM a -> (a -> CoreExpr) -> DsM CoreExpr
dsWhenNoErrs ((Type -> DsM CoreBndr) -> [Type] -> DsM [CoreBndr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM CoreBndr
newSysLocalDsNoLP [Type
x_ty, Type
y_ty])
                 (\[x_id :: CoreBndr
x_id, y_id :: CoreBndr
y_id] -> CoreBndr -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec CoreBndr
y_id CoreExpr
y_core (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
                                   CoreBndr -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam CoreBndr
x_id (SDoc -> CoreExpr -> [CoreExpr] -> CoreExpr
mkCoreAppsDs (String -> SDoc
text "sectionr" SDoc -> SDoc -> SDoc
<+> HsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcTc
e)
                                                          CoreExpr
core_op [CoreBndr -> CoreExpr
forall b. CoreBndr -> Expr b
Var CoreBndr
x_id, CoreBndr -> CoreExpr
forall b. CoreBndr -> Expr b
Var CoreBndr
y_id]))

ds_expr _ (ExplicitTuple _ tup_args :: [LHsTupArg GhcTc]
tup_args boxity :: Boxity
boxity)
  = do { let go :: ([CoreBndr], [CoreExpr])
-> a -> IOEnv (Env DsGblEnv DsLclEnv) ([CoreBndr], [CoreExpr])
go (lam_vars :: [CoreBndr]
lam_vars, args :: [CoreExpr]
args) (a -> Located (SrcSpanLess a)
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (Missing ty))
                    -- For every missing expression, we need
                    -- another lambda in the desugaring.
               = do { CoreBndr
lam_var <- Type -> DsM CoreBndr
newSysLocalDsNoLP Type
XMissing GhcTc
ty
                    ; ([CoreBndr], [CoreExpr])
-> IOEnv (Env DsGblEnv DsLclEnv) ([CoreBndr], [CoreExpr])
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreBndr
lam_var CoreBndr -> [CoreBndr] -> [CoreBndr]
forall a. a -> [a] -> [a]
: [CoreBndr]
lam_vars, CoreBndr -> CoreExpr
forall b. CoreBndr -> Expr b
Var CoreBndr
lam_var CoreExpr -> [CoreExpr] -> [CoreExpr]
forall a. a -> [a] -> [a]
: [CoreExpr]
args) }
             go (lam_vars :: [CoreBndr]
lam_vars, args :: [CoreExpr]
args) (a -> Located (SrcSpanLess a)
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ (Present _ expr))
                    -- Expressions that are present don't generate
                    -- lambdas, just arguments.
               = do { CoreExpr
core_expr <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
expr
                    ; ([CoreBndr], [CoreExpr])
-> IOEnv (Env DsGblEnv DsLclEnv) ([CoreBndr], [CoreExpr])
forall (m :: * -> *) a. Monad m => a -> m a
return ([CoreBndr]
lam_vars, CoreExpr
core_expr CoreExpr -> [CoreExpr] -> [CoreExpr]
forall a. a -> [a] -> [a]
: [CoreExpr]
args) }
             go _ _ = String -> IOEnv (Env DsGblEnv DsLclEnv) ([CoreBndr], [CoreExpr])
forall a. String -> a
panic "ds_expr"

       ; IOEnv (Env DsGblEnv DsLclEnv) ([CoreBndr], [CoreExpr])
-> (([CoreBndr], [CoreExpr]) -> CoreExpr) -> DsM CoreExpr
forall a. DsM a -> (a -> CoreExpr) -> DsM CoreExpr
dsWhenNoErrs ((([CoreBndr], [CoreExpr])
 -> LHsTupArg GhcTc
 -> IOEnv (Env DsGblEnv DsLclEnv) ([CoreBndr], [CoreExpr]))
-> ([CoreBndr], [CoreExpr])
-> [LHsTupArg GhcTc]
-> IOEnv (Env DsGblEnv DsLclEnv) ([CoreBndr], [CoreExpr])
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM ([CoreBndr], [CoreExpr])
-> LHsTupArg GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) ([CoreBndr], [CoreExpr])
forall a.
(HasSrcSpan a, SrcSpanLess a ~ HsTupArg GhcTc) =>
([CoreBndr], [CoreExpr])
-> a -> IOEnv (Env DsGblEnv DsLclEnv) ([CoreBndr], [CoreExpr])
go ([], []) ([LHsTupArg GhcTc] -> [LHsTupArg GhcTc]
forall a. [a] -> [a]
reverse [LHsTupArg GhcTc]
tup_args))
                -- The reverse is because foldM goes left-to-right
                      (\(lam_vars :: [CoreBndr]
lam_vars, args :: [CoreExpr]
args) -> [CoreBndr] -> CoreExpr -> CoreExpr
mkCoreLams [CoreBndr]
lam_vars (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
                                            Boxity -> [CoreExpr] -> CoreExpr
mkCoreTupBoxity Boxity
boxity [CoreExpr]
args) }

ds_expr _ (ExplicitSum types :: XExplicitSum GhcTc
types alt :: Int
alt arity :: Int
arity expr :: LHsExpr GhcTc
expr)
  = do { DsM CoreExpr -> (CoreExpr -> CoreExpr) -> DsM CoreExpr
forall a. DsM a -> (a -> CoreExpr) -> DsM CoreExpr
dsWhenNoErrs (LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
expr)
                      (\core_expr :: CoreExpr
core_expr -> DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps (Int -> Int -> DataCon
sumDataCon Int
alt Int
arity)
                                     ((Type -> CoreExpr) -> [Type] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map (Type -> CoreExpr
forall b. Type -> Expr b
Type (Type -> CoreExpr) -> (Type -> Type) -> Type -> CoreExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasDebugCallStack => Type -> Type
Type -> Type
getRuntimeRep) [Type]
XExplicitSum GhcTc
types [CoreExpr] -> [CoreExpr] -> [CoreExpr]
forall a. [a] -> [a] -> [a]
++
                                      (Type -> CoreExpr) -> [Type] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map Type -> CoreExpr
forall b. Type -> Expr b
Type [Type]
XExplicitSum GhcTc
types [CoreExpr] -> [CoreExpr] -> [CoreExpr]
forall a. [a] -> [a] -> [a]
++
                                      [CoreExpr
core_expr]) ) }

ds_expr _ (HsSCC _ _ cc :: StringLiteral
cc expr :: LHsExpr GhcTc
expr@(LHsExpr GhcTc -> Located (SrcSpanLess (LHsExpr GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc _)) = do
    DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
    if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_SccProfilingOn DynFlags
dflags
      then do
        Module
mod_name <- IOEnv (Env DsGblEnv DsLclEnv) Module
forall (m :: * -> *). HasModule m => m Module
getModule
        Bool
count <- GeneralFlag -> TcRnIf DsGblEnv DsLclEnv Bool
forall gbl lcl. GeneralFlag -> TcRnIf gbl lcl Bool
goptM GeneralFlag
Opt_ProfCountEntries
        let nm :: FastString
nm = StringLiteral -> FastString
sl_fs StringLiteral
cc
        CCFlavour
flavour <- CostCentreIndex -> CCFlavour
ExprCC (CostCentreIndex -> CCFlavour)
-> IOEnv (Env DsGblEnv DsLclEnv) CostCentreIndex
-> IOEnv (Env DsGblEnv DsLclEnv) CCFlavour
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FastString -> IOEnv (Env DsGblEnv DsLclEnv) CostCentreIndex
forall gbl lcl.
ContainsCostCentreState gbl =>
FastString -> TcRnIf gbl lcl CostCentreIndex
getCCIndexM FastString
nm
        Tickish CoreBndr -> CoreExpr -> CoreExpr
forall b. Tickish CoreBndr -> Expr b -> Expr b
Tick (CostCentre -> Bool -> Bool -> Tickish CoreBndr
forall id. CostCentre -> Bool -> Bool -> Tickish id
ProfNote (FastString -> Module -> SrcSpan -> CCFlavour -> CostCentre
mkUserCC FastString
nm Module
mod_name SrcSpan
loc CCFlavour
flavour) Bool
count Bool
True)
               (CoreExpr -> CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
expr
      else LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
expr

ds_expr _ (HsCoreAnn _ _ _ expr :: LHsExpr GhcTc
expr)
  = LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
expr

ds_expr _ (HsCase _ discrim :: LHsExpr GhcTc
discrim matches :: MatchGroup GhcTc (LHsExpr GhcTc)
matches)
  = do { CoreExpr
core_discrim <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
discrim
       ; ([discrim_var :: CoreBndr
discrim_var], matching_code :: CoreExpr
matching_code) <- HsMatchContext Name
-> Maybe (LHsExpr GhcTc)
-> MatchGroup GhcTc (LHsExpr GhcTc)
-> DsM ([CoreBndr], CoreExpr)
matchWrapper HsMatchContext Name
forall id. HsMatchContext id
CaseAlt (LHsExpr GhcTc -> Maybe (LHsExpr GhcTc)
forall a. a -> Maybe a
Just LHsExpr GhcTc
discrim) MatchGroup GhcTc (LHsExpr GhcTc)
matches
       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreBndr -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec CoreBndr
discrim_var CoreExpr
core_discrim CoreExpr
matching_code) }

-- Pepe: The binds are in scope in the body but NOT in the binding group
--       This is to avoid silliness in breakpoints
ds_expr _ (HsLet _ binds :: LHsLocalBinds GhcTc
binds body :: LHsExpr GhcTc
body) = do
    CoreExpr
body' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
body
    LHsLocalBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds LHsLocalBinds GhcTc
binds CoreExpr
body'

-- We need the `ListComp' form to use `deListComp' (rather than the "do" form)
-- because the interpretation of `stmts' depends on what sort of thing it is.
--
ds_expr _ (HsDo res_ty :: XDo GhcTc
res_ty ListComp (Located [ExprLStmt GhcTc]
-> Located (SrcSpanLess (Located [ExprLStmt GhcTc]))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ stmts :: SrcSpanLess (Located [ExprLStmt GhcTc])
stmts)) = [ExprLStmt GhcTc] -> Type -> DsM CoreExpr
dsListComp [ExprLStmt GhcTc]
SrcSpanLess (Located [ExprLStmt GhcTc])
stmts Type
XDo GhcTc
res_ty
ds_expr _ (HsDo _ DoExpr        (Located [ExprLStmt GhcTc]
-> Located (SrcSpanLess (Located [ExprLStmt GhcTc]))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ stmts :: SrcSpanLess (Located [ExprLStmt GhcTc])
stmts)) = [ExprLStmt GhcTc] -> DsM CoreExpr
dsDo [ExprLStmt GhcTc]
SrcSpanLess (Located [ExprLStmt GhcTc])
stmts
ds_expr _ (HsDo _ GhciStmtCtxt  (Located [ExprLStmt GhcTc]
-> Located (SrcSpanLess (Located [ExprLStmt GhcTc]))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ stmts :: SrcSpanLess (Located [ExprLStmt GhcTc])
stmts)) = [ExprLStmt GhcTc] -> DsM CoreExpr
dsDo [ExprLStmt GhcTc]
SrcSpanLess (Located [ExprLStmt GhcTc])
stmts
ds_expr _ (HsDo _ MDoExpr       (Located [ExprLStmt GhcTc]
-> Located (SrcSpanLess (Located [ExprLStmt GhcTc]))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ stmts :: SrcSpanLess (Located [ExprLStmt GhcTc])
stmts)) = [ExprLStmt GhcTc] -> DsM CoreExpr
dsDo [ExprLStmt GhcTc]
SrcSpanLess (Located [ExprLStmt GhcTc])
stmts
ds_expr _ (HsDo _ MonadComp     (Located [ExprLStmt GhcTc]
-> Located (SrcSpanLess (Located [ExprLStmt GhcTc]))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ stmts :: SrcSpanLess (Located [ExprLStmt GhcTc])
stmts)) = [ExprLStmt GhcTc] -> DsM CoreExpr
dsMonadComp [ExprLStmt GhcTc]
SrcSpanLess (Located [ExprLStmt GhcTc])
stmts

ds_expr _ (HsIf _ mb_fun :: Maybe (SyntaxExpr GhcTc)
mb_fun guard_expr :: LHsExpr GhcTc
guard_expr then_expr :: LHsExpr GhcTc
then_expr else_expr :: LHsExpr GhcTc
else_expr)
  = do { CoreExpr
pred <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
guard_expr
       ; CoreExpr
b1 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
then_expr
       ; CoreExpr
b2 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
else_expr
       ; case Maybe (SyntaxExpr GhcTc)
mb_fun of
           Just fun :: SyntaxExpr GhcTc
fun -> SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
fun [CoreExpr
pred, CoreExpr
b1, CoreExpr
b2]
           Nothing  -> CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ CoreExpr -> CoreExpr -> CoreExpr -> CoreExpr
mkIfThenElse CoreExpr
pred CoreExpr
b1 CoreExpr
b2 }

ds_expr _ (HsMultiIf res_ty :: XMultiIf GhcTc
res_ty alts :: [LGRHS GhcTc (LHsExpr GhcTc)]
alts)
  | [LGRHS GhcTc (LHsExpr GhcTc)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LGRHS GhcTc (LHsExpr GhcTc)]
alts
  = DsM CoreExpr
mkErrorExpr

  | Bool
otherwise
  = do { MatchResult
match_result <- ([MatchResult] -> MatchResult)
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult]
-> IOEnv (Env DsGblEnv DsLclEnv) MatchResult
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ((MatchResult -> MatchResult -> MatchResult)
-> [MatchResult] -> MatchResult
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 MatchResult -> MatchResult -> MatchResult
combineMatchResults)
                               ((LGRHS GhcTc (LHsExpr GhcTc)
 -> IOEnv (Env DsGblEnv DsLclEnv) MatchResult)
-> [LGRHS GhcTc (LHsExpr GhcTc)]
-> IOEnv (Env DsGblEnv DsLclEnv) [MatchResult]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (HsMatchContext Name
-> Type
-> LGRHS GhcTc (LHsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) MatchResult
dsGRHS HsMatchContext Name
forall id. HsMatchContext id
IfAlt Type
XMultiIf GhcTc
res_ty) [LGRHS GhcTc (LHsExpr GhcTc)]
alts)
       ; HsMatchContext Name
-> GRHSs GhcTc (LHsExpr GhcTc) -> IOEnv (Env DsGblEnv DsLclEnv) ()
checkGuardMatches HsMatchContext Name
forall id. HsMatchContext id
IfAlt (XCGRHSs GhcTc (LHsExpr GhcTc)
-> [LGRHS GhcTc (LHsExpr GhcTc)]
-> LHsLocalBinds GhcTc
-> GRHSs GhcTc (LHsExpr GhcTc)
forall p body.
XCGRHSs p body -> [LGRHS p body] -> LHsLocalBinds p -> GRHSs p body
GRHSs XCGRHSs GhcTc (LHsExpr GhcTc)
NoExt
noExt [LGRHS GhcTc (LHsExpr GhcTc)]
alts (SrcSpanLess (LHsLocalBinds GhcTc) -> LHsLocalBinds GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc SrcSpanLess (LHsLocalBinds GhcTc)
forall (a :: Pass) (b :: Pass).
HsLocalBindsLR (GhcPass a) (GhcPass b)
emptyLocalBinds))
       ; CoreExpr
error_expr   <- DsM CoreExpr
mkErrorExpr
       ; MatchResult -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult
match_result CoreExpr
error_expr }
  where
    mkErrorExpr :: DsM CoreExpr
mkErrorExpr = CoreBndr -> Type -> SDoc -> DsM CoreExpr
mkErrorAppDs CoreBndr
nON_EXHAUSTIVE_GUARDS_ERROR_ID Type
XMultiIf GhcTc
res_ty
                               (String -> SDoc
text "multi-way if")

{-
\noindent
\underline{\bf Various data construction things}
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-}

ds_expr _ (ExplicitList elt_ty :: XExplicitList GhcTc
elt_ty wit :: Maybe (SyntaxExpr GhcTc)
wit xs :: [LHsExpr GhcTc]
xs)
  = Type -> Maybe (SyntaxExpr GhcTc) -> [LHsExpr GhcTc] -> DsM CoreExpr
dsExplicitList Type
XExplicitList GhcTc
elt_ty Maybe (SyntaxExpr GhcTc)
wit [LHsExpr GhcTc]
xs

ds_expr _ (ArithSeq expr :: XArithSeq GhcTc
expr witness :: Maybe (SyntaxExpr GhcTc)
witness seq :: ArithSeqInfo GhcTc
seq)
  = case Maybe (SyntaxExpr GhcTc)
witness of
     Nothing -> HsExpr GhcTc -> ArithSeqInfo GhcTc -> DsM CoreExpr
dsArithSeq XArithSeq GhcTc
HsExpr GhcTc
expr ArithSeqInfo GhcTc
seq
     Just fl :: SyntaxExpr GhcTc
fl -> do { CoreExpr
newArithSeq <- HsExpr GhcTc -> ArithSeqInfo GhcTc -> DsM CoreExpr
dsArithSeq XArithSeq GhcTc
HsExpr GhcTc
expr ArithSeqInfo GhcTc
seq
                   ; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
fl [CoreExpr
newArithSeq] }

{-
Static Pointers
~~~~~~~~~~~~~~~

See Note [Grand plan for static forms] in StaticPtrTable for an overview.

    g = ... static f ...
==>
    g = ... makeStatic loc f ...
-}

ds_expr _ (HsStatic _ expr :: LHsExpr GhcTc
expr@(LHsExpr GhcTc -> Located (SrcSpanLess (LHsExpr GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc _)) = do
    CoreExpr
expr_ds <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
expr
    let ty :: Type
ty = CoreExpr -> Type
exprType CoreExpr
expr_ds
    CoreBndr
makeStaticId <- Name -> DsM CoreBndr
dsLookupGlobalId Name
makeStaticName

    DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
    let (line :: Int
line, col :: Int
col) = case SrcSpan
loc of
           RealSrcSpan r :: RealSrcSpan
r -> ( RealSrcLoc -> Int
srcLocLine (RealSrcLoc -> Int) -> RealSrcLoc -> Int
forall a b. (a -> b) -> a -> b
$ RealSrcSpan -> RealSrcLoc
realSrcSpanStart RealSrcSpan
r
                            , RealSrcLoc -> Int
srcLocCol  (RealSrcLoc -> Int) -> RealSrcLoc -> Int
forall a b. (a -> b) -> a -> b
$ RealSrcSpan -> RealSrcLoc
realSrcSpanStart RealSrcSpan
r
                            )
           _             -> (0, 0)
        srcLoc :: CoreExpr
srcLoc = DataCon -> [CoreExpr] -> CoreExpr
mkCoreConApps (Boxity -> Int -> DataCon
tupleDataCon Boxity
Boxed 2)
                     [ Type -> CoreExpr
forall b. Type -> Expr b
Type Type
intTy              , Type -> CoreExpr
forall b. Type -> Expr b
Type Type
intTy
                     , DynFlags -> Int -> CoreExpr
mkIntExprInt DynFlags
dflags Int
line, DynFlags -> Int -> CoreExpr
mkIntExprInt DynFlags
dflags Int
col
                     ]

    SrcSpan -> DsM CoreExpr -> DsM CoreExpr
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (DsM CoreExpr -> DsM CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$
      CoreExpr -> [CoreExpr] -> CoreExpr
mkCoreApps (CoreBndr -> CoreExpr
forall b. CoreBndr -> Expr b
Var CoreBndr
makeStaticId) [ Type -> CoreExpr
forall b. Type -> Expr b
Type Type
ty, CoreExpr
srcLoc, CoreExpr
expr_ds ]

{-
\noindent
\underline{\bf Record construction and update}
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For record construction we do this (assuming T has three arguments)
\begin{verbatim}
        T { op2 = e }
==>
        let err = /\a -> recConErr a
        T (recConErr t1 "M.hs/230/op1")
          e
          (recConErr t1 "M.hs/230/op3")
\end{verbatim}
@recConErr@ then converts its argument string into a proper message
before printing it as
\begin{verbatim}
        M.hs, line 230: missing field op1 was evaluated
\end{verbatim}

We also handle @C{}@ as valid construction syntax for an unlabelled
constructor @C@, setting all of @C@'s fields to bottom.
-}

ds_expr _ (RecordCon { rcon_flds :: forall p. HsExpr p -> HsRecordBinds p
rcon_flds = HsRecordBinds GhcTc
rbinds
                     , rcon_ext :: forall p. HsExpr p -> XRecordCon p
rcon_ext = RecordConTc { rcon_con_expr = con_expr
                                              , rcon_con_like = con_like }})
  = do { CoreExpr
con_expr' <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
con_expr
       ; let
             (arg_tys :: [Type]
arg_tys, _) = Type -> ([Type], Type)
tcSplitFunTys (CoreExpr -> Type
exprType CoreExpr
con_expr')
             -- A newtype in the corner should be opaque;
             -- hence TcType.tcSplitFunTys

             mk_arg :: (Type, FieldLbl Name) -> DsM CoreExpr
mk_arg (arg_ty :: Type
arg_ty, fl :: FieldLbl Name
fl)
               = case [LHsRecField GhcTc (LHsExpr GhcTc)] -> Name -> [LHsExpr GhcTc]
forall arg. [LHsRecField GhcTc arg] -> Name -> [arg]
findField (HsRecordBinds GhcTc -> [LHsRecField GhcTc (LHsExpr GhcTc)]
forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds HsRecordBinds GhcTc
rbinds) (FieldLbl Name -> Name
forall a. FieldLbl a -> a
flSelector FieldLbl Name
fl) of
                   (rhs :: LHsExpr GhcTc
rhs:rhss :: [LHsExpr GhcTc]
rhss) -> ASSERT( null rhss )
                                 LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
rhs
                   []         -> CoreBndr -> Type -> SDoc -> DsM CoreExpr
mkErrorAppDs CoreBndr
rEC_CON_ERROR_ID Type
arg_ty (FastString -> SDoc
forall a. Outputable a => a -> SDoc
ppr (FieldLbl Name -> FastString
forall a. FieldLbl a -> FastString
flLabel FieldLbl Name
fl))
             unlabelled_bottom :: Type -> DsM CoreExpr
unlabelled_bottom arg_ty :: Type
arg_ty = CoreBndr -> Type -> SDoc -> DsM CoreExpr
mkErrorAppDs CoreBndr
rEC_CON_ERROR_ID Type
arg_ty SDoc
Outputable.empty

             labels :: [FieldLbl Name]
labels = ConLike -> [FieldLbl Name]
conLikeFieldLabels ConLike
con_like

       ; [CoreExpr]
con_args <- if [FieldLbl Name] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLbl Name]
labels
                     then (Type -> DsM CoreExpr) -> [Type] -> DsM [CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM CoreExpr
unlabelled_bottom [Type]
arg_tys
                     else ((Type, FieldLbl Name) -> DsM CoreExpr)
-> [(Type, FieldLbl Name)] -> DsM [CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Type, FieldLbl Name) -> DsM CoreExpr
mk_arg (String -> [Type] -> [FieldLbl Name] -> [(Type, FieldLbl Name)]
forall a b. String -> [a] -> [b] -> [(a, b)]
zipEqual "dsExpr:RecordCon" [Type]
arg_tys [FieldLbl Name]
labels)

       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> [CoreExpr] -> CoreExpr
mkCoreApps CoreExpr
con_expr' [CoreExpr]
con_args) }

{-
Record update is a little harder. Suppose we have the decl:
\begin{verbatim}
        data T = T1 {op1, op2, op3 :: Int}
               | T2 {op4, op2 :: Int}
               | T3
\end{verbatim}
Then we translate as follows:
\begin{verbatim}
        r { op2 = e }
===>
        let op2 = e in
        case r of
          T1 op1 _ op3 -> T1 op1 op2 op3
          T2 op4 _     -> T2 op4 op2
          other        -> recUpdError "M.hs/230"
\end{verbatim}
It's important that we use the constructor Ids for @T1@, @T2@ etc on the
RHSs, and do not generate a Core constructor application directly, because the constructor
might do some argument-evaluation first; and may have to throw away some
dictionaries.

Note [Update for GADTs]
~~~~~~~~~~~~~~~~~~~~~~~
Consider
   data T a b where
     T1 :: { f1 :: a } -> T a Int

Then the wrapper function for T1 has type
   $WT1 :: a -> T a Int
But if x::T a b, then
   x { f1 = v } :: T a b   (not T a Int!)
So we need to cast (T a Int) to (T a b).  Sigh.

-}

ds_expr _ expr :: HsExpr GhcTc
expr@(RecordUpd { rupd_expr :: forall p. HsExpr p -> LHsExpr p
rupd_expr = LHsExpr GhcTc
record_expr, rupd_flds :: forall p. HsExpr p -> [LHsRecUpdField p]
rupd_flds = [LHsRecUpdField GhcTc]
fields
                          , rupd_ext :: forall p. HsExpr p -> XRecordUpd p
rupd_ext = RecordUpdTc
                              { rupd_cons = cons_to_upd
                              , rupd_in_tys = in_inst_tys
                              , rupd_out_tys = out_inst_tys
                              , rupd_wrap = dict_req_wrap }} )
  | [LHsRecUpdField GhcTc] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LHsRecUpdField GhcTc]
fields
  = LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
record_expr
  | Bool
otherwise
  = ASSERT2( notNull cons_to_upd, ppr expr )

    do  { CoreExpr
record_expr' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
record_expr
        ; [(Name, CoreBndr, CoreExpr)]
field_binds' <- (LHsRecUpdField GhcTc
 -> IOEnv (Env DsGblEnv DsLclEnv) (Name, CoreBndr, CoreExpr))
-> [LHsRecUpdField GhcTc]
-> IOEnv (Env DsGblEnv DsLclEnv) [(Name, CoreBndr, CoreExpr)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LHsRecUpdField GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) (Name, CoreBndr, CoreExpr)
ds_field [LHsRecUpdField GhcTc]
fields
        ; let upd_fld_env :: NameEnv Id -- Maps field name to the LocalId of the field binding
              upd_fld_env :: NameEnv CoreBndr
upd_fld_env = [(Name, CoreBndr)] -> NameEnv CoreBndr
forall a. [(Name, a)] -> NameEnv a
mkNameEnv [(Name
f,CoreBndr
l) | (f :: Name
f,l :: CoreBndr
l,_) <- [(Name, CoreBndr, CoreExpr)]
field_binds']

        -- It's important to generate the match with matchWrapper,
        -- and the right hand sides with applications of the wrapper Id
        -- so that everything works when we are doing fancy unboxing on the
        -- constructor arguments.
        ; [LMatch GhcTc (LHsExpr GhcTc)]
alts <- (ConLike
 -> IOEnv (Env DsGblEnv DsLclEnv) (LMatch GhcTc (LHsExpr GhcTc)))
-> [ConLike]
-> IOEnv (Env DsGblEnv DsLclEnv) [LMatch GhcTc (LHsExpr GhcTc)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (NameEnv CoreBndr
-> ConLike
-> IOEnv (Env DsGblEnv DsLclEnv) (LMatch GhcTc (LHsExpr GhcTc))
mk_alt NameEnv CoreBndr
upd_fld_env) [ConLike]
cons_to_upd
        ; ([discrim_var :: CoreBndr
discrim_var], matching_code :: CoreExpr
matching_code)
                <- HsMatchContext Name
-> Maybe (LHsExpr GhcTc)
-> MatchGroup GhcTc (LHsExpr GhcTc)
-> DsM ([CoreBndr], CoreExpr)
matchWrapper HsMatchContext Name
forall id. HsMatchContext id
RecUpd Maybe (LHsExpr GhcTc)
forall a. Maybe a
Nothing
                                      (MG :: forall p body.
XMG p body
-> Located [LMatch p body] -> Origin -> MatchGroup p body
MG { mg_alts :: Located [LMatch GhcTc (LHsExpr GhcTc)]
mg_alts = SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
-> Located [LMatch GhcTc (LHsExpr GhcTc)]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc [LMatch GhcTc (LHsExpr GhcTc)]
SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
alts
                                          , mg_ext :: XMG GhcTc (LHsExpr GhcTc)
mg_ext = [Type] -> Type -> MatchGroupTc
MatchGroupTc [Type
in_ty] Type
out_ty
                                          , mg_origin :: Origin
mg_origin = Origin
FromSource })
                                     -- FromSource is not strictly right, but we
                                     -- want incomplete pattern-match warnings

        ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return ([(Name, CoreBndr, CoreExpr)] -> CoreExpr -> CoreExpr
forall a. [(a, CoreBndr, CoreExpr)] -> CoreExpr -> CoreExpr
add_field_binds [(Name, CoreBndr, CoreExpr)]
field_binds' (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
$
                  CoreBndr -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec CoreBndr
discrim_var CoreExpr
record_expr' CoreExpr
matching_code) }
  where
    ds_field :: LHsRecUpdField GhcTc -> DsM (Name, Id, CoreExpr)
      -- Clone the Id in the HsRecField, because its Name is that
      -- of the record selector, and we must not make that a local binder
      -- else we shadow other uses of the record selector
      -- Hence 'lcl_id'.  Cf Trac #2735
    ds_field :: LHsRecUpdField GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) (Name, CoreBndr, CoreExpr)
ds_field (LHsRecUpdField GhcTc
-> Located (SrcSpanLess (LHsRecUpdField GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ rec_field :: SrcSpanLess (LHsRecUpdField GhcTc)
rec_field)
      = do { CoreExpr
rhs <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr (HsRecField' (AmbiguousFieldOcc GhcTc) (LHsExpr GhcTc)
-> LHsExpr GhcTc
forall id arg. HsRecField' id arg -> arg
hsRecFieldArg SrcSpanLess (LHsRecUpdField GhcTc)
HsRecField' (AmbiguousFieldOcc GhcTc) (LHsExpr GhcTc)
rec_field)
           ; let fld_id :: SrcSpanLess (Located CoreBndr)
fld_id = Located CoreBndr -> SrcSpanLess (Located CoreBndr)
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc (HsRecField' (AmbiguousFieldOcc GhcTc) (LHsExpr GhcTc)
-> Located CoreBndr
forall arg.
HsRecField' (AmbiguousFieldOcc GhcTc) arg -> Located CoreBndr
hsRecUpdFieldId SrcSpanLess (LHsRecUpdField GhcTc)
HsRecField' (AmbiguousFieldOcc GhcTc) (LHsExpr GhcTc)
rec_field)
           ; CoreBndr
lcl_id <- Type -> DsM CoreBndr
newSysLocalDs (CoreBndr -> Type
idType SrcSpanLess (Located CoreBndr)
CoreBndr
fld_id)
           ; (Name, CoreBndr, CoreExpr)
-> IOEnv (Env DsGblEnv DsLclEnv) (Name, CoreBndr, CoreExpr)
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreBndr -> Name
idName SrcSpanLess (Located CoreBndr)
CoreBndr
fld_id, CoreBndr
lcl_id, CoreExpr
rhs) }

    add_field_binds :: [(a, CoreBndr, CoreExpr)] -> CoreExpr -> CoreExpr
add_field_binds [] expr :: CoreExpr
expr = CoreExpr
expr
    add_field_binds ((_,b :: CoreBndr
b,r :: CoreExpr
r):bs :: [(a, CoreBndr, CoreExpr)]
bs) expr :: CoreExpr
expr = CoreBndr -> CoreExpr -> CoreExpr -> CoreExpr
bindNonRec CoreBndr
b CoreExpr
r ([(a, CoreBndr, CoreExpr)] -> CoreExpr -> CoreExpr
add_field_binds [(a, CoreBndr, CoreExpr)]
bs CoreExpr
expr)

        -- Awkwardly, for families, the match goes
        -- from instance type to family type
    (in_ty :: Type
in_ty, out_ty :: Type
out_ty) =
      case ([ConLike] -> ConLike
forall a. [a] -> a
head [ConLike]
cons_to_upd) of
        RealDataCon data_con :: DataCon
data_con ->
          let tycon :: TyCon
tycon = DataCon -> TyCon
dataConTyCon DataCon
data_con in
          (TyCon -> [Type] -> Type
mkTyConApp TyCon
tycon [Type]
in_inst_tys, TyCon -> [Type] -> Type
mkFamilyTyConApp TyCon
tycon [Type]
out_inst_tys)
        PatSynCon pat_syn :: PatSyn
pat_syn ->
          ( PatSyn -> [Type] -> Type
patSynInstResTy PatSyn
pat_syn [Type]
in_inst_tys
          , PatSyn -> [Type] -> Type
patSynInstResTy PatSyn
pat_syn [Type]
out_inst_tys)
    mk_alt :: NameEnv CoreBndr
-> ConLike
-> IOEnv (Env DsGblEnv DsLclEnv) (LMatch GhcTc (LHsExpr GhcTc))
mk_alt upd_fld_env :: NameEnv CoreBndr
upd_fld_env con :: ConLike
con
      = do { let (univ_tvs :: [CoreBndr]
univ_tvs, ex_tvs :: [CoreBndr]
ex_tvs, eq_spec :: [EqSpec]
eq_spec,
                  prov_theta :: [Type]
prov_theta, _req_theta :: [Type]
_req_theta, arg_tys :: [Type]
arg_tys, _) = ConLike
-> ([CoreBndr], [CoreBndr], [EqSpec], [Type], [Type], [Type], Type)
conLikeFullSig ConLike
con
                 user_tvs :: [CoreBndr]
user_tvs =
                   case ConLike
con of
                     RealDataCon data_con :: DataCon
data_con -> DataCon -> [CoreBndr]
dataConUserTyVars DataCon
data_con
                     PatSynCon _          -> [CoreBndr]
univ_tvs [CoreBndr] -> [CoreBndr] -> [CoreBndr]
forall a. [a] -> [a] -> [a]
++ [CoreBndr]
ex_tvs
                       -- The order here is because of the order in `TcPatSyn`.
                 in_subst :: TCvSubst
in_subst  = [CoreBndr] -> [Type] -> TCvSubst
HasDebugCallStack => [CoreBndr] -> [Type] -> TCvSubst
zipTvSubst [CoreBndr]
univ_tvs [Type]
in_inst_tys
                 out_subst :: TCvSubst
out_subst = [CoreBndr] -> [Type] -> TCvSubst
HasDebugCallStack => [CoreBndr] -> [Type] -> TCvSubst
zipTvSubst [CoreBndr]
univ_tvs [Type]
out_inst_tys

                -- I'm not bothering to clone the ex_tvs
           ; [CoreBndr]
eqs_vars   <- (Type -> DsM CoreBndr) -> [Type] -> DsM [CoreBndr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM CoreBndr
newPredVarDs (HasCallStack => TCvSubst -> [Type] -> [Type]
TCvSubst -> [Type] -> [Type]
substTheta TCvSubst
in_subst ([EqSpec] -> [Type]
eqSpecPreds [EqSpec]
eq_spec))
           ; [CoreBndr]
theta_vars <- (Type -> DsM CoreBndr) -> [Type] -> DsM [CoreBndr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Type -> DsM CoreBndr
newPredVarDs (HasCallStack => TCvSubst -> [Type] -> [Type]
TCvSubst -> [Type] -> [Type]
substTheta TCvSubst
in_subst [Type]
prov_theta)
           ; [CoreBndr]
arg_ids    <- [Type] -> DsM [CoreBndr]
newSysLocalsDs (TCvSubst -> [Type] -> [Type]
substTysUnchecked TCvSubst
in_subst [Type]
arg_tys)
           ; let field_labels :: [FieldLbl Name]
field_labels = ConLike -> [FieldLbl Name]
conLikeFieldLabels ConLike
con
                 val_args :: [LHsExpr GhcTc]
val_args = String
-> (FieldLbl Name -> CoreBndr -> LHsExpr GhcTc)
-> [FieldLbl Name]
-> [CoreBndr]
-> [LHsExpr GhcTc]
forall a b c. String -> (a -> b -> c) -> [a] -> [b] -> [c]
zipWithEqual "dsExpr:RecordUpd" FieldLbl Name -> CoreBndr -> LHsExpr GhcTc
mk_val_arg
                                         [FieldLbl Name]
field_labels [CoreBndr]
arg_ids
                 mk_val_arg :: FieldLbl Name -> CoreBndr -> LHsExpr GhcTc
mk_val_arg fl :: FieldLbl Name
fl pat_arg_id :: CoreBndr
pat_arg_id
                     = IdP GhcTc -> LHsExpr GhcTc
forall (id :: Pass). IdP (GhcPass id) -> LHsExpr (GhcPass id)
nlHsVar (NameEnv CoreBndr -> Name -> Maybe CoreBndr
forall a. NameEnv a -> Name -> Maybe a
lookupNameEnv NameEnv CoreBndr
upd_fld_env (FieldLbl Name -> Name
forall a. FieldLbl a -> a
flSelector FieldLbl Name
fl) Maybe CoreBndr -> CoreBndr -> CoreBndr
forall a. Maybe a -> a -> a
`orElse` CoreBndr
pat_arg_id)

                 inst_con :: LHsExpr GhcTc
inst_con = SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc)
-> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a b. (a -> b) -> a -> b
$ HsWrapper -> HsExpr GhcTc -> HsExpr GhcTc
forall (id :: Pass).
HsWrapper -> HsExpr (GhcPass id) -> HsExpr (GhcPass id)
mkHsWrap HsWrapper
wrap (XConLikeOut GhcTc -> ConLike -> HsExpr GhcTc
forall p. XConLikeOut p -> ConLike -> HsExpr p
HsConLikeOut XConLikeOut GhcTc
NoExt
noExt ConLike
con)
                        -- Reconstruct with the WrapId so that unpacking happens
                 wrap :: HsWrapper
wrap = [CoreBndr] -> HsWrapper
mkWpEvVarApps [CoreBndr]
theta_vars                                HsWrapper -> HsWrapper -> HsWrapper
<.>
                        HsWrapper
dict_req_wrap                                           HsWrapper -> HsWrapper -> HsWrapper
<.>
                        [Type] -> HsWrapper
mkWpTyApps    [ TCvSubst -> CoreBndr -> Maybe Type
lookupTyVar TCvSubst
out_subst CoreBndr
tv
                                          Maybe Type -> Type -> Type
forall a. Maybe a -> a -> a
`orElse` CoreBndr -> Type
mkTyVarTy CoreBndr
tv
                                      | CoreBndr
tv <- [CoreBndr]
user_tvs
                                      , Bool -> Bool
not (CoreBndr
tv CoreBndr -> VarEnv TcCoercion -> Bool
forall a. CoreBndr -> VarEnv a -> Bool
`elemVarEnv` VarEnv TcCoercion
wrap_subst) ]
                          -- Be sure to use user_tvs (which may be ordered
                          -- differently than `univ_tvs ++ ex_tvs) above.
                          -- See Note [DataCon user type variable binders]
                          -- in DataCon.
                 rhs :: LHsExpr GhcTc
rhs = (LHsExpr GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc)
-> LHsExpr GhcTc -> [LHsExpr GhcTc] -> LHsExpr GhcTc
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\a :: LHsExpr GhcTc
a b :: LHsExpr GhcTc
b -> LHsExpr GhcTc -> LHsExpr GhcTc -> LHsExpr GhcTc
forall (id :: Pass).
LHsExpr (GhcPass id)
-> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
nlHsApp LHsExpr GhcTc
a LHsExpr GhcTc
b) LHsExpr GhcTc
inst_con [LHsExpr GhcTc]
val_args

                        -- Tediously wrap the application in a cast
                        -- Note [Update for GADTs]
                 wrapped_rhs :: LHsExpr GhcTc
wrapped_rhs =
                  case ConLike
con of
                    RealDataCon data_con :: DataCon
data_con ->
                      let
                        wrap_co :: TcCoercion
wrap_co =
                          Role -> TyCon -> [TcCoercion] -> TcCoercion
mkTcTyConAppCo Role
Nominal
                            (DataCon -> TyCon
dataConTyCon DataCon
data_con)
                            [ CoreBndr -> Type -> TcCoercion
lookup CoreBndr
tv Type
ty
                              | (tv :: CoreBndr
tv,ty :: Type
ty) <- [CoreBndr]
univ_tvs [CoreBndr] -> [Type] -> [(CoreBndr, Type)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [Type]
out_inst_tys ]
                        lookup :: CoreBndr -> Type -> TcCoercion
lookup univ_tv :: CoreBndr
univ_tv ty :: Type
ty =
                          case VarEnv TcCoercion -> CoreBndr -> Maybe TcCoercion
forall a. VarEnv a -> CoreBndr -> Maybe a
lookupVarEnv VarEnv TcCoercion
wrap_subst CoreBndr
univ_tv of
                            Just co' :: TcCoercion
co' -> TcCoercion
co'
                            Nothing  -> Role -> Type -> TcCoercion
mkTcReflCo Role
Nominal Type
ty
                        in if [EqSpec] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [EqSpec]
eq_spec
                             then LHsExpr GhcTc
rhs
                             else HsWrapper -> LHsExpr GhcTc -> LHsExpr GhcTc
forall (id :: Pass).
HsWrapper -> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
mkLHsWrap (TcCoercion -> HsWrapper
mkWpCastN TcCoercion
wrap_co) LHsExpr GhcTc
rhs
                    -- eq_spec is always null for a PatSynCon
                    PatSynCon _ -> LHsExpr GhcTc
rhs

                 wrap_subst :: VarEnv TcCoercion
wrap_subst =
                  [(CoreBndr, TcCoercion)] -> VarEnv TcCoercion
forall a. [(CoreBndr, a)] -> VarEnv a
mkVarEnv [ (CoreBndr
tv, TcCoercion -> TcCoercion
mkTcSymCo (CoreBndr -> TcCoercion
mkTcCoVarCo CoreBndr
eq_var))
                           | (spec :: EqSpec
spec, eq_var :: CoreBndr
eq_var) <- [EqSpec]
eq_spec [EqSpec] -> [CoreBndr] -> [(EqSpec, CoreBndr)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [CoreBndr]
eqs_vars
                           , let tv :: CoreBndr
tv = EqSpec -> CoreBndr
eqSpecTyVar EqSpec
spec ]

                 req_wrap :: HsWrapper
req_wrap = HsWrapper
dict_req_wrap HsWrapper -> HsWrapper -> HsWrapper
<.> [Type] -> HsWrapper
mkWpTyApps [Type]
in_inst_tys

                 pat :: LPat GhcTc
pat = SrcSpanLess (LPat GhcTc) -> LPat GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LPat GhcTc) -> LPat GhcTc)
-> SrcSpanLess (LPat GhcTc) -> LPat GhcTc
forall a b. (a -> b) -> a -> b
$ ConPatOut :: forall p.
Located ConLike
-> [Type]
-> [CoreBndr]
-> [CoreBndr]
-> TcEvBinds
-> HsConPatDetails p
-> HsWrapper
-> Pat p
ConPatOut { pat_con :: Located ConLike
pat_con = SrcSpanLess (Located ConLike) -> Located ConLike
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc SrcSpanLess (Located ConLike)
ConLike
con
                                         , pat_tvs :: [CoreBndr]
pat_tvs = [CoreBndr]
ex_tvs
                                         , pat_dicts :: [CoreBndr]
pat_dicts = [CoreBndr]
eqs_vars [CoreBndr] -> [CoreBndr] -> [CoreBndr]
forall a. [a] -> [a] -> [a]
++ [CoreBndr]
theta_vars
                                         , pat_binds :: TcEvBinds
pat_binds = TcEvBinds
emptyTcEvBinds
                                         , pat_args :: HsConPatDetails GhcTc
pat_args = [LPat GhcTc] -> HsConPatDetails GhcTc
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon ([LPat GhcTc] -> HsConPatDetails GhcTc)
-> [LPat GhcTc] -> HsConPatDetails GhcTc
forall a b. (a -> b) -> a -> b
$ (CoreBndr -> LPat GhcTc) -> [CoreBndr] -> [LPat GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map CoreBndr -> LPat GhcTc
forall (id :: Pass). IdP (GhcPass id) -> LPat (GhcPass id)
nlVarPat [CoreBndr]
arg_ids
                                         , pat_arg_tys :: [Type]
pat_arg_tys = [Type]
in_inst_tys
                                         , pat_wrap :: HsWrapper
pat_wrap = HsWrapper
req_wrap }
           ; LMatch GhcTc (LHsExpr GhcTc)
-> IOEnv (Env DsGblEnv DsLclEnv) (LMatch GhcTc (LHsExpr GhcTc))
forall (m :: * -> *) a. Monad m => a -> m a
return (HsMatchContext (NameOrRdrName (IdP GhcTc))
-> [LPat GhcTc] -> LHsExpr GhcTc -> LMatch GhcTc (LHsExpr GhcTc)
forall (p :: Pass) (body :: * -> *).
HsMatchContext (NameOrRdrName (IdP (GhcPass p)))
-> [LPat (GhcPass p)]
-> Located (body (GhcPass p))
-> LMatch (GhcPass p) (Located (body (GhcPass p)))
mkSimpleMatch HsMatchContext (NameOrRdrName (IdP GhcTc))
forall id. HsMatchContext id
RecUpd [LPat GhcTc
pat] LHsExpr GhcTc
wrapped_rhs) }

-- Here is where we desugar the Template Haskell brackets and escapes

-- Template Haskell stuff

ds_expr _ (HsRnBracketOut _ _ _)  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr HsRnBracketOut"
ds_expr _ (HsTcBracketOut _ x :: HsBracket GhcRn
x ps :: [PendingTcSplice]
ps) = HsBracket GhcRn -> [PendingTcSplice] -> DsM CoreExpr
dsBracket HsBracket GhcRn
x [PendingTcSplice]
ps
ds_expr _ (HsSpliceE _ s :: HsSplice GhcTc
s)         = String -> SDoc -> DsM CoreExpr
forall a. HasCallStack => String -> SDoc -> a
pprPanic "dsExpr:splice" (HsSplice GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsSplice GhcTc
s)

-- Arrow notation extension
ds_expr _ (HsProc _ pat :: LPat GhcTc
pat cmd :: LHsCmdTop GhcTc
cmd) = LPat GhcTc -> LHsCmdTop GhcTc -> DsM CoreExpr
dsProcExpr LPat GhcTc
pat LHsCmdTop GhcTc
cmd

-- Hpc Support

ds_expr _ (HsTick _ tickish :: Tickish (IdP GhcTc)
tickish e :: LHsExpr GhcTc
e) = do
  CoreExpr
e' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
e
  CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (Tickish CoreBndr -> CoreExpr -> CoreExpr
forall b. Tickish CoreBndr -> Expr b -> Expr b
Tick Tickish CoreBndr
Tickish (IdP GhcTc)
tickish CoreExpr
e')

-- There is a problem here. The then and else branches
-- have no free variables, so they are open to lifting.
-- We need someway of stopping this.
-- This will make no difference to binary coverage
-- (did you go here: YES or NO), but will effect accurate
-- tick counting.

ds_expr _ (HsBinTick _ ixT :: Int
ixT ixF :: Int
ixF e :: LHsExpr GhcTc
e) = do
  CoreExpr
e2 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
e
  do { ASSERT(exprType e2 `eqType` boolTy)
       Int -> Int -> CoreExpr -> DsM CoreExpr
mkBinaryTickBox Int
ixT Int
ixF CoreExpr
e2
     }

ds_expr _ (HsTickPragma _ _ _ _ expr :: LHsExpr GhcTc
expr) = do
  DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
  if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_Hpc DynFlags
dflags
    then String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:HsTickPragma"
    else LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
expr

-- HsSyn constructs that just shouldn't be here:
ds_expr _ (HsBracket     {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:HsBracket"
ds_expr _ (HsArrApp      {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:HsArrApp"
ds_expr _ (HsArrForm     {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:HsArrForm"
ds_expr _ (EWildPat      {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:EWildPat"
ds_expr _ (EAsPat        {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:EAsPat"
ds_expr _ (EViewPat      {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:EViewPat"
ds_expr _ (ELazyPat      {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:ELazyPat"
ds_expr _ (HsDo          {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:HsDo"
ds_expr _ (HsRecFld      {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr:HsRecFld"
ds_expr _ (XExpr         {})  = String -> DsM CoreExpr
forall a. String -> a
panic "dsExpr: XExpr"


------------------------------
dsSyntaxExpr :: SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr :: SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr (SyntaxExpr { syn_expr :: forall p. SyntaxExpr p -> HsExpr p
syn_expr      = HsExpr GhcTc
expr
                         , syn_arg_wraps :: forall p. SyntaxExpr p -> [HsWrapper]
syn_arg_wraps = [HsWrapper]
arg_wraps
                         , syn_res_wrap :: forall p. SyntaxExpr p -> HsWrapper
syn_res_wrap  = HsWrapper
res_wrap })
             arg_exprs :: [CoreExpr]
arg_exprs
  = do { CoreExpr
fun            <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
expr
       ; [CoreExpr -> CoreExpr]
core_arg_wraps <- (HsWrapper -> DsM (CoreExpr -> CoreExpr))
-> [HsWrapper]
-> IOEnv (Env DsGblEnv DsLclEnv) [CoreExpr -> CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM HsWrapper -> DsM (CoreExpr -> CoreExpr)
dsHsWrapper [HsWrapper]
arg_wraps
       ; CoreExpr -> CoreExpr
core_res_wrap  <- HsWrapper -> DsM (CoreExpr -> CoreExpr)
dsHsWrapper HsWrapper
res_wrap
       ; let wrapped_args :: [CoreExpr]
wrapped_args = ((CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr)
-> [CoreExpr -> CoreExpr] -> [CoreExpr] -> [CoreExpr]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (CoreExpr -> CoreExpr) -> CoreExpr -> CoreExpr
forall a b. (a -> b) -> a -> b
($) [CoreExpr -> CoreExpr]
core_arg_wraps [CoreExpr]
arg_exprs
       ; IOEnv (Env DsGblEnv DsLclEnv) ()
-> (() -> CoreExpr) -> DsM CoreExpr
forall a. DsM a -> (a -> CoreExpr) -> DsM CoreExpr
dsWhenNoErrs ((CoreExpr -> SDoc -> IOEnv (Env DsGblEnv DsLclEnv) ())
-> [CoreExpr] -> [SDoc] -> IOEnv (Env DsGblEnv DsLclEnv) ()
forall (m :: * -> *) a b c.
Applicative m =>
(a -> b -> m c) -> [a] -> [b] -> m ()
zipWithM_ CoreExpr -> SDoc -> IOEnv (Env DsGblEnv DsLclEnv) ()
dsNoLevPolyExpr [CoreExpr]
wrapped_args [ Int -> SDoc
mk_doc Int
n | Int
n <- [1..] ])
                      (\_ -> CoreExpr -> CoreExpr
core_res_wrap (CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
fun [CoreExpr]
wrapped_args)) }
  where
    mk_doc :: Int -> SDoc
mk_doc n :: Int
n = String -> SDoc
text "In the" SDoc -> SDoc -> SDoc
<+> Int -> SDoc
speakNth Int
n SDoc -> SDoc -> SDoc
<+> String -> SDoc
text "argument of" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes (HsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr HsExpr GhcTc
expr)

findField :: [LHsRecField GhcTc arg] -> Name -> [arg]
findField :: [LHsRecField GhcTc arg] -> Name -> [arg]
findField rbinds :: [LHsRecField GhcTc arg]
rbinds sel :: Name
sel
  = [HsRecField' (FieldOcc GhcTc) arg -> arg
forall id arg. HsRecField' id arg -> arg
hsRecFieldArg SrcSpanLess (LHsRecField GhcTc arg)
HsRecField' (FieldOcc GhcTc) arg
fld | (LHsRecField GhcTc arg
-> Located (SrcSpanLess (LHsRecField GhcTc arg))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ fld :: SrcSpanLess (LHsRecField GhcTc arg)
fld) <- [LHsRecField GhcTc arg]
rbinds
                       , Name
sel Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== CoreBndr -> Name
idName (Located CoreBndr -> CoreBndr
forall a. HasSrcSpan a => a -> SrcSpanLess a
unLoc (Located CoreBndr -> CoreBndr) -> Located CoreBndr -> CoreBndr
forall a b. (a -> b) -> a -> b
$ HsRecField' (FieldOcc GhcTc) arg -> Located CoreBndr
forall arg. HsRecField GhcTc arg -> Located CoreBndr
hsRecFieldId SrcSpanLess (LHsRecField GhcTc arg)
HsRecField' (FieldOcc GhcTc) arg
fld) ]

{-
%--------------------------------------------------------------------

Note [Desugaring explicit lists]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Explicit lists are desugared in a cleverer way to prevent some
fruitless allocations.  Essentially, whenever we see a list literal
[x_1, ..., x_n] we generate the corresponding expression in terms of
build:

Explicit lists (literals) are desugared to allow build/foldr fusion when
beneficial. This is a bit of a trade-off,

 * build/foldr fusion can generate far larger code than the corresponding
   cons-chain (e.g. see #11707)

 * even when it doesn't produce more code, build can still fail to fuse,
   requiring that the simplifier do more work to bring the expression
   back into cons-chain form; this costs compile time

 * when it works, fusion can be a significant win. Allocations are reduced
   by up to 25% in some nofib programs. Specifically,

        Program           Size    Allocs   Runtime  CompTime
        rewrite          +0.0%    -26.3%      0.02     -1.8%
           ansi          -0.3%    -13.8%      0.00     +0.0%
           lift          +0.0%     -8.7%      0.00     -2.3%

At the moment we use a simple heuristic to determine whether build will be
fruitful: for small lists we assume the benefits of fusion will be worthwhile;
for long lists we assume that the benefits will be outweighted by the cost of
code duplication. This magic length threshold is @maxBuildLength@. Also, fusion
won't work at all if rewrite rules are disabled, so we don't use the build-based
desugaring in this case.

We used to have a more complex heuristic which would try to break the list into
"static" and "dynamic" parts and only build-desugar the dynamic part.
Unfortunately, determining "static-ness" reliably is a bit tricky and the
heuristic at times produced surprising behavior (see #11710) so it was dropped.
-}

{- | The longest list length which we will desugar using @build@.

This is essentially a magic number and its setting is unfortunate rather
arbitrary. The idea here, as mentioned in Note [Desugaring explicit lists],
is to avoid deforesting large static data into large(r) code. Ideally we'd
want a smaller threshold with larger consumers and vice-versa, but we have no
way of knowing what will be consuming our list in the desugaring impossible to
set generally correctly.

The effect of reducing this number will be that 'build' fusion is applied
less often. From a runtime performance perspective, applying 'build' more
liberally on "moderately" sized lists should rarely hurt and will often it can
only expose further optimization opportunities; if no fusion is possible it will
eventually get rule-rewritten back to a list). We do, however, pay in compile
time.
-}
maxBuildLength :: Int
maxBuildLength :: Int
maxBuildLength = 32

dsExplicitList :: Type -> Maybe (SyntaxExpr GhcTc) -> [LHsExpr GhcTc]
               -> DsM CoreExpr
-- See Note [Desugaring explicit lists]
dsExplicitList :: Type -> Maybe (SyntaxExpr GhcTc) -> [LHsExpr GhcTc] -> DsM CoreExpr
dsExplicitList elt_ty :: Type
elt_ty Nothing xs :: [LHsExpr GhcTc]
xs
  = do { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       ; [CoreExpr]
xs' <- (LHsExpr GhcTc -> DsM CoreExpr)
-> [LHsExpr GhcTc] -> DsM [CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP [LHsExpr GhcTc]
xs
       ; if [CoreExpr]
xs' [CoreExpr] -> Int -> Bool
forall a. [a] -> Int -> Bool
`lengthExceeds` Int
maxBuildLength
                -- Don't generate builds if the list is very long.
         Bool -> Bool -> Bool
|| [CoreExpr] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [CoreExpr]
xs'
                -- Don't generate builds when the [] constructor will do
         Bool -> Bool -> Bool
|| Bool -> Bool
not (GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_EnableRewriteRules DynFlags
dflags)  -- Rewrite rules off
                -- Don't generate a build if there are no rules to eliminate it!
                -- See Note [Desugaring RULE left hand sides] in Desugar
         then CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ Type -> [CoreExpr] -> CoreExpr
mkListExpr Type
elt_ty [CoreExpr]
xs'
         else Type
-> ((CoreBndr, Type) -> (CoreBndr, Type) -> DsM CoreExpr)
-> DsM CoreExpr
forall (m :: * -> *).
(MonadFail m, MonadThings m, MonadUnique m) =>
Type
-> ((CoreBndr, Type) -> (CoreBndr, Type) -> m CoreExpr)
-> m CoreExpr
mkBuildExpr Type
elt_ty ([CoreExpr] -> (CoreBndr, Type) -> (CoreBndr, Type) -> DsM CoreExpr
forall (m :: * -> *) (t :: * -> *) b b b.
(Monad m, Foldable t) =>
t (Arg b) -> (CoreBndr, b) -> (CoreBndr, b) -> m (Arg b)
mk_build_list [CoreExpr]
xs') }
  where
    mk_build_list :: t (Arg b) -> (CoreBndr, b) -> (CoreBndr, b) -> m (Arg b)
mk_build_list xs' :: t (Arg b)
xs' (cons :: CoreBndr
cons, _) (nil :: CoreBndr
nil, _)
      = Arg b -> m (Arg b)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Arg b -> Arg b -> Arg b) -> Arg b -> t (Arg b) -> Arg b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Arg b -> Arg b -> Arg b
forall b. Expr b -> Expr b -> Expr b
App (Arg b -> Arg b -> Arg b)
-> (Arg b -> Arg b) -> Arg b -> Arg b -> Arg b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Arg b -> Arg b -> Arg b
forall b. Expr b -> Expr b -> Expr b
App (CoreBndr -> Arg b
forall b. CoreBndr -> Expr b
Var CoreBndr
cons)) (CoreBndr -> Arg b
forall b. CoreBndr -> Expr b
Var CoreBndr
nil) t (Arg b)
xs')

dsExplicitList elt_ty :: Type
elt_ty (Just fln :: SyntaxExpr GhcTc
fln) xs :: [LHsExpr GhcTc]
xs
  = do { CoreExpr
list <- Type -> Maybe (SyntaxExpr GhcTc) -> [LHsExpr GhcTc] -> DsM CoreExpr
dsExplicitList Type
elt_ty Maybe (SyntaxExpr GhcTc)
forall a. Maybe a
Nothing [LHsExpr GhcTc]
xs
       ; DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       ; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
fln [DynFlags -> Int -> CoreExpr
mkIntExprInt DynFlags
dflags ([LHsExpr GhcTc] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [LHsExpr GhcTc]
xs), CoreExpr
list] }

dsArithSeq :: PostTcExpr -> (ArithSeqInfo GhcTc) -> DsM CoreExpr
dsArithSeq :: HsExpr GhcTc -> ArithSeqInfo GhcTc -> DsM CoreExpr
dsArithSeq expr :: HsExpr GhcTc
expr (From from :: LHsExpr GhcTc
from)
  = CoreExpr -> CoreExpr -> CoreExpr
forall b. Expr b -> Expr b -> Expr b
App (CoreExpr -> CoreExpr -> CoreExpr)
-> DsM CoreExpr -> DsM (CoreExpr -> CoreExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
expr DsM (CoreExpr -> CoreExpr) -> DsM CoreExpr -> DsM CoreExpr
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
from
dsArithSeq expr :: HsExpr GhcTc
expr (FromTo from :: LHsExpr GhcTc
from to :: LHsExpr GhcTc
to)
  = do DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       DynFlags
-> LHsExpr GhcTc
-> Maybe (LHsExpr GhcTc)
-> LHsExpr GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) ()
warnAboutEmptyEnumerations DynFlags
dflags LHsExpr GhcTc
from Maybe (LHsExpr GhcTc)
forall a. Maybe a
Nothing LHsExpr GhcTc
to
       CoreExpr
expr' <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
expr
       CoreExpr
from' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
from
       CoreExpr
to'   <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
to
       CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
expr' [CoreExpr
from', CoreExpr
to']
dsArithSeq expr :: HsExpr GhcTc
expr (FromThen from :: LHsExpr GhcTc
from thn :: LHsExpr GhcTc
thn)
  = CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps (CoreExpr -> [CoreExpr] -> CoreExpr)
-> DsM CoreExpr
-> IOEnv (Env DsGblEnv DsLclEnv) ([CoreExpr] -> CoreExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
expr IOEnv (Env DsGblEnv DsLclEnv) ([CoreExpr] -> CoreExpr)
-> DsM [CoreExpr] -> DsM CoreExpr
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (LHsExpr GhcTc -> DsM CoreExpr)
-> [LHsExpr GhcTc] -> DsM [CoreExpr]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP [LHsExpr GhcTc
from, LHsExpr GhcTc
thn]
dsArithSeq expr :: HsExpr GhcTc
expr (FromThenTo from :: LHsExpr GhcTc
from thn :: LHsExpr GhcTc
thn to :: LHsExpr GhcTc
to)
  = do DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       DynFlags
-> LHsExpr GhcTc
-> Maybe (LHsExpr GhcTc)
-> LHsExpr GhcTc
-> IOEnv (Env DsGblEnv DsLclEnv) ()
warnAboutEmptyEnumerations DynFlags
dflags LHsExpr GhcTc
from (LHsExpr GhcTc -> Maybe (LHsExpr GhcTc)
forall a. a -> Maybe a
Just LHsExpr GhcTc
thn) LHsExpr GhcTc
to
       CoreExpr
expr' <- HsExpr GhcTc -> DsM CoreExpr
dsExpr HsExpr GhcTc
expr
       CoreExpr
from' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
from
       CoreExpr
thn'  <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
thn
       CoreExpr
to'   <- LHsExpr GhcTc -> DsM CoreExpr
dsLExprNoLP LHsExpr GhcTc
to
       CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ CoreExpr -> [CoreExpr] -> CoreExpr
forall b. Expr b -> [Expr b] -> Expr b
mkApps CoreExpr
expr' [CoreExpr
from', CoreExpr
thn', CoreExpr
to']

{-
Desugar 'do' and 'mdo' expressions (NOT list comprehensions, they're
handled in DsListComp).  Basically does the translation given in the
Haskell 98 report:
-}

dsDo :: [ExprLStmt GhcTc] -> DsM CoreExpr
dsDo :: [ExprLStmt GhcTc] -> DsM CoreExpr
dsDo stmts :: [ExprLStmt GhcTc]
stmts
  = [ExprLStmt GhcTc] -> DsM CoreExpr
goL [ExprLStmt GhcTc]
stmts
  where
    goL :: [ExprLStmt GhcTc] -> DsM CoreExpr
goL [] = String -> DsM CoreExpr
forall a. String -> a
panic "dsDo"
    goL ((ExprLStmt GhcTc -> Located (SrcSpanLess (ExprLStmt GhcTc))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L loc :: SrcSpan
loc stmt :: SrcSpanLess (ExprLStmt GhcTc)
stmt):lstmts :: [ExprLStmt GhcTc]
lstmts) = SrcSpan -> DsM CoreExpr -> DsM CoreExpr
forall a. SrcSpan -> DsM a -> DsM a
putSrcSpanDs SrcSpan
loc (SrcSpan
-> StmtLR GhcTc GhcTc (LHsExpr GhcTc)
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
go SrcSpan
loc SrcSpanLess (ExprLStmt GhcTc)
StmtLR GhcTc GhcTc (LHsExpr GhcTc)
stmt [ExprLStmt GhcTc]
lstmts)

    go :: SrcSpan
-> StmtLR GhcTc GhcTc (LHsExpr GhcTc)
-> [ExprLStmt GhcTc]
-> DsM CoreExpr
go _ (LastStmt _ body :: LHsExpr GhcTc
body _ _) stmts :: [ExprLStmt GhcTc]
stmts
      = ASSERT( null stmts ) dsLExpr body
        -- The 'return' op isn't used for 'do' expressions

    go _ (BodyStmt _ rhs :: LHsExpr GhcTc
rhs then_expr :: SyntaxExpr GhcTc
then_expr _) stmts :: [ExprLStmt GhcTc]
stmts
      = do { CoreExpr
rhs2 <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
rhs
           ; LHsExpr GhcTc -> Type -> IOEnv (Env DsGblEnv DsLclEnv) ()
warnDiscardedDoBindings LHsExpr GhcTc
rhs (CoreExpr -> Type
exprType CoreExpr
rhs2)
           ; CoreExpr
rest <- [ExprLStmt GhcTc] -> DsM CoreExpr
goL [ExprLStmt GhcTc]
stmts
           ; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
then_expr [CoreExpr
rhs2, CoreExpr
rest] }

    go _ (LetStmt _ binds :: LHsLocalBinds GhcTc
binds) stmts :: [ExprLStmt GhcTc]
stmts
      = do { CoreExpr
rest <- [ExprLStmt GhcTc] -> DsM CoreExpr
goL [ExprLStmt GhcTc]
stmts
           ; LHsLocalBinds GhcTc -> CoreExpr -> DsM CoreExpr
dsLocalBinds LHsLocalBinds GhcTc
binds CoreExpr
rest }

    go _ (BindStmt res1_ty :: XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
res1_ty pat :: LPat GhcTc
pat rhs :: LHsExpr GhcTc
rhs bind_op :: SyntaxExpr GhcTc
bind_op fail_op :: SyntaxExpr GhcTc
fail_op) stmts :: [ExprLStmt GhcTc]
stmts
      = do  { CoreExpr
body     <- [ExprLStmt GhcTc] -> DsM CoreExpr
goL [ExprLStmt GhcTc]
stmts
            ; CoreExpr
rhs'     <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
rhs
            ; CoreBndr
var   <- LPat GhcTc -> DsM CoreBndr
selectSimpleMatchVarL LPat GhcTc
pat
            ; MatchResult
match <- CoreBndr
-> HsMatchContext Name
-> LPat GhcTc
-> Type
-> MatchResult
-> IOEnv (Env DsGblEnv DsLclEnv) MatchResult
matchSinglePatVar CoreBndr
var (HsStmtContext Name -> HsMatchContext Name
forall id. HsStmtContext id -> HsMatchContext id
StmtCtxt HsStmtContext Name
forall id. HsStmtContext id
DoExpr) LPat GhcTc
pat
                                      Type
XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
res1_ty (CoreExpr -> MatchResult
cantFailMatchResult CoreExpr
body)
            ; CoreExpr
match_code <- LPat GhcTc -> MatchResult -> SyntaxExpr GhcTc -> DsM CoreExpr
handle_failure LPat GhcTc
pat MatchResult
match SyntaxExpr GhcTc
fail_op
            ; SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
bind_op [CoreExpr
rhs', CoreBndr -> CoreExpr -> CoreExpr
forall b. b -> Expr b -> Expr b
Lam CoreBndr
var CoreExpr
match_code] }

    go _ (ApplicativeStmt body_ty :: XApplicativeStmt GhcTc GhcTc (LHsExpr GhcTc)
body_ty args :: [(SyntaxExpr GhcTc, ApplicativeArg GhcTc)]
args mb_join :: Maybe (SyntaxExpr GhcTc)
mb_join) stmts :: [ExprLStmt GhcTc]
stmts
      = do {
             let
               (pats :: [LPat GhcTc]
pats, rhss :: [DsM CoreExpr]
rhss) = [(LPat GhcTc, DsM CoreExpr)] -> ([LPat GhcTc], [DsM CoreExpr])
forall a b. [(a, b)] -> ([a], [b])
unzip (((SyntaxExpr GhcTc, ApplicativeArg GhcTc)
 -> (LPat GhcTc, DsM CoreExpr))
-> [(SyntaxExpr GhcTc, ApplicativeArg GhcTc)]
-> [(LPat GhcTc, DsM CoreExpr)]
forall a b. (a -> b) -> [a] -> [b]
map (ApplicativeArg GhcTc -> (LPat GhcTc, DsM CoreExpr)
do_arg (ApplicativeArg GhcTc -> (LPat GhcTc, DsM CoreExpr))
-> ((SyntaxExpr GhcTc, ApplicativeArg GhcTc)
    -> ApplicativeArg GhcTc)
-> (SyntaxExpr GhcTc, ApplicativeArg GhcTc)
-> (LPat GhcTc, DsM CoreExpr)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SyntaxExpr GhcTc, ApplicativeArg GhcTc) -> ApplicativeArg GhcTc
forall a b. (a, b) -> b
snd) [(SyntaxExpr GhcTc, ApplicativeArg GhcTc)]
args)

               do_arg :: ApplicativeArg GhcTc -> (LPat GhcTc, DsM CoreExpr)
do_arg (ApplicativeArgOne _ pat :: LPat GhcTc
pat expr :: LHsExpr GhcTc
expr _) =
                 (LPat GhcTc
pat, LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
expr)
               do_arg (ApplicativeArgMany _ stmts :: [ExprLStmt GhcTc]
stmts ret :: HsExpr GhcTc
ret pat :: LPat GhcTc
pat) =
                 (LPat GhcTc
pat, [ExprLStmt GhcTc] -> DsM CoreExpr
dsDo ([ExprLStmt GhcTc]
stmts [ExprLStmt GhcTc] -> [ExprLStmt GhcTc] -> [ExprLStmt GhcTc]
forall a. [a] -> [a] -> [a]
++ [SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc)
-> SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcTc -> StmtLR GhcTc GhcTc (LHsExpr GhcTc)
forall (bodyR :: * -> *) (idR :: Pass) (idL :: Pass).
Located (bodyR (GhcPass idR))
-> StmtLR
     (GhcPass idL) (GhcPass idR) (Located (bodyR (GhcPass idR)))
mkLastStmt (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc SrcSpanLess (LHsExpr GhcTc)
HsExpr GhcTc
ret)]))
               do_arg (XApplicativeArg _) = String -> (LPat GhcTc, DsM CoreExpr)
forall a. String -> a
panic "dsDo"

               arg_tys :: [Type]
arg_tys = (LPat GhcTc -> Type) -> [LPat GhcTc] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map LPat GhcTc -> Type
hsLPatType [LPat GhcTc]
pats

           ; [CoreExpr]
rhss' <- [DsM CoreExpr] -> DsM [CoreExpr]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence [DsM CoreExpr]
rhss

           ; let body' :: LHsExpr GhcTc
body' = SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc)
-> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a b. (a -> b) -> a -> b
$ XDo GhcTc
-> HsStmtContext Name -> Located [ExprLStmt GhcTc] -> HsExpr GhcTc
forall p.
XDo p -> HsStmtContext Name -> Located [ExprLStmt p] -> HsExpr p
HsDo XApplicativeStmt GhcTc GhcTc (LHsExpr GhcTc)
XDo GhcTc
body_ty HsStmtContext Name
forall id. HsStmtContext id
DoExpr (SrcSpanLess (Located [ExprLStmt GhcTc])
-> Located [ExprLStmt GhcTc]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc [ExprLStmt GhcTc]
SrcSpanLess (Located [ExprLStmt GhcTc])
stmts)

           ; let fun :: LHsExpr GhcTc
fun = SrcSpan -> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
noSrcSpan (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc)
-> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a b. (a -> b) -> a -> b
$ XLam GhcTc -> MatchGroup GhcTc (LHsExpr GhcTc) -> HsExpr GhcTc
forall p. XLam p -> MatchGroup p (LHsExpr p) -> HsExpr p
HsLam XLam GhcTc
NoExt
noExt (MatchGroup GhcTc (LHsExpr GhcTc) -> SrcSpanLess (LHsExpr GhcTc))
-> MatchGroup GhcTc (LHsExpr GhcTc) -> SrcSpanLess (LHsExpr GhcTc)
forall a b. (a -> b) -> a -> b
$
                   MG :: forall p body.
XMG p body
-> Located [LMatch p body] -> Origin -> MatchGroup p body
MG { mg_alts :: Located [LMatch GhcTc (LHsExpr GhcTc)]
mg_alts = SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
-> Located [LMatch GhcTc (LHsExpr GhcTc)]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc [HsMatchContext (NameOrRdrName (IdP GhcTc))
-> [LPat GhcTc] -> LHsExpr GhcTc -> LMatch GhcTc (LHsExpr GhcTc)
forall (p :: Pass) (body :: * -> *).
HsMatchContext (NameOrRdrName (IdP (GhcPass p)))
-> [LPat (GhcPass p)]
-> Located (body (GhcPass p))
-> LMatch (GhcPass p) (Located (body (GhcPass p)))
mkSimpleMatch HsMatchContext (NameOrRdrName (IdP GhcTc))
forall id. HsMatchContext id
LambdaExpr [LPat GhcTc]
pats
                                                       LHsExpr GhcTc
body']
                      , mg_ext :: XMG GhcTc (LHsExpr GhcTc)
mg_ext = [Type] -> Type -> MatchGroupTc
MatchGroupTc [Type]
arg_tys Type
XApplicativeStmt GhcTc GhcTc (LHsExpr GhcTc)
body_ty
                      , mg_origin :: Origin
mg_origin = Origin
Generated }

           ; CoreExpr
fun' <- LHsExpr GhcTc -> DsM CoreExpr
dsLExpr LHsExpr GhcTc
fun
           ; let mk_ap_call :: CoreExpr -> (SyntaxExpr GhcTc, CoreExpr) -> DsM CoreExpr
mk_ap_call l :: CoreExpr
l (op :: SyntaxExpr GhcTc
op,r :: CoreExpr
r) = SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
op [CoreExpr
l,CoreExpr
r]
           ; CoreExpr
expr <- (CoreExpr -> (SyntaxExpr GhcTc, CoreExpr) -> DsM CoreExpr)
-> CoreExpr -> [(SyntaxExpr GhcTc, CoreExpr)] -> DsM CoreExpr
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> [b] -> m a
foldlM CoreExpr -> (SyntaxExpr GhcTc, CoreExpr) -> DsM CoreExpr
mk_ap_call CoreExpr
fun' ([SyntaxExpr GhcTc] -> [CoreExpr] -> [(SyntaxExpr GhcTc, CoreExpr)]
forall a b. [a] -> [b] -> [(a, b)]
zip (((SyntaxExpr GhcTc, ApplicativeArg GhcTc) -> SyntaxExpr GhcTc)
-> [(SyntaxExpr GhcTc, ApplicativeArg GhcTc)] -> [SyntaxExpr GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map (SyntaxExpr GhcTc, ApplicativeArg GhcTc) -> SyntaxExpr GhcTc
forall a b. (a, b) -> a
fst [(SyntaxExpr GhcTc, ApplicativeArg GhcTc)]
args) [CoreExpr]
rhss')
           ; case Maybe (SyntaxExpr GhcTc)
mb_join of
               Nothing -> CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
expr
               Just join_op :: SyntaxExpr GhcTc
join_op -> SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
join_op [CoreExpr
expr] }

    go loc :: SrcSpan
loc (RecStmt { recS_stmts :: forall idL idR body. StmtLR idL idR body -> [LStmtLR idL idR body]
recS_stmts = [ExprLStmt GhcTc]
rec_stmts, recS_later_ids :: forall idL idR body. StmtLR idL idR body -> [IdP idR]
recS_later_ids = [IdP GhcTc]
later_ids
                    , recS_rec_ids :: forall idL idR body. StmtLR idL idR body -> [IdP idR]
recS_rec_ids = [IdP GhcTc]
rec_ids, recS_ret_fn :: forall idL idR body. StmtLR idL idR body -> SyntaxExpr idR
recS_ret_fn = SyntaxExpr GhcTc
return_op
                    , recS_mfix_fn :: forall idL idR body. StmtLR idL idR body -> SyntaxExpr idR
recS_mfix_fn = SyntaxExpr GhcTc
mfix_op, recS_bind_fn :: forall idL idR body. StmtLR idL idR body -> SyntaxExpr idR
recS_bind_fn = SyntaxExpr GhcTc
bind_op
                    , recS_ext :: forall idL idR body. StmtLR idL idR body -> XRecStmt idL idR body
recS_ext = RecStmtTc
                        { recS_bind_ty = bind_ty
                        , recS_rec_rets = rec_rets
                        , recS_ret_ty = body_ty} }) stmts :: [ExprLStmt GhcTc]
stmts
      = [ExprLStmt GhcTc] -> DsM CoreExpr
goL (ExprLStmt GhcTc
new_bind_stmt ExprLStmt GhcTc -> [ExprLStmt GhcTc] -> [ExprLStmt GhcTc]
forall a. a -> [a] -> [a]
: [ExprLStmt GhcTc]
stmts)  -- rec_ids can be empty; eg  rec { print 'x' }
      where
        new_bind_stmt :: ExprLStmt GhcTc
new_bind_stmt = SrcSpan -> SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc
forall a. HasSrcSpan a => SrcSpan -> SrcSpanLess a -> a
cL SrcSpan
loc (SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc)
-> SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc
forall a b. (a -> b) -> a -> b
$ XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
-> LPat GhcTc
-> LHsExpr GhcTc
-> SyntaxExpr GhcTc
-> SyntaxExpr GhcTc
-> StmtLR GhcTc GhcTc (LHsExpr GhcTc)
forall idL idR body.
XBindStmt idL idR body
-> LPat idL
-> body
-> SyntaxExpr idR
-> SyntaxExpr idR
-> StmtLR idL idR body
BindStmt Type
XBindStmt GhcTc GhcTc (LHsExpr GhcTc)
bind_ty ([LPat GhcTc] -> LPat GhcTc
mkBigLHsPatTupId [LPat GhcTc]
later_pats)
                                         LHsExpr GhcTc
mfix_app SyntaxExpr GhcTc
bind_op
                                         SyntaxExpr GhcTc
forall (p :: Pass). SyntaxExpr (GhcPass p)
noSyntaxExpr  -- Tuple cannot fail

        tup_ids :: [CoreBndr]
tup_ids      = [CoreBndr]
[IdP GhcTc]
rec_ids [CoreBndr] -> [CoreBndr] -> [CoreBndr]
forall a. [a] -> [a] -> [a]
++ (CoreBndr -> Bool) -> [CoreBndr] -> [CoreBndr]
forall a. (a -> Bool) -> [a] -> [a]
filterOut (CoreBndr -> [CoreBndr] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [CoreBndr]
[IdP GhcTc]
rec_ids) [CoreBndr]
[IdP GhcTc]
later_ids
        tup_ty :: Type
tup_ty       = [Type] -> Type
mkBigCoreTupTy ((CoreBndr -> Type) -> [CoreBndr] -> [Type]
forall a b. (a -> b) -> [a] -> [b]
map CoreBndr -> Type
idType [CoreBndr]
tup_ids) -- Deals with singleton case
        rec_tup_pats :: [LPat GhcTc]
rec_tup_pats = (CoreBndr -> LPat GhcTc) -> [CoreBndr] -> [LPat GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map CoreBndr -> LPat GhcTc
forall (id :: Pass). IdP (GhcPass id) -> LPat (GhcPass id)
nlVarPat [CoreBndr]
tup_ids
        later_pats :: [LPat GhcTc]
later_pats   = [LPat GhcTc]
rec_tup_pats
        rets :: [LHsExpr GhcTc]
rets         = (HsExpr GhcTc -> LHsExpr GhcTc)
-> [HsExpr GhcTc] -> [LHsExpr GhcTc]
forall a b. (a -> b) -> [a] -> [b]
map HsExpr GhcTc -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc [HsExpr GhcTc]
rec_rets
        mfix_app :: LHsExpr GhcTc
mfix_app     = SyntaxExpr GhcTc -> [LHsExpr GhcTc] -> LHsExpr GhcTc
forall (id :: Pass).
SyntaxExpr (GhcPass id)
-> [LHsExpr (GhcPass id)] -> LHsExpr (GhcPass id)
nlHsSyntaxApps SyntaxExpr GhcTc
mfix_op [LHsExpr GhcTc
mfix_arg]
        mfix_arg :: LHsExpr GhcTc
mfix_arg     = SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc)
-> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a b. (a -> b) -> a -> b
$ XLam GhcTc -> MatchGroup GhcTc (LHsExpr GhcTc) -> HsExpr GhcTc
forall p. XLam p -> MatchGroup p (LHsExpr p) -> HsExpr p
HsLam XLam GhcTc
NoExt
noExt
                           (MG :: forall p body.
XMG p body
-> Located [LMatch p body] -> Origin -> MatchGroup p body
MG { mg_alts :: Located [LMatch GhcTc (LHsExpr GhcTc)]
mg_alts = SrcSpanLess (Located [LMatch GhcTc (LHsExpr GhcTc)])
-> Located [LMatch GhcTc (LHsExpr GhcTc)]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc [HsMatchContext (NameOrRdrName (IdP GhcTc))
-> [LPat GhcTc] -> LHsExpr GhcTc -> LMatch GhcTc (LHsExpr GhcTc)
forall (p :: Pass) (body :: * -> *).
HsMatchContext (NameOrRdrName (IdP (GhcPass p)))
-> [LPat (GhcPass p)]
-> Located (body (GhcPass p))
-> LMatch (GhcPass p) (Located (body (GhcPass p)))
mkSimpleMatch
                                                    HsMatchContext (NameOrRdrName (IdP GhcTc))
forall id. HsMatchContext id
LambdaExpr
                                                    [LPat GhcTc
mfix_pat] LHsExpr GhcTc
body]
                               , mg_ext :: XMG GhcTc (LHsExpr GhcTc)
mg_ext = [Type] -> Type -> MatchGroupTc
MatchGroupTc [Type
tup_ty] Type
body_ty
                               , mg_origin :: Origin
mg_origin = Origin
Generated })
        mfix_pat :: LPat GhcTc
mfix_pat     = SrcSpanLess (LPat GhcTc) -> LPat GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LPat GhcTc) -> LPat GhcTc)
-> SrcSpanLess (LPat GhcTc) -> LPat GhcTc
forall a b. (a -> b) -> a -> b
$ XLazyPat GhcTc -> LPat GhcTc -> LPat GhcTc
forall p. XLazyPat p -> Pat p -> Pat p
LazyPat XLazyPat GhcTc
NoExt
noExt (LPat GhcTc -> SrcSpanLess (LPat GhcTc))
-> LPat GhcTc -> SrcSpanLess (LPat GhcTc)
forall a b. (a -> b) -> a -> b
$ [LPat GhcTc] -> LPat GhcTc
mkBigLHsPatTupId [LPat GhcTc]
rec_tup_pats
        body :: LHsExpr GhcTc
body         = SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc)
-> SrcSpanLess (LHsExpr GhcTc) -> LHsExpr GhcTc
forall a b. (a -> b) -> a -> b
$ XDo GhcTc
-> HsStmtContext Name -> Located [ExprLStmt GhcTc] -> HsExpr GhcTc
forall p.
XDo p -> HsStmtContext Name -> Located [ExprLStmt p] -> HsExpr p
HsDo Type
XDo GhcTc
body_ty
                                HsStmtContext Name
forall id. HsStmtContext id
DoExpr (SrcSpanLess (Located [ExprLStmt GhcTc])
-> Located [ExprLStmt GhcTc]
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc ([ExprLStmt GhcTc]
rec_stmts [ExprLStmt GhcTc] -> [ExprLStmt GhcTc] -> [ExprLStmt GhcTc]
forall a. [a] -> [a] -> [a]
++ [ExprLStmt GhcTc
ret_stmt]))
        ret_app :: LHsExpr GhcTc
ret_app      = SyntaxExpr GhcTc -> [LHsExpr GhcTc] -> LHsExpr GhcTc
forall (id :: Pass).
SyntaxExpr (GhcPass id)
-> [LHsExpr (GhcPass id)] -> LHsExpr (GhcPass id)
nlHsSyntaxApps SyntaxExpr GhcTc
return_op [[LHsExpr GhcTc] -> LHsExpr GhcTc
mkBigLHsTupId [LHsExpr GhcTc]
rets]
        ret_stmt :: ExprLStmt GhcTc
ret_stmt     = SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc)
-> SrcSpanLess (ExprLStmt GhcTc) -> ExprLStmt GhcTc
forall a b. (a -> b) -> a -> b
$ LHsExpr GhcTc -> StmtLR GhcTc GhcTc (LHsExpr GhcTc)
forall (bodyR :: * -> *) (idR :: Pass) (idL :: Pass).
Located (bodyR (GhcPass idR))
-> StmtLR
     (GhcPass idL) (GhcPass idR) (Located (bodyR (GhcPass idR)))
mkLastStmt LHsExpr GhcTc
ret_app
                     -- This LastStmt will be desugared with dsDo,
                     -- which ignores the return_op in the LastStmt,
                     -- so we must apply the return_op explicitly

    go _ (ParStmt   {}) _ = String -> DsM CoreExpr
forall a. String -> a
panic "dsDo ParStmt"
    go _ (TransStmt {}) _ = String -> DsM CoreExpr
forall a. String -> a
panic "dsDo TransStmt"
    go _ (XStmtLR   {}) _ = String -> DsM CoreExpr
forall a. String -> a
panic "dsDo XStmtLR"

handle_failure :: LPat GhcTc -> MatchResult -> SyntaxExpr GhcTc -> DsM CoreExpr
    -- In a do expression, pattern-match failure just calls
    -- the monadic 'fail' rather than throwing an exception
handle_failure :: LPat GhcTc -> MatchResult -> SyntaxExpr GhcTc -> DsM CoreExpr
handle_failure pat :: LPat GhcTc
pat match :: MatchResult
match fail_op :: SyntaxExpr GhcTc
fail_op
  | MatchResult -> Bool
matchCanFail MatchResult
match
  = do { DynFlags
dflags <- IOEnv (Env DsGblEnv DsLclEnv) DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
       ; CoreExpr
fail_msg <- String -> DsM CoreExpr
forall (m :: * -> *). MonadThings m => String -> m CoreExpr
mkStringExpr (DynFlags -> LPat GhcTc -> String
forall e. HasSrcSpan e => DynFlags -> e -> String
mk_fail_msg DynFlags
dflags LPat GhcTc
pat)
       ; CoreExpr
fail_expr <- SyntaxExpr GhcTc -> [CoreExpr] -> DsM CoreExpr
dsSyntaxExpr SyntaxExpr GhcTc
fail_op [CoreExpr
fail_msg]
       ; MatchResult -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult
match CoreExpr
fail_expr }
  | Bool
otherwise
  = MatchResult -> CoreExpr -> DsM CoreExpr
extractMatchResult MatchResult
match (String -> CoreExpr
forall a. HasCallStack => String -> a
error "It can't fail")

mk_fail_msg :: HasSrcSpan e => DynFlags -> e -> String
mk_fail_msg :: DynFlags -> e -> String
mk_fail_msg dflags :: DynFlags
dflags pat :: e
pat = "Pattern match failure in do expression at " String -> String -> String
forall a. [a] -> [a] -> [a]
++
                         DynFlags -> SrcSpan -> String
forall a. Outputable a => DynFlags -> a -> String
showPpr DynFlags
dflags (e -> SrcSpan
forall a. HasSrcSpan a => a -> SrcSpan
getLoc e
pat)

{-
************************************************************************
*                                                                      *
   Desugaring Variables
*                                                                      *
************************************************************************
-}

dsHsVar :: Bool  -- are we directly inside an HsWrap?
                 -- See Wrinkle in Note [Detecting forced eta expansion]
        -> Id -> DsM CoreExpr
dsHsVar :: Bool -> CoreBndr -> DsM CoreExpr
dsHsVar w :: Bool
w var :: CoreBndr
var
  | Bool -> Bool
not Bool
w
  , let bad_tys :: [Type]
bad_tys = CoreBndr -> Type -> [Type]
badUseOfLevPolyPrimop CoreBndr
var Type
ty
  , Bool -> Bool
not ([Type] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Type]
bad_tys)
  = do { CoreBndr -> Type -> [Type] -> IOEnv (Env DsGblEnv DsLclEnv) ()
levPolyPrimopErr CoreBndr
var Type
ty [Type]
bad_tys
       ; CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return CoreExpr
unitExpr }  -- return something eminently safe

  | Bool
otherwise
  = CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreBndr -> CoreExpr
forall b. CoreBndr -> Expr b
varToCoreExpr CoreBndr
var)   -- See Note [Desugaring vars]

  where
    ty :: Type
ty = CoreBndr -> Type
idType CoreBndr
var

dsConLike :: Bool  -- as in dsHsVar
          -> ConLike -> DsM CoreExpr
dsConLike :: Bool -> ConLike -> DsM CoreExpr
dsConLike w :: Bool
w (RealDataCon dc :: DataCon
dc) = Bool -> CoreBndr -> DsM CoreExpr
dsHsVar Bool
w (DataCon -> CoreBndr
dataConWrapId DataCon
dc)
dsConLike _ (PatSynCon ps :: PatSyn
ps)   = CoreExpr -> DsM CoreExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (CoreExpr -> DsM CoreExpr) -> CoreExpr -> DsM CoreExpr
forall a b. (a -> b) -> a -> b
$ case PatSyn -> Maybe (CoreBndr, Bool)
patSynBuilder PatSyn
ps of
  Just (id :: CoreBndr
id, add_void :: Bool
add_void)
    | Bool
add_void  -> SDoc -> CoreExpr -> CoreExpr -> CoreExpr
mkCoreApp (String -> SDoc
text "dsConLike" SDoc -> SDoc -> SDoc
<+> PatSyn -> SDoc
forall a. Outputable a => a -> SDoc
ppr PatSyn
ps) (CoreBndr -> CoreExpr
forall b. CoreBndr -> Expr b
Var CoreBndr
id) (CoreBndr -> CoreExpr
forall b. CoreBndr -> Expr b
Var CoreBndr
voidPrimId)
    | Bool
otherwise -> CoreBndr -> CoreExpr
forall b. CoreBndr -> Expr b
Var CoreBndr
id
  _ -> String -> SDoc -> CoreExpr
forall a. HasCallStack => String -> SDoc -> a
pprPanic "dsConLike" (PatSyn -> SDoc
forall a. Outputable a => a -> SDoc
ppr PatSyn
ps)

{-
************************************************************************
*                                                                      *
\subsection{Errors and contexts}
*                                                                      *
************************************************************************
-}

-- Warn about certain types of values discarded in monadic bindings (#3263)
warnDiscardedDoBindings :: LHsExpr GhcTc -> Type -> DsM ()
warnDiscardedDoBindings :: LHsExpr GhcTc -> Type -> IOEnv (Env DsGblEnv DsLclEnv) ()
warnDiscardedDoBindings rhs :: LHsExpr GhcTc
rhs rhs_ty :: Type
rhs_ty
  | Just (m_ty :: Type
m_ty, elt_ty :: Type
elt_ty) <- Type -> Maybe (Type, Type)
tcSplitAppTy_maybe Type
rhs_ty
  = do { Bool
warn_unused <- WarningFlag -> TcRnIf DsGblEnv DsLclEnv Bool
forall gbl lcl. WarningFlag -> TcRnIf gbl lcl Bool
woptM WarningFlag
Opt_WarnUnusedDoBind
       ; Bool
warn_wrong <- WarningFlag -> TcRnIf DsGblEnv DsLclEnv Bool
forall gbl lcl. WarningFlag -> TcRnIf gbl lcl Bool
woptM WarningFlag
Opt_WarnWrongDoBind
       ; Bool
-> IOEnv (Env DsGblEnv DsLclEnv) ()
-> IOEnv (Env DsGblEnv DsLclEnv) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
warn_unused Bool -> Bool -> Bool
|| Bool
warn_wrong) (IOEnv (Env DsGblEnv DsLclEnv) ()
 -> IOEnv (Env DsGblEnv DsLclEnv) ())
-> IOEnv (Env DsGblEnv DsLclEnv) ()
-> IOEnv (Env DsGblEnv DsLclEnv) ()
forall a b. (a -> b) -> a -> b
$
    do { FamInstEnvs
fam_inst_envs <- DsM FamInstEnvs
dsGetFamInstEnvs
       ; let norm_elt_ty :: Type
norm_elt_ty = FamInstEnvs -> Type -> Type
topNormaliseType FamInstEnvs
fam_inst_envs Type
elt_ty

           -- Warn about discarding non-() things in 'monadic' binding
       ; if Bool
warn_unused Bool -> Bool -> Bool
&& Bool -> Bool
not (Type -> Bool
isUnitTy Type
norm_elt_ty)
         then WarnReason -> SDoc -> IOEnv (Env DsGblEnv DsLclEnv) ()
warnDs (WarningFlag -> WarnReason
Reason WarningFlag
Opt_WarnUnusedDoBind)
                     (LHsExpr GhcTc -> Type -> SDoc
badMonadBind LHsExpr GhcTc
rhs Type
elt_ty)
         else

           -- Warn about discarding m a things in 'monadic' binding of the same type,
           -- but only if we didn't already warn due to Opt_WarnUnusedDoBind
           Bool
-> IOEnv (Env DsGblEnv DsLclEnv) ()
-> IOEnv (Env DsGblEnv DsLclEnv) ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
warn_wrong (IOEnv (Env DsGblEnv DsLclEnv) ()
 -> IOEnv (Env DsGblEnv DsLclEnv) ())
-> IOEnv (Env DsGblEnv DsLclEnv) ()
-> IOEnv (Env DsGblEnv DsLclEnv) ()
forall a b. (a -> b) -> a -> b
$
                do { case Type -> Maybe (Type, Type)
tcSplitAppTy_maybe Type
norm_elt_ty of
                         Just (elt_m_ty :: Type
elt_m_ty, _)
                            | Type
m_ty Type -> Type -> Bool
`eqType` FamInstEnvs -> Type -> Type
topNormaliseType FamInstEnvs
fam_inst_envs Type
elt_m_ty
                            -> WarnReason -> SDoc -> IOEnv (Env DsGblEnv DsLclEnv) ()
warnDs (WarningFlag -> WarnReason
Reason WarningFlag
Opt_WarnWrongDoBind)
                                      (LHsExpr GhcTc -> Type -> SDoc
badMonadBind LHsExpr GhcTc
rhs Type
elt_ty)
                         _ -> () -> IOEnv (Env DsGblEnv DsLclEnv) ()
forall (m :: * -> *) a. Monad m => a -> m a
return () } } }

  | Bool
otherwise   -- RHS does have type of form (m ty), which is weird
  = () -> IOEnv (Env DsGblEnv DsLclEnv) ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()   -- but at lesat this warning is irrelevant

badMonadBind :: LHsExpr GhcTc -> Type -> SDoc
badMonadBind :: LHsExpr GhcTc -> Type -> SDoc
badMonadBind rhs :: LHsExpr GhcTc
rhs elt_ty :: Type
elt_ty
  = [SDoc] -> SDoc
vcat [ SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "A do-notation statement discarded a result of type")
              2 (SDoc -> SDoc
quotes (Type -> SDoc
forall a. Outputable a => a -> SDoc
ppr Type
elt_ty))
         , SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "Suppress this warning by saying")
              2 (SDoc -> SDoc
quotes (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> SDoc
text "_ <-" SDoc -> SDoc -> SDoc
<+> LHsExpr GhcTc -> SDoc
forall a. Outputable a => a -> SDoc
ppr LHsExpr GhcTc
rhs)
         ]

{-
************************************************************************
*                                                                      *
   Forced eta expansion and levity polymorphism
*                                                                      *
************************************************************************

Note [Detecting forced eta expansion]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We cannot have levity polymorphic function arguments. See
Note [Levity polymorphism invariants] in CoreSyn. But we *can* have
functions that take levity polymorphism arguments, as long as these
functions are eta-reduced. (See #12708 for an example.)

However, we absolutely cannot do this for functions that have no
binding (i.e., say True to Id.hasNoBinding), like primops and unboxed
tuple constructors. These get eta-expanded in CorePrep.maybeSaturate.

Detecting when this is about to happen is a bit tricky, though. When
the desugarer is looking at the Id itself (let's be concrete and
suppose we have (#,#)), we don't know whether it will be levity
polymorphic. So the right spot seems to be to look after the Id has
been applied to its type arguments. To make the algorithm efficient,
it's important to be able to spot ((#,#) @a @b @c @d) without looking
past all the type arguments. We thus require that
  * The body of an HsWrap is not an HsWrap.
With that representation invariant, we simply look inside every HsWrap
to see if its body is an HsVar whose Id hasNoBinding. Then, we look
at the wrapped type. If it has any levity polymorphic arguments, reject.

Interestingly, this approach does not look to see whether the Id in
question will be eta expanded. The logic is this:
  * Either the Id in question is saturated or not.
  * If it is, then it surely can't have levity polymorphic arguments.
    If its wrapped type contains levity polymorphic arguments, reject.
  * If it's not, then it can't be eta expanded with levity polymorphic
    argument. If its wrapped type contains levity polymorphic arguments, reject.
So, either way, we're good to reject.

Wrinkle
~~~~~~~
Not all polymorphic Ids are wrapped in
HsWrap, due to the lazy instantiation of TypeApplications. (See "Visible type
application", ESOP '16.) But if we spot a levity-polymorphic hasNoBinding Id
without a wrapper, then that is surely problem and we can reject.

We thus have a parameter to `dsExpr` that tracks whether or not we are
directly in an HsWrap. If we find a levity-polymorphic hasNoBinding Id when
we're not directly in an HsWrap, reject.

-}

-- | Takes an expression and its instantiated type. If the expression is an
-- HsVar with a hasNoBinding primop and the type has levity-polymorphic arguments,
-- issue an error. See Note [Detecting forced eta expansion]
checkForcedEtaExpansion :: HsExpr GhcTc -> Type -> DsM ()
checkForcedEtaExpansion :: HsExpr GhcTc -> Type -> IOEnv (Env DsGblEnv DsLclEnv) ()
checkForcedEtaExpansion expr :: HsExpr GhcTc
expr ty :: Type
ty
  | Just var :: CoreBndr
var <- case HsExpr GhcTc
expr of
                  HsVar _ (Located (IdP GhcTc) -> Located (SrcSpanLess (Located CoreBndr))
forall a. HasSrcSpan a => a -> Located (SrcSpanLess a)
dL->L _ var :: SrcSpanLess (Located CoreBndr)
var)           -> CoreBndr -> Maybe CoreBndr
forall a. a -> Maybe a
Just SrcSpanLess (Located CoreBndr)
CoreBndr
var
                  HsConLikeOut _ (RealDataCon dc :: DataCon
dc) -> CoreBndr -> Maybe CoreBndr
forall a. a -> Maybe a
Just (DataCon -> CoreBndr
dataConWrapId DataCon
dc)
                  _                               -> Maybe CoreBndr
forall a. Maybe a
Nothing
  , let bad_tys :: [Type]
bad_tys = CoreBndr -> Type -> [Type]
badUseOfLevPolyPrimop CoreBndr
var Type
ty
  , Bool -> Bool
not ([Type] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Type]
bad_tys)
  = CoreBndr -> Type -> [Type] -> IOEnv (Env DsGblEnv DsLclEnv) ()
levPolyPrimopErr CoreBndr
var Type
ty [Type]
bad_tys
checkForcedEtaExpansion _ _ = () -> IOEnv (Env DsGblEnv DsLclEnv) ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- | Is this a hasNoBinding Id with a levity-polymorphic type?
-- Returns the arguments that are levity polymorphic if they are bad;
-- or an empty list otherwise
-- See Note [Detecting forced eta expansion]
badUseOfLevPolyPrimop :: Id -> Type -> [Type]
badUseOfLevPolyPrimop :: CoreBndr -> Type -> [Type]
badUseOfLevPolyPrimop id :: CoreBndr
id ty :: Type
ty
  | CoreBndr -> Bool
hasNoBinding CoreBndr
id
  = (Type -> Bool) -> [Type] -> [Type]
forall a. (a -> Bool) -> [a] -> [a]
filter Type -> Bool
isTypeLevPoly [Type]
arg_tys
  | Bool
otherwise
  = []
  where
    (binders :: [TyCoBinder]
binders, _) = Type -> ([TyCoBinder], Type)
splitPiTys Type
ty
    arg_tys :: [Type]
arg_tys      = (TyCoBinder -> Maybe Type) -> [TyCoBinder] -> [Type]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe TyCoBinder -> Maybe Type
binderRelevantType_maybe [TyCoBinder]
binders

levPolyPrimopErr :: Id -> Type -> [Type] -> DsM ()
levPolyPrimopErr :: CoreBndr -> Type -> [Type] -> IOEnv (Env DsGblEnv DsLclEnv) ()
levPolyPrimopErr primop :: CoreBndr
primop ty :: Type
ty bad_tys :: [Type]
bad_tys
  = SDoc -> IOEnv (Env DsGblEnv DsLclEnv) ()
errDs (SDoc -> IOEnv (Env DsGblEnv DsLclEnv) ())
-> SDoc -> IOEnv (Env DsGblEnv DsLclEnv) ()
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
vcat [ SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "Cannot use primitive with levity-polymorphic arguments:")
                      2 (CoreBndr -> SDoc
forall a. Outputable a => a -> SDoc
ppr CoreBndr
primop SDoc -> SDoc -> SDoc
<+> SDoc
dcolon SDoc -> SDoc -> SDoc
<+> Type -> SDoc
pprWithTYPE Type
ty)
                 , SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "Levity-polymorphic arguments:")
                      2 ([SDoc] -> SDoc
vcat ((Type -> SDoc) -> [Type] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (\t :: Type
t -> Type -> SDoc
pprWithTYPE Type
t SDoc -> SDoc -> SDoc
<+> SDoc
dcolon SDoc -> SDoc -> SDoc
<+> Type -> SDoc
pprWithTYPE (HasDebugCallStack => Type -> Type
Type -> Type
typeKind Type
t)) [Type]
bad_tys)) ]