{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
{-# LANGUAGE ViewPatterns #-}

module PyF.Internal.Meta (toExp, baseDynFlags, toName) where

#if MIN_VERSION_ghc(9,2,0)
import GHC.Hs.Type (HsWildCardBndrs (..), HsType (..), HsSigType(HsSig), sig_body)
#elif MIN_VERSION_ghc(9,0,0)
import GHC.Hs.Type (HsWildCardBndrs (..), HsType (..), HsImplicitBndrs(HsIB), hsib_body)
#elif MIN_VERSION_ghc(8,10,0)
import GHC.Hs.Types (HsWildCardBndrs (..), HsType (..), HsImplicitBndrs (HsIB, hsib_body))
#else
import HsTypes (HsWildCardBndrs (..), HsType (..), HsImplicitBndrs (HsIB), hsib_body)
#endif

#if MIN_VERSION_ghc(9,6,0)
import Language.Haskell.Syntax.Basic (field_label)
#endif

#if MIN_VERSION_ghc(8,10,0)
import GHC.Hs.Expr as Expr
import GHC.Hs.Extension as Ext
import GHC.Hs.Pat as Pat
import GHC.Hs.Lit
#else
import HsExpr as Expr
import HsExtension as Ext
import HsPat as Pat
import HsLit
#endif

import qualified Data.ByteString as B
import qualified Language.Haskell.TH.Syntax as GhcTH
import qualified Language.Haskell.TH.Syntax as TH

#if MIN_VERSION_ghc(9,6,0)
import PyF.Internal.ParserEx (fakeSettings)
#else
import PyF.Internal.ParserEx (fakeLlvmConfig, fakeSettings)
#endif

#if MIN_VERSION_ghc(9,6,0)
import GHC.Types.SourceText (il_value, rationalFromFractionalLit,SourceText(..))
#endif

#if MIN_VERSION_ghc(9,0,0)
import GHC.Types.SrcLoc
import GHC.Types.Name
import GHC.Types.Name.Reader
import GHC.Data.FastString
#if MIN_VERSION_ghc(9,2,0)
import GHC.Utils.Outputable (ppr)
import GHC.Types.Basic (Boxity(..))
#if MIN_VERSION_ghc(9,6,0)
import GHC.Types.SourceText (FractionalLit)
#else
import GHC.Types.SourceText (il_value, rationalFromFractionalLit, FractionalLit)
#endif
import GHC.Driver.Ppr (showSDoc)
#else
import GHC.Utils.Outputable (ppr, showSDoc)
import GHC.Types.Basic (il_value, fl_value, Boxity(..))
#endif
import GHC.Driver.Session (DynFlags, xopt_set, defaultDynFlags)
import qualified GHC.Unit.Module as Module
#else
import SrcLoc
import Name
import RdrName
import FastString
import Outputable (ppr, showSDoc)
import BasicTypes (il_value, fl_value, Boxity(..))
import DynFlags (DynFlags, xopt_set, defaultDynFlags)
import qualified Module
#endif

import GHC.Stack

#if MIN_VERSION_ghc(9,2,0)
-- TODO: why this disapears in GHC >= 9.2?
fl_value :: FractionalLit -> Rational
fl_value :: FractionalLit -> Rational
fl_value = FractionalLit -> Rational
rationalFromFractionalLit
#endif

toLit :: HsLit GhcPs -> TH.Lit
toLit :: HsLit GhcPs -> Lit
toLit (HsChar XHsChar GhcPs
_ Char
c) = Char -> Lit
TH.CharL Char
c
toLit (HsCharPrim XHsCharPrim GhcPs
_ Char
c) = Char -> Lit
TH.CharPrimL Char
c
toLit (HsString XHsString GhcPs
_ FastString
s) = String -> Lit
TH.StringL (FastString -> String
unpackFS FastString
s)
toLit (HsStringPrim XHsStringPrim GhcPs
_ ByteString
s) = [Word8] -> Lit
TH.StringPrimL (ByteString -> [Word8]
B.unpack ByteString
s)
toLit (HsInt XHsInt GhcPs
_ IntegralLit
i) = Integer -> Lit
TH.IntegerL (IntegralLit -> Integer
il_value IntegralLit
i)
toLit (HsIntPrim XHsIntPrim GhcPs
_ Integer
i) = Integer -> Lit
TH.IntPrimL Integer
i
toLit (HsWordPrim XHsWordPrim GhcPs
_ Integer
i) = Integer -> Lit
TH.WordPrimL Integer
i
toLit (HsInt64Prim XHsInt64Prim GhcPs
_ Integer
i) = Integer -> Lit
TH.IntegerL Integer
i
toLit (HsWord64Prim XHsWord64Prim GhcPs
_ Integer
i) = Integer -> Lit
TH.WordPrimL Integer
i
toLit (HsInteger XHsInteger GhcPs
_ Integer
i Type
_) = Integer -> Lit
TH.IntegerL Integer
i
toLit (HsRat XHsRat GhcPs
_ FractionalLit
f Type
_) = Rational -> Lit
TH.FloatPrimL (FractionalLit -> Rational
fl_value FractionalLit
f)
toLit (HsFloatPrim XHsFloatPrim GhcPs
_ FractionalLit
f) = Rational -> Lit
TH.FloatPrimL (FractionalLit -> Rational
fl_value FractionalLit
f)
toLit (HsDoublePrim XHsDoublePrim GhcPs
_ FractionalLit
f) = Rational -> Lit
TH.DoublePrimL (FractionalLit -> Rational
fl_value FractionalLit
f)

#if !MIN_VERSION_ghc(9,0,0)
toLit (XLit _) = noTH "toLit" "XLit"
#endif

toLit' :: OverLitVal -> TH.Lit
toLit' :: OverLitVal -> Lit
toLit' (HsIntegral IntegralLit
i) = Integer -> Lit
TH.IntegerL (IntegralLit -> Integer
il_value IntegralLit
i)
toLit' (HsFractional FractionalLit
f) = Rational -> Lit
TH.RationalL (FractionalLit -> Rational
fl_value FractionalLit
f)
toLit' (HsIsString SourceText
_ FastString
fs) = String -> Lit
TH.StringL (FastString -> String
unpackFS FastString
fs)

toType :: HsType GhcPs -> TH.Type
toType :: HsType GhcPs -> Type
toType (HsWildCardTy XWildCardTy GhcPs
_) = Type
TH.WildCardT
toType (HsTyVar XTyVar GhcPs
_ PromotionFlag
_ LIdP GhcPs
n) =
  let n' :: RdrName
n' = forall l e. GenLocated l e -> e
unLoc LIdP GhcPs
n
   in if RdrName -> Bool
isRdrTyVar RdrName
n'
        then Name -> Type
TH.VarT (RdrName -> Name
toName RdrName
n')
        else Name -> Type
TH.ConT (RdrName -> Name
toName RdrName
n')
toType HsType GhcPs
t = forall e a. (HasCallStack, Show e) => String -> e -> a
todo String
"toType" (DynFlags -> SDoc -> String
showSDoc ([Extension] -> DynFlags
baseDynFlags []) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Outputable a => a -> SDoc
ppr forall a b. (a -> b) -> a -> b
$ HsType GhcPs
t)

toName :: RdrName -> TH.Name
toName :: RdrName -> Name
toName RdrName
n = case RdrName
n of
  (Unqual OccName
o) -> String -> Name
TH.mkName (OccName -> String
occNameString OccName
o)
  (Qual ModuleName
m OccName
o) -> String -> Name
TH.mkName (ModuleName -> String
Module.moduleNameString ModuleName
m forall a. Semigroup a => a -> a -> a
<> String
"." forall a. Semigroup a => a -> a -> a
<> OccName -> String
occNameString OccName
o)
  (Orig Module
_m OccName
_o) -> forall a. HasCallStack => String -> a
error String
"PyFMeta: not supported toName (Orig _)"
  (Exact Name
nm) -> case forall a. NamedThing a => a -> String
getOccString Name
nm of
    String
"[]" -> '[]
    String
"()" -> '()
    String
_ -> forall a. HasCallStack => String -> a
error String
"toName: exact name encountered"

toFieldExp :: a
toFieldExp :: forall a. a
toFieldExp = forall a. HasCallStack => a
undefined

toPat :: DynFlags -> Pat.Pat GhcPs -> TH.Pat
toPat :: DynFlags -> Pat GhcPs -> Pat
toPat DynFlags
_dynFlags (Pat.VarPat XVarPat GhcPs
_ (forall l e. GenLocated l e -> e
unLoc -> RdrName
name)) = Name -> Pat
TH.VarP (RdrName -> Name
toName RdrName
name)
toPat DynFlags
dynFlags Pat GhcPs
p = forall e a. (HasCallStack, Show e) => String -> e -> a
todo String
"Advanced pattern match are not supported in PyF. See https://github.com/guibou/PyF/issues/107 if that's a problem for you." (DynFlags -> SDoc -> String
showSDoc DynFlags
dynFlags forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Outputable a => a -> SDoc
ppr forall a b. (a -> b) -> a -> b
$ Pat GhcPs
p)

{- ORMOLU_DISABLE -}
toExp :: DynFlags -> Expr.HsExpr GhcPs -> TH.Exp
toExp :: DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
_ (Expr.HsVar XVar GhcPs
_ LIdP GhcPs
n) =
  let n' :: RdrName
n' = forall l e. GenLocated l e -> e
unLoc LIdP GhcPs
n
   in if RdrName -> Bool
isRdrDataCon RdrName
n'
        then Name -> Exp
TH.ConE (RdrName -> Name
toName RdrName
n')
        else Name -> Exp
TH.VarE (RdrName -> Name
toName RdrName
n')
#if MIN_VERSION_ghc(9,6,0)
toExp _ (Expr.HsUnboundVar _ n)              = TH.UnboundVarE (TH.mkName . occNameString . rdrNameOcc $ n)
#elif MIN_VERSION_ghc(9,0,0)
toExp DynFlags
_ (Expr.HsUnboundVar XUnboundVar GhcPs
_ OccName
n)              = Name -> Exp
TH.UnboundVarE (String -> Name
TH.mkName forall b c a. (b -> c) -> (a -> b) -> a -> c
. OccName -> String
occNameString forall a b. (a -> b) -> a -> b
$ OccName
n)
#else
toExp _ (Expr.HsUnboundVar _ n)              = TH.UnboundVarE (TH.mkName . occNameString . Expr.unboundVarOcc $ n)
#endif
toExp DynFlags
_ Expr.HsIPVar {} = forall e a. (HasCallStack, Show e) => String -> e -> a
noTH String
"toExp" String
"HsIPVar"
toExp DynFlags
_ (Expr.HsLit XLitE GhcPs
_ HsLit GhcPs
l) = Lit -> Exp
TH.LitE (HsLit GhcPs -> Lit
toLit HsLit GhcPs
l)
toExp DynFlags
_ (Expr.HsOverLit XOverLitE GhcPs
_ OverLit {OverLitVal
ol_val :: forall p. HsOverLit p -> OverLitVal
ol_val :: OverLitVal
ol_val}) = Lit -> Exp
TH.LitE (OverLitVal -> Lit
toLit' OverLitVal
ol_val)
toExp DynFlags
d (Expr.HsApp XApp GhcPs
_ XRec GhcPs (HsExpr GhcPs)
e1 XRec GhcPs (HsExpr GhcPs)
e2) = Exp -> Exp -> Exp
TH.AppE (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (HsExpr GhcPs)
e1) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (HsExpr GhcPs)
e2)
#if MIN_VERSION_ghc(9,6,0)
toExp d (Expr.HsAppType _ e _ HsWC{hswc_body}) = TH.AppTypeE (toExp d . unLoc $ e) (toType . unLoc $ hswc_body)
toExp d (Expr.ExprWithTySig _ e HsWC{hswc_body=unLoc -> HsSig{sig_body}}) = TH.SigE (toExp d . unLoc $ e) (toType . unLoc $ sig_body)
#elif MIN_VERSION_ghc(9,2,0)
toExp DynFlags
d (Expr.HsAppType XAppTypeE GhcPs
_ XRec GhcPs (HsExpr GhcPs)
e HsWC {LHsType (NoGhcTc GhcPs)
hswc_body :: forall pass thing. HsWildCardBndrs pass thing -> thing
hswc_body :: LHsType (NoGhcTc GhcPs)
hswc_body}) = Exp -> Type -> Exp
TH.AppTypeE (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (HsExpr GhcPs)
e) (HsType GhcPs -> Type
toType forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ LHsType (NoGhcTc GhcPs)
hswc_body)
toExp DynFlags
d (Expr.ExprWithTySig XExprWithTySig GhcPs
_ XRec GhcPs (HsExpr GhcPs)
e HsWC{hswc_body :: forall pass thing. HsWildCardBndrs pass thing -> thing
hswc_body=forall l e. GenLocated l e -> e
unLoc -> HsSig{LHsType GhcPs
sig_body :: LHsType GhcPs
sig_body :: forall pass. HsSigType pass -> LHsType pass
sig_body}}) = Exp -> Type -> Exp
TH.SigE (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (HsExpr GhcPs)
e) (HsType GhcPs -> Type
toType forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ LHsType GhcPs
sig_body)
#elif MIN_VERSION_ghc(8,8,0)
toExp d (Expr.HsAppType _ e HsWC {hswc_body}) = TH.AppTypeE (toExp d . unLoc $ e) (toType . unLoc $ hswc_body)
toExp d (Expr.ExprWithTySig _ e HsWC{hswc_body=HsIB{hsib_body}}) = TH.SigE (toExp d . unLoc $ e) (toType . unLoc $ hsib_body)
#else
toExp d (Expr.HsAppType HsWC {hswc_body} e) = TH.AppTypeE (toExp d . unLoc $ e) (toType . unLoc $ hswc_body)
toExp d (Expr.ExprWithTySig HsWC{hswc_body=HsIB{hsib_body}} e) = TH.SigE (toExp d . unLoc $ e) (toType . unLoc $ hsib_body)
#endif
toExp DynFlags
d (Expr.OpApp XOpApp GhcPs
_ XRec GhcPs (HsExpr GhcPs)
e1 XRec GhcPs (HsExpr GhcPs)
o XRec GhcPs (HsExpr GhcPs)
e2) = Exp -> Exp -> Exp -> Exp
TH.UInfixE (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (HsExpr GhcPs)
e1) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (HsExpr GhcPs)
o) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (HsExpr GhcPs)
e2)
toExp DynFlags
d (Expr.NegApp XNegApp GhcPs
_ XRec GhcPs (HsExpr GhcPs)
e SyntaxExpr GhcPs
_) = Exp -> Exp -> Exp
TH.AppE (Name -> Exp
TH.VarE 'negate) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (HsExpr GhcPs)
e)
-- NOTE: for lambda, there is only one match
#if MIN_VERSION_ghc(9,6,0)
toExp d (Expr.HsLam _ (Expr.MG _ (unLoc -> (map unLoc -> [Expr.Match _ _ (map unLoc -> ps) (Expr.GRHSs _ [unLoc -> Expr.GRHS _ _ (unLoc -> e)] _)])))) = TH.LamE (fmap (toPat d) ps) (toExp d e)
#else
toExp DynFlags
d (Expr.HsLam XLam GhcPs
_ (Expr.MG XMG GhcPs (XRec GhcPs (HsExpr GhcPs))
_ (forall l e. GenLocated l e -> e
unLoc -> (forall a b. (a -> b) -> [a] -> [b]
map forall l e. GenLocated l e -> e
unLoc -> [Expr.Match XCMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ HsMatchContext (NoGhcTc GhcPs)
_ (forall a b. (a -> b) -> [a] -> [b]
map forall l e. GenLocated l e -> e
unLoc -> [Pat GhcPs]
ps) (Expr.GRHSs XCGRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [forall l e. GenLocated l e -> e
unLoc -> Expr.GRHS XCGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))
_ [GuardLStmt GhcPs]
_ (forall l e. GenLocated l e -> e
unLoc -> HsExpr GhcPs
e)] HsLocalBinds GhcPs
_)])) Origin
_)) = [Pat] -> Exp -> Exp
TH.LamE (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (DynFlags -> Pat GhcPs -> Pat
toPat DynFlags
d) [Pat GhcPs]
ps) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d HsExpr GhcPs
e)
#endif
-- toExp (Expr.Let _ bs e)                       = TH.LetE (toDecs bs) (toExp e)
-- toExp (Expr.If _ a b c)                       = TH.CondE (toExp a) (toExp b) (toExp c)
-- toExp (Expr.MultiIf _ ifs)                    = TH.MultiIfE (map toGuard ifs)
-- toExp (Expr.Case _ e alts)                    = TH.CaseE (toExp e) (map toMatch alts)
-- toExp (Expr.Do _ ss)                          = TH.DoE (map toStmt ss)
-- toExp e@Expr.MDo{}                            = noTH "toExp" e
#if MIN_VERSION_ghc(9, 2, 0)
toExp DynFlags
d (Expr.ExplicitTuple XExplicitTuple GhcPs
_ [HsTupArg GhcPs]
args Boxity
boxity) = [Maybe Exp] -> Exp
ctor [Maybe Exp]
tupArgs
#else
toExp d (Expr.ExplicitTuple _ (map unLoc -> args) boxity) = ctor tupArgs
#endif
  where
    toTupArg :: HsTupArg id -> Maybe (HsExpr id)
toTupArg (Expr.Present XPresent id
_ XRec id (HsExpr id)
e) = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc XRec id (HsExpr id)
e
    toTupArg (Expr.Missing XMissing id
_) = forall a. Maybe a
Nothing
    toTupArg HsTupArg id
_ = forall a. HasCallStack => String -> a
error String
"impossible case"

    ctor :: [Maybe Exp] -> Exp
ctor = case Boxity
boxity of
      Boxity
Boxed -> [Maybe Exp] -> Exp
TH.TupE
      Boxity
Unboxed -> [Maybe Exp] -> Exp
TH.UnboxedTupE

#if MIN_VERSION_ghc(8,10,0)
    tupArgs :: [Maybe Exp]
tupArgs = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {id} {l}.
(XRec id (HsExpr id) ~ GenLocated l (HsExpr id)) =>
HsTupArg id -> Maybe (HsExpr id)
toTupArg) [HsTupArg GhcPs]
args
#else
    tupArgs = case traverse toTupArg args of
      Nothing -> error "Tuple section are not supported by template haskell < 8.10"
      Just args' -> fmap (toExp d) args'
#endif

{- ORMOLU_ENABLE -}

-- toExp (Expr.List _ xs)                        = TH.ListE (fmap toExp xs)
#if MIN_VERSION_ghc(9,3,0)
toExp d (Expr.HsPar _ _ e _) = TH.ParensE (toExp d . unLoc $ e)
#else
toExp DynFlags
d (Expr.HsPar XPar GhcPs
_ XRec GhcPs (HsExpr GhcPs)
e) = Exp -> Exp
TH.ParensE (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (HsExpr GhcPs)
e)
#endif
toExp DynFlags
d (Expr.SectionL XSectionL GhcPs
_ (forall l e. GenLocated l e -> e
unLoc -> HsExpr GhcPs
a) (forall l e. GenLocated l e -> e
unLoc -> HsExpr GhcPs
b)) = Maybe Exp -> Exp -> Maybe Exp -> Exp
TH.InfixE (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ HsExpr GhcPs
a) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d HsExpr GhcPs
b) forall a. Maybe a
Nothing
toExp DynFlags
d (Expr.SectionR XSectionR GhcPs
_ (forall l e. GenLocated l e -> e
unLoc -> HsExpr GhcPs
a) (forall l e. GenLocated l e -> e
unLoc -> HsExpr GhcPs
b)) = Maybe Exp -> Exp -> Maybe Exp -> Exp
TH.InfixE forall a. Maybe a
Nothing (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d HsExpr GhcPs
a) (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ HsExpr GhcPs
b)
toExp DynFlags
_ (Expr.RecordCon XRecordCon GhcPs
_ XRec GhcPs (ConLikeP GhcPs)
name HsRecFields {[LHsRecField GhcPs (XRec GhcPs (HsExpr GhcPs))]
rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds :: [LHsRecField GhcPs (XRec GhcPs (HsExpr GhcPs))]
rec_flds}) =
  Name -> [FieldExp] -> Exp
TH.RecConE (RdrName -> Name
toName forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ XRec GhcPs (ConLikeP GhcPs)
name) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a
toFieldExp [LHsRecField GhcPs (XRec GhcPs (HsExpr GhcPs))]
rec_flds)

-- toExp (Expr.RecUpdate _ e xs)                 = TH.RecUpdE (toExp e) (fmap toFieldExp xs)
-- toExp (Expr.ListComp _ e ss)                  = TH.CompE $ map convert ss ++ [TH.NoBindS (toExp e)]
--  where
--   convert (Expr.QualStmt _ st)                = toStmt st
--   convert s                                   = noTH "toExp ListComp" s
-- toExp (Expr.ExpTypeSig _ e t)                 = TH.SigE (toExp e) (toType t)
#if MIN_VERSION_ghc(9, 2, 0)
toExp DynFlags
d (Expr.ExplicitList XExplicitList GhcPs
_ (forall a b. (a -> b) -> [a] -> [b]
map forall l e. GenLocated l e -> e
unLoc -> [HsExpr GhcPs]
args)) = [Exp] -> Exp
TH.ListE (forall a b. (a -> b) -> [a] -> [b]
map (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d) [HsExpr GhcPs]
args)
#else
toExp d (Expr.ExplicitList _ _ (map unLoc -> args)) = TH.ListE (map (toExp d) args)
#endif
toExp DynFlags
d (Expr.ArithSeq XArithSeq GhcPs
_ Maybe (SyntaxExpr GhcPs)
_ ArithSeqInfo GhcPs
e) = Range -> Exp
TH.ArithSeqE forall a b. (a -> b) -> a -> b
$ case ArithSeqInfo GhcPs
e of
  (From XRec GhcPs (HsExpr GhcPs)
a) -> Exp -> Range
TH.FromR (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc XRec GhcPs (HsExpr GhcPs)
a)
  (FromThen XRec GhcPs (HsExpr GhcPs)
a XRec GhcPs (HsExpr GhcPs)
b) -> Exp -> Exp -> Range
TH.FromThenR (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc XRec GhcPs (HsExpr GhcPs)
a) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc XRec GhcPs (HsExpr GhcPs)
b)
  (FromTo XRec GhcPs (HsExpr GhcPs)
a XRec GhcPs (HsExpr GhcPs)
b) -> Exp -> Exp -> Range
TH.FromToR (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc XRec GhcPs (HsExpr GhcPs)
a) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc XRec GhcPs (HsExpr GhcPs)
b)
  (FromThenTo XRec GhcPs (HsExpr GhcPs)
a XRec GhcPs (HsExpr GhcPs)
b XRec GhcPs (HsExpr GhcPs)
c) -> Exp -> Exp -> Exp -> Range
TH.FromThenToR (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc XRec GhcPs (HsExpr GhcPs)
a) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc XRec GhcPs (HsExpr GhcPs)
b) (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
d forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> e
unLoc XRec GhcPs (HsExpr GhcPs)
c)
#if MIN_VERSION_ghc(9,6,0)
toExp _ (HsOverLabel _ lbl _) = TH.LabelE (fromSourceText lbl)
   where
     fromSourceText :: SourceText -> String
     fromSourceText (SourceText s) = s
     fromSourceText NoSourceText = ""
#elif MIN_VERSION_ghc(9, 2, 0)
toExp DynFlags
_ (HsOverLabel XOverLabel GhcPs
_ FastString
lbl) = String -> Exp
TH.LabelE (FastString -> String
unpackFS FastString
lbl)
#else
-- It's not quite clear what to do in case when overloaded syntax is
-- enabled thus match on Nothing
toExp _ (HsOverLabel _ Nothing lbl) = TH.LabelE (unpackFS lbl)
#endif
#if MIN_VERSION_ghc(9,6,0)
toExp dynFlags (HsGetField _ expr field) = TH.GetFieldE (toExp dynFlags (unLoc expr)) (unpackFS . field_label . unLoc . dfoLabel . unLoc $ field)
#elif MIN_VERSION_ghc(9, 4, 0)
toExp dynFlags (HsGetField _ expr field) = TH.GetFieldE (toExp dynFlags (unLoc expr)) (unpackFS . unLoc . dfoLabel . unLoc $ field)
toExp _ (HsProjection _ fields) = TH.ProjectionE (fmap (unpackFS . unLoc . dfoLabel . unLoc) fields)
#elif MIN_VERSION_ghc(9, 2, 0)
toExp DynFlags
dynFlags (HsGetField XGetField GhcPs
_ XRec GhcPs (HsExpr GhcPs)
expr Located (HsFieldLabel GhcPs)
field) = Exp -> String -> Exp
TH.GetFieldE (DynFlags -> HsExpr GhcPs -> Exp
toExp DynFlags
dynFlags (forall l e. GenLocated l e -> e
unLoc XRec GhcPs (HsExpr GhcPs)
expr)) (FastString -> String
unpackFS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p. HsFieldLabel p -> GenLocated SrcSpan FastString
hflLabel forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall a b. (a -> b) -> a -> b
$ Located (HsFieldLabel GhcPs)
field)
toExp DynFlags
_ (HsProjection XProjection GhcPs
_ NonEmpty (Located (HsFieldLabel GhcPs))
fields) = NonEmpty String -> Exp
TH.ProjectionE (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (FastString -> String
unpackFS forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall p. HsFieldLabel p -> GenLocated SrcSpan FastString
hflLabel forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l e. GenLocated l e -> e
unLoc) NonEmpty (Located (HsFieldLabel GhcPs))
fields)
#endif
toExp DynFlags
dynFlags HsExpr GhcPs
e = forall e a. (HasCallStack, Show e) => String -> e -> a
todo String
"toExp" (DynFlags -> SDoc -> String
showSDoc DynFlags
dynFlags forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Outputable a => a -> SDoc
ppr forall a b. (a -> b) -> a -> b
$ HsExpr GhcPs
e)

todo :: (HasCallStack, Show e) => String -> e -> a
todo :: forall e a. (HasCallStack, Show e) => String -> e -> a
todo String
fun e
thing = forall a. HasCallStack => String -> a
error forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ [String
moduleName, String
".", String
fun, String
": not implemented: ", forall a. Show a => a -> String
show e
thing, String
"Please open an issue at https://github.com/guibou/PyF/issues"]

noTH :: (HasCallStack, Show e) => String -> e -> a
noTH :: forall e a. (HasCallStack, Show e) => String -> e -> a
noTH String
fun e
thing = forall a. HasCallStack => String -> a
error forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall a b. (a -> b) -> a -> b
$ [String
moduleName, String
".", String
fun, String
": no TemplateHaskell for: ", forall a. Show a => a -> String
show e
thing, String
"Please open an issue at https://github.com/guibou/PyF/issues"]

moduleName :: String
moduleName :: String
moduleName = String
"PyF.Internal.Meta"

baseDynFlags :: [GhcTH.Extension] -> DynFlags
baseDynFlags :: [Extension] -> DynFlags
baseDynFlags [Extension]
exts = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl DynFlags -> Extension -> DynFlags
xopt_set DynFlags
dynFlags [Extension]
enable
  where
    enable :: [Extension]
enable = Extension
GhcTH.TemplateHaskellQuotes forall a. a -> [a] -> [a]
: [Extension]
exts
#if MIN_VERSION_ghc(9,6,0)
    dynFlags = defaultDynFlags fakeSettings
#else
    dynFlags :: DynFlags
dynFlags = Settings -> LlvmConfig -> DynFlags
defaultDynFlags Settings
fakeSettings LlvmConfig
fakeLlvmConfig
#endif