-- 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 #-}
-- | This module provides combinators for constructing Haskell declarations.
module GHC.SourceGen.Decl
    ( HsDecl'
      -- * Type declarations
    , type'
    , newtype'
    , data'
      -- ** Data constructors
    , ConDecl'
    , prefixCon
    , infixCon
    , recordCon
    , Field
    , field
    , strict
    , lazy
      -- ** Deriving clauses
    , HsDerivingClause'
    , deriving'
    , derivingStock
    , derivingAnyclass
    , derivingNewtype
#if MIN_VERSION_ghc(8,6,0)
    , derivingVia
#endif
    , standaloneDeriving
    , standaloneDerivingStock
    , standaloneDerivingNewtype
    , standaloneDerivingAnyclass
      -- * Class declarations
    , class'
    , ClassDecl
    , funDep
      -- * Instance declarations
    , instance'
    , RawInstDecl
    , HasTyFamInst(..)
    , tyFamInst
      -- * Pattern synonyms
    , patSynSigs
    , patSynSig
    , patSynBind
    ) where

#if MIN_VERSION_ghc(9,0,0)
import GHC.Types.Basic (LexicalFixity(Prefix))
import GHC.Data.Bag (listToBag)
import GHC.Types.SrcLoc (Located, LayoutInfo(..))
#else
import BasicTypes (LexicalFixity(Prefix))
import Bag (listToBag)
import SrcLoc (Located)
#endif
#if !MIN_VERSION_ghc(8,6,0)
import BasicTypes (DerivStrategy(..))
#endif
import GHC.Hs.Binds
import GHC.Hs.Decls

import GHC.Hs.Type
    ( ConDeclField(..)
    , FieldOcc(..)
    , HsConDetails(..)
    , HsImplicitBndrs (..)
    , HsSrcBang(..)
    , HsType(..)
#if MIN_VERSION_ghc(8,6,0)
    , HsWildCardBndrs (..)
#endif
#if MIN_VERSION_ghc(8,8,0)
    , HsArg(..)
#endif
    , SrcStrictness(..)
    , SrcUnpackedness(..)
#if MIN_VERSION_ghc(9,0,0)
    , hsUnrestricted
#endif
    )

#if MIN_VERSION_ghc(8,10,0)
import GHC.Hs.Extension (NoExtField(NoExtField))
#elif MIN_VERSION_ghc(8,6,0)
import GHC.Hs.Extension (NoExt(NoExt))
#else
import PlaceHolder (PlaceHolder(..))
#endif

import GHC.SourceGen.Binds.Internal
import GHC.SourceGen.Lit.Internal (noSourceText)
import GHC.SourceGen.Name
import GHC.SourceGen.Name.Internal
import GHC.SourceGen.Syntax.Internal
import GHC.SourceGen.Type.Internal

-- | A definition that can appear in the body of a @class@ declaration.
--
-- 'ClassDecl' definitions may be constructed using 'funDep' or using the
-- instance of 'HasValBind'.  For more details, see the documentation of
-- that function, and of "GHC.SourceGen.Binds" overall.
data ClassDecl
    = ClassSig Sig'
    | ClassDefaultMethod HsBind'
    | ClassFunDep [RdrNameStr] [RdrNameStr]
    -- TODO: type families

instance HasValBind ClassDecl where
    sigB :: Sig' -> ClassDecl
sigB = Sig' -> ClassDecl
ClassSig
    bindB :: HsBind' -> ClassDecl
bindB = HsBind' -> ClassDecl
ClassDefaultMethod

-- | A functional dependency for a class.
--
-- > | a, b -> c
-- > =====
-- > funDep ["a", "b"] ["c"]
--
-- > class Ident a b | a -> b, b -> a where
-- >   ident :: a -> b
-- > =====
-- > class' [] "Ident" ["a", "b"]
-- >    [ funDep ["a"] ["b"]
-- >    , funDep ["b"] ["a"]
-- >    , typeSig "ident" $ var "a" --> var "b"
-- >    ]
funDep :: [RdrNameStr] -> [RdrNameStr] -> ClassDecl
funDep :: [RdrNameStr] -> [RdrNameStr] -> ClassDecl
funDep = [RdrNameStr] -> [RdrNameStr] -> ClassDecl
ClassFunDep

-- TODO:
-- - kinded variables
-- - fixity of declaration
-- - functional dependencies
-- - associated types

-- | A class declaration.
--
-- > class (Real a, Enum a) => Integral a where
-- >   divMod :: a -> a -> (a, a)
-- >   div :: a -> a -> a
-- >   div x y = fst (divMod x y)
-- > =====
-- > let a = var "a"
-- > in class'
-- >      [var "Real" @@ a, var "Enum" @@ a]
-- >      "Integral"
-- >      [bvar "a"]
-- >      [ typeSig "divMod" $ a --> a --> tuple [a, a]
-- >      , typeSig "div" $ a --> a --> a
-- >      , funBind "div"
-- >          $ match [bvar "x", bvar "y"]
-- >             $ var "fst" @@ (var "divMod" @@ var "x" @@ var "y")
-- >      ]
class'
    :: [HsType'] -- ^ Context
    -> OccNameStr -- ^ Class name
    -> [HsTyVarBndr'] -- ^ Type parameters
    -> [ClassDecl] -- ^ Class declarations
    -> HsDecl'
class' :: [HsType'] -> OccNameStr -> [HsTyVarBndr'] -> [ClassDecl] -> HsDecl'
class' [HsType']
context OccNameStr
name [HsTyVarBndr']
vars [ClassDecl]
decls
    = (NoExtField -> TyClDecl GhcPs -> HsDecl')
-> TyClDecl GhcPs -> HsDecl'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> TyClDecl GhcPs -> HsDecl'
forall p. XTyClD p -> TyClDecl p -> HsDecl p
TyClD (TyClDecl GhcPs -> HsDecl') -> TyClDecl GhcPs -> HsDecl'
forall a b. (a -> b) -> a -> b
$ ClassDecl :: forall pass.
XClassDecl pass
-> LHsContext pass
-> Located (IdP pass)
-> LHsQTyVars pass
-> LexicalFixity
-> [LHsFunDep pass]
-> [LSig pass]
-> LHsBinds pass
-> [LFamilyDecl pass]
-> [LTyFamDefltDecl pass]
-> [LDocDecl]
-> TyClDecl pass
ClassDecl
            { tcdCtxt :: LHsContext GhcPs
tcdCtxt = [Located HsType'] -> LHsContext GhcPs
forall e. e -> Located e
builtLoc ([Located HsType'] -> LHsContext GhcPs)
-> [Located HsType'] -> LHsContext GhcPs
forall a b. (a -> b) -> a -> b
$ (HsType' -> Located HsType') -> [HsType'] -> [Located HsType']
forall a b. (a -> b) -> [a] -> [b]
map HsType' -> Located HsType'
forall e. e -> Located e
builtLoc [HsType']
context
#if MIN_VERSION_ghc(9,0,0)
            , tcdCExt = NoLayoutInfo
#elif MIN_VERSION_ghc(8,10,0)
            , tcdCExt :: XClassDecl GhcPs
tcdCExt = NoExtField
XClassDecl GhcPs
NoExtField
#elif MIN_VERSION_ghc(8,6,0)
            , tcdCExt = NoExt
#else
            , tcdFVs = PlaceHolder
#endif
            , tcdLName :: Located (IdP GhcPs)
tcdLName = RdrNameStr -> Located RdrName
typeRdrName (RdrNameStr -> Located RdrName) -> RdrNameStr -> Located RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
name
            , tcdTyVars :: LHsQTyVars GhcPs
tcdTyVars = [HsTyVarBndr'] -> LHsQTyVars GhcPs
mkQTyVars [HsTyVarBndr']
vars
            , tcdFixity :: LexicalFixity
tcdFixity = LexicalFixity
Prefix
            , tcdFDs :: [LHsFunDep GhcPs]
tcdFDs = [ ([Located RdrName], [Located RdrName])
-> Located ([Located RdrName], [Located RdrName])
forall e. e -> Located e
builtLoc ((RdrNameStr -> Located RdrName)
-> [RdrNameStr] -> [Located RdrName]
forall a b. (a -> b) -> [a] -> [b]
map RdrNameStr -> Located RdrName
typeRdrName [RdrNameStr]
xs, (RdrNameStr -> Located RdrName)
-> [RdrNameStr] -> [Located RdrName]
forall a b. (a -> b) -> [a] -> [b]
map RdrNameStr -> Located RdrName
typeRdrName [RdrNameStr]
ys)
                       | ClassFunDep [RdrNameStr]
xs [RdrNameStr]
ys <- [ClassDecl]
decls
                       ]
            , tcdSigs :: [LSig GhcPs]
tcdSigs = [Sig' -> LSig GhcPs
forall e. e -> Located e
builtLoc Sig'
sig | ClassSig Sig'
sig <- [ClassDecl]
decls]
            , tcdMeths :: LHsBinds GhcPs
tcdMeths =
                [Located HsBind'] -> LHsBinds GhcPs
forall a. [a] -> Bag a
listToBag [HsBind' -> Located HsBind'
forall e. e -> Located e
builtLoc HsBind'
bind | ClassDefaultMethod HsBind'
bind <- [ClassDecl]
decls]
            , tcdATs :: [LFamilyDecl GhcPs]
tcdATs = []  -- Associated types
            , tcdATDefs :: [LTyFamDefltDecl GhcPs]
tcdATDefs = []  -- Associated type defaults
            , tcdDocs :: [LDocDecl]
tcdDocs = []  -- Haddocks
            }

-- | A definition that can appear in the body of an @instance@ declaration.
--
-- 'RawInstDecl' definitions may be constructed using its class instances, e.g.,
-- 'HasValBind'.  For more details, see the documentation of those classes.
data RawInstDecl
    = InstSig Sig'
    | InstBind HsBind'
    | InstTyFam TyFamInstDecl'

instance HasValBind RawInstDecl where
    sigB :: Sig' -> RawInstDecl
sigB = Sig' -> RawInstDecl
InstSig
    bindB :: HsBind' -> RawInstDecl
bindB = HsBind' -> RawInstDecl
InstBind

-- | An instance declaration.
--
-- > instance Show Bool where
-- >   show :: Bool -> String -- Requires the InstanceSigs extension
-- >   show True = "True"
-- >   show False = "False"
-- > =====
-- > instance' (var "Show" @@ var "Bool")
-- >   [ typeSig "show" $ var "Bool" --> var "String"
-- >   , funBinds "show"
-- >       [ match [bvar "True"] $ string "True"
-- >       , match [bvar "False"] $ string "False"
-- >       ]
-- >   ]
instance' :: HsType' -> [RawInstDecl] -> HsDecl'
instance' :: HsType' -> [RawInstDecl] -> HsDecl'
instance' HsType'
ty [RawInstDecl]
decls = (NoExtField -> InstDecl GhcPs -> HsDecl')
-> InstDecl GhcPs -> HsDecl'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> InstDecl GhcPs -> HsDecl'
forall p. XInstD p -> InstDecl p -> HsDecl p
InstD  (InstDecl GhcPs -> HsDecl') -> InstDecl GhcPs -> HsDecl'
forall a b. (a -> b) -> a -> b
$ (NoExtField -> ClsInstDecl GhcPs -> InstDecl GhcPs)
-> ClsInstDecl GhcPs -> InstDecl GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField -> ClsInstDecl GhcPs -> InstDecl GhcPs
forall pass. XClsInstD pass -> ClsInstDecl pass -> InstDecl pass
ClsInstD (ClsInstDecl GhcPs -> InstDecl GhcPs)
-> ClsInstDecl GhcPs -> InstDecl GhcPs
forall a b. (a -> b) -> a -> b
$ ClsInstDecl :: forall pass.
XCClsInstDecl pass
-> LHsSigType pass
-> LHsBinds pass
-> [LSig pass]
-> [LTyFamInstDecl pass]
-> [LDataFamInstDecl pass]
-> Maybe (Located OverlapMode)
-> ClsInstDecl pass
ClsInstDecl
    { cid_poly_ty :: LHsSigType GhcPs
cid_poly_ty = HsType' -> LHsSigType GhcPs
sigType HsType'
ty
#if MIN_VERSION_ghc(8,10,0)
    , cid_ext :: XCClsInstDecl GhcPs
cid_ext = NoExtField
XCClsInstDecl GhcPs
NoExtField
#elif MIN_VERSION_ghc(8,6,0)
    , cid_ext = NoExt
#endif
    , cid_binds :: LHsBinds GhcPs
cid_binds = [Located HsBind'] -> LHsBinds GhcPs
forall a. [a] -> Bag a
listToBag [HsBind' -> Located HsBind'
forall e. e -> Located e
builtLoc HsBind'
b | InstBind HsBind'
b <- [RawInstDecl]
decls]
    , cid_sigs :: [LSig GhcPs]
cid_sigs = [Sig' -> LSig GhcPs
forall e. e -> Located e
builtLoc Sig'
sig | InstSig Sig'
sig <- [RawInstDecl]
decls]
    , cid_tyfam_insts :: [LTyFamDefltDecl GhcPs]
cid_tyfam_insts = [TyFamInstDecl' -> LTyFamDefltDecl GhcPs
forall e. e -> Located e
builtLoc (TyFamInstDecl' -> LTyFamDefltDecl GhcPs)
-> TyFamInstDecl' -> LTyFamDefltDecl GhcPs
forall a b. (a -> b) -> a -> b
$ TyFamInstDecl'
t | InstTyFam TyFamInstDecl'
t <- [RawInstDecl]
decls]
    , cid_datafam_insts :: [LDataFamInstDecl GhcPs]
cid_datafam_insts = []
    , cid_overlap_mode :: Maybe (Located OverlapMode)
cid_overlap_mode = Maybe (Located OverlapMode)
forall a. Maybe a
Nothing
    }

-- | Terms which can contain a type instance declaration.
--
-- To use this class, call 'tyFamInst'.
class HasTyFamInst t where
    tyFamInstD :: TyFamInstDecl' -> t

instance HasTyFamInst HsDecl' where
    tyFamInstD :: TyFamInstDecl' -> HsDecl'
tyFamInstD = (NoExtField -> InstDecl GhcPs -> HsDecl')
-> InstDecl GhcPs -> HsDecl'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> InstDecl GhcPs -> HsDecl'
forall p. XInstD p -> InstDecl p -> HsDecl p
InstD (InstDecl GhcPs -> HsDecl')
-> (TyFamInstDecl' -> InstDecl GhcPs) -> TyFamInstDecl' -> HsDecl'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NoExtField -> TyFamInstDecl' -> InstDecl GhcPs)
-> TyFamInstDecl' -> InstDecl GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField -> TyFamInstDecl' -> InstDecl GhcPs
forall pass.
XTyFamInstD pass -> TyFamInstDecl pass -> InstDecl pass
TyFamInstD

instance HasTyFamInst RawInstDecl where
    tyFamInstD :: TyFamInstDecl' -> RawInstDecl
tyFamInstD = TyFamInstDecl' -> RawInstDecl
InstTyFam

-- | A type family instance.
--
-- > type Elt String = Char
-- > =====
-- > tyFamInst "Elt" [var "String"] (var "Char")
tyFamInst :: HasTyFamInst t => RdrNameStr -> [HsType'] -> HsType' -> t
tyFamInst :: RdrNameStr -> [HsType'] -> HsType' -> t
tyFamInst RdrNameStr
name [HsType']
params HsType'
ty = TyFamInstDecl' -> t
forall t. HasTyFamInst t => TyFamInstDecl' -> t
tyFamInstD
        (TyFamInstDecl' -> t) -> TyFamInstDecl' -> t
forall a b. (a -> b) -> a -> b
$ TyFamInstEqn GhcPs -> TyFamInstDecl'
forall pass. TyFamInstEqn pass -> TyFamInstDecl pass
TyFamInstDecl
        (TyFamInstEqn GhcPs -> TyFamInstDecl')
-> TyFamInstEqn GhcPs -> TyFamInstDecl'
forall a b. (a -> b) -> a -> b
$ FamEqn GhcPs (Located HsType') -> TyFamInstEqn GhcPs
forall t. t -> HsImplicitBndrs' t
implicitBndrs
        (FamEqn GhcPs (Located HsType') -> TyFamInstEqn GhcPs)
-> FamEqn GhcPs (Located HsType') -> TyFamInstEqn GhcPs
forall a b. (a -> b) -> a -> b
$ (NoExtField
 -> Located RdrName
 -> Maybe [LHsTyVarBndr GhcPs]
 -> [HsArg (Located HsType') (Located HsType')]
 -> LexicalFixity
 -> Located HsType'
 -> FamEqn GhcPs (Located HsType'))
-> Located RdrName
-> Maybe [LHsTyVarBndr GhcPs]
-> [HsArg (Located HsType') (Located HsType')]
-> LexicalFixity
-> Located HsType'
-> FamEqn GhcPs (Located HsType')
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> Located RdrName
-> Maybe [LHsTyVarBndr GhcPs]
-> [HsArg (Located HsType') (Located HsType')]
-> LexicalFixity
-> Located HsType'
-> FamEqn GhcPs (Located HsType')
forall pass rhs.
XCFamEqn pass rhs
-> Located (IdP pass)
-> Maybe [LHsTyVarBndr pass]
-> HsTyPats pass
-> LexicalFixity
-> rhs
-> FamEqn pass rhs
FamEqn (RdrNameStr -> Located RdrName
typeRdrName RdrNameStr
name)
#if MIN_VERSION_ghc(8,8,0)
            Maybe [LHsTyVarBndr GhcPs]
forall a. Maybe a
Nothing -- eqn binders
            ((HsType' -> HsArg (Located HsType') (Located HsType'))
-> [HsType'] -> [HsArg (Located HsType') (Located HsType')]
forall a b. (a -> b) -> [a] -> [b]
map (Located HsType' -> HsArg (Located HsType') (Located HsType')
forall tm ty. tm -> HsArg tm ty
HsValArg (Located HsType' -> HsArg (Located HsType') (Located HsType'))
-> (HsType' -> Located HsType')
-> HsType'
-> HsArg (Located HsType') (Located HsType')
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HsType' -> Located HsType'
forall e. e -> Located e
builtLoc) [HsType']
params)
#else
            (map builtLoc params)
#endif
            LexicalFixity
Prefix
            (HsType' -> Located HsType'
forall e. e -> Located e
builtLoc HsType'
ty)

-- | Declares a type synonym.
--
-- > type A a b = B b a
-- > =====
-- > type' "A" [bvar "a", bvar "b"] $ var "B" @@ var "b" @@ var "a"
type' :: OccNameStr -> [HsTyVarBndr'] -> HsType' -> HsDecl'
type' :: OccNameStr -> [HsTyVarBndr'] -> HsType' -> HsDecl'
type' OccNameStr
name [HsTyVarBndr']
vars HsType'
t =
    (NoExtField -> TyClDecl GhcPs -> HsDecl')
-> TyClDecl GhcPs -> HsDecl'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> TyClDecl GhcPs -> HsDecl'
forall p. XTyClD p -> TyClDecl p -> HsDecl p
TyClD (TyClDecl GhcPs -> HsDecl') -> TyClDecl GhcPs -> HsDecl'
forall a b. (a -> b) -> a -> b
$ TyClDecl GhcPs -> TyClDecl GhcPs
forall a. a -> a
withPlaceHolder (TyClDecl GhcPs -> TyClDecl GhcPs)
-> TyClDecl GhcPs -> TyClDecl GhcPs
forall a b. (a -> b) -> a -> b
$ (NoExtField
 -> Located RdrName
 -> LHsQTyVars GhcPs
 -> LexicalFixity
 -> Located HsType'
 -> TyClDecl GhcPs)
-> Located RdrName
-> LHsQTyVars GhcPs
-> LexicalFixity
-> Located HsType'
-> TyClDecl GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> Located RdrName
-> LHsQTyVars GhcPs
-> LexicalFixity
-> Located HsType'
-> TyClDecl GhcPs
forall pass.
XSynDecl pass
-> Located (IdP pass)
-> LHsQTyVars pass
-> LexicalFixity
-> LHsType pass
-> TyClDecl pass
SynDecl (RdrNameStr -> Located RdrName
typeRdrName (RdrNameStr -> Located RdrName) -> RdrNameStr -> Located RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
name)
        ([HsTyVarBndr'] -> LHsQTyVars GhcPs
mkQTyVars [HsTyVarBndr']
vars)
        LexicalFixity
Prefix
        (HsType' -> Located HsType'
forall e. e -> Located e
builtLoc HsType'
t)

newOrDataType
    :: NewOrData
    -> OccNameStr
    -> [HsTyVarBndr']
    -> [ConDecl']
    -> [HsDerivingClause']
    -> HsDecl'
newOrDataType :: NewOrData
-> OccNameStr
-> [HsTyVarBndr']
-> [ConDecl']
-> [HsDerivingClause']
-> HsDecl'
newOrDataType NewOrData
newOrData OccNameStr
name [HsTyVarBndr']
vars [ConDecl']
conDecls [HsDerivingClause']
derivs
    = (NoExtField -> TyClDecl GhcPs -> HsDecl')
-> TyClDecl GhcPs -> HsDecl'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> TyClDecl GhcPs -> HsDecl'
forall p. XTyClD p -> TyClDecl p -> HsDecl p
TyClD (TyClDecl GhcPs -> HsDecl') -> TyClDecl GhcPs -> HsDecl'
forall a b. (a -> b) -> a -> b
$ TyClDecl GhcPs -> TyClDecl GhcPs
forall a. a -> a
withPlaceHolder (TyClDecl GhcPs -> TyClDecl GhcPs)
-> TyClDecl GhcPs -> TyClDecl GhcPs
forall a b. (a -> b) -> a -> b
$ TyClDecl GhcPs -> TyClDecl GhcPs
forall a. a -> a
withPlaceHolder (TyClDecl GhcPs -> TyClDecl GhcPs)
-> TyClDecl GhcPs -> TyClDecl GhcPs
forall a b. (a -> b) -> a -> b
$
        (NoExtField
 -> Located RdrName
 -> LHsQTyVars GhcPs
 -> LexicalFixity
 -> HsDataDefn GhcPs
 -> TyClDecl GhcPs)
-> Located RdrName
-> LHsQTyVars GhcPs
-> LexicalFixity
-> HsDataDefn GhcPs
-> TyClDecl GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> Located RdrName
-> LHsQTyVars GhcPs
-> LexicalFixity
-> HsDataDefn GhcPs
-> TyClDecl GhcPs
forall pass.
XDataDecl pass
-> Located (IdP pass)
-> LHsQTyVars pass
-> LexicalFixity
-> HsDataDefn pass
-> TyClDecl pass
DataDecl (RdrNameStr -> Located RdrName
typeRdrName (RdrNameStr -> Located RdrName) -> RdrNameStr -> Located RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
name)
            ([HsTyVarBndr'] -> LHsQTyVars GhcPs
mkQTyVars [HsTyVarBndr']
vars)
            LexicalFixity
Prefix
            (HsDataDefn GhcPs -> TyClDecl GhcPs)
-> HsDataDefn GhcPs -> TyClDecl GhcPs
forall a b. (a -> b) -> a -> b
$ (NoExtField
 -> NewOrData
 -> LHsContext GhcPs
 -> Maybe (Located CType)
 -> Maybe (Located HsType')
 -> [LConDecl GhcPs]
 -> HsDeriving GhcPs
 -> HsDataDefn GhcPs)
-> NewOrData
-> LHsContext GhcPs
-> Maybe (Located CType)
-> Maybe (Located HsType')
-> [LConDecl GhcPs]
-> HsDeriving GhcPs
-> HsDataDefn GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> NewOrData
-> LHsContext GhcPs
-> Maybe (Located CType)
-> Maybe (Located HsType')
-> [LConDecl GhcPs]
-> HsDeriving GhcPs
-> HsDataDefn GhcPs
forall pass.
XCHsDataDefn pass
-> NewOrData
-> LHsContext pass
-> Maybe (Located CType)
-> Maybe (LHsKind pass)
-> [LConDecl pass]
-> HsDeriving pass
-> HsDataDefn pass
HsDataDefn NewOrData
newOrData
                ([Located HsType'] -> LHsContext GhcPs
forall e. e -> Located e
builtLoc []) Maybe (Located CType)
forall a. Maybe a
Nothing
                Maybe (Located HsType')
forall a. Maybe a
Nothing
                ((ConDecl' -> LConDecl GhcPs) -> [ConDecl'] -> [LConDecl GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map ConDecl' -> LConDecl GhcPs
forall e. e -> Located e
builtLoc [ConDecl']
conDecls)
                ([Located HsDerivingClause'] -> HsDeriving GhcPs
forall e. e -> Located e
builtLoc ([Located HsDerivingClause'] -> HsDeriving GhcPs)
-> [Located HsDerivingClause'] -> HsDeriving GhcPs
forall a b. (a -> b) -> a -> b
$ (HsDerivingClause' -> Located HsDerivingClause')
-> [HsDerivingClause'] -> [Located HsDerivingClause']
forall a b. (a -> b) -> [a] -> [b]
map HsDerivingClause' -> Located HsDerivingClause'
forall e. e -> Located e
builtLoc [HsDerivingClause']
derivs)

-- | A newtype declaration.
--
-- > newtype Const a b = Const a deriving Eq
-- > =====
-- > newtype' "Const" [bvar "a", bvar "b"]
-- >    (conDecl "Const" [var "a"])
-- >    [var "Show"]
newtype' :: OccNameStr -> [HsTyVarBndr'] -> ConDecl' -> [HsDerivingClause'] -> HsDecl'
newtype' :: OccNameStr
-> [HsTyVarBndr'] -> ConDecl' -> [HsDerivingClause'] -> HsDecl'
newtype' OccNameStr
name [HsTyVarBndr']
vars ConDecl'
conD = NewOrData
-> OccNameStr
-> [HsTyVarBndr']
-> [ConDecl']
-> [HsDerivingClause']
-> HsDecl'
newOrDataType NewOrData
NewType OccNameStr
name [HsTyVarBndr']
vars [ConDecl'
conD]

-- | A data declaration.
--
-- > data Either a b = Left a | Right b
-- >    deriving Show
-- > =====
-- > data' "Either" [bvar "a", bvar "b"]
-- >   [ conDecl "Left" [var "a"]
-- >   , conDecl "Right" [var "b"]
-- >   ]
-- >   [var "Show"]
data' :: OccNameStr -> [HsTyVarBndr'] -> [ConDecl'] -> [HsDerivingClause'] -> HsDecl'
data' :: OccNameStr
-> [HsTyVarBndr'] -> [ConDecl'] -> [HsDerivingClause'] -> HsDecl'
data' = NewOrData
-> OccNameStr
-> [HsTyVarBndr']
-> [ConDecl']
-> [HsDerivingClause']
-> HsDecl'
newOrDataType NewOrData
DataType

-- | Declares a Haskell-98-style prefix constructor for a data or type
-- declaration.
--
-- > Foo a Int
-- > =====
-- > prefixCon "Foo" [field (var "a"), field (var "Int")]
prefixCon :: OccNameStr -> [Field] -> ConDecl'
prefixCon :: OccNameStr -> [Field] -> ConDecl'
prefixCon OccNameStr
name [Field]
fields = OccNameStr -> HsConDeclDetails' -> ConDecl'
renderCon98Decl OccNameStr
name
    (HsConDeclDetails' -> ConDecl') -> HsConDeclDetails' -> ConDecl'
forall a b. (a -> b) -> a -> b
$ [Located HsType'] -> HsConDeclDetails'
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon ([Located HsType'] -> HsConDeclDetails')
-> [Located HsType'] -> HsConDeclDetails'
forall a b. (a -> b) -> a -> b
$ (Field -> Located HsType') -> [Field] -> [Located HsType']
forall a b. (a -> b) -> [a] -> [b]
map (Located HsType' -> Located HsType'
forall a. a -> a
hsUnrestricted (Located HsType' -> Located HsType')
-> (Field -> Located HsType') -> Field -> Located HsType'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field -> Located HsType'
renderField) [Field]
fields

-- | Declares a Haskell-98-style infix constructor for a data or type
-- declaration.
--
-- > A b :+: C d
-- > =====
-- > infixCon (field (var "A" @@ var "b")) ":+:" (field (Var "C" @@ var "d"))
infixCon :: Field -> OccNameStr -> Field -> ConDecl'
infixCon :: Field -> OccNameStr -> Field -> ConDecl'
infixCon Field
f OccNameStr
name Field
f' = OccNameStr -> HsConDeclDetails' -> ConDecl'
renderCon98Decl OccNameStr
name
    (HsConDeclDetails' -> ConDecl') -> HsConDeclDetails' -> ConDecl'
forall a b. (a -> b) -> a -> b
$ Located HsType' -> Located HsType' -> HsConDeclDetails'
forall arg rec. arg -> arg -> HsConDetails arg rec
InfixCon (Located HsType' -> Located HsType'
forall a. a -> a
hsUnrestricted (Located HsType' -> Located HsType')
-> Located HsType' -> Located HsType'
forall a b. (a -> b) -> a -> b
$ Field -> Located HsType'
renderField Field
f) (Located HsType' -> Located HsType'
forall a. a -> a
hsUnrestricted (Located HsType' -> Located HsType')
-> Located HsType' -> Located HsType'
forall a b. (a -> b) -> a -> b
$ Field -> Located HsType'
renderField Field
f')

-- | Declares Haskell-98-style record constructor for a data or type
-- declaration.
--
-- > A { x :: B, y :: C }
-- > =====
-- > recordCon "A" [("x", var "B"), ("y", var "C")]
recordCon :: OccNameStr -> [(OccNameStr, Field)] -> ConDecl'
recordCon :: OccNameStr -> [(OccNameStr, Field)] -> ConDecl'
recordCon OccNameStr
name [(OccNameStr, Field)]
fields = OccNameStr -> HsConDeclDetails' -> ConDecl'
renderCon98Decl OccNameStr
name
    (HsConDeclDetails' -> ConDecl') -> HsConDeclDetails' -> ConDecl'
forall a b. (a -> b) -> a -> b
$ Located [Located (ConDeclField GhcPs)] -> HsConDeclDetails'
forall arg rec. rec -> HsConDetails arg rec
RecCon (Located [Located (ConDeclField GhcPs)] -> HsConDeclDetails')
-> Located [Located (ConDeclField GhcPs)] -> HsConDeclDetails'
forall a b. (a -> b) -> a -> b
$ [Located (ConDeclField GhcPs)]
-> Located [Located (ConDeclField GhcPs)]
forall e. e -> Located e
builtLoc ([Located (ConDeclField GhcPs)]
 -> Located [Located (ConDeclField GhcPs)])
-> [Located (ConDeclField GhcPs)]
-> Located [Located (ConDeclField GhcPs)]
forall a b. (a -> b) -> a -> b
$ ((OccNameStr, Field) -> Located (ConDeclField GhcPs))
-> [(OccNameStr, Field)] -> [Located (ConDeclField GhcPs)]
forall a b. (a -> b) -> [a] -> [b]
map (OccNameStr, Field) -> Located (ConDeclField GhcPs)
mkLConDeclField [(OccNameStr, Field)]
fields
  where
    mkLConDeclField :: (OccNameStr, Field) -> Located (ConDeclField GhcPs)
mkLConDeclField (OccNameStr
n, Field
f) =
        ConDeclField GhcPs -> Located (ConDeclField GhcPs)
forall e. e -> Located e
builtLoc (ConDeclField GhcPs -> Located (ConDeclField GhcPs))
-> ConDeclField GhcPs -> Located (ConDeclField GhcPs)
forall a b. (a -> b) -> a -> b
$ (NoExtField
 -> [LFieldOcc GhcPs]
 -> Located HsType'
 -> Maybe LHsDocString
 -> ConDeclField GhcPs)
-> [LFieldOcc GhcPs]
-> Located HsType'
-> Maybe LHsDocString
-> ConDeclField GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> [LFieldOcc GhcPs]
-> Located HsType'
-> Maybe LHsDocString
-> ConDeclField GhcPs
forall pass.
XConDeclField pass
-> [LFieldOcc pass]
-> LBangType pass
-> Maybe LHsDocString
-> ConDeclField pass
ConDeclField
                        [FieldOcc GhcPs -> LFieldOcc GhcPs
forall e. e -> Located e
builtLoc (FieldOcc GhcPs -> LFieldOcc GhcPs)
-> FieldOcc GhcPs -> LFieldOcc GhcPs
forall a b. (a -> b) -> a -> b
$ FieldOcc GhcPs -> FieldOcc GhcPs
forall a. a -> a
withPlaceHolder (FieldOcc GhcPs -> FieldOcc GhcPs)
-> FieldOcc GhcPs -> FieldOcc GhcPs
forall a b. (a -> b) -> a -> b
$ (NoExtField -> Located RdrName -> FieldOcc GhcPs)
-> Located RdrName -> FieldOcc GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField -> Located RdrName -> FieldOcc GhcPs
forall pass. XCFieldOcc pass -> Located RdrName -> FieldOcc pass
FieldOcc (Located RdrName -> FieldOcc GhcPs)
-> Located RdrName -> FieldOcc GhcPs
forall a b. (a -> b) -> a -> b
$ RdrNameStr -> Located RdrName
valueRdrName (RdrNameStr -> Located RdrName) -> RdrNameStr -> Located RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
n]
                        (Field -> Located HsType'
renderField Field
f)
                        Maybe LHsDocString
forall a. Maybe a
Nothing

-- | An individual argument of a data constructor.  Contains a type for the field,
-- and whether the field is strict or lazy.
data Field = Field
    { Field -> HsType'
fieldType :: HsType'
    , Field -> SrcStrictness
strictness :: SrcStrictness
    }

-- | A field with no explicit strictness annotations.
--
-- > A b
-- > =====
-- > field $ var "A" @@ var "b"
field :: HsType' -> Field
field :: HsType' -> Field
field HsType'
t = HsType' -> SrcStrictness -> Field
Field HsType'
t SrcStrictness
NoSrcStrict

-- | Give a field an explicit strictness annotation.  Overrides any such previous
-- annotations (for example, from 'lazy').
--
-- > !(A b)
-- > =====
-- > strict $ field $ var "A" @@ var "b"
strict :: Field -> Field
strict :: Field -> Field
strict Field
f = Field
f { strictness :: SrcStrictness
strictness = SrcStrictness
SrcStrict }

-- | Give a field an explicit laziness annotation.  This feature is useful in combination
-- with the @StrictData@ extension.  Overrides any such previous
-- annotations (for example, from 'strict').
--
-- > !(A b)
-- > =====
-- > strict $ field $ var "A" @@ var "b"
lazy :: Field -> Field
lazy :: Field -> Field
lazy Field
f = Field
f { strictness :: SrcStrictness
strictness = SrcStrictness
SrcLazy }

#if !MIN_VERSION_ghc(9,0,0)
hsUnrestricted :: a -> a
hsUnrestricted :: a -> a
hsUnrestricted = a -> a
forall a. a -> a
id
#endif

renderField :: Field -> Located HsType'
-- TODO: parenthesizeTypeForApp is an overestimate in the case of
-- rendering an infix or record type.
renderField :: Field -> Located HsType'
renderField Field
f = Located HsType' -> Located HsType'
wrap (Located HsType' -> Located HsType')
-> Located HsType' -> Located HsType'
forall a b. (a -> b) -> a -> b
$ Located HsType' -> Located HsType'
parenthesizeTypeForApp (Located HsType' -> Located HsType')
-> Located HsType' -> Located HsType'
forall a b. (a -> b) -> a -> b
$ HsType' -> Located HsType'
forall e. e -> Located e
builtLoc (HsType' -> Located HsType') -> HsType' -> Located HsType'
forall a b. (a -> b) -> a -> b
$ Field -> HsType'
fieldType Field
f
  where
    wrap :: Located HsType' -> Located HsType'
wrap = case Field -> SrcStrictness
strictness Field
f of
        SrcStrictness
NoSrcStrict -> Located HsType' -> Located HsType'
forall a. a -> a
id
        SrcStrictness
s -> HsType' -> Located HsType'
forall e. e -> Located e
builtLoc (HsType' -> Located HsType')
-> (Located HsType' -> HsType')
-> Located HsType'
-> Located HsType'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((NoExtField -> HsSrcBang -> Located HsType' -> HsType')
-> HsSrcBang -> Located HsType' -> HsType'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> HsSrcBang -> Located HsType' -> HsType'
forall pass.
XBangTy pass -> HsSrcBang -> LHsType pass -> HsType pass
HsBangTy (HsSrcBang -> Located HsType' -> HsType')
-> HsSrcBang -> Located HsType' -> HsType'
forall a b. (a -> b) -> a -> b
$ (SourceText -> SrcUnpackedness -> SrcStrictness -> HsSrcBang)
-> SrcUnpackedness -> SrcStrictness -> HsSrcBang
forall a. (SourceText -> a) -> a
noSourceText SourceText -> SrcUnpackedness -> SrcStrictness -> HsSrcBang
HsSrcBang SrcUnpackedness
NoSrcUnpack SrcStrictness
s)

renderCon98Decl :: OccNameStr -> HsConDeclDetails' -> ConDecl'
renderCon98Decl :: OccNameStr -> HsConDeclDetails' -> ConDecl'
renderCon98Decl OccNameStr
name HsConDeclDetails'
details = (NoExtField
 -> Located RdrName
 -> Located Bool
 -> [LHsTyVarBndr GhcPs]
 -> Maybe (LHsContext GhcPs)
 -> HsConDeclDetails'
 -> Maybe LHsDocString
 -> ConDecl')
-> Located RdrName
-> Located Bool
-> [LHsTyVarBndr GhcPs]
-> Maybe (LHsContext GhcPs)
-> HsConDeclDetails'
-> Maybe LHsDocString
-> ConDecl'
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> Located RdrName
-> Located Bool
-> [LHsTyVarBndr GhcPs]
-> Maybe (LHsContext GhcPs)
-> HsConDeclDetails'
-> Maybe LHsDocString
-> ConDecl'
forall pass.
XConDeclH98 pass
-> Located (IdP pass)
-> Located Bool
-> [LHsTyVarBndr pass]
-> Maybe (LHsContext pass)
-> HsConDeclDetails pass
-> Maybe LHsDocString
-> ConDecl pass
ConDeclH98 (RdrNameStr -> Located RdrName
typeRdrName (RdrNameStr -> Located RdrName) -> RdrNameStr -> Located RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
name)
#if MIN_VERSION_ghc(8,6,0)
    (Bool -> Located Bool
forall e. e -> Located e
builtLoc Bool
False)
    []
#else
    Nothing
#endif
    Maybe (LHsContext GhcPs)
forall a. Maybe a
Nothing
    HsConDeclDetails'
details
    Maybe LHsDocString
forall a. Maybe a
Nothing

deriving' :: [HsType'] -> HsDerivingClause'
deriving' :: [HsType'] -> HsDerivingClause'
deriving' = Maybe DerivStrategy' -> [HsType'] -> HsDerivingClause'
derivingWay Maybe DerivStrategy'
forall a. Maybe a
Nothing

derivingWay :: Maybe DerivStrategy' -> [HsType'] -> HsDerivingClause'
derivingWay :: Maybe DerivStrategy' -> [HsType'] -> HsDerivingClause'
derivingWay Maybe DerivStrategy'
way [HsType']
ts =
    (NoExtField
 -> Maybe (LDerivStrategy GhcPs)
 -> Located [LHsSigType GhcPs]
 -> HsDerivingClause')
-> Maybe (LDerivStrategy GhcPs)
-> Located [LHsSigType GhcPs]
-> HsDerivingClause'
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> Maybe (LDerivStrategy GhcPs)
-> Located [LHsSigType GhcPs]
-> HsDerivingClause'
forall pass.
XCHsDerivingClause pass
-> Maybe (LDerivStrategy pass)
-> Located [LHsSigType pass]
-> HsDerivingClause pass
HsDerivingClause ((DerivStrategy' -> LDerivStrategy GhcPs)
-> Maybe DerivStrategy' -> Maybe (LDerivStrategy GhcPs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DerivStrategy' -> LDerivStrategy GhcPs
forall e. e -> Located e
builtLoc Maybe DerivStrategy'
way) (Located [LHsSigType GhcPs] -> HsDerivingClause')
-> Located [LHsSigType GhcPs] -> HsDerivingClause'
forall a b. (a -> b) -> a -> b
$ [LHsSigType GhcPs] -> Located [LHsSigType GhcPs]
forall e. e -> Located e
builtLoc ([LHsSigType GhcPs] -> Located [LHsSigType GhcPs])
-> [LHsSigType GhcPs] -> Located [LHsSigType GhcPs]
forall a b. (a -> b) -> a -> b
$ (HsType' -> LHsSigType GhcPs) -> [HsType'] -> [LHsSigType GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map HsType' -> LHsSigType GhcPs
sigType [HsType']
ts

derivingStock :: [HsType'] -> HsDerivingClause'
derivingStock :: [HsType'] -> HsDerivingClause'
derivingStock = Maybe DerivStrategy' -> [HsType'] -> HsDerivingClause'
derivingWay (DerivStrategy' -> Maybe DerivStrategy'
forall a. a -> Maybe a
Just DerivStrategy'
forall pass. DerivStrategy pass
StockStrategy)

derivingNewtype :: [HsType'] -> HsDerivingClause'
derivingNewtype :: [HsType'] -> HsDerivingClause'
derivingNewtype = Maybe DerivStrategy' -> [HsType'] -> HsDerivingClause'
derivingWay (DerivStrategy' -> Maybe DerivStrategy'
forall a. a -> Maybe a
Just DerivStrategy'
forall pass. DerivStrategy pass
NewtypeStrategy)

derivingAnyclass :: [HsType'] -> HsDerivingClause'
derivingAnyclass :: [HsType'] -> HsDerivingClause'
derivingAnyclass = Maybe DerivStrategy' -> [HsType'] -> HsDerivingClause'
derivingWay (DerivStrategy' -> Maybe DerivStrategy'
forall a. a -> Maybe a
Just DerivStrategy'
forall pass. DerivStrategy pass
AnyclassStrategy)

#if MIN_VERSION_ghc(8,6,0)
-- | A `DerivingVia` clause.
--
-- > deriving (Eq, Show) via T
-- > =====
-- > derivingVia (var "T") [var "Eq", var "Show"]
-- Available with @ghc>=8.6@.
derivingVia :: HsType' -> [HsType'] -> HsDerivingClause'
derivingVia :: HsType' -> [HsType'] -> HsDerivingClause'
derivingVia HsType'
t = Maybe DerivStrategy' -> [HsType'] -> HsDerivingClause'
derivingWay (DerivStrategy' -> Maybe DerivStrategy'
forall a. a -> Maybe a
Just (DerivStrategy' -> Maybe DerivStrategy')
-> DerivStrategy' -> Maybe DerivStrategy'
forall a b. (a -> b) -> a -> b
$ XViaStrategy GhcPs -> DerivStrategy'
forall pass. XViaStrategy pass -> DerivStrategy pass
ViaStrategy (XViaStrategy GhcPs -> DerivStrategy')
-> XViaStrategy GhcPs -> DerivStrategy'
forall a b. (a -> b) -> a -> b
$ HsType' -> LHsSigType GhcPs
sigType HsType'
t)
#endif

standaloneDeriving :: HsType' -> HsDecl'
standaloneDeriving :: HsType' -> HsDecl'
standaloneDeriving = Maybe DerivStrategy' -> HsType' -> HsDecl'
standaloneDerivingWay Maybe DerivStrategy'
forall a. Maybe a
Nothing

standaloneDerivingStock :: HsType' -> HsDecl'
standaloneDerivingStock :: HsType' -> HsDecl'
standaloneDerivingStock = Maybe DerivStrategy' -> HsType' -> HsDecl'
standaloneDerivingWay (DerivStrategy' -> Maybe DerivStrategy'
forall a. a -> Maybe a
Just DerivStrategy'
forall pass. DerivStrategy pass
StockStrategy)

standaloneDerivingNewtype :: HsType' -> HsDecl'
standaloneDerivingNewtype :: HsType' -> HsDecl'
standaloneDerivingNewtype = Maybe DerivStrategy' -> HsType' -> HsDecl'
standaloneDerivingWay (DerivStrategy' -> Maybe DerivStrategy'
forall a. a -> Maybe a
Just DerivStrategy'
forall pass. DerivStrategy pass
NewtypeStrategy)

standaloneDerivingAnyclass :: HsType' -> HsDecl'
standaloneDerivingAnyclass :: HsType' -> HsDecl'
standaloneDerivingAnyclass = Maybe DerivStrategy' -> HsType' -> HsDecl'
standaloneDerivingWay (DerivStrategy' -> Maybe DerivStrategy'
forall a. a -> Maybe a
Just DerivStrategy'
forall pass. DerivStrategy pass
AnyclassStrategy)

standaloneDerivingWay :: Maybe DerivStrategy' -> HsType' -> HsDecl'
standaloneDerivingWay :: Maybe DerivStrategy' -> HsType' -> HsDecl'
standaloneDerivingWay Maybe DerivStrategy'
way HsType'
ty = (NoExtField -> DerivDecl GhcPs -> HsDecl')
-> DerivDecl GhcPs -> HsDecl'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> DerivDecl GhcPs -> HsDecl'
forall p. XDerivD p -> DerivDecl p -> HsDecl p
DerivD DerivDecl GhcPs
derivDecl
  where derivDecl :: DerivDecl GhcPs
derivDecl =
          (NoExtField
 -> LHsSigWcType GhcPs
 -> Maybe (LDerivStrategy GhcPs)
 -> Maybe (Located OverlapMode)
 -> DerivDecl GhcPs)
-> LHsSigWcType GhcPs
-> Maybe (LDerivStrategy GhcPs)
-> Maybe (Located OverlapMode)
-> DerivDecl GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> LHsSigWcType GhcPs
-> Maybe (LDerivStrategy GhcPs)
-> Maybe (Located OverlapMode)
-> DerivDecl GhcPs
forall pass.
XCDerivDecl pass
-> LHsSigWcType pass
-> Maybe (LDerivStrategy pass)
-> Maybe (Located OverlapMode)
-> DerivDecl pass
DerivDecl (LHsSigType GhcPs -> LHsSigWcType GhcPs
hsWC LHsSigType GhcPs
hsIB) ((DerivStrategy' -> LDerivStrategy GhcPs)
-> Maybe DerivStrategy' -> Maybe (LDerivStrategy GhcPs)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DerivStrategy' -> LDerivStrategy GhcPs
forall e. e -> Located e
builtLoc Maybe DerivStrategy'
way) Maybe (Located OverlapMode)
forall a. Maybe a
Nothing
        hsIB :: LHsSigType GhcPs
hsIB =
          LHsSigType GhcPs -> LHsSigType GhcPs
forall a. a -> a
withPlaceHolder (LHsSigType GhcPs -> LHsSigType GhcPs)
-> LHsSigType GhcPs -> LHsSigType GhcPs
forall a b. (a -> b) -> a -> b
$ (NoExtField -> Located HsType' -> LHsSigType GhcPs)
-> Located HsType' -> LHsSigType GhcPs
forall a. (NoExtField -> a) -> a
noExtOrPlaceHolder NoExtField -> Located HsType' -> LHsSigType GhcPs
forall pass thing.
XHsIB pass thing -> thing -> HsImplicitBndrs pass thing
HsIB (HsType' -> Located HsType'
forall e. e -> Located e
builtLoc HsType'
ty)
        hsWC :: LHsSigType GhcPs -> LHsSigWcType GhcPs
hsWC =
#if MIN_VERSION_ghc(8,6,0)
          (NoExtField -> LHsSigType GhcPs -> LHsSigWcType GhcPs)
-> LHsSigType GhcPs -> LHsSigWcType GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField -> LHsSigType GhcPs -> LHsSigWcType GhcPs
forall pass thing.
XHsWC pass thing -> thing -> HsWildCardBndrs pass thing
HsWC
#else
          id
#endif

-- | Declares multiple pattern signatures of the same type.
--
-- > pattern F, G :: T
-- > =====
-- > patSynSigs ["F", "G"] $ var "T"
patSynSigs :: [OccNameStr] -> HsType' -> HsDecl'
patSynSigs :: [OccNameStr] -> HsType' -> HsDecl'
patSynSigs [OccNameStr]
names HsType'
t =
    Sig' -> HsDecl'
forall t. HasValBind t => Sig' -> t
sigB (Sig' -> HsDecl') -> Sig' -> HsDecl'
forall a b. (a -> b) -> a -> b
$ (NoExtField -> [Located RdrName] -> LHsSigType GhcPs -> Sig')
-> [Located RdrName] -> LHsSigType GhcPs -> Sig'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> [Located RdrName] -> LHsSigType GhcPs -> Sig'
forall pass.
XPatSynSig pass
-> [Located (IdP pass)] -> LHsSigType pass -> Sig pass
PatSynSig ((OccNameStr -> Located RdrName)
-> [OccNameStr] -> [Located RdrName]
forall a b. (a -> b) -> [a] -> [b]
map (RdrNameStr -> Located RdrName
typeRdrName (RdrNameStr -> Located RdrName)
-> (OccNameStr -> RdrNameStr) -> OccNameStr -> Located RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OccNameStr -> RdrNameStr
unqual) [OccNameStr]
names)
        (LHsSigType GhcPs -> Sig') -> LHsSigType GhcPs -> Sig'
forall a b. (a -> b) -> a -> b
$ HsType' -> LHsSigType GhcPs
sigType HsType'
t

-- | Declares a pattern signature and its type.
--
-- > pattern F :: T
-- > =====
-- > patSynSigs "F" $ var "T"
patSynSig :: OccNameStr -> HsType' -> HsDecl'
patSynSig :: OccNameStr -> HsType' -> HsDecl'
patSynSig OccNameStr
n = [OccNameStr] -> HsType' -> HsDecl'
patSynSigs [OccNameStr
n]

-- TODO: patSynBidi, patSynUni

-- | Defines a pattern signature.
--
-- > pattern F a b = G b a
-- > =====
-- > patSynBind "F" ["a", "b"] $ conP "G" [bvar "b", bvar "a"]
patSynBind :: OccNameStr -> [OccNameStr] -> Pat' -> HsDecl'
patSynBind :: OccNameStr -> [OccNameStr] -> Pat' -> HsDecl'
patSynBind OccNameStr
n [OccNameStr]
ns Pat'
p = HsBind' -> HsDecl'
forall t. HasValBind t => HsBind' -> t
bindB (HsBind' -> HsDecl') -> HsBind' -> HsDecl'
forall a b. (a -> b) -> a -> b
$ (NoExtField -> PatSynBind GhcPs GhcPs -> HsBind')
-> PatSynBind GhcPs GhcPs -> HsBind'
forall a. (NoExtField -> a) -> a
noExt NoExtField -> PatSynBind GhcPs GhcPs -> HsBind'
forall idL idR.
XPatSynBind idL idR -> PatSynBind idL idR -> HsBindLR idL idR
PatSynBind
                    (PatSynBind GhcPs GhcPs -> HsBind')
-> PatSynBind GhcPs GhcPs -> HsBind'
forall a b. (a -> b) -> a -> b
$ (HsConDetails
   (Located RdrName) [RecordPatSynField (Located RdrName)]
 -> GenLocated SrcSpan Pat'
 -> HsPatSynDir GhcPs
 -> PatSynBind GhcPs GhcPs)
-> HsConDetails
     (Located RdrName) [RecordPatSynField (Located RdrName)]
-> GenLocated SrcSpan Pat'
-> HsPatSynDir GhcPs
-> PatSynBind GhcPs GhcPs
forall a. a -> a
withPlaceHolder ((NoExtField
 -> Located RdrName
 -> HsConDetails
      (Located RdrName) [RecordPatSynField (Located RdrName)]
 -> GenLocated SrcSpan Pat'
 -> HsPatSynDir GhcPs
 -> PatSynBind GhcPs GhcPs)
-> Located RdrName
-> HsConDetails
     (Located RdrName) [RecordPatSynField (Located RdrName)]
-> GenLocated SrcSpan Pat'
-> HsPatSynDir GhcPs
-> PatSynBind GhcPs GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> Located RdrName
-> HsConDetails
     (Located RdrName) [RecordPatSynField (Located RdrName)]
-> GenLocated SrcSpan Pat'
-> HsPatSynDir GhcPs
-> PatSynBind GhcPs GhcPs
forall idL idR.
XPSB idL idR
-> Located (IdP idL)
-> HsPatSynDetails (Located (IdP idR))
-> LPat idR
-> HsPatSynDir idR
-> PatSynBind idL idR
PSB (RdrNameStr -> Located RdrName
valueRdrName (RdrNameStr -> Located RdrName) -> RdrNameStr -> Located RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
n))
                        ([Located RdrName]
-> HsConDetails
     (Located RdrName) [RecordPatSynField (Located RdrName)]
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon ((OccNameStr -> Located RdrName)
-> [OccNameStr] -> [Located RdrName]
forall a b. (a -> b) -> [a] -> [b]
map (RdrNameStr -> Located RdrName
valueRdrName (RdrNameStr -> Located RdrName)
-> (OccNameStr -> RdrNameStr) -> OccNameStr -> Located RdrName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OccNameStr -> RdrNameStr
unqual) [OccNameStr]
ns))
                        (Pat' -> LPat'
builtPat Pat'
p)
                        HsPatSynDir GhcPs
forall id. HsPatSynDir id
ImplicitBidirectional