    {-# LANGUAGE LambdaCase #-}
    {-# LANGUAGE OverloadedLabels #-}
    {-# LANGUAGE PatternSynonyms #-}
    {-# LANGUAGE RecordWildCards #-}
    {-# LANGUAGE ViewPatterns #-}

    -- | Rendering of declarations.
    module Ormolu.Printer.Meat.Declaration
      ( p_hsDecls,
        p_hsDeclsRespectGrouping,
      )
    where

    import Data.Choice (pattern Without)
    import Data.List (sort)
    import Data.List.NonEmpty (NonEmpty (..), (<|))
    import Data.List.NonEmpty qualified as NE
    import GHC.Hs
    import GHC.Types.Name.Occurrence (occNameFS)
    import GHC.Types.Name.Reader
    import GHC.Types.SrcLoc
    import Ormolu.Config (SourceType (SignatureSource))
    import Ormolu.Printer.Combinators
    import Ormolu.Printer.Meat.Common
    import Ormolu.Printer.Meat.Declaration.Annotation
    import Ormolu.Printer.Meat.Declaration.Class
    import Ormolu.Printer.Meat.Declaration.Data
    import Ormolu.Printer.Meat.Declaration.Default
    import Ormolu.Printer.Meat.Declaration.Foreign
    import Ormolu.Printer.Meat.Declaration.Instance
    import Ormolu.Printer.Meat.Declaration.RoleAnnotation
    import Ormolu.Printer.Meat.Declaration.Rule
    import Ormolu.Printer.Meat.Declaration.Signature
    import Ormolu.Printer.Meat.Declaration.Splice
    import Ormolu.Printer.Meat.Declaration.Type
    import Ormolu.Printer.Meat.Declaration.TypeFamily
    import Ormolu.Printer.Meat.Declaration.Value
    import Ormolu.Printer.Meat.Declaration.Warning
    import Ormolu.Printer.Meat.Type
    import Ormolu.Utils

    data UserGrouping
      = -- | Always put newlines where we think they should be
        Disregard
      | -- | Respect user preferences regarding grouping
        Respect
      deriving (UserGrouping -> UserGrouping -> Bool
    (UserGrouping -> UserGrouping -> Bool)
    -> (UserGrouping -> UserGrouping -> Bool) -> Eq UserGrouping
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: UserGrouping -> UserGrouping -> Bool
    == :: UserGrouping -> UserGrouping -> Bool
    $c/= :: UserGrouping -> UserGrouping -> Bool
    /= :: UserGrouping -> UserGrouping -> Bool
    Eq, Int -> UserGrouping -> ShowS
    [UserGrouping] -> ShowS
    UserGrouping -> String
    (Int -> UserGrouping -> ShowS)
    -> (UserGrouping -> String)
    -> ([UserGrouping] -> ShowS)
    -> Show UserGrouping
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> UserGrouping -> ShowS
    showsPrec :: Int -> UserGrouping -> ShowS
    $cshow :: UserGrouping -> String
    show :: UserGrouping -> String
    $cshowList :: [UserGrouping] -> ShowS
    showList :: [UserGrouping] -> ShowS
    Show)

    p_hsDecls :: FamilyStyle -> [LHsDecl GhcPs] -> R ()
    p_hsDecls :: FamilyStyle -> [LHsDecl GhcPs] -> R ()
    p_hsDecls = UserGrouping -> FamilyStyle -> [LHsDecl GhcPs] -> R ()
    p_hsDecls' UserGrouping
    Disregard

    -- | Like 'p_hsDecls' but respects user choices regarding grouping. If the
    -- user omits newlines between declarations, we also omit them in most
    -- cases, except when said declarations have associated Haddocks.
    --
    -- Does some normalization (compress subsequent newlines into a single one)
    p_hsDeclsRespectGrouping :: FamilyStyle -> [LHsDecl GhcPs] -> R ()
    p_hsDeclsRespectGrouping :: FamilyStyle -> [LHsDecl GhcPs] -> R ()
    p_hsDeclsRespectGrouping = UserGrouping -> FamilyStyle -> [LHsDecl GhcPs] -> R ()
    p_hsDecls' UserGrouping
    Respect

    p_hsDecls' :: UserGrouping -> FamilyStyle -> [LHsDecl GhcPs] -> R ()
    p_hsDecls' :: UserGrouping -> FamilyStyle -> [LHsDecl GhcPs] -> R ()
    p_hsDecls' UserGrouping
    grouping FamilyStyle
    style [LHsDecl GhcPs]
    decls = do
      isSig <- (SourceType -> SourceType -> Bool
    forall a. Eq a => a -> a -> Bool
    == SourceType
    SignatureSource) (SourceType -> Bool) -> R SourceType -> R Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> R SourceType
    askSourceType
      sepSemi id $
        -- Return a list of rendered declarations, adding a newline to separate
        -- groups.
        case groupDecls isSig decls of
          [] -> []
          (NonEmpty (LHsDecl GhcPs)
    x : [NonEmpty (LHsDecl GhcPs)]
    xs) -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs)) -> [R ()]
    renderGroup NonEmpty (LHsDecl GhcPs)
    NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    x [R ()] -> [R ()] -> [R ()]
    forall a. [a] -> [a] -> [a]
    ++ [[R ()]] -> [R ()]
    forall (t :: * -> *) a. Foldable t => t [a] -> [a]
    concat ((NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
     -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs)) -> [R ()])
    -> [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    -> [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    -> [[R ()]]
    forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
    zipWith NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs)) -> [R ()]
    renderGroupWithPrev (NonEmpty (LHsDecl GhcPs)
    NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    x NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    -> [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    forall a. a -> [a] -> [a]
    : [NonEmpty (LHsDecl GhcPs)]
    [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    xs) [NonEmpty (LHsDecl GhcPs)]
    [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    xs)
      where
        renderGroup :: NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs)) -> [R ()]
    renderGroup = NonEmpty (R ()) -> [R ()]
    forall a. NonEmpty a -> [a]
    NE.toList (NonEmpty (R ()) -> [R ()])
    -> (NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
        -> NonEmpty (R ()))
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> [R ()]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (GenLocated SrcSpanAnnA (HsDecl GhcPs) -> R ())
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> NonEmpty (R ())
    forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap ((HsDecl GhcPs -> R ())
    -> GenLocated SrcSpanAnnA (HsDecl GhcPs) -> R ()
    forall l a. HasLoc l => (a -> R ()) -> GenLocated l a -> R ()
    located' ((HsDecl GhcPs -> R ())
     -> GenLocated SrcSpanAnnA (HsDecl GhcPs) -> R ())
    -> (HsDecl GhcPs -> R ())
    -> GenLocated SrcSpanAnnA (HsDecl GhcPs)
    -> R ()
    forall a b. (a -> b) -> a -> b
    $ R () -> R ()
    dontUseBraces (R () -> R ()) -> (HsDecl GhcPs -> R ()) -> HsDecl GhcPs -> R ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . FamilyStyle -> HsDecl GhcPs -> R ()
    p_hsDecl FamilyStyle
    style)
        renderGroupWithPrev :: NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs)) -> [R ()]
    renderGroupWithPrev NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    prev NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    curr =
          -- We can omit a blank line when the user didn't add one, but we must
          -- ensure we always add blank lines around documented declarations
          case UserGrouping
    grouping of
            UserGrouping
    Disregard ->
              R ()
    declNewline R () -> [R ()] -> [R ()]
    forall a. a -> [a] -> [a]
    : NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs)) -> [R ()]
    renderGroup NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    curr
            UserGrouping
    Respect ->
              if (GenLocated SrcSpanAnnA (HsDecl GhcPs) -> SrcSpan)
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> Bool
    forall a. (a -> SrcSpan) -> NonEmpty a -> NonEmpty a -> Bool
    separatedByBlankNE GenLocated SrcSpanAnnA (HsDecl GhcPs) -> SrcSpan
    forall a e. HasLoc a => GenLocated a e -> SrcSpan
    getLocA NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    prev NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    curr
                Bool -> Bool -> Bool
    || NonEmpty (LHsDecl GhcPs) -> Bool
    isDocumented NonEmpty (LHsDecl GhcPs)
    NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    prev
                Bool -> Bool -> Bool
    || NonEmpty (LHsDecl GhcPs) -> Bool
    isDocumented NonEmpty (LHsDecl GhcPs)
    NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    curr
                then R ()
    declNewline R () -> [R ()] -> [R ()]
    forall a. a -> [a] -> [a]
    : NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs)) -> [R ()]
    renderGroup NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    curr
                else NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs)) -> [R ()]
    renderGroup NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    curr

    -- | Is a declaration group documented?
    isDocumented :: NonEmpty (LHsDecl GhcPs) -> Bool
    isDocumented :: NonEmpty (LHsDecl GhcPs) -> Bool
    isDocumented = (GenLocated SrcSpanAnnA (HsDecl GhcPs) -> Bool)
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs)) -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any (HsDecl GhcPs -> Bool
    isHaddock (HsDecl GhcPs -> Bool)
    -> (GenLocated SrcSpanAnnA (HsDecl GhcPs) -> HsDecl GhcPs)
    -> GenLocated SrcSpanAnnA (HsDecl GhcPs)
    -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated SrcSpanAnnA (HsDecl GhcPs) -> HsDecl GhcPs
    forall l e. GenLocated l e -> e
    unLoc)
      where
        isHaddock :: HsDecl GhcPs -> Bool
    isHaddock HsDecl GhcPs
    DocNext = Bool
    True
        isHaddock HsDecl GhcPs
    DocPrev = Bool
    True
        isHaddock HsDecl GhcPs
    _ = Bool
    False

    -- | Group relevant declarations together.
    groupDecls ::
      -- | Is the source a signature file?
      Bool ->
      -- | List of declarations
      [LHsDecl GhcPs] ->
      [NonEmpty (LHsDecl GhcPs)]
    groupDecls :: Bool -> [LHsDecl GhcPs] -> [NonEmpty (LHsDecl GhcPs)]
    groupDecls Bool
    _ [] = []
    groupDecls Bool
    isSig (l :: LHsDecl GhcPs
    l@(L SrcSpanAnnA
    _ HsDecl GhcPs
    DocNext) : [LHsDecl GhcPs]
    xs) =
      -- If the first element is a doc string for next element, just include it
      -- in the next block:
      case Bool -> [LHsDecl GhcPs] -> [NonEmpty (LHsDecl GhcPs)]
    groupDecls Bool
    isSig [LHsDecl GhcPs]
    xs of
        [] -> [LHsDecl GhcPs
    GenLocated SrcSpanAnnA (HsDecl GhcPs)
    l GenLocated SrcSpanAnnA (HsDecl GhcPs)
    -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    forall a. a -> [a] -> NonEmpty a
    :| []]
        (NonEmpty (LHsDecl GhcPs)
    x : [NonEmpty (LHsDecl GhcPs)]
    xs') -> (LHsDecl GhcPs
    GenLocated SrcSpanAnnA (HsDecl GhcPs)
    l GenLocated SrcSpanAnnA (HsDecl GhcPs)
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    forall a. a -> NonEmpty a -> NonEmpty a
    <| NonEmpty (LHsDecl GhcPs)
    NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    x) NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    -> [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    forall a. a -> [a] -> [a]
    : [NonEmpty (LHsDecl GhcPs)]
    [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    xs'
    groupDecls Bool
    isSig (LHsDecl GhcPs
    header : [LHsDecl GhcPs]
    xs) =
      let ([(GenLocated SrcSpanAnnA (HsDecl GhcPs),
      GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    grp, [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
      GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    rest) = (((GenLocated SrcSpanAnnA (HsDecl GhcPs),
       GenLocated SrcSpanAnnA (HsDecl GhcPs))
      -> Bool)
     -> [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
          GenLocated SrcSpanAnnA (HsDecl GhcPs))]
     -> ([(GenLocated SrcSpanAnnA (HsDecl GhcPs),
           GenLocated SrcSpanAnnA (HsDecl GhcPs))],
         [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
           GenLocated SrcSpanAnnA (HsDecl GhcPs))]))
    -> [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
         GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    -> ((GenLocated SrcSpanAnnA (HsDecl GhcPs),
         GenLocated SrcSpanAnnA (HsDecl GhcPs))
        -> Bool)
    -> ([(GenLocated SrcSpanAnnA (HsDecl GhcPs),
          GenLocated SrcSpanAnnA (HsDecl GhcPs))],
        [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
          GenLocated SrcSpanAnnA (HsDecl GhcPs))])
    forall a b c. (a -> b -> c) -> b -> a -> c
    flip ((GenLocated SrcSpanAnnA (HsDecl GhcPs),
      GenLocated SrcSpanAnnA (HsDecl GhcPs))
     -> Bool)
    -> [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
         GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    -> ([(GenLocated SrcSpanAnnA (HsDecl GhcPs),
          GenLocated SrcSpanAnnA (HsDecl GhcPs))],
        [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
          GenLocated SrcSpanAnnA (HsDecl GhcPs))])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span ([GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    -> [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
         GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    forall a b. [a] -> [b] -> [(a, b)]
    zip (LHsDecl GhcPs
    GenLocated SrcSpanAnnA (HsDecl GhcPs)
    header GenLocated SrcSpanAnnA (HsDecl GhcPs)
    -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    forall a. a -> [a] -> [a]
    : [LHsDecl GhcPs]
    [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    xs) [LHsDecl GhcPs]
    [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    xs) (((GenLocated SrcSpanAnnA (HsDecl GhcPs),
       GenLocated SrcSpanAnnA (HsDecl GhcPs))
      -> Bool)
     -> ([(GenLocated SrcSpanAnnA (HsDecl GhcPs),
           GenLocated SrcSpanAnnA (HsDecl GhcPs))],
         [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
           GenLocated SrcSpanAnnA (HsDecl GhcPs))]))
    -> ((GenLocated SrcSpanAnnA (HsDecl GhcPs),
         GenLocated SrcSpanAnnA (HsDecl GhcPs))
        -> Bool)
    -> ([(GenLocated SrcSpanAnnA (HsDecl GhcPs),
          GenLocated SrcSpanAnnA (HsDecl GhcPs))],
        [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
          GenLocated SrcSpanAnnA (HsDecl GhcPs))])
    forall a b. (a -> b) -> a -> b
    $ \(GenLocated SrcSpanAnnA (HsDecl GhcPs)
    previous, GenLocated SrcSpanAnnA (HsDecl GhcPs)
    current) ->
            let relevantToHdr :: Bool
    relevantToHdr = LHsDecl GhcPs -> LHsDecl GhcPs -> Bool
    groupedDecls LHsDecl GhcPs
    header LHsDecl GhcPs
    GenLocated SrcSpanAnnA (HsDecl GhcPs)
    current
                relevantToPrev :: Bool
    relevantToPrev = LHsDecl GhcPs -> LHsDecl GhcPs -> Bool
    groupedDecls LHsDecl GhcPs
    GenLocated SrcSpanAnnA (HsDecl GhcPs)
    previous LHsDecl GhcPs
    GenLocated SrcSpanAnnA (HsDecl GhcPs)
    current
                isDeclSeries :: Bool
    isDeclSeries = Bool -> Bool
    not Bool
    isSig Bool -> Bool -> Bool
    && LHsDecl GhcPs -> LHsDecl GhcPs -> Bool
    declSeries LHsDecl GhcPs
    GenLocated SrcSpanAnnA (HsDecl GhcPs)
    previous LHsDecl GhcPs
    GenLocated SrcSpanAnnA (HsDecl GhcPs)
    current
             in Bool
    isDeclSeries Bool -> Bool -> Bool
    || Bool
    relevantToHdr Bool -> Bool -> Bool
    || Bool
    relevantToPrev
       in (LHsDecl GhcPs
    GenLocated SrcSpanAnnA (HsDecl GhcPs)
    header GenLocated SrcSpanAnnA (HsDecl GhcPs)
    -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    -> NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    forall a. a -> [a] -> NonEmpty a
    :| ((GenLocated SrcSpanAnnA (HsDecl GhcPs),
      GenLocated SrcSpanAnnA (HsDecl GhcPs))
     -> GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
         GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    forall a b. (a -> b) -> [a] -> [b]
    map (GenLocated SrcSpanAnnA (HsDecl GhcPs),
     GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> GenLocated SrcSpanAnnA (HsDecl GhcPs)
    forall a b. (a, b) -> b
    snd [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
      GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    grp) NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    -> [NonEmpty (GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    forall a. a -> [a] -> [a]
    : Bool -> [LHsDecl GhcPs] -> [NonEmpty (LHsDecl GhcPs)]
    groupDecls Bool
    isSig (((GenLocated SrcSpanAnnA (HsDecl GhcPs),
      GenLocated SrcSpanAnnA (HsDecl GhcPs))
     -> GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
         GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    -> [GenLocated SrcSpanAnnA (HsDecl GhcPs)]
    forall a b. (a -> b) -> [a] -> [b]
    map (GenLocated SrcSpanAnnA (HsDecl GhcPs),
     GenLocated SrcSpanAnnA (HsDecl GhcPs))
    -> GenLocated SrcSpanAnnA (HsDecl GhcPs)
    forall a b. (a, b) -> b
    snd [(GenLocated SrcSpanAnnA (HsDecl GhcPs),
      GenLocated SrcSpanAnnA (HsDecl GhcPs))]
    rest)

    p_hsDecl :: FamilyStyle -> HsDecl GhcPs -> R ()
    p_hsDecl :: FamilyStyle -> HsDecl GhcPs -> R ()
    p_hsDecl FamilyStyle
    style = \case
      TyClD XTyClD GhcPs
    _ TyClDecl GhcPs
    x -> FamilyStyle -> TyClDecl GhcPs -> R ()
    p_tyClDecl FamilyStyle
    style TyClDecl GhcPs
    x
      ValD XValD GhcPs
    _ HsBind GhcPs
    x -> HsBind GhcPs -> R ()
    p_valDecl HsBind GhcPs
    x
      SigD XSigD GhcPs
    _ Sig GhcPs
    x -> Sig GhcPs -> R ()
    p_sigDecl Sig GhcPs
    x
      InstD XInstD GhcPs
    _ InstDecl GhcPs
    x -> FamilyStyle -> InstDecl GhcPs -> R ()
    p_instDecl FamilyStyle
    style InstDecl GhcPs
    x
      DerivD XDerivD GhcPs
    _ DerivDecl GhcPs
    x -> DerivDecl GhcPs -> R ()
    p_standaloneDerivDecl DerivDecl GhcPs
    x
      DefD XDefD GhcPs
    _ DefaultDecl GhcPs
    x -> DefaultDecl GhcPs -> R ()
    p_defaultDecl DefaultDecl GhcPs
    x
      ForD XForD GhcPs
    _ ForeignDecl GhcPs
    x -> ForeignDecl GhcPs -> R ()
    p_foreignDecl ForeignDecl GhcPs
    x
      WarningD XWarningD GhcPs
    _ WarnDecls GhcPs
    x -> WarnDecls GhcPs -> R ()
    p_warnDecls WarnDecls GhcPs
    x
      AnnD XAnnD GhcPs
    _ AnnDecl GhcPs
    x -> AnnDecl GhcPs -> R ()
    p_annDecl AnnDecl GhcPs
    x
      RuleD XRuleD GhcPs
    _ RuleDecls GhcPs
    x -> RuleDecls GhcPs -> R ()
    p_ruleDecls RuleDecls GhcPs
    x
      SpliceD XSpliceD GhcPs
    _ SpliceDecl GhcPs
    x -> SpliceDecl GhcPs -> R ()
    p_spliceDecl SpliceDecl GhcPs
    x
      DocD XDocD GhcPs
    _ DocDecl GhcPs
    docDecl ->
        case DocDecl GhcPs
    docDecl of
          DocCommentNext LHsDoc GhcPs
    str -> HaddockStyle -> Choice "endNewline" -> LHsDoc GhcPs -> R ()
    p_hsDoc HaddockStyle
    Pipe (Label "endNewline" -> Choice "endNewline"
    forall (a :: Symbol). Label a -> Choice a
    Without Label "endNewline"
    #endNewline) LHsDoc GhcPs
    str
          DocCommentPrev LHsDoc GhcPs
    str -> HaddockStyle -> Choice "endNewline" -> LHsDoc GhcPs -> R ()
    p_hsDoc HaddockStyle
    Caret (Label "endNewline" -> Choice "endNewline"
    forall (a :: Symbol). Label a -> Choice a
    Without Label "endNewline"
    #endNewline) LHsDoc GhcPs
    str
          DocCommentNamed String
    name LHsDoc GhcPs
    str -> HaddockStyle -> Choice "endNewline" -> LHsDoc GhcPs -> R ()
    p_hsDoc (String -> HaddockStyle
    Named String
    name) (Label "endNewline" -> Choice "endNewline"
    forall (a :: Symbol). Label a -> Choice a
    Without Label "endNewline"
    #endNewline) LHsDoc GhcPs
    str
          DocGroup Int
    n LHsDoc GhcPs
    str -> HaddockStyle -> Choice "endNewline" -> LHsDoc GhcPs -> R ()
    p_hsDoc (Int -> HaddockStyle
    Asterisk Int
    n) (Label "endNewline" -> Choice "endNewline"
    forall (a :: Symbol). Label a -> Choice a
    Without Label "endNewline"
    #endNewline) LHsDoc GhcPs
    str
      RoleAnnotD XRoleAnnotD GhcPs
    _ RoleAnnotDecl GhcPs
    x -> RoleAnnotDecl GhcPs -> R ()
    p_roleAnnot RoleAnnotDecl GhcPs
    x
      KindSigD XKindSigD GhcPs
    _ StandaloneKindSig GhcPs
    s -> StandaloneKindSig GhcPs -> R ()
    p_standaloneKindSig StandaloneKindSig GhcPs
    s

    p_tyClDecl :: FamilyStyle -> TyClDecl GhcPs -> R ()
    p_tyClDecl :: FamilyStyle -> TyClDecl GhcPs -> R ()
    p_tyClDecl FamilyStyle
    style = \case
      FamDecl XFamDecl GhcPs
    _ FamilyDecl GhcPs
    x -> FamilyStyle -> FamilyDecl GhcPs -> R ()
    p_famDecl FamilyStyle
    style FamilyDecl GhcPs
    x
      SynDecl {LexicalFixity
    XSynDecl GhcPs
    LIdP GhcPs
    LHsType GhcPs
    LHsQTyVars GhcPs
    tcdSExt :: XSynDecl GhcPs
    tcdLName :: LIdP GhcPs
    tcdTyVars :: LHsQTyVars GhcPs
    tcdFixity :: LexicalFixity
    tcdRhs :: LHsType GhcPs
    tcdRhs :: forall pass. TyClDecl pass -> LHsType pass
    tcdFixity :: forall pass. TyClDecl pass -> LexicalFixity
    tcdTyVars :: forall pass. TyClDecl pass -> LHsQTyVars pass
    tcdLName :: forall pass. TyClDecl pass -> LIdP pass
    tcdSExt :: forall pass. TyClDecl pass -> XSynDecl pass
    ..} -> LocatedN RdrName
    -> LexicalFixity -> LHsQTyVars GhcPs -> LHsType GhcPs -> R ()
    p_synDecl LIdP GhcPs
    LocatedN RdrName
    tcdLName LexicalFixity
    tcdFixity LHsQTyVars GhcPs
    tcdTyVars LHsType GhcPs
    tcdRhs
      DataDecl {LexicalFixity
    XDataDecl GhcPs
    LIdP GhcPs
    LHsQTyVars GhcPs
    HsDataDefn GhcPs
    tcdFixity :: forall pass. TyClDecl pass -> LexicalFixity
    tcdTyVars :: forall pass. TyClDecl pass -> LHsQTyVars pass
    tcdLName :: forall pass. TyClDecl pass -> LIdP pass
    tcdDExt :: XDataDecl GhcPs
    tcdLName :: LIdP GhcPs
    tcdTyVars :: LHsQTyVars GhcPs
    tcdFixity :: LexicalFixity
    tcdDataDefn :: HsDataDefn GhcPs
    tcdDataDefn :: forall pass. TyClDecl pass -> HsDataDefn pass
    tcdDExt :: forall pass. TyClDecl pass -> XDataDecl pass
    ..} ->
        FamilyStyle
    -> LocatedN RdrName
    -> [GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)]
    -> (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
        -> SrcSpan)
    -> (GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
        -> R ())
    -> LexicalFixity
    -> HsDataDefn GhcPs
    -> R ()
    forall tyVar.
    FamilyStyle
    -> LocatedN RdrName
    -> [tyVar]
    -> (tyVar -> SrcSpan)
    -> (tyVar -> R ())
    -> LexicalFixity
    -> HsDataDefn GhcPs
    -> R ()
    p_dataDecl
          FamilyStyle
    Associated
          LIdP GhcPs
    LocatedN RdrName
    tcdLName
          (LHsQTyVars GhcPs -> [LHsTyVarBndr (HsBndrVis GhcPs) GhcPs]
    forall pass.
    LHsQTyVars pass -> [LHsTyVarBndr (HsBndrVis pass) pass]
    hsq_explicit LHsQTyVars GhcPs
    tcdTyVars)
          GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
    -> SrcSpan
    forall a e. HasLoc a => GenLocated a e -> SrcSpan
    getLocA
          ((HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> R ())
    -> GenLocated SrcSpanAnnA (HsTyVarBndr (HsBndrVis GhcPs) GhcPs)
    -> R ()
    forall l a. HasLoc l => (a -> R ()) -> GenLocated l a -> R ()
    located' HsTyVarBndr (HsBndrVis GhcPs) GhcPs -> R ()
    forall flag. IsTyVarBndrFlag flag => HsTyVarBndr flag GhcPs -> R ()
    p_hsTyVarBndr)
          LexicalFixity
    tcdFixity
          HsDataDefn GhcPs
    tcdDataDefn
      ClassDecl {[LSig GhcPs]
    LHsBinds GhcPs
    [LDocDecl GhcPs]
    [LTyFamDefltDecl GhcPs]
    [LFamilyDecl GhcPs]
    [LHsFunDep GhcPs]
    Maybe (LHsContext GhcPs)
    LexicalFixity
    XClassDecl GhcPs
    LIdP GhcPs
    LHsQTyVars GhcPs
    tcdFixity :: forall pass. TyClDecl pass -> LexicalFixity
    tcdTyVars :: forall pass. TyClDecl pass -> LHsQTyVars pass
    tcdLName :: forall pass. TyClDecl pass -> LIdP pass
    tcdCExt :: XClassDecl GhcPs
    tcdCtxt :: Maybe (LHsContext GhcPs)
    tcdLName :: LIdP GhcPs
    tcdTyVars :: LHsQTyVars GhcPs
    tcdFixity :: LexicalFixity
    tcdFDs :: [LHsFunDep GhcPs]
    tcdSigs :: [LSig GhcPs]
    tcdMeths :: LHsBinds GhcPs
    tcdATs :: [LFamilyDecl GhcPs]
    tcdATDefs :: [LTyFamDefltDecl GhcPs]
    tcdDocs :: [LDocDecl GhcPs]
    tcdDocs :: forall pass. TyClDecl pass -> [LDocDecl pass]
    tcdATDefs :: forall pass. TyClDecl pass -> [LTyFamDefltDecl pass]
    tcdATs :: forall pass. TyClDecl pass -> [LFamilyDecl pass]
    tcdMeths :: forall pass. TyClDecl pass -> LHsBinds pass
    tcdSigs :: forall pass. TyClDecl pass -> [LSig pass]
    tcdFDs :: forall pass. TyClDecl pass -> [LHsFunDep pass]
    tcdCtxt :: forall pass. TyClDecl pass -> Maybe (LHsContext pass)
    tcdCExt :: forall pass. TyClDecl pass -> XClassDecl pass
    ..} ->
        Maybe (LHsContext GhcPs)
    -> LocatedN RdrName
    -> LHsQTyVars GhcPs
    -> LexicalFixity
    -> [LHsFunDep GhcPs]
    -> [LSig GhcPs]
    -> LHsBinds GhcPs
    -> [LFamilyDecl GhcPs]
    -> [LTyFamDefltDecl GhcPs]
    -> [LDocDecl GhcPs]
    -> R ()
    p_classDecl
          Maybe (LHsContext GhcPs)
    tcdCtxt
          LIdP GhcPs
    LocatedN RdrName
    tcdLName
          LHsQTyVars GhcPs
    tcdTyVars
          LexicalFixity
    tcdFixity
          [LHsFunDep GhcPs]
    tcdFDs
          [LSig GhcPs]
    tcdSigs
          LHsBinds GhcPs
    tcdMeths
          [LFamilyDecl GhcPs]
    tcdATs
          [LTyFamDefltDecl GhcPs]
    tcdATDefs
          [LDocDecl GhcPs]
    tcdDocs

    p_instDecl :: FamilyStyle -> InstDecl GhcPs -> R ()
    p_instDecl :: FamilyStyle -> InstDecl GhcPs -> R ()
    p_instDecl FamilyStyle
    style = \case
      ClsInstD XClsInstD GhcPs
    _ ClsInstDecl GhcPs
    x -> ClsInstDecl GhcPs -> R ()
    p_clsInstDecl ClsInstDecl GhcPs
    x
      TyFamInstD XTyFamInstD GhcPs
    _ TyFamInstDecl GhcPs
    x -> FamilyStyle -> TyFamInstDecl GhcPs -> R ()
    p_tyFamInstDecl FamilyStyle
    style TyFamInstDecl GhcPs
    x
      DataFamInstD XDataFamInstD GhcPs
    _ DataFamInstDecl GhcPs
    x -> FamilyStyle -> DataFamInstDecl GhcPs -> R ()
    p_dataFamInstDecl FamilyStyle
    style DataFamInstDecl GhcPs
    x

    -- | Determine if these declarations should be grouped together.
    groupedDecls ::
      LHsDecl GhcPs ->
      LHsDecl GhcPs ->
      Bool
    groupedDecls :: LHsDecl GhcPs -> LHsDecl GhcPs -> Bool
    groupedDecls (L (SrcSpanAnnA -> SrcSpan
    forall a. HasLoc a => a -> SrcSpan
    locA -> SrcSpan
    l_x) HsDecl GhcPs
    x') (L (SrcSpanAnnA -> SrcSpan
    forall a. HasLoc a => a -> SrcSpan
    locA -> SrcSpan
    l_y) HsDecl GhcPs
    y') =
      case (HsDecl GhcPs
    x', HsDecl GhcPs
    y') of
        (TypeSignature [RdrName]
    ns, FunctionBody [RdrName]
    ns') -> [RdrName]
    ns [RdrName] -> [RdrName] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    `intersects` [RdrName]
    ns'
        (TypeSignature [RdrName]
    ns, DefaultSignature [RdrName]
    ns') -> [RdrName]
    ns [RdrName] -> [RdrName] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    `intersects` [RdrName]
    ns'
        (DefaultSignature [RdrName]
    ns, TypeSignature [RdrName]
    ns') -> [RdrName]
    ns [RdrName] -> [RdrName] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    `intersects` [RdrName]
    ns'
        (DefaultSignature [RdrName]
    ns, FunctionBody [RdrName]
    ns') -> [RdrName]
    ns [RdrName] -> [RdrName] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    `intersects` [RdrName]
    ns'
        (HsDecl GhcPs
    x, FunctionBody [RdrName]
    ns) | Just [RdrName]
    ns' <- HsDecl GhcPs -> Maybe [RdrName]
    isPragma HsDecl GhcPs
    x -> [RdrName]
    ns [RdrName] -> [RdrName] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    `intersects` [RdrName]
    ns'
        (FunctionBody [RdrName]
    ns, HsDecl GhcPs
    x) | Just [RdrName]
    ns' <- HsDecl GhcPs -> Maybe [RdrName]
    isPragma HsDecl GhcPs
    x -> [RdrName]
    ns [RdrName] -> [RdrName] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    `intersects` [RdrName]
    ns'
        (HsDecl GhcPs
    x, DataDeclaration RdrName
    n) | Just [RdrName]
    ns <- HsDecl GhcPs -> Maybe [RdrName]
    isPragma HsDecl GhcPs
    x -> RdrName
    n RdrName -> [RdrName] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [RdrName]
    ns
        (DataDeclaration RdrName
    n, HsDecl GhcPs
    x)
          | Just [RdrName]
    ns <- HsDecl GhcPs -> Maybe [RdrName]
    isPragma HsDecl GhcPs
    x ->
              let f :: RdrName -> FastString
    f = OccName -> FastString
    occNameFS (OccName -> FastString)
    -> (RdrName -> OccName) -> RdrName -> FastString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . RdrName -> OccName
    rdrNameOcc in RdrName -> FastString
    f RdrName
    n FastString -> [FastString] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` (RdrName -> FastString) -> [RdrName] -> [FastString]
    forall a b. (a -> b) -> [a] -> [b]
    map RdrName -> FastString
    f [RdrName]
    ns
        (HsDecl GhcPs
    x, HsDecl GhcPs
    y)
          | Just [RdrName]
    ns <- HsDecl GhcPs -> Maybe [RdrName]
    isPragma HsDecl GhcPs
    x,
            Just [RdrName]
    ns' <- HsDecl GhcPs -> Maybe [RdrName]
    isPragma HsDecl GhcPs
    y ->
              [RdrName]
    ns [RdrName] -> [RdrName] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    `intersects` [RdrName]
    ns'
        (HsDecl GhcPs
    x, TypeSignature [RdrName]
    ns) | Just [RdrName]
    ns' <- HsDecl GhcPs -> Maybe [RdrName]
    isPragma HsDecl GhcPs
    x -> [RdrName]
    ns [RdrName] -> [RdrName] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    `intersects` [RdrName]
    ns'
        (TypeSignature [RdrName]
    ns, HsDecl GhcPs
    x) | Just [RdrName]
    ns' <- HsDecl GhcPs -> Maybe [RdrName]
    isPragma HsDecl GhcPs
    x -> [RdrName]
    ns [RdrName] -> [RdrName] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    `intersects` [RdrName]
    ns'
        (PatternSignature [RdrName]
    ns, Pattern RdrName
    n) -> RdrName
    n RdrName -> [RdrName] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [RdrName]
    ns
        (KindSignature RdrName
    n, DataDeclaration RdrName
    n') -> RdrName
    n RdrName -> RdrName -> Bool
    forall a. Eq a => a -> a -> Bool
    == RdrName
    n'
        (KindSignature RdrName
    n, ClassDeclaration RdrName
    n') -> RdrName
    n RdrName -> RdrName -> Bool
    forall a. Eq a => a -> a -> Bool
    == RdrName
    n'
        (KindSignature RdrName
    n, FamilyDeclaration RdrName
    n') -> RdrName
    n RdrName -> RdrName -> Bool
    forall a. Eq a => a -> a -> Bool
    == RdrName
    n'
        (KindSignature RdrName
    n, TypeSynonym RdrName
    n') -> RdrName
    n RdrName -> RdrName -> Bool
    forall a. Eq a => a -> a -> Bool
    == RdrName
    n'
        -- Special case for TH splices, we look at locations
        (HsDecl GhcPs
    Splice, HsDecl GhcPs
    Splice) -> Bool -> Bool
    not ((SrcSpan -> SrcSpan) -> SrcSpan -> SrcSpan -> Bool
    forall a. (a -> SrcSpan) -> a -> a -> Bool
    separatedByBlank SrcSpan -> SrcSpan
    forall a. a -> a
    id SrcSpan
    l_x SrcSpan
    l_y)
        -- This looks only at Haddocks, normal comments are handled elsewhere
        (HsDecl GhcPs
    DocNext, HsDecl GhcPs
    _) -> Bool
    True
        (HsDecl GhcPs
    _, HsDecl GhcPs
    DocPrev) -> Bool
    True
        (HsDecl GhcPs, HsDecl GhcPs)
    _ -> Bool
    False

    -- | Detect declaration series that should not have blanks between them.
    declSeries ::
      LHsDecl GhcPs ->
      LHsDecl GhcPs ->
      Bool
    declSeries :: LHsDecl GhcPs -> LHsDecl GhcPs -> Bool
    declSeries (L SrcSpanAnnA
    _ HsDecl GhcPs
    x) (L SrcSpanAnnA
    _ HsDecl GhcPs
    y) =
      case (HsDecl GhcPs
    x, HsDecl GhcPs
    y) of
        ( SigD XSigD GhcPs
    _ (TypeSig XTypeSig GhcPs
    _ [LIdP GhcPs]
    _ LHsSigWcType GhcPs
    _),
          SigD XSigD GhcPs
    _ (TypeSig XTypeSig GhcPs
    _ [LIdP GhcPs]
    _ LHsSigWcType GhcPs
    _)
          ) -> Bool
    True
        (HsDecl GhcPs, HsDecl GhcPs)
    _ -> Bool
    False

    intersects :: (Ord a) => [a] -> [a] -> Bool
    intersects :: forall a. Ord a => [a] -> [a] -> Bool
    intersects [a]
    a [a]
    b = [a] -> [a] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    go ([a] -> [a]
    forall a. Ord a => [a] -> [a]
    sort [a]
    a) ([a] -> [a]
    forall a. Ord a => [a] -> [a]
    sort [a]
    b)
      where
        go :: (Ord a) => [a] -> [a] -> Bool
        go :: forall a. Ord a => [a] -> [a] -> Bool
    go [a]
    _ [] = Bool
    False
        go [] [a]
    _ = Bool
    False
        go (a
    x : [a]
    xs) (a
    y : [a]
    ys)
          | a
    x a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    < a
    y = [a] -> [a] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    go [a]
    xs (a
    y a -> [a] -> [a]
    forall a. a -> [a] -> [a]
    : [a]
    ys)
          | a
    x a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    > a
    y = [a] -> [a] -> Bool
    forall a. Ord a => [a] -> [a] -> Bool
    go (a
    x a -> [a] -> [a]
    forall a. a -> [a] -> [a]
    : [a]
    xs) [a]
    ys
          | Bool
    otherwise = Bool
    True

    isPragma ::
      HsDecl GhcPs ->
      Maybe [RdrName]
    isPragma :: HsDecl GhcPs -> Maybe [RdrName]
    isPragma = \case
      InlinePragma RdrName
    n -> [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just [RdrName
    n]
      SpecializePragma RdrName
    n -> [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just [RdrName
    n]
      SCCPragma RdrName
    n -> [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just [RdrName
    n]
      AnnTypePragma RdrName
    n -> [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just [RdrName
    n]
      AnnValuePragma RdrName
    n -> [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just [RdrName
    n]
      WarningPragma [RdrName]
    n -> [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just [RdrName]
    n
      HsDecl GhcPs
    _ -> Maybe [RdrName]
    forall a. Maybe a
    Nothing

    -- Declarations that do not refer to names

    pattern Splice :: HsDecl GhcPs
    pattern $mSplice :: forall {r}. HsDecl GhcPs -> ((# #) -> r) -> ((# #) -> r) -> r
    Splice <- SpliceD _ (SpliceDecl _ _ _)

    -- Declarations referring to a single name

    pattern
      InlinePragma,
      SpecializePragma,
      SCCPragma,
      AnnTypePragma,
      AnnValuePragma,
      Pattern,
      DataDeclaration,
      ClassDeclaration,
      KindSignature,
      FamilyDeclaration,
      TypeSynonym ::
        RdrName -> HsDecl GhcPs
    pattern $mInlinePragma :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    InlinePragma n <- SigD _ (InlineSig _ (L _ n) _)
    pattern $mSpecializePragma :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    SpecializePragma n <- SigD _ (SpecSig _ (L _ n) _ _)
    pattern $mSCCPragma :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    SCCPragma n <- SigD _ (SCCFunSig _ (L _ n) _)
    pattern $mAnnTypePragma :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    AnnTypePragma n <- AnnD _ (HsAnnotation _ (TypeAnnProvenance (L _ n)) _)
    pattern $mAnnValuePragma :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    AnnValuePragma n <- AnnD _ (HsAnnotation _ (ValueAnnProvenance (L _ n)) _)
    pattern $mPattern :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    Pattern n <- ValD _ (PatSynBind _ (PSB _ (L _ n) _ _ _))
    pattern $mDataDeclaration :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    DataDeclaration n <- TyClD _ (DataDecl _ (L _ n) _ _ _)
    pattern $mClassDeclaration :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    ClassDeclaration n <- TyClD _ (ClassDecl _ _ (L _ n) _ _ _ _ _ _ _ _)
    pattern $mKindSignature :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    KindSignature n <- KindSigD _ (StandaloneKindSig _ (L _ n) _)
    pattern $mFamilyDeclaration :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    FamilyDeclaration n <- TyClD _ (FamDecl _ (FamilyDecl _ _ _ (L _ n) _ _ _ _))
    pattern $mTypeSynonym :: forall {r}. HsDecl GhcPs -> (RdrName -> r) -> ((# #) -> r) -> r
    TypeSynonym n <- TyClD _ (SynDecl _ (L _ n) _ _ _)

    -- Declarations which can refer to multiple names

    pattern
      TypeSignature,
      DefaultSignature,
      FunctionBody,
      PatternSignature,
      WarningPragma ::
        [RdrName] -> HsDecl GhcPs
    pattern $mTypeSignature :: forall {r}. HsDecl GhcPs -> ([RdrName] -> r) -> ((# #) -> r) -> r
    TypeSignature n <- (sigRdrNames -> Just n)
    pattern $mDefaultSignature :: forall {r}. HsDecl GhcPs -> ([RdrName] -> r) -> ((# #) -> r) -> r
    DefaultSignature n <- (defSigRdrNames -> Just n)
    pattern $mFunctionBody :: forall {r}. HsDecl GhcPs -> ([RdrName] -> r) -> ((# #) -> r) -> r
    FunctionBody n <- (funRdrNames -> Just n)
    pattern $mPatternSignature :: forall {r}. HsDecl GhcPs -> ([RdrName] -> r) -> ((# #) -> r) -> r
    PatternSignature n <- (patSigRdrNames -> Just n)
    pattern $mWarningPragma :: forall {r}. HsDecl GhcPs -> ([RdrName] -> r) -> ((# #) -> r) -> r
    WarningPragma n <- (warnSigRdrNames -> Just n)

    pattern DocNext, DocPrev :: HsDecl GhcPs
    pattern $mDocNext :: forall {r}. HsDecl GhcPs -> ((# #) -> r) -> ((# #) -> r) -> r
    DocNext <- (DocD _ (DocCommentNext _))
    pattern $mDocPrev :: forall {r}. HsDecl GhcPs -> ((# #) -> r) -> ((# #) -> r) -> r
    DocPrev <- (DocD _ (DocCommentPrev _))

    sigRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    sigRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    sigRdrNames (SigD XSigD GhcPs
    _ (TypeSig XTypeSig GhcPs
    _ [LIdP GhcPs]
    ns LHsSigWcType GhcPs
    _)) = [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just ([RdrName] -> Maybe [RdrName]) -> [RdrName] -> Maybe [RdrName]
    forall a b. (a -> b) -> a -> b
    $ (LocatedN RdrName -> RdrName) -> [LocatedN RdrName] -> [RdrName]
    forall a b. (a -> b) -> [a] -> [b]
    map LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc [LIdP GhcPs]
    [LocatedN RdrName]
    ns
    sigRdrNames (SigD XSigD GhcPs
    _ (ClassOpSig XClassOpSig GhcPs
    _ Bool
    _ [LIdP GhcPs]
    ns LHsSigType GhcPs
    _)) = [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just ([RdrName] -> Maybe [RdrName]) -> [RdrName] -> Maybe [RdrName]
    forall a b. (a -> b) -> a -> b
    $ (LocatedN RdrName -> RdrName) -> [LocatedN RdrName] -> [RdrName]
    forall a b. (a -> b) -> [a] -> [b]
    map LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc [LIdP GhcPs]
    [LocatedN RdrName]
    ns
    sigRdrNames (SigD XSigD GhcPs
    _ (PatSynSig XPatSynSig GhcPs
    _ [LIdP GhcPs]
    ns LHsSigType GhcPs
    _)) = [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just ([RdrName] -> Maybe [RdrName]) -> [RdrName] -> Maybe [RdrName]
    forall a b. (a -> b) -> a -> b
    $ (LocatedN RdrName -> RdrName) -> [LocatedN RdrName] -> [RdrName]
    forall a b. (a -> b) -> [a] -> [b]
    map LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc [LIdP GhcPs]
    [LocatedN RdrName]
    ns
    sigRdrNames HsDecl GhcPs
    _ = Maybe [RdrName]
    forall a. Maybe a
    Nothing

    defSigRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    defSigRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    defSigRdrNames (SigD XSigD GhcPs
    _ (ClassOpSig XClassOpSig GhcPs
    _ Bool
    True [LIdP GhcPs]
    ns LHsSigType GhcPs
    _)) = [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just ([RdrName] -> Maybe [RdrName]) -> [RdrName] -> Maybe [RdrName]
    forall a b. (a -> b) -> a -> b
    $ (LocatedN RdrName -> RdrName) -> [LocatedN RdrName] -> [RdrName]
    forall a b. (a -> b) -> [a] -> [b]
    map LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc [LIdP GhcPs]
    [LocatedN RdrName]
    ns
    defSigRdrNames HsDecl GhcPs
    _ = Maybe [RdrName]
    forall a. Maybe a
    Nothing

    funRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    funRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    funRdrNames (ValD XValD GhcPs
    _ (FunBind XFunBind GhcPs GhcPs
    _ (L SrcSpanAnnN
    _ RdrName
    n) MatchGroup GhcPs (XRec GhcPs (HsExpr GhcPs))
    _)) = [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just [RdrName
    n]
    funRdrNames (ValD XValD GhcPs
    _ (PatBind XPatBind GhcPs GhcPs
    _ (L SrcSpanAnnA
    _ Pat GhcPs
    n) HsMultAnn GhcPs
    _ GRHSs GhcPs (XRec GhcPs (HsExpr GhcPs))
    _)) = [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just ([RdrName] -> Maybe [RdrName]) -> [RdrName] -> Maybe [RdrName]
    forall a b. (a -> b) -> a -> b
    $ Pat GhcPs -> [RdrName]
    patBindNames Pat GhcPs
    n
    funRdrNames HsDecl GhcPs
    _ = Maybe [RdrName]
    forall a. Maybe a
    Nothing

    patSigRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    patSigRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    patSigRdrNames (SigD XSigD GhcPs
    _ (PatSynSig XPatSynSig GhcPs
    _ [LIdP GhcPs]
    ns LHsSigType GhcPs
    _)) = [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just ([RdrName] -> Maybe [RdrName]) -> [RdrName] -> Maybe [RdrName]
    forall a b. (a -> b) -> a -> b
    $ (LocatedN RdrName -> RdrName) -> [LocatedN RdrName] -> [RdrName]
    forall a b. (a -> b) -> [a] -> [b]
    map LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc [LIdP GhcPs]
    [LocatedN RdrName]
    ns
    patSigRdrNames HsDecl GhcPs
    _ = Maybe [RdrName]
    forall a. Maybe a
    Nothing

    warnSigRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    warnSigRdrNames :: HsDecl GhcPs -> Maybe [RdrName]
    warnSigRdrNames (WarningD XWarningD GhcPs
    _ (Warnings XWarnings GhcPs
    _ [LWarnDecl GhcPs]
    ws)) = [RdrName] -> Maybe [RdrName]
    forall a. a -> Maybe a
    Just ([RdrName] -> Maybe [RdrName]) -> [RdrName] -> Maybe [RdrName]
    forall a b. (a -> b) -> a -> b
    $
      ((GenLocated SrcSpanAnnA (WarnDecl GhcPs) -> [RdrName])
     -> [GenLocated SrcSpanAnnA (WarnDecl GhcPs)] -> [RdrName])
    -> [GenLocated SrcSpanAnnA (WarnDecl GhcPs)]
    -> (GenLocated SrcSpanAnnA (WarnDecl GhcPs) -> [RdrName])
    -> [RdrName]
    forall a b c. (a -> b -> c) -> b -> a -> c
    flip (GenLocated SrcSpanAnnA (WarnDecl GhcPs) -> [RdrName])
    -> [GenLocated SrcSpanAnnA (WarnDecl GhcPs)] -> [RdrName]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap [LWarnDecl GhcPs]
    [GenLocated SrcSpanAnnA (WarnDecl GhcPs)]
    ws ((GenLocated SrcSpanAnnA (WarnDecl GhcPs) -> [RdrName])
     -> [RdrName])
    -> (GenLocated SrcSpanAnnA (WarnDecl GhcPs) -> [RdrName])
    -> [RdrName]
    forall a b. (a -> b) -> a -> b
    $
        \(L SrcSpanAnnA
    _ (Warning XWarning GhcPs
    _ [LIdP GhcPs]
    ns WarningTxt GhcPs
    _)) -> (LocatedN RdrName -> RdrName) -> [LocatedN RdrName] -> [RdrName]
    forall a b. (a -> b) -> [a] -> [b]
    map LocatedN RdrName -> RdrName
    forall l e. GenLocated l e -> e
    unLoc [LIdP GhcPs]
    [LocatedN RdrName]
    ns
    warnSigRdrNames HsDecl GhcPs
    _ = Maybe [RdrName]
    forall a. Maybe a
    Nothing

    patBindNames :: Pat GhcPs -> [RdrName]
    patBindNames :: Pat GhcPs -> [RdrName]
    patBindNames (TuplePat XTuplePat GhcPs
    _ [LPat GhcPs]
    ps Boxity
    _) = (GenLocated SrcSpanAnnA (Pat GhcPs) -> [RdrName])
    -> [GenLocated SrcSpanAnnA (Pat GhcPs)] -> [RdrName]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap (Pat GhcPs -> [RdrName]
    patBindNames (Pat GhcPs -> [RdrName])
    -> (GenLocated SrcSpanAnnA (Pat GhcPs) -> Pat GhcPs)
    -> GenLocated SrcSpanAnnA (Pat GhcPs)
    -> [RdrName]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated SrcSpanAnnA (Pat GhcPs) -> Pat GhcPs
    forall l e. GenLocated l e -> e
    unLoc) [LPat GhcPs]
    [GenLocated SrcSpanAnnA (Pat GhcPs)]
    ps
    patBindNames (OrPat XOrPat GhcPs
    _ NonEmpty (LPat GhcPs)
    ps) = (GenLocated SrcSpanAnnA (Pat GhcPs) -> [RdrName])
    -> NonEmpty (GenLocated SrcSpanAnnA (Pat GhcPs)) -> [RdrName]
    forall m a. Monoid m => (a -> m) -> NonEmpty a -> m
    forall (t :: * -> *) m a.
    (Foldable t, Monoid m) =>
    (a -> m) -> t a -> m
    foldMap (Pat GhcPs -> [RdrName]
    patBindNames (Pat GhcPs -> [RdrName])
    -> (GenLocated SrcSpanAnnA (Pat GhcPs) -> Pat GhcPs)
    -> GenLocated SrcSpanAnnA (Pat GhcPs)
    -> [RdrName]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated SrcSpanAnnA (Pat GhcPs) -> Pat GhcPs
    forall l e. GenLocated l e -> e
    unLoc) NonEmpty (LPat GhcPs)
    NonEmpty (GenLocated SrcSpanAnnA (Pat GhcPs))
    ps
    patBindNames (VarPat XVarPat GhcPs
    _ (L SrcSpanAnnN
    _ RdrName
    n)) = [RdrName
    n]
    patBindNames (WildPat XWildPat GhcPs
    _) = []
    patBindNames (LazyPat XLazyPat GhcPs
    _ (L SrcSpanAnnA
    _ Pat GhcPs
    p)) = Pat GhcPs -> [RdrName]
    patBindNames Pat GhcPs
    p
    patBindNames (BangPat XBangPat GhcPs
    _ (L SrcSpanAnnA
    _ Pat GhcPs
    p)) = Pat GhcPs -> [RdrName]
    patBindNames Pat GhcPs
    p
    patBindNames (ParPat XParPat GhcPs
    _ (L SrcSpanAnnA
    _ Pat GhcPs
    p)) = Pat GhcPs -> [RdrName]
    patBindNames Pat GhcPs
    p
    patBindNames (ListPat XListPat GhcPs
    _ [LPat GhcPs]
    ps) = (GenLocated SrcSpanAnnA (Pat GhcPs) -> [RdrName])
    -> [GenLocated SrcSpanAnnA (Pat GhcPs)] -> [RdrName]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap (Pat GhcPs -> [RdrName]
    patBindNames (Pat GhcPs -> [RdrName])
    -> (GenLocated SrcSpanAnnA (Pat GhcPs) -> Pat GhcPs)
    -> GenLocated SrcSpanAnnA (Pat GhcPs)
    -> [RdrName]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated SrcSpanAnnA (Pat GhcPs) -> Pat GhcPs
    forall l e. GenLocated l e -> e
    unLoc) [LPat GhcPs]
    [GenLocated SrcSpanAnnA (Pat GhcPs)]
    ps
    patBindNames (AsPat XAsPat GhcPs
    _ (L SrcSpanAnnN
    _ RdrName
    n) (L SrcSpanAnnA
    _ Pat GhcPs
    p)) = RdrName
    n RdrName -> [RdrName] -> [RdrName]
    forall a. a -> [a] -> [a]
    : Pat GhcPs -> [RdrName]
    patBindNames Pat GhcPs
    p
    patBindNames (SumPat XSumPat GhcPs
    _ (L SrcSpanAnnA
    _ Pat GhcPs
    p) Int
    _ Int
    _) = Pat GhcPs -> [RdrName]
    patBindNames Pat GhcPs
    p
    patBindNames (ViewPat XViewPat GhcPs
    _ XRec GhcPs (HsExpr GhcPs)
    _ (L SrcSpanAnnA
    _ Pat GhcPs
    p)) = Pat GhcPs -> [RdrName]
    patBindNames Pat GhcPs
    p
    patBindNames (SplicePat XSplicePat GhcPs
    _ HsUntypedSplice GhcPs
    _) = []
    patBindNames (LitPat XLitPat GhcPs
    _ HsLit GhcPs
    _) = []
    patBindNames (SigPat XSigPat GhcPs
    _ (L SrcSpanAnnA
    _ Pat GhcPs
    p) HsPatSigType (NoGhcTc GhcPs)
    _) = Pat GhcPs -> [RdrName]
    patBindNames Pat GhcPs
    p
    patBindNames (NPat XNPat GhcPs
    _ XRec GhcPs (HsOverLit GhcPs)
    _ Maybe (SyntaxExpr GhcPs)
    _ SyntaxExpr GhcPs
    _) = []
    patBindNames (NPlusKPat XNPlusKPat GhcPs
    _ (L SrcSpanAnnN
    _ RdrName
    n) XRec GhcPs (HsOverLit GhcPs)
    _ HsOverLit GhcPs
    _ SyntaxExpr GhcPs
    _ SyntaxExpr GhcPs
    _) = [RdrName
    n]
    patBindNames (ConPat XConPat GhcPs
    _ XRec GhcPs (ConLikeP GhcPs)
    _ HsConPatDetails GhcPs
    d) = (GenLocated SrcSpanAnnA (Pat GhcPs) -> [RdrName])
    -> [GenLocated SrcSpanAnnA (Pat GhcPs)] -> [RdrName]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap (Pat GhcPs -> [RdrName]
    patBindNames (Pat GhcPs -> [RdrName])
    -> (GenLocated SrcSpanAnnA (Pat GhcPs) -> Pat GhcPs)
    -> GenLocated SrcSpanAnnA (Pat GhcPs)
    -> [RdrName]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . GenLocated SrcSpanAnnA (Pat GhcPs) -> Pat GhcPs
    forall l e. GenLocated l e -> e
    unLoc) (HsConPatDetails GhcPs -> [LPat GhcPs]
    forall p. UnXRec p => HsConPatDetails p -> [LPat p]
    hsConPatArgs HsConPatDetails GhcPs
    d)
    patBindNames (EmbTyPat XEmbTyPat GhcPs
    _ HsTyPat (NoGhcTc GhcPs)
    _) = []
    patBindNames (InvisPat XInvisPat GhcPs
    _ HsTyPat (NoGhcTc GhcPs)
    _) = []