never executed always true always false
    1 -- | This stuff here is related to supporting the Safe Haskell extension,
    2 -- primarily about storing under what trust type a module has been compiled.
    3 module GHC.Types.SafeHaskell
    4    ( IsSafeImport
    5    , SafeHaskellMode(..)
    6    , IfaceTrustInfo
    7    , getSafeMode
    8    , setSafeMode
    9    , noIfaceTrustInfo
   10    )
   11 where
   12 
   13 import GHC.Prelude
   14 
   15 import GHC.Utils.Binary
   16 import GHC.Utils.Outputable
   17 
   18 import Data.Word
   19 
   20 
   21 -- | Is an import a safe import?
   22 type IsSafeImport = Bool
   23 
   24 -- | The various Safe Haskell modes
   25 data SafeHaskellMode
   26    = Sf_None          -- ^ inferred unsafe
   27    | Sf_Unsafe        -- ^ declared and checked
   28    | Sf_Trustworthy   -- ^ declared and checked
   29    | Sf_Safe          -- ^ declared and checked
   30    | Sf_SafeInferred  -- ^ inferred as safe
   31    | Sf_Ignore        -- ^ @-fno-safe-haskell@ state
   32    deriving (Eq)
   33 
   34 instance Show SafeHaskellMode where
   35     show Sf_None         = "None"
   36     show Sf_Unsafe       = "Unsafe"
   37     show Sf_Trustworthy  = "Trustworthy"
   38     show Sf_Safe         = "Safe"
   39     show Sf_SafeInferred = "Safe-Inferred"
   40     show Sf_Ignore       = "Ignore"
   41 
   42 instance Outputable SafeHaskellMode where
   43     ppr = text . show
   44 
   45 -- | Safe Haskell information for 'ModIface'
   46 -- Simply a wrapper around SafeHaskellMode to sepperate iface and flags
   47 newtype IfaceTrustInfo = TrustInfo SafeHaskellMode
   48 
   49 getSafeMode :: IfaceTrustInfo -> SafeHaskellMode
   50 getSafeMode (TrustInfo x) = x
   51 
   52 setSafeMode :: SafeHaskellMode -> IfaceTrustInfo
   53 setSafeMode = TrustInfo
   54 
   55 noIfaceTrustInfo :: IfaceTrustInfo
   56 noIfaceTrustInfo = setSafeMode Sf_None
   57 
   58 trustInfoToNum :: IfaceTrustInfo -> Word8
   59 trustInfoToNum it
   60   = case getSafeMode it of
   61             Sf_None         -> 0
   62             Sf_Unsafe       -> 1
   63             Sf_Trustworthy  -> 2
   64             Sf_Safe         -> 3
   65             Sf_SafeInferred -> 4
   66             Sf_Ignore       -> 0
   67 
   68 numToTrustInfo :: Word8 -> IfaceTrustInfo
   69 numToTrustInfo 0 = setSafeMode Sf_None
   70 numToTrustInfo 1 = setSafeMode Sf_Unsafe
   71 numToTrustInfo 2 = setSafeMode Sf_Trustworthy
   72 numToTrustInfo 3 = setSafeMode Sf_Safe
   73 numToTrustInfo 4 = setSafeMode Sf_SafeInferred
   74 numToTrustInfo n = error $ "numToTrustInfo: bad input number! (" ++ show n ++ ")"
   75 
   76 instance Outputable IfaceTrustInfo where
   77     ppr (TrustInfo Sf_None)          = text "none"
   78     ppr (TrustInfo Sf_Ignore)        = text "none"
   79     ppr (TrustInfo Sf_Unsafe)        = text "unsafe"
   80     ppr (TrustInfo Sf_Trustworthy)   = text "trustworthy"
   81     ppr (TrustInfo Sf_Safe)          = text "safe"
   82     ppr (TrustInfo Sf_SafeInferred)  = text "safe-inferred"
   83 
   84 instance Binary IfaceTrustInfo where
   85     put_ bh iftrust = putByte bh $ trustInfoToNum iftrust
   86     get bh = getByte bh >>= (return . numToTrustInfo)