{-# LANGUAGE CPP #-}

-- | GHC.AssertNF.CPP localizes our use of CPP around calls
-- to 'assertNFHere', primarily to give tooling (e.g. ormolu)
-- an easier time.
--
-- We disable the 'assertNF'-related code because it is provided
-- by the package ghc-heap-view, which can't be built using profiling.

#ifdef PROFILING
{-# LANGUAGE TemplateHaskell #-}

module GHC.AssertNF.CPP
  ( assertNFHere,
    disableAssertNF,
  )
where

import Hasura.Prelude
import Language.Haskell.TH

assertNFHere :: Q Exp
assertNFHere = [| const (return ()) |]

disableAssertNF :: IO ()
disableAssertNF = return ()

#else

module GHC.AssertNF.CPP
  ( assertNFHere,
    disableAssertNF,
    GHC.AssertNF.assertNFNamed,
  )
where

import GHC.AssertNF qualified
import Hasura.Prelude
import Language.Haskell.TH
import Text.Printf (printf)

assertNFHere :: Q Exp
-- This is a copy of 'GHC.AssertNF.assertNFHere'. We can't easily
-- use the original because that relies on an import of "GHC.AssertNF".
-- Instead, we rewrite it to use the re-exported 'assertNFNamed'.
assertNFHere :: Q Exp
assertNFHere = do
    String
locStr <- Loc -> String
formatLoc (Loc -> String) -> Q Loc -> Q String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Q Loc
location
    Exp -> Q Exp
forall (m :: * -> *) a. Monad m => a -> m a
return (Exp -> Q Exp) -> Exp -> Q Exp
forall a b. (a -> b) -> a -> b
$ Exp -> Exp -> Exp
AppE (Name -> Exp
VarE (String -> Name
mkName String
"GHC.AssertNF.CPP.assertNFNamed"))
                  (Lit -> Exp
LitE (String -> Lit
StringL String
locStr))
  where formatLoc :: Loc -> String
        formatLoc :: Loc -> String
formatLoc Loc
loc = let file :: String
file = Loc -> String
loc_filename Loc
loc
                            (Int
line, Int
col) = Loc -> (Int, Int)
loc_start Loc
loc
                        in  String -> String -> Int -> Int -> String
forall r. PrintfType r => String -> r
printf String
"parameter at %s:%d:%d" String
file Int
line Int
col

disableAssertNF :: IO ()
disableAssertNF :: IO ()
disableAssertNF = IO ()
GHC.AssertNF.disableAssertNF

#endif