[Git][ghc/ghc][wip/fix-26700] 2 commits: Adding TTG extension point for 'CType'
recursion-ninja pushed to branch wip/fix-26700 at Glasgow Haskell Compiler / GHC Commits: 740d2f68 by Recursion Ninja at 2026-02-06T10:56:27-05:00 Adding TTG extension point for 'CType' - - - - - 0c23fdd0 by Recursion Ninja at 2026-02-06T11:07:19-05:00 Refactoring module name; correcting test-suite dependency counting test cases - - - - - 22 changed files: - compiler/GHC/Builtin/Types.hs - compiler/GHC/Core/TyCon.hs - compiler/GHC/Hs/Decls.hs - compiler/GHC/Hs/Instances.hs - compiler/GHC/HsToCore/Foreign/C.hs - compiler/GHC/Iface/Syntax.hs - compiler/GHC/Parser.y - compiler/GHC/Parser/PostProcess.hs - compiler/GHC/Rename/Module.hs - compiler/GHC/Tc/TyCl.hs - compiler/GHC/Tc/TyCl/Instance.hs - compiler/GHC/Tc/Types/ErrCtxt.hs - compiler/GHC/Types/ForeignCall.hs - compiler/Language/Haskell/Syntax/Decls.hs - compiler/Language/Haskell/Syntax/Decls/ForeignCall.hs → compiler/Language/Haskell/Syntax/Decls/Foreign.hs - compiler/Language/Haskell/Syntax/Extension.hs - compiler/ghc.cabal.in - testsuite/tests/count-deps/CountDepsAst.stdout - testsuite/tests/count-deps/CountDepsParser.stdout - utils/check-exact/ExactPrint.hs - utils/haddock/haddock-api/src/Haddock/Interface/Rename.hs - utils/haddock/haddock-api/src/Haddock/Types.hs Changes: ===================================== compiler/GHC/Builtin/Types.hs ===================================== @@ -189,6 +189,8 @@ import GHC.Core.Class ( Class, mkClass ) import GHC.Core.Map.Type ( TypeMap, emptyTypeMap, extendTypeMap, lookupTypeMap ) import qualified GHC.Core.TyCo.Rep as TyCoRep ( Type(TyConApp) ) +import GHC.Hs.Extension (GhcTc) + import GHC.Types.TyThing import GHC.Types.SourceText import GHC.Types.Var ( VarBndr (Bndr), tyVarName ) @@ -617,7 +619,7 @@ consDataCon_RDR = nameRdrName consDataConName -- This function assumes that the types it creates have all parameters at -- Representational role, and that there is no kind polymorphism. -pcTyCon :: Name -> Maybe CType -> [TyVar] -> [DataCon] -> TyCon +pcTyCon :: Name -> Maybe (CType GhcTc) -> [TyVar] -> [DataCon] -> TyCon pcTyCon name cType tyvars cons = mkAlgTyCon name (mkTyConKind bndrs res_kind) bndrs 0 res_kind (map (const Representational) tyvars) @@ -2301,14 +2303,17 @@ int8ElemRepDataConTy, int16ElemRepDataConTy, int32ElemRepDataConTy, * * ********************************************************************* -} +-- | Generic constructor for primative C Types. +mkCTypeCon :: Name -> String -> [DataCon] -> TyCon +mkCTypeCon cName str = + pcTyCon cName (Just (defaultCType str)) [] + charTy :: Type charTy = mkTyConTy charTyCon charTyCon :: TyCon -charTyCon = pcTyCon charTyConName - (Just (CType NoSourceText Nothing - (NoSourceText,fsLit "HsChar"))) - [] [charDataCon] +charTyCon = mkCTypeCon charTyConName "HsChar" [charDataCon] + charDataCon :: DataCon charDataCon = pcDataCon charDataConName [] [charPrimTy] charTyCon @@ -2327,9 +2332,8 @@ intTy :: Type intTy = mkTyConTy intTyCon intTyCon :: TyCon -intTyCon = pcTyCon intTyConName - (Just (CType NoSourceText Nothing (NoSourceText,fsLit "HsInt"))) - [] [intDataCon] +intTyCon = mkCTypeCon intTyConName "HsInt" [intDataCon] + intDataCon :: DataCon intDataCon = pcDataCon intDataConName [] [intPrimTy] intTyCon @@ -2337,9 +2341,8 @@ wordTy :: Type wordTy = mkTyConTy wordTyCon wordTyCon :: TyCon -wordTyCon = pcTyCon wordTyConName - (Just (CType NoSourceText Nothing (NoSourceText, fsLit "HsWord"))) - [] [wordDataCon] +wordTyCon = mkCTypeCon wordTyConName "HsWord" [wordDataCon] + wordDataCon :: DataCon wordDataCon = pcDataCon wordDataConName [] [wordPrimTy] wordTyCon @@ -2347,10 +2350,8 @@ word8Ty :: Type word8Ty = mkTyConTy word8TyCon word8TyCon :: TyCon -word8TyCon = pcTyCon word8TyConName - (Just (CType NoSourceText Nothing - (NoSourceText, fsLit "HsWord8"))) [] - [word8DataCon] +word8TyCon = mkCTypeCon word8TyConName "HsWord8" [word8DataCon] + word8DataCon :: DataCon word8DataCon = pcDataCon word8DataConName [] [word8PrimTy] word8TyCon @@ -2358,21 +2359,16 @@ floatTy :: Type floatTy = mkTyConTy floatTyCon floatTyCon :: TyCon -floatTyCon = pcTyCon floatTyConName - (Just (CType NoSourceText Nothing - (NoSourceText, fsLit "HsFloat"))) [] - [floatDataCon] +floatTyCon = mkCTypeCon floatTyConName "HsFloat" [floatDataCon] + floatDataCon :: DataCon -floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon +floatDataCon = pcDataCon floatDataConName [] [floatPrimTy] floatTyCon doubleTy :: Type doubleTy = mkTyConTy doubleTyCon doubleTyCon :: TyCon -doubleTyCon = pcTyCon doubleTyConName - (Just (CType NoSourceText Nothing - (NoSourceText,fsLit "HsDouble"))) [] - [doubleDataCon] +doubleTyCon = mkCTypeCon doubleTyConName "HsDouble" [doubleDataCon] doubleDataCon :: DataCon doubleDataCon = pcDataCon doubleDataConName [] [doublePrimTy] doubleTyCon @@ -2565,10 +2561,7 @@ boolTy :: Type boolTy = mkTyConTy boolTyCon boolTyCon :: TyCon -boolTyCon = pcTyCon boolTyConName - (Just (CType NoSourceText Nothing - (NoSourceText, fsLit "HsBool"))) - [] [falseDataCon, trueDataCon] +boolTyCon = mkCTypeCon boolTyConName "HsBool" [falseDataCon, trueDataCon] falseDataCon, trueDataCon :: DataCon falseDataCon = pcDataCon falseDataConName [] [] boolTyCon ===================================== compiler/GHC/Core/TyCon.hs ===================================== @@ -158,6 +158,8 @@ import GHC.Builtin.Uniques ( tyConRepNameUnique , dataConTyRepNameUnique ) +import GHC.Hs.Extension (GhcTc) + import GHC.Utils.Binary import GHC.Types.Var import GHC.Types.Var.Set @@ -822,7 +824,8 @@ data TyConDetails = -- Note that it does /not/ scope over the data -- constructors. - tyConCType :: Maybe CType,-- ^ The C type that should be used + tyConCType :: Maybe (CType GhcTc), + -- ^ The C type that should be used -- for this type when using the FFI -- and CAPI @@ -2066,18 +2069,18 @@ well be different from 'mkTyConKind binders res_kind'. -- | This is the making of an algebraic 'TyCon'. mkAlgTyCon :: Name - -> Kind -- ^ TyCon kind - -> [TyConBinder] -- ^ Binders of the 'TyCon' - -> Int -- ^ Number of binders introduced by eta expansion - -> Kind -- ^ Result kind - -> [Role] -- ^ The roles for each TyVar - -> Maybe CType -- ^ The C type this type corresponds to - -- when using the CAPI FFI - -> [PredType] -- ^ Stupid theta: see 'algTcStupidTheta' - -> AlgTyConRhs -- ^ Information about data constructors - -> AlgTyConFlav -- ^ What flavour is it? - -- (e.g. vanilla, type family) - -> Bool -- ^ Was the 'TyCon' declared with GADT syntax? + -> Kind -- ^ TyCon kind + -> [TyConBinder] -- ^ Binders of the 'TyCon' + -> Int -- ^ Number of binders introduced by eta expansion + -> Kind -- ^ Result kind + -> [Role] -- ^ The roles for each TyVar + -> Maybe (CType GhcTc) -- ^ The C type this type corresponds to + -- when using the CAPI FFI + -> [PredType] -- ^ Stupid theta: see 'algTcStupidTheta' + -> AlgTyConRhs -- ^ Information about data constructors + -> AlgTyConFlav -- ^ What flavour is it? + -- (e.g. vanilla, type family) + -> Bool -- ^ Was the 'TyCon' declared with GADT syntax? -> TyCon mkAlgTyCon name kind binders nb_eta_bndrs res_kind roles cType stupid rhs parent gadt_syn = mkTyCon name kind binders nb_eta_bndrs res_kind roles $ @@ -2708,7 +2711,7 @@ isImplicitTyCon (TyCon { tyConName = name, tyConDetails = details }) = go detail | SumTyCon {} <- rhs = True | otherwise = False -tyConCType_maybe :: TyCon -> Maybe CType +tyConCType_maybe :: TyCon -> Maybe (CType GhcTc) tyConCType_maybe (TyCon { tyConDetails = details }) | AlgTyCon { tyConCType = mb_ctype} <- details = mb_ctype | otherwise = Nothing ===================================== compiler/GHC/Hs/Decls.hs ===================================== @@ -102,7 +102,7 @@ import GHC.Prelude import Language.Haskell.Syntax.Binds import Language.Haskell.Syntax.Decls -import Language.Haskell.Syntax.Decls.ForeignCall +import Language.Haskell.Syntax.Decls.Foreign import Language.Haskell.Syntax.Decls.Overlap (OverlapMode(..)) import Language.Haskell.Syntax.Extension @@ -1483,7 +1483,7 @@ type instance Anno (FunDep (GhcPass p)) = SrcSpanAnnA type instance Anno (FamilyResultSig (GhcPass p)) = EpAnnCO type instance Anno (FamilyDecl (GhcPass p)) = SrcSpanAnnA type instance Anno (InjectivityAnn (GhcPass p)) = EpAnnCO -type instance Anno CType = SrcSpanAnnP +type instance Anno (CType (GhcPass p)) = SrcSpanAnnP type instance Anno (HsDerivingClause (GhcPass p)) = EpAnnCO type instance Anno (DerivClauseTys (GhcPass _)) = SrcSpanAnnC type instance Anno (StandaloneKindSig (GhcPass p)) = SrcSpanAnnA ===================================== compiler/GHC/Hs/Instances.hs ===================================== @@ -33,7 +33,7 @@ import GHC.Types.Name.Reader (WithUserRdr(..)) import GHC.Types.InlinePragma (ActivationGhc) import GHC.Data.BooleanFormula (BooleanFormula(..)) import Language.Haskell.Syntax.Decls -import Language.Haskell.Syntax.Decls.ForeignCall +import Language.Haskell.Syntax.Decls.Foreign import Language.Haskell.Syntax.Decls.Overlap (OverlapMode(..)) import Language.Haskell.Syntax.Extension (Anno) import Language.Haskell.Syntax.Binds.InlinePragma (ActivationX(..), InlinePragma(..)) @@ -255,6 +255,11 @@ deriving instance Data (CCallTarget GhcPs) deriving instance Data (CCallTarget GhcRn) deriving instance Data (CCallTarget GhcTc) +-- deriving instance (DataIdLR p p) => Data (CImportSpec p) +deriving instance Data (CType GhcPs) +deriving instance Data (CType GhcRn) +deriving instance Data (CType GhcTc) + -- deriving instance (DataIdLR p p) => Data (RuleDecls p) deriving instance Data (RuleDecls GhcPs) deriving instance Data (RuleDecls GhcRn) ===================================== compiler/GHC/HsToCore/Foreign/C.hs ===================================== @@ -347,7 +347,7 @@ toCType = f False -- Note that we aren't looking through type synonyms or -- anything, as it may be the synonym that is annotated. | Just tycon <- tyConAppTyConPicky_maybe t - , Just (CType _ mHeader (_,cType)) <- tyConCType_maybe tycon + , Just (CType _ mHeader cType) <- tyConCType_maybe tycon = (mHeader, ftext cType) -- If we don't know a C type for this type, then try looking -- through one layer of type synonym etc. ===================================== compiler/GHC/Iface/Syntax.hs ===================================== @@ -84,7 +84,7 @@ import GHC.Core.DataCon (SrcStrictness(..), SrcUnpackedness(..)) import GHC.Builtin.Types ( constraintKindTyConName ) import GHC.Stg.EnforceEpt.TagSig import GHC.Parser.Annotation (noLocA) -import GHC.Hs.Extension ( GhcRn ) +import GHC.Hs.Extension ( GhcPass, GhcRn, GhcTc ) import GHC.Hs.Decls.Overlap ( OverlapFlag ) import GHC.Hs.Doc ( WithHsDocIdentifiers(..) ) @@ -196,7 +196,7 @@ data IfaceDecl ifBinders :: [IfaceTyConBinder], ifNbEtaBinders :: Int, -- ^ number of binders introduced by eta-expansion ifResKind :: IfaceType, -- Result kind of type constructor - ifCType :: Maybe CType, -- C type for CAPI FFI + ifCType :: Maybe (CType GhcTc), -- C type for CAPI FFI ifRoles :: [Role], -- Roles ifCtxt :: IfaceContext, -- The "stupid theta" ifCons :: IfaceConDecls, -- Includes new/data/data family info @@ -1450,7 +1450,7 @@ pprIfaceDecl _ (IfaceAxiom { ifName = name, ifTyCon = tycon = hang (text "axiom" <+> ppr name <+> dcolon) 2 (vcat $ unzipWith (pprAxBranch (ppr tycon)) $ zip [0..] branches) -pprCType :: Maybe CType -> SDoc +pprCType :: Maybe (CType (GhcPass p)) -> SDoc pprCType Nothing = Outputable.empty pprCType (Just cType) = text "C type:" <+> ppr cType ===================================== compiler/GHC/Parser.y ===================================== @@ -1680,14 +1680,14 @@ datafam_inst_hdr :: { Located (Maybe (LHsContext GhcPs), HsOuterFamEqnTyVarBndrs | type { sL1 $1 (Nothing, mkHsOuterImplicit, $1) } -capi_ctype :: { Maybe (LocatedP CType) } +capi_ctype :: { Maybe (LocatedP (CType GhcPs)) } capi_ctype : '{-# CTYPE' STRING STRING '#-}' - {% fmap Just $ amsr (sLL $1 $> (CType (getCTYPEs $1) (Just (Header (getSTRINGs $2) (getSTRING $2))) - (getSTRINGs $3,getSTRING $3))) + {% fmap Just $ amsr (sLL $1 $> (mkCType (getCTYPEs $1) (getSTRINGs $3) (Just (Header (getSTRINGs $2) (getSTRING $2))) + (getSTRING $3))) (AnnPragma (glR $1) (epTok $4) noAnn (glR $2) (glR $3) noAnn noAnn) } | '{-# CTYPE' STRING '#-}' - {% fmap Just $ amsr (sLL $1 $> (CType (getCTYPEs $1) Nothing (getSTRINGs $2, getSTRING $2))) + {% fmap Just $ amsr (sLL $1 $> (mkCType (getCTYPEs $1) (getSTRINGs $2) Nothing (getSTRING $2))) (AnnPragma (glR $1) (epTok $3) noAnn noAnn (glR $2) noAnn noAnn) } | { Nothing } ===================================== compiler/GHC/Parser/PostProcess.hs ===================================== @@ -229,7 +229,7 @@ mkClassDecl loc' (L _ (mcxt, tycl_hdr)) fds where_cls layout annsIn mkTyData :: SrcSpan -> Bool -> NewOrData - -> Maybe (LocatedP CType) + -> Maybe (LocatedP (CType GhcPs)) -> Located (Maybe (LHsContext GhcPs), LHsType GhcPs) -> Maybe (LHsKind GhcPs) -> [LConDecl GhcPs] @@ -250,7 +250,7 @@ mkTyData loc' is_type_data new_or_data cType (L _ (mcxt, tycl_hdr)) tcdFixity = fixity, tcdDataDefn = defn })) } -mkDataDefn :: Maybe (LocatedP CType) +mkDataDefn :: Maybe (LocatedP (CType GhcPs)) -> Maybe (LHsContext GhcPs) -> Maybe (LHsKind GhcPs) -> DataDefnCons (LConDecl GhcPs) @@ -325,7 +325,7 @@ mkTyFamInstEqn loc bndrs lhs rhs annEq mkDataFamInst :: SrcSpan -> NewOrData - -> Maybe (LocatedP CType) + -> Maybe (LocatedP (CType GhcPs)) -> (Maybe ( LHsContext GhcPs), HsOuterFamEqnTyVarBndrs GhcPs , LHsType GhcPs) -> Maybe (LHsKind GhcPs) ===================================== compiler/GHC/Rename/Module.hs ===================================== @@ -2029,13 +2029,15 @@ rnDataDefn doc (HsDataDefn { dd_cType = cType, dd_ctxt = context, dd_cons = cond ; let all_fvs = fvs1 `plusFV` fvs3 `plusFV` con_fvs `plusFV` sig_fvs - ; return ( HsDataDefn { dd_ext = noAnn, dd_cType = cType + ; return ( HsDataDefn { dd_ext = noAnn, dd_cType = fmap rn_ctype <$> cType , dd_ctxt = context', dd_kindSig = m_sig' , dd_cons = condecls' , dd_derivs = derivs' } , all_fvs ) } where + rn_ctype :: CType GhcPs -> CType GhcRn + rn_ctype (CType x y z) = CType x y z h98_style = not $ anyLConIsGadt condecls -- Note [Stupid theta] rn_derivs ds ===================================== compiler/GHC/Tc/TyCl.hs ===================================== @@ -75,6 +75,7 @@ import GHC.Core.TyCon import GHC.Core.DataCon import GHC.Core.Unify +import GHC.Types.ForeignCall ( CType(..) ) import GHC.Types.Id import GHC.Types.Id.Make import GHC.Types.Var @@ -3598,12 +3599,13 @@ tcDataDefn err_ctxt roles_info tc_name { data_cons <- tcConDecls DDataType rec_tycon tc_bndrs res_kind cons ; tc_rhs <- mk_tc_rhs hsc_src rec_tycon data_cons ; tc_rep_nm <- newTyConRepName tc_name - + ; let tc_ctype :: CType GhcRn -> CType GhcTc + tc_ctype (CType x y z) = CType x y z ; return (mkAlgTyCon tc_name kind bndrs nb_eta res_kind (roles_info tc_name) - (fmap unLoc cType) + (fmap (tc_ctype . unLoc) cType) stupid_theta tc_rhs (VanillaAlgTyCon tc_rep_nm) gadt_syntax) ===================================== compiler/GHC/Tc/TyCl/Instance.hs ===================================== @@ -69,6 +69,7 @@ import GHC.Types.Var as Var import GHC.Types.Var.Env import GHC.Types.Var.Set import GHC.Types.Basic +import GHC.Types.ForeignCall ( CType(..) ) import GHC.Types.Id import GHC.Types.InlinePragma import GHC.Types.SourceFile @@ -826,11 +827,13 @@ tcDataFamInstDecl mb_clsinfo tv_skol_env -- NB: Use the full ty_binders from the pats. See bullet toward -- the end of Note [Data type families] in GHC.Core.TyCon + tc_ctype :: CType GhcRn -> CType GhcTc + tc_ctype (CType x y z) = CType x y z rep_tc = mkAlgTyCon rep_tc_name user_kind ty_binders (length extra_tcbs) res_kind (map (const Nominal) ty_binders) - (fmap unLoc cType) stupid_theta + (fmap (tc_ctype . unLoc) cType) stupid_theta tc_rhs parent gadt_syntax -- We always assume that indexed types are recursive. Why? ===================================== compiler/GHC/Tc/Types/ErrCtxt.hs ===================================== @@ -39,7 +39,7 @@ import GHC.Utils.Outputable ( Outputable(..) ) import Language.Haskell.Syntax import Language.Haskell.Syntax.Basic ( FieldLabelString(..) ) -import Language.Haskell.Syntax.Decls.ForeignCall ( ForeignDecl(..) ) +import Language.Haskell.Syntax.Decls.Foreign ( ForeignDecl(..) ) import GHC.Boot.TH.Syntax qualified as TH import qualified Data.List.NonEmpty as NE ===================================== compiler/GHC/Types/ForeignCall.hs ===================================== @@ -21,19 +21,69 @@ To be resolved at a later time, see TODO at the end of this module. -} module GHC.Types.ForeignCall ( - ForeignCall(..), - isSafeForeignCall, - Safety(..), playSafe, playInterruptible, - - CExportSpec(..), CLabelString, isCLabelString, pprCLabelString, - CCallSpec(..), - CCallTarget(..), isDynamicTarget, - CCallConv(..), defaultCCallConv, ccallConvAttribute, - ForeignKind(..), - - Header(..), CType(..), - StaticTargetGhc(..), - ) where + -- * Foreign function interface declarations + -- ** Data-type + ForeignDecl(..), + -- ** Record synonym + LForeignDecl, + + -- * Foreign call + ForeignCall(..), + -- ** Queries + isSafeForeignCall, + -- ** CCallSpec + CCallSpec(..), + + -- * Foreign export types + -- ** Data-type + ForeignExport(..), + -- ** Specification + CExportSpec(..), + + -- * Foreign import types + -- ** Data-type + ForeignImport(..), + -- ** Call target + CCallTarget(..), + -- *** GHC extension point + StaticTargetGhc(..), + -- *** Queries + isDynamicTarget, + -- ** Foreign target kind + ForeignKind(..), + -- ** Safety + Safety(..), + -- *** Queries + playSafe, + playInterruptible, + -- ** Specification + CImportSpec(..), + + -- * Foreign binding type + -- ** Data-type + CType(..), + -- *** Construction + defaultCType, + mkCType, + -- *** GHC extension point + CTypeGhc(..), + + -- * General sub-types + -- ** CCallConv + CCallConv(..), + -- *** Default construction + defaultCCallConv, + -- *** Pretty-printing + ccallConvAttribute, + -- ** CLabelString + CLabelString, + -- *** Queries + isCLabelString, + -- *** Pretty-printing + pprCLabelString, + -- ** Header + Header(..), + ) where import GHC.Prelude @@ -42,17 +92,16 @@ import GHC.Hs.Extension import GHC.Utils.Binary import GHC.Utils.Outputable import GHC.Utils.Panic -import GHC.Types.SourceText (SourceText, pprWithSourceText) +import GHC.Types.SourceText (SourceText(..), pprWithSourceText) import GHC.Unit.Types -import Language.Haskell.Syntax.Decls.ForeignCall - (CCallConv(..), CCallTarget(..), CExportSpec(..), CLabelString, - CType(..), ForeignKind(..), Header(..), Safety(..)) +import Language.Haskell.Syntax.Decls.Foreign import Language.Haskell.Syntax.Extension import Data.Char import Data.Data (Data) import Data.Functor ((<&>)) +import Data.String (fromString) import Control.DeepSeq (NFData(..)) @@ -158,6 +207,14 @@ instance forall p. IsPass p => Outputable (CCallSpec (GhcPass p)) where <> ppr label <+> (pprWithSourceText srcTxt empty) +defaultCType :: String -> CType (GhcPass p) +defaultCType = + CType (CTypeGhc NoSourceText NoSourceText) Nothing . fromString + +mkCType :: SourceText -> SourceText -> Maybe Header -> FastString -> CType (GhcPass p) +mkCType x y m = + CType (CTypeGhc x y) m + {- ************************************************************************ * * @@ -233,12 +290,42 @@ data StaticTargetGhc = StaticTargetGhc } deriving (Data, Eq) +data CTypeGhc = CTypeGhc + { cTypeSourceText :: SourceText + , cTypeOtherText :: SourceText + } + deriving (Data, Eq) + type instance XStaticTarget GhcPs = SourceText type instance XStaticTarget GhcRn = StaticTargetGhc type instance XStaticTarget GhcTc = StaticTargetGhc type instance XDynamicTarget (GhcPass p) = NoExtField type instance XXCCallTarget (GhcPass p) = DataConCantHappen +type instance XCType (GhcPass p) = CTypeGhc +type instance XXCType (GhcPass p) = DataConCantHappen + +instance {-# OVERLAPPING #-} NFData (CType (GhcPass p)) where + rnf (CType ext mh fs) = + rnf ext `seq` rnf mh `seq` rnf fs + +instance NFData CTypeGhc where + rnf st = + rnf (cTypeSourceText st) `seq` + rnf (cTypeOtherText st) + +instance Binary CTypeGhc where + put_ bh ct = do + put_ bh (cTypeSourceText ct) + put_ bh (cTypeOtherText ct) + get bh = do + str1 <- get bh + str2 <- get bh + return $ CTypeGhc + { cTypeSourceText = str1 + , cTypeOtherText = str2 + } + instance NFData StaticTargetGhc where rnf st = rnf (staticTargetLabel st) `seq` @@ -258,24 +345,24 @@ instance Binary StaticTargetGhc where } instance {-# OVERLAPPING #-} forall p. IsPass p => Eq (CCallTarget (GhcPass p)) where - (==) = \case - DynamicTarget{} -> \case - DynamicTarget{} -> True - _ -> False - StaticTarget x1 a1 b1 -> \case - StaticTarget x2 a2 b2 -> a1 == a2 && b1 == b2 && case ghcPass @p of - GhcPs -> x1 == x2 - GhcRn -> x1 == x2 - GhcTc -> x1 == x2 - _ -> False + (==) = \case + DynamicTarget{} -> \case + DynamicTarget{} -> True + _ -> False + StaticTarget x1 a1 b1 -> \case + StaticTarget x2 a2 b2 -> a1 == a2 && b1 == b2 && case ghcPass @p of + GhcPs -> x1 == x2 + GhcRn -> x1 == x2 + GhcTc -> x1 == x2 + _ -> False instance forall p. IsPass p => NFData (CCallTarget (GhcPass p)) where - rnf = \case - DynamicTarget NoExtField -> () - StaticTarget x a b -> rnf a `seq` rnf b `seq` case ghcPass @p of - GhcPs -> rnf x - GhcRn -> rnf x - GhcTc -> rnf x + rnf = \case + DynamicTarget NoExtField -> () + StaticTarget x a b -> rnf a `seq` rnf b `seq` case ghcPass @p of + GhcPs -> rnf x + GhcRn -> rnf x + GhcTc -> rnf x instance forall p. IsPass p => Binary (CCallTarget (GhcPass p)) where put_ bh = \case @@ -312,16 +399,16 @@ instance Binary CExportSpec where ab <- get bh return (CExportStatic aa ab) -instance Binary CType where - put_ bh (CType s mh fs) = do - put_ bh s - put_ bh mh - put_ bh fs +instance Binary (CType (GhcPass p)) where + put_ bh (CType ext mh fs) = do + put_ bh ext + put_ bh mh + put_ bh fs get bh = do - s <- get bh - mh <- get bh - fs <- get bh - return (CType s mh fs) + ext <- get bh + mh <- get bh + fs <- get bh + return (CType ext mh fs) instance Binary ForeignKind where put_ bh = putByte bh . \case @@ -352,27 +439,30 @@ instance Binary Safety where _ -> return PlayRisky instance Outputable CCallConv where - ppr StdCallConv = text "stdcall" - ppr CCallConv = text "ccall" - ppr CApiConv = text "capi" - ppr PrimCallConv = text "prim" - ppr JavaScriptCallConv = text "javascript" + ppr StdCallConv = text "stdcall" + ppr CCallConv = text "ccall" + ppr CApiConv = text "capi" + ppr PrimCallConv = text "prim" + ppr JavaScriptCallConv = text "javascript" instance Outputable CExportSpec where - ppr (CExportStatic str _) = pprCLabelString str - -instance Outputable CType where - ppr (CType stp mh (stct,ct)) - = pprWithSourceText stp (text "{-# CTYPE") <+> hDoc - <+> pprWithSourceText stct (doubleQuotes (ftext ct)) <+> text "#-}" - where hDoc = case mh of - Nothing -> empty - Just h -> ppr h + ppr (CExportStatic str _) = pprCLabelString str + +instance Outputable (CType (GhcPass p)) where + ppr (CType ext mh ct) = + pprWithSourceText stp (text "{-# CTYPE") <+> hDoc <+> + pprWithSourceText stct (doubleQuotes (ftext ct)) <+> text "#-}" + where + stp = cTypeSourceText ext + stct = cTypeOtherText ext + hDoc = case mh of + Nothing -> empty + Just h -> ppr h instance Outputable Header where ppr (Header st h) = pprWithSourceText st (doubleQuotes $ ppr h) instance Outputable Safety where - ppr PlaySafe = text "safe" - ppr PlayInterruptible = text "interruptible" - ppr PlayRisky = text "unsafe" + ppr PlaySafe = text "safe" + ppr PlayInterruptible = text "interruptible" + ppr PlayRisky = text "unsafe" ===================================== compiler/Language/Haskell/Syntax/Decls.hs ===================================== @@ -97,9 +97,9 @@ import {-# SOURCE #-} Language.Haskell.Syntax.Expr import Language.Haskell.Syntax.Basic (LexicalFixity, Role, RuleName, TopLevelFlag) import Language.Haskell.Syntax.Binds +import Language.Haskell.Syntax.Decls.Foreign import Language.Haskell.Syntax.Binds.InlinePragma (Activation) import Language.Haskell.Syntax.Decls.Overlap (OverlapMode) -import Language.Haskell.Syntax.Decls.ForeignCall import Language.Haskell.Syntax.Extension import Language.Haskell.Syntax.Specificity (Specificity) import Language.Haskell.Syntax.Type @@ -808,7 +808,7 @@ data HsDataDefn pass -- The payload of a data type defn -- @ HsDataDefn { dd_ext :: XCHsDataDefn pass, dd_ctxt :: Maybe (LHsContext pass), -- ^ Context - dd_cType :: Maybe (XRec pass CType), + dd_cType :: Maybe (XRec pass (CType pass)), dd_kindSig:: Maybe (LHsKind pass), -- ^ Optional kind signature. -- ===================================== compiler/Language/Haskell/Syntax/Decls/ForeignCall.hs → compiler/Language/Haskell/Syntax/Decls/Foreign.hs ===================================== @@ -10,26 +10,58 @@ -- See Note [Language.Haskell.Syntax.* Hierarchy] for why not GHC.Hs.* --- | Abstract syntax of global declarations. --- --- Definitions for: @SynDecl@ and @ConDecl@, @ClassDecl@, --- @InstDecl@, @DefaultDecl@ and @ForeignDecl@. -module Language.Haskell.Syntax.Decls.ForeignCall ( - -- ** Foreign function interface declarationss - ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..), ForeignKind(..), - CCallConv(..), CCallTarget(..), CExportSpec(..), CImportSpec(..), CLabelString, - CType(..), Header(..), Safety(..), - - -- ** Extension - XCExport, - XCImport, - XDynamicTarget, +-- | +-- Abstract syntax of foreign function interface declarations. +module Language.Haskell.Syntax.Decls.Foreign ( + -- * Foreign function interface declarations + -- ** Data-type + ForeignDecl(..), + -- ** Record synonym + LForeignDecl, + + -- * Foreign export types + -- ** Data-type + ForeignExport(..), + -- ** Specification + CExportSpec(..), + + -- * Foreign import types + -- ** Data-type + ForeignImport(..), + -- ** Specification + CImportSpec(..), + -- ** Sub-types + CCallTarget(..), + ForeignKind(..), + Safety(..), + + -- * Foreign binding type + CType(..), + + -- * General sub-types + CLabelString, + CCallConv(..), + Header(..), + + -- * Extension points + -- ** ForeignDecl XForeignExport, XForeignImport, + XXForeignDecl, + -- ** ForeignExport + XCExport, + XXForeignExport, + -- ** ForeignImport + XCImport, + XXForeignImport, + -- ** CCallTarget XStaticTarget, + XDynamicTarget, XXCCallTarget, - XXForeignDecl, - ) where + -- ** CType + XCType, + XXCType, + ) where import Language.Haskell.Syntax.Extension import Language.Haskell.Syntax.Type @@ -193,18 +225,27 @@ data CExportSpec type CLabelString = FastString -- A C label, completely unencoded --- TODO: TTG - -- | A C type, used in CAPI FFI calls -data CType = CType SourceText -- To EXT point - -- ^ See Note [Pragma source text] in "GHC.Types.SourceText" - (Maybe Header) -- header to include for this type - ( SourceText -- TO EXT Point - , FastString) -- the type itself - deriving (Eq, Data) - -instance NFData CType where - rnf (CType s mh fs) = rnf s `seq` rnf mh `seq` rnf fs +data CType pass + = CType + (XCType pass) + (Maybe Header) -- header to include for this type + FastString + | XCType !(XXCType pass) + +deriving instance {-# OVERLAPPABLE #-} + ( Eq (XCType pass) + , Eq (XXCType pass) + ) => + Eq (CType pass) + +instance {-# OVERLAPPABLE #-} + ( NFData (XCType pass) + , NFData (XXCType pass) + ) => NFData (CType pass) where + rnf = \case + CType ext mh fs -> rnf ext `seq` rnf mh `seq` rnf fs + XCType ext -> rnf ext -- The filename for a C header file -- See Note [Pragma source text] in "GHC.Types.SourceText" ===================================== compiler/Language/Haskell/Syntax/Extension.hs ===================================== @@ -387,6 +387,11 @@ type family XStaticTarget x type family XDynamicTarget x type family XXCCallTarget x +-- ------------------------------------- +-- CType type family +type family XCType x +type family XXCType x + -- ------------------------------------- -- ForeignDecl type families type family XForeignImport x ===================================== compiler/ghc.cabal.in ===================================== @@ -1025,8 +1025,8 @@ Library Language.Haskell.Syntax.Binds.InlinePragma Language.Haskell.Syntax.BooleanFormula Language.Haskell.Syntax.Decls + Language.Haskell.Syntax.Decls.Foreign Language.Haskell.Syntax.Decls.Overlap - Language.Haskell.Syntax.Decls.ForeignCall Language.Haskell.Syntax.Expr Language.Haskell.Syntax.Extension Language.Haskell.Syntax.ImpExp ===================================== testsuite/tests/count-deps/CountDepsAst.stdout ===================================== @@ -242,6 +242,7 @@ Language.Haskell.Syntax.Binds Language.Haskell.Syntax.Binds.InlinePragma Language.Haskell.Syntax.BooleanFormula Language.Haskell.Syntax.Decls +Language.Haskell.Syntax.Decls.Foreign Language.Haskell.Syntax.Decls.Overlap Language.Haskell.Syntax.Expr Language.Haskell.Syntax.Extension ===================================== testsuite/tests/count-deps/CountDepsParser.stdout ===================================== @@ -271,6 +271,7 @@ Language.Haskell.Syntax.Binds Language.Haskell.Syntax.Binds.InlinePragma Language.Haskell.Syntax.BooleanFormula Language.Haskell.Syntax.Decls +Language.Haskell.Syntax.Decls.Foreign Language.Haskell.Syntax.Decls.Overlap Language.Haskell.Syntax.Expr Language.Haskell.Syntax.Extension ===================================== utils/check-exact/ExactPrint.hs ===================================== @@ -1901,10 +1901,10 @@ instance ExactPrint (ForeignExport GhcPs) where instance ExactPrint CExportSpec where getAnnotationEntry = const NoEntryVal setAnnotationAnchor a _ _ _ = a - exact (CExportStatic st lbl cconv) = do + exact (CExportStatic lbl cconv) = do debugM $ "CExportStatic starting" cconv' <- markAnnotated cconv - return (CExportStatic st lbl cconv') + return (CExportStatic lbl cconv') -- --------------------------------------------------------------------- @@ -4445,11 +4445,13 @@ exactBang ((o,c,tk), mt) str = do -- --------------------------------------------------------------------- -instance ExactPrint (LocatedP CType) where +instance Typeable p => ExactPrint (LocatedP (CType (GhcPass p))) where getAnnotationEntry = entryFromLocatedA setAnnotationAnchor = setAnchorAn - exact (L (EpAnn l (AnnPragma o c s l1 l2 t m) cs) (CType stp mh (stct,ct))) = do + exact (L (EpAnn l (AnnPragma o c s l1 l2 t m) cs) (CType ext mh ct)) = do + let stp = cTypeSourceText ext + stct = cTypeOtherText ext o' <- markAnnOpen'' o stp "{-# CTYPE" l1' <- case mh of Nothing -> return l1 @@ -4457,7 +4459,7 @@ instance ExactPrint (LocatedP CType) where printStringAtAA l1 (toSourceTextWithSuffix srcH "" "") l2' <- printStringAtAA l2 (toSourceTextWithSuffix stct (unpackFS ct) "") c' <- markEpToken c - return (L (EpAnn l (AnnPragma o' c' s l1' l2' t m) cs) (CType stp mh (stct,ct))) + return (L (EpAnn l (AnnPragma o' c' s l1' l2' t m) cs) (CType ext mh ct)) -- --------------------------------------------------------------------- ===================================== utils/haddock/haddock-api/src/Haddock/Interface/Rename.hs ===================================== @@ -39,6 +39,7 @@ import GHC.Core.TyCon (tyConResKind) import GHC.Driver.DynFlags (getDynFlags) import GHC.Hs.Decls.Overlap (OverlapMode(..)) import GHC.Types.Basic (TupleSort (..)) +import GHC.Types.ForeignCall import GHC.Types.Name import GHC.Types.Name.Reader (RdrName (Exact)) import Language.Haskell.Syntax.BooleanFormula(BooleanFormula(..)) @@ -674,13 +675,16 @@ renameDataDefn ( HsDataDefn { dd_ext = noExtField , dd_ctxt = lcontext' - , dd_cType = cType + , dd_cType = fmap renameCType <$> cType , dd_kindSig = k' , dd_cons = cons' , dd_derivs = [] } ) +renameCType :: CType GhcRn -> CType DocNameI +renameCType (CType _ y z) = CType NoExtField y z + renameCon :: ConDecl GhcRn -> RnM (ConDecl DocNameI) renameCon decl@( ConDeclH98 @@ -830,11 +834,22 @@ renameForD (ForeignExport _ lname ltype x) = do return (ForeignExport noExtField lname' ltype' (renameForE x)) renameForI :: ForeignImport GhcRn -> ForeignImport DocNameI -renameForI (CImport _ cconv safety mHeader spec) = CImport noExtField cconv safety mHeader spec +renameForI (CImport _ cconv safety mHeader spec) = CImport noExtField cconv safety mHeader (renameForISpec spec) renameForE :: ForeignExport GhcRn -> ForeignExport DocNameI renameForE (CExport _ spec) = CExport noExtField spec +renameForISpec :: CImportSpec GhcRn -> CImportSpec DocNameI +renameForISpec = \case + CLabel str -> CLabel str + CFunction cTarget -> CFunction $ renameCCallTarget cTarget + CWrapper -> CWrapper + +renameCCallTarget :: CCallTarget GhcRn -> CCallTarget DocNameI +renameCCallTarget = \case + DynamicTarget {} -> DynamicTarget NoExtField + StaticTarget _ cStr fKind -> StaticTarget NoExtField cStr fKind + renameInstD :: InstDecl GhcRn -> RnM (InstDecl DocNameI) renameInstD (ClsInstD{cid_inst = d}) = do d' <- renameClsInstD d ===================================== utils/haddock/haddock-api/src/Haddock/Types.hs ===================================== @@ -57,6 +57,7 @@ import GHC.Driver.Session (Language) import qualified GHC.LanguageExtensions as LangExt import GHC.Core.InstEnv (is_dfun_name) import GHC.Hs.Decls.Overlap (OverlapMode) +import GHC.Types.ForeignCall (CType) import GHC.Types.Name (stableNameCmp) import GHC.Types.Name.Occurrence import GHC.Types.Name.Reader (RdrName (..)) @@ -831,6 +832,7 @@ type instance Anno (HsOuterTyVarBndrs Specificity DocNameI) = SrcSpanAnnA type instance Anno (HsSigType DocNameI) = SrcSpanAnnA type instance Anno (BooleanFormula DocNameI) = SrcSpanAnnL type instance Anno (OverlapMode DocNameI) = EpAnn AnnPragma +type instance Anno (CType DocNameI) = EpAnn AnnPragma type XRecCond a = ( XParTy a ~ (EpToken "(", EpToken ")") @@ -925,6 +927,13 @@ type instance XCExport DocNameI = NoExtField type instance XXForeignImport DocNameI = DataConCantHappen type instance XXForeignExport DocNameI = DataConCantHappen +type instance XStaticTarget DocNameI = NoExtField +type instance XDynamicTarget DocNameI = NoExtField +type instance XXCCallTarget DocNameI = DataConCantHappen + +type instance XCType DocNameI = NoExtField +type instance XXCType DocNameI = DataConCantHappen + type instance XConDeclGADT DocNameI = NoExtField type instance XConDeclH98 DocNameI = NoExtField type instance XXConDecl DocNameI = DataConCantHappen View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/bf8c77a01fbb260be04cfd3438a2762... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/bf8c77a01fbb260be04cfd3438a2762... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
recursion-ninja (@recursion-ninja)