packages feed

scrod-0.2026.2.21: source/library/Scrod/Convert/FromGhc/RoleParents.hs

-- | Resolve role annotation parent relationships.
--
-- Associates role annotation items with their target type declarations
-- when those declarations are defined in the same module. Uses the shared
-- parent association logic from 'Scrod.Convert.FromGhc.ParentAssociation'.
module Scrod.Convert.FromGhc.RoleParents where

import qualified Data.Set as Set
import qualified GHC.Hs.Extension as Ghc
import qualified GHC.Parser.Annotation as Annotation
import qualified GHC.Types.SrcLoc as SrcLoc
import qualified Language.Haskell.Syntax as Syntax
import qualified Scrod.Convert.FromGhc.Internal as Internal
import qualified Scrod.Core.Location as Location

-- | Extract the set of source locations that correspond to role annotation
-- declaration names.
extractRoleLocations ::
  SrcLoc.Located (Syntax.HsModule Ghc.GhcPs) ->
  Set.Set Location.Location
extractRoleLocations = Internal.extractDeclLocations extractDeclRoleLocations

-- | Extract role annotation name locations from a single declaration.
extractDeclRoleLocations ::
  Syntax.LHsDecl Ghc.GhcPs ->
  [Location.Location]
extractDeclRoleLocations lDecl = case SrcLoc.unLoc lDecl of
  Syntax.RoleAnnotD _ (Syntax.RoleAnnotDecl _ lName _) ->
    foldMap pure $ Internal.locationFromSrcSpan (Annotation.getLocA lName)
  _ -> []