{-# LANGUAGE TypeFamilies #-}
module GHC.Core.FVs (
exprFreeVars, exprsFreeVars,
exprFreeVarsDSet,
exprFreeVarsList, exprsFreeVarsList,
exprFreeIds, exprsFreeIds,
exprFreeIdsDSet, exprsFreeIdsDSet,
exprFreeIdsList, exprsFreeIdsList,
bindFreeVars,
InterestingVarFun,
exprSomeFreeVars, exprsSomeFreeVars,
exprSomeFreeVarsList, exprsSomeFreeVarsList, exprsSomeFreeVarsDSet,
deepExprsFreeVarsDSet,
bndrTypeTyCoFVs, bndrFVs, dBndrFreeVars,
idUnfoldingVars, bndrFreeVars,
bndrRuleAndUnfoldingVarsDSet,
bndrRuleAndUnfoldingVars,
idRuleVars, stableUnfoldingVars,
ruleFreeVars, rulesFreeVars,
rulesFreeVarsDSet, mkRuleInfo,
ruleLhsFreeIds, ruleLhsFreeIdsList,
ruleRhsFreeVars, rulesRhsFreeIds,
exprFVs, addCoreBndrFV, addCoreBndrsFV, unitFV,
orphNamesOfType, orphNamesOfTypes, orphNamesOfAxiomLHS,
orphNamesOfExprs,
FVAnn,
CoreExprWithFVs,
CoreExprWithFVs',
CoreBindWithFVs,
CoreAltWithFVs,
freeVars,
freeVarsBind,
freeVarsOf,
freeVarsOfAnn
) where
import GHC.Prelude
import GHC.Core
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Types.Name.Set
import GHC.Types.Name
import GHC.Types.Tickish
import GHC.Types.Var
import GHC.Types.Var.Set
import GHC.Types.Var.FV
import GHC.Core.Type
import GHC.Core.TyCo.Rep
import GHC.Core.TyCo.FVs
import GHC.Core.TyCon
import GHC.Core.Coercion.Axiom
import GHC.Builtin.Types( unrestrictedFunTyConName )
import GHC.Builtin.Types.Prim( fUNTyCon )
import GHC.Data.Maybe( orElse )
import GHC.Utils.EndoOS
import GHC.Utils.Misc
import GHC.Utils.Panic.Plain
exprFreeVars :: CoreExpr -> VarSet
exprFreeVars :: CoreExpr -> VarSet
exprFreeVars = DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet) -> (CoreExpr -> DVarSet) -> CoreExpr -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> DVarSet
exprFreeVarsDSet
exprFreeVarsDSet :: CoreExpr -> DVarSet
exprFreeVarsDSet :: CoreExpr -> DVarSet
exprFreeVarsDSet = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
isLocalVar (SelectiveDFV -> DVarSet)
-> (CoreExpr -> SelectiveDFV) -> CoreExpr -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> SelectiveDFV
exprFVs
exprsFreeVars :: [CoreExpr] -> VarSet
exprsFreeVars :: [CoreExpr] -> VarSet
exprsFreeVars = DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet)
-> ([CoreExpr] -> DVarSet) -> [CoreExpr] -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> DVarSet
exprsFreeVarsDSet
exprsFreeVarsList :: [CoreExpr] -> [Var]
exprsFreeVarsList :: [CoreExpr] -> [Var]
exprsFreeVarsList = InterestingVarFun -> SelectiveDFV -> [Var]
runFVSelectiveList InterestingVarFun
isLocalVar (SelectiveDFV -> [Var])
-> ([CoreExpr] -> SelectiveDFV) -> [CoreExpr] -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> SelectiveDFV
exprsFVs
exprsFreeVarsDSet :: [CoreExpr] -> DVarSet
exprsFreeVarsDSet :: [CoreExpr] -> DVarSet
exprsFreeVarsDSet = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
isLocalVar (SelectiveDFV -> DVarSet)
-> ([CoreExpr] -> SelectiveDFV) -> [CoreExpr] -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> SelectiveDFV
exprsFVs
deepExprsFreeVarsDSet :: [CoreExpr] -> DVarSet
deepExprsFreeVarsDSet :: [CoreExpr] -> DVarSet
deepExprsFreeVarsDSet = DVarSet -> DVarSet
closeOverKindsDSet (DVarSet -> DVarSet)
-> ([CoreExpr] -> DVarSet) -> [CoreExpr] -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> DVarSet
exprsFreeVarsDSet
exprFreeVarsList :: CoreExpr -> [Var]
exprFreeVarsList :: CoreExpr -> [Var]
exprFreeVarsList = DVarSet -> [Var]
dVarSetElems (DVarSet -> [Var]) -> (CoreExpr -> DVarSet) -> CoreExpr -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> DVarSet
exprFreeVarsDSet
exprFreeIds :: CoreExpr -> IdSet
exprFreeIds :: CoreExpr -> VarSet
exprFreeIds = DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet) -> (CoreExpr -> DVarSet) -> CoreExpr -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> DVarSet
exprFreeIdsDSet
exprsFreeIds :: [CoreExpr] -> IdSet
exprsFreeIds :: [CoreExpr] -> VarSet
exprsFreeIds = DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet)
-> ([CoreExpr] -> DVarSet) -> [CoreExpr] -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> DVarSet
exprsFreeIdsDSet
exprFreeIdsDSet :: CoreExpr -> DIdSet
exprFreeIdsDSet :: CoreExpr -> DVarSet
exprFreeIdsDSet = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
isLocalId (SelectiveDFV -> DVarSet)
-> (CoreExpr -> SelectiveDFV) -> CoreExpr -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> SelectiveDFV
exprFVs
exprsFreeIdsDSet :: [CoreExpr] -> DIdSet
exprsFreeIdsDSet :: [CoreExpr] -> DVarSet
exprsFreeIdsDSet = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
isLocalId (SelectiveDFV -> DVarSet)
-> ([CoreExpr] -> SelectiveDFV) -> [CoreExpr] -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> SelectiveDFV
exprsFVs
exprFreeIdsList :: CoreExpr -> [Id]
exprFreeIdsList :: CoreExpr -> [Var]
exprFreeIdsList = DVarSet -> [Var]
dVarSetElems (DVarSet -> [Var]) -> (CoreExpr -> DVarSet) -> CoreExpr -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> DVarSet
exprFreeIdsDSet
exprsFreeIdsList :: [CoreExpr] -> [Id]
exprsFreeIdsList :: [CoreExpr] -> [Var]
exprsFreeIdsList = DVarSet -> [Var]
dVarSetElems (DVarSet -> [Var])
-> ([CoreExpr] -> DVarSet) -> [CoreExpr] -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> DVarSet
exprsFreeIdsDSet
bindFreeVars :: CoreBind -> VarSet
bindFreeVars :: CoreBind -> VarSet
bindFreeVars = InterestingVarFun -> SelectiveDFV -> VarSet
runFVSelectiveSet InterestingVarFun
isLocalVar (SelectiveDFV -> VarSet)
-> (CoreBind -> SelectiveDFV) -> CoreBind -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreBind -> SelectiveDFV
bind_fvs
bind_fvs :: CoreBind -> SelectiveDFV
bind_fvs :: CoreBind -> SelectiveDFV
bind_fvs (NonRec Var
b CoreExpr
r) = (Var, CoreExpr) -> SelectiveDFV
rhs_fvs (Var
b,CoreExpr
r)
bind_fvs (Rec [(Var, CoreExpr)]
prs) = [Var] -> SelectiveDFV -> SelectiveDFV
forall f a. [Var] -> FV (f, VarSet) a -> FV (f, VarSet) a
addBndrsSelectiveFV (((Var, CoreExpr) -> Var) -> [(Var, CoreExpr)] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst [(Var, CoreExpr)]
prs) (SelectiveDFV -> SelectiveDFV) -> SelectiveDFV -> SelectiveDFV
forall a b. (a -> b) -> a -> b
$
((Var, CoreExpr) -> SelectiveDFV)
-> [(Var, CoreExpr)] -> SelectiveDFV
forall (t :: * -> *) acc a env.
(Foldable t, Monoid acc) =>
(a -> FV env acc) -> t a -> FV env acc
mapUnionFV (Var, CoreExpr) -> SelectiveDFV
rhs_fvs [(Var, CoreExpr)]
prs
exprSomeFreeVars :: InterestingVarFun
-> CoreExpr
-> VarSet
exprSomeFreeVars :: InterestingVarFun -> CoreExpr -> VarSet
exprSomeFreeVars InterestingVarFun
fv_cand = DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet) -> (CoreExpr -> DVarSet) -> CoreExpr -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InterestingVarFun -> CoreExpr -> DVarSet
exprSomeFreeVarsDSet InterestingVarFun
fv_cand
exprSomeFreeVarsList :: InterestingVarFun
-> CoreExpr
-> [Var]
exprSomeFreeVarsList :: InterestingVarFun -> CoreExpr -> [Var]
exprSomeFreeVarsList InterestingVarFun
fv_cand = DVarSet -> [Var]
dVarSetElems (DVarSet -> [Var]) -> (CoreExpr -> DVarSet) -> CoreExpr -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InterestingVarFun -> CoreExpr -> DVarSet
exprSomeFreeVarsDSet InterestingVarFun
fv_cand
exprSomeFreeVarsDSet :: InterestingVarFun
-> CoreExpr
-> DVarSet
exprSomeFreeVarsDSet :: InterestingVarFun -> CoreExpr -> DVarSet
exprSomeFreeVarsDSet InterestingVarFun
fv_cand = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
fv_cand (SelectiveDFV -> DVarSet)
-> (CoreExpr -> SelectiveDFV) -> CoreExpr -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> SelectiveDFV
exprFVs
exprsSomeFreeVars :: InterestingVarFun
-> [CoreExpr]
-> VarSet
InterestingVarFun
fv_cand = DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet)
-> ([CoreExpr] -> DVarSet) -> [CoreExpr] -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InterestingVarFun -> [CoreExpr] -> DVarSet
exprsSomeFreeVarsDSet InterestingVarFun
fv_cand
exprsSomeFreeVarsList :: InterestingVarFun
-> [CoreExpr]
-> [Var]
InterestingVarFun
fv_cand = DVarSet -> [Var]
dVarSetElems (DVarSet -> [Var])
-> ([CoreExpr] -> DVarSet) -> [CoreExpr] -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InterestingVarFun -> [CoreExpr] -> DVarSet
exprsSomeFreeVarsDSet InterestingVarFun
fv_cand
exprsSomeFreeVarsDSet :: InterestingVarFun
-> [CoreExpr]
-> DVarSet
InterestingVarFun
fv_cand = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
fv_cand (SelectiveDFV -> DVarSet)
-> ([CoreExpr] -> SelectiveDFV) -> [CoreExpr] -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreExpr] -> SelectiveDFV
exprsFVs
addCoreBndrFV :: CoreBndr -> SelectiveDFV -> SelectiveDFV
addCoreBndrFV :: Var -> SelectiveDFV -> SelectiveDFV
addCoreBndrFV Var
bndr SelectiveDFV
fvr
= Var -> SelectiveDFV
bndrTypeTyCoFVs Var
bndr SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend`
Var -> SelectiveDFV -> SelectiveDFV
forall f a. Var -> FV (f, VarSet) a -> FV (f, VarSet) a
addBndrSelectiveFV Var
bndr SelectiveDFV
fvr
addCoreBndrsFV :: [CoreBndr] -> SelectiveDFV -> SelectiveDFV
addCoreBndrsFV :: [Var] -> SelectiveDFV -> SelectiveDFV
addCoreBndrsFV [Var]
bndrs SelectiveDFV
fv = (Var -> SelectiveDFV -> SelectiveDFV)
-> SelectiveDFV -> [Var] -> SelectiveDFV
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Var -> SelectiveDFV -> SelectiveDFV
addCoreBndrFV SelectiveDFV
fv [Var]
bndrs
unitFV :: Var -> SelectiveDFV
unitFV :: Var -> SelectiveDFV
unitFV Var
v = ((InterestingVarFun, VarSet) -> EndoOS DVarSet) -> SelectiveDFV
forall env acc. (env -> acc) -> FV env acc
MkFV (\(InterestingVarFun, VarSet)
bvs -> (DVarSet -> DVarSet) -> EndoOS DVarSet
forall a. (a -> a) -> EndoOS a
EndoOS ((InterestingVarFun, VarSet) -> DVarSet -> DVarSet
do_it (InterestingVarFun, VarSet)
bvs))
where
do_it :: (InterestingVarFun, VarSet) -> DVarSet -> DVarSet
do_it (InterestingVarFun
is_interesting,VarSet
bvs) DVarSet
acc
| Bool -> Bool
not (InterestingVarFun
is_interesting Var
v) = DVarSet
acc
| Var
v Var -> VarSet -> Bool
`elemVarSet` VarSet
bvs = DVarSet
acc
| Var
v Var -> DVarSet -> Bool
`elemDVarSet` DVarSet
acc = DVarSet
acc
| Bool
otherwise = DVarSet
acc DVarSet -> Var -> DVarSet
`extendDVarSet` Var
v
exprsFVs :: [CoreExpr] -> SelectiveDFV
exprsFVs :: [CoreExpr] -> SelectiveDFV
exprsFVs = (CoreExpr -> SelectiveDFV) -> [CoreExpr] -> SelectiveDFV
forall (t :: * -> *) acc a env.
(Foldable t, Monoid acc) =>
(a -> FV env acc) -> t a -> FV env acc
mapUnionFV CoreExpr -> SelectiveDFV
exprFVs
exprFVs :: CoreExpr -> SelectiveDFV
exprFVs :: CoreExpr -> SelectiveDFV
exprFVs (Type Type
ty) = Type -> SelectiveDFV
shallowSelTypeFV Type
ty
exprFVs (Coercion Coercion
co) = Coercion -> SelectiveDFV
shallowSelCoFV Coercion
co
exprFVs (Var Var
var) = Var -> SelectiveDFV
unitFV Var
var
exprFVs (Lit Literal
_) = SelectiveDFV
forall a. Monoid a => a
mempty
exprFVs (Tick CoreTickish
t CoreExpr
expr) = CoreTickish -> SelectiveDFV
tickish_fvs CoreTickish
t SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend` CoreExpr -> SelectiveDFV
exprFVs CoreExpr
expr
exprFVs (App CoreExpr
fun CoreExpr
arg) = CoreExpr -> SelectiveDFV
exprFVs CoreExpr
fun SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend` CoreExpr -> SelectiveDFV
exprFVs CoreExpr
arg
exprFVs (Lam Var
bndr CoreExpr
body) = Var -> SelectiveDFV -> SelectiveDFV
addCoreBndrFV Var
bndr (CoreExpr -> SelectiveDFV
exprFVs CoreExpr
body)
exprFVs (Cast CoreExpr
expr Coercion
co) = CoreExpr -> SelectiveDFV
exprFVs CoreExpr
expr SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend` Coercion -> SelectiveDFV
shallowSelCoFV Coercion
co
exprFVs (Case CoreExpr
scrut Var
bndr Type
ty [Alt Var]
alts)
= CoreExpr -> SelectiveDFV
exprFVs CoreExpr
scrut SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend` Type -> SelectiveDFV
shallowSelTypeFV Type
ty SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend`
Var -> SelectiveDFV -> SelectiveDFV
addCoreBndrFV Var
bndr ((Alt Var -> SelectiveDFV) -> [Alt Var] -> SelectiveDFV
forall (t :: * -> *) acc a env.
(Foldable t, Monoid acc) =>
(a -> FV env acc) -> t a -> FV env acc
mapUnionFV Alt Var -> SelectiveDFV
alt_fvs [Alt Var]
alts)
where
alt_fvs :: Alt Var -> SelectiveDFV
alt_fvs (Alt AltCon
_ [Var]
bndrs CoreExpr
rhs) = [Var] -> SelectiveDFV -> SelectiveDFV
addCoreBndrsFV [Var]
bndrs (CoreExpr -> SelectiveDFV
exprFVs CoreExpr
rhs)
exprFVs (Let (NonRec Var
bndr CoreExpr
rhs) CoreExpr
body)
= (Var, CoreExpr) -> SelectiveDFV
rhs_fvs (Var
bndr, CoreExpr
rhs) SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend` Var -> SelectiveDFV -> SelectiveDFV
addCoreBndrFV Var
bndr (CoreExpr -> SelectiveDFV
exprFVs CoreExpr
body)
exprFVs (Let (Rec [(Var, CoreExpr)]
pairs) CoreExpr
body)
= [Var] -> SelectiveDFV -> SelectiveDFV
addCoreBndrsFV (((Var, CoreExpr) -> Var) -> [(Var, CoreExpr)] -> [Var]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> Var
forall a b. (a, b) -> a
fst [(Var, CoreExpr)]
pairs) (SelectiveDFV -> SelectiveDFV) -> SelectiveDFV -> SelectiveDFV
forall a b. (a -> b) -> a -> b
$
((Var, CoreExpr) -> SelectiveDFV)
-> [(Var, CoreExpr)] -> SelectiveDFV
forall (t :: * -> *) acc a env.
(Foldable t, Monoid acc) =>
(a -> FV env acc) -> t a -> FV env acc
mapUnionFV (Var, CoreExpr) -> SelectiveDFV
rhs_fvs [(Var, CoreExpr)]
pairs SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend` CoreExpr -> SelectiveDFV
exprFVs CoreExpr
body
rhs_fvs :: (Id, CoreExpr) -> SelectiveDFV
rhs_fvs :: (Var, CoreExpr) -> SelectiveDFV
rhs_fvs (Var
bndr, CoreExpr
rhs) = CoreExpr -> SelectiveDFV
exprFVs CoreExpr
rhs SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend`
Var -> SelectiveDFV
bndrRuleAndUnfoldingFVs Var
bndr
tickish_fvs :: CoreTickish -> SelectiveDFV
tickish_fvs :: CoreTickish -> SelectiveDFV
tickish_fvs (Breakpoint XBreakpoint 'TickishPassCore
_ BreakpointId
_ [XTickishId 'TickishPassCore]
ids) = (Var -> SelectiveDFV) -> [Var] -> SelectiveDFV
forall (t :: * -> *) acc a env.
(Foldable t, Monoid acc) =>
(a -> FV env acc) -> t a -> FV env acc
mapUnionFV Var -> SelectiveDFV
unitFV [Var]
[XTickishId 'TickishPassCore]
ids
tickish_fvs CoreTickish
_ = SelectiveDFV
forall a. Monoid a => a
mempty
bndrTypeTyCoFVs :: Var -> SelectiveDFV
bndrTypeTyCoFVs :: Var -> SelectiveDFV
bndrTypeTyCoFVs Var
var
= Type -> SelectiveDFV
shallowSelTypeFV (Var -> Type
varType Var
var) SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend` SelectiveDFV
mult_fvs
where
mult_fvs :: SelectiveDFV
mult_fvs = case Var -> Maybe Type
varMultMaybe Var
var of
Just Type
mult -> Type -> SelectiveDFV
shallowSelTypeFV Type
mult
Maybe Type
Nothing -> SelectiveDFV
forall a. Monoid a => a
mempty
dBndrTypeTyCoVars :: Var -> DTyCoVarSet
dBndrTypeTyCoVars :: Var -> DVarSet
dBndrTypeTyCoVars = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
isLocalVar (SelectiveDFV -> DVarSet)
-> (Var -> SelectiveDFV) -> Var -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> SelectiveDFV
bndrTypeTyCoFVs
bndrFreeVars :: Id -> VarSet
bndrFreeVars :: Var -> VarSet
bndrFreeVars Var
id = Bool -> VarSet -> VarSet
forall a. HasCallStack => Bool -> a -> a
assert (InterestingVarFun
isId Var
id) (VarSet -> VarSet) -> VarSet -> VarSet
forall a b. (a -> b) -> a -> b
$
DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet) -> DVarSet -> VarSet
forall a b. (a -> b) -> a -> b
$
Var -> DVarSet
dBndrFreeVars Var
id
dBndrFreeVars :: Id -> DVarSet
dBndrFreeVars :: Var -> DVarSet
dBndrFreeVars Var
id = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
isLocalVar (SelectiveDFV -> DVarSet) -> SelectiveDFV -> DVarSet
forall a b. (a -> b) -> a -> b
$ Var -> SelectiveDFV
bndrFVs Var
id
bndrFVs :: Id -> SelectiveDFV
bndrFVs :: Var -> SelectiveDFV
bndrFVs Var
id = Bool -> SelectiveDFV -> SelectiveDFV
forall a. HasCallStack => Bool -> a -> a
assert (InterestingVarFun
isId Var
id) (SelectiveDFV -> SelectiveDFV) -> SelectiveDFV -> SelectiveDFV
forall a b. (a -> b) -> a -> b
$
Var -> SelectiveDFV
bndrTypeTyCoFVs Var
id SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend`
Var -> SelectiveDFV
bndrRuleAndUnfoldingFVs Var
id
bndrRuleAndUnfoldingVarsDSet :: Id -> DVarSet
bndrRuleAndUnfoldingVarsDSet :: Var -> DVarSet
bndrRuleAndUnfoldingVarsDSet = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
isLocalVar (SelectiveDFV -> DVarSet)
-> (Var -> SelectiveDFV) -> Var -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> SelectiveDFV
bndrRuleAndUnfoldingFVs
bndrRuleAndUnfoldingVars :: Id -> VarSet
bndrRuleAndUnfoldingVars :: Var -> VarSet
bndrRuleAndUnfoldingVars = DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet) -> (Var -> DVarSet) -> Var -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> DVarSet
bndrRuleAndUnfoldingVarsDSet
bndrRuleAndUnfoldingFVs :: Id -> SelectiveDFV
bndrRuleAndUnfoldingFVs :: Var -> SelectiveDFV
bndrRuleAndUnfoldingFVs Var
id
| InterestingVarFun
isId Var
id = Var -> SelectiveDFV
idRuleFVs Var
id SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
`mappend` Var -> SelectiveDFV
idUnfoldingFVs Var
id
| Bool
otherwise = SelectiveDFV
forall a. Monoid a => a
mempty
idRuleVars :: Id -> VarSet
idRuleVars :: Var -> VarSet
idRuleVars = DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet) -> (Var -> DVarSet) -> Var -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RuleInfo -> DVarSet
ruleInfoFreeVars (RuleInfo -> DVarSet) -> (Var -> RuleInfo) -> Var -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> RuleInfo
idSpecialisation
idRuleFVs :: Id -> SelectiveDFV
idRuleFVs :: Var -> SelectiveDFV
idRuleFVs Var
id = Bool -> SelectiveDFV -> SelectiveDFV
forall a. HasCallStack => Bool -> a -> a
assert (InterestingVarFun
isId Var
id) (SelectiveDFV -> SelectiveDFV) -> SelectiveDFV -> SelectiveDFV
forall a b. (a -> b) -> a -> b
$
(Var -> SelectiveDFV -> SelectiveDFV)
-> SelectiveDFV -> DVarSet -> SelectiveDFV
forall a r. (a -> r -> r) -> r -> UniqDSet a -> r
strictFoldDVarSet (SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Monoid a => a -> a -> a
mappend (SelectiveDFV -> SelectiveDFV -> SelectiveDFV)
-> (Var -> SelectiveDFV) -> Var -> SelectiveDFV -> SelectiveDFV
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> SelectiveDFV
unitFV) SelectiveDFV
forall a. Monoid a => a
mempty (DVarSet -> SelectiveDFV) -> DVarSet -> SelectiveDFV
forall a b. (a -> b) -> a -> b
$
RuleInfo -> DVarSet
ruleInfoFreeVars (Var -> RuleInfo
idSpecialisation Var
id)
idUnfoldingVars :: Id -> VarSet
idUnfoldingVars :: Var -> VarSet
idUnfoldingVars = InterestingVarFun -> SelectiveDFV -> VarSet
runFVSelectiveSet InterestingVarFun
isLocalVar (SelectiveDFV -> VarSet) -> (Var -> SelectiveDFV) -> Var -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Var -> SelectiveDFV
idUnfoldingFVs
idUnfoldingFVs :: Id -> SelectiveDFV
idUnfoldingFVs :: Var -> SelectiveDFV
idUnfoldingFVs Var
id = Unfolding -> Maybe SelectiveDFV
stableUnfoldingFVs (Var -> Unfolding
realIdUnfolding Var
id) Maybe SelectiveDFV -> SelectiveDFV -> SelectiveDFV
forall a. Maybe a -> a -> a
`orElse` SelectiveDFV
forall a. Monoid a => a
mempty
stableUnfoldingVars :: Unfolding -> Maybe VarSet
stableUnfoldingVars :: Unfolding -> Maybe VarSet
stableUnfoldingVars Unfolding
unf = (SelectiveDFV -> VarSet) -> Maybe SelectiveDFV -> Maybe VarSet
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (InterestingVarFun -> SelectiveDFV -> VarSet
runFVSelectiveSet InterestingVarFun
isLocalVar) (Maybe SelectiveDFV -> Maybe VarSet)
-> Maybe SelectiveDFV -> Maybe VarSet
forall a b. (a -> b) -> a -> b
$
Unfolding -> Maybe SelectiveDFV
stableUnfoldingFVs Unfolding
unf
stableUnfoldingFVs :: Unfolding -> Maybe SelectiveDFV
stableUnfoldingFVs :: Unfolding -> Maybe SelectiveDFV
stableUnfoldingFVs Unfolding
unf
= case Unfolding
unf of
CoreUnfolding { uf_tmpl :: Unfolding -> CoreExpr
uf_tmpl = CoreExpr
rhs, uf_src :: Unfolding -> UnfoldingSource
uf_src = UnfoldingSource
src }
| UnfoldingSource -> Bool
isStableSource UnfoldingSource
src
-> SelectiveDFV -> Maybe SelectiveDFV
forall a. a -> Maybe a
Just (CoreExpr -> SelectiveDFV
exprFVs CoreExpr
rhs)
DFunUnfolding { df_bndrs :: Unfolding -> [Var]
df_bndrs = [Var]
bndrs, df_args :: Unfolding -> [CoreExpr]
df_args = [CoreExpr]
args }
-> SelectiveDFV -> Maybe SelectiveDFV
forall a. a -> Maybe a
Just ([Var] -> SelectiveDFV -> SelectiveDFV
addCoreBndrsFV [Var]
bndrs ([CoreExpr] -> SelectiveDFV
exprsFVs [CoreExpr]
args))
Unfolding
_other -> Maybe SelectiveDFV
forall a. Maybe a
Nothing
orphNamesOfTyCon :: TyCon -> NameSet
orphNamesOfTyCon :: TyCon -> NameSet
orphNamesOfTyCon TyCon
tycon = Name -> NameSet
unitNameSet (TyCon -> Name
forall a. NamedThing a => a -> Name
getName TyCon
tycon) NameSet -> NameSet -> NameSet
`unionNameSet` case TyCon -> Maybe Class
tyConClass_maybe TyCon
tycon of
Maybe Class
Nothing -> NameSet
emptyNameSet
Just Class
cls -> Name -> NameSet
unitNameSet (Class -> Name
forall a. NamedThing a => a -> Name
getName Class
cls)
orphNamesOfType :: Type -> NameSet
orphNamesOfType :: Type -> NameSet
orphNamesOfType Type
ty | Just Type
ty' <- Type -> Maybe Type
coreView Type
ty = Type -> NameSet
orphNamesOfType Type
ty'
orphNamesOfType (TyVarTy Var
_) = NameSet
emptyNameSet
orphNamesOfType (LitTy {}) = NameSet
emptyNameSet
orphNamesOfType (ForAllTy ForAllTyBinder
bndr Type
res) = Type -> NameSet
orphNamesOfType (ForAllTyBinder -> Type
forall argf. VarBndr Var argf -> Type
binderType ForAllTyBinder
bndr)
NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
res
orphNamesOfType (AppTy Type
fun Type
arg) = Type -> NameSet
orphNamesOfType Type
fun NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
arg
orphNamesOfType (TyConApp TyCon
tycon [Type]
tys) = NameSet
func
NameSet -> NameSet -> NameSet
`unionNameSet` TyCon -> NameSet
orphNamesOfTyCon TyCon
tycon
NameSet -> NameSet -> NameSet
`unionNameSet` [Type] -> NameSet
orphNamesOfTypes [Type]
tys
where func :: NameSet
func = case [Type]
tys of
Type
arg:[Type]
_ | TyCon
tycon TyCon -> TyCon -> Bool
forall a. Eq a => a -> a -> Bool
== TyCon
fUNTyCon -> Type -> NameSet
orph_names_of_fun_ty_con Type
arg
[Type]
_ -> NameSet
emptyNameSet
orphNamesOfType (FunTy FunTyFlag
af Type
w Type
arg Type
res) = NameSet
func
NameSet -> NameSet -> NameSet
`unionNameSet` Name -> NameSet
unitNameSet Name
fun_tc
NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
w
NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
arg
NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
res
where func :: NameSet
func | FunTyFlag -> Bool
isVisibleFunArg FunTyFlag
af = Type -> NameSet
orph_names_of_fun_ty_con Type
w
| Bool
otherwise = NameSet
emptyNameSet
fun_tc :: Name
fun_tc = TyCon -> Name
tyConName (FunTyFlag -> TyCon
funTyFlagTyCon FunTyFlag
af)
orphNamesOfType (CastTy Type
ty Coercion
_co) = Type -> NameSet
orphNamesOfType Type
ty
orphNamesOfType (CoercionTy Coercion
_co) = NameSet
emptyNameSet
orphNamesOfThings :: (a -> NameSet) -> [a] -> NameSet
orphNamesOfThings :: forall a. (a -> NameSet) -> [a] -> NameSet
orphNamesOfThings a -> NameSet
f = (a -> NameSet -> NameSet) -> NameSet -> [a] -> NameSet
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NameSet -> NameSet -> NameSet
unionNameSet (NameSet -> NameSet -> NameSet)
-> (a -> NameSet) -> a -> NameSet -> NameSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> NameSet
f) NameSet
emptyNameSet
orphNamesOfTypes :: [Type] -> NameSet
orphNamesOfTypes :: [Type] -> NameSet
orphNamesOfTypes = (Type -> NameSet) -> [Type] -> NameSet
forall a. (a -> NameSet) -> [a] -> NameSet
orphNamesOfThings Type -> NameSet
orphNamesOfType
orphNamesOfAxiomLHS :: CoAxiom br -> NameSet
orphNamesOfAxiomLHS :: forall (br :: BranchFlag). CoAxiom br -> NameSet
orphNamesOfAxiomLHS CoAxiom br
axiom
= ([Type] -> NameSet
orphNamesOfTypes ([Type] -> NameSet) -> [Type] -> NameSet
forall a b. (a -> b) -> a -> b
$ (CoAxBranch -> [Type]) -> [CoAxBranch] -> [Type]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CoAxBranch -> [Type]
coAxBranchLHS ([CoAxBranch] -> [Type]) -> [CoAxBranch] -> [Type]
forall a b. (a -> b) -> a -> b
$ Branches br -> [CoAxBranch]
forall (br :: BranchFlag). Branches br -> [CoAxBranch]
fromBranches (Branches br -> [CoAxBranch]) -> Branches br -> [CoAxBranch]
forall a b. (a -> b) -> a -> b
$ CoAxiom br -> Branches br
forall (br :: BranchFlag). CoAxiom br -> Branches br
coAxiomBranches CoAxiom br
axiom)
NameSet -> Name -> NameSet
`extendNameSet` TyCon -> Name
forall a. NamedThing a => a -> Name
getName (CoAxiom br -> TyCon
forall (br :: BranchFlag). CoAxiom br -> TyCon
coAxiomTyCon CoAxiom br
axiom)
orph_names_of_fun_ty_con :: Mult -> NameSet
orph_names_of_fun_ty_con :: Type -> NameSet
orph_names_of_fun_ty_con Type
ManyTy = Name -> NameSet
unitNameSet Name
unrestrictedFunTyConName
orph_names_of_fun_ty_con Type
_ = NameSet
emptyNameSet
orphNamesOfExpr :: CoreExpr -> NameSet
orphNamesOfExpr :: CoreExpr -> NameSet
orphNamesOfExpr CoreExpr
e
= CoreExpr -> NameSet
go CoreExpr
e
where
go :: CoreExpr -> NameSet
go (Var Var
v)
| Name -> Bool
isExternalName Name
n = Name -> NameSet
unitNameSet Name
n
| Bool
otherwise = NameSet
emptyNameSet
where n :: Name
n = Var -> Name
idName Var
v
go (Lit Literal
_) = NameSet
emptyNameSet
go (Type Type
ty) = Type -> NameSet
orphNamesOfType Type
ty
go (Coercion Coercion
_co) = NameSet
emptyNameSet
go (App CoreExpr
e1 CoreExpr
e2) = CoreExpr -> NameSet
go CoreExpr
e1 NameSet -> NameSet -> NameSet
`unionNameSet` CoreExpr -> NameSet
go CoreExpr
e2
go (Lam Var
v CoreExpr
e) = CoreExpr -> NameSet
go CoreExpr
e NameSet -> Name -> NameSet
`delFromNameSet` Var -> Name
idName Var
v
go (Tick CoreTickish
_ CoreExpr
e) = CoreExpr -> NameSet
go CoreExpr
e
go (Cast CoreExpr
e Coercion
_co) = CoreExpr -> NameSet
go CoreExpr
e
go (Let (NonRec Var
_ CoreExpr
r) CoreExpr
e) = CoreExpr -> NameSet
go CoreExpr
e NameSet -> NameSet -> NameSet
`unionNameSet` CoreExpr -> NameSet
go CoreExpr
r
go (Let (Rec [(Var, CoreExpr)]
prs) CoreExpr
e) = [CoreExpr] -> NameSet
orphNamesOfExprs (((Var, CoreExpr) -> CoreExpr) -> [(Var, CoreExpr)] -> [CoreExpr]
forall a b. (a -> b) -> [a] -> [b]
map (Var, CoreExpr) -> CoreExpr
forall a b. (a, b) -> b
snd [(Var, CoreExpr)]
prs) NameSet -> NameSet -> NameSet
`unionNameSet` CoreExpr -> NameSet
go CoreExpr
e
go (Case CoreExpr
e Var
_ Type
ty [Alt Var]
as) = CoreExpr -> NameSet
go CoreExpr
e NameSet -> NameSet -> NameSet
`unionNameSet` Type -> NameSet
orphNamesOfType Type
ty
NameSet -> NameSet -> NameSet
`unionNameSet` [NameSet] -> NameSet
unionNameSets ((Alt Var -> NameSet) -> [Alt Var] -> [NameSet]
forall a b. (a -> b) -> [a] -> [b]
map Alt Var -> NameSet
go_alt [Alt Var]
as)
go_alt :: Alt Var -> NameSet
go_alt (Alt AltCon
_ [Var]
_ CoreExpr
r) = CoreExpr -> NameSet
go CoreExpr
r
orphNamesOfExprs :: [CoreExpr] -> NameSet
orphNamesOfExprs :: [CoreExpr] -> NameSet
orphNamesOfExprs [CoreExpr]
es = (CoreExpr -> NameSet -> NameSet)
-> NameSet -> [CoreExpr] -> NameSet
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (NameSet -> NameSet -> NameSet
unionNameSet (NameSet -> NameSet -> NameSet)
-> (CoreExpr -> NameSet) -> CoreExpr -> NameSet -> NameSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExpr -> NameSet
orphNamesOfExpr) NameSet
emptyNameSet [CoreExpr]
es
data RuleFVsFrom
= LhsOnly
| RhsOnly
| BothSides
ruleFVs :: RuleFVsFrom -> CoreRule -> SelectiveDFV
ruleFVs :: RuleFVsFrom -> CoreRule -> SelectiveDFV
ruleFVs !RuleFVsFrom
_ (BuiltinRule {}) = SelectiveDFV
forall a. Monoid a => a
mempty
ruleFVs RuleFVsFrom
from (Rule { ru_fn :: CoreRule -> Name
ru_fn = Name
_do_not_include
, ru_bndrs :: CoreRule -> [Var]
ru_bndrs = [Var]
bndrs
, ru_rhs :: CoreRule -> CoreExpr
ru_rhs = CoreExpr
rhs, ru_args :: CoreRule -> [CoreExpr]
ru_args = [CoreExpr]
args })
= [Var] -> SelectiveDFV -> SelectiveDFV
addCoreBndrsFV [Var]
bndrs ([CoreExpr] -> SelectiveDFV
exprsFVs [CoreExpr]
exprs)
where
exprs :: [CoreExpr]
exprs = case RuleFVsFrom
from of
RuleFVsFrom
LhsOnly -> [CoreExpr]
args
RuleFVsFrom
RhsOnly -> [CoreExpr
rhs]
RuleFVsFrom
BothSides -> CoreExpr
rhsCoreExpr -> [CoreExpr] -> [CoreExpr]
forall a. a -> [a] -> [a]
:[CoreExpr]
args
rulesFVs :: RuleFVsFrom -> [CoreRule] -> SelectiveDFV
rulesFVs :: RuleFVsFrom -> [CoreRule] -> SelectiveDFV
rulesFVs RuleFVsFrom
from = (CoreRule -> SelectiveDFV) -> [CoreRule] -> SelectiveDFV
forall (t :: * -> *) acc a env.
(Foldable t, Monoid acc) =>
(a -> FV env acc) -> t a -> FV env acc
mapUnionFV (RuleFVsFrom -> CoreRule -> SelectiveDFV
ruleFVs RuleFVsFrom
from)
ruleRhsFreeVars :: CoreRule -> VarSet
ruleRhsFreeVars :: CoreRule -> VarSet
ruleRhsFreeVars = InterestingVarFun -> SelectiveDFV -> VarSet
runFVSelectiveSet InterestingVarFun
isLocalId (SelectiveDFV -> VarSet)
-> (CoreRule -> SelectiveDFV) -> CoreRule -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RuleFVsFrom -> CoreRule -> SelectiveDFV
ruleFVs RuleFVsFrom
RhsOnly
rulesRhsFreeIds :: [CoreRule] -> VarSet
rulesRhsFreeIds :: [CoreRule] -> VarSet
rulesRhsFreeIds = InterestingVarFun -> SelectiveDFV -> VarSet
runFVSelectiveSet InterestingVarFun
isLocalId (SelectiveDFV -> VarSet)
-> ([CoreRule] -> SelectiveDFV) -> [CoreRule] -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RuleFVsFrom -> [CoreRule] -> SelectiveDFV
rulesFVs RuleFVsFrom
RhsOnly
ruleLhsFreeIds :: CoreRule -> VarSet
ruleLhsFreeIds :: CoreRule -> VarSet
ruleLhsFreeIds = InterestingVarFun -> SelectiveDFV -> VarSet
runFVSelectiveSet InterestingVarFun
isLocalId (SelectiveDFV -> VarSet)
-> (CoreRule -> SelectiveDFV) -> CoreRule -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RuleFVsFrom -> CoreRule -> SelectiveDFV
ruleFVs RuleFVsFrom
LhsOnly
ruleLhsFreeIdsList :: CoreRule -> [Var]
ruleLhsFreeIdsList :: CoreRule -> [Var]
ruleLhsFreeIdsList = InterestingVarFun -> SelectiveDFV -> [Var]
runFVSelectiveList InterestingVarFun
isLocalId (SelectiveDFV -> [Var])
-> (CoreRule -> SelectiveDFV) -> CoreRule -> [Var]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RuleFVsFrom -> CoreRule -> SelectiveDFV
ruleFVs RuleFVsFrom
LhsOnly
ruleFreeVars :: CoreRule -> VarSet
ruleFreeVars :: CoreRule -> VarSet
ruleFreeVars = InterestingVarFun -> SelectiveDFV -> VarSet
runFVSelectiveSet InterestingVarFun
isLocalVar (SelectiveDFV -> VarSet)
-> (CoreRule -> SelectiveDFV) -> CoreRule -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RuleFVsFrom -> CoreRule -> SelectiveDFV
ruleFVs RuleFVsFrom
BothSides
rulesFreeVarsDSet :: [CoreRule] -> DVarSet
rulesFreeVarsDSet :: [CoreRule] -> DVarSet
rulesFreeVarsDSet = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
isLocalVar (SelectiveDFV -> DVarSet)
-> ([CoreRule] -> SelectiveDFV) -> [CoreRule] -> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RuleFVsFrom -> [CoreRule] -> SelectiveDFV
rulesFVs RuleFVsFrom
BothSides
rulesFreeVars :: [CoreRule] -> VarSet
rulesFreeVars :: [CoreRule] -> VarSet
rulesFreeVars = DVarSet -> VarSet
dVarSetToVarSet (DVarSet -> VarSet)
-> ([CoreRule] -> DVarSet) -> [CoreRule] -> VarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CoreRule] -> DVarSet
rulesFreeVarsDSet
mkRuleInfo :: [CoreRule] -> RuleInfo
mkRuleInfo :: [CoreRule] -> RuleInfo
mkRuleInfo [CoreRule]
rules = [CoreRule] -> DVarSet -> RuleInfo
RuleInfo [CoreRule]
rules ([CoreRule] -> DVarSet
rulesFreeVarsDSet [CoreRule]
rules)
type FVAnn = DVarSet
type CoreBindWithFVs = AnnBind Id FVAnn
type CoreExprWithFVs = AnnExpr Id FVAnn
type CoreExprWithFVs' = AnnExpr' Id FVAnn
type CoreAltWithFVs = AnnAlt Id FVAnn
freeVarsOf :: CoreExprWithFVs -> DIdSet
freeVarsOf :: CoreExprWithFVs -> DVarSet
freeVarsOf (DVarSet
fvs, AnnExpr' Var DVarSet
_) = DVarSet
fvs
freeVarsOfAnn :: FVAnn -> DIdSet
freeVarsOfAnn :: DVarSet -> DVarSet
freeVarsOfAnn DVarSet
fvs = DVarSet
fvs
aFreeVar :: Var -> DVarSet
aFreeVar :: Var -> DVarSet
aFreeVar = Var -> DVarSet
unitDVarSet
delBindersFV :: [Var] -> DVarSet -> DVarSet
delBindersFV :: [Var] -> DVarSet -> DVarSet
delBindersFV [Var]
bs DVarSet
fvs = (Var -> DVarSet -> DVarSet) -> DVarSet -> [Var] -> DVarSet
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Var -> DVarSet -> DVarSet
delBinderFV DVarSet
fvs [Var]
bs
delBinderFV :: Var -> DVarSet -> DVarSet
delBinderFV :: Var -> DVarSet -> DVarSet
delBinderFV Var
b DVarSet
s = (DVarSet
s DVarSet -> Var -> DVarSet
`delDVarSet` Var
b) DVarSet -> DVarSet -> DVarSet
`unionDVarSet` Var -> DVarSet
dBndrTypeTyCoVars Var
b
freeVarsBind :: CoreBind
-> DVarSet
-> (CoreBindWithFVs, DVarSet)
freeVarsBind :: CoreBind -> DVarSet -> (CoreBindWithFVs, DVarSet)
freeVarsBind (NonRec Var
binder CoreExpr
rhs) DVarSet
body_fvs
= ( Var -> CoreExprWithFVs -> CoreBindWithFVs
forall bndr annot. bndr -> AnnExpr bndr annot -> AnnBind bndr annot
AnnNonRec Var
binder CoreExprWithFVs
rhs2
, CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
rhs2 DVarSet -> DVarSet -> DVarSet
`unionDVarSet` DVarSet
body_fvs2
DVarSet -> DVarSet -> DVarSet
`unionDVarSet` Var -> DVarSet
bndrRuleAndUnfoldingVarsDSet Var
binder )
where
rhs2 :: CoreExprWithFVs
rhs2 = CoreExpr -> CoreExprWithFVs
freeVars CoreExpr
rhs
body_fvs2 :: DVarSet
body_fvs2 = Var
binder Var -> DVarSet -> DVarSet
`delBinderFV` DVarSet
body_fvs
freeVarsBind (Rec [(Var, CoreExpr)]
binds) DVarSet
body_fvs
= ( [(Var, CoreExprWithFVs)] -> CoreBindWithFVs
forall bndr annot.
[(bndr, AnnExpr bndr annot)] -> AnnBind bndr annot
AnnRec ([Var]
binders [Var] -> [CoreExprWithFVs] -> [(Var, CoreExprWithFVs)]
forall a b. [a] -> [b] -> [(a, b)]
`zip` [CoreExprWithFVs]
rhss2)
, [Var] -> DVarSet -> DVarSet
delBindersFV [Var]
binders DVarSet
all_fvs )
where
([Var]
binders, [CoreExpr]
rhss) = [(Var, CoreExpr)] -> ([Var], [CoreExpr])
forall (f :: * -> *) a b. Functor f => f (a, b) -> (f a, f b)
unzip [(Var, CoreExpr)]
binds
rhss2 :: [CoreExprWithFVs]
rhss2 = (CoreExpr -> CoreExprWithFVs) -> [CoreExpr] -> [CoreExprWithFVs]
forall a b. (a -> b) -> [a] -> [b]
map CoreExpr -> CoreExprWithFVs
freeVars [CoreExpr]
rhss
rhs_body_fvs :: DVarSet
rhs_body_fvs = (CoreExprWithFVs -> DVarSet -> DVarSet)
-> DVarSet -> [CoreExprWithFVs] -> DVarSet
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (DVarSet -> DVarSet -> DVarSet
unionDVarSet (DVarSet -> DVarSet -> DVarSet)
-> (CoreExprWithFVs -> DVarSet)
-> CoreExprWithFVs
-> DVarSet
-> DVarSet
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CoreExprWithFVs -> DVarSet
freeVarsOf) DVarSet
body_fvs [CoreExprWithFVs]
rhss2
binders_fvs :: DVarSet
binders_fvs = InterestingVarFun -> SelectiveDFV -> DVarSet
runFVSelective InterestingVarFun
isLocalVar (SelectiveDFV -> DVarSet) -> SelectiveDFV -> DVarSet
forall a b. (a -> b) -> a -> b
$ (Var -> SelectiveDFV) -> [Var] -> SelectiveDFV
forall (t :: * -> *) acc a env.
(Foldable t, Monoid acc) =>
(a -> FV env acc) -> t a -> FV env acc
mapUnionFV Var -> SelectiveDFV
bndrRuleAndUnfoldingFVs [Var]
binders
all_fvs :: DVarSet
all_fvs = DVarSet
rhs_body_fvs DVarSet -> DVarSet -> DVarSet
`unionDVarSet` DVarSet
binders_fvs
freeVars :: CoreExpr -> CoreExprWithFVs
freeVars :: CoreExpr -> CoreExprWithFVs
freeVars = CoreExpr -> CoreExprWithFVs
go
where
go :: CoreExpr -> CoreExprWithFVs
go :: CoreExpr -> CoreExprWithFVs
go (Var Var
v)
| InterestingVarFun
isLocalVar Var
v = (Var -> DVarSet
aFreeVar Var
v DVarSet -> DVarSet -> DVarSet
`unionDVarSet` DVarSet
ty_fvs DVarSet -> DVarSet -> DVarSet
`unionDVarSet` DVarSet
mult_vars, Var -> AnnExpr' Var DVarSet
forall bndr annot. Var -> AnnExpr' bndr annot
AnnVar Var
v)
| Bool
otherwise = (DVarSet
emptyDVarSet, Var -> AnnExpr' Var DVarSet
forall bndr annot. Var -> AnnExpr' bndr annot
AnnVar Var
v)
where
mult_vars :: DVarSet
mult_vars = Type -> DVarSet
tyCoVarsOfTypeDSet (HasDebugCallStack => Var -> Type
Var -> Type
idMult Var
v)
ty_fvs :: DVarSet
ty_fvs = Var -> DVarSet
dBndrTypeTyCoVars Var
v
go (Lit Literal
lit) = (DVarSet
emptyDVarSet, Literal -> AnnExpr' Var DVarSet
forall bndr annot. Literal -> AnnExpr' bndr annot
AnnLit Literal
lit)
go (Lam Var
b CoreExpr
body)
= ( DVarSet
b_fvs DVarSet -> DVarSet -> DVarSet
`unionDVarSet` (Var
b Var -> DVarSet -> DVarSet
`delBinderFV` DVarSet
body_fvs)
, Var -> CoreExprWithFVs -> AnnExpr' Var DVarSet
forall bndr annot.
bndr -> AnnExpr bndr annot -> AnnExpr' bndr annot
AnnLam Var
b CoreExprWithFVs
body' )
where
body' :: CoreExprWithFVs
body'@(DVarSet
body_fvs, AnnExpr' Var DVarSet
_) = CoreExpr -> CoreExprWithFVs
go CoreExpr
body
b_ty :: Type
b_ty = Var -> Type
idType Var
b
b_fvs :: DVarSet
b_fvs = Type -> DVarSet
tyCoVarsOfTypeDSet Type
b_ty
go (App CoreExpr
fun CoreExpr
arg)
= ( CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
fun' DVarSet -> DVarSet -> DVarSet
`unionDVarSet` CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
arg'
, CoreExprWithFVs -> CoreExprWithFVs -> AnnExpr' Var DVarSet
forall bndr annot.
AnnExpr bndr annot -> AnnExpr bndr annot -> AnnExpr' bndr annot
AnnApp CoreExprWithFVs
fun' CoreExprWithFVs
arg' )
where
fun' :: CoreExprWithFVs
fun' = CoreExpr -> CoreExprWithFVs
go CoreExpr
fun
arg' :: CoreExprWithFVs
arg' = CoreExpr -> CoreExprWithFVs
go CoreExpr
arg
go (Case CoreExpr
scrut Var
bndr Type
ty [Alt Var]
alts)
= ( (Var
bndr Var -> DVarSet -> DVarSet
`delBinderFV` DVarSet
alts_fvs)
DVarSet -> DVarSet -> DVarSet
`unionDVarSet` CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
scrut2
DVarSet -> DVarSet -> DVarSet
`unionDVarSet` Type -> DVarSet
tyCoVarsOfTypeDSet Type
ty
, CoreExprWithFVs
-> Var -> Type -> [AnnAlt Var DVarSet] -> AnnExpr' Var DVarSet
forall bndr annot.
AnnExpr bndr annot
-> bndr -> Type -> [AnnAlt bndr annot] -> AnnExpr' bndr annot
AnnCase CoreExprWithFVs
scrut2 Var
bndr Type
ty [AnnAlt Var DVarSet]
alts2 )
where
scrut2 :: CoreExprWithFVs
scrut2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
scrut
([DVarSet]
alts_fvs_s, [AnnAlt Var DVarSet]
alts2) = (Alt Var -> (DVarSet, AnnAlt Var DVarSet))
-> [Alt Var] -> ([DVarSet], [AnnAlt Var DVarSet])
forall a b c. (a -> (b, c)) -> [a] -> ([b], [c])
mapAndUnzip Alt Var -> (DVarSet, AnnAlt Var DVarSet)
fv_alt [Alt Var]
alts
alts_fvs :: DVarSet
alts_fvs = [DVarSet] -> DVarSet
unionDVarSets [DVarSet]
alts_fvs_s
fv_alt :: Alt Var -> (DVarSet, AnnAlt Var DVarSet)
fv_alt (Alt AltCon
con [Var]
args CoreExpr
rhs) = ([Var] -> DVarSet -> DVarSet
delBindersFV [Var]
args (CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
rhs2),
(AltCon -> [Var] -> CoreExprWithFVs -> AnnAlt Var DVarSet
forall bndr annot.
AltCon -> [bndr] -> AnnExpr bndr annot -> AnnAlt bndr annot
AnnAlt AltCon
con [Var]
args CoreExprWithFVs
rhs2))
where
rhs2 :: CoreExprWithFVs
rhs2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
rhs
go (Let CoreBind
bind CoreExpr
body)
= (DVarSet
bind_fvs, CoreBindWithFVs -> CoreExprWithFVs -> AnnExpr' Var DVarSet
forall bndr annot.
AnnBind bndr annot -> AnnExpr bndr annot -> AnnExpr' bndr annot
AnnLet CoreBindWithFVs
bind2 CoreExprWithFVs
body2)
where
(CoreBindWithFVs
bind2, DVarSet
bind_fvs) = CoreBind -> DVarSet -> (CoreBindWithFVs, DVarSet)
freeVarsBind CoreBind
bind (CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
body2)
body2 :: CoreExprWithFVs
body2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
body
go (Cast CoreExpr
expr Coercion
co)
= ( CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
expr2 DVarSet -> DVarSet -> DVarSet
`unionDVarSet` DVarSet
cfvs
, CoreExprWithFVs -> (DVarSet, Coercion) -> AnnExpr' Var DVarSet
forall bndr annot.
AnnExpr bndr annot -> (annot, Coercion) -> AnnExpr' bndr annot
AnnCast CoreExprWithFVs
expr2 (DVarSet
cfvs, Coercion
co) )
where
expr2 :: CoreExprWithFVs
expr2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
expr
cfvs :: DVarSet
cfvs = Coercion -> DVarSet
tyCoVarsOfCoDSet Coercion
co
go (Tick CoreTickish
tickish CoreExpr
expr)
= ( CoreTickish -> DVarSet
forall {pass :: TickishPass}.
(XTickishId pass ~ Var) =>
GenTickish pass -> DVarSet
tickishFVs CoreTickish
tickish DVarSet -> DVarSet -> DVarSet
`unionDVarSet` CoreExprWithFVs -> DVarSet
freeVarsOf CoreExprWithFVs
expr2
, CoreTickish -> CoreExprWithFVs -> AnnExpr' Var DVarSet
forall bndr annot.
CoreTickish -> AnnExpr bndr annot -> AnnExpr' bndr annot
AnnTick CoreTickish
tickish CoreExprWithFVs
expr2 )
where
expr2 :: CoreExprWithFVs
expr2 = CoreExpr -> CoreExprWithFVs
go CoreExpr
expr
tickishFVs :: GenTickish pass -> DVarSet
tickishFVs (Breakpoint XBreakpoint pass
_ BreakpointId
_ [XTickishId pass]
ids) = [Var] -> DVarSet
mkDVarSet [Var]
[XTickishId pass]
ids
tickishFVs GenTickish pass
_ = DVarSet
emptyDVarSet
go (Type Type
ty) = (Type -> DVarSet
tyCoVarsOfTypeDSet Type
ty, Type -> AnnExpr' Var DVarSet
forall bndr annot. Type -> AnnExpr' bndr annot
AnnType Type
ty)
go (Coercion Coercion
co) = (Coercion -> DVarSet
tyCoVarsOfCoDSet Coercion
co, Coercion -> AnnExpr' Var DVarSet
forall bndr annot. Coercion -> AnnExpr' bndr annot
AnnCoercion Coercion
co)