{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE TupleSections #-}
module GHC.Core.Opt.CallerCC
( addCallerCostCentres
, CallerCcFilter
, parseCallerCcFilter
) where
import Data.Bifunctor
import Data.Word (Word8)
import Data.Maybe
import qualified Text.Parsec as P
import Control.Applicative
import Control.Monad.Trans.State.Strict
import Data.Either
import Control.Monad
import GHC.Prelude
import GHC.Utils.Outputable as Outputable
import GHC.Driver.Session
import GHC.Driver.Ppr
import GHC.Types.CostCentre
import GHC.Types.CostCentre.State
import GHC.Types.Name hiding (varName)
import GHC.Types.Tickish
import GHC.Unit.Module.Name
import GHC.Unit.Module.ModGuts
import GHC.Types.SrcLoc
import GHC.Types.Var
import GHC.Unit.Types
import GHC.Data.FastString
import GHC.Core
import GHC.Core.Opt.Monad
import GHC.Utils.Panic
import qualified GHC.Utils.Binary as B
addCallerCostCentres :: ModGuts -> CoreM ModGuts
addCallerCostCentres :: ModGuts -> CoreM ModGuts
addCallerCostCentres ModGuts
guts = do
DynFlags
dflags <- CoreM DynFlags
forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let filters :: [CallerCcFilter]
filters = DynFlags -> [CallerCcFilter]
callerCcFilters DynFlags
dflags
let env :: Env
env :: Env
env = Env
{ thisModule :: Module
thisModule = ModGuts -> Module
mg_module ModGuts
guts
, ccState :: CostCentreState
ccState = CostCentreState
newCostCentreState
, dflags :: DynFlags
dflags = DynFlags
dflags
, revParents :: [Id]
revParents = []
, filters :: [CallerCcFilter]
filters = [CallerCcFilter]
filters
}
let guts' :: ModGuts
guts' = ModGuts
guts { mg_binds :: CoreProgram
mg_binds = Env -> CoreProgram -> CoreProgram
doCoreProgram Env
env (ModGuts -> CoreProgram
mg_binds ModGuts
guts)
}
ModGuts -> CoreM ModGuts
forall (m :: * -> *) a. Monad m => a -> m a
return ModGuts
guts'
doCoreProgram :: Env -> CoreProgram -> CoreProgram
doCoreProgram :: Env -> CoreProgram -> CoreProgram
doCoreProgram Env
env CoreProgram
binds = (State CostCentreState CoreProgram
-> CostCentreState -> CoreProgram)
-> CostCentreState
-> State CostCentreState CoreProgram
-> CoreProgram
forall a b c. (a -> b -> c) -> b -> a -> c
flip State CostCentreState CoreProgram -> CostCentreState -> CoreProgram
forall s a. State s a -> s -> a
evalState CostCentreState
newCostCentreState (State CostCentreState CoreProgram -> CoreProgram)
-> State CostCentreState CoreProgram -> CoreProgram
forall a b. (a -> b) -> a -> b
$ do
(CoreBind -> StateT CostCentreState Identity CoreBind)
-> CoreProgram -> State CostCentreState CoreProgram
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Env -> CoreBind -> StateT CostCentreState Identity CoreBind
doBind Env
env) CoreProgram
binds
doBind :: Env -> CoreBind -> M CoreBind
doBind :: Env -> CoreBind -> StateT CostCentreState Identity CoreBind
doBind Env
env (NonRec Id
b Expr Id
rhs) = Id -> Expr Id -> CoreBind
forall b. b -> Expr b -> Bind b
NonRec Id
b (Expr Id -> CoreBind)
-> StateT CostCentreState Identity (Expr Id)
-> StateT CostCentreState Identity CoreBind
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr (Id -> Env -> Env
addParent Id
b Env
env) Expr Id
rhs
doBind Env
env (Rec [(Id, Expr Id)]
bs) = [(Id, Expr Id)] -> CoreBind
forall b. [(b, Expr b)] -> Bind b
Rec ([(Id, Expr Id)] -> CoreBind)
-> StateT CostCentreState Identity [(Id, Expr Id)]
-> StateT CostCentreState Identity CoreBind
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((Id, Expr Id) -> StateT CostCentreState Identity (Id, Expr Id))
-> [(Id, Expr Id)]
-> StateT CostCentreState Identity [(Id, Expr Id)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Id, Expr Id) -> StateT CostCentreState Identity (Id, Expr Id)
doPair [(Id, Expr Id)]
bs
where
doPair :: (Id, Expr Id) -> StateT CostCentreState Identity (Id, Expr Id)
doPair (Id
b,Expr Id
rhs) = (Id
b,) (Expr Id -> (Id, Expr Id))
-> StateT CostCentreState Identity (Expr Id)
-> StateT CostCentreState Identity (Id, Expr Id)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr (Id -> Env -> Env
addParent Id
b Env
env) Expr Id
rhs
doExpr :: Env -> CoreExpr -> M CoreExpr
doExpr :: Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr Env
env e :: Expr Id
e@(Var Id
v)
| Env -> Id -> Bool
needsCallSiteCostCentre Env
env Id
v = do
let nameDoc :: SDoc
nameDoc :: SDoc
nameDoc = PrintUnqualified -> Depth -> SDoc -> SDoc
withUserStyle PrintUnqualified
alwaysQualify Depth
DefaultDepth (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$
[SDoc] -> SDoc
hcat (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
dot ((Id -> SDoc) -> [Id] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Env -> [Id]
parents Env
env))) SDoc -> SDoc -> SDoc
<> SDoc -> SDoc
parens (String -> SDoc
text String
"calling:" SDoc -> SDoc -> SDoc
<> Id -> SDoc
forall a. Outputable a => a -> SDoc
ppr Id
v)
ccName :: CcName
ccName :: CcName
ccName = String -> CcName
mkFastString (String -> CcName) -> String -> CcName
forall a b. (a -> b) -> a -> b
$ DynFlags -> SDoc -> String
showSDoc (Env -> DynFlags
dflags Env
env) SDoc
nameDoc
CostCentreIndex
ccIdx <- CcName -> M CostCentreIndex
getCCIndex' CcName
ccName
let span :: SrcSpan
span = case Env -> [Id]
revParents Env
env of
Id
top:[Id]
_ -> Name -> SrcSpan
nameSrcSpan (Name -> SrcSpan) -> Name -> SrcSpan
forall a b. (a -> b) -> a -> b
$ Id -> Name
varName Id
top
[Id]
_ -> SrcSpan
noSrcSpan
cc :: CostCentre
cc = CCFlavour -> CcName -> Module -> SrcSpan -> CostCentre
NormalCC (CostCentreIndex -> CCFlavour
ExprCC CostCentreIndex
ccIdx) CcName
ccName (Env -> Module
thisModule Env
env) SrcSpan
span
tick :: CoreTickish
tick :: CoreTickish
tick = CostCentre -> Bool -> Bool -> CoreTickish
forall (pass :: TickishPass).
CostCentre -> Bool -> Bool -> GenTickish pass
ProfNote CostCentre
cc Bool
True Bool
True
Expr Id -> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Expr Id -> StateT CostCentreState Identity (Expr Id))
-> Expr Id -> StateT CostCentreState Identity (Expr Id)
forall a b. (a -> b) -> a -> b
$ CoreTickish -> Expr Id -> Expr Id
forall b. CoreTickish -> Expr b -> Expr b
Tick CoreTickish
tick Expr Id
e
| Bool
otherwise = Expr Id -> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Expr Id
e
doExpr Env
_env e :: Expr Id
e@(Lit Literal
_) = Expr Id -> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Expr Id
e
doExpr Env
env (Expr Id
f `App` Expr Id
x) = Expr Id -> Expr Id -> Expr Id
forall b. Expr b -> Expr b -> Expr b
App (Expr Id -> Expr Id -> Expr Id)
-> StateT CostCentreState Identity (Expr Id)
-> StateT CostCentreState Identity (Expr Id -> Expr Id)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr Env
env Expr Id
f StateT CostCentreState Identity (Expr Id -> Expr Id)
-> StateT CostCentreState Identity (Expr Id)
-> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr Env
env Expr Id
x
doExpr Env
env (Lam Id
b Expr Id
x) = Id -> Expr Id -> Expr Id
forall b. b -> Expr b -> Expr b
Lam Id
b (Expr Id -> Expr Id)
-> StateT CostCentreState Identity (Expr Id)
-> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr Env
env Expr Id
x
doExpr Env
env (Let CoreBind
b Expr Id
rhs) = CoreBind -> Expr Id -> Expr Id
forall b. Bind b -> Expr b -> Expr b
Let (CoreBind -> Expr Id -> Expr Id)
-> StateT CostCentreState Identity CoreBind
-> StateT CostCentreState Identity (Expr Id -> Expr Id)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env -> CoreBind -> StateT CostCentreState Identity CoreBind
doBind Env
env CoreBind
b StateT CostCentreState Identity (Expr Id -> Expr Id)
-> StateT CostCentreState Identity (Expr Id)
-> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr Env
env Expr Id
rhs
doExpr Env
env (Case Expr Id
scrut Id
b Type
ty [Alt Id]
alts) =
Expr Id -> Id -> Type -> [Alt Id] -> Expr Id
forall b. Expr b -> b -> Type -> [Alt b] -> Expr b
Case (Expr Id -> Id -> Type -> [Alt Id] -> Expr Id)
-> StateT CostCentreState Identity (Expr Id)
-> StateT
CostCentreState Identity (Id -> Type -> [Alt Id] -> Expr Id)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr Env
env Expr Id
scrut StateT CostCentreState Identity (Id -> Type -> [Alt Id] -> Expr Id)
-> StateT CostCentreState Identity Id
-> StateT CostCentreState Identity (Type -> [Alt Id] -> Expr Id)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Id -> StateT CostCentreState Identity Id
forall (f :: * -> *) a. Applicative f => a -> f a
pure Id
b StateT CostCentreState Identity (Type -> [Alt Id] -> Expr Id)
-> StateT CostCentreState Identity Type
-> StateT CostCentreState Identity ([Alt Id] -> Expr Id)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Type -> StateT CostCentreState Identity Type
forall (f :: * -> *) a. Applicative f => a -> f a
pure Type
ty StateT CostCentreState Identity ([Alt Id] -> Expr Id)
-> StateT CostCentreState Identity [Alt Id]
-> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Alt Id -> StateT CostCentreState Identity (Alt Id))
-> [Alt Id] -> StateT CostCentreState Identity [Alt Id]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Alt Id -> StateT CostCentreState Identity (Alt Id)
doAlt [Alt Id]
alts
where
doAlt :: Alt Id -> StateT CostCentreState Identity (Alt Id)
doAlt (Alt AltCon
con [Id]
bs Expr Id
rhs) = AltCon -> [Id] -> Expr Id -> Alt Id
forall b. AltCon -> [b] -> Expr b -> Alt b
Alt AltCon
con [Id]
bs (Expr Id -> Alt Id)
-> StateT CostCentreState Identity (Expr Id)
-> StateT CostCentreState Identity (Alt Id)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr Env
env Expr Id
rhs
doExpr Env
env (Cast Expr Id
expr CoercionR
co) = Expr Id -> CoercionR -> Expr Id
forall b. Expr b -> CoercionR -> Expr b
Cast (Expr Id -> CoercionR -> Expr Id)
-> StateT CostCentreState Identity (Expr Id)
-> StateT CostCentreState Identity (CoercionR -> Expr Id)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr Env
env Expr Id
expr StateT CostCentreState Identity (CoercionR -> Expr Id)
-> StateT CostCentreState Identity CoercionR
-> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> CoercionR -> StateT CostCentreState Identity CoercionR
forall (f :: * -> *) a. Applicative f => a -> f a
pure CoercionR
co
doExpr Env
env (Tick CoreTickish
t Expr Id
e) = CoreTickish -> Expr Id -> Expr Id
forall b. CoreTickish -> Expr b -> Expr b
Tick CoreTickish
t (Expr Id -> Expr Id)
-> StateT CostCentreState Identity (Expr Id)
-> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env -> Expr Id -> StateT CostCentreState Identity (Expr Id)
doExpr Env
env Expr Id
e
doExpr Env
_env e :: Expr Id
e@(Type Type
_) = Expr Id -> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Expr Id
e
doExpr Env
_env e :: Expr Id
e@(Coercion CoercionR
_) = Expr Id -> StateT CostCentreState Identity (Expr Id)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Expr Id
e
type M = State CostCentreState
getCCIndex' :: FastString -> M CostCentreIndex
getCCIndex' :: CcName -> M CostCentreIndex
getCCIndex' CcName
name = (CostCentreState -> (CostCentreIndex, CostCentreState))
-> M CostCentreIndex
forall (m :: * -> *) s a. Monad m => (s -> (a, s)) -> StateT s m a
state (CcName -> CostCentreState -> (CostCentreIndex, CostCentreState)
getCCIndex CcName
name)
data Env = Env
{ Env -> Module
thisModule :: Module
, Env -> DynFlags
dflags :: DynFlags
, Env -> CostCentreState
ccState :: CostCentreState
, Env -> [Id]
revParents :: [Id]
, Env -> [CallerCcFilter]
filters :: [CallerCcFilter]
}
addParent :: Id -> Env -> Env
addParent :: Id -> Env -> Env
addParent Id
i Env
env = Env
env { revParents :: [Id]
revParents = Id
i Id -> [Id] -> [Id]
forall a. a -> [a] -> [a]
: Env -> [Id]
revParents Env
env }
parents :: Env -> [Id]
parents :: Env -> [Id]
parents Env
env = [Id] -> [Id]
forall a. [a] -> [a]
reverse (Env -> [Id]
revParents Env
env)
needsCallSiteCostCentre :: Env -> Id -> Bool
needsCallSiteCostCentre :: Env -> Id -> Bool
needsCallSiteCostCentre Env
env Id
i =
(CallerCcFilter -> Bool) -> [CallerCcFilter] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any CallerCcFilter -> Bool
matches (Env -> [CallerCcFilter]
filters Env
env)
where
matches :: CallerCcFilter -> Bool
matches :: CallerCcFilter -> Bool
matches CallerCcFilter
ccf =
Bool
checkModule Bool -> Bool -> Bool
&& Bool
checkFunc
where
checkModule :: Bool
checkModule =
case CallerCcFilter -> Maybe ModuleName
ccfModuleName CallerCcFilter
ccf of
Just ModuleName
modFilt
| Just Module
iMod <- Name -> Maybe Module
nameModule_maybe (Id -> Name
varName Id
i)
-> Module -> ModuleName
forall unit. GenModule unit -> ModuleName
moduleName Module
iMod ModuleName -> ModuleName -> Bool
forall a. Eq a => a -> a -> Bool
== ModuleName
modFilt
| Bool
otherwise -> Bool
False
Maybe ModuleName
Nothing -> Bool
True
checkFunc :: Bool
checkFunc =
NamePattern -> OccName -> Bool
occNameMatches (CallerCcFilter -> NamePattern
ccfFuncName CallerCcFilter
ccf) (Id -> OccName
forall a. NamedThing a => a -> OccName
getOccName Id
i)
data NamePattern
= PChar Char NamePattern
| PWildcard NamePattern
| PEnd
instance Outputable NamePattern where
ppr :: NamePattern -> SDoc
ppr (PChar Char
c NamePattern
rest) = Char -> SDoc
char Char
c SDoc -> SDoc -> SDoc
<> NamePattern -> SDoc
forall a. Outputable a => a -> SDoc
ppr NamePattern
rest
ppr (PWildcard NamePattern
rest) = Char -> SDoc
char Char
'*' SDoc -> SDoc -> SDoc
<> NamePattern -> SDoc
forall a. Outputable a => a -> SDoc
ppr NamePattern
rest
ppr NamePattern
PEnd = SDoc
Outputable.empty
instance B.Binary NamePattern where
get :: BinHandle -> IO NamePattern
get BinHandle
bh = do
Word8
tag <- BinHandle -> IO Word8
forall a. Binary a => BinHandle -> IO a
B.get BinHandle
bh
case Word8
tag :: Word8 of
Word8
0 -> Char -> NamePattern -> NamePattern
PChar (Char -> NamePattern -> NamePattern)
-> IO Char -> IO (NamePattern -> NamePattern)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BinHandle -> IO Char
forall a. Binary a => BinHandle -> IO a
B.get BinHandle
bh IO (NamePattern -> NamePattern) -> IO NamePattern -> IO NamePattern
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> BinHandle -> IO NamePattern
forall a. Binary a => BinHandle -> IO a
B.get BinHandle
bh
Word8
1 -> NamePattern -> NamePattern
PWildcard (NamePattern -> NamePattern) -> IO NamePattern -> IO NamePattern
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BinHandle -> IO NamePattern
forall a. Binary a => BinHandle -> IO a
B.get BinHandle
bh
Word8
2 -> NamePattern -> IO NamePattern
forall (f :: * -> *) a. Applicative f => a -> f a
pure NamePattern
PEnd
Word8
_ -> String -> IO NamePattern
forall a. String -> a
panic String
"Binary(NamePattern): Invalid tag"
put_ :: BinHandle -> NamePattern -> IO ()
put_ BinHandle
bh (PChar Char
x NamePattern
y) = BinHandle -> Word8 -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
B.put_ BinHandle
bh (Word8
0 :: Word8) IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BinHandle -> Char -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
B.put_ BinHandle
bh Char
x IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BinHandle -> NamePattern -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
B.put_ BinHandle
bh NamePattern
y
put_ BinHandle
bh (PWildcard NamePattern
x) = BinHandle -> Word8 -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
B.put_ BinHandle
bh (Word8
1 :: Word8) IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BinHandle -> NamePattern -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
B.put_ BinHandle
bh NamePattern
x
put_ BinHandle
bh NamePattern
PEnd = BinHandle -> Word8 -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
B.put_ BinHandle
bh (Word8
2 :: Word8)
occNameMatches :: NamePattern -> OccName -> Bool
occNameMatches :: NamePattern -> OccName -> Bool
occNameMatches NamePattern
pat = NamePattern -> String -> Bool
go NamePattern
pat (String -> Bool) -> (OccName -> String) -> OccName -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. OccName -> String
occNameString
where
go :: NamePattern -> String -> Bool
go :: NamePattern -> String -> Bool
go NamePattern
PEnd String
"" = Bool
True
go (PChar Char
c NamePattern
rest) (Char
d:String
s)
= Char
d Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
c Bool -> Bool -> Bool
&& NamePattern -> String -> Bool
go NamePattern
rest String
s
go (PWildcard NamePattern
rest) String
s
= NamePattern -> String -> Bool
go NamePattern
rest String
s Bool -> Bool -> Bool
|| NamePattern -> String -> Bool
go (NamePattern -> NamePattern
PWildcard NamePattern
rest) (String -> String
forall a. [a] -> [a]
tail String
s)
go NamePattern
_ String
_ = Bool
False
type Parser = P.Parsec String ()
parseNamePattern :: Parser NamePattern
parseNamePattern :: Parser NamePattern
parseNamePattern = Parser NamePattern
pattern
where
pattern :: Parser NamePattern
pattern = Parser NamePattern
star Parser NamePattern -> Parser NamePattern -> Parser NamePattern
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser NamePattern
wildcard Parser NamePattern -> Parser NamePattern -> Parser NamePattern
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser NamePattern
char Parser NamePattern -> Parser NamePattern -> Parser NamePattern
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser NamePattern
forall {u}. ParsecT String u Identity NamePattern
end
star :: Parser NamePattern
star = Char -> NamePattern -> NamePattern
PChar Char
'*' (NamePattern -> NamePattern)
-> ParsecT String () Identity String
-> ParsecT String () Identity (NamePattern -> NamePattern)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ String -> ParsecT String () Identity String
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m String
P.string String
"\\*" ParsecT String () Identity (NamePattern -> NamePattern)
-> Parser NamePattern -> Parser NamePattern
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser NamePattern
pattern
wildcard :: Parser NamePattern
wildcard = do
ParsecT String () Identity Char -> ParsecT String () Identity ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (ParsecT String () Identity Char -> ParsecT String () Identity ())
-> ParsecT String () Identity Char -> ParsecT String () Identity ()
forall a b. (a -> b) -> a -> b
$ Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
P.char Char
'*'
NamePattern -> NamePattern
PWildcard (NamePattern -> NamePattern)
-> Parser NamePattern -> Parser NamePattern
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser NamePattern
pattern
char :: Parser NamePattern
char = Char -> NamePattern -> NamePattern
PChar (Char -> NamePattern -> NamePattern)
-> ParsecT String () Identity Char
-> ParsecT String () Identity (NamePattern -> NamePattern)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
P.anyChar ParsecT String () Identity (NamePattern -> NamePattern)
-> Parser NamePattern -> Parser NamePattern
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser NamePattern
pattern
end :: ParsecT String u Identity NamePattern
end = NamePattern
PEnd NamePattern
-> ParsecT String u Identity ()
-> ParsecT String u Identity NamePattern
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ ParsecT String u Identity ()
forall s (m :: * -> *) t u.
(Stream s m t, Show t) =>
ParsecT s u m ()
P.eof
data CallerCcFilter
= CallerCcFilter { CallerCcFilter -> Maybe ModuleName
ccfModuleName :: Maybe ModuleName
, CallerCcFilter -> NamePattern
ccfFuncName :: NamePattern
}
instance Outputable CallerCcFilter where
ppr :: CallerCcFilter -> SDoc
ppr CallerCcFilter
ccf =
SDoc -> (ModuleName -> SDoc) -> Maybe ModuleName -> SDoc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Char -> SDoc
char Char
'*') ModuleName -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CallerCcFilter -> Maybe ModuleName
ccfModuleName CallerCcFilter
ccf)
SDoc -> SDoc -> SDoc
<> Char -> SDoc
char Char
'.'
SDoc -> SDoc -> SDoc
<> NamePattern -> SDoc
forall a. Outputable a => a -> SDoc
ppr (CallerCcFilter -> NamePattern
ccfFuncName CallerCcFilter
ccf)
instance B.Binary CallerCcFilter where
get :: BinHandle -> IO CallerCcFilter
get BinHandle
bh = Maybe ModuleName -> NamePattern -> CallerCcFilter
CallerCcFilter (Maybe ModuleName -> NamePattern -> CallerCcFilter)
-> IO (Maybe ModuleName) -> IO (NamePattern -> CallerCcFilter)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> BinHandle -> IO (Maybe ModuleName)
forall a. Binary a => BinHandle -> IO a
B.get BinHandle
bh IO (NamePattern -> CallerCcFilter)
-> IO NamePattern -> IO CallerCcFilter
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> BinHandle -> IO NamePattern
forall a. Binary a => BinHandle -> IO a
B.get BinHandle
bh
put_ :: BinHandle -> CallerCcFilter -> IO ()
put_ BinHandle
bh (CallerCcFilter Maybe ModuleName
x NamePattern
y) = BinHandle -> Maybe ModuleName -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
B.put_ BinHandle
bh Maybe ModuleName
x IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BinHandle -> NamePattern -> IO ()
forall a. Binary a => BinHandle -> a -> IO ()
B.put_ BinHandle
bh NamePattern
y
parseCallerCcFilter :: String -> Either String CallerCcFilter
parseCallerCcFilter :: String -> Either String CallerCcFilter
parseCallerCcFilter =
(ParseError -> String)
-> Either ParseError CallerCcFilter -> Either String CallerCcFilter
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ParseError -> String
forall a. Show a => a -> String
show (Either ParseError CallerCcFilter -> Either String CallerCcFilter)
-> (String -> Either ParseError CallerCcFilter)
-> String
-> Either String CallerCcFilter
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parsec String () CallerCcFilter
-> String -> String -> Either ParseError CallerCcFilter
forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
P.parse Parsec String () CallerCcFilter
parseCallerCcFilter' String
"caller-CC filter"
parseCallerCcFilter' :: Parser CallerCcFilter
parseCallerCcFilter' :: Parsec String () CallerCcFilter
parseCallerCcFilter' =
Maybe ModuleName -> NamePattern -> CallerCcFilter
CallerCcFilter
(Maybe ModuleName -> NamePattern -> CallerCcFilter)
-> ParsecT String () Identity (Maybe ModuleName)
-> ParsecT String () Identity (NamePattern -> CallerCcFilter)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity (Maybe ModuleName)
moduleFilter
ParsecT String () Identity (NamePattern -> CallerCcFilter)
-> ParsecT String () Identity Char
-> ParsecT String () Identity (NamePattern -> CallerCcFilter)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
P.char Char
'.'
ParsecT String () Identity (NamePattern -> CallerCcFilter)
-> Parser NamePattern -> Parsec String () CallerCcFilter
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser NamePattern
parseNamePattern
where
moduleFilter :: Parser (Maybe ModuleName)
moduleFilter :: ParsecT String () Identity (Maybe ModuleName)
moduleFilter =
(ModuleName -> Maybe ModuleName
forall a. a -> Maybe a
Just (ModuleName -> Maybe ModuleName)
-> (String -> ModuleName) -> String -> Maybe ModuleName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ModuleName
mkModuleName (String -> Maybe ModuleName)
-> ParsecT String () Identity String
-> ParsecT String () Identity (Maybe ModuleName)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParsecT String () Identity String
moduleName)
ParsecT String () Identity (Maybe ModuleName)
-> ParsecT String () Identity (Maybe ModuleName)
-> ParsecT String () Identity (Maybe ModuleName)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
(Maybe ModuleName
forall a. Maybe a
Nothing Maybe ModuleName
-> ParsecT String () Identity Char
-> ParsecT String () Identity (Maybe ModuleName)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
P.char Char
'*')
moduleName :: Parser String
moduleName :: ParsecT String () Identity String
moduleName = do
Char
c <- ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
P.upper
String
cs <- ParsecT String () Identity Char
-> ParsecT String () Identity String
forall (f :: * -> *) a. Alternative f => f a -> f [a]
some (ParsecT String () Identity Char
-> ParsecT String () Identity String)
-> ParsecT String () Identity Char
-> ParsecT String () Identity String
forall a b. (a -> b) -> a -> b
$ ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
P.upper ParsecT String () Identity Char
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
P.lower ParsecT String () Identity Char
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> ParsecT String () Identity Char
forall s (m :: * -> *) u. Stream s m Char => ParsecT s u m Char
P.digit ParsecT String () Identity Char
-> ParsecT String () Identity Char
-> ParsecT String () Identity Char
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> String -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
String -> ParsecT s u m Char
P.oneOf String
"_"
Maybe String
rest <- ParsecT String () Identity String
-> ParsecT String () Identity (Maybe String)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (ParsecT String () Identity String
-> ParsecT String () Identity (Maybe String))
-> ParsecT String () Identity String
-> ParsecT String () Identity (Maybe String)
forall a b. (a -> b) -> a -> b
$ ParsecT String () Identity String
-> ParsecT String () Identity String
forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
P.try (ParsecT String () Identity String
-> ParsecT String () Identity String)
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall a b. (a -> b) -> a -> b
$ Char -> ParsecT String () Identity Char
forall s (m :: * -> *) u.
Stream s m Char =>
Char -> ParsecT s u m Char
P.char Char
'.' ParsecT String () Identity Char
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> (String -> String)
-> ParsecT String () Identity String
-> ParsecT String () Identity String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Char
'.'Char -> String -> String
forall a. a -> [a] -> [a]
:) ParsecT String () Identity String
moduleName
String -> ParsecT String () Identity String
forall (m :: * -> *) a. Monad m => a -> m a
return (String -> ParsecT String () Identity String)
-> String -> ParsecT String () Identity String
forall a b. (a -> b) -> a -> b
$ Char
c Char -> String -> String
forall a. a -> [a] -> [a]
: (String
cs String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
"" Maybe String
rest)