-- 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 #-}
{-# LANGUAGE TypeFamilies #-}

-- | 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 (LexicalFixity(Prefix))
import GHC.Data.Bag (listToBag)
import GHC.Types.SrcLoc (LayoutInfo(..))
#else
import BasicTypes (LexicalFixity(Prefix))
import Bag (listToBag)
#endif
#if !MIN_VERSION_ghc(8,6,0)
import BasicTypes (DerivStrategy(..))
#endif
import GHC (GhcPs)
import GHC.Hs.Binds
import GHC.Hs.Decls

import GHC.Hs.Type
    ( ConDeclField(..)
    , FieldOcc(..)
    , HsConDetails(..)
#if !MIN_VERSION_ghc(9,2,0)
    , HsImplicitBndrs (..)
#endif
#if MIN_VERSION_ghc(9,2,0)
    , HsOuterTyVarBndrs (..)
#endif
    , HsSrcBang(..)
    , HsType(..)
    , LHsType
#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(9,2,0)
import GHC.Parser.Annotation (AnnSortKey(..), EpAnn(..))
#elif 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 = LHsContext GhcPs -> LHsContext GhcPs
forall a. a -> a
toHsContext (LHsContext GhcPs -> LHsContext GhcPs)
-> LHsContext GhcPs -> LHsContext GhcPs
forall a b. (a -> b) -> a -> b
$ [GenLocated (SrcSpanAnn Any) HsType'] -> LHsContext GhcPs
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated ([GenLocated (SrcSpanAnn Any) HsType'] -> LHsContext GhcPs)
-> [GenLocated (SrcSpanAnn Any) HsType'] -> LHsContext GhcPs
forall a b. (a -> b) -> a -> b
$ (HsType' -> GenLocated (SrcSpanAnn Any) HsType')
-> [HsType'] -> [GenLocated (SrcSpanAnn Any) HsType']
forall a b. (a -> b) -> [a] -> [b]
map HsType' -> GenLocated (SrcSpanAnn Any) HsType'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated [HsType']
context
#if MIN_VERSION_ghc(9,2,0)
            , tcdCExt = (EpAnnNotUsed, NoAnnSortKey, NoLayoutInfo)
#elif 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 -> LocatedN RdrName
typeRdrName (RdrNameStr -> LocatedN RdrName) -> RdrNameStr -> LocatedN 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 = [ ([LocatedN RdrName], [LocatedN RdrName])
-> GenLocated
     (SrcSpanAnn Any) ([LocatedN RdrName], [LocatedN RdrName])
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated (([LocatedN RdrName], [LocatedN RdrName])
 -> GenLocated
      (SrcSpanAnn Any) ([LocatedN RdrName], [LocatedN RdrName]))
-> ([LocatedN RdrName], [LocatedN RdrName])
-> GenLocated
     (SrcSpanAnn Any) ([LocatedN RdrName], [LocatedN RdrName])
forall a b. (a -> b) -> a -> b
$ [LocatedN RdrName]
-> [LocatedN RdrName] -> ([LocatedN RdrName], [LocatedN RdrName])
forall a b. a -> b -> (a, b)
funDep' ((RdrNameStr -> LocatedN RdrName)
-> [RdrNameStr] -> [LocatedN RdrName]
forall a b. (a -> b) -> [a] -> [b]
map RdrNameStr -> LocatedN RdrName
typeRdrName [RdrNameStr]
xs) ((RdrNameStr -> LocatedN RdrName)
-> [RdrNameStr] -> [LocatedN RdrName]
forall a b. (a -> b) -> [a] -> [b]
map RdrNameStr -> LocatedN RdrName
typeRdrName [RdrNameStr]
ys)
                       | ClassFunDep [RdrNameStr]
xs [RdrNameStr]
ys <- [ClassDecl]
decls
                       ]
            , tcdSigs :: [LSig GhcPs]
tcdSigs = [Sig' -> LSig GhcPs
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated Sig'
sig | ClassSig Sig'
sig <- [ClassDecl]
decls]
            , tcdMeths :: LHsBinds GhcPs
tcdMeths =
                [GenLocated (SrcSpanAnn Any) HsBind'] -> LHsBinds GhcPs
forall a. [a] -> Bag a
listToBag [HsBind' -> GenLocated (SrcSpanAnn Any) HsBind'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated HsBind'
bind | ClassDefaultMethod HsBind'
bind <- [ClassDecl]
decls]
            , tcdATs :: [LFamilyDecl GhcPs]
tcdATs = []  -- Associated types
            , tcdATDefs :: [LTyFamDefltDecl GhcPs]
tcdATDefs = []  -- Associated type defaults
            , tcdDocs :: [LDocDecl]
tcdDocs = []  -- Haddocks
            }
  where
#if MIN_VERSION_ghc(9,2,0)
    funDep' = withEpAnnNotUsed FunDep
#else
    funDep' :: a -> b -> (a, b)
funDep' = (,)
#endif
#if MIN_VERSION_ghc(9,2,0)
    toHsContext = Just
#else
    toHsContext :: a -> a
toHsContext = a -> a
forall a. a -> a
id
#endif

-- | 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(9,2,0)
    , cid_ext = (EpAnnNotUsed, NoAnnSortKey)
#elif 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 = [GenLocated (SrcSpanAnn Any) HsBind'] -> LHsBinds GhcPs
forall a. [a] -> Bag a
listToBag [HsBind' -> GenLocated (SrcSpanAnn Any) HsBind'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated HsBind'
b | InstBind HsBind'
b <- [RawInstDecl]
decls]
    , cid_sigs :: [LSig GhcPs]
cid_sigs = [Sig' -> LSig GhcPs
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated Sig'
sig | InstSig Sig'
sig <- [RawInstDecl]
decls]
    , cid_tyfam_insts :: [LTyFamDefltDecl GhcPs]
cid_tyfam_insts = [TyFamInstDecl' -> LTyFamDefltDecl GhcPs
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated (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
$ FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
-> TyFamInstDecl'
tyFamInstDecl
        (FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
 -> TyFamInstDecl')
-> FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
-> TyFamInstDecl'
forall a b. (a -> b) -> a -> b
$ Located (IdP GhcPs)
-> Maybe [LHsTyVarBndr GhcPs]
-> [GenLocated (SrcSpanAnn Any) HsType']
-> LexicalFixity
-> GenLocated (SrcSpanAnn Any) HsType'
-> FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
forall pass rhs.
(XCFamEqn pass rhs ~ NoExtField) =>
GenLocated (SrcSpanAnn Any) (IdP pass)
-> Maybe [LHsTyVarBndr pass]
-> [LHsType pass]
-> LexicalFixity
-> rhs
-> FamEqn pass rhs
famEqn
            (RdrNameStr -> LocatedN RdrName
typeRdrName RdrNameStr
name)
            Maybe [LHsTyVarBndr GhcPs]
forall a. Maybe a
eqn_bndrs
            ((HsType' -> GenLocated (SrcSpanAnn Any) HsType')
-> [HsType'] -> [GenLocated (SrcSpanAnn Any) HsType']
forall a b. (a -> b) -> [a] -> [b]
map HsType' -> GenLocated (SrcSpanAnn Any) HsType'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated [HsType']
params)
            LexicalFixity
Prefix
            (HsType' -> GenLocated (SrcSpanAnn Any) HsType'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated HsType'
ty)
  where
#if MIN_VERSION_ghc(9,2,0)
    tyFamInstDecl = withEpAnnNotUsed TyFamInstDecl
#else
    tyFamInstDecl :: FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
-> TyFamInstDecl'
tyFamInstDecl = TyFamInstEqn GhcPs -> TyFamInstDecl'
forall pass. TyFamInstEqn pass -> TyFamInstDecl pass
TyFamInstDecl (TyFamInstEqn GhcPs -> TyFamInstDecl')
-> (FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
    -> TyFamInstEqn GhcPs)
-> FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
-> TyFamInstDecl'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TyFamInstEqn GhcPs -> TyFamInstEqn GhcPs
forall a. a -> a
withPlaceHolder (TyFamInstEqn GhcPs -> TyFamInstEqn GhcPs)
-> (FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
    -> TyFamInstEqn GhcPs)
-> FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
-> TyFamInstEqn GhcPs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NoExtField
 -> FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
 -> TyFamInstEqn GhcPs)
-> FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
-> TyFamInstEqn GhcPs
forall a. (NoExtField -> a) -> a
noExt ((NoExtField
 -> FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
 -> TyFamInstEqn GhcPs)
-> NoExtField
-> FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
-> TyFamInstEqn GhcPs
forall a. a -> a
withPlaceHolder NoExtField
-> FamEqn GhcPs (GenLocated (SrcSpanAnn Any) HsType')
-> TyFamInstEqn GhcPs
forall pass thing.
XHsIB pass thing -> thing -> HsImplicitBndrs pass thing
HsIB)
#endif
#if MIN_VERSION_ghc(9,2,0)
    famEqn tycon bndrs pats = withEpAnnNotUsed FamEqn tycon bndrs (map HsValArg pats)
#elif MIN_VERSION_ghc(8,8,0)
    famEqn :: GenLocated (SrcSpanAnn Any) (IdP pass)
-> Maybe [LHsTyVarBndr pass]
-> [LHsType pass]
-> LexicalFixity
-> rhs
-> FamEqn pass rhs
famEqn GenLocated (SrcSpanAnn Any) (IdP pass)
tycon Maybe [LHsTyVarBndr pass]
bndrs [LHsType pass]
pats = (NoExtField
 -> GenLocated (SrcSpanAnn Any) (IdP pass)
 -> Maybe [LHsTyVarBndr pass]
 -> [HsArg (LHsType pass) (LHsType pass)]
 -> LexicalFixity
 -> rhs
 -> FamEqn pass rhs)
-> GenLocated (SrcSpanAnn Any) (IdP pass)
-> Maybe [LHsTyVarBndr pass]
-> [HsArg (LHsType pass) (LHsType pass)]
-> LexicalFixity
-> rhs
-> FamEqn pass rhs
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> GenLocated (SrcSpanAnn Any) (IdP pass)
-> Maybe [LHsTyVarBndr pass]
-> [HsArg (LHsType pass) (LHsType pass)]
-> LexicalFixity
-> rhs
-> FamEqn pass rhs
forall pass rhs.
XCFamEqn pass rhs
-> Located (IdP pass)
-> Maybe [LHsTyVarBndr pass]
-> HsTyPats pass
-> LexicalFixity
-> rhs
-> FamEqn pass rhs
FamEqn GenLocated (SrcSpanAnn Any) (IdP pass)
tycon Maybe [LHsTyVarBndr pass]
bndrs ((LHsType pass -> HsArg (LHsType pass) (LHsType pass))
-> [LHsType pass] -> [HsArg (LHsType pass) (LHsType pass)]
forall a b. (a -> b) -> [a] -> [b]
map LHsType pass -> HsArg (LHsType pass) (LHsType pass)
forall tm ty. tm -> HsArg tm ty
HsValArg [LHsType pass]
pats)
#else
    famEqn tycon _ = noExt FamEqn tycon
#endif
#if MIN_VERSION_ghc(9,2,0)
    eqn_bndrs = noExt HsOuterImplicit
#else
    eqn_bndrs :: Maybe a
eqn_bndrs = Maybe a
forall a. Maybe a
Nothing
#endif

-- | 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
 -> LocatedN RdrName
 -> LHsQTyVars GhcPs
 -> LexicalFixity
 -> GenLocated (SrcSpanAnn Any) HsType'
 -> TyClDecl GhcPs)
-> LocatedN RdrName
-> LHsQTyVars GhcPs
-> LexicalFixity
-> GenLocated (SrcSpanAnn Any) HsType'
-> TyClDecl GhcPs
forall a. (NoExtField -> a) -> a
withEpAnnNotUsed NoExtField
-> LocatedN RdrName
-> LHsQTyVars GhcPs
-> LexicalFixity
-> GenLocated (SrcSpanAnn Any) HsType'
-> TyClDecl GhcPs
forall pass.
XSynDecl pass
-> Located (IdP pass)
-> LHsQTyVars pass
-> LexicalFixity
-> LHsType pass
-> TyClDecl pass
SynDecl (RdrNameStr -> LocatedN RdrName
typeRdrName (RdrNameStr -> LocatedN RdrName) -> RdrNameStr -> LocatedN RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
name)
        ([HsTyVarBndr'] -> LHsQTyVars GhcPs
mkQTyVars [HsTyVarBndr']
vars)
        LexicalFixity
Prefix
        (HsType' -> GenLocated (SrcSpanAnn Any) HsType'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated 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
 -> LocatedN RdrName
 -> LHsQTyVars GhcPs
 -> LexicalFixity
 -> HsDataDefn GhcPs
 -> TyClDecl GhcPs)
-> LocatedN RdrName
-> LHsQTyVars GhcPs
-> LexicalFixity
-> HsDataDefn GhcPs
-> TyClDecl GhcPs
forall a. (NoExtField -> a) -> a
withEpAnnNotUsed NoExtField
-> LocatedN RdrName
-> LHsQTyVars GhcPs
-> LexicalFixity
-> HsDataDefn GhcPs
-> TyClDecl GhcPs
forall pass.
XDataDecl pass
-> Located (IdP pass)
-> LHsQTyVars pass
-> LexicalFixity
-> HsDataDefn pass
-> TyClDecl pass
DataDecl (RdrNameStr -> LocatedN RdrName
typeRdrName (RdrNameStr -> LocatedN RdrName) -> RdrNameStr -> LocatedN 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 (GenLocated (SrcSpanAnn Any) HsType')
 -> [LConDecl GhcPs]
 -> HsDeriving GhcPs
 -> HsDataDefn GhcPs)
-> NewOrData
-> LHsContext GhcPs
-> Maybe (Located CType)
-> Maybe (GenLocated (SrcSpanAnn Any) HsType')
-> [LConDecl GhcPs]
-> HsDeriving GhcPs
-> HsDataDefn GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> NewOrData
-> LHsContext GhcPs
-> Maybe (Located CType)
-> Maybe (GenLocated (SrcSpanAnn Any) 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
                LHsContext GhcPs
forall a. Located [a]
cxt
                Maybe (Located CType)
forall a. Maybe a
Nothing
                Maybe (GenLocated (SrcSpanAnn Any) HsType')
forall a. Maybe a
Nothing
                ((ConDecl' -> LConDecl GhcPs) -> [ConDecl'] -> [LConDecl GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map ConDecl' -> LConDecl GhcPs
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated [ConDecl']
conDecls)
                ([Located HsDerivingClause'] -> HsDeriving GhcPs
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
toHsDeriving ([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)
  where
#if MIN_VERSION_ghc(9,2,0)
    cxt = Nothing
#else
    cxt :: Located [a]
cxt = [a] -> Located [a]
forall e. e -> Located e
builtLoc []
#endif
#if MIN_VERSION_ghc(9,2,0)
    toHsDeriving = id
#else
    toHsDeriving :: a -> GenLocated (SrcSpanAnn Any) a
toHsDeriving = a -> GenLocated (SrcSpanAnn Any) a
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated
#endif

-- | 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
$ [GenLocated (SrcSpanAnn Any) HsType'] -> HsConDeclDetails'
forall arg rec. [arg] -> HsConDetails arg rec
prefixCon' ([GenLocated (SrcSpanAnn Any) HsType'] -> HsConDeclDetails')
-> [GenLocated (SrcSpanAnn Any) HsType'] -> HsConDeclDetails'
forall a b. (a -> b) -> a -> b
$ (Field -> GenLocated (SrcSpanAnn Any) HsType')
-> [Field] -> [GenLocated (SrcSpanAnn Any) HsType']
forall a b. (a -> b) -> [a] -> [b]
map (GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
forall a. a -> a
hsUnrestricted (GenLocated (SrcSpanAnn Any) HsType'
 -> GenLocated (SrcSpanAnn Any) HsType')
-> (Field -> GenLocated (SrcSpanAnn Any) HsType')
-> Field
-> GenLocated (SrcSpanAnn Any) HsType'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Field -> GenLocated (SrcSpanAnn Any) HsType'
renderField) [Field]
fields
  where
#if MIN_VERSION_ghc(9,2,0)
    prefixCon' = PrefixCon []
#else
    prefixCon' :: [arg] -> HsConDetails arg rec
prefixCon' = [arg] -> HsConDetails arg rec
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon
#endif

-- | 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
$ GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType' -> HsConDeclDetails'
forall arg rec. arg -> arg -> HsConDetails arg rec
InfixCon (GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
forall a. a -> a
hsUnrestricted (GenLocated (SrcSpanAnn Any) HsType'
 -> GenLocated (SrcSpanAnn Any) HsType')
-> GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
forall a b. (a -> b) -> a -> b
$ Field -> GenLocated (SrcSpanAnn Any) HsType'
renderField Field
f) (GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
forall a. a -> a
hsUnrestricted (GenLocated (SrcSpanAnn Any) HsType'
 -> GenLocated (SrcSpanAnn Any) HsType')
-> GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
forall a b. (a -> b) -> a -> b
$ Field -> GenLocated (SrcSpanAnn Any) 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
$ GenLocated
  (SrcSpanAnn Any) [GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)]
-> HsConDeclDetails'
forall arg rec. rec -> HsConDetails arg rec
RecCon (GenLocated
   (SrcSpanAnn Any) [GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)]
 -> HsConDeclDetails')
-> GenLocated
     (SrcSpanAnn Any) [GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)]
-> HsConDeclDetails'
forall a b. (a -> b) -> a -> b
$ [GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)]
-> GenLocated
     (SrcSpanAnn Any) [GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)]
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated ([GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)]
 -> GenLocated
      (SrcSpanAnn Any)
      [GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)])
-> [GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)]
-> GenLocated
     (SrcSpanAnn Any) [GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)]
forall a b. (a -> b) -> a -> b
$ ((OccNameStr, Field)
 -> GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs))
-> [(OccNameStr, Field)]
-> [GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)]
forall a b. (a -> b) -> [a] -> [b]
map (OccNameStr, Field)
-> GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)
forall ann.
(OccNameStr, Field)
-> GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)
mkLConDeclField [(OccNameStr, Field)]
fields
  where
    mkLConDeclField :: (OccNameStr, Field)
-> GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)
mkLConDeclField (OccNameStr
n, Field
f) =
        ConDeclField GhcPs
-> GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated (ConDeclField GhcPs
 -> GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs))
-> ConDeclField GhcPs
-> GenLocated (SrcSpanAnn Any) (ConDeclField GhcPs)
forall a b. (a -> b) -> a -> b
$ (NoExtField
 -> [LFieldOcc GhcPs]
 -> GenLocated (SrcSpanAnn Any) HsType'
 -> Maybe LHsDocString
 -> ConDeclField GhcPs)
-> [LFieldOcc GhcPs]
-> GenLocated (SrcSpanAnn Any) HsType'
-> Maybe LHsDocString
-> ConDeclField GhcPs
forall a. (NoExtField -> a) -> a
withEpAnnNotUsed NoExtField
-> [LFieldOcc GhcPs]
-> GenLocated (SrcSpanAnn Any) 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 -> LocatedN RdrName -> FieldOcc GhcPs)
-> LocatedN RdrName -> FieldOcc GhcPs
forall a. (NoExtField -> a) -> a
noExt NoExtField -> LocatedN RdrName -> FieldOcc GhcPs
forall pass. XCFieldOcc pass -> LocatedN RdrName -> FieldOcc pass
FieldOcc (LocatedN RdrName -> FieldOcc GhcPs)
-> LocatedN RdrName -> FieldOcc GhcPs
forall a b. (a -> b) -> a -> b
$ RdrNameStr -> LocatedN RdrName
valueRdrName (RdrNameStr -> LocatedN RdrName) -> RdrNameStr -> LocatedN RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
n]
                        (Field -> GenLocated (SrcSpanAnn Any) 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 -> LHsType GhcPs
-- TODO: parenthesizeTypeForApp is an overestimate in the case of
-- rendering an infix or record type.
renderField :: Field -> GenLocated (SrcSpanAnn Any) HsType'
renderField Field
f = GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
wrap (GenLocated (SrcSpanAnn Any) HsType'
 -> GenLocated (SrcSpanAnn Any) HsType')
-> GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
forall a b. (a -> b) -> a -> b
$ GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
parenthesizeTypeForApp (GenLocated (SrcSpanAnn Any) HsType'
 -> GenLocated (SrcSpanAnn Any) HsType')
-> GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
forall a b. (a -> b) -> a -> b
$ HsType' -> GenLocated (SrcSpanAnn Any) HsType'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated (HsType' -> GenLocated (SrcSpanAnn Any) HsType')
-> HsType' -> GenLocated (SrcSpanAnn Any) HsType'
forall a b. (a -> b) -> a -> b
$ Field -> HsType'
fieldType Field
f
  where
    wrap :: GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
wrap = case Field -> SrcStrictness
strictness Field
f of
        SrcStrictness
NoSrcStrict -> GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
forall a. a -> a
id
        SrcStrictness
s -> HsType' -> GenLocated (SrcSpanAnn Any) HsType'
forall a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated (HsType' -> GenLocated (SrcSpanAnn Any) HsType')
-> (GenLocated (SrcSpanAnn Any) HsType' -> HsType')
-> GenLocated (SrcSpanAnn Any) HsType'
-> GenLocated (SrcSpanAnn Any) HsType'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((NoExtField
 -> HsSrcBang -> GenLocated (SrcSpanAnn Any) HsType' -> HsType')
-> HsSrcBang -> GenLocated (SrcSpanAnn Any) HsType' -> HsType'
forall a. (NoExtField -> a) -> a
withEpAnnNotUsed NoExtField
-> HsSrcBang -> GenLocated (SrcSpanAnn Any) HsType' -> HsType'
forall pass.
XBangTy pass -> HsSrcBang -> LHsType pass -> HsType pass
HsBangTy (HsSrcBang -> GenLocated (SrcSpanAnn Any) HsType' -> HsType')
-> HsSrcBang -> GenLocated (SrcSpanAnn Any) 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 =
    Located (IdP GhcPs)
-> Bool
-> [LHsTyVarBndr GhcPs]
-> Maybe (LHsContext GhcPs)
-> HsConDeclDetails'
-> Maybe LHsDocString
-> ConDecl'
forall pass.
(XConDeclH98 pass ~ NoExtField) =>
GenLocated (SrcSpanAnn Any) (IdP pass)
-> Bool
-> [LHsTyVarBndr pass]
-> Maybe (LHsContext pass)
-> HsConDeclDetails pass
-> Maybe LHsDocString
-> ConDecl pass
conDeclH98 (RdrNameStr -> LocatedN RdrName
typeRdrName (RdrNameStr -> LocatedN RdrName) -> RdrNameStr -> LocatedN RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
name) Bool
False [] Maybe (LHsContext GhcPs)
forall a. Maybe a
Nothing HsConDeclDetails'
details Maybe LHsDocString
forall a. Maybe a
Nothing
  where
#if MIN_VERSION_ghc(9,2,0)
    conDeclH98 = withEpAnnNotUsed ConDeclH98
#elif MIN_VERSION_ghc(8,6,0)
    conDeclH98 :: GenLocated (SrcSpanAnn Any) (IdP pass)
-> Bool
-> [LHsTyVarBndr pass]
-> Maybe (LHsContext pass)
-> HsConDeclDetails pass
-> Maybe LHsDocString
-> ConDecl pass
conDeclH98 GenLocated (SrcSpanAnn Any) (IdP pass)
n = (NoExtField
 -> GenLocated (SrcSpanAnn Any) (IdP pass)
 -> Located Bool
 -> [LHsTyVarBndr pass]
 -> Maybe (LHsContext pass)
 -> HsConDeclDetails pass
 -> Maybe LHsDocString
 -> ConDecl pass)
-> GenLocated (SrcSpanAnn Any) (IdP pass)
-> Located Bool
-> [LHsTyVarBndr pass]
-> Maybe (LHsContext pass)
-> HsConDeclDetails pass
-> Maybe LHsDocString
-> ConDecl pass
forall a. (NoExtField -> a) -> a
noExt NoExtField
-> GenLocated (SrcSpanAnn Any) (IdP pass)
-> Located Bool
-> [LHsTyVarBndr pass]
-> Maybe (LHsContext pass)
-> HsConDeclDetails pass
-> Maybe LHsDocString
-> ConDecl pass
forall pass.
XConDeclH98 pass
-> Located (IdP pass)
-> Located Bool
-> [LHsTyVarBndr pass]
-> Maybe (LHsContext pass)
-> HsConDeclDetails pass
-> Maybe LHsDocString
-> ConDecl pass
ConDeclH98 GenLocated (SrcSpanAnn Any) (IdP pass)
n (Located Bool
 -> [LHsTyVarBndr pass]
 -> Maybe (LHsContext pass)
 -> HsConDeclDetails pass
 -> Maybe LHsDocString
 -> ConDecl pass)
-> (Bool -> Located Bool)
-> Bool
-> [LHsTyVarBndr pass]
-> Maybe (LHsContext pass)
-> HsConDeclDetails pass
-> Maybe LHsDocString
-> ConDecl pass
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Located Bool
forall e. e -> Located e
builtLoc
#else
    conDeclH98 n _ _ = ConDeclH98 n Nothing
#endif

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
withEpAnnNotUsed 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 a ann. a -> GenLocated (SrcSpanAnn Any) a
mkLocated ([LHsSigType GhcPs] -> Located [LHsSigType GhcPs])
-> [LHsSigType GhcPs] -> Located [LHsSigType GhcPs]
forall a b. (a -> b) -> a -> b
$ [LHsSigType GhcPs] -> [LHsSigType GhcPs]
forall a. a -> a
derivClauseTys ([LHsSigType GhcPs] -> [LHsSigType GhcPs])
-> [LHsSigType GhcPs] -> [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
  where
#if MIN_VERSION_ghc(9,2,0)
    derivClauseTys [x] = noExt DctSingle x
    derivClauseTys xs = noExt DctMulti xs
#else
    derivClauseTys :: a -> a
derivClauseTys = a -> a
forall a. a -> a
id
#endif

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
strat)
  where
#if MIN_VERSION_ghc(9,2,0)
    strat = withEpAnnNotUsed StockStrategy
#else
    strat :: DerivStrategy pass
strat = DerivStrategy pass
forall pass. DerivStrategy pass
StockStrategy
#endif

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
strat)
  where
#if MIN_VERSION_ghc(9,2,0)
    strat = withEpAnnNotUsed NewtypeStrategy
#else
    strat :: DerivStrategy pass
strat = DerivStrategy pass
forall pass. DerivStrategy pass
NewtypeStrategy
#endif

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
strat)
  where
#if MIN_VERSION_ghc(9,2,0)
    strat = withEpAnnNotUsed AnyclassStrategy
#else
    strat :: DerivStrategy pass
strat = DerivStrategy pass
forall pass. DerivStrategy pass
AnyclassStrategy
#endif

#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
strat (XViaStrategy GhcPs -> DerivStrategy')
-> XViaStrategy GhcPs -> DerivStrategy'
forall a b. (a -> b) -> a -> b
$ HsType' -> LHsSigType GhcPs
sigType HsType'
t)
  where
#if MIN_VERSION_ghc(9,2,0)
    strat = ViaStrategy . withEpAnnNotUsed XViaStrategyPs
#else
    strat :: XViaStrategy pass -> DerivStrategy pass
strat = XViaStrategy pass -> DerivStrategy pass
forall pass. XViaStrategy pass -> DerivStrategy pass
ViaStrategy
#endif
#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
strat)
  where
#if MIN_VERSION_ghc(9,2,0)
    strat = withEpAnnNotUsed StockStrategy
#else
    strat :: DerivStrategy pass
strat = DerivStrategy pass
forall pass. DerivStrategy pass
StockStrategy
#endif

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
strat)
  where
#if MIN_VERSION_ghc(9,2,0)
    strat = withEpAnnNotUsed NewtypeStrategy
#else
    strat :: DerivStrategy pass
strat = DerivStrategy pass
forall pass. DerivStrategy pass
NewtypeStrategy
#endif

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
strat)
  where
#if MIN_VERSION_ghc(9,2,0)
    strat = withEpAnnNotUsed AnyclassStrategy
#else
    strat :: DerivStrategy pass
strat = DerivStrategy pass
forall pass. DerivStrategy pass
AnyclassStrategy
#endif

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
withEpAnnNotUsed 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 -> LHsSigWcType GhcPs)
-> LHsSigType GhcPs -> LHsSigWcType GhcPs
forall a b. (a -> b) -> a -> b
$ HsType' -> LHsSigType GhcPs
sigType HsType'
ty) ((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
        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 -> [LocatedN RdrName] -> LHsSigType GhcPs -> Sig')
-> [LocatedN RdrName] -> LHsSigType GhcPs -> Sig'
forall a. (NoExtField -> a) -> a
withEpAnnNotUsed NoExtField -> [LocatedN RdrName] -> LHsSigType GhcPs -> Sig'
forall pass.
XPatSynSig pass
-> [Located (IdP pass)] -> LHsSigType pass -> Sig pass
PatSynSig ((OccNameStr -> LocatedN RdrName)
-> [OccNameStr] -> [LocatedN RdrName]
forall a b. (a -> b) -> [a] -> [b]
map (RdrNameStr -> LocatedN RdrName
typeRdrName (RdrNameStr -> LocatedN RdrName)
-> (OccNameStr -> RdrNameStr) -> OccNameStr -> LocatedN 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
   (LocatedN RdrName) [RecordPatSynField (LocatedN RdrName)]
 -> GenLocated (SrcSpanAnn Any) Pat'
 -> HsPatSynDir GhcPs
 -> PatSynBind GhcPs GhcPs)
-> HsConDetails
     (LocatedN RdrName) [RecordPatSynField (LocatedN RdrName)]
-> GenLocated (SrcSpanAnn Any) Pat'
-> HsPatSynDir GhcPs
-> PatSynBind GhcPs GhcPs
forall a. a -> a
withPlaceHolder ((NoExtField
 -> LocatedN RdrName
 -> HsConDetails
      (LocatedN RdrName) [RecordPatSynField (LocatedN RdrName)]
 -> GenLocated (SrcSpanAnn Any) Pat'
 -> HsPatSynDir GhcPs
 -> PatSynBind GhcPs GhcPs)
-> LocatedN RdrName
-> HsConDetails
     (LocatedN RdrName) [RecordPatSynField (LocatedN RdrName)]
-> GenLocated (SrcSpanAnn Any) Pat'
-> HsPatSynDir GhcPs
-> PatSynBind GhcPs GhcPs
forall a. (NoExtField -> a) -> a
withEpAnnNotUsed NoExtField
-> LocatedN RdrName
-> HsConDetails
     (LocatedN RdrName) [RecordPatSynField (LocatedN RdrName)]
-> GenLocated (SrcSpanAnn Any) 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 -> LocatedN RdrName
valueRdrName (RdrNameStr -> LocatedN RdrName) -> RdrNameStr -> LocatedN RdrName
forall a b. (a -> b) -> a -> b
$ OccNameStr -> RdrNameStr
unqual OccNameStr
n))
                        ([LocatedN RdrName]
-> HsConDetails
     (LocatedN RdrName) [RecordPatSynField (LocatedN RdrName)]
forall arg rec. [arg] -> HsConDetails arg rec
prefixCon' ((OccNameStr -> LocatedN RdrName)
-> [OccNameStr] -> [LocatedN RdrName]
forall a b. (a -> b) -> [a] -> [b]
map (RdrNameStr -> LocatedN RdrName
valueRdrName (RdrNameStr -> LocatedN RdrName)
-> (OccNameStr -> RdrNameStr) -> OccNameStr -> LocatedN 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
  where
#if MIN_VERSION_ghc(9,2,0)
    prefixCon' = PrefixCon []
#else
    prefixCon' :: [arg] -> HsConDetails arg rec
prefixCon' = [arg] -> HsConDetails arg rec
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon
#endif