Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit f0b1198

Browse files
committed
Use functions exported from HsToCore
1 parent 792b828 commit f0b1198

File tree

5 files changed

+4
-220
lines changed

5 files changed

+4
-220
lines changed

haddock-api/src/Haddock/GhcUtils.hs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,6 @@ moduleString = moduleNameString . moduleName
5555
isNameSym :: Name -> Bool
5656
isNameSym = isSymOcc . nameOccName
5757

58-
getMainDeclBinder :: (CollectPass (GhcPass p)) => HsDecl (GhcPass p) -> [IdP (GhcPass p)]
59-
getMainDeclBinder (TyClD _ d) = [tcdName d]
60-
getMainDeclBinder (ValD _ d) =
61-
case collectHsBindBinders d of
62-
[] -> []
63-
(name:_) -> [name]
64-
getMainDeclBinder (SigD _ d) = sigNameNoLoc d
65-
getMainDeclBinder (ForD _ (ForeignImport _ name _ _)) = [unLoc name]
66-
getMainDeclBinder (ForD _ (ForeignExport _ _ _ _)) = []
67-
getMainDeclBinder _ = []
68-
69-
-- Extract the source location where an instance is defined. This is used
70-
-- to correlate InstDecls with their Instance/CoAxiom Names, via the
71-
-- instanceMap.
72-
getInstLoc :: InstDecl (GhcPass p) -> SrcSpan
73-
getInstLoc (ClsInstD _ (ClsInstDecl { cid_poly_ty = ty })) = getLoc (hsSigType ty)
74-
getInstLoc (DataFamInstD _ (DataFamInstDecl
75-
{ dfid_eqn = HsIB { hsib_body = FamEqn { feqn_tycon = L l _ }}})) = l
76-
getInstLoc (TyFamInstD _ (TyFamInstDecl
77-
-- Since CoAxioms' Names refer to the whole line for type family instances
78-
-- in particular, we need to dig a bit deeper to pull out the entire
79-
-- equation. This does not happen for data family instances, for some reason.
80-
{ tfid_eqn = HsIB { hsib_body = FamEqn { feqn_rhs = L l _ }}})) = l
81-
82-
83-
8458
-- Useful when there is a signature with multiple names, e.g.
8559
-- foo, bar :: Types..
8660
-- but only one of the names is exported and we have to change the
@@ -138,24 +112,9 @@ isClassD :: HsDecl a -> Bool
138112
isClassD (TyClD _ d) = isClassDecl d
139113
isClassD _ = False
140114

141-
isValD :: HsDecl a -> Bool
142-
isValD (ValD _ _) = True
143-
isValD _ = False
144-
145115
pretty :: Outputable a => DynFlags -> a -> String
146116
pretty = showPpr
147117

148-
nubByName :: (a -> Name) -> [a] -> [a]
149-
nubByName f ns = go emptyNameSet ns
150-
where
151-
go !_ [] = []
152-
go !s (x:xs)
153-
| y `elemNameSet` s = go s xs
154-
| otherwise = let !s' = extendNameSet s y
155-
in x : go s' xs
156-
where
157-
y = f x
158-
159118
-- ---------------------------------------------------------------------
160119

161120
-- These functions are duplicated from the GHC API, as they must be

haddock-api/src/Haddock/Interface.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import GHC.Types.Name (nameIsFromExternalPackage, nameOccName)
6161
import GHC.Types.Name.Occurrence (isTcOcc)
6262
import GHC.Types.Name.Reader (unQualOK, gre_name, globalRdrEnvElts)
6363
import GHC.Utils.Error (withTimingD)
64+
import GHC.HsToCore.Docs
6465

6566
#if defined(mingw32_HOST_OS)
6667
import System.IO

haddock-api/src/Haddock/Interface/AttachInstances.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import GHC.Core.TyCon
4444
import GHC.Core.TyCo.Rep
4545
import GHC.Builtin.Types.Prim( funTyConName )
4646
import GHC.Types.Var hiding (varName)
47+
import GHC.HsToCore.Docs
4748

4849
type ExportedNames = Set.Set Name
4950
type Modules = Set.Set Module

haddock-api/src/Haddock/Interface/Create.hs

Lines changed: 1 addition & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import GHC.Tc.Types
5555
import GHC.Data.FastString ( unpackFS, bytesFS )
5656
import GHC.Types.Basic ( StringLiteral(..), SourceText(..), PromotionFlag(..) )
5757
import qualified GHC.Utils.Outputable as O
58+
import GHC.HsToCore.Docs hiding (mkMaps)
5859

5960

6061
-- | Use a 'TypecheckedModule' to produce an 'Interface'.
@@ -437,105 +438,6 @@ mkMaps dflags pkgName gre instances decls = do
437438
-- | Get all subordinate declarations inside a declaration, and their docs.
438439
-- A subordinate declaration is something like the associate type or data
439440
-- family of a type class.
440-
subordinates :: InstMap
441-
-> HsDecl GhcRn
442-
-> [(Name, [HsDocString], Map Int HsDocString)]
443-
subordinates instMap decl = case decl of
444-
InstD _ (ClsInstD _ d) -> do
445-
DataFamInstDecl { dfid_eqn = HsIB { hsib_body =
446-
FamEqn { feqn_tycon = L l _
447-
, feqn_rhs = defn }}} <- unLoc <$> cid_datafam_insts d
448-
[ (n, [], M.empty) | Just n <- [SrcLoc.lookupSrcSpan l instMap] ] ++ dataSubs defn
449-
450-
InstD _ (DataFamInstD _ (DataFamInstDecl (HsIB { hsib_body = d })))
451-
-> dataSubs (feqn_rhs d)
452-
TyClD _ d | isClassDecl d -> classSubs d
453-
| isDataDecl d -> dataSubs (tcdDataDefn d)
454-
_ -> []
455-
where
456-
classSubs dd = [ (name, doc, declTypeDocs d) | (L _ d, doc) <- classDecls dd
457-
, name <- getMainDeclBinder d, not (isValD d)
458-
]
459-
dataSubs :: HsDataDefn GhcRn -> [(Name, [HsDocString], Map Int HsDocString)]
460-
dataSubs dd = constrs ++ fields ++ derivs
461-
where
462-
cons = map unL $ (dd_cons dd)
463-
constrs = [ (unL cname, maybeToList $ fmap unL $ con_doc c, conArgDocs c)
464-
| c <- cons, cname <- getConNames c ]
465-
fields = [ (extFieldOcc n, maybeToList $ fmap unL doc, M.empty)
466-
| RecCon flds <- map getConArgs cons
467-
, L _ (ConDeclField _ ns _ doc) <- (unLoc flds)
468-
, L _ n <- ns ]
469-
derivs = [ (instName, [unL doc], M.empty)
470-
| (l, doc) <- mapMaybe (extract_deriv_ty . hsib_body) $
471-
concatMap (unLoc . deriv_clause_tys . unLoc) $
472-
unLoc $ dd_derivs dd
473-
, Just instName <- [SrcLoc.lookupSrcSpan l instMap] ]
474-
475-
extract_deriv_ty :: LHsType GhcRn -> Maybe (SrcSpan, LHsDocString)
476-
extract_deriv_ty (L l ty) =
477-
case ty of
478-
-- deriving (forall a. C a {- ^ Doc comment -})
479-
HsForAllTy{ hst_fvf = ForallInvis
480-
, hst_body = L _ (HsDocTy _ _ doc) }
481-
-> Just (l, doc)
482-
-- deriving (C a {- ^ Doc comment -})
483-
HsDocTy _ _ doc -> Just (l, doc)
484-
_ -> Nothing
485-
486-
-- | Extract constructor argument docs from inside constructor decls.
487-
conArgDocs :: ConDecl GhcRn -> Map Int HsDocString
488-
conArgDocs con = case getConArgs con of
489-
PrefixCon args -> go 0 (map unLoc args ++ ret)
490-
InfixCon arg1 arg2 -> go 0 ([unLoc arg1, unLoc arg2] ++ ret)
491-
RecCon _ -> go 1 ret
492-
where
493-
go n (HsDocTy _ _ (L _ ds) : tys) = M.insert n ds $ go (n+1) tys
494-
go n (HsBangTy _ _ (L _ (HsDocTy _ _ (L _ ds))) : tys) = M.insert n ds $ go (n+1) tys
495-
go n (_ : tys) = go (n+1) tys
496-
go _ [] = M.empty
497-
498-
ret = case con of
499-
ConDeclGADT { con_res_ty = res_ty } -> [ unLoc res_ty ]
500-
_ -> []
501-
502-
-- | Extract function argument docs from inside top-level decls.
503-
declTypeDocs :: HsDecl GhcRn -> Map Int HsDocString
504-
declTypeDocs (SigD _ (TypeSig _ _ ty)) = typeDocs (unLoc (hsSigWcType ty))
505-
declTypeDocs (SigD _ (ClassOpSig _ _ _ ty)) = typeDocs (unLoc (hsSigType ty))
506-
declTypeDocs (SigD _ (PatSynSig _ _ ty)) = typeDocs (unLoc (hsSigType ty))
507-
declTypeDocs (ForD _ (ForeignImport _ _ ty _)) = typeDocs (unLoc (hsSigType ty))
508-
declTypeDocs (TyClD _ (SynDecl { tcdRhs = ty })) = typeDocs (unLoc ty)
509-
declTypeDocs _ = M.empty
510-
511-
-- | Extract function argument docs from inside types.
512-
typeDocs :: HsType GhcRn -> Map Int HsDocString
513-
typeDocs = go 0
514-
where
515-
go n (HsForAllTy { hst_body = ty }) = go n (unLoc ty)
516-
go n (HsQualTy { hst_body = ty }) = go n (unLoc ty)
517-
go n (HsFunTy _ (L _ (HsDocTy _ _ (L _ x))) (L _ ty)) = M.insert n x $ go (n+1) ty
518-
go n (HsFunTy _ _ ty) = go (n+1) (unLoc ty)
519-
go n (HsDocTy _ _ (L _ doc)) = M.singleton n doc
520-
go _ _ = M.empty
521-
522-
-- | All the sub declarations of a class (that we handle), ordered by
523-
-- source location, with documentation attached if it exists.
524-
classDecls :: TyClDecl GhcRn -> [(LHsDecl GhcRn, [HsDocString])]
525-
classDecls class_ = filterDecls . collectDocs . SrcLoc.sortLocated $ decls
526-
where
527-
decls = docs ++ defs ++ sigs ++ ats
528-
docs = mkDecls tcdDocs (DocD noExtField) class_
529-
defs = mkDecls (bagToList . tcdMeths) (ValD noExtField) class_
530-
sigs = mkDecls tcdSigs (SigD noExtField) class_
531-
ats = mkDecls tcdATs (TyClD noExtField . FamDecl noExtField) class_
532-
533-
534-
-- | The top-level declarations of a module that we care about,
535-
-- ordered by source location, with documentation attached if it exists.
536-
topDecls :: HsGroup GhcRn -> [(LHsDecl GhcRn, [HsDocString])]
537-
topDecls =
538-
filterClasses . filterDecls . collectDocs . SrcLoc.sortLocated . ungroup
539441

540442
-- | Extract a map of fixity declarations only
541443
mkFixMap :: HsGroup GhcRn -> FixMap
@@ -545,86 +447,6 @@ mkFixMap group_ =
545447
L _ n <- ns ]
546448

547449

548-
-- | Take all declarations except pragmas, infix decls, rules from an 'HsGroup'.
549-
ungroup :: HsGroup GhcRn -> [LHsDecl GhcRn]
550-
ungroup group_ =
551-
mkDecls (tyClGroupTyClDecls . hs_tyclds) (TyClD noExtField) group_ ++
552-
mkDecls hs_derivds (DerivD noExtField) group_ ++
553-
mkDecls hs_defds (DefD noExtField) group_ ++
554-
mkDecls hs_fords (ForD noExtField) group_ ++
555-
mkDecls hs_docs (DocD noExtField) group_ ++
556-
mkDecls (tyClGroupInstDecls . hs_tyclds) (InstD noExtField) group_ ++
557-
mkDecls (typesigs . hs_valds) (SigD noExtField) group_ ++
558-
mkDecls (valbinds . hs_valds) (ValD noExtField) group_
559-
where
560-
typesigs (XValBindsLR (NValBinds _ sigs)) = filter isUserLSig sigs
561-
typesigs _ = error "expected ValBindsOut"
562-
563-
valbinds (XValBindsLR (NValBinds binds _)) = concatMap bagToList . snd . unzip $ binds
564-
valbinds _ = error "expected ValBindsOut"
565-
566-
567-
-- | Take a field of declarations from a data structure and create HsDecls
568-
-- using the given constructor
569-
mkDecls :: (a -> [Located b]) -> (b -> c) -> a -> [Located c]
570-
mkDecls field con struct = [ L loc (con decl) | L loc decl <- field struct ]
571-
572-
--------------------------------------------------------------------------------
573-
-- Filtering of declarations
574-
--
575-
-- We filter out declarations that we don't intend to handle later.
576-
--------------------------------------------------------------------------------
577-
578-
579-
-- | Filter out declarations that we don't handle in Haddock
580-
filterDecls :: [(LHsDecl a, doc)] -> [(LHsDecl a, doc)]
581-
filterDecls = filter (isHandled . unL . fst)
582-
where
583-
isHandled (ForD _ (ForeignImport {})) = True
584-
isHandled (TyClD {}) = True
585-
isHandled (InstD {}) = True
586-
isHandled (DerivD {}) = True
587-
isHandled (SigD _ d) = isUserLSig (reL d)
588-
isHandled (ValD {}) = True
589-
-- we keep doc declarations to be able to get at named docs
590-
isHandled (DocD {}) = True
591-
isHandled _ = False
592-
593-
-- | Go through all class declarations and filter their sub-declarations
594-
filterClasses :: [(LHsDecl a, doc)] -> [(LHsDecl a, doc)]
595-
filterClasses decls = [ if isClassD d then (L loc (filterClass d), doc) else x
596-
| x@(L loc d, doc) <- decls ]
597-
where
598-
filterClass (TyClD x c) =
599-
TyClD x $ c { tcdSigs = filter (liftA2 (||) isUserLSig isMinimalLSig) $ tcdSigs c }
600-
filterClass _ = error "expected TyClD"
601-
602-
603-
--------------------------------------------------------------------------------
604-
-- Collect docs
605-
--
606-
-- To be able to attach the right Haddock comment to the right declaration,
607-
-- we sort the declarations by their SrcLoc and "collect" the docs for each
608-
-- declaration.
609-
--------------------------------------------------------------------------------
610-
611-
612-
-- | Collect docs and attach them to the right declarations.
613-
collectDocs :: [LHsDecl a] -> [(LHsDecl a, [HsDocString])]
614-
collectDocs = go Nothing []
615-
where
616-
go Nothing _ [] = []
617-
go (Just prev) docs [] = finished prev docs []
618-
go prev docs (L _ (DocD _ (DocCommentNext str)) : ds)
619-
| Nothing <- prev = go Nothing (str:docs) ds
620-
| Just decl <- prev = finished decl docs (go Nothing [str] ds)
621-
go prev docs (L _ (DocD _ (DocCommentPrev str)) : ds) = go prev (str:docs) ds
622-
go Nothing docs (d:ds) = go (Just d) docs ds
623-
go (Just prev) docs (d:ds) = finished prev docs (go (Just d) [] ds)
624-
625-
finished decl docs rest = (decl, reverse docs) : rest
626-
627-
628450
-- | Build the list of items that will become the documentation, from the
629451
-- export list. At this point, the list of ExportItems is in terms of
630452
-- original names.

haddock-api/src/Haddock/Interface/Rename.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Control.Monad hiding (mapM)
3131
import Data.List
3232
import qualified Data.Map as Map hiding ( Map )
3333
import Prelude hiding (mapM)
34+
import GHC.HsToCore.Docs
3435

3536
renameInterface :: DynFlags -> LinkEnv -> Bool -> Interface -> ErrMsgM Interface
3637
renameInterface dflags renamingEnv warnings iface =

0 commit comments

Comments
 (0)