{-# LANGUAGE DataKinds      #-}
{-# LANGUAGE GADTs          #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase     #-}
{-# 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
  | PkgPrimitive

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
  SPrimitive           :: SPkg 'PkgPrimitive

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
instance IsKnownPkg 'PkgPrimitive           where singPkg :: SPkg 'PkgPrimitive
singPkg = SPkg 'PkgPrimitive
SPrimitive

{-------------------------------------------------------------------------------
  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
  | DataVectorStorable
  | DataVectorStorableMutable
  | DataVectorPrimitive
  | DataVectorPrimitiveMutable

{-------------------------------------------------------------------------------
  Modules in @primitive@
-------------------------------------------------------------------------------}

data instance KnownModule 'PkgPrimitive =
    DataPrimitiveArray

{-------------------------------------------------------------------------------
  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
        -- We ignore the package version for now
        Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (String -> String
stripVowels (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 -> String
stripVowels String
pkg)
        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"
    namePkg SPkg pkg
SPrimitive           = String
"primitive"

    nameModl :: SPkg pkg -> KnownModule pkg -> String
    nameModl :: SPkg pkg -> KnownModule pkg -> String
nameModl = \case
        SPkg pkg
SGhcPrim -> \case
          KnownModule pkg
GhcTypes -> String
"GHC.Types"
          KnownModule pkg
GhcTuple -> String
"GHC.Tuple"

        SPkg pkg
SBase -> \case
          KnownModule pkg
GhcInt      -> String
"GHC.Int"
          KnownModule pkg
GhcWord     -> String
"GHC.Word"
          KnownModule pkg
GhcSTRef    -> String
"GHC.STRef"
          KnownModule pkg
GhcMVar     -> String
"GHC.MVar"
          KnownModule pkg
GhcConcSync -> String
"GHC.Conc.Sync"
          KnownModule pkg
GhcMaybe    -> String
"GHC.Maybe"
          KnownModule pkg
GhcReal     -> String
"GHC.Real"
          KnownModule pkg
DataEither  -> String
"Data.Either"

        SPkg pkg
SByteString -> \case
          KnownModule pkg
DataByteStringInternal      -> String
"Data.ByteString.Internal"
          KnownModule pkg
DataByteStringLazyInternal  -> String
"Data.ByteString.Lazy.Internal"
          KnownModule pkg
DataByteStringShortInternal -> String
"Data.ByteString.Short.Internal"

        SPkg pkg
SText -> \case
          KnownModule pkg
DataTextInternal     -> String
"Data.Text.Internal"
          KnownModule pkg
DataTextInternalLazy -> String
"Data.Text.Internal.Lazy"

        SPkg pkg
SIntegerWiredIn -> \case
          KnownModule pkg
GhcIntegerType -> String
"GHC.Integer.Type"

        SPkg pkg
SGhcBignum -> \case
          KnownModule pkg
GhcNumInteger -> String
"GHC.Num.Integer"

        SPkg pkg
SContainers -> \case
          KnownModule pkg
DataSetInternal      -> String
"Data.Set.Internal"
          KnownModule pkg
DataMapInternal      -> String
"Data.Map.Internal"
          KnownModule pkg
DataIntSetInternal   -> String
"Data.IntSet.Internal"
          KnownModule pkg
DataIntMapInternal   -> String
"Data.IntMap.Internal"
          KnownModule pkg
DataSequenceInternal -> String
"Data.Sequence.Internal"
          KnownModule pkg
DataTree             -> String
"Data.Tree"

        SPkg pkg
SAeson -> \case
          KnownModule pkg
DataAesonTypesInternal -> String
"Data.Aeson.Types.Internal"

        SPkg pkg
SUnorderedContainers -> \case
          KnownModule pkg
DataHashMapInternal      -> String
"Data.HashMap.Internal"
          KnownModule pkg
DataHashMapInternalArray -> String
"Data.HashMap.Internal.Array"

        SPkg pkg
SVector -> \case
          KnownModule pkg
DataVector                 -> String
"Data.Vector"
          KnownModule pkg
DataVectorStorable         -> String
"Data.Vector.Storable"
          KnownModule pkg
DataVectorStorableMutable  -> String
"Data.Vector.Storable.Mutable"
          KnownModule pkg
DataVectorPrimitive        -> String
"Data.Vector.Primitive"
          KnownModule pkg
DataVectorPrimitiveMutable -> String
"Data.Vector.Primitive.Mutable"

        SPkg pkg
SPrimitive -> \case
          KnownModule pkg
DataPrimitiveArray -> String
"Data.Primitive.Array"

    -- On OSX, cabal strips vowels from package IDs in order to work around
    -- limitations around path lengths
    -- <https://github.com/haskell/cabal/blob/3f397c0c661facd0be9c5c67ad26f66a87725472/cabal-install/src/Distribution/Client/PackageHash.hs#L125-L157>
    stripVowels :: String -> String
    stripVowels :: String -> String
stripVowels = (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
filter (Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` String
"aeoiu")