1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
-- | Types and functions for raw and lexed docstrings.
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
module GHC.Hs.Doc
( HsDoc
, WithHsDocIdentifiers(..)
, hsDocIds
, LHsDoc
, pprHsDocDebug
, pprWithDoc
, pprMaybeWithDoc
, module GHC.Hs.DocString
, ExtractedTHDocs(..)
, DocStructureItem(..)
, DocStructure
, Docs(..)
, emptyDocs
) where
import GHC.Prelude
import GHC.Utils.Binary
import GHC.Types.Name
import GHC.Utils.Outputable as Outputable hiding ((<>))
import GHC.Types.SrcLoc
import qualified GHC.Data.EnumSet as EnumSet
import GHC.Data.EnumSet (EnumSet)
import GHC.Types.Avail
import GHC.Types.Name.Set
import GHC.Driver.Flags
import Control.DeepSeq
import Data.Data
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.Map (Map)
import qualified Data.Map as Map
import Data.List.NonEmpty (NonEmpty(..))
import GHC.LanguageExtensions.Type
import qualified GHC.Utils.Outputable as O
import GHC.Hs.Extension
import GHC.Types.Unique.Map
import Data.List (sortBy)
import GHC.Hs.DocString
import Language.Haskell.Syntax.Extension
import Language.Haskell.Syntax.Module.Name
-- | A docstring with the (probable) identifiers found in it.
type HsDoc = WithHsDocIdentifiers HsDocString
-- | Annotate a value with the probable identifiers found in it
-- These will be used by haddock to generate links.
--
-- The identifiers are bundled along with their location in the source file.
-- This is useful for tooling to know exactly where they originate.
--
-- This type is currently used in two places - for regular documentation comments,
-- with 'a' set to 'HsDocString', and for adding identifier information to
-- warnings, where 'a' is 'StringLiteral'
data WithHsDocIdentifiers a pass = WithHsDocIdentifiers
{ hsDocString :: !a
, hsDocIdentifiers :: ![Located (IdP pass)]
}
deriving instance (Data pass, Data (IdP pass), Data a) => Data (WithHsDocIdentifiers a pass)
deriving instance (Eq (IdP pass), Eq a) => Eq (WithHsDocIdentifiers a pass)
instance (NFData (IdP pass), NFData a) => NFData (WithHsDocIdentifiers a pass) where
rnf (WithHsDocIdentifiers d i) = rnf d `seq` rnf i
-- | For compatibility with the existing @-ddump-parsed' output, we only show
-- the docstring.
--
-- Use 'pprHsDoc' to show `HsDoc`'s internals.
instance Outputable a => Outputable (WithHsDocIdentifiers a pass) where
ppr (WithHsDocIdentifiers s _ids) = ppr s
instance Binary a => Binary (WithHsDocIdentifiers a GhcRn) where
put_ bh (WithHsDocIdentifiers s ids) = do
put_ bh s
put_ bh $ BinLocated <$> ids
get bh =
liftA2 WithHsDocIdentifiers (get bh) (fmap unBinLocated <$> get bh)
-- | Extract a mapping from the lexed identifiers to the names they may
-- correspond to.
hsDocIds :: WithHsDocIdentifiers a GhcRn -> NameSet
hsDocIds (WithHsDocIdentifiers _ ids) = mkNameSet $ map unLoc ids
-- | Pretty print a thing with its doc
-- The docstring will include the comment decorators '-- |', '{-|' etc
-- and will come either before or after depending on how it was written
-- i.e it will come after the thing if it is a '-- ^' or '{-^' and before
-- otherwise.
pprWithDoc :: LHsDoc name -> SDoc -> SDoc
pprWithDoc doc = pprWithDocString (hsDocString $ unLoc doc)
-- | See 'pprWithHsDoc'
pprMaybeWithDoc :: Maybe (LHsDoc name) -> SDoc -> SDoc
pprMaybeWithDoc Nothing = id
pprMaybeWithDoc (Just doc) = pprWithDoc doc
-- | Print a doc with its identifiers, useful for debugging
pprHsDocDebug :: (Outputable (IdP name)) => HsDoc name -> SDoc
pprHsDocDebug (WithHsDocIdentifiers s ids) =
vcat [ text "text:" $$ nest 2 (pprHsDocString s)
, text "identifiers:" $$ nest 2 (vcat (map pprLocatedAlways ids))
]
type LHsDoc pass = Located (HsDoc pass)
-- | A simplified version of 'HsImpExp.IE'.
data DocStructureItem
= DsiSectionHeading !Int !(HsDoc GhcRn)
| DsiDocChunk !(HsDoc GhcRn)
| DsiNamedChunkRef !(String)
| DsiExports !Avails
| DsiModExport
!(NonEmpty ModuleName) -- ^ We might re-export avails from multiple
-- modules with a single export declaration. E.g.
-- when we have
--
-- > module M (module X) where
-- > import R0 as X
-- > import R1 as X
!Avails
instance Binary DocStructureItem where
put_ bh = \case
DsiSectionHeading level doc -> do
putByte bh 0
put_ bh level
put_ bh doc
DsiDocChunk doc -> do
putByte bh 1
put_ bh doc
DsiNamedChunkRef name -> do
putByte bh 2
put_ bh name
DsiExports avails -> do
putByte bh 3
put_ bh avails
DsiModExport mod_names avails -> do
putByte bh 4
put_ bh mod_names
put_ bh avails
get bh = do
tag <- getByte bh
case tag of
0 -> DsiSectionHeading <$> get bh <*> get bh
1 -> DsiDocChunk <$> get bh
2 -> DsiNamedChunkRef <$> get bh
3 -> DsiExports <$> get bh
4 -> DsiModExport <$> get bh <*> get bh
_ -> fail "instance Binary DocStructureItem: Invalid tag"
instance Outputable DocStructureItem where
ppr = \case
DsiSectionHeading level doc -> vcat
[ text "section heading, level" <+> ppr level O.<> colon
, nest 2 (pprHsDocDebug doc)
]
DsiDocChunk doc -> vcat
[ text "documentation chunk:"
, nest 2 (pprHsDocDebug doc)
]
DsiNamedChunkRef name ->
text "reference to named chunk:" <+> text name
DsiExports avails ->
text "avails:" $$ nest 2 (ppr avails)
DsiModExport mod_names avails ->
text "re-exported module(s):" <+> ppr mod_names $$ nest 2 (ppr avails)
instance NFData DocStructureItem where
rnf = \case
DsiSectionHeading level doc -> rnf level `seq` rnf doc
DsiDocChunk doc -> rnf doc
DsiNamedChunkRef name -> rnf name
DsiExports avails -> rnf avails
DsiModExport mod_names avails -> rnf mod_names `seq` rnf avails
type DocStructure = [DocStructureItem]
data Docs = Docs
{ docs_mod_hdr :: Maybe (HsDoc GhcRn)
-- ^ Module header.
, docs_decls :: UniqMap Name [HsDoc GhcRn]
-- ^ Docs for declarations: functions, data types, instances, methods etc.
-- A list because sometimes subsequent haddock comments can be combined into one
, docs_args :: UniqMap Name (IntMap (HsDoc GhcRn))
-- ^ Docs for arguments. E.g. function arguments, method arguments.
, docs_structure :: DocStructure
, docs_named_chunks :: Map String (HsDoc GhcRn)
-- ^ Map from chunk name to content.
--
-- This map will be empty unless we have an explicit export list from which
-- we can reference the chunks.
, docs_haddock_opts :: Maybe String
-- ^ Haddock options from @OPTIONS_HADDOCK@ or from @-haddock-opts@.
, docs_language :: Maybe Language
-- ^ The 'Language' used in the module, for example 'Haskell2010'.
, docs_extensions :: EnumSet Extension
-- ^ The full set of language extensions used in the module.
}
instance NFData Docs where
rnf (Docs mod_hdr decls args structure named_chunks haddock_opts language extentions)
= rnf mod_hdr `seq` rnf decls `seq` rnf args `seq` rnf structure `seq` rnf named_chunks
`seq` rnf haddock_opts `seq` rnf language `seq` rnf extentions
`seq` ()
instance Binary Docs where
put_ bh docs = do
put_ bh (docs_mod_hdr docs)
put_ bh (sortBy (\a b -> (fst a) `stableNameCmp` fst b) $ nonDetEltsUniqMap $ docs_decls docs)
put_ bh (sortBy (\a b -> (fst a) `stableNameCmp` fst b) $ nonDetEltsUniqMap $ docs_args docs)
put_ bh (docs_structure docs)
put_ bh (Map.toList $ docs_named_chunks docs)
put_ bh (docs_haddock_opts docs)
put_ bh (docs_language docs)
put_ bh (docs_extensions docs)
get bh = do
mod_hdr <- get bh
decls <- listToUniqMap <$> get bh
args <- listToUniqMap <$> get bh
structure <- get bh
named_chunks <- Map.fromList <$> get bh
haddock_opts <- get bh
language <- get bh
exts <- get bh
pure Docs { docs_mod_hdr = mod_hdr
, docs_decls = decls
, docs_args = args
, docs_structure = structure
, docs_named_chunks = named_chunks
, docs_haddock_opts = haddock_opts
, docs_language = language
, docs_extensions = exts
}
instance Outputable Docs where
ppr docs =
vcat
[ pprField (pprMaybe pprHsDocDebug) "module header" docs_mod_hdr
, pprField (ppr . fmap (ppr . map pprHsDocDebug)) "declaration docs" docs_decls
, pprField (ppr . fmap (pprIntMap ppr pprHsDocDebug)) "arg docs" docs_args
, pprField (vcat . map ppr) "documentation structure" docs_structure
, pprField (pprMap (doubleQuotes . text) pprHsDocDebug) "named chunks"
docs_named_chunks
, pprField pprMbString "haddock options" docs_haddock_opts
, pprField ppr "language" docs_language
, pprField (vcat . map ppr . EnumSet.toList) "language extensions"
docs_extensions
]
where
pprField :: (a -> SDoc) -> String -> (Docs -> a) -> SDoc
pprField ppr' heading lbl =
text heading O.<> colon $$ nest 2 (ppr' (lbl docs))
pprMap pprKey pprVal m =
vcat $ flip map (Map.toList m) $ \(k, v) ->
pprKey k O.<> colon $$ nest 2 (pprVal v)
pprIntMap pprKey pprVal m =
vcat $ flip map (IntMap.toList m) $ \(k, v) ->
pprKey k O.<> colon $$ nest 2 (pprVal v)
pprMbString Nothing = empty
pprMbString (Just s) = text s
pprMaybe ppr' = \case
Nothing -> text "Nothing"
Just x -> text "Just" <+> ppr' x
emptyDocs :: Docs
emptyDocs = Docs
{ docs_mod_hdr = Nothing
, docs_decls = emptyUniqMap
, docs_args = emptyUniqMap
, docs_structure = []
, docs_named_chunks = Map.empty
, docs_haddock_opts = Nothing
, docs_language = Nothing
, docs_extensions = EnumSet.empty
}
-- | Maps of docs that were added via Template Haskell's @putDoc@.
data ExtractedTHDocs =
ExtractedTHDocs
{ ethd_mod_header :: Maybe (HsDoc GhcRn)
-- ^ The added module header documentation, if it exists.
, ethd_decl_docs :: UniqMap Name (HsDoc GhcRn)
-- ^ The documentation added to declarations.
, ethd_arg_docs :: UniqMap Name (IntMap (HsDoc GhcRn))
-- ^ The documentation added to function arguments.
, ethd_inst_docs :: UniqMap Name (HsDoc GhcRn)
-- ^ The documentation added to class and family instances.
}
|