{-#LANGUAGE CPP#-}
module Foreign.Storable.Generic.Plugin.Internal.Predicates
(
isGStorableInstId
, isSizeOfId
, isAlignmentId
, isPeekId
, isPokeId
, isSpecGStorableInstId
, isSpecSizeOfId
, isSpecAlignmentId
, isSpecPeekId
, isSpecPokeId
, isChoiceSizeOfId
, isChoiceAlignmentId
, isChoicePeekId
, isChoicePokeId
, isOffsetsId
, isGStorableId
, isGStorableMethodId
, isNonRecBind
, toIsBind
, withTypeCheck
)
where
import CoreSyn (Bind(..),Expr(..), CoreExpr, CoreBind, CoreProgram, Alt)
import Literal (Literal(..))
import Id (isLocalId, isGlobalId,Id)
import Var (Var(..))
import Name (getOccName,mkOccName)
import OccName (OccName(..), occNameString)
import qualified Name as N (varName, tcClsName)
import SrcLoc (noSrcSpan)
import Unique (getUnique)
import HscMain (hscCompileCoreExpr)
import HscTypes (HscEnv,ModGuts(..))
import CoreMonad (CoreM, CoreToDo(..), getHscEnv)
import BasicTypes (CompilerPhase(..))
import Type (isAlgType, splitTyConApp_maybe)
import TyCon (TyCon,tyConName, algTyConRhs, visibleDataCons)
import TyCoRep (Type(..), TyBinder(..))
import TysWiredIn (intDataCon)
import DataCon (dataConWorkId,dataConOrigArgTys)
import MkCore (mkWildValBinder)
import Outputable (cat, ppr, SDoc, showSDocUnsafe)
import CoreMonad (putMsg, putMsgS)
import Name (nameStableString)
import Data.Maybe
import Foreign.Storable.Generic.Plugin.Internal.Helpers
isGStorableInstId :: Id -> Bool
isGStorableInstId id = cutted_occ_name == gstorable_dict_name
&& cutted_occ_name2 /= gstorable'_dict_name
where cutted_occ_name = cutOccName 11 $ getOccName (varName id)
cutted_occ_name2 = cutOccName 12 $ getOccName (varName id)
gstorable_dict_name = mkOccName N.varName "$fGStorable"
gstorable'_dict_name = mkOccName N.varName "$fGStorable'"
isSizeOfId :: Id -> Bool
isSizeOfId ident = getOccName (varName ident) == mkOccName N.varName "$cgsizeOf"
isAlignmentId :: Id -> Bool
isAlignmentId ident = getOccName (varName ident) == mkOccName N.varName "$cgalignment"
isPeekId :: Id -> Bool
isPeekId id = occStr == compared1
where occStr = nameStableString $ varName id
compared1 = "$_in$$cgpeekByteOff"
isPokeId :: Id -> Bool
isPokeId id = occStr == compared1
where occStr = nameStableString $ varName id
compared1 = "$_in$$cgpokeByteOff"
isChoiceSizeOfId :: Id -> Bool
isChoiceSizeOfId id = occStr == compared1 || occStr == compared2
where occStr = nameStableString $ varName id
compared1 = "$_in$$s$fGStorableChoice'Falsea_$cchSizeOf"
compared2 = "$_in$$s$fGStorableChoice'Truea_$cchSizeOf"
isChoiceAlignmentId :: Id -> Bool
isChoiceAlignmentId id = occStr == compared1 || occStr == compared2
where occStr = nameStableString $ varName id
compared1 = "$_in$$s$fGStorableChoice'Falsea_$cchAlignment"
compared2 = "$_in$$s$fGStorableChoice'Truea_$cchAlignment"
isChoicePeekId :: Id -> Bool
isChoicePeekId id = compared1 == occStr || compared2 == occStr
where occStr = nameStableString $ varName id
compared1 = "$_in$$s$fGStorableChoice'Falsea_$cchPeekByteOff"
compared2 = "$_in$$s$fGStorableChoice'Truea_$cchPeekByteOff"
isChoicePokeId :: Id -> Bool
isChoicePokeId id = compared1 == occStr || compared2 == occStr
where occStr = nameStableString $ varName id
compared1 = "$_in$$s$fGStorableChoice'Falsea_$cchPokeByteOff"
compared2 = "$_in$$s$fGStorableChoice'Truea_$cchPokeByteOff"
isSpecGStorableInstId :: Id -> Bool
isSpecGStorableInstId id = cutted_occ_name == gstorable_dict_name
&& cutted_occ_name2 /= gstorable'_dict_name
where cutted_occ_name = cutOccName 11 $ getOccName (varName id)
cutted_occ_name2 = cutOccName 12 $ getOccName (varName id)
gstorable_dict_name = mkOccName N.varName "$s$fGStorable"
gstorable'_dict_name = mkOccName N.varName "$s$fGStorable'"
isSpecSizeOfId :: Id -> Bool
isSpecSizeOfId ident = getOccName (varName ident) == mkOccName N.varName "$s$cgsizeOf"
isSpecAlignmentId :: Id -> Bool
isSpecAlignmentId ident = getOccName (varName ident) == mkOccName N.varName "$s$cgalignment"
isSpecPeekId :: Id -> Bool
isSpecPeekId ident = getOccName (varName ident) == mkOccName N.varName "$s$cgpeekByteOff"
isSpecPokeId :: Id -> Bool
isSpecPokeId ident = getOccName (varName ident) == mkOccName N.varName "$s$cgpokeByteOff"
isOffsetsId :: Id -> Bool
isOffsetsId id = getOccName (varName id) == mkOccName N.varName "offsets"
isGStorableId :: Id -> Bool
isGStorableId id = any ($id) [ isSizeOfId, isAlignmentId, isPeekId
, isPokeId, isGStorableInstId
, isSpecSizeOfId, isSpecAlignmentId
, isSpecPeekId, isSpecPokeId
, isSpecGStorableInstId
]
isGStorableMethodId :: Id -> Bool
isGStorableMethodId id = any ($id) [isSizeOfId, isAlignmentId
, isPeekId, isPokeId
, isSpecSizeOfId, isSpecAlignmentId
, isSpecPeekId, isSpecPokeId
#if MIN_VERSION_GLASGOW_HASKELL(8,8,1,0)
, isChoiceSizeOfId, isChoiceAlignmentId
, isChoicePeekId, isChoicePokeId
#endif
]
isNonRecBind :: CoreBind -> Bool
isNonRecBind (NonRec _ _) = True
isNonRecBind _ = False
toIsBind :: (Id -> Bool) -> CoreBind -> Bool
toIsBind pred (NonRec id rhs) = pred id
toIsBind pred (Rec bs) = any pred $ map fst bs
withTypeCheck :: (Type -> Maybe Type) -> (Id -> Bool) -> Id -> Bool
withTypeCheck ty_f id_f id = do
let ty_checked = ty_f $ varType id
id_checked = id_f id
and [isJust ty_checked, id_checked]