{-
(c) The University of Glasgow 2006-2012
(c) The GRASP Project, Glasgow University, 1992-1998
-}

-- | This module defines classes and functions for pretty-printing. It also
-- exports a number of helpful debugging and other utilities such as 'trace' and 'panic'.
--
-- The interface to this module is very similar to the standard Hughes-PJ pretty printing
-- module, except that it exports a number of additional functions that are rarely used,
-- and works over the 'SDoc' type.
module Outputable (
        -- * Type classes
        Outputable(..), OutputableBndr(..),

        -- * Pretty printing combinators
        SDoc, runSDoc, initSDocContext,
        docToSDoc,
        interppSP, interpp'SP,
        pprQuotedList, pprWithCommas, quotedListWithOr, quotedListWithNor,
        pprWithBars,
        empty, isEmpty, nest,
        char,
        text, ftext, ptext, ztext,
        int, intWithCommas, integer, word, float, double, rational, doublePrec,
        parens, cparen, brackets, braces, quotes, quote,
        doubleQuotes, angleBrackets,
        semi, comma, colon, dcolon, space, equals, dot, vbar,
        arrow, larrow, darrow, arrowt, larrowt, arrowtt, larrowtt,
        lparen, rparen, lbrack, rbrack, lbrace, rbrace, underscore,
        blankLine, forAllLit, kindType, bullet,
        (<>), (<+>), hcat, hsep,
        ($$), ($+$), vcat,
        sep, cat,
        fsep, fcat,
        hang, hangNotEmpty, punctuate, ppWhen, ppUnless,
        speakNth, speakN, speakNOf, plural, isOrAre, doOrDoes,
        unicodeSyntax,

        coloured, keyword,

        -- * Converting 'SDoc' into strings and outputing it
        printSDoc, printSDocLn, printForUser, printForUserPartWay,
        printForC, bufLeftRenderSDoc,
        pprCode, mkCodeStyle,
        showSDoc, showSDocUnsafe, showSDocOneLine,
        showSDocForUser, showSDocDebug, showSDocDump, showSDocDumpOneLine,
        showSDocUnqual, showPpr,
        renderWithStyle,

        pprInfixVar, pprPrefixVar,
        pprHsChar, pprHsString, pprHsBytes,

        primFloatSuffix, primCharSuffix, primWordSuffix, primDoubleSuffix,
        primInt64Suffix, primWord64Suffix, primIntSuffix,

        pprPrimChar, pprPrimInt, pprPrimWord, pprPrimInt64, pprPrimWord64,

        pprFastFilePath,

        -- * Controlling the style in which output is printed
        BindingSite(..),

        PprStyle, CodeStyle(..), PrintUnqualified(..),
        QueryQualifyName, QueryQualifyModule, QueryQualifyPackage,
        reallyAlwaysQualify, reallyAlwaysQualifyNames,
        alwaysQualify, alwaysQualifyNames, alwaysQualifyModules,
        neverQualify, neverQualifyNames, neverQualifyModules,
        alwaysQualifyPackages, neverQualifyPackages,
        QualifyName(..), queryQual,
        sdocWithDynFlags, sdocWithPlatform,
        updSDocDynFlags,
        getPprStyle, withPprStyle, withPprStyleDoc, setStyleColoured,
        pprDeeper, pprDeeperList, pprSetDepth,
        codeStyle, userStyle, debugStyle, dumpStyle, asmStyle,
        qualName, qualModule, qualPackage,
        mkErrStyle, defaultErrStyle, defaultDumpStyle, mkDumpStyle, defaultUserStyle,
        mkUserStyle, cmdlineParserStyle, Depth(..),

        ifPprDebug, whenPprDebug, getPprDebug,

        -- * Error handling and debugging utilities
        pprPanic, pprSorry, assertPprPanic, pprPgmError,
        pprTrace, pprTraceDebug, pprTraceIt, warnPprTrace, pprSTrace,
        pprTraceException, pprTraceM,
        trace, pgmError, panic, sorry, assertPanic,
        pprDebugAndThen, callStackDoc,
    ) where

import GhcPrelude

import {-# SOURCE #-}   DynFlags( DynFlags, hasPprDebug, hasNoDebugOutput,
                                  targetPlatform, pprUserLength, pprCols,
                                  useUnicode, useUnicodeSyntax, useStarIsType,
                                  shouldUseColor, unsafeGlobalDynFlags,
                                  shouldUseHexWordLiterals )
import {-# SOURCE #-}   Module( UnitId, Module, ModuleName, moduleName )
import {-# SOURCE #-}   OccName( OccName )

import BufWrite (BufHandle)
import FastString
import qualified Pretty
import Util
import Platform
import qualified PprColour as Col
import Pretty           ( Doc, Mode(..) )
import Panic
import GHC.Serialized
import GHC.LanguageExtensions (Extension)

import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Char
import qualified Data.Map as M
import Data.Int
import qualified Data.IntMap as IM
import Data.Set (Set)
import qualified Data.Set as Set
import Data.String
import Data.Word
import System.IO        ( Handle )
import System.FilePath
import Text.Printf
import Numeric (showFFloat)
import Data.Graph (SCC(..))
import Data.List (intersperse)

import GHC.Fingerprint
import GHC.Show         ( showMultiLineString )
import GHC.Stack        ( callStack, prettyCallStack )
import Control.Monad.IO.Class
import Exception

{-
************************************************************************
*                                                                      *
\subsection{The @PprStyle@ data type}
*                                                                      *
************************************************************************
-}

data PprStyle
  = PprUser PrintUnqualified Depth Coloured
                -- Pretty-print in a way that will make sense to the
                -- ordinary user; must be very close to Haskell
                -- syntax, etc.
                -- Assumes printing tidied code: non-system names are
                -- printed without uniques.

  | PprDump PrintUnqualified
                -- For -ddump-foo; less verbose than PprDebug, but more than PprUser
                -- Does not assume tidied code: non-external names
                -- are printed with uniques.

  | PprDebug    -- Full debugging output

  | PprCode CodeStyle
                -- Print code; either C or assembler

data CodeStyle = CStyle         -- The format of labels differs for C and assembler
               | AsmStyle

data Depth = AllTheWay
           | PartWay Int        -- 0 => stop

data Coloured
  = Uncoloured
  | Coloured

-- -----------------------------------------------------------------------------
-- Printing original names

-- | When printing code that contains original names, we need to map the
-- original names back to something the user understands.  This is the
-- purpose of the triple of functions that gets passed around
-- when rendering 'SDoc'.
data PrintUnqualified = QueryQualify {
    PrintUnqualified -> QueryQualifyName
queryQualifyName    :: QueryQualifyName,
    PrintUnqualified -> QueryQualifyModule
queryQualifyModule  :: QueryQualifyModule,
    PrintUnqualified -> QueryQualifyPackage
queryQualifyPackage :: QueryQualifyPackage
}

-- | Given a `Name`'s `Module` and `OccName`, decide whether and how to qualify
-- it.
type QueryQualifyName = Module -> OccName -> QualifyName

-- | For a given module, we need to know whether to print it with
-- a package name to disambiguate it.
type QueryQualifyModule = Module -> Bool

-- | For a given package, we need to know whether to print it with
-- the component id to disambiguate it.
type QueryQualifyPackage = UnitId -> Bool

-- See Note [Printing original names] in HscTypes
data QualifyName   -- Given P:M.T
  = NameUnqual           -- It's in scope unqualified as "T"
                         -- OR nothing called "T" is in scope

  | NameQual ModuleName  -- It's in scope qualified as "X.T"

  | NameNotInScope1      -- It's not in scope at all, but M.T is not bound
                         -- in the current scope, so we can refer to it as "M.T"

  | NameNotInScope2      -- It's not in scope at all, and M.T is already bound in
                         -- the current scope, so we must refer to it as "P:M.T"

instance Outputable QualifyName where
  ppr :: QualifyName -> SDoc
ppr NameUnqual      = String -> SDoc
text "NameUnqual"
  ppr (NameQual _mod :: ModuleName
_mod) = String -> SDoc
text "NameQual"  -- can't print the mod without module loops :(
  ppr NameNotInScope1 = String -> SDoc
text "NameNotInScope1"
  ppr NameNotInScope2 = String -> SDoc
text "NameNotInScope2"

reallyAlwaysQualifyNames :: QueryQualifyName
reallyAlwaysQualifyNames :: QueryQualifyName
reallyAlwaysQualifyNames _ _ = QualifyName
NameNotInScope2

-- | NB: This won't ever show package IDs
alwaysQualifyNames :: QueryQualifyName
alwaysQualifyNames :: QueryQualifyName
alwaysQualifyNames m :: Module
m _ = ModuleName -> QualifyName
NameQual (Module -> ModuleName
moduleName Module
m)

neverQualifyNames :: QueryQualifyName
neverQualifyNames :: QueryQualifyName
neverQualifyNames _ _ = QualifyName
NameUnqual

alwaysQualifyModules :: QueryQualifyModule
alwaysQualifyModules :: QueryQualifyModule
alwaysQualifyModules _ = Bool
True

neverQualifyModules :: QueryQualifyModule
neverQualifyModules :: QueryQualifyModule
neverQualifyModules _ = Bool
False

alwaysQualifyPackages :: QueryQualifyPackage
alwaysQualifyPackages :: QueryQualifyPackage
alwaysQualifyPackages _ = Bool
True

neverQualifyPackages :: QueryQualifyPackage
neverQualifyPackages :: QueryQualifyPackage
neverQualifyPackages _ = Bool
False

reallyAlwaysQualify, alwaysQualify, neverQualify :: PrintUnqualified
reallyAlwaysQualify :: PrintUnqualified
reallyAlwaysQualify
              = QueryQualifyName
-> QueryQualifyModule -> QueryQualifyPackage -> PrintUnqualified
QueryQualify QueryQualifyName
reallyAlwaysQualifyNames
                             QueryQualifyModule
alwaysQualifyModules
                             QueryQualifyPackage
alwaysQualifyPackages
alwaysQualify :: PrintUnqualified
alwaysQualify = QueryQualifyName
-> QueryQualifyModule -> QueryQualifyPackage -> PrintUnqualified
QueryQualify QueryQualifyName
alwaysQualifyNames
                             QueryQualifyModule
alwaysQualifyModules
                             QueryQualifyPackage
alwaysQualifyPackages
neverQualify :: PrintUnqualified
neverQualify  = QueryQualifyName
-> QueryQualifyModule -> QueryQualifyPackage -> PrintUnqualified
QueryQualify QueryQualifyName
neverQualifyNames
                             QueryQualifyModule
neverQualifyModules
                             QueryQualifyPackage
neverQualifyPackages

defaultUserStyle :: DynFlags -> PprStyle
defaultUserStyle :: DynFlags -> PprStyle
defaultUserStyle dflags :: DynFlags
dflags = DynFlags -> PrintUnqualified -> Depth -> PprStyle
mkUserStyle DynFlags
dflags PrintUnqualified
neverQualify Depth
AllTheWay

defaultDumpStyle :: DynFlags -> PprStyle
 -- Print without qualifiers to reduce verbosity, unless -dppr-debug
defaultDumpStyle :: DynFlags -> PprStyle
defaultDumpStyle dflags :: DynFlags
dflags
   | DynFlags -> Bool
hasPprDebug DynFlags
dflags = PprStyle
PprDebug
   | Bool
otherwise          = PrintUnqualified -> PprStyle
PprDump PrintUnqualified
neverQualify

mkDumpStyle :: DynFlags -> PrintUnqualified -> PprStyle
mkDumpStyle :: DynFlags -> PrintUnqualified -> PprStyle
mkDumpStyle dflags :: DynFlags
dflags print_unqual :: PrintUnqualified
print_unqual
   | DynFlags -> Bool
hasPprDebug DynFlags
dflags = PprStyle
PprDebug
   | Bool
otherwise          = PrintUnqualified -> PprStyle
PprDump PrintUnqualified
print_unqual

defaultErrStyle :: DynFlags -> PprStyle
-- Default style for error messages, when we don't know PrintUnqualified
-- It's a bit of a hack because it doesn't take into account what's in scope
-- Only used for desugarer warnings, and typechecker errors in interface sigs
-- NB that -dppr-debug will still get into PprDebug style
defaultErrStyle :: DynFlags -> PprStyle
defaultErrStyle dflags :: DynFlags
dflags = DynFlags -> PrintUnqualified -> PprStyle
mkErrStyle DynFlags
dflags PrintUnqualified
neverQualify

-- | Style for printing error messages
mkErrStyle :: DynFlags -> PrintUnqualified -> PprStyle
mkErrStyle :: DynFlags -> PrintUnqualified -> PprStyle
mkErrStyle dflags :: DynFlags
dflags qual :: PrintUnqualified
qual =
   DynFlags -> PrintUnqualified -> Depth -> PprStyle
mkUserStyle DynFlags
dflags PrintUnqualified
qual (Int -> Depth
PartWay (DynFlags -> Int
pprUserLength DynFlags
dflags))

cmdlineParserStyle :: DynFlags -> PprStyle
cmdlineParserStyle :: DynFlags -> PprStyle
cmdlineParserStyle dflags :: DynFlags
dflags = DynFlags -> PrintUnqualified -> Depth -> PprStyle
mkUserStyle DynFlags
dflags PrintUnqualified
alwaysQualify Depth
AllTheWay

mkUserStyle :: DynFlags -> PrintUnqualified -> Depth -> PprStyle
mkUserStyle :: DynFlags -> PrintUnqualified -> Depth -> PprStyle
mkUserStyle dflags :: DynFlags
dflags unqual :: PrintUnqualified
unqual depth :: Depth
depth
   | DynFlags -> Bool
hasPprDebug DynFlags
dflags = PprStyle
PprDebug
   | Bool
otherwise          = PrintUnqualified -> Depth -> Coloured -> PprStyle
PprUser PrintUnqualified
unqual Depth
depth Coloured
Uncoloured

setStyleColoured :: Bool -> PprStyle -> PprStyle
setStyleColoured :: Bool -> PprStyle -> PprStyle
setStyleColoured col :: Bool
col style :: PprStyle
style =
  case PprStyle
style of
    PprUser q :: PrintUnqualified
q d :: Depth
d _ -> PrintUnqualified -> Depth -> Coloured -> PprStyle
PprUser PrintUnqualified
q Depth
d Coloured
c
    _             -> PprStyle
style
  where
    c :: Coloured
c | Bool
col       = Coloured
Coloured
      | Bool
otherwise = Coloured
Uncoloured

instance Outputable PprStyle where
  ppr :: PprStyle -> SDoc
ppr (PprUser {})  = String -> SDoc
text "user-style"
  ppr (PprCode {})  = String -> SDoc
text "code-style"
  ppr (PprDump {})  = String -> SDoc
text "dump-style"
  ppr (PprDebug {}) = String -> SDoc
text "debug-style"

{-
Orthogonal to the above printing styles are (possibly) some
command-line flags that affect printing (often carried with the
style).  The most likely ones are variations on how much type info is
shown.

The following test decides whether or not we are actually generating
code (either C or assembly), or generating interface files.

************************************************************************
*                                                                      *
\subsection{The @SDoc@ data type}
*                                                                      *
************************************************************************
-}

-- | Represents a pretty-printable document.
--
-- To display an 'SDoc', use 'printSDoc', 'printSDocLn', 'bufLeftRenderSDoc',
-- or 'renderWithStyle'.  Avoid calling 'runSDoc' directly as it breaks the
-- abstraction layer.
newtype SDoc = SDoc { SDoc -> SDocContext -> Doc
runSDoc :: SDocContext -> Doc }

data SDocContext = SDC
  { SDocContext -> PprStyle
sdocStyle      :: !PprStyle
  , SDocContext -> PprColour
sdocLastColour :: !Col.PprColour
    -- ^ The most recently used colour.  This allows nesting colours.
  , SDocContext -> DynFlags
sdocDynFlags   :: !DynFlags
  }

instance IsString SDoc where
  fromString :: String -> SDoc
fromString = String -> SDoc
text

initSDocContext :: DynFlags -> PprStyle -> SDocContext
initSDocContext :: DynFlags -> PprStyle -> SDocContext
initSDocContext dflags :: DynFlags
dflags sty :: PprStyle
sty = $WSDC :: PprStyle -> PprColour -> DynFlags -> SDocContext
SDC
  { sdocStyle :: PprStyle
sdocStyle = PprStyle
sty
  , sdocLastColour :: PprColour
sdocLastColour = PprColour
Col.colReset
  , sdocDynFlags :: DynFlags
sdocDynFlags = DynFlags
dflags
  }

withPprStyle :: PprStyle -> SDoc -> SDoc
withPprStyle :: PprStyle -> SDoc -> SDoc
withPprStyle sty :: PprStyle
sty d :: SDoc
d = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ctxt :: SDocContext
ctxt -> SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
ctxt{sdocStyle :: PprStyle
sdocStyle=PprStyle
sty}

-- | This is not a recommended way to render 'SDoc', since it breaks the
-- abstraction layer of 'SDoc'.  Prefer to use 'printSDoc', 'printSDocLn',
-- 'bufLeftRenderSDoc', or 'renderWithStyle' instead.
withPprStyleDoc :: DynFlags -> PprStyle -> SDoc -> Doc
withPprStyleDoc :: DynFlags -> PprStyle -> SDoc -> Doc
withPprStyleDoc dflags :: DynFlags
dflags sty :: PprStyle
sty d :: SDoc
d = SDoc -> SDocContext -> Doc
runSDoc SDoc
d (DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags PprStyle
sty)

pprDeeper :: SDoc -> SDoc
pprDeeper :: SDoc -> SDoc
pprDeeper d :: SDoc
d = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ctx :: SDocContext
ctx -> case SDocContext
ctx of
  SDC{sdocStyle :: SDocContext -> PprStyle
sdocStyle=PprUser _ (PartWay 0) _} -> String -> Doc
Pretty.text "..."
  SDC{sdocStyle :: SDocContext -> PprStyle
sdocStyle=PprUser q :: PrintUnqualified
q (PartWay n :: Int
n) c :: Coloured
c} ->
    SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
ctx{sdocStyle :: PprStyle
sdocStyle = PrintUnqualified -> Depth -> Coloured -> PprStyle
PprUser PrintUnqualified
q (Int -> Depth
PartWay (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1)) Coloured
c}
  _ -> SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
ctx

-- | Truncate a list that is longer than the current depth.
pprDeeperList :: ([SDoc] -> SDoc) -> [SDoc] -> SDoc
pprDeeperList :: ([SDoc] -> SDoc) -> [SDoc] -> SDoc
pprDeeperList f :: [SDoc] -> SDoc
f ds :: [SDoc]
ds
  | [SDoc] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [SDoc]
ds   = [SDoc] -> SDoc
f []
  | Bool
otherwise = (SDocContext -> Doc) -> SDoc
SDoc SDocContext -> Doc
work
 where
  work :: SDocContext -> Doc
work ctx :: SDocContext
ctx@SDC{sdocStyle :: SDocContext -> PprStyle
sdocStyle=PprUser q :: PrintUnqualified
q (PartWay n :: Int
n) c :: Coloured
c}
   | Int
nInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==0      = String -> Doc
Pretty.text "..."
   | Bool
otherwise =
      SDoc -> SDocContext -> Doc
runSDoc ([SDoc] -> SDoc
f (Int -> [SDoc] -> [SDoc]
go 0 [SDoc]
ds)) SDocContext
ctx{sdocStyle :: PprStyle
sdocStyle = PrintUnqualified -> Depth -> Coloured -> PprStyle
PprUser PrintUnqualified
q (Int -> Depth
PartWay (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-1)) Coloured
c}
   where
     go :: Int -> [SDoc] -> [SDoc]
go _ [] = []
     go i :: Int
i (d :: SDoc
d:ds :: [SDoc]
ds) | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n    = [String -> SDoc
text "...."]
                 | Bool
otherwise = SDoc
d SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
: Int -> [SDoc] -> [SDoc]
go (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) [SDoc]
ds
  work other_ctx :: SDocContext
other_ctx = SDoc -> SDocContext -> Doc
runSDoc ([SDoc] -> SDoc
f [SDoc]
ds) SDocContext
other_ctx

pprSetDepth :: Depth -> SDoc -> SDoc
pprSetDepth :: Depth -> SDoc -> SDoc
pprSetDepth depth :: Depth
depth doc :: SDoc
doc = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ctx :: SDocContext
ctx ->
    case SDocContext
ctx of
        SDC{sdocStyle :: SDocContext -> PprStyle
sdocStyle=PprUser q :: PrintUnqualified
q _ c :: Coloured
c} ->
            SDoc -> SDocContext -> Doc
runSDoc SDoc
doc SDocContext
ctx{sdocStyle :: PprStyle
sdocStyle = PrintUnqualified -> Depth -> Coloured -> PprStyle
PprUser PrintUnqualified
q Depth
depth Coloured
c}
        _ ->
            SDoc -> SDocContext -> Doc
runSDoc SDoc
doc SDocContext
ctx

getPprStyle :: (PprStyle -> SDoc) -> SDoc
getPprStyle :: (PprStyle -> SDoc) -> SDoc
getPprStyle df :: PprStyle -> SDoc
df = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ctx :: SDocContext
ctx -> SDoc -> SDocContext -> Doc
runSDoc (PprStyle -> SDoc
df (SDocContext -> PprStyle
sdocStyle SDocContext
ctx)) SDocContext
ctx

sdocWithDynFlags :: (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags :: (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags f :: DynFlags -> SDoc
f = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ctx :: SDocContext
ctx -> SDoc -> SDocContext -> Doc
runSDoc (DynFlags -> SDoc
f (SDocContext -> DynFlags
sdocDynFlags SDocContext
ctx)) SDocContext
ctx

sdocWithPlatform :: (Platform -> SDoc) -> SDoc
sdocWithPlatform :: (Platform -> SDoc) -> SDoc
sdocWithPlatform f :: Platform -> SDoc
f = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags (Platform -> SDoc
f (Platform -> SDoc) -> (DynFlags -> Platform) -> DynFlags -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> Platform
targetPlatform)

updSDocDynFlags :: (DynFlags -> DynFlags) -> SDoc -> SDoc
updSDocDynFlags :: (DynFlags -> DynFlags) -> SDoc -> SDoc
updSDocDynFlags upd :: DynFlags -> DynFlags
upd doc :: SDoc
doc
  = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ctx :: SDocContext
ctx -> SDoc -> SDocContext -> Doc
runSDoc SDoc
doc (SDocContext
ctx { sdocDynFlags :: DynFlags
sdocDynFlags = DynFlags -> DynFlags
upd (SDocContext -> DynFlags
sdocDynFlags SDocContext
ctx) })

qualName :: PprStyle -> QueryQualifyName
qualName :: PprStyle -> QueryQualifyName
qualName (PprUser q :: PrintUnqualified
q _ _) mod :: Module
mod occ :: OccName
occ = PrintUnqualified -> QueryQualifyName
queryQualifyName PrintUnqualified
q Module
mod OccName
occ
qualName (PprDump q :: PrintUnqualified
q)     mod :: Module
mod occ :: OccName
occ = PrintUnqualified -> QueryQualifyName
queryQualifyName PrintUnqualified
q Module
mod OccName
occ
qualName _other :: PprStyle
_other          mod :: Module
mod _   = ModuleName -> QualifyName
NameQual (Module -> ModuleName
moduleName Module
mod)

qualModule :: PprStyle -> QueryQualifyModule
qualModule :: PprStyle -> QueryQualifyModule
qualModule (PprUser q :: PrintUnqualified
q _ _)  m :: Module
m = PrintUnqualified -> QueryQualifyModule
queryQualifyModule PrintUnqualified
q Module
m
qualModule (PprDump q :: PrintUnqualified
q)      m :: Module
m = PrintUnqualified -> QueryQualifyModule
queryQualifyModule PrintUnqualified
q Module
m
qualModule _other :: PprStyle
_other          _m :: Module
_m = Bool
True

qualPackage :: PprStyle -> QueryQualifyPackage
qualPackage :: PprStyle -> QueryQualifyPackage
qualPackage (PprUser q :: PrintUnqualified
q _ _)  m :: UnitId
m = PrintUnqualified -> QueryQualifyPackage
queryQualifyPackage PrintUnqualified
q UnitId
m
qualPackage (PprDump q :: PrintUnqualified
q)      m :: UnitId
m = PrintUnqualified -> QueryQualifyPackage
queryQualifyPackage PrintUnqualified
q UnitId
m
qualPackage _other :: PprStyle
_other          _m :: UnitId
_m = Bool
True

queryQual :: PprStyle -> PrintUnqualified
queryQual :: PprStyle -> PrintUnqualified
queryQual s :: PprStyle
s = QueryQualifyName
-> QueryQualifyModule -> QueryQualifyPackage -> PrintUnqualified
QueryQualify (PprStyle -> QueryQualifyName
qualName PprStyle
s)
                           (PprStyle -> QueryQualifyModule
qualModule PprStyle
s)
                           (PprStyle -> QueryQualifyPackage
qualPackage PprStyle
s)

codeStyle :: PprStyle -> Bool
codeStyle :: PprStyle -> Bool
codeStyle (PprCode _)     = Bool
True
codeStyle _               = Bool
False

asmStyle :: PprStyle -> Bool
asmStyle :: PprStyle -> Bool
asmStyle (PprCode AsmStyle)  = Bool
True
asmStyle _other :: PprStyle
_other              = Bool
False

dumpStyle :: PprStyle -> Bool
dumpStyle :: PprStyle -> Bool
dumpStyle (PprDump {}) = Bool
True
dumpStyle _other :: PprStyle
_other       = Bool
False

debugStyle :: PprStyle -> Bool
debugStyle :: PprStyle -> Bool
debugStyle PprDebug = Bool
True
debugStyle _other :: PprStyle
_other   = Bool
False

userStyle ::  PprStyle -> Bool
userStyle :: PprStyle -> Bool
userStyle (PprUser {}) = Bool
True
userStyle _other :: PprStyle
_other       = Bool
False

getPprDebug :: (Bool -> SDoc) -> SDoc
getPprDebug :: (Bool -> SDoc) -> SDoc
getPprDebug d :: Bool -> SDoc
d = (PprStyle -> SDoc) -> SDoc
getPprStyle ((PprStyle -> SDoc) -> SDoc) -> (PprStyle -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ sty :: PprStyle
sty -> Bool -> SDoc
d (PprStyle -> Bool
debugStyle PprStyle
sty)

ifPprDebug :: SDoc -> SDoc -> SDoc
-- ^ Says what to do with and without -dppr-debug
ifPprDebug :: SDoc -> SDoc -> SDoc
ifPprDebug yes :: SDoc
yes no :: SDoc
no = (Bool -> SDoc) -> SDoc
getPprDebug ((Bool -> SDoc) -> SDoc) -> (Bool -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ dbg :: Bool
dbg -> if Bool
dbg then SDoc
yes else SDoc
no

whenPprDebug :: SDoc -> SDoc        -- Empty for non-debug style
-- ^ Says what to do with -dppr-debug; without, return empty
whenPprDebug :: SDoc -> SDoc
whenPprDebug d :: SDoc
d = SDoc -> SDoc -> SDoc
ifPprDebug SDoc
d SDoc
empty

-- | The analog of 'Pretty.printDoc_' for 'SDoc', which tries to make sure the
--   terminal doesn't get screwed up by the ANSI color codes if an exception
--   is thrown during pretty-printing.
printSDoc :: Mode -> DynFlags -> Handle -> PprStyle -> SDoc -> IO ()
printSDoc :: Mode -> DynFlags -> Handle -> PprStyle -> SDoc -> IO ()
printSDoc mode :: Mode
mode dflags :: DynFlags
dflags handle :: Handle
handle sty :: PprStyle
sty doc :: SDoc
doc =
  Mode -> Int -> Handle -> Doc -> IO ()
Pretty.printDoc_ Mode
mode Int
cols Handle
handle (SDoc -> SDocContext -> Doc
runSDoc SDoc
doc SDocContext
ctx)
    IO () -> IO () -> IO ()
forall a b. IO a -> IO b -> IO a
`finally`
      Mode -> Int -> Handle -> Doc -> IO ()
Pretty.printDoc_ Mode
mode Int
cols Handle
handle
        (SDoc -> SDocContext -> Doc
runSDoc (PprColour -> SDoc -> SDoc
coloured PprColour
Col.colReset SDoc
empty) SDocContext
ctx)
  where
    cols :: Int
cols = DynFlags -> Int
pprCols DynFlags
dflags
    ctx :: SDocContext
ctx = DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags PprStyle
sty

-- | Like 'printSDoc' but appends an extra newline.
printSDocLn :: Mode -> DynFlags -> Handle -> PprStyle -> SDoc -> IO ()
printSDocLn :: Mode -> DynFlags -> Handle -> PprStyle -> SDoc -> IO ()
printSDocLn mode :: Mode
mode dflags :: DynFlags
dflags handle :: Handle
handle sty :: PprStyle
sty doc :: SDoc
doc =
  Mode -> DynFlags -> Handle -> PprStyle -> SDoc -> IO ()
printSDoc Mode
mode DynFlags
dflags Handle
handle PprStyle
sty (SDoc
doc SDoc -> SDoc -> SDoc
$$ String -> SDoc
text "")

printForUser :: DynFlags -> Handle -> PrintUnqualified -> SDoc -> IO ()
printForUser :: DynFlags -> Handle -> PrintUnqualified -> SDoc -> IO ()
printForUser dflags :: DynFlags
dflags handle :: Handle
handle unqual :: PrintUnqualified
unqual doc :: SDoc
doc
  = Mode -> DynFlags -> Handle -> PprStyle -> SDoc -> IO ()
printSDocLn Mode
PageMode DynFlags
dflags Handle
handle
               (DynFlags -> PrintUnqualified -> Depth -> PprStyle
mkUserStyle DynFlags
dflags PrintUnqualified
unqual Depth
AllTheWay) SDoc
doc

printForUserPartWay :: DynFlags -> Handle -> Int -> PrintUnqualified -> SDoc
                    -> IO ()
printForUserPartWay :: DynFlags -> Handle -> Int -> PrintUnqualified -> SDoc -> IO ()
printForUserPartWay dflags :: DynFlags
dflags handle :: Handle
handle d :: Int
d unqual :: PrintUnqualified
unqual doc :: SDoc
doc
  = Mode -> DynFlags -> Handle -> PprStyle -> SDoc -> IO ()
printSDocLn Mode
PageMode DynFlags
dflags Handle
handle
                (DynFlags -> PrintUnqualified -> Depth -> PprStyle
mkUserStyle DynFlags
dflags PrintUnqualified
unqual (Int -> Depth
PartWay Int
d)) SDoc
doc

-- | Like 'printSDocLn' but specialized with 'LeftMode' and
-- @'PprCode' 'CStyle'@.  This is typically used to output C-- code.
printForC :: DynFlags -> Handle -> SDoc -> IO ()
printForC :: DynFlags -> Handle -> SDoc -> IO ()
printForC dflags :: DynFlags
dflags handle :: Handle
handle doc :: SDoc
doc =
  Mode -> DynFlags -> Handle -> PprStyle -> SDoc -> IO ()
printSDocLn Mode
LeftMode DynFlags
dflags Handle
handle (CodeStyle -> PprStyle
PprCode CodeStyle
CStyle) SDoc
doc

-- | An efficient variant of 'printSDoc' specialized for 'LeftMode' that
-- outputs to a 'BufHandle'.
bufLeftRenderSDoc :: DynFlags -> BufHandle -> PprStyle -> SDoc -> IO ()
bufLeftRenderSDoc :: DynFlags -> BufHandle -> PprStyle -> SDoc -> IO ()
bufLeftRenderSDoc dflags :: DynFlags
dflags bufHandle :: BufHandle
bufHandle sty :: PprStyle
sty doc :: SDoc
doc =
  BufHandle -> Doc -> IO ()
Pretty.bufLeftRender BufHandle
bufHandle (SDoc -> SDocContext -> Doc
runSDoc SDoc
doc (DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags PprStyle
sty))

pprCode :: CodeStyle -> SDoc -> SDoc
pprCode :: CodeStyle -> SDoc -> SDoc
pprCode cs :: CodeStyle
cs d :: SDoc
d = PprStyle -> SDoc -> SDoc
withPprStyle (CodeStyle -> PprStyle
PprCode CodeStyle
cs) SDoc
d

mkCodeStyle :: CodeStyle -> PprStyle
mkCodeStyle :: CodeStyle -> PprStyle
mkCodeStyle = CodeStyle -> PprStyle
PprCode

-- Can't make SDoc an instance of Show because SDoc is just a function type
-- However, Doc *is* an instance of Show
-- showSDoc just blasts it out as a string
showSDoc :: DynFlags -> SDoc -> String
showSDoc :: DynFlags -> SDoc -> String
showSDoc dflags :: DynFlags
dflags sdoc :: SDoc
sdoc = DynFlags -> SDoc -> PprStyle -> String
renderWithStyle DynFlags
dflags SDoc
sdoc (DynFlags -> PprStyle
defaultUserStyle DynFlags
dflags)

-- showSDocUnsafe is unsafe, because `unsafeGlobalDynFlags` might not be
-- initialised yet.
showSDocUnsafe :: SDoc -> String
showSDocUnsafe :: SDoc -> String
showSDocUnsafe sdoc :: SDoc
sdoc = DynFlags -> SDoc -> String
showSDoc DynFlags
unsafeGlobalDynFlags SDoc
sdoc

showPpr :: Outputable a => DynFlags -> a -> String
showPpr :: DynFlags -> a -> String
showPpr dflags :: DynFlags
dflags thing :: a
thing = DynFlags -> SDoc -> String
showSDoc DynFlags
dflags (a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
thing)

showSDocUnqual :: DynFlags -> SDoc -> String
-- Only used by Haddock
showSDocUnqual :: DynFlags -> SDoc -> String
showSDocUnqual dflags :: DynFlags
dflags sdoc :: SDoc
sdoc = DynFlags -> SDoc -> String
showSDoc DynFlags
dflags SDoc
sdoc

showSDocForUser :: DynFlags -> PrintUnqualified -> SDoc -> String
-- Allows caller to specify the PrintUnqualified to use
showSDocForUser :: DynFlags -> PrintUnqualified -> SDoc -> String
showSDocForUser dflags :: DynFlags
dflags unqual :: PrintUnqualified
unqual doc :: SDoc
doc
 = DynFlags -> SDoc -> PprStyle -> String
renderWithStyle DynFlags
dflags SDoc
doc (DynFlags -> PrintUnqualified -> Depth -> PprStyle
mkUserStyle DynFlags
dflags PrintUnqualified
unqual Depth
AllTheWay)

showSDocDump :: DynFlags -> SDoc -> String
showSDocDump :: DynFlags -> SDoc -> String
showSDocDump dflags :: DynFlags
dflags d :: SDoc
d = DynFlags -> SDoc -> PprStyle -> String
renderWithStyle DynFlags
dflags SDoc
d (DynFlags -> PprStyle
defaultDumpStyle DynFlags
dflags)

showSDocDebug :: DynFlags -> SDoc -> String
showSDocDebug :: DynFlags -> SDoc -> String
showSDocDebug dflags :: DynFlags
dflags d :: SDoc
d = DynFlags -> SDoc -> PprStyle -> String
renderWithStyle DynFlags
dflags SDoc
d PprStyle
PprDebug

renderWithStyle :: DynFlags -> SDoc -> PprStyle -> String
renderWithStyle :: DynFlags -> SDoc -> PprStyle -> String
renderWithStyle dflags :: DynFlags
dflags sdoc :: SDoc
sdoc sty :: PprStyle
sty
  = let s :: Style
s = Style
Pretty.style{ mode :: Mode
Pretty.mode = Mode
PageMode,
                          lineLength :: Int
Pretty.lineLength = DynFlags -> Int
pprCols DynFlags
dflags }
    in Style -> Doc -> String
Pretty.renderStyle Style
s (Doc -> String) -> Doc -> String
forall a b. (a -> b) -> a -> b
$ SDoc -> SDocContext -> Doc
runSDoc SDoc
sdoc (DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags PprStyle
sty)

-- This shows an SDoc, but on one line only. It's cheaper than a full
-- showSDoc, designed for when we're getting results like "Foo.bar"
-- and "foo{uniq strictness}" so we don't want fancy layout anyway.
showSDocOneLine :: DynFlags -> SDoc -> String
showSDocOneLine :: DynFlags -> SDoc -> String
showSDocOneLine dflags :: DynFlags
dflags d :: SDoc
d
 = let s :: Style
s = Style
Pretty.style{ mode :: Mode
Pretty.mode = Mode
OneLineMode,
                         lineLength :: Int
Pretty.lineLength = DynFlags -> Int
pprCols DynFlags
dflags } in
   Style -> Doc -> String
Pretty.renderStyle Style
s (Doc -> String) -> Doc -> String
forall a b. (a -> b) -> a -> b
$
      SDoc -> SDocContext -> Doc
runSDoc SDoc
d (DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags (DynFlags -> PprStyle
defaultUserStyle DynFlags
dflags))

showSDocDumpOneLine :: DynFlags -> SDoc -> String
showSDocDumpOneLine :: DynFlags -> SDoc -> String
showSDocDumpOneLine dflags :: DynFlags
dflags d :: SDoc
d
 = let s :: Style
s = Style
Pretty.style{ mode :: Mode
Pretty.mode = Mode
OneLineMode,
                         lineLength :: Int
Pretty.lineLength = Int
irrelevantNCols } in
   Style -> Doc -> String
Pretty.renderStyle Style
s (Doc -> String) -> Doc -> String
forall a b. (a -> b) -> a -> b
$
      SDoc -> SDocContext -> Doc
runSDoc SDoc
d (DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags (DynFlags -> PprStyle
defaultDumpStyle DynFlags
dflags))

irrelevantNCols :: Int
-- Used for OneLineMode and LeftMode when number of cols isn't used
irrelevantNCols :: Int
irrelevantNCols = 1

isEmpty :: DynFlags -> SDoc -> Bool
isEmpty :: DynFlags -> SDoc -> Bool
isEmpty dflags :: DynFlags
dflags sdoc :: SDoc
sdoc = Doc -> Bool
Pretty.isEmpty (Doc -> Bool) -> Doc -> Bool
forall a b. (a -> b) -> a -> b
$ SDoc -> SDocContext -> Doc
runSDoc SDoc
sdoc SDocContext
dummySDocContext
   where dummySDocContext :: SDocContext
dummySDocContext = DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags PprStyle
PprDebug

docToSDoc :: Doc -> SDoc
docToSDoc :: Doc -> SDoc
docToSDoc d :: Doc
d = (SDocContext -> Doc) -> SDoc
SDoc (\_ -> Doc
d)

empty    :: SDoc
char     :: Char       -> SDoc
text     :: String     -> SDoc
ftext    :: FastString -> SDoc
ptext    :: PtrString  -> SDoc
ztext    :: FastZString -> SDoc
int      :: Int        -> SDoc
integer  :: Integer    -> SDoc
word     :: Integer    -> SDoc
float    :: Float      -> SDoc
double   :: Double     -> SDoc
rational :: Rational   -> SDoc

empty :: SDoc
empty       = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.empty
char :: Char -> SDoc
char c :: Char
c      = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Char -> Doc
Pretty.char Char
c

text :: String -> SDoc
text s :: String
s      = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text String
s
{-# INLINE text #-}   -- Inline so that the RULE Pretty.text will fire

ftext :: FastString -> SDoc
ftext s :: FastString
s     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ FastString -> Doc
Pretty.ftext FastString
s
ptext :: PtrString -> SDoc
ptext s :: PtrString
s     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ PtrString -> Doc
Pretty.ptext PtrString
s
ztext :: FastZString -> SDoc
ztext s :: FastZString
s     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ FastZString -> Doc
Pretty.ztext FastZString
s
int :: Int -> SDoc
int n :: Int
n       = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Int -> Doc
Pretty.int Int
n
integer :: Integer -> SDoc
integer n :: Integer
n   = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Integer -> Doc
Pretty.integer Integer
n
float :: Float -> SDoc
float n :: Float
n     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Float -> Doc
Pretty.float Float
n
double :: Double -> SDoc
double n :: Double
n    = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Double -> Doc
Pretty.double Double
n
rational :: Rational -> SDoc
rational n :: Rational
n  = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Rational -> Doc
Pretty.rational Rational
n
word :: Integer -> SDoc
word n :: Integer
n      = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
    -- See Note [Print Hexadecimal Literals] in Pretty.hs
    if DynFlags -> Bool
shouldUseHexWordLiterals DynFlags
dflags
        then Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Integer -> Doc
Pretty.hex Integer
n
        else Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Integer -> Doc
Pretty.integer Integer
n

-- | @doublePrec p n@ shows a floating point number @n@ with @p@
-- digits of precision after the decimal point.
doublePrec :: Int -> Double -> SDoc
doublePrec :: Int -> Double -> SDoc
doublePrec p :: Int
p n :: Double
n = String -> SDoc
text (Maybe Int -> Double -> ShowS
forall a. RealFloat a => Maybe Int -> a -> ShowS
showFFloat (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
p) Double
n "")

parens, braces, brackets, quotes, quote,
        doubleQuotes, angleBrackets :: SDoc -> SDoc

parens :: SDoc -> SDoc
parens d :: SDoc
d        = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
Pretty.parens (Doc -> Doc) -> (SDocContext -> Doc) -> SDocContext -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> SDocContext -> Doc
runSDoc SDoc
d
braces :: SDoc -> SDoc
braces d :: SDoc
d        = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
Pretty.braces (Doc -> Doc) -> (SDocContext -> Doc) -> SDocContext -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> SDocContext -> Doc
runSDoc SDoc
d
brackets :: SDoc -> SDoc
brackets d :: SDoc
d      = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
Pretty.brackets (Doc -> Doc) -> (SDocContext -> Doc) -> SDocContext -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> SDocContext -> Doc
runSDoc SDoc
d
quote :: SDoc -> SDoc
quote d :: SDoc
d         = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
Pretty.quote (Doc -> Doc) -> (SDocContext -> Doc) -> SDocContext -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> SDocContext -> Doc
runSDoc SDoc
d
doubleQuotes :: SDoc -> SDoc
doubleQuotes d :: SDoc
d  = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
Pretty.doubleQuotes (Doc -> Doc) -> (SDocContext -> Doc) -> SDocContext -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> SDocContext -> Doc
runSDoc SDoc
d
angleBrackets :: SDoc -> SDoc
angleBrackets d :: SDoc
d = Char -> SDoc
char '<' SDoc -> SDoc -> SDoc
<> SDoc
d SDoc -> SDoc -> SDoc
<> Char -> SDoc
char '>'

cparen :: Bool -> SDoc -> SDoc
cparen :: Bool -> SDoc -> SDoc
cparen b :: Bool
b d :: SDoc
d = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ Bool -> Doc -> Doc
Pretty.maybeParens Bool
b (Doc -> Doc) -> (SDocContext -> Doc) -> SDocContext -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> SDocContext -> Doc
runSDoc SDoc
d

-- 'quotes' encloses something in single quotes...
-- but it omits them if the thing begins or ends in a single quote
-- so that we don't get `foo''.  Instead we just have foo'.
quotes :: SDoc -> SDoc
quotes d :: SDoc
d =
      (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
      if DynFlags -> Bool
useUnicode DynFlags
dflags
      then Char -> SDoc
char '‘' SDoc -> SDoc -> SDoc
<> SDoc
d SDoc -> SDoc -> SDoc
<> Char -> SDoc
char '’'
      else (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty ->
           let pp_d :: Doc
pp_d = SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
sty
               str :: String
str  = Doc -> String
forall a. Show a => a -> String
show Doc
pp_d
           in case (String
str, String -> Maybe (String, Char)
forall a. [a] -> Maybe ([a], a)
snocView String
str) of
             (_, Just (_, '\'')) -> Doc
pp_d
             ('\'' : _, _)       -> Doc
pp_d
             _other :: (String, Maybe (String, Char))
_other              -> Doc -> Doc
Pretty.quotes Doc
pp_d

semi, comma, colon, equals, space, dcolon, underscore, dot, vbar :: SDoc
arrow, larrow, darrow, arrowt, larrowt, arrowtt, larrowtt :: SDoc
lparen, rparen, lbrack, rbrack, lbrace, rbrace, blankLine :: SDoc

blankLine :: SDoc
blankLine  = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text ""
dcolon :: SDoc
dcolon     = SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '∷') (Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text "::")
arrow :: SDoc
arrow      = SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '→') (Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text "->")
larrow :: SDoc
larrow     = SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '←') (Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text "<-")
darrow :: SDoc
darrow     = SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '⇒') (Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text "=>")
arrowt :: SDoc
arrowt     = SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '⤚') (Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text ">-")
larrowt :: SDoc
larrowt    = SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '⤙') (Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text "-<")
arrowtt :: SDoc
arrowtt    = SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '⤜') (Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text ">>-")
larrowtt :: SDoc
larrowtt   = SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '⤛') (Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ String -> Doc
Pretty.text "-<<")
semi :: SDoc
semi       = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.semi
comma :: SDoc
comma      = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.comma
colon :: SDoc
colon      = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.colon
equals :: SDoc
equals     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.equals
space :: SDoc
space      = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.space
underscore :: SDoc
underscore = Char -> SDoc
char '_'
dot :: SDoc
dot        = Char -> SDoc
char '.'
vbar :: SDoc
vbar       = Char -> SDoc
char '|'
lparen :: SDoc
lparen     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.lparen
rparen :: SDoc
rparen     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.rparen
lbrack :: SDoc
lbrack     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.lbrack
rbrack :: SDoc
rbrack     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.rbrack
lbrace :: SDoc
lbrace     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.lbrace
rbrace :: SDoc
rbrace     = Doc -> SDoc
docToSDoc (Doc -> SDoc) -> Doc -> SDoc
forall a b. (a -> b) -> a -> b
$ Doc
Pretty.rbrace

forAllLit :: SDoc
forAllLit :: SDoc
forAllLit = SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '∀') (String -> SDoc
text "forall")

kindType :: SDoc
kindType :: SDoc
kindType = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
    if DynFlags -> Bool
useStarIsType DynFlags
dflags
    then SDoc -> SDoc -> SDoc
unicodeSyntax (Char -> SDoc
char '★') (Char -> SDoc
char '*')
    else String -> SDoc
text "Type"

bullet :: SDoc
bullet :: SDoc
bullet = SDoc -> SDoc -> SDoc
unicode (Char -> SDoc
char '•') (Char -> SDoc
char '*')

unicodeSyntax :: SDoc -> SDoc -> SDoc
unicodeSyntax :: SDoc -> SDoc -> SDoc
unicodeSyntax unicode :: SDoc
unicode plain :: SDoc
plain = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
    if DynFlags -> Bool
useUnicode DynFlags
dflags Bool -> Bool -> Bool
&& DynFlags -> Bool
useUnicodeSyntax DynFlags
dflags
    then SDoc
unicode
    else SDoc
plain

unicode :: SDoc -> SDoc -> SDoc
unicode :: SDoc -> SDoc -> SDoc
unicode unicode :: SDoc
unicode plain :: SDoc
plain = (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
    if DynFlags -> Bool
useUnicode DynFlags
dflags
    then SDoc
unicode
    else SDoc
plain

nest :: Int -> SDoc -> SDoc
-- ^ Indent 'SDoc' some specified amount
(<>) :: SDoc -> SDoc -> SDoc
-- ^ Join two 'SDoc' together horizontally without a gap
(<+>) :: SDoc -> SDoc -> SDoc
-- ^ Join two 'SDoc' together horizontally with a gap between them
($$) :: SDoc -> SDoc -> SDoc
-- ^ Join two 'SDoc' together vertically; if there is
-- no vertical overlap it "dovetails" the two onto one line
($+$) :: SDoc -> SDoc -> SDoc
-- ^ Join two 'SDoc' together vertically

nest :: Int -> SDoc -> SDoc
nest n :: Int
n d :: SDoc
d    = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ Int -> Doc -> Doc
Pretty.nest Int
n (Doc -> Doc) -> (SDocContext -> Doc) -> SDocContext -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> SDocContext -> Doc
runSDoc SDoc
d
<> :: SDoc -> SDoc -> SDoc
(<>) d1 :: SDoc
d1 d2 :: SDoc
d2  = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> Doc -> Doc -> Doc
(Pretty.<>)  (SDoc -> SDocContext -> Doc
runSDoc SDoc
d1 SDocContext
sty) (SDoc -> SDocContext -> Doc
runSDoc SDoc
d2 SDocContext
sty)
<+> :: SDoc -> SDoc -> SDoc
(<+>) d1 :: SDoc
d1 d2 :: SDoc
d2 = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> Doc -> Doc -> Doc
(Pretty.<+>) (SDoc -> SDocContext -> Doc
runSDoc SDoc
d1 SDocContext
sty) (SDoc -> SDocContext -> Doc
runSDoc SDoc
d2 SDocContext
sty)
$$ :: SDoc -> SDoc -> SDoc
($$) d1 :: SDoc
d1 d2 :: SDoc
d2  = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> Doc -> Doc -> Doc
(Pretty.$$)  (SDoc -> SDocContext -> Doc
runSDoc SDoc
d1 SDocContext
sty) (SDoc -> SDocContext -> Doc
runSDoc SDoc
d2 SDocContext
sty)
$+$ :: SDoc -> SDoc -> SDoc
($+$) d1 :: SDoc
d1 d2 :: SDoc
d2 = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> Doc -> Doc -> Doc
(Pretty.$+$) (SDoc -> SDocContext -> Doc
runSDoc SDoc
d1 SDocContext
sty) (SDoc -> SDocContext -> Doc
runSDoc SDoc
d2 SDocContext
sty)

hcat :: [SDoc] -> SDoc
-- ^ Concatenate 'SDoc' horizontally
hsep :: [SDoc] -> SDoc
-- ^ Concatenate 'SDoc' horizontally with a space between each one
vcat :: [SDoc] -> SDoc
-- ^ Concatenate 'SDoc' vertically with dovetailing
sep :: [SDoc] -> SDoc
-- ^ Separate: is either like 'hsep' or like 'vcat', depending on what fits
cat :: [SDoc] -> SDoc
-- ^ Catenate: is either like 'hcat' or like 'vcat', depending on what fits
fsep :: [SDoc] -> SDoc
-- ^ A paragraph-fill combinator. It's much like sep, only it
-- keeps fitting things on one line until it can't fit any more.
fcat :: [SDoc] -> SDoc
-- ^ This behaves like 'fsep', but it uses '<>' for horizontal conposition rather than '<+>'


hcat :: [SDoc] -> SDoc
hcat ds :: [SDoc]
ds = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> [Doc] -> Doc
Pretty.hcat [SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
sty | SDoc
d <- [SDoc]
ds]
hsep :: [SDoc] -> SDoc
hsep ds :: [SDoc]
ds = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> [Doc] -> Doc
Pretty.hsep [SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
sty | SDoc
d <- [SDoc]
ds]
vcat :: [SDoc] -> SDoc
vcat ds :: [SDoc]
ds = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> [Doc] -> Doc
Pretty.vcat [SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
sty | SDoc
d <- [SDoc]
ds]
sep :: [SDoc] -> SDoc
sep ds :: [SDoc]
ds  = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> [Doc] -> Doc
Pretty.sep  [SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
sty | SDoc
d <- [SDoc]
ds]
cat :: [SDoc] -> SDoc
cat ds :: [SDoc]
ds  = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> [Doc] -> Doc
Pretty.cat  [SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
sty | SDoc
d <- [SDoc]
ds]
fsep :: [SDoc] -> SDoc
fsep ds :: [SDoc]
ds = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> [Doc] -> Doc
Pretty.fsep [SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
sty | SDoc
d <- [SDoc]
ds]
fcat :: [SDoc] -> SDoc
fcat ds :: [SDoc]
ds = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> [Doc] -> Doc
Pretty.fcat [SDoc -> SDocContext -> Doc
runSDoc SDoc
d SDocContext
sty | SDoc
d <- [SDoc]
ds]

hang :: SDoc  -- ^ The header
      -> Int  -- ^ Amount to indent the hung body
      -> SDoc -- ^ The hung body, indented and placed below the header
      -> SDoc
hang :: SDoc -> Int -> SDoc -> SDoc
hang d1 :: SDoc
d1 n :: Int
n d2 :: SDoc
d2   = (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> Doc -> Int -> Doc -> Doc
Pretty.hang (SDoc -> SDocContext -> Doc
runSDoc SDoc
d1 SDocContext
sty) Int
n (SDoc -> SDocContext -> Doc
runSDoc SDoc
d2 SDocContext
sty)

-- | This behaves like 'hang', but does not indent the second document
-- when the header is empty.
hangNotEmpty :: SDoc -> Int -> SDoc -> SDoc
hangNotEmpty :: SDoc -> Int -> SDoc -> SDoc
hangNotEmpty d1 :: SDoc
d1 n :: Int
n d2 :: SDoc
d2 =
    (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \sty :: SDocContext
sty -> Doc -> Int -> Doc -> Doc
Pretty.hangNotEmpty (SDoc -> SDocContext -> Doc
runSDoc SDoc
d1 SDocContext
sty) Int
n (SDoc -> SDocContext -> Doc
runSDoc SDoc
d2 SDocContext
sty)

punctuate :: SDoc   -- ^ The punctuation
          -> [SDoc] -- ^ The list that will have punctuation added between every adjacent pair of elements
          -> [SDoc] -- ^ Punctuated list
punctuate :: SDoc -> [SDoc] -> [SDoc]
punctuate _ []     = []
punctuate p :: SDoc
p (d :: SDoc
d:ds :: [SDoc]
ds) = SDoc -> [SDoc] -> [SDoc]
go SDoc
d [SDoc]
ds
                   where
                     go :: SDoc -> [SDoc] -> [SDoc]
go d :: SDoc
d [] = [SDoc
d]
                     go d :: SDoc
d (e :: SDoc
e:es :: [SDoc]
es) = (SDoc
d SDoc -> SDoc -> SDoc
<> SDoc
p) SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
: SDoc -> [SDoc] -> [SDoc]
go SDoc
e [SDoc]
es

ppWhen, ppUnless :: Bool -> SDoc -> SDoc
ppWhen :: Bool -> SDoc -> SDoc
ppWhen True  doc :: SDoc
doc = SDoc
doc
ppWhen False _   = SDoc
empty

ppUnless :: Bool -> SDoc -> SDoc
ppUnless True  _   = SDoc
empty
ppUnless False doc :: SDoc
doc = SDoc
doc

-- | Apply the given colour\/style for the argument.
--
-- Only takes effect if colours are enabled.
coloured :: Col.PprColour -> SDoc -> SDoc
coloured :: PprColour -> SDoc -> SDoc
coloured col :: PprColour
col sdoc :: SDoc
sdoc =
  (DynFlags -> SDoc) -> SDoc
sdocWithDynFlags ((DynFlags -> SDoc) -> SDoc) -> (DynFlags -> SDoc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \dflags :: DynFlags
dflags ->
    if DynFlags -> Bool
shouldUseColor DynFlags
dflags
    then (SDocContext -> Doc) -> SDoc
SDoc ((SDocContext -> Doc) -> SDoc) -> (SDocContext -> Doc) -> SDoc
forall a b. (a -> b) -> a -> b
$ \ctx :: SDocContext
ctx@SDC{ sdocLastColour :: SDocContext -> PprColour
sdocLastColour = PprColour
lastCol } ->
         case SDocContext
ctx of
           SDC{ sdocStyle :: SDocContext -> PprStyle
sdocStyle = PprUser _ _ Coloured } ->
             let ctx' :: SDocContext
ctx' = SDocContext
ctx{ sdocLastColour :: PprColour
sdocLastColour = PprColour
lastCol PprColour -> PprColour -> PprColour
forall a. Monoid a => a -> a -> a
`mappend` PprColour
col } in
             String -> Doc
Pretty.zeroWidthText (PprColour -> String
Col.renderColour PprColour
col)
               Doc -> Doc -> Doc
Pretty.<> SDoc -> SDocContext -> Doc
runSDoc SDoc
sdoc SDocContext
ctx'
               Doc -> Doc -> Doc
Pretty.<> String -> Doc
Pretty.zeroWidthText (PprColour -> String
Col.renderColourAfresh PprColour
lastCol)
           _ -> SDoc -> SDocContext -> Doc
runSDoc SDoc
sdoc SDocContext
ctx
    else SDoc
sdoc

keyword :: SDoc -> SDoc
keyword :: SDoc -> SDoc
keyword = PprColour -> SDoc -> SDoc
coloured PprColour
Col.colBold

{-
************************************************************************
*                                                                      *
\subsection[Outputable-class]{The @Outputable@ class}
*                                                                      *
************************************************************************
-}

-- | Class designating that some type has an 'SDoc' representation
class Outputable a where
        ppr :: a -> SDoc
        pprPrec :: Rational -> a -> SDoc
                -- 0 binds least tightly
                -- We use Rational because there is always a
                -- Rational between any other two Rationals

        ppr = Rational -> a -> SDoc
forall a. Outputable a => Rational -> a -> SDoc
pprPrec 0
        pprPrec _ = a -> SDoc
forall a. Outputable a => a -> SDoc
ppr

instance Outputable Char where
    ppr :: Char -> SDoc
ppr c :: Char
c = String -> SDoc
text [Char
c]

instance Outputable Bool where
    ppr :: Bool -> SDoc
ppr True  = String -> SDoc
text "True"
    ppr False = String -> SDoc
text "False"

instance Outputable Ordering where
    ppr :: Ordering -> SDoc
ppr LT = String -> SDoc
text "LT"
    ppr EQ = String -> SDoc
text "EQ"
    ppr GT = String -> SDoc
text "GT"

instance Outputable Int32 where
   ppr :: Int32 -> SDoc
ppr n :: Int32
n = Integer -> SDoc
integer (Integer -> SDoc) -> Integer -> SDoc
forall a b. (a -> b) -> a -> b
$ Int32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
n

instance Outputable Int64 where
   ppr :: Int64 -> SDoc
ppr n :: Int64
n = Integer -> SDoc
integer (Integer -> SDoc) -> Integer -> SDoc
forall a b. (a -> b) -> a -> b
$ Int64 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
n

instance Outputable Int where
    ppr :: Int -> SDoc
ppr n :: Int
n = Int -> SDoc
int Int
n

instance Outputable Integer where
    ppr :: Integer -> SDoc
ppr n :: Integer
n = Integer -> SDoc
integer Integer
n

instance Outputable Word16 where
    ppr :: Word16 -> SDoc
ppr n :: Word16
n = Integer -> SDoc
integer (Integer -> SDoc) -> Integer -> SDoc
forall a b. (a -> b) -> a -> b
$ Word16 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
n

instance Outputable Word32 where
    ppr :: Word32 -> SDoc
ppr n :: Word32
n = Integer -> SDoc
integer (Integer -> SDoc) -> Integer -> SDoc
forall a b. (a -> b) -> a -> b
$ Word32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
n

instance Outputable Word where
    ppr :: Word -> SDoc
ppr n :: Word
n = Integer -> SDoc
integer (Integer -> SDoc) -> Integer -> SDoc
forall a b. (a -> b) -> a -> b
$ Word -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
n

instance Outputable () where
    ppr :: () -> SDoc
ppr _ = String -> SDoc
text "()"

instance (Outputable a) => Outputable [a] where
    ppr :: [a] -> SDoc
ppr xs :: [a]
xs = SDoc -> SDoc
brackets ([SDoc] -> SDoc
fsep (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma ((a -> SDoc) -> [a] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map a -> SDoc
forall a. Outputable a => a -> SDoc
ppr [a]
xs)))

instance (Outputable a) => Outputable (Set a) where
    ppr :: Set a -> SDoc
ppr s :: Set a
s = SDoc -> SDoc
braces ([SDoc] -> SDoc
fsep (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma ((a -> SDoc) -> [a] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map a -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Set a -> [a]
forall a. Set a -> [a]
Set.toList Set a
s))))

instance (Outputable a, Outputable b) => Outputable (a, b) where
    ppr :: (a, b) -> SDoc
ppr (x :: a
x,y :: b
y) = SDoc -> SDoc
parens ([SDoc] -> SDoc
sep [a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
x SDoc -> SDoc -> SDoc
<> SDoc
comma, b -> SDoc
forall a. Outputable a => a -> SDoc
ppr b
y])

instance Outputable a => Outputable (Maybe a) where
    ppr :: Maybe a -> SDoc
ppr Nothing  = String -> SDoc
text "Nothing"
    ppr (Just x :: a
x) = String -> SDoc
text "Just" SDoc -> SDoc -> SDoc
<+> a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
x

instance (Outputable a, Outputable b) => Outputable (Either a b) where
    ppr :: Either a b -> SDoc
ppr (Left x :: a
x)  = String -> SDoc
text "Left"  SDoc -> SDoc -> SDoc
<+> a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
x
    ppr (Right y :: b
y) = String -> SDoc
text "Right" SDoc -> SDoc -> SDoc
<+> b -> SDoc
forall a. Outputable a => a -> SDoc
ppr b
y

-- ToDo: may not be used
instance (Outputable a, Outputable b, Outputable c) => Outputable (a, b, c) where
    ppr :: (a, b, c) -> SDoc
ppr (x :: a
x,y :: b
y,z :: c
z) =
      SDoc -> SDoc
parens ([SDoc] -> SDoc
sep [a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
x SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   b -> SDoc
forall a. Outputable a => a -> SDoc
ppr b
y SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   c -> SDoc
forall a. Outputable a => a -> SDoc
ppr c
z ])

instance (Outputable a, Outputable b, Outputable c, Outputable d) =>
         Outputable (a, b, c, d) where
    ppr :: (a, b, c, d) -> SDoc
ppr (a :: a
a,b :: b
b,c :: c
c,d :: d
d) =
      SDoc -> SDoc
parens ([SDoc] -> SDoc
sep [a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
a SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   b -> SDoc
forall a. Outputable a => a -> SDoc
ppr b
b SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   c -> SDoc
forall a. Outputable a => a -> SDoc
ppr c
c SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   d -> SDoc
forall a. Outputable a => a -> SDoc
ppr d
d])

instance (Outputable a, Outputable b, Outputable c, Outputable d, Outputable e) =>
         Outputable (a, b, c, d, e) where
    ppr :: (a, b, c, d, e) -> SDoc
ppr (a :: a
a,b :: b
b,c :: c
c,d :: d
d,e :: e
e) =
      SDoc -> SDoc
parens ([SDoc] -> SDoc
sep [a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
a SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   b -> SDoc
forall a. Outputable a => a -> SDoc
ppr b
b SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   c -> SDoc
forall a. Outputable a => a -> SDoc
ppr c
c SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   d -> SDoc
forall a. Outputable a => a -> SDoc
ppr d
d SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   e -> SDoc
forall a. Outputable a => a -> SDoc
ppr e
e])

instance (Outputable a, Outputable b, Outputable c, Outputable d, Outputable e, Outputable f) =>
         Outputable (a, b, c, d, e, f) where
    ppr :: (a, b, c, d, e, f) -> SDoc
ppr (a :: a
a,b :: b
b,c :: c
c,d :: d
d,e :: e
e,f :: f
f) =
      SDoc -> SDoc
parens ([SDoc] -> SDoc
sep [a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
a SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   b -> SDoc
forall a. Outputable a => a -> SDoc
ppr b
b SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   c -> SDoc
forall a. Outputable a => a -> SDoc
ppr c
c SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   d -> SDoc
forall a. Outputable a => a -> SDoc
ppr d
d SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   e -> SDoc
forall a. Outputable a => a -> SDoc
ppr e
e SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   f -> SDoc
forall a. Outputable a => a -> SDoc
ppr f
f])

instance (Outputable a, Outputable b, Outputable c, Outputable d, Outputable e, Outputable f, Outputable g) =>
         Outputable (a, b, c, d, e, f, g) where
    ppr :: (a, b, c, d, e, f, g) -> SDoc
ppr (a :: a
a,b :: b
b,c :: c
c,d :: d
d,e :: e
e,f :: f
f,g :: g
g) =
      SDoc -> SDoc
parens ([SDoc] -> SDoc
sep [a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
a SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   b -> SDoc
forall a. Outputable a => a -> SDoc
ppr b
b SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   c -> SDoc
forall a. Outputable a => a -> SDoc
ppr c
c SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   d -> SDoc
forall a. Outputable a => a -> SDoc
ppr d
d SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   e -> SDoc
forall a. Outputable a => a -> SDoc
ppr e
e SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   f -> SDoc
forall a. Outputable a => a -> SDoc
ppr f
f SDoc -> SDoc -> SDoc
<> SDoc
comma,
                   g -> SDoc
forall a. Outputable a => a -> SDoc
ppr g
g])

instance Outputable FastString where
    ppr :: FastString -> SDoc
ppr fs :: FastString
fs = FastString -> SDoc
ftext FastString
fs           -- Prints an unadorned string,
                                -- no double quotes or anything

instance (Outputable key, Outputable elt) => Outputable (M.Map key elt) where
    ppr :: Map key elt -> SDoc
ppr m :: Map key elt
m = [(key, elt)] -> SDoc
forall a. Outputable a => a -> SDoc
ppr (Map key elt -> [(key, elt)]
forall k a. Map k a -> [(k, a)]
M.toList Map key elt
m)
instance (Outputable elt) => Outputable (IM.IntMap elt) where
    ppr :: IntMap elt -> SDoc
ppr m :: IntMap elt
m = [(Int, elt)] -> SDoc
forall a. Outputable a => a -> SDoc
ppr (IntMap elt -> [(Int, elt)]
forall a. IntMap a -> [(Int, a)]
IM.toList IntMap elt
m)

instance Outputable Fingerprint where
    ppr :: Fingerprint -> SDoc
ppr (Fingerprint w1 :: Word64
w1 w2 :: Word64
w2) = String -> SDoc
text (String -> Word64 -> Word64 -> String
forall r. PrintfType r => String -> r
printf "%016x%016x" Word64
w1 Word64
w2)

instance Outputable a => Outputable (SCC a) where
   ppr :: SCC a -> SDoc
ppr (AcyclicSCC v :: a
v) = String -> SDoc
text "NONREC" SDoc -> SDoc -> SDoc
$$ (Int -> SDoc -> SDoc
nest 3 (a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
v))
   ppr (CyclicSCC vs :: [a]
vs) = String -> SDoc
text "REC" SDoc -> SDoc -> SDoc
$$ (Int -> SDoc -> SDoc
nest 3 ([SDoc] -> SDoc
vcat ((a -> SDoc) -> [a] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map a -> SDoc
forall a. Outputable a => a -> SDoc
ppr [a]
vs)))

instance Outputable Serialized where
    ppr :: Serialized -> SDoc
ppr (Serialized the_type :: TypeRep
the_type bytes :: [Word8]
bytes) = Int -> SDoc
int ([Word8] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Word8]
bytes) SDoc -> SDoc -> SDoc
<+> String -> SDoc
text "of type" SDoc -> SDoc -> SDoc
<+> String -> SDoc
text (TypeRep -> String
forall a. Show a => a -> String
show TypeRep
the_type)

instance Outputable Extension where
    ppr :: Extension -> SDoc
ppr = String -> SDoc
text (String -> SDoc) -> (Extension -> String) -> Extension -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Extension -> String
forall a. Show a => a -> String
show

{-
************************************************************************
*                                                                      *
\subsection{The @OutputableBndr@ class}
*                                                                      *
************************************************************************
-}

-- | 'BindingSite' is used to tell the thing that prints binder what
-- language construct is binding the identifier.  This can be used
-- to decide how much info to print.
-- Also see Note [Binding-site specific printing] in PprCore
data BindingSite
    = LambdaBind  -- ^ The x in   (\x. e)
    | CaseBind    -- ^ The x in   case scrut of x { (y,z) -> ... }
    | CasePatBind -- ^ The y,z in case scrut of x { (y,z) -> ... }
    | LetBind     -- ^ The x in   (let x = rhs in e)

-- | When we print a binder, we often want to print its type too.
-- The @OutputableBndr@ class encapsulates this idea.
class Outputable a => OutputableBndr a where
   pprBndr :: BindingSite -> a -> SDoc
   pprBndr _b :: BindingSite
_b x :: a
x = a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
x

   pprPrefixOcc, pprInfixOcc :: a -> SDoc
      -- Print an occurrence of the name, suitable either in the
      -- prefix position of an application, thus   (f a b) or  ((+) x)
      -- or infix position,                 thus   (a `f` b) or  (x + y)

   bndrIsJoin_maybe :: a -> Maybe Int
   bndrIsJoin_maybe _ = Maybe Int
forall a. Maybe a
Nothing
      -- When pretty-printing we sometimes want to find
      -- whether the binder is a join point.  You might think
      -- we could have a function of type (a->Var), but Var
      -- isn't available yet, alas

{-
************************************************************************
*                                                                      *
\subsection{Random printing helpers}
*                                                                      *
************************************************************************
-}

-- We have 31-bit Chars and will simply use Show instances of Char and String.

-- | Special combinator for showing character literals.
pprHsChar :: Char -> SDoc
pprHsChar :: Char -> SDoc
pprHsChar c :: Char
c | Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
> '\x10ffff' = Char -> SDoc
char '\\' SDoc -> SDoc -> SDoc
<> String -> SDoc
text (Word32 -> String
forall a. Show a => a -> String
show (Int -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Char -> Int
ord Char
c) :: Word32))
            | Bool
otherwise      = String -> SDoc
text (Char -> String
forall a. Show a => a -> String
show Char
c)

-- | Special combinator for showing string literals.
pprHsString :: FastString -> SDoc
pprHsString :: FastString -> SDoc
pprHsString fs :: FastString
fs = [SDoc] -> SDoc
vcat ((String -> SDoc) -> [String] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map String -> SDoc
text (String -> [String]
showMultiLineString (FastString -> String
unpackFS FastString
fs)))

-- | Special combinator for showing bytestring literals.
pprHsBytes :: ByteString -> SDoc
pprHsBytes :: ByteString -> SDoc
pprHsBytes bs :: ByteString
bs = let escaped :: String
escaped = (Word8 -> String) -> [Word8] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Word8 -> String
escape ([Word8] -> String) -> [Word8] -> String
forall a b. (a -> b) -> a -> b
$ ByteString -> [Word8]
BS.unpack ByteString
bs
                in [SDoc] -> SDoc
vcat ((String -> SDoc) -> [String] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map String -> SDoc
text (String -> [String]
showMultiLineString String
escaped)) SDoc -> SDoc -> SDoc
<> Char -> SDoc
char '#'
    where escape :: Word8 -> String
          escape :: Word8 -> String
escape w :: Word8
w = let c :: Char
c = Int -> Char
chr (Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
w)
                     in if Char -> Bool
isAscii Char
c
                        then [Char
c]
                        else '\\' Char -> ShowS
forall a. a -> [a] -> [a]
: Word8 -> String
forall a. Show a => a -> String
show Word8
w

-- Postfix modifiers for unboxed literals.
-- See Note [Printing of literals in Core] in `basicTypes/Literal.hs`.
primCharSuffix, primFloatSuffix, primIntSuffix :: SDoc
primDoubleSuffix, primWordSuffix, primInt64Suffix, primWord64Suffix :: SDoc
primCharSuffix :: SDoc
primCharSuffix   = Char -> SDoc
char '#'
primFloatSuffix :: SDoc
primFloatSuffix  = Char -> SDoc
char '#'
primIntSuffix :: SDoc
primIntSuffix    = Char -> SDoc
char '#'
primDoubleSuffix :: SDoc
primDoubleSuffix = String -> SDoc
text "##"
primWordSuffix :: SDoc
primWordSuffix   = String -> SDoc
text "##"
primInt64Suffix :: SDoc
primInt64Suffix  = String -> SDoc
text "L#"
primWord64Suffix :: SDoc
primWord64Suffix = String -> SDoc
text "L##"

-- | Special combinator for showing unboxed literals.
pprPrimChar :: Char -> SDoc
pprPrimInt, pprPrimWord, pprPrimInt64, pprPrimWord64 :: Integer -> SDoc
pprPrimChar :: Char -> SDoc
pprPrimChar c :: Char
c   = Char -> SDoc
pprHsChar Char
c SDoc -> SDoc -> SDoc
<> SDoc
primCharSuffix
pprPrimInt :: Integer -> SDoc
pprPrimInt i :: Integer
i    = Integer -> SDoc
integer Integer
i   SDoc -> SDoc -> SDoc
<> SDoc
primIntSuffix
pprPrimWord :: Integer -> SDoc
pprPrimWord w :: Integer
w   = Integer -> SDoc
word    Integer
w   SDoc -> SDoc -> SDoc
<> SDoc
primWordSuffix
pprPrimInt64 :: Integer -> SDoc
pprPrimInt64 i :: Integer
i  = Integer -> SDoc
integer Integer
i   SDoc -> SDoc -> SDoc
<> SDoc
primInt64Suffix
pprPrimWord64 :: Integer -> SDoc
pprPrimWord64 w :: Integer
w = Integer -> SDoc
word    Integer
w   SDoc -> SDoc -> SDoc
<> SDoc
primWord64Suffix

---------------------
-- Put a name in parens if it's an operator
pprPrefixVar :: Bool -> SDoc -> SDoc
pprPrefixVar :: Bool -> SDoc -> SDoc
pprPrefixVar is_operator :: Bool
is_operator pp_v :: SDoc
pp_v
  | Bool
is_operator = SDoc -> SDoc
parens SDoc
pp_v
  | Bool
otherwise   = SDoc
pp_v

-- Put a name in backquotes if it's not an operator
pprInfixVar :: Bool -> SDoc -> SDoc
pprInfixVar :: Bool -> SDoc -> SDoc
pprInfixVar is_operator :: Bool
is_operator pp_v :: SDoc
pp_v
  | Bool
is_operator = SDoc
pp_v
  | Bool
otherwise   = Char -> SDoc
char '`' SDoc -> SDoc -> SDoc
<> SDoc
pp_v SDoc -> SDoc -> SDoc
<> Char -> SDoc
char '`'

---------------------
pprFastFilePath :: FastString -> SDoc
pprFastFilePath :: FastString -> SDoc
pprFastFilePath path :: FastString
path = String -> SDoc
text (String -> SDoc) -> String -> SDoc
forall a b. (a -> b) -> a -> b
$ ShowS
normalise ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ FastString -> String
unpackFS FastString
path

{-
************************************************************************
*                                                                      *
\subsection{Other helper functions}
*                                                                      *
************************************************************************
-}

pprWithCommas :: (a -> SDoc) -- ^ The pretty printing function to use
              -> [a]         -- ^ The things to be pretty printed
              -> SDoc        -- ^ 'SDoc' where the things have been pretty printed,
                             -- comma-separated and finally packed into a paragraph.
pprWithCommas :: (a -> SDoc) -> [a] -> SDoc
pprWithCommas pp :: a -> SDoc
pp xs :: [a]
xs = [SDoc] -> SDoc
fsep (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma ((a -> SDoc) -> [a] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map a -> SDoc
pp [a]
xs))

pprWithBars :: (a -> SDoc) -- ^ The pretty printing function to use
            -> [a]         -- ^ The things to be pretty printed
            -> SDoc        -- ^ 'SDoc' where the things have been pretty printed,
                           -- bar-separated and finally packed into a paragraph.
pprWithBars :: (a -> SDoc) -> [a] -> SDoc
pprWithBars pp :: a -> SDoc
pp xs :: [a]
xs = [SDoc] -> SDoc
fsep (SDoc -> [SDoc] -> [SDoc]
forall a. a -> [a] -> [a]
intersperse SDoc
vbar ((a -> SDoc) -> [a] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map a -> SDoc
pp [a]
xs))

-- | Returns the separated concatenation of the pretty printed things.
interppSP  :: Outputable a => [a] -> SDoc
interppSP :: [a] -> SDoc
interppSP  xs :: [a]
xs = [SDoc] -> SDoc
sep ((a -> SDoc) -> [a] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map a -> SDoc
forall a. Outputable a => a -> SDoc
ppr [a]
xs)

-- | Returns the comma-separated concatenation of the pretty printed things.
interpp'SP :: Outputable a => [a] -> SDoc
interpp'SP :: [a] -> SDoc
interpp'SP xs :: [a]
xs = [SDoc] -> SDoc
sep (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma ((a -> SDoc) -> [a] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map a -> SDoc
forall a. Outputable a => a -> SDoc
ppr [a]
xs))

-- | Returns the comma-separated concatenation of the quoted pretty printed things.
--
-- > [x,y,z]  ==>  `x', `y', `z'
pprQuotedList :: Outputable a => [a] -> SDoc
pprQuotedList :: [a] -> SDoc
pprQuotedList = [SDoc] -> SDoc
quotedList ([SDoc] -> SDoc) -> ([a] -> [SDoc]) -> [a] -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> SDoc) -> [a] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map a -> SDoc
forall a. Outputable a => a -> SDoc
ppr

quotedList :: [SDoc] -> SDoc
quotedList :: [SDoc] -> SDoc
quotedList xs :: [SDoc]
xs = [SDoc] -> SDoc
fsep (SDoc -> [SDoc] -> [SDoc]
punctuate SDoc
comma ((SDoc -> SDoc) -> [SDoc] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map SDoc -> SDoc
quotes [SDoc]
xs))

quotedListWithOr :: [SDoc] -> SDoc
-- [x,y,z]  ==>  `x', `y' or `z'
quotedListWithOr :: [SDoc] -> SDoc
quotedListWithOr xs :: [SDoc]
xs@(_:_:_) = [SDoc] -> SDoc
quotedList ([SDoc] -> [SDoc]
forall a. [a] -> [a]
init [SDoc]
xs) SDoc -> SDoc -> SDoc
<+> String -> SDoc
text "or" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes ([SDoc] -> SDoc
forall a. [a] -> a
last [SDoc]
xs)
quotedListWithOr xs :: [SDoc]
xs = [SDoc] -> SDoc
quotedList [SDoc]
xs

quotedListWithNor :: [SDoc] -> SDoc
-- [x,y,z]  ==>  `x', `y' nor `z'
quotedListWithNor :: [SDoc] -> SDoc
quotedListWithNor xs :: [SDoc]
xs@(_:_:_) = [SDoc] -> SDoc
quotedList ([SDoc] -> [SDoc]
forall a. [a] -> [a]
init [SDoc]
xs) SDoc -> SDoc -> SDoc
<+> String -> SDoc
text "nor" SDoc -> SDoc -> SDoc
<+> SDoc -> SDoc
quotes ([SDoc] -> SDoc
forall a. [a] -> a
last [SDoc]
xs)
quotedListWithNor xs :: [SDoc]
xs = [SDoc] -> SDoc
quotedList [SDoc]
xs

{-
************************************************************************
*                                                                      *
\subsection{Printing numbers verbally}
*                                                                      *
************************************************************************
-}

intWithCommas :: Integral a => a -> SDoc
-- Prints a big integer with commas, eg 345,821
intWithCommas :: a -> SDoc
intWithCommas n :: a
n
  | a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
< 0     = Char -> SDoc
char '-' SDoc -> SDoc -> SDoc
<> a -> SDoc
forall a. Integral a => a -> SDoc
intWithCommas (-a
n)
  | a
q a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== 0    = Int -> SDoc
int (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
r)
  | Bool
otherwise = a -> SDoc
forall a. Integral a => a -> SDoc
intWithCommas a
q SDoc -> SDoc -> SDoc
<> SDoc
comma SDoc -> SDoc -> SDoc
<> SDoc
zeroes SDoc -> SDoc -> SDoc
<> Int -> SDoc
int (a -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral a
r)
  where
    (q :: a
q,r :: a
r) = a
n a -> a -> (a, a)
forall a. Integral a => a -> a -> (a, a)
`quotRem` 1000
    zeroes :: SDoc
zeroes | a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= 100  = SDoc
empty
           | a
r a -> a -> Bool
forall a. Ord a => a -> a -> Bool
>= 10   = Char -> SDoc
char '0'
           | Bool
otherwise = String -> SDoc
text "00"

-- | Converts an integer to a verbal index:
--
-- > speakNth 1 = text "first"
-- > speakNth 5 = text "fifth"
-- > speakNth 21 = text "21st"
speakNth :: Int -> SDoc
speakNth :: Int -> SDoc
speakNth 1 = String -> SDoc
text "first"
speakNth 2 = String -> SDoc
text "second"
speakNth 3 = String -> SDoc
text "third"
speakNth 4 = String -> SDoc
text "fourth"
speakNth 5 = String -> SDoc
text "fifth"
speakNth 6 = String -> SDoc
text "sixth"
speakNth n :: Int
n = [SDoc] -> SDoc
hcat [ Int -> SDoc
int Int
n, String -> SDoc
text String
suffix ]
  where
    suffix :: String
suffix | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 20       = "th"       -- 11,12,13 are non-std
           | Int
last_dig Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = "st"
           | Int
last_dig Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 2 = "nd"
           | Int
last_dig Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== 3 = "rd"
           | Bool
otherwise     = "th"

    last_dig :: Int
last_dig = Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` 10

-- | Converts an integer to a verbal multiplicity:
--
-- > speakN 0 = text "none"
-- > speakN 5 = text "five"
-- > speakN 10 = text "10"
speakN :: Int -> SDoc
speakN :: Int -> SDoc
speakN 0 = String -> SDoc
text "none"  -- E.g.  "he has none"
speakN 1 = String -> SDoc
text "one"   -- E.g.  "he has one"
speakN 2 = String -> SDoc
text "two"
speakN 3 = String -> SDoc
text "three"
speakN 4 = String -> SDoc
text "four"
speakN 5 = String -> SDoc
text "five"
speakN 6 = String -> SDoc
text "six"
speakN n :: Int
n = Int -> SDoc
int Int
n

-- | Converts an integer and object description to a statement about the
-- multiplicity of those objects:
--
-- > speakNOf 0 (text "melon") = text "no melons"
-- > speakNOf 1 (text "melon") = text "one melon"
-- > speakNOf 3 (text "melon") = text "three melons"
speakNOf :: Int -> SDoc -> SDoc
speakNOf :: Int -> SDoc -> SDoc
speakNOf 0 d :: SDoc
d = String -> SDoc
text "no" SDoc -> SDoc -> SDoc
<+> SDoc
d SDoc -> SDoc -> SDoc
<> Char -> SDoc
char 's'
speakNOf 1 d :: SDoc
d = String -> SDoc
text "one" SDoc -> SDoc -> SDoc
<+> SDoc
d                 -- E.g. "one argument"
speakNOf n :: Int
n d :: SDoc
d = Int -> SDoc
speakN Int
n SDoc -> SDoc -> SDoc
<+> SDoc
d SDoc -> SDoc -> SDoc
<> Char -> SDoc
char 's'               -- E.g. "three arguments"

-- | Determines the pluralisation suffix appropriate for the length of a list:
--
-- > plural [] = char 's'
-- > plural ["Hello"] = empty
-- > plural ["Hello", "World"] = char 's'
plural :: [a] -> SDoc
plural :: [a] -> SDoc
plural [_] = SDoc
empty  -- a bit frightening, but there you are
plural _   = Char -> SDoc
char 's'

-- | Determines the form of to be appropriate for the length of a list:
--
-- > isOrAre [] = text "are"
-- > isOrAre ["Hello"] = text "is"
-- > isOrAre ["Hello", "World"] = text "are"
isOrAre :: [a] -> SDoc
isOrAre :: [a] -> SDoc
isOrAre [_] = String -> SDoc
text "is"
isOrAre _   = String -> SDoc
text "are"

-- | Determines the form of to do appropriate for the length of a list:
--
-- > doOrDoes [] = text "do"
-- > doOrDoes ["Hello"] = text "does"
-- > doOrDoes ["Hello", "World"] = text "do"
doOrDoes :: [a] -> SDoc
doOrDoes :: [a] -> SDoc
doOrDoes [_] = String -> SDoc
text "does"
doOrDoes _   = String -> SDoc
text "do"

{-
************************************************************************
*                                                                      *
\subsection{Error handling}
*                                                                      *
************************************************************************
-}

callStackDoc :: HasCallStack => SDoc
callStackDoc :: SDoc
callStackDoc =
    SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text "Call stack:")
       4 ([SDoc] -> SDoc
vcat ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ (String -> SDoc) -> [String] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map String -> SDoc
text ([String] -> [SDoc]) -> [String] -> [SDoc]
forall a b. (a -> b) -> a -> b
$ String -> [String]
lines (CallStack -> String
prettyCallStack CallStack
HasCallStack => CallStack
callStack))

pprPanic :: HasCallStack => String -> SDoc -> a
-- ^ Throw an exception saying "bug in GHC"
pprPanic :: String -> SDoc -> a
pprPanic s :: String
s doc :: SDoc
doc = String -> SDoc -> a
forall a. String -> SDoc -> a
panicDoc String
s (SDoc
doc SDoc -> SDoc -> SDoc
$$ SDoc
HasCallStack => SDoc
callStackDoc)

pprSorry :: String -> SDoc -> a
-- ^ Throw an exception saying "this isn't finished yet"
pprSorry :: String -> SDoc -> a
pprSorry    = String -> SDoc -> a
forall a. String -> SDoc -> a
sorryDoc


pprPgmError :: String -> SDoc -> a
-- ^ Throw an exception saying "bug in pgm being compiled" (used for unusual program errors)
pprPgmError :: String -> SDoc -> a
pprPgmError = String -> SDoc -> a
forall a. String -> SDoc -> a
pgmErrorDoc

pprTraceDebug :: String -> SDoc -> a -> a
pprTraceDebug :: String -> SDoc -> a -> a
pprTraceDebug str :: String
str doc :: SDoc
doc x :: a
x
   | Bool
debugIsOn Bool -> Bool -> Bool
&& DynFlags -> Bool
hasPprDebug DynFlags
unsafeGlobalDynFlags = String -> SDoc -> a -> a
forall a. String -> SDoc -> a -> a
pprTrace String
str SDoc
doc a
x
   | Bool
otherwise                                     = a
x

pprTrace :: String -> SDoc -> a -> a
-- ^ If debug output is on, show some 'SDoc' on the screen
pprTrace :: String -> SDoc -> a -> a
pprTrace str :: String
str doc :: SDoc
doc x :: a
x
   | DynFlags -> Bool
hasNoDebugOutput DynFlags
unsafeGlobalDynFlags = a
x
   | Bool
otherwise                             =
      DynFlags -> (String -> a -> a) -> SDoc -> SDoc -> a -> a
forall a. DynFlags -> (String -> a) -> SDoc -> SDoc -> a
pprDebugAndThen DynFlags
unsafeGlobalDynFlags String -> a -> a
forall a. String -> a -> a
trace (String -> SDoc
text String
str) SDoc
doc a
x

pprTraceM :: Applicative f => String -> SDoc -> f ()
pprTraceM :: String -> SDoc -> f ()
pprTraceM str :: String
str doc :: SDoc
doc = String -> SDoc -> f () -> f ()
forall a. String -> SDoc -> a -> a
pprTrace String
str SDoc
doc (() -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ())

-- | @pprTraceIt desc x@ is equivalent to @pprTrace desc (ppr x) x@
pprTraceIt :: Outputable a => String -> a -> a
pprTraceIt :: String -> a -> a
pprTraceIt desc :: String
desc x :: a
x = String -> SDoc -> a -> a
forall a. String -> SDoc -> a -> a
pprTrace String
desc (a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
x) a
x

-- | @pprTraceException desc x action@ runs action, printing a message
-- if it throws an exception.
pprTraceException :: ExceptionMonad m => String -> SDoc -> m a -> m a
pprTraceException :: String -> SDoc -> m a -> m a
pprTraceException heading :: String
heading doc :: SDoc
doc =
    (GhcException -> m a) -> m a -> m a
forall (m :: * -> *) a.
ExceptionMonad m =>
(GhcException -> m a) -> m a -> m a
handleGhcException ((GhcException -> m a) -> m a -> m a)
-> (GhcException -> m a) -> m a -> m a
forall a b. (a -> b) -> a -> b
$ \exc :: GhcException
exc -> IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ do
        String -> IO ()
putStrLn (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ DynFlags -> SDoc -> String
showSDocDump DynFlags
unsafeGlobalDynFlags ([SDoc] -> SDoc
sep [String -> SDoc
text String
heading, Int -> SDoc -> SDoc
nest 2 SDoc
doc])
        GhcException -> IO a
forall a. GhcException -> IO a
throwGhcExceptionIO GhcException
exc

-- | If debug output is on, show some 'SDoc' on the screen along
-- with a call stack when available.
pprSTrace :: HasCallStack => SDoc -> a -> a
pprSTrace :: SDoc -> a -> a
pprSTrace doc :: SDoc
doc = String -> SDoc -> a -> a
forall a. String -> SDoc -> a -> a
pprTrace "" (SDoc
doc SDoc -> SDoc -> SDoc
$$ SDoc
HasCallStack => SDoc
callStackDoc)

warnPprTrace :: Bool -> String -> Int -> SDoc -> a -> a
-- ^ Just warn about an assertion failure, recording the given file and line number.
-- Should typically be accessed with the WARN macros
warnPprTrace :: Bool -> String -> Int -> SDoc -> a -> a
warnPprTrace _     _     _     _    x :: a
x | Bool -> Bool
not Bool
debugIsOn     = a
x
warnPprTrace _     _file :: String
_file _line :: Int
_line _msg :: SDoc
_msg x :: a
x
   | DynFlags -> Bool
hasNoDebugOutput DynFlags
unsafeGlobalDynFlags = a
x
warnPprTrace False _file :: String
_file _line :: Int
_line _msg :: SDoc
_msg x :: a
x = a
x
warnPprTrace True   file :: String
file  line :: Int
line  msg :: SDoc
msg x :: a
x
  = DynFlags -> (String -> a -> a) -> SDoc -> SDoc -> a -> a
forall a. DynFlags -> (String -> a) -> SDoc -> SDoc -> a
pprDebugAndThen DynFlags
unsafeGlobalDynFlags String -> a -> a
forall a. String -> a -> a
trace SDoc
heading SDoc
msg a
x
  where
    heading :: SDoc
heading = [SDoc] -> SDoc
hsep [String -> SDoc
text "WARNING: file", String -> SDoc
text String
file SDoc -> SDoc -> SDoc
<> SDoc
comma, String -> SDoc
text "line", Int -> SDoc
int Int
line]

-- | Panic with an assertation failure, recording the given file and
-- line number. Should typically be accessed with the ASSERT family of macros
assertPprPanic :: HasCallStack => String -> Int -> SDoc -> a
assertPprPanic :: String -> Int -> SDoc -> a
assertPprPanic _file :: String
_file _line :: Int
_line msg :: SDoc
msg
  = String -> SDoc -> a
forall a. HasCallStack => String -> SDoc -> a
pprPanic "ASSERT failed!" SDoc
msg

pprDebugAndThen :: DynFlags -> (String -> a) -> SDoc -> SDoc -> a
pprDebugAndThen :: DynFlags -> (String -> a) -> SDoc -> SDoc -> a
pprDebugAndThen dflags :: DynFlags
dflags cont :: String -> a
cont heading :: SDoc
heading pretty_msg :: SDoc
pretty_msg
 = String -> a
cont (DynFlags -> SDoc -> String
showSDocDump DynFlags
dflags SDoc
doc)
 where
     doc :: SDoc
doc = [SDoc] -> SDoc
sep [SDoc
heading, Int -> SDoc -> SDoc
nest 2 SDoc
pretty_msg]