summaryrefslogtreecommitdiff
path: root/compiler/GHC/Types/ForeignStubs.hs
blob: 0a37d230feae2c4a4c6808858c967b65c098b3a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
-- | Foreign export stubs
{-# LANGUAGE DerivingVia #-}
module GHC.Types.ForeignStubs
   ( ForeignStubs (..)
   , CHeader(..)
   , CStub(..)
   , appendStubC
   )
where

import GHC.Utils.Outputable
import Data.Monoid
import Data.Semigroup
import Data.Coerce

newtype CStub = CStub { getCStub :: SDoc }

emptyCStub :: CStub
emptyCStub = CStub empty

instance Monoid CStub where
  mempty = emptyCStub
  mconcat = coerce vcat

instance Semigroup CStub where
  (<>) = coerce ($$)

newtype CHeader = CHeader { getCHeader :: SDoc }
  deriving (Monoid, Semigroup) via CStub

-- | Foreign export stubs
data ForeignStubs
  = NoStubs
      -- ^ We don't have any stubs
  | ForeignStubs CHeader CStub
      -- ^ There are some stubs. Parameters:
      --
      --  1) Header file prototypes for
      --     "foreign exported" functions
      --
      --  2) C stubs to use when calling
      --     "foreign exported" functions

appendStubC :: ForeignStubs -> CStub -> ForeignStubs
appendStubC NoStubs         c_code = ForeignStubs mempty c_code
appendStubC (ForeignStubs h c) c_code = ForeignStubs h (c `mappend` c_code)