{-# LANGUAGE DataKinds      #-}
{-# LANGUAGE GADTs          #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE TypeFamilies   #-}

-- | Modules we recognize types from
module Debug.RecoverRTTI.Modules (
    KnownPkg(..)
  , KnownModule(..)
  , IsKnownPkg(..)
    -- * Matching
  , inKnownModule
  , inKnownModuleNested
  ) where

import Control.Monad
import Data.List (isPrefixOf)

import Debug.RecoverRTTI.FlatClosure

{-------------------------------------------------------------------------------
  Packages
-------------------------------------------------------------------------------}

data KnownPkg =
    PkgGhcPrim
  | PkgBase
  | PkgByteString
  | PkgText
  | PkgIntegerWiredIn
  | PkgGhcBignum
  | PkgContainers
  | PkgAeson
  | PkgUnorderedContainers
  | PkgVector

data family KnownModule (pkg :: KnownPkg)

{-------------------------------------------------------------------------------
  Singleton instance for KnownPkg
-------------------------------------------------------------------------------}

data SPkg (pkg :: KnownPkg) where
  SGhcPrim             :: SPkg 'PkgGhcPrim
  SBase                :: SPkg 'PkgBase
  SByteString          :: SPkg 'PkgByteString
  SText                :: SPkg 'PkgText
  SIntegerWiredIn      :: SPkg 'PkgIntegerWiredIn
  SGhcBignum           :: SPkg 'PkgGhcBignum
  SContainers          :: SPkg 'PkgContainers
  SAeson               :: SPkg 'PkgAeson
  SUnorderedContainers :: SPkg 'PkgUnorderedContainers
  SVector              :: SPkg 'PkgVector

class IsKnownPkg pkg where
  singPkg :: SPkg pkg

instance IsKnownPkg 'PkgGhcPrim             where singPkg :: SPkg 'PkgGhcPrim
singPkg = SPkg 'PkgGhcPrim
SGhcPrim
instance IsKnownPkg 'PkgBase                where singPkg :: SPkg 'PkgBase
singPkg = SPkg 'PkgBase
SBase
instance IsKnownPkg 'PkgByteString          where singPkg :: SPkg 'PkgByteString
singPkg = SPkg 'PkgByteString
SByteString
instance IsKnownPkg 'PkgText                where singPkg :: SPkg 'PkgText
singPkg = SPkg 'PkgText
SText
instance IsKnownPkg 'PkgIntegerWiredIn      where singPkg :: SPkg 'PkgIntegerWiredIn
singPkg = SPkg 'PkgIntegerWiredIn
SIntegerWiredIn
instance IsKnownPkg 'PkgGhcBignum           where singPkg :: SPkg 'PkgGhcBignum
singPkg = SPkg 'PkgGhcBignum
SGhcBignum
instance IsKnownPkg 'PkgContainers          where singPkg :: SPkg 'PkgContainers
singPkg = SPkg 'PkgContainers
SContainers
instance IsKnownPkg 'PkgAeson               where singPkg :: SPkg 'PkgAeson
singPkg = SPkg 'PkgAeson
SAeson
instance IsKnownPkg 'PkgUnorderedContainers where singPkg :: SPkg 'PkgUnorderedContainers
singPkg = SPkg 'PkgUnorderedContainers
SUnorderedContainers
instance IsKnownPkg 'PkgVector              where singPkg :: SPkg 'PkgVector
singPkg = SPkg 'PkgVector
SVector

{-------------------------------------------------------------------------------
  Modules in @ghc-pri@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgGhcPrim =
    GhcTypes
  | GhcTuple

{-------------------------------------------------------------------------------
  Modules in @base@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgBase =
    GhcInt
  | GhcWord
  | GhcSTRef
  | GhcMVar
  | GhcConcSync
  | GhcMaybe
  | GhcReal
  | DataEither

{-------------------------------------------------------------------------------
  Modules in @bytestring@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgByteString =
    DataByteStringInternal
  | DataByteStringLazyInternal
  | DataByteStringShortInternal

{-------------------------------------------------------------------------------
  Modules in @text@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgText =
    DataTextInternal
  | DataTextInternalLazy

{-------------------------------------------------------------------------------
  Modules in @integer-wired-in@ (this is a virtual package)
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgIntegerWiredIn =
    GhcIntegerType

{-------------------------------------------------------------------------------
  Modules in @ghc-bignum@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgGhcBignum =
    GhcNumInteger

{-------------------------------------------------------------------------------
  Modules in @containers@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgContainers =
    DataSetInternal
  | DataMapInternal
  | DataIntSetInternal
  | DataIntMapInternal
  | DataSequenceInternal
  | DataTree

{-------------------------------------------------------------------------------
  Modules in @aeson@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgAeson =
    DataAesonTypesInternal

{-------------------------------------------------------------------------------
  Modules in @unordered-containers@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgUnorderedContainers =
    DataHashMapInternal
  | DataHashMapInternalArray

{-------------------------------------------------------------------------------
  Modules in @vector@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgVector =
   DataVector

{-------------------------------------------------------------------------------
  Matching
-------------------------------------------------------------------------------}

-- | Check if the given closure is from a known package/module
inKnownModule :: IsKnownPkg pkg
  => KnownModule pkg
  -> FlatClosure -> Maybe String
inKnownModule :: KnownModule pkg -> FlatClosure -> Maybe String
inKnownModule KnownModule pkg
modl = ((String, [Box]) -> String)
-> Maybe (String, [Box]) -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String, [Box]) -> String
forall a b. (a, b) -> a
fst (Maybe (String, [Box]) -> Maybe String)
-> (FlatClosure -> Maybe (String, [Box]))
-> FlatClosure
-> Maybe String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KnownModule pkg -> FlatClosure -> Maybe (String, [Box])
forall (pkg :: KnownPkg).
IsKnownPkg pkg =>
KnownModule pkg -> FlatClosure -> Maybe (String, [Box])
inKnownModuleNested KnownModule pkg
modl

-- | Generalization of 'inKnownModule' that additionally returns nested pointers
inKnownModuleNested :: IsKnownPkg pkg
  => KnownModule pkg
  -> FlatClosure -> Maybe (String, [Box])
inKnownModuleNested :: KnownModule pkg -> FlatClosure -> Maybe (String, [Box])
inKnownModuleNested = SPkg pkg -> KnownModule pkg -> FlatClosure -> Maybe (String, [Box])
forall (pkg :: KnownPkg).
SPkg pkg -> KnownModule pkg -> FlatClosure -> Maybe (String, [Box])
go SPkg pkg
forall (pkg :: KnownPkg). IsKnownPkg pkg => SPkg pkg
singPkg
  where
    go :: SPkg pkg -> KnownModule pkg -> FlatClosure -> Maybe (String, [Box])
    go :: SPkg pkg -> KnownModule pkg -> FlatClosure -> Maybe (String, [Box])
go SPkg pkg
knownPkg KnownModule pkg
knownModl ConstrClosure{String
pkg :: FlatClosure -> String
pkg :: String
pkg, String
modl :: FlatClosure -> String
modl :: String
modl, String
name :: FlatClosure -> String
name :: String
name, [Box]
ptrArgs :: FlatClosure -> [Box]
ptrArgs :: [Box]
ptrArgs} = do
        Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (SPkg pkg -> String
forall (pkg :: KnownPkg). SPkg pkg -> String
namePkg SPkg pkg
knownPkg String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` String
pkg) -- ignore the version number
        Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (String
modl String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== SPkg pkg -> KnownModule pkg -> String
forall (pkg :: KnownPkg). SPkg pkg -> KnownModule pkg -> String
nameModl SPkg pkg
knownPkg KnownModule pkg
knownModl)
        (String, [Box]) -> Maybe (String, [Box])
forall (m :: * -> *) a. Monad m => a -> m a
return (String
name, [Box]
ptrArgs)
    go SPkg pkg
_ KnownModule pkg
_ FlatClosure
_otherClosure = Maybe (String, [Box])
forall a. Maybe a
Nothing

    namePkg :: SPkg pkg -> String
    namePkg :: SPkg pkg -> String
namePkg SPkg pkg
SGhcPrim             = String
"ghc-prim"
    namePkg SPkg pkg
SBase                = String
"base"
    namePkg SPkg pkg
SByteString          = String
"bytestring"
    namePkg SPkg pkg
SText                = String
"text"
    namePkg SPkg pkg
SIntegerWiredIn      = String
"integer-wired-in"
    namePkg SPkg pkg
SGhcBignum           = String
"ghc-bignum"
    namePkg SPkg pkg
SContainers          = String
"containers"
    namePkg SPkg pkg
SAeson               = String
"aeson"
    namePkg SPkg pkg
SUnorderedContainers = String
"unordered-containers"
    namePkg SPkg pkg
SVector              = String
"vector"

    nameModl :: SPkg pkg -> KnownModule pkg -> String
    nameModl :: SPkg pkg -> KnownModule pkg -> String
nameModl SPkg pkg
SGhcPrim             KnownModule pkg
GhcTypes                    = String
"GHC.Types"
    nameModl SPkg pkg
SGhcPrim             KnownModule pkg
GhcTuple                    = String
"GHC.Tuple"
    nameModl SPkg pkg
SBase                KnownModule pkg
GhcInt                      = String
"GHC.Int"
    nameModl SPkg pkg
SBase                KnownModule pkg
GhcWord                     = String
"GHC.Word"
    nameModl SPkg pkg
SBase                KnownModule pkg
GhcSTRef                    = String
"GHC.STRef"
    nameModl SPkg pkg
SBase                KnownModule pkg
GhcMVar                     = String
"GHC.MVar"
    nameModl SPkg pkg
SBase                KnownModule pkg
GhcConcSync                 = String
"GHC.Conc.Sync"
    nameModl SPkg pkg
SBase                KnownModule pkg
GhcMaybe                    = String
"GHC.Maybe"
    nameModl SPkg pkg
SBase                KnownModule pkg
GhcReal                     = String
"GHC.Real"
    nameModl SPkg pkg
SBase                KnownModule pkg
DataEither                  = String
"Data.Either"
    nameModl SPkg pkg
SByteString          KnownModule pkg
DataByteStringInternal      = String
"Data.ByteString.Internal"
    nameModl SPkg pkg
SByteString          KnownModule pkg
DataByteStringLazyInternal  = String
"Data.ByteString.Lazy.Internal"
    nameModl SPkg pkg
SByteString          KnownModule pkg
DataByteStringShortInternal = String
"Data.ByteString.Short.Internal"
    nameModl SPkg pkg
SText                KnownModule pkg
DataTextInternal            = String
"Data.Text.Internal"
    nameModl SPkg pkg
SText                KnownModule pkg
DataTextInternalLazy        = String
"Data.Text.Internal.Lazy"
    nameModl SPkg pkg
SIntegerWiredIn      KnownModule pkg
GhcIntegerType              = String
"GHC.Integer.Type"
    nameModl SPkg pkg
SGhcBignum           KnownModule pkg
GhcNumInteger               = String
"GHC.Num.Integer"
    nameModl SPkg pkg
SContainers          KnownModule pkg
DataSetInternal             = String
"Data.Set.Internal"
    nameModl SPkg pkg
SContainers          KnownModule pkg
DataMapInternal             = String
"Data.Map.Internal"
    nameModl SPkg pkg
SContainers          KnownModule pkg
DataIntSetInternal          = String
"Data.IntSet.Internal"
    nameModl SPkg pkg
SContainers          KnownModule pkg
DataIntMapInternal          = String
"Data.IntMap.Internal"
    nameModl SPkg pkg
SContainers          KnownModule pkg
DataSequenceInternal        = String
"Data.Sequence.Internal"
    nameModl SPkg pkg
SContainers          KnownModule pkg
DataTree                    = String
"Data.Tree"
    nameModl SPkg pkg
SAeson               KnownModule pkg
DataAesonTypesInternal      = String
"Data.Aeson.Types.Internal"
    nameModl SPkg pkg
SUnorderedContainers KnownModule pkg
DataHashMapInternal         = String
"Data.HashMap.Internal"
    nameModl SPkg pkg
SUnorderedContainers KnownModule pkg
DataHashMapInternalArray    = String
"Data.HashMap.Internal.Array"
    nameModl SPkg pkg
SVector              KnownModule pkg
DataVector                  = String
"Data.Vector"