diff options
Diffstat (limited to 'libraries/template-haskell/Language/Haskell/TH')
4 files changed, 40 insertions, 1 deletions
diff --git a/libraries/template-haskell/Language/Haskell/TH/Lib.hs b/libraries/template-haskell/Language/Haskell/TH/Lib.hs index 69a40428b8..0a9e11b936 100644 --- a/libraries/template-haskell/Language/Haskell/TH/Lib.hs +++ b/libraries/template-haskell/Language/Haskell/TH/Lib.hs @@ -26,7 +26,7 @@ module Language.Haskell.TH.Lib ( -- ** Constructors lifted to 'Q' -- *** Literals intPrimL, wordPrimL, floatPrimL, doublePrimL, integerL, rationalL, - charL, stringL, stringPrimL, charPrimL, + charL, stringL, stringPrimL, charPrimL, bytesPrimL, mkBytes, -- *** Patterns litP, varP, tupP, unboxedTupP, unboxedSumP, conP, uInfixP, parensP, infixP, tildeP, bangP, asP, wildP, recP, @@ -157,6 +157,8 @@ import Language.Haskell.TH.Lib.Internal hiding import Language.Haskell.TH.Syntax import Control.Monad (liftM2) +import Foreign.ForeignPtr +import Data.Word import Prelude -- All definitions below represent the "old" API, since their definitions are @@ -303,3 +305,17 @@ standaloneDerivWithStrategyD mds ctxt ty = do ctxt' <- ctxt ty' <- ty return $ StandaloneDerivD mds ctxt' ty' + +------------------------------------------------------------------------------- +-- * Bytes literals + +-- | Create a Bytes datatype representing raw bytes to be embedded into the +-- program/library binary. +-- +-- @since 2.16.0.0 +mkBytes + :: ForeignPtr Word8 -- ^ Pointer to the data + -> Word -- ^ Offset from the pointer + -> Word -- ^ Number of bytes + -> Bytes +mkBytes = Bytes diff --git a/libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs b/libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs index 14ef0a02a8..b08b31c4fe 100644 --- a/libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs +++ b/libraries/template-haskell/Language/Haskell/TH/Lib/Internal.hs @@ -86,6 +86,8 @@ stringL :: String -> Lit stringL = StringL stringPrimL :: [Word8] -> Lit stringPrimL = StringPrimL +bytesPrimL :: Bytes -> Lit +bytesPrimL = BytesPrimL rationalL :: Rational -> Lit rationalL = RationalL diff --git a/libraries/template-haskell/Language/Haskell/TH/Ppr.hs b/libraries/template-haskell/Language/Haskell/TH/Ppr.hs index fa00c8c537..bc9efe6e3d 100644 --- a/libraries/template-haskell/Language/Haskell/TH/Ppr.hs +++ b/libraries/template-haskell/Language/Haskell/TH/Ppr.hs @@ -268,6 +268,7 @@ pprLit _ (CharL c) = text (show c) pprLit _ (CharPrimL c) = text (show c) <> char '#' pprLit _ (StringL s) = pprString s pprLit _ (StringPrimL s) = pprString (bytesToString s) <> char '#' +pprLit _ (BytesPrimL {}) = pprString "<binary data>" pprLit i (RationalL rat) = parensIf (i > noPrec) $ integer (numerator rat) <+> char '/' <+> integer (denominator rat) diff --git a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs index 22c6cd1def..690d63807c 100644 --- a/libraries/template-haskell/Language/Haskell/TH/Syntax.hs +++ b/libraries/template-haskell/Language/Haskell/TH/Syntax.hs @@ -44,6 +44,7 @@ import GHC.ForeignSrcLang.Type import Language.Haskell.TH.LanguageExtensions import Numeric.Natural import Prelude +import Foreign.ForeignPtr import qualified Control.Monad.Fail as Fail @@ -1619,6 +1620,7 @@ data Lit = CharL Char | FloatPrimL Rational | DoublePrimL Rational | StringPrimL [Word8] -- ^ A primitive C-style string, type Addr# + | BytesPrimL Bytes -- ^ Some raw bytes, type Addr#: | CharPrimL Char deriving( Show, Eq, Ord, Data, Generic ) @@ -1626,6 +1628,24 @@ data Lit = CharL Char -- but that could complicate the -- supposedly-simple TH.Syntax literal type +-- | Raw bytes embedded into the binary. +-- +-- Avoid using Bytes constructor directly as it is likely to change in the +-- future. Use helpers such as `mkBytes` in Language.Haskell.TH.Lib instead. +data Bytes = Bytes + { bytesPtr :: ForeignPtr Word8 -- ^ Pointer to the data + , bytesOffset :: Word -- ^ Offset from the pointer + , bytesSize :: Word -- ^ Number of bytes + -- Maybe someday: + -- , bytesAlignement :: Word -- ^ Alignement constraint + -- , bytesReadOnly :: Bool -- ^ Shall we embed into a read-only + -- -- section or not + -- , bytesInitialized :: Bool -- ^ False: only use `bytesSize` to allocate + -- -- an uninitialized region + } + deriving (Eq,Ord,Data,Generic,Show) + + -- | Pattern in Haskell given in @{}@ data Pat = LitP Lit -- ^ @{ 5 or \'c\' }@ |