{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE ViewPatterns #-}
#if __GLASGOW_HASKELL__ >= 914
{-# OPTIONS_GHC -Wno-pattern-namespace-specifier #-}
#endif
module Polytropos.Plugin.Source
( sourcePlugin
) where
import Prelude
import Data.Data
( Data
, gmapT
, Typeable
, cast
)
import Data.List
( nub
)
import Data.List.NonEmpty
( NonEmpty(..)
)
import GHC.Plugins
( CommandLineOption
, FastString
, fsLit
, Hsc
, GenLocated(..)
, unLoc
, mkRdrQual
, mkRdrUnqual
, mkVarOcc
, mkDataOcc
, mkModuleName
, rdrNameOcc
, occNameString
, occNameFS
, moduleNameString
)
import GHC.Hs.Extension
( GhcPs
)
import GHC.Hs
( HsParsedModule(..)
, HsModule(..)
, HsExpr(..)
, Pat(..)
, HsRecFields(..)
, HsFieldBind(..)
, FieldOcc(..)
, LHsExpr
, LHsRecField
, LPat
, LHsDecl
, noLocA
, HsConDetails(..)
, LImportDecl
, ImportDecl(..)
, noAnn
, noExtField
, HsDecl(..)
, TyClDecl(..)
, HsDataDefn(..)
, ConDecl(..)
, DataDefnCons(..)
, HsDerivingClause(..)
, DerivClauseTys(..)
, HsSigType(..)
, HsType(..)
)
import Language.Haskell.Syntax.Basic
( Boxity(..)
)
import GHC.Driver.Plugins
( ParsedResult(..)
)
import Polytropos.Plugin.Compat
( CompatRecUpdField
, matchRecordUpd
, extractRecUpdField
, pattern PrefixConPat
, stripHsPar
, polyLabel
, mkPolytroposImportDecl
, mkHsApp
, mkTupArg
, mkLambda
)
sourcePlugin :: [CommandLineOption] -> ParsedResult -> Hsc ParsedResult
sourcePlugin :: [String] -> ParsedResult -> Hsc ParsedResult
sourcePlugin [String]
_opts ParsedResult
res = do
let pm :: HsParsedModule
pm = ParsedResult -> HsParsedModule
parsedResultModule ParsedResult
res
let pm' :: HsParsedModule
pm' = HsParsedModule -> HsParsedModule
transformParsedModule HsParsedModule
pm
ParsedResult -> Hsc ParsedResult
forall a. a -> Hsc a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ParsedResult
res { parsedResultModule = pm' }
transformParsedModule :: HsParsedModule -> HsParsedModule
transformParsedModule :: HsParsedModule -> HsParsedModule
transformParsedModule pm :: HsParsedModule
pm@HsParsedModule{ hpm_module :: HsParsedModule -> Located (HsModule GhcPs)
hpm_module = L SrcSpan
modLoc HsModule GhcPs
hsmod } =
let decls0 :: [LHsDecl GhcPs]
decls0 = HsModule GhcPs -> [LHsDecl GhcPs]
forall p. HsModule p -> [LHsDecl p]
hsmodDecls HsModule GhcPs
hsmod
([String]
knownRecs, [String]
knownVars, [LHsDecl GhcPs]
decls1) = [LHsDecl GhcPs] -> ([String], [String], [LHsDecl GhcPs])
extractAndCleanDeclarations [LHsDecl GhcPs]
decls0
decls2 :: [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
decls2 = (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere ((HsExpr GhcPs -> HsExpr GhcPs) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT ([String] -> [String] -> HsExpr GhcPs -> HsExpr GhcPs
transformExpr [String]
knownRecs [String]
knownVars)) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
[LHsDecl GhcPs]
decls1
decls3 :: [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
decls3 = (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere ((Pat GhcPs -> Pat GhcPs) -> a -> a
forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT ([String] -> [String] -> Pat GhcPs -> Pat GhcPs
transformPat [String]
knownRecs [String]
knownVars)) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
decls2
hsmod' :: HsModule GhcPs
hsmod' = HsModule GhcPs
hsmod
{ hsmodDecls = decls3
, hsmodImports = ensurePolytroposImport (hsmodImports hsmod)
}
in HsParsedModule
pm { hpm_module = L modLoc hsmod' }
mkT :: (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT :: forall a b. (Typeable a, Typeable b) => (b -> b) -> a -> a
mkT b -> b
f a
x = case a -> Maybe b
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast a
x of
Just b
y -> case b -> Maybe a
forall a b. (Typeable a, Typeable b) => a -> Maybe b
cast (b -> b
f b
y) of
Just a
x' -> a
x'
Maybe a
Nothing -> a
x
Maybe b
Nothing -> a
x
everywhere :: (forall a. Data a => a -> a) -> (forall a. Data a => a -> a)
everywhere :: (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere forall a. Data a => a -> a
f a
x = a -> a
forall a. Data a => a -> a
f ((forall a. Data a => a -> a) -> a -> a
forall a. Data a => (forall a. Data a => a -> a) -> a -> a
gmapT ((forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere a -> a
forall a. Data a => a -> a
f) a
x)
extractAndCleanDeclarations :: [LHsDecl GhcPs] -> ([String], [String], [LHsDecl GhcPs])
extractAndCleanDeclarations :: [LHsDecl GhcPs] -> ([String], [String], [LHsDecl GhcPs])
extractAndCleanDeclarations [LHsDecl GhcPs]
decls =
let ([String]
recs, [String]
vars, [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
cleanedDecls) = (GenLocated SrcSpanAnnA (HsDecl GhcPs)
-> ([String], [String], [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> ([String], [String], [GenLocated SrcSpanAnnA (HsDecl GhcPs)]))
-> ([String], [String], [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
-> ([String], [String], [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr GenLocated SrcSpanAnnA (HsDecl GhcPs)
-> ([String], [String], [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
-> ([String], [String], [GenLocated SrcSpanAnnA (HsDecl GhcPs)])
forall {pass} {l} {l} {l} {l} {l} {l} {l}.
(IdP pass ~ RdrName, IdOccP pass ~ RdrName,
XRec pass (ConDecl pass) ~ GenLocated l (ConDecl pass),
XRec pass (HsSigType pass) ~ GenLocated l (HsSigType pass),
XRec pass (HsType pass) ~ GenLocated l (HsType pass),
XRec pass (HsDerivingClause pass)
~ GenLocated l (HsDerivingClause pass),
XRec pass (DerivClauseTys pass)
~ GenLocated l (DerivClauseTys pass),
XRec pass RdrName ~ GenLocated l RdrName, HasAnnotation l) =>
GenLocated l (HsDecl pass)
-> ([String], [String], [GenLocated l (HsDecl pass)])
-> ([String], [String], [GenLocated l (HsDecl pass)])
step ([], [], []) [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
[LHsDecl GhcPs]
decls
in ([String] -> [String]
forall a. Eq a => [a] -> [a]
nub [String]
recs, [String] -> [String]
forall a. Eq a => [a] -> [a]
nub [String]
vars, [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
[LHsDecl GhcPs]
cleanedDecls)
where
step :: GenLocated l (HsDecl pass)
-> ([String], [String], [GenLocated l (HsDecl pass)])
-> ([String], [String], [GenLocated l (HsDecl pass)])
step (L l
loc (TyClD XTyClD pass
x TyClDecl pass
decl)) ([String]
rAcc, [String]
vAcc, [GenLocated l (HsDecl pass)]
dAcc)
| DataDecl XDataDecl pass
ext (L l
_ RdrName
name) LHsQTyVars pass
tyvars LexicalFixity
fixity (HsDataDefn XCHsDataDefn pass
defnExt Maybe (LHsContext pass)
ctxt Maybe (XRec pass CType)
mtype Maybe (XRec pass (HsType pass))
mkind DataDefnCons (XRec pass (ConDecl pass))
cons HsDeriving pass
derivs) <- TyClDecl pass
decl
, let hasOpenRec :: Bool
hasOpenRec = (GenLocated l (HsDerivingClause pass) -> Bool)
-> [GenLocated l (HsDerivingClause pass)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any GenLocated l (HsDerivingClause pass) -> Bool
forall {pass} {l} {l} {l} {l} {l}.
(IdOccP pass ~ RdrName,
XRec pass (HsType pass) ~ GenLocated l (HsType pass),
XRec pass (HsSigType pass) ~ GenLocated l (HsSigType pass),
XRec pass (DerivClauseTys pass)
~ GenLocated l (DerivClauseTys pass),
XRec pass RdrName ~ GenLocated l RdrName) =>
GenLocated l (HsDerivingClause pass) -> Bool
isDerivingOpenRecord [GenLocated l (HsDerivingClause pass)]
HsDeriving pass
derivs
, let hasOpenVar :: Bool
hasOpenVar = (GenLocated l (HsDerivingClause pass) -> Bool)
-> [GenLocated l (HsDerivingClause pass)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any GenLocated l (HsDerivingClause pass) -> Bool
forall {pass} {l} {l} {l} {l} {l}.
(IdOccP pass ~ RdrName,
XRec pass (HsType pass) ~ GenLocated l (HsType pass),
XRec pass (HsSigType pass) ~ GenLocated l (HsSigType pass),
XRec pass (DerivClauseTys pass)
~ GenLocated l (DerivClauseTys pass),
XRec pass RdrName ~ GenLocated l RdrName) =>
GenLocated l (HsDerivingClause pass) -> Bool
isDerivingOpenVariant [GenLocated l (HsDerivingClause pass)]
HsDeriving pass
derivs
= let typeNameStr :: String
typeNameStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
name)
rAcc' :: [String]
rAcc' = if Bool
hasOpenRec then String
typeNameStr String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
rAcc else [String]
rAcc
vCons :: [String]
vCons = if Bool
hasOpenVar then DataDefnCons (GenLocated l (ConDecl pass)) -> [String]
forall {pass} {l} {l}.
(IdP pass ~ RdrName, XRec pass RdrName ~ GenLocated l RdrName,
HasAnnotation l) =>
DataDefnCons (GenLocated l (ConDecl pass)) -> [String]
extractConNames DataDefnCons (GenLocated l (ConDecl pass))
DataDefnCons (XRec pass (ConDecl pass))
cons else []
vAcc' :: [String]
vAcc' = if Bool
hasOpenVar then [String]
vCons [String] -> [String] -> [String]
forall a. [a] -> [a] -> [a]
++ [String]
vAcc else [String]
vAcc
cleanedDerivs :: [GenLocated l (HsDerivingClause pass)]
cleanedDerivs = (GenLocated l (HsDerivingClause pass) -> Bool)
-> [GenLocated l (HsDerivingClause pass)]
-> [GenLocated l (HsDerivingClause pass)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\GenLocated l (HsDerivingClause pass)
c -> Bool -> Bool
not (GenLocated l (HsDerivingClause pass) -> Bool
forall {pass} {l} {l} {l} {l} {l}.
(IdOccP pass ~ RdrName,
XRec pass (HsType pass) ~ GenLocated l (HsType pass),
XRec pass (HsSigType pass) ~ GenLocated l (HsSigType pass),
XRec pass (DerivClauseTys pass)
~ GenLocated l (DerivClauseTys pass),
XRec pass RdrName ~ GenLocated l RdrName) =>
GenLocated l (HsDerivingClause pass) -> Bool
isDerivingOpenRecord GenLocated l (HsDerivingClause pass)
c Bool -> Bool -> Bool
|| GenLocated l (HsDerivingClause pass) -> Bool
forall {pass} {l} {l} {l} {l} {l}.
(IdOccP pass ~ RdrName,
XRec pass (HsType pass) ~ GenLocated l (HsType pass),
XRec pass (HsSigType pass) ~ GenLocated l (HsSigType pass),
XRec pass (DerivClauseTys pass)
~ GenLocated l (DerivClauseTys pass),
XRec pass RdrName ~ GenLocated l RdrName) =>
GenLocated l (HsDerivingClause pass) -> Bool
isDerivingOpenVariant GenLocated l (HsDerivingClause pass)
c)) [GenLocated l (HsDerivingClause pass)]
HsDeriving pass
derivs
defn' :: HsDataDefn pass
defn' = XCHsDataDefn pass
-> Maybe (LHsContext pass)
-> Maybe (XRec pass CType)
-> Maybe (XRec pass (HsType pass))
-> DataDefnCons (XRec pass (ConDecl pass))
-> HsDeriving pass
-> HsDataDefn pass
forall pass.
XCHsDataDefn pass
-> Maybe (LHsContext pass)
-> Maybe (XRec pass CType)
-> Maybe (LHsKind pass)
-> DataDefnCons (LConDecl pass)
-> HsDeriving pass
-> HsDataDefn pass
HsDataDefn XCHsDataDefn pass
defnExt Maybe (LHsContext pass)
ctxt Maybe (XRec pass CType)
mtype Maybe (XRec pass (HsType pass))
mkind DataDefnCons (XRec pass (ConDecl pass))
cons [GenLocated l (HsDerivingClause pass)]
HsDeriving pass
cleanedDerivs
decl' :: TyClDecl pass
decl' = XDataDecl pass
-> LIdP pass
-> LHsQTyVars pass
-> LexicalFixity
-> HsDataDefn pass
-> TyClDecl pass
forall pass.
XDataDecl pass
-> LIdP pass
-> LHsQTyVars pass
-> LexicalFixity
-> HsDataDefn pass
-> TyClDecl pass
DataDecl XDataDecl pass
ext (RdrName -> GenLocated l RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA RdrName
name) LHsQTyVars pass
tyvars LexicalFixity
fixity HsDataDefn pass
defn'
in ([String]
rAcc', [String]
vAcc', l -> HsDecl pass -> GenLocated l (HsDecl pass)
forall l e. l -> e -> GenLocated l e
L l
loc (XTyClD pass -> TyClDecl pass -> HsDecl pass
forall p. XTyClD p -> TyClDecl p -> HsDecl p
TyClD XTyClD pass
x TyClDecl pass
decl') GenLocated l (HsDecl pass)
-> [GenLocated l (HsDecl pass)] -> [GenLocated l (HsDecl pass)]
forall a. a -> [a] -> [a]
: [GenLocated l (HsDecl pass)]
dAcc)
step GenLocated l (HsDecl pass)
d ([String]
rAcc, [String]
vAcc, [GenLocated l (HsDecl pass)]
dAcc) =
([String]
rAcc, [String]
vAcc, GenLocated l (HsDecl pass)
d GenLocated l (HsDecl pass)
-> [GenLocated l (HsDecl pass)] -> [GenLocated l (HsDecl pass)]
forall a. a -> [a] -> [a]
: [GenLocated l (HsDecl pass)]
dAcc)
isDerivingOpenRecord :: GenLocated l (HsDerivingClause pass) -> Bool
isDerivingOpenRecord (L l
_ (HsDerivingClause XCHsDerivingClause pass
_ Maybe (LDerivStrategy pass)
_ (L l
_ DerivClauseTys pass
dtys))) =
String -> DerivClauseTys pass -> Bool
forall {pass} {l} {l} {l}.
(IdOccP pass ~ RdrName,
XRec pass (HsType pass) ~ GenLocated l (HsType pass),
XRec pass (HsSigType pass) ~ GenLocated l (HsSigType pass),
XRec pass RdrName ~ GenLocated l RdrName) =>
String -> DerivClauseTys pass -> Bool
hasSigNamed String
"OpenRecord" DerivClauseTys pass
dtys
isDerivingOpenRecord GenLocated l (HsDerivingClause pass)
_ = Bool
False
isDerivingOpenVariant :: GenLocated l (HsDerivingClause pass) -> Bool
isDerivingOpenVariant (L l
_ (HsDerivingClause XCHsDerivingClause pass
_ Maybe (LDerivStrategy pass)
_ (L l
_ DerivClauseTys pass
dtys))) =
String -> DerivClauseTys pass -> Bool
forall {pass} {l} {l} {l}.
(IdOccP pass ~ RdrName,
XRec pass (HsType pass) ~ GenLocated l (HsType pass),
XRec pass (HsSigType pass) ~ GenLocated l (HsSigType pass),
XRec pass RdrName ~ GenLocated l RdrName) =>
String -> DerivClauseTys pass -> Bool
hasSigNamed String
"OpenVariant" DerivClauseTys pass
dtys
isDerivingOpenVariant GenLocated l (HsDerivingClause pass)
_ = Bool
False
hasSigNamed :: String -> DerivClauseTys pass -> Bool
hasSigNamed String
target (DctSingle XDctSingle pass
_ XRec pass (HsSigType pass)
sig) = String -> GenLocated l (HsSigType pass) -> Bool
forall {pass} {l} {l} {l}.
(IdOccP pass ~ RdrName,
XRec pass (HsType pass) ~ GenLocated l (HsType pass),
XRec pass RdrName ~ GenLocated l RdrName) =>
String -> GenLocated l (HsSigType pass) -> Bool
sigMatches String
target GenLocated l (HsSigType pass)
XRec pass (HsSigType pass)
sig
hasSigNamed String
target (DctMulti XDctMulti pass
_ [XRec pass (HsSigType pass)]
sigs) = (GenLocated l (HsSigType pass) -> Bool)
-> [GenLocated l (HsSigType pass)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (String -> GenLocated l (HsSigType pass) -> Bool
forall {pass} {l} {l} {l}.
(IdOccP pass ~ RdrName,
XRec pass (HsType pass) ~ GenLocated l (HsType pass),
XRec pass RdrName ~ GenLocated l RdrName) =>
String -> GenLocated l (HsSigType pass) -> Bool
sigMatches String
target) [GenLocated l (HsSigType pass)]
[XRec pass (HsSigType pass)]
sigs
hasSigNamed String
_ DerivClauseTys pass
_ = Bool
False
sigMatches :: String -> GenLocated l (HsSigType pass) -> Bool
sigMatches String
target (L l
_ (HsSig XHsSig pass
_ HsOuterSigTyVarBndrs pass
_ (L l
_ HsType pass
hty))) =
case HsType pass
hty of
HsTyVar XTyVar pass
_ PromotionFlag
_ (L l
_ RdrName
rdr) -> OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
rdr) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
target
HsAppTy XAppTy pass
_ (L l
_ (HsTyVar XTyVar pass
_ PromotionFlag
_ (L l
_ RdrName
rdr))) XRec pass (HsType pass)
_ -> OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
rdr) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
target
HsType pass
_ -> Bool
False
sigMatches String
_ GenLocated l (HsSigType pass)
_ = Bool
False
extractConNames :: DataDefnCons (GenLocated l (ConDecl pass)) -> [String]
extractConNames DataDefnCons (GenLocated l (ConDecl pass))
cons =
[ OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc (GenLocated l RdrName -> RdrName
forall l e. GenLocated l e -> e
unLoc GenLocated l RdrName
LIdP pass
conName))
| L l
_ ConDecl pass
cdecl <- case DataDefnCons (GenLocated l (ConDecl pass))
cons of
DataTypeCons Bool
_ [GenLocated l (ConDecl pass)]
cs -> [GenLocated l (ConDecl pass)]
cs
NewTypeCon GenLocated l (ConDecl pass)
c -> [GenLocated l (ConDecl pass)
c]
, let conName :: LIdP pass
conName = case ConDecl pass
cdecl of
ConDeclH98 { con_name :: forall pass. ConDecl pass -> LIdP pass
con_name = LIdP pass
n } -> LIdP pass
n
ConDeclGADT { con_names :: forall pass. ConDecl pass -> NonEmpty (LIdP pass)
con_names = (LIdP pass
n :| [LIdP pass]
_) } -> LIdP pass
n
ConDecl pass
_ -> RdrName -> GenLocated l RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (OccName -> RdrName
mkRdrUnqual (String -> OccName
mkDataOcc String
"Unknown"))
]
transformExpr :: [String] -> [String] -> HsExpr GhcPs -> HsExpr GhcPs
transformExpr :: [String] -> [String] -> HsExpr GhcPs -> HsExpr GhcPs
transformExpr [String]
knownRecs [String]
knownVars HsExpr GhcPs
expr = case HsExpr GhcPs
expr of
RecordCon XRecordCon GhcPs
_ext (L SrcSpanAnnN
_ RdrName
nm) HsRecFields{ rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcPs (LHsExpr GhcPs)]
flds }
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"REC" Bool -> Bool -> Bool
|| String
conStr String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
knownRecs
-> Bool -> [LHsRecField GhcPs (LHsExpr GhcPs)] -> HsExpr GhcPs
desugarRecordCon Bool
False [LHsRecField GhcPs (LHsExpr GhcPs)]
flds
RecordCon XRecordCon GhcPs
_ext (L SrcSpanAnnN
_ RdrName
nm) HsRecFields{ rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcPs (LHsExpr GhcPs)]
flds }
| OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"REC_F"
-> Bool -> [LHsRecField GhcPs (LHsExpr GhcPs)] -> HsExpr GhcPs
desugarRecordCon Bool
True [LHsRecField GhcPs (LHsExpr GhcPs)]
flds
RecordCon XRecordCon GhcPs
_ext (L SrcSpanAnnN
_ RdrName
nm) HsRecFields{ rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcPs (LHsExpr GhcPs)]
flds }
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"VAR" Bool -> Bool -> Bool
|| String
conStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"VARIANT"
-> [LHsRecField GhcPs (LHsExpr GhcPs)] -> HsExpr GhcPs
desugarVariantCon [LHsRecField GhcPs (LHsExpr GhcPs)]
flds
(HsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [LHsRecField GhcPs (LHsExpr GhcPs)])
matchRecordUpd -> Just (LHsExpr GhcPs -> LHsExpr GhcPs
stripHsPar -> L SrcSpanAnnA
_ (HsApp XApp GhcPs
_ (LHsExpr GhcPs -> LHsExpr GhcPs
stripHsPar -> L SrcSpanAnnA
_ (HsVar XVar GhcPs
_ (L SrcSpanAnnN
_ RdrName
nm))) LHsExpr GhcPs
r), [LHsRecField GhcPs (LHsExpr GhcPs)]
flds))
| let nmStr :: String
nmStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
nmStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"REC_UPD" Bool -> Bool -> Bool
|| String
nmStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"REC_UPD_F"
-> Bool
-> LHsExpr GhcPs
-> [LHsRecField GhcPs (LHsExpr GhcPs)]
-> HsExpr GhcPs
desugarRecordUpd (String
nmStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"REC_UPD_F") LHsExpr GhcPs
r [LHsRecField GhcPs (LHsExpr GhcPs)]
flds
HsApp XApp GhcPs
_ (LHsExpr GhcPs -> LHsExpr GhcPs
stripHsPar -> L SrcSpanAnnA
_ (HsVar XVar GhcPs
_ (L SrcSpanAnnN
_ RdrName
nm))) (LHsExpr GhcPs -> LHsExpr GhcPs
stripHsPar -> L SrcSpanAnnA
_ (HsExpr GhcPs
-> Maybe (LHsExpr GhcPs, [LHsRecField GhcPs (LHsExpr GhcPs)])
matchRecordUpd -> Just (LHsExpr GhcPs
r, [LHsRecField GhcPs (LHsExpr GhcPs)]
flds)))
| let nmStr :: String
nmStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
nmStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"REC_UPD" Bool -> Bool -> Bool
|| String
nmStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"REC_UPD_F"
-> Bool
-> LHsExpr GhcPs
-> [LHsRecField GhcPs (LHsExpr GhcPs)]
-> HsExpr GhcPs
desugarRecordUpd (String
nmStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"REC_UPD_F") LHsExpr GhcPs
r [LHsRecField GhcPs (LHsExpr GhcPs)]
flds
RecordCon XRecordCon GhcPs
_ext (L SrcSpanAnnN
_ RdrName
nm) HsRecFields{ rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcPs (LHsExpr GhcPs)]
flds }
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
knownVars
-> let injFn :: LHsExpr GhcPs
injFn = String -> LHsExpr GhcPs
polyVar String
"injectVariant"
lblExpr :: LHsExpr GhcPs
lblExpr = FastString -> LHsExpr GhcPs
polyLabel (String -> FastString
fsLit String
conStr)
recPayload :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
recPayload = HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (Bool -> [LHsRecField GhcPs (LHsExpr GhcPs)] -> HsExpr GhcPs
desugarRecordCon Bool
False [LHsRecField GhcPs (LHsExpr GhcPs)]
flds)
in GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs
forall l e. GenLocated l e -> e
unLoc (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
injFn LHsExpr GhcPs
lblExpr) GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
recPayload)
HsApp XApp GhcPs
appExt (L SrcSpanAnnA
_ (HsVar XVar GhcPs
_ (L SrcSpanAnnN
_ RdrName
nm))) LHsExpr GhcPs
arg
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
knownVars
-> let injFn :: LHsExpr GhcPs
injFn = String -> LHsExpr GhcPs
polyVar String
"injectVariant"
lblExpr :: LHsExpr GhcPs
lblExpr = FastString -> LHsExpr GhcPs
polyLabel (String -> FastString
fsLit String
conStr)
in XApp GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
forall p. XApp p -> LHsExpr p -> LHsExpr p -> HsExpr p
HsApp XApp GhcPs
appExt (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
injFn LHsExpr GhcPs
lblExpr) LHsExpr GhcPs
arg
HsVar XVar GhcPs
_ (L SrcSpanAnnN
_ RdrName
nm)
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
knownVars
-> let injFn :: LHsExpr GhcPs
injFn = String -> LHsExpr GhcPs
polyVar String
"injectVariant"
lblExpr :: LHsExpr GhcPs
lblExpr = FastString -> LHsExpr GhcPs
polyLabel (String -> FastString
fsLit String
conStr)
emptyFn :: LHsExpr GhcPs
emptyFn = String -> LHsExpr GhcPs
polyVar String
"emptyRecord"
in GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs
forall l e. GenLocated l e -> e
unLoc (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
injFn LHsExpr GhcPs
lblExpr) GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
emptyFn)
HsExpr GhcPs
other -> HsExpr GhcPs
other
desugarRecordCon :: Bool -> [LHsRecField GhcPs (LHsExpr GhcPs)] -> HsExpr GhcPs
desugarRecordCon :: Bool -> [LHsRecField GhcPs (LHsExpr GhcPs)] -> HsExpr GhcPs
desugarRecordCon Bool
isHKD [LHsRecField GhcPs (LHsExpr GhcPs)]
flds =
let emptyFn :: LHsExpr GhcPs
emptyFn = String -> LHsExpr GhcPs
polyVar (if Bool
isHKD then String
"emptyRecordF" else String
"emptyRecord")
insertFnName :: String
insertFnName = if Bool
isHKD then String
"insertFieldF" else String
"insertField"
foldStep :: LHsRecField GhcPs (LHsExpr GhcPs) -> LHsExpr GhcPs -> LHsExpr GhcPs
foldStep :: LHsRecField GhcPs (LHsExpr GhcPs) -> LHsExpr GhcPs -> LHsExpr GhcPs
foldStep LHsRecField GhcPs (LHsExpr GhcPs)
fld LHsExpr GhcPs
acc =
let (FastString
lbl, LHsExpr GhcPs
arg) = LHsRecField GhcPs (LHsExpr GhcPs) -> (FastString, LHsExpr GhcPs)
extractRecField LHsRecField GhcPs (LHsExpr GhcPs)
fld
insFn :: LHsExpr GhcPs
insFn = String -> LHsExpr GhcPs
polyVar String
insertFnName
lblExpr :: LHsExpr GhcPs
lblExpr = FastString -> LHsExpr GhcPs
polyLabel FastString
lbl
in LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp LHsExpr GhcPs
insFn LHsExpr GhcPs
lblExpr) LHsExpr GhcPs
arg) LHsExpr GhcPs
acc
in GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs
forall l e. GenLocated l e -> e
unLoc (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ (GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsRecField GhcPs (LHsExpr GhcPs) -> LHsExpr GhcPs -> LHsExpr GhcPs
foldStep GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
emptyFn [GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
[LHsRecField GhcPs (LHsExpr GhcPs)]
flds
desugarVariantCon :: [LHsRecField GhcPs (LHsExpr GhcPs)] -> HsExpr GhcPs
desugarVariantCon :: [LHsRecField GhcPs (LHsExpr GhcPs)] -> HsExpr GhcPs
desugarVariantCon [] =
String -> HsExpr GhcPs
forall a. HasCallStack => String -> a
error String
"Polytropos.Plugin.Source: VAR {} requires a constructor tag (e.g. VAR { Ok = 200 })"
desugarVariantCon (LHsRecField GhcPs (LHsExpr GhcPs)
fld : [LHsRecField GhcPs (LHsExpr GhcPs)]
_) =
let (FastString
lbl, LHsExpr GhcPs
arg) = LHsRecField GhcPs (LHsExpr GhcPs) -> (FastString, LHsExpr GhcPs)
extractRecField LHsRecField GhcPs (LHsExpr GhcPs)
fld
injFn :: LHsExpr GhcPs
injFn = String -> LHsExpr GhcPs
polyVar String
"injectVariant"
lblExpr :: LHsExpr GhcPs
lblExpr = FastString -> LHsExpr GhcPs
polyLabel FastString
lbl
in GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs
forall l e. GenLocated l e -> e
unLoc (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
injFn LHsExpr GhcPs
lblExpr) LHsExpr GhcPs
arg)
desugarRecordUpd :: Bool -> LHsExpr GhcPs -> [CompatRecUpdField] -> HsExpr GhcPs
desugarRecordUpd :: Bool
-> LHsExpr GhcPs
-> [LHsRecField GhcPs (LHsExpr GhcPs)]
-> HsExpr GhcPs
desugarRecordUpd Bool
isHKD LHsExpr GhcPs
r [LHsRecField GhcPs (LHsExpr GhcPs)]
flds =
let setFnName :: String
setFnName = if Bool
isHKD then String
"setFieldF" else String
"setField"
foldStep :: CompatRecUpdField -> LHsExpr GhcPs -> LHsExpr GhcPs
foldStep :: LHsRecField GhcPs (LHsExpr GhcPs) -> LHsExpr GhcPs -> LHsExpr GhcPs
foldStep LHsRecField GhcPs (LHsExpr GhcPs)
fld LHsExpr GhcPs
acc =
let (FastString
fs, LHsExpr GhcPs
arg) = LHsRecField GhcPs (LHsExpr GhcPs) -> (FastString, LHsExpr GhcPs)
extractRecUpdField LHsRecField GhcPs (LHsExpr GhcPs)
fld
setFn :: LHsExpr GhcPs
setFn = String -> LHsExpr GhcPs
polyVar String
setFnName
lblExpr :: LHsExpr GhcPs
lblExpr = FastString -> LHsExpr GhcPs
polyLabel FastString
fs
in LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp LHsExpr GhcPs
setFn LHsExpr GhcPs
lblExpr) LHsExpr GhcPs
arg) LHsExpr GhcPs
acc
in GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs
forall l e. GenLocated l e -> e
unLoc (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ (GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> [GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (HsExpr GhcPs)))
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsRecField GhcPs (LHsExpr GhcPs) -> LHsExpr GhcPs -> LHsExpr GhcPs
foldStep GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
r [GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (HsExpr GhcPs)))]
[LHsRecField GhcPs (LHsExpr GhcPs)]
flds
extractRecField :: LHsRecField GhcPs (LHsExpr GhcPs) -> (FastString, LHsExpr GhcPs)
(L SrcSpanAnnA
_ (HsFieldBind XHsFieldBind (GenLocated SrcSpanAnnA (FieldOcc GhcPs))
_ (L SrcSpanAnnA
_ (FieldOcc XCFieldOcc GhcPs
_ (L SrcSpanAnnN
_ RdrName
lbl))) GenLocated SrcSpanAnnA (HsExpr GhcPs)
arg Bool
pun)) =
let fs :: FastString
fs = OccName -> FastString
occNameFS (RdrName -> OccName
rdrNameOcc RdrName
lbl)
arg' :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
arg' = if Bool
pun then HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (XVar GhcPs -> LIdOccP GhcPs -> HsExpr GhcPs
forall p. XVar p -> LIdOccP p -> HsExpr p
HsVar NoExtField
XVar GhcPs
noExtField (RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA RdrName
lbl)) else GenLocated SrcSpanAnnA (HsExpr GhcPs)
arg
in (FastString
fs, GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
arg')
transformPat :: [String] -> [String] -> Pat GhcPs -> Pat GhcPs
transformPat :: [String] -> [String] -> Pat GhcPs -> Pat GhcPs
transformPat [String]
knownRecs [String]
knownVars Pat GhcPs
pat = case Pat GhcPs
pat of
ConPat XConPat GhcPs
_ext (L SrcSpanAnnN
_ RdrName
nm) (RecCon HsRecFields{ rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcPs (LPat GhcPs)]
flds })
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"REC" Bool -> Bool -> Bool
|| String
conStr String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
knownRecs
-> [LHsRecField GhcPs (LPat GhcPs)] -> Pat GhcPs
desugarRecordPat [LHsRecField GhcPs (LPat GhcPs)]
flds
ConPat XConPat GhcPs
_ext (L SrcSpanAnnN
_ RdrName
nm) (RecCon HsRecFields{ rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcPs (LPat GhcPs)]
flds })
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"VAR" Bool -> Bool -> Bool
|| String
conStr String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"VARIANT"
-> [LHsRecField GhcPs (LPat GhcPs)] -> Pat GhcPs
desugarVariantPat [LHsRecField GhcPs (LPat GhcPs)]
flds
ConPat XConPat GhcPs
_ext (L SrcSpanAnnN
_ RdrName
nm) (RecCon HsRecFields{ rec_flds :: forall p arg. HsRecFields p arg -> [LHsRecField p arg]
rec_flds = [LHsRecField GhcPs (LPat GhcPs)]
flds })
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
knownVars
-> let viewExpr :: LHsExpr GhcPs
viewExpr = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (String -> LHsExpr GhcPs
polyVar String
"projectVariant") (FastString -> LHsExpr GhcPs
polyLabel (String -> FastString
fsLit String
conStr))
justCon :: GenLocated SrcSpanAnnN RdrName
justCon = RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (OccName -> RdrName
mkRdrUnqual (String -> OccName
mkDataOcc String
"Just"))
innerPat :: GenLocated SrcSpanAnnA (Pat GhcPs)
innerPat = Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA ([LHsRecField GhcPs (LPat GhcPs)] -> Pat GhcPs
desugarRecordPat [LHsRecField GhcPs (LPat GhcPs)]
flds)
justPat :: GenLocated SrcSpanAnnA (Pat GhcPs)
justPat = Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs))
-> Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XConPat GhcPs
-> XRec GhcPs (ConLikeP GhcPs)
-> HsConDetails (LPat GhcPs) (HsRecFields GhcPs (LPat GhcPs))
-> Pat GhcPs
forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
forall a. NoAnn a => a
noAnn GenLocated SrcSpanAnnN RdrName
XRec GhcPs (ConLikeP GhcPs)
justCon ([GenLocated SrcSpanAnnA (Pat GhcPs)]
-> HsConDetails
(GenLocated SrcSpanAnnA (Pat GhcPs))
(HsRecFields GhcPs (GenLocated SrcSpanAnnA (Pat GhcPs)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixConPat [GenLocated SrcSpanAnnA (Pat GhcPs)
innerPat])
in XViewPat GhcPs -> LHsExpr GhcPs -> LPat GhcPs -> Pat GhcPs
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat TokRarrow
XViewPat GhcPs
forall a. NoAnn a => a
noAnn LHsExpr GhcPs
viewExpr GenLocated SrcSpanAnnA (Pat GhcPs)
LPat GhcPs
justPat
ConPat XConPat GhcPs
_ext (L SrcSpanAnnN
_ RdrName
nm) (PrefixConPat [GenLocated SrcSpanAnnA (Pat GhcPs)
argPat])
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
knownVars
-> let viewExpr :: LHsExpr GhcPs
viewExpr = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (String -> LHsExpr GhcPs
polyVar String
"projectVariant") (FastString -> LHsExpr GhcPs
polyLabel (String -> FastString
fsLit String
conStr))
justCon :: GenLocated SrcSpanAnnN RdrName
justCon = RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (OccName -> RdrName
mkRdrUnqual (String -> OccName
mkDataOcc String
"Just"))
justPat :: GenLocated SrcSpanAnnA (Pat GhcPs)
justPat = Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs))
-> Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XConPat GhcPs
-> XRec GhcPs (ConLikeP GhcPs)
-> HsConDetails (LPat GhcPs) (HsRecFields GhcPs (LPat GhcPs))
-> Pat GhcPs
forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
forall a. NoAnn a => a
noAnn GenLocated SrcSpanAnnN RdrName
XRec GhcPs (ConLikeP GhcPs)
justCon ([GenLocated SrcSpanAnnA (Pat GhcPs)]
-> HsConDetails
(GenLocated SrcSpanAnnA (Pat GhcPs))
(HsRecFields GhcPs (GenLocated SrcSpanAnnA (Pat GhcPs)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixConPat [GenLocated SrcSpanAnnA (Pat GhcPs)
argPat])
in XViewPat GhcPs -> LHsExpr GhcPs -> LPat GhcPs -> Pat GhcPs
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat TokRarrow
XViewPat GhcPs
forall a. NoAnn a => a
noAnn LHsExpr GhcPs
viewExpr GenLocated SrcSpanAnnA (Pat GhcPs)
LPat GhcPs
justPat
ConPat XConPat GhcPs
_ext (L SrcSpanAnnN
_ RdrName
nm) (PrefixConPat [])
| let conStr :: String
conStr = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc RdrName
nm)
, String
conStr String -> [String] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String]
knownVars
-> let viewExpr :: LHsExpr GhcPs
viewExpr = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (String -> LHsExpr GhcPs
polyVar String
"projectVariant") (FastString -> LHsExpr GhcPs
polyLabel (String -> FastString
fsLit String
conStr))
justCon :: GenLocated SrcSpanAnnN RdrName
justCon = RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (OccName -> RdrName
mkRdrUnqual (String -> OccName
mkDataOcc String
"Just"))
wildPat :: GenLocated SrcSpanAnnA (Pat GhcPs)
wildPat = Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (XWildPat GhcPs -> Pat GhcPs
forall p. XWildPat p -> Pat p
WildPat NoExtField
XWildPat GhcPs
noExtField)
justPat :: GenLocated SrcSpanAnnA (Pat GhcPs)
justPat = Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs))
-> Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XConPat GhcPs
-> XRec GhcPs (ConLikeP GhcPs)
-> HsConDetails (LPat GhcPs) (HsRecFields GhcPs (LPat GhcPs))
-> Pat GhcPs
forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
forall a. NoAnn a => a
noAnn GenLocated SrcSpanAnnN RdrName
XRec GhcPs (ConLikeP GhcPs)
justCon ([GenLocated SrcSpanAnnA (Pat GhcPs)]
-> HsConDetails
(GenLocated SrcSpanAnnA (Pat GhcPs))
(HsRecFields GhcPs (GenLocated SrcSpanAnnA (Pat GhcPs)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixConPat [GenLocated SrcSpanAnnA (Pat GhcPs)
wildPat])
in XViewPat GhcPs -> LHsExpr GhcPs -> LPat GhcPs -> Pat GhcPs
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat TokRarrow
XViewPat GhcPs
forall a. NoAnn a => a
noAnn LHsExpr GhcPs
viewExpr GenLocated SrcSpanAnnA (Pat GhcPs)
LPat GhcPs
justPat
Pat GhcPs
other -> Pat GhcPs
other
desugarRecordPat :: [LHsRecField GhcPs (LPat GhcPs)] -> Pat GhcPs
desugarRecordPat :: [LHsRecField GhcPs (LPat GhcPs)] -> Pat GhcPs
desugarRecordPat [] =
XWildPat GhcPs -> Pat GhcPs
forall p. XWildPat p -> Pat p
WildPat NoExtField
XWildPat GhcPs
noExtField
desugarRecordPat [LHsRecField GhcPs (LPat GhcPs)
fld] =
let (FastString
lbl, LPat GhcPs
p) = LHsRecField GhcPs (LPat GhcPs) -> (FastString, LPat GhcPs)
extractRecPatField LHsRecField GhcPs (LPat GhcPs)
fld
viewExpr :: LHsExpr GhcPs
viewExpr = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (String -> LHsExpr GhcPs
polyVar String
"recordField") (FastString -> LHsExpr GhcPs
polyLabel FastString
lbl)
in XViewPat GhcPs -> LHsExpr GhcPs -> LPat GhcPs -> Pat GhcPs
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat TokRarrow
XViewPat GhcPs
forall a. NoAnn a => a
noAnn LHsExpr GhcPs
viewExpr LPat GhcPs
p
desugarRecordPat [LHsRecField GhcPs (LPat GhcPs)]
flds =
let ([FastString]
lbls, [GenLocated SrcSpanAnnA (Pat GhcPs)]
pats) = [(FastString, GenLocated SrcSpanAnnA (Pat GhcPs))]
-> ([FastString], [GenLocated SrcSpanAnnA (Pat GhcPs)])
forall a b. [(a, b)] -> ([a], [b])
unzip ((GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (Pat GhcPs)))
-> (FastString, GenLocated SrcSpanAnnA (Pat GhcPs)))
-> [GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (Pat GhcPs)))]
-> [(FastString, GenLocated SrcSpanAnnA (Pat GhcPs))]
forall a b. (a -> b) -> [a] -> [b]
map GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (Pat GhcPs)))
-> (FastString, GenLocated SrcSpanAnnA (Pat GhcPs))
LHsRecField GhcPs (LPat GhcPs) -> (FastString, LPat GhcPs)
extractRecPatField [GenLocated
SrcSpanAnnA
(HsFieldBind
(GenLocated SrcSpanAnnA (FieldOcc GhcPs))
(GenLocated SrcSpanAnnA (Pat GhcPs)))]
[LHsRecField GhcPs (LPat GhcPs)]
flds)
rVar :: RdrName
rVar = OccName -> RdrName
mkRdrUnqual (String -> OccName
mkVarOcc String
"_r")
rExpr :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
rExpr = HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (XVar GhcPs -> LIdOccP GhcPs -> HsExpr GhcPs
forall p. XVar p -> LIdOccP p -> HsExpr p
HsVar NoExtField
XVar GhcPs
noExtField (RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA RdrName
rVar))
mkProj :: FastString -> LHsExpr GhcPs
mkProj FastString
lbl = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (String -> LHsExpr GhcPs
polyVar String
"recordField") (FastString -> LHsExpr GhcPs
polyLabel FastString
lbl)) GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
rExpr
tupExpr :: GenLocated SrcSpanAnnA (HsExpr GhcPs)
tupExpr = HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall a b. (a -> b) -> a -> b
$ XExplicitTuple GhcPs -> [HsTupArg GhcPs] -> Boxity -> HsExpr GhcPs
forall p. XExplicitTuple p -> [HsTupArg p] -> Boxity -> HsExpr p
ExplicitTuple (EpaLocation, EpaLocation)
XExplicitTuple GhcPs
forall a. NoAnn a => a
noAnn ((FastString -> HsTupArg GhcPs) -> [FastString] -> [HsTupArg GhcPs]
forall a b. (a -> b) -> [a] -> [b]
map (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsTupArg GhcPs
LHsExpr GhcPs -> HsTupArg GhcPs
mkTupArg (GenLocated SrcSpanAnnA (HsExpr GhcPs) -> HsTupArg GhcPs)
-> (FastString -> GenLocated SrcSpanAnnA (HsExpr GhcPs))
-> FastString
-> HsTupArg GhcPs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FastString -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
FastString -> LHsExpr GhcPs
mkProj) [FastString]
lbls) Boxity
Boxed
rPat :: GenLocated SrcSpanAnnA (Pat GhcPs)
rPat = Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (XVarPat GhcPs -> LIdP GhcPs -> Pat GhcPs
forall p. XVarPat p -> LIdP p -> Pat p
VarPat NoExtField
XVarPat GhcPs
noExtField (RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA RdrName
rVar))
lamExpr :: LHsExpr GhcPs
lamExpr = [LPat GhcPs] -> LHsExpr GhcPs -> LHsExpr GhcPs
mkLambda [GenLocated SrcSpanAnnA (Pat GhcPs)
LPat GhcPs
rPat] GenLocated SrcSpanAnnA (HsExpr GhcPs)
LHsExpr GhcPs
tupExpr
tupPat :: GenLocated SrcSpanAnnA (Pat GhcPs)
tupPat = Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs))
-> Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XTuplePat GhcPs -> [LPat GhcPs] -> Boxity -> Pat GhcPs
forall p. XTuplePat p -> [LPat p] -> Boxity -> Pat p
TuplePat XTuplePat GhcPs
forall a. NoAnn a => a
noAnn [GenLocated SrcSpanAnnA (Pat GhcPs)]
[LPat GhcPs]
pats Boxity
Boxed
in XViewPat GhcPs -> LHsExpr GhcPs -> LPat GhcPs -> Pat GhcPs
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat TokRarrow
XViewPat GhcPs
forall a. NoAnn a => a
noAnn LHsExpr GhcPs
lamExpr GenLocated SrcSpanAnnA (Pat GhcPs)
LPat GhcPs
tupPat
desugarVariantPat :: [LHsRecField GhcPs (LPat GhcPs)] -> Pat GhcPs
desugarVariantPat :: [LHsRecField GhcPs (LPat GhcPs)] -> Pat GhcPs
desugarVariantPat [] =
String -> Pat GhcPs
forall a. HasCallStack => String -> a
error String
"Polytropos.Plugin.Source: VAR {} pattern requires a constructor tag"
desugarVariantPat (LHsRecField GhcPs (LPat GhcPs)
fld : [LHsRecField GhcPs (LPat GhcPs)]
_) =
let (FastString
lbl, LPat GhcPs
p) = LHsRecField GhcPs (LPat GhcPs) -> (FastString, LPat GhcPs)
extractRecPatField LHsRecField GhcPs (LPat GhcPs)
fld
viewExpr :: LHsExpr GhcPs
viewExpr = LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
mkHsApp (String -> LHsExpr GhcPs
polyVar String
"projectVariant") (FastString -> LHsExpr GhcPs
polyLabel FastString
lbl)
justCon :: GenLocated SrcSpanAnnN RdrName
justCon = RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (OccName -> RdrName
mkRdrUnqual (String -> OccName
mkDataOcc String
"Just"))
justPat :: GenLocated SrcSpanAnnA (Pat GhcPs)
justPat = Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs))
-> Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XConPat GhcPs
-> XRec GhcPs (ConLikeP GhcPs)
-> HsConDetails (LPat GhcPs) (HsRecFields GhcPs (LPat GhcPs))
-> Pat GhcPs
forall p.
XConPat p -> XRec p (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat XConPat GhcPs
forall a. NoAnn a => a
noAnn GenLocated SrcSpanAnnN RdrName
XRec GhcPs (ConLikeP GhcPs)
justCon ([LPat GhcPs]
-> HsConDetails (LPat GhcPs) (HsRecFields GhcPs (LPat GhcPs))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixConPat [LPat GhcPs
p])
in XViewPat GhcPs -> LHsExpr GhcPs -> LPat GhcPs -> Pat GhcPs
forall p. XViewPat p -> LHsExpr p -> LPat p -> Pat p
ViewPat TokRarrow
XViewPat GhcPs
forall a. NoAnn a => a
noAnn LHsExpr GhcPs
viewExpr GenLocated SrcSpanAnnA (Pat GhcPs)
LPat GhcPs
justPat
extractRecPatField :: LHsRecField GhcPs (LPat GhcPs) -> (FastString, LPat GhcPs)
(L SrcSpanAnnA
_ (HsFieldBind XHsFieldBind (GenLocated SrcSpanAnnA (FieldOcc GhcPs))
_ (L SrcSpanAnnA
_ (FieldOcc XCFieldOcc GhcPs
_ (L SrcSpanAnnN
_ RdrName
lbl))) GenLocated SrcSpanAnnA (Pat GhcPs)
p Bool
pun)) =
let fs :: FastString
fs = OccName -> FastString
occNameFS (RdrName -> OccName
rdrNameOcc RdrName
lbl)
p' :: GenLocated SrcSpanAnnA (Pat GhcPs)
p' = if Bool
pun then Pat GhcPs -> GenLocated SrcSpanAnnA (Pat GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (XVarPat GhcPs -> LIdP GhcPs -> Pat GhcPs
forall p. XVarPat p -> LIdP p -> Pat p
VarPat NoExtField
XVarPat GhcPs
noExtField (RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA RdrName
lbl)) else GenLocated SrcSpanAnnA (Pat GhcPs)
p
in (FastString
fs, GenLocated SrcSpanAnnA (Pat GhcPs)
LPat GhcPs
p')
polyVar :: String -> LHsExpr GhcPs
polyVar :: String -> LHsExpr GhcPs
polyVar String
occ = HsExpr GhcPs -> GenLocated SrcSpanAnnA (HsExpr GhcPs)
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (XVar GhcPs -> LIdOccP GhcPs -> HsExpr GhcPs
forall p. XVar p -> LIdOccP p -> HsExpr p
HsVar NoExtField
XVar GhcPs
noExtField (RdrName -> GenLocated SrcSpanAnnN RdrName
forall e a. HasAnnotation e => a -> GenLocated e a
noLocA (ModuleName -> OccName -> RdrName
mkRdrQual (String -> ModuleName
mkModuleName String
"Polytropos") (String -> OccName
mkVarOcc String
occ))))
ensurePolytroposImport :: [LImportDecl GhcPs] -> [LImportDecl GhcPs]
ensurePolytroposImport :: [LImportDecl GhcPs] -> [LImportDecl GhcPs]
ensurePolytroposImport [LImportDecl GhcPs]
imps =
if (GenLocated SrcSpanAnnA (ImportDecl GhcPs) -> Bool)
-> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any GenLocated SrcSpanAnnA (ImportDecl GhcPs) -> Bool
forall {pass} {l} {l}.
(XRec pass ModuleName ~ GenLocated l ModuleName) =>
GenLocated l (ImportDecl pass) -> Bool
isPolytroposImport [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
[LImportDecl GhcPs]
imps
then [LImportDecl GhcPs]
imps
else GenLocated SrcSpanAnnA (ImportDecl GhcPs)
LImportDecl GhcPs
mkPolytroposImportDecl GenLocated SrcSpanAnnA (ImportDecl GhcPs)
-> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
-> [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
forall a. a -> [a] -> [a]
: [GenLocated SrcSpanAnnA (ImportDecl GhcPs)]
[LImportDecl GhcPs]
imps
where
isPolytroposImport :: GenLocated l (ImportDecl pass) -> Bool
isPolytroposImport (L l
_ ImportDecl{ ideclName :: forall pass. ImportDecl pass -> XRec pass ModuleName
ideclName = L l
_ ModuleName
modNm }) =
ModuleName -> String
moduleNameString ModuleName
modNm String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"Polytropos"
isPolytroposImport GenLocated l (ImportDecl pass)
_ = Bool
False