-----------------------------------------------------------------------------
-- A Parser monad with access to the 'DynFlags'.
--
-- The 'P' monad only has access to the subset of 'DynFlags'
-- required for parsing Haskell.

-- The parser for C-- requires access to a lot more of the 'DynFlags',
-- so 'PD' provides access to 'DynFlags' via a 'HasDynFlags' instance.
-----------------------------------------------------------------------------
module GHC.Cmm.Parser.Monad (
    PD(..)
  , liftP
  , failMsgPD
  , getPDConfig
  , getProfile
  , getPlatform
  , getHomeUnitId
  , PDConfig(..)
  ) where

import GHC.Prelude

import GHC.Cmm.Parser.Config

import GHC.Platform
import GHC.Platform.Profile

import Control.Monad

import GHC.Parser.Lexer
import GHC.Parser.Errors.Types
import GHC.Types.Error ( MsgEnvelope )
import GHC.Types.SrcLoc
import GHC.Unit.Types
import GHC.Unit.Home

newtype PD a = PD { forall a. PD a -> PDConfig -> HomeUnit -> PState -> ParseResult a
unPD :: PDConfig -> HomeUnit -> PState -> ParseResult a }

instance Functor PD where
  fmap :: forall a b. (a -> b) -> PD a -> PD b
fmap = forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM

instance Applicative PD where
  pure :: forall a. a -> PD a
pure = forall a. a -> PD a
returnPD
  <*> :: forall a b. PD (a -> b) -> PD a -> PD b
(<*>) = forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad PD where
  >>= :: forall a b. PD a -> (a -> PD b) -> PD b
(>>=) = forall a b. PD a -> (a -> PD b) -> PD b
thenPD

liftP :: P a -> PD a
liftP :: forall a. P a -> PD a
liftP (P PState -> ParseResult a
f) = forall a. (PDConfig -> HomeUnit -> PState -> ParseResult a) -> PD a
PD forall a b. (a -> b) -> a -> b
$ \PDConfig
_ HomeUnit
_ PState
s -> PState -> ParseResult a
f PState
s

failMsgPD :: (SrcSpan -> MsgEnvelope PsMessage) -> PD a
failMsgPD :: forall a. (SrcSpan -> MsgEnvelope PsMessage) -> PD a
failMsgPD = forall a. P a -> PD a
liftP forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (SrcSpan -> MsgEnvelope PsMessage) -> P a
failMsgP

returnPD :: a -> PD a
returnPD :: forall a. a -> PD a
returnPD = forall a. P a -> PD a
liftP forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return

thenPD :: PD a -> (a -> PD b) -> PD b
(PD PDConfig -> HomeUnit -> PState -> ParseResult a
m) thenPD :: forall a b. PD a -> (a -> PD b) -> PD b
`thenPD` a -> PD b
k = forall a. (PDConfig -> HomeUnit -> PState -> ParseResult a) -> PD a
PD forall a b. (a -> b) -> a -> b
$ \PDConfig
d HomeUnit
hu PState
s ->
        case PDConfig -> HomeUnit -> PState -> ParseResult a
m PDConfig
d HomeUnit
hu PState
s of
                POk PState
s1 a
a   -> forall a. PD a -> PDConfig -> HomeUnit -> PState -> ParseResult a
unPD (a -> PD b
k a
a) PDConfig
d HomeUnit
hu PState
s1
                PFailed PState
s1 -> forall a. PState -> ParseResult a
PFailed PState
s1

getPDConfig :: PD PDConfig
getPDConfig :: PD PDConfig
getPDConfig = forall a. (PDConfig -> HomeUnit -> PState -> ParseResult a) -> PD a
PD forall a b. (a -> b) -> a -> b
$ \PDConfig
pdc HomeUnit
_ PState
s -> forall a. PState -> a -> ParseResult a
POk PState
s PDConfig
pdc

getProfile :: PD Profile
getProfile :: PD Profile
getProfile = forall a. (PDConfig -> HomeUnit -> PState -> ParseResult a) -> PD a
PD forall a b. (a -> b) -> a -> b
$ \PDConfig
pdc HomeUnit
_ PState
s -> forall a. PState -> a -> ParseResult a
POk PState
s (PDConfig -> Profile
pdProfile PDConfig
pdc)

getPlatform :: PD Platform
getPlatform :: PD Platform
getPlatform = Profile -> Platform
profilePlatform forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PD Profile
getProfile

-- | Return the UnitId of the home-unit. This is used to create labels.
getHomeUnitId :: PD UnitId
getHomeUnitId :: PD UnitId
getHomeUnitId = forall a. (PDConfig -> HomeUnit -> PState -> ParseResult a) -> PD a
PD forall a b. (a -> b) -> a -> b
$ \PDConfig
_ HomeUnit
hu PState
s -> forall a. PState -> a -> ParseResult a
POk PState
s (forall u. GenHomeUnit u -> UnitId
homeUnitId HomeUnit
hu)