recursion-ninja pushed to branch wip/fix-26700 at Glasgow Haskell Compiler / GHC
Commits:
-
bf8c77a0
by Recursion Ninja at 2026-02-05T13:07:32-05:00
24 changed files:
- compiler/GHC/CoreToStg.hs
- compiler/GHC/Hs/Decls.hs
- compiler/GHC/Hs/Instances.hs
- compiler/GHC/HsToCore/Errors/Types.hs
- compiler/GHC/HsToCore/Foreign/C.hs
- compiler/GHC/HsToCore/Foreign/Call.hs
- compiler/GHC/HsToCore/Foreign/Decl.hs
- compiler/GHC/HsToCore/Foreign/JavaScript.hs
- compiler/GHC/HsToCore/Foreign/Wasm.hs
- compiler/GHC/HsToCore/Quote.hs
- compiler/GHC/Parser/PostProcess.hs
- compiler/GHC/Rename/Module.hs
- compiler/GHC/StgToByteCode.hs
- compiler/GHC/StgToCmm/Foreign.hs
- compiler/GHC/StgToJS/FFI.hs
- compiler/GHC/Tc/Errors/Types.hs
- compiler/GHC/Tc/Gen/Foreign.hs
- compiler/GHC/Tc/Types/ErrCtxt.hs
- compiler/GHC/ThToHs.hs
- compiler/GHC/Types/ForeignCall.hs
- compiler/Language/Haskell/Syntax/Decls.hs
- compiler/Language/Haskell/Syntax/Decls/ForeignCall.hs
- libraries/text
- libraries/transformers
Changes:
| ... | ... | @@ -554,7 +554,7 @@ mkStgApp f how_bound core_args stg_args res_ty |
| 554 | 554 | StgOpApp (StgPrimOp op) stg_args res_ty
|
| 555 | 555 | |
| 556 | 556 | -- A call to some primitive Cmm function.
|
| 557 | - FCallId (CCall (CCallSpec (StaticTarget ext lbl True)
|
|
| 557 | + FCallId (CCall (CCallSpec (StaticTarget ext lbl ForeignFunction)
|
|
| 558 | 558 | PrimCallConv _))
|
| 559 | 559 | -> assert exactly_saturated $
|
| 560 | 560 | StgOpApp (StgPrimCallOp (PrimCall lbl (staticTargetUnit ext))) stg_args res_ty
|
| ... | ... | @@ -102,6 +102,7 @@ import GHC.Prelude |
| 102 | 102 | |
| 103 | 103 | import Language.Haskell.Syntax.Binds
|
| 104 | 104 | import Language.Haskell.Syntax.Decls
|
| 105 | +import Language.Haskell.Syntax.Decls.ForeignCall
|
|
| 105 | 106 | import Language.Haskell.Syntax.Decls.Overlap (OverlapMode(..))
|
| 106 | 107 | import Language.Haskell.Syntax.Extension
|
| 107 | 108 | |
| ... | ... | @@ -1285,7 +1286,7 @@ instance forall p. (IsPass p, OutputableBndrId p) |
| 1285 | 1286 | |
| 1286 | 1287 | pprCEntity (CLabel lbl) _ =
|
| 1287 | 1288 | doubleQuotes $ text "static" <+> pp_hdr <+> char '&' <> ppr lbl
|
| 1288 | - pprCEntity (CFunction (StaticTarget stExt _ isFun)) src =
|
|
| 1289 | + pprCEntity (CFunction (StaticTarget stExt _ targetKind)) src =
|
|
| 1289 | 1290 | if dqNeeded then doubleQuotes ce else empty
|
| 1290 | 1291 | where
|
| 1291 | 1292 | st = case ghcPass @p of
|
| ... | ... | @@ -1294,13 +1295,13 @@ instance forall p. (IsPass p, OutputableBndrId p) |
| 1294 | 1295 | GhcTc -> staticTargetLabel stExt
|
| 1295 | 1296 | dqNeeded = (take 6 src == "static")
|
| 1296 | 1297 | || isJust mHeader
|
| 1297 | - || not isFun
|
|
| 1298 | + || targetKind == ForeignValue
|
|
| 1298 | 1299 | || st /= NoSourceText
|
| 1299 | 1300 | ce =
|
| 1300 | 1301 | -- We may need to drop leading spaces first
|
| 1301 | 1302 | (if take 6 src == "static" then text "static" else empty)
|
| 1302 | 1303 | <+> pp_hdr
|
| 1303 | - <+> (if isFun then empty else text "value")
|
|
| 1304 | + <+> (if targetKind == ForeignFunction then empty else text "value")
|
|
| 1304 | 1305 | <+> (pprWithSourceText st empty)
|
| 1305 | 1306 | pprCEntity (CFunction DynamicTarget{}) _ =
|
| 1306 | 1307 | doubleQuotes $ text "dynamic"
|
| ... | ... | @@ -33,6 +33,7 @@ import GHC.Types.Name.Reader (WithUserRdr(..)) |
| 33 | 33 | import GHC.Types.InlinePragma (ActivationGhc)
|
| 34 | 34 | import GHC.Data.BooleanFormula (BooleanFormula(..))
|
| 35 | 35 | import Language.Haskell.Syntax.Decls
|
| 36 | +import Language.Haskell.Syntax.Decls.ForeignCall
|
|
| 36 | 37 | import Language.Haskell.Syntax.Decls.Overlap (OverlapMode(..))
|
| 37 | 38 | import Language.Haskell.Syntax.Extension (Anno)
|
| 38 | 39 | import Language.Haskell.Syntax.Binds.InlinePragma (ActivationX(..), InlinePragma(..))
|
| ... | ... | @@ -13,6 +13,7 @@ import GHC.Driver.Flags (WarningFlag) |
| 13 | 13 | import GHC.Hs
|
| 14 | 14 | import GHC.HsToCore.Pmc.Solver.Types
|
| 15 | 15 | import GHC.Types.Error
|
| 16 | +import GHC.Types.ForeignCall (CLabelString)
|
|
| 16 | 17 | import GHC.Types.Id
|
| 17 | 18 | import GHC.Types.InlinePragma (ActivationGhc)
|
| 18 | 19 | import GHC.Types.Name (Name)
|
| ... | ... | @@ -68,7 +68,8 @@ dsCFExport:: Id -- Either the exported Id, |
| 68 | 68 | -- from C, and its representation type
|
| 69 | 69 | -> CLabelString -- The name to export to C land
|
| 70 | 70 | -> CCallConv
|
| 71 | - -> Bool -- True => foreign export dynamic
|
|
| 71 | + -> ForeignKind -- If it is a function,
|
|
| 72 | + -- then is foreign export dynamic
|
|
| 72 | 73 | -- so invoke IO action that's hanging off
|
| 73 | 74 | -- the first argument's stable pointer
|
| 74 | 75 | -> DsM ( CHeader -- contents of Module_stub.h
|
| ... | ... | @@ -76,29 +77,30 @@ dsCFExport:: Id -- Either the exported Id, |
| 76 | 77 | , String -- string describing type to pass to createAdj.
|
| 77 | 78 | )
|
| 78 | 79 | |
| 79 | -dsCFExport fn_id co ext_name cconv isDyn = do
|
|
| 80 | +dsCFExport fn_id co ext_name cconv target_kind = do
|
|
| 80 | 81 | let
|
| 81 | 82 | ty = coercionRKind co
|
| 82 | 83 | (bndrs, orig_res_ty) = tcSplitPiTys ty
|
| 83 | 84 | fe_arg_tys' = mapMaybe anonPiTyBinderType_maybe bndrs
|
| 84 | 85 | -- We must use tcSplits here, because we want to see
|
| 85 | 86 | -- the (IO t) in the corner of the type!
|
| 86 | - fe_arg_tys | isDyn = tail fe_arg_tys'
|
|
| 87 | - | otherwise = fe_arg_tys'
|
|
| 87 | + fe_arg_tys = case target_kind of
|
|
| 88 | + ForeignFunction -> tail fe_arg_tys'
|
|
| 89 | + _ -> fe_arg_tys'
|
|
| 88 | 90 | |
| 89 | 91 | -- Look at the result type of the exported function, orig_res_ty
|
| 90 | - -- If it's IO t, return (t, True)
|
|
| 91 | - -- If it's plain t, return (t, False)
|
|
| 92 | + -- If it's IO t, return (t, ForeignFunction)
|
|
| 93 | + -- If it's plain t, return (t, ForeignValue)
|
|
| 92 | 94 | (res_ty, is_IO_res_ty) = case tcSplitIOType_maybe orig_res_ty of
|
| 93 | 95 | -- The function already returns IO t
|
| 94 | - Just (_ioTyCon, res_ty) -> (res_ty, True)
|
|
| 96 | + Just (_ioTyCon, res_ty) -> (res_ty, ForeignFunction)
|
|
| 95 | 97 | -- The function returns t
|
| 96 | - Nothing -> (orig_res_ty, False)
|
|
| 98 | + Nothing -> (orig_res_ty, ForeignValue)
|
|
| 97 | 99 | |
| 98 | 100 | dflags <- getDynFlags
|
| 99 | 101 | return $
|
| 100 | 102 | mkFExportCBits dflags ext_name
|
| 101 | - (if isDyn then Nothing else Just fn_id)
|
|
| 103 | + (if target_kind == ForeignFunction then Nothing else Just fn_id)
|
|
| 102 | 104 | fe_arg_tys res_ty is_IO_res_ty cconv
|
| 103 | 105 | |
| 104 | 106 | dsCImport :: Id
|
| ... | ... | @@ -184,7 +186,7 @@ dsCFExportDynamic id co0 cconv = do |
| 184 | 186 | export_ty = mkVisFunTyMany stable_ptr_ty arg_ty
|
| 185 | 187 | bindIOId <- dsLookupGlobalId bindIOName
|
| 186 | 188 | stbl_value <- newSysLocalMDs stable_ptr_ty
|
| 187 | - (h_code, c_code, typestring) <- dsCFExport id (mkRepReflCo export_ty) fe_nm cconv True
|
|
| 189 | + (h_code, c_code, typestring) <- dsCFExport id (mkRepReflCo export_ty) fe_nm cconv ForeignFunction
|
|
| 188 | 190 | let
|
| 189 | 191 | {-
|
| 190 | 192 | The arguments to the external function which will
|
| ... | ... | @@ -258,14 +260,14 @@ dsFCall fn_id co fcall mDeclHeader = do |
| 258 | 260 | |
| 259 | 261 | (fcall', cDoc) <-
|
| 260 | 262 | case fcall of
|
| 261 | - CCall (CCallSpec (StaticTarget stExt cName isFun)
|
|
| 263 | + CCall (CCallSpec (StaticTarget stExt cName targetKind)
|
|
| 262 | 264 | CApiConv safety) ->
|
| 263 | 265 | do nextWrapperNum <- ds_next_wrapper_num <$> getGblEnv
|
| 264 | 266 | wrapperName <- mkWrapperName nextWrapperNum "ghc_wrapper" (unpackFS cName)
|
| 265 | 267 | let fcall' = CCall (CCallSpec
|
| 266 | 268 | (StaticTarget (stExt { staticTargetLabel = NoSourceText} )
|
| 267 | 269 | wrapperName
|
| 268 | - True)
|
|
| 270 | + ForeignFunction)
|
|
| 269 | 271 | CApiConv safety)
|
| 270 | 272 | c = includes
|
| 271 | 273 | $$ fun_proto <+> braces (cRet <> semi)
|
| ... | ... | @@ -274,10 +276,10 @@ dsFCall fn_id co fcall mDeclHeader = do |
| 274 | 276 | | Header _ h <- nub headers ]
|
| 275 | 277 | fun_proto = constQual <+> cResType <+> pprCconv <+> ppr wrapperName <> parens argTypes
|
| 276 | 278 | cRet
|
| 277 | - | isVoidRes = cCall
|
|
| 278 | - | otherwise = text "return" <+> cCall
|
|
| 279 | + | isVoidRes = cCall
|
|
| 280 | + | otherwise = text "return" <+> cCall
|
|
| 279 | 281 | cCall
|
| 280 | - | isFun = ppr cName <> parens argVals
|
|
| 282 | + | targetKind == ForeignFunction = ppr cName <> parens argVals
|
|
| 281 | 283 | | null arg_tys = ppr cName
|
| 282 | 284 | | otherwise = panic "dsFCall: Unexpected arguments to FFI value import"
|
| 283 | 285 | raw_res_ty = case tcSplitIOType_maybe io_res_ty of
|
| ... | ... | @@ -380,13 +382,13 @@ mkFExportCBits :: DynFlags |
| 380 | 382 | -> Maybe Id -- Just==static, Nothing==dynamic
|
| 381 | 383 | -> [Type]
|
| 382 | 384 | -> Type
|
| 383 | - -> Bool -- True <=> returns an IO type
|
|
| 385 | + -> ForeignKind -- Function <=> returns an IO type
|
|
| 384 | 386 | -> CCallConv
|
| 385 | 387 | -> (CHeader,
|
| 386 | 388 | CStub,
|
| 387 | 389 | String -- the argument reps
|
| 388 | 390 | )
|
| 389 | -mkFExportCBits dflags c_nm maybe_target arg_htys res_hty is_IO_res_ty cc
|
|
| 391 | +mkFExportCBits dflags c_nm maybe_target arg_htys res_hty io_res_ty cc
|
|
| 390 | 392 | =
|
| 391 | 393 | ( header_bits
|
| 392 | 394 | , CStub body [] []
|
| ... | ... | @@ -517,7 +519,7 @@ mkFExportCBits dflags c_nm maybe_target arg_htys res_hty is_IO_res_ty cc |
| 517 | 519 | char '&' <> cap <>
|
| 518 | 520 | text "rts_apply" <> parens (
|
| 519 | 521 | cap
|
| 520 | - <> (if is_IO_res_ty
|
|
| 522 | + <> (if io_res_ty == ForeignFunction
|
|
| 521 | 523 | then text "ghc_hs_iface->runIO_closure"
|
| 522 | 524 | else text "ghc_hs_iface->runNonIO_closure")
|
| 523 | 525 | <> comma
|
| ... | ... | @@ -108,7 +108,7 @@ dsCCall lbl unit args may_gc result_ty |
| 108 | 108 | { staticTargetLabel = NoSourceText
|
| 109 | 109 | , staticTargetUnit = unit
|
| 110 | 110 | }
|
| 111 | - target = StaticTarget stExt lbl True
|
|
| 111 | + target = StaticTarget stExt lbl ForeignFunction
|
|
| 112 | 112 | the_fcall = CCall (CCallSpec target CCallConv may_gc)
|
| 113 | 113 | the_prim_app = mkFCall uniq the_fcall unboxed_args ccall_result_ty
|
| 114 | 114 | return (foldr ($) (res_wrapper the_prim_app) arg_wrappers)
|
| ... | ... | @@ -24,6 +24,7 @@ import GHC.HsToCore.Monad |
| 24 | 24 | |
| 25 | 25 | import GHC.Hs
|
| 26 | 26 | import GHC.Types.Id
|
| 27 | +import GHC.Types.ForeignCall
|
|
| 27 | 28 | import GHC.Types.ForeignStubs
|
| 28 | 29 | import GHC.Unit.Module
|
| 29 | 30 | import GHC.Core.Coercion
|
| ... | ... | @@ -92,7 +93,7 @@ dsForeigns' fos = do |
| 92 | 93 | , fd_e_ext = co
|
| 93 | 94 | , fd_fe = CExport _
|
| 94 | 95 | (L _ (CExportStatic ext_nm cconv)) }) = do
|
| 95 | - (h, c, _, ids, bs) <- dsFExport id co ext_nm cconv False
|
|
| 96 | + (h, c, _, ids, bs) <- dsFExport id co ext_nm cconv ForeignValue
|
|
| 96 | 97 | return (h, c, ids, bs)
|
| 97 | 98 | |
| 98 | 99 | {-
|
| ... | ... | @@ -164,7 +165,8 @@ dsFExport :: Id -- Either the exported Id, |
| 164 | 165 | -- from C, and its representation type
|
| 165 | 166 | -> CLabelString -- The name to export to C land
|
| 166 | 167 | -> CCallConv
|
| 167 | - -> Bool -- True => foreign export dynamic
|
|
| 168 | + -> ForeignKind -- If it is a function,
|
|
| 169 | + -- then is foreign export dynamic
|
|
| 168 | 170 | -- so invoke IO action that's hanging off
|
| 169 | 171 | -- the first argument's stable pointer
|
| 170 | 172 | -> DsM ( CHeader -- contents of Module_stub.h
|
| ... | ... | @@ -173,16 +175,16 @@ dsFExport :: Id -- Either the exported Id, |
| 173 | 175 | , [Id] -- function closures to be registered as GC roots
|
| 174 | 176 | , [Binding] -- additional bindings used by desugared foreign export
|
| 175 | 177 | )
|
| 176 | -dsFExport fn_id co ext_name cconv is_dyn = do
|
|
| 178 | +dsFExport fn_id co ext_name cconv target_kind = do
|
|
| 177 | 179 | platform <- getPlatform
|
| 178 | 180 | case (platformArch platform, cconv) of
|
| 179 | 181 | (ArchJavaScript, _) -> do
|
| 180 | - (h, c, ts) <- dsJsFExport fn_id co ext_name cconv is_dyn
|
|
| 182 | + (h, c, ts) <- dsJsFExport fn_id co ext_name cconv target_kind
|
|
| 181 | 183 | pure (h, c, ts, [fn_id], [])
|
| 182 | 184 | (ArchWasm32, JavaScriptCallConv) ->
|
| 183 | 185 | dsWasmJSExport fn_id co ext_name
|
| 184 | 186 | _ -> do
|
| 185 | - (h, c, ts) <- dsCFExport fn_id co ext_name cconv is_dyn
|
|
| 187 | + (h, c, ts) <- dsCFExport fn_id co ext_name cconv target_kind
|
|
| 186 | 188 | pure (h, c, ts, [fn_id], [])
|
| 187 | 189 | |
| 188 | 190 |
| ... | ... | @@ -72,7 +72,8 @@ dsJsFExport |
| 72 | 72 | -- from C, and its representation type
|
| 73 | 73 | -> CLabelString -- The name to export to C land
|
| 74 | 74 | -> CCallConv
|
| 75 | - -> Bool -- True => foreign export dynamic
|
|
| 75 | + -> ForeignKind -- If it is a function,
|
|
| 76 | + -- then is foreign export dynamic
|
|
| 76 | 77 | -- so invoke IO action that's hanging off
|
| 77 | 78 | -- the first argument's stable pointer
|
| 78 | 79 | -> DsM ( CHeader -- contents of Module_stub.h
|
| ... | ... | @@ -80,15 +81,16 @@ dsJsFExport |
| 80 | 81 | , String -- string describing type to pass to createAdj.
|
| 81 | 82 | )
|
| 82 | 83 | |
| 83 | -dsJsFExport fn_id co ext_name cconv isDyn = do
|
|
| 84 | +dsJsFExport fn_id co ext_name cconv target_kind = do
|
|
| 84 | 85 | let
|
| 85 | 86 | ty = coercionRKind co
|
| 86 | 87 | (_tvs,sans_foralls) = tcSplitForAllTyVars ty
|
| 87 | 88 | (fe_arg_tys', orig_res_ty) = tcSplitFunTys sans_foralls
|
| 88 | 89 | -- We must use tcSplits here, because we want to see
|
| 89 | 90 | -- the (IO t) in the corner of the type!
|
| 90 | - fe_arg_tys | isDyn = tail fe_arg_tys'
|
|
| 91 | - | otherwise = fe_arg_tys'
|
|
| 91 | + fe_arg_tys = case target_kind of
|
|
| 92 | + ForeignFunction -> tail fe_arg_tys'
|
|
| 93 | + _ -> fe_arg_tys'
|
|
| 92 | 94 | |
| 93 | 95 | -- Look at the result type of the exported function, orig_res_ty
|
| 94 | 96 | -- If it's IO t, return (t, True)
|
| ... | ... | @@ -101,7 +103,7 @@ dsJsFExport fn_id co ext_name cconv isDyn = do |
| 101 | 103 | platform <- targetPlatform <$> getDynFlags
|
| 102 | 104 | return $
|
| 103 | 105 | mkFExportJSBits platform ext_name
|
| 104 | - (if isDyn then Nothing else Just fn_id)
|
|
| 106 | + (if target_kind == ForeignFunction then Nothing else Just fn_id)
|
|
| 105 | 107 | (map scaledThing fe_arg_tys) res_ty is_IO_res_ty cconv
|
| 106 | 108 | |
| 107 | 109 | mkFExportJSBits
|
| ... | ... | @@ -282,7 +284,7 @@ dsJsFExportDynamic id co0 cconv = do |
| 282 | 284 | export_ty = mkVisFunTyMany stable_ptr_ty arg_ty
|
| 283 | 285 | bindIOId <- dsLookupGlobalId bindIOName
|
| 284 | 286 | stbl_value <- newSysLocalMDs stable_ptr_ty
|
| 285 | - (h_code, c_code, typestring) <- dsJsFExport id (mkRepReflCo export_ty) fe_nm cconv True
|
|
| 287 | + (h_code, c_code, typestring) <- dsJsFExport id (mkRepReflCo export_ty) fe_nm cconv ForeignFunction
|
|
| 286 | 288 | let
|
| 287 | 289 | {-
|
| 288 | 290 | The arguments to the external function which will
|
| ... | ... | @@ -652,6 +654,6 @@ mkJsCall u tgt args t = mkFCall u ccall args t |
| 652 | 654 | , staticTargetUnit = ghcInternalUnit
|
| 653 | 655 | }
|
| 654 | 656 | ccall = CCall $ CCallSpec
|
| 655 | - (StaticTarget stExt tgt True)
|
|
| 657 | + (StaticTarget stExt tgt ForeignFunction)
|
|
| 656 | 658 | JavaScriptCallConv
|
| 657 | 659 | PlayRisky |
| ... | ... | @@ -460,7 +460,7 @@ importBindingRHS unitId cfun_name tvs arg_tys orig_res_ty res_trans = do |
| 460 | 460 | let cfun_fcall =
|
| 461 | 461 | CCall
|
| 462 | 462 | ( CCallSpec
|
| 463 | - (StaticTarget stExt cfun_name True)
|
|
| 463 | + (StaticTarget stExt cfun_name ForeignFunction)
|
|
| 464 | 464 | CCallConv
|
| 465 | 465 | -- Same even for foreign import javascript unsafe, for
|
| 466 | 466 | -- the sake of re-entrancy.
|
| ... | ... | @@ -70,6 +70,7 @@ import qualified GHC.Data.List.NonEmpty as NE |
| 70 | 70 | import GHC.Types.SrcLoc as SrcLoc
|
| 71 | 71 | import GHC.Types.Unique
|
| 72 | 72 | import GHC.Types.Var
|
| 73 | +import GHC.Types.ForeignCall
|
|
| 73 | 74 | import GHC.Types.Id
|
| 74 | 75 | import GHC.Types.InlinePragma
|
| 75 | 76 | import GHC.Types.SourceText
|
| ... | ... | @@ -739,9 +740,9 @@ repForD (L loc (ForeignImport { fd_name = name, fd_sig_ty = typ |
| 739 | 740 | conv_cimportspec (CLabel cls)
|
| 740 | 741 | = notHandled (ThForeignLabel cls)
|
| 741 | 742 | conv_cimportspec (CFunction (DynamicTarget{})) = return "dynamic"
|
| 742 | - conv_cimportspec (CFunction (StaticTarget _ fs True))
|
|
| 743 | + conv_cimportspec (CFunction (StaticTarget _ fs ForeignFunction))
|
|
| 743 | 744 | = return (unpackFS fs)
|
| 744 | - conv_cimportspec (CFunction (StaticTarget _ _ False))
|
|
| 745 | + conv_cimportspec (CFunction (StaticTarget _ _ ForeignValue))
|
|
| 745 | 746 | = panic "conv_cimportspec: values not supported yet"
|
| 746 | 747 | conv_cimportspec CWrapper = return "wrapper"
|
| 747 | 748 | -- these calling conventions do not support headers and the static keyword
|
| ... | ... | @@ -142,6 +142,7 @@ import GHC.Types.Name |
| 142 | 142 | import GHC.Types.Basic
|
| 143 | 143 | import GHC.Types.Error
|
| 144 | 144 | import GHC.Types.Fixity
|
| 145 | +import GHC.Types.ForeignCall
|
|
| 145 | 146 | import GHC.Types.Hint
|
| 146 | 147 | import GHC.Types.InlinePragma
|
| 147 | 148 | import GHC.Types.SourceText
|
| ... | ... | @@ -3137,7 +3138,7 @@ mkImport cconv safety (L loc (StringLiteral esrc entity _), v, ty) (timport, td) |
| 3137 | 3138 | entity' = if nullFS entity
|
| 3138 | 3139 | then mkExtName (unLoc v)
|
| 3139 | 3140 | else entity
|
| 3140 | - funcTarget = CFunction (StaticTarget esrc entity' True)
|
|
| 3141 | + funcTarget = CFunction (StaticTarget esrc entity' ForeignFunction)
|
|
| 3141 | 3142 | importSpec = CImport (L (l2l loc) esrc) (reLoc cconv) (reLoc safety) Nothing funcTarget
|
| 3142 | 3143 | |
| 3143 | 3144 | returnSpec spec = return $ \tforeign -> ForD noExtField $ ForeignImport
|
| ... | ... | @@ -3192,15 +3193,15 @@ parseCImport cconv safety nm str sourceText = |
| 3192 | 3193 | id_char c = isAlphaNum c || c == '_'
|
| 3193 | 3194 | |
| 3194 | 3195 | cimp nm = (ReadP.char '&' >> skipSpaces >> CLabel <$> cid)
|
| 3195 | - +++ (do isFun <- case unLoc cconv of
|
|
| 3196 | + +++ (do targetKind <- case unLoc cconv of
|
|
| 3196 | 3197 | CApiConv ->
|
| 3197 | - option True
|
|
| 3198 | + option ForeignFunction
|
|
| 3198 | 3199 | (do token "value"
|
| 3199 | 3200 | skipSpaces
|
| 3200 | - return False)
|
|
| 3201 | - _ -> return True
|
|
| 3201 | + return ForeignValue)
|
|
| 3202 | + _ -> return ForeignFunction
|
|
| 3202 | 3203 | cid' <- cid
|
| 3203 | - return (CFunction (StaticTarget NoSourceText cid' isFun)))
|
|
| 3204 | + return (CFunction (StaticTarget NoSourceText cid' targetKind)))
|
|
| 3204 | 3205 | where
|
| 3205 | 3206 | cid = return nm +++
|
| 3206 | 3207 | (do c <- satisfy id_first_char
|
| ... | ... | @@ -50,7 +50,7 @@ import GHC.Builtin.Names( applicativeClassName, pureAName, thenAName |
| 50 | 50 | |
| 51 | 51 | import GHC.Types.FieldLabel
|
| 52 | 52 | import GHC.Types.Name.Reader
|
| 53 | -import GHC.Types.ForeignCall ( StaticTargetGhc(..) )
|
|
| 53 | +import GHC.Types.ForeignCall
|
|
| 54 | 54 | import GHC.Types.Name
|
| 55 | 55 | import GHC.Types.Name.Set
|
| 56 | 56 | import GHC.Types.Name.Env
|
| ... | ... | @@ -434,14 +434,12 @@ patchCImportSpec unit = \case |
| 434 | 434 | patchCCallTarget :: Unit -> CCallTarget GhcPs -> CCallTarget GhcRn
|
| 435 | 435 | patchCCallTarget unit = \case
|
| 436 | 436 | DynamicTarget x -> DynamicTarget x
|
| 437 | --- StaticTarget src label Nothing isFun ->
|
|
| 438 | --- StaticTarget src label (Just unit) isFun
|
|
| 439 | - StaticTarget sTxt label isFun ->
|
|
| 437 | + StaticTarget sTxt label targetKind ->
|
|
| 440 | 438 | let ext = StaticTargetGhc
|
| 441 | 439 | { staticTargetLabel = sTxt
|
| 442 | 440 | , staticTargetUnit = unit
|
| 443 | 441 | }
|
| 444 | - in StaticTarget ext label isFun
|
|
| 442 | + in StaticTarget ext label targetKind
|
|
| 445 | 443 | |
| 446 | 444 | {-
|
| 447 | 445 | *********************************************************
|
| ... | ... | @@ -2045,9 +2045,9 @@ generateCCall d0 s p (CCallSpec target _ safety) result_ty args |
| 2045 | 2045 | maybe_static_target =
|
| 2046 | 2046 | case target of
|
| 2047 | 2047 | DynamicTarget{} -> Nothing
|
| 2048 | - StaticTarget _ _ False ->
|
|
| 2048 | + StaticTarget _ _ ForeignValue ->
|
|
| 2049 | 2049 | panic "generateCCall: unexpected FFI value import"
|
| 2050 | - StaticTarget _ target True ->
|
|
| 2050 | + StaticTarget _ target ForeignFunction ->
|
|
| 2051 | 2051 | Just (LitLabel target IsFunction)
|
| 2052 | 2052 | |
| 2053 | 2053 | let
|
| ... | ... | @@ -78,9 +78,9 @@ cgForeignCall (CCall (CCallSpec target cconv safety)) typ stg_args res_ty |
| 78 | 78 | ; (res_regs, res_hints) <- newUnboxedTupleRegs res_ty
|
| 79 | 79 | ; let ((call_args, arg_hints), cmm_target)
|
| 80 | 80 | = case target of
|
| 81 | - StaticTarget _ _ False ->
|
|
| 81 | + StaticTarget _ _ ForeignValue ->
|
|
| 82 | 82 | panic "cgForeignCall: unexpected FFI value import"
|
| 83 | - StaticTarget ext lbl True ->
|
|
| 83 | + StaticTarget ext lbl ForeignFunction ->
|
|
| 84 | 84 | let labelSource =
|
| 85 | 85 | ForeignLabelInPackage . toUnitId $ staticTargetUnit ext
|
| 86 | 86 | in ( unzip cmm_args
|
| ... | ... | @@ -181,7 +181,7 @@ genForeignCall :: HasDebugCallStack |
| 181 | 181 | -> [StgArg]
|
| 182 | 182 | -> G (JStgStat, ExprResult)
|
| 183 | 183 | genForeignCall _ctx
|
| 184 | - (CCall (CCallSpec (StaticTarget _ tgt True)
|
|
| 184 | + (CCall (CCallSpec (StaticTarget _ tgt ForeignFunction)
|
|
| 185 | 185 | JavaScriptCallConv
|
| 186 | 186 | PlayRisky))
|
| 187 | 187 | _t
|
| ... | ... | @@ -197,6 +197,7 @@ import GHC.Types.Basic |
| 197 | 197 | import GHC.Types.Error
|
| 198 | 198 | import GHC.Types.Avail
|
| 199 | 199 | import GHC.Types.Hint
|
| 200 | +import GHC.Types.ForeignCall ( CLabelString )
|
|
| 200 | 201 | import GHC.Types.Id.Info ( RecSelParent(..) )
|
| 201 | 202 | import GHC.Types.InlinePragma (InlinePragma(..))
|
| 202 | 203 | import GHC.Types.Name (NamedThing(..), Name, OccName, getSrcLoc, getSrcSpan)
|
| ... | ... | @@ -355,7 +355,7 @@ tcCheckFIType arg_tys res_ty idecl@(CImport src (L lc cconv) (L ls safety) mh |
| 355 | 355 | checkForeignRes nonIOok checkSafe (isFFIImportResultTy dflags) res_ty
|
| 356 | 356 | checkMissingAmpersand idecl target (map scaledThing arg_tys) res_ty
|
| 357 | 357 | case target of
|
| 358 | - StaticTarget _ _ False
|
|
| 358 | + StaticTarget _ _ ForeignValue
|
|
| 359 | 359 | | not (null arg_tys) ->
|
| 360 | 360 | addErrTc (TcRnForeignFunctionImportAsValue idecl)
|
| 361 | 361 | _ -> return ()
|
| ... | ... | @@ -381,7 +381,7 @@ checkCTarget idecl (StaticTarget _ str _) = do |
| 381 | 381 | checkCTarget _ (DynamicTarget{}) = panic "checkCTarget DynamicTarget"
|
| 382 | 382 | |
| 383 | 383 | checkMissingAmpersand :: ForeignImport GhcRn -> CCallTarget GhcRn -> [Type] -> Type -> TcM ()
|
| 384 | -checkMissingAmpersand _ (StaticTarget _ _ False) _ _ = return ()
|
|
| 384 | +checkMissingAmpersand _ (StaticTarget _ _ ForeignValue) _ _ = return ()
|
|
| 385 | 385 | |
| 386 | 386 | checkMissingAmpersand idecl _ arg_tys res_ty
|
| 387 | 387 | | null arg_tys && isFunPtrTy res_ty
|
| ... | ... | @@ -37,8 +37,9 @@ import GHC.Unit.State ( UnitState ) |
| 37 | 37 | import GHC.Data.FastString ( FastString )
|
| 38 | 38 | import GHC.Utils.Outputable ( Outputable(..) )
|
| 39 | 39 | |
| 40 | -import Language.Haskell.Syntax.Basic ( FieldLabelString(..) )
|
|
| 41 | 40 | import Language.Haskell.Syntax
|
| 41 | +import Language.Haskell.Syntax.Basic ( FieldLabelString(..) )
|
|
| 42 | +import Language.Haskell.Syntax.Decls.ForeignCall ( ForeignDecl(..) )
|
|
| 42 | 43 | import GHC.Boot.TH.Syntax qualified as TH
|
| 43 | 44 | |
| 44 | 45 | import qualified Data.List.NonEmpty as NE
|
| ... | ... | @@ -40,6 +40,7 @@ import GHC.Builtin.Types |
| 40 | 40 | import GHC.Builtin.Types.Prim( fUNTyCon )
|
| 41 | 41 | import GHC.Hs.Decls.Overlap as Hs
|
| 42 | 42 | import GHC.Types.Basic as Hs
|
| 43 | +import GHC.Types.ForeignCall
|
|
| 43 | 44 | import GHC.Types.InlinePragma as Hs
|
| 44 | 45 | import GHC.Types.Unique
|
| 45 | 46 | import GHC.Types.SourceText
|
| ... | ... | @@ -840,7 +841,7 @@ cvtForD (ImportF callconv safety from nm ty) = |
| 840 | 841 | (StaticTarget
|
| 841 | 842 | (SourceText fromtxt)
|
| 842 | 843 | fromtxt
|
| 843 | - True
|
|
| 844 | + ForeignFunction
|
|
| 844 | 845 | )
|
| 845 | 846 | )
|
| 846 | 847 | )
|
| ... | ... | @@ -21,13 +21,15 @@ To be resolved at a later time, see TODO at the end of this module. |
| 21 | 21 | -}
|
| 22 | 22 | |
| 23 | 23 | module GHC.Types.ForeignCall (
|
| 24 | - ForeignCall(..), isSafeForeignCall,
|
|
| 24 | + ForeignCall(..),
|
|
| 25 | + isSafeForeignCall,
|
|
| 25 | 26 | Safety(..), playSafe, playInterruptible,
|
| 26 | 27 | |
| 27 | 28 | CExportSpec(..), CLabelString, isCLabelString, pprCLabelString,
|
| 28 | 29 | CCallSpec(..),
|
| 29 | 30 | CCallTarget(..), isDynamicTarget,
|
| 30 | 31 | CCallConv(..), defaultCCallConv, ccallConvAttribute,
|
| 32 | + ForeignKind(..),
|
|
| 31 | 33 | |
| 32 | 34 | Header(..), CType(..),
|
| 33 | 35 | StaticTargetGhc(..),
|
| ... | ... | @@ -45,11 +47,12 @@ import GHC.Unit.Types |
| 45 | 47 | |
| 46 | 48 | import Language.Haskell.Syntax.Decls.ForeignCall
|
| 47 | 49 | (CCallConv(..), CCallTarget(..), CExportSpec(..), CLabelString,
|
| 48 | - CType(..), Header(..), Safety(..))
|
|
| 50 | + CType(..), ForeignKind(..), Header(..), Safety(..))
|
|
| 49 | 51 | import Language.Haskell.Syntax.Extension
|
| 50 | 52 | |
| 51 | 53 | import Data.Char
|
| 52 | 54 | import Data.Data (Data)
|
| 55 | +import Data.Functor ((<&>))
|
|
| 53 | 56 | |
| 54 | 57 | import Control.DeepSeq (NFData(..))
|
| 55 | 58 | |
| ... | ... | @@ -140,9 +143,9 @@ instance forall p. IsPass p => Outputable (CCallSpec (GhcPass p)) where |
| 140 | 143 | ppr_fun = \case
|
| 141 | 144 | DynamicTarget{} -> text "__ffi_dyn_ccall" <> gc_suf <+> text "\"\""
|
| 142 | 145 | st@(StaticTarget _ label isFun) ->
|
| 143 | - let pCallType
|
|
| 144 | - | isFun = text "__ffi_static_ccall"
|
|
| 145 | - | otherwise = text "__ffi_static_ccall_value"
|
|
| 146 | + let pCallType = case isFun of
|
|
| 147 | + ForeignValue -> text "__ffi_static_ccall_value"
|
|
| 148 | + ForeignFunction -> text "__ffi_static_ccall"
|
|
| 146 | 149 | (srcTxt, pPkgId) = case ghcPass @p of
|
| 147 | 150 | GhcPs | StaticTarget ext _ _ <- st -> (ext, empty)
|
| 148 | 151 | GhcRn | StaticTarget ext _ _ <- st -> (staticTargetLabel ext, ppr $ staticTargetUnit ext)
|
| ... | ... | @@ -320,6 +323,14 @@ instance Binary CType where |
| 320 | 323 | fs <- get bh
|
| 321 | 324 | return (CType s mh fs)
|
| 322 | 325 | |
| 326 | +instance Binary ForeignKind where
|
|
| 327 | + put_ bh = putByte bh . \case
|
|
| 328 | + ForeignValue -> 0
|
|
| 329 | + ForeignFunction -> 1
|
|
| 330 | + get bh = getByte bh <&> \case
|
|
| 331 | + 0 -> ForeignValue
|
|
| 332 | + _ -> ForeignFunction
|
|
| 333 | + |
|
| 323 | 334 | instance Binary Header where
|
| 324 | 335 | put_ bh (Header s h) = put_ bh s >> put_ bh h
|
| 325 | 336 | get bh = do
|
| ... | ... | @@ -13,7 +13,7 @@ |
| 13 | 13 | -- | Abstract syntax of global declarations.
|
| 14 | 14 | --
|
| 15 | 15 | -- Definitions for: @SynDecl@ and @ConDecl@, @ClassDecl@,
|
| 16 | --- @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
|
|
| 16 | +-- @InstDecl@, @DefaultDecl@.
|
|
| 17 | 17 | module Language.Haskell.Syntax.Decls (
|
| 18 | 18 | -- * Toplevel declarations
|
| 19 | 19 | HsDecl(..), LHsDecl, HsDataDefn(..), HsDeriving, LHsFunDep, FunDep(..),
|
| ... | ... | @@ -51,10 +51,12 @@ module Language.Haskell.Syntax.Decls ( |
| 51 | 51 | -- ** Template haskell declaration splice
|
| 52 | 52 | SpliceDecoration(..),
|
| 53 | 53 | SpliceDecl(..), LSpliceDecl,
|
| 54 | +{-
|
|
| 54 | 55 | -- ** Foreign function interface declarations
|
| 55 | 56 | ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..),
|
| 56 | 57 | CCallConv(..), CCallTarget(..), CExportSpec(..), CImportSpec(..), CLabelString,
|
| 57 | 58 | CType(..), Header(..), Safety(..),
|
| 59 | +-}
|
|
| 58 | 60 | -- ** Data-constructor declarations
|
| 59 | 61 | ConDecl(..), LConDecl,
|
| 60 | 62 | HsConDeclH98Details,
|
| ... | ... | @@ -105,7 +107,7 @@ import Language.Haskell.Syntax.Type |
| 105 | 107 | import GHC.Data.FastString (FastString)
|
| 106 | 108 | import GHC.Hs.Doc (LHsDoc) -- ROMES:TODO Discuss in #21592 whether this is parsed AST or base AST
|
| 107 | 109 | import GHC.Hs.Doc (WithHsDocIdentifiers)
|
| 108 | -import GHC.Types.SourceText (SourceText, StringLiteral)
|
|
| 110 | +import GHC.Types.SourceText (StringLiteral)
|
|
| 109 | 111 | |
| 110 | 112 | import Control.DeepSeq
|
| 111 | 113 | import Control.Monad
|
| ... | ... | @@ -15,8 +15,8 @@ |
| 15 | 15 | -- Definitions for: @SynDecl@ and @ConDecl@, @ClassDecl@,
|
| 16 | 16 | -- @InstDecl@, @DefaultDecl@ and @ForeignDecl@.
|
| 17 | 17 | module Language.Haskell.Syntax.Decls.ForeignCall (
|
| 18 | - -- ** Foreign function interface declarations
|
|
| 19 | - ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..),
|
|
| 18 | + -- ** Foreign function interface declarationss
|
|
| 19 | + ForeignDecl(..), LForeignDecl, ForeignImport(..), ForeignExport(..), ForeignKind(..),
|
|
| 20 | 20 | CCallConv(..), CCallTarget(..), CExportSpec(..), CImportSpec(..), CLabelString,
|
| 21 | 21 | CType(..), Header(..), Safety(..),
|
| 22 | 22 | |
| ... | ... | @@ -41,7 +41,6 @@ import Control.DeepSeq |
| 41 | 41 | import Data.Data hiding (TyCon, Fixity, Infix)
|
| 42 | 42 | import Data.Maybe
|
| 43 | 43 | import Data.Eq
|
| 44 | -import Data.Bool
|
|
| 45 | 44 | import Prelude (Enum, Show, seq)
|
| 46 | 45 | |
| 47 | 46 | {-
|
| ... | ... | @@ -144,16 +143,25 @@ data CCallConv |
| 144 | 143 | | StdCallConv
|
| 145 | 144 | | PrimCallConv
|
| 146 | 145 | | JavaScriptCallConv
|
| 147 | - deriving (Show, Eq, Data, Enum)
|
|
| 146 | + deriving (Enum, Eq, Data, Show)
|
|
| 148 | 147 | |
| 149 | 148 | instance NFData CCallConv where
|
| 150 | - rnf CCallConv = ()
|
|
| 151 | - rnf StdCallConv = ()
|
|
| 152 | - rnf PrimCallConv = ()
|
|
| 153 | - rnf CApiConv = ()
|
|
| 154 | - rnf JavaScriptCallConv = ()
|
|
| 155 | - |
|
| 156 | --- TODO TTG: Rename the Bool field
|
|
| 149 | + rnf = \case
|
|
| 150 | + CCallConv -> ()
|
|
| 151 | + StdCallConv -> ()
|
|
| 152 | + PrimCallConv -> ()
|
|
| 153 | + CApiConv -> ()
|
|
| 154 | + JavaScriptCallConv -> ()
|
|
| 155 | + |
|
| 156 | +data ForeignKind
|
|
| 157 | + = ForeignValue -- ^ Binds to a value, a zero-arity function.
|
|
| 158 | + | ForeignFunction -- ^ Binds to a function with arity /of at least one/.
|
|
| 159 | + deriving stock (Enum, Eq, Data, Show)
|
|
| 160 | + |
|
| 161 | +instance NFData ForeignKind where
|
|
| 162 | + rnf = \case
|
|
| 163 | + ForeignValue -> ()
|
|
| 164 | + ForeignFunction -> ()
|
|
| 157 | 165 | |
| 158 | 166 | -- | How to call a particular function in C-land.
|
| 159 | 167 | data CCallTarget pass
|
| ... | ... | @@ -162,7 +170,7 @@ data CCallTarget pass |
| 162 | 170 | (XStaticTarget pass)
|
| 163 | 171 | CLabelString -- C-land name of label.
|
| 164 | 172 | -- Used when importing a label as "foreign import ccall "dynamic" ..."
|
| 165 | - Bool -- True => really a function
|
|
| 173 | + ForeignKind -- True => really a function
|
|
| 166 | 174 | -- False => a value; only
|
| 167 | 175 | -- allowed in CAPI imports
|
| 168 | 176 |
| 1 | -Subproject commit 423fd981e576bd17a8b5fa48d0ad6b9a0c370e77 |
|
| 1 | +Subproject commit 5f343f668f421bfb30cead594e52d0ac6206ff67 |
| 1 | -Subproject commit 0d615bc2457d5d2c695dcfdb902d88c1225beff3 |
|
| 1 | +Subproject commit cee47cca7705edafe0a5839439e679edbd61890a |