{- | This module provides functions to generate Haskell records using
   the large-records library.
-}
module Proto3.Suite.DotProto.Generate.LargeRecord where

import Language.Haskell.Syntax
import Proto3.Suite.DotProto.Generate.Syntax

isLargeRecord :: HsDecl -> Bool
isLargeRecord :: HsDecl -> Bool
isLargeRecord (HsDataDecl SrcLoc
_ HsContext
_ HsName
_ [HsName]
_ [HsRecDecl SrcLoc
_ HsName
_ (([HsName], HsBangType)
_fld1:([HsName], HsBangType)
_fld2:[([HsName], HsBangType)]
_)] [HsQName]
_) = Bool
True
isLargeRecord HsDecl
_ = Bool
False

-- | Generate 'NFData' instance for a type using large-generics
nfDataInstD :: HsDecl -> String -> HsDecl
nfDataInstD :: HsDecl -> String -> HsDecl
nfDataInstD HsDecl
typeDecl String
typeName =
  HsQName -> [HsType] -> [HsDecl] -> HsDecl
instDecl_ (String -> HsQName
haskellName String
"NFData")
      [ String -> HsType
type_ String
typeName ]
      [ [HsMatch] -> HsDecl
HsFunBind [HsMatch
rnfDecl] | HsDecl -> Bool
isLargeRecord HsDecl
typeDecl ]
  where
    rnfDecl :: HsMatch
rnfDecl = HsName -> [HsPat] -> HsRhs -> [HsDecl] -> HsMatch
match_ (String -> HsName
HsIdent String
"rnf") []
                     (HsExp -> HsRhs
HsUnGuardedRhs (HsQName -> HsExp
HsVar (String -> HsQName
lrName String
"grnf")))
                     []