recursion-ninja pushed to branch wip/fix-26971 at Glasgow Haskell Compiler / GHC

Commits:

19 changed files:

Changes:

  • compiler/GHC/Hs/Basic.hs
    ... ... @@ -14,7 +14,7 @@ import GHC.Prelude
    14 14
     import GHC.Utils.Outputable
    
    15 15
     import GHC.Utils.Binary
    
    16 16
     import GHC.Types.Name
    
    17
    -import GHC.Parser.Annotation
    
    17
    +--import GHC.Parser.Annotation
    
    18 18
     import GHC.Utils.Misc ((<||>))
    
    19 19
     
    
    20 20
     import Data.Data (Data)
    
    ... ... @@ -86,8 +86,8 @@ instance Binary FixityDirection where
    86 86
     -- @
    
    87 87
     data NamespaceSpecifier
    
    88 88
       = NoNamespaceSpecifier
    
    89
    -  | TypeNamespaceSpecifier (EpToken "type")
    
    90
    -  | DataNamespaceSpecifier (EpToken "data")
    
    89
    +  | TypeNamespaceSpecifier () -- (EpToken "type")
    
    90
    +  | DataNamespaceSpecifier () --(EpToken "data")
    
    91 91
       deriving (Eq, Data)
    
    92 92
     
    
    93 93
     -- | Check if namespace specifiers overlap, i.e. if they are equal or
    

  • compiler/GHC/Hs/Doc.hs
    ... ... @@ -32,6 +32,7 @@ import GHC.Data.EnumSet (EnumSet)
    32 32
     import GHC.Types.Avail
    
    33 33
     import GHC.Types.Name.Set
    
    34 34
     import GHC.Driver.Flags
    
    35
    +import GHC.Parser.Annotation
    
    35 36
     
    
    36 37
     import Control.DeepSeq
    
    37 38
     import Data.Data
    
    ... ... @@ -49,29 +50,15 @@ import Data.Function
    49 50
     
    
    50 51
     import GHC.Hs.DocString
    
    51 52
     
    
    53
    +import Language.Haskell.Syntax.Doc
    
    52 54
     import Language.Haskell.Syntax.Extension
    
    53 55
     import Language.Haskell.Syntax.Module.Name
    
    54 56
     
    
    55
    --- | A docstring with the (probable) identifiers found in it.
    
    56
    -type HsDoc = WithHsDocIdentifiers HsDocString
    
    57
    +deriving instance Eq a => Eq (WithHsDocIdentifiers a GhcPs)
    
    58
    +deriving instance Eq a => Eq (WithHsDocIdentifiers a GhcRn)
    
    59
    +deriving instance Eq a => Eq (WithHsDocIdentifiers a GhcTc)
    
    57 60
     
    
    58
    --- | Annotate a value with the probable identifiers found in it
    
    59
    --- These will be used by haddock to generate links.
    
    60
    ---
    
    61
    --- The identifiers are bundled along with their location in the source file.
    
    62
    --- This is useful for tooling to know exactly where they originate.
    
    63
    ---
    
    64
    --- This type is currently used in two places - for regular documentation comments,
    
    65
    --- with 'a' set to 'HsDocString', and for adding identifier information to
    
    66
    --- warnings, where 'a' is 'StringLiteral'
    
    67
    -data WithHsDocIdentifiers a pass = WithHsDocIdentifiers
    
    68
    -  { hsDocString      :: !a
    
    69
    -  , hsDocIdentifiers :: ![Located (IdP pass)]
    
    70
    -  }
    
    71
    -
    
    72
    -deriving instance (Data pass, Data (IdP pass), Data a) => Data (WithHsDocIdentifiers a pass)
    
    73
    -deriving instance (Eq (IdP pass), Eq a) => Eq (WithHsDocIdentifiers a pass)
    
    74
    -instance (NFData (IdP pass), NFData a) => NFData (WithHsDocIdentifiers a pass) where
    
    61
    +instance (NFData (LIdP (GhcPass pass)), NFData a) => NFData (WithHsDocIdentifiers a (GhcPass pass)) where
    
    75 62
       rnf (WithHsDocIdentifiers d i) = rnf d `seq` rnf i
    
    76 63
     
    
    77 64
     -- | For compatibility with the existing @-ddump-parsed' output, we only show
    
    ... ... @@ -81,12 +68,26 @@ instance (NFData (IdP pass), NFData a) => NFData (WithHsDocIdentifiers a pass) w
    81 68
     instance Outputable a => Outputable (WithHsDocIdentifiers a pass) where
    
    82 69
       ppr (WithHsDocIdentifiers s _ids) = ppr s
    
    83 70
     
    
    84
    -instance Binary a => Binary (WithHsDocIdentifiers a GhcRn) where
    
    71
    +{-
    
    72
    +instance forall a . (Binary a, Binary (Anno Name)) => Binary (WithHsDocIdentifiers a (GhcRn)) where
    
    85 73
       put_ bh (WithHsDocIdentifiers s ids) = do
    
    86 74
         put_ bh s
    
    87
    -    put_ bh $ BinLocated <$> (sortBy  (stableNameCmp `on` getName) ids)
    
    75
    +    put_ bh $ BinGenLocated <$> ids
    
    76
    +--    put_ bh ids
    
    88 77
       get bh =
    
    89
    -    liftA2 WithHsDocIdentifiers (get bh) (fmap unBinLocated <$> get bh)
    
    78
    +    liftA2 (WithHsDocIdentifiers :: a -> [LIdP (GhcPass p)] -> WithHsDocIdentifiers a (GhcPass p)) (get bh) (fmap unBinGenLocated <$> get bh)
    
    79
    +-}
    
    80
    +{-
    
    81
    +ids = [GenLocated (Anno Name) Name]
    
    82
    +    = [GenLocated (SrcSpanAnnN) Name]
    
    83
    +    = [GenLocated (EpAnn NameAnn) Name]
    
    84
    +
    
    85
    +
    
    86
    +[LIdP GhcRn] = [XRec GhcRn (IdP GhcRn)]
    
    87
    +             = [XRec GhcRn (IdGhcP 'Renamed)]
    
    88
    +             = [XRec GhcRn Name]
    
    89
    +-}
    
    90
    +
    
    90 91
     
    
    91 92
     -- | Extract a mapping from the lexed identifiers to the names they may
    
    92 93
     -- correspond to.
    
    ... ... @@ -98,22 +99,21 @@ hsDocIds (WithHsDocIdentifiers _ ids) = mkNameSet $ map unLoc ids
    98 99
     -- and will come either before or after depending on how it was written
    
    99 100
     -- i.e it will come after the thing if it is a '-- ^' or '{-^' and before
    
    100 101
     -- otherwise.
    
    101
    -pprWithDoc :: LHsDoc name -> SDoc -> SDoc
    
    102
    +pprWithDoc :: LHsDoc (GhcPass name) -> SDoc -> SDoc
    
    102 103
     pprWithDoc doc = pprWithDocString (hsDocString $ unLoc doc)
    
    103 104
     
    
    104 105
     -- | See 'pprWithHsDoc'
    
    105
    -pprMaybeWithDoc :: Maybe (LHsDoc name) -> SDoc -> SDoc
    
    106
    +pprMaybeWithDoc :: Maybe (LHsDoc (GhcPass name)) -> SDoc -> SDoc
    
    106 107
     pprMaybeWithDoc Nothing    = id
    
    107 108
     pprMaybeWithDoc (Just doc) = pprWithDoc doc
    
    108 109
     
    
    109 110
     -- | Print a doc with its identifiers, useful for debugging
    
    110
    -pprHsDocDebug :: (Outputable (IdP name)) => HsDoc name -> SDoc
    
    111
    +pprHsDocDebug :: HsDoc (GhcPass name) -> SDoc
    
    111 112
     pprHsDocDebug (WithHsDocIdentifiers s ids) =
    
    112 113
         vcat [ text "text:" $$ nest 2 (pprHsDocString s)
    
    113
    -         , text "identifiers:" $$ nest 2 (vcat (map pprLocatedAlways ids))
    
    114
    +--         , text "identifiers:" $$ nest 2 (vcat (map pprLocatedAlways ids))
    
    114 115
              ]
    
    115
    -
    
    116
    -type LHsDoc pass = Located (HsDoc pass)
    
    116
    +-- XRec p (IdP p)
    
    117 117
     
    
    118 118
     -- | A simplified version of 'HsImpExp.IE'.
    
    119 119
     data DocStructureItem
    
    ... ... @@ -136,6 +136,7 @@ data DocStructureItem
    136 136
                                 -- ^ Invariant: This list of Avails must be sorted
    
    137 137
                                 -- to guarantee interface file determinism.
    
    138 138
     
    
    139
    +{-
    
    139 140
     instance Binary DocStructureItem where
    
    140 141
       put_ bh = \case
    
    141 142
         DsiSectionHeading level doc -> do
    
    ... ... @@ -165,6 +166,7 @@ instance Binary DocStructureItem where
    165 166
           3 -> DsiExports <$> get bh
    
    166 167
           4 -> DsiModExport <$> get bh <*> get bh
    
    167 168
           _ -> fail "instance Binary DocStructureItem: Invalid tag"
    
    169
    +-}
    
    168 170
     
    
    169 171
     instance Outputable DocStructureItem where
    
    170 172
       ppr = \case
    
    ... ... @@ -185,8 +187,8 @@ instance Outputable DocStructureItem where
    185 187
     
    
    186 188
     instance NFData DocStructureItem where
    
    187 189
       rnf = \case
    
    188
    -    DsiSectionHeading level doc -> rnf level `seq` rnf doc
    
    189
    -    DsiDocChunk doc -> rnf doc
    
    190
    +    DsiSectionHeading level !doc -> rnf level -- `seq` rnf doc
    
    191
    +    DsiDocChunk !doc -> () -- rnf doc
    
    190 192
         DsiNamedChunkRef name -> rnf name
    
    191 193
         DsiExports avails -> rnf avails
    
    192 194
         DsiModExport mod_names avails -> rnf mod_names `seq` rnf avails
    
    ... ... @@ -220,10 +222,16 @@ data Docs = Docs
    220 222
     
    
    221 223
     instance NFData Docs where
    
    222 224
       rnf (Docs mod_hdr exps decls args structure named_chunks haddock_opts language extentions)
    
    225
    +{-
    
    223 226
         = rnf mod_hdr `seq` rnf exps `seq` rnf decls `seq` rnf args `seq` rnf structure `seq` rnf named_chunks
    
    224 227
         `seq` rnf haddock_opts `seq` rnf language `seq` rnf extentions
    
    225 228
         `seq` ()
    
    229
    +-}
    
    230
    +    = rnf structure
    
    231
    +    `seq` rnf haddock_opts `seq` rnf language `seq` rnf extentions
    
    232
    +    `seq` ()
    
    226 233
     
    
    234
    +{-
    
    227 235
     instance Binary Docs where
    
    228 236
       put_ bh docs = do
    
    229 237
         put_ bh (docs_mod_hdr docs)
    
    ... ... @@ -255,6 +263,7 @@ instance Binary Docs where
    255 263
                   , docs_language = language
    
    256 264
                   , docs_extensions = exts
    
    257 265
                   }
    
    266
    +-}
    
    258 267
     
    
    259 268
     instance Outputable Docs where
    
    260 269
       ppr docs =
    

  • compiler/GHC/Hs/Doc.hs-boot deleted
    1
    -module GHC.Hs.Doc where
    
    2
    -
    
    3
    --- See #21592 for progress on removing this boot file.
    
    4
    -
    
    5
    -import GHC.Types.SrcLoc
    
    6
    -import GHC.Hs.DocString
    
    7
    -import Data.Kind
    
    8
    -
    
    9
    -type role WithHsDocIdentifiers representational nominal
    
    10
    -type WithHsDocIdentifiers :: Type -> Type -> Type
    
    11
    -data WithHsDocIdentifiers a pass
    
    12
    -
    
    13
    -type HsDoc :: Type -> Type
    
    14
    -type HsDoc = WithHsDocIdentifiers HsDocString
    
    15
    -
    
    16
    -type LHsDoc :: Type -> Type
    
    17
    -type LHsDoc pass = Located (HsDoc pass)
    
    18
    -

  • compiler/GHC/Hs/DocString.hs
    1
    +{-# LANGUAGE TypeFamilies #-}
    
    2
    +
    
    1 3
     -- | An exactprintable structure for docstrings
    
    2 4
     
    
    3 5
     module GHC.Hs.DocString
    
    4 6
       ( LHsDocString
    
    5 7
       , HsDocString(..)
    
    8
    +  , HsDocStringGhc
    
    6 9
       , HsDocStringDecorator(..)
    
    7 10
       , HsDocStringChunk(..)
    
    8 11
       , LHsDocStringChunk
    
    ... ... @@ -23,6 +26,8 @@ module GHC.Hs.DocString
    23 26
     
    
    24 27
     import GHC.Prelude
    
    25 28
     
    
    29
    +import GHC.Hs.Extension
    
    30
    +
    
    26 31
     import GHC.Utils.Binary
    
    27 32
     import GHC.Utils.Encoding
    
    28 33
     import GHC.Utils.Outputable as Outputable hiding ((<>))
    
    ... ... @@ -34,9 +39,16 @@ import qualified Data.ByteString as BS
    34 39
     import Data.Data
    
    35 40
     import Data.List.NonEmpty (NonEmpty(..))
    
    36 41
     import Data.List (intercalate)
    
    42
    +import Data.Void
    
    43
    +
    
    44
    +import Language.Haskell.Syntax.Doc
    
    45
    +import Language.Haskell.Syntax.Extension
    
    46
    +
    
    47
    +type LHsDocString pass = Located (HsDocString pass)
    
    37 48
     
    
    38
    -type LHsDocString = Located HsDocString
    
    49
    +type HsDocStringGhc = HsDocString Void
    
    39 50
     
    
    51
    +{-
    
    40 52
     -- | Haskell Documentation String
    
    41 53
     --
    
    42 54
     -- Rich structure to support exact printing
    
    ... ... @@ -56,62 +68,81 @@ data HsDocString
    56 68
          -- This is because it may contain unbalanced pairs of '{-' and '-}' and
    
    57 69
          -- not form a valid 'NestedDocString'
    
    58 70
       deriving (Eq, Data, Show)
    
    71
    +-}
    
    59 72
     
    
    60
    -instance Outputable HsDocString where
    
    61
    -  ppr = text . renderHsDocString
    
    73
    +type instance XMultiLineDocString (GhcPass p) = NoExtField
    
    74
    +type instance XNestedDocString    (GhcPass p) = NoExtField
    
    75
    +type instance XGeneratedDocString (GhcPass p) = NoExtField
    
    76
    +type instance XXHsDocString       (GhcPass p) = DataConCantHappen
    
    62 77
     
    
    78
    +{-
    
    63 79
     instance NFData HsDocString where
    
    64 80
       rnf (MultiLineDocString a b) = rnf a `seq` rnf b
    
    65 81
       rnf (NestedDocString a b) = rnf a `seq` rnf b
    
    66 82
       rnf (GeneratedDocString a) = rnf a
    
    83
    +-}
    
    84
    +deriving stock instance Eq   (HsDocString (GhcPass pass))
    
    85
    +-- deriving stock instance Show (HsDocString (GhcPass pass))
    
    67 86
     
    
    68
    --- | Annotate a pretty printed thing with its doc
    
    69
    --- The docstring comes after if is 'HsDocStringPrevious'
    
    70
    --- Otherwise it comes before.
    
    71
    --- Note - we convert MultiLineDocString HsDocStringPrevious to HsDocStringNext
    
    72
    --- because we can't control if something else will be pretty printed on the same line
    
    73
    -pprWithDocString :: HsDocString -> SDoc -> SDoc
    
    74
    -pprWithDocString  (MultiLineDocString HsDocStringPrevious ds) sd = pprWithDocString (MultiLineDocString HsDocStringNext ds) sd
    
    75
    -pprWithDocString doc@(NestedDocString HsDocStringPrevious  _) sd = sd <+> pprHsDocString doc
    
    76
    -pprWithDocString doc sd = pprHsDocString doc $+$ sd
    
    77
    -
    
    78
    -
    
    79
    -instance Binary HsDocString where
    
    87
    +instance Binary (HsDocString (GhcPass p)) where
    
    80 88
       put_ bh x = case x of
    
    81
    -    MultiLineDocString dec xs -> do
    
    89
    +    MultiLineDocString _ dec xs -> do
    
    82 90
           putByte bh 0
    
    83 91
           put_ bh dec
    
    84 92
           put_ bh $ BinLocated <$> xs
    
    85
    -    NestedDocString dec x -> do
    
    93
    +    NestedDocString _ dec x -> do
    
    86 94
           putByte bh 1
    
    87 95
           put_ bh dec
    
    88 96
           put_ bh $ BinLocated x
    
    89
    -    GeneratedDocString x -> do
    
    97
    +    GeneratedDocString _ x -> do
    
    90 98
           putByte bh 2
    
    91 99
           put_ bh x
    
    92 100
       get bh = do
    
    93 101
         tag <- getByte bh
    
    94 102
         case tag of
    
    95
    -      0 -> MultiLineDocString <$> get bh <*> (fmap unBinLocated <$> get bh)
    
    96
    -      1 -> NestedDocString <$> get bh <*> (unBinLocated <$> get bh)
    
    97
    -      2 -> GeneratedDocString <$> get bh
    
    103
    +      0 -> MultiLineDocString NoExtField <$> get bh <*> (fmap unBinLocated <$> get bh)
    
    104
    +      1 -> NestedDocString    NoExtField <$> get bh <*> (unBinLocated <$> get bh)
    
    105
    +      2 -> GeneratedDocString NoExtField <$> get bh
    
    98 106
           t -> fail $ "HsDocString: invalid tag " ++ show t
    
    99 107
     
    
    108
    +instance NFData (HsDocString (GhcPass pass)) where
    
    109
    +  rnf = \case
    
    110
    +    MultiLineDocString NoExtField a b -> rnf a `seq` rnf b
    
    111
    +    NestedDocString    NoExtField a b -> rnf a `seq` rnf b
    
    112
    +    GeneratedDocString NoExtField a   -> rnf a
    
    113
    +
    
    114
    +instance Outputable (HsDocString (GhcPass p)) where
    
    115
    +  ppr = text . renderHsDocString
    
    116
    +
    
    117
    +-- | Annotate a pretty printed thing with its doc
    
    118
    +-- The docstring comes after if is 'HsDocStringPrevious'
    
    119
    +-- Otherwise it comes before.
    
    120
    +-- Note - we convert MultiLineDocString HsDocStringPrevious to HsDocStringNext
    
    121
    +-- because we can't control if something else will be pretty printed on the same line
    
    122
    +pprWithDocString :: HsDocString (GhcPass p) -> SDoc -> SDoc
    
    123
    +pprWithDocString  (MultiLineDocString x HsDocStringPrevious ds) sd = pprWithDocString (MultiLineDocString x HsDocStringNext ds) sd
    
    124
    +pprWithDocString doc@(NestedDocString _ HsDocStringPrevious  _) sd = sd <+> pprHsDocString doc
    
    125
    +pprWithDocString doc sd = pprHsDocString doc $+$ sd
    
    126
    +
    
    127
    +{-
    
    100 128
     data HsDocStringDecorator
    
    101 129
       = HsDocStringNext -- ^ '|' is the decorator
    
    102 130
       | HsDocStringPrevious -- ^ '^' is the decorator
    
    103 131
       | HsDocStringNamed !String -- ^ '$<string>' is the decorator
    
    104 132
       | HsDocStringGroup !Int -- ^ The decorator is the given number of '*'s
    
    105 133
       deriving (Eq, Ord, Show, Data)
    
    134
    +-}
    
    106 135
     
    
    107 136
     instance Outputable HsDocStringDecorator where
    
    108 137
       ppr = text . printDecorator
    
    109 138
     
    
    139
    +{-
    
    110 140
     instance NFData HsDocStringDecorator where
    
    111 141
       rnf HsDocStringNext = ()
    
    112 142
       rnf HsDocStringPrevious = ()
    
    113 143
       rnf (HsDocStringNamed x) = rnf x
    
    114 144
       rnf (HsDocStringGroup x) = rnf x
    
    145
    +-}
    
    115 146
     
    
    116 147
     printDecorator :: HsDocStringDecorator -> String
    
    117 148
     printDecorator HsDocStringNext = "|"
    
    ... ... @@ -134,20 +165,26 @@ instance Binary HsDocStringDecorator where
    134 165
           3 -> HsDocStringGroup <$> get bh
    
    135 166
           t -> fail $ "HsDocStringDecorator: invalid tag " ++ show t
    
    136 167
     
    
    168
    +{-
    
    137 169
     type LHsDocStringChunk = Located HsDocStringChunk
    
    138 170
     
    
    139 171
     -- | A contiguous chunk of documentation
    
    140 172
     newtype HsDocStringChunk = HsDocStringChunk ByteString
    
    141 173
       deriving stock (Eq,Ord,Data, Show)
    
    142 174
       deriving newtype (NFData)
    
    175
    +-}
    
    176
    +
    
    177
    +type instance Anno HsDocStringChunk = SrcSpan
    
    143 178
     
    
    144 179
     instance Binary HsDocStringChunk where
    
    145 180
       put_ bh (HsDocStringChunk bs) = put_ bh bs
    
    146 181
       get bh = HsDocStringChunk <$> get bh
    
    147 182
     
    
    183
    +
    
    148 184
     instance Outputable HsDocStringChunk where
    
    149 185
       ppr = text . unpackHDSC
    
    150 186
     
    
    187
    +{-
    
    151 188
     mkHsDocStringChunk :: String -> HsDocStringChunk
    
    152 189
     mkHsDocStringChunk s = HsDocStringChunk (utf8EncodeByteString s)
    
    153 190
     
    
    ... ... @@ -163,41 +200,42 @@ nullHDSC (HsDocStringChunk bs) = BS.null bs
    163 200
     
    
    164 201
     mkGeneratedHsDocString :: String -> HsDocString
    
    165 202
     mkGeneratedHsDocString = GeneratedDocString . mkHsDocStringChunk
    
    203
    +-}
    
    166 204
     
    
    167
    -isEmptyDocString :: HsDocString -> Bool
    
    168
    -isEmptyDocString (MultiLineDocString _ xs) = all (nullHDSC . unLoc) xs
    
    169
    -isEmptyDocString (NestedDocString _ s) = nullHDSC $ unLoc s
    
    170
    -isEmptyDocString (GeneratedDocString x) = nullHDSC x
    
    205
    +isEmptyDocString :: HsDocString (GhcPass p) -> Bool
    
    206
    +isEmptyDocString (MultiLineDocString _ _ xs) = all (nullHDSC . unLoc) xs
    
    207
    +isEmptyDocString (NestedDocString _ _ s) = nullHDSC $ unLoc s
    
    208
    +isEmptyDocString (GeneratedDocString _ x) = nullHDSC x
    
    171 209
     
    
    172
    -docStringChunks :: HsDocString -> [LHsDocStringChunk]
    
    173
    -docStringChunks (MultiLineDocString _ (x:|xs)) = x:xs
    
    174
    -docStringChunks (NestedDocString _ x) = [x]
    
    175
    -docStringChunks (GeneratedDocString x) = [L (UnhelpfulSpan UnhelpfulGenerated) x]
    
    210
    +docStringChunks :: HsDocString (GhcPass p) -> [LHsDocStringChunk (GhcPass p)]
    
    211
    +docStringChunks (MultiLineDocString _ _ (x:|xs)) = x:xs
    
    212
    +docStringChunks (NestedDocString _ _ x) = [x]
    
    213
    +docStringChunks (GeneratedDocString _ x) = [L (UnhelpfulSpan UnhelpfulGenerated) x]
    
    176 214
     
    
    177 215
     -- | Pretty print with decorators, exactly as the user wrote it
    
    178
    -pprHsDocString :: HsDocString -> SDoc
    
    216
    +pprHsDocString :: HsDocString (GhcPass p) -> SDoc
    
    179 217
     pprHsDocString = text . exactPrintHsDocString
    
    180 218
     
    
    181
    -pprHsDocStrings :: [HsDocString] -> SDoc
    
    219
    +pprHsDocStrings :: [HsDocString (GhcPass p)] -> SDoc
    
    182 220
     pprHsDocStrings = text . intercalate "\n\n" . map exactPrintHsDocString
    
    183 221
     
    
    184 222
     -- | Pretty print with decorators, exactly as the user wrote it
    
    185
    -exactPrintHsDocString :: HsDocString -> String
    
    186
    -exactPrintHsDocString (MultiLineDocString dec (x :| xs))
    
    223
    +exactPrintHsDocString :: HsDocString (GhcPass p) -> String
    
    224
    +exactPrintHsDocString (MultiLineDocString _ dec (x :| xs))
    
    187 225
       = unlines' $ ("-- " ++ printDecorator dec ++ unpackHDSC (unLoc x))
    
    188 226
                 : map (\x -> "--" ++ unpackHDSC (unLoc x)) xs
    
    189
    -exactPrintHsDocString (NestedDocString dec (L _ s))
    
    227
    +exactPrintHsDocString (NestedDocString _ dec (L _ s))
    
    190 228
       = "{-" ++ printDecorator dec ++ unpackHDSC s ++ "-}"
    
    191
    -exactPrintHsDocString (GeneratedDocString x) = case lines (unpackHDSC x) of
    
    229
    +exactPrintHsDocString (GeneratedDocString _ x) = case lines (unpackHDSC x) of
    
    192 230
       [] -> ""
    
    193 231
       (x:xs) -> unlines' $ ( "-- |" ++ x)
    
    194 232
                         : map (\y -> "--"++y) xs
    
    195 233
     
    
    196 234
     -- | Just get the docstring, without any decorators
    
    197
    -renderHsDocString :: HsDocString -> String
    
    198
    -renderHsDocString (MultiLineDocString _ (x :| xs)) = unlines' $ map (unpackHDSC . unLoc) (x:xs)
    
    199
    -renderHsDocString (NestedDocString _ ds) = unpackHDSC $ unLoc ds
    
    200
    -renderHsDocString (GeneratedDocString x) = unpackHDSC x
    
    235
    +renderHsDocString :: HsDocString (GhcPass p) -> String
    
    236
    +renderHsDocString (MultiLineDocString _ _ (x :| xs)) = unlines' $ map (unpackHDSC . unLoc) (x:xs)
    
    237
    +renderHsDocString (NestedDocString _ _ ds) = unpackHDSC $ unLoc ds
    
    238
    +renderHsDocString (GeneratedDocString _ x) = unpackHDSC x
    
    201 239
     
    
    202 240
     -- | Don't add a newline to a single string
    
    203 241
     unlines' :: [String] -> String
    
    ... ... @@ -205,5 +243,5 @@ unlines' = intercalate "\n"
    205 243
     
    
    206 244
     -- | Just get the docstring, without any decorators
    
    207 245
     -- Separates docstrings using "\n\n", which is how haddock likes to render them
    
    208
    -renderHsDocStrings :: [HsDocString] -> String
    
    246
    +renderHsDocStrings :: [HsDocString (GhcPass p)] -> String
    
    209 247
     renderHsDocStrings = intercalate "\n\n" . map renderHsDocString

  • compiler/GHC/Hs/Extension.hs
    ... ... @@ -21,7 +21,7 @@ import GHC.Types.Var
    21 21
     import GHC.Utils.Outputable hiding ((<>))
    
    22 22
     import GHC.Types.SrcLoc (GenLocated(..), unLoc)
    
    23 23
     import GHC.Utils.Panic
    
    24
    -import GHC.Parser.Annotation
    
    24
    +--import GHC.Parser.Annotation
    
    25 25
     
    
    26 26
     {-
    
    27 27
     Note [IsPass]
    
    ... ... @@ -92,16 +92,18 @@ type instance XRec (GhcPass p) a = XRecGhc a
    92 92
     -- but pass-independent, source location
    
    93 93
     type XRecGhc a = GenLocated (Anno a) a
    
    94 94
     
    
    95
    -type instance Anno RdrName = SrcSpanAnnN
    
    96
    -type instance Anno Name    = SrcSpanAnnN
    
    97
    -type instance Anno Id      = SrcSpanAnnN
    
    95
    +--type instance Anno RdrName = SrcSpanAnnN
    
    96
    +--type instance Anno Name    = SrcSpanAnnN
    
    97
    +--type instance Anno Id      = SrcSpanAnnN
    
    98 98
     
    
    99 99
     type instance Anno (WithUserRdr a) = Anno a
    
    100 100
     
    
    101
    +{-
    
    101 102
     type IsSrcSpanAnn p a = ( Anno (IdGhcP p) ~ EpAnn a,
    
    102 103
                               Anno (IdOccGhcP p) ~ EpAnn a,
    
    103 104
                               NoAnn a,
    
    104 105
                               IsPass p)
    
    106
    +-}
    
    105 107
     
    
    106 108
     instance UnXRec (GhcPass p) where
    
    107 109
       unXRec = unLoc
    

  • compiler/GHC/Hs/ImpExp.hs
    ... ... @@ -17,6 +17,7 @@ module GHC.Hs.ImpExp
    17 17
         , module GHC.Hs.ImpExp
    
    18 18
         ) where
    
    19 19
     
    
    20
    +import Language.Haskell.Syntax.Doc
    
    20 21
     import Language.Haskell.Syntax.Extension
    
    21 22
     import Language.Haskell.Syntax.Module.Name
    
    22 23
     import Language.Haskell.Syntax.ImpExp
    
    ... ... @@ -39,7 +40,6 @@ import GHC.Unit.Module.Warnings
    39 40
     
    
    40 41
     import Data.Data
    
    41 42
     import Data.Maybe
    
    42
    -import GHC.Hs.Doc (LHsDoc)
    
    43 43
     
    
    44 44
     
    
    45 45
     {-
    
    ... ... @@ -144,6 +144,10 @@ simpleImportDecl mn = ImportDecl {
    144 144
         }
    
    145 145
     
    
    146 146
     instance (OutputableBndrId p
    
    147
    +         , Outputable
    
    148
    +             (GenLocated
    
    149
    +               (Anno (WithHsDocIdentifiers (HsDocString (GhcPass p)) (GhcPass p)))
    
    150
    +               (WithHsDocIdentifiers (HsDocString (GhcPass p)) (GhcPass p)))
    
    147 151
              , Outputable (Anno (IE (GhcPass p)))
    
    148 152
              , Outputable (ImportDeclPkgQual (GhcPass p)))
    
    149 153
            => Outputable (ImportDecl (GhcPass p)) where
    
    ... ... @@ -353,10 +357,10 @@ replaceWrappedName (IEData r (L l _)) n = IEData r (L l n)
    353 357
     replaceLWrappedName :: LIEWrappedName GhcPs -> IdP GhcRn -> LIEWrappedName GhcRn
    
    354 358
     replaceLWrappedName (L l n) n' = L l (replaceWrappedName n n')
    
    355 359
     
    
    356
    -exportDocstring :: LHsDoc pass -> SDoc
    
    360
    +exportDocstring :: Outputable (LHsDoc (GhcPass p)) => LHsDoc (GhcPass p) -> SDoc
    
    357 361
     exportDocstring doc = braces (text "docstring: " <> ppr doc)
    
    358 362
     
    
    359
    -instance OutputableBndrId p => Outputable (IE (GhcPass p)) where
    
    363
    +instance (OutputableBndrId p, Outputable (LHsDoc (GhcPass p)))  => Outputable (IE (GhcPass p)) where
    
    360 364
         ppr ie@(IEVar       _     var doc) =
    
    361 365
           sep $ catMaybes [ ppr <$> ieDeprecation ie
    
    362 366
                           , Just $ ppr (unLoc var)
    

  • compiler/GHC/Hs/Instances.hs
    ... ... @@ -35,6 +35,7 @@ import GHC.Data.BooleanFormula (BooleanFormula(..))
    35 35
     import Language.Haskell.Syntax.Decls
    
    36 36
     import Language.Haskell.Syntax.Decls.Foreign (CType(..), Header(..))
    
    37 37
     import Language.Haskell.Syntax.Decls.Overlap (OverlapMode(..))
    
    38
    +import Language.Haskell.Syntax.Doc (HsDocString(..), WithHsDocIdentifiers(..))
    
    38 39
     import Language.Haskell.Syntax.Extension (Anno)
    
    39 40
     import Language.Haskell.Syntax.Binds.InlinePragma (ActivationX(..), InlinePragma(..))
    
    40 41
     
    
    ... ... @@ -629,9 +630,9 @@ deriving instance Eq (IEWholeNamespaceExt GhcRn)
    629 630
     deriving instance Eq (IEWholeNamespaceExt GhcTc)
    
    630 631
     
    
    631 632
     -- deriving instance (DataId name)             => Data (IE name)
    
    632
    -deriving instance Data (IE GhcPs)
    
    633
    -deriving instance Data (IE GhcRn)
    
    634
    -deriving instance Data (IE GhcTc)
    
    633
    +deriving instance Data (Anno (WithHsDocIdentifiers (HsDocString GhcPs) GhcPs)) => Data (IE GhcPs)
    
    634
    +deriving instance Data (Anno (WithHsDocIdentifiers (HsDocString GhcRn) GhcRn)) => Data (IE GhcRn)
    
    635
    +deriving instance Data (Anno (WithHsDocIdentifiers (HsDocString GhcTc) GhcTc)) => Data (IE GhcTc)
    
    635 636
     
    
    636 637
     -- deriving instance (Eq name, Eq (IdP name)) => Eq (IE name)
    
    637 638
     deriving instance Eq (IE GhcPs)
    
    ... ... @@ -669,3 +670,13 @@ deriving instance Data (InlinePragma GhcTc)
    669 670
     deriving instance Data (OverlapMode GhcPs)
    
    670 671
     deriving instance Data (OverlapMode GhcRn)
    
    671 672
     deriving instance Data (OverlapMode GhcTc)
    
    673
    +
    
    674
    +
    
    675
    +-- deriving instance Data (HsDocString p)
    
    676
    +deriving instance Data (HsDocString GhcPs)
    
    677
    +deriving instance Data (HsDocString GhcRn)
    
    678
    +deriving instance Data (HsDocString GhcTc)
    
    679
    +
    
    680
    +deriving instance Data a => Data (WithHsDocIdentifiers a GhcPs)
    
    681
    +deriving instance Data a => Data (WithHsDocIdentifiers a GhcRn)
    
    682
    +deriving instance Data a => Data (WithHsDocIdentifiers a GhcTc)

  • compiler/GHC/Hs/Lit.hs
    ... ... @@ -26,6 +26,7 @@ import GHC.Types.Basic (PprPrec(..), topPrec )
    26 26
     import GHC.Core.Ppr ( {- instance OutputableBndr TyVar -} )
    
    27 27
     import GHC.Types.SourceText
    
    28 28
     import GHC.Core.Type
    
    29
    +import GHC.Parser.Annotation ( {- type instance Anno Id -} )
    
    29 30
     import GHC.Utils.Misc (split)
    
    30 31
     import GHC.Utils.Outputable
    
    31 32
     import GHC.Utils.Panic (panic)
    

  • compiler/GHC/Iface/Syntax.hs
    ... ... @@ -668,7 +668,7 @@ fromIfaceWarningTxt = \case
    668 668
         IfDeprecatedTxt src strs -> DeprecatedTxt src (noLocA <$> map fromIfaceStringLiteralWithNames strs)
    
    669 669
     
    
    670 670
     fromIfaceStringLiteralWithNames :: (IfaceStringLiteral, [IfExtName]) -> WithHsDocIdentifiers StringLiteral GhcRn
    
    671
    -fromIfaceStringLiteralWithNames (str, names) = WithHsDocIdentifiers (fromIfaceStringLiteral str) (map noLoc names)
    
    671
    +fromIfaceStringLiteralWithNames (str, names) = WithHsDocIdentifiers (fromIfaceStringLiteral str) (map noLocA names)
    
    672 672
     
    
    673 673
     fromIfaceStringLiteral :: IfaceStringLiteral -> StringLiteral
    
    674 674
     fromIfaceStringLiteral (IfStringLiteral st fs) = StringLiteral st fs Nothing
    

  • compiler/GHC/Parser/Annotation.hs
    1
    +{-# LANGUAGE TypeFamilies #-}
    
    2
    +
    
    1 3
     module GHC.Parser.Annotation (
    
    2 4
       -- * Core Exact Print Annotation types
    
    3 5
       EpToken(..), EpUniToken(..),
    
    ... ... @@ -32,6 +34,7 @@ module GHC.Parser.Annotation (
    32 34
       SrcSpanAnnA, SrcSpanAnnL, SrcSpanAnnP, SrcSpanAnnC, SrcSpanAnnN,
    
    33 35
       SrcSpanAnnLC, SrcSpanAnnLW, SrcSpanAnnLS, SrcSpanAnnLI,
    
    34 36
       LocatedE,
    
    37
    +  IsSrcSpanAnn,
    
    35 38
     
    
    36 39
       -- ** Annotation data types used in 'GenLocated'
    
    37 40
     
    
    ... ... @@ -94,17 +97,34 @@ import Data.Data
    94 97
     import Data.Function (on)
    
    95 98
     import Data.List (sortBy)
    
    96 99
     import Data.Semigroup
    
    100
    +import Data.Void
    
    97 101
     import GHC.Data.FastString
    
    98 102
     import GHC.TypeLits (Symbol, KnownSymbol, symbolVal)
    
    99 103
     import GHC.Types.Name
    
    104
    +import GHC.Types.Name.Reader (RdrName)
    
    100 105
     import GHC.Types.SrcLoc
    
    101
    -import GHC.Hs.DocString
    
    106
    +--import GHC.Hs.DocString
    
    107
    +import GHC.Types.Var (Id)
    
    102 108
     import GHC.Utils.Misc
    
    103 109
     import GHC.Utils.Outputable hiding ( (<>) )
    
    104 110
     import GHC.Utils.Panic
    
    105 111
     import qualified GHC.Data.Strict as Strict
    
    106 112
     import GHC.Types.SourceText (SourceText (NoSourceText))
    
    107 113
     
    
    114
    +import GHC.Hs.Extension
    
    115
    +
    
    116
    +import Language.Haskell.Syntax.Doc
    
    117
    +import Language.Haskell.Syntax.Extension (Anno)
    
    118
    +
    
    119
    +type instance Anno Id      = SrcSpanAnnN
    
    120
    +type instance Anno Name    = SrcSpanAnnN
    
    121
    +type instance Anno RdrName = SrcSpanAnnN
    
    122
    +
    
    123
    +type IsSrcSpanAnn p a = ( Anno (IdGhcP p) ~ EpAnn a,
    
    124
    +                          Anno (IdOccGhcP p) ~ EpAnn a,
    
    125
    +                          NoAnn a,
    
    126
    +                          IsPass p)
    
    127
    +
    
    108 128
     {-
    
    109 129
     Note [exact print annotations]
    
    110 130
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -308,18 +328,46 @@ data EpaComment =
    308 328
         -- and the start of this location is used for the spacing when
    
    309 329
         -- exact printing the comment.
    
    310 330
         }
    
    311
    -    deriving (Eq, Data, Show)
    
    331
    +    deriving (Eq, Show)
    
    332
    +
    
    333
    +instance Data EpaComment where
    
    334
    +  gunfold _ _ _ = undefined
    
    335
    +  toConstr = undefined
    
    336
    +  dataTypeOf = undefined
    
    312 337
     
    
    313 338
     data EpaCommentTok =
    
    314 339
       -- Documentation annotations
    
    315
    -    EpaDocComment      HsDocString -- ^ a docstring that can be pretty printed using pprHsDocString
    
    340
    +    EpaDocComment (HsDocString Void) -- ^ a docstring that can be pretty printed using pprHsDocString
    
    316 341
       | EpaDocOptions      String     -- ^ doc options (prune, ignore-exports, etc)
    
    317 342
       | EpaLineComment     String     -- ^ comment starting by "--"
    
    318 343
       | EpaBlockComment    String     -- ^ comment in {- -}
    
    319
    -    deriving (Eq, Data, Show)
    
    344
    +    deriving (Eq, Show)
    
    345
    +    -- TODO: add back the Data and Show instance
    
    320 346
     -- Note: these are based on the Token versions, but the Token type is
    
    321 347
     -- defined in GHC.Parser.Lexer and bringing it in here would create a loop
    
    322 348
     
    
    349
    +instance {-# OVERLAPPING #-} Eq (HsDocString Void) where
    
    350
    +  (==) = \case
    
    351
    +       MultiLineDocString   _ a1 b1 -> \case
    
    352
    +         MultiLineDocString _ a2 b2 -> a1 == a2 && length b1 == length b2
    
    353
    +         _ -> False
    
    354
    +       NestedDocString    _ a1 b1 -> \case
    
    355
    +         NestedDocString  _ a2 b2 -> a1 == a2
    
    356
    +         _ -> False
    
    357
    +       GeneratedDocString   _ a1 -> \case
    
    358
    +         GeneratedDocString _ a2 -> a1 == a2
    
    359
    +         _ -> False
    
    360
    +       XHsDocString _ -> \case
    
    361
    +         XHsDocString _ -> True
    
    362
    +         _ -> False
    
    363
    +
    
    364
    +instance {-# OVERLAPPING #-} Show (HsDocString Void) where
    
    365
    +  show = \case
    
    366
    +    MultiLineDocString _ a b -> unwords ["MultiLineDocString", show a ]
    
    367
    +    NestedDocString    _ a b -> unwords ["NestedDocString"   , show a ]
    
    368
    +    GeneratedDocString _ a   -> unwords ["GeneratedDocString", show a ]
    
    369
    +    XHsDocString       _     -> "XHsDocString"
    
    370
    +
    
    323 371
     instance Outputable EpaComment where
    
    324 372
       ppr x = text (show x)
    
    325 373
     
    
    ... ... @@ -390,7 +438,6 @@ data EpAnn ann
    390 438
             deriving (Data, Eq, Functor)
    
    391 439
     -- See Note [XRec and Anno in the AST]
    
    392 440
     
    
    393
    -
    
    394 441
     spanAsAnchor :: SrcSpan -> (EpaLocation' a)
    
    395 442
     spanAsAnchor ss  = EpaSpan ss
    
    396 443
     
    
    ... ... @@ -537,7 +584,7 @@ data AnnList a
    537 584
           al_trailing  :: ![TrailingAnn] -- ^ items appearing after the
    
    538 585
                                          -- list, such as '=>' for a
    
    539 586
                                          -- context
    
    540
    -      } deriving (Data,Eq)
    
    587
    +      } deriving (Data, Eq)
    
    541 588
     
    
    542 589
     data AnnListBrackets
    
    543 590
       = ListParens (EpToken "(")         (EpToken ")")
    
    ... ... @@ -570,7 +617,6 @@ data AnnContext
    570 617
           ac_close     :: [EpToken ")"]  -- ^ zero or more closing parentheses.
    
    571 618
           } deriving (Data)
    
    572 619
     
    
    573
    -
    
    574 620
     -- ---------------------------------------------------------------------
    
    575 621
     -- Annotations for names
    
    576 622
     -- ---------------------------------------------------------------------
    
    ... ... @@ -648,7 +694,7 @@ data AnnPragma
    648 694
           apr_loc2      :: EpaLocation,
    
    649 695
           apr_type      :: EpToken "type",
    
    650 696
           apr_module    :: EpToken "module"
    
    651
    -      } deriving (Data,Eq)
    
    697
    +      } deriving (Data, Eq)
    
    652 698
     
    
    653 699
     -- ---------------------------------------------------------------------
    
    654 700
     
    

  • compiler/GHC/Types/Name/Reader.hs
    ... ... @@ -150,6 +150,10 @@ import qualified Data.Map.Strict as Map
    150 150
     import qualified Data.Semigroup as S
    
    151 151
     import System.IO.Unsafe ( unsafePerformIO )
    
    152 152
     
    
    153
    +import Language.Haskell.Syntax.Extension
    
    154
    +
    
    155
    +
    
    156
    +
    
    153 157
     {-
    
    154 158
     ************************************************************************
    
    155 159
     *                                                                      *
    

  • compiler/GHC/Utils/Binary.hs
    ... ... @@ -103,7 +103,7 @@ module GHC.Utils.Binary
    103 103
        getGenericSymtab, putGenericSymTab,
    
    104 104
        getGenericSymbolTable, putGenericSymbolTable,
    
    105 105
        -- * Newtype wrappers
    
    106
    -   BinSpan(..), BinSrcSpan(..), BinLocated(..),
    
    106
    +   BinSpan(..), BinSrcSpan(..), BinLocated(..), BinGenLocated(..),
    
    107 107
        -- * Newtypes for types that have canonically more than one valid encoding
    
    108 108
        BindingName(..),
    
    109 109
        simpleBindingNameWriter,
    
    ... ... @@ -121,6 +121,7 @@ import Language.Haskell.Syntax.Basic
    121 121
     import Language.Haskell.Syntax.Binds.InlinePragma
    
    122 122
     import Language.Haskell.Syntax.Module.Name (ModuleName(..))
    
    123 123
     import Language.Haskell.Syntax.ImpExp.IsBoot (IsBootInterface(..))
    
    124
    +import Language.Haskell.Syntax.UTF8
    
    124 125
     
    
    125 126
     import {-# SOURCE #-} GHC.Types.Name (Name)
    
    126 127
     import GHC.Data.FastString
    
    ... ... @@ -1905,6 +1906,18 @@ instance Binary a => Binary (BinLocated a) where
    1905 1906
                 x <- get bh
    
    1906 1907
                 return $ BinLocated (L l x)
    
    1907 1908
     
    
    1909
    +newtype BinGenLocated l a = BinGenLocated { unBinGenLocated :: GenLocated l a }
    
    1910
    +
    
    1911
    +instance (Binary a, Binary l) => Binary (BinGenLocated l a) where
    
    1912
    +    put_ bh (BinGenLocated (L l x)) = do
    
    1913
    +            put_ bh l
    
    1914
    +            put_ bh x
    
    1915
    +
    
    1916
    +    get bh = do
    
    1917
    +            l <- get bh
    
    1918
    +            x <- get bh
    
    1919
    +            return $ BinGenLocated (L l x)
    
    1920
    +
    
    1908 1921
     newtype BinSpan = BinSpan { unBinSpan :: RealSrcSpan }
    
    1909 1922
     
    
    1910 1923
     -- See Note [Source Location Wrappers]
    
    ... ... @@ -2097,3 +2110,8 @@ instance Binary RuleMatchInfo where
    2097 2110
           h <- getByte bh
    
    2098 2111
           if h == 1 then pure ConLike
    
    2099 2112
                     else pure FunLike
    
    2113
    +
    
    2114
    +instance Binary TextUTF8 where
    
    2115
    +  put_ bh = putSBS bh . bytesUTF8
    
    2116
    +
    
    2117
    +  get = fmap unsafeFromShortByteString . getSBS

  • compiler/GHC/Utils/Outputable.hs
    ... ... @@ -116,6 +116,7 @@ import Language.Haskell.Syntax.Basic
    116 116
     import Language.Haskell.Syntax.Binds.InlinePragma
    
    117 117
     import Language.Haskell.Syntax.Decls.Overlap ( OverlapMode(..) )
    
    118 118
     import Language.Haskell.Syntax.Module.Name ( ModuleName(..) )
    
    119
    +import Language.Haskell.Syntax.UTF8
    
    119 120
     
    
    120 121
     import GHC.Prelude.Basic
    
    121 122
     
    
    ... ... @@ -2036,3 +2037,6 @@ instance Outputable (OverlapMode p) where
    2036 2037
       ppr (Incoherent   _) = text "[incoherent]"
    
    2037 2038
       ppr (NonCanonical _) = text "[noncanonical]"
    
    2038 2039
       ppr (XOverlapMode _) = text "[user TTG extension]"
    
    2040
    +
    
    2041
    +instance Outputable TextUTF8 where
    
    2042
    +  ppr = text . decodeUTF8

  • compiler/Language/Haskell/Syntax/Decls.hs
    ... ... @@ -1410,7 +1410,7 @@ data DocDecl pass
    1410 1410
       | DocCommentNamed String (LHsDoc pass)
    
    1411 1411
       | DocGroup Int (LHsDoc pass)
    
    1412 1412
     
    
    1413
    -deriving instance (Data pass, Data (IdP pass)) => Data (DocDecl pass)
    
    1413
    +--deriving instance (Data pass, Data (IdP pass)) => Data (DocDecl pass)
    
    1414 1414
     
    
    1415 1415
     docDeclDoc :: DocDecl pass -> LHsDoc pass
    
    1416 1416
     docDeclDoc (DocCommentNext d) = d
    

  • compiler/Language/Haskell/Syntax/Doc.hs
    1
    +{-# LANGUAGE LambdaCase #-}
    
    2
    +{-# LANGUAGE TypeFamilies #-}
    
    3
    +{-# LANGUAGE UndecidableInstances #-} -- Eq XOverlapMode, NFData OverlapMode
    
    4
    +
    
    5
    +{-
    
    6
    +## Migrate and restructure `LHsDoc`
    
    7
    +
    
    8
    +[X] 1. Create a new **`L.H.S.Doc`** module and move the following into it.
    
    9
    +[X] 2. Move `HsDoc` *(no change)*
    
    10
    +[X] 3. Move `HsDocStringChunk` *(no change)*
    
    11
    +[X] 4. Move `HsDocStringDecorator` *(no change)*
    
    12
    +[_] 5. Move `LHsDoc p` as `XRec p (HsDoc p)`
    
    13
    +[X] 6. Move `WithHsDocIdentifiers p` as `Located (IdP p) as LIdP p`
    
    14
    +[_] 7. Move `HsDocString`, adding TTG parameter and extension point
    
    15
    +[X] 8. Move `LHsDocStringChunk = Located HsDocStringChunk` as `type LHsDocStringChunk pass = XRec pass HsDocStringChunk`
    
    16
    +[ ] 9. Add `type instance Anno HsDocStringChunk = SrcSpan`
    
    17
    +-}
    
    18
    +
    
    19
    +{- |
    
    20
    +Data-types describing the raw and lexical docstrings of
    
    21
    +the Haskell programming language.
    
    22
    +-}
    
    23
    +module Language.Haskell.Syntax.Doc
    
    24
    +  ( HsDoc
    
    25
    +  , WithHsDocIdentifiers(..)
    
    26
    +
    
    27
    +  , HsDocString(..)
    
    28
    +  -- ** Construcction
    
    29
    +  , mkGeneratedHsDocString
    
    30
    +
    
    31
    +  , HsDocStringChunk(..)
    
    32
    +  -- ** Construction
    
    33
    +  , mkHsDocStringChunk
    
    34
    +  , mkHsDocStringChunkUtf8ByteString
    
    35
    +  -- ** Deconstruction
    
    36
    +  , unpackHDSC
    
    37
    +  -- ** Query
    
    38
    +  , nullHDSC
    
    39
    +
    
    40
    +  , HsDocStringDecorator(..)
    
    41
    +  , LHsDoc
    
    42
    +  , LHsDocStringChunk
    
    43
    +  ) where
    
    44
    +
    
    45
    +import Control.DeepSeq
    
    46
    +import Data.ByteString (ByteString)
    
    47
    +import qualified Data.ByteString.Short as SBS
    
    48
    +import Data.Data
    
    49
    +import Data.Kind (Type)
    
    50
    +import Data.Eq
    
    51
    +import Data.List.NonEmpty (NonEmpty(..))
    
    52
    +import Data.Function
    
    53
    +import Prelude
    
    54
    +import Language.Haskell.Syntax.Extension
    
    55
    +import Language.Haskell.Syntax.UTF8
    
    56
    +
    
    57
    +-- | A docstring with the (probable) identifiers found in it.
    
    58
    +type HsDoc (pass :: Type) = WithHsDocIdentifiers (HsDocString pass) pass
    
    59
    +
    
    60
    +-- | Haskell Documentation String
    
    61
    +--
    
    62
    +-- Rich structure to support exact printing
    
    63
    +-- The location around each chunk doesn't include the decorators
    
    64
    +data HsDocString pass
    
    65
    +  = MultiLineDocString
    
    66
    +      !(XMultiLineDocString pass)
    
    67
    +      !HsDocStringDecorator
    
    68
    +      !(NonEmpty (LHsDocStringChunk pass))
    
    69
    +     -- ^ The first chunk is preceded by "-- <decorator>" and each following chunk is preceded by "--"
    
    70
    +     -- Example: -- | This is a docstring for 'foo'. It is the line with the decorator '|' and is always included
    
    71
    +     --          -- This continues that docstring and is the second element in the NonEmpty list
    
    72
    +     --          foo :: a -> a
    
    73
    +  | NestedDocString
    
    74
    +      !(XNestedDocString pass)
    
    75
    +      !HsDocStringDecorator
    
    76
    +      (LHsDocStringChunk pass)
    
    77
    +     -- ^ The docstring is preceded by "{-<decorator>" and followed by "-}"
    
    78
    +     -- The chunk contains balanced pairs of '{-' and '-}'
    
    79
    +  | GeneratedDocString
    
    80
    +      !(XGeneratedDocString pass)
    
    81
    +      HsDocStringChunk
    
    82
    +     -- ^ A docstring generated either internally or via TH
    
    83
    +     -- Pretty printed with the '-- |' decorator
    
    84
    +     -- This is because it may contain unbalanced pairs of '{-' and '-}' and
    
    85
    +     -- not form a valid 'NestedDocString'
    
    86
    +  | XHsDocString
    
    87
    +      !(XXHsDocString pass)
    
    88
    +{-
    
    89
    +deriving stock instance (
    
    90
    +  Eq (XMultiLineDocString pass),
    
    91
    +  Eq (XNestedDocString pass),
    
    92
    +  Eq (XGeneratedDocString pass),
    
    93
    +  Eq (XXHsDocString pass),
    
    94
    +  Eq (XRec pass HsDocStringChunk),
    
    95
    +  Typeable pass
    
    96
    +  ) => Eq (HsDocString pass)
    
    97
    +
    
    98
    +deriving stock instance (
    
    99
    +  Show (XMultiLineDocString pass),
    
    100
    +  Show (XNestedDocString pass),
    
    101
    +  Show (XGeneratedDocString pass),
    
    102
    +  Show (XXHsDocString pass),
    
    103
    +  Show (XRec pass HsDocStringChunk),
    
    104
    +  Typeable pass
    
    105
    +  ) => Show (HsDocString pass)
    
    106
    +
    
    107
    +instance {-# OVERLAPPABLE #-} (
    
    108
    +  NFData (XMultiLineDocString pass),
    
    109
    +  NFData (XNestedDocString pass),
    
    110
    +  NFData (XGeneratedDocString pass),
    
    111
    +  NFData (XXHsDocString pass),
    
    112
    +  NFData (XRec pass HsDocStringChunk)
    
    113
    +  ) => NFData (HsDocString pass) where
    
    114
    +    rnf = \case
    
    115
    +       MultiLineDocString x a b -> rnf x `seq` rnf a `seq` rnf b
    
    116
    +       NestedDocString    x a b -> rnf x `seq` rnf a `seq` rnf b
    
    117
    +       GeneratedDocString x a   -> rnf x `seq` rnf a
    
    118
    +       XHsDocString       x     -> rnf x
    
    119
    +-}
    
    120
    +mkGeneratedHsDocString :: XGeneratedDocString p -> String -> HsDocString p
    
    121
    +mkGeneratedHsDocString x = GeneratedDocString x . mkHsDocStringChunk
    
    122
    +
    
    123
    +type LHsDoc pass = XRec pass (HsDoc pass)
    
    124
    +--type LHsDoc pass = Located (HsDoc pass)
    
    125
    +--type LIdP p = XRec p (IdP p)
    
    126
    +
    
    127
    +type LHsDocStringChunk pass = XRec pass HsDocStringChunk
    
    128
    +
    
    129
    +-- | A contiguous chunk of documentation
    
    130
    +newtype HsDocStringChunk = HsDocStringChunk TextUTF8
    
    131
    +  deriving stock (Eq,Ord,Data, Show)
    
    132
    +  deriving newtype (NFData)
    
    133
    +
    
    134
    +mkHsDocStringChunk :: String -> HsDocStringChunk
    
    135
    +mkHsDocStringChunk = HsDocStringChunk . encodeUTF8
    
    136
    +
    
    137
    +mkHsDocStringChunkUtf8ByteString :: ByteString -> HsDocStringChunk
    
    138
    +mkHsDocStringChunkUtf8ByteString =
    
    139
    +  HsDocStringChunk . unsafeFromShortByteString . SBS.toShort
    
    140
    +
    
    141
    +unpackHDSC :: HsDocStringChunk -> String
    
    142
    +unpackHDSC (HsDocStringChunk bs) = decodeUTF8 bs
    
    143
    +
    
    144
    +nullHDSC :: HsDocStringChunk -> Bool
    
    145
    +nullHDSC (HsDocStringChunk bs) = headUTF8 bs == Nothing
    
    146
    +
    
    147
    +data HsDocStringDecorator
    
    148
    +  = HsDocStringNext          -- ^ '|' is the decorator
    
    149
    +  | HsDocStringPrevious      -- ^ '^' is the decorator
    
    150
    +  | HsDocStringNamed !String -- ^ '$<string>' is the decorator
    
    151
    +  | HsDocStringGroup !Int    -- ^ The decorator is the given number of '*'s
    
    152
    +  deriving (Eq, Ord, Show, Data)
    
    153
    +
    
    154
    +instance NFData HsDocStringDecorator where
    
    155
    +  rnf HsDocStringNext = ()
    
    156
    +  rnf HsDocStringPrevious = ()
    
    157
    +  rnf (HsDocStringNamed x) = rnf x
    
    158
    +  rnf (HsDocStringGroup x) = rnf x
    
    159
    +
    
    160
    +-- | Annotate a value with the probable identifiers found in it
    
    161
    +-- These will be used by haddock to generate links.
    
    162
    +--
    
    163
    +-- The identifiers are bundled along with their location in the source file.
    
    164
    +-- This is useful for tooling to know exactly where they originate.
    
    165
    +--
    
    166
    +-- This type is currently used in two places - for regular documentation comments,
    
    167
    +-- with 'a' set to 'HsDocString', and for adding identifier information to
    
    168
    +-- warnings, where 'a' is 'StringLiteral'
    
    169
    +data WithHsDocIdentifiers a pass = WithHsDocIdentifiers
    
    170
    +  { hsDocString      :: !a
    
    171
    +  , hsDocIdentifiers :: ![LIdP pass]
    
    172
    +  }

  • compiler/Language/Haskell/Syntax/Extension.hs
    ... ... @@ -646,6 +646,13 @@ type family XXLit x
    646 646
     type family XOverLit  x
    
    647 647
     type family XXOverLit x
    
    648 648
     
    
    649
    +-- -------------------------------------
    
    650
    +-- Type families for the HsDocString extension points
    
    651
    +type family XMultiLineDocString x
    
    652
    +type family XNestedDocString x
    
    653
    +type family XGeneratedDocString x
    
    654
    +type family XXHsDocString x
    
    655
    +
    
    649 656
     -- =====================================================================
    
    650 657
     -- Type families for the HsPat extension points
    
    651 658
     
    

  • compiler/Language/Haskell/Syntax/ImpExp.hs
    1 1
     {-# LANGUAGE TypeFamilies #-}
    
    2 2
     module Language.Haskell.Syntax.ImpExp ( module Language.Haskell.Syntax.ImpExp, IsBootInterface(..) ) where
    
    3 3
     
    
    4
    +import Language.Haskell.Syntax.Doc
    
    4 5
     import Language.Haskell.Syntax.Extension
    
    5 6
     import Language.Haskell.Syntax.Module.Name
    
    6 7
     import Language.Haskell.Syntax.ImpExp.IsBoot ( IsBootInterface(..) )
    
    ... ... @@ -13,7 +14,7 @@ import Data.String (String)
    13 14
     import Data.Int (Int)
    
    14 15
     
    
    15 16
     import Control.DeepSeq
    
    16
    -import {-# SOURCE #-} GHC.Hs.Doc (LHsDoc) -- ROMES:TODO Discuss in #21592 whether this is parsed AST or base AST
    
    17
    +--import {-# SOURCE #-} GHC.Hs.Doc (LHsDoc) -- ROMES:TODO Discuss in #21592 whether this is parsed AST or base AST
    
    17 18
     
    
    18 19
     {-
    
    19 20
     ************************************************************************
    

  • compiler/Language/Haskell/Syntax/UTF8.hs
    1
    +{-# LANGUAGE MagicHash #-}
    
    2
    +{-# LANGUAGE UnboxedTuples #-}
    
    3
    +
    
    4
    +{- |
    
    5
    +Represents a small chunk of UTF8 text from a source code file.
    
    6
    +-}
    
    7
    +module Language.Haskell.Syntax.UTF8
    
    8
    +   (
    
    9
    +   -- * Data-type
    
    10
    +     TextUTF8()
    
    11
    +   -- ** Construction
    
    12
    +   , encodeUTF8
    
    13
    +   , unsafeFromShortByteString
    
    14
    +   -- ** Deconstruction
    
    15
    +   , bytesUTF8
    
    16
    +   , byteStringUTF8
    
    17
    +   , decodeUTF8
    
    18
    +   , headUTF8
    
    19
    +   -- ** Transformation
    
    20
    +   , linesUTF8
    
    21
    +   , unlinesUTF8
    
    22
    +   ) where
    
    23
    +
    
    24
    +
    
    25
    +import Prelude
    
    26
    +
    
    27
    +import Control.DeepSeq
    
    28
    +import Data.ByteString (StrictByteString)
    
    29
    +import Data.ByteString.Short (ShortByteString(..))
    
    30
    +import qualified Data.ByteString.Short as SBS
    
    31
    +import Data.Data
    
    32
    +import Data.Foldable (toList)
    
    33
    +import Data.String (IsString(..))
    
    34
    +import Data.Word (Word8)
    
    35
    +
    
    36
    +-- These is a modules are components of the @base@ package,
    
    37
    +-- hence they do not directly couple the library to GHC.
    
    38
    +import GHC.Base (Char(C#))
    
    39
    +import GHC.Encoding.UTF8
    
    40
    +
    
    41
    +{- |
    
    42
    +A UTF8 encoded ShortByteString representing the textual snippet of code
    
    43
    +associated with a given element of the abstract syntax tree.
    
    44
    +-}
    
    45
    +newtype TextUTF8 = TextUTF8 { bytesUTF8 :: ShortByteString }
    
    46
    +    deriving (Data, Eq, Ord)
    
    47
    +
    
    48
    +instance IsString TextUTF8 where
    
    49
    +  fromString = encodeUTF8
    
    50
    +
    
    51
    +instance NFData TextUTF8 where
    
    52
    +  rnf (TextUTF8 !sbs) = rnf sbs
    
    53
    +
    
    54
    +instance Semigroup TextUTF8 where
    
    55
    +  (TextUTF8 x) <> (TextUTF8 y) = TextUTF8 $ x <> y
    
    56
    +
    
    57
    +instance Monoid TextUTF8 where
    
    58
    +  mempty = TextUTF8 mempty
    
    59
    +
    
    60
    +instance Show TextUTF8 where
    
    61
    +  show (TextUTF8 sbs) = utf8DecodeShortByteString sbs
    
    62
    +
    
    63
    +{- |
    
    64
    +Convert the UTF8 chunk of text to a 'ByteString'.
    
    65
    +
    
    66
    +_Time:_ $\mathcal{O}\left( n \right )$
    
    67
    +-}
    
    68
    +byteStringUTF8 :: TextUTF8 -> StrictByteString
    
    69
    +byteStringUTF8 (TextUTF8 sbs) = SBS.fromShort sbs
    
    70
    +
    
    71
    +{- |
    
    72
    +Decode a UTF8 chunk of text to a 'String'.
    
    73
    +-}
    
    74
    +{-# INLINE decodeUTF8 #-}
    
    75
    +decodeUTF8 :: TextUTF8 -> String
    
    76
    +decodeUTF8 = utf8DecodeShortByteString . bytesUTF8
    
    77
    +
    
    78
    +{- |
    
    79
    +Encode a 'String' as a UTF8 chunk of text.
    
    80
    +-}
    
    81
    +{-# INLINE encodeUTF8 #-}
    
    82
    +encodeUTF8 :: String -> TextUTF8
    
    83
    +encodeUTF8 = TextUTF8 . utf8EncodeShortByteString
    
    84
    +
    
    85
    +{- |
    
    86
    +Extract the first code point from the UTF8 check of text.
    
    87
    +
    
    88
    +_Time:_ $\mathcal{O}\left( 1 \right )$
    
    89
    +-}
    
    90
    +headUTF8 :: TextUTF8 -> Maybe Char
    
    91
    +headUTF8 (TextUTF8 sbs@(SBS ba#))
    
    92
    +  | SBS.length sbs == 0 = Nothing
    
    93
    +  | otherwise =
    
    94
    +    let !(# c#, _ #) = utf8DecodeCharByteArray# ba# 0#
    
    95
    +    in  Just $ C# c#
    
    96
    +
    
    97
    +{- |
    
    98
    +Split a UTF8 fragment of text on newline characters (@'\n'@).
    
    99
    +-}
    
    100
    +linesUTF8 :: TextUTF8 -> [TextUTF8]
    
    101
    +linesUTF8 = fmap TextUTF8 . SBS.split newlineByte . bytesUTF8
    
    102
    +
    
    103
    +{- |
    
    104
    +Join a collection of UTF8 text fragments with newline characters (@'\n'@).
    
    105
    +-}
    
    106
    +unlinesUTF8 :: Foldable f => f TextUTF8 -> TextUTF8
    
    107
    +unlinesUTF8 =
    
    108
    +  TextUTF8 . SBS.intercalate (SBS.singleton newlineByte) . fmap bytesUTF8 . toList
    
    109
    +
    
    110
    +{- |
    
    111
    +Assumes that the shortByteString is already UTF8 encoded.
    
    112
    +
    
    113
    +/This precondition is not checked!/
    
    114
    +-}
    
    115
    +{-# INLINE unsafeFromShortByteString #-}
    
    116
    +unsafeFromShortByteString :: ShortByteString -> TextUTF8
    
    117
    +unsafeFromShortByteString = TextUTF8
    
    118
    +
    
    119
    +-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    
    120
    +-- Internal Functionality
    
    121
    +-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    
    122
    +
    
    123
    +newlineByte :: Word8
    
    124
    +newlineByte = 0x0A -- 0x0A (10) is the new line character (\n)
    
    125
    +
    
    126
    +utf8DecodeShortByteString :: ShortByteString -> [Char]
    
    127
    +utf8DecodeShortByteString (SBS ba#) = utf8DecodeByteArray# ba#
    
    128
    +
    
    129
    +utf8EncodeShortByteString :: String -> ShortByteString
    
    130
    +utf8EncodeShortByteString str = SBS (utf8EncodeByteArray# str)

  • compiler/ghc.cabal.in
    ... ... @@ -1028,6 +1028,7 @@ Library
    1028 1028
             Language.Haskell.Syntax.Decls
    
    1029 1029
             Language.Haskell.Syntax.Decls.Foreign
    
    1030 1030
             Language.Haskell.Syntax.Decls.Overlap
    
    1031
    +        Language.Haskell.Syntax.Doc
    
    1031 1032
             Language.Haskell.Syntax.Expr
    
    1032 1033
             Language.Haskell.Syntax.Extension
    
    1033 1034
             Language.Haskell.Syntax.ImpExp
    
    ... ... @@ -1037,6 +1038,7 @@ Library
    1037 1038
             Language.Haskell.Syntax.Pat
    
    1038 1039
             Language.Haskell.Syntax.Specificity
    
    1039 1040
             Language.Haskell.Syntax.Type
    
    1041
    +        Language.Haskell.Syntax.UTF8
    
    1040 1042
     
    
    1041 1043
         autogen-modules: GHC.Platform.Constants
    
    1042 1044
                          GHC.Settings.Config