-- Copyright 2019 Google LLC
--
-- Use of this source code is governed by a BSD-style
-- license that can be found in the LICENSE file or at
-- https://developers.google.com/open-source/licenses/bsd

{-# LANGUAGE CPP #-}
module GHC.SourceGen.Binds.Internal where

#if MIN_VERSION_ghc(9,0,0)
import GHC.Types.Basic (Origin(Generated))
import GHC.Data.Bag (listToBag)
#else
import BasicTypes (Origin(Generated))
import Bag (listToBag)
#endif
import GHC.Hs.Binds
import GHC.Hs.Decls
import GHC.Hs.Expr (MatchGroup(..), Match(..), GRHSs(..))

#if !MIN_VERSION_ghc(8,6,0)
import PlaceHolder (PlaceHolder(..))
#endif

import GHC.SourceGen.Pat.Internal (parenthesize)
import GHC.SourceGen.Syntax.Internal

-- | A binding definition inside of a @let@ or @where@ clause.
--
-- 'RawValBind' definitions may be constructed using its instance of
-- 'HasValBind'.  For more details, see the documentation of that function, and
-- of "GHC.SourceGen.Binds" overall.
data RawValBind
    = SigV Sig'
    | BindV HsBind'

valBinds :: [RawValBind] -> HsLocalBinds'
-- This case prevents GHC from printing an empty "where" clause:
valBinds :: [RawValBind] -> HsLocalBinds'
valBinds [] = (NoExtField -> HsLocalBinds') -> HsLocalBinds'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> HsLocalBinds'
forall idL idR. XEmptyLocalBinds idL idR -> HsLocalBindsLR idL idR
EmptyLocalBinds
valBinds [RawValBind]
vbs =
    (NoExtField -> HsValBindsLR GhcPs GhcPs -> HsLocalBinds')
-> HsValBindsLR GhcPs GhcPs -> HsLocalBinds'
forall a. (NoExtField -> a) -> a
withEpAnnNotUsed NoExtField -> HsValBindsLR GhcPs GhcPs -> HsLocalBinds'
forall idL idR.
XHsValBinds idL idR
-> HsValBindsLR idL idR -> HsLocalBindsLR idL idR
HsValBinds
#if MIN_VERSION_ghc(8,6,0)
        (HsValBindsLR GhcPs GhcPs -> HsLocalBinds')
-> HsValBindsLR GhcPs GhcPs -> HsLocalBinds'
forall a b. (a -> b) -> a -> b
$ (NoExtField
 -> LHsBindsLR GhcPs GhcPs
 -> [LSig GhcPs]
 -> HsValBindsLR GhcPs GhcPs)
-> LHsBindsLR GhcPs GhcPs
-> [LSig GhcPs]
-> HsValBindsLR GhcPs GhcPs
forall a. (NoExtField -> a) -> a
withNoAnnSortKey NoExtField
-> LHsBindsLR GhcPs GhcPs
-> [LSig GhcPs]
-> HsValBindsLR GhcPs GhcPs
forall idL idR.
XValBinds idL idR
-> LHsBindsLR idL idR -> [LSig idR] -> HsValBindsLR idL idR
ValBinds
#else
        $ noExt ValBindsIn
#endif
            ([GenLocated (SrcSpanAnn Any) HsBind'] -> LHsBindsLR GhcPs GhcPs
forall a. [a] -> Bag a
listToBag ([GenLocated (SrcSpanAnn Any) HsBind'] -> LHsBindsLR GhcPs GhcPs)
-> [GenLocated (SrcSpanAnn Any) HsBind'] -> LHsBindsLR GhcPs GhcPs
forall a b. (a -> b) -> a -> b
$ (HsBind' -> GenLocated (SrcSpanAnn Any) HsBind')
-> [HsBind'] -> [GenLocated (SrcSpanAnn Any) HsBind']
forall a b. (a -> b) -> [a] -> [b]
map HsBind' -> GenLocated (SrcSpanAnn Any) HsBind'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated [HsBind']
binds)
            ((Sig' -> LSig GhcPs) -> [Sig'] -> [LSig GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map Sig' -> LSig GhcPs
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated [Sig']
sigs)
  where
    sigs :: [Sig']
sigs = [Sig'
s | SigV Sig'
s <- [RawValBind]
vbs]
    binds :: [HsBind']
binds = [HsBind'
b | BindV HsBind'
b <- [RawValBind]
vbs]

-- | A single function pattern match, including an optional "where" clause.
--
-- For example:
--
-- > f x
-- >    | cond = y
-- >    | otherwise = z
-- >  where
-- >    y = ...
-- >    z = ...
data RawMatch = RawMatch
    { RawMatch -> [Pat']
rawMatchPats :: [Pat']
    , RawMatch -> RawGRHSs
rawMatchGRHSs :: RawGRHSs
    }

-- | A set of match guards plus an optional "where" clause.
--
-- This type is used in matches and in multi-way if expressions.
--
-- For example:
--
-- >    | cond = y
-- >    | otherwise = z
-- >  where
-- >    y = ...
-- >    z = ...
data RawGRHSs = RawGRHSs
    { RawGRHSs -> [GuardedExpr]
rawGRHSs :: [GuardedExpr]
    , RawGRHSs -> [RawValBind]
rawGRHSWhere :: [RawValBind]
    }

matchGroup :: HsMatchContext' -> [RawMatch] -> MatchGroup' LHsExpr'
matchGroup :: HsMatchContext' -> [RawMatch] -> MatchGroup' LHsExpr'
matchGroup HsMatchContext'
context [RawMatch]
matches =
    (NoExtField
 -> Located [LMatch GhcPs LHsExpr']
 -> Origin
 -> MatchGroup' LHsExpr')
-> Located [LMatch GhcPs LHsExpr']
-> Origin
-> MatchGroup' LHsExpr'
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> Located [LMatch GhcPs LHsExpr']
-> Origin
-> MatchGroup' LHsExpr'
forall p body.
XMG p body
-> Located [LMatch p body] -> Origin -> MatchGroup p body
MG ([LMatch GhcPs LHsExpr'] -> Located [LMatch GhcPs LHsExpr']
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated ([LMatch GhcPs LHsExpr'] -> Located [LMatch GhcPs LHsExpr'])
-> [LMatch GhcPs LHsExpr'] -> Located [LMatch GhcPs LHsExpr']
forall a b. (a -> b) -> a -> b
$ (RawMatch -> LMatch GhcPs LHsExpr')
-> [RawMatch] -> [LMatch GhcPs LHsExpr']
forall a b. (a -> b) -> [a] -> [b]
map (Match' LHsExpr' -> LMatch GhcPs LHsExpr'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated (Match' LHsExpr' -> LMatch GhcPs LHsExpr')
-> (RawMatch -> Match' LHsExpr')
-> RawMatch
-> LMatch GhcPs LHsExpr'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RawMatch -> Match' LHsExpr'
mkMatch) [RawMatch]
matches)
#if !MIN_VERSION_ghc(8,6,0)
                            [] PlaceHolder
#endif
                            Origin
Generated
  where
    mkMatch :: RawMatch -> Match' LHsExpr'
    mkMatch :: RawMatch -> Match' LHsExpr'
mkMatch RawMatch
r = (NoExtField
 -> HsMatchContext'
 -> [Located Pat']
 -> GRHSs' LHsExpr'
 -> Match' LHsExpr')
-> HsMatchContext'
-> [Located Pat']
-> GRHSs' LHsExpr'
-> Match' LHsExpr'
forall a. (NoExtField -> a) -> a
withEpAnnNotUsed NoExtField
-> HsMatchContext'
-> [Located Pat']
-> GRHSs' LHsExpr'
-> Match' LHsExpr'
forall p body.
XCMatch p body
-> HsMatchContext (NameOrRdrName (IdP p))
-> [LPat p]
-> GRHSs p body
-> Match p body
Match HsMatchContext'
context
                    ((Pat' -> Located Pat') -> [Pat'] -> [Located Pat']
forall a b. (a -> b) -> [a] -> [b]
map Pat' -> LPat'
Pat' -> Located Pat'
builtPat ([Pat'] -> [Located Pat']) -> [Pat'] -> [Located Pat']
forall a b. (a -> b) -> a -> b
$ (Pat' -> Pat') -> [Pat'] -> [Pat']
forall a b. (a -> b) -> [a] -> [b]
map Pat' -> Pat'
parenthesize ([Pat'] -> [Pat']) -> [Pat'] -> [Pat']
forall a b. (a -> b) -> a -> b
$ RawMatch -> [Pat']
rawMatchPats RawMatch
r)
                    (RawGRHSs -> GRHSs' LHsExpr'
mkGRHSs (RawGRHSs -> GRHSs' LHsExpr') -> RawGRHSs -> GRHSs' LHsExpr'
forall a b. (a -> b) -> a -> b
$ RawMatch -> RawGRHSs
rawMatchGRHSs RawMatch
r)

mkGRHSs :: RawGRHSs -> GRHSs' LHsExpr'
mkGRHSs :: RawGRHSs -> GRHSs' LHsExpr'
mkGRHSs RawGRHSs
g = (NoExtField
 -> [LGRHS GhcPs LHsExpr']
 -> LHsLocalBinds GhcPs
 -> GRHSs' LHsExpr')
-> [LGRHS GhcPs LHsExpr'] -> LHsLocalBinds GhcPs -> GRHSs' LHsExpr'
forall a. (NoExtField -> a) -> a
withEmptyEpAnnComments NoExtField
-> [LGRHS GhcPs LHsExpr'] -> LHsLocalBinds GhcPs -> GRHSs' LHsExpr'
forall p body.
XCGRHSs p body -> [LGRHS p body] -> LHsLocalBinds p -> GRHSs p body
GRHSs
                ((GuardedExpr -> LGRHS GhcPs LHsExpr')
-> [GuardedExpr] -> [LGRHS GhcPs LHsExpr']
forall a b. (a -> b) -> [a] -> [b]
map GuardedExpr -> LGRHS GhcPs LHsExpr'
forall e. e -> Located e
builtLoc ([GuardedExpr] -> [LGRHS GhcPs LHsExpr'])
-> [GuardedExpr] -> [LGRHS GhcPs LHsExpr']
forall a b. (a -> b) -> a -> b
$ RawGRHSs -> [GuardedExpr]
rawGRHSs RawGRHSs
g)
                (HsLocalBinds' -> LHsLocalBinds GhcPs
forall e. e -> Located e
fromLocalBinds (HsLocalBinds' -> LHsLocalBinds GhcPs)
-> HsLocalBinds' -> LHsLocalBinds GhcPs
forall a b. (a -> b) -> a -> b
$ [RawValBind] -> HsLocalBinds'
valBinds ([RawValBind] -> HsLocalBinds') -> [RawValBind] -> HsLocalBinds'
forall a b. (a -> b) -> a -> b
$ RawGRHSs -> [RawValBind]
rawGRHSWhere RawGRHSs
g)
  where
#if MIN_VERSION_ghc(9,2,0)
    fromLocalBinds = id
#else
    fromLocalBinds :: e -> Located e
fromLocalBinds = e -> Located e
forall e. e -> Located e
builtLoc
#endif

-- | An expression with a single guard.
--
-- For example:
--
-- > | otherwise = ()
type GuardedExpr = GRHS' LHsExpr'

-- | Syntax types which can declare/define functions.  For example:
-- declarations, or the body of a class declaration or class instance.
--
-- To declare the type of a function or value, use
-- 'GHC.SourceGen.Binds.typeSig' or 'GHC.SourceGen.Binds.typeSigs'.
--
-- To define a function, use 
-- 'GHC.SourceGen.Binds.funBind' or 'GHC.SourceGen.Binds.funBinds'.
--
-- To define a value, use
-- 'GHC.SourceGen.Binds.valBind' or 'GHC.SourceGen.Binds.valBindGuarded'.
class HasValBind t where
    sigB :: Sig' -> t
    bindB :: HsBind' -> t

instance HasValBind HsDecl' where
    sigB :: Sig' -> HsDecl'
sigB = (NoExtField -> Sig' -> HsDecl') -> Sig' -> HsDecl'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> Sig' -> HsDecl'
forall p. XSigD p -> Sig p -> HsDecl p
SigD
    bindB :: HsBind' -> HsDecl'
bindB = (NoExtField -> HsBind' -> HsDecl') -> HsBind' -> HsDecl'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> HsBind' -> HsDecl'
forall p. XValD p -> HsBind p -> HsDecl p
ValD

instance HasValBind RawValBind where
    sigB :: Sig' -> RawValBind
sigB = Sig' -> RawValBind
SigV
    bindB :: HsBind' -> RawValBind
bindB = HsBind' -> RawValBind
BindV