module GHC.Rename.Doc ( rnHsDoc, rnLHsDoc, rnLDocDecl, rnDocDecl ) where

import GHC.Prelude

import GHC.Tc.Types
import GHC.Hs
import GHC.Types.Name.Reader
import GHC.Types.Name
import GHC.Types.SrcLoc
import GHC.Tc.Utils.Monad (getGblEnv)

rnLHsDoc :: LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
rnLHsDoc :: LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
rnLHsDoc = (HsDoc GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (HsDoc GhcRn))
-> LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> GenLocated SrcSpan a -> f (GenLocated SrcSpan b)
traverse HsDoc GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (HsDoc GhcRn)
forall a.
WithHsDocIdentifiers a GhcPs -> RnM (WithHsDocIdentifiers a GhcRn)
rnHsDoc

rnLDocDecl :: LDocDecl GhcPs -> RnM (LDocDecl GhcRn)
rnLDocDecl :: LDocDecl GhcPs -> RnM (LDocDecl GhcRn)
rnLDocDecl = (DocDecl GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn))
-> GenLocated SrcSpanAnnA (DocDecl GhcPs)
-> IOEnv
     (Env TcGblEnv TcLclEnv) (GenLocated SrcSpanAnnA (DocDecl GhcRn))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b)
-> GenLocated SrcSpanAnnA a -> f (GenLocated SrcSpanAnnA b)
traverse DocDecl GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
rnDocDecl

rnDocDecl :: DocDecl GhcPs -> RnM (DocDecl GhcRn)
rnDocDecl :: DocDecl GhcPs -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
rnDocDecl (DocCommentNext LHsDoc GhcPs
doc) = do
  LHsDoc GhcRn
doc' <- LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
rnLHsDoc LHsDoc GhcPs
doc
  DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn))
-> DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
forall a b. (a -> b) -> a -> b
$ (LHsDoc GhcRn -> DocDecl GhcRn
forall pass. LHsDoc pass -> DocDecl pass
DocCommentNext LHsDoc GhcRn
doc')
rnDocDecl (DocCommentPrev LHsDoc GhcPs
doc) = do
  LHsDoc GhcRn
doc' <- LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
rnLHsDoc LHsDoc GhcPs
doc
  DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn))
-> DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
forall a b. (a -> b) -> a -> b
$ (LHsDoc GhcRn -> DocDecl GhcRn
forall pass. LHsDoc pass -> DocDecl pass
DocCommentPrev LHsDoc GhcRn
doc')
rnDocDecl (DocCommentNamed String
n LHsDoc GhcPs
doc) = do
  LHsDoc GhcRn
doc' <- LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
rnLHsDoc LHsDoc GhcPs
doc
  DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn))
-> DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
forall a b. (a -> b) -> a -> b
$ (String -> LHsDoc GhcRn -> DocDecl GhcRn
forall pass. String -> LHsDoc pass -> DocDecl pass
DocCommentNamed String
n LHsDoc GhcRn
doc')
rnDocDecl (DocGroup Int
i LHsDoc GhcPs
doc) = do
  LHsDoc GhcRn
doc' <- LHsDoc GhcPs -> RnM (LHsDoc GhcRn)
rnLHsDoc LHsDoc GhcPs
doc
  DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn))
-> DocDecl GhcRn -> IOEnv (Env TcGblEnv TcLclEnv) (DocDecl GhcRn)
forall a b. (a -> b) -> a -> b
$ (Int -> LHsDoc GhcRn -> DocDecl GhcRn
forall pass. Int -> LHsDoc pass -> DocDecl pass
DocGroup Int
i LHsDoc GhcRn
doc')

rnHsDoc :: WithHsDocIdentifiers a GhcPs -> RnM (WithHsDocIdentifiers a GhcRn)
rnHsDoc :: forall a.
WithHsDocIdentifiers a GhcPs -> RnM (WithHsDocIdentifiers a GhcRn)
rnHsDoc (WithHsDocIdentifiers a
s [Located (IdP GhcPs)]
ids) = do
  GlobalRdrEnv
gre <- TcGblEnv -> GlobalRdrEnv
tcg_rdr_env (TcGblEnv -> GlobalRdrEnv)
-> IOEnv (Env TcGblEnv TcLclEnv) TcGblEnv
-> IOEnv (Env TcGblEnv TcLclEnv) GlobalRdrEnv
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IOEnv (Env TcGblEnv TcLclEnv) TcGblEnv
forall gbl lcl. TcRnIf gbl lcl gbl
getGblEnv
  WithHsDocIdentifiers a GhcRn -> RnM (WithHsDocIdentifiers a GhcRn)
forall a. a -> IOEnv (Env TcGblEnv TcLclEnv) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> [Located (IdP GhcRn)] -> WithHsDocIdentifiers a GhcRn
forall a pass.
a -> [Located (IdP pass)] -> WithHsDocIdentifiers a pass
WithHsDocIdentifiers a
s (GlobalRdrEnv -> [Located RdrName] -> [Located Name]
rnHsDocIdentifiers GlobalRdrEnv
gre [Located (IdP GhcPs)]
[Located RdrName]
ids))

rnHsDocIdentifiers :: GlobalRdrEnv
                   -> [Located RdrName]
                   -> [Located Name]
rnHsDocIdentifiers :: GlobalRdrEnv -> [Located RdrName] -> [Located Name]
rnHsDocIdentifiers GlobalRdrEnv
gre_env [Located RdrName]
ns =
  [ SrcSpan -> Name -> Located Name
forall l e. l -> e -> GenLocated l e
L SrcSpan
l (Name -> Located Name) -> Name -> Located Name
forall a b. (a -> b) -> a -> b
$ GlobalRdrEltX GREInfo -> Name
forall info. GlobalRdrEltX info -> Name
greName GlobalRdrEltX GREInfo
gre
  | L SrcSpan
l RdrName
rdr_name <- [Located RdrName]
ns
  , GlobalRdrEltX GREInfo
gre <- GlobalRdrEnv -> LookupGRE GREInfo -> [GlobalRdrEltX GREInfo]
forall info.
GlobalRdrEnvX info -> LookupGRE info -> [GlobalRdrEltX info]
lookupGRE GlobalRdrEnv
gre_env (RdrName -> WhichGREs GREInfo -> LookupGRE GREInfo
forall info. RdrName -> WhichGREs info -> LookupGRE info
LookupRdrName RdrName
rdr_name WhichGREs GREInfo
AllRelevantGREs)
  ]